1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Netronome Systems, Inc.
9 #include <rte_ethdev_pci.h>
11 #include "nfp-common/nfp_platform.h"
12 #include "nfp-common/nfp_resid.h"
27 struct nfp_cpp_mutex *mutex_cache;
28 const struct nfp_cpp_operations *op;
31 * NFP-6xxx originating island IMB CPP Address Translation. CPP Target
32 * ID is index into array. Values are obtained at runtime from local
35 uint32_t imb_cat_table[16];
37 int driver_lock_needed;
41 * NFP CPP device area handle
46 unsigned long long offset;
48 /* Here follows the 'priv' part of nfp_cpp_area. */
52 * NFP CPP operations structure
54 struct nfp_cpp_operations {
55 /* Size of priv area in struct nfp_cpp_area */
56 size_t area_priv_size;
58 /* Instance an NFP CPP */
59 int (*init)(struct nfp_cpp *cpp, struct rte_pci_device *dev);
63 * Called only once, during nfp_cpp_unregister()
65 void (*free)(struct nfp_cpp *cpp);
68 * Initialize a new NFP CPP area
69 * NOTE: This is _not_ serialized
71 int (*area_init)(struct nfp_cpp_area *area,
73 unsigned long long address,
76 * Clean up a NFP CPP area before it is freed
77 * NOTE: This is _not_ serialized
79 void (*area_cleanup)(struct nfp_cpp_area *area);
82 * Acquire resources for a NFP CPP area
85 int (*area_acquire)(struct nfp_cpp_area *area);
87 * Release resources for a NFP CPP area
90 void (*area_release)(struct nfp_cpp_area *area);
92 * Return a void IO pointer to a NFP CPP area
93 * NOTE: This is _not_ serialized
96 void *(*area_iomem)(struct nfp_cpp_area *area);
98 void *(*area_mapped)(struct nfp_cpp_area *area);
100 * Perform a read from a NFP CPP area
103 int (*area_read)(struct nfp_cpp_area *area,
105 unsigned long offset,
106 unsigned int length);
108 * Perform a write to a NFP CPP area
111 int (*area_write)(struct nfp_cpp_area *area,
112 const void *kernel_vaddr,
113 unsigned long offset,
114 unsigned int length);
118 * This should be the only external function the transport
121 const struct nfp_cpp_operations *nfp_cpp_transport_operations(void);
126 * @param cpp NFP CPP operations structure
127 * @param model Model ID
129 void nfp_cpp_model_set(struct nfp_cpp *cpp, uint32_t model);
132 * Set the private instance owned data of a nfp_cpp struct
134 * @param cpp NFP CPP operations structure
135 * @param interface Interface ID
137 void nfp_cpp_interface_set(struct nfp_cpp *cpp, uint32_t interface);
140 * Set the private instance owned data of a nfp_cpp struct
142 * @param cpp NFP CPP operations structure
143 * @param serial NFP serial byte array
144 * @param len Length of the serial byte array
146 int nfp_cpp_serial_set(struct nfp_cpp *cpp, const uint8_t *serial,
150 * Set the private data of the nfp_cpp instance
152 * @param cpp NFP CPP operations structure
153 * @return Opaque device pointer
155 void nfp_cpp_priv_set(struct nfp_cpp *cpp, void *priv);
158 * Return the private data of the nfp_cpp instance
160 * @param cpp NFP CPP operations structure
161 * @return Opaque device pointer
163 void *nfp_cpp_priv(struct nfp_cpp *cpp);
166 * Get the privately allocated portion of a NFP CPP area handle
168 * @param cpp_area NFP CPP area handle
169 * @return Pointer to the private area, or NULL on failure
171 void *nfp_cpp_area_priv(struct nfp_cpp_area *cpp_area);
173 uint32_t __nfp_cpp_model_autodetect(struct nfp_cpp *cpp);
176 * NFP CPP core interface for CPP clients.
180 * Open a NFP CPP handle to a CPP device
182 * @param[in] id 0-based ID for the CPP interface to use
184 * @return NFP CPP handle, or NULL on failure (and set errno accordingly).
186 struct nfp_cpp *nfp_cpp_from_device_name(struct rte_pci_device *dev,
187 int driver_lock_needed);
190 * Free a NFP CPP handle
192 * @param[in] cpp NFP CPP handle
194 void nfp_cpp_free(struct nfp_cpp *cpp);
196 #define NFP_CPP_MODEL_INVALID 0xffffffff
199 * NFP_CPP_MODEL_CHIP_of - retrieve the chip ID from the model ID
201 * The chip ID is a 16-bit BCD+A-F encoding for the chip type.
203 * @param[in] model NFP CPP model id
204 * @return NFP CPP chip id
206 #define NFP_CPP_MODEL_CHIP_of(model) (((model) >> 16) & 0xffff)
209 * NFP_CPP_MODEL_IS_6000 - Check for the NFP6000 family of devices
211 * NOTE: The NFP4000 series is considered as a NFP6000 series variant.
213 * @param[in] model NFP CPP model id
214 * @return true if model is in the NFP6000 family, false otherwise.
216 #define NFP_CPP_MODEL_IS_6000(model) \
217 ((NFP_CPP_MODEL_CHIP_of(model) >= 0x4000) && \
218 (NFP_CPP_MODEL_CHIP_of(model) < 0x7000))
221 * nfp_cpp_model - Retrieve the Model ID of the NFP
223 * @param[in] cpp NFP CPP handle
224 * @return NFP CPP Model ID
226 uint32_t nfp_cpp_model(struct nfp_cpp *cpp);
229 * NFP Interface types - logical interface for this CPP connection 4 bits are
230 * reserved for interface type.
232 #define NFP_CPP_INTERFACE_TYPE_INVALID 0x0
233 #define NFP_CPP_INTERFACE_TYPE_PCI 0x1
234 #define NFP_CPP_INTERFACE_TYPE_ARM 0x2
235 #define NFP_CPP_INTERFACE_TYPE_RPC 0x3
236 #define NFP_CPP_INTERFACE_TYPE_ILA 0x4
239 * Construct a 16-bit NFP Interface ID
241 * Interface IDs consists of 4 bits of interface type, 4 bits of unit
242 * identifier, and 8 bits of channel identifier.
244 * The NFP Interface ID is used in the implementation of NFP CPP API mutexes,
245 * which use the MU Atomic CompareAndWrite operation - hence the limit to 16
246 * bits to be able to use the NFP Interface ID as a lock owner.
248 * @param[in] type NFP Interface Type
249 * @param[in] unit Unit identifier for the interface type
250 * @param[in] channel Channel identifier for the interface unit
251 * @return Interface ID
253 #define NFP_CPP_INTERFACE(type, unit, channel) \
254 ((((type) & 0xf) << 12) | \
255 (((unit) & 0xf) << 8) | \
256 (((channel) & 0xff) << 0))
259 * Get the interface type of a NFP Interface ID
260 * @param[in] interface NFP Interface ID
261 * @return NFP Interface ID's type
263 #define NFP_CPP_INTERFACE_TYPE_of(interface) (((interface) >> 12) & 0xf)
266 * Get the interface unit of a NFP Interface ID
267 * @param[in] interface NFP Interface ID
268 * @return NFP Interface ID's unit
270 #define NFP_CPP_INTERFACE_UNIT_of(interface) (((interface) >> 8) & 0xf)
273 * Get the interface channel of a NFP Interface ID
274 * @param[in] interface NFP Interface ID
275 * @return NFP Interface ID's channel
277 #define NFP_CPP_INTERFACE_CHANNEL_of(interface) (((interface) >> 0) & 0xff)
280 * Retrieve the Interface ID of the NFP
281 * @param[in] cpp NFP CPP handle
282 * @return NFP CPP Interface ID
284 uint16_t nfp_cpp_interface(struct nfp_cpp *cpp);
287 * Retrieve the NFP Serial Number (unique per NFP)
288 * @param[in] cpp NFP CPP handle
289 * @param[out] serial Pointer to reference the serial number array
291 * @return size of the NFP6000 serial number, in bytes
293 int nfp_cpp_serial(struct nfp_cpp *cpp, const uint8_t **serial);
296 * Allocate a NFP CPP area handle, as an offset into a CPP ID
297 * @param[in] cpp NFP CPP handle
298 * @param[in] cpp_id NFP CPP ID
299 * @param[in] address Offset into the NFP CPP ID address space
300 * @param[in] size Size of the area to reserve
302 * @return NFP CPP handle, or NULL on failure (and set errno accordingly).
304 struct nfp_cpp_area *nfp_cpp_area_alloc(struct nfp_cpp *cpp, uint32_t cpp_id,
305 unsigned long long address,
309 * Allocate a NFP CPP area handle, as an offset into a CPP ID, by a named owner
310 * @param[in] cpp NFP CPP handle
311 * @param[in] cpp_id NFP CPP ID
312 * @param[in] name Name of owner of the area
313 * @param[in] address Offset into the NFP CPP ID address space
314 * @param[in] size Size of the area to reserve
316 * @return NFP CPP handle, or NULL on failure (and set errno accordingly).
318 struct nfp_cpp_area *nfp_cpp_area_alloc_with_name(struct nfp_cpp *cpp,
321 unsigned long long address,
325 * Free an allocated NFP CPP area handle
326 * @param[in] area NFP CPP area handle
328 void nfp_cpp_area_free(struct nfp_cpp_area *area);
331 * Acquire the resources needed to access the NFP CPP area handle
333 * @param[in] area NFP CPP area handle
335 * @return 0 on success, -1 on failure (and set errno accordingly).
337 int nfp_cpp_area_acquire(struct nfp_cpp_area *area);
340 * Release the resources needed to access the NFP CPP area handle
342 * @param[in] area NFP CPP area handle
344 void nfp_cpp_area_release(struct nfp_cpp_area *area);
347 * Allocate, then acquire the resources needed to access the NFP CPP area handle
348 * @param[in] cpp NFP CPP handle
349 * @param[in] cpp_id NFP CPP ID
350 * @param[in] address Offset into the NFP CPP ID address space
351 * @param[in] size Size of the area to reserve
353 * @return NFP CPP handle, or NULL on failure (and set errno accordingly).
355 struct nfp_cpp_area *nfp_cpp_area_alloc_acquire(struct nfp_cpp *cpp,
357 unsigned long long address,
361 * Release the resources, then free the NFP CPP area handle
362 * @param[in] area NFP CPP area handle
364 void nfp_cpp_area_release_free(struct nfp_cpp_area *area);
366 uint8_t *nfp_cpp_map_area(struct nfp_cpp *cpp, int domain, int target,
367 uint64_t addr, unsigned long size,
368 struct nfp_cpp_area **area);
370 * Return an IO pointer to the beginning of the NFP CPP area handle. The area
371 * must be acquired with 'nfp_cpp_area_acquire()' before calling this operation.
373 * @param[in] area NFP CPP area handle
375 * @return Pointer to IO memory, or NULL on failure (and set errno accordingly).
377 void *nfp_cpp_area_mapped(struct nfp_cpp_area *area);
380 * Read from a NFP CPP area handle into a buffer. The area must be acquired with
381 * 'nfp_cpp_area_acquire()' before calling this operation.
383 * @param[in] area NFP CPP area handle
384 * @param[in] offset Offset into the area
385 * @param[in] buffer Location of buffer to receive the data
386 * @param[in] length Length of the data to read
388 * @return bytes read on success, -1 on failure (and set errno accordingly).
391 int nfp_cpp_area_read(struct nfp_cpp_area *area, unsigned long offset,
392 void *buffer, size_t length);
395 * Write to a NFP CPP area handle from a buffer. The area must be acquired with
396 * 'nfp_cpp_area_acquire()' before calling this operation.
398 * @param[in] area NFP CPP area handle
399 * @param[in] offset Offset into the area
400 * @param[in] buffer Location of buffer that holds the data
401 * @param[in] length Length of the data to read
403 * @return bytes written on success, -1 on failure (and set errno accordingly).
405 int nfp_cpp_area_write(struct nfp_cpp_area *area, unsigned long offset,
406 const void *buffer, size_t length);
409 * nfp_cpp_area_iomem() - get IOMEM region for CPP area
410 * @area: CPP area handle
412 * Returns an iomem pointer for use with readl()/writel() style operations.
414 * NOTE: Area must have been locked down with an 'acquire'.
416 * Return: pointer to the area, or NULL
418 void *nfp_cpp_area_iomem(struct nfp_cpp_area *area);
421 * Verify that IO can be performed on an offset in an area
423 * @param[in] area NFP CPP area handle
424 * @param[in] offset Offset into the area
425 * @param[in] size Size of region to validate
427 * @return 0 on success, -1 on failure (and set errno accordingly).
429 int nfp_cpp_area_check_range(struct nfp_cpp_area *area,
430 unsigned long long offset, unsigned long size);
433 * Get the NFP CPP handle that is the parent of a NFP CPP area handle
435 * @param cpp_area NFP CPP area handle
436 * @return NFP CPP handle
438 struct nfp_cpp *nfp_cpp_area_cpp(struct nfp_cpp_area *cpp_area);
441 * Get the name passed during allocation of the NFP CPP area handle
443 * @param cpp_area NFP CPP area handle
444 * @return Pointer to the area's name
446 const char *nfp_cpp_area_name(struct nfp_cpp_area *cpp_area);
449 * Read a block of data from a NFP CPP ID
451 * @param[in] cpp NFP CPP handle
452 * @param[in] cpp_id NFP CPP ID
453 * @param[in] address Offset into the NFP CPP ID address space
454 * @param[in] kernel_vaddr Buffer to copy read data to
455 * @param[in] length Size of the area to reserve
457 * @return bytes read on success, -1 on failure (and set errno accordingly).
459 int nfp_cpp_read(struct nfp_cpp *cpp, uint32_t cpp_id,
460 unsigned long long address, void *kernel_vaddr, size_t length);
463 * Write a block of data to a NFP CPP ID
465 * @param[in] cpp NFP CPP handle
466 * @param[in] cpp_id NFP CPP ID
467 * @param[in] address Offset into the NFP CPP ID address space
468 * @param[in] kernel_vaddr Buffer to copy write data from
469 * @param[in] length Size of the area to reserve
471 * @return bytes written on success, -1 on failure (and set errno accordingly).
473 int nfp_cpp_write(struct nfp_cpp *cpp, uint32_t cpp_id,
474 unsigned long long address, const void *kernel_vaddr,
480 * Fill a NFP CPP area handle and offset with a value
482 * @param[in] area NFP CPP area handle
483 * @param[in] offset Offset into the NFP CPP ID address space
484 * @param[in] value 32-bit value to fill area with
485 * @param[in] length Size of the area to reserve
487 * @return bytes written on success, -1 on failure (and set errno accordingly).
489 int nfp_cpp_area_fill(struct nfp_cpp_area *area, unsigned long offset,
490 uint32_t value, size_t length);
493 * Read a single 32-bit value from a NFP CPP area handle
495 * @param area NFP CPP area handle
496 * @param offset offset into NFP CPP area handle
497 * @param value output value
499 * The area must be acquired with 'nfp_cpp_area_acquire()' before calling this
502 * NOTE: offset must be 32-bit aligned.
504 * @return 0 on success, or -1 on error (and set errno accordingly).
506 int nfp_cpp_area_readl(struct nfp_cpp_area *area, unsigned long offset,
510 * Write a single 32-bit value to a NFP CPP area handle
512 * @param area NFP CPP area handle
513 * @param offset offset into NFP CPP area handle
514 * @param value value to write
516 * The area must be acquired with 'nfp_cpp_area_acquire()' before calling this
519 * NOTE: offset must be 32-bit aligned.
521 * @return 0 on success, or -1 on error (and set errno accordingly).
523 int nfp_cpp_area_writel(struct nfp_cpp_area *area, unsigned long offset,
527 * Read a single 64-bit value from a NFP CPP area handle
529 * @param area NFP CPP area handle
530 * @param offset offset into NFP CPP area handle
531 * @param value output value
533 * The area must be acquired with 'nfp_cpp_area_acquire()' before calling this
536 * NOTE: offset must be 64-bit aligned.
538 * @return 0 on success, or -1 on error (and set errno accordingly).
540 int nfp_cpp_area_readq(struct nfp_cpp_area *area, unsigned long offset,
544 * Write a single 64-bit value to a NFP CPP area handle
546 * @param area NFP CPP area handle
547 * @param offset offset into NFP CPP area handle
548 * @param value value to write
550 * The area must be acquired with 'nfp_cpp_area_acquire()' before calling this
553 * NOTE: offset must be 64-bit aligned.
555 * @return 0 on success, or -1 on error (and set errno accordingly).
557 int nfp_cpp_area_writeq(struct nfp_cpp_area *area, unsigned long offset,
561 * Write a single 32-bit value on the XPB bus
563 * @param cpp NFP CPP device handle
564 * @param xpb_tgt XPB target and address
565 * @param value value to write
567 * @return 0 on success, or -1 on failure (and set errno accordingly).
569 int nfp_xpb_writel(struct nfp_cpp *cpp, uint32_t xpb_tgt, uint32_t value);
572 * Read a single 32-bit value from the XPB bus
574 * @param cpp NFP CPP device handle
575 * @param xpb_tgt XPB target and address
576 * @param value output value
578 * @return 0 on success, or -1 on failure (and set errno accordingly).
580 int nfp_xpb_readl(struct nfp_cpp *cpp, uint32_t xpb_tgt, uint32_t *value);
583 * Modify bits of a 32-bit value from the XPB bus
585 * @param cpp NFP CPP device handle
586 * @param xpb_tgt XPB target and address
587 * @param mask mask of bits to alter
588 * @param value value to modify
590 * @return 0 on success, or -1 on failure (and set errno accordingly).
592 int nfp_xpb_writelm(struct nfp_cpp *cpp, uint32_t xpb_tgt, uint32_t mask,
596 * Modify bits of a 32-bit value from the XPB bus
598 * @param cpp NFP CPP device handle
599 * @param xpb_tgt XPB target and address
600 * @param mask mask of bits to alter
601 * @param value value to monitor for
602 * @param timeout_us maximum number of us to wait (-1 for forever)
604 * @return >= 0 on success, or -1 on failure (and set errno accordingly).
606 int nfp_xpb_waitlm(struct nfp_cpp *cpp, uint32_t xpb_tgt, uint32_t mask,
607 uint32_t value, int timeout_us);
610 * Read a 32-bit word from a NFP CPP ID
612 * @param cpp NFP CPP handle
613 * @param cpp_id NFP CPP ID
614 * @param address offset into the NFP CPP ID address space
615 * @param value output value
617 * @return 0 on success, or -1 on failure (and set errno accordingly).
619 int nfp_cpp_readl(struct nfp_cpp *cpp, uint32_t cpp_id,
620 unsigned long long address, uint32_t *value);
623 * Write a 32-bit value to a NFP CPP ID
625 * @param cpp NFP CPP handle
626 * @param cpp_id NFP CPP ID
627 * @param address offset into the NFP CPP ID address space
628 * @param value value to write
630 * @return 0 on success, or -1 on failure (and set errno accordingly).
633 int nfp_cpp_writel(struct nfp_cpp *cpp, uint32_t cpp_id,
634 unsigned long long address, uint32_t value);
637 * Read a 64-bit work from a NFP CPP ID
639 * @param cpp NFP CPP handle
640 * @param cpp_id NFP CPP ID
641 * @param address offset into the NFP CPP ID address space
642 * @param value output value
644 * @return 0 on success, or -1 on failure (and set errno accordingly).
646 int nfp_cpp_readq(struct nfp_cpp *cpp, uint32_t cpp_id,
647 unsigned long long address, uint64_t *value);
650 * Write a 64-bit value to a NFP CPP ID
652 * @param cpp NFP CPP handle
653 * @param cpp_id NFP CPP ID
654 * @param address offset into the NFP CPP ID address space
655 * @param value value to write
657 * @return 0 on success, or -1 on failure (and set errno accordingly).
659 int nfp_cpp_writeq(struct nfp_cpp *cpp, uint32_t cpp_id,
660 unsigned long long address, uint64_t value);
663 * Initialize a mutex location
665 * The CPP target:address must point to a 64-bit aligned location, and will
666 * initialize 64 bits of data at the location.
668 * This creates the initial mutex state, as locked by this nfp_cpp_interface().
670 * This function should only be called when setting up the initial lock state
671 * upon boot-up of the system.
673 * @param cpp NFP CPP handle
674 * @param target NFP CPP target ID
675 * @param address Offset into the address space of the NFP CPP target ID
676 * @param key_id Unique 32-bit value for this mutex
678 * @return 0 on success, or -1 on failure (and set errno accordingly).
680 int nfp_cpp_mutex_init(struct nfp_cpp *cpp, int target,
681 unsigned long long address, uint32_t key_id);
684 * Create a mutex handle from an address controlled by a MU Atomic engine
686 * The CPP target:address must point to a 64-bit aligned location, and reserve
687 * 64 bits of data at the location for use by the handle.
689 * Only target/address pairs that point to entities that support the MU Atomic
690 * Engine's CmpAndSwap32 command are supported.
692 * @param cpp NFP CPP handle
693 * @param target NFP CPP target ID
694 * @param address Offset into the address space of the NFP CPP target ID
695 * @param key_id 32-bit unique key (must match the key at this location)
697 * @return A non-NULL struct nfp_cpp_mutex * on success, NULL on
700 struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target,
701 unsigned long long address,
705 * Get the NFP CPP handle the mutex was created with
707 * @param mutex NFP mutex handle
708 * @return NFP CPP handle
710 struct nfp_cpp *nfp_cpp_mutex_cpp(struct nfp_cpp_mutex *mutex);
715 * @param mutex NFP mutex handle
718 uint32_t nfp_cpp_mutex_key(struct nfp_cpp_mutex *mutex);
721 * Get the mutex owner
723 * @param mutex NFP mutex handle
724 * @return Interface ID of the mutex owner
726 * NOTE: This is for debug purposes ONLY - the owner may change at any time,
727 * unless it has been locked by this NFP CPP handle.
729 uint16_t nfp_cpp_mutex_owner(struct nfp_cpp_mutex *mutex);
732 * Get the mutex target
734 * @param mutex NFP mutex handle
735 * @return Mutex CPP target (ie NFP_CPP_TARGET_MU)
737 int nfp_cpp_mutex_target(struct nfp_cpp_mutex *mutex);
740 * Get the mutex address
742 * @param mutex NFP mutex handle
743 * @return Mutex CPP address
745 uint64_t nfp_cpp_mutex_address(struct nfp_cpp_mutex *mutex);
748 * Free a mutex handle - does not alter the lock state
750 * @param mutex NFP CPP Mutex handle
752 void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex);
755 * Lock a mutex handle, using the NFP MU Atomic Engine
757 * @param mutex NFP CPP Mutex handle
759 * @return 0 on success, or -1 on failure (and set errno accordingly).
761 int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex);
764 * Unlock a mutex handle, using the NFP MU Atomic Engine
766 * @param mutex NFP CPP Mutex handle
768 * @return 0 on success, or -1 on failure (and set errno accordingly).
770 int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex);
773 * Attempt to lock a mutex handle, using the NFP MU Atomic Engine
775 * @param mutex NFP CPP Mutex handle
776 * @return 0 if the lock succeeded, -1 on failure (and errno set
779 int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex);
781 #endif /* !__NFP_CPP_H__ */