Marc Jones | 1587dc8 | 2017-05-15 18:55:11 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 4 | * Copyright (C) 2015 Intel Corp. |
| 5 | * |
Marc Jones | 1587dc8 | 2017-05-15 18:55:11 -0600 | [diff] [blame] | 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 | |
| 16 | #define __SIMPLE_DEVICE__ |
| 17 | |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 18 | #include <assert.h> |
Marc Jones | 1587dc8 | 2017-05-15 18:55:11 -0600 | [diff] [blame] | 19 | #include <stdint.h> |
Elyes HAOUAS | 20eaef0 | 2019-03-29 17:45:28 +0100 | [diff] [blame] | 20 | #include <console/console.h> |
Marshall Dawson | 94ee937 | 2017-06-15 12:18:23 -0600 | [diff] [blame] | 21 | #include <cpu/x86/msr.h> |
Kyösti Mälkki | b2a5f0b | 2019-08-04 19:54:32 +0300 | [diff] [blame^] | 22 | #include <cpu/x86/smm.h> |
Elyes HAOUAS | 400ce55 | 2018-10-12 10:54:30 +0200 | [diff] [blame] | 23 | #include <cpu/amd/msr.h> |
Marshall Dawson | 94ee937 | 2017-06-15 12:18:23 -0600 | [diff] [blame] | 24 | #include <cpu/amd/mtrr.h> |
Marc Jones | 1587dc8 | 2017-05-15 18:55:11 -0600 | [diff] [blame] | 25 | #include <cbmem.h> |
Marshall Dawson | f3c57a7c | 2018-01-29 18:08:16 -0700 | [diff] [blame] | 26 | #include <stage_cache.h> |
Marshall Dawson | 4b0f6fa | 2018-09-04 13:08:25 -0600 | [diff] [blame] | 27 | #include <arch/bert_storage.h> |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 28 | #include <soc/northbridge.h> |
Marshall Dawson | 69486ca | 2019-05-02 12:03:45 -0600 | [diff] [blame] | 29 | #include <soc/iomap.h> |
| 30 | #include <amdblocks/acpimmio.h> |
Marc Jones | 1587dc8 | 2017-05-15 18:55:11 -0600 | [diff] [blame] | 31 | |
| 32 | void backup_top_of_low_cacheable(uintptr_t ramtop) |
| 33 | { |
Marshall Dawson | 22f54c5 | 2017-11-29 09:30:23 -0700 | [diff] [blame] | 34 | biosram_write32(BIOSRAM_CBMEM_TOP, ramtop); |
Marc Jones | 1587dc8 | 2017-05-15 18:55:11 -0600 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | uintptr_t restore_top_of_low_cacheable(void) |
| 38 | { |
Marshall Dawson | 22f54c5 | 2017-11-29 09:30:23 -0700 | [diff] [blame] | 39 | return biosram_read32(BIOSRAM_CBMEM_TOP); |
Marc Jones | 1587dc8 | 2017-05-15 18:55:11 -0600 | [diff] [blame] | 40 | } |
Marshall Dawson | 94ee937 | 2017-06-15 12:18:23 -0600 | [diff] [blame] | 41 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 42 | #if CONFIG(ACPI_BERT) |
Marshall Dawson | f0de242 | 2018-09-10 13:28:49 -0600 | [diff] [blame] | 43 | #if CONFIG_SMM_TSEG_SIZE == 0x0 |
| 44 | #define BERT_REGION_MAX_SIZE 0x100000 |
| 45 | #else |
| 46 | /* SMM_TSEG_SIZE must stay on a boundary appropriate for its granularity */ |
| 47 | #define BERT_REGION_MAX_SIZE CONFIG_SMM_TSEG_SIZE |
| 48 | #endif |
Marshall Dawson | 4b0f6fa | 2018-09-04 13:08:25 -0600 | [diff] [blame] | 49 | #else |
Marshall Dawson | f0de242 | 2018-09-10 13:28:49 -0600 | [diff] [blame] | 50 | #define BERT_REGION_MAX_SIZE 0 |
Marshall Dawson | 4b0f6fa | 2018-09-04 13:08:25 -0600 | [diff] [blame] | 51 | #endif |
| 52 | |
| 53 | void bert_reserved_region(void **start, size_t *size) |
| 54 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 55 | if (CONFIG(ACPI_BERT)) |
Marshall Dawson | 4b0f6fa | 2018-09-04 13:08:25 -0600 | [diff] [blame] | 56 | *start = cbmem_top(); |
| 57 | else |
| 58 | start = NULL; |
| 59 | *size = BERT_REGION_MAX_SIZE; |
| 60 | } |
| 61 | |
Marshall Dawson | 94ee937 | 2017-06-15 12:18:23 -0600 | [diff] [blame] | 62 | void *cbmem_top(void) |
| 63 | { |
| 64 | msr_t tom = rdmsr(TOP_MEM); |
| 65 | |
| 66 | if (!tom.lo) |
| 67 | return 0; |
Richard Spiegel | 8c614f2 | 2018-10-23 14:53:23 -0700 | [diff] [blame] | 68 | |
| 69 | /* 8MB alignment to keep MTRR usage low */ |
| 70 | return (void *)ALIGN_DOWN(restore_top_of_low_cacheable() |
| 71 | - CONFIG_SMM_TSEG_SIZE |
| 72 | - BERT_REGION_MAX_SIZE, 8*MiB); |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static uintptr_t smm_region_start(void) |
| 76 | { |
Marshall Dawson | 4b0f6fa | 2018-09-04 13:08:25 -0600 | [diff] [blame] | 77 | return (uintptr_t)cbmem_top() + BERT_REGION_MAX_SIZE; |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static size_t smm_region_size(void) |
| 81 | { |
| 82 | return CONFIG_SMM_TSEG_SIZE; |
| 83 | } |
| 84 | |
Marshall Dawson | f3c57a7c | 2018-01-29 18:08:16 -0700 | [diff] [blame] | 85 | void stage_cache_external_region(void **base, size_t *size) |
| 86 | { |
| 87 | if (smm_subregion(SMM_SUBREGION_CACHE, base, size)) { |
| 88 | printk(BIOS_ERR, "ERROR: No cache SMM subregion.\n"); |
| 89 | *base = NULL; |
| 90 | *size = 0; |
| 91 | } |
| 92 | } |
| 93 | |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 94 | void smm_region_info(void **start, size_t *size) |
| 95 | { |
| 96 | *start = (void *)smm_region_start(); |
| 97 | *size = smm_region_size(); |
| 98 | } |
| 99 | |
Marshall Dawson | f3c57a7c | 2018-01-29 18:08:16 -0700 | [diff] [blame] | 100 | /* |
| 101 | * For data stored in TSEG, ensure TValid is clear so R/W access can reach |
| 102 | * the DRAM when not in SMM. |
| 103 | */ |
| 104 | static void clear_tvalid(void) |
| 105 | { |
| 106 | msr_t hwcr = rdmsr(HWCR_MSR); |
Elyes HAOUAS | 400ce55 | 2018-10-12 10:54:30 +0200 | [diff] [blame] | 107 | msr_t mask = rdmsr(SMM_MASK_MSR); |
Marshall Dawson | f3c57a7c | 2018-01-29 18:08:16 -0700 | [diff] [blame] | 108 | int tvalid = !!(mask.lo & SMM_TSEG_VALID); |
| 109 | |
| 110 | if (hwcr.lo & SMM_LOCK) { |
| 111 | if (!tvalid) /* not valid but locked means still accessible */ |
| 112 | return; |
| 113 | |
| 114 | printk(BIOS_ERR, "Error: can't clear TValid, already locked\n"); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | mask.lo &= ~SMM_TSEG_VALID; |
Elyes HAOUAS | 400ce55 | 2018-10-12 10:54:30 +0200 | [diff] [blame] | 119 | wrmsr(SMM_MASK_MSR, mask); |
Marshall Dawson | f3c57a7c | 2018-01-29 18:08:16 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 122 | int smm_subregion(int sub, void **start, size_t *size) |
| 123 | { |
| 124 | uintptr_t sub_base; |
| 125 | size_t sub_size; |
| 126 | const size_t cache_size = CONFIG_SMM_RESERVED_SIZE; |
| 127 | |
| 128 | sub_base = smm_region_start(); |
| 129 | sub_size = smm_region_size(); |
| 130 | |
| 131 | assert(sub_size > CONFIG_SMM_RESERVED_SIZE); |
| 132 | |
| 133 | switch (sub) { |
| 134 | case SMM_SUBREGION_HANDLER: |
| 135 | /* Handler starts at the base of TSEG. */ |
| 136 | sub_size -= cache_size; |
| 137 | break; |
| 138 | case SMM_SUBREGION_CACHE: |
| 139 | /* External cache is in the middle of TSEG. */ |
| 140 | sub_base += sub_size - cache_size; |
| 141 | sub_size = cache_size; |
Marshall Dawson | f3c57a7c | 2018-01-29 18:08:16 -0700 | [diff] [blame] | 142 | clear_tvalid(); |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 143 | break; |
| 144 | default: |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | *start = (void *)sub_base; |
| 149 | *size = sub_size; |
| 150 | |
| 151 | return 0; |
Marshall Dawson | 94ee937 | 2017-06-15 12:18:23 -0600 | [diff] [blame] | 152 | } |