2 * AMD CPU Microcode Update Driver for Linux
3 * Copyright (C) 2008 Advanced Micro Devices Inc.
5 * Author: Peter Oruba <peter.oruba@amd.com>
8 * Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
10 * This driver allows to upgrade microcode on AMD
11 * family 0x10 and 0x11 processors.
13 * Licensed unter the terms of the GNU General Public
14 * License version 2. See file COPYING for details.
17 #include <linux/capability.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/sched.h>
21 #include <linux/cpumask.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/miscdevice.h>
26 #include <linux/spinlock.h>
29 #include <linux/mutex.h>
30 #include <linux/cpu.h>
31 #include <linux/firmware.h>
32 #include <linux/platform_device.h>
33 #include <linux/pci.h>
34 #include <linux/pci_ids.h>
37 #include <asm/uaccess.h>
38 #include <asm/processor.h>
39 #include <asm/microcode.h>
41 MODULE_DESCRIPTION("AMD Microcode Update Driver");
42 MODULE_AUTHOR("Peter Oruba");
43 MODULE_LICENSE("GPL v2");
45 #define UCODE_MAGIC 0x00414d44
46 #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
47 #define UCODE_UCODE_TYPE 0x00000001
49 struct equiv_cpu_entry
{
50 unsigned int installed_cpu
;
51 unsigned int fixed_errata_mask
;
52 unsigned int fixed_errata_compare
;
53 unsigned int equiv_cpu
;
56 struct microcode_header_amd
{
57 unsigned int data_code
;
58 unsigned int patch_id
;
59 unsigned char mc_patch_data_id
[2];
60 unsigned char mc_patch_data_len
;
61 unsigned char init_flag
;
62 unsigned int mc_patch_data_checksum
;
63 unsigned int nb_dev_id
;
64 unsigned int sb_dev_id
;
66 unsigned char nb_rev_id
;
67 unsigned char sb_rev_id
;
68 unsigned char bios_api_rev
;
69 unsigned char reserved1
[3];
70 unsigned int match_reg
[8];
73 struct microcode_amd
{
74 struct microcode_header_amd hdr
;
78 #define UCODE_MAX_SIZE (2048)
79 #define DEFAULT_UCODE_DATASIZE (896)
80 #define MC_HEADER_SIZE (sizeof(struct microcode_header_amd))
81 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
82 #define DWSIZE (sizeof(u32))
83 /* For now we support a fixed ucode total size only */
84 #define get_totalsize(mc) \
85 ((((struct microcode_amd *)mc)->hdr.mc_patch_data_len * 28) \
88 /* serialize access to the physical write */
89 static DEFINE_SPINLOCK(microcode_update_lock
);
91 static struct equiv_cpu_entry
*equiv_cpu_table
;
93 static int collect_cpu_info_amd(int cpu
, struct cpu_signature
*csig
)
95 struct cpuinfo_x86
*c
= &cpu_data(cpu
);
97 memset(csig
, 0, sizeof(*csig
));
99 if (c
->x86_vendor
!= X86_VENDOR_AMD
|| c
->x86
< 0x10) {
100 printk(KERN_ERR
"microcode: CPU%d not a capable AMD processor\n",
105 asm volatile("movl %1, %%ecx; rdmsr"
107 : "i" (0x0000008B) : "ecx");
109 printk(KERN_INFO
"microcode: collect_cpu_info_amd : patch_id=0x%x\n",
115 static int get_matching_microcode(int cpu
, void *mc
, int rev
)
117 struct microcode_header_amd
*mc_header
= mc
;
118 struct pci_dev
*nb_pci_dev
, *sb_pci_dev
;
119 unsigned int current_cpu_id
;
120 unsigned int equiv_cpu_id
= 0x00;
123 BUG_ON(equiv_cpu_table
== NULL
);
124 current_cpu_id
= cpuid_eax(0x00000001);
126 while (equiv_cpu_table
[i
].installed_cpu
!= 0) {
127 if (current_cpu_id
== equiv_cpu_table
[i
].installed_cpu
) {
128 equiv_cpu_id
= equiv_cpu_table
[i
].equiv_cpu
& 0xffff;
135 printk(KERN_ERR
"microcode: CPU%d cpu_id "
136 "not found in equivalent cpu table \n", cpu
);
140 if (mc_header
->processor_rev_id
!= equiv_cpu_id
) {
141 printk(KERN_ERR
"microcode: CPU%d patch does not match "
142 "(processor_rev_id: %x, eqiv_cpu_id: %x)\n",
143 cpu
, mc_header
->processor_rev_id
, equiv_cpu_id
);
147 /* ucode may be northbridge specific */
148 if (mc_header
->nb_dev_id
) {
149 nb_pci_dev
= pci_get_device(PCI_VENDOR_ID_AMD
,
150 (mc_header
->nb_dev_id
& 0xff),
153 (mc_header
->nb_rev_id
!= nb_pci_dev
->revision
)) {
154 printk(KERN_ERR
"microcode: CPU%d NB mismatch \n", cpu
);
155 pci_dev_put(nb_pci_dev
);
158 pci_dev_put(nb_pci_dev
);
161 /* ucode may be southbridge specific */
162 if (mc_header
->sb_dev_id
) {
163 sb_pci_dev
= pci_get_device(PCI_VENDOR_ID_AMD
,
164 (mc_header
->sb_dev_id
& 0xff),
167 (mc_header
->sb_rev_id
!= sb_pci_dev
->revision
)) {
168 printk(KERN_ERR
"microcode: CPU%d SB mismatch \n", cpu
);
169 pci_dev_put(sb_pci_dev
);
172 pci_dev_put(sb_pci_dev
);
175 if (mc_header
->patch_id
<= rev
)
181 static void apply_microcode_amd(int cpu
)
184 unsigned int eax
, edx
;
186 int cpu_num
= raw_smp_processor_id();
187 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu_num
;
188 struct microcode_amd
*mc_amd
= uci
->mc
;
191 /* We should bind the task to the CPU */
192 BUG_ON(cpu_num
!= cpu
);
197 spin_lock_irqsave(µcode_update_lock
, flags
);
199 addr
= (unsigned long)&mc_amd
->hdr
.data_code
;
200 edx
= (unsigned int)(((unsigned long)upper_32_bits(addr
)));
201 eax
= (unsigned int)(((unsigned long)lower_32_bits(addr
)));
203 asm volatile("movl %0, %%ecx; wrmsr" :
204 : "i" (0xc0010020), "a" (eax
), "d" (edx
) : "ecx");
206 /* get patch id after patching */
207 asm volatile("movl %1, %%ecx; rdmsr"
209 : "i" (0x0000008B) : "ecx");
211 spin_unlock_irqrestore(µcode_update_lock
, flags
);
213 /* check current patch id and patch's id for match */
214 if (rev
!= mc_amd
->hdr
.patch_id
) {
215 printk(KERN_ERR
"microcode: CPU%d update from revision "
216 "0x%x to 0x%x failed\n", cpu_num
,
217 mc_amd
->hdr
.patch_id
, rev
);
221 printk(KERN_INFO
"microcode: CPU%d updated from revision "
223 cpu_num
, uci
->cpu_sig
.rev
, mc_amd
->hdr
.patch_id
);
225 uci
->cpu_sig
.rev
= rev
;
228 static void * get_next_ucode(u8
*buf
, unsigned int size
,
229 int (*get_ucode_data
)(void *, const void *, size_t),
230 unsigned int *mc_size
)
232 unsigned int total_size
;
233 #define UCODE_CONTAINER_SECTION_HDR 8
234 u8 section_hdr
[UCODE_CONTAINER_SECTION_HDR
];
237 if (get_ucode_data(section_hdr
, buf
, UCODE_CONTAINER_SECTION_HDR
))
240 if (section_hdr
[0] != UCODE_UCODE_TYPE
) {
241 printk(KERN_ERR
"microcode: error! "
242 "Wrong microcode payload type field\n");
246 total_size
= (unsigned long) (section_hdr
[4] + (section_hdr
[5] << 8));
248 printk(KERN_INFO
"microcode: size %u, total_size %u\n",
251 if (total_size
> size
|| total_size
> UCODE_MAX_SIZE
) {
252 printk(KERN_ERR
"microcode: error! Bad data in microcode data file\n");
256 mc
= vmalloc(UCODE_MAX_SIZE
);
258 memset(mc
, 0, UCODE_MAX_SIZE
);
259 if (get_ucode_data(mc
, buf
+ UCODE_CONTAINER_SECTION_HDR
, total_size
)) {
263 *mc_size
= total_size
+ UCODE_CONTAINER_SECTION_HDR
;
265 #undef UCODE_CONTAINER_SECTION_HDR
270 static int install_equiv_cpu_table(u8
*buf
,
271 int (*get_ucode_data
)(void *, const void *, size_t))
273 #define UCODE_CONTAINER_HEADER_SIZE 12
274 u8
*container_hdr
[UCODE_CONTAINER_HEADER_SIZE
];
275 unsigned int *buf_pos
= (unsigned int *)container_hdr
;
278 if (get_ucode_data(&container_hdr
, buf
, UCODE_CONTAINER_HEADER_SIZE
))
283 if (buf_pos
[1] != UCODE_EQUIV_CPU_TABLE_TYPE
|| !size
) {
284 printk(KERN_ERR
"microcode: error! "
285 "Wrong microcode equivalnet cpu table\n");
289 equiv_cpu_table
= (struct equiv_cpu_entry
*) vmalloc(size
);
290 if (!equiv_cpu_table
) {
291 printk(KERN_ERR
"microcode: error, can't allocate memory for equiv CPU table\n");
295 buf
+= UCODE_CONTAINER_HEADER_SIZE
;
296 if (get_ucode_data(equiv_cpu_table
, buf
, size
)) {
297 vfree(equiv_cpu_table
);
301 return size
+ UCODE_CONTAINER_HEADER_SIZE
; /* add header length */
302 #undef UCODE_CONTAINER_HEADER_SIZE
305 static void free_equiv_cpu_table(void)
307 if (equiv_cpu_table
) {
308 vfree(equiv_cpu_table
);
309 equiv_cpu_table
= NULL
;
313 static int generic_load_microcode(int cpu
, void *data
, size_t size
,
314 int (*get_ucode_data
)(void *, const void *, size_t))
316 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
317 u8
*ucode_ptr
= data
, *new_mc
= NULL
, *mc
;
318 int new_rev
= uci
->cpu_sig
.rev
;
319 unsigned int leftover
;
320 unsigned long offset
;
322 offset
= install_equiv_cpu_table(ucode_ptr
, get_ucode_data
);
324 printk(KERN_ERR
"microcode: installing equivalent cpu table failed\n");
329 leftover
= size
- offset
;
332 unsigned int uninitialized_var(mc_size
);
333 struct microcode_header_amd
*mc_header
;
335 mc
= get_next_ucode(ucode_ptr
, leftover
, get_ucode_data
, &mc_size
);
339 mc_header
= (struct microcode_header_amd
*)mc
;
340 if (get_matching_microcode(cpu
, mc
, new_rev
)) {
343 new_rev
= mc_header
->patch_id
;
348 ucode_ptr
+= mc_size
;
357 pr_debug("microcode: CPU%d found a matching microcode update with"
358 " version 0x%x (current=0x%x)\n",
359 cpu
, new_rev
, uci
->cpu_sig
.rev
);
364 free_equiv_cpu_table();
366 return (int)leftover
;
369 static int get_ucode_fw(void *to
, const void *from
, size_t n
)
375 static int request_microcode_fw(int cpu
, struct device
*device
)
377 const char *fw_name
= "amd-ucode/microcode_amd.bin";
378 const struct firmware
*firmware
;
381 /* We should bind the task to the CPU */
382 BUG_ON(cpu
!= raw_smp_processor_id());
384 ret
= request_firmware(&firmware
, fw_name
, device
);
386 printk(KERN_ERR
"microcode: ucode data file %s load failed\n", fw_name
);
390 ret
= generic_load_microcode(cpu
, (void*)firmware
->data
, firmware
->size
,
393 release_firmware(firmware
);
398 static int request_microcode_user(int cpu
, const void __user
*buf
, size_t size
)
400 printk(KERN_WARNING
"microcode: AMD microcode update via /dev/cpu/microcode"
401 "is not supported\n");
405 static void microcode_fini_cpu_amd(int cpu
)
407 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
413 static struct microcode_ops microcode_amd_ops
= {
414 .request_microcode_user
= request_microcode_user
,
415 .request_microcode_fw
= request_microcode_fw
,
416 .collect_cpu_info
= collect_cpu_info_amd
,
417 .apply_microcode
= apply_microcode_amd
,
418 .microcode_fini_cpu
= microcode_fini_cpu_amd
,
421 struct microcode_ops
* __init
init_amd_microcode(void)
423 return µcode_amd_ops
;