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