xref: /kvm-unit-tests/lib/arm/setup.c (revision 68ea0e0be63d341362dca92d3bacac85b465fb3c)
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(const char *args);
26 
27 u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) };
28 int nr_cpus;
29 
30 phys_addr_t __phys_offset, __phys_end;
31 
32 static void cpu_set(int fdtnode __unused, u32 regval, void *info __unused)
33 {
34 	int cpu = nr_cpus++;
35 	assert(cpu < NR_CPUS);
36 	cpus[cpu] = regval;
37 	set_cpu_present(cpu, true);
38 }
39 
40 static void cpu_init(void)
41 {
42 	nr_cpus = 0;
43 	assert(dt_for_each_cpu_node(cpu_set, NULL) == 0);
44 	set_cpu_online(0, true);
45 }
46 
47 static void mem_init(phys_addr_t freemem_start)
48 {
49 	/* we only expect one membank to be defined in the DT */
50 	struct dt_pbus_reg regs[1];
51 	phys_addr_t mem_start, mem_end;
52 
53 	assert(dt_get_memory_params(regs, 1));
54 
55 	mem_start = regs[0].addr;
56 	mem_end = mem_start + regs[0].size;
57 
58 	assert(!(mem_start & ~PHYS_MASK) && !((mem_end-1) & ~PHYS_MASK));
59 	assert(freemem_start >= mem_start && freemem_start < mem_end);
60 
61 	__phys_offset = mem_start;	/* PHYS_OFFSET */
62 	__phys_end = mem_end;		/* PHYS_END */
63 
64 	phys_alloc_init(freemem_start, mem_end - freemem_start);
65 	phys_alloc_set_minimum_alignment(SMP_CACHE_BYTES);
66 
67 	mmu_enable_idmap();
68 }
69 
70 void setup(const void *fdt)
71 {
72 	const char *bootargs;
73 	u32 fdt_size;
74 
75 	/*
76 	 * Move the fdt to just above the stack. The free memory
77 	 * then starts just after the fdt.
78 	 */
79 	fdt_size = fdt_totalsize(fdt);
80 	assert(fdt_move(fdt, &stacktop, fdt_size) == 0);
81 	assert(dt_init(&stacktop) == 0);
82 
83 	mem_init(PAGE_ALIGN((unsigned long)&stacktop + fdt_size));
84 	io_init();
85 	cpu_init();
86 
87 	thread_info_init(current_thread_info(), 0);
88 
89 	assert(dt_get_bootargs(&bootargs) == 0);
90 	setup_args(bootargs);
91 }
92