|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | + |
| 3 | +#include <cpu/power/mvpd.h> |
| 4 | + |
| 5 | +#include <commonlib/region.h> |
| 6 | +#include <console/console.h> |
| 7 | +#include <cpu/power/vpd.h> |
| 8 | +#include <endian.h> |
| 9 | +#include <stdint.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +#include "tor.h" |
| 13 | +#include "rs4.h" |
| 14 | + |
| 15 | +#define MVPD_TOC_ENTRIES 32 |
| 16 | +#define MVPD_TOC_SIZE (MVPD_TOC_ENTRIES*sizeof(struct mvpd_toc_entry)) |
| 17 | + |
| 18 | +/* Each entry points to a VPD record */ |
| 19 | +struct mvpd_toc_entry { |
| 20 | + char name[4]; // Name without trailing NUL byte |
| 21 | + uint16_t offset; // Offset from the beginning of partition in LE |
| 22 | + uint8_t reserved[2]; // Unused |
| 23 | +} __attribute__((packed)); |
| 24 | + |
| 25 | +static struct mvpd_toc_entry *find_record(struct mvpd_toc_entry *toc, |
| 26 | + const char *name) |
| 27 | +{ |
| 28 | + int i = 0; |
| 29 | + for (i = 0; i < MVPD_TOC_ENTRIES; ++i) { |
| 30 | + if (memcmp(toc[i].name, name, VPD_RECORD_NAME_LEN) == 0) |
| 31 | + return &toc[i]; |
| 32 | + } |
| 33 | + return NULL; |
| 34 | +} |
| 35 | + |
| 36 | +/* Checks if rings ends here. End is marked by an "END" string. */ |
| 37 | +static bool is_end_of_rings(const uint8_t *buf_left, uint32_t len_left) |
| 38 | +{ |
| 39 | + return (len_left < 3 || memcmp(buf_left, "END", 3) == 0); |
| 40 | +} |
| 41 | + |
| 42 | +/* Finds specific ring by combination of chiplet and ring ids */ |
| 43 | +static struct ring_hdr *find_ring_step(uint8_t chiplet_id, uint16_t ring_id, |
| 44 | + const uint8_t **buf_left, |
| 45 | + uint32_t *len_left) |
| 46 | +{ |
| 47 | + uint32_t even_odd_mask = 0; |
| 48 | + struct ring_hdr *hdr = (struct ring_hdr *)*buf_left; |
| 49 | + |
| 50 | + if (*len_left < sizeof(struct ring_hdr) || |
| 51 | + be16toh(hdr->magic) != RS4_MAGIC) |
| 52 | + return NULL; |
| 53 | + |
| 54 | + *buf_left += be16toh(hdr->size); |
| 55 | + *len_left -= be16toh(hdr->size); |
| 56 | + |
| 57 | + switch (even_odd_mask) { |
| 58 | + case EX_L3_REPR: even_odd_mask = 0x00001000; break; |
| 59 | + case EX_L2_REPR: even_odd_mask = 0x00000400; break; |
| 60 | + case EX_L3_REFR_TIME: |
| 61 | + case EX_L3_REFR_REPR: even_odd_mask = 0x00000040; break; |
| 62 | + |
| 63 | + default: even_odd_mask = 0; break; |
| 64 | + } |
| 65 | + |
| 66 | + if (be16toh(hdr->ring_id) != ring_id) |
| 67 | + return NULL; |
| 68 | + if (((be32toh(hdr->scan_addr) >> 24) & 0xFF) != chiplet_id) |
| 69 | + return NULL; |
| 70 | + if (even_odd_mask != 0 && !(be32toh(hdr->scan_addr) & even_odd_mask)) |
| 71 | + return NULL; |
| 72 | + |
| 73 | + return hdr; |
| 74 | +} |
| 75 | + |
| 76 | +/* Searches for a specific ring in a keyword */ |
| 77 | +static struct ring_hdr *find_ring(uint8_t chiplet_id, uint16_t ring_id, |
| 78 | + const uint8_t *buf, uint32_t buf_len) |
| 79 | +{ |
| 80 | + /* Skip version number */ |
| 81 | + --buf_len; |
| 82 | + ++buf; |
| 83 | + |
| 84 | + while (!is_end_of_rings(buf, buf_len)) { |
| 85 | + struct ring_hdr *ring = find_ring_step(chiplet_id, ring_id, |
| 86 | + &buf, &buf_len); |
| 87 | + if (ring != NULL) |
| 88 | + return ring; |
| 89 | + } |
| 90 | + |
| 91 | + return NULL; |
| 92 | +} |
| 93 | + |
| 94 | +/* Finds a specific ring in MVPD partition and extracts it */ |
| 95 | +bool mvpd_extract_ring(const char *record_name, const char *kwd_name, |
| 96 | + uint8_t chiplet_id, uint16_t ring_id, uint8_t *buf, |
| 97 | + uint32_t buf_size) |
| 98 | +{ |
| 99 | + const struct region_device *mvpd_device; |
| 100 | + |
| 101 | + uint8_t mvpd_buf[MVPD_TOC_SIZE]; |
| 102 | + struct mvpd_toc_entry *mvpd_toc = (struct mvpd_toc_entry *)mvpd_buf; |
| 103 | + |
| 104 | + struct mvpd_toc_entry *cp00 = NULL; |
| 105 | + uint16_t cp00_offset = 0; |
| 106 | + const uint8_t *cp00_data = NULL; |
| 107 | + uint16_t cp00_size = 0; |
| 108 | + |
| 109 | + const uint8_t *rings = NULL; |
| 110 | + size_t rings_size = 0; |
| 111 | + |
| 112 | + struct ring_hdr *ring = NULL; |
| 113 | + uint32_t ring_size = 0; |
| 114 | + |
| 115 | + mvpd_device_init(); |
| 116 | + mvpd_device = mvpd_device_ro(); |
| 117 | + |
| 118 | + /* Copy all TOC at once */ |
| 119 | + if (rdev_readat(mvpd_device, mvpd_buf, 0, |
| 120 | + sizeof(mvpd_buf)) != sizeof(mvpd_buf)) |
| 121 | + die("Failed to read MVPD TOC!\n"); |
| 122 | + |
| 123 | + cp00 = find_record(mvpd_toc, record_name); |
| 124 | + if (cp00 == NULL) |
| 125 | + die("Failed to find %s MVPD record!\n", record_name); |
| 126 | + cp00_offset = le16toh(cp00->offset); |
| 127 | + |
| 128 | + /* Read size of the record */ |
| 129 | + if (rdev_readat(mvpd_device, &cp00_size, cp00_offset, |
| 130 | + sizeof(cp00_size)) != sizeof(cp00_size)) |
| 131 | + die("Failed to read size of %s!\n", record_name); |
| 132 | + |
| 133 | + cp00_data = rdev_mmap(mvpd_device, cp00_offset, cp00_size); |
| 134 | + if (!cp00_data) |
| 135 | + die("Failed to map %s record!\n", record_name); |
| 136 | + |
| 137 | + rings = vpd_find_kwd(cp00_data, record_name, kwd_name, &rings_size); |
| 138 | + if (rings == NULL) |
| 139 | + die("Failed to find %s keyword in %s!\n", kwd_name, |
| 140 | + record_name); |
| 141 | + |
| 142 | + ring = find_ring(chiplet_id, ring_id, rings, rings_size); |
| 143 | + if (ring == NULL) { |
| 144 | + if (rdev_munmap(mvpd_device, (void *)cp00_data)) |
| 145 | + die("Failed to unmap %s record!\n", record_name); |
| 146 | + |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + ring_size = be32toh(ring->size); |
| 151 | + if (buf_size >= ring_size) |
| 152 | + memcpy(buf, ring, ring_size); |
| 153 | + |
| 154 | + if (rdev_munmap(mvpd_device, (void *)cp00_data)) |
| 155 | + die("Failed to unmap %s record!\n", record_name); |
| 156 | + |
| 157 | + return (buf_size >= ring_size); |
| 158 | +} |
0 commit comments