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