setup.c 14 KB

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