Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 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 | * |
Andreas Herrmann | 2a3282a7 | 2008-12-16 19:08:53 +0100 | [diff] [blame] | 13 | * Licensed under the terms of the GNU General Public |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 14 | * License version 2. See file COPYING for details. |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame^] | 15 | */ |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame^] | 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> |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 22 | #include <linux/pci_ids.h> |
Andreas Herrmann | be95776 | 2008-12-16 19:11:23 +0100 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame^] | 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> |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 35 | |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 36 | #include <asm/microcode.h> |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame^] | 37 | #include <asm/processor.h> |
| 38 | #include <asm/msr.h> |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 39 | |
| 40 | MODULE_DESCRIPTION("AMD Microcode Update Driver"); |
Peter Oruba | 3c52204 | 2008-10-17 15:30:38 +0200 | [diff] [blame] | 41 | MODULE_AUTHOR("Peter Oruba"); |
Ingo Molnar | 5d7b605 | 2008-07-29 10:07:36 +0200 | [diff] [blame] | 42 | MODULE_LICENSE("GPL v2"); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 43 | |
| 44 | #define UCODE_MAGIC 0x00414d44 |
| 45 | #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000 |
| 46 | #define UCODE_UCODE_TYPE 0x00000001 |
| 47 | |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 48 | struct equiv_cpu_entry { |
Andreas Herrmann | 5549b94 | 2008-12-16 19:21:30 +0100 | [diff] [blame] | 49 | u32 installed_cpu; |
| 50 | u32 fixed_errata_mask; |
| 51 | u32 fixed_errata_compare; |
| 52 | u16 equiv_cpu; |
| 53 | u16 res; |
| 54 | } __attribute__((packed)); |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 55 | |
| 56 | struct microcode_header_amd { |
Andreas Herrmann | 5549b94 | 2008-12-16 19:21:30 +0100 | [diff] [blame] | 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)); |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 72 | |
| 73 | struct microcode_amd { |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame^] | 74 | struct microcode_header_amd hdr; |
| 75 | unsigned int mpb[0]; |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 76 | }; |
| 77 | |
Andreas Herrmann | 6cc9b6d | 2008-12-16 19:17:45 +0100 | [diff] [blame] | 78 | #define UCODE_MAX_SIZE 2048 |
| 79 | #define UCODE_CONTAINER_SECTION_HDR 8 |
| 80 | #define UCODE_CONTAINER_HEADER_SIZE 12 |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 81 | |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 82 | /* serialize access to the physical write */ |
| 83 | static DEFINE_SPINLOCK(microcode_update_lock); |
| 84 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 85 | static struct equiv_cpu_entry *equiv_cpu_table; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 86 | |
Dmitry Adamushko | d45de40 | 2008-08-20 00:22:26 +0200 | [diff] [blame] | 87 | static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 88 | { |
| 89 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
Andreas Herrmann | 29d0887 | 2008-12-16 19:16:34 +0100 | [diff] [blame] | 90 | u32 dummy; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 91 | |
Dmitry Adamushko | d45de40 | 2008-08-20 00:22:26 +0200 | [diff] [blame] | 92 | memset(csig, 0, sizeof(*csig)); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 93 | if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 94 | printk(KERN_WARNING "microcode: CPU%d: AMD CPU family 0x%x not " |
| 95 | "supported\n", cpu, c->x86); |
Dmitry Adamushko | d45de40 | 2008-08-20 00:22:26 +0200 | [diff] [blame] | 96 | return -1; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 97 | } |
Andreas Herrmann | 29d0887 | 2008-12-16 19:16:34 +0100 | [diff] [blame] | 98 | rdmsr(MSR_AMD64_PATCH_LEVEL, csig->rev, dummy); |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 99 | printk(KERN_INFO "microcode: CPU%d: patch_level=0x%x\n", cpu, csig->rev); |
Dmitry Adamushko | d45de40 | 2008-08-20 00:22:26 +0200 | [diff] [blame] | 100 | return 0; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 103 | static int get_matching_microcode(int cpu, void *mc, int rev) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 104 | { |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 105 | struct microcode_header_amd *mc_header = mc; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 106 | unsigned int current_cpu_id; |
Andreas Herrmann | 5549b94 | 2008-12-16 19:21:30 +0100 | [diff] [blame] | 107 | u16 equiv_cpu_id = 0; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 108 | unsigned int i = 0; |
| 109 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 110 | BUG_ON(equiv_cpu_table == NULL); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 111 | current_cpu_id = cpuid_eax(0x00000001); |
| 112 | |
| 113 | while (equiv_cpu_table[i].installed_cpu != 0) { |
| 114 | if (current_cpu_id == equiv_cpu_table[i].installed_cpu) { |
Andreas Herrmann | 5549b94 | 2008-12-16 19:21:30 +0100 | [diff] [blame] | 115 | equiv_cpu_id = equiv_cpu_table[i].equiv_cpu; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 116 | break; |
| 117 | } |
| 118 | i++; |
| 119 | } |
| 120 | |
| 121 | if (!equiv_cpu_id) { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 122 | printk(KERN_WARNING "microcode: CPU%d: cpu revision " |
| 123 | "not listed in equivalent cpu table\n", cpu); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
Andreas Herrmann | 3c763fd | 2008-12-16 19:07:47 +0100 | [diff] [blame] | 127 | if (mc_header->processor_rev_id != equiv_cpu_id) { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 128 | printk(KERN_ERR "microcode: CPU%d: patch mismatch " |
| 129 | "(processor_rev_id: %x, equiv_cpu_id: %x)\n", |
Andreas Herrmann | 3c763fd | 2008-12-16 19:07:47 +0100 | [diff] [blame] | 130 | cpu, mc_header->processor_rev_id, equiv_cpu_id); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
Andreas Herrmann | 9841530 | 2008-12-16 19:20:21 +0100 | [diff] [blame] | 134 | /* ucode might be chipset specific -- currently we don't support this */ |
| 135 | if (mc_header->nb_dev_id || mc_header->sb_dev_id) { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 136 | printk(KERN_ERR "microcode: CPU%d: loading of chipset " |
Andreas Herrmann | 9841530 | 2008-12-16 19:20:21 +0100 | [diff] [blame] | 137 | "specific code not yet supported\n", cpu); |
| 138 | return 0; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 141 | if (mc_header->patch_id <= rev) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 142 | return 0; |
| 143 | |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | static void apply_microcode_amd(int cpu) |
| 148 | { |
| 149 | unsigned long flags; |
Andreas Herrmann | 29d0887 | 2008-12-16 19:16:34 +0100 | [diff] [blame] | 150 | u32 rev, dummy; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 151 | int cpu_num = raw_smp_processor_id(); |
| 152 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 153 | struct microcode_amd *mc_amd = uci->mc; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 154 | |
| 155 | /* We should bind the task to the CPU */ |
| 156 | BUG_ON(cpu_num != cpu); |
| 157 | |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 158 | if (mc_amd == NULL) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 159 | return; |
| 160 | |
| 161 | spin_lock_irqsave(µcode_update_lock, flags); |
Ingo Molnar | f34a10b | 2008-12-19 01:36:14 +0100 | [diff] [blame] | 162 | wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 163 | /* get patch id after patching */ |
Andreas Herrmann | 29d0887 | 2008-12-16 19:16:34 +0100 | [diff] [blame] | 164 | rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 165 | spin_unlock_irqrestore(µcode_update_lock, flags); |
| 166 | |
| 167 | /* check current patch id and patch's id for match */ |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 168 | if (rev != mc_amd->hdr.patch_id) { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 169 | printk(KERN_ERR "microcode: CPU%d: update failed " |
| 170 | "(for patch_level=0x%x)\n", cpu, mc_amd->hdr.patch_id); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 174 | printk(KERN_INFO "microcode: CPU%d: updated (new patch_level=0x%x)\n", |
| 175 | cpu, rev); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 176 | |
Dmitry Adamushko | d45de40 | 2008-08-20 00:22:26 +0200 | [diff] [blame] | 177 | uci->cpu_sig.rev = rev; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Andreas Herrmann | 0657d9e | 2008-12-16 19:14:05 +0100 | [diff] [blame] | 180 | static int get_ucode_data(void *to, const u8 *from, size_t n) |
| 181 | { |
| 182 | memcpy(to, from, n); |
| 183 | return 0; |
| 184 | } |
| 185 | |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame^] | 186 | static void * |
| 187 | get_next_ucode(const u8 *buf, unsigned int size, unsigned int *mc_size) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 188 | { |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 189 | unsigned int total_size; |
Peter Oruba | d473879 | 2008-09-17 15:39:18 +0200 | [diff] [blame] | 190 | u8 section_hdr[UCODE_CONTAINER_SECTION_HDR]; |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 191 | void *mc; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 192 | |
Peter Oruba | d473879 | 2008-09-17 15:39:18 +0200 | [diff] [blame] | 193 | if (get_ucode_data(section_hdr, buf, UCODE_CONTAINER_SECTION_HDR)) |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 194 | return NULL; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 195 | |
Peter Oruba | d473879 | 2008-09-17 15:39:18 +0200 | [diff] [blame] | 196 | if (section_hdr[0] != UCODE_UCODE_TYPE) { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 197 | printk(KERN_ERR "microcode: error: invalid type field in " |
| 198 | "container file section header\n"); |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 199 | return NULL; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Peter Oruba | d473879 | 2008-09-17 15:39:18 +0200 | [diff] [blame] | 202 | total_size = (unsigned long) (section_hdr[4] + (section_hdr[5] << 8)); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 203 | |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 204 | printk(KERN_DEBUG "microcode: size %u, total_size %u\n", |
| 205 | size, total_size); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 206 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 207 | if (total_size > size || total_size > UCODE_MAX_SIZE) { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 208 | printk(KERN_ERR "microcode: error: size mismatch\n"); |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 209 | return NULL; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 212 | mc = vmalloc(UCODE_MAX_SIZE); |
| 213 | if (mc) { |
| 214 | memset(mc, 0, UCODE_MAX_SIZE); |
Andreas Herrmann | be95776 | 2008-12-16 19:11:23 +0100 | [diff] [blame] | 215 | if (get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR, |
| 216 | total_size)) { |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 217 | vfree(mc); |
| 218 | mc = NULL; |
| 219 | } else |
Peter Oruba | d473879 | 2008-09-17 15:39:18 +0200 | [diff] [blame] | 220 | *mc_size = total_size + UCODE_CONTAINER_SECTION_HDR; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 221 | } |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 222 | return mc; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Andreas Herrmann | 0657d9e | 2008-12-16 19:14:05 +0100 | [diff] [blame] | 225 | static int install_equiv_cpu_table(const u8 *buf) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 226 | { |
Peter Oruba | b6cffde | 2008-09-17 15:05:52 +0200 | [diff] [blame] | 227 | u8 *container_hdr[UCODE_CONTAINER_HEADER_SIZE]; |
| 228 | unsigned int *buf_pos = (unsigned int *)container_hdr; |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 229 | unsigned long size; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 230 | |
Peter Oruba | b6cffde | 2008-09-17 15:05:52 +0200 | [diff] [blame] | 231 | if (get_ucode_data(&container_hdr, buf, UCODE_CONTAINER_HEADER_SIZE)) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 232 | return 0; |
| 233 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 234 | size = buf_pos[2]; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 235 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 236 | if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE || !size) { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 237 | printk(KERN_ERR "microcode: error: invalid type field in " |
| 238 | "container file section header\n"); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size); |
| 243 | if (!equiv_cpu_table) { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 244 | printk(KERN_ERR "microcode: failed to allocate " |
| 245 | "equivalent CPU table\n"); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Peter Oruba | b6cffde | 2008-09-17 15:05:52 +0200 | [diff] [blame] | 249 | buf += UCODE_CONTAINER_HEADER_SIZE; |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 250 | if (get_ucode_data(equiv_cpu_table, buf, size)) { |
| 251 | vfree(equiv_cpu_table); |
| 252 | return 0; |
| 253 | } |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 254 | |
Peter Oruba | b6cffde | 2008-09-17 15:05:52 +0200 | [diff] [blame] | 255 | return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */ |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 258 | static void free_equiv_cpu_table(void) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 259 | { |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 260 | if (equiv_cpu_table) { |
| 261 | vfree(equiv_cpu_table); |
| 262 | equiv_cpu_table = NULL; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 263 | } |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 264 | } |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 265 | |
Andreas Herrmann | 0657d9e | 2008-12-16 19:14:05 +0100 | [diff] [blame] | 266 | static int generic_load_microcode(int cpu, const u8 *data, size_t size) |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 267 | { |
| 268 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
Andreas Herrmann | 8c13520 | 2008-12-16 19:13:00 +0100 | [diff] [blame] | 269 | const u8 *ucode_ptr = data; |
| 270 | void *new_mc = NULL; |
| 271 | void *mc; |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 272 | int new_rev = uci->cpu_sig.rev; |
| 273 | unsigned int leftover; |
| 274 | unsigned long offset; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 275 | |
Andreas Herrmann | 0657d9e | 2008-12-16 19:14:05 +0100 | [diff] [blame] | 276 | offset = install_equiv_cpu_table(ucode_ptr); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 277 | if (!offset) { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 278 | printk(KERN_ERR "microcode: failed to create " |
| 279 | "equivalent cpu table\n"); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 280 | return -EINVAL; |
| 281 | } |
| 282 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 283 | ucode_ptr += offset; |
| 284 | leftover = size - offset; |
| 285 | |
| 286 | while (leftover) { |
Dmitry Adamushko | 2f9284e | 2008-09-23 22:56:35 +0200 | [diff] [blame] | 287 | unsigned int uninitialized_var(mc_size); |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 288 | struct microcode_header_amd *mc_header; |
| 289 | |
Andreas Herrmann | 0657d9e | 2008-12-16 19:14:05 +0100 | [diff] [blame] | 290 | mc = get_next_ucode(ucode_ptr, leftover, &mc_size); |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 291 | if (!mc) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 292 | break; |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 293 | |
| 294 | mc_header = (struct microcode_header_amd *)mc; |
| 295 | if (get_matching_microcode(cpu, mc, new_rev)) { |
Ingo Molnar | a1c75cc | 2008-09-14 14:50:26 +0200 | [diff] [blame] | 296 | if (new_mc) |
| 297 | vfree(new_mc); |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 298 | new_rev = mc_header->patch_id; |
| 299 | new_mc = mc; |
Andreas Herrmann | be95776 | 2008-12-16 19:11:23 +0100 | [diff] [blame] | 300 | } else |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 301 | vfree(mc); |
| 302 | |
| 303 | ucode_ptr += mc_size; |
| 304 | leftover -= mc_size; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 305 | } |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 306 | |
| 307 | if (new_mc) { |
| 308 | if (!leftover) { |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 309 | if (uci->mc) |
| 310 | vfree(uci->mc); |
| 311 | uci->mc = new_mc; |
Andreas Herrmann | be95776 | 2008-12-16 19:11:23 +0100 | [diff] [blame] | 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); |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 315 | } else |
| 316 | vfree(new_mc); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 317 | } |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 318 | |
| 319 | free_equiv_cpu_table(); |
| 320 | |
| 321 | return (int)leftover; |
| 322 | } |
| 323 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 324 | static int request_microcode_fw(int cpu, struct device *device) |
| 325 | { |
| 326 | const char *fw_name = "amd-ucode/microcode_amd.bin"; |
| 327 | const struct firmware *firmware; |
| 328 | int ret; |
| 329 | |
| 330 | /* We should bind the task to the CPU */ |
| 331 | BUG_ON(cpu != raw_smp_processor_id()); |
| 332 | |
| 333 | ret = request_firmware(&firmware, fw_name, device); |
| 334 | if (ret) { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 335 | printk(KERN_ERR "microcode: failed to load file %s\n", fw_name); |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 336 | return ret; |
| 337 | } |
| 338 | |
Andreas Herrmann | 0657d9e | 2008-12-16 19:14:05 +0100 | [diff] [blame] | 339 | ret = generic_load_microcode(cpu, firmware->data, firmware->size); |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 340 | |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 341 | release_firmware(firmware); |
| 342 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 343 | return ret; |
| 344 | } |
| 345 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 346 | static int request_microcode_user(int cpu, const void __user *buf, size_t size) |
| 347 | { |
Andreas Herrmann | df23cab | 2008-12-16 19:22:36 +0100 | [diff] [blame] | 348 | printk(KERN_INFO "microcode: AMD microcode update via " |
| 349 | "/dev/cpu/microcode not supported\n"); |
Dmitry Adamushko | 2f9284e | 2008-09-23 22:56:35 +0200 | [diff] [blame] | 350 | return -1; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 353 | static void microcode_fini_cpu_amd(int cpu) |
| 354 | { |
| 355 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 356 | |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 357 | vfree(uci->mc); |
| 358 | uci->mc = NULL; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static struct microcode_ops microcode_amd_ops = { |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 362 | .request_microcode_user = request_microcode_user, |
| 363 | .request_microcode_fw = request_microcode_fw, |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 364 | .collect_cpu_info = collect_cpu_info_amd, |
| 365 | .apply_microcode = apply_microcode_amd, |
| 366 | .microcode_fini_cpu = microcode_fini_cpu_amd, |
| 367 | }; |
| 368 | |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 369 | struct microcode_ops * __init init_amd_microcode(void) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 370 | { |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 371 | return µcode_amd_ops; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 372 | } |