mmconfig.c 3.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
3
 *
L
Linus Torvalds 已提交
4 5 6 7 8 9
 * This is an 64bit optimized version that always keeps the full mmconfig
 * space mapped. This allows lockless config space operation.
 */

#include <linux/pci.h>
#include <linux/init.h>
10
#include <linux/acpi.h>
11
#include <linux/bitmap.h>
12 13
#include <asm/e820.h>

L
Linus Torvalds 已提交
14 15
#include "pci.h"

16 17 18 19
/* aperture is up to 256MB but BIOS may reserve less */
#define MMCONFIG_APER_MIN	(2 * 1024*1024)
#define MMCONFIG_APER_MAX	(256 * 1024*1024)

20 21
/* Verify the first 16 busses. We assume that systems with more busses
   get MCFG right. */
22
#define PCI_MMCFG_MAX_CHECK_BUS 16
23

L
Linus Torvalds 已提交
24
/* Static virtual mapping of the MMCONFIG aperture */
25
struct mmcfg_virt {
26
	struct acpi_mcfg_allocation *cfg;
27
	char __iomem *virt;
28 29
};
static struct mmcfg_virt *pci_mmcfg_virt;
L
Linus Torvalds 已提交
30

31
static char __iomem *get_virt(unsigned int seg, unsigned bus)
L
Linus Torvalds 已提交
32
{
33
	int cfg_num = -1;
34
	struct acpi_mcfg_allocation *cfg;
35 36 37

	while (1) {
		++cfg_num;
38 39
		if (cfg_num >= pci_mmcfg_config_num)
			break;
40
		cfg = pci_mmcfg_virt[cfg_num].cfg;
41
		if (cfg->pci_segment != seg)
42 43 44 45 46
			continue;
		if ((cfg->start_bus_number <= bus) &&
		    (cfg->end_bus_number >= bus))
			return pci_mmcfg_virt[cfg_num].virt;
	}
47 48 49 50 51 52

	/* Handle more broken MCFG tables on Asus etc.
	   They only contain a single entry for bus 0-0. Assume
 	   this applies to all busses. */
	cfg = &pci_mmcfg_config[0];
	if (pci_mmcfg_config_num == 1 &&
53
		cfg->pci_segment == 0 &&
54
		(cfg->start_bus_number | cfg->end_bus_number) == 0)
55
		return pci_mmcfg_virt[0].virt;
56 57

	/* Fall back to type 0 */
58
	return NULL;
59 60
}

61
static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
62
{
63
	char __iomem *addr;
64 65
	if (seg == 0 && bus < PCI_MMCFG_MAX_CHECK_BUS &&
		test_bit(32*bus + PCI_SLOT(devfn), pci_mmcfg_fallback_slots))
66 67
		return NULL;
	addr = get_virt(seg, bus);
68 69 70
	if (!addr)
		return NULL;
 	return addr + ((bus << 20) | (devfn << 12));
L
Linus Torvalds 已提交
71 72 73 74 75
}

static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
			  unsigned int devfn, int reg, int len, u32 *value)
{
76
	char __iomem *addr;
L
Linus Torvalds 已提交
77

78
	/* Why do we have this when nobody checks it. How about a BUG()!? -AK */
79
	if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
80
		*value = -1;
L
Linus Torvalds 已提交
81
		return -EINVAL;
82
	}
L
Linus Torvalds 已提交
83

84 85 86 87
	addr = pci_dev_base(seg, bus, devfn);
	if (!addr)
		return pci_conf1_read(seg,bus,devfn,reg,len,value);

L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	switch (len) {
	case 1:
		*value = readb(addr + reg);
		break;
	case 2:
		*value = readw(addr + reg);
		break;
	case 4:
		*value = readl(addr + reg);
		break;
	}

	return 0;
}

static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
			   unsigned int devfn, int reg, int len, u32 value)
{
106
	char __iomem *addr;
L
Linus Torvalds 已提交
107

108
	/* Why do we have this when nobody checks it. How about a BUG()!? -AK */
L
Linus Torvalds 已提交
109 110 111
	if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
		return -EINVAL;

112 113 114 115
	addr = pci_dev_base(seg, bus, devfn);
	if (!addr)
		return pci_conf1_write(seg,bus,devfn,reg,len,value);

L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	switch (len) {
	case 1:
		writeb(value, addr + reg);
		break;
	case 2:
		writew(value, addr + reg);
		break;
	case 4:
		writel(value, addr + reg);
		break;
	}

	return 0;
}

static struct pci_raw_ops pci_mmcfg = {
	.read =		pci_mmcfg_read,
	.write =	pci_mmcfg_write,
};

136
int __init pci_mmcfg_arch_init(void)
L
Linus Torvalds 已提交
137
{
138
	int i;
139 140
	pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) *
				 pci_mmcfg_config_num, GFP_KERNEL);
141
	if (pci_mmcfg_virt == NULL) {
142
		printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
143
		return 0;
144
	}
145

146 147
	for (i = 0; i < pci_mmcfg_config_num; ++i) {
		pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
148
		pci_mmcfg_virt[i].virt = ioremap_nocache(pci_mmcfg_config[i].address,
149
							 MMCONFIG_APER_MAX);
150
		if (!pci_mmcfg_virt[i].virt) {
151 152
			printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
					"segment %d\n",
153
				pci_mmcfg_config[i].pci_segment);
154
			return 0;
155
		}
156 157
		printk(KERN_INFO "PCI: Using MMCONFIG at %Lx\n",
				pci_mmcfg_config[i].address);
158
	}
L
Linus Torvalds 已提交
159
	raw_pci_ops = &pci_mmcfg;
160
	return 1;
L
Linus Torvalds 已提交
161
}