Debian Codegraph @

src/soc/ibm/power9/rom_media.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <string.h>
4 #include <boot_device.h>
5 #include <arch/io.h>
6 #include <console/console.h>
7 #include <endian.h>
8 #include <cbfs.h>
9 #include <symbols.h>
10 #include "../../../../3rdparty/ffs/ffs/ffs.h"
11
12 #define LPC_FLASH_MIN (MMIO_GROUP0_CHIP0_LPC_BASE_ADDR + LPCHC_FW_SPACE)
13 #define LPC_FLASH_TOP (LPC_FLASH_MIN + FW_SPACE_SIZE)
14
15 #define CBFS_PARTITION_NAME "HBI"
16
17 /* ffs_entry is not complete in included ffs.h, it lacks user data layout.
18 * See https://github.com/open-power/skiboot/blob/master/libflash/ffs.h */
19
20 /* Data integrity flags */
21 #define FFS_ENRY_INTEG_ECC 0x8000
22
23 /* Version Checking : 1 byte */
24 #define FFS_VERS_SHA512 0x80
25
26 enum ecc_status {
27 CLEAN = 0, //< No ECC Error was detected.
28 CORRECTED = 1, //< ECC error detected and corrected.
29 UNCORRECTABLE = 2 //< ECC error detected and uncorrectable.
30 };
31 typedef enum ecc_status ecc_status_t;
32
33 enum ecc_bitfields {
34 GD = 0xff, //< Good, ECC matches.
35 UE = 0xfe, //< Uncorrectable.
36 E0 = 71, //< Error in ECC bit 0
37 E1 = 70, //< Error in ECC bit 1
38 E2 = 69, //< Error in ECC bit 2
39 E3 = 68, //< Error in ECC bit 3
40 E4 = 67, //< Error in ECC bit 4
41 E5 = 66, //< Error in ECC bit 5
42 E6 = 65, //< Error in ECC bit 6
43 E7 = 64 //< Error in ECC bit 7
44 };
45
46 /*
47 static uint64_t ecc_matrix[] = {
48 //0000000000000000111010000100001000111100000011111001100111111111
49 0x0000e8423c0f99ff,
50 //0000000011101000010000100011110000001111100110011111111100000000
51 0x00e8423c0f99ff00,
52 //1110100001000010001111000000111110011001111111110000000000000000
53 0xe8423c0f99ff0000,
54 //0100001000111100000011111001100111111111000000000000000011101000
55 0x423c0f99ff0000e8,
56 //0011110000001111100110011111111100000000000000001110100001000010
57 0x3c0f99ff0000e842,
58 //0000111110011001111111110000000000000000111010000100001000111100
59 0x0f99ff0000e8423c,
60 //1001100111111111000000000000000011101000010000100011110000001111
61 0x99ff0000e8423c0f,
62 //1111111100000000000000001110100001000010001111000000111110011001
63 0xff0000e8423c0f99
64 };
65 */
66
67 /*
68 * Compressed version of table above, saves 48 bytes. Rotating value in register
69 * results in exactly the same size as full table, due to cost of loading values
70 * into registers.
71 */
72 static uint8_t ecc_matrix[] = {
73 0x00, 0x00, 0xe8, 0x42, 0x3c, 0x0f, 0x99, 0xff,
74 0x00, 0x00, 0xe8, 0x42, 0x3c, 0x0f, 0x99
75 };
76
77 static uint8_t syndrome_matrix[] = {
78 GD, E7, E6, UE, E5, UE, UE, 47, E4, UE, UE, 37, UE, 35, 39, UE,
79 E3, UE, UE, 48, UE, 30, 29, UE, UE, 57, 27, UE, 31, UE, UE, UE,
80 E2, UE, UE, 17, UE, 18, 40, UE, UE, 58, 22, UE, 21, UE, UE, UE,
81 UE, 16, 49, UE, 19, UE, UE, UE, 23, UE, UE, UE, UE, 20, UE, UE,
82 E1, UE, UE, 51, UE, 46, 9, UE, UE, 34, 10, UE, 32, UE, UE, 36,
83 UE, 62, 50, UE, 14, UE, UE, UE, 13, UE, UE, UE, UE, UE, UE, UE,
84 UE, 61, 8, UE, 41, UE, UE, UE, 11, UE, UE, UE, UE, UE, UE, UE,
85 15, UE, UE, UE, UE, UE, UE, UE, UE, UE, 12, UE, UE, UE, UE, UE,
86 E0, UE, UE, 55, UE, 45, 43, UE, UE, 56, 38, UE, 1, UE, UE, UE,
87 UE, 25, 26, UE, 2, UE, UE, UE, 24, UE, UE, UE, UE, UE, 28, UE,
88 UE, 59, 54, UE, 42, UE, UE, 44, 6, UE, UE, UE, UE, UE, UE, UE,
89 5, UE, UE, UE, UE, UE, UE, UE, UE, UE, UE, UE, UE, UE, UE, UE,
90 UE, 63, 53, UE, 0, UE, UE, UE, 33, UE, UE, UE, UE, UE, UE, UE,
91 3, UE, UE, 52, UE, UE, UE, UE, UE, UE, UE, UE, UE, UE, UE, UE,
92 7, UE, UE, UE, UE, UE, UE, UE, UE, 60, UE, UE, UE, UE, UE, UE,
93 UE, UE, UE, UE, 4, UE, UE, UE, UE, UE, UE, UE, UE, UE, UE, UE,
94 };
95
96 static inline uint32_t rd32(void *addr)
97 {
98 uint64_t ret;
99
100 /* Cache-inhibited load word */
101 asm volatile("lwzcix %0, 0, %1"
102 : "=r" (ret)
103 : "r" (addr)
104 : );
105
106 return ret;
107 }
108
109 static uint64_t rd64_unaligned(void *addr, int first_read)
110 {
111 static uint64_t tmp1; /* static is used to reduce number of PNOR reads */
112 uint64_t tmp2;
113 uint64_t ret;
114 uint64_t addr_aligned = ALIGN_DOWN((uint64_t)addr, 8);
115 unsigned int shift = 8 * ((uint64_t) addr - addr_aligned);
116
117 if (shift == 0 /* Previous tmp2 ended with ECC byte */
118 || first_read) { /* or it is the first invocation from remove_ecc */
119 asm volatile("ldcix %0, 0, %1"
120 : "=r" (tmp1)
121 : "r" (addr_aligned)
122 : );
123 }
124
125 asm volatile("ldcix %0, 0, %1"
126 : "=r" (tmp2)
127 : "r" (addr_aligned+8)
128 : );
129
130 ret = (tmp1 << shift) | (tmp2 >> (64 - shift));
131 tmp1 = tmp2;
132
133 return ret;
134 }
135
136 /*
137 * memcpy from cache-inhibited source
138 *
139 * Assume src is 8B-aligned and does not overlap with dest. Copies ALIGN(n,8)
140 * bytes, make sure dest is big enough.
141 */
142 static inline void memcpy_ci_src(void *dest, const void *src, size_t n)
143 {
144 int i;
145 uint64_t tmp;
146 for (i = 0; i < n; i += 8) {
147 asm volatile("ldcix %0, %1, %2"
148 : "=r" (tmp)
149 : "b"(src), "r" (i));
150 asm volatile("stdx %0, %1, %2"
151 :: "r" (tmp), "b"(dest), "r" (i)
152 : "memory");
153 }
154 }
155
156 static uint8_t generate_ecc(uint64_t i_data)
157 {
158 uint8_t result = 0;
159
160 for (int i = 0; i < 8; i++)
161 result |= __builtin_parityll((*(uint64_t *)&ecc_matrix[i]) & i_data) << i;
162 return result;
163 }
164
165 static uint8_t verify_ecc(uint64_t i_data, uint8_t i_ecc)
166 {
167 return syndrome_matrix[generate_ecc(i_data) ^ i_ecc];
168 }
169
170 static uint8_t correct_ecc(uint64_t *io_data, uint8_t *io_ecc)
171 {
172 uint8_t bad_bit = verify_ecc(*io_data, *io_ecc);
173
174 if ((bad_bit != GD) && (bad_bit != UE)) { /* Good is done, UE is hopeless */
175 /* Determine if the ECC or data part is bad, do bit flip. */
176 if (bad_bit >= E7)
177 *io_ecc ^= (1 << (bad_bit - E7));
178 else
179 *io_data ^= (1ull << (63 - bad_bit));
180 }
181 return bad_bit;
182 }
183
184 static ecc_status_t remove_ecc(uint8_t *io_src, size_t i_srcSz,
185 uint8_t *o_dst, size_t i_dstSz)
186 {
187 ecc_status_t rc = CLEAN;
188 int first_read = 1;
189
190 for (size_t i = 0, o = 0; i < i_srcSz;
191 i += sizeof(uint64_t) + sizeof(uint8_t), o += sizeof(uint64_t)) {
192 /*
193 * Read data and ECC parts. Reads from cache-inhibited storage always
194 * have to be aligned!
195 */
196 uint64_t data = rd64_unaligned(&io_src[i], first_read);
197 first_read = 0;
198
199 uint8_t ecc = io_src[i + sizeof(uint64_t)];
200
201 /* Calculate failing bit and fix data */
202 uint8_t bad_bit = correct_ecc(&data, &ecc);
203
204 /* Perform correction and status update */
205 if (bad_bit == UE)
206 rc = UNCORRECTABLE;
207 /* Unused, our source is not writable */
208 /*
209 else if (bad_bit != GD)
210 {
211 if (rc != UNCORRECTABLE)
212 {
213 rc = CORRECTED;
214 }
215
216 *(uint64_t*)(&io_src[i]) = data;
217 io_src[i + sizeof(uint64_t)] = ecc;
218 }
219 */
220
221 /* Copy fixed data to destination buffer */
222 *(uint64_t *)(&o_dst[o]) = data;
223 }
224 return rc;
225 }
226
227 static char *pnor_base;
228
229 /*
230 * PNOR has to be accessed with Cache Inhibited forms of instructions, and they
231 * require that the address is aligned, so we can just memcpy the data.
232 */
233 static ssize_t no_ecc_readat(const struct region_device *rd, void *b,
234 size_t offset, size_t size)
235 {
236 uint8_t tmp[8];
237 offset -= rd->region.offset;
238 size_t off_a = ALIGN_DOWN(offset, 8);
239 size_t size_left = size;
240 char *part_base = pnor_base + rd->region.offset;
241
242 /* If offset is not 8B-aligned */
243 if (offset & 0x7) {
244 int i;
245 memcpy_ci_src(tmp, &part_base[off_a], 8);
246 for (i = 8 - (offset & 7); i < 8; i++) {
247 *((uint8_t *)(b++)) = tmp[i];
248 if (!--size_left)
249 return size;
250 }
251 off_a += 8;
252 }
253
254 /* Align down size_left to 8B */
255 memcpy_ci_src(b, &part_base[off_a], ALIGN_DOWN(size_left, 8));
256
257 /* Copy the rest of requested unaligned data, if any */
258 if (size_left & 7) {
259 off_a += ALIGN_DOWN(size_left, 8);
260 b += ALIGN_DOWN(size_left, 8);
261 int i;
262 memcpy_ci_src(tmp, &part_base[off_a], 8);
263 for (i = 0; i < (size_left & 7); i++)
264 *((uint8_t *)(b++)) = tmp[i];
265 }
266
267 return size;
268 }
269
270 static ssize_t ecc_readat(const struct region_device *rd, void *b,
271 size_t offset, size_t size)
272 {
273 uint8_t tmp[8];
274 offset -= rd->region.offset;
275 size_t off_a = ALIGN_DOWN(offset, 8);
276 size_t size_left = size;
277 char *part_base = pnor_base + rd->region.offset;
278
279 /* If offset is not 8B-aligned */
280 if (offset & 0x7) {
281 int i;
282 remove_ecc((uint8_t *) &part_base[(off_a * 9)/8], 9, tmp, 8);
283 for (i = 8 - (offset & 7); i < 8; i++) {
284 *((uint8_t *)(b++)) = tmp[i];
285 if (!--size_left)
286 return size;
287 }
288 off_a += 8;
289 }
290
291 /* Align down size_left to 8B */
292 remove_ecc((uint8_t *) &part_base[(off_a * 9)/8],
293 (ALIGN_DOWN(size_left, 8) * 9) / 8,
294 b,
295 ALIGN_DOWN(size_left, 8));
296
297 /* Copy the rest of requested unaligned data, if any */
298 if (size_left & 7) {
299 off_a += ALIGN_DOWN(size_left, 8);
300 b += ALIGN_DOWN(size_left, 8);
301 int i;
302 remove_ecc((uint8_t *) &part_base[(off_a * 9)/8], 9, tmp, 8);
303 for (i = 0; i < (size_left & 7); i++)
304 *((uint8_t *)(b++)) = tmp[i];
305 }
306
307 return size;
308 }
309
310 static struct region_device_ops no_ecc_rdev_ops = {
311 .mmap = mmap_helper_rdev_mmap,
312 .munmap = mmap_helper_rdev_munmap,
313 .readat = no_ecc_readat,
314 };
315
316 static struct region_device_ops ecc_rdev_ops = {
317 .mmap = mmap_helper_rdev_mmap,
318 .munmap = mmap_helper_rdev_munmap,
319 .readat = ecc_readat,
320 };
321
322 static void mount_part_from_pnor(const char *part_name,
323 struct mmap_helper_region_device *mdev)
324 {
325 size_t base, size;
326 unsigned int i, block_size, entry_count = 0;
327 struct ffs_hdr *hdr_pnor = (struct ffs_hdr *)LPC_FLASH_TOP;
328
329 /* This loop could be skipped if we may assume that PNOR is always 64M */
330 while (hdr_pnor > (struct ffs_hdr *)LPC_FLASH_MIN) {
331 uint32_t csum = 0;
332 /* Size is aligned up to 8 because of how memcpy_ci_src works */
333 uint8_t buffer[ALIGN(FFS_HDR_SIZE, 8)];
334 struct ffs_hdr *hdr = (struct ffs_hdr *)buffer;
335
336 /* Assume block_size = 4K */
337 hdr_pnor = (struct ffs_hdr *)(((char *)hdr_pnor) - 0x1000);
338
339 if (rd32(&hdr_pnor->magic) != FFS_MAGIC)
340 continue;
341
342 if (rd32(&hdr_pnor->version) != FFS_VERSION_1)
343 continue;
344
345 /* Copy the header so we won't have to rd32() for further accesses */
346 memcpy_ci_src(buffer, hdr_pnor, FFS_HDR_SIZE);
347 csum = hdr->magic ^ hdr->version ^ hdr->size ^ hdr->entry_size ^
348 hdr->entry_count ^ hdr->block_size ^ hdr->block_count ^
349 hdr->resvd[0] ^ hdr->resvd[1] ^ hdr->resvd[2] ^ hdr->resvd[3] ^
350 hdr->checksum;
351 if (csum != 0)
352 continue;
353
354 pnor_base = (char *) LPC_FLASH_TOP - hdr->block_size * hdr->block_count;
355 entry_count = hdr->entry_count;
356 block_size = hdr->block_size;
357
358 /* Every byte counts when building for SEEPROM */
359 if (!CONFIG(BOOTBLOCK_IN_SEEPROM)) {
360 printk(BIOS_DEBUG, "FFS header at %p\n", hdr_pnor);
361 printk(BIOS_SPEW, " size %x\n", hdr->size);
362 printk(BIOS_SPEW, " entry_size %x\n", hdr->entry_size);
363 printk(BIOS_SPEW, " entry_count %x\n", hdr->entry_count);
364 printk(BIOS_SPEW, " block_size %x\n", hdr->block_size);
365 printk(BIOS_SPEW, " block_count %x\n", hdr->block_count);
366 printk(BIOS_DEBUG, "PNOR base at %p\n", pnor_base);
367 }
368
369 break;
370 }
371
372 if (hdr_pnor <= (struct ffs_hdr *)LPC_FLASH_MIN)
373 die("FFS header not found!\n");
374
375 for (i = 0; i < entry_count; i++) {
376 uint32_t *val, csum = 0;
377 int j;
378 /* Size is aligned up to 8 because of how memcpy_ci_src works */
379 uint8_t buffer[ALIGN(FFS_ENTRY_SIZE, 8)];
380 struct ffs_entry *e = (struct ffs_entry *)buffer;
381
382 /* Copy the entry so we won't have to rd32() for further accesses */
383 memcpy_ci_src(buffer, &hdr_pnor->entries[i], FFS_ENTRY_SIZE);
384
385 /* Every byte counts when building for SEEPROM */
386 if (!CONFIG(BOOTBLOCK_IN_SEEPROM)) {
387 printk(BIOS_SPEW, "%s: base %x, size %x (%x)\n\t type %x, flags %x\n",
388 e->name, e->base, e->size, e->actual, e->type, e->flags);
389 }
390
391 if (strcmp(e->name, part_name) != 0)
392 continue;
393
394 val = (uint32_t *) e;
395 for (j = 0; j < (FFS_ENTRY_SIZE / sizeof(uint32_t)); j++)
396 csum ^= val[j];
397
398 if (csum != 0)
399 continue;
400
401 base = block_size * e->base;
402 /* This is size of the partition, it does not include header or ECC */
403 size = e->actual;
404
405 mdev->rdev.ops = &no_ecc_rdev_ops;
406
407 if (e->user.data[0] & FFS_ENRY_INTEG_ECC) {
408 printk(BIOS_DEBUG, "%s partition has ECC\n", part_name);
409 mdev->rdev.ops = &ecc_rdev_ops;
410 size = size / 9 * 8;
411 }
412
413 if ((e->user.data[1] >> 24) & FFS_VERS_SHA512) {
414 /* Skip PNOR partition header */
415 base += 0x1000;
416
417 /* Possibly skip ECC of the header */
418 if (e->user.data[0] & FFS_ENRY_INTEG_ECC)
419 base += 0x200;
420 }
421
422 mdev->rdev.region.offset = base;
423 mdev->rdev.region.size = size;
424
425 break;
426 }
427 }
428
429 static struct mmap_helper_region_device boot_mdev = MMAP_HELPER_DEV_INIT(
430 &no_ecc_rdev_ops, 0, CONFIG_ROM_SIZE, &cbfs_cache);
431
432 void boot_device_init(void)
433 {
434 static int init_done;
435 if (init_done)
436 return;
437
438 mount_part_from_pnor(CBFS_PARTITION_NAME, &boot_mdev);
439
440 init_done = 1;
441 }
442
443 const struct region_device *boot_device_ro(void)
444 {
445 return &boot_mdev.rdev;
446 }