blob: 7b75e651343640ffd5147e58c70d5d634e2fba32 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "pci.h"
17
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
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050071 if (reg < 256)
72 return pci_conf1_read(seg,bus,devfn,reg,len,value);
73
Andi Kleend6ece542005-12-12 22:17:11 -080074 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -080075 if (!base)
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -050076 goto err;
Andi Kleen928cf8c2005-12-12 22:17:10 -080077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 spin_lock_irqsave(&pci_config_lock, flags);
79
Andi Kleen928cf8c2005-12-12 22:17:10 -080080 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 switch (len) {
83 case 1:
dean gaudet3320ad92007-08-10 22:30:59 +020084 *value = mmio_config_readb(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 break;
86 case 2:
dean gaudet3320ad92007-08-10 22:30:59 +020087 *value = mmio_config_readw(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 break;
89 case 4:
dean gaudet3320ad92007-08-10 22:30:59 +020090 *value = mmio_config_readl(mmcfg_virt_addr + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 break;
92 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 spin_unlock_irqrestore(&pci_config_lock, flags);
94
95 return 0;
96}
97
98static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
99 unsigned int devfn, int reg, int len, u32 value)
100{
101 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -0800102 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300104 if ((bus > 255) || (devfn > 255) || (reg > 4095))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -EINVAL;
106
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -0500107 if (reg < 256)
108 return pci_conf1_write(seg,bus,devfn,reg,len,value);
109
Andi Kleend6ece542005-12-12 22:17:11 -0800110 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -0800111 if (!base)
Ivan Kokshayskya0ca9902008-01-14 17:31:09 -0500112 return -EINVAL;
Andi Kleen928cf8c2005-12-12 22:17:10 -0800113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 spin_lock_irqsave(&pci_config_lock, flags);
115
Andi Kleen928cf8c2005-12-12 22:17:10 -0800116 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 switch (len) {
119 case 1:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700120 mmio_config_writeb(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 break;
122 case 2:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700123 mmio_config_writew(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 break;
125 case 4:
Linus Torvaldsc1502e22007-08-12 02:23:16 -0700126 mmio_config_writel(mmcfg_virt_addr + reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 break;
128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 spin_unlock_irqrestore(&pci_config_lock, flags);
130
131 return 0;
132}
133
134static struct pci_raw_ops pci_mmcfg = {
135 .read = pci_mmcfg_read,
136 .write = pci_mmcfg_write,
137};
138
Olivier Galibertb7867392007-02-13 13:26:20 +0100139int __init pci_mmcfg_arch_init(void)
Andi Kleend6ece542005-12-12 22:17:11 -0800140{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 printk(KERN_INFO "PCI: Using MMCONFIG\n");
142 raw_pci_ops = &pci_mmcfg;
Olivier Galibertb7867392007-02-13 13:26:20 +0100143 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}