blob: 5f3e2569fb11e4593a393a05a672e1f3917151f5 [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
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020019#include <device/pci_ops.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>
Arthur Heymanscf3076e2018-04-10 12:57:42 +020027#include <cpu/intel/smm/gen1/smi.h>
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030028#include <stdint.h>
29#include <stage_cache.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030030
Arthur Heymansf6d14772018-01-26 11:50:04 +010031/* Decodes TSEG region size to bytes. */
32u32 decode_tseg_size(const u8 esmramc)
33{
34 if (!(esmramc & 1))
35 return 0;
36 switch ((esmramc >> 1) & 3) {
37 case 0:
38 return 1 << 20;
39 case 1:
40 return 2 << 20;
41 case 2:
42 return 8 << 20;
43 case 3:
44 default:
45 die("Bad TSEG setting.\n");
46 }
47}
48
Arthur Heymanscf3076e2018-04-10 12:57:42 +020049u32 northbridge_get_tseg_base(void)
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030050{
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020051 uintptr_t tom;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030052
Arthur Heymans70a8e342017-03-09 11:30:23 +010053 if (pci_read_config8(PCI_DEV(0, 0x0, 0), DEVEN) & (DEVEN_D2F0 | DEVEN_D2F1))
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030054 /* IGD enabled, get top of Memory from BSM register */
Arthur Heymans70a8e342017-03-09 11:30:23 +010055 tom = pci_read_config32(PCI_DEV(0, 2, 0), BSM);
56 else
57 tom = (pci_read_config8(PCI_DEV(0, 0, 0), TOLUD) & 0xf7) << 24;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030058
Arthur Heymansf6d14772018-01-26 11:50:04 +010059 /* subsctract TSEG size */
60 tom -= decode_tseg_size(pci_read_config8(PCI_DEV(0, 0, 0), ESMRAMC));
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020061 return tom;
62}
63
Arthur Heymanscf3076e2018-04-10 12:57:42 +020064u32 northbridge_get_tseg_size(void)
65{
66 const u8 esmramc = pci_read_config8(PCI_DEV(0, 0, 0), ESMRAMC);
67 return decode_tseg_size(esmramc);
68}
69
70/*
71 * Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +020072 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +030073 * CBMEM top downwards to 4 MiB boundary.
74 */
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020075void *cbmem_top(void)
76{
Arthur Heymanscf3076e2018-04-10 12:57:42 +020077 uintptr_t top_of_ram = ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
Kyösti Mälkki811932a2016-07-22 22:53:19 +030078 return (void *) top_of_ram;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030079}
Arthur Heymans874a8f92016-05-19 16:06:09 +020080
81/** Decodes used Graphics Mode Select (GMS) to kilobytes. */
82u32 decode_igd_memory_size(const u32 gms)
83{
84 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32,
85 48, 64 };
86
Jacob Garberf74f6cb2019-04-08 17:54:35 -060087 if (gms >= ARRAY_SIZE(ggc2uma))
Arthur Heymans874a8f92016-05-19 16:06:09 +020088 die("Bad Graphics Mode Select (GMS) setting.\n");
89
90 return ggc2uma[gms] << 10;
91}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030092
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030093void stage_cache_external_region(void **base, size_t *size)
94{
Kyösti Mälkkibccd2b62019-08-02 06:12:03 +030095 /* The stage cache lives at the end of the TSEG region.
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030096 * The top of RAM is defined to be the TSEG base address.
97 */
98 *size = CONFIG_SMM_RESERVED_SIZE;
Kyösti Mälkkibccd2b62019-08-02 06:12:03 +030099 *base = (void *)((uintptr_t)northbridge_get_tseg_base()
100 + northbridge_get_tseg_size() - CONFIG_SMM_RESERVED_SIZE);
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +0300101}
102
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300103void fill_postcar_frame(struct postcar_frame *pcf)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300104{
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300105 uintptr_t top_of_ram;
106
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300107
108 /* Cache the ROM as WP just below 4GiB. */
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300109 postcar_frame_add_romcache(pcf, MTRR_TYPE_WRPROT);
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300110
111 /* Cache RAM as WB from 0 -> CACHE_TMP_RAMTOP. */
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300112 postcar_frame_add_mtrr(pcf, 0, CACHE_TMP_RAMTOP, MTRR_TYPE_WRBACK);
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300113
Arthur Heymanscf3076e2018-04-10 12:57:42 +0200114 /* Cache 8 MiB region below the top of ram and 2 MiB above top of
115 * ram to cover both cbmem as the TSEG region.
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300116 */
117 top_of_ram = (uintptr_t)cbmem_top();
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300118 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB,
Arthur Heymanscf3076e2018-04-10 12:57:42 +0200119 MTRR_TYPE_WRBACK);
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +0300120 postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(),
Arthur Heymanscf3076e2018-04-10 12:57:42 +0200121 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300122
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +0300123}