xref: /dpdk/drivers/common/nfp/nfp_common.h (revision 87f5b35ba4e82ebe010c3aebb40a738da35a3130)
1503ac807SChaoyong He /* SPDX-License-Identifier: BSD-3-Clause
2503ac807SChaoyong He  * Copyright (c) 2023 Corigine, Inc.
3503ac807SChaoyong He  * All rights reserved.
4503ac807SChaoyong He  */
5503ac807SChaoyong He 
6503ac807SChaoyong He #ifndef __NFP_COMMON_H__
7503ac807SChaoyong He #define __NFP_COMMON_H__
8503ac807SChaoyong He 
9503ac807SChaoyong He #include <rte_byteorder.h>
10503ac807SChaoyong He #include <rte_ether.h>
11503ac807SChaoyong He #include <rte_io.h>
12503ac807SChaoyong He #include <rte_spinlock.h>
13503ac807SChaoyong He 
14503ac807SChaoyong He #include "nfp_common_ctrl.h"
15503ac807SChaoyong He 
16503ac807SChaoyong He #define NFP_QCP_QUEUE_ADDR_SZ   (0x800)
17503ac807SChaoyong He 
18503ac807SChaoyong He /* Macros for accessing the Queue Controller Peripheral 'CSRs' */
19503ac807SChaoyong He #define NFP_QCP_QUEUE_OFF(_x)                 ((_x) * 0x800)
20503ac807SChaoyong He #define NFP_QCP_QUEUE_ADD_RPTR                  0x0000
21503ac807SChaoyong He #define NFP_QCP_QUEUE_ADD_WPTR                  0x0004
22503ac807SChaoyong He #define NFP_QCP_QUEUE_STS_LO                    0x0008
23503ac807SChaoyong He #define NFP_QCP_QUEUE_STS_LO_READPTR_MASK     (0x3ffff)
24503ac807SChaoyong He #define NFP_QCP_QUEUE_STS_HI                    0x000c
25503ac807SChaoyong He #define NFP_QCP_QUEUE_STS_HI_WRITEPTR_MASK    (0x3ffff)
26503ac807SChaoyong He 
27503ac807SChaoyong He /* Read or Write Pointer of a queue */
28503ac807SChaoyong He enum nfp_qcp_ptr {
29503ac807SChaoyong He 	NFP_QCP_READ_PTR = 0,
30503ac807SChaoyong He 	NFP_QCP_WRITE_PTR
31503ac807SChaoyong He };
32503ac807SChaoyong He 
33503ac807SChaoyong He struct nfp_hw {
34503ac807SChaoyong He 	uint8_t *ctrl_bar;
35503ac807SChaoyong He 	uint8_t *qcp_cfg;
36503ac807SChaoyong He 	uint32_t cap;
37503ac807SChaoyong He 	uint32_t cap_ext;
38503ac807SChaoyong He 	uint32_t ctrl;
39503ac807SChaoyong He 	uint32_t ctrl_ext;
40503ac807SChaoyong He 	rte_spinlock_t reconfig_lock;
41503ac807SChaoyong He 	struct rte_ether_addr mac_addr;
42503ac807SChaoyong He };
43503ac807SChaoyong He 
44503ac807SChaoyong He static inline uint8_t
nn_readb(volatile const void * addr)45503ac807SChaoyong He nn_readb(volatile const void *addr)
46503ac807SChaoyong He {
47503ac807SChaoyong He 	return rte_read8(addr);
48503ac807SChaoyong He }
49503ac807SChaoyong He 
50503ac807SChaoyong He static inline void
nn_writeb(uint8_t val,volatile void * addr)51503ac807SChaoyong He nn_writeb(uint8_t val,
52503ac807SChaoyong He 		volatile void *addr)
53503ac807SChaoyong He {
54503ac807SChaoyong He 	rte_write8(val, addr);
55503ac807SChaoyong He }
56503ac807SChaoyong He 
57503ac807SChaoyong He static inline uint32_t
nn_readl(volatile const void * addr)58503ac807SChaoyong He nn_readl(volatile const void *addr)
59503ac807SChaoyong He {
60503ac807SChaoyong He 	return rte_read32(addr);
61503ac807SChaoyong He }
62503ac807SChaoyong He 
63503ac807SChaoyong He static inline void
nn_writel(uint32_t val,volatile void * addr)64503ac807SChaoyong He nn_writel(uint32_t val,
65503ac807SChaoyong He 		volatile void *addr)
66503ac807SChaoyong He {
67503ac807SChaoyong He 	rte_write32(val, addr);
68503ac807SChaoyong He }
69503ac807SChaoyong He 
70503ac807SChaoyong He static inline uint16_t
nn_readw(volatile const void * addr)71503ac807SChaoyong He nn_readw(volatile const void *addr)
72503ac807SChaoyong He {
73503ac807SChaoyong He 	return rte_read16(addr);
74503ac807SChaoyong He }
75503ac807SChaoyong He 
76503ac807SChaoyong He static inline void
nn_writew(uint16_t val,volatile void * addr)77503ac807SChaoyong He nn_writew(uint16_t val,
78503ac807SChaoyong He 		volatile void *addr)
79503ac807SChaoyong He {
80503ac807SChaoyong He 	rte_write16(val, addr);
81503ac807SChaoyong He }
82503ac807SChaoyong He 
83503ac807SChaoyong He static inline uint64_t
nn_readq(volatile void * addr)84503ac807SChaoyong He nn_readq(volatile void *addr)
85503ac807SChaoyong He {
86503ac807SChaoyong He 	uint32_t low;
87503ac807SChaoyong He 	uint32_t high;
88503ac807SChaoyong He 	const volatile uint32_t *p = addr;
89503ac807SChaoyong He 
90503ac807SChaoyong He 	high = nn_readl((volatile const void *)(p + 1));
91503ac807SChaoyong He 	low = nn_readl((volatile const void *)p);
92503ac807SChaoyong He 
93503ac807SChaoyong He 	return low + ((uint64_t)high << 32);
94503ac807SChaoyong He }
95503ac807SChaoyong He 
96503ac807SChaoyong He static inline void
nn_writeq(uint64_t val,volatile void * addr)97503ac807SChaoyong He nn_writeq(uint64_t val,
98503ac807SChaoyong He 		volatile void *addr)
99503ac807SChaoyong He {
100503ac807SChaoyong He 	nn_writel(val >> 32, (volatile char *)addr + 4);
101503ac807SChaoyong He 	nn_writel(val, addr);
102503ac807SChaoyong He }
103503ac807SChaoyong He 
104503ac807SChaoyong He static inline uint8_t
nn_cfg_readb(struct nfp_hw * hw,uint32_t off)105503ac807SChaoyong He nn_cfg_readb(struct nfp_hw *hw,
106503ac807SChaoyong He 		uint32_t off)
107503ac807SChaoyong He {
108503ac807SChaoyong He 	return nn_readb(hw->ctrl_bar + off);
109503ac807SChaoyong He }
110503ac807SChaoyong He 
111503ac807SChaoyong He static inline void
nn_cfg_writeb(struct nfp_hw * hw,uint32_t off,uint8_t val)112503ac807SChaoyong He nn_cfg_writeb(struct nfp_hw *hw,
113503ac807SChaoyong He 		uint32_t off,
114503ac807SChaoyong He 		uint8_t val)
115503ac807SChaoyong He {
116503ac807SChaoyong He 	nn_writeb(val, hw->ctrl_bar + off);
117503ac807SChaoyong He }
118503ac807SChaoyong He 
119503ac807SChaoyong He static inline uint16_t
nn_cfg_readw(struct nfp_hw * hw,uint32_t off)120503ac807SChaoyong He nn_cfg_readw(struct nfp_hw *hw,
121503ac807SChaoyong He 		uint32_t off)
122503ac807SChaoyong He {
123503ac807SChaoyong He 	return rte_le_to_cpu_16(nn_readw(hw->ctrl_bar + off));
124503ac807SChaoyong He }
125503ac807SChaoyong He 
126503ac807SChaoyong He static inline void
nn_cfg_writew(struct nfp_hw * hw,uint32_t off,uint16_t val)127503ac807SChaoyong He nn_cfg_writew(struct nfp_hw *hw,
128503ac807SChaoyong He 		uint32_t off,
129503ac807SChaoyong He 		uint16_t val)
130503ac807SChaoyong He {
131503ac807SChaoyong He 	nn_writew(rte_cpu_to_le_16(val), hw->ctrl_bar + off);
132503ac807SChaoyong He }
133503ac807SChaoyong He 
134503ac807SChaoyong He static inline uint32_t
nn_cfg_readl(struct nfp_hw * hw,uint32_t off)135503ac807SChaoyong He nn_cfg_readl(struct nfp_hw *hw,
136503ac807SChaoyong He 		uint32_t off)
137503ac807SChaoyong He {
138503ac807SChaoyong He 	return rte_le_to_cpu_32(nn_readl(hw->ctrl_bar + off));
139503ac807SChaoyong He }
140503ac807SChaoyong He 
141503ac807SChaoyong He static inline void
nn_cfg_writel(struct nfp_hw * hw,uint32_t off,uint32_t val)142503ac807SChaoyong He nn_cfg_writel(struct nfp_hw *hw,
143503ac807SChaoyong He 		uint32_t off,
144503ac807SChaoyong He 		uint32_t val)
145503ac807SChaoyong He {
146503ac807SChaoyong He 	nn_writel(rte_cpu_to_le_32(val), hw->ctrl_bar + off);
147503ac807SChaoyong He }
148503ac807SChaoyong He 
149503ac807SChaoyong He static inline uint64_t
nn_cfg_readq(struct nfp_hw * hw,uint32_t off)150503ac807SChaoyong He nn_cfg_readq(struct nfp_hw *hw,
151503ac807SChaoyong He 		uint32_t off)
152503ac807SChaoyong He {
153503ac807SChaoyong He 	return rte_le_to_cpu_64(nn_readq(hw->ctrl_bar + off));
154503ac807SChaoyong He }
155503ac807SChaoyong He 
156503ac807SChaoyong He static inline void
nn_cfg_writeq(struct nfp_hw * hw,uint32_t off,uint64_t val)157503ac807SChaoyong He nn_cfg_writeq(struct nfp_hw *hw,
158503ac807SChaoyong He 		uint32_t off,
159503ac807SChaoyong He 		uint64_t val)
160503ac807SChaoyong He {
161503ac807SChaoyong He 	nn_writeq(rte_cpu_to_le_64(val), hw->ctrl_bar + off);
162503ac807SChaoyong He }
163503ac807SChaoyong He 
164503ac807SChaoyong He /**
165503ac807SChaoyong He  * Add the value to the selected pointer of a queue.
166503ac807SChaoyong He  *
167503ac807SChaoyong He  * @param queue
168503ac807SChaoyong He  *   Base address for queue structure
169503ac807SChaoyong He  * @param ptr
170503ac807SChaoyong He  *   Add to the read or write pointer
171503ac807SChaoyong He  * @param val
172503ac807SChaoyong He  *   Value to add to the queue pointer
173503ac807SChaoyong He  */
174503ac807SChaoyong He static inline void
nfp_qcp_ptr_add(uint8_t * queue,enum nfp_qcp_ptr ptr,uint32_t val)175503ac807SChaoyong He nfp_qcp_ptr_add(uint8_t *queue,
176503ac807SChaoyong He 		enum nfp_qcp_ptr ptr,
177503ac807SChaoyong He 		uint32_t val)
178503ac807SChaoyong He {
179503ac807SChaoyong He 	uint32_t off;
180503ac807SChaoyong He 
181503ac807SChaoyong He 	if (ptr == NFP_QCP_READ_PTR)
182503ac807SChaoyong He 		off = NFP_QCP_QUEUE_ADD_RPTR;
183503ac807SChaoyong He 	else
184503ac807SChaoyong He 		off = NFP_QCP_QUEUE_ADD_WPTR;
185503ac807SChaoyong He 
186503ac807SChaoyong He 	nn_writel(rte_cpu_to_le_32(val), queue + off);
187503ac807SChaoyong He }
188503ac807SChaoyong He 
189503ac807SChaoyong He /**
190503ac807SChaoyong He  * Read the current read/write pointer value for a queue.
191503ac807SChaoyong He  *
192503ac807SChaoyong He  * @param queue
193503ac807SChaoyong He  *   Base address for queue structure
194503ac807SChaoyong He  * @param ptr
195503ac807SChaoyong He  *   Read or Write pointer
196503ac807SChaoyong He  */
197503ac807SChaoyong He static inline uint32_t
nfp_qcp_read(uint8_t * queue,enum nfp_qcp_ptr ptr)198503ac807SChaoyong He nfp_qcp_read(uint8_t *queue,
199503ac807SChaoyong He 		enum nfp_qcp_ptr ptr)
200503ac807SChaoyong He {
201503ac807SChaoyong He 	uint32_t off;
202503ac807SChaoyong He 	uint32_t val;
203503ac807SChaoyong He 
204503ac807SChaoyong He 	if (ptr == NFP_QCP_READ_PTR)
205503ac807SChaoyong He 		off = NFP_QCP_QUEUE_STS_LO;
206503ac807SChaoyong He 	else
207503ac807SChaoyong He 		off = NFP_QCP_QUEUE_STS_HI;
208503ac807SChaoyong He 
209503ac807SChaoyong He 	val = rte_cpu_to_le_32(nn_readl(queue + off));
210503ac807SChaoyong He 
211503ac807SChaoyong He 	if (ptr == NFP_QCP_READ_PTR)
212503ac807SChaoyong He 		return val & NFP_QCP_QUEUE_STS_LO_READPTR_MASK;
213503ac807SChaoyong He 	else
214503ac807SChaoyong He 		return val & NFP_QCP_QUEUE_STS_HI_WRITEPTR_MASK;
215503ac807SChaoyong He }
216503ac807SChaoyong He 
217503ac807SChaoyong He __rte_internal
218503ac807SChaoyong He int nfp_reconfig_real(struct nfp_hw *hw, uint32_t update);
219503ac807SChaoyong He 
220503ac807SChaoyong He __rte_internal
221503ac807SChaoyong He int nfp_reconfig(struct nfp_hw *hw, uint32_t ctrl, uint32_t update);
222503ac807SChaoyong He 
223503ac807SChaoyong He __rte_internal
224503ac807SChaoyong He int nfp_ext_reconfig(struct nfp_hw *hw, uint32_t ctrl_ext, uint32_t update);
225503ac807SChaoyong He 
226503ac807SChaoyong He __rte_internal
227503ac807SChaoyong He void nfp_read_mac(struct nfp_hw *hw);
228503ac807SChaoyong He 
229503ac807SChaoyong He __rte_internal
230503ac807SChaoyong He void nfp_write_mac(struct nfp_hw *hw, uint8_t *mac);
231503ac807SChaoyong He 
232*87f5b35bSChaoyong He __rte_internal
233*87f5b35bSChaoyong He void nfp_enable_queues(struct nfp_hw *hw, uint16_t nb_rx_queues,
234*87f5b35bSChaoyong He 		uint16_t nb_tx_queues);
235*87f5b35bSChaoyong He 
236*87f5b35bSChaoyong He __rte_internal
237*87f5b35bSChaoyong He void nfp_disable_queues(struct nfp_hw *hw);
238*87f5b35bSChaoyong He 
239503ac807SChaoyong He #endif/* __NFP_COMMON_H__ */
240