mmconfig_64.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
  3. *
  4. * This is an 64bit optimized version that always keeps the full mmconfig
  5. * space mapped. This allows lockless config space operation.
  6. */
  7. #include <linux/pci.h>
  8. #include <linux/init.h>
  9. #include <linux/acpi.h>
  10. #include <linux/bitmap.h>
  11. #include <asm/e820.h>
  12. #include "pci.h"
  13. /* Static virtual mapping of the MMCONFIG aperture */
  14. struct mmcfg_virt {
  15. struct acpi_mcfg_allocation *cfg;
  16. char __iomem *virt;
  17. };
  18. static struct mmcfg_virt *pci_mmcfg_virt;
  19. static char __iomem *get_virt(unsigned int seg, unsigned bus)
  20. {
  21. struct acpi_mcfg_allocation *cfg;
  22. int cfg_num;
  23. for (cfg_num = 0; cfg_num < pci_mmcfg_config_num; cfg_num++) {
  24. cfg = pci_mmcfg_virt[cfg_num].cfg;
  25. if (cfg->pci_segment == seg &&
  26. (cfg->start_bus_number <= bus) &&
  27. (cfg->end_bus_number >= bus))
  28. return pci_mmcfg_virt[cfg_num].virt;
  29. }
  30. /* Fall back to type 0 */
  31. return NULL;
  32. }
  33. static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
  34. {
  35. char __iomem *addr;
  36. if (seg == 0 && bus < PCI_MMCFG_MAX_CHECK_BUS &&
  37. test_bit(32*bus + PCI_SLOT(devfn), pci_mmcfg_fallback_slots))
  38. return NULL;
  39. addr = get_virt(seg, bus);
  40. if (!addr)
  41. return NULL;
  42. return addr + ((bus << 20) | (devfn << 12));
  43. }
  44. static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
  45. unsigned int devfn, int reg, int len, u32 *value)
  46. {
  47. char __iomem *addr;
  48. /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
  49. if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
  50. *value = -1;
  51. return -EINVAL;
  52. }
  53. addr = pci_dev_base(seg, bus, devfn);
  54. if (!addr)
  55. return pci_conf1_read(seg,bus,devfn,reg,len,value);
  56. switch (len) {
  57. case 1:
  58. *value = mmio_config_readb(addr + reg);
  59. break;
  60. case 2:
  61. *value = mmio_config_readw(addr + reg);
  62. break;
  63. case 4:
  64. *value = mmio_config_readl(addr + reg);
  65. break;
  66. }
  67. return 0;
  68. }
  69. static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
  70. unsigned int devfn, int reg, int len, u32 value)
  71. {
  72. char __iomem *addr;
  73. /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
  74. if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
  75. return -EINVAL;
  76. addr = pci_dev_base(seg, bus, devfn);
  77. if (!addr)
  78. return pci_conf1_write(seg,bus,devfn,reg,len,value);
  79. switch (len) {
  80. case 1:
  81. mmio_config_writeb(addr + reg, value);
  82. break;
  83. case 2:
  84. mmio_config_writew(addr + reg, value);
  85. break;
  86. case 4:
  87. mmio_config_writel(addr + reg, value);
  88. break;
  89. }
  90. return 0;
  91. }
  92. static struct pci_raw_ops pci_mmcfg = {
  93. .read = pci_mmcfg_read,
  94. .write = pci_mmcfg_write,
  95. };
  96. static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg)
  97. {
  98. void __iomem *addr;
  99. u32 size;
  100. size = (cfg->end_bus_number + 1) << 20;
  101. addr = ioremap_nocache(cfg->address, size);
  102. if (addr) {
  103. printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n",
  104. cfg->address, cfg->address + size - 1);
  105. }
  106. return addr;
  107. }
  108. int __init pci_mmcfg_arch_reachable(unsigned int seg, unsigned int bus,
  109. unsigned int devfn)
  110. {
  111. return pci_dev_base(seg, bus, devfn) != NULL;
  112. }
  113. int __init pci_mmcfg_arch_init(void)
  114. {
  115. int i;
  116. pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) *
  117. pci_mmcfg_config_num, GFP_KERNEL);
  118. if (pci_mmcfg_virt == NULL) {
  119. printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
  120. return 0;
  121. }
  122. for (i = 0; i < pci_mmcfg_config_num; ++i) {
  123. pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
  124. pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]);
  125. if (!pci_mmcfg_virt[i].virt) {
  126. printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
  127. "segment %d\n",
  128. pci_mmcfg_config[i].pci_segment);
  129. return 0;
  130. }
  131. }
  132. raw_pci_ops = &pci_mmcfg;
  133. return 1;
  134. }