mmconfig.c 6.3 KB

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