Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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.

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
17 changes: 17 additions & 0 deletions drivers/accel/ane/ane_h16_hwx.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
#define ANE_H16_REGIONS 7
#define ANE_H16_HWX_MAX (SZ_256M + SZ_1M)
/* Mach-O CPU subtypes of the targets that share this container form. */
#define ANE_HWX_SUBTYPE_H14 5
#define ANE_HWX_SUBTYPE_H15 6
#define ANE_HWX_SUBTYPE_H16 7
#define ANE_HWX_SUBTYPE_H17 9
/* BAR slots a task record can name (bits 28:23 of a dense record). */
#define ANE_HWX_BAR_SLOTS 64

struct ane_h16_hwx_section {
u64 vm;
Expand Down Expand Up @@ -56,6 +59,12 @@ struct ane_h16_hwx {
u32 subtype;
/* Fixed words per task of the target's task stream (ane_hx_task_frame()). */
u32 task_header_words;
/*
* The program descriptor's BAR slot table names the constants, the
* scratch and every surface (H14): with no __KERN_0, the kernel
* resource binds the __TEXT/__const slot.
*/
bool slot_table;
struct ane_h16_hwx_surface surfaces[ANE_H16_SURFACES];
u32 surface_count;
u32 tasks;
Expand All @@ -67,6 +76,14 @@ struct ane_h16_hwx {
/* Input must be one immutable kernel-owned snapshot of the original file. */
int ane_h16_hwx_validate(const void *bytes, size_t size, struct ane_h16_hwx *info);

/*
* The HWX address BAR slot @slot of a validated program takes its base from:
* the program descriptor qword at +0x10 * (@slot + 1), 0 for a slot that
* names no region. @descriptor is the program descriptor command in the
* same snapshot.
*/
u64 ane_h16_hwx_slot_base(const struct ane_h16_hwx *info, const void *descriptor, u32 slot);

struct ane_h16_hwx_snapshot {
void *bytes;
size_t size;
Expand Down
Loading