]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/x86/kernel/cpu/microcode/intel_lib.c
Merge tag 'for-4.2' of git://git.infradead.org/battery-2.6
[mirror_ubuntu-focal-kernel.git] / arch / x86 / kernel / cpu / microcode / intel_lib.c
CommitLineData
e666dfa2
FY
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
6b2d469f
BP
34static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1,
35 unsigned int s2, unsigned int p2)
e666dfa2 36{
6b2d469f
BP
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;
e666dfa2
FY
46}
47
e666dfa2
FY
48int 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}
129EXPORT_SYMBOL_GPL(microcode_sanity_check);
130
131/*
e3d8f674 132 * Returns 1 if update has been found, 0 otherwise.
e666dfa2 133 */
e774eaa9 134int find_matching_signature(void *mc, unsigned int csig, int cpf)
e666dfa2 135{
9e5aed83
BP
136 struct microcode_header_intel *mc_hdr = mc;
137 struct extended_sigtable *ext_hdr;
e666dfa2 138 struct extended_signature *ext_sig;
9e5aed83 139 int i;
e666dfa2 140
9e5aed83 141 if (cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf))
e666dfa2
FY
142 return 1;
143
144 /* Look for ext. headers: */
9e5aed83 145 if (get_totalsize(mc_hdr) <= get_datasize(mc_hdr) + MC_HEADER_SIZE)
e666dfa2
FY
146 return 0;
147
9e5aed83
BP
148 ext_hdr = mc + get_datasize(mc_hdr) + MC_HEADER_SIZE;
149 ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
e666dfa2 150
9e5aed83 151 for (i = 0; i < ext_hdr->count; i++) {
6b2d469f 152 if (cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf))
e666dfa2
FY
153 return 1;
154 ext_sig++;
155 }
156 return 0;
157}
158
159/*
e3d8f674 160 * Returns 1 if update has been found, 0 otherwise.
e666dfa2 161 */
8de3eafc 162int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev)
e666dfa2 163{
e3d8f674 164 struct microcode_header_intel *mc_hdr = mc;
e666dfa2 165
a1a32d29 166 if (mc_hdr->rev <= new_rev)
e666dfa2
FY
167 return 0;
168
e774eaa9 169 return find_matching_signature(mc, csig, cpf);
e666dfa2 170}
8de3eafc 171EXPORT_SYMBOL_GPL(has_newer_microcode);