Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Intel CPU Microcode Update Driver for Linux |
| 3 | * |
| 4 | * Copyright (C) 2000-2004 Tigran Aivazian |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 5 | * 2006 Shaohua Li <shaohua.li@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This driver allows to upgrade microcode on Intel processors |
| 8 | * belonging to IA-32 family - PentiumPro, Pentium II, |
| 9 | * Pentium III, Xeon, Pentium 4, etc. |
| 10 | * |
| 11 | * Reference: Section 8.10 of Volume III, Intel Pentium 4 Manual, |
| 12 | * Order Number 245472 or free download from: |
| 13 | * |
| 14 | * http://developer.intel.com/design/pentium4/manuals/245472.htm |
| 15 | * |
| 16 | * For more information, go to http://www.urbanmyth.org/microcode |
| 17 | * |
| 18 | * This program is free software; you can redistribute it and/or |
| 19 | * modify it under the terms of the GNU General Public License |
| 20 | * as published by the Free Software Foundation; either version |
| 21 | * 2 of the License, or (at your option) any later version. |
| 22 | * |
| 23 | * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com> |
| 24 | * Initial release. |
| 25 | * 1.01 18 Feb 2000, Tigran Aivazian <tigran@sco.com> |
| 26 | * Added read() support + cleanups. |
| 27 | * 1.02 21 Feb 2000, Tigran Aivazian <tigran@sco.com> |
| 28 | * Added 'device trimming' support. open(O_WRONLY) zeroes |
| 29 | * and frees the saved copy of applied microcode. |
| 30 | * 1.03 29 Feb 2000, Tigran Aivazian <tigran@sco.com> |
| 31 | * Made to use devfs (/dev/cpu/microcode) + cleanups. |
| 32 | * 1.04 06 Jun 2000, Simon Trimmer <simon@veritas.com> |
| 33 | * Added misc device support (now uses both devfs and misc). |
| 34 | * Added MICROCODE_IOCFREE ioctl to clear memory. |
| 35 | * 1.05 09 Jun 2000, Simon Trimmer <simon@veritas.com> |
| 36 | * Messages for error cases (non Intel & no suitable microcode). |
| 37 | * 1.06 03 Aug 2000, Tigran Aivazian <tigran@veritas.com> |
| 38 | * Removed ->release(). Removed exclusive open and status bitmap. |
| 39 | * Added microcode_rwsem to serialize read()/write()/ioctl(). |
| 40 | * Removed global kernel lock usage. |
| 41 | * 1.07 07 Sep 2000, Tigran Aivazian <tigran@veritas.com> |
| 42 | * Write 0 to 0x8B msr and then cpuid before reading revision, |
| 43 | * so that it works even if there were no update done by the |
| 44 | * BIOS. Otherwise, reading from 0x8B gives junk (which happened |
| 45 | * to be 0 on my machine which is why it worked even when I |
| 46 | * disabled update by the BIOS) |
| 47 | * Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix. |
| 48 | * 1.08 11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and |
| 49 | * Tigran Aivazian <tigran@veritas.com> |
| 50 | * Intel Pentium 4 processor support and bugfixes. |
| 51 | * 1.09 30 Oct 2001, Tigran Aivazian <tigran@veritas.com> |
| 52 | * Bugfix for HT (Hyper-Threading) enabled processors |
| 53 | * whereby processor resources are shared by all logical processors |
| 54 | * in a single CPU package. |
| 55 | * 1.10 28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and |
| 56 | * Tigran Aivazian <tigran@veritas.com>, |
| 57 | * Serialize updates as required on HT processors due to speculative |
| 58 | * nature of implementation. |
| 59 | * 1.11 22 Mar 2002 Tigran Aivazian <tigran@veritas.com> |
| 60 | * Fix the panic when writing zero-length microcode chunk. |
| 61 | * 1.12 29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>, |
| 62 | * Jun Nakajima <jun.nakajima@intel.com> |
| 63 | * Support for the microcode updates in the new format. |
| 64 | * 1.13 10 Oct 2003 Tigran Aivazian <tigran@veritas.com> |
| 65 | * Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl |
| 66 | * because we no longer hold a copy of applied microcode |
| 67 | * in kernel memory. |
| 68 | * 1.14 25 Jun 2004 Tigran Aivazian <tigran@veritas.com> |
| 69 | * Fix sigmatch() macro to handle old CPUs with pf == 0. |
| 70 | * Thanks to Stuart Swales for pointing out this bug. |
| 71 | */ |
| 72 | |
| 73 | //#define DEBUG /* pr_debug */ |
Randy Dunlap | a941564 | 2006-01-11 12:17:48 -0800 | [diff] [blame] | 74 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #include <linux/kernel.h> |
| 76 | #include <linux/init.h> |
| 77 | #include <linux/sched.h> |
Dave Jones | 5cf6c54 | 2006-02-28 16:58:53 -0800 | [diff] [blame] | 78 | #include <linux/cpumask.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #include <linux/module.h> |
| 80 | #include <linux/slab.h> |
| 81 | #include <linux/vmalloc.h> |
| 82 | #include <linux/miscdevice.h> |
| 83 | #include <linux/spinlock.h> |
| 84 | #include <linux/mm.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 85 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | #include <asm/msr.h> |
| 88 | #include <asm/uaccess.h> |
| 89 | #include <asm/processor.h> |
| 90 | |
| 91 | MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver"); |
| 92 | MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>"); |
| 93 | MODULE_LICENSE("GPL"); |
| 94 | |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 95 | #define MICROCODE_VERSION "1.14a" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */ |
| 98 | #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */ |
| 99 | #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */ |
| 100 | #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */ |
| 101 | #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */ |
| 102 | #define DWSIZE (sizeof (u32)) |
| 103 | #define get_totalsize(mc) \ |
| 104 | (((microcode_t *)mc)->hdr.totalsize ? \ |
| 105 | ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE) |
| 106 | #define get_datasize(mc) \ |
| 107 | (((microcode_t *)mc)->hdr.datasize ? \ |
| 108 | ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE) |
| 109 | |
| 110 | #define sigmatch(s1, s2, p1, p2) \ |
| 111 | (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0)))) |
| 112 | |
| 113 | #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE) |
| 114 | |
| 115 | /* serialize access to the physical write to MSR 0x79 */ |
| 116 | static DEFINE_SPINLOCK(microcode_update_lock); |
| 117 | |
| 118 | /* no concurrent ->write()s are allowed on /dev/cpu/microcode */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 119 | static DEFINE_MUTEX(microcode_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | static struct ucode_cpu_info { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 122 | int valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | unsigned int sig; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 124 | unsigned int pf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | unsigned int rev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | microcode_t *mc; |
| 127 | } ucode_cpu_info[NR_CPUS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 129 | static void collect_cpu_info(int cpu_num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | struct cpuinfo_x86 *c = cpu_data + cpu_num; |
| 132 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
| 133 | unsigned int val[2]; |
| 134 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 135 | /* We should bind the task to the CPU */ |
| 136 | BUG_ON(raw_smp_processor_id() != cpu_num); |
| 137 | uci->pf = uci->rev = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | uci->mc = NULL; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 139 | uci->valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 || |
| 142 | cpu_has(c, X86_FEATURE_IA64)) { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 143 | printk(KERN_ERR "microcode: CPU%d not a capable Intel " |
| 144 | "processor\n", cpu_num); |
| 145 | uci->valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | return; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 149 | uci->sig = cpuid_eax(0x00000001); |
| 150 | |
| 151 | if ((c->x86_model >= 5) || (c->x86 > 6)) { |
| 152 | /* get processor flags from MSR 0x17 */ |
| 153 | rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); |
| 154 | uci->pf = 1 << ((val[1] >> 18) & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | wrmsr(MSR_IA32_UCODE_REV, 0, 0); |
Zachary Amsden | 245067d | 2005-09-03 15:56:37 -0700 | [diff] [blame] | 158 | /* see notes above for revision 1.07. Apparent chip bug */ |
Andi Kleen | 487472b | 2006-01-11 22:45:27 +0100 | [diff] [blame] | 159 | sync_core(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | /* get the current revision from MSR 0x8B */ |
| 161 | rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev); |
| 162 | pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n", |
| 163 | uci->sig, uci->pf, uci->rev); |
| 164 | } |
| 165 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 166 | static inline int microcode_update_match(int cpu_num, |
| 167 | microcode_header_t *mc_header, int sig, int pf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
| 170 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 171 | if (!sigmatch(sig, uci->sig, pf, uci->pf) |
| 172 | || mc_header->rev <= uci->rev) |
| 173 | return 0; |
| 174 | return 1; |
| 175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 177 | static int microcode_sanity_check(void *mc) |
| 178 | { |
| 179 | microcode_header_t *mc_header = mc; |
| 180 | struct extended_sigtable *ext_header = NULL; |
| 181 | struct extended_signature *ext_sig; |
| 182 | unsigned long total_size, data_size, ext_table_size; |
| 183 | int sum, orig_sum, ext_sigcount = 0, i; |
| 184 | |
| 185 | total_size = get_totalsize(mc_header); |
| 186 | data_size = get_datasize(mc_header); |
| 187 | if ((data_size + MC_HEADER_SIZE > total_size) |
| 188 | || (data_size < DEFAULT_UCODE_DATASIZE)) { |
| 189 | printk(KERN_ERR "microcode: error! " |
| 190 | "Bad data size in microcode data file\n"); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | if (mc_header->ldrver != 1 || mc_header->hdrver != 1) { |
| 195 | printk(KERN_ERR "microcode: error! " |
| 196 | "Unknown microcode update format\n"); |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | ext_table_size = total_size - (MC_HEADER_SIZE + data_size); |
| 200 | if (ext_table_size) { |
| 201 | if ((ext_table_size < EXT_HEADER_SIZE) |
| 202 | || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) { |
| 203 | printk(KERN_ERR "microcode: error! " |
| 204 | "Small exttable size in microcode data file\n"); |
| 205 | return -EINVAL; |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 206 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 207 | ext_header = mc + MC_HEADER_SIZE + data_size; |
| 208 | if (ext_table_size != exttable_size(ext_header)) { |
| 209 | printk(KERN_ERR "microcode: error! " |
| 210 | "Bad exttable size in microcode data file\n"); |
| 211 | return -EFAULT; |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 212 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 213 | ext_sigcount = ext_header->count; |
| 214 | } |
| 215 | |
| 216 | /* check extended table checksum */ |
| 217 | if (ext_table_size) { |
| 218 | int ext_table_sum = 0; |
| 219 | int * ext_tablep = (int *)ext_header; |
| 220 | |
| 221 | i = ext_table_size / DWSIZE; |
| 222 | while (i--) |
| 223 | ext_table_sum += ext_tablep[i]; |
| 224 | if (ext_table_sum) { |
| 225 | printk(KERN_WARNING "microcode: aborting, " |
| 226 | "bad extended signature table checksum\n"); |
| 227 | return -EINVAL; |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 230 | |
| 231 | /* calculate the checksum */ |
| 232 | orig_sum = 0; |
| 233 | i = (MC_HEADER_SIZE + data_size) / DWSIZE; |
| 234 | while (i--) |
| 235 | orig_sum += ((int *)mc)[i]; |
| 236 | if (orig_sum) { |
| 237 | printk(KERN_ERR "microcode: aborting, bad checksum\n"); |
| 238 | return -EINVAL; |
| 239 | } |
| 240 | if (!ext_table_size) |
| 241 | return 0; |
| 242 | /* check extended signature checksum */ |
| 243 | for (i = 0; i < ext_sigcount; i++) { |
| 244 | ext_sig = (struct extended_signature *)((void *)ext_header |
| 245 | + EXT_HEADER_SIZE + EXT_SIGNATURE_SIZE * i); |
| 246 | sum = orig_sum |
| 247 | - (mc_header->sig + mc_header->pf + mc_header->cksum) |
| 248 | + (ext_sig->sig + ext_sig->pf + ext_sig->cksum); |
| 249 | if (sum) { |
| 250 | printk(KERN_ERR "microcode: aborting, bad checksum\n"); |
| 251 | return -EINVAL; |
| 252 | } |
| 253 | } |
| 254 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 257 | /* |
| 258 | * return 0 - no update found |
| 259 | * return 1 - found update |
| 260 | * return < 0 - error |
| 261 | */ |
| 262 | static int get_maching_microcode(void *mc, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 264 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 265 | microcode_header_t *mc_header = mc; |
| 266 | struct extended_sigtable *ext_header; |
| 267 | unsigned long total_size = get_totalsize(mc_header); |
| 268 | int ext_sigcount, i; |
| 269 | struct extended_signature *ext_sig; |
| 270 | void *new_mc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 272 | if (microcode_update_match(cpu, mc_header, |
| 273 | mc_header->sig, mc_header->pf)) |
| 274 | goto find; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 276 | if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE) |
| 277 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 279 | ext_header = (struct extended_sigtable *)(mc + |
| 280 | get_datasize(mc_header) + MC_HEADER_SIZE); |
| 281 | ext_sigcount = ext_header->count; |
| 282 | ext_sig = (struct extended_signature *)((void *)ext_header |
| 283 | + EXT_HEADER_SIZE); |
| 284 | for (i = 0; i < ext_sigcount; i++) { |
| 285 | if (microcode_update_match(cpu, mc_header, |
| 286 | ext_sig->sig, ext_sig->pf)) |
| 287 | goto find; |
| 288 | ext_sig++; |
| 289 | } |
| 290 | return 0; |
| 291 | find: |
| 292 | pr_debug("microcode: CPU %d found a matching microcode update with" |
| 293 | " version 0x%x (current=0x%x)\n", cpu, mc_header->rev,uci->rev); |
| 294 | new_mc = vmalloc(total_size); |
| 295 | if (!new_mc) { |
| 296 | printk(KERN_ERR "microcode: error! Can not allocate memory\n"); |
| 297 | return -ENOMEM; |
| 298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 300 | /* free previous update file */ |
| 301 | vfree(uci->mc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 303 | memcpy(new_mc, mc, total_size); |
| 304 | uci->mc = new_mc; |
| 305 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 308 | static void apply_microcode(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
| 310 | unsigned long flags; |
| 311 | unsigned int val[2]; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 312 | int cpu_num = raw_smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
| 314 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 315 | /* We should bind the task to the CPU */ |
| 316 | BUG_ON(cpu_num != cpu); |
| 317 | |
| 318 | if (uci->mc == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
| 321 | /* serialize access to the physical write to MSR 0x79 */ |
| 322 | spin_lock_irqsave(µcode_update_lock, flags); |
| 323 | |
| 324 | /* write microcode via MSR 0x79 */ |
| 325 | wrmsr(MSR_IA32_UCODE_WRITE, |
| 326 | (unsigned long) uci->mc->bits, |
| 327 | (unsigned long) uci->mc->bits >> 16 >> 16); |
| 328 | wrmsr(MSR_IA32_UCODE_REV, 0, 0); |
| 329 | |
Zachary Amsden | 245067d | 2005-09-03 15:56:37 -0700 | [diff] [blame] | 330 | /* see notes above for revision 1.07. Apparent chip bug */ |
Andi Kleen | 487472b | 2006-01-11 22:45:27 +0100 | [diff] [blame] | 331 | sync_core(); |
Zachary Amsden | 245067d | 2005-09-03 15:56:37 -0700 | [diff] [blame] | 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | /* get the current revision from MSR 0x8B */ |
| 334 | rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]); |
| 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | spin_unlock_irqrestore(µcode_update_lock, flags); |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 337 | if (val[1] != uci->mc->hdr.rev) { |
| 338 | printk(KERN_ERR "microcode: CPU%d updated from revision " |
| 339 | "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]); |
| 340 | return; |
| 341 | } |
| 342 | pr_debug("microcode: CPU%d updated from revision " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | "0x%x to 0x%x, date = %08x \n", |
| 344 | cpu_num, uci->rev, val[1], uci->mc->hdr.date); |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 345 | uci->rev = val[1]; |
| 346 | } |
| 347 | |
| 348 | #ifdef CONFIG_MICROCODE_OLD_INTERFACE |
| 349 | static void __user *user_buffer; /* user area microcode data buffer */ |
| 350 | static unsigned int user_buffer_size; /* it's size */ |
| 351 | |
| 352 | static long get_next_ucode(void **mc, long offset) |
| 353 | { |
| 354 | microcode_header_t mc_header; |
| 355 | unsigned long total_size; |
| 356 | |
| 357 | /* No more data */ |
| 358 | if (offset >= user_buffer_size) |
| 359 | return 0; |
| 360 | if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) { |
| 361 | printk(KERN_ERR "microcode: error! Can not read user data\n"); |
| 362 | return -EFAULT; |
| 363 | } |
| 364 | total_size = get_totalsize(&mc_header); |
| 365 | if ((offset + total_size > user_buffer_size) |
| 366 | || (total_size < DEFAULT_UCODE_TOTALSIZE)) { |
| 367 | printk(KERN_ERR "microcode: error! Bad total size in microcode " |
| 368 | "data file\n"); |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | *mc = vmalloc(total_size); |
| 372 | if (!*mc) |
| 373 | return -ENOMEM; |
| 374 | if (copy_from_user(*mc, user_buffer + offset, total_size)) { |
| 375 | printk(KERN_ERR "microcode: error! Can not read user data\n"); |
| 376 | vfree(*mc); |
| 377 | return -EFAULT; |
| 378 | } |
| 379 | return offset + total_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int do_microcode_update (void) |
| 383 | { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 384 | long cursor = 0; |
| 385 | int error = 0; |
| 386 | void * new_mc; |
| 387 | int cpu; |
| 388 | cpumask_t old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 390 | old = current->cpus_allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 392 | while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) { |
| 393 | error = microcode_sanity_check(new_mc); |
| 394 | if (error) |
| 395 | goto out; |
| 396 | /* |
| 397 | * It's possible the data file has multiple matching ucode, |
| 398 | * lets keep searching till the latest version |
| 399 | */ |
| 400 | for_each_online_cpu(cpu) { |
| 401 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 403 | if (!uci->valid) |
| 404 | continue; |
| 405 | set_cpus_allowed(current, cpumask_of_cpu(cpu)); |
| 406 | error = get_maching_microcode(new_mc, cpu); |
| 407 | if (error < 0) |
| 408 | goto out; |
| 409 | if (error == 1) |
| 410 | apply_microcode(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 412 | vfree(new_mc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | out: |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 415 | if (cursor > 0) |
| 416 | vfree(new_mc); |
| 417 | if (cursor < 0) |
| 418 | error = cursor; |
| 419 | set_cpus_allowed(current, old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return error; |
| 421 | } |
| 422 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 423 | static int microcode_open (struct inode *unused1, struct file *unused2) |
| 424 | { |
| 425 | return capable(CAP_SYS_RAWIO) ? 0 : -EPERM; |
| 426 | } |
| 427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos) |
| 429 | { |
| 430 | ssize_t ret; |
| 431 | |
| 432 | if (len < DEFAULT_UCODE_TOTALSIZE) { |
| 433 | printk(KERN_ERR "microcode: not enough data\n"); |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
| 437 | if ((len >> PAGE_SHIFT) > num_physpages) { |
| 438 | printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages); |
| 439 | return -EINVAL; |
| 440 | } |
| 441 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 442 | lock_cpu_hotplug(); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 443 | mutex_lock(µcode_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
| 445 | user_buffer = (void __user *) buf; |
| 446 | user_buffer_size = (int) len; |
| 447 | |
| 448 | ret = do_microcode_update(); |
| 449 | if (!ret) |
| 450 | ret = (ssize_t)len; |
| 451 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 452 | mutex_unlock(µcode_mutex); |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 453 | unlock_cpu_hotplug(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | return ret; |
| 456 | } |
| 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | static struct file_operations microcode_fops = { |
| 459 | .owner = THIS_MODULE, |
| 460 | .write = microcode_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | .open = microcode_open, |
| 462 | }; |
| 463 | |
| 464 | static struct miscdevice microcode_dev = { |
| 465 | .minor = MICROCODE_MINOR, |
| 466 | .name = "microcode", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | .fops = µcode_fops, |
| 468 | }; |
| 469 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 470 | static int __init microcode_dev_init (void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
| 472 | int error; |
| 473 | |
| 474 | error = misc_register(µcode_dev); |
| 475 | if (error) { |
| 476 | printk(KERN_ERR |
| 477 | "microcode: can't misc_register on minor=%d\n", |
| 478 | MICROCODE_MINOR); |
| 479 | return error; |
| 480 | } |
| 481 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | static void __exit microcode_dev_exit (void) |
| 486 | { |
| 487 | misc_deregister(µcode_dev); |
| 488 | } |
| 489 | |
| 490 | MODULE_ALIAS_MISCDEV(MICROCODE_MINOR); |
| 491 | #else |
| 492 | #define microcode_dev_init() 0 |
| 493 | #define microcode_dev_exit() do { } while(0) |
| 494 | #endif |
| 495 | |
| 496 | static int __init microcode_init (void) |
| 497 | { |
| 498 | int error; |
| 499 | |
| 500 | error = microcode_dev_init(); |
| 501 | if (error) |
| 502 | return error; |
| 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | printk(KERN_INFO |
| 505 | "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@veritas.com>\n"); |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static void __exit microcode_exit (void) |
| 510 | { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame^] | 511 | microcode_dev_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | module_init(microcode_init) |
| 515 | module_exit(microcode_exit) |