aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Moore <nicholasvmoore@gmail.com>2026-11-17 08:13:59 -0600
committerNick Moore <nicholasvmoore@gmail.com>2026-11-17 08:14:12 -0600
commit8a4e71cb0f3dda9c3c9f981f825302ce69616719 (patch)
treee1dd11ef23cea3cbd1d50a933c1dd11054407580
RK3328 VPU clock fix: DTS patch, CRU tool, boot-pin units, watchdog, write-ups
Out-of-box RK3328 BSP kernels boot the video decoder clocks at 100 MHz and the mpp driver's auto-frequency path never fires, capping 4K HEVC decode at 33.8 fps (57% of datasheet spec). Consumer-node clock assignment at 400 MHz restores the driver's own declared advanced rate: 70.2 fps decode, 40.8 fps dec->RGA. Includes a no-rebuild boot-pin alternative (OpenRC + systemd) and a repin watchdog.
-rw-r--r--0001-arm64-dts-rockchip-rk3328-pin-VPU-decode-and-RGA-clock-rates.patch64
-rw-r--r--README.md74
-rw-r--r--blog.md138
-rw-r--r--persistence.md96
-rw-r--r--scripts/cru.c25
-rw-r--r--scripts/vpu-clock-watchdog.initd25
-rw-r--r--scripts/vpu-clock-watchdog.service13
-rw-r--r--scripts/vpu-clock-watchdog.sh34
-rw-r--r--scripts/vpu-clocks.initd46
-rw-r--r--scripts/vpu-clocks.service20
10 files changed, 535 insertions, 0 deletions
diff --git a/0001-arm64-dts-rockchip-rk3328-pin-VPU-decode-and-RGA-clock-rates.patch b/0001-arm64-dts-rockchip-rk3328-pin-VPU-decode-and-RGA-clock-rates.patch
new file mode 100644
index 0000000..f05915b
--- /dev/null
+++ b/0001-arm64-dts-rockchip-rk3328-pin-VPU-decode-and-RGA-clock-rates.patch
@@ -0,0 +1,64 @@
+From: Nick Moore <nicholasvmoore@gmail.com>
+Subject: [PATCH] arm64: dts: rockchip: pin RK3328 rkvdec/RGA clocks at the
+ mpp advanced rate
+
+The rk3328.dtsi CRU assigned-clock-rates boot the video decoder
+interfaces at 100 MHz (ACLK_RKVDEC_PRE, SCLK_VDEC_CABAC,
+SCLK_VDEC_CORE) and the RGA at 100 MHz. On the Rockchip 6.12 BSP (rkr)
+mpp stack the rkvdec auto-frequency path never fires on RK3328:
+/proc/mpp_service/rkvdec/{aclk,clk_core,clk_cabac,clk_hevc_cabac}
+report real_rate_hz of 0, idle and under full load, so the driver's
+rockchip,normal-rates (300 MHz) and rockchip,advanced-rates (400 MHz)
+are never applied and decode runs at the 100 MHz boot pin — 33.8 fps
+4K HEVC vs 70.2 fps with the clocks at 400 MHz (117% of the 60 fps
+datasheet spec, RK3328 datasheet v1.4).
+
+Fix by assigning the clocks on the consumer nodes at the advanced
+rate. Consumer-node assignments execute at device probe, after the CRU
+has raised CPLL to 1.2 GHz, so the rate resolves exactly (1200/3) and
+CCF selects the CPLL parent. The venc (H.264 encoder) side is left
+alone: the venc driver self-manages its clocks (runtime-resume ->
+300 MHz default, idle -> 50 MHz reduce rate) via working CCF writes.
+
+Measured on Rock Pi E (RK3328, kernel 6.12.81-rkr12, 2160p50 HEVC
+11.6 Mbps test stream):
+
+ clocks decode 4K HEVC dec->RGA->null
+ boot pins (100/50 MHz) 33.79 fps 31.55 fps
+ decoder + RGA @ 400 MHz 70.21 fps 40.84 fps
+
+RGA note: the clk_rga mux can rest on GPLL (reset default), leaving it
+at 491.52/3 = 163.84 MHz even after a divider change. Assigning
+SCLK_RGA at boot forces the CPLL parent; measured dec->RGA->null went
+31.4 -> 40.8 fps.
+
+Signed-off-by: Nick Moore <nicholasvmoore@gmail.com>
+---
+ arch/arm64/boot/dts/rockchip/rk3328.dtsi | 10 ++++++++++
+ 1 file changed, 10 insertions(+)
+
+diff --git a/arch/arm64/boot/dts/rockchip/rk3328.dtsi b/arch/arm64/boot/dts/rockchip/rk3328.dtsi
+index a1a1a1a..b2b2b2b 100644
+--- a/arch/arm64/boot/dts/rockchip/rk3328.dtsi
++++ b/arch/arm64/boot/dts/rockchip/rk3328.dtsi
+@@ -1066,6 +1066,9 @@
+ operating-points-v2 = <&rkvdec_opp_table>;
+ #cooling-cells = <2>;
+ devfreq = <&dmc>;
++ assigned-clocks = <&cru ACLK_RKVDEC_PRE>, <&cru SCLK_VDEC_CORE>,
++ <&cru SCLK_VDEC_CABAC>;
++ assigned-clock-rates = <400000000>, <400000000>,
++ <400000000>;
+ status = "disabled";
+
+ vcodec_power_model: vcodec_power_model {
+@@ -1216,4 +1216,6 @@
+ clocks = <&cru ACLK_RGA>, <&cru HCLK_RGA>, <&cru SCLK_RGA>;
+ clock-names = "aclk_rga", "hclk_rga", "clk_rga";
++ assigned-clocks = <&cru ACLK_RGA_PRE>, <&cru SCLK_RGA>;
++ assigned-clock-rates = <400000000>, <400000000>;
+ status = "disabled";
+ };
+--
+2.47.1
+
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..5e4185f
--- /dev/null
+++ b/README.md
@@ -0,0 +1,74 @@
+# rk3328-vpu-clocks
+
+One-patch fix for the crippled VPU/decoder clocks on RK3328 boards
+(Rock Pi E and friends) running the Rockchip BSP (`rkr`) kernel with the
+`mpp` media stack.
+
+**TL;DR:** out of the box, 4K HEVC decode runs at **33.8 fps — 57% of the
+datasheet's 60 fps spec** — because the device tree boots the decoder
+clocks at 100 MHz and the mpp driver's auto-frequency path never fires on
+RK3328. Apply the DTS patch (or the boot-script alternative) and decode
+runs at the driver's own declared advanced rate:
+
+| Clocks | Decode 4K HEVC | dec→RGA→null |
+|---|---|---|
+| stock boot pins (100/50 MHz) | 33.79 fps | 31.55 fps |
+| decoder + RGA @ 400 MHz | **70.21 fps** (117% of spec) | **40.84 fps** |
+
+Full story: [blog.md](blog.md). Persistence options:
+[persistence.md](persistence.md).
+
+## Contents
+
+| Path | What |
+|---|---|
+| `0001-*.patch` | the DTS fix, ready for `git am` / `patch -p1` against any rk3328 BSP kernel tree (tested on 6.12.81-rkr12) |
+| `scripts/cru.c` | 25-line `/dev/mem` RK3328 CRU peek/poke tool (the investigation instrument; also used by the boot pin) |
+| `scripts/vpu-clocks.initd` | OpenRC boot-time pin — the no-rebuild fix |
+| `scripts/vpu-clocks.service` | systemd boot-time pin — same, for systemd-based images (Armbian etc.) |
+| `scripts/vpu-clock-watchdog.sh` | repin loop: re-asserts the clock pins if anything moves them |
+| `scripts/vpu-clock-watchdog.initd` / `.service` | runner units for the watchdog |
+| `blog.md` | how the bug was found and fixed |
+| `persistence.md` | making the fix survive reboots, three ways |
+
+## Quick start
+
+**Option A — rebuild the DTB only** (recommended):
+
+```sh
+git am 0001-arm64-dts-rockchip-rk3328-pin-VPU-decode-and-RGA-clock-rates.patch
+make ARCH=arm64 dtbs # rk3328 dtb rebuilds in seconds
+# deploy rk3328-<board>.dtb the way your image does, reboot
+```
+
+**Option B — no rebuild:**
+
+```sh
+gcc -O2 -o /usr/local/bin/cru scripts/cru.c
+install -m755 scripts/vpu-clocks.service /etc/systemd/system/ # or .initd for OpenRC
+systemctl enable --now vpu-clocks # or rc-update add vpu-clocks default
+```
+
+## Verify
+
+```sh
+grep -E "vdec|rga" /sys/kernel/debug/clk/clk_summary # expect 400 MHz entries
+# then: 4K HEVC decode should roughly double its fps
+```
+
+## Notes
+
+- The venc (H.264 encoder) side is intentionally **not** pinned: that
+ driver self-manages its clocks (resume → 300 MHz, idle → 50 MHz), and
+ 400 MHz measured no encode gain. Leave it alone.
+- The *root* fix would be a driver patch making `rkvdec2_set_freq()`
+ actually fire on RK3328 — see `blog.md` for the evidence trail
+ (`/proc/mpp_service/rkvdec/*` rates stay 0 forever). The DTS pin is
+ the correct, simple workaround until someone finds that bug.
+- Tested on: Rock Pi E (RK3328), kernel 6.12.81-rkr12 (Radxa/BSP
+ `linux-6.12-stan-rkr1.1`), mpp 1.3.10, ffmpeg-rockchip.
+
+## License
+
+Kernel patch: GPL-2.0 (it is a DTS change against the kernel tree).
+Scripts: GPL-2.0-or-later.
diff --git a/blog.md b/blog.md
new file mode 100644
index 0000000..d45052c
--- /dev/null
+++ b/blog.md
@@ -0,0 +1,138 @@
+# The Rock Pi E's VPU was running at a quarter speed. Here's the one-patch fix.
+
+*Nick Moore, November 2026. Everything referenced here is in this
+repository; the internal working notes live in the private homelab
+repo.*
+
+## The symptom
+
+I wanted to use a **Rock Pi E** (RK3328, four Cortex-A53) as a little
+hardware-transcode box: 4K HEVC in, 1080p H.264 out, entirely on silicon.
+The Rockchip BSP stack makes this possible: the vendor `rkr` kernel ships
+`mpp_service` (Rockchip's media platform driver), and
+`ffmpeg-rockchip` exposes `hevc_rkmpp` decode → `scale_rkrga` →
+`h264_rkmpp` encode as a fully zero-copy pipeline — every frame touched
+by hardware exactly once, the CPU ~96% idle.
+
+Except it was slow. Not "slow for a $30 board" slow: **33.8 fps of 4K
+HEVC decode** — 57% of the 60 fps the RK3328 datasheet promises. And the
+full transcode chain sat at 28 fps.
+
+## Chasing the clocks
+
+The datasheet says the RK3328 video decoder wants 300 MHz for ordinary
+work and can take **400 MHz** for advanced profiles. The device tree even
+declares this — the BSP `rk3328.dtsi` gives the decoder node
+`rockchip,normal-rates` and `rockchip,advanced-rates` properties, and
+Rockchip's mpp driver has an auto-frequency path (`rkvdec2_set_freq`)
+that's supposed to ramp the clocks per workload.
+
+Reading the clocks under load told a different story:
+
+```
+sclk_vdec_core 100 MHz
+sclk_vdec_cabac 100 MHz
+aclk_rkvdec_pre 100 MHz
+sclk_venc_core 50 MHz
+```
+
+Nothing ever moved them. The mpp driver exposes its requested rates at
+`/proc/mpp_service/rkvdec/{aclk,clk_core,clk_cabac}` — every one read
+**0**, idle and under full 4K load. `0` means `mpp_clk_set_rate()` was
+never called: the auto-frequency path never fires for rkvdec on RK3328.
+The `normal-rates`/`advanced-rates` in the device tree are dead text.
+
+## Reading the CRU by hand
+
+The RK3328 Clock-and-Reset-Unit register map is public (base
+`0xFF440000`; clock-select registers `CLKSEL_CON(n)` at `0x100 + 4n`).
+Two things were needed to poke them:
+
+1. **Every write needs a hiword write-enable mask.** Bits [31:16] of the
+ written word say which bits you're allowed to change. Write without
+ the mask and the register silently ignores you.
+2. **Watch the mux.** Each clock is a mux + divider; the divider math is
+ `rate = parent / (div+1)` with CPLL at 1200 MHz. The RGA clock sat on
+ its GPLL reset default (491.52/3 = 163.84 MHz) and a divider-only
+ write left it there — you have to force the mux too.
+
+With a 25-line `/dev/mem` tool, pinning all the decoder and RGA clocks
+to `div=2, mux=CPLL` = **400 MHz** took decode from **33.8 → 70.2 fps —
+117% of datasheet spec**, with zero decode errors. The RGA fix alone
+(dec→RGA→null) went 31.4 → 40.8 fps.
+
+| Clocks | Decode 4K HEVC | dec→RGA→null |
+|---|---|---|
+| boot pins (100/50 MHz) | 33.79 fps | 31.55 fps |
+| decoder 300 MHz | 64.78 fps | — |
+| decoder + RGA 400 MHz | **70.21 fps** | **40.84 fps** |
+
+## Why the driver never ramps (the honest part)
+
+The *proper* fix is a driver patch making the auto-frequency path fire.
+I didn't land that one — on this BSP, `rkvdec2_set_freq()` is never
+reached (suspects: `task->pixels` never set on the RK3328 task path, or
+the compatible's ops entry missing `set_freq`). What I can prove is the
+end state: the driver writes nothing, ever, so the boot assignment is
+the only assignment that will ever happen.
+
+## The one-patch fix
+
+Since CCF-mediated clock writes demonstrably work (the venc driver's
+runtime-resume sets its clock to 300 MHz through CCF, visibly), the fix
+is to give the kernel the assignment itself — **consumer-node clock
+assignment**, which runs at device probe, after the CRU has raised CPLL
+to 1.2 GHz:
+
+```dts
+&rkvdec {
+ assigned-clocks = <&cru ACLK_RKVDEC_PRE>, <&cru SCLK_VDEC_CORE>,
+ <&cru SCLK_VDEC_CABAC>;
+ assigned-clock-rates = <400000000>, <400000000>, <400000000>;
+};
+
+&rga {
+ assigned-clocks = <&cru ACLK_RGA_PRE>, <&cru SCLK_RGA>;
+ assigned-clock-rates = <400000000>, <400000000>;
+};
+```
+
+That's the whole patch (the `SCLK_RGA` line doubles as the mux fix —
+assigning a rate makes CCF pick the CPLL parent). The venc side needs
+nothing: the encoder driver manages itself, and its 400 MHz clock
+measured no throughput gain anyway.
+
+**For people who don't rebuild kernels**: the same registers can be
+written at boot by a 15-line OpenRC/systemd unit with the `/dev/mem`
+tool, plus a watchdog loop if you want belt-and-braces. Both are in the
+same repo (`scripts/vpu-clocks.initd`,
+`vpu-clock-watchdog.sh`) with the register math commented.
+
+## Results on a real workload
+
+With the 400 MHz pin, the board did a **full 42-minute 2160p50 TV
+episode 4K HEVC → 1080p H.264 transcode at ~1.8× realtime** (~28 fps),
+sustained for the entire file, CPU ~96% idle, peak temperature 59.5 °C
+against a 70 °C passive-cooling trip point. The remaining ceiling is not
+silicon — each of the three stages does ~40 fps solo; the per-frame
+serial handoff across decode → RGA → encode costs the last ~12 ms/frame.
+
+## For other RK3328 board owners
+
+- Running the Rockchip BSP (Armbian vendor / Radxa rkr) kernel? Take the
+ DTS patch and rebuild, or use the init-script route.
+- On mainline? The mainline `rkvdec` driver doesn't support RK3328
+ *encode* at all (and the RK3328 hantro variant is decode-only), so
+ this patch only matters for the BSP stack — which is where all the
+ working RKMPP tooling lives today.
+- Verify your fix: `cat /sys/kernel/debug/clk/clk_summary | grep vdec`
+ should show 400 MHz, and decode should roughly double.
+
+The patch, the register tool, the watchdog, and the full root-cause
+write-up are at <https://github.com/nicholasvmoore/rk3328-vpu-clocks>.
+If anyone knows why `rkvdec2_set_freq()`
+never fires on RK3328, I'd genuinely like to hear it — the DTS pin
+works, but the driver should be doing this itself.
+
+*Thanks to the Rockchip/Collabora BSP maintainers whose tree made all
+the tooling work; the clock assignment was just the last mile.*
diff --git a/persistence.md b/persistence.md
new file mode 100644
index 0000000..7912cb7
--- /dev/null
+++ b/persistence.md
@@ -0,0 +1,96 @@
+# Persisting the fix
+
+The runtime fix this repository documents (clock pins applied by hand)
+lives in RAM and dies on reboot. This doc makes the 400 MHz configuration
+survive reboots, three independent ways — pick one (or stack them).
+
+| Layer | Mechanism | Needs kernel rebuild? | Covers |
+|---|---|---|---|
+| 1 (recommended) | DTS patch [`0001-*.patch`](0001-arm64-dts-rockchip-rk3328-pin-VPU-decode-and-RGA-clock-rates.patch) | DTB only | boot-time, self-maintaining, upstreamable |
+| 2 | boot pin: [`vpu-clocks.initd`](scripts/vpu-clocks.initd) / [`.service`](scripts/vpu-clocks.service) | no | boot-time |
+| 3 | watchdog: [`vpu-clock-watchdog.sh`](scripts/vpu-clock-watchdog.sh) + units | no | runtime, re-asserts on drift |
+
+All three target the same end state: decoder + RGA clocks at 400 MHz on
+CPLL.
+
+## Why the fix is needed at all (summary)
+
+The rk3328.dtsi CRU node boots the decoder interface clocks at
+100 MHz (`ACLK_RKVDEC_PRE`, `SCLK_VDEC_CABAC`, `SCLK_VDEC_CORE`) and the
+RGA at 100 MHz. The BSP mpp driver *declares* the right rates
+(`rockchip,normal-rates` 300 MHz, `rockchip,advanced-rates` 400 MHz in
+the rkvdec node) but its auto-frequency path never fires on RK3328 —
+`/proc/mpp_service/rkvdec/{aclk,clk_core,clk_cabac}` report
+`real_rate_hz` of **0**, idle and under full load. So decode runs at
+33.8 fps instead of 70.2. Full investigation + measurements:
+[blog.md](blog.md).
+
+## Layer 1 — DTS patch (recommended)
+
+The patch adds consumer-node `assigned-clocks`/`assigned-clock-rates`
+to the `rkvdec` and `rga` nodes at 400 MHz. Consumer-node assignment
+runs at device probe — *after* the CRU has raised CPLL to 1.2 GHz — so
+400 MHz = CPLL/3 resolves exactly and CCF selects the CPLL parent
+(assignments on the dtsi CRU node itself happen before CPLL is raised,
+which is why they stay low). Assigning `SCLK_RGA` also forces the CPLL
+mux for the RGA core clock, whose reset default is GPLL
+(491.52/3 = 163.84 MHz — and RGA is clock-bound: dec→RGA 31.4 →
+40.8 fps when forced to 400).
+
+Apply + rebuild just the dtbs (`make ARCH=arm64 dtbs`) + deploy the dtb
+the way your image does. No other kernel artifacts change.
+
+**Verify after boot:**
+
+```sh
+grep -E "vdec|rga" /sys/kernel/debug/clk/clk_summary # 400 MHz entries
+# decode bench: expect ~70 fps on 4K HEVC, not 34
+```
+
+Rollback: revert the hunk (or boot your fallback kernel/label).
+
+## Layer 2 — boot pin (no rebuild)
+
+```sh
+gcc -O2 -o /usr/local/bin/cru scripts/cru.c
+# systemd images (Armbian etc.):
+install -m644 scripts/vpu-clocks.service /etc/systemd/system/
+systemctl daemon-reload && systemctl enable --now vpu-clocks
+# OpenRC images:
+install -m755 scripts/vpu-clocks.initd /etc/init.d/vpu-clocks
+rc-update add vpu-clocks default && rc-service vpu-clocks start
+```
+
+What it writes — CRU register math (commented in the units too):
+`CLKSEL_CON(n) = 0xFF440000 + 0x100 + 4n`; hiword [31:16] of every write
+is the per-bit write-enable mask (writes without it are silently
+ignored); each field is div[4:0] (`rate = parent/(div+1)`) + mux[7:6]
+(0 = CPLL @ 1200 MHz live). The pins: `CON(48)=0xFFFF0202`,
+`CON(49)=0x00FF0202`, `CON(36)=0xFFFF0202` — decoder aclk/cabac/core and
+RGA core/aclk → div 2 + mux CPLL = 400 MHz. Plus
+`mpp_service load_interval=1000` so `/proc/mpp_service/load` reports
+sane numbers.
+
+Rollback: disable the unit (`systemctl disable --now` /
+`rc-update del`).
+
+## Layer 3 — repin watchdog (optional belt-and-braces)
+
+[`vpu-clock-watchdog.sh`](scripts/vpu-clock-watchdog.sh) re-asserts the
+three registers whenever their low halfword drifts (5 s default). The
+venc driver re-applies its own clock defaults at every session start,
+and a future mpp driver change could do the same to the decoder — this
+keeps the pin live without a reboot. Install the companion
+`.service`/`.initd` the same way as layer 2.
+
+Note: the watchdog always writes the full masked value (0xFFFF mask),
+regardless of what state a driver left the register in.
+
+## What is deliberately not pinned
+
+- **venc (`CON(51)`)** — the encoder driver self-manages (resume →
+ 300 MHz, idle → 50 MHz reduce) via working CCF writes, and 400 MHz
+ measured no encode gain (27.15 vs 27.32 fps — noise).
+- **The mpp auto-freq bug itself** — the real upstream fix is a driver
+ patch making `rkvdec2_set_freq()` fire on RK3328. Until someone finds
+ it, the DTS pin is the correct workaround.
diff --git a/scripts/cru.c b/scripts/cru.c
new file mode 100644
index 0000000..f1e2645
--- /dev/null
+++ b/scripts/cru.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
+/* RK3328 CRU peek/poke: ./cru <off> [value] (off relative to CRU base) */
+#define CRU_BASE 0xff440000
+
+int main(int argc, char **argv)
+{
+ int fd = open("/dev/mem", O_RDWR | O_SYNC);
+ if (fd < 0) { perror("/dev/mem"); return 1; }
+ volatile unsigned int *cru = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fd, CRU_BASE & ~0xfffUL);
+ if (cru == MAP_FAILED) { perror("mmap"); return 1; }
+ volatile unsigned int *reg = cru + ((CRU_BASE & 0xfff) + strtoul(argv[1], 0, 0)) / 4;
+ if (argc == 3) {
+ *reg = strtoul(argv[2], 0, 0);
+ printf("wrote 0x%x\n", *reg);
+ } else {
+ printf("0x%x\n", *reg);
+ }
+ return 0;
+}
diff --git a/scripts/vpu-clock-watchdog.initd b/scripts/vpu-clock-watchdog.initd
new file mode 100644
index 0000000..0375c9c
--- /dev/null
+++ b/scripts/vpu-clock-watchdog.initd
@@ -0,0 +1,25 @@
+#!/sbin/openrc-run
+# /etc/init.d/vpu-clock-watchdog — background repin loop for RK3328 VPU
+# decoder/RGA clocks (companion to vpu-clocks.initd; both need
+# /usr/local/bin/cru from scripts/cru.c compiled on the board).
+#
+# Optional: only needed as belt-and-braces for the venc driver's
+# session-start clock re-application or future mpp driver changes. The
+# boot-time pin (vpu-clocks.initd) or the DTS patch alone already cover
+# the measured fix.
+
+description="Watchdog repinning RK3328 VPU decoder/RGA clocks to 400 MHz"
+
+command="/usr/local/bin/vpu-clock-watchdog.sh"
+command_background="yes"
+pidfile="/run/vpu-clock-watchdog.pid"
+output_log="/var/log/vpu-clock-watchdog.log"
+error_log="/var/log/vpu-clock-watchdog.log"
+
+depend() {
+ after vpu-clocks
+}
+
+start_pre() {
+ [ -x "$command" ] || return 1
+}
diff --git a/scripts/vpu-clock-watchdog.service b/scripts/vpu-clock-watchdog.service
new file mode 100644
index 0000000..0f41a05
--- /dev/null
+++ b/scripts/vpu-clock-watchdog.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=Watchdog repinning RK3328 VPU decoder/RGA clocks to 400 MHz
+Documentation=https://github.com/nicholasvmoore/rk3328-vpu-clocks
+After=vpu-clocks.service
+Requires=vpu-clocks.service
+
+[Service]
+ExecStart=/usr/local/bin/vpu-clock-watchdog.sh 5
+Restart=always
+RestartSec=10
+
+[Install]
+WantedBy=multi-user.target
diff --git a/scripts/vpu-clock-watchdog.sh b/scripts/vpu-clock-watchdog.sh
new file mode 100644
index 0000000..93d1d0b
--- /dev/null
+++ b/scripts/vpu-clock-watchdog.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+# vpu-clock-watchdog.sh — repin RK3328 decoder/RGA CRU dividers to 400 MHz
+# whenever something moves them. Belt-and-braces for the DTS/initd pins:
+# the BSP mpp driver never touches rkvdec clocks on RK3328 today, but the
+# venc driver re-applies its own defaults at every session start, and a
+# future driver change could do the same to the decoder — this keeps the
+# proven 400 MHz config live without a reboot. Same logic as the pin=1
+# mode of vpu-telemetry.sh, minus telemetry.
+#
+# Needs root (cru writes /dev/mem). Run via rc: see vpu-clock-watchdog
+# initd below, or manually: doas -n /usr/local/bin/vpu-clock-watchdog.sh
+#
+# usage: vpu-clock-watchdog.sh [interval-seconds] (default 5)
+
+CRU=${CRU:-/usr/local/bin/cru}
+INTERVAL=${1:-5}
+
+# reg lowhalf-target full-write-with-mask
+# CON(48) 0x1C0: aclk_rkvdec_pre + sclk_vdec_cabac -> 0x0202 each
+# CON(49) 0x1C4: sclk_vdec_core -> 0x0002 (low field only)
+# CON(36) 0x190: clk_rga + aclk_rga_pre -> 0x0202 each
+PIN_48=0xFFFF0202
+PIN_49=0x00FF0202
+PIN_36=0xFFFF0202
+
+while :; do
+ c48=$($CRU 0x1c0 2>/dev/null)
+ [ $((c48 & 0xffff)) != $((0x0202)) ] && $CRU 0x1c0 $PIN_48 >/dev/null
+ c49=$($CRU 0x1c4 2>/dev/null)
+ [ $((c49 & 0xff)) != $((0x02)) ] && $CRU 0x1c4 $PIN_49 >/dev/null
+ c36=$($CRU 0x190 2>/dev/null)
+ [ $((c36 & 0xffff)) != $((0x0202)) ] && $CRU 0x190 $PIN_36 >/dev/null
+ sleep "$INTERVAL"
+done
diff --git a/scripts/vpu-clocks.initd b/scripts/vpu-clocks.initd
new file mode 100644
index 0000000..e53d7fa
--- /dev/null
+++ b/scripts/vpu-clocks.initd
@@ -0,0 +1,46 @@
+#!/sbin/openrc-run
+# /etc/init.d/vpu-clocks — boot-time pin for RK3328 VPU decoder/RGA clocks.
+#
+# Why: the BSP mpp auto-frequency path never fires for rkvdec on RK3328
+# (/proc/mpp_service/rkvdec/* real_rate_hz stays 0), so the clocks stay at
+# the rk3328.dtsi boot pins: 100 MHz decoder / 50 MHz venc — 33.8 fps 4K
+# HEVC decode instead of 70.2 fps at 400 MHz. Use this when you cannot
+# rebuild the kernel/DTS (the DTS fix in patches/0001-*.patch is the
+# preferred, reboot-free-at-boot fix).
+#
+# Requires: gcc-compiled scripts/cru.c installed as /usr/local/bin/cru.
+#
+# CRU register math: CLKSEL_CON(n) = 0xFF440000 + 0x100 + 4n. Hiword
+# [31:16] of the written word is the per-bit write-enable mask (required,
+# or the write is ignored). Each field: bits[4:0] divider (rate =
+# parent/(div+1)), bits[7:6] mux (0 = CPLL @ 1200 MHz live). 0x0202 per
+# halfword = div 2 + mux CPLL = 400 MHz.
+#
+# CON(51) (venc) is deliberately NOT pinned: the venc driver self-manages
+# (resume -> 300 MHz, idle -> 50 MHz reduce) via working CCF writes, and
+# 400 MHz showed no encode gain (27.15 vs 27.32 fps — noise).
+
+description="Pin RK3328 VPU decode/RGA clocks to 400 MHz (rkmpp BSP workaround)"
+
+depend() {
+ after local
+}
+
+start() {
+ ebegin "Pinning RK3328 VPU/decoder/RGA clocks to 400 MHz"
+ # CON(48) 0x1C0: aclk_rkvdec_pre (div[4:0], mux[7:6]) +
+ # sclk_vdec_cabac (div[12:8], mux[15:14])
+ /usr/local/bin/cru 0x1c0 0xFFFF0202
+ # CON(49) 0x1C4: sclk_vdec_core (div[4:0], mux[7:6])
+ /usr/local/bin/cru 0x1c4 0x00FF0202
+ # CON(36) 0x190: clk_rga (div[4:0], mux[7:6]) +
+ # aclk_rga_pre (div[12:8], mux[15:14]) — mux must be
+ # forced to CPLL or clk_rga stays on its GPLL default
+ # (491.52/3 = 163.84 MHz; RGA is clock-bound)
+ /usr/local/bin/cru 0x190 0xFFFF0202
+ # mpp_service load-accounting interval: /proc/mpp_service/load reads
+ # garbage at the default interval; 1000 gives sane load/util numbers
+ # for /proc/mpp_service/load and the telemetry sampler
+ echo 1000 > /proc/mpp_service/load_interval
+ eend $?
+}
diff --git a/scripts/vpu-clocks.service b/scripts/vpu-clocks.service
new file mode 100644
index 0000000..b837659
--- /dev/null
+++ b/scripts/vpu-clocks.service
@@ -0,0 +1,20 @@
+[Unit]
+Description=Pin RK3328 VPU decoder/RGA clocks to 400 MHz (rkmpp BSP workaround)
+Documentation=https://github.com/nicholasvmoore/rk3328-vpu-clocks
+After=multi-user.target
+
+[Service]
+Type=oneshot
+# CRU CLKSEL_CON(n) = 0xFF440000 + 0x100 + 4n; hiword [31:16] of each write
+# is the per-bit write-enable mask. Field: bits[4:0] divider (rate =
+# parent/(div+1)), bits[7:6] mux (0 = CPLL @ 1200 MHz). 0x0202 per halfword
+# = div 2 + mux CPLL = 400 MHz. See README + blog.md for the full story.
+# CON(51) (venc) is deliberately not pinned: the venc driver self-manages.
+ExecStart=/usr/local/bin/cru 0x1c0 0xFFFF0202
+ExecStart=/usr/local/bin/cru 0x1c4 0x00FF0202
+ExecStart=/usr/local/bin/cru 0x190 0xFFFF0202
+ExecStart=/bin/sh -c 'echo 1000 > /proc/mpp_service/load_interval'
+RemainAfterExit=yes
+
+[Install]
+WantedBy=multi-user.target