mmconfig_64.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 pci_mmcfg_region *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 pci_mmcfg_region *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 + (PCI_MMCFG_BUS_OFFSET(bus) | (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 pci_mmcfg_region *cfg)
  94. {
  95. void __iomem *addr;
  96. u64 start, size;
  97. int num_buses;
  98. start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus_number);
  99. num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
  100. size = PCI_MMCFG_BUS_OFFSET(num_buses);
  101. addr = ioremap_nocache(start, size);
  102. if (addr) {
  103. printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n",
  104. start, start + size - 1);
  105. addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus_number);
  106. }
  107. return addr;
  108. }
  109. int __init pci_mmcfg_arch_init(void)
  110. {
  111. int i;
  112. pci_mmcfg_virt = kzalloc(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. pci_mmcfg_arch_free();
  126. return 0;
  127. }
  128. }
  129. raw_pci_ext_ops = &pci_mmcfg;
  130. return 1;
  131. }
  132. void __init pci_mmcfg_arch_free(void)
  133. {
  134. int i;
  135. if (pci_mmcfg_virt == NULL)
  136. return;
  137. for (i = 0; i < pci_mmcfg_config_num; ++i) {
  138. if (pci_mmcfg_virt[i].virt) {
  139. iounmap(pci_mmcfg_virt[i].virt + PCI_MMCFG_BUS_OFFSET(pci_mmcfg_virt[i].cfg->start_bus_number));
  140. pci_mmcfg_virt[i].virt = NULL;
  141. pci_mmcfg_virt[i].cfg = NULL;
  142. }
  143. }
  144. kfree(pci_mmcfg_virt);
  145. pci_mmcfg_virt = NULL;
  146. }