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/setup.h> 18 #include <asm/page.h> 19 #include <asm/mmu.h> 20 21 extern unsigned long stacktop; 22 extern void io_init(void); 23 extern void setup_args(const char *args); 24 25 u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) }; 26 int nr_cpus; 27 28 phys_addr_t __phys_offset, __phys_end; 29 30 static void cpu_set(int fdtnode __unused, u32 regval, void *info __unused) 31 { 32 assert(nr_cpus < NR_CPUS); 33 cpus[nr_cpus++] = regval; 34 } 35 36 static void cpu_init(void) 37 { 38 nr_cpus = 0; 39 assert(dt_for_each_cpu_node(cpu_set, NULL) == 0); 40 } 41 42 static void mem_init(phys_addr_t freemem_start) 43 { 44 /* we only expect one membank to be defined in the DT */ 45 struct dt_pbus_reg regs[1]; 46 phys_addr_t mem_start, mem_end; 47 48 assert(dt_get_memory_params(regs, 1)); 49 50 mem_start = regs[0].addr; 51 mem_end = mem_start + regs[0].size; 52 53 assert(!(mem_start & ~PHYS_MASK) && !((mem_end-1) & ~PHYS_MASK)); 54 assert(freemem_start >= mem_start && freemem_start < mem_end); 55 56 __phys_offset = mem_start; /* PHYS_OFFSET */ 57 __phys_end = mem_end; /* PHYS_END */ 58 59 phys_alloc_init(freemem_start, mem_end - freemem_start); 60 phys_alloc_set_minimum_alignment(SMP_CACHE_BYTES); 61 62 mmu_enable_idmap(); 63 } 64 65 void setup(const void *fdt) 66 { 67 const char *bootargs; 68 u32 fdt_size; 69 70 /* 71 * Move the fdt to just above the stack. The free memory 72 * then starts just after the fdt. 73 */ 74 fdt_size = fdt_totalsize(fdt); 75 assert(fdt_move(fdt, &stacktop, fdt_size) == 0); 76 assert(dt_init(&stacktop) == 0); 77 78 mem_init(PAGE_ALIGN((unsigned long)&stacktop + fdt_size)); 79 io_init(); 80 cpu_init(); 81 82 assert(dt_get_bootargs(&bootargs) == 0); 83 setup_args(bootargs); 84 } 85