Angel Pons | 4b42983 | 2020-04-02 23:48:50 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Patrick Georgi | 2efc880 | 2012-11-06 11:03:53 +0100 | [diff] [blame] | 2 | |
Kyösti Mälkki | ef84401 | 2013-06-25 23:17:43 +0300 | [diff] [blame] | 3 | // Use simple device model for this file even in ramstage |
| 4 | #define __SIMPLE_DEVICE__ |
| 5 | |
Patrick Georgi | 2efc880 | 2012-11-06 11:03:53 +0100 | [diff] [blame] | 6 | #include <stdint.h> |
Kyösti Mälkki | a963acd | 2019-08-16 20:34:25 +0300 | [diff] [blame] | 7 | #include <arch/romstage.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 8 | #include <device/pci_ops.h> |
Patrick Georgi | 2efc880 | 2012-11-06 11:03:53 +0100 | [diff] [blame] | 9 | #include <device/pci_def.h> |
| 10 | #include <console/console.h> |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 11 | #include <cpu/x86/mtrr.h> |
Kyösti Mälkki | d53fd70 | 2019-08-14 06:25:55 +0300 | [diff] [blame] | 12 | #include <cpu/x86/smm.h> |
Kyösti Mälkki | dcb688e | 2013-09-04 01:11:16 +0300 | [diff] [blame] | 13 | #include <cbmem.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> |
Patrick Georgi | 2efc880 | 2012-11-06 11:03:53 +0100 | [diff] [blame] | 16 | #include "gm45.h" |
| 17 | |
Arthur Heymans | eeaf9e4 | 2016-11-12 20:13:07 +0100 | [diff] [blame] | 18 | /* |
| 19 | * Decodes used Graphics Mode Select (GMS) to kilobytes. |
| 20 | * The options for 1M, 4M, 8M and 16M preallocated igd memory are |
| 21 | * undocumented but are verified to work. |
| 22 | */ |
Patrick Georgi | 2efc880 | 2012-11-06 11:03:53 +0100 | [diff] [blame] | 23 | u32 decode_igd_memory_size(const u32 gms) |
| 24 | { |
Arthur Heymans | eeaf9e4 | 2016-11-12 20:13:07 +0100 | [diff] [blame] | 25 | static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32, 48, 64, 128, 256, |
| 26 | 96, 160, 224, 352 }; |
| 27 | |
Jacob Garber | f74f6cb | 2019-04-08 17:54:35 -0600 | [diff] [blame] | 28 | if (gms >= ARRAY_SIZE(ggc2uma)) |
Patrick Georgi | 2efc880 | 2012-11-06 11:03:53 +0100 | [diff] [blame] | 29 | die("Bad Graphics Mode Select (GMS) setting.\n"); |
Arthur Heymans | eeaf9e4 | 2016-11-12 20:13:07 +0100 | [diff] [blame] | 30 | |
| 31 | return ggc2uma[gms] << 10; |
Patrick Georgi | 2efc880 | 2012-11-06 11:03:53 +0100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /** Decodes used Graphics Stolen Memory (GSM) to kilobytes. */ |
| 35 | u32 decode_igd_gtt_size(const u32 gsm) |
| 36 | { |
| 37 | switch (gsm) { |
| 38 | case 0: |
| 39 | return 0 << 10; |
| 40 | case 1: |
| 41 | return 1 << 10; |
| 42 | case 3: |
| 43 | case 9: |
| 44 | return 2 << 10; |
| 45 | case 10: |
| 46 | return 3 << 10; |
| 47 | case 11: |
| 48 | return 4 << 10; |
| 49 | default: |
| 50 | die("Bad Graphics Stolen Memory (GSM) setting.\n"); |
| 51 | return 0; |
| 52 | } |
| 53 | } |
| 54 | |
Arthur Heymans | 8b76605 | 2018-01-24 23:25:13 +0100 | [diff] [blame] | 55 | /* Decodes TSEG region size to kilobytes. */ |
| 56 | u32 decode_tseg_size(u8 esmramc) |
| 57 | { |
| 58 | if (!(esmramc & 1)) |
| 59 | return 0; |
| 60 | switch ((esmramc >> 1) & 3) { |
| 61 | case 0: |
| 62 | return 1 << 10; |
| 63 | case 1: |
| 64 | return 2 << 10; |
| 65 | case 2: |
| 66 | return 8 << 10; |
| 67 | case 3: |
| 68 | default: |
| 69 | die("Bad TSEG setting.\n"); |
| 70 | } |
| 71 | } |
| 72 | |
Kyösti Mälkki | d53fd70 | 2019-08-14 06:25:55 +0300 | [diff] [blame] | 73 | static uintptr_t northbridge_get_tseg_base(void) |
Patrick Georgi | 2efc880 | 2012-11-06 11:03:53 +0100 | [diff] [blame] | 74 | { |
Kyösti Mälkki | 3f9a62e | 2013-06-20 20:25:21 +0300 | [diff] [blame] | 75 | const pci_devfn_t dev = PCI_DEV(0, 0, 0); |
Patrick Georgi | 2efc880 | 2012-11-06 11:03:53 +0100 | [diff] [blame] | 76 | |
| 77 | u32 tor; |
| 78 | |
| 79 | /* Top of Lower Usable DRAM */ |
| 80 | tor = (pci_read_config16(dev, D0F0_TOLUD) & 0xfff0) << 16; |
| 81 | |
| 82 | /* Graphics memory comes next */ |
| 83 | const u32 ggc = pci_read_config16(dev, D0F0_GGC); |
Arthur Heymans | 8b76605 | 2018-01-24 23:25:13 +0100 | [diff] [blame] | 84 | const u8 esmramc = pci_read_config8(dev, D0F0_ESMRAMC); |
Patrick Georgi | 2efc880 | 2012-11-06 11:03:53 +0100 | [diff] [blame] | 85 | if (!(ggc & 2)) { |
| 86 | /* Graphics memory */ |
| 87 | tor -= decode_igd_memory_size((ggc >> 4) & 0xf) << 10; |
| 88 | /* GTT Graphics Stolen Memory Size (GGMS) */ |
| 89 | tor -= decode_igd_gtt_size((ggc >> 8) & 0xf) << 10; |
| 90 | } |
Arthur Heymans | 8b76605 | 2018-01-24 23:25:13 +0100 | [diff] [blame] | 91 | /* TSEG size */ |
| 92 | tor -= decode_tseg_size(esmramc) << 10; |
Patrick Georgi | 2efc880 | 2012-11-06 11:03:53 +0100 | [diff] [blame] | 93 | return tor; |
| 94 | } |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 95 | |
Kyösti Mälkki | d53fd70 | 2019-08-14 06:25:55 +0300 | [diff] [blame] | 96 | static size_t northbridge_get_tseg_size(void) |
Arthur Heymans | 009518e | 2018-11-27 14:06:21 +0100 | [diff] [blame] | 97 | { |
| 98 | const u8 esmramc = pci_read_config8(PCI_DEV(0, 0, 0), D0F0_ESMRAMC); |
| 99 | return decode_tseg_size(esmramc) << 10; |
| 100 | } |
| 101 | |
Kyösti Mälkki | 811932a | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 102 | /* Depending of UMA and TSEG configuration, TSEG might start at any |
Elyes HAOUAS | 64f6b71 | 2018-08-07 12:16:56 +0200 | [diff] [blame] | 103 | * 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] | 104 | * CBMEM top downwards to 4 MiB boundary. |
| 105 | */ |
Arthur Heymans | 340e4b8 | 2019-10-23 17:25:58 +0200 | [diff] [blame] | 106 | void *cbmem_top_chipset(void) |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 107 | { |
Arthur Heymans | 009518e | 2018-11-27 14:06:21 +0100 | [diff] [blame] | 108 | 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] | 109 | return (void *) top_of_ram; |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 110 | } |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 111 | |
Kyösti Mälkki | d53fd70 | 2019-08-14 06:25:55 +0300 | [diff] [blame] | 112 | void smm_region(uintptr_t *start, size_t *size) |
Kyösti Mälkki | aba8fb1 | 2019-08-02 06:11:28 +0300 | [diff] [blame] | 113 | { |
Kyösti Mälkki | d53fd70 | 2019-08-14 06:25:55 +0300 | [diff] [blame] | 114 | *start = northbridge_get_tseg_base(); |
| 115 | *size = northbridge_get_tseg_size(); |
Kyösti Mälkki | aba8fb1 | 2019-08-02 06:11:28 +0300 | [diff] [blame] | 116 | } |
| 117 | |
Kyösti Mälkki | 5bc641a | 2019-08-09 09:37:49 +0300 | [diff] [blame] | 118 | void fill_postcar_frame(struct postcar_frame *pcf) |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 119 | { |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 120 | uintptr_t top_of_ram; |
| 121 | |
Elyes HAOUAS | ef90609 | 2020-02-20 19:41:17 +0100 | [diff] [blame] | 122 | /* Cache 8 MiB region below the top of RAM and 2 MiB above top of |
| 123 | * RAM to cover both cbmem as the TSEG region. |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 124 | */ |
| 125 | top_of_ram = (uintptr_t)cbmem_top(); |
Kyösti Mälkki | 5bc641a | 2019-08-09 09:37:49 +0300 | [diff] [blame] | 126 | postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB, |
Arthur Heymans | 009518e | 2018-11-27 14:06:21 +0100 | [diff] [blame] | 127 | MTRR_TYPE_WRBACK); |
Kyösti Mälkki | 5bc641a | 2019-08-09 09:37:49 +0300 | [diff] [blame] | 128 | postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(), |
Arthur Heymans | 009518e | 2018-11-27 14:06:21 +0100 | [diff] [blame] | 129 | northbridge_get_tseg_size(), MTRR_TYPE_WRBACK); |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 130 | |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 131 | } |