Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Intel CPU Microcode Update Driver for Linux |
| 3 | * |
Tigran Aivazian | 6968826 | 2006-12-13 00:35:14 -0800 | [diff] [blame] | 4 | * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk> |
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 |
Ben Castricum | bc4e0f9 | 2008-06-10 13:15:12 +0200 | [diff] [blame] | 8 | * belonging to IA-32 family - PentiumPro, Pentium II, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Pentium III, Xeon, Pentium 4, etc. |
| 10 | * |
Ben Castricum | bc4e0f9 | 2008-06-10 13:15:12 +0200 | [diff] [blame] | 11 | * Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture |
| 12 | * Software Developer's Manual |
| 13 | * Order Number 253668 or free download from: |
| 14 | * |
| 15 | * http://developer.intel.com/design/pentium4/manuals/253668.htm |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * |
| 17 | * For more information, go to http://www.urbanmyth.org/microcode |
| 18 | * |
| 19 | * This program is free software; you can redistribute it and/or |
| 20 | * modify it under the terms of the GNU General Public License |
| 21 | * as published by the Free Software Foundation; either version |
| 22 | * 2 of the License, or (at your option) any later version. |
| 23 | * |
| 24 | * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com> |
| 25 | * Initial release. |
| 26 | * 1.01 18 Feb 2000, Tigran Aivazian <tigran@sco.com> |
| 27 | * Added read() support + cleanups. |
| 28 | * 1.02 21 Feb 2000, Tigran Aivazian <tigran@sco.com> |
| 29 | * Added 'device trimming' support. open(O_WRONLY) zeroes |
| 30 | * and frees the saved copy of applied microcode. |
| 31 | * 1.03 29 Feb 2000, Tigran Aivazian <tigran@sco.com> |
| 32 | * Made to use devfs (/dev/cpu/microcode) + cleanups. |
| 33 | * 1.04 06 Jun 2000, Simon Trimmer <simon@veritas.com> |
| 34 | * Added misc device support (now uses both devfs and misc). |
| 35 | * Added MICROCODE_IOCFREE ioctl to clear memory. |
| 36 | * 1.05 09 Jun 2000, Simon Trimmer <simon@veritas.com> |
| 37 | * Messages for error cases (non Intel & no suitable microcode). |
| 38 | * 1.06 03 Aug 2000, Tigran Aivazian <tigran@veritas.com> |
| 39 | * Removed ->release(). Removed exclusive open and status bitmap. |
| 40 | * Added microcode_rwsem to serialize read()/write()/ioctl(). |
| 41 | * Removed global kernel lock usage. |
| 42 | * 1.07 07 Sep 2000, Tigran Aivazian <tigran@veritas.com> |
| 43 | * Write 0 to 0x8B msr and then cpuid before reading revision, |
| 44 | * so that it works even if there were no update done by the |
| 45 | * BIOS. Otherwise, reading from 0x8B gives junk (which happened |
| 46 | * to be 0 on my machine which is why it worked even when I |
| 47 | * disabled update by the BIOS) |
| 48 | * Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix. |
| 49 | * 1.08 11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and |
| 50 | * Tigran Aivazian <tigran@veritas.com> |
| 51 | * Intel Pentium 4 processor support and bugfixes. |
| 52 | * 1.09 30 Oct 2001, Tigran Aivazian <tigran@veritas.com> |
| 53 | * Bugfix for HT (Hyper-Threading) enabled processors |
| 54 | * whereby processor resources are shared by all logical processors |
| 55 | * in a single CPU package. |
| 56 | * 1.10 28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and |
| 57 | * Tigran Aivazian <tigran@veritas.com>, |
| 58 | * Serialize updates as required on HT processors due to speculative |
| 59 | * nature of implementation. |
| 60 | * 1.11 22 Mar 2002 Tigran Aivazian <tigran@veritas.com> |
| 61 | * Fix the panic when writing zero-length microcode chunk. |
Ben Castricum | bc4e0f9 | 2008-06-10 13:15:12 +0200 | [diff] [blame] | 62 | * 1.12 29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * Jun Nakajima <jun.nakajima@intel.com> |
| 64 | * Support for the microcode updates in the new format. |
| 65 | * 1.13 10 Oct 2003 Tigran Aivazian <tigran@veritas.com> |
| 66 | * Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl |
Ben Castricum | bc4e0f9 | 2008-06-10 13:15:12 +0200 | [diff] [blame] | 67 | * because we no longer hold a copy of applied microcode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | * in kernel memory. |
| 69 | * 1.14 25 Jun 2004 Tigran Aivazian <tigran@veritas.com> |
| 70 | * Fix sigmatch() macro to handle old CPUs with pf == 0. |
| 71 | * Thanks to Stuart Swales for pointing out this bug. |
| 72 | */ |
| 73 | |
| 74 | //#define DEBUG /* pr_debug */ |
Randy Dunlap | a941564 | 2006-01-11 12:17:48 -0800 | [diff] [blame] | 75 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #include <linux/kernel.h> |
| 77 | #include <linux/init.h> |
| 78 | #include <linux/sched.h> |
Arnd Bergmann | 7714936 | 2008-05-20 19:16:16 +0200 | [diff] [blame] | 79 | #include <linux/smp_lock.h> |
Dave Jones | 5cf6c54 | 2006-02-28 16:58:53 -0800 | [diff] [blame] | 80 | #include <linux/cpumask.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #include <linux/module.h> |
| 82 | #include <linux/slab.h> |
| 83 | #include <linux/vmalloc.h> |
| 84 | #include <linux/miscdevice.h> |
| 85 | #include <linux/spinlock.h> |
| 86 | #include <linux/mm.h> |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 87 | #include <linux/fs.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 88 | #include <linux/mutex.h> |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 89 | #include <linux/cpu.h> |
| 90 | #include <linux/firmware.h> |
| 91 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | #include <asm/msr.h> |
| 94 | #include <asm/uaccess.h> |
| 95 | #include <asm/processor.h> |
| 96 | |
| 97 | MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver"); |
Tigran Aivazian | 6968826 | 2006-12-13 00:35:14 -0800 | [diff] [blame] | 98 | MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | MODULE_LICENSE("GPL"); |
| 100 | |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 101 | #define MICROCODE_VERSION "1.14a" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */ |
| 104 | #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */ |
| 105 | #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */ |
| 106 | #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */ |
| 107 | #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */ |
| 108 | #define DWSIZE (sizeof (u32)) |
| 109 | #define get_totalsize(mc) \ |
| 110 | (((microcode_t *)mc)->hdr.totalsize ? \ |
| 111 | ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE) |
| 112 | #define get_datasize(mc) \ |
| 113 | (((microcode_t *)mc)->hdr.datasize ? \ |
| 114 | ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE) |
| 115 | |
| 116 | #define sigmatch(s1, s2, p1, p2) \ |
| 117 | (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0)))) |
| 118 | |
| 119 | #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE) |
| 120 | |
| 121 | /* serialize access to the physical write to MSR 0x79 */ |
| 122 | static DEFINE_SPINLOCK(microcode_update_lock); |
| 123 | |
| 124 | /* no concurrent ->write()s are allowed on /dev/cpu/microcode */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 125 | static DEFINE_MUTEX(microcode_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | static struct ucode_cpu_info { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 128 | int valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | unsigned int sig; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 130 | unsigned int pf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | unsigned int rev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | microcode_t *mc; |
| 133 | } ucode_cpu_info[NR_CPUS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 135 | static void collect_cpu_info(int cpu_num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 137 | struct cpuinfo_x86 *c = &cpu_data(cpu_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
| 139 | unsigned int val[2]; |
| 140 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 141 | /* We should bind the task to the CPU */ |
| 142 | BUG_ON(raw_smp_processor_id() != cpu_num); |
| 143 | uci->pf = uci->rev = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | uci->mc = NULL; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 145 | uci->valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 || |
| 148 | cpu_has(c, X86_FEATURE_IA64)) { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 149 | printk(KERN_ERR "microcode: CPU%d not a capable Intel " |
| 150 | "processor\n", cpu_num); |
| 151 | uci->valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 155 | uci->sig = cpuid_eax(0x00000001); |
| 156 | |
| 157 | if ((c->x86_model >= 5) || (c->x86 > 6)) { |
| 158 | /* get processor flags from MSR 0x17 */ |
| 159 | rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); |
| 160 | uci->pf = 1 << ((val[1] >> 18) & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | wrmsr(MSR_IA32_UCODE_REV, 0, 0); |
Zachary Amsden | 245067d | 2005-09-03 15:56:37 -0700 | [diff] [blame] | 164 | /* see notes above for revision 1.07. Apparent chip bug */ |
Andi Kleen | 487472b | 2006-01-11 22:45:27 +0100 | [diff] [blame] | 165 | sync_core(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* get the current revision from MSR 0x8B */ |
| 167 | rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev); |
| 168 | pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n", |
| 169 | uci->sig, uci->pf, uci->rev); |
| 170 | } |
| 171 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 172 | static inline int microcode_update_match(int cpu_num, |
| 173 | microcode_header_t *mc_header, int sig, int pf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
| 175 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
| 176 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 177 | if (!sigmatch(sig, uci->sig, pf, uci->pf) |
| 178 | || mc_header->rev <= uci->rev) |
| 179 | return 0; |
| 180 | return 1; |
| 181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 183 | static int microcode_sanity_check(void *mc) |
| 184 | { |
| 185 | microcode_header_t *mc_header = mc; |
| 186 | struct extended_sigtable *ext_header = NULL; |
| 187 | struct extended_signature *ext_sig; |
| 188 | unsigned long total_size, data_size, ext_table_size; |
| 189 | int sum, orig_sum, ext_sigcount = 0, i; |
| 190 | |
| 191 | total_size = get_totalsize(mc_header); |
| 192 | data_size = get_datasize(mc_header); |
Shaohua Li | bd8e39f | 2006-09-27 01:50:54 -0700 | [diff] [blame] | 193 | if (data_size + MC_HEADER_SIZE > total_size) { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 194 | printk(KERN_ERR "microcode: error! " |
| 195 | "Bad data size in microcode data file\n"); |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | |
| 199 | if (mc_header->ldrver != 1 || mc_header->hdrver != 1) { |
| 200 | printk(KERN_ERR "microcode: error! " |
| 201 | "Unknown microcode update format\n"); |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | ext_table_size = total_size - (MC_HEADER_SIZE + data_size); |
| 205 | if (ext_table_size) { |
| 206 | if ((ext_table_size < EXT_HEADER_SIZE) |
| 207 | || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) { |
| 208 | printk(KERN_ERR "microcode: error! " |
| 209 | "Small exttable size in microcode data file\n"); |
| 210 | return -EINVAL; |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 211 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 212 | ext_header = mc + MC_HEADER_SIZE + data_size; |
| 213 | if (ext_table_size != exttable_size(ext_header)) { |
| 214 | printk(KERN_ERR "microcode: error! " |
| 215 | "Bad exttable size in microcode data file\n"); |
| 216 | return -EFAULT; |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 217 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 218 | ext_sigcount = ext_header->count; |
| 219 | } |
| 220 | |
| 221 | /* check extended table checksum */ |
| 222 | if (ext_table_size) { |
| 223 | int ext_table_sum = 0; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 224 | int *ext_tablep = (int *)ext_header; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 225 | |
| 226 | i = ext_table_size / DWSIZE; |
| 227 | while (i--) |
| 228 | ext_table_sum += ext_tablep[i]; |
| 229 | if (ext_table_sum) { |
| 230 | printk(KERN_WARNING "microcode: aborting, " |
| 231 | "bad extended signature table checksum\n"); |
| 232 | return -EINVAL; |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 233 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 235 | |
| 236 | /* calculate the checksum */ |
| 237 | orig_sum = 0; |
| 238 | i = (MC_HEADER_SIZE + data_size) / DWSIZE; |
| 239 | while (i--) |
| 240 | orig_sum += ((int *)mc)[i]; |
| 241 | if (orig_sum) { |
| 242 | printk(KERN_ERR "microcode: aborting, bad checksum\n"); |
| 243 | return -EINVAL; |
| 244 | } |
| 245 | if (!ext_table_size) |
| 246 | return 0; |
| 247 | /* check extended signature checksum */ |
| 248 | for (i = 0; i < ext_sigcount; i++) { |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 249 | ext_sig = (void *)ext_header + EXT_HEADER_SIZE + |
| 250 | EXT_SIGNATURE_SIZE * i; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 251 | sum = orig_sum |
| 252 | - (mc_header->sig + mc_header->pf + mc_header->cksum) |
| 253 | + (ext_sig->sig + ext_sig->pf + ext_sig->cksum); |
| 254 | if (sum) { |
| 255 | printk(KERN_ERR "microcode: aborting, bad checksum\n"); |
| 256 | return -EINVAL; |
| 257 | } |
| 258 | } |
| 259 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 262 | /* |
| 263 | * return 0 - no update found |
| 264 | * return 1 - found update |
| 265 | * return < 0 - error |
| 266 | */ |
| 267 | static int get_maching_microcode(void *mc, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 269 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 270 | microcode_header_t *mc_header = mc; |
| 271 | struct extended_sigtable *ext_header; |
| 272 | unsigned long total_size = get_totalsize(mc_header); |
| 273 | int ext_sigcount, i; |
| 274 | struct extended_signature *ext_sig; |
| 275 | void *new_mc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 277 | if (microcode_update_match(cpu, mc_header, |
| 278 | mc_header->sig, mc_header->pf)) |
| 279 | goto find; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 281 | if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE) |
| 282 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 284 | ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 285 | ext_sigcount = ext_header->count; |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 286 | ext_sig = (void *)ext_header + EXT_HEADER_SIZE; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 287 | for (i = 0; i < ext_sigcount; i++) { |
| 288 | if (microcode_update_match(cpu, mc_header, |
| 289 | ext_sig->sig, ext_sig->pf)) |
| 290 | goto find; |
| 291 | ext_sig++; |
| 292 | } |
| 293 | return 0; |
| 294 | find: |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame] | 295 | pr_debug("microcode: CPU%d found a matching microcode update with" |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 296 | " version 0x%x (current=0x%x)\n", cpu, mc_header->rev,uci->rev); |
| 297 | new_mc = vmalloc(total_size); |
| 298 | if (!new_mc) { |
| 299 | printk(KERN_ERR "microcode: error! Can not allocate memory\n"); |
| 300 | return -ENOMEM; |
| 301 | } |
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 | /* free previous update file */ |
| 304 | vfree(uci->mc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 306 | memcpy(new_mc, mc, total_size); |
| 307 | uci->mc = new_mc; |
| 308 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 311 | static void apply_microcode(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
| 313 | unsigned long flags; |
| 314 | unsigned int val[2]; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 315 | int cpu_num = raw_smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
| 317 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 318 | /* We should bind the task to the CPU */ |
| 319 | BUG_ON(cpu_num != cpu); |
| 320 | |
| 321 | if (uci->mc == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
| 324 | /* serialize access to the physical write to MSR 0x79 */ |
Ben Castricum | bc4e0f9 | 2008-06-10 13:15:12 +0200 | [diff] [blame] | 325 | spin_lock_irqsave(µcode_update_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
| 327 | /* write microcode via MSR 0x79 */ |
| 328 | wrmsr(MSR_IA32_UCODE_WRITE, |
Ben Castricum | bc4e0f9 | 2008-06-10 13:15:12 +0200 | [diff] [blame] | 329 | (unsigned long) uci->mc->bits, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | (unsigned long) uci->mc->bits >> 16 >> 16); |
| 331 | wrmsr(MSR_IA32_UCODE_REV, 0, 0); |
| 332 | |
Zachary Amsden | 245067d | 2005-09-03 15:56:37 -0700 | [diff] [blame] | 333 | /* see notes above for revision 1.07. Apparent chip bug */ |
Andi Kleen | 487472b | 2006-01-11 22:45:27 +0100 | [diff] [blame] | 334 | sync_core(); |
Zachary Amsden | 245067d | 2005-09-03 15:56:37 -0700 | [diff] [blame] | 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | /* get the current revision from MSR 0x8B */ |
| 337 | rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]); |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | spin_unlock_irqrestore(µcode_update_lock, flags); |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 340 | if (val[1] != uci->mc->hdr.rev) { |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame] | 341 | printk(KERN_ERR "microcode: CPU%d update from revision " |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 342 | "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]); |
| 343 | return; |
| 344 | } |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame] | 345 | printk(KERN_INFO "microcode: CPU%d updated from revision " |
Ben Castricum | bc4e0f9 | 2008-06-10 13:15:12 +0200 | [diff] [blame] | 346 | "0x%x to 0x%x, date = %08x \n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | cpu_num, uci->rev, val[1], uci->mc->hdr.date); |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 348 | uci->rev = val[1]; |
| 349 | } |
| 350 | |
| 351 | #ifdef CONFIG_MICROCODE_OLD_INTERFACE |
| 352 | static void __user *user_buffer; /* user area microcode data buffer */ |
| 353 | static unsigned int user_buffer_size; /* it's size */ |
| 354 | |
| 355 | static long get_next_ucode(void **mc, long offset) |
| 356 | { |
| 357 | microcode_header_t mc_header; |
| 358 | unsigned long total_size; |
| 359 | |
| 360 | /* No more data */ |
| 361 | if (offset >= user_buffer_size) |
| 362 | return 0; |
| 363 | if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) { |
| 364 | printk(KERN_ERR "microcode: error! Can not read user data\n"); |
| 365 | return -EFAULT; |
| 366 | } |
| 367 | total_size = get_totalsize(&mc_header); |
Shaohua Li | bd8e39f | 2006-09-27 01:50:54 -0700 | [diff] [blame] | 368 | if (offset + total_size > user_buffer_size) { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 369 | printk(KERN_ERR "microcode: error! Bad total size in microcode " |
| 370 | "data file\n"); |
| 371 | return -EINVAL; |
| 372 | } |
| 373 | *mc = vmalloc(total_size); |
| 374 | if (!*mc) |
| 375 | return -ENOMEM; |
| 376 | if (copy_from_user(*mc, user_buffer + offset, total_size)) { |
| 377 | printk(KERN_ERR "microcode: error! Can not read user data\n"); |
| 378 | vfree(*mc); |
| 379 | return -EFAULT; |
| 380 | } |
| 381 | return offset + total_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static int do_microcode_update (void) |
| 385 | { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 386 | long cursor = 0; |
| 387 | int error = 0; |
Andi Kleen | 2ba1ff2 | 2007-02-13 13:26:25 +0100 | [diff] [blame] | 388 | void *new_mc = NULL; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 389 | int cpu; |
| 390 | cpumask_t old; |
Mike Travis | 65c0118 | 2008-07-15 14:14:30 -0700 | [diff] [blame^] | 391 | cpumask_of_cpu_ptr_declare(newmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 393 | old = current->cpus_allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 395 | while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) { |
| 396 | error = microcode_sanity_check(new_mc); |
| 397 | if (error) |
| 398 | goto out; |
| 399 | /* |
| 400 | * It's possible the data file has multiple matching ucode, |
| 401 | * lets keep searching till the latest version |
| 402 | */ |
| 403 | for_each_online_cpu(cpu) { |
| 404 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 406 | if (!uci->valid) |
| 407 | continue; |
Mike Travis | 65c0118 | 2008-07-15 14:14:30 -0700 | [diff] [blame^] | 408 | cpumask_of_cpu_ptr_next(newmask, cpu); |
| 409 | set_cpus_allowed_ptr(current, newmask); |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 410 | error = get_maching_microcode(new_mc, cpu); |
| 411 | if (error < 0) |
| 412 | goto out; |
| 413 | if (error == 1) |
| 414 | apply_microcode(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 416 | vfree(new_mc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | out: |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 419 | if (cursor > 0) |
| 420 | vfree(new_mc); |
| 421 | if (cursor < 0) |
| 422 | error = cursor; |
Mike Travis | fc0e474 | 2008-04-04 18:11:05 -0700 | [diff] [blame] | 423 | set_cpus_allowed_ptr(current, &old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return error; |
| 425 | } |
| 426 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 427 | static int microcode_open (struct inode *unused1, struct file *unused2) |
| 428 | { |
Arnd Bergmann | 7714936 | 2008-05-20 19:16:16 +0200 | [diff] [blame] | 429 | cycle_kernel_lock(); |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 430 | return capable(CAP_SYS_RAWIO) ? 0 : -EPERM; |
| 431 | } |
| 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos) |
| 434 | { |
| 435 | ssize_t ret; |
| 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 442 | get_online_cpus(); |
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); |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 453 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | return ret; |
| 456 | } |
| 457 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 458 | static const struct file_operations microcode_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sam Ravnborg | f8281a2 | 2007-06-01 00:47:10 -0700 | [diff] [blame] | 485 | static void microcode_dev_exit (void) |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 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 | |
Greg Kroah-Hartman | a13b04a | 2008-05-29 10:05:08 -0700 | [diff] [blame] | 496 | static long get_next_ucode_from_buffer(void **mc, const u8 *buf, |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 497 | unsigned long size, long offset) |
| 498 | { |
| 499 | microcode_header_t *mc_header; |
| 500 | unsigned long total_size; |
| 501 | |
| 502 | /* No more data */ |
| 503 | if (offset >= size) |
| 504 | return 0; |
| 505 | mc_header = (microcode_header_t *)(buf + offset); |
| 506 | total_size = get_totalsize(mc_header); |
| 507 | |
Shaohua Li | bd8e39f | 2006-09-27 01:50:54 -0700 | [diff] [blame] | 508 | if (offset + total_size > size) { |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 509 | printk(KERN_ERR "microcode: error! Bad data in microcode data file\n"); |
| 510 | return -EINVAL; |
| 511 | } |
| 512 | |
| 513 | *mc = vmalloc(total_size); |
| 514 | if (!*mc) { |
| 515 | printk(KERN_ERR "microcode: error! Can not allocate memory\n"); |
| 516 | return -ENOMEM; |
| 517 | } |
| 518 | memcpy(*mc, buf + offset, total_size); |
| 519 | return offset + total_size; |
| 520 | } |
| 521 | |
| 522 | /* fake device for request_firmware */ |
| 523 | static struct platform_device *microcode_pdev; |
| 524 | |
| 525 | static int cpu_request_microcode(int cpu) |
| 526 | { |
| 527 | char name[30]; |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 528 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 529 | const struct firmware *firmware; |
Greg Kroah-Hartman | a13b04a | 2008-05-29 10:05:08 -0700 | [diff] [blame] | 530 | const u8 *buf; |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 531 | unsigned long size; |
| 532 | long offset = 0; |
| 533 | int error; |
| 534 | void *mc; |
| 535 | |
| 536 | /* We should bind the task to the CPU */ |
| 537 | BUG_ON(cpu != raw_smp_processor_id()); |
| 538 | sprintf(name,"intel-ucode/%02x-%02x-%02x", |
| 539 | c->x86, c->x86_model, c->x86_mask); |
| 540 | error = request_firmware(&firmware, name, µcode_pdev->dev); |
| 541 | if (error) { |
Ben Castricum | bc4e0f9 | 2008-06-10 13:15:12 +0200 | [diff] [blame] | 542 | pr_debug("microcode: data file %s load failed\n", name); |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 543 | return error; |
| 544 | } |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 545 | buf = firmware->data; |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 546 | size = firmware->size; |
| 547 | while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset)) |
| 548 | > 0) { |
| 549 | error = microcode_sanity_check(mc); |
| 550 | if (error) |
| 551 | break; |
| 552 | error = get_maching_microcode(mc, cpu); |
| 553 | if (error < 0) |
| 554 | break; |
| 555 | /* |
| 556 | * It's possible the data file has multiple matching ucode, |
| 557 | * lets keep searching till the latest version |
| 558 | */ |
| 559 | if (error == 1) { |
| 560 | apply_microcode(cpu); |
| 561 | error = 0; |
| 562 | } |
| 563 | vfree(mc); |
| 564 | } |
| 565 | if (offset > 0) |
| 566 | vfree(mc); |
| 567 | if (offset < 0) |
| 568 | error = offset; |
| 569 | release_firmware(firmware); |
| 570 | |
| 571 | return error; |
| 572 | } |
| 573 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 574 | static int apply_microcode_check_cpu(int cpu) |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 575 | { |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 576 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 577 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 578 | cpumask_t old; |
Mike Travis | 65c0118 | 2008-07-15 14:14:30 -0700 | [diff] [blame^] | 579 | cpumask_of_cpu_ptr(newmask, cpu); |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 580 | unsigned int val[2]; |
| 581 | int err = 0; |
| 582 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 583 | /* Check if the microcode is available */ |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 584 | if (!uci->mc) |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 585 | return 0; |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 586 | |
| 587 | old = current->cpus_allowed; |
Mike Travis | 65c0118 | 2008-07-15 14:14:30 -0700 | [diff] [blame^] | 588 | set_cpus_allowed_ptr(current, newmask); |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 589 | |
| 590 | /* Check if the microcode we have in memory matches the CPU */ |
| 591 | if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 || |
| 592 | cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001)) |
| 593 | err = -EINVAL; |
| 594 | |
| 595 | if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) { |
| 596 | /* get processor flags from MSR 0x17 */ |
| 597 | rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); |
| 598 | if (uci->pf != (1 << ((val[1] >> 18) & 7))) |
| 599 | err = -EINVAL; |
| 600 | } |
| 601 | |
| 602 | if (!err) { |
| 603 | wrmsr(MSR_IA32_UCODE_REV, 0, 0); |
| 604 | /* see notes above for revision 1.07. Apparent chip bug */ |
| 605 | sync_core(); |
| 606 | /* get the current revision from MSR 0x8B */ |
| 607 | rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]); |
| 608 | if (uci->rev != val[1]) |
| 609 | err = -EINVAL; |
| 610 | } |
| 611 | |
| 612 | if (!err) |
| 613 | apply_microcode(cpu); |
| 614 | else |
| 615 | printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:" |
| 616 | " sig=0x%x, pf=0x%x, rev=0x%x\n", |
| 617 | cpu, uci->sig, uci->pf, uci->rev); |
| 618 | |
Mike Travis | fc0e474 | 2008-04-04 18:11:05 -0700 | [diff] [blame] | 619 | set_cpus_allowed_ptr(current, &old); |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 620 | return err; |
| 621 | } |
| 622 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 623 | static void microcode_init_cpu(int cpu, int resume) |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 624 | { |
| 625 | cpumask_t old; |
Mike Travis | 65c0118 | 2008-07-15 14:14:30 -0700 | [diff] [blame^] | 626 | cpumask_of_cpu_ptr(newmask, cpu); |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 627 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 628 | |
| 629 | old = current->cpus_allowed; |
| 630 | |
Mike Travis | 65c0118 | 2008-07-15 14:14:30 -0700 | [diff] [blame^] | 631 | set_cpus_allowed_ptr(current, newmask); |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 632 | mutex_lock(µcode_mutex); |
| 633 | collect_cpu_info(cpu); |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 634 | if (uci->valid && system_state == SYSTEM_RUNNING && !resume) |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 635 | cpu_request_microcode(cpu); |
| 636 | mutex_unlock(µcode_mutex); |
Mike Travis | fc0e474 | 2008-04-04 18:11:05 -0700 | [diff] [blame] | 637 | set_cpus_allowed_ptr(current, &old); |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static void microcode_fini_cpu(int cpu) |
| 641 | { |
| 642 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 643 | |
| 644 | mutex_lock(µcode_mutex); |
| 645 | uci->valid = 0; |
| 646 | vfree(uci->mc); |
| 647 | uci->mc = NULL; |
| 648 | mutex_unlock(µcode_mutex); |
| 649 | } |
| 650 | |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 651 | static ssize_t reload_store(struct sys_device *dev, const char *buf, size_t sz) |
| 652 | { |
| 653 | struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; |
| 654 | char *end; |
| 655 | unsigned long val = simple_strtoul(buf, &end, 0); |
| 656 | int err = 0; |
| 657 | int cpu = dev->id; |
| 658 | |
| 659 | if (end == buf) |
| 660 | return -EINVAL; |
| 661 | if (val == 1) { |
| 662 | cpumask_t old; |
Mike Travis | 65c0118 | 2008-07-15 14:14:30 -0700 | [diff] [blame^] | 663 | cpumask_of_cpu_ptr(newmask, cpu); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 664 | |
| 665 | old = current->cpus_allowed; |
| 666 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 667 | get_online_cpus(); |
Mike Travis | 65c0118 | 2008-07-15 14:14:30 -0700 | [diff] [blame^] | 668 | set_cpus_allowed_ptr(current, newmask); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 669 | |
| 670 | mutex_lock(µcode_mutex); |
| 671 | if (uci->valid) |
| 672 | err = cpu_request_microcode(cpu); |
| 673 | mutex_unlock(µcode_mutex); |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 674 | put_online_cpus(); |
Mike Travis | fc0e474 | 2008-04-04 18:11:05 -0700 | [diff] [blame] | 675 | set_cpus_allowed_ptr(current, &old); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 676 | } |
| 677 | if (err) |
| 678 | return err; |
| 679 | return sz; |
| 680 | } |
| 681 | |
| 682 | static ssize_t version_show(struct sys_device *dev, char *buf) |
| 683 | { |
| 684 | struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; |
| 685 | |
| 686 | return sprintf(buf, "0x%x\n", uci->rev); |
| 687 | } |
| 688 | |
| 689 | static ssize_t pf_show(struct sys_device *dev, char *buf) |
| 690 | { |
| 691 | struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; |
| 692 | |
| 693 | return sprintf(buf, "0x%x\n", uci->pf); |
| 694 | } |
| 695 | |
| 696 | static SYSDEV_ATTR(reload, 0200, NULL, reload_store); |
| 697 | static SYSDEV_ATTR(version, 0400, version_show, NULL); |
| 698 | static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL); |
| 699 | |
| 700 | static struct attribute *mc_default_attrs[] = { |
| 701 | &attr_reload.attr, |
| 702 | &attr_version.attr, |
| 703 | &attr_processor_flags.attr, |
| 704 | NULL |
| 705 | }; |
| 706 | |
| 707 | static struct attribute_group mc_attr_group = { |
| 708 | .attrs = mc_default_attrs, |
| 709 | .name = "microcode", |
| 710 | }; |
| 711 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 712 | static int __mc_sysdev_add(struct sys_device *sys_dev, int resume) |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 713 | { |
Jeff Garzik | 2e3ad8a | 2006-10-11 01:21:53 -0700 | [diff] [blame] | 714 | int err, cpu = sys_dev->id; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 715 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 716 | |
| 717 | if (!cpu_online(cpu)) |
| 718 | return 0; |
Jeff Garzik | 2e3ad8a | 2006-10-11 01:21:53 -0700 | [diff] [blame] | 719 | |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame] | 720 | pr_debug("microcode: CPU%d added\n", cpu); |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 721 | memset(uci, 0, sizeof(*uci)); |
Jeff Garzik | 2e3ad8a | 2006-10-11 01:21:53 -0700 | [diff] [blame] | 722 | |
| 723 | err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group); |
| 724 | if (err) |
| 725 | return err; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 726 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 727 | microcode_init_cpu(cpu, resume); |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 728 | |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 729 | return 0; |
| 730 | } |
| 731 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 732 | static int mc_sysdev_add(struct sys_device *sys_dev) |
| 733 | { |
| 734 | return __mc_sysdev_add(sys_dev, 0); |
| 735 | } |
| 736 | |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 737 | static int mc_sysdev_remove(struct sys_device *sys_dev) |
| 738 | { |
| 739 | int cpu = sys_dev->id; |
| 740 | |
| 741 | if (!cpu_online(cpu)) |
| 742 | return 0; |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 743 | |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame] | 744 | pr_debug("microcode: CPU%d removed\n", cpu); |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 745 | microcode_fini_cpu(cpu); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 746 | sysfs_remove_group(&sys_dev->kobj, &mc_attr_group); |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | static int mc_sysdev_resume(struct sys_device *dev) |
| 751 | { |
| 752 | int cpu = dev->id; |
| 753 | |
| 754 | if (!cpu_online(cpu)) |
| 755 | return 0; |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame] | 756 | pr_debug("microcode: CPU%d resumed\n", cpu); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 757 | /* only CPU 0 will apply ucode here */ |
| 758 | apply_microcode(0); |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static struct sysdev_driver mc_sysdev_driver = { |
| 763 | .add = mc_sysdev_add, |
| 764 | .remove = mc_sysdev_remove, |
| 765 | .resume = mc_sysdev_resume, |
| 766 | }; |
| 767 | |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 768 | static __cpuinit int |
| 769 | mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu) |
| 770 | { |
| 771 | unsigned int cpu = (unsigned long)hcpu; |
| 772 | struct sys_device *sys_dev; |
| 773 | |
| 774 | sys_dev = get_cpu_sysdev(cpu); |
| 775 | switch (action) { |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 776 | case CPU_UP_CANCELED_FROZEN: |
| 777 | /* The CPU refused to come up during a system resume */ |
| 778 | microcode_fini_cpu(cpu); |
| 779 | break; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 780 | case CPU_ONLINE: |
| 781 | case CPU_DOWN_FAILED: |
| 782 | mc_sysdev_add(sys_dev); |
| 783 | break; |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 784 | case CPU_ONLINE_FROZEN: |
| 785 | /* System-wide resume is in progress, try to apply microcode */ |
| 786 | if (apply_microcode_check_cpu(cpu)) { |
| 787 | /* The application of microcode failed */ |
| 788 | microcode_fini_cpu(cpu); |
| 789 | __mc_sysdev_add(sys_dev, 1); |
| 790 | break; |
| 791 | } |
| 792 | case CPU_DOWN_FAILED_FROZEN: |
| 793 | if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group)) |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame] | 794 | printk(KERN_ERR "microcode: Failed to create the sysfs " |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 795 | "group for CPU%d\n", cpu); |
| 796 | break; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 797 | case CPU_DOWN_PREPARE: |
| 798 | mc_sysdev_remove(sys_dev); |
| 799 | break; |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 800 | case CPU_DOWN_PREPARE_FROZEN: |
| 801 | /* Suspend is in progress, only remove the interface */ |
| 802 | sysfs_remove_group(&sys_dev->kobj, &mc_attr_group); |
| 803 | break; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 804 | } |
| 805 | return NOTIFY_OK; |
| 806 | } |
| 807 | |
Sam Ravnborg | c72258c | 2008-02-01 17:49:42 +0100 | [diff] [blame] | 808 | static struct notifier_block __refdata mc_cpu_notifier = { |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 809 | .notifier_call = mc_cpu_callback, |
| 810 | }; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 811 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 812 | static int __init microcode_init (void) |
| 813 | { |
| 814 | int error; |
| 815 | |
Ben Castricum | bc4e0f9 | 2008-06-10 13:15:12 +0200 | [diff] [blame] | 816 | printk(KERN_INFO |
| 817 | "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@aivazian.fsnet.co.uk>\n"); |
| 818 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 819 | error = microcode_dev_init(); |
| 820 | if (error) |
| 821 | return error; |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 822 | microcode_pdev = platform_device_register_simple("microcode", -1, |
| 823 | NULL, 0); |
| 824 | if (IS_ERR(microcode_pdev)) { |
| 825 | microcode_dev_exit(); |
| 826 | return PTR_ERR(microcode_pdev); |
| 827 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 828 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 829 | get_online_cpus(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 830 | error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver); |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 831 | put_online_cpus(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 832 | if (error) { |
| 833 | microcode_dev_exit(); |
| 834 | platform_device_unregister(microcode_pdev); |
| 835 | return error; |
| 836 | } |
| 837 | |
| 838 | register_hotcpu_notifier(&mc_cpu_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static void __exit microcode_exit (void) |
| 843 | { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 844 | microcode_dev_exit(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 845 | |
| 846 | unregister_hotcpu_notifier(&mc_cpu_notifier); |
| 847 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 848 | get_online_cpus(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 849 | sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver); |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 850 | put_online_cpus(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 851 | |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 852 | platform_device_unregister(microcode_pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | module_init(microcode_init) |
| 856 | module_exit(microcode_exit) |