aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/pci/amd_bus.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86/pci/amd_bus.c')
-rw-r--r--arch/x86/pci/amd_bus.c560
1 files changed, 560 insertions, 0 deletions
diff --git a/arch/x86/pci/amd_bus.c b/arch/x86/pci/amd_bus.c
new file mode 100644
index 000000000000..15f505d3a78e
--- /dev/null
+++ b/arch/x86/pci/amd_bus.c
@@ -0,0 +1,560 @@
1#include <linux/init.h>
2#include <linux/pci.h>
3#include "pci.h"
4
5#ifdef CONFIG_X86_64
6
7#include <asm/pci-direct.h>
8#include <asm/mpspec.h>
9#include <linux/cpumask.h>
10#include <linux/topology.h>
11
12/*
13 * This discovers the pcibus <-> node mapping on AMD K8.
14 * also get peer root bus resource for io,mmio
15 */
16
17
18/*
19 * sub bus (transparent) will use entres from 3 to store extra from root,
20 * so need to make sure have enought slot there, increase PCI_BUS_NUM_RESOURCES?
21 */
22#define RES_NUM 16
23struct pci_root_info {
24 char name[12];
25 unsigned int res_num;
26 struct resource res[RES_NUM];
27 int bus_min;
28 int bus_max;
29 int node;
30 int link;
31};
32
33/* 4 at this time, it may become to 32 */
34#define PCI_ROOT_NR 4
35static int pci_root_num;
36static struct pci_root_info pci_root_info[PCI_ROOT_NR];
37
38#ifdef CONFIG_NUMA
39
40#define BUS_NR 256
41
42static int mp_bus_to_node[BUS_NR];
43
44void set_mp_bus_to_node(int busnum, int node)
45{
46 if (busnum >= 0 && busnum < BUS_NR)
47 mp_bus_to_node[busnum] = node;
48}
49
50int get_mp_bus_to_node(int busnum)
51{
52 int node = -1;
53
54 if (busnum < 0 || busnum > (BUS_NR - 1))
55 return node;
56
57 node = mp_bus_to_node[busnum];
58
59 /*
60 * let numa_node_id to decide it later in dma_alloc_pages
61 * if there is no ram on that node
62 */
63 if (node != -1 && !node_online(node))
64 node = -1;
65
66 return node;
67}
68#endif
69
70void set_pci_bus_resources_arch_default(struct pci_bus *b)
71{
72 int i;
73 int j;
74 struct pci_root_info *info;
75
76 /* if only one root bus, don't need to anything */
77 if (pci_root_num < 2)
78 return;
79
80 for (i = 0; i < pci_root_num; i++) {
81 if (pci_root_info[i].bus_min == b->number)
82 break;
83 }
84
85 if (i == pci_root_num)
86 return;
87
88 info = &pci_root_info[i];
89 for (j = 0; j < info->res_num; j++) {
90 struct resource *res;
91 struct resource *root;
92
93 res = &info->res[j];
94 b->resource[j] = res;
95 if (res->flags & IORESOURCE_IO)
96 root = &ioport_resource;
97 else
98 root = &iomem_resource;
99 insert_resource(root, res);
100 }
101}
102
103#define RANGE_NUM 16
104
105struct res_range {
106 size_t start;
107 size_t end;
108};
109
110static void __init update_range(struct res_range *range, size_t start,
111 size_t end)
112{
113 int i;
114 int j;
115
116 for (j = 0; j < RANGE_NUM; j++) {
117 if (!range[j].end)
118 continue;
119
120 if (start <= range[j].start && end >= range[j].end) {
121 range[j].start = 0;
122 range[j].end = 0;
123 continue;
124 }
125
126 if (start <= range[j].start && end < range[j].end && range[j].start < end + 1) {
127 range[j].start = end + 1;
128 continue;
129 }
130
131
132 if (start > range[j].start && end >= range[j].end && range[j].end > start - 1) {
133 range[j].end = start - 1;
134 continue;
135 }
136
137 if (start > range[j].start && end < range[j].end) {
138 /* find the new spare */
139 for (i = 0; i < RANGE_NUM; i++) {
140 if (range[i].end == 0)
141 break;
142 }
143 if (i < RANGE_NUM) {
144 range[i].end = range[j].end;
145 range[i].start = end + 1;
146 } else {
147 printk(KERN_ERR "run of slot in ranges\n");
148 }
149 range[j].end = start - 1;
150 continue;
151 }
152 }
153}
154
155static void __init update_res(struct pci_root_info *info, size_t start,
156 size_t end, unsigned long flags, int merge)
157{
158 int i;
159 struct resource *res;
160
161 if (!merge)
162 goto addit;
163
164 /* try to merge it with old one */
165 for (i = 0; i < info->res_num; i++) {
166 size_t final_start, final_end;
167 size_t common_start, common_end;
168
169 res = &info->res[i];
170 if (res->flags != flags)
171 continue;
172
173 common_start = max((size_t)res->start, start);
174 common_end = min((size_t)res->end, end);
175 if (common_start > common_end + 1)
176 continue;
177
178 final_start = min((size_t)res->start, start);
179 final_end = max((size_t)res->end, end);
180
181 res->start = final_start;
182 res->end = final_end;
183 return;
184 }
185
186addit:
187
188 /* need to add that */
189 if (info->res_num >= RES_NUM)
190 return;
191
192 res = &info->res[info->res_num];
193 res->name = info->name;
194 res->flags = flags;
195 res->start = start;
196 res->end = end;
197 res->child = NULL;
198 info->res_num++;
199}
200
201struct pci_hostbridge_probe {
202 u32 bus;
203 u32 slot;
204 u32 vendor;
205 u32 device;
206};
207
208static struct pci_hostbridge_probe pci_probes[] __initdata = {
209 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
210 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
211 { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
212 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
213};
214
215static u64 __initdata fam10h_mmconf_start;
216static u64 __initdata fam10h_mmconf_end;
217static void __init get_pci_mmcfg_amd_fam10h_range(void)
218{
219 u32 address;
220 u64 base, msr;
221 unsigned segn_busn_bits;
222
223 /* assume all cpus from fam10h have mmconf */
224 if (boot_cpu_data.x86 < 0x10)
225 return;
226
227 address = MSR_FAM10H_MMIO_CONF_BASE;
228 rdmsrl(address, msr);
229
230 /* mmconfig is not enable */
231 if (!(msr & FAM10H_MMIO_CONF_ENABLE))
232 return;
233
234 base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
235
236 segn_busn_bits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
237 FAM10H_MMIO_CONF_BUSRANGE_MASK;
238
239 fam10h_mmconf_start = base;
240 fam10h_mmconf_end = base + (1ULL<<(segn_busn_bits + 20)) - 1;
241}
242
243/**
244 * early_fill_mp_bus_to_node()
245 * called before pcibios_scan_root and pci_scan_bus
246 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
247 * Registers found in the K8 northbridge
248 */
249static int __init early_fill_mp_bus_info(void)
250{
251 int i;
252 int j;
253 unsigned bus;
254 unsigned slot;
255 int found;
256 int node;
257 int link;
258 int def_node;
259 int def_link;
260 struct pci_root_info *info;
261 u32 reg;
262 struct resource *res;
263 size_t start;
264 size_t end;
265 struct res_range range[RANGE_NUM];
266 u64 val;
267 u32 address;
268
269#ifdef CONFIG_NUMA
270 for (i = 0; i < BUS_NR; i++)
271 mp_bus_to_node[i] = -1;
272#endif
273
274 if (!early_pci_allowed())
275 return -1;
276
277 found = 0;
278 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
279 u32 id;
280 u16 device;
281 u16 vendor;
282
283 bus = pci_probes[i].bus;
284 slot = pci_probes[i].slot;
285 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
286
287 vendor = id & 0xffff;
288 device = (id>>16) & 0xffff;
289 if (pci_probes[i].vendor == vendor &&
290 pci_probes[i].device == device) {
291 found = 1;
292 break;
293 }
294 }
295
296 if (!found)
297 return 0;
298
299 pci_root_num = 0;
300 for (i = 0; i < 4; i++) {
301 int min_bus;
302 int max_bus;
303 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
304
305 /* Check if that register is enabled for bus range */
306 if ((reg & 7) != 3)
307 continue;
308
309 min_bus = (reg >> 16) & 0xff;
310 max_bus = (reg >> 24) & 0xff;
311 node = (reg >> 4) & 0x07;
312#ifdef CONFIG_NUMA
313 for (j = min_bus; j <= max_bus; j++)
314 mp_bus_to_node[j] = (unsigned char) node;
315#endif
316 link = (reg >> 8) & 0x03;
317
318 info = &pci_root_info[pci_root_num];
319 info->bus_min = min_bus;
320 info->bus_max = max_bus;
321 info->node = node;
322 info->link = link;
323 sprintf(info->name, "PCI Bus #%02x", min_bus);
324 pci_root_num++;
325 }
326
327 /* get the default node and link for left over res */
328 reg = read_pci_config(bus, slot, 0, 0x60);
329 def_node = (reg >> 8) & 0x07;
330 reg = read_pci_config(bus, slot, 0, 0x64);
331 def_link = (reg >> 8) & 0x03;
332
333 memset(range, 0, sizeof(range));
334 range[0].end = 0xffff;
335 /* io port resource */
336 for (i = 0; i < 4; i++) {
337 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
338 if (!(reg & 3))
339 continue;
340
341 start = reg & 0xfff000;
342 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
343 node = reg & 0x07;
344 link = (reg >> 4) & 0x03;
345 end = (reg & 0xfff000) | 0xfff;
346
347 /* find the position */
348 for (j = 0; j < pci_root_num; j++) {
349 info = &pci_root_info[j];
350 if (info->node == node && info->link == link)
351 break;
352 }
353 if (j == pci_root_num)
354 continue; /* not found */
355
356 info = &pci_root_info[j];
357 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
358 node, link, (u64)start, (u64)end);
359
360 /* kernel only handle 16 bit only */
361 if (end > 0xffff)
362 end = 0xffff;
363 update_res(info, start, end, IORESOURCE_IO, 1);
364 update_range(range, start, end);
365 }
366 /* add left over io port range to def node/link, [0, 0xffff] */
367 /* find the position */
368 for (j = 0; j < pci_root_num; j++) {
369 info = &pci_root_info[j];
370 if (info->node == def_node && info->link == def_link)
371 break;
372 }
373 if (j < pci_root_num) {
374 info = &pci_root_info[j];
375 for (i = 0; i < RANGE_NUM; i++) {
376 if (!range[i].end)
377 continue;
378
379 update_res(info, range[i].start, range[i].end,
380 IORESOURCE_IO, 1);
381 }
382 }
383
384 memset(range, 0, sizeof(range));
385 /* 0xfd00000000-0xffffffffff for HT */
386 range[0].end = (0xfdULL<<32) - 1;
387
388 /* need to take out [0, TOM) for RAM*/
389 address = MSR_K8_TOP_MEM1;
390 rdmsrl(address, val);
391 end = (val & 0xffffff8000000ULL);
392 printk(KERN_INFO "TOM: %016lx aka %ldM\n", end, end>>20);
393 if (end < (1ULL<<32))
394 update_range(range, 0, end - 1);
395
396 /* get mmconfig */
397 get_pci_mmcfg_amd_fam10h_range();
398 /* need to take out mmconf range */
399 if (fam10h_mmconf_end) {
400 printk(KERN_DEBUG "Fam 10h mmconf [%llx, %llx]\n", fam10h_mmconf_start, fam10h_mmconf_end);
401 update_range(range, fam10h_mmconf_start, fam10h_mmconf_end);
402 }
403
404 /* mmio resource */
405 for (i = 0; i < 8; i++) {
406 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
407 if (!(reg & 3))
408 continue;
409
410 start = reg & 0xffffff00; /* 39:16 on 31:8*/
411 start <<= 8;
412 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
413 node = reg & 0x07;
414 link = (reg >> 4) & 0x03;
415 end = (reg & 0xffffff00);
416 end <<= 8;
417 end |= 0xffff;
418
419 /* find the position */
420 for (j = 0; j < pci_root_num; j++) {
421 info = &pci_root_info[j];
422 if (info->node == node && info->link == link)
423 break;
424 }
425 if (j == pci_root_num)
426 continue; /* not found */
427
428 info = &pci_root_info[j];
429
430 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
431 node, link, (u64)start, (u64)end);
432 /*
433 * some sick allocation would have range overlap with fam10h
434 * mmconf range, so need to update start and end.
435 */
436 if (fam10h_mmconf_end) {
437 int changed = 0;
438 u64 endx = 0;
439 if (start >= fam10h_mmconf_start &&
440 start <= fam10h_mmconf_end) {
441 start = fam10h_mmconf_end + 1;
442 changed = 1;
443 }
444
445 if (end >= fam10h_mmconf_start &&
446 end <= fam10h_mmconf_end) {
447 end = fam10h_mmconf_start - 1;
448 changed = 1;
449 }
450
451 if (start < fam10h_mmconf_start &&
452 end > fam10h_mmconf_end) {
453 /* we got a hole */
454 endx = fam10h_mmconf_start - 1;
455 update_res(info, start, endx, IORESOURCE_MEM, 0);
456 update_range(range, start, endx);
457 printk(KERN_CONT " ==> [%llx, %llx]", (u64)start, endx);
458 start = fam10h_mmconf_end + 1;
459 changed = 1;
460 }
461 if (changed) {
462 if (start <= end) {
463 printk(KERN_CONT " %s [%llx, %llx]", endx?"and":"==>", (u64)start, (u64)end);
464 } else {
465 printk(KERN_CONT "%s\n", endx?"":" ==> none");
466 continue;
467 }
468 }
469 }
470
471 update_res(info, start, end, IORESOURCE_MEM, 1);
472 update_range(range, start, end);
473 printk(KERN_CONT "\n");
474 }
475
476 /* need to take out [4G, TOM2) for RAM*/
477 /* SYS_CFG */
478 address = MSR_K8_SYSCFG;
479 rdmsrl(address, val);
480 /* TOP_MEM2 is enabled? */
481 if (val & (1<<21)) {
482 /* TOP_MEM2 */
483 address = MSR_K8_TOP_MEM2;
484 rdmsrl(address, val);
485 end = (val & 0xffffff8000000ULL);
486 printk(KERN_INFO "TOM2: %016lx aka %ldM\n", end, end>>20);
487 update_range(range, 1ULL<<32, end - 1);
488 }
489
490 /*
491 * add left over mmio range to def node/link ?
492 * that is tricky, just record range in from start_min to 4G
493 */
494 for (j = 0; j < pci_root_num; j++) {
495 info = &pci_root_info[j];
496 if (info->node == def_node && info->link == def_link)
497 break;
498 }
499 if (j < pci_root_num) {
500 info = &pci_root_info[j];
501
502 for (i = 0; i < RANGE_NUM; i++) {
503 if (!range[i].end)
504 continue;
505
506 update_res(info, range[i].start, range[i].end,
507 IORESOURCE_MEM, 1);
508 }
509 }
510
511 for (i = 0; i < pci_root_num; i++) {
512 int res_num;
513 int busnum;
514
515 info = &pci_root_info[i];
516 res_num = info->res_num;
517 busnum = info->bus_min;
518 printk(KERN_DEBUG "bus: [%02x,%02x] on node %x link %x\n",
519 info->bus_min, info->bus_max, info->node, info->link);
520 for (j = 0; j < res_num; j++) {
521 res = &info->res[j];
522 printk(KERN_DEBUG "bus: %02x index %x %s: [%llx, %llx]\n",
523 busnum, j,
524 (res->flags & IORESOURCE_IO)?"io port":"mmio",
525 res->start, res->end);
526 }
527 }
528
529 return 0;
530}
531
532postcore_initcall(early_fill_mp_bus_info);
533
534#endif
535
536/* common 32/64 bit code */
537
538#define ENABLE_CF8_EXT_CFG (1ULL << 46)
539
540static void enable_pci_io_ecs_per_cpu(void *unused)
541{
542 u64 reg;
543 rdmsrl(MSR_AMD64_NB_CFG, reg);
544 if (!(reg & ENABLE_CF8_EXT_CFG)) {
545 reg |= ENABLE_CF8_EXT_CFG;
546 wrmsrl(MSR_AMD64_NB_CFG, reg);
547 }
548}
549
550static int __init enable_pci_io_ecs(void)
551{
552 /* assume all cpus from fam10h have IO ECS */
553 if (boot_cpu_data.x86 < 0x10)
554 return 0;
555 on_each_cpu(enable_pci_io_ecs_per_cpu, NULL, 1, 1);
556 pci_probe |= PCI_HAS_IO_ECS;
557 return 0;
558}
559
560postcore_initcall(enable_pci_io_ecs);