xref: /kvm-unit-tests/lib/arm/setup.c (revision f1071c6d5fc454957e2501934fea6d9e592da8a5)
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 <asm/thread_info.h>
18 #include <asm/setup.h>
19 #include <asm/page.h>
20 #include <asm/mmu.h>
21 #include <asm/smp.h>
22 
23 extern unsigned long stacktop;
24 extern void io_init(void);
25 extern void setup_args_progname(const char *args);
26 
27 u64 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (u64)~0 };
28 int nr_cpus;
29 
30 struct mem_region mem_regions[NR_MEM_REGIONS];
31 phys_addr_t __phys_offset, __phys_end;
32 
33 int mpidr_to_cpu(uint64_t mpidr)
34 {
35 	int i;
36 
37 	for (i = 0; i < nr_cpus; ++i)
38 		if (cpus[i] == (mpidr & MPIDR_HWID_BITMASK))
39 			return i;
40 	return -1;
41 }
42 
43 static void cpu_set(int fdtnode __unused, u64 regval, void *info __unused)
44 {
45 	int cpu = nr_cpus++;
46 
47 	if (cpu >= NR_CPUS) {
48 		printf("Number cpus exceeds maximum supported (%d).\n",
49 			NR_CPUS);
50 		assert(0);
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 	const char *bootargs;
116 	u32 fdt_size;
117 	int ret;
118 
119 	/*
120 	 * Move the fdt to just above the stack. The free memory
121 	 * then starts just after the fdt.
122 	 */
123 	fdt_size = fdt_totalsize(fdt);
124 	ret = fdt_move(fdt, &stacktop, fdt_size);
125 	assert(ret == 0);
126 	ret = dt_init(&stacktop);
127 	assert(ret == 0);
128 
129 	cpu_init();
130 
131 	/* cpu_init must be called before thread_info_init */
132 	thread_info_init(current_thread_info(), 0);
133 
134 	/* thread_info_init must be called before mem_init */
135 	mem_init(PAGE_ALIGN((unsigned long)&stacktop + fdt_size));
136 
137 	/* mem_init must be called before io_init */
138 	io_init();
139 
140 	ret = dt_get_bootargs(&bootargs);
141 	assert(ret == 0);
142 	setup_args_progname(bootargs);
143 }
144