Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2010 coresystems GmbH |
| 5 | * Copyright (C) 2010 Rudolf Marek |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <console/console.h> |
| 18 | #include <arch/io.h> |
| 19 | #include <cpu/cpu.h> |
| 20 | #include <cpu/x86/lapic.h> |
| 21 | #include <cpu/x86/msr.h> |
| 22 | #include <cpu/x86/mtrr.h> |
| 23 | #include <cpu/amd/mtrr.h> |
Stefan Reinauer | 991f184 | 2015-11-22 23:40:29 +0100 | [diff] [blame^] | 24 | #include <cpu/amd/msr.h> |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 25 | #include <cpu/x86/cache.h> |
| 26 | #include <cpu/x86/smm.h> |
| 27 | #include <string.h> |
| 28 | |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 29 | extern unsigned char _binary_smm_start; |
| 30 | extern unsigned char _binary_smm_size; |
| 31 | |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 32 | void smm_init(void) |
| 33 | { |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 34 | msr_t msr, syscfg_orig, mtrr_aseg_orig; |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 35 | |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 36 | /* Back up MSRs for later restore */ |
| 37 | syscfg_orig = rdmsr(SYSCFG_MSR); |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 38 | mtrr_aseg_orig = rdmsr(MTRR_FIX_16K_A0000); |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 39 | |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 40 | /* MTRR changes don't like an enabled cache */ |
| 41 | disable_cache(); |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 42 | |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 43 | msr = syscfg_orig; |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 44 | |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 45 | /* Allow changes to MTRR extended attributes */ |
| 46 | msr.lo |= SYSCFG_MSR_MtrrFixDramModEn; |
| 47 | /* turn the extended attributes off until we fix |
| 48 | * them so A0000 is routed to memory |
| 49 | */ |
| 50 | msr.lo &= ~SYSCFG_MSR_MtrrFixDramEn; |
| 51 | wrmsr(SYSCFG_MSR, msr); |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 52 | |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 53 | /* set DRAM access to 0xa0000 */ |
| 54 | msr.lo = 0x18181818; |
| 55 | msr.hi = 0x18181818; |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 56 | wrmsr(MTRR_FIX_16K_A0000, msr); |
Rudolf Marek | 2c36627 | 2010-12-18 23:30:59 +0000 | [diff] [blame] | 57 | |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 58 | /* enable the extended features */ |
| 59 | msr = syscfg_orig; |
| 60 | msr.lo |= SYSCFG_MSR_MtrrFixDramModEn; |
| 61 | msr.lo |= SYSCFG_MSR_MtrrFixDramEn; |
| 62 | wrmsr(SYSCFG_MSR, msr); |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 63 | |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 64 | enable_cache(); |
| 65 | /* copy the real SMM handler */ |
| 66 | memcpy((void *)SMM_BASE, &_binary_smm_start, (size_t)&_binary_smm_size); |
| 67 | wbinvd(); |
| 68 | disable_cache(); |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 69 | |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 70 | /* Restore SYSCFG and MTRR */ |
| 71 | wrmsr(SYSCFG_MSR, syscfg_orig); |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 72 | wrmsr(MTRR_FIX_16K_A0000, mtrr_aseg_orig); |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 73 | enable_cache(); |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 74 | |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 75 | /* CPU MSR are set in CPU init */ |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 76 | } |
Rudolf Marek | 2c36627 | 2010-12-18 23:30:59 +0000 | [diff] [blame] | 77 | |
| 78 | void smm_lock(void) |
| 79 | { |
Rudolf Marek | b5b3b3b | 2011-07-02 16:36:17 +0200 | [diff] [blame] | 80 | /* We lock SMM in CPU init */ |
Rudolf Marek | 2c36627 | 2010-12-18 23:30:59 +0000 | [diff] [blame] | 81 | } |