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