1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Initialize machine setup information and I/O. 4 * 5 * Copyright (C) 2023, Ventana Micro Systems Inc., Andrew Jones <ajones@ventanamicro.com> 6 */ 7 #include <libcflat.h> 8 #include <alloc.h> 9 #include <alloc_page.h> 10 #include <alloc_phys.h> 11 #include <argv.h> 12 #include <cpumask.h> 13 #include <devicetree.h> 14 #include <memregions.h> 15 #include <on-cpus.h> 16 #include <asm/csr.h> 17 #include <asm/mmu.h> 18 #include <asm/page.h> 19 #include <asm/processor.h> 20 #include <asm/setup.h> 21 22 #define VA_BASE ((phys_addr_t)3 * SZ_1G) 23 24 #define MAX_DT_MEM_REGIONS 16 25 #define NR_MEM_REGIONS (MAX_DT_MEM_REGIONS + 16) 26 27 char *initrd; 28 u32 initrd_size; 29 30 struct thread_info cpus[NR_CPUS]; 31 int nr_cpus; 32 33 static struct mem_region riscv_mem_regions[NR_MEM_REGIONS + 1]; 34 35 int hartid_to_cpu(unsigned long hartid) 36 { 37 int cpu; 38 39 for_each_present_cpu(cpu) 40 if (cpus[cpu].hartid == hartid) 41 return cpu; 42 return -1; 43 } 44 45 static void cpu_set_fdt(int fdtnode __unused, u64 regval, void *info __unused) 46 { 47 int cpu = nr_cpus++; 48 49 assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS); 50 51 cpus[cpu].cpu = cpu; 52 cpus[cpu].hartid = regval; 53 set_cpu_present(cpu, true); 54 } 55 56 static void cpu_init_acpi(void) 57 { 58 assert_msg(false, "ACPI not available"); 59 } 60 61 static void cpu_init(void) 62 { 63 int ret; 64 65 nr_cpus = 0; 66 if (dt_available()) { 67 ret = dt_for_each_cpu_node(cpu_set_fdt, NULL); 68 assert(ret == 0); 69 } else { 70 cpu_init_acpi(); 71 } 72 73 set_cpu_online(hartid_to_cpu(csr_read(CSR_SSCRATCH)), true); 74 cpu0_calls_idle = true; 75 } 76 77 extern unsigned long _etext; 78 79 static void mem_init(phys_addr_t freemem_start) 80 { 81 struct mem_region *freemem, *code, *data; 82 phys_addr_t freemem_end, base, top; 83 84 memregions_init(riscv_mem_regions, NR_MEM_REGIONS); 85 memregions_add_dt_regions(MAX_DT_MEM_REGIONS); 86 87 /* Split the region with the code into two regions; code and data */ 88 memregions_split((unsigned long)&_etext, &code, &data); 89 assert(code); 90 code->flags |= MR_F_CODE; 91 92 freemem = memregions_find(freemem_start); 93 assert(freemem && !(freemem->flags & (MR_F_IO | MR_F_CODE))); 94 95 freemem_end = freemem->end & PAGE_MASK; 96 97 /* 98 * The assert below is mostly checking that the free memory doesn't 99 * start in the 3G-4G range, which is reserved for virtual addresses, 100 * but it also confirms that there is some free memory (the amount 101 * is arbitrarily selected, but should be sufficient for a unit test) 102 * 103 * TODO: Allow the VA range to shrink and move. 104 */ 105 if (freemem_end > VA_BASE) 106 freemem_end = VA_BASE; 107 assert(freemem_end - freemem_start >= SZ_1M * 16); 108 109 /* 110 * TODO: Remove the need for this phys allocator dance, since, as we 111 * can see with the assert, we could have gone straight to the page 112 * allocator. 113 */ 114 phys_alloc_init(freemem_start, freemem_end - freemem_start); 115 phys_alloc_set_minimum_alignment(PAGE_SIZE); 116 phys_alloc_get_unused(&base, &top); 117 assert(base == freemem_start && top == freemem_end); 118 119 page_alloc_init_area(0, freemem_start >> PAGE_SHIFT, freemem_end >> PAGE_SHIFT); 120 page_alloc_ops_enable(); 121 } 122 123 static void banner(void) 124 { 125 puts("\n"); 126 puts("##########################################################################\n"); 127 puts("# kvm-unit-tests\n"); 128 puts("##########################################################################\n"); 129 puts("\n"); 130 } 131 132 void setup(const void *fdt, phys_addr_t freemem_start) 133 { 134 void *freemem; 135 const char *bootargs, *tmp; 136 u32 fdt_size; 137 int ret; 138 139 assert(sizeof(long) == 8 || freemem_start < VA_BASE); 140 freemem = (void *)(unsigned long)freemem_start; 141 142 /* Move the FDT to the base of free memory */ 143 fdt_size = fdt_totalsize(fdt); 144 ret = fdt_move(fdt, freemem, fdt_size); 145 assert(ret == 0); 146 ret = dt_init(freemem); 147 assert(ret == 0); 148 freemem += fdt_size; 149 150 /* Move the initrd to the top of the FDT */ 151 ret = dt_get_initrd(&tmp, &initrd_size); 152 assert(ret == 0 || ret == -FDT_ERR_NOTFOUND); 153 if (ret == 0) { 154 initrd = freemem; 155 memmove(initrd, tmp, initrd_size); 156 freemem += initrd_size; 157 } 158 159 mem_init(PAGE_ALIGN((unsigned long)freemem)); 160 cpu_init(); 161 thread_info_init(); 162 io_init(); 163 164 ret = dt_get_bootargs(&bootargs); 165 assert(ret == 0 || ret == -FDT_ERR_NOTFOUND); 166 setup_args_progname(bootargs); 167 168 if (initrd) { 169 /* environ is currently the only file in the initrd */ 170 char *env = malloc(initrd_size); 171 memcpy(env, initrd, initrd_size); 172 setup_env(env, initrd_size); 173 } 174 175 setup_mmu(); 176 177 banner(); 178 } 179