ia64/xen-unstable

annotate xen/arch/x86/microcode.c @ 5285:a480b2990961

bitkeeper revision 1.1649 (429f6bb65KQm70mnFwO33ykh9n1qag)

sched.h:
g/c unneeded include.
Many files:
Rename xen/slab.h to xen/xmalloc.h and fix resulting fallout.
xmalloc.h:
Rename: xen/include/xen/slab.h -> xen/include/xen/xmalloc.h
sched.h, multicall.c, dom_mem_ops.c, asm-offsets.c:
cleanup incorrect includes.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Thu Jun 02 20:27:34 2005 +0000 (2005-06-02)
parents de3abc161c24
children 529d5150d163
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>
kaf24@3362 77 #include <xen/spinlock.h>
kaf24@3362 78
kaf24@3362 79 #include <asm/msr.h>
kaf24@3362 80 #include <asm/uaccess.h>
kaf24@3362 81 #include <asm/processor.h>
kaf24@3362 82
kaf24@3362 83 #define pr_debug(x...) ((void)0)
kaf24@3362 84 #define DECLARE_MUTEX(_m) spinlock_t _m = SPIN_LOCK_UNLOCKED
kaf24@3362 85 #define down(_m) spin_lock(_m)
kaf24@3362 86 #define up(_m) spin_unlock(_m)
kaf24@3629 87 #define vmalloc(_s) xmalloc_bytes(_s)
kaf24@3362 88 #define vfree(_p) xfree(_p)
kaf24@3362 89
kaf24@3362 90 #if 0
kaf24@3362 91 MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
kaf24@3362 92 MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
kaf24@3362 93 MODULE_LICENSE("GPL");
kaf24@3362 94 #endif
kaf24@3362 95
kaf24@3362 96 #define MICROCODE_VERSION "1.14"
kaf24@3362 97
kaf24@3362 98 #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
kaf24@3362 99 #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
kaf24@3362 100 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
kaf24@3362 101 #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
kaf24@3362 102 #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
kaf24@3362 103 #define DWSIZE (sizeof (u32))
kaf24@3362 104 #define get_totalsize(mc) \
kaf24@3362 105 (((microcode_t *)mc)->hdr.totalsize ? \
kaf24@3362 106 ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
kaf24@3362 107 #define get_datasize(mc) \
kaf24@3362 108 (((microcode_t *)mc)->hdr.datasize ? \
kaf24@3362 109 ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
kaf24@3362 110
kaf24@3362 111 #define sigmatch(s1, s2, p1, p2) \
kaf24@3362 112 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
kaf24@3362 113
kaf24@3362 114 #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
kaf24@3362 115
kaf24@3362 116 /* serialize access to the physical write to MSR 0x79 */
kaf24@3362 117 static spinlock_t microcode_update_lock = SPIN_LOCK_UNLOCKED;
kaf24@3362 118
kaf24@3362 119 /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
kaf24@3362 120 static DECLARE_MUTEX(microcode_sem);
kaf24@3362 121
kaf24@3362 122 static void __user *user_buffer; /* user area microcode data buffer */
kaf24@3362 123 static unsigned int user_buffer_size; /* it's size */
kaf24@3362 124
kaf24@3362 125 typedef enum mc_error_code {
kaf24@3362 126 MC_SUCCESS = 0,
kaf24@3362 127 MC_NOTFOUND = 1,
kaf24@3362 128 MC_MARKED = 2,
kaf24@3362 129 MC_ALLOCATED = 3,
kaf24@3362 130 } mc_error_code_t;
kaf24@3362 131
kaf24@3362 132 static struct ucode_cpu_info {
kaf24@3362 133 unsigned int sig;
kaf24@3362 134 unsigned int pf;
kaf24@3362 135 unsigned int rev;
kaf24@3362 136 unsigned int cksum;
kaf24@3362 137 mc_error_code_t err;
kaf24@3362 138 microcode_t *mc;
kaf24@3362 139 } ucode_cpu_info[NR_CPUS];
kaf24@3362 140
kaf24@3362 141 static void collect_cpu_info (void *unused)
kaf24@3362 142 {
kaf24@3362 143 int cpu_num = smp_processor_id();
kaf24@3362 144 struct cpuinfo_x86 *c = cpu_data + cpu_num;
kaf24@3362 145 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 146 unsigned int val[2];
kaf24@3362 147
kaf24@3362 148 uci->sig = uci->pf = uci->rev = uci->cksum = 0;
kaf24@3362 149 uci->err = MC_NOTFOUND;
kaf24@3362 150 uci->mc = NULL;
kaf24@3362 151
kaf24@3362 152 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
kaf24@3362 153 cpu_has(c, X86_FEATURE_IA64)) {
kaf24@3362 154 printk(KERN_ERR "microcode: CPU%d not a capable Intel processor\n", cpu_num);
kaf24@3362 155 return;
kaf24@3362 156 } else {
kaf24@3362 157 uci->sig = cpuid_eax(0x00000001);
kaf24@3362 158
kaf24@3362 159 if ((c->x86_model >= 5) || (c->x86 > 6)) {
kaf24@3362 160 /* get processor flags from MSR 0x17 */
kaf24@3362 161 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
kaf24@3362 162 uci->pf = 1 << ((val[1] >> 18) & 7);
kaf24@3362 163 }
kaf24@3362 164 }
kaf24@3362 165
kaf24@3362 166 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
kaf24@3362 167 __asm__ __volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx");
kaf24@3362 168 /* get the current revision from MSR 0x8B */
kaf24@3362 169 rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
kaf24@3362 170 pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
kaf24@3362 171 uci->sig, uci->pf, uci->rev);
kaf24@3362 172 }
kaf24@3362 173
kaf24@3362 174 static inline void mark_microcode_update (int cpu_num, microcode_header_t *mc_header, int sig, int pf, int cksum)
kaf24@3362 175 {
kaf24@3362 176 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 177
kaf24@3362 178 pr_debug("Microcode Found.\n");
kaf24@3362 179 pr_debug(" Header Revision 0x%x\n", mc_header->hdrver);
kaf24@3362 180 pr_debug(" Loader Revision 0x%x\n", mc_header->ldrver);
kaf24@3362 181 pr_debug(" Revision 0x%x \n", mc_header->rev);
kaf24@3362 182 pr_debug(" Date %x/%x/%x\n",
kaf24@3362 183 ((mc_header->date >> 24 ) & 0xff),
kaf24@3362 184 ((mc_header->date >> 16 ) & 0xff),
kaf24@3362 185 (mc_header->date & 0xFFFF));
kaf24@3362 186 pr_debug(" Signature 0x%x\n", sig);
kaf24@3362 187 pr_debug(" Type 0x%x Family 0x%x Model 0x%x Stepping 0x%x\n",
kaf24@3362 188 ((sig >> 12) & 0x3),
kaf24@3362 189 ((sig >> 8) & 0xf),
kaf24@3362 190 ((sig >> 4) & 0xf),
kaf24@3362 191 ((sig & 0xf)));
kaf24@3362 192 pr_debug(" Processor Flags 0x%x\n", pf);
kaf24@3362 193 pr_debug(" Checksum 0x%x\n", cksum);
kaf24@3362 194
kaf24@3362 195 if (mc_header->rev < uci->rev) {
kaf24@3362 196 printk(KERN_ERR "microcode: CPU%d not 'upgrading' to earlier revision"
kaf24@3362 197 " 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
kaf24@3362 198 goto out;
kaf24@3362 199 } else if (mc_header->rev == uci->rev) {
kaf24@3362 200 /* notify the caller of success on this cpu */
kaf24@3362 201 uci->err = MC_SUCCESS;
kaf24@3362 202 printk(KERN_ERR "microcode: CPU%d already at revision"
kaf24@3362 203 " 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
kaf24@3362 204 goto out;
kaf24@3362 205 }
kaf24@3362 206
kaf24@3362 207 pr_debug("microcode: CPU%d found a matching microcode update with "
kaf24@3362 208 " revision 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
kaf24@3362 209 uci->cksum = cksum;
kaf24@3362 210 uci->pf = pf; /* keep the original mc pf for cksum calculation */
kaf24@3362 211 uci->err = MC_MARKED; /* found the match */
kaf24@3362 212 out:
kaf24@3362 213 return;
kaf24@3362 214 }
kaf24@3362 215
kaf24@3362 216 static int find_matching_ucodes (void)
kaf24@3362 217 {
kaf24@3362 218 int cursor = 0;
kaf24@3362 219 int error = 0;
kaf24@3362 220
kaf24@3362 221 while (cursor + MC_HEADER_SIZE < user_buffer_size) {
kaf24@3362 222 microcode_header_t mc_header;
kaf24@3362 223 void *newmc = NULL;
kaf24@3362 224 int i, sum, cpu_num, allocated_flag, total_size, data_size, ext_table_size;
kaf24@3362 225
kaf24@3362 226 if (copy_from_user(&mc_header, user_buffer + cursor, MC_HEADER_SIZE)) {
kaf24@3362 227 printk(KERN_ERR "microcode: error! Can not read user data\n");
kaf24@3362 228 error = -EFAULT;
kaf24@3362 229 goto out;
kaf24@3362 230 }
kaf24@3362 231
kaf24@3362 232 total_size = get_totalsize(&mc_header);
kaf24@3362 233 if ((cursor + total_size > user_buffer_size) || (total_size < DEFAULT_UCODE_TOTALSIZE)) {
kaf24@3362 234 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
kaf24@3362 235 error = -EINVAL;
kaf24@3362 236 goto out;
kaf24@3362 237 }
kaf24@3362 238
kaf24@3362 239 data_size = get_datasize(&mc_header);
kaf24@3362 240 if ((data_size + MC_HEADER_SIZE > total_size) || (data_size < DEFAULT_UCODE_DATASIZE)) {
kaf24@3362 241 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
kaf24@3362 242 error = -EINVAL;
kaf24@3362 243 goto out;
kaf24@3362 244 }
kaf24@3362 245
kaf24@3362 246 if (mc_header.ldrver != 1 || mc_header.hdrver != 1) {
kaf24@3362 247 printk(KERN_ERR "microcode: error! Unknown microcode update format\n");
kaf24@3362 248 error = -EINVAL;
kaf24@3362 249 goto out;
kaf24@3362 250 }
kaf24@3362 251
kaf24@3362 252 for (cpu_num = 0; cpu_num < num_online_cpus(); cpu_num++) {
kaf24@3362 253 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 254 if (uci->err != MC_NOTFOUND) /* already found a match or not an online cpu*/
kaf24@3362 255 continue;
kaf24@3362 256
kaf24@3362 257 if (sigmatch(mc_header.sig, uci->sig, mc_header.pf, uci->pf))
kaf24@3362 258 mark_microcode_update(cpu_num, &mc_header, mc_header.sig, mc_header.pf, mc_header.cksum);
kaf24@3362 259 }
kaf24@3362 260
kaf24@3362 261 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
kaf24@3362 262 if (ext_table_size) {
kaf24@3362 263 struct extended_sigtable ext_header;
kaf24@3362 264 struct extended_signature ext_sig;
kaf24@3362 265 int ext_sigcount;
kaf24@3362 266
kaf24@3362 267 if ((ext_table_size < EXT_HEADER_SIZE)
kaf24@3362 268 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
kaf24@3362 269 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
kaf24@3362 270 error = -EINVAL;
kaf24@3362 271 goto out;
kaf24@3362 272 }
kaf24@3362 273 if (copy_from_user(&ext_header, user_buffer + cursor
kaf24@3362 274 + MC_HEADER_SIZE + data_size, EXT_HEADER_SIZE)) {
kaf24@3362 275 printk(KERN_ERR "microcode: error! Can not read user data\n");
kaf24@3362 276 error = -EFAULT;
kaf24@3362 277 goto out;
kaf24@3362 278 }
kaf24@3362 279 if (ext_table_size != exttable_size(&ext_header)) {
kaf24@3362 280 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
kaf24@3362 281 error = -EFAULT;
kaf24@3362 282 goto out;
kaf24@3362 283 }
kaf24@3362 284
kaf24@3362 285 ext_sigcount = ext_header.count;
kaf24@3362 286
kaf24@3362 287 for (i = 0; i < ext_sigcount; i++) {
kaf24@3362 288 if (copy_from_user(&ext_sig, user_buffer + cursor + MC_HEADER_SIZE + data_size + EXT_HEADER_SIZE
kaf24@3362 289 + EXT_SIGNATURE_SIZE * i, EXT_SIGNATURE_SIZE)) {
kaf24@3362 290 printk(KERN_ERR "microcode: error! Can not read user data\n");
kaf24@3362 291 error = -EFAULT;
kaf24@3362 292 goto out;
kaf24@3362 293 }
kaf24@3362 294 for (cpu_num = 0; cpu_num < num_online_cpus(); cpu_num++) {
kaf24@3362 295 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 296 if (uci->err != MC_NOTFOUND) /* already found a match or not an online cpu*/
kaf24@3362 297 continue;
kaf24@3362 298 if (sigmatch(ext_sig.sig, uci->sig, ext_sig.pf, uci->pf)) {
kaf24@3362 299 mark_microcode_update(cpu_num, &mc_header, ext_sig.sig, ext_sig.pf, ext_sig.cksum);
kaf24@3362 300 }
kaf24@3362 301 }
kaf24@3362 302 }
kaf24@3362 303 }
kaf24@3362 304 /* now check if any cpu has matched */
kaf24@3362 305 for (cpu_num = 0, allocated_flag = 0, sum = 0; cpu_num < num_online_cpus(); cpu_num++) {
kaf24@3362 306 if (ucode_cpu_info[cpu_num].err == MC_MARKED) {
kaf24@3362 307 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 308 if (!allocated_flag) {
kaf24@3362 309 allocated_flag = 1;
kaf24@3362 310 newmc = vmalloc(total_size);
kaf24@3362 311 if (!newmc) {
kaf24@3362 312 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
kaf24@3362 313 error = -ENOMEM;
kaf24@3362 314 goto out;
kaf24@3362 315 }
kaf24@3362 316 if (copy_from_user(newmc + MC_HEADER_SIZE,
kaf24@3362 317 user_buffer + cursor + MC_HEADER_SIZE,
kaf24@3362 318 total_size - MC_HEADER_SIZE)) {
kaf24@3362 319 printk(KERN_ERR "microcode: error! Can not read user data\n");
kaf24@3362 320 vfree(newmc);
kaf24@3362 321 error = -EFAULT;
kaf24@3362 322 goto out;
kaf24@3362 323 }
kaf24@3362 324 memcpy(newmc, &mc_header, MC_HEADER_SIZE);
kaf24@3362 325 /* check extended table checksum */
kaf24@3362 326 if (ext_table_size) {
kaf24@3362 327 int ext_table_sum = 0;
kaf24@3362 328 int * ext_tablep = (((void *) newmc) + MC_HEADER_SIZE + data_size);
kaf24@3362 329 i = ext_table_size / DWSIZE;
kaf24@3362 330 while (i--) ext_table_sum += ext_tablep[i];
kaf24@3362 331 if (ext_table_sum) {
kaf24@3362 332 printk(KERN_WARNING "microcode: aborting, bad extended signature table checksum\n");
kaf24@3362 333 vfree(newmc);
kaf24@3362 334 error = -EINVAL;
kaf24@3362 335 goto out;
kaf24@3362 336 }
kaf24@3362 337 }
kaf24@3362 338
kaf24@3362 339 /* calculate the checksum */
kaf24@3362 340 i = (MC_HEADER_SIZE + data_size) / DWSIZE;
kaf24@3362 341 while (i--) sum += ((int *)newmc)[i];
kaf24@3362 342 sum -= (mc_header.sig + mc_header.pf + mc_header.cksum);
kaf24@3362 343 }
kaf24@3362 344 ucode_cpu_info[cpu_num].mc = newmc;
kaf24@3362 345 ucode_cpu_info[cpu_num].err = MC_ALLOCATED; /* mc updated */
kaf24@3362 346 if (sum + uci->sig + uci->pf + uci->cksum != 0) {
kaf24@3362 347 printk(KERN_ERR "microcode: CPU%d aborting, bad checksum\n", cpu_num);
kaf24@3362 348 error = -EINVAL;
kaf24@3362 349 goto out;
kaf24@3362 350 }
kaf24@3362 351 }
kaf24@3362 352 }
kaf24@3362 353 cursor += total_size; /* goto the next update patch */
kaf24@3362 354 } /* end of while */
kaf24@3362 355 out:
kaf24@3362 356 return error;
kaf24@3362 357 }
kaf24@3362 358
kaf24@3362 359 static void do_update_one (void * unused)
kaf24@3362 360 {
kaf24@3362 361 unsigned long flags;
kaf24@3362 362 unsigned int val[2];
kaf24@3362 363 int cpu_num = smp_processor_id();
kaf24@3362 364 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
kaf24@3362 365
kaf24@3362 366 if (uci->mc == NULL) {
kaf24@3362 367 printk(KERN_INFO "microcode: No suitable data for CPU%d\n", cpu_num);
kaf24@3362 368 return;
kaf24@3362 369 }
kaf24@3362 370
kaf24@3362 371 /* serialize access to the physical write to MSR 0x79 */
kaf24@3362 372 spin_lock_irqsave(&microcode_update_lock, flags);
kaf24@3362 373
kaf24@3362 374 /* write microcode via MSR 0x79 */
kaf24@3362 375 wrmsr(MSR_IA32_UCODE_WRITE,
kaf24@3362 376 (unsigned long) uci->mc->bits,
kaf24@3362 377 (unsigned long) uci->mc->bits >> 16 >> 16);
kaf24@3362 378 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
kaf24@3362 379
kaf24@3362 380 __asm__ __volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx");
kaf24@3362 381 /* get the current revision from MSR 0x8B */
kaf24@3362 382 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
kaf24@3362 383
kaf24@3362 384 /* notify the caller of success on this cpu */
kaf24@3362 385 uci->err = MC_SUCCESS;
kaf24@3362 386 spin_unlock_irqrestore(&microcode_update_lock, flags);
kaf24@3362 387 printk(KERN_INFO "microcode: CPU%d updated from revision "
kaf24@3362 388 "0x%x to 0x%x, date = %08x \n",
kaf24@3362 389 cpu_num, uci->rev, val[1], uci->mc->hdr.date);
kaf24@3362 390 return;
kaf24@3362 391 }
kaf24@3362 392
kaf24@3362 393 static int do_microcode_update (void)
kaf24@3362 394 {
kaf24@3362 395 int i, error;
kaf24@3362 396
kaf24@3362 397 if (on_each_cpu(collect_cpu_info, NULL, 1, 1) != 0) {
kaf24@3362 398 printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
kaf24@3362 399 error = -EIO;
kaf24@3362 400 goto out;
kaf24@3362 401 }
kaf24@3362 402
kaf24@3362 403 if ((error = find_matching_ucodes())) {
kaf24@3362 404 printk(KERN_ERR "microcode: Error in the microcode data\n");
kaf24@3362 405 goto out_free;
kaf24@3362 406 }
kaf24@3362 407
kaf24@3362 408 if (on_each_cpu(do_update_one, NULL, 1, 1) != 0) {
kaf24@3362 409 printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
kaf24@3362 410 error = -EIO;
kaf24@3362 411 }
kaf24@3362 412
kaf24@3362 413 out_free:
kaf24@3362 414 for (i = 0; i < num_online_cpus(); i++) {
kaf24@3362 415 if (ucode_cpu_info[i].mc) {
kaf24@3362 416 int j;
kaf24@3362 417 void *tmp = ucode_cpu_info[i].mc;
kaf24@3362 418 vfree(tmp);
kaf24@3362 419 for (j = i; j < num_online_cpus(); j++) {
kaf24@3362 420 if (ucode_cpu_info[j].mc == tmp)
kaf24@3362 421 ucode_cpu_info[j].mc = NULL;
kaf24@3362 422 }
kaf24@3362 423 }
kaf24@3362 424 }
kaf24@3362 425 out:
kaf24@3362 426 return error;
kaf24@3362 427 }
kaf24@3362 428
kaf24@3362 429 int microcode_update(void *buf, unsigned long len)
kaf24@3362 430 {
kaf24@3362 431 int ret;
kaf24@3362 432
kaf24@3362 433 if (len < DEFAULT_UCODE_TOTALSIZE) {
kaf24@3362 434 printk(KERN_ERR "microcode: not enough data\n");
kaf24@3362 435 return -EINVAL;
kaf24@3362 436 }
kaf24@3362 437
kaf24@3362 438 down(&microcode_sem);
kaf24@3362 439
kaf24@3362 440 user_buffer = (void __user *) buf;
kaf24@3362 441 user_buffer_size = (int) len;
kaf24@3362 442
kaf24@3362 443 ret = do_microcode_update();
kaf24@3362 444
kaf24@3362 445 up(&microcode_sem);
kaf24@3362 446
kaf24@3362 447 return ret;
kaf24@3362 448 }