x86: cpu_debug remove execute permission
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / microcode_amd.c
blob453b5795a5c6a23817f8e833f722d6140c6be644
1 /*
2 * AMD CPU Microcode Update Driver for Linux
3 * Copyright (C) 2008 Advanced Micro Devices Inc.
5 * Author: Peter Oruba <peter.oruba@amd.com>
7 * Based on work by:
8 * Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
10 * This driver allows to upgrade microcode on AMD
11 * family 0x10 and 0x11 processors.
13 * Licensed under the terms of the GNU General Public
14 * License version 2. See file COPYING for details.
16 #include <linux/platform_device.h>
17 #include <linux/capability.h>
18 #include <linux/miscdevice.h>
19 #include <linux/firmware.h>
20 #include <linux/spinlock.h>
21 #include <linux/cpumask.h>
22 #include <linux/pci_ids.h>
23 #include <linux/uaccess.h>
24 #include <linux/vmalloc.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/sched.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/cpu.h>
32 #include <linux/pci.h>
33 #include <linux/fs.h>
34 #include <linux/mm.h>
36 #include <asm/microcode.h>
37 #include <asm/processor.h>
38 #include <asm/msr.h>
40 MODULE_DESCRIPTION("AMD Microcode Update Driver");
41 MODULE_AUTHOR("Peter Oruba");
42 MODULE_LICENSE("GPL v2");
44 #define UCODE_MAGIC 0x00414d44
45 #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
46 #define UCODE_UCODE_TYPE 0x00000001
48 struct equiv_cpu_entry {
49 u32 installed_cpu;
50 u32 fixed_errata_mask;
51 u32 fixed_errata_compare;
52 u16 equiv_cpu;
53 u16 res;
54 } __attribute__((packed));
56 struct microcode_header_amd {
57 u32 data_code;
58 u32 patch_id;
59 u16 mc_patch_data_id;
60 u8 mc_patch_data_len;
61 u8 init_flag;
62 u32 mc_patch_data_checksum;
63 u32 nb_dev_id;
64 u32 sb_dev_id;
65 u16 processor_rev_id;
66 u8 nb_rev_id;
67 u8 sb_rev_id;
68 u8 bios_api_rev;
69 u8 reserved1[3];
70 u32 match_reg[8];
71 } __attribute__((packed));
73 struct microcode_amd {
74 struct microcode_header_amd hdr;
75 unsigned int mpb[0];
78 #define UCODE_MAX_SIZE 2048
79 #define UCODE_CONTAINER_SECTION_HDR 8
80 #define UCODE_CONTAINER_HEADER_SIZE 12
82 /* serialize access to the physical write */
83 static DEFINE_SPINLOCK(microcode_update_lock);
85 static struct equiv_cpu_entry *equiv_cpu_table;
87 static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
89 struct cpuinfo_x86 *c = &cpu_data(cpu);
90 u32 dummy;
92 memset(csig, 0, sizeof(*csig));
93 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
94 printk(KERN_WARNING "microcode: CPU%d: AMD CPU family 0x%x not "
95 "supported\n", cpu, c->x86);
96 return -1;
98 rdmsr(MSR_AMD64_PATCH_LEVEL, csig->rev, dummy);
99 printk(KERN_INFO "microcode: CPU%d: patch_level=0x%x\n", cpu, csig->rev);
100 return 0;
103 static int get_matching_microcode(int cpu, void *mc, int rev)
105 struct microcode_header_amd *mc_header = mc;
106 unsigned int current_cpu_id;
107 u16 equiv_cpu_id = 0;
108 unsigned int i = 0;
110 BUG_ON(equiv_cpu_table == NULL);
111 current_cpu_id = cpuid_eax(0x00000001);
113 while (equiv_cpu_table[i].installed_cpu != 0) {
114 if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
115 equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
116 break;
118 i++;
121 if (!equiv_cpu_id) {
122 printk(KERN_WARNING "microcode: CPU%d: cpu revision "
123 "not listed in equivalent cpu table\n", cpu);
124 return 0;
127 if (mc_header->processor_rev_id != equiv_cpu_id) {
128 printk(KERN_ERR "microcode: CPU%d: patch mismatch "
129 "(processor_rev_id: %x, equiv_cpu_id: %x)\n",
130 cpu, mc_header->processor_rev_id, equiv_cpu_id);
131 return 0;
134 /* ucode might be chipset specific -- currently we don't support this */
135 if (mc_header->nb_dev_id || mc_header->sb_dev_id) {
136 printk(KERN_ERR "microcode: CPU%d: loading of chipset "
137 "specific code not yet supported\n", cpu);
138 return 0;
141 if (mc_header->patch_id <= rev)
142 return 0;
144 return 1;
147 static void apply_microcode_amd(int cpu)
149 unsigned long flags;
150 u32 rev, dummy;
151 int cpu_num = raw_smp_processor_id();
152 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
153 struct microcode_amd *mc_amd = uci->mc;
155 /* We should bind the task to the CPU */
156 BUG_ON(cpu_num != cpu);
158 if (mc_amd == NULL)
159 return;
161 spin_lock_irqsave(&microcode_update_lock, flags);
162 wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
163 /* get patch id after patching */
164 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
165 spin_unlock_irqrestore(&microcode_update_lock, flags);
167 /* check current patch id and patch's id for match */
168 if (rev != mc_amd->hdr.patch_id) {
169 printk(KERN_ERR "microcode: CPU%d: update failed "
170 "(for patch_level=0x%x)\n", cpu, mc_amd->hdr.patch_id);
171 return;
174 printk(KERN_INFO "microcode: CPU%d: updated (new patch_level=0x%x)\n",
175 cpu, rev);
177 uci->cpu_sig.rev = rev;
180 static int get_ucode_data(void *to, const u8 *from, size_t n)
182 memcpy(to, from, n);
183 return 0;
186 static void *
187 get_next_ucode(const u8 *buf, unsigned int size, unsigned int *mc_size)
189 unsigned int total_size;
190 u8 section_hdr[UCODE_CONTAINER_SECTION_HDR];
191 void *mc;
193 if (get_ucode_data(section_hdr, buf, UCODE_CONTAINER_SECTION_HDR))
194 return NULL;
196 if (section_hdr[0] != UCODE_UCODE_TYPE) {
197 printk(KERN_ERR "microcode: error: invalid type field in "
198 "container file section header\n");
199 return NULL;
202 total_size = (unsigned long) (section_hdr[4] + (section_hdr[5] << 8));
204 printk(KERN_DEBUG "microcode: size %u, total_size %u\n",
205 size, total_size);
207 if (total_size > size || total_size > UCODE_MAX_SIZE) {
208 printk(KERN_ERR "microcode: error: size mismatch\n");
209 return NULL;
212 mc = vmalloc(UCODE_MAX_SIZE);
213 if (mc) {
214 memset(mc, 0, UCODE_MAX_SIZE);
215 if (get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR,
216 total_size)) {
217 vfree(mc);
218 mc = NULL;
219 } else
220 *mc_size = total_size + UCODE_CONTAINER_SECTION_HDR;
222 return mc;
225 static int install_equiv_cpu_table(const u8 *buf)
227 u8 *container_hdr[UCODE_CONTAINER_HEADER_SIZE];
228 unsigned int *buf_pos = (unsigned int *)container_hdr;
229 unsigned long size;
231 if (get_ucode_data(&container_hdr, buf, UCODE_CONTAINER_HEADER_SIZE))
232 return 0;
234 size = buf_pos[2];
236 if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
237 printk(KERN_ERR "microcode: error: invalid type field in "
238 "container file section header\n");
239 return 0;
242 equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
243 if (!equiv_cpu_table) {
244 printk(KERN_ERR "microcode: failed to allocate "
245 "equivalent CPU table\n");
246 return 0;
249 buf += UCODE_CONTAINER_HEADER_SIZE;
250 if (get_ucode_data(equiv_cpu_table, buf, size)) {
251 vfree(equiv_cpu_table);
252 return 0;
255 return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */
258 static void free_equiv_cpu_table(void)
260 if (equiv_cpu_table) {
261 vfree(equiv_cpu_table);
262 equiv_cpu_table = NULL;
266 static int generic_load_microcode(int cpu, const u8 *data, size_t size)
268 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
269 const u8 *ucode_ptr = data;
270 void *new_mc = NULL;
271 void *mc;
272 int new_rev = uci->cpu_sig.rev;
273 unsigned int leftover;
274 unsigned long offset;
276 offset = install_equiv_cpu_table(ucode_ptr);
277 if (!offset) {
278 printk(KERN_ERR "microcode: failed to create "
279 "equivalent cpu table\n");
280 return -EINVAL;
283 ucode_ptr += offset;
284 leftover = size - offset;
286 while (leftover) {
287 unsigned int uninitialized_var(mc_size);
288 struct microcode_header_amd *mc_header;
290 mc = get_next_ucode(ucode_ptr, leftover, &mc_size);
291 if (!mc)
292 break;
294 mc_header = (struct microcode_header_amd *)mc;
295 if (get_matching_microcode(cpu, mc, new_rev)) {
296 if (new_mc)
297 vfree(new_mc);
298 new_rev = mc_header->patch_id;
299 new_mc = mc;
300 } else
301 vfree(mc);
303 ucode_ptr += mc_size;
304 leftover -= mc_size;
307 if (new_mc) {
308 if (!leftover) {
309 if (uci->mc)
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);
319 free_equiv_cpu_table();
321 return (int)leftover;
324 static int request_microcode_fw(int cpu, struct device *device)
326 const char *fw_name = "amd-ucode/microcode_amd.bin";
327 const struct firmware *firmware;
328 int ret;
330 /* We should bind the task to the CPU */
331 BUG_ON(cpu != raw_smp_processor_id());
333 ret = request_firmware(&firmware, fw_name, device);
334 if (ret) {
335 printk(KERN_ERR "microcode: failed to load file %s\n", fw_name);
336 return ret;
339 ret = generic_load_microcode(cpu, firmware->data, firmware->size);
341 release_firmware(firmware);
343 return ret;
346 static int request_microcode_user(int cpu, const void __user *buf, size_t size)
348 printk(KERN_INFO "microcode: AMD microcode update via "
349 "/dev/cpu/microcode not supported\n");
350 return -1;
353 static void microcode_fini_cpu_amd(int cpu)
355 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
357 vfree(uci->mc);
358 uci->mc = NULL;
361 static struct microcode_ops microcode_amd_ops = {
362 .request_microcode_user = request_microcode_user,
363 .request_microcode_fw = request_microcode_fw,
364 .collect_cpu_info = collect_cpu_info_amd,
365 .apply_microcode = apply_microcode_amd,
366 .microcode_fini_cpu = microcode_fini_cpu_amd,
369 struct microcode_ops * __init init_amd_microcode(void)
371 return &microcode_amd_ops;