ia64/xen-unstable

changeset 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 e827c54462d3
children 12f3edfab6ef
files xen/arch/x86/Makefile xen/arch/x86/microcode.c xen/arch/x86/microcode_amd.c xen/arch/x86/microcode_intel.c xen/include/asm-x86/microcode.h xen/include/asm-x86/msr-index.h xen/include/asm-x86/processor.h
line diff
     1.1 --- a/xen/arch/x86/Makefile	Fri Sep 12 13:15:36 2008 +0100
     1.2 +++ b/xen/arch/x86/Makefile	Fri Sep 12 14:18:13 2008 +0100
     1.3 @@ -28,6 +28,8 @@ obj-y += msi.o
     1.4  obj-y += ioport_emulate.o
     1.5  obj-y += irq.o
     1.6  obj-y += microcode.o
     1.7 +obj-y += microcode_amd.o
     1.8 +obj-y += microcode_intel.o
     1.9  obj-y += mm.o
    1.10  obj-y += mpparse.o
    1.11  obj-y += nmi.o
     2.1 --- a/xen/arch/x86/microcode.c	Fri Sep 12 13:15:36 2008 +0100
     2.2 +++ b/xen/arch/x86/microcode.c	Fri Sep 12 14:18:13 2008 +0100
     2.3 @@ -1,72 +1,73 @@
     2.4  /*
     2.5 - *	Intel CPU Microcode Update Driver for Linux
     2.6 - *
     2.7 - *	Copyright (C) 2000-2004 Tigran Aivazian
     2.8 - *
     2.9 - *	This driver allows to upgrade microcode on Intel processors
    2.10 - *	belonging to IA-32 family - PentiumPro, Pentium II, 
    2.11 - *	Pentium III, Xeon, Pentium 4, etc.
    2.12 - *
    2.13 - *	Reference: Section 8.10 of Volume III, Intel Pentium 4 Manual, 
    2.14 - *	Order Number 245472 or free download from:
    2.15 - *		
    2.16 - *	http://developer.intel.com/design/pentium4/manuals/245472.htm
    2.17 - *
    2.18 - *	For more information, go to http://www.urbanmyth.org/microcode
    2.19 - *
    2.20 - *	This program is free software; you can redistribute it and/or
    2.21 - *	modify it under the terms of the GNU General Public License
    2.22 - *	as published by the Free Software Foundation; either version
    2.23 - *	2 of the License, or (at your option) any later version.
    2.24 + *      Intel CPU Microcode Update Driver for Linux
    2.25   *
    2.26 - *	1.0	16 Feb 2000, Tigran Aivazian <tigran@sco.com>
    2.27 - *		Initial release.
    2.28 - *	1.01	18 Feb 2000, Tigran Aivazian <tigran@sco.com>
    2.29 - *		Added read() support + cleanups.
    2.30 - *	1.02	21 Feb 2000, Tigran Aivazian <tigran@sco.com>
    2.31 - *		Added 'device trimming' support. open(O_WRONLY) zeroes
    2.32 - *		and frees the saved copy of applied microcode.
    2.33 - *	1.03	29 Feb 2000, Tigran Aivazian <tigran@sco.com>
    2.34 - *		Made to use devfs (/dev/cpu/microcode) + cleanups.
    2.35 - *	1.04	06 Jun 2000, Simon Trimmer <simon@veritas.com>
    2.36 - *		Added misc device support (now uses both devfs and misc).
    2.37 - *		Added MICROCODE_IOCFREE ioctl to clear memory.
    2.38 - *	1.05	09 Jun 2000, Simon Trimmer <simon@veritas.com>
    2.39 - *		Messages for error cases (non Intel & no suitable microcode).
    2.40 - *	1.06	03 Aug 2000, Tigran Aivazian <tigran@veritas.com>
    2.41 - *		Removed ->release(). Removed exclusive open and status bitmap.
    2.42 - *		Added microcode_rwsem to serialize read()/write()/ioctl().
    2.43 - *		Removed global kernel lock usage.
    2.44 - *	1.07	07 Sep 2000, Tigran Aivazian <tigran@veritas.com>
    2.45 - *		Write 0 to 0x8B msr and then cpuid before reading revision,
    2.46 - *		so that it works even if there were no update done by the
    2.47 - *		BIOS. Otherwise, reading from 0x8B gives junk (which happened
    2.48 - *		to be 0 on my machine which is why it worked even when I
    2.49 - *		disabled update by the BIOS)
    2.50 - *		Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix.
    2.51 - *	1.08	11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and
    2.52 - *			     Tigran Aivazian <tigran@veritas.com>
    2.53 - *		Intel Pentium 4 processor support and bugfixes.
    2.54 - *	1.09	30 Oct 2001, Tigran Aivazian <tigran@veritas.com>
    2.55 - *		Bugfix for HT (Hyper-Threading) enabled processors
    2.56 - *		whereby processor resources are shared by all logical processors
    2.57 - *		in a single CPU package.
    2.58 - *	1.10	28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and
    2.59 - *		Tigran Aivazian <tigran@veritas.com>,
    2.60 - *		Serialize updates as required on HT processors due to speculative
    2.61 - *		nature of implementation.
    2.62 - *	1.11	22 Mar 2002 Tigran Aivazian <tigran@veritas.com>
    2.63 - *		Fix the panic when writing zero-length microcode chunk.
    2.64 - *	1.12	29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>, 
    2.65 - *		Jun Nakajima <jun.nakajima@intel.com>
    2.66 - *		Support for the microcode updates in the new format.
    2.67 - *	1.13	10 Oct 2003 Tigran Aivazian <tigran@veritas.com>
    2.68 - *		Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl
    2.69 - *		because we no longer hold a copy of applied microcode 
    2.70 - *		in kernel memory.
    2.71 - *	1.14	25 Jun 2004 Tigran Aivazian <tigran@veritas.com>
    2.72 - *		Fix sigmatch() macro to handle old CPUs with pf == 0.
    2.73 - *		Thanks to Stuart Swales for pointing out this bug.
    2.74 + *      Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
    2.75 + *                    2006      Shaohua Li <shaohua.li@intel.com> *
    2.76 + *      This driver allows to upgrade microcode on Intel processors
    2.77 + *      belonging to IA-32 family - PentiumPro, Pentium II,
    2.78 + *      Pentium III, Xeon, Pentium 4, etc.
    2.79 + *
    2.80 + *      Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
    2.81 + *      Software Developer's Manual
    2.82 + *      Order Number 253668 or free download from:
    2.83 + *
    2.84 + *      http://developer.intel.com/design/pentium4/manuals/253668.htm
    2.85 + *
    2.86 + *      For more information, go to http://www.urbanmyth.org/microcode
    2.87 + *
    2.88 + *      This program is free software; you can redistribute it and/or
    2.89 + *      modify it under the terms of the GNU General Public License
    2.90 + *      as published by the Free Software Foundation; either version
    2.91 + *      2 of the License, or (at your option) any later version.
    2.92 + *
    2.93 + *      1.0     16 Feb 2000, Tigran Aivazian <tigran@sco.com>
    2.94 + *              Initial release.
    2.95 + *      1.01    18 Feb 2000, Tigran Aivazian <tigran@sco.com>
    2.96 + *              Added read() support + cleanups.
    2.97 + *      1.02    21 Feb 2000, Tigran Aivazian <tigran@sco.com>
    2.98 + *              Added 'device trimming' support. open(O_WRONLY) zeroes
    2.99 + *              and frees the saved copy of applied microcode.
   2.100 + *      1.03    29 Feb 2000, Tigran Aivazian <tigran@sco.com>
   2.101 + *              Made to use devfs (/dev/cpu/microcode) + cleanups.
   2.102 + *      1.04    06 Jun 2000, Simon Trimmer <simon@veritas.com>
   2.103 + *              Added misc device support (now uses both devfs and misc).
   2.104 + *              Added MICROCODE_IOCFREE ioctl to clear memory.
   2.105 + *      1.05    09 Jun 2000, Simon Trimmer <simon@veritas.com>
   2.106 + *              Messages for error cases (non Intel & no suitable microcode).
   2.107 + *      1.06    03 Aug 2000, Tigran Aivazian <tigran@veritas.com>
   2.108 + *              Removed ->release(). Removed exclusive open and status bitmap.
   2.109 + *              Added microcode_rwsem to serialize read()/write()/ioctl().
   2.110 + *              Removed global kernel lock usage.
   2.111 + *      1.07    07 Sep 2000, Tigran Aivazian <tigran@veritas.com>
   2.112 + *              Write 0 to 0x8B msr and then cpuid before reading revision,
   2.113 + *              so that it works even if there were no update done by the
   2.114 + *              BIOS. Otherwise, reading from 0x8B gives junk (which happened
   2.115 + *              to be 0 on my machine which is why it worked even when I
   2.116 + *              disabled update by the BIOS)
   2.117 + *              Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix.
   2.118 + *      1.08    11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and
   2.119 + *                           Tigran Aivazian <tigran@veritas.com>
   2.120 + *              Intel Pentium 4 processor support and bugfixes.
   2.121 + *      1.09    30 Oct 2001, Tigran Aivazian <tigran@veritas.com>
   2.122 + *              Bugfix for HT (Hyper-Threading) enabled processors
   2.123 + *              whereby processor resources are shared by all logical processors
   2.124 + *              in a single CPU package.
   2.125 + *      1.10    28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and
   2.126 + *              Tigran Aivazian <tigran@veritas.com>,
   2.127 + *              Serialize updates as required on HT processors due to
   2.128 + *              speculative nature of implementation.
   2.129 + *      1.11    22 Mar 2002 Tigran Aivazian <tigran@veritas.com>
   2.130 + *              Fix the panic when writing zero-length microcode chunk.
   2.131 + *      1.12    29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>,
   2.132 + *              Jun Nakajima <jun.nakajima@intel.com>
   2.133 + *              Support for the microcode updates in the new format.
   2.134 + *      1.13    10 Oct 2003 Tigran Aivazian <tigran@veritas.com>
   2.135 + *              Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl
   2.136 + *              because we no longer hold a copy of applied microcode
   2.137 + *              in kernel memory.
   2.138 + *      1.14    25 Jun 2004 Tigran Aivazian <tigran@veritas.com>
   2.139 + *              Fix sigmatch() macro to handle old CPUs with pf == 0.
   2.140 + *              Thanks to Stuart Swales for pointing out this bug.
   2.141   */
   2.142  
   2.143  #include <xen/config.h>
   2.144 @@ -76,381 +77,144 @@
   2.145  #include <xen/sched.h>
   2.146  #include <xen/smp.h>
   2.147  #include <xen/spinlock.h>
   2.148 +#include <xen/guest_access.h>
   2.149  
   2.150  #include <asm/current.h>
   2.151  #include <asm/msr.h>
   2.152  #include <asm/uaccess.h>
   2.153  #include <asm/processor.h>
   2.154 -
   2.155 -#define pr_debug(x...) ((void)0)
   2.156 -#define DEFINE_MUTEX(_m) DEFINE_SPINLOCK(_m)
   2.157 -#define mutex_lock(_m) spin_lock(_m)
   2.158 -#define mutex_unlock(_m) spin_unlock(_m)
   2.159 -#define vmalloc(_s) xmalloc_bytes(_s)
   2.160 -#define vfree(_p) xfree(_p)
   2.161 -
   2.162 -#if 0
   2.163 -MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
   2.164 -MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
   2.165 -MODULE_LICENSE("GPL");
   2.166 -#endif
   2.167 +#include <asm/microcode.h>
   2.168  
   2.169  static int verbose;
   2.170  boolean_param("microcode.verbose", verbose);
   2.171  
   2.172 -#define MICROCODE_VERSION 	"1.14a"
   2.173 -
   2.174 -#define DEFAULT_UCODE_DATASIZE 	(2000) 	  /* 2000 bytes */
   2.175 -#define MC_HEADER_SIZE		(sizeof (microcode_header_t))  	  /* 48 bytes */
   2.176 -#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
   2.177 -#define EXT_HEADER_SIZE		(sizeof (struct extended_sigtable)) /* 20 bytes */
   2.178 -#define EXT_SIGNATURE_SIZE	(sizeof (struct extended_signature)) /* 12 bytes */
   2.179 -#define DWSIZE			(sizeof (u32))
   2.180 -#define get_totalsize(mc) \
   2.181 -	(((microcode_t *)mc)->hdr.totalsize ? \
   2.182 -	 ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
   2.183 -#define get_datasize(mc) \
   2.184 -	(((microcode_t *)mc)->hdr.datasize ? \
   2.185 -	 ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
   2.186 -
   2.187 -#define sigmatch(s1, s2, p1, p2) \
   2.188 -	(((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
   2.189 -
   2.190 -#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
   2.191 -
   2.192 -/* serialize access to the physical write to MSR 0x79 */
   2.193 -static DEFINE_SPINLOCK(microcode_update_lock);
   2.194 -
   2.195 -/* no concurrent ->write()s are allowed on /dev/cpu/microcode */
   2.196 -static DEFINE_MUTEX(microcode_mutex);
   2.197 -
   2.198 -static const void __user *user_buffer;	/* user area microcode data buffer */
   2.199 -static unsigned int user_buffer_size;	/* it's size */
   2.200 +const struct microcode_ops *microcode_ops;
   2.201  
   2.202 -typedef enum mc_error_code {
   2.203 -	MC_SUCCESS 	= 0,
   2.204 -	MC_IGNORED 	= 1,
   2.205 -	MC_NOTFOUND 	= 2,
   2.206 -	MC_MARKED 	= 3,
   2.207 -	MC_ALLOCATED 	= 4,
   2.208 -} mc_error_code_t;
   2.209 +static DEFINE_SPINLOCK(microcode_mutex);
   2.210  
   2.211 -static struct ucode_cpu_info {
   2.212 -	unsigned int sig;
   2.213 -	unsigned int pf, orig_pf;
   2.214 -	unsigned int rev;
   2.215 -	unsigned int cksum;
   2.216 -	mc_error_code_t err;
   2.217 -	microcode_t *mc;
   2.218 -} ucode_cpu_info[NR_CPUS];
   2.219 -				
   2.220 -static void collect_cpu_info (void *unused)
   2.221 +struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
   2.222 +
   2.223 +struct microcode_buffer {
   2.224 +	void *buf;
   2.225 +	size_t size;
   2.226 +};
   2.227 +
   2.228 +static struct microcode_buffer microcode_buffer;
   2.229 +static bool_t microcode_error;
   2.230 +
   2.231 +static void microcode_fini_cpu(int cpu)
   2.232  {
   2.233 -	int cpu_num = smp_processor_id();
   2.234 -	struct cpuinfo_x86 *c = cpu_data + cpu_num;
   2.235 -	struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
   2.236 -	unsigned int val[2];
   2.237 -
   2.238 -	uci->sig = uci->pf = uci->rev = uci->cksum = 0;
   2.239 -	uci->err = MC_NOTFOUND;
   2.240 -	uci->mc = NULL;
   2.241 +	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
   2.242  
   2.243 -	if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
   2.244 -	    	cpu_has(c, X86_FEATURE_IA64)) {
   2.245 -		printk(KERN_ERR "microcode: CPU%d not a capable Intel processor\n", cpu_num);
   2.246 -		return;
   2.247 -	} else {
   2.248 -		uci->sig = cpuid_eax(0x00000001);
   2.249 +	spin_lock(&microcode_mutex);
   2.250 +	microcode_ops->microcode_fini_cpu(cpu);
   2.251 +	uci->valid = 0;
   2.252 +	spin_unlock(&microcode_mutex);
   2.253 +}
   2.254  
   2.255 -		if ((c->x86_model >= 5) || (c->x86 > 6)) {
   2.256 -			/* get processor flags from MSR 0x17 */
   2.257 -			rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
   2.258 -			uci->pf = 1 << ((val[1] >> 18) & 7);
   2.259 -		}
   2.260 -		uci->orig_pf = uci->pf;
   2.261 +static int collect_cpu_info(int cpu)
   2.262 +{
   2.263 +	int err = 0;
   2.264 +	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
   2.265 +
   2.266 +	memset(uci, 0, sizeof(*uci));
   2.267 +	err = microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig);
   2.268 +	if (!err)
   2.269 +		uci->valid = 1;
   2.270 +
   2.271 +	return err;
   2.272 +}
   2.273 +
   2.274 +static int microcode_resume_cpu(int cpu)
   2.275 +{
   2.276 +	int err = 0;
   2.277 +	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
   2.278 +	struct cpu_signature nsig;
   2.279 +
   2.280 +	gdprintk(XENLOG_INFO, "microcode: CPU%d resumed\n", cpu);
   2.281 +
   2.282 +	if (!uci->mc.valid_mc)
   2.283 +		return -EIO;
   2.284 +
   2.285 +	/*
   2.286 +	 * Let's verify that the 'cached' ucode does belong
   2.287 +	 * to this cpu (a bit of paranoia):
   2.288 +	 */
   2.289 +	err = microcode_ops->collect_cpu_info(cpu, &nsig);
   2.290 +	if (err) {
   2.291 +		microcode_fini_cpu(cpu);
   2.292 +		return err;
   2.293  	}
   2.294  
   2.295 -	wrmsr(MSR_IA32_UCODE_REV, 0, 0);
   2.296 -	/* see notes above for revision 1.07.  Apparent chip bug */
   2.297 -	sync_core();
   2.298 -	/* get the current revision from MSR 0x8B */
   2.299 -	rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
   2.300 -	pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
   2.301 -			uci->sig, uci->pf, uci->rev);
   2.302 +	if (memcmp(&nsig, &uci->cpu_sig, sizeof(nsig))) {
   2.303 +		microcode_fini_cpu(cpu);
   2.304 +		/* Should we look for a new ucode here? */
   2.305 +		return -EIO;
   2.306 +	}
   2.307 +
   2.308 +	err = microcode_ops->apply_microcode(cpu);
   2.309 +
   2.310 +	return err;
   2.311  }
   2.312  
   2.313 -static inline void mark_microcode_update (int cpu_num, microcode_header_t *mc_header, int sig, int pf, int cksum)
   2.314 +static int microcode_update_cpu(int cpu, const void *buf, size_t size)
   2.315  {
   2.316 -	struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
   2.317 -
   2.318 -	pr_debug("Microcode Found.\n");
   2.319 -	pr_debug("   Header Revision 0x%x\n", mc_header->hdrver);
   2.320 -	pr_debug("   Loader Revision 0x%x\n", mc_header->ldrver);
   2.321 -	pr_debug("   Revision 0x%x \n", mc_header->rev);
   2.322 -	pr_debug("   Date %x/%x/%x\n",
   2.323 -		((mc_header->date >> 24 ) & 0xff),
   2.324 -		((mc_header->date >> 16 ) & 0xff),
   2.325 -		(mc_header->date & 0xFFFF));
   2.326 -	pr_debug("   Signature 0x%x\n", sig);
   2.327 -	pr_debug("   Type 0x%x Family 0x%x Model 0x%x Stepping 0x%x\n",
   2.328 -		((sig >> 12) & 0x3),
   2.329 -		((sig >> 8) & 0xf),
   2.330 -		((sig >> 4) & 0xf),
   2.331 -		((sig & 0xf)));
   2.332 -	pr_debug("   Processor Flags 0x%x\n", pf);
   2.333 -	pr_debug("   Checksum 0x%x\n", cksum);
   2.334 +	int err = 0;
   2.335 +	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
   2.336  
   2.337 -	if (mc_header->rev < uci->rev) {
   2.338 -		if (uci->err == MC_NOTFOUND) {
   2.339 -			uci->err = MC_IGNORED;
   2.340 -			uci->cksum = mc_header->rev;
   2.341 -		} else if (uci->err == MC_IGNORED && uci->cksum < mc_header->rev)
   2.342 -			uci->cksum = mc_header->rev;
   2.343 -	} else if (mc_header->rev == uci->rev) {
   2.344 -		if (uci->err < MC_MARKED) {
   2.345 -			/* notify the caller of success on this cpu */
   2.346 -			uci->err = MC_SUCCESS;
   2.347 -		}
   2.348 -	} else if (uci->err != MC_ALLOCATED || mc_header->rev > uci->mc->hdr.rev) {
   2.349 -		pr_debug("microcode: CPU%d found a matching microcode update with "
   2.350 -			" revision 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
   2.351 -		uci->cksum = cksum;
   2.352 -		uci->pf = pf; /* keep the original mc pf for cksum calculation */
   2.353 -		uci->err = MC_MARKED; /* found the match */
   2.354 -		for_each_online_cpu(cpu_num) {
   2.355 -			if (ucode_cpu_info + cpu_num != uci
   2.356 -			    && ucode_cpu_info[cpu_num].mc == uci->mc) {
   2.357 -				uci->mc = NULL;
   2.358 -				break;
   2.359 -			}
   2.360 -		}
   2.361 -		if (uci->mc != NULL) {
   2.362 -			vfree(uci->mc);
   2.363 -			uci->mc = NULL;
   2.364 +	/* We should bind the task to the CPU */
   2.365 +	BUG_ON(raw_smp_processor_id() != cpu);
   2.366 +
   2.367 +	spin_lock(&microcode_mutex);
   2.368 +	/*
   2.369 +	 * Check if the system resume is in progress (uci->valid != NULL),
   2.370 +	 * otherwise just request a firmware:
   2.371 +	 */
   2.372 +	if (uci->valid) {
   2.373 +		err = microcode_resume_cpu(cpu);
   2.374 +	} else {
   2.375 +		err = collect_cpu_info(cpu);
   2.376 +		if (err)
   2.377 +			goto out;
   2.378 +		if (uci->valid) {
   2.379 +			err = microcode_ops->cpu_request_microcode(cpu, buf, size);
   2.380  		}
   2.381  	}
   2.382 -	return;
   2.383 +
   2.384 +out:
   2.385 +	spin_unlock(&microcode_mutex);
   2.386 +
   2.387 +	return err;
   2.388  }
   2.389  
   2.390 -static int find_matching_ucodes (void) 
   2.391 +static void do_microcode_update_one(void *info)
   2.392  {
   2.393 -	int cursor = 0;
   2.394  	int error = 0;
   2.395  
   2.396 -	while (cursor + MC_HEADER_SIZE < user_buffer_size) {
   2.397 -		microcode_header_t mc_header;
   2.398 -		void *newmc = NULL;
   2.399 -		int i, sum, cpu_num, allocated_flag, total_size, data_size, ext_table_size;
   2.400 -
   2.401 -		if (copy_from_user(&mc_header, user_buffer + cursor, MC_HEADER_SIZE)) {
   2.402 -			printk(KERN_ERR "microcode: error! Can not read user data\n");
   2.403 -			error = -EFAULT;
   2.404 -			goto out;
   2.405 -		}
   2.406 -
   2.407 -		total_size = get_totalsize(&mc_header);
   2.408 -		if (cursor + total_size > user_buffer_size) {
   2.409 -			printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
   2.410 -			error = -EINVAL;
   2.411 -			goto out;
   2.412 -		}
   2.413 -
   2.414 -		data_size = get_datasize(&mc_header);
   2.415 -		if (data_size + MC_HEADER_SIZE > total_size) {
   2.416 -			printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
   2.417 -			error = -EINVAL;
   2.418 -			goto out;
   2.419 -		}
   2.420 -
   2.421 -		if (mc_header.ldrver != 1 || mc_header.hdrver != 1) {
   2.422 -			printk(KERN_ERR "microcode: error! Unknown microcode update format\n");
   2.423 -			error = -EINVAL;
   2.424 -			goto out;
   2.425 -		}
   2.426 -
   2.427 -		for_each_online_cpu(cpu_num) {
   2.428 -			struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
   2.429 -
   2.430 -			if (sigmatch(mc_header.sig, uci->sig, mc_header.pf, uci->orig_pf))
   2.431 -				mark_microcode_update(cpu_num, &mc_header, mc_header.sig, mc_header.pf, mc_header.cksum);
   2.432 -		}
   2.433 -
   2.434 -		ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
   2.435 -		if (ext_table_size) {
   2.436 -			struct extended_sigtable ext_header;
   2.437 -			struct extended_signature ext_sig;
   2.438 -			int ext_sigcount;
   2.439 -
   2.440 -			if ((ext_table_size < EXT_HEADER_SIZE) 
   2.441 -					|| ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
   2.442 -				printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
   2.443 -				error = -EINVAL;
   2.444 -				goto out;
   2.445 -			}
   2.446 -			if (copy_from_user(&ext_header, user_buffer + cursor 
   2.447 -					+ MC_HEADER_SIZE + data_size, EXT_HEADER_SIZE)) {
   2.448 -				printk(KERN_ERR "microcode: error! Can not read user data\n");
   2.449 -				error = -EFAULT;
   2.450 -				goto out;
   2.451 -			}
   2.452 -			if (ext_table_size != exttable_size(&ext_header)) {
   2.453 -				printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
   2.454 -				error = -EFAULT;
   2.455 -				goto out;
   2.456 -			}
   2.457 +	error = microcode_update_cpu(smp_processor_id(),
   2.458 +			microcode_buffer.buf, microcode_buffer.size);
   2.459  
   2.460 -			ext_sigcount = ext_header.count;
   2.461 -			
   2.462 -			for (i = 0; i < ext_sigcount; i++) {
   2.463 -				if (copy_from_user(&ext_sig, user_buffer + cursor + MC_HEADER_SIZE + data_size + EXT_HEADER_SIZE 
   2.464 -						+ EXT_SIGNATURE_SIZE * i, EXT_SIGNATURE_SIZE)) {
   2.465 -					printk(KERN_ERR "microcode: error! Can not read user data\n");
   2.466 -					error = -EFAULT;
   2.467 -					goto out;
   2.468 -				}
   2.469 -				for_each_online_cpu(cpu_num) {
   2.470 -					struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
   2.471 -
   2.472 -					if (sigmatch(ext_sig.sig, uci->sig, ext_sig.pf, uci->orig_pf)) {
   2.473 -						mark_microcode_update(cpu_num, &mc_header, ext_sig.sig, ext_sig.pf, ext_sig.cksum);
   2.474 -					}
   2.475 -				}
   2.476 -			}
   2.477 -		}
   2.478 -		/* now check if any cpu has matched */
   2.479 -		allocated_flag = 0;
   2.480 -		sum = 0;
   2.481 -		for_each_online_cpu(cpu_num) {
   2.482 -			if (ucode_cpu_info[cpu_num].err == MC_MARKED) { 
   2.483 -				struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
   2.484 -				if (!allocated_flag) {
   2.485 -					allocated_flag = 1;
   2.486 -					newmc = vmalloc(total_size);
   2.487 -					if (!newmc) {
   2.488 -						printk(KERN_ERR "microcode: error! Can not allocate memory\n");
   2.489 -						error = -ENOMEM;
   2.490 -						goto out;
   2.491 -					}
   2.492 -					if (copy_from_user(newmc + MC_HEADER_SIZE, 
   2.493 -								user_buffer + cursor + MC_HEADER_SIZE, 
   2.494 -								total_size - MC_HEADER_SIZE)) {
   2.495 -						printk(KERN_ERR "microcode: error! Can not read user data\n");
   2.496 -						vfree(newmc);
   2.497 -						error = -EFAULT;
   2.498 -						goto out;
   2.499 -					}
   2.500 -					memcpy(newmc, &mc_header, MC_HEADER_SIZE);
   2.501 -					/* check extended table checksum */
   2.502 -					if (ext_table_size) {
   2.503 -						int ext_table_sum = 0;
   2.504 -						int * ext_tablep = (((void *) newmc) + MC_HEADER_SIZE + data_size);
   2.505 -						i = ext_table_size / DWSIZE;
   2.506 -						while (i--) ext_table_sum += ext_tablep[i];
   2.507 -						if (ext_table_sum) {
   2.508 -							printk(KERN_WARNING "microcode: aborting, bad extended signature table checksum\n");
   2.509 -							vfree(newmc);
   2.510 -							error = -EINVAL;
   2.511 -							goto out;
   2.512 -						}
   2.513 -					}
   2.514 -
   2.515 -					/* calculate the checksum */
   2.516 -					i = (MC_HEADER_SIZE + data_size) / DWSIZE;
   2.517 -					while (i--) sum += ((int *)newmc)[i];
   2.518 -					sum -= (mc_header.sig + mc_header.pf + mc_header.cksum);
   2.519 -				}
   2.520 -				ucode_cpu_info[cpu_num].mc = newmc;
   2.521 -				ucode_cpu_info[cpu_num].err = MC_ALLOCATED; /* mc updated */
   2.522 -				if (sum + uci->sig + uci->pf + uci->cksum != 0) {
   2.523 -					printk(KERN_ERR "microcode: CPU%d aborting, bad checksum\n", cpu_num);
   2.524 -					error = -EINVAL;
   2.525 -					goto out;
   2.526 -				}
   2.527 -			}
   2.528 -		}
   2.529 -		cursor += total_size; /* goto the next update patch */
   2.530 -	} /* end of while */
   2.531 -out:
   2.532 -	return error;
   2.533 +	if (error)
   2.534 +		microcode_error = error;	
   2.535  }
   2.536  
   2.537 -static void do_update_one (void * unused)
   2.538 +static int do_microcode_update(void)
   2.539  {
   2.540 -	unsigned long flags;
   2.541 -	unsigned int val[2];
   2.542 -	int cpu_num = smp_processor_id();
   2.543 -	struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
   2.544 -
   2.545 -	if (uci->mc == NULL) {
   2.546 -		if (verbose) {
   2.547 -			if (uci->err == MC_SUCCESS)
   2.548 -				printk(KERN_INFO "microcode: CPU%d already at revision 0x%x\n",
   2.549 -					cpu_num, uci->rev);
   2.550 -			else
   2.551 -				printk(KERN_INFO "microcode: No new microcode data for CPU%d\n", cpu_num);
   2.552 -		}
   2.553 -		return;
   2.554 -	}
   2.555 -
   2.556 -	/* serialize access to the physical write to MSR 0x79 */
   2.557 -	spin_lock_irqsave(&microcode_update_lock, flags);          
   2.558 +	int error = 0;
   2.559  
   2.560 -	/* write microcode via MSR 0x79 */
   2.561 -	wrmsr(MSR_IA32_UCODE_WRITE,
   2.562 -		(unsigned long) uci->mc->bits, 
   2.563 -		(unsigned long) uci->mc->bits >> 16 >> 16);
   2.564 -	wrmsr(MSR_IA32_UCODE_REV, 0, 0);
   2.565 -
   2.566 -	/* see notes above for revision 1.07.  Apparent chip bug */
   2.567 -	sync_core();
   2.568 -
   2.569 -	/* get the current revision from MSR 0x8B */
   2.570 -	rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
   2.571 +	microcode_error = 0;
   2.572  
   2.573 -	/* notify the caller of success on this cpu */
   2.574 -	uci->err = MC_SUCCESS;
   2.575 -	spin_unlock_irqrestore(&microcode_update_lock, flags);
   2.576 -	printk(KERN_INFO "microcode: CPU%d updated from revision "
   2.577 -	       "0x%x to 0x%x, date = %08x \n", 
   2.578 -	       cpu_num, uci->rev, val[1], uci->mc->hdr.date);
   2.579 -	return;
   2.580 -}
   2.581 -
   2.582 -static int do_microcode_update (void)
   2.583 -{
   2.584 -	int i, error;
   2.585 -
   2.586 -	if (on_each_cpu(collect_cpu_info, NULL, 1, 1) != 0) {
   2.587 +	if (on_each_cpu(do_microcode_update_one, NULL, 1, 1) != 0) {
   2.588  		printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
   2.589  		error = -EIO;
   2.590  		goto out;
   2.591  	}
   2.592  
   2.593 -	if ((error = find_matching_ucodes())) {
   2.594 -		printk(KERN_ERR "microcode: Error in the microcode data\n");
   2.595 -		goto out_free;
   2.596 -	}
   2.597 -
   2.598 -	if (on_each_cpu(do_update_one, NULL, 1, 1) != 0) {
   2.599 -		printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
   2.600 -		error = -EIO;
   2.601 +	if (microcode_error) {
   2.602 +		error = microcode_error;
   2.603 +		goto out;
   2.604  	}
   2.605  
   2.606 -out_free:
   2.607 -	for_each_online_cpu(i) {
   2.608 -		if (ucode_cpu_info[i].mc) {
   2.609 -			int j;
   2.610 -			void *tmp = ucode_cpu_info[i].mc;
   2.611 -			vfree(tmp);
   2.612 -			for_each_online_cpu(j) {
   2.613 -				if (ucode_cpu_info[j].mc == tmp)
   2.614 -					ucode_cpu_info[j].mc = NULL;
   2.615 -			}
   2.616 -		}
   2.617 -		if (ucode_cpu_info[i].err == MC_IGNORED && verbose)
   2.618 -			printk(KERN_WARNING "microcode: CPU%d not 'upgrading' to earlier revision"
   2.619 -			       " 0x%x (current=0x%x)\n", i, ucode_cpu_info[i].cksum, ucode_cpu_info[i].rev);
   2.620 -	}
   2.621  out:
   2.622  	return error;
   2.623  }
   2.624 @@ -458,20 +222,46 @@ out:
   2.625  int microcode_update(XEN_GUEST_HANDLE(const_void) buf, unsigned long len)
   2.626  {
   2.627  	int ret;
   2.628 +	struct cpuinfo_x86 *c = &boot_cpu_data;
   2.629  
   2.630 -	if (len != (typeof(user_buffer_size))len) {
   2.631 +	if (len != (typeof(microcode_buffer.size))len) {
   2.632  		printk(KERN_ERR "microcode: too much data\n");
   2.633  		return -E2BIG;
   2.634  	}
   2.635  
   2.636 -	mutex_lock(&microcode_mutex);
   2.637 +	switch (c->x86_vendor) {
   2.638 +	case X86_VENDOR_AMD:
   2.639 +		ret = microcode_init_amd(c);
   2.640 +		break;
   2.641  
   2.642 -	user_buffer = buf.p;
   2.643 -	user_buffer_size = len;
   2.644 +	case X86_VENDOR_INTEL:
   2.645 +		ret = microcode_init_intel(c);
   2.646 +		break;
   2.647 +	default:
   2.648 +		printk(KERN_ERR "microcode: CPU vendor not supported\n");
   2.649 +		ret = -EINVAL;
   2.650 +		break;
   2.651 +	}
   2.652 +
   2.653 +	if (ret != 0)
   2.654 +		return ret;
   2.655 +
   2.656 +	microcode_buffer.buf = xmalloc_array(uint8_t, len);
   2.657 +	if (!microcode_buffer.buf)
   2.658 +		return -ENOMEM;
   2.659 +
   2.660 +	ret = copy_from_guest(microcode_buffer.buf, buf, len);
   2.661 +	if (ret != 0)
   2.662 +		return ret;
   2.663 +
   2.664 +	microcode_buffer.size = len;
   2.665 +	wmb();
   2.666  
   2.667  	ret = do_microcode_update();
   2.668  
   2.669 -	mutex_unlock(&microcode_mutex);
   2.670 +	xfree(microcode_buffer.buf);
   2.671 +	microcode_buffer.buf = NULL;
   2.672 +	microcode_buffer.size = 0;
   2.673  
   2.674  	return ret;
   2.675  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/xen/arch/x86/microcode_amd.c	Fri Sep 12 14:18:13 2008 +0100
     3.3 @@ -0,0 +1,366 @@
     3.4 +/*
     3.5 + *  AMD CPU Microcode Update Driver for Linux
     3.6 + *  Copyright (C) 2008 Advanced Micro Devices Inc.
     3.7 + *
     3.8 + *  Author: Peter Oruba <peter.oruba@amd.com>
     3.9 + *
    3.10 + *  Based on work by:
    3.11 + *  Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
    3.12 + *
    3.13 + *  This driver allows to upgrade microcode on AMD
    3.14 + *  family 0x10 and 0x11 processors.
    3.15 + *
    3.16 + *  Licensed unter the terms of the GNU General Public
    3.17 + *  License version 2. See file COPYING for details.
    3.18 +*/
    3.19 +
    3.20 +#include <xen/config.h>
    3.21 +#include <xen/lib.h>
    3.22 +#include <xen/kernel.h>
    3.23 +#include <xen/init.h>
    3.24 +#include <xen/sched.h>
    3.25 +#include <xen/smp.h>
    3.26 +#include <xen/spinlock.h>
    3.27 +
    3.28 +#include <asm/msr.h>
    3.29 +#include <asm/uaccess.h>
    3.30 +#include <asm/processor.h>
    3.31 +#include <asm/microcode.h>
    3.32 +
    3.33 +
    3.34 +#define pr_debug(x...) ((void)0)
    3.35 +#define DEFINE_MUTEX(_m) DEFINE_SPINLOCK(_m)
    3.36 +#define mutex_lock(_m) spin_lock(_m)
    3.37 +#define mutex_unlock(_m) spin_unlock(_m)
    3.38 +#define vmalloc(_s) xmalloc_bytes(_s)
    3.39 +#define vfree(_p) xfree(_p)
    3.40 +
    3.41 +
    3.42 +#define UCODE_MAGIC                0x00414d44
    3.43 +#define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
    3.44 +#define UCODE_UCODE_TYPE           0x00000001
    3.45 +
    3.46 +#define UCODE_MAX_SIZE          (2048)
    3.47 +#define DEFAULT_UCODE_DATASIZE	(896)
    3.48 +#define MC_HEADER_SIZE		(sizeof(struct microcode_header_amd))
    3.49 +#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
    3.50 +#define DWSIZE			(sizeof(uint32_t))
    3.51 +/* For now we support a fixed ucode total size only */
    3.52 +#define get_totalsize(mc) \
    3.53 +	((((struct microcode_amd *)mc)->hdr.mc_patch_data_len * 28) \
    3.54 +	 + MC_HEADER_SIZE)
    3.55 +
    3.56 +/* serialize access to the physical write */
    3.57 +static DEFINE_SPINLOCK(microcode_update_lock);
    3.58 +
    3.59 +struct equiv_cpu_entry *equiv_cpu_table;
    3.60 +
    3.61 +static long install_equiv_cpu_table(const void *, uint32_t, long);
    3.62 +
    3.63 +static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
    3.64 +{
    3.65 +	struct cpuinfo_x86 *c = &cpu_data[cpu];
    3.66 +
    3.67 +	memset(csig, 0, sizeof(*csig));
    3.68 +
    3.69 +	if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
    3.70 +		printk(KERN_ERR "microcode: CPU%d not a capable AMD processor\n",
    3.71 +		       cpu);
    3.72 +		return -1;
    3.73 +	}
    3.74 +
    3.75 +	asm volatile("movl %1, %%ecx; rdmsr"
    3.76 +		     : "=a" (csig->rev)
    3.77 +		     : "i" (MSR_AMD_PATCHLEVEL) : "ecx");
    3.78 +
    3.79 +	printk(KERN_INFO "microcode: collect_cpu_info_amd : patch_id=0x%x\n",
    3.80 +		csig->rev);
    3.81 +
    3.82 +	return 0;
    3.83 +}
    3.84 +
    3.85 +static int get_matching_microcode_amd(void *mc, int cpu)
    3.86 +{
    3.87 +	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
    3.88 +	struct microcode_header_amd *mc_header = mc;
    3.89 +	unsigned long total_size = get_totalsize(mc_header);
    3.90 +	void *new_mc;
    3.91 +	unsigned int current_cpu_id;
    3.92 +	unsigned int equiv_cpu_id = 0x00;
    3.93 +	unsigned int i = 0;
    3.94 +
    3.95 +	/* We should bind the task to the CPU */
    3.96 +	BUG_ON(cpu != raw_smp_processor_id());
    3.97 +
    3.98 +	/* This is a tricky part. We might be called from a write operation
    3.99 +	 * to the device file instead of the usual process of firmware
   3.100 +	 * loading. This routine needs to be able to distinguish both
   3.101 +	 * cases. This is done by checking if there already is a equivalent
   3.102 +	 * CPU table installed. If not, we're written through
   3.103 +	 * /dev/cpu/microcode.
   3.104 +	 * Since we ignore all checks. The error case in which going through
   3.105 +	 * firmware loading and that table is not loaded has already been
   3.106 +	 * checked earlier.
   3.107 +	 */
   3.108 +	if (equiv_cpu_table == NULL) {
   3.109 +		printk(KERN_INFO "microcode: CPU%d microcode update with "
   3.110 +		       "version 0x%x (current=0x%x)\n",
   3.111 +		       cpu, mc_header->patch_id, uci->cpu_sig.rev);
   3.112 +		goto out;
   3.113 +	}
   3.114 +
   3.115 +	current_cpu_id = cpuid_eax(0x00000001);
   3.116 +
   3.117 +	while (equiv_cpu_table[i].installed_cpu != 0) {
   3.118 +		if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
   3.119 +			equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
   3.120 +			break;
   3.121 +		}
   3.122 +		i++;
   3.123 +	}
   3.124 +
   3.125 +	if (!equiv_cpu_id) {
   3.126 +		printk(KERN_ERR "microcode: CPU%d cpu_id "
   3.127 +		       "not found in equivalent cpu table \n", cpu);
   3.128 +		return 0;
   3.129 +	}
   3.130 +
   3.131 +	if ((mc_header->processor_rev_id[0]) != (equiv_cpu_id & 0xff)) {
   3.132 +		printk(KERN_INFO
   3.133 +			"microcode: CPU%d patch does not match "
   3.134 +			"(patch is %x, cpu extended is %x) \n",
   3.135 +			cpu, mc_header->processor_rev_id[0],
   3.136 +			(equiv_cpu_id & 0xff));
   3.137 +		return 0;
   3.138 +	}
   3.139 +
   3.140 +	if ((mc_header->processor_rev_id[1]) != ((equiv_cpu_id >> 16) & 0xff)) {
   3.141 +		printk(KERN_INFO "microcode: CPU%d patch does not match "
   3.142 +			"(patch is %x, cpu base id is %x) \n",
   3.143 +			cpu, mc_header->processor_rev_id[1],
   3.144 +			((equiv_cpu_id >> 16) & 0xff));
   3.145 +
   3.146 +		return 0;
   3.147 +	}
   3.148 +
   3.149 +	if (mc_header->patch_id <= uci->cpu_sig.rev)
   3.150 +		return 0;
   3.151 +
   3.152 +	printk(KERN_INFO "microcode: CPU%d found a matching microcode "
   3.153 +	       "update with version 0x%x (current=0x%x)\n",
   3.154 +	       cpu, mc_header->patch_id, uci->cpu_sig.rev);
   3.155 +
   3.156 +out:
   3.157 +	new_mc = vmalloc(UCODE_MAX_SIZE);
   3.158 +	if (!new_mc) {
   3.159 +		printk(KERN_ERR "microcode: error, can't allocate memory\n");
   3.160 +		return -ENOMEM;
   3.161 +	}
   3.162 +	memset(new_mc, 0, UCODE_MAX_SIZE);
   3.163 +
   3.164 +	/* free previous update file */
   3.165 +	vfree(uci->mc.mc_amd);
   3.166 +
   3.167 +	memcpy(new_mc, mc, total_size);
   3.168 +
   3.169 +	uci->mc.mc_amd = new_mc;
   3.170 +	return 1;
   3.171 +}
   3.172 +
   3.173 +static int apply_microcode_amd(int cpu)
   3.174 +{
   3.175 +	unsigned long flags;
   3.176 +	unsigned int eax, edx;
   3.177 +	unsigned int rev;
   3.178 +	int cpu_num = raw_smp_processor_id();
   3.179 +	struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
   3.180 +	unsigned long addr;
   3.181 +
   3.182 +	/* We should bind the task to the CPU */
   3.183 +	BUG_ON(cpu_num != cpu);
   3.184 +
   3.185 +	if (uci->mc.mc_amd == NULL)
   3.186 +		return -EINVAL;
   3.187 +
   3.188 +	spin_lock_irqsave(&microcode_update_lock, flags);
   3.189 +
   3.190 +	addr = (unsigned long)&uci->mc.mc_amd->hdr.data_code;
   3.191 +	edx = (unsigned int)((unsigned long)(addr >> 32));
   3.192 +	eax = (unsigned int)((unsigned long)(addr & 0xffffffff));
   3.193 +
   3.194 +	asm volatile("movl %0, %%ecx; wrmsr" :
   3.195 +		     : "i" (MSR_AMD_PATCHLOADER), "a" (eax), "d" (edx) : "ecx");
   3.196 +
   3.197 +	/* get patch id after patching */
   3.198 +	asm volatile("movl %1, %%ecx; rdmsr"
   3.199 +		     : "=a" (rev)
   3.200 +		     : "i" (MSR_AMD_PATCHLEVEL) : "ecx");
   3.201 +
   3.202 +	spin_unlock_irqrestore(&microcode_update_lock, flags);
   3.203 +
   3.204 +	/* check current patch id and patch's id for match */
   3.205 +	if (rev != uci->mc.mc_amd->hdr.patch_id) {
   3.206 +		printk(KERN_ERR "microcode: CPU%d update from revision "
   3.207 +		       "0x%x to 0x%x failed\n", cpu_num,
   3.208 +		       uci->mc.mc_amd->hdr.patch_id, rev);
   3.209 +		return -EIO;
   3.210 +	}
   3.211 +
   3.212 +	printk("microcode: CPU%d updated from revision "
   3.213 +	       "0x%x to 0x%x \n",
   3.214 +	       cpu_num, uci->cpu_sig.rev, uci->mc.mc_amd->hdr.patch_id);
   3.215 +
   3.216 +	uci->cpu_sig.rev = rev;
   3.217 +
   3.218 +	return 0;
   3.219 +}
   3.220 +
   3.221 +static long get_next_ucode_from_buffer_amd(void **mc, const void *buf,
   3.222 +				       unsigned long size, long offset)
   3.223 +{
   3.224 +	struct microcode_header_amd *mc_header;
   3.225 +	unsigned long total_size;
   3.226 +	const uint8_t *buf_pos = buf;
   3.227 +
   3.228 +	/* No more data */
   3.229 +	if (offset >= size)
   3.230 +		return 0;
   3.231 +
   3.232 +	if (buf_pos[offset] != UCODE_UCODE_TYPE) {
   3.233 +		printk(KERN_ERR "microcode: error! "
   3.234 +		       "Wrong microcode payload type field\n");
   3.235 +		return -EINVAL;
   3.236 +	}
   3.237 +
   3.238 +	mc_header = (struct microcode_header_amd *)(&buf_pos[offset+8]);
   3.239 +
   3.240 +	total_size = (unsigned long) (buf_pos[offset+4] +
   3.241 +				      (buf_pos[offset+5] << 8));
   3.242 +
   3.243 +	printk(KERN_INFO "microcode: size %lu, total_size %lu, offset %ld\n",
   3.244 +		size, total_size, offset);
   3.245 +
   3.246 +	if (offset + total_size > size) {
   3.247 +		printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
   3.248 +		return -EINVAL;
   3.249 +	}
   3.250 +
   3.251 +	*mc = vmalloc(UCODE_MAX_SIZE);
   3.252 +	if (!*mc) {
   3.253 +		printk(KERN_ERR "microcode: error! "
   3.254 +		       "Can not allocate memory for microcode patch\n");
   3.255 +		return -ENOMEM;
   3.256 +	}
   3.257 +
   3.258 +	memset(*mc, 0, UCODE_MAX_SIZE);
   3.259 +	memcpy(*mc, (const void *)(buf + offset + 8), total_size);
   3.260 +
   3.261 +	return offset + total_size + 8;
   3.262 +}
   3.263 +
   3.264 +static long install_equiv_cpu_table(const void *buf,
   3.265 +				uint32_t size, long offset)
   3.266 +{
   3.267 +	const uint32_t *buf_pos = buf;
   3.268 +
   3.269 +	/* No more data */
   3.270 +	if (offset >= size)
   3.271 +		return 0;
   3.272 +
   3.273 +	if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE) {
   3.274 +		printk(KERN_ERR "microcode: error! "
   3.275 +		       "Wrong microcode equivalnet cpu table type field\n");
   3.276 +		return 0;
   3.277 +	}
   3.278 +
   3.279 +	if (size == 0) {
   3.280 +		printk(KERN_ERR "microcode: error! "
   3.281 +		       "Wrong microcode equivalnet cpu table length\n");
   3.282 +		return 0;
   3.283 +	}
   3.284 +
   3.285 +	equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
   3.286 +	if (!equiv_cpu_table) {
   3.287 +		printk(KERN_ERR "microcode: error, can't allocate memory for equiv CPU table\n");
   3.288 +		return 0;
   3.289 +	}
   3.290 +
   3.291 +	memset(equiv_cpu_table, 0, size);
   3.292 +	memcpy(equiv_cpu_table, (const void *)&buf_pos[3], size);
   3.293 +
   3.294 +	return size + 12; /* add header length */
   3.295 +}
   3.296 +
   3.297 +static int cpu_request_microcode_amd(int cpu, const void *buf,
   3.298 +				size_t size)
   3.299 +{
   3.300 +	const uint32_t *buf_pos;
   3.301 +	long offset = 0;
   3.302 +	int error = 0;
   3.303 +	void *mc;
   3.304 +
   3.305 +	/* We should bind the task to the CPU */
   3.306 +	BUG_ON(cpu != raw_smp_processor_id());
   3.307 +
   3.308 +	buf_pos = (const uint32_t *)buf;
   3.309 +
   3.310 +	if (buf_pos[0] != UCODE_MAGIC) {
   3.311 +		printk(KERN_ERR "microcode: error! Wrong microcode patch file magic\n");
   3.312 +		return -EINVAL;
   3.313 +	}
   3.314 +
   3.315 +	offset = install_equiv_cpu_table(buf, (uint32_t)(buf_pos[2]), offset);
   3.316 +	if (!offset) {
   3.317 +		printk(KERN_ERR "microcode: installing equivalent cpu table failed\n");
   3.318 +		return -EINVAL;
   3.319 +	}
   3.320 +
   3.321 +	while ((offset =
   3.322 +		get_next_ucode_from_buffer_amd(&mc, buf, size, offset)) > 0) {
   3.323 +		error = get_matching_microcode_amd(mc, cpu);
   3.324 +		if (error < 0)
   3.325 +			break;
   3.326 +		/*
   3.327 +		 * It's possible the data file has multiple matching ucode,
   3.328 +		 * lets keep searching till the latest version
   3.329 +		 */
   3.330 +		if (error == 1) {
   3.331 +			apply_microcode_amd(cpu);
   3.332 +			error = 0;
   3.333 +		}
   3.334 +		vfree(mc);
   3.335 +	}
   3.336 +	if (offset > 0) {
   3.337 +		vfree(mc);
   3.338 +		vfree(equiv_cpu_table);
   3.339 +		equiv_cpu_table = NULL;
   3.340 +	}
   3.341 +	if (offset < 0)
   3.342 +		error = offset;
   3.343 +
   3.344 +	return error;
   3.345 +}
   3.346 +
   3.347 +static void microcode_fini_cpu_amd(int cpu)
   3.348 +{
   3.349 +	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
   3.350 +
   3.351 +	vfree(uci->mc.mc_amd);
   3.352 +	uci->mc.mc_amd = NULL;
   3.353 +}
   3.354 +
   3.355 +static struct microcode_ops microcode_amd_ops = {
   3.356 +	.get_matching_microcode           = get_matching_microcode_amd,
   3.357 +	.microcode_sanity_check           = NULL,
   3.358 +	.cpu_request_microcode            = cpu_request_microcode_amd,
   3.359 +	.collect_cpu_info                 = collect_cpu_info_amd,
   3.360 +	.apply_microcode                  = apply_microcode_amd,
   3.361 +	.microcode_fini_cpu               = microcode_fini_cpu_amd,
   3.362 +};
   3.363 +
   3.364 +int microcode_init_amd(struct cpuinfo_x86 *c)
   3.365 +{
   3.366 +	microcode_ops = &microcode_amd_ops;
   3.367 +	return 0;
   3.368 +}
   3.369 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/xen/arch/x86/microcode_intel.c	Fri Sep 12 14:18:13 2008 +0100
     4.3 @@ -0,0 +1,425 @@
     4.4 +/*
     4.5 + *	Intel CPU Microcode Update Driver for Linux
     4.6 + *
     4.7 + *	Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
     4.8 + *		      2006	Shaohua Li <shaohua.li@intel.com> *
     4.9 + *	This driver allows to upgrade microcode on Intel processors
    4.10 + *	belonging to IA-32 family - PentiumPro, Pentium II,
    4.11 + *	Pentium III, Xeon, Pentium 4, etc.
    4.12 + *
    4.13 + *	Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
    4.14 + *	Software Developer's Manual
    4.15 + *	Order Number 253668 or free download from:
    4.16 + *
    4.17 + *	http://developer.intel.com/design/pentium4/manuals/253668.htm
    4.18 + *
    4.19 + *	For more information, go to http://www.urbanmyth.org/microcode
    4.20 + *
    4.21 + *	This program is free software; you can redistribute it and/or
    4.22 + *	modify it under the terms of the GNU General Public License
    4.23 + *	as published by the Free Software Foundation; either version
    4.24 + *	2 of the License, or (at your option) any later version.
    4.25 + *
    4.26 + *	1.0	16 Feb 2000, Tigran Aivazian <tigran@sco.com>
    4.27 + *		Initial release.
    4.28 + *	1.01	18 Feb 2000, Tigran Aivazian <tigran@sco.com>
    4.29 + *		Added read() support + cleanups.
    4.30 + *	1.02	21 Feb 2000, Tigran Aivazian <tigran@sco.com>
    4.31 + *		Added 'device trimming' support. open(O_WRONLY) zeroes
    4.32 + *		and frees the saved copy of applied microcode.
    4.33 + *	1.03	29 Feb 2000, Tigran Aivazian <tigran@sco.com>
    4.34 + *		Made to use devfs (/dev/cpu/microcode) + cleanups.
    4.35 + *	1.04	06 Jun 2000, Simon Trimmer <simon@veritas.com>
    4.36 + *		Added misc device support (now uses both devfs and misc).
    4.37 + *		Added MICROCODE_IOCFREE ioctl to clear memory.
    4.38 + *	1.05	09 Jun 2000, Simon Trimmer <simon@veritas.com>
    4.39 + *		Messages for error cases (non Intel & no suitable microcode).
    4.40 + *	1.06	03 Aug 2000, Tigran Aivazian <tigran@veritas.com>
    4.41 + *		Removed ->release(). Removed exclusive open and status bitmap.
    4.42 + *		Added microcode_rwsem to serialize read()/write()/ioctl().
    4.43 + *		Removed global kernel lock usage.
    4.44 + *	1.07	07 Sep 2000, Tigran Aivazian <tigran@veritas.com>
    4.45 + *		Write 0 to 0x8B msr and then cpuid before reading revision,
    4.46 + *		so that it works even if there were no update done by the
    4.47 + *		BIOS. Otherwise, reading from 0x8B gives junk (which happened
    4.48 + *		to be 0 on my machine which is why it worked even when I
    4.49 + *		disabled update by the BIOS)
    4.50 + *		Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix.
    4.51 + *	1.08	11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and
    4.52 + *			     Tigran Aivazian <tigran@veritas.com>
    4.53 + *		Intel Pentium 4 processor support and bugfixes.
    4.54 + *	1.09	30 Oct 2001, Tigran Aivazian <tigran@veritas.com>
    4.55 + *		Bugfix for HT (Hyper-Threading) enabled processors
    4.56 + *		whereby processor resources are shared by all logical processors
    4.57 + *		in a single CPU package.
    4.58 + *	1.10	28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and
    4.59 + *		Tigran Aivazian <tigran@veritas.com>,
    4.60 + *		Serialize updates as required on HT processors due to
    4.61 + *		speculative nature of implementation.
    4.62 + *	1.11	22 Mar 2002 Tigran Aivazian <tigran@veritas.com>
    4.63 + *		Fix the panic when writing zero-length microcode chunk.
    4.64 + *	1.12	29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>,
    4.65 + *		Jun Nakajima <jun.nakajima@intel.com>
    4.66 + *		Support for the microcode updates in the new format.
    4.67 + *	1.13	10 Oct 2003 Tigran Aivazian <tigran@veritas.com>
    4.68 + *		Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl
    4.69 + *		because we no longer hold a copy of applied microcode
    4.70 + *		in kernel memory.
    4.71 + *	1.14	25 Jun 2004 Tigran Aivazian <tigran@veritas.com>
    4.72 + *		Fix sigmatch() macro to handle old CPUs with pf == 0.
    4.73 + *		Thanks to Stuart Swales for pointing out this bug.
    4.74 + */
    4.75 +
    4.76 +#include <xen/config.h>
    4.77 +#include <xen/lib.h>
    4.78 +#include <xen/kernel.h>
    4.79 +#include <xen/init.h>
    4.80 +#include <xen/sched.h>
    4.81 +#include <xen/smp.h>
    4.82 +#include <xen/spinlock.h>
    4.83 +
    4.84 +#include <asm/msr.h>
    4.85 +#include <asm/uaccess.h>
    4.86 +#include <asm/processor.h>
    4.87 +#include <asm/microcode.h>
    4.88 +
    4.89 +#define pr_debug(x...) ((void)0)
    4.90 +#define DEFINE_MUTEX(_m) DEFINE_SPINLOCK(_m)
    4.91 +#define mutex_lock(_m) spin_lock(_m)
    4.92 +#define mutex_unlock(_m) spin_unlock(_m)
    4.93 +#define vmalloc(_s) xmalloc_bytes(_s)
    4.94 +#define vfree(_p) xfree(_p)
    4.95 +
    4.96 +#if 0
    4.97 +MODULE_DESCRIPTION("Microcode Update Driver");
    4.98 +MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
    4.99 +MODULE_LICENSE("GPL");
   4.100 +#endif
   4.101 +
   4.102 +#define DEFAULT_UCODE_DATASIZE 	(2000)
   4.103 +#define MC_HEADER_SIZE		(sizeof(struct microcode_header_intel))
   4.104 +#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
   4.105 +#define EXT_HEADER_SIZE		(sizeof(struct extended_sigtable))
   4.106 +#define EXT_SIGNATURE_SIZE	(sizeof(struct extended_signature))
   4.107 +#define DWSIZE			(sizeof(u32))
   4.108 +#define get_totalsize(mc) \
   4.109 +	(((struct microcode_intel *)mc)->hdr.totalsize ? \
   4.110 +	 ((struct microcode_intel *)mc)->hdr.totalsize : \
   4.111 +	 DEFAULT_UCODE_TOTALSIZE)
   4.112 +
   4.113 +#define get_datasize(mc) \
   4.114 +	(((struct microcode_intel *)mc)->hdr.datasize ? \
   4.115 +	 ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
   4.116 +
   4.117 +#define sigmatch(s1, s2, p1, p2) \
   4.118 +	(((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
   4.119 +
   4.120 +#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
   4.121 +
   4.122 +/* serialize access to the physical write to MSR 0x79 */
   4.123 +static DEFINE_SPINLOCK(microcode_update_lock);
   4.124 +
   4.125 +static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
   4.126 +{
   4.127 +	struct cpuinfo_x86 *c = &cpu_data[cpu_num];
   4.128 +	unsigned int val[2];
   4.129 +
   4.130 +	memset(csig, 0, sizeof(*csig));
   4.131 +
   4.132 +	if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
   4.133 +	    cpu_has(c, X86_FEATURE_IA64)) {
   4.134 +		printk(KERN_ERR "microcode: CPU%d not a capable Intel "
   4.135 +			"processor\n", cpu_num);
   4.136 +		return -1;
   4.137 +	}
   4.138 +
   4.139 +	csig->sig = cpuid_eax(0x00000001);
   4.140 +
   4.141 +	if ((c->x86_model >= 5) || (c->x86 > 6)) {
   4.142 +		/* get processor flags from MSR 0x17 */
   4.143 +		rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
   4.144 +		csig->pf = 1 << ((val[1] >> 18) & 7);
   4.145 +	}
   4.146 +
   4.147 +	wrmsr(MSR_IA32_UCODE_REV, 0, 0);
   4.148 +	/* see notes above for revision 1.07.  Apparent chip bug */
   4.149 +	sync_core();
   4.150 +	/* get the current revision from MSR 0x8B */
   4.151 +	rdmsr(MSR_IA32_UCODE_REV, val[0], csig->rev);
   4.152 +	pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
   4.153 +			csig->sig, csig->pf, csig->rev);
   4.154 +
   4.155 +	return 0;
   4.156 +}
   4.157 +
   4.158 +static inline int microcode_update_match(int cpu_num,
   4.159 +	struct microcode_header_intel *mc_header, int sig, int pf)
   4.160 +{
   4.161 +	struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
   4.162 +
   4.163 +	if (!sigmatch(sig, uci->cpu_sig.sig, pf, uci->cpu_sig.pf)
   4.164 +		|| mc_header->rev <= uci->cpu_sig.rev)
   4.165 +		return 0;
   4.166 +	return 1;
   4.167 +}
   4.168 +
   4.169 +static int microcode_sanity_check(void *mc)
   4.170 +{
   4.171 +	struct microcode_header_intel *mc_header = mc;
   4.172 +	struct extended_sigtable *ext_header = NULL;
   4.173 +	struct extended_signature *ext_sig;
   4.174 +	unsigned long total_size, data_size, ext_table_size;
   4.175 +	int sum, orig_sum, ext_sigcount = 0, i;
   4.176 +
   4.177 +	total_size = get_totalsize(mc_header);
   4.178 +	data_size = get_datasize(mc_header);
   4.179 +	if (data_size + MC_HEADER_SIZE > total_size) {
   4.180 +		printk(KERN_ERR "microcode: error! "
   4.181 +			"Bad data size in microcode data file\n");
   4.182 +		return -EINVAL;
   4.183 +	}
   4.184 +
   4.185 +	if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
   4.186 +		printk(KERN_ERR "microcode: error! "
   4.187 +			"Unknown microcode update format\n");
   4.188 +		return -EINVAL;
   4.189 +	}
   4.190 +	ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
   4.191 +	if (ext_table_size) {
   4.192 +		if ((ext_table_size < EXT_HEADER_SIZE)
   4.193 +		 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
   4.194 +			printk(KERN_ERR "microcode: error! "
   4.195 +				"Small exttable size in microcode data file\n");
   4.196 +			return -EINVAL;
   4.197 +		}
   4.198 +		ext_header = mc + MC_HEADER_SIZE + data_size;
   4.199 +		if (ext_table_size != exttable_size(ext_header)) {
   4.200 +			printk(KERN_ERR "microcode: error! "
   4.201 +				"Bad exttable size in microcode data file\n");
   4.202 +			return -EFAULT;
   4.203 +		}
   4.204 +		ext_sigcount = ext_header->count;
   4.205 +	}
   4.206 +
   4.207 +	/* check extended table checksum */
   4.208 +	if (ext_table_size) {
   4.209 +		int ext_table_sum = 0;
   4.210 +		int *ext_tablep = (int *)ext_header;
   4.211 +
   4.212 +		i = ext_table_size / DWSIZE;
   4.213 +		while (i--)
   4.214 +			ext_table_sum += ext_tablep[i];
   4.215 +		if (ext_table_sum) {
   4.216 +			printk(KERN_WARNING "microcode: aborting, "
   4.217 +				"bad extended signature table checksum\n");
   4.218 +			return -EINVAL;
   4.219 +		}
   4.220 +	}
   4.221 +
   4.222 +	/* calculate the checksum */
   4.223 +	orig_sum = 0;
   4.224 +	i = (MC_HEADER_SIZE + data_size) / DWSIZE;
   4.225 +	while (i--)
   4.226 +		orig_sum += ((int *)mc)[i];
   4.227 +	if (orig_sum) {
   4.228 +		printk(KERN_ERR "microcode: aborting, bad checksum\n");
   4.229 +		return -EINVAL;
   4.230 +	}
   4.231 +	if (!ext_table_size)
   4.232 +		return 0;
   4.233 +	/* check extended signature checksum */
   4.234 +	for (i = 0; i < ext_sigcount; i++) {
   4.235 +		ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
   4.236 +			  EXT_SIGNATURE_SIZE * i;
   4.237 +		sum = orig_sum
   4.238 +			- (mc_header->sig + mc_header->pf + mc_header->cksum)
   4.239 +			+ (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
   4.240 +		if (sum) {
   4.241 +			printk(KERN_ERR "microcode: aborting, bad checksum\n");
   4.242 +			return -EINVAL;
   4.243 +		}
   4.244 +	}
   4.245 +	return 0;
   4.246 +}
   4.247 +
   4.248 +/*
   4.249 + * return 0 - no update found
   4.250 + * return 1 - found update
   4.251 + * return < 0 - error
   4.252 + */
   4.253 +static int get_matching_microcode(void *mc, int cpu)
   4.254 +{
   4.255 +	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
   4.256 +	struct microcode_header_intel *mc_header = mc;
   4.257 +	struct extended_sigtable *ext_header;
   4.258 +	unsigned long total_size = get_totalsize(mc_header);
   4.259 +	int ext_sigcount, i;
   4.260 +	struct extended_signature *ext_sig;
   4.261 +	void *new_mc;
   4.262 +
   4.263 +	if (microcode_update_match(cpu, mc_header,
   4.264 +			mc_header->sig, mc_header->pf))
   4.265 +		goto find;
   4.266 +
   4.267 +	if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
   4.268 +		return 0;
   4.269 +
   4.270 +	ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
   4.271 +	ext_sigcount = ext_header->count;
   4.272 +	ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
   4.273 +	for (i = 0; i < ext_sigcount; i++) {
   4.274 +		if (microcode_update_match(cpu, mc_header,
   4.275 +				ext_sig->sig, ext_sig->pf))
   4.276 +			goto find;
   4.277 +		ext_sig++;
   4.278 +	}
   4.279 +	return 0;
   4.280 +find:
   4.281 +	pr_debug("microcode: CPU%d found a matching microcode update with"
   4.282 +		 " version 0x%x (current=0x%x)\n",
   4.283 +		 cpu, mc_header->rev, uci->cpu_sig.rev);
   4.284 +	new_mc = vmalloc(total_size);
   4.285 +	if (!new_mc) {
   4.286 +		printk(KERN_ERR "microcode: error! Can not allocate memory\n");
   4.287 +		return -ENOMEM;
   4.288 +	}
   4.289 +
   4.290 +	/* free previous update file */
   4.291 +	vfree(uci->mc.mc_intel);
   4.292 +
   4.293 +	memcpy(new_mc, mc, total_size);
   4.294 +	uci->mc.mc_intel = new_mc;
   4.295 +	return 1;
   4.296 +}
   4.297 +
   4.298 +static int apply_microcode(int cpu)
   4.299 +{
   4.300 +	unsigned long flags;
   4.301 +	unsigned int val[2];
   4.302 +	int cpu_num = raw_smp_processor_id();
   4.303 +	struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
   4.304 +
   4.305 +	/* We should bind the task to the CPU */
   4.306 +	BUG_ON(cpu_num != cpu);
   4.307 +
   4.308 +	if (uci->mc.mc_intel == NULL)
   4.309 +		return -EINVAL;
   4.310 +
   4.311 +	/* serialize access to the physical write to MSR 0x79 */
   4.312 +	spin_lock_irqsave(&microcode_update_lock, flags);
   4.313 +
   4.314 +	/* write microcode via MSR 0x79 */
   4.315 +	wrmsr(MSR_IA32_UCODE_WRITE,
   4.316 +	      (unsigned long) uci->mc.mc_intel->bits,
   4.317 +	      (unsigned long) uci->mc.mc_intel->bits >> 16 >> 16);
   4.318 +	wrmsr(MSR_IA32_UCODE_REV, 0, 0);
   4.319 +
   4.320 +	/* see notes above for revision 1.07.  Apparent chip bug */
   4.321 +	sync_core();
   4.322 +
   4.323 +	/* get the current revision from MSR 0x8B */
   4.324 +	rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
   4.325 +
   4.326 +	spin_unlock_irqrestore(&microcode_update_lock, flags);
   4.327 +	if (val[1] != uci->mc.mc_intel->hdr.rev) {
   4.328 +		printk(KERN_ERR "microcode: CPU%d update from revision "
   4.329 +			"0x%x to 0x%x failed\n", cpu_num, uci->cpu_sig.rev, val[1]);
   4.330 +		return -EIO;
   4.331 +	}
   4.332 +	printk(KERN_INFO "microcode: CPU%d updated from revision "
   4.333 +	       "0x%x to 0x%x, date = %04x-%02x-%02x \n",
   4.334 +		cpu_num, uci->cpu_sig.rev, val[1],
   4.335 +		uci->mc.mc_intel->hdr.date & 0xffff,
   4.336 +		uci->mc.mc_intel->hdr.date >> 24,
   4.337 +		(uci->mc.mc_intel->hdr.date >> 16) & 0xff);
   4.338 +	uci->cpu_sig.rev = val[1];
   4.339 +
   4.340 +	return 0;
   4.341 +}
   4.342 +
   4.343 +static long get_next_ucode_from_buffer(void **mc, const u8 *buf,
   4.344 +	unsigned long size, long offset)
   4.345 +{
   4.346 +	struct microcode_header_intel *mc_header;
   4.347 +	unsigned long total_size;
   4.348 +
   4.349 +	/* No more data */
   4.350 +	if (offset >= size)
   4.351 +		return 0;
   4.352 +	mc_header = (struct microcode_header_intel *)(buf + offset);
   4.353 +	total_size = get_totalsize(mc_header);
   4.354 +
   4.355 +	if (offset + total_size > size) {
   4.356 +		printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
   4.357 +		return -EINVAL;
   4.358 +	}
   4.359 +
   4.360 +	*mc = vmalloc(total_size);
   4.361 +	if (!*mc) {
   4.362 +		printk(KERN_ERR "microcode: error! Can not allocate memory\n");
   4.363 +		return -ENOMEM;
   4.364 +	}
   4.365 +	memcpy(*mc, (const void *)(buf + offset), total_size);
   4.366 +	return offset + total_size;
   4.367 +}
   4.368 +
   4.369 +/* fake device for request_firmware */
   4.370 +extern struct platform_device *microcode_pdev;
   4.371 +
   4.372 +static int cpu_request_microcode(int cpu, const void *buf, size_t size)
   4.373 +{
   4.374 +	long offset = 0;
   4.375 +	int error = 0;
   4.376 +	void *mc;
   4.377 +
   4.378 +	/* We should bind the task to the CPU */
   4.379 +	BUG_ON(cpu != raw_smp_processor_id());
   4.380 +
   4.381 +	while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
   4.382 +			> 0) {
   4.383 +		error = microcode_sanity_check(mc);
   4.384 +		if (error)
   4.385 +			break;
   4.386 +		error = get_matching_microcode(mc, cpu);
   4.387 +		if (error < 0)
   4.388 +			break;
   4.389 +		/*
   4.390 +		 * It's possible the data file has multiple matching ucode,
   4.391 +		 * lets keep searching till the latest version
   4.392 +		 */
   4.393 +		if (error == 1) {
   4.394 +			apply_microcode(cpu);
   4.395 +			error = 0;
   4.396 +		}
   4.397 +		vfree(mc);
   4.398 +	}
   4.399 +	if (offset > 0)
   4.400 +		vfree(mc);
   4.401 +	if (offset < 0)
   4.402 +		error = offset;
   4.403 +
   4.404 +	return error;
   4.405 +}
   4.406 +
   4.407 +static void microcode_fini_cpu(int cpu)
   4.408 +{
   4.409 +	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
   4.410 +
   4.411 +	vfree(uci->mc.mc_intel);
   4.412 +	uci->mc.mc_intel = NULL;
   4.413 +}
   4.414 +
   4.415 +static struct microcode_ops microcode_intel_ops = {
   4.416 +	.get_matching_microcode           = get_matching_microcode,
   4.417 +	.microcode_sanity_check           = microcode_sanity_check,
   4.418 +	.cpu_request_microcode            = cpu_request_microcode,
   4.419 +	.collect_cpu_info                 = collect_cpu_info,
   4.420 +	.apply_microcode                  = apply_microcode,
   4.421 +	.microcode_fini_cpu               = microcode_fini_cpu,
   4.422 +};
   4.423 +
   4.424 +int microcode_init_intel(struct cpuinfo_x86 *c)
   4.425 +{
   4.426 +	microcode_ops = &microcode_intel_ops;
   4.427 +	return 0;
   4.428 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/xen/include/asm-x86/microcode.h	Fri Sep 12 14:18:13 2008 +0100
     5.3 @@ -0,0 +1,100 @@
     5.4 +#ifndef ASM_X86__MICROCODE_H
     5.5 +#define ASM_X86__MICROCODE_H
     5.6 +
     5.7 +struct cpu_signature;
     5.8 +
     5.9 +struct microcode_ops {
    5.10 +	long (*microcode_get_next_ucode)(void **mc, long offset);
    5.11 +	int (*get_matching_microcode)(void *mc, int cpu);
    5.12 +	int (*microcode_sanity_check)(void *mc);
    5.13 +	int (*cpu_request_microcode)(int cpu, const void *buf, size_t size);
    5.14 +	int (*collect_cpu_info)(int cpu_num, struct cpu_signature *csig);
    5.15 +	int (*apply_microcode)(int cpu);
    5.16 +	void (*microcode_fini_cpu)(int cpu);
    5.17 +	void (*clear_patch)(void *data);
    5.18 +};
    5.19 +
    5.20 +struct microcode_header_intel {
    5.21 +	unsigned int            hdrver;
    5.22 +	unsigned int            rev;
    5.23 +	unsigned int            date;
    5.24 +	unsigned int            sig;
    5.25 +	unsigned int            cksum;
    5.26 +	unsigned int            ldrver;
    5.27 +	unsigned int            pf;
    5.28 +	unsigned int            datasize;
    5.29 +	unsigned int            totalsize;
    5.30 +	unsigned int            reserved[3];
    5.31 +};
    5.32 +
    5.33 +struct microcode_intel {
    5.34 +	struct microcode_header_intel hdr;
    5.35 +	unsigned int            bits[0];
    5.36 +};
    5.37 +
    5.38 +/* microcode format is extended from prescott processors */
    5.39 +struct extended_signature {
    5.40 +	unsigned int            sig;
    5.41 +	unsigned int            pf;
    5.42 +	unsigned int            cksum;
    5.43 +};
    5.44 +
    5.45 +struct extended_sigtable {
    5.46 +	unsigned int            count;
    5.47 +	unsigned int            cksum;
    5.48 +	unsigned int            reserved[3];
    5.49 +	struct extended_signature sigs[0];
    5.50 +};
    5.51 +
    5.52 +struct equiv_cpu_entry {
    5.53 +	unsigned int installed_cpu;
    5.54 +	unsigned int fixed_errata_mask;
    5.55 +	unsigned int fixed_errata_compare;
    5.56 +	unsigned int equiv_cpu;
    5.57 +};
    5.58 +
    5.59 +struct microcode_header_amd {
    5.60 +	unsigned int  data_code;
    5.61 +	unsigned int  patch_id;
    5.62 +	unsigned char mc_patch_data_id[2];
    5.63 +	unsigned char mc_patch_data_len;
    5.64 +	unsigned char init_flag;
    5.65 +	unsigned int  mc_patch_data_checksum;
    5.66 +	unsigned int  nb_dev_id;
    5.67 +	unsigned int  sb_dev_id;
    5.68 +	unsigned char processor_rev_id[2];
    5.69 +	unsigned char nb_rev_id;
    5.70 +	unsigned char sb_rev_id;
    5.71 +	unsigned char bios_api_rev;
    5.72 +	unsigned char reserved1[3];
    5.73 +	unsigned int  match_reg[8];
    5.74 +};
    5.75 +
    5.76 +struct microcode_amd {
    5.77 +	struct microcode_header_amd hdr;
    5.78 +	unsigned int mpb[0];
    5.79 +};
    5.80 +
    5.81 +struct cpu_signature {
    5.82 +	unsigned int sig;
    5.83 +	unsigned int pf;
    5.84 +	unsigned int rev;
    5.85 +};
    5.86 +
    5.87 +struct ucode_cpu_info {
    5.88 +	struct cpu_signature cpu_sig;
    5.89 +	int valid;
    5.90 +	union {
    5.91 +		struct microcode_intel *mc_intel;
    5.92 +		struct microcode_amd *mc_amd;
    5.93 +		void *valid_mc;
    5.94 +	} mc;
    5.95 +};
    5.96 +extern struct ucode_cpu_info ucode_cpu_info[];
    5.97 +
    5.98 +extern const struct microcode_ops *microcode_ops;
    5.99 +
   5.100 +int microcode_init_amd(struct cpuinfo_x86 *c);
   5.101 +int microcode_init_intel(struct cpuinfo_x86 *c);
   5.102 +
   5.103 +#endif /* ASM_X86__MICROCODE_H */
     6.1 --- a/xen/include/asm-x86/msr-index.h	Fri Sep 12 13:15:36 2008 +0100
     6.2 +++ b/xen/include/asm-x86/msr-index.h	Fri Sep 12 14:18:13 2008 +0100
     6.3 @@ -211,6 +211,10 @@
     6.4  #define FAM10H_MMIO_CONF_BASE_MASK	0xfffffff
     6.5  #define FAM10H_MMIO_CONF_BASE_SHIFT	20
     6.6  
     6.7 +/* AMD Microcode MSRs */
     6.8 +#define MSR_AMD_PATCHLEVEL		0x0000008b
     6.9 +#define MSR_AMD_PATCHLOADER		0xc0010020
    6.10 +
    6.11  /* K6 MSRs */
    6.12  #define MSR_K6_EFER			0xc0000080
    6.13  #define MSR_K6_STAR			0xc0000081
     7.1 --- a/xen/include/asm-x86/processor.h	Fri Sep 12 13:15:36 2008 +0100
     7.2 +++ b/xen/include/asm-x86/processor.h	Fri Sep 12 14:18:13 2008 +0100
     7.3 @@ -486,41 +486,6 @@ long set_gdt(struct vcpu *d,
     7.4  })
     7.5  long set_debugreg(struct vcpu *p, int reg, unsigned long value);
     7.6  
     7.7 -struct microcode_header {
     7.8 -    unsigned int hdrver;
     7.9 -    unsigned int rev;
    7.10 -    unsigned int date;
    7.11 -    unsigned int sig;
    7.12 -    unsigned int cksum;
    7.13 -    unsigned int ldrver;
    7.14 -    unsigned int pf;
    7.15 -    unsigned int datasize;
    7.16 -    unsigned int totalsize;
    7.17 -    unsigned int reserved[3];
    7.18 -};
    7.19 -
    7.20 -struct microcode {
    7.21 -    struct microcode_header hdr;
    7.22 -    unsigned int bits[0];
    7.23 -};
    7.24 -
    7.25 -typedef struct microcode microcode_t;
    7.26 -typedef struct microcode_header microcode_header_t;
    7.27 -
    7.28 -/* microcode format is extended from prescott processors */
    7.29 -struct extended_signature {
    7.30 -    unsigned int sig;
    7.31 -    unsigned int pf;
    7.32 -    unsigned int cksum;
    7.33 -};
    7.34 -
    7.35 -struct extended_sigtable {
    7.36 -    unsigned int count;
    7.37 -    unsigned int cksum;
    7.38 -    unsigned int reserved[3];
    7.39 -    struct extended_signature sigs[0];
    7.40 -};
    7.41 -
    7.42  /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
    7.43  static always_inline void rep_nop(void)
    7.44  {