blob: aa936e3a201999b273fa78e4c6cb9b50bd01a25f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/init.h>
2#include <linux/pci.h>
Robert Richterd199a042008-07-02 22:50:26 +02003#include <linux/topology.h>
Robert Richter91ede002008-08-22 20:23:38 +02004#include <linux/cpu.h>
Yinghai Lu27811d82010-02-10 01:20:07 -08005#include <linux/range.h>
6
Jan Beulich24d9b702011-01-10 16:20:23 +00007#include <asm/amd_nb.h>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +05308#include <asm/pci_x86.h>
Robert Richter3a27dd12008-06-12 20:19:23 +02009
Yinghai Lu871d5f82008-02-19 03:20:09 -080010#include <asm/pci-direct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Yinghai Lu99935a72009-10-04 21:54:24 -070012#include "bus_numa.h"
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/*
15 * This discovers the pcibus <-> node mapping on AMD K8.
Yinghai Lu30a18d62008-02-19 03:21:20 -080016 * also get peer root bus resource for io,mmio
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Yinghai Lu30a18d62008-02-19 03:21:20 -080019struct pci_hostbridge_probe {
20 u32 bus;
21 u32 slot;
22 u32 vendor;
23 u32 device;
24};
25
26static struct pci_hostbridge_probe pci_probes[] __initdata = {
Suravee Suthikulpanit94d4bb52014-05-08 11:44:18 -050027 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 }, /* K8 */
28 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 }, /* Fam10h */
29 { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 }, /* Fam10h */
30 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 }, /* Fam11h */
31 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1600 }, /* Fam15h */
Yinghai Lu30a18d62008-02-19 03:21:20 -080032};
33
Yinghai Lu27811d82010-02-10 01:20:07 -080034#define RANGE_NUM 16
35
Yinghai Lud28e5ac2012-04-02 18:31:54 -070036static struct pci_root_info __init *find_pci_root_info(int node, int link)
37{
38 struct pci_root_info *info;
39
40 /* find the position */
41 list_for_each_entry(info, &pci_root_infos, list)
42 if (info->node == node && info->link == link)
43 return info;
44
45 return NULL;
46}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/**
Yinghai Lu871d5f82008-02-19 03:20:09 -080049 * early_fill_mp_bus_to_node()
50 * called before pcibios_scan_root and pci_scan_bus
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
52 * Registers found in the K8 northbridge
53 */
Yinghai Lu30a18d62008-02-19 03:21:20 -080054static int __init early_fill_mp_bus_info(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Yinghai Lu30a18d62008-02-19 03:21:20 -080056 int i;
Yinghai Lu30a18d62008-02-19 03:21:20 -080057 unsigned bus;
Yinghai Lu871d5f82008-02-19 03:20:09 -080058 unsigned slot;
Yinghai Lu35ddd062008-02-19 03:15:08 -080059 int node;
Yinghai Lu30a18d62008-02-19 03:21:20 -080060 int link;
61 int def_node;
62 int def_link;
63 struct pci_root_info *info;
64 u32 reg;
Yinghai Lu97445c32010-02-10 01:20:10 -080065 u64 start;
66 u64 end;
Yinghai Lu27811d82010-02-10 01:20:07 -080067 struct range range[RANGE_NUM];
Yinghai Lu30a18d62008-02-19 03:21:20 -080068 u64 val;
69 u32 address;
Yinghai Lu3e3da002010-02-10 01:20:09 -080070 bool found;
Bjorn Helgaas24d25db2012-01-05 14:27:19 -070071 struct resource fam10h_mmconf_res, *fam10h_mmconf;
72 u64 fam10h_mmconf_start;
73 u64 fam10h_mmconf_end;
Yinghai Lu35ddd062008-02-19 03:15:08 -080074
Yinghai Lu871d5f82008-02-19 03:20:09 -080075 if (!early_pci_allowed())
76 return -1;
77
Yinghai Lu3e3da002010-02-10 01:20:09 -080078 found = false;
Yinghai Lu30a18d62008-02-19 03:21:20 -080079 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
80 u32 id;
81 u16 device;
82 u16 vendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Yinghai Lu30a18d62008-02-19 03:21:20 -080084 bus = pci_probes[i].bus;
85 slot = pci_probes[i].slot;
86 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
Yinghai Lu35ddd062008-02-19 03:15:08 -080087
Yinghai Lu30a18d62008-02-19 03:21:20 -080088 vendor = id & 0xffff;
89 device = (id>>16) & 0xffff;
90 if (pci_probes[i].vendor == vendor &&
91 pci_probes[i].device == device) {
Yinghai Lu3e3da002010-02-10 01:20:09 -080092 found = true;
Yinghai Lu30a18d62008-02-19 03:21:20 -080093 break;
94 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96
Yinghai Lu3e3da002010-02-10 01:20:09 -080097 if (!found)
Yinghai Lu30a18d62008-02-19 03:21:20 -080098 return 0;
99
Suravee Suthikulpanit94d4bb52014-05-08 11:44:18 -0500100 /*
101 * We should learn topology and routing information from _PXM and
102 * _CRS methods in the ACPI namespace. We extract node numbers
103 * here to work around BIOSes that don't supply _PXM.
104 */
Yinghai Lu30a18d62008-02-19 03:21:20 -0800105 for (i = 0; i < 4; i++) {
106 int min_bus;
107 int max_bus;
108 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
109
110 /* Check if that register is enabled for bus range */
111 if ((reg & 7) != 3)
112 continue;
113
114 min_bus = (reg >> 16) & 0xff;
115 max_bus = (reg >> 24) & 0xff;
116 node = (reg >> 4) & 0x07;
Yinghai Lu30a18d62008-02-19 03:21:20 -0800117 link = (reg >> 8) & 0x03;
118
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700119 info = alloc_pci_root_info(min_bus, max_bus, node, link);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800120 }
121
Suravee Suthikulpanit94d4bb52014-05-08 11:44:18 -0500122 /*
123 * The following code extracts routing information for use on old
124 * systems where Linux doesn't automatically use host bridge _CRS
125 * methods (or when the user specifies "pci=nocrs").
126 *
127 * We only do this through Fam11h, because _CRS should be enough on
128 * newer systems.
129 */
130 if (boot_cpu_data.x86 > 0x11)
131 return 0;
132
Yinghai Lu30a18d62008-02-19 03:21:20 -0800133 /* get the default node and link for left over res */
134 reg = read_pci_config(bus, slot, 0, 0x60);
135 def_node = (reg >> 8) & 0x07;
136 reg = read_pci_config(bus, slot, 0, 0x64);
137 def_link = (reg >> 8) & 0x03;
138
139 memset(range, 0, sizeof(range));
Yinghai Lue9a00642010-02-10 01:20:13 -0800140 add_range(range, RANGE_NUM, 0, 0, 0xffff + 1);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800141 /* io port resource */
142 for (i = 0; i < 4; i++) {
143 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
144 if (!(reg & 3))
145 continue;
146
147 start = reg & 0xfff000;
148 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
149 node = reg & 0x07;
150 link = (reg >> 4) & 0x03;
151 end = (reg & 0xfff000) | 0xfff;
152
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700153 info = find_pci_root_info(node, link);
154 if (!info)
Yinghai Lu30a18d62008-02-19 03:21:20 -0800155 continue; /* not found */
156
Yinghai Lu6e184f22008-03-06 01:15:31 -0800157 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
Yinghai Lu97445c32010-02-10 01:20:10 -0800158 node, link, start, end);
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700159
160 /* kernel only handle 16 bit only */
161 if (end > 0xffff)
162 end = 0xffff;
163 update_res(info, start, end, IORESOURCE_IO, 1);
Yinghai Lue9a00642010-02-10 01:20:13 -0800164 subtract_range(range, RANGE_NUM, start, end + 1);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800165 }
166 /* add left over io port range to def node/link, [0, 0xffff] */
167 /* find the position */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700168 info = find_pci_root_info(def_node, def_link);
169 if (info) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800170 for (i = 0; i < RANGE_NUM; i++) {
171 if (!range[i].end)
172 continue;
173
Yinghai Lue9a00642010-02-10 01:20:13 -0800174 update_res(info, range[i].start, range[i].end - 1,
Yinghai Lu30a18d62008-02-19 03:21:20 -0800175 IORESOURCE_IO, 1);
176 }
177 }
178
179 memset(range, 0, sizeof(range));
180 /* 0xfd00000000-0xffffffffff for HT */
Yinghai Lue9a00642010-02-10 01:20:13 -0800181 end = cap_resource((0xfdULL<<32) - 1);
182 end++;
183 add_range(range, RANGE_NUM, 0, 0, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800184
185 /* need to take out [0, TOM) for RAM*/
186 address = MSR_K8_TOP_MEM1;
187 rdmsrl(address, val);
Yinghai Lu8004dd92008-05-12 17:40:39 -0700188 end = (val & 0xffffff800000ULL);
Yinghai Lu97445c32010-02-10 01:20:10 -0800189 printk(KERN_INFO "TOM: %016llx aka %lldM\n", end, end>>20);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800190 if (end < (1ULL<<32))
Yinghai Lue9a00642010-02-10 01:20:13 -0800191 subtract_range(range, RANGE_NUM, 0, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800192
Yinghai Lu6e184f22008-03-06 01:15:31 -0800193 /* get mmconfig */
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700194 fam10h_mmconf = amd_get_mmconfig_range(&fam10h_mmconf_res);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800195 /* need to take out mmconf range */
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700196 if (fam10h_mmconf) {
197 printk(KERN_DEBUG "Fam 10h mmconf %pR\n", fam10h_mmconf);
198 fam10h_mmconf_start = fam10h_mmconf->start;
199 fam10h_mmconf_end = fam10h_mmconf->end;
Yinghai Lue9a00642010-02-10 01:20:13 -0800200 subtract_range(range, RANGE_NUM, fam10h_mmconf_start,
201 fam10h_mmconf_end + 1);
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700202 } else {
203 fam10h_mmconf_start = 0;
204 fam10h_mmconf_end = 0;
Yinghai Lu6e184f22008-03-06 01:15:31 -0800205 }
206
Yinghai Lu30a18d62008-02-19 03:21:20 -0800207 /* mmio resource */
208 for (i = 0; i < 8; i++) {
209 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
210 if (!(reg & 3))
211 continue;
212
213 start = reg & 0xffffff00; /* 39:16 on 31:8*/
214 start <<= 8;
215 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
216 node = reg & 0x07;
217 link = (reg >> 4) & 0x03;
218 end = (reg & 0xffffff00);
219 end <<= 8;
220 end |= 0xffff;
221
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700222 info = find_pci_root_info(node, link);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800223
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700224 if (!info)
225 continue;
Yinghai Lu6e184f22008-03-06 01:15:31 -0800226
227 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
Yinghai Lu97445c32010-02-10 01:20:10 -0800228 node, link, start, end);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800229 /*
230 * some sick allocation would have range overlap with fam10h
231 * mmconf range, so need to update start and end.
232 */
233 if (fam10h_mmconf_end) {
234 int changed = 0;
235 u64 endx = 0;
236 if (start >= fam10h_mmconf_start &&
237 start <= fam10h_mmconf_end) {
238 start = fam10h_mmconf_end + 1;
239 changed = 1;
240 }
241
242 if (end >= fam10h_mmconf_start &&
243 end <= fam10h_mmconf_end) {
244 end = fam10h_mmconf_start - 1;
245 changed = 1;
246 }
247
248 if (start < fam10h_mmconf_start &&
249 end > fam10h_mmconf_end) {
250 /* we got a hole */
251 endx = fam10h_mmconf_start - 1;
252 update_res(info, start, endx, IORESOURCE_MEM, 0);
Yinghai Lue9a00642010-02-10 01:20:13 -0800253 subtract_range(range, RANGE_NUM, start,
254 endx + 1);
Yinghai Lu97445c32010-02-10 01:20:10 -0800255 printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800256 start = fam10h_mmconf_end + 1;
257 changed = 1;
258 }
259 if (changed) {
260 if (start <= end) {
Yinghai Lu97445c32010-02-10 01:20:10 -0800261 printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800262 } else {
263 printk(KERN_CONT "%s\n", endx?"":" ==> none");
264 continue;
265 }
266 }
267 }
268
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -0800269 update_res(info, cap_resource(start), cap_resource(end),
270 IORESOURCE_MEM, 1);
Yinghai Lue9a00642010-02-10 01:20:13 -0800271 subtract_range(range, RANGE_NUM, start, end + 1);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800272 printk(KERN_CONT "\n");
Yinghai Lu30a18d62008-02-19 03:21:20 -0800273 }
274
275 /* need to take out [4G, TOM2) for RAM*/
276 /* SYS_CFG */
277 address = MSR_K8_SYSCFG;
278 rdmsrl(address, val);
279 /* TOP_MEM2 is enabled? */
280 if (val & (1<<21)) {
281 /* TOP_MEM2 */
282 address = MSR_K8_TOP_MEM2;
283 rdmsrl(address, val);
Yinghai Lu8004dd92008-05-12 17:40:39 -0700284 end = (val & 0xffffff800000ULL);
Yinghai Lu97445c32010-02-10 01:20:10 -0800285 printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
Yinghai Lue9a00642010-02-10 01:20:13 -0800286 subtract_range(range, RANGE_NUM, 1ULL<<32, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800287 }
288
289 /*
290 * add left over mmio range to def node/link ?
291 * that is tricky, just record range in from start_min to 4G
292 */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700293 info = find_pci_root_info(def_node, def_link);
294 if (info) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800295 for (i = 0; i < RANGE_NUM; i++) {
296 if (!range[i].end)
297 continue;
298
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -0800299 update_res(info, cap_resource(range[i].start),
Yinghai Lue9a00642010-02-10 01:20:13 -0800300 cap_resource(range[i].end - 1),
Yinghai Lu30a18d62008-02-19 03:21:20 -0800301 IORESOURCE_MEM, 1);
302 }
303 }
304
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700305 list_for_each_entry(info, &pci_root_infos, list) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800306 int busnum;
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700307 struct pci_root_res *root_res;
Yinghai Lu30a18d62008-02-19 03:21:20 -0800308
Yinghai Lua10bb122012-05-17 18:51:12 -0700309 busnum = info->busn.start;
310 printk(KERN_DEBUG "bus: %pR on node %x link %x\n",
311 &info->busn, info->node, info->link);
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700312 list_for_each_entry(root_res, &info->resources, list)
313 printk(KERN_DEBUG "bus: %02x %pR\n",
314 busnum, &root_res->res);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800315 }
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
318}
319
Robert Richter3a27dd12008-06-12 20:19:23 +0200320#define ENABLE_CF8_EXT_CFG (1ULL << 46)
321
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400322static void enable_pci_io_ecs(void *unused)
Robert Richter3a27dd12008-06-12 20:19:23 +0200323{
324 u64 reg;
325 rdmsrl(MSR_AMD64_NB_CFG, reg);
326 if (!(reg & ENABLE_CF8_EXT_CFG)) {
327 reg |= ENABLE_CF8_EXT_CFG;
328 wrmsrl(MSR_AMD64_NB_CFG, reg);
329 }
330}
331
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400332static int amd_cpu_notify(struct notifier_block *self, unsigned long action,
333 void *hcpu)
Robert Richter3a27dd12008-06-12 20:19:23 +0200334{
Robert Richter91ede002008-08-22 20:23:38 +0200335 int cpu = (long)hcpu;
Robert Richtered217632008-08-22 20:23:38 +0200336 switch (action) {
Robert Richter91ede002008-08-22 20:23:38 +0200337 case CPU_ONLINE:
338 case CPU_ONLINE_FROZEN:
339 smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
340 break;
341 default:
342 break;
343 }
344 return NOTIFY_OK;
345}
346
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400347static struct notifier_block amd_cpu_notifier = {
Robert Richter91ede002008-08-22 20:23:38 +0200348 .notifier_call = amd_cpu_notify,
349};
350
Jan Beulich24d9b702011-01-10 16:20:23 +0000351static void __init pci_enable_pci_io_ecs(void)
352{
353#ifdef CONFIG_AMD_NB
354 unsigned int i, n;
355
356 for (n = i = 0; !n && amd_nb_bus_dev_ranges[i].dev_limit; ++i) {
357 u8 bus = amd_nb_bus_dev_ranges[i].bus;
358 u8 slot = amd_nb_bus_dev_ranges[i].dev_base;
359 u8 limit = amd_nb_bus_dev_ranges[i].dev_limit;
360
361 for (; slot < limit; ++slot) {
362 u32 val = read_pci_config(bus, slot, 3, 0);
363
364 if (!early_is_amd_nb(val))
365 continue;
366
367 val = read_pci_config(bus, slot, 3, 0x8c);
368 if (!(val & (ENABLE_CF8_EXT_CFG >> 32))) {
369 val |= ENABLE_CF8_EXT_CFG >> 32;
370 write_pci_config(bus, slot, 3, 0x8c, val);
371 }
372 ++n;
373 }
374 }
Jan Beulich24d9b702011-01-10 16:20:23 +0000375#endif
376}
377
Robert Richter91ede002008-08-22 20:23:38 +0200378static int __init pci_io_ecs_init(void)
379{
380 int cpu;
381
Robert Richter3a27dd12008-06-12 20:19:23 +0200382 /* assume all cpus from fam10h have IO ECS */
383 if (boot_cpu_data.x86 < 0x10)
384 return 0;
Robert Richter91ede002008-08-22 20:23:38 +0200385
Jan Beulich24d9b702011-01-10 16:20:23 +0000386 /* Try the PCI method first. */
387 if (early_pci_allowed())
388 pci_enable_pci_io_ecs();
389
Srivatsa S. Bhat9f668f62014-03-11 02:08:43 +0530390 cpu_notifier_register_begin();
Robert Richter91ede002008-08-22 20:23:38 +0200391 for_each_online_cpu(cpu)
392 amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
393 (void *)(long)cpu);
Srivatsa S. Bhat9f668f62014-03-11 02:08:43 +0530394 __register_cpu_notifier(&amd_cpu_notifier);
395 cpu_notifier_register_done();
396
Robert Richter3a27dd12008-06-12 20:19:23 +0200397 pci_probe |= PCI_HAS_IO_ECS;
Robert Richter91ede002008-08-22 20:23:38 +0200398
Robert Richter3a27dd12008-06-12 20:19:23 +0200399 return 0;
400}
401
Robert Richter9b4e27b2008-08-22 20:23:37 +0200402static int __init amd_postcore_init(void)
403{
404 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
405 return 0;
406
407 early_fill_mp_bus_info();
Robert Richter91ede002008-08-22 20:23:38 +0200408 pci_io_ecs_init();
Robert Richter9b4e27b2008-08-22 20:23:37 +0200409
410 return 0;
411}
412
413postcore_initcall(amd_postcore_init);