blob: 8b2d561046a3bba83ef3953171fb1abdd5a195b5 [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>
Greg Kroah-Hartman54549392005-06-23 17:35:56 -070014#include <linux/acpi.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{
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +030030 struct acpi_mcfg_allocation *cfg;
OGAWA Hirofumi429d5122007-02-13 13:26:20 +010031 int cfg_num;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070032
OGAWA Hirofumi429d5122007-02-13 13:26:20 +010033 for (cfg_num = 0; cfg_num < pci_mmcfg_config_num; cfg_num++) {
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070034 cfg = &pci_mmcfg_config[cfg_num];
OGAWA Hirofumi429d5122007-02-13 13:26:20 +010035 if (cfg->pci_segment == seg &&
36 (cfg->start_bus_number <= bus) &&
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070037 (cfg->end_bus_number >= bus))
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +030038 return cfg->address;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070039 }
Andi Kleen31030392006-01-27 02:03:50 +010040
Andi Kleen31030392006-01-27 02:03:50 +010041 /* Fall back to type 0 */
42 return 0;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070043}
44
Andrew Mortonbe5b7a82006-09-30 23:27:10 -070045/*
46 * This is always called under pci_config_lock
47 */
48static void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070049{
Andi Kleen928cf8c2005-12-12 22:17:10 -080050 u32 dev_base = base | (bus << 20) | (devfn << 12);
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090051 int cpu = smp_processor_id();
52 if (dev_base != mmcfg_last_accessed_device ||
53 cpu != mmcfg_last_accessed_cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 mmcfg_last_accessed_device = dev_base;
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090055 mmcfg_last_accessed_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
57 }
58}
59
60static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
61 unsigned int devfn, int reg, int len, u32 *value)
62{
63 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -080064 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Andi Kleenecc16ba2006-04-11 12:54:48 +020066 if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050067err: *value = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return -EINVAL;
Andi Kleen49c93e82006-04-07 19:50:15 +020069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Andi Kleend6ece542005-12-12 22:17:11 -080071 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -080072 if (!base)
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050073 goto err;
Andi Kleen928cf8c2005-12-12 22:17:10 -080074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 spin_lock_irqsave(&pci_config_lock, flags);
76
Andi Kleen928cf8c2005-12-12 22:17:10 -080077 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 switch (len) {
80 case 1:
dean gaudet3320ad92007-08-10 22:30:59 +020081 *value = mmio_config_readb(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
83 case 2:
dean gaudet3320ad92007-08-10 22:30:59 +020084 *value = mmio_config_readw(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 break;
86 case 4:
dean gaudet3320ad92007-08-10 22:30:59 +020087 *value = mmio_config_readl(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 break;
89 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 spin_unlock_irqrestore(&pci_config_lock, flags);
91
92 return 0;
93}
94
95static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
96 unsigned int devfn, int reg, int len, u32 value)
97{
98 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -080099 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300101 if ((bus > 255) || (devfn > 255) || (reg > 4095))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return -EINVAL;
103
Andi Kleend6ece542005-12-12 22:17:11 -0800104 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -0800105 if (!base)
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -0500106 return -EINVAL;
Andi Kleen928cf8c2005-12-12 22:17:10 -0800107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 spin_lock_irqsave(&pci_config_lock, flags);
109
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 spin_unlock_irqrestore(&pci_config_lock, flags);
124
125 return 0;
126}
127
128static struct pci_raw_ops pci_mmcfg = {
129 .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}