diff --git a/drivers/accel/ane/ane_fw_cold.c b/drivers/accel/ane/ane_fw_cold.c index 7c83be580e668..920a2b23a8900 100644 --- a/drivers/accel/ane/ane_fw_cold.c +++ b/drivers/accel/ane/ane_fw_cold.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT /* Shared firmware backend. H17 is the first measured cold power provider. */ #include +#include #include #include #include @@ -31,6 +32,10 @@ #define ANE_FW_IPC_DVA 0x100003d0000ULL #define ANE_FW_HEAP_DVA 0x100003e4000ULL #define ANE_FW_ALLOC_DVA 0x10001300000ULL +/* SHAREDMALLOC and program mappings: DVAs from ANE_FW_ALLOC_DVA on. */ +#define ANE_FW_WINDOW SZ_512M +/* ane_fw_alloc_mem(): a DVA range from the window. */ +#define ANE_FW_WINDOW_ANY U64_MAX #define ANE_FW_TIMEOUT_NS (3ULL * NSEC_PER_SEC) enum ane_fw_stage { @@ -47,6 +52,8 @@ struct ane_fw_mapping { struct list_head list; struct ane_fw_surface surface; dma_addr_t physical; + /* The DVA range, for a mapping in the window. */ + struct drm_mm_node node; }; struct ane_fw_backend { @@ -113,7 +120,13 @@ struct ane_fw_backend { u64 sys_off_at, sys_off_ns; struct ane_fw_cold_state cold; u64 ranges[ANE_FW_DAPF_MAX * 2]; - u64 next_dva; + /* + * The DVA window and the mappings list. Program mappings come and go + * under the session lock while the engine is up; the others are made + * with the session closed. + */ + struct drm_mm dva_window; + struct mutex window_lock; enum ane_fw_stage stage; bool retained; /* Brings the engine up to the program stage after a loader hand-off. */ @@ -1287,6 +1300,10 @@ static int ane_fw_alloc_mem(struct ane_fw_backend *b, u64 dva, size_t size, struct ane_fw_mapping *m; int ret; + /* No memory for a range the window cannot hold. */ + if (dva == ANE_FW_WINDOW_ANY && size > ANE_FW_WINDOW) + return -ENOSPC; + m = kzalloc_obj(*m); if (!m) return -ENOMEM; @@ -1306,10 +1323,32 @@ static int ane_fw_alloc_mem(struct ane_fw_backend *b, u64 dva, size_t size, dma_sync_single_for_device(b->memory, m->physical, size, DMA_BIDIRECTIONAL); m->surface.handle = m->physical; } + mutex_lock(&b->window_lock); + if (dva == ANE_FW_WINDOW_ANY) { + ret = drm_mm_insert_node_in_range(&b->dva_window, &m->node, size, SZ_16K, 0, + ANE_FW_ALLOC_DVA, + ANE_FW_ALLOC_DVA + ANE_FW_WINDOW, + DRM_MM_INSERT_LOW); + if (ret) { + mutex_unlock(&b->window_lock); + if (cached) + dma_free_noncoherent(b->memory, size, m->surface.cpu, + m->physical, DMA_BIDIRECTIONAL); + else + dma_free_coherent(b->memory, size, m->surface.cpu, m->physical); + kfree(m); + return ret; + } + dva = m->node.start; + } m->surface.dva = dva; m->surface.size = size; - /* Record before publication, including a failed/partial mapping. */ + /* + * Record before publication, including a failed/partial mapping: it + * keeps its memory and range until reset. + */ list_add_tail(&m->list, &b->mappings); + mutex_unlock(&b->window_lock); ret = ane_fw_map(b, dva, m->physical, size, prot); if (!ret) *s = m->surface; @@ -1327,14 +1366,11 @@ static int ane_fw_alloc(void *ctx, size_t size, u32 tag, struct ane_fw_surface * struct ane_fw_backend *b = ctx; int ret; - if (!size || size > ANE_FW_SHARED_TOTAL || - b->next_dva > ANE_FW_ALLOC_DVA + SZ_512M - size) + if (!size || size > ANE_FW_SHARED_TOTAL) return -ENOSPC; - ret = ane_fw_alloc_at(b, b->next_dva, size, IOMMU_READ | IOMMU_WRITE, s); - if (!ret) { - b->next_dva += size; + ret = ane_fw_alloc_at(b, ANE_FW_WINDOW_ANY, size, IOMMU_READ | IOMMU_WRITE, s); + if (!ret) dev_dbg(&b->pdev->dev, "SHAREDMALLOC tag=%#x bytes=%zu\n", tag, size); - } return ret; } @@ -1647,7 +1683,7 @@ static int ane_fw_prepare(struct ane_fw_backend *b) return ret; if (!(b->domain->pgsize_bitmap & SZ_16K) || !b->domain->ops->iotlb_sync_map || b->domain->geometry.aperture_start || - b->domain->geometry.aperture_end < ANE_FW_ALLOC_DVA + SZ_512M) + b->domain->geometry.aperture_end < ANE_FW_ALLOC_DVA + ANE_FW_WINDOW) return -ERANGE; b->memory = root_device_register("ane-fw-memory"); if (IS_ERR(b->memory)) @@ -1841,17 +1877,53 @@ static int ane_fw_startup(struct ane_fw_backend *b) static int ane_fw_program_map(void *ctx, size_t size, bool write, struct ane_fw_surface *out) { struct ane_fw_backend *b = ctx; - int ret; - if (!size || !IS_ALIGNED(size, SZ_16K) || - b->next_dva > ANE_FW_ALLOC_DVA + SZ_512M - size) + if (!size || !IS_ALIGNED(size, SZ_16K)) return -ENOSPC; - ret = ane_fw_alloc_mem(b, b->next_dva, size, - IOMMU_READ | (write ? IOMMU_WRITE : 0), true, out); - if (ret) - return ret; - b->next_dva += size; - return 0; + return ane_fw_alloc_mem(b, ANE_FW_WINDOW_ANY, size, + IOMMU_READ | (write ? IOMMU_WRITE : 0), true, out); +} + +/* + * A program surface the firmware no longer uses (ops->unmap). The session + * calls it with the engine up, so the DART invalidation that iommu_unmap() + * ends with reaches the engine before its pages and its DVA range go back. + * A short unmap keeps everything, as a failed mapping does. + */ +static void ane_fw_program_unmap(void *ctx, const struct ane_fw_surface *s) +{ + struct ane_fw_backend *b = ctx; + struct ane_fw_mapping *m, *found = NULL; + size_t unmapped; + + mutex_lock(&b->window_lock); + list_for_each_entry(m, &b->mappings, list) { + if (m->surface.dva == s->dva && m->surface.size == s->size && + m->surface.handle && drm_mm_node_allocated(&m->node)) { + found = m; + list_del(&m->list); + break; + } + } + mutex_unlock(&b->window_lock); + if (WARN_ON_ONCE(!found)) + return; + m = found; + unmapped = iommu_unmap(b->domain, s->dva, s->size); + if (unmapped != s->size) { + dev_err(&b->pdev->dev, "H17 ANE: program unmap short at %#llx: %zu of %zu; kept\n", + s->dva, unmapped, s->size); + mutex_lock(&b->window_lock); + list_add_tail(&m->list, &b->mappings); + mutex_unlock(&b->window_lock); + return; + } + dma_free_noncoherent(b->memory, s->size, m->surface.cpu, m->physical, + DMA_BIDIRECTIONAL); + mutex_lock(&b->window_lock); + drm_mm_remove_node(&m->node); + mutex_unlock(&b->window_lock); + kfree(m); } static void ane_fw_program_log(void *ctx, const char *item, u64 a, u64 c) @@ -1881,6 +1953,7 @@ static void ane_fw_program_busy(void *ctx, bool busy) static const struct ane_h16_session_ops ane_fw_session_ops = { .map = ane_fw_program_map, + .unmap = ane_fw_program_unmap, .log = ane_fw_program_log, .sync = ane_fw_program_sync, .busy = ane_fw_program_busy, @@ -2286,7 +2359,8 @@ int ane_fw_staged_init(struct platform_device *pdev, const struct ane_fw_platfor b->pdev = pdev; get_device(&pdev->dev); b->soc = soc; - b->next_dva = ANE_FW_ALLOC_DVA; + drm_mm_init(&b->dva_window, ANE_FW_ALLOC_DVA, ANE_FW_WINDOW); + mutex_init(&b->window_lock); mutex_init(&b->lock); INIT_LIST_HEAD(&b->mappings); if (of_property_count_strings(np, "reg-names") != soc->reg_count || diff --git a/drivers/accel/ane/ane_fw_transport.h b/drivers/accel/ane/ane_fw_transport.h index ab1d7134eb3a6..e0e1a0edd1211 100644 --- a/drivers/accel/ane/ane_fw_transport.h +++ b/drivers/accel/ane/ane_fw_transport.h @@ -145,6 +145,12 @@ struct ane_fw_transport { u8 table[ANE_FW_TABLE_SIZE]; size_t table_offset; size_t pool_used; + /* + * The arena range a caller places commands into itself + * (ane_fw_transport_reserve()); zero-sized until it is taken. + */ + size_t placed; + size_t placed_size; size_t shared_bytes; u32 shared_count; u64 terminal_messages; @@ -374,10 +380,17 @@ static inline bool ane_fw_transport_switch_safe(const struct ane_fw_transport *t return t->soc->chip_family == 16 || t->soc->chip_family == 17; } -/* Commands are copied into a monotonic pool arena, never external DMA memory. */ +/* ane_fw_transport_publish_at() offset that copies to the end of the arena. */ +#define ANE_FW_ARENA SIZE_MAX + +/* + * Commands are copied into the pool, never external DMA memory: by default + * (ANE_FW_ARENA) to the end of a monotonic arena, else to offset at of the + * caller's placed range (ane_fw_transport_reserve()). + */ static inline int __ane_fw_transport_publish(struct ane_fw_transport *t, unsigned int ch, - const void *command, size_t size, u64 deadline_ns) + const void *command, size_t size, u64 deadline_ns, size_t at) { struct ane_fw_transaction *tx; size_t offset; @@ -401,11 +414,19 @@ __ane_fw_transport_publish(struct ane_fw_transport *t, unsigned int ch, return ane_fw_transport_fail(t, -EPROTO); if (!(header & 1)) return -EBUSY; - if (t->pool_used > SIZE_MAX - (ANE_FW_SLOT_SIZE - 1)) - return -ENOSPC; - offset = ALIGN(t->pool_used, ANE_FW_SLOT_SIZE); - if (offset > t->pool.size || size > t->pool.size - offset) - return -ENOSPC; + if (at != ANE_FW_ARENA) { + if (!IS_ALIGNED(at, ANE_FW_SLOT_SIZE) || at < t->placed || + at - t->placed > t->placed_size || + size > t->placed_size - (at - t->placed)) + return -EINVAL; + offset = at; + } else { + if (t->pool_used > SIZE_MAX - (ANE_FW_SLOT_SIZE - 1)) + return -ENOSPC; + offset = ALIGN(t->pool_used, ANE_FW_SLOT_SIZE); + if (offset > t->pool.size || size > t->pool.size - offset) + return -ENOSPC; + } memcpy((u8 *)t->pool.cpu + offset, command, size); *tx = (struct ane_fw_transaction) { .dva = t->pool.dva + offset, @@ -414,7 +435,8 @@ __ane_fw_transport_publish(struct ane_fw_transport *t, unsigned int ch, .id = get_unaligned_le16((const u8 *)command + 4), .active = true, }; - t->pool_used = offset + size; + if (at == ANE_FW_ARENA) + t->pool_used = offset + size; if (ane_fw_transport_switch_safe(t)) ane_fw_transport_bar_context(t, (u8 *)t->pool.cpu + offset, size); t->ops->store64(t->ctx, p + 8, size); @@ -428,15 +450,22 @@ __ane_fw_transport_publish(struct ane_fw_transport *t, unsigned int ch, /* IO and DATA_CHAIN; mode and ownership commands are refused. */ static inline int -ane_fw_transport_publish(struct ane_fw_transport *t, unsigned int ch, - const void *command, size_t size, u64 deadline_ns) +ane_fw_transport_publish_at(struct ane_fw_transport *t, unsigned int ch, + const void *command, size_t size, u64 deadline_ns, size_t at) { if (ch != ANE_FW_IO && ch != ANE_FW_DATA_CHAIN) return -EINVAL; if (command && size >= 8 && ane_fw_command_denied(get_unaligned_le16((const u8 *)command + 4))) return -EPERM; - return __ane_fw_transport_publish(t, ch, command, size, deadline_ns); + return __ane_fw_transport_publish(t, ch, command, size, deadline_ns, at); +} + +static inline int +ane_fw_transport_publish(struct ane_fw_transport *t, unsigned int ch, + const void *command, size_t size, u64 deadline_ns) +{ + return ane_fw_transport_publish_at(t, ch, command, size, deadline_ns, ANE_FW_ARENA); } /* @@ -453,7 +482,7 @@ ane_fw_transport_publish_pmu_base(struct ane_fw_transport *t, u64 base, u64 dead put_unaligned_le16(ANE_FW_CMD_SET_PMU_BASE, command + 4); put_unaligned_le64(base, command + 8); return __ane_fw_transport_publish(t, t->control_ch, command, sizeof(command), - deadline_ns); + deadline_ns, ANE_FW_ARENA); } /* @@ -469,7 +498,7 @@ ane_fw_transport_publish_mode_stop(struct ane_fw_transport *t, u64 deadline_ns) return -ENODEV; put_unaligned_le16(ANE_FW_CMD_EXCLAVE_MODE_STOP, command + 4); return __ane_fw_transport_publish(t, t->control_ch, command, sizeof(command), - deadline_ns); + deadline_ns, ANE_FW_ARENA); } /* Command arena position, for ane_fw_transport_release(). */ @@ -478,17 +507,44 @@ static inline size_t ane_fw_transport_mark(const struct ane_fw_transport *t) return t->pool_used; } +/* + * Take size bytes at the end of the arena, once per firmware boot, for + * commands the caller places and reuses itself (ane_fw_transport_publish_at()). + * No ane_fw_transport_release() gives the range back; a new firmware boot + * (ane_fw_transport_init()) does. + */ +static inline int ane_fw_transport_reserve(struct ane_fw_transport *t, size_t size) +{ + size_t offset; + + if (t->error) + return t->error; + if (t->placed_size || !size || !IS_ALIGNED(size, ANE_FW_SLOT_SIZE)) + return -EINVAL; + if (t->pool_used > SIZE_MAX - (ANE_FW_SLOT_SIZE - 1)) + return -ENOSPC; + offset = ALIGN(t->pool_used, ANE_FW_SLOT_SIZE); + if (offset > t->pool.size || size > t->pool.size - offset) + return -ENOSPC; + t->placed = offset; + t->placed_size = size; + t->pool_used = offset + size; + return 0; +} + /* * Give back the arena used by every command published since @mark. The caller * guarantees that the firmware is done with all of them, not only that they * were acknowledged: an EXECUTE is done once its completion report arrived. - * Refused while any channel still has a command in flight. + * Refused while any channel still has a command in flight, and below the + * placed range. */ static inline int ane_fw_transport_release(struct ane_fw_transport *t, size_t mark) { unsigned int ch; - if (t->error || mark > t->pool_used || mark > t->pool.size) + if (t->error || mark > t->pool_used || mark > t->pool.size || + (t->placed_size && mark < t->placed + t->placed_size)) return -EINVAL; for (ch = 0; ch < ANE_FW_CHANNELS; ch++) if (t->pending[ch].active) diff --git a/drivers/accel/ane/ane_h16_backend.c b/drivers/accel/ane/ane_h16_backend.c index d009767105d23..5aca6caa277f5 100644 --- a/drivers/accel/ane/ane_h16_backend.c +++ b/drivers/accel/ane/ane_h16_backend.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT /* Staged H16 backend, using the accepted H16 cold-boot facts, round 5. */ +#include #include #include #include @@ -36,6 +37,8 @@ #define ANE_H16_TX_WORDS 272 /* Private program window above the SHAREDMALLOC range; host policy. */ #define ANE_H16_PROGRAM_WINDOW SZ_1G +/* ane_h16_alloc_prot(): a DVA range from the program window. */ +#define ANE_H16_PROGRAM_ANY U64_MAX struct ane_h16_mapping { struct list_head node; @@ -45,6 +48,8 @@ struct ane_h16_mapping { struct page **pages; u32 blocks; void *cpu; + /* The DVA range, for a mapping in the program window. */ + struct drm_mm_node range; }; struct ane_h16_device { @@ -61,6 +66,12 @@ struct ane_h16_device { u32 powered; u32 darts_powered; u64 preload_phys[2]; + /* + * Guards mappings and program_window. Program mappings come and go + * under the session lock while the engine is up; SHAREDMALLOC may be + * mapped at the same time. + */ + struct mutex window_lock; struct list_head mappings; struct iommu_domain *domain; struct iommu_group *group; @@ -97,7 +108,9 @@ struct ane_h16_device { u32 tx_write; bool rtk_failed; u64 shared_next; - u64 program_next; + /* Program sections and surfaces: [program_start, program_end). */ + struct drm_mm program_window; + u64 program_start; u64 program_end; struct dentry *debugfs; bool acquired; @@ -893,7 +906,9 @@ static int ane_h16_map_range(struct ane_h16_device *h, u64 dva, u64 pa, return -ENOMEM; m->dva = dva; m->size = size; + mutex_lock(&h->window_lock); list_add_tail(&m->node, &h->mappings); + mutex_unlock(&h->window_lock); ret = iommu_map(h->domain, dva, pa, size, prot, GFP_KERNEL); if (!ret) m->mapped = size; @@ -1015,6 +1030,25 @@ static int ane_h16_map(void *ctx) return 0; } +/* Memory only: off h->mappings, its range (if any) already given back. */ +static void ane_h16_mapping_free(struct ane_h16_mapping *m) +{ + unsigned int i, pages_per_block = SZ_16K / PAGE_SIZE; + + if (m->cpu) + vunmap(m->cpu); + for (i = 0; i < m->blocks; i++) + __free_pages(m->pages[i * pages_per_block], get_order(SZ_16K)); + kvfree(m->pages); + kfree(m); +} + +/* + * Map size bytes of fresh zeroed memory at dva, or with ANE_H16_PROGRAM_ANY + * at a range of the program window. Memory that never reached the IOMMU is + * freed on failure; a failed or partial iommu_map_sgtable() keeps the + * mapping, its memory and its range until reset. + */ static int ane_h16_alloc_prot(struct ane_h16_device *h, u64 dva, size_t size, int prot, struct ane_fw_surface *out) { @@ -1029,23 +1063,22 @@ static int ane_h16_alloc_prot(struct ane_h16_device *h, u64 dva, size_t size, in dev_err(h->dev, "H16 private surfaces require a coherent client\n"); return -EOPNOTSUPP; } - if (!size || !IS_ALIGNED(size, SZ_16K) || !IS_ALIGNED(dva, SZ_16K) || - PAGE_SIZE > SZ_16K) + if (!size || !IS_ALIGNED(size, SZ_16K) || PAGE_SIZE > SZ_16K || + (dva != ANE_H16_PROGRAM_ANY && !IS_ALIGNED(dva, SZ_16K))) return -EINVAL; m = kzalloc_obj(*m); if (!m) return -ENOMEM; m->size = size; - m->dva = dva; - list_add_tail(&m->node, &h->mappings); + ret = -ENOMEM; m->pages = kvcalloc(size / PAGE_SIZE, sizeof(*m->pages), GFP_KERNEL); if (!m->pages) - return -ENOMEM; + goto free; for (i = 0; i < size / SZ_16K; i++) { struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order); if (!page) - return -ENOMEM; + goto free; for (j = 0; j < pages_per_block; j++) m->pages[i * pages_per_block + j] = page + j; m->blocks++; @@ -1053,10 +1086,26 @@ static int ane_h16_alloc_prot(struct ane_h16_device *h, u64 dva, size_t size, in /* The coherent-client gate permits matching cached CPU/device aliases. */ m->cpu = vmap(m->pages, size / PAGE_SIZE, VM_MAP, PAGE_KERNEL); if (!m->cpu) - return -ENOMEM; + goto free; ret = sg_alloc_table_from_pages(&sgt, m->pages, size / PAGE_SIZE, 0, size, GFP_KERNEL); if (ret) - return ret; + goto free; + mutex_lock(&h->window_lock); + if (dva == ANE_H16_PROGRAM_ANY) { + ret = drm_mm_insert_node_in_range(&h->program_window, &m->range, size, + SZ_16K, 0, 0, ANE_H16_PROGRAM_WINDOW, + DRM_MM_INSERT_LOW); + if (ret) { + mutex_unlock(&h->window_lock); + sg_free_table(&sgt); + goto free; + } + dva = h->program_start + m->range.start; + } + m->dva = dva; + /* Recorded before publication, including a failed/partial mapping. */ + list_add_tail(&m->node, &h->mappings); + mutex_unlock(&h->window_lock); /* A failed call has already unmapped what it mapped. */ mapped = iommu_map_sgtable(h->domain, dva, &sgt, prot); sg_free_table(&sgt); @@ -1065,6 +1114,9 @@ static int ane_h16_alloc_prot(struct ane_h16_device *h, u64 dva, size_t size, in m->mapped = mapped; *out = (struct ane_fw_surface) { .cpu = m->cpu, .dva = dva, .size = size }; return 0; +free: + ane_h16_mapping_free(m); + return ret; } static int ane_h16_alloc_at(void *ctx, u64 dva, size_t size, struct ane_fw_surface *out) @@ -1076,21 +1128,61 @@ static int ane_h16_alloc_at(void *ctx, u64 dva, size_t size, struct ane_fw_surfa static int ane_h16_program_map(void *ctx, size_t size, bool write, struct ane_fw_surface *out) { struct ane_h16_device *h = ctx; - u64 dva = h->program_next; int ret; - if (!size || size > h->program_end - dva) + if (!size || size > ANE_H16_PROGRAM_WINDOW) return -ENOSPC; - /* A failed mapping keeps its range; DVAs are never reused before reset. */ - h->program_next += size; - ret = ane_h16_alloc_prot(h, dva, size, + ret = ane_h16_alloc_prot(h, ANE_H16_PROGRAM_ANY, size, IOMMU_READ | IOMMU_CACHE | (write ? IOMMU_WRITE : 0), out); if (!ret) - dev_info(h->dev, "H16 program mapping %llx bytes %zx%s\n", dva, size, + dev_info(h->dev, "H16 program mapping %llx bytes %zx%s\n", out->dva, size, write ? " writable" : ""); return ret; } +/* + * A program surface the firmware no longer uses (ops->unmap). The session + * calls it with the engine up and its lock held. iommu_unmap() ends with + * the DART TLB invalidation, so the engine cannot reach the memory once it + * returns; only then do the pages and the DVA range go back. A short unmap + * keeps everything, as a failed mapping does. Only program-window mappings + * qualify: SHAREDMALLOC, preload, heap tail and MMIO stay until reset. + */ +static void ane_h16_program_unmap(void *ctx, const struct ane_fw_surface *s) +{ + struct ane_h16_device *h = ctx; + struct ane_h16_mapping *m, *found = NULL; + size_t unmapped; + + mutex_lock(&h->window_lock); + list_for_each_entry(m, &h->mappings, node) { + if (m->dva == s->dva && m->size == s->size && m->mapped == m->size && + drm_mm_node_allocated(&m->range)) { + found = m; + list_del(&m->node); + break; + } + } + mutex_unlock(&h->window_lock); + if (WARN_ON_ONCE(!found)) + return; + m = found; + unmapped = iommu_unmap(h->domain, m->dva, m->size); + if (unmapped != m->size) { + dev_err(h->dev, "H16 program unmap short at %llx: %zx of %zx; kept\n", + m->dva, unmapped, m->size); + mutex_lock(&h->window_lock); + list_add_tail(&m->node, &h->mappings); + mutex_unlock(&h->window_lock); + return; + } + dev_dbg(h->dev, "H16 program unmapped %llx bytes %zx\n", m->dva, m->size); + mutex_lock(&h->window_lock); + drm_mm_remove_node(&m->range); + mutex_unlock(&h->window_lock); + ane_h16_mapping_free(m); +} + static u64 ane_h16_load64(void *ctx, const void *p) { return le64_to_cpu(READ_ONCE(*(const __le64 *)p)); @@ -1451,6 +1543,7 @@ static void ane_h16_hung(void *ctx, int error) static const struct ane_h16_session_ops ane_h16_session_ops = { .map = ane_h16_program_map, + .unmap = ane_h16_program_unmap, .log = ane_h16_log, .busy = ane_h16_busy, .hung = ane_h16_hung, @@ -1477,20 +1570,17 @@ static const struct ane_fw_transport_ops ane_h16_transport_ops = { static void ane_h16_release(struct ane_h16_device *h) { struct ane_h16_mapping *m, *next; - unsigned int i, pages_per_block = SZ_16K / PAGE_SIZE; + unsigned int i; if (h->boot.ran || h->pinned || !h->acquired) return; list_for_each_entry_safe_reverse(m, next, &h->mappings, node) { if (m->mapped) iommu_unmap(h->domain, m->dva, m->mapped); - if (m->cpu) - vunmap(m->cpu); - for (i = 0; i < m->blocks; i++) - __free_pages(m->pages[i * pages_per_block], get_order(SZ_16K)); - kvfree(m->pages); list_del(&m->node); - kfree(m); + if (drm_mm_node_allocated(&m->range)) + drm_mm_remove_node(&m->range); + ane_h16_mapping_free(m); } if (h->attached) iommu_detach_device(h->domain, h->dev); @@ -1802,8 +1892,8 @@ static int ane_h16_preload_identity(struct ane_h16_device *h) b->max_restarts = ANE_H16_STARTUP_RESTARTS; } h->shared_next = b->build->shared_dva; - h->program_next = b->build->shared_dva + ANE_FW_SHARED_TOTAL; - h->program_end = h->program_next + ANE_H16_PROGRAM_WINDOW; + h->program_start = b->build->shared_dva + ANE_FW_SHARED_TOTAL; + h->program_end = h->program_start + ANE_H16_PROGRAM_WINDOW; dev_info(h->dev, "H16 preload identity %s accepted\n", identity); out: vfree(second); @@ -1841,7 +1931,7 @@ static int ane_h16_program_stage(struct ane_h16_device *h) } h->program_ready = true; ane_h16_irq_wait_set(h, true); - dev_info(h->dev, "H16 stage 5 program window %llx..%llx%s\n", h->program_next, + dev_info(h->dev, "H16 stage 5 program window %llx..%llx%s\n", h->program_start, h->program_end - 1, h->clpc ? ", CLPC input attached" : ""); return 0; } @@ -2074,7 +2164,10 @@ int ane_h16_backend_probe(struct platform_device *pdev, const struct ane_fw_plat h->irq = -1; spin_lock_init(&h->irq_lock); init_waitqueue_head(&h->irq_wq); + mutex_init(&h->window_lock); INIT_LIST_HEAD(&h->mappings); + /* Offsets from program_start, which the preload's build decides. */ + drm_mm_init(&h->program_window, 0, ANE_H16_PROGRAM_WINDOW); h->boot.soc = soc; /* The build, and with it the DVA layout, is chosen from the preload. */ h->boot.build = NULL; @@ -2135,6 +2228,7 @@ int ane_h16_backend_probe(struct platform_device *pdev, const struct ane_fw_plat return 0; free: dev_err(h->dev, "H16 DT description refused, error %d\n", ret); + drm_mm_takedown(&h->program_window); kfree(h); unlock: mutex_unlock(&ane_h16_lock); @@ -2176,6 +2270,7 @@ void ane_h16_backend_remove(struct platform_device *pdev) dev_warn(h->dev, "H16 remove held; reboot reclaims firmware DMA\n"); } else { ane_h16_release(h); + drm_mm_takedown(&h->program_window); kfree(h); } mutex_unlock(&ane_h16_lock); diff --git a/drivers/accel/ane/ane_h16_program.c b/drivers/accel/ane/ane_h16_program.c index 9132cc2ddaded..e7d9c67bc89a9 100644 --- a/drivers/accel/ane/ane_h16_program.c +++ b/drivers/accel/ane/ane_h16_program.c @@ -578,16 +578,20 @@ static u64 h16_now(struct ane_h16_session *s) return s->transport->ops->now_ns(s->transport->ctx); } -/* Publish one command and wait for its transport reply, kept in the pool. */ -static int h16_send(struct ane_h16_session *s, unsigned int ch, const void *command, - size_t length, const u8 **reply) +/* + * Publish one command at arena offset at (ANE_FW_ARENA: the arena's end) and + * wait for its transport reply, kept in the pool. + */ +static int h16_send_at(struct ane_h16_session *s, unsigned int ch, const void *command, + size_t length, size_t at, const u8 **reply) { struct ane_fw_transport *t = s->transport; int ret; if (ane_fw_command_denied(get_unaligned_le16((const u8 *)command + 4))) return -EPERM; - ret = ane_fw_transport_publish(t, ch, command, length, h16_now(s) + s->timeout_ns); + ret = ane_fw_transport_publish_at(t, ch, command, length, + h16_now(s) + s->timeout_ns, at); if (ret) return ret; ret = ane_fw_transport_wait(t, ch); @@ -595,11 +599,21 @@ static int h16_send(struct ane_h16_session *s, unsigned int ch, const void *comm return ret; } +static int h16_send(struct ane_h16_session *s, unsigned int ch, const void *command, + size_t length, const u8 **reply) +{ + return h16_send_at(s, ch, command, length, ANE_FW_ARENA, reply); +} + /* - * The transport copies commands into a pool arena. LOAD and CREATE stay in - * it; each EXECUTE sequence gives its space back once the firmware reported - * the request complete. Refuse a sequence up front when it cannot fit, so - * running out of arena is a clean -ENOSPC and never a half-sent sequence. + * The transport copies commands into a pool arena. A program's LOAD and + * CREATE stay in its own record (h16_record_get()) until the firmware + * acknowledged its UNLOAD. Everything else goes to the end of the arena and + * is given back as soon as the firmware is done with it: each EXECUTE + * sequence once the firmware reported the request complete, a gate or a + * TERMINATE and UNLOAD outside a request once acknowledged. Refuse a + * sequence up front when it cannot fit, so running out of arena is a clean + * -ENOSPC and never a half-sent sequence. */ static int h16_pool_room(struct ane_h16_session *s, const size_t *lengths, unsigned int count) @@ -617,6 +631,88 @@ static int h16_pool_room(struct ane_h16_session *s, const size_t *lengths, return 0; } +/* + * Outside a request batch, the commands published since mark were all + * acknowledged and the firmware keeps no pointer into them: give their + * arena back. A batch gives back its own commands, and those published + * while it ran, when it ends; a quarantined session keeps everything. + */ +static void h16_pool_release(struct ane_h16_session *s, size_t mark) +{ + int ret; + + if (s->running || s->quarantined) + return; + ret = ane_fw_transport_release(s->transport, mark); + if (ret) + s->ops->log(s->ctx, "command arena kept", (u64)-ret, mark); +} + +/* + * Each program's LOAD and CREATE get a record of their own, freed at its + * UNLOAD, so a program that stays loaded never keeps another program's + * commands in the arena, and EXECUTE batches never wait for program + * lifetimes. The records are taken at the first LOAD of a firmware boot, + * before any EXECUTE batch. + */ +static int h16_records_ready(struct ane_h16_session *s) +{ + if (s->transport->placed_size) + return 0; + /* A new placed range: no record of it is in use. */ + bitmap_zero(s->records, ANE_H16_RECORDS); + return ane_fw_transport_reserve(s->transport, ANE_H16_RECORDS * ANE_H16_RECORD_SIZE); +} + +/* Room for one more program: a free record, and its teardown at the arena's end. */ +static int h16_load_room(struct ane_h16_session *s) +{ + static const size_t teardown[] = { + ANE_H16_GATE_SIZE, ANE_H16_TERMINATE_SIZE, ANE_H16_UNLOAD_SIZE, + }; + int ret = h16_records_ready(s); + + if (ret) + return ret; + if (bitmap_full(s->records, ANE_H16_RECORDS)) + return -ENOSPC; + return h16_pool_room(s, teardown, ARRAY_SIZE(teardown)); +} + +static int h16_record_get(struct ane_h16_session *s, struct ane_h16_program *p) +{ + unsigned int i; + int ret = h16_records_ready(s); + + if (ret) + return ret; + i = find_first_zero_bit(s->records, ANE_H16_RECORDS); + if (i >= ANE_H16_RECORDS) + return -ENOSPC; + __set_bit(i, s->records); + p->record = i; + return 0; +} + +/* + * The firmware acknowledged the program's UNLOAD, after which it keeps no + * pointer into its LOAD and CREATE: the record serves the next program. + */ +static void h16_record_put(struct ane_h16_session *s, struct ane_h16_program *p) +{ + if (p->record >= 0 && p->epoch == s->epoch) + __clear_bit(p->record, s->records); + p->record = -1; +} + +/* Arena offset of the program's LOAD, or with create of its CREATE. */ +static size_t h16_record_at(const struct ane_h16_session *s, const struct ane_h16_program *p, + bool create) +{ + return s->transport->placed + p->record * ANE_H16_RECORD_SIZE + + (create ? ALIGN(ANE_H16_LOAD_SIZE, ANE_FW_SLOT_SIZE) : 0); +} + static void h16_sync(struct ane_h16_session *s, const struct ane_fw_surface *surface, bool for_device) { @@ -640,6 +736,37 @@ void ane_h16_session_sync(struct ane_h16_session *s, struct ane_h16_program *p, } EXPORT_SYMBOL_GPL(ane_h16_session_sync); +/* + * Spare memory can go back to the backend: it has ops->unmap and the engine + * is up, so the DART invalidation that comes with the unmap reaches it. + */ +static bool h16_can_unmap(const struct ane_h16_session *s) +{ + return s->ops->unmap && !s->asleep && !s->parked; +} + +static struct ane_fw_surface h16_spare_remove(struct ane_h16_session *s, unsigned int i) +{ + struct ane_fw_surface surface = s->spares[i].surface; + + s->spare_bytes -= surface.size; + s->spare_count--; + memmove(&s->spares[i], &s->spares[i + 1], (s->spare_count - i) * sizeof(s->spares[0])); + return surface; +} + +/* Free the oldest spares until at most @count entries and @bytes remain. */ +static void h16_spares_trim(struct ane_h16_session *s, unsigned int count, size_t bytes) +{ + if (!h16_can_unmap(s)) + return; + while (s->spare_count && (s->spare_count > count || s->spare_bytes > bytes)) { + struct ane_fw_surface surface = h16_spare_remove(s, 0); + + s->ops->unmap(s->ctx, &surface); + } +} + static bool h16_take_spare(struct ane_h16_session *s, size_t size, bool write, struct ane_fw_surface *out) { @@ -648,8 +775,7 @@ static bool h16_take_spare(struct ane_h16_session *s, size_t size, bool write, for (i = 0; i < s->spare_count; i++) { if (s->spares[i].surface.size != size || s->spares[i].write != write) continue; - *out = s->spares[i].surface; - s->spares[i] = s->spares[--s->spare_count]; + *out = h16_spare_remove(s, i); /* Zeroed like new memory; LOAD writes it back after filling it. */ memset(out->cpu, 0, size); return true; @@ -660,12 +786,20 @@ static bool h16_take_spare(struct ane_h16_session *s, size_t size, bool write, static void h16_give_spare(struct ane_h16_session *s, struct ane_fw_surface *surface, bool write) { - /* A full table keeps the surface mapped and unused until reset. */ - if (surface->cpu && s->spare_count < ANE_H16_SPARES) + if (!surface->cpu) { + memset(surface, 0, sizeof(*surface)); + return; + } + if (s->spare_count < ANE_H16_SPARES) { s->spares[s->spare_count++] = (struct ane_h16_spare) { .surface = *surface, .write = write, }; + s->spare_bytes += surface->size; + } else if (h16_can_unmap(s)) { + s->ops->unmap(s->ctx, surface); + } + /* Otherwise a full table keeps the surface mapped and unused until reset. */ memset(surface, 0, sizeof(*surface)); } @@ -684,6 +818,7 @@ static void h16_export_work(struct work_struct *work) h16_give_spare(s, &e->surface, e->write); kfree(e); } + h16_spares_trim(s, ANE_H16_SPARES_KEEP, ANE_H16_SPARE_BYTES); mutex_unlock(s->lock); } @@ -775,13 +910,22 @@ static void h16_give_back(struct ane_h16_session *s, struct ane_h16_program *p) h16_give_spare(s, &p->aux[i], false); memset(p->surfaces, 0, sizeof(p->surfaces)); p->released = true; + h16_spares_trim(s, ANE_H16_SPARES_KEEP, ANE_H16_SPARE_BYTES); } static int h16_map(struct ane_h16_session *s, size_t size, bool write, struct ane_fw_surface *out) { - int ret = h16_take_spare(s, size, write, out) ? 0 : s->ops->map(s->ctx, size, write, out); + int ret = 0; + if (!h16_take_spare(s, size, write, out)) { + ret = s->ops->map(s->ctx, size, write, out); + /* No DVA range or memory left: free every spare and try once more. */ + if ((ret == -ENOSPC || ret == -ENOMEM) && s->spare_count && h16_can_unmap(s)) { + h16_spares_trim(s, 0, 0); + ret = s->ops->map(s->ctx, size, write, out); + } + } if (ret) return ret; if (!ane_fw_surface_valid(out) || out->size != size || !IS_ALIGNED(out->dva, SZ_16K)) @@ -904,9 +1048,18 @@ static int h16_program_map(struct ane_h16_session *s, struct ane_h16_program *p, return ane_h16_custom_bars(&p->snapshot, hwx, s->dsid, p->bars, &p->bar_count); } -static const size_t h16_load_sequence[] = { - ANE_H16_LOAD_SIZE, ANE_H16_CREATE_SIZE, ANE_H16_TERMINATE_SIZE, ANE_H16_UNLOAD_SIZE, -}; +/* + * LOAD or CREATE failed with the firmware holding the program, maybe: the + * session is quarantined and the program is kept, memory and all, with + * those closed while quarantined, until a verified power cycle gives them + * back (ane_h16_session_recovered()). + */ +static int h16_failed_load(struct ane_h16_session *s, struct ane_h16_program *p, int error) +{ + p->closed = true; + list_add_tail(&p->node, &s->held); + return h16_fail(s, error); +} /* * On this firmware family a direct EXECUTE reuses the BAR context of the @@ -915,8 +1068,9 @@ static const size_t h16_load_sequence[] = { * queues the first EXECUTE whenever the engine last ran another program, the * first one after boot included (see ane_h16_session_execute()). A LOAD is * serialized with in-flight EXECUTEs. The transport also requeues on every - * program or process change, so a session may hold several programs. Every - * program's firmware-visible memory stays mapped until reset. + * program or process change, so a session may hold several programs. A + * program's firmware-visible memory goes back to the spares once the + * firmware no longer uses it (see h16_give_back()). */ int ane_h16_session_load(struct ane_h16_session *s, struct ane_h16_hwx_snapshot *snapshot, @@ -946,7 +1100,7 @@ int ane_h16_session_load(struct ane_h16_session *s, ret = ane_h16_hwx_snapshot_check(snapshot); if (ret) return ret; - ret = h16_pool_room(s, h16_load_sequence, ARRAY_SIZE(h16_load_sequence)); + ret = h16_load_room(s); if (ret) return ret; p = kzalloc_obj(*p); @@ -972,10 +1126,11 @@ int ane_h16_session_load(struct ane_h16_session *s, ret = h16_program_bind(p, aux, aux_size, &binds); if (ret) goto free; + p->record = -1; /* - * From the first mapping on, firmware-visible memory exists that is never - * released before reset, so the program becomes the session's here and - * is never freed. + * From the first mapping on, firmware-visible memory exists, so the + * program becomes the session's here; it is freed only once that memory + * went back (ane_h16_session_put()). */ p->generation = ++s->generation; p->epoch = s->epoch; @@ -987,32 +1142,36 @@ int ane_h16_session_load(struct ane_h16_session *s, ret = h16_program_map(s, p, aux, aux_size, &binds, &layout); if (!ret) ret = ane_h16_load_encode(command, ANE_H16_LOAD_SIZE, p->slots); + if (!ret) + ret = h16_record_get(s, p); if (ret) { - /* - * Firmware never saw this program. It keeps its mappings until - * reset, but it must not hold the session against the next LOAD. - */ + /* Firmware never saw this program: its memory goes back now. */ p->closed = true; + h16_give_back(s, p); + ane_h16_session_put(s, p); goto out; } - ret = h16_send(s, ANE_FW_IO, command, ANE_H16_LOAD_SIZE, &reply); + /* A refused LOAD or CREATE keeps the record, like the rest, until reset. */ + ret = h16_send_at(s, ANE_FW_IO, command, ANE_H16_LOAD_SIZE, h16_record_at(s, p, false), + &reply); if (!ret) ret = ane_h16_load_reply(reply, ANE_H16_LOAD_SIZE, command, &p->key.program); if (ret) { s->ops->log(s->ctx, "LOAD refused", (u64)-ret, 0); - ret = h16_fail(s, ret); + ret = h16_failed_load(s, p, ret); goto out; } s->ops->log(s->ctx, "LOAD program", p->key.program, p->bar_count); ret = ane_h16_create_encode(command, ANE_H16_CREATE_SIZE, p->key.program); if (!ret) - ret = h16_send(s, ANE_FW_IO, command, ANE_H16_CREATE_SIZE, &reply); + ret = h16_send_at(s, ANE_FW_IO, command, ANE_H16_CREATE_SIZE, + h16_record_at(s, p, true), &reply); if (!ret) ret = ane_h16_create_reply(reply, ANE_H16_CREATE_SIZE, p->key.program, &p->key.process); if (ret) { s->ops->log(s->ctx, "CREATE refused", (u64)-ret, 0); - ret = h16_fail(s, ret); + ret = h16_failed_load(s, p, ret); goto out; } /* Each validated program has exactly one procedure. */ @@ -1022,6 +1181,7 @@ int ane_h16_session_load(struct ane_h16_session *s, s->ops->log(s->ctx, "CREATE process", p->key.program, p->key.process); *program = p; out: + h16_spares_trim(s, ANE_H16_SPARES_KEEP, ANE_H16_SPARE_BYTES); kfree(command); return ret; free: @@ -1033,6 +1193,7 @@ EXPORT_SYMBOL_GPL(ane_h16_session_load); static int h16_gate(struct ane_h16_session *s, u32 value) { + size_t mark = ane_fw_transport_mark(s->transport); u8 command[ANE_H16_GATE_SIZE]; const u8 *reply; int ret = ane_h16_gate_encode(command, sizeof(command), value); @@ -1041,6 +1202,8 @@ static int h16_gate(struct ane_h16_session *s, u32 value) ret = h16_send(s, ANE_FW_DATA_CHAIN, command, sizeof(command), &reply); if (!ret) ret = ane_h16_id_reply(reply, sizeof(command), command); + if (!ret) + h16_pool_release(s, mark); return ret; } @@ -1698,11 +1861,15 @@ int ane_h16_session_stats_show(struct seq_file *m, struct ane_h16_session *s) } EXPORT_SYMBOL_GPL(ane_h16_session_stats_show); -/* TERMINATE then UNLOAD. The mappings stay until reset; a new LOAD may follow. */ +/* + * TERMINATE then UNLOAD; once acknowledged, the program's memory goes back + * to the spares and its commands give their arena back. + */ int ane_h16_session_close(struct ane_h16_session *s, struct ane_h16_program *p) { u8 command[ANE_H16_TERMINATE_SIZE]; const u8 *reply; + size_t mark; int ret; if (!s || !p || p->session != s) @@ -1737,6 +1904,8 @@ int ane_h16_session_close(struct ane_h16_session *s, struct ane_h16_program *p) list_add_tail(&p->node, &s->held); return -EIO; } + /* Nothing runs after the drain: every command from here on is ours. */ + mark = ane_fw_transport_mark(s->transport); /* TERMINATE and UNLOAD run with gating on, as without the hold. */ ret = h16_gate_release(s); if (ret) { @@ -1769,9 +1938,12 @@ int ane_h16_session_close(struct ane_h16_session *s, struct ane_h16_program *p) } /* * The firmware acknowledged TERMINATE and UNLOAD, so it no longer - * reads or writes this program's memory: later programs may reuse it. + * reads or writes this program's memory or its commands: later + * programs may reuse them. */ h16_give_back(s, p); + h16_record_put(s, p); + h16_pool_release(s, mark); return 0; } EXPORT_SYMBOL_GPL(ane_h16_session_close); @@ -1850,6 +2022,8 @@ void ane_h16_session_restart(struct ane_h16_session *s) { s->epoch++; s->program = NULL; + /* The new firmware knows no command of the old one. */ + bitmap_zero(s->records, ANE_H16_RECORDS); /* A firmware booted again from the preload starts with gating on. */ s->gate_held = false; s->executed = 0; @@ -1900,12 +2074,16 @@ static int h16_replay(struct ane_h16_session *s, struct ane_h16_program *p) unsigned int i; int ret; - ret = h16_pool_room(s, h16_load_sequence, 2); - if (ret) - return ret; command = kzalloc(ANE_H16_CREATE_SIZE, GFP_KERNEL); if (!command) return -ENOMEM; + /* The old firmware's record went with it (ane_h16_session_restart()). */ + p->record = -1; + ret = h16_record_get(s, p); + if (ret) { + kfree(command); + return ret; + } for (i = 0; i < p->plan.count; i++) { const struct ane_h16_hwx_region *r = &p->plan.regions[i]; @@ -1916,7 +2094,8 @@ static int h16_replay(struct ane_h16_session *s, struct ane_h16_program *p) } ret = ane_h16_load_encode(command, ANE_H16_LOAD_SIZE, p->slots); if (!ret) - ret = h16_send(s, ANE_FW_IO, command, ANE_H16_LOAD_SIZE, &reply); + ret = h16_send_at(s, ANE_FW_IO, command, ANE_H16_LOAD_SIZE, + h16_record_at(s, p, false), &reply); if (!ret) ret = ane_h16_load_reply(reply, ANE_H16_LOAD_SIZE, command, &p->key.program); if (!ret) { @@ -1924,7 +2103,8 @@ static int h16_replay(struct ane_h16_session *s, struct ane_h16_program *p) ret = ane_h16_create_encode(command, ANE_H16_CREATE_SIZE, p->key.program); } if (!ret) - ret = h16_send(s, ANE_FW_IO, command, ANE_H16_CREATE_SIZE, &reply); + ret = h16_send_at(s, ANE_FW_IO, command, ANE_H16_CREATE_SIZE, + h16_record_at(s, p, true), &reply); if (!ret) ret = ane_h16_create_reply(reply, ANE_H16_CREATE_SIZE, p->key.program, &p->key.process); diff --git a/drivers/accel/ane/ane_h16_program.h b/drivers/accel/ane/ane_h16_program.h index c22919a79349c..bb30d3dce8c20 100644 --- a/drivers/accel/ane/ane_h16_program.h +++ b/drivers/accel/ane/ane_h16_program.h @@ -2,6 +2,7 @@ #ifndef __ANE_H16_PROGRAM_H__ #define __ANE_H16_PROGRAM_H__ +#include #include #include #include @@ -30,6 +31,14 @@ #define ANE_H16_UNLOAD_SIZE 0xc #define ANE_H16_GATE_SIZE 0xc #define ANE_H16_REPORT_MAX 0x100 +/* + * One program's LOAD and CREATE, which stay in the command arena while the + * program lives, in a record of their own. 128 records take 136 KiB of the + * 256 KiB arena and leave the rest to EXECUTE batches and teardown. + */ +#define ANE_H16_RECORD_SIZE (ALIGN(ANE_H16_LOAD_SIZE, ANE_FW_SLOT_SIZE) + \ + ALIGN(ANE_H16_CREATE_SIZE, ANE_FW_SLOT_SIZE)) +#define ANE_H16_RECORDS 128 /* First value of the firmware mcache DSID pool (custom-BAR facts, 4). */ #define ANE_H16_MCACHE_DSID 36 @@ -150,10 +159,22 @@ 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); -/* Private, pinned mappings in the firmware domain. Never released before reset. */ +/* + * Private, pinned mappings in the firmware domain. Without unmap they are + * never released before reset. + */ struct ane_h16_session_ops { int (*map)(void *ctx, size_t size, bool device_write, struct ane_fw_surface *surface); + /* + * Optional. Give back a surface map() returned: unmap it from the + * engine, free its memory and its DVA range. The session calls it + * only for memory the firmware no longer uses (UNLOAD acknowledged, + * the program lost to a new firmware, or never seen by the firmware), + * only while the engine is up (not asleep, not parked), and with the + * session lock held. + */ + void (*unmap)(void *ctx, const struct ane_fw_surface *surface); void (*log)(void *ctx, const char *item, u64 a, u64 b); /* * Optional. For a surface with a handle: before the engine uses it @@ -236,6 +257,8 @@ struct ane_h16_program { struct list_head deferred; /* Firmware boot the program was loaded in; an older one means lost. */ u64 epoch; + /* Its LOAD and CREATE record in the session's arena range, or -1. */ + int record; /* In the session's list of programs to replay after a cold boot. */ struct list_head node; }; @@ -286,11 +309,15 @@ void ane_h16_exec_stats_add(struct ane_h16_exec_stats *st, */ /* * Program memory given back by a program that the firmware unloaded. It - * stays mapped until reset like every firmware-visible mapping, and serves - * a later program's mapping of the same size and permission instead of a - * new one, so loading the same model again and again uses no new memory. + * serves a later program's mapping of the same size and permission instead + * of a new one, so loading the same model again and again uses no new + * memory. With ops->unmap the oldest spares beyond ANE_H16_SPARES_KEEP + * entries or ANE_H16_SPARE_BYTES are freed, and all of them when a new + * mapping finds no room; without it, they stay mapped until reset. */ #define ANE_H16_SPARES 64 +#define ANE_H16_SPARES_KEEP 32 +#define ANE_H16_SPARE_BYTES SZ_128M struct ane_h16_spare { struct ane_fw_surface surface; @@ -305,8 +332,10 @@ struct ane_h16_session { struct ane_h16_pending_table pending; /* The last loaded program. */ struct ane_h16_program *program; + /* Oldest first. */ struct ane_h16_spare spares[ANE_H16_SPARES]; unsigned int spare_count; + size_t spare_bytes; /* Quiet time that ends serving the SHAREDMALLOC ring after UNLOAD. */ u64 settle_ns; /* @@ -349,6 +378,11 @@ struct ane_h16_session { bool pipelined; /* Reclaim a batch of command buffers only after its last caller ends. */ size_t execute_mark; + /* + * Records in use in the transport's placed range (ane_h16_program + * record), cleared when the firmware boots again. + */ + DECLARE_BITMAP(records, ANE_H16_RECORDS); /* Firmware log lines printed after the last failed EXECUTE. */ unsigned int fwlog_lines; bool quarantined; diff --git a/drivers/accel/ane/ane_h16_program_test.c b/drivers/accel/ane/ane_h16_program_test.c index cf494e1a1c0ea..91963fad00a29 100644 --- a/drivers/accel/ane/ane_h16_program_test.c +++ b/drivers/accel/ane/ane_h16_program_test.c @@ -80,6 +80,13 @@ struct prog_fw { /* ops->hung calls and the error of the last one. */ unsigned int hung; int hung_error; + /* + * Bytes mapped and not unmapped, ops->unmap calls, the most bytes a + * nonzero window lets be mapped at once (-ENOSPC beyond), and the + * number of the map call that fails with -ENOMEM (0: none). + */ + u64 live, window; + unsigned int unmaps, map_fail_at; }; static u64 fw_load(void *ctx, const void *p) @@ -328,6 +335,12 @@ static int fw_map(void *ctx, size_t size, bool write, struct ane_fw_surface *s) { struct prog_fw *f = ctx; + if (f->window && size > f->window - f->live) + return -ENOSPC; + if (f->map_fail_at && f->maps + 1 == f->map_fail_at) { + f->map_fail_at = 0; + return -ENOMEM; + } *s = (struct ane_fw_surface) { .cpu = fw_zalloc(f->test, size), .dva = f->next_dva, @@ -339,9 +352,21 @@ static int fw_map(void *ctx, size_t size, bool write, struct ane_fw_surface *s) f->next_dva += size; f->maps++; f->writable_maps += write; + f->live += size; return 0; } +static void fw_unmap(void *ctx, const struct ane_fw_surface *s) +{ + struct prog_fw *f = ctx; + + KUNIT_EXPECT_FALSE(f->test, f->s.asleep || f->s.parked); + KUNIT_EXPECT_GE(f->test, f->live, (u64)s->size); + f->live -= s->size; + f->unmaps++; + kunit_release_action(f->test, fw_kvfree, s->cpu); +} + static void fw_log(void *ctx, const char *item, u64 a, u64 b) { struct prog_fw *f = ctx; @@ -399,6 +424,16 @@ static void fw_hung(void *ctx, int error) f->hung_error = error; } +/* As fw_session_ops, with a backend that frees what the session gives back. */ +static const struct ane_h16_session_ops fw_session_ops_unmap = { + .map = fw_map, + .unmap = fw_unmap, + .log = fw_log, + .busy = fw_busy, + .sync = fw_sync, + .hung = fw_hung, +}; + /* As fw_session_ops, with a backend that recovers a quarantined session. */ static const struct ane_h16_session_ops fw_session_ops_hung = { .map = fw_map, @@ -2809,7 +2844,530 @@ static void program_hung_recovery_test(struct kunit *test) ane_h16_session_put(&f->s, p); } +/* Load, run, close and free one program. */ +static int fw_cycle(struct prog_fw *f) +{ + struct ane_h16_program *p = NULL; + int ret = fw_load_program(f, &p); + + if (ret) + return ret; + ret = ane_h16_session_execute(&f->s, p); + if (!ret) + ret = ane_h16_session_close(&f->s, p); + if (!ret) + ane_h16_session_put(&f->s, p); + return ret; +} + +/* Records the session holds in its placed range. */ +static unsigned int fw_records(struct prog_fw *f) +{ + return bitmap_weight(f->s.records, ANE_H16_RECORDS); +} + +/* The LOAD and CREATE bytes of one program's record, as the firmware sees them. */ +static const u8 *fw_record(struct prog_fw *f, const struct ane_h16_program *p) +{ + return (const u8 *)f->pool.cpu + f->t.placed + p->record * ANE_H16_RECORD_SIZE; +} + +/* + * One client's load/close cycles give back all of their arena: LOAD and + * CREATE through their record, the rest at the arena's end. A quarantined + * session keeps both. + */ +static void program_pool_rewind_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *p = NULL; + size_t base, kept; + unsigned int i; + + KUNIT_ASSERT_NOT_NULL(test, f); + base = ane_fw_transport_mark(&f->t); + KUNIT_ASSERT_EQ(test, fw_cycle(f), 0); + /* The first LOAD of a firmware boot takes the records, once. */ + KUNIT_EXPECT_EQ(test, f->t.placed, base); + KUNIT_EXPECT_EQ(test, f->t.placed_size, (size_t)ANE_H16_RECORDS * ANE_H16_RECORD_SIZE); + base = ane_fw_transport_mark(&f->t); + KUNIT_EXPECT_EQ(test, base, f->t.placed + f->t.placed_size); + for (i = 0; i < 1000; i++) { + KUNIT_ASSERT_EQ(test, fw_cycle(f), 0); + KUNIT_ASSERT_EQ(test, ane_fw_transport_mark(&f->t), base); + KUNIT_ASSERT_EQ(test, fw_records(f), 0U); + } + + /* A quarantined session keeps the arena and the record as they are. */ + KUNIT_ASSERT_EQ(test, fw_load_program(f, &p), 0); + KUNIT_EXPECT_EQ(test, p->record, 0); + kept = ane_fw_transport_mark(&f->t); + f->s.quarantined = true; + KUNIT_EXPECT_EQ(test, ane_h16_session_close(&f->s, p), -EIO); + KUNIT_EXPECT_EQ(test, ane_fw_transport_mark(&f->t), kept); + KUNIT_EXPECT_EQ(test, fw_records(f), 1U); + KUNIT_EXPECT_FALSE(test, list_empty(&f->s.held)); +} + +/* + * One client keeps a program loaded and runs it while another loads, runs + * and closes far more programs than the arena held before records (about + * 210): no cycle leaves anything behind, the resident program's LOAD and + * CREATE stay untouched, and its EXECUTEs keep working throughout. + */ +static void program_pool_resident_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *a = NULL, *b; + u8 *record = kunit_kzalloc(test, ANE_H16_RECORD_SIZE, GFP_KERNEL); + size_t base; + unsigned int i; + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, record); + f->strict = true; + KUNIT_ASSERT_EQ(test, fw_load_program(f, &a), 0); + KUNIT_ASSERT_EQ(test, ane_h16_session_execute(&f->s, a), 0); + memcpy(record, fw_record(f, a), ANE_H16_RECORD_SIZE); + base = ane_fw_transport_mark(&f->t); + for (i = 0; i < 1000; i++) { + b = NULL; + KUNIT_ASSERT_EQ(test, fw_load_program(f, &b), 0); + /* The lowest free record: the resident program holds record 0. */ + KUNIT_ASSERT_EQ(test, b->record, 1); + KUNIT_ASSERT_EQ(test, ane_h16_session_execute(&f->s, b), 0); + KUNIT_ASSERT_EQ(test, ane_h16_session_execute(&f->s, a), 0); + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, b), 0); + ane_h16_session_put(&f->s, b); + KUNIT_ASSERT_EQ(test, ane_fw_transport_mark(&f->t), base); + KUNIT_ASSERT_EQ(test, fw_records(f), 1U); + } + KUNIT_EXPECT_EQ(test, memcmp(fw_record(f, a), record, ANE_H16_RECORD_SIZE), 0); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); + KUNIT_ASSERT_EQ(test, ane_h16_session_execute(&f->s, a), 0); + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, a), 0); + ane_h16_session_put(&f->s, a); + KUNIT_EXPECT_EQ(test, fw_records(f), 0U); + KUNIT_EXPECT_EQ(test, ane_fw_transport_mark(&f->t), base); +} + +/* + * Programs closed out of order free their records for the next LOADs, lowest + * first; the programs still loaded keep theirs and keep running. + */ +static void program_pool_records_reuse_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *p[24] = {}; + size_t base = 0; + unsigned int i, round; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->strict = true; + for (round = 0; round < 50; round++) { + for (i = 0; i < ARRAY_SIZE(p); i++) { + if (p[i]) + continue; + KUNIT_ASSERT_EQ(test, fw_load_program(f, &p[i]), 0); + if (!base) + base = ane_fw_transport_mark(&f->t); + } + for (i = 0; i < ARRAY_SIZE(p); i++) + KUNIT_ASSERT_EQ(test, p[i]->record, (int)i); + KUNIT_ASSERT_EQ(test, fw_records(f), (unsigned int)ARRAY_SIZE(p)); + /* Close a different pattern each round: every 2nd, 3rd, ... program. */ + for (i = round % 5; i < ARRAY_SIZE(p); i += 2 + round % 4) { + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, p[i]), 0); + ane_h16_session_put(&f->s, p[i]); + p[i] = NULL; + } + for (i = 0; i < ARRAY_SIZE(p); i++) + if (p[i]) + KUNIT_ASSERT_EQ(test, ane_h16_session_execute(&f->s, p[i]), 0); + KUNIT_ASSERT_EQ(test, ane_fw_transport_mark(&f->t), base); + } + for (i = 0; i < ARRAY_SIZE(p); i++) { + if (!p[i]) + continue; + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, p[i]), 0); + ane_h16_session_put(&f->s, p[i]); + } + KUNIT_EXPECT_EQ(test, fw_records(f), 0U); + KUNIT_EXPECT_EQ(test, ane_fw_transport_mark(&f->t), base); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); +} + +/* + * With every record in use a LOAD is a clean -ENOSPC: nothing is sent or + * mapped and the session stays usable. The records taken by other programs + * are faked: the fake firmware holds 32 programs at most. + */ +static void program_pool_records_full_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *p = NULL, *q = NULL; + unsigned int sent, maps; + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_EQ(test, fw_cycle(f), 0); + bitmap_set(f->s.records, 0, ANE_H16_RECORDS - 1); + KUNIT_ASSERT_EQ(test, fw_load_program(f, &p), 0); + KUNIT_EXPECT_EQ(test, p->record, ANE_H16_RECORDS - 1); + KUNIT_EXPECT_TRUE(test, bitmap_full(f->s.records, ANE_H16_RECORDS)); + sent = f->count; + maps = f->maps; + KUNIT_EXPECT_EQ(test, fw_load_program(f, &q), -ENOSPC); + KUNIT_EXPECT_NULL(test, q); + KUNIT_EXPECT_EQ(test, f->count, sent); + KUNIT_EXPECT_EQ(test, f->maps, maps); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); + /* EXECUTE does not use records: it still runs. */ + KUNIT_EXPECT_EQ(test, ane_h16_session_execute(&f->s, p), 0); + + /* A freed record in the middle serves the next LOAD, in place. */ + __clear_bit(40, f->s.records); + KUNIT_ASSERT_EQ(test, fw_load_program(f, &q), 0); + KUNIT_EXPECT_EQ(test, q->record, 40); + /* The firmware read this LOAD from the record. */ + KUNIT_EXPECT_EQ(test, memcmp(f->load + 8, fw_record(f, q) + 8, + ANE_H16_LOAD_ID_OFFSET - 8), 0); + KUNIT_EXPECT_EQ(test, ane_h16_session_execute(&f->s, q), 0); + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, q), 0); + ane_h16_session_put(&f->s, q); + KUNIT_EXPECT_FALSE(test, test_bit(40, f->s.records)); + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, p), 0); + ane_h16_session_put(&f->s, p); + KUNIT_EXPECT_EQ(test, fw_records(f), (unsigned int)ANE_H16_RECORDS - 2); +} + +/* No room for the records at the first LOAD: a clean -ENOSPC, nothing sent. */ +static void program_pool_records_no_room_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *p = NULL; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->t.pool_used = f->t.pool.size - ANE_H16_RECORDS * ANE_H16_RECORD_SIZE + 1; + KUNIT_EXPECT_EQ(test, fw_load_program(f, &p), -ENOSPC); + KUNIT_EXPECT_EQ(test, f->count, 0U); + KUNIT_EXPECT_EQ(test, f->t.placed_size, (size_t)0); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); + /* Once the arena's end came back, the records fit. */ + f->t.pool_used = SZ_16K; + KUNIT_ASSERT_EQ(test, fw_load_program(f, &p), 0); + KUNIT_EXPECT_EQ(test, f->t.placed, (size_t)SZ_16K); + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, p), 0); + ane_h16_session_put(&f->s, p); +} + +/* Placed commands stay inside the placed range; releases stay above it. */ +static void program_pool_placed_bounds_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + u8 command[ANE_H16_UNLOAD_SIZE] = {}; + size_t end; + + KUNIT_ASSERT_NOT_NULL(test, f); + put_unaligned_le16(ANE_H16_UNLOAD_PROGRAM, command + 4); + /* Nothing is placed before the range exists. */ + KUNIT_EXPECT_EQ(test, ane_fw_transport_publish_at(&f->t, ANE_FW_IO, command, + sizeof(command), U64_MAX, SZ_16K), + -EINVAL); + KUNIT_EXPECT_EQ(test, ane_fw_transport_reserve(&f->t, 100), -EINVAL); + KUNIT_ASSERT_EQ(test, ane_fw_transport_reserve(&f->t, SZ_4K), 0); + KUNIT_EXPECT_EQ(test, ane_fw_transport_reserve(&f->t, SZ_4K), -EINVAL); + end = f->t.placed + f->t.placed_size; + KUNIT_EXPECT_EQ(test, ane_fw_transport_mark(&f->t), end); + KUNIT_EXPECT_EQ(test, ane_fw_transport_publish_at(&f->t, ANE_FW_IO, command, + sizeof(command), U64_MAX, + f->t.placed + 8), + -EINVAL); + KUNIT_EXPECT_EQ(test, ane_fw_transport_publish_at(&f->t, ANE_FW_IO, command, + sizeof(command), U64_MAX, + f->t.placed - ANE_FW_SLOT_SIZE), + -EINVAL); + KUNIT_EXPECT_EQ(test, ane_fw_transport_publish_at(&f->t, ANE_FW_IO, command, + sizeof(command), U64_MAX, end), + -EINVAL); + KUNIT_EXPECT_EQ(test, ane_fw_transport_publish_at(&f->t, ANE_FW_IO, command, + ANE_FW_SLOT_SIZE + 8, U64_MAX, + end - ANE_FW_SLOT_SIZE), + -EINVAL); + KUNIT_EXPECT_EQ(test, f->count, 0U); + /* The last slot of the range is fine, and leaves the arena's end alone. */ + KUNIT_EXPECT_EQ(test, ane_fw_transport_publish_at(&f->t, ANE_FW_IO, command, + sizeof(command), U64_MAX, + end - ANE_FW_SLOT_SIZE), + 0); + KUNIT_EXPECT_EQ(test, ane_fw_transport_wait(&f->t, ANE_FW_IO), 0); + KUNIT_EXPECT_EQ(test, f->count, 1U); + KUNIT_EXPECT_EQ(test, ane_fw_transport_mark(&f->t), end); + KUNIT_EXPECT_EQ(test, ane_fw_transport_release(&f->t, end - 1), -EINVAL); + KUNIT_EXPECT_EQ(test, ane_fw_transport_release(&f->t, end), 0); +} + +/* + * After a cold boot the replayed programs take records again from the + * start; a program closed while asleep, lost with the old firmware, does + * not free the record a replayed program now holds. + */ +static void program_pool_records_restart_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *a = NULL, *b = NULL; + struct mutex *lock = kunit_kzalloc(test, sizeof(*lock), GFP_KERNEL); + unsigned int replayed; + u64 first; + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, lock); + mutex_init(lock); + f->s.lock = lock; + KUNIT_ASSERT_EQ(test, fw_load_program(f, &a), 0); + KUNIT_ASSERT_EQ(test, fw_load_program(f, &b), 0); + KUNIT_EXPECT_EQ(test, a->record, 0); + KUNIT_EXPECT_EQ(test, b->record, 1); + ane_h16_session_sleep(&f->s, true); + KUNIT_EXPECT_EQ(test, ane_h16_session_close(&f->s, a), -EINPROGRESS); + fw_cold_boot(f); + ane_h16_session_restart(&f->s); + KUNIT_EXPECT_EQ(test, fw_records(f), 0U); + KUNIT_ASSERT_EQ(test, ane_h16_session_replay(&f->s, &replayed, &first), 0); + KUNIT_EXPECT_EQ(test, replayed, 1U); + KUNIT_EXPECT_EQ(test, b->record, 0); + /* The deferred close of the lost program runs here. */ + ane_h16_session_sleep(&f->s, false); + KUNIT_EXPECT_TRUE(test, test_bit(0, f->s.records)); + KUNIT_EXPECT_EQ(test, fw_records(f), 1U); + mutex_lock(lock); + KUNIT_EXPECT_EQ(test, ane_h16_session_execute(&f->s, b), 0); + KUNIT_EXPECT_EQ(test, ane_h16_session_close(&f->s, b), 0); + mutex_unlock(lock); + ane_h16_session_put(&f->s, b); + KUNIT_EXPECT_EQ(test, fw_records(f), 0U); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); +} + +/* + * Memory a program of another shape gave back: no later program of these + * tests maps a surface of this size, so only freeing it makes room. + */ +static bool fw_foreign_spare(struct prog_fw *f, size_t size) +{ + struct ane_fw_surface surface; + + if (f->s.spare_count == ANE_H16_SPARES || fw_map(f, size, false, &surface)) + return false; + f->s.spares[f->s.spare_count++] = (struct ane_h16_spare) { .surface = surface }; + f->s.spare_bytes += size; + return true; +} + +/* Larger than any region or section of the fixture programs. */ +#define FW_FOREIGN(i) (SZ_16K * (9 + (i) % 3)) +#define FW_WINDOW SZ_16M + +/* + * Programs of ever new shapes leave spares that no later program can use. + * Without ops->unmap a spare table full of them makes every close leave its + * memory mapped and unused, the DVA window fills, and every LOAD fails with + * -ENOSPC from then on, until reset. With it, the session frees the oldest + * spares beyond its limits: thousands of programs load into a window that + * holds a few dozen, with a new shape left behind before each. + */ +static void program_unmap_window_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test), *g = fw_create(test); + unsigned int i; + int ret = 0; + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, g); + f->window = FW_WINDOW; + for (i = 0; i < ANE_H16_SPARES; i++) + KUNIT_ASSERT_TRUE(test, fw_foreign_spare(f, FW_FOREIGN(i))); + for (i = 0; i < 2000 && !ret; i++) + ret = fw_cycle(f); + KUNIT_EXPECT_EQ(test, ret, -ENOSPC); + KUNIT_EXPECT_LT(test, i, 2000U); + KUNIT_EXPECT_EQ(test, f->unmaps, 0U); + + g->s.ops = &fw_session_ops_unmap; + g->window = FW_WINDOW; + for (i = 0; i < ANE_H16_SPARES; i++) + KUNIT_ASSERT_TRUE(test, fw_foreign_spare(g, FW_FOREIGN(i))); + for (i = 0; i < 2000; i++) { + fw_foreign_spare(g, FW_FOREIGN(i)); + KUNIT_ASSERT_EQ(test, fw_cycle(g), 0); + KUNIT_ASSERT_LE(test, g->s.spare_count, ANE_H16_SPARES_KEEP); + } + KUNIT_EXPECT_GE(test, g->unmaps, 2000U); + KUNIT_EXPECT_FALSE(test, g->s.quarantined); + KUNIT_EXPECT_PTR_EQ(test, g->s.program, NULL); + /* What the spares hold is all that stays mapped. */ + KUNIT_EXPECT_EQ(test, g->live, (u64)g->s.spare_bytes); +} + +/* A mapping that finds no room frees every spare and tries again. */ +static void program_unmap_pressure_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + unsigned int i; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->s.ops = &fw_session_ops_unmap; + f->window = SZ_2M; + for (i = 0; fw_foreign_spare(f, FW_FOREIGN(i)); i++) + ; + /* Not even the smallest mapping fits beside them. */ + f->window = f->live; + KUNIT_ASSERT_GT(test, i, 0U); + KUNIT_ASSERT_LT(test, i, ANE_H16_SPARES_KEEP); + KUNIT_ASSERT_EQ(test, fw_cycle(f), 0); + KUNIT_EXPECT_EQ(test, f->unmaps, i); + KUNIT_EXPECT_EQ(test, f->live, (u64)f->s.spare_bytes); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); +} + +static u8 fw_dummy[SZ_16K]; + +/* The spares stay within their byte limit too, freeing the oldest first. */ +static void program_unmap_bytes_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + const size_t half = ANE_H16_SPARE_BYTES / 2; + u64 newest = 0; + unsigned int i; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->s.ops = &fw_session_ops_unmap; + /* Large spares with no memory behind them; never taken, only freed. */ + for (i = 0; i < 3; i++) { + newest = f->next_dva; + f->s.spares[f->s.spare_count++] = (struct ane_h16_spare) { + .surface = { .cpu = fw_dummy, .dva = newest, .size = half }, + }; + f->s.spare_bytes += half; + f->next_dva += half; + f->live += half; + } + KUNIT_ASSERT_EQ(test, fw_cycle(f), 0); + KUNIT_EXPECT_LE(test, f->s.spare_bytes, (size_t)ANE_H16_SPARE_BYTES); + KUNIT_EXPECT_EQ(test, f->live, (u64)f->s.spare_bytes); + KUNIT_EXPECT_EQ(test, f->unmaps, 2U); + for (i = 0; i < f->s.spare_count; i++) + if (f->s.spares[i].surface.size == half) + KUNIT_EXPECT_EQ(test, f->s.spares[i].surface.dva, newest); +} + +/* + * Nothing is freed while the engine is parked: the DART invalidation of an + * unmap could not reach it. The spares keep the memory until the next LOAD, + * which runs with the engine up. + */ +static void program_unmap_parked_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *p = NULL; + unsigned int i; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->s.ops = &fw_session_ops_unmap; + KUNIT_ASSERT_EQ(test, fw_load_program(f, &p), 0); + for (i = 0; i < ANE_H16_SPARES_KEEP + 8; i++) + KUNIT_ASSERT_TRUE(test, fw_foreign_spare(f, SZ_16K * 3)); + f->s.parked = true; + KUNIT_ASSERT_EQ(test, ane_h16_session_close(&f->s, p), 0); + ane_h16_session_put(&f->s, p); + KUNIT_EXPECT_EQ(test, f->unmaps, 0U); + KUNIT_EXPECT_GT(test, f->s.spare_count, ANE_H16_SPARES_KEEP); + f->s.parked = false; + KUNIT_ASSERT_EQ(test, fw_cycle(f), 0); + KUNIT_EXPECT_GT(test, f->unmaps, 0U); + KUNIT_EXPECT_LE(test, f->s.spare_count, ANE_H16_SPARES_KEEP); +} + +/* + * A LOAD that fails before the firmware saw the program (here, no memory + * for its third mapping) gives back what it mapped and frees the program. + */ +static void program_unmap_failed_map_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *p = NULL; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->s.ops = &fw_session_ops_unmap; + f->map_fail_at = 3; + KUNIT_EXPECT_EQ(test, fw_load_program(f, &p), -ENOMEM); + KUNIT_EXPECT_PTR_EQ(test, p, NULL); + KUNIT_EXPECT_PTR_EQ(test, f->s.program, NULL); + KUNIT_EXPECT_EQ(test, f->count, 0U); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); + KUNIT_EXPECT_EQ(test, f->s.spare_count, 2U); + KUNIT_EXPECT_EQ(test, f->live, (u64)f->s.spare_bytes); + /* The next LOAD takes them. */ + KUNIT_ASSERT_EQ(test, fw_load_program(f, &p), 0); + KUNIT_EXPECT_EQ(test, f->s.spare_count, 0U); +} + +/* + * A LOAD the firmware refused quarantines the session; the program is held + * with those closed while quarantined, and the verified power cycle of the + * recovery gives its memory back. + */ +static void program_unmap_refused_load_test(struct kunit *test) +{ + struct prog_fw *f = fw_create(test); + struct ane_h16_program *p = NULL; + struct mutex *lock = kunit_kzalloc(test, sizeof(*lock), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, lock); + mutex_init(lock); + f->s.ops = &fw_session_ops_unmap; + f->mode = FW_LOAD_NAK; + KUNIT_EXPECT_NE(test, fw_load_program(f, &p), 0); + KUNIT_EXPECT_PTR_EQ(test, p, NULL); + KUNIT_EXPECT_TRUE(test, f->s.quarantined); + KUNIT_EXPECT_EQ(test, f->hung, 1U); + KUNIT_EXPECT_FALSE(test, list_empty(&f->s.held)); + KUNIT_EXPECT_EQ(test, f->s.spare_count, 0U); + + f->s.lock = lock; + ane_h16_session_sleep(&f->s, true); + fw_cold_boot(f); + /* The backend's cold boot starts a new transport. */ + memset(f->head, 0, sizeof(f->head)); + KUNIT_ASSERT_EQ(test, ane_fw_transport_init(&f->t, &f->soc, &fw_ops, f, f->ipc, f->pool, + SZ_16K, f->ipc.dva, ANE_FW_TABLE_SIZE), 0); + KUNIT_ASSERT_EQ(test, ane_fw_transport_arm(&f->t), 0); + f->mode = FW_OK; + ane_h16_session_restart(&f->s); + ane_h16_session_recovered(&f->s); + ane_h16_session_sleep(&f->s, false); + KUNIT_EXPECT_TRUE(test, list_empty(&f->s.held)); + KUNIT_EXPECT_PTR_EQ(test, f->s.program, NULL); + KUNIT_EXPECT_GT(test, f->s.spare_count, 0U); + KUNIT_EXPECT_EQ(test, f->live, (u64)f->s.spare_bytes); + f->s.lock = NULL; + KUNIT_EXPECT_EQ(test, fw_cycle(f), 0); +} + static struct kunit_case program_cases[] = { + KUNIT_CASE_SLOW(program_pool_rewind_test), + KUNIT_CASE_SLOW(program_pool_resident_test), + KUNIT_CASE_SLOW(program_pool_records_reuse_test), + KUNIT_CASE(program_pool_records_full_test), + KUNIT_CASE(program_pool_records_no_room_test), + KUNIT_CASE(program_pool_placed_bounds_test), + KUNIT_CASE(program_pool_records_restart_test), + KUNIT_CASE_SLOW(program_unmap_window_test), + KUNIT_CASE(program_unmap_pressure_test), + KUNIT_CASE(program_unmap_bytes_test), + KUNIT_CASE(program_unmap_parked_test), + KUNIT_CASE(program_unmap_failed_map_test), + KUNIT_CASE(program_unmap_refused_load_test), KUNIT_CASE(program_hung_recovery_test), KUNIT_CASE(program_pipeline_out_of_order_test), KUNIT_CASE(program_pipeline_same_program_test),