Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2021 Marvell.
3 : : */
4 : :
5 : : #ifndef __ROC_MBOX_PRIV_H__
6 : : #define __ROC_MBOX_PRIV_H__
7 : :
8 : : #include <errno.h>
9 : : #include <stdbool.h>
10 : : #include <stdint.h>
11 : :
12 : : #define SZ_64K (64ULL * 1024ULL)
13 : : #define SZ_1K (1ULL * 1024ULL)
14 : : #define MBOX_SIZE SZ_64K
15 : :
16 : : /* AF/PF: PF initiated, PF/VF VF initiated */
17 : : #define MBOX_DOWN_RX_START 0
18 : : #define MBOX_DOWN_RX_SIZE (46 * SZ_1K)
19 : : #define MBOX_DOWN_TX_START (MBOX_DOWN_RX_START + MBOX_DOWN_RX_SIZE)
20 : : #define MBOX_DOWN_TX_SIZE (16 * SZ_1K)
21 : : /* AF/PF: AF initiated, PF/VF PF initiated */
22 : : #define MBOX_UP_RX_START (MBOX_DOWN_TX_START + MBOX_DOWN_TX_SIZE)
23 : : #define MBOX_UP_RX_SIZE SZ_1K
24 : : #define MBOX_UP_TX_START (MBOX_UP_RX_START + MBOX_UP_RX_SIZE)
25 : : #define MBOX_UP_TX_SIZE SZ_1K
26 : :
27 : : #if MBOX_UP_TX_SIZE + MBOX_UP_TX_START != MBOX_SIZE
28 : : #error "Incorrect mailbox area sizes"
29 : : #endif
30 : :
31 : : #define INTR_MASK(pfvfs) ((pfvfs < 64) ? (BIT_ULL(pfvfs) - 1) : (~0ull))
32 : :
33 : : #define MBOX_RSP_TIMEOUT 3000 /* Time to wait for mbox response in ms */
34 : :
35 : : #define MBOX_MSG_ALIGN 16 /* Align mbox msg start to 16bytes */
36 : :
37 : : /* Mailbox directions */
38 : : #define MBOX_DIR_AFPF 0 /* AF replies to PF */
39 : : #define MBOX_DIR_PFAF 1 /* PF sends messages to AF */
40 : : #define MBOX_DIR_PFVF 2 /* PF replies to VF */
41 : : #define MBOX_DIR_VFPF 3 /* VF sends messages to PF */
42 : : #define MBOX_DIR_AFPF_UP 4 /* AF sends messages to PF */
43 : : #define MBOX_DIR_PFAF_UP 5 /* PF replies to AF */
44 : : #define MBOX_DIR_PFVF_UP 6 /* PF sends messages to VF */
45 : : #define MBOX_DIR_VFPF_UP 7 /* VF replies to PF */
46 : :
47 : : struct mbox_dev {
48 : : void *mbase; /* This dev's mbox region */
49 : : plt_spinlock_t mbox_lock;
50 : : uint16_t msg_size; /* Total msg size to be sent */
51 : : uint16_t rsp_size; /* Total rsp size to be sure the reply is ok */
52 : : uint16_t num_msgs; /* No of msgs sent or waiting for response */
53 : : uint16_t msgs_acked; /* No of msgs for which response is received */
54 : : };
55 : :
56 : : struct mbox {
57 : : uintptr_t hwbase; /* Mbox region advertised by HW */
58 : : uintptr_t reg_base; /* CSR base for this dev */
59 : : uint64_t trigger; /* Trigger mbox notification */
60 : : uint16_t tr_shift; /* Mbox trigger shift */
61 : : uint64_t rx_start; /* Offset of Rx region in mbox memory */
62 : : uint64_t tx_start; /* Offset of Tx region in mbox memory */
63 : : uint16_t rx_size; /* Size of Rx region */
64 : : uint16_t tx_size; /* Size of Tx region */
65 : : uint16_t ndevs; /* The number of peers */
66 : : struct mbox_dev *dev;
67 : : uint64_t intr_offset; /* Offset to interrupt register */
68 : : uint32_t rsp_tmo;
69 : : };
70 : :
71 : : const char *mbox_id2name(uint16_t id);
72 : : int mbox_id2size(uint16_t id);
73 : : void mbox_reset(struct mbox *mbox, int devid);
74 : : int mbox_init(struct mbox *mbox, uintptr_t hwbase, uintptr_t reg_base, int direction, int ndevsi,
75 : : uint64_t intr_offset);
76 : : void mbox_fini(struct mbox *mbox);
77 : : void mbox_msg_send(struct mbox *mbox, int devid);
78 : : void mbox_msg_send_up(struct mbox *mbox, int devid);
79 : : bool mbox_wait_for_zero(struct mbox *mbox, int devid);
80 : : int mbox_wait_for_rsp(struct mbox *mbox, int devid);
81 : : int mbox_wait_for_rsp_tmo(struct mbox *mbox, int devid, uint32_t tmo);
82 : : int mbox_get_rsp(struct mbox *mbox, int devid, void **msg);
83 : : int mbox_get_rsp_tmo(struct mbox *mbox, int devid, void **msg, uint32_t tmo);
84 : : int mbox_get_availmem(struct mbox *mbox, int devid);
85 : : struct mbox_msghdr *mbox_alloc_msg_rsp(struct mbox *mbox, int devid, int size,
86 : : int size_rsp);
87 : :
88 : : static inline struct mbox_msghdr *
89 : : mbox_alloc_msg(struct mbox *mbox, int devid, int size)
90 : : {
91 : 0 : return mbox_alloc_msg_rsp(mbox, devid, size, 0);
92 : : }
93 : :
94 : : static inline void
95 : : mbox_req_init(uint16_t mbox_id, void *msghdr)
96 : : {
97 : : struct mbox_msghdr *hdr = msghdr;
98 : :
99 : 0 : hdr->sig = MBOX_REQ_SIG;
100 : 0 : hdr->ver = MBOX_VERSION;
101 : 0 : hdr->id = mbox_id;
102 : 0 : hdr->pcifunc = 0;
103 : : }
104 : :
105 : : static inline void
106 : : mbox_rsp_init(uint16_t mbox_id, void *msghdr)
107 : : {
108 : : struct mbox_msghdr *hdr = msghdr;
109 : :
110 : 0 : hdr->sig = MBOX_RSP_SIG;
111 : 0 : hdr->rc = -ETIMEDOUT;
112 [ # # # # ]: 0 : hdr->id = mbox_id;
113 : : }
114 : :
115 : : static inline bool
116 : : mbox_nonempty_nolock(struct mbox *mbox, int devid)
117 : : {
118 : 0 : struct mbox_dev *mdev = &mbox->dev[devid];
119 : : bool ret;
120 : :
121 [ # # # # : 0 : ret = mdev->num_msgs != 0 && mdev->msg_size != 0;
# # ]
122 : :
123 : : return ret;
124 : : }
125 : :
126 : : static inline int
127 : 0 : mbox_process(struct mbox *mbox)
128 : : {
129 : 0 : mbox_msg_send(mbox, 0);
130 : 0 : return mbox_get_rsp(mbox, 0, NULL);
131 : : }
132 : :
133 : : static inline int
134 : : mbox_process_msg(struct mbox *mbox, void **msg)
135 : : {
136 : 0 : mbox_msg_send(mbox, 0);
137 : 0 : return mbox_get_rsp(mbox, 0, msg);
138 : : }
139 : :
140 : : static inline int
141 : 0 : mbox_process_tmo(struct mbox *mbox, uint32_t tmo)
142 : : {
143 : 0 : mbox_msg_send(mbox, 0);
144 : 0 : return mbox_get_rsp_tmo(mbox, 0, NULL, tmo);
145 : : }
146 : :
147 : : static inline int
148 : : mbox_process_msg_tmo(struct mbox *mbox, void **msg, uint32_t tmo)
149 : : {
150 : : mbox_msg_send(mbox, 0);
151 : : return mbox_get_rsp_tmo(mbox, 0, msg, tmo);
152 : : }
153 : :
154 : : static inline struct mbox *
155 : : mbox_get(struct mbox *mbox)
156 : : {
157 : 0 : struct mbox_dev *mdev = &mbox->dev[0];
158 : 0 : plt_spinlock_lock(&mdev->mbox_lock);
159 : : return mbox;
160 : : }
161 : :
162 : : static inline void
163 : : mbox_put(struct mbox *mbox)
164 : : {
165 : 0 : struct mbox_dev *mdev = &mbox->dev[0];
166 : 0 : plt_spinlock_unlock(&mdev->mbox_lock);
167 : 0 : }
168 : :
169 : : int send_ready_msg(struct mbox *mbox, uint16_t *pf_func /* out */);
170 : : int reply_invalid_msg(struct mbox *mbox, int devid, uint16_t pf_func,
171 : : uint16_t id);
172 : :
173 : : #define M(_name, _id, _fn_name, _req_type, _rsp_type) \
174 : : static inline struct _req_type *mbox_alloc_msg_##_fn_name( \
175 : : struct mbox *mbox) \
176 : : { \
177 : : struct _req_type *req; \
178 : : req = (struct _req_type *)mbox_alloc_msg_rsp( \
179 : : mbox, 0, sizeof(struct _req_type), \
180 : : sizeof(struct _rsp_type)); \
181 : : if (!req) \
182 : : return NULL; \
183 : : req->hdr.sig = MBOX_REQ_SIG; \
184 : : req->hdr.id = _id; \
185 : : plt_mbox_dbg("id=0x%x (%s)", req->hdr.id, \
186 : : mbox_id2name(req->hdr.id)); \
187 : : return req; \
188 : : }
189 : :
190 [ # # ]: 0 : MBOX_MESSAGES
191 : : #undef M
192 : :
193 : : /* This is required for copy operations from device memory which do not work on
194 : : * addresses which are unaligned to 16B. This is because of specific
195 : : * optimizations to libc memcpy.
196 : : */
197 : : static inline volatile void *
198 : : mbox_memcpy(volatile void *d, const volatile void *s, size_t l)
199 : : {
200 : : const volatile uint8_t *sb;
201 : : volatile uint8_t *db;
202 : : size_t i;
203 : :
204 [ # # # # : 0 : if (!d || !s)
# # # # ]
205 : : return NULL;
206 : : db = (volatile uint8_t *)d;
207 : : sb = (const volatile uint8_t *)s;
208 [ # # # # : 0 : for (i = 0; i < l; i++)
# # # # #
# # # # #
# # ]
209 : 0 : db[i] = sb[i];
210 : : return d;
211 : : }
212 : :
213 : : /* This is required for memory operations from device memory which do not
214 : : * work on addresses which are unaligned to 16B. This is because of specific
215 : : * optimizations to libc memset.
216 : : */
217 : : static inline void
218 : : mbox_memset(volatile void *d, uint8_t val, size_t l)
219 : : {
220 : : volatile uint8_t *db;
221 : : size_t i = 0;
222 : :
223 [ # # ]: 0 : if (!d || !l)
224 : : return;
225 : : db = (volatile uint8_t *)d;
226 [ # # ]: 0 : for (i = 0; i < l; i++)
227 : 0 : db[i] = val;
228 : : }
229 : :
230 : : #endif /* __ROC_MBOX_PRIV_H__ */
|