blob: 459a7316375c543267d892ba98ac6e4039d6979d [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 = {
27 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
28 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
29 { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
30 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
31};
32
Yinghai Lu27811d82010-02-10 01:20:07 -080033#define RANGE_NUM 16
34
Yinghai Lud28e5ac2012-04-02 18:31:54 -070035static struct pci_root_info __init *find_pci_root_info(int node, int link)
36{
37 struct pci_root_info *info;
38
39 /* find the position */
40 list_for_each_entry(info, &pci_root_infos, list)
41 if (info->node == node && info->link == link)
42 return info;
43
44 return NULL;
45}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/**
Yinghai Lu871d5f82008-02-19 03:20:09 -080048 * early_fill_mp_bus_to_node()
49 * called before pcibios_scan_root and pci_scan_bus
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
51 * Registers found in the K8 northbridge
52 */
Yinghai Lu30a18d62008-02-19 03:21:20 -080053static int __init early_fill_mp_bus_info(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Yinghai Lu30a18d62008-02-19 03:21:20 -080055 int i;
56 int j;
57 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
Yinghai Lu30a18d62008-02-19 03:21:20 -0800100 for (i = 0; i < 4; i++) {
101 int min_bus;
102 int max_bus;
103 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
104
105 /* Check if that register is enabled for bus range */
106 if ((reg & 7) != 3)
107 continue;
108
109 min_bus = (reg >> 16) & 0xff;
110 max_bus = (reg >> 24) & 0xff;
111 node = (reg >> 4) & 0x07;
112#ifdef CONFIG_NUMA
113 for (j = min_bus; j <= max_bus; j++)
Jesse Barnes25470892009-07-10 14:04:30 -0700114 set_mp_bus_to_node(j, node);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800115#endif
116 link = (reg >> 8) & 0x03;
117
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700118 info = alloc_pci_root_info(min_bus, max_bus, node, link);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800119 sprintf(info->name, "PCI Bus #%02x", min_bus);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800120 }
121
122 /* get the default node and link for left over res */
123 reg = read_pci_config(bus, slot, 0, 0x60);
124 def_node = (reg >> 8) & 0x07;
125 reg = read_pci_config(bus, slot, 0, 0x64);
126 def_link = (reg >> 8) & 0x03;
127
128 memset(range, 0, sizeof(range));
Yinghai Lue9a00642010-02-10 01:20:13 -0800129 add_range(range, RANGE_NUM, 0, 0, 0xffff + 1);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800130 /* io port resource */
131 for (i = 0; i < 4; i++) {
132 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
133 if (!(reg & 3))
134 continue;
135
136 start = reg & 0xfff000;
137 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
138 node = reg & 0x07;
139 link = (reg >> 4) & 0x03;
140 end = (reg & 0xfff000) | 0xfff;
141
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700142 info = find_pci_root_info(node, link);
143 if (!info)
Yinghai Lu30a18d62008-02-19 03:21:20 -0800144 continue; /* not found */
145
Yinghai Lu6e184f22008-03-06 01:15:31 -0800146 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
Yinghai Lu97445c32010-02-10 01:20:10 -0800147 node, link, start, end);
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700148
149 /* kernel only handle 16 bit only */
150 if (end > 0xffff)
151 end = 0xffff;
152 update_res(info, start, end, IORESOURCE_IO, 1);
Yinghai Lue9a00642010-02-10 01:20:13 -0800153 subtract_range(range, RANGE_NUM, start, end + 1);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800154 }
155 /* add left over io port range to def node/link, [0, 0xffff] */
156 /* find the position */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700157 info = find_pci_root_info(def_node, def_link);
158 if (info) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800159 for (i = 0; i < RANGE_NUM; i++) {
160 if (!range[i].end)
161 continue;
162
Yinghai Lue9a00642010-02-10 01:20:13 -0800163 update_res(info, range[i].start, range[i].end - 1,
Yinghai Lu30a18d62008-02-19 03:21:20 -0800164 IORESOURCE_IO, 1);
165 }
166 }
167
168 memset(range, 0, sizeof(range));
169 /* 0xfd00000000-0xffffffffff for HT */
Yinghai Lue9a00642010-02-10 01:20:13 -0800170 end = cap_resource((0xfdULL<<32) - 1);
171 end++;
172 add_range(range, RANGE_NUM, 0, 0, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800173
174 /* need to take out [0, TOM) for RAM*/
175 address = MSR_K8_TOP_MEM1;
176 rdmsrl(address, val);
Yinghai Lu8004dd92008-05-12 17:40:39 -0700177 end = (val & 0xffffff800000ULL);
Yinghai Lu97445c32010-02-10 01:20:10 -0800178 printk(KERN_INFO "TOM: %016llx aka %lldM\n", end, end>>20);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800179 if (end < (1ULL<<32))
Yinghai Lue9a00642010-02-10 01:20:13 -0800180 subtract_range(range, RANGE_NUM, 0, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800181
Yinghai Lu6e184f22008-03-06 01:15:31 -0800182 /* get mmconfig */
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700183 fam10h_mmconf = amd_get_mmconfig_range(&fam10h_mmconf_res);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800184 /* need to take out mmconf range */
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700185 if (fam10h_mmconf) {
186 printk(KERN_DEBUG "Fam 10h mmconf %pR\n", fam10h_mmconf);
187 fam10h_mmconf_start = fam10h_mmconf->start;
188 fam10h_mmconf_end = fam10h_mmconf->end;
Yinghai Lue9a00642010-02-10 01:20:13 -0800189 subtract_range(range, RANGE_NUM, fam10h_mmconf_start,
190 fam10h_mmconf_end + 1);
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700191 } else {
192 fam10h_mmconf_start = 0;
193 fam10h_mmconf_end = 0;
Yinghai Lu6e184f22008-03-06 01:15:31 -0800194 }
195
Yinghai Lu30a18d62008-02-19 03:21:20 -0800196 /* mmio resource */
197 for (i = 0; i < 8; i++) {
198 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
199 if (!(reg & 3))
200 continue;
201
202 start = reg & 0xffffff00; /* 39:16 on 31:8*/
203 start <<= 8;
204 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
205 node = reg & 0x07;
206 link = (reg >> 4) & 0x03;
207 end = (reg & 0xffffff00);
208 end <<= 8;
209 end |= 0xffff;
210
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700211 info = find_pci_root_info(node, link);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800212
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700213 if (!info)
214 continue;
Yinghai Lu6e184f22008-03-06 01:15:31 -0800215
216 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
Yinghai Lu97445c32010-02-10 01:20:10 -0800217 node, link, start, end);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800218 /*
219 * some sick allocation would have range overlap with fam10h
220 * mmconf range, so need to update start and end.
221 */
222 if (fam10h_mmconf_end) {
223 int changed = 0;
224 u64 endx = 0;
225 if (start >= fam10h_mmconf_start &&
226 start <= fam10h_mmconf_end) {
227 start = fam10h_mmconf_end + 1;
228 changed = 1;
229 }
230
231 if (end >= fam10h_mmconf_start &&
232 end <= fam10h_mmconf_end) {
233 end = fam10h_mmconf_start - 1;
234 changed = 1;
235 }
236
237 if (start < fam10h_mmconf_start &&
238 end > fam10h_mmconf_end) {
239 /* we got a hole */
240 endx = fam10h_mmconf_start - 1;
241 update_res(info, start, endx, IORESOURCE_MEM, 0);
Yinghai Lue9a00642010-02-10 01:20:13 -0800242 subtract_range(range, RANGE_NUM, start,
243 endx + 1);
Yinghai Lu97445c32010-02-10 01:20:10 -0800244 printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800245 start = fam10h_mmconf_end + 1;
246 changed = 1;
247 }
248 if (changed) {
249 if (start <= end) {
Yinghai Lu97445c32010-02-10 01:20:10 -0800250 printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800251 } else {
252 printk(KERN_CONT "%s\n", endx?"":" ==> none");
253 continue;
254 }
255 }
256 }
257
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -0800258 update_res(info, cap_resource(start), cap_resource(end),
259 IORESOURCE_MEM, 1);
Yinghai Lue9a00642010-02-10 01:20:13 -0800260 subtract_range(range, RANGE_NUM, start, end + 1);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800261 printk(KERN_CONT "\n");
Yinghai Lu30a18d62008-02-19 03:21:20 -0800262 }
263
264 /* need to take out [4G, TOM2) for RAM*/
265 /* SYS_CFG */
266 address = MSR_K8_SYSCFG;
267 rdmsrl(address, val);
268 /* TOP_MEM2 is enabled? */
269 if (val & (1<<21)) {
270 /* TOP_MEM2 */
271 address = MSR_K8_TOP_MEM2;
272 rdmsrl(address, val);
Yinghai Lu8004dd92008-05-12 17:40:39 -0700273 end = (val & 0xffffff800000ULL);
Yinghai Lu97445c32010-02-10 01:20:10 -0800274 printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
Yinghai Lue9a00642010-02-10 01:20:13 -0800275 subtract_range(range, RANGE_NUM, 1ULL<<32, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800276 }
277
278 /*
279 * add left over mmio range to def node/link ?
280 * that is tricky, just record range in from start_min to 4G
281 */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700282 info = find_pci_root_info(def_node, def_link);
283 if (info) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800284 for (i = 0; i < RANGE_NUM; i++) {
285 if (!range[i].end)
286 continue;
287
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -0800288 update_res(info, cap_resource(range[i].start),
Yinghai Lue9a00642010-02-10 01:20:13 -0800289 cap_resource(range[i].end - 1),
Yinghai Lu30a18d62008-02-19 03:21:20 -0800290 IORESOURCE_MEM, 1);
291 }
292 }
293
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700294 list_for_each_entry(info, &pci_root_infos, list) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800295 int busnum;
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700296 struct pci_root_res *root_res;
Yinghai Lu30a18d62008-02-19 03:21:20 -0800297
Yinghai Lu30a18d62008-02-19 03:21:20 -0800298 busnum = info->bus_min;
Yinghai Lu99935a72009-10-04 21:54:24 -0700299 printk(KERN_DEBUG "bus: [%02x, %02x] on node %x link %x\n",
Yinghai Lu30a18d62008-02-19 03:21:20 -0800300 info->bus_min, info->bus_max, info->node, info->link);
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700301 list_for_each_entry(root_res, &info->resources, list)
302 printk(KERN_DEBUG "bus: %02x %pR\n",
303 busnum, &root_res->res);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800304 }
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 0;
307}
308
Robert Richter3a27dd12008-06-12 20:19:23 +0200309#define ENABLE_CF8_EXT_CFG (1ULL << 46)
310
Jan Beulich691269f2011-02-09 08:26:53 +0000311static void __cpuinit enable_pci_io_ecs(void *unused)
Robert Richter3a27dd12008-06-12 20:19:23 +0200312{
313 u64 reg;
314 rdmsrl(MSR_AMD64_NB_CFG, reg);
315 if (!(reg & ENABLE_CF8_EXT_CFG)) {
316 reg |= ENABLE_CF8_EXT_CFG;
317 wrmsrl(MSR_AMD64_NB_CFG, reg);
318 }
319}
320
Robert Richter91ede002008-08-22 20:23:38 +0200321static int __cpuinit amd_cpu_notify(struct notifier_block *self,
322 unsigned long action, void *hcpu)
Robert Richter3a27dd12008-06-12 20:19:23 +0200323{
Robert Richter91ede002008-08-22 20:23:38 +0200324 int cpu = (long)hcpu;
Robert Richtered217632008-08-22 20:23:38 +0200325 switch (action) {
Robert Richter91ede002008-08-22 20:23:38 +0200326 case CPU_ONLINE:
327 case CPU_ONLINE_FROZEN:
328 smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
329 break;
330 default:
331 break;
332 }
333 return NOTIFY_OK;
334}
335
336static struct notifier_block __cpuinitdata amd_cpu_notifier = {
337 .notifier_call = amd_cpu_notify,
338};
339
Jan Beulich24d9b702011-01-10 16:20:23 +0000340static void __init pci_enable_pci_io_ecs(void)
341{
342#ifdef CONFIG_AMD_NB
343 unsigned int i, n;
344
345 for (n = i = 0; !n && amd_nb_bus_dev_ranges[i].dev_limit; ++i) {
346 u8 bus = amd_nb_bus_dev_ranges[i].bus;
347 u8 slot = amd_nb_bus_dev_ranges[i].dev_base;
348 u8 limit = amd_nb_bus_dev_ranges[i].dev_limit;
349
350 for (; slot < limit; ++slot) {
351 u32 val = read_pci_config(bus, slot, 3, 0);
352
353 if (!early_is_amd_nb(val))
354 continue;
355
356 val = read_pci_config(bus, slot, 3, 0x8c);
357 if (!(val & (ENABLE_CF8_EXT_CFG >> 32))) {
358 val |= ENABLE_CF8_EXT_CFG >> 32;
359 write_pci_config(bus, slot, 3, 0x8c, val);
360 }
361 ++n;
362 }
363 }
Jan Beulich24d9b702011-01-10 16:20:23 +0000364#endif
365}
366
Robert Richter91ede002008-08-22 20:23:38 +0200367static int __init pci_io_ecs_init(void)
368{
369 int cpu;
370
Robert Richter3a27dd12008-06-12 20:19:23 +0200371 /* assume all cpus from fam10h have IO ECS */
372 if (boot_cpu_data.x86 < 0x10)
373 return 0;
Robert Richter91ede002008-08-22 20:23:38 +0200374
Jan Beulich24d9b702011-01-10 16:20:23 +0000375 /* Try the PCI method first. */
376 if (early_pci_allowed())
377 pci_enable_pci_io_ecs();
378
Robert Richter91ede002008-08-22 20:23:38 +0200379 register_cpu_notifier(&amd_cpu_notifier);
380 for_each_online_cpu(cpu)
381 amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
382 (void *)(long)cpu);
Robert Richter3a27dd12008-06-12 20:19:23 +0200383 pci_probe |= PCI_HAS_IO_ECS;
Robert Richter91ede002008-08-22 20:23:38 +0200384
Robert Richter3a27dd12008-06-12 20:19:23 +0200385 return 0;
386}
387
Robert Richter9b4e27b2008-08-22 20:23:37 +0200388static int __init amd_postcore_init(void)
389{
390 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
391 return 0;
392
393 early_fill_mp_bus_info();
Robert Richter91ede002008-08-22 20:23:38 +0200394 pci_io_ecs_init();
Robert Richter9b4e27b2008-08-22 20:23:37 +0200395
396 return 0;
397}
398
399postcore_initcall(amd_postcore_init);