diff --git a/drivers/accel/ane/ane_h16_accel.c b/drivers/accel/ane/ane_h16_accel.c index 8ae375af68ed7..9a2d2526d2944 100644 --- a/drivers/accel/ane/ane_h16_accel.c +++ b/drivers/accel/ane/ane_h16_accel.c @@ -7,6 +7,7 @@ * commands. A process may map its program's surfaces (inputs and outputs * only) and run requests on them in place, without copies. */ +#include #include #include #include @@ -157,12 +158,180 @@ static int ane_h16_load_ioctl(struct drm_device *drm, void *data, struct drm_fil return ret; } +/* + * The user memory of one EXECUTE buffer, taken before any driver lock: its + * pages pinned, or, where they cannot be pinned (VM_PFNMAP and VM_IO + * mappings, such as a GEM-shmem or dma-buf heap mmap), a kernel bounce copy. + * Faulting user memory in can wait for as long as whoever backs it likes, and + * mmap() of the node takes the file and node locks under the process mmap + * lock, so no lock of ours is held then. Under the node lock the request + * copies through the kernel mapping of the pinned pages or the bounce copy, + * neither of which faults; a bounced output goes back to user memory after + * both locks are dropped. + */ +struct ane_h16_user_buffer { + struct page **pages; + void *bounce; + u64 ptr; + unsigned int count; + size_t offset; + size_t size; + bool write, dirty; +}; + +static int ane_h16_user_pin(struct ane_h16_user_buffer *u, u64 ptr, size_t size, bool write) +{ + int ret; + + *u = (struct ane_h16_user_buffer) { + .offset = offset_in_page(ptr), + .size = size, + .write = write, + }; + /* At most INT_MAX pages, checked before the page count can wrap. */ + if (!size || ptr + size < ptr || size > (size_t)INT_MAX * PAGE_SIZE - u->offset) + return -EFAULT; + u->count = DIV_ROUND_UP(u->offset + size, PAGE_SIZE); + u->pages = kvmalloc_array(u->count, sizeof(*u->pages), GFP_KERNEL); + if (!u->pages) + return -ENOMEM; + ret = pin_user_pages_fast(ptr & PAGE_MASK, u->count, write ? FOLL_WRITE : 0, u->pages); + if (ret == u->count) + return 0; + if (ret > 0) + unpin_user_pages(u->pages, ret); + kvfree(u->pages); + u->pages = NULL; + return ret < 0 ? ret : -EFAULT; +} + +static void ane_h16_user_unpin(struct ane_h16_user_buffer *u) +{ + if (!u->pages) + return; + unpin_user_pages_dirty_lock(u->pages, u->count, u->dirty); + kvfree(u->pages); + u->pages = NULL; +} + +/* + * Whether [ptr, ptr + size) is completely mapped with the requested access + * and reaches a VM_PFNMAP or VM_IO mapping, whose pages cannot be pinned. + */ +static bool ane_h16_user_unpinnable(u64 ptr, size_t size, bool write) +{ + struct mm_struct *mm = current->mm; + struct vm_area_struct *vma; + unsigned long need = write ? VM_WRITE : VM_READ; + u64 addr = ptr, end = ptr + size; + bool unpinnable = false; + + if (!size || end < ptr) + return false; + mmap_read_lock(mm); + while (addr < end) { + vma = find_vma(mm, addr); + if (!vma || vma->vm_start > addr || !(vma->vm_flags & need)) { + unpinnable = false; + break; + } + if (vma->vm_flags & (VM_PFNMAP | VM_IO)) + unpinnable = true; + addr = vma->vm_end; + } + mmap_read_unlock(mm); + return unpinnable; +} + +/* A kernel copy of the buffer; an input is read into it now. */ +static int ane_h16_user_bounce(struct ane_h16_user_buffer *u, u64 ptr, size_t size, bool write) +{ + *u = (struct ane_h16_user_buffer) { .size = size, .write = write }; + u->bounce = kvmalloc(size, GFP_KERNEL_ACCOUNT); + if (!u->bounce) + return -ENOMEM; + u->ptr = ptr; + if (!write && copy_from_user(u->bounce, u64_to_user_ptr(ptr), size)) { + kvfree(u->bounce); + u->bounce = NULL; + return -EFAULT; + } + return 0; +} + +/* + * Take one buffer with no lock held: pin it, or bounce it when it lies in a + * mapping whose pages cannot be pinned. An input is read into the bounce copy + * now, and fails here as copy_from_user() would; an output is written back by + * ane_h16_user_put(). Unmapped memory and read-only outputs still fail here. + */ +static int ane_h16_user_get(struct ane_h16_user_buffer *u, u64 ptr, size_t size, bool write) +{ + int ret = ane_h16_user_pin(u, ptr, size, write); + + if (ret != -EFAULT || !ane_h16_user_unpinnable(ptr, size, write)) + return ret; + return ane_h16_user_bounce(u, ptr, size, write); +} + +/* Release one buffer after both locks are dropped; a bounced output goes back now. */ +static int ane_h16_user_put(struct ane_h16_user_buffer *u) +{ + int ret = 0; + + if (!u->bounce) { + ane_h16_user_unpin(u); + return 0; + } + if (u->dirty && copy_to_user(u64_to_user_ptr(u->ptr), u->bounce, u->size)) + ret = -EFAULT; + kvfree(u->bounce); + u->bounce = NULL; + return ret; +} + +/* + * Copy between cpu and the pinned pages or the bounce copy. It cannot fault: + * safe under the node lock. + */ +static void ane_h16_user_copy(struct ane_h16_user_buffer *u, void *cpu, bool to_user) +{ + size_t done = 0, offset = u->offset; + unsigned int i; + + if (u->bounce) { + if (to_user) + memcpy(u->bounce, cpu, u->size); + else + memcpy(cpu, u->bounce, u->size); + u->dirty |= to_user; + return; + } + for (i = 0; i < u->count; i++) { + size_t n = min_t(size_t, PAGE_SIZE - offset, u->size - done); + u8 *page = kmap_local_page(u->pages[i]); + + if (to_user) { + memcpy(page + offset, (u8 *)cpu + done, n); + flush_dcache_page(u->pages[i]); + } else { + memcpy((u8 *)cpu + done, page + offset, n); + } + kunmap_local(page); + done += n; + offset = 0; + } + if (to_user) + u->dirty = true; +} + static int ane_h16_exec_ioctl(struct drm_device *drm, void *data, struct drm_file *file) { struct ane_h16_accel *a = to_h16_accel(drm); struct ane_h16_accel_file *f = file->driver_priv; struct drm_ane_h16_exec *args = data; struct drm_ane_h16_buffer buffers[ANE_H16_SURFACES]; + struct ane_h16_user_buffer user[ANE_H16_SURFACES] = {}; struct ane_h16_program *p; u64 start, locked, copied, done; unsigned int i; @@ -177,22 +346,48 @@ static int ane_h16_exec_ioctl(struct drm_device *drm, void *data, struct drm_fil array_size(args->count, sizeof(*buffers)))) return -EFAULT; start = ktime_get_ns(); + /* + * Check the buffers against the loaded program, then take them (pin or + * bounce) with no lock held: user memory is not touched under a lock of + * ours. The program can change while unlocked, so the taken buffers are + * checked against it again under the file lock, which then pins the + * program through the request. + */ mutex_lock(&f->lock); - ret = ane_h16_session_enter(a->session, file->filp->f_flags & O_NONBLOCK); - if (ret) { - mutex_unlock(&f->lock); - return ret; - } p = f->program; - if (!p || p->closed || args->count != p->resource_count) { + if (!p || args->count != p->resource_count) ret = -EINVAL; - goto unlock; + for (i = 0; i < args->count && !in_place && !ret; i++) { + if (buffers[i].size != p->resources[i].buffer.size) + ret = -EINVAL; + user[i].write = p->resources[i].permissions == 2; } + mutex_unlock(&f->lock); + if (ret) + return ret; for (i = 0; i < args->count && !in_place; i++) { - if (buffers[i].size != p->resources[i].buffer.size) { + ret = ane_h16_user_get(&user[i], buffers[i].ptr, buffers[i].size, + user[i].write); + if (ret) + goto unpin; + } + mutex_lock(&f->lock); + p = f->program; + if (!p || args->count != p->resource_count) + ret = -EINVAL; + for (i = 0; i < args->count && !in_place && !ret; i++) { + bool write = p->resources[i].permissions == 2; + + if (buffers[i].size != p->resources[i].buffer.size || user[i].write != write) ret = -EINVAL; - goto unlock; - } + } + if (!ret) + ret = ane_h16_session_enter(a->session, file->filp->f_flags & O_NONBLOCK); + if (ret) + goto unlock_file; + if (p->closed) { + ret = -EINVAL; + goto unlock; } ret = ane_h16_session_acquire(a->session, p); if (ret) @@ -202,14 +397,10 @@ static int ane_h16_exec_ioctl(struct drm_device *drm, void *data, struct drm_fil for (i = 0; i < args->count && !in_place; i++) { void *cpu = p->surfaces[i]->cpu; - if (p->resources[i].permissions == 1) { - if (copy_from_user(cpu, u64_to_user_ptr(buffers[i].ptr), buffers[i].size)) { - ret = -EFAULT; - goto release; - } - } else { + if (p->resources[i].permissions == 1) + ane_h16_user_copy(&user[i], cpu, false); + else memset(cpu, 0xff, buffers[i].size); - } } ane_h16_session_sync(a->session, p, true); copied = ktime_get_ns(); @@ -219,10 +410,8 @@ static int ane_h16_exec_ioctl(struct drm_device *drm, void *data, struct drm_fil done = ktime_get_ns(); ane_h16_session_sync(a->session, p, false); for (i = 0; i < args->count && !in_place; i++) - if (p->resources[i].permissions == 2 && - copy_to_user(u64_to_user_ptr(buffers[i].ptr), p->surfaces[i]->cpu, - buffers[i].size)) - ret = -EFAULT; + if (p->resources[i].permissions == 2) + ane_h16_user_copy(&user[i], p->surfaces[i]->cpu, true); if (!ret) { u64 end = ktime_get_ns(); const u64 *ns = a->session->timing.ns; @@ -248,7 +437,15 @@ static int ane_h16_exec_ioctl(struct drm_device *drm, void *data, struct drm_fil ane_h16_session_release(a->session, p); unlock: mutex_unlock(&a->lock); +unlock_file: mutex_unlock(&f->lock); +unpin: + for (i = 0; i < args->count && !in_place; i++) { + int put = ane_h16_user_put(&user[i]); + + if (put && !ret) + ret = put; + } return ret; } diff --git a/drivers/accel/ane/ane_h16_accel_test.c b/drivers/accel/ane/ane_h16_accel_test.c index b88a8d70336e5..7e6bd043a315b 100644 --- a/drivers/accel/ane/ane_h16_accel_test.c +++ b/drivers/accel/ane/ane_h16_accel_test.c @@ -5,6 +5,7 @@ */ #include #include +#include #include #include @@ -60,9 +61,185 @@ static void ane_h16_accel_open_test(struct kunit *test) drm_dev_unregister(&a->drm); } +/* + * EXECUTE pins a plain buffer's pages before it takes the node lock and copies + * through them under it. The copies equal what copy_from_user() and + * copy_to_user() move, at an offset that crosses pages; an unmapped buffer, + * a read-only output and a wrapping range fail at the pin, holding nothing. + */ +static void ane_h16_accel_user_pages_test(struct kunit *test) +{ + const size_t size = 2 * PAGE_SIZE, off = PAGE_SIZE - 24; + struct ane_h16_user_buffer u; + unsigned long user, ro; + u8 *src, *dst; + size_t i; + + user = kunit_vm_mmap(test, NULL, 0, 3 * PAGE_SIZE, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, 0); + ro = kunit_vm_mmap(test, NULL, 0, PAGE_SIZE, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, 0); + KUNIT_ASSERT_TRUE(test, user && !IS_ERR_VALUE(user)); + KUNIT_ASSERT_TRUE(test, ro && !IS_ERR_VALUE(ro)); + src = kunit_kzalloc(test, size, GFP_KERNEL); + dst = kunit_kzalloc(test, size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, src); + KUNIT_ASSERT_NOT_NULL(test, dst); + for (i = 0; i < size; i++) + src[i] = i * 7 + 3; + KUNIT_ASSERT_EQ(test, copy_to_user((void __user *)(user + off), src, size), 0); + + /* An input: three pages for a buffer that starts 24 bytes before a page end. */ + KUNIT_ASSERT_EQ(test, ane_h16_user_pin(&u, user + off, size, false), 0); + KUNIT_EXPECT_EQ(test, u.count, 3U); + ane_h16_user_copy(&u, dst, false); + KUNIT_EXPECT_MEMEQ(test, dst, src, size); + KUNIT_EXPECT_FALSE(test, u.dirty); + ane_h16_user_unpin(&u); + KUNIT_EXPECT_NULL(test, u.pages); + + /* An output: written through the pinned pages, read back from user memory. */ + for (i = 0; i < size; i++) + src[i] ^= 0x5a; + KUNIT_ASSERT_EQ(test, ane_h16_user_pin(&u, user + off, size, true), 0); + ane_h16_user_copy(&u, src, true); + KUNIT_EXPECT_TRUE(test, u.dirty); + ane_h16_user_unpin(&u); + KUNIT_ASSERT_EQ(test, copy_from_user(dst, (void __user *)(user + off), size), 0); + KUNIT_EXPECT_MEMEQ(test, dst, src, size); + + KUNIT_EXPECT_EQ(test, ane_h16_user_pin(&u, 0, 64, false), -EFAULT); + KUNIT_EXPECT_NULL(test, u.pages); + KUNIT_EXPECT_EQ(test, ane_h16_user_pin(&u, ro, 64, true), -EFAULT); + KUNIT_EXPECT_NULL(test, u.pages); + KUNIT_EXPECT_EQ(test, ane_h16_user_pin(&u, ro, 64, false), 0); + ane_h16_user_unpin(&u); + KUNIT_EXPECT_EQ(test, ane_h16_user_pin(&u, U64_MAX - 8, 64, false), -EFAULT); + KUNIT_EXPECT_NULL(test, u.pages); + /* A size whose page count would wrap to 0 without its own check. */ + KUNIT_EXPECT_EQ(test, ane_h16_user_pin(&u, PAGE_SIZE - 1, + SIZE_MAX - (PAGE_SIZE - 1), false), -EFAULT); + KUNIT_EXPECT_NULL(test, u.pages); + KUNIT_EXPECT_EQ(test, ane_h16_user_pin(&u, 0, (size_t)INT_MAX * PAGE_SIZE + 1, false), + -EFAULT); + KUNIT_EXPECT_NULL(test, u.pages); +} + +/* Set or clear VM_IO on the mapping at @addr, which pin_user_pages_fast() then refuses. */ +static void ane_h16_accel_test_vm_io(unsigned long addr, bool on) +{ + struct mm_struct *mm = current->mm; + struct vm_area_struct *vma; + + mmap_write_lock(mm); + vma = find_vma(mm, addr); + if (vma && vma->vm_start <= addr) { + if (on) + vm_flags_set(vma, VM_IO); + else + vm_flags_clear(vma, VM_IO); + } + mmap_write_unlock(mm); +} + +/* + * A buffer in a mapping whose pages cannot be pinned (VM_PFNMAP or VM_IO: a + * GEM-shmem or dma-buf heap mmap) is bounced: an input is read when it is + * taken, with no lock held, an output is written back when it is put, after + * the locks drop, and only once the request copied into it. A VM_IO flag set + * on anonymous memory is seen by the range check but not by the lockless + * pin, so the bounce itself is called directly here. + */ +static void ane_h16_accel_user_bounce_test(struct kunit *test) +{ + const size_t size = 2 * PAGE_SIZE, off = PAGE_SIZE - 24; + struct ane_h16_user_buffer u; + unsigned long user, ro; + u8 *src, *dst; + size_t i; + + user = kunit_vm_mmap(test, NULL, 0, 3 * PAGE_SIZE, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_POPULATE, 0); + ro = kunit_vm_mmap(test, NULL, 0, PAGE_SIZE, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, 0); + KUNIT_ASSERT_TRUE(test, user && !IS_ERR_VALUE(user)); + KUNIT_ASSERT_TRUE(test, ro && !IS_ERR_VALUE(ro)); + src = kunit_kzalloc(test, size, GFP_KERNEL); + dst = kunit_kzalloc(test, size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, src); + KUNIT_ASSERT_NOT_NULL(test, dst); + for (i = 0; i < size; i++) + src[i] = i * 5 + 1; + KUNIT_ASSERT_EQ(test, copy_to_user((void __user *)(user + off), src, size), 0); + + /* The range check finds a VM_IO mapping anywhere in the range and checks access. */ + KUNIT_EXPECT_FALSE(test, ane_h16_user_unpinnable(user + off, size, false)); + KUNIT_EXPECT_FALSE(test, ane_h16_user_unpinnable(user + off, size, true)); + ane_h16_accel_test_vm_io(user, true); + KUNIT_EXPECT_TRUE(test, ane_h16_user_unpinnable(user + off, size, false)); + KUNIT_EXPECT_TRUE(test, ane_h16_user_unpinnable(user + off, size, true)); + /* A VM_IO mapping extending into an unmapped hole fails before bouncing. */ + { + unsigned long hole = kunit_vm_mmap(test, NULL, 0, 2 * PAGE_SIZE, + PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, 0); + + KUNIT_ASSERT_TRUE(test, hole && !IS_ERR_VALUE(hole)); + KUNIT_ASSERT_EQ(test, vm_munmap(hole + PAGE_SIZE, PAGE_SIZE), 0); + ane_h16_accel_test_vm_io(hole, true); + KUNIT_EXPECT_TRUE(test, ane_h16_user_unpinnable(hole, PAGE_SIZE, true)); + KUNIT_EXPECT_FALSE(test, ane_h16_user_unpinnable(hole, 2 * PAGE_SIZE, false)); + KUNIT_EXPECT_FALSE(test, ane_h16_user_unpinnable(hole, 2 * PAGE_SIZE, true)); + KUNIT_EXPECT_EQ(test, ane_h16_user_get(&u, hole, 2 * PAGE_SIZE, true), -EFAULT); + KUNIT_EXPECT_NULL(test, u.bounce); + KUNIT_EXPECT_NULL(test, u.pages); + ane_h16_accel_test_vm_io(hole, false); + } + /* A read-only VM_IO mapping is readable as an input, but fails as an output. */ + ane_h16_accel_test_vm_io(ro, true); + KUNIT_EXPECT_TRUE(test, ane_h16_user_unpinnable(ro, 64, false)); + KUNIT_EXPECT_FALSE(test, ane_h16_user_unpinnable(ro, 64, true)); + KUNIT_EXPECT_EQ(test, ane_h16_user_get(&u, ro, 64, true), -EFAULT); + KUNIT_EXPECT_NULL(test, u.bounce); + KUNIT_EXPECT_NULL(test, u.pages); + ane_h16_accel_test_vm_io(ro, false); + ane_h16_accel_test_vm_io(user, false); + + /* An input: read into the bounce copy when taken. */ + KUNIT_ASSERT_EQ(test, ane_h16_user_bounce(&u, user + off, size, false), 0); + KUNIT_EXPECT_NULL(test, u.pages); + KUNIT_EXPECT_NOT_NULL(test, u.bounce); + ane_h16_user_copy(&u, dst, false); + KUNIT_EXPECT_MEMEQ(test, dst, src, size); + KUNIT_EXPECT_EQ(test, ane_h16_user_put(&u), 0); + KUNIT_EXPECT_NULL(test, u.bounce); + + /* An output the request never wrote: user memory is left as it was. */ + KUNIT_ASSERT_EQ(test, ane_h16_user_bounce(&u, user + off, size, true), 0); + KUNIT_EXPECT_EQ(test, ane_h16_user_put(&u), 0); + KUNIT_ASSERT_EQ(test, copy_from_user(dst, (void __user *)(user + off), size), 0); + KUNIT_EXPECT_MEMEQ(test, dst, src, size); + + /* An output: written back at the put. */ + for (i = 0; i < size; i++) + src[i] ^= 0xa5; + KUNIT_ASSERT_EQ(test, ane_h16_user_bounce(&u, user + off, size, true), 0); + ane_h16_user_copy(&u, src, true); + KUNIT_EXPECT_EQ(test, ane_h16_user_put(&u), 0); + KUNIT_ASSERT_EQ(test, copy_from_user(dst, (void __user *)(user + off), size), 0); + KUNIT_EXPECT_MEMEQ(test, dst, src, size); + + /* Unmapped memory, and an output in a read-only mapping, still fail when taken. */ + KUNIT_EXPECT_EQ(test, ane_h16_user_get(&u, 0, 64, false), -EFAULT); + KUNIT_EXPECT_NULL(test, u.bounce); + KUNIT_EXPECT_EQ(test, ane_h16_user_get(&u, ro, 64, true), -EFAULT); + KUNIT_EXPECT_NULL(test, u.bounce); + KUNIT_EXPECT_NULL(test, u.pages); +} + static struct kunit_case ane_h16_accel_cases[] = { KUNIT_CASE(ane_h16_accel_fops_test), KUNIT_CASE(ane_h16_accel_open_test), + KUNIT_CASE(ane_h16_accel_user_pages_test), + KUNIT_CASE(ane_h16_accel_user_bounce_test), {} }; @@ -73,5 +250,5 @@ static struct kunit_suite ane_h16_accel_suite = { kunit_test_suite(ane_h16_accel_suite); -MODULE_DESCRIPTION("KUnit open test for the H16 ANE accel node"); +MODULE_DESCRIPTION("KUnit open and user-buffer tests for the H16 ANE accel node"); MODULE_LICENSE("Dual MIT/GPL"); diff --git a/drivers/accel/ane/ane_h16_hwx.c b/drivers/accel/ane/ane_h16_hwx.c index c8b6dfeeadda1..2c5d72671c738 100644 --- a/drivers/accel/ane/ane_h16_hwx.c +++ b/drivers/accel/ane/ane_h16_hwx.c @@ -392,7 +392,8 @@ static int hwx_program(struct hwx_parser *p) if (!memcmp(c, expected, length)) { ret = ane_hx_task_stream_validate_hdr(p->bytes + info->text.offset, info->text.size, info->tasks, prologue, - t->header_words, &record_limit, &final_end); + info->task_header_words, &record_limit, + &final_end); /* This compiler hint need not equal the encoded record count. */ if (!ret && (!records || records > record_limit)) ret = -ENOEXEC; @@ -599,31 +600,23 @@ static int hwx_symbols(struct hwx_parser *p) static bool hwx_relocations_in_payload(const struct hwx_parser *p, unsigned long *sites) { const u8 *text = p->bytes + p->info.text.offset; - size_t pos = 16; + struct ane_hx_task t; + size_t pos = 16, record; + u32 count; + int ret; while (pos < p->info.text.size) { - u32 words = (get_unaligned_le32(text + pos) >> 16) & 0x7ff; - size_t end, record; - - if (!words) { - pos += 16; + ret = ane_hx_task_frame(text, p->info.text.size, pos, + p->info.task_header_words, &t); + if (ret < 0) + return false; + pos = t.next; + if (!ret) continue; + for (record = t.rec; record < t.end; record += 4 + count * 4) { + count = ane_hx_record_count(get_unaligned_le32(text + record)); + bitmap_clear(sites, record / 4 + 1, count); } - end = pos + words * 4; - record = pos + p->target->header_words * 4; - if ((get_unaligned_le32(text + pos + (p->target->header_words - 1) * 4) & 3) == 3) - record += 4; - while (record < end) { - u32 header = get_unaligned_le32(text + record); - u32 count = header & BIT(31) ? - 1 + hweight32((header >> 15) & 0xffff) : - 1 + ((header >> 15) & 0x3f); - - record += 4; - bitmap_clear(sites, record / 4, count); - record += count * 4; - } - pos = ALIGN(end, 16); } return bitmap_empty(sites, p->info.text.size / 4); } @@ -659,6 +652,7 @@ int ane_h16_hwx_validate(const void *bytes, size_t size, struct ane_h16_hwx *inf p->size = size; p->command_end = 32 + (size_t)length; p->info.subtype = target->subtype; + p->info.task_header_words = target->header_words; for (i = 0; i < count; i++) { u32 cmd, len; diff --git a/drivers/accel/ane/ane_h16_hwx.h b/drivers/accel/ane/ane_h16_hwx.h index 7b2fe402cad72..eb1022a8928ed 100644 --- a/drivers/accel/ane/ane_h16_hwx.h +++ b/drivers/accel/ane/ane_h16_hwx.h @@ -54,6 +54,8 @@ struct ane_h16_hwx { struct ane_h16_hwx_section text, constants, kernel, scratch; /* Target; a backend accepts only its own engine's subtype. */ u32 subtype; + /* Fixed words per task of the target's task stream (ane_hx_task_frame()). */ + u32 task_header_words; struct ane_h16_hwx_surface surfaces[ANE_H16_SURFACES]; u32 surface_count; u32 tasks; diff --git a/drivers/accel/ane/ane_h16_program.c b/drivers/accel/ane/ane_h16_program.c index c835fab0f1076..1d88691cfa064 100644 --- a/drivers/accel/ane/ane_h16_program.c +++ b/drivers/accel/ane/ane_h16_program.c @@ -8,6 +8,7 @@ #include #include "ane_h16_program.h" +#include "ane_h16_tasks.h" /* * The one LOAD slot table. Slot 1 carries the HWX kernel section, or the @@ -302,10 +303,12 @@ int ane_h16_process_bindings(const struct ane_h16_hwx *hwx, const void *generic, hwx->surface_count > ANE_H16_SURFACES || (hwx->scratch.size && !scratch)) return -EINVAL; entries = hwx->surface_count + !!hwx->scratch.size; + /* The firmware reads the whole section: every byte has its one value. */ if (generic_size != ANE_H16_GENERIC_HEADER + entries * ANE_H16_GENERIC_ENTRY || get_unaligned_le32(g) != ANE_H16_GENERIC_MAGIC || !get_unaligned_le32(g + 4) || get_unaligned_le32(g + 4) > ANE_H16_GENERIC_VERSION || + memchr_inv(g + 8, 0, ANE_H16_GENERIC_COUNT - 8) || get_unaligned_le32(g + ANE_H16_GENERIC_COUNT) != entries) return -ENOEXEC; for (i = 0; i < entries; i++) { @@ -320,7 +323,11 @@ int ane_h16_process_bindings(const struct ane_h16_hwx *hwx, const void *generic, if (get_unaligned_le32(e) != 1 || id == 2 || id == 3 || id > ANE_H16_GENERIC_ID_MAX || get_unaligned_le32(e + 8) != direction - 1 || - get_unaligned_le32(e + 0x10) != direction) + get_unaligned_le32(e + 0xc) || + get_unaligned_le32(e + 0x10) != direction || + memchr_inv(e + 0x14, 0, 0xc) || + get_unaligned_le32(e + 0x28) != ANE_H16_GENERIC_ENTRY_TAIL || + get_unaligned_le32(e + 0x2c)) return -ENOEXEC; /* The scratch entry may round __bss up to its 16 KiB mapping. */ if (is_scratch ? bytes < hwx->scratch.size || @@ -355,6 +362,22 @@ int ane_h16_process_bindings(const struct ane_h16_hwx *hwx, const void *generic, } EXPORT_SYMBOL_GPL(ane_h16_process_bindings); +/* The kernel slot runs to the end of the section's last 16 KiB page of its region. */ +static u64 h16_kernel_reach(const struct ane_h16_hwx *hwx) +{ + unsigned int i; + + for (i = 0; i < hwx->region_count; i++) { + const struct ane_h16_hwx_region *r = &hwx->regions[i]; + u64 offset = hwx->kernel.vm - r->vm; + + if (r->kind == ANE_H16_REGION_KERNEL && hwx->kernel.vm >= r->vm && + offset < r->size && hwx->kernel.size <= r->size - offset) + return ALIGN(offset + hwx->kernel.size, SZ_16K) - offset; + } + return hwx->kernel.size; +} + /* * The operation section binds BAR slots to resources: per record, a count at * +8 and (slot, tag) pairs from +0xc. A tag is a generic-section ID, or the @@ -363,6 +386,12 @@ EXPORT_SYMBOL_GPL(ane_h16_process_bindings); * name the resource whose HWX address is that base. Kernel programs must bind * the kernel: the legacy tag 2 instead makes the H17 firmware assert and stop * answering (H17 harness facts, kernel-table programs). + * + * Each DMA base offset in the task stream is taken relative to the DVA of + * the buffer bound to its slot, so every base must name a bound slot and + * stay inside that buffer with the extent of its transfer: a surface's + * declared size, the scratch's __bss size, or the kernel section to the end + * of its last 16 KiB page, which is what the kernel slot maps. */ int ane_h16_operation_validate(const struct ane_h16_hwx_snapshot *snapshot, const struct ane_h16_hwx *hwx, const void *operation, @@ -374,6 +403,7 @@ int ane_h16_operation_validate(const struct ane_h16_hwx_snapshot *snapshot, const u8 *o = operation, *c; unsigned int r, records, i, j; bool kernel_bound = false; + u64 *bound; int ret; if (!hwx || !o || !bindings || count != hwx->surface_count || @@ -390,43 +420,107 @@ int ane_h16_operation_validate(const struct ane_h16_hwx_snapshot *snapshot, return -ENOEXEC; records = get_unaligned_le32(o); if (!records || records > ANE_H16_OPERATION_RECORDS_MAX || - operation_size != 4 + (size_t)records * ANE_H16_OPERATION_RECORD) + operation_size != 4 + (size_t)records * ANE_H16_OPERATION_RECORD || + hwx->text.offset > snapshot->size || hwx->text.size > snapshot->size - hwx->text.offset) return -ENOEXEC; + bound = kcalloc(ANE_HX_DMA_SLOTS, sizeof(*bound), GFP_KERNEL); + if (!bound) + return -ENOMEM; + ret = -ENOEXEC; for (r = 0; r < records; r++) { const u8 *rec = o + 4 + r * ANE_H16_OPERATION_RECORD; u32 refs = get_unaligned_le32(rec + 8); u64 seen = 0; if (get_unaligned_le32(rec) || get_unaligned_le32(rec + 4) || - refs > ANE_H16_OPERATION_REFS_MAX) - return -ENOEXEC; + refs > ANE_H16_OPERATION_REFS_MAX || + memchr_inv(rec + 0xc + refs * 8, 0, ANE_H16_OPERATION_RECORD - 0xc - refs * 8)) + goto out; for (i = 0; i < refs; i++) { u32 slot = get_unaligned_le32(rec + 0xc + i * 8); u32 tag = get_unaligned_le32(rec + 0x10 + i * 8); - u64 base, want = 0; + u64 base, want = 0, size = 0; - if (slot >= 64 || (seen & BIT_ULL(slot))) - return -ENOEXEC; + if (slot >= ANE_HX_DMA_SLOTS || (seen & BIT_ULL(slot))) + goto out; seen |= BIT_ULL(slot); base = get_unaligned_le64(c + 0x10 * (slot + 1)); if (tag == ANE_H16_RESOURCE_KERNEL && hwx->kernel.size) { want = hwx->kernel.vm; + size = h16_kernel_reach(hwx); kernel_bound = true; } else if (scratch && hwx->scratch.size && tag == scratch->index) { want = scratch->hwx_vm; + size = hwx->scratch.size; } else { for (j = 0; j < count; j++) - if (tag == bindings[j].index) + if (tag == bindings[j].index) { want = bindings[j].hwx_vm; + size = bindings[j].bytes; + } } if (!want || base != want) - return -ENOEXEC; + goto out; + bound[slot] = size; } } - return hwx->kernel.size && !kernel_bound ? -ENOEXEC : 0; + if (hwx->kernel.size && !kernel_bound) + goto out; + ret = ane_hx_task_stream_dma_validate((const u8 *)snapshot->bytes + hwx->text.offset, + hwx->text.size, hwx->task_header_words, bound); +out: + kfree(bound); + return ret; } EXPORT_SYMBOL_GPL(ane_h16_operation_validate); +/* + * The procedure and text-properties sections have one form each, which the + * kernel derives from the validated program: one procedure, and one text + * segment of the whole task stream with its task count. The firmware takes + * the stream's framing (segment offset, size, task count) from the text + * properties, so any other form would describe a stream other than the one + * the task validator checked. + */ +void ane_h16_fixed_sections(const struct ane_h16_hwx *hwx, + u8 procedure[ANE_H16_PROCEDURE_SIZE], + u8 properties[ANE_H16_PROPERTIES_SIZE]) +{ + static const u32 call[] = { 1, 3, 0, 0, 1, 0, U32_MAX, 4 }; + unsigned int i; + + memset(procedure, 0, ANE_H16_PROCEDURE_SIZE); + put_unaligned_le32(1, procedure); + put_unaligned_le64(0x18, procedure + 8); + put_unaligned_le64(sizeof(call), procedure + 0x10); + for (i = 0; i < ARRAY_SIZE(call); i++) + put_unaligned_le32(call[i], procedure + 0x18 + i * 4); + memset(properties, 0, ANE_H16_PROPERTIES_SIZE); + put_unaligned_le32(1, properties); + put_unaligned_le32(hwx->tasks, properties + 0xc); + put_unaligned_le64(hwx->text.size, properties + 0x20); +} +EXPORT_SYMBOL_GPL(ane_h16_fixed_sections); + +int ane_h16_fixed_sections_validate(const struct ane_h16_hwx *hwx, + const void *const aux[ANE_H16_AUX_COUNT], + const size_t aux_size[ANE_H16_AUX_COUNT]) +{ + u8 procedure[ANE_H16_PROCEDURE_SIZE], properties[ANE_H16_PROPERTIES_SIZE]; + + if (!hwx || !aux || !aux_size || !aux[ANE_H16_AUX_PROCEDURE] || + !aux[ANE_H16_AUX_TEXT_PROPERTIES]) + return -EINVAL; + ane_h16_fixed_sections(hwx, procedure, properties); + if (aux_size[ANE_H16_AUX_PROCEDURE] != sizeof(procedure) || + aux_size[ANE_H16_AUX_TEXT_PROPERTIES] != sizeof(properties) || + memcmp(aux[ANE_H16_AUX_PROCEDURE], procedure, sizeof(procedure)) || + memcmp(aux[ANE_H16_AUX_TEXT_PROPERTIES], properties, sizeof(properties))) + return -ENOEXEC; + return 0; +} +EXPORT_SYMBOL_GPL(ane_h16_fixed_sections_validate); + /* * The mcache custom-BAR records come from the program descriptor relocation * tuples pinned by the HWX validator. The linear 0x8a0 form has one prefetch @@ -895,8 +989,14 @@ EXPORT_SYMBOL_GPL(ane_h16_export_get); /* * Drop a reference. The last one returns a released surface to the spares - * from a work item: a mapping's last put runs under the process mmap lock, - * which an EXECUTE may wait for while holding the node lock. + * from a work item, because the spares need the node lock and the last put + * can come from two places that cannot take it: h16_give_back(), which + * already holds it, and a mapping's close in munmap() or process exit, which + * would then wait under the process mmap lock for a running request to end. + * Taking the node lock under the mmap lock is the order ane_h16_mmap() uses; + * the order that must not happen is the reverse, so nothing that faults user + * memory or takes the mmap lock runs under the node lock (EXECUTE pins or + * bounces its buffers before taking it). */ void ane_h16_export_put(struct ane_h16_export *e) { @@ -970,9 +1070,11 @@ static int h16_program_bind(const struct ane_h16_program *p, { int ret; - ret = ane_h16_process_bindings(&p->hwx, aux[ANE_H16_AUX_GENERIC], - aux_size[ANE_H16_AUX_GENERIC], b->bindings, - &b->count, &b->scratch); + ret = ane_h16_fixed_sections_validate(&p->hwx, aux, aux_size); + if (!ret) + ret = ane_h16_process_bindings(&p->hwx, aux[ANE_H16_AUX_GENERIC], + aux_size[ANE_H16_AUX_GENERIC], b->bindings, + &b->count, &b->scratch); if (!ret) ret = ane_h16_operation_validate(&p->snapshot, &p->hwx, aux[ANE_H16_AUX_OPERATION], diff --git a/drivers/accel/ane/ane_h16_program.h b/drivers/accel/ane/ane_h16_program.h index bb30d3dce8c20..be8aae41e34d5 100644 --- a/drivers/accel/ane/ane_h16_program.h +++ b/drivers/accel/ane/ane_h16_program.h @@ -53,6 +53,8 @@ #define ANE_H16_GENERIC_HEADER 0x208 #define ANE_H16_GENERIC_ENTRY 0x30 #define ANE_H16_GENERIC_ID_MAX 0x3c +/* The u32 at +0x28 of every entry, as every host builder writes it. */ +#define ANE_H16_GENERIC_ENTRY_TAIL 0xffff /* Host-built operation section: a count, then fixed-size BAR records. */ #define ANE_H16_OPERATION_RECORD 0x40c @@ -71,6 +73,10 @@ enum ane_h16_aux { #define ANE_H16_AUX_MIN 0x28 #define ANE_H16_AUX_MAX SZ_64K +/* The one form of the procedure and text-properties sections. */ +#define ANE_H16_PROCEDURE_SIZE 0x38 +#define ANE_H16_PROPERTIES_SIZE 0x28 + /* Source of one LOAD slot. */ enum ane_h16_slot_source { ANE_H16_SLOT_EMPTY, @@ -155,6 +161,12 @@ int ane_h16_operation_validate(const struct ane_h16_hwx_snapshot *snapshot, const struct ane_h16_process_binding *bindings, unsigned int count, const struct ane_h16_process_binding *scratch); +void ane_h16_fixed_sections(const struct ane_h16_hwx *hwx, + u8 procedure[ANE_H16_PROCEDURE_SIZE], + u8 properties[ANE_H16_PROPERTIES_SIZE]); +int ane_h16_fixed_sections_validate(const struct ane_h16_hwx *hwx, + const void *const aux[ANE_H16_AUX_COUNT], + const size_t aux_size[ANE_H16_AUX_COUNT]); int ane_h16_custom_bars(const struct ane_h16_hwx_snapshot *snapshot, const struct ane_h16_hwx *hwx, u32 dsid, struct ane_h16_custom_bar *bars, unsigned int *count); diff --git a/drivers/accel/ane/ane_h16_program_test.c b/drivers/accel/ane/ane_h16_program_test.c index 6a48bbf030e24..f8e51ac753be2 100644 --- a/drivers/accel/ane/ane_h16_program_test.c +++ b/drivers/accel/ane/ane_h16_program_test.c @@ -592,6 +592,15 @@ static size_t test_aux_size[ANE_H16_AUX_COUNT] = { 0x298, 0x410, 0x38, 0x28 }; #define TEST_GENERIC_BASE 8 +/* The procedure and text-properties sections a host builder writes for hwx. */ +static void fixed_fill(const struct ane_h16_hwx *hwx) +{ + ane_h16_fixed_sections(hwx, test_aux[ANE_H16_AUX_PROCEDURE], + test_aux[ANE_H16_AUX_TEXT_PROPERTIES]); + test_aux_size[ANE_H16_AUX_PROCEDURE] = ANE_H16_PROCEDURE_SIZE; + test_aux_size[ANE_H16_AUX_TEXT_PROPERTIES] = ANE_H16_PROPERTIES_SIZE; +} + static int fw_load_snapshot(struct prog_fw *f, struct ane_h16_hwx_snapshot *snapshot, struct ane_h16_program **p) { @@ -604,6 +613,7 @@ static int fw_load_snapshot(struct prog_fw *f, struct ane_h16_hwx_snapshot *snap test_aux_size[ANE_H16_AUX_OPERATION] = operation_fill(test_aux[ANE_H16_AUX_OPERATION], snapshot, &hwx, TEST_GENERIC_BASE); + fixed_fill(&hwx); return ane_h16_session_load(&f->s, snapshot, test_aux_ptr, test_aux_size, p); } @@ -1248,6 +1258,7 @@ static void program_scratch_test(struct kunit *test) put_unaligned_le32(TEST_GENERIC_BASE + 2, e + 4); test_aux_size[ANE_H16_AUX_GENERIC] = size; + fixed_fill(hwx); KUNIT_ASSERT_EQ(test, ane_h16_session_load(&f->s, snapshot, test_aux_ptr, test_aux_size, &p), 0); KUNIT_ASSERT_TRUE(test, p->has_scratch); @@ -1328,6 +1339,7 @@ static void program_bad_operation_test(struct kunit *test) test_aux_size[ANE_H16_AUX_OPERATION] = operation_fill(test_aux[ANE_H16_AUX_OPERATION], snapshot, &hwx, TEST_GENERIC_BASE); + fixed_fill(&hwx); /* One record more than the section carries. */ put_unaligned_le32(get_unaligned_le32(test_aux[ANE_H16_AUX_OPERATION]) + 1, test_aux[ANE_H16_AUX_OPERATION]); @@ -1613,6 +1625,68 @@ static void program_pool_test(struct kunit *test) KUNIT_EXPECT_EQ(test, f->count, sent + 2); } +/* The add-128 source 1 base record: [0x4d10, 0x4d14], relocated on BAR slot 6. */ +#define ADD128_SRC1 (0x4d10 / 4 | 1 << 15 | BIT(29) | 6 << 23) + +/* + * Run the LOAD checks of the add-128 program with its source 1 base record + * header and offset replaced. The operation section binds x, y, z to slots + * 6, 7, 8; source 1 reads 128 rows of 0x100 bytes, all 32 KiB of x. + */ +static int add128_dma_load(struct kunit *test, u32 header, u64 offset) +{ + struct ane_h16_process_binding bindings[ANE_H16_SURFACES], scratch = {}; + struct ane_h16_hwx_snapshot *snapshot = kunit_kzalloc(test, sizeof(*snapshot), + GFP_KERNEL); + u8 *hwx = sparse_bytes(test, add128_hwx_runs, ARRAY_SIZE(add128_hwx_runs), + ADD128_HWX_SIZE); + u8 *generic = sparse_bytes(test, add128_generic_runs, ARRAY_SIZE(add128_generic_runs), + ADD128_GENERIC_SIZE); + u8 *operation = sparse_bytes(test, add128_operation_runs, + ARRAY_SIZE(add128_operation_runs), ADD128_OPERATION_SIZE); + unsigned int count; + size_t at; + int ret; + + if (!snapshot || !hwx || !generic || !operation) + return -ENOMEM; + for (at = 0; at + 12 <= ADD128_HWX_SIZE; at += 4) + if (get_unaligned_le32(hwx + at) == ADD128_SRC1) + break; + if (at + 12 > ADD128_HWX_SIZE) + return -ENOENT; + put_unaligned_le32(header, hwx + at); + put_unaligned_le64(offset, hwx + at + 4); + ret = ane_h16_hwx_snapshot_create(hwx, ADD128_HWX_SIZE, snapshot); + if (ret) + return ret; + ret = kunit_add_action_or_reset(test, snapshot_free, snapshot); + if (ret) + return ret; + ret = ane_h16_process_bindings(&snapshot->info, generic, ADD128_GENERIC_SIZE, bindings, + &count, &scratch); + if (!ret) + ret = ane_h16_operation_validate(snapshot, &snapshot->info, operation, + ADD128_OPERATION_SIZE, bindings, count, &scratch); + return ret; +} + +/* Each DMA base stays inside the buffer its slot binds, at LOAD and before any mapping. */ +static void program_add128_dma_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, add128_dma_load(test, ADD128_SRC1, 0), 0); + /* One 64-byte line past the end of x, into z's place, and before x. */ + KUNIT_EXPECT_EQ(test, add128_dma_load(test, ADD128_SRC1, 0x40), -ENOEXEC); + KUNIT_EXPECT_EQ(test, add128_dma_load(test, ADD128_SRC1, 0x10000), -ENOEXEC); + KUNIT_EXPECT_EQ(test, add128_dma_load(test, ADD128_SRC1, -0x8000), -ENOEXEC); + /* Slot 9, which the operation section does not bind. */ + KUNIT_EXPECT_EQ(test, add128_dma_load(test, (ADD128_SRC1 & ~(0x3f << 23)) | 9 << 23, 0), + -ENOEXEC); + /* The base without its relocation: an absolute DVA. */ + KUNIT_EXPECT_EQ(test, add128_dma_load(test, ADD128_SRC1 & ~(BIT(29) | 0x3f << 23), 0), + -ENOEXEC); +} + static u8 *fw_sparse_large(struct kunit *test, const struct ane_h16_sparse_run *runs, unsigned int count, size_t size) { @@ -1660,6 +1734,147 @@ static void mobilenet_sections(struct kunit *test, const void *aux[ANE_H16_AUX_C ARRAY_SIZE(mobilenet_properties_runs), aux_size[3]); } +/* + * Every byte of the four host-built sections has its one value. The add128 + * and mobilenet fixtures' sections from h16-sections.py are the kernel's + * procedure and text properties, and any other procedure, text framing or + * nonzero reserved byte is refused at LOAD, before anything is mapped or sent. + */ +static void program_sections_exact_test(struct kunit *test) +{ + struct ane_h16_process_binding bindings[ANE_H16_SURFACES], scratch = {}; + struct ane_h16_hwx_snapshot *snapshot = kunit_kzalloc(test, sizeof(*snapshot), + GFP_KERNEL); + size_t aux_size[ANE_H16_AUX_COUNT] = { + ADD128_GENERIC_SIZE, ADD128_OPERATION_SIZE, + ADD128_PROCEDURE_SIZE, ADD128_PROPERTIES_SIZE, + }; + static const size_t generic_bytes[] = { 0x8, 0x100, 0x203 }; + static const size_t entry_bytes[] = { 0xc, 0x14, 0x1f, 0x28, 0x2c }; + const void *mobilenet[ANE_H16_AUX_COUNT]; + size_t mobilenet_size[ANE_H16_AUX_COUNT]; + struct ane_h16_hwx_snapshot *m = mobilenet_snapshot(test); + struct prog_fw *f = fw_create(test); + struct ane_h16_program *p = NULL; + void *aux[ANE_H16_AUX_COUNT]; + u8 *hwx, *g, *o, *pr, *tp; + unsigned int i, count; + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, snapshot); + KUNIT_ASSERT_NOT_NULL(test, m); + hwx = sparse_bytes(test, add128_hwx_runs, ARRAY_SIZE(add128_hwx_runs), ADD128_HWX_SIZE); + g = sparse_bytes(test, add128_generic_runs, ARRAY_SIZE(add128_generic_runs), + ADD128_GENERIC_SIZE); + o = sparse_bytes(test, add128_operation_runs, ARRAY_SIZE(add128_operation_runs), + ADD128_OPERATION_SIZE); + pr = sparse_bytes(test, add128_procedure_runs, ARRAY_SIZE(add128_procedure_runs), + ADD128_PROCEDURE_SIZE); + tp = sparse_bytes(test, add128_properties_runs, ARRAY_SIZE(add128_properties_runs), + ADD128_PROPERTIES_SIZE); + aux[0] = g; + aux[1] = o; + aux[2] = pr; + aux[3] = tp; + KUNIT_ASSERT_NOT_NULL(test, hwx); + for (i = 0; i < ANE_H16_AUX_COUNT; i++) + KUNIT_ASSERT_NOT_NULL(test, aux[i]); + KUNIT_ASSERT_EQ(test, ane_h16_hwx_snapshot_create(hwx, ADD128_HWX_SIZE, snapshot), 0); + KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, snapshot_free, snapshot), 0); + + /* What the builder wrote is the kernel's form, for one task and for 28. */ + KUNIT_EXPECT_EQ(test, ane_h16_fixed_sections_validate(&snapshot->info, + (const void *const *)aux, + aux_size), 0); + mobilenet_sections(test, mobilenet, mobilenet_size); + KUNIT_EXPECT_EQ(test, ane_h16_fixed_sections_validate(&m->info, mobilenet, + mobilenet_size), 0); + KUNIT_EXPECT_EQ(test, ane_h16_fixed_sections_validate(&m->info, + (const void *const *)aux, + aux_size), -ENOEXEC); + + /* Another task count, segment offset or size, or a longer section. */ + put_unaligned_le32(2, tp + 0xc); + KUNIT_EXPECT_EQ(test, ane_h16_fixed_sections_validate(&snapshot->info, + (const void *const *)aux, + aux_size), -ENOEXEC); + put_unaligned_le32(1, tp + 0xc); + put_unaligned_le64(0x10, tp + 0x10); + KUNIT_EXPECT_EQ(test, ane_h16_fixed_sections_validate(&snapshot->info, + (const void *const *)aux, + aux_size), -ENOEXEC); + put_unaligned_le64(0, tp + 0x10); + put_unaligned_le64(0x120, tp + 0x20); + KUNIT_EXPECT_EQ(test, ane_h16_fixed_sections_validate(&snapshot->info, + (const void *const *)aux, + aux_size), -ENOEXEC); + put_unaligned_le64(0x130, tp + 0x20); + aux_size[3] = ADD128_PROPERTIES_SIZE + 4; + KUNIT_EXPECT_EQ(test, ane_h16_fixed_sections_validate(&snapshot->info, + (const void *const *)aux, + aux_size), -ENOEXEC); + aux_size[3] = ADD128_PROPERTIES_SIZE; + /* A second procedure, or another word of the one there is. */ + put_unaligned_le32(2, pr); + KUNIT_EXPECT_EQ(test, ane_h16_fixed_sections_validate(&snapshot->info, + (const void *const *)aux, + aux_size), -ENOEXEC); + put_unaligned_le32(1, pr); + pr[0x34] = 5; + KUNIT_EXPECT_EQ(test, ane_h16_fixed_sections_validate(&snapshot->info, + (const void *const *)aux, + aux_size), -ENOEXEC); + pr[0x34] = 4; + + /* Reserved generic bytes: in the header and in every field of an entry. */ + KUNIT_ASSERT_EQ(test, ane_h16_process_bindings(&snapshot->info, g, ADD128_GENERIC_SIZE, + bindings, &count, &scratch), 0); + for (i = 0; i < ARRAY_SIZE(generic_bytes); i++) { + g[generic_bytes[i]] ^= 1; + KUNIT_EXPECT_EQ(test, ane_h16_process_bindings(&snapshot->info, g, + ADD128_GENERIC_SIZE, bindings, + &count, &scratch), -ENOEXEC); + g[generic_bytes[i]] ^= 1; + } + for (i = 0; i < ARRAY_SIZE(entry_bytes); i++) { + u8 *e = g + ANE_H16_GENERIC_HEADER + 2 * ANE_H16_GENERIC_ENTRY + entry_bytes[i]; + + *e ^= 1; + KUNIT_EXPECT_EQ(test, ane_h16_process_bindings(&snapshot->info, g, + ADD128_GENERIC_SIZE, bindings, + &count, &scratch), -ENOEXEC); + *e ^= 1; + } + KUNIT_ASSERT_EQ(test, ane_h16_process_bindings(&snapshot->info, g, ADD128_GENERIC_SIZE, + bindings, &count, &scratch), 0); + /* An operation record's bytes after its last reference. */ + KUNIT_EXPECT_EQ(test, ane_h16_operation_validate(snapshot, &snapshot->info, o, + ADD128_OPERATION_SIZE, bindings, count, + &scratch), 0); + o[4 + 0xc + 3 * 8] = 7; + KUNIT_EXPECT_EQ(test, ane_h16_operation_validate(snapshot, &snapshot->info, o, + ADD128_OPERATION_SIZE, bindings, count, + &scratch), -ENOEXEC); + o[4 + 0xc + 3 * 8] = 0; + o[ADD128_OPERATION_SIZE - 1] = 1; + KUNIT_EXPECT_EQ(test, ane_h16_operation_validate(snapshot, &snapshot->info, o, + ADD128_OPERATION_SIZE, bindings, count, + &scratch), -ENOEXEC); + o[ADD128_OPERATION_SIZE - 1] = 0; + + /* At LOAD: refused before any mapping or firmware traffic, then loads. */ + put_unaligned_le32(2, tp + 0xc); + KUNIT_EXPECT_EQ(test, ane_h16_session_load(&f->s, snapshot, (const void *const *)aux, + aux_size, &p), -ENOEXEC); + KUNIT_EXPECT_PTR_EQ(test, p, NULL); + KUNIT_EXPECT_EQ(test, f->count, 0); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); + put_unaligned_le32(1, tp + 0xc); + KUNIT_ASSERT_EQ(test, ane_h16_session_load(&f->s, snapshot, (const void *const *)aux, + aux_size, &p), 0); + KUNIT_EXPECT_EQ(test, ane_h16_session_close(&f->s, p), 0); +} + /* * The H17 MobileNetV1 HWX is one linked program: 28 tasks, input x and * output y as surfaces, the 3.5 MiB __DATA scratch on BAR slot 3 and the @@ -3764,6 +3979,8 @@ static struct kunit_case program_cases[] = { KUNIT_CASE(program_add128_slots_test), KUNIT_CASE(program_kernel_slots_test), KUNIT_CASE(program_add128_hwx_test), + KUNIT_CASE(program_add128_dma_test), + KUNIT_CASE(program_sections_exact_test), KUNIT_CASE(program_replies_test), KUNIT_CASE(program_deny_list_test), KUNIT_CASE(program_custom_bar_test), diff --git a/drivers/accel/ane/ane_h16_tasks.h b/drivers/accel/ane/ane_h16_tasks.h index 166d46abdae0d..f4560e6eb64ff 100644 --- a/drivers/accel/ane/ane_h16_tasks.h +++ b/drivers/accel/ane/ane_h16_tasks.h @@ -6,7 +6,10 @@ #include #include #include +#include +#include #include +#include #include #include #include @@ -33,62 +36,97 @@ static inline bool ane_h16_register_valid(u32 word) return false; } +/* Values after record header @h: a dense run, or one plus a scatter mask's bits. */ +static inline u32 ane_hx_record_count(u32 h) +{ + return h & BIT(31) ? 1 + hweight32((h >> 15) & 0xffff) : ((h >> 15) & 0x3f) + 1; +} + /* * A stream opens with a 16-byte frame: little-endian u32 @prologue, then * twelve reserved zero bytes. H15/H16/H17 programs share this framing with - * @header_words fixed words per task (8 on H15, 9 on H16 and H17). The last - * task may end at the end of the section; every other task is padded with - * zeros to 16 bytes. This structural gate does not replace container/tensor/ - * aperture validation. + * @header_words fixed words per task (8 on H15, 9 on H16 and H17, from the + * HWX target), the task's length in words in bits 16-26 of the first. Low + * bits 3 in the last fixed word add one more header word. The last task may + * end at the end of the section; every other task is padded with zeros to + * 16 bytes, and a 16-byte zero block may stand between tasks. + * + * ane_hx_task_frame() is the one walk of this framing: the structural gate, + * the relocation check of the HWX validator and the DMA bound all step + * through a stream with it, so they cannot disagree on where a task's + * records start. + */ +struct ane_hx_task { + size_t rec, end; /* the task's records */ + size_t next; /* the next task or zero block */ +}; + +/* + * Frame the task or zero block at @pos of a stream of @size bytes. Returns 1 + * for a task, 0 for a zero block (only @t->next is set) and -ENOEXEC for a + * frame that does not fit or is not padded with zeros. + */ +static inline int ane_hx_task_frame(const u8 *p, size_t size, size_t pos, + unsigned int header_words, struct ane_hx_task *t) +{ + u32 words; + + if (!header_words || pos > size || size - pos < 16) + return -ENOEXEC; + words = (get_unaligned_le32(p + pos) >> 16) & 0x7ff; + if (!words) { + t->next = pos + 16; + return memchr_inv(p + pos, 0, 16) ? -ENOEXEC : 0; + } + if (words < header_words || words > (size - pos) / 4) + return -ENOEXEC; + t->end = pos + words * 4; + if ((get_unaligned_le32(p + pos + (header_words - 1) * 4) & 3) == 3) + header_words++; + if (words < header_words) + return -ENOEXEC; + t->rec = pos + header_words * 4; + t->next = t->end == size ? size : ALIGN(t->end, 16); + if (t->next > size || memchr_inv(p + t->end, 0, t->next - t->end)) + return -ENOEXEC; + return 1; +} + +/* + * The structural gate over the framing above. Every record names registers + * of the H16 image. This gate does not replace container/tensor/aperture + * validation. */ static inline int ane_hx_task_stream_validate_hdr(const void *text, size_t size, u32 expected_tasks, - u32 prologue, unsigned int fixed_header_words, + u32 prologue, unsigned int header_words, u32 *record_limit, size_t *final_end) { const u8 *p = text; size_t pos = 16, last = 0; u32 tasks = 0, limit = 0; - if (!p || !expected_tasks || !fixed_header_words || size < 16 || - size > ANE_H16_TASK_MAX || (size & 3) || + if (!p || !expected_tasks || size < 16 || size > ANE_H16_TASK_MAX || (size & 3) || get_unaligned_le32(p) != prologue || memchr_inv(p + 4, 0, 12)) return -ENOEXEC; while (pos < size) { - u32 words, header_words = fixed_header_words; - size_t end, padded, rec; + struct ane_hx_task t; + int ret = ane_hx_task_frame(p, size, pos, header_words, &t); + size_t rec; - if (size - pos < 16) - return -ENOEXEC; - words = (get_unaligned_le32(p + pos) >> 16) & 0x7ff; - if (!words) { - if (memchr_inv(p + pos, 0, 16)) - return -ENOEXEC; - pos += 16; + if (ret < 0) + return ret; + pos = t.next; + if (!ret) continue; - } - if (words < fixed_header_words || words > (size - pos) / 4) - return -ENOEXEC; - end = pos + words * 4; - if ((get_unaligned_le32(p + pos + (fixed_header_words - 1) * 4) & 3) == 3) - header_words++; - if (words < header_words) - return -ENOEXEC; - rec = pos + header_words * 4; /* A record needs at least one header and one register value. */ - limit += (end - rec) / 8; - while (rec < end) { - u32 h = get_unaligned_le32(p + rec); - u32 base = h & 0x7fff; - u32 mask = (h >> 15) & 0xffff; - u32 count, i; + limit += (t.end - t.rec) / 8; + for (rec = t.rec; rec < t.end; ) { + u32 h = get_unaligned_le32(p + rec), count = ane_hx_record_count(h); + u32 base = h & 0x7fff, mask = (h >> 15) & 0xffff, i; rec += 4; - if (h & BIT(31)) - count = 1 + hweight32(mask); - else - count = ((h >> 15) & 0x3f) + 1; - if (count > (end - rec) / 4 || !ane_h16_register_valid(base)) + if (count > (t.end - rec) / 4 || !ane_h16_register_valid(base)) return -ENOEXEC; if (h & BIT(31)) { for (i = 0; i < 16; i++) @@ -102,11 +140,7 @@ ane_hx_task_stream_validate_hdr(const void *text, size_t size, u32 expected_task } rec += count * 4; } - padded = end == size ? end : ALIGN(end, 16); - if (padded > size || memchr_inv(p + end, 0, padded - end)) - return -ENOEXEC; - last = end; - pos = padded; + last = t.end; if (++tasks > expected_tasks) return -ENOEXEC; } @@ -140,6 +174,429 @@ ane_h16_task_stream_validate(const void *text, size_t size, u32 expected_tasks) return ane_hx_task_stream_validate(text, size, expected_tasks, 1); } +/* + * DMA bases. A task names each buffer the engine reads or writes as a + * relocated [lo, hi] record: its 64-bit offset is taken relative to the DVA + * of the buffer bound to the record's BAR slot, and nothing after this point + * bounds the sum. The bases are the input DMA's sources 1 and 2, the output + * DMA, the coefficient DMA and the lookup table. + */ +#define ANE_HX_DMA_SLOTS 64 +#define ANE_HX_DMA_WORDS (23 + 81 + 21 + 72) + +/* Image index of a common or DMA register byte address, or -1. */ +static inline int ane_hx_dma_index(u32 addr) +{ + if (addr < 0x5c) + return addr / 4; + if (addr >= 0x4d00 && addr < 0x4e44) + return 23 + (addr - 0x4d00) / 4; + if (addr >= 0x5100 && addr < 0x5154) + return 104 + (addr - 0x5100) / 4; + if (addr >= 0x5500 && addr < 0x5620) + return 125 + (addr - 0x5500) / 4; + return -1; +} + +static inline bool ane_hx_dma_base(u32 addr) +{ + switch (addr & ~4) { + case 0x4d10: case 0x4d28: case 0x5108: case 0x5510: case 0x5618: + return true; + } + return false; +} + +/* + * The DMA registers a task may set: the bases, their strides, formats and + * coefficient lanes, and the words that only configure or hint a transfer. + * The rest of each DMA block (metadata and compression, texture, crop and + * wrap, the bias, post-scale and palette tables) moves addresses this bound + * does not model, and no measured program sets it, so it stays zero. + */ +static inline bool ane_hx_dma_modelled(u32 addr) +{ + return (addr >= 0x4d00 && addr <= 0x4d3c) || addr == 0x4d68 || addr == 0x4d6c || + addr == 0x5100 || (addr >= 0x5108 && addr <= 0x511c) || addr == 0x5138 || + addr == 0x5500 || addr == 0x5504 || (addr >= 0x5510 && addr <= 0x55dc) || + (addr >= 0x5610 && addr <= 0x561c); +} + +/* Source 1, source 2 and destination DMA configuration and coefficient lanes. */ +static inline bool ane_hx_dma_config(u32 addr) +{ + return addr == 0x4d00 || addr == 0x4d04 || addr == 0x5100 || + (addr >= 0x5520 && addr <= 0x555c); +} + +/* + * Whether configuration word @w at @addr enables its DMA: any non-zero value + * except a coefficient lane's idle form, 0x20 in the low half, which + * lookup-table tasks write for the lanes they do not fetch. + */ +static inline bool ane_hx_dma_config_on(u32 addr, u32 w) +{ + return w && !(addr >= 0x5520 && addr <= 0x555c && (w & 0xffff) == 0x20); +} + +/* + * A count of @n steps @stride apart. Only a zero stride lets a zero count be + * read as one: the kernel cannot bound what a zero count moves when the + * engine steps by the stride. + */ +static inline int ane_hx_dma_count(u32 n, u32 stride, u64 *count) +{ + if (!n && stride) + return -ENOEXEC; + *count = max(1U, n); + return 0; +} + +/* Byte address of value @i of the record with header @h. */ +static inline u32 ane_hx_record_addr(u32 h, u32 i) +{ + u32 base = h & 0x7fff, mask = (h >> 15) & 0xffff, bit; + + if (!(h & BIT(31)) || !i) + return (base + i) * 4; + for (bit = 0; bit < 16; bit++) + if ((mask & BIT(bit)) && !--i) + break; + return (base + bit + 1) * 4; +} + +/* + * Bytes an input or output DMA stream touches from its base: the last + * element of the last row, plane, depth slice and group. Interleave k packs + * k channels into each element, so a plane holds k channels (the measured + * matmul staging writes 128 channels interleaved 8 as 16 planes of 2 KiB, its + * exact scratch). The row, plane, depth and group strides follow the base; + * a zero height, channel, depth or group count is one only with a zero + * stride (ane_hx_dma_count()). + */ +static inline int ane_hx_dma_tile(const u32 *img, u32 base, u64 *extent) +{ + static const u8 bytes[] = { 1, 1, 2, 4 }; + bool dst = base == 0x5108; + u32 stride = base + 8, format = dst ? 0x5138 : base == 0x4d10 ? 0x4d68 : 0x4d6c; + u32 dim = dst ? 0x14 : 0x4, f = img[ane_hx_dma_index(format)], step; + u64 k = max(1U, (f >> 24) & 0xf), n, sum, term; + unsigned int i; + + sum = (u64)max(1U, img[ane_hx_dma_index(dim)]) * bytes[(f >> 12) & 3] * k; + for (i = 1; i < 5; i++) { + step = img[ane_hx_dma_index(stride + 4 * (i - 1))]; + if (ane_hx_dma_count(img[ane_hx_dma_index(i < 4 ? dim + 4 * i : 0x24)], step, &n)) + return -ENOEXEC; + if (i == 2) + n = DIV_ROUND_UP(n, k); + if (check_mul_overflow(n - 1, (u64)step, &term) || + check_add_overflow(sum, term, &sum)) + return -ENOEXEC; + } + *extent = sum; + return 0; +} + +/* + * Bytes the coefficient DMA reads from its base: the furthest of the 16 lanes + * (offset 0x5560 + 4i, size 0x55a0 + 4i, at least the per-channel aligned + * size 0x5504; an unset size is 64), then every group (stride 0x5518) and + * every output channel group (stride 0x551c) after the first. An output + * channel group is 16 << (common word 0x40 & 7) channels, 128 when that + * word is unset. As with a zero width, a lane of size 0 is refused rather + * than read as nothing, and a zero group or channel count is one only with + * a zero stride. + */ +static inline int ane_hx_dma_kernel(const u32 *img, u64 *extent) +{ + u32 group_stride = img[ane_hx_dma_index(0x5518)]; + u32 ocg_stride = img[ane_hx_dma_index(0x551c)]; + u64 lanes = 0, size, groups, ocgs, term; + unsigned int i; + + for (i = 0; i < 16; i++) { + size = max(img[ane_hx_dma_index(0x55a0 + 4 * i)], img[ane_hx_dma_index(0x5504)]); + if (!size) + return -ENOEXEC; + lanes = max(lanes, img[ane_hx_dma_index(0x5560 + 4 * i)] + size); + } + if (ane_hx_dma_count(img[ane_hx_dma_index(0x24)], group_stride, &groups) || + ane_hx_dma_count(img[ane_hx_dma_index(0x1c)], ocg_stride, &ocgs)) + return -ENOEXEC; + ocgs = DIV_ROUND_UP(ocgs, 16U << (img[ane_hx_dma_index(0x40)] & 7)); + if (check_mul_overflow(groups - 1, (u64)group_stride, &term) || + check_add_overflow(lanes, term, &lanes) || + check_mul_overflow(ocgs - 1, (u64)ocg_stride, &term) || + check_add_overflow(lanes, term, extent)) + return -ENOEXEC; + return 0; +} + +/* The five DMA bases, in the order of ane_hx_dma_state's arrays. */ +static const u16 ane_hx_dma_base_addr[] = { 0x4d10, 0x4d28, 0x5108, 0x5510, 0x5618 }; +#define ANE_HX_DMA_BASES ARRAY_SIZE(ane_hx_dma_base_addr) + +/* + * One task's register image, the image words it wrote, and each base's slot + * and offset as it wrote them. + */ +struct ane_hx_dma_state { + u32 img[ANE_HX_DMA_WORDS]; + DECLARE_BITMAP(set, ANE_HX_DMA_WORDS); + u64 offset[ANE_HX_DMA_BASES]; + u8 slot[ANE_HX_DMA_BASES]; + u8 written; /* bit b: base b was written */ +}; + +/* + * The reset values of the image: a task starts from them, and a program + * may leave out every word that holds its reset value. On the J775D's H17 a + * second task that leaves out the output base or a row stride runs with + * the reset value 0, and one that leaves out the widths does not run with + * the first task's. + */ +static inline void ane_hx_dma_defaults(struct ane_hx_dma_state *st) +{ + unsigned int i; + + memset(st, 0, sizeof(*st)); + st->img[ane_hx_dma_index(0x40)] = 3; + for (i = 0; i < 16; i++) + st->img[ane_hx_dma_index(0x55a0 + 4 * i)] = 64; +} + +/* + * Registers outside the common and DMA blocks that the bound does not model. + * The L2 (0x4100), planar engine (0x4500) and neural engine (0x4900) blocks + * configure on-chip buffers and arithmetic, and none of them can be + * relocated, so none carries a buffer address. 0x44d0 is never written; of + * the cache DMA block (0x5900), only the words the compiler writes, none of + * them an address, may be non-zero. + */ +static inline bool ane_hx_other_allowed(u32 addr) +{ + if (addr == 0x44d0) + return false; + if (addr >= 0x5900 && addr < 0x5934) + return addr == 0x5900 || addr == 0x5904 || addr == 0x5908 || + addr == 0x5918 || addr == 0x5928 || addr == 0x592c; + return true; +} + +/* + * Apply one task's records (bytes [rec, end) of @p) to @st, refusing the + * header forms the task stream never uses. A relocated record covering a + * base's [lo, hi] pair sets that base's slot and offset. Any other + * relocation names the slot 4 custom BAR and takes its value + * (ane_h16_custom_bars()), which this check does not see, so the + * configuration word must already enable its DMA: that value must not be + * able to turn a DMA on. + * A word written twice in one task must repeat its value, and a base its + * slot too, so no earlier write names anything unchecked; H17 programs + * repeat source 2's base unchanged. + */ +static inline int ane_hx_dma_apply(const u8 *p, size_t rec, size_t end, + struct ane_hx_dma_state *st) +{ + unsigned int i, b; + int index; + + while (rec < end) { + u32 h = get_unaligned_le32(p + rec), count = ane_hx_record_count(h); + bool reloc = !(h & BIT(31)) && (h & BIT(29)); + u32 slot = (h >> 23) & 0x3f, first = (h & 0x7fff) * 4; + + rec += 4; + if (count > (end - rec) / 4) + return -ENOEXEC; + /* Dense header bits 21, 22 and 30 are unused, the slot only names a relocation. */ + if (!(h & BIT(31)) && ((h & (BIT(21) | BIT(22) | BIT(30))) || (!reloc && slot))) + return -ENOEXEC; + if (reloc && count == 2 && ane_hx_dma_base(first) && !(first & 4)) { + u64 offset = get_unaligned_le32(p + rec) | + (u64)get_unaligned_le32(p + rec + 4) << 32; + + for (b = 0; ane_hx_dma_base_addr[b] != first; b++) + ; + if ((st->written & BIT(b)) && st->slot[b] != slot) + return -ENOEXEC; + st->offset[b] = offset; + st->slot[b] = slot; + st->written |= BIT(b); + } else if (reloc) { + if (slot != 4) + return -ENOEXEC; + for (i = 0; i < count; i++) + if (!ane_hx_dma_config(first + 4 * i) || + !ane_hx_dma_config_on(first + 4 * i, + get_unaligned_le32(p + rec + 4 * i))) + return -ENOEXEC; + } + for (i = 0; i < count; i++) { + u32 addr = ane_hx_record_addr(h, i); + u32 w = get_unaligned_le32(p + rec + 4 * i); + + if (!reloc && ane_hx_dma_base(addr)) + return -ENOEXEC; + index = ane_hx_dma_index(addr); + if (index < 0) { + if (w && !ane_hx_other_allowed(addr)) + return -ENOEXEC; + continue; + } + if (test_and_set_bit(index, st->set) && st->img[index] != w) + return -ENOEXEC; + st->img[index] = w; + } + rec += count * 4; + } + return 0; +} + +/* + * The DMA words of an image: only modelled registers, and no wrap, offset, + * crop or compression. Format bits other than the interleave, the element + * size and the bits every program sets are refused, as is an output channel + * group (common word 0x40 & 7) above 5, the largest the compiler writes. + */ +static inline int ane_hx_dma_image_check(const u32 *img) +{ + unsigned int i; + + for (i = 23; i < ANE_HX_DMA_WORDS; i++) { + u32 addr = i < 104 ? 0x4d00 + (i - 23) * 4 : i < 125 ? 0x5100 + (i - 104) * 4 : + 0x5500 + (i - 125) * 4; + + if (img[i] && !ane_hx_dma_modelled(addr)) + return -ENOEXEC; + } + if ((img[ane_hx_dma_index(0x4d08)] | img[ane_hx_dma_index(0x4d0c)]) & ~0xffU || + (img[ane_hx_dma_index(0x4d68)] | img[ane_hx_dma_index(0x4d6c)]) & ~0x0f003031U || + img[ane_hx_dma_index(0x5138)] & ~0x0f303031U || + (img[ane_hx_dma_index(0x40)] & 7) > 5) + return -ENOEXEC; + return 0; +} + +/* + * What one task enables it must also locate and size. Each DMA has its + * configuration words: source 1 0x4d00, source 2 0x4d04, the destination + * 0x5100, the 16 coefficient lanes 0x5520-0x555c and the lookup table's + * 0x5600-0x5614, which enable it as ane_hx_dma_config_on() says. An + * enabled DMA needs its relocated base in the same task, and an enabled + * input or output stream a width, height and channel count that are not + * zero. A zero width is not read as one: on the J775D's H17 a task without + * widths stalled the engine, and on H16 a matmul without them ran 256 wide. + * A zero depth or group count is one only with a zero stride, which every + * program that leaves them out has (ane_hx_dma_tile()). + */ +static inline int ane_hx_dma_enabled_check(const struct ane_hx_dma_state *st) +{ + static const struct { u16 config, last, dim; u8 base; } dma[] = { + { 0x4d00, 0x4d00, 0x4, 0 }, { 0x4d04, 0x4d04, 0x4, 1 }, + { 0x5100, 0x5100, 0x14, 2 }, { 0x5520, 0x555c, 0, 3 }, + { 0x5600, 0x5614, 0, 4 }, + }; + unsigned int i; + bool on; + u32 a; + + for (i = 0; i < ARRAY_SIZE(dma); i++) { + for (a = dma[i].config, on = false; a <= dma[i].last; a += 4) + if (ane_hx_dma_config_on(a, st->img[ane_hx_dma_index(a)])) + on = true; + if (!on) + continue; + if (!(st->written & BIT(dma[i].base))) + return -ENOEXEC; + for (a = dma[i].dim; dma[i].dim && a <= dma[i].dim + 8; a += 4) + if (!st->img[ane_hx_dma_index(a)]) + return -ENOEXEC; + } + return 0; +} + +/* + * Base @b of @st, with the extent its image gives, must lie inside the buffer + * bound to its slot: the end of the last 64-byte line it touches, + * ALIGN(offset + extent, 64), at most the buffer's size. The offset is + * unsigned: a negative one is larger than any buffer. + */ +static inline int ane_hx_dma_fits(const struct ane_hx_dma_state *st, unsigned int b, + const u64 bound[ANE_HX_DMA_SLOTS]) +{ + u32 base = ane_hx_dma_base_addr[b]; + u64 extent, last; + int ret = 0; + + if (base == 0x5510) + ret = ane_hx_dma_kernel(st->img, &extent); + else if (base == 0x5618) + extent = max(64U, st->img[ane_hx_dma_index(0x5614)]); + else + ret = ane_hx_dma_tile(st->img, base, &extent); + if (ret || !bound[st->slot[b]] || + check_add_overflow(st->offset[b], extent, &last) || + check_add_overflow(last, 63ULL, &last) || + (last & ~63ULL) > bound[st->slot[b]]) + return -ENOEXEC; + return 0; +} + +/* + * Bound every DMA base of an already validated stream with @header_words + * fixed words per task. @bound[s] is the size of the buffer the operation + * section binds to BAR slot s, 0 when it binds none. A base must be a + * relocated [lo, hi] record on a bound slot, and its offset plus the extent + * of its transfer must stay inside that buffer. + * + * Each task is checked on its own words over the reset values, which is the + * state the engine runs it from (see ane_hx_dma_defaults()). A word a task + * leaves out holds its reset value, whatever an earlier task wrote there, + * and a DMA the task does not enable reads and writes nothing. No base can + * reach a later task either: every DMA a task enables has its base written + * in that task (ane_hx_dma_enabled_check()). + */ +static inline int +ane_hx_task_stream_dma_validate(const void *text, size_t size, unsigned int header_words, + const u64 bound[ANE_HX_DMA_SLOTS]) +{ + struct ane_hx_dma_state *task; + struct ane_hx_task t; + const u8 *p = text; + size_t pos = 16; + unsigned int b; + int ret = 0; + + if (!p || !bound || size < 16 || size > ANE_H16_TASK_MAX || (size & 3)) + return -ENOEXEC; + task = kmalloc_obj(*task); + if (!task) + return -ENOMEM; + while (pos < size) { + ret = ane_hx_task_frame(p, size, pos, header_words, &t); + if (ret < 0) + break; + pos = t.next; + if (!ret) + continue; + ane_hx_dma_defaults(task); + ret = ane_hx_dma_apply(p, t.rec, t.end, task); + if (!ret) + ret = ane_hx_dma_image_check(task->img); + if (!ret) + ret = ane_hx_dma_enabled_check(task); + for (b = 0; !ret && b < ANE_HX_DMA_BASES; b++) + if (task->written & BIT(b)) + ret = ane_hx_dma_fits(task, b, bound); + if (ret) + break; + } + kfree(task); + return ret; +} + /* Section ordinal is supplied only by the validated complete section table. * This checks Mach-O relocation entries, not the distinct aperture records. */ diff --git a/drivers/accel/ane/ane_h16_tasks_test.c b/drivers/accel/ane/ane_h16_tasks_test.c index 2104e429185b7..5ca9a0d4f44c5 100644 --- a/drivers/accel/ane/ane_h16_tasks_test.c +++ b/drivers/accel/ane/ane_h16_tasks_test.c @@ -137,6 +137,661 @@ static void ane_h17_prologue_test(struct kunit *test) KUNIT_EXPECT_EQ(test, ane_h16_task_stream_validate(text, sizeof(text), 1), -ENOEXEC); } +/* + * One 16x16x128 fp16 add task as mil-hwx-compiler writes it: source 1 on BAR + * slot 6 and the destination on slot 7, each a relocated [lo, hi] record, + * rows of 0x40 and planes of 0x400 bytes, a source depth stride of 0. + * Returns the stream size. + */ +static size_t dma_task(u8 *text, u64 src, u64 dst) +{ + static const u32 dims[] = { 16, 16, 128, 0, 16, 16, 128 }; + size_t at = 16 + 9 * 4; + unsigned int i; + +#define PUT(v) do { put_unaligned_le32(v, text + at); at += 4; } while (0) + memset(text, 0, 256); + put_unaligned_le32(1, text); + PUT(0x4 / 4 | 6 << 15); + for (i = 0; i < ARRAY_SIZE(dims); i++) + PUT(dims[i]); + PUT(0x4d18 / 4 | 2 << 15); + PUT(0x40); + PUT(0x400); + PUT(0); + PUT(0x4d68 / 4); + PUT(0x1002031); + PUT(0x5110 / 4 | 1 << 15); + PUT(0x40); + PUT(0x400); + PUT(0x5138 / 4); + PUT(0x1302031); + PUT(0x4d10 / 4 | 1 << 15 | BIT(29) | 6 << 23); + PUT(lower_32_bits(src)); + PUT(upper_32_bits(src)); + PUT(0x5108 / 4 | 1 << 15 | BIT(29) | 7 << 23); + PUT(lower_32_bits(dst)); + PUT(upper_32_bits(dst)); + /* The cache-allocation word takes the slot 4 custom BAR value. */ + PUT(0x4d00 / 4 | BIT(29) | 4 << 23); + PUT(0x21); +#undef PUT + put_unaligned_le32((at - 16) / 4 << 16, text + 16); + return at; +} + +/* Record @n (0-based) of dma_task's one task. */ +static u8 *dma_record(u8 *text, unsigned int n) +{ + static const u8 words[] = { 8, 4, 2, 3, 2, 3, 3, 2 }; + size_t at = 16 + 9 * 4; + + while (n--) + at += words[n] * 4; + return text + at; +} + +static void ane_hx_dma_bound_test(struct kunit *test) +{ + u64 bound[ANE_HX_DMA_SLOTS] = { [6] = 0x20000, [7] = 0x20000 }; + u8 text[256]; + size_t size; + + /* 15 rows of 0x40, 127 planes of 0x400 and one 32-byte row: exactly 128 KiB. */ + size = dma_task(text, 0, 0); + KUNIT_ASSERT_EQ(test, ane_h16_task_stream_validate(text, size, 1), 0); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + size = dma_task(text, 0x40, 0); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + size = dma_task(text, 0, 0x20000); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + size = dma_task(text, 0, -0x70000); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + size = dma_task(text, -1, 0); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* A larger buffer takes the same offset. */ + bound[7] = 0x40000; + size = dma_task(text, 0, 0x20000); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + + /* A slot the operation section does not bind. */ + bound[7] = 0; + size = dma_task(text, 0, 0); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + bound[7] = 0x20000; + + /* A base without relocation, or relocated on the custom BAR slot. */ + put_unaligned_le32(0x4d10 / 4 | 1 << 15, dma_record(text, 5)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(0x4d10 / 4 | 1 << 15 | BIT(29) | 4 << 23, dma_record(text, 5)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* A relocated record that is not exactly the [lo, hi] pair. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0x4d0c / 4 | 1 << 15 | BIT(29) | 6 << 23, dma_record(text, 5)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* A scatter write of the hi word. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(BIT(31) | BIT(15) | 0x5108 / 4, dma_record(text, 6)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* A relocation on a stride, and an unused header bit. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0x4d18 / 4 | 1 << 15 | BIT(29) | 6 << 23, dma_record(text, 1)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(0x4d18 / 4 | 1 << 15 | BIT(30), dma_record(text, 1)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + + /* One byte more of plane stride crosses a line past the end. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0x401, dma_record(text, 1) + 8); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* Rows, planes and depth slices whose sum wraps 64 bits to 32 bytes. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0x80000001, dma_record(text, 0) + 8); + put_unaligned_le32(0x80000001, dma_record(text, 0) + 12); + put_unaligned_le32(3, dma_record(text, 0) + 16); + put_unaligned_le32(U32_MAX, dma_record(text, 1) + 4); + put_unaligned_le32(U32_MAX, dma_record(text, 1) + 8); + put_unaligned_le32(0x80000000, dma_record(text, 1) + 12); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* A source 1 metadata address, which the bound does not model. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0x4d40 / 4, dma_record(text, 2)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* A wrap configuration beside the cache hint. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0x4d08 / 4, dma_record(text, 2)); + put_unaligned_le32(0x1ce, dma_record(text, 2) + 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(0xce, dma_record(text, 2) + 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); +} + +/* Record headers: @n dense words from @addr, and a relocated [lo, hi] pair on @slot. */ +#define DMA_REC(addr, n) ((addr) / 4 | ((n) - 1) << 15) +#define DMA_RELOC(addr, slot) (DMA_REC(addr, 2) | BIT(29) | (slot) << 23) + +/* + * Parts of dma_task's one task, for streams of several tasks: its geometry + * (dimensions with @rows source rows, strides with @plane source plane + * stride, formats), its two bases (source 1 at @src, the destination at 0) + * and the source 1 configuration word that enables it. + */ +enum { DMA_GEOMETRY = 1, DMA_BASES = 2, DMA_ENABLE = 4, DMA_EXTRA = 8 }; + +/* Append one task of @parts, then @n more words of @extra; returns the new end. */ +static size_t dma_append(u8 *text, size_t at, unsigned int parts, u32 rows, u32 plane, + u64 src, const u32 *extra, unsigned int n) +{ + const u32 geometry[] = { + DMA_REC(0x4, 7), 16, rows, 128, 0, 16, 16, 128, + DMA_REC(0x4d18, 3), 0x40, plane, 0, DMA_REC(0x4d68, 1), 0x1002031, + DMA_REC(0x5110, 2), 0x40, 0x400, DMA_REC(0x5138, 1), 0x1302031, + }; + const u32 bases[] = { + DMA_RELOC(0x4d10, 6), lower_32_bits(src), upper_32_bits(src), + DMA_RELOC(0x5108, 7), 0, 0, + }; + const u32 enable[] = { DMA_REC(0x4d00, 1) | BIT(29) | 4 << 23, 0x21 }; + const struct { unsigned int part; const u32 *w; unsigned int n; } part[] = { + { DMA_GEOMETRY, geometry, ARRAY_SIZE(geometry) }, + { DMA_BASES, bases, ARRAY_SIZE(bases) }, + { DMA_ENABLE, enable, ARRAY_SIZE(enable) }, + { DMA_EXTRA, extra, n }, + }; + size_t task = at; + unsigned int k, i; + + parts |= DMA_EXTRA; + memset(text + at, 0, 9 * 4); + at += 9 * 4; + for (k = 0; k < ARRAY_SIZE(part); k++) + for (i = 0; (parts & part[k].part) && i < part[k].n; i++, at += 4) + put_unaligned_le32(part[k].w[i], text + at); + put_unaligned_le32((at - task) / 4 << 16, text + task); + return ALIGN(at, 16); +} + +/* A stream of one complete task from dma_append() with @n words of @extra. */ +static size_t dma_one(u8 *text, u32 plane, const u32 *extra, unsigned int n) +{ + memset(text, 0, 512); + put_unaligned_le32(1, text); + return dma_append(text, 16, 7, 16, plane, 0, extra, n); +} + +/* A stream of two tasks, @first then @second, from dma_append(). */ +static size_t dma_two(u8 *text, unsigned int first, unsigned int second, u32 plane) +{ + size_t at; + + memset(text, 0, 512); + put_unaligned_le32(1, text); + at = dma_append(text, 16, first, 16, 0x400, 0, NULL, 0); + return dma_append(text, at, second, 16, plane, 0, NULL, 0); +} + +/* + * Each task runs from the reset values, so each is checked on its own words + * over them, and every DMA a task enables has its base in that task. A word + * a task leaves out holds its reset value, whatever an earlier task wrote. + */ +static void ane_hx_dma_reset_test(struct kunit *test) +{ + u64 bound[ANE_HX_DMA_SLOTS] = { [6] = 0x20000, [7] = 0x20000 }; + const u32 groups[] = { DMA_REC(0x24, 1), 2, DMA_REC(0x4d24, 1), 0x20000 }; + const u32 dst_only[] = { + DMA_REC(0x4, 7), 16, 32, 128, 0, 16, 16, 128, + DMA_REC(0x5110, 2), 0x40, 0x400, DMA_REC(0x5138, 1), 0x1302031, + DMA_RELOC(0x5108, 7), 0, 0, + }; + u8 text[512]; + size_t size; + + /* Both tasks complete and in range. */ + size = dma_two(text, 7, 7, 0x400); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + /* + * Task N: geometry with no base; task N+1: the bases and the enable + * alone, which enable source 1 with no size of its own. + */ + size = dma_two(text, DMA_GEOMETRY, DMA_BASES | DMA_ENABLE, 0x400); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* N+1 with larger planes and nothing enabled moves nothing: N's base does not reach it. */ + size = dma_two(text, 7, DMA_GEOMETRY, 0x800); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + /* ... but enabling source 1 there without its base is refused, */ + size = dma_two(text, 7, DMA_GEOMETRY | DMA_ENABLE, 0x800); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* ... and so is its base with those planes. */ + size = dma_two(text, 7, 7, 0x800); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + + /* + * An L2-resident task: N+1 takes its input from L2 and writes only the + * destination, with 32 rows. Source 1 is not enabled in N+1, so N's + * source base is not read with N+1's rows (which would end past 128 KiB). + */ + memset(text, 0, 512); + put_unaligned_le32(1, text); + size = dma_append(text, 16, 7, 16, 0x400, 0, NULL, 0); + size = dma_append(text, size, 0, 0, 0, 0, dst_only, ARRAY_SIZE(dst_only)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + + /* + * A word left at its reset value after an earlier task set it: N reads + * two groups 128 KiB apart from a 256 KiB source; N+1 leaves the group + * count and stride out and reads one group from 128 KiB on. + */ + bound[6] = 0x40000; + memset(text, 0, 512); + put_unaligned_le32(1, text); + size = dma_append(text, 16, 7, 16, 0x400, 0, groups, ARRAY_SIZE(groups)); + size = dma_append(text, size, 7, 16, 0x400, 0x20000, NULL, 0); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + /* N+1 writing the two groups itself reaches 128 KiB past the source. */ + memset(text, 0, 512); + put_unaligned_le32(1, text); + size = dma_append(text, 16, 7, 16, 0x400, 0, groups, ARRAY_SIZE(groups)); + size = dma_append(text, size, 7, 16, 0x400, 0x20000, groups, ARRAY_SIZE(groups)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); +} + +/* + * A base written twice in one task must repeat its slot and offset: an + * out-of-range first write followed by an in-range one is refused, as is + * a second write on another slot. H17 programs repeat 0x4d28 unchanged. + */ +static void ane_hx_dma_rewrite_test(struct kunit *test) +{ + u64 bound[ANE_HX_DMA_SLOTS] = { [6] = 0x20000, [7] = 0x20000, [8] = 0x20000 }; + u32 again[] = { DMA_RELOC(0x4d10, 6), 0, 0 }; + u8 text[512]; + size_t size; + + memset(text, 0, 512); + put_unaligned_le32(1, text); + size = dma_append(text, 16, 7, 16, 0x400, 0, again, ARRAY_SIZE(again)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + memset(text, 0, 512); + put_unaligned_le32(1, text); + size = dma_append(text, 16, 7, 16, 0x400, 0x20000, again, ARRAY_SIZE(again)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + again[0] = DMA_RELOC(0x4d10, 8); + memset(text, 0, 512); + put_unaligned_le32(1, text); + size = dma_append(text, 16, 7, 16, 0x400, 0, again, ARRAY_SIZE(again)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); +} + +/* + * A task that enables a DMA must locate it and give it a real size; the + * channel group, the format bits and the registers outside the modelled + * blocks stay within what the compiler writes. + */ +static void ane_hx_dma_strict_test(struct kunit *test) +{ + u64 bound[ANE_HX_DMA_SLOTS] = { [6] = 0x20000, [7] = 0x20000 }; + u8 text[256]; + size_t size; + + /* The destination enabled: the cache word (record 7) becomes its configuration 0x5100. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0x5100 / 4, dma_record(text, 7)); + put_unaligned_le32(0x1000021, dma_record(text, 7) + 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + /* ... with a zero channel count, */ + put_unaligned_le32(0, dma_record(text, 0) + 28); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(128, dma_record(text, 0) + 28); + /* ... and without its base: record 6 rewrites the destination strides instead. */ + put_unaligned_le32(0x5110 / 4 | 1 << 15, dma_record(text, 6)); + put_unaligned_le32(0x40, dma_record(text, 6) + 4); + put_unaligned_le32(0x400, dma_record(text, 6) + 8); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* A zero channel count on an enabled source. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0, dma_record(text, 0) + 12); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* + * A zero width on an enabled stream: the J775D's H17 stalled on one and + * an M4 Pro ran one 256 wide, so it is not read as one. + */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0, dma_record(text, 0) + 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* The destination's width matters once the destination is enabled. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0, dma_record(text, 0) + 20); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + put_unaligned_le32(0x5100 / 4, dma_record(text, 7)); + put_unaligned_le32(0x1000021, dma_record(text, 7) + 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(16, dma_record(text, 0) + 20); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + /* An output channel group of 6, then 5: common word 0x40 in place of the source format. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0x40 / 4, dma_record(text, 2)); + put_unaligned_le32(6, dma_record(text, 2) + 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(5, dma_record(text, 2) + 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + /* A source format bit the compiler never sets. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0x1002033, dma_record(text, 2) + 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* 0x44d0, and a cache DMA word the compiler never writes, in place of the source format. */ + size = dma_task(text, 0, 0); + put_unaligned_le32(0x44d0 / 4, dma_record(text, 2)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(0x590c / 4, dma_record(text, 2)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(0x5904 / 4, dma_record(text, 2)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); +} + +/* + * The end of a transfer is the end of the last 64-byte line it touches: a + * 64-byte lookup table at offset 1 reads [0, 128). + */ +static void ane_hx_dma_align_test(struct kunit *test) +{ + u64 bound[ANE_HX_DMA_SLOTS] = { [6] = 100 }; + u8 text[64] = {}; + size_t at = 16 + 9 * 4; + + put_unaligned_le32(1, text); + put_unaligned_le32(0x5618 / 4 | 1 << 15 | BIT(29) | 6 << 23, text + at); + put_unaligned_le32(1, text + at + 4); + put_unaligned_le32(0, text + at + 8); + at += 12; + put_unaligned_le32((at - 16) / 4 << 16, text + 16); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, at, 9, bound), -ENOEXEC); + bound[6] = 128; + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, at, 9, bound), 0); + /* Offset 0x41: the line at 0x40 through the one at 0x80. */ + put_unaligned_le32(0x41, text + 16 + 9 * 4 + 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, at, 9, bound), -ENOEXEC); +} + +/* + * Coefficient DMA: 16 lanes of 0x80 bytes at 0x80 apart from a base on slot + * 8, then a second output channel group 0x800 on (256 channels, 128 per + * group). + */ +static void ane_hx_dma_kernel_test(struct kunit *test) +{ + u64 bound[ANE_HX_DMA_SLOTS] = { [8] = 0x1000 }; + u8 text[512] = {}; + size_t at = 16 + 9 * 4; + unsigned int i; + +#define PUT(v) do { put_unaligned_le32(v, text + at); at += 4; } while (0) + put_unaligned_le32(1, text); + PUT(0x1c / 4); + PUT(256); + PUT(0x551c / 4); + PUT(0x800); + PUT(0x5560 / 4 | 31 << 15); + for (i = 0; i < 16; i++) + PUT(i * 0x80); + for (i = 0; i < 16; i++) + PUT(0x80); + PUT(0x5510 / 4 | 1 << 15 | BIT(29) | 8 << 23); + PUT(0); + PUT(0); +#undef PUT + put_unaligned_le32((at - 16) / 4 << 16, text + 16); + KUNIT_ASSERT_EQ(test, ane_h16_task_stream_validate(text, at, 1), 0); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, at, 9, bound), 0); + bound[8] = 0xfc0; + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, at, 9, bound), -ENOEXEC); + /* 3 at 0x40 is the default 128-channel group, 2 a 64-channel one: four groups. */ + bound[8] = 0x1000; + put_unaligned_le32(0x40 / 4, text + at); + put_unaligned_le32(2, text + at + 4); + put_unaligned_le32((at + 8 - 16) / 4 << 16, text + 16); + KUNIT_ASSERT_EQ(test, ane_h16_task_stream_validate(text, at + 8, 1), 0); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, at + 8, 9, bound), -ENOEXEC); + bound[8] = 0x2000; + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, at + 8, 9, bound), 0); +} + +/* + * A matmul's coefficient DMA from slot 3: 16 lanes of 0x1000 bytes 0x1000 + * apart, 256 output channels in two groups of 128 (common word 0x40 unset) + * @ocgs apart, the lane configuration words set to @lane (none when 0) and + * the base written when @base. + */ +static size_t dma_kernel(u8 *text, u32 lane, bool base, u32 ocgs) +{ + size_t at = 16 + 9 * 4; + unsigned int i; + + memset(text, 0, 512); + put_unaligned_le32(1, text); +#define PUT(v) do { put_unaligned_le32(v, text + at); at += 4; } while (0) + PUT(DMA_REC(0x1c, 1)); + PUT(256); + PUT(DMA_REC(0x3c, 1)); + PUT(0x20200004); + PUT(DMA_REC(0x551c, 1)); + PUT(ocgs); + PUT(DMA_REC(0x5560, 32)); + for (i = 0; i < 16; i++) + PUT(i * 0x1000); + for (i = 0; i < 16; i++) + PUT(0x1000); + if (lane) { + /* Enabled lanes take the custom BAR value; idle ones are not relocated. */ + PUT(DMA_REC(0x5520, 16) | ((lane & 0xffff) == 0x20 ? 0 : BIT(29) | 4 << 23)); + for (i = 0; i < 16; i++) + PUT(lane); + } + if (base) { + PUT(DMA_RELOC(0x5510, 3)); + PUT(0); + PUT(0); + } +#undef PUT + put_unaligned_le32((at - 16) / 4 << 16, text + 16); + return at; +} + +/* + * The coefficient DMA is held to the same rule as the others: lanes that + * enable it need its base in the task. A lookup-table task writes its lanes + * idle (0x20 in the low half) and fetches nothing. The second + * output channel group of 128 x 256 fp16 coefficients starts 0x10000 into + * them; a group stride of 0x40000 reads 0x30000 past a 0x20000 buffer. + */ +static void ane_hx_dma_kernel_enable_test(struct kunit *test) +{ + u64 bound[ANE_HX_DMA_SLOTS] = { [3] = 0x20000 }; + u8 text[512]; + size_t size; + + size = dma_kernel(text, 0x10041, true, 0x10000); + KUNIT_ASSERT_EQ(test, ane_h16_task_stream_validate(text, size, 1), 0); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + size = dma_kernel(text, 0x10041, true, 0x40000); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* Enabled lanes without a base. */ + size = dma_kernel(text, 0x10041, false, 0x10000); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* Idle lanes without a base fetch nothing; any other lane word enables the DMA. */ + size = dma_kernel(text, 0x50020, false, 0x40000); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + size = dma_kernel(text, 0x50022, false, 0x40000); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + size = dma_kernel(text, 0x50120, false, 0x40000); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* A base written beside idle lanes is still bounded. */ + size = dma_kernel(text, 0x50020, true, 0x40000); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + /* Idle lanes that take the custom BAR value could become enabled lanes. */ + size = dma_kernel(text, 0x50020, false, 0x10000); + put_unaligned_le32(get_unaligned_le32(text + size - 17 * 4) | BIT(29) | 4 << 23, + text + size - 17 * 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); +} + +/* + * Counts the bound cannot read as one, and a lane that reads nothing. In + * dma_kernel()'s task, 52, 60 and 68 hold the records of the output channel + * count, common word 0x3c and the output channel group stride; 80 + 4i + * holds lane i's offset and 144 + 4i its size. + */ +static void ane_hx_dma_kernel_count_test(struct kunit *test) +{ + u64 bound[ANE_HX_DMA_SLOTS] = { [3] = 0x20000 }; + u8 text[512]; + size_t size; + + /* No group count with a group stride, then two groups 0x1000 apart. */ + size = dma_kernel(text, 0x10041, true, 0x10000); + put_unaligned_le32(DMA_REC(0x5518, 1), text + 60); + put_unaligned_le32(0x1000, text + 64); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(DMA_REC(0x24, 1), text + 68); + put_unaligned_le32(2, text + 72); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + /* No output channel count with an output channel group stride. */ + size = dma_kernel(text, 0x10041, true, 0x10000); + put_unaligned_le32(0, text + 56); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + size = dma_kernel(text, 0x10041, true, 0); + put_unaligned_le32(0, text + 56); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + /* A lane of size 0 at the end of the buffer, then one 0x5504 sizes. */ + size = dma_kernel(text, 0x10041, true, 0); + put_unaligned_le32(0x20000, text + 80 + 15 * 4); + put_unaligned_le32(0, text + 144 + 15 * 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(DMA_REC(0x5504, 1), text + 60); + put_unaligned_le32(0x80, text + 64); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(0x1f000, text + 80 + 15 * 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); +} + +/* + * A zero depth or group count on a source or the destination is one only + * with a zero stride. dma_task()'s source has depth 0 and writes its depth + * stride at record 1 + 12. + */ +static void ane_hx_dma_count_test(struct kunit *test) +{ + u64 bound[ANE_HX_DMA_SLOTS] = { [6] = 0x20000, [7] = 0x20000 }; + static const u32 dst_depth[] = { DMA_REC(0x5118, 1), 0x400 }; + static const u32 dst_depths[] = { DMA_REC(0x5118, 1), 0x400, DMA_REC(0x20, 1), 1 }; + static const u32 src_group[] = { DMA_REC(0x4d24, 1), 0x20000 }; + static const u32 dst_group[] = { DMA_REC(0x511c, 1), 0x20000 }; + static const u32 src_groups[] = { DMA_REC(0x24, 1), 1, DMA_REC(0x4d24, 1), 0x20000 }; + u8 text[512]; + size_t size; + + size = dma_task(text, 0, 0); + put_unaligned_le32(0x400, dma_record(text, 1) + 12); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + put_unaligned_le32(1, dma_record(text, 0) + 16); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + put_unaligned_le32(2, dma_record(text, 0) + 16); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + + size = dma_one(text, 0x400, dst_depth, ARRAY_SIZE(dst_depth)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + size = dma_one(text, 0x400, dst_depths, ARRAY_SIZE(dst_depths)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + size = dma_one(text, 0x400, src_group, ARRAY_SIZE(src_group)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + size = dma_one(text, 0x400, dst_group, ARRAY_SIZE(dst_group)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + size = dma_one(text, 0x400, src_groups, ARRAY_SIZE(src_groups)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); +} + +/* + * A word written twice in one task repeats its value: a plane stride that + * would end past the source, then one that would not, is refused. A + * configuration word on the custom BAR must already enable its DMA. + */ +static void ane_hx_dma_repeat_test(struct kunit *test) +{ + u64 bound[ANE_HX_DMA_SLOTS] = { [6] = 0x20000, [7] = 0x20000 }; + static const u32 plane[] = { DMA_REC(0x4d1c, 1), 0x400 }; + u8 text[512]; + size_t size; + + size = dma_one(text, 0x400, plane, ARRAY_SIZE(plane)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), 0); + size = dma_one(text, 0x800, plane, ARRAY_SIZE(plane)); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); + + size = dma_task(text, 0, 0); + put_unaligned_le32(0, dma_record(text, 7) + 4); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 9, bound), -ENOEXEC); +} + +/* + * One H15 task: eight header words (nine when @extra sets the last one's + * low bits), then a source 1 row stride of @row, three L2 records and + * dma_task()'s geometry, bases and enable. Read with nine header words, the + * row stride record would be a header word and the L2 records would hide + * it, so the stride goes unchecked. + */ +static size_t dma_h15(u8 *text, u32 row, bool extra) +{ + static const u32 tail[] = { + DMA_REC(0x4104, 1), DMA_REC(0x4108, 2), DMA_REC(0x410c, 1), 0, + DMA_REC(0x4, 7), 16, 16, 128, 0, 16, 16, 128, + DMA_REC(0x4d1c, 2), 0x400, 0, DMA_REC(0x4d68, 1), 0x1002031, + DMA_REC(0x5110, 2), 0x40, 0x400, DMA_REC(0x5138, 1), 0x1302031, + DMA_RELOC(0x4d10, 6), 0, 0, DMA_RELOC(0x5108, 7), 0, 0, + DMA_REC(0x4d00, 1) | BIT(29) | 4 << 23, 0x21, + }; + size_t at = 16 + 8 * 4; + unsigned int i; + + memset(text, 0, 256); + put_unaligned_le32(1, text); + if (extra) { + put_unaligned_le32(3, text + at - 4); + at += 4; + } + put_unaligned_le32(DMA_REC(0x4d18, 1), text + at); + put_unaligned_le32(row, text + at + 4); + at += 8; + for (i = 0; i < ARRAY_SIZE(tail); i++, at += 4) + put_unaligned_le32(tail[i], text + at); + put_unaligned_le32((at - 16) / 4 << 16, text + 16); + return at; +} + +/* The DMA bound walks a stream with its target's header words, as the structural gate does. */ +static void ane_hx_dma_h15_test(struct kunit *test) +{ + u64 bound[ANE_HX_DMA_SLOTS] = { [6] = 0x20000, [7] = 0x20000 }; + u8 text[256]; + size_t size; + bool extra; + + for (extra = false; ; extra = true) { + size = dma_h15(text, 0x40, extra); + KUNIT_ASSERT_EQ(test, ane_hx_task_stream_validate_hdr(text, size, 1, 1, 8, + NULL, NULL), 0); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 8, bound), 0); + /* 15 rows of 0x1040 end past the source. */ + size = dma_h15(text, 0x1040, extra); + KUNIT_ASSERT_EQ(test, ane_hx_task_stream_validate_hdr(text, size, 1, 1, 8, + NULL, NULL), 0); + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 8, bound), + -ENOEXEC); + if (extra) + break; + } + KUNIT_EXPECT_EQ(test, ane_hx_task_stream_dma_validate(text, size, 0, bound), -ENOEXEC); +} + static struct kunit_case ane_h16_task_cases[] = { KUNIT_CASE(ane_h16_prologue_test), KUNIT_CASE(ane_h17_prologue_test), @@ -144,6 +799,17 @@ static struct kunit_case ane_h16_task_cases[] = { KUNIT_CASE(ane_h16_scatter_task_test), KUNIT_CASE(ane_h16_extra_header_test), KUNIT_CASE(ane_h16_relocation_test), + KUNIT_CASE(ane_hx_dma_bound_test), + KUNIT_CASE(ane_hx_dma_kernel_test), + KUNIT_CASE(ane_hx_dma_kernel_enable_test), + KUNIT_CASE(ane_hx_dma_kernel_count_test), + KUNIT_CASE(ane_hx_dma_count_test), + KUNIT_CASE(ane_hx_dma_repeat_test), + KUNIT_CASE(ane_hx_dma_h15_test), + KUNIT_CASE(ane_hx_dma_reset_test), + KUNIT_CASE(ane_hx_dma_rewrite_test), + KUNIT_CASE(ane_hx_dma_strict_test), + KUNIT_CASE(ane_hx_dma_align_test), {} }; @@ -153,5 +819,5 @@ static struct kunit_suite ane_h16_task_suite = { }; kunit_test_suite(ane_h16_task_suite); -MODULE_DESCRIPTION("KUnit tests for H16 task and relocation validation"); +MODULE_DESCRIPTION("KUnit tests for H16 task, relocation and DMA base validation"); MODULE_LICENSE("Dual MIT/GPL");