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) /* 896 bytes */
51 #define MC_HEADER_SIZE (sizeof(struct microcode_header_amd)) /* 64 bytes */
52 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 960 bytes */
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 extern int microcode_init(void *opaque, struct module *module);
60 extern void microcode_exit(void);
62 /* serialize access to the physical write */
63 static DEFINE_SPINLOCK(microcode_update_lock);
65 /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
66 extern struct mutex (microcode_mutex);
68 struct equiv_cpu_entry *equiv_cpu_table;
70 extern struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
72 static void collect_cpu_info_amd(int cpu)
74 struct cpuinfo_x86 *c = &cpu_data(cpu);
75 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
77 /* We should bind the task to the CPU */
78 BUG_ON(raw_smp_processor_id() != cpu);
81 uci->mc.mc_amd = NULL;
84 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
85 printk(KERN_ERR "microcode: CPU%d not a capable AMD processor\n",
91 asm volatile("movl %1, %%ecx; rdmsr"
93 : "i" (0x0000008B) : "ecx");
95 printk(KERN_INFO "microcode: collect_cpu_info_amd : patch_id=0x%x\n",
99 static int get_matching_microcode_amd(void *mc, int cpu)
101 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
102 struct microcode_header_amd *mc_header = mc;
103 unsigned long total_size = get_totalsize(mc_header);
105 struct pci_dev *nb_pci_dev, *sb_pci_dev;
106 unsigned int current_cpu_id;
107 unsigned int equiv_cpu_id = 0x00;
110 /* We should bind the task to the CPU */
111 BUG_ON(cpu != raw_smp_processor_id());
113 /* This is a tricky part. We might be called from a write operation */
114 /* to the device file instead of the usual process of firmware */
115 /* loading. This routine needs to be able to distinguish both */
116 /* cases. This is done by checking if there alread is a equivalent */
117 /* CPU table installed. If not, we're written through */
118 /* /dev/cpu/microcode. */
119 /* Since we ignore all checks. The error case in which going through */
120 /* firmware loading and that table is not loaded has already been */
121 /* checked earlier. */
122 if (equiv_cpu_table == NULL) {
123 printk(KERN_INFO "microcode: CPU%d microcode update with "
124 "version 0x%x (current=0x%x)\n",
125 cpu, mc_header->patch_id, uci->rev);
129 current_cpu_id = cpuid_eax(0x00000001);
131 while (equiv_cpu_table[i].installed_cpu != 0) {
132 if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
133 equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
140 printk(KERN_ERR "microcode: CPU%d cpu_id "
141 "not found in equivalent cpu table \n", cpu);
145 if ((mc_header->processor_rev_id[0]) != (equiv_cpu_id & 0xff)) {
147 "microcode: CPU%d patch does not match "
148 "(patch is %x, cpu extended is %x) \n",
149 cpu, mc_header->processor_rev_id[0],
150 (equiv_cpu_id & 0xff));
154 if ((mc_header->processor_rev_id[1]) != ((equiv_cpu_id >> 16) & 0xff)) {
155 printk(KERN_ERR "microcode: CPU%d patch does not match "
156 "(patch is %x, cpu base id is %x) \n",
157 cpu, mc_header->processor_rev_id[1],
158 ((equiv_cpu_id >> 16) & 0xff));
163 /* ucode may be northbridge specific */
164 if (mc_header->nb_dev_id) {
165 nb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
166 (mc_header->nb_dev_id & 0xff),
169 (mc_header->nb_rev_id != nb_pci_dev->revision)) {
170 printk(KERN_ERR "microcode: CPU%d NB mismatch \n", cpu);
171 pci_dev_put(nb_pci_dev);
174 pci_dev_put(nb_pci_dev);
177 /* ucode may be southbridge specific */
178 if (mc_header->sb_dev_id) {
179 sb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
180 (mc_header->sb_dev_id & 0xff),
183 (mc_header->sb_rev_id != sb_pci_dev->revision)) {
184 printk(KERN_ERR "microcode: CPU%d SB mismatch \n", cpu);
185 pci_dev_put(sb_pci_dev);
188 pci_dev_put(sb_pci_dev);
191 if (mc_header->patch_id <= uci->rev)
194 printk(KERN_INFO "microcode: CPU%d found a matching microcode "
195 "update with version 0x%x (current=0x%x)\n",
196 cpu, mc_header->patch_id, uci->rev);
199 new_mc = vmalloc(UCODE_MAX_SIZE);
201 printk(KERN_ERR "microcode: error, can't allocate memory\n");
204 memset(new_mc, 0, UCODE_MAX_SIZE);
206 /* free previous update file */
207 vfree(uci->mc.mc_amd);
209 memcpy(new_mc, mc, total_size);
211 uci->mc.mc_amd = new_mc;
215 static void apply_microcode_amd(int cpu)
218 unsigned int eax, edx;
220 int cpu_num = raw_smp_processor_id();
221 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
223 /* We should bind the task to the CPU */
224 BUG_ON(cpu_num != cpu);
226 if (uci->mc.mc_amd == NULL)
229 spin_lock_irqsave(µcode_update_lock, flags);
231 edx = (unsigned int)(((unsigned long)
232 &(uci->mc.mc_amd->hdr.data_code)) >> 32);
233 eax = (unsigned int)(((unsigned long)
234 &(uci->mc.mc_amd->hdr.data_code)) & 0xffffffffL);
236 asm volatile("movl %0, %%ecx; wrmsr" :
237 : "i" (0xc0010020), "a" (eax), "d" (edx) : "ecx");
239 /* get patch id after patching */
240 asm volatile("movl %1, %%ecx; rdmsr"
242 : "i" (0x0000008B) : "ecx");
244 spin_unlock_irqrestore(µcode_update_lock, flags);
246 /* check current patch id and patch's id for match */
247 if (rev != uci->mc.mc_amd->hdr.patch_id) {
248 printk(KERN_ERR "microcode: CPU%d update from revision "
249 "0x%x to 0x%x failed\n", cpu_num,
250 uci->mc.mc_amd->hdr.patch_id, rev);
254 printk(KERN_INFO "microcode: CPU%d updated from revision "
256 cpu_num, uci->rev, uci->mc.mc_amd->hdr.patch_id);
261 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
262 extern void __user *user_buffer; /* user area microcode data buffer */
263 extern unsigned int user_buffer_size; /* it's size */
265 static long get_next_ucode_amd(void **mc, long offset)
267 struct microcode_header_amd mc_header;
268 unsigned long total_size;
271 if (offset >= user_buffer_size)
273 if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
274 printk(KERN_ERR "microcode: error! Can not read user data\n");
277 total_size = get_totalsize(&mc_header);
278 if (offset + total_size > user_buffer_size) {
279 printk(KERN_ERR "microcode: error! Bad total size in microcode "
283 *mc = vmalloc(UCODE_MAX_SIZE);
286 memset(*mc, 0, UCODE_MAX_SIZE);
288 if (copy_from_user(*mc, user_buffer + offset, total_size)) {
289 printk(KERN_ERR "microcode: error! Can not read user data\n");
293 return offset + total_size;
296 #define get_next_ucode_amd() NULL
299 static long get_next_ucode_from_buffer_amd(void **mc, void *buf,
300 unsigned long size, long offset)
302 struct microcode_header_amd *mc_header;
303 unsigned long total_size;
304 unsigned char *buf_pos = buf;
310 if (buf_pos[offset] != UCODE_UCODE_TYPE) {
311 printk(KERN_ERR "microcode: error! "
312 "Wrong microcode payload type field\n");
316 mc_header = (struct microcode_header_amd *)(&buf_pos[offset+8]);
318 total_size = (unsigned long) (buf_pos[offset+4] +
319 (buf_pos[offset+5] << 8));
321 printk(KERN_INFO "microcode: size %lu, total_size %lu, offset %ld\n",
322 size, total_size, offset);
324 if (offset + total_size > size) {
325 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
329 *mc = vmalloc(UCODE_MAX_SIZE);
331 printk(KERN_ERR "microcode: error! "
332 "Can not allocate memory for microcode patch\n");
336 memset(*mc, 0, UCODE_MAX_SIZE);
337 memcpy(*mc, buf + offset + 8, total_size);
339 return offset + total_size + 8;
342 static long install_equiv_cpu_table(void *buf, unsigned long size, long offset)
344 unsigned int *buf_pos = buf;
350 if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE) {
351 printk(KERN_ERR "microcode: error! "
352 "Wrong microcode equivalnet cpu table type field\n");
357 printk(KERN_ERR "microcode: error! "
358 "Wrong microcode equivalnet cpu table length\n");
362 equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
363 if (!equiv_cpu_table) {
364 printk(KERN_ERR "microcode: error, can't allocate memory for equiv CPU table\n");
368 memset(equiv_cpu_table, 0, size);
369 memcpy(equiv_cpu_table, &buf_pos[3], size);
371 return size + 12; /* add header length */
374 /* fake device for request_firmware */
375 extern struct platform_device *microcode_pdev;
377 static int cpu_request_microcode_amd(int cpu)
380 const struct firmware *firmware;
382 unsigned int *buf_pos;
388 /* We should bind the task to the CPU */
389 BUG_ON(cpu != raw_smp_processor_id());
391 sprintf(name, "amd-ucode/microcode_amd.bin");
392 error = request_firmware(&firmware, "amd-ucode/microcode_amd.bin",
393 µcode_pdev->dev);
395 printk(KERN_ERR "microcode: ucode data file %s load failed\n",
400 buf_pos = buf = firmware->data;
401 size = firmware->size;
403 if (buf_pos[0] != UCODE_MAGIC) {
404 printk(KERN_ERR "microcode: error! Wrong microcode patch file magic\n");
408 offset = install_equiv_cpu_table(buf, buf_pos[2], offset);
411 printk(KERN_ERR "microcode: installing equivalent cpu table failed\n");
416 get_next_ucode_from_buffer_amd(&mc, buf, size, offset)) > 0) {
417 error = get_matching_microcode_amd(mc, cpu);
421 * It's possible the data file has multiple matching ucode,
422 * lets keep searching till the latest version
425 apply_microcode_amd(cpu);
432 vfree(equiv_cpu_table);
433 equiv_cpu_table = NULL;
437 release_firmware(firmware);
442 static int apply_microcode_check_cpu_amd(int cpu)
444 struct cpuinfo_x86 *c = &cpu_data(cpu);
445 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
448 cpumask_of_cpu_ptr(newmask, cpu);
451 /* Check if the microcode is available */
455 old = current->cpus_allowed;
456 set_cpus_allowed(current, newmask);
458 /* Check if the microcode we have in memory matches the CPU */
459 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 16)
463 asm volatile("movl %1, %%ecx; rdmsr"
465 : "i" (0x0000008B) : "ecx");
472 apply_microcode_amd(cpu);
474 printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
478 set_cpus_allowed(current, old);
482 static void microcode_fini_cpu_amd(int cpu)
484 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
486 mutex_lock(µcode_mutex);
488 vfree(uci->mc.mc_amd);
489 uci->mc.mc_amd = NULL;
490 mutex_unlock(µcode_mutex);
493 static struct microcode_ops microcode_amd_ops = {
494 .get_next_ucode = get_next_ucode_amd,
495 .get_matching_microcode = get_matching_microcode_amd,
496 .microcode_sanity_check = NULL,
497 .apply_microcode_check_cpu = apply_microcode_check_cpu_amd,
498 .cpu_request_microcode = cpu_request_microcode_amd,
499 .collect_cpu_info = collect_cpu_info_amd,
500 .apply_microcode = apply_microcode_amd,
501 .microcode_fini_cpu = microcode_fini_cpu_amd,
504 static int __init microcode_amd_module_init(void)
506 struct cpuinfo_x86 *c = &cpu_data(get_cpu());
508 equiv_cpu_table = NULL;
509 if (c->x86_vendor == X86_VENDOR_AMD)
510 return microcode_init(µcode_amd_ops, THIS_MODULE);
515 static void __exit microcode_amd_module_exit(void)
520 module_init(microcode_amd_module_init)
521 module_exit(microcode_amd_module_exit)