drm/panthor: Wrap register accessor helpers for type safety
Subsystems (3)
PATCH 1/1 drm/panthor: Wrap register accessor helpers for type safety
In Commit a8f5738779a9 ("drm/panthor: Pass an iomem pointer to GPU register access helpers"), the gpu register access helpers were changed from taking a pointer to a struct panthor_device in their first argument, to taking a void pointer.
This can cause problems, as patches based on panthor before this change will still compile fine after it. struct panthor_device * implicitly casts to a void pointer, resulting in completely wrong semantics.
Prevent this problem by wrapping the affected functions with macros that specifically check for and reject the struct panthor_device * type as the first argument.
--- drivers/gpu/drm/panthor/panthor_device.h | 68 +++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 15 deletions(-)
drivers/gpu/drm/panthor/panthor_device.h +53 -15
@@ -630,49 +630,87 @@ static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irqextern struct workqueue_struct *panthor_cleanup_wq; -static inline void gpu_write (void __iomem *iomem,u32 reg,u32 data) +static inline void _gpu_write (void __iomem *iomem,u32 reg,u32 data) {writel (data, iomem + reg); } -static inline u32 gpu_read (void __iomem *iomem,u32 reg) +static inline u32 _gpu_read (void __iomem *iomem,u32 reg) {return readl (iomem + reg); } -static inline u32 gpu_read_relaxed (void __iomem *iomem,u32 reg) +static inline u32 _gpu_read_relaxed (void __iomem *iomem,u32 reg) {return readl_relaxed (iomem + reg); } -static inline void gpu_write64 (void __iomem *iomem,u32 reg,u64 data) +/* +* The function signature of gpu_read/gpu_write/gpu_read_relaxed/... used to +* take a &struct panthor_device* as the first parameter. During the split of +* iomem ranges into individual sub-components, this was changed to take a +* void __iomem* instead. These wrappers exists Tto avoid situations wherein +* pre-refactor patches are applied in error, as they'd compile fine. That's +* because the old calling convention's first parameter implicitly casts to a +* void pointer. +*/ + +#define gpu_write (iomem, reg, data) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \ + _gpu_write((iomem), (reg), (data)); }) + +#define gpu_read (iomem, reg) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \ + _gpu_read((iomem), (reg)); }) + +#define gpu_read_relaxed (iomem, reg) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \ + _gpu_read_relaxed((iomem), (reg)); }) + +static inline void _gpu_write64 (void __iomem *iomem,u32 reg,u64 data) { -gpu_write (iomem, reg,lower_32_bits (data)); -gpu_write (iomem, reg +4 ,upper_32_bits (data)); +_gpu_write (iomem, reg,lower_32_bits (data)); +_gpu_write (iomem, reg +4 ,upper_32_bits (data)); } -static inline u64 gpu_read64 (void __iomem *iomem,u32 reg) +#define gpu_write64 (iomem, reg, data) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \ + _gpu_write64((iomem), (reg), (data)); }) + +static inline u64 _gpu_read64 (void __iomem *iomem,u32 reg) { -return (gpu_read (iomem, reg) | ((u64 )gpu_read (iomem, reg +4 ) <<32 )); +return (_gpu_read (iomem, reg) | ((u64 )_gpu_read (iomem, reg +4 ) <<32 )); } -static inline u64 gpu_read64_relaxed (void __iomem *iomem,u32 reg) +#define gpu_read64 (iomem, reg) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \ + _gpu_read64((iomem), (reg)); }) + +static inline u64 _gpu_read64_relaxed (void __iomem *iomem,u32 reg) { -return (gpu_read_relaxed (iomem, reg) | - ((u64 )gpu_read_relaxed (iomem, reg +4 ) <<32 )); +return (_gpu_read_relaxed (iomem, reg) | + ((u64 )_gpu_read_relaxed (iomem, reg +4 ) <<32 )); } -static inline u64 gpu_read64_counter (void __iomem *iomem,u32 reg) +#define gpu_read64_relaxed (iomem, reg) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \ + _gpu_read64_relaxed((iomem), (reg)); }) + +static inline u64 _gpu_read64_counter (void __iomem *iomem,u32 reg) {u32 lo, hi1, hi2;do { - hi1 =gpu_read (iomem, reg +4 ); - lo =gpu_read (iomem, reg); - hi2 =gpu_read (iomem, reg +4 ); + hi1 =_gpu_read (iomem, reg +4 ); + lo =_gpu_read (iomem, reg); + hi2 =_gpu_read (iomem, reg +4 ); }while (hi1 != hi2);return lo | ((u64 )hi2 <<32 ); } +#define gpu_read64_counter (iomem, reg) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \ + _gpu_read64_counter((iomem), (reg)); }) +#define gpu_read_poll_timeout (iomem, reg, val, cond, delay_us, timeout_us) \ read_poll_timeout(gpu_read, val, cond, delay_us, timeout_us, false, \ iomem, reg)
---
base-commit: 3c253a3bef01b39d4640cfe3dfd38d8d5557ae0c
change-id: 20260508-panthor-gpu-read-type-7ac3fffd124c
Best regards,
Signature · 1 line
Nicolas Frattaroli <[email protected]>
Boris Brezillon
maintainerHi Nicolas,
Quoted 30 lines · On Fri, 08 May 2026 20:00:54 +0200 Nicolas Frattaroli…
In Commit
a8f5738779a9("drm/panthor: Pass an iomem pointer to GPU register access helpers"), the gpu register access helpers were changed from taking a pointer to a structpanthor_devicein their first argument, to taking a void pointer.This can cause problems, as patches based on panthor before this change will still compile fine after it. struct
panthor_device* implicitly casts to a void pointer, resulting in completely wrong semantics.Prevent this problem by wrapping the affected functions with macros that specifically check for and reject the struct
panthor_device* type as the first argument.Signed-off-by Nicolas Frattaroli <[email protected]>--- drivers/gpu/drm/panthor/panthor_device.h | 68 +++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 15 deletions(-)drivers/gpu/drm/panthor/panthor_device.h +1
-1@@ -630,49 +630,87 @@ static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irqextern struct workqueue_struct *panthor_cleanup_wq; -static inline void gpu_write (void __iomem *iomem,u32 reg,u32 data) +static inline void _gpu_write (void __iomem *iomem,u32 reg,u32 data) {writel (data, iomem + reg);
drivers/gpu/drm/panthor/panthor_device.h static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irq · line 643
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_device.h +2
-2} -static inline u32 gpu_read (void __iomem *iomem,u32 reg) +static inline u32 _gpu_read (void __iomem *iomem,u32 reg) {return readl (iomem + reg); } -static inline u32 gpu_read_relaxed (void __iomem *iomem,u32 reg) +static inline u32 _gpu_read_relaxed (void __iomem *iomem,u32 reg)
First off, I'm not a huge fan of these _ prefixes to convey the "internal, don't use directly" information. If we're going to have macros around these helpers (meaning these helpers won't be used directly), I'd rather have a fully descriptive name like gpu_{write,read}_iomem[_relaxed]() and a comment stating that they should never be used.
Quoted 7 lines
drivers/gpu/drm/panthor/panthor_device.h +2
-1{return readl_relaxed (iomem + reg); } -static inline void gpu_write64 (void __iomem *iomem,u32 reg,u64 data) +/* +* The function signature of gpu_read/gpu_write/gpu_read_relaxed/... used to
drivers/gpu/drm/panthor/panthor_device.h static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irq · line 659
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_device.h +10
-0+* take a &struct panthor_device* as the first parameter. During the split of +* iomem ranges into individual sub-components, this was changed to take a +* void __iomem* instead. These wrappers exists Tto avoid situations wherein +* pre-refactor patches are applied in error, as they'd compile fine. That's +* because the old calling convention's first parameter implicitly casts to a +* void pointer. +*/ + +#define gpu_write (iomem, reg, data) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \
Hm, this only covers ptdev being passed as an iomem pointer. I know it's the only case we had so far, but if we're going to add type enforcement, I think I'd prefer if we were covered for more than just ptdev.
One way of doing that would be to wrap the void __iomem *iomem in an explicit type like:
struct panthor_reg_bank {
void __iomem *iomem;
};which then gets passed to gpu_{read,write} helpers (see the diff below).
The other way would be to pass the component, and have the macro do the <component>->iomem deref, but there's a few places where reg banks are accessed outside of the components that own them (panthor_hw.c).
Regards,
Boris
--->8---
drivers/gpu/drm/panthor/panthor_device.h +46 -42
@@ -73,6 +73,10 @@ enum panthor_irq_state { PANTHOR_IRQ_STATE_SUSPENDING, }; +struct panthor_reg_bank { +void __iomem *iomem; +}; +/** * struct panthor_irq - IRQ data *
@@ -82,8 +86,8 @@ struct panthor_irq {/** @ptdev: Panthor device */ struct panthor_device *ptdev; -/** @iomem: CPU mapping of IRQ base address */ -void __iomem *iomem; +/** @regs: CPU mapping of IRQ base address */ +struct panthor_reg_bank regs;/** @irq: IRQ number. */ int irq;
@@ -519,7 +523,7 @@ static irqreturn_t panthor_ ## __name ## _irq_raw_handler(int irq, void *data)struct panthor_irq *pirq = data; \enum panthor_irq_state old_state; \ \ -if (!gpu_read (pirq->iomem,INT_STAT )) \ +if (!gpu_read (pirq->regs,INT_STAT )) \return IRQ_NONE ; \ \guard (spinlock_irqsave)(&pirq->mask_lock); \
@@ -529,7 +533,7 @@ static irqreturn_t panthor_ ## __name ## _irq_raw_handler(int irq, void *data)if (old_state !=PANTHOR_IRQ_STATE_ACTIVE ) \return IRQ_NONE ; \ \ -gpu_write (pirq->iomem,INT_MASK ,0 ); \ +gpu_write (pirq->regs,INT_MASK ,0 ); \return IRQ_WAKE_THREAD ; \ } \ \
@@ -548,7 +552,7 @@ static irqreturn_t panthor_ ## __name ## _irq_threaded_handler(int irq, void *da* right before the HW event kicks in. TLDR; it's all expected races we're \ * covered for. \ */ \ -u32 status =gpu_read (pirq->iomem,INT_RAWSTAT ) & pirq->mask; \ +u32 status =gpu_read (pirq->regs,INT_RAWSTAT ) & pirq->mask; \ \if (!status) \break ; \
@@ -564,7 +568,7 @@ static irqreturn_t panthor_ ## __name ## _irq_threaded_handler(int irq, void *daPANTHOR_IRQ_STATE_PROCESSING , \ PANTHOR_IRQ_STATE_ACTIVE); \if (old_state ==PANTHOR_IRQ_STATE_PROCESSING ) \ -gpu_write (pirq->iomem,INT_MASK , pirq->mask); \ +gpu_write (pirq->regs,INT_MASK , pirq->mask); \ } \ \return ret; \
@@ -574,7 +578,7 @@ static inline void panthor_ ## __name ## _irq_suspend(struct panthor_irq *pirq) { \ scoped_guard(spinlock_irqsave, &pirq->mask_lock) { \atomic_set (&pirq->state,PANTHOR_IRQ_STATE_SUSPENDING ); \ -gpu_write (pirq->iomem,INT_MASK ,0 ); \ +gpu_write (pirq->regs,INT_MASK ,0 ); \ } \synchronize_irq (pirq->irq); \atomic_set (&pirq->state,PANTHOR_IRQ_STATE_SUSPENDED ); \
@@ -585,8 +589,8 @@ static inline void panthor_ ## __name ## _irq_resume(struct panthor_irq *pirq)guard (spinlock_irqsave)(&pirq->mask_lock); \ \atomic_set (&pirq->state,PANTHOR_IRQ_STATE_ACTIVE ); \ -gpu_write (pirq->iomem,INT_CLEAR , pirq->mask); \ -gpu_write (pirq->iomem,INT_MASK , pirq->mask); \ +gpu_write (pirq->regs,INT_CLEAR , pirq->mask); \ +gpu_write (pirq->regs,INT_MASK , pirq->mask); \ } \ \ static int panthor_request_ ## __name ## _irq(struct panthor_device *ptdev, \
@@ -596,7 +600,7 @@ static int panthor_request_ ## __name ## _irq(struct panthor_device *ptdev, \ pirq->ptdev = ptdev; \ pirq->irq = irq; \ pirq->mask = mask; \ - pirq->iomem = iomem; \ + pirq->regs.iomem = iomem; \spin_lock_init (&pirq->mask_lock); \ panthor_ ## __name ## _irq_resume(pirq); \ \
@@ -618,7 +622,7 @@ static inline void panthor_ ## __name ## _irq_enable_events(struct panthor_irq ** If the IRQ is suspended/suspending, the mask is restored at resume time. \ */ \if (atomic_read (&pirq->state) ==PANTHOR_IRQ_STATE_ACTIVE ) \ -gpu_write (pirq->iomem,INT_MASK , pirq->mask); \ +gpu_write (pirq->regs,INT_MASK , pirq->mask); \ } \ \ static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irq *pirq, u32 mask)\
@@ -632,80 +636,80 @@ static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irq* If the IRQ is suspended/suspending, the mask is restored at resume time. \ */ \if (atomic_read (&pirq->state) ==PANTHOR_IRQ_STATE_ACTIVE ) \ -gpu_write (pirq->iomem,INT_MASK , pirq->mask); \ +gpu_write (pirq->regs,INT_MASK , pirq->mask); \ }extern struct workqueue_struct *panthor_cleanup_wq; -static inline void gpu_write (void __iomem *iomem,u32 reg,u32 data) +static inline void gpu_write (struct panthor_reg_bank rbank,u32 reg,u32 data) { -writel (data, iomem + reg); +writel (data, rbank.iomem + reg); } -static inline u32 gpu_read (void __iomem *iomem,u32 reg) +static inline u32 gpu_read (struct panthor_reg_bank rbank,u32 reg) { -return readl (iomem + reg); +return readl (rbank.iomem + reg); } -static inline u32 gpu_read_relaxed (void __iomem *iomem,u32 reg) +static inline u32 gpu_read_relaxed (struct panthor_reg_bank rbank,u32 reg) { -return readl_relaxed (iomem + reg); +return readl_relaxed (rbank.iomem + reg); } -static inline void gpu_write64 (void __iomem *iomem,u32 reg,u64 data) +static inline void gpu_write64 (struct panthor_reg_bank rbank,u32 reg,u64 data) { -gpu_write (iomem, reg,lower_32_bits (data)); -gpu_write (iomem, reg +4 ,upper_32_bits (data)); +gpu_write (rbank, reg,lower_32_bits (data)); +gpu_write (rbank, reg +4 ,upper_32_bits (data)); } -static inline u64 gpu_read64 (void __iomem *iomem,u32 reg) +static inline u64 gpu_read64 (struct panthor_reg_bank rbank,u32 reg) { -return (gpu_read (iomem, reg) | ((u64 )gpu_read (iomem, reg +4 ) <<32 )); +return (gpu_read (rbank, reg) | ((u64 )gpu_read (rbank, reg +4 ) <<32 )); } -static inline u64 gpu_read64_relaxed (void __iomem *iomem,u32 reg) +static inline u64 gpu_read64_relaxed (struct panthor_reg_bank rbank,u32 reg) { -return (gpu_read_relaxed (iomem, reg) | - ((u64 )gpu_read_relaxed (iomem, reg +4 ) <<32 )); +return (gpu_read_relaxed (rbank, reg) | + ((u64 )gpu_read_relaxed (rbank, reg +4 ) <<32 )); } -static inline u64 gpu_read64_counter (void __iomem *iomem,u32 reg) +static inline u64 gpu_read64_counter (struct panthor_reg_bank rbank,u32 reg) {u32 lo, hi1, hi2;do { - hi1 =gpu_read (iomem, reg +4 ); - lo =gpu_read (iomem, reg); - hi2 =gpu_read (iomem, reg +4 ); + hi1 =gpu_read (rbank, reg +4 ); + lo =gpu_read (rbank, reg); + hi2 =gpu_read (rbank, reg +4 ); }while (hi1 != hi2);return lo | ((u64 )hi2 <<32 ); } -#define gpu_read_poll_timeout (iomem, reg, val, cond, delay_us, timeout_us) \ +#define gpu_read_poll_timeout (rbank, reg, val, cond, delay_us, timeout_us) \ read_poll_timeout(gpu_read, val, cond, delay_us, timeout_us, false, \ - iomem, reg) + rbank, reg) -#define gpu_read_poll_timeout_atomic (iomem, reg, val, cond, delay_us, \ +#define gpu_read_poll_timeout_atomic (rbank, reg, val, cond, delay_us, \ timeout_us) \ read_poll_timeout_atomic(gpu_read, val, cond, delay_us, timeout_us, \ - false, iomem, reg) + false, rbank, reg) -#define gpu_read64_poll_timeout (iomem, reg, val, cond, delay_us, timeout_us) \ +#define gpu_read64_poll_timeout (rbank, reg, val, cond, delay_us, timeout_us) \ read_poll_timeout(gpu_read64, val, cond, delay_us, timeout_us, false, \ - iomem, reg) + rbank, reg) -#define gpu_read64_poll_timeout_atomic (iomem, reg, val, cond, delay_us, \ +#define gpu_read64_poll_timeout_atomic (rbank, reg, val, cond, delay_us, \ timeout_us) \ read_poll_timeout_atomic(gpu_read64, val, cond, delay_us, timeout_us, \ - false, iomem, reg) + false, rbank, reg) -#define gpu_read_relaxed_poll_timeout_atomic (iomem, reg, val, cond, delay_us, \ +#define gpu_read_relaxed_poll_timeout_atomic (rbank, reg, val, cond, delay_us, \ timeout_us) \ read_poll_timeout_atomic(gpu_read_relaxed, val, cond, delay_us, \ - timeout_us, false, iomem, reg) + timeout_us, false, rbank, reg) -#define gpu_read64_relaxed_poll_timeout (iomem, reg, val, cond, delay_us, \ +#define gpu_read64_relaxed_poll_timeout (rbank, reg, val, cond, delay_us, \ timeout_us) \ read_poll_timeout(gpu_read64_relaxed, val, cond, delay_us, timeout_us, \ - false, iomem, reg) + false, rbank, reg)#endif
drivers/gpu/drm/panthor/panthor_fw.c +15 -11
@@ -234,8 +234,8 @@ struct panthor_fw_iface {* struct panthor_fw - Firmware management */ struct panthor_fw { -/** @iomem: CPU mapping of MCU_CONTROL iomem region */ -void __iomem *iomem; +/** @regs: CPU mapping of MCU_CONTROL iomem region */ +struct panthor_reg_bank regs;/** @vm: MCU VM. */ struct panthor_vm *vm;
@@ -1072,7 +1072,7 @@ static void panthor_job_irq_handler(struct panthor_device *ptdev, u32 status)if (tracepoint_enabled (gpu_job_irq)) start =ktime_get_ns (); -gpu_write (ptdev->fw->irq.iomem,INT_CLEAR , status); +gpu_write (ptdev->fw->irq.regs,INT_CLEAR , status);if (!ptdev->fw->booted && (status &JOB_INT_GLOBAL_IF )) ptdev->fw->booted = true;
@@ -1101,13 +1101,13 @@ static int panthor_fw_start(struct panthor_device *ptdev) ptdev->fw->booted = false;panthor_job_irq_enable_events (&ptdev->fw->irq, ~0 );panthor_job_irq_resume (&ptdev->fw->irq); -gpu_write (fw->iomem,MCU_CONTROL ,MCU_CONTROL_AUTO ); +gpu_write (fw->regs,MCU_CONTROL ,MCU_CONTROL_AUTO );if (!wait_event_timeout (ptdev->fw->req_waitqueue, ptdev->fw->booted,msecs_to_jiffies (1000 ))) {if (!ptdev->fw->booted && - !(gpu_read (fw->irq.iomem,INT_STAT ) &JOB_INT_GLOBAL_IF )) + !(gpu_read (fw->irq.regs,INT_STAT ) &JOB_INT_GLOBAL_IF )) timedout = true; }
@@ -1118,7 +1118,7 @@ static int panthor_fw_start(struct panthor_device *ptdev) [MCU_STATUS_HALT] = "halt", [MCU_STATUS_FATAL] = "fatal", }; -u32 status =gpu_read (fw->iomem,MCU_STATUS ); +u32 status =gpu_read (fw->regs,MCU_STATUS );drm_err (&ptdev->base,"Failed to boot MCU (status=%s)" , status <ARRAY_SIZE (status_str) ? status_str[status] :"unknown" );
@@ -1133,8 +1133,8 @@ static void panthor_fw_stop(struct panthor_device *ptdev)struct panthor_fw *fw = ptdev->fw;u32 status; -gpu_write (fw->iomem,MCU_CONTROL ,MCU_CONTROL_DISABLE ); -if (gpu_read_poll_timeout (fw->iomem,MCU_STATUS , status, +gpu_write (fw->regs,MCU_CONTROL ,MCU_CONTROL_DISABLE ); +if (gpu_read_poll_timeout (fw->regs,MCU_STATUS , status, status ==MCU_STATUS_DISABLED ,10 ,100000 ))drm_err (&ptdev->base,"Failed to stop MCU" ); }
@@ -1144,7 +1144,7 @@ static bool panthor_fw_mcu_halted(struct panthor_device *ptdev)struct panthor_fw_global_iface *glb_iface =panthor_fw_get_glb_iface (ptdev);bool halted; - halted =gpu_read (ptdev->fw->iomem,MCU_STATUS ) ==MCU_STATUS_HALT ; + halted =gpu_read (ptdev->fw->regs,MCU_STATUS ) ==MCU_STATUS_HALT ;if (panthor_fw_has_glb_state (ptdev)) halted &= (GLB_STATE_GET (glb_iface->output->ack) ==GLB_STATE_HALT );
@@ -1407,7 +1407,11 @@ int panthor_fw_csg_wait_acks(struct panthor_device *ptdev, u32 csg_slot,void panthor_fw_ring_doorbell (struct panthor_device *ptdev,u32 doorbell_id) { -gpu_write (ptdev->iomem,CSF_DOORBELL (doorbell_id),1 ); +const struct panthor_reg_bank db_regs = { + .iomem = ptdev->iomem, + }; + +gpu_write (db_regs,CSF_DOORBELL (doorbell_id),1 ); }/**
@@ -1466,7 +1470,7 @@ int panthor_fw_init(struct panthor_device *ptdev)if (!fw)return -ENOMEM ; - fw->iomem = ptdev->iomem +MCU_CONTROL_BASE ; + fw->regs.iomem = ptdev->iomem +MCU_CONTROL_BASE ; ptdev->fw = fw;init_waitqueue_head (&fw->req_waitqueue);INIT_LIST_HEAD (&fw->sections);
drivers/gpu/drm/panthor/panthor_gpu.c +29 -29
171 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_hw.c +32 -25
83 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_mmu.c +17 -17
@@ -55,8 +55,8 @@ struct panthor_as_slot {* struct panthor_mmu - MMU related data */ struct panthor_mmu { -/** @iomem: CPU mapping of MMU_AS_CONTROL iomem region */ -void __iomem *iomem; +/** @regs: CPU mapping of MMU_AS_CONTROL iomem region */ +struct panthor_reg_bank regs;/** @irq: The MMU irq. */ struct panthor_irq irq;
@@ -527,7 +527,7 @@ static int wait_ready(struct panthor_device *ptdev, u32 as_nr)/* Wait for the MMU status to indicate there is no active command, in * case one is pending. */ - ret =gpu_read_relaxed_poll_timeout_atomic (mmu->iomem,AS_STATUS (as_nr), val, + ret =gpu_read_relaxed_poll_timeout_atomic (mmu->regs,AS_STATUS (as_nr), val, !(val &AS_STATUS_AS_ACTIVE ),10 ,100000 );if (ret) {
@@ -545,7 +545,7 @@ static int as_send_cmd_and_wait(struct panthor_device *ptdev, u32 as_nr, u32 cmd/* write AS_COMMAND when MMU is ready to accept another command */ status =wait_ready (ptdev, as_nr);if (!status) { -gpu_write (ptdev->mmu->iomem,AS_COMMAND (as_nr), cmd); +gpu_write (ptdev->mmu->regs,AS_COMMAND (as_nr), cmd); status =wait_ready (ptdev, as_nr); }
@@ -598,9 +598,9 @@ static int panthor_mmu_as_enable(struct panthor_device *ptdev, u32 as_nr,panthor_mmu_irq_enable_events (&ptdev->mmu->irq,panthor_mmu_as_fault_mask (ptdev, as_nr)); -gpu_write64 (mmu->iomem,AS_TRANSTAB (as_nr), transtab); -gpu_write64 (mmu->iomem,AS_MEMATTR (as_nr), memattr); -gpu_write64 (mmu->iomem,AS_TRANSCFG (as_nr), transcfg); +gpu_write64 (mmu->regs,AS_TRANSTAB (as_nr), transtab); +gpu_write64 (mmu->regs,AS_MEMATTR (as_nr), memattr); +gpu_write64 (mmu->regs,AS_TRANSCFG (as_nr), transcfg);return as_send_cmd_and_wait (ptdev, as_nr,AS_COMMAND_UPDATE ); }
@@ -636,9 +636,9 @@ static int panthor_mmu_as_disable(struct panthor_device *ptdev, u32 as_nr,if (recycle_slot)return 0 ; -gpu_write64 (mmu->iomem,AS_TRANSTAB (as_nr),0 ); -gpu_write64 (mmu->iomem,AS_MEMATTR (as_nr),0 ); -gpu_write64 (mmu->iomem,AS_TRANSCFG (as_nr),AS_TRANSCFG_ADRMODE_UNMAPPED ); +gpu_write64 (mmu->regs,AS_TRANSTAB (as_nr),0 ); +gpu_write64 (mmu->regs,AS_MEMATTR (as_nr),0 ); +gpu_write64 (mmu->regs,AS_TRANSCFG (as_nr),AS_TRANSCFG_ADRMODE_UNMAPPED );return as_send_cmd_and_wait (ptdev, as_nr,AS_COMMAND_UPDATE ); }
@@ -791,7 +791,7 @@ int panthor_vm_active(struct panthor_vm *vm)*/ fault_mask =panthor_mmu_as_fault_mask (ptdev, as);if (ptdev->mmu->as.faulty_mask & fault_mask) { -gpu_write (ptdev->mmu->irq.iomem,INT_CLEAR , fault_mask); +gpu_write (ptdev->mmu->irq.regs,INT_CLEAR , fault_mask); ptdev->mmu->as.faulty_mask &= ~fault_mask; }
@@ -1738,7 +1738,7 @@ static int panthor_vm_lock_region(struct panthor_vm *vm, u64 start, u64 size)mutex_lock (&ptdev->mmu->as.slots_lock);if (vm->as.id >=0 && size) {/* Lock the region that needs to be updated */ -gpu_write64 (ptdev->mmu->iomem,AS_LOCKADDR (vm->as.id), +gpu_write64 (ptdev->mmu->regs,AS_LOCKADDR (vm->as.id),pack_region_range (ptdev, &start, &size));/* If the lock succeeded, update the locked_region info. */
@@ -1800,8 +1800,8 @@ static void panthor_mmu_irq_handler(struct panthor_device *ptdev, u32 status)u32 access_type;u32 source_id; - fault_status =gpu_read (mmu->iomem,AS_FAULTSTATUS (as)); - addr =gpu_read64 (mmu->iomem,AS_FAULTADDRESS (as)); + fault_status =gpu_read (mmu->regs,AS_FAULTSTATUS (as)); + addr =gpu_read64 (mmu->regs,AS_FAULTADDRESS (as));/* decode the fault status */ exception_type = fault_status &0xFF ;
@@ -1832,7 +1832,7 @@ static void panthor_mmu_irq_handler(struct panthor_device *ptdev, u32 status)* Note that COMPLETED irqs are never cleared, but this is fine * because they are always masked. */ -gpu_write (mmu->irq.iomem,INT_CLEAR , mask); +gpu_write (mmu->irq.regs,INT_CLEAR , mask);if (ptdev->mmu->as.slots[as].vm) ptdev->mmu->as.slots[as].vm->unhandled_fault = true;
@@ -3255,7 +3255,7 @@ int panthor_mmu_init(struct panthor_device *ptdev)if (ret)return ret; - mmu->iomem = ptdev->iomem +MMU_AS_BASE ; + mmu->regs.iomem = ptdev->iomem +MMU_AS_BASE ; ptdev->mmu = mmu; irq =platform_get_irq_byname (to_platform_device (ptdev->base.dev),"mmu" );
@@ -3264,7 +3264,7 @@ int panthor_mmu_init(struct panthor_device *ptdev) ret =panthor_request_mmu_irq (ptdev, &mmu->irq, irq,panthor_mmu_fault_mask (ptdev, ~0 ), - ptdev->iomem +MMU_INT_BASE ); + mmu->regs.iomem +MMU_INT_BASE );if (ret)return ret;
drivers/gpu/drm/panthor/panthor_pwr.c +31 -31
181 lines not on this page · raw message
Quoted 43 lines · On Monday, 11 May 2026 13:56:41 Central European Summer Time Boris Brezillon…
Hi Nicolas,
Quoted 38 lines · On Fri, 08 May 2026 20:00:54 +0200 Nicolas Frattaroli…
In Commit
a8f5738779a9("drm/panthor: Pass an iomem pointer to GPU register access helpers"), the gpu register access helpers were changed from taking a pointer to a structpanthor_devicein their first argument, to taking a void pointer.This can cause problems, as patches based on panthor before this change will still compile fine after it. struct
panthor_device* implicitly casts to a void pointer, resulting in completely wrong semantics.Prevent this problem by wrapping the affected functions with macros that specifically check for and reject the struct
panthor_device* type as the first argument.Signed-off-by Nicolas Frattaroli <[email protected]>--- drivers/gpu/drm/panthor/panthor_device.h | 68 +++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 15 deletions(-)drivers/gpu/drm/panthor/panthor_device.h +2
-2@@ -630,49 +630,87 @@ static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irqextern struct workqueue_struct *panthor_cleanup_wq; -static inline void gpu_write (void __iomem *iomem,u32 reg,u32 data) +static inline void _gpu_write (void __iomem *iomem,u32 reg,u32 data) {writel (data, iomem + reg); } -static inline u32 gpu_read (void __iomem *iomem,u32 reg) +static inline u32 _gpu_read (void __iomem *iomem,u32 reg) {return readl (iomem + reg); }
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_device.h static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irq · line 643
Quoted 2 lines
drivers/gpu/drm/panthor/panthor_device.h +1
-1-static inline u32 gpu_read_relaxed (void __iomem *iomem,u32 reg) +static inline u32 _gpu_read_relaxed (void __iomem *iomem,u32 reg)First off, I'm not a huge fan of these _ prefixes to convey the "internal, don't use directly" information. If we're going to have macros around these helpers (meaning these helpers won't be used directly), I'd rather have a fully descriptive name like gpu_{write,read}_iomem[_relaxed]() and a comment stating that they should never be used.
Agreed.
Quoted 23 lines
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_device.h +2
-1{return readl_relaxed (iomem + reg); } -static inline void gpu_write64 (void __iomem *iomem,u32 reg,u64 data) +/* +* The function signature of gpu_read/gpu_write/gpu_read_relaxed/... used to drivers/gpu/drm/panthor/panthor_device.h static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irq · line 659
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_device.h +10
-0+* take a &struct panthor_device* as the first parameter. During the split of +* iomem ranges into individual sub-components, this was changed to take a +* void __iomem* instead. These wrappers exists Tto avoid situations wherein +* pre-refactor patches are applied in error, as they'd compile fine. That's +* because the old calling convention's first parameter implicitly casts to a +* void pointer. +*/ + +#define gpu_write (iomem, reg, data) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \Hm, this only covers ptdev being passed as an iomem pointer. I know it's the only case we had so far, but if we're going to add type enforcement, I think I'd prefer if we were covered for more than just ptdev.
Quoted 8 lines
One way of doing that would be to wrap the
void __iomem *iomemin an explicit type like:struct panthor_reg_bank { void __iomem *iomem; };which then gets passed to gpu_{read,write} helpers (see the diff below).
Hm, okay, the diff below is smaller than I feared. Though it doesn't get us type checking for someone, say, trying to read GPU_STATUS with the iomem of panthor_fw. But neither does my proposal below.
Quoted 4 lines
The other way would be to pass the component, and have the macro do the <component>->iomem deref, but there's a few places where reg banks are accessed outside of the components that own them (
panthor_hw.c).
Yeah, I prototyped going down something along that route by having the register accessors be generics that are implemented by each component, and it's a bit messy. Either you expose the struct definitions of individual components so that this header has visibility into them (not great), or you add boilerplate "do this accessor operation for this component" helpers for every component, which is both verbose and possibly causes the inlining to no longer work, though I have yet to verify that.
If we do want to go down this route (though I'm not sure, since your reg bank solution seems to get us the same guarantees but without bringing generics into this), then the following may be an okay idea:
I think having just the iomem deref genericised may be a good middle ground. If instead of making it a deref, we make it return the pointer to the member into the component that it can then deref, then the component-specific part can be pure (since offset of the iomem member is constant so for a particular pointer to a component, the pointer to the iomem member only depends on the passed-in pointer to component.)
This should make sure that when the compiler gets
panthor_gpu_write(ptdev->gpu, foo, bar);
val = panthor_gpu_read(ptdev->gpu, baz);
it can optimise the expanded
iomem = *panthor_get_iomem_ptr(ptdev->gpu);panthor_actual_write(iomem, foo, bar);
iomem = *panthor_get_iomem_ptr(ptdev->gpu);
val = panthor_actual_read(iomem, baz);
to the simplified
iomem = *panthor_get_iomem_ptr(ptdev->gpu);panthor_actual_write(iomem, foo, bar);
val = panthor_actual_read(iomem, baz);
because panthor_get_iomem_ptr will be known to return the same value when called with the same input param.
Anway, I think it's probably best if I abandon this and you just send your patch to the list with a real base. I only have one comment on it, which I've included inline.
Quoted 114 lines
Regards,
Boris
--->8---
[... snip ...]drivers/gpu/drm/panthor/panthor_mmu.c +16
-16@@ -55,8 +55,8 @@ struct panthor_as_slot {* struct panthor_mmu - MMU related data */ struct panthor_mmu { -/** @iomem: CPU mapping of MMU_AS_CONTROL iomem region */ -void __iomem *iomem; +/** @regs: CPU mapping of MMU_AS_CONTROL iomem region */ +struct panthor_reg_bank regs;/** @irq: The MMU irq. */ struct panthor_irq irq;@@ -527,7 +527,7 @@ static int wait_ready(struct panthor_device *ptdev, u32 as_nr)/* Wait for the MMU status to indicate there is no active command, in * case one is pending. */ - ret =gpu_read_relaxed_poll_timeout_atomic (mmu->iomem,AS_STATUS (as_nr), val, + ret =gpu_read_relaxed_poll_timeout_atomic (mmu->regs,AS_STATUS (as_nr), val, !(val &AS_STATUS_AS_ACTIVE ),10 ,100000 );if (ret) {@@ -545,7 +545,7 @@ static int as_send_cmd_and_wait(struct panthor_device *ptdev, u32 as_nr, u32 cmd/* write AS_COMMAND when MMU is ready to accept another command */ status =wait_ready (ptdev, as_nr);if (!status) { -gpu_write (ptdev->mmu->iomem,AS_COMMAND (as_nr), cmd); +gpu_write (ptdev->mmu->regs,AS_COMMAND (as_nr), cmd); status =wait_ready (ptdev, as_nr); }@@ -598,9 +598,9 @@ static int panthor_mmu_as_enable(struct panthor_device *ptdev, u32 as_nr,panthor_mmu_irq_enable_events (&ptdev->mmu->irq,panthor_mmu_as_fault_mask (ptdev, as_nr)); -gpu_write64 (mmu->iomem,AS_TRANSTAB (as_nr), transtab); -gpu_write64 (mmu->iomem,AS_MEMATTR (as_nr), memattr); -gpu_write64 (mmu->iomem,AS_TRANSCFG (as_nr), transcfg); +gpu_write64 (mmu->regs,AS_TRANSTAB (as_nr), transtab); +gpu_write64 (mmu->regs,AS_MEMATTR (as_nr), memattr); +gpu_write64 (mmu->regs,AS_TRANSCFG (as_nr), transcfg);return as_send_cmd_and_wait (ptdev, as_nr,AS_COMMAND_UPDATE ); }@@ -636,9 +636,9 @@ static int panthor_mmu_as_disable(struct panthor_device *ptdev, u32 as_nr,if (recycle_slot)return 0 ; -gpu_write64 (mmu->iomem,AS_TRANSTAB (as_nr),0 ); -gpu_write64 (mmu->iomem,AS_MEMATTR (as_nr),0 ); -gpu_write64 (mmu->iomem,AS_TRANSCFG (as_nr),AS_TRANSCFG_ADRMODE_UNMAPPED ); +gpu_write64 (mmu->regs,AS_TRANSTAB (as_nr),0 ); +gpu_write64 (mmu->regs,AS_MEMATTR (as_nr),0 ); +gpu_write64 (mmu->regs,AS_TRANSCFG (as_nr),AS_TRANSCFG_ADRMODE_UNMAPPED );return as_send_cmd_and_wait (ptdev, as_nr,AS_COMMAND_UPDATE ); }@@ -791,7 +791,7 @@ int panthor_vm_active(struct panthor_vm *vm)*/ fault_mask =panthor_mmu_as_fault_mask (ptdev, as);if (ptdev->mmu->as.faulty_mask & fault_mask) { -gpu_write (ptdev->mmu->irq.iomem,INT_CLEAR , fault_mask); +gpu_write (ptdev->mmu->irq.regs,INT_CLEAR , fault_mask); ptdev->mmu->as.faulty_mask &= ~fault_mask; }@@ -1738,7 +1738,7 @@ static int panthor_vm_lock_region(struct panthor_vm *vm, u64 start, u64 size)mutex_lock (&ptdev->mmu->as.slots_lock);if (vm->as.id >=0 && size) {/* Lock the region that needs to be updated */ -gpu_write64 (ptdev->mmu->iomem,AS_LOCKADDR (vm->as.id), +gpu_write64 (ptdev->mmu->regs,AS_LOCKADDR (vm->as.id),pack_region_range (ptdev, &start, &size));/* If the lock succeeded, update the locked_region info. */ @@ -1800,8 +1800,8 @@ static void panthor_mmu_irq_handler(struct panthor_device *ptdev, u32 status)u32 access_type;u32 source_id; - fault_status =gpu_read (mmu->iomem,AS_FAULTSTATUS (as)); - addr =gpu_read64 (mmu->iomem,AS_FAULTADDRESS (as)); + fault_status =gpu_read (mmu->regs,AS_FAULTSTATUS (as)); + addr =gpu_read64 (mmu->regs,AS_FAULTADDRESS (as));/* decode the fault status */ exception_type = fault_status &0xFF ;@@ -1832,7 +1832,7 @@ static void panthor_mmu_irq_handler(struct panthor_device *ptdev, u32 status)* Note that COMPLETED irqs are never cleared, but this is fine * because they are always masked. */ -gpu_write (mmu->irq.iomem,INT_CLEAR , mask); +gpu_write (mmu->irq.regs,INT_CLEAR , mask);if (ptdev->mmu->as.slots[as].vm) ptdev->mmu->as.slots[as].vm->unhandled_fault = true;@@ -3255,7 +3255,7 @@ int panthor_mmu_init(struct panthor_device *ptdev)if (ret)return ret; - mmu->iomem = ptdev->iomem +MMU_AS_BASE ; + mmu->regs.iomem = ptdev->iomem +MMU_AS_BASE ; ptdev->mmu = mmu; irq =platform_get_irq_byname (to_platform_device (ptdev->base.dev),"mmu" );
drivers/gpu/drm/panthor/panthor_mmu.c int panthor_mmu_init(struct panthor_device *ptdev) · line 3267
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_mmu.c +1
-1@@ -3264,7 +3264,7 @@ int panthor_mmu_init(struct panthor_device *ptdev) ret =panthor_request_mmu_irq (ptdev, &mmu->irq, irq,panthor_mmu_fault_mask (ptdev, ~0 ), - ptdev->iomem +MMU_INT_BASE ); + mmu->regs.iomem +MMU_INT_BASE );
This is wrong. Before, we used an effective whole-device offset of 0x2000. Now it's 0x4400.
drivers/gpu/drm/panthor/panthor_mmu.c int panthor_mmu_init(struct panthor_device *ptdev) · line 3270
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_mmu.c +0
-0if (ret)return ret;
Kind regards,
Nicolas Frattaroli
Boris Brezillon
maintainerQuoted 256 lines · On Mon, 11 May 2026 15:53:31 +0200 Nicolas Frattaroli…
Quoted 43 lines · On Monday, 11 May 2026 13:56:41 Central European Summer Time Boris Brezillon…
Hi Nicolas,
Quoted 38 lines · On Fri, 08 May 2026 20:00:54 +0200 Nicolas Frattaroli…
In Commit
a8f5738779a9("drm/panthor: Pass an iomem pointer to GPU register access helpers"), the gpu register access helpers were changed from taking a pointer to a structpanthor_devicein their first argument, to taking a void pointer.This can cause problems, as patches based on panthor before this change will still compile fine after it. struct
panthor_device* implicitly casts to a void pointer, resulting in completely wrong semantics.Prevent this problem by wrapping the affected functions with macros that specifically check for and reject the struct
panthor_device* type as the first argument.Signed-off-by Nicolas Frattaroli <[email protected]>--- drivers/gpu/drm/panthor/panthor_device.h | 68 +++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 15 deletions(-)drivers/gpu/drm/panthor/panthor_device.h +2
-2@@ -630,49 +630,87 @@ static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irqextern struct workqueue_struct *panthor_cleanup_wq; -static inline void gpu_write (void __iomem *iomem,u32 reg,u32 data) +static inline void _gpu_write (void __iomem *iomem,u32 reg,u32 data) {writel (data, iomem + reg); } -static inline u32 gpu_read (void __iomem *iomem,u32 reg) +static inline u32 _gpu_read (void __iomem *iomem,u32 reg) {return readl (iomem + reg); }Quoted 8 lines
drivers/gpu/drm/panthor/panthor_device.h static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irq · line 643
Quoted 2 lines
drivers/gpu/drm/panthor/panthor_device.h +1
-1-static inline u32 gpu_read_relaxed (void __iomem *iomem,u32 reg) +static inline u32 _gpu_read_relaxed (void __iomem *iomem,u32 reg)First off, I'm not a huge fan of these _ prefixes to convey the "internal, don't use directly" information. If we're going to have macros around these helpers (meaning these helpers won't be used directly), I'd rather have a fully descriptive name like gpu_{write,read}_iomem[_relaxed]() and a comment stating that they should never be used.
Agreed.
Quoted 23 lines
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_device.h +2
-1{return readl_relaxed (iomem + reg); } -static inline void gpu_write64 (void __iomem *iomem,u32 reg,u64 data) +/* +* The function signature of gpu_read/gpu_write/gpu_read_relaxed/... used to drivers/gpu/drm/panthor/panthor_device.h static inline void panthor_ ## __name ## _irq_disable_events(struct panthor_irq · line 659
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_device.h +10
-0+* take a &struct panthor_device* as the first parameter. During the split of +* iomem ranges into individual sub-components, this was changed to take a +* void __iomem* instead. These wrappers exists Tto avoid situations wherein +* pre-refactor patches are applied in error, as they'd compile fine. That's +* because the old calling convention's first parameter implicitly casts to a +* void pointer. +*/ + +#define gpu_write (iomem, reg, data) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \Hm, this only covers ptdev being passed as an iomem pointer. I know it's the only case we had so far, but if we're going to add type enforcement, I think I'd prefer if we were covered for more than just ptdev.
Quoted 8 lines
One way of doing that would be to wrap the
void __iomem *iomemin an explicit type like:struct panthor_reg_bank { void __iomem *iomem; };which then gets passed to gpu_{read,write} helpers (see the diff below).
Hm, okay, the diff below is smaller than I feared. Though it doesn't get us type checking for someone, say, trying to read
GPU_STATUSwith the iomem ofpanthor_fw. But neither does my proposal below.Quoted 4 lines
The other way would be to pass the component, and have the macro do the <component>->iomem deref, but there's a few places where reg banks are accessed outside of the components that own them (
panthor_hw.c).Yeah, I prototyped going down something along that route by having the register accessors be generics that are implemented by each component, and it's a bit messy. Either you expose the struct definitions of individual components so that this header has visibility into them (not great), or you add boilerplate "do this accessor operation for this component" helpers for every component, which is both verbose and possibly causes the inlining to no longer work, though I have yet to verify that.
If we do want to go down this route (though I'm not sure, since your reg bank solution seems to get us the same guarantees but without bringing generics into this), then the following may be an okay idea:
I think having just the iomem deref genericised may be a good middle ground. If instead of making it a deref, we make it return the pointer to the member into the component that it can then deref, then the component-specific part can be pure (since offset of the iomem member is constant so for a particular pointer to a component, the pointer to the iomem member only depends on the passed-in pointer to component.)
This should make sure that when the compiler gets
panthor_gpu_write(ptdev->gpu, foo, bar);
val =panthor_gpu_read(ptdev->gpu, baz);it can optimise the expanded
iomem =
*panthor_get_iomem_ptr(ptdev->gpu);panthor_actual_write(iomem, foo, bar);
iomem =*panthor_get_iomem_ptr(ptdev->gpu);
val =panthor_actual_read(iomem, baz);to the simplified
iomem =
*panthor_get_iomem_ptr(ptdev->gpu);panthor_actual_write(iomem, foo, bar);
val =panthor_actual_read(iomem, baz);because
panthor_get_iomem_ptrwill be known to return the same value when called with the same input param.Anway, I think it's probably best if I abandon this and you just send your patch to the list with a real base. I only have one comment on it, which I've included inline.
Quoted 115 lines
Regards,
Boris
--->8---
[... snip ...]drivers/gpu/drm/panthor/panthor_mmu.c +16
-16@@ -55,8 +55,8 @@ struct panthor_as_slot {* struct panthor_mmu - MMU related data */ struct panthor_mmu { -/** @iomem: CPU mapping of MMU_AS_CONTROL iomem region */ -void __iomem *iomem; +/** @regs: CPU mapping of MMU_AS_CONTROL iomem region */ +struct panthor_reg_bank regs;/** @irq: The MMU irq. */ struct panthor_irq irq;@@ -527,7 +527,7 @@ static int wait_ready(struct panthor_device *ptdev, u32 as_nr)/* Wait for the MMU status to indicate there is no active command, in * case one is pending. */ - ret =gpu_read_relaxed_poll_timeout_atomic (mmu->iomem,AS_STATUS (as_nr), val, + ret =gpu_read_relaxed_poll_timeout_atomic (mmu->regs,AS_STATUS (as_nr), val, !(val &AS_STATUS_AS_ACTIVE ),10 ,100000 );if (ret) {@@ -545,7 +545,7 @@ static int as_send_cmd_and_wait(struct panthor_device *ptdev, u32 as_nr, u32 cmd/* write AS_COMMAND when MMU is ready to accept another command */ status =wait_ready (ptdev, as_nr);if (!status) { -gpu_write (ptdev->mmu->iomem,AS_COMMAND (as_nr), cmd); +gpu_write (ptdev->mmu->regs,AS_COMMAND (as_nr), cmd); status =wait_ready (ptdev, as_nr); }@@ -598,9 +598,9 @@ static int panthor_mmu_as_enable(struct panthor_device *ptdev, u32 as_nr,panthor_mmu_irq_enable_events (&ptdev->mmu->irq,panthor_mmu_as_fault_mask (ptdev, as_nr)); -gpu_write64 (mmu->iomem,AS_TRANSTAB (as_nr), transtab); -gpu_write64 (mmu->iomem,AS_MEMATTR (as_nr), memattr); -gpu_write64 (mmu->iomem,AS_TRANSCFG (as_nr), transcfg); +gpu_write64 (mmu->regs,AS_TRANSTAB (as_nr), transtab); +gpu_write64 (mmu->regs,AS_MEMATTR (as_nr), memattr); +gpu_write64 (mmu->regs,AS_TRANSCFG (as_nr), transcfg);return as_send_cmd_and_wait (ptdev, as_nr,AS_COMMAND_UPDATE ); }@@ -636,9 +636,9 @@ static int panthor_mmu_as_disable(struct panthor_device *ptdev, u32 as_nr,if (recycle_slot)return 0 ; -gpu_write64 (mmu->iomem,AS_TRANSTAB (as_nr),0 ); -gpu_write64 (mmu->iomem,AS_MEMATTR (as_nr),0 ); -gpu_write64 (mmu->iomem,AS_TRANSCFG (as_nr),AS_TRANSCFG_ADRMODE_UNMAPPED ); +gpu_write64 (mmu->regs,AS_TRANSTAB (as_nr),0 ); +gpu_write64 (mmu->regs,AS_MEMATTR (as_nr),0 ); +gpu_write64 (mmu->regs,AS_TRANSCFG (as_nr),AS_TRANSCFG_ADRMODE_UNMAPPED );return as_send_cmd_and_wait (ptdev, as_nr,AS_COMMAND_UPDATE ); }@@ -791,7 +791,7 @@ int panthor_vm_active(struct panthor_vm *vm)*/ fault_mask =panthor_mmu_as_fault_mask (ptdev, as);if (ptdev->mmu->as.faulty_mask & fault_mask) { -gpu_write (ptdev->mmu->irq.iomem,INT_CLEAR , fault_mask); +gpu_write (ptdev->mmu->irq.regs,INT_CLEAR , fault_mask); ptdev->mmu->as.faulty_mask &= ~fault_mask; }@@ -1738,7 +1738,7 @@ static int panthor_vm_lock_region(struct panthor_vm *vm, u64 start, u64 size)mutex_lock (&ptdev->mmu->as.slots_lock);if (vm->as.id >=0 && size) {/* Lock the region that needs to be updated */ -gpu_write64 (ptdev->mmu->iomem,AS_LOCKADDR (vm->as.id), +gpu_write64 (ptdev->mmu->regs,AS_LOCKADDR (vm->as.id),pack_region_range (ptdev, &start, &size));/* If the lock succeeded, update the locked_region info. */ @@ -1800,8 +1800,8 @@ static void panthor_mmu_irq_handler(struct panthor_device *ptdev, u32 status)u32 access_type;u32 source_id; - fault_status =gpu_read (mmu->iomem,AS_FAULTSTATUS (as)); - addr =gpu_read64 (mmu->iomem,AS_FAULTADDRESS (as)); + fault_status =gpu_read (mmu->regs,AS_FAULTSTATUS (as)); + addr =gpu_read64 (mmu->regs,AS_FAULTADDRESS (as));/* decode the fault status */ exception_type = fault_status &0xFF ;@@ -1832,7 +1832,7 @@ static void panthor_mmu_irq_handler(struct panthor_device *ptdev, u32 status)* Note that COMPLETED irqs are never cleared, but this is fine * because they are always masked. */ -gpu_write (mmu->irq.iomem,INT_CLEAR , mask); +gpu_write (mmu->irq.regs,INT_CLEAR , mask);if (ptdev->mmu->as.slots[as].vm) ptdev->mmu->as.slots[as].vm->unhandled_fault = true;@@ -3255,7 +3255,7 @@ int panthor_mmu_init(struct panthor_device *ptdev)if (ret)return ret; - mmu->iomem = ptdev->iomem +MMU_AS_BASE ; + mmu->regs.iomem = ptdev->iomem +MMU_AS_BASE ; ptdev->mmu = mmu; irq =platform_get_irq_byname (to_platform_device (ptdev->base.dev),"mmu" );@@ -3264,7 +3264,7 @@ int panthor_mmu_init(struct panthor_device *ptdev)
Quoted 7 lines
drivers/gpu/drm/panthor/panthor_mmu.c int panthor_mmu_init(struct panthor_device *ptdev) · line 3267
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_mmu.c +1
-1ret =panthor_request_mmu_irq (ptdev, &mmu->irq, irq,panthor_mmu_fault_mask (ptdev, ~0 ), - ptdev->iomem +MMU_INT_BASE ); + mmu->regs.iomem +MMU_INT_BASE );This is wrong. Before, we used an effective whole-device offset of
0x2000. Now it's0x4400.
Uh, right. I hate the fact sometimes xxx_INT_BASE is some offset into the the xxx_BASE regbank (GPU_INT_BASE), and other times ({MMU,JOB}_INT_BASE), it's an absolute offset from the start of the whole IO region.
Boris Brezillon
maintainerQuoted 30 lines · On Mon, 11 May 2026 15:53:31 +0200 Nicolas Frattaroli…
Quoted 30 lines
Quoted 8 lines
{
(quoted diff) +2
-1return readl_relaxed(iomem + reg); } -static inline void gpu_write64(void __iomem *iomem, u32 reg, u64 data) +/* + * The function signature of gpu_read/gpu_write/gpu_read_relaxed/... used toReview
Quoted 10 lines
(quoted diff) +10
-0+ * take a &struct panthor_device* as the first parameter. During the split of + * iomem ranges into individual sub-components, this was changed to take a + * void __iomem* instead. These wrappers exists Tto avoid situations wherein + * pre-refactor patches are applied in error, as they'd compile fine. That's + * because the old calling convention's first parameter implicitly casts to a + * void pointer. + */ + +#define gpu_write(iomem, reg, data) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \Hm, this only covers ptdev being passed as an iomem pointer. I know it's the only case we had so far, but if we're going to add type enforcement, I think I'd prefer if we were covered for more than just ptdev.
One way of doing that would be to wrap the
void __iomem *iomemin an explicit type like:struct panthor_reg_bank { void __iomem *iomem; };
Quoted 5 lines
Quoted 1 line
which then gets passed to gpu_{read,write} helpers (see the diff below).
Hm, okay, the diff below is smaller than I feared. Though it doesn't get us type checking for someone, say, trying to read
GPU_STATUSwith the iomem ofpanthor_fw.
Yep, that's annoying, though solving that would require connecting reg definitions (in panthor_xxx_regs.h) to a specific reg_bank, which is only doable if we provide per-component accessors like:
#define mmu_reg(_mmu, _name) ((_mmu)->iomem + MMU_ ##)
#define mmu_read(_mmu, _name) gpu_read_iomem(mmu_reg(_mmu, _name))
#define mmu_write(_mmu, _name, _val) \ gpu_write_iomem(mmu_reg(_mmu, _name),_ val)
Quoted 39 lines
But neither does my proposal below.
Quoted 4 lines
The other way would be to pass the component, and have the macro do the <component>->iomem deref, but there's a few places where reg banks are accessed outside of the components that own them (
panthor_hw.c).Yeah, I prototyped going down something along that route by having the register accessors be generics that are implemented by each component, and it's a bit messy. Either you expose the struct definitions of individual components so that this header has visibility into them (not great), or you add boilerplate "do this accessor operation for this component" helpers for every component, which is both verbose and possibly causes the inlining to no longer work, though I have yet to verify that.
If we do want to go down this route (though I'm not sure, since your reg bank solution seems to get us the same guarantees but without bringing generics into this), then the following may be an okay idea:
I think having just the iomem deref genericised may be a good middle ground. If instead of making it a deref, we make it return the pointer to the member into the component that it can then deref, then the component-specific part can be pure (since offset of the iomem member is constant so for a particular pointer to a component, the pointer to the iomem member only depends on the passed-in pointer to component.)
This should make sure that when the compiler gets
panthor_gpu_write(ptdev->gpu, foo, bar);
val =panthor_gpu_read(ptdev->gpu, baz);it can optimise the expanded
iomem =
*panthor_get_iomem_ptr(ptdev->gpu);panthor_actual_write(iomem, foo, bar);
iomem =*panthor_get_iomem_ptr(ptdev->gpu);
val =panthor_actual_read(iomem, baz);
Quoted 8 lines
to the simplified
iomem =
*panthor_get_iomem_ptr(ptdev->gpu);panthor_actual_write(iomem, foo, bar);
val =panthor_actual_read(iomem, baz);because
panthor_get_iomem_ptrwill be known to return the same value when called with the same input param.
Right, as long as the compiler sees the definition of panthor_get_<component>_iomem_ptr() (which should be the case any time a read/write happens inside the panthor_<component>.c compilation unit), it hopefully inlines the whole thing and you get the iomem pointer from a direct deref rather than a function call. LTO might even give us link time optim for the bits in panthor_hw.c where the compiler can't see through struct panthor_{gpu,pwr}.
This being said, there's still no guarantee that one would mix regs and banks randomly, like
gpu_read(ptdev->mmu, GPU_ID);
Quoted 4 lines
Anway, I think it's probably best if I abandon this and you just send your patch to the list with a real base. I only have one comment on it, which I've included inline.
Let's wait for Liviu's and Steve's feedback before taking any action, cause that's still quite a lot of changes, and it's not clear it will help much once we've got all the pending patchset rebased on misc-next (that's a mistake you do once at rebase time, once you've got bitten, you tend to be more careful ;-)).
Steven Price
maintainerQuoted 115 lines · Boris Brezillon
Quoted 30 lines · On Mon, 11 May 2026 15:53:31 +0200 Nicolas Frattaroli…
Quoted 30 lines
Quoted 8 lines
{
(quoted diff) +2
-1return readl_relaxed(iomem + reg); } -static inline void gpu_write64(void __iomem *iomem, u32 reg, u64 data) +/* + * The function signature of gpu_read/gpu_write/gpu_read_relaxed/... used toReview
Quoted 10 lines
(quoted diff) +10
-0+ * take a &struct panthor_device* as the first parameter. During the split of + * iomem ranges into individual sub-components, this was changed to take a + * void __iomem* instead. These wrappers exists Tto avoid situations wherein + * pre-refactor patches are applied in error, as they'd compile fine. That's + * because the old calling convention's first parameter implicitly casts to a + * void pointer. + */ + +#define gpu_write(iomem, reg, data) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \Hm, this only covers ptdev being passed as an iomem pointer. I know it's the only case we had so far, but if we're going to add type enforcement, I think I'd prefer if we were covered for more than just ptdev.
One way of doing that would be to wrap the
void __iomem *iomemin an explicit type like:struct panthor_reg_bank { void __iomem *iomem; };Quoted 5 lines
Quoted 1 line
which then gets passed to gpu_{read,write} helpers (see the diff below).
Hm, okay, the diff below is smaller than I feared. Though it doesn't get us type checking for someone, say, trying to read
GPU_STATUSwith the iomem ofpanthor_fw.Yep, that's annoying, though solving that would require connecting reg definitions (in
panthor_xxx_regs.h) to a specificreg_bank, which is only doable if we provide per-component accessors like:#define
mmu_reg(_mmu, _name)((_mmu)->iomem +MMU_##)#define
mmu_read(_mmu, _name)gpu_read_iomem(mmu_reg(_mmu, _name))#define mmu_write(_mmu, _name, _val) \ gpu_write_iomem(mmu_reg(_mmu, _name),_ val)Quoted 39 lines
But neither does my proposal below.
Quoted 4 lines
The other way would be to pass the component, and have the macro do the <component>->iomem deref, but there's a few places where reg banks are accessed outside of the components that own them (
panthor_hw.c).Yeah, I prototyped going down something along that route by having the register accessors be generics that are implemented by each component, and it's a bit messy. Either you expose the struct definitions of individual components so that this header has visibility into them (not great), or you add boilerplate "do this accessor operation for this component" helpers for every component, which is both verbose and possibly causes the inlining to no longer work, though I have yet to verify that.
If we do want to go down this route (though I'm not sure, since your reg bank solution seems to get us the same guarantees but without bringing generics into this), then the following may be an okay idea:
I think having just the iomem deref genericised may be a good middle ground. If instead of making it a deref, we make it return the pointer to the member into the component that it can then deref, then the component-specific part can be pure (since offset of the iomem member is constant so for a particular pointer to a component, the pointer to the iomem member only depends on the passed-in pointer to component.)
This should make sure that when the compiler gets
panthor_gpu_write(ptdev->gpu, foo, bar);
val =panthor_gpu_read(ptdev->gpu, baz);it can optimise the expanded
iomem =
*panthor_get_iomem_ptr(ptdev->gpu);panthor_actual_write(iomem, foo, bar);
iomem =*panthor_get_iomem_ptr(ptdev->gpu);
val =panthor_actual_read(iomem, baz);Quoted 8 lines
to the simplified
iomem =
*panthor_get_iomem_ptr(ptdev->gpu);panthor_actual_write(iomem, foo, bar);
val =panthor_actual_read(iomem, baz);because
panthor_get_iomem_ptrwill be known to return the same value when called with the same input param.Right, as long as the compiler sees the definition of panthor_get_<component>_iomem_ptr() (which should be the case any time a read/write happens inside the panthor_<component>.c compilation unit), it hopefully inlines the whole thing and you get the iomem pointer from a direct deref rather than a function call. LTO might even give us link time optim for the bits in
panthor_hw.cwhere the compiler can't see through struct panthor_{gpu,pwr}.This being said, there's still no guarantee that one would mix regs and banks randomly, like
gpu_read(ptdev->mmu, GPU_ID);Quoted 4 lines
Anway, I think it's probably best if I abandon this and you just send your patch to the list with a real base. I only have one comment on it, which I've included inline.
Quoted 5 lines
Let's wait for Liviu's and Steve's feedback before taking any action, cause that's still quite a lot of changes, and it's not clear it will help much once we've got all the pending patchset rebased on misc-next (that's a mistake you do once at rebase time, once you've got bitten, you tend to be more careful ;-)).
This is always the problem with changing this sort of thing - I was somewhat wary of the whole split things by component series. But I think there's some hardware changes in the pipeline that effectively require something like that. And perhaps going forward the code will be slightly easier to reason about.
Boris' patch is certainly the neater - the static_assert is rather a point fix and doesn't solve the underlying problem. I'm beginning to wonder if we should have gone with a mmu_write/job_write/gpu_write() split at the function level. Which would have the benefit of not having to deal with iomem pointers.
Ultimately as a C programmer I tend to feel it's a case of "once bitten, twice shy" and you learn to be careful around things like this. Rust programmers tend to have the opposite viewpoint and want the compiler to detect anything incorrect, but given it's a C driver I'm not sure there is a fully robust way of making this "safe".
TLDR; I'd be happy to merge (a fixed version of) Boris' patch (after some sanity testing). I'm not convinced it's worth trying too hard to introduce lots of type safety as C really doesn't give us the tools.
Thanks,
Steve
Liviu Dudau
maintainerQuoted 122 lines · Steven Price
Quoted 115 lines · Boris Brezillon
Quoted 30 lines · On Mon, 11 May 2026 15:53:31 +0200 Nicolas Frattaroli…
Quoted 30 lines
Quoted 8 lines
{
(quoted diff) +2
-1return readl_relaxed(iomem + reg); } -static inline void gpu_write64(void __iomem *iomem, u32 reg, u64 data) +/* + * The function signature of gpu_read/gpu_write/gpu_read_relaxed/... used toReview
Quoted 10 lines
(quoted diff) +10
-0+ * take a &struct panthor_device* as the first parameter. During the split of + * iomem ranges into individual sub-components, this was changed to take a + * void __iomem* instead. These wrappers exists Tto avoid situations wherein + * pre-refactor patches are applied in error, as they'd compile fine. That's + * because the old calling convention's first parameter implicitly casts to a + * void pointer. + */ + +#define gpu_write(iomem, reg, data) ({ \ + static_assert(!__same_type((iomem), struct panthor_device *)); \Hm, this only covers ptdev being passed as an iomem pointer. I know it's the only case we had so far, but if we're going to add type enforcement, I think I'd prefer if we were covered for more than just ptdev.
One way of doing that would be to wrap the
void __iomem *iomemin an explicit type like:struct panthor_reg_bank { void __iomem *iomem; };Quoted 5 lines
Quoted 1 line
which then gets passed to gpu_{read,write} helpers (see the diff below).
Hm, okay, the diff below is smaller than I feared. Though it doesn't get us type checking for someone, say, trying to read
GPU_STATUSwith the iomem ofpanthor_fw.Yep, that's annoying, though solving that would require connecting reg definitions (in
panthor_xxx_regs.h) to a specificreg_bank, which is only doable if we provide per-component accessors like:#define
mmu_reg(_mmu, _name)((_mmu)->iomem +MMU_##)#define
mmu_read(_mmu, _name)gpu_read_iomem(mmu_reg(_mmu, _name))#define mmu_write(_mmu, _name, _val) \ gpu_write_iomem(mmu_reg(_mmu, _name),_ val)Quoted 39 lines
But neither does my proposal below.
Quoted 4 lines
The other way would be to pass the component, and have the macro do the <component>->iomem deref, but there's a few places where reg banks are accessed outside of the components that own them (
panthor_hw.c).Yeah, I prototyped going down something along that route by having the register accessors be generics that are implemented by each component, and it's a bit messy. Either you expose the struct definitions of individual components so that this header has visibility into them (not great), or you add boilerplate "do this accessor operation for this component" helpers for every component, which is both verbose and possibly causes the inlining to no longer work, though I have yet to verify that.
If we do want to go down this route (though I'm not sure, since your reg bank solution seems to get us the same guarantees but without bringing generics into this), then the following may be an okay idea:
I think having just the iomem deref genericised may be a good middle ground. If instead of making it a deref, we make it return the pointer to the member into the component that it can then deref, then the component-specific part can be pure (since offset of the iomem member is constant so for a particular pointer to a component, the pointer to the iomem member only depends on the passed-in pointer to component.)
This should make sure that when the compiler gets
panthor_gpu_write(ptdev->gpu, foo, bar);
val =panthor_gpu_read(ptdev->gpu, baz);it can optimise the expanded
iomem =
*panthor_get_iomem_ptr(ptdev->gpu);panthor_actual_write(iomem, foo, bar);
iomem =*panthor_get_iomem_ptr(ptdev->gpu);
val =panthor_actual_read(iomem, baz);Quoted 8 lines
to the simplified
iomem =
*panthor_get_iomem_ptr(ptdev->gpu);panthor_actual_write(iomem, foo, bar);
val =panthor_actual_read(iomem, baz);because
panthor_get_iomem_ptrwill be known to return the same value when called with the same input param.Right, as long as the compiler sees the definition of panthor_get_<component>_iomem_ptr() (which should be the case any time a read/write happens inside the panthor_<component>.c compilation unit), it hopefully inlines the whole thing and you get the iomem pointer from a direct deref rather than a function call. LTO might even give us link time optim for the bits in
panthor_hw.cwhere the compiler can't see through struct panthor_{gpu,pwr}.This being said, there's still no guarantee that one would mix regs and banks randomly, like
gpu_read(ptdev->mmu, GPU_ID);Quoted 4 lines
Anway, I think it's probably best if I abandon this and you just send your patch to the list with a real base. I only have one comment on it, which I've included inline.
Quoted 5 lines
Let's wait for Liviu's and Steve's feedback before taking any action, cause that's still quite a lot of changes, and it's not clear it will help much once we've got all the pending patchset rebased on misc-next (that's a mistake you do once at rebase time, once you've got bitten, you tend to be more careful ;-)).
Quoted 5 lines
This is always the problem with changing this sort of thing - I was somewhat wary of the whole split things by component series. But I think there's some hardware changes in the pipeline that effectively require something like that. And perhaps going forward the code will be slightly easier to reason about.
Yes, unfortunately Arch15 brings a lot of components shifting their relative positions and some registers widening, which breaks the neat, generic thing we had until now.
Quoted 6 lines
Boris' patch is certainly the neater - the
static_assertis rather a point fix and doesn't solve the underlying problem. I'm beginning to wonder if we should have gone with a mmu_write/job_write/gpu_write() split at the function level. Which would have the benefit of not having to deal with iomem pointers.
Once the dust settles that would be my preferred option too. I don't like the fact that we expose the iomem pointer everywhere and in some places that's the start of the component block, in other is the start of the registers handling IRQs, etc.
Quoted 10 lines
Ultimately as a C programmer I tend to feel it's a case of "once bitten, twice shy" and you learn to be careful around things like this. Rust programmers tend to have the opposite viewpoint and want the compiler to detect anything incorrect, but given it's a C driver I'm not sure there is a fully robust way of making this "safe".
TLDR; I'd be happy to merge (a fixed version of) Boris' patch (after some sanity testing). I'm not convinced it's worth trying too hard to introduce lots of type safety as C really doesn't give us the tools.
Yes, I'm happy with Boris' change. I'm also not convinced on the benefit of trying to prevent future patches from using outdated versions of the function signatures, unless someone ends up backporting them to kernels that are old and things compile but get broken.
Best regards,
Liviu
Quoted 4 lines
Thanks,
Steve
Signature · 8 lines
====================
| I would like to |
| fix the world, |
| but they're not |
| giving me the |
\ source code! /
---------------
¯\_(ツ)_/¯