Commit | Line | Data |
---|---|---|
3e135d88 PO |
1 | /* |
2 | * Intel CPU Microcode Update Driver for Linux | |
3 | * | |
4 | * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk> | |
5 | * 2006 Shaohua Li <shaohua.li@intel.com> | |
6 | * | |
7 | * This driver allows to upgrade microcode on Intel processors | |
8 | * belonging to IA-32 family - PentiumPro, Pentium II, | |
9 | * Pentium III, Xeon, Pentium 4, etc. | |
10 | * | |
11 | * Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture | |
12 | * Software Developer's Manual | |
13 | * Order Number 253668 or free download from: | |
14 | * | |
15 | * http://developer.intel.com/design/pentium4/manuals/253668.htm | |
16 | * | |
17 | * For more information, go to http://www.urbanmyth.org/microcode | |
18 | * | |
19 | * This program is free software; you can redistribute it and/or | |
20 | * modify it under the terms of the GNU General Public License | |
21 | * as published by the Free Software Foundation; either version | |
22 | * 2 of the License, or (at your option) any later version. | |
23 | * | |
24 | * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com> | |
25 | * Initial release. | |
26 | * 1.01 18 Feb 2000, Tigran Aivazian <tigran@sco.com> | |
27 | * Added read() support + cleanups. | |
28 | * 1.02 21 Feb 2000, Tigran Aivazian <tigran@sco.com> | |
29 | * Added 'device trimming' support. open(O_WRONLY) zeroes | |
30 | * and frees the saved copy of applied microcode. | |
31 | * 1.03 29 Feb 2000, Tigran Aivazian <tigran@sco.com> | |
32 | * Made to use devfs (/dev/cpu/microcode) + cleanups. | |
33 | * 1.04 06 Jun 2000, Simon Trimmer <simon@veritas.com> | |
34 | * Added misc device support (now uses both devfs and misc). | |
35 | * Added MICROCODE_IOCFREE ioctl to clear memory. | |
36 | * 1.05 09 Jun 2000, Simon Trimmer <simon@veritas.com> | |
37 | * Messages for error cases (non Intel & no suitable microcode). | |
38 | * 1.06 03 Aug 2000, Tigran Aivazian <tigran@veritas.com> | |
39 | * Removed ->release(). Removed exclusive open and status bitmap. | |
40 | * Added microcode_rwsem to serialize read()/write()/ioctl(). | |
41 | * Removed global kernel lock usage. | |
42 | * 1.07 07 Sep 2000, Tigran Aivazian <tigran@veritas.com> | |
43 | * Write 0 to 0x8B msr and then cpuid before reading revision, | |
44 | * so that it works even if there were no update done by the | |
45 | * BIOS. Otherwise, reading from 0x8B gives junk (which happened | |
46 | * to be 0 on my machine which is why it worked even when I | |
47 | * disabled update by the BIOS) | |
48 | * Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix. | |
49 | * 1.08 11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and | |
50 | * Tigran Aivazian <tigran@veritas.com> | |
51 | * Intel Pentium 4 processor support and bugfixes. | |
52 | * 1.09 30 Oct 2001, Tigran Aivazian <tigran@veritas.com> | |
53 | * Bugfix for HT (Hyper-Threading) enabled processors | |
54 | * whereby processor resources are shared by all logical processors | |
55 | * in a single CPU package. | |
56 | * 1.10 28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and | |
57 | * Tigran Aivazian <tigran@veritas.com>, | |
d33dcb9e PO |
58 | * Serialize updates as required on HT processors due to |
59 | * speculative nature of implementation. | |
3e135d88 PO |
60 | * 1.11 22 Mar 2002 Tigran Aivazian <tigran@veritas.com> |
61 | * Fix the panic when writing zero-length microcode chunk. | |
62 | * 1.12 29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>, | |
63 | * Jun Nakajima <jun.nakajima@intel.com> | |
64 | * Support for the microcode updates in the new format. | |
65 | * 1.13 10 Oct 2003 Tigran Aivazian <tigran@veritas.com> | |
66 | * Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl | |
67 | * because we no longer hold a copy of applied microcode | |
68 | * in kernel memory. | |
69 | * 1.14 25 Jun 2004 Tigran Aivazian <tigran@veritas.com> | |
70 | * Fix sigmatch() macro to handle old CPUs with pf == 0. | |
71 | * Thanks to Stuart Swales for pointing out this bug. | |
72 | */ | |
73 | ||
d45de409 | 74 | /* #define DEBUG pr_debug */ |
3e135d88 PO |
75 | #include <linux/capability.h> |
76 | #include <linux/kernel.h> | |
77 | #include <linux/init.h> | |
78 | #include <linux/sched.h> | |
79 | #include <linux/smp_lock.h> | |
80 | #include <linux/cpumask.h> | |
81 | #include <linux/module.h> | |
82 | #include <linux/slab.h> | |
83 | #include <linux/vmalloc.h> | |
84 | #include <linux/miscdevice.h> | |
85 | #include <linux/spinlock.h> | |
86 | #include <linux/mm.h> | |
87 | #include <linux/fs.h> | |
88 | #include <linux/mutex.h> | |
89 | #include <linux/cpu.h> | |
90 | #include <linux/firmware.h> | |
91 | #include <linux/platform_device.h> | |
92 | ||
93 | #include <asm/msr.h> | |
94 | #include <asm/uaccess.h> | |
95 | #include <asm/processor.h> | |
96 | #include <asm/microcode.h> | |
97 | ||
98 | MODULE_DESCRIPTION("Microcode Update Driver"); | |
99 | MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>"); | |
100 | MODULE_LICENSE("GPL"); | |
101 | ||
8d86f390 | 102 | #define MICROCODE_VERSION "2.00" |
3e135d88 | 103 | |
8d86f390 | 104 | struct microcode_ops *microcode_ops; |
3e135d88 | 105 | |
8d86f390 | 106 | /* no concurrent ->write()s are allowed on /dev/cpu/microcode */ |
d45de409 | 107 | static DEFINE_MUTEX(microcode_mutex); |
3e135d88 | 108 | |
224e946b | 109 | struct ucode_cpu_info ucode_cpu_info[NR_CPUS]; |
8d86f390 | 110 | EXPORT_SYMBOL_GPL(ucode_cpu_info); |
3e135d88 PO |
111 | |
112 | #ifdef CONFIG_MICROCODE_OLD_INTERFACE | |
224e946b | 113 | void __user *user_buffer; /* user area microcode data buffer */ |
8d86f390 | 114 | EXPORT_SYMBOL_GPL(user_buffer); |
224e946b | 115 | unsigned int user_buffer_size; /* it's size */ |
8d86f390 | 116 | EXPORT_SYMBOL_GPL(user_buffer_size); |
3e135d88 | 117 | |
d33dcb9e | 118 | static int do_microcode_update(void) |
3e135d88 PO |
119 | { |
120 | long cursor = 0; | |
121 | int error = 0; | |
122 | void *new_mc = NULL; | |
123 | int cpu; | |
124 | cpumask_t old; | |
3e135d88 PO |
125 | |
126 | old = current->cpus_allowed; | |
127 | ||
8d86f390 | 128 | while ((cursor = microcode_ops->get_next_ucode(&new_mc, cursor)) > 0) { |
636a3178 PO |
129 | if (microcode_ops->microcode_sanity_check != NULL) |
130 | error = microcode_ops->microcode_sanity_check(new_mc); | |
3e135d88 PO |
131 | if (error) |
132 | goto out; | |
133 | /* | |
134 | * It's possible the data file has multiple matching ucode, | |
135 | * lets keep searching till the latest version | |
136 | */ | |
137 | for_each_online_cpu(cpu) { | |
138 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; | |
139 | ||
140 | if (!uci->valid) | |
141 | continue; | |
0bc3cc03 | 142 | set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu)); |
8d86f390 PO |
143 | error = microcode_ops->get_matching_microcode(new_mc, |
144 | cpu); | |
3e135d88 PO |
145 | if (error < 0) |
146 | goto out; | |
147 | if (error == 1) | |
8d86f390 | 148 | microcode_ops->apply_microcode(cpu); |
3e135d88 PO |
149 | } |
150 | vfree(new_mc); | |
151 | } | |
152 | out: | |
153 | if (cursor > 0) | |
154 | vfree(new_mc); | |
155 | if (cursor < 0) | |
156 | error = cursor; | |
157 | set_cpus_allowed_ptr(current, &old); | |
158 | return error; | |
159 | } | |
160 | ||
d33dcb9e | 161 | static int microcode_open(struct inode *unused1, struct file *unused2) |
3e135d88 PO |
162 | { |
163 | cycle_kernel_lock(); | |
164 | return capable(CAP_SYS_RAWIO) ? 0 : -EPERM; | |
165 | } | |
166 | ||
d33dcb9e PO |
167 | static ssize_t microcode_write(struct file *file, const char __user *buf, |
168 | size_t len, loff_t *ppos) | |
3e135d88 PO |
169 | { |
170 | ssize_t ret; | |
171 | ||
172 | if ((len >> PAGE_SHIFT) > num_physpages) { | |
d33dcb9e PO |
173 | printk(KERN_ERR "microcode: too much data (max %ld pages)\n", |
174 | num_physpages); | |
3e135d88 PO |
175 | return -EINVAL; |
176 | } | |
177 | ||
178 | get_online_cpus(); | |
179 | mutex_lock(µcode_mutex); | |
180 | ||
181 | user_buffer = (void __user *) buf; | |
182 | user_buffer_size = (int) len; | |
183 | ||
184 | ret = do_microcode_update(); | |
185 | if (!ret) | |
186 | ret = (ssize_t)len; | |
187 | ||
188 | mutex_unlock(µcode_mutex); | |
189 | put_online_cpus(); | |
190 | ||
191 | return ret; | |
192 | } | |
193 | ||
194 | static const struct file_operations microcode_fops = { | |
195 | .owner = THIS_MODULE, | |
196 | .write = microcode_write, | |
197 | .open = microcode_open, | |
198 | }; | |
199 | ||
200 | static struct miscdevice microcode_dev = { | |
201 | .minor = MICROCODE_MINOR, | |
202 | .name = "microcode", | |
203 | .fops = µcode_fops, | |
204 | }; | |
205 | ||
d33dcb9e | 206 | static int __init microcode_dev_init(void) |
3e135d88 PO |
207 | { |
208 | int error; | |
209 | ||
210 | error = misc_register(µcode_dev); | |
211 | if (error) { | |
212 | printk(KERN_ERR | |
213 | "microcode: can't misc_register on minor=%d\n", | |
214 | MICROCODE_MINOR); | |
215 | return error; | |
216 | } | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
d33dcb9e | 221 | static void microcode_dev_exit(void) |
3e135d88 PO |
222 | { |
223 | misc_deregister(µcode_dev); | |
224 | } | |
225 | ||
226 | MODULE_ALIAS_MISCDEV(MICROCODE_MINOR); | |
227 | #else | |
228 | #define microcode_dev_init() 0 | |
d33dcb9e | 229 | #define microcode_dev_exit() do { } while (0) |
3e135d88 PO |
230 | #endif |
231 | ||
232 | /* fake device for request_firmware */ | |
224e946b | 233 | struct platform_device *microcode_pdev; |
8d86f390 | 234 | EXPORT_SYMBOL_GPL(microcode_pdev); |
3e135d88 | 235 | |
3e135d88 PO |
236 | static ssize_t reload_store(struct sys_device *dev, |
237 | struct sysdev_attribute *attr, | |
238 | const char *buf, size_t sz) | |
239 | { | |
240 | struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; | |
241 | char *end; | |
242 | unsigned long val = simple_strtoul(buf, &end, 0); | |
243 | int err = 0; | |
244 | int cpu = dev->id; | |
245 | ||
246 | if (end == buf) | |
247 | return -EINVAL; | |
248 | if (val == 1) { | |
0bc3cc03 | 249 | cpumask_t old = current->cpus_allowed; |
3e135d88 PO |
250 | |
251 | get_online_cpus(); | |
d45de409 DA |
252 | if (cpu_online(cpu)) { |
253 | set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu)); | |
254 | mutex_lock(µcode_mutex); | |
255 | if (uci->valid) | |
256 | err = microcode_ops->cpu_request_microcode(cpu); | |
257 | mutex_unlock(µcode_mutex); | |
258 | set_cpus_allowed_ptr(current, &old); | |
259 | } | |
3e135d88 | 260 | put_online_cpus(); |
3e135d88 PO |
261 | } |
262 | if (err) | |
263 | return err; | |
264 | return sz; | |
265 | } | |
266 | ||
267 | static ssize_t version_show(struct sys_device *dev, | |
268 | struct sysdev_attribute *attr, char *buf) | |
269 | { | |
270 | struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; | |
271 | ||
d45de409 | 272 | return sprintf(buf, "0x%x\n", uci->cpu_sig.rev); |
3e135d88 PO |
273 | } |
274 | ||
275 | static ssize_t pf_show(struct sys_device *dev, | |
276 | struct sysdev_attribute *attr, char *buf) | |
277 | { | |
278 | struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; | |
279 | ||
d45de409 | 280 | return sprintf(buf, "0x%x\n", uci->cpu_sig.pf); |
3e135d88 PO |
281 | } |
282 | ||
283 | static SYSDEV_ATTR(reload, 0200, NULL, reload_store); | |
284 | static SYSDEV_ATTR(version, 0400, version_show, NULL); | |
285 | static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL); | |
286 | ||
287 | static struct attribute *mc_default_attrs[] = { | |
288 | &attr_reload.attr, | |
289 | &attr_version.attr, | |
290 | &attr_processor_flags.attr, | |
291 | NULL | |
292 | }; | |
293 | ||
294 | static struct attribute_group mc_attr_group = { | |
295 | .attrs = mc_default_attrs, | |
296 | .name = "microcode", | |
297 | }; | |
298 | ||
d45de409 DA |
299 | static void microcode_fini_cpu(int cpu) |
300 | { | |
301 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; | |
302 | ||
303 | mutex_lock(µcode_mutex); | |
304 | microcode_ops->microcode_fini_cpu(cpu); | |
305 | uci->valid = 0; | |
306 | mutex_unlock(µcode_mutex); | |
307 | } | |
308 | ||
309 | static void collect_cpu_info(int cpu) | |
310 | { | |
311 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; | |
312 | ||
313 | memset(uci, 0, sizeof(*uci)); | |
314 | if (!microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig)) | |
315 | uci->valid = 1; | |
316 | } | |
317 | ||
318 | static void microcode_resume_cpu(int cpu) | |
319 | { | |
320 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; | |
321 | struct cpu_signature nsig; | |
322 | ||
323 | pr_debug("microcode: CPU%d resumed\n", cpu); | |
324 | ||
325 | if (!uci->mc.valid_mc) | |
326 | return; | |
327 | ||
328 | /* | |
329 | * Let's verify that the 'cached' ucode does belong | |
330 | * to this cpu (a bit of paranoia): | |
331 | */ | |
332 | if (microcode_ops->collect_cpu_info(cpu, &nsig)) { | |
333 | microcode_fini_cpu(cpu); | |
334 | return; | |
335 | } | |
336 | ||
337 | if (memcmp(&nsig, &uci->cpu_sig, sizeof(nsig))) { | |
338 | microcode_fini_cpu(cpu); | |
339 | /* Should we look for a new ucode here? */ | |
340 | return; | |
341 | } | |
342 | ||
343 | microcode_ops->apply_microcode(cpu); | |
344 | } | |
345 | ||
346 | void microcode_update_cpu(int cpu) | |
347 | { | |
348 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; | |
349 | ||
350 | /* We should bind the task to the CPU */ | |
351 | BUG_ON(raw_smp_processor_id() != cpu); | |
352 | ||
353 | mutex_lock(µcode_mutex); | |
354 | /* | |
355 | * Check if the system resume is in progress (uci->valid != NULL), | |
356 | * otherwise just request a firmware: | |
357 | */ | |
358 | if (uci->valid) { | |
359 | microcode_resume_cpu(cpu); | |
360 | } else { | |
361 | collect_cpu_info(cpu); | |
362 | if (uci->valid && system_state == SYSTEM_RUNNING) | |
363 | microcode_ops->cpu_request_microcode(cpu); | |
364 | } | |
365 | mutex_unlock(µcode_mutex); | |
366 | } | |
367 | ||
368 | static void microcode_init_cpu(int cpu) | |
369 | { | |
370 | cpumask_t old = current->cpus_allowed; | |
371 | ||
372 | set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu)); | |
373 | microcode_update_cpu(cpu); | |
374 | set_cpus_allowed_ptr(current, &old); | |
375 | } | |
376 | ||
377 | static int mc_sysdev_add(struct sys_device *sys_dev) | |
3e135d88 PO |
378 | { |
379 | int err, cpu = sys_dev->id; | |
380 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; | |
381 | ||
382 | if (!cpu_online(cpu)) | |
383 | return 0; | |
384 | ||
385 | pr_debug("microcode: CPU%d added\n", cpu); | |
386 | memset(uci, 0, sizeof(*uci)); | |
387 | ||
388 | err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group); | |
389 | if (err) | |
390 | return err; | |
391 | ||
d45de409 | 392 | microcode_init_cpu(cpu); |
3e135d88 PO |
393 | return 0; |
394 | } | |
395 | ||
3e135d88 PO |
396 | static int mc_sysdev_remove(struct sys_device *sys_dev) |
397 | { | |
398 | int cpu = sys_dev->id; | |
399 | ||
400 | if (!cpu_online(cpu)) | |
401 | return 0; | |
402 | ||
403 | pr_debug("microcode: CPU%d removed\n", cpu); | |
d45de409 | 404 | microcode_fini_cpu(cpu); |
3e135d88 PO |
405 | sysfs_remove_group(&sys_dev->kobj, &mc_attr_group); |
406 | return 0; | |
407 | } | |
408 | ||
409 | static int mc_sysdev_resume(struct sys_device *dev) | |
410 | { | |
411 | int cpu = dev->id; | |
412 | ||
413 | if (!cpu_online(cpu)) | |
414 | return 0; | |
415 | pr_debug("microcode: CPU%d resumed\n", cpu); | |
416 | /* only CPU 0 will apply ucode here */ | |
8d86f390 | 417 | microcode_ops->apply_microcode(0); |
3e135d88 PO |
418 | return 0; |
419 | } | |
420 | ||
421 | static struct sysdev_driver mc_sysdev_driver = { | |
422 | .add = mc_sysdev_add, | |
423 | .remove = mc_sysdev_remove, | |
424 | .resume = mc_sysdev_resume, | |
425 | }; | |
426 | ||
427 | static __cpuinit int | |
428 | mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu) | |
429 | { | |
430 | unsigned int cpu = (unsigned long)hcpu; | |
431 | struct sys_device *sys_dev; | |
432 | ||
433 | sys_dev = get_cpu_sysdev(cpu); | |
434 | switch (action) { | |
3e135d88 | 435 | case CPU_ONLINE: |
3e135d88 | 436 | case CPU_ONLINE_FROZEN: |
d45de409 DA |
437 | microcode_init_cpu(cpu); |
438 | case CPU_DOWN_FAILED: | |
3e135d88 | 439 | case CPU_DOWN_FAILED_FROZEN: |
d45de409 | 440 | pr_debug("microcode: CPU%d added\n", cpu); |
3e135d88 PO |
441 | if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group)) |
442 | printk(KERN_ERR "microcode: Failed to create the sysfs " | |
443 | "group for CPU%d\n", cpu); | |
444 | break; | |
445 | case CPU_DOWN_PREPARE: | |
3e135d88 PO |
446 | case CPU_DOWN_PREPARE_FROZEN: |
447 | /* Suspend is in progress, only remove the interface */ | |
448 | sysfs_remove_group(&sys_dev->kobj, &mc_attr_group); | |
d45de409 DA |
449 | pr_debug("microcode: CPU%d removed\n", cpu); |
450 | break; | |
451 | case CPU_DEAD: | |
452 | case CPU_UP_CANCELED_FROZEN: | |
453 | /* The CPU refused to come up during a system resume */ | |
454 | microcode_fini_cpu(cpu); | |
3e135d88 PO |
455 | break; |
456 | } | |
457 | return NOTIFY_OK; | |
458 | } | |
459 | ||
460 | static struct notifier_block __refdata mc_cpu_notifier = { | |
461 | .notifier_call = mc_cpu_callback, | |
462 | }; | |
463 | ||
45b1e23e | 464 | int microcode_init(void *opaque, struct module *module) |
3e135d88 | 465 | { |
8d86f390 | 466 | struct microcode_ops *ops = (struct microcode_ops *)opaque; |
3e135d88 PO |
467 | int error; |
468 | ||
8d86f390 PO |
469 | if (microcode_ops) { |
470 | printk(KERN_ERR "microcode: already loaded the other module\n"); | |
471 | return -EEXIST; | |
472 | } | |
473 | ||
474 | microcode_ops = ops; | |
3e135d88 PO |
475 | |
476 | error = microcode_dev_init(); | |
477 | if (error) | |
478 | return error; | |
479 | microcode_pdev = platform_device_register_simple("microcode", -1, | |
480 | NULL, 0); | |
481 | if (IS_ERR(microcode_pdev)) { | |
482 | microcode_dev_exit(); | |
483 | return PTR_ERR(microcode_pdev); | |
484 | } | |
485 | ||
486 | get_online_cpus(); | |
487 | error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver); | |
488 | put_online_cpus(); | |
489 | if (error) { | |
490 | microcode_dev_exit(); | |
491 | platform_device_unregister(microcode_pdev); | |
492 | return error; | |
493 | } | |
494 | ||
495 | register_hotcpu_notifier(&mc_cpu_notifier); | |
8d86f390 PO |
496 | |
497 | printk(KERN_INFO | |
498 | "Microcode Update Driver: v" MICROCODE_VERSION | |
499 | " <tigran@aivazian.fsnet.co.uk>" | |
500 | " <peter.oruba@amd.com>\n"); | |
501 | ||
3e135d88 PO |
502 | return 0; |
503 | } | |
8d86f390 | 504 | EXPORT_SYMBOL_GPL(microcode_init); |
3e135d88 | 505 | |
d33dcb9e | 506 | void __exit microcode_exit(void) |
3e135d88 PO |
507 | { |
508 | microcode_dev_exit(); | |
509 | ||
510 | unregister_hotcpu_notifier(&mc_cpu_notifier); | |
511 | ||
512 | get_online_cpus(); | |
513 | sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver); | |
514 | put_online_cpus(); | |
515 | ||
516 | platform_device_unregister(microcode_pdev); | |
3e135d88 | 517 | |
8d86f390 PO |
518 | microcode_ops = NULL; |
519 | ||
520 | printk(KERN_INFO | |
521 | "Microcode Update Driver: v" MICROCODE_VERSION " removed.\n"); | |
522 | } | |
523 | EXPORT_SYMBOL_GPL(microcode_exit); |