]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/x86/kernel/cpu/microcode/intel_lib.c
Merge branch 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[karo-tx-linux.git] / arch / x86 / kernel / cpu / microcode / intel_lib.c
1 /*
2  *      Intel CPU Microcode Update Driver for Linux
3  *
4  *      Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
5  *                         H Peter Anvin" <hpa@zytor.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  */
25 #include <linux/firmware.h>
26 #include <linux/uaccess.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29
30 #include <asm/microcode_intel.h>
31 #include <asm/processor.h>
32 #include <asm/msr.h>
33
34 static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1,
35                                         unsigned int s2, unsigned int p2)
36 {
37         if (s1 != s2)
38                 return false;
39
40         /* Processor flags are either both 0 ... */
41         if (!p1 && !p2)
42                 return true;
43
44         /* ... or they intersect. */
45         return p1 & p2;
46 }
47
48 int microcode_sanity_check(void *mc, int print_err)
49 {
50         unsigned long total_size, data_size, ext_table_size;
51         struct microcode_header_intel *mc_header = mc;
52         struct extended_sigtable *ext_header = NULL;
53         int sum, orig_sum, ext_sigcount = 0, i;
54         struct extended_signature *ext_sig;
55
56         total_size = get_totalsize(mc_header);
57         data_size = get_datasize(mc_header);
58
59         if (data_size + MC_HEADER_SIZE > total_size) {
60                 if (print_err)
61                         pr_err("error! Bad data size in microcode data file\n");
62                 return -EINVAL;
63         }
64
65         if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
66                 if (print_err)
67                         pr_err("error! Unknown microcode update format\n");
68                 return -EINVAL;
69         }
70         ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
71         if (ext_table_size) {
72                 if ((ext_table_size < EXT_HEADER_SIZE)
73                  || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
74                         if (print_err)
75                                 pr_err("error! Small exttable size in microcode data file\n");
76                         return -EINVAL;
77                 }
78                 ext_header = mc + MC_HEADER_SIZE + data_size;
79                 if (ext_table_size != exttable_size(ext_header)) {
80                         if (print_err)
81                                 pr_err("error! Bad exttable size in microcode data file\n");
82                         return -EFAULT;
83                 }
84                 ext_sigcount = ext_header->count;
85         }
86
87         /* check extended table checksum */
88         if (ext_table_size) {
89                 int ext_table_sum = 0;
90                 int *ext_tablep = (int *)ext_header;
91
92                 i = ext_table_size / DWSIZE;
93                 while (i--)
94                         ext_table_sum += ext_tablep[i];
95                 if (ext_table_sum) {
96                         if (print_err)
97                                 pr_warn("aborting, bad extended signature table checksum\n");
98                         return -EINVAL;
99                 }
100         }
101
102         /* calculate the checksum */
103         orig_sum = 0;
104         i = (MC_HEADER_SIZE + data_size) / DWSIZE;
105         while (i--)
106                 orig_sum += ((int *)mc)[i];
107         if (orig_sum) {
108                 if (print_err)
109                         pr_err("aborting, bad checksum\n");
110                 return -EINVAL;
111         }
112         if (!ext_table_size)
113                 return 0;
114         /* check extended signature checksum */
115         for (i = 0; i < ext_sigcount; i++) {
116                 ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
117                           EXT_SIGNATURE_SIZE * i;
118                 sum = orig_sum
119                         - (mc_header->sig + mc_header->pf + mc_header->cksum)
120                         + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
121                 if (sum) {
122                         if (print_err)
123                                 pr_err("aborting, bad checksum\n");
124                         return -EINVAL;
125                 }
126         }
127         return 0;
128 }
129 EXPORT_SYMBOL_GPL(microcode_sanity_check);
130
131 /*
132  * Returns 1 if update has been found, 0 otherwise.
133  */
134 int find_matching_signature(void *mc, unsigned int csig, int cpf)
135 {
136         struct microcode_header_intel *mc_hdr = mc;
137         struct extended_sigtable *ext_hdr;
138         struct extended_signature *ext_sig;
139         int i;
140
141         if (cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf))
142                 return 1;
143
144         /* Look for ext. headers: */
145         if (get_totalsize(mc_hdr) <= get_datasize(mc_hdr) + MC_HEADER_SIZE)
146                 return 0;
147
148         ext_hdr = mc + get_datasize(mc_hdr) + MC_HEADER_SIZE;
149         ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
150
151         for (i = 0; i < ext_hdr->count; i++) {
152                 if (cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf))
153                         return 1;
154                 ext_sig++;
155         }
156         return 0;
157 }
158
159 /*
160  * Returns 1 if update has been found, 0 otherwise.
161  */
162 int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev)
163 {
164         struct microcode_header_intel *mc_hdr = mc;
165
166         if (mc_hdr->rev <= new_rev)
167                 return 0;
168
169         return find_matching_signature(mc, csig, cpf);
170 }
171 EXPORT_SYMBOL_GPL(has_newer_microcode);