blob: 5aed49bff05817aad85712e280f2913bd5a883a1 [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
Yinghai Lu0f1103e2012-05-01 17:25:18 -060047static void __init set_mp_bus_range_to_node(int min_bus, int max_bus, int node)
48{
49#ifdef CONFIG_NUMA
50 int j;
51
52 for (j = min_bus; j <= max_bus; j++)
53 set_mp_bus_to_node(j, node);
54#endif
55}
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/**
Yinghai Lu871d5f82008-02-19 03:20:09 -080057 * early_fill_mp_bus_to_node()
58 * called before pcibios_scan_root and pci_scan_bus
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
60 * Registers found in the K8 northbridge
61 */
Yinghai Lu30a18d62008-02-19 03:21:20 -080062static int __init early_fill_mp_bus_info(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Yinghai Lu30a18d62008-02-19 03:21:20 -080064 int i;
Yinghai Lu30a18d62008-02-19 03:21:20 -080065 unsigned bus;
Yinghai Lu871d5f82008-02-19 03:20:09 -080066 unsigned slot;
Yinghai Lu35ddd062008-02-19 03:15:08 -080067 int node;
Yinghai Lu30a18d62008-02-19 03:21:20 -080068 int link;
69 int def_node;
70 int def_link;
71 struct pci_root_info *info;
72 u32 reg;
Yinghai Lu97445c32010-02-10 01:20:10 -080073 u64 start;
74 u64 end;
Yinghai Lu27811d82010-02-10 01:20:07 -080075 struct range range[RANGE_NUM];
Yinghai Lu30a18d62008-02-19 03:21:20 -080076 u64 val;
77 u32 address;
Yinghai Lu3e3da002010-02-10 01:20:09 -080078 bool found;
Bjorn Helgaas24d25db2012-01-05 14:27:19 -070079 struct resource fam10h_mmconf_res, *fam10h_mmconf;
80 u64 fam10h_mmconf_start;
81 u64 fam10h_mmconf_end;
Yinghai Lu35ddd062008-02-19 03:15:08 -080082
Yinghai Lu871d5f82008-02-19 03:20:09 -080083 if (!early_pci_allowed())
84 return -1;
85
Yinghai Lu3e3da002010-02-10 01:20:09 -080086 found = false;
Yinghai Lu30a18d62008-02-19 03:21:20 -080087 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
88 u32 id;
89 u16 device;
90 u16 vendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Yinghai Lu30a18d62008-02-19 03:21:20 -080092 bus = pci_probes[i].bus;
93 slot = pci_probes[i].slot;
94 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
Yinghai Lu35ddd062008-02-19 03:15:08 -080095
Yinghai Lu30a18d62008-02-19 03:21:20 -080096 vendor = id & 0xffff;
97 device = (id>>16) & 0xffff;
98 if (pci_probes[i].vendor == vendor &&
99 pci_probes[i].device == device) {
Yinghai Lu3e3da002010-02-10 01:20:09 -0800100 found = true;
Yinghai Lu30a18d62008-02-19 03:21:20 -0800101 break;
102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
Yinghai Lu3e3da002010-02-10 01:20:09 -0800105 if (!found)
Yinghai Lu30a18d62008-02-19 03:21:20 -0800106 return 0;
107
Yinghai Lu30a18d62008-02-19 03:21:20 -0800108 for (i = 0; i < 4; i++) {
109 int min_bus;
110 int max_bus;
111 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
112
113 /* Check if that register is enabled for bus range */
114 if ((reg & 7) != 3)
115 continue;
116
117 min_bus = (reg >> 16) & 0xff;
118 max_bus = (reg >> 24) & 0xff;
119 node = (reg >> 4) & 0x07;
Yinghai Lu0f1103e2012-05-01 17:25:18 -0600120 set_mp_bus_range_to_node(min_bus, max_bus, node);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800121 link = (reg >> 8) & 0x03;
122
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700123 info = alloc_pci_root_info(min_bus, max_bus, node, link);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800124 sprintf(info->name, "PCI Bus #%02x", min_bus);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800125 }
126
127 /* get the default node and link for left over res */
128 reg = read_pci_config(bus, slot, 0, 0x60);
129 def_node = (reg >> 8) & 0x07;
130 reg = read_pci_config(bus, slot, 0, 0x64);
131 def_link = (reg >> 8) & 0x03;
132
133 memset(range, 0, sizeof(range));
Yinghai Lue9a00642010-02-10 01:20:13 -0800134 add_range(range, RANGE_NUM, 0, 0, 0xffff + 1);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800135 /* io port resource */
136 for (i = 0; i < 4; i++) {
137 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
138 if (!(reg & 3))
139 continue;
140
141 start = reg & 0xfff000;
142 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
143 node = reg & 0x07;
144 link = (reg >> 4) & 0x03;
145 end = (reg & 0xfff000) | 0xfff;
146
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700147 info = find_pci_root_info(node, link);
148 if (!info)
Yinghai Lu30a18d62008-02-19 03:21:20 -0800149 continue; /* not found */
150
Yinghai Lu6e184f22008-03-06 01:15:31 -0800151 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
Yinghai Lu97445c32010-02-10 01:20:10 -0800152 node, link, start, end);
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700153
154 /* kernel only handle 16 bit only */
155 if (end > 0xffff)
156 end = 0xffff;
157 update_res(info, start, end, IORESOURCE_IO, 1);
Yinghai Lue9a00642010-02-10 01:20:13 -0800158 subtract_range(range, RANGE_NUM, start, end + 1);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800159 }
160 /* add left over io port range to def node/link, [0, 0xffff] */
161 /* find the position */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700162 info = find_pci_root_info(def_node, def_link);
163 if (info) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800164 for (i = 0; i < RANGE_NUM; i++) {
165 if (!range[i].end)
166 continue;
167
Yinghai Lue9a00642010-02-10 01:20:13 -0800168 update_res(info, range[i].start, range[i].end - 1,
Yinghai Lu30a18d62008-02-19 03:21:20 -0800169 IORESOURCE_IO, 1);
170 }
171 }
172
173 memset(range, 0, sizeof(range));
174 /* 0xfd00000000-0xffffffffff for HT */
Yinghai Lue9a00642010-02-10 01:20:13 -0800175 end = cap_resource((0xfdULL<<32) - 1);
176 end++;
177 add_range(range, RANGE_NUM, 0, 0, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800178
179 /* need to take out [0, TOM) for RAM*/
180 address = MSR_K8_TOP_MEM1;
181 rdmsrl(address, val);
Yinghai Lu8004dd92008-05-12 17:40:39 -0700182 end = (val & 0xffffff800000ULL);
Yinghai Lu97445c32010-02-10 01:20:10 -0800183 printk(KERN_INFO "TOM: %016llx aka %lldM\n", end, end>>20);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800184 if (end < (1ULL<<32))
Yinghai Lue9a00642010-02-10 01:20:13 -0800185 subtract_range(range, RANGE_NUM, 0, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800186
Yinghai Lu6e184f22008-03-06 01:15:31 -0800187 /* get mmconfig */
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700188 fam10h_mmconf = amd_get_mmconfig_range(&fam10h_mmconf_res);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800189 /* need to take out mmconf range */
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700190 if (fam10h_mmconf) {
191 printk(KERN_DEBUG "Fam 10h mmconf %pR\n", fam10h_mmconf);
192 fam10h_mmconf_start = fam10h_mmconf->start;
193 fam10h_mmconf_end = fam10h_mmconf->end;
Yinghai Lue9a00642010-02-10 01:20:13 -0800194 subtract_range(range, RANGE_NUM, fam10h_mmconf_start,
195 fam10h_mmconf_end + 1);
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700196 } else {
197 fam10h_mmconf_start = 0;
198 fam10h_mmconf_end = 0;
Yinghai Lu6e184f22008-03-06 01:15:31 -0800199 }
200
Yinghai Lu30a18d62008-02-19 03:21:20 -0800201 /* mmio resource */
202 for (i = 0; i < 8; i++) {
203 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
204 if (!(reg & 3))
205 continue;
206
207 start = reg & 0xffffff00; /* 39:16 on 31:8*/
208 start <<= 8;
209 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
210 node = reg & 0x07;
211 link = (reg >> 4) & 0x03;
212 end = (reg & 0xffffff00);
213 end <<= 8;
214 end |= 0xffff;
215
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700216 info = find_pci_root_info(node, link);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800217
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700218 if (!info)
219 continue;
Yinghai Lu6e184f22008-03-06 01:15:31 -0800220
221 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
Yinghai Lu97445c32010-02-10 01:20:10 -0800222 node, link, start, end);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800223 /*
224 * some sick allocation would have range overlap with fam10h
225 * mmconf range, so need to update start and end.
226 */
227 if (fam10h_mmconf_end) {
228 int changed = 0;
229 u64 endx = 0;
230 if (start >= fam10h_mmconf_start &&
231 start <= fam10h_mmconf_end) {
232 start = fam10h_mmconf_end + 1;
233 changed = 1;
234 }
235
236 if (end >= fam10h_mmconf_start &&
237 end <= fam10h_mmconf_end) {
238 end = fam10h_mmconf_start - 1;
239 changed = 1;
240 }
241
242 if (start < fam10h_mmconf_start &&
243 end > fam10h_mmconf_end) {
244 /* we got a hole */
245 endx = fam10h_mmconf_start - 1;
246 update_res(info, start, endx, IORESOURCE_MEM, 0);
Yinghai Lue9a00642010-02-10 01:20:13 -0800247 subtract_range(range, RANGE_NUM, start,
248 endx + 1);
Yinghai Lu97445c32010-02-10 01:20:10 -0800249 printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800250 start = fam10h_mmconf_end + 1;
251 changed = 1;
252 }
253 if (changed) {
254 if (start <= end) {
Yinghai Lu97445c32010-02-10 01:20:10 -0800255 printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800256 } else {
257 printk(KERN_CONT "%s\n", endx?"":" ==> none");
258 continue;
259 }
260 }
261 }
262
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -0800263 update_res(info, cap_resource(start), cap_resource(end),
264 IORESOURCE_MEM, 1);
Yinghai Lue9a00642010-02-10 01:20:13 -0800265 subtract_range(range, RANGE_NUM, start, end + 1);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800266 printk(KERN_CONT "\n");
Yinghai Lu30a18d62008-02-19 03:21:20 -0800267 }
268
269 /* need to take out [4G, TOM2) for RAM*/
270 /* SYS_CFG */
271 address = MSR_K8_SYSCFG;
272 rdmsrl(address, val);
273 /* TOP_MEM2 is enabled? */
274 if (val & (1<<21)) {
275 /* TOP_MEM2 */
276 address = MSR_K8_TOP_MEM2;
277 rdmsrl(address, val);
Yinghai Lu8004dd92008-05-12 17:40:39 -0700278 end = (val & 0xffffff800000ULL);
Yinghai Lu97445c32010-02-10 01:20:10 -0800279 printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
Yinghai Lue9a00642010-02-10 01:20:13 -0800280 subtract_range(range, RANGE_NUM, 1ULL<<32, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800281 }
282
283 /*
284 * add left over mmio range to def node/link ?
285 * that is tricky, just record range in from start_min to 4G
286 */
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700287 info = find_pci_root_info(def_node, def_link);
288 if (info) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800289 for (i = 0; i < RANGE_NUM; i++) {
290 if (!range[i].end)
291 continue;
292
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -0800293 update_res(info, cap_resource(range[i].start),
Yinghai Lue9a00642010-02-10 01:20:13 -0800294 cap_resource(range[i].end - 1),
Yinghai Lu30a18d62008-02-19 03:21:20 -0800295 IORESOURCE_MEM, 1);
296 }
297 }
298
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700299 list_for_each_entry(info, &pci_root_infos, list) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800300 int busnum;
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700301 struct pci_root_res *root_res;
Yinghai Lu30a18d62008-02-19 03:21:20 -0800302
Yinghai Lu30a18d62008-02-19 03:21:20 -0800303 busnum = info->bus_min;
Yinghai Lu99935a72009-10-04 21:54:24 -0700304 printk(KERN_DEBUG "bus: [%02x, %02x] on node %x link %x\n",
Yinghai Lu30a18d62008-02-19 03:21:20 -0800305 info->bus_min, info->bus_max, info->node, info->link);
Yinghai Lud28e5ac2012-04-02 18:31:54 -0700306 list_for_each_entry(root_res, &info->resources, list)
307 printk(KERN_DEBUG "bus: %02x %pR\n",
308 busnum, &root_res->res);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800309 }
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 0;
312}
313
Robert Richter3a27dd12008-06-12 20:19:23 +0200314#define ENABLE_CF8_EXT_CFG (1ULL << 46)
315
Jan Beulich691269f2011-02-09 08:26:53 +0000316static void __cpuinit enable_pci_io_ecs(void *unused)
Robert Richter3a27dd12008-06-12 20:19:23 +0200317{
318 u64 reg;
319 rdmsrl(MSR_AMD64_NB_CFG, reg);
320 if (!(reg & ENABLE_CF8_EXT_CFG)) {
321 reg |= ENABLE_CF8_EXT_CFG;
322 wrmsrl(MSR_AMD64_NB_CFG, reg);
323 }
324}
325
Robert Richter91ede002008-08-22 20:23:38 +0200326static int __cpuinit amd_cpu_notify(struct notifier_block *self,
327 unsigned long action, void *hcpu)
Robert Richter3a27dd12008-06-12 20:19:23 +0200328{
Robert Richter91ede002008-08-22 20:23:38 +0200329 int cpu = (long)hcpu;
Robert Richtered217632008-08-22 20:23:38 +0200330 switch (action) {
Robert Richter91ede002008-08-22 20:23:38 +0200331 case CPU_ONLINE:
332 case CPU_ONLINE_FROZEN:
333 smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
334 break;
335 default:
336 break;
337 }
338 return NOTIFY_OK;
339}
340
341static struct notifier_block __cpuinitdata amd_cpu_notifier = {
342 .notifier_call = amd_cpu_notify,
343};
344
Jan Beulich24d9b702011-01-10 16:20:23 +0000345static void __init pci_enable_pci_io_ecs(void)
346{
347#ifdef CONFIG_AMD_NB
348 unsigned int i, n;
349
350 for (n = i = 0; !n && amd_nb_bus_dev_ranges[i].dev_limit; ++i) {
351 u8 bus = amd_nb_bus_dev_ranges[i].bus;
352 u8 slot = amd_nb_bus_dev_ranges[i].dev_base;
353 u8 limit = amd_nb_bus_dev_ranges[i].dev_limit;
354
355 for (; slot < limit; ++slot) {
356 u32 val = read_pci_config(bus, slot, 3, 0);
357
358 if (!early_is_amd_nb(val))
359 continue;
360
361 val = read_pci_config(bus, slot, 3, 0x8c);
362 if (!(val & (ENABLE_CF8_EXT_CFG >> 32))) {
363 val |= ENABLE_CF8_EXT_CFG >> 32;
364 write_pci_config(bus, slot, 3, 0x8c, val);
365 }
366 ++n;
367 }
368 }
Jan Beulich24d9b702011-01-10 16:20:23 +0000369#endif
370}
371
Robert Richter91ede002008-08-22 20:23:38 +0200372static int __init pci_io_ecs_init(void)
373{
374 int cpu;
375
Robert Richter3a27dd12008-06-12 20:19:23 +0200376 /* assume all cpus from fam10h have IO ECS */
377 if (boot_cpu_data.x86 < 0x10)
378 return 0;
Robert Richter91ede002008-08-22 20:23:38 +0200379
Jan Beulich24d9b702011-01-10 16:20:23 +0000380 /* Try the PCI method first. */
381 if (early_pci_allowed())
382 pci_enable_pci_io_ecs();
383
Robert Richter91ede002008-08-22 20:23:38 +0200384 register_cpu_notifier(&amd_cpu_notifier);
385 for_each_online_cpu(cpu)
386 amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
387 (void *)(long)cpu);
Robert Richter3a27dd12008-06-12 20:19:23 +0200388 pci_probe |= PCI_HAS_IO_ECS;
Robert Richter91ede002008-08-22 20:23:38 +0200389
Robert Richter3a27dd12008-06-12 20:19:23 +0200390 return 0;
391}
392
Robert Richter9b4e27b2008-08-22 20:23:37 +0200393static int __init amd_postcore_init(void)
394{
395 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
396 return 0;
397
398 early_fill_mp_bus_info();
Robert Richter91ede002008-08-22 20:23:38 +0200399 pci_io_ecs_init();
Robert Richter9b4e27b2008-08-22 20:23:37 +0200400
401 return 0;
402}
403
404postcore_initcall(amd_postcore_init);