Skip to contentSkip to conversation

microcode update driver rewrite - takes 2

PATCH In review Shaohua Li 6 participants 12 messages Jun 27, 2006 – Feb 14, 2007 Driver core

lore
Share & tools
lore

Shaohua Li

author
#0 lore raw

PATCH 1/1 microcode update driver rewrite - takes 2

This is the rewrite of microcode update driver. Changes:
1. trim the code
2. using request_firmware to pull ucode from userspace, so we don't need the application 'microcode_ctl' to assist. We name each ucode file according to CPU's info as intel-ucode/family-model-stepping. In this way we could split ucode file as small one. This has a lot of advantages such as selectively update and validate microcode for specific models, better manage microcode file, easily write tools for administerators and so on.
3. add sysfs support. Currently each CPU has two microcode related attributes. One is 'version' which shows current ucode version of CPU. Tools can use the attribute do validation or show CPU ucode status. The other is 'reload' which allows manually reloading ucode.
4. add suspend/resume and CPU hotplug support.

With the changes, we should put all intel-ucode/xx-xx-xx microcode files into the firmware dir (I had a tool to split previous big data file into small one and later we will release new style data file). The init script should be changed to just loading the driver without unloading for hotplug and suspend/resume (for back compatibility I keep old interface, so old init script also works).

Previous post is at
http://marc.theaimsgroup.com/?l=linux-kernel&m=114852925121064&w=2
Changes against previous patch:
1. use sys_create_group to add attributes
2. add a fake platform_device for reqeust_firmware as Greg disliked the request_firmware_kobj interface previous patch introduced
3. add a new attribute 'pf' to help tools check if CPU has latest ucode

---
 arch/i386/Kconfig             |    5 
 arch/i386/kernel/microcode.c  |  743 ++++++++++++++++++++++++++++--------------
 arch/x86_64/Kconfig           |    5 
 drivers/base/firmware_class.c |    2 
 4 files changed, 523 insertions(+), 232 deletions(-)
arch/i386/Kconfig +5 -0
Index: linux-2.6.17/arch/i386/Kconfig
===================================================================
@@ -389,6 +389,11 @@ config MICROCODE
 	  To compile this driver as a module, choose M here: the
 	  module will be called microcode.
 
+config MICROCODE_OLD_INTERFACE
+	bool
+	depends on MICROCODE
+	default y
+
 config X86_MSR
 	tristate "/dev/cpu/*/msr - Model-specific register support"
 	help
arch/i386/kernel/microcode.c +512 -231

863 lines not on this page · raw message

arch/x86_64/Kconfig +5 -0
Index: linux-2.6.17/arch/x86_64/Kconfig
===================================================================
@@ -166,6 +166,11 @@ config MICROCODE
 	  If you use modprobe or kmod you may also want to add the line
 	  'alias char-major-10-184 microcode' to your /etc/modules.conf file.
 
+config MICROCODE_OLD_INTERFACE
+	bool
+	depends on MICROCODE
+	default y
+
 config X86_MSR
 	tristate "/dev/cpu/*/msr - Model-specific register support"
 	help
drivers/base/firmware_class.c +1 -1
Index: linux-2.6.17/drivers/base/firmware_class.c
===================================================================
@@ -602,7 +602,7 @@ firmware_class_exit(void)
 	class_unregister(&firmware_class);
 }
 
-module_init(firmware_class_init);
+fs_initcall(firmware_class_init);
 module_exit(firmware_class_exit);
 
 EXPORT_SYMBOL(release_firmware);
#1 lore raw
Quoted 6 lines · Shaohua Li

This is the rewrite of microcode update driver. Changes:
1. trim the code
2. using request_firmware to pull ucode from userspace, so we don't need the application 'microcode_ctl' to assist. We name each ucode file according to CPU's info as intel-ucode/family-model-stepping. In this way we could split ucode file as small one. This has a lot of advantages

Quoted 8 lines

such as selectively update and validate microcode for specific models, better manage microcode file, easily write tools for administerators and so on.
3. add sysfs support. Currently each CPU has two microcode related attributes. One is 'version' which shows current ucode version of CPU. Tools can use the attribute do validation or show CPU ucode status. The other is 'reload' which allows manually reloading ucode.
4. add suspend/resume and CPU hotplug support.

Why not break this up into 4 patches so we can better review them?

Remember, one patch per change please :)

Quoted 7 lines

With the changes, we should put all intel-ucode/xx-xx-xx microcode files into the firmware dir (I had a tool to split previous big data file into small one and later we will release new style data file). The init script should be changed to just loading the driver without unloading for hotplug and suspend/resume (for back compatibility I keep old interface, so old init script also works).

Quoted 7 lines

Previous post is at
http://marc.theaimsgroup.com/?l=linux-kernel&m=114852925121064&w=2
Changes against previous patch:
1. use sys_create_group to add attributes
2. add a fake platform_device for reqeust_firmware as Greg disliked the request_firmware_kobj interface previous patch introduced
3. add a new attribute 'pf' to help tools check if CPU has latest ucode

What does "pf" stand for?

thanks,

greg k-h

author Acked-by Tigran Aivazian
#2 lore raw
Quoted 16 lines · Greg KH
Quoted 6 lines · Shaohua Li

This is the rewrite of microcode update driver. Changes:
1. trim the code
2. using request_firmware to pull ucode from userspace, so we don't need the application 'microcode_ctl' to assist. We name each ucode file according to CPU's info as intel-ucode/family-model-stepping. In this way we could split ucode file as small one. This has a lot of advantages

Quoted 8 lines

such as selectively update and validate microcode for specific models, better manage microcode file, easily write tools for administerators and so on.
3. add sysfs support. Currently each CPU has two microcode related attributes. One is 'version' which shows current ucode version of CPU. Tools can use the attribute do validation or show CPU ucode status. The other is 'reload' which allows manually reloading ucode.
4. add suspend/resume and CPU hotplug support.

Quoted 3 lines

Why not break this up into 4 patches so we can better review them?

Remember, one patch per change please :)

Ok, I split it to three patches (3 & 4 could be in one patch)

Quoted 16 lines
Quoted 8 lines

With the changes, we should put all intel-ucode/xx-xx-xx microcode files into the firmware dir (I had a tool to split previous big data file into small one and later we will release new style data file). The init script should be changed to just loading the driver without unloading for hotplug and suspend/resume (for back compatibility I keep old interface, so old init script also works).

Quoted 7 lines

Previous post is at
http://marc.theaimsgroup.com/?l=linux-kernel&m=114852925121064&w=2
Changes against previous patch:
1. use sys_create_group to add attributes
2. add a fake platform_device for reqeust_firmware as Greg disliked the request_firmware_kobj interface previous patch introduced
3. add a new attribute 'pf' to help tools check if CPU has latest ucode

Quoted 1 line

What does "pf" stand for?

processor flags. I changed it to processor_flags in next path.

[PATCH 1/3] trim microcode driver to make it clean. Old interface is kept.

---
 arch/i386/Kconfig            |    5 
 arch/i386/kernel/microcode.c |  488 ++++++++++++++++++++++---------------------
 arch/x86_64/Kconfig          |    5 
 3 files changed, 263 insertions(+), 235 deletions(-)
arch/i386/Kconfig +5 -0
Index: linux-2.6.17/arch/i386/Kconfig
===================================================================
@@ -389,6 +389,11 @@ config MICROCODE
 	  To compile this driver as a module, choose M here: the
 	  module will be called microcode.
 
+config MICROCODE_OLD_INTERFACE
+	bool
+	depends on MICROCODE
+	default y
+
 config X86_MSR
 	tristate "/dev/cpu/*/msr - Model-specific register support"
 	help
arch/i386/kernel/microcode.c +253 -235

598 lines not on this page · raw message

arch/x86_64/Kconfig +5 -0
Index: linux-2.6.17/arch/x86_64/Kconfig
===================================================================
@@ -166,6 +166,11 @@ config MICROCODE
 	  If you use modprobe or kmod you may also want to add the line
 	  'alias char-major-10-184 microcode' to your /etc/modules.conf file.
 
+config MICROCODE_OLD_INTERFACE
+	bool
+	depends on MICROCODE
+	default y
+
 config X86_MSR
 	tristate "/dev/cpu/*/msr - Model-specific register support"
 	help
author Acked-by Tigran Aivazian
#3 lore raw
Quoted 48 lines · Shaohua Li
Quoted 4 lines · Greg KH
Quoted 3 lines · Shaohua Li

This is the rewrite of microcode update driver. Changes:
1. trim the code
2. using request_firmware to pull ucode from userspace, so we

don't need

Quoted 1 line
Quoted 1 line

the application 'microcode_ctl' to assist. We name each ucode

file

Quoted 1 line
Quoted 1 line

according to CPU's info as intel-ucode/family-model-stepping. In

this

Quoted 1 line
Quoted 1 line

way we could split ucode file as small one. This has a lot of

advantages

Quoted 1 line
Quoted 1 line

such as selectively update and validate microcode for specific

models,

Quoted 1 line
Quoted 1 line

better manage microcode file, easily write tools for

administerators and

Quoted 2 lines
Quoted 2 lines

so on.
3. add sysfs support. Currently each CPU has two microcode

related

Quoted 1 line
Quoted 1 line

attributes. One is 'version' which shows current ucode version of

CPU.

Quoted 1 line
Quoted 1 line

Tools can use the attribute do validation or show CPU ucode

status. The

Quoted 6 lines
Quoted 2 lines

other is 'reload' which allows manually reloading ucode.
4. add suspend/resume and CPU hotplug support.

Why not break this up into 4 patches so we can better review them?

Remember, one patch per change please :)

Ok, I split it to three patches (3 & 4 could be in one patch)

Quoted 2 lines
Quoted 2 lines

With the changes, we should put all intel-ucode/xx-xx-xx microcode

files

Quoted 1 line
Quoted 1 line

into the firmware dir (I had a tool to split previous big data

file into

Quoted 1 line
Quoted 1 line

small one and later we will release new style data file). The

init

Quoted 1 line
Quoted 1 line

script should be changed to just loading the driver without

unloading

Quoted 5 lines
Quoted 5 lines

for hotplug and suspend/resume (for back compatibility I keep old interface, so old init script also works).

Previous post is at

http://marc.theaimsgroup.com/?l=linux-kernel&m=114852925121064&w=2

Quoted 3 lines
Quoted 3 lines

Changes against previous patch:
1. use sys_create_group to add attributes
2. add a fake platform_device for reqeust_firmware as Greg

Quoted 7 lines

disliked the

Quoted 2 lines
Quoted 2 lines

request_firmware_kobj interface previous patch introduced
3. add a new attribute 'pf' to help tools check if CPU has latest

ucode

Quoted 2 lines

What does "pf" stand for?

processor flags. I changed it to processor_flags in next path.

Andrew, this one can be applied in latest -mm. Other two patches can still apply.
Jan, I saw you added some printk in microcode.c recently. I removed it as we could export CPU's microcode version in sysfs. If you really needed it I can added it.

[PATCH 1/3] trim microcode driver to make it clean. Old interface is kept.

---
 arch/i386/Kconfig            |    5 
 arch/i386/kernel/microcode.c |  519 +++++++++++++++++++++----------------------
 arch/x86_64/Kconfig          |    5 
 3 files changed, 266 insertions(+), 263 deletions(-)
arch/i386/Kconfig +5 -0
Index: linux-2.6.17-mm3/arch/i386/Kconfig
===================================================================
@@ -415,6 +415,11 @@ config MICROCODE
 	  To compile this driver as a module, choose M here: the
 	  module will be called microcode.
 
+config MICROCODE_OLD_INTERFACE
+	bool
+	depends on MICROCODE
+	default y
+
 config X86_MSR
 	tristate "/dev/cpu/*/msr - Model-specific register support"
 	help
arch/i386/kernel/microcode.c +256 -263

632 lines not on this page · raw message

arch/x86_64/Kconfig +5 -0
Index: linux-2.6.17-mm3/arch/x86_64/Kconfig
===================================================================
@@ -174,6 +174,11 @@ config MICROCODE
 	  If you use modprobe or kmod you may also want to add the line
 	  'alias char-major-10-184 microcode' to your /etc/modules.conf file.
 
+config MICROCODE_OLD_INTERFACE
+	bool
+	depends on MICROCODE
+	default y
+
 config X86_MSR
 	tristate "/dev/cpu/*/msr - Model-specific register support"
 	help
#4 lore raw

I'm not having a problem removing the messages if the current state can be obtained elsewhere.

Looking at the patch I see at least one problem (in more than one place) - before accessing data, you should check that the relevant piece to be read is entirely within range. You should not (as done at least once) rely on copy_from_user() failing - the data may be readable, but out of bounds wrt. the information in the headers (or the header sizes themselves).

Jan

author
#5 lore raw
Quoted 8 lines · Jan Beulich

I'm not having a problem removing the messages if the current state can be obtained elsewhere.

Looking at the patch I see at least one problem (in more than one place) - before accessing data, you should check that the relevant piece to be read is entirely within range. You should not (as done at least once) rely on copy_from_user() failing - the data may be readable, but out of bounds wrt. the information in the headers (or the header sizes themselves).

Can you please give me more details? I had a lot of checks there if the size is incorrect.

Thanks,
Shaohua

#6 lore raw
Quoted 5 lines
Quoted 1 line
Quoted 1 line

Shaohua Li <[email protected]> 29.06.06 11:15 >>>

Quoted 3 lines · Jan Beulich

I'm not having a problem removing the messages if the current state can be obtained elsewhere.

Quoted 7 lines
Quoted 5 lines

Looking at the patch I see at least one problem (in more than one place) - before accessing data, you should check that the relevant piece to be read is entirely within range. You should not (as done at least once) rely on copy_from_user() failing - the data may be readable, but out of bounds wrt. the information in the headers (or the header sizes themselves).

Can you please give me more details? I had a lot of checks there if the size is incorrect.

Ah, yes, I missed the sanity checking function. That seems to take care of what I thought was missing elsewhere.

Jan

author Acked-by Tigran Aivazian
#7 lore raw
In reply to Greg KH
Quoted 35 lines · Greg KH
Quoted 6 lines · Shaohua Li

This is the rewrite of microcode update driver. Changes:
1. trim the code
2. using request_firmware to pull ucode from userspace, so we don't need the application 'microcode_ctl' to assist. We name each ucode file according to CPU's info as intel-ucode/family-model-stepping. In this way we could split ucode file as small one. This has a lot of advantages

Quoted 8 lines

such as selectively update and validate microcode for specific models, better manage microcode file, easily write tools for administerators and so on.
3. add sysfs support. Currently each CPU has two microcode related attributes. One is 'version' which shows current ucode version of CPU. Tools can use the attribute do validation or show CPU ucode status. The other is 'reload' which allows manually reloading ucode.
4. add suspend/resume and CPU hotplug support.

Why not break this up into 4 patches so we can better review them?

Remember, one patch per change please :)

Quoted 7 lines

With the changes, we should put all intel-ucode/xx-xx-xx microcode files into the firmware dir (I had a tool to split previous big data file into small one and later we will release new style data file). The init script should be changed to just loading the driver without unloading for hotplug and suspend/resume (for back compatibility I keep old interface, so old init script also works).

Quoted 7 lines

Previous post is at
http://marc.theaimsgroup.com/?l=linux-kernel&m=114852925121064&w=2
Changes against previous patch:
1. use sys_create_group to add attributes
2. add a fake platform_device for reqeust_firmware as Greg disliked the request_firmware_kobj interface previous patch introduced
3. add a new attribute 'pf' to help tools check if CPU has latest ucode

Quoted 1 line

What does "pf" stand for?

[PATCH 2/3] using request_firmware to load microcode

using request_firmware to pull ucode from userspace, so we don't need the application 'microcode_ctl' to assist. We name each ucode file according to CPU's info as intel-ucode/family-model-stepping. In this way we could split ucode file as small one. This has a lot of advantages such as selectively update and validate microcode for specific models, better manage microcode file, easily write tools for administerators and so on.
with the changes, we should put all intel-ucode/xx-xx-xx microcode files into the firmware dir (I had a tool to split previous big data file into small one and later we will release new style data file). The init script should be changed to just loading the driver without unloading

---
 arch/i386/kernel/microcode.c  |  116 ++++++++++++++++++++++++++++++++++++++++++
 drivers/base/firmware_class.c |    2 
 2 files changed, 117 insertions(+), 1 deletion(-)
arch/i386/kernel/microcode.c +116 -0
Index: linux-2.6.17/arch/i386/kernel/microcode.c
===================================================================
@@ -83,6 +83,9 @@
 #include <linux/spinlock.h>
 #include <linux/mm.h>
 #include <linux/mutex.h>
+#include <linux/cpu.h>
+#include <linux/firmware.h>
+#include <linux/platform_device.h>
 
 #include <asm/msr.h>
 #include <asm/uaccess.h>
@@ -495,6 +498,112 @@ MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
 #define microcode_dev_exit() do { } while(0)
 #endif
 
+static long get_next_ucode_from_buffer(void **mc, void *buf,
+	unsigned long size, long offset)
+{
+	microcode_header_t *mc_header;
+	unsigned long total_size;
+
+	/* No more data */
+	if (offset >= size)
+		return 0;
+	mc_header = (microcode_header_t *)(buf + offset);
+	total_size = get_totalsize(mc_header);
+
+	if ((offset + total_size > size)
+		|| (total_size < DEFAULT_UCODE_TOTALSIZE)) {
+		printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
+		return -EINVAL;
+	}
+
+	*mc = vmalloc(total_size);
+	if (!*mc) {
+		printk(KERN_ERR "microcode: error! Can not allocate memory\n");
+		return -ENOMEM;
+	}
+	memcpy(*mc, buf + offset, total_size);
+	return offset + total_size;
+}
+
+/* fake device for request_firmware */
+static struct platform_device *microcode_pdev;
+
+static int cpu_request_microcode(int cpu)
+{
+	char name[30];
+	struct cpuinfo_x86 *c = cpu_data + cpu;
+	const struct firmware *firmware;
+	void * buf;
+	unsigned long size;
+	long offset = 0;
+	int error;
+	void *mc;
+
+	/* We should bind the task to the CPU */
+	BUG_ON(cpu != raw_smp_processor_id());
+	sprintf(name,"intel-ucode/%02x-%02x-%02x",
+		c->x86, c->x86_model, c->x86_mask);
+	error = request_firmware(&firmware, name, &microcode_pdev->dev);
+	if (error) {
+		pr_debug("ucode data file %s load failed\n", name);
+		return error;
+	}
+	buf = (void *)firmware->data;
+	size = firmware->size;
+	while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
+			> 0) {
+		error = microcode_sanity_check(mc);
+		if (error)
+			break;
+		error = get_maching_microcode(mc, cpu);
+		if (error < 0)
+			break;
+		/*
+		 * It's possible the data file has multiple matching ucode,
+		 * lets keep searching till the latest version
+		 */
+		if (error == 1) {
+			apply_microcode(cpu);
+			error = 0;
+		}
+		vfree(mc);
+	}
+	if (offset > 0)
+		vfree(mc);
+	if (offset < 0)
+		error = offset;
+	release_firmware(firmware);
+
+	return error;
+}
+
+static void microcode_init_cpu(int cpu)
+{
+	cpumask_t old;
+	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+
+	old = current->cpus_allowed;
+
+	set_cpus_allowed(current, cpumask_of_cpu(cpu));
+	mutex_lock(&microcode_mutex);
+	collect_cpu_info(cpu);
+	if (uci->valid)
+		cpu_request_microcode(cpu);
+	mutex_unlock(&microcode_mutex);
+	set_cpus_allowed(current, old);
+}
+
+static void microcode_fini_cpu(int cpu)
+{
+	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+
+	mutex_lock(&microcode_mutex);
+	uci->valid = 0;
+	vfree(uci->mc);
+	uci->mc = NULL;
+	mutex_unlock(&microcode_mutex);
+}
+
 static int __init microcode_init (void)
 {
 	int error;
@@ -502,6 +611,12 @@ static int __init microcode_init (void)
 	error = microcode_dev_init();
 	if (error)
 		return error;
+	microcode_pdev = platform_device_register_simple("microcode", -1,
+							 NULL, 0);
+	if (IS_ERR(microcode_pdev)) {
+		microcode_dev_exit();
+		return PTR_ERR(microcode_pdev);
+	}
 
 	printk(KERN_INFO 
 		"IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <[email protected]>\n");
@@ -511,6 +626,7 @@ static int __init microcode_init (void)
 static void __exit microcode_exit (void)
 {
 	microcode_dev_exit();
+	platform_device_unregister(microcode_pdev);
 }
 
 module_init(microcode_init)
drivers/base/firmware_class.c +1 -1
Index: linux-2.6.17/drivers/base/firmware_class.c
===================================================================
@@ -602,7 +602,7 @@ firmware_class_exit(void)
 	class_unregister(&firmware_class);
 }
 
-module_init(firmware_class_init);
+fs_initcall(firmware_class_init);
 module_exit(firmware_class_exit);
 
 EXPORT_SYMBOL(release_firmware);
author Acked-by Tigran Aivazian
#8 lore raw
In reply to Greg KH
Quoted 35 lines · Greg KH
Quoted 6 lines · Shaohua Li

This is the rewrite of microcode update driver. Changes:
1. trim the code
2. using request_firmware to pull ucode from userspace, so we don't need the application 'microcode_ctl' to assist. We name each ucode file according to CPU's info as intel-ucode/family-model-stepping. In this way we could split ucode file as small one. This has a lot of advantages

Quoted 8 lines

such as selectively update and validate microcode for specific models, better manage microcode file, easily write tools for administerators and so on.
3. add sysfs support. Currently each CPU has two microcode related attributes. One is 'version' which shows current ucode version of CPU. Tools can use the attribute do validation or show CPU ucode status. The other is 'reload' which allows manually reloading ucode.
4. add suspend/resume and CPU hotplug support.

Why not break this up into 4 patches so we can better review them?

Remember, one patch per change please :)

Quoted 7 lines

With the changes, we should put all intel-ucode/xx-xx-xx microcode files into the firmware dir (I had a tool to split previous big data file into small one and later we will release new style data file). The init script should be changed to just loading the driver without unloading for hotplug and suspend/resume (for back compatibility I keep old interface, so old init script also works).

Quoted 7 lines

Previous post is at
http://marc.theaimsgroup.com/?l=linux-kernel&m=114852925121064&w=2
Changes against previous patch:
1. use sys_create_group to add attributes
2. add a fake platform_device for reqeust_firmware as Greg disliked the request_firmware_kobj interface previous patch introduced
3. add a new attribute 'pf' to help tools check if CPU has latest ucode

Quoted 1 line

What does "pf" stand for?

[PATCH 3/3] suspend/resume & hotplug support for microcode driver

add sysfs support. Currently each CPU has three microcode related attributes. One is 'version' which shows current ucode version of CPU. Tools can use the attribute do validation or show CPU ucode status. one is 'reload' which allows manually reloading ucode. Another is 'processor_flags', which exports processor flags, so we can write tools to check if CPU has latest ucode. Also add suspend/resume and CPU hotplug support.

---
 arch/i386/kernel/microcode.c |  146 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 146 insertions(+)
arch/i386/kernel/microcode.c +146 -0
Index: linux-2.6.17/arch/i386/kernel/microcode.c
===================================================================
@@ -604,6 +604,134 @@ static void microcode_fini_cpu(int cpu)
 	mutex_unlock(&microcode_mutex);
 }
 
+static ssize_t reload_store(struct sys_device *dev, const char *buf, size_t sz)
+{
+	struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
+	char *end;
+	unsigned long val = simple_strtoul(buf, &end, 0);
+	int err = 0;
+	int cpu = dev->id;
+
+	if (end == buf)
+		return -EINVAL;
+	if (val == 1) {
+		cpumask_t old;
+
+		old = current->cpus_allowed;
+
+		lock_cpu_hotplug();
+		set_cpus_allowed(current, cpumask_of_cpu(cpu));
+
+		mutex_lock(&microcode_mutex);
+		if (uci->valid)
+			err = cpu_request_microcode(cpu);
+		mutex_unlock(&microcode_mutex);
+		unlock_cpu_hotplug();
+		set_cpus_allowed(current, old);
+	}
+	if (err)
+		return err;
+	return sz;
+}
+
+static ssize_t version_show(struct sys_device *dev, char *buf)
+{
+	struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
+
+	return sprintf(buf, "0x%x\n", uci->rev);
+}
+
+static ssize_t pf_show(struct sys_device *dev, char *buf)
+{
+	struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
+
+	return sprintf(buf, "0x%x\n", uci->pf);
+}
+
+static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
+static SYSDEV_ATTR(version, 0400, version_show, NULL);
+static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
+
+static struct attribute * mc_default_attrs[] = {
+	&attr_reload.attr,
+	&attr_version.attr,
+	&attr_processor_flags.attr,
+	NULL
+};
+
+static struct attribute_group mc_attr_group = {
+	.attrs = mc_default_attrs,
+	.name = "microcode",
+};
+
+static int mc_sysdev_add(struct sys_device *sys_dev)
+{
+	int cpu = sys_dev->id;
+	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+
+	if (!cpu_online(cpu))
+		return 0;
+	pr_debug("Microcode:CPU %d added\n", cpu);
+	memset(uci, 0, sizeof(*uci));
+	sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
+
+	microcode_init_cpu(cpu);
+	return 0;
+}
+
+static int mc_sysdev_remove(struct sys_device *sys_dev)
+{
+	int cpu = sys_dev->id;
+
+	if (!cpu_online(cpu))
+		return 0;
+	pr_debug("Microcode:CPU %d removed\n", cpu);
+	microcode_fini_cpu(cpu);
+	sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
+	return 0;
+}
+
+static int mc_sysdev_resume(struct sys_device *dev)
+{
+	int cpu = dev->id;
+
+	if (!cpu_online(cpu))
+		return 0;
+	pr_debug("Microcode:CPU %d resumed\n", cpu);
+	/* only CPU 0 will apply ucode here */
+	apply_microcode(0);
+	return 0;
+}
+
+static struct sysdev_driver mc_sysdev_driver = {
+	.add = mc_sysdev_add,
+	.remove = mc_sysdev_remove,
+	.resume = mc_sysdev_resume,
+};
+
+static __cpuinit int
+mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
+{
+	unsigned int cpu = (unsigned long)hcpu;
+	struct sys_device *sys_dev;
+
+	sys_dev = get_cpu_sysdev(cpu);
+	switch (action) {
+	case CPU_ONLINE:
+	case CPU_DOWN_FAILED:
+		mc_sysdev_add(sys_dev);
+		break;
+	case CPU_DOWN_PREPARE:
+		mc_sysdev_remove(sys_dev);
+		break;
+	}
+	return NOTIFY_OK;
+}
+
+static struct notifier_block mc_cpu_notifier = {
+	.notifier_call = mc_cpu_callback,
+};
+
 static int __init microcode_init (void)
 {
 	int error;
@@ -618,6 +746,17 @@ static int __init microcode_init (void)
 		return PTR_ERR(microcode_pdev);
 	}
 
+	lock_cpu_hotplug();
+	error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
+	unlock_cpu_hotplug();
+	if (error) {
+		microcode_dev_exit();
+		platform_device_unregister(microcode_pdev);
+		return error;
+	}
+
+	register_cpu_notifier(&mc_cpu_notifier);
+
 	printk(KERN_INFO 
 		"IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <[email protected]>\n");
 	return 0;
@@ -626,6 +765,13 @@ static int __init microcode_init (void)
 static void __exit microcode_exit (void)
 {
 	microcode_dev_exit();
+
+	unregister_cpu_notifier(&mc_cpu_notifier);
+
+	lock_cpu_hotplug();
+	sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
+	unlock_cpu_hotplug();
+
 	platform_device_unregister(microcode_pdev);
 }
 
#9 lore raw
Quoted 36 lines · On Tue, 27 Jun 2006 16:15:09 +0800 Shaohua Li wrote:
Quoted 35 lines · Greg KH
Quoted 6 lines · Shaohua Li

This is the rewrite of microcode update driver. Changes:
1. trim the code
2. using request_firmware to pull ucode from userspace, so we don't need the application 'microcode_ctl' to assist. We name each ucode file according to CPU's info as intel-ucode/family-model-stepping. In this way we could split ucode file as small one. This has a lot of advantages

Quoted 8 lines

such as selectively update and validate microcode for specific models, better manage microcode file, easily write tools for administerators and so on.
3. add sysfs support. Currently each CPU has two microcode related attributes. One is 'version' which shows current ucode version of CPU. Tools can use the attribute do validation or show CPU ucode status. The other is 'reload' which allows manually reloading ucode.
4. add suspend/resume and CPU hotplug support.

Why not break this up into 4 patches so we can better review them?

Remember, one patch per change please :)

Quoted 14 lines

With the changes, we should put all intel-ucode/xx-xx-xx microcode files into the firmware dir (I had a tool to split previous big data file into small one and later we will release new style data file). The init script should be changed to just loading the driver without unloading for hotplug and suspend/resume (for back compatibility I keep old interface, so old init script also works).

Previous post is at
http://marc.theaimsgroup.com/?l=linux-kernel&m=114852925121064&w=2
Changes against previous patch:
1. use sys_create_group to add attributes
2. add a fake platform_device for reqeust_firmware as Greg disliked the request_firmware_kobj interface previous patch introduced
3. add a new attribute 'pf' to help tools check if CPU has latest ucode

Quoted 2 lines
Quoted 1 line

What does "pf" stand for?

[PATCH 3/3] suspend/resume & hotplug support for microcode driver

meta-comment: that line would have made a good Subject: line.

See "2: Subject:" from
http://www.zip.com.au/~akpm/linux/patches/stuff/tpp.txt

---
~Randy

#10 lore raw
In reply to Greg KH
Quoted 3 lines · Greg KH
Quoted 1 line

3. add a new attribute 'pf' to help tools check if CPU has latest ucode

What does "pf" stand for?

processor flags.

Kind regards
Tigran

#11 lore raw
In reply to Shaohua Li
Quoted 3 lines · Shaohua Li

With the changes, we should put all intel-ucode/xx-xx-xx microcode files into the firmware dir (I had a tool to split previous big data file into small one and later we will release new style data file).

BTW, where this tool can be found? At the moment we seem to have only microcode_ctl, and microcode.dat from it distribution is not understood by the driver.