2 * Shared support code for AMD K8 northbridges and derivates.
3 * Copyright 2006 Andi Kleen, SUSE Labs. Subject to GPLv2.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/types.h>
9 #include <linux/slab.h>
10 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/spinlock.h>
14 #include <asm/amd_nb.h>
16 static u32
*flush_words
;
18 const struct pci_device_id amd_nb_misc_ids
[] = {
19 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_K8_NB_MISC
) },
20 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_10H_NB_MISC
) },
21 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_15H_NB_F3
) },
22 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_15H_M10H_F3
) },
23 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_16H_NB_F3
) },
26 EXPORT_SYMBOL(amd_nb_misc_ids
);
28 static const struct pci_device_id amd_nb_link_ids
[] = {
29 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_15H_NB_F4
) },
30 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_16H_NB_F4
) },
34 const struct amd_nb_bus_dev_range amd_nb_bus_dev_ranges
[] __initconst
= {
41 struct amd_northbridge_info amd_northbridges
;
42 EXPORT_SYMBOL(amd_northbridges
);
44 static struct pci_dev
*next_northbridge(struct pci_dev
*dev
,
45 const struct pci_device_id
*ids
)
48 dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, dev
);
51 } while (!pci_match_id(ids
, dev
));
55 int amd_cache_northbridges(void)
58 struct amd_northbridge
*nb
;
59 struct pci_dev
*misc
, *link
;
65 while ((misc
= next_northbridge(misc
, amd_nb_misc_ids
)) != NULL
)
71 nb
= kzalloc(i
* sizeof(struct amd_northbridge
), GFP_KERNEL
);
75 amd_northbridges
.nb
= nb
;
76 amd_northbridges
.num
= i
;
79 for (i
= 0; i
!= amd_nb_num(); i
++) {
80 node_to_amd_nb(i
)->misc
= misc
=
81 next_northbridge(misc
, amd_nb_misc_ids
);
82 node_to_amd_nb(i
)->link
= link
=
83 next_northbridge(link
, amd_nb_link_ids
);
86 if (boot_cpu_data
.x86
== 0xf || boot_cpu_data
.x86
== 0x10 ||
87 boot_cpu_data
.x86
== 0x15)
88 amd_northbridges
.flags
|= AMD_NB_GART
;
91 * Some CPU families support L3 Cache Index Disable. There are some
92 * limitations because of E382 and E388 on family 0x10.
94 if (boot_cpu_data
.x86
== 0x10 &&
95 boot_cpu_data
.x86_model
>= 0x8 &&
96 (boot_cpu_data
.x86_model
> 0x9 ||
97 boot_cpu_data
.x86_mask
>= 0x1))
98 amd_northbridges
.flags
|= AMD_NB_L3_INDEX_DISABLE
;
100 if (boot_cpu_data
.x86
== 0x15)
101 amd_northbridges
.flags
|= AMD_NB_L3_INDEX_DISABLE
;
103 /* L3 cache partitioning is supported on family 0x15 */
104 if (boot_cpu_data
.x86
== 0x15)
105 amd_northbridges
.flags
|= AMD_NB_L3_PARTITIONING
;
109 EXPORT_SYMBOL_GPL(amd_cache_northbridges
);
112 * Ignores subdevice/subvendor but as far as I can figure out
113 * they're useless anyways
115 bool __init
early_is_amd_nb(u32 device
)
117 const struct pci_device_id
*id
;
118 u32 vendor
= device
& 0xffff;
121 for (id
= amd_nb_misc_ids
; id
->vendor
; id
++)
122 if (vendor
== id
->vendor
&& device
== id
->device
)
127 struct resource
*amd_get_mmconfig_range(struct resource
*res
)
131 unsigned segn_busn_bits
;
133 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
)
136 /* assume all cpus from fam10h have mmconfig */
137 if (boot_cpu_data
.x86
< 0x10)
140 address
= MSR_FAM10H_MMIO_CONF_BASE
;
141 rdmsrl(address
, msr
);
143 /* mmconfig is not enabled */
144 if (!(msr
& FAM10H_MMIO_CONF_ENABLE
))
147 base
= msr
& (FAM10H_MMIO_CONF_BASE_MASK
<<FAM10H_MMIO_CONF_BASE_SHIFT
);
149 segn_busn_bits
= (msr
>> FAM10H_MMIO_CONF_BUSRANGE_SHIFT
) &
150 FAM10H_MMIO_CONF_BUSRANGE_MASK
;
152 res
->flags
= IORESOURCE_MEM
;
154 res
->end
= base
+ (1ULL<<(segn_busn_bits
+ 20)) - 1;
158 int amd_get_subcaches(int cpu
)
160 struct pci_dev
*link
= node_to_amd_nb(amd_get_nb_id(cpu
))->link
;
164 if (!amd_nb_has_feature(AMD_NB_L3_PARTITIONING
))
167 pci_read_config_dword(link
, 0x1d4, &mask
);
169 cuid
= cpu_data(cpu
).compute_unit_id
;
170 return (mask
>> (4 * cuid
)) & 0xf;
173 int amd_set_subcaches(int cpu
, int mask
)
175 static unsigned int reset
, ban
;
176 struct amd_northbridge
*nb
= node_to_amd_nb(amd_get_nb_id(cpu
));
180 if (!amd_nb_has_feature(AMD_NB_L3_PARTITIONING
) || mask
> 0xf)
183 /* if necessary, collect reset state of L3 partitioning and BAN mode */
185 pci_read_config_dword(nb
->link
, 0x1d4, &reset
);
186 pci_read_config_dword(nb
->misc
, 0x1b8, &ban
);
190 /* deactivate BAN mode if any subcaches are to be disabled */
192 pci_read_config_dword(nb
->misc
, 0x1b8, ®
);
193 pci_write_config_dword(nb
->misc
, 0x1b8, reg
& ~0x180000);
196 cuid
= cpu_data(cpu
).compute_unit_id
;
198 mask
|= (0xf ^ (1 << cuid
)) << 26;
200 pci_write_config_dword(nb
->link
, 0x1d4, mask
);
202 /* reset BAN mode if L3 partitioning returned to reset state */
203 pci_read_config_dword(nb
->link
, 0x1d4, ®
);
205 pci_read_config_dword(nb
->misc
, 0x1b8, ®
);
207 pci_write_config_dword(nb
->misc
, 0x1b8, reg
| ban
);
213 static int amd_cache_gart(void)
217 if (!amd_nb_has_feature(AMD_NB_GART
))
220 flush_words
= kmalloc(amd_nb_num() * sizeof(u32
), GFP_KERNEL
);
222 amd_northbridges
.flags
&= ~AMD_NB_GART
;
226 for (i
= 0; i
!= amd_nb_num(); i
++)
227 pci_read_config_dword(node_to_amd_nb(i
)->misc
, 0x9c,
233 void amd_flush_garts(void)
237 static DEFINE_SPINLOCK(gart_lock
);
239 if (!amd_nb_has_feature(AMD_NB_GART
))
242 /* Avoid races between AGP and IOMMU. In theory it's not needed
243 but I'm not sure if the hardware won't lose flush requests
244 when another is pending. This whole thing is so expensive anyways
245 that it doesn't matter to serialize more. -AK */
246 spin_lock_irqsave(&gart_lock
, flags
);
248 for (i
= 0; i
< amd_nb_num(); i
++) {
249 pci_write_config_dword(node_to_amd_nb(i
)->misc
, 0x9c,
253 for (i
= 0; i
< amd_nb_num(); i
++) {
255 /* Make sure the hardware actually executed the flush*/
257 pci_read_config_dword(node_to_amd_nb(i
)->misc
,
264 spin_unlock_irqrestore(&gart_lock
, flags
);
266 pr_notice("nothing to flush?\n");
268 EXPORT_SYMBOL_GPL(amd_flush_garts
);
270 static __init
int init_amd_nbs(void)
274 err
= amd_cache_northbridges();
277 pr_notice("Cannot enumerate AMD northbridges\n");
279 if (amd_cache_gart() < 0)
280 pr_notice("Cannot initialize GART flush words, GART support disabled\n");
285 /* This has to go after the PCI subsystem */
286 fs_initcall(init_amd_nbs
);