diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index ba73ba857249a..d8c99eccc0973 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -2092,6 +2092,24 @@ static void dcp_rtkit_live_unmap(struct dcp_rtkit_live *live) live->session.num_buffers = 0; } +/* + * Validate one apple,rtkit-buffers tuple before translating it through the DART. + * Firmware-placed buffers (is_mapped == 1) use RTKit's 4 KiB granularity inside + * a pre-mapped firmware carveout (for example T8103's crashlog at 0x276000 on a + * 16 KiB host kernel); loader-allocated buffers (is_mapped == 0) sit in no-map + * reserved-memory regions and must be host page-aligned. + */ +static bool dcp_rtkit_live_buf_valid(u32 ep, u64 iova, u64 size, u32 is_mapped) +{ + if ((ep != 1 && ep != 2 && ep != 4 && ep != 8) || is_mapped > 1) + return false; + if (!size || size > SZ_16M || iova + size < iova) + return false; + if (is_mapped) + return IS_ALIGNED(iova, SZ_4K) && IS_ALIGNED(size, SZ_4K); + return PAGE_ALIGNED(iova) && PAGE_ALIGNED(size); +} + /* * A boot stage that negotiated RTKit with this DCP and handed it over running * describes the session on the node, with the RTKit handoff properties of other @@ -2167,9 +2185,7 @@ static int dcp_rtkit_live_parse(struct apple_dcp *dcp, struct dcp_rtkit_live *li phys_addr_t phys; /* The system endpoints with shared buffers, as for other handoffs. */ - if ((t[0] != 1 && t[0] != 2 && t[0] != 4 && t[0] != 8) || - t[4] > 1 || !size || size > SZ_16M || !PAGE_ALIGNED(iova) || - !PAGE_ALIGNED(size) || iova + size < iova) { + if (!dcp_rtkit_live_buf_valid(t[0], iova, size, t[4])) { ret = -EINVAL; goto fail; } diff --git a/drivers/gpu/drm/apple/dcp_handoff_test.c b/drivers/gpu/drm/apple/dcp_handoff_test.c index 22c88773b9114..533e7add8ca58 100644 --- a/drivers/gpu/drm/apple/dcp_handoff_test.c +++ b/drivers/gpu/drm/apple/dcp_handoff_test.c @@ -109,9 +109,35 @@ static void dcp_handoff_boot_notch(struct kunit *test) KUNIT_EXPECT_FALSE(test, dcp_handoff_notch(unterminated, sizeof(unterminated))); } +static void dcp_handoff_live_rtkit_buf_valid(struct kunit *test) +{ + /* Firmware-placed (is_mapped == 1) uses RTKit's 4 KiB granularity. */ + KUNIT_EXPECT_TRUE(test, dcp_rtkit_live_buf_valid(1, 0x276000, SZ_64K, 1)); + KUNIT_EXPECT_TRUE(test, dcp_rtkit_live_buf_valid(8, 0x277000, SZ_4K, 1)); + KUNIT_EXPECT_FALSE(test, dcp_rtkit_live_buf_valid(1, 0x276800, SZ_64K, 1)); + KUNIT_EXPECT_FALSE(test, dcp_rtkit_live_buf_valid(1, 0x276000, 0x800, 1)); + + /* Loader-allocated (is_mapped == 0) must be host page-aligned. */ + KUNIT_EXPECT_TRUE(test, dcp_rtkit_live_buf_valid(2, 0xf10004000ULL, PAGE_SIZE, 0)); + KUNIT_EXPECT_TRUE(test, dcp_rtkit_live_buf_valid(4, 0xf10000000ULL, PAGE_SIZE, 0)); + if (PAGE_SIZE > SZ_4K) { + KUNIT_EXPECT_FALSE(test, dcp_rtkit_live_buf_valid(2, 0xf10001000ULL, PAGE_SIZE, 0)); + KUNIT_EXPECT_FALSE(test, dcp_rtkit_live_buf_valid(2, 0xf10004000ULL, SZ_4K, 0)); + } + + /* Endpoint, flag, size and wraparound bounds. */ + KUNIT_EXPECT_FALSE(test, dcp_rtkit_live_buf_valid(0, 0x276000, SZ_64K, 1)); + KUNIT_EXPECT_FALSE(test, dcp_rtkit_live_buf_valid(3, 0x276000, SZ_64K, 1)); + KUNIT_EXPECT_FALSE(test, dcp_rtkit_live_buf_valid(1, 0x276000, SZ_64K, 2)); + KUNIT_EXPECT_FALSE(test, dcp_rtkit_live_buf_valid(1, 0x276000, 0, 1)); + KUNIT_EXPECT_FALSE(test, dcp_rtkit_live_buf_valid(1, 0x276000, SZ_16M + SZ_4K, 1)); + KUNIT_EXPECT_FALSE(test, dcp_rtkit_live_buf_valid(1, U64_MAX - SZ_4K + 1, SZ_4K, 1)); +} + static struct kunit_case dcp_handoff_cases[] = { KUNIT_CASE(dcp_handoff_external_idle), KUNIT_CASE(dcp_handoff_boot_notch), + KUNIT_CASE(dcp_handoff_live_rtkit_buf_valid), {} };