1 /*
2 * AMD Family 10h mmconfig enablement (taken from Linux 2.6.36)
3 */
4
5 #include <xen/lib.h>
6 #include <xen/acpi.h>
7 #include <xen/pci.h>
8 #include <xen/pci_regs.h>
9 #include <xen/pci_ids.h>
10 #include <xen/init.h>
11 #include <xen/dmi.h>
12 #include <asm/amd.h>
13 #include <asm/e820.h>
14 #include <asm/msr.h>
15 #include <asm/processor.h>
16
17 #include "mmconfig.h"
18
19 struct pci_hostbridge_probe {
20 u32 bus;
21 u32 slot;
22 u32 vendor;
23 u32 device;
24 };
25
26 static u64 fam10h_pci_mmconf_base;
27
28 static struct pci_hostbridge_probe pci_probes[] = {
29 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
30 { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
31 };
32
33 #define UNIT (1ULL << FAM10H_MMIO_CONF_BASE_SHIFT)
34 #define MASK (~(UNIT - 1))
35 #define SIZE (UNIT << 8)
36 /* need to avoid (0xfd<<32) and (0xfe<<32), ht used space */
37 #define FAM10H_PCI_MMCONF_BASE (0xfcULL<<32)
38 #define BASE_VALID(b) ((b) + SIZE <= (0xfdULL<<32) || (b) >= (1ULL<<40))
get_fam10h_pci_mmconf_base(void)39 static void __init get_fam10h_pci_mmconf_base(void)
40 {
41 unsigned int i, j, bus, slot, hi_mmio_num;
42 u32 address;
43 u64 val, tom2, start, end;
44 struct range {
45 u64 start, end;
46 } range[8];
47
48 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
49 u32 id;
50 u16 device;
51 u16 vendor;
52
53 bus = pci_probes[i].bus;
54 slot = pci_probes[i].slot;
55 id = pci_conf_read32(PCI_SBDF(0, bus, slot, 0), PCI_VENDOR_ID);
56
57 vendor = id & 0xffff;
58 device = (id>>16) & 0xffff;
59 if (pci_probes[i].vendor == vendor &&
60 pci_probes[i].device == device)
61 break;
62 }
63
64 if (i >= ARRAY_SIZE(pci_probes))
65 return;
66
67 /* SYS_CFG */
68 address = MSR_K8_SYSCFG;
69 rdmsrl(address, val);
70
71 /* TOP_MEM2 is not enabled? */
72 if (!(val & (1<<21))) {
73 tom2 = 1ULL << 32;
74 } else {
75 /* TOP_MEM2 */
76 address = MSR_K8_TOP_MEM2;
77 rdmsrl(address, val);
78 tom2 = max(val & 0xffffff800000ULL, 1ULL << 32);
79 }
80
81 /*
82 * need to check if the range is in the high mmio range that is
83 * above 4G
84 */
85 for (hi_mmio_num = i = 0; i < 8; i++) {
86 val = pci_conf_read32(PCI_SBDF(0, bus, slot, 1),
87 0x80 + (i << 3));
88 if (!(val & 3))
89 continue;
90
91 start = (val & 0xffffff00) << 8; /* 39:16 on 31:8*/
92 val = pci_conf_read32(PCI_SBDF(0, bus, slot, 1),
93 0x84 + (i << 3));
94 end = ((val & 0xffffff00) << 8) | 0xffff; /* 39:16 on 31:8*/
95
96 if (end < tom2)
97 continue;
98
99 for (j = hi_mmio_num; j; --j) {
100 if (range[j - 1].start < start)
101 break;
102 range[j] = range[j - 1];
103 }
104 range[j].start = start;
105 range[j].end = end;
106 hi_mmio_num++;
107 }
108
109 start = FAM10H_PCI_MMCONF_BASE;
110 if (start <= tom2)
111 start = (tom2 + 2 * UNIT - 1) & MASK;
112
113 if (!hi_mmio_num)
114 goto out;
115
116 if (range[hi_mmio_num - 1].end < start)
117 goto out;
118 if (range[0].start > start + SIZE)
119 goto out;
120
121 /* need to find one window */
122 start = (range[0].start & MASK) - UNIT;
123 if (start > tom2 && BASE_VALID(start))
124 goto out;
125 start = (range[hi_mmio_num - 1].end + UNIT) & MASK;
126 if (BASE_VALID(start))
127 goto out;
128 /* need to find window between ranges */
129 for (i = 1; i < hi_mmio_num; i++) {
130 start = (range[i - 1].end + UNIT) & MASK;
131 end = range[i].start & MASK;
132 if (end >= start + SIZE && BASE_VALID(start))
133 goto out;
134 }
135 return;
136
137 out:
138 if (e820_add_range(&e820, start, start + SIZE, E820_RESERVED))
139 fam10h_pci_mmconf_base = start;
140 }
141
fam10h_check_enable_mmcfg(void)142 void fam10h_check_enable_mmcfg(void)
143 {
144 u64 val;
145 bool_t print = opt_cpu_info;
146
147 if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
148 return;
149
150 rdmsrl(MSR_FAM10H_MMIO_CONF_BASE, val);
151
152 /* try to make sure that AP's setting is identical to BSP setting */
153 if (val & FAM10H_MMIO_CONF_ENABLE) {
154 u64 base = val & MASK;
155
156 if (!fam10h_pci_mmconf_base) {
157 fam10h_pci_mmconf_base = base;
158 return;
159 }
160 if (fam10h_pci_mmconf_base == base)
161 return;
162 }
163
164 /*
165 * if it is not enabled, try to enable it and assume only one segment
166 * with 256 buses
167 */
168 /* only try to get setting from BSP */
169 if (!fam10h_pci_mmconf_base) {
170 get_fam10h_pci_mmconf_base();
171 print = 1;
172 }
173 if (!fam10h_pci_mmconf_base) {
174 pci_probe &= ~PCI_CHECK_ENABLE_AMD_MMCONF;
175 return;
176 }
177
178 if (print)
179 printk(KERN_INFO "Enable MMCONFIG on AMD Fam10h at %"PRIx64"\n",
180 fam10h_pci_mmconf_base);
181 val &= ~((FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT) |
182 (FAM10H_MMIO_CONF_BUSRANGE_MASK<<FAM10H_MMIO_CONF_BUSRANGE_SHIFT));
183 val |= fam10h_pci_mmconf_base | (8 << FAM10H_MMIO_CONF_BUSRANGE_SHIFT) |
184 FAM10H_MMIO_CONF_ENABLE;
185 wrmsrl(MSR_FAM10H_MMIO_CONF_BASE, val);
186 }
187
188 static const struct dmi_system_id __initconstrel mmconf_dmi_table[] = {
189 {
190 .ident = "Sun Microsystems Machine",
191 .matches = {
192 DMI_MATCH(DMI_SYS_VENDOR, "Sun Microsystems"),
193 },
194 },
195 {}
196 };
197
check_enable_amd_mmconf_dmi(void)198 void __init check_enable_amd_mmconf_dmi(void)
199 {
200 if (dmi_check_system(mmconf_dmi_table))
201 pci_probe |= PCI_CHECK_ENABLE_AMD_MMCONF;
202 }
203