setup.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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/export.h>
  20. #include <linux/kernel.h>
  21. #include <linux/stddef.h>
  22. #include <linux/ioport.h>
  23. #include <linux/delay.h>
  24. #include <linux/utsname.h>
  25. #include <linux/initrd.h>
  26. #include <linux/console.h>
  27. #include <linux/cache.h>
  28. #include <linux/bootmem.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/screen_info.h>
  31. #include <linux/init.h>
  32. #include <linux/kexec.h>
  33. #include <linux/crash_dump.h>
  34. #include <linux/root_dev.h>
  35. #include <linux/clk-provider.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_fdt.h>
  43. #include <linux/of_platform.h>
  44. #include <linux/efi.h>
  45. #include <linux/personality.h>
  46. #include <asm/fixmap.h>
  47. #include <asm/cpu.h>
  48. #include <asm/cputype.h>
  49. #include <asm/elf.h>
  50. #include <asm/cputable.h>
  51. #include <asm/cpufeature.h>
  52. #include <asm/cpu_ops.h>
  53. #include <asm/sections.h>
  54. #include <asm/setup.h>
  55. #include <asm/smp_plat.h>
  56. #include <asm/cacheflush.h>
  57. #include <asm/tlbflush.h>
  58. #include <asm/traps.h>
  59. #include <asm/memblock.h>
  60. #include <asm/psci.h>
  61. #include <asm/efi.h>
  62. unsigned int processor_id;
  63. EXPORT_SYMBOL(processor_id);
  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. static const char *cpu_name;
  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. void __init smp_setup_processor_id(void)
  109. {
  110. u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
  111. cpu_logical_map(0) = mpidr;
  112. /*
  113. * clear __my_cpu_offset on boot CPU to avoid hang caused by
  114. * using percpu variable early, for example, lockdep will
  115. * access percpu variable inside lock_release
  116. */
  117. set_my_cpu_offset(0);
  118. pr_info("Booting Linux on physical CPU 0x%lx\n", (unsigned long)mpidr);
  119. }
  120. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  121. {
  122. return phys_id == cpu_logical_map(cpu);
  123. }
  124. struct mpidr_hash mpidr_hash;
  125. #ifdef CONFIG_SMP
  126. /**
  127. * smp_build_mpidr_hash - Pre-compute shifts required at each affinity
  128. * level in order to build a linear index from an
  129. * MPIDR value. Resulting algorithm is a collision
  130. * free hash carried out through shifting and ORing
  131. */
  132. static void __init smp_build_mpidr_hash(void)
  133. {
  134. u32 i, affinity, fs[4], bits[4], ls;
  135. u64 mask = 0;
  136. /*
  137. * Pre-scan the list of MPIDRS and filter out bits that do
  138. * not contribute to affinity levels, ie they never toggle.
  139. */
  140. for_each_possible_cpu(i)
  141. mask |= (cpu_logical_map(i) ^ cpu_logical_map(0));
  142. pr_debug("mask of set bits %#llx\n", mask);
  143. /*
  144. * Find and stash the last and first bit set at all affinity levels to
  145. * check how many bits are required to represent them.
  146. */
  147. for (i = 0; i < 4; i++) {
  148. affinity = MPIDR_AFFINITY_LEVEL(mask, i);
  149. /*
  150. * Find the MSB bit and LSB bits position
  151. * to determine how many bits are required
  152. * to express the affinity level.
  153. */
  154. ls = fls(affinity);
  155. fs[i] = affinity ? ffs(affinity) - 1 : 0;
  156. bits[i] = ls - fs[i];
  157. }
  158. /*
  159. * An index can be created from the MPIDR_EL1 by isolating the
  160. * significant bits at each affinity level and by shifting
  161. * them in order to compress the 32 bits values space to a
  162. * compressed set of values. This is equivalent to hashing
  163. * the MPIDR_EL1 through shifting and ORing. It is a collision free
  164. * hash though not minimal since some levels might contain a number
  165. * of CPUs that is not an exact power of 2 and their bit
  166. * representation might contain holes, eg MPIDR_EL1[7:0] = {0x2, 0x80}.
  167. */
  168. mpidr_hash.shift_aff[0] = MPIDR_LEVEL_SHIFT(0) + fs[0];
  169. mpidr_hash.shift_aff[1] = MPIDR_LEVEL_SHIFT(1) + fs[1] - bits[0];
  170. mpidr_hash.shift_aff[2] = MPIDR_LEVEL_SHIFT(2) + fs[2] -
  171. (bits[1] + bits[0]);
  172. mpidr_hash.shift_aff[3] = MPIDR_LEVEL_SHIFT(3) +
  173. fs[3] - (bits[2] + bits[1] + bits[0]);
  174. mpidr_hash.mask = mask;
  175. mpidr_hash.bits = bits[3] + bits[2] + bits[1] + bits[0];
  176. pr_debug("MPIDR hash: aff0[%u] aff1[%u] aff2[%u] aff3[%u] mask[%#llx] bits[%u]\n",
  177. mpidr_hash.shift_aff[0],
  178. mpidr_hash.shift_aff[1],
  179. mpidr_hash.shift_aff[2],
  180. mpidr_hash.shift_aff[3],
  181. mpidr_hash.mask,
  182. mpidr_hash.bits);
  183. /*
  184. * 4x is an arbitrary value used to warn on a hash table much bigger
  185. * than expected on most systems.
  186. */
  187. if (mpidr_hash_size() > 4 * num_possible_cpus())
  188. pr_warn("Large number of MPIDR hash buckets detected\n");
  189. __flush_dcache_area(&mpidr_hash, sizeof(struct mpidr_hash));
  190. }
  191. #endif
  192. static void __init setup_processor(void)
  193. {
  194. struct cpu_info *cpu_info;
  195. u64 features, block;
  196. u32 cwg;
  197. int cls;
  198. cpu_info = lookup_processor_type(read_cpuid_id());
  199. if (!cpu_info) {
  200. printk("CPU configuration botched (ID %08x), unable to continue.\n",
  201. read_cpuid_id());
  202. while (1);
  203. }
  204. cpu_name = cpu_info->cpu_name;
  205. printk("CPU: %s [%08x] revision %d\n",
  206. cpu_name, read_cpuid_id(), read_cpuid_id() & 15);
  207. sprintf(init_utsname()->machine, ELF_PLATFORM);
  208. elf_hwcap = 0;
  209. cpuinfo_store_boot_cpu();
  210. /*
  211. * Check for sane CTR_EL0.CWG value.
  212. */
  213. cwg = cache_type_cwg();
  214. cls = cache_line_size();
  215. if (!cwg)
  216. pr_warn("No Cache Writeback Granule information, assuming cache line size %d\n",
  217. cls);
  218. if (L1_CACHE_BYTES < cls)
  219. pr_warn("L1_CACHE_BYTES smaller than the Cache Writeback Granule (%d < %d)\n",
  220. L1_CACHE_BYTES, cls);
  221. /*
  222. * ID_AA64ISAR0_EL1 contains 4-bit wide signed feature blocks.
  223. * The blocks we test below represent incremental functionality
  224. * for non-negative values. Negative values are reserved.
  225. */
  226. features = read_cpuid(ID_AA64ISAR0_EL1);
  227. block = (features >> 4) & 0xf;
  228. if (!(block & 0x8)) {
  229. switch (block) {
  230. default:
  231. case 2:
  232. elf_hwcap |= HWCAP_PMULL;
  233. case 1:
  234. elf_hwcap |= HWCAP_AES;
  235. case 0:
  236. break;
  237. }
  238. }
  239. block = (features >> 8) & 0xf;
  240. if (block && !(block & 0x8))
  241. elf_hwcap |= HWCAP_SHA1;
  242. block = (features >> 12) & 0xf;
  243. if (block && !(block & 0x8))
  244. elf_hwcap |= HWCAP_SHA2;
  245. block = (features >> 16) & 0xf;
  246. if (block && !(block & 0x8))
  247. elf_hwcap |= HWCAP_CRC32;
  248. #ifdef CONFIG_COMPAT
  249. /*
  250. * ID_ISAR5_EL1 carries similar information as above, but pertaining to
  251. * the Aarch32 32-bit execution state.
  252. */
  253. features = read_cpuid(ID_ISAR5_EL1);
  254. block = (features >> 4) & 0xf;
  255. if (!(block & 0x8)) {
  256. switch (block) {
  257. default:
  258. case 2:
  259. compat_elf_hwcap2 |= COMPAT_HWCAP2_PMULL;
  260. case 1:
  261. compat_elf_hwcap2 |= COMPAT_HWCAP2_AES;
  262. case 0:
  263. break;
  264. }
  265. }
  266. block = (features >> 8) & 0xf;
  267. if (block && !(block & 0x8))
  268. compat_elf_hwcap2 |= COMPAT_HWCAP2_SHA1;
  269. block = (features >> 12) & 0xf;
  270. if (block && !(block & 0x8))
  271. compat_elf_hwcap2 |= COMPAT_HWCAP2_SHA2;
  272. block = (features >> 16) & 0xf;
  273. if (block && !(block & 0x8))
  274. compat_elf_hwcap2 |= COMPAT_HWCAP2_CRC32;
  275. #endif
  276. }
  277. static void __init setup_machine_fdt(phys_addr_t dt_phys)
  278. {
  279. if (!dt_phys || !early_init_dt_scan(phys_to_virt(dt_phys))) {
  280. early_print("\n"
  281. "Error: invalid device tree blob at physical address 0x%p (virtual address 0x%p)\n"
  282. "The dtb must be 8-byte aligned and passed in the first 512MB of memory\n"
  283. "\nPlease check your bootloader.\n",
  284. dt_phys, phys_to_virt(dt_phys));
  285. while (true)
  286. cpu_relax();
  287. }
  288. dump_stack_set_arch_desc("%s (DT)", of_flat_dt_get_machine_name());
  289. }
  290. /*
  291. * Limit the memory size that was specified via FDT.
  292. */
  293. static int __init early_mem(char *p)
  294. {
  295. phys_addr_t limit;
  296. if (!p)
  297. return 1;
  298. limit = memparse(p, &p) & PAGE_MASK;
  299. pr_notice("Memory limited to %lldMB\n", limit >> 20);
  300. memblock_enforce_memory_limit(limit);
  301. return 0;
  302. }
  303. early_param("mem", early_mem);
  304. static void __init request_standard_resources(void)
  305. {
  306. struct memblock_region *region;
  307. struct resource *res;
  308. kernel_code.start = virt_to_phys(_text);
  309. kernel_code.end = virt_to_phys(_etext - 1);
  310. kernel_data.start = virt_to_phys(_sdata);
  311. kernel_data.end = virt_to_phys(_end - 1);
  312. for_each_memblock(memory, region) {
  313. res = alloc_bootmem_low(sizeof(*res));
  314. res->name = "System RAM";
  315. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  316. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  317. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  318. request_resource(&iomem_resource, res);
  319. if (kernel_code.start >= res->start &&
  320. kernel_code.end <= res->end)
  321. request_resource(res, &kernel_code);
  322. if (kernel_data.start >= res->start &&
  323. kernel_data.end <= res->end)
  324. request_resource(res, &kernel_data);
  325. }
  326. }
  327. u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID };
  328. void __init setup_arch(char **cmdline_p)
  329. {
  330. setup_processor();
  331. setup_machine_fdt(__fdt_pointer);
  332. init_mm.start_code = (unsigned long) _text;
  333. init_mm.end_code = (unsigned long) _etext;
  334. init_mm.end_data = (unsigned long) _edata;
  335. init_mm.brk = (unsigned long) _end;
  336. *cmdline_p = boot_command_line;
  337. early_fixmap_init();
  338. early_ioremap_init();
  339. parse_early_param();
  340. /*
  341. * Unmask asynchronous aborts after bringing up possible earlycon.
  342. * (Report possible System Errors once we can report this occurred)
  343. */
  344. local_async_enable();
  345. efi_init();
  346. arm64_memblock_init();
  347. paging_init();
  348. request_standard_resources();
  349. efi_idmap_init();
  350. unflatten_device_tree();
  351. psci_init();
  352. cpu_read_bootcpu_ops();
  353. #ifdef CONFIG_SMP
  354. smp_init_cpus();
  355. smp_build_mpidr_hash();
  356. #endif
  357. #ifdef CONFIG_VT
  358. #if defined(CONFIG_VGA_CONSOLE)
  359. conswitchp = &vga_con;
  360. #elif defined(CONFIG_DUMMY_CONSOLE)
  361. conswitchp = &dummy_con;
  362. #endif
  363. #endif
  364. }
  365. static int __init arm64_device_init(void)
  366. {
  367. of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
  368. return 0;
  369. }
  370. arch_initcall_sync(arm64_device_init);
  371. static int __init topology_init(void)
  372. {
  373. int i;
  374. for_each_possible_cpu(i) {
  375. struct cpu *cpu = &per_cpu(cpu_data.cpu, i);
  376. cpu->hotpluggable = 1;
  377. register_cpu(cpu, i);
  378. }
  379. return 0;
  380. }
  381. subsys_initcall(topology_init);
  382. static const char *hwcap_str[] = {
  383. "fp",
  384. "asimd",
  385. "evtstrm",
  386. "aes",
  387. "pmull",
  388. "sha1",
  389. "sha2",
  390. "crc32",
  391. NULL
  392. };
  393. #ifdef CONFIG_COMPAT
  394. static const char *compat_hwcap_str[] = {
  395. "swp",
  396. "half",
  397. "thumb",
  398. "26bit",
  399. "fastmult",
  400. "fpa",
  401. "vfp",
  402. "edsp",
  403. "java",
  404. "iwmmxt",
  405. "crunch",
  406. "thumbee",
  407. "neon",
  408. "vfpv3",
  409. "vfpv3d16",
  410. "tls",
  411. "vfpv4",
  412. "idiva",
  413. "idivt",
  414. "vfpd32",
  415. "lpae",
  416. "evtstrm"
  417. };
  418. static const char *compat_hwcap2_str[] = {
  419. "aes",
  420. "pmull",
  421. "sha1",
  422. "sha2",
  423. "crc32",
  424. NULL
  425. };
  426. #endif /* CONFIG_COMPAT */
  427. static int c_show(struct seq_file *m, void *v)
  428. {
  429. int i, j;
  430. for_each_online_cpu(i) {
  431. struct cpuinfo_arm64 *cpuinfo = &per_cpu(cpu_data, i);
  432. u32 midr = cpuinfo->reg_midr;
  433. /*
  434. * glibc reads /proc/cpuinfo to determine the number of
  435. * online processors, looking for lines beginning with
  436. * "processor". Give glibc what it expects.
  437. */
  438. #ifdef CONFIG_SMP
  439. seq_printf(m, "processor\t: %d\n", i);
  440. #endif
  441. /*
  442. * Dump out the common processor features in a single line.
  443. * Userspace should read the hwcaps with getauxval(AT_HWCAP)
  444. * rather than attempting to parse this, but there's a body of
  445. * software which does already (at least for 32-bit).
  446. */
  447. seq_puts(m, "Features\t:");
  448. if (personality(current->personality) == PER_LINUX32) {
  449. #ifdef CONFIG_COMPAT
  450. for (j = 0; compat_hwcap_str[j]; j++)
  451. if (compat_elf_hwcap & (1 << j))
  452. seq_printf(m, " %s", compat_hwcap_str[j]);
  453. for (j = 0; compat_hwcap2_str[j]; j++)
  454. if (compat_elf_hwcap2 & (1 << j))
  455. seq_printf(m, " %s", compat_hwcap2_str[j]);
  456. #endif /* CONFIG_COMPAT */
  457. } else {
  458. for (j = 0; hwcap_str[j]; j++)
  459. if (elf_hwcap & (1 << j))
  460. seq_printf(m, " %s", hwcap_str[j]);
  461. }
  462. seq_puts(m, "\n");
  463. seq_printf(m, "CPU implementer\t: 0x%02x\n",
  464. MIDR_IMPLEMENTOR(midr));
  465. seq_printf(m, "CPU architecture: 8\n");
  466. seq_printf(m, "CPU variant\t: 0x%x\n", MIDR_VARIANT(midr));
  467. seq_printf(m, "CPU part\t: 0x%03x\n", MIDR_PARTNUM(midr));
  468. seq_printf(m, "CPU revision\t: %d\n\n", MIDR_REVISION(midr));
  469. }
  470. return 0;
  471. }
  472. static void *c_start(struct seq_file *m, loff_t *pos)
  473. {
  474. return *pos < 1 ? (void *)1 : NULL;
  475. }
  476. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  477. {
  478. ++*pos;
  479. return NULL;
  480. }
  481. static void c_stop(struct seq_file *m, void *v)
  482. {
  483. }
  484. const struct seq_operations cpuinfo_op = {
  485. .start = c_start,
  486. .next = c_next,
  487. .stop = c_stop,
  488. .show = c_show
  489. };