setup.c 12 KB

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