xref: /kvm-unit-tests/lib/powerpc/setup.c (revision d35482d5b65906eaef215aecd4b19decf9002afb)
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 u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) };
26 int nr_cpus;
27 uint64_t tb_hz;
28 
29 struct mem_region mem_regions[NR_MEM_REGIONS];
30 phys_addr_t __physical_start, __physical_end;
31 unsigned __icache_bytes, __dcache_bytes;
32 
33 struct cpu_set_params {
34 	unsigned icache_bytes;
35 	unsigned dcache_bytes;
36 	uint64_t tb_hz;
37 };
38 
39 #define EXCEPTION_STACK_SIZE	(32*1024) /* 32kB */
40 
41 static char exception_stack[NR_CPUS][EXCEPTION_STACK_SIZE];
42 
43 static void cpu_set(int fdtnode, u32 regval, void *info)
44 {
45 	static bool read_common_info = false;
46 	struct cpu_set_params *params = info;
47 	int cpu = nr_cpus++;
48 
49 	if (cpu >= NR_CPUS) {
50 		printf("Number cpus exceeds maximum supported (%d).\n",
51 			NR_CPUS);
52 		assert(0);
53 	}
54 	cpus[cpu] = regval;
55 
56 	/* set exception stack address for this CPU (in SPGR0) */
57 
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 	const char *bootargs;
157 	u32 fdt_size;
158 	int ret;
159 
160 	/*
161 	 * Move the fdt to just above the stack. The free memory
162 	 * then starts just after the fdt.
163 	 */
164 	fdt_size = fdt_totalsize(fdt);
165 	ret = fdt_move(fdt, &stacktop, fdt_size);
166 	assert(ret == 0);
167 	ret = dt_init(&stacktop);
168 	assert(ret == 0);
169 
170 	cpu_init();
171 
172 	/* cpu_init must be called before mem_init */
173 	mem_init(PAGE_ALIGN((unsigned long)&stacktop + fdt_size));
174 
175 	/* mem_init must be called before io_init */
176 	io_init();
177 
178 	ret = dt_get_bootargs(&bootargs);
179 	assert(ret == 0);
180 	setup_args_progname(bootargs);
181 }
182