setup.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /*
  2. * linux/arch/arm/kernel/setup.c
  3. *
  4. * Copyright (C) 1995-2001 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/efi.h>
  11. #include <linux/export.h>
  12. #include <linux/kernel.h>
  13. #include <linux/stddef.h>
  14. #include <linux/ioport.h>
  15. #include <linux/delay.h>
  16. #include <linux/utsname.h>
  17. #include <linux/initrd.h>
  18. #include <linux/console.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/screen_info.h>
  22. #include <linux/of_iommu.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/init.h>
  25. #include <linux/kexec.h>
  26. #include <linux/of_fdt.h>
  27. #include <linux/cpu.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/smp.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/memblock.h>
  32. #include <linux/bug.h>
  33. #include <linux/compiler.h>
  34. #include <linux/sort.h>
  35. #include <linux/psci.h>
  36. #include <asm/unified.h>
  37. #include <asm/cp15.h>
  38. #include <asm/cpu.h>
  39. #include <asm/cputype.h>
  40. #include <asm/efi.h>
  41. #include <asm/elf.h>
  42. #include <asm/early_ioremap.h>
  43. #include <asm/fixmap.h>
  44. #include <asm/procinfo.h>
  45. #include <asm/psci.h>
  46. #include <asm/sections.h>
  47. #include <asm/setup.h>
  48. #include <asm/smp_plat.h>
  49. #include <asm/mach-types.h>
  50. #include <asm/cacheflush.h>
  51. #include <asm/cachetype.h>
  52. #include <asm/tlbflush.h>
  53. #include <asm/xen/hypervisor.h>
  54. #include <asm/prom.h>
  55. #include <asm/mach/arch.h>
  56. #include <asm/mach/irq.h>
  57. #include <asm/mach/time.h>
  58. #include <asm/system_info.h>
  59. #include <asm/system_misc.h>
  60. #include <asm/traps.h>
  61. #include <asm/unwind.h>
  62. #include <asm/memblock.h>
  63. #include <asm/virt.h>
  64. #include "atags.h"
  65. #if defined(CONFIG_FPE_NWFPE) || defined(CONFIG_FPE_FASTFPE)
  66. char fpe_type[8];
  67. static int __init fpe_setup(char *line)
  68. {
  69. memcpy(fpe_type, line, 8);
  70. return 1;
  71. }
  72. __setup("fpe=", fpe_setup);
  73. #endif
  74. extern void init_default_cache_policy(unsigned long);
  75. extern void paging_init(const struct machine_desc *desc);
  76. extern void early_paging_init(const struct machine_desc *);
  77. extern void sanity_check_meminfo(void);
  78. extern enum reboot_mode reboot_mode;
  79. extern void setup_dma_zone(const struct machine_desc *desc);
  80. unsigned int processor_id;
  81. EXPORT_SYMBOL(processor_id);
  82. unsigned int __machine_arch_type __read_mostly;
  83. EXPORT_SYMBOL(__machine_arch_type);
  84. unsigned int cacheid __read_mostly;
  85. EXPORT_SYMBOL(cacheid);
  86. unsigned int __atags_pointer __initdata;
  87. unsigned int system_rev;
  88. EXPORT_SYMBOL(system_rev);
  89. const char *system_serial;
  90. EXPORT_SYMBOL(system_serial);
  91. unsigned int system_serial_low;
  92. EXPORT_SYMBOL(system_serial_low);
  93. unsigned int system_serial_high;
  94. EXPORT_SYMBOL(system_serial_high);
  95. unsigned int elf_hwcap __read_mostly;
  96. EXPORT_SYMBOL(elf_hwcap);
  97. unsigned int elf_hwcap2 __read_mostly;
  98. EXPORT_SYMBOL(elf_hwcap2);
  99. #ifdef MULTI_CPU
  100. struct processor processor __read_mostly;
  101. #endif
  102. #ifdef MULTI_TLB
  103. struct cpu_tlb_fns cpu_tlb __read_mostly;
  104. #endif
  105. #ifdef MULTI_USER
  106. struct cpu_user_fns cpu_user __read_mostly;
  107. #endif
  108. #ifdef MULTI_CACHE
  109. struct cpu_cache_fns cpu_cache __read_mostly;
  110. #endif
  111. #ifdef CONFIG_OUTER_CACHE
  112. struct outer_cache_fns outer_cache __read_mostly;
  113. EXPORT_SYMBOL(outer_cache);
  114. #endif
  115. /*
  116. * Cached cpu_architecture() result for use by assembler code.
  117. * C code should use the cpu_architecture() function instead of accessing this
  118. * variable directly.
  119. */
  120. int __cpu_architecture __read_mostly = CPU_ARCH_UNKNOWN;
  121. struct stack {
  122. u32 irq[3];
  123. u32 abt[3];
  124. u32 und[3];
  125. u32 fiq[3];
  126. } ____cacheline_aligned;
  127. #ifndef CONFIG_CPU_V7M
  128. static struct stack stacks[NR_CPUS];
  129. #endif
  130. char elf_platform[ELF_PLATFORM_SIZE];
  131. EXPORT_SYMBOL(elf_platform);
  132. static const char *cpu_name;
  133. static const char *machine_name;
  134. static char __initdata cmd_line[COMMAND_LINE_SIZE];
  135. const struct machine_desc *machine_desc __initdata;
  136. static union { char c[4]; unsigned long l; } endian_test __initdata = { { 'l', '?', '?', 'b' } };
  137. #define ENDIANNESS ((char)endian_test.l)
  138. DEFINE_PER_CPU(struct cpuinfo_arm, cpu_data);
  139. /*
  140. * Standard memory resources
  141. */
  142. static struct resource mem_res[] = {
  143. {
  144. .name = "Video RAM",
  145. .start = 0,
  146. .end = 0,
  147. .flags = IORESOURCE_MEM
  148. },
  149. {
  150. .name = "Kernel code",
  151. .start = 0,
  152. .end = 0,
  153. .flags = IORESOURCE_SYSTEM_RAM
  154. },
  155. {
  156. .name = "Kernel data",
  157. .start = 0,
  158. .end = 0,
  159. .flags = IORESOURCE_SYSTEM_RAM
  160. }
  161. };
  162. #define video_ram mem_res[0]
  163. #define kernel_code mem_res[1]
  164. #define kernel_data mem_res[2]
  165. static struct resource io_res[] = {
  166. {
  167. .name = "reserved",
  168. .start = 0x3bc,
  169. .end = 0x3be,
  170. .flags = IORESOURCE_IO | IORESOURCE_BUSY
  171. },
  172. {
  173. .name = "reserved",
  174. .start = 0x378,
  175. .end = 0x37f,
  176. .flags = IORESOURCE_IO | IORESOURCE_BUSY
  177. },
  178. {
  179. .name = "reserved",
  180. .start = 0x278,
  181. .end = 0x27f,
  182. .flags = IORESOURCE_IO | IORESOURCE_BUSY
  183. }
  184. };
  185. #define lp0 io_res[0]
  186. #define lp1 io_res[1]
  187. #define lp2 io_res[2]
  188. static const char *proc_arch[] = {
  189. "undefined/unknown",
  190. "3",
  191. "4",
  192. "4T",
  193. "5",
  194. "5T",
  195. "5TE",
  196. "5TEJ",
  197. "6TEJ",
  198. "7",
  199. "7M",
  200. "?(12)",
  201. "?(13)",
  202. "?(14)",
  203. "?(15)",
  204. "?(16)",
  205. "?(17)",
  206. };
  207. #ifdef CONFIG_CPU_V7M
  208. static int __get_cpu_architecture(void)
  209. {
  210. return CPU_ARCH_ARMv7M;
  211. }
  212. #else
  213. static int __get_cpu_architecture(void)
  214. {
  215. int cpu_arch;
  216. if ((read_cpuid_id() & 0x0008f000) == 0) {
  217. cpu_arch = CPU_ARCH_UNKNOWN;
  218. } else if ((read_cpuid_id() & 0x0008f000) == 0x00007000) {
  219. cpu_arch = (read_cpuid_id() & (1 << 23)) ? CPU_ARCH_ARMv4T : CPU_ARCH_ARMv3;
  220. } else if ((read_cpuid_id() & 0x00080000) == 0x00000000) {
  221. cpu_arch = (read_cpuid_id() >> 16) & 7;
  222. if (cpu_arch)
  223. cpu_arch += CPU_ARCH_ARMv3;
  224. } else if ((read_cpuid_id() & 0x000f0000) == 0x000f0000) {
  225. /* Revised CPUID format. Read the Memory Model Feature
  226. * Register 0 and check for VMSAv7 or PMSAv7 */
  227. unsigned int mmfr0 = read_cpuid_ext(CPUID_EXT_MMFR0);
  228. if ((mmfr0 & 0x0000000f) >= 0x00000003 ||
  229. (mmfr0 & 0x000000f0) >= 0x00000030)
  230. cpu_arch = CPU_ARCH_ARMv7;
  231. else if ((mmfr0 & 0x0000000f) == 0x00000002 ||
  232. (mmfr0 & 0x000000f0) == 0x00000020)
  233. cpu_arch = CPU_ARCH_ARMv6;
  234. else
  235. cpu_arch = CPU_ARCH_UNKNOWN;
  236. } else
  237. cpu_arch = CPU_ARCH_UNKNOWN;
  238. return cpu_arch;
  239. }
  240. #endif
  241. int __pure cpu_architecture(void)
  242. {
  243. BUG_ON(__cpu_architecture == CPU_ARCH_UNKNOWN);
  244. return __cpu_architecture;
  245. }
  246. static int cpu_has_aliasing_icache(unsigned int arch)
  247. {
  248. int aliasing_icache;
  249. unsigned int id_reg, num_sets, line_size;
  250. /* PIPT caches never alias. */
  251. if (icache_is_pipt())
  252. return 0;
  253. /* arch specifies the register format */
  254. switch (arch) {
  255. case CPU_ARCH_ARMv7:
  256. asm("mcr p15, 2, %0, c0, c0, 0 @ set CSSELR"
  257. : /* No output operands */
  258. : "r" (1));
  259. isb();
  260. asm("mrc p15, 1, %0, c0, c0, 0 @ read CCSIDR"
  261. : "=r" (id_reg));
  262. line_size = 4 << ((id_reg & 0x7) + 2);
  263. num_sets = ((id_reg >> 13) & 0x7fff) + 1;
  264. aliasing_icache = (line_size * num_sets) > PAGE_SIZE;
  265. break;
  266. case CPU_ARCH_ARMv6:
  267. aliasing_icache = read_cpuid_cachetype() & (1 << 11);
  268. break;
  269. default:
  270. /* I-cache aliases will be handled by D-cache aliasing code */
  271. aliasing_icache = 0;
  272. }
  273. return aliasing_icache;
  274. }
  275. static void __init cacheid_init(void)
  276. {
  277. unsigned int arch = cpu_architecture();
  278. if (arch == CPU_ARCH_ARMv7M) {
  279. cacheid = 0;
  280. } else if (arch >= CPU_ARCH_ARMv6) {
  281. unsigned int cachetype = read_cpuid_cachetype();
  282. if ((cachetype & (7 << 29)) == 4 << 29) {
  283. /* ARMv7 register format */
  284. arch = CPU_ARCH_ARMv7;
  285. cacheid = CACHEID_VIPT_NONALIASING;
  286. switch (cachetype & (3 << 14)) {
  287. case (1 << 14):
  288. cacheid |= CACHEID_ASID_TAGGED;
  289. break;
  290. case (3 << 14):
  291. cacheid |= CACHEID_PIPT;
  292. break;
  293. }
  294. } else {
  295. arch = CPU_ARCH_ARMv6;
  296. if (cachetype & (1 << 23))
  297. cacheid = CACHEID_VIPT_ALIASING;
  298. else
  299. cacheid = CACHEID_VIPT_NONALIASING;
  300. }
  301. if (cpu_has_aliasing_icache(arch))
  302. cacheid |= CACHEID_VIPT_I_ALIASING;
  303. } else {
  304. cacheid = CACHEID_VIVT;
  305. }
  306. pr_info("CPU: %s data cache, %s instruction cache\n",
  307. cache_is_vivt() ? "VIVT" :
  308. cache_is_vipt_aliasing() ? "VIPT aliasing" :
  309. cache_is_vipt_nonaliasing() ? "PIPT / VIPT nonaliasing" : "unknown",
  310. cache_is_vivt() ? "VIVT" :
  311. icache_is_vivt_asid_tagged() ? "VIVT ASID tagged" :
  312. icache_is_vipt_aliasing() ? "VIPT aliasing" :
  313. icache_is_pipt() ? "PIPT" :
  314. cache_is_vipt_nonaliasing() ? "VIPT nonaliasing" : "unknown");
  315. }
  316. /*
  317. * These functions re-use the assembly code in head.S, which
  318. * already provide the required functionality.
  319. */
  320. extern struct proc_info_list *lookup_processor_type(unsigned int);
  321. void __init early_print(const char *str, ...)
  322. {
  323. extern void printascii(const char *);
  324. char buf[256];
  325. va_list ap;
  326. va_start(ap, str);
  327. vsnprintf(buf, sizeof(buf), str, ap);
  328. va_end(ap);
  329. #ifdef CONFIG_DEBUG_LL
  330. printascii(buf);
  331. #endif
  332. printk("%s", buf);
  333. }
  334. #ifdef CONFIG_ARM_PATCH_IDIV
  335. static inline u32 __attribute_const__ sdiv_instruction(void)
  336. {
  337. if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
  338. /* "sdiv r0, r0, r1" */
  339. u32 insn = __opcode_thumb32_compose(0xfb90, 0xf0f1);
  340. return __opcode_to_mem_thumb32(insn);
  341. }
  342. /* "sdiv r0, r0, r1" */
  343. return __opcode_to_mem_arm(0xe710f110);
  344. }
  345. static inline u32 __attribute_const__ udiv_instruction(void)
  346. {
  347. if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
  348. /* "udiv r0, r0, r1" */
  349. u32 insn = __opcode_thumb32_compose(0xfbb0, 0xf0f1);
  350. return __opcode_to_mem_thumb32(insn);
  351. }
  352. /* "udiv r0, r0, r1" */
  353. return __opcode_to_mem_arm(0xe730f110);
  354. }
  355. static inline u32 __attribute_const__ bx_lr_instruction(void)
  356. {
  357. if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
  358. /* "bx lr; nop" */
  359. u32 insn = __opcode_thumb32_compose(0x4770, 0x46c0);
  360. return __opcode_to_mem_thumb32(insn);
  361. }
  362. /* "bx lr" */
  363. return __opcode_to_mem_arm(0xe12fff1e);
  364. }
  365. static void __init patch_aeabi_idiv(void)
  366. {
  367. extern void __aeabi_uidiv(void);
  368. extern void __aeabi_idiv(void);
  369. uintptr_t fn_addr;
  370. unsigned int mask;
  371. mask = IS_ENABLED(CONFIG_THUMB2_KERNEL) ? HWCAP_IDIVT : HWCAP_IDIVA;
  372. if (!(elf_hwcap & mask))
  373. return;
  374. pr_info("CPU: div instructions available: patching division code\n");
  375. fn_addr = ((uintptr_t)&__aeabi_uidiv) & ~1;
  376. asm ("" : "+g" (fn_addr));
  377. ((u32 *)fn_addr)[0] = udiv_instruction();
  378. ((u32 *)fn_addr)[1] = bx_lr_instruction();
  379. flush_icache_range(fn_addr, fn_addr + 8);
  380. fn_addr = ((uintptr_t)&__aeabi_idiv) & ~1;
  381. asm ("" : "+g" (fn_addr));
  382. ((u32 *)fn_addr)[0] = sdiv_instruction();
  383. ((u32 *)fn_addr)[1] = bx_lr_instruction();
  384. flush_icache_range(fn_addr, fn_addr + 8);
  385. }
  386. #else
  387. static inline void patch_aeabi_idiv(void) { }
  388. #endif
  389. static void __init cpuid_init_hwcaps(void)
  390. {
  391. int block;
  392. u32 isar5;
  393. if (cpu_architecture() < CPU_ARCH_ARMv7)
  394. return;
  395. block = cpuid_feature_extract(CPUID_EXT_ISAR0, 24);
  396. if (block >= 2)
  397. elf_hwcap |= HWCAP_IDIVA;
  398. if (block >= 1)
  399. elf_hwcap |= HWCAP_IDIVT;
  400. /* LPAE implies atomic ldrd/strd instructions */
  401. block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);
  402. if (block >= 5)
  403. elf_hwcap |= HWCAP_LPAE;
  404. /* check for supported v8 Crypto instructions */
  405. isar5 = read_cpuid_ext(CPUID_EXT_ISAR5);
  406. block = cpuid_feature_extract_field(isar5, 4);
  407. if (block >= 2)
  408. elf_hwcap2 |= HWCAP2_PMULL;
  409. if (block >= 1)
  410. elf_hwcap2 |= HWCAP2_AES;
  411. block = cpuid_feature_extract_field(isar5, 8);
  412. if (block >= 1)
  413. elf_hwcap2 |= HWCAP2_SHA1;
  414. block = cpuid_feature_extract_field(isar5, 12);
  415. if (block >= 1)
  416. elf_hwcap2 |= HWCAP2_SHA2;
  417. block = cpuid_feature_extract_field(isar5, 16);
  418. if (block >= 1)
  419. elf_hwcap2 |= HWCAP2_CRC32;
  420. }
  421. static void __init elf_hwcap_fixup(void)
  422. {
  423. unsigned id = read_cpuid_id();
  424. /*
  425. * HWCAP_TLS is available only on 1136 r1p0 and later,
  426. * see also kuser_get_tls_init.
  427. */
  428. if (read_cpuid_part() == ARM_CPU_PART_ARM1136 &&
  429. ((id >> 20) & 3) == 0) {
  430. elf_hwcap &= ~HWCAP_TLS;
  431. return;
  432. }
  433. /* Verify if CPUID scheme is implemented */
  434. if ((id & 0x000f0000) != 0x000f0000)
  435. return;
  436. /*
  437. * If the CPU supports LDREX/STREX and LDREXB/STREXB,
  438. * avoid advertising SWP; it may not be atomic with
  439. * multiprocessing cores.
  440. */
  441. if (cpuid_feature_extract(CPUID_EXT_ISAR3, 12) > 1 ||
  442. (cpuid_feature_extract(CPUID_EXT_ISAR3, 12) == 1 &&
  443. cpuid_feature_extract(CPUID_EXT_ISAR3, 20) >= 3))
  444. elf_hwcap &= ~HWCAP_SWP;
  445. }
  446. /*
  447. * cpu_init - initialise one CPU.
  448. *
  449. * cpu_init sets up the per-CPU stacks.
  450. */
  451. void notrace cpu_init(void)
  452. {
  453. #ifndef CONFIG_CPU_V7M
  454. unsigned int cpu = smp_processor_id();
  455. struct stack *stk = &stacks[cpu];
  456. if (cpu >= NR_CPUS) {
  457. pr_crit("CPU%u: bad primary CPU number\n", cpu);
  458. BUG();
  459. }
  460. /*
  461. * This only works on resume and secondary cores. For booting on the
  462. * boot cpu, smp_prepare_boot_cpu is called after percpu area setup.
  463. */
  464. set_my_cpu_offset(per_cpu_offset(cpu));
  465. cpu_proc_init();
  466. /*
  467. * Define the placement constraint for the inline asm directive below.
  468. * In Thumb-2, msr with an immediate value is not allowed.
  469. */
  470. #ifdef CONFIG_THUMB2_KERNEL
  471. #define PLC "r"
  472. #else
  473. #define PLC "I"
  474. #endif
  475. /*
  476. * setup stacks for re-entrant exception handlers
  477. */
  478. __asm__ (
  479. "msr cpsr_c, %1\n\t"
  480. "add r14, %0, %2\n\t"
  481. "mov sp, r14\n\t"
  482. "msr cpsr_c, %3\n\t"
  483. "add r14, %0, %4\n\t"
  484. "mov sp, r14\n\t"
  485. "msr cpsr_c, %5\n\t"
  486. "add r14, %0, %6\n\t"
  487. "mov sp, r14\n\t"
  488. "msr cpsr_c, %7\n\t"
  489. "add r14, %0, %8\n\t"
  490. "mov sp, r14\n\t"
  491. "msr cpsr_c, %9"
  492. :
  493. : "r" (stk),
  494. PLC (PSR_F_BIT | PSR_I_BIT | IRQ_MODE),
  495. "I" (offsetof(struct stack, irq[0])),
  496. PLC (PSR_F_BIT | PSR_I_BIT | ABT_MODE),
  497. "I" (offsetof(struct stack, abt[0])),
  498. PLC (PSR_F_BIT | PSR_I_BIT | UND_MODE),
  499. "I" (offsetof(struct stack, und[0])),
  500. PLC (PSR_F_BIT | PSR_I_BIT | FIQ_MODE),
  501. "I" (offsetof(struct stack, fiq[0])),
  502. PLC (PSR_F_BIT | PSR_I_BIT | SVC_MODE)
  503. : "r14");
  504. #endif
  505. }
  506. u32 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = MPIDR_INVALID };
  507. void __init smp_setup_processor_id(void)
  508. {
  509. int i;
  510. u32 mpidr = is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK : 0;
  511. u32 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  512. cpu_logical_map(0) = cpu;
  513. for (i = 1; i < nr_cpu_ids; ++i)
  514. cpu_logical_map(i) = i == cpu ? 0 : i;
  515. /*
  516. * clear __my_cpu_offset on boot CPU to avoid hang caused by
  517. * using percpu variable early, for example, lockdep will
  518. * access percpu variable inside lock_release
  519. */
  520. set_my_cpu_offset(0);
  521. pr_info("Booting Linux on physical CPU 0x%x\n", mpidr);
  522. }
  523. struct mpidr_hash mpidr_hash;
  524. #ifdef CONFIG_SMP
  525. /**
  526. * smp_build_mpidr_hash - Pre-compute shifts required at each affinity
  527. * level in order to build a linear index from an
  528. * MPIDR value. Resulting algorithm is a collision
  529. * free hash carried out through shifting and ORing
  530. */
  531. static void __init smp_build_mpidr_hash(void)
  532. {
  533. u32 i, affinity;
  534. u32 fs[3], bits[3], ls, mask = 0;
  535. /*
  536. * Pre-scan the list of MPIDRS and filter out bits that do
  537. * not contribute to affinity levels, ie they never toggle.
  538. */
  539. for_each_possible_cpu(i)
  540. mask |= (cpu_logical_map(i) ^ cpu_logical_map(0));
  541. pr_debug("mask of set bits 0x%x\n", mask);
  542. /*
  543. * Find and stash the last and first bit set at all affinity levels to
  544. * check how many bits are required to represent them.
  545. */
  546. for (i = 0; i < 3; i++) {
  547. affinity = MPIDR_AFFINITY_LEVEL(mask, i);
  548. /*
  549. * Find the MSB bit and LSB bits position
  550. * to determine how many bits are required
  551. * to express the affinity level.
  552. */
  553. ls = fls(affinity);
  554. fs[i] = affinity ? ffs(affinity) - 1 : 0;
  555. bits[i] = ls - fs[i];
  556. }
  557. /*
  558. * An index can be created from the MPIDR by isolating the
  559. * significant bits at each affinity level and by shifting
  560. * them in order to compress the 24 bits values space to a
  561. * compressed set of values. This is equivalent to hashing
  562. * the MPIDR through shifting and ORing. It is a collision free
  563. * hash though not minimal since some levels might contain a number
  564. * of CPUs that is not an exact power of 2 and their bit
  565. * representation might contain holes, eg MPIDR[7:0] = {0x2, 0x80}.
  566. */
  567. mpidr_hash.shift_aff[0] = fs[0];
  568. mpidr_hash.shift_aff[1] = MPIDR_LEVEL_BITS + fs[1] - bits[0];
  569. mpidr_hash.shift_aff[2] = 2*MPIDR_LEVEL_BITS + fs[2] -
  570. (bits[1] + bits[0]);
  571. mpidr_hash.mask = mask;
  572. mpidr_hash.bits = bits[2] + bits[1] + bits[0];
  573. pr_debug("MPIDR hash: aff0[%u] aff1[%u] aff2[%u] mask[0x%x] bits[%u]\n",
  574. mpidr_hash.shift_aff[0],
  575. mpidr_hash.shift_aff[1],
  576. mpidr_hash.shift_aff[2],
  577. mpidr_hash.mask,
  578. mpidr_hash.bits);
  579. /*
  580. * 4x is an arbitrary value used to warn on a hash table much bigger
  581. * than expected on most systems.
  582. */
  583. if (mpidr_hash_size() > 4 * num_possible_cpus())
  584. pr_warn("Large number of MPIDR hash buckets detected\n");
  585. sync_cache_w(&mpidr_hash);
  586. }
  587. #endif
  588. static void __init setup_processor(void)
  589. {
  590. struct proc_info_list *list;
  591. /*
  592. * locate processor in the list of supported processor
  593. * types. The linker builds this table for us from the
  594. * entries in arch/arm/mm/proc-*.S
  595. */
  596. list = lookup_processor_type(read_cpuid_id());
  597. if (!list) {
  598. pr_err("CPU configuration botched (ID %08x), unable to continue.\n",
  599. read_cpuid_id());
  600. while (1);
  601. }
  602. cpu_name = list->cpu_name;
  603. __cpu_architecture = __get_cpu_architecture();
  604. #ifdef MULTI_CPU
  605. processor = *list->proc;
  606. #endif
  607. #ifdef MULTI_TLB
  608. cpu_tlb = *list->tlb;
  609. #endif
  610. #ifdef MULTI_USER
  611. cpu_user = *list->user;
  612. #endif
  613. #ifdef MULTI_CACHE
  614. cpu_cache = *list->cache;
  615. #endif
  616. pr_info("CPU: %s [%08x] revision %d (ARMv%s), cr=%08lx\n",
  617. cpu_name, read_cpuid_id(), read_cpuid_id() & 15,
  618. proc_arch[cpu_architecture()], get_cr());
  619. snprintf(init_utsname()->machine, __NEW_UTS_LEN + 1, "%s%c",
  620. list->arch_name, ENDIANNESS);
  621. snprintf(elf_platform, ELF_PLATFORM_SIZE, "%s%c",
  622. list->elf_name, ENDIANNESS);
  623. elf_hwcap = list->elf_hwcap;
  624. cpuid_init_hwcaps();
  625. patch_aeabi_idiv();
  626. #ifndef CONFIG_ARM_THUMB
  627. elf_hwcap &= ~(HWCAP_THUMB | HWCAP_IDIVT);
  628. #endif
  629. #ifdef CONFIG_MMU
  630. init_default_cache_policy(list->__cpu_mm_mmu_flags);
  631. #endif
  632. erratum_a15_798181_init();
  633. elf_hwcap_fixup();
  634. cacheid_init();
  635. cpu_init();
  636. }
  637. void __init dump_machine_table(void)
  638. {
  639. const struct machine_desc *p;
  640. early_print("Available machine support:\n\nID (hex)\tNAME\n");
  641. for_each_machine_desc(p)
  642. early_print("%08x\t%s\n", p->nr, p->name);
  643. early_print("\nPlease check your kernel config and/or bootloader.\n");
  644. while (true)
  645. /* can't use cpu_relax() here as it may require MMU setup */;
  646. }
  647. int __init arm_add_memory(u64 start, u64 size)
  648. {
  649. u64 aligned_start;
  650. /*
  651. * Ensure that start/size are aligned to a page boundary.
  652. * Size is rounded down, start is rounded up.
  653. */
  654. aligned_start = PAGE_ALIGN(start);
  655. if (aligned_start > start + size)
  656. size = 0;
  657. else
  658. size -= aligned_start - start;
  659. #ifndef CONFIG_ARCH_PHYS_ADDR_T_64BIT
  660. if (aligned_start > ULONG_MAX) {
  661. pr_crit("Ignoring memory at 0x%08llx outside 32-bit physical address space\n",
  662. (long long)start);
  663. return -EINVAL;
  664. }
  665. if (aligned_start + size > ULONG_MAX) {
  666. pr_crit("Truncating memory at 0x%08llx to fit in 32-bit physical address space\n",
  667. (long long)start);
  668. /*
  669. * To ensure bank->start + bank->size is representable in
  670. * 32 bits, we use ULONG_MAX as the upper limit rather than 4GB.
  671. * This means we lose a page after masking.
  672. */
  673. size = ULONG_MAX - aligned_start;
  674. }
  675. #endif
  676. if (aligned_start < PHYS_OFFSET) {
  677. if (aligned_start + size <= PHYS_OFFSET) {
  678. pr_info("Ignoring memory below PHYS_OFFSET: 0x%08llx-0x%08llx\n",
  679. aligned_start, aligned_start + size);
  680. return -EINVAL;
  681. }
  682. pr_info("Ignoring memory below PHYS_OFFSET: 0x%08llx-0x%08llx\n",
  683. aligned_start, (u64)PHYS_OFFSET);
  684. size -= PHYS_OFFSET - aligned_start;
  685. aligned_start = PHYS_OFFSET;
  686. }
  687. start = aligned_start;
  688. size = size & ~(phys_addr_t)(PAGE_SIZE - 1);
  689. /*
  690. * Check whether this memory region has non-zero size or
  691. * invalid node number.
  692. */
  693. if (size == 0)
  694. return -EINVAL;
  695. memblock_add(start, size);
  696. return 0;
  697. }
  698. /*
  699. * Pick out the memory size. We look for mem=size@start,
  700. * where start and size are "size[KkMm]"
  701. */
  702. static int __init early_mem(char *p)
  703. {
  704. static int usermem __initdata = 0;
  705. u64 size;
  706. u64 start;
  707. char *endp;
  708. /*
  709. * If the user specifies memory size, we
  710. * blow away any automatically generated
  711. * size.
  712. */
  713. if (usermem == 0) {
  714. usermem = 1;
  715. memblock_remove(memblock_start_of_DRAM(),
  716. memblock_end_of_DRAM() - memblock_start_of_DRAM());
  717. }
  718. start = PHYS_OFFSET;
  719. size = memparse(p, &endp);
  720. if (*endp == '@')
  721. start = memparse(endp + 1, NULL);
  722. arm_add_memory(start, size);
  723. return 0;
  724. }
  725. early_param("mem", early_mem);
  726. static void __init request_standard_resources(const struct machine_desc *mdesc)
  727. {
  728. struct memblock_region *region;
  729. struct resource *res;
  730. kernel_code.start = virt_to_phys(_text);
  731. kernel_code.end = virt_to_phys(_etext - 1);
  732. kernel_data.start = virt_to_phys(_sdata);
  733. kernel_data.end = virt_to_phys(_end - 1);
  734. for_each_memblock(memory, region) {
  735. res = memblock_virt_alloc(sizeof(*res), 0);
  736. res->name = "System RAM";
  737. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  738. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  739. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  740. request_resource(&iomem_resource, res);
  741. if (kernel_code.start >= res->start &&
  742. kernel_code.end <= res->end)
  743. request_resource(res, &kernel_code);
  744. if (kernel_data.start >= res->start &&
  745. kernel_data.end <= res->end)
  746. request_resource(res, &kernel_data);
  747. }
  748. if (mdesc->video_start) {
  749. video_ram.start = mdesc->video_start;
  750. video_ram.end = mdesc->video_end;
  751. request_resource(&iomem_resource, &video_ram);
  752. }
  753. /*
  754. * Some machines don't have the possibility of ever
  755. * possessing lp0, lp1 or lp2
  756. */
  757. if (mdesc->reserve_lp0)
  758. request_resource(&ioport_resource, &lp0);
  759. if (mdesc->reserve_lp1)
  760. request_resource(&ioport_resource, &lp1);
  761. if (mdesc->reserve_lp2)
  762. request_resource(&ioport_resource, &lp2);
  763. }
  764. #if defined(CONFIG_VGA_CONSOLE) || defined(CONFIG_DUMMY_CONSOLE)
  765. struct screen_info screen_info = {
  766. .orig_video_lines = 30,
  767. .orig_video_cols = 80,
  768. .orig_video_mode = 0,
  769. .orig_video_ega_bx = 0,
  770. .orig_video_isVGA = 1,
  771. .orig_video_points = 8
  772. };
  773. #endif
  774. static int __init customize_machine(void)
  775. {
  776. /*
  777. * customizes platform devices, or adds new ones
  778. * On DT based machines, we fall back to populating the
  779. * machine from the device tree, if no callback is provided,
  780. * otherwise we would always need an init_machine callback.
  781. */
  782. of_iommu_init();
  783. if (machine_desc->init_machine)
  784. machine_desc->init_machine();
  785. #ifdef CONFIG_OF
  786. else
  787. of_platform_populate(NULL, of_default_bus_match_table,
  788. NULL, NULL);
  789. #endif
  790. return 0;
  791. }
  792. arch_initcall(customize_machine);
  793. static int __init init_machine_late(void)
  794. {
  795. struct device_node *root;
  796. int ret;
  797. if (machine_desc->init_late)
  798. machine_desc->init_late();
  799. root = of_find_node_by_path("/");
  800. if (root) {
  801. ret = of_property_read_string(root, "serial-number",
  802. &system_serial);
  803. if (ret)
  804. system_serial = NULL;
  805. }
  806. if (!system_serial)
  807. system_serial = kasprintf(GFP_KERNEL, "%08x%08x",
  808. system_serial_high,
  809. system_serial_low);
  810. return 0;
  811. }
  812. late_initcall(init_machine_late);
  813. #ifdef CONFIG_KEXEC
  814. static inline unsigned long long get_total_mem(void)
  815. {
  816. unsigned long total;
  817. total = max_low_pfn - min_low_pfn;
  818. return total << PAGE_SHIFT;
  819. }
  820. /**
  821. * reserve_crashkernel() - reserves memory are for crash kernel
  822. *
  823. * This function reserves memory area given in "crashkernel=" kernel command
  824. * line parameter. The memory reserved is used by a dump capture kernel when
  825. * primary kernel is crashing.
  826. */
  827. static void __init reserve_crashkernel(void)
  828. {
  829. unsigned long long crash_size, crash_base;
  830. unsigned long long total_mem;
  831. int ret;
  832. total_mem = get_total_mem();
  833. ret = parse_crashkernel(boot_command_line, total_mem,
  834. &crash_size, &crash_base);
  835. if (ret)
  836. return;
  837. ret = memblock_reserve(crash_base, crash_size);
  838. if (ret < 0) {
  839. pr_warn("crashkernel reservation failed - memory is in use (0x%lx)\n",
  840. (unsigned long)crash_base);
  841. return;
  842. }
  843. pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
  844. (unsigned long)(crash_size >> 20),
  845. (unsigned long)(crash_base >> 20),
  846. (unsigned long)(total_mem >> 20));
  847. crashk_res.start = crash_base;
  848. crashk_res.end = crash_base + crash_size - 1;
  849. insert_resource(&iomem_resource, &crashk_res);
  850. }
  851. #else
  852. static inline void reserve_crashkernel(void) {}
  853. #endif /* CONFIG_KEXEC */
  854. void __init hyp_mode_check(void)
  855. {
  856. #ifdef CONFIG_ARM_VIRT_EXT
  857. sync_boot_mode();
  858. if (is_hyp_mode_available()) {
  859. pr_info("CPU: All CPU(s) started in HYP mode.\n");
  860. pr_info("CPU: Virtualization extensions available.\n");
  861. } else if (is_hyp_mode_mismatched()) {
  862. pr_warn("CPU: WARNING: CPU(s) started in wrong/inconsistent modes (primary CPU mode 0x%x)\n",
  863. __boot_cpu_mode & MODE_MASK);
  864. pr_warn("CPU: This may indicate a broken bootloader or firmware.\n");
  865. } else
  866. pr_info("CPU: All CPU(s) started in SVC mode.\n");
  867. #endif
  868. }
  869. void __init setup_arch(char **cmdline_p)
  870. {
  871. const struct machine_desc *mdesc;
  872. setup_processor();
  873. mdesc = setup_machine_fdt(__atags_pointer);
  874. if (!mdesc)
  875. mdesc = setup_machine_tags(__atags_pointer, __machine_arch_type);
  876. machine_desc = mdesc;
  877. machine_name = mdesc->name;
  878. dump_stack_set_arch_desc("%s", mdesc->name);
  879. if (mdesc->reboot_mode != REBOOT_HARD)
  880. reboot_mode = mdesc->reboot_mode;
  881. init_mm.start_code = (unsigned long) _text;
  882. init_mm.end_code = (unsigned long) _etext;
  883. init_mm.end_data = (unsigned long) _edata;
  884. init_mm.brk = (unsigned long) _end;
  885. /* populate cmd_line too for later use, preserving boot_command_line */
  886. strlcpy(cmd_line, boot_command_line, COMMAND_LINE_SIZE);
  887. *cmdline_p = cmd_line;
  888. early_fixmap_init();
  889. early_ioremap_init();
  890. parse_early_param();
  891. #ifdef CONFIG_MMU
  892. early_paging_init(mdesc);
  893. #endif
  894. setup_dma_zone(mdesc);
  895. efi_init();
  896. sanity_check_meminfo();
  897. arm_memblock_init(mdesc);
  898. early_ioremap_reset();
  899. paging_init(mdesc);
  900. request_standard_resources(mdesc);
  901. if (mdesc->restart)
  902. arm_pm_restart = mdesc->restart;
  903. unflatten_device_tree();
  904. arm_dt_init_cpu_maps();
  905. psci_dt_init();
  906. xen_early_init();
  907. #ifdef CONFIG_SMP
  908. if (is_smp()) {
  909. if (!mdesc->smp_init || !mdesc->smp_init()) {
  910. if (psci_smp_available())
  911. smp_set_ops(&psci_smp_ops);
  912. else if (mdesc->smp)
  913. smp_set_ops(mdesc->smp);
  914. }
  915. smp_init_cpus();
  916. smp_build_mpidr_hash();
  917. }
  918. #endif
  919. if (!is_smp())
  920. hyp_mode_check();
  921. reserve_crashkernel();
  922. #ifdef CONFIG_MULTI_IRQ_HANDLER
  923. handle_arch_irq = mdesc->handle_irq;
  924. #endif
  925. #ifdef CONFIG_VT
  926. #if defined(CONFIG_VGA_CONSOLE)
  927. conswitchp = &vga_con;
  928. #elif defined(CONFIG_DUMMY_CONSOLE)
  929. conswitchp = &dummy_con;
  930. #endif
  931. #endif
  932. if (mdesc->init_early)
  933. mdesc->init_early();
  934. }
  935. static int __init topology_init(void)
  936. {
  937. int cpu;
  938. for_each_possible_cpu(cpu) {
  939. struct cpuinfo_arm *cpuinfo = &per_cpu(cpu_data, cpu);
  940. cpuinfo->cpu.hotpluggable = platform_can_hotplug_cpu(cpu);
  941. register_cpu(&cpuinfo->cpu, cpu);
  942. }
  943. return 0;
  944. }
  945. subsys_initcall(topology_init);
  946. #ifdef CONFIG_HAVE_PROC_CPU
  947. static int __init proc_cpu_init(void)
  948. {
  949. struct proc_dir_entry *res;
  950. res = proc_mkdir("cpu", NULL);
  951. if (!res)
  952. return -ENOMEM;
  953. return 0;
  954. }
  955. fs_initcall(proc_cpu_init);
  956. #endif
  957. static const char *hwcap_str[] = {
  958. "swp",
  959. "half",
  960. "thumb",
  961. "26bit",
  962. "fastmult",
  963. "fpa",
  964. "vfp",
  965. "edsp",
  966. "java",
  967. "iwmmxt",
  968. "crunch",
  969. "thumbee",
  970. "neon",
  971. "vfpv3",
  972. "vfpv3d16",
  973. "tls",
  974. "vfpv4",
  975. "idiva",
  976. "idivt",
  977. "vfpd32",
  978. "lpae",
  979. "evtstrm",
  980. NULL
  981. };
  982. static const char *hwcap2_str[] = {
  983. "aes",
  984. "pmull",
  985. "sha1",
  986. "sha2",
  987. "crc32",
  988. NULL
  989. };
  990. static int c_show(struct seq_file *m, void *v)
  991. {
  992. int i, j;
  993. u32 cpuid;
  994. for_each_online_cpu(i) {
  995. /*
  996. * glibc reads /proc/cpuinfo to determine the number of
  997. * online processors, looking for lines beginning with
  998. * "processor". Give glibc what it expects.
  999. */
  1000. seq_printf(m, "processor\t: %d\n", i);
  1001. cpuid = is_smp() ? per_cpu(cpu_data, i).cpuid : read_cpuid_id();
  1002. seq_printf(m, "model name\t: %s rev %d (%s)\n",
  1003. cpu_name, cpuid & 15, elf_platform);
  1004. #if defined(CONFIG_SMP)
  1005. seq_printf(m, "BogoMIPS\t: %lu.%02lu\n",
  1006. per_cpu(cpu_data, i).loops_per_jiffy / (500000UL/HZ),
  1007. (per_cpu(cpu_data, i).loops_per_jiffy / (5000UL/HZ)) % 100);
  1008. #else
  1009. seq_printf(m, "BogoMIPS\t: %lu.%02lu\n",
  1010. loops_per_jiffy / (500000/HZ),
  1011. (loops_per_jiffy / (5000/HZ)) % 100);
  1012. #endif
  1013. /* dump out the processor features */
  1014. seq_puts(m, "Features\t: ");
  1015. for (j = 0; hwcap_str[j]; j++)
  1016. if (elf_hwcap & (1 << j))
  1017. seq_printf(m, "%s ", hwcap_str[j]);
  1018. for (j = 0; hwcap2_str[j]; j++)
  1019. if (elf_hwcap2 & (1 << j))
  1020. seq_printf(m, "%s ", hwcap2_str[j]);
  1021. seq_printf(m, "\nCPU implementer\t: 0x%02x\n", cpuid >> 24);
  1022. seq_printf(m, "CPU architecture: %s\n",
  1023. proc_arch[cpu_architecture()]);
  1024. if ((cpuid & 0x0008f000) == 0x00000000) {
  1025. /* pre-ARM7 */
  1026. seq_printf(m, "CPU part\t: %07x\n", cpuid >> 4);
  1027. } else {
  1028. if ((cpuid & 0x0008f000) == 0x00007000) {
  1029. /* ARM7 */
  1030. seq_printf(m, "CPU variant\t: 0x%02x\n",
  1031. (cpuid >> 16) & 127);
  1032. } else {
  1033. /* post-ARM7 */
  1034. seq_printf(m, "CPU variant\t: 0x%x\n",
  1035. (cpuid >> 20) & 15);
  1036. }
  1037. seq_printf(m, "CPU part\t: 0x%03x\n",
  1038. (cpuid >> 4) & 0xfff);
  1039. }
  1040. seq_printf(m, "CPU revision\t: %d\n\n", cpuid & 15);
  1041. }
  1042. seq_printf(m, "Hardware\t: %s\n", machine_name);
  1043. seq_printf(m, "Revision\t: %04x\n", system_rev);
  1044. seq_printf(m, "Serial\t\t: %s\n", system_serial);
  1045. return 0;
  1046. }
  1047. static void *c_start(struct seq_file *m, loff_t *pos)
  1048. {
  1049. return *pos < 1 ? (void *)1 : NULL;
  1050. }
  1051. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  1052. {
  1053. ++*pos;
  1054. return NULL;
  1055. }
  1056. static void c_stop(struct seq_file *m, void *v)
  1057. {
  1058. }
  1059. const struct seq_operations cpuinfo_op = {
  1060. .start = c_start,
  1061. .next = c_next,
  1062. .stop = c_stop,
  1063. .show = c_show
  1064. };