blob: 5c90975cdf0f8242db167fa9c471c1ea2bf3e512 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/*
9 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
10 */
11
12#include <linux/pci.h>
13#include <linux/init.h>
Jiang Liu376f70a2012-06-22 14:55:12 +080014#include <linux/rcupdate.h>
Arjan van de Ven946f2ee2006-04-07 19:49:30 +020015#include <asm/e820.h>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +053016#include <asm/pci_x86.h>
Feng Tang5f0db7a2009-08-14 15:37:50 -040017#include <acpi/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Andi Kleen8c30b1a742006-04-07 19:50:12 +020019/* Assume systems with more busses have correct MCFG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG))
21
22/* The base address of the last MMCONFIG device accessed */
23static u32 mmcfg_last_accessed_device;
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090024static int mmcfg_last_accessed_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * Functions for accessing PCI configuration space with MMCONFIG accesses
28 */
Andi Kleend6ece542005-12-12 22:17:11 -080029static u32 get_base_addr(unsigned int seg, int bus, unsigned devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Bjorn Helgaasf6e1d8c2009-11-13 17:35:04 -070031 struct pci_mmcfg_region *cfg = pci_mmconfig_lookup(seg, bus);
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070032
Bjorn Helgaasf6e1d8c2009-11-13 17:35:04 -070033 if (cfg)
34 return cfg->address;
Andi Kleen31030392006-01-27 02:03:50 +010035 return 0;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070036}
37
Andrew Mortonbe5b7a82006-09-30 23:27:10 -070038/*
39 * This is always called under pci_config_lock
40 */
41static void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070042{
Bjorn Helgaasdf5eb1d2009-11-13 17:34:08 -070043 u32 dev_base = base | PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12);
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090044 int cpu = smp_processor_id();
45 if (dev_base != mmcfg_last_accessed_device ||
46 cpu != mmcfg_last_accessed_cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 mmcfg_last_accessed_device = dev_base;
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090048 mmcfg_last_accessed_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
50 }
51}
52
53static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
54 unsigned int devfn, int reg, int len, u32 *value)
55{
56 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -080057 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Andi Kleenecc16ba2006-04-11 12:54:48 +020059 if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050060err: *value = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return -EINVAL;
Andi Kleen49c93e82006-04-07 19:50:15 +020062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jiang Liu376f70a2012-06-22 14:55:12 +080064 rcu_read_lock();
Andi Kleend6ece542005-12-12 22:17:11 -080065 base = get_base_addr(seg, bus, devfn);
Jiang Liu376f70a2012-06-22 14:55:12 +080066 if (!base) {
67 rcu_read_unlock();
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050068 goto err;
Jiang Liu376f70a2012-06-22 14:55:12 +080069 }
Andi Kleen928cf8c2005-12-12 22:17:10 -080070
Thomas Gleixnerd19f61f2010-02-17 14:35:25 +000071 raw_spin_lock_irqsave(&pci_config_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Andi Kleen928cf8c2005-12-12 22:17:10 -080073 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 switch (len) {
76 case 1:
dean gaudet3320ad92007-08-10 22:30:59 +020077 *value = mmio_config_readb(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
79 case 2:
dean gaudet3320ad92007-08-10 22:30:59 +020080 *value = mmio_config_readw(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 break;
82 case 4:
dean gaudet3320ad92007-08-10 22:30:59 +020083 *value = mmio_config_readl(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 break;
85 }
Thomas Gleixnerd19f61f2010-02-17 14:35:25 +000086 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
Jiang Liu376f70a2012-06-22 14:55:12 +080087 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 return 0;
90}
91
92static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
93 unsigned int devfn, int reg, int len, u32 value)
94{
95 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -080096 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +030098 if ((bus > 255) || (devfn > 255) || (reg > 4095))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -EINVAL;
100
Jiang Liu376f70a2012-06-22 14:55:12 +0800101 rcu_read_lock();
Andi Kleend6ece542005-12-12 22:17:11 -0800102 base = get_base_addr(seg, bus, devfn);
Jiang Liu376f70a2012-06-22 14:55:12 +0800103 if (!base) {
104 rcu_read_unlock();
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -0500105 return -EINVAL;
Jiang Liu376f70a2012-06-22 14:55:12 +0800106 }
Andi Kleen928cf8c2005-12-12 22:17:10 -0800107
Thomas Gleixnerd19f61f2010-02-17 14:35:25 +0000108 raw_spin_lock_irqsave(&pci_config_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Andi Kleen928cf8c2005-12-12 22:17:10 -0800110 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 switch (len) {
113 case 1:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700114 mmio_config_writeb(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 break;
116 case 2:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700117 mmio_config_writew(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 break;
119 case 4:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700120 mmio_config_writel(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 break;
122 }
Thomas Gleixnerd19f61f2010-02-17 14:35:25 +0000123 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
Jiang Liu376f70a2012-06-22 14:55:12 +0800124 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 return 0;
127}
128
Jiang Liuc0fa4072012-06-22 14:55:17 +0800129const struct pci_raw_ops pci_mmcfg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .read = pci_mmcfg_read,
131 .write = pci_mmcfg_write,
132};
133
Olivier Galibertb7867392007-02-13 13:26:20 +0100134int __init pci_mmcfg_arch_init(void)
Andi Kleend6ece542005-12-12 22:17:11 -0800135{
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500136 printk(KERN_INFO "PCI: Using MMCONFIG for extended config space\n");
137 raw_pci_ext_ops = &pci_mmcfg;
Olivier Galibertb7867392007-02-13 13:26:20 +0100138 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
Yinghai Lu0b64ad72008-02-15 01:28:41 -0800140
141void __init pci_mmcfg_arch_free(void)
142{
143}
Jiang Liu9cf01052012-06-22 14:55:13 +0800144
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -0800145int pci_mmcfg_arch_map(struct pci_mmcfg_region *cfg)
Jiang Liu9cf01052012-06-22 14:55:13 +0800146{
147 return 0;
148}
149
150void pci_mmcfg_arch_unmap(struct pci_mmcfg_region *cfg)
151{
152 unsigned long flags;
153
154 /* Invalidate the cached mmcfg map entry. */
155 raw_spin_lock_irqsave(&pci_config_lock, flags);
156 mmcfg_last_accessed_device = 0;
157 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
158}