Skip to contentSkip to conversation

mailbox: Add Tegra HSP driver

PATCH Superseded Thierry Reding 2 participants 10 messages Nov 15, 2016 – Nov 18, 2016

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover mailbox: Add Tegra HSP driver

From: Thierry Reding <[email protected]>

Hi Jassi,

These patches have gone through a number of revisions now and I've tested them to the point where I'm confident that they work well.

Given the chain of drivers that depends on this, I think it'd be good if I took this through the Tegra tree with your Acked-by, so that I can deal with the dependencies within one tree. I can provide a stable tag for you to pull into the mailbox tree.

Thanks,
Thierry

Joseph Lo (1):
dt-bindings: mailbox: Add Tegra HSP binding

Thierry Reding (1):
mailbox: Add Tegra HSP driver

 .../bindings/mailbox/nvidia,tegra186-hsp.txt       |  52 ++
 drivers/mailbox/Kconfig                            |   9 +
 drivers/mailbox/Makefile                           |   2 +
 drivers/mailbox/tegra-hsp.c                        | 542 +++++++++++++++++++++
 include/dt-bindings/mailbox/tegra186-hsp.h         |  24 +
 5 files changed, 629 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mailbox/nvidia,tegra186-hsp.txt
 create mode 100644 drivers/mailbox/tegra-hsp.c
 create mode 100644 include/dt-bindings/mailbox/tegra186-hsp.h
Signature · 1 line
2.10.2
#1 lore raw

PATCH v4 1/2 dt-bindings: mailbox: Add Tegra HSP binding

From: Joseph Lo <[email protected]>

Add DT binding for the Hardware Synchronization Primitives (HSP). The HSP is designed for the processors to share resources and communicate with one another. A set of hardware synchronization primitives for interprocessor communication (IPC) is provided. IPC protocols can use use these hardware synchronization primitives when operating between processors in an AMP configuration.

---
 .../bindings/mailbox/nvidia,tegra186-hsp.txt       | 52 ++++++++++++++++++++++
 include/dt-bindings/mailbox/tegra186-hsp.h         | 24 ++++++++++
 2 files changed, 76 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mailbox/nvidia,tegra186-hsp.txt
 create mode 100644 include/dt-bindings/mailbox/tegra186-hsp.h
Documentation/devicetree/bindings/mailbox/nvidia,tegra186-hsp.txt new file +52 -0
new file mode 100644
index 000000000000..b99d25fc2f26
@@ -0,0 +1,52 @@
+NVIDIA Tegra Hardware Synchronization Primitives (HSP)
+
+The HSP modules are used for the processors to share resources and communicate
+together. It provides a set of hardware synchronization primitives for
+interprocessor communication. So the interprocessor communication (IPC)
+protocols can use hardware synchronization primitives, when operating between
+two processors not in an SMP relationship.
+
+The features that HSP supported are shared mailboxes, shared semaphores,
+arbitrated semaphores and doorbells.
+
+Required properties:
+- name : Should be hsp
+- compatible
+    Array of strings.
+    one of:
+    - "nvidia,tegra186-hsp"
+- reg : Offset and length of the register set for the device.
+- interrupt-names
+    Array of strings.
+    Contains a list of names for the interrupts described by the interrupt
+    property. May contain the following entries, in any order:
+    - "doorbell"
+    Users of this binding MUST look up entries in the interrupt property
+    by name, using this interrupt-names property to do so.
+- interrupts
+    Array of interrupt specifiers.
+    Must contain one entry per entry in the interrupt-names property,
+    in a matching order.
+- #mbox-cells : Should be 2.
+
+The mbox specifier of the "mboxes" property in the client node should
+contain two data. The first one should be the HSP type and the second
+one should be the ID that the client is going to use. Those information
+can be found in the following file.
+
+- <dt-bindings/mailbox/tegra186-hsp.h>.
+
+Example:
+
+hsp_top0: hsp@3c00000 {
+	compatible = "nvidia,tegra186-hsp";
+	reg = <0x0 0x03c00000 0x0 0xa0000>;
+	interrupts = <GIC_SPI 176 IRQ_TYPE_LEVEL_HIGH>;
+	interrupt-names = "doorbell";
+	#mbox-cells = <2>;
+};
+
+client {
+	...
+	mboxes = <&hsp_top0 TEGRA_HSP_MBOX_TYPE_DB TEGRA_HSP_DB_MASTER_XXX>;
+};
include/dt-bindings/mailbox/tegra186-hsp.h new file +24 -0
new file mode 100644
index 000000000000..f5d66e5f5f10
@@ -0,0 +1,24 @@
+/*
+ * This header provides constants for binding nvidia,tegra186-hsp.
+ */
+
+#ifndef _DT_BINDINGS_MAILBOX_TEGRA186_HSP_H
+#define _DT_BINDINGS_MAILBOX_TEGRA186_HSP_H
+
+/*
+ * These define the type of mailbox that is to be used (doorbell, shared
+ * mailbox, shared semaphore or arbitrated semaphore).
+ */
+#define TEGRA_HSP_MBOX_TYPE_DB 0x0
+#define TEGRA_HSP_MBOX_TYPE_SM 0x1
+#define TEGRA_HSP_MBOX_TYPE_SS 0x2
+#define TEGRA_HSP_MBOX_TYPE_AS 0x3
+
+/*
+ * These defines represent the bit associated with the given master ID in the
+ * doorbell registers.
+ */
+#define TEGRA_HSP_DB_MASTER_CCPLEX 17
+#define TEGRA_HSP_DB_MASTER_BPMP 19
+
+#endif
Signature · 1 line
2.10.2
#2 lore raw

PATCH v4 2/2 mailbox: Add Tegra HSP driver

In reply to Thierry Reding

From: Thierry Reding <[email protected]>

This driver exposes a mailbox interface for interprocessor communication using the Hardware Synchronization Primitives (HSP) module's doorbell mechanism. There are multiple HSP instances and they provide additional features such as shared mailboxes, shared and arbitrated semaphores.

A driver for a remote processor can use the mailbox client provided by the HSP driver and build an IPC protocol on top of this synchronization mechanism.

Based on work by Joseph Lo <[email protected]>.

---
Changes in v4:
- check for doorbell ring before enabling new master
- add missing space in Kconfig description
- remove unnecessary barriers
- simplify doorbell lookup
- remove debug leftovers

Changes in v3:
- use a more object oriented design
- fix some locking issues

 drivers/mailbox/Kconfig     |   9 +
 drivers/mailbox/Makefile    |   2 +
 drivers/mailbox/tegra-hsp.c | 542 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 553 insertions(+)
 create mode 100644 drivers/mailbox/tegra-hsp.c
drivers/mailbox/Kconfig +9 -0
index 11eebfe8a4cb..ceff415f201c 100644
@@ -124,6 +124,15 @@ config MAILBOX_TEST
 	  Test client to help with testing new Controller driver
 	  implementations.
 
+config TEGRA_HSP_MBOX
+	bool "Tegra HSP (Hardware Synchronization Primitives) Driver"
+	depends on ARCH_TEGRA_186_SOC
+	help
+	  The Tegra HSP driver is used for the interprocessor communication
+	  between different remote processors and host processors on Tegra186
+	  and later SoCs. Say Y here if you want to have this support.
+	  If unsure say N.
+
 config XGENE_SLIMPRO_MBOX
 	tristate "APM SoC X-Gene SLIMpro Mailbox Controller"
 	depends on ARCH_XGENE
drivers/mailbox/Makefile +2 -0
index ace6fed8fea9..7dde4f609ae8 100644
@@ -29,3 +29,5 @@ obj-$(CONFIG_XGENE_SLIMPRO_MBOX) += mailbox-xgene-slimpro.o
 obj-$(CONFIG_HI6220_MBOX)	+= hi6220-mailbox.o
 
 obj-$(CONFIG_BCM_PDC_MBOX)	+= bcm-pdc-mailbox.o
+
+obj-$(CONFIG_TEGRA_HSP_MBOX)	+= tegra-hsp.o
drivers/mailbox/tegra-hsp.c new file +542 -0

542 lines not on this page · raw message

Signature · 1 line
2.10.2
maintainer
#3 lore raw

On Tue, Nov 15, 2016 at 9:18 PM, Thierry Reding <[email protected]> wrote:

....

Quoted 13 lines
drivers/mailbox/tegra-hsp.c new file +13 -0
+
+struct tegra_hsp_channel;
+struct tegra_hsp;
+
+struct tegra_hsp_channel_ops {
+       int (*send_data)(struct tegra_hsp_channel *channel, void *data);
+       int (*startup)(struct tegra_hsp_channel *channel);
+       void (*shutdown)(struct tegra_hsp_channel *channel);
+       bool (*last_tx_done)(struct tegra_hsp_channel *channel);
+};
+
+struct tegra_hsp_channel {
+       struct tegra_hsp *hsp;

drivers/mailbox/tegra-hsp.c line 62

Quoted 10 lines
drivers/mailbox/tegra-hsp.c new file +10 -0
+       const struct tegra_hsp_channel_ops *ops;
+       struct mbox_chan *chan;
+       void __iomem *regs;
+};
+
+static struct tegra_hsp_channel *to_tegra_hsp_channel(struct mbox_chan *chan)
+{
+       return chan->con_priv;
+}
+

It seems

       channel = to_tegra_hsp_channel(chan);
is no simpler ritual than
       channel = chan->con_priv;   ?
Quoted 7 lines
drivers/mailbox/tegra-hsp.c new file +7 -0
+struct tegra_hsp_doorbell {
+       struct tegra_hsp_channel channel;
+       struct list_head list;
+       const char *name;
+       unsigned int master;
+       unsigned int index;
+};

drivers/mailbox/tegra-hsp.c line 79

Quoted 10 lines
drivers/mailbox/tegra-hsp.c new file +10 -0
+
+static struct tegra_hsp_doorbell *
+to_tegra_hsp_doorbell(struct tegra_hsp_channel *channel)
+{
+       if (!channel)
+               return NULL;
+
+       return container_of(channel, struct tegra_hsp_doorbell, channel);
+}
+

But you don't check for NULL returned, before dereferencing the pointer 'db'

Quoted 88 lines
drivers/mailbox/tegra-hsp.c new file +88 -0
+struct tegra_hsp_db_map {
+       const char *name;
+       unsigned int master;
+       unsigned int index;
+};
+
+struct tegra_hsp_soc {
+       const struct tegra_hsp_db_map *map;
+};
+
+struct tegra_hsp {
+       const struct tegra_hsp_soc *soc;
+       struct mbox_controller mbox;
+       void __iomem *regs;
+       unsigned int irq;
+       unsigned int num_sm;
+       unsigned int num_as;
+       unsigned int num_ss;
+       unsigned int num_db;
+       unsigned int num_si;
+       spinlock_t lock;
+
+       struct list_head doorbells;
+};
+
+static inline struct tegra_hsp *
+to_tegra_hsp(struct mbox_controller *mbox)
+{
+       return container_of(mbox, struct tegra_hsp, mbox);
+}
+
+static inline u32 tegra_hsp_readl(struct tegra_hsp *hsp, unsigned int offset)
+{
+       return readl(hsp->regs + offset);
+}
+
+static inline void tegra_hsp_writel(struct tegra_hsp *hsp, u32 value,
+                                   unsigned int offset)
+{
+       writel(value, hsp->regs + offset);
+}
+
+static inline u32 tegra_hsp_channel_readl(struct tegra_hsp_channel *channel,
+                                         unsigned int offset)
+{
+       return readl(channel->regs + offset);
+}
+
+static inline void tegra_hsp_channel_writel(struct tegra_hsp_channel *channel,
+                                           u32 value, unsigned int offset)
+{
+       writel(value, channel->regs + offset);
+}
+
+static bool tegra_hsp_doorbell_can_ring(struct tegra_hsp_doorbell *db)
+{
+       u32 value;
+
+       value = tegra_hsp_channel_readl(&db->channel, HSP_DB_ENABLE);
+
+       return (value & BIT(TEGRA_HSP_DB_MASTER_CCPLEX)) != 0;
+}
+
+static struct tegra_hsp_doorbell *
+__tegra_hsp_doorbell_get(struct tegra_hsp *hsp, unsigned int master)
+{
+       struct tegra_hsp_doorbell *entry;
+
+       list_for_each_entry(entry, &hsp->doorbells, list)
+               if (entry->master == master)
+                       return entry;
+
+       return NULL;
+}
+
+static struct tegra_hsp_doorbell *
+tegra_hsp_doorbell_get(struct tegra_hsp *hsp, unsigned int master)
+{
+       struct tegra_hsp_doorbell *db;
+       unsigned long flags;
+
+       spin_lock_irqsave(&hsp->lock, flags);
+       db = __tegra_hsp_doorbell_get(hsp, master);
+       spin_unlock_irqrestore(&hsp->lock, flags);
+
+       return db;
+}
+

.....

Quoted 57 lines
drivers/mailbox/tegra-hsp.c new file +57 -0
+
+static int tegra_hsp_doorbell_send_data(struct tegra_hsp_channel *channel,
+                                       void *data)
+{
+       tegra_hsp_channel_writel(channel, 1, HSP_DB_TRIGGER);
+
+       return 0;
+}
+
+static int tegra_hsp_doorbell_startup(struct tegra_hsp_channel *channel)
+{
+       struct tegra_hsp_doorbell *db = to_tegra_hsp_doorbell(channel);
+       struct tegra_hsp *hsp = channel->hsp;
+       struct tegra_hsp_doorbell *ccplex;
+       unsigned long flags;
+       u32 value;
+
+       if (db->master >= hsp->mbox.num_chans) {
+               dev_err(hsp->mbox.dev,
+                       "invalid master ID %u for HSP channel\n",
+                       db->master);
+               return -EINVAL;
+       }
+
+       ccplex = tegra_hsp_doorbell_get(hsp, TEGRA_HSP_DB_MASTER_CCPLEX);
+       if (!ccplex)
+               return -ENODEV;
+
+       if (!tegra_hsp_doorbell_can_ring(db))
+               return -ENODEV;
+
+       spin_lock_irqsave(&hsp->lock, flags);
+
+       value = tegra_hsp_channel_readl(&ccplex->channel, HSP_DB_ENABLE);
+       value |= BIT(db->master);
+       tegra_hsp_channel_writel(&ccplex->channel, value, HSP_DB_ENABLE);
+
+       spin_unlock_irqrestore(&hsp->lock, flags);
+
+       return 0;
+}
+
+static void tegra_hsp_doorbell_shutdown(struct tegra_hsp_channel *channel)
+{
+       struct tegra_hsp_doorbell *db = to_tegra_hsp_doorbell(channel);
+       struct tegra_hsp *hsp = channel->hsp;
+       struct tegra_hsp_doorbell *ccplex;
+       unsigned long flags;
+       u32 value;
+
+       ccplex = tegra_hsp_doorbell_get(hsp, TEGRA_HSP_DB_MASTER_CCPLEX);
+       if (!ccplex)
+               return;
+
+       spin_lock_irqsave(&hsp->lock, flags);
+
+       value = tegra_hsp_channel_readl(&ccplex->channel, HSP_DB_ENABLE);

drivers/mailbox/tegra-hsp.c line 272

Quoted 10 lines
drivers/mailbox/tegra-hsp.c new file +10 -0
+       value &= ~BIT(db->master);
+       tegra_hsp_channel_writel(&ccplex->channel, value, HSP_DB_ENABLE);
+
+       spin_unlock_irqrestore(&hsp->lock, flags);
+}
+
+static bool tegra_hsp_doorbell_last_tx_done(struct tegra_hsp_channel *channel)
+{
+       return true;
+}

Just curious, is the IPC done instantly after writing HSP_DB_TRIGGER bit? Usually there is at least some bit that stays (un)set as a 'busy flag'.

Quoted 8 lines
drivers/mailbox/tegra-hsp.c new file +8 -0
+
+static const struct tegra_hsp_channel_ops tegra_hsp_doorbell_ops = {
+       .send_data = tegra_hsp_doorbell_send_data,
+       .startup = tegra_hsp_doorbell_startup,
+       .shutdown = tegra_hsp_doorbell_shutdown,
+       .last_tx_done = tegra_hsp_doorbell_last_tx_done,
+};
+

....

Quoted 25 lines
drivers/mailbox/tegra-hsp.c new file +25 -0
+static int tegra_hsp_send_data(struct mbox_chan *chan, void *data)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->send_data(channel, data);
+}
+
+static int tegra_hsp_startup(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->startup(channel);
+}
+
+static void tegra_hsp_shutdown(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->shutdown(channel);
+}
+
+static bool tegra_hsp_last_tx_done(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+

drivers/mailbox/tegra-hsp.c line 352

Quoted 10 lines
drivers/mailbox/tegra-hsp.c new file +10 -0
+       return channel->ops->last_tx_done(channel);
+}
+
+static const struct mbox_chan_ops tegra_hsp_ops = {
+       .send_data = tegra_hsp_send_data,
+       .startup = tegra_hsp_startup,
+       .shutdown = tegra_hsp_shutdown,
+       .last_tx_done = tegra_hsp_last_tx_done,
+};
+

These 4 above seem overkill. Why not directly use tegra_hsp_doorbell_xxx() ?

Cheers!

#4 lore raw
Quoted 28 lines · Jassi Brar

On Tue, Nov 15, 2016 at 9:18 PM, Thierry Reding <[email protected]> wrote:

....

Quoted 13 lines
drivers/mailbox/tegra-hsp.c new file +13 -0
+
+struct tegra_hsp_channel;
+struct tegra_hsp;
+
+struct tegra_hsp_channel_ops {
+       int (*send_data)(struct tegra_hsp_channel *channel, void *data);
+       int (*startup)(struct tegra_hsp_channel *channel);
+       void (*shutdown)(struct tegra_hsp_channel *channel);
+       bool (*last_tx_done)(struct tegra_hsp_channel *channel);
+};
+
+struct tegra_hsp_channel {
+       struct tegra_hsp *hsp;

drivers/mailbox/tegra-hsp.c line 62

Quoted 10 lines
drivers/mailbox/tegra-hsp.c new file +10 -0
+       const struct tegra_hsp_channel_ops *ops;
+       struct mbox_chan *chan;
+       void __iomem *regs;
+};
+
+static struct tegra_hsp_channel *to_tegra_hsp_channel(struct mbox_chan *chan)
+{
+       return chan->con_priv;
+}
+

It seems

Quoted 3 lines
       channel = to_tegra_hsp_channel(chan);
is no simpler ritual than
       channel = chan->con_priv;   ?

Yes, that's true. I've dropped the to_tegra_hsp_channel() inline in favour of using the chan->con_priv directly.

Quoted 10 lines
Quoted 10 lines
drivers/mailbox/tegra-hsp.c new file +10 -0
+struct tegra_hsp_doorbell {
+       struct tegra_hsp_channel channel;
+       struct list_head list;
+       const char *name;
+       unsigned int master;
+       unsigned int index;
+};
+
+static struct tegra_hsp_doorbell *
+to_tegra_hsp_doorbell(struct tegra_hsp_channel *channel)
Quoted 8 lines

drivers/mailbox/tegra-hsp.c line 79

Quoted 7 lines
drivers/mailbox/tegra-hsp.c new file +7 -0
+{
+       if (!channel)
+               return NULL;
+
+       return container_of(channel, struct tegra_hsp_doorbell, channel);
+}
+

But you don't check for NULL returned, before dereferencing the pointer 'db'

In all the call sites where this is used the channel is guaranteed not to be NULL, hence no checking is necessary. However the function here could potentially be used in other cases where no such guarantees can be given and checking the !channel above is merely there to avoid casting to a non-NULL pointer from a NULL pointer.

I've run occasionally into this issue because container_of() will simply perform arithmetic on the pointer given, so passing channel as NULL would convert to some very large pointer that can no longer be easily discerned from an invalid pointer.

So this is primarily a safety feature, and one that I'd prefer to keep just to avoid running into issues down the road when the function gets used under different circumstances.

Quoted 7 lines

drivers/mailbox/tegra-hsp.c line 272

Quoted 4 lines
drivers/mailbox/tegra-hsp.c new file +4 -0
+static bool tegra_hsp_doorbell_last_tx_done(struct tegra_hsp_channel *channel)
+{
+       return true;
+}

Just curious, is the IPC done instantly after writing HSP_DB_TRIGGER bit? Usually there is at least some bit that stays (un)set as a 'busy flag'.

I don't think there's a bit like that for doorbells. The way that these doorbells are used is in combination with a shared memory IPC protocol. Two processors will communicate by writing to and reading from what is essentially a ring buffer in shared memory. The doorbells are merely a means of communicating their peer that a new entry is available in the shared memory.

Quoted 37 lines
Quoted 7 lines
drivers/mailbox/tegra-hsp.c new file +7 -0
+static const struct tegra_hsp_channel_ops tegra_hsp_doorbell_ops = {
+       .send_data = tegra_hsp_doorbell_send_data,
+       .startup = tegra_hsp_doorbell_startup,
+       .shutdown = tegra_hsp_doorbell_shutdown,
+       .last_tx_done = tegra_hsp_doorbell_last_tx_done,
+};
+

....

Quoted 28 lines
drivers/mailbox/tegra-hsp.c new file +28 -0
+static int tegra_hsp_send_data(struct mbox_chan *chan, void *data)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->send_data(channel, data);
+}
+
+static int tegra_hsp_startup(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->startup(channel);
+}
+
+static void tegra_hsp_shutdown(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->shutdown(channel);
+}
+
+static bool tegra_hsp_last_tx_done(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->last_tx_done(channel);
+}
+
Quoted 8 lines

drivers/mailbox/tegra-hsp.c line 352

Quoted 7 lines
drivers/mailbox/tegra-hsp.c new file +7 -0
+static const struct mbox_chan_ops tegra_hsp_ops = {
+       .send_data = tegra_hsp_send_data,
+       .startup = tegra_hsp_startup,
+       .shutdown = tegra_hsp_shutdown,
+       .last_tx_done = tegra_hsp_last_tx_done,
+};
+

These 4 above seem overkill. Why not directly use tegra_hsp_doorbell_xxx() ?

This is in preparation for supporting the other synchronization primitives that the HSP IP block exposes. Some of them use different programming and semantics, hence why we want to have this second level of abstraction. It will allow us to share some of the code between the different primitives once their implementations are added.

If you feel really strongly about it, I suppose I could eliminate it, but I suspect that we'd want to add them back in after support for the other primitives is added.

Thanks,
Thierry

maintainer
#5 lore raw
Quoted 78 lines · Thierry Reding
Quoted 28 lines · Jassi Brar

On Tue, Nov 15, 2016 at 9:18 PM, Thierry Reding <[email protected]> wrote:

....

Quoted 13 lines
drivers/mailbox/tegra-hsp.c new file +13 -0
+
+struct tegra_hsp_channel;
+struct tegra_hsp;
+
+struct tegra_hsp_channel_ops {
+       int (*send_data)(struct tegra_hsp_channel *channel, void *data);
+       int (*startup)(struct tegra_hsp_channel *channel);
+       void (*shutdown)(struct tegra_hsp_channel *channel);
+       bool (*last_tx_done)(struct tegra_hsp_channel *channel);
+};
+
+struct tegra_hsp_channel {
+       struct tegra_hsp *hsp;

drivers/mailbox/tegra-hsp.c line 62

Quoted 10 lines
drivers/mailbox/tegra-hsp.c new file +10 -0
+       const struct tegra_hsp_channel_ops *ops;
+       struct mbox_chan *chan;
+       void __iomem *regs;
+};
+
+static struct tegra_hsp_channel *to_tegra_hsp_channel(struct mbox_chan *chan)
+{
+       return chan->con_priv;
+}
+

It seems

Quoted 3 lines
       channel = to_tegra_hsp_channel(chan);
is no simpler ritual than
       channel = chan->con_priv;   ?

Yes, that's true. I've dropped the to_tegra_hsp_channel() inline in favour of using the chan->con_priv directly.

Quoted 10 lines
Quoted 10 lines
drivers/mailbox/tegra-hsp.c new file +10 -0
+struct tegra_hsp_doorbell {
+       struct tegra_hsp_channel channel;
+       struct list_head list;
+       const char *name;
+       unsigned int master;
+       unsigned int index;
+};
+
+static struct tegra_hsp_doorbell *
+to_tegra_hsp_doorbell(struct tegra_hsp_channel *channel)
Quoted 8 lines

drivers/mailbox/tegra-hsp.c line 79

Quoted 7 lines
drivers/mailbox/tegra-hsp.c new file +7 -0
+{
+       if (!channel)
+               return NULL;
+
+       return container_of(channel, struct tegra_hsp_doorbell, channel);
+}
+

But you don't check for NULL returned, before dereferencing the pointer 'db'

In all the call sites where this is used the channel is guaranteed not to be NULL, hence no checking is necessary. However the function here could potentially be used in other cases where no such guarantees can be given and checking the !channel above is merely there to avoid casting to a non-NULL pointer from a NULL pointer.

I've run occasionally into this issue because container_of() will simply perform arithmetic on the pointer given, so passing channel as NULL would convert to some very large pointer that can no longer be easily discerned from an invalid pointer.

So this is primarily a safety feature, and one that I'd prefer to keep just to avoid running into issues down the road when the function gets used under different circumstances.

Quoted 7 lines

drivers/mailbox/tegra-hsp.c line 272

Quoted 4 lines
drivers/mailbox/tegra-hsp.c new file +4 -0
+static bool tegra_hsp_doorbell_last_tx_done(struct tegra_hsp_channel *channel)
+{
+       return true;
+}

Just curious, is the IPC done instantly after writing HSP_DB_TRIGGER bit? Usually there is at least some bit that stays (un)set as a 'busy flag'.

Quoted 7 lines

I don't think there's a bit like that for doorbells. The way that these doorbells are used is in combination with a shared memory IPC protocol. Two processors will communicate by writing to and reading from what is essentially a ring buffer in shared memory. The doorbells are merely a means of communicating their peer that a new entry is available in the shared memory.

For such protocols, we have the TXDONE_BY_ACK. I assume your client drivers will drive the state-machine. Otherwise, you risk overrunning the ring-buffer in SHM, but not caring if the first filled buffer was actually consumed by the remote (just like ALSA ring buffer).

Quoted 46 lines
Quoted 37 lines
Quoted 7 lines
drivers/mailbox/tegra-hsp.c new file +7 -0
+static const struct tegra_hsp_channel_ops tegra_hsp_doorbell_ops = {
+       .send_data = tegra_hsp_doorbell_send_data,
+       .startup = tegra_hsp_doorbell_startup,
+       .shutdown = tegra_hsp_doorbell_shutdown,
+       .last_tx_done = tegra_hsp_doorbell_last_tx_done,
+};
+

....

Quoted 28 lines
drivers/mailbox/tegra-hsp.c new file +28 -0
+static int tegra_hsp_send_data(struct mbox_chan *chan, void *data)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->send_data(channel, data);
+}
+
+static int tegra_hsp_startup(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->startup(channel);
+}
+
+static void tegra_hsp_shutdown(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->shutdown(channel);
+}
+
+static bool tegra_hsp_last_tx_done(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->last_tx_done(channel);
+}
+
Quoted 8 lines

drivers/mailbox/tegra-hsp.c line 352

Quoted 7 lines
drivers/mailbox/tegra-hsp.c new file +7 -0
+static const struct mbox_chan_ops tegra_hsp_ops = {
+       .send_data = tegra_hsp_send_data,
+       .startup = tegra_hsp_startup,
+       .shutdown = tegra_hsp_shutdown,
+       .last_tx_done = tegra_hsp_last_tx_done,
+};
+

These 4 above seem overkill. Why not directly use tegra_hsp_doorbell_xxx() ?

Quoted 6 lines

This is in preparation for supporting the other synchronization primitives that the HSP IP block exposes. Some of them use different programming and semantics, hence why we want to have this second level of abstraction. It will allow us to share some of the code between the different primitives once their implementations are added.

OK, but until then this, and the above NULL check, will look silly. Usually we add only necessary code at any time.

Thanks.

#7 lore raw

PATCH v5 1/1 mailbox: Add Tegra HSP driver

In reply to Thierry Reding

From: Thierry Reding <[email protected]>

This driver exposes a mailbox interface for interprocessor communication using the Hardware Synchronization Primitives (HSP) module's doorbell mechanism. There are multiple HSP instances and they provide additional features such as shared mailboxes, shared and arbitrated semaphores.

A driver for a remote processor can use the mailbox client provided by the HSP driver and build an IPC protocol on top of this synchronization mechanism.

Based on work by Joseph Lo <[email protected]>.

---
Changes in v5:
- remove unused ->last_tx_done() callback
- get rid of extra layer of indirection
- drop useless to_tegra_hsp_channel()

Changes in v4:
- check for doorbell ring before enabling new master
- add missing space in Kconfig description
- remove unnecessary barriers
- simplify doorbell lookup
- remove debug leftovers

Changes in v3:
- use a more object oriented design
- fix some locking issues

 drivers/mailbox/Kconfig     |   9 +
 drivers/mailbox/Makefile    |   2 +
 drivers/mailbox/tegra-hsp.c | 479 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 490 insertions(+)
 create mode 100644 drivers/mailbox/tegra-hsp.c
drivers/mailbox/Kconfig +9 -0
index 11eebfe8a4cb..ceff415f201c 100644
@@ -124,6 +124,15 @@ config MAILBOX_TEST
 	  Test client to help with testing new Controller driver
 	  implementations.
 
+config TEGRA_HSP_MBOX
+	bool "Tegra HSP (Hardware Synchronization Primitives) Driver"
+	depends on ARCH_TEGRA_186_SOC
+	help
+	  The Tegra HSP driver is used for the interprocessor communication
+	  between different remote processors and host processors on Tegra186
+	  and later SoCs. Say Y here if you want to have this support.
+	  If unsure say N.
+
 config XGENE_SLIMPRO_MBOX
 	tristate "APM SoC X-Gene SLIMpro Mailbox Controller"
 	depends on ARCH_XGENE
drivers/mailbox/Makefile +2 -0
index ace6fed8fea9..7dde4f609ae8 100644
@@ -29,3 +29,5 @@ obj-$(CONFIG_XGENE_SLIMPRO_MBOX) += mailbox-xgene-slimpro.o
 obj-$(CONFIG_HI6220_MBOX)	+= hi6220-mailbox.o
 
 obj-$(CONFIG_BCM_PDC_MBOX)	+= bcm-pdc-mailbox.o
+
+obj-$(CONFIG_TEGRA_HSP_MBOX)	+= tegra-hsp.o
drivers/mailbox/tegra-hsp.c new file +479 -0

479 lines not on this page · raw message

Signature · 1 line
2.10.2
#6 lore raw
In reply to Jassi Brar
Quoted 87 lines · Jassi Brar
Quoted 78 lines · Thierry Reding
Quoted 28 lines · Jassi Brar

On Tue, Nov 15, 2016 at 9:18 PM, Thierry Reding
<[email protected]> wrote:

....

Quoted 13 lines
drivers/mailbox/tegra-hsp.c new file +13 -0
+
+struct tegra_hsp_channel;
+struct tegra_hsp;
+
+struct tegra_hsp_channel_ops {
+       int (*send_data)(struct tegra_hsp_channel *channel, void *data);
+       int (*startup)(struct tegra_hsp_channel *channel);
+       void (*shutdown)(struct tegra_hsp_channel *channel);
+       bool (*last_tx_done)(struct tegra_hsp_channel *channel);
+};
+
+struct tegra_hsp_channel {
+       struct tegra_hsp *hsp;

drivers/mailbox/tegra-hsp.c line 62

Quoted 10 lines
drivers/mailbox/tegra-hsp.c new file +10 -0
+       const struct tegra_hsp_channel_ops *ops;
+       struct mbox_chan *chan;
+       void __iomem *regs;
+};
+
+static struct tegra_hsp_channel *to_tegra_hsp_channel(struct mbox_chan *chan)
+{
+       return chan->con_priv;
+}
+

It seems

Quoted 3 lines
       channel = to_tegra_hsp_channel(chan);
is no simpler ritual than
       channel = chan->con_priv;   ?

Yes, that's true. I've dropped the to_tegra_hsp_channel() inline in favour of using the chan->con_priv directly.

Quoted 10 lines
Quoted 10 lines
drivers/mailbox/tegra-hsp.c new file +10 -0
+struct tegra_hsp_doorbell {
+       struct tegra_hsp_channel channel;
+       struct list_head list;
+       const char *name;
+       unsigned int master;
+       unsigned int index;
+};
+
+static struct tegra_hsp_doorbell *
+to_tegra_hsp_doorbell(struct tegra_hsp_channel *channel)
Quoted 8 lines

drivers/mailbox/tegra-hsp.c line 79

Quoted 7 lines
drivers/mailbox/tegra-hsp.c new file +7 -0
+{
+       if (!channel)
+               return NULL;
+
+       return container_of(channel, struct tegra_hsp_doorbell, channel);
+}
+

But you don't check for NULL returned, before dereferencing the pointer 'db'

In all the call sites where this is used the channel is guaranteed not to be NULL, hence no checking is necessary. However the function here could potentially be used in other cases where no such guarantees can be given and checking the !channel above is merely there to avoid casting to a non-NULL pointer from a NULL pointer.

I've run occasionally into this issue because container_of() will simply perform arithmetic on the pointer given, so passing channel as NULL would convert to some very large pointer that can no longer be easily discerned from an invalid pointer.

So this is primarily a safety feature, and one that I'd prefer to keep just to avoid running into issues down the road when the function gets used under different circumstances.

Quoted 7 lines

drivers/mailbox/tegra-hsp.c line 272

Quoted 4 lines
drivers/mailbox/tegra-hsp.c new file +4 -0
+static bool tegra_hsp_doorbell_last_tx_done(struct tegra_hsp_channel *channel)
+{
+       return true;
+}

Just curious, is the IPC done instantly after writing HSP_DB_TRIGGER bit? Usually there is at least some bit that stays (un)set as a 'busy flag'.

Quoted 7 lines

I don't think there's a bit like that for doorbells. The way that these doorbells are used is in combination with a shared memory IPC protocol. Two processors will communicate by writing to and reading from what is essentially a ring buffer in shared memory. The doorbells are merely a means of communicating their peer that a new entry is available in the shared memory.

Quoted 4 lines

For such protocols, we have the TXDONE_BY_ACK. I assume your client drivers will drive the state-machine. Otherwise, you risk overrunning the ring-buffer in SHM, but not caring if the first filled buffer was actually consumed by the remote (just like ALSA ring buffer).

I did some digging and it turns out that the driver is already using TXDONE_BY_ACK (it sets .txdone_irq = false and .txdone_poll = false) and indeed users are driving the TX state machine by calling the mbox_client_txdone() function where appropriate.

Given the above the .last_tx_done() callback is completely unused so I've just dropped it.

Quoted 46 lines
Quoted 46 lines
Quoted 45 lines
Quoted 7 lines
drivers/mailbox/tegra-hsp.c new file +7 -0
+static const struct tegra_hsp_channel_ops tegra_hsp_doorbell_ops = {
+       .send_data = tegra_hsp_doorbell_send_data,
+       .startup = tegra_hsp_doorbell_startup,
+       .shutdown = tegra_hsp_doorbell_shutdown,
+       .last_tx_done = tegra_hsp_doorbell_last_tx_done,
+};
+

....

Quoted 25 lines
drivers/mailbox/tegra-hsp.c new file +25 -0
+static int tegra_hsp_send_data(struct mbox_chan *chan, void *data)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->send_data(channel, data);
+}
+
+static int tegra_hsp_startup(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->startup(channel);
+}
+
+static void tegra_hsp_shutdown(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+
+       return channel->ops->shutdown(channel);
+}
+
+static bool tegra_hsp_last_tx_done(struct mbox_chan *chan)
+{
+       struct tegra_hsp_channel *channel = to_tegra_hsp_channel(chan);
+

drivers/mailbox/tegra-hsp.c line 352

Quoted 10 lines
drivers/mailbox/tegra-hsp.c new file +10 -0
+       return channel->ops->last_tx_done(channel);
+}
+
+static const struct mbox_chan_ops tegra_hsp_ops = {
+       .send_data = tegra_hsp_send_data,
+       .startup = tegra_hsp_startup,
+       .shutdown = tegra_hsp_shutdown,
+       .last_tx_done = tegra_hsp_last_tx_done,
+};
+

These 4 above seem overkill. Why not directly use tegra_hsp_doorbell_xxx() ?

Quoted 8 lines
Quoted 6 lines

This is in preparation for supporting the other synchronization primitives that the HSP IP block exposes. Some of them use different programming and semantics, hence why we want to have this second level of abstraction. It will allow us to share some of the code between the different primitives once their implementations are added.

OK, but until then this, and the above NULL check, will look silly. Usually we add only necessary code at any time.

I've removed the additional level of indirection, we can always add this back when/if required. As a side-effect the to_tegra_hsp_doorbell() goes away because it is unused, thereby removing your last concern as well.

I just sent out a v5 that should address all of your comments.

Thanks,
Thierry

maintainer Acked-by on v5 1/1
#8 lore raw
In reply to Thierry Reding
Quoted 7 lines · Thierry Reding

From: Thierry Reding <[email protected]>

This driver exposes a mailbox interface for interprocessor communication using the Hardware Synchronization Primitives (HSP) module's doorbell mechanism. There are multiple HSP instances and they provide additional features such as shared mailboxes, shared and arbitrated semaphores.

Quoted 7 lines

A driver for a remote processor can use the mailbox client provided by the HSP driver and build an IPC protocol on top of this synchronization mechanism.

Based on work by Joseph Lo <[email protected]>.

#9 lore raw
Quoted 13 lines · Jassi Brar
Quoted 11 lines · Thierry Reding

From: Thierry Reding <[email protected]>

This driver exposes a mailbox interface for interprocessor communication using the Hardware Synchronization Primitives (HSP) module's doorbell mechanism. There are multiple HSP instances and they provide additional features such as shared mailboxes, shared and arbitrated semaphores.

A driver for a remote processor can use the mailbox client provided by the HSP driver and build an IPC protocol on top of this synchronization mechanism.

Quoted 5 lines
Quoted 3 lines

Based on work by Joseph Lo <[email protected]>.

Thanks!

Thierry