This commit was manufactured by cvs2svn to create tag
[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         return 0;
67 }
68
69 static struct pci_raw_ops pci_mmcfg = {
70         .read =         pci_mmcfg_read,
71         .write =        pci_mmcfg_write,
72 };
73
74 static int __init pci_mmcfg_init(void)
75 {
76         if ((pci_probe & PCI_PROBE_MMCONF) == 0)
77                 return 0;
78         if (!pci_mmcfg_base_addr)
79                 return 0;
80
81         /* RED-PEN i386 doesn't do _nocache right now */
82         pci_mmcfg_virt = ioremap_nocache(pci_mmcfg_base_addr, MMCONFIG_APER_SIZE);
83         if (!pci_mmcfg_virt) { 
84                 printk("PCI: Cannot map mmconfig aperture\n");
85                 return 0;
86         }       
87
88         printk(KERN_INFO "PCI: Using MMCONFIG at %x\n", pci_mmcfg_base_addr);
89         raw_pci_ops = &pci_mmcfg;
90         pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
91
92         return 0;
93 }
94
95 arch_initcall(pci_mmcfg_init);