1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4 *
5 * Derived from MIPS:
6 * Copyright (C) 1995 Linus Torvalds
7 * Copyright (C) 1995 Waldorf Electronics
8 * Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 01, 02, 03 Ralf Baechle
9 * Copyright (C) 1996 Stoned Elipot
10 * Copyright (C) 1999 Silicon Graphics, Inc.
11 * Copyright (C) 2000, 2001, 2002, 2007 Maciej W. Rozycki
12 */
13 #include <linux/init.h>
14 #include <linux/acpi.h>
15 #include <linux/dmi.h>
16 #include <linux/efi.h>
17 #include <linux/export.h>
18 #include <linux/screen_info.h>
19 #include <linux/memblock.h>
20 #include <linux/initrd.h>
21 #include <linux/ioport.h>
22 #include <linux/kexec.h>
23 #include <linux/crash_dump.h>
24 #include <linux/root_dev.h>
25 #include <linux/console.h>
26 #include <linux/pfn.h>
27 #include <linux/platform_device.h>
28 #include <linux/sizes.h>
29 #include <linux/device.h>
30 #include <linux/dma-map-ops.h>
31 #include <linux/swiotlb.h>
32
33 #include <asm/addrspace.h>
34 #include <asm/bootinfo.h>
35 #include <asm/cache.h>
36 #include <asm/cpu.h>
37 #include <asm/dma.h>
38 #include <asm/efi.h>
39 #include <asm/loongson.h>
40 #include <asm/numa.h>
41 #include <asm/pgalloc.h>
42 #include <asm/sections.h>
43 #include <asm/setup.h>
44 #include <asm/time.h>
45
46 #define SMBIOS_BIOSSIZE_OFFSET 0x09
47 #define SMBIOS_BIOSEXTERN_OFFSET 0x13
48 #define SMBIOS_FREQLOW_OFFSET 0x16
49 #define SMBIOS_FREQHIGH_OFFSET 0x17
50 #define SMBIOS_FREQLOW_MASK 0xFF
51 #define SMBIOS_CORE_PACKAGE_OFFSET 0x23
52 #define LOONGSON_EFI_ENABLE (1 << 3)
53
54 struct screen_info screen_info __section(".data");
55
56 unsigned long fw_arg0, fw_arg1, fw_arg2;
57 DEFINE_PER_CPU(unsigned long, kernelsp);
58 struct cpuinfo_loongarch cpu_data[NR_CPUS] __read_mostly;
59
60 EXPORT_SYMBOL(cpu_data);
61
62 struct loongson_board_info b_info;
63 static const char dmi_empty_string[] = " ";
64
65 /*
66 * Setup information
67 *
68 * These are initialized so they are in the .data section
69 */
70
71 static int num_standard_resources;
72 static struct resource *standard_resources;
73
74 static struct resource code_resource = { .name = "Kernel code", };
75 static struct resource data_resource = { .name = "Kernel data", };
76 static struct resource bss_resource = { .name = "Kernel bss", };
77
get_system_type(void)78 const char *get_system_type(void)
79 {
80 return "generic-loongson-machine";
81 }
82
dmi_string_parse(const struct dmi_header * dm,u8 s)83 static const char *dmi_string_parse(const struct dmi_header *dm, u8 s)
84 {
85 const u8 *bp = ((u8 *) dm) + dm->length;
86
87 if (s) {
88 s--;
89 while (s > 0 && *bp) {
90 bp += strlen(bp) + 1;
91 s--;
92 }
93
94 if (*bp != 0) {
95 size_t len = strlen(bp)+1;
96 size_t cmp_len = len > 8 ? 8 : len;
97
98 if (!memcmp(bp, dmi_empty_string, cmp_len))
99 return dmi_empty_string;
100
101 return bp;
102 }
103 }
104
105 return "";
106 }
107
parse_cpu_table(const struct dmi_header * dm)108 static void __init parse_cpu_table(const struct dmi_header *dm)
109 {
110 long freq_temp = 0;
111 char *dmi_data = (char *)dm;
112
113 freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
114 ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
115 cpu_clock_freq = freq_temp * 1000000;
116
117 loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
118 loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_CORE_PACKAGE_OFFSET);
119
120 pr_info("CpuClock = %llu\n", cpu_clock_freq);
121 }
122
parse_bios_table(const struct dmi_header * dm)123 static void __init parse_bios_table(const struct dmi_header *dm)
124 {
125 char *dmi_data = (char *)dm;
126
127 b_info.bios_size = (*(dmi_data + SMBIOS_BIOSSIZE_OFFSET) + 1) << 6;
128 }
129
find_tokens(const struct dmi_header * dm,void * dummy)130 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
131 {
132 switch (dm->type) {
133 case 0x0: /* Extern BIOS */
134 parse_bios_table(dm);
135 break;
136 case 0x4: /* Calling interface */
137 parse_cpu_table(dm);
138 break;
139 }
140 }
smbios_parse(void)141 static void __init smbios_parse(void)
142 {
143 b_info.bios_vendor = (void *)dmi_get_system_info(DMI_BIOS_VENDOR);
144 b_info.bios_version = (void *)dmi_get_system_info(DMI_BIOS_VERSION);
145 b_info.bios_release_date = (void *)dmi_get_system_info(DMI_BIOS_DATE);
146 b_info.board_vendor = (void *)dmi_get_system_info(DMI_BOARD_VENDOR);
147 b_info.board_name = (void *)dmi_get_system_info(DMI_BOARD_NAME);
148 dmi_walk(find_tokens, NULL);
149 }
150
151 static int usermem __initdata;
152
early_parse_mem(char * p)153 static int __init early_parse_mem(char *p)
154 {
155 phys_addr_t start, size;
156
157 if (!p) {
158 pr_err("mem parameter is empty, do nothing\n");
159 return -EINVAL;
160 }
161
162 /*
163 * If a user specifies memory size, we
164 * blow away any automatically generated
165 * size.
166 */
167 if (usermem == 0) {
168 usermem = 1;
169 memblock_remove(memblock_start_of_DRAM(),
170 memblock_end_of_DRAM() - memblock_start_of_DRAM());
171 }
172 start = 0;
173 size = memparse(p, &p);
174 if (*p == '@')
175 start = memparse(p + 1, &p);
176 else {
177 pr_err("Invalid format!\n");
178 return -EINVAL;
179 }
180
181 if (!IS_ENABLED(CONFIG_NUMA))
182 memblock_add(start, size);
183 else
184 memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
185
186 return 0;
187 }
188 early_param("mem", early_parse_mem);
189
arch_reserve_vmcore(void)190 static void __init arch_reserve_vmcore(void)
191 {
192 #ifdef CONFIG_PROC_VMCORE
193 u64 i;
194 phys_addr_t start, end;
195
196 if (!is_kdump_kernel())
197 return;
198
199 if (!elfcorehdr_size) {
200 for_each_mem_range(i, &start, &end) {
201 if (elfcorehdr_addr >= start && elfcorehdr_addr < end) {
202 /*
203 * Reserve from the elf core header to the end of
204 * the memory segment, that should all be kdump
205 * reserved memory.
206 */
207 elfcorehdr_size = end - elfcorehdr_addr;
208 break;
209 }
210 }
211 }
212
213 if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) {
214 pr_warn("elfcorehdr is overlapped\n");
215 return;
216 }
217
218 memblock_reserve(elfcorehdr_addr, elfcorehdr_size);
219
220 pr_info("Reserving %llu KiB of memory at 0x%llx for elfcorehdr\n",
221 elfcorehdr_size >> 10, elfcorehdr_addr);
222 #endif
223 }
224
arch_parse_crashkernel(void)225 static void __init arch_parse_crashkernel(void)
226 {
227 #ifdef CONFIG_KEXEC
228 int ret;
229 unsigned long long start;
230 unsigned long long total_mem;
231 unsigned long long crash_base, crash_size;
232
233 total_mem = memblock_phys_mem_size();
234 ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
235 if (ret < 0 || crash_size <= 0)
236 return;
237
238 start = memblock_phys_alloc_range(crash_size, 1, crash_base, crash_base + crash_size);
239 if (start != crash_base) {
240 pr_warn("Invalid memory region reserved for crash kernel\n");
241 return;
242 }
243
244 crashk_res.start = crash_base;
245 crashk_res.end = crash_base + crash_size - 1;
246 #endif
247 }
248
platform_init(void)249 void __init platform_init(void)
250 {
251 arch_reserve_vmcore();
252 arch_parse_crashkernel();
253
254 #ifdef CONFIG_ACPI_TABLE_UPGRADE
255 acpi_table_upgrade();
256 #endif
257 #ifdef CONFIG_ACPI
258 acpi_gbl_use_default_register_widths = false;
259 acpi_boot_table_init();
260 #endif
261
262 #ifdef CONFIG_NUMA
263 init_numa_memory();
264 #endif
265 dmi_setup();
266 smbios_parse();
267 pr_info("The BIOS Version: %s\n", b_info.bios_version);
268
269 efi_runtime_init();
270 }
271
check_kernel_sections_mem(void)272 static void __init check_kernel_sections_mem(void)
273 {
274 phys_addr_t start = __pa_symbol(&_text);
275 phys_addr_t size = __pa_symbol(&_end) - start;
276
277 if (!memblock_is_region_memory(start, size)) {
278 pr_info("Kernel sections are not in the memory maps\n");
279 memblock_add(start, size);
280 }
281 }
282
283 /*
284 * arch_mem_init - initialize memory management subsystem
285 */
arch_mem_init(char ** cmdline_p)286 static void __init arch_mem_init(char **cmdline_p)
287 {
288 if (usermem)
289 pr_info("User-defined physical RAM map overwrite\n");
290
291 check_kernel_sections_mem();
292
293 /*
294 * In order to reduce the possibility of kernel panic when failed to
295 * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
296 * low memory as small as possible before plat_swiotlb_setup(), so
297 * make sparse_init() using top-down allocation.
298 */
299 memblock_set_bottom_up(false);
300 sparse_init();
301 memblock_set_bottom_up(true);
302
303 swiotlb_init(true, SWIOTLB_VERBOSE);
304
305 dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
306
307 memblock_dump_all();
308
309 early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
310 }
311
resource_init(void)312 static void __init resource_init(void)
313 {
314 long i = 0;
315 size_t res_size;
316 struct resource *res;
317 struct memblock_region *region;
318
319 code_resource.start = __pa_symbol(&_text);
320 code_resource.end = __pa_symbol(&_etext) - 1;
321 data_resource.start = __pa_symbol(&_etext);
322 data_resource.end = __pa_symbol(&_edata) - 1;
323 bss_resource.start = __pa_symbol(&__bss_start);
324 bss_resource.end = __pa_symbol(&__bss_stop) - 1;
325
326 num_standard_resources = memblock.memory.cnt;
327 res_size = num_standard_resources * sizeof(*standard_resources);
328 standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
329
330 for_each_mem_region(region) {
331 res = &standard_resources[i++];
332 if (!memblock_is_nomap(region)) {
333 res->name = "System RAM";
334 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
335 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
336 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
337 } else {
338 res->name = "Reserved";
339 res->flags = IORESOURCE_MEM;
340 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
341 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
342 }
343
344 request_resource(&iomem_resource, res);
345
346 /*
347 * We don't know which RAM region contains kernel data,
348 * so we try it repeatedly and let the resource manager
349 * test it.
350 */
351 request_resource(res, &code_resource);
352 request_resource(res, &data_resource);
353 request_resource(res, &bss_resource);
354 }
355
356 #ifdef CONFIG_KEXEC
357 if (crashk_res.start < crashk_res.end) {
358 insert_resource(&iomem_resource, &crashk_res);
359 pr_info("Reserving %ldMB of memory at %ldMB for crashkernel\n",
360 (unsigned long)((crashk_res.end - crashk_res.start + 1) >> 20),
361 (unsigned long)(crashk_res.start >> 20));
362 }
363 #endif
364 }
365
reserve_memblock_reserved_regions(void)366 static int __init reserve_memblock_reserved_regions(void)
367 {
368 u64 i, j;
369
370 for (i = 0; i < num_standard_resources; ++i) {
371 struct resource *mem = &standard_resources[i];
372 phys_addr_t r_start, r_end, mem_size = resource_size(mem);
373
374 if (!memblock_is_region_reserved(mem->start, mem_size))
375 continue;
376
377 for_each_reserved_mem_range(j, &r_start, &r_end) {
378 resource_size_t start, end;
379
380 start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
381 end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
382
383 if (start > mem->end || end < mem->start)
384 continue;
385
386 reserve_region_with_split(mem, start, end, "Reserved");
387 }
388 }
389
390 return 0;
391 }
392 arch_initcall(reserve_memblock_reserved_regions);
393
394 #ifdef CONFIG_SMP
prefill_possible_map(void)395 static void __init prefill_possible_map(void)
396 {
397 int i, possible;
398
399 possible = num_processors + disabled_cpus;
400 if (possible > nr_cpu_ids)
401 possible = nr_cpu_ids;
402
403 pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
404 possible, max((possible - num_processors), 0));
405
406 for (i = 0; i < possible; i++)
407 set_cpu_possible(i, true);
408 for (; i < NR_CPUS; i++)
409 set_cpu_possible(i, false);
410
411 set_nr_cpu_ids(possible);
412 }
413 #endif
414
setup_arch(char ** cmdline_p)415 void __init setup_arch(char **cmdline_p)
416 {
417 cpu_probe();
418 *cmdline_p = boot_command_line;
419
420 init_environ();
421 efi_init();
422 memblock_init();
423 pagetable_init();
424 parse_early_param();
425 reserve_initrd_mem();
426
427 platform_init();
428 arch_mem_init(cmdline_p);
429
430 resource_init();
431 #ifdef CONFIG_SMP
432 plat_smp_setup();
433 prefill_possible_map();
434 #endif
435
436 paging_init();
437 }
438