mmconfig_64.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. addr = get_virt(seg, bus);
  37. if (!addr)
  38. return NULL;
  39. return addr + ((bus << 20) | (devfn << 12));
  40. }
  41. static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
  42. unsigned int devfn, int reg, int len, u32 *value)
  43. {
  44. char __iomem *addr;
  45. /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
  46. if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
  47. err: *value = -1;
  48. return -EINVAL;
  49. }
  50. if (reg < 256)
  51. return pci_conf1_read(seg,bus,devfn,reg,len,value);
  52. addr = pci_dev_base(seg, bus, devfn);
  53. if (!addr)
  54. goto err;
  55. switch (len) {
  56. case 1:
  57. *value = mmio_config_readb(addr + reg);
  58. break;
  59. case 2:
  60. *value = mmio_config_readw(addr + reg);
  61. break;
  62. case 4:
  63. *value = mmio_config_readl(addr + reg);
  64. break;
  65. }
  66. return 0;
  67. }
  68. static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
  69. unsigned int devfn, int reg, int len, u32 value)
  70. {
  71. char __iomem *addr;
  72. /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
  73. if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
  74. return -EINVAL;
  75. if (reg < 256)
  76. return pci_conf1_write(seg,bus,devfn,reg,len,value);
  77. addr = pci_dev_base(seg, bus, devfn);
  78. if (!addr)
  79. return -EINVAL;
  80. switch (len) {
  81. case 1:
  82. mmio_config_writeb(addr + reg, value);
  83. break;
  84. case 2:
  85. mmio_config_writew(addr + reg, value);
  86. break;
  87. case 4:
  88. mmio_config_writel(addr + reg, value);
  89. break;
  90. }
  91. return 0;
  92. }
  93. static struct pci_raw_ops pci_mmcfg = {
  94. .read = pci_mmcfg_read,
  95. .write = pci_mmcfg_write,
  96. };
  97. static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg)
  98. {
  99. void __iomem *addr;
  100. u32 size;
  101. size = (cfg->end_bus_number + 1) << 20;
  102. addr = ioremap_nocache(cfg->address, size);
  103. if (addr) {
  104. printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n",
  105. cfg->address, cfg->address + size - 1);
  106. }
  107. return addr;
  108. }
  109. int __init pci_mmcfg_arch_init(void)
  110. {
  111. int i;
  112. pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) *
  113. pci_mmcfg_config_num, GFP_KERNEL);
  114. if (pci_mmcfg_virt == NULL) {
  115. printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
  116. return 0;
  117. }
  118. for (i = 0; i < pci_mmcfg_config_num; ++i) {
  119. pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
  120. pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]);
  121. if (!pci_mmcfg_virt[i].virt) {
  122. printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
  123. "segment %d\n",
  124. pci_mmcfg_config[i].pci_segment);
  125. return 0;
  126. }
  127. }
  128. raw_pci_ops = &pci_mmcfg;
  129. return 1;
  130. }