mmconfig.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /* aperture is up to 256MB but BIOS may reserve less */
  16. #define MMCONFIG_APER_MIN (2 * 1024*1024)
  17. #define MMCONFIG_APER_MAX (256 * 1024*1024)
  18. /* Assume systems with more busses have correct MCFG */
  19. #define MAX_CHECK_BUS 16
  20. #define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG))
  21. /* The base address of the last MMCONFIG device accessed */
  22. static u32 mmcfg_last_accessed_device;
  23. static int mmcfg_last_accessed_cpu;
  24. static DECLARE_BITMAP(fallback_slots, MAX_CHECK_BUS*32);
  25. /*
  26. * Functions for accessing PCI configuration space with MMCONFIG accesses
  27. */
  28. static u32 get_base_addr(unsigned int seg, int bus, unsigned devfn)
  29. {
  30. int cfg_num = -1;
  31. struct acpi_mcfg_allocation *cfg;
  32. if (seg == 0 && bus < MAX_CHECK_BUS &&
  33. test_bit(PCI_SLOT(devfn) + 32*bus, fallback_slots))
  34. return 0;
  35. while (1) {
  36. ++cfg_num;
  37. if (cfg_num >= pci_mmcfg_config_num) {
  38. break;
  39. }
  40. cfg = &pci_mmcfg_config[cfg_num];
  41. if (cfg->pci_segment != seg)
  42. continue;
  43. if ((cfg->start_bus_number <= bus) &&
  44. (cfg->end_bus_number >= bus))
  45. return cfg->address;
  46. }
  47. /* Handle more broken MCFG tables on Asus etc.
  48. They only contain a single entry for bus 0-0. Assume
  49. this applies to all busses. */
  50. cfg = &pci_mmcfg_config[0];
  51. if (pci_mmcfg_config_num == 1 &&
  52. cfg->pci_segment == 0 &&
  53. (cfg->start_bus_number | cfg->end_bus_number) == 0)
  54. return cfg->address;
  55. /* Fall back to type 0 */
  56. return 0;
  57. }
  58. /*
  59. * This is always called under pci_config_lock
  60. */
  61. static void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
  62. {
  63. u32 dev_base = base | (bus << 20) | (devfn << 12);
  64. int cpu = smp_processor_id();
  65. if (dev_base != mmcfg_last_accessed_device ||
  66. cpu != mmcfg_last_accessed_cpu) {
  67. mmcfg_last_accessed_device = dev_base;
  68. mmcfg_last_accessed_cpu = cpu;
  69. set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
  70. }
  71. }
  72. static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
  73. unsigned int devfn, int reg, int len, u32 *value)
  74. {
  75. unsigned long flags;
  76. u32 base;
  77. if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
  78. *value = -1;
  79. return -EINVAL;
  80. }
  81. base = get_base_addr(seg, bus, devfn);
  82. if (!base)
  83. return pci_conf1_read(seg,bus,devfn,reg,len,value);
  84. spin_lock_irqsave(&pci_config_lock, flags);
  85. pci_exp_set_dev_base(base, bus, devfn);
  86. switch (len) {
  87. case 1:
  88. *value = readb(mmcfg_virt_addr + reg);
  89. break;
  90. case 2:
  91. *value = readw(mmcfg_virt_addr + reg);
  92. break;
  93. case 4:
  94. *value = readl(mmcfg_virt_addr + reg);
  95. break;
  96. }
  97. spin_unlock_irqrestore(&pci_config_lock, flags);
  98. return 0;
  99. }
  100. static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
  101. unsigned int devfn, int reg, int len, u32 value)
  102. {
  103. unsigned long flags;
  104. u32 base;
  105. if ((bus > 255) || (devfn > 255) || (reg > 4095))
  106. return -EINVAL;
  107. base = get_base_addr(seg, bus, devfn);
  108. if (!base)
  109. return pci_conf1_write(seg,bus,devfn,reg,len,value);
  110. spin_lock_irqsave(&pci_config_lock, flags);
  111. pci_exp_set_dev_base(base, bus, devfn);
  112. switch (len) {
  113. case 1:
  114. writeb(value, mmcfg_virt_addr + reg);
  115. break;
  116. case 2:
  117. writew(value, mmcfg_virt_addr + reg);
  118. break;
  119. case 4:
  120. writel(value, mmcfg_virt_addr + reg);
  121. break;
  122. }
  123. spin_unlock_irqrestore(&pci_config_lock, flags);
  124. return 0;
  125. }
  126. static struct pci_raw_ops pci_mmcfg = {
  127. .read = pci_mmcfg_read,
  128. .write = pci_mmcfg_write,
  129. };
  130. /* K8 systems have some devices (typically in the builtin northbridge)
  131. that are only accessible using type1
  132. Normally this can be expressed in the MCFG by not listing them
  133. and assigning suitable _SEGs, but this isn't implemented in some BIOS.
  134. Instead try to discover all devices on bus 0 that are unreachable using MM
  135. and fallback for them. */
  136. static __init void unreachable_devices(void)
  137. {
  138. int i, k;
  139. unsigned long flags;
  140. for (k = 0; k < MAX_CHECK_BUS; k++) {
  141. for (i = 0; i < 32; i++) {
  142. u32 val1;
  143. u32 addr;
  144. pci_conf1_read(0, k, PCI_DEVFN(i, 0), 0, 4, &val1);
  145. if (val1 == 0xffffffff)
  146. continue;
  147. /* Locking probably not needed, but safer */
  148. spin_lock_irqsave(&pci_config_lock, flags);
  149. addr = get_base_addr(0, k, PCI_DEVFN(i, 0));
  150. if (addr != 0)
  151. pci_exp_set_dev_base(addr, k, PCI_DEVFN(i, 0));
  152. if (addr == 0 ||
  153. readl((u32 __iomem *)mmcfg_virt_addr) != val1) {
  154. set_bit(i + 32*k, fallback_slots);
  155. printk(KERN_NOTICE
  156. "PCI: No mmconfig possible on %x:%x\n", k, i);
  157. }
  158. spin_unlock_irqrestore(&pci_config_lock, flags);
  159. }
  160. }
  161. }
  162. void __init pci_mmcfg_init(int type)
  163. {
  164. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  165. return;
  166. acpi_table_parse(ACPI_SIG_MCFG, acpi_parse_mcfg);
  167. if ((pci_mmcfg_config_num == 0) ||
  168. (pci_mmcfg_config == NULL) ||
  169. (pci_mmcfg_config[0].address == 0))
  170. return;
  171. /* Only do this check when type 1 works. If it doesn't work
  172. assume we run on a Mac and always use MCFG */
  173. if (type == 1 && !e820_all_mapped(pci_mmcfg_config[0].address,
  174. pci_mmcfg_config[0].address + MMCONFIG_APER_MIN,
  175. E820_RESERVED)) {
  176. printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %lx is not E820-reserved\n",
  177. (unsigned long)pci_mmcfg_config[0].address);
  178. printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
  179. return;
  180. }
  181. printk(KERN_INFO "PCI: Using MMCONFIG\n");
  182. raw_pci_ops = &pci_mmcfg;
  183. pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
  184. unreachable_devices();
  185. }