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