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 
17 #ifndef NFP_SUBSYS
18 #define NFP_SUBSYS "nfp"
19 #endif
20 
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)
32 
33 #define PCI_64BIT_BAR_COUNT             3
34 
35 /* NFP hardware vendor/device ids.
36  */
37 #define PCI_DEVICE_ID_NETRONOME_NFP3800	0x3800
38 
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
42 
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
47 
48 struct device;
49 
50 struct nfp_cpp_area;
51 struct nfp_cpp;
52 struct resource;
53 
54 /* Wildcard indicating a CPP read or write action
55  *
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
61  * resources.
62  */
63 #define NFP_CPP_ACTION_RW               32
64 
65 #define NFP_CPP_TARGET_ID_MASK          0x1f
66 
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))
71 
72 /**
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
77  *
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
81  * specified.
82  *
83  * Return:      NFP CPP ID
84  */
85 #define NFP_CPP_ID(target, action, token)			 \
86 	((((target) & 0x7f) << 24) | (((token)  & 0xff) << 16) | \
87 	 (((action) & 0xff) <<  8))
88 
89 /**
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
95  *
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
99  * specified.
100  *
101  * Return:      NFP CPP ID
102  */
103 #define NFP_CPP_ISLAND_ID(target, action, token, island)	 \
104 	((((target) & 0x7f) << 24) | (((token)  & 0xff) << 16) | \
105 	 (((action) & 0xff) <<  8) | (((island) & 0xff) << 0))
106 
107 /**
108  * NFP_CPP_ID_TARGET_of() - Return the NFP CPP target of a NFP CPP ID
109  * @id:         NFP CPP ID
110  *
111  * Return:      NFP CPP target
112  */
NFP_CPP_ID_TARGET_of(u32 id)113 static inline u8 NFP_CPP_ID_TARGET_of(u32 id)
114 {
115 	return (id >> 24) & NFP_CPP_TARGET_ID_MASK;
116 }
117 
118 /**
119  * NFP_CPP_ID_TOKEN_of() - Return the NFP CPP token of a NFP CPP ID
120  * @id:         NFP CPP ID
121  * Return:      NFP CPP token
122  */
NFP_CPP_ID_TOKEN_of(u32 id)123 static inline u8 NFP_CPP_ID_TOKEN_of(u32 id)
124 {
125 	return (id >> 16) & 0xff;
126 }
127 
128 /**
129  * NFP_CPP_ID_ACTION_of() - Return the NFP CPP action of a NFP CPP ID
130  * @id:         NFP CPP ID
131  *
132  * Return:      NFP CPP action
133  */
NFP_CPP_ID_ACTION_of(u32 id)134 static inline u8 NFP_CPP_ID_ACTION_of(u32 id)
135 {
136 	return (id >> 8) & 0xff;
137 }
138 
139 /**
140  * NFP_CPP_ID_ISLAND_of() - Return the NFP CPP island of a NFP CPP ID
141  * @id: NFP CPP ID
142  *
143  * Return:      NFP CPP island
144  */
NFP_CPP_ID_ISLAND_of(u32 id)145 static inline u8 NFP_CPP_ID_ISLAND_of(u32 id)
146 {
147 	return (id >> 0) & 0xff;
148 }
149 
150 /* NFP Interface types - logical interface for this CPP connection
151  * 4 bits are reserved for interface type.
152  */
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
158 
159 /**
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
164  *
165  * Interface IDs consists of 4 bits of interface type,
166  * 4 bits of unit identifier, and 8 bits of channel identifier.
167  *
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.
172  *
173  * Return:      Interface ID
174  */
175 #define NFP_CPP_INTERFACE(type, unit, channel)	\
176 	((((type) & 0xf) << 12) |		\
177 	 (((unit) & 0xf) <<  8) |		\
178 	 (((channel) & 0xff) << 0))
179 
180 /**
181  * NFP_CPP_INTERFACE_TYPE_of() - Get the interface type
182  * @interface:  NFP Interface ID
183  * Return:      NFP Interface ID's type
184  */
185 #define NFP_CPP_INTERFACE_TYPE_of(interface)   (((interface) >> 12) & 0xf)
186 
187 /**
188  * NFP_CPP_INTERFACE_UNIT_of() - Get the interface unit
189  * @interface:  NFP Interface ID
190  * Return:      NFP Interface ID's unit
191  */
192 #define NFP_CPP_INTERFACE_UNIT_of(interface)   (((interface) >>  8) & 0xf)
193 
194 /**
195  * NFP_CPP_INTERFACE_CHANNEL_of() - Get the interface channel
196  * @interface:  NFP Interface ID
197  * Return:      NFP Interface ID's channel
198  */
199 #define NFP_CPP_INTERFACE_CHANNEL_of(interface)   (((interface) >>  0) & 0xff)
200 
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);
207 
208 struct nfp_cpp_area *nfp_cpp_area_alloc_with_name(struct nfp_cpp *cpp,
209 						  u32 cpp_id,
210 						  const char *name,
211 						  unsigned long long address,
212 						  unsigned long size);
213 struct nfp_cpp_area *nfp_cpp_area_alloc(struct nfp_cpp *cpp, u32 cpp_id,
214 					unsigned long long address,
215 					unsigned long size);
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);
235 
236 int nfp_cpp_area_readl(struct nfp_cpp_area *area, unsigned long offset,
237 		       u32 *value);
238 int nfp_cpp_area_writel(struct nfp_cpp_area *area, unsigned long offset,
239 			u32 value);
240 int nfp_cpp_area_readq(struct nfp_cpp_area *area, unsigned long offset,
241 		       u64 *value);
242 int nfp_cpp_area_writeq(struct nfp_cpp_area *area, unsigned long offset,
243 			u64 value);
244 int nfp_cpp_area_fill(struct nfp_cpp_area *area, unsigned long offset,
245 		      u32 value, size_t length);
246 
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);
250 
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,
256 		  size_t length);
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);
265 
266 u8 __iomem *
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);
269 
270 struct nfp_cpp_mutex;
271 
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,
276 					  u32 key_id);
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);
283 
284 /**
285  * nfp_cppcore_pcie_unit() - Get PCI Unit of a CPP handle
286  * @cpp:	CPP handle
287  *
288  * Return: PCI unit for the NFP CPP handle
289  */
nfp_cppcore_pcie_unit(struct nfp_cpp * cpp)290 static inline u8 nfp_cppcore_pcie_unit(struct nfp_cpp *cpp)
291 {
292 	return NFP_CPP_INTERFACE_UNIT_of(nfp_cpp_interface(cpp));
293 }
294 
295 struct nfp_cpp_explicit;
296 
297 struct nfp_cpp_explicit_command {
298 	u32 cpp_id;
299 	u16 data_ref;
300 	u8  data_master;
301 	u8  len;
302 	u8  byte_mask;
303 	u8  signal_master;
304 	u8  signal_ref;
305 	u8  posted;
306 	u8  siga;
307 	u8  sigb;
308 	s8   siga_mode;
309 	s8   sigb_mode;
310 };
311 
312 #define NFP_SERIAL_LEN		6
313 
314 /**
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
337  */
338 struct nfp_cpp_operations {
339 	size_t area_priv_size;
340 	struct module *owner;
341 
342 	int (*init)(struct nfp_cpp *cpp);
343 	void (*free)(struct nfp_cpp *cpp);
344 
345 	int (*read_serial)(struct device *dev, u8 *serial);
346 	int (*get_interface)(struct device *dev);
347 
348 	int (*area_init)(struct nfp_cpp_area *area,
349 			 u32 dest, unsigned long long address,
350 			 unsigned long size);
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);
361 
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,
371 			   u64 address);
372 };
373 
374 struct nfp_cpp *
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);
378 
379 int nfp_cpp_area_cache_add(struct nfp_cpp *cpp, size_t size);
380 
381 /* The following section contains extensions to the
382  * NFP CPP API, to be used in a Linux kernel-space context.
383  */
384 
385 /* Use this channel ID for multiple virtual channel interfaces
386  * (ie ARM and PCIe) when setting up the interface field.
387  */
388 #define NFP_CPP_INTERFACE_CHANNEL_PEROPENER	255
389 struct device *nfp_cpp_device(struct nfp_cpp *cpp);
390 
391 /* Return code masks for nfp_cpp_explicit_do()
392  */
393 #define NFP_SIGNAL_MASK_A	BIT(0)	/* Signal A fired */
394 #define NFP_SIGNAL_MASK_B	BIT(1)	/* Signal B fired */
395 
396 enum nfp_cpp_explicit_signal_mode {
397 	NFP_SIGNAL_NONE = 0,
398 	NFP_SIGNAL_PUSH = 1,
399 	NFP_SIGNAL_PUSH_OPTIONAL = -1,
400 	NFP_SIGNAL_PULL = 2,
401 	NFP_SIGNAL_PULL_OPTIONAL = -2,
402 };
403 
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,
406 				u8 len, u8 mask);
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,
412 				u8 siga,
413 				enum nfp_cpp_explicit_signal_mode siga_mode,
414 				u8 sigb,
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);
423 
424 /* Implemented in nfp_cpplib.c */
425 
426 int nfp_cpp_model_autodetect(struct nfp_cpp *cpp, u32 *model);
427 
428 int nfp_cpp_explicit_read(struct nfp_cpp *cpp, u32 cpp_id,
429 			  u64 addr, void *buff, size_t len,
430 			  int width_read);
431 
432 int nfp_cpp_explicit_write(struct nfp_cpp *cpp, u32 cpp_id,
433 			   u64 addr, const void *buff, size_t len,
434 			   int width_write);
435 
436 #endif /* !__NFP_CPP_H__ */
437