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