blob: dfe84c322552bc015d54f04a18202131eac27971 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
8#include <linux/pci.h>
9#include <linux/init.h>
Greg Kroah-Hartman54549392005-06-23 17:35:56 -070010#include <linux/acpi.h>
Andi Kleend6ece542005-12-12 22:17:11 -080011#include <linux/bitmap.h>
Arjan van de Ven946f2ee2006-04-07 19:49:30 +020012#include <asm/e820.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "pci.h"
15
16#define MMCONFIG_APER_SIZE (256*1024*1024)
17
Andi Kleend6ece542005-12-12 22:17:11 -080018static DECLARE_BITMAP(fallback_slots, 32);
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/* Static virtual mapping of the MMCONFIG aperture */
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070021struct mmcfg_virt {
22 struct acpi_table_mcfg_config *cfg;
Al Viro8b8a4e32005-12-15 09:17:44 +000023 char __iomem *virt;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070024};
25static struct mmcfg_virt *pci_mmcfg_virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Al Viro8b8a4e32005-12-15 09:17:44 +000027static char __iomem *get_virt(unsigned int seg, unsigned bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070029 int cfg_num = -1;
30 struct acpi_table_mcfg_config *cfg;
31
32 while (1) {
33 ++cfg_num;
Andi Kleen31030392006-01-27 02:03:50 +010034 if (cfg_num >= pci_mmcfg_config_num)
35 break;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070036 cfg = pci_mmcfg_virt[cfg_num].cfg;
37 if (cfg->pci_segment_group_number != seg)
38 continue;
39 if ((cfg->start_bus_number <= bus) &&
40 (cfg->end_bus_number >= bus))
41 return pci_mmcfg_virt[cfg_num].virt;
42 }
Andi Kleen31030392006-01-27 02:03:50 +010043
44 /* Handle more broken MCFG tables on Asus etc.
45 They only contain a single entry for bus 0-0. Assume
46 this applies to all busses. */
47 cfg = &pci_mmcfg_config[0];
48 if (pci_mmcfg_config_num == 1 &&
49 cfg->pci_segment_group_number == 0 &&
50 (cfg->start_bus_number | cfg->end_bus_number) == 0)
Andi Kleen1de6bf32006-02-03 21:51:29 +010051 return pci_mmcfg_virt[0].virt;
Andi Kleen31030392006-01-27 02:03:50 +010052
53 /* Fall back to type 0 */
Al Virocc598532006-02-03 20:28:01 -050054 return NULL;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070055}
56
Al Viro8b8a4e32005-12-15 09:17:44 +000057static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070058{
Al Viro8b8a4e32005-12-15 09:17:44 +000059 char __iomem *addr;
Akinobu Mita3d1712c2006-03-24 03:15:11 -080060 if (seg == 0 && bus == 0 && test_bit(PCI_SLOT(devfn), fallback_slots))
Andi Kleend6ece542005-12-12 22:17:11 -080061 return NULL;
62 addr = get_virt(seg, bus);
Andi Kleen928cf8c2005-12-12 22:17:10 -080063 if (!addr)
64 return NULL;
65 return addr + ((bus << 20) | (devfn << 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
69 unsigned int devfn, int reg, int len, u32 *value)
70{
Al Viro8b8a4e32005-12-15 09:17:44 +000071 char __iomem *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Andi Kleen928cf8c2005-12-12 22:17:10 -080073 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (unlikely(!value || (bus > 255) || (devfn > 255) || (reg > 4095)))
75 return -EINVAL;
76
Andi Kleen928cf8c2005-12-12 22:17:10 -080077 addr = pci_dev_base(seg, bus, devfn);
78 if (!addr)
79 return pci_conf1_read(seg,bus,devfn,reg,len,value);
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 switch (len) {
82 case 1:
83 *value = readb(addr + reg);
84 break;
85 case 2:
86 *value = readw(addr + reg);
87 break;
88 case 4:
89 *value = readl(addr + reg);
90 break;
91 }
92
93 return 0;
94}
95
96static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
97 unsigned int devfn, int reg, int len, u32 value)
98{
Al Viro8b8a4e32005-12-15 09:17:44 +000099 char __iomem *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Andi Kleen928cf8c2005-12-12 22:17:10 -0800101 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
103 return -EINVAL;
104
Andi Kleen928cf8c2005-12-12 22:17:10 -0800105 addr = pci_dev_base(seg, bus, devfn);
106 if (!addr)
107 return pci_conf1_write(seg,bus,devfn,reg,len,value);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 switch (len) {
110 case 1:
111 writeb(value, addr + reg);
112 break;
113 case 2:
114 writew(value, addr + reg);
115 break;
116 case 4:
117 writel(value, addr + reg);
118 break;
119 }
120
121 return 0;
122}
123
124static struct pci_raw_ops pci_mmcfg = {
125 .read = pci_mmcfg_read,
126 .write = pci_mmcfg_write,
127};
128
Andi Kleend6ece542005-12-12 22:17:11 -0800129/* K8 systems have some devices (typically in the builtin northbridge)
130 that are only accessible using type1
131 Normally this can be expressed in the MCFG by not listing them
132 and assigning suitable _SEGs, but this isn't implemented in some BIOS.
133 Instead try to discover all devices on bus 0 that are unreachable using MM
134 and fallback for them.
135 We only do this for bus 0/seg 0 */
136static __init void unreachable_devices(void)
137{
138 int i;
139 for (i = 0; i < 32; i++) {
140 u32 val1;
Al Viro8b8a4e32005-12-15 09:17:44 +0000141 char __iomem *addr;
Andi Kleend6ece542005-12-12 22:17:11 -0800142
143 pci_conf1_read(0, 0, PCI_DEVFN(i,0), 0, 4, &val1);
144 if (val1 == 0xffffffff)
145 continue;
146 addr = pci_dev_base(0, 0, PCI_DEVFN(i, 0));
147 if (addr == NULL|| readl(addr) != val1) {
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800148 set_bit(i, fallback_slots);
Andi Kleend6ece542005-12-12 22:17:11 -0800149 }
150 }
151}
152
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800153void __init pci_mmcfg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700155 int i;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800158 return;
Greg Kroah-Hartman54549392005-06-23 17:35:56 -0700159
160 acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
161 if ((pci_mmcfg_config_num == 0) ||
162 (pci_mmcfg_config == NULL) ||
163 (pci_mmcfg_config[0].base_address == 0))
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800164 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Arjan van de Ven946f2ee2006-04-07 19:49:30 +0200166 if (!e820_all_mapped(pci_mmcfg_config[0].base_address,
167 pci_mmcfg_config[0].base_address + MMCONFIG_APER_SIZE,
168 E820_RESERVED)) {
169 printk(KERN_ERR "PCI: BIOS Bug: MCFG area is not E820-reserved\n");
170 printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
171 return;
172 }
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /* RED-PEN i386 doesn't do _nocache right now */
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700175 pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) * pci_mmcfg_config_num, GFP_KERNEL);
176 if (pci_mmcfg_virt == NULL) {
177 printk("PCI: Can not allocate memory for mmconfig structures\n");
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800178 return;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700179 }
180 for (i = 0; i < pci_mmcfg_config_num; ++i) {
181 pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
182 pci_mmcfg_virt[i].virt = ioremap_nocache(pci_mmcfg_config[i].base_address, MMCONFIG_APER_SIZE);
183 if (!pci_mmcfg_virt[i].virt) {
184 printk("PCI: Cannot map mmconfig aperture for segment %d\n",
185 pci_mmcfg_config[i].pci_segment_group_number);
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800186 return;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700187 }
188 printk(KERN_INFO "PCI: Using MMCONFIG at %x\n", pci_mmcfg_config[i].base_address);
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Andi Kleend6ece542005-12-12 22:17:11 -0800191 unreachable_devices();
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 raw_pci_ops = &pci_mmcfg;
194 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}