blob: e783841bd1d7c42ea8ca4607d884ffe1e65ba97f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +03003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +053013#include <asm/pci_x86.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Bjorn Helgaas8c577862009-11-13 17:34:59 -070015#define PREFIX "PCI: "
16
Al Viro8b8a4e32005-12-15 09:17:44 +000017static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070018{
Bjorn Helgaasf6e1d8c2009-11-13 17:35:04 -070019 struct pci_mmcfg_region *cfg = pci_mmconfig_lookup(seg, bus);
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050020
Bjorn Helgaasf6e1d8c2009-11-13 17:35:04 -070021 if (cfg && cfg->virt)
22 return cfg->virt + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12));
23 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024}
25
26static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
27 unsigned int devfn, int reg, int len, u32 *value)
28{
Al Viro8b8a4e32005-12-15 09:17:44 +000029 char __iomem *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Andi Kleen928cf8c2005-12-12 22:17:10 -080031 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
Andi Kleenecc16ba2006-04-11 12:54:48 +020032 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050033err: *value = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return -EINVAL;
Andi Kleen49c93e82006-04-07 19:50:15 +020035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Andi Kleen928cf8c2005-12-12 22:17:10 -080037 addr = pci_dev_base(seg, bus, devfn);
38 if (!addr)
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050039 goto err;
Andi Kleen928cf8c2005-12-12 22:17:10 -080040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 switch (len) {
42 case 1:
dean gaudet3320ad92007-08-10 22:30:59 +020043 *value = mmio_config_readb(addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 break;
45 case 2:
dean gaudet3320ad92007-08-10 22:30:59 +020046 *value = mmio_config_readw(addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 break;
48 case 4:
dean gaudet3320ad92007-08-10 22:30:59 +020049 *value = mmio_config_readl(addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 break;
51 }
52
53 return 0;
54}
55
56static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
57 unsigned int devfn, int reg, int len, u32 value)
58{
Al Viro8b8a4e32005-12-15 09:17:44 +000059 char __iomem *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Andi Kleen928cf8c2005-12-12 22:17:10 -080061 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
63 return -EINVAL;
64
Andi Kleen928cf8c2005-12-12 22:17:10 -080065 addr = pci_dev_base(seg, bus, devfn);
66 if (!addr)
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050067 return -EINVAL;
Andi Kleen928cf8c2005-12-12 22:17:10 -080068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 switch (len) {
70 case 1:
dean gaudet3320ad92007-08-10 22:30:59 +020071 mmio_config_writeb(addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 break;
73 case 2:
dean gaudet3320ad92007-08-10 22:30:59 +020074 mmio_config_writew(addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 break;
76 case 4:
dean gaudet3320ad92007-08-10 22:30:59 +020077 mmio_config_writel(addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
79 }
80
81 return 0;
82}
83
84static struct pci_raw_ops pci_mmcfg = {
85 .read = pci_mmcfg_read,
86 .write = pci_mmcfg_write,
87};
88
Bjorn Helgaasd215a9c2009-11-13 17:34:13 -070089static void __iomem * __init mcfg_ioremap(struct pci_mmcfg_region *cfg)
OGAWA Hirofumi44de0202007-02-13 13:26:20 +010090{
91 void __iomem *addr;
Yinghai Lu068258b2009-03-19 20:55:35 -070092 u64 start, size;
Bjorn Helgaasdf5eb1d2009-11-13 17:34:08 -070093 int num_buses;
OGAWA Hirofumi44de0202007-02-13 13:26:20 +010094
Bjorn Helgaasd7e6b662009-11-13 17:34:18 -070095 start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
96 num_buses = cfg->end_bus - cfg->start_bus + 1;
Bjorn Helgaasdf5eb1d2009-11-13 17:34:08 -070097 size = PCI_MMCFG_BUS_OFFSET(num_buses);
Yinghai Lu068258b2009-03-19 20:55:35 -070098 addr = ioremap_nocache(start, size);
Bjorn Helgaas8c577862009-11-13 17:34:59 -070099 if (addr)
Bjorn Helgaasd7e6b662009-11-13 17:34:18 -0700100 addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
OGAWA Hirofumi44de0202007-02-13 13:26:20 +0100101 return addr;
102}
103
Olivier Galibertb7867392007-02-13 13:26:20 +0100104int __init pci_mmcfg_arch_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Bjorn Helgaas3f0f5502009-11-13 17:34:39 -0700106 struct pci_mmcfg_region *cfg;
Olivier Galibertb7867392007-02-13 13:26:20 +0100107
Bjorn Helgaasff097dd2009-11-13 17:34:49 -0700108 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
Bjorn Helgaas3f0f5502009-11-13 17:34:39 -0700109 cfg->virt = mcfg_ioremap(cfg);
110 if (!cfg->virt) {
Bjorn Helgaas8c577862009-11-13 17:34:59 -0700111 printk(KERN_ERR PREFIX "can't map MMCONFIG at %pR\n",
112 &cfg->res);
Yinghai Lu0b64ad72008-02-15 01:28:41 -0800113 pci_mmcfg_arch_free();
Olivier Galibertb7867392007-02-13 13:26:20 +0100114 return 0;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700115 }
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700116 }
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500117 raw_pci_ext_ops = &pci_mmcfg;
Olivier Galibertb7867392007-02-13 13:26:20 +0100118 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
Yinghai Lu0b64ad72008-02-15 01:28:41 -0800120
121void __init pci_mmcfg_arch_free(void)
122{
Bjorn Helgaas3f0f5502009-11-13 17:34:39 -0700123 struct pci_mmcfg_region *cfg;
Yinghai Lu0b64ad72008-02-15 01:28:41 -0800124
Bjorn Helgaasff097dd2009-11-13 17:34:49 -0700125 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
Bjorn Helgaas3f0f5502009-11-13 17:34:39 -0700126 if (cfg->virt) {
127 iounmap(cfg->virt + PCI_MMCFG_BUS_OFFSET(cfg->start_bus));
128 cfg->virt = NULL;
Yinghai Lu0b64ad72008-02-15 01:28:41 -0800129 }
130 }
Yinghai Lu0b64ad72008-02-15 01:28:41 -0800131}