mmconfig.c 5.3 KB

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