pci-p5ioc2.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <asm/sections.h>
  22. #include <asm/io.h>
  23. #include <asm/prom.h>
  24. #include <asm/pci-bridge.h>
  25. #include <asm/machdep.h>
  26. #include <asm/ppc-pci.h>
  27. #include <asm/opal.h>
  28. #include <asm/iommu.h>
  29. #include <asm/tce.h>
  30. #include <asm/abs_addr.h>
  31. #include "powernv.h"
  32. #include "pci.h"
  33. /* For now, use a fixed amount of TCE memory for each p5ioc2
  34. * hub, 16M will do
  35. */
  36. #define P5IOC2_TCE_MEMORY 0x01000000
  37. static void __devinit pnv_pci_p5ioc2_dma_dev_setup(struct pnv_phb *phb,
  38. struct pci_dev *pdev)
  39. {
  40. if (phb->p5ioc2.iommu_table.it_map == NULL)
  41. iommu_init_table(&phb->p5ioc2.iommu_table, phb->hose->node);
  42. set_iommu_table_base(&pdev->dev, &phb->p5ioc2.iommu_table);
  43. }
  44. static void __init pnv_pci_init_p5ioc2_phb(struct device_node *np,
  45. void *tce_mem, u64 tce_size)
  46. {
  47. struct pnv_phb *phb;
  48. const u64 *prop64;
  49. u64 phb_id;
  50. int64_t rc;
  51. static int primary = 1;
  52. pr_info(" Initializing p5ioc2 PHB %s\n", np->full_name);
  53. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  54. if (!prop64) {
  55. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  56. return;
  57. }
  58. phb_id = be64_to_cpup(prop64);
  59. pr_devel(" PHB-ID : 0x%016llx\n", phb_id);
  60. pr_devel(" TCE AT : 0x%016lx\n", __pa(tce_mem));
  61. pr_devel(" TCE SZ : 0x%016llx\n", tce_size);
  62. rc = opal_pci_set_phb_tce_memory(phb_id, __pa(tce_mem), tce_size);
  63. if (rc != OPAL_SUCCESS) {
  64. pr_err(" Failed to set TCE memory, OPAL error %lld\n", rc);
  65. return;
  66. }
  67. phb = alloc_bootmem(sizeof(struct pnv_phb));
  68. if (phb) {
  69. memset(phb, 0, sizeof(struct pnv_phb));
  70. phb->hose = pcibios_alloc_controller(np);
  71. }
  72. if (!phb || !phb->hose) {
  73. pr_err(" Failed to allocate PCI controller\n");
  74. return;
  75. }
  76. spin_lock_init(&phb->lock);
  77. phb->hose->first_busno = 0;
  78. phb->hose->last_busno = 0xff;
  79. phb->hose->private_data = phb;
  80. phb->opal_id = phb_id;
  81. phb->type = PNV_PHB_P5IOC2;
  82. phb->regs = of_iomap(np, 0);
  83. if (phb->regs == NULL)
  84. pr_err(" Failed to map registers !\n");
  85. else {
  86. pr_devel(" P_BUID = 0x%08x\n", in_be32(phb->regs + 0x100));
  87. pr_devel(" P_IOSZ = 0x%08x\n", in_be32(phb->regs + 0x1b0));
  88. pr_devel(" P_IO_ST = 0x%08x\n", in_be32(phb->regs + 0x1e0));
  89. pr_devel(" P_MEM1_H = 0x%08x\n", in_be32(phb->regs + 0x1a0));
  90. pr_devel(" P_MEM1_L = 0x%08x\n", in_be32(phb->regs + 0x190));
  91. pr_devel(" P_MSZ1_L = 0x%08x\n", in_be32(phb->regs + 0x1c0));
  92. pr_devel(" P_MEM_ST = 0x%08x\n", in_be32(phb->regs + 0x1d0));
  93. pr_devel(" P_MEM2_H = 0x%08x\n", in_be32(phb->regs + 0x2c0));
  94. pr_devel(" P_MEM2_L = 0x%08x\n", in_be32(phb->regs + 0x2b0));
  95. pr_devel(" P_MSZ2_H = 0x%08x\n", in_be32(phb->regs + 0x2d0));
  96. pr_devel(" P_MSZ2_L = 0x%08x\n", in_be32(phb->regs + 0x2e0));
  97. }
  98. /* Interpret the "ranges" property */
  99. /* This also maps the I/O region and sets isa_io/mem_base */
  100. pci_process_bridge_OF_ranges(phb->hose, np, primary);
  101. primary = 0;
  102. phb->hose->ops = &pnv_pci_ops;
  103. /* Setup TCEs */
  104. phb->dma_dev_setup = pnv_pci_p5ioc2_dma_dev_setup;
  105. pnv_pci_setup_iommu_table(&phb->p5ioc2.iommu_table,
  106. tce_mem, tce_size, 0);
  107. }
  108. void __init pnv_pci_init_p5ioc2_hub(struct device_node *np)
  109. {
  110. struct device_node *phbn;
  111. const u64 *prop64;
  112. u64 hub_id;
  113. void *tce_mem;
  114. uint64_t tce_per_phb;
  115. int64_t rc;
  116. int phb_count = 0;
  117. pr_info("Probing p5ioc2 IO-Hub %s\n", np->full_name);
  118. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  119. if (!prop64) {
  120. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  121. return;
  122. }
  123. hub_id = be64_to_cpup(prop64);
  124. pr_info(" HUB-ID : 0x%016llx\n", hub_id);
  125. /* Currently allocate 16M of TCE memory for every Hub
  126. *
  127. * XXX TODO: Make it chip local if possible
  128. */
  129. tce_mem = __alloc_bootmem(P5IOC2_TCE_MEMORY, P5IOC2_TCE_MEMORY,
  130. __pa(MAX_DMA_ADDRESS));
  131. if (!tce_mem) {
  132. pr_err(" Failed to allocate TCE Memory !\n");
  133. return;
  134. }
  135. pr_debug(" TCE : 0x%016lx..0x%016lx\n",
  136. __pa(tce_mem), __pa(tce_mem) + P5IOC2_TCE_MEMORY - 1);
  137. rc = opal_pci_set_hub_tce_memory(hub_id, __pa(tce_mem),
  138. P5IOC2_TCE_MEMORY);
  139. if (rc != OPAL_SUCCESS) {
  140. pr_err(" Failed to allocate TCE memory, OPAL error %lld\n", rc);
  141. return;
  142. }
  143. /* Count child PHBs */
  144. for_each_child_of_node(np, phbn) {
  145. if (of_device_is_compatible(phbn, "ibm,p5ioc2-pcix") ||
  146. of_device_is_compatible(phbn, "ibm,p5ioc2-pciex"))
  147. phb_count++;
  148. }
  149. /* Calculate how much TCE space we can give per PHB */
  150. tce_per_phb = __rounddown_pow_of_two(P5IOC2_TCE_MEMORY / phb_count);
  151. pr_info(" Allocating %lld MB of TCE memory per PHB\n",
  152. tce_per_phb >> 20);
  153. /* Initialize PHBs */
  154. for_each_child_of_node(np, phbn) {
  155. if (of_device_is_compatible(phbn, "ibm,p5ioc2-pcix") ||
  156. of_device_is_compatible(phbn, "ibm,p5ioc2-pciex")) {
  157. pnv_pci_init_p5ioc2_phb(phbn, tce_mem, tce_per_phb);
  158. tce_mem += tce_per_phb;
  159. }
  160. }
  161. }