blob: 90d5fd476ed4fe3a093b514f095ca3d237023f54 [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>
Arjan van de Ven946f2ee2006-04-07 19:49:30 +020014#include <asm/e820.h>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +053015#include <asm/pci_x86.h>
Feng Tang5f0db7a2009-08-14 15:37:50 -040016#include <acpi/acpi.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
Andi Kleend6ece542005-12-12 22:17:11 -080063 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -080064 if (!base)
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050065 goto err;
Andi Kleen928cf8c2005-12-12 22:17:10 -080066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 spin_lock_irqsave(&pci_config_lock, flags);
68
Andi Kleen928cf8c2005-12-12 22:17:10 -080069 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 switch (len) {
72 case 1:
dean gaudet3320ad92007-08-10 22:30:59 +020073 *value = mmio_config_readb(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 break;
75 case 2:
dean gaudet3320ad92007-08-10 22:30:59 +020076 *value = mmio_config_readw(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 break;
78 case 4:
dean gaudet3320ad92007-08-10 22:30:59 +020079 *value = mmio_config_readl(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 break;
81 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 spin_unlock_irqrestore(&pci_config_lock, flags);
83
84 return 0;
85}
86
87static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
88 unsigned int devfn, int reg, int len, u32 value)
89{
90 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -080091 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +030093 if ((bus > 255) || (devfn > 255) || (reg > 4095))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return -EINVAL;
95
Andi Kleend6ece542005-12-12 22:17:11 -080096 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -080097 if (!base)
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050098 return -EINVAL;
Andi Kleen928cf8c2005-12-12 22:17:10 -080099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 spin_lock_irqsave(&pci_config_lock, flags);
101
Andi Kleen928cf8c2005-12-12 22:17:10 -0800102 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 switch (len) {
105 case 1:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700106 mmio_config_writeb(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 break;
108 case 2:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700109 mmio_config_writew(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 break;
111 case 4:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700112 mmio_config_writel(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 break;
114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 spin_unlock_irqrestore(&pci_config_lock, flags);
116
117 return 0;
118}
119
120static struct pci_raw_ops pci_mmcfg = {
121 .read = pci_mmcfg_read,
122 .write = pci_mmcfg_write,
123};
124
Olivier Galibertb7867392007-02-13 13:26:20 +0100125int __init pci_mmcfg_arch_init(void)
Andi Kleend6ece542005-12-12 22:17:11 -0800126{
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500127 printk(KERN_INFO "PCI: Using MMCONFIG for extended config space\n");
128 raw_pci_ext_ops = &pci_mmcfg;
Olivier Galibertb7867392007-02-13 13:26:20 +0100129 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
Yinghai Lu0b64ad72008-02-15 01:28:41 -0800131
132void __init pci_mmcfg_arch_free(void)
133{
134}