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