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 |
| 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> |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 85 | #include <linux/fs.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 86 | #include <linux/mutex.h> |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 87 | #include <linux/cpu.h> |
| 88 | #include <linux/firmware.h> |
| 89 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | #include <asm/msr.h> |
| 92 | #include <asm/uaccess.h> |
| 93 | #include <asm/processor.h> |
| 94 | |
| 95 | MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver"); |
Tigran Aivazian | 6968826 | 2006-12-13 00:35:14 -0800 | [diff] [blame] | 96 | MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | MODULE_LICENSE("GPL"); |
| 98 | |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 99 | #define MICROCODE_VERSION "1.14a" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */ |
| 102 | #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */ |
| 103 | #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */ |
| 104 | #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */ |
| 105 | #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */ |
| 106 | #define DWSIZE (sizeof (u32)) |
| 107 | #define get_totalsize(mc) \ |
| 108 | (((microcode_t *)mc)->hdr.totalsize ? \ |
| 109 | ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE) |
| 110 | #define get_datasize(mc) \ |
| 111 | (((microcode_t *)mc)->hdr.datasize ? \ |
| 112 | ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE) |
| 113 | |
| 114 | #define sigmatch(s1, s2, p1, p2) \ |
| 115 | (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0)))) |
| 116 | |
| 117 | #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE) |
| 118 | |
| 119 | /* serialize access to the physical write to MSR 0x79 */ |
| 120 | static DEFINE_SPINLOCK(microcode_update_lock); |
| 121 | |
| 122 | /* no concurrent ->write()s are allowed on /dev/cpu/microcode */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 123 | static DEFINE_MUTEX(microcode_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | static struct ucode_cpu_info { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 126 | int valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | unsigned int sig; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 128 | unsigned int pf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | unsigned int rev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | microcode_t *mc; |
| 131 | } ucode_cpu_info[NR_CPUS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 133 | static void collect_cpu_info(int cpu_num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 135 | struct cpuinfo_x86 *c = &cpu_data(cpu_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
| 137 | unsigned int val[2]; |
| 138 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 139 | /* We should bind the task to the CPU */ |
| 140 | BUG_ON(raw_smp_processor_id() != cpu_num); |
| 141 | uci->pf = uci->rev = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | uci->mc = NULL; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 143 | uci->valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 || |
| 146 | cpu_has(c, X86_FEATURE_IA64)) { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 147 | printk(KERN_ERR "microcode: CPU%d not a capable Intel " |
| 148 | "processor\n", cpu_num); |
| 149 | uci->valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | return; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 153 | uci->sig = cpuid_eax(0x00000001); |
| 154 | |
| 155 | if ((c->x86_model >= 5) || (c->x86 > 6)) { |
| 156 | /* get processor flags from MSR 0x17 */ |
| 157 | rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); |
| 158 | uci->pf = 1 << ((val[1] >> 18) & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | wrmsr(MSR_IA32_UCODE_REV, 0, 0); |
Zachary Amsden | 245067d | 2005-09-03 15:56:37 -0700 | [diff] [blame] | 162 | /* see notes above for revision 1.07. Apparent chip bug */ |
Andi Kleen | 487472b | 2006-01-11 22:45:27 +0100 | [diff] [blame] | 163 | sync_core(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | /* get the current revision from MSR 0x8B */ |
| 165 | rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev); |
| 166 | pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n", |
| 167 | uci->sig, uci->pf, uci->rev); |
| 168 | } |
| 169 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 170 | static inline int microcode_update_match(int cpu_num, |
| 171 | microcode_header_t *mc_header, int sig, int pf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
| 174 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 175 | if (!sigmatch(sig, uci->sig, pf, uci->pf) |
| 176 | || mc_header->rev <= uci->rev) |
| 177 | return 0; |
| 178 | return 1; |
| 179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 181 | static int microcode_sanity_check(void *mc) |
| 182 | { |
| 183 | microcode_header_t *mc_header = mc; |
| 184 | struct extended_sigtable *ext_header = NULL; |
| 185 | struct extended_signature *ext_sig; |
| 186 | unsigned long total_size, data_size, ext_table_size; |
| 187 | int sum, orig_sum, ext_sigcount = 0, i; |
| 188 | |
| 189 | total_size = get_totalsize(mc_header); |
| 190 | data_size = get_datasize(mc_header); |
Shaohua Li | bd8e39f | 2006-09-27 01:50:54 -0700 | [diff] [blame] | 191 | if (data_size + MC_HEADER_SIZE > total_size) { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 192 | printk(KERN_ERR "microcode: error! " |
| 193 | "Bad data size in microcode data file\n"); |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | |
| 197 | if (mc_header->ldrver != 1 || mc_header->hdrver != 1) { |
| 198 | printk(KERN_ERR "microcode: error! " |
| 199 | "Unknown microcode update format\n"); |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | ext_table_size = total_size - (MC_HEADER_SIZE + data_size); |
| 203 | if (ext_table_size) { |
| 204 | if ((ext_table_size < EXT_HEADER_SIZE) |
| 205 | || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) { |
| 206 | printk(KERN_ERR "microcode: error! " |
| 207 | "Small exttable size in microcode data file\n"); |
| 208 | return -EINVAL; |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 209 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 210 | ext_header = mc + MC_HEADER_SIZE + data_size; |
| 211 | if (ext_table_size != exttable_size(ext_header)) { |
| 212 | printk(KERN_ERR "microcode: error! " |
| 213 | "Bad exttable size in microcode data file\n"); |
| 214 | return -EFAULT; |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 215 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 216 | ext_sigcount = ext_header->count; |
| 217 | } |
| 218 | |
| 219 | /* check extended table checksum */ |
| 220 | if (ext_table_size) { |
| 221 | int ext_table_sum = 0; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 222 | int *ext_tablep = (int *)ext_header; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 223 | |
| 224 | i = ext_table_size / DWSIZE; |
| 225 | while (i--) |
| 226 | ext_table_sum += ext_tablep[i]; |
| 227 | if (ext_table_sum) { |
| 228 | printk(KERN_WARNING "microcode: aborting, " |
| 229 | "bad extended signature table checksum\n"); |
| 230 | return -EINVAL; |
Jan Beulich | ba528f2 | 2006-06-23 02:04:19 -0700 | [diff] [blame] | 231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 233 | |
| 234 | /* calculate the checksum */ |
| 235 | orig_sum = 0; |
| 236 | i = (MC_HEADER_SIZE + data_size) / DWSIZE; |
| 237 | while (i--) |
| 238 | orig_sum += ((int *)mc)[i]; |
| 239 | if (orig_sum) { |
| 240 | printk(KERN_ERR "microcode: aborting, bad checksum\n"); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | if (!ext_table_size) |
| 244 | return 0; |
| 245 | /* check extended signature checksum */ |
| 246 | for (i = 0; i < ext_sigcount; i++) { |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 247 | ext_sig = (void *)ext_header + EXT_HEADER_SIZE + |
| 248 | EXT_SIGNATURE_SIZE * i; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 249 | sum = orig_sum |
| 250 | - (mc_header->sig + mc_header->pf + mc_header->cksum) |
| 251 | + (ext_sig->sig + ext_sig->pf + ext_sig->cksum); |
| 252 | if (sum) { |
| 253 | printk(KERN_ERR "microcode: aborting, bad checksum\n"); |
| 254 | return -EINVAL; |
| 255 | } |
| 256 | } |
| 257 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 260 | /* |
| 261 | * return 0 - no update found |
| 262 | * return 1 - found update |
| 263 | * return < 0 - error |
| 264 | */ |
| 265 | static int get_maching_microcode(void *mc, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 267 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 268 | microcode_header_t *mc_header = mc; |
| 269 | struct extended_sigtable *ext_header; |
| 270 | unsigned long total_size = get_totalsize(mc_header); |
| 271 | int ext_sigcount, i; |
| 272 | struct extended_signature *ext_sig; |
| 273 | void *new_mc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 275 | if (microcode_update_match(cpu, mc_header, |
| 276 | mc_header->sig, mc_header->pf)) |
| 277 | goto find; |
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 | if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE) |
| 280 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 282 | ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 283 | ext_sigcount = ext_header->count; |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 284 | ext_sig = (void *)ext_header + EXT_HEADER_SIZE; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 285 | for (i = 0; i < ext_sigcount; i++) { |
| 286 | if (microcode_update_match(cpu, mc_header, |
| 287 | ext_sig->sig, ext_sig->pf)) |
| 288 | goto find; |
| 289 | ext_sig++; |
| 290 | } |
| 291 | return 0; |
| 292 | find: |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame^] | 293 | pr_debug("microcode: CPU%d found a matching microcode update with" |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 294 | " version 0x%x (current=0x%x)\n", cpu, mc_header->rev,uci->rev); |
| 295 | new_mc = vmalloc(total_size); |
| 296 | if (!new_mc) { |
| 297 | printk(KERN_ERR "microcode: error! Can not allocate memory\n"); |
| 298 | return -ENOMEM; |
| 299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 301 | /* free previous update file */ |
| 302 | vfree(uci->mc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 304 | memcpy(new_mc, mc, total_size); |
| 305 | uci->mc = new_mc; |
| 306 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 309 | static void apply_microcode(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
| 311 | unsigned long flags; |
| 312 | unsigned int val[2]; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 313 | int cpu_num = raw_smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
| 315 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 316 | /* We should bind the task to the CPU */ |
| 317 | BUG_ON(cpu_num != cpu); |
| 318 | |
| 319 | if (uci->mc == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 322 | /* serialize access to the physical write to MSR 0x79 */ |
| 323 | spin_lock_irqsave(µcode_update_lock, flags); |
| 324 | |
| 325 | /* write microcode via MSR 0x79 */ |
| 326 | wrmsr(MSR_IA32_UCODE_WRITE, |
| 327 | (unsigned long) uci->mc->bits, |
| 328 | (unsigned long) uci->mc->bits >> 16 >> 16); |
| 329 | wrmsr(MSR_IA32_UCODE_REV, 0, 0); |
| 330 | |
Zachary Amsden | 245067d | 2005-09-03 15:56:37 -0700 | [diff] [blame] | 331 | /* see notes above for revision 1.07. Apparent chip bug */ |
Andi Kleen | 487472b | 2006-01-11 22:45:27 +0100 | [diff] [blame] | 332 | sync_core(); |
Zachary Amsden | 245067d | 2005-09-03 15:56:37 -0700 | [diff] [blame] | 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | /* get the current revision from MSR 0x8B */ |
| 335 | rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]); |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | spin_unlock_irqrestore(µcode_update_lock, flags); |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 338 | if (val[1] != uci->mc->hdr.rev) { |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame^] | 339 | printk(KERN_ERR "microcode: CPU%d update from revision " |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 340 | "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]); |
| 341 | return; |
| 342 | } |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame^] | 343 | printk(KERN_INFO "microcode: CPU%d updated from revision " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | "0x%x to 0x%x, date = %08x \n", |
| 345 | cpu_num, uci->rev, val[1], uci->mc->hdr.date); |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 346 | uci->rev = val[1]; |
| 347 | } |
| 348 | |
| 349 | #ifdef CONFIG_MICROCODE_OLD_INTERFACE |
| 350 | static void __user *user_buffer; /* user area microcode data buffer */ |
| 351 | static unsigned int user_buffer_size; /* it's size */ |
| 352 | |
| 353 | static long get_next_ucode(void **mc, long offset) |
| 354 | { |
| 355 | microcode_header_t mc_header; |
| 356 | unsigned long total_size; |
| 357 | |
| 358 | /* No more data */ |
| 359 | if (offset >= user_buffer_size) |
| 360 | return 0; |
| 361 | if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) { |
| 362 | printk(KERN_ERR "microcode: error! Can not read user data\n"); |
| 363 | return -EFAULT; |
| 364 | } |
| 365 | total_size = get_totalsize(&mc_header); |
Shaohua Li | bd8e39f | 2006-09-27 01:50:54 -0700 | [diff] [blame] | 366 | if (offset + total_size > user_buffer_size) { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 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; |
Andi Kleen | 2ba1ff2 | 2007-02-13 13:26:25 +0100 | [diff] [blame] | 386 | void *new_mc = NULL; |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | if ((len >> PAGE_SHIFT) > num_physpages) { |
| 433 | printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages); |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 437 | get_online_cpus(); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 438 | mutex_lock(µcode_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | user_buffer = (void __user *) buf; |
| 441 | user_buffer_size = (int) len; |
| 442 | |
| 443 | ret = do_microcode_update(); |
| 444 | if (!ret) |
| 445 | ret = (ssize_t)len; |
| 446 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 447 | mutex_unlock(µcode_mutex); |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 448 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 453 | static const struct file_operations microcode_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | .owner = THIS_MODULE, |
| 455 | .write = microcode_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | .open = microcode_open, |
| 457 | }; |
| 458 | |
| 459 | static struct miscdevice microcode_dev = { |
| 460 | .minor = MICROCODE_MINOR, |
| 461 | .name = "microcode", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | .fops = µcode_fops, |
| 463 | }; |
| 464 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 465 | static int __init microcode_dev_init (void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { |
| 467 | int error; |
| 468 | |
| 469 | error = misc_register(µcode_dev); |
| 470 | if (error) { |
| 471 | printk(KERN_ERR |
| 472 | "microcode: can't misc_register on minor=%d\n", |
| 473 | MICROCODE_MINOR); |
| 474 | return error; |
| 475 | } |
| 476 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | |
Sam Ravnborg | f8281a2 | 2007-06-01 00:47:10 -0700 | [diff] [blame] | 480 | static void microcode_dev_exit (void) |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 481 | { |
| 482 | misc_deregister(µcode_dev); |
| 483 | } |
| 484 | |
| 485 | MODULE_ALIAS_MISCDEV(MICROCODE_MINOR); |
| 486 | #else |
| 487 | #define microcode_dev_init() 0 |
| 488 | #define microcode_dev_exit() do { } while(0) |
| 489 | #endif |
| 490 | |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 491 | static long get_next_ucode_from_buffer(void **mc, void *buf, |
| 492 | unsigned long size, long offset) |
| 493 | { |
| 494 | microcode_header_t *mc_header; |
| 495 | unsigned long total_size; |
| 496 | |
| 497 | /* No more data */ |
| 498 | if (offset >= size) |
| 499 | return 0; |
| 500 | mc_header = (microcode_header_t *)(buf + offset); |
| 501 | total_size = get_totalsize(mc_header); |
| 502 | |
Shaohua Li | bd8e39f | 2006-09-27 01:50:54 -0700 | [diff] [blame] | 503 | if (offset + total_size > size) { |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 504 | printk(KERN_ERR "microcode: error! Bad data in microcode data file\n"); |
| 505 | return -EINVAL; |
| 506 | } |
| 507 | |
| 508 | *mc = vmalloc(total_size); |
| 509 | if (!*mc) { |
| 510 | printk(KERN_ERR "microcode: error! Can not allocate memory\n"); |
| 511 | return -ENOMEM; |
| 512 | } |
| 513 | memcpy(*mc, buf + offset, total_size); |
| 514 | return offset + total_size; |
| 515 | } |
| 516 | |
| 517 | /* fake device for request_firmware */ |
| 518 | static struct platform_device *microcode_pdev; |
| 519 | |
| 520 | static int cpu_request_microcode(int cpu) |
| 521 | { |
| 522 | char name[30]; |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 523 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 524 | const struct firmware *firmware; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 525 | void *buf; |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 526 | unsigned long size; |
| 527 | long offset = 0; |
| 528 | int error; |
| 529 | void *mc; |
| 530 | |
| 531 | /* We should bind the task to the CPU */ |
| 532 | BUG_ON(cpu != raw_smp_processor_id()); |
| 533 | sprintf(name,"intel-ucode/%02x-%02x-%02x", |
| 534 | c->x86, c->x86_model, c->x86_mask); |
| 535 | error = request_firmware(&firmware, name, µcode_pdev->dev); |
| 536 | if (error) { |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame^] | 537 | pr_debug("microcode: ucode data file %s load failed\n", name); |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 538 | return error; |
| 539 | } |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 540 | buf = firmware->data; |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 541 | size = firmware->size; |
| 542 | while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset)) |
| 543 | > 0) { |
| 544 | error = microcode_sanity_check(mc); |
| 545 | if (error) |
| 546 | break; |
| 547 | error = get_maching_microcode(mc, cpu); |
| 548 | if (error < 0) |
| 549 | break; |
| 550 | /* |
| 551 | * It's possible the data file has multiple matching ucode, |
| 552 | * lets keep searching till the latest version |
| 553 | */ |
| 554 | if (error == 1) { |
| 555 | apply_microcode(cpu); |
| 556 | error = 0; |
| 557 | } |
| 558 | vfree(mc); |
| 559 | } |
| 560 | if (offset > 0) |
| 561 | vfree(mc); |
| 562 | if (offset < 0) |
| 563 | error = offset; |
| 564 | release_firmware(firmware); |
| 565 | |
| 566 | return error; |
| 567 | } |
| 568 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 569 | static int apply_microcode_check_cpu(int cpu) |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 570 | { |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 571 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 572 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 573 | cpumask_t old; |
| 574 | unsigned int val[2]; |
| 575 | int err = 0; |
| 576 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 577 | /* Check if the microcode is available */ |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 578 | if (!uci->mc) |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 579 | return 0; |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 580 | |
| 581 | old = current->cpus_allowed; |
| 582 | set_cpus_allowed(current, cpumask_of_cpu(cpu)); |
| 583 | |
| 584 | /* Check if the microcode we have in memory matches the CPU */ |
| 585 | if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 || |
| 586 | cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001)) |
| 587 | err = -EINVAL; |
| 588 | |
| 589 | if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) { |
| 590 | /* get processor flags from MSR 0x17 */ |
| 591 | rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); |
| 592 | if (uci->pf != (1 << ((val[1] >> 18) & 7))) |
| 593 | err = -EINVAL; |
| 594 | } |
| 595 | |
| 596 | if (!err) { |
| 597 | wrmsr(MSR_IA32_UCODE_REV, 0, 0); |
| 598 | /* see notes above for revision 1.07. Apparent chip bug */ |
| 599 | sync_core(); |
| 600 | /* get the current revision from MSR 0x8B */ |
| 601 | rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]); |
| 602 | if (uci->rev != val[1]) |
| 603 | err = -EINVAL; |
| 604 | } |
| 605 | |
| 606 | if (!err) |
| 607 | apply_microcode(cpu); |
| 608 | else |
| 609 | printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:" |
| 610 | " sig=0x%x, pf=0x%x, rev=0x%x\n", |
| 611 | cpu, uci->sig, uci->pf, uci->rev); |
| 612 | |
| 613 | set_cpus_allowed(current, old); |
| 614 | return err; |
| 615 | } |
| 616 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 617 | static void microcode_init_cpu(int cpu, int resume) |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 618 | { |
| 619 | cpumask_t old; |
| 620 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 621 | |
| 622 | old = current->cpus_allowed; |
| 623 | |
| 624 | set_cpus_allowed(current, cpumask_of_cpu(cpu)); |
| 625 | mutex_lock(µcode_mutex); |
| 626 | collect_cpu_info(cpu); |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 627 | if (uci->valid && system_state == SYSTEM_RUNNING && !resume) |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 628 | cpu_request_microcode(cpu); |
| 629 | mutex_unlock(µcode_mutex); |
| 630 | set_cpus_allowed(current, old); |
| 631 | } |
| 632 | |
| 633 | static void microcode_fini_cpu(int cpu) |
| 634 | { |
| 635 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 636 | |
| 637 | mutex_lock(µcode_mutex); |
| 638 | uci->valid = 0; |
| 639 | vfree(uci->mc); |
| 640 | uci->mc = NULL; |
| 641 | mutex_unlock(µcode_mutex); |
| 642 | } |
| 643 | |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 644 | static ssize_t reload_store(struct sys_device *dev, const char *buf, size_t sz) |
| 645 | { |
| 646 | struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; |
| 647 | char *end; |
| 648 | unsigned long val = simple_strtoul(buf, &end, 0); |
| 649 | int err = 0; |
| 650 | int cpu = dev->id; |
| 651 | |
| 652 | if (end == buf) |
| 653 | return -EINVAL; |
| 654 | if (val == 1) { |
| 655 | cpumask_t old; |
| 656 | |
| 657 | old = current->cpus_allowed; |
| 658 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 659 | get_online_cpus(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 660 | set_cpus_allowed(current, cpumask_of_cpu(cpu)); |
| 661 | |
| 662 | mutex_lock(µcode_mutex); |
| 663 | if (uci->valid) |
| 664 | err = cpu_request_microcode(cpu); |
| 665 | mutex_unlock(µcode_mutex); |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 666 | put_online_cpus(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 667 | set_cpus_allowed(current, old); |
| 668 | } |
| 669 | if (err) |
| 670 | return err; |
| 671 | return sz; |
| 672 | } |
| 673 | |
| 674 | static ssize_t version_show(struct sys_device *dev, char *buf) |
| 675 | { |
| 676 | struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; |
| 677 | |
| 678 | return sprintf(buf, "0x%x\n", uci->rev); |
| 679 | } |
| 680 | |
| 681 | static ssize_t pf_show(struct sys_device *dev, char *buf) |
| 682 | { |
| 683 | struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; |
| 684 | |
| 685 | return sprintf(buf, "0x%x\n", uci->pf); |
| 686 | } |
| 687 | |
| 688 | static SYSDEV_ATTR(reload, 0200, NULL, reload_store); |
| 689 | static SYSDEV_ATTR(version, 0400, version_show, NULL); |
| 690 | static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL); |
| 691 | |
| 692 | static struct attribute *mc_default_attrs[] = { |
| 693 | &attr_reload.attr, |
| 694 | &attr_version.attr, |
| 695 | &attr_processor_flags.attr, |
| 696 | NULL |
| 697 | }; |
| 698 | |
| 699 | static struct attribute_group mc_attr_group = { |
| 700 | .attrs = mc_default_attrs, |
| 701 | .name = "microcode", |
| 702 | }; |
| 703 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 704 | static int __mc_sysdev_add(struct sys_device *sys_dev, int resume) |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 705 | { |
Jeff Garzik | 2e3ad8a | 2006-10-11 01:21:53 -0700 | [diff] [blame] | 706 | int err, cpu = sys_dev->id; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 707 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 708 | |
| 709 | if (!cpu_online(cpu)) |
| 710 | return 0; |
Jeff Garzik | 2e3ad8a | 2006-10-11 01:21:53 -0700 | [diff] [blame] | 711 | |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame^] | 712 | pr_debug("microcode: CPU%d added\n", cpu); |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 713 | memset(uci, 0, sizeof(*uci)); |
Jeff Garzik | 2e3ad8a | 2006-10-11 01:21:53 -0700 | [diff] [blame] | 714 | |
| 715 | err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group); |
| 716 | if (err) |
| 717 | return err; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 718 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 719 | microcode_init_cpu(cpu, resume); |
Rafael J. Wysocki | 1d64b9c | 2007-04-01 23:49:49 -0700 | [diff] [blame] | 720 | |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 724 | static int mc_sysdev_add(struct sys_device *sys_dev) |
| 725 | { |
| 726 | return __mc_sysdev_add(sys_dev, 0); |
| 727 | } |
| 728 | |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 729 | static int mc_sysdev_remove(struct sys_device *sys_dev) |
| 730 | { |
| 731 | int cpu = sys_dev->id; |
| 732 | |
| 733 | if (!cpu_online(cpu)) |
| 734 | return 0; |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 735 | |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame^] | 736 | pr_debug("microcode: CPU%d removed\n", cpu); |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 737 | microcode_fini_cpu(cpu); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 738 | sysfs_remove_group(&sys_dev->kobj, &mc_attr_group); |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | static int mc_sysdev_resume(struct sys_device *dev) |
| 743 | { |
| 744 | int cpu = dev->id; |
| 745 | |
| 746 | if (!cpu_online(cpu)) |
| 747 | return 0; |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame^] | 748 | pr_debug("microcode: CPU%d resumed\n", cpu); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 749 | /* only CPU 0 will apply ucode here */ |
| 750 | apply_microcode(0); |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | static struct sysdev_driver mc_sysdev_driver = { |
| 755 | .add = mc_sysdev_add, |
| 756 | .remove = mc_sysdev_remove, |
| 757 | .resume = mc_sysdev_resume, |
| 758 | }; |
| 759 | |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 760 | static __cpuinit int |
| 761 | mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu) |
| 762 | { |
| 763 | unsigned int cpu = (unsigned long)hcpu; |
| 764 | struct sys_device *sys_dev; |
| 765 | |
| 766 | sys_dev = get_cpu_sysdev(cpu); |
| 767 | switch (action) { |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 768 | case CPU_UP_CANCELED_FROZEN: |
| 769 | /* The CPU refused to come up during a system resume */ |
| 770 | microcode_fini_cpu(cpu); |
| 771 | break; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 772 | case CPU_ONLINE: |
| 773 | case CPU_DOWN_FAILED: |
| 774 | mc_sysdev_add(sys_dev); |
| 775 | break; |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 776 | case CPU_ONLINE_FROZEN: |
| 777 | /* System-wide resume is in progress, try to apply microcode */ |
| 778 | if (apply_microcode_check_cpu(cpu)) { |
| 779 | /* The application of microcode failed */ |
| 780 | microcode_fini_cpu(cpu); |
| 781 | __mc_sysdev_add(sys_dev, 1); |
| 782 | break; |
| 783 | } |
| 784 | case CPU_DOWN_FAILED_FROZEN: |
| 785 | if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group)) |
Ben Castricum | fe176de | 2008-03-27 20:52:35 +0100 | [diff] [blame^] | 786 | printk(KERN_ERR "microcode: Failed to create the sysfs " |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 787 | "group for CPU%d\n", cpu); |
| 788 | break; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 789 | case CPU_DOWN_PREPARE: |
| 790 | mc_sysdev_remove(sys_dev); |
| 791 | break; |
Rafael J. Wysocki | 455c017 | 2007-05-09 02:35:11 -0700 | [diff] [blame] | 792 | case CPU_DOWN_PREPARE_FROZEN: |
| 793 | /* Suspend is in progress, only remove the interface */ |
| 794 | sysfs_remove_group(&sys_dev->kobj, &mc_attr_group); |
| 795 | break; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 796 | } |
| 797 | return NOTIFY_OK; |
| 798 | } |
| 799 | |
Sam Ravnborg | c72258c | 2008-02-01 17:49:42 +0100 | [diff] [blame] | 800 | static struct notifier_block __refdata mc_cpu_notifier = { |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 801 | .notifier_call = mc_cpu_callback, |
| 802 | }; |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 803 | |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 804 | static int __init microcode_init (void) |
| 805 | { |
| 806 | int error; |
| 807 | |
| 808 | error = microcode_dev_init(); |
| 809 | if (error) |
| 810 | return error; |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 811 | microcode_pdev = platform_device_register_simple("microcode", -1, |
| 812 | NULL, 0); |
| 813 | if (IS_ERR(microcode_pdev)) { |
| 814 | microcode_dev_exit(); |
| 815 | return PTR_ERR(microcode_pdev); |
| 816 | } |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 817 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 818 | get_online_cpus(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 819 | error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver); |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 820 | put_online_cpus(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 821 | if (error) { |
| 822 | microcode_dev_exit(); |
| 823 | platform_device_unregister(microcode_pdev); |
| 824 | return error; |
| 825 | } |
| 826 | |
| 827 | register_hotcpu_notifier(&mc_cpu_notifier); |
| 828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | printk(KERN_INFO |
Tigran Aivazian | 6968826 | 2006-12-13 00:35:14 -0800 | [diff] [blame] | 830 | "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@aivazian.fsnet.co.uk>\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static void __exit microcode_exit (void) |
| 835 | { |
Shaohua Li | 9a3110b | 2006-09-27 01:50:51 -0700 | [diff] [blame] | 836 | microcode_dev_exit(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 837 | |
| 838 | unregister_hotcpu_notifier(&mc_cpu_notifier); |
| 839 | |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 840 | get_online_cpus(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 841 | sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver); |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 842 | put_online_cpus(); |
Shaohua Li | 9a4b9ef | 2006-09-27 01:50:53 -0700 | [diff] [blame] | 843 | |
Shaohua Li | a30a6a2 | 2006-09-27 01:50:52 -0700 | [diff] [blame] | 844 | platform_device_unregister(microcode_pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | module_init(microcode_init) |
| 848 | module_exit(microcode_exit) |