pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 <asm/sections.h>
  23. #include <asm/io.h>
  24. #include <asm/prom.h>
  25. #include <asm/pci-bridge.h>
  26. #include <asm/machdep.h>
  27. #include <asm/ppc-pci.h>
  28. #include <asm/opal.h>
  29. #include <asm/iommu.h>
  30. #include <asm/tce.h>
  31. #include <asm/abs_addr.h>
  32. #include "powernv.h"
  33. #include "pci.h"
  34. /* Delay in usec */
  35. #define PCI_RESET_DELAY_US 3000000
  36. #define cfg_dbg(fmt...) do { } while(0)
  37. //#define cfg_dbg(fmt...) printk(fmt)
  38. #ifdef CONFIG_PCI_MSI
  39. static int pnv_msi_check_device(struct pci_dev* pdev, int nvec, int type)
  40. {
  41. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  42. struct pnv_phb *phb = hose->private_data;
  43. return (phb && phb->msi_map) ? 0 : -ENODEV;
  44. }
  45. static unsigned int pnv_get_one_msi(struct pnv_phb *phb)
  46. {
  47. unsigned int id;
  48. spin_lock(&phb->lock);
  49. id = find_next_zero_bit(phb->msi_map, phb->msi_count, phb->msi_next);
  50. if (id >= phb->msi_count && phb->msi_next)
  51. id = find_next_zero_bit(phb->msi_map, phb->msi_count, 0);
  52. if (id >= phb->msi_count) {
  53. spin_unlock(&phb->lock);
  54. return 0;
  55. }
  56. __set_bit(id, phb->msi_map);
  57. spin_unlock(&phb->lock);
  58. return id + phb->msi_base;
  59. }
  60. static void pnv_put_msi(struct pnv_phb *phb, unsigned int hwirq)
  61. {
  62. unsigned int id;
  63. if (WARN_ON(hwirq < phb->msi_base ||
  64. hwirq >= (phb->msi_base + phb->msi_count)))
  65. return;
  66. id = hwirq - phb->msi_base;
  67. spin_lock(&phb->lock);
  68. __clear_bit(id, phb->msi_map);
  69. spin_unlock(&phb->lock);
  70. }
  71. static int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  72. {
  73. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  74. struct pnv_phb *phb = hose->private_data;
  75. struct msi_desc *entry;
  76. struct msi_msg msg;
  77. unsigned int hwirq, virq;
  78. int rc;
  79. if (WARN_ON(!phb))
  80. return -ENODEV;
  81. list_for_each_entry(entry, &pdev->msi_list, list) {
  82. if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
  83. pr_warn("%s: Supports only 64-bit MSIs\n",
  84. pci_name(pdev));
  85. return -ENXIO;
  86. }
  87. hwirq = pnv_get_one_msi(phb);
  88. if (!hwirq) {
  89. pr_warn("%s: Failed to find a free MSI\n",
  90. pci_name(pdev));
  91. return -ENOSPC;
  92. }
  93. virq = irq_create_mapping(NULL, hwirq);
  94. if (virq == NO_IRQ) {
  95. pr_warn("%s: Failed to map MSI to linux irq\n",
  96. pci_name(pdev));
  97. pnv_put_msi(phb, hwirq);
  98. return -ENOMEM;
  99. }
  100. rc = phb->msi_setup(phb, pdev, hwirq, entry->msi_attrib.is_64,
  101. &msg);
  102. if (rc) {
  103. pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
  104. irq_dispose_mapping(virq);
  105. pnv_put_msi(phb, hwirq);
  106. return rc;
  107. }
  108. irq_set_msi_desc(virq, entry);
  109. write_msi_msg(virq, &msg);
  110. }
  111. return 0;
  112. }
  113. static void pnv_teardown_msi_irqs(struct pci_dev *pdev)
  114. {
  115. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  116. struct pnv_phb *phb = hose->private_data;
  117. struct msi_desc *entry;
  118. if (WARN_ON(!phb))
  119. return;
  120. list_for_each_entry(entry, &pdev->msi_list, list) {
  121. if (entry->irq == NO_IRQ)
  122. continue;
  123. irq_set_msi_desc(entry->irq, NULL);
  124. pnv_put_msi(phb, virq_to_hw(entry->irq));
  125. irq_dispose_mapping(entry->irq);
  126. }
  127. }
  128. #endif /* CONFIG_PCI_MSI */
  129. static void pnv_pci_config_check_eeh(struct pnv_phb *phb, struct pci_bus *bus,
  130. u32 bdfn)
  131. {
  132. s64 rc;
  133. u8 fstate;
  134. u16 pcierr;
  135. u32 pe_no;
  136. /* Get PE# if we support IODA */
  137. pe_no = phb->bdfn_to_pe ? phb->bdfn_to_pe(phb, bus, bdfn & 0xff) : 0;
  138. /* Read freeze status */
  139. rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no, &fstate, &pcierr,
  140. NULL);
  141. if (rc) {
  142. pr_warning("PCI %d: Failed to read EEH status for PE#%d,"
  143. " err %lld\n", phb->hose->global_number, pe_no, rc);
  144. return;
  145. }
  146. cfg_dbg(" -> EEH check, bdfn=%04x PE%d fstate=%x\n",
  147. bdfn, pe_no, fstate);
  148. if (fstate != 0) {
  149. rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
  150. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  151. if (rc) {
  152. pr_warning("PCI %d: Failed to clear EEH freeze state"
  153. " for PE#%d, err %lld\n",
  154. phb->hose->global_number, pe_no, rc);
  155. }
  156. }
  157. }
  158. static int pnv_pci_read_config(struct pci_bus *bus,
  159. unsigned int devfn,
  160. int where, int size, u32 *val)
  161. {
  162. struct pci_controller *hose = pci_bus_to_host(bus);
  163. struct pnv_phb *phb = hose->private_data;
  164. u32 bdfn = (((uint64_t)bus->number) << 8) | devfn;
  165. s64 rc;
  166. if (hose == NULL)
  167. return PCIBIOS_DEVICE_NOT_FOUND;
  168. switch (size) {
  169. case 1: {
  170. u8 v8;
  171. rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
  172. *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
  173. break;
  174. }
  175. case 2: {
  176. u16 v16;
  177. rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
  178. &v16);
  179. *val = (rc == OPAL_SUCCESS) ? v16 : 0xffff;
  180. break;
  181. }
  182. case 4: {
  183. u32 v32;
  184. rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
  185. *val = (rc == OPAL_SUCCESS) ? v32 : 0xffffffff;
  186. break;
  187. }
  188. default:
  189. return PCIBIOS_FUNC_NOT_SUPPORTED;
  190. }
  191. cfg_dbg("pnv_pci_read_config bus: %x devfn: %x +%x/%x -> %08x\n",
  192. bus->number, devfn, where, size, *val);
  193. /* Check if the PHB got frozen due to an error (no response) */
  194. pnv_pci_config_check_eeh(phb, bus, bdfn);
  195. return PCIBIOS_SUCCESSFUL;
  196. }
  197. static int pnv_pci_write_config(struct pci_bus *bus,
  198. unsigned int devfn,
  199. int where, int size, u32 val)
  200. {
  201. struct pci_controller *hose = pci_bus_to_host(bus);
  202. struct pnv_phb *phb = hose->private_data;
  203. u32 bdfn = (((uint64_t)bus->number) << 8) | devfn;
  204. if (hose == NULL)
  205. return PCIBIOS_DEVICE_NOT_FOUND;
  206. cfg_dbg("pnv_pci_write_config bus: %x devfn: %x +%x/%x -> %08x\n",
  207. bus->number, devfn, where, size, val);
  208. switch (size) {
  209. case 1:
  210. opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
  211. break;
  212. case 2:
  213. opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
  214. break;
  215. case 4:
  216. opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
  217. break;
  218. default:
  219. return PCIBIOS_FUNC_NOT_SUPPORTED;
  220. }
  221. /* Check if the PHB got frozen due to an error (no response) */
  222. pnv_pci_config_check_eeh(phb, bus, bdfn);
  223. return PCIBIOS_SUCCESSFUL;
  224. }
  225. struct pci_ops pnv_pci_ops = {
  226. .read = pnv_pci_read_config,
  227. .write = pnv_pci_write_config,
  228. };
  229. static int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
  230. unsigned long uaddr, enum dma_data_direction direction,
  231. struct dma_attrs *attrs)
  232. {
  233. u64 proto_tce;
  234. u64 *tcep;
  235. u64 rpn;
  236. proto_tce = TCE_PCI_READ; // Read allowed
  237. if (direction != DMA_TO_DEVICE)
  238. proto_tce |= TCE_PCI_WRITE;
  239. tcep = ((u64 *)tbl->it_base) + index;
  240. while (npages--) {
  241. /* can't move this out since we might cross LMB boundary */
  242. rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
  243. *tcep = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
  244. uaddr += TCE_PAGE_SIZE;
  245. tcep++;
  246. }
  247. return 0;
  248. }
  249. static void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
  250. {
  251. u64 *tcep = ((u64 *)tbl->it_base) + index;
  252. while (npages--)
  253. *(tcep++) = 0;
  254. }
  255. void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
  256. void *tce_mem, u64 tce_size,
  257. u64 dma_offset)
  258. {
  259. tbl->it_blocksize = 16;
  260. tbl->it_base = (unsigned long)tce_mem;
  261. tbl->it_offset = dma_offset >> IOMMU_PAGE_SHIFT;
  262. tbl->it_index = 0;
  263. tbl->it_size = tce_size >> 3;
  264. tbl->it_busno = 0;
  265. tbl->it_type = TCE_PCI;
  266. }
  267. static struct iommu_table * __devinit
  268. pnv_pci_setup_bml_iommu(struct pci_controller *hose)
  269. {
  270. struct iommu_table *tbl;
  271. const __be64 *basep;
  272. const __be32 *sizep;
  273. basep = of_get_property(hose->dn, "linux,tce-base", NULL);
  274. sizep = of_get_property(hose->dn, "linux,tce-size", NULL);
  275. if (basep == NULL || sizep == NULL) {
  276. pr_err("PCI: %s has missing tce entries !\n", hose->dn->full_name);
  277. return NULL;
  278. }
  279. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, hose->node);
  280. if (WARN_ON(!tbl))
  281. return NULL;
  282. pnv_pci_setup_iommu_table(tbl, __va(be64_to_cpup(basep)),
  283. be32_to_cpup(sizep), 0);
  284. iommu_init_table(tbl, hose->node);
  285. return tbl;
  286. }
  287. static void __devinit pnv_pci_dma_fallback_setup(struct pci_controller *hose,
  288. struct pci_dev *pdev)
  289. {
  290. struct device_node *np = pci_bus_to_OF_node(hose->bus);
  291. struct pci_dn *pdn;
  292. if (np == NULL)
  293. return;
  294. pdn = PCI_DN(np);
  295. if (!pdn->iommu_table)
  296. pdn->iommu_table = pnv_pci_setup_bml_iommu(hose);
  297. if (!pdn->iommu_table)
  298. return;
  299. set_iommu_table_base(&pdev->dev, pdn->iommu_table);
  300. }
  301. static void __devinit pnv_pci_dma_dev_setup(struct pci_dev *pdev)
  302. {
  303. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  304. struct pnv_phb *phb = hose->private_data;
  305. /* If we have no phb structure, try to setup a fallback based on
  306. * the device-tree (RTAS PCI for example)
  307. */
  308. if (phb && phb->dma_dev_setup)
  309. phb->dma_dev_setup(phb, pdev);
  310. else
  311. pnv_pci_dma_fallback_setup(hose, pdev);
  312. }
  313. static int pnv_pci_probe_mode(struct pci_bus *bus)
  314. {
  315. struct pci_controller *hose = pci_bus_to_host(bus);
  316. const __be64 *tstamp;
  317. u64 now, target;
  318. /* We hijack this as a way to ensure we have waited long
  319. * enough since the reset was lifted on the PCI bus
  320. */
  321. if (bus != hose->bus)
  322. return PCI_PROBE_NORMAL;
  323. tstamp = of_get_property(hose->dn, "reset-clear-timestamp", NULL);
  324. if (!tstamp || !*tstamp)
  325. return PCI_PROBE_NORMAL;
  326. now = mftb() / tb_ticks_per_usec;
  327. target = (be64_to_cpup(tstamp) / tb_ticks_per_usec)
  328. + PCI_RESET_DELAY_US;
  329. pr_devel("pci %04d: Reset target: 0x%llx now: 0x%llx\n",
  330. hose->global_number, target, now);
  331. if (now < target)
  332. msleep((target - now + 999) / 1000);
  333. return PCI_PROBE_NORMAL;
  334. }
  335. void __init pnv_pci_init(void)
  336. {
  337. struct device_node *np;
  338. pci_set_flags(PCI_CAN_SKIP_ISA_ALIGN);
  339. /* We do not want to just probe */
  340. pci_probe_only = 0;
  341. /* OPAL absent, try POPAL first then RTAS detection of PHBs */
  342. if (!firmware_has_feature(FW_FEATURE_OPAL)) {
  343. #ifdef CONFIG_PPC_POWERNV_RTAS
  344. init_pci_config_tokens();
  345. find_and_init_phbs();
  346. #endif /* CONFIG_PPC_POWERNV_RTAS */
  347. } else {
  348. /* OPAL is here, do our normal stuff */
  349. /* Look for p5ioc2 IO-Hubs */
  350. for_each_compatible_node(np, NULL, "ibm,p5ioc2")
  351. pnv_pci_init_p5ioc2_hub(np);
  352. }
  353. /* Setup the linkage between OF nodes and PHBs */
  354. pci_devs_phb_init();
  355. /* Configure IOMMU DMA hooks */
  356. ppc_md.pci_dma_dev_setup = pnv_pci_dma_dev_setup;
  357. ppc_md.tce_build = pnv_tce_build;
  358. ppc_md.tce_free = pnv_tce_free;
  359. ppc_md.pci_probe_mode = pnv_pci_probe_mode;
  360. set_pci_dma_ops(&dma_iommu_ops);
  361. /* Configure MSIs */
  362. #ifdef CONFIG_PCI_MSI
  363. ppc_md.msi_check_device = pnv_msi_check_device;
  364. ppc_md.setup_msi_irqs = pnv_setup_msi_irqs;
  365. ppc_md.teardown_msi_irqs = pnv_teardown_msi_irqs;
  366. #endif
  367. }