ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / x86_64 / pci / mmconfig.c
1 /*
2  * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
3  * 
4  * This is an 64bit optimized version that always keeps the full mmconfig
5  * space mapped. This allows lockless config space operation.
6  */
7
8 #include <linux/pci.h>
9 #include <linux/init.h>
10 #include "pci.h"
11
12 #define MMCONFIG_APER_SIZE (256*1024*1024)
13
14 /* The physical address of the MMCONFIG aperture.  Set from ACPI tables. */
15 u32 pci_mmcfg_base_addr;
16
17 /* Static virtual mapping of the MMCONFIG aperture */
18 char *pci_mmcfg_virt;
19
20 static inline char *pci_dev_base(int bus, int devfn)
21 {
22         return pci_mmcfg_virt + ((bus << 20) | (devfn << 12));
23 }
24
25 static int pci_mmcfg_read(int seg, int bus, int devfn, int reg, int len, u32 *value)
26 {
27         char *addr = pci_dev_base(bus, devfn); 
28
29         if (unlikely(!value || (bus > 255) || (devfn > 255) || (reg > 4095)))
30                 return -EINVAL;
31
32         switch (len) {
33         case 1:
34                 *value = readb(addr + reg);
35                 break;
36         case 2:
37                 *value = readw(addr + reg);
38                 break;
39         case 4:
40                 *value = readl(addr + reg);
41                 break;
42         }
43
44         return 0;
45 }
46
47 static int pci_mmcfg_write(int seg, int bus, int devfn, int reg, int len, u32 value)
48 {
49         char *addr = pci_dev_base(bus,devfn);
50
51         if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
52                 return -EINVAL;
53
54         switch (len) {
55         case 1:
56                 writeb(value, addr + reg);
57                 break;
58         case 2:
59                 writew(value, addr + reg);
60                 break;
61         case 4:
62                 writel(value, addr + reg);
63                 break;
64         }
65
66         /* Dummy read to flush PCI write */
67         readl(addr);
68
69         return 0;
70 }
71
72 static struct pci_raw_ops pci_mmcfg = {
73         .read =         pci_mmcfg_read,
74         .write =        pci_mmcfg_write,
75 };
76
77 static int __init pci_mmcfg_init(void)
78 {
79         if ((pci_probe & PCI_PROBE_MMCONF) == 0)
80                 return 0;
81         if (!pci_mmcfg_base_addr)
82                 return 0;
83
84         /* RED-PEN i386 doesn't do _nocache right now */
85         pci_mmcfg_virt = ioremap_nocache(pci_mmcfg_base_addr, MMCONFIG_APER_SIZE);
86         if (!pci_mmcfg_virt) { 
87                 printk("PCI: Cannot map mmconfig aperture\n");
88                 return 0;
89         }       
90
91         printk(KERN_INFO "PCI: Using MMCONFIG at %x\n", pci_mmcfg_base_addr);
92         raw_pci_ops = &pci_mmcfg;
93         pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
94
95         return 0;
96 }
97
98 arch_initcall(pci_mmcfg_init);