ia64/xen-unstable

annotate xen/arch/x86/microcode.c @ 18489:15b1c3d4459a

x86: microcode update support for AMD CPUs

Microcode update support for AMD CPUs Family10h and Family11h.
It is based on a patch for Linux which is on its way for 2.6.28.

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Sep 12 14:18:13 2008 +0100 (2008-09-12)
parents 481f0dc6beb0
children 087b8b29b6b2
rev   line source
kaf24@3362 1 /*
keir@18489 2 * Intel CPU Microcode Update Driver for Linux
kaf24@3362 3 *
keir@18489 4 * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
keir@18489 5 * 2006 Shaohua Li <shaohua.li@intel.com> *
keir@18489 6 * This driver allows to upgrade microcode on Intel processors
keir@18489 7 * belonging to IA-32 family - PentiumPro, Pentium II,
keir@18489 8 * Pentium III, Xeon, Pentium 4, etc.
keir@18489 9 *
keir@18489 10 * Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
keir@18489 11 * Software Developer's Manual
keir@18489 12 * Order Number 253668 or free download from:
keir@18489 13 *
keir@18489 14 * http://developer.intel.com/design/pentium4/manuals/253668.htm
keir@18489 15 *
keir@18489 16 * For more information, go to http://www.urbanmyth.org/microcode
keir@18489 17 *
keir@18489 18 * This program is free software; you can redistribute it and/or
keir@18489 19 * modify it under the terms of the GNU General Public License
keir@18489 20 * as published by the Free Software Foundation; either version
keir@18489 21 * 2 of the License, or (at your option) any later version.
keir@18489 22 *
keir@18489 23 * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com>
keir@18489 24 * Initial release.
keir@18489 25 * 1.01 18 Feb 2000, Tigran Aivazian <tigran@sco.com>
keir@18489 26 * Added read() support + cleanups.
keir@18489 27 * 1.02 21 Feb 2000, Tigran Aivazian <tigran@sco.com>
keir@18489 28 * Added 'device trimming' support. open(O_WRONLY) zeroes
keir@18489 29 * and frees the saved copy of applied microcode.
keir@18489 30 * 1.03 29 Feb 2000, Tigran Aivazian <tigran@sco.com>
keir@18489 31 * Made to use devfs (/dev/cpu/microcode) + cleanups.
keir@18489 32 * 1.04 06 Jun 2000, Simon Trimmer <simon@veritas.com>
keir@18489 33 * Added misc device support (now uses both devfs and misc).
keir@18489 34 * Added MICROCODE_IOCFREE ioctl to clear memory.
keir@18489 35 * 1.05 09 Jun 2000, Simon Trimmer <simon@veritas.com>
keir@18489 36 * Messages for error cases (non Intel & no suitable microcode).
keir@18489 37 * 1.06 03 Aug 2000, Tigran Aivazian <tigran@veritas.com>
keir@18489 38 * Removed ->release(). Removed exclusive open and status bitmap.
keir@18489 39 * Added microcode_rwsem to serialize read()/write()/ioctl().
keir@18489 40 * Removed global kernel lock usage.
keir@18489 41 * 1.07 07 Sep 2000, Tigran Aivazian <tigran@veritas.com>
keir@18489 42 * Write 0 to 0x8B msr and then cpuid before reading revision,
keir@18489 43 * so that it works even if there were no update done by the
keir@18489 44 * BIOS. Otherwise, reading from 0x8B gives junk (which happened
keir@18489 45 * to be 0 on my machine which is why it worked even when I
keir@18489 46 * disabled update by the BIOS)
keir@18489 47 * Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix.
keir@18489 48 * 1.08 11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and
keir@18489 49 * Tigran Aivazian <tigran@veritas.com>
keir@18489 50 * Intel Pentium 4 processor support and bugfixes.
keir@18489 51 * 1.09 30 Oct 2001, Tigran Aivazian <tigran@veritas.com>
keir@18489 52 * Bugfix for HT (Hyper-Threading) enabled processors
keir@18489 53 * whereby processor resources are shared by all logical processors
keir@18489 54 * in a single CPU package.
keir@18489 55 * 1.10 28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and
keir@18489 56 * Tigran Aivazian <tigran@veritas.com>,
keir@18489 57 * Serialize updates as required on HT processors due to
keir@18489 58 * speculative nature of implementation.
keir@18489 59 * 1.11 22 Mar 2002 Tigran Aivazian <tigran@veritas.com>
keir@18489 60 * Fix the panic when writing zero-length microcode chunk.
keir@18489 61 * 1.12 29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>,
keir@18489 62 * Jun Nakajima <jun.nakajima@intel.com>
keir@18489 63 * Support for the microcode updates in the new format.
keir@18489 64 * 1.13 10 Oct 2003 Tigran Aivazian <tigran@veritas.com>
keir@18489 65 * Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl
keir@18489 66 * because we no longer hold a copy of applied microcode
keir@18489 67 * in kernel memory.
keir@18489 68 * 1.14 25 Jun 2004 Tigran Aivazian <tigran@veritas.com>
keir@18489 69 * Fix sigmatch() macro to handle old CPUs with pf == 0.
keir@18489 70 * Thanks to Stuart Swales for pointing out this bug.
kaf24@3362 71 */
kaf24@3362 72
kaf24@3362 73 #include <xen/config.h>
cl349@5285 74 #include <xen/lib.h>
kaf24@3362 75 #include <xen/kernel.h>
kaf24@3362 76 #include <xen/init.h>
kaf24@3362 77 #include <xen/sched.h>
cl349@5287 78 #include <xen/smp.h>
kaf24@3362 79 #include <xen/spinlock.h>
keir@18489 80 #include <xen/guest_access.h>
kaf24@3362 81
cl349@5291 82 #include <asm/current.h>
kaf24@3362 83 #include <asm/msr.h>
kaf24@3362 84 #include <asm/uaccess.h>
kaf24@3362 85 #include <asm/processor.h>
keir@18489 86 #include <asm/microcode.h>
kaf24@3362 87
kfraser@10545 88 static int verbose;
kfraser@10545 89 boolean_param("microcode.verbose", verbose);
kfraser@10545 90
keir@18489 91 const struct microcode_ops *microcode_ops;
kaf24@3362 92
keir@18489 93 static DEFINE_SPINLOCK(microcode_mutex);
kaf24@3362 94
keir@18489 95 struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
keir@18489 96
keir@18489 97 struct microcode_buffer {
keir@18489 98 void *buf;
keir@18489 99 size_t size;
keir@18489 100 };
keir@18489 101
keir@18489 102 static struct microcode_buffer microcode_buffer;
keir@18489 103 static bool_t microcode_error;
keir@18489 104
keir@18489 105 static void microcode_fini_cpu(int cpu)
kaf24@3362 106 {
keir@18489 107 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
kaf24@3362 108
keir@18489 109 spin_lock(&microcode_mutex);
keir@18489 110 microcode_ops->microcode_fini_cpu(cpu);
keir@18489 111 uci->valid = 0;
keir@18489 112 spin_unlock(&microcode_mutex);
keir@18489 113 }
kaf24@3362 114
keir@18489 115 static int collect_cpu_info(int cpu)
keir@18489 116 {
keir@18489 117 int err = 0;
keir@18489 118 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
keir@18489 119
keir@18489 120 memset(uci, 0, sizeof(*uci));
keir@18489 121 err = microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig);
keir@18489 122 if (!err)
keir@18489 123 uci->valid = 1;
keir@18489 124
keir@18489 125 return err;
keir@18489 126 }
keir@18489 127
keir@18489 128 static int microcode_resume_cpu(int cpu)
keir@18489 129 {
keir@18489 130 int err = 0;
keir@18489 131 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
keir@18489 132 struct cpu_signature nsig;
keir@18489 133
keir@18489 134 gdprintk(XENLOG_INFO, "microcode: CPU%d resumed\n", cpu);
keir@18489 135
keir@18489 136 if (!uci->mc.valid_mc)
keir@18489 137 return -EIO;
keir@18489 138
keir@18489 139 /*
keir@18489 140 * Let's verify that the 'cached' ucode does belong
keir@18489 141 * to this cpu (a bit of paranoia):
keir@18489 142 */
keir@18489 143 err = microcode_ops->collect_cpu_info(cpu, &nsig);
keir@18489 144 if (err) {
keir@18489 145 microcode_fini_cpu(cpu);
keir@18489 146 return err;
kaf24@3362 147 }
kaf24@3362 148
keir@18489 149 if (memcmp(&nsig, &uci->cpu_sig, sizeof(nsig))) {
keir@18489 150 microcode_fini_cpu(cpu);
keir@18489 151 /* Should we look for a new ucode here? */
keir@18489 152 return -EIO;
keir@18489 153 }
keir@18489 154
keir@18489 155 err = microcode_ops->apply_microcode(cpu);
keir@18489 156
keir@18489 157 return err;
kaf24@3362 158 }
kaf24@3362 159
keir@18489 160 static int microcode_update_cpu(int cpu, const void *buf, size_t size)
kaf24@3362 161 {
keir@18489 162 int err = 0;
keir@18489 163 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
kaf24@3362 164
keir@18489 165 /* We should bind the task to the CPU */
keir@18489 166 BUG_ON(raw_smp_processor_id() != cpu);
keir@18489 167
keir@18489 168 spin_lock(&microcode_mutex);
keir@18489 169 /*
keir@18489 170 * Check if the system resume is in progress (uci->valid != NULL),
keir@18489 171 * otherwise just request a firmware:
keir@18489 172 */
keir@18489 173 if (uci->valid) {
keir@18489 174 err = microcode_resume_cpu(cpu);
keir@18489 175 } else {
keir@18489 176 err = collect_cpu_info(cpu);
keir@18489 177 if (err)
keir@18489 178 goto out;
keir@18489 179 if (uci->valid) {
keir@18489 180 err = microcode_ops->cpu_request_microcode(cpu, buf, size);
kfraser@10545 181 }
kaf24@3362 182 }
keir@18489 183
keir@18489 184 out:
keir@18489 185 spin_unlock(&microcode_mutex);
keir@18489 186
keir@18489 187 return err;
kaf24@3362 188 }
kaf24@3362 189
keir@18489 190 static void do_microcode_update_one(void *info)
kaf24@3362 191 {
kaf24@3362 192 int error = 0;
kaf24@3362 193
keir@18489 194 error = microcode_update_cpu(smp_processor_id(),
keir@18489 195 microcode_buffer.buf, microcode_buffer.size);
kaf24@3362 196
keir@18489 197 if (error)
keir@18489 198 microcode_error = error;
kaf24@3362 199 }
kaf24@3362 200
keir@18489 201 static int do_microcode_update(void)
kaf24@3362 202 {
keir@18489 203 int error = 0;
kaf24@3362 204
keir@18489 205 microcode_error = 0;
kaf24@3362 206
keir@18489 207 if (on_each_cpu(do_microcode_update_one, NULL, 1, 1) != 0) {
kaf24@3362 208 printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
kaf24@3362 209 error = -EIO;
kaf24@3362 210 goto out;
kaf24@3362 211 }
kaf24@3362 212
keir@18489 213 if (microcode_error) {
keir@18489 214 error = microcode_error;
keir@18489 215 goto out;
kaf24@3362 216 }
kaf24@3362 217
kaf24@3362 218 out:
kaf24@3362 219 return error;
kaf24@3362 220 }
kaf24@3362 221
keir@18417 222 int microcode_update(XEN_GUEST_HANDLE(const_void) buf, unsigned long len)
kaf24@3362 223 {
kfraser@10545 224 int ret;
keir@18489 225 struct cpuinfo_x86 *c = &boot_cpu_data;
kaf24@3362 226
keir@18489 227 if (len != (typeof(microcode_buffer.size))len) {
kfraser@11757 228 printk(KERN_ERR "microcode: too much data\n");
kfraser@11757 229 return -E2BIG;
kfraser@11757 230 }
kfraser@11757 231
keir@18489 232 switch (c->x86_vendor) {
keir@18489 233 case X86_VENDOR_AMD:
keir@18489 234 ret = microcode_init_amd(c);
keir@18489 235 break;
kfraser@10545 236
keir@18489 237 case X86_VENDOR_INTEL:
keir@18489 238 ret = microcode_init_intel(c);
keir@18489 239 break;
keir@18489 240 default:
keir@18489 241 printk(KERN_ERR "microcode: CPU vendor not supported\n");
keir@18489 242 ret = -EINVAL;
keir@18489 243 break;
keir@18489 244 }
keir@18489 245
keir@18489 246 if (ret != 0)
keir@18489 247 return ret;
keir@18489 248
keir@18489 249 microcode_buffer.buf = xmalloc_array(uint8_t, len);
keir@18489 250 if (!microcode_buffer.buf)
keir@18489 251 return -ENOMEM;
keir@18489 252
keir@18489 253 ret = copy_from_guest(microcode_buffer.buf, buf, len);
keir@18489 254 if (ret != 0)
keir@18489 255 return ret;
keir@18489 256
keir@18489 257 microcode_buffer.size = len;
keir@18489 258 wmb();
kfraser@10545 259
kfraser@10545 260 ret = do_microcode_update();
kfraser@10545 261
keir@18489 262 xfree(microcode_buffer.buf);
keir@18489 263 microcode_buffer.buf = NULL;
keir@18489 264 microcode_buffer.size = 0;
kfraser@10545 265
kfraser@10545 266 return ret;
kaf24@3362 267 }