pci.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 msi_desc *entry;
  48. struct msi_msg msg;
  49. int hwirq;
  50. unsigned int virq;
  51. int rc;
  52. if (WARN_ON(!phb) || !phb->msi_bmp.bitmap)
  53. return -ENODEV;
  54. if (pdev->no_64bit_msi && !phb->msi32_support)
  55. return -ENODEV;
  56. list_for_each_entry(entry, &pdev->msi_list, list) {
  57. if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
  58. pr_warn("%s: Supports only 64-bit MSIs\n",
  59. pci_name(pdev));
  60. return -ENXIO;
  61. }
  62. hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1);
  63. if (hwirq < 0) {
  64. pr_warn("%s: Failed to find a free MSI\n",
  65. pci_name(pdev));
  66. return -ENOSPC;
  67. }
  68. virq = irq_create_mapping(NULL, phb->msi_base + hwirq);
  69. if (virq == NO_IRQ) {
  70. pr_warn("%s: Failed to map MSI to linux irq\n",
  71. pci_name(pdev));
  72. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
  73. return -ENOMEM;
  74. }
  75. rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq,
  76. virq, entry->msi_attrib.is_64, &msg);
  77. if (rc) {
  78. pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
  79. irq_dispose_mapping(virq);
  80. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
  81. return rc;
  82. }
  83. irq_set_msi_desc(virq, entry);
  84. write_msi_msg(virq, &msg);
  85. }
  86. return 0;
  87. }
  88. static void pnv_teardown_msi_irqs(struct pci_dev *pdev)
  89. {
  90. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  91. struct pnv_phb *phb = hose->private_data;
  92. struct msi_desc *entry;
  93. if (WARN_ON(!phb))
  94. return;
  95. list_for_each_entry(entry, &pdev->msi_list, list) {
  96. if (entry->irq == NO_IRQ)
  97. continue;
  98. irq_set_msi_desc(entry->irq, NULL);
  99. msi_bitmap_free_hwirqs(&phb->msi_bmp,
  100. virq_to_hw(entry->irq) - phb->msi_base, 1);
  101. irq_dispose_mapping(entry->irq);
  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 ((data->pestA[i] >> 63) == 0 &&
  185. (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 pnv_phb *phb,
  340. struct device_node *dn)
  341. {
  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 = PCI_DN(dn)->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. (PCI_DN(dn)->busno << 8) | (PCI_DN(dn)->devfn),
  378. pe_no, fstate);
  379. /* Clear the frozen state if applicable */
  380. if (fstate == OPAL_EEH_STOPPED_MMIO_FREEZE ||
  381. fstate == OPAL_EEH_STOPPED_DMA_FREEZE ||
  382. fstate == OPAL_EEH_STOPPED_MMIO_DMA_FREEZE) {
  383. /*
  384. * If PHB supports compound PE, freeze it for
  385. * consistency.
  386. */
  387. if (phb->freeze_pe)
  388. phb->freeze_pe(phb, pe_no);
  389. pnv_pci_handle_eeh_config(phb, pe_no);
  390. }
  391. }
  392. int pnv_pci_cfg_read(struct device_node *dn,
  393. int where, int size, u32 *val)
  394. {
  395. struct pci_dn *pdn = PCI_DN(dn);
  396. struct pnv_phb *phb = pdn->phb->private_data;
  397. u32 bdfn = (pdn->busno << 8) | pdn->devfn;
  398. s64 rc;
  399. switch (size) {
  400. case 1: {
  401. u8 v8;
  402. rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
  403. *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
  404. break;
  405. }
  406. case 2: {
  407. __be16 v16;
  408. rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
  409. &v16);
  410. *val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
  411. break;
  412. }
  413. case 4: {
  414. __be32 v32;
  415. rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
  416. *val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
  417. break;
  418. }
  419. default:
  420. return PCIBIOS_FUNC_NOT_SUPPORTED;
  421. }
  422. cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
  423. __func__, pdn->busno, pdn->devfn, where, size, *val);
  424. return PCIBIOS_SUCCESSFUL;
  425. }
  426. int pnv_pci_cfg_write(struct device_node *dn,
  427. int where, int size, u32 val)
  428. {
  429. struct pci_dn *pdn = PCI_DN(dn);
  430. struct pnv_phb *phb = pdn->phb->private_data;
  431. u32 bdfn = (pdn->busno << 8) | pdn->devfn;
  432. cfg_dbg("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
  433. pdn->busno, pdn->devfn, where, size, val);
  434. switch (size) {
  435. case 1:
  436. opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
  437. break;
  438. case 2:
  439. opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
  440. break;
  441. case 4:
  442. opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
  443. break;
  444. default:
  445. return PCIBIOS_FUNC_NOT_SUPPORTED;
  446. }
  447. return PCIBIOS_SUCCESSFUL;
  448. }
  449. #if CONFIG_EEH
  450. static bool pnv_pci_cfg_check(struct pci_controller *hose,
  451. struct device_node *dn)
  452. {
  453. struct eeh_dev *edev = NULL;
  454. struct pnv_phb *phb = hose->private_data;
  455. /* EEH not enabled ? */
  456. if (!(phb->flags & PNV_PHB_FLAG_EEH))
  457. return true;
  458. /* PE reset or device removed ? */
  459. edev = of_node_to_eeh_dev(dn);
  460. if (edev) {
  461. if (edev->pe &&
  462. (edev->pe->state & EEH_PE_CFG_BLOCKED))
  463. return false;
  464. if (edev->mode & EEH_DEV_REMOVED)
  465. return false;
  466. }
  467. return true;
  468. }
  469. #else
  470. static inline pnv_pci_cfg_check(struct pci_controller *hose,
  471. struct device_node *dn)
  472. {
  473. return true;
  474. }
  475. #endif /* CONFIG_EEH */
  476. static int pnv_pci_read_config(struct pci_bus *bus,
  477. unsigned int devfn,
  478. int where, int size, u32 *val)
  479. {
  480. struct device_node *dn, *busdn = pci_bus_to_OF_node(bus);
  481. struct pci_dn *pdn;
  482. struct pnv_phb *phb;
  483. bool found = false;
  484. int ret;
  485. *val = 0xFFFFFFFF;
  486. for (dn = busdn->child; dn; dn = dn->sibling) {
  487. pdn = PCI_DN(dn);
  488. if (pdn && pdn->devfn == devfn) {
  489. phb = pdn->phb->private_data;
  490. found = true;
  491. break;
  492. }
  493. }
  494. if (!found || !pnv_pci_cfg_check(pdn->phb, dn))
  495. return PCIBIOS_DEVICE_NOT_FOUND;
  496. ret = pnv_pci_cfg_read(dn, where, size, val);
  497. if (phb->flags & PNV_PHB_FLAG_EEH) {
  498. if (*val == EEH_IO_ERROR_VALUE(size) &&
  499. eeh_dev_check_failure(of_node_to_eeh_dev(dn)))
  500. return PCIBIOS_DEVICE_NOT_FOUND;
  501. } else {
  502. pnv_pci_config_check_eeh(phb, dn);
  503. }
  504. return ret;
  505. }
  506. static int pnv_pci_write_config(struct pci_bus *bus,
  507. unsigned int devfn,
  508. int where, int size, u32 val)
  509. {
  510. struct device_node *dn, *busdn = pci_bus_to_OF_node(bus);
  511. struct pci_dn *pdn;
  512. struct pnv_phb *phb;
  513. bool found = false;
  514. int ret;
  515. for (dn = busdn->child; dn; dn = dn->sibling) {
  516. pdn = PCI_DN(dn);
  517. if (pdn && pdn->devfn == devfn) {
  518. phb = pdn->phb->private_data;
  519. found = true;
  520. break;
  521. }
  522. }
  523. if (!found || !pnv_pci_cfg_check(pdn->phb, dn))
  524. return PCIBIOS_DEVICE_NOT_FOUND;
  525. ret = pnv_pci_cfg_write(dn, where, size, val);
  526. if (!(phb->flags & PNV_PHB_FLAG_EEH))
  527. pnv_pci_config_check_eeh(phb, dn);
  528. return ret;
  529. }
  530. struct pci_ops pnv_pci_ops = {
  531. .read = pnv_pci_read_config,
  532. .write = pnv_pci_write_config,
  533. };
  534. static int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
  535. unsigned long uaddr, enum dma_data_direction direction,
  536. struct dma_attrs *attrs, bool rm)
  537. {
  538. u64 proto_tce;
  539. __be64 *tcep, *tces;
  540. u64 rpn;
  541. proto_tce = TCE_PCI_READ; // Read allowed
  542. if (direction != DMA_TO_DEVICE)
  543. proto_tce |= TCE_PCI_WRITE;
  544. tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset;
  545. rpn = __pa(uaddr) >> tbl->it_page_shift;
  546. while (npages--)
  547. *(tcep++) = cpu_to_be64(proto_tce |
  548. (rpn++ << tbl->it_page_shift));
  549. /* Some implementations won't cache invalid TCEs and thus may not
  550. * need that flush. We'll probably turn it_type into a bit mask
  551. * of flags if that becomes the case
  552. */
  553. if (tbl->it_type & TCE_PCI_SWINV_CREATE)
  554. pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm);
  555. return 0;
  556. }
  557. static int pnv_tce_build_vm(struct iommu_table *tbl, long index, long npages,
  558. unsigned long uaddr,
  559. enum dma_data_direction direction,
  560. struct dma_attrs *attrs)
  561. {
  562. return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs,
  563. false);
  564. }
  565. static void pnv_tce_free(struct iommu_table *tbl, long index, long npages,
  566. bool rm)
  567. {
  568. __be64 *tcep, *tces;
  569. tces = tcep = ((__be64 *)tbl->it_base) + index - tbl->it_offset;
  570. while (npages--)
  571. *(tcep++) = cpu_to_be64(0);
  572. if (tbl->it_type & TCE_PCI_SWINV_FREE)
  573. pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1, rm);
  574. }
  575. static void pnv_tce_free_vm(struct iommu_table *tbl, long index, long npages)
  576. {
  577. pnv_tce_free(tbl, index, npages, false);
  578. }
  579. static unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
  580. {
  581. return ((u64 *)tbl->it_base)[index - tbl->it_offset];
  582. }
  583. static int pnv_tce_build_rm(struct iommu_table *tbl, long index, long npages,
  584. unsigned long uaddr,
  585. enum dma_data_direction direction,
  586. struct dma_attrs *attrs)
  587. {
  588. return pnv_tce_build(tbl, index, npages, uaddr, direction, attrs, true);
  589. }
  590. static void pnv_tce_free_rm(struct iommu_table *tbl, long index, long npages)
  591. {
  592. pnv_tce_free(tbl, index, npages, true);
  593. }
  594. void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
  595. void *tce_mem, u64 tce_size,
  596. u64 dma_offset, unsigned page_shift)
  597. {
  598. tbl->it_blocksize = 16;
  599. tbl->it_base = (unsigned long)tce_mem;
  600. tbl->it_page_shift = page_shift;
  601. tbl->it_offset = dma_offset >> tbl->it_page_shift;
  602. tbl->it_index = 0;
  603. tbl->it_size = tce_size >> 3;
  604. tbl->it_busno = 0;
  605. tbl->it_type = TCE_PCI;
  606. }
  607. static struct iommu_table *pnv_pci_setup_bml_iommu(struct pci_controller *hose)
  608. {
  609. struct iommu_table *tbl;
  610. const __be64 *basep, *swinvp;
  611. const __be32 *sizep;
  612. basep = of_get_property(hose->dn, "linux,tce-base", NULL);
  613. sizep = of_get_property(hose->dn, "linux,tce-size", NULL);
  614. if (basep == NULL || sizep == NULL) {
  615. pr_err("PCI: %s has missing tce entries !\n",
  616. hose->dn->full_name);
  617. return NULL;
  618. }
  619. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, hose->node);
  620. if (WARN_ON(!tbl))
  621. return NULL;
  622. pnv_pci_setup_iommu_table(tbl, __va(be64_to_cpup(basep)),
  623. be32_to_cpup(sizep), 0, IOMMU_PAGE_SHIFT_4K);
  624. iommu_init_table(tbl, hose->node);
  625. iommu_register_group(tbl, pci_domain_nr(hose->bus), 0);
  626. /* Deal with SW invalidated TCEs when needed (BML way) */
  627. swinvp = of_get_property(hose->dn, "linux,tce-sw-invalidate-info",
  628. NULL);
  629. if (swinvp) {
  630. tbl->it_busno = be64_to_cpu(swinvp[1]);
  631. tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
  632. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
  633. }
  634. return tbl;
  635. }
  636. static void pnv_pci_dma_fallback_setup(struct pci_controller *hose,
  637. struct pci_dev *pdev)
  638. {
  639. struct device_node *np = pci_bus_to_OF_node(hose->bus);
  640. struct pci_dn *pdn;
  641. if (np == NULL)
  642. return;
  643. pdn = PCI_DN(np);
  644. if (!pdn->iommu_table)
  645. pdn->iommu_table = pnv_pci_setup_bml_iommu(hose);
  646. if (!pdn->iommu_table)
  647. return;
  648. set_iommu_table_base_and_group(&pdev->dev, pdn->iommu_table);
  649. }
  650. static void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
  651. {
  652. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  653. struct pnv_phb *phb = hose->private_data;
  654. /* If we have no phb structure, try to setup a fallback based on
  655. * the device-tree (RTAS PCI for example)
  656. */
  657. if (phb && phb->dma_dev_setup)
  658. phb->dma_dev_setup(phb, pdev);
  659. else
  660. pnv_pci_dma_fallback_setup(hose, pdev);
  661. }
  662. int pnv_pci_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
  663. {
  664. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  665. struct pnv_phb *phb = hose->private_data;
  666. if (phb && phb->dma_set_mask)
  667. return phb->dma_set_mask(phb, pdev, dma_mask);
  668. return __dma_set_mask(&pdev->dev, dma_mask);
  669. }
  670. u64 pnv_pci_dma_get_required_mask(struct pci_dev *pdev)
  671. {
  672. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  673. struct pnv_phb *phb = hose->private_data;
  674. if (phb && phb->dma_get_required_mask)
  675. return phb->dma_get_required_mask(phb, pdev);
  676. return __dma_get_required_mask(&pdev->dev);
  677. }
  678. void pnv_pci_shutdown(void)
  679. {
  680. struct pci_controller *hose;
  681. list_for_each_entry(hose, &hose_list, list_node) {
  682. struct pnv_phb *phb = hose->private_data;
  683. if (phb && phb->shutdown)
  684. phb->shutdown(phb);
  685. }
  686. }
  687. /* Fixup wrong class code in p7ioc and p8 root complex */
  688. static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
  689. {
  690. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  691. }
  692. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
  693. static int pnv_pci_probe_mode(struct pci_bus *bus)
  694. {
  695. struct pci_controller *hose = pci_bus_to_host(bus);
  696. const __be64 *tstamp;
  697. u64 now, target;
  698. /* We hijack this as a way to ensure we have waited long
  699. * enough since the reset was lifted on the PCI bus
  700. */
  701. if (bus != hose->bus)
  702. return PCI_PROBE_NORMAL;
  703. tstamp = of_get_property(hose->dn, "reset-clear-timestamp", NULL);
  704. if (!tstamp || !*tstamp)
  705. return PCI_PROBE_NORMAL;
  706. now = mftb() / tb_ticks_per_usec;
  707. target = (be64_to_cpup(tstamp) / tb_ticks_per_usec)
  708. + PCI_RESET_DELAY_US;
  709. pr_devel("pci %04d: Reset target: 0x%llx now: 0x%llx\n",
  710. hose->global_number, target, now);
  711. if (now < target)
  712. msleep((target - now + 999) / 1000);
  713. return PCI_PROBE_NORMAL;
  714. }
  715. void __init pnv_pci_init(void)
  716. {
  717. struct device_node *np;
  718. pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
  719. /* OPAL absent, try POPAL first then RTAS detection of PHBs */
  720. if (!firmware_has_feature(FW_FEATURE_OPAL)) {
  721. #ifdef CONFIG_PPC_POWERNV_RTAS
  722. init_pci_config_tokens();
  723. find_and_init_phbs();
  724. #endif /* CONFIG_PPC_POWERNV_RTAS */
  725. }
  726. /* OPAL is here, do our normal stuff */
  727. else {
  728. int found_ioda = 0;
  729. /* Look for IODA IO-Hubs. We don't support mixing IODA
  730. * and p5ioc2 due to the need to change some global
  731. * probing flags
  732. */
  733. for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
  734. pnv_pci_init_ioda_hub(np);
  735. found_ioda = 1;
  736. }
  737. /* Look for p5ioc2 IO-Hubs */
  738. if (!found_ioda)
  739. for_each_compatible_node(np, NULL, "ibm,p5ioc2")
  740. pnv_pci_init_p5ioc2_hub(np);
  741. /* Look for ioda2 built-in PHB3's */
  742. for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
  743. pnv_pci_init_ioda2_phb(np);
  744. }
  745. /* Setup the linkage between OF nodes and PHBs */
  746. pci_devs_phb_init();
  747. /* Configure IOMMU DMA hooks */
  748. ppc_md.pci_dma_dev_setup = pnv_pci_dma_dev_setup;
  749. ppc_md.tce_build = pnv_tce_build_vm;
  750. ppc_md.tce_free = pnv_tce_free_vm;
  751. ppc_md.tce_build_rm = pnv_tce_build_rm;
  752. ppc_md.tce_free_rm = pnv_tce_free_rm;
  753. ppc_md.tce_get = pnv_tce_get;
  754. ppc_md.pci_probe_mode = pnv_pci_probe_mode;
  755. set_pci_dma_ops(&dma_iommu_ops);
  756. /* Configure MSIs */
  757. #ifdef CONFIG_PCI_MSI
  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);