xref: /kvm-unit-tests/lib/arm/setup.c (revision 728e71ee457384eb1cf2d5111d1e4329498581db)
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/smp.h>
24 
25 #include "io.h"
26 
27 extern unsigned long stacktop;
28 
29 char *initrd;
30 u32 initrd_size;
31 
32 u64 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (u64)~0 };
33 int nr_cpus;
34 
35 struct mem_region mem_regions[NR_MEM_REGIONS];
36 phys_addr_t __phys_offset, __phys_end;
37 
38 int mpidr_to_cpu(uint64_t mpidr)
39 {
40 	int i;
41 
42 	for (i = 0; i < nr_cpus; ++i)
43 		if (cpus[i] == (mpidr & MPIDR_HWID_BITMASK))
44 			return i;
45 	return -1;
46 }
47 
48 static void cpu_set(int fdtnode __unused, u64 regval, void *info __unused)
49 {
50 	int cpu = nr_cpus++;
51 
52 	assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS);
53 
54 	cpus[cpu] = regval;
55 	set_cpu_present(cpu, true);
56 }
57 
58 static void cpu_init(void)
59 {
60 	int ret;
61 
62 	nr_cpus = 0;
63 	ret = dt_for_each_cpu_node(cpu_set, NULL);
64 	assert(ret == 0);
65 	set_cpu_online(0, true);
66 }
67 
68 static void mem_init(phys_addr_t freemem_start)
69 {
70 	struct dt_pbus_reg regs[NR_MEM_REGIONS];
71 	struct mem_region primary, mem = {
72 		.start = (phys_addr_t)-1,
73 	};
74 	phys_addr_t base, top;
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 = (struct mem_region){ 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 = primary.start;	/* PHYS_OFFSET */
108 	__phys_end = primary.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 	phys_alloc_get_unused(&base, &top);
114 	base = PAGE_ALIGN(base);
115 	top = top & PAGE_MASK;
116 	assert(sizeof(long) == 8 || !(base >> 32));
117 	if (sizeof(long) != 8 && (top >> 32) != 0)
118 		top = ((uint64_t)1 << 32);
119 	free_pages((void *)(unsigned long)base, top - base);
120 	page_alloc_ops_enable();
121 }
122 
123 void setup(const void *fdt)
124 {
125 	void *freemem = &stacktop;
126 	const char *bootargs, *tmp;
127 	u32 fdt_size;
128 	int ret;
129 
130 	/*
131 	 * Before calling mem_init we need to move the fdt and initrd
132 	 * to safe locations. We move them to construct the memory
133 	 * map illustrated below:
134 	 *
135 	 *    +----------------------+   <-- top of physical memory
136 	 *    |                      |
137 	 *    ~                      ~
138 	 *    |                      |
139 	 *    +----------------------+   <-- top of initrd
140 	 *    |                      |
141 	 *    +----------------------+   <-- top of FDT
142 	 *    |                      |
143 	 *    +----------------------+   <-- top of cpu0's stack
144 	 *    |                      |
145 	 *    +----------------------+   <-- top of text/data/bss sections,
146 	 *    |                      |       see arm/flat.lds
147 	 *    |                      |
148 	 *    +----------------------+   <-- load address
149 	 *    |                      |
150 	 *    +----------------------+
151 	 */
152 	fdt_size = fdt_totalsize(fdt);
153 	ret = fdt_move(fdt, freemem, fdt_size);
154 	assert(ret == 0);
155 	ret = dt_init(freemem);
156 	assert(ret == 0);
157 	freemem += fdt_size;
158 
159 	ret = dt_get_initrd(&tmp, &initrd_size);
160 	assert(ret == 0 || ret == -FDT_ERR_NOTFOUND);
161 	if (ret == 0) {
162 		initrd = freemem;
163 		memmove(initrd, tmp, initrd_size);
164 		freemem += initrd_size;
165 	}
166 
167 	/* call init functions */
168 	mem_init(PAGE_ALIGN((unsigned long)freemem));
169 	cpu_init();
170 
171 	/* cpu_init must be called before thread_info_init */
172 	thread_info_init(current_thread_info(), 0);
173 
174 	/* mem_init must be called before io_init */
175 	io_init();
176 
177 	/* finish setup */
178 	ret = dt_get_bootargs(&bootargs);
179 	assert(ret == 0 || ret == -FDT_ERR_NOTFOUND);
180 	setup_args_progname(bootargs);
181 
182 	if (initrd) {
183 		/* environ is currently the only file in the initrd */
184 		char *env = malloc(initrd_size);
185 		memcpy(env, initrd, initrd_size);
186 		setup_env(env, initrd_size);
187 	}
188 }
189