[RFC PATCH] iommu/rockchip: program the MMU even when the device is not in use

Robin Murphy robin.murphy at arm.com
Wed Nov 25 08:15:12 PST 2026


On 25/11/2026 12:23 pm, Anton Sovetov wrote:
> Hi Robin,
> 
> Thanks, that's a fair reading. I'll take 2 and 3 as answered: zapping a
> disabled MMU is pointless when resume re-enables it with ZAP_CACHE, and an
> attach that leaves paging off is fine because the client's next runtime
> resume turns it on anyway.
> 
> On 1 I'd like to check one thing before I drop it. pm_runtime_get_if_in_use()
> returns 0 not only when the device is suspended but also while it is still
> RPM_ACTIVE with the usage count already at zero, and during the suspending
> transition, since pm_runtime_put() defers the suspend to the workqueue. In
> that window the MMU is still paging and can latch a fault the handler then
> discards, which is how I ended up with dte 0x480000 status 0x2b fault 0xdc80
> latched and nothing in dmesg. Would pm_runtime_get_if_active() be acceptable
> there, or is that window too narrow to be worth the change?

Ah, good point - it is indeed possible for the client to cause a fault 
but then immediately suspend itself before we've managed to take the 
interrupt, so technically anything up to the point that we actually 
disable the IOMMU interrupt sources in rk_iommu_suspend() can still be 
considered legitimate. So yes, I think I'd agree that RPM_ACTIVE is a 
more accurate condition than usage_count here (strictly that still 
leaves a small window for "missed" interrupts during RPM_SUSPENDING 
itself but oh well).

That now also makes me wonder if autosuspend etc. could ever result in 
the IOMMU's usage_count going from 0 back to 1 without the full 
suspend/resume cycle - if so then we probably want the same change in 
the other 3 instances too.

Thanks,
Robin.
  > Either way I'm now looking at the client driver's RPM accounting as you
> suggest, and will report back what I find.
> 
> вт, 24 нояб. 2026 г. в 20:57, Robin Murphy <robin.murphy at arm.com>:
>>
>> On 23/11/2026 8:07 pm, Anton Sovetov wrote:
>>> Hi,
>>>
>>> I hit three related problems in drivers/iommu/rockchip-iommu.c on an RK3566
>>> (Radxa Zero 3W) with the out-of-tree NPU driver. All three have the same shape:
>>> the driver gates work on pm_runtime_get_if_in_use(), which is 0 unless somebody
>>> already holds a reference, and for a device that keeps its DMA domain for its
>>> whole lifetime that is the normal state.
>>>
>>> 1. rk_iommu_irq(), line 583, gate at 592.
>>>
>>>      A page fault raised while the device is running, outside a map or unmap
>>>      call, finds usage_count 0,
>>
>> That sounds wrong - if "the device is running" then its RPM state should
>> be logically active, which should in turn hold the IOMMU active via the
>> device link. And if the client device is still doing DMA and able to
>> cause page faults while logically suspended, that would seem like a bug
>> in the client driver.
>>
>>> so the handler returns IRQ_NONE without touching
>>>      the registers. The fault stays latched: INT_RAWSTAT bit 0 and INT_STATUS bit
>>>      0 remain set, PAGE_FAULT_ACTIVE stays set in STATUS, REPLAY_BUFFER_EMPTY
>>>      clears, and nothing is printed. The device then sits on a stalled
>>>      transaction and dmesg says nothing at all, which is the worst part for
>>>      anyone trying to debug it.
>>>
>>>      Observed on the NPU: after a job that never completed, the MMU read back
>>>      dte 0x480000 status 0x2b rawstat 0x1 intstatus 0x1 fault 0xdc80, and the
>>>      handler had never run. A normal fault report would have printed "Page fault
>>>      at 0x0000dc80 of type read" plus the page table walk.
>>>
>>> 2. rk_iommu_zap_iova(), line 680, gate at 694.
>>>
>>>      The comment says "Only zap TLBs of IOMMUs that are powered on", but the test
>>>      is the usage count, not the power state. An unmap that happens while nothing
>>>      else is using the IOMMU skips the shootdown. A device that keeps its domain
>>>      but is idle at that moment, which is the usual case when the process that
>>>      owned the buffers has exited, keeps stale entries, and the next owner of the
>>>      same iova can still be translated to the previous physical page.
>>>
>>>      I could not prove this one with a bus trace, so I am reporting it as a code
>>>      observation and not as a measured failure.
>>
>> The physical power state shouldn't matter - as long as the IOMMU is
>> logically suspended then we should have been through rk_iommu_suspend()
>> -> rk_iommu_disable(), after which we're not going to touch it again
>> until rk_iommu_resume() -> rk_iommu_enable() which inherently does an
>> RK_MMU_CMD_ZAP_CACHE. Thus resuming a suspended IOMMU, invalidating all
>> the TLBs in the process, just to issue a command to re-invalidate
>> nothing, then disabling it until the client device next resumes it and
>> invalidates all the TLBs yet again, clearly seems a bit silly.
>>
>>> 3. rk_iommu_attach_device(), line 1008, gate at 1040.
>>>
>>>      Detach plus attach is how a driver resets its IOMMU domain. On attach, when
>>>      the usage count is 0, the function returns 0 without calling
>>>      rk_iommu_enable(), so paging stays off, DTE_ADDR stays 0 and INT_MASK stays
>>>      0. The caller is told the attach succeeded.
>>>
>>>      Observed on the NPU: its soft reset does iommu_detach_device() and then
>>>      iommu_attach_device() on the same domain. After the first reset the MMU read
>>>      dte 0x0 status 0x18 intmask 0x0, no translation at all, and every later DMA
>>>      ran on raw iovas. Before the reset it was dte 0x480000 status 0x19
>>>      intmask 0x3. Holding the MMU device with pm_runtime_resume_and_get() around
>>>      the detach/attach makes the same sequence end with dte 0x480000 status 0x19,
>>>      which is how I convinced myself the gate, and not the reset itself, was
>>>      responsible.
>>
>> Again, that sounds very much like something is wrong with either the
>> device link or how the out-of-tree client driver is managing its own
>> RPM. And since the device link support has been upstream and apparently
>> working fine with the upstream VOP/RGA/etc. client drivers for over 8
>> years now...
>>
>> Thanks,
>> Robin.
>>
>>> The patch below resumes the device around the attach and the zap. Both run in
>>> process context, so pm_runtime_get_sync() is fine there. For the interrupt
>>> handler I only added a log line on the drop path, because I do not want to
>>> touch registers from hard irq context while the device may be suspended.
>>>
>>> What it fixes: the MMU now stays programmed across a detach/attach cycle, and a
>>> fault that arrives while nothing is mapping gets reported instead of vanishing.
>>>
>>> What it does not fix: the reason I went looking. Our NPU still stops executing
>>> jobs in IOMMU mode after a few separate processes, with a healthy MMU and no
>>> page fault at all. That is a different problem and I am not claiming this patch
>>> touches it. I am sending these three because each one is a real failure mode on
>>> its own.
>>>
>>> Tested only on rk3566 with the v1 block. I have no rk3588 hardware and would
>>> rather not guess about the v2 path.
>>>
>>> Questions:
>>>
>>> - Is the usage count gate deliberate, in the sense that register access is
>>>     unsafe unless someone holds a reference? If so, is the right fix to keep the
>>>     MMU programmed for as long as the device is attached to a domain, instead of
>>>     tying it to the map/unmap window?
>>> - For the interrupt path, what is the intended behaviour when a fault arrives
>>>     while the IOMMU device is runtime suspended? Ignoring it leaves the device
>>>     stalled and the status latched, which is what we see today.
>>>
>>> Signed-off-by: Anton Sovetov <anton.sovetov at gmail.com>
>>>
>>> ---
>>>    drivers/iommu/rockchip-iommu.c | 42 ++++++++++++++++++++++------------
>>>    1 file changed, 28 insertions(+), 14 deletions(-)
>>>
>>> diff --git a/drivers/iommu/rockchip-iommu.c b/drivers/iommu/rockchip-iommu.c
>>> index 07e3517..a3da612 100644
>>> --- a/drivers/iommu/rockchip-iommu.c
>>> +++ b/drivers/iommu/rockchip-iommu.c
>>> @@ -590,8 +590,11 @@ static irqreturn_t rk_iommu_irq(int irq, void *dev_id)
>>>     int i, err;
>>>
>>>     err = pm_runtime_get_if_in_use(iommu->dev);
>>> - if (!err || WARN_ON_ONCE(err < 0))
>>> + if (!err || WARN_ON_ONCE(err < 0)) {
>>> + dev_err(iommu->dev,
>>> + "interrupt while the mmu is not in use, ignored\n");
>>>     return ret;
>>> + }
>>>
>>>     if (WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks)))
>>>     goto out;
>>> @@ -691,17 +694,19 @@ static void rk_iommu_zap_iova(struct
>>> rk_iommu_domain *rk_domain,
>>>
>>>     iommu = list_entry(pos, struct rk_iommu, node);
>>>
>>> - /* Only zap TLBs of IOMMUs that are powered on. */
>>> - ret = pm_runtime_get_if_in_use(iommu->dev);
>>> + /*
>>> + * The usage count says nothing about the power state, so an
>>> + * unmap that runs while the IOMMU is idle skipped the
>>> + * shootdown and left stale translations for the next owner of
>>> + * the same iova.
>>> + */
>>> + ret = pm_runtime_get_sync(iommu->dev);
>>>     if (WARN_ON_ONCE(ret < 0))
>>>     continue;
>>> - if (ret) {
>>> - WARN_ON(clk_bulk_enable(iommu->num_clocks,
>>> - iommu->clocks));
>>> - rk_iommu_zap_lines(iommu, iova, size);
>>> - clk_bulk_disable(iommu->num_clocks, iommu->clocks);
>>> - pm_runtime_put(iommu->dev);
>>> - }
>>> + WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks));
>>> + rk_iommu_zap_lines(iommu, iova, size);
>>> + clk_bulk_disable(iommu->num_clocks, iommu->clocks);
>>> + pm_runtime_put(iommu->dev);
>>>     }
>>>     spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
>>>    }
>>> @@ -1037,13 +1042,22 @@ static int rk_iommu_attach_device(struct
>>> iommu_domain *domain,
>>>     list_add_tail(&iommu->node, &rk_domain->iommus);
>>>     spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
>>>
>>> - ret = pm_runtime_get_if_in_use(iommu->dev);
>>> - if (!ret || WARN_ON_ONCE(ret < 0))
>>> - return 0;
>>> + /*
>>> + * Detach plus attach is how drivers reset their domain, and the caller
>>> + * has no way to notice that the MMU was left unprogrammed. Resume the
>>> + * device and program it.
>>> + */
>>> + ret = pm_runtime_get_sync(iommu->dev);
>>> + if (ret < 0) {
>>> + dev_err(dev, "cannot resume the mmu, ret: %d\n", ret);
>>> + return ret;
>>> + }
>>>
>>>     ret = rk_iommu_enable(iommu);
>>> - if (ret)
>>> + if (ret) {
>>> + dev_err(dev, "mmu enable failed, ret: %d\n", ret);
>>>     WARN_ON(rk_iommu_identity_attach(&rk_identity_domain, dev));
>>> + }
>>>
>>>     pm_runtime_put(iommu->dev);
>>>
>>>
>>
> 
> 




More information about the Linux-rockchip mailing list