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