Skip to contentSkip to conversation

Add MediaTek APU Mailbox Support For MT8196

PATCH In review Karl.Li 4 participants 9 messages Dec 30, 2024 – Jan 12, 2025

Subsystems (3)
lore
Share & tools
lore

Karl.Li

author
#0 lore raw

v2 cover Add MediaTek APU Mailbox Support For MT8196

From: Karl Li <[email protected]>

Based on tag: next-20241220, linux-next/master

Hello,

This patch series introduces support for the MediaTek APU (AI Processing Unit) mailbox, a crucial component for facilitating communication between the APU and other system processors within MediaTek platforms. The APU subsystem relies on a message-passing mechanism built atop the mailbox infrastructure, necessitating enhancements to the existing mailbox framework to accommodate the APU's communication requirements.

The series begins by adding the necessary device tree bindings for the APU mailbox, followed by an enhancement to the mailbox framework allowing for bottom-half processing of received data. This is particularly important for the APU's operation, as it relies on a combination of mailbox messages and shared memory for data exchange. Finally, we introduce the MediaTek APU mailbox driver itself, which implements the communication protocol and exposes additional hardware features for broader system integration.

Patch Summary:
1. dt-bindings: mailbox: mediatek: Add apu-mailbox document
- Introduces the device tree bindings necessary for describing the APU mailbox in device tree sources, enabling the kernel to correctly configure and utilize this component.

2. mailbox: add support for bottom half received data
- Enhances the mailbox framework to support sleepable contexts in the processing of received messages. This is critical for APU communication, where message handling may require operations that cannot be performed in atomic contexts.

3. mailbox: mediatek: Add mtk-apu-mailbox driver
- Adds the driver for the MediaTek APU mailbox, facilitating communication with the APU microprocessor and providing interfaces for other system components to interact with the APU through spare registers.

This work is a step towards fully integrating MediaTek's APU capabilities with the Linux kernel, enhancing support for AI features on MediaTek platforms.

Please review and provide feedback.

Best regards

---

Changes in v2
- [1/3]
- Add APU description in commit message and dt-bindings description.
- Remove mt8188 in dt-bindings to make this patch focus on mt8196.
- Drop "apu_mailbox" label since no one reference to it.
- Modify the rule of compatible property.
- Add cells number to resolve dt_binding_check error.
- [2/3]
- Add more description in commit message to describe why we need sleepable callback function.
- [3/3]
- Use regmap provide spare register operation.
- Remove global variable g_mbox.
- Add MTK_APU_MBOX prefix for each macro define.
- Only keep mt8196 compatible to make this patch focus on mt8196.
- Rename mbox_controller to mbox.
- Use mtk_apu_mbox_msg to define msgs in mtk_apu_mailbox.
- Rename top-half and bottom-half function.
- Change to pass mbox.chans to isr.
- Move platform_get_irq before register controller.
- Remove remove callback function.
- Rename send_cnt to data_cnt, and using u8 instead of int.
- Use platform data to support the available slots for different platforms.
- Use memory copy to sync mailbox data.
- Use outbox irq bits to indicate the received slots number.

---

Karl Li (3):
dt-bindings: mailbox: mediatek: Add apu-mailbox dt-bindings
mailbox: add support for bottom half received data
mailbox: mediatek: Add mtk-apu-mailbox driver

 .../mailbox/mediatek,apu-mailbox.yaml         |  60 ++++++
 drivers/mailbox/Kconfig                       |   9 +
 drivers/mailbox/Makefile                      |   2 +
 drivers/mailbox/mailbox.c                     |  16 ++
 drivers/mailbox/mtk-apu-mailbox.c             | 202 ++++++++++++++++++
 include/linux/mailbox/mtk-apu-mailbox.h       |  15 ++
 include/linux/mailbox_client.h                |   2 +
 include/linux/mailbox_controller.h            |   1 +
 8 files changed, 307 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mailbox/mediatek,apu-mailbox.yaml
 create mode 100644 drivers/mailbox/mtk-apu-mailbox.c
 create mode 100644 include/linux/mailbox/mtk-apu-mailbox.h
Signature · 1 line
2.18.0
author
#1 lore raw

PATCH v2 1/3 dt-bindings: mailbox: mediatek: Add apu-mailbox dt-bindings

From: Karl Li <[email protected]>

Add devicetree binding for apu-mailbox.

MediaTek APU (AI Processing Unit) is a microcontroller unit designed for AI acceleration tasks. The MediaTek APU-Mailbox facilitates communication with the APU microcontroller.

---
 .../mailbox/mediatek,apu-mailbox.yaml         | 60 +++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mailbox/mediatek,apu-mailbox.yaml
Documentation/devicetree/bindings/mailbox/mediatek,apu-mailbox.yaml new file +60 -0
new file mode 100644
index 000000000000..fdb0dec5fb78
@@ -0,0 +1,60 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mailbox/mediatek,apu-mailbox.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek APU mailbox
+
+maintainers:
+  - Karl Li <[email protected]>
+
+description:
+  MediaTek APU (AI Processing Unit) is a microcontroller unit
+  designed for AI acceleration tasks. The MediaTek APU-Mailbox
+  facilitates communication with the APU microcontroller.
+  Within the MediaTek APU subsystem, a message passing mechanism is
+  built on top of the mailbox system. The mailbox only has limited
+  space for each message. The firmware expects the message header
+  from the mailbox, while the message body is passed through some
+  fixed shared memory.
+
+properties:
+  compatible:
+    const: mediatek,mt8196-apu-mailbox
+
+  "#mbox-cells":
+    const: 1
+    description:
+      The cell describe which channel the device will use.
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - "#mbox-cells"
+  - reg
+  - interrupts
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    soc {
+      #address-cells = <2>;
+      #size-cells = <2>;
+
+      mailbox@4c200000 {
+        compatible = "mediatek,mt8196-apu-mailbox";
+        reg = <0 0x4c200000 0 0xfffff>;
+        interrupts = <GIC_SPI 638 IRQ_TYPE_LEVEL_HIGH 0>;
+        #mbox-cells = <1>;
+      };
+    };
Signature · 1 line
2.18.0
#2 lore raw

Re: [PATCH v2 1/3] dt-bindings: mailbox: mediatek: Add apu-mailbox dt-bindings

Quoted 3 lines · Karl.Li

From: Karl Li <[email protected]>

Add devicetree binding for apu-mailbox.

A nit, subject: drop second/last, redundant "dt-bindings". The "dt-bindings" prefix is already stating that these are bindings. See also:
https://elixir.bootlin.com/linux/v6.7-rc8/source/Documentation/devicetree/bindings/submitting-patches.rst#L18

I already asked for this.

Quoted 4 lines

MediaTek APU (AI Processing Unit) is a microcontroller unit designed for AI acceleration tasks. The MediaTek APU-Mailbox facilitates communication with the APU microcontroller.

Please wrap commit message according to Linux coding style / submission process (neither too early nor over the limit):
https://elixir.bootlin.com/linux/v6.4-rc1/source/Documentation/process/submitting-patches.rst#L597

Documentation/devicetree/bindings/mailbox/mediatek,apu-mailbox.yaml

Quoted 12 lines
---
 .../mailbox/mediatek,apu-mailbox.yaml         | 60 +++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mailbox/mediatek,apu-mailbox.yaml
Documentation/devicetree/bindings/mailbox/mediatek,apu-mailbox.yaml new file +0 -0
new file mode 100644
index 000000000000..fdb0dec5fb78

Use compatible as filename.

Best regards,
Krzysztof

author
#3 lore raw

PATCH v2 2/3 mailbox: add support for bottom half received data

In reply to Karl.Li

From: Karl Li <[email protected]>

Within the MediaTek APU subsystem, a message passing mechanism is constructed on top of the mailbox system.

The mailbox only has limited space for each message. The MTK APU firmware expects the message header from the mailbox, while the message body is passed through some fixed shared memory.

The mailbox interrupt also serves as a mutex for the shared memory. Thus the interrupt may only be cleared after the message is handled.

To be specific, the MTK APU firmware fills the message body into a fixed shared memory, while passing the message header via a mailbox.

--------------- --------------- ---------

| Kernel Buffer |  <-  | Shared Memory |  <-  | MTK APU |
 ---------------        ---------------        ---------

The top-half handler first copies the data from the shared memory to the kernel buffer. Subsequently, the bottom-half handler uses the data in the kernel buffer for specific processing tasks.

However, it is possible for the MTK APU to send a new message while the kernel is still processing the previous one. In such cases, the top-half handler may overwrite the kernel buffer while the data is still needed, leading to a race condition.

Additionally, due to performance considerations, we cannot allocate a new kernel buffer for each message.

To prevent this, we need a mechanism to protect the kernel buffer from such race conditions. Since we cannot use locks to protect the kernel buffer in an atomic context, we use the mailbox interrupt as a mutex for the shared memory to further protect our kernel buffer. Therefore, we clear the interrupt only after the message is handled.

Since the bottom half callback might need to go to sleep, we require a sleepable callback for mailbox clients.

This patch adds a new sleepable RX callback for mailbox clients for cases where handling the incoming message requires sleeping.

---
 drivers/mailbox/mailbox.c          | 16 ++++++++++++++++
 include/linux/mailbox_client.h     |  2 ++
 include/linux/mailbox_controller.h |  1 +
 3 files changed, 19 insertions(+)
drivers/mailbox/mailbox.c +16 -0
index d3d26a2c9895..d58a77fcf804 100644
@@ -164,6 +164,22 @@ void mbox_chan_received_data(struct mbox_chan *chan, void *mssg)
 }
 EXPORT_SYMBOL_GPL(mbox_chan_received_data);
 
+/**
+ * mbox_chan_received_data_bh - A way for controller driver to push data
+ *				received from remote to the upper layer.
+ * @chan: Pointer to the mailbox channel on which RX happened.
+ * @mssg: Client specific message typecasted as void *
+ *
+ * For the operations which is not atomic can be called from
+ * mbox_chan_received_data_bh().
+ */
+void mbox_chan_received_data_bh(struct mbox_chan *chan, void *mssg)
+{
+	if (chan->cl->rx_callback_bh)
+		chan->cl->rx_callback_bh(chan->cl, mssg);
+}
+EXPORT_SYMBOL_GPL(mbox_chan_received_data_bh);
+
 /**
  * mbox_chan_txdone - A way for controller driver to notify the
  *			framework that the last TX has completed.
include/linux/mailbox_client.h +2 -0
index 734694912ef7..2cc6fa4e1bf9 100644
@@ -22,6 +22,7 @@ struct mbox_chan;
  *			if the client receives some ACK packet for transmission.
  *			Unused if the controller already has TX_Done/RTR IRQ.
  * @rx_callback:	Atomic callback to provide client the data received
+ * @rx_callback_bh:	Non-atomic callback to provide client the data received
  * @tx_prepare: 	Atomic callback to ask client to prepare the payload
  *			before initiating the transmission if required.
  * @tx_done:		Atomic callback to tell client of data transmission
@@ -33,6 +34,7 @@ struct mbox_client {
 	bool knows_txdone;
 
 	void (*rx_callback)(struct mbox_client *cl, void *mssg);
+	void (*rx_callback_bh)(struct mbox_client *cl, void *mssg);
 	void (*tx_prepare)(struct mbox_client *cl, void *mssg);
 	void (*tx_done)(struct mbox_client *cl, void *mssg, int r);
 };
include/linux/mailbox_controller.h +1 -0
index 6fee33cb52f5..74c6a31cd313 100644
@@ -130,6 +130,7 @@ struct mbox_chan {
 int mbox_controller_register(struct mbox_controller *mbox); /* can sleep */
 void mbox_controller_unregister(struct mbox_controller *mbox); /* can sleep */
 void mbox_chan_received_data(struct mbox_chan *chan, void *data); /* atomic */
+void mbox_chan_received_data_bh(struct mbox_chan *chan, void *data); /* can sleep */
 void mbox_chan_txdone(struct mbox_chan *chan, int r); /* atomic */
 
 int devm_mbox_controller_register(struct device *dev,
Signature · 1 line
2.18.0
author
#4 lore raw

PATCH v2 3/3 mailbox: mediatek: Add mtk-apu-mailbox driver

In reply to Karl.Li

From: Karl Li <[email protected]>

Add mtk-apu-mailbox driver to support the communication with APU remote microprocessor.

Also, the mailbox hardware contains extra spare (scratch) registers that other hardware blocks use to communicate through. Use regmap to provide the such operations.

---
 drivers/mailbox/Kconfig                 |   9 ++
 drivers/mailbox/Makefile                |   2 +
 drivers/mailbox/mtk-apu-mailbox.c       | 202 ++++++++++++++++++++++++
 include/linux/mailbox/mtk-apu-mailbox.h |  15 ++
 4 files changed, 228 insertions(+)
 create mode 100644 drivers/mailbox/mtk-apu-mailbox.c
 create mode 100644 include/linux/mailbox/mtk-apu-mailbox.h
drivers/mailbox/Kconfig +9 -0
index 8ecba7fb999e..7cea37e7b9df 100644
@@ -241,6 +241,15 @@ config MTK_ADSP_MBOX
           between processors with ADSP. It will place the message to share
 	  buffer and will access the ipc control.
 
+config MTK_APU_MBOX
+	tristate "MediaTek APU Mailbox Support"
+	depends on ARCH_MEDIATEK || COMPILE_TEST
+	help
+	  Say yes here to add support for the MediaTek APU Mailbox
+	  driver. The mailbox implementation provides access from the
+	  application processor to the MediaTek AI Processing Unit.
+	  If unsure say N.
+
 config MTK_CMDQ_MBOX
 	tristate "MediaTek CMDQ Mailbox Support"
 	depends on ARCH_MEDIATEK || COMPILE_TEST
drivers/mailbox/Makefile +2 -0
index 5f4f5b0ce2cc..0b992be69f6f 100644
@@ -53,6 +53,8 @@ obj-$(CONFIG_STM32_IPCC) 	+= stm32-ipcc.o
 
 obj-$(CONFIG_MTK_ADSP_MBOX)	+= mtk-adsp-mailbox.o
 
+obj-$(CONFIG_MTK_APU_MBOX)	+= mtk-apu-mailbox.o
+
 obj-$(CONFIG_MTK_CMDQ_MBOX)	+= mtk-cmdq-mailbox.o
 
 obj-$(CONFIG_ZYNQMP_IPI_MBOX)	+= zynqmp-ipi-mailbox.o
drivers/mailbox/mtk-apu-mailbox.c new file +202 -0

202 lines not on this page · raw message

include/linux/mailbox/mtk-apu-mailbox.h new file +15 -0
new file mode 100644
index 000000000000..0afce8848205
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2024 MediaTek Inc.
+ *
+ */
+
+#ifndef __MTK_APU_MAILBOX_H__
+#define __MTK_APU_MAILBOX_H__
+
+struct mtk_apu_mailbox_msg {
+	u8 data_cnt;
+	u32 *data;
+};
+
+#endif /* __MTK_APU_MAILBOX_H__ */
Signature · 1 line
2.18.0
maintainer
#5 lore raw

Re: [PATCH v2 3/3] mailbox: mediatek: Add mtk-apu-mailbox driver

On Sun, Dec 29, 2024 at 9:46 PM Karl.Li <[email protected]> wrote:
....

Quoted 6 lines
drivers/mailbox/mtk-apu-mailbox.c new file +6 -0
+
+static irqreturn_t mtk_apu_mailbox_irq(int irq, void *data)
+{
+       struct mbox_chan *chan = data;
+       struct mtk_apu_mailbox *apu_mbox = get_mtk_apu_mailbox(chan->mbox);
+       struct mbox_chan *link = &apu_mbox->mbox.chans[0];

drivers/mailbox/mtk-apu-mailbox.c line 59

Quoted 10 lines
drivers/mailbox/mtk-apu-mailbox.c new file +10 -0
+       u8 data_cnt = fls(readl(apu_mbox->regs + MTK_APU_MBOX_OUTBOX_IRQ));
+
+       memcpy_fromio(apu_mbox->msgs.data, apu_mbox->regs + MTK_APU_MBOX_OUTBOX,
+                     sizeof(*apu_mbox->msgs.data) * data_cnt);
+
+       mbox_chan_received_data(link, apu_mbox->msgs.data);
+
+       return IRQ_WAKE_THREAD;
+}
+

You don't seem to do anything that 'ack' the irq line. So if you merge this into mtk_apu_mailbox_irq_thread() and do
devm_request_threaded_irq(dev, irq, NULL,
mtk_apu_mailbox_irq_thread, IRQF_ONESHOT, ...);
you can avoid adding a new api and keep your client code simple. mbox_chan_received_data() is strictly between the client and controller driver. If it is not called from an atomic context, feel free to do memcpy() from it.

Cheers!
Jassi

#6 lore raw

Re: [PATCH v2 3/3] mailbox: mediatek: Add mtk-apu-mailbox driver

Quoted 19 lines · Jassi Brar

On Sun, Dec 29, 2024 at 9:46 PM Karl.Li <[email protected]> wrote:
....

Quoted 6 lines
drivers/mailbox/mtk-apu-mailbox.c new file +6 -0
+
+static irqreturn_t mtk_apu_mailbox_irq(int irq, void *data)
+{
+       struct mbox_chan *chan = data;
+       struct mtk_apu_mailbox *apu_mbox = get_mtk_apu_mailbox(chan->mbox);
+       struct mbox_chan *link = &apu_mbox->mbox.chans[0];

drivers/mailbox/mtk-apu-mailbox.c line 59

Quoted 10 lines
drivers/mailbox/mtk-apu-mailbox.c new file +10 -0
+       u8 data_cnt = fls(readl(apu_mbox->regs + MTK_APU_MBOX_OUTBOX_IRQ));
+
+       memcpy_fromio(apu_mbox->msgs.data, apu_mbox->regs + MTK_APU_MBOX_OUTBOX,
+                     sizeof(*apu_mbox->msgs.data) * data_cnt);
+
+       mbox_chan_received_data(link, apu_mbox->msgs.data);
+
+       return IRQ_WAKE_THREAD;
+}
+
Quoted 8 lines

You don't seem to do anything that 'ack' the irq line. So if you merge this into mtk_apu_mailbox_irq_thread() and do
devm_request_threaded_irq(dev, irq, NULL,
mtk_apu_mailbox_irq_thread, IRQF_ONESHOT, ...);
you can avoid adding a new api and keep your client code simple. mbox_chan_received_data() is strictly between the client and controller driver. If it is not called from an atomic context, feel free to do memcpy() from it.

That sounds great. Could we perhaps update the API documentation and drop the "atomic" requirement, or update it to something you describe here as "between the client and controller driver".

Disclosure: I've been doing pre-reviews for this series and we were operating under the assumption that mbox_chan_received_data() and its callbacks must work under atomic contexts.

Thanks
ChenYu

maintainer
#7 lore raw

Re: [PATCH v2 3/3] mailbox: mediatek: Add mtk-apu-mailbox driver

Quoted 30 lines · Chen-Yu Tsai
Quoted 19 lines · Jassi Brar

On Sun, Dec 29, 2024 at 9:46 PM Karl.Li <[email protected]> wrote:
....

Quoted 6 lines
drivers/mailbox/mtk-apu-mailbox.c new file +6 -0
+
+static irqreturn_t mtk_apu_mailbox_irq(int irq, void *data)
+{
+       struct mbox_chan *chan = data;
+       struct mtk_apu_mailbox *apu_mbox = get_mtk_apu_mailbox(chan->mbox);
+       struct mbox_chan *link = &apu_mbox->mbox.chans[0];

drivers/mailbox/mtk-apu-mailbox.c line 59

Quoted 10 lines
drivers/mailbox/mtk-apu-mailbox.c new file +10 -0
+       u8 data_cnt = fls(readl(apu_mbox->regs + MTK_APU_MBOX_OUTBOX_IRQ));
+
+       memcpy_fromio(apu_mbox->msgs.data, apu_mbox->regs + MTK_APU_MBOX_OUTBOX,
+                     sizeof(*apu_mbox->msgs.data) * data_cnt);
+
+       mbox_chan_received_data(link, apu_mbox->msgs.data);
+
+       return IRQ_WAKE_THREAD;
+}
+
Quoted 8 lines

You don't seem to do anything that 'ack' the irq line. So if you merge this into mtk_apu_mailbox_irq_thread() and do
devm_request_threaded_irq(dev, irq, NULL,
mtk_apu_mailbox_irq_thread, IRQF_ONESHOT, ...);
you can avoid adding a new api and keep your client code simple. mbox_chan_received_data() is strictly between the client and controller driver. If it is not called from an atomic context, feel free to do memcpy() from it.

Quoted 4 lines

That sounds great. Could we perhaps update the API documentation and drop the "atomic" requirement, or update it to something you describe here as "between the client and controller driver".

Not drop, but simply add a line that the client needs to respect the context in which the controller does the callback.

thanks

#8 lore raw
In reply to Karl.Li
Quoted 8 lines · Karl.Li

From: Karl Li <[email protected]>

Based on tag: next-20241220, linux-next/master

Hello,

This patch series introduces support for the MediaTek APU (AI Processing Unit) mailbox, a crucial component for facilitating communication between the APU and other system processors within MediaTek platforms. The APU subsystem relies on a message-passing mechanism built atop the mailbox infrastructure, necessitating enhancements to the existing mailbox framework to accommodate the APU's communication requirements.

Wrap your patch descriptions.

https://elixir.bootlin.com/linux/v6.4-rc1/source/Documentation/process/submitting-patches.rst#L597

Best regards,
Krzysztof