setup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on arch/arm/kernel/setup.c
  4. *
  5. * Copyright (C) 1995-2001 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/export.h>
  10. #include <linux/kernel.h>
  11. #include <linux/stddef.h>
  12. #include <linux/ioport.h>
  13. #include <linux/delay.h>
  14. #include <linux/initrd.h>
  15. #include <linux/console.h>
  16. #include <linux/cache.h>
  17. #include <linux/screen_info.h>
  18. #include <linux/init.h>
  19. #include <linux/kexec.h>
  20. #include <linux/root_dev.h>
  21. #include <linux/cpu.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/smp.h>
  24. #include <linux/fs.h>
  25. #include <linux/panic_notifier.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/memblock.h>
  28. #include <linux/of_fdt.h>
  29. #include <linux/efi.h>
  30. #include <linux/psci.h>
  31. #include <linux/sched/task.h>
  32. #include <linux/scs.h>
  33. #include <linux/mm.h>
  34. #include <asm/acpi.h>
  35. #include <asm/fixmap.h>
  36. #include <asm/cpu.h>
  37. #include <asm/cputype.h>
  38. #include <asm/daifflags.h>
  39. #include <asm/elf.h>
  40. #include <asm/cpufeature.h>
  41. #include <asm/cpu_ops.h>
  42. #include <asm/kasan.h>
  43. #include <asm/numa.h>
  44. #include <asm/scs.h>
  45. #include <asm/sections.h>
  46. #include <asm/setup.h>
  47. #include <asm/smp_plat.h>
  48. #include <asm/cacheflush.h>
  49. #include <asm/tlbflush.h>
  50. #include <asm/traps.h>
  51. #include <asm/efi.h>
  52. #include <asm/xen/hypervisor.h>
  53. #include <asm/mmu_context.h>
  54. static int num_standard_resources;
  55. static struct resource *standard_resources;
  56. phys_addr_t __fdt_pointer __initdata;
  57. u64 mmu_enabled_at_boot __initdata;
  58. /*
  59. * Standard memory resources
  60. */
  61. static struct resource mem_res[] = {
  62. {
  63. .name = "Kernel code",
  64. .start = 0,
  65. .end = 0,
  66. .flags = IORESOURCE_SYSTEM_RAM
  67. },
  68. {
  69. .name = "Kernel data",
  70. .start = 0,
  71. .end = 0,
  72. .flags = IORESOURCE_SYSTEM_RAM
  73. }
  74. };
  75. #define kernel_code mem_res[0]
  76. #define kernel_data mem_res[1]
  77. /*
  78. * The recorded values of x0 .. x3 upon kernel entry.
  79. */
  80. u64 __cacheline_aligned boot_args[4];
  81. void __init smp_setup_processor_id(void)
  82. {
  83. u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
  84. set_cpu_logical_map(0, mpidr);
  85. pr_info("Booting Linux on physical CPU 0x%010lx [0x%08x]\n",
  86. (unsigned long)mpidr, read_cpuid_id());
  87. }
  88. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  89. {
  90. return phys_id == cpu_logical_map(cpu);
  91. }
  92. struct mpidr_hash mpidr_hash;
  93. /**
  94. * smp_build_mpidr_hash - Pre-compute shifts required at each affinity
  95. * level in order to build a linear index from an
  96. * MPIDR value. Resulting algorithm is a collision
  97. * free hash carried out through shifting and ORing
  98. */
  99. static void __init smp_build_mpidr_hash(void)
  100. {
  101. u32 i, affinity, fs[4], bits[4], ls;
  102. u64 mask = 0;
  103. /*
  104. * Pre-scan the list of MPIDRS and filter out bits that do
  105. * not contribute to affinity levels, ie they never toggle.
  106. */
  107. for_each_possible_cpu(i)
  108. mask |= (cpu_logical_map(i) ^ cpu_logical_map(0));
  109. pr_debug("mask of set bits %#llx\n", mask);
  110. /*
  111. * Find and stash the last and first bit set at all affinity levels to
  112. * check how many bits are required to represent them.
  113. */
  114. for (i = 0; i < 4; i++) {
  115. affinity = MPIDR_AFFINITY_LEVEL(mask, i);
  116. /*
  117. * Find the MSB bit and LSB bits position
  118. * to determine how many bits are required
  119. * to express the affinity level.
  120. */
  121. ls = fls(affinity);
  122. fs[i] = affinity ? ffs(affinity) - 1 : 0;
  123. bits[i] = ls - fs[i];
  124. }
  125. /*
  126. * An index can be created from the MPIDR_EL1 by isolating the
  127. * significant bits at each affinity level and by shifting
  128. * them in order to compress the 32 bits values space to a
  129. * compressed set of values. This is equivalent to hashing
  130. * the MPIDR_EL1 through shifting and ORing. It is a collision free
  131. * hash though not minimal since some levels might contain a number
  132. * of CPUs that is not an exact power of 2 and their bit
  133. * representation might contain holes, eg MPIDR_EL1[7:0] = {0x2, 0x80}.
  134. */
  135. mpidr_hash.shift_aff[0] = MPIDR_LEVEL_SHIFT(0) + fs[0];
  136. mpidr_hash.shift_aff[1] = MPIDR_LEVEL_SHIFT(1) + fs[1] - bits[0];
  137. mpidr_hash.shift_aff[2] = MPIDR_LEVEL_SHIFT(2) + fs[2] -
  138. (bits[1] + bits[0]);
  139. mpidr_hash.shift_aff[3] = MPIDR_LEVEL_SHIFT(3) +
  140. fs[3] - (bits[2] + bits[1] + bits[0]);
  141. mpidr_hash.mask = mask;
  142. mpidr_hash.bits = bits[3] + bits[2] + bits[1] + bits[0];
  143. pr_debug("MPIDR hash: aff0[%u] aff1[%u] aff2[%u] aff3[%u] mask[%#llx] bits[%u]\n",
  144. mpidr_hash.shift_aff[0],
  145. mpidr_hash.shift_aff[1],
  146. mpidr_hash.shift_aff[2],
  147. mpidr_hash.shift_aff[3],
  148. mpidr_hash.mask,
  149. mpidr_hash.bits);
  150. /*
  151. * 4x is an arbitrary value used to warn on a hash table much bigger
  152. * than expected on most systems.
  153. */
  154. if (mpidr_hash_size() > 4 * num_possible_cpus())
  155. pr_warn("Large number of MPIDR hash buckets detected\n");
  156. }
  157. static void __init setup_machine_fdt(phys_addr_t dt_phys)
  158. {
  159. int size;
  160. void *dt_virt = fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL);
  161. const char *name;
  162. if (dt_virt)
  163. memblock_reserve(dt_phys, size);
  164. /*
  165. * dt_virt is a fixmap address, hence __pa(dt_virt) can't be used.
  166. * Pass dt_phys directly.
  167. */
  168. if (!early_init_dt_scan(dt_virt, dt_phys)) {
  169. pr_crit("\n"
  170. "Error: invalid device tree blob at physical address %pa (virtual address 0x%px)\n"
  171. "The dtb must be 8-byte aligned and must not exceed 2 MB in size\n"
  172. "\nPlease check your bootloader.",
  173. &dt_phys, dt_virt);
  174. /*
  175. * Note that in this _really_ early stage we cannot even BUG()
  176. * or oops, so the least terrible thing to do is cpu_relax(),
  177. * or else we could end-up printing non-initialized data, etc.
  178. */
  179. while (true)
  180. cpu_relax();
  181. }
  182. /* Early fixups are done, map the FDT as read-only now */
  183. fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
  184. name = of_flat_dt_get_machine_name();
  185. if (!name)
  186. return;
  187. pr_info("Machine model: %s\n", name);
  188. dump_stack_set_arch_desc("%s (DT)", name);
  189. }
  190. static void __init request_standard_resources(void)
  191. {
  192. struct memblock_region *region;
  193. struct resource *res;
  194. unsigned long i = 0;
  195. size_t res_size;
  196. kernel_code.start = __pa_symbol(_text);
  197. kernel_code.end = __pa_symbol(__init_begin - 1);
  198. kernel_data.start = __pa_symbol(_sdata);
  199. kernel_data.end = __pa_symbol(_end - 1);
  200. insert_resource(&iomem_resource, &kernel_code);
  201. insert_resource(&iomem_resource, &kernel_data);
  202. num_standard_resources = memblock.memory.cnt;
  203. res_size = num_standard_resources * sizeof(*standard_resources);
  204. standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
  205. if (!standard_resources)
  206. panic("%s: Failed to allocate %zu bytes\n", __func__, res_size);
  207. for_each_mem_region(region) {
  208. res = &standard_resources[i++];
  209. if (memblock_is_nomap(region)) {
  210. res->name = "reserved";
  211. res->flags = IORESOURCE_MEM;
  212. res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
  213. res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
  214. } else {
  215. res->name = "System RAM";
  216. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  217. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  218. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  219. }
  220. insert_resource(&iomem_resource, res);
  221. }
  222. }
  223. static int __init reserve_memblock_reserved_regions(void)
  224. {
  225. u64 i, j;
  226. for (i = 0; i < num_standard_resources; ++i) {
  227. struct resource *mem = &standard_resources[i];
  228. phys_addr_t r_start, r_end, mem_size = resource_size(mem);
  229. if (!memblock_is_region_reserved(mem->start, mem_size))
  230. continue;
  231. for_each_reserved_mem_range(j, &r_start, &r_end) {
  232. resource_size_t start, end;
  233. start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
  234. end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
  235. if (start > mem->end || end < mem->start)
  236. continue;
  237. reserve_region_with_split(mem, start, end, "reserved");
  238. }
  239. }
  240. return 0;
  241. }
  242. arch_initcall(reserve_memblock_reserved_regions);
  243. u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID };
  244. u64 cpu_logical_map(unsigned int cpu)
  245. {
  246. return __cpu_logical_map[cpu];
  247. }
  248. void __init __no_sanitize_address setup_arch(char **cmdline_p)
  249. {
  250. setup_initial_init_mm(_text, _etext, _edata, _end);
  251. *cmdline_p = boot_command_line;
  252. kaslr_init();
  253. early_fixmap_init();
  254. early_ioremap_init();
  255. setup_machine_fdt(__fdt_pointer);
  256. /*
  257. * Initialise the static keys early as they may be enabled by the
  258. * cpufeature code and early parameters.
  259. */
  260. jump_label_init();
  261. parse_early_param();
  262. dynamic_scs_init();
  263. /*
  264. * The primary CPU enters the kernel with all DAIF exceptions masked.
  265. *
  266. * We must unmask Debug and SError before preemption or scheduling is
  267. * possible to ensure that these are consistently unmasked across
  268. * threads, and we want to unmask SError as soon as possible after
  269. * initializing earlycon so that we can report any SErrors immediately.
  270. *
  271. * IRQ and FIQ will be unmasked after the root irqchip has been
  272. * detected and initialized.
  273. */
  274. local_daif_restore(DAIF_PROCCTX_NOIRQ);
  275. /*
  276. * TTBR0 is only used for the identity mapping at this stage. Make it
  277. * point to zero page to avoid speculatively fetching new entries.
  278. */
  279. cpu_uninstall_idmap();
  280. xen_early_init();
  281. efi_init();
  282. if (!efi_enabled(EFI_BOOT)) {
  283. if ((u64)_text % MIN_KIMG_ALIGN)
  284. pr_warn(FW_BUG "Kernel image misaligned at boot, please fix your bootloader!");
  285. WARN_TAINT(mmu_enabled_at_boot, TAINT_FIRMWARE_WORKAROUND,
  286. FW_BUG "Booted with MMU enabled!");
  287. }
  288. arm64_memblock_init();
  289. paging_init();
  290. acpi_table_upgrade();
  291. /* Parse the ACPI tables for possible boot-time configuration */
  292. acpi_boot_table_init();
  293. if (acpi_disabled)
  294. unflatten_device_tree();
  295. bootmem_init();
  296. kasan_init();
  297. request_standard_resources();
  298. early_ioremap_reset();
  299. if (acpi_disabled)
  300. psci_dt_init();
  301. else
  302. psci_acpi_init();
  303. init_bootcpu_ops();
  304. smp_init_cpus();
  305. smp_build_mpidr_hash();
  306. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  307. /*
  308. * Make sure init_thread_info.ttbr0 always generates translation
  309. * faults in case uaccess_enable() is inadvertently called by the init
  310. * thread.
  311. */
  312. init_task.thread_info.ttbr0 = phys_to_ttbr(__pa_symbol(reserved_pg_dir));
  313. #endif
  314. if (boot_args[1] || boot_args[2] || boot_args[3]) {
  315. pr_err("WARNING: x1-x3 nonzero in violation of boot protocol:\n"
  316. "\tx1: %016llx\n\tx2: %016llx\n\tx3: %016llx\n"
  317. "This indicates a broken bootloader or old kernel\n",
  318. boot_args[1], boot_args[2], boot_args[3]);
  319. }
  320. }
  321. static inline bool cpu_can_disable(unsigned int cpu)
  322. {
  323. #ifdef CONFIG_HOTPLUG_CPU
  324. const struct cpu_operations *ops = get_cpu_ops(cpu);
  325. if (ops && ops->cpu_can_disable)
  326. return ops->cpu_can_disable(cpu);
  327. #endif
  328. return false;
  329. }
  330. bool arch_cpu_is_hotpluggable(int num)
  331. {
  332. return cpu_can_disable(num);
  333. }
  334. static void dump_kernel_offset(void)
  335. {
  336. const unsigned long offset = kaslr_offset();
  337. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && offset > 0) {
  338. pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
  339. offset, KIMAGE_VADDR);
  340. pr_emerg("PHYS_OFFSET: 0x%llx\n", PHYS_OFFSET);
  341. } else {
  342. pr_emerg("Kernel Offset: disabled\n");
  343. }
  344. }
  345. static int arm64_panic_block_dump(struct notifier_block *self,
  346. unsigned long v, void *p)
  347. {
  348. dump_kernel_offset();
  349. dump_cpu_features();
  350. dump_mem_limit();
  351. return 0;
  352. }
  353. static struct notifier_block arm64_panic_block = {
  354. .notifier_call = arm64_panic_block_dump
  355. };
  356. static int __init register_arm64_panic_block(void)
  357. {
  358. atomic_notifier_chain_register(&panic_notifier_list,
  359. &arm64_panic_block);
  360. return 0;
  361. }
  362. device_initcall(register_arm64_panic_block);
  363. static int __init check_mmu_enabled_at_boot(void)
  364. {
  365. if (!efi_enabled(EFI_BOOT) && mmu_enabled_at_boot)
  366. panic("Non-EFI boot detected with MMU and caches enabled");
  367. return 0;
  368. }
  369. device_initcall_sync(check_mmu_enabled_at_boot);