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