1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Intel Corporation
9 #include <asm/global_data.h>
11 #include <linux/printk.h>
13 DECLARE_GLOBAL_DATA_PTR;
16 * SFI tables are part of the first stage bootloader.
18 * U-Boot finds the System Table by searching 16-byte boundaries between
19 * physical address 0x000E0000 and 0x000FFFFF. U-Boot shall search this region
20 * starting at the low address and shall stop searching when the 1st valid SFI
21 * System Table is found.
23 #define SFI_BASE_ADDR 0x000E0000
24 #define SFI_LENGTH 0x00020000
25 #define SFI_TABLE_LENGTH 16
27 static int sfi_table_check(struct sfi_table_header *sbh)
30 char *pos = (char *)sbh;
33 if (sbh->len < SFI_TABLE_LENGTH)
36 if (sbh->len > SFI_LENGTH)
39 for (i = 0; i < sbh->len; i++)
43 pr_err("sfi: Invalid checksum\n");
45 /* Checksum is OK if zero */
46 return chksum ? -EILSEQ : 0;
49 static int sfi_table_is_type(struct sfi_table_header *sbh, const char *signature)
51 return !strncmp(sbh->sig, signature, SFI_SIGNATURE_SIZE) &&
52 !sfi_table_check(sbh);
55 static struct sfi_table_simple *sfi_get_table_by_sig(unsigned long addr,
56 const char *signature)
58 struct sfi_table_simple *sb;
61 for (i = 0; i < SFI_LENGTH; i += SFI_TABLE_LENGTH) {
62 sb = (struct sfi_table_simple *)(addr + i);
63 if (sfi_table_is_type(&sb->header, signature))
70 static struct sfi_table_simple *sfi_search_mmap(void)
72 struct sfi_table_header *sbh;
73 struct sfi_table_simple *sb;
78 sb = sfi_get_table_by_sig(SFI_BASE_ADDR, SFI_SIG_SYST);
80 pr_err("sfi: failed to locate SYST table\n");
84 sys_entry_cnt = (sb->header.len - sizeof(*sbh)) / 8;
86 /* Search through each SYST entry for MMAP table */
87 for (i = 0; i < sys_entry_cnt; i++) {
88 sbh = (struct sfi_table_header *)(unsigned long)sb->pentry[i];
90 if (sfi_table_is_type(sbh, SFI_SIG_MMAP))
91 return (struct sfi_table_simple *)sbh;
94 pr_err("sfi: failed to locate SFI MMAP table\n");
98 #define sfi_for_each_mentry(i, sb, mentry) \
99 for (i = 0, mentry = (struct sfi_mem_entry *)sb->pentry; \
100 i < SFI_GET_NUM_ENTRIES(sb, struct sfi_mem_entry); \
103 static unsigned int sfi_setup_e820(unsigned int max_entries,
104 struct e820_entry *entries)
106 struct sfi_table_simple *sb;
107 struct sfi_mem_entry *mentry;
108 unsigned long long start, end, size;
112 sb = sfi_search_mmap();
116 sfi_for_each_mentry(i, sb, mentry) {
117 start = mentry->phys_start;
118 size = mentry->pages << 12;
124 /* translate SFI mmap type to E820 map type */
125 switch (mentry->type) {
129 case SFI_MEM_UNUSABLE:
130 case SFI_RUNTIME_SERVICE_DATA:
133 type = E820_RESERVED;
136 if (total == E820MAX)
138 entries[total].addr = start;
139 entries[total].size = size;
140 entries[total].type = type;
148 static int sfi_get_bank_size(void)
150 struct sfi_table_simple *sb;
151 struct sfi_mem_entry *mentry;
155 sb = sfi_search_mmap();
159 sfi_for_each_mentry(i, sb, mentry) {
160 if (mentry->type != SFI_MEM_CONV)
163 gd->bd->bi_dram[bank].start = mentry->phys_start;
164 gd->bd->bi_dram[bank].size = mentry->pages << 12;
171 static phys_size_t sfi_get_ram_size(void)
173 struct sfi_table_simple *sb;
174 struct sfi_mem_entry *mentry;
178 sb = sfi_search_mmap();
182 sfi_for_each_mentry(i, sb, mentry) {
183 if (mentry->type != SFI_MEM_CONV)
186 ram += mentry->pages << 12;
189 debug("sfi: RAM size %llu\n", ram);
193 unsigned int install_e820_map(unsigned int max_entries,
194 struct e820_entry *entries)
196 return sfi_setup_e820(max_entries, entries);
200 * This function looks for the highest region of memory lower than 2GB which
201 * has enough space for U-Boot where U-Boot is aligned on a page boundary. It
202 * overrides the default implementation found elsewhere which simply picks the
203 * end of RAM, wherever that may be. The location of the stack, the relocation
204 * address, and how far U-Boot is moved by relocation are set in the global
207 phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
209 struct sfi_table_simple *sb;
210 struct sfi_mem_entry *mentry;
214 sb = sfi_search_mmap();
216 panic("No available memory found for relocation");
218 sfi_for_each_mentry(i, sb, mentry) {
219 unsigned long long start, end;
221 if (mentry->type != SFI_MEM_CONV)
224 start = mentry->phys_start;
225 end = start + (mentry->pages << 12);
227 /* Filter memory over 2GB. */
228 if (end > 0x7fffffffULL)
230 /* Skip this region if it's too small. */
231 if (end - start < total_size)
234 /* Use this address if it's the largest so far. */
242 int dram_init_banksize(void)
250 gd->ram_size = sfi_get_ram_size();