blob: 4006e5e3adf03a7ca73a3c26dc286762ffac7bdc [file] [log] [blame]
Peter Oruba80cc9f12008-07-28 18:44:22 +02001/*
2 * AMD CPU Microcode Update Driver for Linux
3 * Copyright (C) 2008 Advanced Micro Devices Inc.
4 *
5 * Author: Peter Oruba <peter.oruba@amd.com>
6 *
7 * Based on work by:
8 * Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
9 *
10 * This driver allows to upgrade microcode on AMD
11 * family 0x10 and 0x11 processors.
12 *
13 * Licensed unter the terms of the GNU General Public
14 * License version 2. See file COPYING for details.
15*/
16
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>
27#include <linux/mm.h>
28#include <linux/fs.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
36#include <asm/msr.h>
37#include <asm/uaccess.h>
38#include <asm/processor.h>
39#include <asm/microcode.h>
40
41MODULE_DESCRIPTION("AMD Microcode Update Driver");
42MODULE_AUTHOR("Peter Oruba <peter.oruba@amd.com>");
Ingo Molnar5d7b6052008-07-29 10:07:36 +020043MODULE_LICENSE("GPL v2");
Peter Oruba80cc9f12008-07-28 18:44:22 +020044
45#define UCODE_MAGIC 0x00414d44
46#define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
47#define UCODE_UCODE_TYPE 0x00000001
48
49#define UCODE_MAX_SIZE (2048)
Peter Orubaa0ac87d2008-07-29 17:41:04 +020050#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)
Peter Oruba80cc9f12008-07-28 18:44:22 +020053#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) \
57 + MC_HEADER_SIZE)
58
Peter Oruba80cc9f12008-07-28 18:44:22 +020059/* serialize access to the physical write */
60static DEFINE_SPINLOCK(microcode_update_lock);
61
Peter Oruba80cc9f12008-07-28 18:44:22 +020062struct equiv_cpu_entry *equiv_cpu_table;
63
Dmitry Adamushkod45de402008-08-20 00:22:26 +020064static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
Peter Oruba80cc9f12008-07-28 18:44:22 +020065{
66 struct cpuinfo_x86 *c = &cpu_data(cpu);
Peter Oruba80cc9f12008-07-28 18:44:22 +020067
Dmitry Adamushkod45de402008-08-20 00:22:26 +020068 memset(csig, 0, sizeof(*csig));
Peter Oruba80cc9f12008-07-28 18:44:22 +020069
70 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
71 printk(KERN_ERR "microcode: CPU%d not a capable AMD processor\n",
72 cpu);
Dmitry Adamushkod45de402008-08-20 00:22:26 +020073 return -1;
Peter Oruba80cc9f12008-07-28 18:44:22 +020074 }
75
76 asm volatile("movl %1, %%ecx; rdmsr"
Dmitry Adamushkod45de402008-08-20 00:22:26 +020077 : "=a" (csig->rev)
Peter Oruba80cc9f12008-07-28 18:44:22 +020078 : "i" (0x0000008B) : "ecx");
79
80 printk(KERN_INFO "microcode: collect_cpu_info_amd : patch_id=0x%x\n",
Dmitry Adamushkod45de402008-08-20 00:22:26 +020081 csig->rev);
82
83 return 0;
Peter Oruba80cc9f12008-07-28 18:44:22 +020084}
85
86static int get_matching_microcode_amd(void *mc, int cpu)
87{
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);
91 void *new_mc;
92 struct pci_dev *nb_pci_dev, *sb_pci_dev;
93 unsigned int current_cpu_id;
94 unsigned int equiv_cpu_id = 0x00;
95 unsigned int i = 0;
96
97 /* We should bind the task to the CPU */
98 BUG_ON(cpu != raw_smp_processor_id());
99
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",
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200112 cpu, mc_header->patch_id, uci->cpu_sig.rev);
Peter Oruba80cc9f12008-07-28 18:44:22 +0200113 goto out;
114 }
115
116 current_cpu_id = cpuid_eax(0x00000001);
117
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;
121 break;
122 }
123 i++;
124 }
125
126 if (!equiv_cpu_id) {
127 printk(KERN_ERR "microcode: CPU%d cpu_id "
128 "not found in equivalent cpu table \n", cpu);
129 return 0;
130 }
131
132 if ((mc_header->processor_rev_id[0]) != (equiv_cpu_id & 0xff)) {
133 printk(KERN_ERR
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));
138 return 0;
139 }
140
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));
146
147 return 0;
148 }
149
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),
154 NULL);
155 if ((!nb_pci_dev) ||
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);
159 return 0;
160 }
161 pci_dev_put(nb_pci_dev);
162 }
163
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),
168 NULL);
169 if ((!sb_pci_dev) ||
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);
173 return 0;
174 }
175 pci_dev_put(sb_pci_dev);
176 }
177
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200178 if (mc_header->patch_id <= uci->cpu_sig.rev)
Peter Oruba80cc9f12008-07-28 18:44:22 +0200179 return 0;
180
181 printk(KERN_INFO "microcode: CPU%d found a matching microcode "
182 "update with version 0x%x (current=0x%x)\n",
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200183 cpu, mc_header->patch_id, uci->cpu_sig.rev);
Peter Oruba80cc9f12008-07-28 18:44:22 +0200184
185out:
186 new_mc = vmalloc(UCODE_MAX_SIZE);
187 if (!new_mc) {
188 printk(KERN_ERR "microcode: error, can't allocate memory\n");
189 return -ENOMEM;
190 }
191 memset(new_mc, 0, UCODE_MAX_SIZE);
192
193 /* free previous update file */
194 vfree(uci->mc.mc_amd);
195
196 memcpy(new_mc, mc, total_size);
197
198 uci->mc.mc_amd = new_mc;
199 return 1;
200}
201
202static void apply_microcode_amd(int cpu)
203{
204 unsigned long flags;
205 unsigned int eax, edx;
206 unsigned int rev;
207 int cpu_num = raw_smp_processor_id();
208 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
209
210 /* We should bind the task to the CPU */
211 BUG_ON(cpu_num != cpu);
212
213 if (uci->mc.mc_amd == NULL)
214 return;
215
216 spin_lock_irqsave(&microcode_update_lock, flags);
217
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);
222
223 asm volatile("movl %0, %%ecx; wrmsr" :
224 : "i" (0xc0010020), "a" (eax), "d" (edx) : "ecx");
225
226 /* get patch id after patching */
227 asm volatile("movl %1, %%ecx; rdmsr"
228 : "=a" (rev)
229 : "i" (0x0000008B) : "ecx");
230
231 spin_unlock_irqrestore(&microcode_update_lock, flags);
232
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);
238 return;
239 }
240
241 printk(KERN_INFO "microcode: CPU%d updated from revision "
242 "0x%x to 0x%x \n",
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200243 cpu_num, uci->cpu_sig.rev, uci->mc.mc_amd->hdr.patch_id);
Peter Oruba80cc9f12008-07-28 18:44:22 +0200244
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200245 uci->cpu_sig.rev = rev;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200246}
247
248#ifdef CONFIG_MICROCODE_OLD_INTERFACE
249extern void __user *user_buffer; /* user area microcode data buffer */
250extern unsigned int user_buffer_size; /* it's size */
251
252static long get_next_ucode_amd(void **mc, long offset)
253{
254 struct microcode_header_amd mc_header;
255 unsigned long total_size;
256
257 /* No more data */
258 if (offset >= user_buffer_size)
259 return 0;
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");
262 return -EFAULT;
263 }
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 "
267 "data file\n");
268 return -EINVAL;
269 }
270 *mc = vmalloc(UCODE_MAX_SIZE);
271 if (!*mc)
272 return -ENOMEM;
273 memset(*mc, 0, UCODE_MAX_SIZE);
274
275 if (copy_from_user(*mc, user_buffer + offset, total_size)) {
276 printk(KERN_ERR "microcode: error! Can not read user data\n");
277 vfree(*mc);
278 return -EFAULT;
279 }
280 return offset + total_size;
281}
282#else
283#define get_next_ucode_amd() NULL
284#endif
285
286static long get_next_ucode_from_buffer_amd(void **mc, void *buf,
287 unsigned long size, long offset)
288{
289 struct microcode_header_amd *mc_header;
290 unsigned long total_size;
291 unsigned char *buf_pos = buf;
292
293 /* No more data */
294 if (offset >= size)
295 return 0;
296
297 if (buf_pos[offset] != UCODE_UCODE_TYPE) {
298 printk(KERN_ERR "microcode: error! "
299 "Wrong microcode payload type field\n");
300 return -EINVAL;
301 }
302
303 mc_header = (struct microcode_header_amd *)(&buf_pos[offset+8]);
304
305 total_size = (unsigned long) (buf_pos[offset+4] +
306 (buf_pos[offset+5] << 8));
307
308 printk(KERN_INFO "microcode: size %lu, total_size %lu, offset %ld\n",
309 size, total_size, offset);
310
311 if (offset + total_size > size) {
312 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
313 return -EINVAL;
314 }
315
316 *mc = vmalloc(UCODE_MAX_SIZE);
317 if (!*mc) {
318 printk(KERN_ERR "microcode: error! "
319 "Can not allocate memory for microcode patch\n");
320 return -ENOMEM;
321 }
322
323 memset(*mc, 0, UCODE_MAX_SIZE);
324 memcpy(*mc, buf + offset + 8, total_size);
325
326 return offset + total_size + 8;
327}
328
329static long install_equiv_cpu_table(void *buf, unsigned long size, long offset)
330{
331 unsigned int *buf_pos = buf;
332
333 /* No more data */
334 if (offset >= size)
335 return 0;
336
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");
340 return 0;
341 }
342
343 if (size == 0) {
344 printk(KERN_ERR "microcode: error! "
345 "Wrong microcode equivalnet cpu table length\n");
346 return 0;
347 }
348
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");
352 return 0;
353 }
354
355 memset(equiv_cpu_table, 0, size);
356 memcpy(equiv_cpu_table, &buf_pos[3], size);
357
358 return size + 12; /* add header length */
359}
360
361/* fake device for request_firmware */
362extern struct platform_device *microcode_pdev;
363
364static int cpu_request_microcode_amd(int cpu)
365{
366 char name[30];
367 const struct firmware *firmware;
368 void *buf;
369 unsigned int *buf_pos;
370 unsigned long size;
371 long offset = 0;
372 int error;
373 void *mc;
374
375 /* We should bind the task to the CPU */
376 BUG_ON(cpu != raw_smp_processor_id());
377
378 sprintf(name, "amd-ucode/microcode_amd.bin");
379 error = request_firmware(&firmware, "amd-ucode/microcode_amd.bin",
380 &microcode_pdev->dev);
381 if (error) {
382 printk(KERN_ERR "microcode: ucode data file %s load failed\n",
383 name);
384 return error;
385 }
386
Peter Orubadaa9c0f2008-07-29 17:41:07 +0200387 buf_pos = (unsigned int *)firmware->data;
388 buf = (void *)firmware->data;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200389 size = firmware->size;
390
391 if (buf_pos[0] != UCODE_MAGIC) {
392 printk(KERN_ERR "microcode: error! Wrong microcode patch file magic\n");
393 return -EINVAL;
394 }
395
396 offset = install_equiv_cpu_table(buf, buf_pos[2], offset);
397
398 if (!offset) {
399 printk(KERN_ERR "microcode: installing equivalent cpu table failed\n");
400 return -EINVAL;
401 }
402
403 while ((offset =
404 get_next_ucode_from_buffer_amd(&mc, buf, size, offset)) > 0) {
405 error = get_matching_microcode_amd(mc, cpu);
406 if (error < 0)
407 break;
408 /*
409 * It's possible the data file has multiple matching ucode,
410 * lets keep searching till the latest version
411 */
412 if (error == 1) {
413 apply_microcode_amd(cpu);
414 error = 0;
415 }
416 vfree(mc);
417 }
418 if (offset > 0) {
419 vfree(mc);
420 vfree(equiv_cpu_table);
421 equiv_cpu_table = NULL;
422 }
423 if (offset < 0)
424 error = offset;
425 release_firmware(firmware);
426
427 return error;
428}
429
Peter Oruba80cc9f12008-07-28 18:44:22 +0200430static void microcode_fini_cpu_amd(int cpu)
431{
432 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
433
Peter Oruba80cc9f12008-07-28 18:44:22 +0200434 vfree(uci->mc.mc_amd);
435 uci->mc.mc_amd = NULL;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200436}
437
438static 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,
Peter Oruba80cc9f12008-07-28 18:44:22 +0200442 .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,
446};
447
448static int __init microcode_amd_module_init(void)
449{
Dmitry Adamushko8343ef22008-08-20 00:16:13 +0200450 struct cpuinfo_x86 *c = &cpu_data(0);
Peter Oruba80cc9f12008-07-28 18:44:22 +0200451
452 equiv_cpu_table = NULL;
Dmitry Adamushko8343ef22008-08-20 00:16:13 +0200453 if (c->x86_vendor != X86_VENDOR_AMD) {
454 printk(KERN_ERR "microcode: CPU platform is not AMD-capable\n");
Peter Oruba80cc9f12008-07-28 18:44:22 +0200455 return -ENODEV;
Dmitry Adamushko8343ef22008-08-20 00:16:13 +0200456 }
457
458 return microcode_init(&microcode_amd_ops, THIS_MODULE);
Peter Oruba80cc9f12008-07-28 18:44:22 +0200459}
460
461static void __exit microcode_amd_module_exit(void)
462{
463 microcode_exit();
464}
465
466module_init(microcode_amd_module_init)
467module_exit(microcode_amd_module_exit)