debuggers.hg
annotate xen/arch/x86/microcode.c @ 3658:0ef6e8e6e85d
bitkeeper revision 1.1159.212.71 (4200f0afX_JumfbEHQex6TdFENULMQ)
Merge labyrinth.cl.cam.ac.uk:/auto/groups/xeno-xenod/BK/xen-unstable.bk
into labyrinth.cl.cam.ac.uk:/auto/groups/xeno/users/iap10/xeno-clone/xen-unstable.bk
Merge labyrinth.cl.cam.ac.uk:/auto/groups/xeno-xenod/BK/xen-unstable.bk
into labyrinth.cl.cam.ac.uk:/auto/groups/xeno/users/iap10/xeno-clone/xen-unstable.bk
author | iap10@labyrinth.cl.cam.ac.uk |
---|---|
date | Wed Feb 02 15:24:31 2005 +0000 (2005-02-02) |
parents | 524b831d8294 10a0f6b0a996 |
children | 8472fafee3cf |
rev | line source |
---|---|
kaf24@3400 | 1 /* |
kaf24@3400 | 2 * Intel CPU Microcode Update Driver for Linux |
kaf24@3400 | 3 * |
kaf24@3400 | 4 * Copyright (C) 2000-2004 Tigran Aivazian |
kaf24@3400 | 5 * |
kaf24@3400 | 6 * This driver allows to upgrade microcode on Intel processors |
kaf24@3400 | 7 * belonging to IA-32 family - PentiumPro, Pentium II, |
kaf24@3400 | 8 * Pentium III, Xeon, Pentium 4, etc. |
kaf24@3400 | 9 * |
kaf24@3400 | 10 * Reference: Section 8.10 of Volume III, Intel Pentium 4 Manual, |
kaf24@3400 | 11 * Order Number 245472 or free download from: |
kaf24@3400 | 12 * |
kaf24@3400 | 13 * http://developer.intel.com/design/pentium4/manuals/245472.htm |
kaf24@3400 | 14 * |
kaf24@3400 | 15 * For more information, go to http://www.urbanmyth.org/microcode |
kaf24@3400 | 16 * |
kaf24@3400 | 17 * This program is free software; you can redistribute it and/or |
kaf24@3400 | 18 * modify it under the terms of the GNU General Public License |
kaf24@3400 | 19 * as published by the Free Software Foundation; either version |
kaf24@3400 | 20 * 2 of the License, or (at your option) any later version. |
kaf24@3400 | 21 * |
kaf24@3400 | 22 * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com> |
kaf24@3400 | 23 * Initial release. |
kaf24@3400 | 24 * 1.01 18 Feb 2000, Tigran Aivazian <tigran@sco.com> |
kaf24@3400 | 25 * Added read() support + cleanups. |
kaf24@3400 | 26 * 1.02 21 Feb 2000, Tigran Aivazian <tigran@sco.com> |
kaf24@3400 | 27 * Added 'device trimming' support. open(O_WRONLY) zeroes |
kaf24@3400 | 28 * and frees the saved copy of applied microcode. |
kaf24@3400 | 29 * 1.03 29 Feb 2000, Tigran Aivazian <tigran@sco.com> |
kaf24@3400 | 30 * Made to use devfs (/dev/cpu/microcode) + cleanups. |
kaf24@3400 | 31 * 1.04 06 Jun 2000, Simon Trimmer <simon@veritas.com> |
kaf24@3400 | 32 * Added misc device support (now uses both devfs and misc). |
kaf24@3400 | 33 * Added MICROCODE_IOCFREE ioctl to clear memory. |
kaf24@3400 | 34 * 1.05 09 Jun 2000, Simon Trimmer <simon@veritas.com> |
kaf24@3400 | 35 * Messages for error cases (non Intel & no suitable microcode). |
kaf24@3400 | 36 * 1.06 03 Aug 2000, Tigran Aivazian <tigran@veritas.com> |
kaf24@3400 | 37 * Removed ->release(). Removed exclusive open and status bitmap. |
kaf24@3400 | 38 * Added microcode_rwsem to serialize read()/write()/ioctl(). |
kaf24@3400 | 39 * Removed global kernel lock usage. |
kaf24@3400 | 40 * 1.07 07 Sep 2000, Tigran Aivazian <tigran@veritas.com> |
kaf24@3400 | 41 * Write 0 to 0x8B msr and then cpuid before reading revision, |
kaf24@3400 | 42 * so that it works even if there were no update done by the |
kaf24@3400 | 43 * BIOS. Otherwise, reading from 0x8B gives junk (which happened |
kaf24@3400 | 44 * to be 0 on my machine which is why it worked even when I |
kaf24@3400 | 45 * disabled update by the BIOS) |
kaf24@3400 | 46 * Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix. |
kaf24@3400 | 47 * 1.08 11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and |
kaf24@3400 | 48 * Tigran Aivazian <tigran@veritas.com> |
kaf24@3400 | 49 * Intel Pentium 4 processor support and bugfixes. |
kaf24@3400 | 50 * 1.09 30 Oct 2001, Tigran Aivazian <tigran@veritas.com> |
kaf24@3400 | 51 * Bugfix for HT (Hyper-Threading) enabled processors |
kaf24@3400 | 52 * whereby processor resources are shared by all logical processors |
kaf24@3400 | 53 * in a single CPU package. |
kaf24@3400 | 54 * 1.10 28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and |
kaf24@3400 | 55 * Tigran Aivazian <tigran@veritas.com>, |
kaf24@3400 | 56 * Serialize updates as required on HT processors due to speculative |
kaf24@3400 | 57 * nature of implementation. |
kaf24@3400 | 58 * 1.11 22 Mar 2002 Tigran Aivazian <tigran@veritas.com> |
kaf24@3400 | 59 * Fix the panic when writing zero-length microcode chunk. |
kaf24@3400 | 60 * 1.12 29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>, |
kaf24@3400 | 61 * Jun Nakajima <jun.nakajima@intel.com> |
kaf24@3400 | 62 * Support for the microcode updates in the new format. |
kaf24@3400 | 63 * 1.13 10 Oct 2003 Tigran Aivazian <tigran@veritas.com> |
kaf24@3400 | 64 * Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl |
kaf24@3400 | 65 * because we no longer hold a copy of applied microcode |
kaf24@3400 | 66 * in kernel memory. |
kaf24@3400 | 67 * 1.14 25 Jun 2004 Tigran Aivazian <tigran@veritas.com> |
kaf24@3400 | 68 * Fix sigmatch() macro to handle old CPUs with pf == 0. |
kaf24@3400 | 69 * Thanks to Stuart Swales for pointing out this bug. |
kaf24@3400 | 70 */ |
kaf24@3400 | 71 |
kaf24@3400 | 72 #include <xen/config.h> |
kaf24@3400 | 73 #include <xen/kernel.h> |
kaf24@3400 | 74 #include <xen/init.h> |
kaf24@3400 | 75 #include <xen/sched.h> |
kaf24@3400 | 76 #include <xen/slab.h> |
kaf24@3400 | 77 #include <xen/spinlock.h> |
kaf24@3400 | 78 |
kaf24@3400 | 79 #include <asm/msr.h> |
kaf24@3400 | 80 #include <asm/uaccess.h> |
kaf24@3400 | 81 #include <asm/processor.h> |
kaf24@3400 | 82 |
kaf24@3400 | 83 #define pr_debug(x...) ((void)0) |
kaf24@3400 | 84 #define DECLARE_MUTEX(_m) spinlock_t _m = SPIN_LOCK_UNLOCKED |
kaf24@3400 | 85 #define down(_m) spin_lock(_m) |
kaf24@3400 | 86 #define up(_m) spin_unlock(_m) |
iap10@3652 | 87 #define vmalloc(_s) _xmalloc(_s) |
kaf24@3400 | 88 #define vfree(_p) xfree(_p) |
kaf24@3400 | 89 #define num_online_cpus() smp_num_cpus |
kaf24@3400 | 90 static inline int on_each_cpu( |
kaf24@3400 | 91 void (*func) (void *info), void *info, int retry, int wait) |
kaf24@3400 | 92 { |
kaf24@3400 | 93 int ret = smp_call_function(func, info, retry, wait); |
kaf24@3400 | 94 func(info); |
kaf24@3400 | 95 return ret; |
kaf24@3400 | 96 } |
kaf24@3400 | 97 |
kaf24@3400 | 98 #if 0 |
kaf24@3400 | 99 MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver"); |
kaf24@3400 | 100 MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>"); |
kaf24@3400 | 101 MODULE_LICENSE("GPL"); |
kaf24@3400 | 102 #endif |
kaf24@3400 | 103 |
kaf24@3400 | 104 #define MICROCODE_VERSION "1.14" |
kaf24@3400 | 105 |
kaf24@3400 | 106 #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */ |
kaf24@3400 | 107 #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */ |
kaf24@3400 | 108 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */ |
kaf24@3400 | 109 #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */ |
kaf24@3400 | 110 #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */ |
kaf24@3400 | 111 #define DWSIZE (sizeof (u32)) |
kaf24@3400 | 112 #define get_totalsize(mc) \ |
kaf24@3400 | 113 (((microcode_t *)mc)->hdr.totalsize ? \ |
kaf24@3400 | 114 ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE) |
kaf24@3400 | 115 #define get_datasize(mc) \ |
kaf24@3400 | 116 (((microcode_t *)mc)->hdr.datasize ? \ |
kaf24@3400 | 117 ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE) |
kaf24@3400 | 118 |
kaf24@3400 | 119 #define sigmatch(s1, s2, p1, p2) \ |
kaf24@3400 | 120 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0)))) |
kaf24@3400 | 121 |
kaf24@3400 | 122 #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE) |
kaf24@3400 | 123 |
kaf24@3400 | 124 /* serialize access to the physical write to MSR 0x79 */ |
kaf24@3400 | 125 static spinlock_t microcode_update_lock = SPIN_LOCK_UNLOCKED; |
kaf24@3400 | 126 |
kaf24@3400 | 127 /* no concurrent ->write()s are allowed on /dev/cpu/microcode */ |
kaf24@3400 | 128 static DECLARE_MUTEX(microcode_sem); |
kaf24@3400 | 129 |
kaf24@3400 | 130 static void __user *user_buffer; /* user area microcode data buffer */ |
kaf24@3400 | 131 static unsigned int user_buffer_size; /* it's size */ |
kaf24@3400 | 132 |
kaf24@3400 | 133 typedef enum mc_error_code { |
kaf24@3400 | 134 MC_SUCCESS = 0, |
kaf24@3400 | 135 MC_NOTFOUND = 1, |
kaf24@3400 | 136 MC_MARKED = 2, |
kaf24@3400 | 137 MC_ALLOCATED = 3, |
kaf24@3400 | 138 } mc_error_code_t; |
kaf24@3400 | 139 |
kaf24@3400 | 140 static struct ucode_cpu_info { |
kaf24@3400 | 141 unsigned int sig; |
kaf24@3400 | 142 unsigned int pf; |
kaf24@3400 | 143 unsigned int rev; |
kaf24@3400 | 144 unsigned int cksum; |
kaf24@3400 | 145 mc_error_code_t err; |
kaf24@3400 | 146 microcode_t *mc; |
kaf24@3400 | 147 } ucode_cpu_info[NR_CPUS]; |
kaf24@3400 | 148 |
kaf24@3400 | 149 static void collect_cpu_info (void *unused) |
kaf24@3400 | 150 { |
kaf24@3400 | 151 int cpu_num = smp_processor_id(); |
kaf24@3400 | 152 struct cpuinfo_x86 *c = cpu_data + cpu_num; |
kaf24@3400 | 153 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
kaf24@3400 | 154 unsigned int val[2]; |
kaf24@3400 | 155 |
kaf24@3400 | 156 uci->sig = uci->pf = uci->rev = uci->cksum = 0; |
kaf24@3400 | 157 uci->err = MC_NOTFOUND; |
kaf24@3400 | 158 uci->mc = NULL; |
kaf24@3400 | 159 |
kaf24@3400 | 160 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 || |
kaf24@3400 | 161 cpu_has(c, X86_FEATURE_IA64)) { |
kaf24@3400 | 162 printk(KERN_ERR "microcode: CPU%d not a capable Intel processor\n", cpu_num); |
kaf24@3400 | 163 return; |
kaf24@3400 | 164 } else { |
kaf24@3400 | 165 uci->sig = cpuid_eax(0x00000001); |
kaf24@3400 | 166 |
kaf24@3400 | 167 if ((c->x86_model >= 5) || (c->x86 > 6)) { |
kaf24@3400 | 168 /* get processor flags from MSR 0x17 */ |
kaf24@3400 | 169 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); |
kaf24@3400 | 170 uci->pf = 1 << ((val[1] >> 18) & 7); |
kaf24@3400 | 171 } |
kaf24@3400 | 172 } |
kaf24@3400 | 173 |
kaf24@3400 | 174 wrmsr(MSR_IA32_UCODE_REV, 0, 0); |
kaf24@3400 | 175 __asm__ __volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx"); |
kaf24@3400 | 176 /* get the current revision from MSR 0x8B */ |
kaf24@3400 | 177 rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev); |
kaf24@3400 | 178 pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n", |
kaf24@3400 | 179 uci->sig, uci->pf, uci->rev); |
kaf24@3400 | 180 } |
kaf24@3400 | 181 |
kaf24@3400 | 182 static inline void mark_microcode_update (int cpu_num, microcode_header_t *mc_header, int sig, int pf, int cksum) |
kaf24@3400 | 183 { |
kaf24@3400 | 184 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
kaf24@3400 | 185 |
kaf24@3400 | 186 pr_debug("Microcode Found.\n"); |
kaf24@3400 | 187 pr_debug(" Header Revision 0x%x\n", mc_header->hdrver); |
kaf24@3400 | 188 pr_debug(" Loader Revision 0x%x\n", mc_header->ldrver); |
kaf24@3400 | 189 pr_debug(" Revision 0x%x \n", mc_header->rev); |
kaf24@3400 | 190 pr_debug(" Date %x/%x/%x\n", |
kaf24@3400 | 191 ((mc_header->date >> 24 ) & 0xff), |
kaf24@3400 | 192 ((mc_header->date >> 16 ) & 0xff), |
kaf24@3400 | 193 (mc_header->date & 0xFFFF)); |
kaf24@3400 | 194 pr_debug(" Signature 0x%x\n", sig); |
kaf24@3400 | 195 pr_debug(" Type 0x%x Family 0x%x Model 0x%x Stepping 0x%x\n", |
kaf24@3400 | 196 ((sig >> 12) & 0x3), |
kaf24@3400 | 197 ((sig >> 8) & 0xf), |
kaf24@3400 | 198 ((sig >> 4) & 0xf), |
kaf24@3400 | 199 ((sig & 0xf))); |
kaf24@3400 | 200 pr_debug(" Processor Flags 0x%x\n", pf); |
kaf24@3400 | 201 pr_debug(" Checksum 0x%x\n", cksum); |
kaf24@3400 | 202 |
kaf24@3400 | 203 if (mc_header->rev < uci->rev) { |
kaf24@3400 | 204 printk(KERN_ERR "microcode: CPU%d not 'upgrading' to earlier revision" |
kaf24@3400 | 205 " 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev); |
kaf24@3400 | 206 goto out; |
kaf24@3400 | 207 } else if (mc_header->rev == uci->rev) { |
kaf24@3400 | 208 /* notify the caller of success on this cpu */ |
kaf24@3400 | 209 uci->err = MC_SUCCESS; |
kaf24@3400 | 210 printk(KERN_ERR "microcode: CPU%d already at revision" |
kaf24@3400 | 211 " 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev); |
kaf24@3400 | 212 goto out; |
kaf24@3400 | 213 } |
kaf24@3400 | 214 |
kaf24@3400 | 215 pr_debug("microcode: CPU%d found a matching microcode update with " |
kaf24@3400 | 216 " revision 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev); |
kaf24@3400 | 217 uci->cksum = cksum; |
kaf24@3400 | 218 uci->pf = pf; /* keep the original mc pf for cksum calculation */ |
kaf24@3400 | 219 uci->err = MC_MARKED; /* found the match */ |
kaf24@3400 | 220 out: |
kaf24@3400 | 221 return; |
kaf24@3400 | 222 } |
kaf24@3400 | 223 |
kaf24@3400 | 224 static int find_matching_ucodes (void) |
kaf24@3400 | 225 { |
kaf24@3400 | 226 int cursor = 0; |
kaf24@3400 | 227 int error = 0; |
kaf24@3400 | 228 |
kaf24@3400 | 229 while (cursor + MC_HEADER_SIZE < user_buffer_size) { |
kaf24@3400 | 230 microcode_header_t mc_header; |
kaf24@3400 | 231 void *newmc = NULL; |
kaf24@3400 | 232 int i, sum, cpu_num, allocated_flag, total_size, data_size, ext_table_size; |
kaf24@3400 | 233 |
kaf24@3400 | 234 if (copy_from_user(&mc_header, user_buffer + cursor, MC_HEADER_SIZE)) { |
kaf24@3400 | 235 printk(KERN_ERR "microcode: error! Can not read user data\n"); |
kaf24@3400 | 236 error = -EFAULT; |
kaf24@3400 | 237 goto out; |
kaf24@3400 | 238 } |
kaf24@3400 | 239 |
kaf24@3400 | 240 total_size = get_totalsize(&mc_header); |
kaf24@3400 | 241 if ((cursor + total_size > user_buffer_size) || (total_size < DEFAULT_UCODE_TOTALSIZE)) { |
kaf24@3400 | 242 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n"); |
kaf24@3400 | 243 error = -EINVAL; |
kaf24@3400 | 244 goto out; |
kaf24@3400 | 245 } |
kaf24@3400 | 246 |
kaf24@3400 | 247 data_size = get_datasize(&mc_header); |
kaf24@3400 | 248 if ((data_size + MC_HEADER_SIZE > total_size) || (data_size < DEFAULT_UCODE_DATASIZE)) { |
kaf24@3400 | 249 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n"); |
kaf24@3400 | 250 error = -EINVAL; |
kaf24@3400 | 251 goto out; |
kaf24@3400 | 252 } |
kaf24@3400 | 253 |
kaf24@3400 | 254 if (mc_header.ldrver != 1 || mc_header.hdrver != 1) { |
kaf24@3400 | 255 printk(KERN_ERR "microcode: error! Unknown microcode update format\n"); |
kaf24@3400 | 256 error = -EINVAL; |
kaf24@3400 | 257 goto out; |
kaf24@3400 | 258 } |
kaf24@3400 | 259 |
kaf24@3400 | 260 for (cpu_num = 0; cpu_num < num_online_cpus(); cpu_num++) { |
kaf24@3400 | 261 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
kaf24@3400 | 262 if (uci->err != MC_NOTFOUND) /* already found a match or not an online cpu*/ |
kaf24@3400 | 263 continue; |
kaf24@3400 | 264 |
kaf24@3400 | 265 if (sigmatch(mc_header.sig, uci->sig, mc_header.pf, uci->pf)) |
kaf24@3400 | 266 mark_microcode_update(cpu_num, &mc_header, mc_header.sig, mc_header.pf, mc_header.cksum); |
kaf24@3400 | 267 } |
kaf24@3400 | 268 |
kaf24@3400 | 269 ext_table_size = total_size - (MC_HEADER_SIZE + data_size); |
kaf24@3400 | 270 if (ext_table_size) { |
kaf24@3400 | 271 struct extended_sigtable ext_header; |
kaf24@3400 | 272 struct extended_signature ext_sig; |
kaf24@3400 | 273 int ext_sigcount; |
kaf24@3400 | 274 |
kaf24@3400 | 275 if ((ext_table_size < EXT_HEADER_SIZE) |
kaf24@3400 | 276 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) { |
kaf24@3400 | 277 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n"); |
kaf24@3400 | 278 error = -EINVAL; |
kaf24@3400 | 279 goto out; |
kaf24@3400 | 280 } |
kaf24@3400 | 281 if (copy_from_user(&ext_header, user_buffer + cursor |
kaf24@3400 | 282 + MC_HEADER_SIZE + data_size, EXT_HEADER_SIZE)) { |
kaf24@3400 | 283 printk(KERN_ERR "microcode: error! Can not read user data\n"); |
kaf24@3400 | 284 error = -EFAULT; |
kaf24@3400 | 285 goto out; |
kaf24@3400 | 286 } |
kaf24@3400 | 287 if (ext_table_size != exttable_size(&ext_header)) { |
kaf24@3400 | 288 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n"); |
kaf24@3400 | 289 error = -EFAULT; |
kaf24@3400 | 290 goto out; |
kaf24@3400 | 291 } |
kaf24@3400 | 292 |
kaf24@3400 | 293 ext_sigcount = ext_header.count; |
kaf24@3400 | 294 |
kaf24@3400 | 295 for (i = 0; i < ext_sigcount; i++) { |
kaf24@3400 | 296 if (copy_from_user(&ext_sig, user_buffer + cursor + MC_HEADER_SIZE + data_size + EXT_HEADER_SIZE |
kaf24@3400 | 297 + EXT_SIGNATURE_SIZE * i, EXT_SIGNATURE_SIZE)) { |
kaf24@3400 | 298 printk(KERN_ERR "microcode: error! Can not read user data\n"); |
kaf24@3400 | 299 error = -EFAULT; |
kaf24@3400 | 300 goto out; |
kaf24@3400 | 301 } |
kaf24@3400 | 302 for (cpu_num = 0; cpu_num < num_online_cpus(); cpu_num++) { |
kaf24@3400 | 303 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
kaf24@3400 | 304 if (uci->err != MC_NOTFOUND) /* already found a match or not an online cpu*/ |
kaf24@3400 | 305 continue; |
kaf24@3400 | 306 if (sigmatch(ext_sig.sig, uci->sig, ext_sig.pf, uci->pf)) { |
kaf24@3400 | 307 mark_microcode_update(cpu_num, &mc_header, ext_sig.sig, ext_sig.pf, ext_sig.cksum); |
kaf24@3400 | 308 } |
kaf24@3400 | 309 } |
kaf24@3400 | 310 } |
kaf24@3400 | 311 } |
kaf24@3400 | 312 /* now check if any cpu has matched */ |
kaf24@3400 | 313 for (cpu_num = 0, allocated_flag = 0, sum = 0; cpu_num < num_online_cpus(); cpu_num++) { |
kaf24@3400 | 314 if (ucode_cpu_info[cpu_num].err == MC_MARKED) { |
kaf24@3400 | 315 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
kaf24@3400 | 316 if (!allocated_flag) { |
kaf24@3400 | 317 allocated_flag = 1; |
kaf24@3400 | 318 newmc = vmalloc(total_size); |
kaf24@3400 | 319 if (!newmc) { |
kaf24@3400 | 320 printk(KERN_ERR "microcode: error! Can not allocate memory\n"); |
kaf24@3400 | 321 error = -ENOMEM; |
kaf24@3400 | 322 goto out; |
kaf24@3400 | 323 } |
kaf24@3400 | 324 if (copy_from_user(newmc + MC_HEADER_SIZE, |
kaf24@3400 | 325 user_buffer + cursor + MC_HEADER_SIZE, |
kaf24@3400 | 326 total_size - MC_HEADER_SIZE)) { |
kaf24@3400 | 327 printk(KERN_ERR "microcode: error! Can not read user data\n"); |
kaf24@3400 | 328 vfree(newmc); |
kaf24@3400 | 329 error = -EFAULT; |
kaf24@3400 | 330 goto out; |
kaf24@3400 | 331 } |
kaf24@3400 | 332 memcpy(newmc, &mc_header, MC_HEADER_SIZE); |
kaf24@3400 | 333 /* check extended table checksum */ |
kaf24@3400 | 334 if (ext_table_size) { |
kaf24@3400 | 335 int ext_table_sum = 0; |
kaf24@3400 | 336 int * ext_tablep = (((void *) newmc) + MC_HEADER_SIZE + data_size); |
kaf24@3400 | 337 i = ext_table_size / DWSIZE; |
kaf24@3400 | 338 while (i--) ext_table_sum += ext_tablep[i]; |
kaf24@3400 | 339 if (ext_table_sum) { |
kaf24@3400 | 340 printk(KERN_WARNING "microcode: aborting, bad extended signature table checksum\n"); |
kaf24@3400 | 341 vfree(newmc); |
kaf24@3400 | 342 error = -EINVAL; |
kaf24@3400 | 343 goto out; |
kaf24@3400 | 344 } |
kaf24@3400 | 345 } |
kaf24@3400 | 346 |
kaf24@3400 | 347 /* calculate the checksum */ |
kaf24@3400 | 348 i = (MC_HEADER_SIZE + data_size) / DWSIZE; |
kaf24@3400 | 349 while (i--) sum += ((int *)newmc)[i]; |
kaf24@3400 | 350 sum -= (mc_header.sig + mc_header.pf + mc_header.cksum); |
kaf24@3400 | 351 } |
kaf24@3400 | 352 ucode_cpu_info[cpu_num].mc = newmc; |
kaf24@3400 | 353 ucode_cpu_info[cpu_num].err = MC_ALLOCATED; /* mc updated */ |
kaf24@3400 | 354 if (sum + uci->sig + uci->pf + uci->cksum != 0) { |
kaf24@3400 | 355 printk(KERN_ERR "microcode: CPU%d aborting, bad checksum\n", cpu_num); |
kaf24@3400 | 356 error = -EINVAL; |
kaf24@3400 | 357 goto out; |
kaf24@3400 | 358 } |
kaf24@3400 | 359 } |
kaf24@3400 | 360 } |
kaf24@3400 | 361 cursor += total_size; /* goto the next update patch */ |
kaf24@3400 | 362 } /* end of while */ |
kaf24@3400 | 363 out: |
kaf24@3400 | 364 return error; |
kaf24@3400 | 365 } |
kaf24@3400 | 366 |
kaf24@3400 | 367 static void do_update_one (void * unused) |
kaf24@3400 | 368 { |
kaf24@3400 | 369 unsigned long flags; |
kaf24@3400 | 370 unsigned int val[2]; |
kaf24@3400 | 371 int cpu_num = smp_processor_id(); |
kaf24@3400 | 372 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
kaf24@3400 | 373 |
kaf24@3400 | 374 if (uci->mc == NULL) { |
kaf24@3400 | 375 printk(KERN_INFO "microcode: No suitable data for CPU%d\n", cpu_num); |
kaf24@3400 | 376 return; |
kaf24@3400 | 377 } |
kaf24@3400 | 378 |
kaf24@3400 | 379 /* serialize access to the physical write to MSR 0x79 */ |
kaf24@3400 | 380 spin_lock_irqsave(µcode_update_lock, flags); |
kaf24@3400 | 381 |
kaf24@3400 | 382 /* write microcode via MSR 0x79 */ |
kaf24@3400 | 383 wrmsr(MSR_IA32_UCODE_WRITE, |
kaf24@3400 | 384 (unsigned long) uci->mc->bits, |
kaf24@3400 | 385 (unsigned long) uci->mc->bits >> 16 >> 16); |
kaf24@3400 | 386 wrmsr(MSR_IA32_UCODE_REV, 0, 0); |
kaf24@3400 | 387 |
kaf24@3400 | 388 __asm__ __volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx"); |
kaf24@3400 | 389 /* get the current revision from MSR 0x8B */ |
kaf24@3400 | 390 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]); |
kaf24@3400 | 391 |
kaf24@3400 | 392 /* notify the caller of success on this cpu */ |
kaf24@3400 | 393 uci->err = MC_SUCCESS; |
kaf24@3400 | 394 spin_unlock_irqrestore(µcode_update_lock, flags); |
kaf24@3400 | 395 printk(KERN_INFO "microcode: CPU%d updated from revision " |
kaf24@3400 | 396 "0x%x to 0x%x, date = %08x \n", |
kaf24@3400 | 397 cpu_num, uci->rev, val[1], uci->mc->hdr.date); |
kaf24@3400 | 398 return; |
kaf24@3400 | 399 } |
kaf24@3400 | 400 |
kaf24@3400 | 401 static int do_microcode_update (void) |
kaf24@3400 | 402 { |
kaf24@3400 | 403 int i, error; |
kaf24@3400 | 404 |
kaf24@3400 | 405 if (on_each_cpu(collect_cpu_info, NULL, 1, 1) != 0) { |
kaf24@3400 | 406 printk(KERN_ERR "microcode: Error! Could not run on all processors\n"); |
kaf24@3400 | 407 error = -EIO; |
kaf24@3400 | 408 goto out; |
kaf24@3400 | 409 } |
kaf24@3400 | 410 |
kaf24@3400 | 411 if ((error = find_matching_ucodes())) { |
kaf24@3400 | 412 printk(KERN_ERR "microcode: Error in the microcode data\n"); |
kaf24@3400 | 413 goto out_free; |
kaf24@3400 | 414 } |
kaf24@3400 | 415 |
kaf24@3400 | 416 if (on_each_cpu(do_update_one, NULL, 1, 1) != 0) { |
kaf24@3400 | 417 printk(KERN_ERR "microcode: Error! Could not run on all processors\n"); |
kaf24@3400 | 418 error = -EIO; |
kaf24@3400 | 419 } |
kaf24@3400 | 420 |
kaf24@3400 | 421 out_free: |
kaf24@3400 | 422 for (i = 0; i < num_online_cpus(); i++) { |
kaf24@3400 | 423 if (ucode_cpu_info[i].mc) { |
kaf24@3400 | 424 int j; |
kaf24@3400 | 425 void *tmp = ucode_cpu_info[i].mc; |
kaf24@3400 | 426 vfree(tmp); |
kaf24@3400 | 427 for (j = i; j < num_online_cpus(); j++) { |
kaf24@3400 | 428 if (ucode_cpu_info[j].mc == tmp) |
kaf24@3400 | 429 ucode_cpu_info[j].mc = NULL; |
kaf24@3400 | 430 } |
kaf24@3400 | 431 } |
kaf24@3400 | 432 } |
kaf24@3400 | 433 out: |
kaf24@3400 | 434 return error; |
kaf24@3400 | 435 } |
kaf24@3400 | 436 |
kaf24@3400 | 437 int microcode_update(void *buf, unsigned long len) |
kaf24@3400 | 438 { |
kaf24@3400 | 439 int ret; |
kaf24@3400 | 440 |
kaf24@3400 | 441 if (len < DEFAULT_UCODE_TOTALSIZE) { |
kaf24@3400 | 442 printk(KERN_ERR "microcode: not enough data\n"); |
kaf24@3400 | 443 return -EINVAL; |
kaf24@3400 | 444 } |
kaf24@3400 | 445 |
kaf24@3400 | 446 down(µcode_sem); |
kaf24@3400 | 447 |
kaf24@3400 | 448 user_buffer = (void __user *) buf; |
kaf24@3400 | 449 user_buffer_size = (int) len; |
kaf24@3400 | 450 |
kaf24@3400 | 451 ret = do_microcode_update(); |
kaf24@3400 | 452 |
kaf24@3400 | 453 up(µcode_sem); |
kaf24@3400 | 454 |
kaf24@3400 | 455 return ret; |
kaf24@3400 | 456 } |