From 0fa85ec72bcf3e86d78038b596b6f659c887ffcd Mon Sep 17 00:00:00 2001 From: Leandro Tavares de Melo Date: Sun, 14 Jun 2026 17:09:10 -0300 Subject: [PATCH] v4l2: built-in auto-boot via initcall ordering (sem service/self-init) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Faz o hi_v4l2 subir sozinho no boot quando built-in (CFG_HI_MSP_BUILDIN=y), sem serviço de userspace nem self-init do VDEC. A causa-raiz de o decoder falhar no boot (HI_DRV_VDEC_Open -> 0xffffffff) era ORDEM DE INITCALL: o hi_init.c (HI_DRV_LoadModules, init master do stack msp) roda em late_initcall (nivel 7) e faz VFMW/VDEC/VPSS *_DRV_ModInit; o nosso driver tambem usava late_initcall, mesmo nivel -> ordem por link -> corria antes -> VDEC_DRV_Open nao achava as funcoes de VFMW/VPSS (HI_DRV_MODULE_GetFunction) e caia no err. Correcoes (padrao dvb-hisi): - hi_v4l2_core.c: late_initcall_sync(hi_v4l2_init) INCONDICIONAL + module_exit (sob CONFIG MODULE os *_initcall viram module_init; built-in vira nivel 7s, que roda DEPOIS do nivel 7 do HI_DRV_LoadModules). Identico ao dvb-hisi. - hi_vdec_hal.c: global_init so faz HI_DRV_VDEC_Open (NAO HI_DRV_VDEC_Init -- o hi_init.c ja faz VDEC_DRV_ModInit); global_exit so HI_DRV_VDEC_Close (NAO DeInit -- nao destruir o modulo VDEC que o hi_init e dono). Open/Close sao refcontados (atmOpenCnt) -> coexiste com AVServer/AVPLAY. - Makefile: obj-$(HI_DRV_BUILDTYPE) (built-in quando CFG_HI_MSP_BUILDIN=y). Validado em HW (Hi3798Cv200, kernel #18): no boot, /dev/video0=decoder (8 coded formats) + /dev/video1=encoder (H264) automaticos; decode VP8 1080p 291 frames; encode NV12->H264 720p valido; coexistencia com AVServer ativo (dec+enc OK, sem derrubar o stack do AVServer, sem oops). Co-Authored-By: Claude Opus 4.8 (1M context) --- Makefile | 18 +++++++++--------- hi_v4l2_core.c | 13 ++++++++++++- hi_v4l2_dec.c | 6 ++++++ hi_vdec_hal.c | 15 +++++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 11eaf53..2eb4221 100644 --- a/Makefile +++ b/Makefile @@ -59,15 +59,15 @@ KBUILD_EXTRA_SYMBOLS += $(MSP_DIR)/drv/Module.symvers #=============================================================================== MOD_NAME := hi_v4l2 -# NOTE: unlike dvb-hisi (obj-$(HI_DRV_BUILDTYPE), built-in when CFG_HI_MSP_BUILDIN=y), -# hi_v4l2 is ALWAYS a loadable module (obj-m). It depends on the V4L2 mem2mem + -# videobuf2 framework (v4l2-mem2mem.ko, videobuf2-*.ko) which ship as kernel -# modules (=m) in this defconfig, so a built-in hi_v4l2 would fail to link -# (unresolved v4l2_m2m_*/vb2_* symbols). The HI_DRV_VDEC_*/HI_DRV_VENC_* symbols -# it also uses ARE in vmlinux (msp built-in), resolved via the kernel -# Module.symvers during `make modules`. The parent Makefile still reaches this -# dir through `obj-y += v4l2-hisi/`; this obj-m just makes the unit a module. -obj-m += $(MOD_NAME).o +# Built-in when CFG_HI_MSP_BUILDIN=y (HI_DRV_BUILDTYPE=y), like dvb-hisi. +# Works at boot via initcall ordering: hi_v4l2_core.c registers with +# late_initcall_sync (level 7s), which runs AFTER the msp boot init +# (hi_init.c HI_DRV_LoadModules @ late_initcall, level 7) that does the +# VFMW/VDEC/VPSS *_DRV_ModInit. So when our decoder probe opens the VDEC, the +# VFMW/VPSS functions it needs are already registered. We only HI_DRV_VDEC_Open +# (not Init/DeInit -- hi_init owns those), so we coexist with AVServer/AVPLAY. +# Deps built-in via the VIDEO_HISI_V4L2_DEPS Kconfig stub. Build w/ CONFIG_MSP=y. +obj-$(HI_DRV_BUILDTYPE) += $(MOD_NAME).o $(MOD_NAME)-y := hi_v4l2_core.o \ hi_v4l2_dec.o \ diff --git a/hi_v4l2_core.c b/hi_v4l2_core.c index cddbcb2..1dfc02c 100644 --- a/hi_v4l2_core.c +++ b/hi_v4l2_core.c @@ -43,7 +43,18 @@ static void __exit hi_v4l2_exit(void) hi_dec_unregister(); } -module_init(hi_v4l2_init); +/* + * Same idiom as dvb-hisi (drivers/msp/drv/dvb-hisi): late_initcall_sync works + * for both build kinds -- under CONFIG MODULE all *_initcall macros collapse to + * module_init (runs at insmod, stack already up); built-in it is initcall level + * 7s, which runs AFTER the msp boot init (hi_init.c HI_DRV_LoadModules @ + * late_initcall, level 7) that does VFMW/VDEC/VPSS *_DRV_ModInit. We must run + * after it: the decoder probe opens the VDEC (HI_DRV_VDEC_Open -> GetFunction + * of VFMW/VPSS) and probes caps (HI_DRV_VDEC_GetCap), which need those + * subsystems already registered. Plain late_initcall (level 7) raced + * HI_DRV_LoadModules by link order and lost (VDEC_DRV_Open -> 0xffffffff). + */ +late_initcall_sync(hi_v4l2_init); module_exit(hi_v4l2_exit); MODULE_DESCRIPTION("Hisilicon V4L2 mem2mem video codec (decoder + encoder)"); diff --git a/hi_v4l2_dec.c b/hi_v4l2_dec.c index 7ce35fd..c6ca5bc 100644 --- a/hi_v4l2_dec.c +++ b/hi_v4l2_dec.c @@ -1459,6 +1459,12 @@ static int hi_dec_probe(struct platform_device *pdev) mutex_init(&dev->dev_mutex); atomic_set(&dev->num_inst, 0); + /* + * Open the VDEC subsystem here (refcounted; coexists with AVServer). The + * msp boot init (hi_init.c) already did VDEC_DRV_ModInit, and our + * late_initcall_sync (hi_v4l2_core.c) runs after it, so the VFMW/VPSS + * functions the open needs are already registered. + */ ret = hi_vdec_hal_global_init(); if (ret) return ret; diff --git a/hi_vdec_hal.c b/hi_vdec_hal.c index e9b3cf6..60c1db0 100644 --- a/hi_vdec_hal.c +++ b/hi_vdec_hal.c @@ -161,6 +161,15 @@ int hi_vdec_hal_global_init(void) mutex_lock(&g_lock); if (g_open_refcnt == 0) { + /* + * Only OPEN the VDEC -- do NOT call HI_DRV_VDEC_Init. The msp boot + * init (hi_init.c HI_DRV_LoadModules) already does VDEC_DRV_ModInit + * (-> HI_DRV_VDEC_Init: module-register + bufmng + task); calling it + * again fights that single owner. We behave like the userspace MPI: + * just HI_DRV_VDEC_Open, which is refcounted (atmOpenCnt) and so + * coexists with AVServer/AVPLAY. This needs VFMW/VPSS already + * registered, hence late_initcall_sync ordering in hi_v4l2_core.c. + */ ret = HI_DRV_VDEC_Open(); if (ret != HI_SUCCESS) { pr_err("hi-v4l2: HI_DRV_VDEC_Open failed (0x%x)\n", ret); @@ -177,6 +186,12 @@ out: void hi_vdec_hal_global_exit(void) { mutex_lock(&g_lock); + /* + * Close only -- never HI_DRV_VDEC_DeInit: the VDEC subsystem is owned by + * the msp boot init (hi_init.c), not by us. DeInit would tear down the + * whole VDEC module out from under AVServer/AVPLAY and the rest of the + * stack. Close is the refcounted (atmOpenCnt) counterpart of our Open. + */ if (--g_open_refcnt == 0) HI_DRV_VDEC_Close(); if (g_open_refcnt < 0)