setup.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. DEFINE_STATIC_KEY_FALSE(shared_processor);
  74. EXPORT_SYMBOL_GPL(shared_processor);
  75. int CMO_PrPSP = -1;
  76. int CMO_SecPSP = -1;
  77. unsigned long CMO_PageSize = (ASM_CONST(1) << IOMMU_PAGE_SHIFT_4K);
  78. EXPORT_SYMBOL(CMO_PageSize);
  79. int fwnmi_active; /* TRUE if an FWNMI handler is present */
  80. static void pSeries_show_cpuinfo(struct seq_file *m)
  81. {
  82. struct device_node *root;
  83. const char *model = "";
  84. root = of_find_node_by_path("/");
  85. if (root)
  86. model = of_get_property(root, "model", NULL);
  87. seq_printf(m, "machine\t\t: CHRP %s\n", model);
  88. of_node_put(root);
  89. if (radix_enabled())
  90. seq_printf(m, "MMU\t\t: Radix\n");
  91. else
  92. seq_printf(m, "MMU\t\t: Hash\n");
  93. }
  94. /* Initialize firmware assisted non-maskable interrupts if
  95. * the firmware supports this feature.
  96. */
  97. static void __init fwnmi_init(void)
  98. {
  99. unsigned long system_reset_addr, machine_check_addr;
  100. u8 *mce_data_buf;
  101. unsigned int i;
  102. int nr_cpus = num_possible_cpus();
  103. int ibm_nmi_register = rtas_token("ibm,nmi-register");
  104. if (ibm_nmi_register == RTAS_UNKNOWN_SERVICE)
  105. return;
  106. /* If the kernel's not linked at zero we point the firmware at low
  107. * addresses anyway, and use a trampoline to get to the real code. */
  108. system_reset_addr = __pa(system_reset_fwnmi) - PHYSICAL_START;
  109. machine_check_addr = __pa(machine_check_fwnmi) - PHYSICAL_START;
  110. if (0 == rtas_call(ibm_nmi_register, 2, 1, NULL, system_reset_addr,
  111. machine_check_addr))
  112. fwnmi_active = 1;
  113. /*
  114. * Allocate a chunk for per cpu buffer to hold rtas errorlog.
  115. * It will be used in real mode mce handler, hence it needs to be
  116. * below RMA.
  117. */
  118. mce_data_buf = __va(memblock_alloc_base(RTAS_ERROR_LOG_MAX * nr_cpus,
  119. RTAS_ERROR_LOG_MAX, ppc64_rma_size));
  120. for_each_possible_cpu(i) {
  121. paca_ptrs[i]->mce_data_buf = mce_data_buf +
  122. (RTAS_ERROR_LOG_MAX * i);
  123. }
  124. }
  125. static void pseries_8259_cascade(struct irq_desc *desc)
  126. {
  127. struct irq_chip *chip = irq_desc_get_chip(desc);
  128. unsigned int cascade_irq = i8259_irq();
  129. if (cascade_irq)
  130. generic_handle_irq(cascade_irq);
  131. chip->irq_eoi(&desc->irq_data);
  132. }
  133. static void __init pseries_setup_i8259_cascade(void)
  134. {
  135. struct device_node *np, *old, *found = NULL;
  136. unsigned int cascade;
  137. const u32 *addrp;
  138. unsigned long intack = 0;
  139. int naddr;
  140. for_each_node_by_type(np, "interrupt-controller") {
  141. if (of_device_is_compatible(np, "chrp,iic")) {
  142. found = np;
  143. break;
  144. }
  145. }
  146. if (found == NULL) {
  147. printk(KERN_DEBUG "pic: no ISA interrupt controller\n");
  148. return;
  149. }
  150. cascade = irq_of_parse_and_map(found, 0);
  151. if (!cascade) {
  152. printk(KERN_ERR "pic: failed to map cascade interrupt");
  153. return;
  154. }
  155. pr_debug("pic: cascade mapped to irq %d\n", cascade);
  156. for (old = of_node_get(found); old != NULL ; old = np) {
  157. np = of_get_parent(old);
  158. of_node_put(old);
  159. if (np == NULL)
  160. break;
  161. if (strcmp(np->name, "pci") != 0)
  162. continue;
  163. addrp = of_get_property(np, "8259-interrupt-acknowledge", NULL);
  164. if (addrp == NULL)
  165. continue;
  166. naddr = of_n_addr_cells(np);
  167. intack = addrp[naddr-1];
  168. if (naddr > 1)
  169. intack |= ((unsigned long)addrp[naddr-2]) << 32;
  170. }
  171. if (intack)
  172. printk(KERN_DEBUG "pic: PCI 8259 intack at 0x%016lx\n", intack);
  173. i8259_init(found, intack);
  174. of_node_put(found);
  175. irq_set_chained_handler(cascade, pseries_8259_cascade);
  176. }
  177. static void __init pseries_init_irq(void)
  178. {
  179. /* Try using a XIVE if available, otherwise use a XICS */
  180. if (!xive_spapr_init()) {
  181. xics_init();
  182. pseries_setup_i8259_cascade();
  183. }
  184. }
  185. static void pseries_lpar_enable_pmcs(void)
  186. {
  187. unsigned long set, reset;
  188. set = 1UL << 63;
  189. reset = 0;
  190. plpar_hcall_norets(H_PERFMON, set, reset);
  191. }
  192. static int pci_dn_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *data)
  193. {
  194. struct of_reconfig_data *rd = data;
  195. struct device_node *parent, *np = rd->dn;
  196. struct pci_dn *pdn;
  197. int err = NOTIFY_OK;
  198. switch (action) {
  199. case OF_RECONFIG_ATTACH_NODE:
  200. parent = of_get_parent(np);
  201. pdn = parent ? PCI_DN(parent) : NULL;
  202. if (pdn)
  203. pci_add_device_node_info(pdn->phb, np);
  204. of_node_put(parent);
  205. break;
  206. case OF_RECONFIG_DETACH_NODE:
  207. pdn = PCI_DN(np);
  208. if (pdn)
  209. list_del(&pdn->list);
  210. break;
  211. default:
  212. err = NOTIFY_DONE;
  213. break;
  214. }
  215. return err;
  216. }
  217. static struct notifier_block pci_dn_reconfig_nb = {
  218. .notifier_call = pci_dn_reconfig_notifier,
  219. };
  220. struct kmem_cache *dtl_cache;
  221. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  222. /*
  223. * Allocate space for the dispatch trace log for all possible cpus
  224. * and register the buffers with the hypervisor. This is used for
  225. * computing time stolen by the hypervisor.
  226. */
  227. static int alloc_dispatch_logs(void)
  228. {
  229. int cpu, ret;
  230. struct paca_struct *pp;
  231. struct dtl_entry *dtl;
  232. if (!firmware_has_feature(FW_FEATURE_SPLPAR))
  233. return 0;
  234. if (!dtl_cache)
  235. return 0;
  236. for_each_possible_cpu(cpu) {
  237. pp = paca_ptrs[cpu];
  238. dtl = kmem_cache_alloc(dtl_cache, GFP_KERNEL);
  239. if (!dtl) {
  240. pr_warn("Failed to allocate dispatch trace log for cpu %d\n",
  241. cpu);
  242. pr_warn("Stolen time statistics will be unreliable\n");
  243. break;
  244. }
  245. pp->dtl_ridx = 0;
  246. pp->dispatch_log = dtl;
  247. pp->dispatch_log_end = dtl + N_DISPATCH_LOG;
  248. pp->dtl_curr = dtl;
  249. }
  250. /* Register the DTL for the current (boot) cpu */
  251. dtl = get_paca()->dispatch_log;
  252. get_paca()->dtl_ridx = 0;
  253. get_paca()->dtl_curr = dtl;
  254. get_paca()->lppaca_ptr->dtl_idx = 0;
  255. /* hypervisor reads buffer length from this field */
  256. dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES);
  257. ret = register_dtl(hard_smp_processor_id(), __pa(dtl));
  258. if (ret)
  259. pr_err("WARNING: DTL registration of cpu %d (hw %d) failed "
  260. "with %d\n", smp_processor_id(),
  261. hard_smp_processor_id(), ret);
  262. get_paca()->lppaca_ptr->dtl_enable_mask = 2;
  263. return 0;
  264. }
  265. #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  266. static inline int alloc_dispatch_logs(void)
  267. {
  268. return 0;
  269. }
  270. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  271. static int alloc_dispatch_log_kmem_cache(void)
  272. {
  273. dtl_cache = kmem_cache_create("dtl", DISPATCH_LOG_BYTES,
  274. DISPATCH_LOG_BYTES, 0, NULL);
  275. if (!dtl_cache) {
  276. pr_warn("Failed to create dispatch trace log buffer cache\n");
  277. pr_warn("Stolen time statistics will be unreliable\n");
  278. return 0;
  279. }
  280. return alloc_dispatch_logs();
  281. }
  282. machine_early_initcall(pseries, alloc_dispatch_log_kmem_cache);
  283. static void pseries_lpar_idle(void)
  284. {
  285. /*
  286. * Default handler to go into low thread priority and possibly
  287. * low power mode by ceding processor to hypervisor
  288. */
  289. if (!prep_irq_for_idle())
  290. return;
  291. /* Indicate to hypervisor that we are idle. */
  292. get_lppaca()->idle = 1;
  293. /*
  294. * Yield the processor to the hypervisor. We return if
  295. * an external interrupt occurs (which are driven prior
  296. * to returning here) or if a prod occurs from another
  297. * processor. When returning here, external interrupts
  298. * are enabled.
  299. */
  300. cede_processor();
  301. get_lppaca()->idle = 0;
  302. }
  303. /*
  304. * Enable relocation on during exceptions. This has partition wide scope and
  305. * may take a while to complete, if it takes longer than one second we will
  306. * just give up rather than wasting any more time on this - if that turns out
  307. * to ever be a problem in practice we can move this into a kernel thread to
  308. * finish off the process later in boot.
  309. */
  310. void pseries_enable_reloc_on_exc(void)
  311. {
  312. long rc;
  313. unsigned int delay, total_delay = 0;
  314. while (1) {
  315. rc = enable_reloc_on_exceptions();
  316. if (!H_IS_LONG_BUSY(rc)) {
  317. if (rc == H_P2) {
  318. pr_info("Relocation on exceptions not"
  319. " supported\n");
  320. } else if (rc != H_SUCCESS) {
  321. pr_warn("Unable to enable relocation"
  322. " on exceptions: %ld\n", rc);
  323. }
  324. break;
  325. }
  326. delay = get_longbusy_msecs(rc);
  327. total_delay += delay;
  328. if (total_delay > 1000) {
  329. pr_warn("Warning: Giving up waiting to enable "
  330. "relocation on exceptions (%u msec)!\n",
  331. total_delay);
  332. return;
  333. }
  334. mdelay(delay);
  335. }
  336. }
  337. EXPORT_SYMBOL(pseries_enable_reloc_on_exc);
  338. void pseries_disable_reloc_on_exc(void)
  339. {
  340. long rc;
  341. while (1) {
  342. rc = disable_reloc_on_exceptions();
  343. if (!H_IS_LONG_BUSY(rc))
  344. break;
  345. mdelay(get_longbusy_msecs(rc));
  346. }
  347. if (rc != H_SUCCESS)
  348. pr_warn("Warning: Failed to disable relocation on exceptions: %ld\n",
  349. rc);
  350. }
  351. EXPORT_SYMBOL(pseries_disable_reloc_on_exc);
  352. #ifdef CONFIG_KEXEC_CORE
  353. static void pSeries_machine_kexec(struct kimage *image)
  354. {
  355. if (firmware_has_feature(FW_FEATURE_SET_MODE))
  356. pseries_disable_reloc_on_exc();
  357. default_machine_kexec(image);
  358. }
  359. #endif
  360. #ifdef __LITTLE_ENDIAN__
  361. void pseries_big_endian_exceptions(void)
  362. {
  363. long rc;
  364. while (1) {
  365. rc = enable_big_endian_exceptions();
  366. if (!H_IS_LONG_BUSY(rc))
  367. break;
  368. mdelay(get_longbusy_msecs(rc));
  369. }
  370. /*
  371. * At this point it is unlikely panic() will get anything
  372. * out to the user, since this is called very late in kexec
  373. * but at least this will stop us from continuing on further
  374. * and creating an even more difficult to debug situation.
  375. *
  376. * There is a known problem when kdump'ing, if cpus are offline
  377. * the above call will fail. Rather than panicking again, keep
  378. * going and hope the kdump kernel is also little endian, which
  379. * it usually is.
  380. */
  381. if (rc && !kdump_in_progress())
  382. panic("Could not enable big endian exceptions");
  383. }
  384. void pseries_little_endian_exceptions(void)
  385. {
  386. long rc;
  387. while (1) {
  388. rc = enable_little_endian_exceptions();
  389. if (!H_IS_LONG_BUSY(rc))
  390. break;
  391. mdelay(get_longbusy_msecs(rc));
  392. }
  393. if (rc) {
  394. ppc_md.progress("H_SET_MODE LE exception fail", 0);
  395. panic("Could not enable little endian exceptions");
  396. }
  397. }
  398. #endif
  399. static void __init find_and_init_phbs(void)
  400. {
  401. struct device_node *node;
  402. struct pci_controller *phb;
  403. struct device_node *root = of_find_node_by_path("/");
  404. for_each_child_of_node(root, node) {
  405. if (node->type == NULL || (strcmp(node->type, "pci") != 0 &&
  406. strcmp(node->type, "pciex") != 0))
  407. continue;
  408. phb = pcibios_alloc_controller(node);
  409. if (!phb)
  410. continue;
  411. rtas_setup_phb(phb);
  412. pci_process_bridge_OF_ranges(phb, node, 0);
  413. isa_bridge_find_early(phb);
  414. phb->controller_ops = pseries_pci_controller_ops;
  415. }
  416. of_node_put(root);
  417. /*
  418. * PCI_PROBE_ONLY and PCI_REASSIGN_ALL_BUS can be set via properties
  419. * in chosen.
  420. */
  421. of_pci_check_probe_only();
  422. }
  423. static void init_cpu_char_feature_flags(struct h_cpu_char_result *result)
  424. {
  425. /*
  426. * The features below are disabled by default, so we instead look to see
  427. * if firmware has *enabled* them, and set them if so.
  428. */
  429. if (result->character & H_CPU_CHAR_SPEC_BAR_ORI31)
  430. security_ftr_set(SEC_FTR_SPEC_BAR_ORI31);
  431. if (result->character & H_CPU_CHAR_BCCTRL_SERIALISED)
  432. security_ftr_set(SEC_FTR_BCCTRL_SERIALISED);
  433. if (result->character & H_CPU_CHAR_L1D_FLUSH_ORI30)
  434. security_ftr_set(SEC_FTR_L1D_FLUSH_ORI30);
  435. if (result->character & H_CPU_CHAR_L1D_FLUSH_TRIG2)
  436. security_ftr_set(SEC_FTR_L1D_FLUSH_TRIG2);
  437. if (result->character & H_CPU_CHAR_L1D_THREAD_PRIV)
  438. security_ftr_set(SEC_FTR_L1D_THREAD_PRIV);
  439. if (result->character & H_CPU_CHAR_COUNT_CACHE_DISABLED)
  440. security_ftr_set(SEC_FTR_COUNT_CACHE_DISABLED);
  441. if (result->character & H_CPU_CHAR_BCCTR_FLUSH_ASSIST)
  442. security_ftr_set(SEC_FTR_BCCTR_FLUSH_ASSIST);
  443. if (result->behaviour & H_CPU_BEHAV_FLUSH_COUNT_CACHE)
  444. security_ftr_set(SEC_FTR_FLUSH_COUNT_CACHE);
  445. /*
  446. * The features below are enabled by default, so we instead look to see
  447. * if firmware has *disabled* them, and clear them if so.
  448. */
  449. if (!(result->behaviour & H_CPU_BEHAV_FAVOUR_SECURITY))
  450. security_ftr_clear(SEC_FTR_FAVOUR_SECURITY);
  451. if (!(result->behaviour & H_CPU_BEHAV_L1D_FLUSH_PR))
  452. security_ftr_clear(SEC_FTR_L1D_FLUSH_PR);
  453. if (!(result->behaviour & H_CPU_BEHAV_BNDS_CHK_SPEC_BAR))
  454. security_ftr_clear(SEC_FTR_BNDS_CHK_SPEC_BAR);
  455. }
  456. void pseries_setup_rfi_flush(void)
  457. {
  458. struct h_cpu_char_result result;
  459. enum l1d_flush_type types;
  460. bool enable;
  461. long rc;
  462. /*
  463. * Set features to the defaults assumed by init_cpu_char_feature_flags()
  464. * so it can set/clear again any features that might have changed after
  465. * migration, and in case the hypercall fails and it is not even called.
  466. */
  467. powerpc_security_features = SEC_FTR_DEFAULT;
  468. rc = plpar_get_cpu_characteristics(&result);
  469. if (rc == H_SUCCESS)
  470. init_cpu_char_feature_flags(&result);
  471. /*
  472. * We're the guest so this doesn't apply to us, clear it to simplify
  473. * handling of it elsewhere.
  474. */
  475. security_ftr_clear(SEC_FTR_L1D_FLUSH_HV);
  476. types = L1D_FLUSH_FALLBACK;
  477. if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_TRIG2))
  478. types |= L1D_FLUSH_MTTRIG;
  479. if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_ORI30))
  480. types |= L1D_FLUSH_ORI;
  481. enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) && \
  482. security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR);
  483. setup_rfi_flush(types, enable);
  484. setup_count_cache_flush();
  485. enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
  486. security_ftr_enabled(SEC_FTR_L1D_FLUSH_ENTRY);
  487. setup_entry_flush(enable);
  488. enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
  489. security_ftr_enabled(SEC_FTR_L1D_FLUSH_UACCESS);
  490. setup_uaccess_flush(enable);
  491. }
  492. #ifdef CONFIG_PCI_IOV
  493. enum rtas_iov_fw_value_map {
  494. NUM_RES_PROPERTY = 0, /* Number of Resources */
  495. LOW_INT = 1, /* Lowest 32 bits of Address */
  496. START_OF_ENTRIES = 2, /* Always start of entry */
  497. APERTURE_PROPERTY = 2, /* Start of entry+ to Aperture Size */
  498. WDW_SIZE_PROPERTY = 4, /* Start of entry+ to Window Size */
  499. NEXT_ENTRY = 7 /* Go to next entry on array */
  500. };
  501. enum get_iov_fw_value_index {
  502. BAR_ADDRS = 1, /* Get Bar Address */
  503. APERTURE_SIZE = 2, /* Get Aperture Size */
  504. WDW_SIZE = 3 /* Get Window Size */
  505. };
  506. resource_size_t pseries_get_iov_fw_value(struct pci_dev *dev, int resno,
  507. enum get_iov_fw_value_index value)
  508. {
  509. const int *indexes;
  510. struct device_node *dn = pci_device_to_OF_node(dev);
  511. int i, num_res, ret = 0;
  512. indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
  513. if (!indexes)
  514. return 0;
  515. /*
  516. * First element in the array is the number of Bars
  517. * returned. Search through the list to find the matching
  518. * bar
  519. */
  520. num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
  521. if (resno >= num_res)
  522. return 0; /* or an errror */
  523. i = START_OF_ENTRIES + NEXT_ENTRY * resno;
  524. switch (value) {
  525. case BAR_ADDRS:
  526. ret = of_read_number(&indexes[i], 2);
  527. break;
  528. case APERTURE_SIZE:
  529. ret = of_read_number(&indexes[i + APERTURE_PROPERTY], 2);
  530. break;
  531. case WDW_SIZE:
  532. ret = of_read_number(&indexes[i + WDW_SIZE_PROPERTY], 2);
  533. break;
  534. }
  535. return ret;
  536. }
  537. void of_pci_set_vf_bar_size(struct pci_dev *dev, const int *indexes)
  538. {
  539. struct resource *res;
  540. resource_size_t base, size;
  541. int i, r, num_res;
  542. num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
  543. num_res = min_t(int, num_res, PCI_SRIOV_NUM_BARS);
  544. for (i = START_OF_ENTRIES, r = 0; r < num_res && r < PCI_SRIOV_NUM_BARS;
  545. i += NEXT_ENTRY, r++) {
  546. res = &dev->resource[r + PCI_IOV_RESOURCES];
  547. base = of_read_number(&indexes[i], 2);
  548. size = of_read_number(&indexes[i + APERTURE_PROPERTY], 2);
  549. res->flags = pci_parse_of_flags(of_read_number
  550. (&indexes[i + LOW_INT], 1), 0);
  551. res->flags |= (IORESOURCE_MEM_64 | IORESOURCE_PCI_FIXED);
  552. res->name = pci_name(dev);
  553. res->start = base;
  554. res->end = base + size - 1;
  555. }
  556. }
  557. void of_pci_parse_iov_addrs(struct pci_dev *dev, const int *indexes)
  558. {
  559. struct resource *res, *root, *conflict;
  560. resource_size_t base, size;
  561. int i, r, num_res;
  562. /*
  563. * First element in the array is the number of Bars
  564. * returned. Search through the list to find the matching
  565. * bars assign them from firmware into resources structure.
  566. */
  567. num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
  568. for (i = START_OF_ENTRIES, r = 0; r < num_res && r < PCI_SRIOV_NUM_BARS;
  569. i += NEXT_ENTRY, r++) {
  570. res = &dev->resource[r + PCI_IOV_RESOURCES];
  571. base = of_read_number(&indexes[i], 2);
  572. size = of_read_number(&indexes[i + WDW_SIZE_PROPERTY], 2);
  573. res->name = pci_name(dev);
  574. res->start = base;
  575. res->end = base + size - 1;
  576. root = &iomem_resource;
  577. dev_dbg(&dev->dev,
  578. "pSeries IOV BAR %d: trying firmware assignment %pR\n",
  579. r + PCI_IOV_RESOURCES, res);
  580. conflict = request_resource_conflict(root, res);
  581. if (conflict) {
  582. dev_info(&dev->dev,
  583. "BAR %d: %pR conflicts with %s %pR\n",
  584. r + PCI_IOV_RESOURCES, res,
  585. conflict->name, conflict);
  586. res->flags |= IORESOURCE_UNSET;
  587. }
  588. }
  589. }
  590. static void pseries_disable_sriov_resources(struct pci_dev *pdev)
  591. {
  592. int i;
  593. pci_warn(pdev, "No hypervisor support for SR-IOV on this device, IOV BARs disabled.\n");
  594. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
  595. pdev->resource[i + PCI_IOV_RESOURCES].flags = 0;
  596. }
  597. static void pseries_pci_fixup_resources(struct pci_dev *pdev)
  598. {
  599. const int *indexes;
  600. struct device_node *dn = pci_device_to_OF_node(pdev);
  601. /*Firmware must support open sriov otherwise dont configure*/
  602. indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
  603. if (indexes)
  604. of_pci_set_vf_bar_size(pdev, indexes);
  605. else
  606. pseries_disable_sriov_resources(pdev);
  607. }
  608. static void pseries_pci_fixup_iov_resources(struct pci_dev *pdev)
  609. {
  610. const int *indexes;
  611. struct device_node *dn = pci_device_to_OF_node(pdev);
  612. if (!pdev->is_physfn || pci_dev_is_added(pdev))
  613. return;
  614. /*Firmware must support open sriov otherwise dont configure*/
  615. indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
  616. if (indexes)
  617. of_pci_parse_iov_addrs(pdev, indexes);
  618. else
  619. pseries_disable_sriov_resources(pdev);
  620. }
  621. static resource_size_t pseries_pci_iov_resource_alignment(struct pci_dev *pdev,
  622. int resno)
  623. {
  624. const __be32 *reg;
  625. struct device_node *dn = pci_device_to_OF_node(pdev);
  626. /*Firmware must support open sriov otherwise report regular alignment*/
  627. reg = of_get_property(dn, "ibm,is-open-sriov-pf", NULL);
  628. if (!reg)
  629. return pci_iov_resource_size(pdev, resno);
  630. if (!pdev->is_physfn)
  631. return 0;
  632. return pseries_get_iov_fw_value(pdev,
  633. resno - PCI_IOV_RESOURCES,
  634. APERTURE_SIZE);
  635. }
  636. #endif
  637. static void __init pSeries_setup_arch(void)
  638. {
  639. set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
  640. /* Discover PIC type and setup ppc_md accordingly */
  641. smp_init_pseries();
  642. /* openpic global configuration register (64-bit format). */
  643. /* openpic Interrupt Source Unit pointer (64-bit format). */
  644. /* python0 facility area (mmio) (64-bit format) REAL address. */
  645. /* init to some ~sane value until calibrate_delay() runs */
  646. loops_per_jiffy = 50000000;
  647. fwnmi_init();
  648. pseries_setup_rfi_flush();
  649. setup_stf_barrier();
  650. /* By default, only probe PCI (can be overridden by rtas_pci) */
  651. pci_add_flags(PCI_PROBE_ONLY);
  652. /* Find and initialize PCI host bridges */
  653. init_pci_config_tokens();
  654. find_and_init_phbs();
  655. of_reconfig_notifier_register(&pci_dn_reconfig_nb);
  656. pSeries_nvram_init();
  657. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  658. vpa_init(boot_cpuid);
  659. if (lppaca_shared_proc(get_lppaca()))
  660. static_branch_enable(&shared_processor);
  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_exception = pSeries_machine_check_exception,
  882. #ifdef CONFIG_KEXEC_CORE
  883. .machine_kexec = pSeries_machine_kexec,
  884. .kexec_cpu_down = pseries_kexec_cpu_down,
  885. #endif
  886. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  887. .memory_block_size = pseries_memory_block_size,
  888. #endif
  889. };