pci.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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 pci_controller *hose,
  113. struct OpalIoPhbErrorCommon *common)
  114. {
  115. struct OpalIoP7IOCPhbErrorData *data;
  116. int i;
  117. data = (struct OpalIoP7IOCPhbErrorData *)common;
  118. pr_info("P7IOC PHB#%d Diag-data (Version: %d)\n",
  119. hose->global_number, be32_to_cpu(common->version));
  120. if (data->brdgCtl)
  121. pr_info("brdgCtl: %08x\n",
  122. be32_to_cpu(data->brdgCtl));
  123. if (data->portStatusReg || data->rootCmplxStatus ||
  124. data->busAgentStatus)
  125. pr_info("UtlSts: %08x %08x %08x\n",
  126. be32_to_cpu(data->portStatusReg),
  127. be32_to_cpu(data->rootCmplxStatus),
  128. be32_to_cpu(data->busAgentStatus));
  129. if (data->deviceStatus || data->slotStatus ||
  130. data->linkStatus || data->devCmdStatus ||
  131. data->devSecStatus)
  132. pr_info("RootSts: %08x %08x %08x %08x %08x\n",
  133. be32_to_cpu(data->deviceStatus),
  134. be32_to_cpu(data->slotStatus),
  135. be32_to_cpu(data->linkStatus),
  136. be32_to_cpu(data->devCmdStatus),
  137. be32_to_cpu(data->devSecStatus));
  138. if (data->rootErrorStatus || data->uncorrErrorStatus ||
  139. data->corrErrorStatus)
  140. pr_info("RootErrSts: %08x %08x %08x\n",
  141. be32_to_cpu(data->rootErrorStatus),
  142. be32_to_cpu(data->uncorrErrorStatus),
  143. be32_to_cpu(data->corrErrorStatus));
  144. if (data->tlpHdr1 || data->tlpHdr2 ||
  145. data->tlpHdr3 || data->tlpHdr4)
  146. pr_info("RootErrLog: %08x %08x %08x %08x\n",
  147. be32_to_cpu(data->tlpHdr1),
  148. be32_to_cpu(data->tlpHdr2),
  149. be32_to_cpu(data->tlpHdr3),
  150. be32_to_cpu(data->tlpHdr4));
  151. if (data->sourceId || data->errorClass ||
  152. data->correlator)
  153. pr_info("RootErrLog1: %08x %016llx %016llx\n",
  154. be32_to_cpu(data->sourceId),
  155. be64_to_cpu(data->errorClass),
  156. be64_to_cpu(data->correlator));
  157. if (data->p7iocPlssr || data->p7iocCsr)
  158. pr_info("PhbSts: %016llx %016llx\n",
  159. be64_to_cpu(data->p7iocPlssr),
  160. be64_to_cpu(data->p7iocCsr));
  161. if (data->lemFir)
  162. pr_info("Lem: %016llx %016llx %016llx\n",
  163. be64_to_cpu(data->lemFir),
  164. be64_to_cpu(data->lemErrorMask),
  165. be64_to_cpu(data->lemWOF));
  166. if (data->phbErrorStatus)
  167. pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
  168. be64_to_cpu(data->phbErrorStatus),
  169. be64_to_cpu(data->phbFirstErrorStatus),
  170. be64_to_cpu(data->phbErrorLog0),
  171. be64_to_cpu(data->phbErrorLog1));
  172. if (data->mmioErrorStatus)
  173. pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
  174. be64_to_cpu(data->mmioErrorStatus),
  175. be64_to_cpu(data->mmioFirstErrorStatus),
  176. be64_to_cpu(data->mmioErrorLog0),
  177. be64_to_cpu(data->mmioErrorLog1));
  178. if (data->dma0ErrorStatus)
  179. pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
  180. be64_to_cpu(data->dma0ErrorStatus),
  181. be64_to_cpu(data->dma0FirstErrorStatus),
  182. be64_to_cpu(data->dma0ErrorLog0),
  183. be64_to_cpu(data->dma0ErrorLog1));
  184. if (data->dma1ErrorStatus)
  185. pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
  186. be64_to_cpu(data->dma1ErrorStatus),
  187. be64_to_cpu(data->dma1FirstErrorStatus),
  188. be64_to_cpu(data->dma1ErrorLog0),
  189. be64_to_cpu(data->dma1ErrorLog1));
  190. for (i = 0; i < OPAL_P7IOC_NUM_PEST_REGS; i++) {
  191. if ((data->pestA[i] >> 63) == 0 &&
  192. (data->pestB[i] >> 63) == 0)
  193. continue;
  194. pr_info("PE[%3d] A/B: %016llx %016llx\n",
  195. i, be64_to_cpu(data->pestA[i]),
  196. be64_to_cpu(data->pestB[i]));
  197. }
  198. }
  199. static void pnv_pci_dump_phb3_diag_data(struct pci_controller *hose,
  200. struct OpalIoPhbErrorCommon *common)
  201. {
  202. struct OpalIoPhb3ErrorData *data;
  203. int i;
  204. data = (struct OpalIoPhb3ErrorData*)common;
  205. pr_info("PHB3 PHB#%d Diag-data (Version: %d)\n",
  206. hose->global_number, be32_to_cpu(common->version));
  207. if (data->brdgCtl)
  208. pr_info("brdgCtl: %08x\n",
  209. be32_to_cpu(data->brdgCtl));
  210. if (data->portStatusReg || data->rootCmplxStatus ||
  211. data->busAgentStatus)
  212. pr_info("UtlSts: %08x %08x %08x\n",
  213. be32_to_cpu(data->portStatusReg),
  214. be32_to_cpu(data->rootCmplxStatus),
  215. be32_to_cpu(data->busAgentStatus));
  216. if (data->deviceStatus || data->slotStatus ||
  217. data->linkStatus || data->devCmdStatus ||
  218. data->devSecStatus)
  219. pr_info("RootSts: %08x %08x %08x %08x %08x\n",
  220. be32_to_cpu(data->deviceStatus),
  221. be32_to_cpu(data->slotStatus),
  222. be32_to_cpu(data->linkStatus),
  223. be32_to_cpu(data->devCmdStatus),
  224. be32_to_cpu(data->devSecStatus));
  225. if (data->rootErrorStatus || data->uncorrErrorStatus ||
  226. data->corrErrorStatus)
  227. pr_info("RootErrSts: %08x %08x %08x\n",
  228. be32_to_cpu(data->rootErrorStatus),
  229. be32_to_cpu(data->uncorrErrorStatus),
  230. be32_to_cpu(data->corrErrorStatus));
  231. if (data->tlpHdr1 || data->tlpHdr2 ||
  232. data->tlpHdr3 || data->tlpHdr4)
  233. pr_info("RootErrLog: %08x %08x %08x %08x\n",
  234. be32_to_cpu(data->tlpHdr1),
  235. be32_to_cpu(data->tlpHdr2),
  236. be32_to_cpu(data->tlpHdr3),
  237. be32_to_cpu(data->tlpHdr4));
  238. if (data->sourceId || data->errorClass ||
  239. data->correlator)
  240. pr_info("RootErrLog1: %08x %016llx %016llx\n",
  241. be32_to_cpu(data->sourceId),
  242. be64_to_cpu(data->errorClass),
  243. be64_to_cpu(data->correlator));
  244. if (data->nFir)
  245. pr_info("nFir: %016llx %016llx %016llx\n",
  246. be64_to_cpu(data->nFir),
  247. be64_to_cpu(data->nFirMask),
  248. be64_to_cpu(data->nFirWOF));
  249. if (data->phbPlssr || data->phbCsr)
  250. pr_info("PhbSts: %016llx %016llx\n",
  251. be64_to_cpu(data->phbPlssr),
  252. be64_to_cpu(data->phbCsr));
  253. if (data->lemFir)
  254. pr_info("Lem: %016llx %016llx %016llx\n",
  255. be64_to_cpu(data->lemFir),
  256. be64_to_cpu(data->lemErrorMask),
  257. be64_to_cpu(data->lemWOF));
  258. if (data->phbErrorStatus)
  259. pr_info("PhbErr: %016llx %016llx %016llx %016llx\n",
  260. be64_to_cpu(data->phbErrorStatus),
  261. be64_to_cpu(data->phbFirstErrorStatus),
  262. be64_to_cpu(data->phbErrorLog0),
  263. be64_to_cpu(data->phbErrorLog1));
  264. if (data->mmioErrorStatus)
  265. pr_info("OutErr: %016llx %016llx %016llx %016llx\n",
  266. be64_to_cpu(data->mmioErrorStatus),
  267. be64_to_cpu(data->mmioFirstErrorStatus),
  268. be64_to_cpu(data->mmioErrorLog0),
  269. be64_to_cpu(data->mmioErrorLog1));
  270. if (data->dma0ErrorStatus)
  271. pr_info("InAErr: %016llx %016llx %016llx %016llx\n",
  272. be64_to_cpu(data->dma0ErrorStatus),
  273. be64_to_cpu(data->dma0FirstErrorStatus),
  274. be64_to_cpu(data->dma0ErrorLog0),
  275. be64_to_cpu(data->dma0ErrorLog1));
  276. if (data->dma1ErrorStatus)
  277. pr_info("InBErr: %016llx %016llx %016llx %016llx\n",
  278. be64_to_cpu(data->dma1ErrorStatus),
  279. be64_to_cpu(data->dma1FirstErrorStatus),
  280. be64_to_cpu(data->dma1ErrorLog0),
  281. be64_to_cpu(data->dma1ErrorLog1));
  282. for (i = 0; i < OPAL_PHB3_NUM_PEST_REGS; i++) {
  283. if ((be64_to_cpu(data->pestA[i]) >> 63) == 0 &&
  284. (be64_to_cpu(data->pestB[i]) >> 63) == 0)
  285. continue;
  286. pr_info("PE[%3d] A/B: %016llx %016llx\n",
  287. i, be64_to_cpu(data->pestA[i]),
  288. be64_to_cpu(data->pestB[i]));
  289. }
  290. }
  291. void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
  292. unsigned char *log_buff)
  293. {
  294. struct OpalIoPhbErrorCommon *common;
  295. if (!hose || !log_buff)
  296. return;
  297. common = (struct OpalIoPhbErrorCommon *)log_buff;
  298. switch (be32_to_cpu(common->ioType)) {
  299. case OPAL_PHB_ERROR_DATA_TYPE_P7IOC:
  300. pnv_pci_dump_p7ioc_diag_data(hose, common);
  301. break;
  302. case OPAL_PHB_ERROR_DATA_TYPE_PHB3:
  303. pnv_pci_dump_phb3_diag_data(hose, common);
  304. break;
  305. default:
  306. pr_warn("%s: Unrecognized ioType %d\n",
  307. __func__, be32_to_cpu(common->ioType));
  308. }
  309. }
  310. static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
  311. {
  312. unsigned long flags, rc;
  313. int has_diag, ret = 0;
  314. spin_lock_irqsave(&phb->lock, flags);
  315. /* Fetch PHB diag-data */
  316. rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob,
  317. PNV_PCI_DIAG_BUF_SIZE);
  318. has_diag = (rc == OPAL_SUCCESS);
  319. /* If PHB supports compound PE, to handle it */
  320. if (phb->unfreeze_pe) {
  321. ret = phb->unfreeze_pe(phb,
  322. pe_no,
  323. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  324. } else {
  325. rc = opal_pci_eeh_freeze_clear(phb->opal_id,
  326. pe_no,
  327. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  328. if (rc) {
  329. pr_warn("%s: Failure %ld clearing frozen "
  330. "PHB#%x-PE#%x\n",
  331. __func__, rc, phb->hose->global_number,
  332. pe_no);
  333. ret = -EIO;
  334. }
  335. }
  336. /*
  337. * For now, let's only display the diag buffer when we fail to clear
  338. * the EEH status. We'll do more sensible things later when we have
  339. * proper EEH support. We need to make sure we don't pollute ourselves
  340. * with the normal errors generated when probing empty slots
  341. */
  342. if (has_diag && ret)
  343. pnv_pci_dump_phb_diag_data(phb->hose, phb->diag.blob);
  344. spin_unlock_irqrestore(&phb->lock, flags);
  345. }
  346. static void pnv_pci_config_check_eeh(struct pnv_phb *phb,
  347. struct device_node *dn)
  348. {
  349. u8 fstate;
  350. __be16 pcierr;
  351. int pe_no;
  352. s64 rc;
  353. /*
  354. * Get the PE#. During the PCI probe stage, we might not
  355. * setup that yet. So all ER errors should be mapped to
  356. * reserved PE.
  357. */
  358. pe_no = PCI_DN(dn)->pe_number;
  359. if (pe_no == IODA_INVALID_PE) {
  360. if (phb->type == PNV_PHB_P5IOC2)
  361. pe_no = 0;
  362. else
  363. pe_no = phb->ioda.reserved_pe;
  364. }
  365. /*
  366. * Fetch frozen state. If the PHB support compound PE,
  367. * we need handle that case.
  368. */
  369. if (phb->get_pe_state) {
  370. fstate = phb->get_pe_state(phb, pe_no);
  371. } else {
  372. rc = opal_pci_eeh_freeze_status(phb->opal_id,
  373. pe_no,
  374. &fstate,
  375. &pcierr,
  376. NULL);
  377. if (rc) {
  378. pr_warn("%s: Failure %lld getting PHB#%x-PE#%x state\n",
  379. __func__, rc, phb->hose->global_number, pe_no);
  380. return;
  381. }
  382. }
  383. cfg_dbg(" -> EEH check, bdfn=%04x PE#%d fstate=%x\n",
  384. (PCI_DN(dn)->busno << 8) | (PCI_DN(dn)->devfn),
  385. pe_no, fstate);
  386. /* Clear the frozen state if applicable */
  387. if (fstate == OPAL_EEH_STOPPED_MMIO_FREEZE ||
  388. fstate == OPAL_EEH_STOPPED_DMA_FREEZE ||
  389. fstate == OPAL_EEH_STOPPED_MMIO_DMA_FREEZE) {
  390. /*
  391. * If PHB supports compound PE, freeze it for
  392. * consistency.
  393. */
  394. if (phb->freeze_pe)
  395. phb->freeze_pe(phb, pe_no);
  396. pnv_pci_handle_eeh_config(phb, pe_no);
  397. }
  398. }
  399. int pnv_pci_cfg_read(struct device_node *dn,
  400. int where, int size, u32 *val)
  401. {
  402. struct pci_dn *pdn = PCI_DN(dn);
  403. struct pnv_phb *phb = pdn->phb->private_data;
  404. u32 bdfn = (pdn->busno << 8) | pdn->devfn;
  405. s64 rc;
  406. switch (size) {
  407. case 1: {
  408. u8 v8;
  409. rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
  410. *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
  411. break;
  412. }
  413. case 2: {
  414. __be16 v16;
  415. rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
  416. &v16);
  417. *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
  418. break;
  419. }
  420. case 4: {
  421. __be32 v32;
  422. rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
  423. *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
  424. break;
  425. }
  426. default:
  427. return PCIBIOS_FUNC_NOT_SUPPORTED;
  428. }
  429. cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
  430. __func__, pdn->busno, pdn->devfn, where, size, *val);
  431. return PCIBIOS_SUCCESSFUL;
  432. }
  433. int pnv_pci_cfg_write(struct device_node *dn,
  434. int where, int size, u32 val)
  435. {
  436. struct pci_dn *pdn = PCI_DN(dn);
  437. struct pnv_phb *phb = pdn->phb->private_data;
  438. u32 bdfn = (pdn->busno << 8) | pdn->devfn;
  439. cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
  440. pdn->busno, pdn->devfn, where, size, val);
  441. switch (size) {
  442. case 1:
  443. opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
  444. break;
  445. case 2:
  446. opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
  447. break;
  448. case 4:
  449. opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
  450. break;
  451. default:
  452. return PCIBIOS_FUNC_NOT_SUPPORTED;
  453. }
  454. return PCIBIOS_SUCCESSFUL;
  455. }
  456. #if CONFIG_EEH
  457. static bool pnv_pci_cfg_check(struct pci_controller *hose,
  458. struct device_node *dn)
  459. {
  460. struct eeh_dev *edev = NULL;
  461. struct pnv_phb *phb = hose->private_data;
  462. /* EEH not enabled ? */
  463. if (!(phb->flags & PNV_PHB_FLAG_EEH))
  464. return true;
  465. /* PE reset or device removed ? */
  466. edev = of_node_to_eeh_dev(dn);
  467. if (edev) {
  468. if (edev->pe &&
  469. (edev->pe->state & EEH_PE_RESET))
  470. return false;
  471. if (edev->mode & EEH_DEV_REMOVED)
  472. return false;
  473. }
  474. return true;
  475. }
  476. #else
  477. static inline pnv_pci_cfg_check(struct pci_controller *hose,
  478. struct device_node *dn)
  479. {
  480. return true;
  481. }
  482. #endif /* CONFIG_EEH */
  483. static int pnv_pci_read_config(struct pci_bus *bus,
  484. unsigned int devfn,
  485. int where, int size, u32 *val)
  486. {
  487. struct device_node *dn, *busdn = pci_bus_to_OF_node(bus);
  488. struct pci_dn *pdn;
  489. struct pnv_phb *phb;
  490. bool found = false;
  491. int ret;
  492. *val = 0xFFFFFFFF;
  493. for (dn = busdn->child; dn; dn = dn->sibling) {
  494. pdn = PCI_DN(dn);
  495. if (pdn && pdn->devfn == devfn) {
  496. phb = pdn->phb->private_data;
  497. found = true;
  498. break;
  499. }
  500. }
  501. if (!found || !pnv_pci_cfg_check(pdn->phb, dn))
  502. return PCIBIOS_DEVICE_NOT_FOUND;
  503. ret = pnv_pci_cfg_read(dn, where, size, val);
  504. if (phb->flags & PNV_PHB_FLAG_EEH) {
  505. if (*val == EEH_IO_ERROR_VALUE(size) &&
  506. eeh_dev_check_failure(of_node_to_eeh_dev(dn)))
  507. return PCIBIOS_DEVICE_NOT_FOUND;
  508. } else {
  509. pnv_pci_config_check_eeh(phb, dn);
  510. }
  511. return ret;
  512. }
  513. static int pnv_pci_write_config(struct pci_bus *bus,
  514. unsigned int devfn,
  515. int where, int size, u32 val)
  516. {
  517. struct device_node *dn, *busdn = pci_bus_to_OF_node(bus);
  518. struct pci_dn *pdn;
  519. struct pnv_phb *phb;
  520. bool found = false;
  521. int ret;
  522. for (dn = busdn->child; dn; dn = dn->sibling) {
  523. pdn = PCI_DN(dn);
  524. if (pdn && pdn->devfn == devfn) {
  525. phb = pdn->phb->private_data;
  526. found = true;
  527. break;
  528. }
  529. }
  530. if (!found || !pnv_pci_cfg_check(pdn->phb, dn))
  531. return PCIBIOS_DEVICE_NOT_FOUND;
  532. ret = pnv_pci_cfg_write(dn, where, size, val);
  533. if (!(phb->flags & PNV_PHB_FLAG_EEH))
  534. pnv_pci_config_check_eeh(phb, dn);
  535. return ret;
  536. }
  537. struct pci_ops pnv_pci_ops = {
  538. .read = pnv_pci_read_config,
  539. .write = pnv_pci_write_config,
  540. };
  541. static int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
  542. unsigned long uaddr, enum dma_data_direction direction,
  543. struct dma_attrs *attrs, bool rm)
  544. {
  545. u64 proto_tce;
  546. __be64 *tcep, *tces;
  547. u64 rpn;
  548. proto_tce = TCE_PCI_READ; // Read allowed
  549. if (direction != DMA_TO_DEVICE)
  550. proto_tce |= TCE_PCI_WRITE;
  551. tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset;
  552. rpn = __pa(uaddr) >> tbl->it_page_shift;
  553. while (npages--)
  554. *(tcep++) = cpu_to_be64(proto_tce |
  555. (rpn++ << tbl->it_page_shift));
  556. /* Some implementations won't cache invalid TCEs and thus may not
  557. * need that flush. We'll probably turn it_type into a bit mask
  558. * of flags if that becomes the case
  559. */
  560. if (tbl->it_type & TCE_PCI_SWINV_CREATE)
  561. pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm);
  562. return 0;
  563. }
  564. static int pnv_tce_build_vm(struct iommu_table *tbl, long index, long npages,
  565. unsigned long uaddr,
  566. enum dma_data_direction direction,
  567. struct dma_attrs *attrs)
  568. {
  569. return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs,
  570. false);
  571. }
  572. static void pnv_tce_free(struct iommu_table *tbl, long index, long npages,
  573. bool rm)
  574. {
  575. __be64 *tcep, *tces;
  576. tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset;
  577. while (npages--)
  578. *(tcep++) = cpu_to_be64(0);
  579. if (tbl->it_type & TCE_PCI_SWINV_FREE)
  580. pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm);
  581. }
  582. static void pnv_tce_free_vm(struct iommu_table *tbl, long index, long npages)
  583. {
  584. pnv_tce_free(tbl, index, npages, false);
  585. }
  586. static unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
  587. {
  588. return ((u64 *)tbl->it_base)[index - tbl->it_offset];
  589. }
  590. static int pnv_tce_build_rm(struct iommu_table *tbl, long index, long npages,
  591. unsigned long uaddr,
  592. enum dma_data_direction direction,
  593. struct dma_attrs *attrs)
  594. {
  595. return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs, true);
  596. }
  597. static void pnv_tce_free_rm(struct iommu_table *tbl, long index, long npages)
  598. {
  599. pnv_tce_free(tbl, index, npages, true);
  600. }
  601. void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
  602. void *tce_mem, u64 tce_size,
  603. u64 dma_offset, unsigned page_shift)
  604. {
  605. tbl->it_blocksize = 16;
  606. tbl->it_base = (unsigned long)tce_mem;
  607. tbl->it_page_shift = page_shift;
  608. tbl->it_offset = dma_offset >> tbl->it_page_shift;
  609. tbl->it_index = 0;
  610. tbl->it_size = tce_size >> 3;
  611. tbl->it_busno = 0;
  612. tbl->it_type = TCE_PCI;
  613. }
  614. static struct iommu_table *pnv_pci_setup_bml_iommu(struct pci_controller *hose)
  615. {
  616. struct iommu_table *tbl;
  617. const __be64 *basep, *swinvp;
  618. const __be32 *sizep;
  619. basep = of_get_property(hose->dn, "linux,tce-base", NULL);
  620. sizep = of_get_property(hose->dn, "linux,tce-size", NULL);
  621. if (basep == NULL || sizep == NULL) {
  622. pr_err("PCI: %s has missing tce entries !\n",
  623. hose->dn->full_name);
  624. return NULL;
  625. }
  626. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, hose->node);
  627. if (WARN_ON(!tbl))
  628. return NULL;
  629. pnv_pci_setup_iommu_table(tbl, __va(be64_to_cpup(basep)),
  630. be32_to_cpup(sizep), 0, IOMMU_PAGE_SHIFT_4K);
  631. iommu_init_table(tbl, hose->node);
  632. iommu_register_group(tbl, pci_domain_nr(hose->bus), 0);
  633. /* Deal with SW invalidated TCEs when needed (BML way) */
  634. swinvp = of_get_property(hose->dn, "linux,tce-sw-invalidate-info",
  635. NULL);
  636. if (swinvp) {
  637. tbl->it_busno = be64_to_cpu(swinvp[1]);
  638. tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
  639. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
  640. }
  641. return tbl;
  642. }
  643. static void pnv_pci_dma_fallback_setup(struct pci_controller *hose,
  644. struct pci_dev *pdev)
  645. {
  646. struct device_node *np = pci_bus_to_OF_node(hose->bus);
  647. struct pci_dn *pdn;
  648. if (np == NULL)
  649. return;
  650. pdn = PCI_DN(np);
  651. if (!pdn->iommu_table)
  652. pdn->iommu_table = pnv_pci_setup_bml_iommu(hose);
  653. if (!pdn->iommu_table)
  654. return;
  655. set_iommu_table_base_and_group(&pdev->dev, pdn->iommu_table);
  656. }
  657. static void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
  658. {
  659. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  660. struct pnv_phb *phb = hose->private_data;
  661. /* If we have no phb structure, try to setup a fallback based on
  662. * the device-tree (RTAS PCI for example)
  663. */
  664. if (phb && phb->dma_dev_setup)
  665. phb->dma_dev_setup(phb, pdev);
  666. else
  667. pnv_pci_dma_fallback_setup(hose, pdev);
  668. }
  669. int pnv_pci_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
  670. {
  671. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  672. struct pnv_phb *phb = hose->private_data;
  673. if (phb && phb->dma_set_mask)
  674. return phb->dma_set_mask(phb, pdev, dma_mask);
  675. return __dma_set_mask(&pdev->dev, dma_mask);
  676. }
  677. void pnv_pci_shutdown(void)
  678. {
  679. struct pci_controller *hose;
  680. list_for_each_entry(hose, &hose_list, list_node) {
  681. struct pnv_phb *phb = hose->private_data;
  682. if (phb && phb->shutdown)
  683. phb->shutdown(phb);
  684. }
  685. }
  686. /* Fixup wrong class code in p7ioc and p8 root complex */
  687. static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
  688. {
  689. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  690. }
  691. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
  692. static int pnv_pci_probe_mode(struct pci_bus *bus)
  693. {
  694. struct pci_controller *hose = pci_bus_to_host(bus);
  695. const __be64 *tstamp;
  696. u64 now, target;
  697. /* We hijack this as a way to ensure we have waited long
  698. * enough since the reset was lifted on the PCI bus
  699. */
  700. if (bus != hose->bus)
  701. return PCI_PROBE_NORMAL;
  702. tstamp = of_get_property(hose->dn, "reset-clear-timestamp", NULL);
  703. if (!tstamp || !*tstamp)
  704. return PCI_PROBE_NORMAL;
  705. now = mftb() / tb_ticks_per_usec;
  706. target = (be64_to_cpup(tstamp) / tb_ticks_per_usec)
  707. + PCI_RESET_DELAY_US;
  708. pr_devel("pci %04d: Reset target: 0x%llx now: 0x%llx\n",
  709. hose->global_number, target, now);
  710. if (now < target)
  711. msleep((target - now + 999) / 1000);
  712. return PCI_PROBE_NORMAL;
  713. }
  714. void __init pnv_pci_init(void)
  715. {
  716. struct device_node *np;
  717. pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
  718. /* OPAL absent, try POPAL first then RTAS detection of PHBs */
  719. if (!firmware_has_feature(FW_FEATURE_OPAL)) {
  720. #ifdef CONFIG_PPC_POWERNV_RTAS
  721. init_pci_config_tokens();
  722. find_and_init_phbs();
  723. #endif /* CONFIG_PPC_POWERNV_RTAS */
  724. }
  725. /* OPAL is here, do our normal stuff */
  726. else {
  727. int found_ioda = 0;
  728. /* Look for IODA IO-Hubs. We don't support mixing IODA
  729. * and p5ioc2 due to the need to change some global
  730. * probing flags
  731. */
  732. for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
  733. pnv_pci_init_ioda_hub(np);
  734. found_ioda = 1;
  735. }
  736. /* Look for p5ioc2 IO-Hubs */
  737. if (!found_ioda)
  738. for_each_compatible_node(np, NULL, "ibm,p5ioc2")
  739. pnv_pci_init_p5ioc2_hub(np);
  740. /* Look for ioda2 built-in PHB3's */
  741. for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
  742. pnv_pci_init_ioda2_phb(np);
  743. }
  744. /* Setup the linkage between OF nodes and PHBs */
  745. pci_devs_phb_init();
  746. /* Configure IOMMU DMA hooks */
  747. ppc_md.pci_dma_dev_setup = pnv_pci_dma_dev_setup;
  748. ppc_md.tce_build = pnv_tce_build_vm;
  749. ppc_md.tce_free = pnv_tce_free_vm;
  750. ppc_md.tce_build_rm = pnv_tce_build_rm;
  751. ppc_md.tce_free_rm = pnv_tce_free_rm;
  752. ppc_md.tce_get = pnv_tce_get;
  753. ppc_md.pci_probe_mode = pnv_pci_probe_mode;
  754. set_pci_dma_ops(&dma_iommu_ops);
  755. /* Configure MSIs */
  756. #ifdef CONFIG_PCI_MSI
  757. ppc_md.msi_check_device = pnv_msi_check_device;
  758. ppc_md.setup_msi_irqs = pnv_setup_msi_irqs;
  759. ppc_md.teardown_msi_irqs = pnv_teardown_msi_irqs;
  760. #endif
  761. }
  762. static int tce_iommu_bus_notifier(struct notifier_block *nb,
  763. unsigned long action, void *data)
  764. {
  765. struct device *dev = data;
  766. switch (action) {
  767. case BUS_NOTIFY_ADD_DEVICE:
  768. return iommu_add_device(dev);
  769. case BUS_NOTIFY_DEL_DEVICE:
  770. if (dev->iommu_group)
  771. iommu_del_device(dev);
  772. return 0;
  773. default:
  774. return 0;
  775. }
  776. }
  777. static struct notifier_block tce_iommu_bus_nb = {
  778. .notifier_call = tce_iommu_bus_notifier,
  779. };
  780. static int __init tce_iommu_bus_notifier_init(void)
  781. {
  782. bus_register_notifier(&pci_bus_type, &tce_iommu_bus_nb);
  783. return 0;
  784. }
  785. machine_subsys_initcall_sync(powernv, tce_iommu_bus_notifier_init);