Skip to content

Commit 9cac460

Browse files
committed
ref(drivers/mailbox): flatten mbox APIs to support protocol agnosticism
Signed-off-by: puranikvinit <kvp933.vinit@gmail.com>
1 parent 8db6270 commit 9cac460

2 files changed

Lines changed: 41 additions & 56 deletions

File tree

src/platform/drivers/mailbox/inc/drivers/k3_sec_proxy.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,6 @@ typedef enum {
6767
#define MBOX_K3_SEC_PROXY_DATA_START_OFFSET (0x4U)
6868
#define MBOX_K3_SEC_PROXY_DATA_END_OFFSET (0x3CU) /* completion trigger offset */
6969

70-
/**
71-
* @brief Structure representing a message to be sent or received via Secure
72-
* Proxy.
73-
*/
74-
typedef struct {
75-
size_t len; /**< Length of the message in bytes */
76-
uint32_t *buffer; /**< Pointer to the message data buffer */
77-
} mbox_k3_sec_proxy_msg;
78-
7970
/**
8071
* @brief Hardware configuration for a Secure Proxy instance.
8172
*/
@@ -167,21 +158,27 @@ typedef struct {
167158
extern mbox_k3_sec_proxy_desc sec_proxy_desc;
168159

169160
/**
170-
* @brief Verifies the status and configuration of a Secure Proxy thread.
161+
* @brief Verifies the status and configuration of a Secure Proxy thread before
162+
* a transaction.
163+
*
164+
* @desc This function checks for thread corruption, validates the thread's
165+
* configured direction (read/write) against its intended usage, and ensures the
166+
* message queue is not empty if reading.
171167
*
172168
* @param thread_id The ID of the thread to verify.
173169
* @param msg_drxn Expected message direction (MSG_DRXN_READ or MSG_DRXN_WRITE).
174170
*
175-
* @return int32_t STATUS_CODE_NO_ERROR on success, or respective error codes
176-
* on failure.
171+
* @return int32_t STATUS_CODE_NO_ERROR if the thread is valid and ready,
172+
* otherwise respective error codes.
177173
*/
178174
int32_t mbox_k3_sec_proxy_verify_thread(uint8_t thread_id, uint8_t msg_drxn);
179175

180176
/**
181177
* @brief Reads a message from a specific Secure Proxy thread.
182178
*
183179
* @param thread_id The ID of the thread to read from.
184-
* @param msg Pointer to the structure where the read message will be stored.
180+
* @param buffer Pointer to the buffer where the read message will be stored.
181+
* @param len Length of the message to read in bytes.
185182
*
186183
* @return int32_t STATUS_CODE_NO_ERROR on success, or respective error codes
187184
* on failure.
@@ -191,13 +188,14 @@ int32_t mbox_k3_sec_proxy_verify_thread(uint8_t thread_id, uint8_t msg_drxn);
191188
* for little-endian.
192189
* - byte-ordering logic for trailing bytes assumes LSB-first memory layout.
193190
*/
194-
int32_t mbox_k3_sec_proxy_read(uint8_t thread_id, mbox_k3_sec_proxy_msg *msg);
191+
int32_t mbox_k3_sec_proxy_read(uint8_t thread_id, void* buffer, size_t len);
195192

196193
/**
197194
* @brief Writes a message to a specific Secure Proxy thread.
198195
*
199196
* @param thread_id The ID of the thread to write to.
200-
* @param msg Pointer to the structure containing the message to send.
197+
* @param buffer Pointer to the buffer containing the message to send.
198+
* @param len Length of the message to send in bytes.
201199
*
202200
* @return int32_t STATUS_CODE_NO_ERROR on success, or an error code on
203201
* failure.
@@ -207,7 +205,7 @@ int32_t mbox_k3_sec_proxy_read(uint8_t thread_id, mbox_k3_sec_proxy_msg *msg);
207205
* for little-endian.
208206
* - byte-ordering logic for trailing bytes assumes LSB-first memory layout.
209207
*/
210-
int32_t mbox_k3_sec_proxy_write(uint8_t thread_id, mbox_k3_sec_proxy_msg *msg);
208+
int32_t mbox_k3_sec_proxy_write(uint8_t thread_id, void* buffer, size_t len);
211209

212210
/**
213211
* @brief Clears all pending messages from a Secure Proxy thread.

src/platform/drivers/mailbox/k3_sec_proxy.c

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,6 @@ static inline void write_reg(paddr_t addr, paddr_t offset, uint32_t value)
3030
*((volatile uint32_t*)((paddr_t)(addr + offset))) = value;
3131
}
3232

33-
/**
34-
* @brief Verifies the status and configuration of a Secure Proxy thread before
35-
* a transaction.
36-
*
37-
* @desc This function checks for thread corruption, validates the thread's
38-
* configured direction (read/write) against its intended usage, and ensures the
39-
* message queue is not empty if reading.
40-
*
41-
* @param thread_id The ID of the thread to verify.
42-
* @param msg_drxn Expected message direction (MSG_DRXN_READ or MSG_DRXN_WRITE).
43-
*
44-
* @return int32_t STATUS_CODE_NO_ERROR if the thread is valid and ready,
45-
* otherwise respective error codes.
46-
*/
4733
int32_t mbox_k3_sec_proxy_verify_thread(uint8_t thread_id, uint8_t msg_drxn)
4834
{
4935
paddr_t thread_rt_base =
@@ -108,7 +94,7 @@ int32_t mbox_k3_sec_proxy_verify_thread(uint8_t thread_id, uint8_t msg_drxn)
10894
* | not reached yet | +------------------------------------+
10995
* +-------------------------+
11096
*/
111-
int32_t mbox_k3_sec_proxy_read(uint8_t thread_id, mbox_k3_sec_proxy_msg* msg)
97+
int32_t mbox_k3_sec_proxy_read(uint8_t thread_id, void* buffer, size_t len)
11298
{
11399
/* verify thread status */
114100
int32_t read_status = mbox_k3_sec_proxy_verify_thread(thread_id, MSG_DRXN_READ);
@@ -123,20 +109,19 @@ int32_t mbox_k3_sec_proxy_read(uint8_t thread_id, mbox_k3_sec_proxy_msg* msg)
123109

124110
/* read whole words first */
125111
uint32_t word_iterator;
126-
size_t num_words = msg->len / sizeof(uint32_t);
112+
size_t num_words = len / sizeof(uint32_t);
127113
for (word_iterator = 0; word_iterator < num_words; word_iterator++) {
128-
((uint32_t*)msg->buffer)[word_iterator] =
129-
read_reg(data_reg, word_iterator * sizeof(uint32_t));
114+
((uint32_t*)buffer)[word_iterator] = read_reg(data_reg, word_iterator * sizeof(uint32_t));
130115
}
131116

132117
/* read remaining bytes */
133-
uint32_t trail_bytes = msg->len % sizeof(uint32_t);
118+
uint32_t trail_bytes = len % sizeof(uint32_t);
134119
if (0 != trail_bytes) {
135120
uint32_t data_trail = read_reg(data_reg, word_iterator++ * sizeof(uint32_t));
136121

137-
size_t trail_iterator = msg->len - trail_bytes;
122+
size_t trail_iterator = len - trail_bytes;
138123
while (trail_bytes--) {
139-
((uint8_t*)msg->buffer)[trail_iterator++] = (uint8_t)(data_trail & 0xFFU);
124+
((uint8_t*)buffer)[trail_iterator++] = (uint8_t)(data_trail & 0xFFU);
140125
data_trail >>= 8;
141126
}
142127
}
@@ -183,7 +168,7 @@ int32_t mbox_k3_sec_proxy_read(uint8_t thread_id, mbox_k3_sec_proxy_msg* msg)
183168
* | end offset (trigger) | +------------------------------------+
184169
* +-------------------------+
185170
*/
186-
int32_t mbox_k3_sec_proxy_write(uint8_t thread_id, mbox_k3_sec_proxy_msg* msg)
171+
int32_t mbox_k3_sec_proxy_write(uint8_t thread_id, void* buffer, size_t len)
187172
{
188173
/* verify thread status */
189174
int32_t write_status = mbox_k3_sec_proxy_verify_thread(thread_id, MSG_DRXN_WRITE);
@@ -193,7 +178,7 @@ int32_t mbox_k3_sec_proxy_write(uint8_t thread_id, mbox_k3_sec_proxy_msg* msg)
193178
}
194179

195180
/* msg len check */
196-
if (msg->len > sec_proxy_desc.thread_inst.max_msg_size) {
181+
if (len > sec_proxy_desc.thread_inst.max_msg_size) {
197182
ERROR("secure_proxy_thread_%d msg len exceeds limit", thread_id);
198183
return STATUS_CODE_INVALID_MSG_LEN;
199184
}
@@ -204,20 +189,19 @@ int32_t mbox_k3_sec_proxy_write(uint8_t thread_id, mbox_k3_sec_proxy_msg* msg)
204189

205190
/* write whole words first */
206191
uint32_t word_iterator;
207-
size_t num_words = msg->len / sizeof(uint32_t);
192+
size_t num_words = len / sizeof(uint32_t);
208193
for (word_iterator = 0; word_iterator < num_words; word_iterator++) {
209-
write_reg(data_reg, word_iterator * sizeof(uint32_t),
210-
((uint32_t*)msg->buffer)[word_iterator]);
194+
write_reg(data_reg, word_iterator * sizeof(uint32_t), ((uint32_t*)buffer)[word_iterator]);
211195
}
212196

213197
/* write remaining bytes */
214-
uint32_t trail_bytes = msg->len % sizeof(uint32_t);
198+
uint32_t trail_bytes = len % sizeof(uint32_t);
215199
if (0 != trail_bytes) {
216200
uint32_t data_trail = 0;
217201

218-
size_t trail_iterator = msg->len - trail_bytes;
202+
size_t trail_iterator = len - trail_bytes;
219203
for (uint32_t i = 0; i < trail_bytes; i++) {
220-
data_trail |= (uint32_t)((uint8_t*)msg->buffer)[trail_iterator++] << (i * 8);
204+
data_trail |= (uint32_t)((uint8_t*)buffer)[trail_iterator++] << (i * 8);
221205
}
222206

223207
write_reg(data_reg, word_iterator++ * sizeof(uint32_t), data_trail);
@@ -273,9 +257,6 @@ int32_t mbox_k3_sec_proxy_probe(uint8_t thread_id)
273257
sec_proxy_desc.thread_inst.scfg_base + MBOX_K3_SEC_PROXY_THREAD_OFFSET(thread_id);
274258
uint32_t config = read_reg(thread_scfg_base, MBOX_K3_SEC_PROXY_SCFG_THREAD_CTRL_OFFSET);
275259

276-
paddr_t thread_rt_base =
277-
sec_proxy_desc.thread_inst.rt_base + MBOX_K3_SEC_PROXY_THREAD_OFFSET(thread_id);
278-
279260
uint8_t hw_host = (config >> 8) & 0xFF;
280261
uint8_t expected_host = sec_proxy_desc.sec_proxy_thread_desc[thread_id].host_id;
281262

@@ -287,19 +268,25 @@ int32_t mbox_k3_sec_proxy_probe(uint8_t thread_id)
287268
}
288269

289270
/* [step-2] verify if thread is clean */
290-
int32_t probe_status = mbox_k3_sec_proxy_verify_thread(thread_id, MSG_DRXN_WRITE);
291-
if (STATUS_CODE_NO_ERROR != probe_status) {
292-
INFO("sec_proxy_thread_%d probe failed (error_id=%d)", thread_id, probe_status);
293-
return probe_status;
294-
}
271+
int32_t probe_status = mbox_k3_sec_proxy_verify_thread(thread_id,
272+
sec_proxy_desc.sec_proxy_thread_desc[thread_id].msg_drxn);
295273

296-
if (0 !=
297-
(read_reg(thread_rt_base, MBOX_K3_SEC_PROXY_RT_THREAD_STATUS_OFFSET) &
298-
MBOX_K3_SEC_PROXY_RT_STATUS_CUR_CNT_MASK)) {
274+
if (MSG_DRXN_READ == sec_proxy_desc.sec_proxy_thread_desc[thread_id].msg_drxn &&
275+
STATUS_CODE_NO_ERROR == probe_status) {
299276
ERROR("secure_proxy_thread_%d probe failed (message queue not clean)", thread_id);
300277
return STATUS_CODE_DIRTY_HANDOFF;
301278
}
302279

280+
if (MSG_DRXN_READ == sec_proxy_desc.sec_proxy_thread_desc[thread_id].msg_drxn &&
281+
STATUS_CODE_NO_DATA == probe_status) {
282+
probe_status = STATUS_CODE_NO_ERROR;
283+
}
284+
285+
if (STATUS_CODE_NO_ERROR != probe_status) {
286+
INFO("sec_proxy_thread_%d probe failed (error_id=%d)", thread_id, probe_status);
287+
return probe_status;
288+
}
289+
303290
/* [step-3] check pipeline health by pinging sysfw
304291
*
305292
* @notes

0 commit comments

Comments
 (0)