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 },
33 static u64 __initdata fam10h_mmconf_start
;
34 static u64 __initdata fam10h_mmconf_end
;
35 static void __init
get_pci_mmcfg_amd_fam10h_range(void)
39 unsigned segn_busn_bits
;
41 /* assume all cpus from fam10h have mmconf */
42 if (boot_cpu_data
.x86
< 0x10)
45 address
= MSR_FAM10H_MMIO_CONF_BASE
;
48 /* mmconfig is not enable */
49 if (!(msr
& FAM10H_MMIO_CONF_ENABLE
))
52 base
= msr
& (FAM10H_MMIO_CONF_BASE_MASK
<<FAM10H_MMIO_CONF_BASE_SHIFT
);
54 segn_busn_bits
= (msr
>> FAM10H_MMIO_CONF_BUSRANGE_SHIFT
) &
55 FAM10H_MMIO_CONF_BUSRANGE_MASK
;
57 fam10h_mmconf_start
= base
;
58 fam10h_mmconf_end
= base
+ (1ULL<<(segn_busn_bits
+ 20)) - 1;
64 * early_fill_mp_bus_to_node()
65 * called before pcibios_scan_root and pci_scan_bus
66 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
67 * Registers found in the K8 northbridge
69 static int __init
early_fill_mp_bus_info(void)
79 struct pci_root_info
*info
;
84 struct range range
[RANGE_NUM
];
89 if (!early_pci_allowed())
93 for (i
= 0; i
< ARRAY_SIZE(pci_probes
); i
++) {
98 bus
= pci_probes
[i
].bus
;
99 slot
= pci_probes
[i
].slot
;
100 id
= read_pci_config(bus
, slot
, 0, PCI_VENDOR_ID
);
102 vendor
= id
& 0xffff;
103 device
= (id
>>16) & 0xffff;
104 if (pci_probes
[i
].vendor
== vendor
&&
105 pci_probes
[i
].device
== device
) {
115 for (i
= 0; i
< 4; i
++) {
118 reg
= read_pci_config(bus
, slot
, 1, 0xe0 + (i
<< 2));
120 /* Check if that register is enabled for bus range */
124 min_bus
= (reg
>> 16) & 0xff;
125 max_bus
= (reg
>> 24) & 0xff;
126 node
= (reg
>> 4) & 0x07;
128 for (j
= min_bus
; j
<= max_bus
; j
++)
129 set_mp_bus_to_node(j
, node
);
131 link
= (reg
>> 8) & 0x03;
133 info
= &pci_root_info
[pci_root_num
];
134 info
->bus_min
= min_bus
;
135 info
->bus_max
= max_bus
;
138 sprintf(info
->name
, "PCI Bus #%02x", min_bus
);
142 /* get the default node and link for left over res */
143 reg
= read_pci_config(bus
, slot
, 0, 0x60);
144 def_node
= (reg
>> 8) & 0x07;
145 reg
= read_pci_config(bus
, slot
, 0, 0x64);
146 def_link
= (reg
>> 8) & 0x03;
148 memset(range
, 0, sizeof(range
));
149 add_range(range
, RANGE_NUM
, 0, 0, 0xffff + 1);
150 /* io port resource */
151 for (i
= 0; i
< 4; i
++) {
152 reg
= read_pci_config(bus
, slot
, 1, 0xc0 + (i
<< 3));
156 start
= reg
& 0xfff000;
157 reg
= read_pci_config(bus
, slot
, 1, 0xc4 + (i
<< 3));
159 link
= (reg
>> 4) & 0x03;
160 end
= (reg
& 0xfff000) | 0xfff;
162 /* find the position */
163 for (j
= 0; j
< pci_root_num
; j
++) {
164 info
= &pci_root_info
[j
];
165 if (info
->node
== node
&& info
->link
== link
)
168 if (j
== pci_root_num
)
169 continue; /* not found */
171 info
= &pci_root_info
[j
];
172 printk(KERN_DEBUG
"node %d link %d: io port [%llx, %llx]\n",
173 node
, link
, start
, end
);
175 /* kernel only handle 16 bit only */
178 update_res(info
, start
, end
, IORESOURCE_IO
, 1);
179 subtract_range(range
, RANGE_NUM
, start
, end
+ 1);
181 /* add left over io port range to def node/link, [0, 0xffff] */
182 /* find the position */
183 for (j
= 0; j
< pci_root_num
; j
++) {
184 info
= &pci_root_info
[j
];
185 if (info
->node
== def_node
&& info
->link
== def_link
)
188 if (j
< pci_root_num
) {
189 info
= &pci_root_info
[j
];
190 for (i
= 0; i
< RANGE_NUM
; i
++) {
194 update_res(info
, range
[i
].start
, range
[i
].end
- 1,
199 memset(range
, 0, sizeof(range
));
200 /* 0xfd00000000-0xffffffffff for HT */
201 end
= cap_resource((0xfdULL
<<32) - 1);
203 add_range(range
, RANGE_NUM
, 0, 0, end
);
205 /* need to take out [0, TOM) for RAM*/
206 address
= MSR_K8_TOP_MEM1
;
207 rdmsrl(address
, val
);
208 end
= (val
& 0xffffff800000ULL
);
209 printk(KERN_INFO
"TOM: %016llx aka %lldM\n", end
, end
>>20);
210 if (end
< (1ULL<<32))
211 subtract_range(range
, RANGE_NUM
, 0, end
);
214 get_pci_mmcfg_amd_fam10h_range();
215 /* need to take out mmconf range */
216 if (fam10h_mmconf_end
) {
217 printk(KERN_DEBUG
"Fam 10h mmconf [%llx, %llx]\n", fam10h_mmconf_start
, fam10h_mmconf_end
);
218 subtract_range(range
, RANGE_NUM
, fam10h_mmconf_start
,
219 fam10h_mmconf_end
+ 1);
223 for (i
= 0; i
< 8; i
++) {
224 reg
= read_pci_config(bus
, slot
, 1, 0x80 + (i
<< 3));
228 start
= reg
& 0xffffff00; /* 39:16 on 31:8*/
230 reg
= read_pci_config(bus
, slot
, 1, 0x84 + (i
<< 3));
232 link
= (reg
>> 4) & 0x03;
233 end
= (reg
& 0xffffff00);
237 /* find the position */
238 for (j
= 0; j
< pci_root_num
; j
++) {
239 info
= &pci_root_info
[j
];
240 if (info
->node
== node
&& info
->link
== link
)
243 if (j
== pci_root_num
)
244 continue; /* not found */
246 info
= &pci_root_info
[j
];
248 printk(KERN_DEBUG
"node %d link %d: mmio [%llx, %llx]",
249 node
, link
, start
, end
);
251 * some sick allocation would have range overlap with fam10h
252 * mmconf range, so need to update start and end.
254 if (fam10h_mmconf_end
) {
257 if (start
>= fam10h_mmconf_start
&&
258 start
<= fam10h_mmconf_end
) {
259 start
= fam10h_mmconf_end
+ 1;
263 if (end
>= fam10h_mmconf_start
&&
264 end
<= fam10h_mmconf_end
) {
265 end
= fam10h_mmconf_start
- 1;
269 if (start
< fam10h_mmconf_start
&&
270 end
> fam10h_mmconf_end
) {
272 endx
= fam10h_mmconf_start
- 1;
273 update_res(info
, start
, endx
, IORESOURCE_MEM
, 0);
274 subtract_range(range
, RANGE_NUM
, start
,
276 printk(KERN_CONT
" ==> [%llx, %llx]", start
, endx
);
277 start
= fam10h_mmconf_end
+ 1;
282 printk(KERN_CONT
" %s [%llx, %llx]", endx
? "and" : "==>", start
, end
);
284 printk(KERN_CONT
"%s\n", endx
?"":" ==> none");
290 update_res(info
, cap_resource(start
), cap_resource(end
),
292 subtract_range(range
, RANGE_NUM
, start
, end
+ 1);
293 printk(KERN_CONT
"\n");
296 /* need to take out [4G, TOM2) for RAM*/
298 address
= MSR_K8_SYSCFG
;
299 rdmsrl(address
, val
);
300 /* TOP_MEM2 is enabled? */
303 address
= MSR_K8_TOP_MEM2
;
304 rdmsrl(address
, val
);
305 end
= (val
& 0xffffff800000ULL
);
306 printk(KERN_INFO
"TOM2: %016llx aka %lldM\n", end
, end
>>20);
307 subtract_range(range
, RANGE_NUM
, 1ULL<<32, end
);
311 * add left over mmio range to def node/link ?
312 * that is tricky, just record range in from start_min to 4G
314 for (j
= 0; j
< pci_root_num
; j
++) {
315 info
= &pci_root_info
[j
];
316 if (info
->node
== def_node
&& info
->link
== def_link
)
319 if (j
< pci_root_num
) {
320 info
= &pci_root_info
[j
];
322 for (i
= 0; i
< RANGE_NUM
; i
++) {
326 update_res(info
, cap_resource(range
[i
].start
),
327 cap_resource(range
[i
].end
- 1),
332 for (i
= 0; i
< pci_root_num
; i
++) {
336 info
= &pci_root_info
[i
];
337 res_num
= info
->res_num
;
338 busnum
= info
->bus_min
;
339 printk(KERN_DEBUG
"bus: [%02x, %02x] on node %x link %x\n",
340 info
->bus_min
, info
->bus_max
, info
->node
, info
->link
);
341 for (j
= 0; j
< res_num
; j
++) {
343 printk(KERN_DEBUG
"bus: %02x index %x %pR\n",
351 #define ENABLE_CF8_EXT_CFG (1ULL << 46)
353 static void __cpuinit
enable_pci_io_ecs(void *unused
)
356 rdmsrl(MSR_AMD64_NB_CFG
, reg
);
357 if (!(reg
& ENABLE_CF8_EXT_CFG
)) {
358 reg
|= ENABLE_CF8_EXT_CFG
;
359 wrmsrl(MSR_AMD64_NB_CFG
, reg
);
363 static int __cpuinit
amd_cpu_notify(struct notifier_block
*self
,
364 unsigned long action
, void *hcpu
)
366 int cpu
= (long)hcpu
;
369 case CPU_ONLINE_FROZEN
:
370 smp_call_function_single(cpu
, enable_pci_io_ecs
, NULL
, 0);
378 static struct notifier_block __cpuinitdata amd_cpu_notifier
= {
379 .notifier_call
= amd_cpu_notify
,
382 static void __init
pci_enable_pci_io_ecs(void)
387 for (n
= i
= 0; !n
&& amd_nb_bus_dev_ranges
[i
].dev_limit
; ++i
) {
388 u8 bus
= amd_nb_bus_dev_ranges
[i
].bus
;
389 u8 slot
= amd_nb_bus_dev_ranges
[i
].dev_base
;
390 u8 limit
= amd_nb_bus_dev_ranges
[i
].dev_limit
;
392 for (; slot
< limit
; ++slot
) {
393 u32 val
= read_pci_config(bus
, slot
, 3, 0);
395 if (!early_is_amd_nb(val
))
398 val
= read_pci_config(bus
, slot
, 3, 0x8c);
399 if (!(val
& (ENABLE_CF8_EXT_CFG
>> 32))) {
400 val
|= ENABLE_CF8_EXT_CFG
>> 32;
401 write_pci_config(bus
, slot
, 3, 0x8c, val
);
406 pr_info("Extended Config Space enabled on %u nodes\n", n
);
410 static int __init
pci_io_ecs_init(void)
414 /* assume all cpus from fam10h have IO ECS */
415 if (boot_cpu_data
.x86
< 0x10)
418 /* Try the PCI method first. */
419 if (early_pci_allowed())
420 pci_enable_pci_io_ecs();
422 register_cpu_notifier(&amd_cpu_notifier
);
423 for_each_online_cpu(cpu
)
424 amd_cpu_notify(&amd_cpu_notifier
, (unsigned long)CPU_ONLINE
,
426 pci_probe
|= PCI_HAS_IO_ECS
;
431 static int __init
amd_postcore_init(void)
433 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
)
436 early_fill_mp_bus_info();
442 postcore_initcall(amd_postcore_init
);