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 under 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>
35 #include <linux/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
{
51 u32 fixed_errata_mask
;
52 u32 fixed_errata_compare
;
55 } __attribute__((packed
));
57 struct microcode_header_amd
{
63 u32 mc_patch_data_checksum
;
72 } __attribute__((packed
));
74 struct microcode_amd
{
75 struct microcode_header_amd hdr
;
79 #define UCODE_MAX_SIZE 2048
80 #define UCODE_CONTAINER_SECTION_HDR 8
81 #define UCODE_CONTAINER_HEADER_SIZE 12
83 /* serialize access to the physical write */
84 static DEFINE_SPINLOCK(microcode_update_lock
);
86 static struct equiv_cpu_entry
*equiv_cpu_table
;
88 static int collect_cpu_info_amd(int cpu
, struct cpu_signature
*csig
)
90 struct cpuinfo_x86
*c
= &cpu_data(cpu
);
93 memset(csig
, 0, sizeof(*csig
));
94 if (c
->x86_vendor
!= X86_VENDOR_AMD
|| c
->x86
< 0x10) {
95 printk(KERN_WARNING
"microcode: CPU%d: AMD CPU family 0x%x not "
96 "supported\n", cpu
, c
->x86
);
99 rdmsr(MSR_AMD64_PATCH_LEVEL
, csig
->rev
, dummy
);
100 printk(KERN_INFO
"microcode: CPU%d: patch_level=0x%x\n", cpu
, csig
->rev
);
104 static int get_matching_microcode(int cpu
, void *mc
, int rev
)
106 struct microcode_header_amd
*mc_header
= mc
;
107 unsigned int current_cpu_id
;
108 u16 equiv_cpu_id
= 0;
111 BUG_ON(equiv_cpu_table
== NULL
);
112 current_cpu_id
= cpuid_eax(0x00000001);
114 while (equiv_cpu_table
[i
].installed_cpu
!= 0) {
115 if (current_cpu_id
== equiv_cpu_table
[i
].installed_cpu
) {
116 equiv_cpu_id
= equiv_cpu_table
[i
].equiv_cpu
;
123 printk(KERN_WARNING
"microcode: CPU%d: cpu revision "
124 "not listed in equivalent cpu table\n", cpu
);
128 if (mc_header
->processor_rev_id
!= equiv_cpu_id
) {
129 printk(KERN_ERR
"microcode: CPU%d: patch mismatch "
130 "(processor_rev_id: %x, equiv_cpu_id: %x)\n",
131 cpu
, mc_header
->processor_rev_id
, equiv_cpu_id
);
135 /* ucode might be chipset specific -- currently we don't support this */
136 if (mc_header
->nb_dev_id
|| mc_header
->sb_dev_id
) {
137 printk(KERN_ERR
"microcode: CPU%d: loading of chipset "
138 "specific code not yet supported\n", cpu
);
142 if (mc_header
->patch_id
<= rev
)
148 static void apply_microcode_amd(int cpu
)
152 int cpu_num
= raw_smp_processor_id();
153 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu_num
;
154 struct microcode_amd
*mc_amd
= uci
->mc
;
156 /* We should bind the task to the CPU */
157 BUG_ON(cpu_num
!= cpu
);
162 spin_lock_irqsave(µcode_update_lock
, flags
);
163 wrmsrl(MSR_AMD64_PATCH_LOADER
, (u64
)(long)&mc_amd
->hdr
.data_code
);
164 /* get patch id after patching */
165 rdmsr(MSR_AMD64_PATCH_LEVEL
, rev
, dummy
);
166 spin_unlock_irqrestore(µcode_update_lock
, flags
);
168 /* check current patch id and patch's id for match */
169 if (rev
!= mc_amd
->hdr
.patch_id
) {
170 printk(KERN_ERR
"microcode: CPU%d: update failed "
171 "(for patch_level=0x%x)\n", cpu
, mc_amd
->hdr
.patch_id
);
175 printk(KERN_INFO
"microcode: CPU%d: updated (new patch_level=0x%x)\n",
178 uci
->cpu_sig
.rev
= rev
;
181 static int get_ucode_data(void *to
, const u8
*from
, size_t n
)
187 static void *get_next_ucode(const u8
*buf
, unsigned int size
,
188 unsigned int *mc_size
)
190 unsigned int total_size
;
191 u8 section_hdr
[UCODE_CONTAINER_SECTION_HDR
];
194 if (get_ucode_data(section_hdr
, buf
, UCODE_CONTAINER_SECTION_HDR
))
197 if (section_hdr
[0] != UCODE_UCODE_TYPE
) {
198 printk(KERN_ERR
"microcode: error: invalid type field in "
199 "container file section header\n");
203 total_size
= (unsigned long) (section_hdr
[4] + (section_hdr
[5] << 8));
205 printk(KERN_DEBUG
"microcode: size %u, total_size %u\n",
208 if (total_size
> size
|| total_size
> UCODE_MAX_SIZE
) {
209 printk(KERN_ERR
"microcode: error: size mismatch\n");
213 mc
= vmalloc(UCODE_MAX_SIZE
);
215 memset(mc
, 0, UCODE_MAX_SIZE
);
216 if (get_ucode_data(mc
, buf
+ UCODE_CONTAINER_SECTION_HDR
,
221 *mc_size
= total_size
+ UCODE_CONTAINER_SECTION_HDR
;
227 static int install_equiv_cpu_table(const u8
*buf
)
229 u8
*container_hdr
[UCODE_CONTAINER_HEADER_SIZE
];
230 unsigned int *buf_pos
= (unsigned int *)container_hdr
;
233 if (get_ucode_data(&container_hdr
, buf
, UCODE_CONTAINER_HEADER_SIZE
))
238 if (buf_pos
[1] != UCODE_EQUIV_CPU_TABLE_TYPE
|| !size
) {
239 printk(KERN_ERR
"microcode: error: invalid type field in "
240 "container file section header\n");
244 equiv_cpu_table
= (struct equiv_cpu_entry
*) vmalloc(size
);
245 if (!equiv_cpu_table
) {
246 printk(KERN_ERR
"microcode: failed to allocate "
247 "equivalent CPU table\n");
251 buf
+= UCODE_CONTAINER_HEADER_SIZE
;
252 if (get_ucode_data(equiv_cpu_table
, buf
, size
)) {
253 vfree(equiv_cpu_table
);
257 return size
+ UCODE_CONTAINER_HEADER_SIZE
; /* add header length */
260 static void free_equiv_cpu_table(void)
262 if (equiv_cpu_table
) {
263 vfree(equiv_cpu_table
);
264 equiv_cpu_table
= NULL
;
268 static int generic_load_microcode(int cpu
, const u8
*data
, size_t size
)
270 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
271 const u8
*ucode_ptr
= data
;
274 int new_rev
= uci
->cpu_sig
.rev
;
275 unsigned int leftover
;
276 unsigned long offset
;
278 offset
= install_equiv_cpu_table(ucode_ptr
);
280 printk(KERN_ERR
"microcode: failed to create "
281 "equivalent cpu table\n");
286 leftover
= size
- offset
;
289 unsigned int uninitialized_var(mc_size
);
290 struct microcode_header_amd
*mc_header
;
292 mc
= get_next_ucode(ucode_ptr
, leftover
, &mc_size
);
296 mc_header
= (struct microcode_header_amd
*)mc
;
297 if (get_matching_microcode(cpu
, mc
, new_rev
)) {
300 new_rev
= mc_header
->patch_id
;
305 ucode_ptr
+= mc_size
;
314 pr_debug("microcode: CPU%d found a matching microcode "
315 "update with version 0x%x (current=0x%x)\n",
316 cpu
, new_rev
, uci
->cpu_sig
.rev
);
321 free_equiv_cpu_table();
323 return (int)leftover
;
326 static int request_microcode_fw(int cpu
, struct device
*device
)
328 const char *fw_name
= "amd-ucode/microcode_amd.bin";
329 const struct firmware
*firmware
;
332 /* We should bind the task to the CPU */
333 BUG_ON(cpu
!= raw_smp_processor_id());
335 ret
= request_firmware(&firmware
, fw_name
, device
);
337 printk(KERN_ERR
"microcode: failed to load file %s\n", fw_name
);
341 ret
= generic_load_microcode(cpu
, firmware
->data
, firmware
->size
);
343 release_firmware(firmware
);
348 static int request_microcode_user(int cpu
, const void __user
*buf
, size_t size
)
350 printk(KERN_INFO
"microcode: AMD microcode update via "
351 "/dev/cpu/microcode not supported\n");
355 static void microcode_fini_cpu_amd(int cpu
)
357 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
363 static struct microcode_ops microcode_amd_ops
= {
364 .request_microcode_user
= request_microcode_user
,
365 .request_microcode_fw
= request_microcode_fw
,
366 .collect_cpu_info
= collect_cpu_info_amd
,
367 .apply_microcode
= apply_microcode_amd
,
368 .microcode_fini_cpu
= microcode_fini_cpu_amd
,
371 struct microcode_ops
* __init
init_amd_microcode(void)
373 return µcode_amd_ops
;