blob: f6aa4eadd4a404a8514e2d98c291efb239e073c8 [file] [log] [blame]
Subrata Banik30a01142023-03-22 00:35:42 +05301/* SPDX-License-Identifier: GPL-2.0-or-later */
2
3#include <console/console.h>
4#include <cpu/x86/mtrr.h>
5#include <ip_checksum.h>
6#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
22#if CMOS_VSTART_ramtop % 8 != 0
23#error "The `ramtop` CMOS entry needs to be byte aligned, check your cmos.layout."
24#endif // CMOS_VSTART_ramtop % 8 != 0
25
Sean Rhodes4265d522023-05-02 20:54:40 +010026#if CMOS_VLEN_ramtop != (10 * 8)
27#error "The `ramtop` CMOS entry needs to be 10 bytes long, check your cmos.layout."
28#endif // CMOS_VLEN_ramtop != (10 * 8)
Sean Rhodes579e03a2023-04-18 21:18:30 +010029
30#else
31#define CMOS_VSTART_ramtop 800
32#endif // CONFIG(USE_OPTION_TABLE)
Subrata Banik30a01142023-03-22 00:35:42 +053033
34struct ramtop_table {
35 uint32_t signature;
36 uint32_t addr;
37 uint16_t checksum;
38} __packed;
39
40/* Read and validate ramtop_table structure from CMOS */
41static int ramtop_cmos_read(struct ramtop_table *ramtop)
42{
43 u8 i, *p;
44 u16 csum;
45
46 for (p = (u8 *)ramtop, i = 0; i < sizeof(*ramtop); i++, p++)
Sean Rhodes579e03a2023-04-18 21:18:30 +010047 *p = cmos_read((CMOS_VSTART_ramtop / 8) + i);
Subrata Banik30a01142023-03-22 00:35:42 +053048
49 /* Verify signature */
50 if (ramtop->signature != RAMTOP_SIGNATURE) {
51 printk(BIOS_DEBUG, "ramtop_table invalid signature\n");
52 return -1;
53 }
54
55 /* Verify checksum over signature and counter only */
56 csum = compute_ip_checksum(ramtop, offsetof(struct ramtop_table, checksum));
57
58 if (csum != ramtop->checksum) {
59 printk(BIOS_DEBUG, "ramtop_table checksum mismatch\n");
60 return -1;
61 }
62
63 return 0;
64}
65
66/* Write ramtop_table structure to CMOS */
67static void ramtop_cmos_write(struct ramtop_table *ramtop)
68{
69 u8 i, *p;
70
71 /* Checksum over signature and counter only */
72 ramtop->checksum = compute_ip_checksum(
73 ramtop, offsetof(struct ramtop_table, checksum));
74
75 for (p = (u8 *)ramtop, i = 0; i < sizeof(*ramtop); i++, p++)
Sean Rhodes579e03a2023-04-18 21:18:30 +010076 cmos_write(*p, (CMOS_VSTART_ramtop / 8) + i);
Subrata Banik30a01142023-03-22 00:35:42 +053077}
78
79/* Update the RAMTOP if required based on the input top_of_ram address */
80void update_ramtop(uint32_t addr)
81{
82 struct ramtop_table ramtop;
83
84 /* Read and update ramtop (if required) */
85 if (ramtop_cmos_read(&ramtop) < 0) {
86 /* Structure invalid, re-initialize */
87 ramtop.signature = RAMTOP_SIGNATURE;
88 ramtop.addr = 0;
89 }
90
91 /* Update ramtop if required */
92 if (ramtop.addr == addr)
93 return;
94
95 ramtop.addr = addr;
96
97 /* Write the new top_of_ram address to CMOS */
98 ramtop_cmos_write(&ramtop);
99
100 printk(BIOS_DEBUG, "Updated the RAMTOP address into CMOS 0x%x\n", ramtop.addr);
101}
102
103static uint32_t get_ramtop_addr(void)
104{
105 struct ramtop_table ramtop;
106
107 if (ramtop_cmos_read(&ramtop) < 0)
108 return 0;
109
110 return ramtop.addr;
111}
112
113/* Early caching of top_of_ram region */
114void early_ramtop_enable_cache_range(void)
115{
116 uint32_t ramtop = get_ramtop_addr();
117 if (!ramtop)
118 return;
119
120 int mtrr = get_free_var_mtrr();
121 if (mtrr == -1) {
122 printk(BIOS_WARNING, "ramtop_table update failure due to no free MTRR available!\n");
123 return;
124 }
125 /*
126 * We need to make sure late romstage (including FSP-M post mem) will be run
127 * cached. Caching 16MB below ramtop is a safe to cover late romstage.
128 */
129 set_var_mtrr(mtrr, ramtop - 16 * MiB, 16 * MiB, MTRR_TYPE_WRBACK);
130}