setup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 <asm/fixmap.h>
  46. #include <asm/cpu.h>
  47. #include <asm/cputype.h>
  48. #include <asm/elf.h>
  49. #include <asm/cputable.h>
  50. #include <asm/cpu_ops.h>
  51. #include <asm/sections.h>
  52. #include <asm/setup.h>
  53. #include <asm/smp_plat.h>
  54. #include <asm/cacheflush.h>
  55. #include <asm/tlbflush.h>
  56. #include <asm/traps.h>
  57. #include <asm/memblock.h>
  58. #include <asm/psci.h>
  59. #include <asm/efi.h>
  60. unsigned int processor_id;
  61. EXPORT_SYMBOL(processor_id);
  62. unsigned long elf_hwcap __read_mostly;
  63. EXPORT_SYMBOL_GPL(elf_hwcap);
  64. #ifdef CONFIG_COMPAT
  65. #define COMPAT_ELF_HWCAP_DEFAULT \
  66. (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
  67. COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
  68. COMPAT_HWCAP_TLS|COMPAT_HWCAP_VFP|\
  69. COMPAT_HWCAP_VFPv3|COMPAT_HWCAP_VFPv4|\
  70. COMPAT_HWCAP_NEON|COMPAT_HWCAP_IDIV)
  71. unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
  72. unsigned int compat_elf_hwcap2 __read_mostly;
  73. #endif
  74. static const char *cpu_name;
  75. static const char *machine_name;
  76. phys_addr_t __fdt_pointer __initdata;
  77. /*
  78. * Standard memory resources
  79. */
  80. static struct resource mem_res[] = {
  81. {
  82. .name = "Kernel code",
  83. .start = 0,
  84. .end = 0,
  85. .flags = IORESOURCE_MEM
  86. },
  87. {
  88. .name = "Kernel data",
  89. .start = 0,
  90. .end = 0,
  91. .flags = IORESOURCE_MEM
  92. }
  93. };
  94. #define kernel_code mem_res[0]
  95. #define kernel_data mem_res[1]
  96. void __init early_print(const char *str, ...)
  97. {
  98. char buf[256];
  99. va_list ap;
  100. va_start(ap, str);
  101. vsnprintf(buf, sizeof(buf), str, ap);
  102. va_end(ap);
  103. printk("%s", buf);
  104. }
  105. void __init smp_setup_processor_id(void)
  106. {
  107. /*
  108. * clear __my_cpu_offset on boot CPU to avoid hang caused by
  109. * using percpu variable early, for example, lockdep will
  110. * access percpu variable inside lock_release
  111. */
  112. set_my_cpu_offset(0);
  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. #ifdef CONFIG_SMP
  120. /**
  121. * smp_build_mpidr_hash - Pre-compute shifts required at each affinity
  122. * level in order to build a linear index from an
  123. * MPIDR value. Resulting algorithm is a collision
  124. * free hash carried out through shifting and ORing
  125. */
  126. static void __init smp_build_mpidr_hash(void)
  127. {
  128. u32 i, affinity, fs[4], bits[4], ls;
  129. u64 mask = 0;
  130. /*
  131. * Pre-scan the list of MPIDRS and filter out bits that do
  132. * not contribute to affinity levels, ie they never toggle.
  133. */
  134. for_each_possible_cpu(i)
  135. mask |= (cpu_logical_map(i) ^ cpu_logical_map(0));
  136. pr_debug("mask of set bits %#llx\n", mask);
  137. /*
  138. * Find and stash the last and first bit set at all affinity levels to
  139. * check how many bits are required to represent them.
  140. */
  141. for (i = 0; i < 4; i++) {
  142. affinity = MPIDR_AFFINITY_LEVEL(mask, i);
  143. /*
  144. * Find the MSB bit and LSB bits position
  145. * to determine how many bits are required
  146. * to express the affinity level.
  147. */
  148. ls = fls(affinity);
  149. fs[i] = affinity ? ffs(affinity) - 1 : 0;
  150. bits[i] = ls - fs[i];
  151. }
  152. /*
  153. * An index can be created from the MPIDR_EL1 by isolating the
  154. * significant bits at each affinity level and by shifting
  155. * them in order to compress the 32 bits values space to a
  156. * compressed set of values. This is equivalent to hashing
  157. * the MPIDR_EL1 through shifting and ORing. It is a collision free
  158. * hash though not minimal since some levels might contain a number
  159. * of CPUs that is not an exact power of 2 and their bit
  160. * representation might contain holes, eg MPIDR_EL1[7:0] = {0x2, 0x80}.
  161. */
  162. mpidr_hash.shift_aff[0] = MPIDR_LEVEL_SHIFT(0) + fs[0];
  163. mpidr_hash.shift_aff[1] = MPIDR_LEVEL_SHIFT(1) + fs[1] - bits[0];
  164. mpidr_hash.shift_aff[2] = MPIDR_LEVEL_SHIFT(2) + fs[2] -
  165. (bits[1] + bits[0]);
  166. mpidr_hash.shift_aff[3] = MPIDR_LEVEL_SHIFT(3) +
  167. fs[3] - (bits[2] + bits[1] + bits[0]);
  168. mpidr_hash.mask = mask;
  169. mpidr_hash.bits = bits[3] + bits[2] + bits[1] + bits[0];
  170. pr_debug("MPIDR hash: aff0[%u] aff1[%u] aff2[%u] aff3[%u] mask[%#llx] bits[%u]\n",
  171. mpidr_hash.shift_aff[0],
  172. mpidr_hash.shift_aff[1],
  173. mpidr_hash.shift_aff[2],
  174. mpidr_hash.shift_aff[3],
  175. mpidr_hash.mask,
  176. mpidr_hash.bits);
  177. /*
  178. * 4x is an arbitrary value used to warn on a hash table much bigger
  179. * than expected on most systems.
  180. */
  181. if (mpidr_hash_size() > 4 * num_possible_cpus())
  182. pr_warn("Large number of MPIDR hash buckets detected\n");
  183. __flush_dcache_area(&mpidr_hash, sizeof(struct mpidr_hash));
  184. }
  185. #endif
  186. static void __init setup_processor(void)
  187. {
  188. struct cpu_info *cpu_info;
  189. u64 features, block;
  190. u32 cwg;
  191. int cls;
  192. cpu_info = lookup_processor_type(read_cpuid_id());
  193. if (!cpu_info) {
  194. printk("CPU configuration botched (ID %08x), unable to continue.\n",
  195. read_cpuid_id());
  196. while (1);
  197. }
  198. cpu_name = cpu_info->cpu_name;
  199. printk("CPU: %s [%08x] revision %d\n",
  200. cpu_name, read_cpuid_id(), read_cpuid_id() & 15);
  201. sprintf(init_utsname()->machine, ELF_PLATFORM);
  202. elf_hwcap = 0;
  203. cpuinfo_store_boot_cpu();
  204. /*
  205. * Check for sane CTR_EL0.CWG value.
  206. */
  207. cwg = cache_type_cwg();
  208. cls = cache_line_size();
  209. if (!cwg)
  210. pr_warn("No Cache Writeback Granule information, assuming cache line size %d\n",
  211. cls);
  212. if (L1_CACHE_BYTES < cls)
  213. pr_warn("L1_CACHE_BYTES smaller than the Cache Writeback Granule (%d < %d)\n",
  214. L1_CACHE_BYTES, cls);
  215. /*
  216. * ID_AA64ISAR0_EL1 contains 4-bit wide signed feature blocks.
  217. * The blocks we test below represent incremental functionality
  218. * for non-negative values. Negative values are reserved.
  219. */
  220. features = read_cpuid(ID_AA64ISAR0_EL1);
  221. block = (features >> 4) & 0xf;
  222. if (!(block & 0x8)) {
  223. switch (block) {
  224. default:
  225. case 2:
  226. elf_hwcap |= HWCAP_PMULL;
  227. case 1:
  228. elf_hwcap |= HWCAP_AES;
  229. case 0:
  230. break;
  231. }
  232. }
  233. block = (features >> 8) & 0xf;
  234. if (block && !(block & 0x8))
  235. elf_hwcap |= HWCAP_SHA1;
  236. block = (features >> 12) & 0xf;
  237. if (block && !(block & 0x8))
  238. elf_hwcap |= HWCAP_SHA2;
  239. block = (features >> 16) & 0xf;
  240. if (block && !(block & 0x8))
  241. elf_hwcap |= HWCAP_CRC32;
  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 = (features >> 4) & 0xf;
  249. if (!(block & 0x8)) {
  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. block = (features >> 8) & 0xf;
  261. if (block && !(block & 0x8))
  262. compat_elf_hwcap2 |= COMPAT_HWCAP2_SHA1;
  263. block = (features >> 12) & 0xf;
  264. if (block && !(block & 0x8))
  265. compat_elf_hwcap2 |= COMPAT_HWCAP2_SHA2;
  266. block = (features >> 16) & 0xf;
  267. if (block && !(block & 0x8))
  268. compat_elf_hwcap2 |= COMPAT_HWCAP2_CRC32;
  269. #endif
  270. }
  271. static void __init setup_machine_fdt(phys_addr_t dt_phys)
  272. {
  273. if (!dt_phys || !early_init_dt_scan(phys_to_virt(dt_phys))) {
  274. early_print("\n"
  275. "Error: invalid device tree blob at physical address 0x%p (virtual address 0x%p)\n"
  276. "The dtb must be 8-byte aligned and passed in the first 512MB of memory\n"
  277. "\nPlease check your bootloader.\n",
  278. dt_phys, phys_to_virt(dt_phys));
  279. while (true)
  280. cpu_relax();
  281. }
  282. machine_name = of_flat_dt_get_machine_name();
  283. }
  284. /*
  285. * Limit the memory size that was specified via FDT.
  286. */
  287. static int __init early_mem(char *p)
  288. {
  289. phys_addr_t limit;
  290. if (!p)
  291. return 1;
  292. limit = memparse(p, &p) & PAGE_MASK;
  293. pr_notice("Memory limited to %lldMB\n", limit >> 20);
  294. memblock_enforce_memory_limit(limit);
  295. return 0;
  296. }
  297. early_param("mem", early_mem);
  298. static void __init request_standard_resources(void)
  299. {
  300. struct memblock_region *region;
  301. struct resource *res;
  302. kernel_code.start = virt_to_phys(_text);
  303. kernel_code.end = virt_to_phys(_etext - 1);
  304. kernel_data.start = virt_to_phys(_sdata);
  305. kernel_data.end = virt_to_phys(_end - 1);
  306. for_each_memblock(memory, region) {
  307. res = alloc_bootmem_low(sizeof(*res));
  308. res->name = "System RAM";
  309. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  310. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  311. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  312. request_resource(&iomem_resource, res);
  313. if (kernel_code.start >= res->start &&
  314. kernel_code.end <= res->end)
  315. request_resource(res, &kernel_code);
  316. if (kernel_data.start >= res->start &&
  317. kernel_data.end <= res->end)
  318. request_resource(res, &kernel_data);
  319. }
  320. }
  321. u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID };
  322. void __init setup_arch(char **cmdline_p)
  323. {
  324. setup_processor();
  325. setup_machine_fdt(__fdt_pointer);
  326. init_mm.start_code = (unsigned long) _text;
  327. init_mm.end_code = (unsigned long) _etext;
  328. init_mm.end_data = (unsigned long) _edata;
  329. init_mm.brk = (unsigned long) _end;
  330. *cmdline_p = boot_command_line;
  331. early_ioremap_init();
  332. parse_early_param();
  333. /*
  334. * Unmask asynchronous aborts after bringing up possible earlycon.
  335. * (Report possible System Errors once we can report this occurred)
  336. */
  337. local_async_enable();
  338. efi_init();
  339. arm64_memblock_init();
  340. paging_init();
  341. request_standard_resources();
  342. efi_idmap_init();
  343. unflatten_device_tree();
  344. psci_init();
  345. cpu_logical_map(0) = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
  346. cpu_read_bootcpu_ops();
  347. #ifdef CONFIG_SMP
  348. smp_init_cpus();
  349. smp_build_mpidr_hash();
  350. #endif
  351. #ifdef CONFIG_VT
  352. #if defined(CONFIG_VGA_CONSOLE)
  353. conswitchp = &vga_con;
  354. #elif defined(CONFIG_DUMMY_CONSOLE)
  355. conswitchp = &dummy_con;
  356. #endif
  357. #endif
  358. }
  359. static int __init arm64_device_init(void)
  360. {
  361. of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
  362. return 0;
  363. }
  364. arch_initcall_sync(arm64_device_init);
  365. static int __init topology_init(void)
  366. {
  367. int i;
  368. for_each_possible_cpu(i) {
  369. struct cpu *cpu = &per_cpu(cpu_data.cpu, i);
  370. cpu->hotpluggable = 1;
  371. register_cpu(cpu, i);
  372. }
  373. return 0;
  374. }
  375. subsys_initcall(topology_init);
  376. static const char *hwcap_str[] = {
  377. "fp",
  378. "asimd",
  379. "evtstrm",
  380. "aes",
  381. "pmull",
  382. "sha1",
  383. "sha2",
  384. "crc32",
  385. NULL
  386. };
  387. static int c_show(struct seq_file *m, void *v)
  388. {
  389. int i;
  390. seq_printf(m, "Processor\t: %s rev %d (%s)\n",
  391. cpu_name, read_cpuid_id() & 15, ELF_PLATFORM);
  392. for_each_online_cpu(i) {
  393. /*
  394. * glibc reads /proc/cpuinfo to determine the number of
  395. * online processors, looking for lines beginning with
  396. * "processor". Give glibc what it expects.
  397. */
  398. #ifdef CONFIG_SMP
  399. seq_printf(m, "processor\t: %d\n", i);
  400. #endif
  401. }
  402. /* dump out the processor features */
  403. seq_puts(m, "Features\t: ");
  404. for (i = 0; hwcap_str[i]; i++)
  405. if (elf_hwcap & (1 << i))
  406. seq_printf(m, "%s ", hwcap_str[i]);
  407. seq_printf(m, "\nCPU implementer\t: 0x%02x\n", read_cpuid_id() >> 24);
  408. seq_printf(m, "CPU architecture: AArch64\n");
  409. seq_printf(m, "CPU variant\t: 0x%x\n", (read_cpuid_id() >> 20) & 15);
  410. seq_printf(m, "CPU part\t: 0x%03x\n", (read_cpuid_id() >> 4) & 0xfff);
  411. seq_printf(m, "CPU revision\t: %d\n", read_cpuid_id() & 15);
  412. seq_puts(m, "\n");
  413. seq_printf(m, "Hardware\t: %s\n", machine_name);
  414. return 0;
  415. }
  416. static void *c_start(struct seq_file *m, loff_t *pos)
  417. {
  418. return *pos < 1 ? (void *)1 : NULL;
  419. }
  420. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  421. {
  422. ++*pos;
  423. return NULL;
  424. }
  425. static void c_stop(struct seq_file *m, void *v)
  426. {
  427. }
  428. const struct seq_operations cpuinfo_op = {
  429. .start = c_start,
  430. .next = c_next,
  431. .stop = c_stop,
  432. .show = c_show
  433. };