pci.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * Support PCI/PCIe on PowerNV platforms
  3. *
  4. * Currently supports only P5IOC2
  5. *
  6. * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/pci.h>
  15. #include <linux/delay.h>
  16. #include <linux/string.h>
  17. #include <linux/init.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/irq.h>
  20. #include <linux/io.h>
  21. #include <linux/msi.h>
  22. #include <linux/iommu.h>
  23. #include <asm/sections.h>
  24. #include <asm/io.h>
  25. #include <asm/prom.h>
  26. #include <asm/pci-bridge.h>
  27. #include <asm/machdep.h>
  28. #include <asm/msi_bitmap.h>
  29. #include <asm/ppc-pci.h>
  30. #include <asm/opal.h>
  31. #include <asm/iommu.h>
  32. #include <asm/tce.h>
  33. #include <asm/firmware.h>
  34. #include <asm/eeh_event.h>
  35. #include <asm/eeh.h>
  36. #include "powernv.h"
  37. #include "pci.h"
  38. /* Delay in usec */
  39. #define PCI_RESET_DELAY_US 3000000
  40. #define cfg_dbg(fmt...) do { } while(0)
  41. //#define cfg_dbg(fmt...) printk(fmt)
  42. #ifdef CONFIG_PCI_MSI
  43. static int pnv_msi_check_device(struct pci_dev* pdev, int nvec, int type)
  44. {
  45. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  46. struct pnv_phb *phb = hose->private_data;
  47. struct pci_dn *pdn = pci_get_pdn(pdev);
  48. if (pdn && pdn->force_32bit_msi && !phb->msi32_support)
  49. return -ENODEV;
  50. return (phb && phb->msi_bmp.bitmap) ? 0 : -ENODEV;
  51. }
  52. static int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  53. {
  54. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  55. struct pnv_phb *phb = hose->private_data;
  56. struct msi_desc *entry;
  57. struct msi_msg msg;
  58. int hwirq;
  59. unsigned int virq;
  60. int rc;
  61. if (WARN_ON(!phb))
  62. return -ENODEV;
  63. list_for_each_entry(entry, &pdev->msi_list, list) {
  64. if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
  65. pr_warn("%s: Supports only 64-bit MSIs\n",
  66. pci_name(pdev));
  67. return -ENXIO;
  68. }
  69. hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1);
  70. if (hwirq < 0) {
  71. pr_warn("%s: Failed to find a free MSI\n",
  72. pci_name(pdev));
  73. return -ENOSPC;
  74. }
  75. virq = irq_create_mapping(NULL, phb->msi_base + hwirq);
  76. if (virq == NO_IRQ) {
  77. pr_warn("%s: Failed to map MSI to linux irq\n",
  78. pci_name(pdev));
  79. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
  80. return -ENOMEM;
  81. }
  82. rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq,
  83. virq, entry->msi_attrib.is_64, &msg);
  84. if (rc) {
  85. pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
  86. irq_dispose_mapping(virq);
  87. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
  88. return rc;
  89. }
  90. irq_set_msi_desc(virq, entry);
  91. write_msi_msg(virq, &msg);
  92. }
  93. return 0;
  94. }
  95. static void pnv_teardown_msi_irqs(struct pci_dev *pdev)
  96. {
  97. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  98. struct pnv_phb *phb = hose->private_data;
  99. struct msi_desc *entry;
  100. if (WARN_ON(!phb))
  101. return;
  102. list_for_each_entry(entry, &pdev->msi_list, list) {
  103. if (entry->irq == NO_IRQ)
  104. continue;
  105. irq_set_msi_desc(entry->irq, NULL);
  106. msi_bitmap_free_hwirqs(&phb->msi_bmp,
  107. virq_to_hw(entry->irq) - phb->msi_base, 1);
  108. irq_dispose_mapping(entry->irq);
  109. }
  110. }
  111. #endif /* CONFIG_PCI_MSI */
  112. static void pnv_pci_dump_p7ioc_diag_data(struct pnv_phb *phb)
  113. {
  114. struct OpalIoP7IOCPhbErrorData *data = &phb->diag.p7ioc;
  115. int i;
  116. pr_info("PHB %d diagnostic data:\n", phb->hose->global_number);
  117. pr_info(" brdgCtl = 0x%08x\n", data->brdgCtl);
  118. pr_info(" portStatusReg = 0x%08x\n", data->portStatusReg);
  119. pr_info(" rootCmplxStatus = 0x%08x\n", data->rootCmplxStatus);
  120. pr_info(" busAgentStatus = 0x%08x\n", data->busAgentStatus);
  121. pr_info(" deviceStatus = 0x%08x\n", data->deviceStatus);
  122. pr_info(" slotStatus = 0x%08x\n", data->slotStatus);
  123. pr_info(" linkStatus = 0x%08x\n", data->linkStatus);
  124. pr_info(" devCmdStatus = 0x%08x\n", data->devCmdStatus);
  125. pr_info(" devSecStatus = 0x%08x\n", data->devSecStatus);
  126. pr_info(" rootErrorStatus = 0x%08x\n", data->rootErrorStatus);
  127. pr_info(" uncorrErrorStatus = 0x%08x\n", data->uncorrErrorStatus);
  128. pr_info(" corrErrorStatus = 0x%08x\n", data->corrErrorStatus);
  129. pr_info(" tlpHdr1 = 0x%08x\n", data->tlpHdr1);
  130. pr_info(" tlpHdr2 = 0x%08x\n", data->tlpHdr2);
  131. pr_info(" tlpHdr3 = 0x%08x\n", data->tlpHdr3);
  132. pr_info(" tlpHdr4 = 0x%08x\n", data->tlpHdr4);
  133. pr_info(" sourceId = 0x%08x\n", data->sourceId);
  134. pr_info(" errorClass = 0x%016llx\n", data->errorClass);
  135. pr_info(" correlator = 0x%016llx\n", data->correlator);
  136. pr_info(" p7iocPlssr = 0x%016llx\n", data->p7iocPlssr);
  137. pr_info(" p7iocCsr = 0x%016llx\n", data->p7iocCsr);
  138. pr_info(" lemFir = 0x%016llx\n", data->lemFir);
  139. pr_info(" lemErrorMask = 0x%016llx\n", data->lemErrorMask);
  140. pr_info(" lemWOF = 0x%016llx\n", data->lemWOF);
  141. pr_info(" phbErrorStatus = 0x%016llx\n", data->phbErrorStatus);
  142. pr_info(" phbFirstErrorStatus = 0x%016llx\n", data->phbFirstErrorStatus);
  143. pr_info(" phbErrorLog0 = 0x%016llx\n", data->phbErrorLog0);
  144. pr_info(" phbErrorLog1 = 0x%016llx\n", data->phbErrorLog1);
  145. pr_info(" mmioErrorStatus = 0x%016llx\n", data->mmioErrorStatus);
  146. pr_info(" mmioFirstErrorStatus = 0x%016llx\n", data->mmioFirstErrorStatus);
  147. pr_info(" mmioErrorLog0 = 0x%016llx\n", data->mmioErrorLog0);
  148. pr_info(" mmioErrorLog1 = 0x%016llx\n", data->mmioErrorLog1);
  149. pr_info(" dma0ErrorStatus = 0x%016llx\n", data->dma0ErrorStatus);
  150. pr_info(" dma0FirstErrorStatus = 0x%016llx\n", data->dma0FirstErrorStatus);
  151. pr_info(" dma0ErrorLog0 = 0x%016llx\n", data->dma0ErrorLog0);
  152. pr_info(" dma0ErrorLog1 = 0x%016llx\n", data->dma0ErrorLog1);
  153. pr_info(" dma1ErrorStatus = 0x%016llx\n", data->dma1ErrorStatus);
  154. pr_info(" dma1FirstErrorStatus = 0x%016llx\n", data->dma1FirstErrorStatus);
  155. pr_info(" dma1ErrorLog0 = 0x%016llx\n", data->dma1ErrorLog0);
  156. pr_info(" dma1ErrorLog1 = 0x%016llx\n", data->dma1ErrorLog1);
  157. for (i = 0; i < OPAL_P7IOC_NUM_PEST_REGS; i++) {
  158. if ((data->pestA[i] >> 63) == 0 &&
  159. (data->pestB[i] >> 63) == 0)
  160. continue;
  161. pr_info(" PE[%3d] PESTA = 0x%016llx\n", i, data->pestA[i]);
  162. pr_info(" PESTB = 0x%016llx\n", data->pestB[i]);
  163. }
  164. }
  165. static void pnv_pci_dump_phb_diag_data(struct pnv_phb *phb)
  166. {
  167. switch(phb->model) {
  168. case PNV_PHB_MODEL_P7IOC:
  169. pnv_pci_dump_p7ioc_diag_data(phb);
  170. break;
  171. default:
  172. pr_warning("PCI %d: Can't decode this PHB diag data\n",
  173. phb->hose->global_number);
  174. }
  175. }
  176. static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
  177. {
  178. unsigned long flags, rc;
  179. int has_diag;
  180. spin_lock_irqsave(&phb->lock, flags);
  181. rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob,
  182. PNV_PCI_DIAG_BUF_SIZE);
  183. has_diag = (rc == OPAL_SUCCESS);
  184. rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
  185. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  186. if (rc) {
  187. pr_warning("PCI %d: Failed to clear EEH freeze state"
  188. " for PE#%d, err %ld\n",
  189. phb->hose->global_number, pe_no, rc);
  190. /* For now, let's only display the diag buffer when we fail to clear
  191. * the EEH status. We'll do more sensible things later when we have
  192. * proper EEH support. We need to make sure we don't pollute ourselves
  193. * with the normal errors generated when probing empty slots
  194. */
  195. if (has_diag)
  196. pnv_pci_dump_phb_diag_data(phb);
  197. else
  198. pr_warning("PCI %d: No diag data available\n",
  199. phb->hose->global_number);
  200. }
  201. spin_unlock_irqrestore(&phb->lock, flags);
  202. }
  203. static void pnv_pci_config_check_eeh(struct pnv_phb *phb,
  204. struct device_node *dn)
  205. {
  206. s64 rc;
  207. u8 fstate;
  208. __be16 pcierr;
  209. u32 pe_no;
  210. /*
  211. * Get the PE#. During the PCI probe stage, we might not
  212. * setup that yet. So all ER errors should be mapped to
  213. * reserved PE.
  214. */
  215. pe_no = PCI_DN(dn)->pe_number;
  216. if (pe_no == IODA_INVALID_PE) {
  217. if (phb->type == PNV_PHB_P5IOC2)
  218. pe_no = 0;
  219. else
  220. pe_no = phb->ioda.reserved_pe;
  221. }
  222. /* Read freeze status */
  223. rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no, &fstate, &pcierr,
  224. NULL);
  225. if (rc) {
  226. pr_warning("%s: Can't read EEH status (PE#%d) for "
  227. "%s, err %lld\n",
  228. __func__, pe_no, dn->full_name, rc);
  229. return;
  230. }
  231. cfg_dbg(" -> EEH check, bdfn=%04x PE#%d fstate=%x\n",
  232. (PCI_DN(dn)->busno << 8) | (PCI_DN(dn)->devfn),
  233. pe_no, fstate);
  234. if (fstate != 0)
  235. pnv_pci_handle_eeh_config(phb, pe_no);
  236. }
  237. int pnv_pci_cfg_read(struct device_node *dn,
  238. int where, int size, u32 *val)
  239. {
  240. struct pci_dn *pdn = PCI_DN(dn);
  241. struct pnv_phb *phb = pdn->phb->private_data;
  242. u32 bdfn = (pdn->busno << 8) | pdn->devfn;
  243. #ifdef CONFIG_EEH
  244. struct eeh_pe *phb_pe = NULL;
  245. #endif
  246. s64 rc;
  247. switch (size) {
  248. case 1: {
  249. u8 v8;
  250. rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
  251. *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
  252. break;
  253. }
  254. case 2: {
  255. __be16 v16;
  256. rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
  257. &v16);
  258. *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
  259. break;
  260. }
  261. case 4: {
  262. __be32 v32;
  263. rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
  264. *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
  265. break;
  266. }
  267. default:
  268. return PCIBIOS_FUNC_NOT_SUPPORTED;
  269. }
  270. cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
  271. __func__, pdn->busno, pdn->devfn, where, size, *val);
  272. /*
  273. * Check if the specified PE has been put into frozen
  274. * state. On the other hand, we needn't do that while
  275. * the PHB has been put into frozen state because of
  276. * PHB-fatal errors.
  277. */
  278. #ifdef CONFIG_EEH
  279. phb_pe = eeh_phb_pe_get(pdn->phb);
  280. if (phb_pe && (phb_pe->state & EEH_PE_ISOLATED))
  281. return PCIBIOS_SUCCESSFUL;
  282. if (phb->eeh_state & PNV_EEH_STATE_ENABLED) {
  283. if (*val == EEH_IO_ERROR_VALUE(size) &&
  284. eeh_dev_check_failure(of_node_to_eeh_dev(dn)))
  285. return PCIBIOS_DEVICE_NOT_FOUND;
  286. } else {
  287. pnv_pci_config_check_eeh(phb, dn);
  288. }
  289. #else
  290. pnv_pci_config_check_eeh(phb, dn);
  291. #endif
  292. return PCIBIOS_SUCCESSFUL;
  293. }
  294. int pnv_pci_cfg_write(struct device_node *dn,
  295. int where, int size, u32 val)
  296. {
  297. struct pci_dn *pdn = PCI_DN(dn);
  298. struct pnv_phb *phb = pdn->phb->private_data;
  299. u32 bdfn = (pdn->busno << 8) | pdn->devfn;
  300. cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
  301. pdn->busno, pdn->devfn, where, size, val);
  302. switch (size) {
  303. case 1:
  304. opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
  305. break;
  306. case 2:
  307. opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
  308. break;
  309. case 4:
  310. opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
  311. break;
  312. default:
  313. return PCIBIOS_FUNC_NOT_SUPPORTED;
  314. }
  315. /* Check if the PHB got frozen due to an error (no response) */
  316. #ifdef CONFIG_EEH
  317. if (!(phb->eeh_state & PNV_EEH_STATE_ENABLED))
  318. pnv_pci_config_check_eeh(phb, dn);
  319. #else
  320. pnv_pci_config_check_eeh(phb, dn);
  321. #endif
  322. return PCIBIOS_SUCCESSFUL;
  323. }
  324. static int pnv_pci_read_config(struct pci_bus *bus,
  325. unsigned int devfn,
  326. int where, int size, u32 *val)
  327. {
  328. struct device_node *dn, *busdn = pci_bus_to_OF_node(bus);
  329. struct pci_dn *pdn;
  330. for (dn = busdn->child; dn; dn = dn->sibling) {
  331. pdn = PCI_DN(dn);
  332. if (pdn && pdn->devfn == devfn)
  333. return pnv_pci_cfg_read(dn, where, size, val);
  334. }
  335. *val = 0xFFFFFFFF;
  336. return PCIBIOS_DEVICE_NOT_FOUND;
  337. }
  338. static int pnv_pci_write_config(struct pci_bus *bus,
  339. unsigned int devfn,
  340. int where, int size, u32 val)
  341. {
  342. struct device_node *dn, *busdn = pci_bus_to_OF_node(bus);
  343. struct pci_dn *pdn;
  344. for (dn = busdn->child; dn; dn = dn->sibling) {
  345. pdn = PCI_DN(dn);
  346. if (pdn && pdn->devfn == devfn)
  347. return pnv_pci_cfg_write(dn, where, size, val);
  348. }
  349. return PCIBIOS_DEVICE_NOT_FOUND;
  350. }
  351. struct pci_ops pnv_pci_ops = {
  352. .read = pnv_pci_read_config,
  353. .write = pnv_pci_write_config,
  354. };
  355. static int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
  356. unsigned long uaddr, enum dma_data_direction direction,
  357. struct dma_attrs *attrs, bool rm)
  358. {
  359. u64 proto_tce;
  360. __be64 *tcep, *tces;
  361. u64 rpn;
  362. proto_tce = TCE_PCI_READ; // Read allowed
  363. if (direction != DMA_TO_DEVICE)
  364. proto_tce |= TCE_PCI_WRITE;
  365. tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset;
  366. rpn = __pa(uaddr) >> TCE_SHIFT;
  367. while (npages--)
  368. *(tcep++) = cpu_to_be64(proto_tce | (rpn++ << TCE_RPN_SHIFT));
  369. /* Some implementations won't cache invalid TCEs and thus may not
  370. * need that flush. We'll probably turn it_type into a bit mask
  371. * of flags if that becomes the case
  372. */
  373. if (tbl->it_type & TCE_PCI_SWINV_CREATE)
  374. pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm);
  375. return 0;
  376. }
  377. static int pnv_tce_build_vm(struct iommu_table *tbl, long index, long npages,
  378. unsigned long uaddr,
  379. enum dma_data_direction direction,
  380. struct dma_attrs *attrs)
  381. {
  382. return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs,
  383. false);
  384. }
  385. static void pnv_tce_free(struct iommu_table *tbl, long index, long npages,
  386. bool rm)
  387. {
  388. __be64 *tcep, *tces;
  389. tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset;
  390. while (npages--)
  391. *(tcep++) = cpu_to_be64(0);
  392. if (tbl->it_type & TCE_PCI_SWINV_FREE)
  393. pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm);
  394. }
  395. static void pnv_tce_free_vm(struct iommu_table *tbl, long index, long npages)
  396. {
  397. pnv_tce_free(tbl, index, npages, false);
  398. }
  399. static unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
  400. {
  401. return ((u64 *)tbl->it_base)[index - tbl->it_offset];
  402. }
  403. static int pnv_tce_build_rm(struct iommu_table *tbl, long index, long npages,
  404. unsigned long uaddr,
  405. enum dma_data_direction direction,
  406. struct dma_attrs *attrs)
  407. {
  408. return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs, true);
  409. }
  410. static void pnv_tce_free_rm(struct iommu_table *tbl, long index, long npages)
  411. {
  412. pnv_tce_free(tbl, index, npages, true);
  413. }
  414. void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
  415. void *tce_mem, u64 tce_size,
  416. u64 dma_offset)
  417. {
  418. tbl->it_blocksize = 16;
  419. tbl->it_base = (unsigned long)tce_mem;
  420. tbl->it_offset = dma_offset >> IOMMU_PAGE_SHIFT;
  421. tbl->it_index = 0;
  422. tbl->it_size = tce_size >> 3;
  423. tbl->it_busno = 0;
  424. tbl->it_type = TCE_PCI;
  425. }
  426. static struct iommu_table *pnv_pci_setup_bml_iommu(struct pci_controller *hose)
  427. {
  428. struct iommu_table *tbl;
  429. const __be64 *basep, *swinvp;
  430. const __be32 *sizep;
  431. basep = of_get_property(hose->dn, "linux,tce-base", NULL);
  432. sizep = of_get_property(hose->dn, "linux,tce-size", NULL);
  433. if (basep == NULL || sizep == NULL) {
  434. pr_err("PCI: %s has missing tce entries !\n",
  435. hose->dn->full_name);
  436. return NULL;
  437. }
  438. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, hose->node);
  439. if (WARN_ON(!tbl))
  440. return NULL;
  441. pnv_pci_setup_iommu_table(tbl, __va(be64_to_cpup(basep)),
  442. be32_to_cpup(sizep), 0);
  443. iommu_init_table(tbl, hose->node);
  444. iommu_register_group(tbl, pci_domain_nr(hose->bus), 0);
  445. /* Deal with SW invalidated TCEs when needed (BML way) */
  446. swinvp = of_get_property(hose->dn, "linux,tce-sw-invalidate-info",
  447. NULL);
  448. if (swinvp) {
  449. tbl->it_busno = be64_to_cpu(swinvp[1]);
  450. tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
  451. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
  452. }
  453. return tbl;
  454. }
  455. static void pnv_pci_dma_fallback_setup(struct pci_controller *hose,
  456. struct pci_dev *pdev)
  457. {
  458. struct device_node *np = pci_bus_to_OF_node(hose->bus);
  459. struct pci_dn *pdn;
  460. if (np == NULL)
  461. return;
  462. pdn = PCI_DN(np);
  463. if (!pdn->iommu_table)
  464. pdn->iommu_table = pnv_pci_setup_bml_iommu(hose);
  465. if (!pdn->iommu_table)
  466. return;
  467. set_iommu_table_base(&pdev->dev, pdn->iommu_table);
  468. }
  469. static void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
  470. {
  471. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  472. struct pnv_phb *phb = hose->private_data;
  473. /* If we have no phb structure, try to setup a fallback based on
  474. * the device-tree (RTAS PCI for example)
  475. */
  476. if (phb && phb->dma_dev_setup)
  477. phb->dma_dev_setup(phb, pdev);
  478. else
  479. pnv_pci_dma_fallback_setup(hose, pdev);
  480. }
  481. void pnv_pci_shutdown(void)
  482. {
  483. struct pci_controller *hose;
  484. list_for_each_entry(hose, &hose_list, list_node) {
  485. struct pnv_phb *phb = hose->private_data;
  486. if (phb && phb->shutdown)
  487. phb->shutdown(phb);
  488. }
  489. }
  490. /* Fixup wrong class code in p7ioc and p8 root complex */
  491. static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
  492. {
  493. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  494. }
  495. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
  496. static int pnv_pci_probe_mode(struct pci_bus *bus)
  497. {
  498. struct pci_controller *hose = pci_bus_to_host(bus);
  499. const __be64 *tstamp;
  500. u64 now, target;
  501. /* We hijack this as a way to ensure we have waited long
  502. * enough since the reset was lifted on the PCI bus
  503. */
  504. if (bus != hose->bus)
  505. return PCI_PROBE_NORMAL;
  506. tstamp = of_get_property(hose->dn, "reset-clear-timestamp", NULL);
  507. if (!tstamp || !*tstamp)
  508. return PCI_PROBE_NORMAL;
  509. now = mftb() / tb_ticks_per_usec;
  510. target = (be64_to_cpup(tstamp) / tb_ticks_per_usec)
  511. + PCI_RESET_DELAY_US;
  512. pr_devel("pci %04d: Reset target: 0x%llx now: 0x%llx\n",
  513. hose->global_number, target, now);
  514. if (now < target)
  515. msleep((target - now + 999) / 1000);
  516. return PCI_PROBE_NORMAL;
  517. }
  518. void __init pnv_pci_init(void)
  519. {
  520. struct device_node *np;
  521. pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
  522. /* OPAL absent, try POPAL first then RTAS detection of PHBs */
  523. if (!firmware_has_feature(FW_FEATURE_OPAL)) {
  524. #ifdef CONFIG_PPC_POWERNV_RTAS
  525. init_pci_config_tokens();
  526. find_and_init_phbs();
  527. #endif /* CONFIG_PPC_POWERNV_RTAS */
  528. }
  529. /* OPAL is here, do our normal stuff */
  530. else {
  531. int found_ioda = 0;
  532. /* Look for IODA IO-Hubs. We don't support mixing IODA
  533. * and p5ioc2 due to the need to change some global
  534. * probing flags
  535. */
  536. for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
  537. pnv_pci_init_ioda_hub(np);
  538. found_ioda = 1;
  539. }
  540. /* Look for p5ioc2 IO-Hubs */
  541. if (!found_ioda)
  542. for_each_compatible_node(np, NULL, "ibm,p5ioc2")
  543. pnv_pci_init_p5ioc2_hub(np);
  544. /* Look for ioda2 built-in PHB3's */
  545. for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
  546. pnv_pci_init_ioda2_phb(np);
  547. }
  548. /* Setup the linkage between OF nodes and PHBs */
  549. pci_devs_phb_init();
  550. /* Configure IOMMU DMA hooks */
  551. ppc_md.pci_dma_dev_setup = pnv_pci_dma_dev_setup;
  552. ppc_md.tce_build = pnv_tce_build_vm;
  553. ppc_md.tce_free = pnv_tce_free_vm;
  554. ppc_md.tce_build_rm = pnv_tce_build_rm;
  555. ppc_md.tce_free_rm = pnv_tce_free_rm;
  556. ppc_md.tce_get = pnv_tce_get;
  557. ppc_md.pci_probe_mode = pnv_pci_probe_mode;
  558. set_pci_dma_ops(&dma_iommu_ops);
  559. /* Configure MSIs */
  560. #ifdef CONFIG_PCI_MSI
  561. ppc_md.msi_check_device = pnv_msi_check_device;
  562. ppc_md.setup_msi_irqs = pnv_setup_msi_irqs;
  563. ppc_md.teardown_msi_irqs = pnv_teardown_msi_irqs;
  564. #endif
  565. }