accel/rocket: DVFS for the RK3588 NPU
Subsystems (4)
v2 cover accel/rocket: DVFS for the RK3588 NPU
The rocket driver runs the NPU at whatever rate the devicetree pinned it to: 200 MHz on the RK3588, out of the 1 GHz the hardware reaches. This series adds devfreq, the OPP table and the thermal plumbing that go with it.
v1: https://lore.kernel.org/r/[email protected]
As Tomeu asked [1], the slot-search fix now leads the series, with the two rocket fixes this builds on and, as patch 4, 2/2 of my July lifecycle series [2], without which v2 oopsed when one core was unbound, bound and unbound again. KASAN found that v1 4/7 (now 8) kept a clk handle that unbinding freed; the notes under each --- list the rest.
Hardware constraint
-------------------
The three cores share one clock and one supply. The clock comes from a PVTPLL inside the NPU power islands: for a PVTPLL rate the firmware programs NPU GRF registers inside the islands, for the 200 MHz boot rate it only switches CRU selectors to GPLL (clk_npu_set_rate() in plat/rockchip/rk3588/drivers/scmi/rk3588_clk.c, TF-A v2.12.0, which Nicolas pointed me to). An island powered up while the clock is above the boot rate never acknowledges the power-on, and the next register access into it takes an asynchronous SError. Returning to the boot rate is safe at any time; raising the clock is not, so while it is above the boot rate every core is held runtime resumed and no island can transition. Unbinding any core takes devfreq down until all are bound again.
The firmware accepts only the nine rates of its rk3588_npu_pvtpll_table, 300 to 1000 MHz in 100 MHz steps plus 200 MHz, and refuses any other. The refusal never reaches the caller: clk_change_rate() ignores what ->set_rate() returns, so clk_set_rate() reports success and the clock stays put. Hence patch 7 names exactly those nine.
The table is on all three cores with opp-shared (Nicolas's review); the one devfreq device sits on rknn_core_0, the lowest devicetree index. Patch 9 explains the maximum over the cores and why runtime suspend does not call devfreq_suspend_device().
Testing
-------
Orange Pi 5 Plus, 7.3.0-rc2 (drm-misc-next 3afbeb6b99dd) plus this series, in-tree rocket, BL31 v2.12.0-10-g70d814213: the edk2-rk3588 TF-A branch with upstream d2d6928641ba, so that it reports the PVTPLL rate it measures. MobileNetV1 through Teflon, 3 x 20 s per arm, one thread pinned to one CPU, CPUs on the performance governor, a bit-exact oracle on every inference, polling_interval at the driver's 50 ms, core 0's interrupt on a CPU with its deep idle state disabled.
cur_freq is only the driver's request and scmi_clk_npu on the GPLL path echoes the last accepted rate, so the CRU selector is the witness (CLKSEL_CON74 bit 0 = PVTPLL, CLKSEL_CON73 = GPLL divider), read once a second:
200 MHz OPP, userspace 91.15 inf/s CRU GPLL/6 = 198 MHz, 700 mV 1000 MHz OPP, userspace 234.45 inf/s CRU PVTPLL (BL31 1 GHz), 850 mV simple_ondemand 232.88 inf/s OPPs 200-1000 MHz 200 MHz OPP again 90.87 inf/s drift 0.997
That is 2.57x pinned and 2.55x under the governor, 0.7 % below pinned; the oracle and the interrupts per inference were the same in all four arms.
Three clients, each with its own oracle, all three cores computing together, output bit-exact and identical to a single-client control:
OPP one client three clients total 900 MHz, 800 mV 234.42 199.33 + 198.95 + 199.19 597.47 inf/s 1000 MHz, 850 mV 240.49 203.16 + 203.01 + 203.46 609.63 inf/s
After 25 s of load the governor is at the 200 MHz OPP from the first sample (0.24 s apart) through the 60 s idle window: CRU 198 MHz in all 60 one-second samples, rail 700 mV. The islands were power-cycled five times at 700 mV and powered down once more after a raise to 1000 MHz. All six bind orders give bit-exact output, with devfreq and cooling back on rknn_core_0 after every rebind.
Unbind and reload: 25 rounds of all three cores, 4 of a single core with an inference while it was gone, 5 rmmod rounds, and 3 unbinds plus 1 rmmod with the clock raised (CRU on the PVTPLL before each). Without patch 4 the single-core round oopses, as its commit message shows.
On a KASAN, kmemleak and PROVE_LOCKING build of the same tree the unbind (no client submitting), reload, three-client and island tests gave no report and kmemleak nothing from this driver, and a lockdep run through the governor, cooling, three clients, and unbind and rmmod with the clock raised to the 1 GHz OPP left lockdep enabled.
Build: the series was rebased onto drm-misc-next 8ef59ee79407 to send it; apart from one comment corrected in 9 (rocket_devfreq.o is byte-identical), the patches are the ones tested above. On that base, arm64, gcc 13.3: each patch that touches drivers/accel/rocket builds with W=1 and no warning, and sparse reports nothing in drivers/accel/rocket; 9, 10 and 11 also build with W=1 and no warning with CONFIG_DEVFREQ_THERMAL=n. dt_binding_check at 6 and 11, and CHECK_DTBS on rk3588-orangepi-5-plus, rk3588s-orangepi-5 and rk3588-edgeble-neu6b-io at the base, 7 and 11, give no schema warning. A full Image, modules and dtbs build at 11 completes with no warning from drivers/accel/rocket. checkpatch --strict flags only the 75-column oops line in 4 and MAINTAINERS for the new files in 9, which
F: drivers/accel/rocket/ covers.
Two corrections to my reply of 9 September [3]: the supply could have come down to the 200 MHz voltage even had the clock change failed, since the refusal is not reported, so the CRU selector is the witness now; and the v1 governor arm was the governor running, not limits pinned by hand.
Not done
--------
- Unbinding a core while clients submit is still unsafe. Patch 3 notes that an open file keeps an entity on the departed core's scheduler; on the KASAN build (three clients running, then as root "echo fdac0000.npu > /sys/bus/platform/drivers/rocket/unbind") that is a use-after-free:
BUG: KASAN: slab-use-after-free in _raw_spin_lock+0xb4/0x118
drm_sched_rq_remove_entity+0x94/0x2b0 [gpu_sched]
drm_sched_entity_select_rq+0x1f4/0x698 [gpu_sched]
drm_sched_job_arm+0x50/0x258 [gpu_sched]
rocket_job_push+0x2f8/0x6b8 [rocket]
rocket_ioctl_submit+0x888/0xd28 [rocket]
Freed by: drm_sched_fini <- rocket_job_fini <- rocket_core_fini <-
rocket_remove then a soft lockup: drm_sched_fini() frees the run queues while the entity still points at one. drm-misc-next has the same path (read, not run): rocket_job_open() builds the entity over every core, and rocket_remove() ignores open files. The fixes I see are policy, suppress_bind_attrs (undoing patch 4) or unplugging the DRM device when any core goes. Tomeu, which would you prefer? I will write it.
- Probe and remove are not serialised against open, and a file opened while a core probes can see its scheduler uninitialised; both predate this series.
- Lifecycle 1/2 [2] (a devres leak that moves the accel minor up after every full unbind and rebind, invisible to kmemleak) builds on Guangshuo Li's "accel/rocket: clear rdev on device init failure" [4], which is not in drm-misc-next. Sashiko's other pre-existing findings on the standalone slot-search patch, which I agreed with [5], stand unless patch 4 covers them.
- Not exercised: a devicetree without the OPP table or without assigned-clock-rates (code-read only), throttling at 85 degrees (not reachable here), system suspend (s2idle does not resume on this board, for reasons outside the NPU), kexec from a raised clock, and any other board. No milliwatt figure, no energy model.
Routing
-------
Patches 1-4 are fixes with Cc: stable and 9 builds on 1, 2 and 4, so the simplest route is all but the two dts patches through drm-misc-next. The binding (6) must land before those (7 and 11, Heiko's tree), which I can resend once the rest is in. 1-3 supersede their standalone postings (links under each ---); 5 is also 01/14 of Jiaxing Hu's RK3576 series, and whichever lands first, the other drops it. I have not rebased onto that series.
Questions
---------
Q1. DT maintainers: is opp-shared on all three cores the right
description of one clock and one supply? v2 leaves opp-suspend out; Nicolas found it "not 100% fit" for 200 MHz but not unfit either, and asked for your view [6].
Q2. Maximum over the cores, or a summed busy count? And no OPP table
means no devfreq and no error, as in panfrost and lima: the policy you want?
Q3. assigned-clock-rates stays on all three nodes. Jiaxing reports that
on the RK3576 it hangs the board before the console when set on the SCMI clock, and that the vendor driver never writes that rate from DT [7]. I have not booted this board without it. Keep it, or drop it in a patch on top?
Nicolas also floated the component framework; I have not tried it.
Credits
-------
Nicolas Dufresne: review, opp-shared, the TF-A pointer, and a proof of concept with the same rates and voltages. Tomeu Vizoso agreed to the full-range table [8]. Jiaxing Hu: the RK3576 multi-core corruption the three-client test answers, a test and a review. Sidong Yang: the report behind patch 3 and a test.
Jonas Karlman pointed me at his GPU approach, a .config_clks() that checks pm_runtime_suspended() [9][10], and warned that "just reading clk_summary to read SCMI clock rate can issue SError or a freeze" [11]. From the code, not a test: ->get_cur_freq() returns the last request, probe reads the boot rate with the domain on, and with the in-tree devicetree every set_rate that reaches the firmware, re-read included, runs with the cores held.
Hüseyin BIYIK wrote TF-A commit d2d6928641ba, which makes BL31 report the PVTPLL rate it measures rather than the one asked for, and guessed early that the clock framework ignores what SCMI answers [12]; for a refused set_rate it does.
The Assisted-by: LLM tags are Claude (Fable 5.1 and Opus 5; the v1 cover named only Opus 5, though Fable 5.1 worked on v1 too). We wrote the code and these messages together, including the v3 changes to patch 4; it also found the silent refusal with me in the clock, OPP and TF-A code, wrote and adversarially reviewed the test scripts, and analysed the crash patch 4 fixes and the one under Not done. I threw out plenty of what it proposed. The board, every boot and measurement, the decision to send this and the responsibility for it are mine.
[1] https://lore.kernel.org/r/CAPsqS2QXQda2ftbEJb_-Qeva9ay4+4ysdpUtj8jAvbyAsYiAJw@mail.gmail.com
[2] https://lore.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lore.kernel.org/r/[email protected]
[5] https://lore.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/[email protected]
[7] https://lore.kernel.org/r/[email protected]
[8] https://lore.kernel.org/r/CAPsqS2RRUyG1vz5Nm7990yd0hcKR5E-SWhbt0ishaVYvCqQftw@mail.gmail.com
[9] https://lore.kernel.org/r/[email protected]
[10] https://github.com/Kwiboo/linux-rockchip/commits/next-20260619-rk35xx-scmi-gpu-clk/
[11] https://lore.kernel.org/r/[email protected]
[12] https://lore.kernel.org/r/[email protected]
Igor Paunovic (11):
accel/rocket: search every core slot when a core is removed accel/rocket: number the cores by devicetree position, not bind order accel/rocket: search every core slot when looking up a scheduler accel/rocket: keep core slots stable across unbind and rebind accel/rocket: request the core clocks by name
dt-bindings: npu: rockchip: allow DVFS and thermal properties
arm64: dts: rockchip: rk3588: add an OPP table for the NPU accel/rocket: restore the NPU clock boot rate before powering the
cores down
accel/rocket: add devfreq support
accel/rocket: register a devfreq cooling device
arm64: dts: rockchip: rk3588: add passive cooling to the NPU thermal
zone
.../npu/rockchip,rk3588-rknn-core.yaml | 10 + arch/arm64/boot/dts/rockchip/rk3588-base.dtsi | 17 +- arch/arm64/boot/dts/rockchip/rk3588-opp.dtsi | 54 ++ drivers/accel/rocket/Kconfig | 2 + drivers/accel/rocket/Makefile | 1 + drivers/accel/rocket/rocket_core.c | 14 + drivers/accel/rocket/rocket_core.h | 16 + drivers/accel/rocket/rocket_devfreq.c | 525 ++++++++++++++++++ drivers/accel/rocket/rocket_devfreq.h | 67 +++ drivers/accel/rocket/rocket_device.c | 2 + drivers/accel/rocket/rocket_device.h | 21 + drivers/accel/rocket/rocket_drv.c | 192 ++++++- drivers/accel/rocket/rocket_job.c | 47 +- 13 files changed, 945 insertions(+), 23 deletions(-) create mode 100644 drivers/accel/rocket/rocket_devfreq.c create mode 100644 drivers/accel/rocket/rocket_devfreq.h
base-commit: 8ef59ee794076e2b58cff357b12de2ba5d441271
Signature · 1 line
2.43.0
PATCH v2 1/11 accel/rocket: search every core slot when a core is removed
rocket_remove() decrements rdev->num_cores for each core it removes, while find_core_for_dev() searches slots 0 to num_cores - 1. Unbinding the cores in the order they were bound therefore loses the last one: by the time it is removed the search range has already shrunk past its slot, so find_core_for_dev() returns -1 and rocket_remove() gives up without doing anything.
num_cores never reaches zero, rocket_device_fini() never runs, and the file-scoped rdev keeps pointing at a device that is going away. Binding the cores again starts from that stale count, because rocket_probe() takes rdev->num_cores as the slot to fill. On an RK3588, which describes three cores, the second round lands on slots 1, 2 and 3 while rdev->cores was allocated with room for three:
rocket fdab0000.npu: Rockchip NPU core 1 version: 1179210309 rocket fdac0000.npu: Rockchip NPU core 2 version: 1179210309 rocket fdad0000.npu: Rockchip NPU core 3 version: 1179210309
The write to rdev->cores[3] is past the end of the array.
Nothing in tree reads the core array often enough to notice, so the overrun is silent today. It turned up while testing a devfreq series on top of this, where a worker walks every core a few times a second, and UBSAN caught the first bool it read out of the overrun entry:
UBSAN: invalid-load in drivers/accel/rocket/rocket_devfreq.c:47:10 load of value 5 is not a valid value for type '_Bool' Workqueue: devfreq_wq devfreq_monitor
Record how many slots were allocated and search all of them. Every core is then found on removal, num_cores reaches zero, the device is torn down and a later bind starts from a clean rdev.
This does not make unbinding a single core out of several work: probe still takes num_cores as the slot to fill, and rocket_open() still reaches for cores[0] whether or not anything is there. The fourth patch takes care of both.
Found by unbinding and rebinding all three cores on an Orange Pi 5 Plus. With this applied the cores land in slots 0, 1 and 2 every time, whichever order they are bound in, and the shared supply goes back to a single user after each round.
---
Two paragraphs changed from the standalone posting, which this series
supersedes: the one on unbinding a single core now points at patch 4 instead of saying what is left, and the round counts are dropped from the last one. Posting:
https://lore.kernel.org/r/[email protected]
The two Tested-by tags are from that thread. The round counts were taken on a kernel that carried the use-after-free fixed in 8/11; they were re-measured with the whole series applied, see the notes on patch 4 and the cover letter. The single-core unbind and rocket_open() cases in that paragraph are 2/2 of the lifecycle series, now rebased into this one as patch 4:
https://lore.kernel.org/r/[email protected]
drivers/accel/rocket/rocket_device.c | 2 ++ drivers/accel/rocket/rocket_device.h | 1 + drivers/accel/rocket/rocket_drv.c | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-)
drivers/accel/rocket/rocket_device.c +2 -0
@@ -31,6 +31,8 @@ struct rocket_device *rocket_device_init(struct platform_device *pdev,if (of_device_is_available (core_node)) num_cores++; + rdev->max_cores = num_cores; + rdev->cores =devm_kcalloc (dev, num_cores,sizeof (*rdev->cores),GFP_KERNEL );if (!rdev->cores)return ERR_PTR (-ENOMEM );
drivers/accel/rocket/rocket_device.h +1 -0
@@ -19,6 +19,7 @@ struct rocket_device {struct rocket_core *cores;unsigned int num_cores; +unsigned int max_cores; }; struct rocket_device *rocket_device_init(struct platform_device *pdev,
drivers/accel/rocket/rocket_drv.c +1 -1
@@ -223,7 +223,7 @@ static int find_core_for_dev(struct device *dev) {struct rocket_device *rdev =dev_get_drvdata (dev); -for (unsigned int core =0 ; core < rdev->num_cores; core++) { +for (unsigned int core =0 ; core < rdev->max_cores; core++) {if (dev == rdev->cores[core].dev)return core; }
Signature · 1 line
2.43.0
Heiko Stübner
maintainerRe: [PATCH v2 01/11] accel/rocket: search every core slot when a core is removed
Am Dienstag, 22. September 2026, 10:01:04 Mitteleuropäische Normalzeit schrieb Igor Paunovic:
Quoted 6 lines
rocket_remove()decrementsrdev->num_coresfor each core it removes, whilefind_core_for_dev()searches slots 0 tonum_cores- 1. Unbinding the cores in the order they were bound therefore loses the last one: by the time it is removed the search range has already shrunk past its slot, sofind_core_for_dev()returns -1 androcket_remove()gives up without doing anything.
[...]
Quoted 4 lines
Two paragraphs changed from the standalone posting, which this series
supersedes: the one on unbinding a single core now points at patch 4 instead of saying what is left, and the round counts are dropped from the last one. Posting:
Quoted 8 lines
https://lore.kernel.org/r/[email protected]
The two Tested-by tags are from that thread. The round counts were taken on a kernel that carried the use-after-free fixed in 8/11; they were re-measured with the whole series applied, see the notes on patch 4 and the cover letter. The single-core unbind androcket_open()cases in that paragraph are 2/2 of the lifecycle series, now rebased into this one as patch 4:
https://lore.kernel.org/r/[email protected]
Seems like I merged the standalone version in [0].
The code change is the same, so I guess we'll just have to live with the older commit description :-) .
TL;DR this has been merged in 7.3 already now.
Heiko
PATCH v2 2/11 accel/rocket: number the cores by devicetree position, not bind order
In reply to Igor Paunovicrocket_job_hw_submit() programs the S_POINTER registers of a core with an extra bit derived from core->index, the way the vendor driver derives it from the hardware number of the core. rocket_probe() sets core->index to the slot the core takes in rdev->cores[], which is the order the cores bind in.
The two agree only while the cores that bind are a prefix of the core nodes in the devicetree, in devicetree order. Unbind them and bind them back with a different core first, have one core's probe deferred behind a sibling's, or disable a core other than the last one, and every task submitted to a core whose slot is not its hardware number times out after 500 ms. The reset that follows does not help, and the inference finishes with wrong output.
Observed on an Orange Pi 5 Plus with a KASAN build, over all six bind orders of the three cores: only the devicetree order ran clean. The other five produced 27 to 141 "NPU job timed out". In four of them no output tensor changed with the input, and the harness gave up before its first measured round; the fifth got through a six-second run with 27 timeouts and a wrong top-1 class. All but one of the timeouts land on the cores whose slot is not their hardware number, in proportion to the tasks the scheduler hands them, and in both directions of the mismatch.
Number the cores by their position among the core nodes in the devicetree instead, which is what the hardware number is.
The wrong value has been assigned since the driver was added, but it only reached the hardware once the extra bit was introduced, hence the Fixes tag below.
---
Supersedes the standalone posting:
https://lore.kernel.org/r/[email protected]
Same diff. The message now says the numbers come from a KASAN build, corrects the timeout range to 27-141 against the raw log (it said 140), says what the four failed orders showed (the output did not change with the input; it said an oracle rejected the output), notes the one timeout that landed on a matching core, and drops the throughput figure, which was measured under KASAN.
drivers/accel/rocket/rocket_core.h | 5 +++++ drivers/accel/rocket/rocket_drv.c | 31 +++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-)
drivers/accel/rocket/rocket_core.h +5 -0
@@ -30,6 +30,11 @@struct rocket_core {struct device *dev;struct rocket_device *rdev; +/* +* Hardware number of the core: its position among the core nodes in +* the devicetree. Not an index into rdev->cores[] - that slot is what +* find_core_for_dev() returns. +*/ unsigned int index;int irq;
drivers/accel/rocket/rocket_drv.c +30 -1
@@ -157,10 +157,39 @@ static const struct drm_driver rocket_drm_driver = { .desc = "rocket DRM", }; +/* +* The extra bit that rocket_job_hw_submit() sets in the S_POINTER registers +* is the hardware number of the core, which is its position among the core +* nodes in the devicetree: a disabled core keeps its number. The slot a core +* takes in rdev->cores[] is the order the cores happened to bind in, and the +* two only agree while the cores that bind are a prefix of those nodes, in +* devicetree order. Every task submitted to a core whose slot is not its +* hardware number then times out. +*/ +static int rocket_core_hw_index (struct device *dev) +{ +struct device_node *np; +int index =0 ; + +for_each_matching_node (np, dev->driver->of_match_table) { +if (np == dev->of_node) { +of_node_put (np); +return index; + } + index++; + } + +return -ENODEV ; +} +static int rocket_probe (struct platform_device *pdev) { +int index =rocket_core_hw_index (&pdev->dev);int ret; +if (index <0 ) +return index; +if (rdev ==NULL ) {/* First core probing, initialize DRM device. */ rdev =rocket_device_init (drm_dev, &rocket_drm_driver);
@@ -176,7 +205,7 @@ static int rocket_probe(struct platform_device *pdev) rdev->cores[core].rdev = rdev; rdev->cores[core].dev = &pdev->dev; - rdev->cores[core].index = core; + rdev->cores[core].index = index; rdev->num_cores++;
Signature · 1 line
2.43.0
Re: [PATCH v2 02/11] accel/rocket: number the cores by devicetree position, not bind order
Am Dienstag, 22. September 2026, 10:01:05 Mitteleuropäische Normalzeit schrieb Igor Paunovic:
Quoted 26 lines
rocket_job_hw_submit()programs theS_POINTERregisters of a core with an extra bit derived fromcore->index, the way the vendor driver derives it from the hardware number of the core.rocket_probe()setscore->indexto the slot the core takes in rdev->cores[], which is the order the cores bind in.The two agree only while the cores that bind are a prefix of the core nodes in the devicetree, in devicetree order. Unbind them and bind them back with a different core first, have one core's probe deferred behind a sibling's, or disable a core other than the last one, and every task submitted to a core whose slot is not its hardware number times out after 500 ms. The reset that follows does not help, and the inference finishes with wrong output.
Observed on an Orange Pi 5 Plus with a KASAN build, over all six bind orders of the three cores: only the devicetree order ran clean. The other five produced 27 to 141 "NPU job timed out". In four of them no output tensor changed with the input, and the harness gave up before its first measured round; the fifth got through a six-second run with 27 timeouts and a wrong top-1 class. All but one of the timeouts land on the cores whose slot is not their hardware number, in proportion to the tasks the scheduler hands them, and in both directions of the mismatch.
Number the cores by their position among the core nodes in the devicetree instead, which is what the hardware number is.
Quoted 8 lines
The wrong value has been assigned since the driver was added, but it only reached the hardware once the extra bit was introduced, hence the Fixes tag below.
Fixes 0810d5ad88a1 ("accel/rocket: Add job submission IOCTL")Assisted-by LLM sparse checkpatchSigned-off-by Igor Paunovic <[email protected]>
nits: both the commit message, as well as the comment blocks are excessivly long, and could be shortened a lot, if a future version is needed.
Heiko
Quoted 84 lines
---
Supersedes the standalone posting:
https://lore.kernel.org/r/[email protected]
Same diff. The message now says the numbers come from a KASAN build, corrects the timeout range to 27-141 against the raw log (it said 140), says what the four failed orders showed (the output did not change with the input; it said an oracle rejected the output), notes the one timeout that landed on a matching core, and drops the throughput figure, which was measured under KASAN.drivers/accel/rocket/rocket_core.h | 5 +++++ drivers/accel/rocket/rocket_drv.c | 31 +++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-)drivers/accel/rocket/rocket_core.h +5
-0@@ -30,6 +30,11 @@struct rocket_core {struct device *dev;struct rocket_device *rdev; +/* +* Hardware number of the core: its position among the core nodes in +* the devicetree. Not an index into rdev->cores[] - that slot is what +* find_core_for_dev() returns. +*/ unsigned int index;int irq;drivers/accel/rocket/rocket_drv.c +30
-1@@ -157,10 +157,39 @@ static const struct drm_driver rocket_drm_driver = { .desc = "rocket DRM", }; +/* +* The extra bit that rocket_job_hw_submit() sets in the S_POINTER registers +* is the hardware number of the core, which is its position among the core +* nodes in the devicetree: a disabled core keeps its number. The slot a core +* takes in rdev->cores[] is the order the cores happened to bind in, and the +* two only agree while the cores that bind are a prefix of those nodes, in +* devicetree order. Every task submitted to a core whose slot is not its +* hardware number then times out. +*/ +static int rocket_core_hw_index (struct device *dev) +{ +struct device_node *np; +int index =0 ; + +for_each_matching_node (np, dev->driver->of_match_table) { +if (np == dev->of_node) { +of_node_put (np); +return index; + } + index++; + } + +return -ENODEV ; +} +static int rocket_probe (struct platform_device *pdev) { +int index =rocket_core_hw_index (&pdev->dev);int ret; +if (index <0 ) +return index; +if (rdev ==NULL ) {/* First core probing, initialize DRM device. */ rdev =rocket_device_init (drm_dev, &rocket_drm_driver);@@ -176,7 +205,7 @@ static int rocket_probe(struct platform_device *pdev) rdev->cores[core].rdev = rdev; rdev->cores[core].dev = &pdev->dev; - rdev->cores[core].index = core; + rdev->cores[core].index = index; rdev->num_cores++;
PATCH v2 3/11 accel/rocket: search every core slot when looking up a scheduler
In reply to Igor Paunovicsched_to_core() walks rdev->cores[] up to rdev->num_cores, and rocket_remove() decrements num_cores for every core it removes. Unbind a core that is not the last one and the cores behind it fall outside the search, so sched_to_core() returns NULL for a core that is still bound and still running jobs. Neither caller checks the result:
rocket_job_run(): rocket_fence_create(core), core->dev
rocket_job_timedout(): dev_err(core->dev, "NPU job timed out")
Unbinding the middle core of the three on an RK3588 while three clients are submitting to all of them faults twice, once from the surviving core's job queue and once from its reset work:
KASAN: null-ptr-deref in range [0x0000000000000220-0x0000000000000227] Workqueue: fdad0000.npu drm_sched_run_job_work [gpu_sched] pc : rocket_job_run+0x234/0x838 [rocket] Call trace: rocket_job_run+0x234/0x838 [rocket] drm_sched_run_job_work+0x2cc/0xad8 [gpu_sched] process_one_work+0x640/0x14f0 KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] Workqueue: rocket-reset-2 drm_sched_job_timedout [gpu_sched] pc : rocket_job_timedout+0xf0/0x1e0 [rocket] Call trace: rocket_job_timedout+0xf0/0x1e0 [rocket] drm_sched_job_timedout+0x188/0x6a0 [gpu_sched]
Both are the third core: the workqueue names are its device and its core->index, and it was left at slot 2 while num_cores had dropped to 2.
Search all the slots that were allocated, the way find_core_for_dev() now does. A core that is still bound is then found, and the two callers get the pointer they already assume they have.
This does not make unbinding one core out of several safe. An open client keeps an entity pointing at the scheduler of the core that went away:drm_sched reports it as not ready for every job that lands on it, and the client waits in dma_fence_default_wait for a fence that will never signal. Stopping the NULL dereference is what belongs in a fix; the rest wants more thought.
---
Unchanged from the standalone posting, which this series supersedes:
https://lore.kernel.org/r/[email protected]
drivers/accel/rocket/rocket_job.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/accel/rocket/rocket_job.c +1 -1
@@ -283,7 +283,7 @@ static struct rocket_core *sched_to_core(struct rocket_device *rdev, {unsigned int core; -for (core =0 ; core < rdev->num_cores; core++) { +for (core =0 ; core < rdev->max_cores; core++) {if (&rdev->cores[core].sched == sched)return &rdev->cores[core]; }
Signature · 1 line
2.43.0
Heiko Stübner
maintainerRe: [PATCH v2 03/11] accel/rocket: search every core slot when looking up a scheduler
Am Dienstag, 22. September 2026, 10:01:06 Mitteleuropäische Normalzeit schrieb Igor Paunovic:
[...]
Quoted 3 lines
Search all the slots that were allocated, the way
find_core_for_dev()now does. A core that is still bound is then found, and the two callers get the pointer they already assume they have.
[...]
Quoted 3 lines
---
Unchanged from the standalone posting, which this series supersedes:
https://lore.kernel.org/r/[email protected]
not to self: I had applied the standalone version for 7.3
Heiko
PATCH v2 4/11 accel/rocket: keep core slots stable across unbind and rebind
In reply to Igor Paunovicrocket_probe() inserts a new core at slot num_cores, and rocket_remove() only decrements that counter without clearing the slot. That falls apart as soon as one core is unbound while its siblings stay bound:
- the next bind reuses the slot of a still-live core and overwrites it while its IRQ handler (dev_id points into cores[]) and its DRM scheduler are still active;
- rocket_open() unconditionally uses cores[0].dev, which after an unbind of core 0 is a stale pointer to an unbound device.
On an RK3588 with three cores, unbinding the first one and binding it again puts it on top of the third:
rocket fdab0000.npu: drm_sched_init: scheduler already initialized!
One device now sits in two slots and the third core in none. The next unbind of the first core finds its stale slot, torn down already, and finishes the same scheduler a second time:
Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 pc : drm_sched_fini+0x4c/0x1e0 [gpu_sched] Call trace: drm_sched_fini+0x4c/0x1e0 [gpu_sched] (P) rocket_job_fini+0x28/0x60 [rocket] rocket_core_fini+0x4c/0x78 [rocket] rocket_remove+0x78/0x110 [rocket] platform_remove+0x2c/0x68 device_remove+0x58/0xc0 device_release_driver_internal+0x214/0x2e0 device_driver_detach+0x24/0x50 unbind_store+0xd8/0xe8
Make .dev the slot-liveness marker: probe takes the first free slot and clears it again if core init fails, remove clears .dev after rocket_core_fini() and warns if the core cannot be found, lookups skip empty slots, and rocket_open() and rocket_job_open() use only live slots. num_cores keeps counting bound cores for the last-core teardown check. A missing core is no reason to refuse a new file: the device is one core short, not gone, and any bound core will do for the IOMMU domain, which is attached to the group of whichever core runs a job. With a single core live the scheduler list that drm_sched_entity_init() does not keep is freed at once, as rocket_job_close() only frees what the entity kept.
---
v3, now in this series:
- rebased onto the three fixes before it: "accel/rocket: search
every core slot when a core is removed", which provides max_cores, "accel/rocket: number the cores by devicetree position, not bind order", so a slot no longer doubles as the hardware number, and "accel/rocket: search every core slot when looking up a scheduler"
- the crash above: this is what the devfreq patches ran into when tested on a kernel without this one
- free the scheduler list when a single core is live
- Cc: stable
v2: https://lore.kernel.org/r/[email protected]
- also clear the slot's .dev when rocket_core_init() fails (Jiaxing Hu)
- check .dev in sched_to_core() (Jiaxing Hu)
- document the synchronous-probe assumption at the slot scan
v1: https://lore.kernel.org/dri-devel/[email protected]/
Unbinding a core that still has jobs in flight, or that an open file already holds the scheduler of, has further pre-existing issues that are out of scope for this bookkeeping fix. One of them, a use-after-free under KASAN, is described in the cover letter. The driver does not serialize probe and remove against open; this does not change that. For stable, this goes with the three patches before it.
Verified on RK3588 (Orange Pi 5 Plus), 7.3.0-rc2 drm-misc-next plus this series, in-tree rocket, all three cores enabled: 25 rounds of unbinding and rebinding all three cores; 4 rounds of unbinding a single core (twice the devicetree-first core, twice the second one), each with an inference run while the core was absent and one more after all four; 5 rmmod/modprobe rounds; 3 unbind/rebind rounds and 1 rmmod with the clock raised to the 1 GHz OPP (CRU selector on the PVTPLL before each). No "scheduler already initialized" message and no oops; the regulator user count returns to its boot value after every round. Without this patch the same single-core round oopses in drm_sched_fini() on the second unbind, as shown above.
The single-core, full, rmmod and raised-clock rounds were repeated (10 full rounds and 3 rmmod rounds this time) on a KASAN and PROVE_LOCKING build of the same tree: no report, and lockdep still enabled afterwards.
drivers/accel/rocket/rocket_device.h | 2 + drivers/accel/rocket/rocket_drv.c | 55 +++++++++++++++++++++++----- drivers/accel/rocket/rocket_job.c | 38 ++++++++++++++----- 3 files changed, 77 insertions(+), 18 deletions(-)
drivers/accel/rocket/rocket_device.h +2 -0
@@ -18,7 +18,9 @@ struct rocket_device {struct mutex sched_lock;struct rocket_core *cores; +/* Number of currently bound cores. */ unsigned int num_cores; +/* Slot capacity (DT core count); slots with a NULL .dev are free. */ unsigned int max_cores; };
drivers/accel/rocket/rocket_drv.c +46 -9
98 lines not on this page · raw message
drivers/accel/rocket/rocket_job.c +29 -9
@@ -284,7 +284,7 @@ static struct rocket_core *sched_to_core(struct rocket_device *rdev,unsigned int core;for (core =0 ; core < rdev->max_cores; core++) { -if (&rdev->cores[core].sched == sched) +if (rdev->cores[core].dev && &rdev->cores[core].sched == sched)return &rdev->cores[core]; }
@@ -511,22 +511,42 @@ void rocket_job_fini(struct rocket_core *core)int rocket_job_open (struct rocket_file_priv *rocket_priv) {struct rocket_device *rdev = rocket_priv->rdev; -struct drm_gpu_scheduler **scheds =kmalloc_objs (*scheds, - rdev->num_cores); -unsigned int core; +struct drm_gpu_scheduler **scheds; +unsigned int core, n =0 ;int ret; -for (core =0 ; core < rdev->num_cores; core++) - scheds[core] = &rdev->cores[core].sched; + scheds =kmalloc_objs (*scheds, rdev->max_cores); +if (!scheds) +return -ENOMEM ; + +/* Only the cores that are bound right now have a scheduler to offer. */ +for (core =0 ; core < rdev->max_cores; core++) +if (rdev->cores[core].dev) + scheds[n++] = &rdev->cores[core].sched; + +if (!n) { + ret = -ENODEV ; + goto err_free; + } ret =drm_sched_entity_init (&rocket_priv->sched_entity,DRM_SCHED_PRIORITY_NORMAL , - scheds, - rdev->num_cores,NULL ); + scheds, n,NULL );if (WARN_ON (ret)) -return ret; + goto err_free; + +/* +* drm_sched_entity_init() keeps the list only when it holds more +* than one scheduler, and rocket_job_close() frees what it kept. +*/ +if (n <2 ) +kfree (scheds);return 0 ; + +err_free: +kfree (scheds); +return ret; }void rocket_job_close (struct rocket_file_priv *rocket_priv)
Signature · 1 line
2.43.0
PATCH v2 5/11 accel/rocket: request the core clocks by name
In reply to Igor Paunovicrocket_core_init() hands core->clks to devm_clk_bulk_get() without ever setting the .id members. The rocket_core array is allocated with devm_kcalloc() in rocket_device_init(), and rocket_probe() only fills in .rdev, .dev and .index, so all four clk_bulk_data entries are requested with a NULL con_id (unlike core->resets, whose ids are set a few lines above).
clk_get(dev, NULL) ends up in of_clk_get_hw(np, 0, NULL), and of_parse_clkspec() only consults "clock-names" when a name was passed, so the index stays 0 for all four entries. Every entry therefore ends up holding a handle to the *first* clock of the DT "clocks" property, i.e. ACLK_NPUn. Nothing fails: probe succeeds and the driver believes it owns four different clocks.
The consequence is that rocket_device_runtime_resume() prepares and enables the AXI clock four times, while hclk, pclk and - most importantly - the NPU compute clock ("npu", SCMI_CLK_NPU on RK3588) are never prepared or enabled by this driver at all. The NPU still works only because the Rockchip power-domain driver sets GENPD_FLAG_PM_CLK and its attach_dev() callback walks the device node with of_clk_get() and adds every clock to the pm_clk list, so genpd happens to keep the remaining clocks running. The bug is therefore latent today, but it means the driver holds no reference to the clock that actually feeds the NPU, which stands in the way of any future frequency scaling (OPP/devfreq) work.
Found on an Orange Pi 5 Plus (RK3588) by reading the live clock tree:/sys/kernel/debug/clk/clk_summary shows four "fdab0000.npu" consumer handles on aclk_npu0 (and likewise on aclk_npu1/aclk_npu2 for the other two cores), while hclk_npu0, pclk_npu_root and scmi_clk_npu have no "fdab0000.npu" consumer at all - their only consumers are the "npu@fdab0000" handles created by the power-domain driver via of_clk_get().
Set the ids explicitly, in the order mandated by the binding (Documentation/devicetree/bindings/npu/rockchip,rk3588-rknn-core.yaml):
aclk, hclk, npu, pclk. After the change the driver holds one handle per distinct clock and clk_bulk_prepare_enable() covers all four.
Note that this is a user-visible tightening for out-of-tree DTs: the old NULL-id requests resolved by index and succeeded no matter what "clock-names" contained, while the named requests fail probe with -ENOENT when one of the four names is missing. That is the right outcome for in-tree users - the binding requires exactly these four clock-names and rk3588-base.dtsi carries them on all three cores - but a DT that relied on the permissive lookup goes from silently running on the wrong clock handles to not probing at all, so record the change here where git log will find it.
---
Same diff and the same message text (his copy re-wraps the lines) as 01/14 of Jiaxing Hu's RK3576 series, which carries this patch as well; whichever lands first, the other drops it. The trailers differ: Jiaxing asked me to drop his Signed-off-by from my own posting of it, as he did not pass this copy along, and Assisted-by is added because an LLM helped with the checks on this v2.
drivers/accel/rocket/rocket_core.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/accel/rocket/rocket_core.c +4 -0
@@ -28,6 +28,10 @@ int rocket_core_init(struct rocket_core *core)if (err)return dev_err_probe (dev, err,"failed to get resets for core %d\n" , core->index); + core->clks[0 ].id ="aclk" ; + core->clks[1 ].id ="hclk" ; + core->clks[2 ].id ="npu" ; + core->clks[3 ].id ="pclk" ; err =devm_clk_bulk_get (dev,ARRAY_SIZE (core->clks), core->clks);if (err)return dev_err_probe (dev, err,"failed to get clocks for core %d\n" , core->index);
Signature · 1 line
2.43.0
Heiko Stübner
maintainerRe: [PATCH v2 05/11] accel/rocket: request the core clocks by name
Am Dienstag, 22. September 2026, 10:01:08 Mitteleuropäische Normalzeit schrieb Igor Paunovic:
Quoted 39 lines
rocket_core_init()handscore->clkstodevm_clk_bulk_get()without ever setting the .id members. Therocket_corearray is allocated withdevm_kcalloc()inrocket_device_init(), androcket_probe()only fills in .rdev, .dev and .index, so all fourclk_bulk_dataentries are requested with a NULLcon_id(unlikecore->resets, whose ids are set a few lines above).
clk_get(dev, NULL)ends up inof_clk_get_hw(np, 0, NULL), andof_parse_clkspec()only consults "clock-names" when a name was passed, so the index stays 0 for all four entries. Every entry therefore ends up holding a handle to the *first* clock of the DT "clocks" property, i.e.ACLK_NPUn. Nothing fails: probe succeeds and the driver believes it owns four different clocks.The consequence is that
rocket_device_runtime_resume()prepares and enables the AXI clock four times, while hclk, pclk and - most importantly - the NPU compute clock ("npu",SCMI_CLK_NPUon RK3588) are never prepared or enabled by this driver at all. The NPU still works only because the Rockchip power-domain driver setsGENPD_FLAG_PM_CLKand itsattach_dev()callback walks the device node withof_clk_get()and adds every clock to thepm_clklist, so genpd happens to keep the remaining clocks running. The bug is therefore latent today, but it means the driver holds no reference to the clock that actually feeds the NPU, which stands in the way of any future frequency scaling (OPP/devfreq) work.Found on an Orange Pi 5 Plus (RK3588) by reading the live clock tree:
/sys/kernel/debug/clk/clk_summaryshows four "fdab0000.npu" consumer handles onaclk_npu0(and likewise on aclk_npu1/aclk_npu2 for the other two cores), whilehclk_npu0,pclk_npu_rootandscmi_clk_npuhave no "fdab0000.npu" consumer at all - their only consumers are the "npu@fdab0000" handles created by the power-domain driver viaof_clk_get().Set the ids explicitly, in the order mandated by the binding (
Documentation/devicetree/bindings/npu/rockchip,rk3588-rknn-core.yaml):
aclk, hclk, npu, pclk. After the change the driver holds one handle per distinct clock andclk_bulk_prepare_enable()covers all four.
Quoted 9 lines
Note that this is a user-visible tightening for out-of-tree DTs: the old NULL-id requests resolved by index and succeeded no matter what "clock-names" contained, while the named requests fail probe with
-ENOENTwhen one of the four names is missing. That is the right outcome for in-tree users - the binding requires exactly these four clock-names andrk3588-base.dtsicarries them on all three cores - but a DT that relied on the permissive lookup goes from silently running on the wrong clock handles to not probing at all, so record the change here where git log will find it.
same comment as in the RK3576 series, please trim this down a lot. The change is not that big :-) .
Heiko
Heiko Stübner
maintainerRe: [PATCH v2 05/11] accel/rocket: request the core clocks by name
In reply to Igor PaunovicAm Dienstag, 22. September 2026, 10:01:08 Mitteleuropäische Normalzeit schrieb Igor Paunovic:
Quoted 72 lines
rocket_core_init()handscore->clkstodevm_clk_bulk_get()without ever setting the .id members. Therocket_corearray is allocated withdevm_kcalloc()inrocket_device_init(), androcket_probe()only fills in .rdev, .dev and .index, so all fourclk_bulk_dataentries are requested with a NULLcon_id(unlikecore->resets, whose ids are set a few lines above).
clk_get(dev, NULL)ends up inof_clk_get_hw(np, 0, NULL), andof_parse_clkspec()only consults "clock-names" when a name was passed, so the index stays 0 for all four entries. Every entry therefore ends up holding a handle to the *first* clock of the DT "clocks" property, i.e.ACLK_NPUn. Nothing fails: probe succeeds and the driver believes it owns four different clocks.The consequence is that
rocket_device_runtime_resume()prepares and enables the AXI clock four times, while hclk, pclk and - most importantly - the NPU compute clock ("npu",SCMI_CLK_NPUon RK3588) are never prepared or enabled by this driver at all. The NPU still works only because the Rockchip power-domain driver setsGENPD_FLAG_PM_CLKand itsattach_dev()callback walks the device node withof_clk_get()and adds every clock to thepm_clklist, so genpd happens to keep the remaining clocks running. The bug is therefore latent today, but it means the driver holds no reference to the clock that actually feeds the NPU, which stands in the way of any future frequency scaling (OPP/devfreq) work.Found on an Orange Pi 5 Plus (RK3588) by reading the live clock tree:
/sys/kernel/debug/clk/clk_summaryshows four "fdab0000.npu" consumer handles onaclk_npu0(and likewise on aclk_npu1/aclk_npu2 for the other two cores), whilehclk_npu0,pclk_npu_rootandscmi_clk_npuhave no "fdab0000.npu" consumer at all - their only consumers are the "npu@fdab0000" handles created by the power-domain driver viaof_clk_get().Set the ids explicitly, in the order mandated by the binding (
Documentation/devicetree/bindings/npu/rockchip,rk3588-rknn-core.yaml):
aclk, hclk, npu, pclk. After the change the driver holds one handle per distinct clock andclk_bulk_prepare_enable()covers all four.Note that this is a user-visible tightening for out-of-tree DTs: the old NULL-id requests resolved by index and succeeded no matter what "clock-names" contained, while the named requests fail probe with
-ENOENTwhen one of the four names is missing. That is the right outcome for in-tree users - the binding requires exactly these four clock-names andrk3588-base.dtsicarries them on all three cores - but a DT that relied on the permissive lookup goes from silently running on the wrong clock handles to not probing at all, so record the change here where git log will find it.Fixes ed98261b4168 ("accel/rocket: Add a new driver for Rockchip's NPU")Assisted-by LLM sparse checkpatchSigned-off-by Igor Paunovic <[email protected]>Tested-by Sidong Yang <[email protected]>Reviewed-by Sebastian Reichel <[email protected]>Reviewed-by Jiaxing Hu <[email protected]>---
Same diff and the same message text (his copy re-wraps the lines) as 01/14 of Jiaxing Hu's RK3576 series, which carries this patch as well; whichever lands first, the other drops it. The trailers differ: Jiaxing asked me to drop his Signed-off-by from my own posting of it, as he did not pass this copy along, and Assisted-by is added because an LLM helped with the checks on this v2.drivers/accel/rocket/rocket_core.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/accel/rocket/rocket_core.c +0
-0
drivers/accel/rocket/rocket_core.c int rocket_core_init(struct rocket_core *core) · line 34
Quoted 7 lines
drivers/accel/rocket/rocket_core.c +4
-0@@ -28,6 +28,10 @@ int rocket_core_init(struct rocket_core *core)if (err)return dev_err_probe (dev, err,"failed to get resets for core %d\n" , core->index); + core->clks[0 ].id ="aclk" ; + core->clks[1 ].id ="hclk" ; + core->clks[2 ].id ="npu" ; + core->clks[3 ].id ="pclk" ;
additionally, while reading the followup patches, please introduce some constants for the indices. ROCKET_CLK_ACLK, ... or some other name. Because when you access them later by index, that makes things harder to read and follow (especially in the future)
Heiko
PATCH v2 6/11 dt-bindings: npu: rockchip: allow DVFS and thermal properties
In reply to Igor PaunovicThe three NPU cores on the RK3588 are fed by a single clock and a single supply, and the firmware accepts a fixed set of rates for that clock. Describing those rates as an operating-points-v2 table is what lets a driver scale the NPU instead of leaving it at whatever rate the bootloader set, so allow the property on the core node.
Throttling the NPU from a thermal zone needs a core node to be usable as a cooling device, so allow #cooling-cells too.
The OPP table belongs on every core, with opp-shared: the cores have no clock of their own, and one shared table for one shared clock is the same shape a CPU cluster uses. #cooling-cells goes on one core only, the one a thermal zone's cooling map names, because the cores cannot be throttled independently. Naming one representative node for a shared frequency domain is the established shape, as in "Cpufreq cooling device on CPU0" in Documentation/devicetree/bindings/thermal/thermal-cooling-devices.yaml.
The schema cannot enforce which core carries #cooling-cells, because all three cores share a compatible string and a node name pattern, so that stays a devicetree convention. That is the same situation as for CPU cooling, where cpus.yaml does not restrict #cooling-cells to cpu@0 either.
The example gains #cooling-cells; the operating-points-v2 property is exercised by the RK3588 devicetree later in this series.
---
v2: the text on where the properties go is rewritten for opp-shared on all three cores, following Nicolas Dufresne's review of v1 3/7. The only change to the schema file is the wording of the #cooling-cells description; the constraints and the example are unchanged. Conor, your Ack is kept on that basis; please say if it no longer holds.
.../bindings/npu/rockchip,rk3588-rknn-core.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+)
Documentation/devicetree/bindings/npu/rockchip,rk3588-rknn-core.yaml +10 -0
@@ -42,6 +42,13 @@ properties: - const: npu - const: pclk + "#cooling-cells": + description: + Present on one core only, the first, which stands for the shared NPU + clock as a cooling device. The other cores have no clock of their own + and cannot be throttled independently of it. + const: 2 + interrupts: maxItems: 1
@@ -50,6 +57,8 @@ properties: npu-supply: true + operating-points-v2: true + power-domains: maxItems: 1
@@ -100,6 +109,7 @@ examples: clocks = <&cru ACLK_NPU0>, <&cru HCLK_NPU0>, <&scmi_clk SCMI_CLK_NPU>, <&cru PCLK_NPU_ROOT>; clock-names = "aclk", "hclk", "npu", "pclk"; + #cooling-cells = <2>; interrupts = <GIC_SPI 110 IRQ_TYPE_LEVEL_HIGH 0>; iommus = <&rknn_mmu_0>; npu-supply = <&vdd_npu_s0>;
Signature · 1 line
2.43.0
Rob Herring
maintainerRe: [PATCH v2 06/11] dt-bindings: npu: rockchip: allow DVFS and thermal properties
Quoted 23 lines · Igor Paunovic
The three NPU cores on the RK3588 are fed by a single clock and a single supply, and the firmware accepts a fixed set of rates for that clock. Describing those rates as an operating-points-v2 table is what lets a driver scale the NPU instead of leaving it at whatever rate the bootloader set, so allow the property on the core node.
Throttling the NPU from a thermal zone needs a core node to be usable as a cooling device, so allow #cooling-cells too.
The OPP table belongs on every core, with opp-shared: the cores have no clock of their own, and one shared table for one shared clock is the same shape a CPU cluster uses. #cooling-cells goes on one core only, the one a thermal zone's cooling map names, because the cores cannot be throttled independently. Naming one representative node for a shared frequency domain is the established shape, as in "Cpufreq cooling device on CPU0" in
Documentation/devicetree/bindings/thermal/thermal-cooling-devices.yaml.The schema cannot enforce which core carries #cooling-cells, because all three cores share a compatible string and a node name pattern, so that stays a devicetree convention. That is the same situation as for CPU cooling, where
cpus.yamldoes not restrict #cooling-cells to cpu@0 either.
Quoted 4 lines
The example gains #cooling-cells; the operating-points-v2 property is exercised by the RK3588 devicetree later in this series.
Assisted-by LLM checkpatch dt_binding_check
Drop 'dt_binding_check'. That's expected/assumed.
Quoted 2 lines
Signed-off-by Igor Paunovic <[email protected]>Acked-by Conor Dooley <[email protected]>
Re: [PATCH v2 06/11] dt-bindings: npu: rockchip: allow DVFS and thermal properties
Quoted 4 lines · Rob Herring
Quoted 1 line · Igor Paunovic
Assisted-by LLM checkpatch dt_binding_checkDrop '
dt_binding_check'. That's expected/assumed.
Will do in v3. By the same reasoning I will drop 'dtbs_check' from the two DTS patches, 07/11 and 11/11.
Igor
Re: [PATCH v2 06/11] dt-bindings: npu: rockchip: allow DVFS and thermal properties
Quoted 8 lines · Igor Paunovic
Quoted 4 lines · Rob Herring
Quoted 1 line · Igor Paunovic
Assisted-by LLM checkpatch dt_binding_checkDrop '
dt_binding_check'. That's expected/assumed.Will do in v3. By the same reasoning I will drop '
dtbs_check' from the two DTS patches, 07/11 and 11/11.
Doesn't the same apply to 'checkpatch' as well?
Cheers,
Diederik
Re: [PATCH v2 06/11] dt-bindings: npu: rockchip: allow DVFS and thermal properties
Quoted 5 lines · Diederik de Haas
Quoted 2 lines · Igor Paunovic
Will do in v3. By the same reasoning I will drop '
dtbs_check' from the two DTS patches, 07/11 and 11/11.Doesn't the same apply to 'checkpatch' as well?
Yes, it does, so it goes too. In v3 the trailer will only name sparse, on the patches where it was used, and the DT patches will carry a bare 'Assisted-by: LLM'.
Igor
PATCH v2 7/11 arm64: dts: rockchip: rk3588: add an OPP table for the NPU
In reply to Igor PaunovicThe NPU compute clock is driven by the firmware, which only accepts one of the rates in its own PVTPLL table: 300, 400, 500, 600, 700, 800, 900 and 1000 MHz through the PVTPLL, plus 200 MHz off GPLL. Anything else comes back as SCMI_INVALID_PARAMETERS, and that refusal never reaches the caller:
the clock framework does not look at what the clock's set_rate returns, so clk_set_rate() reports success and the clock stays where it was. The table therefore has to name those rates exactly rather than describe a range.
200 MHz is included even though the vendor table stops at 300, because mainline pins the cores there with assigned-clock-rates, the firmware's table names 200 MHz exactly, on its GPLL path, and that is the rate the NPU boots and idles at. Its voltage is the same 700 mV the vendor uses for 300 MHz, so it is conservative.
The voltages are the vendor's, and the upper half of the table matches the GPU table in this file step for step: 700 MHz at 700 mV, 800 at 750, 900 at 800, 1000 at 850. There is no PVTM or binning here, for the same reason the GPU table has none: mainline uses conservative worst-case voltages instead of per-chip nvmem data.
The table is marked opp-shared and referenced from all three cores. They have one clock and one supply between them and cannot be scaled independently, and that is what opp-shared describes: one table for one clock, the way a CPU cluster shares its table.
The full SoC range is described rather than a per-board subset, so that a board which cannot cool the upper rates drops them in its own .dts with a /delete-node/ on the OPP it does not want. A board may only delete OPPs that way, never invent intermediate ones: a rate that is not in the firmware's table is refused by the firmware, but the kernel never learns of it, so an invented OPP would be refused while the kernel went on reporting it as set.
rk3588j.dtsi does not include this file; it carries its own derated tables for the CPU clusters and the GPU, and it gets no NPU table here. That is deliberate. The J part is rated lower than the rates in this table and none of it can be measured on the hardware this was written on, so inventing a derated NPU table would be guessing. Its NPU node stays disabled, so nothing binds and the cooling map added later in this series is simply never resolved.
The same rates and voltages were arrived at independently by Nicolas Dufresne in a proof of concept that was never posted to the list; his version differs in that it marks 200 MHz as opp-suspend and drops the assigned-clock-rates pins.
---
v2:
- opp-shared, and the table referenced from all three cores (Nicolas).
- The opp-suspend paragraph is gone. In the v1 thread I said v2 would argue that the driver already puts the device back at its boot rate; that is again driver behaviour used as a devicetree argument, which is what Nicolas objected to, so I am not making it. Whether opp-suspend at 200 MHz describes the hardware is a question for the DT maintainers, in the cover letter.
- "give a driver nowhere to return to" is gone for the same reason.
- The paragraph on the table being inert until the driver patch is gone (Nicolas).
- New: the firmware's refusal of a rate is not reported back through the clock framework. Found by reading clk_change_rate() in drivers/clk/clk.c after a test that requested a rate outside the table.
arch/arm64/boot/dts/rockchip/rk3588-opp.dtsi | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+)
arch/arm64/boot/dts/rockchip/rk3588-opp.dtsi +54 -0
@@ -151,6 +151,48 @@ opp-1000000000 { opp-microvolt = <850000 850000 850000>; }; }; + + npu_opp_table: opp-table-npu { + compatible = "operating-points-v2"; + opp-shared; + + opp-200000000 { + opp-hz = /bits/ 64 <200000000>; + opp-microvolt = <700000 700000 850000>; + }; + opp-300000000 { + opp-hz = /bits/ 64 <300000000>; + opp-microvolt = <700000 700000 850000>; + }; + opp-400000000 { + opp-hz = /bits/ 64 <400000000>; + opp-microvolt = <700000 700000 850000>; + }; + opp-500000000 { + opp-hz = /bits/ 64 <500000000>; + opp-microvolt = <700000 700000 850000>; + }; + opp-600000000 { + opp-hz = /bits/ 64 <600000000>; + opp-microvolt = <700000 700000 850000>; + }; + opp-700000000 { + opp-hz = /bits/ 64 <700000000>; + opp-microvolt = <700000 700000 850000>; + }; + opp-800000000 { + opp-hz = /bits/ 64 <800000000>; + opp-microvolt = <750000 750000 850000>; + }; + opp-900000000 { + opp-hz = /bits/ 64 <900000000>; + opp-microvolt = <800000 800000 850000>; + }; + opp-1000000000 { + opp-hz = /bits/ 64 <1000000000>; + opp-microvolt = <850000 850000 850000>; + }; + }; }; &cpu_b0 {
@@ -188,3 +230,15 @@ &cpu_l3 { &gpu { operating-points-v2 = <&gpu_opp_table>; }; + +&rknn_core_0 { + operating-points-v2 = <&npu_opp_table>; +}; + +&rknn_core_1 { + operating-points-v2 = <&npu_opp_table>; +}; + +&rknn_core_2 { + operating-points-v2 = <&npu_opp_table>; +};
Signature · 1 line
2.43.0
PATCH v2 8/11 accel/rocket: restore the NPU clock boot rate before powering the cores down
In reply to Igor PaunovicThe compute clock is generated by a PVTPLL that lives inside the NPU power island. Powering an island up while that clock is above the rate the bootloader left it at does not work: the domain never acks the power-on, and the first register access into it afterwards takes an asynchronous SError. So the rate has to be back down before the last core goes away.
Nothing in the driver raises the clock today, which makes this a no-op on its own, but it is the guard that has to be in the tree before anything does, and the next patches do. The .shutdown hook is the same guard for the
handover: once devfreq is driving the clock, a kexec would otherwise pass the raised rate to the next kernel, which powers the islands up before it looks at it. What this cannot do is rescue a rate it did not set - the rate read at probe is taken as the boot rate whatever it is.
The rate is read at probe rather than hardcoded. Mainline pins the RK3588 cores at 200 MHz with assigned-clock-rates, but that is a devicetree property, not a property of the hardware, and a SoC whose devicetree does not set it would be left running at a rate this driver had invented.
All three cores share the clock, so only the last core to suspend may lower it; the others just drop the count. Lowering it is safe with the islands already down as long as the boot rate is one the firmware serves from GPLL, which on the RK3588 is the 200 MHz the devicetree pins: for that rate the firmware writes only CRU clock selectors, never a register inside the NPU.
---
v2: v1 of this patch kept a struct clk handle in struct rocket_device, taken from the devres of the first core to probe, and used it from the runtime suspend of whichever core went down last. Unbinding the cores freed the handle underneath it: KASAN reported a slab-use-after-free in clk_set_rate() during a ten-round unbind/rebind test on this board after v1 was posted. No handle is kept any more; the callback uses the handle of the core it runs for, which is bound for as long as the call lasts. "A reboot" is dropped from the kexec sentence and the comment: whether the clock selectors survive the global reset the firmware does on reboot has not been checked. The argument that lowering the rate is safe is now limited to a boot rate the firmware serves from GPLL, which on the RK3588 is the 200 MHz the devicetree pins.
drivers/accel/rocket/rocket_core.c | 10 ++++++ drivers/accel/rocket/rocket_device.h | 15 +++++++++ drivers/accel/rocket/rocket_drv.c | 47 ++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+)
drivers/accel/rocket/rocket_core.c +10 -0
@@ -12,6 +12,7 @@#include <linux/reset.h> #include "rocket_core.h" +#include "rocket_device.h" #include "rocket_job.h" int rocket_core_init (struct rocket_core *core)
@@ -36,6 +37,15 @@ int rocket_core_init(struct rocket_core *core)if (err)return dev_err_probe (dev, err,"failed to get clocks for core %d\n" , core->index); +/* +* Record what the compute clock was running at before anything here +* touched it, on the first core to probe. Reading it rather than +* hardcoding a rate keeps this working on a SoC whose devicetree does +* not pin the clock with assigned-clock-rates. +*/ +if (!core->rdev->npu_boot_rate) + core->rdev->npu_boot_rate =clk_get_rate (core->clks[2 ].clk); + core->pc_iomem =devm_platform_ioremap_resource_byname (pdev,"pc" );if (IS_ERR (core->pc_iomem)) {dev_err (dev,"couldn't find PC registers %ld\n" ,PTR_ERR (core->pc_iomem));
drivers/accel/rocket/rocket_device.h +15 -0
@@ -22,6 +22,21 @@ struct rocket_device {unsigned int num_cores;/* Slot capacity (DT core count); slots with a NULL .dev are free. */ unsigned int max_cores; + +/* +* The cores have no clock of their own: one clock feeds all of them, +* so any core's handle refers to the same thing. No handle is kept +* here: each one belongs to the devres of the core that asked for it +* and dies with that core's unbind, while this structure outlives any +* single core. Whoever needs the clock uses the handle of the core it +* was called for, which is bound for as long as the call lasts. +* +* npu_boot_rate is the rate the clock was left at before the driver +* touched it, and active_cores counts the cores that are runtime +* resumed right now. +*/ +unsigned long npu_boot_rate; +atomic_t active_cores; }; struct rocket_device *rocket_device_init(struct platform_device *pdev,
drivers/accel/rocket/rocket_drv.c +47 -0
74 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 08/11] accel/rocket: restore the NPU clock boot rate before powering the cores down
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] kexec: missing hardware quiesce and DMA halt in rocket_shutdown --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kexec: missing hardware quiesce and DMA halt in rocket_shutdown
--
commit a41629401e441e7353babe8449ad03dc39082ce0
Author: Igor Paunovic <[email protected]>
accel/rocket: restore the NPU clock boot rate before powering the cores down
This commit records the initial NPU clock rate at probe and restores it during runtime suspend when the last core goes away. It also introduces a shutdown hook to restore the rate before system reboot or kexec, ensuring the hardware does not fault when the domain is subsequently powered back on.
Quoted 3 lines
drivers/accel/rocket/rocket_drv.c +0
-0
[ ... ]
Quoted 5 lines
drivers/accel/rocket/rocket_drv.c +5
-0+/* +* A kexec hands the next kernel whatever rate is set here, and that kernel +* will power the islands up before it looks at the clock. +*/ +static void rocket_shutdown (struct platform_device *pdev)
drivers/accel/rocket/rocket_drv.c EXPORT_GPL_DEV_PM_OPS(rocket_pm_ops) = { · line 382
Quoted 10 lines
drivers/accel/rocket/rocket_drv.c +10
-0+{ +struct rocket_device *rdev =dev_get_drvdata (&pdev->dev); +int core; + +if (!rdev) +return ; + + core =find_core_for_dev (&pdev->dev); +if (core >=0 ) +rocket_npu_restore_boot_rate (&rdev->cores[core]);
[Severity: High]
Does this code leave the hardware executing active jobs and performing DMA?
If the system reboots or executes a kexec while the NPU is actively executing a job, lowering the clock rate here does not halt the hardware or stop active jobs. Because this is a platform device, there is no automatic bus-master clearing.
Can this lead to the NPU continuing to perform asynchronous DMA into memory while the new kernel boots, potentially causing memory corruption or fatal bus faults?
Should the driver explicitly halt jobs, mask DMA, and reset the core (for example, via rocket_core_reset) before returning from this shutdown hook?
Quoted 1 line
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Re: [PATCH v2 08/11] accel/rocket: restore the NPU clock boot rate before powering the cores down
On the shutdown question from the Sashiko review of this patch:
The gap is real, but it predates this patch. The driver in drm-misc-next has no .shutdown callback at all, so a reboot or kexec with a job in flight already leaves the cores running today. The callback added here takes the devfreq device down and puts the clock back to its boot rate; it does not stop, drain or reset the cores.
Quiescing the cores on shutdown (stopping the schedulers and resetting the cores through rocket_core_reset()) is part of the same teardown work as the use-after-free under "Not done" in the cover letter, where a core is unbound while clients submit: both need the driver to stop its schedulers and cores while files are still open. I would rather fix both in one follow-up, once Tomeu has said which way he wants the teardown to go.
Igor
Heiko Stübner
maintainerRe: [PATCH v2 08/11] accel/rocket: restore the NPU clock boot rate before powering the cores down
In reply to Igor PaunovicAm Dienstag, 22. September 2026, 10:01:11 Mitteleuropäische Normalzeit schrieb Igor Paunovic:
Quoted 59 lines
The compute clock is generated by a PVTPLL that lives inside the NPU power island. Powering an island up while that clock is above the rate the bootloader left it at does not work: the domain never acks the power-on, and the first register access into it afterwards takes an asynchronous SError. So the rate has to be back down before the last core goes away.
Nothing in the driver raises the clock today, which makes this a no-op on its own, but it is the guard that has to be in the tree before anything does, and the next patches do. The .shutdown hook is the same guard for the
handover: once devfreq is driving the clock, a kexec would otherwise pass the raised rate to the next kernel, which powers the islands up before it looks at it. What this cannot do is rescue a rate it did not set - the rate read at probe is taken as the boot rate whatever it is.The rate is read at probe rather than hardcoded. Mainline pins the RK3588 cores at 200 MHz with assigned-clock-rates, but that is a devicetree property, not a property of the hardware, and a SoC whose devicetree does not set it would be left running at a rate this driver had invented.
All three cores share the clock, so only the last core to suspend may lower it; the others just drop the count. Lowering it is safe with the islands already down as long as the boot rate is one the firmware serves from GPLL, which on the RK3588 is the 200 MHz the devicetree pins: for that rate the firmware writes only CRU clock selectors, never a register inside the NPU.
Assisted-by LLM sparse checkpatchSigned-off-by Igor Paunovic <[email protected]>---
v2: v1 of this patch kept a struct clk handle in structrocket_device, taken from the devres of the first core to probe, and used it from the runtime suspend of whichever core went down last. Unbinding the cores freed the handle underneath it: KASAN reported a slab-use-after-free inclk_set_rate()during a ten-round unbind/rebind test on this board after v1 was posted. No handle is kept any more; the callback uses the handle of the core it runs for, which is bound for as long as the call lasts. "A reboot" is dropped from the kexec sentence and the comment: whether the clock selectors survive the global reset the firmware does on reboot has not been checked. The argument that lowering the rate is safe is now limited to a boot rate the firmware serves from GPLL, which on the RK3588 is the 200 MHz the devicetree pins.drivers/accel/rocket/rocket_core.c | 10 ++++++ drivers/accel/rocket/rocket_device.h | 15 +++++++++ drivers/accel/rocket/rocket_drv.c | 47 ++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+)drivers/accel/rocket/rocket_core.c +1
-0@@ -12,6 +12,7 @@#include <linux/reset.h> #include "rocket_core.h" +#include "rocket_device.h" #include "rocket_job.h" int rocket_core_init (struct rocket_core *core)
drivers/accel/rocket/rocket_core.c int rocket_core_init(struct rocket_core *core) · line 45
Quoted 9 lines
drivers/accel/rocket/rocket_core.c +6
-0@@ -36,6 +37,15 @@ int rocket_core_init(struct rocket_core *core)if (err)return dev_err_probe (dev, err,"failed to get clocks for core %d\n" , core->index); +/* +* Record what the compute clock was running at before anything here +* touched it, on the first core to probe. Reading it rather than +* hardcoding a rate keeps this working on a SoC whose devicetree does +* not pin the clock with assigned-clock-rates. +*/
shorter comment please
drivers/accel/rocket/rocket_core.c int rocket_core_init(struct rocket_core *core) · line 47
Quoted 2 lines
drivers/accel/rocket/rocket_core.c +2
-0+if (!core->rdev->npu_boot_rate) + core->rdev->npu_boot_rate =clk_get_rate (core->clks[2 ].clk);
see comment about using a constant for the array index in patch 5.
Quoted 15 lines
drivers/accel/rocket/rocket_core.c +1
-0+ core->pc_iomem =devm_platform_ioremap_resource_byname (pdev,"pc" );if (IS_ERR (core->pc_iomem)) {dev_err (dev,"couldn't find PC registers %ld\n" ,PTR_ERR (core->pc_iomem));drivers/accel/rocket/rocket_device.h +3
-0@@ -22,6 +22,21 @@ struct rocket_device {unsigned int num_cores;/* Slot capacity (DT core count); slots with a NULL .dev are free. */ unsigned int max_cores; + +/* +* The cores have no clock of their own: one clock feeds all of them,
drivers/accel/rocket/rocket_device.h struct rocket_device { · line 37
Quoted 10 lines
drivers/accel/rocket/rocket_device.h +10
-0+* so any core's handle refers to the same thing. No handle is kept +* here: each one belongs to the devres of the core that asked for it +* and dies with that core's unbind, while this structure outlives any +* single core. Whoever needs the clock uses the handle of the core it +* was called for, which is bound for as long as the call lasts. +* +* npu_boot_rate is the rate the clock was left at before the driver +* touched it, and active_cores counts the cores that are runtime +* resumed right now. +*/
shorter comment please
Quoted 11 lines
drivers/accel/rocket/rocket_device.h +2
-0+unsigned long npu_boot_rate; +atomic_t active_cores; }; struct rocket_device *rocket_device_init(struct platform_device *pdev,drivers/accel/rocket/rocket_drv.c +0
-0@@ -297,6 +297,30 @@ static int find_core_for_dev(struct device *dev)return -1 ;
drivers/accel/rocket/rocket_drv.c static int find_core_for_dev(struct device *dev) · line 307
Quoted 10 lines
drivers/accel/rocket/rocket_drv.c +8
-0} +/* +* Put the compute clock back where the bootloader had it. The cores share +* this clock, so this is only correct once none of them is running any more. +* +* Lowering the rate is safe with the power islands down as long as the boot +* rate is one the firmware serves from GPLL, which on the RK3588 is the +* 200 MHz the devicetree pins: for that rate the firmware touches only the +* CRU clock selectors, none of the NPU's own registers.
shorter comments please.
Thanks
Heiko
PATCH v2 9/11 accel/rocket: add devfreq support
In reply to Igor PaunovicThe NPU has run at whatever rate the devicetree pinned it to since the driver was merged, which on the RK3588 is 200 MHz. The hardware reaches 1 GHz, and the firmware will change the rate on request, so let devfreq drive it from how busy the cores actually are.
One devfreq device drives all of the cores, because they have one clock and one supply between them and cannot be scaled apart. It hangs off the first core in devicetree order that carries an OPP table, which on the RK3588, where all three cores reference the shared table, is rknn_core_0. The choice has to be fixed rather than "whichever core bound last": the devfreq device is named after that core, and a cooling map in the devicetree resolves against that core's node. core->index is the core's position among the core nodes, so the lowest index is the first node.
The awkward part is that the clock is generated by a PVTPLL that sits inside the NPU power islands. An island powered up while the clock is above the rate the bootloader left never acknowledges the power-on, and the first register access into it afterwards takes an asynchronous SError. So before the rate goes up, every core is runtime resumed, and the references are held for as long as the clock stays raised: no island can transition while they are held, which is what makes a raised clock safe rather than merely unlikely to be caught out. The guard and the thing it guards arrive in the same commit, so no commit in the tree ever raises the rate without it.
Unbinding any core takes the devfreq device down, and it comes back once every core is bound again: a rate change resumes all of them, so the device has to be whole for the guard to mean anything. Every walk of the core array here relies on that.
The cost is that a boosted NPU does not power-gate individual cores. It is paid only above the boot rate; at the boot rate the references are dropped and runtime PM behaves as before. What that costs in milliwatts has not been measured on this board. A measurement, or a design that does not need the references at all, would both be welcome.
Utilisation is aggregated as the maximum over the cores, not the sum: the rate has to satisfy the busiest core, and summing would report one saturated core out of three as a third of the load. Whether that is the right aggregation for a shared clock is a fair question for review.
Unlike panfrost, panthor, lima and msm, the driver does not call devfreq_suspend_device() from runtime suspend. That call ends in cancel_delayed_work_sync() on the governor's worker, and the worker is what calls ->target(), which resumes every core: a runtime-suspend callback would wait for a worker that is waiting for that same callback. Instead ->target() returns early when the rate is unchanged, so a governor tick on an idle NPU costs one comparison and resumes nothing.
System suspend is different: dpm_suspend() runs devfreq_suspend() over every registered devfreq before it walks the devices, from a path that holds none of this driver's locks. What the driver adds is lowering the rate and dropping the references from every core's ->suspend, not only the owning core's. pm_runtime_force_suspend() powers a core down whatever the usage count says, and the owning core need not be the first one suspended, so a suspend that aborts partway would otherwise leave the clock raised over islands that are already gated.
The clock and the supply are claimed in one dev_pm_opp_set_config() call before the table is added. Naming the clock there is not optional: with no name the OPP core takes the first clock in the node, which is the bus clock. Configuration and table live on the owning core, which need not be the device being probed at the time, so none of it can be devres: the call hands back a token, and rocket_devfreq_fini() releases it, and the table, by hand. Left to devres, a later bind would fail on an OPP table the previous teardown never emptied.
The rate is changed through dev_pm_opp_set_rate(), so that the supply moves with it. Nothing may set the rate behind the OPP core's back: it caches the OPP it last applied and skips a repeat request for it, so a raw clk_set_rate() would turn the next request for a raised rate into a silent no-op. The boot-rate restore added in the previous patch is converted accordingly.
The rate the previous patch reads at probe is the firmware's own number and need not be in the OPP table: a board that does not pin the clock with assigned-clock-rates gets whatever the firmware's divider produces. So the boot rate is normalised to the table once, at init, to the lowest OPP that is not below it, and everything here compares against and returns to that OPP. Comparing cur_freq against a rate that is not in the table would leave the cores held after the first change, with no governor tick ever able to match it.
->get_cur_freq() and the status callback report the rate this driver last requested rather than asking the clock: devfreq queries them from sysfs with no runtime PM reference of its own, and the answer has to be a rate from the OPP table or devfreq_get_freq_level() does not find it and devfreq warns on every transition. "Requested" is the accurate word: a rate the firmware refuses does not come back as an error, because the clock framework ignores what the clock's set_rate returns. Keeping every request to the OPP table, which names only the rates the firmware accepts, keeps a request from being refused; what sysfs shows is still the request, not a measurement.
A devicetree with no OPP table is not an error: the driver returns without a devfreq device and the NPU keeps its boot rate, as before this patch.
The governor thresholds are a starting point taken from the other accelerators in tree, not a measurement. Inference workloads have not been profiled against them.
---
v2:
- The devfreq device goes on the first core in devicetree order with an OPP table, not on whichever core with the table bound first, as v1 would have done with the table on all three cores (Nicolas).
- The boot rate is normalised to the OPP table at init. Found by reading the code: without assigned-clock-rates the raw rate is not in the table and the cores would have stayed held after the first change. Code read only so far; a devicetree without the property has not been booted.
- Init holds every core while it programs the initial OPP, since rounding the boot rate up to the table may raise the clock.
- "last programmed" is now "last requested", with the reason.
- Comments and text updated for the shared table; a comment that counted ten governor ticks a second now counts twenty (50 ms polling).
- The commit message now says that unbinding any core takes the devfreq device down until all are bound again; v1 did the same without saying so.
- The utilisation counters of a core that was unbound and bound again start from zero.
- A comment over hold_all() states the invariant the loops rely on.
drivers/accel/rocket/Kconfig | 2 + drivers/accel/rocket/Makefile | 1 + drivers/accel/rocket/rocket_core.h | 11 + drivers/accel/rocket/rocket_devfreq.c | 500 ++++++++++++++++++++++++++ drivers/accel/rocket/rocket_devfreq.h | 65 ++++ drivers/accel/rocket/rocket_device.h | 3 + drivers/accel/rocket/rocket_drv.c | 59 ++- drivers/accel/rocket/rocket_job.c | 7 + 8 files changed, 646 insertions(+), 2 deletions(-) create mode 100644 drivers/accel/rocket/rocket_devfreq.c create mode 100644 drivers/accel/rocket/rocket_devfreq.h
drivers/accel/rocket/Kconfig +2 -0
@@ -8,6 +8,8 @@ config DRM_ACCEL_ROCKET depends on MMU select DRM_SCHED select DRM_GEM_SHMEM_HELPER + select PM_DEVFREQ + select DEVFREQ_GOV_SIMPLE_ONDEMAND help Choose this option if you have a Rockchip SoC that contains a compatible Neural Processing Unit (NPU), such as the RK3588. Called by
drivers/accel/rocket/Makefile +1 -0
@@ -5,6 +5,7 @@ obj-$(CONFIG_DRM_ACCEL_ROCKET) := rocket.o rocket-y := \ rocket_core.o \ rocket_device.o \ + rocket_devfreq.o \ rocket_drv.o \ rocket_gem.o \ rocket_job.o
drivers/accel/rocket/rocket_core.h +11 -0
@@ -7,6 +7,7 @@#include <drm/gpu_scheduler.h> #include <linux/clk.h> #include <linux/io.h> +#include <linux/ktime.h> #include <linux/mutex_types.h> #include <linux/reset.h>
@@ -60,6 +61,16 @@ struct rocket_core {struct drm_gpu_scheduler sched;u64 fence_context;u64 emit_seqno; + +/* +* Utilisation seen by devfreq, guarded by rdev->devfreq.busy_lock. A +* core runs one task at a time, so a flag is exact here and, unlike a +* counter, cannot be left skewed by a job the reset path tore down. +*/ +bool busy; +ktime_t busy_time; +ktime_t idle_time; +ktime_t time_last_update; };int rocket_core_init (struct rocket_core *core);
drivers/accel/rocket/rocket_devfreq.c new file +500 -0
500 lines not on this page · raw message
drivers/accel/rocket/rocket_devfreq.h new file +65 -0
65 lines not on this page · raw message
drivers/accel/rocket/rocket_device.h +3 -0
@@ -11,6 +11,7 @@#include <linux/platform_device.h> #include "rocket_core.h" +#include "rocket_devfreq.h" struct rocket_device {struct drm_device ddev;
@@ -37,6 +38,8 @@ struct rocket_device {*/ unsigned long npu_boot_rate;atomic_t active_cores; + +struct rocket_devfreq devfreq; }; struct rocket_device *rocket_device_init(struct platform_device *pdev,
drivers/accel/rocket/rocket_drv.c +57 -2
102 lines not on this page · raw message
drivers/accel/rocket/rocket_job.c +7 -0
@@ -15,6 +15,7 @@#include "rocket_core.h" #include "rocket_device.h" +#include "rocket_devfreq.h" #include "rocket_drv.h" #include "rocket_job.h" #include "rocket_registers.h"
@@ -149,6 +150,8 @@ static void rocket_job_hw_submit(struct rocket_core *core, struct rocket_job *jorocket_pc_writel (core,TASK_DMA_BASE_ADDR ,PC_TASK_DMA_BASE_ADDR_DMA_BASE_ADDR (0x0 )); +rocket_devfreq_record_busy (core); +rocket_pc_writel (core,OPERATION_ENABLE ,PC_OPERATION_ENABLE_OP_EN (1 ));dev_dbg (core->dev,"Submitted regcmd at 0x%llx to core %d" , task->regcmd, core->index);
@@ -348,6 +351,8 @@ static void rocket_job_handle_irq(struct rocket_core *core)rocket_pc_writel (core,OPERATION_ENABLE ,0x0 );rocket_pc_writel (core,INTERRUPT_CLEAR ,0x1ffff ); +rocket_devfreq_record_idle (core); + scoped_guard(mutex, &core->job_lock)if (core->in_flight_job) {if (core->in_flight_job->next_task_idx < core->in_flight_job->task_count) {
@@ -379,6 +384,8 @@ rocket_reset(struct rocket_core *core, struct drm_sched_job *bad)if (core->in_flight_job)pm_runtime_put_noidle (core->dev); +rocket_devfreq_record_idle (core); +iommu_detach_group (NULL , core->iommu_group); core->in_flight_job =NULL ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 09/11] accel/rocket: add devfreq support
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] pm: power leak due to async pm_runtime_put_autosuspend() cancelled by pm_runtime_disable() in…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] pm: power leak due to async pm_runtime_put_autosuspend() cancelled by pm_runtime_disable() in teardown
- [High] pm: SError crash on resume due to ignored return value of dev_pm_opp_set_rate()
- [High] pm: SError on power-on due to unsafe rounding up of the boot rate
- [High] race: UAF / NULL pointer dereference in rocket_npu_restore_boot_rate() racing with shutdown
- [High] race: double-free of devfreq and OPP table due to concurrent sysfs unbinds
--
commit e0300730a57da6df01c259dea64c48000cbfedae
Author: Igor Paunovic <[email protected]>
accel/rocket: add devfreq support
This commit introduces devfreq support to scale the NPU clock rate based on core utilization. It uses a single devfreq device for all cores and runtime resumes all cores when raising the clock rate to prevent hardware SError.
Quoted 5 lines
drivers/accel/rocket/rocket_devfreq.c new file +0
-0
[ ... ]
Quoted 6 lines
drivers/accel/rocket/rocket_devfreq.c new file +6
-0+void rocket_devfreq_suspend (struct rocket_device *rdev) +{ +struct rocket_devfreq *rdevfreq = &rdev->devfreq; + +if (!rdevfreq->devfreq) +return ;
drivers/accel/rocket/rocket_devfreq.c line 298
Quoted 10 lines
drivers/accel/rocket/rocket_devfreq.c new file +10
-0+ +guard (mutex)(&rdevfreq->lock); + +if (!rdevfreq->cores_held) +return ; + +rocket_devfreq_set_rate (rdev, rdevfreq->boot_freq); + rdevfreq->cores_held = false; +rocket_devfreq_release_all (rdev); +}
[Severity: High]
What happens if rocket_devfreq_set_rate() fails here? The return value is ignored, and cores_held is cleared unconditionally. If the rate wasn't lowered successfully but we drop references and allow the core to suspend, will this cause an asynchronous SError when the core later powers on with a raised clock?
[ ... ]
Quoted 21 lines
drivers/accel/rocket/rocket_devfreq.c new file +21
-0+ freq = rdev->npu_boot_rate; + opp =devfreq_recommended_opp (dev, &freq,0 ); +if (IS_ERR (opp)) { + ret =dev_err_probe (dev,PTR_ERR (opp), +"no OPP covers the %lu Hz boot rate\n" , + rdev->npu_boot_rate); + goto err_remove_table; + } + +/* +* From here on the boot rate is the OPP it maps to. The raw rate is +* the firmware's number and need not be in the table at all. +*/ + rdevfreq->boot_freq = freq; + +/* +* Program the supply for the rate the NPU is already running, so that +* the regulator is not switched off underneath it by +* regulator_late_cleanup(). That OPP is the boot rate rounded up to +* the table, so this may raise the clock, and a raised clock is only +* ever programmed with every core held: the same rule as ->target().
drivers/accel/rocket/rocket_devfreq.c line 418
Quoted 10 lines
drivers/accel/rocket/rocket_devfreq.c new file +10
-0+*/ + ret =rocket_devfreq_hold_all (rdev); +if (ret) { +dev_pm_opp_put (opp); +dev_err_probe (dev, ret, +"cannot resume the NPU cores to set the initial OPP\n" ); + goto err_remove_table; + } + ret =dev_pm_opp_set_opp (dev, opp); +rocket_devfreq_release_all (rdev);
[Severity: High]
Is it safe to round up npu_boot_rate to the nearest OPP and then allow the device to suspend? The commit message mentions that an island powered up while the clock is above the rate the bootloader left will take an SError. If devfreq_recommended_opp() rounds the frequency up, and we set it and drop references via rocket_devfreq_release_all(), won't the next power-on trigger the SError because the clock is now higher than the safe bootloader rate?
[ ... ]
Quoted 15 lines
drivers/accel/rocket/rocket_devfreq.c new file +15
-0+void rocket_devfreq_fini (struct rocket_device *rdev) +{ +struct rocket_devfreq *rdevfreq = &rdev->devfreq; +struct device *dev; + +if (!rdevfreq->devfreq) +return ; + + dev = rdevfreq->owner->dev; + +devfreq_remove_device (rdevfreq->devfreq); + rdevfreq->devfreq =NULL ; + +/* +* Lower the clock before letting go of the cores, not after: a core
drivers/accel/rocket/rocket_devfreq.c line 485
Quoted 10 lines
drivers/accel/rocket/rocket_devfreq.c new file +10
-0+* that suspends while the clock is still raised would be unable to +* come back. +*/ + scoped_guard(mutex, &rdevfreq->lock) { +if (rdevfreq->cores_held) { +rocket_devfreq_set_rate (rdev, rdevfreq->boot_freq); + rdevfreq->cores_held = false; +rocket_devfreq_release_all (rdev); + } + }
[Severity: High]
Could concurrent sysfs unbinds cause a double-free here? Since rocket_remove() in rocket_drv.c calls rocket_devfreq_fini() without a driver-wide lock, multiple unbinds could pass the if (!rdevfreq->devfreq) check simultaneously and try to free rdevfreq->devfreq and destroy the mutex twice.
Also, similar to rocket_devfreq_suspend(), if rocket_devfreq_set_rate() fails, won't unconditionally dropping references here lead to an SError on resume?
Quoted 4 lines
drivers/accel/rocket/rocket_drv.c +0
-0
[ ... ]
drivers/accel/rocket/rocket_drv.c static void rocket_remove(struct platform_device *pdev) · line 289
Quoted 8 lines
drivers/accel/rocket/rocket_drv.c +3
-0@@ -268,6 +283,9 @@ static void rocket_remove(struct platform_device *pdev)if (WARN_ON (core <0 ))return ; +/* The devfreq device drives every core, so it goes before any of them. */ +rocket_devfreq_fini (rdev); +rocket_core_fini (&rdev->cores[core]);
[Severity: High]
If rocket_devfreq_fini() uses pm_runtime_put_autosuspend() under the hood to release PM references via rocket_devfreq_release_all(), does rocket_core_fini() immediately disable runtime PM? As per the PM subsystem guide, pm_runtime_disable() cancels any pending asynchronous suspend. Will this leave the device permanently powered on and cause a power leak?
[ ... ]
Quoted 7 lines
drivers/accel/rocket/rocket_drv.c +2
-1@@ -314,7 +332,18 @@ static void rocket_npu_restore_boot_rate(struct rocket_core *core)if (!rdev->npu_boot_rate)return ; - err =clk_set_rate (core->clks[2 ].clk, rdev->npu_boot_rate); +/* +* Go through the OPP core once there is a table, never behind its
drivers/accel/rocket/rocket_drv.c static void rocket_npu_restore_boot_rate(struct rocket_core *core) · line 346
Quoted 10 lines
drivers/accel/rocket/rocket_drv.c +10
-0+* back: it caches the OPP it last applied and skips a request for that +* same OPP, so a raw clk_set_rate() here would make the next request +* for the raised rate a silent no-op, with sysfs reporting a rate the +* hardware was not running. +*/ +if (rdev->devfreq.owner) + err =rocket_devfreq_set_boot_rate (rdev); +else + err =clk_set_rate (core->clks[2 ].clk, rdev->npu_boot_rate); +
[Severity: High]
Is there a race condition between rocket_npu_restore_boot_rate() and rocket_shutdown()? If a job finishes and the PM workqueue calls rocket_device_runtime_suspend() concurrently with system shutdown:
CPU1 in rocket_shutdown():rocket_devfreq_fini(rdev); (sets rdevfreq->owner = NULL and frees OPP table)
CPU2 in rocket_npu_restore_boot_rate() sees rdev->devfreq.owner as valid just before it is set to NULL, and calls rocket_devfreq_set_boot_rate(rdev) which does:
dev_pm_opp_set_rate(rdevfreq->owner->dev, rdevfreq->boot_freq);
Could this result in a NULL pointer dereference or use-after-free of the OPP table since the devfreq owner has been destroyed?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Re: [PATCH v2 09/11] accel/rocket: add devfreq support
On the five findings from the Sashiko review of this patch, in its order. All of this is from reading the code; none of it was reproduced.
1. Cores left powered after unbind: not with the driver's own settings. rocket_core_fini(), which this series does not change, calls pm_runtime_dont_use_autosuspend() before pm_runtime_disable(); that runs rpm_idle() synchronously and suspends the core on the spot. It can happen if root sets power/autosuspend_delay_ms to 0 and unbinds a core with the clock raised: the put then queues an asynchronous suspend that pm_runtime_disable() can cancel. v3 will drop those references synchronously on the teardown path.
2. Ignored return value: true, but here an error does not leave the clock raised. Scaling down, the OPP core sets the clock before the supply, so a regulator error comes after clk_set_rate() has already asked for the 200 MHz boot rate, which TF-A serves from GPLL, and a rate the firmware refuses never reaches the caller. v3 will say so in a comment at both call sites.
3. Rounding the boot rate up: not with the in-tree devicetree, where assigned-clock-rates gives exactly the lowest OPP. Without it, a boot rate above 200 MHz would map to a PVTPLL OPP and the cores would be released there; that is the code-read-only case under "Not done". v3 will create the devfreq device only when the boot rate is at or below the lowest OPP (without assigned-clock-rates this firmware reports 198 MHz, GPLL/6).
4. Restore racing with rocket_devfreq_fini(): real, and this patch introduces it. device_shutdown() pins only the core it shuts down, and __device_release_driver() drops its runtime PM reference before ->remove(), so the last core to go idle can reach rocket_npu_restore_boot_rate() from its autosuspend timer while rocket_devfreq_fini() clears ->owner and removes the OPP table and config. The window is a few microseconds and none of my runs set it up. v3 will take pm_runtime_get_noresume() and pm_runtime_barrier() on every core before ->owner is cleared, and drop them afterwards.
5. Concurrent unbinds: real. Each sysfs unbind takes only its own device lock, so two unbinds can both remove the same devfreq device. The unlocked probe and remove predate this series (Sashiko's finding on the standalone slot-search patch, which I agreed with [1] and the cover letter lists as open), but this patch makes the result worse. The OPP table is not freed twice. v3 will add a driver-wide mutex around rocket_probe(), rocket_remove() and rocket_shutdown().
I will wait for review of v2 before sending v3.
[1] https://lore.kernel.org/r/[email protected]
Igor
Re: [PATCH v2 09/11] accel/rocket: add devfreq support
In reply to Igor PaunovicHi Igor,
Quoted 801 lines · Igor Paunovic
The NPU has run at whatever rate the devicetree pinned it to since the driver was merged, which on the RK3588 is 200 MHz. The hardware reaches 1 GHz, and the firmware will change the rate on request, so let devfreq drive it from how busy the cores actually are.
One devfreq device drives all of the cores, because they have one clock and one supply between them and cannot be scaled apart. It hangs off the first core in devicetree order that carries an OPP table, which on the RK3588, where all three cores reference the shared table, is
rknn_core_0. The choice has to be fixed rather than "whichever core bound last": the devfreq device is named after that core, and a cooling map in the devicetree resolves against that core's node.core->indexis the core's position among the core nodes, so the lowest index is the first node.The awkward part is that the clock is generated by a PVTPLL that sits inside the NPU power islands. An island powered up while the clock is above the rate the bootloader left never acknowledges the power-on, and the first register access into it afterwards takes an asynchronous SError. So before the rate goes up, every core is runtime resumed, and the references are held for as long as the clock stays raised: no island can transition while they are held, which is what makes a raised clock safe rather than merely unlikely to be caught out. The guard and the thing it guards arrive in the same commit, so no commit in the tree ever raises the rate without it.
Unbinding any core takes the devfreq device down, and it comes back once every core is bound again: a rate change resumes all of them, so the device has to be whole for the guard to mean anything. Every walk of the core array here relies on that.
The cost is that a boosted NPU does not power-gate individual cores. It is paid only above the boot rate; at the boot rate the references are dropped and runtime PM behaves as before. What that costs in milliwatts has not been measured on this board. A measurement, or a design that does not need the references at all, would both be welcome.
Utilisation is aggregated as the maximum over the cores, not the sum: the rate has to satisfy the busiest core, and summing would report one saturated core out of three as a third of the load. Whether that is the right aggregation for a shared clock is a fair question for review.
Unlike panfrost, panthor, lima and msm, the driver does not call
devfreq_suspend_device()from runtime suspend. That call ends incancel_delayed_work_sync()on the governor's worker, and the worker is what calls ->target(), which resumes every core: a runtime-suspend callback would wait for a worker that is waiting for that same callback. Instead ->target() returns early when the rate is unchanged, so a governor tick on an idle NPU costs one comparison and resumes nothing.System suspend is different:
dpm_suspend()runsdevfreq_suspend()over every registered devfreq before it walks the devices, from a path that holds none of this driver's locks. What the driver adds is lowering the rate and dropping the references from every core's ->suspend, not only the owning core's.pm_runtime_force_suspend()powers a core down whatever the usage count says, and the owning core need not be the first one suspended, so a suspend that aborts partway would otherwise leave the clock raised over islands that are already gated.The clock and the supply are claimed in one
dev_pm_opp_set_config()call before the table is added. Naming the clock there is not optional: with no name the OPP core takes the first clock in the node, which is the bus clock. Configuration and table live on the owning core, which need not be the device being probed at the time, so none of it can be devres: the call hands back a token, androcket_devfreq_fini()releases it, and the table, by hand. Left to devres, a later bind would fail on an OPP table the previous teardown never emptied.The rate is changed through
dev_pm_opp_set_rate(), so that the supply moves with it. Nothing may set the rate behind the OPP core's back: it caches the OPP it last applied and skips a repeat request for it, so a rawclk_set_rate()would turn the next request for a raised rate into a silent no-op. The boot-rate restore added in the previous patch is converted accordingly.The rate the previous patch reads at probe is the firmware's own number and need not be in the OPP table: a board that does not pin the clock with assigned-clock-rates gets whatever the firmware's divider produces. So the boot rate is normalised to the table once, at init, to the lowest OPP that is not below it, and everything here compares against and returns to that OPP. Comparing
cur_freqagainst a rate that is not in the table would leave the cores held after the first change, with no governor tick ever able to match it.->get_cur_freq() and the status callback report the rate this driver last requested rather than asking the clock: devfreq queries them from sysfs with no runtime PM reference of its own, and the answer has to be a rate from the OPP table or
devfreq_get_freq_level()does not find it and devfreq warns on every transition. "Requested" is the accurate word: a rate the firmware refuses does not come back as an error, because the clock framework ignores what the clock'sset_ratereturns. Keeping every request to the OPP table, which names only the rates the firmware accepts, keeps a request from being refused; what sysfs shows is still the request, not a measurement.A devicetree with no OPP table is not an error: the driver returns without a devfreq device and the NPU keeps its boot rate, as before this patch.
The governor thresholds are a starting point taken from the other accelerators in tree, not a measurement. Inference workloads have not been profiled against them.
Assisted-by LLM sparse checkpatchSigned-off-by Igor Paunovic <[email protected]>---
v2:
- The devfreq device goes on the first core in devicetree order with an OPP table, not on whichever core with the table bound first, as v1 would have done with the table on all three cores (Nicolas).
- The boot rate is normalised to the OPP table at init. Found by reading the code: without assigned-clock-rates the raw rate is not in the table and the cores would have stayed held after the first change. Code read only so far; a devicetree without the property has not been booted.
- Init holds every core while it programs the initial OPP, since rounding the boot rate up to the table may raise the clock.
- "last programmed" is now "last requested", with the reason.
- Comments and text updated for the shared table; a comment that counted ten governor ticks a second now counts twenty (50 ms polling).
- The commit message now says that unbinding any core takes the devfreq device down until all are bound again; v1 did the same without saying so.
- The utilisation counters of a core that was unbound and bound again start from zero.
- A comment overhold_all()states the invariant the loops rely on.drivers/accel/rocket/Kconfig | 2 + drivers/accel/rocket/Makefile | 1 + drivers/accel/rocket/rocket_core.h | 11 + drivers/accel/rocket/rocket_devfreq.c | 500 ++++++++++++++++++++++++++ drivers/accel/rocket/rocket_devfreq.h | 65 ++++ drivers/accel/rocket/rocket_device.h | 3 + drivers/accel/rocket/rocket_drv.c | 59 ++- drivers/accel/rocket/rocket_job.c | 7 + 8 files changed, 646 insertions(+), 2 deletions(-) create mode 100644 drivers/accel/rocket/rocket_devfreq.c create mode 100644 drivers/accel/rocket/rocket_devfreq.hdrivers/accel/rocket/Kconfig +2
-0@@ -8,6 +8,8 @@ config DRM_ACCEL_ROCKET depends on MMU select DRM_SCHED select DRM_GEM_SHMEM_HELPER + select PM_DEVFREQ + select DEVFREQ_GOV_SIMPLE_ONDEMAND help Choose this option if you have a Rockchip SoC that contains a compatible Neural Processing Unit (NPU), such as the RK3588. Called bydrivers/accel/rocket/Makefile +1
-0@@ -5,6 +5,7 @@ obj-$(CONFIG_DRM_ACCEL_ROCKET) := rocket.o rocket-y := \ rocket_core.o \ rocket_device.o \ + rocket_devfreq.o \ rocket_drv.o \ rocket_gem.o \ rocket_job.odrivers/accel/rocket/rocket_core.h +11
-0@@ -7,6 +7,7 @@#include <drm/gpu_scheduler.h> #include <linux/clk.h> #include <linux/io.h> +#include <linux/ktime.h> #include <linux/mutex_types.h> #include <linux/reset.h> @@ -60,6 +61,16 @@ struct rocket_core {struct drm_gpu_scheduler sched;u64 fence_context;u64 emit_seqno; + +/* +* Utilisation seen by devfreq, guarded by rdev->devfreq.busy_lock. A +* core runs one task at a time, so a flag is exact here and, unlike a +* counter, cannot be left skewed by a job the reset path tore down. +*/ +bool busy; +ktime_t busy_time; +ktime_t idle_time; +ktime_t time_last_update; };int rocket_core_init (struct rocket_core *core);drivers/accel/rocket/rocket_devfreq.c new file +500
-0@@ -0,0 +1,500 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright 2025 Igor Paunovic <[email protected]> */ + +#include <linux/clk.h> +#include <linux/devfreq.h> +#include <linux/ktime.h> +#include <linux/minmax.h> +#include <linux/of.h> +#include <linux/pm_opp.h> +#include <linux/pm_runtime.h> + +#include "rocket_core.h" +#include "rocket_device.h" +#include "rocket_devfreq.h" + +/* +* One clock and one supply feed all of the NPU cores, so a single devfreq +* device drives them together. It hangs off the first core in devicetree +* order that carries an OPP table. +* +* The awkward part is that the clock is generated by a PVTPLL that sits +* inside the NPU power islands. An island that is powered up while the clock +* is above the rate the bootloader left never acknowledges the power-on, and +* the first register access into it afterwards takes an asynchronous SError. +* +* Lowering the rate is fine at any time as long as the boot rate is one the +* firmware serves from GPLL, which on the RK3588 is the pinned 200 MHz: for +* that rate it writes only CRU clock selectors. Raising the rate is not, so +* before it goes up every core is runtime resumed and the +* references are kept for as long as the clock stays raised. While they are +* held no core can suspend, so no island can transition at all, which is the +* property that makes the raised clock safe rather than merely unlikely to be +* caught out. +* +* The cost is that a busy NPU does not power-gate individual cores. It is +* paid only above the boot rate: at the boot rate the references are dropped +* and runtime PM behaves exactly as it did before this file existed. The cost +* in milliwatts has not been measured on this board, and a measurement or a +* better idea would both be welcome. +*/ + +static void rocket_devfreq_update_utilisation (struct rocket_core *core) +{ +ktime_t now =ktime_get (); +ktime_t elapsed =ktime_sub (now, core->time_last_update); + +if (core->busy) + core->busy_time =ktime_add (core->busy_time, elapsed); +else + core->idle_time =ktime_add (core->idle_time, elapsed); + + core->time_last_update = now; +} + +/* +* The devfreq device exists only while every slot in rdev->cores[] is +* filled: it goes up when num_cores reaches max_cores and comes down in +* rocket_remove() before the leaving core empties its slot. The loops +* over num_cores here and below rely on that. +*/ +static int rocket_devfreq_hold_all (struct rocket_device *rdev) +{ +unsigned int i; +int ret; + +for (i =0 ; i < rdev->num_cores; i++) { + ret =pm_runtime_resume_and_get (rdev->cores[i].dev); +if (ret <0 ) { +while (i--) +pm_runtime_put_autosuspend (rdev->cores[i].dev); + +return ret; + } + } + +return 0 ; +} + +static void rocket_devfreq_release_all (struct rocket_device *rdev) +{ +unsigned int i; + +for (i =0 ; i < rdev->num_cores; i++) +pm_runtime_put_autosuspend (rdev->cores[i].dev); +} + +/* Caller holds rdev->devfreq.lock. */ +static int rocket_devfreq_set_rate (struct rocket_device *rdev,unsigned long freq) +{ +struct rocket_devfreq *rdevfreq = &rdev->devfreq; +struct device *dev = rdevfreq->owner->dev; +int ret; + + ret =dev_pm_opp_set_rate (dev, freq); +if (ret) { +dev_err (dev,"failed to set the NPU rate to %lu Hz: %d\n" , freq, ret); +return ret; + } + +WRITE_ONCE (rdevfreq->cur_freq, freq); + +return 0 ; +} + +static int rocket_devfreq_target (struct device *dev,unsigned long *freq,u32 flags) +{ +struct rocket_device *rdev =dev_get_drvdata (dev); +struct rocket_devfreq *rdevfreq = &rdev->devfreq; +struct dev_pm_opp *opp; +int ret; + + opp =devfreq_recommended_opp (dev, freq, flags); +if (IS_ERR (opp)) +return PTR_ERR (opp); +dev_pm_opp_put (opp); + +guard (mutex)(&rdevfreq->lock); + +/* +* The governor calls this on every tick, including the ticks where it +* arrives at the rate the NPU is already running. Without this an idle +* NPU would resume all of its cores twenty times a second to set the +* rate they already have. +*/ +if (*freq ==READ_ONCE (rdevfreq->cur_freq)) +return 0 ; + +if (!rdevfreq->cores_held) { + ret =rocket_devfreq_hold_all (rdev); +if (ret) { +/* +* Runtime PM is disabled on the way into system +* suspend, so this is the ordinary way for a governor +* tick that raced with it to end. +*/ +dev_dbg (dev,"cannot resume the NPU cores to change rate: %d\n" , ret); +return ret; + } + rdevfreq->cores_held = true; + } + + ret =rocket_devfreq_set_rate (rdev, *freq); + +/* At the boot rate the cores are free to suspend again. */ +if (READ_ONCE (rdevfreq->cur_freq) <= rdevfreq->boot_freq) { + rdevfreq->cores_held = false; +rocket_devfreq_release_all (rdev); + } + +return ret; +} + +static int rocket_devfreq_get_dev_status (struct device *dev, +struct devfreq_dev_status *status) +{ +struct rocket_device *rdev =dev_get_drvdata (dev); +ktime_t busy =0 , total =0 ; +unsigned int i; + + scoped_guard(spinlock_irqsave, &rdev->devfreq.busy_lock) { +for (i =0 ; i < rdev->num_cores; i++) { +struct rocket_core *core = &rdev->cores[i]; + +rocket_devfreq_update_utilisation (core); + +/* +* The cores share the clock, so what the rate has to +* satisfy is the busiest of them. Adding the cores up +* instead would report one saturated core out of three +* as a third of the load, and clock down underneath it. +*/ + busy =max (busy, core->busy_time); + total =max (total,ktime_add (core->busy_time, core->idle_time)); + + core->busy_time =0 ; + core->idle_time =0 ; + } + } + + status->busy_time =ktime_to_ns (busy); + status->total_time =ktime_to_ns (total); + status->current_frequency =READ_ONCE (rdev->devfreq.cur_freq); + +dev_dbg (dev,"busy %lu total %lu %lu%% freq %lu MHz\n" , + status->busy_time, status->total_time, + status->busy_time *100 /max (status->total_time,1UL ), + status->current_frequency /1000 /1000 ); + +return 0 ; +} + +/* +* Report the rate this driver last requested, not what the firmware says. +* devfreq asks for the current frequency from sysfs as well, without a runtime +* PM reference of its own, and the answer has to be one of the rates in the +* OPP table or devfreq_get_freq_level() will not find it and every transition +* will be logged as unknown. +* +* "Requested" is the accurate word: a rate the firmware refuses does not come +* back as an error, because the clock framework ignores what the clock's +* set_rate returns and the clock stays where it was. Keeping every request to +* the OPP table, which names only the rates the firmware accepts, keeps a +* request from being refused; what is reported is still the request, not a +* measurement. +*/ +static int rocket_devfreq_get_cur_freq (struct device *dev,unsigned long *freq) +{ +struct rocket_device *rdev =dev_get_drvdata (dev); + + *freq =READ_ONCE (rdev->devfreq.cur_freq); + +return 0 ; +} + +static struct devfreq_dev_profile rocket_devfreq_profile = { + .timer =DEVFREQ_TIMER_DELAYED , + .polling_ms =50 , + .target = rocket_devfreq_target, + .get_dev_status = rocket_devfreq_get_dev_status, + .get_cur_freq = rocket_devfreq_get_cur_freq, +}; + +void rocket_devfreq_record_busy (struct rocket_core *core) +{ +struct rocket_devfreq *rdevfreq = &core->rdev->devfreq; + +if (!rdevfreq->devfreq) +return ; + + scoped_guard(spinlock_irqsave, &rdevfreq->busy_lock) { +rocket_devfreq_update_utilisation (core); + core->busy = true; + } +} + +/* +* Idempotent on purpose: a job that times out is torn down by the reset path, +* which cannot know whether the completion interrupt got there first. +*/ +void rocket_devfreq_record_idle (struct rocket_core *core) +{ +struct rocket_devfreq *rdevfreq = &core->rdev->devfreq; + +if (!rdevfreq->devfreq) +return ; + + scoped_guard(spinlock_irqsave, &rdevfreq->busy_lock) { +rocket_devfreq_update_utilisation (core); + core->busy = false; + } +} + +/* +* Put the clock back to the boot OPP through the OPP core. Called with no +* lock held, from the last core on its way down. +*/ +int rocket_devfreq_set_boot_rate (struct rocket_device *rdev) +{ +struct rocket_devfreq *rdevfreq = &rdev->devfreq; +int ret; + + ret =dev_pm_opp_set_rate (rdevfreq->owner->dev, rdevfreq->boot_freq); +if (!ret) +WRITE_ONCE (rdevfreq->cur_freq, rdevfreq->boot_freq); + +return ret; +} + +/* +* System suspend, called from every core's ->suspend before it is forced down. +* The first one to get here does the work and the rest are no-ops. +* +* It has to be every core and not just the one that owns the devfreq device. +* That core is suspended last, and a suspend that aborts partway - a pending +* wakeup, or this driver's own -EBUSY on a core that is still busy - would +* never reach it, leaving the clock raised over already gated islands. +* +* No governor tick can be in flight here: dpm_suspend() calls devfreq_suspend() +* before it walks the devices, which stops the monitor on every registered +* devfreq. That is also where this driver does get devfreq_suspend_device() - +* from the PM core, on a path that holds no runtime PM lock of ours. +*/ +void rocket_devfreq_suspend (struct rocket_device *rdev) +{ +struct rocket_devfreq *rdevfreq = &rdev->devfreq; + +if (!rdevfreq->devfreq) +return ; + +guard (mutex)(&rdevfreq->lock); + +if (!rdevfreq->cores_held) +return ; + +rocket_devfreq_set_rate (rdev, rdevfreq->boot_freq); + rdevfreq->cores_held = false; +rocket_devfreq_release_all (rdev); +} + +int rocket_devfreq_init (struct rocket_device *rdev) +{ +static const char *const clk_names[] = {"npu" ,NULL }; +static const char *const supplies[] = {"npu" ,NULL }; +struct dev_pm_opp_config config = { + .clk_names = clk_names, + .regulator_names = supplies, + }; +struct rocket_devfreq *rdevfreq = &rdev->devfreq; +struct rocket_core *owner =NULL ; +struct dev_pm_opp *opp; +struct device *dev; +unsigned long freq; +unsigned int i; +int ret; + +/* +* The devfreq device hangs off the first core in devicetree order that +* carries an OPP table: on the RK3588 every core references the shared +* table, so that is rknn_core_0. It has to be a fixed choice and not +* whichever core bound last, because the devfreq device is named after +* this core and a cooling map in the devicetree resolves against its +* node. core->index is the core's position among the core nodes, so +* the lowest index is the first node. +*/ +for (i =0 ; i < rdev->num_cores; i++) { +struct rocket_core *core = &rdev->cores[i]; + +if (!of_property_present (core->dev->of_node,"operating-points-v2" )) +continue ; + +if (!owner || core->index < owner->index) + owner = core; + } + +/* +* No OPP table is not an error. It asks for the NPU to stay at the +* rate it booted at, which is what this driver did before devfreq. +*/ +if (!owner) +return 0 ; + + dev = owner->dev; + +/* +* None of this can be devres. It is attached to the owning core, while +* the device being probed right now is whichever core happened to bind +* last, so devres would outlive the teardown in rocket_devfreq_fini() +* and a later rebind would find the OPP table still populated. +* +* Naming the clock matters as much as claiming the supply: without it +* the OPP core takes the first clock in the node, which is the bus +* clock, and would scale that instead of the compute clock. +*/ + ret =dev_pm_opp_set_config (dev, &config); +if (ret <0 ) { +if (ret != -ENODEV ) +return dev_err_probe (dev, ret, +"failed to set the OPP clock and supply\n" ); + +dev_info (dev,"no NPU supply described, leaving the clock alone\n" ); +return 0 ; + } + rdevfreq->opp_token = ret; + + ret =dev_pm_opp_of_add_table (dev); +if (ret) { +if (ret != -ENODEV ) +dev_err_probe (dev, ret,"failed to add the OPP table\n" ); +else + ret =0 ; + + goto err_clear_config; + } + +mutex_init (&rdevfreq->lock); +spin_lock_init (&rdevfreq->busy_lock); + +for (i =0 ; i < rdev->num_cores; i++) { +struct rocket_core *core = &rdev->cores[i]; + +/* A core that was unbound and bound again starts over. */ + core->busy = false; + core->busy_time =0 ; + core->idle_time =0 ; + core->time_last_update =ktime_get (); + } + + freq = rdev->npu_boot_rate; + opp =devfreq_recommended_opp (dev, &freq,0 ); +if (IS_ERR (opp)) { + ret =dev_err_probe (dev,PTR_ERR (opp), +"no OPP covers the %lu Hz boot rate\n" , + rdev->npu_boot_rate); + goto err_remove_table; + } + +/* +* From here on the boot rate is the OPP it maps to. The raw rate is +* the firmware's number and need not be in the table at all. +*/ + rdevfreq->boot_freq = freq; + +/* +* Program the supply for the rate the NPU is already running, so that +* the regulator is not switched off underneath it by +* regulator_late_cleanup(). That OPP is the boot rate rounded up to +* the table, so this may raise the clock, and a raised clock is only +* ever programmed with every core held: the same rule as ->target(). +*/ + ret =rocket_devfreq_hold_all (rdev); +if (ret) { +dev_pm_opp_put (opp); +dev_err_probe (dev, ret, +"cannot resume the NPU cores to set the initial OPP\n" ); + goto err_remove_table; + } + ret =dev_pm_opp_set_opp (dev, opp); +rocket_devfreq_release_all (rdev); +dev_pm_opp_put (opp); +if (ret) { +dev_err_probe (dev, ret,"failed to set the initial OPP\n" ); + goto err_remove_table; + } + + rdevfreq->cur_freq = freq; + rdevfreq->owner = owner; + rocket_devfreq_profile.initial_freq = freq; + +/* +* A starting point taken from the other accelerators in tree, not a +* measurement: inference workloads have not been profiled against +* these thresholds. +*/ + rdevfreq->gov_data.upthreshold =50 ; + rdevfreq->gov_data.downdifferential =10 ; + + rdevfreq->devfreq =devfreq_add_device (dev, &rocket_devfreq_profile, +DEVFREQ_GOV_SIMPLE_ONDEMAND , + &rdevfreq->gov_data); +if (IS_ERR (rdevfreq->devfreq)) { + ret =PTR_ERR (rdevfreq->devfreq); + rdevfreq->devfreq =NULL ; + rdevfreq->owner =NULL ; + +dev_err_probe (dev, ret,"failed to add the devfreq device\n" ); + goto err_remove_table; + } + +return 0 ; + +err_remove_table: +mutex_destroy (&rdevfreq->lock); +dev_pm_opp_of_remove_table (dev); +err_clear_config: +dev_pm_opp_clear_config (rdevfreq->opp_token); + rdevfreq->opp_token =0 ; + +return ret; +} + +void rocket_devfreq_fini (struct rocket_device *rdev) +{ +struct rocket_devfreq *rdevfreq = &rdev->devfreq; +struct device *dev; + +if (!rdevfreq->devfreq) +return ; + + dev = rdevfreq->owner->dev; + +devfreq_remove_device (rdevfreq->devfreq); + rdevfreq->devfreq =NULL ; + +/* +* Lower the clock before letting go of the cores, not after: a core +* that suspends while the clock is still raised would be unable to +* come back. +*/ + scoped_guard(mutex, &rdevfreq->lock) { +if (rdevfreq->cores_held) { +rocket_devfreq_set_rate (rdev, rdevfreq->boot_freq); + rdevfreq->cores_held = false; +rocket_devfreq_release_all (rdev); + } + } + +/* +* Undo the OPP setup by hand, in the reverse order. Everything above +* lives on the owning core rather than on the device that was probing +* when it was set up, so nothing here is released by devres; leaving +* the table populated would make the next bind fail with the OPP core +* complaining that it is not empty. After this the owner is gone, so +* anything that still wants the boot rate asks the clock directly. +*/ + rdevfreq->owner =NULL ; +mutex_destroy (&rdevfreq->lock); +dev_pm_opp_of_remove_table (dev); +dev_pm_opp_clear_config (rdevfreq->opp_token); + rdevfreq->opp_token =0 ; +}drivers/accel/rocket/rocket_devfreq.h new file +65
-0@@ -0,0 +1,65 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright 2025 Igor Paunovic <[email protected]> */ + +#ifndef __ROCKET_DEVFREQ_H__ +#define __ROCKET_DEVFREQ_H__ + +#include <linux/devfreq.h> +#include <linux/mutex_types.h> +#include <linux/spinlock_types.h> + +struct rocket_core ; +struct rocket_device ; + +struct rocket_devfreq { +struct devfreq *devfreq; +struct devfreq_simple_ondemand_data gov_data; + +/* +* The core the devfreq device hangs off: the first in devicetree order +* to carry an OPP table. NULL when no core does. +*/ +struct rocket_core *owner; + +/* +* The OPP clock and supply configuration is attached to the owning +* core, which is not the device this driver is probing when it is set +* up, so it cannot be devres. Zero means nothing is attached. +*/ +int opp_token; + +/* +* Serialises rate changes against each other and against the set of +* runtime PM references taken below. +* +* This is never taken from a runtime PM callback. A rate change +* resumes every core while holding it, so a core that took it on its +* way down would wait for a rate change that is waiting for that same +* core to finish suspending. +*/ +struct mutex lock; +unsigned long cur_freq; +bool cores_held; + +/* +* The boot rate as an OPP: the lowest rate in the table that is not +* below rdev->npu_boot_rate. The raw boot rate is whatever the firmware +* reported at probe and need not be in the table, and comparing +* cur_freq against a rate that is not in the table would leave the +* cores held for good after the first change. Everything here compares +* against and returns to this instead. +*/ +unsigned long boot_freq; + +/* Guards the utilisation fields of every core. */ +spinlock_t busy_lock; +}; + +int rocket_devfreq_init (struct rocket_device *rdev); +void rocket_devfreq_fini (struct rocket_device *rdev); +void rocket_devfreq_suspend (struct rocket_device *rdev); +int rocket_devfreq_set_boot_rate (struct rocket_device *rdev); +void rocket_devfreq_record_busy (struct rocket_core *core); +void rocket_devfreq_record_idle (struct rocket_core *core); + +#endif /* __ROCKET_DEVFREQ_H__ */ drivers/accel/rocket/rocket_device.h +3
-0@@ -11,6 +11,7 @@#include <linux/platform_device.h> #include "rocket_core.h" +#include "rocket_devfreq.h" struct rocket_device {struct drm_device ddev;@@ -37,6 +38,8 @@ struct rocket_device {*/ unsigned long npu_boot_rate;atomic_t active_cores; + +struct rocket_devfreq devfreq; }; struct rocket_device *rocket_device_init(struct platform_device *pdev,drivers/accel/rocket/rocket_drv.c +1
-0@@ -14,6 +14,7 @@#include <linux/pm_runtime.h> #include "rocket_device.h" +#include "rocket_devfreq.h" #include "rocket_drv.h" #include "rocket_gem.h" #include "rocket_job.h" @@ -244,6 +245,20 @@ static int rocket_probe(struct platform_device *pdev)if (ret)
drivers/accel/rocket/rocket_drv.c static int rocket_probe(struct platform_device *pdev) · line 255
Quoted 10 lines
drivers/accel/rocket/rocket_drv.c +8
-0goto err_core; +/* +* Every core described in the devicetree has to be bound before the +* devfreq device goes up. A rate change resumes all of them and keeps +* them resumed, and a core that had not probed yet would come up later +* underneath a raised clock. +*/ +if (rdev->num_cores == rdev->max_cores) { + ret =rocket_devfreq_init (rdev);
IMHO, handling rocket_devfreq_init() error as critical error to disable core is too much. How about just printing error for user?
Thanks,
Sidong
Quoted 130 lines
drivers/accel/rocket/rocket_drv.c +48
-2+if (ret) { +rocket_core_fini (&rdev->cores[core]); + goto err_core; + } + } +return 0 ; err_core:@@ -268,6 +283,9 @@ static void rocket_remove(struct platform_device *pdev)if (WARN_ON (core <0 ))return ; +/* The devfreq device drives every core, so it goes before any of them. */ +rocket_devfreq_fini (rdev); +rocket_core_fini (&rdev->cores[core]); rdev->cores[core].dev =NULL ; rdev->num_cores--;@@ -314,7 +332,18 @@ static void rocket_npu_restore_boot_rate(struct rocket_core *core)if (!rdev->npu_boot_rate)return ; - err =clk_set_rate (core->clks[2 ].clk, rdev->npu_boot_rate); +/* +* Go through the OPP core once there is a table, never behind its +* back: it caches the OPP it last applied and skips a request for that +* same OPP, so a raw clk_set_rate() here would make the next request +* for the raised rate a silent no-op, with sysfs reporting a rate the +* hardware was not running. +*/ +if (rdev->devfreq.owner) + err =rocket_devfreq_set_boot_rate (rdev); +else + err =clk_set_rate (core->clks[2 ].clk, rdev->npu_boot_rate); +if (err) dev_warn(core->dev, "failed to restore the NPU boot rate of %lu Hz: %d\n",@@ -360,14 +389,38 @@ static int rocket_device_runtime_suspend(struct device *dev)return 0 ; } +static int rocket_device_suspend (struct device *dev) +{ +struct rocket_device *rdev =dev_get_drvdata (dev); + +/* +* pm_runtime_force_suspend() below powers this core down whatever the +* runtime PM usage count says, so the references taken while the clock +* is raised do not hold it off. Put the rate back first; the call is a +* no-op on every core after the first. +*/ +rocket_devfreq_suspend (rdev); + +return pm_runtime_force_suspend (dev); +} +EXPORT_GPL_DEV_PM_OPS (rocket_pm_ops) = { RUNTIME_PM_OPS(rocket_device_runtime_suspend, rocket_device_runtime_resume, NULL) -SYSTEM_SLEEP_PM_OPS (pm_runtime_force_suspend, pm_runtime_force_resume) +SYSTEM_SLEEP_PM_OPS (rocket_device_suspend, pm_runtime_force_resume) };/* * A kexec hands the next kernel whatever rate is set here, and that kernel * will power the islands up before it looks at the clock. +* +* Take the devfreq device down before restoring the rate rather than after. +* Nothing freezes workqueues on this path, so a governor tick that landed +* after the restore would raise the clock straight back up and hand on exactly +* what this is here to prevent. +* +* The hook runs once per core. After the first call the devfreq device is +* gone, and each later restore asks the clock for the rate it already has, +* which the clock framework drops before it reaches the firmware. */ static void rocket_shutdown (struct platform_device *pdev) {@@ -377,6 +430,8 @@ static void rocket_shutdown(struct platform_device *pdev)if (!rdev)return ; +rocket_devfreq_fini (rdev); + core =find_core_for_dev (&pdev->dev);if (core >=0 )rocket_npu_restore_boot_rate (&rdev->cores[core]);drivers/accel/rocket/rocket_job.c +7
-0@@ -15,6 +15,7 @@#include "rocket_core.h" #include "rocket_device.h" +#include "rocket_devfreq.h" #include "rocket_drv.h" #include "rocket_job.h" #include "rocket_registers.h" @@ -149,6 +150,8 @@ static void rocket_job_hw_submit(struct rocket_core *core, struct rocket_job *jorocket_pc_writel (core,TASK_DMA_BASE_ADDR ,PC_TASK_DMA_BASE_ADDR_DMA_BASE_ADDR (0x0 )); +rocket_devfreq_record_busy (core); +rocket_pc_writel (core,OPERATION_ENABLE ,PC_OPERATION_ENABLE_OP_EN (1 ));dev_dbg (core->dev,"Submitted regcmd at 0x%llx to core %d" , task->regcmd, core->index);@@ -348,6 +351,8 @@ static void rocket_job_handle_irq(struct rocket_core *core)rocket_pc_writel (core,OPERATION_ENABLE ,0x0 );rocket_pc_writel (core,INTERRUPT_CLEAR ,0x1ffff ); +rocket_devfreq_record_idle (core); + scoped_guard(mutex, &core->job_lock)if (core->in_flight_job) {if (core->in_flight_job->next_task_idx < core->in_flight_job->task_count) {@@ -379,6 +384,8 @@ rocket_reset(struct rocket_core *core, struct drm_sched_job *bad)if (core->in_flight_job)pm_runtime_put_noidle (core->dev); +rocket_devfreq_record_idle (core); +iommu_detach_group (NULL , core->iommu_group); core->in_flight_job =NULL ;Signature · 1 line
2.43.0
Re: [PATCH v2 09/11] accel/rocket: add devfreq support
Hi Sidong,
Quoted 2 lines · Sidong Yang
IMHO, handling
rocket_devfreq_init()error as critical error to disable core is too much. How about just printing error for user?
Thanks for reading this far into it. Agreed: rocket_devfreq_init() runs from the probe of the core that binds last, which need not be the core the devfreq device hangs off, so a failure there takes down one NPU core while the remaining cores keep running without devfreq anyway. panfrost, lima and panthor do fail their probe at this point, but there it is the whole device; msm_devfreq_init() only logs, as you suggest.
v3 will warn and carry on without devfreq. On that path the driver will hold no OPP table, no OPP configuration and no runtime PM references, the same as a board that describes no OPP table.
-EPROBE_DEFER stays fatal: npu-supply is first requested there, through dev_pm_opp_set_config(), and swallowing a deferral would leave the board without frequency scaling for good.
All of this is from reading the code; nothing was run for this mail.
Igor
PATCH v2 10/11 accel/rocket: register a devfreq cooling device
In reply to Igor PaunovicWith devfreq driving the NPU clock, a thermal zone can now throttle the NPU by capping that clock. Register the cooling device so a devicetree can bind it to a zone.
The _em variant is used, not because there is an energy model today but so that there will be one the day a power coefficient for this NPU is measured. There is none now: the NPU core nodes carry no dynamic-power-coefficient, Rockchip does not publish one, and a made-up number would be worse than no number. devfreq_cooling_em_register() logs the missing model at debug level and registers the cooling device anyway, so what this gets today is step-wise throttling with no power model for the IPA governor to use. Measuring the coefficient is follow-up work.
Registration is allowed to fail. A kernel built without DEVFREQ_THERMAL gets a stub that returns an error, and losing throttling is not a reason to refuse to drive the NPU at all, so the failure is logged and probe carries on. The cooling device is unregistered by hand before the devfreq device it is attached to goes away.
---
v2: wording only, for the shared table.
drivers/accel/rocket/rocket_devfreq.c | 25 +++++++++++++++++++++++++ drivers/accel/rocket/rocket_devfreq.h | 2 ++ 2 files changed, 27 insertions(+)
drivers/accel/rocket/rocket_devfreq.c +25 -0
@@ -3,6 +3,7 @@#include <linux/clk.h> #include <linux/devfreq.h> +#include <linux/devfreq_cooling.h> #include <linux/ktime.h> #include <linux/minmax.h> #include <linux/of.h>
@@ -446,6 +447,25 @@ int rocket_devfreq_init(struct rocket_device *rdev) goto err_remove_table; } +/* +* Thermal throttling is optional, so a kernel built without +* DEVFREQ_THERMAL keeps a working NPU rather than a failed probe. +* +* The _em variant is used so that the driver is ready for an energy +* model the day a power coefficient for this NPU is measured. There is +* none today: the NPU core nodes have no dynamic-power-coefficient, +* the vendor does not publish one, and inventing a number would be +* worse than having none. Without it the EM registration inside is +* skipped and throttling is step-wise, with no power model for IPA to +* use. +*/ + rdevfreq->cooling =devfreq_cooling_em_register (rdevfreq->devfreq,NULL ); +if (IS_ERR (rdevfreq->cooling)) { +dev_info (dev,"no devfreq cooling device (%pe), NPU will not be throttled\n" , + rdevfreq->cooling); + rdevfreq->cooling =NULL ; + } +return 0 ; err_remove_table:
@@ -468,6 +488,11 @@ void rocket_devfreq_fini(struct rocket_device *rdev) dev = rdevfreq->owner->dev; +if (rdevfreq->cooling) { +devfreq_cooling_unregister (rdevfreq->cooling); + rdevfreq->cooling =NULL ; + } +devfreq_remove_device (rdevfreq->devfreq); rdevfreq->devfreq =NULL ;
drivers/accel/rocket/rocket_devfreq.h +2 -0
@@ -10,9 +10,11 @@struct rocket_core ;struct rocket_device ; +struct thermal_cooling_device ;struct rocket_devfreq {struct devfreq *devfreq; +struct thermal_cooling_device *cooling;struct devfreq_simple_ondemand_data gov_data;/*
Signature · 1 line
2.43.0
PATCH v2 11/11 arm64: dts: rockchip: rk3588: add passive cooling to the NPU thermal zone
In reply to Igor PaunovicThe NPU zone has had only a critical trip at 115 degrees, which is a shutdown and not a cooling policy. Now that the NPU can be throttled by capping its clock, give the zone a passive trip and a cooling map, in the same shape and at the same temperatures as the GPU zone right above it:
85 degrees with 2 degrees of hysteresis, and a 100 ms passive polling delay.
The #cooling-cells property goes on rknn_core_0, the first of the three core nodes, as the one node that stands for the shared clock as a cooling device. The other two cores have no clock of their own and cannot be throttled independently of it.
The cooling map is inert until the driver registers a cooling device:thermal_of_should_bind() only resolves a map entry once a matching cdev appears, so this patch on its own changes nothing but the trip point. On a board that leaves the NPU disabled the passive trip has no cooling device to act on and only changes the polling rate above 85 degrees, as the GPU zone's trip already does on a board without the GPU.
The thermal path itself has not been exercised on the board this was written on. Reaching 85 degrees on an NPU workload with the fan curve here has not been possible, so what is verified is that the zone parses and binds, not that throttling engages at temperature.
---
v2: commit message only: wording for the shared table, and a sentence on boards that leave the NPU disabled. The diff is unchanged.
arch/arm64/boot/dts/rockchip/rk3588-base.dtsi | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
arch/arm64/boot/dts/rockchip/rk3588-base.dtsi +16 -1
@@ -1162,6 +1162,7 @@ rknn_core_0: npu@fdab0000 { clock-names = "aclk", "hclk", "npu", "pclk"; assigned-clocks = <&scmi_clk SCMI_CLK_NPU>; assigned-clock-rates = <200000000>; + #cooling-cells = <2>; resets = <&cru SRST_A_RKNN0>, <&cru SRST_H_RKNN0>; reset-names = "srst_a", "srst_h"; power-domains = <&power RK3588_PD_NPUTOP>;
@@ -3212,17 +3213,31 @@ map0 { }; npu_thermal: npu-thermal { - polling-delay-passive = <0>; + polling-delay-passive = <100>; polling-delay = <0>; thermal-sensors = <&tsadc 6>; trips { + npu_alert: npu-alert { + temperature = <85000>; + hysteresis = <2000>; + type = "passive"; + }; + npu_crit: npu-crit { temperature = <115000>; hysteresis = <0>; type = "critical"; }; }; + + cooling-maps { + map0 { + trip = <&npu_alert>; + cooling-device = + <&rknn_core_0 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>; + }; + }; }; };
Signature · 1 line
2.43.0
Le mardi 22 septembre 2026 à 10:01 +0200, Igor Paunovic a écrit :
Quoted 5 lines
The rocket driver runs the NPU at whatever rate the devicetree pinned it to: 200 MHz on the RK3588, out of the 1 GHz the hardware reaches. This series adds devfreq, the OPP table and the thermal plumbing that go with it.
Quoted 7 lines
v1: https://lore.kernel.org/r/[email protected]
As Tomeu asked [1], the slot-search fix now leads the series, with the two rocket fixes this builds on and, as patch 4, 2/2 of my July lifecycle series [2], without which v2 oopsed when one core was unbound, bound and unbound again. KASAN found that v1 4/7 (now 8) kept a clk handle that unbinding freed; the notes under each --- list the rest.
In future series or update, can you use common formatting in the future, I find your changelog hard to read. Do something like:
Since v1:
- Changed patch X with action Z
- Fix blabla
- ...
Its actually good to be more concise in your communication, "wall of text" are difficult to cope with for maintainers.
cheers,
Nicolas
Quoted 243 lines
Hardware constraint
-------------------The three cores share one clock and one supply. The clock comes from a PVTPLL inside the NPU power islands: for a PVTPLL rate the firmware programs NPU GRF registers inside the islands, for the 200 MHz boot rate it only switches CRU selectors to GPLL (
clk_npu_set_rate()inplat/rockchip/rk3588/drivers/scmi/rk3588_clk.c, TF-A v2.12.0, which Nicolas pointed me to). An island powered up while the clock is above the boot rate never acknowledges the power-on, and the next register access into it takes an asynchronous SError. Returning to the boot rate is safe at any time; raising the clock is not, so while it is above the boot rate every core is held runtime resumed and no island can transition. Unbinding any core takes devfreq down until all are bound again.The firmware accepts only the nine rates of its
rk3588_npu_pvtpll_table, 300 to 1000 MHz in 100 MHz steps plus 200 MHz, and refuses any other. The refusal never reaches the caller:clk_change_rate()ignores what ->set_rate() returns, soclk_set_rate()reports success and the clock stays put. Hence patch 7 names exactly those nine.The table is on all three cores with opp-shared (Nicolas's review); the one devfreq device sits on
rknn_core_0, the lowest devicetree index. Patch 9 explains the maximum over the cores and why runtime suspend does not calldevfreq_suspend_device().Testing
-------Orange Pi 5 Plus, 7.3.0-rc2 (drm-misc-next
3afbeb6b99dd) plus this series, in-tree rocket, BL31 v2.12.0-10-g70d814213: the edk2-rk3588 TF-A branch with upstreamd2d6928641ba, so that it reports the PVTPLL rate it measures. MobileNetV1 through Teflon, 3 x 20 s per arm, one thread pinned to one CPU, CPUs on the performance governor, a bit-exact oracle on every inference,polling_intervalat the driver's 50 ms, core 0's interrupt on a CPU with its deep idle state disabled.
cur_freqis only the driver's request andscmi_clk_npuon the GPLL path echoes the last accepted rate, so the CRU selector is the witness (CLKSEL_CON74bit 0 = PVTPLL,CLKSEL_CON73= GPLL divider), read once a second:200 MHz OPP, userspace 91.15 inf/s CRU GPLL/6 = 198 MHz, 700 mV 1000 MHz OPP, userspace 234.45 inf/s CRU PVTPLL (BL31 1 GHz), 850 mV simple_ondemand 232.88 inf/s OPPs 200-1000 MHz 200 MHz OPP again 90.87 inf/s drift 0.997That is 2.57x pinned and 2.55x under the governor, 0.7 % below pinned; the oracle and the interrupts per inference were the same in all four arms.
Three clients, each with its own oracle, all three cores computing together, output bit-exact and identical to a single-client control:
OPP one client three clients total 900 MHz, 800 mV 234.42 199.33 + 198.95 + 199.19 597.47 inf/s 1000 MHz, 850 mV 240.49 203.16 + 203.01 + 203.46 609.63 inf/sAfter 25 s of load the governor is at the 200 MHz OPP from the first sample (0.24 s apart) through the 60 s idle window: CRU 198 MHz in all 60 one-second samples, rail 700 mV. The islands were power-cycled five times at 700 mV and powered down once more after a raise to 1000 MHz. All six bind orders give bit-exact output, with devfreq and cooling back on
rknn_core_0after every rebind.Unbind and reload: 25 rounds of all three cores, 4 of a single core with an inference while it was gone, 5 rmmod rounds, and 3 unbinds plus 1 rmmod with the clock raised (CRU on the PVTPLL before each). Without patch 4 the single-core round oopses, as its commit message shows.
On a KASAN, kmemleak and
PROVE_LOCKINGbuild of the same tree the unbind (no client submitting), reload, three-client and island tests gave no report and kmemleak nothing from this driver, and a lockdep run through the governor, cooling, three clients, and unbind and rmmod with the clock raised to the 1 GHz OPP left lockdep enabled.Build: the series was rebased onto drm-misc-next
8ef59ee79407to send it; apart from one comment corrected in 9 (rocket_devfreq.o is byte-identical), the patches are the ones tested above. On that base, arm64, gcc 13.3: each patch that touchesdrivers/accel/rocketbuilds with W=1 and no warning, and sparse reports nothing indrivers/accel/rocket; 9, 10 and 11 also build with W=1 and no warning withCONFIG_DEVFREQ_THERMAL=n.dt_binding_checkat 6 and 11, andCHECK_DTBSon rk3588-orangepi-5-plus, rk3588s-orangepi-5 and rk3588-edgeble-neu6b-io at the base, 7 and 11, give no schema warning. A full Image, modules and dtbs build at 11 completes with no warning fromdrivers/accel/rocket. checkpatch --strict flags only the 75-column oops line in 4 andMAINTAINERSfor the new files in 9, which
F:drivers/accel/rocket/covers.Two corrections to my reply of 9 September [3]: the supply could have come down to the 200 MHz voltage even had the clock change failed, since the refusal is not reported, so the CRU selector is the witness now; and the v1 governor arm was the governor running, not limits pinned by hand.
Not done
--------- Unbinding a core while clients submit is still unsafe. Patch 3 notes that an open file keeps an entity on the departed core's scheduler; on the KASAN build (three clients running, then as root "echo fdac0000.npu >
/sys/bus/platform/drivers/rocket/unbind") that is a use-after-free:BUG: KASAN: slab-use-after-free in _raw_spin_lock+0xb4/0x118 drm_sched_rq_remove_entity+0x94/0x2b0 [gpu_sched] drm_sched_entity_select_rq+0x1f4/0x698 [gpu_sched] drm_sched_job_arm+0x50/0x258 [gpu_sched] rocket_job_push+0x2f8/0x6b8 [rocket] rocket_ioctl_submit+0x888/0xd28 [rocket] Freed by: drm_sched_fini <- rocket_job_fini <- rocket_core_fini <- rocket_removethen a soft lockup:
drm_sched_fini()frees the run queues while the entity still points at one. drm-misc-next has the same path (read, not run):rocket_job_open()builds the entity over every core, androcket_remove()ignores open files. The fixes I see are policy,suppress_bind_attrs(undoing patch 4) or unplugging the DRM device when any core goes. Tomeu, which would you prefer? I will write it.
- Probe and remove are not serialised against open, and a file opened while a core probes can see its scheduler uninitialised; both predate this series.
- Lifecycle 1/2 [2] (a devres leak that moves the accel minor up after every full unbind and rebind, invisible to kmemleak) builds on Guangshuo Li's "accel/rocket: clear rdev on device init failure" [4], which is not in drm-misc-next. Sashiko's other pre-existing findings on the standalone slot-search patch, which I agreed with [5], stand unless patch 4 covers them.
- Not exercised: a devicetree without the OPP table or without assigned-clock-rates (code-read only), throttling at 85 degrees (not reachable here), system suspend (s2idle does not resume on this board, for reasons outside the NPU), kexec from a raised clock, and any other board. No milliwatt figure, no energy model.Routing
-------Patches 1-4 are fixes with Cc: stable and 9 builds on 1, 2 and 4, so the simplest route is all but the two dts patches through drm-misc-next. The binding (6) must land before those (7 and 11, Heiko's tree), which I can resend once the rest is in. 1-3 supersede their standalone postings (links under each ---); 5 is also 01/14 of Jiaxing Hu's RK3576 series, and whichever lands first, the other drops it. I have not rebased onto that series.
Questions
---------Q1. DT maintainers: is opp-shared on all three cores the right
description of one clock and one supply? v2 leaves opp-suspend out; Nicolas found it "not 100% fit" for 200 MHz but not unfit either, and asked for your view [6].
Q2. Maximum over the cores, or a summed busy count? And no OPP table
means no devfreq and no error, as in panfrost and lima: the policy you want?
Q3. assigned-clock-rates stays on all three nodes. Jiaxing reports that
on the RK3576 it hangs the board before the console when set on the SCMI clock, and that the vendor driver never writes that rate from DT [7]. I have not booted this board without it. Keep it, or drop it in a patch on top?Nicolas also floated the component framework; I have not tried it.
Credits
-------Nicolas Dufresne: review, opp-shared, the TF-A pointer, and a proof of concept with the same rates and voltages. Tomeu Vizoso agreed to the full-range table [8]. Jiaxing Hu: the RK3576 multi-core corruption the three-client test answers, a test and a review. Sidong Yang: the report behind patch 3 and a test.
Jonas Karlman pointed me at his GPU approach, a .config_clks() that checks
pm_runtime_suspended()[9][10], and warned that "just readingclk_summaryto read SCMI clock rate can issue SError or a freeze" [11]. From the code, not a test: ->get_cur_freq() returns the last request, probe reads the boot rate with the domain on, and with the in-tree devicetree everyset_ratethat reaches the firmware, re-read included, runs with the cores held.Hüseyin BIYIK wrote TF-A commit
d2d6928641ba, which makes BL31 report the PVTPLL rate it measures rather than the one asked for, and guessed early that the clock framework ignores what SCMI answers [12]; for a refusedset_rateit does.The Assisted-by: LLM tags are Claude (Fable 5.1 and Opus 5; the v1 cover named only Opus 5, though Fable 5.1 worked on v1 too). We wrote the code and these messages together, including the v3 changes to patch 4; it also found the silent refusal with me in the clock, OPP and TF-A code, wrote and adversarially reviewed the test scripts, and analysed the crash patch 4 fixes and the one under Not done. I threw out plenty of what it proposed. The board, every boot and measurement, the decision to send this and the responsibility for it are mine.
[1] https://lore.kernel.org/r/CAPsqS2QXQda2ftbEJb_-Qeva9ay4+4ysdpUtj8jAvbyAsYiAJw@mail.gmail.com
[2] https://lore.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lore.kernel.org/r/[email protected]
[5] https://lore.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/[email protected]
[7] https://lore.kernel.org/r/[email protected]
[8] https://lore.kernel.org/r/CAPsqS2RRUyG1vz5Nm7990yd0hcKR5E-SWhbt0ishaVYvCqQftw@mail.gmail.com
[9] https://lore.kernel.org/r/[email protected]
[10] https://github.com/Kwiboo/linux-rockchip/commits/next-20260619-rk35xx-scmi-gpu-clk/
[11] https://lore.kernel.org/r/[email protected]
[12] https://lore.kernel.org/r/[email protected]Igor Paunovic (11):
accel/rocket: search every core slot when a core is removed accel/rocket: number the cores by devicetree position, not bind order accel/rocket: search every core slot when looking up a scheduler accel/rocket: keep core slots stable across unbind and rebind accel/rocket: request the core clocks by name
dt-bindings: npu: rockchip: allow DVFS and thermal properties
arm64: dts: rockchip: rk3588: add an OPP table for the NPU accel/rocket: restore the NPU clock boot rate before powering the
cores down
accel/rocket: add devfreq support
accel/rocket: register a devfreq cooling device
arm64: dts: rockchip: rk3588: add passive cooling to the NPU thermal
zone.../npu/rockchip,rk3588-rknn-core.yaml | 10 + arch/arm64/boot/dts/rockchip/rk3588-base.dtsi | 17 +- arch/arm64/boot/dts/rockchip/rk3588-opp.dtsi | 54 ++ drivers/accel/rocket/Kconfig | 2 + drivers/accel/rocket/Makefile | 1 + drivers/accel/rocket/rocket_core.c | 14 + drivers/accel/rocket/rocket_core.h | 16 + drivers/accel/rocket/rocket_devfreq.c | 525 ++++++++++++++++++ drivers/accel/rocket/rocket_devfreq.h | 67 +++ drivers/accel/rocket/rocket_device.c | 2 + drivers/accel/rocket/rocket_device.h | 21 + drivers/accel/rocket/rocket_drv.c | 192 ++++++- drivers/accel/rocket/rocket_job.c | 47 +- 13 files changed, 945 insertions(+), 23 deletions(-) create mode 100644 drivers/accel/rocket/rocket_devfreq.c create mode 100644 drivers/accel/rocket/rocket_devfreq.hbase-commit:
8ef59ee794076e2b58cff357b12de2ba5d441271
Quoted 10 lines · Nicolas Dufresne
In future series or update, can you use common formatting in the future, I find your changelog hard to read. Do something like:
Since v1:
- Changed patch X with action Z
- Fix blabla
- ...Its actually good to be more concise in your communication, "wall of text" are difficult to cope with for maintainers.
Thanks, noted. The v3 cover will carry a "Since v2:" list, one line per change, and will be shorter overall.
Igor
Tomeu Vizoso
maintainerQuoted 18 lines · Nicolas Dufresne
Quoted 5 lines · Igor Paunovic
The rocket driver runs the NPU at whatever rate the devicetree pinned it to: 200 MHz on the RK3588, out of the 1 GHz the hardware reaches. This series adds devfreq, the OPP table and the thermal plumbing that go with it.
Quoted 7 lines
v1: https://lore.kernel.org/r/[email protected]
As Tomeu asked [1], the slot-search fix now leads the series, with the two rocket fixes this builds on and, as patch 4, 2/2 of my July lifecycle series [2], without which v2 oopsed when one core was unbound, bound and unbound again. KASAN found that v1 4/7 (now 8) kept a clk handle that unbinding freed; the notes under each --- list the rest.
In future series or update, can you use common formatting in the future, I find your changelog hard to read. Do something like:
Quoted 7 lines
Since v1:
- Changed patch X with action Z
- Fix blabla
- ...Its actually good to be more concise in your communication, "wall of text" are difficult to cope with for maintainers.
I will go a bit further and recommend you to not let the LLM write any text at all that is to be consumed by humans. If you don't feel confident in your English, you can ask it to correct and improve your text. Or write it in your mother language and ask it to translate.
Otherwise, if I need to make sense of what the LLM wrote (and they will happily write a lot!), then I need to find big slots of time to go over this kind of submissions, which is difficult for me now.
Regards,
Tomeu
Quoted 246 lines
cheers,
NicolasQuoted 243 lines
Hardware constraint
-------------------The three cores share one clock and one supply. The clock comes from a PVTPLL inside the NPU power islands: for a PVTPLL rate the firmware programs NPU GRF registers inside the islands, for the 200 MHz boot rate it only switches CRU selectors to GPLL (
clk_npu_set_rate()inplat/rockchip/rk3588/drivers/scmi/rk3588_clk.c, TF-A v2.12.0, which Nicolas pointed me to). An island powered up while the clock is above the boot rate never acknowledges the power-on, and the next register access into it takes an asynchronous SError. Returning to the boot rate is safe at any time; raising the clock is not, so while it is above the boot rate every core is held runtime resumed and no island can transition. Unbinding any core takes devfreq down until all are bound again.The firmware accepts only the nine rates of its
rk3588_npu_pvtpll_table, 300 to 1000 MHz in 100 MHz steps plus 200 MHz, and refuses any other. The refusal never reaches the caller:clk_change_rate()ignores what ->set_rate() returns, soclk_set_rate()reports success and the clock stays put. Hence patch 7 names exactly those nine.The table is on all three cores with opp-shared (Nicolas's review); the one devfreq device sits on
rknn_core_0, the lowest devicetree index. Patch 9 explains the maximum over the cores and why runtime suspend does not calldevfreq_suspend_device().Testing
-------Orange Pi 5 Plus, 7.3.0-rc2 (drm-misc-next
3afbeb6b99dd) plus this series, in-tree rocket, BL31 v2.12.0-10-g70d814213: the edk2-rk3588 TF-A branch with upstreamd2d6928641ba, so that it reports the PVTPLL rate it measures. MobileNetV1 through Teflon, 3 x 20 s per arm, one thread pinned to one CPU, CPUs on the performance governor, a bit-exact oracle on every inference,polling_intervalat the driver's 50 ms, core 0's interrupt on a CPU with its deep idle state disabled.
cur_freqis only the driver's request andscmi_clk_npuon the GPLL path echoes the last accepted rate, so the CRU selector is the witness (CLKSEL_CON74bit 0 = PVTPLL,CLKSEL_CON73= GPLL divider), read once a second:200 MHz OPP, userspace 91.15 inf/s CRU GPLL/6 = 198 MHz, 700 mV 1000 MHz OPP, userspace 234.45 inf/s CRU PVTPLL (BL31 1 GHz), 850 mV simple_ondemand 232.88 inf/s OPPs 200-1000 MHz 200 MHz OPP again 90.87 inf/s drift 0.997That is 2.57x pinned and 2.55x under the governor, 0.7 % below pinned; the oracle and the interrupts per inference were the same in all four arms.
Three clients, each with its own oracle, all three cores computing together, output bit-exact and identical to a single-client control:
OPP one client three clients total 900 MHz, 800 mV 234.42 199.33 + 198.95 + 199.19 597.47 inf/s 1000 MHz, 850 mV 240.49 203.16 + 203.01 + 203.46 609.63 inf/sAfter 25 s of load the governor is at the 200 MHz OPP from the first sample (0.24 s apart) through the 60 s idle window: CRU 198 MHz in all 60 one-second samples, rail 700 mV. The islands were power-cycled five times at 700 mV and powered down once more after a raise to 1000 MHz. All six bind orders give bit-exact output, with devfreq and cooling back on
rknn_core_0after every rebind.Unbind and reload: 25 rounds of all three cores, 4 of a single core with an inference while it was gone, 5 rmmod rounds, and 3 unbinds plus 1 rmmod with the clock raised (CRU on the PVTPLL before each). Without patch 4 the single-core round oopses, as its commit message shows.
On a KASAN, kmemleak and
PROVE_LOCKINGbuild of the same tree the unbind (no client submitting), reload, three-client and island tests gave no report and kmemleak nothing from this driver, and a lockdep run through the governor, cooling, three clients, and unbind and rmmod with the clock raised to the 1 GHz OPP left lockdep enabled.Build: the series was rebased onto drm-misc-next
8ef59ee79407to send it; apart from one comment corrected in 9 (rocket_devfreq.o is byte-identical), the patches are the ones tested above. On that base, arm64, gcc 13.3: each patch that touchesdrivers/accel/rocketbuilds with W=1 and no warning, and sparse reports nothing indrivers/accel/rocket; 9, 10 and 11 also build with W=1 and no warning withCONFIG_DEVFREQ_THERMAL=n.dt_binding_checkat 6 and 11, andCHECK_DTBSon rk3588-orangepi-5-plus, rk3588s-orangepi-5 and rk3588-edgeble-neu6b-io at the base, 7 and 11, give no schema warning. A full Image, modules and dtbs build at 11 completes with no warning fromdrivers/accel/rocket. checkpatch --strict flags only the 75-column oops line in 4 andMAINTAINERSfor the new files in 9, which
F:drivers/accel/rocket/covers.Two corrections to my reply of 9 September [3]: the supply could have come down to the 200 MHz voltage even had the clock change failed, since the refusal is not reported, so the CRU selector is the witness now; and the v1 governor arm was the governor running, not limits pinned by hand.
Not done
--------- Unbinding a core while clients submit is still unsafe. Patch 3 notes that an open file keeps an entity on the departed core's scheduler; on the KASAN build (three clients running, then as root "echo fdac0000.npu >
/sys/bus/platform/drivers/rocket/unbind") that is a use-after-free:BUG: KASAN: slab-use-after-free in _raw_spin_lock+0xb4/0x118 drm_sched_rq_remove_entity+0x94/0x2b0 [gpu_sched] drm_sched_entity_select_rq+0x1f4/0x698 [gpu_sched] drm_sched_job_arm+0x50/0x258 [gpu_sched] rocket_job_push+0x2f8/0x6b8 [rocket] rocket_ioctl_submit+0x888/0xd28 [rocket] Freed by: drm_sched_fini <- rocket_job_fini <- rocket_core_fini <- rocket_removethen a soft lockup:
drm_sched_fini()frees the run queues while the entity still points at one. drm-misc-next has the same path (read, not run):rocket_job_open()builds the entity over every core, androcket_remove()ignores open files. The fixes I see are policy,suppress_bind_attrs(undoing patch 4) or unplugging the DRM device when any core goes. Tomeu, which would you prefer? I will write it.
- Probe and remove are not serialised against open, and a file opened while a core probes can see its scheduler uninitialised; both predate this series.
- Lifecycle 1/2 [2] (a devres leak that moves the accel minor up after every full unbind and rebind, invisible to kmemleak) builds on Guangshuo Li's "accel/rocket: clear rdev on device init failure" [4], which is not in drm-misc-next. Sashiko's other pre-existing findings on the standalone slot-search patch, which I agreed with [5], stand unless patch 4 covers them.
- Not exercised: a devicetree without the OPP table or without assigned-clock-rates (code-read only), throttling at 85 degrees (not reachable here), system suspend (s2idle does not resume on this board, for reasons outside the NPU), kexec from a raised clock, and any other board. No milliwatt figure, no energy model.Routing
-------Patches 1-4 are fixes with Cc: stable and 9 builds on 1, 2 and 4, so the simplest route is all but the two dts patches through drm-misc-next. The binding (6) must land before those (7 and 11, Heiko's tree), which I can resend once the rest is in. 1-3 supersede their standalone postings (links under each ---); 5 is also 01/14 of Jiaxing Hu's RK3576 series, and whichever lands first, the other drops it. I have not rebased onto that series.
Questions
---------Q1. DT maintainers: is opp-shared on all three cores the right
description of one clock and one supply? v2 leaves opp-suspend out; Nicolas found it "not 100% fit" for 200 MHz but not unfit either, and asked for your view [6].
Q2. Maximum over the cores, or a summed busy count? And no OPP table
means no devfreq and no error, as in panfrost and lima: the policy you want?
Q3. assigned-clock-rates stays on all three nodes. Jiaxing reports that
on the RK3576 it hangs the board before the console when set on the SCMI clock, and that the vendor driver never writes that rate from DT [7]. I have not booted this board without it. Keep it, or drop it in a patch on top?Nicolas also floated the component framework; I have not tried it.
Credits
-------Nicolas Dufresne: review, opp-shared, the TF-A pointer, and a proof of concept with the same rates and voltages. Tomeu Vizoso agreed to the full-range table [8]. Jiaxing Hu: the RK3576 multi-core corruption the three-client test answers, a test and a review. Sidong Yang: the report behind patch 3 and a test.
Jonas Karlman pointed me at his GPU approach, a .config_clks() that checks
pm_runtime_suspended()[9][10], and warned that "just readingclk_summaryto read SCMI clock rate can issue SError or a freeze" [11]. From the code, not a test: ->get_cur_freq() returns the last request, probe reads the boot rate with the domain on, and with the in-tree devicetree everyset_ratethat reaches the firmware, re-read included, runs with the cores held.Hüseyin BIYIK wrote TF-A commit
d2d6928641ba, which makes BL31 report the PVTPLL rate it measures rather than the one asked for, and guessed early that the clock framework ignores what SCMI answers [12]; for a refusedset_rateit does.The Assisted-by: LLM tags are Claude (Fable 5.1 and Opus 5; the v1 cover named only Opus 5, though Fable 5.1 worked on v1 too). We wrote the code and these messages together, including the v3 changes to patch 4; it also found the silent refusal with me in the clock, OPP and TF-A code, wrote and adversarially reviewed the test scripts, and analysed the crash patch 4 fixes and the one under Not done. I threw out plenty of what it proposed. The board, every boot and measurement, the decision to send this and the responsibility for it are mine.
[1] https://lore.kernel.org/r/CAPsqS2QXQda2ftbEJb_-Qeva9ay4+4ysdpUtj8jAvbyAsYiAJw@mail.gmail.com
[2] https://lore.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]
[4] https://lore.kernel.org/r/[email protected]
[5] https://lore.kernel.org/r/[email protected]
[6] https://lore.kernel.org/r/[email protected]
[7] https://lore.kernel.org/r/[email protected]
[8] https://lore.kernel.org/r/CAPsqS2RRUyG1vz5Nm7990yd0hcKR5E-SWhbt0ishaVYvCqQftw@mail.gmail.com
[9] https://lore.kernel.org/r/[email protected]
[10] https://github.com/Kwiboo/linux-rockchip/commits/next-20260619-rk35xx-scmi-gpu-clk/
[11] https://lore.kernel.org/r/[email protected]
[12] https://lore.kernel.org/r/[email protected]Igor Paunovic (11):
accel/rocket: search every core slot when a core is removed accel/rocket: number the cores by devicetree position, not bind order accel/rocket: search every core slot when looking up a scheduler accel/rocket: keep core slots stable across unbind and rebind accel/rocket: request the core clocks by name
dt-bindings: npu: rockchip: allow DVFS and thermal properties
arm64: dts: rockchip: rk3588: add an OPP table for the NPU accel/rocket: restore the NPU clock boot rate before powering the
cores down
accel/rocket: add devfreq support
accel/rocket: register a devfreq cooling device
arm64: dts: rockchip: rk3588: add passive cooling to the NPU thermal
zone.../npu/rockchip,rk3588-rknn-core.yaml | 10 + arch/arm64/boot/dts/rockchip/rk3588-base.dtsi | 17 +- arch/arm64/boot/dts/rockchip/rk3588-opp.dtsi | 54 ++ drivers/accel/rocket/Kconfig | 2 + drivers/accel/rocket/Makefile | 1 + drivers/accel/rocket/rocket_core.c | 14 + drivers/accel/rocket/rocket_core.h | 16 + drivers/accel/rocket/rocket_devfreq.c | 525 ++++++++++++++++++ drivers/accel/rocket/rocket_devfreq.h | 67 +++ drivers/accel/rocket/rocket_device.c | 2 + drivers/accel/rocket/rocket_device.h | 21 + drivers/accel/rocket/rocket_drv.c | 192 ++++++- drivers/accel/rocket/rocket_job.c | 47 +- 13 files changed, 945 insertions(+), 23 deletions(-) create mode 100644 drivers/accel/rocket/rocket_devfreq.c create mode 100644 drivers/accel/rocket/rocket_devfreq.hbase-commit:
8ef59ee794076e2b58cff357b12de2ba5d441271
Quoted 4 lines · Tomeu Vizoso
I will go a bit further and recommend you to not let the LLM write any text at all that is to be consumed by humans. If you don't feel confident in your English, you can ask it to correct and improve your text. Or write it in your mother language and ask it to translate.
Thank you, Tomeu and Nicolas.
Yes, it is true that reading and writing in English is hard for me (speaking is a little better than reading and writing). I understood your message, and thank you for the advice. I have already applied it to this reply: I wrote it in my mother language, and an LLM translated it into English and corrected it.
In future replies I will always say when an LLM or Google has translated my text and corrected my grammar and spelling mistakes.
Thank you, and kind regards,
Igor Paunovic