setup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. static const char *machine_name;
  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. void __init early_print(const char *str, ...)
  101. {
  102. char buf[256];
  103. va_list ap;
  104. va_start(ap, str);
  105. vsnprintf(buf, sizeof(buf), str, ap);
  106. va_end(ap);
  107. printk("%s", buf);
  108. }
  109. void __init smp_setup_processor_id(void)
  110. {
  111. /*
  112. * clear __my_cpu_offset on boot CPU to avoid hang caused by
  113. * using percpu variable early, for example, lockdep will
  114. * access percpu variable inside lock_release
  115. */
  116. set_my_cpu_offset(0);
  117. }
  118. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  119. {
  120. return phys_id == cpu_logical_map(cpu);
  121. }
  122. struct mpidr_hash mpidr_hash;
  123. #ifdef CONFIG_SMP
  124. /**
  125. * smp_build_mpidr_hash - Pre-compute shifts required at each affinity
  126. * level in order to build a linear index from an
  127. * MPIDR value. Resulting algorithm is a collision
  128. * free hash carried out through shifting and ORing
  129. */
  130. static void __init smp_build_mpidr_hash(void)
  131. {
  132. u32 i, affinity, fs[4], bits[4], ls;
  133. u64 mask = 0;
  134. /*
  135. * Pre-scan the list of MPIDRS and filter out bits that do
  136. * not contribute to affinity levels, ie they never toggle.
  137. */
  138. for_each_possible_cpu(i)
  139. mask |= (cpu_logical_map(i) ^ cpu_logical_map(0));
  140. pr_debug("mask of set bits %#llx\n", mask);
  141. /*
  142. * Find and stash the last and first bit set at all affinity levels to
  143. * check how many bits are required to represent them.
  144. */
  145. for (i = 0; i < 4; i++) {
  146. affinity = MPIDR_AFFINITY_LEVEL(mask, i);
  147. /*
  148. * Find the MSB bit and LSB bits position
  149. * to determine how many bits are required
  150. * to express the affinity level.
  151. */
  152. ls = fls(affinity);
  153. fs[i] = affinity ? ffs(affinity) - 1 : 0;
  154. bits[i] = ls - fs[i];
  155. }
  156. /*
  157. * An index can be created from the MPIDR_EL1 by isolating the
  158. * significant bits at each affinity level and by shifting
  159. * them in order to compress the 32 bits values space to a
  160. * compressed set of values. This is equivalent to hashing
  161. * the MPIDR_EL1 through shifting and ORing. It is a collision free
  162. * hash though not minimal since some levels might contain a number
  163. * of CPUs that is not an exact power of 2 and their bit
  164. * representation might contain holes, eg MPIDR_EL1[7:0] = {0x2, 0x80}.
  165. */
  166. mpidr_hash.shift_aff[0] = MPIDR_LEVEL_SHIFT(0) + fs[0];
  167. mpidr_hash.shift_aff[1] = MPIDR_LEVEL_SHIFT(1) + fs[1] - bits[0];
  168. mpidr_hash.shift_aff[2] = MPIDR_LEVEL_SHIFT(2) + fs[2] -
  169. (bits[1] + bits[0]);
  170. mpidr_hash.shift_aff[3] = MPIDR_LEVEL_SHIFT(3) +
  171. fs[3] - (bits[2] + bits[1] + bits[0]);
  172. mpidr_hash.mask = mask;
  173. mpidr_hash.bits = bits[3] + bits[2] + bits[1] + bits[0];
  174. pr_debug("MPIDR hash: aff0[%u] aff1[%u] aff2[%u] aff3[%u] mask[%#llx] bits[%u]\n",
  175. mpidr_hash.shift_aff[0],
  176. mpidr_hash.shift_aff[1],
  177. mpidr_hash.shift_aff[2],
  178. mpidr_hash.shift_aff[3],
  179. mpidr_hash.mask,
  180. mpidr_hash.bits);
  181. /*
  182. * 4x is an arbitrary value used to warn on a hash table much bigger
  183. * than expected on most systems.
  184. */
  185. if (mpidr_hash_size() > 4 * num_possible_cpus())
  186. pr_warn("Large number of MPIDR hash buckets detected\n");
  187. __flush_dcache_area(&mpidr_hash, sizeof(struct mpidr_hash));
  188. }
  189. #endif
  190. static void __init setup_processor(void)
  191. {
  192. struct cpu_info *cpu_info;
  193. u64 features, block;
  194. u32 cwg;
  195. int cls;
  196. cpu_info = lookup_processor_type(read_cpuid_id());
  197. if (!cpu_info) {
  198. printk("CPU configuration botched (ID %08x), unable to continue.\n",
  199. read_cpuid_id());
  200. while (1);
  201. }
  202. cpu_name = cpu_info->cpu_name;
  203. printk("CPU: %s [%08x] revision %d\n",
  204. cpu_name, read_cpuid_id(), read_cpuid_id() & 15);
  205. sprintf(init_utsname()->machine, ELF_PLATFORM);
  206. elf_hwcap = 0;
  207. cpuinfo_store_boot_cpu();
  208. /*
  209. * Check for sane CTR_EL0.CWG value.
  210. */
  211. cwg = cache_type_cwg();
  212. cls = cache_line_size();
  213. if (!cwg)
  214. pr_warn("No Cache Writeback Granule information, assuming cache line size %d\n",
  215. cls);
  216. if (L1_CACHE_BYTES < cls)
  217. pr_warn("L1_CACHE_BYTES smaller than the Cache Writeback Granule (%d < %d)\n",
  218. L1_CACHE_BYTES, cls);
  219. /*
  220. * ID_AA64ISAR0_EL1 contains 4-bit wide signed feature blocks.
  221. * The blocks we test below represent incremental functionality
  222. * for non-negative values. Negative values are reserved.
  223. */
  224. features = read_cpuid(ID_AA64ISAR0_EL1);
  225. block = (features >> 4) & 0xf;
  226. if (!(block & 0x8)) {
  227. switch (block) {
  228. default:
  229. case 2:
  230. elf_hwcap |= HWCAP_PMULL;
  231. case 1:
  232. elf_hwcap |= HWCAP_AES;
  233. case 0:
  234. break;
  235. }
  236. }
  237. block = (features >> 8) & 0xf;
  238. if (block && !(block & 0x8))
  239. elf_hwcap |= HWCAP_SHA1;
  240. block = (features >> 12) & 0xf;
  241. if (block && !(block & 0x8))
  242. elf_hwcap |= HWCAP_SHA2;
  243. block = (features >> 16) & 0xf;
  244. if (block && !(block & 0x8))
  245. elf_hwcap |= HWCAP_CRC32;
  246. #ifdef CONFIG_COMPAT
  247. /*
  248. * ID_ISAR5_EL1 carries similar information as above, but pertaining to
  249. * the Aarch32 32-bit execution state.
  250. */
  251. features = read_cpuid(ID_ISAR5_EL1);
  252. block = (features >> 4) & 0xf;
  253. if (!(block & 0x8)) {
  254. switch (block) {
  255. default:
  256. case 2:
  257. compat_elf_hwcap2 |= COMPAT_HWCAP2_PMULL;
  258. case 1:
  259. compat_elf_hwcap2 |= COMPAT_HWCAP2_AES;
  260. case 0:
  261. break;
  262. }
  263. }
  264. block = (features >> 8) & 0xf;
  265. if (block && !(block & 0x8))
  266. compat_elf_hwcap2 |= COMPAT_HWCAP2_SHA1;
  267. block = (features >> 12) & 0xf;
  268. if (block && !(block & 0x8))
  269. compat_elf_hwcap2 |= COMPAT_HWCAP2_SHA2;
  270. block = (features >> 16) & 0xf;
  271. if (block && !(block & 0x8))
  272. compat_elf_hwcap2 |= COMPAT_HWCAP2_CRC32;
  273. #endif
  274. }
  275. static void __init setup_machine_fdt(phys_addr_t dt_phys)
  276. {
  277. if (!dt_phys || !early_init_dt_scan(phys_to_virt(dt_phys))) {
  278. early_print("\n"
  279. "Error: invalid device tree blob at physical address 0x%p (virtual address 0x%p)\n"
  280. "The dtb must be 8-byte aligned and passed in the first 512MB of memory\n"
  281. "\nPlease check your bootloader.\n",
  282. dt_phys, phys_to_virt(dt_phys));
  283. while (true)
  284. cpu_relax();
  285. }
  286. machine_name = of_flat_dt_get_machine_name();
  287. dump_stack_set_arch_desc("%s (DT)", of_flat_dt_get_machine_name());
  288. }
  289. /*
  290. * Limit the memory size that was specified via FDT.
  291. */
  292. static int __init early_mem(char *p)
  293. {
  294. phys_addr_t limit;
  295. if (!p)
  296. return 1;
  297. limit = memparse(p, &p) & PAGE_MASK;
  298. pr_notice("Memory limited to %lldMB\n", limit >> 20);
  299. memblock_enforce_memory_limit(limit);
  300. return 0;
  301. }
  302. early_param("mem", early_mem);
  303. static void __init request_standard_resources(void)
  304. {
  305. struct memblock_region *region;
  306. struct resource *res;
  307. kernel_code.start = virt_to_phys(_text);
  308. kernel_code.end = virt_to_phys(_etext - 1);
  309. kernel_data.start = virt_to_phys(_sdata);
  310. kernel_data.end = virt_to_phys(_end - 1);
  311. for_each_memblock(memory, region) {
  312. res = alloc_bootmem_low(sizeof(*res));
  313. res->name = "System RAM";
  314. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  315. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  316. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  317. request_resource(&iomem_resource, res);
  318. if (kernel_code.start >= res->start &&
  319. kernel_code.end <= res->end)
  320. request_resource(res, &kernel_code);
  321. if (kernel_data.start >= res->start &&
  322. kernel_data.end <= res->end)
  323. request_resource(res, &kernel_data);
  324. }
  325. }
  326. u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID };
  327. void __init setup_arch(char **cmdline_p)
  328. {
  329. setup_processor();
  330. setup_machine_fdt(__fdt_pointer);
  331. init_mm.start_code = (unsigned long) _text;
  332. init_mm.end_code = (unsigned long) _etext;
  333. init_mm.end_data = (unsigned long) _edata;
  334. init_mm.brk = (unsigned long) _end;
  335. *cmdline_p = boot_command_line;
  336. early_ioremap_init();
  337. parse_early_param();
  338. /*
  339. * Unmask asynchronous aborts after bringing up possible earlycon.
  340. * (Report possible System Errors once we can report this occurred)
  341. */
  342. local_async_enable();
  343. efi_init();
  344. arm64_memblock_init();
  345. paging_init();
  346. request_standard_resources();
  347. efi_idmap_init();
  348. early_ioremap_reset();
  349. unflatten_device_tree();
  350. psci_init();
  351. cpu_logical_map(0) = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
  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. NULL
  418. };
  419. static const char *compat_hwcap2_str[] = {
  420. "aes",
  421. "pmull",
  422. "sha1",
  423. "sha2",
  424. "crc32",
  425. NULL
  426. };
  427. #endif /* CONFIG_COMPAT */
  428. static int c_show(struct seq_file *m, void *v)
  429. {
  430. int i, j;
  431. pr_err("Dump cpuinfo\n");
  432. seq_printf(m, "Processor\t: %s rev %d (%s)\n",
  433. cpu_name, read_cpuid_id() & 15, ELF_PLATFORM);
  434. for_each_online_cpu(i) {
  435. struct cpuinfo_arm64 *cpuinfo = &per_cpu(cpu_data, i);
  436. u32 midr = cpuinfo->reg_midr;
  437. /*
  438. * glibc reads /proc/cpuinfo to determine the number of
  439. * online processors, looking for lines beginning with
  440. * "processor". Give glibc what it expects.
  441. */
  442. seq_printf(m, "processor\t: %d\n", i);
  443. seq_printf(m, "model name\t: %s rev %d (%s)\n",
  444. cpu_name, read_cpuid_id() & 15, ELF_PLATFORM);
  445. seq_printf(m, "BogoMIPS\t: %lu.%02lu\n",
  446. loops_per_jiffy / (500000UL/HZ),
  447. loops_per_jiffy / (5000UL/HZ) % 100);
  448. /*
  449. * Dump out the common processor features in a single line.
  450. * Userspace should read the hwcaps with getauxval(AT_HWCAP)
  451. * rather than attempting to parse this, but there's a body of
  452. * software which does already (at least for 32-bit).
  453. */
  454. seq_puts(m, "Features\t:");
  455. if (personality(current->personality) == PER_LINUX32) {
  456. #ifdef CONFIG_COMPAT
  457. for (j = 0; compat_hwcap_str[j]; j++)
  458. if (compat_elf_hwcap & (1 << j))
  459. seq_printf(m, " %s", compat_hwcap_str[j]);
  460. for (j = 0; compat_hwcap2_str[j]; j++)
  461. if (compat_elf_hwcap2 & (1 << j))
  462. seq_printf(m, " %s", compat_hwcap2_str[j]);
  463. #endif /* CONFIG_COMPAT */
  464. } else {
  465. for (j = 0; hwcap_str[j]; j++)
  466. if (elf_hwcap & (1 << j))
  467. seq_printf(m, " %s", hwcap_str[j]);
  468. }
  469. seq_puts(m, "\n");
  470. seq_printf(m, "CPU implementer\t: 0x%02x\n",
  471. MIDR_IMPLEMENTOR(midr));
  472. seq_printf(m, "CPU architecture: 8\n");
  473. seq_printf(m, "CPU variant\t: 0x%x\n", MIDR_VARIANT(midr));
  474. seq_printf(m, "CPU part\t: 0x%03x\n", MIDR_PARTNUM(midr));
  475. seq_printf(m, "CPU revision\t: %d\n\n", MIDR_REVISION(midr));
  476. }
  477. seq_printf(m, "Hardware\t: %s\n", machine_name);
  478. return 0;
  479. }
  480. static void *c_start(struct seq_file *m, loff_t *pos)
  481. {
  482. return *pos < 1 ? (void *)1 : NULL;
  483. }
  484. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  485. {
  486. ++*pos;
  487. return NULL;
  488. }
  489. static void c_stop(struct seq_file *m, void *v)
  490. {
  491. }
  492. const struct seq_operations cpuinfo_op = {
  493. .start = c_start,
  494. .next = c_next,
  495. .stop = c_stop,
  496. .show = c_show
  497. };