1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
6 * Interface for low-level NFP CPP access.
7 * Authors: Jason McMullan <jason.mcmullan@netronome.com>
8 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
13 #include <linux/ctype.h>
14 #include <linux/types.h>
15 #include <linux/sizes.h>
18 #define NFP_SUBSYS "nfp"
21 #define nfp_err(cpp, fmt, args...) \
22 dev_err(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
23 #define nfp_warn(cpp, fmt, args...) \
24 dev_warn(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
25 #define nfp_info(cpp, fmt, args...) \
26 dev_info(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
27 #define nfp_dbg(cpp, fmt, args...) \
28 dev_dbg(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
29 #define nfp_printk(level, cpp, fmt, args...) \
30 dev_printk(level, nfp_cpp_device(cpp)->parent, \
31 NFP_SUBSYS ": " fmt, ## args)
33 #define PCI_64BIT_BAR_COUNT 3
35 /* NFP hardware vendor/device ids.
37 #define PCI_DEVICE_ID_NETRONOME_NFP3800 0x3800
39 #define NFP_CPP_NUM_TARGETS 16
40 /* Max size of area it should be safe to request */
41 #define NFP_CPP_SAFE_AREA_SIZE SZ_2M
43 /* NFP_MUTEX_WAIT_* are timeouts in seconds when waiting for a mutex */
44 #define NFP_MUTEX_WAIT_FIRST_WARN 15
45 #define NFP_MUTEX_WAIT_NEXT_WARN 5
46 #define NFP_MUTEX_WAIT_ERROR 60
54 /* Wildcard indicating a CPP read or write action
56 * The action used will be either read or write depending on whether a
57 * read or write instruction/call is performed on the NFP_CPP_ID. It
58 * is recomended that the RW action is used even if all actions to be
59 * performed on a NFP_CPP_ID are known to be only reads or writes.
60 * Doing so will in many cases save NFP CPP internal software
63 #define NFP_CPP_ACTION_RW 32
65 #define NFP_CPP_TARGET_ID_MASK 0x1f
67 #define NFP_CPP_ATOMIC_RD(target, island) \
68 NFP_CPP_ISLAND_ID((target), 3, 0, (island))
69 #define NFP_CPP_ATOMIC_WR(target, island) \
70 NFP_CPP_ISLAND_ID((target), 4, 0, (island))
73 * NFP_CPP_ID() - pack target, token, and action into a CPP ID.
74 * @target: NFP CPP target id
75 * @action: NFP CPP action id
76 * @token: NFP CPP token id
78 * Create a 32-bit CPP identifier representing the access to be made.
79 * These identifiers are used as parameters to other NFP CPP
80 * functions. Some CPP devices may allow wildcard identifiers to be
85 #define NFP_CPP_ID(target, action, token) \
86 ((((target) & 0x7f) << 24) | (((token) & 0xff) << 16) | \
87 (((action) & 0xff) << 8))
90 * NFP_CPP_ISLAND_ID() - pack target, token, action, and island into a CPP ID.
91 * @target: NFP CPP target id
92 * @action: NFP CPP action id
93 * @token: NFP CPP token id
94 * @island: NFP CPP island id
96 * Create a 32-bit CPP identifier representing the access to be made.
97 * These identifiers are used as parameters to other NFP CPP
98 * functions. Some CPP devices may allow wildcard identifiers to be
103 #define NFP_CPP_ISLAND_ID(target, action, token, island) \
104 ((((target) & 0x7f) << 24) | (((token) & 0xff) << 16) | \
105 (((action) & 0xff) << 8) | (((island) & 0xff) << 0))
108 * NFP_CPP_ID_TARGET_of() - Return the NFP CPP target of a NFP CPP ID
111 * Return: NFP CPP target
113 static inline u8 NFP_CPP_ID_TARGET_of(u32 id)
115 return (id >> 24) & NFP_CPP_TARGET_ID_MASK;
119 * NFP_CPP_ID_TOKEN_of() - Return the NFP CPP token of a NFP CPP ID
121 * Return: NFP CPP token
123 static inline u8 NFP_CPP_ID_TOKEN_of(u32 id)
125 return (id >> 16) & 0xff;
129 * NFP_CPP_ID_ACTION_of() - Return the NFP CPP action of a NFP CPP ID
132 * Return: NFP CPP action
134 static inline u8 NFP_CPP_ID_ACTION_of(u32 id)
136 return (id >> 8) & 0xff;
140 * NFP_CPP_ID_ISLAND_of() - Return the NFP CPP island of a NFP CPP ID
143 * Return: NFP CPP island
145 static inline u8 NFP_CPP_ID_ISLAND_of(u32 id)
147 return (id >> 0) & 0xff;
150 /* NFP Interface types - logical interface for this CPP connection
151 * 4 bits are reserved for interface type.
153 #define NFP_CPP_INTERFACE_TYPE_INVALID 0x0
154 #define NFP_CPP_INTERFACE_TYPE_PCI 0x1
155 #define NFP_CPP_INTERFACE_TYPE_ARM 0x2
156 #define NFP_CPP_INTERFACE_TYPE_RPC 0x3
157 #define NFP_CPP_INTERFACE_TYPE_ILA 0x4
160 * NFP_CPP_INTERFACE() - Construct a 16-bit NFP Interface ID
161 * @type: NFP Interface Type
162 * @unit: Unit identifier for the interface type
163 * @channel: Channel identifier for the interface unit
165 * Interface IDs consists of 4 bits of interface type,
166 * 4 bits of unit identifier, and 8 bits of channel identifier.
168 * The NFP Interface ID is used in the implementation of
169 * NFP CPP API mutexes, which use the MU Atomic CompareAndWrite
170 * operation - hence the limit to 16 bits to be able to
171 * use the NFP Interface ID as a lock owner.
173 * Return: Interface ID
175 #define NFP_CPP_INTERFACE(type, unit, channel) \
176 ((((type) & 0xf) << 12) | \
177 (((unit) & 0xf) << 8) | \
178 (((channel) & 0xff) << 0))
181 * NFP_CPP_INTERFACE_TYPE_of() - Get the interface type
182 * @interface: NFP Interface ID
183 * Return: NFP Interface ID's type
185 #define NFP_CPP_INTERFACE_TYPE_of(interface) (((interface) >> 12) & 0xf)
188 * NFP_CPP_INTERFACE_UNIT_of() - Get the interface unit
189 * @interface: NFP Interface ID
190 * Return: NFP Interface ID's unit
192 #define NFP_CPP_INTERFACE_UNIT_of(interface) (((interface) >> 8) & 0xf)
195 * NFP_CPP_INTERFACE_CHANNEL_of() - Get the interface channel
196 * @interface: NFP Interface ID
197 * Return: NFP Interface ID's channel
199 #define NFP_CPP_INTERFACE_CHANNEL_of(interface) (((interface) >> 0) & 0xff)
201 /* Implemented in nfp_cppcore.c */
202 void nfp_cpp_free(struct nfp_cpp *cpp);
203 u32 nfp_cpp_model(struct nfp_cpp *cpp);
204 u16 nfp_cpp_interface(struct nfp_cpp *cpp);
205 int nfp_cpp_serial(struct nfp_cpp *cpp, const u8 **serial);
206 unsigned int nfp_cpp_mu_locality_lsb(struct nfp_cpp *cpp);
208 struct nfp_cpp_area *nfp_cpp_area_alloc_with_name(struct nfp_cpp *cpp,
211 unsigned long long address,
213 struct nfp_cpp_area *nfp_cpp_area_alloc(struct nfp_cpp *cpp, u32 cpp_id,
214 unsigned long long address,
216 struct nfp_cpp_area *
217 nfp_cpp_area_alloc_acquire(struct nfp_cpp *cpp, const char *name, u32 cpp_id,
218 unsigned long long address, unsigned long size);
219 void nfp_cpp_area_free(struct nfp_cpp_area *area);
220 int nfp_cpp_area_acquire(struct nfp_cpp_area *area);
221 int nfp_cpp_area_acquire_nonblocking(struct nfp_cpp_area *area);
222 void nfp_cpp_area_release(struct nfp_cpp_area *area);
223 void nfp_cpp_area_release_free(struct nfp_cpp_area *area);
224 int nfp_cpp_area_read(struct nfp_cpp_area *area, unsigned long offset,
225 void *buffer, size_t length);
226 int nfp_cpp_area_write(struct nfp_cpp_area *area, unsigned long offset,
227 const void *buffer, size_t length);
228 size_t nfp_cpp_area_size(struct nfp_cpp_area *area);
229 const char *nfp_cpp_area_name(struct nfp_cpp_area *cpp_area);
230 void *nfp_cpp_area_priv(struct nfp_cpp_area *cpp_area);
231 struct nfp_cpp *nfp_cpp_area_cpp(struct nfp_cpp_area *cpp_area);
232 struct resource *nfp_cpp_area_resource(struct nfp_cpp_area *area);
233 phys_addr_t nfp_cpp_area_phys(struct nfp_cpp_area *area);
234 void __iomem *nfp_cpp_area_iomem(struct nfp_cpp_area *area);
236 int nfp_cpp_area_readl(struct nfp_cpp_area *area, unsigned long offset,
238 int nfp_cpp_area_writel(struct nfp_cpp_area *area, unsigned long offset,
240 int nfp_cpp_area_readq(struct nfp_cpp_area *area, unsigned long offset,
242 int nfp_cpp_area_writeq(struct nfp_cpp_area *area, unsigned long offset,
244 int nfp_cpp_area_fill(struct nfp_cpp_area *area, unsigned long offset,
245 u32 value, size_t length);
247 int nfp_xpb_readl(struct nfp_cpp *cpp, u32 xpb_tgt, u32 *value);
248 int nfp_xpb_writel(struct nfp_cpp *cpp, u32 xpb_tgt, u32 value);
249 int nfp_xpb_writelm(struct nfp_cpp *cpp, u32 xpb_tgt, u32 mask, u32 value);
251 /* Implemented in nfp_cpplib.c */
252 int nfp_cpp_read(struct nfp_cpp *cpp, u32 cpp_id,
253 unsigned long long address, void *kernel_vaddr, size_t length);
254 int nfp_cpp_write(struct nfp_cpp *cpp, u32 cpp_id,
255 unsigned long long address, const void *kernel_vaddr,
257 int nfp_cpp_readl(struct nfp_cpp *cpp, u32 cpp_id,
258 unsigned long long address, u32 *value);
259 int nfp_cpp_writel(struct nfp_cpp *cpp, u32 cpp_id,
260 unsigned long long address, u32 value);
261 int nfp_cpp_readq(struct nfp_cpp *cpp, u32 cpp_id,
262 unsigned long long address, u64 *value);
263 int nfp_cpp_writeq(struct nfp_cpp *cpp, u32 cpp_id,
264 unsigned long long address, u64 value);
267 nfp_cpp_map_area(struct nfp_cpp *cpp, const char *name, u32 cpp_id, u64 addr,
268 unsigned long size, struct nfp_cpp_area **area);
270 struct nfp_cpp_mutex;
272 int nfp_cpp_mutex_init(struct nfp_cpp *cpp, int target,
273 unsigned long long address, u32 key_id);
274 struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target,
275 unsigned long long address,
277 void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex);
278 int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex);
279 int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex);
280 int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex);
281 int nfp_cpp_mutex_reclaim(struct nfp_cpp *cpp, int target,
282 unsigned long long address);
285 * nfp_cppcore_pcie_unit() - Get PCI Unit of a CPP handle
288 * Return: PCI unit for the NFP CPP handle
290 static inline u8 nfp_cppcore_pcie_unit(struct nfp_cpp *cpp)
292 return NFP_CPP_INTERFACE_UNIT_of(nfp_cpp_interface(cpp));
295 struct nfp_cpp_explicit;
297 struct nfp_cpp_explicit_command {
312 #define NFP_SERIAL_LEN 6
315 * struct nfp_cpp_operations - NFP CPP operations structure
316 * @area_priv_size: Size of the nfp_cpp_area private data
317 * @owner: Owner module
318 * @init: Initialize the NFP CPP bus
319 * @free: Free the bus
320 * @read_serial: Read serial number to memory provided
321 * @get_interface: Return CPP interface
322 * @area_init: Initialize a new NFP CPP area (not serialized)
323 * @area_cleanup: Clean up a NFP CPP area (not serialized)
324 * @area_acquire: Acquire the NFP CPP area (serialized)
325 * @area_release: Release area (serialized)
326 * @area_resource: Get resource range of area (not serialized)
327 * @area_phys: Get physical address of area (not serialized)
328 * @area_iomem: Get iomem of area (not serialized)
329 * @area_read: Perform a read from a NFP CPP area (serialized)
330 * @area_write: Perform a write to a NFP CPP area (serialized)
331 * @explicit_priv_size: Size of an explicit's private area
332 * @explicit_acquire: Acquire an explicit area
333 * @explicit_release: Release an explicit area
334 * @explicit_put: Write data to send
335 * @explicit_get: Read data received
336 * @explicit_do: Perform the transaction
338 struct nfp_cpp_operations {
339 size_t area_priv_size;
340 struct module *owner;
342 int (*init)(struct nfp_cpp *cpp);
343 void (*free)(struct nfp_cpp *cpp);
345 int (*read_serial)(struct device *dev, u8 *serial);
346 int (*get_interface)(struct device *dev);
348 int (*area_init)(struct nfp_cpp_area *area,
349 u32 dest, unsigned long long address,
351 void (*area_cleanup)(struct nfp_cpp_area *area);
352 int (*area_acquire)(struct nfp_cpp_area *area);
353 void (*area_release)(struct nfp_cpp_area *area);
354 struct resource *(*area_resource)(struct nfp_cpp_area *area);
355 phys_addr_t (*area_phys)(struct nfp_cpp_area *area);
356 void __iomem *(*area_iomem)(struct nfp_cpp_area *area);
357 int (*area_read)(struct nfp_cpp_area *area, void *kernel_vaddr,
358 unsigned long offset, unsigned int length);
359 int (*area_write)(struct nfp_cpp_area *area, const void *kernel_vaddr,
360 unsigned long offset, unsigned int length);
362 size_t explicit_priv_size;
363 int (*explicit_acquire)(struct nfp_cpp_explicit *expl);
364 void (*explicit_release)(struct nfp_cpp_explicit *expl);
365 int (*explicit_put)(struct nfp_cpp_explicit *expl,
366 const void *buff, size_t len);
367 int (*explicit_get)(struct nfp_cpp_explicit *expl,
368 void *buff, size_t len);
369 int (*explicit_do)(struct nfp_cpp_explicit *expl,
370 const struct nfp_cpp_explicit_command *cmd,
375 nfp_cpp_from_operations(const struct nfp_cpp_operations *ops,
376 struct device *parent, void *priv);
377 void *nfp_cpp_priv(struct nfp_cpp *priv);
379 int nfp_cpp_area_cache_add(struct nfp_cpp *cpp, size_t size);
381 /* The following section contains extensions to the
382 * NFP CPP API, to be used in a Linux kernel-space context.
385 /* Use this channel ID for multiple virtual channel interfaces
386 * (ie ARM and PCIe) when setting up the interface field.
388 #define NFP_CPP_INTERFACE_CHANNEL_PEROPENER 255
389 struct device *nfp_cpp_device(struct nfp_cpp *cpp);
391 /* Return code masks for nfp_cpp_explicit_do()
393 #define NFP_SIGNAL_MASK_A BIT(0) /* Signal A fired */
394 #define NFP_SIGNAL_MASK_B BIT(1) /* Signal B fired */
396 enum nfp_cpp_explicit_signal_mode {
399 NFP_SIGNAL_PUSH_OPTIONAL = -1,
401 NFP_SIGNAL_PULL_OPTIONAL = -2,
404 struct nfp_cpp_explicit *nfp_cpp_explicit_acquire(struct nfp_cpp *cpp);
405 int nfp_cpp_explicit_set_target(struct nfp_cpp_explicit *expl, u32 cpp_id,
407 int nfp_cpp_explicit_set_data(struct nfp_cpp_explicit *expl,
408 u8 data_master, u16 data_ref);
409 int nfp_cpp_explicit_set_signal(struct nfp_cpp_explicit *expl,
410 u8 signal_master, u8 signal_ref);
411 int nfp_cpp_explicit_set_posted(struct nfp_cpp_explicit *expl, int posted,
413 enum nfp_cpp_explicit_signal_mode siga_mode,
415 enum nfp_cpp_explicit_signal_mode sigb_mode);
416 int nfp_cpp_explicit_put(struct nfp_cpp_explicit *expl,
417 const void *buff, size_t len);
418 int nfp_cpp_explicit_do(struct nfp_cpp_explicit *expl, u64 address);
419 int nfp_cpp_explicit_get(struct nfp_cpp_explicit *expl, void *buff, size_t len);
420 void nfp_cpp_explicit_release(struct nfp_cpp_explicit *expl);
421 struct nfp_cpp *nfp_cpp_explicit_cpp(struct nfp_cpp_explicit *expl);
422 void *nfp_cpp_explicit_priv(struct nfp_cpp_explicit *cpp_explicit);
424 /* Implemented in nfp_cpplib.c */
426 int nfp_cpp_model_autodetect(struct nfp_cpp *cpp, u32 *model);
428 int nfp_cpp_explicit_read(struct nfp_cpp *cpp, u32 cpp_id,
429 u64 addr, void *buff, size_t len,
432 int nfp_cpp_explicit_write(struct nfp_cpp *cpp, u32 cpp_id,
433 u64 addr, const void *buff, size_t len,
436 #endif /* !__NFP_CPP_H__ */