mmconfig.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "pci.h"
  12. #define MMCONFIG_APER_SIZE (256*1024*1024)
  13. static DECLARE_BITMAP(fallback_slots, 32);
  14. /* Static virtual mapping of the MMCONFIG aperture */
  15. struct mmcfg_virt {
  16. struct acpi_table_mcfg_config *cfg;
  17. char __iomem *virt;
  18. };
  19. static struct mmcfg_virt *pci_mmcfg_virt;
  20. static char __iomem *get_virt(unsigned int seg, unsigned 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. break;
  28. cfg = pci_mmcfg_virt[cfg_num].cfg;
  29. if (cfg->pci_segment_group_number != seg)
  30. continue;
  31. if ((cfg->start_bus_number <= bus) &&
  32. (cfg->end_bus_number >= bus))
  33. return pci_mmcfg_virt[cfg_num].virt;
  34. }
  35. /* Handle more broken MCFG tables on Asus etc.
  36. They only contain a single entry for bus 0-0. Assume
  37. this applies to all busses. */
  38. cfg = &pci_mmcfg_config[0];
  39. if (pci_mmcfg_config_num == 1 &&
  40. cfg->pci_segment_group_number == 0 &&
  41. (cfg->start_bus_number | cfg->end_bus_number) == 0)
  42. return pci_mmcfg_virt[0].virt;
  43. /* Fall back to type 0 */
  44. return 0;
  45. }
  46. static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
  47. {
  48. char __iomem *addr;
  49. if (seg == 0 && bus == 0 && test_bit(PCI_SLOT(devfn), &fallback_slots))
  50. return NULL;
  51. addr = get_virt(seg, bus);
  52. if (!addr)
  53. return NULL;
  54. return addr + ((bus << 20) | (devfn << 12));
  55. }
  56. static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
  57. unsigned int devfn, int reg, int len, u32 *value)
  58. {
  59. char __iomem *addr;
  60. /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
  61. if (unlikely(!value || (bus > 255) || (devfn > 255) || (reg > 4095)))
  62. return -EINVAL;
  63. addr = pci_dev_base(seg, bus, devfn);
  64. if (!addr)
  65. return pci_conf1_read(seg,bus,devfn,reg,len,value);
  66. switch (len) {
  67. case 1:
  68. *value = readb(addr + reg);
  69. break;
  70. case 2:
  71. *value = readw(addr + reg);
  72. break;
  73. case 4:
  74. *value = readl(addr + reg);
  75. break;
  76. }
  77. return 0;
  78. }
  79. static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
  80. unsigned int devfn, int reg, int len, u32 value)
  81. {
  82. char __iomem *addr;
  83. /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
  84. if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
  85. return -EINVAL;
  86. addr = pci_dev_base(seg, bus, devfn);
  87. if (!addr)
  88. return pci_conf1_write(seg,bus,devfn,reg,len,value);
  89. switch (len) {
  90. case 1:
  91. writeb(value, addr + reg);
  92. break;
  93. case 2:
  94. writew(value, addr + reg);
  95. break;
  96. case 4:
  97. writel(value, addr + reg);
  98. break;
  99. }
  100. return 0;
  101. }
  102. static struct pci_raw_ops pci_mmcfg = {
  103. .read = pci_mmcfg_read,
  104. .write = pci_mmcfg_write,
  105. };
  106. /* K8 systems have some devices (typically in the builtin northbridge)
  107. that are only accessible using type1
  108. Normally this can be expressed in the MCFG by not listing them
  109. and assigning suitable _SEGs, but this isn't implemented in some BIOS.
  110. Instead try to discover all devices on bus 0 that are unreachable using MM
  111. and fallback for them.
  112. We only do this for bus 0/seg 0 */
  113. static __init void unreachable_devices(void)
  114. {
  115. int i;
  116. for (i = 0; i < 32; i++) {
  117. u32 val1;
  118. char __iomem *addr;
  119. pci_conf1_read(0, 0, PCI_DEVFN(i,0), 0, 4, &val1);
  120. if (val1 == 0xffffffff)
  121. continue;
  122. addr = pci_dev_base(0, 0, PCI_DEVFN(i, 0));
  123. if (addr == NULL|| readl(addr) != val1) {
  124. set_bit(i, &fallback_slots);
  125. }
  126. }
  127. }
  128. static int __init pci_mmcfg_init(void)
  129. {
  130. int i;
  131. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  132. return 0;
  133. acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
  134. if ((pci_mmcfg_config_num == 0) ||
  135. (pci_mmcfg_config == NULL) ||
  136. (pci_mmcfg_config[0].base_address == 0))
  137. return 0;
  138. /* RED-PEN i386 doesn't do _nocache right now */
  139. pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) * pci_mmcfg_config_num, GFP_KERNEL);
  140. if (pci_mmcfg_virt == NULL) {
  141. printk("PCI: Can not allocate memory for mmconfig structures\n");
  142. return 0;
  143. }
  144. for (i = 0; i < pci_mmcfg_config_num; ++i) {
  145. pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
  146. pci_mmcfg_virt[i].virt = ioremap_nocache(pci_mmcfg_config[i].base_address, MMCONFIG_APER_SIZE);
  147. if (!pci_mmcfg_virt[i].virt) {
  148. printk("PCI: Cannot map mmconfig aperture for segment %d\n",
  149. pci_mmcfg_config[i].pci_segment_group_number);
  150. return 0;
  151. }
  152. printk(KERN_INFO "PCI: Using MMCONFIG at %x\n", pci_mmcfg_config[i].base_address);
  153. }
  154. unreachable_devices();
  155. raw_pci_ops = &pci_mmcfg;
  156. pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
  157. return 0;
  158. }
  159. arch_initcall(pci_mmcfg_init);