diff --git a/MAINTAINERS b/MAINTAINERS index e0a0070243ae6..335a1a32860a3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2830,6 +2830,8 @@ S: Maintained W: https://aurora-silicon.github.io F: Documentation/devicetree/bindings/media/apple,prores.yaml F: drivers/media/platform/apple/prores/ +F: tools/asahi/prores-enc.c +F: tools/asahi/prores-queue-tests.py ARM/APPLE QSPIMC DRIVER M: Ryan Murray diff --git a/drivers/media/platform/apple/prores/apple-prores-drv.c b/drivers/media/platform/apple/prores/apple-prores-drv.c index 1fa95ce3bdbe6..58cfa07ffa367 100644 --- a/drivers/media/platform/apple/prores/apple-prores-drv.c +++ b/drivers/media/platform/apple/prores/apple-prores-drv.c @@ -2,8 +2,8 @@ /* * Apple ProRes (APR) V4L2 memory-to-memory encoder. * - * One job runs at a time. The driver writes a descriptor into a 128 KiB DMA - * ring, advances the producer head and waits for the completion interrupt. + * Publish a bounded batch of up to four descriptors in a 128 KiB DMA ring. + * Keep the batch mapped until each descriptor has an acknowledged DONE. * Raw frames come in on the OUTPUT queue as semi-planar 4:2:2 or 4:4:4 (8 or * 16 bits per sample) and complete ProRes frames go out on CAPTURE. * @@ -46,7 +46,34 @@ #define APR_TIMEOUT_MS 1000 #define APR_POLL_MS 4 #define APR_AUTOSUSPEND_MS 500 -#define APR_RING_ENTRIES (APR_RING_BYTES / APR_DESC_BYTES) +#define APR_MAX_DEPTH 4 + +/* Snapshot at batch start; writes affect the next batch only. */ +static unsigned int apr_depth = 1; + +static int apr_set_depth(const char *val, const struct kernel_param *kp) +{ + unsigned int depth; + int ret = kstrtouint(val, 0, &depth); + + if (ret) + return ret; + if (!depth || depth > APR_MAX_DEPTH) + return -EINVAL; + WRITE_ONCE(*(unsigned int *)kp->arg, depth); + return 0; +} + +static const struct kernel_param_ops apr_depth_ops = { + .set = apr_set_depth, + .get = param_get_uint, +}; +module_param_cb(depth, &apr_depth_ops, &apr_depth, 0644); +MODULE_PARM_DESC(depth, "Hardware descriptors per batch (1-4, default 1); requires one DONE per job"); + +struct apr_job { + struct vb2_v4l2_buffer *src, *dst; +}; struct apr_fmt { u32 fourcc; @@ -213,10 +240,14 @@ struct apr_dev { size_t stats_size; spinlock_t hw_lock; /* protects the fields below */ - struct apr_ctx *active; /* job in the engine, or NULL */ + struct apr_ctx *active; /* owns the m2m slot until every buffer is returned */ + struct apr_job jobs[APR_MAX_DEPTH]; + struct apr_batch batch; + bool recovering; + bool quarantined; /* reset failed: retain every DMA owner until reboot */ u32 head; /* next free ring offset */ bool ring_ready; /* engine registers programmed since power-up */ - bool broken; /* timed out with no way to reset */ + bool broken; /* machine reset required before further submission */ unsigned long started; /* jiffies at submission */ struct delayed_work watchdog; }; @@ -386,35 +417,39 @@ static void apr_finish_job(struct apr_dev *apr, struct apr_ctx *ctx, int err) struct vb2_v4l2_buffer *src, *dst; u32 bytes = 0; - src = v4l2_m2m_src_buf_remove(m2m); - dst = v4l2_m2m_dst_buf_remove(m2m); - if (WARN_ON(!src || !dst)) - goto out; + /* The m2m slot and runtime PM reference cover the entire batch. */ + for (unsigned int i = 0; i < apr->batch.count; i++) { + src = v4l2_m2m_src_buf_remove(m2m); + dst = v4l2_m2m_dst_buf_remove(m2m); + if (WARN_ON(!src || !dst)) + break; + bytes = 0; - if (!err) { - u8 *out = vb2_plane_vaddr(&dst->vb2_buf, 0); + if (!err) { + u8 *out = vb2_plane_vaddr(&dst->vb2_buf, 0); - /* The engine wrote the frame through the DART; see it now. */ - dma_rmb(); - err = out ? apr_check_output(out, vb2_plane_size(&dst->vb2_buf, 0), - &bytes) : -EIO; - if (err) - dev_err_ratelimited(apr->dev, "engine returned no frame\n"); - } - vb2_set_plane_payload(&dst->vb2_buf, 0, err ? 0 : bytes); - v4l2_m2m_buf_copy_metadata(src, dst); - dst->sequence = ctx->sequence++; - dst->flags |= V4L2_BUF_FLAG_KEYFRAME; - if (v4l2_m2m_is_last_draining_src_buf(m2m, src)) { - static const struct v4l2_event eos = { .type = V4L2_EVENT_EOS }; - - dst->flags |= V4L2_BUF_FLAG_LAST; - v4l2_event_queue_fh(&ctx->fh, &eos); - v4l2_m2m_mark_stopped(m2m); + /* The engine wrote the frame through the DART; see it now. */ + dma_rmb(); + err = out ? apr_check_output(out, vb2_plane_size(&dst->vb2_buf, 0), + &bytes) : -EIO; + if (err) + dev_err_ratelimited(apr->dev, "engine returned no frame\n"); + } + vb2_set_plane_payload(&dst->vb2_buf, 0, err ? 0 : bytes); + v4l2_m2m_buf_copy_metadata(src, dst); + dst->sequence = ctx->sequence++; + dst->flags |= V4L2_BUF_FLAG_KEYFRAME; + if (v4l2_m2m_is_last_draining_src_buf(m2m, src)) { + static const struct v4l2_event eos = { .type = V4L2_EVENT_EOS }; + + dst->flags |= V4L2_BUF_FLAG_LAST; + v4l2_event_queue_fh(&ctx->fh, &eos); + v4l2_m2m_mark_stopped(m2m); + } + v4l2_m2m_buf_done(src, err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); + v4l2_m2m_buf_done(dst, err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); } - v4l2_m2m_buf_done(src, err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); - v4l2_m2m_buf_done(dst, err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); -out: + apr->batch.count = 0; pm_runtime_mark_last_busy(apr->dev); pm_runtime_put_autosuspend(apr->dev); v4l2_m2m_job_finish(apr->m2m_dev, m2m); @@ -424,26 +459,41 @@ static void apr_finish_job(struct apr_dev *apr, struct apr_ctx *ctx, int err) * Claim the active job if the engine has finished it. Returns the context to * finish, or NULL. Called with hw_lock held. */ -static struct apr_ctx *apr_take_done(struct apr_dev *apr, int *err) +static struct apr_ctx *apr_take_done(struct apr_dev *apr, int *err, u32 *status_out) { struct apr_ctx *ctx = apr->active; u32 status, tail; - if (!ctx) + *status_out = 0; + if (!ctx || apr->recovering || apr->quarantined) return NULL; status = apr_read(apr, APR_REG_IRQ_STATUS); if (!status) return NULL; + *status_out = status; apr_write(apr, APR_REG_IRQ_STATUS, status); + /* Flush the posted ACK before another status observation can count DONE. */ tail = apr_read(apr, APR_REG_RING_TAIL) & APR_RING_PTR_MASK; - *err = 0; - if (status != APR_IRQ_DONE || tail != apr->head) { + *err = apr_batch_complete(&apr->batch, status, tail); + if (*err < 0) { dev_err_ratelimited(apr->dev, "job failed: status %#x tail %#x head %#x st %#x/%#x\n", status, tail, apr->head, apr_read(apr, APR_REG_ST0), apr_read(apr, APR_REG_ST1)); - *err = -EIO; + apr->recovering = true; + apr_write(apr, APR_REG_IRQ_ENABLE, 0); + apr_write(apr, APR_REG_MODE, APR_MODE_OFF); + mod_delayed_work(system_wq, &apr->watchdog, 0); + return NULL; + } + dev_dbg(apr->dev, "completion: done %u/%u head %#x tail %#x status %#x\n", + apr->batch.done, apr->batch.count, apr->head, tail, status); + if (!*err) { + /* Keep the whole batch mapped, including completed frames. */ + apr->started = jiffies; + return NULL; } + *err = 0; apr->active = NULL; return ctx; } @@ -452,32 +502,34 @@ static irqreturn_t apr_irq(int irq, void *data) { struct apr_dev *apr = data; struct apr_ctx *ctx; + u32 status; int err = 0; spin_lock(&apr->hw_lock); - ctx = apr_take_done(apr, &err); + ctx = apr_take_done(apr, &err, &status); spin_unlock(&apr->hw_lock); if (!ctx) - return IRQ_NONE; + return status ? IRQ_HANDLED : IRQ_NONE; cancel_delayed_work(&apr->watchdog); apr_finish_job(apr, ctx, err); return IRQ_HANDLED; } /* - * Backstop for a lost interrupt and for hangs. A hung engine is reset - * through its power domain and reprogrammed; without a reset line the - * device refuses further jobs rather than reuse memory it may still own. + * Backstop for a lost interrupt and for hangs. Reset before returning the + * batch's DMA buffers. Do not submit again after a domain reset: the DART + * shares the domain and its translation state must first be restored. */ static void apr_watchdog(struct work_struct *work) { struct apr_dev *apr = container_of(work, struct apr_dev, watchdog.work); struct apr_ctx *ctx; unsigned long flags; + u32 status; int err = 0; spin_lock_irqsave(&apr->hw_lock, flags); - ctx = apr_take_done(apr, &err); + ctx = apr_take_done(apr, &err, &status); if (ctx) { spin_unlock_irqrestore(&apr->hw_lock, flags); dev_warn_once(apr->dev, "completion found without an interrupt\n"); @@ -485,87 +537,153 @@ static void apr_watchdog(struct work_struct *work) return; } ctx = apr->active; - if (!ctx) { + if (!ctx || apr->quarantined) { spin_unlock_irqrestore(&apr->hw_lock, flags); return; } - if (time_before(jiffies, apr->started + msecs_to_jiffies(APR_TIMEOUT_MS))) { + if (!apr->recovering && + time_before(jiffies, apr->started + msecs_to_jiffies(APR_TIMEOUT_MS))) { spin_unlock_irqrestore(&apr->hw_lock, flags); schedule_delayed_work(&apr->watchdog, msecs_to_jiffies(APR_POLL_MS)); return; } - dev_err(apr->dev, "job timed out: head %#x tail %#x st %#x/%#x\n", - apr->head, apr_read(apr, APR_REG_RING_TAIL), - apr_read(apr, APR_REG_ST0), apr_read(apr, APR_REG_ST1)); + if (!apr->recovering) + dev_err(apr->dev, "job timed out: head %#x tail %#x st %#x/%#x\n", + apr->head, apr_read(apr, APR_REG_RING_TAIL), + apr_read(apr, APR_REG_ST0), apr_read(apr, APR_REG_ST1)); apr->active = NULL; apr_write(apr, APR_REG_IRQ_ENABLE, 0); apr_write(apr, APR_REG_MODE, APR_MODE_OFF); apr->ring_ready = false; - /* No new job can start until this one is finished below. */ + /* A domain reset also loses the DART state: never submit after it. */ + apr->broken = true; + err = apr->recovering ? -EIO : -ETIMEDOUT; + apr->recovering = false; + /* No new job can start until this batch is finished below. */ spin_unlock_irqrestore(&apr->hw_lock, flags); + /* A reload cannot repair the shared DART state after this reset. */ + __module_get(THIS_MODULE); + get_device(apr->dev); if (!apr->reset || reset_control_reset(apr->reset)) { - dev_err(apr->dev, "engine cannot be reset, refusing further jobs\n"); + /* + * MODE_OFF alone is not a DMA-stop fence. Keep the m2m slot, + * ready buffers and PM reference; streamoff/close must not free + * their mappings. Pin the fixed platform device and driver until + * a machine reset. Manual unbind is disabled below for this case. + */ spin_lock_irqsave(&apr->hw_lock, flags); - apr->broken = true; + apr->active = ctx; + apr->quarantined = true; spin_unlock_irqrestore(&apr->hw_lock, flags); + dev_err(apr->dev, "reset failed: DMA buffers retained until machine reset\n"); + vb2_queue_error(v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx)); + vb2_queue_error(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx)); + return; } - apr_finish_job(apr, ctx, -ETIMEDOUT); + dev_err(apr->dev, "APR/DART domain reset: further jobs require a machine reset\n"); + apr_finish_job(apr, ctx, err); +} + +/* Called with hw_lock held, after all descriptors and outputs are prepared. */ +static void apr_publish_batch(struct apr_dev *apr, struct apr_ctx *ctx) +{ + apr->head = apr_batch_head(&apr->batch); + apr->active = ctx; + apr->started = jiffies; + dev_dbg(apr->dev, "publish: count %u start %#x head %#x\n", + apr->batch.count, apr->batch.start, apr->head); + dma_wmb(); + writel(apr->head, apr->regs + APR_REG_RING_HEAD); } +/* + * Keep the m2m job slot for a whole batch, including buffer callbacks. This + * lets m2m cancellation and system suspend wait for DMA ownership to end. + * Publish the entire bounded batch. The fetch tail is not a completion + * fence: retain every buffer until a separate acknowledged DONE has been + * observed for every descriptor. Coalesced interrupts fail closed through + * the watchdog; never infer missing completions from the fetch cursor. + */ static void apr_device_run(void *priv) { struct apr_ctx *ctx = priv; struct apr_dev *apr = ctx->apr; - struct vb2_v4l2_buffer *src, *dst; - struct apr_frame f; + struct v4l2_m2m_ctx *m2m = ctx->fh.m2m_ctx; + struct v4l2_m2m_buffer *buf; + unsigned int count, i; unsigned long flags; - void *out; int ret; - src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); - dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); - + count = min(v4l2_m2m_num_src_bufs_ready(m2m), v4l2_m2m_num_dst_bufs_ready(m2m)); + count = min(count, READ_ONCE(apr_depth)); + if (WARN_ON(!count)) { + v4l2_m2m_job_finish(apr->m2m_dev, m2m); + return; + } + apr->batch.count = count; + apr->batch.done = 0; + /* Keep ready-list membership until completion, including for STOP/EOS. */ + i = 0; + spin_lock_irqsave(&m2m->out_q_ctx.rdy_spinlock, flags); + v4l2_m2m_for_each_src_buf(m2m, buf) { + apr->jobs[i++].src = &buf->vb; + if (i == count) + break; + } + spin_unlock_irqrestore(&m2m->out_q_ctx.rdy_spinlock, flags); + i = 0; + spin_lock_irqsave(&m2m->cap_q_ctx.rdy_spinlock, flags); + v4l2_m2m_for_each_dst_buf(m2m, buf) { + apr->jobs[i++].dst = &buf->vb; + if (i == count) + break; + } + spin_unlock_irqrestore(&m2m->cap_q_ctx.rdy_spinlock, flags); ret = pm_runtime_resume_and_get(apr->dev); if (ret < 0) { + /* Balance the common completion path without claiming a resume. */ pm_runtime_get_noresume(apr->dev); - apr_finish_job(apr, ctx, ret); - return; - } - apr_fill_frame(ctx, src, dst, &f); - /* Mark the output invalid so a frame the engine never wrote is caught. */ - out = vb2_plane_vaddr(&dst->vb2_buf, 0); - if (!out) { - apr_finish_job(apr, ctx, -EINVAL); - return; + apr->batch.count = 1; + goto finish; } - memset(out, 0, 8); spin_lock_irqsave(&apr->hw_lock, flags); if (apr->broken) { spin_unlock_irqrestore(&apr->hw_lock, flags); - apr_finish_job(apr, ctx, -EIO); - return; + ret = -EIO; + apr->batch.count = 1; + goto finish; } - /* - * The engine's ring wrap protocol is not known, so start again at the - * beginning of the ring instead of wrapping. The engine is idle here. - */ - if (!apr->ring_ready || apr->head + APR_DESC_BYTES > APR_RING_BYTES) + /* Restart only while the engine owns no descriptor, before ring end. */ + if (!apr->ring_ready || apr->head + count * APR_DESC_BYTES > APR_RING_BYTES) apr_hw_init(apr); - ret = apr_build_desc(&f, apr->ring + apr->head); - if (ret) { - spin_unlock_irqrestore(&apr->hw_lock, flags); - apr_finish_job(apr, ctx, ret); - return; + apr->batch.start = apr->head; + spin_unlock_irqrestore(&apr->hw_lock, flags); + + for (i = 0; i < count; i++) { + struct apr_job *job = &apr->jobs[i]; + struct apr_frame f; + void *out; + + apr_fill_frame(ctx, job->src, job->dst, &f); + out = vb2_plane_vaddr(&job->dst->vb2_buf, 0); + if (!out) { + ret = -EINVAL; + goto finish; + } + memset(out, 0, 8); + ret = apr_build_desc(&f, apr->ring + apr->batch.start + i * APR_DESC_BYTES); + if (ret) + goto finish; } - apr->head += APR_DESC_BYTES; - apr->active = ctx; - apr->started = jiffies; - /* Descriptor and buffers must be visible before the head moves. */ - dma_wmb(); - writel(apr->head, apr->regs + APR_REG_RING_HEAD); + spin_lock_irqsave(&apr->hw_lock, flags); + apr_publish_batch(apr, ctx); spin_unlock_irqrestore(&apr->hw_lock, flags); schedule_delayed_work(&apr->watchdog, msecs_to_jiffies(APR_POLL_MS)); + return; + +finish: + apr_finish_job(apr, ctx, ret); } static const struct v4l2_m2m_ops apr_m2m_ops = { @@ -1184,6 +1302,8 @@ static struct platform_driver apr_driver = { .name = APR_NAME, .of_match_table = apr_of_match, .pm = pm_ptr(&apr_pm_ops), + /* A failed-reset quarantine must survive an attempted unbind. */ + .suppress_bind_attrs = true, }, }; module_platform_driver(apr_driver); diff --git a/drivers/media/platform/apple/prores/apple-prores-test.c b/drivers/media/platform/apple/prores/apple-prores-test.c index 15d13ec0ada82..f7e8ebd4b168f 100644 --- a/drivers/media/platform/apple/prores/apple-prores-test.c +++ b/drivers/media/platform/apple/prores/apple-prores-test.c @@ -244,6 +244,49 @@ static void apr_test_output(struct kunit *test) KUNIT_EXPECT_EQ(test, apr_check_output(out, SZ_4K, &bytes), -EIO); } +/* A fetch tail is allowed to reach the head before DONE is asserted. */ +static void apr_test_batch_fence(struct kunit *test) +{ + for (unsigned int depth = 1; depth <= 4; depth++) { + struct apr_batch batch = { .start = 0x600, .count = depth }; + + for (unsigned int i = 0; i < depth; i++) { + u32 head = 0x600 + depth * APR_DESC_BYTES; + + KUNIT_EXPECT_EQ(test, apr_batch_head(&batch), head); + KUNIT_EXPECT_EQ(test, apr_batch_complete(&batch, 0, head), -EAGAIN); + KUNIT_EXPECT_EQ(test, batch.done, i); + KUNIT_EXPECT_EQ(test, apr_batch_complete(&batch, APR_IRQ_DONE, head), + i + 1 == depth ? 1 : 0); + } + } +} + +static void apr_test_batch_coalesced(struct kunit *test) +{ + struct apr_batch batch = { .start = 0x600, .count = 4 }; + u32 head = apr_batch_head(&batch); + + KUNIT_EXPECT_EQ(test, apr_batch_complete(&batch, APR_IRQ_DONE, head), 0); + for (unsigned int i = 0; i < 4; i++) + KUNIT_EXPECT_EQ(test, apr_batch_complete(&batch, 0, head), -EAGAIN); + KUNIT_EXPECT_EQ(test, batch.done, 1U); +} + +static void apr_test_batch_errors(struct kunit *test) +{ + struct apr_batch batch = { .start = 0x600, .count = 4 }; + u32 head = apr_batch_head(&batch); + + KUNIT_EXPECT_EQ(test, apr_batch_complete(&batch, APR_IRQ_DONE, head - 0x80), -EIO); + KUNIT_EXPECT_EQ(test, apr_batch_complete(&batch, APR_IRQ_DONE, head + APR_DESC_BYTES), + -EIO); + KUNIT_EXPECT_EQ(test, apr_batch_complete(&batch, APR_IRQ_ERROR, head), -EIO); + KUNIT_EXPECT_EQ(test, apr_batch_complete(&batch, APR_IRQ_DONE | APR_IRQ_ERROR, head), + -EIO); + KUNIT_EXPECT_EQ(test, batch.done, 0U); +} + static struct kunit_case apr_test_cases[] = { KUNIT_CASE(apr_test_qmat_regs), KUNIT_CASE(apr_test_dc_scale), @@ -254,6 +297,9 @@ static struct kunit_case apr_test_cases[] = { KUNIT_CASE(apr_test_budgets), KUNIT_CASE(apr_test_slices), KUNIT_CASE(apr_test_output), + KUNIT_CASE(apr_test_batch_fence), + KUNIT_CASE(apr_test_batch_errors), + KUNIT_CASE(apr_test_batch_coalesced), {} }; diff --git a/drivers/media/platform/apple/prores/apple-prores.h b/drivers/media/platform/apple/prores/apple-prores.h index 114b52ac76b89..5bd65e5a27d61 100644 --- a/drivers/media/platform/apple/prores/apple-prores.h +++ b/drivers/media/platform/apple/prores/apple-prores.h @@ -16,6 +16,7 @@ #define _APPLE_PRORES_H #include +#include #include #include @@ -53,6 +54,40 @@ #define APR_RING_BYTES SZ_128K #define APR_DESC_BYTES 0x180 +/* A bounded hardware batch; retain every DMA mapping until all DONEs. */ +struct apr_batch { + u32 start; + unsigned int count; + unsigned int done; +}; + +static inline u32 apr_batch_head(const struct apr_batch *batch) +{ + return batch->start + batch->count * APR_DESC_BYTES; +} + +/* + * Count only distinct, acknowledged completion events, never fetched entries. + * A sticky DONE can merge events: undercount and time out rather than return + * a buffer whose DMA completion is uncertain. The caller flushes each ACK + * before sampling status again. No descriptors are added to a live batch. + * + * 0: still owned by hardware, 1: whole batch finished, negative: reset needed. + */ +static inline int apr_batch_complete(struct apr_batch *batch, u32 status, u32 tail) +{ + if (!status) + return -EAGAIN; + if (!batch->count || batch->done >= batch->count) + return -EINVAL; + if (status != APR_IRQ_DONE || tail > apr_batch_head(batch) || + tail < batch->start + (batch->done + 1) * APR_DESC_BYTES || + (tail - batch->start) % APR_DESC_BYTES) + return -EIO; + batch->done++; + return batch->done == batch->count; +} + /* Quantisation tables the engine holds, in register order. */ enum apr_qtable { APR_QTABLE_XQ, diff --git a/tools/asahi/prores-queue-tests.py b/tools/asahi/prores-queue-tests.py new file mode 100644 index 0000000000000..c64a238dcb800 --- /dev/null +++ b/tools/asahi/prores-queue-tests.py @@ -0,0 +1,266 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Execute the driver's completion path with a fetch-ahead MMIO device model.""" +import argparse +import os +from pathlib import Path +import re +import subprocess +import tempfile + + +def function(text, name): + match = re.search(r'^static [^\n]*\b' + name + r'\([^\n]*\)\n\{', text, re.M) + assert match, name + end = match.end() + level = 1 + while level: + level += (text[end] == '{') - (text[end] == '}') + end += 1 + return text[match.start():end] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--root', type=Path, default=Path(__file__).resolve().parents[2]) + parser.add_argument('--out', type=Path) + parser.add_argument('--mutation', choices=('tail-only', 'release-early', 'tail-completes-batch', + 'reset-free', 'ack-order', 'stage-only')) + args = parser.parse_args() + driver = args.root / 'drivers/media/platform/apple/prores' + header = (driver / 'apple-prores.h').read_text() + source = (driver / 'apple-prores-drv.c').read_text() + batch = header[header.index('struct apr_batch {'):header.index('/* Quantisation tables')] + take = function(source, 'apr_take_done') + irq = function(source, 'apr_irq') + watch = function(source, 'apr_watchdog') + publish = function(source, 'apr_publish_batch') + if args.mutation == 'tail-only': + take = take.replace('if (!status)\n\t\treturn NULL;', '') + batch = batch.replace('if (!status)\n\t\treturn -EAGAIN;', '') + batch = batch.replace('status != APR_IRQ_DONE', '(status && status != APR_IRQ_DONE)') + elif args.mutation == 'release-early': + take = take.replace('if (!*err) {', 'if (0) {') + elif args.mutation == 'tail-completes-batch': + batch = batch.replace('batch->done++;', 'batch->done = batch->count;') + elif args.mutation == 'reset-free': + watch = watch.replace('vb2_queue_error(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx));', + 'vb2_queue_error(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx));\n' + '\t\tapr_finish_job(apr, ctx, err);') + elif args.mutation == 'ack-order': + take = take.replace('apr_write(apr, APR_REG_IRQ_STATUS, status);', '') + take = take.replace('tail = apr_read(apr, APR_REG_RING_TAIL) & APR_RING_PTR_MASK;', + 'tail = apr_read(apr, APR_REG_RING_TAIL) & APR_RING_PTR_MASK;\n' + '\tapr_write(apr, APR_REG_IRQ_STATUS, status);') + elif args.mutation == 'stage-only': + publish = publish.replace('apr_batch_head(&apr->batch)', + 'apr->batch.start + APR_DESC_BYTES') + # Keep register values shared with the driver, without host Linux headers. + defines = '\n'.join(line for line in header.splitlines() if + line.startswith('#define APR_') and + any(name in line for name in ('APR_REG_', 'APR_IRQ_', 'APR_MODE_', + 'APR_RING_PTR_MASK', 'APR_DESC_BYTES'))) + defines += '\n' + '\n'.join(line for line in source.splitlines() if + line.startswith(('#define APR_TIMEOUT_MS', '#define APR_POLL_MS'))) + harness = r''' +#include +#include +#include +#include +#include +typedef uint32_t u32; +typedef int irqreturn_t; +#define BIT(n) (1U << (n)) +#define GENMASK(h, l) ((~0U << (l)) & (~0U >> (31 - (h)))) +#define IRQ_NONE 0 +#define IRQ_HANDLED 1 +#define container_of(p, type, member) ((p)->owner) +#define spin_lock_irqsave(p, flags) do { (void)(p); (flags) = 0; } while (0) +#define spin_unlock_irqrestore(p, flags) do { (void)(p); (void)(flags); } while (0) +#define time_before(a,b) ((long)((a)-(b)) < 0) +#define msecs_to_jiffies(n) (n) +#define dev_warn_once(...) ((void)0) +#define dev_err(...) ((void)0) +#define THIS_MODULE NULL +#define spin_lock(p) ((void)(p)) +#define spin_unlock(p) ((void)(p)) +#define dev_err_ratelimited(...) ((void)0) +#define dev_dbg(...) ((void)0) +#define dma_wmb() ((void)0) +#define system_wq NULL +''' + defines + '\n' + batch + r''' +struct vb2_queue { bool error; }; +struct v4l2_m2m_ctx { struct vb2_queue src, dst; }; +struct apr_ctx { int owner; struct { struct v4l2_m2m_ctx *m2m_ctx; } fh; }; +struct apr_dev; +struct work_struct { struct apr_dev *owner; }; +struct delayed_work { struct work_struct work; }; +struct apr_dev { + struct apr_ctx *active; + struct apr_batch batch; + bool recovering, quarantined, broken, ring_ready; + void *reset, *dev; + unsigned long started; + u32 head; + unsigned char *regs; + int hw_lock; + struct delayed_work watchdog; +}; +static u32 mmio[0x2000 / 4]; +static unsigned int released, pm_puts, slots_finished, resets_queued; +static unsigned long jiffies = 100; +static unsigned int reset_calls, module_pins, device_pins; +static int reset_rc, last_err; +static int reset_control_reset(void *r) { assert(r); reset_calls++; return reset_rc; } +static void __module_get(void *m) { (void)m; module_pins++; } +static void get_device(void *d) { (void)d; device_pins++; } +static struct vb2_queue *v4l2_m2m_get_src_vq(struct v4l2_m2m_ctx *m) { return &m->src; } +static struct vb2_queue *v4l2_m2m_get_dst_vq(struct v4l2_m2m_ctx *m) { return &m->dst; } +static void vb2_queue_error(struct vb2_queue *q) { q->error = true; } +static u32 pending_ack; +static u32 apr_read(struct apr_dev *a, u32 reg) { + (void)a; + /* MMIO reads complete prior posted ACK writes at the device. */ + mmio[APR_REG_IRQ_STATUS/4] &= ~pending_ack; + pending_ack = 0; + return mmio[reg/4]; +} +static void apr_write(struct apr_dev *a, u32 reg, u32 value) { + (void)a; + if (reg == APR_REG_IRQ_STATUS) pending_ack |= value; + else mmio[reg/4] = value; +} +static void writel(u32 value, void *addr) { + assert(!pending_ack); + *(u32 *)addr = value; +} +static void mod_delayed_work(void *q, void *w, int delay) { + (void)q; (void)w; (void)delay; resets_queued++; +} +static void cancel_delayed_work(void *w) { (void)w; } +static void schedule_delayed_work(void *w, int d) { (void)w; (void)d; } +static void apr_finish_job(struct apr_dev *a, struct apr_ctx *ctx, int err) { + assert(ctx); + last_err = err; + released += a->batch.count; + pm_puts++; + slots_finished++; +} +''' + take + '\n' + irq + '\n' + watch + '\n' + publish + r''' +static struct v4l2_m2m_ctx m2m; +static struct apr_ctx owner = { .owner = 42, .fh = { .m2m_ctx = &m2m } }; +static struct apr_dev begin(unsigned int depth, u32 base) { + struct apr_dev a = { + .active = &owner, .batch = { .start = base, .count = depth }, + .regs = (unsigned char *)mmio, .reset = (void *)1, .ring_ready = true, + }; + released = pm_puts = slots_finished = resets_queued = 0; + reset_calls = module_pins = device_pins = 0; + reset_rc = last_err = 0; + pending_ack = 0; + m2m.src.error = m2m.dst.error = false; + jiffies = 100; + for (unsigned int i = 0; i < sizeof(mmio)/sizeof(mmio[0]); i++) mmio[i] = 0; + apr_publish_batch(&a, &owner); + assert(a.head == base + depth * APR_DESC_BYTES); + assert(mmio[APR_REG_RING_HEAD / 4] == a.head); + return a; +} +int main(void) { + for (unsigned int depth = 1; depth <= 4; depth++) { + /* The last batch ends at the last whole descriptor in the ring. */ + u32 base = (0x20000 / APR_DESC_BYTES - depth) * APR_DESC_BYTES; + struct apr_dev a = begin(depth, base); + for (unsigned int i = 0; i < depth; i++) { + assert(a.head == base + depth * APR_DESC_BYTES); + /* Fetch finishes first. Repeated watchdog/IRQ reads must retain ownership. */ + mmio[APR_REG_RING_TAIL / 4] = a.head; + for (unsigned int poll = 0; poll < 20; poll++) { + assert(apr_irq(0, &a) == IRQ_NONE); + assert(a.batch.done == i && a.active == &owner); + assert(!released && !pm_puts && !slots_finished); + } + mmio[APR_REG_IRQ_STATUS / 4] = APR_IRQ_DONE; + assert(apr_irq(0, &a) == IRQ_HANDLED); + assert(mmio[APR_REG_IRQ_STATUS / 4] == 0); + if (i + 1 < depth) { + assert(a.active == &owner); + assert(mmio[APR_REG_RING_HEAD / 4] == a.head); + assert(!released && !pm_puts && !slots_finished); + } + } + assert(!a.active && released == depth && pm_puts == 1 && slots_finished == 1); + assert(!resets_queued && !last_err); + assert(apr_irq(0, &a) == IRQ_NONE); + assert(released == depth && pm_puts == 1); + } + /* One coalesced DONE at tail==head must not retire a depth-4 batch. */ + { + struct apr_dev a = begin(4, 0); + a.watchdog.work.owner = &a; + mmio[APR_REG_RING_TAIL / 4] = a.head; + mmio[APR_REG_IRQ_STATUS / 4] = APR_IRQ_DONE; + assert(apr_irq(0, &a) == IRQ_HANDLED); + assert(a.batch.done == 1 && a.active == &owner && !released); + for (unsigned int poll = 0; poll < 20; poll++) { + assert(apr_irq(0, &a) == IRQ_NONE); + assert(a.batch.done == 1 && !released); + } + jiffies = 2000; + apr_watchdog(&a.watchdog.work); + assert(reset_calls == 1 && a.broken && !a.active); + assert(released == 4 && last_err == -ETIMEDOUT); + } + for (unsigned int fault = 0; fault < 4; fault++) { + struct apr_dev a = begin(4, 0); + mmio[APR_REG_IRQ_STATUS / 4] = fault < 2 ? APR_IRQ_DONE : + (fault == 2 ? APR_IRQ_ERROR : APR_IRQ_DONE | APR_IRQ_ERROR); + mmio[APR_REG_RING_TAIL / 4] = a.head + (fault == 0 ? -0x80 : APR_DESC_BYTES); + assert(apr_irq(0, &a) == IRQ_HANDLED); + assert(a.recovering && a.active == &owner && resets_queued == 1); + assert(mmio[APR_REG_MODE / 4] == APR_MODE_OFF); + assert(!released && !pm_puts && !slots_finished); + assert(apr_irq(0, &a) == IRQ_NONE); + } + for (unsigned int failure = 0; failure < 3; failure++) { + struct apr_dev a = begin(4, 0); + a.watchdog.work.owner = &a; + jiffies = 2000; + if (failure == 1) reset_rc = -EIO; + if (failure == 2) a.reset = NULL; + apr_watchdog(&a.watchdog.work); + assert(a.broken && !a.ring_ready); + if (!failure) { + assert(!a.active && !a.quarantined && reset_calls == 1); + assert(released == 4 && pm_puts == 1 && slots_finished == 1); + assert(last_err == -ETIMEDOUT && module_pins == 1 && device_pins == 1); + } else { + assert(a.active == &owner && a.quarantined); + assert(module_pins == 1 && device_pins == 1); + assert(m2m.src.error && m2m.dst.error); + assert(!released && !pm_puts && !slots_finished); + unsigned int calls = reset_calls; + apr_watchdog(&a.watchdog.work); + assert(apr_irq(0, &a) == IRQ_NONE); + assert(reset_calls == calls && module_pins == 1 && device_pins == 1); + assert(!released && !pm_puts && !slots_finished); + } + } + puts("PASS: fetch-ahead ownership, counted DONEs, hardware depths 1-4, ring end, reset quarantine"); +} +''' + with tempfile.TemporaryDirectory(prefix='prores-queue-') as tmp: + out = args.out or Path(tmp) + out.mkdir(parents=True, exist_ok=True) + c = out / 'prores-queue.c' + exe = out / 'prores-queue' + c.write_text(harness) + subprocess.run([os.environ.get('CC', 'cc'), '-std=c11', '-Wall', '-Wextra', '-Werror', '-Wno-unused-parameter', + '-fsanitize=address,undefined', str(c), '-o', str(exe)], check=True) + result = subprocess.run([str(exe)], check=False) + if result.returncode: + raise SystemExit(f'Assertion: completion model exited {result.returncode}') + + +if __name__ == '__main__': + main() diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 2fd8ac3873ea4..39b3246918ed6 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -142,6 +142,9 @@ def mutations(flag, names, base, expect=KILL): 'pmu-rtc-tests.py': [(out(), PASS)] + mutations('--mutation', ('ack-clobber', 'arm-ack', 'offset-seconds', 'offset-sign', 'event-bit', 'wrong-base'), out()), + 'prores-queue-tests.py': [(out(), PASS)] + + mutations('--mutation', ('tail-only', 'release-early', 'tail-completes-batch', + 'reset-free', 'ack-order', 'stage-only'), out()), 'qspi-nvram-tests.py': [(out('--output'), PASS)], 'rtkit-oslog-tests.py': [(out(), PASS)] + mutations('--mutate', ('admission', 'channel', 'counter', 'reset', 'buffer'), out()),