xref: /kvm-unit-tests/lib/arm/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 (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 <argv.h>
18 #include <asm/thread_info.h>
19 #include <asm/setup.h>
20 #include <asm/page.h>
21 #include <asm/mmu.h>
22 #include <asm/smp.h>
23 
24 extern unsigned long stacktop;
25 extern void io_init(void);
26 
27 char *initrd;
28 u32 initrd_size;
29 
30 u64 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (u64)~0 };
31 int nr_cpus;
32 
33 struct mem_region mem_regions[NR_MEM_REGIONS];
34 phys_addr_t __phys_offset, __phys_end;
35 
36 int mpidr_to_cpu(uint64_t mpidr)
37 {
38 	int i;
39 
40 	for (i = 0; i < nr_cpus; ++i)
41 		if (cpus[i] == (mpidr & MPIDR_HWID_BITMASK))
42 			return i;
43 	return -1;
44 }
45 
46 static void cpu_set(int fdtnode __unused, u64 regval, void *info __unused)
47 {
48 	int cpu = nr_cpus++;
49 
50 	assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS);
51 
52 	cpus[cpu] = regval;
53 	set_cpu_present(cpu, true);
54 }
55 
56 static void cpu_init(void)
57 {
58 	int ret;
59 
60 	nr_cpus = 0;
61 	ret = dt_for_each_cpu_node(cpu_set, NULL);
62 	assert(ret == 0);
63 	set_cpu_online(0, true);
64 }
65 
66 static void mem_init(phys_addr_t freemem_start)
67 {
68 	struct dt_pbus_reg regs[NR_MEM_REGIONS];
69 	struct mem_region primary, mem = {
70 		.start = (phys_addr_t)-1,
71 	};
72 	int nr_regs, i;
73 
74 	nr_regs = dt_get_memory_params(regs, NR_MEM_REGIONS);
75 	assert(nr_regs > 0);
76 
77 	primary.end = 0;
78 
79 	for (i = 0; i < nr_regs; ++i) {
80 		mem_regions[i].start = regs[i].addr;
81 		mem_regions[i].end = regs[i].addr + regs[i].size;
82 
83 		/*
84 		 * pick the region we're in for our primary region
85 		 */
86 		if (freemem_start >= mem_regions[i].start
87 				&& freemem_start < mem_regions[i].end) {
88 			mem_regions[i].flags |= MR_F_PRIMARY;
89 			primary = mem_regions[i];
90 		}
91 
92 		/*
93 		 * set the lowest and highest addresses found,
94 		 * ignoring potential gaps
95 		 */
96 		if (mem_regions[i].start < mem.start)
97 			mem.start = mem_regions[i].start;
98 		if (mem_regions[i].end > mem.end)
99 			mem.end = mem_regions[i].end;
100 	}
101 	assert(primary.end != 0);
102 	assert(!(mem.start & ~PHYS_MASK) && !((mem.end - 1) & ~PHYS_MASK));
103 
104 	__phys_offset = mem.start;	/* PHYS_OFFSET */
105 	__phys_end = mem.end;		/* PHYS_END */
106 
107 	phys_alloc_init(freemem_start, primary.end - freemem_start);
108 	phys_alloc_set_minimum_alignment(SMP_CACHE_BYTES);
109 
110 	mmu_enable_idmap();
111 }
112 
113 void setup(const void *fdt)
114 {
115 	void *freemem = &stacktop;
116 	const char *bootargs, *tmp;
117 	u32 fdt_size;
118 	int ret;
119 
120 	/*
121 	 * Before calling mem_init we need to move the fdt and initrd
122 	 * to safe locations. We move them to construct the memory
123 	 * map illustrated below:
124 	 *
125 	 *    +----------------------+   <-- top of physical memory
126 	 *    |                      |
127 	 *    ~                      ~
128 	 *    |                      |
129 	 *    +----------------------+   <-- top of initrd
130 	 *    |                      |
131 	 *    +----------------------+   <-- top of FDT
132 	 *    |                      |
133 	 *    +----------------------+   <-- top of cpu0's stack
134 	 *    |                      |
135 	 *    +----------------------+   <-- top of text/data/bss sections,
136 	 *    |                      |       see arm/flat.lds
137 	 *    |                      |
138 	 *    +----------------------+   <-- load address
139 	 *    |                      |
140 	 *    +----------------------+
141 	 */
142 	fdt_size = fdt_totalsize(fdt);
143 	ret = fdt_move(fdt, freemem, fdt_size);
144 	assert(ret == 0);
145 	ret = dt_init(freemem);
146 	assert(ret == 0);
147 	freemem += fdt_size;
148 
149 	ret = dt_get_initrd(&tmp, &initrd_size);
150 	assert(ret == 0 || ret == -FDT_ERR_NOTFOUND);
151 	if (ret == 0) {
152 		initrd = freemem;
153 		memmove(initrd, tmp, initrd_size);
154 		freemem += initrd_size;
155 	}
156 
157 	/* call init functions */
158 	cpu_init();
159 
160 	/* cpu_init must be called before thread_info_init */
161 	thread_info_init(current_thread_info(), 0);
162 
163 	/* thread_info_init must be called before mem_init */
164 	mem_init(PAGE_ALIGN((unsigned long)freemem));
165 
166 	/* mem_init must be called before io_init */
167 	io_init();
168 
169 	/* finish setup */
170 	ret = dt_get_bootargs(&bootargs);
171 	assert(ret == 0 || ret == -FDT_ERR_NOTFOUND);
172 	setup_args_progname(bootargs);
173 
174 	if (initrd) {
175 		/* environ is currently the only file in the initrd */
176 		char *env = malloc(initrd_size);
177 		memcpy(env, initrd, initrd_size);
178 		setup_env(env, initrd_size);
179 	}
180 }
181