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 /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
63 extern struct mutex (microcode_mutex);
65 struct equiv_cpu_entry *equiv_cpu_table;
67 extern struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
69 static void collect_cpu_info_amd(int cpu)
71 struct cpuinfo_x86 *c = &cpu_data(cpu);
72 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
74 /* We should bind the task to the CPU */
75 BUG_ON(raw_smp_processor_id() != cpu);
78 uci->mc.mc_amd = NULL;
81 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
82 printk(KERN_ERR "microcode: CPU%d not a capable AMD processor\n",
88 asm volatile("movl %1, %%ecx; rdmsr"
90 : "i" (0x0000008B) : "ecx");
92 printk(KERN_INFO "microcode: collect_cpu_info_amd : patch_id=0x%x\n",
96 static int get_matching_microcode_amd(void *mc, int cpu)
98 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
99 struct microcode_header_amd *mc_header = mc;
100 unsigned long total_size = get_totalsize(mc_header);
102 struct pci_dev *nb_pci_dev, *sb_pci_dev;
103 unsigned int current_cpu_id;
104 unsigned int equiv_cpu_id = 0x00;
107 /* We should bind the task to the CPU */
108 BUG_ON(cpu != raw_smp_processor_id());
110 /* This is a tricky part. We might be called from a write operation */
111 /* to the device file instead of the usual process of firmware */
112 /* loading. This routine needs to be able to distinguish both */
113 /* cases. This is done by checking if there alread is a equivalent */
114 /* CPU table installed. If not, we're written through */
115 /* /dev/cpu/microcode. */
116 /* Since we ignore all checks. The error case in which going through */
117 /* firmware loading and that table is not loaded has already been */
118 /* checked earlier. */
119 if (equiv_cpu_table == NULL) {
120 printk(KERN_INFO "microcode: CPU%d microcode update with "
121 "version 0x%x (current=0x%x)\n",
122 cpu, mc_header->patch_id, uci->rev);
126 current_cpu_id = cpuid_eax(0x00000001);
128 while (equiv_cpu_table[i].installed_cpu != 0) {
129 if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
130 equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
137 printk(KERN_ERR "microcode: CPU%d cpu_id "
138 "not found in equivalent cpu table \n", cpu);
142 if ((mc_header->processor_rev_id[0]) != (equiv_cpu_id & 0xff)) {
144 "microcode: CPU%d patch does not match "
145 "(patch is %x, cpu extended is %x) \n",
146 cpu, mc_header->processor_rev_id[0],
147 (equiv_cpu_id & 0xff));
151 if ((mc_header->processor_rev_id[1]) != ((equiv_cpu_id >> 16) & 0xff)) {
152 printk(KERN_ERR "microcode: CPU%d patch does not match "
153 "(patch is %x, cpu base id is %x) \n",
154 cpu, mc_header->processor_rev_id[1],
155 ((equiv_cpu_id >> 16) & 0xff));
160 /* ucode may be northbridge specific */
161 if (mc_header->nb_dev_id) {
162 nb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
163 (mc_header->nb_dev_id & 0xff),
166 (mc_header->nb_rev_id != nb_pci_dev->revision)) {
167 printk(KERN_ERR "microcode: CPU%d NB mismatch \n", cpu);
168 pci_dev_put(nb_pci_dev);
171 pci_dev_put(nb_pci_dev);
174 /* ucode may be southbridge specific */
175 if (mc_header->sb_dev_id) {
176 sb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
177 (mc_header->sb_dev_id & 0xff),
180 (mc_header->sb_rev_id != sb_pci_dev->revision)) {
181 printk(KERN_ERR "microcode: CPU%d SB mismatch \n", cpu);
182 pci_dev_put(sb_pci_dev);
185 pci_dev_put(sb_pci_dev);
188 if (mc_header->patch_id <= uci->rev)
191 printk(KERN_INFO "microcode: CPU%d found a matching microcode "
192 "update with version 0x%x (current=0x%x)\n",
193 cpu, mc_header->patch_id, uci->rev);
196 new_mc = vmalloc(UCODE_MAX_SIZE);
198 printk(KERN_ERR "microcode: error, can't allocate memory\n");
201 memset(new_mc, 0, UCODE_MAX_SIZE);
203 /* free previous update file */
204 vfree(uci->mc.mc_amd);
206 memcpy(new_mc, mc, total_size);
208 uci->mc.mc_amd = new_mc;
212 static void apply_microcode_amd(int cpu)
215 unsigned int eax, edx;
217 int cpu_num = raw_smp_processor_id();
218 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
220 /* We should bind the task to the CPU */
221 BUG_ON(cpu_num != cpu);
223 if (uci->mc.mc_amd == NULL)
226 spin_lock_irqsave(µcode_update_lock, flags);
228 edx = (unsigned int)(((unsigned long)
229 &(uci->mc.mc_amd->hdr.data_code)) >> 32);
230 eax = (unsigned int)(((unsigned long)
231 &(uci->mc.mc_amd->hdr.data_code)) & 0xffffffffL);
233 asm volatile("movl %0, %%ecx; wrmsr" :
234 : "i" (0xc0010020), "a" (eax), "d" (edx) : "ecx");
236 /* get patch id after patching */
237 asm volatile("movl %1, %%ecx; rdmsr"
239 : "i" (0x0000008B) : "ecx");
241 spin_unlock_irqrestore(µcode_update_lock, flags);
243 /* check current patch id and patch's id for match */
244 if (rev != uci->mc.mc_amd->hdr.patch_id) {
245 printk(KERN_ERR "microcode: CPU%d update from revision "
246 "0x%x to 0x%x failed\n", cpu_num,
247 uci->mc.mc_amd->hdr.patch_id, rev);
251 printk(KERN_INFO "microcode: CPU%d updated from revision "
253 cpu_num, uci->rev, uci->mc.mc_amd->hdr.patch_id);
258 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
259 extern void __user *user_buffer; /* user area microcode data buffer */
260 extern unsigned int user_buffer_size; /* it's size */
262 static long get_next_ucode_amd(void **mc, long offset)
264 struct microcode_header_amd mc_header;
265 unsigned long total_size;
268 if (offset >= user_buffer_size)
270 if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
271 printk(KERN_ERR "microcode: error! Can not read user data\n");
274 total_size = get_totalsize(&mc_header);
275 if (offset + total_size > user_buffer_size) {
276 printk(KERN_ERR "microcode: error! Bad total size in microcode "
280 *mc = vmalloc(UCODE_MAX_SIZE);
283 memset(*mc, 0, UCODE_MAX_SIZE);
285 if (copy_from_user(*mc, user_buffer + offset, total_size)) {
286 printk(KERN_ERR "microcode: error! Can not read user data\n");
290 return offset + total_size;
293 #define get_next_ucode_amd() NULL
296 static long get_next_ucode_from_buffer_amd(void **mc, void *buf,
297 unsigned long size, long offset)
299 struct microcode_header_amd *mc_header;
300 unsigned long total_size;
301 unsigned char *buf_pos = buf;
307 if (buf_pos[offset] != UCODE_UCODE_TYPE) {
308 printk(KERN_ERR "microcode: error! "
309 "Wrong microcode payload type field\n");
313 mc_header = (struct microcode_header_amd *)(&buf_pos[offset+8]);
315 total_size = (unsigned long) (buf_pos[offset+4] +
316 (buf_pos[offset+5] << 8));
318 printk(KERN_INFO "microcode: size %lu, total_size %lu, offset %ld\n",
319 size, total_size, offset);
321 if (offset + total_size > size) {
322 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
326 *mc = vmalloc(UCODE_MAX_SIZE);
328 printk(KERN_ERR "microcode: error! "
329 "Can not allocate memory for microcode patch\n");
333 memset(*mc, 0, UCODE_MAX_SIZE);
334 memcpy(*mc, buf + offset + 8, total_size);
336 return offset + total_size + 8;
339 static long install_equiv_cpu_table(void *buf, unsigned long size, long offset)
341 unsigned int *buf_pos = buf;
347 if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE) {
348 printk(KERN_ERR "microcode: error! "
349 "Wrong microcode equivalnet cpu table type field\n");
354 printk(KERN_ERR "microcode: error! "
355 "Wrong microcode equivalnet cpu table length\n");
359 equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
360 if (!equiv_cpu_table) {
361 printk(KERN_ERR "microcode: error, can't allocate memory for equiv CPU table\n");
365 memset(equiv_cpu_table, 0, size);
366 memcpy(equiv_cpu_table, &buf_pos[3], size);
368 return size + 12; /* add header length */
371 /* fake device for request_firmware */
372 extern struct platform_device *microcode_pdev;
374 static int cpu_request_microcode_amd(int cpu)
377 const struct firmware *firmware;
379 unsigned int *buf_pos;
385 /* We should bind the task to the CPU */
386 BUG_ON(cpu != raw_smp_processor_id());
388 sprintf(name, "amd-ucode/microcode_amd.bin");
389 error = request_firmware(&firmware, "amd-ucode/microcode_amd.bin",
390 µcode_pdev->dev);
392 printk(KERN_ERR "microcode: ucode data file %s load failed\n",
397 buf_pos = (unsigned int *)firmware->data;
398 buf = (void *)firmware->data;
399 size = firmware->size;
401 if (buf_pos[0] != UCODE_MAGIC) {
402 printk(KERN_ERR "microcode: error! Wrong microcode patch file magic\n");
406 offset = install_equiv_cpu_table(buf, buf_pos[2], offset);
409 printk(KERN_ERR "microcode: installing equivalent cpu table failed\n");
414 get_next_ucode_from_buffer_amd(&mc, buf, size, offset)) > 0) {
415 error = get_matching_microcode_amd(mc, cpu);
419 * It's possible the data file has multiple matching ucode,
420 * lets keep searching till the latest version
423 apply_microcode_amd(cpu);
430 vfree(equiv_cpu_table);
431 equiv_cpu_table = NULL;
435 release_firmware(firmware);
440 static int apply_microcode_check_cpu_amd(int cpu)
442 struct cpuinfo_x86 *c = &cpu_data(cpu);
443 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
448 /* Check if the microcode is available */
452 old = current->cpus_allowed;
453 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
455 /* Check if the microcode we have in memory matches the CPU */
456 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 16)
460 asm volatile("movl %1, %%ecx; rdmsr"
462 : "i" (0x0000008B) : "ecx");
469 apply_microcode_amd(cpu);
471 printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
475 set_cpus_allowed(current, old);
479 static void microcode_fini_cpu_amd(int cpu)
481 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
483 mutex_lock(µcode_mutex);
485 vfree(uci->mc.mc_amd);
486 uci->mc.mc_amd = NULL;
487 mutex_unlock(µcode_mutex);
490 static struct microcode_ops microcode_amd_ops = {
491 .get_next_ucode = get_next_ucode_amd,
492 .get_matching_microcode = get_matching_microcode_amd,
493 .microcode_sanity_check = NULL,
494 .apply_microcode_check_cpu = apply_microcode_check_cpu_amd,
495 .cpu_request_microcode = cpu_request_microcode_amd,
496 .collect_cpu_info = collect_cpu_info_amd,
497 .apply_microcode = apply_microcode_amd,
498 .microcode_fini_cpu = microcode_fini_cpu_amd,
501 static int __init microcode_amd_module_init(void)
503 struct cpuinfo_x86 *c = &cpu_data(get_cpu());
505 equiv_cpu_table = NULL;
506 if (c->x86_vendor == X86_VENDOR_AMD)
507 return microcode_init(µcode_amd_ops, THIS_MODULE);
512 static void __exit microcode_amd_module_exit(void)
517 module_init(microcode_amd_module_init)
518 module_exit(microcode_amd_module_exit)