pci.c 23 KB

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