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