Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions drivers/gpu/drm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ obj-$(CONFIG_DRM_PANTHOR) += panthor/
obj-$(CONFIG_DRM_TYR) += tyr/
obj-$(CONFIG_DRM_APPLE_FABRIC_CORE) += apple/dcp-fabric-core.o
obj-$(CONFIG_DRM_APPLE_FABRIC_KUNIT_TEST) += apple/dcp-fabric-test.o
obj-$(CONFIG_DRM_APPLE_M4_IOREPORT_KUNIT_TEST) += apple/dcp-m4-ioreport-test.o
obj-$(CONFIG_DRM_APPLE) += apple/
obj-$(CONFIG_DRM_APPLE_KUNIT_TEST) += apple/iomfb-state-test.o
obj-$(CONFIG_DRM_ASPEED_GFX) += aspeed/
Expand Down
10 changes: 10 additions & 0 deletions drivers/gpu/drm/apple/Kconfig.tests
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ config DRM_APPLE_FABRIC_KUNIT_TEST
output priority without DRM devices or Apple display hardware.
The suite covers direct DisplayPort and USB4 tunnel combinations,
immutable tunnel ownership and external display service readiness.

config DRM_APPLE_M4_IOREPORT_KUNIT_TEST
tristate "KUnit tests for the Apple DCP RTKit IOReport buffer parser" if !KUNIT_ALL_TESTS
depends on KUNIT
default KUNIT_ALL_TESTS
help
Test parsing of the firmware IORb shared-memory buffer populated
by Apple DCP firmware on APPLE_RTKIT_EP_IOREPORT, covering frame
counts, scanout and swap subframe histograms, and bounds checks.
No DRM device or Apple display hardware is needed.
140 changes: 140 additions & 0 deletions drivers/gpu/drm/apple/dcp-m4-ioreport-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/*
* KUnit tests for the Apple DCP RTKit IOReport buffer parser
* (dcp-m4-ioreport.h). No hardware.
*/
#include <kunit/test.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/unaligned.h>

#include "dcp-m4-ioreport.h"

static size_t append_simple(u8 *buf, size_t off, u64 ch_id, u64 val)
{
memset(buf + off, 0, DCP_M4_IOREPORT_SIMPLE_SIZE);
put_unaligned_le64(ch_id, buf + off);
put_unaligned_le32(DCP_M4_IOREPORT_TYPE_SIMPLE, buf + off + 0x08);
put_unaligned_le64(val, buf + off + DCP_M4_IOREPORT_ENTRY_HDR_SIZE);
return off + DCP_M4_IOREPORT_SIMPLE_SIZE;
}

static size_t append_state(u8 *buf, size_t off, u64 ch_id, u32 nstates)
{
size_t sz = DCP_M4_IOREPORT_ENTRY_HDR_SIZE +
(size_t)nstates * DCP_M4_IOREPORT_STATE_STRIDE;

memset(buf + off, 0, sz);
put_unaligned_le64(ch_id, buf + off);
put_unaligned_le32(DCP_M4_IOREPORT_TYPE_STATE, buf + off + 0x08);
put_unaligned_le32(nstates, buf + off + 0x48);
return off + sz;
}

static size_t append_array(u8 *buf, size_t off, u64 ch_id, const u64 *vals, u32 n)
{
size_t sz = DCP_M4_IOREPORT_ENTRY_HDR_SIZE +
(size_t)n * DCP_M4_IOREPORT_ARRAY_STRIDE;
u32 i;

memset(buf + off, 0, sz);
put_unaligned_le64(ch_id, buf + off);
put_unaligned_le32(DCP_M4_IOREPORT_TYPE_ARRAY, buf + off + 0x08);
put_unaligned_le32(n, buf + off + 0x48);
for (i = 0; i < n; i++)
put_unaligned_le64(vals[i],
buf + off + DCP_M4_IOREPORT_ENTRY_HDR_SIZE +
(size_t)i * DCP_M4_IOREPORT_ARRAY_STRIDE);
return off + sz;
}

static void dcp_m4_ioreport_parse_valid(struct kunit *test)
{
struct dcp_m4_ioreport_sample s;
u64 scanout[25] = {};
u64 swaps[25] = {};
u8 *buf;
size_t off;

buf = kunit_kzalloc(test, 4096, GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, buf);

scanout[2] = 1200;
scanout[10] = 720;
swaps[2] = 1198;

put_unaligned_le32(DCP_M4_IOREPORT_MAGIC, buf);
put_unaligned_le32(6, buf + 0x04);
put_unaligned_le64(0x123456789abcdef0ULL, buf + 0x08);
off = DCP_M4_IOREPORT_HDR_SIZE;
off = append_simple(buf, off, DCP_M4_CHID_FRAME_COUNT, 1920);
off = append_state(buf, off, 0x504f575253544154ULL, 4);
off = append_simple(buf, off, DCP_M4_CHID_BL_FRAME_COUNT, 1919);
off = append_simple(buf, off, DCP_M4_CHID_DURATION_EXP, 42);
off = append_array(buf, off, DCP_M4_CHID_SCANOUT_SUBFRAMES, scanout, 25);
off = append_array(buf, off, DCP_M4_CHID_SWAP_SUBFRAMES, swaps, 25);

KUNIT_ASSERT_EQ(test, dcp_m4_ioreport_parse(buf, off, &s), 0);
KUNIT_EXPECT_EQ(test, s.timestamp, 0x123456789abcdef0ULL);
KUNIT_EXPECT_EQ(test, s.channels, 6U);
KUNIT_EXPECT_TRUE(test, s.has_frame_count);
KUNIT_EXPECT_EQ(test, s.frame_count, 1920ULL);
KUNIT_EXPECT_TRUE(test, s.has_bl_frame_count);
KUNIT_EXPECT_EQ(test, s.bl_frame_count, 1919ULL);
KUNIT_EXPECT_TRUE(test, s.has_duration_exp);
KUNIT_EXPECT_EQ(test, s.duration_exp, 42ULL);
KUNIT_EXPECT_EQ(test, s.scanout_count, 25U);
KUNIT_EXPECT_EQ(test, s.scanout_subframes[2], 1200ULL);
KUNIT_EXPECT_EQ(test, s.scanout_subframes[10], 720ULL);
KUNIT_EXPECT_EQ(test, s.swap_count, 25U);
KUNIT_EXPECT_EQ(test, s.swap_subframes[2], 1198ULL);
KUNIT_EXPECT_EQ(test, s.swap_subframes[10], 0ULL);
}

static void dcp_m4_ioreport_rejects_malformed(struct kunit *test)
{
struct dcp_m4_ioreport_sample s;
u8 buf[256] = {};
size_t off;

KUNIT_EXPECT_EQ(test, dcp_m4_ioreport_parse(NULL, sizeof(buf), &s), -EINVAL);
KUNIT_EXPECT_EQ(test, dcp_m4_ioreport_parse(buf, 8, &s), -EINVAL);

/* Wrong magic */
put_unaligned_le32(0xdeadbeefU, buf);
KUNIT_EXPECT_EQ(test, dcp_m4_ioreport_parse(buf, sizeof(buf), &s), -EPROTO);

/* Unknown reporter type */
put_unaligned_le32(DCP_M4_IOREPORT_MAGIC, buf);
put_unaligned_le32(1, buf + 0x04);
put_unaligned_le32(99, buf + DCP_M4_IOREPORT_HDR_SIZE + 0x08);
KUNIT_EXPECT_EQ(test, dcp_m4_ioreport_parse(buf, sizeof(buf), &s), -EPROTO);

/* Truncated payload */
off = append_simple(buf, DCP_M4_IOREPORT_HDR_SIZE, DCP_M4_CHID_FRAME_COUNT, 7);
KUNIT_EXPECT_EQ(test, dcp_m4_ioreport_parse(buf, off - 1, &s), -EPROTO);
KUNIT_EXPECT_EQ(test, dcp_m4_ioreport_parse(buf, off, &s), 0);
KUNIT_EXPECT_EQ(test, s.frame_count, 7ULL);

/* Oversized array element count */
put_unaligned_le32(DCP_M4_IOREPORT_TYPE_ARRAY, buf + DCP_M4_IOREPORT_HDR_SIZE + 0x08);
put_unaligned_le32(DCP_M4_IOREPORT_MAX_ELEMS + 1, buf + DCP_M4_IOREPORT_HDR_SIZE + 0x48);
KUNIT_EXPECT_EQ(test, dcp_m4_ioreport_parse(buf, sizeof(buf), &s), -EPROTO);
}

static struct kunit_case dcp_m4_ioreport_cases[] = {
KUNIT_CASE(dcp_m4_ioreport_parse_valid),
KUNIT_CASE(dcp_m4_ioreport_rejects_malformed),
{}
};

static struct kunit_suite dcp_m4_ioreport_suite = {
.name = "apple-dcp-m4-ioreport",
.test_cases = dcp_m4_ioreport_cases,
};

kunit_test_suite(dcp_m4_ioreport_suite);

MODULE_DESCRIPTION("Apple DCP RTKit IOReport buffer parser tests");
MODULE_LICENSE("Dual MIT/GPL");
150 changes: 150 additions & 0 deletions drivers/gpu/drm/apple/dcp-m4-ioreport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
/*
* Parser for the RTKit IOReport shared-memory buffer ("IORb") populated by
* Apple DCP firmware on APPLE_RTKIT_EP_IOREPORT. Pure logic, no hardware:
* unit tested by dcp-m4-ioreport-test.c.
*/
#ifndef _DCP_M4_IOREPORT_H
#define _DCP_M4_IOREPORT_H

#include <linux/errno.h>
#include <linux/minmax.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/unaligned.h>

#define DCP_M4_IOREPORT_MAGIC 0x494f5262U /* "IORb" */
#define DCP_M4_IOREPORT_HDR_SIZE 0x10U
#define DCP_M4_IOREPORT_ENTRY_HDR_SIZE 0x50U

#define DCP_M4_IOREPORT_TYPE_SIMPLE 1U
#define DCP_M4_IOREPORT_TYPE_STATE 2U
#define DCP_M4_IOREPORT_TYPE_ARRAY 3U

#define DCP_M4_IOREPORT_SIMPLE_SIZE 0x58U
#define DCP_M4_IOREPORT_STATE_STRIDE 0x18U
#define DCP_M4_IOREPORT_ARRAY_STRIDE 0x10U
#define DCP_M4_IOREPORT_MAX_ELEMS 1024U
#define DCP_M4_IOREPORT_SUBFRAMES_MAX 25U

#define DCP_M4_CHID_FRAME_COUNT 0x494f4d4642464343ULL /* IOMFBFCC */
#define DCP_M4_CHID_BL_FRAME_COUNT 0x494f4d4642424c46ULL /* IOMFBBLF */
#define DCP_M4_CHID_DURATION_EXP 0x494f4d4642445552ULL /* IOMFBDUR */
#define DCP_M4_CHID_SCANOUT_SUBFRAMES 0x494f4d4642535750ULL /* IOMFBSWP */
#define DCP_M4_CHID_SWAP_SUBFRAMES 0x494f4d4642555352ULL /* IOMFBUSR */

struct dcp_m4_ioreport_sample {
u64 timestamp;
u32 channels;
bool has_frame_count;
u64 frame_count;
bool has_bl_frame_count;
u64 bl_frame_count;
bool has_duration_exp;
u64 duration_exp;
u32 scanout_count;
u64 scanout_subframes[DCP_M4_IOREPORT_SUBFRAMES_MAX];
u32 swap_count;
u64 swap_subframes[DCP_M4_IOREPORT_SUBFRAMES_MAX];
};

static inline int dcp_m4_ioreport_parse(const void *buf, size_t size,
struct dcp_m4_ioreport_sample *out)
{
const u8 *p = buf;
size_t off;
u32 i;

if (!buf || !out || size < DCP_M4_IOREPORT_HDR_SIZE)
return -EINVAL;

memset(out, 0, sizeof(*out));
if (get_unaligned_le32(p) != DCP_M4_IOREPORT_MAGIC)
return -EPROTO;

out->channels = get_unaligned_le32(p + 0x04);
out->timestamp = get_unaligned_le64(p + 0x08);
off = DCP_M4_IOREPORT_HDR_SIZE;

for (i = 0; i < out->channels; i++) {
u64 ch_id;
u32 type, count, j, n;
size_t entry_size;

if (size - off < DCP_M4_IOREPORT_ENTRY_HDR_SIZE)
return -EPROTO;

ch_id = get_unaligned_le64(p + off);
type = get_unaligned_le32(p + off + 0x08);
count = get_unaligned_le32(p + off + 0x48);

switch (type) {
case DCP_M4_IOREPORT_TYPE_SIMPLE:
entry_size = DCP_M4_IOREPORT_SIMPLE_SIZE;
break;
case DCP_M4_IOREPORT_TYPE_STATE:
if (count > DCP_M4_IOREPORT_MAX_ELEMS)
return -EPROTO;
entry_size = DCP_M4_IOREPORT_ENTRY_HDR_SIZE +
(size_t)count * DCP_M4_IOREPORT_STATE_STRIDE;
break;
case DCP_M4_IOREPORT_TYPE_ARRAY:
if (count > DCP_M4_IOREPORT_MAX_ELEMS)
return -EPROTO;
entry_size = DCP_M4_IOREPORT_ENTRY_HDR_SIZE +
(size_t)count * DCP_M4_IOREPORT_ARRAY_STRIDE;
break;
default:
return -EPROTO;
}

if (entry_size > size - off)
return -EPROTO;

if (type == DCP_M4_IOREPORT_TYPE_SIMPLE) {
u64 val = get_unaligned_le64(p + off + DCP_M4_IOREPORT_ENTRY_HDR_SIZE);

switch (ch_id) {
case DCP_M4_CHID_FRAME_COUNT:
out->frame_count = val;
out->has_frame_count = true;
break;
case DCP_M4_CHID_BL_FRAME_COUNT:
out->bl_frame_count = val;
out->has_bl_frame_count = true;
break;
case DCP_M4_CHID_DURATION_EXP:
out->duration_exp = val;
out->has_duration_exp = true;
break;
default:
break;
}
} else if (type == DCP_M4_IOREPORT_TYPE_ARRAY) {
n = min_t(u32, count, DCP_M4_IOREPORT_SUBFRAMES_MAX);
if (ch_id == DCP_M4_CHID_SCANOUT_SUBFRAMES) {
out->scanout_count = n;
for (j = 0; j < n; j++)
out->scanout_subframes[j] =
get_unaligned_le64(p + off +
DCP_M4_IOREPORT_ENTRY_HDR_SIZE +
(size_t)j *
DCP_M4_IOREPORT_ARRAY_STRIDE);
} else if (ch_id == DCP_M4_CHID_SWAP_SUBFRAMES) {
out->swap_count = n;
for (j = 0; j < n; j++)
out->swap_subframes[j] =
get_unaligned_le64(p + off +
DCP_M4_IOREPORT_ENTRY_HDR_SIZE +
(size_t)j *
DCP_M4_IOREPORT_ARRAY_STRIDE);
}
}

off += entry_size;
}

return 0;
}

#endif
60 changes: 60 additions & 0 deletions drivers/gpu/drm/apple/iomfb_m4.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "iomfb_m4.h"

#include "dcp-m4-profile.h"
#include "dcp-m4-ioreport.h"
#include "dcp-m4-protocol.h"
#include "dcp-m4-transaction.h"
#include "dcp-m4-timing.h"
Expand Down Expand Up @@ -6724,7 +6725,66 @@ static int iomfb_m4_status_show(struct seq_file *s, void *unused)
}
DEFINE_SHOW_ATTRIBUTE(iomfb_m4_status);

static int iomfb_m4_ioreport_show(struct seq_file *m, void *unused)
{
struct apple_dcp *owner = m->private;
struct iomfb_m4 *dcp = owner->v26_6;
struct dcp_m4_ioreport_sample sample;
void *buf;
size_t size, len = 0;
unsigned int i;
int ret;

if (!dcp || !dcp->rtkit)
return -ENODEV;

mutex_lock(&dcp->lock);
if (dcp->gated || dcp->display_off || dcp->powered_off ||
READ_ONCE(dcp->error)) {
mutex_unlock(&dcp->lock);
return -EAGAIN;
}

size = apple_rtkit_ioreport_size(dcp->rtkit);
if (size < DCP_M4_IOREPORT_HDR_SIZE || size > SZ_1M) {
mutex_unlock(&dcp->lock);
return -ENODEV;
}

buf = kvmalloc(size, GFP_KERNEL);
if (!buf) {
mutex_unlock(&dcp->lock);
return -ENOMEM;
}

ret = apple_rtkit_ioreport_snapshot(dcp->rtkit, buf, size, &len);
mutex_unlock(&dcp->lock);

if (!ret)
ret = dcp_m4_ioreport_parse(buf, len, &sample);
kvfree(buf);
if (ret)
return ret;

seq_printf(m, "timestamp=%llu\n", sample.timestamp);
seq_printf(m, "channels=%u\n", sample.channels);
seq_printf(m, "frame_count=%llu\n", sample.frame_count);
seq_printf(m, "bl_frame_count=%llu\n", sample.bl_frame_count);
seq_printf(m, "duration_exp=%llu\n", sample.duration_exp);
seq_puts(m, "scanout_subframes=");
for (i = 0; i < sample.scanout_count; i++)
seq_printf(m, "%s%llu", i ? "," : "", sample.scanout_subframes[i]);
seq_putc(m, '\n');
seq_puts(m, "swap_subframes=");
for (i = 0; i < sample.swap_count; i++)
seq_printf(m, "%s%llu", i ? "," : "", sample.swap_subframes[i]);
seq_putc(m, '\n');
return 0;
}
DEFINE_SHOW_ATTRIBUTE(iomfb_m4_ioreport);

void iomfb_m4_debugfs_init(struct apple_dcp *owner, struct dentry *root)
{
debugfs_create_file("iomfb_m4", 0400, root, owner, &iomfb_m4_status_fops);
debugfs_create_file("dcp_ioreport", 0444, root, owner, &iomfb_m4_ioreport_fops);
}
Loading
Loading