aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-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
6 files changed, 163 insertions, 0 deletions
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