Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Documentation/admin-guide/media/apple-ave.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions arch/arm64/boot/dts/apple/t8103-peripherals.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -632,17 +632,23 @@
interrupts = <AIC_IRQ 557 IRQ_TYPE_LEVEL_HIGH>;
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 = <AIC_IRQ 557 IRQ_TYPE_LEVEL_HIGH>;
power-domains = <&ps_venc_sys>;
#iommu-cells = <1>;
status = "disabled";
status = "okay";
};

ave: video-codec@267100000 {
Expand All @@ -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 {
Expand Down
102 changes: 102 additions & 0 deletions drivers/iommu/apple-dart-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <kunit/device.h>
#include <kunit/static_stub.h>
#include <kunit/test.h>
#include <linux/unaligned.h>
#include <linux/vmalloc.h>

struct apple_dart_test_window {
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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),
Expand Down
132 changes: 132 additions & 0 deletions drivers/iommu/apple-dart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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");
Expand Down Expand 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)
Expand Down
3 changes: 2 additions & 1 deletion drivers/media/platform/apple/ave/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Loading
Loading