blob: 43984bc1665a917303ae574b27345138cc9560b6 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Andi Kleen8c30b1a742006-04-07 19:50:12 +020018/* Assume systems with more busses have correct MCFG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG))
20
21/* The base address of the last MMCONFIG device accessed */
22static u32 mmcfg_last_accessed_device;
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090023static int mmcfg_last_accessed_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
26 * Functions for accessing PCI configuration space with MMCONFIG accesses
27 */
Andi Kleend6ece542005-12-12 22:17:11 -080028static u32 get_base_addr(unsigned int seg, int bus, unsigned devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Bjorn Helgaasf6e1d8c2009-11-13 17:35:04 -070030 struct pci_mmcfg_region *cfg = pci_mmconfig_lookup(seg, bus);
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070031
Bjorn Helgaasf6e1d8c2009-11-13 17:35:04 -070032 if (cfg)
33 return cfg->address;
Andi Kleen31030392006-01-27 02:03:50 +010034 return 0;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070035}
36
Andrew Mortonbe5b7a82006-09-30 23:27:10 -070037/*
38 * This is always called under pci_config_lock
39 */
40static void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070041{
Bjorn Helgaasdf5eb1d2009-11-13 17:34:08 -070042 u32 dev_base = base | PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12);
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090043 int cpu = smp_processor_id();
44 if (dev_base != mmcfg_last_accessed_device ||
45 cpu != mmcfg_last_accessed_cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 mmcfg_last_accessed_device = dev_base;
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090047 mmcfg_last_accessed_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
49 }
50}
51
52static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
53 unsigned int devfn, int reg, int len, u32 *value)
54{
55 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -080056 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Andi Kleenecc16ba2006-04-11 12:54:48 +020058 if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050059err: *value = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return -EINVAL;
Andi Kleen49c93e82006-04-07 19:50:15 +020061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jiang Liu376f70a2012-06-22 14:55:12 +080063 rcu_read_lock();
Andi Kleend6ece542005-12-12 22:17:11 -080064 base = get_base_addr(seg, bus, devfn);
Jiang Liu376f70a2012-06-22 14:55:12 +080065 if (!base) {
66 rcu_read_unlock();
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050067 goto err;
Jiang Liu376f70a2012-06-22 14:55:12 +080068 }
Andi Kleen928cf8c2005-12-12 22:17:10 -080069
Thomas Gleixnerd19f61f2010-02-17 14:35:25 +000070 raw_spin_lock_irqsave(&pci_config_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Andi Kleen928cf8c2005-12-12 22:17:10 -080072 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 switch (len) {
75 case 1:
dean gaudet3320ad92007-08-10 22:30:59 +020076 *value = mmio_config_readb(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 break;
78 case 2:
dean gaudet3320ad92007-08-10 22:30:59 +020079 *value = mmio_config_readw(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 break;
81 case 4:
dean gaudet3320ad92007-08-10 22:30:59 +020082 *value = mmio_config_readl(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 break;
84 }
Thomas Gleixnerd19f61f2010-02-17 14:35:25 +000085 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
Jiang Liu376f70a2012-06-22 14:55:12 +080086 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 return 0;
89}
90
91static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
92 unsigned int devfn, int reg, int len, u32 value)
93{
94 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -080095 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +030097 if ((bus > 255) || (devfn > 255) || (reg > 4095))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -EINVAL;
99
Jiang Liu376f70a2012-06-22 14:55:12 +0800100 rcu_read_lock();
Andi Kleend6ece542005-12-12 22:17:11 -0800101 base = get_base_addr(seg, bus, devfn);
Jiang Liu376f70a2012-06-22 14:55:12 +0800102 if (!base) {
103 rcu_read_unlock();
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -0500104 return -EINVAL;
Jiang Liu376f70a2012-06-22 14:55:12 +0800105 }
Andi Kleen928cf8c2005-12-12 22:17:10 -0800106
Thomas Gleixnerd19f61f2010-02-17 14:35:25 +0000107 raw_spin_lock_irqsave(&pci_config_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Andi Kleen928cf8c2005-12-12 22:17:10 -0800109 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 switch (len) {
112 case 1:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700113 mmio_config_writeb(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 break;
115 case 2:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700116 mmio_config_writew(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 break;
118 case 4:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700119 mmio_config_writel(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 break;
121 }
Thomas Gleixnerd19f61f2010-02-17 14:35:25 +0000122 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
Jiang Liu376f70a2012-06-22 14:55:12 +0800123 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 return 0;
126}
127
Jiang Liuc0fa4072012-06-22 14:55:17 +0800128const struct pci_raw_ops pci_mmcfg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .read = pci_mmcfg_read,
130 .write = pci_mmcfg_write,
131};
132
Olivier Galibertb7867392007-02-13 13:26:20 +0100133int __init pci_mmcfg_arch_init(void)
Andi Kleend6ece542005-12-12 22:17:11 -0800134{
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500135 printk(KERN_INFO "PCI: Using MMCONFIG for extended config space\n");
136 raw_pci_ext_ops = &pci_mmcfg;
Olivier Galibertb7867392007-02-13 13:26:20 +0100137 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
Yinghai Lu0b64ad72008-02-15 01:28:41 -0800139
140void __init pci_mmcfg_arch_free(void)
141{
142}
Jiang Liu9cf01052012-06-22 14:55:13 +0800143
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -0800144int pci_mmcfg_arch_map(struct pci_mmcfg_region *cfg)
Jiang Liu9cf01052012-06-22 14:55:13 +0800145{
146 return 0;
147}
148
149void pci_mmcfg_arch_unmap(struct pci_mmcfg_region *cfg)
150{
151 unsigned long flags;
152
153 /* Invalidate the cached mmcfg map entry. */
154 raw_spin_lock_irqsave(&pci_config_lock, flags);
155 mmcfg_last_accessed_device = 0;
156 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
157}