ia64/xen-unstable

annotate xen/arch/x86/microcode.c @ 18417:481f0dc6beb0

x86: constify microcode hypercall argument

Linux 2.6.27 marks the data pointer in its firmware struct 'const',
and hence, to avoid a compiler warning, Xen's microcode update
interface should be properly properly constified too.

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