blob: 990df972e2eff5539fef7e4c5ece6103228c2b59 [file] [log] [blame]
Kyösti Mälkkicb08e162013-10-15 17:19:41 +03001/*
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älkkicb08e162013-10-15 17:19:41 +030014 */
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älkki823020d2016-07-22 22:53:19 +030020#include <arch/cpu.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030021#include <cbmem.h>
22#include "i945.h"
Arthur Heymans874a8f92016-05-19 16:06:09 +020023#include <console/console.h>
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030024#include <cpu/intel/romstage.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030025#include <cpu/x86/mtrr.h>
26#include <program_loading.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030027
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020028static uintptr_t smm_region_start(void)
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030029{
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020030 uintptr_t tom;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030031
Arthur Heymans70a8e342017-03-09 11:30:23 +010032 if (pci_read_config8(PCI_DEV(0, 0x0, 0), DEVEN) & (DEVEN_D2F0 | DEVEN_D2F1))
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030033 /* IGD enabled, get top of Memory from BSM register */
Arthur Heymans70a8e342017-03-09 11:30:23 +010034 tom = pci_read_config32(PCI_DEV(0, 2, 0), BSM);
35 else
36 tom = (pci_read_config8(PCI_DEV(0, 0, 0), TOLUD) & 0xf7) << 24;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030037
38 /* if TSEG enabled subtract size */
Elyes HAOUAS8324d872018-01-19 12:52:25 +010039 switch (pci_read_config8(PCI_DEV(0, 0, 0), ESMRAMC) & 0x07) {
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030040 case 0x01:
41 /* 1MB TSEG */
Kyösti Mälkkifd2501b2014-05-31 16:36:29 +030042 tom -= 0x100000;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030043 break;
44 case 0x03:
45 /* 2MB TSEG */
Kyösti Mälkkifd2501b2014-05-31 16:36:29 +030046 tom -= 0x200000;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030047 break;
48 case 0x05:
49 /* 8MB TSEG */
Kyösti Mälkkifd2501b2014-05-31 16:36:29 +030050 tom -= 0x800000;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030051 break;
52 default:
53 /* TSEG either disabled or invalid */
54 break;
55 }
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020056 return tom;
57}
58
Kyösti Mälkki811932a2016-07-22 22:53:19 +030059/* Depending of UMA and TSEG configuration, TSEG might start at any
60 * 1 MiB aligment. As this may cause very greedy MTRR setup, push
61 * CBMEM top downwards to 4 MiB boundary.
62 */
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020063void *cbmem_top(void)
64{
Kyösti Mälkki811932a2016-07-22 22:53:19 +030065 uintptr_t top_of_ram = ALIGN_DOWN(smm_region_start(), 4*MiB);
66 return (void *) top_of_ram;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030067}
Arthur Heymans874a8f92016-05-19 16:06:09 +020068
69/** Decodes used Graphics Mode Select (GMS) to kilobytes. */
70u32 decode_igd_memory_size(const u32 gms)
71{
72 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32,
73 48, 64 };
74
75 if (gms > ARRAY_SIZE(ggc2uma))
76 die("Bad Graphics Mode Select (GMS) setting.\n");
77
78 return ggc2uma[gms] << 10;
79}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030080
Kyösti Mälkki823020d2016-07-22 22:53:19 +030081#define ROMSTAGE_RAM_STACK_SIZE 0x5000
82
83/* setup_stack_and_mtrrs() determines the stack to use after
84 * cache-as-ram is torn down as well as the MTRR settings to use. */
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030085void *setup_stack_and_mtrrs(void)
86{
Kyösti Mälkki823020d2016-07-22 22:53:19 +030087 struct postcar_frame pcf;
88 uintptr_t top_of_ram;
89
90 if (postcar_frame_init(&pcf, ROMSTAGE_RAM_STACK_SIZE))
91 die("Unable to initialize postcar frame.\n");
92
93 /* Cache the ROM as WP just below 4GiB. */
94 postcar_frame_add_mtrr(&pcf, -CACHE_ROM_SIZE, CACHE_ROM_SIZE,
95 MTRR_TYPE_WRPROT);
96
97 /* Cache RAM as WB from 0 -> CACHE_TMP_RAMTOP. */
98 postcar_frame_add_mtrr(&pcf, 0, CACHE_TMP_RAMTOP, MTRR_TYPE_WRBACK);
99
100 /* Cache two separate 4 MiB regions below the top of ram, this
101 * satisfies MTRR alignment requirements. If you modify this to
102 * cover TSEG, make sure UMA region is not set with WRBACK as it
103 * causes hard-to-recover boot failures.
104 */
105 top_of_ram = (uintptr_t)cbmem_top();
106 postcar_frame_add_mtrr(&pcf, top_of_ram - 4*MiB, 4*MiB, MTRR_TYPE_WRBACK);
107 postcar_frame_add_mtrr(&pcf, top_of_ram - 8*MiB, 4*MiB, MTRR_TYPE_WRBACK);
108
109 /* Save the number of MTRRs to setup. Return the stack location
110 * pointing to the number of MTRRs.
111 */
112 return postcar_commit_mtrrs(&pcf);
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300113}