* Thanks to Stuart Swales for pointing out this bug.
*/
-/*#define DEBUG pr_debug */
+/* #define DEBUG pr_debug */
#include <linux/capability.h>
#include <linux/kernel.h>
#include <linux/init.h>
struct microcode_ops *microcode_ops;
/* no concurrent ->write()s are allowed on /dev/cpu/microcode */
-DEFINE_MUTEX(microcode_mutex);
-EXPORT_SYMBOL_GPL(microcode_mutex);
+static DEFINE_MUTEX(microcode_mutex);
struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
EXPORT_SYMBOL_GPL(ucode_cpu_info);
struct platform_device *microcode_pdev;
EXPORT_SYMBOL_GPL(microcode_pdev);
-static void microcode_init_cpu(int cpu, int resume)
-{
- cpumask_t old;
- struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
-
- old = current->cpus_allowed;
-
- set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
- mutex_lock(µcode_mutex);
- microcode_ops->collect_cpu_info(cpu);
- if (uci->valid && system_state == SYSTEM_RUNNING && !resume)
- microcode_ops->cpu_request_microcode(cpu);
- mutex_unlock(µcode_mutex);
- set_cpus_allowed_ptr(current, &old);
-}
-
static ssize_t reload_store(struct sys_device *dev,
struct sysdev_attribute *attr,
const char *buf, size_t sz)
cpumask_t old = current->cpus_allowed;
get_online_cpus();
- set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
-
- mutex_lock(µcode_mutex);
- if (uci->valid)
- err = microcode_ops->cpu_request_microcode(cpu);
- mutex_unlock(µcode_mutex);
+ if (cpu_online(cpu)) {
+ set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
+ mutex_lock(µcode_mutex);
+ if (uci->valid)
+ err = microcode_ops->cpu_request_microcode(cpu);
+ mutex_unlock(µcode_mutex);
+ set_cpus_allowed_ptr(current, &old);
+ }
put_online_cpus();
- set_cpus_allowed_ptr(current, &old);
}
if (err)
return err;
{
struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
- return sprintf(buf, "0x%x\n", uci->rev);
+ return sprintf(buf, "0x%x\n", uci->cpu_sig.rev);
}
static ssize_t pf_show(struct sys_device *dev,
{
struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
- return sprintf(buf, "0x%x\n", uci->pf);
+ return sprintf(buf, "0x%x\n", uci->cpu_sig.pf);
}
static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
.name = "microcode",
};
-static int __mc_sysdev_add(struct sys_device *sys_dev, int resume)
+static void microcode_fini_cpu(int cpu)
+{
+ struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+
+ mutex_lock(µcode_mutex);
+ microcode_ops->microcode_fini_cpu(cpu);
+ uci->valid = 0;
+ mutex_unlock(µcode_mutex);
+}
+
+static void collect_cpu_info(int cpu)
+{
+ struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+
+ memset(uci, 0, sizeof(*uci));
+ if (!microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig))
+ uci->valid = 1;
+}
+
+static void microcode_resume_cpu(int cpu)
+{
+ struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+ struct cpu_signature nsig;
+
+ pr_debug("microcode: CPU%d resumed\n", cpu);
+
+ if (!uci->mc.valid_mc)
+ return;
+
+ /*
+ * Let's verify that the 'cached' ucode does belong
+ * to this cpu (a bit of paranoia):
+ */
+ if (microcode_ops->collect_cpu_info(cpu, &nsig)) {
+ microcode_fini_cpu(cpu);
+ return;
+ }
+
+ if (memcmp(&nsig, &uci->cpu_sig, sizeof(nsig))) {
+ microcode_fini_cpu(cpu);
+ /* Should we look for a new ucode here? */
+ return;
+ }
+
+ microcode_ops->apply_microcode(cpu);
+}
+
+void microcode_update_cpu(int cpu)
+{
+ struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+
+ /* We should bind the task to the CPU */
+ BUG_ON(raw_smp_processor_id() != cpu);
+
+ mutex_lock(µcode_mutex);
+ /*
+ * Check if the system resume is in progress (uci->valid != NULL),
+ * otherwise just request a firmware:
+ */
+ if (uci->valid) {
+ microcode_resume_cpu(cpu);
+ } else {
+ collect_cpu_info(cpu);
+ if (uci->valid && system_state == SYSTEM_RUNNING)
+ microcode_ops->cpu_request_microcode(cpu);
+ }
+ mutex_unlock(µcode_mutex);
+}
+
+static void microcode_init_cpu(int cpu)
+{
+ cpumask_t old = current->cpus_allowed;
+
+ set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
+ microcode_update_cpu(cpu);
+ set_cpus_allowed_ptr(current, &old);
+}
+
+static int mc_sysdev_add(struct sys_device *sys_dev)
{
int err, cpu = sys_dev->id;
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
if (err)
return err;
- microcode_init_cpu(cpu, resume);
-
+ microcode_init_cpu(cpu);
return 0;
}
-static int mc_sysdev_add(struct sys_device *sys_dev)
-{
- return __mc_sysdev_add(sys_dev, 0);
-}
-
static int mc_sysdev_remove(struct sys_device *sys_dev)
{
int cpu = sys_dev->id;
return 0;
pr_debug("microcode: CPU%d removed\n", cpu);
- microcode_ops->microcode_fini_cpu(cpu);
+ microcode_fini_cpu(cpu);
sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
return 0;
}
sys_dev = get_cpu_sysdev(cpu);
switch (action) {
- case CPU_UP_CANCELED_FROZEN:
- /* The CPU refused to come up during a system resume */
- microcode_ops->microcode_fini_cpu(cpu);
- break;
case CPU_ONLINE:
- case CPU_DOWN_FAILED:
- mc_sysdev_add(sys_dev);
- break;
case CPU_ONLINE_FROZEN:
- /* System-wide resume is in progress, try to apply microcode */
- if (microcode_ops->apply_microcode_check_cpu(cpu)) {
- /* The application of microcode failed */
- microcode_ops->microcode_fini_cpu(cpu);
- __mc_sysdev_add(sys_dev, 1);
- break;
- }
+ microcode_init_cpu(cpu);
+ case CPU_DOWN_FAILED:
case CPU_DOWN_FAILED_FROZEN:
+ pr_debug("microcode: CPU%d added\n", cpu);
if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
printk(KERN_ERR "microcode: Failed to create the sysfs "
"group for CPU%d\n", cpu);
break;
case CPU_DOWN_PREPARE:
- mc_sysdev_remove(sys_dev);
- break;
case CPU_DOWN_PREPARE_FROZEN:
/* Suspend is in progress, only remove the interface */
sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
+ pr_debug("microcode: CPU%d removed\n", cpu);
+ break;
+ case CPU_DEAD:
+ case CPU_UP_CANCELED_FROZEN:
+ /* The CPU refused to come up during a system resume */
+ microcode_fini_cpu(cpu);
break;
}
return NOTIFY_OK;
/* serialize access to the physical write */
static DEFINE_SPINLOCK(microcode_update_lock);
-/* no concurrent ->write()s are allowed on /dev/cpu/microcode */
-extern struct mutex (microcode_mutex);
-
struct equiv_cpu_entry *equiv_cpu_table;
-extern struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
-
-static void collect_cpu_info_amd(int cpu)
+static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
{
struct cpuinfo_x86 *c = &cpu_data(cpu);
- struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
- /* We should bind the task to the CPU */
- BUG_ON(raw_smp_processor_id() != cpu);
- uci->rev = 0;
- uci->pf = 0;
- uci->mc.mc_amd = NULL;
- uci->valid = 1;
+ memset(csig, 0, sizeof(*csig));
if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
printk(KERN_ERR "microcode: CPU%d not a capable AMD processor\n",
cpu);
- uci->valid = 0;
- return;
+ return -1;
}
asm volatile("movl %1, %%ecx; rdmsr"
- : "=a" (uci->rev)
+ : "=a" (csig->rev)
: "i" (0x0000008B) : "ecx");
printk(KERN_INFO "microcode: collect_cpu_info_amd : patch_id=0x%x\n",
- uci->rev);
+ csig->rev);
+
+ return 0;
}
static int get_matching_microcode_amd(void *mc, int cpu)
if (equiv_cpu_table == NULL) {
printk(KERN_INFO "microcode: CPU%d microcode update with "
"version 0x%x (current=0x%x)\n",
- cpu, mc_header->patch_id, uci->rev);
+ cpu, mc_header->patch_id, uci->cpu_sig.rev);
goto out;
}
pci_dev_put(sb_pci_dev);
}
- if (mc_header->patch_id <= uci->rev)
+ if (mc_header->patch_id <= uci->cpu_sig.rev)
return 0;
printk(KERN_INFO "microcode: CPU%d found a matching microcode "
"update with version 0x%x (current=0x%x)\n",
- cpu, mc_header->patch_id, uci->rev);
+ cpu, mc_header->patch_id, uci->cpu_sig.rev);
out:
new_mc = vmalloc(UCODE_MAX_SIZE);
printk(KERN_INFO "microcode: CPU%d updated from revision "
"0x%x to 0x%x \n",
- cpu_num, uci->rev, uci->mc.mc_amd->hdr.patch_id);
+ cpu_num, uci->cpu_sig.rev, uci->mc.mc_amd->hdr.patch_id);
- uci->rev = rev;
+ uci->cpu_sig.rev = rev;
}
#ifdef CONFIG_MICROCODE_OLD_INTERFACE
return error;
}
-static int apply_microcode_check_cpu_amd(int cpu)
-{
- struct cpuinfo_x86 *c = &cpu_data(cpu);
- struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
- unsigned int rev;
- cpumask_t old;
- int err = 0;
-
- /* Check if the microcode is available */
- if (!uci->mc.mc_amd)
- return 0;
-
- old = current->cpus_allowed;
- set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
-
- /* Check if the microcode we have in memory matches the CPU */
- if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 16)
- err = -EINVAL;
-
- if (!err) {
- asm volatile("movl %1, %%ecx; rdmsr"
- : "=a" (rev)
- : "i" (0x0000008B) : "ecx");
-
- if (uci->rev != rev)
- err = -EINVAL;
- }
-
- if (!err)
- apply_microcode_amd(cpu);
- else
- printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
- " rev=0x%x\n",
- cpu, uci->rev);
-
- set_cpus_allowed(current, old);
- return err;
-}
-
static void microcode_fini_cpu_amd(int cpu)
{
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
- mutex_lock(µcode_mutex);
- uci->valid = 0;
vfree(uci->mc.mc_amd);
uci->mc.mc_amd = NULL;
- mutex_unlock(µcode_mutex);
}
static struct microcode_ops microcode_amd_ops = {
.get_next_ucode = get_next_ucode_amd,
.get_matching_microcode = get_matching_microcode_amd,
.microcode_sanity_check = NULL,
- .apply_microcode_check_cpu = apply_microcode_check_cpu_amd,
.cpu_request_microcode = cpu_request_microcode_amd,
.collect_cpu_info = collect_cpu_info_amd,
.apply_microcode = apply_microcode_amd,
/* serialize access to the physical write to MSR 0x79 */
static DEFINE_SPINLOCK(microcode_update_lock);
-/* no concurrent ->write()s are allowed on /dev/cpu/microcode */
-extern struct mutex microcode_mutex;
-
-extern struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
-
-static void collect_cpu_info(int cpu_num)
+static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
{
struct cpuinfo_x86 *c = &cpu_data(cpu_num);
- struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
unsigned int val[2];
- /* We should bind the task to the CPU */
- BUG_ON(raw_smp_processor_id() != cpu_num);
- uci->pf = uci->rev = 0;
- uci->mc.mc_intel = NULL;
- uci->valid = 1;
+ memset(csig, 0, sizeof(*csig));
if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
cpu_has(c, X86_FEATURE_IA64)) {
printk(KERN_ERR "microcode: CPU%d not a capable Intel "
"processor\n", cpu_num);
- uci->valid = 0;
- return;
+ return -1;
}
- uci->sig = cpuid_eax(0x00000001);
+ csig->sig = cpuid_eax(0x00000001);
if ((c->x86_model >= 5) || (c->x86 > 6)) {
/* get processor flags from MSR 0x17 */
rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
- uci->pf = 1 << ((val[1] >> 18) & 7);
+ csig->pf = 1 << ((val[1] >> 18) & 7);
}
wrmsr(MSR_IA32_UCODE_REV, 0, 0);
/* see notes above for revision 1.07. Apparent chip bug */
sync_core();
/* get the current revision from MSR 0x8B */
- rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
+ rdmsr(MSR_IA32_UCODE_REV, val[0], csig->rev);
pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
- uci->sig, uci->pf, uci->rev);
+ csig->sig, csig->pf, csig->rev);
+
+ return 0;
}
static inline int microcode_update_match(int cpu_num,
{
struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
- if (!sigmatch(sig, uci->sig, pf, uci->pf)
- || mc_header->rev <= uci->rev)
+ if (!sigmatch(sig, uci->cpu_sig.sig, pf, uci->cpu_sig.pf)
+ || mc_header->rev <= uci->cpu_sig.rev)
return 0;
return 1;
}
find:
pr_debug("microcode: CPU%d found a matching microcode update with"
" version 0x%x (current=0x%x)\n",
- cpu, mc_header->rev, uci->rev);
+ cpu, mc_header->rev, uci->cpu_sig.rev);
new_mc = vmalloc(total_size);
if (!new_mc) {
printk(KERN_ERR "microcode: error! Can not allocate memory\n");
spin_unlock_irqrestore(µcode_update_lock, flags);
if (val[1] != uci->mc.mc_intel->hdr.rev) {
printk(KERN_ERR "microcode: CPU%d update from revision "
- "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
+ "0x%x to 0x%x failed\n", cpu_num, uci->cpu_sig.rev, val[1]);
return;
}
printk(KERN_INFO "microcode: CPU%d updated from revision "
"0x%x to 0x%x, date = %04x-%02x-%02x \n",
- cpu_num, uci->rev, val[1],
+ cpu_num, uci->cpu_sig.rev, val[1],
uci->mc.mc_intel->hdr.date & 0xffff,
uci->mc.mc_intel->hdr.date >> 24,
(uci->mc.mc_intel->hdr.date >> 16) & 0xff);
- uci->rev = val[1];
+ uci->cpu_sig.rev = val[1];
}
#ifdef CONFIG_MICROCODE_OLD_INTERFACE
return error;
}
-static int apply_microcode_check_cpu(int cpu)
-{
- struct cpuinfo_x86 *c = &cpu_data(cpu);
- struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
- cpumask_t old;
- unsigned int val[2];
- int err = 0;
-
- /* Check if the microcode is available */
- if (!uci->mc.mc_intel)
- return 0;
-
- old = current->cpus_allowed;
- set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
-
- /* Check if the microcode we have in memory matches the CPU */
- if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
- cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001))
- err = -EINVAL;
-
- if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) {
- /* get processor flags from MSR 0x17 */
- rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
- if (uci->pf != (1 << ((val[1] >> 18) & 7)))
- err = -EINVAL;
- }
-
- if (!err) {
- wrmsr(MSR_IA32_UCODE_REV, 0, 0);
- /* see notes above for revision 1.07. Apparent chip bug */
- sync_core();
- /* get the current revision from MSR 0x8B */
- rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
- if (uci->rev != val[1])
- err = -EINVAL;
- }
-
- if (!err)
- apply_microcode(cpu);
- else
- printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
- " sig=0x%x, pf=0x%x, rev=0x%x\n",
- cpu, uci->sig, uci->pf, uci->rev);
-
- set_cpus_allowed_ptr(current, &old);
- return err;
-}
-
static void microcode_fini_cpu(int cpu)
{
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
- mutex_lock(µcode_mutex);
- uci->valid = 0;
vfree(uci->mc.mc_intel);
uci->mc.mc_intel = NULL;
- mutex_unlock(µcode_mutex);
}
static struct microcode_ops microcode_intel_ops = {
.get_next_ucode = get_next_ucode,
.get_matching_microcode = get_matching_microcode,
.microcode_sanity_check = microcode_sanity_check,
- .apply_microcode_check_cpu = apply_microcode_check_cpu,
.cpu_request_microcode = cpu_request_microcode,
.collect_cpu_info = collect_cpu_info,
.apply_microcode = apply_microcode,
+#ifndef ASM_X86__MICROCODE_H
+#define ASM_X86__MICROCODE_H
+
extern int microcode_init(void *opaque, struct module *module);
extern void microcode_exit(void);
+struct cpu_signature;
+
struct microcode_ops {
long (*get_next_ucode)(void **mc, long offset);
long (*microcode_get_next_ucode)(void **mc, long offset);
int (*get_matching_microcode)(void *mc, int cpu);
- int (*apply_microcode_check_cpu)(int cpu);
int (*microcode_sanity_check)(void *mc);
int (*cpu_request_microcode)(int cpu);
- void (*collect_cpu_info)(int cpu_num);
+ int (*collect_cpu_info)(int cpu_num, struct cpu_signature *csig);
void (*apply_microcode)(int cpu);
void (*microcode_fini_cpu)(int cpu);
void (*clear_patch)(void *data);
unsigned int mpb[0];
};
-struct ucode_cpu_info {
- int valid;
+struct cpu_signature {
unsigned int sig;
unsigned int pf;
unsigned int rev;
+};
+
+struct ucode_cpu_info {
+ struct cpu_signature cpu_sig;
+ int valid;
union {
struct microcode_intel *mc_intel;
struct microcode_amd *mc_amd;
+ void *valid_mc;
} mc;
};
+extern struct ucode_cpu_info ucode_cpu_info[];
+
+#endif /* ASM_X86__MICROCODE_H */