Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 76 additions & 25 deletions drivers/accel/ane/ane_fw_transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,9 @@ ane_fw_transport_free(struct ane_fw_transport *t, void *slot, u64 dva, u64 bytes
* firmware frees it after UNLOAD (often only during a later program), but the
* backing stays mapped until reset: without reuse the 12 MiB quota ran out at
* the sixth program of a boot. Only memory the firmware itself gave back by
* FREE is served again, and only for the same tag and size.
* FREE, or that the host reserved for the request
* (ane_fw_transport_shared_reserve()), is served again, and only for the
* same tag and size.
*/
static inline unsigned int
ane_fw_shared_reusable(const struct ane_fw_transport *t, size_t size, u64 tag)
Expand All @@ -737,11 +739,80 @@ ane_fw_shared_reusable(const struct ane_fw_transport *t, size_t size, u64 tag)
return t->shared_count;
}

/*
* A new shared allocation of size bytes under tag, within the host's caps
* (ANE_FW_SHARED_MAX entries, ANE_FW_SHARED_TOTAL bytes): its index, live.
*/
static inline int ane_fw_shared_add(struct ane_fw_transport *t, size_t size, u64 tag,
unsigned int *index)
{
struct ane_fw_surface s = {};
unsigned int i;
int ret;

if (t->shared_count == ANE_FW_SHARED_MAX || size > ANE_FW_SHARED_TOTAL -
t->shared_bytes)
return -ENOSPC;
ret = t->ops->alloc(t->ctx, size, tag, &s);
if (ret)
return ret;
if (!ane_fw_surface_valid(&s) || !IS_ALIGNED(s.dva, SZ_16K) ||
s.size != size || ane_fw_surfaces_overlap(&s, &t->ipc) ||
ane_fw_surfaces_overlap(&s, &t->pool))
return -EPROTO;
for (i = 0; i < t->shared_count; i++)
if (ane_fw_surfaces_overlap(&s, &t->shared[i]))
return -EPROTO;
*index = t->shared_count;
t->shared_tags[t->shared_count] = tag;
t->shared_retired[t->shared_count] = false;
t->shared[t->shared_count++] = s;
t->shared_bytes += size;
return 0;
}

/*
* Make sure a later SHAREDMALLOC request of size bytes under tag is served
* from memory the host already has: a retired allocation of that tag and
* size, or a new one made now and kept retired for it. The firmware waits
* for the answer to every request, with its shared-memory lock held, and a
* request the host cannot serve is never answered: the command behind it
* stalls and only a power cycle recovers the engine. A request the host
* knows is coming is therefore taken before the command that causes it, so
* running out of memory or quota (-ENOMEM, -ENOSPC) refuses that command
* before the firmware sees it.
*/
static inline int ane_fw_transport_shared_reserve(struct ane_fw_transport *t, size_t size,
u32 tag)
{
unsigned int i;
int ret;

if (t->error)
return t->error;
if (!size || !IS_ALIGNED(size, SZ_16K))
return -EINVAL;
if (ane_fw_shared_reusable(t, size, tag) < t->shared_count)
return 0;
ret = ane_fw_shared_add(t, size, tag, &i);
/*
* An invalid surface from the backend is a host error, as it is for a
* request (ane_fw_transport_shared()): its mapping is not recorded, so
* retrying would map another one at every LOAD.
*/
if (ret == -EPROTO)
return ane_fw_transport_fail(t, ret);
if (ret)
return ret;
t->shared_retired[i] = true;
ane_fw_shared_note(t, "reserve", i);
return 0;
}

static inline int ane_fw_transport_irq(struct ane_fw_transport *t, u32 *pending);

static inline int ane_fw_transport_shared(struct ane_fw_transport *t)
{
struct ane_fw_surface s = {};
u64 word0, word1, word2;
size_t size;
unsigned int i;
Expand Down Expand Up @@ -801,33 +872,13 @@ static inline int ane_fw_transport_shared(struct ane_fw_transport *t)
ane_fw_shared_note(t, "reuse", i);
return 0;
}
if (t->shared_count == ANE_FW_SHARED_MAX || size > ANE_FW_SHARED_TOTAL -
t->shared_bytes) {
ret = -ENOSPC;
goto refused;
}
ret = t->ops->alloc(t->ctx, size, word2, &s);
ret = ane_fw_shared_add(t, size, word2, &i);
if (ret)
goto refused;
if (!ane_fw_surface_valid(&s) || !IS_ALIGNED(s.dva, SZ_16K) ||
s.size != size || ane_fw_surfaces_overlap(&s, &t->ipc) ||
ane_fw_surfaces_overlap(&s, &t->pool)) {
ret = -EPROTO;
goto refused;
}
for (i = 0; i < t->shared_count; i++) {
if (ane_fw_surfaces_overlap(&s, &t->shared[i])) {
ret = -EPROTO;
goto refused;
}
}
t->shared_tags[t->shared_count] = word2;
t->shared[t->shared_count++] = s;
t->shared_bytes += size;
t->ops->store64(t->ctx, p + 8, 0);
t->ops->store64(t->ctx, p + 16, t->shared_count);
t->ops->store64(t->ctx, p + 16, i + 1);
t->ops->write_barrier(t->ctx);
t->ops->store64(t->ctx, p, s.dva | 1);
t->ops->store64(t->ctx, p, t->shared[i].dva | 1);
t->ops->write_barrier(t->ctx);
ane_fw_transport_doorbell(t, ANE_FW_SHAREDMALLOC);
ane_fw_transport_advance(t, ANE_FW_SHAREDMALLOC);
Expand Down
83 changes: 83 additions & 0 deletions drivers/accel/ane/ane_fw_transport_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,87 @@ static void ane_transport_pool_no_free_test(struct kunit *test)
KUNIT_EXPECT_EQ(test, ane_fw_transport_shared(&f->t), -ENOSPC);
}

/*
* A reserved allocation is kept retired and serves the request it was made
* for, with no allocation then; a full quota or no memory refuses the
* reservation without failing the transport or touching the ring.
*/
static void ane_transport_shared_reserve_test(struct kunit *test)
{
struct ane_transport_test *f = test->priv;
unsigned int i;
u64 dva;

KUNIT_ASSERT_EQ(test, ane_fw_transport_arm(&f->t), 0);
KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, 1, ANE_FW_SHARED_EXPP),
-EINVAL);
KUNIT_ASSERT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP),
0);
KUNIT_EXPECT_EQ(test, f->allocs, 1U);
KUNIT_EXPECT_TRUE(test, f->t.shared_retired[0]);
dva = f->t.shared[0].dva;
/* Already there: no second allocation. */
KUNIT_ASSERT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP),
0);
KUNIT_EXPECT_EQ(test, f->allocs, 1U);
ane_test_shared_request(f, 0, SZ_2M, ANE_FW_SHARED_EXPP);
KUNIT_ASSERT_EQ(test, ane_fw_transport_shared(&f->t), 0);
KUNIT_EXPECT_EQ(test, f->allocs, 1U);
KUNIT_EXPECT_FALSE(test, f->t.shared_retired[0]);
KUNIT_EXPECT_EQ(test, ane_test_load(f, ane_test_slot(f, ANE_FW_SHAREDMALLOC) -
ANE_FW_SLOT_SIZE), dva | 1);

/* No memory: refused, and nothing recorded. */
f->alloc_error = -ENOMEM;
KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP),
-ENOMEM);
KUNIT_EXPECT_EQ(test, f->t.shared_count, 1U);
KUNIT_EXPECT_EQ(test, f->t.error, 0);
f->alloc_error = 0;
f->allocs = 1;

/* Fill the quota with live pools; one more is refused cleanly. */
for (i = 1; i < ANE_FW_SHARED_TOTAL / SZ_2M; i++) {
KUNIT_ASSERT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M,
ANE_FW_SHARED_EXPP), 0);
ane_test_shared_request(f, 0, SZ_2M, ANE_FW_SHARED_EXPP);
KUNIT_ASSERT_EQ(test, ane_fw_transport_shared(&f->t), 0);
}
KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP),
-ENOSPC);
KUNIT_EXPECT_EQ(test, f->t.error, 0);
KUNIT_EXPECT_EQ(test, f->refusals, 0U);

/* A FREE gives one back, which serves the next reservation. */
f->t.native_free = true;
ane_test_shared_request(f, dva, 0, 0);
KUNIT_ASSERT_EQ(test, ane_fw_transport_shared(&f->t), 0);
KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP),
0);
KUNIT_EXPECT_EQ(test, f->allocs, (unsigned int)(ANE_FW_SHARED_TOTAL / SZ_2M));
KUNIT_EXPECT_EQ(test, f->t.error, 0);
}

/*
* A backend surface that fails validation fails the transport, as it does for
* a request, so a later LOAD does not map another one.
*/
static void ane_transport_shared_reserve_invalid_test(struct kunit *test)
{
struct ane_transport_test *f = test->priv;

KUNIT_ASSERT_EQ(test, ane_fw_transport_arm(&f->t), 0);
f->alloc_bad = 1;
KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP),
-EPROTO);
KUNIT_EXPECT_EQ(test, f->t.error, -EPROTO);
KUNIT_EXPECT_EQ(test, f->t.shared_count, 0U);
KUNIT_EXPECT_EQ(test, f->t.shared_bytes, 0UL);
KUNIT_EXPECT_EQ(test, ane_fw_transport_shared_reserve(&f->t, SZ_2M, ANE_FW_SHARED_EXPP),
-EPROTO);
KUNIT_EXPECT_EQ(test, f->allocs, 1U);
}

/* A FREE posted after UNLOAD is answered by the settle loop. */
static void ane_transport_settle_test(struct kunit *test)
{
Expand Down Expand Up @@ -1550,6 +1631,8 @@ static struct kunit_case ane_transport_cases[] = {
KUNIT_CASE(ane_transport_pool_reuse_test),
KUNIT_CASE(ane_transport_pool_reuse_native_test),
KUNIT_CASE(ane_transport_pool_no_free_test),
KUNIT_CASE(ane_transport_shared_reserve_test),
KUNIT_CASE(ane_transport_shared_reserve_invalid_test),
KUNIT_CASE(ane_transport_settle_test),
KUNIT_CASE(ane_transport_carry_shared_test),
KUNIT_CASE(ane_transport_mode_stop_test),
Expand Down
36 changes: 36 additions & 0 deletions drivers/accel/ane/ane_h16_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,36 @@ static int h16_pool_room(struct ane_h16_session *s, const size_t *lengths,
return 0;
}

/*
* Every CREATE makes the firmware ask for a 2 MiB EXPP process pool by
* SHAREDMALLOC and wait for it before it answers; a pool it could not get
* leaves the CREATE stalled and the engine quarantined, and with it every
* other program of the session. Take the pool before LOAD instead
* (ane_fw_transport_shared_reserve()), so that without memory or quota the
* LOAD fails cleanly, before any traffic. A FREE of a torn-down program's
* pool may still be posted (it can come after the post-UNLOAD settle):
* serve it and try once more before giving up.
*/
static int h16_process_pool(struct ane_h16_session *s)
{
int ret = ane_fw_transport_shared_reserve(s->transport, SZ_2M, ANE_FW_SHARED_EXPP);

if (ret == -ENOSPC) {
ret = ane_fw_transport_settle(s->transport, s->settle_ns,
max(s->settle_ns, ANE_H16_SETTLE_LIMIT_NS));
if (ret < 0)
return h16_fail(s, ret);
ret = ane_fw_transport_shared_reserve(s->transport, SZ_2M,
ANE_FW_SHARED_EXPP);
}
if (ret && s->transport->error)
return h16_fail(s, ret);
if (ret)
s->ops->log(s->ctx, "LOAD: no process pool", (u64)-ret,
s->transport->shared_bytes);
return ret;
}

/*
* Outside a request batch, the commands published since mark were all
* acknowledged and the firmware keeps no pointer into them: give their
Expand Down Expand Up @@ -1101,6 +1131,9 @@ int ane_h16_session_load(struct ane_h16_session *s,
if (ret)
return ret;
ret = h16_load_room(s);
if (ret)
return ret;
ret = h16_process_pool(s);
if (ret)
return ret;
p = kzalloc_obj(*p);
Expand Down Expand Up @@ -2074,6 +2107,9 @@ static int h16_replay(struct ane_h16_session *s, struct ane_h16_program *p)
unsigned int i;
int ret;

ret = h16_process_pool(s);
if (ret)
return ret;
command = kzalloc(ANE_H16_CREATE_SIZE, GFP_KERNEL);
if (!command)
return -ENOMEM;
Expand Down
Loading
Loading