pci-p5ioc2.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. /* 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 is_64,
  41. 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 bmap_size;
  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. phb->msi_count = be32_to_cpup(prop + 1);
  64. bmap_size = BITS_TO_LONGS(phb->msi_count) * sizeof(unsigned long);
  65. phb->msi_map = zalloc_maybe_bootmem(bmap_size, GFP_KERNEL);
  66. if (!phb->msi_map) {
  67. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  68. phb->hose->global_number);
  69. return;
  70. }
  71. phb->msi_setup = pnv_pci_p5ioc2_msi_setup;
  72. phb->msi32_support = 0;
  73. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  74. phb->msi_count, phb->msi_base);
  75. }
  76. #else
  77. static void pnv_pci_init_p5ioc2_msis(struct pnv_phb *phb) { }
  78. #endif /* CONFIG_PCI_MSI */
  79. static void __devinit pnv_pci_p5ioc2_dma_dev_setup(struct pnv_phb *phb,
  80. struct pci_dev *pdev)
  81. {
  82. if (phb->p5ioc2.iommu_table.it_map == NULL)
  83. iommu_init_table(&phb->p5ioc2.iommu_table, phb->hose->node);
  84. set_iommu_table_base(&pdev->dev, &phb->p5ioc2.iommu_table);
  85. }
  86. static void __init pnv_pci_init_p5ioc2_phb(struct device_node *np,
  87. void *tce_mem, u64 tce_size)
  88. {
  89. struct pnv_phb *phb;
  90. const u64 *prop64;
  91. u64 phb_id;
  92. int64_t rc;
  93. static int primary = 1;
  94. pr_info(" Initializing p5ioc2 PHB %s\n", np->full_name);
  95. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  96. if (!prop64) {
  97. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  98. return;
  99. }
  100. phb_id = be64_to_cpup(prop64);
  101. pr_devel(" PHB-ID : 0x%016llx\n", phb_id);
  102. pr_devel(" TCE AT : 0x%016lx\n", __pa(tce_mem));
  103. pr_devel(" TCE SZ : 0x%016llx\n", tce_size);
  104. rc = opal_pci_set_phb_tce_memory(phb_id, __pa(tce_mem), tce_size);
  105. if (rc != OPAL_SUCCESS) {
  106. pr_err(" Failed to set TCE memory, OPAL error %lld\n", rc);
  107. return;
  108. }
  109. phb = alloc_bootmem(sizeof(struct pnv_phb));
  110. if (phb) {
  111. memset(phb, 0, sizeof(struct pnv_phb));
  112. phb->hose = pcibios_alloc_controller(np);
  113. }
  114. if (!phb || !phb->hose) {
  115. pr_err(" Failed to allocate PCI controller\n");
  116. return;
  117. }
  118. spin_lock_init(&phb->lock);
  119. phb->hose->first_busno = 0;
  120. phb->hose->last_busno = 0xff;
  121. phb->hose->private_data = phb;
  122. phb->opal_id = phb_id;
  123. phb->type = PNV_PHB_P5IOC2;
  124. phb->model = PNV_PHB_MODEL_P5IOC2;
  125. phb->regs = of_iomap(np, 0);
  126. if (phb->regs == NULL)
  127. pr_err(" Failed to map registers !\n");
  128. else {
  129. pr_devel(" P_BUID = 0x%08x\n", in_be32(phb->regs + 0x100));
  130. pr_devel(" P_IOSZ = 0x%08x\n", in_be32(phb->regs + 0x1b0));
  131. pr_devel(" P_IO_ST = 0x%08x\n", in_be32(phb->regs + 0x1e0));
  132. pr_devel(" P_MEM1_H = 0x%08x\n", in_be32(phb->regs + 0x1a0));
  133. pr_devel(" P_MEM1_L = 0x%08x\n", in_be32(phb->regs + 0x190));
  134. pr_devel(" P_MSZ1_L = 0x%08x\n", in_be32(phb->regs + 0x1c0));
  135. pr_devel(" P_MEM_ST = 0x%08x\n", in_be32(phb->regs + 0x1d0));
  136. pr_devel(" P_MEM2_H = 0x%08x\n", in_be32(phb->regs + 0x2c0));
  137. pr_devel(" P_MEM2_L = 0x%08x\n", in_be32(phb->regs + 0x2b0));
  138. pr_devel(" P_MSZ2_H = 0x%08x\n", in_be32(phb->regs + 0x2d0));
  139. pr_devel(" P_MSZ2_L = 0x%08x\n", in_be32(phb->regs + 0x2e0));
  140. }
  141. /* Interpret the "ranges" property */
  142. /* This also maps the I/O region and sets isa_io/mem_base */
  143. pci_process_bridge_OF_ranges(phb->hose, np, primary);
  144. primary = 0;
  145. phb->hose->ops = &pnv_pci_ops;
  146. /* Setup MSI support */
  147. pnv_pci_init_p5ioc2_msis(phb);
  148. /* Setup TCEs */
  149. phb->dma_dev_setup = pnv_pci_p5ioc2_dma_dev_setup;
  150. pnv_pci_setup_iommu_table(&phb->p5ioc2.iommu_table,
  151. tce_mem, tce_size, 0);
  152. }
  153. void __init pnv_pci_init_p5ioc2_hub(struct device_node *np)
  154. {
  155. struct device_node *phbn;
  156. const u64 *prop64;
  157. u64 hub_id;
  158. void *tce_mem;
  159. uint64_t tce_per_phb;
  160. int64_t rc;
  161. int phb_count = 0;
  162. pr_info("Probing p5ioc2 IO-Hub %s\n", np->full_name);
  163. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  164. if (!prop64) {
  165. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  166. return;
  167. }
  168. hub_id = be64_to_cpup(prop64);
  169. pr_info(" HUB-ID : 0x%016llx\n", hub_id);
  170. /* Currently allocate 16M of TCE memory for every Hub
  171. *
  172. * XXX TODO: Make it chip local if possible
  173. */
  174. tce_mem = __alloc_bootmem(P5IOC2_TCE_MEMORY, P5IOC2_TCE_MEMORY,
  175. __pa(MAX_DMA_ADDRESS));
  176. if (!tce_mem) {
  177. pr_err(" Failed to allocate TCE Memory !\n");
  178. return;
  179. }
  180. pr_debug(" TCE : 0x%016lx..0x%016lx\n",
  181. __pa(tce_mem), __pa(tce_mem) + P5IOC2_TCE_MEMORY - 1);
  182. rc = opal_pci_set_hub_tce_memory(hub_id, __pa(tce_mem),
  183. P5IOC2_TCE_MEMORY);
  184. if (rc != OPAL_SUCCESS) {
  185. pr_err(" Failed to allocate TCE memory, OPAL error %lld\n", rc);
  186. return;
  187. }
  188. /* Count child PHBs */
  189. for_each_child_of_node(np, phbn) {
  190. if (of_device_is_compatible(phbn, "ibm,p5ioc2-pcix") ||
  191. of_device_is_compatible(phbn, "ibm,p5ioc2-pciex"))
  192. phb_count++;
  193. }
  194. /* Calculate how much TCE space we can give per PHB */
  195. tce_per_phb = __rounddown_pow_of_two(P5IOC2_TCE_MEMORY / phb_count);
  196. pr_info(" Allocating %lld MB of TCE memory per PHB\n",
  197. tce_per_phb >> 20);
  198. /* Initialize PHBs */
  199. for_each_child_of_node(np, phbn) {
  200. if (of_device_is_compatible(phbn, "ibm,p5ioc2-pcix") ||
  201. of_device_is_compatible(phbn, "ibm,p5ioc2-pciex")) {
  202. pnv_pci_init_p5ioc2_phb(phbn, tce_mem, tce_per_phb);
  203. tce_mem += tce_per_phb;
  204. }
  205. }
  206. }