1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
3
4 /*
5 * nfp_cpp.h
6 * Interface for low-level NFP CPP access.
7 * Authors: Jason McMullan <jason.mcmullan@netronome.com>
8 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
9 */
10 #ifndef __NFP_CPP_H__
11 #define __NFP_CPP_H__
12
13 #include <linux/ctype.h>
14 #include <linux/types.h>
15 #include <linux/sizes.h>
16 #include <linux/stringify.h>
17
18 #ifndef NFP_SUBSYS
19 #define NFP_SUBSYS "nfp"
20 #endif
21
22 #define string_format(x) __FILE__ ":" __stringify(__LINE__) ": " x
23
24 #define __nfp_err(cpp, fmt, args...) \
25 dev_err(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
26 #define __nfp_warn(cpp, fmt, args...) \
27 dev_warn(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
28 #define __nfp_info(cpp, fmt, args...) \
29 dev_info(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
30 #define __nfp_dbg(cpp, fmt, args...) \
31 dev_dbg(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
32 #define __nfp_printk(level, cpp, fmt, args...) \
33 dev_printk(level, nfp_cpp_device(cpp)->parent, \
34 NFP_SUBSYS ": " fmt, ## args)
35
36 #define nfp_err(cpp, fmt, args...) \
37 __nfp_err(cpp, string_format(fmt), ## args)
38 #define nfp_warn(cpp, fmt, args...) \
39 __nfp_warn(cpp, string_format(fmt), ## args)
40 #define nfp_info(cpp, fmt, args...) \
41 __nfp_info(cpp, string_format(fmt), ## args)
42 #define nfp_dbg(cpp, fmt, args...) \
43 __nfp_dbg(cpp, string_format(fmt), ## args)
44 #define nfp_printk(level, cpp, fmt, args...) \
45 __nfp_printk(level, cpp, string_format(fmt), ## args)
46
47 #define PCI_64BIT_BAR_COUNT 3
48
49 #define NFP_CPP_NUM_TARGETS 16
50 /* Max size of area it should be safe to request */
51 #define NFP_CPP_SAFE_AREA_SIZE SZ_2M
52
53 /* NFP_MUTEX_WAIT_* are timeouts in seconds when waiting for a mutex */
54 #define NFP_MUTEX_WAIT_FIRST_WARN 15
55 #define NFP_MUTEX_WAIT_NEXT_WARN 5
56 #define NFP_MUTEX_WAIT_ERROR 60
57
58 struct device;
59
60 struct nfp_cpp_area;
61 struct nfp_cpp;
62 struct resource;
63
64 /* Wildcard indicating a CPP read or write action
65 *
66 * The action used will be either read or write depending on whether a
67 * read or write instruction/call is performed on the NFP_CPP_ID. It
68 * is recomended that the RW action is used even if all actions to be
69 * performed on a NFP_CPP_ID are known to be only reads or writes.
70 * Doing so will in many cases save NFP CPP internal software
71 * resources.
72 */
73 #define NFP_CPP_ACTION_RW 32
74
75 #define NFP_CPP_TARGET_ID_MASK 0x1f
76
77 #define NFP_CPP_ATOMIC_RD(target, island) \
78 NFP_CPP_ISLAND_ID((target), 3, 0, (island))
79 #define NFP_CPP_ATOMIC_WR(target, island) \
80 NFP_CPP_ISLAND_ID((target), 4, 0, (island))
81
82 /**
83 * NFP_CPP_ID() - pack target, token, and action into a CPP ID.
84 * @target: NFP CPP target id
85 * @action: NFP CPP action id
86 * @token: NFP CPP token id
87 *
88 * Create a 32-bit CPP identifier representing the access to be made.
89 * These identifiers are used as parameters to other NFP CPP
90 * functions. Some CPP devices may allow wildcard identifiers to be
91 * specified.
92 *
93 * Return: NFP CPP ID
94 */
95 #define NFP_CPP_ID(target, action, token) \
96 ((((target) & 0x7f) << 24) | (((token) & 0xff) << 16) | \
97 (((action) & 0xff) << 8))
98
99 /**
100 * NFP_CPP_ISLAND_ID() - pack target, token, action, and island into a CPP ID.
101 * @target: NFP CPP target id
102 * @action: NFP CPP action id
103 * @token: NFP CPP token id
104 * @island: NFP CPP island id
105 *
106 * Create a 32-bit CPP identifier representing the access to be made.
107 * These identifiers are used as parameters to other NFP CPP
108 * functions. Some CPP devices may allow wildcard identifiers to be
109 * specified.
110 *
111 * Return: NFP CPP ID
112 */
113 #define NFP_CPP_ISLAND_ID(target, action, token, island) \
114 ((((target) & 0x7f) << 24) | (((token) & 0xff) << 16) | \
115 (((action) & 0xff) << 8) | (((island) & 0xff) << 0))
116
117 /**
118 * NFP_CPP_ID_TARGET_of() - Return the NFP CPP target of a NFP CPP ID
119 * @id: NFP CPP ID
120 *
121 * Return: NFP CPP target
122 */
NFP_CPP_ID_TARGET_of(u32 id)123 static inline u8 NFP_CPP_ID_TARGET_of(u32 id)
124 {
125 return (id >> 24) & NFP_CPP_TARGET_ID_MASK;
126 }
127
128 /**
129 * NFP_CPP_ID_TOKEN_of() - Return the NFP CPP token of a NFP CPP ID
130 * @id: NFP CPP ID
131 * Return: NFP CPP token
132 */
NFP_CPP_ID_TOKEN_of(u32 id)133 static inline u8 NFP_CPP_ID_TOKEN_of(u32 id)
134 {
135 return (id >> 16) & 0xff;
136 }
137
138 /**
139 * NFP_CPP_ID_ACTION_of() - Return the NFP CPP action of a NFP CPP ID
140 * @id: NFP CPP ID
141 *
142 * Return: NFP CPP action
143 */
NFP_CPP_ID_ACTION_of(u32 id)144 static inline u8 NFP_CPP_ID_ACTION_of(u32 id)
145 {
146 return (id >> 8) & 0xff;
147 }
148
149 /**
150 * NFP_CPP_ID_ISLAND_of() - Return the NFP CPP island of a NFP CPP ID
151 * @id: NFP CPP ID
152 *
153 * Return: NFP CPP island
154 */
NFP_CPP_ID_ISLAND_of(u32 id)155 static inline u8 NFP_CPP_ID_ISLAND_of(u32 id)
156 {
157 return (id >> 0) & 0xff;
158 }
159
160 /* NFP Interface types - logical interface for this CPP connection
161 * 4 bits are reserved for interface type.
162 */
163 #define NFP_CPP_INTERFACE_TYPE_INVALID 0x0
164 #define NFP_CPP_INTERFACE_TYPE_PCI 0x1
165 #define NFP_CPP_INTERFACE_TYPE_ARM 0x2
166 #define NFP_CPP_INTERFACE_TYPE_RPC 0x3
167 #define NFP_CPP_INTERFACE_TYPE_ILA 0x4
168
169 /**
170 * NFP_CPP_INTERFACE() - Construct a 16-bit NFP Interface ID
171 * @type: NFP Interface Type
172 * @unit: Unit identifier for the interface type
173 * @channel: Channel identifier for the interface unit
174 *
175 * Interface IDs consists of 4 bits of interface type,
176 * 4 bits of unit identifier, and 8 bits of channel identifier.
177 *
178 * The NFP Interface ID is used in the implementation of
179 * NFP CPP API mutexes, which use the MU Atomic CompareAndWrite
180 * operation - hence the limit to 16 bits to be able to
181 * use the NFP Interface ID as a lock owner.
182 *
183 * Return: Interface ID
184 */
185 #define NFP_CPP_INTERFACE(type, unit, channel) \
186 ((((type) & 0xf) << 12) | \
187 (((unit) & 0xf) << 8) | \
188 (((channel) & 0xff) << 0))
189
190 /**
191 * NFP_CPP_INTERFACE_TYPE_of() - Get the interface type
192 * @interface: NFP Interface ID
193 * Return: NFP Interface ID's type
194 */
195 #define NFP_CPP_INTERFACE_TYPE_of(interface) (((interface) >> 12) & 0xf)
196
197 /**
198 * NFP_CPP_INTERFACE_UNIT_of() - Get the interface unit
199 * @interface: NFP Interface ID
200 * Return: NFP Interface ID's unit
201 */
202 #define NFP_CPP_INTERFACE_UNIT_of(interface) (((interface) >> 8) & 0xf)
203
204 /**
205 * NFP_CPP_INTERFACE_CHANNEL_of() - Get the interface channel
206 * @interface: NFP Interface ID
207 * Return: NFP Interface ID's channel
208 */
209 #define NFP_CPP_INTERFACE_CHANNEL_of(interface) (((interface) >> 0) & 0xff)
210
211 /* Implemented in nfp_cppcore.c */
212 void nfp_cpp_free(struct nfp_cpp *cpp);
213 u32 nfp_cpp_model(struct nfp_cpp *cpp);
214 u16 nfp_cpp_interface(struct nfp_cpp *cpp);
215 int nfp_cpp_serial(struct nfp_cpp *cpp, const u8 **serial);
216 unsigned int nfp_cpp_mu_locality_lsb(struct nfp_cpp *cpp);
217
218 struct nfp_cpp_area *nfp_cpp_area_alloc_with_name(struct nfp_cpp *cpp,
219 u32 cpp_id,
220 const char *name,
221 unsigned long long address,
222 unsigned long size);
223 struct nfp_cpp_area *nfp_cpp_area_alloc(struct nfp_cpp *cpp, u32 cpp_id,
224 unsigned long long address,
225 unsigned long size);
226 struct nfp_cpp_area *
227 nfp_cpp_area_alloc_acquire(struct nfp_cpp *cpp, const char *name, u32 cpp_id,
228 unsigned long long address, unsigned long size);
229 void nfp_cpp_area_free(struct nfp_cpp_area *area);
230 int nfp_cpp_area_acquire(struct nfp_cpp_area *area);
231 int nfp_cpp_area_acquire_nonblocking(struct nfp_cpp_area *area);
232 void nfp_cpp_area_release(struct nfp_cpp_area *area);
233 void nfp_cpp_area_release_free(struct nfp_cpp_area *area);
234 int nfp_cpp_area_read(struct nfp_cpp_area *area, unsigned long offset,
235 void *buffer, size_t length);
236 int nfp_cpp_area_write(struct nfp_cpp_area *area, unsigned long offset,
237 const void *buffer, size_t length);
238 size_t nfp_cpp_area_size(struct nfp_cpp_area *area);
239 const char *nfp_cpp_area_name(struct nfp_cpp_area *cpp_area);
240 void *nfp_cpp_area_priv(struct nfp_cpp_area *cpp_area);
241 struct nfp_cpp *nfp_cpp_area_cpp(struct nfp_cpp_area *cpp_area);
242 struct resource *nfp_cpp_area_resource(struct nfp_cpp_area *area);
243 phys_addr_t nfp_cpp_area_phys(struct nfp_cpp_area *area);
244 void __iomem *nfp_cpp_area_iomem(struct nfp_cpp_area *area);
245
246 int nfp_cpp_area_readl(struct nfp_cpp_area *area, unsigned long offset,
247 u32 *value);
248 int nfp_cpp_area_writel(struct nfp_cpp_area *area, unsigned long offset,
249 u32 value);
250 int nfp_cpp_area_readq(struct nfp_cpp_area *area, unsigned long offset,
251 u64 *value);
252 int nfp_cpp_area_writeq(struct nfp_cpp_area *area, unsigned long offset,
253 u64 value);
254 int nfp_cpp_area_fill(struct nfp_cpp_area *area, unsigned long offset,
255 u32 value, size_t length);
256
257 int nfp_xpb_readl(struct nfp_cpp *cpp, u32 xpb_tgt, u32 *value);
258 int nfp_xpb_writel(struct nfp_cpp *cpp, u32 xpb_tgt, u32 value);
259 int nfp_xpb_writelm(struct nfp_cpp *cpp, u32 xpb_tgt, u32 mask, u32 value);
260
261 /* Implemented in nfp_cpplib.c */
262 int nfp_cpp_read(struct nfp_cpp *cpp, u32 cpp_id,
263 unsigned long long address, void *kernel_vaddr, size_t length);
264 int nfp_cpp_write(struct nfp_cpp *cpp, u32 cpp_id,
265 unsigned long long address, const void *kernel_vaddr,
266 size_t length);
267 int nfp_cpp_readl(struct nfp_cpp *cpp, u32 cpp_id,
268 unsigned long long address, u32 *value);
269 int nfp_cpp_writel(struct nfp_cpp *cpp, u32 cpp_id,
270 unsigned long long address, u32 value);
271 int nfp_cpp_readq(struct nfp_cpp *cpp, u32 cpp_id,
272 unsigned long long address, u64 *value);
273 int nfp_cpp_writeq(struct nfp_cpp *cpp, u32 cpp_id,
274 unsigned long long address, u64 value);
275
276 u8 __iomem *
277 nfp_cpp_map_area(struct nfp_cpp *cpp, const char *name, u32 cpp_id, u64 addr,
278 unsigned long size, struct nfp_cpp_area **area);
279
280 struct nfp_cpp_mutex;
281
282 int nfp_cpp_mutex_init(struct nfp_cpp *cpp, int target,
283 unsigned long long address, u32 key_id);
284 struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target,
285 unsigned long long address,
286 u32 key_id);
287 void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex);
288 int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex);
289 int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex);
290 int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex);
291 int nfp_cpp_mutex_reclaim(struct nfp_cpp *cpp, int target,
292 unsigned long long address);
293
294 /**
295 * nfp_cppcore_pcie_unit() - Get PCI Unit of a CPP handle
296 * @cpp: CPP handle
297 *
298 * Return: PCI unit for the NFP CPP handle
299 */
nfp_cppcore_pcie_unit(struct nfp_cpp * cpp)300 static inline u8 nfp_cppcore_pcie_unit(struct nfp_cpp *cpp)
301 {
302 return NFP_CPP_INTERFACE_UNIT_of(nfp_cpp_interface(cpp));
303 }
304
305 struct nfp_cpp_explicit;
306
307 struct nfp_cpp_explicit_command {
308 u32 cpp_id;
309 u16 data_ref;
310 u8 data_master;
311 u8 len;
312 u8 byte_mask;
313 u8 signal_master;
314 u8 signal_ref;
315 u8 posted;
316 u8 siga;
317 u8 sigb;
318 s8 siga_mode;
319 s8 sigb_mode;
320 };
321
322 #define NFP_SERIAL_LEN 6
323
324 /**
325 * struct nfp_cpp_operations - NFP CPP operations structure
326 * @area_priv_size: Size of the nfp_cpp_area private data
327 * @owner: Owner module
328 * @init: Initialize the NFP CPP bus
329 * @free: Free the bus
330 * @read_serial: Read serial number to memory provided
331 * @get_interface: Return CPP interface
332 * @area_init: Initialize a new NFP CPP area (not serialized)
333 * @area_cleanup: Clean up a NFP CPP area (not serialized)
334 * @area_acquire: Acquire the NFP CPP area (serialized)
335 * @area_release: Release area (serialized)
336 * @area_resource: Get resource range of area (not serialized)
337 * @area_phys: Get physical address of area (not serialized)
338 * @area_iomem: Get iomem of area (not serialized)
339 * @area_read: Perform a read from a NFP CPP area (serialized)
340 * @area_write: Perform a write to a NFP CPP area (serialized)
341 * @explicit_priv_size: Size of an explicit's private area
342 * @explicit_acquire: Acquire an explicit area
343 * @explicit_release: Release an explicit area
344 * @explicit_put: Write data to send
345 * @explicit_get: Read data received
346 * @explicit_do: Perform the transaction
347 */
348 struct nfp_cpp_operations {
349 size_t area_priv_size;
350 struct module *owner;
351
352 int (*init)(struct nfp_cpp *cpp);
353 void (*free)(struct nfp_cpp *cpp);
354
355 int (*read_serial)(struct device *dev, u8 *serial);
356 int (*get_interface)(struct device *dev);
357
358 int (*area_init)(struct nfp_cpp_area *area,
359 u32 dest, unsigned long long address,
360 unsigned long size);
361 void (*area_cleanup)(struct nfp_cpp_area *area);
362 int (*area_acquire)(struct nfp_cpp_area *area);
363 void (*area_release)(struct nfp_cpp_area *area);
364 struct resource *(*area_resource)(struct nfp_cpp_area *area);
365 phys_addr_t (*area_phys)(struct nfp_cpp_area *area);
366 void __iomem *(*area_iomem)(struct nfp_cpp_area *area);
367 int (*area_read)(struct nfp_cpp_area *area, void *kernel_vaddr,
368 unsigned long offset, unsigned int length);
369 int (*area_write)(struct nfp_cpp_area *area, const void *kernel_vaddr,
370 unsigned long offset, unsigned int length);
371
372 size_t explicit_priv_size;
373 int (*explicit_acquire)(struct nfp_cpp_explicit *expl);
374 void (*explicit_release)(struct nfp_cpp_explicit *expl);
375 int (*explicit_put)(struct nfp_cpp_explicit *expl,
376 const void *buff, size_t len);
377 int (*explicit_get)(struct nfp_cpp_explicit *expl,
378 void *buff, size_t len);
379 int (*explicit_do)(struct nfp_cpp_explicit *expl,
380 const struct nfp_cpp_explicit_command *cmd,
381 u64 address);
382 };
383
384 struct nfp_cpp *
385 nfp_cpp_from_operations(const struct nfp_cpp_operations *ops,
386 struct device *parent, void *priv);
387 void *nfp_cpp_priv(struct nfp_cpp *priv);
388
389 int nfp_cpp_area_cache_add(struct nfp_cpp *cpp, size_t size);
390
391 /* The following section contains extensions to the
392 * NFP CPP API, to be used in a Linux kernel-space context.
393 */
394
395 /* Use this channel ID for multiple virtual channel interfaces
396 * (ie ARM and PCIe) when setting up the interface field.
397 */
398 #define NFP_CPP_INTERFACE_CHANNEL_PEROPENER 255
399 struct device *nfp_cpp_device(struct nfp_cpp *cpp);
400
401 /* Return code masks for nfp_cpp_explicit_do()
402 */
403 #define NFP_SIGNAL_MASK_A BIT(0) /* Signal A fired */
404 #define NFP_SIGNAL_MASK_B BIT(1) /* Signal B fired */
405
406 enum nfp_cpp_explicit_signal_mode {
407 NFP_SIGNAL_NONE = 0,
408 NFP_SIGNAL_PUSH = 1,
409 NFP_SIGNAL_PUSH_OPTIONAL = -1,
410 NFP_SIGNAL_PULL = 2,
411 NFP_SIGNAL_PULL_OPTIONAL = -2,
412 };
413
414 struct nfp_cpp_explicit *nfp_cpp_explicit_acquire(struct nfp_cpp *cpp);
415 int nfp_cpp_explicit_set_target(struct nfp_cpp_explicit *expl, u32 cpp_id,
416 u8 len, u8 mask);
417 int nfp_cpp_explicit_set_data(struct nfp_cpp_explicit *expl,
418 u8 data_master, u16 data_ref);
419 int nfp_cpp_explicit_set_signal(struct nfp_cpp_explicit *expl,
420 u8 signal_master, u8 signal_ref);
421 int nfp_cpp_explicit_set_posted(struct nfp_cpp_explicit *expl, int posted,
422 u8 siga,
423 enum nfp_cpp_explicit_signal_mode siga_mode,
424 u8 sigb,
425 enum nfp_cpp_explicit_signal_mode sigb_mode);
426 int nfp_cpp_explicit_put(struct nfp_cpp_explicit *expl,
427 const void *buff, size_t len);
428 int nfp_cpp_explicit_do(struct nfp_cpp_explicit *expl, u64 address);
429 int nfp_cpp_explicit_get(struct nfp_cpp_explicit *expl, void *buff, size_t len);
430 void nfp_cpp_explicit_release(struct nfp_cpp_explicit *expl);
431 struct nfp_cpp *nfp_cpp_explicit_cpp(struct nfp_cpp_explicit *expl);
432 void *nfp_cpp_explicit_priv(struct nfp_cpp_explicit *cpp_explicit);
433
434 /* Implemented in nfp_cpplib.c */
435
436 int nfp_cpp_model_autodetect(struct nfp_cpp *cpp, u32 *model);
437
438 int nfp_cpp_explicit_read(struct nfp_cpp *cpp, u32 cpp_id,
439 u64 addr, void *buff, size_t len,
440 int width_read);
441
442 int nfp_cpp_explicit_write(struct nfp_cpp *cpp, u32 cpp_id,
443 u64 addr, const void *buff, size_t len,
444 int width_write);
445
446 #endif /* !__NFP_CPP_H__ */
447