mmconfig.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "pci.h"
  14. #define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG))
  15. /* The base address of the last MMCONFIG device accessed */
  16. static u32 mmcfg_last_accessed_device;
  17. /*
  18. * Functions for accessing PCI configuration space with MMCONFIG accesses
  19. */
  20. static u32 get_base_addr(unsigned int seg, int bus)
  21. {
  22. int cfg_num = -1;
  23. struct acpi_table_mcfg_config *cfg;
  24. while (1) {
  25. ++cfg_num;
  26. if (cfg_num >= pci_mmcfg_config_num) {
  27. /* something bad is going on, no cfg table is found. */
  28. /* so we fall back to the old way we used to do this */
  29. /* and just rely on the first entry to be correct. */
  30. return pci_mmcfg_config[0].base_address;
  31. }
  32. cfg = &pci_mmcfg_config[cfg_num];
  33. if (cfg->pci_segment_group_number != seg)
  34. continue;
  35. if ((cfg->start_bus_number <= bus) &&
  36. (cfg->end_bus_number >= bus))
  37. return cfg->base_address;
  38. }
  39. }
  40. static inline void pci_exp_set_dev_base(unsigned int seg, int bus, int devfn)
  41. {
  42. u32 dev_base = get_base_addr(seg, bus) | (bus << 20) | (devfn << 12);
  43. if (dev_base != mmcfg_last_accessed_device) {
  44. mmcfg_last_accessed_device = dev_base;
  45. set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
  46. }
  47. }
  48. static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
  49. unsigned int devfn, int reg, int len, u32 *value)
  50. {
  51. unsigned long flags;
  52. if (!value || (bus > 255) || (devfn > 255) || (reg > 4095))
  53. return -EINVAL;
  54. spin_lock_irqsave(&pci_config_lock, flags);
  55. pci_exp_set_dev_base(seg, bus, devfn);
  56. switch (len) {
  57. case 1:
  58. *value = readb(mmcfg_virt_addr + reg);
  59. break;
  60. case 2:
  61. *value = readw(mmcfg_virt_addr + reg);
  62. break;
  63. case 4:
  64. *value = readl(mmcfg_virt_addr + reg);
  65. break;
  66. }
  67. spin_unlock_irqrestore(&pci_config_lock, flags);
  68. return 0;
  69. }
  70. static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
  71. unsigned int devfn, int reg, int len, u32 value)
  72. {
  73. unsigned long flags;
  74. if ((bus > 255) || (devfn > 255) || (reg > 4095))
  75. return -EINVAL;
  76. spin_lock_irqsave(&pci_config_lock, flags);
  77. pci_exp_set_dev_base(seg, bus, devfn);
  78. switch (len) {
  79. case 1:
  80. writeb(value, mmcfg_virt_addr + reg);
  81. break;
  82. case 2:
  83. writew(value, mmcfg_virt_addr + reg);
  84. break;
  85. case 4:
  86. writel(value, mmcfg_virt_addr + reg);
  87. break;
  88. }
  89. spin_unlock_irqrestore(&pci_config_lock, flags);
  90. return 0;
  91. }
  92. static struct pci_raw_ops pci_mmcfg = {
  93. .read = pci_mmcfg_read,
  94. .write = pci_mmcfg_write,
  95. };
  96. static int __init pci_mmcfg_init(void)
  97. {
  98. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  99. goto out;
  100. acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
  101. if ((pci_mmcfg_config_num == 0) ||
  102. (pci_mmcfg_config == NULL) ||
  103. (pci_mmcfg_config[0].base_address == 0))
  104. goto out;
  105. printk(KERN_INFO "PCI: Using MMCONFIG\n");
  106. raw_pci_ops = &pci_mmcfg;
  107. pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
  108. out:
  109. return 0;
  110. }
  111. arch_initcall(pci_mmcfg_init);