2 * Copyright (c) 2017 Intel Corporation
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/global_data.h>
12 DECLARE_GLOBAL_DATA_PTR;
15 * SFI tables are part of the first stage bootloader.
17 * U-Boot finds the System Table by searching 16-byte boundaries between
18 * physical address 0x000E0000 and 0x000FFFFF. U-Boot shall search this region
19 * starting at the low address and shall stop searching when the 1st valid SFI
20 * System Table is found.
22 #define SFI_BASE_ADDR 0x000E0000
23 #define SFI_LENGTH 0x00020000
24 #define SFI_TABLE_LENGTH 16
26 static int sfi_table_check(struct sfi_table_header *sbh)
29 char *pos = (char *)sbh;
32 if (sbh->len < SFI_TABLE_LENGTH)
35 if (sbh->len > SFI_LENGTH)
38 for (i = 0; i < sbh->len; i++)
42 pr_err("sfi: Invalid checksum\n");
44 /* Checksum is OK if zero */
45 return chksum ? -EILSEQ : 0;
48 static int sfi_table_is_type(struct sfi_table_header *sbh, const char *signature)
50 return !strncmp(sbh->sig, signature, SFI_SIGNATURE_SIZE) &&
51 !sfi_table_check(sbh);
54 static struct sfi_table_simple *sfi_get_table_by_sig(unsigned long addr,
55 const char *signature)
57 struct sfi_table_simple *sb;
60 for (i = 0; i < SFI_LENGTH; i += SFI_TABLE_LENGTH) {
61 sb = (struct sfi_table_simple *)(addr + i);
62 if (sfi_table_is_type(&sb->header, signature))
69 static struct sfi_table_simple *sfi_search_mmap(void)
71 struct sfi_table_header *sbh;
72 struct sfi_table_simple *sb;
77 sb = sfi_get_table_by_sig(SFI_BASE_ADDR, SFI_SIG_SYST);
79 pr_err("sfi: failed to locate SYST table\n");
83 sys_entry_cnt = (sb->header.len - sizeof(*sbh)) / 8;
85 /* Search through each SYST entry for MMAP table */
86 for (i = 0; i < sys_entry_cnt; i++) {
87 sbh = (struct sfi_table_header *)(unsigned long)sb->pentry[i];
89 if (sfi_table_is_type(sbh, SFI_SIG_MMAP))
90 return (struct sfi_table_simple *)sbh;
93 pr_err("sfi: failed to locate SFI MMAP table\n");
97 #define sfi_for_each_mentry(i, sb, mentry) \
98 for (i = 0, mentry = (struct sfi_mem_entry *)sb->pentry; \
99 i < SFI_GET_NUM_ENTRIES(sb, struct sfi_mem_entry); \
102 static unsigned int sfi_setup_e820(unsigned int max_entries,
103 struct e820_entry *entries)
105 struct sfi_table_simple *sb;
106 struct sfi_mem_entry *mentry;
107 unsigned long long start, end, size;
111 sb = sfi_search_mmap();
115 sfi_for_each_mentry(i, sb, mentry) {
116 start = mentry->phys_start;
117 size = mentry->pages << 12;
123 /* translate SFI mmap type to E820 map type */
124 switch (mentry->type) {
128 case SFI_MEM_UNUSABLE:
129 case SFI_RUNTIME_SERVICE_DATA:
132 type = E820_RESERVED;
135 if (total == E820MAX)
137 entries[total].addr = start;
138 entries[total].size = size;
139 entries[total].type = type;
147 static int sfi_get_bank_size(void)
149 struct sfi_table_simple *sb;
150 struct sfi_mem_entry *mentry;
154 sb = sfi_search_mmap();
158 sfi_for_each_mentry(i, sb, mentry) {
159 if (mentry->type != SFI_MEM_CONV)
162 gd->bd->bi_dram[bank].start = mentry->phys_start;
163 gd->bd->bi_dram[bank].size = mentry->pages << 12;
170 static phys_size_t sfi_get_ram_size(void)
172 struct sfi_table_simple *sb;
173 struct sfi_mem_entry *mentry;
177 sb = sfi_search_mmap();
181 sfi_for_each_mentry(i, sb, mentry) {
182 if (mentry->type != SFI_MEM_CONV)
185 ram += mentry->pages << 12;
188 debug("sfi: RAM size %llu\n", ram);
192 unsigned int install_e820_map(unsigned int max_entries,
193 struct e820_entry *entries)
195 return sfi_setup_e820(max_entries, entries);
198 int dram_init_banksize(void)
206 gd->ram_size = sfi_get_ram_size();