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 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_amd(void *mc, int cpu)
88 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
89 struct microcode_header_amd *mc_header = mc;
90 unsigned long total_size = get_totalsize(mc_header);
92 struct pci_dev *nb_pci_dev, *sb_pci_dev;
93 unsigned int current_cpu_id;
94 unsigned int equiv_cpu_id = 0x00;
97 /* We should bind the task to the CPU */
98 BUG_ON(cpu != raw_smp_processor_id());
100 /* This is a tricky part. We might be called from a write operation */
101 /* to the device file instead of the usual process of firmware */
102 /* loading. This routine needs to be able to distinguish both */
103 /* cases. This is done by checking if there alread is a equivalent */
104 /* CPU table installed. If not, we're written through */
105 /* /dev/cpu/microcode. */
106 /* Since we ignore all checks. The error case in which going through */
107 /* firmware loading and that table is not loaded has already been */
108 /* checked earlier. */
109 if (equiv_cpu_table == NULL) {
110 printk(KERN_INFO "microcode: CPU%d microcode update with "
111 "version 0x%x (current=0x%x)\n",
112 cpu, mc_header->patch_id, uci->cpu_sig.rev);
116 current_cpu_id = cpuid_eax(0x00000001);
118 while (equiv_cpu_table[i].installed_cpu != 0) {
119 if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
120 equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
127 printk(KERN_ERR "microcode: CPU%d cpu_id "
128 "not found in equivalent cpu table \n", cpu);
132 if ((mc_header->processor_rev_id[0]) != (equiv_cpu_id & 0xff)) {
134 "microcode: CPU%d patch does not match "
135 "(patch is %x, cpu extended is %x) \n",
136 cpu, mc_header->processor_rev_id[0],
137 (equiv_cpu_id & 0xff));
141 if ((mc_header->processor_rev_id[1]) != ((equiv_cpu_id >> 16) & 0xff)) {
142 printk(KERN_ERR "microcode: CPU%d patch does not match "
143 "(patch is %x, cpu base id is %x) \n",
144 cpu, mc_header->processor_rev_id[1],
145 ((equiv_cpu_id >> 16) & 0xff));
150 /* ucode may be northbridge specific */
151 if (mc_header->nb_dev_id) {
152 nb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
153 (mc_header->nb_dev_id & 0xff),
156 (mc_header->nb_rev_id != nb_pci_dev->revision)) {
157 printk(KERN_ERR "microcode: CPU%d NB mismatch \n", cpu);
158 pci_dev_put(nb_pci_dev);
161 pci_dev_put(nb_pci_dev);
164 /* ucode may be southbridge specific */
165 if (mc_header->sb_dev_id) {
166 sb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
167 (mc_header->sb_dev_id & 0xff),
170 (mc_header->sb_rev_id != sb_pci_dev->revision)) {
171 printk(KERN_ERR "microcode: CPU%d SB mismatch \n", cpu);
172 pci_dev_put(sb_pci_dev);
175 pci_dev_put(sb_pci_dev);
178 if (mc_header->patch_id <= uci->cpu_sig.rev)
181 printk(KERN_INFO "microcode: CPU%d found a matching microcode "
182 "update with version 0x%x (current=0x%x)\n",
183 cpu, mc_header->patch_id, uci->cpu_sig.rev);
186 new_mc = vmalloc(UCODE_MAX_SIZE);
188 printk(KERN_ERR "microcode: error, can't allocate memory\n");
191 memset(new_mc, 0, UCODE_MAX_SIZE);
193 /* free previous update file */
194 vfree(uci->mc.mc_amd);
196 memcpy(new_mc, mc, total_size);
198 uci->mc.mc_amd = new_mc;
202 static void apply_microcode_amd(int cpu)
205 unsigned int eax, edx;
207 int cpu_num = raw_smp_processor_id();
208 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
210 /* We should bind the task to the CPU */
211 BUG_ON(cpu_num != cpu);
213 if (uci->mc.mc_amd == NULL)
216 spin_lock_irqsave(µcode_update_lock, flags);
218 edx = (unsigned int)(((unsigned long)
219 &(uci->mc.mc_amd->hdr.data_code)) >> 32);
220 eax = (unsigned int)(((unsigned long)
221 &(uci->mc.mc_amd->hdr.data_code)) & 0xffffffffL);
223 asm volatile("movl %0, %%ecx; wrmsr" :
224 : "i" (0xc0010020), "a" (eax), "d" (edx) : "ecx");
226 /* get patch id after patching */
227 asm volatile("movl %1, %%ecx; rdmsr"
229 : "i" (0x0000008B) : "ecx");
231 spin_unlock_irqrestore(µcode_update_lock, flags);
233 /* check current patch id and patch's id for match */
234 if (rev != uci->mc.mc_amd->hdr.patch_id) {
235 printk(KERN_ERR "microcode: CPU%d update from revision "
236 "0x%x to 0x%x failed\n", cpu_num,
237 uci->mc.mc_amd->hdr.patch_id, rev);
241 printk(KERN_INFO "microcode: CPU%d updated from revision "
243 cpu_num, uci->cpu_sig.rev, uci->mc.mc_amd->hdr.patch_id);
245 uci->cpu_sig.rev = rev;
248 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
249 extern void __user *user_buffer; /* user area microcode data buffer */
250 extern unsigned int user_buffer_size; /* it's size */
252 static long get_next_ucode_amd(void **mc, long offset)
254 struct microcode_header_amd mc_header;
255 unsigned long total_size;
258 if (offset >= user_buffer_size)
260 if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
261 printk(KERN_ERR "microcode: error! Can not read user data\n");
264 total_size = get_totalsize(&mc_header);
265 if (offset + total_size > user_buffer_size) {
266 printk(KERN_ERR "microcode: error! Bad total size in microcode "
270 *mc = vmalloc(UCODE_MAX_SIZE);
273 memset(*mc, 0, UCODE_MAX_SIZE);
275 if (copy_from_user(*mc, user_buffer + offset, total_size)) {
276 printk(KERN_ERR "microcode: error! Can not read user data\n");
280 return offset + total_size;
283 #define get_next_ucode_amd() NULL
286 static long get_next_ucode_from_buffer_amd(void **mc, void *buf,
287 unsigned long size, long offset)
289 struct microcode_header_amd *mc_header;
290 unsigned long total_size;
291 unsigned char *buf_pos = buf;
297 if (buf_pos[offset] != UCODE_UCODE_TYPE) {
298 printk(KERN_ERR "microcode: error! "
299 "Wrong microcode payload type field\n");
303 mc_header = (struct microcode_header_amd *)(&buf_pos[offset+8]);
305 total_size = (unsigned long) (buf_pos[offset+4] +
306 (buf_pos[offset+5] << 8));
308 printk(KERN_INFO "microcode: size %lu, total_size %lu, offset %ld\n",
309 size, total_size, offset);
311 if (offset + total_size > size) {
312 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
316 *mc = vmalloc(UCODE_MAX_SIZE);
318 printk(KERN_ERR "microcode: error! "
319 "Can not allocate memory for microcode patch\n");
323 memset(*mc, 0, UCODE_MAX_SIZE);
324 memcpy(*mc, buf + offset + 8, total_size);
326 return offset + total_size + 8;
329 static long install_equiv_cpu_table(void *buf, unsigned long size, long offset)
331 unsigned int *buf_pos = buf;
337 if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE) {
338 printk(KERN_ERR "microcode: error! "
339 "Wrong microcode equivalnet cpu table type field\n");
344 printk(KERN_ERR "microcode: error! "
345 "Wrong microcode equivalnet cpu table length\n");
349 equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
350 if (!equiv_cpu_table) {
351 printk(KERN_ERR "microcode: error, can't allocate memory for equiv CPU table\n");
355 memset(equiv_cpu_table, 0, size);
356 memcpy(equiv_cpu_table, &buf_pos[3], size);
358 return size + 12; /* add header length */
361 /* fake device for request_firmware */
362 extern struct platform_device *microcode_pdev;
364 static int cpu_request_microcode_amd(int cpu)
367 const struct firmware *firmware;
369 unsigned int *buf_pos;
375 /* We should bind the task to the CPU */
376 BUG_ON(cpu != raw_smp_processor_id());
378 sprintf(name, "amd-ucode/microcode_amd.bin");
379 error = request_firmware(&firmware, "amd-ucode/microcode_amd.bin",
380 µcode_pdev->dev);
382 printk(KERN_ERR "microcode: ucode data file %s load failed\n",
387 buf_pos = (unsigned int *)firmware->data;
388 buf = (void *)firmware->data;
389 size = firmware->size;
391 if (buf_pos[0] != UCODE_MAGIC) {
392 printk(KERN_ERR "microcode: error! Wrong microcode patch file magic\n");
396 offset = install_equiv_cpu_table(buf, buf_pos[2], offset);
399 printk(KERN_ERR "microcode: installing equivalent cpu table failed\n");
404 get_next_ucode_from_buffer_amd(&mc, buf, size, offset)) > 0) {
405 error = get_matching_microcode_amd(mc, cpu);
409 * It's possible the data file has multiple matching ucode,
410 * lets keep searching till the latest version
413 apply_microcode_amd(cpu);
420 vfree(equiv_cpu_table);
421 equiv_cpu_table = NULL;
425 release_firmware(firmware);
430 static void microcode_fini_cpu_amd(int cpu)
432 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
434 vfree(uci->mc.mc_amd);
435 uci->mc.mc_amd = NULL;
438 static struct microcode_ops microcode_amd_ops = {
439 .get_next_ucode = get_next_ucode_amd,
440 .get_matching_microcode = get_matching_microcode_amd,
441 .microcode_sanity_check = NULL,
442 .cpu_request_microcode = cpu_request_microcode_amd,
443 .collect_cpu_info = collect_cpu_info_amd,
444 .apply_microcode = apply_microcode_amd,
445 .microcode_fini_cpu = microcode_fini_cpu_amd,
448 static int __init microcode_amd_module_init(void)
450 struct cpuinfo_x86 *c = &cpu_data(0);
452 equiv_cpu_table = NULL;
453 if (c->x86_vendor != X86_VENDOR_AMD) {
454 printk(KERN_ERR "microcode: CPU platform is not AMD-capable\n");
458 return microcode_init(µcode_amd_ops, THIS_MODULE);
461 static void __exit microcode_amd_module_exit(void)
466 module_init(microcode_amd_module_init)
467 module_exit(microcode_amd_module_exit)