xref: /openbmc/linux/arch/loongarch/kernel/setup.c (revision e5242c5f)
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/cpu.h>
16 #include <linux/dmi.h>
17 #include <linux/efi.h>
18 #include <linux/export.h>
19 #include <linux/screen_info.h>
20 #include <linux/memblock.h>
21 #include <linux/initrd.h>
22 #include <linux/ioport.h>
23 #include <linux/kexec.h>
24 #include <linux/crash_dump.h>
25 #include <linux/root_dev.h>
26 #include <linux/console.h>
27 #include <linux/pfn.h>
28 #include <linux/platform_device.h>
29 #include <linux/sizes.h>
30 #include <linux/device.h>
31 #include <linux/dma-map-ops.h>
32 #include <linux/libfdt.h>
33 #include <linux/of_fdt.h>
34 #include <linux/of_address.h>
35 #include <linux/suspend.h>
36 #include <linux/swiotlb.h>
37 
38 #include <asm/addrspace.h>
39 #include <asm/alternative.h>
40 #include <asm/bootinfo.h>
41 #include <asm/cache.h>
42 #include <asm/cpu.h>
43 #include <asm/dma.h>
44 #include <asm/efi.h>
45 #include <asm/loongson.h>
46 #include <asm/numa.h>
47 #include <asm/pgalloc.h>
48 #include <asm/sections.h>
49 #include <asm/setup.h>
50 #include <asm/time.h>
51 
52 #define SMBIOS_BIOSSIZE_OFFSET		0x09
53 #define SMBIOS_BIOSEXTERN_OFFSET	0x13
54 #define SMBIOS_FREQLOW_OFFSET		0x16
55 #define SMBIOS_FREQHIGH_OFFSET		0x17
56 #define SMBIOS_FREQLOW_MASK		0xFF
57 #define SMBIOS_CORE_PACKAGE_OFFSET	0x23
58 #define SMBIOS_THREAD_PACKAGE_OFFSET	0x25
59 #define LOONGSON_EFI_ENABLE		(1 << 3)
60 
61 #ifdef CONFIG_EFI
62 struct screen_info screen_info __section(".data");
63 #endif
64 
65 unsigned long fw_arg0, fw_arg1, fw_arg2;
66 DEFINE_PER_CPU(unsigned long, kernelsp);
67 struct cpuinfo_loongarch cpu_data[NR_CPUS] __read_mostly;
68 
69 EXPORT_SYMBOL(cpu_data);
70 
71 struct loongson_board_info b_info;
72 static const char dmi_empty_string[] = "        ";
73 
74 /*
75  * Setup information
76  *
77  * These are initialized so they are in the .data section
78  */
79 char init_command_line[COMMAND_LINE_SIZE] __initdata;
80 
81 static int num_standard_resources;
82 static struct resource *standard_resources;
83 
84 static struct resource code_resource = { .name = "Kernel code", };
85 static struct resource data_resource = { .name = "Kernel data", };
86 static struct resource bss_resource  = { .name = "Kernel bss", };
87 
88 const char *get_system_type(void)
89 {
90 	return "generic-loongson-machine";
91 }
92 
93 void __init arch_cpu_finalize_init(void)
94 {
95 	alternative_instructions();
96 }
97 
98 static const char *dmi_string_parse(const struct dmi_header *dm, u8 s)
99 {
100 	const u8 *bp = ((u8 *) dm) + dm->length;
101 
102 	if (s) {
103 		s--;
104 		while (s > 0 && *bp) {
105 			bp += strlen(bp) + 1;
106 			s--;
107 		}
108 
109 		if (*bp != 0) {
110 			size_t len = strlen(bp)+1;
111 			size_t cmp_len = len > 8 ? 8 : len;
112 
113 			if (!memcmp(bp, dmi_empty_string, cmp_len))
114 				return dmi_empty_string;
115 
116 			return bp;
117 		}
118 	}
119 
120 	return "";
121 }
122 
123 static void __init parse_cpu_table(const struct dmi_header *dm)
124 {
125 	long freq_temp = 0;
126 	char *dmi_data = (char *)dm;
127 
128 	freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
129 			((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
130 	cpu_clock_freq = freq_temp * 1000000;
131 
132 	loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
133 	loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
134 
135 	pr_info("CpuClock = %llu\n", cpu_clock_freq);
136 }
137 
138 static void __init parse_bios_table(const struct dmi_header *dm)
139 {
140 	char *dmi_data = (char *)dm;
141 
142 	b_info.bios_size = (*(dmi_data + SMBIOS_BIOSSIZE_OFFSET) + 1) << 6;
143 }
144 
145 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
146 {
147 	switch (dm->type) {
148 	case 0x0: /* Extern BIOS */
149 		parse_bios_table(dm);
150 		break;
151 	case 0x4: /* Calling interface */
152 		parse_cpu_table(dm);
153 		break;
154 	}
155 }
156 static void __init smbios_parse(void)
157 {
158 	b_info.bios_vendor = (void *)dmi_get_system_info(DMI_BIOS_VENDOR);
159 	b_info.bios_version = (void *)dmi_get_system_info(DMI_BIOS_VERSION);
160 	b_info.bios_release_date = (void *)dmi_get_system_info(DMI_BIOS_DATE);
161 	b_info.board_vendor = (void *)dmi_get_system_info(DMI_BOARD_VENDOR);
162 	b_info.board_name = (void *)dmi_get_system_info(DMI_BOARD_NAME);
163 	dmi_walk(find_tokens, NULL);
164 }
165 
166 #ifdef CONFIG_ARCH_WRITECOMBINE
167 bool wc_enabled = true;
168 #else
169 bool wc_enabled = false;
170 #endif
171 
172 EXPORT_SYMBOL(wc_enabled);
173 
174 static int __init setup_writecombine(char *p)
175 {
176 	if (!strcmp(p, "on"))
177 		wc_enabled = true;
178 	else if (!strcmp(p, "off"))
179 		wc_enabled = false;
180 	else
181 		pr_warn("Unknown writecombine setting \"%s\".\n", p);
182 
183 	return 0;
184 }
185 early_param("writecombine", setup_writecombine);
186 
187 static int usermem __initdata;
188 
189 static int __init early_parse_mem(char *p)
190 {
191 	phys_addr_t start, size;
192 
193 	if (!p) {
194 		pr_err("mem parameter is empty, do nothing\n");
195 		return -EINVAL;
196 	}
197 
198 	/*
199 	 * If a user specifies memory size, we
200 	 * blow away any automatically generated
201 	 * size.
202 	 */
203 	if (usermem == 0) {
204 		usermem = 1;
205 		memblock_remove(memblock_start_of_DRAM(),
206 			memblock_end_of_DRAM() - memblock_start_of_DRAM());
207 	}
208 	start = 0;
209 	size = memparse(p, &p);
210 	if (*p == '@')
211 		start = memparse(p + 1, &p);
212 	else {
213 		pr_err("Invalid format!\n");
214 		return -EINVAL;
215 	}
216 
217 	if (!IS_ENABLED(CONFIG_NUMA))
218 		memblock_add(start, size);
219 	else
220 		memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
221 
222 	return 0;
223 }
224 early_param("mem", early_parse_mem);
225 
226 static void __init arch_reserve_vmcore(void)
227 {
228 #ifdef CONFIG_PROC_VMCORE
229 	u64 i;
230 	phys_addr_t start, end;
231 
232 	if (!is_kdump_kernel())
233 		return;
234 
235 	if (!elfcorehdr_size) {
236 		for_each_mem_range(i, &start, &end) {
237 			if (elfcorehdr_addr >= start && elfcorehdr_addr < end) {
238 				/*
239 				 * Reserve from the elf core header to the end of
240 				 * the memory segment, that should all be kdump
241 				 * reserved memory.
242 				 */
243 				elfcorehdr_size = end - elfcorehdr_addr;
244 				break;
245 			}
246 		}
247 	}
248 
249 	if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) {
250 		pr_warn("elfcorehdr is overlapped\n");
251 		return;
252 	}
253 
254 	memblock_reserve(elfcorehdr_addr, elfcorehdr_size);
255 
256 	pr_info("Reserving %llu KiB of memory at 0x%llx for elfcorehdr\n",
257 		elfcorehdr_size >> 10, elfcorehdr_addr);
258 #endif
259 }
260 
261 /* 2MB alignment for crash kernel regions */
262 #define CRASH_ALIGN	SZ_2M
263 #define CRASH_ADDR_MAX	SZ_4G
264 
265 static void __init arch_parse_crashkernel(void)
266 {
267 #ifdef CONFIG_KEXEC
268 	int ret;
269 	unsigned long long total_mem;
270 	unsigned long long crash_base, crash_size;
271 
272 	total_mem = memblock_phys_mem_size();
273 	ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
274 	if (ret < 0 || crash_size <= 0)
275 		return;
276 
277 	if (crash_base <= 0) {
278 		crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN, CRASH_ALIGN, CRASH_ADDR_MAX);
279 		if (!crash_base) {
280 			pr_warn("crashkernel reservation failed - No suitable area found.\n");
281 			return;
282 		}
283 	} else if (!memblock_phys_alloc_range(crash_size, CRASH_ALIGN, crash_base, crash_base + crash_size)) {
284 		pr_warn("Invalid memory region reserved for crash kernel\n");
285 		return;
286 	}
287 
288 	crashk_res.start = crash_base;
289 	crashk_res.end	 = crash_base + crash_size - 1;
290 #endif
291 }
292 
293 static void __init fdt_setup(void)
294 {
295 #ifdef CONFIG_OF_EARLY_FLATTREE
296 	void *fdt_pointer;
297 
298 	/* ACPI-based systems do not require parsing fdt */
299 	if (acpi_os_get_root_pointer())
300 		return;
301 
302 	/* Look for a device tree configuration table entry */
303 	fdt_pointer = efi_fdt_pointer();
304 	if (!fdt_pointer || fdt_check_header(fdt_pointer))
305 		return;
306 
307 	early_init_dt_scan(fdt_pointer, __pa(fdt_pointer));
308 	early_init_fdt_reserve_self();
309 
310 	max_low_pfn = PFN_PHYS(memblock_end_of_DRAM());
311 #endif
312 }
313 
314 static void __init bootcmdline_init(char **cmdline_p)
315 {
316 	/*
317 	 * If CONFIG_CMDLINE_FORCE is enabled then initializing the command line
318 	 * is trivial - we simply use the built-in command line unconditionally &
319 	 * unmodified.
320 	 */
321 	if (IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
322 		strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
323 		goto out;
324 	}
325 
326 #ifdef CONFIG_OF_FLATTREE
327 	/*
328 	 * If CONFIG_CMDLINE_BOOTLOADER is enabled and we are in FDT-based system,
329 	 * the boot_command_line will be overwritten by early_init_dt_scan_chosen().
330 	 * So we need to append init_command_line (the original copy of boot_command_line)
331 	 * to boot_command_line.
332 	 */
333 	if (initial_boot_params) {
334 		if (boot_command_line[0])
335 			strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
336 
337 		strlcat(boot_command_line, init_command_line, COMMAND_LINE_SIZE);
338 		goto out;
339 	}
340 #endif
341 
342 	/*
343 	 * Append built-in command line to the bootloader command line if
344 	 * CONFIG_CMDLINE_EXTEND is enabled.
345 	 */
346 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) && CONFIG_CMDLINE[0]) {
347 		strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
348 		strlcat(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
349 	}
350 
351 	/*
352 	 * Use built-in command line if the bootloader command line is empty.
353 	 */
354 	if (IS_ENABLED(CONFIG_CMDLINE_BOOTLOADER) && !boot_command_line[0])
355 		strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
356 
357 out:
358 	*cmdline_p = boot_command_line;
359 }
360 
361 void __init platform_init(void)
362 {
363 	arch_reserve_vmcore();
364 	arch_parse_crashkernel();
365 
366 #ifdef CONFIG_ACPI_TABLE_UPGRADE
367 	acpi_table_upgrade();
368 #endif
369 #ifdef CONFIG_ACPI
370 	acpi_gbl_use_default_register_widths = false;
371 	acpi_boot_table_init();
372 #endif
373 
374 	early_init_fdt_scan_reserved_mem();
375 	unflatten_and_copy_device_tree();
376 
377 #ifdef CONFIG_NUMA
378 	init_numa_memory();
379 #endif
380 	dmi_setup();
381 	smbios_parse();
382 	pr_info("The BIOS Version: %s\n", b_info.bios_version);
383 
384 	efi_runtime_init();
385 }
386 
387 static void __init check_kernel_sections_mem(void)
388 {
389 	phys_addr_t start = __pa_symbol(&_text);
390 	phys_addr_t size = __pa_symbol(&_end) - start;
391 
392 	if (!memblock_is_region_memory(start, size)) {
393 		pr_info("Kernel sections are not in the memory maps\n");
394 		memblock_add(start, size);
395 	}
396 }
397 
398 /*
399  * arch_mem_init - initialize memory management subsystem
400  */
401 static void __init arch_mem_init(char **cmdline_p)
402 {
403 	if (usermem)
404 		pr_info("User-defined physical RAM map overwrite\n");
405 
406 	check_kernel_sections_mem();
407 
408 	/*
409 	 * In order to reduce the possibility of kernel panic when failed to
410 	 * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
411 	 * low memory as small as possible before swiotlb_init(), so make
412 	 * sparse_init() using top-down allocation.
413 	 */
414 	memblock_set_bottom_up(false);
415 	sparse_init();
416 	memblock_set_bottom_up(true);
417 
418 	swiotlb_init(true, SWIOTLB_VERBOSE);
419 
420 	dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
421 
422 	/* Reserve for hibernation. */
423 	register_nosave_region(PFN_DOWN(__pa_symbol(&__nosave_begin)),
424 				   PFN_UP(__pa_symbol(&__nosave_end)));
425 
426 	memblock_dump_all();
427 
428 	early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
429 }
430 
431 static void __init resource_init(void)
432 {
433 	long i = 0;
434 	size_t res_size;
435 	struct resource *res;
436 	struct memblock_region *region;
437 
438 	code_resource.start = __pa_symbol(&_text);
439 	code_resource.end = __pa_symbol(&_etext) - 1;
440 	data_resource.start = __pa_symbol(&_etext);
441 	data_resource.end = __pa_symbol(&_edata) - 1;
442 	bss_resource.start = __pa_symbol(&__bss_start);
443 	bss_resource.end = __pa_symbol(&__bss_stop) - 1;
444 
445 	num_standard_resources = memblock.memory.cnt;
446 	res_size = num_standard_resources * sizeof(*standard_resources);
447 	standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
448 
449 	for_each_mem_region(region) {
450 		res = &standard_resources[i++];
451 		if (!memblock_is_nomap(region)) {
452 			res->name  = "System RAM";
453 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
454 			res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
455 			res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
456 		} else {
457 			res->name  = "Reserved";
458 			res->flags = IORESOURCE_MEM;
459 			res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
460 			res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
461 		}
462 
463 		request_resource(&iomem_resource, res);
464 
465 		/*
466 		 *  We don't know which RAM region contains kernel data,
467 		 *  so we try it repeatedly and let the resource manager
468 		 *  test it.
469 		 */
470 		request_resource(res, &code_resource);
471 		request_resource(res, &data_resource);
472 		request_resource(res, &bss_resource);
473 	}
474 
475 #ifdef CONFIG_KEXEC
476 	if (crashk_res.start < crashk_res.end) {
477 		insert_resource(&iomem_resource, &crashk_res);
478 		pr_info("Reserving %ldMB of memory at %ldMB for crashkernel\n",
479 			(unsigned long)((crashk_res.end - crashk_res.start + 1) >> 20),
480 			(unsigned long)(crashk_res.start  >> 20));
481 	}
482 #endif
483 }
484 
485 static int __init add_legacy_isa_io(struct fwnode_handle *fwnode,
486 				resource_size_t hw_start, resource_size_t size)
487 {
488 	int ret = 0;
489 	unsigned long vaddr;
490 	struct logic_pio_hwaddr *range;
491 
492 	range = kzalloc(sizeof(*range), GFP_ATOMIC);
493 	if (!range)
494 		return -ENOMEM;
495 
496 	range->fwnode = fwnode;
497 	range->size = size = round_up(size, PAGE_SIZE);
498 	range->hw_start = hw_start;
499 	range->flags = LOGIC_PIO_CPU_MMIO;
500 
501 	ret = logic_pio_register_range(range);
502 	if (ret) {
503 		kfree(range);
504 		return ret;
505 	}
506 
507 	/* Legacy ISA must placed at the start of PCI_IOBASE */
508 	if (range->io_start != 0) {
509 		logic_pio_unregister_range(range);
510 		kfree(range);
511 		return -EINVAL;
512 	}
513 
514 	vaddr = (unsigned long)(PCI_IOBASE + range->io_start);
515 	ioremap_page_range(vaddr, vaddr + size, hw_start, pgprot_device(PAGE_KERNEL));
516 
517 	return 0;
518 }
519 
520 static __init int arch_reserve_pio_range(void)
521 {
522 	struct device_node *np;
523 
524 	for_each_node_by_name(np, "isa") {
525 		struct of_range range;
526 		struct of_range_parser parser;
527 
528 		pr_info("ISA Bridge: %pOF\n", np);
529 
530 		if (of_range_parser_init(&parser, np)) {
531 			pr_info("Failed to parse resources.\n");
532 			of_node_put(np);
533 			break;
534 		}
535 
536 		for_each_of_range(&parser, &range) {
537 			switch (range.flags & IORESOURCE_TYPE_BITS) {
538 			case IORESOURCE_IO:
539 				pr_info(" IO 0x%016llx..0x%016llx  ->  0x%016llx\n",
540 					range.cpu_addr,
541 					range.cpu_addr + range.size - 1,
542 					range.bus_addr);
543 				if (add_legacy_isa_io(&np->fwnode, range.cpu_addr, range.size))
544 					pr_warn("Failed to reserve legacy IO in Logic PIO\n");
545 				break;
546 			case IORESOURCE_MEM:
547 				pr_info(" MEM 0x%016llx..0x%016llx  ->  0x%016llx\n",
548 					range.cpu_addr,
549 					range.cpu_addr + range.size - 1,
550 					range.bus_addr);
551 				break;
552 			}
553 		}
554 	}
555 
556 	return 0;
557 }
558 arch_initcall(arch_reserve_pio_range);
559 
560 static int __init reserve_memblock_reserved_regions(void)
561 {
562 	u64 i, j;
563 
564 	for (i = 0; i < num_standard_resources; ++i) {
565 		struct resource *mem = &standard_resources[i];
566 		phys_addr_t r_start, r_end, mem_size = resource_size(mem);
567 
568 		if (!memblock_is_region_reserved(mem->start, mem_size))
569 			continue;
570 
571 		for_each_reserved_mem_range(j, &r_start, &r_end) {
572 			resource_size_t start, end;
573 
574 			start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
575 			end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
576 
577 			if (start > mem->end || end < mem->start)
578 				continue;
579 
580 			reserve_region_with_split(mem, start, end, "Reserved");
581 		}
582 	}
583 
584 	return 0;
585 }
586 arch_initcall(reserve_memblock_reserved_regions);
587 
588 #ifdef CONFIG_SMP
589 static void __init prefill_possible_map(void)
590 {
591 	int i, possible;
592 
593 	possible = num_processors + disabled_cpus;
594 	if (possible > nr_cpu_ids)
595 		possible = nr_cpu_ids;
596 
597 	pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
598 			possible, max((possible - num_processors), 0));
599 
600 	for (i = 0; i < possible; i++)
601 		set_cpu_possible(i, true);
602 	for (; i < NR_CPUS; i++)
603 		set_cpu_possible(i, false);
604 
605 	set_nr_cpu_ids(possible);
606 }
607 #endif
608 
609 void __init setup_arch(char **cmdline_p)
610 {
611 	cpu_probe();
612 
613 	init_environ();
614 	efi_init();
615 	fdt_setup();
616 	memblock_init();
617 	pagetable_init();
618 	bootcmdline_init(cmdline_p);
619 	parse_early_param();
620 	reserve_initrd_mem();
621 
622 	platform_init();
623 	arch_mem_init(cmdline_p);
624 
625 	resource_init();
626 #ifdef CONFIG_SMP
627 	plat_smp_setup();
628 	prefill_possible_map();
629 #endif
630 
631 	paging_init();
632 
633 #ifdef CONFIG_KASAN
634 	kasan_init();
635 #endif
636 }
637