setup.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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/utsname.h>
  26. #include <linux/initrd.h>
  27. #include <linux/console.h>
  28. #include <linux/cache.h>
  29. #include <linux/bootmem.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/screen_info.h>
  32. #include <linux/init.h>
  33. #include <linux/kexec.h>
  34. #include <linux/crash_dump.h>
  35. #include <linux/root_dev.h>
  36. #include <linux/cpu.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/smp.h>
  39. #include <linux/fs.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/memblock.h>
  42. #include <linux/of_iommu.h>
  43. #include <linux/of_fdt.h>
  44. #include <linux/of_platform.h>
  45. #include <linux/efi.h>
  46. #include <linux/personality.h>
  47. #include <linux/psci.h>
  48. #include <asm/acpi.h>
  49. #include <asm/fixmap.h>
  50. #include <asm/cpu.h>
  51. #include <asm/cputype.h>
  52. #include <asm/elf.h>
  53. #include <asm/cpufeature.h>
  54. #include <asm/cpu_ops.h>
  55. #include <asm/sections.h>
  56. #include <asm/setup.h>
  57. #include <asm/smp_plat.h>
  58. #include <asm/cacheflush.h>
  59. #include <asm/tlbflush.h>
  60. #include <asm/traps.h>
  61. #include <asm/memblock.h>
  62. #include <asm/efi.h>
  63. #include <asm/xen/hypervisor.h>
  64. unsigned long elf_hwcap __read_mostly;
  65. EXPORT_SYMBOL_GPL(elf_hwcap);
  66. #ifdef CONFIG_COMPAT
  67. #define COMPAT_ELF_HWCAP_DEFAULT \
  68. (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
  69. COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
  70. COMPAT_HWCAP_TLS|COMPAT_HWCAP_VFP|\
  71. COMPAT_HWCAP_VFPv3|COMPAT_HWCAP_VFPv4|\
  72. COMPAT_HWCAP_NEON|COMPAT_HWCAP_IDIV|\
  73. COMPAT_HWCAP_LPAE)
  74. unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
  75. unsigned int compat_elf_hwcap2 __read_mostly;
  76. #endif
  77. DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
  78. phys_addr_t __fdt_pointer __initdata;
  79. /*
  80. * Standard memory resources
  81. */
  82. static struct resource mem_res[] = {
  83. {
  84. .name = "Kernel code",
  85. .start = 0,
  86. .end = 0,
  87. .flags = IORESOURCE_MEM
  88. },
  89. {
  90. .name = "Kernel data",
  91. .start = 0,
  92. .end = 0,
  93. .flags = IORESOURCE_MEM
  94. }
  95. };
  96. #define kernel_code mem_res[0]
  97. #define kernel_data mem_res[1]
  98. /*
  99. * The recorded values of x0 .. x3 upon kernel entry.
  100. */
  101. u64 __cacheline_aligned boot_args[4];
  102. void __init smp_setup_processor_id(void)
  103. {
  104. u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
  105. cpu_logical_map(0) = mpidr;
  106. /*
  107. * clear __my_cpu_offset on boot CPU to avoid hang caused by
  108. * using percpu variable early, for example, lockdep will
  109. * access percpu variable inside lock_release
  110. */
  111. set_my_cpu_offset(0);
  112. pr_info("Booting Linux on physical CPU 0x%lx\n", (unsigned long)mpidr);
  113. }
  114. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  115. {
  116. return phys_id == cpu_logical_map(cpu);
  117. }
  118. struct mpidr_hash mpidr_hash;
  119. /**
  120. * smp_build_mpidr_hash - Pre-compute shifts required at each affinity
  121. * level in order to build a linear index from an
  122. * MPIDR value. Resulting algorithm is a collision
  123. * free hash carried out through shifting and ORing
  124. */
  125. static void __init smp_build_mpidr_hash(void)
  126. {
  127. u32 i, affinity, fs[4], bits[4], ls;
  128. u64 mask = 0;
  129. /*
  130. * Pre-scan the list of MPIDRS and filter out bits that do
  131. * not contribute to affinity levels, ie they never toggle.
  132. */
  133. for_each_possible_cpu(i)
  134. mask |= (cpu_logical_map(i) ^ cpu_logical_map(0));
  135. pr_debug("mask of set bits %#llx\n", mask);
  136. /*
  137. * Find and stash the last and first bit set at all affinity levels to
  138. * check how many bits are required to represent them.
  139. */
  140. for (i = 0; i < 4; i++) {
  141. affinity = MPIDR_AFFINITY_LEVEL(mask, i);
  142. /*
  143. * Find the MSB bit and LSB bits position
  144. * to determine how many bits are required
  145. * to express the affinity level.
  146. */
  147. ls = fls(affinity);
  148. fs[i] = affinity ? ffs(affinity) - 1 : 0;
  149. bits[i] = ls - fs[i];
  150. }
  151. /*
  152. * An index can be created from the MPIDR_EL1 by isolating the
  153. * significant bits at each affinity level and by shifting
  154. * them in order to compress the 32 bits values space to a
  155. * compressed set of values. This is equivalent to hashing
  156. * the MPIDR_EL1 through shifting and ORing. It is a collision free
  157. * hash though not minimal since some levels might contain a number
  158. * of CPUs that is not an exact power of 2 and their bit
  159. * representation might contain holes, eg MPIDR_EL1[7:0] = {0x2, 0x80}.
  160. */
  161. mpidr_hash.shift_aff[0] = MPIDR_LEVEL_SHIFT(0) + fs[0];
  162. mpidr_hash.shift_aff[1] = MPIDR_LEVEL_SHIFT(1) + fs[1] - bits[0];
  163. mpidr_hash.shift_aff[2] = MPIDR_LEVEL_SHIFT(2) + fs[2] -
  164. (bits[1] + bits[0]);
  165. mpidr_hash.shift_aff[3] = MPIDR_LEVEL_SHIFT(3) +
  166. fs[3] - (bits[2] + bits[1] + bits[0]);
  167. mpidr_hash.mask = mask;
  168. mpidr_hash.bits = bits[3] + bits[2] + bits[1] + bits[0];
  169. pr_debug("MPIDR hash: aff0[%u] aff1[%u] aff2[%u] aff3[%u] mask[%#llx] bits[%u]\n",
  170. mpidr_hash.shift_aff[0],
  171. mpidr_hash.shift_aff[1],
  172. mpidr_hash.shift_aff[2],
  173. mpidr_hash.shift_aff[3],
  174. mpidr_hash.mask,
  175. mpidr_hash.bits);
  176. /*
  177. * 4x is an arbitrary value used to warn on a hash table much bigger
  178. * than expected on most systems.
  179. */
  180. if (mpidr_hash_size() > 4 * num_possible_cpus())
  181. pr_warn("Large number of MPIDR hash buckets detected\n");
  182. __flush_dcache_area(&mpidr_hash, sizeof(struct mpidr_hash));
  183. }
  184. static void __init setup_processor(void)
  185. {
  186. u64 features;
  187. s64 block;
  188. u32 cwg;
  189. int cls;
  190. printk("CPU: AArch64 Processor [%08x] revision %d\n",
  191. read_cpuid_id(), read_cpuid_id() & 15);
  192. sprintf(init_utsname()->machine, ELF_PLATFORM);
  193. elf_hwcap = 0;
  194. cpuinfo_store_boot_cpu();
  195. /*
  196. * Check for sane CTR_EL0.CWG value.
  197. */
  198. cwg = cache_type_cwg();
  199. cls = cache_line_size();
  200. if (!cwg)
  201. pr_warn("No Cache Writeback Granule information, assuming cache line size %d\n",
  202. cls);
  203. if (L1_CACHE_BYTES < cls)
  204. pr_warn("L1_CACHE_BYTES smaller than the Cache Writeback Granule (%d < %d)\n",
  205. L1_CACHE_BYTES, cls);
  206. /*
  207. * ID_AA64ISAR0_EL1 contains 4-bit wide signed feature blocks.
  208. * The blocks we test below represent incremental functionality
  209. * for non-negative values. Negative values are reserved.
  210. */
  211. features = read_cpuid(ID_AA64ISAR0_EL1);
  212. block = cpuid_feature_extract_field(features, 4);
  213. if (block > 0) {
  214. switch (block) {
  215. default:
  216. case 2:
  217. elf_hwcap |= HWCAP_PMULL;
  218. case 1:
  219. elf_hwcap |= HWCAP_AES;
  220. case 0:
  221. break;
  222. }
  223. }
  224. if (cpuid_feature_extract_field(features, 8) > 0)
  225. elf_hwcap |= HWCAP_SHA1;
  226. if (cpuid_feature_extract_field(features, 12) > 0)
  227. elf_hwcap |= HWCAP_SHA2;
  228. if (cpuid_feature_extract_field(features, 16) > 0)
  229. elf_hwcap |= HWCAP_CRC32;
  230. block = cpuid_feature_extract_field(features, 20);
  231. if (block > 0) {
  232. switch (block) {
  233. default:
  234. case 2:
  235. elf_hwcap |= HWCAP_ATOMICS;
  236. case 1:
  237. /* RESERVED */
  238. case 0:
  239. break;
  240. }
  241. }
  242. #ifdef CONFIG_COMPAT
  243. /*
  244. * ID_ISAR5_EL1 carries similar information as above, but pertaining to
  245. * the AArch32 32-bit execution state.
  246. */
  247. features = read_cpuid(ID_ISAR5_EL1);
  248. block = cpuid_feature_extract_field(features, 4);
  249. if (block > 0) {
  250. switch (block) {
  251. default:
  252. case 2:
  253. compat_elf_hwcap2 |= COMPAT_HWCAP2_PMULL;
  254. case 1:
  255. compat_elf_hwcap2 |= COMPAT_HWCAP2_AES;
  256. case 0:
  257. break;
  258. }
  259. }
  260. if (cpuid_feature_extract_field(features, 8) > 0)
  261. compat_elf_hwcap2 |= COMPAT_HWCAP2_SHA1;
  262. if (cpuid_feature_extract_field(features, 12) > 0)
  263. compat_elf_hwcap2 |= COMPAT_HWCAP2_SHA2;
  264. if (cpuid_feature_extract_field(features, 16) > 0)
  265. compat_elf_hwcap2 |= COMPAT_HWCAP2_CRC32;
  266. #endif
  267. }
  268. static void __init setup_machine_fdt(phys_addr_t dt_phys)
  269. {
  270. void *dt_virt = fixmap_remap_fdt(dt_phys);
  271. if (!dt_virt || !early_init_dt_scan(dt_virt)) {
  272. pr_crit("\n"
  273. "Error: invalid device tree blob at physical address %pa (virtual address 0x%p)\n"
  274. "The dtb must be 8-byte aligned and must not exceed 2 MB in size\n"
  275. "\nPlease check your bootloader.",
  276. &dt_phys, dt_virt);
  277. while (true)
  278. cpu_relax();
  279. }
  280. dump_stack_set_arch_desc("%s (DT)", of_flat_dt_get_machine_name());
  281. }
  282. static void __init request_standard_resources(void)
  283. {
  284. struct memblock_region *region;
  285. struct resource *res;
  286. kernel_code.start = virt_to_phys(_text);
  287. kernel_code.end = virt_to_phys(_etext - 1);
  288. kernel_data.start = virt_to_phys(_sdata);
  289. kernel_data.end = virt_to_phys(_end - 1);
  290. for_each_memblock(memory, region) {
  291. res = alloc_bootmem_low(sizeof(*res));
  292. res->name = "System RAM";
  293. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  294. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  295. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  296. request_resource(&iomem_resource, res);
  297. if (kernel_code.start >= res->start &&
  298. kernel_code.end <= res->end)
  299. request_resource(res, &kernel_code);
  300. if (kernel_data.start >= res->start &&
  301. kernel_data.end <= res->end)
  302. request_resource(res, &kernel_data);
  303. }
  304. }
  305. #ifdef CONFIG_BLK_DEV_INITRD
  306. /*
  307. * Relocate initrd if it is not completely within the linear mapping.
  308. * This would be the case if mem= cuts out all or part of it.
  309. */
  310. static void __init relocate_initrd(void)
  311. {
  312. phys_addr_t orig_start = __virt_to_phys(initrd_start);
  313. phys_addr_t orig_end = __virt_to_phys(initrd_end);
  314. phys_addr_t ram_end = memblock_end_of_DRAM();
  315. phys_addr_t new_start;
  316. unsigned long size, to_free = 0;
  317. void *dest;
  318. if (orig_end <= ram_end)
  319. return;
  320. /*
  321. * Any of the original initrd which overlaps the linear map should
  322. * be freed after relocating.
  323. */
  324. if (orig_start < ram_end)
  325. to_free = ram_end - orig_start;
  326. size = orig_end - orig_start;
  327. if (!size)
  328. return;
  329. /* initrd needs to be relocated completely inside linear mapping */
  330. new_start = memblock_find_in_range(0, PFN_PHYS(max_pfn),
  331. size, PAGE_SIZE);
  332. if (!new_start)
  333. panic("Cannot relocate initrd of size %ld\n", size);
  334. memblock_reserve(new_start, size);
  335. initrd_start = __phys_to_virt(new_start);
  336. initrd_end = initrd_start + size;
  337. pr_info("Moving initrd from [%llx-%llx] to [%llx-%llx]\n",
  338. orig_start, orig_start + size - 1,
  339. new_start, new_start + size - 1);
  340. dest = (void *)initrd_start;
  341. if (to_free) {
  342. memcpy(dest, (void *)__phys_to_virt(orig_start), to_free);
  343. dest += to_free;
  344. }
  345. copy_from_early_mem(dest, orig_start + to_free, size - to_free);
  346. if (to_free) {
  347. pr_info("Freeing original RAMDISK from [%llx-%llx]\n",
  348. orig_start, orig_start + to_free - 1);
  349. memblock_free(orig_start, to_free);
  350. }
  351. }
  352. #else
  353. static inline void __init relocate_initrd(void)
  354. {
  355. }
  356. #endif
  357. u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID };
  358. void __init setup_arch(char **cmdline_p)
  359. {
  360. setup_processor();
  361. init_mm.start_code = (unsigned long) _text;
  362. init_mm.end_code = (unsigned long) _etext;
  363. init_mm.end_data = (unsigned long) _edata;
  364. init_mm.brk = (unsigned long) _end;
  365. *cmdline_p = boot_command_line;
  366. early_fixmap_init();
  367. early_ioremap_init();
  368. setup_machine_fdt(__fdt_pointer);
  369. parse_early_param();
  370. /*
  371. * Unmask asynchronous aborts after bringing up possible earlycon.
  372. * (Report possible System Errors once we can report this occurred)
  373. */
  374. local_async_enable();
  375. efi_init();
  376. arm64_memblock_init();
  377. /* Parse the ACPI tables for possible boot-time configuration */
  378. acpi_boot_table_init();
  379. paging_init();
  380. relocate_initrd();
  381. request_standard_resources();
  382. early_ioremap_reset();
  383. if (acpi_disabled) {
  384. unflatten_device_tree();
  385. psci_dt_init();
  386. } else {
  387. psci_acpi_init();
  388. }
  389. xen_early_init();
  390. cpu_read_bootcpu_ops();
  391. smp_init_cpus();
  392. smp_build_mpidr_hash();
  393. #ifdef CONFIG_VT
  394. #if defined(CONFIG_VGA_CONSOLE)
  395. conswitchp = &vga_con;
  396. #elif defined(CONFIG_DUMMY_CONSOLE)
  397. conswitchp = &dummy_con;
  398. #endif
  399. #endif
  400. if (boot_args[1] || boot_args[2] || boot_args[3]) {
  401. pr_err("WARNING: x1-x3 nonzero in violation of boot protocol:\n"
  402. "\tx1: %016llx\n\tx2: %016llx\n\tx3: %016llx\n"
  403. "This indicates a broken bootloader or old kernel\n",
  404. boot_args[1], boot_args[2], boot_args[3]);
  405. }
  406. }
  407. static int __init arm64_device_init(void)
  408. {
  409. if (of_have_populated_dt()) {
  410. of_iommu_init();
  411. of_platform_populate(NULL, of_default_bus_match_table,
  412. NULL, NULL);
  413. } else if (acpi_disabled) {
  414. pr_crit("Device tree not populated\n");
  415. }
  416. return 0;
  417. }
  418. arch_initcall_sync(arm64_device_init);
  419. static int __init topology_init(void)
  420. {
  421. int i;
  422. for_each_possible_cpu(i) {
  423. struct cpu *cpu = &per_cpu(cpu_data.cpu, i);
  424. cpu->hotpluggable = 1;
  425. register_cpu(cpu, i);
  426. }
  427. return 0;
  428. }
  429. subsys_initcall(topology_init);
  430. static const char *hwcap_str[] = {
  431. "fp",
  432. "asimd",
  433. "evtstrm",
  434. "aes",
  435. "pmull",
  436. "sha1",
  437. "sha2",
  438. "crc32",
  439. "atomics",
  440. NULL
  441. };
  442. #ifdef CONFIG_COMPAT
  443. static const char *compat_hwcap_str[] = {
  444. "swp",
  445. "half",
  446. "thumb",
  447. "26bit",
  448. "fastmult",
  449. "fpa",
  450. "vfp",
  451. "edsp",
  452. "java",
  453. "iwmmxt",
  454. "crunch",
  455. "thumbee",
  456. "neon",
  457. "vfpv3",
  458. "vfpv3d16",
  459. "tls",
  460. "vfpv4",
  461. "idiva",
  462. "idivt",
  463. "vfpd32",
  464. "lpae",
  465. "evtstrm"
  466. };
  467. static const char *compat_hwcap2_str[] = {
  468. "aes",
  469. "pmull",
  470. "sha1",
  471. "sha2",
  472. "crc32",
  473. NULL
  474. };
  475. #endif /* CONFIG_COMPAT */
  476. static int c_show(struct seq_file *m, void *v)
  477. {
  478. int i, j;
  479. for_each_online_cpu(i) {
  480. struct cpuinfo_arm64 *cpuinfo = &per_cpu(cpu_data, i);
  481. u32 midr = cpuinfo->reg_midr;
  482. /*
  483. * glibc reads /proc/cpuinfo to determine the number of
  484. * online processors, looking for lines beginning with
  485. * "processor". Give glibc what it expects.
  486. */
  487. seq_printf(m, "processor\t: %d\n", i);
  488. /*
  489. * Dump out the common processor features in a single line.
  490. * Userspace should read the hwcaps with getauxval(AT_HWCAP)
  491. * rather than attempting to parse this, but there's a body of
  492. * software which does already (at least for 32-bit).
  493. */
  494. seq_puts(m, "Features\t:");
  495. if (personality(current->personality) == PER_LINUX32) {
  496. #ifdef CONFIG_COMPAT
  497. for (j = 0; compat_hwcap_str[j]; j++)
  498. if (compat_elf_hwcap & (1 << j))
  499. seq_printf(m, " %s", compat_hwcap_str[j]);
  500. for (j = 0; compat_hwcap2_str[j]; j++)
  501. if (compat_elf_hwcap2 & (1 << j))
  502. seq_printf(m, " %s", compat_hwcap2_str[j]);
  503. #endif /* CONFIG_COMPAT */
  504. } else {
  505. for (j = 0; hwcap_str[j]; j++)
  506. if (elf_hwcap & (1 << j))
  507. seq_printf(m, " %s", hwcap_str[j]);
  508. }
  509. seq_puts(m, "\n");
  510. seq_printf(m, "CPU implementer\t: 0x%02x\n",
  511. MIDR_IMPLEMENTOR(midr));
  512. seq_printf(m, "CPU architecture: 8\n");
  513. seq_printf(m, "CPU variant\t: 0x%x\n", MIDR_VARIANT(midr));
  514. seq_printf(m, "CPU part\t: 0x%03x\n", MIDR_PARTNUM(midr));
  515. seq_printf(m, "CPU revision\t: %d\n\n", MIDR_REVISION(midr));
  516. }
  517. return 0;
  518. }
  519. static void *c_start(struct seq_file *m, loff_t *pos)
  520. {
  521. return *pos < 1 ? (void *)1 : NULL;
  522. }
  523. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  524. {
  525. ++*pos;
  526. return NULL;
  527. }
  528. static void c_stop(struct seq_file *m, void *v)
  529. {
  530. }
  531. const struct seq_operations cpuinfo_op = {
  532. .start = c_start,
  533. .next = c_next,
  534. .stop = c_stop,
  535. .show = c_show
  536. };