diff --git a/Documentation/admin-guide/media/apple-ave.rst b/Documentation/admin-guide/media/apple-ave.rst index 52a44eaaa4e28..d0c16c76fbd96 100644 --- a/Documentation/admin-guide/media/apple-ave.rst +++ b/Documentation/admin-guide/media/apple-ave.rst @@ -67,6 +67,28 @@ those nodes. A probe with a valid handoff reports the verified CRCs without starting the coprocessor. An unknown layout refuses stream start before allocating session buffers or acquiring runtime power. +T8103 (M1) with the macOS 27.2 firmware (TEXT 0x114000, DATA 0x128000) +encodes H.264 and HEVC. The bootloader fills the 64-bit cell at TEXT 0x423c +with the firmware's alias of DATA, as it does for the 6070 builds; with the cell +zeroed the TEXT CRC is ``b6616340``. The firmware boots on the +boot configuration handshake as device 7 with handshake type 7 and speaks the +26B123 command layout; the coprocessor registers and their start are the +T8112 ones. TEXT starts the encoder's DART window at address 0, as on every +M1-family encoder. The encoder and both of its DARTs are enabled in the T8103 +device tree; without the firmware handoff the driver declines to bind. + +The M1 bootloader points the coprocessor's code base at TEXT's physical +address, and the firmware reaches DATA through its own alias, the address in +the TEXT link cell. Both pass through the address filter of the encoder's CPU +DART, which the device tree names as that DART's ``dapf`` window and which only +the bootloader programs, from the ADT. Before every start the driver reads the +filter and refuses to start the coprocessor, with the filter's entries in the +log, unless it admits TEXT and the DATA alias. The ``dart_audit`` debugfs file +shows the filter and that verdict without starting anything. A deep sleep clears +the filter's enable bits and keeps its ranges; the DART driver keeps the +entries the bootloader programmed and writes them back on resume, so encoding +continues after a wake. + T8122 (M3) with the macOS 27.2 firmware (TEXT 0x134000, DATA 0x128000, TEXT CRC ``918d70d3``) encodes H.264 and HEVC: the firmware boots on the boot configuration handshake as device 17 with handshake type 17 and speaks the diff --git a/arch/arm64/boot/dts/apple/t8103-peripherals.dtsi b/arch/arm64/boot/dts/apple/t8103-peripherals.dtsi index b44750a89bd4f..67b927a53e363 100644 --- a/arch/arm64/boot/dts/apple/t8103-peripherals.dtsi +++ b/arch/arm64/boot/dts/apple/t8103-peripherals.dtsi @@ -632,17 +632,23 @@ interrupts = ; power-domains = <&ps_venc_sys>; #iommu-cells = <1>; - status = "disabled"; + status = "okay"; }; + /* + * The coprocessor's CPU DART. Its address filter is the page at + * 0x267044000 (ADT dart-ave reg[3]), which the bootloader programs. + */ ave_dart0: iommu@267040000 { compatible = "apple,t8103-dart"; - reg = <0x2 0x67040000 0x0 0x4000>; + reg = <0x2 0x67040000 0x0 0x4000>, + <0x2 0x67044000 0x0 0x4000>; + reg-names = "dart", "dapf"; interrupt-parent = <&aic>; interrupts = ; power-domains = <&ps_venc_sys>; #iommu-cells = <1>; - status = "disabled"; + status = "okay"; }; ave: video-codec@267100000 { @@ -663,7 +669,7 @@ iommus = <&ave_dart0 0>, <&ave_dart1 0>; reg-names = "codec", "mailbox", "coproc", "pmgr", "pio"; power-domain-names = "dma", "pipe4", "pipe5", "me0", "me1"; - status = "disabled"; + status = "okay"; }; atc0_dpin0: displayport@381e50000 { diff --git a/drivers/iommu/apple-dart-test.c b/drivers/iommu/apple-dart-test.c index a7ae726f341a6..ff4c39f804167 100644 --- a/drivers/iommu/apple-dart-test.c +++ b/drivers/iommu/apple-dart-test.c @@ -3,6 +3,7 @@ #include #include #include +#include #include struct apple_dart_test_window { @@ -1481,6 +1482,104 @@ static void apple_dart_test_host_power_tables(struct kunit *test) } #endif +/* + * The J313 encoder CPU DART's filter as its loader programs it: TEXT at its physical + * address (control 0x11) and the window into the DART (0x33). After a deep sleep the + * ranges read back unchanged and the control words as 0x10 and 0x30. + */ +static const struct apple_dart_dapf_entry apple_dart_test_m1_dapf[] = { + { .ctrl = 0x11, .r4 = 1, .start = 0x800a20000ULL, .end = 0x800b33ffcULL }, + { .ctrl = 0x33, .r4 = 1, .start = 0xf00000000ULL, .end = 0xffffffffcULL }, +}; + +static void apple_dart_test_dapf_t8020_used(struct kunit *test) +{ + struct apple_dart_dapf_entry e[DART_T8020_DAPF_ENTRIES] = {}; + + KUNIT_EXPECT_EQ(test, apple_dart_dapf_t8020_used(e, ARRAY_SIZE(e)), 0U); + memcpy(e, apple_dart_test_m1_dapf, sizeof(apple_dart_test_m1_dapf)); + KUNIT_EXPECT_EQ(test, apple_dart_dapf_t8020_used(e, ARRAY_SIZE(e)), 2U); + /* A sleep's leftovers: ranges without enable bits keep nothing. */ + e[0].ctrl = 0x10; + e[1].ctrl = 0x30; + KUNIT_EXPECT_EQ(test, apple_dart_dapf_t8020_used(e, ARRAY_SIZE(e)), 0U); + /* A hole before the last enabled entry is kept, so slots stay where they were. */ + e[3] = apple_dart_test_m1_dapf[1]; + KUNIT_EXPECT_EQ(test, apple_dart_dapf_t8020_used(e, ARRAY_SIZE(e)), 4U); +} + +static void apple_dart_test_dapf_t8020_same(struct kunit *test) +{ + struct apple_dart_dapf_entry live[2]; + + memcpy(live, apple_dart_test_m1_dapf, sizeof(live)); + KUNIT_EXPECT_TRUE(test, apple_dart_dapf_t8020_same(apple_dart_test_m1_dapf, live, 2)); + /* Ends whose low two bits read back set are the same end. */ + live[0].end |= 3; + KUNIT_EXPECT_TRUE(test, apple_dart_dapf_t8020_same(apple_dart_test_m1_dapf, live, 2)); + /* Control bits above the low byte do not read back. */ + live[1].ctrl |= 0x100; + KUNIT_EXPECT_TRUE(test, apple_dart_dapf_t8020_same(apple_dart_test_m1_dapf, live, 2)); + /* After a sleep: the same ranges, the enable bits gone. Not the same filter. */ + live[0].ctrl = 0x10; + KUNIT_EXPECT_FALSE(test, apple_dart_dapf_t8020_same(apple_dart_test_m1_dapf, live, 2)); + memcpy(live, apple_dart_test_m1_dapf, sizeof(live)); + live[1].start = 0; + KUNIT_EXPECT_FALSE(test, apple_dart_dapf_t8020_same(apple_dart_test_m1_dapf, live, 2)); + memcpy(live, apple_dart_test_m1_dapf, sizeof(live)); + live[0].r4 = 0; + KUNIT_EXPECT_FALSE(test, apple_dart_dapf_t8020_same(apple_dart_test_m1_dapf, live, 2)); +} + +/* The write-back on a memory-backed filter window, as a sleep leaves it. */ +static void apple_dart_test_dapf_t8020_replay(struct kunit *test) +{ + struct apple_dart_dapf_entry live[2]; + struct apple_dart *dart; + u8 *win; + + dart = kunit_kzalloc(test, sizeof(*dart), GFP_KERNEL); + win = kunit_kzalloc(test, DART_T8020_DAPF_ENTRIES * DART_DAPF_STRIDE, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, dart); + KUNIT_ASSERT_NOT_NULL(test, win); + dart->dev = kunit_device_register(test, "apple-dart-dapf-test"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dart->dev); + dart->dapf = (__force void __iomem *)win; + + for (unsigned int i = 0; i < 2; i++) { + u8 *entry = win + i * DART_DAPF_STRIDE; + + put_unaligned_le32(apple_dart_test_m1_dapf[i].ctrl & 0xf0, entry + 0x00); + put_unaligned_le32(apple_dart_test_m1_dapf[i].r4, entry + 0x04); + put_unaligned_le64(apple_dart_test_m1_dapf[i].start, entry + 0x08); + put_unaligned_le64(apple_dart_test_m1_dapf[i].end, entry + 0x10); + } + /* Nothing kept: the filter stays as it is. */ + apple_dart_dapf_replay(dart, false); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(win), 0x10U); + + dart->dapf_saved = kunit_kzalloc(test, sizeof(apple_dart_test_m1_dapf), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, dart->dapf_saved); + memcpy(dart->dapf_saved, apple_dart_test_m1_dapf, sizeof(apple_dart_test_m1_dapf)); + dart->dapf_saved_count = ARRAY_SIZE(apple_dart_test_m1_dapf); + apple_dart_dapf_replay(dart, false); + apple_dart_dapf_t8020_read(dart, live, 2); + KUNIT_EXPECT_TRUE(test, apple_dart_dapf_t8020_same(apple_dart_test_m1_dapf, live, 2)); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(win), 0x11U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(win + DART_DAPF_STRIDE), 0x33U); + + /* + * An intact filter is left alone. Control bits above the low byte are not compared, + * so a write would show as them gone. + */ + put_unaligned_le32(0x111, win); + apple_dart_dapf_replay(dart, false); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(win), 0x111U); + /* Unless asked to always write it. */ + apple_dart_dapf_replay(dart, true); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(win), 0x11U); +} + /* A zeroed 16 KiB page marked reserved, as the firmware's tables are. */ static void apple_dart_test_free_reserved(void *data) { @@ -1640,6 +1739,9 @@ static void apple_dart_test_fw_mirror_prot(struct kunit *test) } static struct kunit_case apple_dart_test_cases[] = { + KUNIT_CASE(apple_dart_test_dapf_t8020_used), + KUNIT_CASE(apple_dart_test_dapf_t8020_same), + KUNIT_CASE(apple_dart_test_dapf_t8020_replay), KUNIT_CASE(apple_dart_test_strict_engines), KUNIT_CASE(apple_dart_test_topology), KUNIT_CASE(apple_dart_test_iommu_specifiers), diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index f96264825a69f..8bee6eb2a2ea4 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -361,6 +361,14 @@ struct apple_dart_fw_tree { */ #define APPLE_DART_GAPF_PROTECT BIT(4) +/* One address filter entry as a T8020-layout DART reads it back */ +struct apple_dart_dapf_entry { + u32 ctrl; + u32 r4; + u64 start; + u64 end; /* inclusive */ +}; + struct apple_dart { bool cold_gen3; u32 cold_gen3_sids; @@ -413,6 +421,12 @@ struct apple_dart { u64 *dapf_ranges; u32 *dapf_r20; unsigned int dapf_count; + /* + * T8020 layout: the entries the loader left programmed, read back at probe, and how + * many of them, through the last enabled one. Written back after a sleep. + */ + struct apple_dart_dapf_entry *dapf_saved; + unsigned int dapf_saved_count; int irq; struct clk_bulk_data *clks; @@ -5689,6 +5703,108 @@ static void apple_dart_sleep_cfg_read(struct apple_dart *dart, u32 *cfg) #define DART_DAPF_STRIDE 0x40 #define DART_DAPF_CONFIG 0x31 +/* + * A T8020-layout address filter (the M1 family's) has 16 entries, 0x40 apart: a control + * word at 0x00 whose low four bits enable the entry, a word at 0x04 and an inclusive range + * at 0x08 and 0x10. The loader writes them from the ADT's filter-data-instance-0. A deep + * sleep clears the enable bits and keeps the rest, so a filter that only compares its + * ranges looks intact while it admits nothing. + */ +#define DART_T8020_DAPF_ENTRIES 16 +#define DART_T8020_DAPF_ENABLE GENMASK(3, 0) + +/* How many leading entries to keep: through the last enabled one, 0 with none. */ +static unsigned int apple_dart_dapf_t8020_used(const struct apple_dart_dapf_entry *e, + unsigned int n) +{ + unsigned int used = 0; + + for (unsigned int i = 0; i < n; i++) + if (e[i].ctrl & DART_T8020_DAPF_ENABLE) + used = i + 1; + return used; +} + +/* + * Whether the live entries are the saved ones, enable bits included. Only the low byte + * of a control word and all but the low two bits of an end are compared: those are what + * read back as written. + */ +static bool apple_dart_dapf_t8020_same(const struct apple_dart_dapf_entry *saved, + const struct apple_dart_dapf_entry *live, + unsigned int n) +{ + for (unsigned int i = 0; i < n; i++) + if ((saved[i].ctrl ^ live[i].ctrl) & 0xff || saved[i].r4 != live[i].r4 || + saved[i].start != live[i].start || (saved[i].end | 3) != (live[i].end | 3)) + return false; + return true; +} + +static void apple_dart_dapf_t8020_read(struct apple_dart *dart, + struct apple_dart_dapf_entry *e, unsigned int n) +{ + for (unsigned int i = 0; i < n; i++) { + void __iomem *entry = dart->dapf + i * DART_DAPF_STRIDE; + + e[i].ctrl = readl(entry + 0x00); + e[i].r4 = readl(entry + 0x04); + e[i].start = readq(entry + 0x08); + e[i].end = readq(entry + 0x10); + } +} + +/* + * A T8020-layout DART that names its filter window ("dapf") without apple,dapf-ranges: + * keep what the loader programmed, read back while the DART is powered at probe, to write + * it back after a sleep. A filter nothing programmed keeps nothing. + */ +static int apple_dart_dapf_t8020_save(struct apple_dart *dart) +{ + struct apple_dart_dapf_entry live[DART_T8020_DAPF_ENTRIES]; + unsigned int used; + + apple_dart_dapf_t8020_read(dart, live, ARRAY_SIZE(live)); + used = apple_dart_dapf_t8020_used(live, ARRAY_SIZE(live)); + if (!used) { + dev_info(dart->dev, "address filter not programmed: nothing to keep across sleep\n"); + return 0; + } + dart->dapf_saved = devm_kmemdup(dart->dev, live, used * sizeof(*live), GFP_KERNEL); + if (!dart->dapf_saved) + return -ENOMEM; + dart->dapf_saved_count = used; + dev_dbg(dart->dev, "address filter: %u entries kept for write-back after sleep\n", used); + return 0; +} + +/* After a power-off: write the kept entries back unless they still read as kept. */ +static void apple_dart_dapf_t8020_replay(struct apple_dart *dart, bool always) +{ + struct apple_dart_dapf_entry live[DART_T8020_DAPF_ENTRIES]; + unsigned int n = dart->dapf_saved_count; + + apple_dart_dapf_t8020_read(dart, live, n); + if (!always && apple_dart_dapf_t8020_same(dart->dapf_saved, live, n)) + return; + /* In the loader's order: each entry's control word last. */ + for (unsigned int i = 0; i < n; i++) { + void __iomem *entry = dart->dapf + i * DART_DAPF_STRIDE; + + writel(dart->dapf_saved[i].r4, entry + 0x04); + writeq(dart->dapf_saved[i].start, entry + 0x08); + writeq(dart->dapf_saved[i].end, entry + 0x10); + writel(dart->dapf_saved[i].ctrl, entry + 0x00); + } + apple_dart_dapf_t8020_read(dart, live, n); + if (!apple_dart_dapf_t8020_same(dart->dapf_saved, live, n)) { + dev_err(dart->dev, "address filter write-back didn't take: entry 0 reads %#x %#llx..%#llx\n", + live[0].ctrl, live[0].start, live[0].end); + return; + } + dev_dbg(dart->dev, "address filter: %u entries written back after the sleep\n", n); +} + static int apple_dart_dapf_parse(struct platform_device *pdev, struct apple_dart *dart) { struct device *dev = &pdev->dev; @@ -5747,6 +5863,10 @@ static void apple_dart_dapf_replay(struct apple_dart *dart, bool always) { unsigned int i; + if (dart->dapf_saved_count) { + apple_dart_dapf_t8020_replay(dart, always); + return; + } if (!dart->dapf_count) return; if (!always && apple_dart_dapf_holds(dart)) @@ -6202,6 +6322,12 @@ static int apple_dart_probe(struct platform_device *pdev) ret = apple_dart_dapf_parse(pdev, dart); if (ret) return ret; + } else if (dart->hw->type == DART_T8020 && + platform_get_resource_byname(pdev, IORESOURCE_MEM, "dapf")) { + /* Read back once the DART is powered, below. */ + dart->dapf = devm_platform_ioremap_resource_byname(pdev, "dapf"); + if (IS_ERR(dart->dapf)) + return PTR_ERR(dart->dapf); } dart->restore_on_power_up = of_property_read_bool(dev->of_node, "apple,restore-on-power-up"); @@ -6372,6 +6498,12 @@ static int apple_dart_probe(struct platform_device *pdev) goto err_clk_disable; } + if (dart->dapf && !dart->dapf_count) { + ret = apple_dart_dapf_t8020_save(dart); + if (ret) + goto err_clk_disable; + } + if (!tunneled && dart->hw->type == DART_T8110) { ret = apple_dart_parse_own_setup(dart, res, dart_params); if (ret) diff --git a/drivers/media/platform/apple/ave/Kconfig b/drivers/media/platform/apple/ave/Kconfig index e71449b2d9564..fd2fd26e79eea 100644 --- a/drivers/media/platform/apple/ave/Kconfig +++ b/drivers/media/platform/apple/ave/Kconfig @@ -12,7 +12,8 @@ config VIDEO_APPLE_AVE 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. - T8122 (M3) with the macOS 27.2 firmware encodes H.264 and HEVC. + T8103 (M1) and T8122 (M3) with the macOS 27.2 firmware encode + H.264 and HEVC. T6041 (M4 Pro) and the M3 Pro/Max (T603x) have probe-only support. T6050 (M5 Pro/Max) encodes H.264. diff --git a/drivers/media/platform/apple/ave/ave-abi-test.c b/drivers/media/platform/apple/ave/ave-abi-test.c index 1698e8a97ec94..4ff90360aa9cc 100644 --- a/drivers/media/platform/apple/ave/ave-abi-test.c +++ b/drivers/media/platform/apple/ave/ave-abi-test.c @@ -1657,6 +1657,206 @@ static void ave_hevc_bframe_extra_bits(struct kunit *test) KUNIT_EXPECT_FALSE(test, ave_au_bframe(b_au, sizeof(b_au), true, false, &bits)); } +/* + * The M1 with the 27.2 stub: its encoder's own variant pins the 27.2 build + * (device 7, handshake type 7) and selects the 26B123 layout. Other segment + * sizes, the M2's 27.2 build among them, are refused. + */ +static void ave_t8103_26b123_build(struct kunit *test) +{ + const struct ave_variant *v = &ave_t8103; + + KUNIT_EXPECT_PTR_EQ(test, ave_fw_select_build(v, 0x114000, 0x128000), v); + KUNIT_EXPECT_TRUE(test, ave_fw_sizes_match(v, 0x114000, 0x128000)); + KUNIT_EXPECT_FALSE(test, ave_fw_sizes_match(v, 0x11c000, 0x128000)); + KUNIT_EXPECT_FALSE(test, ave_fw_sizes_match(v, 0x114000, 0x12c000)); + KUNIT_EXPECT_FALSE(test, ave_fw_sizes_match(v, 0xd0000, 0x128000)); + KUNIT_EXPECT_NULL(test, v->next_build); + KUNIT_EXPECT_EQ(test, v->build_count, 0U); + KUNIT_EXPECT_TRUE(test, v->boot_qualified); + KUNIT_EXPECT_PTR_EQ(test, v->regs, ave_t8112.regs); + KUNIT_EXPECT_EQ(test, v->boot_device_id, 7U); + KUNIT_EXPECT_EQ(test, v->device_type, 7U); + KUNIT_EXPECT_EQ(test, v->boot_arg_value, 0x10000U); + /* The M1's timebase frequency register is in MHz, as on the M1 Pro. */ + KUNIT_EXPECT_EQ(test, v->timebase_multiplier, 1000000U); + KUNIT_EXPECT_EQ(test, v->mbox_idle, 0x38U); + KUNIT_EXPECT_TRUE(test, v->restore_data_first); + /* The loader fills the DATA link cell, as for the 6070 builds; no build list. */ + KUNIT_EXPECT_EQ(test, v->text_link_cell, 0x423cU); + KUNIT_EXPECT_NULL(test, v->builds); + KUNIT_EXPECT_EQ(test, v->lfs_ref_scale, 2U); + KUNIT_EXPECT_EQ(test, v->extra0_size, 0U); + /* The J313's loader code base for TEXT at 0x800a20000. */ + KUNIT_EXPECT_TRUE(test, v->vcbar_text_phys); + KUNIT_EXPECT_EQ(test, ave_fw_vcbar(v, 0x800a20000ULL), 0x102000800a20001ULL); + KUNIT_EXPECT_FALSE(test, ave_t8112.vcbar_text_phys); + KUNIT_EXPECT_EQ(test, v->text_crc, AVE_FW_TEXT_CRC_T8103_26B123); + KUNIT_EXPECT_PTR_EQ(test, ave_layout_for_crc(v->text_crc), &ave_layout_26b123); + { + const u32 off = v->text_link_cell, size = off + 0x40; + u8 *text = kunit_kzalloc(test, size, GFP_KERNEL); + u32 zeroed, filled; + + KUNIT_ASSERT_NOT_NULL(test, text); + zeroed = crc32_le(~0U, text, size) ^ ~0U; + /* DATA follows TEXT at 0x114000; the loader's alias keeps those low bits. */ + put_unaligned_le64(0xf00114000ULL, text + off); + KUNIT_EXPECT_EQ(test, ave_fw_text_id_crc(text, size, off, 0x114000, &filled), 0); + KUNIT_EXPECT_EQ(test, filled, zeroed); + /* A cell that names another DATA address is refused. */ + KUNIT_EXPECT_EQ(test, ave_fw_text_id_crc(text, size, off, 0x118000, &filled), + -ENODEV); + } +} + +static void ave_t8103_write_dapf(void __iomem *dapf, + const struct ave_dapf_entry *e, + unsigned int n) +{ + memset_io(dapf, 0, AVE_DAPF_ENTRIES * AVE_DAPF_STRIDE); + for (unsigned int i = 0; i < n; i++) { + void __iomem *r = dapf + i * AVE_DAPF_STRIDE; + + writel(e[i].ctrl, r + AVE_DAPF_CTRL); + writeq(e[i].start, r + AVE_DAPF_START); + writeq(e[i].end, r + AVE_DAPF_END); + } +} + +/* + * The J313's encoder CPU DART filter as its ADT describes it: TEXT at its + * physical address, and the window the firmware reaches DART space through. + * The driver starts the coprocessor only when the filter admits TEXT and + * the firmware's nonzero DATA alias (the link cell); an unprogrammed filter + * reads zero and admits nothing. + */ +static void ave_t8103_cpu_dart_filter(struct kunit *test) +{ + const u64 text = 0x800a20000ULL, text_last = text + 0x114000 - 1; + const u64 alias = 0xf00114000ULL, alias_last = alias + 0x128000 - 1; + struct ave_dapf_entry e[AVE_DAPF_ENTRIES] = { + { .ctrl = 0x11, .start = text, .end = text_last }, + { .ctrl = 0x33, .start = 0xf00000000ULL, .end = 0xfffffffffULL }, + }; + struct ave_dapf_entry none[AVE_DAPF_ENTRIES] = {}; + struct ave_dapf_entry text_only[AVE_DAPF_ENTRIES] = { + { .ctrl = 0x11, .start = text, .end = text_last }, + }; + struct ave_dev *ave = kunit_kzalloc(test, sizeof(*ave), GFP_KERNEL); + void __iomem *dapf; + u64 first = 0, last = 0; + const char *gap; + + KUNIT_ASSERT_NOT_NULL(test, ave); + dapf = (void __iomem __force *)kunit_kzalloc(test, + AVE_DAPF_ENTRIES * AVE_DAPF_STRIDE, + GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, dapf); + ave->dev = kunit_device_register(test, "apple-ave-dapf-test"); + KUNIT_ASSERT_FALSE(test, IS_ERR(ave->dev)); + ave->variant = &ave_t8103; + ave->text.phys = text; + ave->text.size = 0x114000; + ave->data.size = 0x128000; + ave->data_alias = alias; + ave->dapf = dapf; + ave->dapf_phys = 0x267044000ULL; + + KUNIT_EXPECT_TRUE(test, ave_dapf_admits(e, AVE_DAPF_ENTRIES, text, text_last)); + KUNIT_EXPECT_TRUE(test, ave_dapf_admits(e, AVE_DAPF_ENTRIES, alias, alias_last)); + /* A filter nothing programmed reads zero. */ + KUNIT_EXPECT_FALSE(test, ave_dapf_admits(none, AVE_DAPF_ENTRIES, text, text_last)); + KUNIT_EXPECT_FALSE(test, ave_dapf_admits(none, AVE_DAPF_ENTRIES, alias, alias_last)); + /* The raw DART address is outside both entries. */ + KUNIT_EXPECT_FALSE(test, ave_dapf_admits(e, AVE_DAPF_ENTRIES, 0x114000, 0x23bfff)); + /* One byte past an entry, or a range across two, is not admitted. */ + KUNIT_EXPECT_FALSE(test, ave_dapf_admits(e, AVE_DAPF_ENTRIES, text, text_last + 1)); + KUNIT_EXPECT_FALSE(test, ave_dapf_admits(e, AVE_DAPF_ENTRIES, text - SZ_16K, text_last)); + KUNIT_EXPECT_FALSE(test, ave_dapf_admits(e, AVE_DAPF_ENTRIES, text_last, 0xf00000000ULL)); + /* An end whose low two bits do not read back still covers its range. */ + e[0].end = text_last & ~3ULL; + KUNIT_EXPECT_TRUE(test, ave_dapf_admits(e, AVE_DAPF_ENTRIES, text, text_last)); + + /* Valid filter with a nonzero DATA alias passes the pre-start guard. */ + ave_t8103_write_dapf(dapf, e, 2); + KUNIT_EXPECT_NULL(test, ave_fw_filter_gap(ave, e, &first, &last)); + KUNIT_EXPECT_EQ(test, ave_fw_check_filter(ave), 0); + + /* A required filter that is unavailable or unmapped is refused separately. */ + ave->dapf = NULL; + KUNIT_EXPECT_EQ(test, ave_fw_check_filter(ave), -ENODEV); + ave->variant = &ave_t8112; + KUNIT_EXPECT_EQ(test, ave_fw_check_filter(ave), 0); + ave->variant = &ave_t8103; + ave->dapf = dapf; + + /* A mapped, unprogrammed filter (all-zero entries) is refused. */ + ave_t8103_write_dapf(dapf, none, 0); + gap = ave_fw_filter_gap(ave, none, &first, &last); + KUNIT_ASSERT_NOT_NULL(test, gap); + KUNIT_EXPECT_STREQ(test, gap, "TEXT"); + KUNIT_EXPECT_EQ(test, ave_fw_check_filter(ave), -ENODEV); + + /* An unlinked handoff (zero DATA alias) is refused even on a TEXT-only filter. */ + ave->data_alias = 0; + gap = ave_fw_filter_gap(ave, e, &first, &last); + KUNIT_ASSERT_NOT_NULL(test, gap); + KUNIT_EXPECT_STREQ(test, gap, "DATA"); + KUNIT_EXPECT_EQ(test, ave_fw_check_filter(ave), -ENODEV); + ave_t8103_write_dapf(dapf, text_only, 1); + gap = ave_fw_filter_gap(ave, text_only, &first, &last); + KUNIT_ASSERT_NOT_NULL(test, gap); + KUNIT_EXPECT_STREQ(test, gap, "DATA"); + KUNIT_EXPECT_EQ(test, ave_fw_check_filter(ave), -ENODEV); + + /* An excluded or wrapping DATA alias is refused. */ + ave->data_alias = 0x114000ULL; + ave_t8103_write_dapf(dapf, e, 2); + gap = ave_fw_filter_gap(ave, e, &first, &last); + KUNIT_ASSERT_NOT_NULL(test, gap); + KUNIT_EXPECT_STREQ(test, gap, "DATA"); + KUNIT_EXPECT_EQ(test, ave_fw_check_filter(ave), -ENODEV); + + ave->data_alias = U64_MAX - 0x1000ULL; + gap = ave_fw_filter_gap(ave, e, &first, &last); + KUNIT_ASSERT_NOT_NULL(test, gap); + KUNIT_EXPECT_STREQ(test, gap, "DATA"); + KUNIT_EXPECT_EQ(test, ave_fw_check_filter(ave), -ENODEV); + + ave->data_alias = alias; + gap = ave_fw_filter_gap(ave, text_only, &first, &last); + KUNIT_ASSERT_NOT_NULL(test, gap); + KUNIT_EXPECT_STREQ(test, gap, "DATA"); + ave_t8103_write_dapf(dapf, text_only, 1); + KUNIT_EXPECT_EQ(test, ave_fw_check_filter(ave), -ENODEV); + + /* After a deep sleep the range stays but the enable bits are clear. */ + e[0].ctrl = 0x10; + e[1].ctrl = 0x30; + KUNIT_EXPECT_FALSE(test, ave_dapf_admits(e, AVE_DAPF_ENTRIES, text, text_last)); + KUNIT_EXPECT_FALSE(test, ave_dapf_admits(e, AVE_DAPF_ENTRIES, alias, alias_last)); + e[0].ctrl = 0x11; + ave_t8103_write_dapf(dapf, e, 2); + gap = ave_fw_filter_gap(ave, e, &first, &last); + KUNIT_ASSERT_NOT_NULL(test, gap); + KUNIT_EXPECT_STREQ(test, gap, "DATA"); + KUNIT_EXPECT_EQ(test, ave_fw_check_filter(ave), -ENODEV); + + e[1].ctrl = 0x33; + /* A range with no control word is not programmed. */ + e[0].ctrl = 0; + KUNIT_EXPECT_FALSE(test, ave_dapf_admits(e, AVE_DAPF_ENTRIES, text, text_last)); + KUNIT_EXPECT_FALSE(test, ave_dapf_admits(e, AVE_DAPF_ENTRIES, text_last, text)); + ave_t8103_write_dapf(dapf, e, 2); + gap = ave_fw_filter_gap(ave, e, &first, &last); + KUNIT_ASSERT_NOT_NULL(test, gap); + KUNIT_EXPECT_STREQ(test, gap, "TEXT"); + KUNIT_EXPECT_EQ(test, ave_fw_check_filter(ave), -ENODEV); + /* Only the entries counted are looked at. */ + KUNIT_EXPECT_FALSE(test, ave_dapf_admits(e, 1, alias, alias_last)); +} + static void ave_t6041_text_boot_word(struct kunit *test) { const struct ave_fw_build *m4 = ave_t6041.builds; @@ -1748,6 +1948,8 @@ static struct kunit_case ave_abi_cases[] = { KUNIT_CASE(ave_26b123_layout), KUNIT_CASE(ave_t8112_26b123_build), KUNIT_CASE(ave_hevc_bframe_extra_bits), + KUNIT_CASE(ave_t8103_26b123_build), + KUNIT_CASE(ave_t8103_cpu_dart_filter), {} }; diff --git a/drivers/media/platform/apple/ave/ave-drv.c b/drivers/media/platform/apple/ave/ave-drv.c index bd3568d34deae..3f1971faefa31 100644 --- a/drivers/media/platform/apple/ave/ave-drv.c +++ b/drivers/media/platform/apple/ave/ave-drv.c @@ -118,6 +118,43 @@ const struct ave_variant ave_t8112 = { .next_build = &ave_t8112_26b123, }; +/* + * The M1 (T8103) with the macOS 27.2 stub, as on the MacBook Air (J313): the + * bootloader preloads TEXT 0x114000 and DATA 0x128000 and, as it does for the + * M1 Pro's and M2's 6070 builds, fills the 64-bit cell at TEXT 0x423c with + * the firmware's alias of DATA (the low 32 bits are DATA's DART address); the + * cell is zero in the image, and with it zeroed the TEXT CRC is b6616340. It + * is the M1 encoder's build of the firmware the M2 and M3 run on 27.2 + * (9013.55.1), and it starts the way theirs do: + * the boot configuration handshake as device 7 with handshake type 7, the + * 26B123 command layout, codec clocks ungated by clearing the mailbox idle + * word, the bootloader's code base (marked valid) left as it is, and DATA + * restored from the boot snapshot before every start. The coprocessor and + * mailbox registers are the M2's. As on every M1-family encoder, the DART + * window starts at address 0: TEXT is at 0 and DATA follows it. The code base + * the loader sets is TEXT's physical address, its ADT remap address, with + * VALID and two more bits of its own. Unlike the M2's, its timebase + * frequency register counts in MHz, as the M1 Pro's does. + */ +const struct ave_variant ave_t8103 = { + .regs = &ave_coproc_6070, + .fw_text_size = 0x114000, + .fw_data_size = 0x128000, + .boot_device_id = 7, + .boot_arg_value = AVE_COPROC_BOOT_ARG_VALUE, + .timebase_multiplier = 1000000, + .vcbar_mask = GENMASK_ULL(41, 11), + .vcbar_flags = AVE_COPROC_VCBAR_VALID | AVE_COPROC_VCBAR_M1_LOADER, + .vcbar_text_phys = true, + .device_type = 7, + .lfs_ref_scale = 2, + .text_crc = AVE_FW_TEXT_CRC_T8103_26B123, + .text_link_cell = 0x423c, + .mbox_idle = 0x38, + .restore_data_first = true, + .boot_qualified = 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 @@ -500,11 +537,38 @@ static void ave_debugfs_init(struct ave_dev *ave) static const char *const ave_pd_names[] = { "dma", "pipe4", "pipe5", "me0", "me1", "perf" }; +/* + * Map the address filter a DART of the encoder names ("dapf"), read-only, + * for ave_fw_check_filter(). The loader programs it; the DART driver leaves + * a filter it has no ranges for untouched. + */ +static int ave_map_dapf(struct ave_dev *ave, struct device_node *dart) +{ + int idx = of_property_match_string(dart, "reg-names", "dapf"); + struct resource res; + + if (idx < 0) + return 0; + if (ave->dapf) { + dev_warn(ave->dev, "%pOF: a second DART filter; keeping the first\n", dart); + return 0; + } + if (of_address_to_resource(dart, idx, &res) || + resource_size(&res) < AVE_DAPF_ENTRIES * AVE_DAPF_STRIDE) + return dev_err_probe(ave->dev, -EINVAL, "%pOF: bad DART filter window\n", dart); + ave->dapf = devm_ioremap(ave->dev, res.start, resource_size(&res)); + if (!ave->dapf) + return -ENOMEM; + ave->dapf_phys = res.start; + return 0; +} + /* * Map the encoder's T8110-layout DARTs for the read-only stream audit - * (ave_dart_audit()). The DART driver owns them; nothing here writes. + * (ave_dart_audit()), and the address filter of any of its DARTs that has + * one. The DART driver owns them; nothing here writes. */ -static void ave_map_darts(struct ave_dev *ave) +static int ave_map_darts(struct ave_dev *ave) { struct device_node *np = ave->dev->of_node; int n = of_count_phandle_with_args(np, "iommus", "#iommu-cells"); @@ -514,9 +578,15 @@ static void ave_map_darts(struct ave_dev *ave) struct resource res; void __iomem *r; unsigned int d; + int ret; if (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", i, &args)) break; + ret = ave_map_dapf(ave, args.np); + if (ret) { + of_node_put(args.np); + return ret; + } if (!of_device_is_compatible(args.np, "apple,t8110-dart") || args.args_count != 1 || args.args[0] >= 32 || of_address_to_resource(args.np, 0, &res)) { @@ -544,6 +614,7 @@ static void ave_map_darts(struct ave_dev *ave) ave->n_darts++; } dev_dbg(ave->dev, "%u DARTs for the stream audit\n", ave->n_darts); + return 0; } static int ave_probe(struct platform_device *pdev) @@ -582,7 +653,9 @@ static int ave_probe(struct platform_device *pdev) ave->domain = iommu_get_domain_for_dev(dev); if (!ave->domain || !(ave->domain->type & __IOMMU_DOMAIN_PAGING)) return dev_err_probe(dev, -ENODEV, "needs a translating IOMMU\n"); - ave_map_darts(ave); + ret = ave_map_darts(ave); + if (ret) + return ret; ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(42)); if (ret) return ret; @@ -737,6 +810,7 @@ static const struct of_device_id ave_of_match[] = { { .compatible = "apple,t6032-ave", .data = &ave_m3 }, { .compatible = "apple,t6041-ave", .data = &ave_t6041 }, { .compatible = "apple,t6050-ave", .data = &ave_t6050 }, + { .compatible = "apple,t8103-ave", .data = &ave_t8103 }, { .compatible = "apple,t8112-ave", .data = &ave_t8112 }, { .compatible = "apple,t8122-ave", .data = &ave_t8122 }, { .compatible = "apple,t8132-ave", .data = &ave_t8132 }, diff --git a/drivers/media/platform/apple/ave/ave-fw.c b/drivers/media/platform/apple/ave/ave-fw.c index 9522672040711..643ead9cb0d11 100644 --- a/drivers/media/platform/apple/ave/ave-fw.c +++ b/drivers/media/platform/apple/ave/ave-fw.c @@ -45,6 +45,7 @@ static const u32 ave_known_text_crc[] = { AVE_FW_TEXT_CRC_T8142_26A428, /* encoder firmware 9013.45.1, macOS 27.0 26A428 / T8142 */ AVE_FW_TEXT_CRC_T8122_26B123, /* encoder firmware macOS 27.2 26B123 / T8122 */ AVE_FW_TEXT_CRC_T8112_26B123, /* encoder firmware macOS 27.2 26B123 / T8112 */ + AVE_FW_TEXT_CRC_T8103_26B123, /* encoder firmware 9013.55.1, macOS 27.2 / T8103 */ AVE_FW_TEXT_CRC_T8132_26B123, /* encoder firmware macOS 27.2 26B123 / T8132 */ AVE_FW_TEXT_CRC_T8142_26B123, /* encoder firmware 9013.55.1, macOS 27.2 26B123 / T8142 */ 0x460c7a7c, /* encoder firmware 6070.11.1 / T6000 */ @@ -202,6 +203,11 @@ static int ave_fw_text_boot_id(struct ave_dev *ave, u32 word_off, u32 *crc) return ret; } +static bool ave_fw_range_valid(u64 start, size_t size) +{ + return start && size && start <= U64_MAX - (size - 1); +} + static int ave_fw_text_id(struct ave_dev *ave, const struct ave_fw_build *build, u32 *crc) { @@ -217,6 +223,9 @@ static int ave_fw_text_id(struct ave_dev *ave, const struct ave_fw_build *build, ret = ave_fw_text_id_crc(va, ave->text.size, ave->variant->text_link_cell, ave->data.iova, crc); + /* The cell is in bounds once the identity accepted it. */ + if (!ret) + ave->data_alias = get_unaligned_le64(va + ave->variant->text_link_cell); memunmap(va); return ret; } @@ -339,6 +348,10 @@ int ave_fw_probe(struct ave_dev *ave) if (ret) return dev_err_probe(dev, ret, "firmware text link cell is not the DATA address\n"); + if ((ave->dapf || ave->variant->vcbar_text_phys) && + !ave_fw_range_valid(ave->data_alias, ave->data.size)) + return dev_err_probe(dev, -ENODEV, + "firmware DATA alias is missing or invalid\n"); known |= ave_fw_known_crc(text_crc); if (build) { const struct ave_fw_build *same_size = @@ -538,10 +551,110 @@ u64 ave_fw_vcbar(const struct ave_variant *variant, u64 text_iova) return (text_iova & variant->vcbar_mask) | variant->vcbar_flags; } +/* The code base the loader left: TEXT's DART or, on the M1, physical address. */ +static u64 ave_fw_code_base(struct ave_dev *ave) +{ + return ave_fw_vcbar(ave->variant, ave->variant->vcbar_text_phys ? + ave->text.phys : ave->text.iova); +} + +/* + * Whether one programmed entry of the filter admits all of [first, last]. + * An entry the loader never wrote reads zero. A deep sleep clears the low + * control bits and keeps the range: the J313 reads 0x11 and 0x33 after boot, + * 0x10 and 0x30 after a wake, and a coprocessor started then reset the SoC. + * The low two bits of an end may not read back as written, so the end is + * compared with them set. + */ +bool ave_dapf_admits(const struct ave_dapf_entry *e, unsigned int n, u64 first, + u64 last) +{ + if (last < first) + return false; + for (unsigned int i = 0; i < n; i++) + if ((e[i].ctrl & AVE_DAPF_CTRL_ENABLE) && e[i].start <= first && + (e[i].end | 3) >= last) + return true; + return false; +} + +static void ave_fw_read_dapf(struct ave_dev *ave, struct ave_dapf_entry *e) +{ + for (unsigned int i = 0; i < AVE_DAPF_ENTRIES; i++) { + void __iomem *r = ave->dapf + i * AVE_DAPF_STRIDE; + + e[i].ctrl = readl(r + AVE_DAPF_CTRL); + e[i].start = readq(r + AVE_DAPF_START); + e[i].end = readq(r + AVE_DAPF_END); + } +} + +static bool ave_fw_filter_admits(const struct ave_dapf_entry *e, u64 start, + size_t size, u64 *first, u64 *last) +{ + *first = start; + if (!ave_fw_range_valid(start, size)) { + *last = start; + return false; + } + *last = start + size - 1; + return ave_dapf_admits(e, AVE_DAPF_ENTRIES, *first, *last); +} + +/* The firmware range the filter leaves out, with its bounds, or NULL. */ +const char *ave_fw_filter_gap(struct ave_dev *ave, + const struct ave_dapf_entry *e, u64 *first, + u64 *last) +{ + if (ave->variant->vcbar_text_phys && + !ave_fw_filter_admits(e, ave->text.phys, ave->text.size, first, last)) + return "TEXT"; + if (!ave_fw_filter_admits(e, ave->data_alias, ave->data.size, first, last)) + return "DATA"; + return NULL; +} + +/* + * The coprocessor's CPU reaches memory through its DART's address filter: + * on the M1 the loader points the code base at TEXT's physical address, and + * the firmware's own alias of DATA is in the window the filter passes on to + * the DART. Only the bootloader programs that filter, from the ADT. A core + * started behind a filter that does not admit its code cannot fetch its + * first instruction. Check the filter, read-only, before anything is + * written to start the coprocessor. + */ +int ave_fw_check_filter(struct ave_dev *ave) +{ + struct ave_dapf_entry e[AVE_DAPF_ENTRIES]; + const char *missing; + u64 first = 0, last = 0; + + if (!ave->dapf) { + if (ave->variant->vcbar_text_phys) { + dev_err(ave->dev, + "CPU DART filter is unavailable; not starting the coprocessor\n"); + return -ENODEV; + } + return 0; + } + ave_fw_read_dapf(ave, e); + missing = ave_fw_filter_gap(ave, e, &first, &last); + if (!missing) + return 0; + dev_err(ave->dev, + "CPU DART filter at %pa does not admit the firmware's %s (%#llx-%#llx); not starting the coprocessor\n", + &ave->dapf_phys, missing, first, last); + for (unsigned int i = 0; i < AVE_DAPF_ENTRIES; i++) + if (e[i].ctrl || e[i].start || e[i].end) + dev_err(ave->dev, " filter %2u: ctrl %#x %#llx-%#llx\n", + i, e[i].ctrl, e[i].start, e[i].end); + return -ENODEV; +} + static bool ave_fw_idle(struct ave_dev *ave) { if (readq(ave->coproc + ave->variant->regs->vcbar) != - ave_fw_vcbar(ave->variant, ave->text.iova)) + ave_fw_code_base(ave)) return false; if (readl(ave->coproc + ave->variant->regs->ctrl) || readl(ave->mbox + AVE_MBOX_STATUS)) @@ -585,7 +698,7 @@ static int ave_fw_prepare_restart(struct ave_dev *ave) return 0; } if (readq(ave->coproc + ave->variant->regs->vcbar) != - ave_fw_vcbar(ave->variant, ave->text.iova)) { + ave_fw_code_base(ave)) { dev_err(ave->dev, "coprocessor lost its code base (vcbar %#llx)\n", readq(ave->coproc + ave->variant->regs->vcbar)); @@ -713,6 +826,24 @@ void ave_dart_dump(struct ave_dev *ave, struct seq_file *m) seq_printf(m, "coprocessor ctrl %#x boot_arg %#x fw_state %d\n", readl(ave->coproc + ave->variant->regs->ctrl), readl(ave->coproc + ave->variant->regs->boot_arg), ave->fw_state); + if (ave->dapf) { + struct ave_dapf_entry e[AVE_DAPF_ENTRIES]; + const char *missing; + u64 first = 0, last = 0; + + ave_fw_read_dapf(ave, e); + missing = ave_fw_filter_gap(ave, e, &first, &last); + if (missing) + seq_printf(m, "CPU DART filter %pa: does NOT admit the firmware's %s (%#llx-%#llx)\n", + &ave->dapf_phys, missing, first, last); + else + seq_printf(m, "CPU DART filter %pa: admits the firmware\n", + &ave->dapf_phys); + for (unsigned int i = 0; i < AVE_DAPF_ENTRIES; i++) + if (e[i].ctrl || e[i].start || e[i].end) + seq_printf(m, " filter %2u: ctrl %#x %#llx-%#llx\n", + i, e[i].ctrl, e[i].start, e[i].end); + } for (unsigned int d = 0; d < ave->n_darts; d++) { void __iomem *r = ave->dart_regs[d]; u32 own = ave->dart_sid[d]; @@ -773,6 +904,11 @@ static int ave_fw_boot(struct ave_dev *ave) return -EIO; } + /* Before any coprocessor or mailbox write. */ + ret = ave_fw_check_filter(ave); + if (ret) + return ret; + ret = ave_fw_prepare_restart(ave); 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 b05103511532f..e8e9284ded0b3 100644 --- a/drivers/media/platform/apple/ave/ave-layout.c +++ b/drivers/media/platform/apple/ave/ave-layout.c @@ -1008,7 +1008,8 @@ const struct ave_fw_layout *ave_layout_for_crc(u32 crc) return &ave_layout_26a428; if (crc == AVE_FW_TEXT_CRC_T8132_26B123 || crc == AVE_FW_TEXT_CRC_T8122_26B123 || - crc == AVE_FW_TEXT_CRC_T8112_26B123) + crc == AVE_FW_TEXT_CRC_T8112_26B123 || + crc == AVE_FW_TEXT_CRC_T8103_26B123) return &ave_layout_26b123; /* * The M5's macOS 27.2 build (9013.55.1), between 26A428 and the 27.3 diff --git a/drivers/media/platform/apple/ave/ave-regs.h b/drivers/media/platform/apple/ave/ave-regs.h index c803c028467be..5f5a2db4ebbdf 100644 --- a/drivers/media/platform/apple/ave/ave-regs.h +++ b/drivers/media/platform/apple/ave/ave-regs.h @@ -25,6 +25,11 @@ /* Coprocessor window */ #define AVE_COPROC_VCBAR 0x50000 #define AVE_COPROC_VCBAR_VALID BIT(0) +/* + * Bits the M1's 27.2 loader sets in the code base next to VALID. The driver + * never writes the register; it only checks that the value is still there. + */ +#define AVE_COPROC_VCBAR_M1_LOADER (BIT_ULL(56) | BIT_ULL(49)) #define AVE_COPROC_TIMEBASE_FREQ 0x360020 #define AVE_COPROC_TIMEBASE 0x378000 #define AVE_COPROC_CTRL 0x600044 @@ -34,6 +39,19 @@ #define AVE_COPROC_BOOT_ARG_VALUE 0x10000 #define AVE_COPROC_KEEP_MAPPINGS 0x600808 +/* + * Address filter of the encoder's CPU DART (its "dapf" register window): + * entries 0x40 apart, each a control word and an inclusive address range. + * The driver only reads it. + */ +#define AVE_DAPF_ENTRIES 16 +#define AVE_DAPF_STRIDE 0x40 +#define AVE_DAPF_CTRL 0x00 +/* The control bits a deep sleep clears; an entry without them admits nothing. */ +#define AVE_DAPF_CTRL_ENABLE GENMASK(3, 0) +#define AVE_DAPF_START 0x08 +#define AVE_DAPF_END 0x10 + /* Boot request and the configuration block it points at */ #define AVE_BOOT_MAGIC 0x08042006 #define AVE_BOOT_CFG_BYTES 0x40 diff --git a/drivers/media/platform/apple/ave/ave.h b/drivers/media/platform/apple/ave/ave.h index 331c866381399..6ed0dd6b60a10 100644 --- a/drivers/media/platform/apple/ave/ave.h +++ b/drivers/media/platform/apple/ave/ave.h @@ -112,6 +112,13 @@ struct ave_fw_segment { size_t size; }; +/* One entry of the CPU DART's address filter, as read back */ +struct ave_dapf_entry { + u32 ctrl; + u64 start; + u64 end; /* inclusive */ +}; + struct ave_ring { char name[16]; u32 mode; @@ -189,6 +196,11 @@ enum ave_opcode { * per-boot word at TEXT 0x4224 zeroed. */ #define AVE_FW_TEXT_CRC_T8112_26B123 0x077a3444 +/* + * The M1 (T8103) build preloaded by the macOS 27.2 stub (encoder firmware + * 9013.55.1), with the DATA link cell at TEXT 0x423c zeroed. + */ +#define AVE_FW_TEXT_CRC_T8103_26B123 0xb6616340 /* * The M5 build of macOS 27.2 (26B123; 27.2.1 26B131 ships it unchanged), * AppleAVE2FW 9013.55.1: TEXT 0x154000, DATA 0x12c000, as 26A428's. @@ -242,6 +254,11 @@ struct ave_variant { u32 timebase_multiplier; u64 vcbar_mask; u64 vcbar_flags; + /* + * The loader points the code base at TEXT's physical address (the + * M1's TEXT remap address) instead of its DART address. + */ + bool vcbar_text_phys; /* * TEXT offset of the 64-bit cell the loader fills with the DATA * address; it is zero in the firmware image. 0 when there is none. @@ -291,6 +308,11 @@ struct ave_dev { struct ave_fw_segment text; struct ave_fw_segment data; + /* + * The firmware's own address of DATA, from the TEXT link cell the + * loader filled; 0 where there is no cell or it was left zero. + */ + u64 data_alias; u32 text_crc; u32 data_crc; void *data_pristine; @@ -372,6 +394,12 @@ struct ave_dev { /* every stream the device tree attaches on that DART, bit per SID */ u32 dart_sid_mask[2]; unsigned int n_darts; + /* + * The address filter of the DART the coprocessor's CPU goes through, + * where the device tree names one ("dapf"); read-only, NULL otherwise. + */ + void __iomem *dapf; + phys_addr_t dapf_phys; bool witness_unusable; /* Times the MCTF pool CRC changed while no session held it */ u32 mctf_pool_changed; @@ -578,8 +606,8 @@ 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_t6050, ave_t8112, - ave_t8132, ave_m3; +extern const struct ave_variant ave_t6000, ave_t6041, ave_t6050, ave_t8103, + ave_t8112, ave_t8132, ave_m3; /* ave-fw.c */ u64 ave_fw_vcbar(const struct ave_variant *variant, u64 text_iova); @@ -591,6 +619,12 @@ int ave_fw_text_id_crc(const void *text, size_t size, u32 cell_off, u64 data_iova, u32 *crc); int ave_fw_text_boot_crc(const void *text, size_t size, u32 word_off, u32 *crc); +bool ave_dapf_admits(const struct ave_dapf_entry *e, unsigned int n, u64 first, + u64 last); +const char *ave_fw_filter_gap(struct ave_dev *ave, + const struct ave_dapf_entry *e, u64 *first, + u64 *last); +int ave_fw_check_filter(struct ave_dev *ave); void ave_fw_remove(struct ave_dev *ave); int ave_fw_ensure_running(struct ave_dev *ave); int ave_dart_audit(struct ave_dev *ave, const char *when); diff --git a/drivers/media/platform/apple/ave/tests/recovery-boundary.h b/drivers/media/platform/apple/ave/tests/recovery-boundary.h index 3c4df20fb2463..91055c390baf8 100644 --- a/drivers/media/platform/apple/ave/tests/recovery-boundary.h +++ b/drivers/media/platform/apple/ave/tests/recovery-boundary.h @@ -7,6 +7,7 @@ #define dev_err_ratelimited(...) test_log(__VA_ARGS__) #define lockdep_assert_held(p) assert((p)->held) #define MEMREMAP_WC 0 +#define __iomem /* Host memory is coherent; the production write barrier is simulated. */ #define wmb() ((void)0) #define msecs_to_jiffies(x) (x) @@ -86,6 +87,9 @@ struct ave_dev { const struct ave_fw_layout *layout; u32 data_crc; struct ave_fw_segment text, data; + u64 data_alias; + void *dapf; + phys_addr_t dapf_phys; void *data_pristine; struct ave_ring ring[2]; u64 ipc_fw_base; @@ -158,6 +162,11 @@ static void writel(u32 v, void *p) } } +static void writeq(u64 v, void *p) +{ + *(u64 *)p = v; +} + static void *memremap(phys_addr_t p, size_t size, unsigned int mode) { return map_fail ? NULL : (void *)p; diff --git a/drivers/media/platform/apple/ave/tests/recovery-test.c b/drivers/media/platform/apple/ave/tests/recovery-test.c index b5fa981b30ca5..a77da02f7bca0 100644 --- a/drivers/media/platform/apple/ave/tests/recovery-test.c +++ b/drivers/media/platform/apple/ave/tests/recovery-test.c @@ -563,8 +563,102 @@ static void unconfirmed_close(void) free(ctx[1].sess); } +static void set_dapf(u8 *regs, unsigned int idx, u32 ctrl, u64 start, u64 end) +{ + u8 *r = regs + idx * AVE_DAPF_STRIDE; + + writel(ctrl, r + AVE_DAPF_CTRL); + writeq(start, r + AVE_DAPF_START); + writeq(end, r + AVE_DAPF_END); +} + +/* + * Pre-start CPU DART filter guard rejects unmapped filters and zero, wrapping, + * excluded, or disabled ranges. + */ +static void cpu_dart_filter(void) +{ + static const struct ave_variant t8103_v = { + .regs = ®s, + .vcbar_mask = GENMASK_ULL(41, 11), + .vcbar_flags = AVE_COPROC_VCBAR_VALID | AVE_COPROC_VCBAR_M1_LOADER, + .vcbar_text_phys = true, + }; + const u64 text = 0x800a20000ULL, text_last = text + 0x114000 - 1; + const u64 alias = 0xf00114000ULL; + u8 dapf_regs[AVE_DAPF_ENTRIES * AVE_DAPF_STRIDE] = {}; + struct ave_dapf_entry e[AVE_DAPF_ENTRIES] = {}; + u64 first = 0, last = 0; + + init(); + ave.variant = &t8103_v; + ave.text.phys = text; + ave.text.size = 0x114000; + ave.data.size = 0x128000; + ave.data_alias = alias; + ave.dapf = dapf_regs; + ave.dapf_phys = 0x267044000ULL; + + set_dapf(dapf_regs, 0, 0x11, text, text_last); + set_dapf(dapf_regs, 1, 0x33, 0xf00000000ULL, 0xfffffffffULL); + ave_fw_read_dapf(&ave, e); + assert(!ave_fw_filter_gap(&ave, e, &first, &last)); + assert(ave_fw_check_filter(&ave) == 0); + + /* Required filter missing or unmapped is refused despite valid handoff. */ + ave.dapf = NULL; + assert(ave_fw_check_filter(&ave) == -ENODEV); + ave.variant = &variant; + assert(ave_fw_check_filter(&ave) == 0); + ave.variant = &t8103_v; + ave.dapf = dapf_regs; + + /* Mapped, unprogrammed filter (all-zero entries) is refused as TEXT. */ + memset(dapf_regs, 0, sizeof(dapf_regs)); + ave_fw_read_dapf(&ave, e); + assert(!strcmp(ave_fw_filter_gap(&ave, e, &first, &last), "TEXT")); + assert(ave_fw_check_filter(&ave) == -ENODEV); + + set_dapf(dapf_regs, 0, 0x11, text, text_last); + set_dapf(dapf_regs, 1, 0x33, 0xf00000000ULL, 0xfffffffffULL); + ave_fw_read_dapf(&ave, e); + + /* Unlinked handoff (zero DATA alias) is refused even with a TEXT-only filter. */ + ave.data_alias = 0; + assert(!strcmp(ave_fw_filter_gap(&ave, e, &first, &last), "DATA")); + assert(ave_fw_check_filter(&ave) == -ENODEV); + set_dapf(dapf_regs, 1, 0, 0, 0); + ave_fw_read_dapf(&ave, e); + assert(!strcmp(ave_fw_filter_gap(&ave, e, &first, &last), "DATA")); + assert(ave_fw_check_filter(&ave) == -ENODEV); + + /* Wrapping and excluded DATA alias (raw DART address, unprogrammed, sleep-cleared). */ + ave.data_alias = U64_MAX - 0x1000ULL; + set_dapf(dapf_regs, 1, 0x33, 0xf00000000ULL, 0xfffffffffULL); + ave_fw_read_dapf(&ave, e); + assert(!strcmp(ave_fw_filter_gap(&ave, e, &first, &last), "DATA")); + assert(ave_fw_check_filter(&ave) == -ENODEV); + + ave.data_alias = 0x114000ULL; + assert(!strcmp(ave_fw_filter_gap(&ave, e, &first, &last), "DATA")); + assert(ave_fw_check_filter(&ave) == -ENODEV); + ave.data_alias = alias; + set_dapf(dapf_regs, 1, 0, 0, 0); + assert(ave_fw_check_filter(&ave) == -ENODEV); + set_dapf(dapf_regs, 1, 0x30, 0xf00000000ULL, 0xfffffffffULL); + assert(ave_fw_check_filter(&ave) == -ENODEV); + + /* Sleep-cleared TEXT entry is refused as TEXT. */ + set_dapf(dapf_regs, 1, 0x33, 0xf00000000ULL, 0xfffffffffULL); + set_dapf(dapf_regs, 0, 0x10, text, text_last); + ave_fw_read_dapf(&ave, e); + assert(!strcmp(ave_fw_filter_gap(&ave, e, &first, &last), "TEXT")); + assert(ave_fw_check_filter(&ave) == -ENODEV); +} + int main(void) { + cpu_dart_filter(); session_lifetime(); unconfirmed_close(); unusable_witness(); diff --git a/drivers/media/platform/apple/ave/tests/run-recovery.py b/drivers/media/platform/apple/ave/tests/run-recovery.py index da00c6ad1a351..51001357e7934 100644 --- a/drivers/media/platform/apple/ave/tests/run-recovery.py +++ b/drivers/media/platform/apple/ave/tests/run-recovery.py @@ -29,7 +29,7 @@ def main(): code += source((src / 'ave-regs.h').read_text()) code += 'struct vb2_v4l2_buffer;\n' code += source((src / 'ave-cmp420.h').read_text()) - for name in ('ave_codec', 'ave_buf', 'ave_fw_segment', 'ave_ring', 'ave_fw_state', + for name in ('ave_codec', 'ave_buf', 'ave_fw_segment', 'ave_dapf_entry', 'ave_ring', 'ave_fw_state', 'ave_coproc_regs', 'ave_variant', 'ave_params', 'ave_slot', 'ave_session'): code += declaration(header, name) code += declaration(header, 'ave_image') @@ -42,8 +42,10 @@ def main(): code += source((src / 'ave-vb2.c').read_text()) code += (here / 'command-boundary.h').read_text() groups = { - 'ave-fw.c': ('ave_fw_vcbar', 'ave_fw_lost', 'ave_fw_mark_broken', 'ave_fw_halt', 'ave_fw_stop_hung', 'ave_fw_shutdown', - 'ave_fw_runtime_suspend', 'ave_fw_prepare_restart', 'ave_fw_restore_data'), + 'ave-fw.c': ('ave_fw_vcbar', 'ave_fw_code_base', 'ave_fw_lost', 'ave_fw_mark_broken', 'ave_fw_halt', 'ave_fw_stop_hung', 'ave_fw_shutdown', + 'ave_fw_runtime_suspend', 'ave_fw_prepare_restart', 'ave_fw_restore_data', + 'ave_fw_range_valid', 'ave_dapf_admits', 'ave_fw_read_dapf', + 'ave_fw_filter_admits', 'ave_fw_filter_gap', 'ave_fw_check_filter'), 'ave-sess.c': ('ave_mctf_pool_crc', 'ave_mctf_pool_check', 'ave_mctf_pool_return', 'ave_sess_domain_unsafe', 'ave_sess_free_bufs', 'ave_sess_destroy', 'ave_sess_reap'), 'ave-v4l2.c': ('ave_return_bufs', 'ave_buf_queue', 'ave_session_index', 'ave_session_count', 'ave_session_close', 'ave_session_free', diff --git a/drivers/media/platform/apple/ave/tests/shim.h b/drivers/media/platform/apple/ave/tests/shim.h index 389ca5a430508..a7c6080086fa1 100644 --- a/drivers/media/platform/apple/ave/tests/shim.h +++ b/drivers/media/platform/apple/ave/tests/shim.h @@ -20,6 +20,7 @@ typedef uint64_t dma_addr_t; #define SZ_16K 0x4000U #define SZ_1M 0x100000U #define U32_MAX UINT32_MAX +#define U64_MAX UINT64_MAX #define GENMASK(h, l) ((~0UL >> (63 - (h))) & (~0UL << (l))) #define GENMASK_ULL(h, l) ((~0ULL >> (63 - (h))) & (~0ULL << (l))) #define BIT(n) (1U << (n)) diff --git a/tools/asahi/ave-probe-pm-tests.py b/tools/asahi/ave-probe-pm-tests.py index 5b02e8a5ff850..acfeda3d42416 100644 --- a/tools/asahi/ave-probe-pm-tests.py +++ b/tools/asahi/ave-probe-pm-tests.py @@ -121,7 +121,7 @@ return 0; } static void ave_debugfs_init(struct ave_dev *a) { (void)a; } -static void ave_map_darts(struct ave_dev *a) { (void)a; } +static int ave_map_darts(struct ave_dev *a) { (void)a; return 0; } ''' CASES = r'''