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 low-level NFP CPP access.
37 * Authors: Jason McMullan <jason.mcmullan@netronome.com>
38 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
43 #include <linux/ctype.h>
44 #include <linux/types.h>
45 #include <linux/sizes.h>
48 #define NFP_SUBSYS "nfp"
51 #define nfp_err(cpp, fmt, args...) \
52 dev_err(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
53 #define nfp_warn(cpp, fmt, args...) \
54 dev_warn(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
55 #define nfp_info(cpp, fmt, args...) \
56 dev_info(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
57 #define nfp_dbg(cpp, fmt, args...) \
58 dev_dbg(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
59 #define nfp_printk(level, cpp, fmt, args...) \
60 dev_printk(level, nfp_cpp_device(cpp)->parent, \
61 NFP_SUBSYS ": " fmt, ## args)
63 #define PCI_64BIT_BAR_COUNT 3
65 /* NFP hardware vendor/device ids.
67 #define PCI_DEVICE_ID_NETRONOME_NFP3800 0x3800
69 #define NFP_CPP_NUM_TARGETS 16
70 /* Max size of area it should be safe to request */
71 #define NFP_CPP_SAFE_AREA_SIZE SZ_2M
73 /* NFP_MUTEX_WAIT_* are timeouts in seconds when waiting for a mutex */
74 #define NFP_MUTEX_WAIT_FIRST_WARN 15
75 #define NFP_MUTEX_WAIT_NEXT_WARN 5
76 #define NFP_MUTEX_WAIT_ERROR 60
84 /* Wildcard indicating a CPP read or write action
86 * The action used will be either read or write depending on whether a
87 * read or write instruction/call is performed on the NFP_CPP_ID. It
88 * is recomended that the RW action is used even if all actions to be
89 * performed on a NFP_CPP_ID are known to be only reads or writes.
90 * Doing so will in many cases save NFP CPP internal software
93 #define NFP_CPP_ACTION_RW 32
95 #define NFP_CPP_TARGET_ID_MASK 0x1f
97 #define NFP_CPP_ATOMIC_RD(target, island) \
98 NFP_CPP_ISLAND_ID((target), 3, 0, (island))
99 #define NFP_CPP_ATOMIC_WR(target, island) \
100 NFP_CPP_ISLAND_ID((target), 4, 0, (island))
103 * NFP_CPP_ID() - pack target, token, and action into a CPP ID.
104 * @target: NFP CPP target id
105 * @action: NFP CPP action id
106 * @token: NFP CPP token id
108 * Create a 32-bit CPP identifier representing the access to be made.
109 * These identifiers are used as parameters to other NFP CPP
110 * functions. Some CPP devices may allow wildcard identifiers to be
115 #define NFP_CPP_ID(target, action, token) \
116 ((((target) & 0x7f) << 24) | (((token) & 0xff) << 16) | \
117 (((action) & 0xff) << 8))
120 * NFP_CPP_ISLAND_ID() - pack target, token, action, and island into a CPP ID.
121 * @target: NFP CPP target id
122 * @action: NFP CPP action id
123 * @token: NFP CPP token id
124 * @island: NFP CPP island id
126 * Create a 32-bit CPP identifier representing the access to be made.
127 * These identifiers are used as parameters to other NFP CPP
128 * functions. Some CPP devices may allow wildcard identifiers to be
133 #define NFP_CPP_ISLAND_ID(target, action, token, island) \
134 ((((target) & 0x7f) << 24) | (((token) & 0xff) << 16) | \
135 (((action) & 0xff) << 8) | (((island) & 0xff) << 0))
138 * NFP_CPP_ID_TARGET_of() - Return the NFP CPP target of a NFP CPP ID
141 * Return: NFP CPP target
143 static inline u8 NFP_CPP_ID_TARGET_of(u32 id)
145 return (id >> 24) & NFP_CPP_TARGET_ID_MASK;
149 * NFP_CPP_ID_TOKEN_of() - Return the NFP CPP token of a NFP CPP ID
151 * Return: NFP CPP token
153 static inline u8 NFP_CPP_ID_TOKEN_of(u32 id)
155 return (id >> 16) & 0xff;
159 * NFP_CPP_ID_ACTION_of() - Return the NFP CPP action of a NFP CPP ID
162 * Return: NFP CPP action
164 static inline u8 NFP_CPP_ID_ACTION_of(u32 id)
166 return (id >> 8) & 0xff;
170 * NFP_CPP_ID_ISLAND_of() - Return the NFP CPP island of a NFP CPP ID
173 * Return: NFP CPP island
175 static inline u8 NFP_CPP_ID_ISLAND_of(u32 id)
177 return (id >> 0) & 0xff;
180 /* NFP Interface types - logical interface for this CPP connection
181 * 4 bits are reserved for interface type.
183 #define NFP_CPP_INTERFACE_TYPE_INVALID 0x0
184 #define NFP_CPP_INTERFACE_TYPE_PCI 0x1
185 #define NFP_CPP_INTERFACE_TYPE_ARM 0x2
186 #define NFP_CPP_INTERFACE_TYPE_RPC 0x3
187 #define NFP_CPP_INTERFACE_TYPE_ILA 0x4
190 * NFP_CPP_INTERFACE() - Construct a 16-bit NFP Interface ID
191 * @type: NFP Interface Type
192 * @unit: Unit identifier for the interface type
193 * @channel: Channel identifier for the interface unit
195 * Interface IDs consists of 4 bits of interface type,
196 * 4 bits of unit identifier, and 8 bits of channel identifier.
198 * The NFP Interface ID is used in the implementation of
199 * NFP CPP API mutexes, which use the MU Atomic CompareAndWrite
200 * operation - hence the limit to 16 bits to be able to
201 * use the NFP Interface ID as a lock owner.
203 * Return: Interface ID
205 #define NFP_CPP_INTERFACE(type, unit, channel) \
206 ((((type) & 0xf) << 12) | \
207 (((unit) & 0xf) << 8) | \
208 (((channel) & 0xff) << 0))
211 * NFP_CPP_INTERFACE_TYPE_of() - Get the interface type
212 * @interface: NFP Interface ID
213 * Return: NFP Interface ID's type
215 #define NFP_CPP_INTERFACE_TYPE_of(interface) (((interface) >> 12) & 0xf)
218 * NFP_CPP_INTERFACE_UNIT_of() - Get the interface unit
219 * @interface: NFP Interface ID
220 * Return: NFP Interface ID's unit
222 #define NFP_CPP_INTERFACE_UNIT_of(interface) (((interface) >> 8) & 0xf)
225 * NFP_CPP_INTERFACE_CHANNEL_of() - Get the interface channel
226 * @interface: NFP Interface ID
227 * Return: NFP Interface ID's channel
229 #define NFP_CPP_INTERFACE_CHANNEL_of(interface) (((interface) >> 0) & 0xff)
231 /* Implemented in nfp_cppcore.c */
232 void nfp_cpp_free(struct nfp_cpp *cpp);
233 u32 nfp_cpp_model(struct nfp_cpp *cpp);
234 u16 nfp_cpp_interface(struct nfp_cpp *cpp);
235 int nfp_cpp_serial(struct nfp_cpp *cpp, const u8 **serial);
236 unsigned int nfp_cpp_mu_locality_lsb(struct nfp_cpp *cpp);
238 struct nfp_cpp_area *nfp_cpp_area_alloc_with_name(struct nfp_cpp *cpp,
241 unsigned long long address,
243 struct nfp_cpp_area *nfp_cpp_area_alloc(struct nfp_cpp *cpp, u32 cpp_id,
244 unsigned long long address,
246 struct nfp_cpp_area *
247 nfp_cpp_area_alloc_acquire(struct nfp_cpp *cpp, const char *name, u32 cpp_id,
248 unsigned long long address, unsigned long size);
249 void nfp_cpp_area_free(struct nfp_cpp_area *area);
250 int nfp_cpp_area_acquire(struct nfp_cpp_area *area);
251 int nfp_cpp_area_acquire_nonblocking(struct nfp_cpp_area *area);
252 void nfp_cpp_area_release(struct nfp_cpp_area *area);
253 void nfp_cpp_area_release_free(struct nfp_cpp_area *area);
254 int nfp_cpp_area_read(struct nfp_cpp_area *area, unsigned long offset,
255 void *buffer, size_t length);
256 int nfp_cpp_area_write(struct nfp_cpp_area *area, unsigned long offset,
257 const void *buffer, size_t length);
258 size_t nfp_cpp_area_size(struct nfp_cpp_area *area);
259 const char *nfp_cpp_area_name(struct nfp_cpp_area *cpp_area);
260 void *nfp_cpp_area_priv(struct nfp_cpp_area *cpp_area);
261 struct nfp_cpp *nfp_cpp_area_cpp(struct nfp_cpp_area *cpp_area);
262 struct resource *nfp_cpp_area_resource(struct nfp_cpp_area *area);
263 phys_addr_t nfp_cpp_area_phys(struct nfp_cpp_area *area);
264 void __iomem *nfp_cpp_area_iomem(struct nfp_cpp_area *area);
266 int nfp_cpp_area_readl(struct nfp_cpp_area *area, unsigned long offset,
268 int nfp_cpp_area_writel(struct nfp_cpp_area *area, unsigned long offset,
270 int nfp_cpp_area_readq(struct nfp_cpp_area *area, unsigned long offset,
272 int nfp_cpp_area_writeq(struct nfp_cpp_area *area, unsigned long offset,
274 int nfp_cpp_area_fill(struct nfp_cpp_area *area, unsigned long offset,
275 u32 value, size_t length);
277 int nfp_xpb_readl(struct nfp_cpp *cpp, u32 xpb_tgt, u32 *value);
278 int nfp_xpb_writel(struct nfp_cpp *cpp, u32 xpb_tgt, u32 value);
279 int nfp_xpb_writelm(struct nfp_cpp *cpp, u32 xpb_tgt, u32 mask, u32 value);
281 /* Implemented in nfp_cpplib.c */
282 int nfp_cpp_read(struct nfp_cpp *cpp, u32 cpp_id,
283 unsigned long long address, void *kernel_vaddr, size_t length);
284 int nfp_cpp_write(struct nfp_cpp *cpp, u32 cpp_id,
285 unsigned long long address, const void *kernel_vaddr,
287 int nfp_cpp_readl(struct nfp_cpp *cpp, u32 cpp_id,
288 unsigned long long address, u32 *value);
289 int nfp_cpp_writel(struct nfp_cpp *cpp, u32 cpp_id,
290 unsigned long long address, u32 value);
291 int nfp_cpp_readq(struct nfp_cpp *cpp, u32 cpp_id,
292 unsigned long long address, u64 *value);
293 int nfp_cpp_writeq(struct nfp_cpp *cpp, u32 cpp_id,
294 unsigned long long address, u64 value);
297 nfp_cpp_map_area(struct nfp_cpp *cpp, const char *name, u32 cpp_id, u64 addr,
298 unsigned long size, struct nfp_cpp_area **area);
300 struct nfp_cpp_mutex;
302 int nfp_cpp_mutex_init(struct nfp_cpp *cpp, int target,
303 unsigned long long address, u32 key_id);
304 struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target,
305 unsigned long long address,
307 void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex);
308 int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex);
309 int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex);
310 int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex);
311 int nfp_cpp_mutex_reclaim(struct nfp_cpp *cpp, int target,
312 unsigned long long address);
315 * nfp_cppcore_pcie_unit() - Get PCI Unit of a CPP handle
318 * Return: PCI unit for the NFP CPP handle
320 static inline u8 nfp_cppcore_pcie_unit(struct nfp_cpp *cpp)
322 return NFP_CPP_INTERFACE_UNIT_of(nfp_cpp_interface(cpp));
325 struct nfp_cpp_explicit;
327 struct nfp_cpp_explicit_command {
342 #define NFP_SERIAL_LEN 6
345 * struct nfp_cpp_operations - NFP CPP operations structure
346 * @area_priv_size: Size of the nfp_cpp_area private data
347 * @owner: Owner module
348 * @init: Initialize the NFP CPP bus
349 * @free: Free the bus
350 * @read_serial: Read serial number to memory provided
351 * @get_interface: Return CPP interface
352 * @area_init: Initialize a new NFP CPP area (not serialized)
353 * @area_cleanup: Clean up a NFP CPP area (not serialized)
354 * @area_acquire: Acquire the NFP CPP area (serialized)
355 * @area_release: Release area (serialized)
356 * @area_resource: Get resource range of area (not serialized)
357 * @area_phys: Get physical address of area (not serialized)
358 * @area_iomem: Get iomem of area (not serialized)
359 * @area_read: Perform a read from a NFP CPP area (serialized)
360 * @area_write: Perform a write to a NFP CPP area (serialized)
361 * @explicit_priv_size: Size of an explicit's private area
362 * @explicit_acquire: Acquire an explicit area
363 * @explicit_release: Release an explicit area
364 * @explicit_put: Write data to send
365 * @explicit_get: Read data received
366 * @explicit_do: Perform the transaction
368 struct nfp_cpp_operations {
369 size_t area_priv_size;
370 struct module *owner;
372 int (*init)(struct nfp_cpp *cpp);
373 void (*free)(struct nfp_cpp *cpp);
375 int (*read_serial)(struct device *dev, u8 *serial);
376 int (*get_interface)(struct device *dev);
378 int (*area_init)(struct nfp_cpp_area *area,
379 u32 dest, unsigned long long address,
381 void (*area_cleanup)(struct nfp_cpp_area *area);
382 int (*area_acquire)(struct nfp_cpp_area *area);
383 void (*area_release)(struct nfp_cpp_area *area);
384 struct resource *(*area_resource)(struct nfp_cpp_area *area);
385 phys_addr_t (*area_phys)(struct nfp_cpp_area *area);
386 void __iomem *(*area_iomem)(struct nfp_cpp_area *area);
387 int (*area_read)(struct nfp_cpp_area *area, void *kernel_vaddr,
388 unsigned long offset, unsigned int length);
389 int (*area_write)(struct nfp_cpp_area *area, const void *kernel_vaddr,
390 unsigned long offset, unsigned int length);
392 size_t explicit_priv_size;
393 int (*explicit_acquire)(struct nfp_cpp_explicit *expl);
394 void (*explicit_release)(struct nfp_cpp_explicit *expl);
395 int (*explicit_put)(struct nfp_cpp_explicit *expl,
396 const void *buff, size_t len);
397 int (*explicit_get)(struct nfp_cpp_explicit *expl,
398 void *buff, size_t len);
399 int (*explicit_do)(struct nfp_cpp_explicit *expl,
400 const struct nfp_cpp_explicit_command *cmd,
405 nfp_cpp_from_operations(const struct nfp_cpp_operations *ops,
406 struct device *parent, void *priv);
407 void *nfp_cpp_priv(struct nfp_cpp *priv);
409 int nfp_cpp_area_cache_add(struct nfp_cpp *cpp, size_t size);
411 /* The following section contains extensions to the
412 * NFP CPP API, to be used in a Linux kernel-space context.
415 /* Use this channel ID for multiple virtual channel interfaces
416 * (ie ARM and PCIe) when setting up the interface field.
418 #define NFP_CPP_INTERFACE_CHANNEL_PEROPENER 255
419 struct device *nfp_cpp_device(struct nfp_cpp *cpp);
421 /* Return code masks for nfp_cpp_explicit_do()
423 #define NFP_SIGNAL_MASK_A BIT(0) /* Signal A fired */
424 #define NFP_SIGNAL_MASK_B BIT(1) /* Signal B fired */
426 enum nfp_cpp_explicit_signal_mode {
429 NFP_SIGNAL_PUSH_OPTIONAL = -1,
431 NFP_SIGNAL_PULL_OPTIONAL = -2,
434 struct nfp_cpp_explicit *nfp_cpp_explicit_acquire(struct nfp_cpp *cpp);
435 int nfp_cpp_explicit_set_target(struct nfp_cpp_explicit *expl, u32 cpp_id,
437 int nfp_cpp_explicit_set_data(struct nfp_cpp_explicit *expl,
438 u8 data_master, u16 data_ref);
439 int nfp_cpp_explicit_set_signal(struct nfp_cpp_explicit *expl,
440 u8 signal_master, u8 signal_ref);
441 int nfp_cpp_explicit_set_posted(struct nfp_cpp_explicit *expl, int posted,
443 enum nfp_cpp_explicit_signal_mode siga_mode,
445 enum nfp_cpp_explicit_signal_mode sigb_mode);
446 int nfp_cpp_explicit_put(struct nfp_cpp_explicit *expl,
447 const void *buff, size_t len);
448 int nfp_cpp_explicit_do(struct nfp_cpp_explicit *expl, u64 address);
449 int nfp_cpp_explicit_get(struct nfp_cpp_explicit *expl, void *buff, size_t len);
450 void nfp_cpp_explicit_release(struct nfp_cpp_explicit *expl);
451 struct nfp_cpp *nfp_cpp_explicit_cpp(struct nfp_cpp_explicit *expl);
452 void *nfp_cpp_explicit_priv(struct nfp_cpp_explicit *cpp_explicit);
454 /* Implemented in nfp_cpplib.c */
456 int nfp_cpp_model_autodetect(struct nfp_cpp *cpp, u32 *model);
458 int nfp_cpp_explicit_read(struct nfp_cpp *cpp, u32 cpp_id,
459 u64 addr, void *buff, size_t len,
462 int nfp_cpp_explicit_write(struct nfp_cpp *cpp, u32 cpp_id,
463 u64 addr, const void *buff, size_t len,
466 #endif /* !__NFP_CPP_H__ */