1 /* 2 * Initialize machine setup information and I/O. 3 * 4 * After running setup() unit tests may query how many cpus they have 5 * (nr_cpus), how much memory they have (PHYS_END - PHYS_OFFSET), may 6 * use dynamic memory allocation (malloc, etc.), printf, and exit. 7 * Finally, argc and argv are also ready to be passed to main(). 8 * 9 * Copyright (C) 2014, Red Hat Inc, Andrew Jones <drjones@redhat.com> 10 * 11 * This work is licensed under the terms of the GNU LGPL, version 2. 12 */ 13 #include <libcflat.h> 14 #include <libfdt/libfdt.h> 15 #include <devicetree.h> 16 #include <alloc.h> 17 #include <asm/thread_info.h> 18 #include <asm/setup.h> 19 #include <asm/page.h> 20 #include <asm/mmu.h> 21 22 extern unsigned long stacktop; 23 extern void io_init(void); 24 extern void setup_args(const char *args); 25 26 u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) }; 27 int nr_cpus; 28 29 phys_addr_t __phys_offset, __phys_end; 30 31 static void cpu_set(int fdtnode __unused, u32 regval, void *info __unused) 32 { 33 assert(nr_cpus < NR_CPUS); 34 cpus[nr_cpus++] = regval; 35 } 36 37 static void cpu_init(void) 38 { 39 nr_cpus = 0; 40 assert(dt_for_each_cpu_node(cpu_set, NULL) == 0); 41 } 42 43 static void mem_init(phys_addr_t freemem_start) 44 { 45 /* we only expect one membank to be defined in the DT */ 46 struct dt_pbus_reg regs[1]; 47 phys_addr_t mem_start, mem_end; 48 49 assert(dt_get_memory_params(regs, 1)); 50 51 mem_start = regs[0].addr; 52 mem_end = mem_start + regs[0].size; 53 54 assert(!(mem_start & ~PHYS_MASK) && !((mem_end-1) & ~PHYS_MASK)); 55 assert(freemem_start >= mem_start && freemem_start < mem_end); 56 57 __phys_offset = mem_start; /* PHYS_OFFSET */ 58 __phys_end = mem_end; /* PHYS_END */ 59 60 phys_alloc_init(freemem_start, mem_end - freemem_start); 61 phys_alloc_set_minimum_alignment(SMP_CACHE_BYTES); 62 63 mmu_enable_idmap(); 64 } 65 66 void setup(const void *fdt) 67 { 68 const char *bootargs; 69 u32 fdt_size; 70 71 /* 72 * Move the fdt to just above the stack. The free memory 73 * then starts just after the fdt. 74 */ 75 fdt_size = fdt_totalsize(fdt); 76 assert(fdt_move(fdt, &stacktop, fdt_size) == 0); 77 assert(dt_init(&stacktop) == 0); 78 79 mem_init(PAGE_ALIGN((unsigned long)&stacktop + fdt_size)); 80 io_init(); 81 cpu_init(); 82 83 thread_info_init(current_thread_info(), 0); 84 85 assert(dt_get_bootargs(&bootargs) == 0); 86 setup_args(bootargs); 87 } 88