blob: e0352fba8fbe69b5384f7ca64e7f82c45c48cdeb [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Kyösti Mälkkicb08e162013-10-15 17:19:41 +03002
3// Use simple device model for this file even in ramstage
4#define __SIMPLE_DEVICE__
5
Kyösti Mälkkia963acd2019-08-16 20:34:25 +03006#include <arch/romstage.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +03007#include <cbmem.h>
Arthur Heymans874a8f92016-05-19 16:06:09 +02008#include <console/console.h>
Kyösti Mälkki823020d2016-07-22 22:53:19 +03009#include <cpu/x86/mtrr.h>
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030010#include <cpu/x86/smm.h>
Elyes Haouas072c99a2022-10-02 20:19:07 +020011#include <device/pci_ops.h>
Elyes HAOUAS030d3382021-02-12 08:17:35 +010012#include <types.h>
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030013
Elyes Haouas072c99a2022-10-02 20:19:07 +020014#include "i945.h"
15
Arthur Heymansf6d14772018-01-26 11:50:04 +010016/* Decodes TSEG region size to bytes. */
17u32 decode_tseg_size(const u8 esmramc)
18{
19 if (!(esmramc & 1))
20 return 0;
21 switch ((esmramc >> 1) & 3) {
22 case 0:
23 return 1 << 20;
24 case 1:
25 return 2 << 20;
26 case 2:
27 return 8 << 20;
28 case 3:
29 default:
30 die("Bad TSEG setting.\n");
31 }
32}
33
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030034static uintptr_t northbridge_get_tseg_base(void)
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030035{
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020036 uintptr_t tom;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030037
Angel Pons3580d812020-06-11 14:13:33 +020038 if (pci_read_config8(HOST_BRIDGE, DEVEN) & (DEVEN_D2F0 | DEVEN_D2F1))
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030039 /* IGD enabled, get top of Memory from BSM register */
Angel Pons3580d812020-06-11 14:13:33 +020040 tom = pci_read_config32(IGD_DEV, BSM);
Arthur Heymans70a8e342017-03-09 11:30:23 +010041 else
Petr Cvekb30f8682022-04-21 04:31:39 +020042 tom = (pci_read_config8(HOST_BRIDGE, TOLUD) & 0xf8) << 24;
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030043
Elyes HAOUAS8cb5ea72019-12-05 14:33:07 +010044 /* subtract TSEG size */
Angel Pons3580d812020-06-11 14:13:33 +020045 tom -= decode_tseg_size(pci_read_config8(HOST_BRIDGE, ESMRAMC));
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020046 return tom;
47}
48
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030049static size_t northbridge_get_tseg_size(void)
Arthur Heymanscf3076e2018-04-10 12:57:42 +020050{
Angel Pons3580d812020-06-11 14:13:33 +020051 const u8 esmramc = pci_read_config8(HOST_BRIDGE, ESMRAMC);
Arthur Heymanscf3076e2018-04-10 12:57:42 +020052 return decode_tseg_size(esmramc);
53}
54
55/*
56 * Depending of UMA and TSEG configuration, TSEG might start at any
Elyes HAOUAS64f6b712018-08-07 12:16:56 +020057 * 1 MiB alignment. As this may cause very greedy MTRR setup, push
Kyösti Mälkki811932a2016-07-22 22:53:19 +030058 * CBMEM top downwards to 4 MiB boundary.
59 */
Elyes Haouas799c3212022-11-09 14:00:44 +010060uintptr_t cbmem_top_chipset(void)
Kyösti Mälkkif1e3c762014-12-22 12:28:07 +020061{
Elyes Haouas799c3212022-11-09 14:00:44 +010062 return ALIGN_DOWN(northbridge_get_tseg_base(), 4*MiB);
Kyösti Mälkkicb08e162013-10-15 17:19:41 +030063}
Arthur Heymans874a8f92016-05-19 16:06:09 +020064
Elyes HAOUASf49f4d42020-04-28 16:33:55 +020065/* Decodes used Graphics Mode Select (GMS) to kilobytes. */
Arthur Heymans874a8f92016-05-19 16:06:09 +020066u32 decode_igd_memory_size(const u32 gms)
67{
Elyes HAOUASf49f4d42020-04-28 16:33:55 +020068 static const u16 ggc2uma[] = { 0, 1, 4, 8, 16, 32, 48, 64 };
Arthur Heymans874a8f92016-05-19 16:06:09 +020069
Jacob Garberf74f6cb2019-04-08 17:54:35 -060070 if (gms >= ARRAY_SIZE(ggc2uma))
Arthur Heymans874a8f92016-05-19 16:06:09 +020071 die("Bad Graphics Mode Select (GMS) setting.\n");
72
73 return ggc2uma[gms] << 10;
74}
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030075
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030076void smm_region(uintptr_t *start, size_t *size)
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030077{
Kyösti Mälkkid53fd702019-08-14 06:25:55 +030078 *start = northbridge_get_tseg_base();
79 *size = northbridge_get_tseg_size();
Kyösti Mälkkiaba8fb12019-08-02 06:11:28 +030080}
81
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030082void fill_postcar_frame(struct postcar_frame *pcf)
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030083{
Kyösti Mälkki823020d2016-07-22 22:53:19 +030084 uintptr_t top_of_ram;
85
Elyes HAOUASef906092020-02-20 19:41:17 +010086 /* Cache 8 MiB region below the top of RAM and 2 MiB above top of
87 * RAM to cover both cbmem as the TSEG region.
Kyösti Mälkki823020d2016-07-22 22:53:19 +030088 */
89 top_of_ram = (uintptr_t)cbmem_top();
Elyes HAOUASf49f4d42020-04-28 16:33:55 +020090 postcar_frame_add_mtrr(pcf, top_of_ram - 8*MiB, 8*MiB, MTRR_TYPE_WRBACK);
Kyösti Mälkki5bc641a2019-08-09 09:37:49 +030091 postcar_frame_add_mtrr(pcf, northbridge_get_tseg_base(),
Arthur Heymanscf3076e2018-04-10 12:57:42 +020092 northbridge_get_tseg_size(), MTRR_TYPE_WRBACK);
Kyösti Mälkkia4ffe9d2016-06-27 13:24:11 +030093}