blob: adbcef4f63338fc34fc7c2b23fd96d4eb53aeca7 [file] [log] [blame]
Peter Oruba80cc9f12008-07-28 18:44:22 +02001/*
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 *
Andreas Herrmann2a3282a72008-12-16 19:08:53 +010013 * Licensed under the terms of the GNU General Public
Peter Oruba80cc9f12008-07-28 18:44:22 +020014 * License version 2. See file COPYING for details.
Ingo Molnar4bae1962009-03-11 11:19:46 +010015 */
Joe Perchesf58e1f52009-12-08 22:30:50 -080016
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Ingo Molnar4bae1962009-03-11 11:19:46 +010019#include <linux/firmware.h>
Peter Oruba80cc9f12008-07-28 18:44:22 +020020#include <linux/pci_ids.h>
Andreas Herrmannbe957762008-12-16 19:11:23 +010021#include <linux/uaccess.h>
Ingo Molnar4bae1962009-03-11 11:19:46 +010022#include <linux/vmalloc.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
Ingo Molnar4bae1962009-03-11 11:19:46 +010025#include <linux/pci.h>
Peter Oruba80cc9f12008-07-28 18:44:22 +020026
Peter Oruba80cc9f12008-07-28 18:44:22 +020027#include <asm/microcode.h>
Ingo Molnar4bae1962009-03-11 11:19:46 +010028#include <asm/processor.h>
29#include <asm/msr.h>
Peter Oruba80cc9f12008-07-28 18:44:22 +020030
31MODULE_DESCRIPTION("AMD Microcode Update Driver");
Peter Oruba3c522042008-10-17 15:30:38 +020032MODULE_AUTHOR("Peter Oruba");
Ingo Molnar5d7b6052008-07-29 10:07:36 +020033MODULE_LICENSE("GPL v2");
Peter Oruba80cc9f12008-07-28 18:44:22 +020034
35#define UCODE_MAGIC 0x00414d44
36#define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
37#define UCODE_UCODE_TYPE 0x00000001
38
Dmitry Adamushko18dbc912008-09-23 12:08:44 +020039struct equiv_cpu_entry {
Andreas Herrmann5549b942008-12-16 19:21:30 +010040 u32 installed_cpu;
41 u32 fixed_errata_mask;
42 u32 fixed_errata_compare;
43 u16 equiv_cpu;
44 u16 res;
45} __attribute__((packed));
Dmitry Adamushko18dbc912008-09-23 12:08:44 +020046
47struct microcode_header_amd {
Andreas Herrmann5549b942008-12-16 19:21:30 +010048 u32 data_code;
49 u32 patch_id;
50 u16 mc_patch_data_id;
51 u8 mc_patch_data_len;
52 u8 init_flag;
53 u32 mc_patch_data_checksum;
54 u32 nb_dev_id;
55 u32 sb_dev_id;
56 u16 processor_rev_id;
57 u8 nb_rev_id;
58 u8 sb_rev_id;
59 u8 bios_api_rev;
60 u8 reserved1[3];
61 u32 match_reg[8];
62} __attribute__((packed));
Dmitry Adamushko18dbc912008-09-23 12:08:44 +020063
64struct microcode_amd {
Ingo Molnar4bae1962009-03-11 11:19:46 +010065 struct microcode_header_amd hdr;
66 unsigned int mpb[0];
Dmitry Adamushko18dbc912008-09-23 12:08:44 +020067};
68
Andreas Herrmann6cc9b6d2008-12-16 19:17:45 +010069#define UCODE_MAX_SIZE 2048
70#define UCODE_CONTAINER_SECTION_HDR 8
71#define UCODE_CONTAINER_HEADER_SIZE 12
Peter Oruba80cc9f12008-07-28 18:44:22 +020072
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +020073static struct equiv_cpu_entry *equiv_cpu_table;
Peter Oruba80cc9f12008-07-28 18:44:22 +020074
Dmitry Adamushkod45de402008-08-20 00:22:26 +020075static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
Peter Oruba80cc9f12008-07-28 18:44:22 +020076{
Andreas Herrmann3b2e3d82010-01-22 21:34:56 +010077 struct cpuinfo_x86 *c = &cpu_data(cpu);
Andreas Herrmann29d08872008-12-16 19:16:34 +010078 u32 dummy;
Peter Oruba80cc9f12008-07-28 18:44:22 +020079
Andreas Herrmann3b2e3d82010-01-22 21:34:56 +010080 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
Borislav Petkov258721e2011-01-05 18:13:19 +010081 pr_warning("CPU%d: family %d not supported\n", cpu, c->x86);
Andreas Herrmann3b2e3d82010-01-22 21:34:56 +010082 return -1;
83 }
Borislav Petkov258721e2011-01-05 18:13:19 +010084
Andreas Herrmann29d08872008-12-16 19:16:34 +010085 rdmsr(MSR_AMD64_PATCH_LEVEL, csig->rev, dummy);
Borislav Petkov258721e2011-01-05 18:13:19 +010086 pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
87
Dmitry Adamushkod45de402008-08-20 00:22:26 +020088 return 0;
Peter Oruba80cc9f12008-07-28 18:44:22 +020089}
90
Borislav Petkov7cc27342010-12-31 16:57:48 +010091static int get_matching_microcode(int cpu, struct microcode_header_amd *mc_hdr,
92 int rev)
Peter Oruba80cc9f12008-07-28 18:44:22 +020093{
Peter Oruba80cc9f12008-07-28 18:44:22 +020094 unsigned int current_cpu_id;
Andreas Herrmann5549b942008-12-16 19:21:30 +010095 u16 equiv_cpu_id = 0;
Peter Oruba80cc9f12008-07-28 18:44:22 +020096 unsigned int i = 0;
97
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +020098 BUG_ON(equiv_cpu_table == NULL);
Peter Oruba80cc9f12008-07-28 18:44:22 +020099 current_cpu_id = cpuid_eax(0x00000001);
100
101 while (equiv_cpu_table[i].installed_cpu != 0) {
102 if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
Andreas Herrmann5549b942008-12-16 19:21:30 +0100103 equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200104 break;
105 }
106 i++;
107 }
108
Andreas Herrmann14c56942009-11-10 12:08:25 +0100109 if (!equiv_cpu_id)
Peter Oruba80cc9f12008-07-28 18:44:22 +0200110 return 0;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200111
Borislav Petkov7cc27342010-12-31 16:57:48 +0100112 if (mc_hdr->processor_rev_id != equiv_cpu_id)
Peter Oruba80cc9f12008-07-28 18:44:22 +0200113 return 0;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200114
Andreas Herrmann98415302008-12-16 19:20:21 +0100115 /* ucode might be chipset specific -- currently we don't support this */
Borislav Petkov7cc27342010-12-31 16:57:48 +0100116 if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
Borislav Petkov258721e2011-01-05 18:13:19 +0100117 pr_err("CPU%d: chipset specific code not yet supported\n",
Joe Perchesf58e1f52009-12-08 22:30:50 -0800118 cpu);
Andreas Herrmann98415302008-12-16 19:20:21 +0100119 return 0;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200120 }
121
Borislav Petkov7cc27342010-12-31 16:57:48 +0100122 if (mc_hdr->patch_id <= rev)
Peter Oruba80cc9f12008-07-28 18:44:22 +0200123 return 0;
124
Peter Oruba80cc9f12008-07-28 18:44:22 +0200125 return 1;
126}
127
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200128static int apply_microcode_amd(int cpu)
Peter Oruba80cc9f12008-07-28 18:44:22 +0200129{
Andreas Herrmann29d08872008-12-16 19:16:34 +0100130 u32 rev, dummy;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200131 int cpu_num = raw_smp_processor_id();
132 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
Dmitry Adamushko18dbc912008-09-23 12:08:44 +0200133 struct microcode_amd *mc_amd = uci->mc;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200134
135 /* We should bind the task to the CPU */
136 BUG_ON(cpu_num != cpu);
137
Dmitry Adamushko18dbc912008-09-23 12:08:44 +0200138 if (mc_amd == NULL)
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200139 return 0;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200140
Ingo Molnarf34a10b2008-12-19 01:36:14 +0100141 wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
Peter Oruba80cc9f12008-07-28 18:44:22 +0200142 /* get patch id after patching */
Andreas Herrmann29d08872008-12-16 19:16:34 +0100143 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
Peter Oruba80cc9f12008-07-28 18:44:22 +0200144
145 /* check current patch id and patch's id for match */
Dmitry Adamushko18dbc912008-09-23 12:08:44 +0200146 if (rev != mc_amd->hdr.patch_id) {
Borislav Petkov258721e2011-01-05 18:13:19 +0100147 pr_err("CPU%d: update failed for patch_level=0x%08x\n",
Joe Perchesf58e1f52009-12-08 22:30:50 -0800148 cpu, mc_amd->hdr.patch_id);
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200149 return -1;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200150 }
151
Borislav Petkov258721e2011-01-05 18:13:19 +0100152 pr_info("CPU%d: new patch_level=0x%08x\n", cpu, rev);
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200153 uci->cpu_sig.rev = rev;
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200154
155 return 0;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200156}
157
Borislav Petkov7cc27342010-12-31 16:57:48 +0100158static struct microcode_header_amd *
Ingo Molnar4bae1962009-03-11 11:19:46 +0100159get_next_ucode(const u8 *buf, unsigned int size, unsigned int *mc_size)
Peter Oruba80cc9f12008-07-28 18:44:22 +0200160{
Borislav Petkov7cc27342010-12-31 16:57:48 +0100161 struct microcode_header_amd *mc;
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200162 unsigned int total_size;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200163
Borislav Petkov7cc27342010-12-31 16:57:48 +0100164 if (buf[0] != UCODE_UCODE_TYPE) {
Borislav Petkov258721e2011-01-05 18:13:19 +0100165 pr_err("invalid type field in container file section header\n");
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200166 return NULL;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200167 }
168
Borislav Petkov7cc27342010-12-31 16:57:48 +0100169 total_size = buf[4] + (buf[5] << 8);
Peter Oruba80cc9f12008-07-28 18:44:22 +0200170
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200171 if (total_size > size || total_size > UCODE_MAX_SIZE) {
Borislav Petkov258721e2011-01-05 18:13:19 +0100172 pr_err("section size mismatch\n");
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200173 return NULL;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200174 }
175
Jesper Juhl1ea6be22010-11-01 22:44:34 +0100176 mc = vzalloc(UCODE_MAX_SIZE);
177 if (!mc)
178 return NULL;
179
Borislav Petkovc7657ac2010-11-01 23:36:53 +0100180 get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR, total_size);
181 *mc_size = total_size + UCODE_CONTAINER_SECTION_HDR;
Jesper Juhl1ea6be22010-11-01 22:44:34 +0100182
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200183 return mc;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200184}
185
Andreas Herrmann0657d9e2008-12-16 19:14:05 +0100186static int install_equiv_cpu_table(const u8 *buf)
Peter Oruba80cc9f12008-07-28 18:44:22 +0200187{
Borislav Petkov10de52d2010-12-30 22:10:12 +0100188 unsigned int *ibuf = (unsigned int *)buf;
189 unsigned int type = ibuf[1];
190 unsigned int size = ibuf[2];
Peter Oruba80cc9f12008-07-28 18:44:22 +0200191
Borislav Petkov10de52d2010-12-30 22:10:12 +0100192 if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
Borislav Petkov258721e2011-01-05 18:13:19 +0100193 pr_err("empty section/"
194 "invalid type field in container file section header\n");
Borislav Petkov10de52d2010-12-30 22:10:12 +0100195 return -EINVAL;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200196 }
197
Jesper Juhl8e5e9522010-11-09 00:08:11 +0100198 equiv_cpu_table = vmalloc(size);
Peter Oruba80cc9f12008-07-28 18:44:22 +0200199 if (!equiv_cpu_table) {
Joe Perchesf58e1f52009-12-08 22:30:50 -0800200 pr_err("failed to allocate equivalent CPU table\n");
Borislav Petkov10de52d2010-12-30 22:10:12 +0100201 return -ENOMEM;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200202 }
203
Borislav Petkov10de52d2010-12-30 22:10:12 +0100204 get_ucode_data(equiv_cpu_table, buf + UCODE_CONTAINER_HEADER_SIZE, size);
Peter Oruba80cc9f12008-07-28 18:44:22 +0200205
Peter Orubab6cffde2008-09-17 15:05:52 +0200206 return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */
Peter Oruba80cc9f12008-07-28 18:44:22 +0200207}
208
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200209static void free_equiv_cpu_table(void)
Peter Oruba80cc9f12008-07-28 18:44:22 +0200210{
Figo.zhangaeef50b2009-06-07 22:30:36 +0800211 vfree(equiv_cpu_table);
212 equiv_cpu_table = NULL;
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200213}
Peter Oruba80cc9f12008-07-28 18:44:22 +0200214
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200215static enum ucode_state
216generic_load_microcode(int cpu, const u8 *data, size_t size)
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200217{
218 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
Borislav Petkov7cc27342010-12-31 16:57:48 +0100219 struct microcode_header_amd *mc_hdr = NULL;
220 unsigned int mc_size, leftover;
221 unsigned long offset;
Andreas Herrmann8c135202008-12-16 19:13:00 +0100222 const u8 *ucode_ptr = data;
223 void *new_mc = NULL;
Borislav Petkov258721e2011-01-05 18:13:19 +0100224 unsigned int new_rev = uci->cpu_sig.rev;
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200225 enum ucode_state state = UCODE_OK;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200226
Andreas Herrmann0657d9e2008-12-16 19:14:05 +0100227 offset = install_equiv_cpu_table(ucode_ptr);
Borislav Petkov10de52d2010-12-30 22:10:12 +0100228 if (offset < 0) {
Joe Perchesf58e1f52009-12-08 22:30:50 -0800229 pr_err("failed to create equivalent cpu table\n");
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200230 return UCODE_ERROR;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200231 }
232
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200233 ucode_ptr += offset;
234 leftover = size - offset;
235
236 while (leftover) {
Borislav Petkov7cc27342010-12-31 16:57:48 +0100237 mc_hdr = get_next_ucode(ucode_ptr, leftover, &mc_size);
238 if (!mc_hdr)
Peter Oruba80cc9f12008-07-28 18:44:22 +0200239 break;
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200240
Borislav Petkov7cc27342010-12-31 16:57:48 +0100241 if (get_matching_microcode(cpu, mc_hdr, new_rev)) {
Figo.zhangaeef50b2009-06-07 22:30:36 +0800242 vfree(new_mc);
Borislav Petkov7cc27342010-12-31 16:57:48 +0100243 new_rev = mc_hdr->patch_id;
244 new_mc = mc_hdr;
Andreas Herrmannbe957762008-12-16 19:11:23 +0100245 } else
Borislav Petkov7cc27342010-12-31 16:57:48 +0100246 vfree(mc_hdr);
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200247
248 ucode_ptr += mc_size;
249 leftover -= mc_size;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200250 }
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200251
Borislav Petkov7cc27342010-12-31 16:57:48 +0100252 if (!new_mc) {
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200253 state = UCODE_NFOUND;
Borislav Petkov7cc27342010-12-31 16:57:48 +0100254 goto free_table;
255 }
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200256
Borislav Petkov7cc27342010-12-31 16:57:48 +0100257 if (!leftover) {
258 vfree(uci->mc);
259 uci->mc = new_mc;
Borislav Petkov258721e2011-01-05 18:13:19 +0100260 pr_debug("CPU%d update ucode (0x%08x -> 0x%08x)\n",
261 cpu, uci->cpu_sig.rev, new_rev);
Borislav Petkov7cc27342010-12-31 16:57:48 +0100262 } else {
263 vfree(new_mc);
264 state = UCODE_ERROR;
265 }
266
267free_table:
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200268 free_equiv_cpu_table();
269
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200270 return state;
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200271}
272
Borislav Petkovffc7e8a2010-12-30 21:06:01 +0100273static enum ucode_state request_microcode_amd(int cpu, struct device *device)
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200274{
Andreas Herrmann3b2e3d82010-01-22 21:34:56 +0100275 const char *fw_name = "amd-ucode/microcode_amd.bin";
Borislav Petkovffc7e8a2010-12-30 21:06:01 +0100276 const struct firmware *fw;
277 enum ucode_state ret = UCODE_NFOUND;
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200278
Borislav Petkovffc7e8a2010-12-30 21:06:01 +0100279 if (request_firmware(&fw, fw_name, device)) {
Borislav Petkov258721e2011-01-05 18:13:19 +0100280 pr_err("failed to load file %s\n", fw_name);
Borislav Petkovffc7e8a2010-12-30 21:06:01 +0100281 goto out;
Andreas Herrmann3b2e3d82010-01-22 21:34:56 +0100282 }
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200283
Borislav Petkovffc7e8a2010-12-30 21:06:01 +0100284 ret = UCODE_ERROR;
285 if (*(u32 *)fw->data != UCODE_MAGIC) {
Borislav Petkov258721e2011-01-05 18:13:19 +0100286 pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
Borislav Petkovffc7e8a2010-12-30 21:06:01 +0100287 goto fw_release;
Borislav Petkov506f90e2009-10-29 14:45:52 +0100288 }
289
Borislav Petkovffc7e8a2010-12-30 21:06:01 +0100290 ret = generic_load_microcode(cpu, fw->data, fw->size);
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200291
Borislav Petkovffc7e8a2010-12-30 21:06:01 +0100292fw_release:
293 release_firmware(fw);
Andreas Herrmann3b2e3d82010-01-22 21:34:56 +0100294
Borislav Petkovffc7e8a2010-12-30 21:06:01 +0100295out:
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200296 return ret;
297}
298
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200299static enum ucode_state
300request_microcode_user(int cpu, const void __user *buf, size_t size)
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200301{
Joe Perchesf58e1f52009-12-08 22:30:50 -0800302 pr_info("AMD microcode update via /dev/cpu/microcode not supported\n");
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200303 return UCODE_ERROR;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200304}
305
Peter Oruba80cc9f12008-07-28 18:44:22 +0200306static void microcode_fini_cpu_amd(int cpu)
307{
308 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
309
Dmitry Adamushko18dbc912008-09-23 12:08:44 +0200310 vfree(uci->mc);
311 uci->mc = NULL;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200312}
313
314static struct microcode_ops microcode_amd_ops = {
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200315 .request_microcode_user = request_microcode_user,
Borislav Petkovffc7e8a2010-12-30 21:06:01 +0100316 .request_microcode_fw = request_microcode_amd,
Peter Oruba80cc9f12008-07-28 18:44:22 +0200317 .collect_cpu_info = collect_cpu_info_amd,
318 .apply_microcode = apply_microcode_amd,
319 .microcode_fini_cpu = microcode_fini_cpu_amd,
320};
321
Dmitry Adamushko18dbc912008-09-23 12:08:44 +0200322struct microcode_ops * __init init_amd_microcode(void)
Peter Oruba80cc9f12008-07-28 18:44:22 +0200323{
Dmitry Adamushko18dbc912008-09-23 12:08:44 +0200324 return &microcode_amd_ops;
Peter Oruba80cc9f12008-07-28 18:44:22 +0200325}