Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
0002 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
0003 
0004 /*
0005  * nfp_cpp.h
0006  * Interface for low-level NFP CPP access.
0007  * Authors: Jason McMullan <jason.mcmullan@netronome.com>
0008  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
0009  */
0010 #ifndef __NFP_CPP_H__
0011 #define __NFP_CPP_H__
0012 
0013 #include <linux/ctype.h>
0014 #include <linux/types.h>
0015 #include <linux/sizes.h>
0016 
0017 #ifndef NFP_SUBSYS
0018 #define NFP_SUBSYS "nfp"
0019 #endif
0020 
0021 #define nfp_err(cpp, fmt, args...) \
0022     dev_err(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
0023 #define nfp_warn(cpp, fmt, args...) \
0024     dev_warn(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
0025 #define nfp_info(cpp, fmt, args...) \
0026     dev_info(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
0027 #define nfp_dbg(cpp, fmt, args...) \
0028     dev_dbg(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
0029 #define nfp_printk(level, cpp, fmt, args...) \
0030     dev_printk(level, nfp_cpp_device(cpp)->parent,  \
0031            NFP_SUBSYS ": " fmt, ## args)
0032 
0033 #define PCI_64BIT_BAR_COUNT             3
0034 
0035 #define NFP_CPP_NUM_TARGETS             16
0036 /* Max size of area it should be safe to request */
0037 #define NFP_CPP_SAFE_AREA_SIZE      SZ_2M
0038 
0039 /* NFP_MUTEX_WAIT_* are timeouts in seconds when waiting for a mutex */
0040 #define NFP_MUTEX_WAIT_FIRST_WARN   15
0041 #define NFP_MUTEX_WAIT_NEXT_WARN    5
0042 #define NFP_MUTEX_WAIT_ERROR        60
0043 
0044 struct device;
0045 
0046 struct nfp_cpp_area;
0047 struct nfp_cpp;
0048 struct resource;
0049 
0050 /* Wildcard indicating a CPP read or write action
0051  *
0052  * The action used will be either read or write depending on whether a
0053  * read or write instruction/call is performed on the NFP_CPP_ID.  It
0054  * is recomended that the RW action is used even if all actions to be
0055  * performed on a NFP_CPP_ID are known to be only reads or writes.
0056  * Doing so will in many cases save NFP CPP internal software
0057  * resources.
0058  */
0059 #define NFP_CPP_ACTION_RW               32
0060 
0061 #define NFP_CPP_TARGET_ID_MASK          0x1f
0062 
0063 #define NFP_CPP_ATOMIC_RD(target, island) \
0064     NFP_CPP_ISLAND_ID((target), 3, 0, (island))
0065 #define NFP_CPP_ATOMIC_WR(target, island) \
0066     NFP_CPP_ISLAND_ID((target), 4, 0, (island))
0067 
0068 /**
0069  * NFP_CPP_ID() - pack target, token, and action into a CPP ID.
0070  * @target:     NFP CPP target id
0071  * @action:     NFP CPP action id
0072  * @token:      NFP CPP token id
0073  *
0074  * Create a 32-bit CPP identifier representing the access to be made.
0075  * These identifiers are used as parameters to other NFP CPP
0076  * functions.  Some CPP devices may allow wildcard identifiers to be
0077  * specified.
0078  *
0079  * Return:      NFP CPP ID
0080  */
0081 #define NFP_CPP_ID(target, action, token)            \
0082     ((((target) & 0x7f) << 24) | (((token)  & 0xff) << 16) | \
0083      (((action) & 0xff) <<  8))
0084 
0085 /**
0086  * NFP_CPP_ISLAND_ID() - pack target, token, action, and island into a CPP ID.
0087  * @target:     NFP CPP target id
0088  * @action:     NFP CPP action id
0089  * @token:      NFP CPP token id
0090  * @island:     NFP CPP island id
0091  *
0092  * Create a 32-bit CPP identifier representing the access to be made.
0093  * These identifiers are used as parameters to other NFP CPP
0094  * functions.  Some CPP devices may allow wildcard identifiers to be
0095  * specified.
0096  *
0097  * Return:      NFP CPP ID
0098  */
0099 #define NFP_CPP_ISLAND_ID(target, action, token, island)     \
0100     ((((target) & 0x7f) << 24) | (((token)  & 0xff) << 16) | \
0101      (((action) & 0xff) <<  8) | (((island) & 0xff) << 0))
0102 
0103 /**
0104  * NFP_CPP_ID_TARGET_of() - Return the NFP CPP target of a NFP CPP ID
0105  * @id:         NFP CPP ID
0106  *
0107  * Return:      NFP CPP target
0108  */
0109 static inline u8 NFP_CPP_ID_TARGET_of(u32 id)
0110 {
0111     return (id >> 24) & NFP_CPP_TARGET_ID_MASK;
0112 }
0113 
0114 /**
0115  * NFP_CPP_ID_TOKEN_of() - Return the NFP CPP token of a NFP CPP ID
0116  * @id:         NFP CPP ID
0117  * Return:      NFP CPP token
0118  */
0119 static inline u8 NFP_CPP_ID_TOKEN_of(u32 id)
0120 {
0121     return (id >> 16) & 0xff;
0122 }
0123 
0124 /**
0125  * NFP_CPP_ID_ACTION_of() - Return the NFP CPP action of a NFP CPP ID
0126  * @id:         NFP CPP ID
0127  *
0128  * Return:      NFP CPP action
0129  */
0130 static inline u8 NFP_CPP_ID_ACTION_of(u32 id)
0131 {
0132     return (id >> 8) & 0xff;
0133 }
0134 
0135 /**
0136  * NFP_CPP_ID_ISLAND_of() - Return the NFP CPP island of a NFP CPP ID
0137  * @id: NFP CPP ID
0138  *
0139  * Return:      NFP CPP island
0140  */
0141 static inline u8 NFP_CPP_ID_ISLAND_of(u32 id)
0142 {
0143     return (id >> 0) & 0xff;
0144 }
0145 
0146 /* NFP Interface types - logical interface for this CPP connection
0147  * 4 bits are reserved for interface type.
0148  */
0149 #define NFP_CPP_INTERFACE_TYPE_INVALID      0x0
0150 #define NFP_CPP_INTERFACE_TYPE_PCI          0x1
0151 #define NFP_CPP_INTERFACE_TYPE_ARM          0x2
0152 #define NFP_CPP_INTERFACE_TYPE_RPC          0x3
0153 #define NFP_CPP_INTERFACE_TYPE_ILA          0x4
0154 
0155 /**
0156  * NFP_CPP_INTERFACE() - Construct a 16-bit NFP Interface ID
0157  * @type:       NFP Interface Type
0158  * @unit:       Unit identifier for the interface type
0159  * @channel:    Channel identifier for the interface unit
0160  *
0161  * Interface IDs consists of 4 bits of interface type,
0162  * 4 bits of unit identifier, and 8 bits of channel identifier.
0163  *
0164  * The NFP Interface ID is used in the implementation of
0165  * NFP CPP API mutexes, which use the MU Atomic CompareAndWrite
0166  * operation - hence the limit to 16 bits to be able to
0167  * use the NFP Interface ID as a lock owner.
0168  *
0169  * Return:      Interface ID
0170  */
0171 #define NFP_CPP_INTERFACE(type, unit, channel)  \
0172     ((((type) & 0xf) << 12) |       \
0173      (((unit) & 0xf) <<  8) |       \
0174      (((channel) & 0xff) << 0))
0175 
0176 /**
0177  * NFP_CPP_INTERFACE_TYPE_of() - Get the interface type
0178  * @interface:  NFP Interface ID
0179  * Return:      NFP Interface ID's type
0180  */
0181 #define NFP_CPP_INTERFACE_TYPE_of(interface)   (((interface) >> 12) & 0xf)
0182 
0183 /**
0184  * NFP_CPP_INTERFACE_UNIT_of() - Get the interface unit
0185  * @interface:  NFP Interface ID
0186  * Return:      NFP Interface ID's unit
0187  */
0188 #define NFP_CPP_INTERFACE_UNIT_of(interface)   (((interface) >>  8) & 0xf)
0189 
0190 /**
0191  * NFP_CPP_INTERFACE_CHANNEL_of() - Get the interface channel
0192  * @interface:  NFP Interface ID
0193  * Return:      NFP Interface ID's channel
0194  */
0195 #define NFP_CPP_INTERFACE_CHANNEL_of(interface)   (((interface) >>  0) & 0xff)
0196 
0197 /* Implemented in nfp_cppcore.c */
0198 void nfp_cpp_free(struct nfp_cpp *cpp);
0199 u32 nfp_cpp_model(struct nfp_cpp *cpp);
0200 u16 nfp_cpp_interface(struct nfp_cpp *cpp);
0201 int nfp_cpp_serial(struct nfp_cpp *cpp, const u8 **serial);
0202 unsigned int nfp_cpp_mu_locality_lsb(struct nfp_cpp *cpp);
0203 
0204 struct nfp_cpp_area *nfp_cpp_area_alloc_with_name(struct nfp_cpp *cpp,
0205                           u32 cpp_id,
0206                           const char *name,
0207                           unsigned long long address,
0208                           unsigned long size);
0209 struct nfp_cpp_area *nfp_cpp_area_alloc(struct nfp_cpp *cpp, u32 cpp_id,
0210                     unsigned long long address,
0211                     unsigned long size);
0212 struct nfp_cpp_area *
0213 nfp_cpp_area_alloc_acquire(struct nfp_cpp *cpp, const char *name, u32 cpp_id,
0214                unsigned long long address, unsigned long size);
0215 void nfp_cpp_area_free(struct nfp_cpp_area *area);
0216 int nfp_cpp_area_acquire(struct nfp_cpp_area *area);
0217 int nfp_cpp_area_acquire_nonblocking(struct nfp_cpp_area *area);
0218 void nfp_cpp_area_release(struct nfp_cpp_area *area);
0219 void nfp_cpp_area_release_free(struct nfp_cpp_area *area);
0220 int nfp_cpp_area_read(struct nfp_cpp_area *area, unsigned long offset,
0221               void *buffer, size_t length);
0222 int nfp_cpp_area_write(struct nfp_cpp_area *area, unsigned long offset,
0223                const void *buffer, size_t length);
0224 size_t nfp_cpp_area_size(struct nfp_cpp_area *area);
0225 const char *nfp_cpp_area_name(struct nfp_cpp_area *cpp_area);
0226 void *nfp_cpp_area_priv(struct nfp_cpp_area *cpp_area);
0227 struct nfp_cpp *nfp_cpp_area_cpp(struct nfp_cpp_area *cpp_area);
0228 struct resource *nfp_cpp_area_resource(struct nfp_cpp_area *area);
0229 phys_addr_t nfp_cpp_area_phys(struct nfp_cpp_area *area);
0230 void __iomem *nfp_cpp_area_iomem(struct nfp_cpp_area *area);
0231 
0232 int nfp_cpp_area_readl(struct nfp_cpp_area *area, unsigned long offset,
0233                u32 *value);
0234 int nfp_cpp_area_writel(struct nfp_cpp_area *area, unsigned long offset,
0235             u32 value);
0236 int nfp_cpp_area_readq(struct nfp_cpp_area *area, unsigned long offset,
0237                u64 *value);
0238 int nfp_cpp_area_writeq(struct nfp_cpp_area *area, unsigned long offset,
0239             u64 value);
0240 int nfp_cpp_area_fill(struct nfp_cpp_area *area, unsigned long offset,
0241               u32 value, size_t length);
0242 
0243 int nfp_xpb_readl(struct nfp_cpp *cpp, u32 xpb_tgt, u32 *value);
0244 int nfp_xpb_writel(struct nfp_cpp *cpp, u32 xpb_tgt, u32 value);
0245 int nfp_xpb_writelm(struct nfp_cpp *cpp, u32 xpb_tgt, u32 mask, u32 value);
0246 
0247 /* Implemented in nfp_cpplib.c */
0248 int nfp_cpp_read(struct nfp_cpp *cpp, u32 cpp_id,
0249          unsigned long long address, void *kernel_vaddr, size_t length);
0250 int nfp_cpp_write(struct nfp_cpp *cpp, u32 cpp_id,
0251           unsigned long long address, const void *kernel_vaddr,
0252           size_t length);
0253 int nfp_cpp_readl(struct nfp_cpp *cpp, u32 cpp_id,
0254           unsigned long long address, u32 *value);
0255 int nfp_cpp_writel(struct nfp_cpp *cpp, u32 cpp_id,
0256            unsigned long long address, u32 value);
0257 int nfp_cpp_readq(struct nfp_cpp *cpp, u32 cpp_id,
0258           unsigned long long address, u64 *value);
0259 int nfp_cpp_writeq(struct nfp_cpp *cpp, u32 cpp_id,
0260            unsigned long long address, u64 value);
0261 
0262 u8 __iomem *
0263 nfp_cpp_map_area(struct nfp_cpp *cpp, const char *name, u32 cpp_id, u64 addr,
0264          unsigned long size, struct nfp_cpp_area **area);
0265 
0266 struct nfp_cpp_mutex;
0267 
0268 int nfp_cpp_mutex_init(struct nfp_cpp *cpp, int target,
0269                unsigned long long address, u32 key_id);
0270 struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target,
0271                       unsigned long long address,
0272                       u32 key_id);
0273 void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex);
0274 int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex);
0275 int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex);
0276 int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex);
0277 int nfp_cpp_mutex_reclaim(struct nfp_cpp *cpp, int target,
0278               unsigned long long address);
0279 
0280 /**
0281  * nfp_cppcore_pcie_unit() - Get PCI Unit of a CPP handle
0282  * @cpp:    CPP handle
0283  *
0284  * Return: PCI unit for the NFP CPP handle
0285  */
0286 static inline u8 nfp_cppcore_pcie_unit(struct nfp_cpp *cpp)
0287 {
0288     return NFP_CPP_INTERFACE_UNIT_of(nfp_cpp_interface(cpp));
0289 }
0290 
0291 struct nfp_cpp_explicit;
0292 
0293 struct nfp_cpp_explicit_command {
0294     u32 cpp_id;
0295     u16 data_ref;
0296     u8  data_master;
0297     u8  len;
0298     u8  byte_mask;
0299     u8  signal_master;
0300     u8  signal_ref;
0301     u8  posted;
0302     u8  siga;
0303     u8  sigb;
0304     s8   siga_mode;
0305     s8   sigb_mode;
0306 };
0307 
0308 #define NFP_SERIAL_LEN      6
0309 
0310 /**
0311  * struct nfp_cpp_operations - NFP CPP operations structure
0312  * @area_priv_size:     Size of the nfp_cpp_area private data
0313  * @owner:              Owner module
0314  * @init:               Initialize the NFP CPP bus
0315  * @free:               Free the bus
0316  * @read_serial:    Read serial number to memory provided
0317  * @get_interface:  Return CPP interface
0318  * @area_init:          Initialize a new NFP CPP area (not serialized)
0319  * @area_cleanup:       Clean up a NFP CPP area (not serialized)
0320  * @area_acquire:       Acquire the NFP CPP area (serialized)
0321  * @area_release:       Release area (serialized)
0322  * @area_resource:      Get resource range of area (not serialized)
0323  * @area_phys:          Get physical address of area (not serialized)
0324  * @area_iomem:         Get iomem of area (not serialized)
0325  * @area_read:          Perform a read from a NFP CPP area (serialized)
0326  * @area_write:         Perform a write to a NFP CPP area (serialized)
0327  * @explicit_priv_size: Size of an explicit's private area
0328  * @explicit_acquire:   Acquire an explicit area
0329  * @explicit_release:   Release an explicit area
0330  * @explicit_put:       Write data to send
0331  * @explicit_get:       Read data received
0332  * @explicit_do:        Perform the transaction
0333  */
0334 struct nfp_cpp_operations {
0335     size_t area_priv_size;
0336     struct module *owner;
0337 
0338     int (*init)(struct nfp_cpp *cpp);
0339     void (*free)(struct nfp_cpp *cpp);
0340 
0341     int (*read_serial)(struct device *dev, u8 *serial);
0342     int (*get_interface)(struct device *dev);
0343 
0344     int (*area_init)(struct nfp_cpp_area *area,
0345              u32 dest, unsigned long long address,
0346              unsigned long size);
0347     void (*area_cleanup)(struct nfp_cpp_area *area);
0348     int (*area_acquire)(struct nfp_cpp_area *area);
0349     void (*area_release)(struct nfp_cpp_area *area);
0350     struct resource *(*area_resource)(struct nfp_cpp_area *area);
0351     phys_addr_t (*area_phys)(struct nfp_cpp_area *area);
0352     void __iomem *(*area_iomem)(struct nfp_cpp_area *area);
0353     int (*area_read)(struct nfp_cpp_area *area, void *kernel_vaddr,
0354              unsigned long offset, unsigned int length);
0355     int (*area_write)(struct nfp_cpp_area *area, const void *kernel_vaddr,
0356               unsigned long offset, unsigned int length);
0357 
0358     size_t explicit_priv_size;
0359     int (*explicit_acquire)(struct nfp_cpp_explicit *expl);
0360     void (*explicit_release)(struct nfp_cpp_explicit *expl);
0361     int (*explicit_put)(struct nfp_cpp_explicit *expl,
0362                 const void *buff, size_t len);
0363     int (*explicit_get)(struct nfp_cpp_explicit *expl,
0364                 void *buff, size_t len);
0365     int (*explicit_do)(struct nfp_cpp_explicit *expl,
0366                const struct nfp_cpp_explicit_command *cmd,
0367                u64 address);
0368 };
0369 
0370 struct nfp_cpp *
0371 nfp_cpp_from_operations(const struct nfp_cpp_operations *ops,
0372             struct device *parent, void *priv);
0373 void *nfp_cpp_priv(struct nfp_cpp *priv);
0374 
0375 int nfp_cpp_area_cache_add(struct nfp_cpp *cpp, size_t size);
0376 
0377 /* The following section contains extensions to the
0378  * NFP CPP API, to be used in a Linux kernel-space context.
0379  */
0380 
0381 /* Use this channel ID for multiple virtual channel interfaces
0382  * (ie ARM and PCIe) when setting up the interface field.
0383  */
0384 #define NFP_CPP_INTERFACE_CHANNEL_PEROPENER 255
0385 struct device *nfp_cpp_device(struct nfp_cpp *cpp);
0386 
0387 /* Return code masks for nfp_cpp_explicit_do()
0388  */
0389 #define NFP_SIGNAL_MASK_A   BIT(0)  /* Signal A fired */
0390 #define NFP_SIGNAL_MASK_B   BIT(1)  /* Signal B fired */
0391 
0392 enum nfp_cpp_explicit_signal_mode {
0393     NFP_SIGNAL_NONE = 0,
0394     NFP_SIGNAL_PUSH = 1,
0395     NFP_SIGNAL_PUSH_OPTIONAL = -1,
0396     NFP_SIGNAL_PULL = 2,
0397     NFP_SIGNAL_PULL_OPTIONAL = -2,
0398 };
0399 
0400 struct nfp_cpp_explicit *nfp_cpp_explicit_acquire(struct nfp_cpp *cpp);
0401 int nfp_cpp_explicit_set_target(struct nfp_cpp_explicit *expl, u32 cpp_id,
0402                 u8 len, u8 mask);
0403 int nfp_cpp_explicit_set_data(struct nfp_cpp_explicit *expl,
0404                   u8 data_master, u16 data_ref);
0405 int nfp_cpp_explicit_set_signal(struct nfp_cpp_explicit *expl,
0406                 u8 signal_master, u8 signal_ref);
0407 int nfp_cpp_explicit_set_posted(struct nfp_cpp_explicit *expl, int posted,
0408                 u8 siga,
0409                 enum nfp_cpp_explicit_signal_mode siga_mode,
0410                 u8 sigb,
0411                 enum nfp_cpp_explicit_signal_mode sigb_mode);
0412 int nfp_cpp_explicit_put(struct nfp_cpp_explicit *expl,
0413              const void *buff, size_t len);
0414 int nfp_cpp_explicit_do(struct nfp_cpp_explicit *expl, u64 address);
0415 int nfp_cpp_explicit_get(struct nfp_cpp_explicit *expl, void *buff, size_t len);
0416 void nfp_cpp_explicit_release(struct nfp_cpp_explicit *expl);
0417 struct nfp_cpp *nfp_cpp_explicit_cpp(struct nfp_cpp_explicit *expl);
0418 void *nfp_cpp_explicit_priv(struct nfp_cpp_explicit *cpp_explicit);
0419 
0420 /* Implemented in nfp_cpplib.c */
0421 
0422 int nfp_cpp_model_autodetect(struct nfp_cpp *cpp, u32 *model);
0423 
0424 int nfp_cpp_explicit_read(struct nfp_cpp *cpp, u32 cpp_id,
0425               u64 addr, void *buff, size_t len,
0426               int width_read);
0427 
0428 int nfp_cpp_explicit_write(struct nfp_cpp *cpp, u32 cpp_id,
0429                u64 addr, const void *buff, size_t len,
0430                int width_write);
0431 
0432 #endif /* !__NFP_CPP_H__ */