1 #include <linux/init.h>
3 #include <linux/topology.h>
5 #include <linux/range.h>
7 #include <asm/amd_nb.h>
8 #include <asm/pci_x86.h>
10 #include <asm/pci-direct.h>
14 #define AMD_NB_F0_NODE_ID 0x60
15 #define AMD_NB_F0_UNIT_ID 0x64
16 #define AMD_NB_F1_CONFIG_MAP_REG 0xe0
19 #define AMD_NB_F1_CONFIG_MAP_RANGES 4
21 struct amd_hostbridge {
29 * hb_probes[] and early_root_info_init() is in maintenance mode.
30 * It only supports K8, Fam10h, Fam11h, and Fam15h_00h-0fh .
31 * Future processor will rely on information in ACPI.
33 static struct amd_hostbridge hb_probes[] __initdata = {
34 { 0, 0x18, 0x1100 }, /* K8 */
35 { 0, 0x18, 0x1200 }, /* Family10h */
36 { 0xff, 0, 0x1200 }, /* Family10h */
37 { 0, 0x18, 0x1300 }, /* Family11h */
38 { 0, 0x18, 0x1600 }, /* Family15h */
41 static struct pci_root_info __init *find_pci_root_info(int node, int link)
43 struct pci_root_info *info;
45 /* find the position */
46 list_for_each_entry(info, &pci_root_infos, list)
47 if (info->node == node && info->link == link)
54 * early_root_info_init()
55 * called before pcibios_scan_root and pci_scan_bus
56 * fills the mp_bus_to_cpumask array based according
57 * to the LDT Bus Number Registers found in the northbridge.
59 static int __init early_root_info_init(void)
68 struct pci_root_info *info;
72 struct range range[RANGE_NUM];
76 struct resource fam10h_mmconf_res, *fam10h_mmconf;
77 u64 fam10h_mmconf_start;
78 u64 fam10h_mmconf_end;
80 if (!early_pci_allowed())
84 for (i = 0; i < ARRAY_SIZE(hb_probes); i++) {
89 bus = hb_probes[i].bus;
90 slot = hb_probes[i].slot;
91 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
93 device = (id>>16) & 0xffff;
95 if (vendor != PCI_VENDOR_ID_AMD)
98 if (hb_probes[i].device == device) {
108 * We should learn topology and routing information from _PXM and
109 * _CRS methods in the ACPI namespace. We extract node numbers
110 * here to work around BIOSes that don't supply _PXM.
112 for (i = 0; i < AMD_NB_F1_CONFIG_MAP_RANGES; i++) {
115 reg = read_pci_config(bus, slot, 1,
116 AMD_NB_F1_CONFIG_MAP_REG + (i << 2));
118 /* Check if that register is enabled for bus range */
122 min_bus = (reg >> 16) & 0xff;
123 max_bus = (reg >> 24) & 0xff;
124 node = (reg >> 4) & 0x07;
125 link = (reg >> 8) & 0x03;
127 info = alloc_pci_root_info(min_bus, max_bus, node, link);
131 * The following code extracts routing information for use on old
132 * systems where Linux doesn't automatically use host bridge _CRS
133 * methods (or when the user specifies "pci=nocrs").
135 * We only do this through Fam11h, because _CRS should be enough on
138 if (boot_cpu_data.x86 > 0x11)
141 /* get the default node and link for left over res */
142 reg = read_pci_config(bus, slot, 0, AMD_NB_F0_NODE_ID);
143 def_node = (reg >> 8) & 0x07;
144 reg = read_pci_config(bus, slot, 0, AMD_NB_F0_UNIT_ID);
145 def_link = (reg >> 8) & 0x03;
147 memset(range, 0, sizeof(range));
148 add_range(range, RANGE_NUM, 0, 0, 0xffff + 1);
149 /* io port resource */
150 for (i = 0; i < 4; i++) {
151 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
155 start = reg & 0xfff000;
156 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
158 link = (reg >> 4) & 0x03;
159 end = (reg & 0xfff000) | 0xfff;
161 info = find_pci_root_info(node, link);
163 continue; /* not found */
165 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
166 node, link, start, end);
168 /* kernel only handle 16 bit only */
171 update_res(info, start, end, IORESOURCE_IO, 1);
172 subtract_range(range, RANGE_NUM, start, end + 1);
174 /* add left over io port range to def node/link, [0, 0xffff] */
175 /* find the position */
176 info = find_pci_root_info(def_node, def_link);
178 for (i = 0; i < RANGE_NUM; i++) {
182 update_res(info, range[i].start, range[i].end - 1,
187 memset(range, 0, sizeof(range));
188 /* 0xfd00000000-0xffffffffff for HT */
189 end = cap_resource((0xfdULL<<32) - 1);
191 add_range(range, RANGE_NUM, 0, 0, end);
193 /* need to take out [0, TOM) for RAM*/
194 address = MSR_K8_TOP_MEM1;
195 rdmsrl(address, val);
196 end = (val & 0xffffff800000ULL);
197 printk(KERN_INFO "TOM: %016llx aka %lldM\n", end, end>>20);
198 if (end < (1ULL<<32))
199 subtract_range(range, RANGE_NUM, 0, end);
202 fam10h_mmconf = amd_get_mmconfig_range(&fam10h_mmconf_res);
203 /* need to take out mmconf range */
205 printk(KERN_DEBUG "Fam 10h mmconf %pR\n", fam10h_mmconf);
206 fam10h_mmconf_start = fam10h_mmconf->start;
207 fam10h_mmconf_end = fam10h_mmconf->end;
208 subtract_range(range, RANGE_NUM, fam10h_mmconf_start,
209 fam10h_mmconf_end + 1);
211 fam10h_mmconf_start = 0;
212 fam10h_mmconf_end = 0;
216 for (i = 0; i < 8; i++) {
217 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
221 start = reg & 0xffffff00; /* 39:16 on 31:8*/
223 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
225 link = (reg >> 4) & 0x03;
226 end = (reg & 0xffffff00);
230 info = find_pci_root_info(node, link);
235 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
236 node, link, start, end);
238 * some sick allocation would have range overlap with fam10h
239 * mmconf range, so need to update start and end.
241 if (fam10h_mmconf_end) {
244 if (start >= fam10h_mmconf_start &&
245 start <= fam10h_mmconf_end) {
246 start = fam10h_mmconf_end + 1;
250 if (end >= fam10h_mmconf_start &&
251 end <= fam10h_mmconf_end) {
252 end = fam10h_mmconf_start - 1;
256 if (start < fam10h_mmconf_start &&
257 end > fam10h_mmconf_end) {
259 endx = fam10h_mmconf_start - 1;
260 update_res(info, start, endx, IORESOURCE_MEM, 0);
261 subtract_range(range, RANGE_NUM, start,
263 printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
264 start = fam10h_mmconf_end + 1;
269 printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
271 printk(KERN_CONT "%s\n", endx?"":" ==> none");
277 update_res(info, cap_resource(start), cap_resource(end),
279 subtract_range(range, RANGE_NUM, start, end + 1);
280 printk(KERN_CONT "\n");
283 /* need to take out [4G, TOM2) for RAM*/
285 address = MSR_K8_SYSCFG;
286 rdmsrl(address, val);
287 /* TOP_MEM2 is enabled? */
290 address = MSR_K8_TOP_MEM2;
291 rdmsrl(address, val);
292 end = (val & 0xffffff800000ULL);
293 printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
294 subtract_range(range, RANGE_NUM, 1ULL<<32, end);
298 * add left over mmio range to def node/link ?
299 * that is tricky, just record range in from start_min to 4G
301 info = find_pci_root_info(def_node, def_link);
303 for (i = 0; i < RANGE_NUM; i++) {
307 update_res(info, cap_resource(range[i].start),
308 cap_resource(range[i].end - 1),
313 list_for_each_entry(info, &pci_root_infos, list) {
315 struct pci_root_res *root_res;
317 busnum = info->busn.start;
318 printk(KERN_DEBUG "bus: %pR on node %x link %x\n",
319 &info->busn, info->node, info->link);
320 list_for_each_entry(root_res, &info->resources, list)
321 printk(KERN_DEBUG "bus: %02x %pR\n",
322 busnum, &root_res->res);
328 #define ENABLE_CF8_EXT_CFG (1ULL << 46)
330 static void enable_pci_io_ecs(void *unused)
333 rdmsrl(MSR_AMD64_NB_CFG, reg);
334 if (!(reg & ENABLE_CF8_EXT_CFG)) {
335 reg |= ENABLE_CF8_EXT_CFG;
336 wrmsrl(MSR_AMD64_NB_CFG, reg);
340 static int amd_cpu_notify(struct notifier_block *self, unsigned long action,
343 int cpu = (long)hcpu;
346 case CPU_ONLINE_FROZEN:
347 smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
355 static struct notifier_block amd_cpu_notifier = {
356 .notifier_call = amd_cpu_notify,
359 static void __init pci_enable_pci_io_ecs(void)
364 for (n = i = 0; !n && amd_nb_bus_dev_ranges[i].dev_limit; ++i) {
365 u8 bus = amd_nb_bus_dev_ranges[i].bus;
366 u8 slot = amd_nb_bus_dev_ranges[i].dev_base;
367 u8 limit = amd_nb_bus_dev_ranges[i].dev_limit;
369 for (; slot < limit; ++slot) {
370 u32 val = read_pci_config(bus, slot, 3, 0);
372 if (!early_is_amd_nb(val))
375 val = read_pci_config(bus, slot, 3, 0x8c);
376 if (!(val & (ENABLE_CF8_EXT_CFG >> 32))) {
377 val |= ENABLE_CF8_EXT_CFG >> 32;
378 write_pci_config(bus, slot, 3, 0x8c, val);
386 static int __init pci_io_ecs_init(void)
390 /* assume all cpus from fam10h have IO ECS */
391 if (boot_cpu_data.x86 < 0x10)
394 /* Try the PCI method first. */
395 if (early_pci_allowed())
396 pci_enable_pci_io_ecs();
398 cpu_notifier_register_begin();
399 for_each_online_cpu(cpu)
400 amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
402 __register_cpu_notifier(&amd_cpu_notifier);
403 cpu_notifier_register_done();
405 pci_probe |= PCI_HAS_IO_ECS;
410 static int __init amd_postcore_init(void)
412 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
415 early_root_info_init();
421 postcore_initcall(amd_postcore_init);