Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2007 Advanced Micro Devices, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Paul Menzel | a46a712 | 2013-02-23 18:37:27 +0100 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <console/console.h> |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 22 | #include <cpu/x86/msr.h> |
| 23 | #include <cpu/amd/microcode.h> |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame^] | 24 | #include <cbfs.h> |
| 25 | |
| 26 | #define UCODE_DEBUG(fmt, args...) \ |
| 27 | do { printk(BIOS_DEBUG, "[microcode] "fmt, ##args); } while(0) |
Marc Jones | 8ae8c88 | 2007-12-19 01:32:08 +0000 | [diff] [blame] | 28 | |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 29 | struct microcode { |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 30 | u32 date_code; |
| 31 | u32 patch_id; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 32 | |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 33 | u16 m_patch_data_id; |
| 34 | u8 m_patch_data_len; |
| 35 | u8 init_flag; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 36 | |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 37 | u32 m_patch_data_cksum; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 38 | |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 39 | u32 nb_dev_id; |
| 40 | u32 ht_io_hub_dev_id; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 41 | |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 42 | u16 processor_rev_id; |
| 43 | u8 ht_io_hub_rev_id; |
| 44 | u8 nb_rev_id; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 45 | |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 46 | u8 bios_api_rev; |
| 47 | u8 resv1[3]; |
| 48 | |
| 49 | u32 match_reg[8]; |
| 50 | |
| 51 | u8 m_patch_data[896]; |
| 52 | u8 resv2[896]; |
| 53 | |
| 54 | u8 x86_code_present; |
| 55 | u8 x86_code_entry[191]; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame^] | 58 | static void apply_microcode_patch(const struct microcode *m) |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 59 | { |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame^] | 60 | uint32_t new_patch_id; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 61 | msr_t msr; |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 62 | |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame^] | 63 | /* apply patch */ |
| 64 | msr.hi = 0; |
| 65 | msr.lo = (uint32_t)m; |
| 66 | |
| 67 | wrmsr(0xc0010020, msr); |
| 68 | |
| 69 | UCODE_DEBUG("patch id to apply = 0x%08x\n", m->patch_id); |
| 70 | |
| 71 | /* read the patch_id again */ |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 72 | msr = rdmsr(0x8b); |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame^] | 73 | new_patch_id = msr.lo; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 74 | |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame^] | 75 | UCODE_DEBUG("updated to patch id = 0x%08x %s\n", new_patch_id , |
| 76 | (new_patch_id == m->patch_id) ? "success" : "fail"); |
| 77 | } |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 78 | |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame^] | 79 | static void amd_update_microcode(const void *ucode, size_t ucode_len, |
| 80 | uint32_t equivalent_processor_rev_id) |
| 81 | { |
| 82 | const struct microcode *m; |
| 83 | const void *c; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 84 | |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame^] | 85 | for(m = c = ucode; m->date_code; m = c) { |
Kyösti Mälkki | 75b68d8 | 2013-12-12 06:53:05 +0200 | [diff] [blame] | 86 | if (m->processor_rev_id == equivalent_processor_rev_id) { |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame^] | 87 | apply_microcode_patch(m); |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | c += 2048; |
| 91 | } |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame^] | 92 | } |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 93 | |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame^] | 94 | #define MICROCODE_CBFS_FILE "cpu_microcode_blob.bin" |
| 95 | |
| 96 | void amd_update_microcode_from_cbfs(u32 equivalent_processor_rev_id) |
| 97 | { |
| 98 | const void *ucode; |
| 99 | size_t ucode_len; |
| 100 | |
| 101 | if (equivalent_processor_rev_id == 0) { |
| 102 | UCODE_DEBUG("rev id not found. Skipping microcode patch!\n"); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | ucode = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, MICROCODE_CBFS_FILE, |
| 107 | CBFS_TYPE_MICROCODE, &ucode_len); |
| 108 | if (!ucode) { |
| 109 | UCODE_DEBUG("microcode file not found. Skipping updates.\n"); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | amd_update_microcode(ucode, ucode_len, equivalent_processor_rev_id); |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 114 | } |