blob: c04523e096499abbdf7550fee444e4c00f236a8f [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 Helgaasd215a9c2009-11-13 17:34:13 -070030 struct pci_mmcfg_region *cfg;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070031
Bjorn Helgaasff097dd2009-11-13 17:34:49 -070032 list_for_each_entry(cfg, &pci_mmcfg_list, list)
Bjorn Helgaasd7e6b662009-11-13 17:34:18 -070033 if (cfg->segment == seg &&
34 (cfg->start_bus <= bus) &&
35 (cfg->end_bus >= bus))
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +030036 return cfg->address;
Andi Kleen31030392006-01-27 02:03:50 +010037
Andi Kleen31030392006-01-27 02:03:50 +010038 /* Fall back to type 0 */
39 return 0;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070040}
41
Andrew Mortonbe5b7a82006-09-30 23:27:10 -070042/*
43 * This is always called under pci_config_lock
44 */
45static void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070046{
Bjorn Helgaasdf5eb1d2009-11-13 17:34:08 -070047 u32 dev_base = base | PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12);
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090048 int cpu = smp_processor_id();
49 if (dev_base != mmcfg_last_accessed_device ||
50 cpu != mmcfg_last_accessed_cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 mmcfg_last_accessed_device = dev_base;
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090052 mmcfg_last_accessed_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
54 }
55}
56
57static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
58 unsigned int devfn, int reg, int len, u32 *value)
59{
60 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -080061 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Andi Kleenecc16ba2006-04-11 12:54:48 +020063 if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050064err: *value = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return -EINVAL;
Andi Kleen49c93e82006-04-07 19:50:15 +020066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Andi Kleend6ece542005-12-12 22:17:11 -080068 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -080069 if (!base)
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050070 goto err;
Andi Kleen928cf8c2005-12-12 22:17:10 -080071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 spin_lock_irqsave(&pci_config_lock, flags);
73
Andi Kleen928cf8c2005-12-12 22:17:10 -080074 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 switch (len) {
77 case 1:
dean gaudet3320ad92007-08-10 22:30:59 +020078 *value = mmio_config_readb(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 break;
80 case 2:
dean gaudet3320ad92007-08-10 22:30:59 +020081 *value = mmio_config_readw(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
83 case 4:
dean gaudet3320ad92007-08-10 22:30:59 +020084 *value = mmio_config_readl(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 break;
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 spin_unlock_irqrestore(&pci_config_lock, flags);
88
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
Andi Kleend6ece542005-12-12 22:17:11 -0800101 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -0800102 if (!base)
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -0500103 return -EINVAL;
Andi Kleen928cf8c2005-12-12 22:17:10 -0800104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 spin_lock_irqsave(&pci_config_lock, flags);
106
Andi Kleen928cf8c2005-12-12 22:17:10 -0800107 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 switch (len) {
110 case 1:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700111 mmio_config_writeb(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 break;
113 case 2:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700114 mmio_config_writew(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 break;
116 case 4:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700117 mmio_config_writel(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 break;
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 spin_unlock_irqrestore(&pci_config_lock, flags);
121
122 return 0;
123}
124
125static struct pci_raw_ops pci_mmcfg = {
126 .read = pci_mmcfg_read,
127 .write = pci_mmcfg_write,
128};
129
Olivier Galibertb7867392007-02-13 13:26:20 +0100130int __init pci_mmcfg_arch_init(void)
Andi Kleend6ece542005-12-12 22:17:11 -0800131{
Matthew Wilcoxb6ce0682008-02-10 09:45:28 -0500132 printk(KERN_INFO "PCI: Using MMCONFIG for extended config space\n");
133 raw_pci_ext_ops = &pci_mmcfg;
Olivier Galibertb7867392007-02-13 13:26:20 +0100134 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
Yinghai Lu0b64ad72008-02-15 01:28:41 -0800136
137void __init pci_mmcfg_arch_free(void)
138{
139}