blob: ec326bb1c0ffc5ce1a7d22882d9a4d1d3f7ce056 [file] [log] [blame]
Subrata Banik30a01142023-03-22 00:35:42 +05301/* SPDX-License-Identifier: GPL-2.0-or-later */
2
Julius Wernerde371092024-01-30 16:51:05 -08003#include <commonlib/bsd/ipchksum.h>
Subrata Banik30a01142023-03-22 00:35:42 +05304#include <console/console.h>
5#include <cpu/x86/mtrr.h>
Subrata Banik30a01142023-03-22 00:35:42 +05306#include <intelbasecode/ramtop.h>
7#include <pc80/mc146818rtc.h>
8#include <stdint.h>
9
10/* We need a region in CMOS to store the RAMTOP address */
11
12#define RAMTOP_SIGNATURE 0x52544F50 /* 'RTOP' */
13
Subrata Banik30a01142023-03-22 00:35:42 +053014/*
Sean Rhodesbc602b82023-04-19 08:41:24 +010015 * Address of the ramtop byte in CMOS. Should be reserved
Subrata Banik30a01142023-03-22 00:35:42 +053016 * in mainboards' cmos.layout and not covered by checksum.
17 */
18
19#if CONFIG(USE_OPTION_TABLE)
20#include "option_table.h"
Sean Rhodes579e03a2023-04-18 21:18:30 +010021
Sean Rhodesbfe2cd42023-05-02 21:02:43 +010022#ifndef CMOS_VSTART_ramtop
23#error "The `ramtop` CMOS entry is missing, please add it to your cmos.layout."
24#endif
25
Sean Rhodes579e03a2023-04-18 21:18:30 +010026#if CMOS_VSTART_ramtop % 8 != 0
27#error "The `ramtop` CMOS entry needs to be byte aligned, check your cmos.layout."
28#endif // CMOS_VSTART_ramtop % 8 != 0
29
Sean Rhodes4265d522023-05-02 20:54:40 +010030#if CMOS_VLEN_ramtop != (10 * 8)
31#error "The `ramtop` CMOS entry needs to be 10 bytes long, check your cmos.layout."
32#endif // CMOS_VLEN_ramtop != (10 * 8)
Sean Rhodes579e03a2023-04-18 21:18:30 +010033
34#else
35#define CMOS_VSTART_ramtop 800
36#endif // CONFIG(USE_OPTION_TABLE)
Subrata Banik30a01142023-03-22 00:35:42 +053037
38struct ramtop_table {
39 uint32_t signature;
40 uint32_t addr;
41 uint16_t checksum;
42} __packed;
43
44/* Read and validate ramtop_table structure from CMOS */
45static int ramtop_cmos_read(struct ramtop_table *ramtop)
46{
47 u8 i, *p;
48 u16 csum;
49
50 for (p = (u8 *)ramtop, i = 0; i < sizeof(*ramtop); i++, p++)
Sean Rhodes579e03a2023-04-18 21:18:30 +010051 *p = cmos_read((CMOS_VSTART_ramtop / 8) + i);
Subrata Banik30a01142023-03-22 00:35:42 +053052
53 /* Verify signature */
54 if (ramtop->signature != RAMTOP_SIGNATURE) {
55 printk(BIOS_DEBUG, "ramtop_table invalid signature\n");
56 return -1;
57 }
58
59 /* Verify checksum over signature and counter only */
Julius Wernerde371092024-01-30 16:51:05 -080060 csum = ipchksum(ramtop, offsetof(struct ramtop_table, checksum));
Subrata Banik30a01142023-03-22 00:35:42 +053061
62 if (csum != ramtop->checksum) {
63 printk(BIOS_DEBUG, "ramtop_table checksum mismatch\n");
64 return -1;
65 }
66
67 return 0;
68}
69
70/* Write ramtop_table structure to CMOS */
71static void ramtop_cmos_write(struct ramtop_table *ramtop)
72{
73 u8 i, *p;
74
75 /* Checksum over signature and counter only */
Julius Wernerde371092024-01-30 16:51:05 -080076 ramtop->checksum = ipchksum(ramtop, offsetof(struct ramtop_table, checksum));
Subrata Banik30a01142023-03-22 00:35:42 +053077
78 for (p = (u8 *)ramtop, i = 0; i < sizeof(*ramtop); i++, p++)
Sean Rhodes579e03a2023-04-18 21:18:30 +010079 cmos_write(*p, (CMOS_VSTART_ramtop / 8) + i);
Subrata Banik30a01142023-03-22 00:35:42 +053080}
81
82/* Update the RAMTOP if required based on the input top_of_ram address */
83void update_ramtop(uint32_t addr)
84{
85 struct ramtop_table ramtop;
86
87 /* Read and update ramtop (if required) */
88 if (ramtop_cmos_read(&ramtop) < 0) {
89 /* Structure invalid, re-initialize */
90 ramtop.signature = RAMTOP_SIGNATURE;
91 ramtop.addr = 0;
92 }
93
94 /* Update ramtop if required */
95 if (ramtop.addr == addr)
96 return;
97
98 ramtop.addr = addr;
99
100 /* Write the new top_of_ram address to CMOS */
101 ramtop_cmos_write(&ramtop);
102
103 printk(BIOS_DEBUG, "Updated the RAMTOP address into CMOS 0x%x\n", ramtop.addr);
104}
105
Pratikkumar Prajapatiea66c982023-06-05 18:10:37 -0700106uint32_t get_ramtop_addr(void)
Subrata Banik30a01142023-03-22 00:35:42 +0530107{
108 struct ramtop_table ramtop;
109
110 if (ramtop_cmos_read(&ramtop) < 0)
111 return 0;
112
113 return ramtop.addr;
114}
115
116/* Early caching of top_of_ram region */
117void early_ramtop_enable_cache_range(void)
118{
119 uint32_t ramtop = get_ramtop_addr();
120 if (!ramtop)
121 return;
122
123 int mtrr = get_free_var_mtrr();
124 if (mtrr == -1) {
125 printk(BIOS_WARNING, "ramtop_table update failure due to no free MTRR available!\n");
126 return;
127 }
128 /*
129 * We need to make sure late romstage (including FSP-M post mem) will be run
130 * cached. Caching 16MB below ramtop is a safe to cover late romstage.
131 */
132 set_var_mtrr(mtrr, ramtop - 16 * MiB, 16 * MiB, MTRR_TYPE_WRBACK);
133}