f181e8100cb77383120ef583d40537b747fcef89
[platform/kernel/u-boot.git] / arch / x86 / lib / mrccache.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * From coreboot src/southbridge/intel/bd82x6x/mrccache.c
4  *
5  * Copyright (C) 2014 Google Inc.
6  * Copyright (C) 2015 Bin Meng <bmeng.cn@gmail.com>
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <fdtdec.h>
13 #include <log.h>
14 #include <malloc.h>
15 #include <net.h>
16 #include <spi.h>
17 #include <spi_flash.h>
18 #include <asm/mrccache.h>
19 #include <dm/device-internal.h>
20 #include <dm/uclass-internal.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 static uint mrc_block_size(uint data_size)
25 {
26         uint mrc_size = sizeof(struct mrc_data_container) + data_size;
27
28         return ALIGN(mrc_size, MRC_DATA_ALIGN);
29 }
30
31 static struct mrc_data_container *next_mrc_block(
32         struct mrc_data_container *cache)
33 {
34         /* MRC data blocks are aligned within the region */
35         u8 *region_ptr = (u8 *)cache;
36
37         region_ptr += mrc_block_size(cache->data_size);
38
39         return (struct mrc_data_container *)region_ptr;
40 }
41
42 static int is_mrc_cache(struct mrc_data_container *cache)
43 {
44         return cache && (cache->signature == MRC_DATA_SIGNATURE);
45 }
46
47 struct mrc_data_container *mrccache_find_current(struct mrc_region *entry)
48 {
49         struct mrc_data_container *cache, *next;
50         ulong base_addr, end_addr;
51         uint id;
52
53         base_addr = entry->base + entry->offset;
54         end_addr = base_addr + entry->length;
55         cache = NULL;
56
57         /* Search for the last filled entry in the region */
58         for (id = 0, next = (struct mrc_data_container *)base_addr;
59              is_mrc_cache(next);
60              id++) {
61                 cache = next;
62                 next = next_mrc_block(next);
63                 if ((ulong)next >= end_addr)
64                         break;
65         }
66
67         if (id-- == 0) {
68                 debug("%s: No valid MRC cache found.\n", __func__);
69                 return NULL;
70         }
71
72         /* Verify checksum */
73         if (cache->checksum != compute_ip_checksum(cache->data,
74                                                    cache->data_size)) {
75                 printf("%s: MRC cache checksum mismatch\n", __func__);
76                 return NULL;
77         }
78
79         debug("%s: picked entry %u from cache block\n", __func__, id);
80
81         return cache;
82 }
83
84 /**
85  * find_next_mrc_cache() - get next cache entry
86  *
87  * This moves to the next cache entry in the region, making sure it has enough
88  * space to hold data of size @data_size.
89  *
90  * @entry:      MRC cache flash area
91  * @cache:      Entry to start from
92  * @data_size:  Required data size of the new entry. Note that we assume that
93  *      all cache entries are the same size
94  *
95  * @return next cache entry if found, NULL if we got to the end
96  */
97 static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry,
98                 struct mrc_data_container *prev, int data_size)
99 {
100         struct mrc_data_container *cache;
101         ulong base_addr, end_addr;
102
103         base_addr = entry->base + entry->offset;
104         end_addr = base_addr + entry->length;
105
106         /*
107          * We assume that all cache entries are the same size, but let's use
108          * data_size here for clarity.
109          */
110         cache = next_mrc_block(prev);
111         if ((ulong)cache + mrc_block_size(data_size) > end_addr) {
112                 /* Crossed the boundary */
113                 cache = NULL;
114                 debug("%s: no available entries found\n", __func__);
115         } else {
116                 debug("%s: picked next entry from cache block at %p\n",
117                       __func__, cache);
118         }
119
120         return cache;
121 }
122
123 /**
124  * mrccache_update() - update the MRC cache with a new record
125  *
126  * This writes a new record to the end of the MRC cache region. If the new
127  * record is the same as the latest record then the write is skipped
128  *
129  * @sf:         SPI flash to write to
130  * @entry:      Position and size of MRC cache in SPI flash
131  * @cur:        Record to write
132  * @return 0 if updated, -EEXIST if the record is the same as the latest
133  * record, -EINVAL if the record is not valid, other error if SPI write failed
134  */
135 static int mrccache_update(struct udevice *sf, struct mrc_region *entry,
136                            struct mrc_data_container *cur)
137 {
138         struct mrc_data_container *cache;
139         ulong offset;
140         ulong base_addr;
141         int ret;
142
143         if (!is_mrc_cache(cur)) {
144                 debug("%s: Cache data not valid\n", __func__);
145                 return -EINVAL;
146         }
147
148         /* Find the last used block */
149         base_addr = entry->base + entry->offset;
150         debug("Updating MRC cache data\n");
151         cache = mrccache_find_current(entry);
152         if (cache && (cache->data_size == cur->data_size) &&
153             (!memcmp(cache, cur, cache->data_size + sizeof(*cur)))) {
154                 debug("MRC data in flash is up to date. No update\n");
155                 return -EEXIST;
156         }
157
158         /* Move to the next block, which will be the first unused block */
159         if (cache)
160                 cache = find_next_mrc_cache(entry, cache, cur->data_size);
161
162         /*
163          * If we have got to the end, erase the entire mrc-cache area and start
164          * again at block 0.
165          */
166         if (!cache) {
167                 debug("Erasing the MRC cache region of %x bytes at %x\n",
168                       entry->length, entry->offset);
169
170                 ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
171                 if (ret) {
172                         debug("Failed to erase flash region\n");
173                         return ret;
174                 }
175                 cache = (struct mrc_data_container *)base_addr;
176         }
177
178         /* Write the data out */
179         offset = (ulong)cache - base_addr + entry->offset;
180         debug("Write MRC cache update to flash at %lx\n", offset);
181         ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
182                                  cur);
183         if (ret) {
184                 debug("Failed to write to SPI flash\n");
185                 return log_msg_ret("Cannot update mrccache", ret);
186         }
187
188         return 0;
189 }
190
191 static void mrccache_setup(struct mrc_output *mrc, void *data)
192 {
193         struct mrc_data_container *cache = data;
194         u16 checksum;
195
196         cache->signature = MRC_DATA_SIGNATURE;
197         cache->data_size = mrc->len;
198         checksum = compute_ip_checksum(mrc->buf, cache->data_size);
199         debug("Saving %d bytes for MRC output data, checksum %04x\n",
200               cache->data_size, checksum);
201         cache->checksum = checksum;
202         cache->reserved = 0;
203         memcpy(cache->data, mrc->buf, cache->data_size);
204
205         mrc->cache = cache;
206 }
207
208 int mrccache_reserve(void)
209 {
210         int i;
211
212         for (i = 0; i < MRC_TYPE_COUNT; i++) {
213                 struct mrc_output *mrc = &gd->arch.mrc[i];
214
215                 if (!mrc->len)
216                         continue;
217
218                 /* adjust stack pointer to store pure cache data plus header */
219                 gd->start_addr_sp -= (mrc->len + MRC_DATA_HEADER_SIZE);
220                 mrccache_setup(mrc, (void *)gd->start_addr_sp);
221
222                 gd->start_addr_sp &= ~0xf;
223         }
224
225         return 0;
226 }
227
228 int mrccache_get_region(enum mrc_type_t type, struct udevice **devp,
229                         struct mrc_region *entry)
230 {
231         struct udevice *dev;
232         ofnode mrc_node;
233         ulong map_base;
234         uint map_size;
235         uint offset;
236         ofnode node;
237         u32 reg[2];
238         int ret;
239
240         /*
241          * Find the flash chip within the SPI controller node. Avoid probing
242          * the device here since it may put it into a strange state where the
243          * memory map cannot be read.
244          */
245         ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
246         if (ret || !dev) {
247                 /*
248                  * Fall back to searching the device tree since driver model
249                  * may not be ready yet (e.g. with FSPv1)
250                  */
251                 node = ofnode_by_compatible(ofnode_null(), "jedec,spi-nor");
252                 if (!ofnode_valid(node))
253                         return log_msg_ret("Cannot find SPI flash\n", -ENOENT);
254                 ret = -ENODEV;
255         } else {
256                 ret = dm_spi_get_mmap(dev, &map_base, &map_size, &offset);
257                 if (!ret)
258                         entry->base = map_base;
259                 node = dev_ofnode(dev);
260         }
261
262         /*
263          * At this point we have entry->base if ret == 0. If not, then we have
264          * the node and can look for memory-map
265          */
266         if (ret) {
267                 ret = ofnode_read_u32_array(node, "memory-map", reg, 2);
268                 if (ret)
269                         return log_msg_ret("Cannot find memory map\n", ret);
270                 entry->base = reg[0];
271         }
272
273         /* Find the place where we put the MRC cache */
274         mrc_node = ofnode_find_subnode(node, type == MRC_TYPE_NORMAL ?
275                                        "rw-mrc-cache" : "rw-var-mrc-cache");
276         if (!ofnode_valid(mrc_node))
277                 return log_msg_ret("Cannot find node", -EPERM);
278
279         ret = ofnode_read_u32_array(mrc_node, "reg", reg, 2);
280         if (ret)
281                 return log_msg_ret("Cannot find address", ret);
282         entry->offset = reg[0];
283         entry->length = reg[1];
284
285         if (devp)
286                 *devp = dev;
287         debug("MRC cache type %d in '%s', offset %x, len %x, base %x\n",
288               type, dev ? dev->name : ofnode_get_name(node), entry->offset,
289               entry->length, entry->base);
290
291         return 0;
292 }
293
294 static int mrccache_save_type(enum mrc_type_t type)
295 {
296         struct mrc_data_container *cache;
297         struct mrc_output *mrc;
298         struct mrc_region entry;
299         struct udevice *sf;
300         int ret;
301
302         mrc = &gd->arch.mrc[type];
303         if (!mrc->len)
304                 return 0;
305         log_debug("Saving %#x bytes of MRC output data type %d to SPI flash\n",
306                   mrc->len, type);
307         ret = mrccache_get_region(type, &sf, &entry);
308         if (ret)
309                 return log_msg_ret("Cannot get region", ret);
310         ret = device_probe(sf);
311         if (ret)
312                 return log_msg_ret("Cannot probe device", ret);
313         cache = mrc->cache;
314
315         ret = mrccache_update(sf, &entry, cache);
316         if (!ret)
317                 debug("Saved MRC data with checksum %04x\n", cache->checksum);
318         else if (ret == -EEXIST)
319                 debug("MRC data is the same as last time, skipping save\n");
320
321         return 0;
322 }
323
324 int mrccache_save(void)
325 {
326         int i;
327
328         for (i = 0; i < MRC_TYPE_COUNT; i++) {
329                 int ret;
330
331                 ret = mrccache_save_type(i);
332                 if (ret)
333                         return ret;
334         }
335
336         return 0;
337 }
338
339 int mrccache_spl_save(void)
340 {
341         int i;
342
343         for (i = 0; i < MRC_TYPE_COUNT; i++) {
344                 struct mrc_output *mrc = &gd->arch.mrc[i];
345                 void *data;
346                 int size;
347
348                 size = mrc->len + MRC_DATA_HEADER_SIZE;
349                 data = malloc(size);
350                 if (!data)
351                         return log_msg_ret("Allocate MRC cache block", -ENOMEM);
352                 mrccache_setup(mrc, data);
353         }
354
355         return mrccache_save();
356 }