ia64/xen-unstable

annotate xen/arch/x86/microcode.c @ 18499:3eb7a0cfffc2

x86, microcode: More code cleanups.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Sep 16 13:09:04 2008 +0100 (2008-09-16)
parents 087b8b29b6b2
children 879330497672
rev   line source
kaf24@3362 1 /*
keir@18497 2 * Intel CPU Microcode Update Driver for Linux
keir@18489 3 *
keir@18497 4 * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
keir@18497 5 * 2006 Shaohua Li <shaohua.li@intel.com> *
keir@18497 6 * This driver allows to upgrade microcode on Intel processors
keir@18497 7 * belonging to IA-32 family - PentiumPro, Pentium II,
keir@18497 8 * Pentium III, Xeon, Pentium 4, etc.
keir@18497 9 *
keir@18497 10 * Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
keir@18497 11 * Software Developer's Manual
keir@18497 12 * Order Number 253668 or free download from:
keir@18497 13 *
keir@18497 14 * http://developer.intel.com/design/pentium4/manuals/253668.htm
keir@18497 15 *
keir@18497 16 * For more information, go to http://www.urbanmyth.org/microcode
keir@18497 17 *
keir@18497 18 * This program is free software; you can redistribute it and/or
keir@18497 19 * modify it under the terms of the GNU General Public License
keir@18497 20 * as published by the Free Software Foundation; either version
keir@18497 21 * 2 of the License, or (at your option) any later version.
kaf24@3362 22 */
kaf24@3362 23
kaf24@3362 24 #include <xen/config.h>
cl349@5285 25 #include <xen/lib.h>
kaf24@3362 26 #include <xen/kernel.h>
kaf24@3362 27 #include <xen/init.h>
kaf24@3362 28 #include <xen/sched.h>
cl349@5287 29 #include <xen/smp.h>
kaf24@3362 30 #include <xen/spinlock.h>
keir@18489 31 #include <xen/guest_access.h>
kaf24@3362 32
cl349@5291 33 #include <asm/current.h>
kaf24@3362 34 #include <asm/msr.h>
kaf24@3362 35 #include <asm/uaccess.h>
kaf24@3362 36 #include <asm/processor.h>
keir@18489 37 #include <asm/microcode.h>
kaf24@3362 38
keir@18489 39 const struct microcode_ops *microcode_ops;
kaf24@3362 40
keir@18489 41 static DEFINE_SPINLOCK(microcode_mutex);
kaf24@3362 42
keir@18489 43 struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
keir@18489 44
keir@18489 45 struct microcode_buffer {
keir@18497 46 void *buf;
keir@18497 47 size_t size;
keir@18489 48 };
keir@18489 49
keir@18489 50 static struct microcode_buffer microcode_buffer;
keir@18489 51 static bool_t microcode_error;
keir@18489 52
keir@18489 53 static void microcode_fini_cpu(int cpu)
kaf24@3362 54 {
keir@18497 55 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
kaf24@3362 56
keir@18497 57 spin_lock(&microcode_mutex);
keir@18499 58 xfree(uci->mc.valid_mc);
keir@18499 59 uci->mc.valid_mc = NULL;
keir@18497 60 uci->valid = 0;
keir@18497 61 spin_unlock(&microcode_mutex);
keir@18489 62 }
kaf24@3362 63
keir@18489 64 static int collect_cpu_info(int cpu)
keir@18489 65 {
keir@18497 66 int err = 0;
keir@18497 67 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
keir@18489 68
keir@18497 69 memset(uci, 0, sizeof(*uci));
keir@18497 70 err = microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig);
keir@18497 71 if ( !err )
keir@18497 72 uci->valid = 1;
keir@18489 73
keir@18497 74 return err;
keir@18489 75 }
keir@18489 76
keir@18489 77 static int microcode_resume_cpu(int cpu)
keir@18489 78 {
keir@18497 79 int err = 0;
keir@18497 80 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
keir@18497 81 struct cpu_signature nsig;
keir@18489 82
keir@18497 83 gdprintk(XENLOG_INFO, "microcode: CPU%d resumed\n", cpu);
kaf24@3362 84
keir@18497 85 if ( !uci->mc.valid_mc )
keir@18497 86 return -EIO;
keir@18489 87
keir@18497 88 /*
keir@18497 89 * Let's verify that the 'cached' ucode does belong
keir@18497 90 * to this cpu (a bit of paranoia):
keir@18497 91 */
keir@18497 92 err = microcode_ops->collect_cpu_info(cpu, &nsig);
keir@18497 93 if ( err )
keir@18497 94 {
keir@18497 95 microcode_fini_cpu(cpu);
keir@18497 96 return err;
keir@18497 97 }
keir@18489 98
keir@18497 99 if ( memcmp(&nsig, &uci->cpu_sig, sizeof(nsig)) )
keir@18497 100 {
keir@18497 101 microcode_fini_cpu(cpu);
keir@18497 102 /* Should we look for a new ucode here? */
keir@18497 103 return -EIO;
keir@18497 104 }
keir@18497 105
keir@18497 106 err = microcode_ops->apply_microcode(cpu);
keir@18497 107
keir@18497 108 return err;
kaf24@3362 109 }
kaf24@3362 110
keir@18489 111 static int microcode_update_cpu(int cpu, const void *buf, size_t size)
kaf24@3362 112 {
keir@18497 113 int err = 0;
keir@18497 114 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
keir@18489 115
keir@18497 116 /* We should bind the task to the CPU */
keir@18497 117 BUG_ON(raw_smp_processor_id() != cpu);
keir@18489 118
keir@18497 119 spin_lock(&microcode_mutex);
keir@18489 120
keir@18497 121 /*
keir@18497 122 * Check if the system resume is in progress (uci->valid != NULL),
keir@18497 123 * otherwise just request a firmware:
keir@18497 124 */
keir@18497 125 if ( uci->valid )
keir@18497 126 {
keir@18497 127 err = microcode_resume_cpu(cpu);
keir@18497 128 }
keir@18497 129 else
keir@18497 130 {
keir@18497 131 err = collect_cpu_info(cpu);
keir@18497 132 if ( !err && uci->valid )
keir@18497 133 err = microcode_ops->cpu_request_microcode(cpu, buf, size);
keir@18497 134 }
keir@18497 135
keir@18497 136 spin_unlock(&microcode_mutex);
keir@18497 137
keir@18497 138 return err;
kaf24@3362 139 }
kaf24@3362 140
keir@18489 141 static void do_microcode_update_one(void *info)
kaf24@3362 142 {
keir@18499 143 int error = microcode_update_cpu(
keir@18497 144 smp_processor_id(), microcode_buffer.buf, microcode_buffer.size);
keir@18497 145 if ( error )
keir@18497 146 microcode_error = error;
kaf24@3362 147 }
kaf24@3362 148
keir@18489 149 static int do_microcode_update(void)
kaf24@3362 150 {
keir@18497 151 microcode_error = 0;
kaf24@3362 152
keir@18497 153 if ( on_each_cpu(do_microcode_update_one, NULL, 1, 1) != 0 )
keir@18497 154 {
keir@18497 155 printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
keir@18499 156 return -EIO;
keir@18497 157 }
kaf24@3362 158
keir@18499 159 return microcode_error;
kaf24@3362 160 }
kaf24@3362 161
keir@18417 162 int microcode_update(XEN_GUEST_HANDLE(const_void) buf, unsigned long len)
kaf24@3362 163 {
keir@18497 164 int ret;
keir@18489 165
keir@18497 166 /* XXX FIXME: No allocations in interrupt context. */
keir@18497 167 return -EINVAL;
keir@18489 168
keir@18497 169 if ( len != (typeof(microcode_buffer.size))len )
keir@18497 170 {
keir@18497 171 printk(KERN_ERR "microcode: too much data\n");
keir@18497 172 return -E2BIG;
keir@18497 173 }
kfraser@10545 174
keir@18499 175 if ( microcode_ops == NULL )
keir@18497 176 return -EINVAL;
kfraser@10545 177
keir@18497 178 microcode_buffer.buf = xmalloc_array(uint8_t, len);
keir@18497 179 if ( microcode_buffer.buf == NULL )
keir@18497 180 return -ENOMEM;
keir@18497 181
keir@18497 182 ret = copy_from_guest(microcode_buffer.buf, buf, len);
keir@18497 183 if ( ret != 0 )
keir@18497 184 return ret;
keir@18497 185
keir@18497 186 microcode_buffer.size = len;
keir@18497 187 wmb();
keir@18497 188
keir@18497 189 ret = do_microcode_update();
keir@18497 190
keir@18497 191 xfree(microcode_buffer.buf);
keir@18497 192 microcode_buffer.buf = NULL;
keir@18497 193 microcode_buffer.size = 0;
keir@18497 194
keir@18497 195 return ret;
kaf24@3362 196 }