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