pci.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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/irq.h>
  19. #include <linux/io.h>
  20. #include <linux/msi.h>
  21. #include <linux/iommu.h>
  22. #include <asm/sections.h>
  23. #include <asm/io.h>
  24. #include <asm/prom.h>
  25. #include <asm/pci-bridge.h>
  26. #include <asm/machdep.h>
  27. #include <asm/msi_bitmap.h>
  28. #include <asm/ppc-pci.h>
  29. #include <asm/opal.h>
  30. #include <asm/iommu.h>
  31. #include <asm/tce.h>
  32. #include <asm/firmware.h>
  33. #include <asm/eeh_event.h>
  34. #include <asm/eeh.h>
  35. #include "powernv.h"
  36. #include "pci.h"
  37. /* Delay in usec */
  38. #define PCI_RESET_DELAY_US 3000000
  39. #define cfg_dbg(fmt...) do { } while(0)
  40. //#define cfg_dbg(fmt...) printk(fmt)
  41. #ifdef CONFIG_PCI_MSI
  42. int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  43. {
  44. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  45. struct pnv_phb *phb = hose->private_data;
  46. struct msi_desc *entry;
  47. struct msi_msg msg;
  48. int hwirq;
  49. unsigned int virq;
  50. int rc;
  51. if (WARN_ON(!phb) || !phb->msi_bmp.bitmap)
  52. return -ENODEV;
  53. if (pdev->no_64bit_msi && !phb->msi32_support)
  54. return -ENODEV;
  55. for_each_pci_msi_entry(entry, pdev) {
  56. if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
  57. pr_warn("%s: Supports only 64-bit MSIs\n",
  58. pci_name(pdev));
  59. return -ENXIO;
  60. }
  61. hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1);
  62. if (hwirq < 0) {
  63. pr_warn("%s: Failed to find a free MSI\n",
  64. pci_name(pdev));
  65. return -ENOSPC;
  66. }
  67. virq = irq_create_mapping(NULL, phb->msi_base + hwirq);
  68. if (virq == NO_IRQ) {
  69. pr_warn("%s: Failed to map MSI to linux irq\n",
  70. pci_name(pdev));
  71. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
  72. return -ENOMEM;
  73. }
  74. rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq,
  75. virq, entry->msi_attrib.is_64, &msg);
  76. if (rc) {
  77. pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
  78. irq_dispose_mapping(virq);
  79. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
  80. return rc;
  81. }
  82. irq_set_msi_desc(virq, entry);
  83. pci_write_msi_msg(virq, &msg);
  84. }
  85. return 0;
  86. }
  87. void pnv_teardown_msi_irqs(struct pci_dev *pdev)
  88. {
  89. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  90. struct pnv_phb *phb = hose->private_data;
  91. struct msi_desc *entry;
  92. irq_hw_number_t hwirq;
  93. if (WARN_ON(!phb))
  94. return;
  95. for_each_pci_msi_entry(entry, pdev) {
  96. if (entry->irq == NO_IRQ)
  97. continue;
  98. hwirq = virq_to_hw(entry->irq);
  99. irq_set_msi_desc(entry->irq, NULL);
  100. irq_dispose_mapping(entry->irq);
  101. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, 1);
  102. }
  103. }
  104. #endif /* CONFIG_PCI_MSI */
  105. static void pnv_pci_dump_p7ioc_diag_data(struct pci_controller *hose,
  106. struct OpalIoPhbErrorCommon *common)
  107. {
  108. struct OpalIoP7IOCPhbErrorData *data;
  109. int i;
  110. data = (struct OpalIoP7IOCPhbErrorData *)common;
  111. pr_info("P7IOC PHB#%d Diag-data (Version: %d)\n",
  112. hose->global_number, be32_to_cpu(common->version));
  113. if (data->brdgCtl)
  114. pr_info("brdgCtl: %08x\n",
  115. be32_to_cpu(data->brdgCtl));
  116. if (data->portStatusReg || data->rootCmplxStatus ||
  117. data->busAgentStatus)
  118. pr_info("UtlSts: %08x %08x %08x\n",
  119. be32_to_cpu(data->portStatusReg),
  120. be32_to_cpu(data->rootCmplxStatus),
  121. be32_to_cpu(data->busAgentStatus));
  122. if (data->deviceStatus || data->slotStatus ||
  123. data->linkStatus || data->devCmdStatus ||
  124. data->devSecStatus)
  125. pr_info("RootSts: %08x %08x %08x %08x %08x\n",
  126. be32_to_cpu(data->deviceStatus),
  127. be32_to_cpu(data->slotStatus),
  128. be32_to_cpu(data->linkStatus),
  129. be32_to_cpu(data->devCmdStatus),
  130. be32_to_cpu(data->devSecStatus));
  131. if (data->rootErrorStatus || data->uncorrErrorStatus ||
  132. data->corrErrorStatus)
  133. pr_info("RootErrSts: %08x %08x %08x\n",
  134. be32_to_cpu(data->rootErrorStatus),
  135. be32_to_cpu(data->uncorrErrorStatus),
  136. be32_to_cpu(data->corrErrorStatus));
  137. if (data->tlpHdr1 || data->tlpHdr2 ||
  138. data->tlpHdr3 || data->tlpHdr4)
  139. pr_info("RootErrLog: %08x %08x %08x %08x\n",
  140. be32_to_cpu(data->tlpHdr1),
  141. be32_to_cpu(data->tlpHdr2),
  142. be32_to_cpu(data->tlpHdr3),
  143. be32_to_cpu(data->tlpHdr4));
  144. if (data->sourceId || data->errorClass ||
  145. data->correlator)
  146. pr_info("RootErrLog1: %08x %016llx %016llx\n",
  147. be32_to_cpu(data->sourceId),
  148. be64_to_cpu(data->errorClass),
  149. be64_to_cpu(data->correlator));
  150. if (data->p7iocPlssr || data->p7iocCsr)
  151. pr_info("PhbSts: %016llx %016llx\n",
  152. be64_to_cpu(data->p7iocPlssr),
  153. be64_to_cpu(data->p7iocCsr));
  154. if (data->lemFir)
  155. pr_info("Lem: %016llx %016llx %016llx\n",
  156. be64_to_cpu(data->lemFir),
  157. be64_to_cpu(data->lemErrorMask),
  158. be64_to_cpu(data->lemWOF));
  159. if (data->phbErrorStatus)
  160. pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
  161. be64_to_cpu(data->phbErrorStatus),
  162. be64_to_cpu(data->phbFirstErrorStatus),
  163. be64_to_cpu(data->phbErrorLog0),
  164. be64_to_cpu(data->phbErrorLog1));
  165. if (data->mmioErrorStatus)
  166. pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
  167. be64_to_cpu(data->mmioErrorStatus),
  168. be64_to_cpu(data->mmioFirstErrorStatus),
  169. be64_to_cpu(data->mmioErrorLog0),
  170. be64_to_cpu(data->mmioErrorLog1));
  171. if (data->dma0ErrorStatus)
  172. pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
  173. be64_to_cpu(data->dma0ErrorStatus),
  174. be64_to_cpu(data->dma0FirstErrorStatus),
  175. be64_to_cpu(data->dma0ErrorLog0),
  176. be64_to_cpu(data->dma0ErrorLog1));
  177. if (data->dma1ErrorStatus)
  178. pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
  179. be64_to_cpu(data->dma1ErrorStatus),
  180. be64_to_cpu(data->dma1FirstErrorStatus),
  181. be64_to_cpu(data->dma1ErrorLog0),
  182. be64_to_cpu(data->dma1ErrorLog1));
  183. for (i = 0; i < OPAL_P7IOC_NUM_PEST_REGS; i++) {
  184. if ((be64_to_cpu(data->pestA[i]) >> 63) == 0 &&
  185. (be64_to_cpu(data->pestB[i]) >> 63) == 0)
  186. continue;
  187. pr_info("PE[%3d] A/B: %016llx %016llx\n",
  188. i, be64_to_cpu(data->pestA[i]),
  189. be64_to_cpu(data->pestB[i]));
  190. }
  191. }
  192. static void pnv_pci_dump_phb3_diag_data(struct pci_controller *hose,
  193. struct OpalIoPhbErrorCommon *common)
  194. {
  195. struct OpalIoPhb3ErrorData *data;
  196. int i;
  197. data = (struct OpalIoPhb3ErrorData*)common;
  198. pr_info("PHB3 PHB#%d Diag-data (Version: %d)\n",
  199. hose->global_number, be32_to_cpu(common->version));
  200. if (data->brdgCtl)
  201. pr_info("brdgCtl: %08x\n",
  202. be32_to_cpu(data->brdgCtl));
  203. if (data->portStatusReg || data->rootCmplxStatus ||
  204. data->busAgentStatus)
  205. pr_info("UtlSts: %08x %08x %08x\n",
  206. be32_to_cpu(data->portStatusReg),
  207. be32_to_cpu(data->rootCmplxStatus),
  208. be32_to_cpu(data->busAgentStatus));
  209. if (data->deviceStatus || data->slotStatus ||
  210. data->linkStatus || data->devCmdStatus ||
  211. data->devSecStatus)
  212. pr_info("RootSts: %08x %08x %08x %08x %08x\n",
  213. be32_to_cpu(data->deviceStatus),
  214. be32_to_cpu(data->slotStatus),
  215. be32_to_cpu(data->linkStatus),
  216. be32_to_cpu(data->devCmdStatus),
  217. be32_to_cpu(data->devSecStatus));
  218. if (data->rootErrorStatus || data->uncorrErrorStatus ||
  219. data->corrErrorStatus)
  220. pr_info("RootErrSts: %08x %08x %08x\n",
  221. be32_to_cpu(data->rootErrorStatus),
  222. be32_to_cpu(data->uncorrErrorStatus),
  223. be32_to_cpu(data->corrErrorStatus));
  224. if (data->tlpHdr1 || data->tlpHdr2 ||
  225. data->tlpHdr3 || data->tlpHdr4)
  226. pr_info("RootErrLog: %08x %08x %08x %08x\n",
  227. be32_to_cpu(data->tlpHdr1),
  228. be32_to_cpu(data->tlpHdr2),
  229. be32_to_cpu(data->tlpHdr3),
  230. be32_to_cpu(data->tlpHdr4));
  231. if (data->sourceId || data->errorClass ||
  232. data->correlator)
  233. pr_info("RootErrLog1: %08x %016llx %016llx\n",
  234. be32_to_cpu(data->sourceId),
  235. be64_to_cpu(data->errorClass),
  236. be64_to_cpu(data->correlator));
  237. if (data->nFir)
  238. pr_info("nFir: %016llx %016llx %016llx\n",
  239. be64_to_cpu(data->nFir),
  240. be64_to_cpu(data->nFirMask),
  241. be64_to_cpu(data->nFirWOF));
  242. if (data->phbPlssr || data->phbCsr)
  243. pr_info("PhbSts: %016llx %016llx\n",
  244. be64_to_cpu(data->phbPlssr),
  245. be64_to_cpu(data->phbCsr));
  246. if (data->lemFir)
  247. pr_info("Lem: %016llx %016llx %016llx\n",
  248. be64_to_cpu(data->lemFir),
  249. be64_to_cpu(data->lemErrorMask),
  250. be64_to_cpu(data->lemWOF));
  251. if (data->phbErrorStatus)
  252. pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
  253. be64_to_cpu(data->phbErrorStatus),
  254. be64_to_cpu(data->phbFirstErrorStatus),
  255. be64_to_cpu(data->phbErrorLog0),
  256. be64_to_cpu(data->phbErrorLog1));
  257. if (data->mmioErrorStatus)
  258. pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
  259. be64_to_cpu(data->mmioErrorStatus),
  260. be64_to_cpu(data->mmioFirstErrorStatus),
  261. be64_to_cpu(data->mmioErrorLog0),
  262. be64_to_cpu(data->mmioErrorLog1));
  263. if (data->dma0ErrorStatus)
  264. pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
  265. be64_to_cpu(data->dma0ErrorStatus),
  266. be64_to_cpu(data->dma0FirstErrorStatus),
  267. be64_to_cpu(data->dma0ErrorLog0),
  268. be64_to_cpu(data->dma0ErrorLog1));
  269. if (data->dma1ErrorStatus)
  270. pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
  271. be64_to_cpu(data->dma1ErrorStatus),
  272. be64_to_cpu(data->dma1FirstErrorStatus),
  273. be64_to_cpu(data->dma1ErrorLog0),
  274. be64_to_cpu(data->dma1ErrorLog1));
  275. for (i = 0; i < OPAL_PHB3_NUM_PEST_REGS; i++) {
  276. if ((be64_to_cpu(data->pestA[i]) >> 63) == 0 &&
  277. (be64_to_cpu(data->pestB[i]) >> 63) == 0)
  278. continue;
  279. pr_info("PE[%3d] A/B: %016llx %016llx\n",
  280. i, be64_to_cpu(data->pestA[i]),
  281. be64_to_cpu(data->pestB[i]));
  282. }
  283. }
  284. void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
  285. unsigned char *log_buff)
  286. {
  287. struct OpalIoPhbErrorCommon *common;
  288. if (!hose || !log_buff)
  289. return;
  290. common = (struct OpalIoPhbErrorCommon *)log_buff;
  291. switch (be32_to_cpu(common->ioType)) {
  292. case OPAL_PHB_ERROR_DATA_TYPE_P7IOC:
  293. pnv_pci_dump_p7ioc_diag_data(hose, common);
  294. break;
  295. case OPAL_PHB_ERROR_DATA_TYPE_PHB3:
  296. pnv_pci_dump_phb3_diag_data(hose, common);
  297. break;
  298. default:
  299. pr_warn("%s: Unrecognized ioType %d\n",
  300. __func__, be32_to_cpu(common->ioType));
  301. }
  302. }
  303. static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
  304. {
  305. unsigned long flags, rc;
  306. int has_diag, ret = 0;
  307. spin_lock_irqsave(&phb->lock, flags);
  308. /* Fetch PHB diag-data */
  309. rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob,
  310. PNV_PCI_DIAG_BUF_SIZE);
  311. has_diag = (rc == OPAL_SUCCESS);
  312. /* If PHB supports compound PE, to handle it */
  313. if (phb->unfreeze_pe) {
  314. ret = phb->unfreeze_pe(phb,
  315. pe_no,
  316. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  317. } else {
  318. rc = opal_pci_eeh_freeze_clear(phb->opal_id,
  319. pe_no,
  320. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  321. if (rc) {
  322. pr_warn("%s: Failure %ld clearing frozen "
  323. "PHB#%x-PE#%x\n",
  324. __func__, rc, phb->hose->global_number,
  325. pe_no);
  326. ret = -EIO;
  327. }
  328. }
  329. /*
  330. * For now, let's only display the diag buffer when we fail to clear
  331. * the EEH status. We'll do more sensible things later when we have
  332. * proper EEH support. We need to make sure we don't pollute ourselves
  333. * with the normal errors generated when probing empty slots
  334. */
  335. if (has_diag && ret)
  336. pnv_pci_dump_phb_diag_data(phb->hose, phb->diag.blob);
  337. spin_unlock_irqrestore(&phb->lock, flags);
  338. }
  339. static void pnv_pci_config_check_eeh(struct pci_dn *pdn)
  340. {
  341. struct pnv_phb *phb = pdn->phb->private_data;
  342. u8 fstate;
  343. __be16 pcierr;
  344. int pe_no;
  345. s64 rc;
  346. /*
  347. * Get the PE#. During the PCI probe stage, we might not
  348. * setup that yet. So all ER errors should be mapped to
  349. * reserved PE.
  350. */
  351. pe_no = pdn->pe_number;
  352. if (pe_no == IODA_INVALID_PE) {
  353. if (phb->type == PNV_PHB_P5IOC2)
  354. pe_no = 0;
  355. else
  356. pe_no = phb->ioda.reserved_pe;
  357. }
  358. /*
  359. * Fetch frozen state. If the PHB support compound PE,
  360. * we need handle that case.
  361. */
  362. if (phb->get_pe_state) {
  363. fstate = phb->get_pe_state(phb, pe_no);
  364. } else {
  365. rc = opal_pci_eeh_freeze_status(phb->opal_id,
  366. pe_no,
  367. &fstate,
  368. &pcierr,
  369. NULL);
  370. if (rc) {
  371. pr_warn("%s: Failure %lld getting PHB#%x-PE#%x state\n",
  372. __func__, rc, phb->hose->global_number, pe_no);
  373. return;
  374. }
  375. }
  376. cfg_dbg(" -> EEH check, bdfn=%04x PE#%d fstate=%x\n",
  377. (pdn->busno << 8) | (pdn->devfn), pe_no, fstate);
  378. /* Clear the frozen state if applicable */
  379. if (fstate == OPAL_EEH_STOPPED_MMIO_FREEZE ||
  380. fstate == OPAL_EEH_STOPPED_DMA_FREEZE ||
  381. fstate == OPAL_EEH_STOPPED_MMIO_DMA_FREEZE) {
  382. /*
  383. * If PHB supports compound PE, freeze it for
  384. * consistency.
  385. */
  386. if (phb->freeze_pe)
  387. phb->freeze_pe(phb, pe_no);
  388. pnv_pci_handle_eeh_config(phb, pe_no);
  389. }
  390. }
  391. int pnv_pci_cfg_read(struct pci_dn *pdn,
  392. int where, int size, u32 *val)
  393. {
  394. struct pnv_phb *phb = pdn->phb->private_data;
  395. u32 bdfn = (pdn->busno << 8) | pdn->devfn;
  396. s64 rc;
  397. switch (size) {
  398. case 1: {
  399. u8 v8;
  400. rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
  401. *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
  402. break;
  403. }
  404. case 2: {
  405. __be16 v16;
  406. rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
  407. &v16);
  408. *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
  409. break;
  410. }
  411. case 4: {
  412. __be32 v32;
  413. rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
  414. *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
  415. break;
  416. }
  417. default:
  418. return PCIBIOS_FUNC_NOT_SUPPORTED;
  419. }
  420. cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
  421. __func__, pdn->busno, pdn->devfn, where, size, *val);
  422. return PCIBIOS_SUCCESSFUL;
  423. }
  424. int pnv_pci_cfg_write(struct pci_dn *pdn,
  425. int where, int size, u32 val)
  426. {
  427. struct pnv_phb *phb = pdn->phb->private_data;
  428. u32 bdfn = (pdn->busno << 8) | pdn->devfn;
  429. cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
  430. pdn->busno, pdn->devfn, where, size, val);
  431. switch (size) {
  432. case 1:
  433. opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
  434. break;
  435. case 2:
  436. opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
  437. break;
  438. case 4:
  439. opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
  440. break;
  441. default:
  442. return PCIBIOS_FUNC_NOT_SUPPORTED;
  443. }
  444. return PCIBIOS_SUCCESSFUL;
  445. }
  446. #if CONFIG_EEH
  447. static bool pnv_pci_cfg_check(struct pci_dn *pdn)
  448. {
  449. struct eeh_dev *edev = NULL;
  450. struct pnv_phb *phb = pdn->phb->private_data;
  451. /* EEH not enabled ? */
  452. if (!(phb->flags & PNV_PHB_FLAG_EEH))
  453. return true;
  454. /* PE reset or device removed ? */
  455. edev = pdn->edev;
  456. if (edev) {
  457. if (edev->pe &&
  458. (edev->pe->state & EEH_PE_CFG_BLOCKED))
  459. return false;
  460. if (edev->mode & EEH_DEV_REMOVED)
  461. return false;
  462. }
  463. return true;
  464. }
  465. #else
  466. static inline pnv_pci_cfg_check(struct pci_dn *pdn)
  467. {
  468. return true;
  469. }
  470. #endif /* CONFIG_EEH */
  471. static int pnv_pci_read_config(struct pci_bus *bus,
  472. unsigned int devfn,
  473. int where, int size, u32 *val)
  474. {
  475. struct pci_dn *pdn;
  476. struct pnv_phb *phb;
  477. int ret;
  478. *val = 0xFFFFFFFF;
  479. pdn = pci_get_pdn_by_devfn(bus, devfn);
  480. if (!pdn)
  481. return PCIBIOS_DEVICE_NOT_FOUND;
  482. if (!pnv_pci_cfg_check(pdn))
  483. return PCIBIOS_DEVICE_NOT_FOUND;
  484. ret = pnv_pci_cfg_read(pdn, where, size, val);
  485. phb = pdn->phb->private_data;
  486. if (phb->flags & PNV_PHB_FLAG_EEH && pdn->edev) {
  487. if (*val == EEH_IO_ERROR_VALUE(size) &&
  488. eeh_dev_check_failure(pdn->edev))
  489. return PCIBIOS_DEVICE_NOT_FOUND;
  490. } else {
  491. pnv_pci_config_check_eeh(pdn);
  492. }
  493. return ret;
  494. }
  495. static int pnv_pci_write_config(struct pci_bus *bus,
  496. unsigned int devfn,
  497. int where, int size, u32 val)
  498. {
  499. struct pci_dn *pdn;
  500. struct pnv_phb *phb;
  501. int ret;
  502. pdn = pci_get_pdn_by_devfn(bus, devfn);
  503. if (!pdn)
  504. return PCIBIOS_DEVICE_NOT_FOUND;
  505. if (!pnv_pci_cfg_check(pdn))
  506. return PCIBIOS_DEVICE_NOT_FOUND;
  507. ret = pnv_pci_cfg_write(pdn, where, size, val);
  508. phb = pdn->phb->private_data;
  509. if (!(phb->flags & PNV_PHB_FLAG_EEH))
  510. pnv_pci_config_check_eeh(pdn);
  511. return ret;
  512. }
  513. struct pci_ops pnv_pci_ops = {
  514. .read = pnv_pci_read_config,
  515. .write = pnv_pci_write_config,
  516. };
  517. static __be64 *pnv_tce(struct iommu_table *tbl, long idx)
  518. {
  519. __be64 *tmp = ((__be64 *)tbl->it_base);
  520. int level = tbl->it_indirect_levels;
  521. const long shift = ilog2(tbl->it_level_size);
  522. unsigned long mask = (tbl->it_level_size - 1) << (level * shift);
  523. while (level) {
  524. int n = (idx & mask) >> (level * shift);
  525. unsigned long tce = be64_to_cpu(tmp[n]);
  526. tmp = __va(tce & ~(TCE_PCI_READ | TCE_PCI_WRITE));
  527. idx &= ~mask;
  528. mask >>= shift;
  529. --level;
  530. }
  531. return tmp + idx;
  532. }
  533. int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
  534. unsigned long uaddr, enum dma_data_direction direction,
  535. struct dma_attrs *attrs)
  536. {
  537. u64 proto_tce = iommu_direction_to_tce_perm(direction);
  538. u64 rpn = __pa(uaddr) >> tbl->it_page_shift;
  539. long i;
  540. if (proto_tce & TCE_PCI_WRITE)
  541. proto_tce |= TCE_PCI_READ;
  542. for (i = 0; i < npages; i++) {
  543. unsigned long newtce = proto_tce |
  544. ((rpn + i) << tbl->it_page_shift);
  545. unsigned long idx = index - tbl->it_offset + i;
  546. *(pnv_tce(tbl, idx)) = cpu_to_be64(newtce);
  547. }
  548. return 0;
  549. }
  550. #ifdef CONFIG_IOMMU_API
  551. int pnv_tce_xchg(struct iommu_table *tbl, long index,
  552. unsigned long *hpa, enum dma_data_direction *direction)
  553. {
  554. u64 proto_tce = iommu_direction_to_tce_perm(*direction);
  555. unsigned long newtce = *hpa | proto_tce, oldtce;
  556. unsigned long idx = index - tbl->it_offset;
  557. BUG_ON(*hpa & ~IOMMU_PAGE_MASK(tbl));
  558. if (newtce & TCE_PCI_WRITE)
  559. newtce |= TCE_PCI_READ;
  560. oldtce = xchg(pnv_tce(tbl, idx), cpu_to_be64(newtce));
  561. *hpa = be64_to_cpu(oldtce) & ~(TCE_PCI_READ | TCE_PCI_WRITE);
  562. *direction = iommu_tce_direction(oldtce);
  563. return 0;
  564. }
  565. #endif
  566. void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
  567. {
  568. long i;
  569. for (i = 0; i < npages; i++) {
  570. unsigned long idx = index - tbl->it_offset + i;
  571. *(pnv_tce(tbl, idx)) = cpu_to_be64(0);
  572. }
  573. }
  574. unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
  575. {
  576. return *(pnv_tce(tbl, index - tbl->it_offset));
  577. }
  578. struct iommu_table *pnv_pci_table_alloc(int nid)
  579. {
  580. struct iommu_table *tbl;
  581. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, nid);
  582. INIT_LIST_HEAD_RCU(&tbl->it_group_list);
  583. return tbl;
  584. }
  585. long pnv_pci_link_table_and_group(int node, int num,
  586. struct iommu_table *tbl,
  587. struct iommu_table_group *table_group)
  588. {
  589. struct iommu_table_group_link *tgl = NULL;
  590. if (WARN_ON(!tbl || !table_group))
  591. return -EINVAL;
  592. tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,
  593. node);
  594. if (!tgl)
  595. return -ENOMEM;
  596. tgl->table_group = table_group;
  597. list_add_rcu(&tgl->next, &tbl->it_group_list);
  598. table_group->tables[num] = tbl;
  599. return 0;
  600. }
  601. static void pnv_iommu_table_group_link_free(struct rcu_head *head)
  602. {
  603. struct iommu_table_group_link *tgl = container_of(head,
  604. struct iommu_table_group_link, rcu);
  605. kfree(tgl);
  606. }
  607. void pnv_pci_unlink_table_and_group(struct iommu_table *tbl,
  608. struct iommu_table_group *table_group)
  609. {
  610. long i;
  611. bool found;
  612. struct iommu_table_group_link *tgl;
  613. if (!tbl || !table_group)
  614. return;
  615. /* Remove link to a group from table's list of attached groups */
  616. found = false;
  617. list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
  618. if (tgl->table_group == table_group) {
  619. list_del_rcu(&tgl->next);
  620. call_rcu(&tgl->rcu, pnv_iommu_table_group_link_free);
  621. found = true;
  622. break;
  623. }
  624. }
  625. if (WARN_ON(!found))
  626. return;
  627. /* Clean a pointer to iommu_table in iommu_table_group::tables[] */
  628. found = false;
  629. for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
  630. if (table_group->tables[i] == tbl) {
  631. table_group->tables[i] = NULL;
  632. found = true;
  633. break;
  634. }
  635. }
  636. WARN_ON(!found);
  637. }
  638. void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
  639. void *tce_mem, u64 tce_size,
  640. u64 dma_offset, unsigned page_shift)
  641. {
  642. tbl->it_blocksize = 16;
  643. tbl->it_base = (unsigned long)tce_mem;
  644. tbl->it_page_shift = page_shift;
  645. tbl->it_offset = dma_offset >> tbl->it_page_shift;
  646. tbl->it_index = 0;
  647. tbl->it_size = tce_size >> 3;
  648. tbl->it_busno = 0;
  649. tbl->it_type = TCE_PCI;
  650. }
  651. void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
  652. {
  653. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  654. struct pnv_phb *phb = hose->private_data;
  655. #ifdef CONFIG_PCI_IOV
  656. struct pnv_ioda_pe *pe;
  657. struct pci_dn *pdn;
  658. /* Fix the VF pdn PE number */
  659. if (pdev->is_virtfn) {
  660. pdn = pci_get_pdn(pdev);
  661. WARN_ON(pdn->pe_number != IODA_INVALID_PE);
  662. list_for_each_entry(pe, &phb->ioda.pe_list, list) {
  663. if (pe->rid == ((pdev->bus->number << 8) |
  664. (pdev->devfn & 0xff))) {
  665. pdn->pe_number = pe->pe_number;
  666. pe->pdev = pdev;
  667. break;
  668. }
  669. }
  670. }
  671. #endif /* CONFIG_PCI_IOV */
  672. if (phb && phb->dma_dev_setup)
  673. phb->dma_dev_setup(phb, pdev);
  674. }
  675. void pnv_pci_dma_bus_setup(struct pci_bus *bus)
  676. {
  677. struct pci_controller *hose = bus->sysdata;
  678. struct pnv_phb *phb = hose->private_data;
  679. struct pnv_ioda_pe *pe;
  680. list_for_each_entry(pe, &phb->ioda.pe_list, list) {
  681. if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)))
  682. continue;
  683. if (!pe->pbus)
  684. continue;
  685. if (bus->number == ((pe->rid >> 8) & 0xFF)) {
  686. pe->pbus = bus;
  687. break;
  688. }
  689. }
  690. }
  691. void pnv_pci_shutdown(void)
  692. {
  693. struct pci_controller *hose;
  694. list_for_each_entry(hose, &hose_list, list_node)
  695. if (hose->controller_ops.shutdown)
  696. hose->controller_ops.shutdown(hose);
  697. }
  698. /* Fixup wrong class code in p7ioc and p8 root complex */
  699. static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
  700. {
  701. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  702. }
  703. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
  704. void __init pnv_pci_init(void)
  705. {
  706. struct device_node *np;
  707. bool found_ioda = false;
  708. pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
  709. /* If we don't have OPAL, eg. in sim, just skip PCI probe */
  710. if (!firmware_has_feature(FW_FEATURE_OPAL))
  711. return;
  712. /* Look for IODA IO-Hubs. We don't support mixing IODA
  713. * and p5ioc2 due to the need to change some global
  714. * probing flags
  715. */
  716. for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
  717. pnv_pci_init_ioda_hub(np);
  718. found_ioda = true;
  719. }
  720. /* Look for p5ioc2 IO-Hubs */
  721. if (!found_ioda)
  722. for_each_compatible_node(np, NULL, "ibm,p5ioc2")
  723. pnv_pci_init_p5ioc2_hub(np);
  724. /* Look for ioda2 built-in PHB3's */
  725. for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
  726. pnv_pci_init_ioda2_phb(np);
  727. /* Setup the linkage between OF nodes and PHBs */
  728. pci_devs_phb_init();
  729. /* Configure IOMMU DMA hooks */
  730. set_pci_dma_ops(&dma_iommu_ops);
  731. }
  732. machine_subsys_initcall_sync(powernv, tce_iommu_bus_notifier_init);