xref: /kvm-unit-tests/lib/arm/setup.c (revision a823f36ac0c7143e1816973527d66b9d04257874)
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 <vmalloc.h>
20 #include <auxinfo.h>
21 #include <argv.h>
22 #include <asm/thread_info.h>
23 #include <asm/setup.h>
24 #include <asm/page.h>
25 #include <asm/processor.h>
26 #include <asm/smp.h>
27 #include <asm/timer.h>
28 #include <asm/psci.h>
29 
30 #include "io.h"
31 
32 #define MAX_DT_MEM_REGIONS	16
33 #define NR_EXTRA_MEM_REGIONS	16
34 #define NR_INITIAL_MEM_REGIONS	(MAX_DT_MEM_REGIONS + NR_EXTRA_MEM_REGIONS)
35 
36 extern unsigned long _etext;
37 
38 char *initrd;
39 u32 initrd_size;
40 
41 u64 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (u64)~0 };
42 int nr_cpus;
43 
44 static struct mem_region __initial_mem_regions[NR_INITIAL_MEM_REGIONS + 1];
45 struct mem_region *mem_regions = __initial_mem_regions;
46 phys_addr_t __phys_offset, __phys_end;
47 
48 int mpidr_to_cpu(uint64_t mpidr)
49 {
50 	int i;
51 
52 	for (i = 0; i < nr_cpus; ++i)
53 		if (cpus[i] == (mpidr & MPIDR_HWID_BITMASK))
54 			return i;
55 	return -1;
56 }
57 
58 static void cpu_set_fdt(int fdtnode __unused, u64 regval, void *info __unused)
59 {
60 	int cpu = nr_cpus++;
61 
62 	assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS);
63 
64 	cpus[cpu] = regval;
65 	set_cpu_present(cpu, true);
66 }
67 
68 #ifdef CONFIG_EFI
69 
70 #include <acpi.h>
71 
72 static int cpu_set_acpi(struct acpi_subtable_header *header)
73 {
74 	int cpu = nr_cpus++;
75 	struct acpi_madt_generic_interrupt *gicc = (void *)header;
76 
77 	assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS);
78 
79 	cpus[cpu] = gicc->arm_mpidr;
80 	set_cpu_present(cpu, true);
81 
82 	return 0;
83 }
84 
85 static void cpu_init_acpi(void)
86 {
87 	acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT, cpu_set_acpi);
88 }
89 
90 #else
91 
92 static void cpu_init_acpi(void)
93 {
94 	assert_msg(false, "ACPI not available");
95 }
96 
97 #endif
98 
99 static void cpu_init(void)
100 {
101 	int ret;
102 
103 	nr_cpus = 0;
104 	if (dt_available()) {
105 		ret = dt_for_each_cpu_node(cpu_set_fdt, NULL);
106 		assert(ret == 0);
107 	} else {
108 		cpu_init_acpi();
109 	}
110 
111 	set_cpu_online(0, true);
112 }
113 
114 static void mem_region_add(struct mem_region *r)
115 {
116 	struct mem_region *r_next = mem_regions;
117 	int i = 0;
118 
119 	for (; r_next->end; ++r_next, ++i)
120 		;
121 	assert(i < NR_INITIAL_MEM_REGIONS);
122 
123 	*r_next = *r;
124 }
125 
126 static void mem_regions_add_dt_regions(void)
127 {
128 	struct dt_pbus_reg regs[MAX_DT_MEM_REGIONS];
129 	int nr_regs, i;
130 
131 	nr_regs = dt_get_memory_params(regs, MAX_DT_MEM_REGIONS);
132 	assert(nr_regs > 0);
133 
134 	for (i = 0; i < nr_regs; ++i) {
135 		mem_region_add(&(struct mem_region){
136 			.start = regs[i].addr,
137 			.end = regs[i].addr + regs[i].size,
138 		});
139 	}
140 }
141 
142 struct mem_region *mem_region_find(phys_addr_t paddr)
143 {
144 	struct mem_region *r;
145 
146 	for (r = mem_regions; r->end; ++r)
147 		if (paddr >= r->start && paddr < r->end)
148 			return r;
149 	return NULL;
150 }
151 
152 unsigned int mem_region_get_flags(phys_addr_t paddr)
153 {
154 	struct mem_region *r = mem_region_find(paddr);
155 	return r ? r->flags : MR_F_UNKNOWN;
156 }
157 
158 static void mem_regions_add_assumed(void)
159 {
160 	phys_addr_t code_end = (phys_addr_t)(unsigned long)&_etext;
161 	struct mem_region *r;
162 
163 	r = mem_region_find(code_end - 1);
164 	assert(r);
165 
166 	/* Split the region with the code into two regions; code and data */
167 	mem_region_add(&(struct mem_region){
168 		.start = code_end,
169 		.end = r->end,
170 	});
171 	*r = (struct mem_region){
172 		.start = r->start,
173 		.end = code_end,
174 		.flags = MR_F_CODE,
175 	};
176 
177 	/*
178 	 * mach-virt I/O regions:
179 	 *   - The first 1G (arm/arm64)
180 	 *   - 512M at 256G (arm64, arm uses highmem=off)
181 	 *   - 512G at 512G (arm64, arm uses highmem=off)
182 	 */
183 	mem_region_add(&(struct mem_region){ 0, (1ul << 30), MR_F_IO });
184 #ifdef __aarch64__
185 	mem_region_add(&(struct mem_region){ (1ul << 38), (1ul << 38) | (1ul << 29), MR_F_IO });
186 	mem_region_add(&(struct mem_region){ (1ul << 39), (1ul << 40), MR_F_IO });
187 #endif
188 }
189 
190 static void mem_init(phys_addr_t freemem_start)
191 {
192 	phys_addr_t base, top;
193 	struct mem_region *freemem, *r, mem = {
194 		.start = (phys_addr_t)-1,
195 	};
196 
197 	freemem = mem_region_find(freemem_start);
198 	assert(freemem && !(freemem->flags & (MR_F_IO | MR_F_CODE)));
199 
200 	for (r = mem_regions; r->end; ++r) {
201 		if (!(r->flags & MR_F_IO)) {
202 			if (r->start < mem.start)
203 				mem.start = r->start;
204 			if (r->end > mem.end)
205 				mem.end = r->end;
206 		}
207 	}
208 	assert(mem.end && !(mem.start & ~PHYS_MASK));
209 	mem.end &= PHYS_MASK;
210 
211 	/* Check for holes */
212 	r = mem_region_find(mem.start);
213 	while (r && r->end != mem.end)
214 		r = mem_region_find(r->end);
215 	assert(r);
216 
217 	/* Ensure our selected freemem range is somewhere in our full range */
218 	assert(freemem_start >= mem.start && freemem->end <= mem.end);
219 
220 	__phys_offset = mem.start;	/* PHYS_OFFSET */
221 	__phys_end = mem.end;		/* PHYS_END */
222 
223 	phys_alloc_init(freemem_start, freemem->end - freemem_start);
224 	phys_alloc_set_minimum_alignment(SMP_CACHE_BYTES);
225 
226 	phys_alloc_get_unused(&base, &top);
227 	base = PAGE_ALIGN(base);
228 	top = top & PAGE_MASK;
229 	assert(sizeof(long) == 8 || !(base >> 32));
230 	if (sizeof(long) != 8 && (top >> 32) != 0)
231 		top = ((uint64_t)1 << 32);
232 	page_alloc_init_area(0, base >> PAGE_SHIFT, top >> PAGE_SHIFT);
233 	page_alloc_ops_enable();
234 }
235 
236 void setup(const void *fdt, phys_addr_t freemem_start)
237 {
238 	void *freemem;
239 	const char *bootargs, *tmp;
240 	u32 fdt_size;
241 	int ret;
242 
243 	assert(sizeof(long) == 8 || freemem_start < (3ul << 30));
244 	freemem = (void *)(unsigned long)freemem_start;
245 
246 	/* Move the FDT to the base of free memory */
247 	fdt_size = fdt_totalsize(fdt);
248 	ret = fdt_move(fdt, freemem, fdt_size);
249 	assert(ret == 0);
250 	ret = dt_init(freemem);
251 	assert(ret == 0);
252 	freemem += fdt_size;
253 
254 	/* Move the initrd to the top of the FDT */
255 	ret = dt_get_initrd(&tmp, &initrd_size);
256 	assert(ret == 0 || ret == -FDT_ERR_NOTFOUND);
257 	if (ret == 0) {
258 		initrd = freemem;
259 		memmove(initrd, tmp, initrd_size);
260 		freemem += initrd_size;
261 	}
262 
263 	mem_regions_add_dt_regions();
264 	mem_regions_add_assumed();
265 	mem_init(PAGE_ALIGN((unsigned long)freemem));
266 
267 	psci_set_conduit();
268 	cpu_init();
269 
270 	/* cpu_init must be called before thread_info_init */
271 	thread_info_init(current_thread_info(), 0);
272 
273 	/* mem_init must be called before io_init */
274 	io_init();
275 
276 	timer_save_state();
277 
278 	ret = dt_get_bootargs(&bootargs);
279 	assert(ret == 0 || ret == -FDT_ERR_NOTFOUND);
280 	setup_args_progname(bootargs);
281 
282 	if (initrd) {
283 		/* environ is currently the only file in the initrd */
284 		char *env = malloc(initrd_size);
285 		memcpy(env, initrd, initrd_size);
286 		setup_env(env, initrd_size);
287 	}
288 
289 	if (!(auxinfo.flags & AUXINFO_MMU_OFF))
290 		setup_vm();
291 }
292