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 <alloc_page.h> 19 #include <argv.h> 20 #include <asm/thread_info.h> 21 #include <asm/setup.h> 22 #include <asm/page.h> 23 #include <asm/processor.h> 24 #include <asm/smp.h> 25 26 #include "io.h" 27 28 #define NR_INITIAL_MEM_REGIONS 16 29 30 extern unsigned long stacktop; 31 32 char *initrd; 33 u32 initrd_size; 34 35 u64 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (u64)~0 }; 36 int nr_cpus; 37 38 static struct mem_region __initial_mem_regions[NR_INITIAL_MEM_REGIONS + 1]; 39 struct mem_region *mem_regions = __initial_mem_regions; 40 phys_addr_t __phys_offset, __phys_end; 41 42 u32 dcache_line_size; 43 44 int mpidr_to_cpu(uint64_t mpidr) 45 { 46 int i; 47 48 for (i = 0; i < nr_cpus; ++i) 49 if (cpus[i] == (mpidr & MPIDR_HWID_BITMASK)) 50 return i; 51 return -1; 52 } 53 54 static void cpu_set(int fdtnode __unused, u64 regval, void *info __unused) 55 { 56 int cpu = nr_cpus++; 57 58 assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS); 59 60 cpus[cpu] = regval; 61 set_cpu_present(cpu, true); 62 } 63 64 static void cpu_init(void) 65 { 66 int ret; 67 68 nr_cpus = 0; 69 ret = dt_for_each_cpu_node(cpu_set, NULL); 70 assert(ret == 0); 71 set_cpu_online(0, true); 72 /* 73 * DminLine is log2 of the number of words in the smallest cache line; a 74 * word is 4 bytes. 75 */ 76 dcache_line_size = 1 << (CTR_DMINLINE(get_ctr()) + 2); 77 } 78 79 unsigned int mem_region_get_flags(phys_addr_t paddr) 80 { 81 struct mem_region *r; 82 83 for (r = mem_regions; r->end; ++r) { 84 if (paddr >= r->start && paddr < r->end) 85 return r->flags; 86 } 87 88 return MR_F_UNKNOWN; 89 } 90 91 static void mem_init(phys_addr_t freemem_start) 92 { 93 struct dt_pbus_reg regs[NR_INITIAL_MEM_REGIONS]; 94 struct mem_region primary, mem = { 95 .start = (phys_addr_t)-1, 96 }; 97 phys_addr_t base, top; 98 int nr_regs, nr_io = 0, i; 99 100 /* 101 * mach-virt I/O regions: 102 * - The first 1G (arm/arm64) 103 * - 512M at 256G (arm64, arm uses highmem=off) 104 * - 512G at 512G (arm64, arm uses highmem=off) 105 */ 106 mem_regions[nr_io++] = (struct mem_region){ 0, (1ul << 30), MR_F_IO }; 107 #ifdef __aarch64__ 108 mem_regions[nr_io++] = (struct mem_region){ (1ul << 38), (1ul << 38) | (1ul << 29), MR_F_IO }; 109 mem_regions[nr_io++] = (struct mem_region){ (1ul << 39), (1ul << 40), MR_F_IO }; 110 #endif 111 112 nr_regs = dt_get_memory_params(regs, NR_INITIAL_MEM_REGIONS - nr_io); 113 assert(nr_regs > 0); 114 115 primary = (struct mem_region){ 0 }; 116 117 for (i = 0; i < nr_regs; ++i) { 118 struct mem_region *r = &mem_regions[nr_io + i]; 119 120 r->start = regs[i].addr; 121 r->end = regs[i].addr + regs[i].size; 122 123 /* 124 * pick the region we're in for our primary region 125 */ 126 if (freemem_start >= r->start && freemem_start < r->end) { 127 r->flags |= MR_F_PRIMARY; 128 primary = *r; 129 } 130 131 /* 132 * set the lowest and highest addresses found, 133 * ignoring potential gaps 134 */ 135 if (r->start < mem.start) 136 mem.start = r->start; 137 if (r->end > mem.end) 138 mem.end = r->end; 139 } 140 assert(primary.end != 0); 141 assert(!(mem.start & ~PHYS_MASK) && !((mem.end - 1) & ~PHYS_MASK)); 142 143 __phys_offset = primary.start; /* PHYS_OFFSET */ 144 __phys_end = primary.end; /* PHYS_END */ 145 146 phys_alloc_init(freemem_start, primary.end - freemem_start); 147 phys_alloc_set_minimum_alignment(SMP_CACHE_BYTES); 148 149 phys_alloc_get_unused(&base, &top); 150 base = PAGE_ALIGN(base); 151 top = top & PAGE_MASK; 152 assert(sizeof(long) == 8 || !(base >> 32)); 153 if (sizeof(long) != 8 && (top >> 32) != 0) 154 top = ((uint64_t)1 << 32); 155 free_pages((void *)(unsigned long)base, top - base); 156 page_alloc_ops_enable(); 157 } 158 159 void setup(const void *fdt) 160 { 161 void *freemem = &stacktop; 162 const char *bootargs, *tmp; 163 u32 fdt_size; 164 int ret; 165 166 /* 167 * Before calling mem_init we need to move the fdt and initrd 168 * to safe locations. We move them to construct the memory 169 * map illustrated below: 170 * 171 * +----------------------+ <-- top of physical memory 172 * | | 173 * ~ ~ 174 * | | 175 * +----------------------+ <-- top of initrd 176 * | | 177 * +----------------------+ <-- top of FDT 178 * | | 179 * +----------------------+ <-- top of cpu0's stack 180 * | | 181 * +----------------------+ <-- top of text/data/bss sections, 182 * | | see arm/flat.lds 183 * | | 184 * +----------------------+ <-- load address 185 * | | 186 * +----------------------+ 187 */ 188 fdt_size = fdt_totalsize(fdt); 189 ret = fdt_move(fdt, freemem, fdt_size); 190 assert(ret == 0); 191 ret = dt_init(freemem); 192 assert(ret == 0); 193 freemem += fdt_size; 194 195 ret = dt_get_initrd(&tmp, &initrd_size); 196 assert(ret == 0 || ret == -FDT_ERR_NOTFOUND); 197 if (ret == 0) { 198 initrd = freemem; 199 memmove(initrd, tmp, initrd_size); 200 freemem += initrd_size; 201 } 202 203 /* call init functions */ 204 mem_init(PAGE_ALIGN((unsigned long)freemem)); 205 cpu_init(); 206 207 /* cpu_init must be called before thread_info_init */ 208 thread_info_init(current_thread_info(), 0); 209 210 /* mem_init must be called before io_init */ 211 io_init(); 212 213 /* finish setup */ 214 ret = dt_get_bootargs(&bootargs); 215 assert(ret == 0 || ret == -FDT_ERR_NOTFOUND); 216 setup_args_progname(bootargs); 217 218 if (initrd) { 219 /* environ is currently the only file in the initrd */ 220 char *env = malloc(initrd_size); 221 memcpy(env, initrd, initrd_size); 222 setup_env(env, initrd_size); 223 } 224 } 225