1e47679de3afedd50dc2d9b668c50dd1d65a9140
[firefly-linux-kernel-4.4.55.git] / arch / x86 / kernel / microcode_amd.c
1 /*
2  *  AMD CPU Microcode Update Driver for Linux
3  *  Copyright (C) 2008 Advanced Micro Devices Inc.
4  *
5  *  Author: Peter Oruba <peter.oruba@amd.com>
6  *
7  *  Based on work by:
8  *  Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
9  *
10  *  This driver allows to upgrade microcode on AMD
11  *  family 0x10 and 0x11 processors.
12  *
13  *  Licensed under the terms of the GNU General Public
14  *  License version 2. See file COPYING for details.
15  */
16 #include <linux/firmware.h>
17 #include <linux/pci_ids.h>
18 #include <linux/uaccess.h>
19 #include <linux/vmalloc.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23
24 #include <asm/microcode.h>
25 #include <asm/processor.h>
26 #include <asm/msr.h>
27
28 MODULE_DESCRIPTION("AMD Microcode Update Driver");
29 MODULE_AUTHOR("Peter Oruba");
30 MODULE_LICENSE("GPL v2");
31
32 #define UCODE_MAGIC                0x00414d44
33 #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
34 #define UCODE_UCODE_TYPE           0x00000001
35
36 struct equiv_cpu_entry {
37         u32     installed_cpu;
38         u32     fixed_errata_mask;
39         u32     fixed_errata_compare;
40         u16     equiv_cpu;
41         u16     res;
42 } __attribute__((packed));
43
44 struct microcode_header_amd {
45         u32     data_code;
46         u32     patch_id;
47         u16     mc_patch_data_id;
48         u8      mc_patch_data_len;
49         u8      init_flag;
50         u32     mc_patch_data_checksum;
51         u32     nb_dev_id;
52         u32     sb_dev_id;
53         u16     processor_rev_id;
54         u8      nb_rev_id;
55         u8      sb_rev_id;
56         u8      bios_api_rev;
57         u8      reserved1[3];
58         u32     match_reg[8];
59 } __attribute__((packed));
60
61 struct microcode_amd {
62         struct microcode_header_amd     hdr;
63         unsigned int                    mpb[0];
64 };
65
66 #define UCODE_CONTAINER_SECTION_HDR     8
67 #define UCODE_CONTAINER_HEADER_SIZE     12
68
69 static struct equiv_cpu_entry *equiv_cpu_table;
70
71 static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
72 {
73         struct cpuinfo_x86 *c = &cpu_data(cpu);
74         u32 dummy;
75
76         memset(csig, 0, sizeof(*csig));
77         if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
78                 printk(KERN_WARNING "microcode: CPU%d: AMD CPU family 0x%x not "
79                        "supported\n", cpu, c->x86);
80                 return -1;
81         }
82         rdmsr(MSR_AMD64_PATCH_LEVEL, csig->rev, dummy);
83         printk(KERN_INFO "microcode: CPU%d: patch_level=0x%x\n", cpu, csig->rev);
84         return 0;
85 }
86
87 static int get_matching_microcode(int cpu, void *mc, int rev)
88 {
89         struct microcode_header_amd *mc_header = mc;
90         unsigned int current_cpu_id;
91         u16 equiv_cpu_id = 0;
92         unsigned int i = 0;
93
94         BUG_ON(equiv_cpu_table == NULL);
95         current_cpu_id = cpuid_eax(0x00000001);
96
97         while (equiv_cpu_table[i].installed_cpu != 0) {
98                 if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
99                         equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
100                         break;
101                 }
102                 i++;
103         }
104
105         if (!equiv_cpu_id) {
106                 printk(KERN_WARNING "microcode: CPU%d: cpu revision "
107                        "not listed in equivalent cpu table\n", cpu);
108                 return 0;
109         }
110
111         if (mc_header->processor_rev_id != equiv_cpu_id)
112                 return 0;
113
114         /* ucode might be chipset specific -- currently we don't support this */
115         if (mc_header->nb_dev_id || mc_header->sb_dev_id) {
116                 printk(KERN_ERR "microcode: CPU%d: loading of chipset "
117                        "specific code not yet supported\n", cpu);
118                 return 0;
119         }
120
121         if (mc_header->patch_id <= rev)
122                 return 0;
123
124         return 1;
125 }
126
127 static unsigned int verify_ucode_size(int cpu, const u8 *buf, unsigned int size)
128 {
129         struct cpuinfo_x86 *c = &cpu_data(cpu);
130         unsigned int max_size, actual_size;
131
132 #define F1XH_MPB_MAX_SIZE 2048
133 #define F14H_MPB_MAX_SIZE 1824
134 #define F15H_MPB_MAX_SIZE 4096
135
136         switch (c->x86) {
137         case 0x14:
138                 max_size = F14H_MPB_MAX_SIZE;
139                 break;
140         case 0x15:
141                 max_size = F15H_MPB_MAX_SIZE;
142                 break;
143         default:
144                 max_size = F1XH_MPB_MAX_SIZE;
145                 break;
146         }
147
148         actual_size = buf[4] + (buf[5] << 8);
149
150         if (actual_size > size || actual_size > max_size) {
151                 pr_err("section size mismatch\n");
152                 return 0;
153         }
154
155         return actual_size;
156 }
157
158 static int apply_microcode_amd(int cpu)
159 {
160         u32 rev, dummy;
161         int cpu_num = raw_smp_processor_id();
162         struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
163         struct microcode_amd *mc_amd = uci->mc;
164
165         /* We should bind the task to the CPU */
166         BUG_ON(cpu_num != cpu);
167
168         if (mc_amd == NULL)
169                 return 0;
170
171         wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
172         /* get patch id after patching */
173         rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
174
175         /* check current patch id and patch's id for match */
176         if (rev != mc_amd->hdr.patch_id) {
177                 printk(KERN_ERR "microcode: CPU%d: update failed "
178                        "(for patch_level=0x%x)\n", cpu, mc_amd->hdr.patch_id);
179                 return -1;
180         }
181
182         printk(KERN_INFO "microcode: CPU%d: updated (new patch_level=0x%x)\n",
183                cpu, rev);
184
185         uci->cpu_sig.rev = rev;
186
187         return 0;
188 }
189
190 static int get_ucode_data(void *to, const u8 *from, size_t n)
191 {
192         memcpy(to, from, n);
193         return 0;
194 }
195
196 static void *
197 get_next_ucode(int cpu, const u8 *buf, unsigned int size, unsigned int *mc_size)
198 {
199         unsigned int actual_size = 0;
200         u8 section_hdr[UCODE_CONTAINER_SECTION_HDR];
201         void *mc = NULL;
202
203         if (get_ucode_data(section_hdr, buf, UCODE_CONTAINER_SECTION_HDR))
204                 return NULL;
205
206         if (section_hdr[0] != UCODE_UCODE_TYPE) {
207                 printk(KERN_ERR "microcode: error: invalid type field in "
208                        "container file section header\n");
209                 return NULL;
210         }
211
212         actual_size = verify_ucode_size(cpu, buf, size);
213         if (!actual_size)
214                 return NULL;
215
216         mc = vmalloc(actual_size);
217         if (!mc)
218                 return NULL;
219
220         memset(mc, 0, actual_size);
221         get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR, actual_size);
222         *mc_size = actual_size + UCODE_CONTAINER_SECTION_HDR;
223
224         return mc;
225 }
226
227 static int install_equiv_cpu_table(const u8 *buf)
228 {
229         u8 *container_hdr[UCODE_CONTAINER_HEADER_SIZE];
230         unsigned int *buf_pos = (unsigned int *)container_hdr;
231         unsigned long size;
232
233         if (get_ucode_data(&container_hdr, buf, UCODE_CONTAINER_HEADER_SIZE))
234                 return 0;
235
236         size = buf_pos[2];
237
238         if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
239                 printk(KERN_ERR "microcode: error: invalid type field in "
240                        "container file section header\n");
241                 return 0;
242         }
243
244         equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
245         if (!equiv_cpu_table) {
246                 printk(KERN_ERR "microcode: failed to allocate "
247                        "equivalent CPU table\n");
248                 return 0;
249         }
250
251         buf += UCODE_CONTAINER_HEADER_SIZE;
252         if (get_ucode_data(equiv_cpu_table, buf, size)) {
253                 vfree(equiv_cpu_table);
254                 return 0;
255         }
256
257         return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */
258 }
259
260 static void free_equiv_cpu_table(void)
261 {
262         vfree(equiv_cpu_table);
263         equiv_cpu_table = NULL;
264 }
265
266 static enum ucode_state
267 generic_load_microcode(int cpu, const u8 *data, size_t size)
268 {
269         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
270         const u8 *ucode_ptr = data;
271         void *new_mc = NULL;
272         void *mc;
273         int new_rev = uci->cpu_sig.rev;
274         unsigned int leftover;
275         unsigned long offset;
276         enum ucode_state state = UCODE_OK;
277
278         offset = install_equiv_cpu_table(ucode_ptr);
279         if (!offset) {
280                 printk(KERN_ERR "microcode: failed to create "
281                        "equivalent cpu table\n");
282                 return UCODE_ERROR;
283         }
284
285         ucode_ptr += offset;
286         leftover = size - offset;
287
288         while (leftover) {
289                 unsigned int uninitialized_var(mc_size);
290                 struct microcode_header_amd *mc_header;
291
292                 mc = get_next_ucode(cpu, ucode_ptr, leftover, &mc_size);
293                 if (!mc)
294                         break;
295
296                 mc_header = (struct microcode_header_amd *)mc;
297                 if (get_matching_microcode(cpu, mc, new_rev)) {
298                         vfree(new_mc);
299                         new_rev = mc_header->patch_id;
300                         new_mc  = mc;
301                 } else
302                         vfree(mc);
303
304                 ucode_ptr += mc_size;
305                 leftover  -= mc_size;
306         }
307
308         if (new_mc) {
309                 if (!leftover) {
310                         vfree(uci->mc);
311                         uci->mc = new_mc;
312                         pr_debug("microcode: CPU%d found a matching microcode "
313                                  "update with version 0x%x (current=0x%x)\n",
314                                  cpu, new_rev, uci->cpu_sig.rev);
315                 } else {
316                         vfree(new_mc);
317                         state = UCODE_ERROR;
318                 }
319         } else
320                 state = UCODE_NFOUND;
321
322         free_equiv_cpu_table();
323
324         return state;
325 }
326
327 static enum ucode_state request_microcode_fw(int cpu, struct device *device)
328 {
329         const char *fw_name = "amd-ucode/microcode_amd.bin";
330         const struct firmware *firmware;
331         enum ucode_state ret;
332
333         if (request_firmware(&firmware, fw_name, device)) {
334                 printk(KERN_ERR "microcode: failed to load file %s\n", fw_name);
335                 return UCODE_NFOUND;
336         }
337
338         if (*(u32 *)firmware->data != UCODE_MAGIC) {
339                 printk(KERN_ERR "microcode: invalid UCODE_MAGIC (0x%08x)\n",
340                        *(u32 *)firmware->data);
341                 return UCODE_ERROR;
342         }
343
344         ret = generic_load_microcode(cpu, firmware->data, firmware->size);
345
346         release_firmware(firmware);
347
348         return ret;
349 }
350
351 static enum ucode_state
352 request_microcode_user(int cpu, const void __user *buf, size_t size)
353 {
354         printk(KERN_INFO "microcode: AMD microcode update via "
355                "/dev/cpu/microcode not supported\n");
356         return UCODE_ERROR;
357 }
358
359 static void microcode_fini_cpu_amd(int cpu)
360 {
361         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
362
363         vfree(uci->mc);
364         uci->mc = NULL;
365 }
366
367 static struct microcode_ops microcode_amd_ops = {
368         .request_microcode_user           = request_microcode_user,
369         .request_microcode_fw             = request_microcode_fw,
370         .collect_cpu_info                 = collect_cpu_info_amd,
371         .apply_microcode                  = apply_microcode_amd,
372         .microcode_fini_cpu               = microcode_fini_cpu_amd,
373 };
374
375 struct microcode_ops * __init init_amd_microcode(void)
376 {
377         return &microcode_amd_ops;
378 }