Angel Pons | ae59387 | 2020-04-04 18:50:57 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Marc Jones | 1587dc8 | 2017-05-15 18:55:11 -0600 | [diff] [blame] | 3 | |
| 4 | #define __SIMPLE_DEVICE__ |
| 5 | |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 6 | #include <assert.h> |
Marc Jones | 1587dc8 | 2017-05-15 18:55:11 -0600 | [diff] [blame] | 7 | #include <stdint.h> |
Elyes HAOUAS | 20eaef0 | 2019-03-29 17:45:28 +0100 | [diff] [blame] | 8 | #include <console/console.h> |
Marshall Dawson | 94ee937 | 2017-06-15 12:18:23 -0600 | [diff] [blame] | 9 | #include <cpu/x86/msr.h> |
Kyösti Mälkki | b2a5f0b | 2019-08-04 19:54:32 +0300 | [diff] [blame] | 10 | #include <cpu/x86/smm.h> |
Elyes HAOUAS | 400ce55 | 2018-10-12 10:54:30 +0200 | [diff] [blame] | 11 | #include <cpu/amd/msr.h> |
Marshall Dawson | 94ee937 | 2017-06-15 12:18:23 -0600 | [diff] [blame] | 12 | #include <cpu/amd/mtrr.h> |
Marc Jones | 1587dc8 | 2017-05-15 18:55:11 -0600 | [diff] [blame] | 13 | #include <cbmem.h> |
Marshall Dawson | 4b0f6fa | 2018-09-04 13:08:25 -0600 | [diff] [blame] | 14 | #include <arch/bert_storage.h> |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 15 | #include <soc/northbridge.h> |
Marshall Dawson | 69486ca | 2019-05-02 12:03:45 -0600 | [diff] [blame] | 16 | #include <soc/iomap.h> |
| 17 | #include <amdblocks/acpimmio.h> |
Marc Jones | 1587dc8 | 2017-05-15 18:55:11 -0600 | [diff] [blame] | 18 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 19 | #if CONFIG(ACPI_BERT) |
Marshall Dawson | f0de242 | 2018-09-10 13:28:49 -0600 | [diff] [blame] | 20 | #if CONFIG_SMM_TSEG_SIZE == 0x0 |
| 21 | #define BERT_REGION_MAX_SIZE 0x100000 |
| 22 | #else |
| 23 | /* SMM_TSEG_SIZE must stay on a boundary appropriate for its granularity */ |
| 24 | #define BERT_REGION_MAX_SIZE CONFIG_SMM_TSEG_SIZE |
| 25 | #endif |
Marshall Dawson | 4b0f6fa | 2018-09-04 13:08:25 -0600 | [diff] [blame] | 26 | #else |
Marshall Dawson | f0de242 | 2018-09-10 13:28:49 -0600 | [diff] [blame] | 27 | #define BERT_REGION_MAX_SIZE 0 |
Marshall Dawson | 4b0f6fa | 2018-09-04 13:08:25 -0600 | [diff] [blame] | 28 | #endif |
| 29 | |
| 30 | void bert_reserved_region(void **start, size_t *size) |
| 31 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 32 | if (CONFIG(ACPI_BERT)) |
Marshall Dawson | 4b0f6fa | 2018-09-04 13:08:25 -0600 | [diff] [blame] | 33 | *start = cbmem_top(); |
| 34 | else |
Felix Held | 1b457f8 | 2020-04-19 00:02:01 +0200 | [diff] [blame^] | 35 | *start = NULL; |
Marshall Dawson | 4b0f6fa | 2018-09-04 13:08:25 -0600 | [diff] [blame] | 36 | *size = BERT_REGION_MAX_SIZE; |
| 37 | } |
| 38 | |
Arthur Heymans | 340e4b8 | 2019-10-23 17:25:58 +0200 | [diff] [blame] | 39 | void *cbmem_top_chipset(void) |
Marshall Dawson | 94ee937 | 2017-06-15 12:18:23 -0600 | [diff] [blame] | 40 | { |
| 41 | msr_t tom = rdmsr(TOP_MEM); |
| 42 | |
| 43 | if (!tom.lo) |
| 44 | return 0; |
Richard Spiegel | 8c614f2 | 2018-10-23 14:53:23 -0700 | [diff] [blame] | 45 | |
| 46 | /* 8MB alignment to keep MTRR usage low */ |
| 47 | return (void *)ALIGN_DOWN(restore_top_of_low_cacheable() |
| 48 | - CONFIG_SMM_TSEG_SIZE |
| 49 | - BERT_REGION_MAX_SIZE, 8*MiB); |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static uintptr_t smm_region_start(void) |
| 53 | { |
Marshall Dawson | 4b0f6fa | 2018-09-04 13:08:25 -0600 | [diff] [blame] | 54 | return (uintptr_t)cbmem_top() + BERT_REGION_MAX_SIZE; |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static size_t smm_region_size(void) |
| 58 | { |
| 59 | return CONFIG_SMM_TSEG_SIZE; |
| 60 | } |
| 61 | |
Marshall Dawson | f3c57a7c | 2018-01-29 18:08:16 -0700 | [diff] [blame] | 62 | /* |
| 63 | * For data stored in TSEG, ensure TValid is clear so R/W access can reach |
| 64 | * the DRAM when not in SMM. |
| 65 | */ |
| 66 | static void clear_tvalid(void) |
| 67 | { |
| 68 | msr_t hwcr = rdmsr(HWCR_MSR); |
Elyes HAOUAS | 400ce55 | 2018-10-12 10:54:30 +0200 | [diff] [blame] | 69 | msr_t mask = rdmsr(SMM_MASK_MSR); |
Marshall Dawson | f3c57a7c | 2018-01-29 18:08:16 -0700 | [diff] [blame] | 70 | int tvalid = !!(mask.lo & SMM_TSEG_VALID); |
| 71 | |
| 72 | if (hwcr.lo & SMM_LOCK) { |
| 73 | if (!tvalid) /* not valid but locked means still accessible */ |
| 74 | return; |
| 75 | |
| 76 | printk(BIOS_ERR, "Error: can't clear TValid, already locked\n"); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | mask.lo &= ~SMM_TSEG_VALID; |
Elyes HAOUAS | 400ce55 | 2018-10-12 10:54:30 +0200 | [diff] [blame] | 81 | wrmsr(SMM_MASK_MSR, mask); |
Marshall Dawson | f3c57a7c | 2018-01-29 18:08:16 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Kyösti Mälkki | 4913d8a | 2019-08-05 12:49:09 +0300 | [diff] [blame] | 84 | void smm_region(uintptr_t *start, size_t *size) |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 85 | { |
Kyösti Mälkki | 544369e | 2019-08-06 22:14:34 +0300 | [diff] [blame] | 86 | static int once; |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 87 | |
Kyösti Mälkki | 4913d8a | 2019-08-05 12:49:09 +0300 | [diff] [blame] | 88 | *start = smm_region_start(); |
| 89 | *size = smm_region_size(); |
Marshall Dawson | b617211 | 2017-09-13 17:47:31 -0600 | [diff] [blame] | 90 | |
Kyösti Mälkki | 544369e | 2019-08-06 22:14:34 +0300 | [diff] [blame] | 91 | if (!once) { |
| 92 | clear_tvalid(); |
| 93 | once = 1; |
| 94 | } |
Marshall Dawson | 94ee937 | 2017-06-15 12:18:23 -0600 | [diff] [blame] | 95 | } |