xref: /dpdk/drivers/net/nfp/nfpcore/nfp_rtsym.c (revision 30a1de105a5f40d77b344a891c4a68f79e815c43)
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 /*
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.
101  */
102 #define __round_mask(x, y) ((__typeof__(x))((y) - 1))
103 
104 #define round_up(x, y) \
105 	(__extension__ ({ \
106 		typeof(x) _x = (x); \
107 		((((_x) - 1) | __round_mask(_x, y)) + 1); \
108 	}))
109 
110 #define round_down(x, y) \
111 	(__extension__ ({ \
112 		typeof(x) _x = (x); \
113 		((_x) & ~__round_mask(_x, y)); \
114 	}))
115 
116 struct nfp_rtsym_table *
117 __nfp_rtsym_table_read(struct nfp_cpp *cpp, const struct nfp_mip *mip)
118 {
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) |
124 		NFP_ISL_EMEM0;
125 	int err, n, size;
126 
127 	if (!mip)
128 		return NULL;
129 
130 	nfp_mip_strtab(mip, &strtab_addr, &strtab_size);
131 	nfp_mip_symtab(mip, &symtab_addr, &symtab_size);
132 
133 	if (!symtab_size || !strtab_size || symtab_size % sizeof(*rtsymtab))
134 		return NULL;
135 
136 	/* Align to 64 bits */
137 	symtab_size = round_up(symtab_size, 8);
138 	strtab_size = round_up(strtab_size, 8);
139 
140 	rtsymtab = malloc(symtab_size);
141 	if (!rtsymtab)
142 		return NULL;
143 
144 	size = sizeof(*cache);
145 	size += symtab_size / sizeof(*rtsymtab) * sizeof(struct nfp_rtsym);
146 	size +=	strtab_size + 1;
147 	cache = malloc(size);
148 	if (!cache)
149 		goto exit_free_rtsym_raw;
150 
151 	cache->cpp = cpp;
152 	cache->num = symtab_size / sizeof(*rtsymtab);
153 	cache->strtab = (void *)&cache->symtab[cache->num];
154 
155 	err = nfp_cpp_read(cpp, dram, symtab_addr, rtsymtab, symtab_size);
156 	if (err != (int)symtab_size)
157 		goto exit_free_cache;
158 
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';
163 
164 	for (n = 0; n < cache->num; n++)
165 		nfp_rtsym_sw_entry_init(cache, strtab_size,
166 					&cache->symtab[n], &rtsymtab[n]);
167 
168 	free(rtsymtab);
169 
170 	return cache;
171 
172 exit_free_cache:
173 	free(cache);
174 exit_free_rtsym_raw:
175 	free(rtsymtab);
176 	return NULL;
177 }
178 
179 /*
180  * nfp_rtsym_count() - Get the number of RTSYM descriptors
181  * @rtbl:	NFP RTsym table
182  *
183  * Return: Number of RTSYM descriptors
184  */
185 int
186 nfp_rtsym_count(struct nfp_rtsym_table *rtbl)
187 {
188 	if (!rtbl)
189 		return -EINVAL;
190 
191 	return rtbl->num;
192 }
193 
194 /*
195  * nfp_rtsym_get() - Get the Nth RTSYM descriptor
196  * @rtbl:	NFP RTsym table
197  * @idx:	Index (0-based) of the RTSYM descriptor
198  *
199  * Return: const pointer to a struct nfp_rtsym descriptor, or NULL
200  */
201 const struct nfp_rtsym *
202 nfp_rtsym_get(struct nfp_rtsym_table *rtbl, int idx)
203 {
204 	if (!rtbl)
205 		return NULL;
206 
207 	if (idx >= rtbl->num)
208 		return NULL;
209 
210 	return &rtbl->symtab[idx];
211 }
212 
213 /*
214  * nfp_rtsym_lookup() - Return the RTSYM descriptor for a symbol name
215  * @rtbl:	NFP RTsym table
216  * @name:	Symbol name
217  *
218  * Return: const pointer to a struct nfp_rtsym descriptor, or NULL
219  */
220 const struct nfp_rtsym *
221 nfp_rtsym_lookup(struct nfp_rtsym_table *rtbl, const char *name)
222 {
223 	int n;
224 
225 	if (!rtbl)
226 		return NULL;
227 
228 	for (n = 0; n < rtbl->num; n++)
229 		if (strcmp(name, rtbl->symtab[n].name) == 0)
230 			return &rtbl->symtab[n];
231 
232 	return NULL;
233 }
234 
235 /*
236  * nfp_rtsym_read_le() - Read a simple unsigned scalar value from symbol
237  * @rtbl:	NFP RTsym table
238  * @name:	Symbol name
239  * @error:	Pointer to error code (optional)
240  *
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.
244  *
245  * Return: value read, on error sets the error and returns ~0ULL.
246  */
247 uint64_t
248 nfp_rtsym_read_le(struct nfp_rtsym_table *rtbl, const char *name, int *error)
249 {
250 	const struct nfp_rtsym *sym;
251 	uint32_t val32, id;
252 	uint64_t val;
253 	int err;
254 
255 	sym = nfp_rtsym_lookup(rtbl, name);
256 	if (!sym) {
257 		err = -ENOENT;
258 		goto exit;
259 	}
260 
261 	id = NFP_CPP_ISLAND_ID(sym->target, NFP_CPP_ACTION_RW, 0, sym->domain);
262 
263 #ifdef DEBUG
264 	printf("Reading symbol %s with size %" PRIu64 " at %" PRIx64 "\n",
265 		name, sym->size, sym->addr);
266 #endif
267 	switch (sym->size) {
268 	case 4:
269 		err = nfp_cpp_readl(rtbl->cpp, id, sym->addr, &val32);
270 		val = val32;
271 		break;
272 	case 8:
273 		err = nfp_cpp_readq(rtbl->cpp, id, sym->addr, &val);
274 		break;
275 	default:
276 		printf("rtsym '%s' unsupported size: %" PRId64 "\n",
277 			name, sym->size);
278 		err = -EINVAL;
279 		break;
280 	}
281 
282 	if (err)
283 		err = -EIO;
284 exit:
285 	if (error)
286 		*error = err;
287 
288 	if (err)
289 		return ~0ULL;
290 
291 	return val;
292 }
293 
294 uint8_t *
295 nfp_rtsym_map(struct nfp_rtsym_table *rtbl, const char *name,
296 	      unsigned int min_size, struct nfp_cpp_area **area)
297 {
298 	const struct nfp_rtsym *sym;
299 	uint8_t *mem;
300 
301 #ifdef DEBUG
302 	printf("mapping symbol %s\n", name);
303 #endif
304 	sym = nfp_rtsym_lookup(rtbl, name);
305 	if (!sym) {
306 		printf("symbol lookup fails for %s\n", name);
307 		return NULL;
308 	}
309 
310 	if (sym->size < min_size) {
311 		printf("Symbol %s too small (%" PRIu64 " < %u)\n", name,
312 			sym->size, min_size);
313 		return NULL;
314 	}
315 
316 	mem = nfp_cpp_map_area(rtbl->cpp, sym->domain, sym->target, sym->addr,
317 			       sym->size, area);
318 	if (!mem) {
319 		printf("Failed to map symbol %s\n", name);
320 		return NULL;
321 	}
322 #ifdef DEBUG
323 	printf("symbol %s with address %p\n", name, mem);
324 #endif
325 
326 	return mem;
327 }
328