Merge commit 'v2.6.27-rc7' into x86/microcode
[linux-2.6] / arch / x86 / kernel / microcode_amd.c
1 /*
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
41 MODULE_DESCRIPTION("AMD Microcode Update Driver");
42 MODULE_AUTHOR("Peter Oruba <peter.oruba@amd.com>");
43 MODULE_LICENSE("GPL v2");
44
45 #define UCODE_MAGIC                0x00414d44
46 #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
47 #define UCODE_UCODE_TYPE           0x00000001
48
49 struct equiv_cpu_entry {
50         unsigned int installed_cpu;
51         unsigned int fixed_errata_mask;
52         unsigned int fixed_errata_compare;
53         unsigned int equiv_cpu;
54 };
55
56 struct microcode_header_amd {
57         unsigned int  data_code;
58         unsigned int  patch_id;
59         unsigned char mc_patch_data_id[2];
60         unsigned char mc_patch_data_len;
61         unsigned char init_flag;
62         unsigned int  mc_patch_data_checksum;
63         unsigned int  nb_dev_id;
64         unsigned int  sb_dev_id;
65         unsigned char processor_rev_id[2];
66         unsigned char nb_rev_id;
67         unsigned char sb_rev_id;
68         unsigned char bios_api_rev;
69         unsigned char reserved1[3];
70         unsigned int  match_reg[8];
71 };
72
73 struct microcode_amd {
74         struct microcode_header_amd hdr;
75         unsigned int mpb[0];
76 };
77
78 #define UCODE_MAX_SIZE          (2048)
79 #define DEFAULT_UCODE_DATASIZE  (896)
80 #define MC_HEADER_SIZE          (sizeof(struct microcode_header_amd))
81 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
82 #define DWSIZE                  (sizeof(u32))
83 /* For now we support a fixed ucode total size only */
84 #define get_totalsize(mc) \
85         ((((struct microcode_amd *)mc)->hdr.mc_patch_data_len * 28) \
86          + MC_HEADER_SIZE)
87
88 /* serialize access to the physical write */
89 static DEFINE_SPINLOCK(microcode_update_lock);
90
91 static struct equiv_cpu_entry *equiv_cpu_table;
92
93 static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
94 {
95         struct cpuinfo_x86 *c = &cpu_data(cpu);
96
97         memset(csig, 0, sizeof(*csig));
98
99         if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
100                 printk(KERN_ERR "microcode: CPU%d not a capable AMD processor\n",
101                        cpu);
102                 return -1;
103         }
104
105         asm volatile("movl %1, %%ecx; rdmsr"
106                      : "=a" (csig->rev)
107                      : "i" (0x0000008B) : "ecx");
108
109         printk(KERN_INFO "microcode: collect_cpu_info_amd : patch_id=0x%x\n",
110                 csig->rev);
111
112         return 0;
113 }
114
115 static int get_matching_microcode(int cpu, void *mc, int rev)
116 {
117         struct microcode_header_amd *mc_header = mc;
118         struct pci_dev *nb_pci_dev, *sb_pci_dev;
119         unsigned int current_cpu_id;
120         unsigned int equiv_cpu_id = 0x00;
121         unsigned int i = 0;
122
123         /*
124          * FIXME! dimm: do we need this? Why an update via /dev/... is different
125          * from the one via firmware?
126          *
127          * This is a tricky part. We might be called from a write operation
128          * to the device file instead of the usual process of firmware
129          * loading. This routine needs to be able to distinguish both
130          * cases. This is done by checking if there alread is a equivalent
131          * CPU table installed. If not, we're written through
132          * /dev/cpu/microcode.
133          * Since we ignore all checks. The error case in which going through
134          * firmware loading and that table is not loaded has already been
135          * checked earlier.
136          */
137         BUG_ON(equiv_cpu_table == NULL);
138 #if 0
139         if (equiv_cpu_table == NULL) {
140                 printk(KERN_INFO "microcode: CPU%d microcode update with "
141                        "version 0x%x (current=0x%x)\n",
142                        cpu, mc_header->patch_id, uci->cpu_sig.rev);
143                 goto out;
144         }
145 #endif
146         current_cpu_id = cpuid_eax(0x00000001);
147
148         while (equiv_cpu_table[i].installed_cpu != 0) {
149                 if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
150                         equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
151                         break;
152                 }
153                 i++;
154         }
155
156         if (!equiv_cpu_id) {
157                 printk(KERN_ERR "microcode: CPU%d cpu_id "
158                        "not found in equivalent cpu table \n", cpu);
159                 return 0;
160         }
161
162         if ((mc_header->processor_rev_id[0]) != (equiv_cpu_id & 0xff)) {
163                 printk(KERN_ERR
164                         "microcode: CPU%d patch does not match "
165                         "(patch is %x, cpu extended is %x) \n",
166                         cpu, mc_header->processor_rev_id[0],
167                         (equiv_cpu_id & 0xff));
168                 return 0;
169         }
170
171         if ((mc_header->processor_rev_id[1]) != ((equiv_cpu_id >> 16) & 0xff)) {
172                 printk(KERN_ERR "microcode: CPU%d patch does not match "
173                         "(patch is %x, cpu base id is %x) \n",
174                         cpu, mc_header->processor_rev_id[1],
175                         ((equiv_cpu_id >> 16) & 0xff));
176
177                 return 0;
178         }
179
180         /* ucode may be northbridge specific */
181         if (mc_header->nb_dev_id) {
182                 nb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
183                                             (mc_header->nb_dev_id & 0xff),
184                                             NULL);
185                 if ((!nb_pci_dev) ||
186                     (mc_header->nb_rev_id != nb_pci_dev->revision)) {
187                         printk(KERN_ERR "microcode: CPU%d NB mismatch \n", cpu);
188                         pci_dev_put(nb_pci_dev);
189                         return 0;
190                 }
191                 pci_dev_put(nb_pci_dev);
192         }
193
194         /* ucode may be southbridge specific */
195         if (mc_header->sb_dev_id) {
196                 sb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
197                                             (mc_header->sb_dev_id & 0xff),
198                                             NULL);
199                 if ((!sb_pci_dev) ||
200                     (mc_header->sb_rev_id != sb_pci_dev->revision)) {
201                         printk(KERN_ERR "microcode: CPU%d SB mismatch \n", cpu);
202                         pci_dev_put(sb_pci_dev);
203                         return 0;
204                 }
205                 pci_dev_put(sb_pci_dev);
206         }
207
208         if (mc_header->patch_id <= rev)
209                 return 0;
210
211         return 1;
212 }
213
214 static void apply_microcode_amd(int cpu)
215 {
216         unsigned long flags;
217         unsigned int eax, edx;
218         unsigned int rev;
219         int cpu_num = raw_smp_processor_id();
220         struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
221         struct microcode_amd *mc_amd = uci->mc;
222         unsigned long addr;
223
224         /* We should bind the task to the CPU */
225         BUG_ON(cpu_num != cpu);
226
227         if (mc_amd == NULL)
228                 return;
229
230         spin_lock_irqsave(&microcode_update_lock, flags);
231
232         addr = (unsigned long)&mc_amd->hdr.data_code;
233         edx = (unsigned int)(((unsigned long)upper_32_bits(addr)));
234         eax = (unsigned int)(((unsigned long)lower_32_bits(addr)));
235
236         asm volatile("movl %0, %%ecx; wrmsr" :
237                      : "i" (0xc0010020), "a" (eax), "d" (edx) : "ecx");
238
239         /* get patch id after patching */
240         asm volatile("movl %1, %%ecx; rdmsr"
241                      : "=a" (rev)
242                      : "i" (0x0000008B) : "ecx");
243
244         spin_unlock_irqrestore(&microcode_update_lock, flags);
245
246         /* check current patch id and patch's id for match */
247         if (rev != 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                        mc_amd->hdr.patch_id, rev);
251                 return;
252         }
253
254         printk(KERN_INFO "microcode: CPU%d updated from revision "
255                "0x%x to 0x%x \n",
256                cpu_num, uci->cpu_sig.rev, mc_amd->hdr.patch_id);
257
258         uci->cpu_sig.rev = rev;
259 }
260
261 static void * get_next_ucode(u8 *buf, unsigned int size,
262                         int (*get_ucode_data)(void *, const void *, size_t),
263                         unsigned int *mc_size)
264 {
265         unsigned int total_size;
266 #define UCODE_CONTAINER_SECTION_HDR     8
267         u8 section_hdr[UCODE_CONTAINER_SECTION_HDR];
268         void *mc;
269
270         if (get_ucode_data(section_hdr, buf, UCODE_CONTAINER_SECTION_HDR))
271                 return NULL;
272
273         if (section_hdr[0] != UCODE_UCODE_TYPE) {
274                 printk(KERN_ERR "microcode: error! "
275                        "Wrong microcode payload type field\n");
276                 return NULL;
277         }
278
279         total_size = (unsigned long) (section_hdr[4] + (section_hdr[5] << 8));
280
281         printk(KERN_INFO "microcode: size %u, total_size %u\n",
282                 size, total_size);
283
284         if (total_size > size || total_size > UCODE_MAX_SIZE) {
285                 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
286                 return NULL;
287         }
288
289         mc = vmalloc(UCODE_MAX_SIZE);
290         if (mc) {
291                 memset(mc, 0, UCODE_MAX_SIZE);
292                 if (get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR, total_size)) {
293                         vfree(mc);
294                         mc = NULL;
295                 } else
296                         *mc_size = total_size + UCODE_CONTAINER_SECTION_HDR;
297         }
298 #undef UCODE_CONTAINER_SECTION_HDR
299         return mc;
300 }
301
302
303 static int install_equiv_cpu_table(u8 *buf,
304                 int (*get_ucode_data)(void *, const void *, size_t))
305 {
306 #define UCODE_CONTAINER_HEADER_SIZE     12
307         u8 *container_hdr[UCODE_CONTAINER_HEADER_SIZE];
308         unsigned int *buf_pos = (unsigned int *)container_hdr;
309         unsigned long size;
310
311         if (get_ucode_data(&container_hdr, buf, UCODE_CONTAINER_HEADER_SIZE))
312                 return 0;
313
314         size = buf_pos[2];
315
316         if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
317                 printk(KERN_ERR "microcode: error! "
318                        "Wrong microcode equivalnet cpu table\n");
319                 return 0;
320         }
321
322         equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
323         if (!equiv_cpu_table) {
324                 printk(KERN_ERR "microcode: error, can't allocate memory for equiv CPU table\n");
325                 return 0;
326         }
327
328         buf += UCODE_CONTAINER_HEADER_SIZE;
329         if (get_ucode_data(equiv_cpu_table, buf, size)) {
330                 vfree(equiv_cpu_table);
331                 return 0;
332         }
333
334         return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */
335 #undef UCODE_CONTAINER_HEADER_SIZE
336 }
337
338 static void free_equiv_cpu_table(void)
339 {
340         if (equiv_cpu_table) {
341                 vfree(equiv_cpu_table);
342                 equiv_cpu_table = NULL;
343         }
344 }
345
346 static int generic_load_microcode(int cpu, void *data, size_t size,
347                 int (*get_ucode_data)(void *, const void *, size_t))
348 {
349         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
350         u8 *ucode_ptr = data, *new_mc = NULL, *mc;
351         int new_rev = uci->cpu_sig.rev;
352         unsigned int leftover;
353         unsigned long offset;
354
355         offset = install_equiv_cpu_table(ucode_ptr, get_ucode_data);
356         if (!offset) {
357                 printk(KERN_ERR "microcode: installing equivalent cpu table failed\n");
358                 return -EINVAL;
359         }
360
361         ucode_ptr += offset;
362         leftover = size - offset;
363
364         while (leftover) {
365                 unsigned int mc_size;
366                 struct microcode_header_amd *mc_header;
367
368                 mc = get_next_ucode(ucode_ptr, leftover, get_ucode_data, &mc_size);
369                 if (!mc)
370                         break;
371
372                 mc_header = (struct microcode_header_amd *)mc;
373                 if (get_matching_microcode(cpu, mc, new_rev)) {
374                         if (new_mc)
375                                 vfree(new_mc);
376                         new_rev = mc_header->patch_id;
377                         new_mc  = mc;
378                 } else 
379                         vfree(mc);
380
381                 ucode_ptr += mc_size;
382                 leftover  -= mc_size;
383         }
384
385         if (new_mc) {
386                 if (!leftover) {
387                         if (uci->mc)
388                                 vfree(uci->mc);
389                         uci->mc = new_mc;
390                         pr_debug("microcode: CPU%d found a matching microcode update with"
391                                 " version 0x%x (current=0x%x)\n",
392                                 cpu, new_rev, uci->cpu_sig.rev);
393                 } else
394                         vfree(new_mc);
395         }
396
397         free_equiv_cpu_table();
398
399         return (int)leftover;
400 }
401
402 static int get_ucode_fw(void *to, const void *from, size_t n)
403 {
404         memcpy(to, from, n);
405         return 0;
406 }
407
408 static int request_microcode_fw(int cpu, struct device *device)
409 {
410         const char *fw_name = "amd-ucode/microcode_amd.bin";
411         const struct firmware *firmware;
412         int ret;
413
414         /* We should bind the task to the CPU */
415         BUG_ON(cpu != raw_smp_processor_id());
416
417         ret = request_firmware(&firmware, fw_name, device);
418         if (ret) {
419                 printk(KERN_ERR "microcode: ucode data file %s load failed\n", fw_name);
420                 return ret;
421         }
422
423         ret = generic_load_microcode(cpu, (void*)firmware->data, firmware->size,
424                         &get_ucode_fw);
425
426         release_firmware(firmware);
427
428         return ret;
429 }
430
431 static int get_ucode_user(void *to, const void *from, size_t n)
432 {
433         return copy_from_user(to, from, n);
434 }
435
436 static int request_microcode_user(int cpu, const void __user *buf, size_t size)
437 {
438         /* We should bind the task to the CPU */
439         BUG_ON(cpu != raw_smp_processor_id());
440
441         return generic_load_microcode(cpu, (void*)buf, size, &get_ucode_user);
442 }
443
444 static void microcode_fini_cpu_amd(int cpu)
445 {
446         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
447
448         vfree(uci->mc);
449         uci->mc = NULL;
450 }
451
452 static struct microcode_ops microcode_amd_ops = {
453         .request_microcode_user           = request_microcode_user,
454         .request_microcode_fw             = request_microcode_fw,
455         .collect_cpu_info                 = collect_cpu_info_amd,
456         .apply_microcode                  = apply_microcode_amd,
457         .microcode_fini_cpu               = microcode_fini_cpu_amd,
458 };
459
460 struct microcode_ops * __init init_amd_microcode(void)
461 {
462         return &microcode_amd_ops;
463 }