pci.c 23 KB

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