1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
6 * Interface for accessing run-time symbol table
7 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
8 * Jason McMullan <jason.mcmullan@netronome.com>
9 * Espen Skoglund <espen.skoglund@netronome.com>
10 * Francois H. Theron <francois.theron@netronome.com>
13 #include <asm/unaligned.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/io-64-nonatomic-hi-lo.h>
22 #include "nfp6000/nfp6000.h"
24 /* These need to match the linker */
25 #define SYM_TGT_LMEM 0
26 #define SYM_TGT_EMU_CACHE 0x17
28 struct nfp_rtsym_entry {
40 struct nfp_rtsym_table {
44 struct nfp_rtsym symtab[];
47 static int nfp_meid(u8 island_id, u8 menum)
49 return (island_id & 0x3F) == island_id && menum < 12 ?
50 (island_id << 4) | (menum + 4) : -1;
54 nfp_rtsym_sw_entry_init(struct nfp_rtsym_table *cache, u32 strtab_size,
55 struct nfp_rtsym *sw, struct nfp_rtsym_entry *fw)
58 sw->name = cache->strtab + le16_to_cpu(fw->name) % strtab_size;
59 sw->addr = ((u64)fw->addr_hi << 32) | le32_to_cpu(fw->addr_lo);
60 sw->size = ((u64)fw->size_hi << 32) | le32_to_cpu(fw->size_lo);
64 sw->target = NFP_RTSYM_TARGET_LMEM;
66 case SYM_TGT_EMU_CACHE:
67 sw->target = NFP_RTSYM_TARGET_EMU_CACHE;
70 sw->target = fw->target;
74 if (fw->menum != 0xff)
75 sw->domain = nfp_meid(fw->island, fw->menum);
76 else if (fw->island != 0xff)
77 sw->domain = fw->island;
82 struct nfp_rtsym_table *nfp_rtsym_table_read(struct nfp_cpp *cpp)
84 struct nfp_rtsym_table *rtbl;
85 const struct nfp_mip *mip;
87 mip = nfp_mip_open(cpp);
88 rtbl = __nfp_rtsym_table_read(cpp, mip);
94 struct nfp_rtsym_table *
95 __nfp_rtsym_table_read(struct nfp_cpp *cpp, const struct nfp_mip *mip)
97 const u32 dram = NFP_CPP_ID(NFP_CPP_TARGET_MU, NFP_CPP_ACTION_RW, 0) |
99 u32 strtab_addr, symtab_addr, strtab_size, symtab_size;
100 struct nfp_rtsym_entry *rtsymtab;
101 struct nfp_rtsym_table *cache;
107 nfp_mip_strtab(mip, &strtab_addr, &strtab_size);
108 nfp_mip_symtab(mip, &symtab_addr, &symtab_size);
110 if (!symtab_size || !strtab_size || symtab_size % sizeof(*rtsymtab))
113 /* Align to 64 bits */
114 symtab_size = round_up(symtab_size, 8);
115 strtab_size = round_up(strtab_size, 8);
117 rtsymtab = kmalloc(symtab_size, GFP_KERNEL);
121 size = sizeof(*cache);
122 size += symtab_size / sizeof(*rtsymtab) * sizeof(struct nfp_rtsym);
123 size += strtab_size + 1;
124 cache = kmalloc(size, GFP_KERNEL);
126 goto exit_free_rtsym_raw;
129 cache->num = symtab_size / sizeof(*rtsymtab);
130 cache->strtab = (void *)&cache->symtab[cache->num];
132 err = nfp_cpp_read(cpp, dram, symtab_addr, rtsymtab, symtab_size);
133 if (err != symtab_size)
134 goto exit_free_cache;
136 err = nfp_cpp_read(cpp, dram, strtab_addr, cache->strtab, strtab_size);
137 if (err != strtab_size)
138 goto exit_free_cache;
139 cache->strtab[strtab_size] = '\0';
141 for (n = 0; n < cache->num; n++)
142 nfp_rtsym_sw_entry_init(cache, strtab_size,
143 &cache->symtab[n], &rtsymtab[n]);
157 * nfp_rtsym_count() - Get the number of RTSYM descriptors
158 * @rtbl: NFP RTsym table
160 * Return: Number of RTSYM descriptors
162 int nfp_rtsym_count(struct nfp_rtsym_table *rtbl)
170 * nfp_rtsym_get() - Get the Nth RTSYM descriptor
171 * @rtbl: NFP RTsym table
172 * @idx: Index (0-based) of the RTSYM descriptor
174 * Return: const pointer to a struct nfp_rtsym descriptor, or NULL
176 const struct nfp_rtsym *nfp_rtsym_get(struct nfp_rtsym_table *rtbl, int idx)
180 if (idx >= rtbl->num)
183 return &rtbl->symtab[idx];
187 * nfp_rtsym_lookup() - Return the RTSYM descriptor for a symbol name
188 * @rtbl: NFP RTsym table
191 * Return: const pointer to a struct nfp_rtsym descriptor, or NULL
193 const struct nfp_rtsym *
194 nfp_rtsym_lookup(struct nfp_rtsym_table *rtbl, const char *name)
201 for (n = 0; n < rtbl->num; n++)
202 if (strcmp(name, rtbl->symtab[n].name) == 0)
203 return &rtbl->symtab[n];
208 u64 nfp_rtsym_size(const struct nfp_rtsym *sym)
211 case NFP_RTSYM_TYPE_NONE:
212 pr_err("rtsym '%s': type NONE\n", sym->name);
215 pr_warn("rtsym '%s': unknown type: %d\n", sym->name, sym->type);
217 case NFP_RTSYM_TYPE_OBJECT:
218 case NFP_RTSYM_TYPE_FUNCTION:
220 case NFP_RTSYM_TYPE_ABS:
226 nfp_rtsym_to_dest(struct nfp_cpp *cpp, const struct nfp_rtsym *sym,
227 u8 action, u8 token, u64 off, u32 *cpp_id, u64 *addr)
229 if (sym->type != NFP_RTSYM_TYPE_OBJECT) {
230 nfp_err(cpp, "rtsym '%s': direct access to non-object rtsym\n",
235 *addr = sym->addr + off;
237 if (sym->target == NFP_RTSYM_TARGET_EMU_CACHE) {
238 int locality_off = nfp_cpp_mu_locality_lsb(cpp);
240 *addr &= ~(NFP_MU_ADDR_ACCESS_TYPE_MASK << locality_off);
241 *addr |= NFP_MU_ADDR_ACCESS_TYPE_DIRECT << locality_off;
243 *cpp_id = NFP_CPP_ISLAND_ID(NFP_CPP_TARGET_MU, action, token,
245 } else if (sym->target < 0) {
246 nfp_err(cpp, "rtsym '%s': unhandled target encoding: %d\n",
247 sym->name, sym->target);
250 *cpp_id = NFP_CPP_ISLAND_ID(sym->target, action, token,
257 int __nfp_rtsym_read(struct nfp_cpp *cpp, const struct nfp_rtsym *sym,
258 u8 action, u8 token, u64 off, void *buf, size_t len)
260 u64 sym_size = nfp_rtsym_size(sym);
265 if (off > sym_size) {
266 nfp_err(cpp, "rtsym '%s': read out of bounds: off: %lld + len: %zd > size: %lld\n",
267 sym->name, off, len, sym_size);
270 len = min_t(size_t, len, sym_size - off);
272 if (sym->type == NFP_RTSYM_TYPE_ABS) {
275 put_unaligned_le64(sym->addr, tmp);
276 memcpy(buf, &tmp[off], len);
281 err = nfp_rtsym_to_dest(cpp, sym, action, token, off, &cpp_id, &addr);
285 return nfp_cpp_read(cpp, cpp_id, addr, buf, len);
288 int nfp_rtsym_read(struct nfp_cpp *cpp, const struct nfp_rtsym *sym, u64 off,
289 void *buf, size_t len)
291 return __nfp_rtsym_read(cpp, sym, NFP_CPP_ACTION_RW, 0, off, buf, len);
294 int __nfp_rtsym_readl(struct nfp_cpp *cpp, const struct nfp_rtsym *sym,
295 u8 action, u8 token, u64 off, u32 *value)
301 if (off + 4 > nfp_rtsym_size(sym)) {
302 nfp_err(cpp, "rtsym '%s': readl out of bounds: off: %lld + 4 > size: %lld\n",
303 sym->name, off, nfp_rtsym_size(sym));
307 err = nfp_rtsym_to_dest(cpp, sym, action, token, off, &cpp_id, &addr);
311 return nfp_cpp_readl(cpp, cpp_id, addr, value);
314 int nfp_rtsym_readl(struct nfp_cpp *cpp, const struct nfp_rtsym *sym, u64 off,
317 return __nfp_rtsym_readl(cpp, sym, NFP_CPP_ACTION_RW, 0, off, value);
320 int __nfp_rtsym_readq(struct nfp_cpp *cpp, const struct nfp_rtsym *sym,
321 u8 action, u8 token, u64 off, u64 *value)
327 if (off + 8 > nfp_rtsym_size(sym)) {
328 nfp_err(cpp, "rtsym '%s': readq out of bounds: off: %lld + 8 > size: %lld\n",
329 sym->name, off, nfp_rtsym_size(sym));
333 if (sym->type == NFP_RTSYM_TYPE_ABS) {
338 err = nfp_rtsym_to_dest(cpp, sym, action, token, off, &cpp_id, &addr);
342 return nfp_cpp_readq(cpp, cpp_id, addr, value);
345 int nfp_rtsym_readq(struct nfp_cpp *cpp, const struct nfp_rtsym *sym, u64 off,
348 return __nfp_rtsym_readq(cpp, sym, NFP_CPP_ACTION_RW, 0, off, value);
351 int __nfp_rtsym_write(struct nfp_cpp *cpp, const struct nfp_rtsym *sym,
352 u8 action, u8 token, u64 off, void *buf, size_t len)
354 u64 sym_size = nfp_rtsym_size(sym);
359 if (off > sym_size) {
360 nfp_err(cpp, "rtsym '%s': write out of bounds: off: %lld + len: %zd > size: %lld\n",
361 sym->name, off, len, sym_size);
364 len = min_t(size_t, len, sym_size - off);
366 err = nfp_rtsym_to_dest(cpp, sym, action, token, off, &cpp_id, &addr);
370 return nfp_cpp_write(cpp, cpp_id, addr, buf, len);
373 int nfp_rtsym_write(struct nfp_cpp *cpp, const struct nfp_rtsym *sym, u64 off,
374 void *buf, size_t len)
376 return __nfp_rtsym_write(cpp, sym, NFP_CPP_ACTION_RW, 0, off, buf, len);
379 int __nfp_rtsym_writel(struct nfp_cpp *cpp, const struct nfp_rtsym *sym,
380 u8 action, u8 token, u64 off, u32 value)
386 if (off + 4 > nfp_rtsym_size(sym)) {
387 nfp_err(cpp, "rtsym '%s': writel out of bounds: off: %lld + 4 > size: %lld\n",
388 sym->name, off, nfp_rtsym_size(sym));
392 err = nfp_rtsym_to_dest(cpp, sym, action, token, off, &cpp_id, &addr);
396 return nfp_cpp_writel(cpp, cpp_id, addr, value);
399 int nfp_rtsym_writel(struct nfp_cpp *cpp, const struct nfp_rtsym *sym, u64 off,
402 return __nfp_rtsym_writel(cpp, sym, NFP_CPP_ACTION_RW, 0, off, value);
405 int __nfp_rtsym_writeq(struct nfp_cpp *cpp, const struct nfp_rtsym *sym,
406 u8 action, u8 token, u64 off, u64 value)
412 if (off + 8 > nfp_rtsym_size(sym)) {
413 nfp_err(cpp, "rtsym '%s': writeq out of bounds: off: %lld + 8 > size: %lld\n",
414 sym->name, off, nfp_rtsym_size(sym));
418 err = nfp_rtsym_to_dest(cpp, sym, action, token, off, &cpp_id, &addr);
422 return nfp_cpp_writeq(cpp, cpp_id, addr, value);
425 int nfp_rtsym_writeq(struct nfp_cpp *cpp, const struct nfp_rtsym *sym, u64 off,
428 return __nfp_rtsym_writeq(cpp, sym, NFP_CPP_ACTION_RW, 0, off, value);
432 * nfp_rtsym_read_le() - Read a simple unsigned scalar value from symbol
433 * @rtbl: NFP RTsym table
435 * @error: Poniter to error code (optional)
437 * Lookup a symbol, map, read it and return it's value. Value of the symbol
438 * will be interpreted as a simple little-endian unsigned value. Symbol can
439 * be 4 or 8 bytes in size.
441 * Return: value read, on error sets the error and returns ~0ULL.
443 u64 nfp_rtsym_read_le(struct nfp_rtsym_table *rtbl, const char *name,
446 const struct nfp_rtsym *sym;
451 sym = nfp_rtsym_lookup(rtbl, name);
457 switch (nfp_rtsym_size(sym)) {
459 err = nfp_rtsym_readl(rtbl->cpp, sym, 0, &val32);
463 err = nfp_rtsym_readq(rtbl->cpp, sym, 0, &val);
467 "rtsym '%s': unsupported or non-scalar size: %lld\n",
468 name, nfp_rtsym_size(sym));
483 * nfp_rtsym_write_le() - Write an unsigned scalar value to a symbol
484 * @rtbl: NFP RTsym table
486 * @value: Value to write
488 * Lookup a symbol and write a value to it. Symbol can be 4 or 8 bytes in size.
489 * If 4 bytes then the lower 32-bits of 'value' are used. Value will be
490 * written as simple little-endian unsigned value.
492 * Return: 0 on success or error code.
494 int nfp_rtsym_write_le(struct nfp_rtsym_table *rtbl, const char *name,
497 const struct nfp_rtsym *sym;
500 sym = nfp_rtsym_lookup(rtbl, name);
504 switch (nfp_rtsym_size(sym)) {
506 err = nfp_rtsym_writel(rtbl->cpp, sym, 0, value);
509 err = nfp_rtsym_writeq(rtbl->cpp, sym, 0, value);
513 "rtsym '%s': unsupported or non-scalar size: %lld\n",
514 name, nfp_rtsym_size(sym));
523 nfp_rtsym_map(struct nfp_rtsym_table *rtbl, const char *name, const char *id,
524 unsigned int min_size, struct nfp_cpp_area **area)
526 const struct nfp_rtsym *sym;
532 sym = nfp_rtsym_lookup(rtbl, name);
534 return (u8 __iomem *)ERR_PTR(-ENOENT);
536 err = nfp_rtsym_to_dest(rtbl->cpp, sym, NFP_CPP_ACTION_RW, 0, 0,
539 nfp_err(rtbl->cpp, "rtsym '%s': mapping failed\n", name);
540 return (u8 __iomem *)ERR_PTR(err);
543 if (sym->size < min_size) {
544 nfp_err(rtbl->cpp, "rtsym '%s': too small\n", name);
545 return (u8 __iomem *)ERR_PTR(-EINVAL);
548 mem = nfp_cpp_map_area(rtbl->cpp, id, cpp_id, addr, sym->size, area);
550 nfp_err(rtbl->cpp, "rtysm '%s': failed to map: %ld\n",