devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junhui Liu <liujh2818@gmail.com>
To: d1581209858@live.com
Cc: aou@eecs.berkeley.edu, conor+dt@kernel.org,
	devicetree@vger.kernel.org, inochiama@outlook.com,
	jassisinghbrar@gmail.com, krzk+dt@kernel.org,
	linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org,
	palmer@dabbelt.com, paul.walmsley@sifive.com, robh@kernel.org,
	unicorn_wang@outlook.com
Subject: Re: [PATCH 3/3] mailbox: sophgo: add mailbox driver for cv18x SoCs
Date: Tue, 2 Jul 2024 23:08:37 +0800	[thread overview]
Message-ID: <cf4a9281-a28b-48f6-a53b-27df01980af6@gmail.com> (raw)
In-Reply-To: <SYBP282MB2238F93AB57A398E322644C3C4CE2@SYBP282MB2238.AUSP282.PROD.OUTLOOK.COM>

Hi Yuntao,

On 2024/6/18 23:12, Yuntao Dai wrote:
> Add mailbox controller driver for cv18x SoCs, tested on mailbox-test
> client.
>
> Signed-off-by: Yuntao Dai <d1581209858@live.com>
> ---
>   drivers/mailbox/Kconfig           |  11 ++
>   drivers/mailbox/Makefile          |   2 +
>   drivers/mailbox/cv1800b-mailbox.c | 181 ++++++++++++++++++++++++++++++
>   3 files changed, 194 insertions(+)
>   create mode 100644 drivers/mailbox/cv1800b-mailbox.c
>
> diff --git a/drivers/mailbox/Kconfig b/drivers/mailbox/Kconfig
> index 3b8842c4a..4e5593861 100644
> --- a/drivers/mailbox/Kconfig
> +++ b/drivers/mailbox/Kconfig
> @@ -286,4 +286,15 @@ config QCOM_IPCC
>   	  acts as an interrupt controller for receiving interrupts from clients.
>   	  Say Y here if you want to build this driver.
>   
> +config CV1800B_MBOX
> +	tristate "cv1800b mailbox"
> +	depends on OF
> +	depends on ARCH_SOPHGO || COMPILE_TEST
> +	help
> +	  Mailbox driver implementation for Sophgo cv180x SoCs. This driver
> +	  can be used to send message between different processors in SoC. Any
> +	  processer can write data in a channel, and set co-responding register
> +	  to raise interrupt to notice another processor, and it is allowed to
> +	  send data to itself.
> +
>   endif
> diff --git a/drivers/mailbox/Makefile b/drivers/mailbox/Makefile
> index 5cf2f54de..71f0f746e 100644
> --- a/drivers/mailbox/Makefile
> +++ b/drivers/mailbox/Makefile
> @@ -62,3 +62,5 @@ obj-$(CONFIG_SUN6I_MSGBOX)	+= sun6i-msgbox.o
>   obj-$(CONFIG_SPRD_MBOX)		+= sprd-mailbox.o
>   better
>   obj-$(CONFIG_QCOM_IPCC)		+= qcom-ipcc.o
> +
> +obj-$(CONFIG_CV1800B_MBOX)	+= cv1800b-mailbox.o
> \ No newline at end of file
> diff --git a/drivers/mailbox/cv1800b-mailbox.c b/drivers/mailbox/cv1800b-mailbox.c
> new file mode 100644
> index 000000000..8ef2a5492
> --- /dev/null
> +++ b/drivers/mailbox/cv1800b-mailbox.c
> @@ -0,0 +1,181 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +#include <linux/device.h>
> +#include <linux/err.h>
> +#include <linux/interrupt.h>
> +#include <linux/io.h>
> +#include <linux/kfifo.h>
> +#include <linux/mailbox_controller.h>
> +#include <linux/mailbox_client.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
> +
> +#define MAILBOX_MAX_CHAN 0x0008
> +#define MAILBOX_DONE_OFFSET 0x0002
> +#define MAILBOX_CONTEXT_SIZE 0x0040
> +#define MAILBOX_CONTEXT_OFFSET 0x0400
> +
> +#define MBOX_EN_REG(cpu) (cpu << 2)
> +#define MBOX_DONE_REG(cpu) ((cpu << 2) + MAILBOX_DONE_OFFSET)
> +
> +#define MBOX_SET_CLR_REG(cpu) (0x10 + (cpu << 4))
> +#define MBOX_SET_INT_REG(cpu) (0x18 + (cpu << 4))
> +
> +#define MBOX_SET_REG 0x60
> +
> +struct cv1800b_mbox {
> +	struct mbox_controller mbox;
> +	struct mbox_chan chans[MAILBOX_MAX_CHAN];
> +	u64 *content[MAILBOX_MAX_CHAN];
> +	void __iomem *mbox_base;
> +	int sendto;
> +	int recvid;
> +};
> +
> +static irqreturn_t cv1800b_mbox_isr(int irq, void *dev_id)
> +{
> +	struct cv1800b_mbox *mbox = (struct cv1800b_mbox *)dev_id;
> +	size_t i;
> +
> +	for (i = 0; i < MAILBOX_MAX_CHAN; i++) {
> +		if (mbox->content[i]) {
> +			mbox_chan_received_data(&mbox->chans[i],
> +						mbox->content[i]);
I tested this driver but met "NULL pointer dereference" Oops here
when I sent message from the c906l core without binding clients.

I think maybe it's better to add a check here, like:

struct mbox_chan *chan = &mbox->chans[i];
if (chan->cl) {
     mbox_chan_received_data(chan, mbox->content[i]);
}

Best,
Junhui Liu

> +			mbox->content[i] = NULL;
> +			return IRQ_HANDLED;
> +		}
> +	}
> +	return IRQ_NONE;
> +}
> +
> +static irqreturn_t cv1800b_mbox_irq(int irq, void *dev_id)
> +{
> +	struct cv1800b_mbox *mbox = (struct cv1800b_mbox *)dev_id;
> +	u8 set, valid;
> +	u64 *addr;
> +	size_t i;
> +
> +	set = readb(mbox->mbox_base + MBOX_SET_INT_REG(mbox->recvid));
> +
> +	if (!set)
> +		return IRQ_NONE;
> +
> +	for (i = 0; i < MAILBOX_MAX_CHAN; i++) {
> +		valid = set & (1 << i);
> +		addr = (u64 *)(mbox->mbox_base + MAILBOX_CONTEXT_OFFSET) + i;
> +		if (valid) {
> +			mbox->content[i] = addr;
> +			writeb(valid, mbox->mbox_base +
> +					      MBOX_SET_CLR_REG(mbox->recvid));
> +			writeb(~valid,
> +			       mbox->mbox_base + MBOX_EN_REG(mbox->recvid));
> +			return IRQ_WAKE_THREAD;
> +		}
> +	}
> +
> +	return IRQ_NONE;
> +}
> +
> +static int cv1800b_mbox_send_data(struct mbox_chan *chan, void *data)
> +{
> +	struct cv1800b_mbox *mbox = dev_get_drvdata(chan->mbox->dev);
> +	int idx = (int)chan->con_priv;
> +	u8 en, valid;
> +	u64 *addr = (u64 *)(mbox->mbox_base + MAILBOX_CONTEXT_OFFSET) + idx;
> +
> +	memcpy_toio(addr, data, 8);
> +
> +	valid = 1 << idx;
> +	writeb(valid, mbox->mbox_base + MBOX_SET_CLR_REG(mbox->sendto));
> +	en = readb(mbox->mbox_base + MBOX_EN_REG(mbox->sendto));
> +	writeb(en | valid, mbox->mbox_base + MBOX_EN_REG(mbox->sendto));
> +	writeb(valid, mbox->mbox_base + MBOX_SET_REG);
> +
> +	return 0;
> +}
> +
> +static bool cv1800b_last_tx_done(struct mbox_chan *chan)
> +{
> +	return true;
> +}
> +
> +static const struct mbox_chan_ops cv1800b_mbox_chan_ops = {
> +	.send_data = cv1800b_mbox_send_data,
> +	.last_tx_done = cv1800b_last_tx_done,
> +};
> +
> +static const struct of_device_id cv1800b_mbox_of_match[] = {
> +	{ .compatible = "sophgo,cv1800b-mailbox", },
> +	{},
> +};
> +MODULE_DEVICE_TABLE(of, cv1800b_mbox_of_match);
> +
> +static int cv1800b_mbox_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct cv1800b_mbox *mb;
> +	int irq, idx, err, cpu;
> +
> +	if (!dev->of_node)
> +		return -ENODEV;
> +
> +	mb = devm_kzalloc(dev, sizeof(*mb), GFP_KERNEL);
> +	if (!mb)
> +		return -ENOMEM;
> +
> +	mb->mbox_base = devm_of_iomap(dev, dev->of_node, 0, NULL);
> +	if (IS_ERR(mb->mbox_base))
> +		return dev_err_probe(dev, PTR_ERR(mb->mbox_base),
> +				     "Failed to map resource\n");
> +
> +	err = of_property_read_s32(dev->of_node, "sendto", &cpu);
> +	if (err)
> +		return dev_err_probe(dev, err,
> +				     "Failed to find <sendto> in of_node\n");
> +
> +	mb->sendto = cpu;
> +
> +	err = of_property_read_s32(dev->of_node, "recvid", &cpu);
> +	if (err) {
> +		return dev_err_probe(dev, err,
> +				     "Failed to find <recvid> in of_node\n");
> +	}
> +	mb->recvid = cpu;
> +
> +	mb->mbox.dev = dev;
> +	mb->mbox.num_chans = MAILBOX_MAX_CHAN;
> +	mb->mbox.chans = mb->chans;
> +	mb->mbox.ops = &cv1800b_mbox_chan_ops;
> +	mb->mbox.txdone_poll = true;
> +
> +	irq = platform_get_irq_byname(pdev, "mailbox");
> +	err = devm_request_threaded_irq(dev, irq, cv1800b_mbox_irq,
> +					cv1800b_mbox_isr, IRQF_ONESHOT,
> +					dev_name(&pdev->dev), mb);
> +	if (err < 0)
> +		return dev_err_probe(dev, err, "Failed to register irq\n");
> +
> +	for (idx = 0; idx < MAILBOX_MAX_CHAN; idx++)
> +		mb->mbox.chans[idx].con_priv = (void *)idx;
> +
> +	err = devm_mbox_controller_register(dev, &mb->mbox);
> +	if (err)
> +		return dev_err_probe(dev, err, "Failed to register mailbox\n");
> +
> +	platform_set_drvdata(pdev, mb);
> +	return 0;
> +}
> +
> +static struct platform_driver cv1800b_mbox_driver = {
> +	.driver = {
> +		.name = "cv1800b-mbox",
> +		.of_match_table = cv1800b_mbox_of_match,
> +	},
> +	.probe	= cv1800b_mbox_probe,
> +};
> +
> +module_platform_driver(cv1800b_mbox_driver);
> +
> +MODULE_DESCRIPTION("cv1800b mailbox driver");
> +MODULE_LICENSE("GPL");

  parent reply	other threads:[~2024-07-02 15:08 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-18 15:03 [PATCH 0/3] riscv: sophgo: add mailbox support for cv18x SoCs Yuntao Dai
2024-06-18 15:12 ` [PATCH 1/3] dt-bindings: mailbox: add Sophgo cv18x SoCs mailbox Yuntao Dai
2024-06-18 15:38   ` Conor Dooley
2024-06-19 14:33     ` 韵涛 代
2024-06-19 18:07       ` Conor Dooley
2024-06-18 16:54   ` Rob Herring (Arm)
2024-06-19 14:40     ` 韵涛 代
2024-06-18 22:33   ` Inochi Amaoto
2024-06-19 14:42     ` 韵涛 代
2024-06-18 22:45   ` Inochi Amaoto
     [not found]     ` <IA1PR20MB4953CE25C805EB66EFFDC36DBBCE2@IA1PR20MB4953.namprd20.prod.outlook.c om>
2024-07-14 16:04       ` Yuntao Dai
2024-06-18 23:21   ` kernel test robot
2024-06-18 15:12 ` [PATCH 2/3] riscv: dts: add mailbox for Sophgo cv18x SoCs Yuntao Dai
2024-06-18 15:12 ` [PATCH 3/3] mailbox: sophgo: add mailbox driver for " Yuntao Dai
2024-06-19 18:15   ` kernel test robot
2024-06-20  1:27   ` kernel test robot
2024-06-21  0:21   ` kernel test robot
2024-06-21 13:37   ` kernel test robot
2024-07-02 15:08   ` Junhui Liu [this message]
2024-07-14 16:09     ` Yuntao Dai

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=cf4a9281-a28b-48f6-a53b-27df01980af6@gmail.com \
    --to=liujh2818@gmail.com \
    --cc=aou@eecs.berkeley.edu \
    --cc=conor+dt@kernel.org \
    --cc=d1581209858@live.com \
    --cc=devicetree@vger.kernel.org \
    --cc=inochiama@outlook.com \
    --cc=jassisinghbrar@gmail.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=robh@kernel.org \
    --cc=unicorn_wang@outlook.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).