xref: /kvm-unit-tests/lib/powerpc/setup.c (revision 4ff26ec5da572134631ddae4fd4303feadba45ac)
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 (PHYSICAL_END - PHYSICAL_START),
6  * may 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) 2016, 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 <argv.h>
19 #include <asm/setup.h>
20 #include <asm/page.h>
21 #include <asm/hcall.h>
22 #include "io.h"
23 
24 extern unsigned long stacktop;
25 
26 char *initrd;
27 u32 initrd_size;
28 
29 u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) };
30 int nr_cpus;
31 uint64_t tb_hz;
32 
33 struct mem_region mem_regions[NR_MEM_REGIONS];
34 phys_addr_t __physical_start, __physical_end;
35 unsigned __icache_bytes, __dcache_bytes;
36 
37 struct cpu_set_params {
38 	unsigned icache_bytes;
39 	unsigned dcache_bytes;
40 	uint64_t tb_hz;
41 };
42 
43 #define EXCEPTION_STACK_SIZE	(32*1024) /* 32kB */
44 
45 static char exception_stack[NR_CPUS][EXCEPTION_STACK_SIZE];
46 
47 static void cpu_set(int fdtnode, u64 regval, void *info)
48 {
49 	static bool read_common_info = false;
50 	struct cpu_set_params *params = info;
51 	int cpu = nr_cpus++;
52 
53 	assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS);
54 
55 	cpus[cpu] = regval;
56 
57 	/* set exception stack address for this CPU (in SPGR0) */
58 	asm volatile ("mtsprg0 %[addr]" ::
59 		      [addr] "r" (exception_stack[cpu + 1]));
60 
61 	if (!read_common_info) {
62 		const struct fdt_property *prop;
63 		u32 *data;
64 
65 		prop = fdt_get_property(dt_fdt(), fdtnode,
66 					"i-cache-line-size", NULL);
67 		assert(prop != NULL);
68 		data = (u32 *)prop->data;
69 		params->icache_bytes = fdt32_to_cpu(*data);
70 
71 		prop = fdt_get_property(dt_fdt(), fdtnode,
72 					"d-cache-line-size", NULL);
73 		assert(prop != NULL);
74 		data = (u32 *)prop->data;
75 		params->dcache_bytes = fdt32_to_cpu(*data);
76 
77 		prop = fdt_get_property(dt_fdt(), fdtnode,
78 					"timebase-frequency", NULL);
79 		assert(prop != NULL);
80 		data = (u32 *)prop->data;
81 		params->tb_hz = fdt32_to_cpu(*data);
82 
83 		read_common_info = true;
84 	}
85 }
86 
87 static void cpu_init(void)
88 {
89 	struct cpu_set_params params;
90 	int ret;
91 
92 	nr_cpus = 0;
93 	ret = dt_for_each_cpu_node(cpu_set, &params);
94 	assert(ret == 0);
95 	__icache_bytes = params.icache_bytes;
96 	__dcache_bytes = params.dcache_bytes;
97 	tb_hz = params.tb_hz;
98 
99 	/* Interrupt Endianness */
100 
101 #if  __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
102         hcall(H_SET_MODE, 1, 4, 0, 0);
103 #else
104         hcall(H_SET_MODE, 0, 4, 0, 0);
105 #endif
106 }
107 
108 static void mem_init(phys_addr_t freemem_start)
109 {
110 	struct dt_pbus_reg regs[NR_MEM_REGIONS];
111 	struct mem_region primary, mem = {
112 		.start = (phys_addr_t)-1,
113 	};
114 	int nr_regs, i;
115 
116 	nr_regs = dt_get_memory_params(regs, NR_MEM_REGIONS);
117 	assert(nr_regs > 0);
118 
119 	primary.end = 0;
120 
121 	for (i = 0; i < nr_regs; ++i) {
122 		mem_regions[i].start = regs[i].addr;
123 		mem_regions[i].end = regs[i].addr + regs[i].size;
124 
125 		/*
126 		 * pick the region we're in for our primary region
127 		 */
128 		if (freemem_start >= mem_regions[i].start
129 				&& freemem_start < mem_regions[i].end) {
130 			mem_regions[i].flags |= MR_F_PRIMARY;
131 			primary = mem_regions[i];
132 		}
133 
134 		/*
135 		 * set the lowest and highest addresses found,
136 		 * ignoring potential gaps
137 		 */
138 		if (mem_regions[i].start < mem.start)
139 			mem.start = mem_regions[i].start;
140 		if (mem_regions[i].end > mem.end)
141 			mem.end = mem_regions[i].end;
142 	}
143 	assert(primary.end != 0);
144 //	assert(!(mem.start & ~PHYS_MASK) && !((mem.end - 1) & ~PHYS_MASK));
145 
146 	__physical_start = mem.start;	/* PHYSICAL_START */
147 	__physical_end = mem.end;	/* PHYSICAL_END */
148 
149 	phys_alloc_init(freemem_start, primary.end - freemem_start);
150 	phys_alloc_set_minimum_alignment(__icache_bytes > __dcache_bytes
151 					 ? __icache_bytes : __dcache_bytes);
152 }
153 
154 void setup(const void *fdt)
155 {
156 	void *freemem = &stacktop;
157 	const char *bootargs, *tmp;
158 	u32 fdt_size;
159 	int ret;
160 
161 	/*
162 	 * Before calling mem_init we need to move the fdt and initrd
163 	 * to safe locations. We move them to construct the memory
164 	 * map illustrated below:
165 	 *
166 	 * +----------------------+   <-- top of physical memory
167 	 * |                      |
168 	 * ~                      ~
169 	 * |                      |
170 	 * +----------------------+   <-- top of initrd
171 	 * |                      |
172 	 * +----------------------+   <-- top of FDT
173 	 * |                      |
174 	 * +----------------------+   <-- top of cpu0's stack
175 	 * |                      |
176 	 * +----------------------+   <-- top of text/data/bss/toc sections,
177 	 * |                      |       see powerpc/flat.lds
178 	 * |                      |
179 	 * +----------------------+   <-- load address
180 	 * |                      |
181 	 * +----------------------+
182 	 */
183 	fdt_size = fdt_totalsize(fdt);
184 	ret = fdt_move(fdt, freemem, fdt_size);
185 	assert(ret == 0);
186 	ret = dt_init(freemem);
187 	assert(ret == 0);
188 	freemem += fdt_size;
189 
190 	ret = dt_get_initrd(&tmp, &initrd_size);
191 	assert(ret == 0 || ret == -FDT_ERR_NOTFOUND);
192 	if (ret == 0) {
193 		initrd = freemem;
194 		memmove(initrd, tmp, initrd_size);
195 		freemem += initrd_size;
196 	}
197 
198 	/* call init functions */
199 	cpu_init();
200 
201 	/* cpu_init must be called before mem_init */
202 	mem_init(PAGE_ALIGN((unsigned long)freemem));
203 
204 	/* mem_init must be called before io_init */
205 	io_init();
206 
207 	/* finish setup */
208 	ret = dt_get_bootargs(&bootargs);
209 	assert(ret == 0 || ret == -FDT_ERR_NOTFOUND);
210 	setup_args_progname(bootargs);
211 
212 	if (initrd) {
213 		/* environ is currently the only file in the initrd */
214 		char *env = malloc(initrd_size);
215 		memcpy(env, initrd, initrd_size);
216 		setup_env(env, initrd_size);
217 	}
218 }
219