setup-common.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Common boot and setup code for both 32-bit and 64-bit.
  4. * Extracted from arch/powerpc/kernel/setup_64.c.
  5. *
  6. * Copyright (C) 2001 PPC64 Team, IBM Corp
  7. */
  8. #undef DEBUG
  9. #include <linux/export.h>
  10. #include <linux/panic_notifier.h>
  11. #include <linux/string.h>
  12. #include <linux/sched.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/reboot.h>
  16. #include <linux/delay.h>
  17. #include <linux/initrd.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/printk.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/ioport.h>
  22. #include <linux/console.h>
  23. #include <linux/root_dev.h>
  24. #include <linux/cpu.h>
  25. #include <linux/unistd.h>
  26. #include <linux/seq_buf.h>
  27. #include <linux/serial.h>
  28. #include <linux/serial_8250.h>
  29. #include <linux/percpu.h>
  30. #include <linux/memblock.h>
  31. #include <linux/of.h>
  32. #include <linux/of_fdt.h>
  33. #include <linux/of_irq.h>
  34. #include <linux/hugetlb.h>
  35. #include <linux/pgtable.h>
  36. #include <asm/io.h>
  37. #include <asm/paca.h>
  38. #include <asm/processor.h>
  39. #include <asm/vdso_datapage.h>
  40. #include <asm/smp.h>
  41. #include <asm/elf.h>
  42. #include <asm/machdep.h>
  43. #include <asm/time.h>
  44. #include <asm/cputable.h>
  45. #include <asm/sections.h>
  46. #include <asm/firmware.h>
  47. #include <asm/btext.h>
  48. #include <asm/nvram.h>
  49. #include <asm/setup.h>
  50. #include <asm/rtas.h>
  51. #include <asm/iommu.h>
  52. #include <asm/serial.h>
  53. #include <asm/cache.h>
  54. #include <asm/page.h>
  55. #include <asm/mmu.h>
  56. #include <asm/xmon.h>
  57. #include <asm/cputhreads.h>
  58. #include <mm/mmu_decl.h>
  59. #include <asm/archrandom.h>
  60. #include <asm/fadump.h>
  61. #include <asm/udbg.h>
  62. #include <asm/hugetlb.h>
  63. #include <asm/livepatch.h>
  64. #include <asm/mmu_context.h>
  65. #include <asm/cpu_has_feature.h>
  66. #include <asm/kasan.h>
  67. #include <asm/mce.h>
  68. #include "setup.h"
  69. #ifdef DEBUG
  70. #define DBG(fmt...) udbg_printf(fmt)
  71. #else
  72. #define DBG(fmt...)
  73. #endif
  74. /* The main machine-dep calls structure
  75. */
  76. struct machdep_calls ppc_md;
  77. EXPORT_SYMBOL(ppc_md);
  78. struct machdep_calls *machine_id;
  79. EXPORT_SYMBOL(machine_id);
  80. int boot_cpuid = -1;
  81. EXPORT_SYMBOL_GPL(boot_cpuid);
  82. int __initdata boot_core_hwid = -1;
  83. #ifdef CONFIG_PPC64
  84. int boot_cpu_hwid = -1;
  85. #endif
  86. /*
  87. * These are used in binfmt_elf.c to put aux entries on the stack
  88. * for each elf executable being started.
  89. */
  90. int dcache_bsize;
  91. int icache_bsize;
  92. /* Variables required to store legacy IO irq routing */
  93. int of_i8042_kbd_irq;
  94. EXPORT_SYMBOL_GPL(of_i8042_kbd_irq);
  95. int of_i8042_aux_irq;
  96. EXPORT_SYMBOL_GPL(of_i8042_aux_irq);
  97. #ifdef __DO_IRQ_CANON
  98. /* XXX should go elsewhere eventually */
  99. int ppc_do_canonicalize_irqs;
  100. EXPORT_SYMBOL(ppc_do_canonicalize_irqs);
  101. #endif
  102. #ifdef CONFIG_CRASH_DUMP
  103. /* This keeps a track of which one is the crashing cpu. */
  104. int crashing_cpu = -1;
  105. #endif
  106. /* also used by kexec */
  107. void machine_shutdown(void)
  108. {
  109. /*
  110. * if fadump is active, cleanup the fadump registration before we
  111. * shutdown.
  112. */
  113. fadump_cleanup();
  114. if (ppc_md.machine_shutdown)
  115. ppc_md.machine_shutdown();
  116. }
  117. static void machine_hang(void)
  118. {
  119. pr_emerg("System Halted, OK to turn off power\n");
  120. local_irq_disable();
  121. while (1)
  122. ;
  123. }
  124. void machine_restart(char *cmd)
  125. {
  126. machine_shutdown();
  127. if (ppc_md.restart)
  128. ppc_md.restart(cmd);
  129. smp_send_stop();
  130. do_kernel_restart(cmd);
  131. mdelay(1000);
  132. machine_hang();
  133. }
  134. void machine_power_off(void)
  135. {
  136. machine_shutdown();
  137. do_kernel_power_off();
  138. smp_send_stop();
  139. machine_hang();
  140. }
  141. /* Used by the G5 thermal driver */
  142. EXPORT_SYMBOL_GPL(machine_power_off);
  143. void (*pm_power_off)(void);
  144. EXPORT_SYMBOL_GPL(pm_power_off);
  145. size_t __must_check arch_get_random_seed_longs(unsigned long *v, size_t max_longs)
  146. {
  147. if (max_longs && ppc_md.get_random_seed && ppc_md.get_random_seed(v))
  148. return 1;
  149. return 0;
  150. }
  151. EXPORT_SYMBOL(arch_get_random_seed_longs);
  152. void machine_halt(void)
  153. {
  154. machine_shutdown();
  155. if (ppc_md.halt)
  156. ppc_md.halt();
  157. smp_send_stop();
  158. machine_hang();
  159. }
  160. #ifdef CONFIG_SMP
  161. DEFINE_PER_CPU(unsigned int, cpu_pvr);
  162. #endif
  163. static void show_cpuinfo_summary(struct seq_file *m)
  164. {
  165. struct device_node *root;
  166. const char *model = NULL;
  167. unsigned long bogosum = 0;
  168. int i;
  169. if (IS_ENABLED(CONFIG_SMP) && IS_ENABLED(CONFIG_PPC32)) {
  170. for_each_online_cpu(i)
  171. bogosum += loops_per_jiffy;
  172. seq_printf(m, "total bogomips\t: %lu.%02lu\n",
  173. bogosum / (500000 / HZ), bogosum / (5000 / HZ) % 100);
  174. }
  175. seq_printf(m, "timebase\t: %lu\n", ppc_tb_freq);
  176. if (ppc_md.name)
  177. seq_printf(m, "platform\t: %s\n", ppc_md.name);
  178. root = of_find_node_by_path("/");
  179. if (root)
  180. model = of_get_property(root, "model", NULL);
  181. if (model)
  182. seq_printf(m, "model\t\t: %s\n", model);
  183. of_node_put(root);
  184. if (ppc_md.show_cpuinfo != NULL)
  185. ppc_md.show_cpuinfo(m);
  186. /* Display the amount of memory */
  187. if (IS_ENABLED(CONFIG_PPC32))
  188. seq_printf(m, "Memory\t\t: %d MB\n",
  189. (unsigned int)(total_memory / (1024 * 1024)));
  190. }
  191. static int show_cpuinfo(struct seq_file *m, void *v)
  192. {
  193. unsigned long cpu_id = (unsigned long)v - 1;
  194. unsigned int pvr;
  195. unsigned long proc_freq;
  196. unsigned short maj;
  197. unsigned short min;
  198. #ifdef CONFIG_SMP
  199. pvr = per_cpu(cpu_pvr, cpu_id);
  200. #else
  201. pvr = mfspr(SPRN_PVR);
  202. #endif
  203. maj = (pvr >> 8) & 0xFF;
  204. min = pvr & 0xFF;
  205. seq_printf(m, "processor\t: %lu\ncpu\t\t: ", cpu_id);
  206. if (cur_cpu_spec->pvr_mask && cur_cpu_spec->cpu_name)
  207. seq_puts(m, cur_cpu_spec->cpu_name);
  208. else
  209. seq_printf(m, "unknown (%08x)", pvr);
  210. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  211. seq_puts(m, ", altivec supported");
  212. seq_putc(m, '\n');
  213. #ifdef CONFIG_TAU
  214. if (cpu_has_feature(CPU_FTR_TAU)) {
  215. if (IS_ENABLED(CONFIG_TAU_AVERAGE)) {
  216. /* more straightforward, but potentially misleading */
  217. seq_printf(m, "temperature \t: %u C (uncalibrated)\n",
  218. cpu_temp(cpu_id));
  219. } else {
  220. /* show the actual temp sensor range */
  221. u32 temp;
  222. temp = cpu_temp_both(cpu_id);
  223. seq_printf(m, "temperature \t: %u-%u C (uncalibrated)\n",
  224. temp & 0xff, temp >> 16);
  225. }
  226. }
  227. #endif /* CONFIG_TAU */
  228. /*
  229. * Platforms that have variable clock rates, should implement
  230. * the method ppc_md.get_proc_freq() that reports the clock
  231. * rate of a given cpu. The rest can use ppc_proc_freq to
  232. * report the clock rate that is same across all cpus.
  233. */
  234. if (ppc_md.get_proc_freq)
  235. proc_freq = ppc_md.get_proc_freq(cpu_id);
  236. else
  237. proc_freq = ppc_proc_freq;
  238. if (proc_freq)
  239. seq_printf(m, "clock\t\t: %lu.%06luMHz\n",
  240. proc_freq / 1000000, proc_freq % 1000000);
  241. /* If we are a Freescale core do a simple check so
  242. * we don't have to keep adding cases in the future */
  243. if (PVR_VER(pvr) & 0x8000) {
  244. switch (PVR_VER(pvr)) {
  245. case 0x8000: /* 7441/7450/7451, Voyager */
  246. case 0x8001: /* 7445/7455, Apollo 6 */
  247. case 0x8002: /* 7447/7457, Apollo 7 */
  248. case 0x8003: /* 7447A, Apollo 7 PM */
  249. case 0x8004: /* 7448, Apollo 8 */
  250. case 0x800c: /* 7410, Nitro */
  251. maj = ((pvr >> 8) & 0xF);
  252. min = PVR_MIN(pvr);
  253. break;
  254. default: /* e500/book-e */
  255. maj = PVR_MAJ(pvr);
  256. min = PVR_MIN(pvr);
  257. break;
  258. }
  259. } else {
  260. switch (PVR_VER(pvr)) {
  261. case 0x1008: /* 740P/750P ?? */
  262. maj = ((pvr >> 8) & 0xFF) - 1;
  263. min = pvr & 0xFF;
  264. break;
  265. case 0x004e: /* POWER9 bits 12-15 give chip type */
  266. case 0x0080: /* POWER10 bit 12 gives SMT8/4 */
  267. maj = (pvr >> 8) & 0x0F;
  268. min = pvr & 0xFF;
  269. break;
  270. default:
  271. maj = (pvr >> 8) & 0xFF;
  272. min = pvr & 0xFF;
  273. break;
  274. }
  275. }
  276. seq_printf(m, "revision\t: %hd.%hd (pvr %04x %04x)\n",
  277. maj, min, PVR_VER(pvr), PVR_REV(pvr));
  278. if (IS_ENABLED(CONFIG_PPC32))
  279. seq_printf(m, "bogomips\t: %lu.%02lu\n", loops_per_jiffy / (500000 / HZ),
  280. (loops_per_jiffy / (5000 / HZ)) % 100);
  281. seq_putc(m, '\n');
  282. /* If this is the last cpu, print the summary */
  283. if (cpumask_next(cpu_id, cpu_online_mask) >= nr_cpu_ids)
  284. show_cpuinfo_summary(m);
  285. return 0;
  286. }
  287. static void *c_start(struct seq_file *m, loff_t *pos)
  288. {
  289. if (*pos == 0) /* just in case, cpu 0 is not the first */
  290. *pos = cpumask_first(cpu_online_mask);
  291. else
  292. *pos = cpumask_next(*pos - 1, cpu_online_mask);
  293. if ((*pos) < nr_cpu_ids)
  294. return (void *)(unsigned long)(*pos + 1);
  295. return NULL;
  296. }
  297. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  298. {
  299. (*pos)++;
  300. return c_start(m, pos);
  301. }
  302. static void c_stop(struct seq_file *m, void *v)
  303. {
  304. }
  305. const struct seq_operations cpuinfo_op = {
  306. .start = c_start,
  307. .next = c_next,
  308. .stop = c_stop,
  309. .show = show_cpuinfo,
  310. };
  311. void __init check_for_initrd(void)
  312. {
  313. #ifdef CONFIG_BLK_DEV_INITRD
  314. DBG(" -> check_for_initrd() initrd_start=0x%lx initrd_end=0x%lx\n",
  315. initrd_start, initrd_end);
  316. /* If we were passed an initrd, set the ROOT_DEV properly if the values
  317. * look sensible. If not, clear initrd reference.
  318. */
  319. if (is_kernel_addr(initrd_start) && is_kernel_addr(initrd_end) &&
  320. initrd_end > initrd_start)
  321. ROOT_DEV = Root_RAM0;
  322. else
  323. initrd_start = initrd_end = 0;
  324. if (initrd_start)
  325. pr_info("Found initrd at 0x%lx:0x%lx\n", initrd_start, initrd_end);
  326. DBG(" <- check_for_initrd()\n");
  327. #endif /* CONFIG_BLK_DEV_INITRD */
  328. }
  329. #ifdef CONFIG_SMP
  330. int threads_per_core, threads_per_subcore, threads_shift __read_mostly;
  331. cpumask_t threads_core_mask __read_mostly;
  332. EXPORT_SYMBOL_GPL(threads_per_core);
  333. EXPORT_SYMBOL_GPL(threads_per_subcore);
  334. EXPORT_SYMBOL_GPL(threads_shift);
  335. EXPORT_SYMBOL_GPL(threads_core_mask);
  336. static void __init cpu_init_thread_core_maps(int tpc)
  337. {
  338. int i;
  339. threads_per_core = tpc;
  340. threads_per_subcore = tpc;
  341. cpumask_clear(&threads_core_mask);
  342. /* This implementation only supports power of 2 number of threads
  343. * for simplicity and performance
  344. */
  345. threads_shift = ilog2(tpc);
  346. BUG_ON(tpc != (1 << threads_shift));
  347. for (i = 0; i < tpc; i++)
  348. cpumask_set_cpu(i, &threads_core_mask);
  349. printk(KERN_INFO "CPU maps initialized for %d thread%s per core\n",
  350. tpc, str_plural(tpc));
  351. printk(KERN_DEBUG " (thread shift is %d)\n", threads_shift);
  352. }
  353. u32 *cpu_to_phys_id = NULL;
  354. static int assign_threads(unsigned int cpu, unsigned int nthreads, bool present,
  355. const __be32 *hw_ids)
  356. {
  357. for (int i = 0; i < nthreads && cpu < nr_cpu_ids; i++) {
  358. __be32 hwid;
  359. hwid = be32_to_cpu(hw_ids[i]);
  360. DBG(" thread %d -> cpu %d (hard id %d)\n", i, cpu, hwid);
  361. set_cpu_present(cpu, present);
  362. set_cpu_possible(cpu, true);
  363. cpu_to_phys_id[cpu] = hwid;
  364. cpu++;
  365. }
  366. return cpu;
  367. }
  368. /**
  369. * setup_cpu_maps - initialize the following cpu maps:
  370. * cpu_possible_mask
  371. * cpu_present_mask
  372. *
  373. * Having the possible map set up early allows us to restrict allocations
  374. * of things like irqstacks to nr_cpu_ids rather than NR_CPUS.
  375. *
  376. * We do not initialize the online map here; cpus set their own bits in
  377. * cpu_online_mask as they come up.
  378. *
  379. * This function is valid only for Open Firmware systems. finish_device_tree
  380. * must be called before using this.
  381. *
  382. * While we're here, we may as well set the "physical" cpu ids in the paca.
  383. *
  384. * NOTE: This must match the parsing done in early_init_dt_scan_cpus.
  385. */
  386. void __init smp_setup_cpu_maps(void)
  387. {
  388. struct device_node *dn;
  389. int cpu = 0;
  390. int nthreads = 1;
  391. DBG("smp_setup_cpu_maps()\n");
  392. cpu_to_phys_id = memblock_alloc(nr_cpu_ids * sizeof(u32),
  393. __alignof__(u32));
  394. if (!cpu_to_phys_id)
  395. panic("%s: Failed to allocate %zu bytes align=0x%zx\n",
  396. __func__, nr_cpu_ids * sizeof(u32), __alignof__(u32));
  397. for_each_node_by_type(dn, "cpu") {
  398. const __be32 *intserv;
  399. __be32 cpu_be;
  400. int len;
  401. DBG(" * %pOF...\n", dn);
  402. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s",
  403. &len);
  404. if (intserv) {
  405. DBG(" ibm,ppc-interrupt-server#s -> %lu threads\n",
  406. (len / sizeof(int)));
  407. } else {
  408. DBG(" no ibm,ppc-interrupt-server#s -> 1 thread\n");
  409. intserv = of_get_property(dn, "reg", &len);
  410. if (!intserv) {
  411. cpu_be = cpu_to_be32(cpu);
  412. /* XXX: what is this? uninitialized?? */
  413. intserv = &cpu_be; /* assume logical == phys */
  414. len = 4;
  415. }
  416. }
  417. nthreads = len / sizeof(int);
  418. bool avail = of_device_is_available(dn);
  419. if (!avail)
  420. avail = !of_property_match_string(dn,
  421. "enable-method", "spin-table");
  422. if (boot_core_hwid >= 0) {
  423. if (cpu == 0) {
  424. pr_info("Skipping CPU node %pOF to allow for boot core.\n", dn);
  425. cpu = nthreads;
  426. continue;
  427. }
  428. if (be32_to_cpu(intserv[0]) == boot_core_hwid) {
  429. pr_info("Renumbered boot core %pOF to logical 0\n", dn);
  430. assign_threads(0, nthreads, avail, intserv);
  431. of_node_put(dn);
  432. break;
  433. }
  434. } else if (cpu >= nr_cpu_ids) {
  435. of_node_put(dn);
  436. break;
  437. }
  438. if (cpu < nr_cpu_ids)
  439. cpu = assign_threads(cpu, nthreads, avail, intserv);
  440. }
  441. /* If no SMT supported, nthreads is forced to 1 */
  442. if (!cpu_has_feature(CPU_FTR_SMT)) {
  443. DBG(" SMT disabled ! nthreads forced to 1\n");
  444. nthreads = 1;
  445. }
  446. #ifdef CONFIG_PPC64
  447. /*
  448. * On pSeries LPAR, we need to know how many cpus
  449. * could possibly be added to this partition.
  450. */
  451. if (firmware_has_feature(FW_FEATURE_LPAR) &&
  452. (dn = of_find_node_by_path("/rtas"))) {
  453. int num_addr_cell, num_size_cell, maxcpus;
  454. const __be32 *ireg;
  455. num_addr_cell = of_n_addr_cells(dn);
  456. num_size_cell = of_n_size_cells(dn);
  457. ireg = of_get_property(dn, "ibm,lrdr-capacity", NULL);
  458. if (!ireg)
  459. goto out;
  460. maxcpus = be32_to_cpup(ireg + num_addr_cell + num_size_cell);
  461. /* Double maxcpus for processors which have SMT capability */
  462. if (cpu_has_feature(CPU_FTR_SMT))
  463. maxcpus *= nthreads;
  464. if (maxcpus > nr_cpu_ids) {
  465. printk(KERN_WARNING
  466. "Partition configured for %d cpus, "
  467. "operating system maximum is %u.\n",
  468. maxcpus, nr_cpu_ids);
  469. maxcpus = nr_cpu_ids;
  470. } else
  471. printk(KERN_INFO "Partition configured for %d cpus.\n",
  472. maxcpus);
  473. for (cpu = 0; cpu < maxcpus; cpu++)
  474. set_cpu_possible(cpu, true);
  475. out:
  476. of_node_put(dn);
  477. }
  478. vdso_data->processorCount = num_present_cpus();
  479. #endif /* CONFIG_PPC64 */
  480. /* Initialize CPU <=> thread mapping/
  481. *
  482. * WARNING: We assume that the number of threads is the same for
  483. * every CPU in the system. If that is not the case, then some code
  484. * here will have to be reworked
  485. */
  486. cpu_init_thread_core_maps(nthreads);
  487. /* Now that possible cpus are set, set nr_cpu_ids for later use */
  488. setup_nr_cpu_ids();
  489. free_unused_pacas();
  490. }
  491. #endif /* CONFIG_SMP */
  492. #ifdef CONFIG_PCSPKR_PLATFORM
  493. static __init int add_pcspkr(void)
  494. {
  495. struct device_node *np;
  496. struct platform_device *pd;
  497. int ret;
  498. np = of_find_compatible_node(NULL, NULL, "pnpPNP,100");
  499. of_node_put(np);
  500. if (!np)
  501. return -ENODEV;
  502. pd = platform_device_alloc("pcspkr", -1);
  503. if (!pd)
  504. return -ENOMEM;
  505. ret = platform_device_add(pd);
  506. if (ret)
  507. platform_device_put(pd);
  508. return ret;
  509. }
  510. device_initcall(add_pcspkr);
  511. #endif /* CONFIG_PCSPKR_PLATFORM */
  512. static char ppc_hw_desc_buf[128] __initdata;
  513. struct seq_buf ppc_hw_desc __initdata = {
  514. .buffer = ppc_hw_desc_buf,
  515. .size = sizeof(ppc_hw_desc_buf),
  516. .len = 0,
  517. };
  518. static __init void probe_machine(void)
  519. {
  520. extern struct machdep_calls __machine_desc_start;
  521. extern struct machdep_calls __machine_desc_end;
  522. unsigned int i;
  523. /*
  524. * Iterate all ppc_md structures until we find the proper
  525. * one for the current machine type
  526. */
  527. DBG("Probing machine type ...\n");
  528. /*
  529. * Check ppc_md is empty, if not we have a bug, ie, we setup an
  530. * entry before probe_machine() which will be overwritten
  531. */
  532. for (i = 0; i < (sizeof(ppc_md) / sizeof(void *)); i++) {
  533. if (((void **)&ppc_md)[i]) {
  534. printk(KERN_ERR "Entry %d in ppc_md non empty before"
  535. " machine probe !\n", i);
  536. }
  537. }
  538. for (machine_id = &__machine_desc_start;
  539. machine_id < &__machine_desc_end;
  540. machine_id++) {
  541. DBG(" %s ...\n", machine_id->name);
  542. if (machine_id->compatible && !of_machine_is_compatible(machine_id->compatible))
  543. continue;
  544. if (machine_id->compatibles && !of_machine_compatible_match(machine_id->compatibles))
  545. continue;
  546. memcpy(&ppc_md, machine_id, sizeof(struct machdep_calls));
  547. if (ppc_md.probe && !ppc_md.probe())
  548. continue;
  549. DBG(" %s match !\n", machine_id->name);
  550. break;
  551. }
  552. /* What can we do if we didn't find ? */
  553. if (machine_id >= &__machine_desc_end) {
  554. pr_err("No suitable machine description found !\n");
  555. for (;;);
  556. }
  557. // Append the machine name to other info we've gathered
  558. seq_buf_puts(&ppc_hw_desc, ppc_md.name);
  559. // Set the generic hardware description shown in oopses
  560. dump_stack_set_arch_desc(ppc_hw_desc.buffer);
  561. pr_info("Hardware name: %s\n", ppc_hw_desc.buffer);
  562. }
  563. /* Match a class of boards, not a specific device configuration. */
  564. int check_legacy_ioport(unsigned long base_port)
  565. {
  566. struct device_node *parent, *np = NULL;
  567. int ret = -ENODEV;
  568. switch(base_port) {
  569. case I8042_DATA_REG:
  570. if (!(np = of_find_compatible_node(NULL, NULL, "pnpPNP,303")))
  571. np = of_find_compatible_node(NULL, NULL, "pnpPNP,f03");
  572. if (np) {
  573. parent = of_get_parent(np);
  574. of_i8042_kbd_irq = irq_of_parse_and_map(parent, 0);
  575. if (!of_i8042_kbd_irq)
  576. of_i8042_kbd_irq = 1;
  577. of_i8042_aux_irq = irq_of_parse_and_map(parent, 1);
  578. if (!of_i8042_aux_irq)
  579. of_i8042_aux_irq = 12;
  580. of_node_put(np);
  581. np = parent;
  582. break;
  583. }
  584. np = of_find_node_by_type(NULL, "8042");
  585. /* Pegasos has no device_type on its 8042 node, look for the
  586. * name instead */
  587. if (!np)
  588. np = of_find_node_by_name(NULL, "8042");
  589. if (np) {
  590. of_i8042_kbd_irq = 1;
  591. of_i8042_aux_irq = 12;
  592. }
  593. break;
  594. case FDC_BASE: /* FDC1 */
  595. np = of_find_node_by_type(NULL, "fdc");
  596. break;
  597. default:
  598. /* ipmi is supposed to fail here */
  599. break;
  600. }
  601. if (!np)
  602. return ret;
  603. parent = of_get_parent(np);
  604. if (parent) {
  605. if (of_node_is_type(parent, "isa"))
  606. ret = 0;
  607. of_node_put(parent);
  608. }
  609. of_node_put(np);
  610. return ret;
  611. }
  612. EXPORT_SYMBOL(check_legacy_ioport);
  613. /*
  614. * Panic notifiers setup
  615. *
  616. * We have 3 notifiers for powerpc, each one from a different "nature":
  617. *
  618. * - ppc_panic_fadump_handler() is a hypervisor notifier, which hard-disables
  619. * IRQs and deal with the Firmware-Assisted dump, when it is configured;
  620. * should run early in the panic path.
  621. *
  622. * - dump_kernel_offset() is an informative notifier, just showing the KASLR
  623. * offset if we have RANDOMIZE_BASE set.
  624. *
  625. * - ppc_panic_platform_handler() is a low-level handler that's registered
  626. * only if the platform wishes to perform final actions in the panic path,
  627. * hence it should run late and might not even return. Currently, only
  628. * pseries and ps3 platforms register callbacks.
  629. */
  630. static int ppc_panic_fadump_handler(struct notifier_block *this,
  631. unsigned long event, void *ptr)
  632. {
  633. /*
  634. * panic does a local_irq_disable, but we really
  635. * want interrupts to be hard disabled.
  636. */
  637. hard_irq_disable();
  638. /*
  639. * If firmware-assisted dump has been registered then trigger
  640. * its callback and let the firmware handles everything else.
  641. */
  642. crash_fadump(NULL, ptr);
  643. return NOTIFY_DONE;
  644. }
  645. static int dump_kernel_offset(struct notifier_block *self, unsigned long v,
  646. void *p)
  647. {
  648. pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
  649. kaslr_offset(), KERNELBASE);
  650. return NOTIFY_DONE;
  651. }
  652. static int ppc_panic_platform_handler(struct notifier_block *this,
  653. unsigned long event, void *ptr)
  654. {
  655. /*
  656. * This handler is only registered if we have a panic callback
  657. * on ppc_md, hence NULL check is not needed.
  658. * Also, it may not return, so it runs really late on panic path.
  659. */
  660. ppc_md.panic(ptr);
  661. return NOTIFY_DONE;
  662. }
  663. static struct notifier_block ppc_fadump_block = {
  664. .notifier_call = ppc_panic_fadump_handler,
  665. .priority = INT_MAX, /* run early, to notify the firmware ASAP */
  666. };
  667. static struct notifier_block kernel_offset_notifier = {
  668. .notifier_call = dump_kernel_offset,
  669. };
  670. static struct notifier_block ppc_panic_block = {
  671. .notifier_call = ppc_panic_platform_handler,
  672. .priority = INT_MIN, /* may not return; must be done last */
  673. };
  674. void __init setup_panic(void)
  675. {
  676. /* Hard-disables IRQs + deal with FW-assisted dump (fadump) */
  677. atomic_notifier_chain_register(&panic_notifier_list,
  678. &ppc_fadump_block);
  679. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_offset() > 0)
  680. atomic_notifier_chain_register(&panic_notifier_list,
  681. &kernel_offset_notifier);
  682. /* Low-level platform-specific routines that should run on panic */
  683. if (ppc_md.panic)
  684. atomic_notifier_chain_register(&panic_notifier_list,
  685. &ppc_panic_block);
  686. }
  687. #ifdef CONFIG_CHECK_CACHE_COHERENCY
  688. /*
  689. * For platforms that have configurable cache-coherency. This function
  690. * checks that the cache coherency setting of the kernel matches the setting
  691. * left by the firmware, as indicated in the device tree. Since a mismatch
  692. * will eventually result in DMA failures, we print * and error and call
  693. * BUG() in that case.
  694. */
  695. #define KERNEL_COHERENCY (!IS_ENABLED(CONFIG_NOT_COHERENT_CACHE))
  696. static int __init check_cache_coherency(void)
  697. {
  698. struct device_node *np;
  699. const void *prop;
  700. bool devtree_coherency;
  701. np = of_find_node_by_path("/");
  702. prop = of_get_property(np, "coherency-off", NULL);
  703. of_node_put(np);
  704. devtree_coherency = prop ? false : true;
  705. if (devtree_coherency != KERNEL_COHERENCY) {
  706. printk(KERN_ERR
  707. "kernel coherency:%s != device tree_coherency:%s\n",
  708. KERNEL_COHERENCY ? "on" : "off",
  709. devtree_coherency ? "on" : "off");
  710. BUG();
  711. }
  712. return 0;
  713. }
  714. late_initcall(check_cache_coherency);
  715. #endif /* CONFIG_CHECK_CACHE_COHERENCY */
  716. void ppc_printk_progress(char *s, unsigned short hex)
  717. {
  718. pr_info("%s\n", s);
  719. }
  720. static __init void print_system_info(void)
  721. {
  722. pr_info("-----------------------------------------------------\n");
  723. pr_info("phys_mem_size = 0x%llx\n",
  724. (unsigned long long)memblock_phys_mem_size());
  725. pr_info("dcache_bsize = 0x%x\n", dcache_bsize);
  726. pr_info("icache_bsize = 0x%x\n", icache_bsize);
  727. pr_info("cpu_features = 0x%016lx\n", cur_cpu_spec->cpu_features);
  728. pr_info(" possible = 0x%016lx\n",
  729. (unsigned long)CPU_FTRS_POSSIBLE);
  730. pr_info(" always = 0x%016lx\n",
  731. (unsigned long)CPU_FTRS_ALWAYS);
  732. pr_info("cpu_user_features = 0x%08x 0x%08x\n",
  733. cur_cpu_spec->cpu_user_features,
  734. cur_cpu_spec->cpu_user_features2);
  735. pr_info("mmu_features = 0x%08x\n", cur_cpu_spec->mmu_features);
  736. #ifdef CONFIG_PPC64
  737. pr_info("firmware_features = 0x%016lx\n", powerpc_firmware_features);
  738. #ifdef CONFIG_PPC_BOOK3S
  739. pr_info("vmalloc start = 0x%lx\n", KERN_VIRT_START);
  740. pr_info("IO start = 0x%lx\n", KERN_IO_START);
  741. pr_info("vmemmap start = 0x%lx\n", (unsigned long)vmemmap);
  742. #endif
  743. #endif
  744. if (!early_radix_enabled())
  745. print_system_hash_info();
  746. if (PHYSICAL_START > 0)
  747. pr_info("physical_start = 0x%llx\n",
  748. (unsigned long long)PHYSICAL_START);
  749. pr_info("-----------------------------------------------------\n");
  750. }
  751. #ifdef CONFIG_SMP
  752. static void __init smp_setup_pacas(void)
  753. {
  754. int cpu;
  755. for_each_possible_cpu(cpu) {
  756. if (cpu == smp_processor_id())
  757. continue;
  758. allocate_paca(cpu);
  759. set_hard_smp_processor_id(cpu, cpu_to_phys_id[cpu]);
  760. }
  761. memblock_free(cpu_to_phys_id, nr_cpu_ids * sizeof(u32));
  762. cpu_to_phys_id = NULL;
  763. }
  764. #endif
  765. /*
  766. * Called into from start_kernel this initializes memblock, which is used
  767. * to manage page allocation until mem_init is called.
  768. */
  769. void __init setup_arch(char **cmdline_p)
  770. {
  771. kasan_init();
  772. *cmdline_p = boot_command_line;
  773. /* Set a half-reasonable default so udelay does something sensible */
  774. loops_per_jiffy = 500000000 / HZ;
  775. /* Unflatten the device-tree passed by prom_init or kexec */
  776. unflatten_device_tree();
  777. /*
  778. * Initialize cache line/block info from device-tree (on ppc64) or
  779. * just cputable (on ppc32).
  780. */
  781. initialize_cache_info();
  782. /* Initialize RTAS if available. */
  783. rtas_initialize();
  784. /* Check if we have an initrd provided via the device-tree. */
  785. check_for_initrd();
  786. /* Probe the machine type, establish ppc_md. */
  787. probe_machine();
  788. /* Setup panic notifier if requested by the platform. */
  789. setup_panic();
  790. /*
  791. * Configure ppc_md.power_save (ppc32 only, 64-bit machines do
  792. * it from their respective probe() function.
  793. */
  794. setup_power_save();
  795. /* Discover standard serial ports. */
  796. find_legacy_serial_ports();
  797. /* Register early console with the printk subsystem. */
  798. register_early_udbg_console();
  799. /* Setup the various CPU maps based on the device-tree. */
  800. smp_setup_cpu_maps();
  801. /* Initialize xmon. */
  802. xmon_setup();
  803. /* Check the SMT related command line arguments (ppc64). */
  804. check_smt_enabled();
  805. /* Parse memory topology */
  806. mem_topology_setup();
  807. /* Set max_mapnr before paging_init() */
  808. set_max_mapnr(max_pfn);
  809. high_memory = (void *)__va(max_low_pfn * PAGE_SIZE);
  810. /*
  811. * Release secondary cpus out of their spinloops at 0x60 now that
  812. * we can map physical -> logical CPU ids.
  813. *
  814. * Freescale Book3e parts spin in a loop provided by firmware,
  815. * so smp_release_cpus() does nothing for them.
  816. */
  817. #ifdef CONFIG_SMP
  818. smp_setup_pacas();
  819. /* On BookE, setup per-core TLB data structures. */
  820. setup_tlb_core_data();
  821. #endif
  822. /* Print various info about the machine that has been gathered so far. */
  823. print_system_info();
  824. klp_init_thread_info(&init_task);
  825. setup_initial_init_mm(_stext, _etext, _edata, _end);
  826. /* sched_init() does the mmgrab(&init_mm) for the primary CPU */
  827. VM_WARN_ON(cpumask_test_cpu(smp_processor_id(), mm_cpumask(&init_mm)));
  828. cpumask_set_cpu(smp_processor_id(), mm_cpumask(&init_mm));
  829. inc_mm_active_cpus(&init_mm);
  830. mm_iommu_init(&init_mm);
  831. irqstack_early_init();
  832. exc_lvl_early_init();
  833. emergency_stack_init();
  834. mce_init();
  835. smp_release_cpus();
  836. initmem_init();
  837. /*
  838. * Reserve large chunks of memory for use by CMA for fadump, KVM and
  839. * hugetlb. These must be called after initmem_init(), so that
  840. * pageblock_order is initialised.
  841. */
  842. fadump_cma_init();
  843. kvm_cma_reserve();
  844. gigantic_hugetlb_cma_reserve();
  845. early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
  846. if (ppc_md.setup_arch)
  847. ppc_md.setup_arch();
  848. setup_barrier_nospec();
  849. setup_spectre_v2();
  850. paging_init();
  851. /* Initialize the MMU context management stuff. */
  852. mmu_context_init();
  853. /* Interrupt code needs to be 64K-aligned. */
  854. if (IS_ENABLED(CONFIG_PPC64) && (unsigned long)_stext & 0xffff)
  855. panic("Kernelbase not 64K-aligned (0x%lx)!\n",
  856. (unsigned long)_stext);
  857. }