mmconfig_32.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <asm/e820.h>
  13. #include <asm/pci_x86.h>
  14. #include <acpi/acpi.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. struct pci_mmcfg_region *cfg;
  26. list_for_each_entry(cfg, &pci_mmcfg_list, list)
  27. if (cfg->segment == seg &&
  28. (cfg->start_bus <= bus) &&
  29. (cfg->end_bus >= bus))
  30. return cfg->address;
  31. /* Fall back to type 0 */
  32. return 0;
  33. }
  34. /*
  35. * This is always called under pci_config_lock
  36. */
  37. static void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
  38. {
  39. u32 dev_base = base | PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12);
  40. int cpu = smp_processor_id();
  41. if (dev_base != mmcfg_last_accessed_device ||
  42. cpu != mmcfg_last_accessed_cpu) {
  43. mmcfg_last_accessed_device = dev_base;
  44. mmcfg_last_accessed_cpu = cpu;
  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. u32 base;
  53. if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
  54. err: *value = -1;
  55. return -EINVAL;
  56. }
  57. base = get_base_addr(seg, bus, devfn);
  58. if (!base)
  59. goto err;
  60. spin_lock_irqsave(&pci_config_lock, flags);
  61. pci_exp_set_dev_base(base, bus, devfn);
  62. switch (len) {
  63. case 1:
  64. *value = mmio_config_readb(mmcfg_virt_addr + reg);
  65. break;
  66. case 2:
  67. *value = mmio_config_readw(mmcfg_virt_addr + reg);
  68. break;
  69. case 4:
  70. *value = mmio_config_readl(mmcfg_virt_addr + reg);
  71. break;
  72. }
  73. spin_unlock_irqrestore(&pci_config_lock, flags);
  74. return 0;
  75. }
  76. static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
  77. unsigned int devfn, int reg, int len, u32 value)
  78. {
  79. unsigned long flags;
  80. u32 base;
  81. if ((bus > 255) || (devfn > 255) || (reg > 4095))
  82. return -EINVAL;
  83. base = get_base_addr(seg, bus, devfn);
  84. if (!base)
  85. return -EINVAL;
  86. spin_lock_irqsave(&pci_config_lock, flags);
  87. pci_exp_set_dev_base(base, bus, devfn);
  88. switch (len) {
  89. case 1:
  90. mmio_config_writeb(mmcfg_virt_addr + reg, value);
  91. break;
  92. case 2:
  93. mmio_config_writew(mmcfg_virt_addr + reg, value);
  94. break;
  95. case 4:
  96. mmio_config_writel(mmcfg_virt_addr + reg, value);
  97. break;
  98. }
  99. spin_unlock_irqrestore(&pci_config_lock, flags);
  100. return 0;
  101. }
  102. static struct pci_raw_ops pci_mmcfg = {
  103. .read = pci_mmcfg_read,
  104. .write = pci_mmcfg_write,
  105. };
  106. int __init pci_mmcfg_arch_init(void)
  107. {
  108. printk(KERN_INFO "PCI: Using MMCONFIG for extended config space\n");
  109. raw_pci_ext_ops = &pci_mmcfg;
  110. return 1;
  111. }
  112. void __init pci_mmcfg_arch_free(void)
  113. {
  114. }