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