PCI x86: always use conf1 to access config space below 256 bytes
[deliverable/linux.git] / arch / x86 / pci / mmconfig_64.c
CommitLineData
1da177e4
LT
1/*
2 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
15a58ed1 3 *
1da177e4
LT
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>
54549391 10#include <linux/acpi.h>
d6ece549 11#include <linux/bitmap.h>
946f2ee5
AV
12#include <asm/e820.h>
13
1da177e4
LT
14#include "pci.h"
15
1da177e4 16/* Static virtual mapping of the MMCONFIG aperture */
1cde8a16 17struct mmcfg_virt {
15a58ed1 18 struct acpi_mcfg_allocation *cfg;
8b8a4e33 19 char __iomem *virt;
1cde8a16
GKH
20};
21static struct mmcfg_virt *pci_mmcfg_virt;
1da177e4 22
8b8a4e33 23static char __iomem *get_virt(unsigned int seg, unsigned bus)
1da177e4 24{
15a58ed1 25 struct acpi_mcfg_allocation *cfg;
429d512e 26 int cfg_num;
1cde8a16 27
429d512e 28 for (cfg_num = 0; cfg_num < pci_mmcfg_config_num; cfg_num++) {
1cde8a16 29 cfg = pci_mmcfg_virt[cfg_num].cfg;
429d512e
OH
30 if (cfg->pci_segment == seg &&
31 (cfg->start_bus_number <= bus) &&
1cde8a16
GKH
32 (cfg->end_bus_number >= bus))
33 return pci_mmcfg_virt[cfg_num].virt;
34 }
3103039c 35
3103039c 36 /* Fall back to type 0 */
cc59853b 37 return NULL;
1cde8a16
GKH
38}
39
8b8a4e33 40static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
1cde8a16 41{
8b8a4e33 42 char __iomem *addr;
a0ca9909 43
d6ece549 44 addr = get_virt(seg, bus);
928cf8c6
AK
45 if (!addr)
46 return NULL;
47 return addr + ((bus << 20) | (devfn << 12));
1da177e4
LT
48}
49
50static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
51 unsigned int devfn, int reg, int len, u32 *value)
52{
8b8a4e33 53 char __iomem *addr;
1da177e4 54
928cf8c6 55 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
ecc16ba9 56 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
a0ca9909 57err: *value = -1;
1da177e4 58 return -EINVAL;
49c93e84 59 }
1da177e4 60
a0ca9909
IK
61 if (reg < 256)
62 return pci_conf1_read(seg,bus,devfn,reg,len,value);
63
928cf8c6
AK
64 addr = pci_dev_base(seg, bus, devfn);
65 if (!addr)
a0ca9909 66 goto err;
928cf8c6 67
1da177e4
LT
68 switch (len) {
69 case 1:
3320ad99 70 *value = mmio_config_readb(addr + reg);
1da177e4
LT
71 break;
72 case 2:
3320ad99 73 *value = mmio_config_readw(addr + reg);
1da177e4
LT
74 break;
75 case 4:
3320ad99 76 *value = mmio_config_readl(addr + reg);
1da177e4
LT
77 break;
78 }
79
80 return 0;
81}
82
83static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
84 unsigned int devfn, int reg, int len, u32 value)
85{
8b8a4e33 86 char __iomem *addr;
1da177e4 87
928cf8c6 88 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
1da177e4
LT
89 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
90 return -EINVAL;
91
a0ca9909
IK
92 if (reg < 256)
93 return pci_conf1_write(seg,bus,devfn,reg,len,value);
94
928cf8c6
AK
95 addr = pci_dev_base(seg, bus, devfn);
96 if (!addr)
a0ca9909 97 return -EINVAL;
928cf8c6 98
1da177e4
LT
99 switch (len) {
100 case 1:
3320ad99 101 mmio_config_writeb(addr + reg, value);
1da177e4
LT
102 break;
103 case 2:
3320ad99 104 mmio_config_writew(addr + reg, value);
1da177e4
LT
105 break;
106 case 4:
3320ad99 107 mmio_config_writel(addr + reg, value);
1da177e4
LT
108 break;
109 }
110
111 return 0;
112}
113
114static struct pci_raw_ops pci_mmcfg = {
115 .read = pci_mmcfg_read,
116 .write = pci_mmcfg_write,
117};
118
44de0203
OH
119static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg)
120{
121 void __iomem *addr;
122 u32 size;
123
124 size = (cfg->end_bus_number + 1) << 20;
125 addr = ioremap_nocache(cfg->address, size);
126 if (addr) {
127 printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n",
128 cfg->address, cfg->address + size - 1);
129 }
130 return addr;
131}
132
b7867394 133int __init pci_mmcfg_arch_init(void)
1da177e4 134{
1cde8a16 135 int i;
b7867394
OG
136 pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) *
137 pci_mmcfg_config_num, GFP_KERNEL);
1cde8a16 138 if (pci_mmcfg_virt == NULL) {
3095fc0c 139 printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
b7867394 140 return 0;
1cde8a16 141 }
b7867394 142
1cde8a16
GKH
143 for (i = 0; i < pci_mmcfg_config_num; ++i) {
144 pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
faed197b 145 pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]);
1cde8a16 146 if (!pci_mmcfg_virt[i].virt) {
3095fc0c
DJ
147 printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
148 "segment %d\n",
15a58ed1 149 pci_mmcfg_config[i].pci_segment);
b7867394 150 return 0;
1cde8a16 151 }
1cde8a16 152 }
1da177e4 153 raw_pci_ops = &pci_mmcfg;
b7867394 154 return 1;
1da177e4 155}
This page took 0.31295 seconds and 5 git commands to generate.