1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2018 Netronome Systems, Inc. 3 * All rights reserved. 4 */ 5 6 /* 7 * nfp_rtsym.c 8 * Interface for accessing run-time symbol table 9 */ 10 11 #include <stdio.h> 12 #include <rte_byteorder.h> 13 #include "nfp_cpp.h" 14 #include "nfp_mip.h" 15 #include "nfp_rtsym.h" 16 #include "nfp6000/nfp6000.h" 17 18 /* These need to match the linker */ 19 #define SYM_TGT_LMEM 0 20 #define SYM_TGT_EMU_CACHE 0x17 21 22 struct nfp_rtsym_entry { 23 uint8_t type; 24 uint8_t target; 25 uint8_t island; 26 uint8_t addr_hi; 27 uint32_t addr_lo; 28 uint16_t name; 29 uint8_t menum; 30 uint8_t size_hi; 31 uint32_t size_lo; 32 }; 33 34 struct nfp_rtsym_table { 35 struct nfp_cpp *cpp; 36 int num; 37 char *strtab; 38 struct nfp_rtsym symtab[]; 39 }; 40 41 static int 42 nfp_meid(uint8_t island_id, uint8_t menum) 43 { 44 return (island_id & 0x3F) == island_id && menum < 12 ? 45 (island_id << 4) | (menum + 4) : -1; 46 } 47 48 static void 49 nfp_rtsym_sw_entry_init(struct nfp_rtsym_table *cache, uint32_t strtab_size, 50 struct nfp_rtsym *sw, struct nfp_rtsym_entry *fw) 51 { 52 sw->type = fw->type; 53 sw->name = cache->strtab + rte_le_to_cpu_16(fw->name) % strtab_size; 54 sw->addr = ((uint64_t)fw->addr_hi << 32) | 55 rte_le_to_cpu_32(fw->addr_lo); 56 sw->size = ((uint64_t)fw->size_hi << 32) | 57 rte_le_to_cpu_32(fw->size_lo); 58 59 #ifdef DEBUG 60 printf("rtsym_entry_init\n"); 61 printf("\tname=%s, addr=%" PRIx64 ", size=%" PRIu64 ",target=%d\n", 62 sw->name, sw->addr, sw->size, sw->target); 63 #endif 64 switch (fw->target) { 65 case SYM_TGT_LMEM: 66 sw->target = NFP_RTSYM_TARGET_LMEM; 67 break; 68 case SYM_TGT_EMU_CACHE: 69 sw->target = NFP_RTSYM_TARGET_EMU_CACHE; 70 break; 71 default: 72 sw->target = fw->target; 73 break; 74 } 75 76 if (fw->menum != 0xff) 77 sw->domain = nfp_meid(fw->island, fw->menum); 78 else if (fw->island != 0xff) 79 sw->domain = fw->island; 80 else 81 sw->domain = -1; 82 } 83 84 struct nfp_rtsym_table * 85 nfp_rtsym_table_read(struct nfp_cpp *cpp) 86 { 87 struct nfp_rtsym_table *rtbl; 88 struct nfp_mip *mip; 89 90 mip = nfp_mip_open(cpp); 91 rtbl = __nfp_rtsym_table_read(cpp, mip); 92 nfp_mip_close(mip); 93 94 return rtbl; 95 } 96 97 struct nfp_rtsym_table * 98 __nfp_rtsym_table_read(struct nfp_cpp *cpp, const struct nfp_mip *mip) 99 { 100 uint32_t strtab_addr, symtab_addr, strtab_size, symtab_size; 101 struct nfp_rtsym_entry *rtsymtab; 102 struct nfp_rtsym_table *cache; 103 const uint32_t dram = 104 NFP_CPP_ID(NFP_CPP_TARGET_MU, NFP_CPP_ACTION_RW, 0) | 105 NFP_ISL_EMEM0; 106 int err, n, size; 107 108 if (!mip) 109 return NULL; 110 111 nfp_mip_strtab(mip, &strtab_addr, &strtab_size); 112 nfp_mip_symtab(mip, &symtab_addr, &symtab_size); 113 114 if (!symtab_size || !strtab_size || symtab_size % sizeof(*rtsymtab)) 115 return NULL; 116 117 /* Align to 64 bits */ 118 symtab_size = round_up(symtab_size, 8); 119 strtab_size = round_up(strtab_size, 8); 120 121 rtsymtab = malloc(symtab_size); 122 if (!rtsymtab) 123 return NULL; 124 125 size = sizeof(*cache); 126 size += symtab_size / sizeof(*rtsymtab) * sizeof(struct nfp_rtsym); 127 size += strtab_size + 1; 128 cache = malloc(size); 129 if (!cache) 130 goto exit_free_rtsym_raw; 131 132 cache->cpp = cpp; 133 cache->num = symtab_size / sizeof(*rtsymtab); 134 cache->strtab = (void *)&cache->symtab[cache->num]; 135 136 err = nfp_cpp_read(cpp, dram, symtab_addr, rtsymtab, symtab_size); 137 if (err != (int)symtab_size) 138 goto exit_free_cache; 139 140 err = nfp_cpp_read(cpp, dram, strtab_addr, cache->strtab, strtab_size); 141 if (err != (int)strtab_size) 142 goto exit_free_cache; 143 cache->strtab[strtab_size] = '\0'; 144 145 for (n = 0; n < cache->num; n++) 146 nfp_rtsym_sw_entry_init(cache, strtab_size, 147 &cache->symtab[n], &rtsymtab[n]); 148 149 free(rtsymtab); 150 151 return cache; 152 153 exit_free_cache: 154 free(cache); 155 exit_free_rtsym_raw: 156 free(rtsymtab); 157 return NULL; 158 } 159 160 /* 161 * nfp_rtsym_count() - Get the number of RTSYM descriptors 162 * @rtbl: NFP RTsym table 163 * 164 * Return: Number of RTSYM descriptors 165 */ 166 int 167 nfp_rtsym_count(struct nfp_rtsym_table *rtbl) 168 { 169 if (!rtbl) 170 return -EINVAL; 171 172 return rtbl->num; 173 } 174 175 /* 176 * nfp_rtsym_get() - Get the Nth RTSYM descriptor 177 * @rtbl: NFP RTsym table 178 * @idx: Index (0-based) of the RTSYM descriptor 179 * 180 * Return: const pointer to a struct nfp_rtsym descriptor, or NULL 181 */ 182 const struct nfp_rtsym * 183 nfp_rtsym_get(struct nfp_rtsym_table *rtbl, int idx) 184 { 185 if (!rtbl) 186 return NULL; 187 188 if (idx >= rtbl->num) 189 return NULL; 190 191 return &rtbl->symtab[idx]; 192 } 193 194 /* 195 * nfp_rtsym_lookup() - Return the RTSYM descriptor for a symbol name 196 * @rtbl: NFP RTsym table 197 * @name: Symbol name 198 * 199 * Return: const pointer to a struct nfp_rtsym descriptor, or NULL 200 */ 201 const struct nfp_rtsym * 202 nfp_rtsym_lookup(struct nfp_rtsym_table *rtbl, const char *name) 203 { 204 int n; 205 206 if (!rtbl) 207 return NULL; 208 209 for (n = 0; n < rtbl->num; n++) 210 if (strcmp(name, rtbl->symtab[n].name) == 0) 211 return &rtbl->symtab[n]; 212 213 return NULL; 214 } 215 216 /* 217 * nfp_rtsym_read_le() - Read a simple unsigned scalar value from symbol 218 * @rtbl: NFP RTsym table 219 * @name: Symbol name 220 * @error: Pointer to error code (optional) 221 * 222 * Lookup a symbol, map, read it and return it's value. Value of the symbol 223 * will be interpreted as a simple little-endian unsigned value. Symbol can 224 * be 4 or 8 bytes in size. 225 * 226 * Return: value read, on error sets the error and returns ~0ULL. 227 */ 228 uint64_t 229 nfp_rtsym_read_le(struct nfp_rtsym_table *rtbl, const char *name, int *error) 230 { 231 const struct nfp_rtsym *sym; 232 uint32_t val32, id; 233 uint64_t val; 234 int err; 235 236 sym = nfp_rtsym_lookup(rtbl, name); 237 if (!sym) { 238 err = -ENOENT; 239 goto exit; 240 } 241 242 id = NFP_CPP_ISLAND_ID(sym->target, NFP_CPP_ACTION_RW, 0, sym->domain); 243 244 #ifdef DEBUG 245 printf("Reading symbol %s with size %" PRIu64 " at %" PRIx64 "\n", 246 name, sym->size, sym->addr); 247 #endif 248 switch (sym->size) { 249 case 4: 250 err = nfp_cpp_readl(rtbl->cpp, id, sym->addr, &val32); 251 val = val32; 252 break; 253 case 8: 254 err = nfp_cpp_readq(rtbl->cpp, id, sym->addr, &val); 255 break; 256 default: 257 printf("rtsym '%s' unsupported size: %" PRId64 "\n", 258 name, sym->size); 259 err = -EINVAL; 260 break; 261 } 262 263 if (err) 264 err = -EIO; 265 exit: 266 if (error) 267 *error = err; 268 269 if (err) 270 return ~0ULL; 271 272 return val; 273 } 274 275 uint8_t * 276 nfp_rtsym_map(struct nfp_rtsym_table *rtbl, const char *name, 277 unsigned int min_size, struct nfp_cpp_area **area) 278 { 279 const struct nfp_rtsym *sym; 280 uint8_t *mem; 281 282 #ifdef DEBUG 283 printf("mapping symbol %s\n", name); 284 #endif 285 sym = nfp_rtsym_lookup(rtbl, name); 286 if (!sym) { 287 printf("symbol lookup fails for %s\n", name); 288 return NULL; 289 } 290 291 if (sym->size < min_size) { 292 printf("Symbol %s too small (%" PRIu64 " < %u)\n", name, 293 sym->size, min_size); 294 return NULL; 295 } 296 297 mem = nfp_cpp_map_area(rtbl->cpp, sym->domain, sym->target, sym->addr, 298 sym->size, area); 299 if (!mem) { 300 printf("Failed to map symbol %s\n", name); 301 return NULL; 302 } 303 #ifdef DEBUG 304 printf("symbol %s with address %p\n", name, mem); 305 #endif 306 307 return mem; 308 } 309