Angel Pons | 4b42983 | 2020-04-02 23:48:50 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 3 | |
| 4 | // Use simple device model for this file even in ramstage |
| 5 | #define __SIMPLE_DEVICE__ |
| 6 | |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 7 | #include <device/pci_ops.h> |
Kyösti Mälkki | a963acd | 2019-08-16 20:34:25 +0300 | [diff] [blame] | 8 | #include <arch/romstage.h> |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 9 | #include <cbmem.h> |
| 10 | #include "i945.h" |
Arthur Heymans | 874a8f9 | 2016-05-19 16:06:09 +0200 | [diff] [blame] | 11 | #include <console/console.h> |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 12 | #include <cpu/x86/mtrr.h> |
Kyösti Mälkki | d53fd70 | 2019-08-14 06:25:55 +0300 | [diff] [blame] | 13 | #include <cpu/x86/smm.h> |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 14 | #include <program_loading.h> |
Kyösti Mälkki | f091f4d | 2019-08-14 03:49:21 +0300 | [diff] [blame] | 15 | #include <cpu/intel/smm_reloc.h> |
Kyösti Mälkki | aba8fb1 | 2019-08-02 06:11:28 +0300 | [diff] [blame] | 16 | #include <stdint.h> |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 17 | |
Arthur Heymans | f6d1477 | 2018-01-26 11:50:04 +0100 | [diff] [blame] | 18 | /* Decodes TSEG region size to bytes. */ |
| 19 | u32 decode_tseg_size(const u8 esmramc) |
| 20 | { |
| 21 | if (!(esmramc & 1)) |
| 22 | return 0; |
| 23 | switch ((esmramc >> 1) & 3) { |
| 24 | case 0: |
| 25 | return 1 << 20; |
| 26 | case 1: |
| 27 | return 2 << 20; |
| 28 | case 2: |
| 29 | return 8 << 20; |
| 30 | case 3: |
| 31 | default: |
| 32 | die("Bad TSEG setting.\n"); |
| 33 | } |
| 34 | } |
| 35 | |
Kyösti Mälkki | d53fd70 | 2019-08-14 06:25:55 +0300 | [diff] [blame] | 36 | static uintptr_t northbridge_get_tseg_base(void) |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 37 | { |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 38 | uintptr_t tom; |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 39 | |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 40 | if (pci_read_config8(PCI_DEV(0, 0x0, 0), DEVEN) & (DEVEN_D2F0 | DEVEN_D2F1)) |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 41 | /* IGD enabled, get top of Memory from BSM register */ |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 42 | tom = pci_read_config32(PCI_DEV(0, 2, 0), BSM); |
| 43 | else |
| 44 | tom = (pci_read_config8(PCI_DEV(0, 0, 0), TOLUD) & 0xf7) << 24; |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 45 | |
Elyes HAOUAS | 8cb5ea7 | 2019-12-05 14:33:07 +0100 | [diff] [blame] | 46 | /* subtract TSEG size */ |
Arthur Heymans | f6d1477 | 2018-01-26 11:50:04 +0100 | [diff] [blame] | 47 | tom -= decode_tseg_size(pci_read_config8(PCI_DEV(0, 0, 0), ESMRAMC)); |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 48 | return tom; |
| 49 | } |
| 50 | |
Kyösti Mälkki | d53fd70 | 2019-08-14 06:25:55 +0300 | [diff] [blame] | 51 | static size_t northbridge_get_tseg_size(void) |
Arthur Heymans | cf3076e | 2018-04-10 12:57:42 +0200 | [diff] [blame] | 52 | { |
| 53 | const u8 esmramc = pci_read_config8(PCI_DEV(0, 0, 0), ESMRAMC); |
| 54 | return decode_tseg_size(esmramc); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Depending of UMA and TSEG configuration, TSEG might start at any |
Elyes HAOUAS | 64f6b71 | 2018-08-07 12:16:56 +0200 | [diff] [blame] | 59 | * 1 MiB alignment. As this may cause very greedy MTRR setup, push |
Kyösti Mälkki | 811932a | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 60 | * CBMEM top downwards to 4 MiB boundary. |
| 61 | */ |
Arthur Heymans | 340e4b8 | 2019-10-23 17:25:58 +0200 | [diff] [blame] | 62 | void *cbmem_top_chipset(void) |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 63 | { |
Arthur Heymans | cf3076e | 2018-04-10 12:57:42 +0200 | [diff] [blame] | 64 | uintptr_t top_of_ram = ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB); |
Kyösti Mälkki | 811932a | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 65 | return (void *) top_of_ram; |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 66 | } |
Arthur Heymans | 874a8f9 | 2016-05-19 16:06:09 +0200 | [diff] [blame] | 67 | |
Elyes HAOUAS | f49f4d4 | 2020-04-28 16:33:55 +0200 | [diff] [blame^] | 68 | /* Decodes used Graphics Mode Select (GMS) to kilobytes. */ |
Arthur Heymans | 874a8f9 | 2016-05-19 16:06:09 +0200 | [diff] [blame] | 69 | u32 decode_igd_memory_size(const u32 gms) |
| 70 | { |
Elyes HAOUAS | f49f4d4 | 2020-04-28 16:33:55 +0200 | [diff] [blame^] | 71 | static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32, 48, 64 }; |
Arthur Heymans | 874a8f9 | 2016-05-19 16:06:09 +0200 | [diff] [blame] | 72 | |
Jacob Garber | f74f6cb | 2019-04-08 17:54:35 -0600 | [diff] [blame] | 73 | if (gms >= ARRAY_SIZE(ggc2uma)) |
Arthur Heymans | 874a8f9 | 2016-05-19 16:06:09 +0200 | [diff] [blame] | 74 | die("Bad Graphics Mode Select (GMS) setting.\n"); |
| 75 | |
| 76 | return ggc2uma[gms] << 10; |
| 77 | } |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 78 | |
Kyösti Mälkki | d53fd70 | 2019-08-14 06:25:55 +0300 | [diff] [blame] | 79 | void smm_region(uintptr_t *start, size_t *size) |
Kyösti Mälkki | aba8fb1 | 2019-08-02 06:11:28 +0300 | [diff] [blame] | 80 | { |
Kyösti Mälkki | d53fd70 | 2019-08-14 06:25:55 +0300 | [diff] [blame] | 81 | *start = northbridge_get_tseg_base(); |
| 82 | *size = northbridge_get_tseg_size(); |
Kyösti Mälkki | aba8fb1 | 2019-08-02 06:11:28 +0300 | [diff] [blame] | 83 | } |
| 84 | |
Kyösti Mälkki | 5bc641a | 2019-08-09 09:37:49 +0300 | [diff] [blame] | 85 | void fill_postcar_frame(struct postcar_frame *pcf) |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 86 | { |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 87 | uintptr_t top_of_ram; |
| 88 | |
Elyes HAOUAS | ef90609 | 2020-02-20 19:41:17 +0100 | [diff] [blame] | 89 | /* Cache 8 MiB region below the top of RAM and 2 MiB above top of |
| 90 | * RAM to cover both cbmem as the TSEG region. |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 91 | */ |
| 92 | top_of_ram = (uintptr_t)cbmem_top(); |
Elyes HAOUAS | f49f4d4 | 2020-04-28 16:33:55 +0200 | [diff] [blame^] | 93 | postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB, MTRR_TYPE_WRBACK); |
Kyösti Mälkki | 5bc641a | 2019-08-09 09:37:49 +0300 | [diff] [blame] | 94 | postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(), |
Arthur Heymans | cf3076e | 2018-04-10 12:57:42 +0200 | [diff] [blame] | 95 | northbridge_get_tseg_size(), MTRR_TYPE_WRBACK); |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 96 | } |