blob: df3b9e4ec975ffdce9c8781c7b0b8be277e5527b [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>
12
13DECLARE_GLOBAL_DATA_PTR;
14
15/*
16 * SFI tables are part of the first stage bootloader.
17 *
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.
22 */
23#define SFI_BASE_ADDR 0x000E0000
24#define SFI_LENGTH 0x00020000
25#define SFI_TABLE_LENGTH 16
26
27static int sfi_table_check(struct sfi_table_header *sbh)
28{
29 char chksum = 0;
30 char *pos = (char *)sbh;
31 u32 i;
32
33 if (sbh->len < SFI_TABLE_LENGTH)
34 return -ENXIO;
35
36 if (sbh->len > SFI_LENGTH)
37 return -ENXIO;
38
39 for (i = 0; i < sbh->len; i++)
40 chksum += *pos++;
41
42 if (chksum)
Masahiro Yamada81e10422017-09-16 14:10:41 +090043 pr_err("sfi: Invalid checksum\n");
Felipe Balbie564d592017-07-06 14:41:52 +030044
45 /* Checksum is OK if zero */
46 return chksum ? -EILSEQ : 0;
47}
48
49static int sfi_table_is_type(struct sfi_table_header *sbh, const char *signature)
50{
51 return !strncmp(sbh->sig, signature, SFI_SIGNATURE_SIZE) &&
52 !sfi_table_check(sbh);
53}
54
55static struct sfi_table_simple *sfi_get_table_by_sig(unsigned long addr,
56 const char *signature)
57{
58 struct sfi_table_simple *sb;
59 u32 i;
60
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))
64 return sb;
65 }
66
67 return NULL;
68}
69
70static struct sfi_table_simple *sfi_search_mmap(void)
71{
72 struct sfi_table_header *sbh;
73 struct sfi_table_simple *sb;
74 u32 sys_entry_cnt;
75 u32 i;
76
77 /* Find SYST table */
78 sb = sfi_get_table_by_sig(SFI_BASE_ADDR, SFI_SIG_SYST);
79 if (!sb) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090080 pr_err("sfi: failed to locate SYST table\n");
Felipe Balbie564d592017-07-06 14:41:52 +030081 return NULL;
82 }
83
84 sys_entry_cnt = (sb->header.len - sizeof(*sbh)) / 8;
85
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];
89
90 if (sfi_table_is_type(sbh, SFI_SIG_MMAP))
91 return (struct sfi_table_simple *)sbh;
92 }
93
Masahiro Yamada81e10422017-09-16 14:10:41 +090094 pr_err("sfi: failed to locate SFI MMAP table\n");
Felipe Balbie564d592017-07-06 14:41:52 +030095 return NULL;
96}
97
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); \
101 i++, mentry++) \
102
Bin Meng3838d712018-04-11 22:02:10 -0700103static unsigned int sfi_setup_e820(unsigned int max_entries,
Bin Meng4b8fc742018-04-11 22:02:11 -0700104 struct e820_entry *entries)
Felipe Balbie564d592017-07-06 14:41:52 +0300105{
106 struct sfi_table_simple *sb;
107 struct sfi_mem_entry *mentry;
108 unsigned long long start, end, size;
109 int type, total = 0;
110 u32 i;
111
112 sb = sfi_search_mmap();
113 if (!sb)
114 return 0;
115
116 sfi_for_each_mentry(i, sb, mentry) {
117 start = mentry->phys_start;
118 size = mentry->pages << 12;
119 end = start + size;
120
121 if (start > end)
122 continue;
123
124 /* translate SFI mmap type to E820 map type */
125 switch (mentry->type) {
126 case SFI_MEM_CONV:
127 type = E820_RAM;
128 break;
129 case SFI_MEM_UNUSABLE:
130 case SFI_RUNTIME_SERVICE_DATA:
131 continue;
132 default:
133 type = E820_RESERVED;
134 }
135
136 if (total == E820MAX)
137 break;
138 entries[total].addr = start;
139 entries[total].size = size;
140 entries[total].type = type;
141
142 total++;
143 }
144
145 return total;
146}
147
148static int sfi_get_bank_size(void)
149{
150 struct sfi_table_simple *sb;
151 struct sfi_mem_entry *mentry;
152 int bank = 0;
153 u32 i;
154
155 sb = sfi_search_mmap();
156 if (!sb)
157 return 0;
158
159 sfi_for_each_mentry(i, sb, mentry) {
160 if (mentry->type != SFI_MEM_CONV)
161 continue;
162
163 gd->bd->bi_dram[bank].start = mentry->phys_start;
164 gd->bd->bi_dram[bank].size = mentry->pages << 12;
165 bank++;
166 }
167
168 return bank;
169}
170
171static phys_size_t sfi_get_ram_size(void)
172{
173 struct sfi_table_simple *sb;
174 struct sfi_mem_entry *mentry;
175 phys_size_t ram = 0;
176 u32 i;
177
178 sb = sfi_search_mmap();
179 if (!sb)
180 return 0;
181
182 sfi_for_each_mentry(i, sb, mentry) {
183 if (mentry->type != SFI_MEM_CONV)
184 continue;
185
186 ram += mentry->pages << 12;
187 }
188
189 debug("sfi: RAM size %llu\n", ram);
190 return ram;
191}
192
Bin Meng3838d712018-04-11 22:02:10 -0700193unsigned int install_e820_map(unsigned int max_entries,
Bin Meng4b8fc742018-04-11 22:02:11 -0700194 struct e820_entry *entries)
Felipe Balbie564d592017-07-06 14:41:52 +0300195{
196 return sfi_setup_e820(max_entries, entries);
197}
198
199int dram_init_banksize(void)
200{
201 sfi_get_bank_size();
202 return 0;
203}
204
205int dram_init(void)
206{
207 gd->ram_size = sfi_get_ram_size();
208 return 0;
209}