Merge branch 'misc' into release
[linux-2.6/mini2440.git] / arch / x86 / kernel / mmconf-fam10h_64.c
blobefc2f361fe85f0c1280cfce1aeafefa572a5c83d
1 /*
2 * AMD Family 10h mmconfig enablement
3 */
5 #include <linux/types.h>
6 #include <linux/mm.h>
7 #include <linux/string.h>
8 #include <linux/pci.h>
9 #include <linux/dmi.h>
10 #include <asm/pci-direct.h>
11 #include <linux/sort.h>
12 #include <asm/io.h>
13 #include <asm/msr.h>
14 #include <asm/acpi.h>
15 #include <asm/mmconfig.h>
17 #include "../pci/pci.h"
19 struct pci_hostbridge_probe {
20 u32 bus;
21 u32 slot;
22 u32 vendor;
23 u32 device;
26 static u64 __cpuinitdata fam10h_pci_mmconf_base;
27 static int __cpuinitdata fam10h_pci_mmconf_base_status;
29 static struct pci_hostbridge_probe pci_probes[] __cpuinitdata = {
30 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
31 { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
34 struct range {
35 u64 start;
36 u64 end;
39 static int __cpuinit cmp_range(const void *x1, const void *x2)
41 const struct range *r1 = x1;
42 const struct range *r2 = x2;
43 int start1, start2;
45 start1 = r1->start >> 32;
46 start2 = r2->start >> 32;
48 return start1 - start2;
51 /*[47:0] */
52 /* need to avoid (0xfd<<32) and (0xfe<<32), ht used space */
53 #define FAM10H_PCI_MMCONF_BASE (0xfcULL<<32)
54 #define BASE_VALID(b) ((b != (0xfdULL << 32)) && (b != (0xfeULL << 32)))
55 static void __cpuinit get_fam10h_pci_mmconf_base(void)
57 int i;
58 unsigned bus;
59 unsigned slot;
60 int found;
62 u64 val;
63 u32 address;
64 u64 tom2;
65 u64 base = FAM10H_PCI_MMCONF_BASE;
67 int hi_mmio_num;
68 struct range range[8];
70 /* only try to get setting from BSP */
71 /* -1 or 1 */
72 if (fam10h_pci_mmconf_base_status)
73 return;
75 if (!early_pci_allowed())
76 goto fail;
78 found = 0;
79 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
80 u32 id;
81 u16 device;
82 u16 vendor;
84 bus = pci_probes[i].bus;
85 slot = pci_probes[i].slot;
86 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
88 vendor = id & 0xffff;
89 device = (id>>16) & 0xffff;
90 if (pci_probes[i].vendor == vendor &&
91 pci_probes[i].device == device) {
92 found = 1;
93 break;
97 if (!found)
98 goto fail;
100 /* SYS_CFG */
101 address = MSR_K8_SYSCFG;
102 rdmsrl(address, val);
104 /* TOP_MEM2 is not enabled? */
105 if (!(val & (1<<21))) {
106 tom2 = 0;
107 } else {
108 /* TOP_MEM2 */
109 address = MSR_K8_TOP_MEM2;
110 rdmsrl(address, val);
111 tom2 = val & (0xffffULL<<32);
114 if (base <= tom2)
115 base = tom2 + (1ULL<<32);
118 * need to check if the range is in the high mmio range that is
119 * above 4G
121 hi_mmio_num = 0;
122 for (i = 0; i < 8; i++) {
123 u32 reg;
124 u64 start;
125 u64 end;
126 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
127 if (!(reg & 3))
128 continue;
130 start = (((u64)reg) << 8) & (0xffULL << 32); /* 39:16 on 31:8*/
131 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
132 end = (((u64)reg) << 8) & (0xffULL << 32); /* 39:16 on 31:8*/
134 if (!end)
135 continue;
137 range[hi_mmio_num].start = start;
138 range[hi_mmio_num].end = end;
139 hi_mmio_num++;
142 if (!hi_mmio_num)
143 goto out;
145 /* sort the range */
146 sort(range, hi_mmio_num, sizeof(struct range), cmp_range, NULL);
148 if (range[hi_mmio_num - 1].end < base)
149 goto out;
150 if (range[0].start > base)
151 goto out;
153 /* need to find one window */
154 base = range[0].start - (1ULL << 32);
155 if ((base > tom2) && BASE_VALID(base))
156 goto out;
157 base = range[hi_mmio_num - 1].end + (1ULL << 32);
158 if ((base > tom2) && BASE_VALID(base))
159 goto out;
160 /* need to find window between ranges */
161 if (hi_mmio_num > 1)
162 for (i = 0; i < hi_mmio_num - 1; i++) {
163 if (range[i + 1].start > (range[i].end + (1ULL << 32))) {
164 base = range[i].end + (1ULL << 32);
165 if ((base > tom2) && BASE_VALID(base))
166 goto out;
170 fail:
171 fam10h_pci_mmconf_base_status = -1;
172 return;
173 out:
174 fam10h_pci_mmconf_base = base;
175 fam10h_pci_mmconf_base_status = 1;
178 void __cpuinit fam10h_check_enable_mmcfg(void)
180 u64 val;
181 u32 address;
183 if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
184 return;
186 address = MSR_FAM10H_MMIO_CONF_BASE;
187 rdmsrl(address, val);
189 /* try to make sure that AP's setting is identical to BSP setting */
190 if (val & FAM10H_MMIO_CONF_ENABLE) {
191 unsigned busnbits;
192 busnbits = (val >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
193 FAM10H_MMIO_CONF_BUSRANGE_MASK;
195 /* only trust the one handle 256 buses, if acpi=off */
196 if (!acpi_pci_disabled || busnbits >= 8) {
197 u64 base;
198 base = val & (0xffffULL << 32);
199 if (fam10h_pci_mmconf_base_status <= 0) {
200 fam10h_pci_mmconf_base = base;
201 fam10h_pci_mmconf_base_status = 1;
202 return;
203 } else if (fam10h_pci_mmconf_base == base)
204 return;
209 * if it is not enabled, try to enable it and assume only one segment
210 * with 256 buses
212 get_fam10h_pci_mmconf_base();
213 if (fam10h_pci_mmconf_base_status <= 0)
214 return;
216 printk(KERN_INFO "Enable MMCONFIG on AMD Family 10h\n");
217 val &= ~((FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT) |
218 (FAM10H_MMIO_CONF_BUSRANGE_MASK<<FAM10H_MMIO_CONF_BUSRANGE_SHIFT));
219 val |= fam10h_pci_mmconf_base | (8 << FAM10H_MMIO_CONF_BUSRANGE_SHIFT) |
220 FAM10H_MMIO_CONF_ENABLE;
221 wrmsrl(address, val);
224 static int __devinit set_check_enable_amd_mmconf(const struct dmi_system_id *d)
226 pci_probe |= PCI_CHECK_ENABLE_AMD_MMCONF;
227 return 0;
230 static struct dmi_system_id __devinitdata mmconf_dmi_table[] = {
232 .callback = set_check_enable_amd_mmconf,
233 .ident = "Sun Microsystems Machine",
234 .matches = {
235 DMI_MATCH(DMI_SYS_VENDOR, "Sun Microsystems"),
241 void __cpuinit check_enable_amd_mmconf_dmi(void)
243 dmi_check_system(mmconf_dmi_table);