mmconfig_64.c 3.9 KB

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