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 (PHYSICAL_END - PHYSICAL_START), 6 * may 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) 2016, 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/hcall.h> 20 21 extern unsigned long stacktop; 22 extern void io_init(void); 23 extern void setup_args_progname(const char *args); 24 extern void setup_env(char *env, int size); 25 26 char *initrd; 27 u32 initrd_size; 28 29 u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) }; 30 int nr_cpus; 31 uint64_t tb_hz; 32 33 struct mem_region mem_regions[NR_MEM_REGIONS]; 34 phys_addr_t __physical_start, __physical_end; 35 unsigned __icache_bytes, __dcache_bytes; 36 37 struct cpu_set_params { 38 unsigned icache_bytes; 39 unsigned dcache_bytes; 40 uint64_t tb_hz; 41 }; 42 43 #define EXCEPTION_STACK_SIZE (32*1024) /* 32kB */ 44 45 static char exception_stack[NR_CPUS][EXCEPTION_STACK_SIZE]; 46 47 static void cpu_set(int fdtnode, u64 regval, void *info) 48 { 49 static bool read_common_info = false; 50 struct cpu_set_params *params = info; 51 int cpu = nr_cpus++; 52 53 if (cpu >= NR_CPUS) { 54 printf("Number cpus exceeds maximum supported (%d).\n", 55 NR_CPUS); 56 assert(0); 57 } 58 cpus[cpu] = regval; 59 60 /* set exception stack address for this CPU (in SPGR0) */ 61 62 asm volatile ("mtsprg0 %[addr]" :: 63 [addr] "r" (exception_stack[cpu + 1])); 64 65 if (!read_common_info) { 66 const struct fdt_property *prop; 67 u32 *data; 68 69 prop = fdt_get_property(dt_fdt(), fdtnode, 70 "i-cache-line-size", NULL); 71 assert(prop != NULL); 72 data = (u32 *)prop->data; 73 params->icache_bytes = fdt32_to_cpu(*data); 74 75 prop = fdt_get_property(dt_fdt(), fdtnode, 76 "d-cache-line-size", NULL); 77 assert(prop != NULL); 78 data = (u32 *)prop->data; 79 params->dcache_bytes = fdt32_to_cpu(*data); 80 81 prop = fdt_get_property(dt_fdt(), fdtnode, 82 "timebase-frequency", NULL); 83 assert(prop != NULL); 84 data = (u32 *)prop->data; 85 params->tb_hz = fdt32_to_cpu(*data); 86 87 read_common_info = true; 88 } 89 } 90 91 static void cpu_init(void) 92 { 93 struct cpu_set_params params; 94 int ret; 95 96 nr_cpus = 0; 97 ret = dt_for_each_cpu_node(cpu_set, ¶ms); 98 assert(ret == 0); 99 __icache_bytes = params.icache_bytes; 100 __dcache_bytes = params.dcache_bytes; 101 tb_hz = params.tb_hz; 102 103 /* Interrupt Endianness */ 104 105 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 106 hcall(H_SET_MODE, 1, 4, 0, 0); 107 #else 108 hcall(H_SET_MODE, 0, 4, 0, 0); 109 #endif 110 } 111 112 static void mem_init(phys_addr_t freemem_start) 113 { 114 struct dt_pbus_reg regs[NR_MEM_REGIONS]; 115 struct mem_region primary, mem = { 116 .start = (phys_addr_t)-1, 117 }; 118 int nr_regs, i; 119 120 nr_regs = dt_get_memory_params(regs, NR_MEM_REGIONS); 121 assert(nr_regs > 0); 122 123 primary.end = 0; 124 125 for (i = 0; i < nr_regs; ++i) { 126 mem_regions[i].start = regs[i].addr; 127 mem_regions[i].end = regs[i].addr + regs[i].size; 128 129 /* 130 * pick the region we're in for our primary region 131 */ 132 if (freemem_start >= mem_regions[i].start 133 && freemem_start < mem_regions[i].end) { 134 mem_regions[i].flags |= MR_F_PRIMARY; 135 primary = mem_regions[i]; 136 } 137 138 /* 139 * set the lowest and highest addresses found, 140 * ignoring potential gaps 141 */ 142 if (mem_regions[i].start < mem.start) 143 mem.start = mem_regions[i].start; 144 if (mem_regions[i].end > mem.end) 145 mem.end = mem_regions[i].end; 146 } 147 assert(primary.end != 0); 148 // assert(!(mem.start & ~PHYS_MASK) && !((mem.end - 1) & ~PHYS_MASK)); 149 150 __physical_start = mem.start; /* PHYSICAL_START */ 151 __physical_end = mem.end; /* PHYSICAL_END */ 152 153 phys_alloc_init(freemem_start, primary.end - freemem_start); 154 phys_alloc_set_minimum_alignment(__icache_bytes > __dcache_bytes 155 ? __icache_bytes : __dcache_bytes); 156 } 157 158 void setup(const void *fdt) 159 { 160 void *freemem = &stacktop; 161 const char *bootargs, *tmp; 162 u32 fdt_size; 163 int ret; 164 165 /* 166 * Before calling mem_init we need to move the fdt and initrd 167 * to safe locations. We move them to construct the memory 168 * map illustrated below: 169 * 170 * +----------------------+ <-- top of physical memory 171 * | | 172 * ~ ~ 173 * | | 174 * +----------------------+ <-- top of initrd 175 * | | 176 * +----------------------+ <-- top of FDT 177 * | | 178 * +----------------------+ <-- top of cpu0's stack 179 * | | 180 * +----------------------+ <-- top of text/data/bss sections, 181 * | | see arm/flat.lds 182 * | | 183 * +----------------------+ <-- load address 184 * | | 185 * +----------------------+ 186 */ 187 fdt_size = fdt_totalsize(fdt); 188 ret = fdt_move(fdt, freemem, fdt_size); 189 assert(ret == 0); 190 ret = dt_init(freemem); 191 assert(ret == 0); 192 freemem += fdt_size; 193 194 ret = dt_get_initrd(&tmp, &initrd_size); 195 assert(ret == 0 || ret == -FDT_ERR_NOTFOUND); 196 if (ret == 0) { 197 initrd = freemem; 198 memmove(initrd, tmp, initrd_size); 199 freemem += initrd_size; 200 } 201 202 /* call init functions */ 203 cpu_init(); 204 205 /* cpu_init must be called before mem_init */ 206 mem_init(PAGE_ALIGN((unsigned long)freemem)); 207 208 /* mem_init must be called before io_init */ 209 io_init(); 210 211 /* finish setup */ 212 ret = dt_get_bootargs(&bootargs); 213 assert(ret == 0 || ret == -FDT_ERR_NOTFOUND); 214 setup_args_progname(bootargs); 215 216 if (initrd) { 217 /* environ is currently the only file in the initrd */ 218 char *env = malloc(initrd_size); 219 memcpy(env, initrd, initrd_size); 220 setup_env(env, initrd_size); 221 } 222 } 223