The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Andrew Jeffery <andrew@codeconstruct.com.au>
To: Jammy Huang <jammy_huang@aspeedtech.com>,
	jassisinghbrar@gmail.com,  robh@kernel.org, krzk+dt@kernel.org,
	conor+dt@kernel.org, joel@jms.id.au,
	 linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
	 linux-arm-kernel@lists.infradead.org,
	linux-aspeed@lists.ozlabs.org
Subject: Re: [PATCH v4 2/2] mailbox: aspeed: add mailbox driver for AST27XX series SoC
Date: Wed, 25 Jun 2025 10:42:40 +0930	[thread overview]
Message-ID: <c1820a08930576d19c107d8e007acb21cae07a4d.camel@codeconstruct.com.au> (raw)
In-Reply-To: <20250623024456.2068370-3-jammy_huang@aspeedtech.com>

On Mon, 2025-06-23 at 10:44 +0800, Jammy Huang wrote:
> Add mailbox controller driver for AST27XX SoCs, which provides
> independent tx/rx mailbox between different processors. There are 4
> channels for each tx/rx mailbox and each channel has an 32-byte FIFO.
> 
> Signed-off-by: Jammy Huang <jammy_huang@aspeedtech.com>
> ---
>  drivers/mailbox/Kconfig           |   8 ++
>  drivers/mailbox/Makefile          |   2 +
>  drivers/mailbox/ast2700-mailbox.c | 230 ++++++++++++++++++++++++++++++
>  3 files changed, 240 insertions(+)
>  create mode 100644 drivers/mailbox/ast2700-mailbox.c
> 
> diff --git a/drivers/mailbox/Kconfig b/drivers/mailbox/Kconfig
> index 68eeed660a4a..1c38cd570091 100644
> --- a/drivers/mailbox/Kconfig
> +++ b/drivers/mailbox/Kconfig
> @@ -340,4 +340,12 @@ config THEAD_TH1520_MBOX
>           kernel is running, and E902 core used for power management among other
>           things.
>  
> +config AST2700_MBOX
> +       tristate "ASPEED AST2700 IPC driver"
> +       depends on ARCH_ASPEED || COMPILE_TEST
> +       help
> +         Mailbox driver implementation for ASPEED AST27XX SoCs. This driver
> +         can be used to send message between different processors in SoC.
> +         The driver provides mailbox support for sending interrupts to the
> +         clients. Say Y here if you want to build this driver.
>  endif
> diff --git a/drivers/mailbox/Makefile b/drivers/mailbox/Makefile
> index 13a3448b3271..9a9add9a7548 100644
> --- a/drivers/mailbox/Makefile
> +++ b/drivers/mailbox/Makefile
> @@ -72,3 +72,5 @@ obj-$(CONFIG_QCOM_CPUCP_MBOX) += qcom-cpucp-mbox.o
>  obj-$(CONFIG_QCOM_IPCC)                += qcom-ipcc.o
>  
>  obj-$(CONFIG_THEAD_TH1520_MBOX)        += mailbox-th1520.o
> +
> +obj-$(CONFIG_AST2700_MBOX)     += ast2700-mailbox.o
> diff --git a/drivers/mailbox/ast2700-mailbox.c b/drivers/mailbox/ast2700-mailbox.c
> new file mode 100644
> index 000000000000..64638ac67e7a
> --- /dev/null
> +++ b/drivers/mailbox/ast2700-mailbox.c
> @@ -0,0 +1,230 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright Aspeed Technology Inc. (C) 2025. All rights reserved
> + */
> +
> +#include <linux/interrupt.h>
> +#include <linux/io.h>
> +#include <linux/iopoll.h>
> +#include <linux/kernel.h>
> +#include <linux/mailbox_controller.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
> +
> +/* Each bit in the register represents an IPC ID */
> +#define IPCR_TX_TRIG           0x00
> +#define IPCR_TX_ENABLE         0x04
> +#define IPCR_RX_ENABLE         0x104
> +#define IPCR_TX_STATUS         0x08
> +#define IPCR_RX_STATUS         0x108
> +#define  RX_IRQ(n)             BIT(0 + 1 * (n))

Seems overly complex?

> +#define  RX_IRQ_MASK           0xf
> +#define IPCR_TX_DATA           0x10
> +#define IPCR_RX_DATA           0x110
> +
> +struct ast2700_mbox_data {
> +       u8 num_chans;
> +       u8 msg_size;
> +};
> +
> +struct ast2700_mbox {
> +       struct mbox_controller mbox;
> +       const struct ast2700_mbox_data *drv_data;
> +       void __iomem *regs;
> +};
> +
> +static inline int ch_num(struct mbox_chan *chan)
> +{
> +       return chan - chan->mbox->chans;
> +}
> +
> +static inline int ast2700_mbox_tx_done(struct ast2700_mbox *mb, int idx)
> +{
> +       return !(readl(mb->regs + IPCR_TX_STATUS) & BIT(idx));
> +}
> +
> +static irqreturn_t ast2700_mbox_irq(int irq, void *p)
> +{
> +       struct ast2700_mbox *mb = p;
> +       void __iomem *data_reg;
> +       int num_words;
> +       u32 *word_data;
> +       u32 status;
> +       int n;
> +
> +       /* Only examine channels that are currently enabled. */
> +       status = readl(mb->regs + IPCR_RX_ENABLE) &
> +                readl(mb->regs + IPCR_RX_STATUS);
> +
> +       if (!(status & RX_IRQ_MASK))
> +               return IRQ_NONE;
> +
> +       for (n = 0; n < mb->mbox.num_chans; ++n) {
> +               struct mbox_chan *chan = &mb->mbox.chans[n];
> +
> +               if (!(status & RX_IRQ(n)))
> +                       continue;
> +
> +               /* Read the message data */
> +               for (data_reg = mb->regs + IPCR_RX_DATA + mb->drv_data->msg_size * n,
> +                    word_data = chan->con_priv,
> +                    num_words = (mb->drv_data->msg_size / sizeof(u32));
> +                    num_words;
> +                    num_words--, data_reg += sizeof(u32), word_data++)
> +                       *word_data = readl(data_reg);
> +
> +               mbox_chan_received_data(chan, chan->con_priv);
> +
> +               /* The IRQ can be cleared only once the FIFO is empty. */
> +               writel(RX_IRQ(n), mb->regs + IPCR_RX_STATUS);
> +       }
> +
> +       return IRQ_HANDLED;
> +}
> +
> +static int ast2700_mbox_send_data(struct mbox_chan *chan, void *data)
> +{
> +       struct ast2700_mbox *mb = dev_get_drvdata(chan->mbox->dev);
> +       void __iomem *data_reg;
> +       u32 *word_data;
> +       int num_words;
> +       int idx = ch_num(chan);
> +
> +       if (!(readl(mb->regs + IPCR_TX_ENABLE) & BIT(idx))) {
> +               dev_warn(mb->mbox.dev, "%s: Ch-%d not enabled yet\n", __func__, idx);
> +               return -EBUSY;
> +       }
> +
> +       if (!(ast2700_mbox_tx_done(mb, idx))) {
> +               dev_warn(mb->mbox.dev, "%s: Ch-%d last data has not finished\n", __func__, idx);
> +               return -EBUSY;
> +       }
> +
> +       /* Write the message data */
> +       for (data_reg = mb->regs + IPCR_TX_DATA + mb->drv_data->msg_size * idx,
> +            word_data = (u32 *)data,
> +            num_words = (mb->drv_data->msg_size / sizeof(u32));
> +            num_words;
> +            num_words--, data_reg += sizeof(u32), word_data++)
> +               writel(*word_data, data_reg);
> +
> +       writel(BIT(idx), mb->regs + IPCR_TX_TRIG);
> +       dev_dbg(mb->mbox.dev, "%s: Ch-%d sent\n", __func__, idx);
> +
> +       return 0;
> +}
> +
> +static int ast2700_mbox_startup(struct mbox_chan *chan)
> +{
> +       struct ast2700_mbox *mb = dev_get_drvdata(chan->mbox->dev);
> +       int idx = ch_num(chan);
> +       void __iomem *reg = mb->regs + IPCR_RX_ENABLE;
> +
> +       writel(readl(reg) | BIT(idx), reg);

It seems ->startup is called with the channel spinlock held, but as
each channel is independent, the RMW sequence here could lead to
unexpected results?

I expect you need a spinlock to protect against concurrent changes to
channel state.

> +
> +       return 0;
> +}
> +
> +static void ast2700_mbox_shutdown(struct mbox_chan *chan)
> +{
> +       struct ast2700_mbox *mb = dev_get_drvdata(chan->mbox->dev);
> +       int idx = ch_num(chan);
> +       void __iomem *reg = mb->regs + IPCR_RX_ENABLE;
> +
> +       writel(readl(reg) & ~BIT(idx), reg);

See comment above.

> +}
> +
> +static bool ast2700_mbox_last_tx_done(struct mbox_chan *chan)
> +{
> +       struct ast2700_mbox *mb = dev_get_drvdata(chan->mbox->dev);
> +       int idx = ch_num(chan);
> +
> +       return ast2700_mbox_tx_done(mb, idx) ? true : false;

The ternary expression is unnecessary, we can instead do:

   return !!ast2700_mbox_tx_done(mb, idx);
   
> +}
> +
> +static const struct mbox_chan_ops ast2700_mbox_chan_ops = {
> +       .send_data      = ast2700_mbox_send_data,
> +       .startup        = ast2700_mbox_startup,
> +       .shutdown       = ast2700_mbox_shutdown,
> +       .last_tx_done   = ast2700_mbox_last_tx_done,
> +};
> +
> +static int ast2700_mbox_probe(struct platform_device *pdev)
> +{
> +       struct ast2700_mbox *mb;
> +       const struct ast2700_mbox_data *drv_data;

Calling this drv_data is slightly confusing. Perhaps dev_data?

> +       struct device *dev = &pdev->dev;
> +       int irq, ret;
> +
> +       if (!pdev->dev.of_node)
> +               return -ENODEV;
> +
> +       drv_data = device_get_match_data(&pdev->dev);
> +
> +       mb = devm_kzalloc(dev, sizeof(*mb), GFP_KERNEL);
> +       if (!mb)
> +               return -ENOMEM;
> +
> +       mb->mbox.chans = devm_kcalloc(&pdev->dev, drv_data->num_chans,
> +                                     sizeof(*mb->mbox.chans), GFP_KERNEL);
> +       if (!mb->mbox.chans)
> +               return -ENOMEM;
> +
> +       /* con_priv of each channel is used to store the message received */
> +       for (int i = 0; i < drv_data->num_chans; i++) {
> +               mb->mbox.chans[i].con_priv = devm_kcalloc(dev, drv_data->msg_size,
> +                                                         sizeof(u8), GFP_KERNEL);
> +               if (!mb->mbox.chans[i].con_priv)
> +                       return -ENOMEM;
> +       }
> +
> +       platform_set_drvdata(pdev, mb);
> +
> +       mb->regs = devm_platform_ioremap_resource(pdev, 0);
> +       if (IS_ERR(mb->regs))
> +               return PTR_ERR(mb->regs);
> +
> +       mb->drv_data = drv_data;
> +       mb->mbox.dev = dev;
> +       mb->mbox.num_chans = drv_data->num_chans;
> +       mb->mbox.ops = &ast2700_mbox_chan_ops;
> +       mb->mbox.txdone_irq = false;
> +       mb->mbox.txdone_poll = true;
> +       mb->mbox.txpoll_period = 5;
> +
> +       irq = platform_get_irq(pdev, 0);
> +       if (irq < 0)
> +               return irq;
> +
> +       ret = devm_request_irq(dev, irq, ast2700_mbox_irq, 0, dev_name(dev), mb);
> +       if (ret)
> +               return ret;
> +
> +       return devm_mbox_controller_register(dev, &mb->mbox);
> +}
> +
> +static const struct ast2700_mbox_data ast2700_drv_data = {

Again, this is data about the device, not data about the driver.
Perhaps ast2700_dev_data?

Andrew

> +       .num_chans = 4,
> +       .msg_size = 0x20,
> +};
> +
> +static const struct of_device_id ast2700_mbox_of_match[] = {
> +       { .compatible = "aspeed,ast2700-mailbox", .data = &ast2700_drv_data },
> +       {}
> +};
> +MODULE_DEVICE_TABLE(of, ast2700_mbox_of_match);
> +
> +static struct platform_driver ast2700_mbox_driver = {
> +       .driver = {
> +               .name = "ast2700-mailbox",
> +               .of_match_table = ast2700_mbox_of_match,
> +       },
> +       .probe = ast2700_mbox_probe,
> +};
> +module_platform_driver(ast2700_mbox_driver);
> +
> +MODULE_AUTHOR("Jammy Huang <jammy_huang@aspeedtech.com>");
> +MODULE_DESCRIPTION("ASPEED AST2700 IPC driver");
> +MODULE_LICENSE("GPL");


  reply	other threads:[~2025-06-25  1:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-23  2:44 [PATCH v4 0/2] [PATCH v4 0/2] ASPEED: Add mailbox driver for AST2700 series Jammy Huang
2025-06-23  2:44 ` [PATCH v4 1/2] dt-bindings: mailbox: Add ASPEED AST2700 series SoC Jammy Huang
2025-06-25  0:28   ` Andrew Jeffery
2025-06-25  0:34     ` Jammy Huang
2025-06-25  0:52       ` Andrew Jeffery
2025-06-25  3:03         ` Jammy Huang
2025-06-23  2:44 ` [PATCH v4 2/2] mailbox: aspeed: add mailbox driver for AST27XX " Jammy Huang
2025-06-25  1:12   ` Andrew Jeffery [this message]
2025-06-25  3:05     ` Jammy Huang

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=c1820a08930576d19c107d8e007acb21cae07a4d.camel@codeconstruct.com.au \
    --to=andrew@codeconstruct.com.au \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jammy_huang@aspeedtech.com \
    --cc=jassisinghbrar@gmail.com \
    --cc=joel@jms.id.au \
    --cc=krzk+dt@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-aspeed@lists.ozlabs.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox