All of lore.kernel.org
 help / color / mirror / Atom feed
From: Orson Zhai <orsonzhai@gmail.com>
To: Baolin Wang <baolin.wang7@gmail.com>
Cc: Rob Herring <robh+dt@kernel.org>,
	Jassi Brar <jassisinghbrar@gmail.com>,
	Chunyan Zhang <zhang.lyra@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	Devicetree List <devicetree@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Haidong Yao <haidong.yao@unisoc.com>,
	Orson Zhai <orson.zhai@unisoc.com>
Subject: Re: [PATCH 1/3] mailbox: sprd: Introduce refcnt when clients requests/free channels
Date: Tue, 9 Feb 2021 11:28:38 +0800	[thread overview]
Message-ID: <20210209032838.GA24248@lenovo> (raw)
In-Reply-To: <CADBw62qiUG2dunB_i1iOp_srkAJP4CrVJX9mU25no++_b10hpg@mail.gmail.com>

On Mon, Feb 08, 2021 at 10:06:47PM +0800, Baolin Wang wrote:
> Hi Orson,
> 
> On Mon, Feb 8, 2021 at 7:52 PM Orson Zhai <orsonzhai@gmail.com> wrote:
> >
> > From: Orson Zhai <orson.zhai@unisoc.com>
> >
> > Unisoc mailbox has no way to be enabled/disabled for any single channel.
> > They can only be set to startup or shutdown as a whole device at same time.
> >
> > Add a variable to count references to avoid mailbox FIFO being reset
> > unexpectedly when clients are requesting or freeing channels.
> >
> > Also add a lock to dismiss possible conflicts from register r/w in
> > different startup or shutdown threads.
> >
> > Fixes: ca27fc26cd22 ("mailbox: sprd: Add Spreadtrum mailbox driver")
> > Signed-off-by: Orson Zhai <orson.zhai@unisoc.com>
> > ---
> >  drivers/mailbox/sprd-mailbox.c | 38 +++++++++++++++++++++++++-------------
> >  1 file changed, 25 insertions(+), 13 deletions(-)
> >
> > diff --git a/drivers/mailbox/sprd-mailbox.c b/drivers/mailbox/sprd-mailbox.c
> > index f6fab24..e606f52 100644
> > --- a/drivers/mailbox/sprd-mailbox.c
> > +++ b/drivers/mailbox/sprd-mailbox.c
> > @@ -60,6 +60,8 @@ struct sprd_mbox_priv {
> >         struct clk              *clk;
> >         u32                     outbox_fifo_depth;
> >
> > +       struct mutex            lock;
> > +       u32                     refcnt;
> >         struct mbox_chan        chan[SPRD_MBOX_CHAN_MAX];
> >  };
> >
> > @@ -215,18 +217,22 @@ static int sprd_mbox_startup(struct mbox_chan *chan)
> >         struct sprd_mbox_priv *priv = to_sprd_mbox_priv(chan->mbox);
> >         u32 val;
> >
> > -       /* Select outbox FIFO mode and reset the outbox FIFO status */
> > -       writel(0x0, priv->outbox_base + SPRD_MBOX_FIFO_RST);
> > +       mutex_lock(&priv->lock);
> > +       if (priv->refcnt++ == 0) {
> > +               /* Select outbox FIFO mode and reset the outbox FIFO status */
> > +               writel(0x0, priv->outbox_base + SPRD_MBOX_FIFO_RST);
> >
> > -       /* Enable inbox FIFO overflow and delivery interrupt */
> > -       val = readl(priv->inbox_base + SPRD_MBOX_IRQ_MSK);
> > -       val &= ~(SPRD_INBOX_FIFO_OVERFLOW_IRQ | SPRD_INBOX_FIFO_DELIVER_IRQ);
> > -       writel(val, priv->inbox_base + SPRD_MBOX_IRQ_MSK);
> > +               /* Enable inbox FIFO overflow and delivery interrupt */
> > +               val = readl(priv->inbox_base + SPRD_MBOX_IRQ_MSK);
> > +               val &= ~(SPRD_INBOX_FIFO_OVERFLOW_IRQ | SPRD_INBOX_FIFO_DELIVER_IRQ);
> > +               writel(val, priv->inbox_base + SPRD_MBOX_IRQ_MSK);
> >
> > -       /* Enable outbox FIFO not empty interrupt */
> > -       val = readl(priv->outbox_base + SPRD_MBOX_IRQ_MSK);
> > -       val &= ~SPRD_OUTBOX_FIFO_NOT_EMPTY_IRQ;
> > -       writel(val, priv->outbox_base + SPRD_MBOX_IRQ_MSK);
> > +               /* Enable outbox FIFO not empty interrupt */
> > +               val = readl(priv->outbox_base + SPRD_MBOX_IRQ_MSK);
> > +               val &= ~SPRD_OUTBOX_FIFO_NOT_EMPTY_IRQ;
> > +               writel(val, priv->outbox_base + SPRD_MBOX_IRQ_MSK);
> > +       }
> > +       mutex_unlock(&priv->lock);
> 
> I think using the atomic_add/sub_and_test() related APIs can remove
> the mutex lock.

Yes, atomic could make refcnt itself safe. But mutex lock is to make whole processing of
reading/writing registers safe.

Consider case like this:

  channel #1             channel #2
-------------------------------------
   startup
   .....
   shutdown               startup
     |-refcnt==0            |
     |                      |-retcnt+1
     |                      |-enable mailbox
     |-disable mailbox 

Mailbox will be wrongly disabled after client requests channel #2's startup.

> 
> >
> >         return 0;
> >  }
> > @@ -235,9 +241,13 @@ static void sprd_mbox_shutdown(struct mbox_chan *chan)
> >  {
> >         struct sprd_mbox_priv *priv = to_sprd_mbox_priv(chan->mbox);
> >
> > -       /* Disable inbox & outbox interrupt */
> > -       writel(SPRD_INBOX_FIFO_IRQ_MASK, priv->inbox_base + SPRD_MBOX_IRQ_MSK);
> > -       writel(SPRD_OUTBOX_FIFO_IRQ_MASK, priv->outbox_base + SPRD_MBOX_IRQ_MSK);
> > +       mutex_lock(&priv->lock);
> > +       if (--priv->refcnt == 0) {
> > +               /* Disable inbox & outbox interrupt */
> > +               writel(SPRD_INBOX_FIFO_IRQ_MASK, priv->inbox_base + SPRD_MBOX_IRQ_MSK);
> > +               writel(SPRD_OUTBOX_FIFO_IRQ_MASK, priv->outbox_base + SPRD_MBOX_IRQ_MSK);
> > +       }
> > +       mutex_unlock(&priv->lock);
> >  }
> >
> >  static const struct mbox_chan_ops sprd_mbox_ops = {
> > @@ -266,6 +276,8 @@ static int sprd_mbox_probe(struct platform_device *pdev)
> >                 return -ENOMEM;
> >
> >         priv->dev = dev;
> > +       priv->refcnt = 0;
> 
> No need to do this, the priv structure is already cleared to 0.

Right, will remove at next version.

Thanks for reviewing!

-Orson

> 
> > +       mutex_init(&priv->lock);
> >
> >         /*
> >          * The Spreadtrum mailbox uses an inbox to send messages to the target
> > --
> > 2.7.4
> >
> 
> 
> -- 
> Baolin Wang

  reply	other threads:[~2021-02-09  3:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-08 11:51 [PATCH 1/3] mailbox: sprd: Introduce refcnt when clients requests/free channels Orson Zhai
2021-02-08 11:51 ` [PATCH 2/3] mailbox: sprd: Add supplementary inbox support Orson Zhai
2021-02-08 14:27   ` Baolin Wang
2021-02-09  4:08     ` Orson Zhai
2021-02-09 10:57       ` Baolin Wang
2021-02-08 11:51 ` [PATCH 3/3] dt-bindings: mailbox: Add interrupt-names to SPRD mailbox Orson Zhai
2021-02-08 16:55   ` Rob Herring
2021-02-08 14:06 ` [PATCH 1/3] mailbox: sprd: Introduce refcnt when clients requests/free channels Baolin Wang
2021-02-09  3:28   ` Orson Zhai [this message]
2021-02-09 10:52     ` Baolin Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210209032838.GA24248@lenovo \
    --to=orsonzhai@gmail.com \
    --cc=baolin.wang7@gmail.com \
    --cc=broonie@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=haidong.yao@unisoc.com \
    --cc=jassisinghbrar@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=orson.zhai@unisoc.com \
    --cc=robh+dt@kernel.org \
    --cc=zhang.lyra@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.