Skip to content

Commit 2149ec8

Browse files
MrVanJassi Brar
authored andcommitted
mailbox: Use guard/scoped_guard for spinlock
Use guard and scoped_guard for chan->lock and mbox->poll_hrt_lock to simplify code. Signed-off-by: Peng Fan <[email protected]> Signed-off-by: Jassi Brar <[email protected]>
1 parent 16da9a6 commit 2149ec8

File tree

1 file changed

+45
-60
lines changed

1 file changed

+45
-60
lines changed

drivers/mailbox/mailbox.c

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,12 @@ static DEFINE_MUTEX(con_mutex);
2525
static int add_to_rbuf(struct mbox_chan *chan, void *mssg)
2626
{
2727
int idx;
28-
unsigned long flags;
2928

30-
spin_lock_irqsave(&chan->lock, flags);
29+
guard(spinlock_irqsave)(&chan->lock);
3130

3231
/* See if there is any space left */
33-
if (chan->msg_count == MBOX_TX_QUEUE_LEN) {
34-
spin_unlock_irqrestore(&chan->lock, flags);
32+
if (chan->msg_count == MBOX_TX_QUEUE_LEN)
3533
return -ENOBUFS;
36-
}
3734

3835
idx = chan->msg_free;
3936
chan->msg_data[idx] = mssg;
@@ -44,60 +41,53 @@ static int add_to_rbuf(struct mbox_chan *chan, void *mssg)
4441
else
4542
chan->msg_free++;
4643

47-
spin_unlock_irqrestore(&chan->lock, flags);
48-
4944
return idx;
5045
}
5146

5247
static void msg_submit(struct mbox_chan *chan)
5348
{
5449
unsigned count, idx;
55-
unsigned long flags;
5650
void *data;
5751
int err = -EBUSY;
5852

59-
spin_lock_irqsave(&chan->lock, flags);
53+
scoped_guard(spinlock_irqsave, &chan->lock) {
54+
if (!chan->msg_count || chan->active_req)
55+
break;
6056

61-
if (!chan->msg_count || chan->active_req)
62-
goto exit;
63-
64-
count = chan->msg_count;
65-
idx = chan->msg_free;
66-
if (idx >= count)
67-
idx -= count;
68-
else
69-
idx += MBOX_TX_QUEUE_LEN - count;
57+
count = chan->msg_count;
58+
idx = chan->msg_free;
59+
if (idx >= count)
60+
idx -= count;
61+
else
62+
idx += MBOX_TX_QUEUE_LEN - count;
7063

71-
data = chan->msg_data[idx];
64+
data = chan->msg_data[idx];
7265

73-
if (chan->cl->tx_prepare)
74-
chan->cl->tx_prepare(chan->cl, data);
75-
/* Try to submit a message to the MBOX controller */
76-
err = chan->mbox->ops->send_data(chan, data);
77-
if (!err) {
78-
chan->active_req = data;
79-
chan->msg_count--;
66+
if (chan->cl->tx_prepare)
67+
chan->cl->tx_prepare(chan->cl, data);
68+
/* Try to submit a message to the MBOX controller */
69+
err = chan->mbox->ops->send_data(chan, data);
70+
if (!err) {
71+
chan->active_req = data;
72+
chan->msg_count--;
73+
}
8074
}
81-
exit:
82-
spin_unlock_irqrestore(&chan->lock, flags);
8375

8476
if (!err && (chan->txdone_method & TXDONE_BY_POLL)) {
8577
/* kick start the timer immediately to avoid delays */
86-
spin_lock_irqsave(&chan->mbox->poll_hrt_lock, flags);
87-
hrtimer_start(&chan->mbox->poll_hrt, 0, HRTIMER_MODE_REL);
88-
spin_unlock_irqrestore(&chan->mbox->poll_hrt_lock, flags);
78+
scoped_guard(spinlock_irqsave, &chan->mbox->poll_hrt_lock)
79+
hrtimer_start(&chan->mbox->poll_hrt, 0, HRTIMER_MODE_REL);
8980
}
9081
}
9182

9283
static void tx_tick(struct mbox_chan *chan, int r)
9384
{
94-
unsigned long flags;
9585
void *mssg;
9686

97-
spin_lock_irqsave(&chan->lock, flags);
98-
mssg = chan->active_req;
99-
chan->active_req = NULL;
100-
spin_unlock_irqrestore(&chan->lock, flags);
87+
scoped_guard(spinlock_irqsave, &chan->lock) {
88+
mssg = chan->active_req;
89+
chan->active_req = NULL;
90+
}
10191

10292
/* Submit next message */
10393
msg_submit(chan);
@@ -119,7 +109,6 @@ static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)
119109
container_of(hrtimer, struct mbox_controller, poll_hrt);
120110
bool txdone, resched = false;
121111
int i;
122-
unsigned long flags;
123112

124113
for (i = 0; i < mbox->num_chans; i++) {
125114
struct mbox_chan *chan = &mbox->chans[i];
@@ -134,10 +123,10 @@ static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)
134123
}
135124

136125
if (resched) {
137-
spin_lock_irqsave(&mbox->poll_hrt_lock, flags);
138-
if (!hrtimer_is_queued(hrtimer))
139-
hrtimer_forward_now(hrtimer, ms_to_ktime(mbox->txpoll_period));
140-
spin_unlock_irqrestore(&mbox->poll_hrt_lock, flags);
126+
scoped_guard(spinlock_irqsave, &mbox->poll_hrt_lock) {
127+
if (!hrtimer_is_queued(hrtimer))
128+
hrtimer_forward_now(hrtimer, ms_to_ktime(mbox->txpoll_period));
129+
}
141130

142131
return HRTIMER_RESTART;
143132
}
@@ -319,25 +308,23 @@ EXPORT_SYMBOL_GPL(mbox_flush);
319308
static int __mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl)
320309
{
321310
struct device *dev = cl->dev;
322-
unsigned long flags;
323311
int ret;
324312

325313
if (chan->cl || !try_module_get(chan->mbox->dev->driver->owner)) {
326314
dev_err(dev, "%s: mailbox not free\n", __func__);
327315
return -EBUSY;
328316
}
329317

330-
spin_lock_irqsave(&chan->lock, flags);
331-
chan->msg_free = 0;
332-
chan->msg_count = 0;
333-
chan->active_req = NULL;
334-
chan->cl = cl;
335-
init_completion(&chan->tx_complete);
336-
337-
if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
338-
chan->txdone_method = TXDONE_BY_ACK;
318+
scoped_guard(spinlock_irqsave, &chan->lock) {
319+
chan->msg_free = 0;
320+
chan->msg_count = 0;
321+
chan->active_req = NULL;
322+
chan->cl = cl;
323+
init_completion(&chan->tx_complete);
339324

340-
spin_unlock_irqrestore(&chan->lock, flags);
325+
if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
326+
chan->txdone_method = TXDONE_BY_ACK;
327+
}
341328

342329
if (chan->mbox->ops->startup) {
343330
ret = chan->mbox->ops->startup(chan);
@@ -465,22 +452,20 @@ EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
465452
*/
466453
void mbox_free_channel(struct mbox_chan *chan)
467454
{
468-
unsigned long flags;
469-
470455
if (!chan || !chan->cl)
471456
return;
472457

473458
if (chan->mbox->ops->shutdown)
474459
chan->mbox->ops->shutdown(chan);
475460

476461
/* The queued TX requests are simply aborted, no callbacks are made */
477-
spin_lock_irqsave(&chan->lock, flags);
478-
chan->cl = NULL;
479-
chan->active_req = NULL;
480-
if (chan->txdone_method == TXDONE_BY_ACK)
481-
chan->txdone_method = TXDONE_BY_POLL;
462+
scoped_guard(spinlock_irqsave, &chan->lock) {
463+
chan->cl = NULL;
464+
chan->active_req = NULL;
465+
if (chan->txdone_method == TXDONE_BY_ACK)
466+
chan->txdone_method = TXDONE_BY_POLL;
467+
}
482468

483-
spin_unlock_irqrestore(&chan->lock, flags);
484469
module_put(chan->mbox->dev->driver->owner);
485470
}
486471
EXPORT_SYMBOL_GPL(mbox_free_channel);

0 commit comments

Comments
 (0)