blob: a8ef065fb22faf9986c347ab0427a4ebfd3f0139 [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
Kyösti Mälkki3c3e34d2014-05-31 11:32:54 +030032 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 */
Paul Menzel355ce382014-05-30 13:58:59 +020034 tom = pci_read_config32(PCI_DEV(0,2,0), BSM);
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030035 } 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älkkifd2501b2014-05-31 16:36:29 +030040 switch(pci_read_config8(PCI_DEV(0, 0, 0), ESMRAM) & 0x07) {
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030041 case 0x01:
42 /* 1MB TSEG */
Kyösti Mälkkifd2501b2014-05-31 16:36:29 +030043 tom -= 0x100000;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030044 break;
45 case 0x03:
46 /* 2MB TSEG */
Kyösti Mälkkifd2501b2014-05-31 16:36:29 +030047 tom -= 0x200000;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030048 break;
49 case 0x05:
50 /* 8MB TSEG */
Kyösti Mälkkifd2501b2014-05-31 16:36:29 +030051 tom -= 0x800000;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030052 break;
53 default:
54 /* TSEG either disabled or invalid */
55 break;
56 }
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020057 return tom;
58}
59
Kyösti Mälkki811932a2016-07-22 22:53:19 +030060/* 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älkkif1e3c762014-12-22 12:28:07 +020064void *cbmem_top(void)
65{
Kyösti Mälkki811932a2016-07-22 22:53:19 +030066 uintptr_t top_of_ram = ALIGN_DOWN(smm_region_start(), 4*MiB);
67 return (void *) top_of_ram;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030068}
Arthur Heymans874a8f92016-05-19 16:06:09 +020069
70/** Decodes used Graphics Mode Select (GMS) to kilobytes. */
71u32 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älkkia4ffe9d2016-06-27 13:24:11 +030081
Kyösti Mälkki823020d2016-07-22 22:53:19 +030082#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älkkia4ffe9d2016-06-27 13:24:11 +030086void *setup_stack_and_mtrrs(void)
87{
Kyösti Mälkki823020d2016-07-22 22:53:19 +030088 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älkkia4ffe9d2016-06-27 13:24:11 +0300114}