setup.c 14 KB

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