blob: 22ff1b07d4e0d5be01bd989cf257044ac45911ae [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "pci.h"
12
13#define MMCONFIG_APER_SIZE (256*1024*1024)
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015/* Static virtual mapping of the MMCONFIG aperture */
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070016struct mmcfg_virt {
17 struct acpi_table_mcfg_config *cfg;
18 char *virt;
19};
20static struct mmcfg_virt *pci_mmcfg_virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Andi Kleen928cf8c2005-12-12 22:17:10 -080022static char *get_virt(unsigned int seg, unsigned bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070024 int cfg_num = -1;
25 struct acpi_table_mcfg_config *cfg;
26
27 while (1) {
28 ++cfg_num;
29 if (cfg_num >= pci_mmcfg_config_num) {
Andi Kleen928cf8c2005-12-12 22:17:10 -080030 /* Not found - fall back to type 1. This happens
31 e.g. on the internal devices of a K8 northbridge. */
32 return NULL;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070033 }
34 cfg = pci_mmcfg_virt[cfg_num].cfg;
35 if (cfg->pci_segment_group_number != seg)
36 continue;
37 if ((cfg->start_bus_number <= bus) &&
38 (cfg->end_bus_number >= bus))
39 return pci_mmcfg_virt[cfg_num].virt;
40 }
41}
42
43static inline char *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
44{
Andi Kleen928cf8c2005-12-12 22:17:10 -080045 char *addr = get_virt(seg, bus);
46 if (!addr)
47 return NULL;
48 return addr + ((bus << 20) | (devfn << 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
52 unsigned int devfn, int reg, int len, u32 *value)
53{
Andi Kleen928cf8c2005-12-12 22:17:10 -080054 char *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Andi Kleen928cf8c2005-12-12 22:17:10 -080056 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (unlikely(!value || (bus > 255) || (devfn > 255) || (reg > 4095)))
58 return -EINVAL;
59
Andi Kleen928cf8c2005-12-12 22:17:10 -080060 addr = pci_dev_base(seg, bus, devfn);
61 if (!addr)
62 return pci_conf1_read(seg,bus,devfn,reg,len,value);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 switch (len) {
65 case 1:
66 *value = readb(addr + reg);
67 break;
68 case 2:
69 *value = readw(addr + reg);
70 break;
71 case 4:
72 *value = readl(addr + reg);
73 break;
74 }
75
76 return 0;
77}
78
79static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
80 unsigned int devfn, int reg, int len, u32 value)
81{
Andi Kleen928cf8c2005-12-12 22:17:10 -080082 char *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Andi Kleen928cf8c2005-12-12 22:17:10 -080084 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
86 return -EINVAL;
87
Andi Kleen928cf8c2005-12-12 22:17:10 -080088 addr = pci_dev_base(seg, bus, devfn);
89 if (!addr)
90 return pci_conf1_write(seg,bus,devfn,reg,len,value);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 switch (len) {
93 case 1:
94 writeb(value, addr + reg);
95 break;
96 case 2:
97 writew(value, addr + reg);
98 break;
99 case 4:
100 writel(value, addr + reg);
101 break;
102 }
103
104 return 0;
105}
106
107static struct pci_raw_ops pci_mmcfg = {
108 .read = pci_mmcfg_read,
109 .write = pci_mmcfg_write,
110};
111
112static int __init pci_mmcfg_init(void)
113{
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700114 int i;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
117 return 0;
Greg Kroah-Hartman54549392005-06-23 17:35:56 -0700118
119 acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
120 if ((pci_mmcfg_config_num == 0) ||
121 (pci_mmcfg_config == NULL) ||
122 (pci_mmcfg_config[0].base_address == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /* RED-PEN i386 doesn't do _nocache right now */
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700126 pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) * pci_mmcfg_config_num, GFP_KERNEL);
127 if (pci_mmcfg_virt == NULL) {
128 printk("PCI: Can not allocate memory for mmconfig structures\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700130 }
131 for (i = 0; i < pci_mmcfg_config_num; ++i) {
132 pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
133 pci_mmcfg_virt[i].virt = ioremap_nocache(pci_mmcfg_config[i].base_address, MMCONFIG_APER_SIZE);
134 if (!pci_mmcfg_virt[i].virt) {
135 printk("PCI: Cannot map mmconfig aperture for segment %d\n",
136 pci_mmcfg_config[i].pci_segment_group_number);
137 return 0;
138 }
139 printk(KERN_INFO "PCI: Using MMCONFIG at %x\n", pci_mmcfg_config[i].base_address);
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 raw_pci_ops = &pci_mmcfg;
143 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
144
145 return 0;
146}
147
148arch_initcall(pci_mmcfg_init);