blob: 374b262b1348039dbf0acb47def815fefbc5081d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Felipe Balbie564d592017-07-06 14:41:52 +03002/*
3 * Copyright (c) 2017 Intel Corporation
Felipe Balbie564d592017-07-06 14:41:52 +03004 */
5
6#include <common.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -07007#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Felipe Balbie564d592017-07-06 14:41:52 +03009#include <asm/e820.h>
10#include <asm/global_data.h>
11#include <asm/sfi.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060012#include <linux/printk.h>
Felipe Balbie564d592017-07-06 14:41:52 +030013
14DECLARE_GLOBAL_DATA_PTR;
15
16/*
17 * SFI tables are part of the first stage bootloader.
18 *
19 * U-Boot finds the System Table by searching 16-byte boundaries between
20 * physical address 0x000E0000 and 0x000FFFFF. U-Boot shall search this region
21 * starting at the low address and shall stop searching when the 1st valid SFI
22 * System Table is found.
23 */
24#define SFI_BASE_ADDR 0x000E0000
25#define SFI_LENGTH 0x00020000
26#define SFI_TABLE_LENGTH 16
27
28static int sfi_table_check(struct sfi_table_header *sbh)
29{
30 char chksum = 0;
31 char *pos = (char *)sbh;
32 u32 i;
33
34 if (sbh->len < SFI_TABLE_LENGTH)
35 return -ENXIO;
36
37 if (sbh->len > SFI_LENGTH)
38 return -ENXIO;
39
40 for (i = 0; i < sbh->len; i++)
41 chksum += *pos++;
42
43 if (chksum)
Masahiro Yamada81e10422017-09-16 14:10:41 +090044 pr_err("sfi: Invalid checksum\n");
Felipe Balbie564d592017-07-06 14:41:52 +030045
46 /* Checksum is OK if zero */
47 return chksum ? -EILSEQ : 0;
48}
49
50static int sfi_table_is_type(struct sfi_table_header *sbh, const char *signature)
51{
52 return !strncmp(sbh->sig, signature, SFI_SIGNATURE_SIZE) &&
53 !sfi_table_check(sbh);
54}
55
56static struct sfi_table_simple *sfi_get_table_by_sig(unsigned long addr,
57 const char *signature)
58{
59 struct sfi_table_simple *sb;
60 u32 i;
61
62 for (i = 0; i < SFI_LENGTH; i += SFI_TABLE_LENGTH) {
63 sb = (struct sfi_table_simple *)(addr + i);
64 if (sfi_table_is_type(&sb->header, signature))
65 return sb;
66 }
67
68 return NULL;
69}
70
71static struct sfi_table_simple *sfi_search_mmap(void)
72{
73 struct sfi_table_header *sbh;
74 struct sfi_table_simple *sb;
75 u32 sys_entry_cnt;
76 u32 i;
77
78 /* Find SYST table */
79 sb = sfi_get_table_by_sig(SFI_BASE_ADDR, SFI_SIG_SYST);
80 if (!sb) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090081 pr_err("sfi: failed to locate SYST table\n");
Felipe Balbie564d592017-07-06 14:41:52 +030082 return NULL;
83 }
84
85 sys_entry_cnt = (sb->header.len - sizeof(*sbh)) / 8;
86
87 /* Search through each SYST entry for MMAP table */
88 for (i = 0; i < sys_entry_cnt; i++) {
89 sbh = (struct sfi_table_header *)(unsigned long)sb->pentry[i];
90
91 if (sfi_table_is_type(sbh, SFI_SIG_MMAP))
92 return (struct sfi_table_simple *)sbh;
93 }
94
Masahiro Yamada81e10422017-09-16 14:10:41 +090095 pr_err("sfi: failed to locate SFI MMAP table\n");
Felipe Balbie564d592017-07-06 14:41:52 +030096 return NULL;
97}
98
99#define sfi_for_each_mentry(i, sb, mentry) \
100 for (i = 0, mentry = (struct sfi_mem_entry *)sb->pentry; \
101 i < SFI_GET_NUM_ENTRIES(sb, struct sfi_mem_entry); \
102 i++, mentry++) \
103
Bin Meng3838d712018-04-11 22:02:10 -0700104static unsigned int sfi_setup_e820(unsigned int max_entries,
Bin Meng4b8fc742018-04-11 22:02:11 -0700105 struct e820_entry *entries)
Felipe Balbie564d592017-07-06 14:41:52 +0300106{
107 struct sfi_table_simple *sb;
108 struct sfi_mem_entry *mentry;
109 unsigned long long start, end, size;
110 int type, total = 0;
111 u32 i;
112
113 sb = sfi_search_mmap();
114 if (!sb)
115 return 0;
116
117 sfi_for_each_mentry(i, sb, mentry) {
118 start = mentry->phys_start;
119 size = mentry->pages << 12;
120 end = start + size;
121
122 if (start > end)
123 continue;
124
125 /* translate SFI mmap type to E820 map type */
126 switch (mentry->type) {
127 case SFI_MEM_CONV:
128 type = E820_RAM;
129 break;
130 case SFI_MEM_UNUSABLE:
131 case SFI_RUNTIME_SERVICE_DATA:
132 continue;
133 default:
134 type = E820_RESERVED;
135 }
136
137 if (total == E820MAX)
138 break;
139 entries[total].addr = start;
140 entries[total].size = size;
141 entries[total].type = type;
142
143 total++;
144 }
145
146 return total;
147}
148
149static int sfi_get_bank_size(void)
150{
151 struct sfi_table_simple *sb;
152 struct sfi_mem_entry *mentry;
153 int bank = 0;
154 u32 i;
155
156 sb = sfi_search_mmap();
157 if (!sb)
158 return 0;
159
160 sfi_for_each_mentry(i, sb, mentry) {
161 if (mentry->type != SFI_MEM_CONV)
162 continue;
163
164 gd->bd->bi_dram[bank].start = mentry->phys_start;
165 gd->bd->bi_dram[bank].size = mentry->pages << 12;
166 bank++;
167 }
168
169 return bank;
170}
171
172static phys_size_t sfi_get_ram_size(void)
173{
174 struct sfi_table_simple *sb;
175 struct sfi_mem_entry *mentry;
176 phys_size_t ram = 0;
177 u32 i;
178
179 sb = sfi_search_mmap();
180 if (!sb)
181 return 0;
182
183 sfi_for_each_mentry(i, sb, mentry) {
184 if (mentry->type != SFI_MEM_CONV)
185 continue;
186
187 ram += mentry->pages << 12;
188 }
189
190 debug("sfi: RAM size %llu\n", ram);
191 return ram;
192}
193
Bin Meng3838d712018-04-11 22:02:10 -0700194unsigned int install_e820_map(unsigned int max_entries,
Bin Meng4b8fc742018-04-11 22:02:11 -0700195 struct e820_entry *entries)
Felipe Balbie564d592017-07-06 14:41:52 +0300196{
197 return sfi_setup_e820(max_entries, entries);
198}
199
Andy Shevchenkof65b2ba2020-11-27 14:40:48 +0200200/*
201 * This function looks for the highest region of memory lower than 2GB which
202 * has enough space for U-Boot where U-Boot is aligned on a page boundary. It
203 * overrides the default implementation found elsewhere which simply picks the
204 * end of RAM, wherever that may be. The location of the stack, the relocation
205 * address, and how far U-Boot is moved by relocation are set in the global
206 * data structure.
207 */
Heinrich Schuchardt51a9aac2023-08-12 20:16:58 +0200208phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
Andy Shevchenkof65b2ba2020-11-27 14:40:48 +0200209{
210 struct sfi_table_simple *sb;
211 struct sfi_mem_entry *mentry;
212 ulong dest_addr = 0;
213 u32 i;
214
215 sb = sfi_search_mmap();
216 if (!sb)
217 panic("No available memory found for relocation");
218
219 sfi_for_each_mentry(i, sb, mentry) {
220 unsigned long long start, end;
221
222 if (mentry->type != SFI_MEM_CONV)
223 continue;
224
225 start = mentry->phys_start;
226 end = start + (mentry->pages << 12);
227
228 /* Filter memory over 2GB. */
229 if (end > 0x7fffffffULL)
230 end = 0x80000000ULL;
231 /* Skip this region if it's too small. */
232 if (end - start < total_size)
233 continue;
234
235 /* Use this address if it's the largest so far. */
236 if (end > dest_addr)
237 dest_addr = end;
238 }
239
240 return dest_addr;
241}
242
Felipe Balbie564d592017-07-06 14:41:52 +0300243int dram_init_banksize(void)
244{
245 sfi_get_bank_size();
246 return 0;
247}
248
249int dram_init(void)
250{
251 gd->ram_size = sfi_get_ram_size();
252 return 0;
253}