blob: 0183ea2b3f50832570c14992ac56d65a2f1c151a [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Kyösti Mälkkicb08e162013-10-15 17:19:41 +03003
4// Use simple device model for this file even in ramstage
5#define __SIMPLE_DEVICE__
6
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02007#include <device/pci_ops.h>
Kyösti Mälkkia963acd2019-08-16 20:34:25 +03008#include <arch/romstage.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +03009#include <cbmem.h>
10#include "i945.h"
Arthur Heymans874a8f92016-05-19 16:06:09 +020011#include <console/console.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030012#include <cpu/x86/mtrr.h>
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030013#include <cpu/x86/smm.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +030014#include <program_loading.h>
Kyösti Mälkkif091f4d2019-08-14 03:49:21 +030015#include <cpu/intel/smm_reloc.h>
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030016#include <stdint.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030017
Arthur Heymansf6d14772018-01-26 11:50:04 +010018/* Decodes TSEG region size to bytes. */
19u32 decode_tseg_size(const u8 esmramc)
20{
21 if (!(esmramc & 1))
22 return 0;
23 switch ((esmramc >> 1) & 3) {
24 case 0:
25 return 1 << 20;
26 case 1:
27 return 2 << 20;
28 case 2:
29 return 8 << 20;
30 case 3:
31 default:
32 die("Bad TSEG setting.\n");
33 }
34}
35
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030036static uintptr_t northbridge_get_tseg_base(void)
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030037{
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020038 uintptr_t tom;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030039
Arthur Heymans70a8e342017-03-09 11:30:23 +010040 if (pci_read_config8(PCI_DEV(0, 0x0, 0), DEVEN) & (DEVEN_D2F0 | DEVEN_D2F1))
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030041 /* IGD enabled, get top of Memory from BSM register */
Arthur Heymans70a8e342017-03-09 11:30:23 +010042 tom = pci_read_config32(PCI_DEV(0, 2, 0), BSM);
43 else
44 tom = (pci_read_config8(PCI_DEV(0, 0, 0), TOLUD) & 0xf7) << 24;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030045
Elyes HAOUAS8cb5ea72019-12-05 14:33:07 +010046 /* subtract TSEG size */
Arthur Heymansf6d14772018-01-26 11:50:04 +010047 tom -= decode_tseg_size(pci_read_config8(PCI_DEV(0, 0, 0), ESMRAMC));
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020048 return tom;
49}
50
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030051static size_t northbridge_get_tseg_size(void)
Arthur Heymanscf3076e2018-04-10 12:57:42 +020052{
53 const u8 esmramc = pci_read_config8(PCI_DEV(0, 0, 0), ESMRAMC);
54 return decode_tseg_size(esmramc);
55}
56
57/*
58 * Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +020059 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +030060 * CBMEM top downwards to 4 MiB boundary.
61 */
Arthur Heymans340e4b82019-10-23 17:25:58 +020062void *cbmem_top_chipset(void)
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020063{
Arthur Heymanscf3076e2018-04-10 12:57:42 +020064 uintptr_t top_of_ram = ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
Kyösti Mälkki811932a2016-07-22 22:53:19 +030065 return (void *) top_of_ram;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030066}
Arthur Heymans874a8f92016-05-19 16:06:09 +020067
Elyes HAOUASf49f4d42020-04-28 16:33:55 +020068/* Decodes used Graphics Mode Select (GMS) to kilobytes. */
Arthur Heymans874a8f92016-05-19 16:06:09 +020069u32 decode_igd_memory_size(const u32 gms)
70{
Elyes HAOUASf49f4d42020-04-28 16:33:55 +020071 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32, 48, 64 };
Arthur Heymans874a8f92016-05-19 16:06:09 +020072
Jacob Garberf74f6cb2019-04-08 17:54:35 -060073 if (gms >= ARRAY_SIZE(ggc2uma))
Arthur Heymans874a8f92016-05-19 16:06:09 +020074 die("Bad Graphics Mode Select (GMS) setting.\n");
75
76 return ggc2uma[gms] << 10;
77}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030078
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030079void smm_region(uintptr_t *start, size_t *size)
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030080{
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030081 *start = northbridge_get_tseg_base();
82 *size = northbridge_get_tseg_size();
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030083}
84
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030085void fill_postcar_frame(struct postcar_frame *pcf)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030086{
Kyösti Mälkki823020d2016-07-22 22:53:19 +030087 uintptr_t top_of_ram;
88
Elyes HAOUASef906092020-02-20 19:41:17 +010089 /* Cache 8 MiB region below the top of RAM and 2 MiB above top of
90 * RAM to cover both cbmem as the TSEG region.
Kyösti Mälkki823020d2016-07-22 22:53:19 +030091 */
92 top_of_ram = (uintptr_t)cbmem_top();
Elyes HAOUASf49f4d42020-04-28 16:33:55 +020093 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB, MTRR_TYPE_WRBACK);
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030094 postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(),
Arthur Heymanscf3076e2018-04-10 12:57:42 +020095 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030096}