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
500 changes: 500 additions & 0 deletions drivers/accel/ane/ane_h14_add_fixture.h

Large diffs are not rendered by default.

642 changes: 642 additions & 0 deletions drivers/accel/ane/ane_h14_chain_fixture.h

Large diffs are not rendered by default.

476 changes: 476 additions & 0 deletions drivers/accel/ane/ane_h14_streams_fixture.h

Large diffs are not rendered by default.

101 changes: 101 additions & 0 deletions drivers/accel/ane/ane_h16_accel_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,106 @@ static void ane_h16_test_init_surfaces(struct kunit *test, struct ane_h16_load_n
n->f.program = &n->p;
}

static const struct ane_fw_platform ane_h16_test_soc_h14 = {
.chip_family = DRM_ANE_CHIP_H14,
};

/*
* The request as the engine is about to get it: busy(true) runs after the
* pre-device sync, right before the EXECUTE. Snapshot the input tail there,
* then end the request (the program reads as not created) so this engine-less
* session never builds one; busy(false) undoes that.
*/
static struct {
struct ane_h16_program *p;
const u8 *tail;
u8 seen[128];
unsigned int submits;
} ane_h16_test_submit;

static void ane_h16_test_submit_busy(void *ctx, bool busy)
{
struct ane_h16_program *p = ane_h16_test_submit.p;

if (busy) {
memcpy(ane_h16_test_submit.seen, ane_h16_test_submit.tail,
sizeof(ane_h16_test_submit.seen));
ane_h16_test_submit.submits++;
}
p->created = !busy;
}

static u64 ane_h16_test_now(void *ctx)
{
return ktime_get_ns();
}

static const struct ane_fw_transport_ops ane_h16_test_submit_transport = {
.now_ns = ane_h16_test_now,
};

static const struct ane_h16_session_ops ane_h16_test_submit_ops = {
.map = ane_h16_test_dummy_map,
.log = ane_h16_test_dummy_log,
.busy = ane_h16_test_submit_busy,
};

/*
* H14 sends each input at its declared size, past the span a copy fills, and
* an in-place input may be written through its last page. The in-place and
* fenced entry points zero that tail on their pre-device sync like the copy
* path, and the request carries the zeroed tail when it is submitted; H16
* sends the section and leaves the bytes alone.
*/
static void ane_h16_test_dirty_tail(struct ane_h16_load_node *n, void *in_cpu,
const struct ane_fw_platform *soc)
{
n->s.transport->soc = soc;
n->p.declared[0] = 3 * PAGE_SIZE;
n->p.declared[1] = 2 * PAGE_SIZE;
memset(in_cpu, 0xa5, 3 * PAGE_SIZE);
}

static void ane_h16_exec_pad_tail_test(struct kunit *test)
{
static const struct ane_fw_platform *socs[] = {
&ane_h16_test_soc_h14, &ane_h16_test_soc,
};
size_t span = 3 * PAGE_SIZE - 128;
unsigned int i;

for (i = 0; i < ARRAY_SIZE(socs); i++) {
struct ane_h16_load_node *n = ane_h16_load_node(test);
bool h14 = socs[i] == &ane_h16_test_soc_h14;
void *in_cpu, *out_cpu;
u8 *tail;

KUNIT_ASSERT_NOT_NULL(test, n);
ane_h16_test_init_surfaces(test, n, &in_cpu, &out_cpu);
tail = (u8 *)in_cpu + span;

n->s.ops = &ane_h16_test_submit_ops;
n->s.transport->ops = &ane_h16_test_submit_transport;
ane_h16_test_submit.p = &n->p;
ane_h16_test_submit.tail = tail;
ane_h16_test_submit.submits = 0;

ane_h16_test_dirty_tail(n, in_cpu, socs[i]);
KUNIT_EXPECT_EQ(test, ane_h16_exec_in_place(&n->a, &n->f, 2, false), -EINVAL);
KUNIT_EXPECT_EQ(test, ane_h16_test_submit.submits, 1U);
KUNIT_EXPECT_FALSE(test, memchr_inv(ane_h16_test_submit.seen, h14 ? 0 : 0xa5, 128));
KUNIT_EXPECT_FALSE(test, memchr_inv(in_cpu, 0xa5, span));

ane_h16_test_dirty_tail(n, in_cpu, socs[i]);
KUNIT_EXPECT_EQ(test, ane_h16_exec_sync_run(&n->a, &n->p), -EINVAL);
KUNIT_EXPECT_EQ(test, ane_h16_test_submit.submits, 2U);
KUNIT_EXPECT_FALSE(test, memchr_inv(ane_h16_test_submit.seen, h14 ? 0 : 0xa5, 128));
KUNIT_EXPECT_FALSE(test, memchr_inv(in_cpu, 0xa5, span));
KUNIT_EXPECT_EQ(test, n->s.active, 0U);
KUNIT_EXPECT_TRUE(test, n->p.created);
}
}

static void ane_h16_dmabuf_uapi_test(struct kunit *test)
{
struct ane_h16_load_node *n = ane_h16_load_node(test);
Expand Down Expand Up @@ -1211,6 +1311,7 @@ static struct kunit_case ane_h16_accel_cases[] = {
KUNIT_CASE(ane_h16_dmabuf_sgt_lifetime_test),
KUNIT_CASE(ane_h16_exec_sync_uapi_test),
KUNIT_CASE(ane_h16_exec_sync_fence_test),
KUNIT_CASE(ane_h16_exec_pad_tail_test),
KUNIT_CASE(ane_h16_exec_sync_parked_nonblock_test),
KUNIT_CASE(ane_h16_enter_wakes_unlocked_test),
KUNIT_CASE(ane_h16_in_fence_nonblock_test),
Expand Down
186 changes: 181 additions & 5 deletions drivers/accel/ane/ane_h16_hwx.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,52 @@ struct hwx_range {
u64 start, size;
};

/*
* What differs between the targets of this container form. H14 objects
* (@slot_table) carry a 0x890-byte program descriptor whose BAR slot table
* names the constants, the scratch and every surface
* (hwx_program_slot_table()), open their task stream with 0, keep their
* constants in __TEXT/__const (@no_kernel_segment), leave the __TEXT flags
* zero beside a __DATA scratch segment (@zero_scratch_text_flags) and write
* the H14 register image.
*/
struct hwx_target {
u32 subtype;
u32 linear, mixed;
unsigned int header_words;
u32 trailer_bar;
bool (*register_valid)(u32 word);
bool has_perf_record;
bool allow_zero_prologue;
bool require_tight_end;
bool allow_stabs_note;
bool allow_slot5_table;
bool allow_zero_scratch_text_flags;
bool zero_scratch_text_flags;
bool no_kernel_segment;
bool slot_table;
};

static const struct hwx_target *hwx_target(u32 subtype)
{
static const struct hwx_target targets[] = {
{
.subtype = ANE_HWX_SUBTYPE_H14,
.linear = 0x890,
.header_words = 8,
.trailer_bar = 3,
.register_valid = ane_h14_register_valid,
.zero_scratch_text_flags = true,
.no_kernel_segment = true,
.slot_table = true,
},
{
.subtype = ANE_HWX_SUBTYPE_H15,
.linear = 0x890,
.mixed = 0x8b0,
.header_words = 8,
.trailer_bar = 5,
.register_valid = ane_h16_register_valid,
.allow_zero_prologue = true,
},
{
Expand All @@ -46,6 +70,7 @@ static const struct hwx_target *hwx_target(u32 subtype)
.mixed = 0x8c0,
.header_words = 9,
.trailer_bar = 4,
.register_valid = ane_h16_register_valid,
.has_perf_record = true,
.require_tight_end = true,
},
Expand All @@ -55,6 +80,7 @@ static const struct hwx_target *hwx_target(u32 subtype)
.mixed = 0x8c0,
.header_words = 9,
.trailer_bar = 4,
.register_valid = ane_h16_register_valid,
.has_perf_record = true,
.allow_zero_prologue = true,
.allow_stabs_note = true,
Expand Down Expand Up @@ -170,16 +196,21 @@ static int hwx_segment(struct hwx_parser *p, const u8 *c, u32 length)
/*
* __TEXT is SG_NORELOC (4) beside a scratch segment in the MobileNet program; Apple's
* zin_ane_compiler 10.205 marks it 0 there too on H17 (relocations are checked below).
* H14 objects leave it 0 beside the scratch.
*/
flags = get_unaligned_le32(c + 68);
if (text && p->target->allow_zero_scratch_text_flags && p->info.scratch.size) {
if (flags != 4 && flags)
return -ENOEXEC;
} else if (text && p->target->zero_scratch_text_flags) {
if (flags)
return -ENOEXEC;
} else if (flags != (surface ? 6 : text ? (p->info.scratch.size ? 4 : 0) : 4)) {
return -ENOEXEC;
}
if ((text && (prot != 5 || nsects != 2 || p->info.text.size)) ||
(kernel && (prot != 1 || nsects != 1 || p->info.kernel.size)) ||
(kernel && (p->target->no_kernel_segment || prot != 1 || nsects != 1 ||
p->info.kernel.size)) ||
(scratch && (prot != 3 || p->info.scratch.size)) ||
(surface && (prot < 1 || prot > 2 ||
p->info.surface_count == ANE_H16_SURFACES)))
Expand Down Expand Up @@ -269,6 +300,149 @@ static int hwx_segment(struct hwx_parser *p, const u8 *c, u32 length)
return 0;
}

u64 ane_h16_hwx_slot_base(const struct ane_h16_hwx *info, const void *descriptor, u32 slot)
{
if (!info || !descriptor || slot >= ANE_HWX_BAR_SLOTS)
return 0;
return get_unaligned_le64((const u8 *)descriptor + 0x10 * (slot + 1));
}
EXPORT_SYMBOL_GPL(ane_h16_hwx_slot_base);

/*
* The BAR slot table of the H14 descriptor: slot s takes its base from the
* qword at +0x10 * (s + 1), as on H16. Slot 0 is the text, slot 1 the
* __TEXT/__const coefficients, slot 2 is empty, slot 3 the __DATA scratch
* (linked programs read and write it, 0 without one), and from slot 4 come
* the first input, the output, then the other inputs, in segment order.
*/
static u64 hwx_slot_table_base(const struct ane_h16_hwx *info, u32 slot)
{
u32 i, inputs = 0, want;

if (slot == 0)
return info->text.vm;
if (slot == 1)
return info->constants.vm;
if (slot == 3)
return info->scratch.size ? info->scratch.vm : 0;
if (slot < 4 || slot >= 4 + info->surface_count)
return 0;
if (slot == 5) {
for (i = 0; i < info->surface_count; i++)
if (info->surfaces[i].direction == 2)
return info->surfaces[i].section.vm;
return 0;
}
want = slot == 4 ? 0 : slot - 5;
for (i = 0; i < info->surface_count; i++) {
if (info->surfaces[i].direction != 1)
continue;
if (inputs++ == want)
return info->surfaces[i].section.vm;
}
return 0;
}

/*
* Every relocated record of a slot-table stream must name a slot that binds
* a region: the coefficients, the scratch or a surface, never the text
* (slot 0) or an empty slot.
*/
static int hwx_slot_table_records(const struct hwx_parser *p)
{
const u8 *text = p->bytes + p->info.text.offset;
const u8 *c = p->bytes + p->info.program_offset;
struct ane_hx_task t;
size_t pos = 16, rec;
u32 h, slot;
int ret;

while (pos < p->info.text.size) {
ret = ane_hx_task_frame(text, p->info.text.size, pos,
p->info.task_header_words, &t);
if (ret < 0)
return ret;
pos = t.next;
if (!ret)
continue;
for (rec = t.rec; rec < t.end; rec += 4 + ane_hx_record_count(h) * 4) {
h = get_unaligned_le32(text + rec);
slot = (h >> 23) & 0x3f;
if (!(h & BIT(31)) && (h & BIT(29)) &&
(!slot || !ane_h16_hwx_slot_base(&p->info, c, slot)))
return -ENOEXEC;
}
}
return 0;
}

/*
* The slot-table (H14) program descriptor, one length (@linear): the text
* base, the BAR slot table (hwx_slot_table_base(), every qword pinned), the
* task stream extent, two compiler hints (+0x858 and the work hint at
* +0x880) bounded like the H16 work hint, and the fixed trailer: one record
* naming BAR @trailer_bar, then the function name "main". Every other byte
* is zero. The stream opens with 0. No custom BAR record comes from this
* form (ane_h16_custom_bars()).
*/
static int hwx_program_slot_table(struct hwx_parser *p)
{
static const u16 cells[] = {
0, 4, 8, 0xc, 0x810, 0x814, 0x820, 0x824, 0x830, 0x838, 0x83c,
0x844, 0x848, 0x858, 0x860, 0x880,
};
const struct hwx_target *t = p->target;
const u8 *c = p->bytes + p->info.program_offset;
struct ane_h16_hwx *info = &p->info;
u32 length = get_unaligned_le32(c + 4), records, record_limit, i;
u64 work_limit = 0;
u8 *expected;
int ret = -ENOEXEC;

/* The word at +0xc is 0x222, as in the H16 0x8a0 form, not length / 4 - 6. */
if (length != t->linear || get_unaligned_le32(c + 0xc) != 0x222 ||
get_unaligned_le64(c + 0x810) != info->text.vm ||
get_unaligned_le32(c + 0x820) != 4 ||
(u64)get_unaligned_le32(c + 0x824) * 4 != info->text.size ||
get_unaligned_le32(c + 0x838) != U32_MAX ||
get_unaligned_le32(c + 0x83c) != U32_MAX ||
get_unaligned_le32(c + 0x844) != 0xffff ||
get_unaligned_le32(c + 0x848) != length - 8)
return -ENOEXEC;
for (i = 0; i < info->surface_count; i++)
work_limit += info->surfaces[i].allocation;
if (get_unaligned_le32(c + 0x858) > work_limit ||
get_unaligned_le32(c + length - 16) > work_limit)
return -ENOEXEC;
records = get_unaligned_le32(c + 0x860);
info->tasks = get_unaligned_le32(c + 0x830);

expected = kzalloc(length, GFP_KERNEL);
if (!expected)
return -ENOMEM;
for (i = 0; i < ARRAY_SIZE(cells); i++)
memcpy(expected + cells[i], c + cells[i], 4);
for (i = 0; i < 4 + info->surface_count; i++)
put_unaligned_le64(hwx_slot_table_base(info, i), expected + 0x10 * (i + 1));
put_unaligned_le32(t->trailer_bar, expected + 0x868);
put_unaligned_le32(1, expected + 0x870);
put_unaligned_le32(9, expected + 0x878);
put_unaligned_le32(8, expected + 0x87c);
memcpy(expected + length - 8, "main", 5);
if (!memcmp(c, expected, length)) {
ret = ane_hx_task_stream_check(p->bytes + info->text.offset, info->text.size,
info->tasks, 0, info->task_header_words,
t->register_valid, &record_limit, NULL);
/* This compiler hint need not equal the encoded record count. */
if (!ret && (!records || records > record_limit))
ret = -ENOEXEC;
if (!ret)
ret = hwx_slot_table_records(p);
}
kfree(expected);
return ret;
}

/*
* The fp32 trailer word at +0x880 (+0x8a0 with scratch) is 10000.0f in HWX
* from ANECompiler 10.205 and mil-hwx-compiler, and 1000.0f in H16 and H17
Expand All @@ -293,6 +467,8 @@ static int hwx_program(struct hwx_parser *p)
u64 work_limit = 0;
bool mixed = length == t->mixed;

if (t->slot_table)
return hwx_program_slot_table(p);
if (t->allow_zero_prologue && info->text.size >= 4 &&
!get_unaligned_le32(p->bytes + info->text.offset))
prologue = 0;
Expand Down Expand Up @@ -390,10 +566,9 @@ static int hwx_program(struct hwx_parser *p)
off += 16;
memcpy(expected + off, "main", 5);
if (!memcmp(c, expected, length)) {
ret = ane_hx_task_stream_validate_hdr(p->bytes + info->text.offset,
info->text.size, info->tasks, prologue,
info->task_header_words, &record_limit,
&final_end);
ret = ane_hx_task_stream_check(p->bytes + info->text.offset, info->text.size,
info->tasks, prologue, info->task_header_words,
t->register_valid, &record_limit, &final_end);
/* This compiler hint need not equal the encoded record count. */
if (!ret && (!records || records > record_limit))
ret = -ENOEXEC;
Expand Down Expand Up @@ -653,6 +828,7 @@ int ane_h16_hwx_validate(const void *bytes, size_t size, struct ane_h16_hwx *inf
p->command_end = 32 + (size_t)length;
p->info.subtype = target->subtype;
p->info.task_header_words = target->header_words;
p->info.slot_table = target->slot_table;
for (i = 0; i < count; i++) {
u32 cmd, len;

Expand Down
Loading
Loading