ia64/xen-unstable

annotate xen/arch/x86/microcode.c @ 5287:529d5150d163

bitkeeper revision 1.1651 (429f70102tptpkVKU9_mkUC4olK_-Q)

Many files:
More include cleanups towards "include where it's used".
sched.h:
g/c unneeded include.
include xen/spinlock.h once should be enough.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Thu Jun 02 20:46:08 2005 +0000 (2005-06-02)
parents a480b2990961
children b51e4b4608f9
rev   line source
kaf24@3362 1 /*
kaf24@3362 2 * Intel CPU Microcode Update Driver for Linux
kaf24@3362 3 *
kaf24@3362 4 * Copyright (C) 2000-2004 Tigran Aivazian
kaf24@3362 5 *
kaf24@3362 6 * This driver allows to upgrade microcode on Intel processors
kaf24@3362 7 * belonging to IA-32 family - PentiumPro, Pentium II,
kaf24@3362 8 * Pentium III, Xeon, Pentium 4, etc.
kaf24@3362 9 *
kaf24@3362 10 * Reference: Section 8.10 of Volume III, Intel Pentium 4 Manual,
kaf24@3362 11 * Order Number 245472 or free download from:
kaf24@3362 12 *
kaf24@3362 13 * http://developer.intel.com/design/pentium4/manuals/245472.htm
kaf24@3362 14 *
kaf24@3362 15 * For more information, go to http://www.urbanmyth.org/microcode
kaf24@3362 16 *
kaf24@3362 17 * This program is free software; you can redistribute it and/or
kaf24@3362 18 * modify it under the terms of the GNU General Public License
kaf24@3362 19 * as published by the Free Software Foundation; either version
kaf24@3362 20 * 2 of the License, or (at your option) any later version.
kaf24@3362 21 *
kaf24@3362 22 * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com>
kaf24@3362 23 * Initial release.
kaf24@3362 24 * 1.01 18 Feb 2000, Tigran Aivazian <tigran@sco.com>
kaf24@3362 25 * Added read() support + cleanups.
kaf24@3362 26 * 1.02 21 Feb 2000, Tigran Aivazian <tigran@sco.com>
kaf24@3362 27 * Added 'device trimming' support. open(O_WRONLY) zeroes
kaf24@3362 28 * and frees the saved copy of applied microcode.
kaf24@3362 29 * 1.03 29 Feb 2000, Tigran Aivazian <tigran@sco.com>
kaf24@3362 30 * Made to use devfs (/dev/cpu/microcode) + cleanups.
kaf24@3362 31 * 1.04 06 Jun 2000, Simon Trimmer <simon@veritas.com>
kaf24@3362 32 * Added misc device support (now uses both devfs and misc).
kaf24@3362 33 * Added MICROCODE_IOCFREE ioctl to clear memory.
kaf24@3362 34 * 1.05 09 Jun 2000, Simon Trimmer <simon@veritas.com>
kaf24@3362 35 * Messages for error cases (non Intel & no suitable microcode).
kaf24@3362 36 * 1.06 03 Aug 2000, Tigran Aivazian <tigran@veritas.com>
kaf24@3362 37 * Removed ->release(). Removed exclusive open and status bitmap.
kaf24@3362 38 * Added microcode_rwsem to serialize read()/write()/ioctl().
kaf24@3362 39 * Removed global kernel lock usage.
kaf24@3362 40 * 1.07 07 Sep 2000, Tigran Aivazian <tigran@veritas.com>
kaf24@3362 41 * Write 0 to 0x8B msr and then cpuid before reading revision,
kaf24@3362 42 * so that it works even if there were no update done by the
kaf24@3362 43 * BIOS. Otherwise, reading from 0x8B gives junk (which happened
kaf24@3362 44 * to be 0 on my machine which is why it worked even when I
kaf24@3362 45 * disabled update by the BIOS)
kaf24@3362 46 * Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix.
kaf24@3362 47 * 1.08 11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and
kaf24@3362 48 * Tigran Aivazian <tigran@veritas.com>
kaf24@3362 49 * Intel Pentium 4 processor support and bugfixes.
kaf24@3362 50 * 1.09 30 Oct 2001, Tigran Aivazian <tigran@veritas.com>
kaf24@3362 51 * Bugfix for HT (Hyper-Threading) enabled processors
kaf24@3362 52 * whereby processor resources are shared by all logical processors
kaf24@3362 53 * in a single CPU package.
kaf24@3362 54 * 1.10 28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and
kaf24@3362 55 * Tigran Aivazian <tigran@veritas.com>,
kaf24@3362 56 * Serialize updates as required on HT processors due to speculative
kaf24@3362 57 * nature of implementation.
kaf24@3362 58 * 1.11 22 Mar 2002 Tigran Aivazian <tigran@veritas.com>
kaf24@3362 59 * Fix the panic when writing zero-length microcode chunk.
kaf24@3362 60 * 1.12 29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>,
kaf24@3362 61 * Jun Nakajima <jun.nakajima@intel.com>
kaf24@3362 62 * Support for the microcode updates in the new format.
kaf24@3362 63 * 1.13 10 Oct 2003 Tigran Aivazian <tigran@veritas.com>
kaf24@3362 64 * Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl
kaf24@3362 65 * because we no longer hold a copy of applied microcode
kaf24@3362 66 * in kernel memory.
kaf24@3362 67 * 1.14 25 Jun 2004 Tigran Aivazian <tigran@veritas.com>
kaf24@3362 68 * Fix sigmatch() macro to handle old CPUs with pf == 0.
kaf24@3362 69 * Thanks to Stuart Swales for pointing out this bug.
kaf24@3362 70 */
kaf24@3362 71
kaf24@3362 72 #include <xen/config.h>
cl349@5285 73 #include <xen/lib.h>
kaf24@3362 74 #include <xen/kernel.h>
kaf24@3362 75 #include <xen/init.h>
kaf24@3362 76 #include <xen/sched.h>
cl349@5287 77 #include <xen/smp.h>
kaf24@3362 78 #include <xen/spinlock.h>
kaf24@3362 79
kaf24@3362 80 #include <asm/msr.h>
kaf24@3362 81 #include <asm/uaccess.h>
kaf24@3362 82 #include <asm/processor.h>
kaf24@3362 83
kaf24@3362 84 #define pr_debug(x...) ((void)0)
kaf24@3362 85 #define DECLARE_MUTEX(_m) spinlock_t _m = SPIN_LOCK_UNLOCKED
kaf24@3362 86 #define down(_m) spin_lock(_m)
kaf24@3362 87 #define up(_m) spin_unlock(_m)
kaf24@3629 88 #define vmalloc(_s) xmalloc_bytes(_s)
kaf24@3362 89 #define vfree(_p) xfree(_p)
kaf24@3362 90
kaf24@3362 91 #if 0
kaf24@3362 92 MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
kaf24@3362 93 MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
kaf24@3362 94 MODULE_LICENSE("GPL");
kaf24@3362 95 #endif
kaf24@3362 96
kaf24@3362 97 #define MICROCODE_VERSION "1.14"
kaf24@3362 98
kaf24@3362 99 #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
kaf24@3362 100 #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
kaf24@3362 101 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
kaf24@3362 102 #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
kaf24@3362 103 #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
kaf24@3362 104 #define DWSIZE (sizeof (u32))
kaf24@3362 105 #define get_totalsize(mc) \
kaf24@3362 106 (((microcode_t *)mc)->hdr.totalsize ? \
kaf24@3362 107 ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
kaf24@3362 108 #define get_datasize(mc) \
kaf24@3362 109 (((microcode_t *)mc)->hdr.datasize ? \
kaf24@3362 110 ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
kaf24@3362 111
kaf24@3362 112 #define sigmatch(s1, s2, p1, p2) \
kaf24@3362 113 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
kaf24@3362 114
kaf24@3362 115 #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
kaf24@3362 116
kaf24@3362 117 /* serialize access to the physical write to MSR 0x79 */
kaf24@3362 118 static spinlock_t microcode_update_lock = SPIN_LOCK_UNLOCKED;
kaf24@3362 119
kaf24@3362 120 /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
kaf24@3362 121 static DECLARE_MUTEX(microcode_sem);
kaf24@3362 122
kaf24@3362 123 static void __user *user_buffer; /* user area microcode data buffer */
kaf24@3362 124 static unsigned int user_buffer_size; /* it's size */
kaf24@3362 125
kaf24@3362 126 typedef enum mc_error_code {
kaf24@3362 127 MC_SUCCESS = 0,
kaf24@3362 128 MC_NOTFOUND = 1,
kaf24@3362 129 MC_MARKED = 2,
kaf24@3362 130 MC_ALLOCATED = 3,
kaf24@3362 131 } mc_error_code_t;
kaf24@3362 132
kaf24@3362 133 static struct ucode_cpu_info {
kaf24@3362 134 unsigned int sig;
kaf24@3362 135 unsigned int pf;
kaf24@3362 136 unsigned int rev;
kaf24@3362 137 unsigned int cksum;
kaf24@3362 138 mc_error_code_t err;
kaf24@3362 139 microcode_t *mc;
kaf24@3362 140 } ucode_cpu_info[NR_CPUS];
kaf24@3362 141
kaf24@3362 142 static void collect_cpu_info (void *unused)
kaf24@3362 143 {
kaf24@3362 144 int cpu_num = smp_processor_id();
kaf24@3362 145 struct cpuinfo_x86 *c = cpu_data + cpu_num;
kaf24@3362 146 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 147 unsigned int val[2];
kaf24@3362 148
kaf24@3362 149 uci->sig = uci->pf = uci->rev = uci->cksum = 0;
kaf24@3362 150 uci->err = MC_NOTFOUND;
kaf24@3362 151 uci->mc = NULL;
kaf24@3362 152
kaf24@3362 153 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
kaf24@3362 154 cpu_has(c, X86_FEATURE_IA64)) {
kaf24@3362 155 printk(KERN_ERR "microcode: CPU%d not a capable Intel processor\n", cpu_num);
kaf24@3362 156 return;
kaf24@3362 157 } else {
kaf24@3362 158 uci->sig = cpuid_eax(0x00000001);
kaf24@3362 159
kaf24@3362 160 if ((c->x86_model >= 5) || (c->x86 > 6)) {
kaf24@3362 161 /* get processor flags from MSR 0x17 */
kaf24@3362 162 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
kaf24@3362 163 uci->pf = 1 << ((val[1] >> 18) & 7);
kaf24@3362 164 }
kaf24@3362 165 }
kaf24@3362 166
kaf24@3362 167 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
kaf24@3362 168 __asm__ __volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx");
kaf24@3362 169 /* get the current revision from MSR 0x8B */
kaf24@3362 170 rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
kaf24@3362 171 pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
kaf24@3362 172 uci->sig, uci->pf, uci->rev);
kaf24@3362 173 }
kaf24@3362 174
kaf24@3362 175 static inline void mark_microcode_update (int cpu_num, microcode_header_t *mc_header, int sig, int pf, int cksum)
kaf24@3362 176 {
kaf24@3362 177 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 178
kaf24@3362 179 pr_debug("Microcode Found.\n");
kaf24@3362 180 pr_debug(" Header Revision 0x%x\n", mc_header->hdrver);
kaf24@3362 181 pr_debug(" Loader Revision 0x%x\n", mc_header->ldrver);
kaf24@3362 182 pr_debug(" Revision 0x%x \n", mc_header->rev);
kaf24@3362 183 pr_debug(" Date %x/%x/%x\n",
kaf24@3362 184 ((mc_header->date >> 24 ) & 0xff),
kaf24@3362 185 ((mc_header->date >> 16 ) & 0xff),
kaf24@3362 186 (mc_header->date & 0xFFFF));
kaf24@3362 187 pr_debug(" Signature 0x%x\n", sig);
kaf24@3362 188 pr_debug(" Type 0x%x Family 0x%x Model 0x%x Stepping 0x%x\n",
kaf24@3362 189 ((sig >> 12) & 0x3),
kaf24@3362 190 ((sig >> 8) & 0xf),
kaf24@3362 191 ((sig >> 4) & 0xf),
kaf24@3362 192 ((sig & 0xf)));
kaf24@3362 193 pr_debug(" Processor Flags 0x%x\n", pf);
kaf24@3362 194 pr_debug(" Checksum 0x%x\n", cksum);
kaf24@3362 195
kaf24@3362 196 if (mc_header->rev < uci->rev) {
kaf24@3362 197 printk(KERN_ERR "microcode: CPU%d not 'upgrading' to earlier revision"
kaf24@3362 198 " 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
kaf24@3362 199 goto out;
kaf24@3362 200 } else if (mc_header->rev == uci->rev) {
kaf24@3362 201 /* notify the caller of success on this cpu */
kaf24@3362 202 uci->err = MC_SUCCESS;
kaf24@3362 203 printk(KERN_ERR "microcode: CPU%d already at revision"
kaf24@3362 204 " 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
kaf24@3362 205 goto out;
kaf24@3362 206 }
kaf24@3362 207
kaf24@3362 208 pr_debug("microcode: CPU%d found a matching microcode update with "
kaf24@3362 209 " revision 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
kaf24@3362 210 uci->cksum = cksum;
kaf24@3362 211 uci->pf = pf; /* keep the original mc pf for cksum calculation */
kaf24@3362 212 uci->err = MC_MARKED; /* found the match */
kaf24@3362 213 out:
kaf24@3362 214 return;
kaf24@3362 215 }
kaf24@3362 216
kaf24@3362 217 static int find_matching_ucodes (void)
kaf24@3362 218 {
kaf24@3362 219 int cursor = 0;
kaf24@3362 220 int error = 0;
kaf24@3362 221
kaf24@3362 222 while (cursor + MC_HEADER_SIZE < user_buffer_size) {
kaf24@3362 223 microcode_header_t mc_header;
kaf24@3362 224 void *newmc = NULL;
kaf24@3362 225 int i, sum, cpu_num, allocated_flag, total_size, data_size, ext_table_size;
kaf24@3362 226
kaf24@3362 227 if (copy_from_user(&mc_header, user_buffer + cursor, MC_HEADER_SIZE)) {
kaf24@3362 228 printk(KERN_ERR "microcode: error! Can not read user data\n");
kaf24@3362 229 error = -EFAULT;
kaf24@3362 230 goto out;
kaf24@3362 231 }
kaf24@3362 232
kaf24@3362 233 total_size = get_totalsize(&mc_header);
kaf24@3362 234 if ((cursor + total_size > user_buffer_size) || (total_size < DEFAULT_UCODE_TOTALSIZE)) {
kaf24@3362 235 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
kaf24@3362 236 error = -EINVAL;
kaf24@3362 237 goto out;
kaf24@3362 238 }
kaf24@3362 239
kaf24@3362 240 data_size = get_datasize(&mc_header);
kaf24@3362 241 if ((data_size + MC_HEADER_SIZE > total_size) || (data_size < DEFAULT_UCODE_DATASIZE)) {
kaf24@3362 242 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
kaf24@3362 243 error = -EINVAL;
kaf24@3362 244 goto out;
kaf24@3362 245 }
kaf24@3362 246
kaf24@3362 247 if (mc_header.ldrver != 1 || mc_header.hdrver != 1) {
kaf24@3362 248 printk(KERN_ERR "microcode: error! Unknown microcode update format\n");
kaf24@3362 249 error = -EINVAL;
kaf24@3362 250 goto out;
kaf24@3362 251 }
kaf24@3362 252
kaf24@3362 253 for (cpu_num = 0; cpu_num < num_online_cpus(); cpu_num++) {
kaf24@3362 254 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 255 if (uci->err != MC_NOTFOUND) /* already found a match or not an online cpu*/
kaf24@3362 256 continue;
kaf24@3362 257
kaf24@3362 258 if (sigmatch(mc_header.sig, uci->sig, mc_header.pf, uci->pf))
kaf24@3362 259 mark_microcode_update(cpu_num, &mc_header, mc_header.sig, mc_header.pf, mc_header.cksum);
kaf24@3362 260 }
kaf24@3362 261
kaf24@3362 262 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
kaf24@3362 263 if (ext_table_size) {
kaf24@3362 264 struct extended_sigtable ext_header;
kaf24@3362 265 struct extended_signature ext_sig;
kaf24@3362 266 int ext_sigcount;
kaf24@3362 267
kaf24@3362 268 if ((ext_table_size < EXT_HEADER_SIZE)
kaf24@3362 269 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
kaf24@3362 270 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
kaf24@3362 271 error = -EINVAL;
kaf24@3362 272 goto out;
kaf24@3362 273 }
kaf24@3362 274 if (copy_from_user(&ext_header, user_buffer + cursor
kaf24@3362 275 + MC_HEADER_SIZE + data_size, EXT_HEADER_SIZE)) {
kaf24@3362 276 printk(KERN_ERR "microcode: error! Can not read user data\n");
kaf24@3362 277 error = -EFAULT;
kaf24@3362 278 goto out;
kaf24@3362 279 }
kaf24@3362 280 if (ext_table_size != exttable_size(&ext_header)) {
kaf24@3362 281 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
kaf24@3362 282 error = -EFAULT;
kaf24@3362 283 goto out;
kaf24@3362 284 }
kaf24@3362 285
kaf24@3362 286 ext_sigcount = ext_header.count;
kaf24@3362 287
kaf24@3362 288 for (i = 0; i < ext_sigcount; i++) {
kaf24@3362 289 if (copy_from_user(&ext_sig, user_buffer + cursor + MC_HEADER_SIZE + data_size + EXT_HEADER_SIZE
kaf24@3362 290 + EXT_SIGNATURE_SIZE * i, EXT_SIGNATURE_SIZE)) {
kaf24@3362 291 printk(KERN_ERR "microcode: error! Can not read user data\n");
kaf24@3362 292 error = -EFAULT;
kaf24@3362 293 goto out;
kaf24@3362 294 }
kaf24@3362 295 for (cpu_num = 0; cpu_num < num_online_cpus(); cpu_num++) {
kaf24@3362 296 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 297 if (uci->err != MC_NOTFOUND) /* already found a match or not an online cpu*/
kaf24@3362 298 continue;
kaf24@3362 299 if (sigmatch(ext_sig.sig, uci->sig, ext_sig.pf, uci->pf)) {
kaf24@3362 300 mark_microcode_update(cpu_num, &mc_header, ext_sig.sig, ext_sig.pf, ext_sig.cksum);
kaf24@3362 301 }
kaf24@3362 302 }
kaf24@3362 303 }
kaf24@3362 304 }
kaf24@3362 305 /* now check if any cpu has matched */
kaf24@3362 306 for (cpu_num = 0, allocated_flag = 0, sum = 0; cpu_num < num_online_cpus(); cpu_num++) {
kaf24@3362 307 if (ucode_cpu_info[cpu_num].err == MC_MARKED) {
kaf24@3362 308 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 309 if (!allocated_flag) {
kaf24@3362 310 allocated_flag = 1;
kaf24@3362 311 newmc = vmalloc(total_size);
kaf24@3362 312 if (!newmc) {
kaf24@3362 313 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
kaf24@3362 314 error = -ENOMEM;
kaf24@3362 315 goto out;
kaf24@3362 316 }
kaf24@3362 317 if (copy_from_user(newmc + MC_HEADER_SIZE,
kaf24@3362 318 user_buffer + cursor + MC_HEADER_SIZE,
kaf24@3362 319 total_size - MC_HEADER_SIZE)) {
kaf24@3362 320 printk(KERN_ERR "microcode: error! Can not read user data\n");
kaf24@3362 321 vfree(newmc);
kaf24@3362 322 error = -EFAULT;
kaf24@3362 323 goto out;
kaf24@3362 324 }
kaf24@3362 325 memcpy(newmc, &mc_header, MC_HEADER_SIZE);
kaf24@3362 326 /* check extended table checksum */
kaf24@3362 327 if (ext_table_size) {
kaf24@3362 328 int ext_table_sum = 0;
kaf24@3362 329 int * ext_tablep = (((void *) newmc) + MC_HEADER_SIZE + data_size);
kaf24@3362 330 i = ext_table_size / DWSIZE;
kaf24@3362 331 while (i--) ext_table_sum += ext_tablep[i];
kaf24@3362 332 if (ext_table_sum) {
kaf24@3362 333 printk(KERN_WARNING "microcode: aborting, bad extended signature table checksum\n");
kaf24@3362 334 vfree(newmc);
kaf24@3362 335 error = -EINVAL;
kaf24@3362 336 goto out;
kaf24@3362 337 }
kaf24@3362 338 }
kaf24@3362 339
kaf24@3362 340 /* calculate the checksum */
kaf24@3362 341 i = (MC_HEADER_SIZE + data_size) / DWSIZE;
kaf24@3362 342 while (i--) sum += ((int *)newmc)[i];
kaf24@3362 343 sum -= (mc_header.sig + mc_header.pf + mc_header.cksum);
kaf24@3362 344 }
kaf24@3362 345 ucode_cpu_info[cpu_num].mc = newmc;
kaf24@3362 346 ucode_cpu_info[cpu_num].err = MC_ALLOCATED; /* mc updated */
kaf24@3362 347 if (sum + uci->sig + uci->pf + uci->cksum != 0) {
kaf24@3362 348 printk(KERN_ERR "microcode: CPU%d aborting, bad checksum\n", cpu_num);
kaf24@3362 349 error = -EINVAL;
kaf24@3362 350 goto out;
kaf24@3362 351 }
kaf24@3362 352 }
kaf24@3362 353 }
kaf24@3362 354 cursor += total_size; /* goto the next update patch */
kaf24@3362 355 } /* end of while */
kaf24@3362 356 out:
kaf24@3362 357 return error;
kaf24@3362 358 }
kaf24@3362 359
kaf24@3362 360 static void do_update_one (void * unused)
kaf24@3362 361 {
kaf24@3362 362 unsigned long flags;
kaf24@3362 363 unsigned int val[2];
kaf24@3362 364 int cpu_num = smp_processor_id();
kaf24@3362 365 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 366
kaf24@3362 367 if (uci->mc == NULL) {
kaf24@3362 368 printk(KERN_INFO "microcode: No suitable data for CPU%d\n", cpu_num);
kaf24@3362 369 return;
kaf24@3362 370 }
kaf24@3362 371
kaf24@3362 372 /* serialize access to the physical write to MSR 0x79 */
kaf24@3362 373 spin_lock_irqsave(&microcode_update_lock, flags);
kaf24@3362 374
kaf24@3362 375 /* write microcode via MSR 0x79 */
kaf24@3362 376 wrmsr(MSR_IA32_UCODE_WRITE,
kaf24@3362 377 (unsigned long) uci->mc->bits,
kaf24@3362 378 (unsigned long) uci->mc->bits >> 16 >> 16);
kaf24@3362 379 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
kaf24@3362 380
kaf24@3362 381 __asm__ __volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx");
kaf24@3362 382 /* get the current revision from MSR 0x8B */
kaf24@3362 383 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
kaf24@3362 384
kaf24@3362 385 /* notify the caller of success on this cpu */
kaf24@3362 386 uci->err = MC_SUCCESS;
kaf24@3362 387 spin_unlock_irqrestore(&microcode_update_lock, flags);
kaf24@3362 388 printk(KERN_INFO "microcode: CPU%d updated from revision "
kaf24@3362 389 "0x%x to 0x%x, date = %08x \n",
kaf24@3362 390 cpu_num, uci->rev, val[1], uci->mc->hdr.date);
kaf24@3362 391 return;
kaf24@3362 392 }
kaf24@3362 393
kaf24@3362 394 static int do_microcode_update (void)
kaf24@3362 395 {
kaf24@3362 396 int i, error;
kaf24@3362 397
kaf24@3362 398 if (on_each_cpu(collect_cpu_info, NULL, 1, 1) != 0) {
kaf24@3362 399 printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
kaf24@3362 400 error = -EIO;
kaf24@3362 401 goto out;
kaf24@3362 402 }
kaf24@3362 403
kaf24@3362 404 if ((error = find_matching_ucodes())) {
kaf24@3362 405 printk(KERN_ERR "microcode: Error in the microcode data\n");
kaf24@3362 406 goto out_free;
kaf24@3362 407 }
kaf24@3362 408
kaf24@3362 409 if (on_each_cpu(do_update_one, NULL, 1, 1) != 0) {
kaf24@3362 410 printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
kaf24@3362 411 error = -EIO;
kaf24@3362 412 }
kaf24@3362 413
kaf24@3362 414 out_free:
kaf24@3362 415 for (i = 0; i < num_online_cpus(); i++) {
kaf24@3362 416 if (ucode_cpu_info[i].mc) {
kaf24@3362 417 int j;
kaf24@3362 418 void *tmp = ucode_cpu_info[i].mc;
kaf24@3362 419 vfree(tmp);
kaf24@3362 420 for (j = i; j < num_online_cpus(); j++) {
kaf24@3362 421 if (ucode_cpu_info[j].mc == tmp)
kaf24@3362 422 ucode_cpu_info[j].mc = NULL;
kaf24@3362 423 }
kaf24@3362 424 }
kaf24@3362 425 }
kaf24@3362 426 out:
kaf24@3362 427 return error;
kaf24@3362 428 }
kaf24@3362 429
kaf24@3362 430 int microcode_update(void *buf, unsigned long len)
kaf24@3362 431 {
kaf24@3362 432 int ret;
kaf24@3362 433
kaf24@3362 434 if (len < DEFAULT_UCODE_TOTALSIZE) {
kaf24@3362 435 printk(KERN_ERR "microcode: not enough data\n");
kaf24@3362 436 return -EINVAL;
kaf24@3362 437 }
kaf24@3362 438
kaf24@3362 439 down(&microcode_sem);
kaf24@3362 440
kaf24@3362 441 user_buffer = (void __user *) buf;
kaf24@3362 442 user_buffer_size = (int) len;
kaf24@3362 443
kaf24@3362 444 ret = do_microcode_update();
kaf24@3362 445
kaf24@3362 446 up(&microcode_sem);
kaf24@3362 447
kaf24@3362 448 return ret;
kaf24@3362 449 }