pci-p5ioc2.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/msi_bitmap.h>
  28. #include <asm/ppc-pci.h>
  29. #include <asm/opal.h>
  30. #include <asm/iommu.h>
  31. #include <asm/tce.h>
  32. #include "powernv.h"
  33. #include "pci.h"
  34. /* For now, use a fixed amount of TCE memory for each p5ioc2
  35. * hub, 16M will do
  36. */
  37. #define P5IOC2_TCE_MEMORY 0x01000000
  38. #ifdef CONFIG_PCI_MSI
  39. static int pnv_pci_p5ioc2_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
  40. unsigned int hwirq, unsigned int virq,
  41. unsigned int is_64, struct msi_msg *msg)
  42. {
  43. if (WARN_ON(!is_64))
  44. return -ENXIO;
  45. msg->data = hwirq - phb->msi_base;
  46. msg->address_hi = 0x10000000;
  47. msg->address_lo = 0;
  48. return 0;
  49. }
  50. static void pnv_pci_init_p5ioc2_msis(struct pnv_phb *phb)
  51. {
  52. unsigned int count;
  53. const __be32 *prop = of_get_property(phb->hose->dn,
  54. "ibm,opal-msi-ranges", NULL);
  55. if (!prop)
  56. return;
  57. /* Don't do MSI's on p5ioc2 PCI-X are they are not properly
  58. * verified in HW
  59. */
  60. if (of_device_is_compatible(phb->hose->dn, "ibm,p5ioc2-pcix"))
  61. return;
  62. phb->msi_base = be32_to_cpup(prop);
  63. count = be32_to_cpup(prop + 1);
  64. if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
  65. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  66. phb->hose->global_number);
  67. return;
  68. }
  69. phb->msi_setup = pnv_pci_p5ioc2_msi_setup;
  70. phb->msi32_support = 0;
  71. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  72. count, phb->msi_base);
  73. }
  74. #else
  75. static void pnv_pci_init_p5ioc2_msis(struct pnv_phb *phb) { }
  76. #endif /* CONFIG_PCI_MSI */
  77. static struct iommu_table_ops pnv_p5ioc2_iommu_ops = {
  78. .set = pnv_tce_build,
  79. #ifdef CONFIG_IOMMU_API
  80. .exchange = pnv_tce_xchg,
  81. #endif
  82. .clear = pnv_tce_free,
  83. .get = pnv_tce_get,
  84. };
  85. static void pnv_pci_p5ioc2_dma_dev_setup(struct pnv_phb *phb,
  86. struct pci_dev *pdev)
  87. {
  88. struct iommu_table *tbl = phb->p5ioc2.table_group.tables[0];
  89. if (!tbl->it_map) {
  90. tbl->it_ops = &pnv_p5ioc2_iommu_ops;
  91. iommu_init_table(tbl, phb->hose->node);
  92. iommu_register_group(&phb->p5ioc2.table_group,
  93. pci_domain_nr(phb->hose->bus), phb->opal_id);
  94. INIT_LIST_HEAD_RCU(&tbl->it_group_list);
  95. pnv_pci_link_table_and_group(phb->hose->node, 0,
  96. tbl, &phb->p5ioc2.table_group);
  97. }
  98. set_iommu_table_base(&pdev->dev, tbl);
  99. iommu_add_device(&pdev->dev);
  100. }
  101. static const struct pci_controller_ops pnv_pci_p5ioc2_controller_ops = {
  102. .dma_dev_setup = pnv_pci_dma_dev_setup,
  103. #ifdef CONFIG_PCI_MSI
  104. .setup_msi_irqs = pnv_setup_msi_irqs,
  105. .teardown_msi_irqs = pnv_teardown_msi_irqs,
  106. #endif
  107. };
  108. static void __init pnv_pci_init_p5ioc2_phb(struct device_node *np, u64 hub_id,
  109. void *tce_mem, u64 tce_size)
  110. {
  111. struct pnv_phb *phb;
  112. const __be64 *prop64;
  113. u64 phb_id;
  114. int64_t rc;
  115. static int primary = 1;
  116. struct iommu_table_group *table_group;
  117. struct iommu_table *tbl;
  118. pr_info(" Initializing p5ioc2 PHB %s\n", np->full_name);
  119. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  120. if (!prop64) {
  121. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  122. return;
  123. }
  124. phb_id = be64_to_cpup(prop64);
  125. pr_devel(" PHB-ID : 0x%016llx\n", phb_id);
  126. pr_devel(" TCE AT : 0x%016lx\n", __pa(tce_mem));
  127. pr_devel(" TCE SZ : 0x%016llx\n", tce_size);
  128. rc = opal_pci_set_phb_tce_memory(phb_id, __pa(tce_mem), tce_size);
  129. if (rc != OPAL_SUCCESS) {
  130. pr_err(" Failed to set TCE memory, OPAL error %lld\n", rc);
  131. return;
  132. }
  133. phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
  134. phb->hose = pcibios_alloc_controller(np);
  135. if (!phb->hose) {
  136. pr_err(" Failed to allocate PCI controller\n");
  137. return;
  138. }
  139. spin_lock_init(&phb->lock);
  140. phb->hose->first_busno = 0;
  141. phb->hose->last_busno = 0xff;
  142. phb->hose->private_data = phb;
  143. phb->hose->controller_ops = pnv_pci_p5ioc2_controller_ops;
  144. phb->hub_id = hub_id;
  145. phb->opal_id = phb_id;
  146. phb->type = PNV_PHB_P5IOC2;
  147. phb->model = PNV_PHB_MODEL_P5IOC2;
  148. phb->regs = of_iomap(np, 0);
  149. if (phb->regs == NULL)
  150. pr_err(" Failed to map registers !\n");
  151. else {
  152. pr_devel(" P_BUID = 0x%08x\n", in_be32(phb->regs + 0x100));
  153. pr_devel(" P_IOSZ = 0x%08x\n", in_be32(phb->regs + 0x1b0));
  154. pr_devel(" P_IO_ST = 0x%08x\n", in_be32(phb->regs + 0x1e0));
  155. pr_devel(" P_MEM1_H = 0x%08x\n", in_be32(phb->regs + 0x1a0));
  156. pr_devel(" P_MEM1_L = 0x%08x\n", in_be32(phb->regs + 0x190));
  157. pr_devel(" P_MSZ1_L = 0x%08x\n", in_be32(phb->regs + 0x1c0));
  158. pr_devel(" P_MEM_ST = 0x%08x\n", in_be32(phb->regs + 0x1d0));
  159. pr_devel(" P_MEM2_H = 0x%08x\n", in_be32(phb->regs + 0x2c0));
  160. pr_devel(" P_MEM2_L = 0x%08x\n", in_be32(phb->regs + 0x2b0));
  161. pr_devel(" P_MSZ2_H = 0x%08x\n", in_be32(phb->regs + 0x2d0));
  162. pr_devel(" P_MSZ2_L = 0x%08x\n", in_be32(phb->regs + 0x2e0));
  163. }
  164. /* Interpret the "ranges" property */
  165. /* This also maps the I/O region and sets isa_io/mem_base */
  166. pci_process_bridge_OF_ranges(phb->hose, np, primary);
  167. primary = 0;
  168. phb->hose->ops = &pnv_pci_ops;
  169. /* Setup MSI support */
  170. pnv_pci_init_p5ioc2_msis(phb);
  171. /* Setup TCEs */
  172. phb->dma_dev_setup = pnv_pci_p5ioc2_dma_dev_setup;
  173. pnv_pci_setup_iommu_table(&phb->p5ioc2.iommu_table,
  174. tce_mem, tce_size, 0,
  175. IOMMU_PAGE_SHIFT_4K);
  176. /*
  177. * We do not allocate iommu_table as we do not support
  178. * hotplug or SRIOV on P5IOC2 and therefore iommu_free_table()
  179. * should not be called for phb->p5ioc2.table_group.tables[0] ever.
  180. */
  181. tbl = phb->p5ioc2.table_group.tables[0] = &phb->p5ioc2.iommu_table;
  182. table_group = &phb->p5ioc2.table_group;
  183. table_group->tce32_start = tbl->it_offset << tbl->it_page_shift;
  184. table_group->tce32_size = tbl->it_size << tbl->it_page_shift;
  185. }
  186. void __init pnv_pci_init_p5ioc2_hub(struct device_node *np)
  187. {
  188. struct device_node *phbn;
  189. const __be64 *prop64;
  190. u64 hub_id;
  191. void *tce_mem;
  192. uint64_t tce_per_phb;
  193. int64_t rc;
  194. int phb_count = 0;
  195. pr_info("Probing p5ioc2 IO-Hub %s\n", np->full_name);
  196. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  197. if (!prop64) {
  198. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  199. return;
  200. }
  201. hub_id = be64_to_cpup(prop64);
  202. pr_info(" HUB-ID : 0x%016llx\n", hub_id);
  203. /* Count child PHBs and calculate TCE space per PHB */
  204. for_each_child_of_node(np, phbn) {
  205. if (of_device_is_compatible(phbn, "ibm,p5ioc2-pcix") ||
  206. of_device_is_compatible(phbn, "ibm,p5ioc2-pciex"))
  207. phb_count++;
  208. }
  209. if (phb_count <= 0) {
  210. pr_info(" No PHBs for Hub %s\n", np->full_name);
  211. return;
  212. }
  213. tce_per_phb = __rounddown_pow_of_two(P5IOC2_TCE_MEMORY / phb_count);
  214. pr_info(" Allocating %lld MB of TCE memory per PHB\n",
  215. tce_per_phb >> 20);
  216. /* Currently allocate 16M of TCE memory for every Hub
  217. *
  218. * XXX TODO: Make it chip local if possible
  219. */
  220. tce_mem = memblock_virt_alloc(P5IOC2_TCE_MEMORY, P5IOC2_TCE_MEMORY);
  221. pr_debug(" TCE : 0x%016lx..0x%016lx\n",
  222. __pa(tce_mem), __pa(tce_mem) + P5IOC2_TCE_MEMORY - 1);
  223. rc = opal_pci_set_hub_tce_memory(hub_id, __pa(tce_mem),
  224. P5IOC2_TCE_MEMORY);
  225. if (rc != OPAL_SUCCESS) {
  226. pr_err(" Failed to allocate TCE memory, OPAL error %lld\n", rc);
  227. return;
  228. }
  229. /* Initialize PHBs */
  230. for_each_child_of_node(np, phbn) {
  231. if (of_device_is_compatible(phbn, "ibm,p5ioc2-pcix") ||
  232. of_device_is_compatible(phbn, "ibm,p5ioc2-pciex")) {
  233. pnv_pci_init_p5ioc2_phb(phbn, hub_id,
  234. tce_mem, tce_per_phb);
  235. tce_mem += tce_per_phb;
  236. }
  237. }
  238. }