1 #include <linux/init.h>
3 #include <linux/topology.h>
5 #include <linux/range.h>
7 #include <asm/amd_nb.h>
8 #include <asm/pci_x86.h>
10 #include <asm/pci-direct.h>
15 * This discovers the pcibus <-> node mapping on AMD K8.
16 * also get peer root bus resource for io,mmio
19 struct pci_hostbridge_probe {
26 static 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 },
35 static struct pci_root_info __init *find_pci_root_info(int node, int link)
37 struct pci_root_info *info;
39 /* find the position */
40 list_for_each_entry(info, &pci_root_infos, list)
41 if (info->node == node && info->link == link)
48 * early_fill_mp_bus_to_node()
49 * called before pcibios_scan_root and pci_scan_bus
50 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
51 * Registers found in the K8 northbridge
53 static int __init early_fill_mp_bus_info(void)
62 struct pci_root_info *info;
66 struct range range[RANGE_NUM];
70 struct resource fam10h_mmconf_res, *fam10h_mmconf;
71 u64 fam10h_mmconf_start;
72 u64 fam10h_mmconf_end;
74 if (!early_pci_allowed())
78 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
83 bus = pci_probes[i].bus;
84 slot = pci_probes[i].slot;
85 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
88 device = (id>>16) & 0xffff;
89 if (pci_probes[i].vendor == vendor &&
90 pci_probes[i].device == device) {
99 for (i = 0; i < 4; i++) {
102 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
104 /* Check if that register is enabled for bus range */
108 min_bus = (reg >> 16) & 0xff;
109 max_bus = (reg >> 24) & 0xff;
110 node = (reg >> 4) & 0x07;
111 link = (reg >> 8) & 0x03;
113 info = alloc_pci_root_info(min_bus, max_bus, node, link);
116 /* get the default node and link for left over res */
117 reg = read_pci_config(bus, slot, 0, 0x60);
118 def_node = (reg >> 8) & 0x07;
119 reg = read_pci_config(bus, slot, 0, 0x64);
120 def_link = (reg >> 8) & 0x03;
122 memset(range, 0, sizeof(range));
123 add_range(range, RANGE_NUM, 0, 0, 0xffff + 1);
124 /* io port resource */
125 for (i = 0; i < 4; i++) {
126 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
130 start = reg & 0xfff000;
131 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
133 link = (reg >> 4) & 0x03;
134 end = (reg & 0xfff000) | 0xfff;
136 info = find_pci_root_info(node, link);
138 continue; /* not found */
140 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
141 node, link, start, end);
143 /* kernel only handle 16 bit only */
146 update_res(info, start, end, IORESOURCE_IO, 1);
147 subtract_range(range, RANGE_NUM, start, end + 1);
149 /* add left over io port range to def node/link, [0, 0xffff] */
150 /* find the position */
151 info = find_pci_root_info(def_node, def_link);
153 for (i = 0; i < RANGE_NUM; i++) {
157 update_res(info, range[i].start, range[i].end - 1,
162 memset(range, 0, sizeof(range));
163 /* 0xfd00000000-0xffffffffff for HT */
164 end = cap_resource((0xfdULL<<32) - 1);
166 add_range(range, RANGE_NUM, 0, 0, end);
168 /* need to take out [0, TOM) for RAM*/
169 address = MSR_K8_TOP_MEM1;
170 rdmsrl(address, val);
171 end = (val & 0xffffff800000ULL);
172 printk(KERN_INFO "TOM: %016llx aka %lldM\n", end, end>>20);
173 if (end < (1ULL<<32))
174 subtract_range(range, RANGE_NUM, 0, end);
177 fam10h_mmconf = amd_get_mmconfig_range(&fam10h_mmconf_res);
178 /* need to take out mmconf range */
180 printk(KERN_DEBUG "Fam 10h mmconf %pR\n", fam10h_mmconf);
181 fam10h_mmconf_start = fam10h_mmconf->start;
182 fam10h_mmconf_end = fam10h_mmconf->end;
183 subtract_range(range, RANGE_NUM, fam10h_mmconf_start,
184 fam10h_mmconf_end + 1);
186 fam10h_mmconf_start = 0;
187 fam10h_mmconf_end = 0;
191 for (i = 0; i < 8; i++) {
192 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
196 start = reg & 0xffffff00; /* 39:16 on 31:8*/
198 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
200 link = (reg >> 4) & 0x03;
201 end = (reg & 0xffffff00);
205 info = find_pci_root_info(node, link);
210 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
211 node, link, start, end);
213 * some sick allocation would have range overlap with fam10h
214 * mmconf range, so need to update start and end.
216 if (fam10h_mmconf_end) {
219 if (start >= fam10h_mmconf_start &&
220 start <= fam10h_mmconf_end) {
221 start = fam10h_mmconf_end + 1;
225 if (end >= fam10h_mmconf_start &&
226 end <= fam10h_mmconf_end) {
227 end = fam10h_mmconf_start - 1;
231 if (start < fam10h_mmconf_start &&
232 end > fam10h_mmconf_end) {
234 endx = fam10h_mmconf_start - 1;
235 update_res(info, start, endx, IORESOURCE_MEM, 0);
236 subtract_range(range, RANGE_NUM, start,
238 printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
239 start = fam10h_mmconf_end + 1;
244 printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
246 printk(KERN_CONT "%s\n", endx?"":" ==> none");
252 update_res(info, cap_resource(start), cap_resource(end),
254 subtract_range(range, RANGE_NUM, start, end + 1);
255 printk(KERN_CONT "\n");
258 /* need to take out [4G, TOM2) for RAM*/
260 address = MSR_K8_SYSCFG;
261 rdmsrl(address, val);
262 /* TOP_MEM2 is enabled? */
265 address = MSR_K8_TOP_MEM2;
266 rdmsrl(address, val);
267 end = (val & 0xffffff800000ULL);
268 printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
269 subtract_range(range, RANGE_NUM, 1ULL<<32, end);
273 * add left over mmio range to def node/link ?
274 * that is tricky, just record range in from start_min to 4G
276 info = find_pci_root_info(def_node, def_link);
278 for (i = 0; i < RANGE_NUM; i++) {
282 update_res(info, cap_resource(range[i].start),
283 cap_resource(range[i].end - 1),
288 list_for_each_entry(info, &pci_root_infos, list) {
290 struct pci_root_res *root_res;
292 busnum = info->busn.start;
293 printk(KERN_DEBUG "bus: %pR on node %x link %x\n",
294 &info->busn, info->node, info->link);
295 list_for_each_entry(root_res, &info->resources, list)
296 printk(KERN_DEBUG "bus: %02x %pR\n",
297 busnum, &root_res->res);
303 #define ENABLE_CF8_EXT_CFG (1ULL << 46)
305 static void enable_pci_io_ecs(void *unused)
308 rdmsrl(MSR_AMD64_NB_CFG, reg);
309 if (!(reg & ENABLE_CF8_EXT_CFG)) {
310 reg |= ENABLE_CF8_EXT_CFG;
311 wrmsrl(MSR_AMD64_NB_CFG, reg);
315 static int amd_cpu_notify(struct notifier_block *self, unsigned long action,
318 int cpu = (long)hcpu;
321 case CPU_ONLINE_FROZEN:
322 smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
330 static struct notifier_block amd_cpu_notifier = {
331 .notifier_call = amd_cpu_notify,
334 static void __init pci_enable_pci_io_ecs(void)
339 for (n = i = 0; !n && amd_nb_bus_dev_ranges[i].dev_limit; ++i) {
340 u8 bus = amd_nb_bus_dev_ranges[i].bus;
341 u8 slot = amd_nb_bus_dev_ranges[i].dev_base;
342 u8 limit = amd_nb_bus_dev_ranges[i].dev_limit;
344 for (; slot < limit; ++slot) {
345 u32 val = read_pci_config(bus, slot, 3, 0);
347 if (!early_is_amd_nb(val))
350 val = read_pci_config(bus, slot, 3, 0x8c);
351 if (!(val & (ENABLE_CF8_EXT_CFG >> 32))) {
352 val |= ENABLE_CF8_EXT_CFG >> 32;
353 write_pci_config(bus, slot, 3, 0x8c, val);
361 static int __init pci_io_ecs_init(void)
365 /* assume all cpus from fam10h have IO ECS */
366 if (boot_cpu_data.x86 < 0x10)
369 /* Try the PCI method first. */
370 if (early_pci_allowed())
371 pci_enable_pci_io_ecs();
373 cpu_notifier_register_begin();
374 for_each_online_cpu(cpu)
375 amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
377 __register_cpu_notifier(&amd_cpu_notifier);
378 cpu_notifier_register_done();
380 pci_probe |= PCI_HAS_IO_ECS;
385 static int __init amd_postcore_init(void)
387 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
390 early_fill_mp_bus_info();
396 postcore_initcall(amd_postcore_init);