PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/arch/arm64/kernel/setup.c

https://gitlab.com/deadnem/Singularity
C | 463 lines | 317 code | 73 blank | 73 comment | 19 complexity | abe8daec2e1522ff3c763857f932e472 MD5 | raw file
  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 <linux/dma-mapping.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 int elf_hwcap __read_mostly;
  59. EXPORT_SYMBOL_GPL(elf_hwcap);
  60. unsigned int boot_reason;
  61. EXPORT_SYMBOL(boot_reason);
  62. unsigned int cold_boot;
  63. EXPORT_SYMBOL(cold_boot);
  64. static const char *cpu_name;
  65. static const char *machine_name;
  66. phys_addr_t __fdt_pointer __initdata;
  67. /*
  68. * Standard memory resources
  69. */
  70. static struct resource mem_res[] = {
  71. {
  72. .name = "Kernel code",
  73. .start = 0,
  74. .end = 0,
  75. .flags = IORESOURCE_MEM
  76. },
  77. {
  78. .name = "Kernel data",
  79. .start = 0,
  80. .end = 0,
  81. .flags = IORESOURCE_MEM
  82. }
  83. };
  84. #define kernel_code mem_res[0]
  85. #define kernel_data mem_res[1]
  86. void __init early_print(const char *str, ...)
  87. {
  88. char buf[256];
  89. va_list ap;
  90. va_start(ap, str);
  91. vsnprintf(buf, sizeof(buf), str, ap);
  92. va_end(ap);
  93. printk("%s", buf);
  94. }
  95. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  96. {
  97. return phys_id == cpu_logical_map(cpu);
  98. }
  99. struct mpidr_hash mpidr_hash;
  100. #ifdef CONFIG_SMP
  101. /**
  102. * smp_build_mpidr_hash - Pre-compute shifts required at each affinity
  103. * level in order to build a linear index from an
  104. * MPIDR value. Resulting algorithm is a collision
  105. * free hash carried out through shifting and ORing
  106. */
  107. static void __init smp_build_mpidr_hash(void)
  108. {
  109. u32 i, affinity, fs[4], bits[4], ls;
  110. u64 mask = 0;
  111. /*
  112. * Pre-scan the list of MPIDRS and filter out bits that do
  113. * not contribute to affinity levels, ie they never toggle.
  114. */
  115. for_each_possible_cpu(i)
  116. mask |= (cpu_logical_map(i) ^ cpu_logical_map(0));
  117. pr_debug("mask of set bits %#llx\n", mask);
  118. /*
  119. * Find and stash the last and first bit set at all affinity levels to
  120. * check how many bits are required to represent them.
  121. */
  122. for (i = 0; i < 4; i++) {
  123. affinity = MPIDR_AFFINITY_LEVEL(mask, i);
  124. /*
  125. * Find the MSB bit and LSB bits position
  126. * to determine how many bits are required
  127. * to express the affinity level.
  128. */
  129. ls = fls(affinity);
  130. fs[i] = affinity ? ffs(affinity) - 1 : 0;
  131. bits[i] = ls - fs[i];
  132. }
  133. /*
  134. * An index can be created from the MPIDR_EL1 by isolating the
  135. * significant bits at each affinity level and by shifting
  136. * them in order to compress the 32 bits values space to a
  137. * compressed set of values. This is equivalent to hashing
  138. * the MPIDR_EL1 through shifting and ORing. It is a collision free
  139. * hash though not minimal since some levels might contain a number
  140. * of CPUs that is not an exact power of 2 and their bit
  141. * representation might contain holes, eg MPIDR_EL1[7:0] = {0x2, 0x80}.
  142. */
  143. mpidr_hash.shift_aff[0] = MPIDR_LEVEL_SHIFT(0) + fs[0];
  144. mpidr_hash.shift_aff[1] = MPIDR_LEVEL_SHIFT(1) + fs[1] - bits[0];
  145. mpidr_hash.shift_aff[2] = MPIDR_LEVEL_SHIFT(2) + fs[2] -
  146. (bits[1] + bits[0]);
  147. mpidr_hash.shift_aff[3] = MPIDR_LEVEL_SHIFT(3) +
  148. fs[3] - (bits[2] + bits[1] + bits[0]);
  149. mpidr_hash.mask = mask;
  150. mpidr_hash.bits = bits[3] + bits[2] + bits[1] + bits[0];
  151. pr_debug("MPIDR hash: aff0[%u] aff1[%u] aff2[%u] aff3[%u] mask[%#llx] bits[%u]\n",
  152. mpidr_hash.shift_aff[0],
  153. mpidr_hash.shift_aff[1],
  154. mpidr_hash.shift_aff[2],
  155. mpidr_hash.shift_aff[3],
  156. mpidr_hash.mask,
  157. mpidr_hash.bits);
  158. /*
  159. * 4x is an arbitrary value used to warn on a hash table much bigger
  160. * than expected on most systems.
  161. */
  162. if (mpidr_hash_size() > 4 * num_possible_cpus())
  163. pr_warn("Large number of MPIDR hash buckets detected\n");
  164. __flush_dcache_area(&mpidr_hash, sizeof(struct mpidr_hash));
  165. }
  166. #endif
  167. static void __init setup_processor(void)
  168. {
  169. struct cpu_info *cpu_info;
  170. /*
  171. * locate processor in the list of supported processor
  172. * types. The linker builds this table for us from the
  173. * entries in arch/arm/mm/proc.S
  174. */
  175. cpu_info = lookup_processor_type(read_cpuid_id());
  176. if (!cpu_info) {
  177. printk("CPU configuration botched (ID %08x), unable to continue.\n",
  178. read_cpuid_id());
  179. while (1);
  180. }
  181. cpu_name = cpu_info->cpu_name;
  182. printk("CPU: %s [%08x] revision %d\n",
  183. cpu_name, read_cpuid_id(), read_cpuid_id() & 15);
  184. sprintf(init_utsname()->machine, "aarch64");
  185. elf_hwcap = 0;
  186. }
  187. static void __init setup_machine_fdt(phys_addr_t dt_phys)
  188. {
  189. struct boot_param_header *devtree;
  190. unsigned long dt_root;
  191. /* Check we have a non-NULL DT pointer */
  192. if (!dt_phys) {
  193. early_print("\n"
  194. "Error: NULL or invalid device tree blob\n"
  195. "The dtb must be 8-byte aligned and passed in the first 512MB of memory\n"
  196. "\nPlease check your bootloader.\n");
  197. while (true)
  198. cpu_relax();
  199. }
  200. devtree = phys_to_virt(dt_phys);
  201. /* Check device tree validity */
  202. if (be32_to_cpu(devtree->magic) != OF_DT_HEADER) {
  203. early_print("\n"
  204. "Error: invalid device tree blob at physical address 0x%p (virtual address 0x%p)\n"
  205. "Expected 0x%x, found 0x%x\n"
  206. "\nPlease check your bootloader.\n",
  207. dt_phys, devtree, OF_DT_HEADER,
  208. be32_to_cpu(devtree->magic));
  209. while (true)
  210. cpu_relax();
  211. }
  212. initial_boot_params = devtree;
  213. dt_root = of_get_flat_dt_root();
  214. machine_name = of_get_flat_dt_prop(dt_root, "model", NULL);
  215. if (!machine_name)
  216. machine_name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
  217. if (!machine_name)
  218. machine_name = "<unknown>";
  219. pr_info("Machine: %s\n", machine_name);
  220. /* Retrieve various information from the /chosen node */
  221. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  222. /* Initialize {size,address}-cells info */
  223. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  224. /* Setup memory, calling early_init_dt_add_memory_arch */
  225. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  226. }
  227. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  228. {
  229. base &= PAGE_MASK;
  230. size &= PAGE_MASK;
  231. if (base + size < PHYS_OFFSET) {
  232. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  233. base, base + size);
  234. return;
  235. }
  236. if (base < PHYS_OFFSET) {
  237. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  238. base, PHYS_OFFSET);
  239. size -= PHYS_OFFSET - base;
  240. base = PHYS_OFFSET;
  241. }
  242. memblock_add(base, size);
  243. }
  244. void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
  245. {
  246. return __va(memblock_alloc(size, align));
  247. }
  248. /*
  249. * Limit the memory size that was specified via FDT.
  250. */
  251. static int __init early_mem(char *p)
  252. {
  253. phys_addr_t limit;
  254. if (!p)
  255. return 1;
  256. limit = memparse(p, &p) & PAGE_MASK;
  257. pr_notice("Memory limited to %lldMB\n", limit >> 20);
  258. memblock_enforce_memory_limit(limit);
  259. return 0;
  260. }
  261. early_param("mem", early_mem);
  262. static void __init request_standard_resources(void)
  263. {
  264. struct memblock_region *region;
  265. struct resource *res;
  266. kernel_code.start = virt_to_phys(_text);
  267. kernel_code.end = virt_to_phys(_etext - 1);
  268. kernel_data.start = virt_to_phys(_sdata);
  269. kernel_data.end = virt_to_phys(_end - 1);
  270. for_each_memblock(memory, region) {
  271. res = alloc_bootmem_low(sizeof(*res));
  272. res->name = "System RAM";
  273. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  274. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  275. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  276. request_resource(&iomem_resource, res);
  277. if (kernel_code.start >= res->start &&
  278. kernel_code.end <= res->end)
  279. request_resource(res, &kernel_code);
  280. if (kernel_data.start >= res->start &&
  281. kernel_data.end <= res->end)
  282. request_resource(res, &kernel_data);
  283. }
  284. }
  285. u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID };
  286. void __init setup_arch(char **cmdline_p)
  287. {
  288. setup_processor();
  289. setup_machine_fdt(__fdt_pointer);
  290. init_mm.start_code = (unsigned long) _text;
  291. init_mm.end_code = (unsigned long) _etext;
  292. init_mm.end_data = (unsigned long) _edata;
  293. init_mm.brk = (unsigned long) _end;
  294. *cmdline_p = boot_command_line;
  295. parse_early_param();
  296. arm64_memblock_init();
  297. paging_init();
  298. request_standard_resources();
  299. unflatten_device_tree();
  300. psci_init();
  301. cpu_logical_map(0) = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
  302. cpu_read_bootcpu_ops();
  303. #ifdef CONFIG_SMP
  304. smp_init_cpus();
  305. smp_build_mpidr_hash();
  306. #endif
  307. #ifdef CONFIG_VT
  308. #if defined(CONFIG_VGA_CONSOLE)
  309. conswitchp = &vga_con;
  310. #elif defined(CONFIG_DUMMY_CONSOLE)
  311. conswitchp = &dummy_con;
  312. #endif
  313. #endif
  314. }
  315. static int __init arm64_device_init(void)
  316. {
  317. of_clk_init(NULL);
  318. of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
  319. return 0;
  320. }
  321. arch_initcall(arm64_device_init);
  322. static DEFINE_PER_CPU(struct cpu, cpu_data);
  323. static int __init topology_init(void)
  324. {
  325. int i;
  326. for_each_possible_cpu(i) {
  327. struct cpu *cpu = &per_cpu(cpu_data, i);
  328. cpu->hotpluggable = 1;
  329. register_cpu(cpu, i);
  330. }
  331. return 0;
  332. }
  333. subsys_initcall(topology_init);
  334. static const char *hwcap_str[] = {
  335. "fp",
  336. "asimd",
  337. NULL
  338. };
  339. static int c_show(struct seq_file *m, void *v)
  340. {
  341. int i;
  342. seq_printf(m, "Processor\t: %s rev %d (%s)\n",
  343. cpu_name, read_cpuid_id() & 15, ELF_PLATFORM);
  344. for_each_online_cpu(i) {
  345. /*
  346. * glibc reads /proc/cpuinfo to determine the number of
  347. * online processors, looking for lines beginning with
  348. * "processor". Give glibc what it expects.
  349. */
  350. #ifdef CONFIG_SMP
  351. seq_printf(m, "processor\t: %d\n", i);
  352. #endif
  353. }
  354. /* dump out the processor features */
  355. seq_puts(m, "Features\t: ");
  356. for (i = 0; hwcap_str[i]; i++)
  357. if (elf_hwcap & (1 << i))
  358. seq_printf(m, "%s ", hwcap_str[i]);
  359. seq_printf(m, "\nCPU implementer\t: 0x%02x\n", read_cpuid_id() >> 24);
  360. seq_printf(m, "CPU architecture: AArch64\n");
  361. seq_printf(m, "CPU variant\t: 0x%x\n", (read_cpuid_id() >> 20) & 15);
  362. seq_printf(m, "CPU part\t: 0x%03x\n", (read_cpuid_id() >> 4) & 0xfff);
  363. seq_printf(m, "CPU revision\t: %d\n", read_cpuid_id() & 15);
  364. seq_puts(m, "\n");
  365. seq_printf(m, "Hardware\t: %s\n", machine_name);
  366. return 0;
  367. }
  368. static void *c_start(struct seq_file *m, loff_t *pos)
  369. {
  370. return *pos < 1 ? (void *)1 : NULL;
  371. }
  372. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  373. {
  374. ++*pos;
  375. return NULL;
  376. }
  377. static void c_stop(struct seq_file *m, void *v)
  378. {
  379. }
  380. const struct seq_operations cpuinfo_op = {
  381. .start = c_start,
  382. .next = c_next,
  383. .stop = c_stop,
  384. .show = c_show
  385. };
  386. void arch_setup_pdev_archdata(struct platform_device *pdev)
  387. {
  388. pdev->archdata.dma_mask = DMA_BIT_MASK(32);
  389. pdev->dev.dma_mask = &pdev->archdata.dma_mask;
  390. }