1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Netronome Systems, Inc.
9 #include "nfp-common/nfp_platform.h"
10 #include "nfp-common/nfp_resid.h"
25 struct nfp_cpp_mutex *mutex_cache;
26 const struct nfp_cpp_operations *op;
29 * NFP-6xxx originating island IMB CPP Address Translation. CPP Target
30 * ID is index into array. Values are obtained at runtime from local
33 uint32_t imb_cat_table[16];
35 int driver_lock_needed;
39 * NFP CPP device area handle
44 unsigned long long offset;
46 /* Here follows the 'priv' part of nfp_cpp_area. */
50 * NFP CPP operations structure
52 struct nfp_cpp_operations {
53 /* Size of priv area in struct nfp_cpp_area */
54 size_t area_priv_size;
56 /* Instance an NFP CPP */
57 int (*init)(struct nfp_cpp *cpp, const char *devname);
61 * Called only once, during nfp_cpp_unregister()
63 void (*free)(struct nfp_cpp *cpp);
66 * Initialize a new NFP CPP area
67 * NOTE: This is _not_ serialized
69 int (*area_init)(struct nfp_cpp_area *area,
71 unsigned long long address,
74 * Clean up a NFP CPP area before it is freed
75 * NOTE: This is _not_ serialized
77 void (*area_cleanup)(struct nfp_cpp_area *area);
80 * Acquire resources for a NFP CPP area
83 int (*area_acquire)(struct nfp_cpp_area *area);
85 * Release resources for a NFP CPP area
88 void (*area_release)(struct nfp_cpp_area *area);
90 * Return a void IO pointer to a NFP CPP area
91 * NOTE: This is _not_ serialized
94 void *(*area_iomem)(struct nfp_cpp_area *area);
96 void *(*area_mapped)(struct nfp_cpp_area *area);
98 * Perform a read from a NFP CPP area
101 int (*area_read)(struct nfp_cpp_area *area,
103 unsigned long offset,
104 unsigned int length);
106 * Perform a write to a NFP CPP area
109 int (*area_write)(struct nfp_cpp_area *area,
110 const void *kernel_vaddr,
111 unsigned long offset,
112 unsigned int length);
116 * This should be the only external function the transport
119 const struct nfp_cpp_operations *nfp_cpp_transport_operations(void);
124 * @param cpp NFP CPP operations structure
125 * @param model Model ID
127 void nfp_cpp_model_set(struct nfp_cpp *cpp, uint32_t model);
130 * Set the private instance owned data of a nfp_cpp struct
132 * @param cpp NFP CPP operations structure
133 * @param interface Interface ID
135 void nfp_cpp_interface_set(struct nfp_cpp *cpp, uint32_t interface);
138 * Set the private instance owned data of a nfp_cpp struct
140 * @param cpp NFP CPP operations structure
141 * @param serial NFP serial byte array
142 * @param len Length of the serial byte array
144 int nfp_cpp_serial_set(struct nfp_cpp *cpp, const uint8_t *serial,
148 * Set the private data of the nfp_cpp instance
150 * @param cpp NFP CPP operations structure
151 * @return Opaque device pointer
153 void nfp_cpp_priv_set(struct nfp_cpp *cpp, void *priv);
156 * Return the private data of the nfp_cpp instance
158 * @param cpp NFP CPP operations structure
159 * @return Opaque device pointer
161 void *nfp_cpp_priv(struct nfp_cpp *cpp);
164 * Get the privately allocated portion of a NFP CPP area handle
166 * @param cpp_area NFP CPP area handle
167 * @return Pointer to the private area, or NULL on failure
169 void *nfp_cpp_area_priv(struct nfp_cpp_area *cpp_area);
171 uint32_t __nfp_cpp_model_autodetect(struct nfp_cpp *cpp);
174 * NFP CPP core interface for CPP clients.
178 * Open a NFP CPP handle to a CPP device
180 * @param[in] id 0-based ID for the CPP interface to use
182 * @return NFP CPP handle, or NULL on failure (and set errno accordingly).
184 struct nfp_cpp *nfp_cpp_from_device_name(const char *devname,
185 int driver_lock_needed);
188 * Free a NFP CPP handle
190 * @param[in] cpp NFP CPP handle
192 void nfp_cpp_free(struct nfp_cpp *cpp);
194 #define NFP_CPP_MODEL_INVALID 0xffffffff
197 * NFP_CPP_MODEL_CHIP_of - retrieve the chip ID from the model ID
199 * The chip ID is a 16-bit BCD+A-F encoding for the chip type.
201 * @param[in] model NFP CPP model id
202 * @return NFP CPP chip id
204 #define NFP_CPP_MODEL_CHIP_of(model) (((model) >> 16) & 0xffff)
207 * NFP_CPP_MODEL_IS_6000 - Check for the NFP6000 family of devices
209 * NOTE: The NFP4000 series is considered as a NFP6000 series variant.
211 * @param[in] model NFP CPP model id
212 * @return true if model is in the NFP6000 family, false otherwise.
214 #define NFP_CPP_MODEL_IS_6000(model) \
215 ((NFP_CPP_MODEL_CHIP_of(model) >= 0x4000) && \
216 (NFP_CPP_MODEL_CHIP_of(model) < 0x7000))
219 * nfp_cpp_model - Retrieve the Model ID of the NFP
221 * @param[in] cpp NFP CPP handle
222 * @return NFP CPP Model ID
224 uint32_t nfp_cpp_model(struct nfp_cpp *cpp);
227 * NFP Interface types - logical interface for this CPP connection 4 bits are
228 * reserved for interface type.
230 #define NFP_CPP_INTERFACE_TYPE_INVALID 0x0
231 #define NFP_CPP_INTERFACE_TYPE_PCI 0x1
232 #define NFP_CPP_INTERFACE_TYPE_ARM 0x2
233 #define NFP_CPP_INTERFACE_TYPE_RPC 0x3
234 #define NFP_CPP_INTERFACE_TYPE_ILA 0x4
237 * Construct a 16-bit NFP Interface ID
239 * Interface IDs consists of 4 bits of interface type, 4 bits of unit
240 * identifier, and 8 bits of channel identifier.
242 * The NFP Interface ID is used in the implementation of NFP CPP API mutexes,
243 * which use the MU Atomic CompareAndWrite operation - hence the limit to 16
244 * bits to be able to use the NFP Interface ID as a lock owner.
246 * @param[in] type NFP Interface Type
247 * @param[in] unit Unit identifier for the interface type
248 * @param[in] channel Channel identifier for the interface unit
249 * @return Interface ID
251 #define NFP_CPP_INTERFACE(type, unit, channel) \
252 ((((type) & 0xf) << 12) | \
253 (((unit) & 0xf) << 8) | \
254 (((channel) & 0xff) << 0))
257 * Get the interface type of a NFP Interface ID
258 * @param[in] interface NFP Interface ID
259 * @return NFP Interface ID's type
261 #define NFP_CPP_INTERFACE_TYPE_of(interface) (((interface) >> 12) & 0xf)
264 * Get the interface unit of a NFP Interface ID
265 * @param[in] interface NFP Interface ID
266 * @return NFP Interface ID's unit
268 #define NFP_CPP_INTERFACE_UNIT_of(interface) (((interface) >> 8) & 0xf)
271 * Get the interface channel of a NFP Interface ID
272 * @param[in] interface NFP Interface ID
273 * @return NFP Interface ID's channel
275 #define NFP_CPP_INTERFACE_CHANNEL_of(interface) (((interface) >> 0) & 0xff)
278 * Retrieve the Interface ID of the NFP
279 * @param[in] cpp NFP CPP handle
280 * @return NFP CPP Interface ID
282 uint16_t nfp_cpp_interface(struct nfp_cpp *cpp);
285 * Retrieve the NFP Serial Number (unique per NFP)
286 * @param[in] cpp NFP CPP handle
287 * @param[out] serial Pointer to reference the serial number array
289 * @return size of the NFP6000 serial number, in bytes
291 int nfp_cpp_serial(struct nfp_cpp *cpp, const uint8_t **serial);
294 * Allocate a NFP CPP area handle, as an offset into a CPP ID
295 * @param[in] cpp NFP CPP handle
296 * @param[in] cpp_id NFP CPP ID
297 * @param[in] address Offset into the NFP CPP ID address space
298 * @param[in] size Size of the area to reserve
300 * @return NFP CPP handle, or NULL on failure (and set errno accordingly).
302 struct nfp_cpp_area *nfp_cpp_area_alloc(struct nfp_cpp *cpp, uint32_t cpp_id,
303 unsigned long long address,
307 * Allocate a NFP CPP area handle, as an offset into a CPP ID, by a named owner
308 * @param[in] cpp NFP CPP handle
309 * @param[in] cpp_id NFP CPP ID
310 * @param[in] name Name of owner of the area
311 * @param[in] address Offset into the NFP CPP ID address space
312 * @param[in] size Size of the area to reserve
314 * @return NFP CPP handle, or NULL on failure (and set errno accordingly).
316 struct nfp_cpp_area *nfp_cpp_area_alloc_with_name(struct nfp_cpp *cpp,
319 unsigned long long address,
323 * Free an allocated NFP CPP area handle
324 * @param[in] area NFP CPP area handle
326 void nfp_cpp_area_free(struct nfp_cpp_area *area);
329 * Acquire the resources needed to access the NFP CPP area handle
331 * @param[in] area NFP CPP area handle
333 * @return 0 on success, -1 on failure (and set errno accordingly).
335 int nfp_cpp_area_acquire(struct nfp_cpp_area *area);
338 * Release the resources needed to access the NFP CPP area handle
340 * @param[in] area NFP CPP area handle
342 void nfp_cpp_area_release(struct nfp_cpp_area *area);
345 * Allocate, then acquire the resources needed to access the NFP CPP area handle
346 * @param[in] cpp NFP CPP handle
347 * @param[in] cpp_id NFP CPP ID
348 * @param[in] address Offset into the NFP CPP ID address space
349 * @param[in] size Size of the area to reserve
351 * @return NFP CPP handle, or NULL on failure (and set errno accordingly).
353 struct nfp_cpp_area *nfp_cpp_area_alloc_acquire(struct nfp_cpp *cpp,
355 unsigned long long address,
359 * Release the resources, then free the NFP CPP area handle
360 * @param[in] area NFP CPP area handle
362 void nfp_cpp_area_release_free(struct nfp_cpp_area *area);
364 uint8_t *nfp_cpp_map_area(struct nfp_cpp *cpp, int domain, int target,
365 uint64_t addr, unsigned long size,
366 struct nfp_cpp_area **area);
368 * Return an IO pointer to the beginning of the NFP CPP area handle. The area
369 * must be acquired with 'nfp_cpp_area_acquire()' before calling this operation.
371 * @param[in] area NFP CPP area handle
373 * @return Pointer to IO memory, or NULL on failure (and set errno accordingly).
375 void *nfp_cpp_area_mapped(struct nfp_cpp_area *area);
378 * Read from a NFP CPP area handle into a buffer. The area must be acquired with
379 * 'nfp_cpp_area_acquire()' before calling this operation.
381 * @param[in] area NFP CPP area handle
382 * @param[in] offset Offset into the area
383 * @param[in] buffer Location of buffer to receive the data
384 * @param[in] length Length of the data to read
386 * @return bytes read on success, -1 on failure (and set errno accordingly).
389 int nfp_cpp_area_read(struct nfp_cpp_area *area, unsigned long offset,
390 void *buffer, size_t length);
393 * Write to a NFP CPP area handle from a buffer. The area must be acquired with
394 * 'nfp_cpp_area_acquire()' before calling this operation.
396 * @param[in] area NFP CPP area handle
397 * @param[in] offset Offset into the area
398 * @param[in] buffer Location of buffer that holds the data
399 * @param[in] length Length of the data to read
401 * @return bytes written on success, -1 on failure (and set errno accordingly).
403 int nfp_cpp_area_write(struct nfp_cpp_area *area, unsigned long offset,
404 const void *buffer, size_t length);
407 * nfp_cpp_area_iomem() - get IOMEM region for CPP area
408 * @area: CPP area handle
410 * Returns an iomem pointer for use with readl()/writel() style operations.
412 * NOTE: Area must have been locked down with an 'acquire'.
414 * Return: pointer to the area, or NULL
416 void *nfp_cpp_area_iomem(struct nfp_cpp_area *area);
419 * Verify that IO can be performed on an offset in an area
421 * @param[in] area NFP CPP area handle
422 * @param[in] offset Offset into the area
423 * @param[in] size Size of region to validate
425 * @return 0 on success, -1 on failure (and set errno accordingly).
427 int nfp_cpp_area_check_range(struct nfp_cpp_area *area,
428 unsigned long long offset, unsigned long size);
431 * Get the NFP CPP handle that is the parent of a NFP CPP area handle
433 * @param cpp_area NFP CPP area handle
434 * @return NFP CPP handle
436 struct nfp_cpp *nfp_cpp_area_cpp(struct nfp_cpp_area *cpp_area);
439 * Get the name passed during allocation of the NFP CPP area handle
441 * @param cpp_area NFP CPP area handle
442 * @return Pointer to the area's name
444 const char *nfp_cpp_area_name(struct nfp_cpp_area *cpp_area);
447 * Read a block of data from a NFP CPP ID
449 * @param[in] cpp NFP CPP handle
450 * @param[in] cpp_id NFP CPP ID
451 * @param[in] address Offset into the NFP CPP ID address space
452 * @param[in] kernel_vaddr Buffer to copy read data to
453 * @param[in] length Size of the area to reserve
455 * @return bytes read on success, -1 on failure (and set errno accordingly).
457 int nfp_cpp_read(struct nfp_cpp *cpp, uint32_t cpp_id,
458 unsigned long long address, void *kernel_vaddr, size_t length);
461 * Write a block of data to a NFP CPP ID
463 * @param[in] cpp NFP CPP handle
464 * @param[in] cpp_id NFP CPP ID
465 * @param[in] address Offset into the NFP CPP ID address space
466 * @param[in] kernel_vaddr Buffer to copy write data from
467 * @param[in] length Size of the area to reserve
469 * @return bytes written on success, -1 on failure (and set errno accordingly).
471 int nfp_cpp_write(struct nfp_cpp *cpp, uint32_t cpp_id,
472 unsigned long long address, const void *kernel_vaddr,
478 * Fill a NFP CPP area handle and offset with a value
480 * @param[in] area NFP CPP area handle
481 * @param[in] offset Offset into the NFP CPP ID address space
482 * @param[in] value 32-bit value to fill area with
483 * @param[in] length Size of the area to reserve
485 * @return bytes written on success, -1 on failure (and set errno accordingly).
487 int nfp_cpp_area_fill(struct nfp_cpp_area *area, unsigned long offset,
488 uint32_t value, size_t length);
491 * Read a single 32-bit value from a NFP CPP area handle
493 * @param area NFP CPP area handle
494 * @param offset offset into NFP CPP area handle
495 * @param value output value
497 * The area must be acquired with 'nfp_cpp_area_acquire()' before calling this
500 * NOTE: offset must be 32-bit aligned.
502 * @return 0 on success, or -1 on error (and set errno accordingly).
504 int nfp_cpp_area_readl(struct nfp_cpp_area *area, unsigned long offset,
508 * Write a single 32-bit value to a NFP CPP area handle
510 * @param area NFP CPP area handle
511 * @param offset offset into NFP CPP area handle
512 * @param value value to write
514 * The area must be acquired with 'nfp_cpp_area_acquire()' before calling this
517 * NOTE: offset must be 32-bit aligned.
519 * @return 0 on success, or -1 on error (and set errno accordingly).
521 int nfp_cpp_area_writel(struct nfp_cpp_area *area, unsigned long offset,
525 * Read a single 64-bit value from a NFP CPP area handle
527 * @param area NFP CPP area handle
528 * @param offset offset into NFP CPP area handle
529 * @param value output value
531 * The area must be acquired with 'nfp_cpp_area_acquire()' before calling this
534 * NOTE: offset must be 64-bit aligned.
536 * @return 0 on success, or -1 on error (and set errno accordingly).
538 int nfp_cpp_area_readq(struct nfp_cpp_area *area, unsigned long offset,
542 * Write a single 64-bit value to a NFP CPP area handle
544 * @param area NFP CPP area handle
545 * @param offset offset into NFP CPP area handle
546 * @param value value to write
548 * The area must be acquired with 'nfp_cpp_area_acquire()' before calling this
551 * NOTE: offset must be 64-bit aligned.
553 * @return 0 on success, or -1 on error (and set errno accordingly).
555 int nfp_cpp_area_writeq(struct nfp_cpp_area *area, unsigned long offset,
559 * Write a single 32-bit value on the XPB bus
561 * @param cpp NFP CPP device handle
562 * @param xpb_tgt XPB target and address
563 * @param value value to write
565 * @return 0 on success, or -1 on failure (and set errno accordingly).
567 int nfp_xpb_writel(struct nfp_cpp *cpp, uint32_t xpb_tgt, uint32_t value);
570 * Read a single 32-bit value from the XPB bus
572 * @param cpp NFP CPP device handle
573 * @param xpb_tgt XPB target and address
574 * @param value output value
576 * @return 0 on success, or -1 on failure (and set errno accordingly).
578 int nfp_xpb_readl(struct nfp_cpp *cpp, uint32_t xpb_tgt, uint32_t *value);
581 * Modify bits of a 32-bit value from the XPB bus
583 * @param cpp NFP CPP device handle
584 * @param xpb_tgt XPB target and address
585 * @param mask mask of bits to alter
586 * @param value value to modify
588 * @return 0 on success, or -1 on failure (and set errno accordingly).
590 int nfp_xpb_writelm(struct nfp_cpp *cpp, uint32_t xpb_tgt, uint32_t mask,
594 * Modify bits of a 32-bit value from the XPB bus
596 * @param cpp NFP CPP device handle
597 * @param xpb_tgt XPB target and address
598 * @param mask mask of bits to alter
599 * @param value value to monitor for
600 * @param timeout_us maximum number of us to wait (-1 for forever)
602 * @return >= 0 on success, or -1 on failure (and set errno accordingly).
604 int nfp_xpb_waitlm(struct nfp_cpp *cpp, uint32_t xpb_tgt, uint32_t mask,
605 uint32_t value, int timeout_us);
608 * Read a 32-bit word from a NFP CPP ID
610 * @param cpp NFP CPP handle
611 * @param cpp_id NFP CPP ID
612 * @param address offset into the NFP CPP ID address space
613 * @param value output value
615 * @return 0 on success, or -1 on failure (and set errno accordingly).
617 int nfp_cpp_readl(struct nfp_cpp *cpp, uint32_t cpp_id,
618 unsigned long long address, uint32_t *value);
621 * Write a 32-bit value to a NFP CPP ID
623 * @param cpp NFP CPP handle
624 * @param cpp_id NFP CPP ID
625 * @param address offset into the NFP CPP ID address space
626 * @param value value to write
628 * @return 0 on success, or -1 on failure (and set errno accordingly).
631 int nfp_cpp_writel(struct nfp_cpp *cpp, uint32_t cpp_id,
632 unsigned long long address, uint32_t value);
635 * Read a 64-bit work from a NFP CPP ID
637 * @param cpp NFP CPP handle
638 * @param cpp_id NFP CPP ID
639 * @param address offset into the NFP CPP ID address space
640 * @param value output value
642 * @return 0 on success, or -1 on failure (and set errno accordingly).
644 int nfp_cpp_readq(struct nfp_cpp *cpp, uint32_t cpp_id,
645 unsigned long long address, uint64_t *value);
648 * Write a 64-bit value to a NFP CPP ID
650 * @param cpp NFP CPP handle
651 * @param cpp_id NFP CPP ID
652 * @param address offset into the NFP CPP ID address space
653 * @param value value to write
655 * @return 0 on success, or -1 on failure (and set errno accordingly).
657 int nfp_cpp_writeq(struct nfp_cpp *cpp, uint32_t cpp_id,
658 unsigned long long address, uint64_t value);
661 * Initialize a mutex location
663 * The CPP target:address must point to a 64-bit aligned location, and will
664 * initialize 64 bits of data at the location.
666 * This creates the initial mutex state, as locked by this nfp_cpp_interface().
668 * This function should only be called when setting up the initial lock state
669 * upon boot-up of the system.
671 * @param cpp NFP CPP handle
672 * @param target NFP CPP target ID
673 * @param address Offset into the address space of the NFP CPP target ID
674 * @param key_id Unique 32-bit value for this mutex
676 * @return 0 on success, or -1 on failure (and set errno accordingly).
678 int nfp_cpp_mutex_init(struct nfp_cpp *cpp, int target,
679 unsigned long long address, uint32_t key_id);
682 * Create a mutex handle from an address controlled by a MU Atomic engine
684 * The CPP target:address must point to a 64-bit aligned location, and reserve
685 * 64 bits of data at the location for use by the handle.
687 * Only target/address pairs that point to entities that support the MU Atomic
688 * Engine's CmpAndSwap32 command are supported.
690 * @param cpp NFP CPP handle
691 * @param target NFP CPP target ID
692 * @param address Offset into the address space of the NFP CPP target ID
693 * @param key_id 32-bit unique key (must match the key at this location)
695 * @return A non-NULL struct nfp_cpp_mutex * on success, NULL on
698 struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target,
699 unsigned long long address,
703 * Get the NFP CPP handle the mutex was created with
705 * @param mutex NFP mutex handle
706 * @return NFP CPP handle
708 struct nfp_cpp *nfp_cpp_mutex_cpp(struct nfp_cpp_mutex *mutex);
713 * @param mutex NFP mutex handle
716 uint32_t nfp_cpp_mutex_key(struct nfp_cpp_mutex *mutex);
719 * Get the mutex owner
721 * @param mutex NFP mutex handle
722 * @return Interface ID of the mutex owner
724 * NOTE: This is for debug purposes ONLY - the owner may change at any time,
725 * unless it has been locked by this NFP CPP handle.
727 uint16_t nfp_cpp_mutex_owner(struct nfp_cpp_mutex *mutex);
730 * Get the mutex target
732 * @param mutex NFP mutex handle
733 * @return Mutex CPP target (ie NFP_CPP_TARGET_MU)
735 int nfp_cpp_mutex_target(struct nfp_cpp_mutex *mutex);
738 * Get the mutex address
740 * @param mutex NFP mutex handle
741 * @return Mutex CPP address
743 uint64_t nfp_cpp_mutex_address(struct nfp_cpp_mutex *mutex);
746 * Free a mutex handle - does not alter the lock state
748 * @param mutex NFP CPP Mutex handle
750 void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex);
753 * Lock a mutex handle, using the NFP MU Atomic Engine
755 * @param mutex NFP CPP Mutex handle
757 * @return 0 on success, or -1 on failure (and set errno accordingly).
759 int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex);
762 * Unlock a mutex handle, using the NFP MU Atomic Engine
764 * @param mutex NFP CPP Mutex handle
766 * @return 0 on success, or -1 on failure (and set errno accordingly).
768 int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex);
771 * Attempt to lock a mutex handle, using the NFP MU Atomic Engine
773 * @param mutex NFP CPP Mutex handle
774 * @return 0 if the lock succeeded, -1 on failure (and errno set
777 int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex);
779 #endif /* !__NFP_CPP_H__ */