setup.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/cputype.h>
  44. #include <asm/elf.h>
  45. #include <asm/cputable.h>
  46. #include <asm/sections.h>
  47. #include <asm/setup.h>
  48. #include <asm/smp_plat.h>
  49. #include <asm/cacheflush.h>
  50. #include <asm/tlbflush.h>
  51. #include <asm/traps.h>
  52. #include <asm/memblock.h>
  53. #include <asm/psci.h>
  54. unsigned int processor_id;
  55. EXPORT_SYMBOL(processor_id);
  56. unsigned long elf_hwcap __read_mostly;
  57. EXPORT_SYMBOL_GPL(elf_hwcap);
  58. #ifdef CONFIG_COMPAT
  59. #define COMPAT_ELF_HWCAP_DEFAULT \
  60. (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
  61. COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
  62. COMPAT_HWCAP_TLS|COMPAT_HWCAP_VFP|\
  63. COMPAT_HWCAP_VFPv3|COMPAT_HWCAP_VFPv4|\
  64. COMPAT_HWCAP_NEON|COMPAT_HWCAP_IDIV)
  65. unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
  66. #endif
  67. static const char *cpu_name;
  68. static const char *machine_name;
  69. phys_addr_t __fdt_pointer __initdata;
  70. /*
  71. * Standard memory resources
  72. */
  73. static struct resource mem_res[] = {
  74. {
  75. .name = "Kernel code",
  76. .start = 0,
  77. .end = 0,
  78. .flags = IORESOURCE_MEM
  79. },
  80. {
  81. .name = "Kernel data",
  82. .start = 0,
  83. .end = 0,
  84. .flags = IORESOURCE_MEM
  85. }
  86. };
  87. #define kernel_code mem_res[0]
  88. #define kernel_data mem_res[1]
  89. void __init early_print(const char *str, ...)
  90. {
  91. char buf[256];
  92. va_list ap;
  93. va_start(ap, str);
  94. vsnprintf(buf, sizeof(buf), str, ap);
  95. va_end(ap);
  96. printk("%s", buf);
  97. }
  98. static void __init setup_processor(void)
  99. {
  100. struct cpu_info *cpu_info;
  101. /*
  102. * locate processor in the list of supported processor
  103. * types. The linker builds this table for us from the
  104. * entries in arch/arm/mm/proc.S
  105. */
  106. cpu_info = lookup_processor_type(read_cpuid_id());
  107. if (!cpu_info) {
  108. printk("CPU configuration botched (ID %08x), unable to continue.\n",
  109. read_cpuid_id());
  110. while (1);
  111. }
  112. cpu_name = cpu_info->cpu_name;
  113. printk("CPU: %s [%08x] revision %d\n",
  114. cpu_name, read_cpuid_id(), read_cpuid_id() & 15);
  115. sprintf(init_utsname()->machine, "aarch64");
  116. elf_hwcap = 0;
  117. }
  118. static void __init setup_machine_fdt(phys_addr_t dt_phys)
  119. {
  120. struct boot_param_header *devtree;
  121. unsigned long dt_root;
  122. /* Check we have a non-NULL DT pointer */
  123. if (!dt_phys) {
  124. early_print("\n"
  125. "Error: NULL or invalid device tree blob\n"
  126. "The dtb must be 8-byte aligned and passed in the first 512MB of memory\n"
  127. "\nPlease check your bootloader.\n");
  128. while (true)
  129. cpu_relax();
  130. }
  131. devtree = phys_to_virt(dt_phys);
  132. /* Check device tree validity */
  133. if (be32_to_cpu(devtree->magic) != OF_DT_HEADER) {
  134. early_print("\n"
  135. "Error: invalid device tree blob at physical address 0x%p (virtual address 0x%p)\n"
  136. "Expected 0x%x, found 0x%x\n"
  137. "\nPlease check your bootloader.\n",
  138. dt_phys, devtree, OF_DT_HEADER,
  139. be32_to_cpu(devtree->magic));
  140. while (true)
  141. cpu_relax();
  142. }
  143. initial_boot_params = devtree;
  144. dt_root = of_get_flat_dt_root();
  145. machine_name = of_get_flat_dt_prop(dt_root, "model", NULL);
  146. if (!machine_name)
  147. machine_name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
  148. if (!machine_name)
  149. machine_name = "<unknown>";
  150. pr_info("Machine: %s\n", machine_name);
  151. /* Retrieve various information from the /chosen node */
  152. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  153. /* Initialize {size,address}-cells info */
  154. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  155. /* Setup memory, calling early_init_dt_add_memory_arch */
  156. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  157. }
  158. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  159. {
  160. base &= PAGE_MASK;
  161. size &= PAGE_MASK;
  162. if (base + size < PHYS_OFFSET) {
  163. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  164. base, base + size);
  165. return;
  166. }
  167. if (base < PHYS_OFFSET) {
  168. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  169. base, PHYS_OFFSET);
  170. size -= PHYS_OFFSET - base;
  171. base = PHYS_OFFSET;
  172. }
  173. memblock_add(base, size);
  174. }
  175. /*
  176. * Limit the memory size that was specified via FDT.
  177. */
  178. static int __init early_mem(char *p)
  179. {
  180. phys_addr_t limit;
  181. if (!p)
  182. return 1;
  183. limit = memparse(p, &p) & PAGE_MASK;
  184. pr_notice("Memory limited to %lldMB\n", limit >> 20);
  185. memblock_enforce_memory_limit(limit);
  186. return 0;
  187. }
  188. early_param("mem", early_mem);
  189. static void __init request_standard_resources(void)
  190. {
  191. struct memblock_region *region;
  192. struct resource *res;
  193. kernel_code.start = virt_to_phys(_text);
  194. kernel_code.end = virt_to_phys(_etext - 1);
  195. kernel_data.start = virt_to_phys(_sdata);
  196. kernel_data.end = virt_to_phys(_end - 1);
  197. for_each_memblock(memory, region) {
  198. res = alloc_bootmem_low(sizeof(*res));
  199. res->name = "System RAM";
  200. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  201. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  202. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  203. request_resource(&iomem_resource, res);
  204. if (kernel_code.start >= res->start &&
  205. kernel_code.end <= res->end)
  206. request_resource(res, &kernel_code);
  207. if (kernel_data.start >= res->start &&
  208. kernel_data.end <= res->end)
  209. request_resource(res, &kernel_data);
  210. }
  211. }
  212. u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID };
  213. void __init setup_arch(char **cmdline_p)
  214. {
  215. setup_processor();
  216. setup_machine_fdt(__fdt_pointer);
  217. init_mm.start_code = (unsigned long) _text;
  218. init_mm.end_code = (unsigned long) _etext;
  219. init_mm.end_data = (unsigned long) _edata;
  220. init_mm.brk = (unsigned long) _end;
  221. *cmdline_p = boot_command_line;
  222. parse_early_param();
  223. arm64_memblock_init();
  224. paging_init();
  225. request_standard_resources();
  226. unflatten_device_tree();
  227. psci_init();
  228. cpu_logical_map(0) = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
  229. #ifdef CONFIG_SMP
  230. smp_init_cpus();
  231. #endif
  232. #ifdef CONFIG_VT
  233. #if defined(CONFIG_VGA_CONSOLE)
  234. conswitchp = &vga_con;
  235. #elif defined(CONFIG_DUMMY_CONSOLE)
  236. conswitchp = &dummy_con;
  237. #endif
  238. #endif
  239. }
  240. static int __init arm64_device_init(void)
  241. {
  242. of_clk_init(NULL);
  243. of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
  244. return 0;
  245. }
  246. arch_initcall(arm64_device_init);
  247. static DEFINE_PER_CPU(struct cpu, cpu_data);
  248. static int __init topology_init(void)
  249. {
  250. int i;
  251. for_each_possible_cpu(i) {
  252. struct cpu *cpu = &per_cpu(cpu_data, i);
  253. cpu->hotpluggable = 1;
  254. register_cpu(cpu, i);
  255. }
  256. return 0;
  257. }
  258. subsys_initcall(topology_init);
  259. static const char *hwcap_str[] = {
  260. "fp",
  261. "asimd",
  262. "evtstrm",
  263. NULL
  264. };
  265. static int c_show(struct seq_file *m, void *v)
  266. {
  267. int i;
  268. seq_printf(m, "Processor\t: %s rev %d (%s)\n",
  269. cpu_name, read_cpuid_id() & 15, ELF_PLATFORM);
  270. for_each_online_cpu(i) {
  271. /*
  272. * glibc reads /proc/cpuinfo to determine the number of
  273. * online processors, looking for lines beginning with
  274. * "processor". Give glibc what it expects.
  275. */
  276. #ifdef CONFIG_SMP
  277. seq_printf(m, "processor\t: %d\n", i);
  278. #endif
  279. }
  280. /* dump out the processor features */
  281. seq_puts(m, "Features\t: ");
  282. for (i = 0; hwcap_str[i]; i++)
  283. if (elf_hwcap & (1 << i))
  284. seq_printf(m, "%s ", hwcap_str[i]);
  285. seq_printf(m, "\nCPU implementer\t: 0x%02x\n", read_cpuid_id() >> 24);
  286. seq_printf(m, "CPU architecture: AArch64\n");
  287. seq_printf(m, "CPU variant\t: 0x%x\n", (read_cpuid_id() >> 20) & 15);
  288. seq_printf(m, "CPU part\t: 0x%03x\n", (read_cpuid_id() >> 4) & 0xfff);
  289. seq_printf(m, "CPU revision\t: %d\n", read_cpuid_id() & 15);
  290. seq_puts(m, "\n");
  291. seq_printf(m, "Hardware\t: %s\n", machine_name);
  292. return 0;
  293. }
  294. static void *c_start(struct seq_file *m, loff_t *pos)
  295. {
  296. return *pos < 1 ? (void *)1 : NULL;
  297. }
  298. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  299. {
  300. ++*pos;
  301. return NULL;
  302. }
  303. static void c_stop(struct seq_file *m, void *v)
  304. {
  305. }
  306. const struct seq_operations cpuinfo_op = {
  307. .start = c_start,
  308. .next = c_next,
  309. .stop = c_stop,
  310. .show = c_show
  311. };