setup.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*
  2. * 64-bit pSeries and RS/6000 setup code.
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Adapted from 'alpha' version by Gary Thomas
  6. * Modified by Cort Dougan (cort@cs.nmt.edu)
  7. * Modified by PPC64 Team, IBM Corp
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. /*
  15. * bootup setup stuff..
  16. */
  17. #include <linux/cpu.h>
  18. #include <linux/errno.h>
  19. #include <linux/sched.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/stddef.h>
  23. #include <linux/unistd.h>
  24. #include <linux/user.h>
  25. #include <linux/tty.h>
  26. #include <linux/major.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/reboot.h>
  29. #include <linux/init.h>
  30. #include <linux/ioport.h>
  31. #include <linux/console.h>
  32. #include <linux/pci.h>
  33. #include <linux/utsname.h>
  34. #include <linux/adb.h>
  35. #include <linux/export.h>
  36. #include <linux/delay.h>
  37. #include <linux/irq.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/root_dev.h>
  40. #include <linux/of.h>
  41. #include <linux/of_pci.h>
  42. #include <linux/memblock.h>
  43. #include <asm/mmu.h>
  44. #include <asm/processor.h>
  45. #include <asm/io.h>
  46. #include <asm/pgtable.h>
  47. #include <asm/prom.h>
  48. #include <asm/rtas.h>
  49. #include <asm/pci-bridge.h>
  50. #include <asm/iommu.h>
  51. #include <asm/dma.h>
  52. #include <asm/machdep.h>
  53. #include <asm/irq.h>
  54. #include <asm/time.h>
  55. #include <asm/nvram.h>
  56. #include <asm/pmc.h>
  57. #include <asm/xics.h>
  58. #include <asm/xive.h>
  59. #include <asm/ppc-pci.h>
  60. #include <asm/i8259.h>
  61. #include <asm/udbg.h>
  62. #include <asm/smp.h>
  63. #include <asm/firmware.h>
  64. #include <asm/eeh.h>
  65. #include <asm/reg.h>
  66. #include <asm/plpar_wrappers.h>
  67. #include <asm/kexec.h>
  68. #include <asm/isa-bridge.h>
  69. #include <asm/security_features.h>
  70. #include <asm/asm-const.h>
  71. #include "pseries.h"
  72. #include "../../../../drivers/pci/pci.h"
  73. int CMO_PrPSP = -1;
  74. int CMO_SecPSP = -1;
  75. unsigned long CMO_PageSize = (ASM_CONST(1) << IOMMU_PAGE_SHIFT_4K);
  76. EXPORT_SYMBOL(CMO_PageSize);
  77. int fwnmi_active; /* TRUE if an FWNMI handler is present */
  78. static void pSeries_show_cpuinfo(struct seq_file *m)
  79. {
  80. struct device_node *root;
  81. const char *model = "";
  82. root = of_find_node_by_path("/");
  83. if (root)
  84. model = of_get_property(root, "model", NULL);
  85. seq_printf(m, "machine\t\t: CHRP %s\n", model);
  86. of_node_put(root);
  87. if (radix_enabled())
  88. seq_printf(m, "MMU\t\t: Radix\n");
  89. else
  90. seq_printf(m, "MMU\t\t: Hash\n");
  91. }
  92. /* Initialize firmware assisted non-maskable interrupts if
  93. * the firmware supports this feature.
  94. */
  95. static void __init fwnmi_init(void)
  96. {
  97. unsigned long system_reset_addr, machine_check_addr;
  98. u8 *mce_data_buf;
  99. unsigned int i;
  100. int nr_cpus = num_possible_cpus();
  101. #ifdef CONFIG_PPC_BOOK3S_64
  102. struct slb_entry *slb_ptr;
  103. size_t size;
  104. #endif
  105. int ibm_nmi_register = rtas_token("ibm,nmi-register");
  106. if (ibm_nmi_register == RTAS_UNKNOWN_SERVICE)
  107. return;
  108. /* If the kernel's not linked at zero we point the firmware at low
  109. * addresses anyway, and use a trampoline to get to the real code. */
  110. system_reset_addr = __pa(system_reset_fwnmi) - PHYSICAL_START;
  111. machine_check_addr = __pa(machine_check_fwnmi) - PHYSICAL_START;
  112. if (0 == rtas_call(ibm_nmi_register, 2, 1, NULL, system_reset_addr,
  113. machine_check_addr))
  114. fwnmi_active = 1;
  115. /*
  116. * Allocate a chunk for per cpu buffer to hold rtas errorlog.
  117. * It will be used in real mode mce handler, hence it needs to be
  118. * below RMA.
  119. */
  120. mce_data_buf = __va(memblock_alloc_base(RTAS_ERROR_LOG_MAX * nr_cpus,
  121. RTAS_ERROR_LOG_MAX, ppc64_rma_size));
  122. for_each_possible_cpu(i) {
  123. paca_ptrs[i]->mce_data_buf = mce_data_buf +
  124. (RTAS_ERROR_LOG_MAX * i);
  125. }
  126. #ifdef CONFIG_PPC_BOOK3S_64
  127. /* Allocate per cpu slb area to save old slb contents during MCE */
  128. size = sizeof(struct slb_entry) * mmu_slb_size * nr_cpus;
  129. slb_ptr = __va(memblock_alloc_base(size, sizeof(struct slb_entry),
  130. ppc64_rma_size));
  131. for_each_possible_cpu(i)
  132. paca_ptrs[i]->mce_faulty_slbs = slb_ptr + (mmu_slb_size * i);
  133. #endif
  134. }
  135. static void pseries_8259_cascade(struct irq_desc *desc)
  136. {
  137. struct irq_chip *chip = irq_desc_get_chip(desc);
  138. unsigned int cascade_irq = i8259_irq();
  139. if (cascade_irq)
  140. generic_handle_irq(cascade_irq);
  141. chip->irq_eoi(&desc->irq_data);
  142. }
  143. static void __init pseries_setup_i8259_cascade(void)
  144. {
  145. struct device_node *np, *old, *found = NULL;
  146. unsigned int cascade;
  147. const u32 *addrp;
  148. unsigned long intack = 0;
  149. int naddr;
  150. for_each_node_by_type(np, "interrupt-controller") {
  151. if (of_device_is_compatible(np, "chrp,iic")) {
  152. found = np;
  153. break;
  154. }
  155. }
  156. if (found == NULL) {
  157. printk(KERN_DEBUG "pic: no ISA interrupt controller\n");
  158. return;
  159. }
  160. cascade = irq_of_parse_and_map(found, 0);
  161. if (!cascade) {
  162. printk(KERN_ERR "pic: failed to map cascade interrupt");
  163. return;
  164. }
  165. pr_debug("pic: cascade mapped to irq %d\n", cascade);
  166. for (old = of_node_get(found); old != NULL ; old = np) {
  167. np = of_get_parent(old);
  168. of_node_put(old);
  169. if (np == NULL)
  170. break;
  171. if (strcmp(np->name, "pci") != 0)
  172. continue;
  173. addrp = of_get_property(np, "8259-interrupt-acknowledge", NULL);
  174. if (addrp == NULL)
  175. continue;
  176. naddr = of_n_addr_cells(np);
  177. intack = addrp[naddr-1];
  178. if (naddr > 1)
  179. intack |= ((unsigned long)addrp[naddr-2]) << 32;
  180. }
  181. if (intack)
  182. printk(KERN_DEBUG "pic: PCI 8259 intack at 0x%016lx\n", intack);
  183. i8259_init(found, intack);
  184. of_node_put(found);
  185. irq_set_chained_handler(cascade, pseries_8259_cascade);
  186. }
  187. static void __init pseries_init_irq(void)
  188. {
  189. /* Try using a XIVE if available, otherwise use a XICS */
  190. if (!xive_spapr_init()) {
  191. xics_init();
  192. pseries_setup_i8259_cascade();
  193. }
  194. }
  195. static void pseries_lpar_enable_pmcs(void)
  196. {
  197. unsigned long set, reset;
  198. set = 1UL << 63;
  199. reset = 0;
  200. plpar_hcall_norets(H_PERFMON, set, reset);
  201. }
  202. static int pci_dn_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *data)
  203. {
  204. struct of_reconfig_data *rd = data;
  205. struct device_node *parent, *np = rd->dn;
  206. struct pci_dn *pdn;
  207. int err = NOTIFY_OK;
  208. switch (action) {
  209. case OF_RECONFIG_ATTACH_NODE:
  210. parent = of_get_parent(np);
  211. pdn = parent ? PCI_DN(parent) : NULL;
  212. if (pdn)
  213. pci_add_device_node_info(pdn->phb, np);
  214. of_node_put(parent);
  215. break;
  216. case OF_RECONFIG_DETACH_NODE:
  217. pdn = PCI_DN(np);
  218. if (pdn)
  219. list_del(&pdn->list);
  220. break;
  221. default:
  222. err = NOTIFY_DONE;
  223. break;
  224. }
  225. return err;
  226. }
  227. static struct notifier_block pci_dn_reconfig_nb = {
  228. .notifier_call = pci_dn_reconfig_notifier,
  229. };
  230. struct kmem_cache *dtl_cache;
  231. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  232. /*
  233. * Allocate space for the dispatch trace log for all possible cpus
  234. * and register the buffers with the hypervisor. This is used for
  235. * computing time stolen by the hypervisor.
  236. */
  237. static int alloc_dispatch_logs(void)
  238. {
  239. int cpu, ret;
  240. struct paca_struct *pp;
  241. struct dtl_entry *dtl;
  242. if (!firmware_has_feature(FW_FEATURE_SPLPAR))
  243. return 0;
  244. if (!dtl_cache)
  245. return 0;
  246. for_each_possible_cpu(cpu) {
  247. pp = paca_ptrs[cpu];
  248. dtl = kmem_cache_alloc(dtl_cache, GFP_KERNEL);
  249. if (!dtl) {
  250. pr_warn("Failed to allocate dispatch trace log for cpu %d\n",
  251. cpu);
  252. pr_warn("Stolen time statistics will be unreliable\n");
  253. break;
  254. }
  255. pp->dtl_ridx = 0;
  256. pp->dispatch_log = dtl;
  257. pp->dispatch_log_end = dtl + N_DISPATCH_LOG;
  258. pp->dtl_curr = dtl;
  259. }
  260. /* Register the DTL for the current (boot) cpu */
  261. dtl = get_paca()->dispatch_log;
  262. get_paca()->dtl_ridx = 0;
  263. get_paca()->dtl_curr = dtl;
  264. get_paca()->lppaca_ptr->dtl_idx = 0;
  265. /* hypervisor reads buffer length from this field */
  266. dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES);
  267. ret = register_dtl(hard_smp_processor_id(), __pa(dtl));
  268. if (ret)
  269. pr_err("WARNING: DTL registration of cpu %d (hw %d) failed "
  270. "with %d\n", smp_processor_id(),
  271. hard_smp_processor_id(), ret);
  272. get_paca()->lppaca_ptr->dtl_enable_mask = 2;
  273. return 0;
  274. }
  275. #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  276. static inline int alloc_dispatch_logs(void)
  277. {
  278. return 0;
  279. }
  280. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  281. static int alloc_dispatch_log_kmem_cache(void)
  282. {
  283. dtl_cache = kmem_cache_create("dtl", DISPATCH_LOG_BYTES,
  284. DISPATCH_LOG_BYTES, 0, NULL);
  285. if (!dtl_cache) {
  286. pr_warn("Failed to create dispatch trace log buffer cache\n");
  287. pr_warn("Stolen time statistics will be unreliable\n");
  288. return 0;
  289. }
  290. return alloc_dispatch_logs();
  291. }
  292. machine_early_initcall(pseries, alloc_dispatch_log_kmem_cache);
  293. static void pseries_lpar_idle(void)
  294. {
  295. /*
  296. * Default handler to go into low thread priority and possibly
  297. * low power mode by ceding processor to hypervisor
  298. */
  299. /* Indicate to hypervisor that we are idle. */
  300. get_lppaca()->idle = 1;
  301. /*
  302. * Yield the processor to the hypervisor. We return if
  303. * an external interrupt occurs (which are driven prior
  304. * to returning here) or if a prod occurs from another
  305. * processor. When returning here, external interrupts
  306. * are enabled.
  307. */
  308. cede_processor();
  309. get_lppaca()->idle = 0;
  310. }
  311. /*
  312. * Enable relocation on during exceptions. This has partition wide scope and
  313. * may take a while to complete, if it takes longer than one second we will
  314. * just give up rather than wasting any more time on this - if that turns out
  315. * to ever be a problem in practice we can move this into a kernel thread to
  316. * finish off the process later in boot.
  317. */
  318. void pseries_enable_reloc_on_exc(void)
  319. {
  320. long rc;
  321. unsigned int delay, total_delay = 0;
  322. while (1) {
  323. rc = enable_reloc_on_exceptions();
  324. if (!H_IS_LONG_BUSY(rc)) {
  325. if (rc == H_P2) {
  326. pr_info("Relocation on exceptions not"
  327. " supported\n");
  328. } else if (rc != H_SUCCESS) {
  329. pr_warn("Unable to enable relocation"
  330. " on exceptions: %ld\n", rc);
  331. }
  332. break;
  333. }
  334. delay = get_longbusy_msecs(rc);
  335. total_delay += delay;
  336. if (total_delay > 1000) {
  337. pr_warn("Warning: Giving up waiting to enable "
  338. "relocation on exceptions (%u msec)!\n",
  339. total_delay);
  340. return;
  341. }
  342. mdelay(delay);
  343. }
  344. }
  345. EXPORT_SYMBOL(pseries_enable_reloc_on_exc);
  346. void pseries_disable_reloc_on_exc(void)
  347. {
  348. long rc;
  349. while (1) {
  350. rc = disable_reloc_on_exceptions();
  351. if (!H_IS_LONG_BUSY(rc))
  352. break;
  353. mdelay(get_longbusy_msecs(rc));
  354. }
  355. if (rc != H_SUCCESS)
  356. pr_warn("Warning: Failed to disable relocation on exceptions: %ld\n",
  357. rc);
  358. }
  359. EXPORT_SYMBOL(pseries_disable_reloc_on_exc);
  360. #ifdef CONFIG_KEXEC_CORE
  361. static void pSeries_machine_kexec(struct kimage *image)
  362. {
  363. if (firmware_has_feature(FW_FEATURE_SET_MODE))
  364. pseries_disable_reloc_on_exc();
  365. default_machine_kexec(image);
  366. }
  367. #endif
  368. #ifdef __LITTLE_ENDIAN__
  369. void pseries_big_endian_exceptions(void)
  370. {
  371. long rc;
  372. while (1) {
  373. rc = enable_big_endian_exceptions();
  374. if (!H_IS_LONG_BUSY(rc))
  375. break;
  376. mdelay(get_longbusy_msecs(rc));
  377. }
  378. /*
  379. * At this point it is unlikely panic() will get anything
  380. * out to the user, since this is called very late in kexec
  381. * but at least this will stop us from continuing on further
  382. * and creating an even more difficult to debug situation.
  383. *
  384. * There is a known problem when kdump'ing, if cpus are offline
  385. * the above call will fail. Rather than panicking again, keep
  386. * going and hope the kdump kernel is also little endian, which
  387. * it usually is.
  388. */
  389. if (rc && !kdump_in_progress())
  390. panic("Could not enable big endian exceptions");
  391. }
  392. void pseries_little_endian_exceptions(void)
  393. {
  394. long rc;
  395. while (1) {
  396. rc = enable_little_endian_exceptions();
  397. if (!H_IS_LONG_BUSY(rc))
  398. break;
  399. mdelay(get_longbusy_msecs(rc));
  400. }
  401. if (rc) {
  402. ppc_md.progress("H_SET_MODE LE exception fail", 0);
  403. panic("Could not enable little endian exceptions");
  404. }
  405. }
  406. #endif
  407. static void __init find_and_init_phbs(void)
  408. {
  409. struct device_node *node;
  410. struct pci_controller *phb;
  411. struct device_node *root = of_find_node_by_path("/");
  412. for_each_child_of_node(root, node) {
  413. if (node->type == NULL || (strcmp(node->type, "pci") != 0 &&
  414. strcmp(node->type, "pciex") != 0))
  415. continue;
  416. phb = pcibios_alloc_controller(node);
  417. if (!phb)
  418. continue;
  419. rtas_setup_phb(phb);
  420. pci_process_bridge_OF_ranges(phb, node, 0);
  421. isa_bridge_find_early(phb);
  422. phb->controller_ops = pseries_pci_controller_ops;
  423. }
  424. of_node_put(root);
  425. /*
  426. * PCI_PROBE_ONLY and PCI_REASSIGN_ALL_BUS can be set via properties
  427. * in chosen.
  428. */
  429. of_pci_check_probe_only();
  430. }
  431. static void init_cpu_char_feature_flags(struct h_cpu_char_result *result)
  432. {
  433. /*
  434. * The features below are disabled by default, so we instead look to see
  435. * if firmware has *enabled* them, and set them if so.
  436. */
  437. if (result->character & H_CPU_CHAR_SPEC_BAR_ORI31)
  438. security_ftr_set(SEC_FTR_SPEC_BAR_ORI31);
  439. if (result->character & H_CPU_CHAR_BCCTRL_SERIALISED)
  440. security_ftr_set(SEC_FTR_BCCTRL_SERIALISED);
  441. if (result->character & H_CPU_CHAR_L1D_FLUSH_ORI30)
  442. security_ftr_set(SEC_FTR_L1D_FLUSH_ORI30);
  443. if (result->character & H_CPU_CHAR_L1D_FLUSH_TRIG2)
  444. security_ftr_set(SEC_FTR_L1D_FLUSH_TRIG2);
  445. if (result->character & H_CPU_CHAR_L1D_THREAD_PRIV)
  446. security_ftr_set(SEC_FTR_L1D_THREAD_PRIV);
  447. if (result->character & H_CPU_CHAR_COUNT_CACHE_DISABLED)
  448. security_ftr_set(SEC_FTR_COUNT_CACHE_DISABLED);
  449. if (result->character & H_CPU_CHAR_BCCTR_FLUSH_ASSIST)
  450. security_ftr_set(SEC_FTR_BCCTR_FLUSH_ASSIST);
  451. if (result->behaviour & H_CPU_BEHAV_FLUSH_COUNT_CACHE)
  452. security_ftr_set(SEC_FTR_FLUSH_COUNT_CACHE);
  453. /*
  454. * The features below are enabled by default, so we instead look to see
  455. * if firmware has *disabled* them, and clear them if so.
  456. */
  457. if (!(result->behaviour & H_CPU_BEHAV_FAVOUR_SECURITY))
  458. security_ftr_clear(SEC_FTR_FAVOUR_SECURITY);
  459. if (!(result->behaviour & H_CPU_BEHAV_L1D_FLUSH_PR))
  460. security_ftr_clear(SEC_FTR_L1D_FLUSH_PR);
  461. if (!(result->behaviour & H_CPU_BEHAV_BNDS_CHK_SPEC_BAR))
  462. security_ftr_clear(SEC_FTR_BNDS_CHK_SPEC_BAR);
  463. }
  464. void pseries_setup_rfi_flush(void)
  465. {
  466. struct h_cpu_char_result result;
  467. enum l1d_flush_type types;
  468. bool enable;
  469. long rc;
  470. /*
  471. * Set features to the defaults assumed by init_cpu_char_feature_flags()
  472. * so it can set/clear again any features that might have changed after
  473. * migration, and in case the hypercall fails and it is not even called.
  474. */
  475. powerpc_security_features = SEC_FTR_DEFAULT;
  476. rc = plpar_get_cpu_characteristics(&result);
  477. if (rc == H_SUCCESS)
  478. init_cpu_char_feature_flags(&result);
  479. /*
  480. * We're the guest so this doesn't apply to us, clear it to simplify
  481. * handling of it elsewhere.
  482. */
  483. security_ftr_clear(SEC_FTR_L1D_FLUSH_HV);
  484. types = L1D_FLUSH_FALLBACK;
  485. if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_TRIG2))
  486. types |= L1D_FLUSH_MTTRIG;
  487. if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_ORI30))
  488. types |= L1D_FLUSH_ORI;
  489. enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) && \
  490. security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR);
  491. setup_rfi_flush(types, enable);
  492. setup_count_cache_flush();
  493. }
  494. #ifdef CONFIG_PCI_IOV
  495. enum rtas_iov_fw_value_map {
  496. NUM_RES_PROPERTY = 0, /* Number of Resources */
  497. LOW_INT = 1, /* Lowest 32 bits of Address */
  498. START_OF_ENTRIES = 2, /* Always start of entry */
  499. APERTURE_PROPERTY = 2, /* Start of entry+ to Aperture Size */
  500. WDW_SIZE_PROPERTY = 4, /* Start of entry+ to Window Size */
  501. NEXT_ENTRY = 7 /* Go to next entry on array */
  502. };
  503. enum get_iov_fw_value_index {
  504. BAR_ADDRS = 1, /* Get Bar Address */
  505. APERTURE_SIZE = 2, /* Get Aperture Size */
  506. WDW_SIZE = 3 /* Get Window Size */
  507. };
  508. resource_size_t pseries_get_iov_fw_value(struct pci_dev *dev, int resno,
  509. enum get_iov_fw_value_index value)
  510. {
  511. const int *indexes;
  512. struct device_node *dn = pci_device_to_OF_node(dev);
  513. int i, num_res, ret = 0;
  514. indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
  515. if (!indexes)
  516. return 0;
  517. /*
  518. * First element in the array is the number of Bars
  519. * returned. Search through the list to find the matching
  520. * bar
  521. */
  522. num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
  523. if (resno >= num_res)
  524. return 0; /* or an errror */
  525. i = START_OF_ENTRIES + NEXT_ENTRY * resno;
  526. switch (value) {
  527. case BAR_ADDRS:
  528. ret = of_read_number(&indexes[i], 2);
  529. break;
  530. case APERTURE_SIZE:
  531. ret = of_read_number(&indexes[i + APERTURE_PROPERTY], 2);
  532. break;
  533. case WDW_SIZE:
  534. ret = of_read_number(&indexes[i + WDW_SIZE_PROPERTY], 2);
  535. break;
  536. }
  537. return ret;
  538. }
  539. void of_pci_set_vf_bar_size(struct pci_dev *dev, const int *indexes)
  540. {
  541. struct resource *res;
  542. resource_size_t base, size;
  543. int i, r, num_res;
  544. num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
  545. num_res = min_t(int, num_res, PCI_SRIOV_NUM_BARS);
  546. for (i = START_OF_ENTRIES, r = 0; r < num_res && r < PCI_SRIOV_NUM_BARS;
  547. i += NEXT_ENTRY, r++) {
  548. res = &dev->resource[r + PCI_IOV_RESOURCES];
  549. base = of_read_number(&indexes[i], 2);
  550. size = of_read_number(&indexes[i + APERTURE_PROPERTY], 2);
  551. res->flags = pci_parse_of_flags(of_read_number
  552. (&indexes[i + LOW_INT], 1), 0);
  553. res->flags |= (IORESOURCE_MEM_64 | IORESOURCE_PCI_FIXED);
  554. res->name = pci_name(dev);
  555. res->start = base;
  556. res->end = base + size - 1;
  557. }
  558. }
  559. void of_pci_parse_iov_addrs(struct pci_dev *dev, const int *indexes)
  560. {
  561. struct resource *res, *root, *conflict;
  562. resource_size_t base, size;
  563. int i, r, num_res;
  564. /*
  565. * First element in the array is the number of Bars
  566. * returned. Search through the list to find the matching
  567. * bars assign them from firmware into resources structure.
  568. */
  569. num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
  570. for (i = START_OF_ENTRIES, r = 0; r < num_res && r < PCI_SRIOV_NUM_BARS;
  571. i += NEXT_ENTRY, r++) {
  572. res = &dev->resource[r + PCI_IOV_RESOURCES];
  573. base = of_read_number(&indexes[i], 2);
  574. size = of_read_number(&indexes[i + WDW_SIZE_PROPERTY], 2);
  575. res->name = pci_name(dev);
  576. res->start = base;
  577. res->end = base + size - 1;
  578. root = &iomem_resource;
  579. dev_dbg(&dev->dev,
  580. "pSeries IOV BAR %d: trying firmware assignment %pR\n",
  581. r + PCI_IOV_RESOURCES, res);
  582. conflict = request_resource_conflict(root, res);
  583. if (conflict) {
  584. dev_info(&dev->dev,
  585. "BAR %d: %pR conflicts with %s %pR\n",
  586. r + PCI_IOV_RESOURCES, res,
  587. conflict->name, conflict);
  588. res->flags |= IORESOURCE_UNSET;
  589. }
  590. }
  591. }
  592. static void pseries_disable_sriov_resources(struct pci_dev *pdev)
  593. {
  594. int i;
  595. pci_warn(pdev, "No hypervisor support for SR-IOV on this device, IOV BARs disabled.\n");
  596. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
  597. pdev->resource[i + PCI_IOV_RESOURCES].flags = 0;
  598. }
  599. static void pseries_pci_fixup_resources(struct pci_dev *pdev)
  600. {
  601. const int *indexes;
  602. struct device_node *dn = pci_device_to_OF_node(pdev);
  603. /*Firmware must support open sriov otherwise dont configure*/
  604. indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
  605. if (indexes)
  606. of_pci_set_vf_bar_size(pdev, indexes);
  607. else
  608. pseries_disable_sriov_resources(pdev);
  609. }
  610. static void pseries_pci_fixup_iov_resources(struct pci_dev *pdev)
  611. {
  612. const int *indexes;
  613. struct device_node *dn = pci_device_to_OF_node(pdev);
  614. if (!pdev->is_physfn || pci_dev_is_added(pdev))
  615. return;
  616. /*Firmware must support open sriov otherwise dont configure*/
  617. indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
  618. if (indexes)
  619. of_pci_parse_iov_addrs(pdev, indexes);
  620. else
  621. pseries_disable_sriov_resources(pdev);
  622. }
  623. static resource_size_t pseries_pci_iov_resource_alignment(struct pci_dev *pdev,
  624. int resno)
  625. {
  626. const __be32 *reg;
  627. struct device_node *dn = pci_device_to_OF_node(pdev);
  628. /*Firmware must support open sriov otherwise report regular alignment*/
  629. reg = of_get_property(dn, "ibm,is-open-sriov-pf", NULL);
  630. if (!reg)
  631. return pci_iov_resource_size(pdev, resno);
  632. if (!pdev->is_physfn)
  633. return 0;
  634. return pseries_get_iov_fw_value(pdev,
  635. resno - PCI_IOV_RESOURCES,
  636. APERTURE_SIZE);
  637. }
  638. #endif
  639. static void __init pSeries_setup_arch(void)
  640. {
  641. set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
  642. /* Discover PIC type and setup ppc_md accordingly */
  643. smp_init_pseries();
  644. /* openpic global configuration register (64-bit format). */
  645. /* openpic Interrupt Source Unit pointer (64-bit format). */
  646. /* python0 facility area (mmio) (64-bit format) REAL address. */
  647. /* init to some ~sane value until calibrate_delay() runs */
  648. loops_per_jiffy = 50000000;
  649. fwnmi_init();
  650. pseries_setup_rfi_flush();
  651. setup_stf_barrier();
  652. /* By default, only probe PCI (can be overridden by rtas_pci) */
  653. pci_add_flags(PCI_PROBE_ONLY);
  654. /* Find and initialize PCI host bridges */
  655. init_pci_config_tokens();
  656. find_and_init_phbs();
  657. of_reconfig_notifier_register(&pci_dn_reconfig_nb);
  658. pSeries_nvram_init();
  659. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  660. vpa_init(boot_cpuid);
  661. ppc_md.power_save = pseries_lpar_idle;
  662. ppc_md.enable_pmcs = pseries_lpar_enable_pmcs;
  663. #ifdef CONFIG_PCI_IOV
  664. ppc_md.pcibios_fixup_resources =
  665. pseries_pci_fixup_resources;
  666. ppc_md.pcibios_fixup_sriov =
  667. pseries_pci_fixup_iov_resources;
  668. ppc_md.pcibios_iov_resource_alignment =
  669. pseries_pci_iov_resource_alignment;
  670. #endif
  671. } else {
  672. /* No special idle routine */
  673. ppc_md.enable_pmcs = power4_enable_pmcs;
  674. }
  675. ppc_md.pcibios_root_bridge_prepare = pseries_root_bridge_prepare;
  676. }
  677. static void pseries_panic(char *str)
  678. {
  679. panic_flush_kmsg_end();
  680. rtas_os_term(str);
  681. }
  682. static int __init pSeries_init_panel(void)
  683. {
  684. /* Manually leave the kernel version on the panel. */
  685. #ifdef __BIG_ENDIAN__
  686. ppc_md.progress("Linux ppc64\n", 0);
  687. #else
  688. ppc_md.progress("Linux ppc64le\n", 0);
  689. #endif
  690. ppc_md.progress(init_utsname()->version, 0);
  691. return 0;
  692. }
  693. machine_arch_initcall(pseries, pSeries_init_panel);
  694. static int pseries_set_dabr(unsigned long dabr, unsigned long dabrx)
  695. {
  696. return plpar_hcall_norets(H_SET_DABR, dabr);
  697. }
  698. static int pseries_set_xdabr(unsigned long dabr, unsigned long dabrx)
  699. {
  700. /* Have to set at least one bit in the DABRX according to PAPR */
  701. if (dabrx == 0 && dabr == 0)
  702. dabrx = DABRX_USER;
  703. /* PAPR says we can only set kernel and user bits */
  704. dabrx &= DABRX_KERNEL | DABRX_USER;
  705. return plpar_hcall_norets(H_SET_XDABR, dabr, dabrx);
  706. }
  707. static int pseries_set_dawr(unsigned long dawr, unsigned long dawrx)
  708. {
  709. /* PAPR says we can't set HYP */
  710. dawrx &= ~DAWRX_HYP;
  711. return plpar_set_watchpoint0(dawr, dawrx);
  712. }
  713. #define CMO_CHARACTERISTICS_TOKEN 44
  714. #define CMO_MAXLENGTH 1026
  715. void pSeries_coalesce_init(void)
  716. {
  717. struct hvcall_mpp_x_data mpp_x_data;
  718. if (firmware_has_feature(FW_FEATURE_CMO) && !h_get_mpp_x(&mpp_x_data))
  719. powerpc_firmware_features |= FW_FEATURE_XCMO;
  720. else
  721. powerpc_firmware_features &= ~FW_FEATURE_XCMO;
  722. }
  723. /**
  724. * fw_cmo_feature_init - FW_FEATURE_CMO is not stored in ibm,hypertas-functions,
  725. * handle that here. (Stolen from parse_system_parameter_string)
  726. */
  727. static void pSeries_cmo_feature_init(void)
  728. {
  729. char *ptr, *key, *value, *end;
  730. int call_status;
  731. int page_order = IOMMU_PAGE_SHIFT_4K;
  732. pr_debug(" -> fw_cmo_feature_init()\n");
  733. spin_lock(&rtas_data_buf_lock);
  734. memset(rtas_data_buf, 0, RTAS_DATA_BUF_SIZE);
  735. call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
  736. NULL,
  737. CMO_CHARACTERISTICS_TOKEN,
  738. __pa(rtas_data_buf),
  739. RTAS_DATA_BUF_SIZE);
  740. if (call_status != 0) {
  741. spin_unlock(&rtas_data_buf_lock);
  742. pr_debug("CMO not available\n");
  743. pr_debug(" <- fw_cmo_feature_init()\n");
  744. return;
  745. }
  746. end = rtas_data_buf + CMO_MAXLENGTH - 2;
  747. ptr = rtas_data_buf + 2; /* step over strlen value */
  748. key = value = ptr;
  749. while (*ptr && (ptr <= end)) {
  750. /* Separate the key and value by replacing '=' with '\0' and
  751. * point the value at the string after the '='
  752. */
  753. if (ptr[0] == '=') {
  754. ptr[0] = '\0';
  755. value = ptr + 1;
  756. } else if (ptr[0] == '\0' || ptr[0] == ',') {
  757. /* Terminate the string containing the key/value pair */
  758. ptr[0] = '\0';
  759. if (key == value) {
  760. pr_debug("Malformed key/value pair\n");
  761. /* Never found a '=', end processing */
  762. break;
  763. }
  764. if (0 == strcmp(key, "CMOPageSize"))
  765. page_order = simple_strtol(value, NULL, 10);
  766. else if (0 == strcmp(key, "PrPSP"))
  767. CMO_PrPSP = simple_strtol(value, NULL, 10);
  768. else if (0 == strcmp(key, "SecPSP"))
  769. CMO_SecPSP = simple_strtol(value, NULL, 10);
  770. value = key = ptr + 1;
  771. }
  772. ptr++;
  773. }
  774. /* Page size is returned as the power of 2 of the page size,
  775. * convert to the page size in bytes before returning
  776. */
  777. CMO_PageSize = 1 << page_order;
  778. pr_debug("CMO_PageSize = %lu\n", CMO_PageSize);
  779. if (CMO_PrPSP != -1 || CMO_SecPSP != -1) {
  780. pr_info("CMO enabled\n");
  781. pr_debug("CMO enabled, PrPSP=%d, SecPSP=%d\n", CMO_PrPSP,
  782. CMO_SecPSP);
  783. powerpc_firmware_features |= FW_FEATURE_CMO;
  784. pSeries_coalesce_init();
  785. } else
  786. pr_debug("CMO not enabled, PrPSP=%d, SecPSP=%d\n", CMO_PrPSP,
  787. CMO_SecPSP);
  788. spin_unlock(&rtas_data_buf_lock);
  789. pr_debug(" <- fw_cmo_feature_init()\n");
  790. }
  791. /*
  792. * Early initialization. Relocation is on but do not reference unbolted pages
  793. */
  794. static void __init pseries_init(void)
  795. {
  796. pr_debug(" -> pseries_init()\n");
  797. #ifdef CONFIG_HVC_CONSOLE
  798. if (firmware_has_feature(FW_FEATURE_LPAR))
  799. hvc_vio_init_early();
  800. #endif
  801. if (firmware_has_feature(FW_FEATURE_XDABR))
  802. ppc_md.set_dabr = pseries_set_xdabr;
  803. else if (firmware_has_feature(FW_FEATURE_DABR))
  804. ppc_md.set_dabr = pseries_set_dabr;
  805. if (firmware_has_feature(FW_FEATURE_SET_MODE))
  806. ppc_md.set_dawr = pseries_set_dawr;
  807. pSeries_cmo_feature_init();
  808. iommu_init_early_pSeries();
  809. pr_debug(" <- pseries_init()\n");
  810. }
  811. /**
  812. * pseries_power_off - tell firmware about how to power off the system.
  813. *
  814. * This function calls either the power-off rtas token in normal cases
  815. * or the ibm,power-off-ups token (if present & requested) in case of
  816. * a power failure. If power-off token is used, power on will only be
  817. * possible with power button press. If ibm,power-off-ups token is used
  818. * it will allow auto poweron after power is restored.
  819. */
  820. static void pseries_power_off(void)
  821. {
  822. int rc;
  823. int rtas_poweroff_ups_token = rtas_token("ibm,power-off-ups");
  824. if (rtas_flash_term_hook)
  825. rtas_flash_term_hook(SYS_POWER_OFF);
  826. if (rtas_poweron_auto == 0 ||
  827. rtas_poweroff_ups_token == RTAS_UNKNOWN_SERVICE) {
  828. rc = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
  829. printk(KERN_INFO "RTAS power-off returned %d\n", rc);
  830. } else {
  831. rc = rtas_call(rtas_poweroff_ups_token, 0, 1, NULL);
  832. printk(KERN_INFO "RTAS ibm,power-off-ups returned %d\n", rc);
  833. }
  834. for (;;);
  835. }
  836. static int __init pSeries_probe(void)
  837. {
  838. const char *dtype = of_get_property(of_root, "device_type", NULL);
  839. if (dtype == NULL)
  840. return 0;
  841. if (strcmp(dtype, "chrp"))
  842. return 0;
  843. /* Cell blades firmware claims to be chrp while it's not. Until this
  844. * is fixed, we need to avoid those here.
  845. */
  846. if (of_machine_is_compatible("IBM,CPBW-1.0") ||
  847. of_machine_is_compatible("IBM,CBEA"))
  848. return 0;
  849. pm_power_off = pseries_power_off;
  850. pr_debug("Machine is%s LPAR !\n",
  851. (powerpc_firmware_features & FW_FEATURE_LPAR) ? "" : " not");
  852. pseries_init();
  853. return 1;
  854. }
  855. static int pSeries_pci_probe_mode(struct pci_bus *bus)
  856. {
  857. if (firmware_has_feature(FW_FEATURE_LPAR))
  858. return PCI_PROBE_DEVTREE;
  859. return PCI_PROBE_NORMAL;
  860. }
  861. struct pci_controller_ops pseries_pci_controller_ops = {
  862. .probe_mode = pSeries_pci_probe_mode,
  863. };
  864. define_machine(pseries) {
  865. .name = "pSeries",
  866. .probe = pSeries_probe,
  867. .setup_arch = pSeries_setup_arch,
  868. .init_IRQ = pseries_init_irq,
  869. .show_cpuinfo = pSeries_show_cpuinfo,
  870. .log_error = pSeries_log_error,
  871. .pcibios_fixup = pSeries_final_fixup,
  872. .restart = rtas_restart,
  873. .halt = rtas_halt,
  874. .panic = pseries_panic,
  875. .get_boot_time = rtas_get_boot_time,
  876. .get_rtc_time = rtas_get_rtc_time,
  877. .set_rtc_time = rtas_set_rtc_time,
  878. .calibrate_decr = generic_calibrate_decr,
  879. .progress = rtas_progress,
  880. .system_reset_exception = pSeries_system_reset_exception,
  881. .machine_check_early = pseries_machine_check_realmode,
  882. .machine_check_exception = pSeries_machine_check_exception,
  883. #ifdef CONFIG_KEXEC_CORE
  884. .machine_kexec = pSeries_machine_kexec,
  885. .kexec_cpu_down = pseries_kexec_cpu_down,
  886. #endif
  887. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  888. .memory_block_size = pseries_memory_block_size,
  889. #endif
  890. };