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 | |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 28 | static uintptr_t smm_region_start(void) |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 29 | { |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 30 | uintptr_t tom; |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 31 | |
Kyösti Mälkki | 3c3e34d | 2014-05-31 11:32:54 +0300 | [diff] [blame] | 32 | 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] | 33 | /* IGD enabled, get top of Memory from BSM register */ |
Paul Menzel | 355ce38 | 2014-05-30 13:58:59 +0200 | [diff] [blame] | 34 | tom = pci_read_config32(PCI_DEV(0,2,0), BSM); |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 35 | } else { |
| 36 | tom = (pci_read_config8(PCI_DEV(0,0,0), TOLUD) & 0xf7) << 24; |
| 37 | } |
| 38 | |
| 39 | /* if TSEG enabled subtract size */ |
Kyösti Mälkki | fd2501b | 2014-05-31 16:36:29 +0300 | [diff] [blame] | 40 | switch(pci_read_config8(PCI_DEV(0, 0, 0), ESMRAM) & 0x07) { |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 41 | case 0x01: |
| 42 | /* 1MB TSEG */ |
Kyösti Mälkki | fd2501b | 2014-05-31 16:36:29 +0300 | [diff] [blame] | 43 | tom -= 0x100000; |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 44 | break; |
| 45 | case 0x03: |
| 46 | /* 2MB TSEG */ |
Kyösti Mälkki | fd2501b | 2014-05-31 16:36:29 +0300 | [diff] [blame] | 47 | tom -= 0x200000; |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 48 | break; |
| 49 | case 0x05: |
| 50 | /* 8MB TSEG */ |
Kyösti Mälkki | fd2501b | 2014-05-31 16:36:29 +0300 | [diff] [blame] | 51 | tom -= 0x800000; |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 52 | break; |
| 53 | default: |
| 54 | /* TSEG either disabled or invalid */ |
| 55 | break; |
| 56 | } |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 57 | return tom; |
| 58 | } |
| 59 | |
Kyösti Mälkki | 811932a | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 60 | /* Depending of UMA and TSEG configuration, TSEG might start at any |
| 61 | * 1 MiB aligment. As this may cause very greedy MTRR setup, push |
| 62 | * CBMEM top downwards to 4 MiB boundary. |
| 63 | */ |
Kyösti Mälkki | f1e3c76 | 2014-12-22 12:28:07 +0200 | [diff] [blame] | 64 | void *cbmem_top(void) |
| 65 | { |
Kyösti Mälkki | 811932a | 2016-07-22 22:53:19 +0300 | [diff] [blame] | 66 | uintptr_t top_of_ram = ALIGN_DOWN(smm_region_start(), 4*MiB); |
| 67 | return (void *) top_of_ram; |
Kyösti Mälkki | cb08e16 | 2013-10-15 17:19:41 +0300 | [diff] [blame] | 68 | } |
Arthur Heymans | 874a8f9 | 2016-05-19 16:06:09 +0200 | [diff] [blame] | 69 | |
| 70 | /** Decodes used Graphics Mode Select (GMS) to kilobytes. */ |
| 71 | u32 decode_igd_memory_size(const u32 gms) |
| 72 | { |
| 73 | static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32, |
| 74 | 48, 64 }; |
| 75 | |
| 76 | if (gms > ARRAY_SIZE(ggc2uma)) |
| 77 | die("Bad Graphics Mode Select (GMS) setting.\n"); |
| 78 | |
| 79 | return ggc2uma[gms] << 10; |
| 80 | } |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 81 | |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame^] | 82 | #define ROMSTAGE_RAM_STACK_SIZE 0x5000 |
| 83 | |
| 84 | /* setup_stack_and_mtrrs() determines the stack to use after |
| 85 | * cache-as-ram is torn down as well as the MTRR settings to use. */ |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 86 | void *setup_stack_and_mtrrs(void) |
| 87 | { |
Kyösti Mälkki | 823020d | 2016-07-22 22:53:19 +0300 | [diff] [blame^] | 88 | struct postcar_frame pcf; |
| 89 | uintptr_t top_of_ram; |
| 90 | |
| 91 | if (postcar_frame_init(&pcf, ROMSTAGE_RAM_STACK_SIZE)) |
| 92 | die("Unable to initialize postcar frame.\n"); |
| 93 | |
| 94 | /* Cache the ROM as WP just below 4GiB. */ |
| 95 | postcar_frame_add_mtrr(&pcf, -CACHE_ROM_SIZE, CACHE_ROM_SIZE, |
| 96 | MTRR_TYPE_WRPROT); |
| 97 | |
| 98 | /* Cache RAM as WB from 0 -> CACHE_TMP_RAMTOP. */ |
| 99 | postcar_frame_add_mtrr(&pcf, 0, CACHE_TMP_RAMTOP, MTRR_TYPE_WRBACK); |
| 100 | |
| 101 | /* Cache two separate 4 MiB regions below the top of ram, this |
| 102 | * satisfies MTRR alignment requirements. If you modify this to |
| 103 | * cover TSEG, make sure UMA region is not set with WRBACK as it |
| 104 | * causes hard-to-recover boot failures. |
| 105 | */ |
| 106 | top_of_ram = (uintptr_t)cbmem_top(); |
| 107 | postcar_frame_add_mtrr(&pcf, top_of_ram - 4*MiB, 4*MiB, MTRR_TYPE_WRBACK); |
| 108 | postcar_frame_add_mtrr(&pcf, top_of_ram - 8*MiB, 4*MiB, MTRR_TYPE_WRBACK); |
| 109 | |
| 110 | /* Save the number of MTRRs to setup. Return the stack location |
| 111 | * pointing to the number of MTRRs. |
| 112 | */ |
| 113 | return postcar_commit_mtrrs(&pcf); |
Kyösti Mälkki | a4ffe9d | 2016-06-27 13:24:11 +0300 | [diff] [blame] | 114 | } |