[PATCH 4/7] mailbox: Simplify circular queue math with mod arithmetic

From: Douglas Anderson

Date: Tue Jul 14 2026 - 18:29:18 EST


The mailbox core keeps a circular queue of messages waiting to be
sent. It keeps track of the index of the next free entry in the queue
and the number of entries in the queue.

Given the index of the next free entry and the number of entries in
the queue (count), the index of the first entry in the queue can
therefore be found by starting at the next free entry and going
backwards in the queue by "count" entries. This is succinctly
expressed by the following math, which handles the circular queue
wraparound case:

first_idx = (next_free_idx + MBOX_TX_QUEUE_LEN - count) %
MBOX_TX_QUEUE_LEN;

Currently the mailbox core doesn't use that math and handles the
circular queue wraparound with an "if" test. Replace the code with the
equivalent math.

This is intended to be a no-op change and just code cleanup.

Signed-off-by: Douglas Anderson <dianders@xxxxxxxxxxxx>
---

drivers/mailbox/mailbox.c | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/drivers/mailbox/mailbox.c b/drivers/mailbox/mailbox.c
index 692087d461a5..d99a08652ef1 100644
--- a/drivers/mailbox/mailbox.c
+++ b/drivers/mailbox/mailbox.c
@@ -56,11 +56,7 @@ static void msg_submit(struct mbox_chan *chan)
break;

count = chan->msg_count;
- idx = chan->msg_free;
- if (idx >= count)
- idx -= count;
- else
- idx += MBOX_TX_QUEUE_LEN - count;
+ idx = (chan->msg_free + MBOX_TX_QUEUE_LEN - count) % MBOX_TX_QUEUE_LEN;

data = chan->msg_data[idx];

--
2.55.0.141.g00534a21ce-goog