x86: multi pci root bus with different io resource range, on 64-bit
[safe/jmp/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 (!pci_root_num)
73                 return;
74
75         for (i = 0; i < pci_root_num; i++) {
76                 if (pci_root_info[i].bus_min == b->number)
77                         break;
78         }
79
80         if (i == pci_root_num)
81                 return;
82
83         info = &pci_root_info[i];
84         for (j = 0; j < info->res_num; j++) {
85                 struct resource *res;
86                 struct resource *root;
87
88                 res = &info->res[j];
89                 b->resource[j] = res;
90                 if (res->flags & IORESOURCE_IO)
91                         root = &ioport_resource;
92                 else
93                         root = &iomem_resource;
94                 insert_resource(root, res);
95         }
96 }
97
98 #define RANGE_NUM 16
99
100 struct res_range {
101         size_t start;
102         size_t end;
103 };
104
105 static void __init update_range(struct res_range *range, size_t start,
106                                 size_t end)
107 {
108         int i;
109         int j;
110
111         for (j = 0; j < RANGE_NUM; j++) {
112                 if (!range[j].end)
113                         continue;
114                 if (start == range[j].start && end < range[j].end) {
115                         range[j].start = end + 1;
116                         break;
117                 } else if (start == range[j].start && end == range[j].end) {
118                         range[j].start = 0;
119                         range[j].end = 0;
120                         break;
121                 } else if (start > range[j].start && end == range[j].end) {
122                         range[j].end = start - 1;
123                         break;
124                 } else if (start > range[j].start && end < range[j].end) {
125                         /* find the new spare */
126                         for (i = 0; i < RANGE_NUM; i++) {
127                                 if (range[i].end == 0)
128                                         break;
129                         }
130                         if (i < RANGE_NUM) {
131                                 range[i].end = range[j].end;
132                                 range[i].start = end + 1;
133                         } else {
134                                 printk(KERN_ERR "run of slot in ranges\n");
135                         }
136                         range[j].end = start - 1;
137                         break;
138                 }
139         }
140 }
141
142 static void __init update_res(struct pci_root_info *info, size_t start,
143                               size_t end, unsigned long flags, int merge)
144 {
145         int i;
146         struct resource *res;
147
148         if (!merge)
149                 goto addit;
150
151         /* try to merge it with old one */
152         for (i = 0; i < info->res_num; i++) {
153                 res = &info->res[i];
154                 if (res->flags != flags)
155                         continue;
156                 if (res->end + 1 == start) {
157                         res->end = end;
158                         return;
159                 } else if (end + 1 == res->start) {
160                         res->start = start;
161                         return;
162                 }
163         }
164
165 addit:
166
167         /* need to add that */
168         if (info->res_num >= RES_NUM)
169                 return;
170
171         res = &info->res[info->res_num];
172         res->name = info->name;
173         res->flags = flags;
174         res->start = start;
175         res->end = end;
176         res->child = NULL;
177         info->res_num++;
178 }
179
180 struct pci_hostbridge_probe {
181         u32 bus;
182         u32 slot;
183         u32 vendor;
184         u32 device;
185 };
186
187 static struct pci_hostbridge_probe pci_probes[] __initdata = {
188         { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
189         { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
190         { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
191         { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
192 };
193
194 /**
195  * early_fill_mp_bus_to_node()
196  * called before pcibios_scan_root and pci_scan_bus
197  * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
198  * Registers found in the K8 northbridge
199  */
200 static int __init early_fill_mp_bus_info(void)
201 {
202         int i;
203         int j;
204         unsigned bus;
205         unsigned slot;
206         int found;
207         int node;
208         int link;
209         int def_node;
210         int def_link;
211         struct pci_root_info *info;
212         u32 reg;
213         struct resource *res;
214         size_t start;
215         size_t end;
216         struct res_range range[RANGE_NUM];
217         u64 val;
218         u32 address;
219
220 #ifdef CONFIG_NUMA
221         for (i = 0; i < BUS_NR; i++)
222                 mp_bus_to_node[i] = -1;
223 #endif
224
225         if (!early_pci_allowed())
226                 return -1;
227
228         found = 0;
229         for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
230                 u32 id;
231                 u16 device;
232                 u16 vendor;
233
234                 bus = pci_probes[i].bus;
235                 slot = pci_probes[i].slot;
236                 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
237
238                 vendor = id & 0xffff;
239                 device = (id>>16) & 0xffff;
240                 if (pci_probes[i].vendor == vendor &&
241                     pci_probes[i].device == device) {
242                         found = 1;
243                         break;
244                 }
245         }
246
247         if (!found)
248                 return 0;
249
250         pci_root_num = 0;
251         for (i = 0; i < 4; i++) {
252                 int min_bus;
253                 int max_bus;
254                 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
255
256                 /* Check if that register is enabled for bus range */
257                 if ((reg & 7) != 3)
258                         continue;
259
260                 min_bus = (reg >> 16) & 0xff;
261                 max_bus = (reg >> 24) & 0xff;
262                 node = (reg >> 4) & 0x07;
263 #ifdef CONFIG_NUMA
264                 for (j = min_bus; j <= max_bus; j++)
265                         mp_bus_to_node[j] = (unsigned char) node;
266 #endif
267                 link = (reg >> 8) & 0x03;
268
269                 info = &pci_root_info[pci_root_num];
270                 info->bus_min = min_bus;
271                 info->bus_max = max_bus;
272                 info->node = node;
273                 info->link = link;
274                 sprintf(info->name, "PCI Bus #%02x", min_bus);
275                 pci_root_num++;
276         }
277
278         /* get the default node and link for left over res */
279         reg = read_pci_config(bus, slot, 0, 0x60);
280         def_node = (reg >> 8) & 0x07;
281         reg = read_pci_config(bus, slot, 0, 0x64);
282         def_link = (reg >> 8) & 0x03;
283
284         memset(range, 0, sizeof(range));
285         range[0].end = 0xffff;
286         /* io port resource */
287         for (i = 0; i < 4; i++) {
288                 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
289                 if (!(reg & 3))
290                         continue;
291
292                 start = reg & 0xfff000;
293                 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
294                 node = reg & 0x07;
295                 link = (reg >> 4) & 0x03;
296                 end = (reg & 0xfff000) | 0xfff;
297
298                 /* find the position */
299                 for (j = 0; j < pci_root_num; j++) {
300                         info = &pci_root_info[j];
301                         if (info->node == node && info->link == link)
302                                 break;
303                 }
304                 if (j == pci_root_num)
305                         continue; /* not found */
306
307                 info = &pci_root_info[j];
308                 update_res(info, start, end, IORESOURCE_IO, 0);
309                 update_range(range, start, end);
310         }
311         /* add left over io port range to def node/link, [0, 0xffff] */
312         /* find the position */
313         for (j = 0; j < pci_root_num; j++) {
314                 info = &pci_root_info[j];
315                 if (info->node == def_node && info->link == def_link)
316                         break;
317         }
318         if (j < pci_root_num) {
319                 info = &pci_root_info[j];
320                 for (i = 0; i < RANGE_NUM; i++) {
321                         if (!range[i].end)
322                                 continue;
323
324                         update_res(info, range[i].start, range[i].end,
325                                    IORESOURCE_IO, 1);
326                 }
327         }
328
329         memset(range, 0, sizeof(range));
330         /* 0xfd00000000-0xffffffffff for HT */
331         /* 0xfc00000000-0xfcffffffff for Family 10h mmconfig*/
332         range[0].end = 0xfbffffffffULL;
333
334         /* need to take out [0, TOM) for RAM*/
335         address = MSR_K8_TOP_MEM1;
336         rdmsrl(address, val);
337         end = (val & 0xffffff8000000ULL);
338         printk(KERN_INFO "TOM: %016lx aka %ldM\n", end, end>>20);
339         if (end < (1ULL<<32))
340                 update_range(range, 0, end - 1);
341
342         /* mmio resource */
343         for (i = 0; i < 8; i++) {
344                 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
345                 if (!(reg & 3))
346                         continue;
347
348                 start = reg & 0xffffff00; /* 39:16 on 31:8*/
349                 start <<= 8;
350                 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
351                 node = reg & 0x07;
352                 link = (reg >> 4) & 0x03;
353                 end = (reg & 0xffffff00);
354                 end <<= 8;
355                 end |= 0xffff;
356
357                 /* find the position */
358                 for (j = 0; j < pci_root_num; j++) {
359                         info = &pci_root_info[j];
360                         if (info->node == node && info->link == link)
361                                 break;
362                 }
363                 if (j == pci_root_num)
364                         continue; /* not found */
365
366                 info = &pci_root_info[j];
367                 update_res(info, start, end, IORESOURCE_MEM, 0);
368                 update_range(range, start, end);
369         }
370
371         /* need to take out [4G, TOM2) for RAM*/
372         /* SYS_CFG */
373         address = MSR_K8_SYSCFG;
374         rdmsrl(address, val);
375         /* TOP_MEM2 is enabled? */
376         if (val & (1<<21)) {
377                 /* TOP_MEM2 */
378                 address = MSR_K8_TOP_MEM2;
379                 rdmsrl(address, val);
380                 end = (val & 0xffffff8000000ULL);
381                 printk(KERN_INFO "TOM2: %016lx aka %ldM\n", end, end>>20);
382                 update_range(range, 1ULL<<32, end - 1);
383         }
384
385         /*
386          * add left over mmio range to def node/link ?
387          * that is tricky, just record range in from start_min to 4G
388          */
389         for (j = 0; j < pci_root_num; j++) {
390                 info = &pci_root_info[j];
391                 if (info->node == def_node && info->link == def_link)
392                         break;
393         }
394         if (j < pci_root_num) {
395                 info = &pci_root_info[j];
396
397                 for (i = 0; i < RANGE_NUM; i++) {
398                         if (!range[i].end)
399                                 continue;
400
401                         update_res(info, range[i].start, range[i].end,
402                                    IORESOURCE_MEM, 1);
403                 }
404         }
405
406 #ifdef CONFIG_NUMA
407         for (i = 0; i < BUS_NR; i++) {
408                 node = mp_bus_to_node[i];
409                 if (node >= 0)
410                         printk(KERN_DEBUG "bus: %02x to node: %02x\n", i, node);
411         }
412 #endif
413
414         for (i = 0; i < pci_root_num; i++) {
415                 int res_num;
416                 int busnum;
417
418                 info = &pci_root_info[i];
419                 res_num = info->res_num;
420                 busnum = info->bus_min;
421                 printk(KERN_DEBUG "bus: [%02x,%02x] on node %x link %x\n",
422                        info->bus_min, info->bus_max, info->node, info->link);
423                 for (j = 0; j < res_num; j++) {
424                         res = &info->res[j];
425                         printk(KERN_DEBUG "bus: %02x index %x %s: [%llx, %llx]\n",
426                                busnum, j,
427                                (res->flags & IORESOURCE_IO)?"io port":"mmio",
428                                res->start, res->end);
429                 }
430         }
431
432         return 0;
433 }
434
435 postcore_initcall(early_fill_mp_bus_info);