[PATCH v2 16/28] drm/panthor: Add AM_RESOURCE_GROUP support

From: Karunika Choo

Date: Tue Nov 10 2026 - 12:14:30 EST


Add resource-group probing for the arbitration device, including MMIO
resources, IRQ handling, per-AW AM message helpers, handshake
negotiation, and retry work for deferred outgoing messages.

Hook resource groups into arbitration init, teardown, and runtime PM.

Signed-off-by: Karunika Choo <karunika.choo@xxxxxxx>
---
v2:
- Revert child node parsing from v1.
- Tighten protocol validation and reject incompatible messages.
- Drain pending messages on suspend.
- Fixes the following issues raised by sashiko:
- Simplify and clean up rutnime PM handling and termination paths.
- change message retry work to delayed work.
- Add irq_lock to prevent MMIO access as a result of shared IRQ while
GPU is powered off.
- Fix irq number storage.
- Add check for allocation failure from devm_kasprintf
- Link: https://lore.kernel.org/all/20260922205638.7B1DE1F000FF@xxxxxxxxxxxxxxx/
v1:
- Updated DT structure and accompanying parsing to handle child nodes
instead of named platform resources.
- Hold runtime PM reference during message retries.
- Fix suspend resume ordering.

drivers/gpu/drm/panthor/arbitration/Makefile | 3 +-
.../panthor/arbitration/panthor_arbitration.h | 5 +
.../arbitration/panthor_arbitration_drv.c | 15 +
.../arbitration/panthor_resource_group.c | 373 ++++++++++++++++++
.../arbitration/panthor_resource_group.h | 20 +
5 files changed, 415 insertions(+), 1 deletion(-)
create mode 100644 drivers/gpu/drm/panthor/arbitration/panthor_resource_group.c
create mode 100644 drivers/gpu/drm/panthor/arbitration/panthor_resource_group.h

diff --git a/drivers/gpu/drm/panthor/arbitration/Makefile b/drivers/gpu/drm/panthor/arbitration/Makefile
index b0b6b2e3edf8d..2d2684321d978 100644
--- a/drivers/gpu/drm/panthor/arbitration/Makefile
+++ b/drivers/gpu/drm/panthor/arbitration/Makefile
@@ -4,7 +4,8 @@
obj-$(CONFIG_DRM_PANTHOR_ARBITRATION) += panthor_arbitration.o
panthor_arbitration-y := \
panthor_arbitration_drv.o \
- panthor_partition_control.o
+ panthor_partition_control.o \
+ panthor_resource_group.o

INCLUDES = \
-I$(src)/..
diff --git a/drivers/gpu/drm/panthor/arbitration/panthor_arbitration.h b/drivers/gpu/drm/panthor/arbitration/panthor_arbitration.h
index fc3610aefae29..9612bfc6c4417 100644
--- a/drivers/gpu/drm/panthor/arbitration/panthor_arbitration.h
+++ b/drivers/gpu/drm/panthor/arbitration/panthor_arbitration.h
@@ -9,8 +9,10 @@

struct device;
struct panthor_partition_control;
+struct panthor_resource_group;

#define AM_ARB_MAX_PC_COUNT 1
+#define AM_ARB_MAX_RG_COUNT 1
#define AM_ARB_MAX_AW_COUNT 16

/**
@@ -22,6 +24,9 @@ struct panthor_arbitration {

/** @pc: Pointer array to partition control data */
struct panthor_partition_control *pc[AM_ARB_MAX_PC_COUNT];
+
+ /** @rg: Pointer array to resource group data */
+ struct panthor_resource_group *rg[AM_ARB_MAX_RG_COUNT];
};

#endif
diff --git a/drivers/gpu/drm/panthor/arbitration/panthor_arbitration_drv.c b/drivers/gpu/drm/panthor/arbitration/panthor_arbitration_drv.c
index a27d24c3679fa..0e8ffd3c1e6da 100644
--- a/drivers/gpu/drm/panthor/arbitration/panthor_arbitration_drv.c
+++ b/drivers/gpu/drm/panthor/arbitration/panthor_arbitration_drv.c
@@ -12,6 +12,7 @@

#include "panthor_arbitration.h"
#include "panthor_partition_control.h"
+#include "panthor_resource_group.h"

#define PANTHOR_PM_AUTOSUSPEND_DELAY_MS 100

@@ -20,6 +21,10 @@ static int panthor_arbitration_runtime_suspend(struct device *dev)
struct panthor_arbitration *adev = dev_get_drvdata(dev);
int ret = 0;

+ ret = panthor_resource_group_suspend(adev);
+ if (ret)
+ return ret;
+
ret = panthor_partition_control_suspend(adev);
if (ret)
return ret;
@@ -36,6 +41,10 @@ static int panthor_arbitration_runtime_resume(struct device *dev)
if (ret)
return ret;

+ ret = panthor_resource_group_resume(adev);
+ if (ret)
+ return ret;
+
return 0;
}

@@ -66,6 +75,10 @@ static int panthor_arbitration_probe(struct platform_device *pdev)
if (ret)
goto err_term;

+ ret = panthor_resource_group_init(adev);
+ if (ret)
+ goto err_term;
+
ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
if (ret)
goto err_depopulate;
@@ -81,6 +94,7 @@ static int panthor_arbitration_probe(struct platform_device *pdev)
of_platform_depopulate(dev);

err_term:
+ panthor_resource_group_term(adev);
panthor_partition_control_term(adev);
pm_runtime_put_sync_suspend(dev);

@@ -106,6 +120,7 @@ static void panthor_arbitration_remove(struct platform_device *pdev)
pm_runtime_disable(adev->dev);

panthor_partition_control_term(adev);
+ panthor_resource_group_term(adev);

if (ret >= 0)
pm_runtime_put_noidle(adev->dev);
diff --git a/drivers/gpu/drm/panthor/arbitration/panthor_resource_group.c b/drivers/gpu/drm/panthor/arbitration/panthor_resource_group.c
new file mode 100644
index 0000000000000..e413cfd9e6d25
--- /dev/null
+++ b/drivers/gpu/drm/panthor/arbitration/panthor_resource_group.c
@@ -0,0 +1,373 @@
+// SPDX-License-Identifier: GPL-2.0 or MIT
+/* Copyright 2026 ARM Limited. All rights reserved. */
+
+#include <linux/interrupt.h>
+#include <linux/iopoll.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/property.h>
+#include <linux/workqueue.h>
+
+#include "panthor_am_msg.h"
+#include "panthor_arbitration.h"
+#include "panthor_device_io.h"
+#include "panthor_resource_group.h"
+
+#define AM_RG_IRQ_RAWSTAT 0x80
+#define AM_RG_IRQ_CLEAR 0x84
+#define AM_RG_IRQ_MASK 0x88
+#define AM_RG_IRQ_STATUS 0x8C
+#define AM_RG_MESSAGE_MASK GENMASK(15, 0)
+
+#define AM_RG_AW_MESSAGE(n) (0x100 + (n) * 0x20)
+
+#define AM_RG_RETRY_DELAY_US 10
+
+struct rg_aw_data {
+ /** @msg: Messaging data */
+ struct panthor_am_msg msg;
+};
+
+struct panthor_resource_group {
+ /** @dev: Device pointer */
+ struct device *dev;
+
+ /** @name: Resource name */
+ char *name;
+
+ /** @iomem: CPU mapping of the partition control IOMEM region */
+ void __iomem *iomem;
+
+ /** @irq: IRQ number */
+ int irq;
+
+ /** @irq_lock: Lock protecting IRQ state. */
+ spinlock_t irq_lock;
+
+ /** @irq_suspended: Flag indicating if IRQ was suspended. */
+ bool irq_suspended;
+
+ /** @aw_data: Data for each access window */
+ struct rg_aw_data aw_data[AM_ARB_MAX_AW_COUNT];
+
+ /** @wq: Resource group workqueue */
+ struct workqueue_struct *wq;
+
+ /** @msg_retry_work: Work to retry sending pending messages */
+ struct delayed_work msg_retry_work;
+};
+
+static void rg_retry_messages(struct work_struct *work)
+{
+ struct panthor_resource_group *rg = container_of(
+ work, struct panthor_resource_group, msg_retry_work.work);
+ bool retry = false;
+ int ret;
+
+ ret = pm_runtime_resume_and_get(rg->dev);
+ if (ret) {
+ dev_warn(rg->dev, "Failed to resume to retry messages");
+ return;
+ }
+
+ for (u8 aw_id = 0; aw_id < AM_ARB_MAX_AW_COUNT; aw_id++) {
+ struct panthor_am_msg *msg = &rg->aw_data[aw_id].msg;
+ int ret;
+
+ ret = panthor_am_msg_retry(msg);
+ if (ret == -EBUSY || ret == -EAGAIN)
+ retry = true;
+
+ if (ret == -EINVAL)
+ dev_warn(rg->dev, "AW%u send FIFO unexpectedly empty", aw_id);
+ }
+
+ if (retry)
+ mod_delayed_work(rg->wq, &rg->msg_retry_work,
+ usecs_to_jiffies(AM_RG_RETRY_DELAY_US));
+
+ pm_runtime_put_autosuspend(rg->dev);
+}
+
+static void rg_send_msg(struct panthor_resource_group *rg, u8 aw_id,
+ u64 message)
+{
+ int ret;
+
+ ret = panthor_am_msg_send(&rg->aw_data[aw_id].msg, message);
+ if (ret == -ENOSPC)
+ dev_err(rg->dev, "AW%u send FIFO is full", aw_id);
+
+ if (ret == -EBUSY) {
+ dev_dbg(rg->dev, "AW%u has pending messages, scheduling retry work", aw_id);
+ mod_delayed_work(rg->wq, &rg->msg_retry_work,
+ usecs_to_jiffies(AM_RG_RETRY_DELAY_US));
+ }
+}
+
+static void rg_send_init_msg(struct panthor_resource_group *rg, u8 aw_id,
+ u8 ack, u8 version)
+{
+ u64 message = ARB_VM_INIT_MAKE(ack, version);
+
+ rg_send_msg(rg, aw_id, message);
+}
+
+static void rg_respond_to_handshake(struct panthor_resource_group *rg,
+ u8 aw_id, u64 message)
+{
+ struct rg_aw_data *aw_data = &rg->aw_data[aw_id];
+ bool acked = AM_MSG_ACK_GET(message);
+ u8 version = AM_MSG_VERSION_GET(message);
+ u8 reply_version = AM_MSG_CURRENT_VERSION;
+ int ret;
+
+ ret = panthor_am_msg_version_validate(&aw_data->msg, version, acked,
+ &reply_version);
+ if (ret == -EOPNOTSUPP)
+ dev_warn(rg->dev,
+ "AW%u protocol version (%u) not supported (min: %u, max: %u)",
+ aw_id, version, AM_MSG_MIN_SUPPORTED_VERSION,
+ AM_MSG_CURRENT_VERSION);
+
+ /* AW initiated the handshake, reply with supported version */
+ if (!acked)
+ rg_send_init_msg(rg, aw_id, 1, reply_version);
+}
+
+static void rg_handshake_init(struct panthor_resource_group *rg)
+{
+ for (u8 aw_id = 0; aw_id < AM_ARB_MAX_AW_COUNT; aw_id++)
+ rg_send_init_msg(rg, aw_id, 0, AM_MSG_CURRENT_VERSION);
+}
+
+static void rg_handle_message(struct panthor_resource_group *rg, u8 aw_id,
+ u64 message)
+{
+ struct rg_aw_data *aw_data = &rg->aw_data[aw_id];
+ u8 msg_id = AM_MSG_ID_GET(message);
+
+ if (!msg_id)
+ return;
+
+ if (msg_id == VM_ARB_INIT) {
+ rg_respond_to_handshake(rg, aw_id, message);
+ return;
+ }
+
+ if (!panthor_am_msg_supported(&aw_data->msg)) {
+ dev_warn_ratelimited(
+ rg->dev, "Unsupported AW%u (message protocol)", aw_id);
+ return;
+ }
+
+ switch (msg_id) {
+ case VM_ARB_GPU_REQUEST:
+ /* TODO: on_request */
+ break;
+ case VM_ARB_GPU_STOPPED:
+ /* TODO: on_idle */
+ break;
+ default:
+ dev_warn(rg->dev, "Invalid message (0x%llx)", message);
+ break;
+ }
+}
+
+static irqreturn_t rg_irq_raw_handler(int irq, void *data)
+{
+ struct panthor_resource_group *rg = data;
+ unsigned long message_mask;
+ u32 status;
+ u8 aw_id;
+
+ scoped_guard(spinlock_irqsave, &rg->irq_lock) {
+ if (rg->irq_suspended)
+ return IRQ_NONE;
+ }
+
+ status = gpu_read(rg->iomem, AM_RG_IRQ_STATUS);
+ if (!status)
+ return IRQ_NONE;
+
+ message_mask = status;
+ for_each_set_bit(aw_id, &message_mask, AM_ARB_MAX_AW_COUNT) {
+ u64 message = panthor_am_msg_read(&rg->aw_data[aw_id].msg);
+
+ rg_handle_message(rg, aw_id, message);
+ }
+
+ gpu_write(rg->iomem, AM_RG_IRQ_CLEAR, status);
+
+ return IRQ_HANDLED;
+}
+
+static void rg_drain_pending_messages(struct panthor_resource_group *rg)
+{
+ unsigned long message_mask;
+ u8 aw_id;
+
+ if (pm_runtime_status_suspended(rg->dev))
+ return;
+
+ message_mask = gpu_read(rg->iomem, AM_RG_IRQ_RAWSTAT) & AM_RG_MESSAGE_MASK;
+
+ for_each_set_bit(aw_id, &message_mask, AM_ARB_MAX_AW_COUNT) {
+ u64 message = panthor_am_msg_read(&rg->aw_data[aw_id].msg);
+
+ rg_handle_message(rg, aw_id, message);
+ }
+
+ gpu_write(rg->iomem, AM_RG_IRQ_CLEAR, message_mask);
+}
+
+static void rg_irq_suspend(struct panthor_resource_group *rg)
+{
+ scoped_guard(spinlock_irqsave, &rg->irq_lock) {
+ rg->irq_suspended = true;
+
+ if (!pm_runtime_status_suspended(rg->dev))
+ gpu_write(rg->iomem, AM_RG_IRQ_MASK, 0);
+ }
+
+ synchronize_irq(rg->irq);
+}
+
+static void rg_irq_resume(struct panthor_resource_group *rg)
+{
+ guard(spinlock_irqsave)(&rg->irq_lock);
+ rg->irq_suspended = false;
+ gpu_write(rg->iomem, AM_RG_IRQ_MASK, AM_RG_MESSAGE_MASK);
+}
+
+int panthor_resource_group_suspend(struct panthor_arbitration *adev)
+{
+ for (int i = 0; i < AM_ARB_MAX_RG_COUNT; i++) {
+ struct panthor_resource_group *rg = adev->rg[i];
+
+ if (!rg)
+ continue;
+
+ rg_irq_suspend(rg);
+ rg_drain_pending_messages(rg);
+ }
+
+ return 0;
+}
+
+int panthor_resource_group_resume(struct panthor_arbitration *adev)
+{
+ for (int i = 0; i < AM_ARB_MAX_RG_COUNT; i++) {
+ struct panthor_resource_group *rg = adev->rg[i];
+
+ if (!rg)
+ continue;
+
+ rg_irq_resume(rg);
+ }
+
+ return 0;
+}
+
+void panthor_resource_group_term(struct panthor_arbitration *adev)
+{
+ for (int i = 0; i < AM_ARB_MAX_RG_COUNT; i++) {
+ struct panthor_resource_group *rg = adev->rg[i];
+
+ if (!rg)
+ continue;
+
+ disable_delayed_work_sync(&rg->msg_retry_work);
+ rg_irq_suspend(rg);
+ }
+}
+
+static int resource_group_init(struct panthor_arbitration *adev, int i)
+{
+ struct panthor_resource_group *rg;
+ struct device *dev = adev->dev;
+ struct irq_data *irqdata;
+ struct resource *res;
+ void __iomem *iomem;
+ u32 irq_flags = 0;
+ char *name;
+ int irq;
+ int ret;
+
+ if (adev->rg[i])
+ return dev_warn_probe(dev, -ENODEV,
+ "resource-group %u already present", i);
+
+ rg = devm_kzalloc(dev, sizeof(*rg), GFP_KERNEL);
+ if (!rg)
+ return -ENOMEM;
+
+ name = devm_kasprintf(dev, GFP_KERNEL, "rg%d", i);
+ if (!name)
+ return -ENOMEM;
+
+ res = platform_get_resource_byname(to_platform_device(dev),
+ IORESOURCE_MEM, name);
+ if (!res)
+ return -ENODEV;
+
+ iomem = devm_ioremap_resource(dev, res);
+ if (IS_ERR(iomem))
+ return dev_err_probe(dev, PTR_ERR(iomem), "%s: Failed to ioremap", name);
+
+ rg->name = name;
+ rg->dev = dev;
+ rg->iomem = iomem;
+
+ spin_lock_init(&rg->irq_lock);
+ rg->irq_suspended = true;
+
+ rg->wq = devm_alloc_ordered_workqueue(dev, "panthor-%s-wq", WQ_FREEZABLE, name);
+ if (!rg->wq)
+ return -ENOMEM;
+
+ INIT_DELAYED_WORK(&rg->msg_retry_work, rg_retry_messages);
+
+ for (int i = 0; i < AM_ARB_MAX_AW_COUNT; i++)
+ panthor_am_msg_init(&rg->aw_data[i].msg,
+ rg->iomem + AM_RG_AW_MESSAGE(i));
+
+ irq = platform_get_irq_byname(to_platform_device(dev), name);
+ if (irq < 0)
+ return irq;
+
+ rg->irq = irq;
+
+ irqdata = irq_get_irq_data(irq);
+ if (irqdata)
+ irq_flags = irqd_get_trigger_type(irqdata);
+
+ ret = devm_request_irq(
+ dev, irq, rg_irq_raw_handler, irq_flags | IRQF_SHARED,
+ devm_kasprintf(dev, GFP_KERNEL, "panthor-%s-irq", name), rg);
+ if (ret)
+ return ret;
+
+ adev->rg[i] = rg;
+
+ rg_irq_resume(rg);
+
+ rg_handshake_init(rg);
+
+ return 0;
+}
+
+int panthor_resource_group_init(struct panthor_arbitration *adev)
+{
+ for (int i = 0; i < AM_ARB_MAX_RG_COUNT; i++) {
+ int ret = resource_group_init(adev, i);
+
+ if (ret == -ENODEV)
+ continue;
+
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
diff --git a/drivers/gpu/drm/panthor/arbitration/panthor_resource_group.h b/drivers/gpu/drm/panthor/arbitration/panthor_resource_group.h
new file mode 100644
index 0000000000000..b8633abdd8eb8
--- /dev/null
+++ b/drivers/gpu/drm/panthor/arbitration/panthor_resource_group.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0 or MIT */
+/* Copyright 2026 ARM Limited. All rights reserved. */
+
+#ifndef __PANTHOR_RESOURCE_GROUP_H__
+#define __PANTHOR_RESOURCE_GROUP_H__
+
+#include <linux/types.h>
+
+struct device;
+struct panthor_arbitration;
+
+int panthor_resource_group_init(struct panthor_arbitration *adev);
+
+void panthor_resource_group_term(struct panthor_arbitration *adev);
+
+int panthor_resource_group_suspend(struct panthor_arbitration *adev);
+
+int panthor_resource_group_resume(struct panthor_arbitration *adev);
+
+#endif
--
2.43.0