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