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 | |
| 19 | #include <arch/io.h> |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 20 | #include <arch/cpu.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 | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 24 | #include <cpu/intel/romstage.h> |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 25 | #include <cpu/x86/mtrr.h> |
| 26 | #include <program_loading.h> |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 27 | |
Arthur Heymans | f6d1477 | 2018-01-26 11:50:04 +0100 | [diff] [blame] | 28 | /* Decodes TSEG region size to bytes. */ |
| 29 | u32 decode_tseg_size(const u8 esmramc) |
| 30 | { |
| 31 | if (!(esmramc & 1)) |
| 32 | return 0; |
| 33 | switch ((esmramc >> 1) & 3) { |
| 34 | case 0: |
| 35 | return 1 << 20; |
| 36 | case 1: |
| 37 | return 2 << 20; |
| 38 | case 2: |
| 39 | return 8 << 20; |
| 40 | case 3: |
| 41 | default: |
| 42 | die("Bad TSEG setting.\n"); |
| 43 | } |
| 44 | } |
| 45 | |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 46 | static uintptr_t smm_region_start(void) |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 47 | { |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 48 | uintptr_t tom; |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 49 | |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 50 | 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] | 51 | /* IGD enabled, get top of Memory from BSM register */ |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 52 | tom = pci_read_config32(PCI_DEV(0, 2, 0), BSM); |
| 53 | else |
| 54 | 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] | 55 | |
Arthur Heymans | f6d1477 | 2018-01-26 11:50:04 +0100 | [diff] [blame] | 56 | /* subsctract TSEG size */ |
| 57 | 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] | 58 | return tom; |
| 59 | } |
| 60 | |
Kyösti Mälkki | 811932a | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 61 | /* Depending of UMA and TSEG configuration, TSEG might start at any |
| 62 | * 1 MiB aligment. As this may cause very greedy MTRR setup, push |
| 63 | * CBMEM top downwards to 4 MiB boundary. |
| 64 | */ |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 65 | void *cbmem_top(void) |
| 66 | { |
Kyösti Mälkki | 811932a | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 67 | uintptr_t top_of_ram = ALIGN_DOWN(smm_region_start(), 4*MiB); |
| 68 | return (void *) top_of_ram; |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 69 | } |
Arthur Heymans | 874a8f9 | 2016-05-19 16:06:09 +0200 | [diff] [blame] | 70 | |
| 71 | /** Decodes used Graphics Mode Select (GMS) to kilobytes. */ |
| 72 | u32 decode_igd_memory_size(const u32 gms) |
| 73 | { |
| 74 | static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32, |
| 75 | 48, 64 }; |
| 76 | |
| 77 | if (gms > ARRAY_SIZE(ggc2uma)) |
| 78 | die("Bad Graphics Mode Select (GMS) setting.\n"); |
| 79 | |
| 80 | return ggc2uma[gms] << 10; |
| 81 | } |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 82 | |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 83 | #define ROMSTAGE_RAM_STACK_SIZE 0x5000 |
| 84 | |
Arthur Heymans | 2dcc3a5 | 2018-06-03 10:39:16 +0200 | [diff] [blame] | 85 | /* platform_enter_postcar() determines the stack to use after |
| 86 | * cache-as-ram is torn down as well as the MTRR settings to use, |
| 87 | * and continues execution in postcar stage. */ |
| 88 | void platform_enter_postcar(void) |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 89 | { |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 90 | struct postcar_frame pcf; |
| 91 | uintptr_t top_of_ram; |
| 92 | |
| 93 | if (postcar_frame_init(&pcf, ROMSTAGE_RAM_STACK_SIZE)) |
| 94 | die("Unable to initialize postcar frame.\n"); |
| 95 | |
| 96 | /* Cache the ROM as WP just below 4GiB. */ |
Nico Huber | 089b908 | 2018-05-27 14:37:32 +0200 | [diff] [blame] | 97 | postcar_frame_add_romcache(&pcf, MTRR_TYPE_WRPROT); |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 98 | |
| 99 | /* Cache RAM as WB from 0 -> CACHE_TMP_RAMTOP. */ |
| 100 | postcar_frame_add_mtrr(&pcf, 0, CACHE_TMP_RAMTOP, MTRR_TYPE_WRBACK); |
| 101 | |
| 102 | /* Cache two separate 4 MiB regions below the top of ram, this |
| 103 | * satisfies MTRR alignment requirements. If you modify this to |
| 104 | * cover TSEG, make sure UMA region is not set with WRBACK as it |
| 105 | * causes hard-to-recover boot failures. |
| 106 | */ |
| 107 | top_of_ram = (uintptr_t)cbmem_top(); |
| 108 | postcar_frame_add_mtrr(&pcf, top_of_ram - 4*MiB, 4*MiB, MTRR_TYPE_WRBACK); |
| 109 | postcar_frame_add_mtrr(&pcf, top_of_ram - 8*MiB, 4*MiB, MTRR_TYPE_WRBACK); |
| 110 | |
Arthur Heymans | 2dcc3a5 | 2018-06-03 10:39:16 +0200 | [diff] [blame] | 111 | run_postcar_phase(&pcf); |
| 112 | |
| 113 | /* We do not return here. */ |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 114 | } |