pci.c 24 KB

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