Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[sfrench/cifs-2.6.git] / arch / x86 / kernel / microcode_core.c
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>,
58  *              Serialize updates as required on HT processors due to
59  *              speculative nature of implementation.
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 #include <linux/platform_device.h>
74 #include <linux/miscdevice.h>
75 #include <linux/capability.h>
76 #include <linux/kernel.h>
77 #include <linux/module.h>
78 #include <linux/mutex.h>
79 #include <linux/cpu.h>
80 #include <linux/fs.h>
81 #include <linux/mm.h>
82
83 #include <asm/microcode.h>
84 #include <asm/processor.h>
85
86 MODULE_DESCRIPTION("Microcode Update Driver");
87 MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
88 MODULE_LICENSE("GPL");
89
90 #define MICROCODE_VERSION       "2.00"
91
92 static struct microcode_ops     *microcode_ops;
93
94 /*
95  * Synchronization.
96  *
97  * All non cpu-hotplug-callback call sites use:
98  *
99  * - microcode_mutex to synchronize with each other;
100  * - get/put_online_cpus() to synchronize with
101  *   the cpu-hotplug-callback call sites.
102  *
103  * We guarantee that only a single cpu is being
104  * updated at any particular moment of time.
105  */
106 static DEFINE_MUTEX(microcode_mutex);
107
108 struct ucode_cpu_info           ucode_cpu_info[NR_CPUS];
109 EXPORT_SYMBOL_GPL(ucode_cpu_info);
110
111 /*
112  * Operations that are run on a target cpu:
113  */
114
115 struct cpu_info_ctx {
116         struct cpu_signature    *cpu_sig;
117         int                     err;
118 };
119
120 static void collect_cpu_info_local(void *arg)
121 {
122         struct cpu_info_ctx *ctx = arg;
123
124         ctx->err = microcode_ops->collect_cpu_info(smp_processor_id(),
125                                                    ctx->cpu_sig);
126 }
127
128 static int collect_cpu_info_on_target(int cpu, struct cpu_signature *cpu_sig)
129 {
130         struct cpu_info_ctx ctx = { .cpu_sig = cpu_sig, .err = 0 };
131         int ret;
132
133         ret = smp_call_function_single(cpu, collect_cpu_info_local, &ctx, 1);
134         if (!ret)
135                 ret = ctx.err;
136
137         return ret;
138 }
139
140 static int collect_cpu_info(int cpu)
141 {
142         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
143         int ret;
144
145         memset(uci, 0, sizeof(*uci));
146
147         ret = collect_cpu_info_on_target(cpu, &uci->cpu_sig);
148         if (!ret)
149                 uci->valid = 1;
150
151         return ret;
152 }
153
154 struct apply_microcode_ctx {
155         int err;
156 };
157
158 static void apply_microcode_local(void *arg)
159 {
160         struct apply_microcode_ctx *ctx = arg;
161
162         ctx->err = microcode_ops->apply_microcode(smp_processor_id());
163 }
164
165 static int apply_microcode_on_target(int cpu)
166 {
167         struct apply_microcode_ctx ctx = { .err = 0 };
168         int ret;
169
170         ret = smp_call_function_single(cpu, apply_microcode_local, &ctx, 1);
171         if (!ret)
172                 ret = ctx.err;
173
174         return ret;
175 }
176
177 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
178 static int do_microcode_update(const void __user *buf, size_t size)
179 {
180         int error = 0;
181         int cpu;
182
183         for_each_online_cpu(cpu) {
184                 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
185                 enum ucode_state ustate;
186
187                 if (!uci->valid)
188                         continue;
189
190                 ustate = microcode_ops->request_microcode_user(cpu, buf, size);
191                 if (ustate == UCODE_ERROR) {
192                         error = -1;
193                         break;
194                 } else if (ustate == UCODE_OK)
195                         apply_microcode_on_target(cpu);
196         }
197
198         return error;
199 }
200
201 static int microcode_open(struct inode *unused1, struct file *unused2)
202 {
203         return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
204 }
205
206 static ssize_t microcode_write(struct file *file, const char __user *buf,
207                                size_t len, loff_t *ppos)
208 {
209         ssize_t ret = -EINVAL;
210
211         if ((len >> PAGE_SHIFT) > totalram_pages) {
212                 pr_err("microcode: too much data (max %ld pages)\n", totalram_pages);
213                 return ret;
214         }
215
216         get_online_cpus();
217         mutex_lock(&microcode_mutex);
218
219         if (do_microcode_update(buf, len) == 0)
220                 ret = (ssize_t)len;
221
222         mutex_unlock(&microcode_mutex);
223         put_online_cpus();
224
225         return ret;
226 }
227
228 static const struct file_operations microcode_fops = {
229         .owner                  = THIS_MODULE,
230         .write                  = microcode_write,
231         .open                   = microcode_open,
232 };
233
234 static struct miscdevice microcode_dev = {
235         .minor                  = MICROCODE_MINOR,
236         .name                   = "microcode",
237         .nodename               = "cpu/microcode",
238         .fops                   = &microcode_fops,
239 };
240
241 static int __init microcode_dev_init(void)
242 {
243         int error;
244
245         error = misc_register(&microcode_dev);
246         if (error) {
247                 pr_err("microcode: can't misc_register on minor=%d\n", MICROCODE_MINOR);
248                 return error;
249         }
250
251         return 0;
252 }
253
254 static void microcode_dev_exit(void)
255 {
256         misc_deregister(&microcode_dev);
257 }
258
259 MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
260 #else
261 #define microcode_dev_init()    0
262 #define microcode_dev_exit()    do { } while (0)
263 #endif
264
265 /* fake device for request_firmware */
266 static struct platform_device   *microcode_pdev;
267
268 static int reload_for_cpu(int cpu)
269 {
270         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
271         int err = 0;
272
273         mutex_lock(&microcode_mutex);
274         if (uci->valid) {
275                 enum ucode_state ustate;
276
277                 ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev);
278                 if (ustate == UCODE_OK)
279                         apply_microcode_on_target(cpu);
280                 else
281                         if (ustate == UCODE_ERROR)
282                                 err = -EINVAL;
283         }
284         mutex_unlock(&microcode_mutex);
285
286         return err;
287 }
288
289 static ssize_t reload_store(struct sys_device *dev,
290                             struct sysdev_attribute *attr,
291                             const char *buf, size_t size)
292 {
293         unsigned long val;
294         int cpu = dev->id;
295         int ret = 0;
296         char *end;
297
298         val = simple_strtoul(buf, &end, 0);
299         if (end == buf)
300                 return -EINVAL;
301
302         if (val == 1) {
303                 get_online_cpus();
304                 if (cpu_online(cpu))
305                         ret = reload_for_cpu(cpu);
306                 put_online_cpus();
307         }
308
309         if (!ret)
310                 ret = size;
311
312         return ret;
313 }
314
315 static ssize_t version_show(struct sys_device *dev,
316                         struct sysdev_attribute *attr, char *buf)
317 {
318         struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
319
320         return sprintf(buf, "0x%x\n", uci->cpu_sig.rev);
321 }
322
323 static ssize_t pf_show(struct sys_device *dev,
324                         struct sysdev_attribute *attr, char *buf)
325 {
326         struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
327
328         return sprintf(buf, "0x%x\n", uci->cpu_sig.pf);
329 }
330
331 static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
332 static SYSDEV_ATTR(version, 0400, version_show, NULL);
333 static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
334
335 static struct attribute *mc_default_attrs[] = {
336         &attr_reload.attr,
337         &attr_version.attr,
338         &attr_processor_flags.attr,
339         NULL
340 };
341
342 static struct attribute_group mc_attr_group = {
343         .attrs                  = mc_default_attrs,
344         .name                   = "microcode",
345 };
346
347 static void microcode_fini_cpu(int cpu)
348 {
349         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
350
351         microcode_ops->microcode_fini_cpu(cpu);
352         uci->valid = 0;
353 }
354
355 static enum ucode_state microcode_resume_cpu(int cpu)
356 {
357         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
358
359         if (!uci->mc)
360                 return UCODE_NFOUND;
361
362         pr_debug("microcode: CPU%d updated upon resume\n", cpu);
363         apply_microcode_on_target(cpu);
364
365         return UCODE_OK;
366 }
367
368 static enum ucode_state microcode_init_cpu(int cpu)
369 {
370         enum ucode_state ustate;
371
372         if (collect_cpu_info(cpu))
373                 return UCODE_ERROR;
374
375         /* --dimm. Trigger a delayed update? */
376         if (system_state != SYSTEM_RUNNING)
377                 return UCODE_NFOUND;
378
379         ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev);
380
381         if (ustate == UCODE_OK) {
382                 pr_debug("microcode: CPU%d updated upon init\n", cpu);
383                 apply_microcode_on_target(cpu);
384         }
385
386         return ustate;
387 }
388
389 static enum ucode_state microcode_update_cpu(int cpu)
390 {
391         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
392         enum ucode_state ustate;
393
394         if (uci->valid && uci->mc)
395                 ustate = microcode_resume_cpu(cpu);
396         else
397                 ustate = microcode_init_cpu(cpu);
398
399         return ustate;
400 }
401
402 static int mc_sysdev_add(struct sys_device *sys_dev)
403 {
404         int err, cpu = sys_dev->id;
405
406         if (!cpu_online(cpu))
407                 return 0;
408
409         pr_debug("microcode: CPU%d added\n", cpu);
410
411         err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
412         if (err)
413                 return err;
414
415         if (microcode_init_cpu(cpu) == UCODE_ERROR)
416                 err = -EINVAL;
417
418         return err;
419 }
420
421 static int mc_sysdev_remove(struct sys_device *sys_dev)
422 {
423         int cpu = sys_dev->id;
424
425         if (!cpu_online(cpu))
426                 return 0;
427
428         pr_debug("microcode: CPU%d removed\n", cpu);
429         microcode_fini_cpu(cpu);
430         sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
431         return 0;
432 }
433
434 static int mc_sysdev_resume(struct sys_device *dev)
435 {
436         int cpu = dev->id;
437         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
438
439         if (!cpu_online(cpu))
440                 return 0;
441
442         /*
443          * All non-bootup cpus are still disabled,
444          * so only CPU 0 will apply ucode here.
445          *
446          * Moreover, there can be no concurrent
447          * updates from any other places at this point.
448          */
449         WARN_ON(cpu != 0);
450
451         if (uci->valid && uci->mc)
452                 microcode_ops->apply_microcode(cpu);
453
454         return 0;
455 }
456
457 static struct sysdev_driver mc_sysdev_driver = {
458         .add                    = mc_sysdev_add,
459         .remove                 = mc_sysdev_remove,
460         .resume                 = mc_sysdev_resume,
461 };
462
463 static __cpuinit int
464 mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
465 {
466         unsigned int cpu = (unsigned long)hcpu;
467         struct sys_device *sys_dev;
468
469         sys_dev = get_cpu_sysdev(cpu);
470         switch (action) {
471         case CPU_ONLINE:
472         case CPU_ONLINE_FROZEN:
473                 microcode_update_cpu(cpu);
474         case CPU_DOWN_FAILED:
475         case CPU_DOWN_FAILED_FROZEN:
476                 pr_debug("microcode: CPU%d added\n", cpu);
477                 if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
478                         pr_err("microcode: Failed to create group for CPU%d\n", cpu);
479                 break;
480         case CPU_DOWN_PREPARE:
481         case CPU_DOWN_PREPARE_FROZEN:
482                 /* Suspend is in progress, only remove the interface */
483                 sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
484                 pr_debug("microcode: CPU%d removed\n", cpu);
485                 break;
486         case CPU_DEAD:
487         case CPU_UP_CANCELED_FROZEN:
488                 /* The CPU refused to come up during a system resume */
489                 microcode_fini_cpu(cpu);
490                 break;
491         }
492         return NOTIFY_OK;
493 }
494
495 static struct notifier_block __refdata mc_cpu_notifier = {
496         .notifier_call  = mc_cpu_callback,
497 };
498
499 static int __init microcode_init(void)
500 {
501         struct cpuinfo_x86 *c = &cpu_data(0);
502         int error;
503
504         if (c->x86_vendor == X86_VENDOR_INTEL)
505                 microcode_ops = init_intel_microcode();
506         else if (c->x86_vendor == X86_VENDOR_AMD)
507                 microcode_ops = init_amd_microcode();
508
509         if (!microcode_ops) {
510                 pr_err("microcode: no support for this CPU vendor\n");
511                 return -ENODEV;
512         }
513
514         microcode_pdev = platform_device_register_simple("microcode", -1,
515                                                          NULL, 0);
516         if (IS_ERR(microcode_pdev)) {
517                 microcode_dev_exit();
518                 return PTR_ERR(microcode_pdev);
519         }
520
521         if (microcode_ops->init)
522                 microcode_ops->init(&microcode_pdev->dev);
523
524         get_online_cpus();
525         mutex_lock(&microcode_mutex);
526
527         error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
528
529         mutex_unlock(&microcode_mutex);
530         put_online_cpus();
531
532         if (error) {
533                 platform_device_unregister(microcode_pdev);
534                 return error;
535         }
536
537         error = microcode_dev_init();
538         if (error)
539                 return error;
540
541         register_hotcpu_notifier(&mc_cpu_notifier);
542
543         pr_info("Microcode Update Driver: v" MICROCODE_VERSION
544                " <tigran@aivazian.fsnet.co.uk>,"
545                " Peter Oruba\n");
546
547         return 0;
548 }
549 module_init(microcode_init);
550
551 static void __exit microcode_exit(void)
552 {
553         microcode_dev_exit();
554
555         unregister_hotcpu_notifier(&mc_cpu_notifier);
556
557         get_online_cpus();
558         mutex_lock(&microcode_mutex);
559
560         sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
561
562         mutex_unlock(&microcode_mutex);
563         put_online_cpus();
564
565         platform_device_unregister(microcode_pdev);
566
567         if (microcode_ops->fini)
568                 microcode_ops->fini();
569
570         microcode_ops = NULL;
571
572         pr_info("Microcode Update Driver: v" MICROCODE_VERSION " removed.\n");
573 }
574 module_exit(microcode_exit);