mmconfig.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /* aperture is up to 256MB but BIOS may reserve less */
  14. #define MMCONFIG_APER_MIN (2 * 1024*1024)
  15. #define MMCONFIG_APER_MAX (256 * 1024*1024)
  16. /* Verify the first 16 busses. We assume that systems with more busses
  17. get MCFG right. */
  18. #define PCI_MMCFG_MAX_CHECK_BUS 16
  19. /* Static virtual mapping of the MMCONFIG aperture */
  20. struct mmcfg_virt {
  21. struct acpi_mcfg_allocation *cfg;
  22. char __iomem *virt;
  23. };
  24. static struct mmcfg_virt *pci_mmcfg_virt;
  25. static char __iomem *get_virt(unsigned int seg, unsigned bus)
  26. {
  27. int cfg_num = -1;
  28. struct acpi_mcfg_allocation *cfg;
  29. while (1) {
  30. ++cfg_num;
  31. if (cfg_num >= pci_mmcfg_config_num)
  32. break;
  33. cfg = pci_mmcfg_virt[cfg_num].cfg;
  34. if (cfg->pci_segment != seg)
  35. continue;
  36. if ((cfg->start_bus_number <= bus) &&
  37. (cfg->end_bus_number >= bus))
  38. return pci_mmcfg_virt[cfg_num].virt;
  39. }
  40. /* Handle more broken MCFG tables on Asus etc.
  41. They only contain a single entry for bus 0-0. Assume
  42. this applies to all busses. */
  43. cfg = &pci_mmcfg_config[0];
  44. if (pci_mmcfg_config_num == 1 &&
  45. cfg->pci_segment == 0 &&
  46. (cfg->start_bus_number | cfg->end_bus_number) == 0)
  47. return pci_mmcfg_virt[0].virt;
  48. /* Fall back to type 0 */
  49. return NULL;
  50. }
  51. static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
  52. {
  53. char __iomem *addr;
  54. if (seg == 0 && bus < PCI_MMCFG_MAX_CHECK_BUS &&
  55. test_bit(32*bus + PCI_SLOT(devfn), pci_mmcfg_fallback_slots))
  56. return NULL;
  57. addr = get_virt(seg, bus);
  58. if (!addr)
  59. return NULL;
  60. return addr + ((bus << 20) | (devfn << 12));
  61. }
  62. static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
  63. unsigned int devfn, int reg, int len, u32 *value)
  64. {
  65. char __iomem *addr;
  66. /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
  67. if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
  68. *value = -1;
  69. return -EINVAL;
  70. }
  71. addr = pci_dev_base(seg, bus, devfn);
  72. if (!addr)
  73. return pci_conf1_read(seg,bus,devfn,reg,len,value);
  74. switch (len) {
  75. case 1:
  76. *value = readb(addr + reg);
  77. break;
  78. case 2:
  79. *value = readw(addr + reg);
  80. break;
  81. case 4:
  82. *value = readl(addr + reg);
  83. break;
  84. }
  85. return 0;
  86. }
  87. static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
  88. unsigned int devfn, int reg, int len, u32 value)
  89. {
  90. char __iomem *addr;
  91. /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
  92. if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
  93. return -EINVAL;
  94. addr = pci_dev_base(seg, bus, devfn);
  95. if (!addr)
  96. return pci_conf1_write(seg,bus,devfn,reg,len,value);
  97. switch (len) {
  98. case 1:
  99. writeb(value, addr + reg);
  100. break;
  101. case 2:
  102. writew(value, addr + reg);
  103. break;
  104. case 4:
  105. writel(value, addr + reg);
  106. break;
  107. }
  108. return 0;
  109. }
  110. static struct pci_raw_ops pci_mmcfg = {
  111. .read = pci_mmcfg_read,
  112. .write = pci_mmcfg_write,
  113. };
  114. int __init pci_mmcfg_arch_init(void)
  115. {
  116. int i;
  117. pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) *
  118. pci_mmcfg_config_num, GFP_KERNEL);
  119. if (pci_mmcfg_virt == NULL) {
  120. printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
  121. return 0;
  122. }
  123. for (i = 0; i < pci_mmcfg_config_num; ++i) {
  124. pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
  125. pci_mmcfg_virt[i].virt = ioremap_nocache(pci_mmcfg_config[i].address,
  126. MMCONFIG_APER_MAX);
  127. if (!pci_mmcfg_virt[i].virt) {
  128. printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
  129. "segment %d\n",
  130. pci_mmcfg_config[i].pci_segment);
  131. return 0;
  132. }
  133. printk(KERN_INFO "PCI: Using MMCONFIG at %Lx\n",
  134. pci_mmcfg_config[i].address);
  135. }
  136. raw_pci_ops = &pci_mmcfg;
  137. return 1;
  138. }