mmconfig.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2004 Matthew Wilcox <matthew@wil.cx>
  3. * Copyright (C) 2004 Intel Corp.
  4. *
  5. * This code is released under the GNU General Public License version 2.
  6. */
  7. /*
  8. * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
  9. */
  10. #include <linux/pci.h>
  11. #include <linux/init.h>
  12. #include <linux/acpi.h>
  13. #include <asm/e820.h>
  14. #include "pci.h"
  15. /* Assume systems with more busses have correct MCFG */
  16. #define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG))
  17. /* The base address of the last MMCONFIG device accessed */
  18. static u32 mmcfg_last_accessed_device;
  19. static int mmcfg_last_accessed_cpu;
  20. /*
  21. * Functions for accessing PCI configuration space with MMCONFIG accesses
  22. */
  23. static u32 get_base_addr(unsigned int seg, int bus, unsigned devfn)
  24. {
  25. int cfg_num = -1;
  26. struct acpi_mcfg_allocation *cfg;
  27. if (seg == 0 && bus < PCI_MMCFG_MAX_CHECK_BUS &&
  28. test_bit(PCI_SLOT(devfn) + 32*bus, pci_mmcfg_fallback_slots))
  29. return 0;
  30. while (1) {
  31. ++cfg_num;
  32. if (cfg_num >= pci_mmcfg_config_num) {
  33. break;
  34. }
  35. cfg = &pci_mmcfg_config[cfg_num];
  36. if (cfg->pci_segment != seg)
  37. continue;
  38. if ((cfg->start_bus_number <= bus) &&
  39. (cfg->end_bus_number >= bus))
  40. return cfg->address;
  41. }
  42. /* Handle more broken MCFG tables on Asus etc.
  43. They only contain a single entry for bus 0-0. Assume
  44. this applies to all busses. */
  45. cfg = &pci_mmcfg_config[0];
  46. if (pci_mmcfg_config_num == 1 &&
  47. cfg->pci_segment == 0 &&
  48. (cfg->start_bus_number | cfg->end_bus_number) == 0)
  49. return cfg->address;
  50. /* Fall back to type 0 */
  51. return 0;
  52. }
  53. /*
  54. * This is always called under pci_config_lock
  55. */
  56. static void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
  57. {
  58. u32 dev_base = base | (bus << 20) | (devfn << 12);
  59. int cpu = smp_processor_id();
  60. if (dev_base != mmcfg_last_accessed_device ||
  61. cpu != mmcfg_last_accessed_cpu) {
  62. mmcfg_last_accessed_device = dev_base;
  63. mmcfg_last_accessed_cpu = cpu;
  64. set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
  65. }
  66. }
  67. static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
  68. unsigned int devfn, int reg, int len, u32 *value)
  69. {
  70. unsigned long flags;
  71. u32 base;
  72. if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
  73. *value = -1;
  74. return -EINVAL;
  75. }
  76. base = get_base_addr(seg, bus, devfn);
  77. if (!base)
  78. return pci_conf1_read(seg,bus,devfn,reg,len,value);
  79. spin_lock_irqsave(&pci_config_lock, flags);
  80. pci_exp_set_dev_base(base, bus, devfn);
  81. switch (len) {
  82. case 1:
  83. *value = readb(mmcfg_virt_addr + reg);
  84. break;
  85. case 2:
  86. *value = readw(mmcfg_virt_addr + reg);
  87. break;
  88. case 4:
  89. *value = readl(mmcfg_virt_addr + reg);
  90. break;
  91. }
  92. spin_unlock_irqrestore(&pci_config_lock, flags);
  93. return 0;
  94. }
  95. static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
  96. unsigned int devfn, int reg, int len, u32 value)
  97. {
  98. unsigned long flags;
  99. u32 base;
  100. if ((bus > 255) || (devfn > 255) || (reg > 4095))
  101. return -EINVAL;
  102. base = get_base_addr(seg, bus, devfn);
  103. if (!base)
  104. return pci_conf1_write(seg,bus,devfn,reg,len,value);
  105. spin_lock_irqsave(&pci_config_lock, flags);
  106. pci_exp_set_dev_base(base, bus, devfn);
  107. switch (len) {
  108. case 1:
  109. writeb(value, mmcfg_virt_addr + reg);
  110. break;
  111. case 2:
  112. writew(value, mmcfg_virt_addr + reg);
  113. break;
  114. case 4:
  115. writel(value, mmcfg_virt_addr + reg);
  116. break;
  117. }
  118. spin_unlock_irqrestore(&pci_config_lock, flags);
  119. return 0;
  120. }
  121. static struct pci_raw_ops pci_mmcfg = {
  122. .read = pci_mmcfg_read,
  123. .write = pci_mmcfg_write,
  124. };
  125. int __init pci_mmcfg_arch_init(void)
  126. {
  127. printk(KERN_INFO "PCI: Using MMCONFIG\n");
  128. raw_pci_ops = &pci_mmcfg;
  129. return 1;
  130. }