1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Intel Corporation
8 #include <asm/global_data.h>
11 DECLARE_GLOBAL_DATA_PTR;
14 * SFI tables are part of the first stage bootloader.
16 * U-Boot finds the System Table by searching 16-byte boundaries between
17 * physical address 0x000E0000 and 0x000FFFFF. U-Boot shall search this region
18 * starting at the low address and shall stop searching when the 1st valid SFI
19 * System Table is found.
21 #define SFI_BASE_ADDR 0x000E0000
22 #define SFI_LENGTH 0x00020000
23 #define SFI_TABLE_LENGTH 16
25 static int sfi_table_check(struct sfi_table_header *sbh)
28 char *pos = (char *)sbh;
31 if (sbh->len < SFI_TABLE_LENGTH)
34 if (sbh->len > SFI_LENGTH)
37 for (i = 0; i < sbh->len; i++)
41 pr_err("sfi: Invalid checksum\n");
43 /* Checksum is OK if zero */
44 return chksum ? -EILSEQ : 0;
47 static int sfi_table_is_type(struct sfi_table_header *sbh, const char *signature)
49 return !strncmp(sbh->sig, signature, SFI_SIGNATURE_SIZE) &&
50 !sfi_table_check(sbh);
53 static struct sfi_table_simple *sfi_get_table_by_sig(unsigned long addr,
54 const char *signature)
56 struct sfi_table_simple *sb;
59 for (i = 0; i < SFI_LENGTH; i += SFI_TABLE_LENGTH) {
60 sb = (struct sfi_table_simple *)(addr + i);
61 if (sfi_table_is_type(&sb->header, signature))
68 static struct sfi_table_simple *sfi_search_mmap(void)
70 struct sfi_table_header *sbh;
71 struct sfi_table_simple *sb;
76 sb = sfi_get_table_by_sig(SFI_BASE_ADDR, SFI_SIG_SYST);
78 pr_err("sfi: failed to locate SYST table\n");
82 sys_entry_cnt = (sb->header.len - sizeof(*sbh)) / 8;
84 /* Search through each SYST entry for MMAP table */
85 for (i = 0; i < sys_entry_cnt; i++) {
86 sbh = (struct sfi_table_header *)(unsigned long)sb->pentry[i];
88 if (sfi_table_is_type(sbh, SFI_SIG_MMAP))
89 return (struct sfi_table_simple *)sbh;
92 pr_err("sfi: failed to locate SFI MMAP table\n");
96 #define sfi_for_each_mentry(i, sb, mentry) \
97 for (i = 0, mentry = (struct sfi_mem_entry *)sb->pentry; \
98 i < SFI_GET_NUM_ENTRIES(sb, struct sfi_mem_entry); \
101 static unsigned int sfi_setup_e820(unsigned int max_entries,
102 struct e820_entry *entries)
104 struct sfi_table_simple *sb;
105 struct sfi_mem_entry *mentry;
106 unsigned long long start, end, size;
110 sb = sfi_search_mmap();
114 sfi_for_each_mentry(i, sb, mentry) {
115 start = mentry->phys_start;
116 size = mentry->pages << 12;
122 /* translate SFI mmap type to E820 map type */
123 switch (mentry->type) {
127 case SFI_MEM_UNUSABLE:
128 case SFI_RUNTIME_SERVICE_DATA:
131 type = E820_RESERVED;
134 if (total == E820MAX)
136 entries[total].addr = start;
137 entries[total].size = size;
138 entries[total].type = type;
146 static int sfi_get_bank_size(void)
148 struct sfi_table_simple *sb;
149 struct sfi_mem_entry *mentry;
153 sb = sfi_search_mmap();
157 sfi_for_each_mentry(i, sb, mentry) {
158 if (mentry->type != SFI_MEM_CONV)
161 gd->bd->bi_dram[bank].start = mentry->phys_start;
162 gd->bd->bi_dram[bank].size = mentry->pages << 12;
169 static phys_size_t sfi_get_ram_size(void)
171 struct sfi_table_simple *sb;
172 struct sfi_mem_entry *mentry;
176 sb = sfi_search_mmap();
180 sfi_for_each_mentry(i, sb, mentry) {
181 if (mentry->type != SFI_MEM_CONV)
184 ram += mentry->pages << 12;
187 debug("sfi: RAM size %llu\n", ram);
191 unsigned int install_e820_map(unsigned int max_entries,
192 struct e820_entry *entries)
194 return sfi_setup_e820(max_entries, entries);
197 int dram_init_banksize(void)
205 gd->ram_size = sfi_get_ram_size();