setup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Based on arch/arm/kernel/setup.c
  3. *
  4. * Copyright (C) 1995-2001 Russell King
  5. * Copyright (C) 2012 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/acpi.h>
  20. #include <linux/export.h>
  21. #include <linux/kernel.h>
  22. #include <linux/stddef.h>
  23. #include <linux/ioport.h>
  24. #include <linux/delay.h>
  25. #include <linux/initrd.h>
  26. #include <linux/console.h>
  27. #include <linux/cache.h>
  28. #include <linux/bootmem.h>
  29. #include <linux/screen_info.h>
  30. #include <linux/init.h>
  31. #include <linux/kexec.h>
  32. #include <linux/root_dev.h>
  33. #include <linux/cpu.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/smp.h>
  36. #include <linux/fs.h>
  37. #include <linux/proc_fs.h>
  38. #include <linux/memblock.h>
  39. #include <linux/of_fdt.h>
  40. #include <linux/efi.h>
  41. #include <linux/psci.h>
  42. #include <linux/sched/task.h>
  43. #include <linux/mm.h>
  44. #include <asm/acpi.h>
  45. #include <asm/fixmap.h>
  46. #include <asm/cpu.h>
  47. #include <asm/cputype.h>
  48. #include <asm/daifflags.h>
  49. #include <asm/elf.h>
  50. #include <asm/cpufeature.h>
  51. #include <asm/cpu_ops.h>
  52. #include <asm/kasan.h>
  53. #include <asm/numa.h>
  54. #include <asm/sections.h>
  55. #include <asm/setup.h>
  56. #include <asm/smp_plat.h>
  57. #include <asm/cacheflush.h>
  58. #include <asm/tlbflush.h>
  59. #include <asm/traps.h>
  60. #include <asm/memblock.h>
  61. #include <asm/efi.h>
  62. #include <asm/xen/hypervisor.h>
  63. #include <asm/mmu_context.h>
  64. static int num_standard_resources;
  65. static struct resource *standard_resources;
  66. phys_addr_t __fdt_pointer __initdata;
  67. /*
  68. * Standard memory resources
  69. */
  70. static struct resource mem_res[] = {
  71. {
  72. .name = "Kernel code",
  73. .start = 0,
  74. .end = 0,
  75. .flags = IORESOURCE_SYSTEM_RAM
  76. },
  77. {
  78. .name = "Kernel data",
  79. .start = 0,
  80. .end = 0,
  81. .flags = IORESOURCE_SYSTEM_RAM
  82. }
  83. };
  84. #define kernel_code mem_res[0]
  85. #define kernel_data mem_res[1]
  86. /*
  87. * The recorded values of x0 .. x3 upon kernel entry.
  88. */
  89. u64 __cacheline_aligned boot_args[4];
  90. void __init smp_setup_processor_id(void)
  91. {
  92. u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
  93. cpu_logical_map(0) = mpidr;
  94. /*
  95. * clear __my_cpu_offset on boot CPU to avoid hang caused by
  96. * using percpu variable early, for example, lockdep will
  97. * access percpu variable inside lock_release
  98. */
  99. set_my_cpu_offset(0);
  100. pr_info("Booting Linux on physical CPU 0x%010lx [0x%08x]\n",
  101. (unsigned long)mpidr, read_cpuid_id());
  102. }
  103. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  104. {
  105. return phys_id == cpu_logical_map(cpu);
  106. }
  107. struct mpidr_hash mpidr_hash;
  108. /**
  109. * smp_build_mpidr_hash - Pre-compute shifts required at each affinity
  110. * level in order to build a linear index from an
  111. * MPIDR value. Resulting algorithm is a collision
  112. * free hash carried out through shifting and ORing
  113. */
  114. static void __init smp_build_mpidr_hash(void)
  115. {
  116. u32 i, affinity, fs[4], bits[4], ls;
  117. u64 mask = 0;
  118. /*
  119. * Pre-scan the list of MPIDRS and filter out bits that do
  120. * not contribute to affinity levels, ie they never toggle.
  121. */
  122. for_each_possible_cpu(i)
  123. mask |= (cpu_logical_map(i) ^ cpu_logical_map(0));
  124. pr_debug("mask of set bits %#llx\n", mask);
  125. /*
  126. * Find and stash the last and first bit set at all affinity levels to
  127. * check how many bits are required to represent them.
  128. */
  129. for (i = 0; i < 4; i++) {
  130. affinity = MPIDR_AFFINITY_LEVEL(mask, i);
  131. /*
  132. * Find the MSB bit and LSB bits position
  133. * to determine how many bits are required
  134. * to express the affinity level.
  135. */
  136. ls = fls(affinity);
  137. fs[i] = affinity ? ffs(affinity) - 1 : 0;
  138. bits[i] = ls - fs[i];
  139. }
  140. /*
  141. * An index can be created from the MPIDR_EL1 by isolating the
  142. * significant bits at each affinity level and by shifting
  143. * them in order to compress the 32 bits values space to a
  144. * compressed set of values. This is equivalent to hashing
  145. * the MPIDR_EL1 through shifting and ORing. It is a collision free
  146. * hash though not minimal since some levels might contain a number
  147. * of CPUs that is not an exact power of 2 and their bit
  148. * representation might contain holes, eg MPIDR_EL1[7:0] = {0x2, 0x80}.
  149. */
  150. mpidr_hash.shift_aff[0] = MPIDR_LEVEL_SHIFT(0) + fs[0];
  151. mpidr_hash.shift_aff[1] = MPIDR_LEVEL_SHIFT(1) + fs[1] - bits[0];
  152. mpidr_hash.shift_aff[2] = MPIDR_LEVEL_SHIFT(2) + fs[2] -
  153. (bits[1] + bits[0]);
  154. mpidr_hash.shift_aff[3] = MPIDR_LEVEL_SHIFT(3) +
  155. fs[3] - (bits[2] + bits[1] + bits[0]);
  156. mpidr_hash.mask = mask;
  157. mpidr_hash.bits = bits[3] + bits[2] + bits[1] + bits[0];
  158. pr_debug("MPIDR hash: aff0[%u] aff1[%u] aff2[%u] aff3[%u] mask[%#llx] bits[%u]\n",
  159. mpidr_hash.shift_aff[0],
  160. mpidr_hash.shift_aff[1],
  161. mpidr_hash.shift_aff[2],
  162. mpidr_hash.shift_aff[3],
  163. mpidr_hash.mask,
  164. mpidr_hash.bits);
  165. /*
  166. * 4x is an arbitrary value used to warn on a hash table much bigger
  167. * than expected on most systems.
  168. */
  169. if (mpidr_hash_size() > 4 * num_possible_cpus())
  170. pr_warn("Large number of MPIDR hash buckets detected\n");
  171. }
  172. static void __init setup_machine_fdt(phys_addr_t dt_phys)
  173. {
  174. void *dt_virt = fixmap_remap_fdt(dt_phys);
  175. const char *name;
  176. if (!dt_virt || !early_init_dt_scan(dt_virt)) {
  177. pr_crit("\n"
  178. "Error: invalid device tree blob at physical address %pa (virtual address 0x%p)\n"
  179. "The dtb must be 8-byte aligned and must not exceed 2 MB in size\n"
  180. "\nPlease check your bootloader.",
  181. &dt_phys, dt_virt);
  182. while (true)
  183. cpu_relax();
  184. }
  185. name = of_flat_dt_get_machine_name();
  186. if (!name)
  187. return;
  188. pr_info("Machine model: %s\n", name);
  189. dump_stack_set_arch_desc("%s (DT)", name);
  190. }
  191. static void __init request_standard_resources(void)
  192. {
  193. struct memblock_region *region;
  194. struct resource *res;
  195. unsigned long i = 0;
  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. num_standard_resources = memblock.memory.cnt;
  201. standard_resources = alloc_bootmem_low(num_standard_resources *
  202. sizeof(*standard_resources));
  203. for_each_memblock(memory, region) {
  204. res = &standard_resources[i++];
  205. if (memblock_is_nomap(region)) {
  206. res->name = "reserved";
  207. res->flags = IORESOURCE_MEM;
  208. } else {
  209. res->name = "System RAM";
  210. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  211. }
  212. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  213. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  214. request_resource(&iomem_resource, res);
  215. if (kernel_code.start >= res->start &&
  216. kernel_code.end <= res->end)
  217. request_resource(res, &kernel_code);
  218. if (kernel_data.start >= res->start &&
  219. kernel_data.end <= res->end)
  220. request_resource(res, &kernel_data);
  221. #ifdef CONFIG_KEXEC_CORE
  222. /* Userspace will find "Crash kernel" region in /proc/iomem. */
  223. if (crashk_res.end && crashk_res.start >= res->start &&
  224. crashk_res.end <= res->end)
  225. request_resource(res, &crashk_res);
  226. #endif
  227. }
  228. }
  229. static int __init reserve_memblock_reserved_regions(void)
  230. {
  231. u64 i, j;
  232. for (i = 0; i < num_standard_resources; ++i) {
  233. struct resource *mem = &standard_resources[i];
  234. phys_addr_t r_start, r_end, mem_size = resource_size(mem);
  235. if (!memblock_is_region_reserved(mem->start, mem_size))
  236. continue;
  237. for_each_reserved_mem_region(j, &r_start, &r_end) {
  238. resource_size_t start, end;
  239. start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
  240. end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
  241. if (start > mem->end || end < mem->start)
  242. continue;
  243. reserve_region_with_split(mem, start, end, "reserved");
  244. }
  245. }
  246. return 0;
  247. }
  248. arch_initcall(reserve_memblock_reserved_regions);
  249. u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID };
  250. void __init setup_arch(char **cmdline_p)
  251. {
  252. init_mm.start_code = (unsigned long) _text;
  253. init_mm.end_code = (unsigned long) _etext;
  254. init_mm.end_data = (unsigned long) _edata;
  255. init_mm.brk = (unsigned long) _end;
  256. *cmdline_p = boot_command_line;
  257. early_fixmap_init();
  258. early_ioremap_init();
  259. setup_machine_fdt(__fdt_pointer);
  260. parse_early_param();
  261. /*
  262. * Unmask asynchronous aborts and fiq after bringing up possible
  263. * earlycon. (Report possible System Errors once we can report this
  264. * occurred).
  265. */
  266. local_daif_restore(DAIF_PROCCTX_NOIRQ);
  267. /*
  268. * TTBR0 is only used for the identity mapping at this stage. Make it
  269. * point to zero page to avoid speculatively fetching new entries.
  270. */
  271. cpu_uninstall_idmap();
  272. xen_early_init();
  273. efi_init();
  274. arm64_memblock_init();
  275. paging_init();
  276. acpi_table_upgrade();
  277. /* Parse the ACPI tables for possible boot-time configuration */
  278. acpi_boot_table_init();
  279. if (acpi_disabled)
  280. unflatten_device_tree();
  281. bootmem_init();
  282. kasan_init();
  283. request_standard_resources();
  284. early_ioremap_reset();
  285. if (acpi_disabled)
  286. psci_dt_init();
  287. else
  288. psci_acpi_init();
  289. cpu_read_bootcpu_ops();
  290. smp_init_cpus();
  291. smp_build_mpidr_hash();
  292. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  293. /*
  294. * Make sure init_thread_info.ttbr0 always generates translation
  295. * faults in case uaccess_enable() is inadvertently called by the init
  296. * thread.
  297. */
  298. init_task.thread_info.ttbr0 = __pa_symbol(empty_zero_page);
  299. #endif
  300. #ifdef CONFIG_VT
  301. #if defined(CONFIG_VGA_CONSOLE)
  302. conswitchp = &vga_con;
  303. #elif defined(CONFIG_DUMMY_CONSOLE)
  304. conswitchp = &dummy_con;
  305. #endif
  306. #endif
  307. if (boot_args[1] || boot_args[2] || boot_args[3]) {
  308. pr_err("WARNING: x1-x3 nonzero in violation of boot protocol:\n"
  309. "\tx1: %016llx\n\tx2: %016llx\n\tx3: %016llx\n"
  310. "This indicates a broken bootloader or old kernel\n",
  311. boot_args[1], boot_args[2], boot_args[3]);
  312. }
  313. }
  314. static int __init topology_init(void)
  315. {
  316. int i;
  317. for_each_online_node(i)
  318. register_one_node(i);
  319. for_each_possible_cpu(i) {
  320. struct cpu *cpu = &per_cpu(cpu_data.cpu, i);
  321. cpu->hotpluggable = 1;
  322. register_cpu(cpu, i);
  323. }
  324. return 0;
  325. }
  326. subsys_initcall(topology_init);
  327. /*
  328. * Dump out kernel offset information on panic.
  329. */
  330. static int dump_kernel_offset(struct notifier_block *self, unsigned long v,
  331. void *p)
  332. {
  333. const unsigned long offset = kaslr_offset();
  334. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && offset > 0) {
  335. pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
  336. offset, KIMAGE_VADDR);
  337. } else {
  338. pr_emerg("Kernel Offset: disabled\n");
  339. }
  340. return 0;
  341. }
  342. static struct notifier_block kernel_offset_notifier = {
  343. .notifier_call = dump_kernel_offset
  344. };
  345. static int __init register_kernel_offset_dumper(void)
  346. {
  347. atomic_notifier_chain_register(&panic_notifier_list,
  348. &kernel_offset_notifier);
  349. return 0;
  350. }
  351. __initcall(register_kernel_offset_dumper);