1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Netronome Systems, Inc.
8 * Interface for accessing run-time symbol table
12 #include <rte_byteorder.h>
15 #include "nfp_rtsym.h"
16 #include "nfp6000/nfp6000.h"
18 /* These need to match the linker */
19 #define SYM_TGT_LMEM 0
20 #define SYM_TGT_EMU_CACHE 0x17
22 struct nfp_rtsym_entry
{
34 struct nfp_rtsym_table
{
38 struct nfp_rtsym symtab
[];
42 nfp_meid(uint8_t island_id
, uint8_t menum
)
44 return (island_id
& 0x3F) == island_id
&& menum
< 12 ?
45 (island_id
<< 4) | (menum
+ 4) : -1;
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
)
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
);
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
);
66 sw
->target
= NFP_RTSYM_TARGET_LMEM
;
68 case SYM_TGT_EMU_CACHE
:
69 sw
->target
= NFP_RTSYM_TARGET_EMU_CACHE
;
72 sw
->target
= fw
->target
;
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
;
84 struct nfp_rtsym_table
*
85 nfp_rtsym_table_read(struct nfp_cpp
*cpp
)
87 struct nfp_rtsym_table
*rtbl
;
90 mip
= nfp_mip_open(cpp
);
91 rtbl
= __nfp_rtsym_table_read(cpp
, mip
);
98 * This looks more complex than it should be. But we need to get the type for
99 * the ~ right in round_down (it needs to be as wide as the result!), and we
100 * want to evaluate the macro arguments just once each.
102 #define __round_mask(x, y) ((__typeof__(x))((y) - 1))
104 #define round_up(x, y) \
106 typeof(x) _x = (x); \
107 ((((_x) - 1) | __round_mask(_x, y)) + 1); \
110 #define round_down(x, y) \
112 typeof(x) _x = (x); \
113 ((_x) & ~__round_mask(_x, y)); \
116 struct nfp_rtsym_table
*
117 __nfp_rtsym_table_read(struct nfp_cpp
*cpp
, const struct nfp_mip
*mip
)
119 uint32_t strtab_addr
, symtab_addr
, strtab_size
, symtab_size
;
120 struct nfp_rtsym_entry
*rtsymtab
;
121 struct nfp_rtsym_table
*cache
;
122 const uint32_t dram
=
123 NFP_CPP_ID(NFP_CPP_TARGET_MU
, NFP_CPP_ACTION_RW
, 0) |
130 nfp_mip_strtab(mip
, &strtab_addr
, &strtab_size
);
131 nfp_mip_symtab(mip
, &symtab_addr
, &symtab_size
);
133 if (!symtab_size
|| !strtab_size
|| symtab_size
% sizeof(*rtsymtab
))
136 /* Align to 64 bits */
137 symtab_size
= round_up(symtab_size
, 8);
138 strtab_size
= round_up(strtab_size
, 8);
140 rtsymtab
= malloc(symtab_size
);
144 size
= sizeof(*cache
);
145 size
+= symtab_size
/ sizeof(*rtsymtab
) * sizeof(struct nfp_rtsym
);
146 size
+= strtab_size
+ 1;
147 cache
= malloc(size
);
149 goto exit_free_rtsym_raw
;
152 cache
->num
= symtab_size
/ sizeof(*rtsymtab
);
153 cache
->strtab
= (void *)&cache
->symtab
[cache
->num
];
155 err
= nfp_cpp_read(cpp
, dram
, symtab_addr
, rtsymtab
, symtab_size
);
156 if (err
!= (int)symtab_size
)
157 goto exit_free_cache
;
159 err
= nfp_cpp_read(cpp
, dram
, strtab_addr
, cache
->strtab
, strtab_size
);
160 if (err
!= (int)strtab_size
)
161 goto exit_free_cache
;
162 cache
->strtab
[strtab_size
] = '\0';
164 for (n
= 0; n
< cache
->num
; n
++)
165 nfp_rtsym_sw_entry_init(cache
, strtab_size
,
166 &cache
->symtab
[n
], &rtsymtab
[n
]);
180 * nfp_rtsym_count() - Get the number of RTSYM descriptors
181 * @rtbl: NFP RTsym table
183 * Return: Number of RTSYM descriptors
186 nfp_rtsym_count(struct nfp_rtsym_table
*rtbl
)
195 * nfp_rtsym_get() - Get the Nth RTSYM descriptor
196 * @rtbl: NFP RTsym table
197 * @idx: Index (0-based) of the RTSYM descriptor
199 * Return: const pointer to a struct nfp_rtsym descriptor, or NULL
201 const struct nfp_rtsym
*
202 nfp_rtsym_get(struct nfp_rtsym_table
*rtbl
, int idx
)
207 if (idx
>= rtbl
->num
)
210 return &rtbl
->symtab
[idx
];
214 * nfp_rtsym_lookup() - Return the RTSYM descriptor for a symbol name
215 * @rtbl: NFP RTsym table
218 * Return: const pointer to a struct nfp_rtsym descriptor, or NULL
220 const struct nfp_rtsym
*
221 nfp_rtsym_lookup(struct nfp_rtsym_table
*rtbl
, const char *name
)
228 for (n
= 0; n
< rtbl
->num
; n
++)
229 if (strcmp(name
, rtbl
->symtab
[n
].name
) == 0)
230 return &rtbl
->symtab
[n
];
236 * nfp_rtsym_read_le() - Read a simple unsigned scalar value from symbol
237 * @rtbl: NFP RTsym table
239 * @error: Poniter to error code (optional)
241 * Lookup a symbol, map, read it and return it's value. Value of the symbol
242 * will be interpreted as a simple little-endian unsigned value. Symbol can
243 * be 4 or 8 bytes in size.
245 * Return: value read, on error sets the error and returns ~0ULL.
248 nfp_rtsym_read_le(struct nfp_rtsym_table
*rtbl
, const char *name
, int *error
)
250 const struct nfp_rtsym
*sym
;
255 sym
= nfp_rtsym_lookup(rtbl
, name
);
261 id
= NFP_CPP_ISLAND_ID(sym
->target
, NFP_CPP_ACTION_RW
, 0, sym
->domain
);
264 printf("Reading symbol %s with size %" PRIu64
" at %" PRIx64
"\n",
265 name
, sym
->size
, sym
->addr
);
269 err
= nfp_cpp_readl(rtbl
->cpp
, id
, sym
->addr
, &val32
);
273 err
= nfp_cpp_readq(rtbl
->cpp
, id
, sym
->addr
, &val
);
276 printf("rtsym '%s' unsupported size: %" PRId64
"\n",
295 nfp_rtsym_map(struct nfp_rtsym_table
*rtbl
, const char *name
,
296 unsigned int min_size
, struct nfp_cpp_area
**area
)
298 const struct nfp_rtsym
*sym
;
302 printf("mapping symbol %s\n", name
);
304 sym
= nfp_rtsym_lookup(rtbl
, name
);
306 printf("symbol lookup fails for %s\n", name
);
310 if (sym
->size
< min_size
) {
311 printf("Symbol %s too small (%" PRIu64
" < %u)\n", name
,
312 sym
->size
, min_size
);
316 mem
= nfp_cpp_map_area(rtbl
->cpp
, sym
->domain
, sym
->target
, sym
->addr
,
319 printf("Failed to map symbol %s\n", name
);
323 printf("symbol %s with address %p\n", name
, mem
);