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 <alloc_phys.h> 18 #include <argv.h> 19 #include <asm/thread_info.h> 20 #include <asm/setup.h> 21 #include <asm/page.h> 22 #include <asm/mmu.h> 23 #include <asm/smp.h> 24 25 extern unsigned long stacktop; 26 extern void io_init(void); 27 28 char *initrd; 29 u32 initrd_size; 30 31 u64 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (u64)~0 }; 32 int nr_cpus; 33 34 struct mem_region mem_regions[NR_MEM_REGIONS]; 35 phys_addr_t __phys_offset, __phys_end; 36 37 int mpidr_to_cpu(uint64_t mpidr) 38 { 39 int i; 40 41 for (i = 0; i < nr_cpus; ++i) 42 if (cpus[i] == (mpidr & MPIDR_HWID_BITMASK)) 43 return i; 44 return -1; 45 } 46 47 static void cpu_set(int fdtnode __unused, u64 regval, void *info __unused) 48 { 49 int cpu = nr_cpus++; 50 51 assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS); 52 53 cpus[cpu] = regval; 54 set_cpu_present(cpu, true); 55 } 56 57 static void cpu_init(void) 58 { 59 int ret; 60 61 nr_cpus = 0; 62 ret = dt_for_each_cpu_node(cpu_set, NULL); 63 assert(ret == 0); 64 set_cpu_online(0, true); 65 } 66 67 static void mem_init(phys_addr_t freemem_start) 68 { 69 struct dt_pbus_reg regs[NR_MEM_REGIONS]; 70 struct mem_region primary, mem = { 71 .start = (phys_addr_t)-1, 72 }; 73 int nr_regs, i; 74 75 nr_regs = dt_get_memory_params(regs, NR_MEM_REGIONS); 76 assert(nr_regs > 0); 77 78 primary = (struct mem_region){ 0 }; 79 80 for (i = 0; i < nr_regs; ++i) { 81 mem_regions[i].start = regs[i].addr; 82 mem_regions[i].end = regs[i].addr + regs[i].size; 83 84 /* 85 * pick the region we're in for our primary region 86 */ 87 if (freemem_start >= mem_regions[i].start 88 && freemem_start < mem_regions[i].end) { 89 mem_regions[i].flags |= MR_F_PRIMARY; 90 primary = mem_regions[i]; 91 } 92 93 /* 94 * set the lowest and highest addresses found, 95 * ignoring potential gaps 96 */ 97 if (mem_regions[i].start < mem.start) 98 mem.start = mem_regions[i].start; 99 if (mem_regions[i].end > mem.end) 100 mem.end = mem_regions[i].end; 101 } 102 assert(primary.end != 0); 103 assert(!(mem.start & ~PHYS_MASK) && !((mem.end - 1) & ~PHYS_MASK)); 104 105 __phys_offset = primary.start; /* PHYS_OFFSET */ 106 __phys_end = primary.end; /* PHYS_END */ 107 108 phys_alloc_init(freemem_start, primary.end - freemem_start); 109 phys_alloc_set_minimum_alignment(SMP_CACHE_BYTES); 110 111 setup_vm(); 112 } 113 114 void setup(const void *fdt) 115 { 116 void *freemem = &stacktop; 117 const char *bootargs, *tmp; 118 u32 fdt_size; 119 int ret; 120 121 /* 122 * Before calling mem_init we need to move the fdt and initrd 123 * to safe locations. We move them to construct the memory 124 * map illustrated below: 125 * 126 * +----------------------+ <-- top of physical memory 127 * | | 128 * ~ ~ 129 * | | 130 * +----------------------+ <-- top of initrd 131 * | | 132 * +----------------------+ <-- top of FDT 133 * | | 134 * +----------------------+ <-- top of cpu0's stack 135 * | | 136 * +----------------------+ <-- top of text/data/bss sections, 137 * | | see arm/flat.lds 138 * | | 139 * +----------------------+ <-- load address 140 * | | 141 * +----------------------+ 142 */ 143 fdt_size = fdt_totalsize(fdt); 144 ret = fdt_move(fdt, freemem, fdt_size); 145 assert(ret == 0); 146 ret = dt_init(freemem); 147 assert(ret == 0); 148 freemem += fdt_size; 149 150 ret = dt_get_initrd(&tmp, &initrd_size); 151 assert(ret == 0 || ret == -FDT_ERR_NOTFOUND); 152 if (ret == 0) { 153 initrd = freemem; 154 memmove(initrd, tmp, initrd_size); 155 freemem += initrd_size; 156 } 157 158 /* call init functions */ 159 cpu_init(); 160 161 /* cpu_init must be called before thread_info_init */ 162 thread_info_init(current_thread_info(), 0); 163 164 /* thread_info_init must be called before mem_init */ 165 mem_init(PAGE_ALIGN((unsigned long)freemem)); 166 167 /* mem_init must be called before io_init */ 168 io_init(); 169 170 /* finish setup */ 171 ret = dt_get_bootargs(&bootargs); 172 assert(ret == 0 || ret == -FDT_ERR_NOTFOUND); 173 setup_args_progname(bootargs); 174 175 if (initrd) { 176 /* environ is currently the only file in the initrd */ 177 char *env = malloc(initrd_size); 178 memcpy(env, initrd, initrd_size); 179 setup_env(env, initrd_size); 180 } 181 } 182