mmconfig.c 5.7 KB

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