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 <peter.oruba@amd.com>");
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 #define UCODE_MAX_SIZE (2048)
50 #define DEFAULT_UCODE_DATASIZE (896)
51 #define MC_HEADER_SIZE (sizeof(struct microcode_header_amd))
52 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
53 #define DWSIZE (sizeof(u32))
54 /* For now we support a fixed ucode total size only */
55 #define get_totalsize(mc) \
56 ((((struct microcode_amd *)mc)->hdr.mc_patch_data_len * 28) \
59 /* serialize access to the physical write */
60 static DEFINE_SPINLOCK(microcode_update_lock);
62 static struct equiv_cpu_entry *equiv_cpu_table;
64 static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
66 struct cpuinfo_x86 *c = &cpu_data(cpu);
68 memset(csig, 0, sizeof(*csig));
70 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
71 printk(KERN_ERR "microcode: CPU%d not a capable AMD processor\n",
76 asm volatile("movl %1, %%ecx; rdmsr"
78 : "i" (0x0000008B) : "ecx");
80 printk(KERN_INFO "microcode: collect_cpu_info_amd : patch_id=0x%x\n",
86 static int get_matching_microcode(int cpu, void *mc, int rev)
88 struct microcode_header_amd *mc_header = mc;
89 struct pci_dev *nb_pci_dev, *sb_pci_dev;
90 unsigned int current_cpu_id;
91 unsigned int equiv_cpu_id = 0x00;
95 * dimm: do we need this? Why an update via /dev/... is different
96 * from the one via firmware?
98 * This is a tricky part. We might be called from a write operation
99 * to the device file instead of the usual process of firmware
100 * loading. This routine needs to be able to distinguish both
101 * cases. This is done by checking if there alread is a equivalent
102 * CPU table installed. If not, we're written through
103 * /dev/cpu/microcode.
104 * Since we ignore all checks. The error case in which going through
105 * firmware loading and that table is not loaded has already been
108 BUG_ON(equiv_cpu_table == NULL);
110 if (equiv_cpu_table == NULL) {
111 printk(KERN_INFO "microcode: CPU%d microcode update with "
112 "version 0x%x (current=0x%x)\n",
113 cpu, mc_header->patch_id, uci->cpu_sig.rev);
117 current_cpu_id = cpuid_eax(0x00000001);
119 while (equiv_cpu_table[i].installed_cpu != 0) {
120 if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
121 equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
128 printk(KERN_ERR "microcode: CPU%d cpu_id "
129 "not found in equivalent cpu table \n", cpu);
133 if ((mc_header->processor_rev_id[0]) != (equiv_cpu_id & 0xff)) {
135 "microcode: CPU%d patch does not match "
136 "(patch is %x, cpu extended is %x) \n",
137 cpu, mc_header->processor_rev_id[0],
138 (equiv_cpu_id & 0xff));
142 if ((mc_header->processor_rev_id[1]) != ((equiv_cpu_id >> 16) & 0xff)) {
143 printk(KERN_ERR "microcode: CPU%d patch does not match "
144 "(patch is %x, cpu base id is %x) \n",
145 cpu, mc_header->processor_rev_id[1],
146 ((equiv_cpu_id >> 16) & 0xff));
151 /* ucode may be northbridge specific */
152 if (mc_header->nb_dev_id) {
153 nb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
154 (mc_header->nb_dev_id & 0xff),
157 (mc_header->nb_rev_id != nb_pci_dev->revision)) {
158 printk(KERN_ERR "microcode: CPU%d NB mismatch \n", cpu);
159 pci_dev_put(nb_pci_dev);
162 pci_dev_put(nb_pci_dev);
165 /* ucode may be southbridge specific */
166 if (mc_header->sb_dev_id) {
167 sb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
168 (mc_header->sb_dev_id & 0xff),
171 (mc_header->sb_rev_id != sb_pci_dev->revision)) {
172 printk(KERN_ERR "microcode: CPU%d SB mismatch \n", cpu);
173 pci_dev_put(sb_pci_dev);
176 pci_dev_put(sb_pci_dev);
179 if (mc_header->patch_id <= rev)
185 static void apply_microcode_amd(int cpu)
188 unsigned int eax, edx;
190 int cpu_num = raw_smp_processor_id();
191 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
194 /* We should bind the task to the CPU */
195 BUG_ON(cpu_num != cpu);
197 if (uci->mc.mc_amd == NULL)
200 spin_lock_irqsave(µcode_update_lock, flags);
202 addr = (unsigned long)&uci->mc.mc_amd->hdr.data_code;
203 edx = (unsigned int)(((unsigned long)upper_32_bits(addr)));
204 eax = (unsigned int)(((unsigned long)lower_32_bits(addr)));
206 asm volatile("movl %0, %%ecx; wrmsr" :
207 : "i" (0xc0010020), "a" (eax), "d" (edx) : "ecx");
209 /* get patch id after patching */
210 asm volatile("movl %1, %%ecx; rdmsr"
212 : "i" (0x0000008B) : "ecx");
214 spin_unlock_irqrestore(µcode_update_lock, flags);
216 /* check current patch id and patch's id for match */
217 if (rev != uci->mc.mc_amd->hdr.patch_id) {
218 printk(KERN_ERR "microcode: CPU%d update from revision "
219 "0x%x to 0x%x failed\n", cpu_num,
220 uci->mc.mc_amd->hdr.patch_id, rev);
224 printk(KERN_INFO "microcode: CPU%d updated from revision "
226 cpu_num, uci->cpu_sig.rev, uci->mc.mc_amd->hdr.patch_id);
228 uci->cpu_sig.rev = rev;
231 static void * get_next_ucode(u8 *buf, unsigned int size,
232 int (*get_ucode_data)(void *, const void *, size_t),
233 unsigned int *mc_size)
235 unsigned int total_size;
236 #define UCODE_UNKNOWN_HDR 8
237 u8 hdr[UCODE_UNKNOWN_HDR];
240 if (get_ucode_data(hdr, buf, UCODE_UNKNOWN_HDR))
243 if (hdr[0] != UCODE_UCODE_TYPE) {
244 printk(KERN_ERR "microcode: error! "
245 "Wrong microcode payload type field\n");
249 /* Why not by means of get_totalsize(hdr)? */
250 total_size = (unsigned long) (hdr[4] + (hdr[5] << 8));
252 printk(KERN_INFO "microcode: size %u, total_size %u\n",
255 if (total_size > size || total_size > UCODE_MAX_SIZE) {
256 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
260 mc = vmalloc(UCODE_MAX_SIZE);
262 memset(mc, 0, UCODE_MAX_SIZE);
263 if (get_ucode_data(mc, buf + UCODE_UNKNOWN_HDR, total_size)) {
267 *mc_size = total_size + UCODE_UNKNOWN_HDR;
269 #undef UCODE_UNKNOWN_HDR
274 static int install_equiv_cpu_table(u8 *buf,
275 int (*get_ucode_data)(void *, const void *, size_t))
277 #define UCODE_HEADER_SIZE 12
278 u8 *hdr[UCODE_HEADER_SIZE];
279 unsigned int *buf_pos = (unsigned int *)hdr;
282 if (get_ucode_data(&hdr, buf, UCODE_HEADER_SIZE))
287 if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
288 printk(KERN_ERR "microcode: error! "
289 "Wrong microcode equivalnet cpu table\n");
293 equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
294 if (!equiv_cpu_table) {
295 printk(KERN_ERR "microcode: error, can't allocate memory for equiv CPU table\n");
299 buf += UCODE_HEADER_SIZE;
300 if (get_ucode_data(equiv_cpu_table, buf, size)) {
301 vfree(equiv_cpu_table);
305 return size + UCODE_HEADER_SIZE; /* add header length */
306 #undef UCODE_HEADER_SIZE
309 static void free_equiv_cpu_table(void)
311 if (equiv_cpu_table) {
312 vfree(equiv_cpu_table);
313 equiv_cpu_table = NULL;
317 static int generic_load_microcode(int cpu, void *data, size_t size,
318 int (*get_ucode_data)(void *, const void *, size_t))
320 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
321 u8 *ucode_ptr = data, *new_mc = NULL, *mc;
322 int new_rev = uci->cpu_sig.rev;
323 unsigned int leftover;
324 unsigned long offset;
326 offset = install_equiv_cpu_table(ucode_ptr, get_ucode_data);
328 printk(KERN_ERR "microcode: installing equivalent cpu table failed\n");
333 leftover = size - offset;
336 unsigned int mc_size;
337 struct microcode_header_amd *mc_header;
339 mc = get_next_ucode(ucode_ptr, leftover, get_ucode_data, &mc_size);
343 mc_header = (struct microcode_header_amd *)mc;
344 if (get_matching_microcode(cpu, mc, new_rev)) {
345 new_rev = mc_header->patch_id;
350 ucode_ptr += mc_size;
357 vfree(uci->mc.mc_amd);
358 uci->mc.mc_amd = (struct microcode_amd *)new_mc;
359 pr_debug("microcode: CPU%d found a matching microcode update with"
360 " version 0x%x (current=0x%x)\n",
361 cpu, uci->mc.mc_amd->hdr.patch_id, uci->cpu_sig.rev);
366 free_equiv_cpu_table();
368 return (int)leftover;
371 static int get_ucode_fw(void *to, const void *from, size_t n)
377 static int request_microcode_fw(int cpu, struct device *device)
379 const char *fw_name = "amd-ucode/microcode_amd.bin";
380 const struct firmware *firmware;
383 /* We should bind the task to the CPU */
384 BUG_ON(cpu != raw_smp_processor_id());
386 ret = request_firmware(&firmware, fw_name, device);
388 printk(KERN_ERR "microcode: ucode data file %s load failed\n", fw_name);
392 ret = generic_load_microcode(cpu, (void*)firmware->data, firmware->size,
395 release_firmware(firmware);
400 static int get_ucode_user(void *to, const void *from, size_t n)
402 return copy_from_user(to, from, n);
405 static int request_microcode_user(int cpu, const void __user *buf, size_t size)
407 /* We should bind the task to the CPU */
408 BUG_ON(cpu != raw_smp_processor_id());
410 return generic_load_microcode(cpu, (void*)buf, size, &get_ucode_user);
413 static void microcode_fini_cpu_amd(int cpu)
415 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
417 vfree(uci->mc.mc_amd);
418 uci->mc.mc_amd = NULL;
421 static struct microcode_ops microcode_amd_ops = {
422 .request_microcode_user = request_microcode_user,
423 .request_microcode_fw = request_microcode_fw,
424 .collect_cpu_info = collect_cpu_info_amd,
425 .apply_microcode = apply_microcode_amd,
426 .microcode_fini_cpu = microcode_fini_cpu_amd,
429 static int __init microcode_amd_module_init(void)
431 struct cpuinfo_x86 *c = &cpu_data(0);
433 equiv_cpu_table = NULL;
434 if (c->x86_vendor != X86_VENDOR_AMD) {
435 printk(KERN_ERR "microcode: CPU platform is not AMD-capable\n");
439 return microcode_init(µcode_amd_ops, THIS_MODULE);
442 static void __exit microcode_amd_module_exit(void)
447 module_init(microcode_amd_module_init)
448 module_exit(microcode_amd_module_exit)