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