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