2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * Interface for accessing run-time symbol table
37 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38 * Jason McMullan <jason.mcmullan@netronome.com>
39 * Espen Skoglund <espen.skoglund@netronome.com>
40 * Francois H. Theron <francois.theron@netronome.com>
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/io-64-nonatomic-hi-lo.h>
50 #include "nfp6000/nfp6000.h"
52 /* These need to match the linker */
53 #define SYM_TGT_LMEM 0
54 #define SYM_TGT_EMU_CACHE 0x17
56 struct nfp_rtsym_entry {
68 struct nfp_rtsym_table {
72 struct nfp_rtsym symtab[];
75 static int nfp_meid(u8 island_id, u8 menum)
77 return (island_id & 0x3F) == island_id && menum < 12 ?
78 (island_id << 4) | (menum + 4) : -1;
82 nfp_rtsym_sw_entry_init(struct nfp_rtsym_table *cache, u32 strtab_size,
83 struct nfp_rtsym *sw, struct nfp_rtsym_entry *fw)
86 sw->name = cache->strtab + le16_to_cpu(fw->name) % strtab_size;
87 sw->addr = ((u64)fw->addr_hi << 32) | le32_to_cpu(fw->addr_lo);
88 sw->size = ((u64)fw->size_hi << 32) | le32_to_cpu(fw->size_lo);
92 sw->target = NFP_RTSYM_TARGET_LMEM;
94 case SYM_TGT_EMU_CACHE:
95 sw->target = NFP_RTSYM_TARGET_EMU_CACHE;
98 sw->target = fw->target;
102 if (fw->menum != 0xff)
103 sw->domain = nfp_meid(fw->island, fw->menum);
104 else if (fw->island != 0xff)
105 sw->domain = fw->island;
110 struct nfp_rtsym_table *nfp_rtsym_table_read(struct nfp_cpp *cpp)
112 struct nfp_rtsym_table *rtbl;
113 const struct nfp_mip *mip;
115 mip = nfp_mip_open(cpp);
116 rtbl = __nfp_rtsym_table_read(cpp, mip);
122 struct nfp_rtsym_table *
123 __nfp_rtsym_table_read(struct nfp_cpp *cpp, const struct nfp_mip *mip)
125 const u32 dram = NFP_CPP_ID(NFP_CPP_TARGET_MU, NFP_CPP_ACTION_RW, 0) |
127 u32 strtab_addr, symtab_addr, strtab_size, symtab_size;
128 struct nfp_rtsym_entry *rtsymtab;
129 struct nfp_rtsym_table *cache;
135 nfp_mip_strtab(mip, &strtab_addr, &strtab_size);
136 nfp_mip_symtab(mip, &symtab_addr, &symtab_size);
138 if (!symtab_size || !strtab_size || symtab_size % sizeof(*rtsymtab))
141 /* Align to 64 bits */
142 symtab_size = round_up(symtab_size, 8);
143 strtab_size = round_up(strtab_size, 8);
145 rtsymtab = kmalloc(symtab_size, GFP_KERNEL);
149 size = sizeof(*cache);
150 size += symtab_size / sizeof(*rtsymtab) * sizeof(struct nfp_rtsym);
151 size += strtab_size + 1;
152 cache = kmalloc(size, GFP_KERNEL);
154 goto exit_free_rtsym_raw;
157 cache->num = symtab_size / sizeof(*rtsymtab);
158 cache->strtab = (void *)&cache->symtab[cache->num];
160 err = nfp_cpp_read(cpp, dram, symtab_addr, rtsymtab, symtab_size);
161 if (err != symtab_size)
162 goto exit_free_cache;
164 err = nfp_cpp_read(cpp, dram, strtab_addr, cache->strtab, strtab_size);
165 if (err != strtab_size)
166 goto exit_free_cache;
167 cache->strtab[strtab_size] = '\0';
169 for (n = 0; n < cache->num; n++)
170 nfp_rtsym_sw_entry_init(cache, strtab_size,
171 &cache->symtab[n], &rtsymtab[n]);
185 * nfp_rtsym_count() - Get the number of RTSYM descriptors
186 * @rtbl: NFP RTsym table
188 * Return: Number of RTSYM descriptors
190 int nfp_rtsym_count(struct nfp_rtsym_table *rtbl)
198 * nfp_rtsym_get() - Get the Nth RTSYM descriptor
199 * @rtbl: NFP RTsym table
200 * @idx: Index (0-based) of the RTSYM descriptor
202 * Return: const pointer to a struct nfp_rtsym descriptor, or NULL
204 const struct nfp_rtsym *nfp_rtsym_get(struct nfp_rtsym_table *rtbl, int idx)
208 if (idx >= rtbl->num)
211 return &rtbl->symtab[idx];
215 * nfp_rtsym_lookup() - Return the RTSYM descriptor for a symbol name
216 * @rtbl: NFP RTsym table
219 * Return: const pointer to a struct nfp_rtsym descriptor, or NULL
221 const struct nfp_rtsym *
222 nfp_rtsym_lookup(struct nfp_rtsym_table *rtbl, const char *name)
229 for (n = 0; n < rtbl->num; n++)
230 if (strcmp(name, rtbl->symtab[n].name) == 0)
231 return &rtbl->symtab[n];
237 * nfp_rtsym_read_le() - Read a simple unsigned scalar value from symbol
238 * @rtbl: NFP RTsym table
240 * @error: Poniter to error code (optional)
242 * Lookup a symbol, map, read it and return it's value. Value of the symbol
243 * will be interpreted as a simple little-endian unsigned value. Symbol can
244 * be 4 or 8 bytes in size.
246 * Return: value read, on error sets the error and returns ~0ULL.
248 u64 nfp_rtsym_read_le(struct nfp_rtsym_table *rtbl, const char *name,
251 const struct nfp_rtsym *sym;
256 sym = nfp_rtsym_lookup(rtbl, name);
262 id = NFP_CPP_ISLAND_ID(sym->target, NFP_CPP_ACTION_RW, 0, sym->domain);
266 err = nfp_cpp_readl(rtbl->cpp, id, sym->addr, &val32);
270 err = nfp_cpp_readq(rtbl->cpp, id, sym->addr, &val);
274 "rtsym '%s' unsupported or non-scalar size: %lld\n",
280 if (err == sym->size)
294 nfp_rtsym_map(struct nfp_rtsym_table *rtbl, const char *name, const char *id,
295 unsigned int min_size, struct nfp_cpp_area **area)
297 const struct nfp_rtsym *sym;
300 sym = nfp_rtsym_lookup(rtbl, name);
302 return (u8 __iomem *)ERR_PTR(-ENOENT);
304 if (sym->size < min_size) {
305 nfp_err(rtbl->cpp, "Symbol %s too small\n", name);
306 return (u8 __iomem *)ERR_PTR(-EINVAL);
309 mem = nfp_cpp_map_area(rtbl->cpp, id, sym->domain, sym->target,
310 sym->addr, sym->size, area);
312 nfp_err(rtbl->cpp, "Failed to map symbol %s: %ld\n",