From 9a4f94ef751e9d5ef82d8051c6f20a45f0cd357a Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Wed, 18 Nov 2026 19:10:08 -0300 Subject: [PATCH 01/13] media: apple: ave: pin the T6000 and T8112 6070 command bytes T6000 and T8112 run the 6070.11.1 Start and Process commands that were qualified on hardware byte for byte. Nothing tests that those bytes stay the same when the shared 6070 writer changes for another chip. Add a KUnit case that builds Start and the first two Process commands for H.264 and HEVC, at 1280x720 30/1 and 1920x1080 30000/1001, with default session parameters, and compares their CRC32 with the values the builder produces at this commit. Assisted-by: Grok 4.7 Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- .../media/platform/apple/ave/ave-abi-test.c | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 57e659dc25f25..9f8d9d7469485 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -1496,6 +1496,68 @@ static void ave_t6041_text_boot_word(struct kunit *test) KUNIT_EXPECT_EQ(test, ave_fw_text_boot_crc(text, size, size - 2, &a), -EINVAL); } +/* + * T6000 and T8112 run the 6070 commands upstream qualified byte for byte. + * These CRCs pin the Start and first two Process commands the builder + * produces for them, so a change meant for another chip cannot alter + * those bytes unnoticed. + */ +static void ave_6070_qualified_bytes(struct kunit *test) +{ + static const struct { + enum ave_codec codec; + u32 width, height, fps_num, fps_den; + u32 start_crc, process_crc[2]; + } cases[] = { + { AVE_CODEC_H264, 1280, 720, 30, 1, + 0x0a894c73, { 0xbaecc863, 0x3da7ba31 } }, + { AVE_CODEC_H264, 1920, 1080, 30000, 1001, + 0xd87cabff, { 0x3147b186, 0xb60cc3d4 } }, + { AVE_CODEC_HEVC, 1280, 720, 30, 1, + 0x90dd39ba, { 0x27b4ba17, 0x852da142 } }, + { AVE_CODEC_HEVC, 1920, 1080, 30000, 1001, + 0x55b40bc0, { 0xd96b85f9, 0x7bf29eac } }, + }; + + for (unsigned int i = 0; i < ARRAY_SIZE(cases); i++) { + struct ave_dev *ave = kunit_kzalloc(test, sizeof(*ave), GFP_KERNEL); + struct ave_ctx *ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + struct ave_session *sess = kunit_kzalloc(test, sizeof(*sess), GFP_KERNEL); + enum ave_codec codec = cases[i].codec; + size_t start_len, process_len; + u8 *start, *process; + u32 crc; + + KUNIT_ASSERT_NOT_NULL(test, ave); + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_NOT_NULL(test, sess); + ave_test_6070_session(test, ave, ctx, sess, codec, cases[i].width, + cases[i].height); + sess->p.fps_num = cases[i].fps_num; + sess->p.fps_den = cases[i].fps_den; + start_len = ave->layout->bytes[codec - 1][AVE_IMAGE_START]; + process_len = ave->layout->bytes[codec - 1][AVE_IMAGE_PROCESS]; + start = kunit_kzalloc(test, start_len, GFP_KERNEL); + process = kunit_kzalloc(test, process_len, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, start); + KUNIT_ASSERT_NOT_NULL(test, process); + + KUNIT_ASSERT_EQ(test, ave_build_start(ctx, start, start_len), 0); + crc = crc32_le(~0, start, start_len); + KUNIT_EXPECT_EQ(test, crc, cases[i].start_crc); + for (unsigned int slot = 0; slot < 2; slot++) { + memset(process, 0, process_len); + KUNIT_ASSERT_EQ(test, + ave_build_process(ctx, process, process_len, + slot, 0x8f0000000ULL, + 4000000, false, false), + 0); + crc = crc32_le(~0, process, process_len); + KUNIT_EXPECT_EQ(test, crc, cases[i].process_crc[slot]); + } + } +} + static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_t6041_text_boot_word), KUNIT_CASE(ave_9003_byte_equality), KUNIT_CASE(ave_9003_hevc_main10), @@ -1504,6 +1566,7 @@ static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_6070_replies), KUNIT_CASE(ave_6070_frame_result), KUNIT_CASE(ave_6070_split_surfaces), KUNIT_CASE(ave_6070_start_process_fields), + KUNIT_CASE(ave_6070_qualified_bytes), KUNIT_CASE(ave_6070_reordered_pictures), KUNIT_CASE(ave_6070_hevc_contract), KUNIT_CASE(ave_6070_entropy_interleave), From dce6e177789b27ce7cb01bc92dd23ccddaf35b47 Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Wed, 18 Nov 2026 19:10:08 -0300 Subject: [PATCH 02/13] media: apple: ave: give each encoder its own debugfs directory Every instance creates the debugfs directory apple-ave. On a machine with two encoders the second one fails to create it and has no lifecycle_test, inject_wedge or last-command files. Name the directory apple-ave-, for example apple-ave-40d100000.video-codec, and update the two documents that give the old path. The path changes on every chip. Assisted-by: Grok 4.7 Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- Documentation/admin-guide/media/apple-ave.rst | 2 +- drivers/media/platform/apple/ave/abi.rst | 3 ++- drivers/media/platform/apple/ave/ave-drv.c | 11 +++++++---- drivers/media/platform/apple/ave/tests/README | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Documentation/admin-guide/media/apple-ave.rst b/Documentation/admin-guide/media/apple-ave.rst index 9b4571a02e14d..8f75be0db1abd 100644 --- a/Documentation/admin-guide/media/apple-ave.rst +++ b/Documentation/admin-guide/media/apple-ave.rst @@ -113,7 +113,7 @@ checkout, add ``--compare-baseline /path/to/old/drivers/media/platform/apple/ave Private capture comparisons must keep captured images outside the source tree and mask only addresses, session tokens, clocks and host bookkeeping fields. -With debugfs mounted, ``apple-ave/last_start_h264``, +With debugfs mounted, ``apple-ave-/last_start_h264``, ``last_start_hevc``, ``last_process0_h264`` and ``last_process0_hevc`` hold the last Start command built and the first Process command of the last session, for comparison with reference captures. diff --git a/drivers/media/platform/apple/ave/abi.rst b/drivers/media/platform/apple/ave/abi.rst index 72a440386df79..de72afa375dcd 100644 --- a/drivers/media/platform/apple/ave/abi.rst +++ b/drivers/media/platform/apple/ave/abi.rst @@ -39,7 +39,8 @@ Lifecycle-only validation ------------------------- Build with CONFIG_DEBUG_FS. On a supported 6070 device, write ``1`` (AVC) or ``2`` -(HEVC) to ``/sys/kernel/debug/apple-ave/lifecycle_test``. This synchronous operation +(HEVC) to ``/sys/kernel/debug/apple-ave-/lifecycle_test``, where +```` is the encoder's device name. This synchronous operation runs boot, Config, Open, Close and Halt, then requests runtime suspend. It does not allocate encoding surfaces or issue Start, Process or uninit for an encoder that was never initialized. A second write exercises a cold restart and DATA diff --git a/drivers/media/platform/apple/ave/ave-drv.c b/drivers/media/platform/apple/ave/ave-drv.c index 84810b03196bb..374146ebd1c2c 100644 --- a/drivers/media/platform/apple/ave/ave-drv.c +++ b/drivers/media/platform/apple/ave/ave-drv.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "ave.h" @@ -187,9 +188,9 @@ static const struct ave_variant ave_t8142 = { }; /* - * debugfs: apple-ave/last_start_{h264,hevc} hold the last Start built and - * last_process0_{h264,hevc} the first Process of the last session, so the - * generated commands can be compared with reference captures. + * debugfs: apple-ave-/last_start_{h264,hevc} hold the last Start + * built and last_process0_{h264,hevc} the first Process of the last session, + * so the generated commands can be compared with reference captures. */ void ave_debugfs_save(struct ave_dev *ave, enum ave_codec codec, enum ave_image kind, const void *cmd, size_t len) @@ -343,10 +344,12 @@ static void ave_debugfs_init(struct ave_dev *ave) { "last_start_h264", "last_process0_h264" }, { "last_start_hevc", "last_process0_hevc" }, }; + char name[64]; if (!IS_ENABLED(CONFIG_DEBUG_FS) || !ave->layout) return; - ave->debugfs = debugfs_create_dir("apple-ave", NULL); + scnprintf(name, sizeof(name), "apple-ave-%s", dev_name(ave->dev)); + ave->debugfs = debugfs_create_dir(name, NULL); if (ave->layout->abi == &ave_abi_6070) { debugfs_create_file("lifecycle_test", 0200, ave->debugfs, ave, &ave_lifecycle_fops); diff --git a/drivers/media/platform/apple/ave/tests/README b/drivers/media/platform/apple/ave/tests/README index 61098b32e8cf7..6f3de8bd486ba 100644 --- a/drivers/media/platform/apple/ave/tests/README +++ b/drivers/media/platform/apple/ave/tests/README @@ -76,7 +76,7 @@ Hardware procedure (requires a qualified firmware/SoC and an approved lane) enable the normal PM/genpd tracing available in the test kernel. Confirm a fresh AVC and HEVC encode can each complete and decode correctly. 2. Start a long encode with several OUTPUT and CAPTURE buffers queued. While - it runs, as root write 1 to /sys/kernel/debug/apple-ave/inject_wedge. + it runs, as root write 1 to /sys/kernel/debug/apple-ave-/inject_wedge. This software fault marks the current firmware broken and suppresses the recovery Halt attempt, modelling a command timeout without malformed DMA commands. It does not reproduce a real DART overrun or a firmware deadlock. From 851ad30ab202893f21e2a3aa3d0c13c1e9031258 Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Thu, 19 Nov 2026 14:37:01 -0300 Subject: [PATCH 03/13] media: apple: ave: read the firmware CRC and IOVA per encoder A boot loader that hands over two encoders cannot name both DATA images with the one /chosen aurora,ave-data-crc32 property, and the encoders share one TEXT reservation, so its iommu-addresses property carries one tuple per encoder. Read aurora,crc32 from each memory-region reservation and fall back to the /chosen property when it is absent. Accept several iommu-addresses tuples and use the one whose phandle is this device; a reservation with none, or with two for this device, is refused as before. Describe the reservation property in the admin guide, next to the /chosen ones. Every chip goes through this code. With today's boot loader (no aurora,crc32, one tuple per reservation) it does what it did before; only T6021 was run. Assisted-by: Grok 4.7 Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- Documentation/admin-guide/media/apple-ave.rst | 10 +++- drivers/media/platform/apple/ave/ave-fw.c | 60 +++++++++++++++++-- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/Documentation/admin-guide/media/apple-ave.rst b/Documentation/admin-guide/media/apple-ave.rst index 8f75be0db1abd..7f9a2c9084e6d 100644 --- a/Documentation/admin-guide/media/apple-ave.rst +++ b/Documentation/admin-guide/media/apple-ave.rst @@ -33,9 +33,13 @@ The encoder runs firmware on its own coprocessor. The bootloader loads it into two reserved, DART-mapped segments (TEXT and DATA) but does not start it. The Aurora Stage 2 loader passes both regions in the device's ``memory-region`` property and records their CRC32 under ``/chosen`` as -``aurora,ave-text-crc32`` and ``aurora,ave-data-crc32``. The driver only binds -while DATA still matches the recorded checksum, and keeps a copy of DATA that -it restores before every firmware boot. +``aurora,ave-text-crc32`` and ``aurora,ave-data-crc32``. The loader may also +record each segment's CRC32 as ``aurora,crc32`` on its reservation, as it +must when two encoders' DATA differ, and a segment shared by two encoders +has one ``iommu-addresses`` entry per encoder; the driver prefers the +reservation's value. The driver only binds while DATA still matches the +recorded checksum, and keeps a copy of DATA that it restores before every +firmware boot. No firmware files are needed. The driver builds every command itself, including the two large session commands (Start and Process), from the diff --git a/drivers/media/platform/apple/ave/ave-fw.c b/drivers/media/platform/apple/ave/ave-fw.c index 389f24f0f9ecf..f7ea9f53cdecd 100644 --- a/drivers/media/platform/apple/ave/ave-fw.c +++ b/drivers/media/platform/apple/ave/ave-fw.c @@ -62,13 +62,58 @@ static int ave_fw_chosen_crc(struct ave_dev *ave, const char *name, u32 *crc) return kstrtou32(str, 16, crc); } +/* + * A loader that hands over several encoders puts aurora,crc32 on each + * reservation: one /chosen DATA CRC cannot name two DATA images. Without + * it, the /chosen property is used as before. + */ +static int ave_fw_expect_crc(struct ave_dev *ave, unsigned int index, + const char *chosen_name, u32 *crc) +{ + struct device_node *np; + const char *str; + int ret; + + np = of_parse_phandle(ave->dev->of_node, "memory-region", index); + if (!np) + return -ENOENT; + ret = of_property_read_string(np, "aurora,crc32", &str); + of_node_put(np); + if (!ret) + return kstrtou32(str, 16, crc); + return ave_fw_chosen_crc(ave, chosen_name, crc); +} + +/* + * The iommu-addresses tuple <&dev iova-hi iova-lo size-hi size-lo> for + * @phandle. A reservation shared by several encoders has one tuple each; + * none, or two for the same device, is an error. + */ +static const __be32 *ave_fw_iommu_tuple(const __be32 *prop, int len, + u32 phandle) +{ + const unsigned int cells = 5; + const __be32 *match = NULL; + + if (!prop || len <= 0 || len % (cells * sizeof(u32))) + return NULL; + for (unsigned int i = 0; i < len / sizeof(u32); i += cells) { + if (be32_to_cpup(prop + i) != phandle) + continue; + if (match) + return NULL; + match = prop + i; + } + return match; +} + static int ave_fw_segment(struct ave_dev *ave, unsigned int index, struct ave_fw_segment *seg) { struct device_node *np; struct resource res; const __be32 *prop; - int len, ret; + int len = 0, ret; np = of_parse_phandle(ave->dev->of_node, "memory-region", index); if (!np) @@ -83,10 +128,13 @@ static int ave_fw_segment(struct ave_dev *ave, unsigned int index, if (ret) goto out; - /* iommu-addresses = <&ave iova-hi iova-lo size-hi size-lo> */ + /* + * iommu-addresses = <&ave iova-hi iova-lo size-hi size-lo>, one tuple + * per encoder that shares the reservation. + */ prop = of_get_property(np, "iommu-addresses", &len); - if (!prop || len != 5 * sizeof(u32) || - be32_to_cpup(prop) != ave->dev->of_node->phandle) { + prop = ave_fw_iommu_tuple(prop, len, ave->dev->of_node->phandle); + if (!prop) { ret = -EINVAL; goto out; } @@ -292,7 +340,7 @@ int ave_fw_probe(struct ave_dev *ave) ret = ave_fw_crc(&ave->text, &text_crc, NULL); if (ret) return ret; - ret = ave_fw_chosen_crc(ave, "aurora,ave-text-crc32", &want); + ret = ave_fw_expect_crc(ave, 0, "aurora,ave-text-crc32", &want); if (ret || text_crc != want) return dev_err_probe(dev, -ENODEV, "firmware text differs from loader CRC\n"); @@ -322,7 +370,7 @@ int ave_fw_probe(struct ave_dev *ave) * The loader records the DATA checksum before anything runs. If it * differs, the firmware already ran and there is no clean copy left. */ - ret = ave_fw_chosen_crc(ave, "aurora,ave-data-crc32", &want); + ret = ave_fw_expect_crc(ave, 1, "aurora,ave-data-crc32", &want); if (ret || data_crc != want) { ret = dev_err_probe(dev, -ENODEV, "firmware data is not in its boot state\n"); From aa930a5963aec5131a06fa47f87b4b583d4c4101 Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Wed, 18 Nov 2026 19:10:09 -0300 Subject: [PATCH 04/13] media: apple: ave: map an extra0 window that is not page aligned The driver requires the extra0 register window to start on a 16 KiB boundary. On T6021 neither encoder's window does: ave0's is 0x7c44 bytes at 0x2902803e0, ave1's 0x3dfc bytes at 0x28e680228. dma_map_resource() maps the pages that contain an unaligned range and returns the IOVA of its first byte, which is the address Config takes. Drop the alignment check, and map the larger of the resource size and the variant size, so that the whole window is covered: with the variant size alone, the last 0x24 bytes of ave0's window would be unmapped. T6000's window is 0x24 bytes at a 16 KiB aligned address and T8112's is page aligned and exactly its variant size, so both map 0x4000 bytes as before. The alignment check goes for every chip: an unaligned window is now mapped, not refused. The only such window outside T6021 is on the second M1 Max encoder, which the device trees leave disabled. Assisted-by: Grok 4.7 Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- drivers/media/platform/apple/ave/abi.rst | 7 ++++--- drivers/media/platform/apple/ave/ave-fw.c | 11 ++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/drivers/media/platform/apple/ave/abi.rst b/drivers/media/platform/apple/ave/abi.rst index de72afa375dcd..1ba2ff9b410ba 100644 --- a/drivers/media/platform/apple/ave/abi.rst +++ b/drivers/media/platform/apple/ave/abi.rst @@ -48,9 +48,10 @@ restore. An existing session makes the operation return EBUSY. The platform must supply the extra-0 register resource as ``extra0`` or register resource index 3, with at least 0x24 bytes on T6000 or 0x4000 bytes on T8112. -The driver uses the translated platform resource address, maps its containing -16 KiB page through the device DMA API and puts that dynamically allocated IOVA -in Config. Platform DT/binding changes are separate from this driver change. +The driver uses the translated platform resource address, maps the 16 KiB pages +that contain it through the device DMA API and puts that dynamically allocated +IOVA in Config. The base need not be page aligned: Config then gets the IOVA of +the base, not of its page. Platform DT/binding changes are separate from this driver change. No fixed firmware or register IOVA is used as an ordinary DMA allocation. Before every boot the driver checks the firmware's IOMMU translations. DATA must diff --git a/drivers/media/platform/apple/ave/ave-fw.c b/drivers/media/platform/apple/ave/ave-fw.c index f7ea9f53cdecd..f9e703d8d86a8 100644 --- a/drivers/media/platform/apple/ave/ave-fw.c +++ b/drivers/media/platform/apple/ave/ave-fw.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -509,13 +510,17 @@ static int ave_fw_map_extra0(struct ave_dev *ave) res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "extra0"); if (!res) res = platform_get_resource(pdev, IORESOURCE_MEM, 3); - if (!res || resource_size(res) < ave->variant->extra0_size || - !IS_ALIGNED(res->start, SZ_16K)) + /* + * The window need not be 16 KiB aligned: dma_map_resource maps the + * pages that contain it and returns the IOVA of its base. + */ + if (!res || resource_size(res) < ave->variant->extra0_size) return dev_err_probe( ave->dev, -EINVAL, "missing or invalid extra0 register resource\n"); /* The platform resource supplies the translated physical address. */ - ave->extra0_map_size = ALIGN(ave->variant->extra0_size, SZ_16K); + ave->extra0_map_size = max_t(size_t, resource_size(res), + ALIGN(ave->variant->extra0_size, SZ_16K)); ave->extra0_dma = dma_map_resource(ave->dev, res->start, ave->extra0_map_size, DMA_BIDIRECTIONAL, 0); From 805c89fc6e67ec82d3533a595dda0e2137062040 Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Thu, 19 Nov 2026 14:37:38 -0300 Subject: [PATCH 05/13] media: apple: ave: add the T6021 (M2 Max) encoder The M2 Max runs a larger build of the 6070.11.1 firmware the T6000 and T8112 support uses, AppleAVE2FW_H14C: TEXT 0xf0000 and DATA 0x134000, with the link cell at TEXT 0x423c. Its two encoders share TEXT and each has its own DATA. The coprocessor registers, boot argument and timebase are T8112's. With boot device ID 20 and device type 17 both encoders of a J475c boot, answer the handshake and encode. The identity CRC, TEXT with the link cell cleared, is 0x0d56db87; select the 6070.11.1 layout for it. Config on T6021 needs a shared area. With the pointer and size at +0x60 and +0x68 left zero, as on T8112, the firmware never answers Config. With a 64 KiB, 16 KiB-aligned area both encoders answer Config, Open and Start. Allocate it at the first boot, keep it until the device is removed, and pass it for a variant with config_shared. That is a property of the H14C build rather than of 6070.11.1: the T8112 build answers Config with both words zero. The 6070 sessions on T6021 run the T6000/T8112 commands unchanged in this commit: one constant-quality session per encoder. abi.rst said 6070 streams H.264 without B pictures; the 6070 path already offers HEVC Main and B pictures, so describe that, and say what T6021 adds and what each value was checked against. The Kconfig help now says what each chip offers. Assisted-by: Grok 4.7 Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- drivers/media/platform/apple/ave/Kconfig | 5 ++- drivers/media/platform/apple/ave/abi.rst | 40 ++++++++++++------- .../media/platform/apple/ave/ave-abi-test.c | 25 ++++++++++++ drivers/media/platform/apple/ave/ave-abi.c | 10 ++++- drivers/media/platform/apple/ave/ave-drv.c | 27 +++++++++++++ drivers/media/platform/apple/ave/ave-fw.c | 5 +++ drivers/media/platform/apple/ave/ave-layout.c | 7 +++- drivers/media/platform/apple/ave/ave.h | 11 ++++- 8 files changed, 110 insertions(+), 20 deletions(-) diff --git a/drivers/media/platform/apple/ave/Kconfig b/drivers/media/platform/apple/ave/Kconfig index cf7b39a06218c..36f9c412a5023 100644 --- a/drivers/media/platform/apple/ave/Kconfig +++ b/drivers/media/platform/apple/ave/Kconfig @@ -10,8 +10,9 @@ config VIDEO_APPLE_AVE help V4L2 memory-to-memory driver for the AVE H.264/HEVC encoder in Apple T8140 (A18 Pro) SoCs, as in the MacBook Neo. - T6000 (M1 Pro) and T8112 (M2) support lifecycle-only validation - of firmware 6070.11.1 through debugfs; encoding stays disabled. + T6000 (M1 Pro) and T8112 (M2) run firmware 6070.11.1 and offer + constant-quality H.264 and HEVC. T6021 (M2 Max) runs a larger + build of that firmware on two encoders. T6041 (M4 Pro) has probe-only support. T6050 (M5 Pro/Max) encodes H.264. diff --git a/drivers/media/platform/apple/ave/abi.rst b/drivers/media/platform/apple/ave/abi.rst index 1ba2ff9b410ba..0551f72f2b5c8 100644 --- a/drivers/media/platform/apple/ave/abi.rst +++ b/drivers/media/platform/apple/ave/abi.rst @@ -15,8 +15,10 @@ at 1920x1080 with B-frames enabled/disabled, and 3840x2160 with B-frames enabled They compare both deterministic synthetic DART addresses and masked images. The constant image tables in ave-image.c are unchanged by this refactor. -6070.11.1 is selected only for T6000 TEXT CRC 460c7a7c or T8112 TEXT CRC 25bfd039, -with the corresponding SoC identity. Boot, seven-channel discovery, Config, +6070.11.1 is selected for T6000 TEXT CRC 460c7a7c, T8112 TEXT CRC 25bfd039 or +T6021 TEXT CRC 0d56db87, with the corresponding SoC identity. T6021 runs a +larger build of the same version (TEXT 0xf0000, DATA 0x134000) with T8112's +registers and boot arguments. Boot, seven-channel discovery, Config, Open, Close and scratch-confirmed Halt are implemented from the clean derived SPEC-6070-lifecycle.md ledger. Start and ordinary Process writers implement the clean revision-2/P1 field ledgers: fixed-capacity address banks, split output @@ -26,14 +28,21 @@ session allocator provides distinct backing for every live bank and rejects non-16-KiB-aligned IOVAs. Streaming is enabled for one quality-first NV12 constant-quality session of -8-bit H.264 (Baseline, Main or High) with low delay: I and P pictures, two -reconstruction slots that alternate, frame widths in whole macroblocks. B -pictures, HEVC, Main 10, bit-rate modes, speed priority, standalone LRME and -Reset remain unavailable; a session asking for B pictures is encoded without -them, the others are refused. No Priority command is sent. Complete is the -normal drain barrier; Flush is reserved for cancellation before Stop and Close. -KUnit checks the 1080p and 4K fields directly from the clean ledgers and embeds -no Apple command image. +8-bit H.264 (Baseline, Main or High) or HEVC Main, with up to one B picture per +P picture, frame widths in whole macroblocks. Main 10, bit-rate modes, speed +priority, standalone LRME and Reset remain unavailable and are refused. No +Priority command is sent. Complete is the normal drain barrier; Flush is +reserved for cancellation before Stop and Close. KUnit checks the 1080p and 4K +fields directly from the clean ledgers and embeds no Apple command image. + +T6021 differs in these points, each checked on a J475c; they are not from the +ledgers above. Config carries a shared area, its address at +0x60 and its size +at +0x68: with both left zero, as on T8112, the firmware never answers Config, +and with a 64 KiB, 16 KiB-aligned area both encoders answer Config, Open and +Start and encode. With boot device ID 20 and device type 17 both encoders boot +and answer the handshake. The TEXT word at 0x423c holds the low 32 bits of +DATA's device address on both encoders, and TEXT with that word cleared has +CRC 0d56db87. Lifecycle-only validation ------------------------- @@ -47,11 +56,12 @@ that was never initialized. A second write exercises a cold restart and DATA restore. An existing session makes the operation return EBUSY. The platform must supply the extra-0 register resource as ``extra0`` or register -resource index 3, with at least 0x24 bytes on T6000 or 0x4000 bytes on T8112. -The driver uses the translated platform resource address, maps the 16 KiB pages -that contain it through the device DMA API and puts that dynamically allocated -IOVA in Config. The base need not be page aligned: Config then gets the IOVA of -the base, not of its page. Platform DT/binding changes are separate from this driver change. +resource index 3, with at least 0x24 bytes on T6000, 0x4000 bytes on T8112 or +0x3dfc bytes on T6021. The driver uses the translated platform resource address, +maps the 16 KiB pages that contain it through the device DMA API and puts that +dynamically allocated IOVA in Config. The base need not be page aligned: Config +then gets the IOVA of the base, not of its page. Platform DT/binding changes are +separate from this driver change. No fixed firmware or register IOVA is used as an ordinary DMA allocation. Before every boot the driver checks the firmware's IOMMU translations. DATA must diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 9f8d9d7469485..629ab614d35a5 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -376,6 +376,17 @@ static void ave_6070_images(struct kunit *test) expected[0x41] = 1; put_unaligned_le64(ave->extra0_dma, expected + 0x48); KUNIT_EXPECT_MEMEQ(test, actual, expected, sizeof(actual)); + /* T6021: the shared area goes at +0x60/+0x68, byte 0x40 stays clear. */ + ave->config_shared.cpu = ave; + ave->config_shared.dma = 0x100000000ULL; + ave->config_shared.size = SZ_64K; + memset(actual, 0, sizeof(actual)); + abi->write_config(ave, actual); + ave_stamp_command(abi, actual, 3000000, 0x123456); + put_unaligned_le64(0x100000000ULL, expected + 0x60); + put_unaligned_le32(SZ_64K, expected + 0x68); + KUNIT_EXPECT_MEMEQ(test, actual, expected, sizeof(actual)); + ave->config_shared.cpu = NULL; for (unsigned int codec = AVE_CODEC_H264; codec <= AVE_CODEC_HEVC; codec++) { static const enum ave_opcode ops[] = { AVE_OP_OPEN, @@ -1186,6 +1197,8 @@ static void ave_6070_gate(struct kunit *test) &ave_layout_6070_11_1); KUNIT_EXPECT_PTR_EQ(test, ave_layout_for_crc(0x25bfd039), &ave_layout_6070_11_1); + KUNIT_EXPECT_PTR_EQ(test, ave_layout_for_crc(AVE_FW_TEXT_CRC_T6021_6070), + &ave_layout_6070_11_1); KUNIT_EXPECT_PTR_EQ(test, ave_layout_for_crc(0), NULL); /* H.264 low delay is qualified; a short command buffer is still refused. */ KUNIT_EXPECT_TRUE(test, ave_layout_6070_11_1.encode_ready); @@ -1412,6 +1425,18 @@ static void ave_6070_variants(struct kunit *test) KUNIT_EXPECT_EQ(test, ave_t8112.timebase_multiplier, 1U); KUNIT_EXPECT_EQ(test, ave_t8112.text_crc, 0x25bfd039U); KUNIT_EXPECT_EQ(test, ave_t8112.mbox_idle, 0x38U); + KUNIT_EXPECT_EQ(test, ave_t6021.fw_text_size, 0xf0000U); + KUNIT_EXPECT_EQ(test, ave_t6021.fw_data_size, 0x134000U); + KUNIT_EXPECT_EQ(test, ave_t6021.text_crc, AVE_FW_TEXT_CRC_T6021_6070); + KUNIT_EXPECT_EQ(test, ave_t6021.boot_device_id, 20U); + KUNIT_EXPECT_EQ(test, ave_t6021.device_type, 17U); + KUNIT_EXPECT_EQ(test, ave_t6021.text_link_cell, 0x423cU); + KUNIT_EXPECT_EQ(test, ave_t6021.extra0_size, 0x3dfcU); + KUNIT_EXPECT_EQ(test, ave_t6021.boot_arg_value, 0x10000U); + /* Only T6021 gives Config a shared area. */ + KUNIT_EXPECT_TRUE(test, ave_t6021.config_shared); + KUNIT_EXPECT_FALSE(test, ave_t6000.config_shared); + KUNIT_EXPECT_FALSE(test, ave_t8112.config_shared); KUNIT_EXPECT_EQ(test, ave_t6000.regs->ctrl, 0x400044U); KUNIT_EXPECT_EQ(test, ave_t8112.regs->timebase, 0x178000U); KUNIT_EXPECT_GE(test, t->command_bytes, 0x32dc8U); diff --git a/drivers/media/platform/apple/ave/ave-abi.c b/drivers/media/platform/apple/ave/ave-abi.c index bd7369d343f90..e9c881c72c195 100644 --- a/drivers/media/platform/apple/ave/ave-abi.c +++ b/drivers/media/platform/apple/ave/ave-abi.c @@ -87,7 +87,15 @@ static void ave_config_6070(struct ave_dev *ave, u8 *cmd) ave_write_global(ave, cmd, AVE_OP_CONFIG); cmd[0x41] = 1; put_unaligned_le64(ave->extra0_dma, cmd + 0x48); - /* No MCC, host heap, shared area or optional firmware log. */ + /* No MCC, host heap or optional firmware log. */ + /* + * A shared area at +0x60, its size at +0x68. T6021 does not answer + * Config without one; the area passed is 64 KiB, 16 KiB aligned. + */ + if (ave->config_shared.cpu) { + put_unaligned_le64(ave->config_shared.dma, cmd + 0x60); + put_unaligned_le32(ave->config_shared.size, cmd + 0x68); + } } static int ave_reply_9003(const struct ave_fw_abi *abi, const u8 *msg, u32 len, diff --git a/drivers/media/platform/apple/ave/ave-drv.c b/drivers/media/platform/apple/ave/ave-drv.c index 374146ebd1c2c..313caca014178 100644 --- a/drivers/media/platform/apple/ave/ave-drv.c +++ b/drivers/media/platform/apple/ave/ave-drv.c @@ -87,6 +87,32 @@ const struct ave_variant ave_t8112 = { .boot_qualified = true, }; +/* + * The M2 Max runs a larger build of the same 6070.11.1 firmware: TEXT + * 0xf0000 and DATA 0x134000 (J475c, macOS 13.5). Its two encoders share + * TEXT. Registers and boot parameters are T8112's; the device ID and type + * are its own. Neither encoder's extra0 window is 16 KiB aligned; + * extra0_size is the smaller of the two, ave1's. + */ +const struct ave_variant ave_t6021 = { + .regs = &ave_coproc_6070, + .fw_text_size = 0xf0000, + .fw_data_size = 0x134000, + .boot_device_id = 20, + .device_type = 17, + .lfs_ref_scale = 1, + .text_crc = AVE_FW_TEXT_CRC_T6021_6070, + .boot_arg_value = 0x10000, + .timebase_multiplier = 1, + .text_link_cell = 0x423c, + .mbox_idle = 0x38, + .restore_data_first = true, + + .extra0_size = 0x3dfc, + .boot_qualified = true, + .config_shared = true, +}; + /* * The M4 Pro (T6040 in the ADT, apple,t6041 in the DT) starts its encoder * like the A18 Pro: same coprocessor registers and boot sequence, its own @@ -620,6 +646,7 @@ static const struct dev_pm_ops ave_pm_ops = { static const struct of_device_id ave_of_match[] = { { .compatible = "apple,t6000-ave", .data = &ave_t6000 }, + { .compatible = "apple,t6021-ave", .data = &ave_t6021 }, { .compatible = "apple,t6041-ave", .data = &ave_t6041 }, { .compatible = "apple,t6050-ave", .data = &ave_t6050 }, { .compatible = "apple,t8112-ave", .data = &ave_t8112 }, diff --git a/drivers/media/platform/apple/ave/ave-fw.c b/drivers/media/platform/apple/ave/ave-fw.c index f9e703d8d86a8..e75a43733919f 100644 --- a/drivers/media/platform/apple/ave/ave-fw.c +++ b/drivers/media/platform/apple/ave/ave-fw.c @@ -46,6 +46,7 @@ static const u32 ave_known_text_crc[] = { AVE_FW_TEXT_CRC_T8142_26A428, /* AppleAVE2FW 9013.45.1, macOS 27.0 26A428 / T8142 */ 0x460c7a7c, /* AppleAVE2FW 6070.11.1 / T6000 */ 0x25bfd039, /* AppleAVE2FW 6070.11.1 / T8112 */ + AVE_FW_TEXT_CRC_T6021_6070, /* AppleAVE2FW 6070.11.1 / T6021 H14C */ }; static int ave_fw_chosen_crc(struct ave_dev *ave, const char *name, u32 *crc) @@ -533,6 +534,7 @@ static int ave_fw_map_extra0(struct ave_dev *ave) void ave_fw_remove(struct ave_dev *ave) { ave_fw_release_boot(ave); + ave_free(ave, &ave->config_shared); vfree(ave->data_pristine); ave->data_pristine = NULL; } @@ -768,6 +770,9 @@ static int ave_fw_boot(struct ave_dev *ave) ret = ave_alloc(ave, &ave->log, AVE_LOG_BYTES, 0); if (!ret) ret = ave_fw_map_extra0(ave); + /* Config's shared area is allocated once and kept until remove. */ + if (!ret && ave->variant->config_shared) + ret = ave_alloc(ave, &ave->config_shared, SZ_64K, 0); if (ret) return ret; diff --git a/drivers/media/platform/apple/ave/ave-layout.c b/drivers/media/platform/apple/ave/ave-layout.c index 34bde7f96f641..779a72b2b876b 100644 --- a/drivers/media/platform/apple/ave/ave-layout.c +++ b/drivers/media/platform/apple/ave/ave-layout.c @@ -957,7 +957,12 @@ const struct ave_fw_layout *ave_layout_for_crc(u32 crc) if (crc == AVE_FW_TEXT_CRC_9003_78_0 || crc == AVE_FW_TEXT_CRC_T6040_25G83 || crc == AVE_FW_TEXT_CRC_T6041_25G83) return &ave_layout_9003_78_0; - if (crc == 0x460c7a7c || crc == 0x25bfd039) + /* + * T6000 (H13S), T8112 (H14G) and T6021 (H14C) are one 6070.11.1 + * command layout. H14C is a larger link of the same version. + */ + if (crc == 0x460c7a7c || crc == 0x25bfd039 || + crc == AVE_FW_TEXT_CRC_T6021_6070) return &ave_layout_6070_11_1; /* * The macOS 27.0 build for the M5 Pro and Max, and the M5's build of the diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index d8075a8d38bc1..a630a84d5c895 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -178,6 +178,11 @@ enum ave_opcode { #define AVE_FW_TEXT_CRC_T6050_26C5035 0xc25b4ddb /* The M5 (T8142) build of the same 26A428 firmware (AppleAVE2FW 9013.45.1). */ #define AVE_FW_TEXT_CRC_T8142_26A428 0x8a3784bd +/* + * The M2 Max (T6021) build of AppleAVE2FW 6070.11.1, macOS 13.5 22G74, with + * the link cell at TEXT 0x423c zero. + */ +#define AVE_FW_TEXT_CRC_T6021_6070 0x0d56db87 struct ave_ctx; @@ -243,6 +248,8 @@ struct ave_variant { size_t extra0_size; /* Register sequence and boot/hello configuration have been qualified. */ bool boot_qualified; + /* 6070 Config carries a 64 KiB shared area. */ + bool config_shared; }; /* Start surfaces of an MCTF session, aliases included */ @@ -293,6 +300,8 @@ struct ave_dev { dma_addr_t extra0_dma; size_t extra0_map_size; bool extra0_mapped; + /* Config's shared area, for a variant with config_shared */ + struct ave_buf config_shared; bool module_pinned; /* A 6070 lifecycle command is serialized per logical operation. */ struct ave_reply expected[AVE_OP_COUNT]; @@ -551,7 +560,7 @@ static inline struct ave_ctx *ave_file_to_ctx(struct file *file) return container_of(file_to_v4l2_fh(file), struct ave_ctx, fh); } -extern const struct ave_variant ave_t6000, ave_t6041, ave_t8112; +extern const struct ave_variant ave_t6000, ave_t6021, ave_t6041, ave_t8112; /* ave-fw.c */ u64 ave_fw_vcbar(const struct ave_variant *variant, u64 text_iova); From b4de0709d3f8afbfc84f2afa2789d43e492be00a Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Thu, 19 Nov 2026 14:40:36 -0300 Subject: [PATCH 06/13] media: apple: ave: bound the sessions kept for a power cycle A session the firmware saw keeps its buffers until the coprocessor is proved to have lost power, and that happens only after the last session closes. With two sessions per boot, one long-lived stream and a second that is opened and closed again and again keep every closed session's buffers, a full set per session, with no bound. Count the sessions waiting for the reap. Once four wait, refuse a new session that would join the running firmware with EBUSY and a rate-limited warning. The refused session was never handed to the firmware, so it is freed at once. When the last stream closes, the power cycle frees all of them and resets the count. This holds for every layout; 9003 already ran two sessions per boot. Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- Documentation/admin-guide/media/apple-ave.rst | 4 +++ .../media/platform/apple/ave/ave-abi-test.c | 30 +++++++++++++++++++ drivers/media/platform/apple/ave/ave-sess.c | 2 ++ drivers/media/platform/apple/ave/ave-v4l2.c | 5 ++++ drivers/media/platform/apple/ave/ave.h | 6 ++++ 5 files changed, 47 insertions(+) diff --git a/Documentation/admin-guide/media/apple-ave.rst b/Documentation/admin-guide/media/apple-ave.rst index 7f9a2c9084e6d..4fa58a4378cd6 100644 --- a/Documentation/admin-guide/media/apple-ave.rst +++ b/Documentation/admin-guide/media/apple-ave.rst @@ -129,6 +129,10 @@ The firmware boots when the first stream starts and is halted when the last one ends, as macOS does; the next stream starts it from reset. Runtime PM powers the encoder down two seconds after the last stream. +A stream's buffers are freed only after that power cycle. While one stream +stays open, up to four streams that ended keep their buffers; a further +stream then fails to start with ``EBUSY`` until every stream has closed. + Up to five frames per stream are inside the firmware at once (three without B-frames). The firmware reorders B-frames itself, so CAPTURE buffers come back in bitstream order, each with the timestamp of the frame it codes. diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 629ab614d35a5..7fb13b74e2d00 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -1583,6 +1583,35 @@ static void ave_6070_qualified_bytes(struct kunit *test) } } +/* + * A session the firmware saw counts toward the quarantine bound until a reap; + * one it never saw is freed at once. + */ +static void ave_quarantine_count(struct kunit *test) +{ + struct ave_dev *ave = kunit_kzalloc(test, sizeof(*ave), GFP_KERNEL); + struct ave_ctx *ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + + KUNIT_ASSERT_TRUE(test, ave && ctx); + ave->dev = kunit_device_register(test, "apple-ave-quarantine"); + KUNIT_ASSERT_FALSE(test, IS_ERR(ave->dev)); + mutex_init(&ave->hw_lock); + ctx->ave = ave; + for (unsigned int i = 0; i <= AVE_QUARANTINE_MAX; i++) { + ctx->sess = kzalloc_obj(*ctx->sess); + KUNIT_ASSERT_NOT_NULL(test, ctx->sess); + ctx->sess->handed = i < AVE_QUARANTINE_MAX; + ave_sess_destroy(ctx); + KUNIT_EXPECT_NULL(test, ctx->sess); + } + KUNIT_EXPECT_EQ(test, ave->quarantined, AVE_QUARANTINE_MAX); + mutex_lock(&ave->hw_lock); + ave_sess_reap(ave); + mutex_unlock(&ave->hw_lock); + KUNIT_EXPECT_EQ(test, ave->quarantined, 0U); + KUNIT_EXPECT_NULL(test, ave->failed_sessions); +} + static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_t6041_text_boot_word), KUNIT_CASE(ave_9003_byte_equality), KUNIT_CASE(ave_9003_hevc_main10), @@ -1599,6 +1628,7 @@ static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_6070_gate), KUNIT_CASE(ave_6070_channels), KUNIT_CASE(ave_6070_variants), KUNIT_CASE(ave_6070_text_id), KUNIT_CASE(ave_6070_smalloc), + KUNIT_CASE(ave_quarantine_count), {} }; diff --git a/drivers/media/platform/apple/ave/ave-sess.c b/drivers/media/platform/apple/ave/ave-sess.c index 4e860b5916f01..fd52c29d5db44 100644 --- a/drivers/media/platform/apple/ave/ave-sess.c +++ b/drivers/media/platform/apple/ave/ave-sess.c @@ -943,6 +943,7 @@ void ave_sess_destroy(struct ave_ctx *ctx) sess->mctf_lin = NULL; sess->failed_next = ave->failed_sessions; ave->failed_sessions = sess; + ave->quarantined++; } else { ave_sess_free_bufs(ave, sess); kfree(sess); @@ -978,6 +979,7 @@ void ave_sess_reap(struct ave_dev *ave) kfree(sess); freed = true; } + ave->quarantined = 0; domain = freed ? iommu_get_domain_for_dev(ave->dev) : NULL; if (domain) iommu_flush_iotlb_all(domain); diff --git a/drivers/media/platform/apple/ave/ave-v4l2.c b/drivers/media/platform/apple/ave/ave-v4l2.c index 4715ab8e8c2ab..0fb302d81654a 100644 --- a/drivers/media/platform/apple/ave/ave-v4l2.c +++ b/drivers/media/platform/apple/ave/ave-v4l2.c @@ -839,6 +839,11 @@ static int ave_session_attach(struct ave_ctx *ctx) } else if (ave->fw_state != AVE_FW_RUNNING) { dev_err(ave->dev, "firmware state %d, cannot add a session\n", ave->fw_state); return -EIO; + } else if (ave->quarantined >= AVE_QUARANTINE_MAX) { + dev_warn_ratelimited(ave->dev, + "%u stopped sessions wait for a power cycle; close every stream first\n", + ave->quarantined); + return -EBUSY; } sess->index = idx; diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index a630a84d5c895..0ad2c0528e732 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -44,6 +44,11 @@ struct ave_mctf_layout; #define AVE_MAX_SLOTS 7 /* Sessions one firmware boot serves at once (macOS was seen running two). */ #define AVE_MAX_SESSIONS 2 +/* + * Stopped sessions whose memory may wait for a power cycle while another + * session keeps the firmware up. A session beyond this is refused instead. + */ +#define AVE_QUARANTINE_MAX 4 /* * Coprocessor memory every firmware boot uses, all inside one allocation. @@ -284,6 +289,7 @@ struct ave_dev { struct ave_recovery_history recovery; unsigned int recovery_count; struct ave_session *failed_sessions; + unsigned int quarantined; bool inject_wedge; /* debugfs: the last Start and first Process built, per codec */ From 9aadf56a25ad8b3b9bb30bdbadc9598f5d39b438 Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Thu, 19 Nov 2026 14:38:40 -0300 Subject: [PATCH 07/13] media: apple: ave: run two sessions on one 6070.11.1 boot The 6070.11.1 layout allows one session per firmware boot, while the driver already keeps the process buffers per session. Allow two, on every SoC that runs this firmware. On a J475c, four H.264 sessions, two on each encoder, ran in real time for five minutes at their target rates, and a fifth STREAMON got EBUSY. T6000 and T8112 were not run with two sessions. When the first of two sessions closes, its buffers stay quarantined until the last session closes and the encoder is power cycled, as on 9003. Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- drivers/media/platform/apple/ave/abi.rst | 16 +++++++++++----- drivers/media/platform/apple/ave/ave-abi-test.c | 2 ++ drivers/media/platform/apple/ave/ave-layout.c | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/drivers/media/platform/apple/ave/abi.rst b/drivers/media/platform/apple/ave/abi.rst index 0551f72f2b5c8..fe2f0c286a611 100644 --- a/drivers/media/platform/apple/ave/abi.rst +++ b/drivers/media/platform/apple/ave/abi.rst @@ -27,11 +27,11 @@ retained codec-state partitions and per-slot HEVC slice-header tables. The session allocator provides distinct backing for every live bank and rejects non-16-KiB-aligned IOVAs. -Streaming is enabled for one quality-first NV12 constant-quality session of -8-bit H.264 (Baseline, Main or High) or HEVC Main, with up to one B picture per -P picture, frame widths in whole macroblocks. Main 10, bit-rate modes, speed -priority, standalone LRME and Reset remain unavailable and are refused. No -Priority command is sent. Complete is the normal drain barrier; Flush is +Streaming is enabled for up to two quality-first NV12 constant-quality sessions +per firmware boot, each of 8-bit H.264 (Baseline, Main or High) or HEVC Main, +with up to one B picture per P picture, frame widths in whole macroblocks. +Main 10, bit-rate modes, speed priority, standalone LRME and Reset remain +unavailable and are refused. No Priority command is sent. Complete is the normal drain barrier; Flush is reserved for cancellation before Stop and Close. KUnit checks the 1080p and 4K fields directly from the clean ledgers and embeds no Apple command image. @@ -44,6 +44,12 @@ and answer the handshake. The TEXT word at 0x423c holds the low 32 bits of DATA's device address on both encoders, and TEXT with that word cleared has CRC 0d56db87. +Some 6070.11.1 settings were checked on T6021 (J475c) only. T6000 and T8112 run +the same firmware version and get the same settings, untested: + +- Two sessions per boot. Four H.264 sessions, two on each encoder, ran in real + time for five minutes at their target rates; a fifth STREAMON got EBUSY. + Lifecycle-only validation ------------------------- diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 7fb13b74e2d00..2b1b1257bf067 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -1437,6 +1437,8 @@ static void ave_6070_variants(struct kunit *test) KUNIT_EXPECT_TRUE(test, ave_t6021.config_shared); KUNIT_EXPECT_FALSE(test, ave_t6000.config_shared); KUNIT_EXPECT_FALSE(test, ave_t8112.config_shared); + /* One 6070.11.1 boot serves two sessions, on every SoC. */ + KUNIT_EXPECT_EQ(test, ave_layout_6070_11_1.max_sessions, 2U); KUNIT_EXPECT_EQ(test, ave_t6000.regs->ctrl, 0x400044U); KUNIT_EXPECT_EQ(test, ave_t8112.regs->timebase, 0x178000U); KUNIT_EXPECT_GE(test, t->command_bytes, 0x32dc8U); diff --git a/drivers/media/platform/apple/ave/ave-layout.c b/drivers/media/platform/apple/ave/ave-layout.c index 779a72b2b876b..70a27266ff7fa 100644 --- a/drivers/media/platform/apple/ave/ave-layout.c +++ b/drivers/media/platform/apple/ave/ave-layout.c @@ -552,7 +552,7 @@ const struct ave_fw_layout ave_layout_6070_11_1 = { .process_timeout_us = 46400000, .process_subtype = 0x15, .max_slots = 5, - .max_sessions = 1, + .max_sessions = 2, .codec_state_bytes = { 0x984, 0x5884 }, .start = { [0] = { From d0b0e0aa187c051dd190f278943965c45a2d505f Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Thu, 19 Nov 2026 14:42:31 -0300 Subject: [PATCH 08/13] media: apple: ave: keep 6070.11.1 H.264 P pictures as references Two Start and Process words make 6070.11.1 H.264 P pictures much larger than they need to be. Both were checked on a J475c (T6021): - With the Process timescale word nonzero, every P slice had nal_ref_idc 0, so no P picture was a reference. With it zero, P slices have nal_ref_idc 1 and frame_num counts up. Submit 0 there for H.264; the PTS still counts in the same timescale. - With the motion lambdas at Start 0xff98, 0xffa4 and 0xffa8 zero, about 94% of P macroblocks were 8x8 partitions, and constant quality 0 on a 720p lecture clip came out at 8.4 Mbit/s. With 64 (full-pel search), 256 (inter mode decision) and 64 (intra mode decision), 8x8 became rare, the clip came out at 0.30 Mbit/s, and quality 50 took 1.8 times the bits of libx264 at the same PSNR. Name the lambda offsets in the 6070 layout, and key both on a new layout flag, h264_inter, set for 6070.11.1: they are words of that firmware's commands, and T6000 and T8112 run the same firmware version. Those two were not run with them; their encoded H.264 changes. The lambdas were tuned on one 720p clip and checked with CBR at 720p and 1080p. The pinned T6000/T8112 command bytes are built with the flag off and do not change; a new test checks what the flag writes. Assisted-by: Grok 4.7 Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- drivers/media/platform/apple/ave/abi.rst | 9 ++ .../media/platform/apple/ave/ave-abi-test.c | 86 ++++++++++++++++++- drivers/media/platform/apple/ave/ave-layout.c | 4 + drivers/media/platform/apple/ave/ave-layout.h | 10 +++ drivers/media/platform/apple/ave/ave-sess.c | 2 + drivers/media/platform/apple/ave/ave-writer.c | 20 ++++- drivers/media/platform/apple/ave/ave.h | 2 + 7 files changed, 129 insertions(+), 4 deletions(-) diff --git a/drivers/media/platform/apple/ave/abi.rst b/drivers/media/platform/apple/ave/abi.rst index fe2f0c286a611..29ace386e49a1 100644 --- a/drivers/media/platform/apple/ave/abi.rst +++ b/drivers/media/platform/apple/ave/abi.rst @@ -49,6 +49,15 @@ the same firmware version and get the same settings, untested: - Two sessions per boot. Four H.264 sessions, two on each encoder, ran in real time for five minutes at their target rates; a fifth STREAMON got EBUSY. +- H.264 P pictures as references. With the Process timescale word nonzero, + every P slice had nal_ref_idc 0; with it zero, P slices have nal_ref_idc 1 + and frame_num counts up. The driver writes zero for H.264. +- H.264 motion lambdas, Start 0xff98 (full-pel search), 0xffa4 (inter mode + decision) and 0xffa8 (intra mode decision). With all three zero, about 94% of + P macroblocks were 8x8 partitions and constant quality 0 on a 720p lecture + clip came out at 8.4 Mbit/s. With 64, 256 and 64, 8x8 partitions became rare, + the same clip came out at 0.30 Mbit/s, and quality 50 took 1.8 times the bits + of libx264 at the same PSNR. Lifecycle-only validation ------------------------- diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 2b1b1257bf067..8328c5973632e 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -1523,11 +1523,90 @@ static void ave_t6041_text_boot_word(struct kunit *test) KUNIT_EXPECT_EQ(test, ave_fw_text_boot_crc(text, size, size - 2, &a), -EINVAL); } +/* The process timescale the qualified 6070 image writes. */ +#define AVE_TEST_PROC_TIMESCALE 14400000ULL + +/* Build the Start image and the first Process image of @sess. */ +static void ave_test_6070_build(struct kunit *test, struct ave_ctx *ctx, + u8 *start, u8 *process, u32 bitrate) +{ + const struct ave_fw_layout *fw = ctx->ave->layout; + enum ave_codec codec = ctx->sess->p.codec; + size_t start_len = fw->bytes[codec - 1][AVE_IMAGE_START]; + size_t process_len = fw->bytes[codec - 1][AVE_IMAGE_PROCESS]; + + memset(start, 0, start_len); + memset(process, 0, process_len); + KUNIT_ASSERT_EQ(test, ave_build_start(ctx, start, start_len), 0); + KUNIT_ASSERT_EQ(test, + ave_build_process(ctx, process, process_len, 0, + 0x8f0000000ULL, bitrate, false, false), + 0); +} + +/* h264_inter: P pictures stay references, inter decisions are weighted. */ +static void ave_6070_h264_inter(struct kunit *test) +{ + struct ave_dev *ave = kunit_kzalloc(test, sizeof(*ave), GFP_KERNEL); + struct ave_ctx *ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + struct ave_session *sess = kunit_kzalloc(test, sizeof(*sess), GFP_KERNEL); + u8 *start = kunit_kzalloc(test, 0x32dc8, GFP_KERNEL); + u8 *process = kunit_kzalloc(test, 0x6838, GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, ave); + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_NOT_NULL(test, sess); + KUNIT_ASSERT_NOT_NULL(test, start); + KUNIT_ASSERT_NOT_NULL(test, process); + KUNIT_EXPECT_TRUE(test, ave_layout_6070_11_1.h264_inter); + + for (unsigned int codec = AVE_CODEC_H264; codec <= AVE_CODEC_HEVC; + codec++) { + const struct ave_start_layout *sl; + const struct ave_proc_layout *pl; + bool h264 = codec == AVE_CODEC_H264; + + ave_test_6070_session(test, ave, ctx, sess, codec, 1280, 720); + sl = &ave->layout->start[codec - 1]; + pl = &ave->layout->process[codec - 1]; + for (unsigned int on = 0; on < 2; on++) { + sess->p.h264_inter = on; + ave_test_6070_build(test, ctx, start, process, 4000000); + KUNIT_EXPECT_EQ(test, + get_unaligned_le64(process + pl->timescale), + on && h264 ? 0 : AVE_TEST_PROC_TIMESCALE); + if (!h264) + continue; + KUNIT_EXPECT_EQ(test, + get_unaligned_le32(start + + sl->h264_me_fullpel_lambda), + on ? 64U : 0U); + KUNIT_EXPECT_EQ(test, + get_unaligned_le32(start + + sl->h264_md_inter_lambda), + on ? 256U : 0U); + KUNIT_EXPECT_EQ(test, + get_unaligned_le32(start + + sl->h264_md_intra_lambda), + on ? 64U : 0U); + /* The other H.264 inter words are not touched. */ + KUNIT_EXPECT_EQ(test, + get_unaligned_le16(start + sl->h264_skip_mode), + (u16)AVE_AVC_RECON_SKIP_MODE); + KUNIT_EXPECT_EQ(test, + get_unaligned_le32(process + + pl->static_area_mask), + (u32)AVE_AVC_STATIC_AREA_MASK_ALL); + } + } +} + /* - * T6000 and T8112 run the 6070 commands upstream qualified byte for byte. + * The 6070 commands upstream qualified on T6000 and T8112, byte for byte. * These CRCs pin the Start and first two Process commands the builder - * produces for them, so a change meant for another chip cannot alter - * those bytes unnoticed. + * produces with the layout's encode settings (struct ave_fw_layout) off, + * so nothing else can alter those bytes unnoticed. The tests above check + * what each setting writes. */ static void ave_6070_qualified_bytes(struct kunit *test) { @@ -1623,6 +1702,7 @@ static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_6070_split_surfaces), KUNIT_CASE(ave_6070_start_process_fields), KUNIT_CASE(ave_6070_qualified_bytes), + KUNIT_CASE(ave_6070_h264_inter), KUNIT_CASE(ave_6070_reordered_pictures), KUNIT_CASE(ave_6070_hevc_contract), KUNIT_CASE(ave_6070_entropy_interleave), diff --git a/drivers/media/platform/apple/ave/ave-layout.c b/drivers/media/platform/apple/ave/ave-layout.c index 70a27266ff7fa..dd014958ed883 100644 --- a/drivers/media/platform/apple/ave/ave-layout.c +++ b/drivers/media/platform/apple/ave/ave-layout.c @@ -554,6 +554,7 @@ const struct ave_fw_layout ave_layout_6070_11_1 = { .max_slots = 5, .max_sessions = 2, .codec_state_bytes = { 0x984, 0x5884 }, + .h264_inter = true, .start = { [0] = { .header = AVE_SURFACE(0x40, 0x10), @@ -644,6 +645,9 @@ const struct ave_fw_layout ave_layout_6070_11_1 = { .h264_pixel_packing = 0xfce8, .h264_transform_mode = 0xfcec, .h264_skip_mode = 0xfcf0, + .h264_me_fullpel_lambda = 0xff98, + .h264_md_inter_lambda = 0xffa4, + .h264_md_intra_lambda = 0xffa8, .h264_input_bit_depth = 0xfd20, .h264_parameter_set_count = 0xfda4, .h264_slice_map_count = 0xfdac, diff --git a/drivers/media/platform/apple/ave/ave-layout.h b/drivers/media/platform/apple/ave/ave-layout.h index 38bafb89d223f..b0f23894799d3 100644 --- a/drivers/media/platform/apple/ave/ave-layout.h +++ b/drivers/media/platform/apple/ave/ave-layout.h @@ -208,6 +208,10 @@ struct ave_start_layout { u32 h264_pixel_packing; u32 h264_transform_mode; u32 h264_skip_mode; + /* H.264 motion lambdas. Zero leaves inter mode decision unweighted. */ + u32 h264_me_fullpel_lambda; + u32 h264_md_inter_lambda; + u32 h264_md_intra_lambda; u32 h264_input_bit_depth; u32 h264_parameter_set_count; u32 h264_slice_map_count; @@ -380,6 +384,12 @@ struct ave_fw_layout { u8 process_subtype, max_slots, mcc_dsid; u8 max_sessions; u32 codec_state_bytes[2]; + /* + * Encode settings of this firmware, checked on one SoC; abi.rst says + * which and against what. + */ + /* H.264 P pictures are references; inter decisions are weighted. */ + bool h264_inter; bool counted; }; diff --git a/drivers/media/platform/apple/ave/ave-sess.c b/drivers/media/platform/apple/ave/ave-sess.c index fd52c29d5db44..f522768d236d1 100644 --- a/drivers/media/platform/apple/ave/ave-sess.c +++ b/drivers/media/platform/apple/ave/ave-sess.c @@ -126,6 +126,7 @@ u32 ave_cq_initial_qp(u32 quality) static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) { + const struct ave_fw_layout *layout = ctx->ave->layout; bool baseline = false; int level; @@ -140,6 +141,7 @@ static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) p->fps_num = ctx->timeperframe.denominator; p->fps_den = ctx->timeperframe.numerator; p->fps_int = max(DIV_ROUND_CLOSEST(p->fps_num, p->fps_den), 1U); + p->h264_inter = layout->h264_inter; if (p->codec == AVE_CODEC_H264) { switch (ctx->h264_profile) { diff --git a/drivers/media/platform/apple/ave/ave-writer.c b/drivers/media/platform/apple/ave/ave-writer.c index 8dbc0192a778a..244f96986dd43 100644 --- a/drivers/media/platform/apple/ave/ave-writer.c +++ b/drivers/media/platform/apple/ave/ave-writer.c @@ -864,6 +864,16 @@ static void ave_start_config(const struct ave_params *p, u8 *cmd, ave_start_put32(cmd, l->h264_transform_mode, p->high ? 2 : 0); if (l->h264_skip_mode) ave_put16(cmd, l->h264_skip_mode, AVE_AVC_RECON_SKIP_MODE); + /* + * With these left zero, most of each P picture is coded as + * 8x8 partitions. They weight the full-pel search and the + * inter and intra decisions. + */ + if (p->h264_inter) { + ave_start_put32(cmd, l->h264_me_fullpel_lambda, 64); + ave_start_put32(cmd, l->h264_md_inter_lambda, 256); + ave_start_put32(cmd, l->h264_md_intra_lambda, 64); + } ave_start_put32(cmd, l->h264_input_bit_depth, depth); ave_start_put32(cmd, l->h264_parameter_set_count, 1); ave_start_put32(cmd, l->h264_slice_map_count, 1); @@ -1259,7 +1269,15 @@ int ave_build_process(struct ave_ctx *ctx, u8 *cmd, size_t len, else ave_put32(cmd, l->const5, 5); ave_put64(cmd, l->fps, ave_f64(p->fps_num, p->fps_den)); - ave_put64(cmd, l->timescale, AVE_PROC_TIMESCALE); + /* + * With a nonzero timescale every H.264 P picture is coded with + * nal_ref_idc 0. Zero keeps them references; the PTS below still + * counts in AVE_PROC_TIMESCALE. + */ + if (p->h264_inter && p->codec == AVE_CODEC_H264) + ave_put64(cmd, l->timescale, 0); + else + ave_put64(cmd, l->timescale, AVE_PROC_TIMESCALE); if (l->crop) ave_put32(cmd, l->crop, p->cw != p->width || p->ch != p->height); diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index 0ad2c0528e732..5714484e4a19f 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -395,6 +395,8 @@ struct ave_params { u32 mbs; /* 16x16 macroblocks */ u32 fps_num, fps_den; /* frames per second = num / den */ u32 fps_int; /* the same, rounded */ + /* The layout's encode settings, see struct ave_fw_layout. */ + bool h264_inter; u32 slots; u32 refs; /* max_num_ref_frames + 1 */ bool bframes; From a73782b55f0905f7646504a5b592030f6df1820d Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Thu, 19 Nov 2026 14:44:01 -0300 Subject: [PATCH 09/13] media: apple: ave: offer CBR and VBR on 6070.11.1 6070.11.1 offers constant quality only, because the driver's CBR value, 2, holds the Start QP on that firmware. On a J475c (T6021), mode 1 moved slice QP with the bit rate. Without B pictures the coded rate stayed under the target; for CBR the host's filler data then brought 1.0 and 2.5 Mbit/s (720p) and 4.0 Mbit/s (1080p) within 15% of the target over the file and over its last 150 frames. Submit mode 1 for CBR and VBR and offer both. With a frame interval of 1000/30000 s every frame sat at the QP ceiling: about 0.31 Mbit/s coded for a 1.0 Mbit/s target. Give the firmware the frame rate rounded to an integer, over 1. The client's timeperframe and G_PARM are not changed, and the session keeps timeperframe exactly (client_fps_num, client_fps_den) for the VUI timing, which must not follow the firmware's rounded rate. A new frame rate written into the Process frame-rate words while streaming stepped the HEVC picture clock backwards. Keep the Start rate there; store a rate set by S_PARM while streaming in the context, not the session (the worker frees the session without vdev_lock), and scale the next Process bit rate and VBR limit by start rate / new rate so the bits per frame follow. An HEVC stream at 30, then 15, then 30 frames per second kept every picture. The CBR filler budget uses the live bit rate and frame rate, and drops a surplus above the buffer size after a rate cut. Key this on a new layout flag, rate_control, set for 6070.11.1: the mode numbering is that firmware's, like rc_mode_fixed_qp beside it. T6000 and T8112 run the same firmware version and were not run with it. They now also offer CBR and VBR; constant quality stays the default, and its frame rate is rounded the same way. Assisted-by: Grok 4.7 Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- drivers/media/platform/apple/ave/Kconfig | 4 +- drivers/media/platform/apple/ave/abi.rst | 23 ++++- .../media/platform/apple/ave/ave-abi-test.c | 85 +++++++++++++++++++ drivers/media/platform/apple/ave/ave-layout.c | 1 + drivers/media/platform/apple/ave/ave-layout.h | 2 + drivers/media/platform/apple/ave/ave-sess.c | 31 ++++++- drivers/media/platform/apple/ave/ave-v4l2.c | 23 ++++- drivers/media/platform/apple/ave/ave-writer.c | 50 +++++++++-- drivers/media/platform/apple/ave/ave.h | 14 ++- 9 files changed, 210 insertions(+), 23 deletions(-) diff --git a/drivers/media/platform/apple/ave/Kconfig b/drivers/media/platform/apple/ave/Kconfig index 36f9c412a5023..40d901acc8c59 100644 --- a/drivers/media/platform/apple/ave/Kconfig +++ b/drivers/media/platform/apple/ave/Kconfig @@ -11,8 +11,8 @@ config VIDEO_APPLE_AVE V4L2 memory-to-memory driver for the AVE H.264/HEVC encoder in Apple T8140 (A18 Pro) SoCs, as in the MacBook Neo. T6000 (M1 Pro) and T8112 (M2) run firmware 6070.11.1 and offer - constant-quality H.264 and HEVC. T6021 (M2 Max) runs a larger - build of that firmware on two encoders. + H.264 and HEVC at constant quality, CBR or VBR. T6021 (M2 Max) + runs a larger build of that firmware on two encoders. T6041 (M4 Pro) has probe-only support. T6050 (M5 Pro/Max) encodes H.264. diff --git a/drivers/media/platform/apple/ave/abi.rst b/drivers/media/platform/apple/ave/abi.rst index 29ace386e49a1..a84619a93b51a 100644 --- a/drivers/media/platform/apple/ave/abi.rst +++ b/drivers/media/platform/apple/ave/abi.rst @@ -27,10 +27,10 @@ retained codec-state partitions and per-slot HEVC slice-header tables. The session allocator provides distinct backing for every live bank and rejects non-16-KiB-aligned IOVAs. -Streaming is enabled for up to two quality-first NV12 constant-quality sessions -per firmware boot, each of 8-bit H.264 (Baseline, Main or High) or HEVC Main, -with up to one B picture per P picture, frame widths in whole macroblocks. -Main 10, bit-rate modes, speed priority, standalone LRME and Reset remain +Streaming is enabled for up to two quality-first NV12 sessions per firmware +boot, each of 8-bit H.264 (Baseline, Main or High) or HEVC Main, with up to one +B picture per P picture, frame widths in whole macroblocks, at constant +quality, CBR or VBR. Main 10, speed priority, standalone LRME and Reset remain unavailable and are refused. No Priority command is sent. Complete is the normal drain barrier; Flush is reserved for cancellation before Stop and Close. KUnit checks the 1080p and 4K fields directly from the clean ledgers and embeds no Apple command image. @@ -58,6 +58,21 @@ the same firmware version and get the same settings, untested: clip came out at 8.4 Mbit/s. With 64, 256 and 64, 8x8 partitions became rare, the same clip came out at 0.30 Mbit/s, and quality 50 took 1.8 times the bits of libx264 at the same PSNR. +- CBR and VBR. Rate-control mode 2, the 9003 CBR value, held the Start QP; mode + 1 moved slice QP with the bit rate. Both CBR and VBR submit mode 1. Without B + pictures the coded rate stayed under the target, at 46% to 96% of it. For CBR + the host pads to the target with filler data; with the filler, CBR at 1.0 and + 2.5 Mbit/s (720p) and 4.0 Mbit/s (1080p) came within 15% of the target over + the file and over its last 150 frames. +- Integer frame rate. With a frame interval of 1000/30000 s every frame sat at + the QP ceiling (about 0.31 Mbit/s coded for a 1.0 Mbit/s target), so the + firmware is given the rounded rate over 1. The session keeps the client's + timeperframe exactly as well, for the VUI timing. +- Frame rate while streaming. A new rate written into the Process frame-rate + words stepped the HEVC picture clock backwards. Instead the next Process bit + rate, and VBR's limit, are scaled by the Start rate over the new one; an HEVC + stream at 30, then 15, then 30 frames per second kept every picture, its + frames at 15 about 1.7 times the size. Lifecycle-only validation ------------------------- diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 8328c5973632e..7ca466e0aad2f 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -1601,6 +1601,90 @@ static void ave_6070_h264_inter(struct kunit *test) } } +/* + * rate_control: CBR and VBR are firmware mode 1, the frame rate is an + * integer, and a frame rate set while streaming scales the Process bit rate. + * The neighbour bank keeps its DMA addresses. + */ +static void ave_6070_rate_control(struct kunit *test) +{ + struct ave_dev *ave = kunit_kzalloc(test, sizeof(*ave), GFP_KERNEL); + struct ave_ctx *ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + struct ave_session *sess = kunit_kzalloc(test, sizeof(*sess), GFP_KERNEL); + u8 *start = kunit_kzalloc(test, 0x32dc8, GFP_KERNEL); + u8 *process = kunit_kzalloc(test, 0x6838, GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, ave); + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_NOT_NULL(test, sess); + KUNIT_ASSERT_NOT_NULL(test, start); + KUNIT_ASSERT_NOT_NULL(test, process); + KUNIT_EXPECT_TRUE(test, ave_layout_6070_11_1.rate_control); + + for (unsigned int codec = AVE_CODEC_H264; codec <= AVE_CODEC_HEVC; + codec++) { + const struct ave_start_layout *sl; + const struct ave_proc_layout *pl; + + ave_test_6070_session(test, ave, ctx, sess, codec, 1280, 720); + sl = &ave->layout->start[codec - 1]; + pl = &ave->layout->process[codec - 1]; + sess->p.rate_control = true; + sess->p.fps_num = 30; + sess->p.fps_den = 1; + sess->p.fps_int = 30; + + /* Constant quality stays constant QP. */ + ctx->fps_now = 0; + ave_test_6070_build(test, ctx, start, process, 1200000); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(start + sl->rc_mode), + ave->layout->start[codec - 1].rc_mode_fixed_qp); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(process + pl->bitrate), 0U); + + sess->p.rc_mode = AVE_RC_CBR; + ave_test_6070_build(test, ctx, start, process, 1200000); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(start + sl->rc_mode), + (u32)AVE_6070_RC_ON); + KUNIT_EXPECT_EQ(test, + get_unaligned_le64(start + sl->neighbour_fw.address), + sess->neighbour_fw[0].dma); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(process + pl->bitrate), + 1200000U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(process + pl->fps_int), + 30U); + + /* 60 fps after Start: half the bits per frame, same clock. */ + ctx->fps_now = 60; + ave_test_6070_build(test, ctx, start, process, 1200000); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(process + pl->bitrate), + 600000U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(process + pl->fps_int), + 30U); + + /* VBR's limit follows the live rate: twice it, per second. */ + sess->p.rc_mode = AVE_RC_VBR; + ctx->fps_now = 30; + ctx->bitrate_peak = 0; + ave_test_6070_build(test, ctx, start, process, 2000000); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(start + sl->rc_mode), + (u32)AVE_6070_RC_ON); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(process + pl->limits), 1U); + /* 500000.0 as an IEEE 754 double */ + KUNIT_EXPECT_EQ(test, get_unaligned_le64(process + pl->limit_bytes), + 0x411e848000000000ULL); + + /* Without rate_control nothing is scaled. */ + sess->p.rate_control = false; + sess->p.rc_mode = AVE_RC_CBR; + ctx->fps_now = 60; + ave_test_6070_build(test, ctx, start, process, 1200000); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(start + sl->rc_mode), + (u32)AVE_RC_CBR); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(process + pl->bitrate), + 1200000U); + } +} + /* * The 6070 commands upstream qualified on T6000 and T8112, byte for byte. * These CRCs pin the Start and first two Process commands the builder @@ -1703,6 +1787,7 @@ static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_6070_start_process_fields), KUNIT_CASE(ave_6070_qualified_bytes), KUNIT_CASE(ave_6070_h264_inter), + KUNIT_CASE(ave_6070_rate_control), KUNIT_CASE(ave_6070_reordered_pictures), KUNIT_CASE(ave_6070_hevc_contract), KUNIT_CASE(ave_6070_entropy_interleave), diff --git a/drivers/media/platform/apple/ave/ave-layout.c b/drivers/media/platform/apple/ave/ave-layout.c index dd014958ed883..3c56f48cbfb9d 100644 --- a/drivers/media/platform/apple/ave/ave-layout.c +++ b/drivers/media/platform/apple/ave/ave-layout.c @@ -555,6 +555,7 @@ const struct ave_fw_layout ave_layout_6070_11_1 = { .max_sessions = 2, .codec_state_bytes = { 0x984, 0x5884 }, .h264_inter = true, + .rate_control = true, .start = { [0] = { .header = AVE_SURFACE(0x40, 0x10), diff --git a/drivers/media/platform/apple/ave/ave-layout.h b/drivers/media/platform/apple/ave/ave-layout.h index b0f23894799d3..61c01e08fed2d 100644 --- a/drivers/media/platform/apple/ave/ave-layout.h +++ b/drivers/media/platform/apple/ave/ave-layout.h @@ -390,6 +390,8 @@ struct ave_fw_layout { */ /* H.264 P pictures are references; inter decisions are weighted. */ bool h264_inter; + /* CBR and VBR run the firmware rate controller. */ + bool rate_control; bool counted; }; diff --git a/drivers/media/platform/apple/ave/ave-sess.c b/drivers/media/platform/apple/ave/ave-sess.c index f522768d236d1..3eb56dd6c6b04 100644 --- a/drivers/media/platform/apple/ave/ave-sess.c +++ b/drivers/media/platform/apple/ave/ave-sess.c @@ -141,7 +141,20 @@ static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) p->fps_num = ctx->timeperframe.denominator; p->fps_den = ctx->timeperframe.numerator; p->fps_int = max(DIV_ROUND_CLOSEST(p->fps_num, p->fps_den), 1U); + p->client_fps_num = p->fps_num; + p->client_fps_den = p->fps_den; p->h264_inter = layout->h264_inter; + p->rate_control = layout->rate_control; + /* + * The rate controller divides the bit rate by the frame-rate + * numerator, so 1000/30000 s would divide it by 30000. Give it the + * rounded rate. The client's timeperframe is not changed, and + * client_fps_num/den keep it exactly, for the VUI timing. + */ + if (p->rate_control) { + p->fps_num = p->fps_int; + p->fps_den = 1; + } if (p->codec == AVE_CODEC_H264) { switch (ctx->h264_profile) { @@ -248,7 +261,8 @@ static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) p->limit_bytes = (ctx->bitrate_peak ? max(ctx->bitrate_peak, ctx->bitrate) : 2ULL * ctx->bitrate) / 8; } - if (ctx->ave->layout->kind == AVE_LAYOUT_6070 && p->rc_mode != AVE_RC_CQ) + if (ctx->ave->layout->kind == AVE_LAYOUT_6070 && !p->rate_control && + p->rc_mode != AVE_RC_CQ) return -EOPNOTSUPP; level = ave_pick_level(p, p->codec == AVE_CODEC_H264 ? ctx->h264_level : @@ -1187,13 +1201,22 @@ static size_t ave_sess_cbr_fill(struct ave_ctx *ctx, u8 *out, size_t len, size_t struct ave_session *sess = ctx->sess; const struct ave_params *p = &sess->p; bool hevc = p->codec == AVE_CODEC_HEVC; - s64 budget = div_u64((u64)p->bitrate * p->fps_den, 8ULL * p->fps_num); - s64 vbv = p->bitrate / 16; /* 0.5 s of bit rate, in bytes */ - s64 excess, pad; + u32 bitrate = p->bitrate, fps_num = p->fps_num; + s64 budget, vbv, excess, pad; u8 *f; if (p->rc_mode != AVE_RC_CBR) return len; + /* The firmware follows a bit rate or frame rate set while streaming. */ + if (p->rate_control) { + bitrate = READ_ONCE(ctx->bitrate) ?: bitrate; + fps_num = READ_ONCE(ctx->fps_now) ?: fps_num; + } + budget = div_u64((u64)bitrate * p->fps_den, 8ULL * fps_num); + vbv = bitrate / 16; /* 0.5 s of bit rate, in bytes */ + /* After a drop, the old rate's buffer must not come out as filler. */ + if (p->rate_control && sess->cbr_surplus > vbv) + sess->cbr_surplus = vbv; if (fw_filler && !sess->fw_fills) { sess->fw_fills = true; dev_dbg(ctx->ave->dev, "CBR: the firmware writes filler data\n"); diff --git a/drivers/media/platform/apple/ave/ave-v4l2.c b/drivers/media/platform/apple/ave/ave-v4l2.c index 0fb302d81654a..a6a079c5a67ff 100644 --- a/drivers/media/platform/apple/ave/ave-v4l2.c +++ b/drivers/media/platform/apple/ave/ave-v4l2.c @@ -406,6 +406,13 @@ static int ave_s_parm(struct file *file, void *priv, struct v4l2_streamparm *a) *t = (struct v4l2_fract){ 1, 1 }; ctx->timeperframe = *t; a->parm.output.capability = V4L2_CAP_TIMEPERFRAME; + /* + * The firmware keeps the frame rate from Start. A rate set while + * streaming scales the bit rate each Process asks for instead. + */ + if (ctx->ave->layout && ctx->ave->layout->rate_control) + WRITE_ONCE(ctx->fps_now, + max(DIV_ROUND_CLOSEST(t->denominator, t->numerator), 1U)); return 0; } @@ -576,11 +583,19 @@ static int ave_init_ctrls(struct ave_ctx *ctx) v4l2_ctrl_handler_init(hdl, 19); /* - * The 6070 firmware path (M1 Pro/Max, M2) is qualified for constant - * quality only: offer just that mode, so a client that keeps the - * defaults (GStreamer, FFmpeg) gets a session instead of a refusal. + * A 6070 layout without rate_control runs constant quality only: + * offer just that mode, so a client that keeps the defaults + * (GStreamer, FFmpeg) gets a session instead of a refusal. With + * rate_control it also offers CBR and VBR; the default stays CQ. */ - if (ctx->ave->layout && ctx->ave->layout->kind == AVE_LAYOUT_6070) + if (ctx->ave->layout && ctx->ave->layout->rate_control) + v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_MPEG_VIDEO_BITRATE_MODE, + V4L2_MPEG_VIDEO_BITRATE_MODE_CQ, + ~(BIT(V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) | + BIT(V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) | + BIT(V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)), + V4L2_MPEG_VIDEO_BITRATE_MODE_CQ); + else if (ctx->ave->layout && ctx->ave->layout->kind == AVE_LAYOUT_6070) v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_MPEG_VIDEO_BITRATE_MODE, V4L2_MPEG_VIDEO_BITRATE_MODE_CQ, ~BIT(V4L2_MPEG_VIDEO_BITRATE_MODE_CQ), diff --git a/drivers/media/platform/apple/ave/ave-writer.c b/drivers/media/platform/apple/ave/ave-writer.c index 244f96986dd43..02e3d733ed3d5 100644 --- a/drivers/media/platform/apple/ave/ave-writer.c +++ b/drivers/media/platform/apple/ave/ave-writer.c @@ -788,9 +788,13 @@ static void ave_start_config(const struct ave_params *p, u8 *cmd, /* * Constant QP is mode 3 on 9003 and mode 2 on 6070. With 3 the 6070 * firmware accepts Start but has no rate control for the session and - * stops on the first Process. + * stops on the first Process. 6070 CBR and VBR are both mode 1: the + * 9003 CBR value, 2, would select constant QP there. */ - rc_mode = cq && l->rc_mode_fixed_qp ? l->rc_mode_fixed_qp : p->rc_mode; + if (p->rate_control && !cq) + rc_mode = AVE_6070_RC_ON; + else + rc_mode = cq && l->rc_mode_fixed_qp ? l->rc_mode_fixed_qp : p->rc_mode; ave_start_put32(cmd, l->rc_mode, rc_mode); ave_start_put32(cmd, l->bitrate, cq && fw->kind == AVE_LAYOUT_6070 ? 0 : p->bitrate); @@ -1116,6 +1120,21 @@ int ave_build_start(struct ave_ctx *ctx, u8 *cmd, size_t len) #define AVE_PROC_FORCED_TYPE 2 #define AVE_PROC_LOW_DELAY 3 +/* + * With rate_control the firmware keeps the frame rate from Start and spends + * bit rate / that rate on each frame. After S_PARM the rate it is given is + * scaled so the bits per frame match the new frame rate. + */ +static u32 ave_live_rate(const struct ave_ctx *ctx, u32 rate) +{ + const struct ave_params *p = &ctx->sess->p; + u32 now = READ_ONCE(ctx->fps_now); + + if (!p->rate_control || !now || now == p->fps_int) + return rate; + return min_t(u64, div_u64((u64)rate * p->fps_int, now), U32_MAX); +} + int ave_build_process(struct ave_ctx *ctx, u8 *cmd, size_t len, unsigned int slot, dma_addr_t input, u32 bitrate, bool force_key, bool last) @@ -1257,12 +1276,27 @@ int ave_build_process(struct ave_ctx *ctx, u8 *cmd, size_t len, /* The bit rate may change while streaming; it takes effect here. */ if (l->bitrate) ave_put32(cmd, l->bitrate, - p->rc_mode == 3 && fw->kind == AVE_LAYOUT_6070 ? 0 : bitrate); - if (l->limits) - ave_put32(cmd, l->limits, p->limit_bytes ? 1 : 0); - if (l->limit_bytes && p->limit_bytes) { - ave_put64(cmd, l->limit_bytes, ave_f64(p->limit_bytes, 1)); - ave_put64(cmd, l->limit_secs, ave_f64(1, 1)); + p->rc_mode == 3 && fw->kind == AVE_LAYOUT_6070 ? 0 : + ave_live_rate(ctx, bitrate)); + if (p->rate_control && p->rc_mode == AVE_RC_VBR) { + /* VBR's data-rate limit follows the bit rate too. */ + u32 peak = READ_ONCE(ctx->bitrate_peak); + u64 limit = (peak ? max(peak, bitrate) : 2ULL * bitrate) / 8; + + if (l->limits) + ave_put32(cmd, l->limits, 1); + if (l->limit_bytes) { + ave_put64(cmd, l->limit_bytes, + ave_f64(ave_live_rate(ctx, min_t(u64, limit, U32_MAX)), 1)); + ave_put64(cmd, l->limit_secs, ave_f64(1, 1)); + } + } else { + if (l->limits) + ave_put32(cmd, l->limits, p->limit_bytes ? 1 : 0); + if (l->limit_bytes && p->limit_bytes) { + ave_put64(cmd, l->limit_bytes, ave_f64(p->limit_bytes, 1)); + ave_put64(cmd, l->limit_secs, ave_f64(1, 1)); + } } if (l->frame_type) ave_put32(cmd, l->frame_type, pic.type); diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index 5714484e4a19f..38b67668b59bd 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -40,6 +40,11 @@ struct ave_mctf_layout; #define AVE_RC_CBR 2 #define AVE_RC_CQ 3 #define AVE_RC_VBR 4 +/* + * 6070 rate-control modes: 1 runs the rate controller, for CBR and VBR + * alike; 2 (the 9003 CBR value) holds the Start QP. + */ +#define AVE_6070_RC_ON 1 #define AVE_MAX_SLOTS 7 /* Sessions one firmware boot serves at once (macOS was seen running two). */ @@ -393,10 +398,12 @@ struct ave_params { u32 stride; /* NV12 or P010 line stride in bytes, both planes */ u32 sample_bytes; /* 1 for NV12, 2 for P010 */ u32 mbs; /* 16x16 macroblocks */ - u32 fps_num, fps_den; /* frames per second = num / den */ + u32 fps_num, fps_den; /* frames per second = num / den, as the firmware gets it */ u32 fps_int; /* the same, rounded */ + u32 client_fps_num, client_fps_den; /* the client's timeperframe, exactly */ /* The layout's encode settings, see struct ave_fw_layout. */ bool h264_inter; + bool rate_control; u32 slots; u32 refs; /* max_num_ref_frames + 1 */ bool bframes; @@ -532,6 +539,11 @@ struct ave_ctx { u32 bitrate_mode; u32 quality; /* V4L2 constant quality, 0..100 */ + /* + * Frame rate from the last S_PARM, rounded, with rate_control. It is + * kept here because the worker frees the session without vdev_lock. + */ + u32 fps_now; u32 bitrate; u32 bitrate_peak; u32 gop_size; From 5ac023072f70dcfec8f27f6da70e14ccd0545463 Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Thu, 19 Nov 2026 14:44:28 -0300 Subject: [PATCH 10/13] media: apple: ave: encode 6070.11.1 one frame at a time by default Default the B-frames control to 0 on a low_delay layout, and without B-frames wait for each frame's reply before submitting the next, as for the first frame of every session. A second session on the same encoder waits behind that frame. Set it for 6070.11.1. Every T6021 measurement in this series was made this way; submitting frames ahead without B-frames was not measured on any 6070.11.1 SoC. On T6000 and T8112 this changes the defaults: B-frames 3 to 0, and one frame in the firmware at a time. A client that asks for B-frames still gets them and their pipelining. Assisted-by: Grok 4.7 Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- drivers/media/platform/apple/ave/abi.rst | 4 ++++ drivers/media/platform/apple/ave/ave-abi-test.c | 2 ++ drivers/media/platform/apple/ave/ave-layout.c | 1 + drivers/media/platform/apple/ave/ave-layout.h | 2 ++ drivers/media/platform/apple/ave/ave-sess.c | 1 + drivers/media/platform/apple/ave/ave-v4l2.c | 17 ++++++++++++----- drivers/media/platform/apple/ave/ave.h | 1 + 7 files changed, 23 insertions(+), 5 deletions(-) diff --git a/drivers/media/platform/apple/ave/abi.rst b/drivers/media/platform/apple/ave/abi.rst index a84619a93b51a..823e95f09d20b 100644 --- a/drivers/media/platform/apple/ave/abi.rst +++ b/drivers/media/platform/apple/ave/abi.rst @@ -73,6 +73,10 @@ the same firmware version and get the same settings, untested: rate, and VBR's limit, are scaled by the Start rate over the new one; an HEVC stream at 30, then 15, then 30 frames per second kept every picture, its frames at 15 about 1.7 times the size. +- One frame at a time. B pictures default to off, and without them each frame + is answered before the next is submitted. Every T6021 result above was + measured this way; submitting frames ahead without B pictures was not + measured on 6070.11.1. Lifecycle-only validation ------------------------- diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 7ca466e0aad2f..c4aabc8b1bf89 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -1439,6 +1439,8 @@ static void ave_6070_variants(struct kunit *test) KUNIT_EXPECT_FALSE(test, ave_t8112.config_shared); /* One 6070.11.1 boot serves two sessions, on every SoC. */ KUNIT_EXPECT_EQ(test, ave_layout_6070_11_1.max_sessions, 2U); + /* One frame at a time by default. */ + KUNIT_EXPECT_TRUE(test, ave_layout_6070_11_1.low_delay); KUNIT_EXPECT_EQ(test, ave_t6000.regs->ctrl, 0x400044U); KUNIT_EXPECT_EQ(test, ave_t8112.regs->timebase, 0x178000U); KUNIT_EXPECT_GE(test, t->command_bytes, 0x32dc8U); diff --git a/drivers/media/platform/apple/ave/ave-layout.c b/drivers/media/platform/apple/ave/ave-layout.c index 3c56f48cbfb9d..255ebc5c7471d 100644 --- a/drivers/media/platform/apple/ave/ave-layout.c +++ b/drivers/media/platform/apple/ave/ave-layout.c @@ -556,6 +556,7 @@ const struct ave_fw_layout ave_layout_6070_11_1 = { .codec_state_bytes = { 0x984, 0x5884 }, .h264_inter = true, .rate_control = true, + .low_delay = true, .start = { [0] = { .header = AVE_SURFACE(0x40, 0x10), diff --git a/drivers/media/platform/apple/ave/ave-layout.h b/drivers/media/platform/apple/ave/ave-layout.h index 61c01e08fed2d..0e9c8bf104f5f 100644 --- a/drivers/media/platform/apple/ave/ave-layout.h +++ b/drivers/media/platform/apple/ave/ave-layout.h @@ -392,6 +392,8 @@ struct ave_fw_layout { bool h264_inter; /* CBR and VBR run the firmware rate controller. */ bool rate_control; + /* No B-frames by default; each frame is answered before the next. */ + bool low_delay; bool counted; }; diff --git a/drivers/media/platform/apple/ave/ave-sess.c b/drivers/media/platform/apple/ave/ave-sess.c index 3eb56dd6c6b04..ce455b139f236 100644 --- a/drivers/media/platform/apple/ave/ave-sess.c +++ b/drivers/media/platform/apple/ave/ave-sess.c @@ -145,6 +145,7 @@ static int ave_sess_params(struct ave_ctx *ctx, struct ave_params *p) p->client_fps_den = p->fps_den; p->h264_inter = layout->h264_inter; p->rate_control = layout->rate_control; + p->low_delay = layout->low_delay; /* * The rate controller divides the bit rate by the frame-rate * numerator, so 1000/30000 s would divide it by 30000. Give it the diff --git a/drivers/media/platform/apple/ave/ave-v4l2.c b/drivers/media/platform/apple/ave/ave-v4l2.c index a6a079c5a67ff..719537ebe1120 100644 --- a/drivers/media/platform/apple/ave/ave-v4l2.c +++ b/drivers/media/platform/apple/ave/ave-v4l2.c @@ -619,8 +619,10 @@ static int ave_init_ctrls(struct ave_ctx *ctx) /* * The firmware either uses up to three B-frames (reordering them * itself) or none. Baseline and intra-only H.264 never have any. + * A low_delay layout defaults to none. */ - v4l2_ctrl_new_std(hdl, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 3, 3, 3); + v4l2_ctrl_new_std(hdl, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 3, 3, + ctx->ave->layout && ctx->ave->layout->low_delay ? 0 : 3); v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_MPEG_VIDEO_HEADER_MODE, V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME, BIT(V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE), @@ -1247,12 +1249,17 @@ static int ave_encode_one(struct ave_ctx *ctx) return ret; } - /* Like macOS: the first frame goes alone, then a Complete. */ - if (sess->frames == 1) { + /* + * Like macOS: the first frame goes alone, then a Complete. Without + * B-frames a low_delay layout waits for every frame, so each one is + * answered before the next goes in; a second session on the same + * core waits behind it. + */ + if (sess->frames == 1 || (sess->p.low_delay && !sess->p.bframes)) { ret = ave_ipc_pump(ave, AVE_FRAME_WAIT_MS, ave_frame_answered, &sess->slot[slot]); - if (!ret) + if (!ret && sess->frames == 1) ret = ave_cmd_complete(ctx, 1); - if (!ret) + if (!ret && sess->frames == 1) ret = ave_ipc_settle(ave, AVE_FRAME_WAIT_MS); if (ret) return ret; diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index 38b67668b59bd..4fb4111fd1d7a 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -404,6 +404,7 @@ struct ave_params { /* The layout's encode settings, see struct ave_fw_layout. */ bool h264_inter; bool rate_control; + bool low_delay; u32 slots; u32 refs; /* max_num_ref_frames + 1 */ bool bframes; From 4f6e71b51956f7cb7d8ea26d343799983972fed0 Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Thu, 19 Nov 2026 14:48:43 -0300 Subject: [PATCH 11/13] media: apple: ave: add VUI timing to 6070 parameter sets that lack it 6070 firmware writes the parameter sets to a buffer of their own. On a J475c (T6021) the SPS there had no VUI timing, and with B-frames off no VUI at all, so a raw Annex B reader guessed 25 fps. When an SPS from that buffer has no timing_info, rewrite it in the output buffer: set timing in an existing VUI, or add a minimal one. The timing is the client's timeperframe exactly, not the integer rate the firmware gets: H.264 gets num_units_in_tick equal to the denominator and time_scale twice the numerator, with fixed_frame_rate_flag, which is how H.264 codes a frame rate; HEVC gets vui_num_units_in_tick equal to the denominator and vui_time_scale equal to the numerator. The rate is taken in lowest terms, so 60/2 gives the bytes of 30/1, and 30000/1001 gives 1001 and 60000 (H.264) or 1001 and 30000 (HEVC). ffprobe on a raw H.264 stream prints the field rate (60/1 at 30 fps) and an MP4 mux reports 30/1. When twice the numerator does not fit 32 bits, H.264 halves an even denominator instead; with an odd one there is no exact form and the SPS is left as written. The rewrite is keyed on the stream, not on a SoC or firmware flag: an SPS that already has timing is left alone, and so is one this walk cannot follow (scaling lists, POC type 1, more than one HEVC sub-layer, more than 256 bytes). Firmware that puts its parameter sets in the coded buffer (9003, 26A428) is not touched. The SPS is read with the RBSP reader of the T8142 slice-type probe in aurora-silicon/linux#2218 (struct ave_rbsp, ave_rbsp_init(), ave_rbsp_bit(), ave_rbsp_ue()), with ave_rbsp_get() and ave_rbsp_get_ue() added and the buffer raised to 256 bytes so a whole SPS fits. Whichever of the two lands second drops its copy. Assisted-by: Grok 4.7 Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- drivers/media/platform/apple/ave/abi.rst | 14 + .../media/platform/apple/ave/ave-abi-test.c | 114 +++ drivers/media/platform/apple/ave/ave-sess.c | 738 ++++++++++++++++++ drivers/media/platform/apple/ave/ave.h | 5 + 4 files changed, 871 insertions(+) diff --git a/drivers/media/platform/apple/ave/abi.rst b/drivers/media/platform/apple/ave/abi.rst index 823e95f09d20b..2b1c2dfe90399 100644 --- a/drivers/media/platform/apple/ave/abi.rst +++ b/drivers/media/platform/apple/ave/abi.rst @@ -78,6 +78,20 @@ the same firmware version and get the same settings, untested: measured this way; submitting frames ahead without B pictures was not measured on 6070.11.1. +6070 firmware writes the parameter sets to a buffer of their own. On T6021 the +SPS there had no VUI timing, and with B pictures off no VUI at all, so a raw +Annex B reader guessed 25 fps. The driver adds timing to an SPS from that buffer +that has none, from the client's timeperframe exactly, not from the integer +rate the firmware gets: H.264 num_units_in_tick the denominator and time_scale +twice the numerator, with fixed_frame_rate_flag; HEVC vui_num_units_in_tick the +denominator and vui_time_scale the numerator. The rate is taken in lowest +terms, so 30/1 gives 1 and 60 (H.264) or 1 and 30 (HEVC), and 30000/1001 gives +1001 and 60000 or 1001 and 30000. ffprobe on a raw H.264 stream prints the +field rate (60/1 at 30 fps, 60000/1001 at 30000/1001; an MP4 mux reports +30/1 and 30000/1001). When twice the numerator does not fit 32 bits, an even +denominator is halved instead; otherwise the SPS is left as written. An SPS +that has timing, or that the walk cannot follow, is left as written. + Lifecycle-only validation ------------------------- diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index c4aabc8b1bf89..276517502510b 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -1779,6 +1780,118 @@ static void ave_quarantine_count(struct kunit *test) KUNIT_EXPECT_NULL(test, ave->failed_sessions); } +/* + * The SPS timing rewrite, on parameter sets the T6021 firmware wrote. The + * rate is the client's timeperframe, num/den frames per second. The expected + * bytes for rates other than 30/1 are the 30/1 bytes with the two timing + * words replaced (num_units_in_tick, time_scale), then escaped again. + */ +static void ave_sps_timing(struct kunit *test) +{ + static const struct { + bool hevc; + u32 num, den; + const char *in, *out; + } cases[] = { + /* H.264 without VUI: a minimal VUI with timing is added. */ + { false, 30, 1, "274d001fda014016e4", + "274d001fda014016e840000003004000000f21" }, + /* H.264 with VUI: timing goes in, the rest of the VUI moves. */ + { false, 30, 1, "274d0028e5603c0113f2c03c20104e", + "274d0028e5603c0113f2c2000003000200000300791e100827" }, + /* HEVC without VUI. */ + { + true, 30, 1, + "420101016000000300b0000003000003005da00280802d165aee4522ae5cb97040", + "420101016000000300b0000003000003005da00280802d165aee4522ae5cb9710080000003008000000f04", + }, + /* A PPS is left alone. */ + { false, 30, 1, "68ee3c80", "68ee3c80" }, + /* So is an SPS that already carries timing. */ + { false, 30, 1, "274d001fda014016e840000003004000000f21", + "274d001fda014016e840000003004000000f21" }, + { false, 30, 1, "274d0028e5603c0113f2c2000003000200000300791e100827", + "274d0028e5603c0113f2c2000003000200000300791e100827" }, + { + true, 30, 1, + "420101016000000300b0000003000003005da00280802d165aee4522ae5cb9710080000003008000000f04", + "420101016000000300b0000003000003005da00280802d165aee4522ae5cb9710080000003008000000f04", + }, + /* + * 30000/1001: num_units_in_tick 1001 and time_scale 60000 for + * H.264 (0x3e9, 0xea60), 1001 and 30000 for HEVC. + */ + { false, 30000, 1001, "274d001fda014016e4", + "274d001fda014016e8400000fa40003a9821" }, + { false, 30000, 1001, "274d0028e5603c0113f2c03c20104e", + "274d0028e5603c0113f2c2000007d20001d4c11e100827" }, + { + true, 30000, 1001, + "420101016000000300b0000003000003005da00280802d165aee4522ae5cb97040", + "420101016000000300b0000003000003005da00280802d165aee4522ae5cb97100800001f480003a9804", + }, + /* Lowest terms: 60/2 and 1000/30000 s give the 30/1 bytes. */ + { false, 60, 2, "274d001fda014016e4", + "274d001fda014016e840000003004000000f21" }, + { false, 30000, 1000, "274d0028e5603c0113f2c03c20104e", + "274d0028e5603c0113f2c2000003000200000300791e100827" }, + { + true, 60, 2, + "420101016000000300b0000003000003005da00280802d165aee4522ae5cb97040", + "420101016000000300b0000003000003005da00280802d165aee4522ae5cb9710080000003008000000f04", + }, + /* + * 32-bit limits. H.264's time_scale is twice the numerator: + * 3000000001/2 does not fit, so the even denominator is halved + * (num_units_in_tick 1, time_scale 3000000001); 3000000001/3 + * has no such form and the SPS is left as written. HEVC takes + * the numerator as it is. + */ + { false, 3000000001U, 2, "274d001fda014016e4", + "274d001fda014016e840000003006cb4178061" }, + { false, 3000000001U, 3, "274d001fda014016e4", "274d001fda014016e4" }, + { + true, 4000000000U, 3, + "420101016000000300b0000003000003005da00280802d165aee4522ae5cb97040", + "420101016000000300b0000003000003005da00280802d165aee4522ae5cb971008000000301f735940004", + }, + /* A zero rate stamps nothing. */ + { false, 0, 1, "274d001fda014016e4", "274d001fda014016e4" }, + { false, 30, 0, "274d001fda014016e4", "274d001fda014016e4" }, + }; + u8 *buf = kunit_kzalloc(test, 256, GFP_KERNEL); + u8 *want = kunit_kzalloc(test, 256, GFP_KERNEL); + size_t short_len; + + KUNIT_ASSERT_NOT_NULL(test, buf); + KUNIT_ASSERT_NOT_NULL(test, want); + for (unsigned int i = 0; i < ARRAY_SIZE(cases); i++) { + size_t in = strlen(cases[i].in) / 2; + size_t out = strlen(cases[i].out) / 2; + size_t len = 4 + in; + + memset(buf, 0, 256); + buf[3] = 1; + KUNIT_ASSERT_EQ(test, hex2bin(buf + 4, cases[i].in, in), 0); + KUNIT_ASSERT_EQ(test, hex2bin(want, cases[i].out, out), 0); + if (cases[i].hevc) + ave_hevc_stamp_timing(buf, 256, &len, cases[i].num, + cases[i].den); + else + ave_h264_stamp_timing(buf, 256, &len, cases[i].num, + cases[i].den); + KUNIT_EXPECT_EQ(test, len, 4 + out); + KUNIT_EXPECT_MEMEQ(test, buf + 4, want, out); + } + /* No room to grow: the buffer is not touched. */ + memset(buf, 0, 256); + buf[3] = 1; + short_len = 4 + strlen(cases[0].in) / 2; + KUNIT_ASSERT_EQ(test, hex2bin(buf + 4, cases[0].in, short_len - 4), 0); + ave_h264_stamp_timing(buf, short_len, &short_len, 30, 1); + KUNIT_EXPECT_EQ(test, short_len, 4 + strlen(cases[0].in) / 2); +} + static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_t6041_text_boot_word), KUNIT_CASE(ave_9003_byte_equality), KUNIT_CASE(ave_9003_hevc_main10), @@ -1790,6 +1903,7 @@ static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_6070_qualified_bytes), KUNIT_CASE(ave_6070_h264_inter), KUNIT_CASE(ave_6070_rate_control), + KUNIT_CASE(ave_sps_timing), KUNIT_CASE(ave_6070_reordered_pictures), KUNIT_CASE(ave_6070_hevc_contract), KUNIT_CASE(ave_6070_entropy_interleave), diff --git a/drivers/media/platform/apple/ave/ave-sess.c b/drivers/media/platform/apple/ave/ave-sess.c index ce455b139f236..4c9c73eaa1b60 100644 --- a/drivers/media/platform/apple/ave/ave-sess.c +++ b/drivers/media/platform/apple/ave/ave-sess.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -1112,6 +1113,734 @@ static bool ave_starts_annexb(const u8 *p, size_t len) (len >= 4 && !p[0] && !p[1] && !p[2] && p[3] == 1); } +/* Annex B start code length at @i, or 0. */ +static unsigned int ave_start_code(const u8 *p, size_t len, size_t i) +{ + if (i + 4 <= len && !p[i] && !p[i + 1] && !p[i + 2] && p[i + 3] == 1) + return 4; + if (i + 3 <= len && !p[i] && !p[i + 1] && p[i + 2] == 1) + return 3; + return 0; +} + +/* + * A NAL unit's RBSP, read a bit at a time: ave_rbsp_init() copies up to + * AVE_RBSP_BYTES of the payload after the NAL header, without + * emulation-prevention bytes. A read past the bytes copied gives -1. + */ +#define AVE_RBSP_BYTES 256 + +struct ave_rbsp { + u8 b[AVE_RBSP_BYTES]; + unsigned int n, pos; /* bytes in b; next bit */ +}; + +static void ave_rbsp_init(struct ave_rbsp *r, const u8 *p, size_t len) +{ + unsigned int zeros = 0; + + r->n = 0; + r->pos = 0; + for (size_t i = 0; i < len && r->n < sizeof(r->b); i++) { + if (zeros >= 2 && p[i] == 3) { + zeros = 0; + continue; + } + zeros = p[i] ? 0 : zeros + 1; + r->b[r->n++] = p[i]; + } +} + +static int ave_rbsp_bit(struct ave_rbsp *r) +{ + int bit; + + if (r->pos >= r->n * 8) + return -1; + bit = (r->b[r->pos / 8] >> (7 - r->pos % 8)) & 1; + r->pos++; + return bit; +} + +/* @n (at most 32) bits as an unsigned number in *v; -1 past the bytes read. */ +static int ave_rbsp_get(struct ave_rbsp *r, unsigned int n, s64 *v) +{ + u64 acc = 0; + + for (unsigned int i = 0; i < n; i++) { + int bit = ave_rbsp_bit(r); + + if (bit < 0) + return -1; + acc = acc << 1 | bit; + } + *v = acc; + return 0; +} + +/* ue(v), or -1 past the bytes read or above 31 leading zeros. */ +static s64 ave_rbsp_ue(struct ave_rbsp *r) +{ + unsigned int zeros = 0; + u64 v = 0; + int bit; + + while ((bit = ave_rbsp_bit(r)) == 0) + if (++zeros > 31) + return -1; + if (bit < 0) + return -1; + for (unsigned int i = 0; i < zeros; i++) { + bit = ave_rbsp_bit(r); + if (bit < 0) + return -1; + v = v << 1 | bit; + } + return (s64)((1ULL << zeros) - 1 + v); +} + +/* ue(v) in *v, as ave_rbsp_ue(). */ +static int ave_rbsp_get_ue(struct ave_rbsp *r, s64 *v) +{ + *v = ave_rbsp_ue(r); + return *v < 0 ? -1 : 0; +} + +static int ave_bit_put(u8 *b, size_t cap_bits, size_t *pos, u32 v, unsigned int n) +{ + if (!n || n > 32 || *pos + n > cap_bits) + return -1; + for (unsigned int i = 0; i < n; i++) { + size_t p = *pos + i; + unsigned int bit = (v >> (n - 1 - i)) & 1; + + if (!bit) + b[p / 8] &= ~(1u << (7 - (p & 7))); + else + b[p / 8] |= 1u << (7 - (p & 7)); + } + *pos += n; + return 0; +} + +/* Copy @n bits of @src, from bit @from, to @dst. */ +static int ave_bit_copy(u8 *dst, size_t cap_bits, size_t *dpos, + struct ave_rbsp *src, size_t from, size_t n) +{ + src->pos = from; + for (size_t i = 0; i < n; i++) { + int bit = ave_rbsp_bit(src); + + if (bit < 0 || ave_bit_put(dst, cap_bits, dpos, bit, 1)) + return -1; + } + return 0; +} + +static int ave_rbsp_escape(const u8 *rbsp, size_t rbsp_len, u8 *nal, size_t cap, + size_t *nal_len) +{ + size_t o = 0; + unsigned int zeros = 0; + + for (size_t i = 0; i < rbsp_len; i++) { + if (zeros >= 2 && rbsp[i] <= 3) { + if (o >= cap) + return -1; + nal[o++] = 3; + zeros = 0; + } + if (o >= cap) + return -1; + nal[o++] = rbsp[i]; + zeros = rbsp[i] ? 0 : zeros + 1; + } + *nal_len = o; + return 0; +} + +/* Bit index of rbsp_stop_one_bit: the last bit set in the RBSP. */ +static int ave_rbsp_stop_bit(const u8 *rbsp, size_t nbytes, size_t *bit) +{ + while (nbytes && !rbsp[nbytes - 1]) + nbytes--; + if (!nbytes) + return -1; + *bit = nbytes * 8 - 1 - __ffs(rbsp[nbytes - 1]); + return 0; +} + +/* + * Bit index of timing_info_present_flag, and whether VUI is present. + * High profile is walked only when its scaling lists are absent. + */ +static int ave_h264_timing_bit(struct ave_rbsp *r, size_t *bit, bool *present) +{ + s64 profile, v; + + *present = false; + if (ave_rbsp_get(r, 8, &profile) || + ave_rbsp_get(r, 8, &v) || + ave_rbsp_get(r, 8, &v) || + ave_rbsp_get_ue(r, &v)) + return -1; + if (profile == 100 || profile == 110 || profile == 122 || profile == 244 || + profile == 44 || profile == 83 || profile == 86 || profile == 118 || + profile == 128 || profile == 138 || profile == 139 || profile == 134 || + profile == 135) { + s64 chroma; + + if (ave_rbsp_get_ue(r, &chroma)) + return -1; + if (chroma == 3 && ave_rbsp_get(r, 1, &v)) + return -1; + if (ave_rbsp_get_ue(r, &v) || + ave_rbsp_get_ue(r, &v) || + ave_rbsp_get(r, 1, &v) || + ave_rbsp_get(r, 1, &v)) + return -1; + if (v) + return -1; /* scaling lists: leave the SPS alone */ + } + if (ave_rbsp_get_ue(r, &v)) + return -1; + if (ave_rbsp_get_ue(r, &v)) + return -1; + if (v == 0) { + if (ave_rbsp_get_ue(r, &v)) + return -1; + } else if (v != 2) { + return -1; + } + /* max_num_ref_frames, gaps, width, height */ + if (ave_rbsp_get_ue(r, &v) || + ave_rbsp_get(r, 1, &v) || + ave_rbsp_get_ue(r, &v) || + ave_rbsp_get_ue(r, &v) || + ave_rbsp_get(r, 1, &v)) + return -1; + if (!v && ave_rbsp_get(r, 1, &v)) + return -1; /* mb_adaptive_frame_field_flag */ + if (ave_rbsp_get(r, 1, &v)) + return -1; /* direct_8x8_inference_flag */ + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v) { + for (int i = 0; i < 4; i++) + if (ave_rbsp_get_ue(r, &v)) + return -1; + } + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (!v) { + *bit = r->pos - 1; /* vui_parameters_present_flag */ + return 0; + } + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v) { + s64 idc; + + if (ave_rbsp_get(r, 8, &idc)) + return -1; + if (idc == 255 && + (ave_rbsp_get(r, 16, &v) || + ave_rbsp_get(r, 16, &v))) + return -1; + } + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v && ave_rbsp_get(r, 1, &v)) + return -1; + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v) { + s64 colour; + + if (ave_rbsp_get(r, 3, &v) || + ave_rbsp_get(r, 1, &v) || + ave_rbsp_get(r, 1, &colour)) + return -1; + if (colour && + (ave_rbsp_get(r, 8, &v) || + ave_rbsp_get(r, 8, &v) || + ave_rbsp_get(r, 8, &v))) + return -1; + } + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v && (ave_rbsp_get_ue(r, &v) || + ave_rbsp_get_ue(r, &v))) + return -1; + *bit = r->pos; + *present = true; + return 0; +} + +/* + * Replace one Annex B NAL payload with an escaped RBSP. The header bytes + * stay. Returns 0 after *len grows, or -1 with @buf unchanged. + */ +static int ave_stamp_commit(u8 *buf, size_t room, size_t *len, size_t nal, + size_t nal_len, size_t next, unsigned int hdr, + const u8 *rebuilt, size_t out_bits, u8 *escaped, + size_t esc_cap) +{ + size_t esc_len, new_nal_len, tail, need; + + if (out_bits & 7) + out_bits += 8 - (out_bits & 7); + if (!out_bits || + ave_rbsp_escape(rebuilt, out_bits / 8, escaped, esc_cap, &esc_len)) + return -1; + new_nal_len = hdr + esc_len; + if (new_nal_len < nal_len) + return -1; + need = *len - nal_len + new_nal_len; + if (need > room || nal + new_nal_len > room) + return -1; + tail = *len - next; + if (new_nal_len != nal_len && tail) + memmove(buf + nal + new_nal_len, buf + next, tail); + memcpy(buf + nal + hdr, escaped, esc_len); + *len = need; + return 0; +} + +/* + * The tick counts of a frame rate num/den, as they go in the VUI: the exact + * rate, in lowest terms, so 60/2 gives the same bytes as 30/1. H.264 counts + * field ticks: num_units_in_tick = den and time_scale = 2 * num. When 2 * num + * does not fit 32 bits, an even den is halved instead (time_scale = num, + * num_units_in_tick = den / 2, the same rate). An odd den has no exact + * form there and the SPS is left as the firmware wrote it, like HEVC, whose + * vui_time_scale = num always fits. Returns false then, or for a zero rate. + */ +static bool ave_vui_ticks(bool hevc, u32 num, u32 den, u32 *units, u32 *scale) +{ + u32 g; + + if (!num || !den) + return false; + g = gcd(num, den); + num /= g; + den /= g; + if (hevc) { + *units = den; + *scale = num; + } else if (num <= U32_MAX / 2) { + *units = den; + *scale = num * 2; + } else if (!(den & 1)) { + *units = den / 2; + *scale = num; + } else { + return false; + } + return true; +} + +/* + * The 6070 firmware leaves timing_info_present_flag clear. With B-frames + * off it also leaves vui_parameters_present_flag clear, and a raw Annex B + * reader then reports 25 fps. num_units_in_tick = den and time_scale = + * 2 * num with fixed_frame_rate_flag make the frame rate num/den, the + * client's timeperframe as it is, not the integer rate the firmware gets + * (30/1 -> 1 and 60; 30000/1001 -> 1001 and 60000). An existing VUI is + * rewritten in place. A missing VUI is replaced by a minimal one (aspect, + * overscan, video signal and chroma location clear; timing on; HRD, + * pic_struct and bitstream_restriction clear) and the old trailing bits are + * dropped. The 720p no-VUI SPS + * 274d001fda014016e4 becomes 274d001fda014016e840000003004000000f21, which + * muxes as 30/1. With a VUI, 274d0028e5603c0113f2c03c20104e becomes + * 274d0028e5603c0113f2c2000003000200000300791e100827. + * The SPS is rewritten in the output buffer; the firmware's copy is + * untouched. A stream that already carries timing, or one this walk cannot + * follow, is left as the firmware wrote it. + */ +void ave_h264_stamp_timing(u8 *buf, size_t room, size_t *len, u32 fps_num, + u32 fps_den) +{ + size_t i = 0; + struct ave_rbsp r; + u8 rebuilt[AVE_RBSP_BYTES + 64], escaped[AVE_RBSP_BYTES * 3 / 2]; + const size_t cap = sizeof(rebuilt) * 8; + size_t out_bits = 0; + size_t timing, stop; + u32 units, scale; + bool present; + + if (!buf || !len || !*len || *len > room || + !ave_vui_ticks(false, fps_num, fps_den, &units, &scale)) + return; + while (i + 4 < *len) { + unsigned int sc = ave_start_code(buf, *len, i); + size_t nal, next, nal_len; + u8 hdr; + + if (!sc) { + i++; + continue; + } + nal = i + sc; + next = nal + 1; + while (next < *len && !ave_start_code(buf, *len, next)) + next++; + if (nal >= *len) + return; + hdr = buf[nal]; + if ((hdr & 0x1f) != 7) { + i = next; + continue; + } + nal_len = next - nal; + /* The whole SPS must fit the reader: all of it is written back. */ + if (nal_len < 2 || nal_len - 1 > sizeof(r.b)) + return; + ave_rbsp_init(&r, buf + nal + 1, nal_len - 1); + if (ave_h264_timing_bit(&r, &timing, &present)) + return; + out_bits = 0; + memset(rebuilt, 0, sizeof(rebuilt)); + if (!present) { + if (ave_bit_copy(rebuilt, cap, &out_bits, &r, 0, timing) || + ave_bit_put(rebuilt, cap, &out_bits, 1, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 1, 1) || + ave_bit_put(rebuilt, cap, &out_bits, units, 32) || + ave_bit_put(rebuilt, cap, &out_bits, scale, 32) || + ave_bit_put(rebuilt, cap, &out_bits, 1, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 1, 1)) + return; + } else { + r.pos = timing; + if (ave_rbsp_bit(&r)) + return; /* the firmware already set the clock */ + if (ave_rbsp_stop_bit(r.b, r.n, &stop) || stop <= timing) + return; + /* The rest of the VUI moves; the stop bit is written again. */ + if (ave_bit_copy(rebuilt, cap, &out_bits, &r, 0, timing) || + ave_bit_put(rebuilt, cap, &out_bits, 1, 1) || + ave_bit_put(rebuilt, cap, &out_bits, units, 32) || + ave_bit_put(rebuilt, cap, &out_bits, scale, 32) || + ave_bit_put(rebuilt, cap, &out_bits, 1, 1) || + ave_bit_copy(rebuilt, cap, &out_bits, &r, timing + 1, + stop - (timing + 1)) || + ave_bit_put(rebuilt, cap, &out_bits, 1, 1)) + return; + } + ave_stamp_commit(buf, room, len, nal, nal_len, next, 1, rebuilt, + out_bits, escaped, sizeof(escaped)); + return; + } +} + +/* One short-term reference picture set. Counts above 16 are refused. */ +static int ave_hevc_rps(struct ave_rbsp *r, unsigned int idx, unsigned int nsets, + u8 *nd, unsigned int *nd_n) +{ + s64 inter = 0, neg, pos_pics, v = 0; + unsigned int used = 0; + + if (idx && ave_rbsp_get(r, 1, &inter)) + return -1; + if (inter) { + s64 delta = 0; + int ref; + + if (idx == nsets && ave_rbsp_get_ue(r, &delta)) + return -1; + if (delta >= idx || idx - delta - 1 >= *nd_n) + return -1; + ref = idx - delta - 1; + if (ave_rbsp_get(r, 1, &v) || + ave_rbsp_get_ue(r, &v)) + return -1; + for (unsigned int j = 0; j <= nd[ref]; j++) { + s64 take; + + if (ave_rbsp_get(r, 1, &take)) + return -1; + if (!take && ave_rbsp_get(r, 1, &v)) + return -1; + if (take || v) + used++; + } + if (*nd_n >= 16 || used > 255) + return -1; + nd[(*nd_n)++] = used; + return 0; + } + if (ave_rbsp_get_ue(r, &neg) || + ave_rbsp_get_ue(r, &pos_pics) || + neg > 16 || pos_pics > 16) + return -1; + for (u32 i = 0; i < neg; i++) + if (ave_rbsp_get_ue(r, &v) || + ave_rbsp_get(r, 1, &v)) + return -1; + for (u32 i = 0; i < pos_pics; i++) + if (ave_rbsp_get_ue(r, &v) || + ave_rbsp_get(r, 1, &v)) + return -1; + if (*nd_n >= 16) + return -1; + nd[(*nd_n)++] = neg + pos_pics; + return 0; +} + +/* + * Bit index of vui_timing_info_present_flag. Scaling lists and more than + * one temporal sub-layer are left untouched: the walk would be guessing. + */ +static int ave_hevc_timing_bit(struct ave_rbsp *r, size_t *bit, bool *present) +{ + s64 v, max_sub, chroma, nsets, log2_poc, order; + u8 nd[16]; + unsigned int nd_n = 0; + + *present = false; + if (ave_rbsp_get(r, 4, &v) || + ave_rbsp_get(r, 3, &max_sub) || + ave_rbsp_get(r, 1, &v)) + return -1; + if (max_sub) + return -1; + /* general profile_tier_level is 96 bits when there are no sub-layers */ + if (ave_rbsp_get(r, 32, &v) || + ave_rbsp_get(r, 32, &v) || + ave_rbsp_get(r, 32, &v) || + ave_rbsp_get_ue(r, &v) || + ave_rbsp_get_ue(r, &chroma)) + return -1; + if (chroma == 3 && ave_rbsp_get(r, 1, &v)) + return -1; + if (ave_rbsp_get_ue(r, &v) || + ave_rbsp_get_ue(r, &v) || + ave_rbsp_get(r, 1, &v)) + return -1; + if (v) { + for (int i = 0; i < 4; i++) + if (ave_rbsp_get_ue(r, &v)) + return -1; + } + if (ave_rbsp_get_ue(r, &v) || + ave_rbsp_get_ue(r, &v) || + ave_rbsp_get_ue(r, &log2_poc)) + return -1; + if (log2_poc > 12) + return -1; + log2_poc += 4; + /* sps_sub_layer_ordering_info_present_flag */ + if (ave_rbsp_get(r, 1, &order)) + return -1; + for (u32 i = order ? 0 : max_sub; i <= max_sub; i++) + if (ave_rbsp_get_ue(r, &v) || + ave_rbsp_get_ue(r, &v) || + ave_rbsp_get_ue(r, &v)) + return -1; + for (int i = 0; i < 6; i++) + if (ave_rbsp_get_ue(r, &v)) + return -1; + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v) + return -1; /* scaling lists */ + if (ave_rbsp_get(r, 1, &v) || + ave_rbsp_get(r, 1, &v) || + ave_rbsp_get(r, 1, &v)) + return -1; + if (v) { + if (ave_rbsp_get(r, 4, &v) || + ave_rbsp_get(r, 4, &v) || + ave_rbsp_get_ue(r, &v) || + ave_rbsp_get_ue(r, &v) || + ave_rbsp_get(r, 1, &v)) + return -1; + } + if (ave_rbsp_get_ue(r, &nsets) || nsets > 16) + return -1; + for (u32 i = 0; i < nsets; i++) + if (ave_hevc_rps(r, i, nsets, nd, &nd_n)) + return -1; + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v) { + s64 nlt; + + if (ave_rbsp_get_ue(r, &nlt) || nlt > 16) + return -1; + for (u32 i = 0; i < nlt; i++) + if (ave_rbsp_get(r, log2_poc, &v) || + ave_rbsp_get(r, 1, &v)) + return -1; + } + if (ave_rbsp_get(r, 1, &v) || + ave_rbsp_get(r, 1, &v) || + ave_rbsp_get(r, 1, &v)) + return -1; + if (!v) { + *bit = r->pos - 1; /* vui_parameters_present_flag */ + /* The minimal VUI drops what follows: no SPS extensions. */ + if (ave_rbsp_get(r, 1, &v) || v) + return -1; + return 0; + } + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v) { + s64 idc; + + if (ave_rbsp_get(r, 8, &idc)) + return -1; + if (idc == 255 && + (ave_rbsp_get(r, 16, &v) || + ave_rbsp_get(r, 16, &v))) + return -1; + } + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v && ave_rbsp_get(r, 1, &v)) + return -1; /* overscan_appropriate_flag */ + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v) { + s64 colour; + + if (ave_rbsp_get(r, 3, &v) || + ave_rbsp_get(r, 1, &v) || + ave_rbsp_get(r, 1, &colour)) + return -1; + if (colour && + (ave_rbsp_get(r, 8, &v) || + ave_rbsp_get(r, 8, &v) || + ave_rbsp_get(r, 8, &v))) + return -1; + } + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v && (ave_rbsp_get_ue(r, &v) || + ave_rbsp_get_ue(r, &v))) + return -1; /* chroma_sample_loc */ + if (ave_rbsp_get(r, 1, &v) || + ave_rbsp_get(r, 1, &v) || + ave_rbsp_get(r, 1, &v)) + return -1; /* neutral, field_seq, frame_field_info */ + if (ave_rbsp_get(r, 1, &v)) + return -1; + if (v) { + for (int i = 0; i < 4; i++) + if (ave_rbsp_get_ue(r, &v)) + return -1; + } + *bit = r->pos; + *present = true; + return 0; +} + +/* + * Same missing-clock problem as H.264, different syntax. HEVC frame rate is + * time_scale / num_units_in_tick with no field-tick halving, so the + * client's rate num/den in lowest terms is written as den and num (30/1 as + * 1 and 30, 30000/1001 as 1001 and 30000). vui_poc_proportional_to_timing_flag + * and vui_hrd_parameters_present_flag stay clear. A missing VUI is replaced + * by a minimal one and sps_extension_present_flag stays clear; the old + * trailing bits are dropped. The 720p no-VUI SPS + * 420101016000000300b0000003000003005da00280802d165aee4522ae5cb97040 + * becomes + * 420101016000000300b0000003000003005da00280802d165aee4522ae5cb9710080000003008000000f04, + * which decodes and muxes as 30/1. A raw Annex B reader can still print + * 25 fps; an mp4 mux of the same bytes reports the stamped rate. + */ +void ave_hevc_stamp_timing(u8 *buf, size_t room, size_t *len, u32 fps_num, + u32 fps_den) +{ + size_t i = 0; + struct ave_rbsp r; + u8 rebuilt[AVE_RBSP_BYTES + 64], escaped[AVE_RBSP_BYTES * 3 / 2]; + const size_t cap = sizeof(rebuilt) * 8; + size_t out_bits = 0; + size_t timing, stop; + u32 units, scale; + bool present; + + if (!buf || !len || !*len || *len > room || + !ave_vui_ticks(true, fps_num, fps_den, &units, &scale)) + return; + while (i + 4 < *len) { + unsigned int sc = ave_start_code(buf, *len, i); + size_t nal, next, nal_len; + + if (!sc) { + i++; + continue; + } + nal = i + sc; + next = nal + 1; + while (next < *len && !ave_start_code(buf, *len, next)) + next++; + if (nal + 1 >= *len) + return; + if (((buf[nal] >> 1) & 0x3f) != 33) { + i = next; + continue; + } + nal_len = next - nal; + if (nal_len < 3 || nal_len - 2 > sizeof(r.b)) + return; + ave_rbsp_init(&r, buf + nal + 2, nal_len - 2); + if (ave_hevc_timing_bit(&r, &timing, &present)) + return; + out_bits = 0; + memset(rebuilt, 0, sizeof(rebuilt)); + if (!present) { + unsigned int z; + + if (ave_bit_copy(rebuilt, cap, &out_bits, &r, 0, timing) || + ave_bit_put(rebuilt, cap, &out_bits, 1, 1)) + return; + for (z = 0; z < 8; z++) + if (ave_bit_put(rebuilt, cap, &out_bits, 0, 1)) + return; + if (ave_bit_put(rebuilt, cap, &out_bits, 1, 1) || + ave_bit_put(rebuilt, cap, &out_bits, units, 32) || + ave_bit_put(rebuilt, cap, &out_bits, scale, 32) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 1, 1)) + return; + } else { + r.pos = timing; + if (ave_rbsp_bit(&r)) + return; + if (ave_rbsp_stop_bit(r.b, r.n, &stop) || stop <= timing) + return; + if (ave_bit_copy(rebuilt, cap, &out_bits, &r, 0, timing) || + ave_bit_put(rebuilt, cap, &out_bits, 1, 1) || + ave_bit_put(rebuilt, cap, &out_bits, units, 32) || + ave_bit_put(rebuilt, cap, &out_bits, scale, 32) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_put(rebuilt, cap, &out_bits, 0, 1) || + ave_bit_copy(rebuilt, cap, &out_bits, &r, timing + 1, + stop - (timing + 1)) || + ave_bit_put(rebuilt, cap, &out_bits, 1, 1)) + return; + } + ave_stamp_commit(buf, room, len, nal, nal_len, next, 2, rebuilt, + out_bits, escaped, sizeof(escaped)); + return; + } +} + /* NAL unit types in an Annex B buffer: key frame, parameter sets, filler? */ static void ave_scan_nals(const u8 *p, size_t len, bool hevc, bool *key, bool *params, bool *filler) @@ -1355,6 +2084,15 @@ int ave_sess_output(struct ave_ctx *ctx, unsigned int slot, struct vb2_v4l2_buff return -EPROTO; memcpy(out, sess->param_sets.cpu, result.parameter_set_bytes); ps = result.parameter_set_bytes; + /* An SPS that already carries timing is left as it is. */ + if (hevc) + ave_hevc_stamp_timing(out, room, &ps, + sess->p.client_fps_num, + sess->p.client_fps_den); + else + ave_h264_stamp_timing(out, room, &ps, + sess->p.client_fps_num, + sess->p.client_fps_den); } else if (fw->kind != AVE_LAYOUT_6070) { at = ave_find_params(meta, s->meta.size, hevc); if (at >= 0) { diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index 4fb4111fd1d7a..0d4256abed395 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -673,6 +673,11 @@ int ave_sess_free_slot(struct ave_session *sess); void ave_sess_reply(struct ave_dev *ave, const struct ave_reply *reply); int ave_sess_input(struct ave_ctx *ctx, unsigned int slot, struct vb2_v4l2_buffer *src, dma_addr_t *input); +/* Add VUI timing to the SPS at the start of an Annex B buffer. */ +void ave_h264_stamp_timing(u8 *buf, size_t room, size_t *len, u32 fps_num, + u32 fps_den); +void ave_hevc_stamp_timing(u8 *buf, size_t room, size_t *len, u32 fps_num, + u32 fps_den); int ave_sess_output(struct ave_ctx *ctx, unsigned int slot, struct vb2_v4l2_buffer *dst, bool *keyframe); void ave_sess_prepare_slot(struct ave_session *sess, unsigned int slot); From 9966801f4b0e18418c7ec1f978406182cdf94cbd Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Wed, 18 Nov 2026 19:10:13 -0300 Subject: [PATCH 12/13] arm64: dts: apple: t602x: describe the encoder DMA apertures as one block The t602x trees describe the ranges at *020000 and *044000 next to each encoder as two more DARTs. On the M2 Max (J475c) apple-dart rejects the *044000 nodes: the stream count it reads from them is 385 at 40d044000 and 350 at 507044000, above its 256 limit. The *020000 nodes probe as DARTs. T600x describes the same two ranges as one apple,t6000-ave-dma block, with the DARTs at *030000 and *040000. Do the same on t6020 (ave0), t6021 (ave0, ave1) and t6022 (ave0 to ave3), and drop the two ranges from the encoders' iommus. The binding allows exactly these two ranges. A maintainer may prefer an apple,t6020-ave-dma compatible in front; the binding has none today. The encoders on J475c ran with only the *030000 and *040000 DARTs and showed no DART faults. t6020 and t6022 were not booted. Assisted-by: Grok 4.7 Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- .../boot/dts/apple/t6020-peripherals.dtsi | 20 ++--- .../boot/dts/apple/t6021-peripherals.dtsi | 40 +++------- .../boot/dts/apple/t6022-peripherals.dtsi | 80 +++++-------------- 3 files changed, 35 insertions(+), 105 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t6020-peripherals.dtsi b/arch/arm64/boot/dts/apple/t6020-peripherals.dtsi index a521eb74b9ea2..adcf963b5b6fe 100644 --- a/arch/arm64/boot/dts/apple/t6020-peripherals.dtsi +++ b/arch/arm64/boot/dts/apple/t6020-peripherals.dtsi @@ -613,13 +613,13 @@ status = "disabled"; }; - m2_dart_ave0_2: iommu@40d020000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x4 0xd020000 0x0 0x4000>; + m2_ave0_dma: dma-controller@40d020000 { + compatible = "apple,t6000-ave-dma", "apple,ave-dma"; + reg = <0x4 0xd020000 0x0 0x4000>, + <0x4 0xd044000 0x0 0x4000>; interrupt-parent = <&aic>; interrupts = ; power-domains = <&ps_venc_sys>; - #iommu-cells = <1>; status = "disabled"; }; @@ -643,16 +643,6 @@ status = "disabled"; }; - m2_dart_ave0_3: iommu@40d044000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x4 0xd044000 0x0 0x4000>; - interrupt-parent = <&aic>; - interrupts = ; - power-domains = <&ps_venc_sys>; - #iommu-cells = <1>; - status = "disabled"; - }; - m2_ave0: video-codec@40d100000 { compatible = "apple,t6020-ave", "apple,ave"; power-domain-names = "dma", "pipe4", "pipe5", "me0", "me1", "perf"; @@ -668,7 +658,7 @@ , , ; - iommus = <&m2_dart_ave0_0 0>, <&m2_dart_ave0_1 0>, <&m2_dart_ave0_2 0>, <&m2_dart_ave0_3 0>; + iommus = <&m2_dart_ave0_0 0>, <&m2_dart_ave0_1 0>; power-domains = <&ps_venc_dma>, <&ps_venc_pipe4>, <&ps_venc_pipe5>, <&ps_venc_me0>, <&ps_venc_me1>, <&ps_afr>; status = "disabled"; }; diff --git a/arch/arm64/boot/dts/apple/t6021-peripherals.dtsi b/arch/arm64/boot/dts/apple/t6021-peripherals.dtsi index 61061f89aeb84..5476fb3e606b4 100644 --- a/arch/arm64/boot/dts/apple/t6021-peripherals.dtsi +++ b/arch/arm64/boot/dts/apple/t6021-peripherals.dtsi @@ -791,13 +791,13 @@ status = "disabled"; }; - m2_dart_ave0_2: iommu@40d020000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x4 0xd020000 0x0 0x4000>; + m2_ave0_dma: dma-controller@40d020000 { + compatible = "apple,t6000-ave-dma", "apple,ave-dma"; + reg = <0x4 0xd020000 0x0 0x4000>, + <0x4 0xd044000 0x0 0x4000>; interrupt-parent = <&aic>; interrupts = ; power-domains = <&ps_venc_sys>; - #iommu-cells = <1>; status = "disabled"; }; @@ -821,16 +821,6 @@ status = "disabled"; }; - m2_dart_ave0_3: iommu@40d044000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x4 0xd044000 0x0 0x4000>; - interrupt-parent = <&aic>; - interrupts = ; - power-domains = <&ps_venc_sys>; - #iommu-cells = <1>; - status = "disabled"; - }; - m2_ave0: video-codec@40d100000 { compatible = "apple,t6021-ave", "apple,ave"; power-domain-names = "dma", "pipe4", "pipe5", "me0", "me1", "perf"; @@ -846,7 +836,7 @@ , , ; - iommus = <&m2_dart_ave0_0 0>, <&m2_dart_ave0_1 0>, <&m2_dart_ave0_2 0>, <&m2_dart_ave0_3 0>; + iommus = <&m2_dart_ave0_0 0>, <&m2_dart_ave0_1 0>; power-domains = <&ps_venc_dma>, <&ps_venc_pipe4>, <&ps_venc_pipe5>, <&ps_venc_me0>, <&ps_venc_me1>, <&ps_afr>; status = "disabled"; }; @@ -883,13 +873,13 @@ status = "disabled"; }; - m2_dart_ave1_2: iommu@507020000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x5 0x7020000 0x0 0x4000>; + m2_ave1_dma: dma-controller@507020000 { + compatible = "apple,t6000-ave-dma", "apple,ave-dma"; + reg = <0x5 0x7020000 0x0 0x4000>, + <0x5 0x7044000 0x0 0x4000>; interrupt-parent = <&aic>; interrupts = ; power-domains = <&ps_venc1_sys>; - #iommu-cells = <1>; status = "disabled"; }; @@ -913,16 +903,6 @@ status = "disabled"; }; - m2_dart_ave1_3: iommu@507044000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x5 0x7044000 0x0 0x4000>; - interrupt-parent = <&aic>; - interrupts = ; - power-domains = <&ps_venc1_sys>; - #iommu-cells = <1>; - status = "disabled"; - }; - m2_ave1: video-codec@507100000 { compatible = "apple,t6021-ave", "apple,ave"; power-domain-names = "dma", "pipe4", "pipe5", "me0", "me1", "perf"; @@ -938,7 +918,7 @@ , , ; - iommus = <&m2_dart_ave1_0 0>, <&m2_dart_ave1_1 0>, <&m2_dart_ave1_2 0>, <&m2_dart_ave1_3 0>; + iommus = <&m2_dart_ave1_0 0>, <&m2_dart_ave1_1 0>; power-domains = <&ps_venc1_dma>, <&ps_venc1_pipe4>, <&ps_venc1_pipe5>, <&ps_venc1_me0>, <&ps_venc1_me1>, <&ps_afr>; status = "disabled"; }; diff --git a/arch/arm64/boot/dts/apple/t6022-peripherals.dtsi b/arch/arm64/boot/dts/apple/t6022-peripherals.dtsi index 0b41c11853b58..b3a7cd4e79932 100644 --- a/arch/arm64/boot/dts/apple/t6022-peripherals.dtsi +++ b/arch/arm64/boot/dts/apple/t6022-peripherals.dtsi @@ -1153,13 +1153,13 @@ status = "disabled"; }; - m2_dart_ave0_2: iommu@40d020000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x4 0xd020000 0x0 0x4000>; + m2_ave0_dma: dma-controller@40d020000 { + compatible = "apple,t6000-ave-dma", "apple,ave-dma"; + reg = <0x4 0xd020000 0x0 0x4000>, + <0x4 0xd044000 0x0 0x4000>; interrupt-parent = <&aic>; interrupts = ; power-domains = <&ps_venc_sys>; - #iommu-cells = <1>; status = "disabled"; }; @@ -1183,16 +1183,6 @@ status = "disabled"; }; - m2_dart_ave0_3: iommu@40d044000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x4 0xd044000 0x0 0x4000>; - interrupt-parent = <&aic>; - interrupts = ; - power-domains = <&ps_venc_sys>; - #iommu-cells = <1>; - status = "disabled"; - }; - m2_ave0: video-codec@40d100000 { compatible = "apple,t6022-ave", "apple,ave"; power-domain-names = "dma", "pipe4", "pipe5", "me0", "me1", "perf"; @@ -1208,7 +1198,7 @@ , , ; - iommus = <&m2_dart_ave0_0 0>, <&m2_dart_ave0_1 0>, <&m2_dart_ave0_2 0>, <&m2_dart_ave0_3 0>; + iommus = <&m2_dart_ave0_0 0>, <&m2_dart_ave0_1 0>; power-domains = <&ps_venc_dma>, <&ps_venc_pipe4>, <&ps_venc_pipe5>, <&ps_venc_me0>, <&ps_venc_me1>, <&ps_afr>; status = "disabled"; }; @@ -1311,13 +1301,13 @@ status = "disabled"; }; - m2_dart_ave1_2: iommu@507020000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x5 0x7020000 0x0 0x4000>; + m2_ave1_dma: dma-controller@507020000 { + compatible = "apple,t6000-ave-dma", "apple,ave-dma"; + reg = <0x5 0x7020000 0x0 0x4000>, + <0x5 0x7044000 0x0 0x4000>; interrupt-parent = <&aic>; interrupts = ; power-domains = <&ps_venc1_sys>; - #iommu-cells = <1>; status = "disabled"; }; @@ -1341,16 +1331,6 @@ status = "disabled"; }; - m2_dart_ave1_3: iommu@507044000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x5 0x7044000 0x0 0x4000>; - interrupt-parent = <&aic>; - interrupts = ; - power-domains = <&ps_venc1_sys>; - #iommu-cells = <1>; - status = "disabled"; - }; - m2_ave1: video-codec@507100000 { compatible = "apple,t6022-ave", "apple,ave"; power-domain-names = "dma", "pipe4", "pipe5", "me0", "me1", "perf"; @@ -1366,7 +1346,7 @@ , , ; - iommus = <&m2_dart_ave1_0 0>, <&m2_dart_ave1_1 0>, <&m2_dart_ave1_2 0>, <&m2_dart_ave1_3 0>; + iommus = <&m2_dart_ave1_0 0>, <&m2_dart_ave1_1 0>; power-domains = <&ps_venc1_dma>, <&ps_venc1_pipe4>, <&ps_venc1_pipe5>, <&ps_venc1_me0>, <&ps_venc1_me1>, <&ps_afr>; status = "disabled"; }; @@ -1966,13 +1946,13 @@ status = "disabled"; }; - m2_dart_ave2_2: iommu@40d020000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x4 0xd020000 0x0 0x4000>; + m2_ave2_dma: dma-controller@40d020000 { + compatible = "apple,t6000-ave-dma", "apple,ave-dma"; + reg = <0x4 0xd020000 0x0 0x4000>, + <0x4 0xd044000 0x0 0x4000>; interrupt-parent = <&aic>; interrupts = ; power-domains = <&ps_venc_sys_die1>; - #iommu-cells = <1>; status = "disabled"; }; @@ -1996,16 +1976,6 @@ status = "disabled"; }; - m2_dart_ave2_3: iommu@40d044000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x4 0xd044000 0x0 0x4000>; - interrupt-parent = <&aic>; - interrupts = ; - power-domains = <&ps_venc_sys_die1>; - #iommu-cells = <1>; - status = "disabled"; - }; - m2_ave2: video-codec@40d100000 { compatible = "apple,t6022-ave", "apple,ave"; power-domain-names = "dma", "pipe4", "pipe5", "me0", "me1", "perf"; @@ -2021,7 +1991,7 @@ , , ; - iommus = <&m2_dart_ave2_0 0>, <&m2_dart_ave2_1 0>, <&m2_dart_ave2_2 0>, <&m2_dart_ave2_3 0>; + iommus = <&m2_dart_ave2_0 0>, <&m2_dart_ave2_1 0>; power-domains = <&ps_venc_dma_die1>, <&ps_venc_pipe4_die1>, <&ps_venc_pipe5_die1>, <&ps_venc_me0_die1>, <&ps_venc_me1_die1>, <&ps_afr_die1>; status = "disabled"; }; @@ -2124,13 +2094,13 @@ status = "disabled"; }; - m2_dart_ave3_2: iommu@507020000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x5 0x7020000 0x0 0x4000>; + m2_ave3_dma: dma-controller@507020000 { + compatible = "apple,t6000-ave-dma", "apple,ave-dma"; + reg = <0x5 0x7020000 0x0 0x4000>, + <0x5 0x7044000 0x0 0x4000>; interrupt-parent = <&aic>; interrupts = ; power-domains = <&ps_venc1_sys_die1>; - #iommu-cells = <1>; status = "disabled"; }; @@ -2154,16 +2124,6 @@ status = "disabled"; }; - m2_dart_ave3_3: iommu@507044000 { - compatible = "apple,t6020-dart", "apple,t8110-dart"; - reg = <0x5 0x7044000 0x0 0x4000>; - interrupt-parent = <&aic>; - interrupts = ; - power-domains = <&ps_venc1_sys_die1>; - #iommu-cells = <1>; - status = "disabled"; - }; - m2_ave3: video-codec@507100000 { compatible = "apple,t6022-ave", "apple,ave"; power-domain-names = "dma", "pipe4", "pipe5", "me0", "me1", "perf"; @@ -2179,7 +2139,7 @@ , , ; - iommus = <&m2_dart_ave3_0 0>, <&m2_dart_ave3_1 0>, <&m2_dart_ave3_2 0>, <&m2_dart_ave3_3 0>; + iommus = <&m2_dart_ave3_0 0>, <&m2_dart_ave3_1 0>; power-domains = <&ps_venc1_dma_die1>, <&ps_venc1_pipe4_die1>, <&ps_venc1_pipe5_die1>, <&ps_venc1_me0_die1>, <&ps_venc1_me1_die1>, <&ps_afr_die1>; status = "disabled"; }; From a38dade589ee081d7332a2edda058dca5224be3e Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Thu, 19 Nov 2026 14:50:56 -0300 Subject: [PATCH 13/13] arm64: dts: apple: t6021: enable the video encoders Enable both encoders and their two DARTs each in t6021-peripherals, for every M2 Max machine (J414c, J416c, J475c). That is safe on a machine nobody ran: the boot loader hands each encoder the firmware iBoot preloaded or disables the node, and without a handoff the driver refuses the reservation and does not bind. The auxiliary DMA blocks stay disabled, as on every other SoC. With them enabled, apple-ave-dma binds them; it has no runtime PM, so ps_venc_sys and ps_venc1_sys never power off, the encoder's power cycle does not reset the coprocessor, and after the first session's Halt the firmware never answers a boot again. With them disabled, every session boots the firmware afresh and the VENC domains are off between sessions. The encoders were run on J475c only. Assisted-by: Claude Opus 5.5 Signed-off-by: Lucas Goossen --- .../boot/dts/apple/t6021-peripherals.dtsi | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t6021-peripherals.dtsi b/arch/arm64/boot/dts/apple/t6021-peripherals.dtsi index 5476fb3e606b4..948f4975afd09 100644 --- a/arch/arm64/boot/dts/apple/t6021-peripherals.dtsi +++ b/arch/arm64/boot/dts/apple/t6021-peripherals.dtsi @@ -791,6 +791,12 @@ status = "disabled"; }; + /* + * Stays disabled: apple-ave-dma has no runtime PM, so with it bound + * ps_venc_sys never powers off, the encoder's power cycle no longer + * resets the coprocessor, and after the first session's Halt the + * firmware does not boot again. + */ m2_ave0_dma: dma-controller@40d020000 { compatible = "apple,t6000-ave-dma", "apple,ave-dma"; reg = <0x4 0xd020000 0x0 0x4000>, @@ -808,7 +814,6 @@ interrupts = ; power-domains = <&ps_venc_sys>; #iommu-cells = <1>; - status = "disabled"; }; m2_dart_ave0_1: iommu@40d040000 { @@ -818,9 +823,12 @@ interrupts = ; power-domains = <&ps_venc_sys>; #iommu-cells = <1>; - status = "disabled"; }; + /* + * The boot loader hands this encoder the firmware iBoot preloaded, or + * disables it; without a handoff the driver does not bind. + */ m2_ave0: video-codec@40d100000 { compatible = "apple,t6021-ave", "apple,ave"; power-domain-names = "dma", "pipe4", "pipe5", "me0", "me1", "perf"; @@ -838,7 +846,6 @@ ; iommus = <&m2_dart_ave0_0 0>, <&m2_dart_ave0_1 0>; power-domains = <&ps_venc_dma>, <&ps_venc_pipe4>, <&ps_venc_pipe5>, <&ps_venc_me0>, <&ps_venc_me1>, <&ps_afr>; - status = "disabled"; }; m2_dart_dispext2_1: iommu@48d300000 { @@ -873,6 +880,12 @@ status = "disabled"; }; + /* + * Stays disabled: apple-ave-dma has no runtime PM, so with it bound + * ps_venc1_sys never powers off, the encoder's power cycle no longer + * resets the coprocessor, and after the first session's Halt the + * firmware does not boot again. + */ m2_ave1_dma: dma-controller@507020000 { compatible = "apple,t6000-ave-dma", "apple,ave-dma"; reg = <0x5 0x7020000 0x0 0x4000>, @@ -890,7 +903,6 @@ interrupts = ; power-domains = <&ps_venc1_sys>; #iommu-cells = <1>; - status = "disabled"; }; m2_dart_ave1_1: iommu@507040000 { @@ -900,9 +912,12 @@ interrupts = ; power-domains = <&ps_venc1_sys>; #iommu-cells = <1>; - status = "disabled"; }; + /* + * The boot loader hands this encoder the firmware iBoot preloaded, or + * disables it; without a handoff the driver does not bind. + */ m2_ave1: video-codec@507100000 { compatible = "apple,t6021-ave", "apple,ave"; power-domain-names = "dma", "pipe4", "pipe5", "me0", "me1", "perf"; @@ -920,7 +935,6 @@ ; iommus = <&m2_dart_ave1_0 0>, <&m2_dart_ave1_1 0>; power-domains = <&ps_venc1_dma>, <&ps_venc1_pipe4>, <&ps_venc1_pipe5>, <&ps_venc1_me0>, <&ps_venc1_me1>, <&ps_afr>; - status = "disabled"; }; m2_dart_dispext3_1: iommu@50d300000 {