setup.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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_MEM
  154. },
  155. {
  156. .name = "Kernel data",
  157. .start = 0,
  158. .end = 0,
  159. .flags = IORESOURCE_MEM
  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. ((u32 *)fn_addr)[0] = udiv_instruction();
  377. ((u32 *)fn_addr)[1] = bx_lr_instruction();
  378. flush_icache_range(fn_addr, fn_addr + 8);
  379. fn_addr = ((uintptr_t)&__aeabi_idiv) & ~1;
  380. ((u32 *)fn_addr)[0] = sdiv_instruction();
  381. ((u32 *)fn_addr)[1] = bx_lr_instruction();
  382. flush_icache_range(fn_addr, fn_addr + 8);
  383. }
  384. #else
  385. static inline void patch_aeabi_idiv(void) { }
  386. #endif
  387. static void __init cpuid_init_hwcaps(void)
  388. {
  389. int block;
  390. u32 isar5;
  391. if (cpu_architecture() < CPU_ARCH_ARMv7)
  392. return;
  393. block = cpuid_feature_extract(CPUID_EXT_ISAR0, 24);
  394. if (block >= 2)
  395. elf_hwcap |= HWCAP_IDIVA;
  396. if (block >= 1)
  397. elf_hwcap |= HWCAP_IDIVT;
  398. /* LPAE implies atomic ldrd/strd instructions */
  399. block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);
  400. if (block >= 5)
  401. elf_hwcap |= HWCAP_LPAE;
  402. /* check for supported v8 Crypto instructions */
  403. isar5 = read_cpuid_ext(CPUID_EXT_ISAR5);
  404. block = cpuid_feature_extract_field(isar5, 4);
  405. if (block >= 2)
  406. elf_hwcap2 |= HWCAP2_PMULL;
  407. if (block >= 1)
  408. elf_hwcap2 |= HWCAP2_AES;
  409. block = cpuid_feature_extract_field(isar5, 8);
  410. if (block >= 1)
  411. elf_hwcap2 |= HWCAP2_SHA1;
  412. block = cpuid_feature_extract_field(isar5, 12);
  413. if (block >= 1)
  414. elf_hwcap2 |= HWCAP2_SHA2;
  415. block = cpuid_feature_extract_field(isar5, 16);
  416. if (block >= 1)
  417. elf_hwcap2 |= HWCAP2_CRC32;
  418. }
  419. static void __init elf_hwcap_fixup(void)
  420. {
  421. unsigned id = read_cpuid_id();
  422. /*
  423. * HWCAP_TLS is available only on 1136 r1p0 and later,
  424. * see also kuser_get_tls_init.
  425. */
  426. if (read_cpuid_part() == ARM_CPU_PART_ARM1136 &&
  427. ((id >> 20) & 3) == 0) {
  428. elf_hwcap &= ~HWCAP_TLS;
  429. return;
  430. }
  431. /* Verify if CPUID scheme is implemented */
  432. if ((id & 0x000f0000) != 0x000f0000)
  433. return;
  434. /*
  435. * If the CPU supports LDREX/STREX and LDREXB/STREXB,
  436. * avoid advertising SWP; it may not be atomic with
  437. * multiprocessing cores.
  438. */
  439. if (cpuid_feature_extract(CPUID_EXT_ISAR3, 12) > 1 ||
  440. (cpuid_feature_extract(CPUID_EXT_ISAR3, 12) == 1 &&
  441. cpuid_feature_extract(CPUID_EXT_ISAR3, 20) >= 3))
  442. elf_hwcap &= ~HWCAP_SWP;
  443. }
  444. /*
  445. * cpu_init - initialise one CPU.
  446. *
  447. * cpu_init sets up the per-CPU stacks.
  448. */
  449. void notrace cpu_init(void)
  450. {
  451. #ifndef CONFIG_CPU_V7M
  452. unsigned int cpu = smp_processor_id();
  453. struct stack *stk = &stacks[cpu];
  454. if (cpu >= NR_CPUS) {
  455. pr_crit("CPU%u: bad primary CPU number\n", cpu);
  456. BUG();
  457. }
  458. /*
  459. * This only works on resume and secondary cores. For booting on the
  460. * boot cpu, smp_prepare_boot_cpu is called after percpu area setup.
  461. */
  462. set_my_cpu_offset(per_cpu_offset(cpu));
  463. cpu_proc_init();
  464. /*
  465. * Define the placement constraint for the inline asm directive below.
  466. * In Thumb-2, msr with an immediate value is not allowed.
  467. */
  468. #ifdef CONFIG_THUMB2_KERNEL
  469. #define PLC "r"
  470. #else
  471. #define PLC "I"
  472. #endif
  473. /*
  474. * setup stacks for re-entrant exception handlers
  475. */
  476. __asm__ (
  477. "msr cpsr_c, %1\n\t"
  478. "add r14, %0, %2\n\t"
  479. "mov sp, r14\n\t"
  480. "msr cpsr_c, %3\n\t"
  481. "add r14, %0, %4\n\t"
  482. "mov sp, r14\n\t"
  483. "msr cpsr_c, %5\n\t"
  484. "add r14, %0, %6\n\t"
  485. "mov sp, r14\n\t"
  486. "msr cpsr_c, %7\n\t"
  487. "add r14, %0, %8\n\t"
  488. "mov sp, r14\n\t"
  489. "msr cpsr_c, %9"
  490. :
  491. : "r" (stk),
  492. PLC (PSR_F_BIT | PSR_I_BIT | IRQ_MODE),
  493. "I" (offsetof(struct stack, irq[0])),
  494. PLC (PSR_F_BIT | PSR_I_BIT | ABT_MODE),
  495. "I" (offsetof(struct stack, abt[0])),
  496. PLC (PSR_F_BIT | PSR_I_BIT | UND_MODE),
  497. "I" (offsetof(struct stack, und[0])),
  498. PLC (PSR_F_BIT | PSR_I_BIT | FIQ_MODE),
  499. "I" (offsetof(struct stack, fiq[0])),
  500. PLC (PSR_F_BIT | PSR_I_BIT | SVC_MODE)
  501. : "r14");
  502. #endif
  503. }
  504. u32 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = MPIDR_INVALID };
  505. void __init smp_setup_processor_id(void)
  506. {
  507. int i;
  508. u32 mpidr = is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK : 0;
  509. u32 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  510. cpu_logical_map(0) = cpu;
  511. for (i = 1; i < nr_cpu_ids; ++i)
  512. cpu_logical_map(i) = i == cpu ? 0 : i;
  513. /*
  514. * clear __my_cpu_offset on boot CPU to avoid hang caused by
  515. * using percpu variable early, for example, lockdep will
  516. * access percpu variable inside lock_release
  517. */
  518. set_my_cpu_offset(0);
  519. pr_info("Booting Linux on physical CPU 0x%x\n", mpidr);
  520. }
  521. struct mpidr_hash mpidr_hash;
  522. #ifdef CONFIG_SMP
  523. /**
  524. * smp_build_mpidr_hash - Pre-compute shifts required at each affinity
  525. * level in order to build a linear index from an
  526. * MPIDR value. Resulting algorithm is a collision
  527. * free hash carried out through shifting and ORing
  528. */
  529. static void __init smp_build_mpidr_hash(void)
  530. {
  531. u32 i, affinity;
  532. u32 fs[3], bits[3], ls, mask = 0;
  533. /*
  534. * Pre-scan the list of MPIDRS and filter out bits that do
  535. * not contribute to affinity levels, ie they never toggle.
  536. */
  537. for_each_possible_cpu(i)
  538. mask |= (cpu_logical_map(i) ^ cpu_logical_map(0));
  539. pr_debug("mask of set bits 0x%x\n", mask);
  540. /*
  541. * Find and stash the last and first bit set at all affinity levels to
  542. * check how many bits are required to represent them.
  543. */
  544. for (i = 0; i < 3; i++) {
  545. affinity = MPIDR_AFFINITY_LEVEL(mask, i);
  546. /*
  547. * Find the MSB bit and LSB bits position
  548. * to determine how many bits are required
  549. * to express the affinity level.
  550. */
  551. ls = fls(affinity);
  552. fs[i] = affinity ? ffs(affinity) - 1 : 0;
  553. bits[i] = ls - fs[i];
  554. }
  555. /*
  556. * An index can be created from the MPIDR by isolating the
  557. * significant bits at each affinity level and by shifting
  558. * them in order to compress the 24 bits values space to a
  559. * compressed set of values. This is equivalent to hashing
  560. * the MPIDR through shifting and ORing. It is a collision free
  561. * hash though not minimal since some levels might contain a number
  562. * of CPUs that is not an exact power of 2 and their bit
  563. * representation might contain holes, eg MPIDR[7:0] = {0x2, 0x80}.
  564. */
  565. mpidr_hash.shift_aff[0] = fs[0];
  566. mpidr_hash.shift_aff[1] = MPIDR_LEVEL_BITS + fs[1] - bits[0];
  567. mpidr_hash.shift_aff[2] = 2*MPIDR_LEVEL_BITS + fs[2] -
  568. (bits[1] + bits[0]);
  569. mpidr_hash.mask = mask;
  570. mpidr_hash.bits = bits[2] + bits[1] + bits[0];
  571. pr_debug("MPIDR hash: aff0[%u] aff1[%u] aff2[%u] mask[0x%x] bits[%u]\n",
  572. mpidr_hash.shift_aff[0],
  573. mpidr_hash.shift_aff[1],
  574. mpidr_hash.shift_aff[2],
  575. mpidr_hash.mask,
  576. mpidr_hash.bits);
  577. /*
  578. * 4x is an arbitrary value used to warn on a hash table much bigger
  579. * than expected on most systems.
  580. */
  581. if (mpidr_hash_size() > 4 * num_possible_cpus())
  582. pr_warn("Large number of MPIDR hash buckets detected\n");
  583. sync_cache_w(&mpidr_hash);
  584. }
  585. #endif
  586. static void __init setup_processor(void)
  587. {
  588. struct proc_info_list *list;
  589. /*
  590. * locate processor in the list of supported processor
  591. * types. The linker builds this table for us from the
  592. * entries in arch/arm/mm/proc-*.S
  593. */
  594. list = lookup_processor_type(read_cpuid_id());
  595. if (!list) {
  596. pr_err("CPU configuration botched (ID %08x), unable to continue.\n",
  597. read_cpuid_id());
  598. while (1);
  599. }
  600. cpu_name = list->cpu_name;
  601. __cpu_architecture = __get_cpu_architecture();
  602. #ifdef MULTI_CPU
  603. processor = *list->proc;
  604. #endif
  605. #ifdef MULTI_TLB
  606. cpu_tlb = *list->tlb;
  607. #endif
  608. #ifdef MULTI_USER
  609. cpu_user = *list->user;
  610. #endif
  611. #ifdef MULTI_CACHE
  612. cpu_cache = *list->cache;
  613. #endif
  614. pr_info("CPU: %s [%08x] revision %d (ARMv%s), cr=%08lx\n",
  615. cpu_name, read_cpuid_id(), read_cpuid_id() & 15,
  616. proc_arch[cpu_architecture()], get_cr());
  617. snprintf(init_utsname()->machine, __NEW_UTS_LEN + 1, "%s%c",
  618. list->arch_name, ENDIANNESS);
  619. snprintf(elf_platform, ELF_PLATFORM_SIZE, "%s%c",
  620. list->elf_name, ENDIANNESS);
  621. elf_hwcap = list->elf_hwcap;
  622. cpuid_init_hwcaps();
  623. patch_aeabi_idiv();
  624. #ifndef CONFIG_ARM_THUMB
  625. elf_hwcap &= ~(HWCAP_THUMB | HWCAP_IDIVT);
  626. #endif
  627. #ifdef CONFIG_MMU
  628. init_default_cache_policy(list->__cpu_mm_mmu_flags);
  629. #endif
  630. erratum_a15_798181_init();
  631. elf_hwcap_fixup();
  632. cacheid_init();
  633. cpu_init();
  634. }
  635. void __init dump_machine_table(void)
  636. {
  637. const struct machine_desc *p;
  638. early_print("Available machine support:\n\nID (hex)\tNAME\n");
  639. for_each_machine_desc(p)
  640. early_print("%08x\t%s\n", p->nr, p->name);
  641. early_print("\nPlease check your kernel config and/or bootloader.\n");
  642. while (true)
  643. /* can't use cpu_relax() here as it may require MMU setup */;
  644. }
  645. int __init arm_add_memory(u64 start, u64 size)
  646. {
  647. u64 aligned_start;
  648. /*
  649. * Ensure that start/size are aligned to a page boundary.
  650. * Size is rounded down, start is rounded up.
  651. */
  652. aligned_start = PAGE_ALIGN(start);
  653. if (aligned_start > start + size)
  654. size = 0;
  655. else
  656. size -= aligned_start - start;
  657. #ifndef CONFIG_ARCH_PHYS_ADDR_T_64BIT
  658. if (aligned_start > ULONG_MAX) {
  659. pr_crit("Ignoring memory at 0x%08llx outside 32-bit physical address space\n",
  660. (long long)start);
  661. return -EINVAL;
  662. }
  663. if (aligned_start + size > ULONG_MAX) {
  664. pr_crit("Truncating memory at 0x%08llx to fit in 32-bit physical address space\n",
  665. (long long)start);
  666. /*
  667. * To ensure bank->start + bank->size is representable in
  668. * 32 bits, we use ULONG_MAX as the upper limit rather than 4GB.
  669. * This means we lose a page after masking.
  670. */
  671. size = ULONG_MAX - aligned_start;
  672. }
  673. #endif
  674. if (aligned_start < PHYS_OFFSET) {
  675. if (aligned_start + size <= PHYS_OFFSET) {
  676. pr_info("Ignoring memory below PHYS_OFFSET: 0x%08llx-0x%08llx\n",
  677. aligned_start, aligned_start + size);
  678. return -EINVAL;
  679. }
  680. pr_info("Ignoring memory below PHYS_OFFSET: 0x%08llx-0x%08llx\n",
  681. aligned_start, (u64)PHYS_OFFSET);
  682. size -= PHYS_OFFSET - aligned_start;
  683. aligned_start = PHYS_OFFSET;
  684. }
  685. start = aligned_start;
  686. size = size & ~(phys_addr_t)(PAGE_SIZE - 1);
  687. /*
  688. * Check whether this memory region has non-zero size or
  689. * invalid node number.
  690. */
  691. if (size == 0)
  692. return -EINVAL;
  693. memblock_add(start, size);
  694. return 0;
  695. }
  696. /*
  697. * Pick out the memory size. We look for mem=size@start,
  698. * where start and size are "size[KkMm]"
  699. */
  700. static int __init early_mem(char *p)
  701. {
  702. static int usermem __initdata = 0;
  703. u64 size;
  704. u64 start;
  705. char *endp;
  706. /*
  707. * If the user specifies memory size, we
  708. * blow away any automatically generated
  709. * size.
  710. */
  711. if (usermem == 0) {
  712. usermem = 1;
  713. memblock_remove(memblock_start_of_DRAM(),
  714. memblock_end_of_DRAM() - memblock_start_of_DRAM());
  715. }
  716. start = PHYS_OFFSET;
  717. size = memparse(p, &endp);
  718. if (*endp == '@')
  719. start = memparse(endp + 1, NULL);
  720. arm_add_memory(start, size);
  721. return 0;
  722. }
  723. early_param("mem", early_mem);
  724. static void __init request_standard_resources(const struct machine_desc *mdesc)
  725. {
  726. struct memblock_region *region;
  727. struct resource *res;
  728. kernel_code.start = virt_to_phys(_text);
  729. kernel_code.end = virt_to_phys(_etext - 1);
  730. kernel_data.start = virt_to_phys(_sdata);
  731. kernel_data.end = virt_to_phys(_end - 1);
  732. for_each_memblock(memory, region) {
  733. res = memblock_virt_alloc(sizeof(*res), 0);
  734. res->name = "System RAM";
  735. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  736. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  737. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  738. request_resource(&iomem_resource, res);
  739. if (kernel_code.start >= res->start &&
  740. kernel_code.end <= res->end)
  741. request_resource(res, &kernel_code);
  742. if (kernel_data.start >= res->start &&
  743. kernel_data.end <= res->end)
  744. request_resource(res, &kernel_data);
  745. }
  746. if (mdesc->video_start) {
  747. video_ram.start = mdesc->video_start;
  748. video_ram.end = mdesc->video_end;
  749. request_resource(&iomem_resource, &video_ram);
  750. }
  751. /*
  752. * Some machines don't have the possibility of ever
  753. * possessing lp0, lp1 or lp2
  754. */
  755. if (mdesc->reserve_lp0)
  756. request_resource(&ioport_resource, &lp0);
  757. if (mdesc->reserve_lp1)
  758. request_resource(&ioport_resource, &lp1);
  759. if (mdesc->reserve_lp2)
  760. request_resource(&ioport_resource, &lp2);
  761. }
  762. #if defined(CONFIG_VGA_CONSOLE) || defined(CONFIG_DUMMY_CONSOLE)
  763. struct screen_info screen_info = {
  764. .orig_video_lines = 30,
  765. .orig_video_cols = 80,
  766. .orig_video_mode = 0,
  767. .orig_video_ega_bx = 0,
  768. .orig_video_isVGA = 1,
  769. .orig_video_points = 8
  770. };
  771. #endif
  772. static int __init customize_machine(void)
  773. {
  774. /*
  775. * customizes platform devices, or adds new ones
  776. * On DT based machines, we fall back to populating the
  777. * machine from the device tree, if no callback is provided,
  778. * otherwise we would always need an init_machine callback.
  779. */
  780. of_iommu_init();
  781. if (machine_desc->init_machine)
  782. machine_desc->init_machine();
  783. #ifdef CONFIG_OF
  784. else
  785. of_platform_populate(NULL, of_default_bus_match_table,
  786. NULL, NULL);
  787. #endif
  788. return 0;
  789. }
  790. arch_initcall(customize_machine);
  791. static int __init init_machine_late(void)
  792. {
  793. struct device_node *root;
  794. int ret;
  795. if (machine_desc->init_late)
  796. machine_desc->init_late();
  797. root = of_find_node_by_path("/");
  798. if (root) {
  799. ret = of_property_read_string(root, "serial-number",
  800. &system_serial);
  801. if (ret)
  802. system_serial = NULL;
  803. }
  804. if (!system_serial)
  805. system_serial = kasprintf(GFP_KERNEL, "%08x%08x",
  806. system_serial_high,
  807. system_serial_low);
  808. return 0;
  809. }
  810. late_initcall(init_machine_late);
  811. #ifdef CONFIG_KEXEC
  812. static inline unsigned long long get_total_mem(void)
  813. {
  814. unsigned long total;
  815. total = max_low_pfn - min_low_pfn;
  816. return total << PAGE_SHIFT;
  817. }
  818. /**
  819. * reserve_crashkernel() - reserves memory are for crash kernel
  820. *
  821. * This function reserves memory area given in "crashkernel=" kernel command
  822. * line parameter. The memory reserved is used by a dump capture kernel when
  823. * primary kernel is crashing.
  824. */
  825. static void __init reserve_crashkernel(void)
  826. {
  827. unsigned long long crash_size, crash_base;
  828. unsigned long long total_mem;
  829. int ret;
  830. total_mem = get_total_mem();
  831. ret = parse_crashkernel(boot_command_line, total_mem,
  832. &crash_size, &crash_base);
  833. if (ret)
  834. return;
  835. ret = memblock_reserve(crash_base, crash_size);
  836. if (ret < 0) {
  837. pr_warn("crashkernel reservation failed - memory is in use (0x%lx)\n",
  838. (unsigned long)crash_base);
  839. return;
  840. }
  841. pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
  842. (unsigned long)(crash_size >> 20),
  843. (unsigned long)(crash_base >> 20),
  844. (unsigned long)(total_mem >> 20));
  845. crashk_res.start = crash_base;
  846. crashk_res.end = crash_base + crash_size - 1;
  847. insert_resource(&iomem_resource, &crashk_res);
  848. }
  849. #else
  850. static inline void reserve_crashkernel(void) {}
  851. #endif /* CONFIG_KEXEC */
  852. void __init hyp_mode_check(void)
  853. {
  854. #ifdef CONFIG_ARM_VIRT_EXT
  855. sync_boot_mode();
  856. if (is_hyp_mode_available()) {
  857. pr_info("CPU: All CPU(s) started in HYP mode.\n");
  858. pr_info("CPU: Virtualization extensions available.\n");
  859. } else if (is_hyp_mode_mismatched()) {
  860. pr_warn("CPU: WARNING: CPU(s) started in wrong/inconsistent modes (primary CPU mode 0x%x)\n",
  861. __boot_cpu_mode & MODE_MASK);
  862. pr_warn("CPU: This may indicate a broken bootloader or firmware.\n");
  863. } else
  864. pr_info("CPU: All CPU(s) started in SVC mode.\n");
  865. #endif
  866. }
  867. void __init setup_arch(char **cmdline_p)
  868. {
  869. const struct machine_desc *mdesc;
  870. setup_processor();
  871. mdesc = setup_machine_fdt(__atags_pointer);
  872. if (!mdesc)
  873. mdesc = setup_machine_tags(__atags_pointer, __machine_arch_type);
  874. machine_desc = mdesc;
  875. machine_name = mdesc->name;
  876. dump_stack_set_arch_desc("%s", mdesc->name);
  877. if (mdesc->reboot_mode != REBOOT_HARD)
  878. reboot_mode = mdesc->reboot_mode;
  879. init_mm.start_code = (unsigned long) _text;
  880. init_mm.end_code = (unsigned long) _etext;
  881. init_mm.end_data = (unsigned long) _edata;
  882. init_mm.brk = (unsigned long) _end;
  883. /* populate cmd_line too for later use, preserving boot_command_line */
  884. strlcpy(cmd_line, boot_command_line, COMMAND_LINE_SIZE);
  885. *cmdline_p = cmd_line;
  886. early_fixmap_init();
  887. early_ioremap_init();
  888. parse_early_param();
  889. #ifdef CONFIG_MMU
  890. early_paging_init(mdesc);
  891. #endif
  892. setup_dma_zone(mdesc);
  893. efi_init();
  894. sanity_check_meminfo();
  895. arm_memblock_init(mdesc);
  896. early_ioremap_reset();
  897. paging_init(mdesc);
  898. request_standard_resources(mdesc);
  899. if (mdesc->restart)
  900. arm_pm_restart = mdesc->restart;
  901. unflatten_device_tree();
  902. arm_dt_init_cpu_maps();
  903. psci_dt_init();
  904. xen_early_init();
  905. #ifdef CONFIG_SMP
  906. if (is_smp()) {
  907. if (!mdesc->smp_init || !mdesc->smp_init()) {
  908. if (psci_smp_available())
  909. smp_set_ops(&psci_smp_ops);
  910. else if (mdesc->smp)
  911. smp_set_ops(mdesc->smp);
  912. }
  913. smp_init_cpus();
  914. smp_build_mpidr_hash();
  915. }
  916. #endif
  917. if (!is_smp())
  918. hyp_mode_check();
  919. reserve_crashkernel();
  920. #ifdef CONFIG_MULTI_IRQ_HANDLER
  921. handle_arch_irq = mdesc->handle_irq;
  922. #endif
  923. #ifdef CONFIG_VT
  924. #if defined(CONFIG_VGA_CONSOLE)
  925. conswitchp = &vga_con;
  926. #elif defined(CONFIG_DUMMY_CONSOLE)
  927. conswitchp = &dummy_con;
  928. #endif
  929. #endif
  930. if (mdesc->init_early)
  931. mdesc->init_early();
  932. }
  933. static int __init topology_init(void)
  934. {
  935. int cpu;
  936. for_each_possible_cpu(cpu) {
  937. struct cpuinfo_arm *cpuinfo = &per_cpu(cpu_data, cpu);
  938. cpuinfo->cpu.hotpluggable = platform_can_hotplug_cpu(cpu);
  939. register_cpu(&cpuinfo->cpu, cpu);
  940. }
  941. return 0;
  942. }
  943. subsys_initcall(topology_init);
  944. #ifdef CONFIG_HAVE_PROC_CPU
  945. static int __init proc_cpu_init(void)
  946. {
  947. struct proc_dir_entry *res;
  948. res = proc_mkdir("cpu", NULL);
  949. if (!res)
  950. return -ENOMEM;
  951. return 0;
  952. }
  953. fs_initcall(proc_cpu_init);
  954. #endif
  955. static const char *hwcap_str[] = {
  956. "swp",
  957. "half",
  958. "thumb",
  959. "26bit",
  960. "fastmult",
  961. "fpa",
  962. "vfp",
  963. "edsp",
  964. "java",
  965. "iwmmxt",
  966. "crunch",
  967. "thumbee",
  968. "neon",
  969. "vfpv3",
  970. "vfpv3d16",
  971. "tls",
  972. "vfpv4",
  973. "idiva",
  974. "idivt",
  975. "vfpd32",
  976. "lpae",
  977. "evtstrm",
  978. NULL
  979. };
  980. static const char *hwcap2_str[] = {
  981. "aes",
  982. "pmull",
  983. "sha1",
  984. "sha2",
  985. "crc32",
  986. NULL
  987. };
  988. static int c_show(struct seq_file *m, void *v)
  989. {
  990. int i, j;
  991. u32 cpuid;
  992. for_each_online_cpu(i) {
  993. /*
  994. * glibc reads /proc/cpuinfo to determine the number of
  995. * online processors, looking for lines beginning with
  996. * "processor". Give glibc what it expects.
  997. */
  998. seq_printf(m, "processor\t: %d\n", i);
  999. cpuid = is_smp() ? per_cpu(cpu_data, i).cpuid : read_cpuid_id();
  1000. seq_printf(m, "model name\t: %s rev %d (%s)\n",
  1001. cpu_name, cpuid & 15, elf_platform);
  1002. #if defined(CONFIG_SMP)
  1003. seq_printf(m, "BogoMIPS\t: %lu.%02lu\n",
  1004. per_cpu(cpu_data, i).loops_per_jiffy / (500000UL/HZ),
  1005. (per_cpu(cpu_data, i).loops_per_jiffy / (5000UL/HZ)) % 100);
  1006. #else
  1007. seq_printf(m, "BogoMIPS\t: %lu.%02lu\n",
  1008. loops_per_jiffy / (500000/HZ),
  1009. (loops_per_jiffy / (5000/HZ)) % 100);
  1010. #endif
  1011. /* dump out the processor features */
  1012. seq_puts(m, "Features\t: ");
  1013. for (j = 0; hwcap_str[j]; j++)
  1014. if (elf_hwcap & (1 << j))
  1015. seq_printf(m, "%s ", hwcap_str[j]);
  1016. for (j = 0; hwcap2_str[j]; j++)
  1017. if (elf_hwcap2 & (1 << j))
  1018. seq_printf(m, "%s ", hwcap2_str[j]);
  1019. seq_printf(m, "\nCPU implementer\t: 0x%02x\n", cpuid >> 24);
  1020. seq_printf(m, "CPU architecture: %s\n",
  1021. proc_arch[cpu_architecture()]);
  1022. if ((cpuid & 0x0008f000) == 0x00000000) {
  1023. /* pre-ARM7 */
  1024. seq_printf(m, "CPU part\t: %07x\n", cpuid >> 4);
  1025. } else {
  1026. if ((cpuid & 0x0008f000) == 0x00007000) {
  1027. /* ARM7 */
  1028. seq_printf(m, "CPU variant\t: 0x%02x\n",
  1029. (cpuid >> 16) & 127);
  1030. } else {
  1031. /* post-ARM7 */
  1032. seq_printf(m, "CPU variant\t: 0x%x\n",
  1033. (cpuid >> 20) & 15);
  1034. }
  1035. seq_printf(m, "CPU part\t: 0x%03x\n",
  1036. (cpuid >> 4) & 0xfff);
  1037. }
  1038. seq_printf(m, "CPU revision\t: %d\n\n", cpuid & 15);
  1039. }
  1040. seq_printf(m, "Hardware\t: %s\n", machine_name);
  1041. seq_printf(m, "Revision\t: %04x\n", system_rev);
  1042. seq_printf(m, "Serial\t\t: %s\n", system_serial);
  1043. return 0;
  1044. }
  1045. static void *c_start(struct seq_file *m, loff_t *pos)
  1046. {
  1047. return *pos < 1 ? (void *)1 : NULL;
  1048. }
  1049. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  1050. {
  1051. ++*pos;
  1052. return NULL;
  1053. }
  1054. static void c_stop(struct seq_file *m, void *v)
  1055. {
  1056. }
  1057. const struct seq_operations cpuinfo_op = {
  1058. .start = c_start,
  1059. .next = c_next,
  1060. .stop = c_stop,
  1061. .show = c_show
  1062. };