From 50d3ab95d38fc81d29f07d274327076829151a27 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 03:15:40 +1100 Subject: [PATCH 1/3] media: apple: prores: keep up to eight jobs in the ring The engine works through a producer/consumer descriptor ring, but the driver kept one job in it: v4l2-m2m runs one job at a time and the job only finished at its completion interrupt. On an M1 Pro, VideoToolbox decodes the same clips about 2.6 times faster on the same engine, and between two of our jobs the engine sits idle for the interrupt and the workqueue round trip to the next device_run (100 us per 1080p frame). Publish a job's descriptor, move its buffers off the m2m queues into a per-device FIFO tagged with the ring pointer past the descriptor, and finish the m2m job at once so the next one is prepared while the engine works. Up to APR_MAX_INFLIGHT (8) jobs sit in the ring; when it is full the m2m job stays running until a completion makes room, so device_run is only entered with room for one more descriptor. The interrupt reads the tail and completes, in ring order, every job the tail has passed. DONE and ERROR are latched per engine, so an ERROR fails every job in the ring at that moment. A ring that stops moving for a second fails all of its jobs and resets the engine as before; device_run waits for that reset. stop_streaming, suspend and a STOP command with an empty OUTPUT queue wait for the context's jobs in the ring; job_abort marks only that context's jobs cancelled, since encoder and decoder share the ring. Encode stays at one job in the ring: every encode job uses the one shared statistics area and encode already runs at macOS speed. Add KUnit cases for the ring distance, free space, tail validation and in-order retirement across the ring wrap, and for the FIFO limits. Signed-off-by: Ryan Murray --- .../platform/apple/prores/apple-prores-desc.c | 57 +++ .../platform/apple/prores/apple-prores-drv.c | 421 ++++++++++++++---- .../platform/apple/prores/apple-prores-test.c | 97 ++++ .../platform/apple/prores/apple-prores.h | 20 + 4 files changed, 517 insertions(+), 78 deletions(-) diff --git a/drivers/media/platform/apple/prores/apple-prores-desc.c b/drivers/media/platform/apple/prores/apple-prores-desc.c index e13b98014f8b0..8844574081bf9 100644 --- a/drivers/media/platform/apple/prores/apple-prores-desc.c +++ b/drivers/media/platform/apple/prores/apple-prores-desc.c @@ -675,6 +675,63 @@ u32 apr_ring_advance(u32 ptr, u32 bytes, u32 ring_bytes) } EXPORT_SYMBOL_IF_KUNIT(apr_ring_advance); +/* + * Ring pointers carry a generation bit above the offset, so a pointer names + * one of 2 * ring_bytes positions and a full ring differs from an empty one. + */ +static u32 apr_ring_pos(u32 ptr, u32 ring_bytes) +{ + return (ptr & APR_RING_OFFSET_MASK) + + (ptr & APR_RING_GENERATION ? ring_bytes : 0); +} + +/* Bytes from @from forward to @to. */ +u32 apr_ring_distance(u32 from, u32 to, u32 ring_bytes) +{ + u32 span = 2 * ring_bytes; + + return (apr_ring_pos(to, ring_bytes) + span - + apr_ring_pos(from, ring_bytes)) % span; +} +EXPORT_SYMBOL_IF_KUNIT(apr_ring_distance); + +/* Bytes the producer may still write; @done is what the engine consumed. */ +u32 apr_ring_free(u32 done, u32 head, u32 ring_bytes) +{ + u32 used = apr_ring_distance(done, head, ring_bytes); + + return used < ring_bytes ? ring_bytes - used : 0; +} +EXPORT_SYMBOL_IF_KUNIT(apr_ring_free); + +/* + * Has the consumer @tail moved past the descriptor that ends at @end? @done + * is the start of the oldest descriptor still owned by the engine. + */ +bool apr_ring_passed(u32 done, u32 end, u32 tail, u32 ring_bytes) +{ + return apr_ring_distance(done, end, ring_bytes) <= + apr_ring_distance(done, tail, ring_bytes); +} +EXPORT_SYMBOL_IF_KUNIT(apr_ring_passed); + +/* A tail the engine reports must lie between what it consumed and the head. */ +bool apr_ring_tail_valid(u32 done, u32 tail, u32 head, u32 ring_bytes) +{ + return !(tail & ~APR_RING_PTR_MASK) && + (tail & APR_RING_OFFSET_MASK) < ring_bytes && + apr_ring_distance(done, tail, ring_bytes) <= + apr_ring_distance(done, head, ring_bytes); +} +EXPORT_SYMBOL_IF_KUNIT(apr_ring_tail_valid); + +/* No further job may enter the ring with @jobs in it, @encodes of them encodes. */ +bool apr_jobs_full(unsigned int jobs, unsigned int encodes) +{ + return jobs >= APR_MAX_INFLIGHT || encodes >= APR_ENCODE_MAX_INFLIGHT; +} +EXPORT_SYMBOL_IF_KUNIT(apr_jobs_full); + void apr_ring_write(u8 *ring, u32 ring_bytes, u32 ptr, const u8 *desc, u32 desc_bytes) { diff --git a/drivers/media/platform/apple/prores/apple-prores-drv.c b/drivers/media/platform/apple/prores/apple-prores-drv.c index 344bd2f5f52b1..a4045f808c806 100644 --- a/drivers/media/platform/apple/prores/apple-prores-drv.c +++ b/drivers/media/platform/apple/prores/apple-prores-drv.c @@ -2,8 +2,12 @@ /* * Apple ProRes (APR) V4L2 memory-to-memory codec. * - * 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. + * The driver writes job descriptors into a 128 KiB DMA ring and advances the + * producer head; the engine works through the ring and reports progress with + * a consumer tail and a completion interrupt. Up to APR_MAX_INFLIGHT jobs sit + * in the ring at once: a job's buffers leave the m2m queues when its + * descriptor is published, so v4l2-m2m can run the next job while the engine + * is busy, and they complete in ring order once the tail passes them. * The encoder consumes semi-planar 4:2:2 or 4:4:4 frames and produces one * complete ProRes frame per buffer. The decoder exposes a second stateful * node with the queue directions reversed and linear color output. @@ -11,6 +15,7 @@ * Based on Acelogic's J700 ProRes diagnostic, which first encoded on this * engine under Linux, and on the m1n1 proxyclient ProRes experiment (MIT). */ +#include #include #include #include @@ -29,6 +34,7 @@ #include #include #include +#include #include #include @@ -844,6 +850,8 @@ static const struct apr_variant apr_t6050 = { .apri = &apri_t6050, }; +struct apr_ctx; + /* * A source buffer of the decoder. The frame is parsed once, when it is * queued; job_ready, the source-change check and device_run then use the @@ -861,6 +869,17 @@ static struct apr_buf *to_apr_buf(struct vb2_v4l2_buffer *vbuf) return container_of(vbuf, struct apr_buf, m2m.vb); } +/* A job in the ring; its buffers left the m2m queues when it was published. */ +struct apr_job { + struct apr_ctx *ctx; + struct vb2_v4l2_buffer *src; + struct vb2_v4l2_buffer *dst; + u32 end; /* ring pointer just past its descriptor */ + u32 sequence; + u32 bytes; /* decoded payload */ + int err; /* forced result: engine error or cancellation */ +}; + struct apr_dev { const struct apr_variant *variant; struct device *dev; @@ -891,12 +910,22 @@ 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_job jobs[APR_MAX_INFLIGHT]; /* in ring order */ + unsigned int job_first; /* oldest job in jobs[] */ + unsigned int njobs; /* jobs the engine owns */ + unsigned int nencodes; /* encode jobs among them */ + struct apr_ctx *held; /* m2m job left running while the ring is full */ u32 head; /* next free ring offset */ + u32 done; /* the engine has consumed the ring up to here */ bool ring_ready; /* engine registers programmed since power-up */ bool broken; /* timed out with no way to reset */ - unsigned long started; /* jiffies at submission */ + bool hung; /* ring state is inconsistent, reset now */ + bool resetting; /* the watchdog is resetting the engine */ + unsigned long progress; /* jiffies of the last start or completion */ struct delayed_work watchdog; + + atomic_t outstanding; /* jobs not yet given back to vb2 */ + wait_queue_head_t idle_wq; /* woken when jobs are given back */ }; struct apr_ctx { @@ -910,7 +939,7 @@ struct apr_ctx { bool decode; bool source_valid; bool decode_444; - bool cancelled; + atomic_t inflight; /* jobs in the ring not yet given back to vb2 */ enum apr_profile stream_profile; /* fixed while OUTPUT streams */ u32 sequence; }; @@ -991,6 +1020,7 @@ static void apr_hw_init(struct apr_dev *apr) /* Order the setup before any descriptor publication. */ wmb(); apr->head = 0; + apr->done = 0; apr->ring_ready = true; } @@ -1120,17 +1150,18 @@ static int apr_fill_decode_frame(struct apr_ctx *ctx, return 0; } -static void apr_finish_job(struct apr_dev *apr, struct apr_ctx *ctx, int err) +/* + * Give a job's buffers back to vb2. Safe in interrupt context. Not inlined, + * so ftrace can time completions against apr_device_run and apr_irq. + */ +static noinline void apr_complete(struct apr_dev *apr, struct apr_job *job) { + struct apr_ctx *ctx = job->ctx; struct v4l2_m2m_ctx *m2m = ctx->fh.m2m_ctx; - struct vb2_v4l2_buffer *src, *dst; + struct vb2_v4l2_buffer *src = job->src, *dst = job->dst; + int err = job->err; u32 bytes = 0; - src = v4l2_m2m_src_buf_remove(m2m); - dst = v4l2_m2m_dst_buf_remove(m2m); - if (WARN_ON(!src || !dst)) - goto out; - if (!err && !ctx->decode) { u8 *out = vb2_plane_vaddr(&dst->vb2_buf, 0); @@ -1144,11 +1175,11 @@ static void apr_finish_job(struct apr_dev *apr, struct apr_ctx *ctx, int err) if (!err && ctx->decode) { /* The ACK and tail read complete before output ownership returns. */ dma_rmb(); - bytes = ctx->dst_fmt.plane_fmt[0].sizeimage; + bytes = job->bytes; } vb2_set_plane_payload(&dst->vb2_buf, 0, err ? 0 : bytes); v4l2_m2m_buf_copy_metadata(src, dst); - dst->sequence = ctx->sequence++; + dst->sequence = job->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 }; @@ -1159,27 +1190,72 @@ static void apr_finish_job(struct apr_dev *apr, struct apr_ctx *ctx, int err) } 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: +} + +/* + * Give back jobs taken off the ring, in ring order. A context may be freed + * as soon as its in-flight count drops, so it is not touched after that. + */ +static void apr_retire(struct apr_dev *apr, struct apr_job *jobs, + unsigned int n) +{ + unsigned int i; + + for (i = 0; i < n; i++) { + apr_complete(apr, &jobs[i]); + pm_runtime_mark_last_busy(apr->dev); + pm_runtime_put_autosuspend(apr->dev); + atomic_dec(&jobs[i].ctx->inflight); + atomic_dec(&apr->outstanding); + } + if (n) + wake_up_all(&apr->idle_wq); +} + +/* Fail the current m2m job before its descriptor reached the ring. */ +static void apr_finish_job(struct apr_dev *apr, struct apr_ctx *ctx, int err) +{ + struct v4l2_m2m_ctx *m2m = ctx->fh.m2m_ctx; + struct apr_job job = { + .ctx = ctx, + .err = err, + .sequence = ctx->sequence++, + }; + + job.src = v4l2_m2m_src_buf_remove(m2m); + job.dst = v4l2_m2m_dst_buf_remove(m2m); + if (!WARN_ON(!job.src || !job.dst)) + apr_complete(apr, &job); pm_runtime_mark_last_busy(apr->dev); pm_runtime_put_autosuspend(apr->dev); v4l2_m2m_job_finish(apr->m2m_dev, m2m); } /* - * Claim the active job if the engine has finished it. Returns the context to - * finish, or NULL. Called with hw_lock held. + * Move the jobs the engine has finished, oldest first, to @out (room for + * APR_MAX_INFLIGHT). Returns false if the engine raised nothing. Called with + * hw_lock held and the engine powered. + * + * DONE and ERROR are latched per engine, not per descriptor: the status and + * detail registers do not say which descriptor failed. An ERROR therefore + * fails every job in the ring at that moment; the engine keeps running and + * the watchdog resets it if the tail stops moving. Several descriptors may + * finish behind one interrupt, and a DONE may arrive for a job already taken + * here on the tail alone; both are fine. */ -static struct apr_ctx *apr_take_done(struct apr_dev *apr, int *err) +static bool apr_take_done(struct apr_dev *apr, struct apr_job *out, + unsigned int *n) { - struct apr_ctx *ctx = apr->active; + u32 ring_bytes = apr->variant->ring_bytes; u32 status, tail, st0, st1, st2; u32 detail[6]; + unsigned int i; - if (!ctx) - return NULL; + *n = 0; status = apr_read(apr, APR_REG_IRQ_STATUS); if (!(status & (APR_IRQ_DONE | APR_IRQ_ERROR))) - return NULL; + return false; + /* Snapshot before the W1C, as for a single job. */ st0 = apr_read(apr, APR_REG_ST0); st1 = apr_read(apr, APR_REG_ST1); st2 = apr_read(apr, APR_REG_ST2); @@ -1189,78 +1265,143 @@ static struct apr_ctx *apr_take_done(struct apr_dev *apr, int *err) detail[3] = apr_read(apr, APR_REG_DETAIL2_LO); detail[4] = apr_read(apr, APR_REG_DETAIL2_HI); detail[5] = apr_read(apr, APR_REG_DETAIL3); + if (status & APR_IRQ_ERROR) { + dev_err_ratelimited(apr->dev, + "job failed: irq %#x head %#x done %#x jobs %u st %#x/%#x/%#x detail %#x/%#x/%#x/%#x/%#x/%#x\n", + status, apr->head, apr->done, apr->njobs, + st0, st1, st2, detail[0], detail[1], detail[2], + detail[3], detail[4], detail[5]); + for (i = 0; i < apr->njobs; i++) { + struct apr_job *job = &apr->jobs[(apr->job_first + i) % + APR_MAX_INFLIGHT]; + + if (!job->err) + job->err = -EIO; + } + } apr_write(apr, APR_REG_IRQ_STATUS, status); /* The tail read flushes the posted W1C before buffers are released. */ tail = apr_read(apr, APR_REG_RING_TAIL) & APR_RING_PTR_MASK; - *err = 0; - if (!(status & APR_IRQ_DONE) || status & APR_IRQ_ERROR || tail != apr->head) { - dev_err_ratelimited(apr->dev, - "job failed: irq %#x tail %#x head %#x st %#x/%#x/%#x detail %#x/%#x/%#x/%#x/%#x/%#x\n", - status, tail, apr->head, st0, st1, st2, - detail[0], detail[1], detail[2], detail[3], - detail[4], detail[5]); - *err = -EIO; + if (!apr_ring_tail_valid(apr->done, tail, apr->head, ring_bytes)) { + dev_err_ratelimited(apr->dev, "tail %#x outside %#x..%#x\n", + tail, apr->done, apr->head); + apr->hung = true; + return true; + } + while (apr->njobs) { + struct apr_job *job = &apr->jobs[apr->job_first]; + + if (!apr_ring_passed(apr->done, job->end, tail, ring_bytes)) + break; + out[(*n)++] = *job; + apr->done = job->end; + apr->job_first = (apr->job_first + 1) % APR_MAX_INFLIGHT; + apr->njobs--; + if (!job->ctx->decode) + apr->nencodes--; } - if (ctx->cancelled) - *err = -ECANCELED; - apr->active = NULL; + if (*n) + apr->progress = jiffies; + return true; +} + +/* + * The m2m job left running because the ring was full may finish once there + * is room again. Called with hw_lock held; the caller finishes it unlocked. + */ +static struct apr_ctx *apr_take_held(struct apr_dev *apr) +{ + struct apr_ctx *ctx = apr->held; + + if (!ctx || apr_jobs_full(apr->njobs, apr->nencodes)) + return NULL; + apr->held = NULL; return ctx; } static irqreturn_t apr_irq(int irq, void *data) { struct apr_dev *apr = data; - struct apr_ctx *ctx; - int err = 0; + struct apr_job done[APR_MAX_INFLIGHT]; + struct apr_ctx *held = NULL; + unsigned int n = 0; + bool handled = false, hung; spin_lock(&apr->hw_lock); - ctx = apr_take_done(apr, &err); + /* Registers are only reachable while the engine is powered. */ + if (apr->ring_ready) + handled = apr_take_done(apr, done, &n); + held = apr_take_held(apr); + hung = apr->hung; + if (!apr->njobs) + cancel_delayed_work(&apr->watchdog); spin_unlock(&apr->hw_lock); - if (!ctx) - return IRQ_NONE; - cancel_delayed_work(&apr->watchdog); - apr_finish_job(apr, ctx, err); - return IRQ_HANDLED; + if (hung) + mod_delayed_work(system_wq, &apr->watchdog, 0); + apr_retire(apr, done, n); + if (held) + v4l2_m2m_job_finish(apr->m2m_dev, held->fh.m2m_ctx); + return handled ? IRQ_HANDLED : IRQ_NONE; } /* - * 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. A ring that stops moving for + * APR_TIMEOUT_MS fails every job in it. 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. */ static void apr_watchdog(struct work_struct *work) { struct apr_dev *apr = container_of(work, struct apr_dev, watchdog.work); - struct apr_ctx *ctx; + struct apr_job done[APR_MAX_INFLIGHT]; + struct apr_ctx *held; unsigned long flags; - int err = 0; + unsigned int n = 0; spin_lock_irqsave(&apr->hw_lock, flags); - ctx = apr_take_done(apr, &err); - if (ctx) { + if (!apr->njobs) { spin_unlock_irqrestore(&apr->hw_lock, flags); - dev_warn_once(apr->dev, "completion found without an interrupt\n"); - apr_finish_job(apr, ctx, err); return; } - ctx = apr->active; - if (!ctx) { + if (apr->ring_ready && apr_take_done(apr, done, &n) && n) + dev_warn_once(apr->dev, "completion found without an interrupt\n"); + if (!apr->hung && (!apr->njobs || + time_before(jiffies, apr->progress + + msecs_to_jiffies(APR_TIMEOUT_MS)))) { + held = apr_take_held(apr); + if (apr->njobs) + schedule_delayed_work(&apr->watchdog, + msecs_to_jiffies(APR_POLL_MS)); spin_unlock_irqrestore(&apr->hw_lock, flags); + apr_retire(apr, done, n); + if (held) + v4l2_m2m_job_finish(apr->m2m_dev, held->fh.m2m_ctx); return; } - if (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, "%s: head %#x done %#x tail %#x jobs %u st %#x/%#x\n", + apr->hung ? "ring state invalid" : "job timed out", + apr->head, apr->done, apr_read(apr, APR_REG_RING_TAIL), + apr->njobs, apr_read(apr, APR_REG_ST0), apr_read(apr, APR_REG_ST1)); + /* Every job still in the ring fails; the engine may own none of them. */ + while (apr->njobs) { + struct apr_job *job = &apr->jobs[apr->job_first]; + + done[n] = *job; + if (!done[n].err) + done[n].err = -ETIMEDOUT; + n++; + apr->job_first = (apr->job_first + 1) % APR_MAX_INFLIGHT; + apr->njobs--; } - 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->nencodes = 0; + apr->hung = false; + held = apr->held; + apr->held = 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. */ + /* device_run waits until the reset below is over. */ + apr->resetting = true; spin_unlock_irqrestore(&apr->hw_lock, flags); if (!apr->reset || reset_control_reset(apr->reset)) { dev_err(apr->dev, "engine cannot be reset, refusing further jobs\n"); @@ -1268,24 +1409,41 @@ static void apr_watchdog(struct work_struct *work) apr->broken = true; spin_unlock_irqrestore(&apr->hw_lock, flags); } - apr_finish_job(apr, ctx, -ETIMEDOUT); + spin_lock_irqsave(&apr->hw_lock, flags); + apr->resetting = false; + spin_unlock_irqrestore(&apr->hw_lock, flags); + apr_retire(apr, done, n); + /* apr_retire() woke idle_wq only if there was a job to give back. */ + wake_up_all(&apr->idle_wq); + if (held) + v4l2_m2m_job_finish(apr->m2m_dev, held->fh.m2m_ctx); } +/* + * Publish one job and, while the ring has room for another, finish the m2m + * job at once so the next one is prepared while the engine works. When the + * ring is full the m2m job stays running until a completion makes room, so + * device_run is only entered with room for one more descriptor. + */ static void apr_device_run(void *priv) { struct apr_ctx *ctx = priv; struct apr_dev *apr = ctx->apr; + struct v4l2_m2m_ctx *m2m = ctx->fh.m2m_ctx; + u32 ring_bytes = apr->variant->ring_bytes; struct vb2_v4l2_buffer *src, *dst; struct apr_frame f; struct apr_decode_frame decode; + struct apr_job *job; u8 desc[APR_ENCODE_DESC_BYTES]; u32 desc_bytes; unsigned long flags; + bool hold; void *out; int ret; - src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); - dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); + src = v4l2_m2m_next_src_buf(m2m); + dst = v4l2_m2m_next_dst_buf(m2m); ret = pm_runtime_resume_and_get(apr->dev); if (ret < 0) { @@ -1313,13 +1471,24 @@ static void apr_device_run(void *priv) } spin_lock_irqsave(&apr->hw_lock, flags); + while (apr->resetting) { + spin_unlock_irqrestore(&apr->hw_lock, flags); + wait_event(apr->idle_wq, !READ_ONCE(apr->resetting)); + spin_lock_irqsave(&apr->hw_lock, flags); + } if (apr->broken) { spin_unlock_irqrestore(&apr->hw_lock, flags); apr_finish_job(apr, ctx, -EIO); return; } - if (!apr->ring_ready) + if (!apr->ring_ready && !WARN_ON(apr->njobs)) apr_hw_init(apr); + if (WARN_ON(apr_jobs_full(apr->njobs, apr->nencodes) || + apr_ring_free(apr->done, apr->head, ring_bytes) <= desc_bytes)) { + spin_unlock_irqrestore(&apr->hw_lock, flags); + apr_finish_job(apr, ctx, -EBUSY); + return; + } ret = ctx->decode ? apr_build_decode_desc(&decode, desc) : apr->variant->build_desc(&f, desc); if (ret) { @@ -1327,18 +1496,35 @@ static void apr_device_run(void *priv) apr_finish_job(apr, ctx, ret); return; } - apr_ring_write(apr->ring, apr->variant->ring_bytes, apr->head, - desc, desc_bytes); - apr->head = apr_ring_advance(apr->head, desc_bytes, - apr->variant->ring_bytes); - apr->active = ctx; - ctx->cancelled = false; - apr->started = jiffies; + apr_ring_write(apr->ring, ring_bytes, apr->head, desc, desc_bytes); + apr->head = apr_ring_advance(apr->head, desc_bytes, ring_bytes); + job = &apr->jobs[(apr->job_first + apr->njobs) % APR_MAX_INFLIGHT]; + *job = (struct apr_job){ + .ctx = ctx, + .src = v4l2_m2m_src_buf_remove(m2m), + .dst = v4l2_m2m_dst_buf_remove(m2m), + .end = apr->head, + .sequence = ctx->sequence++, + .bytes = ctx->dst_fmt.plane_fmt[0].sizeimage, + }; + WARN_ON(job->src != src || job->dst != dst); + if (!apr->njobs) + apr->progress = jiffies; + apr->njobs++; + if (!ctx->decode) + apr->nencodes++; + atomic_inc(&ctx->inflight); + atomic_inc(&apr->outstanding); /* Descriptor and buffers must be visible before the head moves. */ dma_wmb(); writel(apr->head, apr->regs + APR_REG_RING_HEAD); - spin_unlock_irqrestore(&apr->hw_lock, flags); + hold = apr_jobs_full(apr->njobs, apr->nencodes); + if (hold) + apr->held = ctx; schedule_delayed_work(&apr->watchdog, msecs_to_jiffies(APR_POLL_MS)); + spin_unlock_irqrestore(&apr->hw_lock, flags); + if (!hold) + v4l2_m2m_job_finish(apr->m2m_dev, m2m); } static int apr_job_ready(void *priv) @@ -1363,10 +1549,21 @@ static void apr_job_abort(void *priv) struct apr_dev *apr = ctx->apr; unsigned long flags; - /* There is no decode- or encode-specific cancellation opcode. */ + unsigned int i; + + /* + * There is no cancellation opcode: the context's jobs still run to the + * end of the ring and only report cancelled. stop_streaming waits for + * them; other contexts' jobs in the same ring are not touched. + */ spin_lock_irqsave(&apr->hw_lock, flags); - if (apr->active == ctx) - ctx->cancelled = true; + for (i = 0; i < apr->njobs; i++) { + struct apr_job *job = &apr->jobs[(apr->job_first + i) % + APR_MAX_INFLIGHT]; + + if (job->ctx == ctx) + job->err = -ECANCELED; + } spin_unlock_irqrestore(&apr->hw_lock, flags); } @@ -1628,6 +1825,63 @@ static int apr_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subsc return v4l2_ctrl_subscribe_event(fh, sub); } +/* + * A STOP with no OUTPUT buffer left in the m2m queue makes the m2m helper + * mark the next CAPTURE buffer LAST at once. With this context's jobs still + * in the ring their frames would follow that LAST buffer, so wait for them + * first. The check and the helper run under hw_lock: device_run moves a + * job's buffers into the ring under the same lock, so the helper sees the + * last OUTPUT buffer either queued or already given back. + */ +static int apr_stop_cmd(struct file *file, struct apr_ctx *ctx, void *cmd) +{ + struct v4l2_m2m_ctx *m2m = ctx->fh.m2m_ctx; + struct apr_dev *apr = ctx->apr; + unsigned long flags; + int ret; + + for (;;) { + spin_lock_irqsave(&apr->hw_lock, flags); + if (v4l2_m2m_num_src_bufs_ready(m2m) || + !atomic_read(&ctx->inflight)) + break; + spin_unlock_irqrestore(&apr->hw_lock, flags); + wait_event(apr->idle_wq, !atomic_read(&ctx->inflight)); + } + ret = ctx->decode ? v4l2_m2m_decoder_cmd(file, m2m, cmd) : + v4l2_m2m_encoder_cmd(file, m2m, cmd); + spin_unlock_irqrestore(&apr->hw_lock, flags); + return ret; +} + +static int apr_encoder_cmd(struct file *file, void *priv, + struct v4l2_encoder_cmd *ec) +{ + struct apr_ctx *ctx = apr_file_to_ctx(file); + int ret; + + ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, ec); + if (ret) + return ret; + if (ec->cmd == V4L2_ENC_CMD_STOP) + return apr_stop_cmd(file, ctx, ec); + return v4l2_m2m_ioctl_encoder_cmd(file, priv, ec); +} + +static int apr_decoder_cmd(struct file *file, void *priv, + struct v4l2_decoder_cmd *dc) +{ + struct apr_ctx *ctx = apr_file_to_ctx(file); + int ret; + + ret = v4l2_m2m_ioctl_try_decoder_cmd(file, priv, dc); + if (ret) + return ret; + if (dc->cmd == V4L2_DEC_CMD_STOP) + return apr_stop_cmd(file, ctx, dc); + return v4l2_m2m_ioctl_decoder_cmd(file, priv, dc); +} + static const struct v4l2_ioctl_ops apr_ioctl_ops = { .vidioc_querycap = apr_querycap, .vidioc_enum_fmt_vid_out = apr_enum_fmt_out, @@ -1651,7 +1905,7 @@ static const struct v4l2_ioctl_ops apr_ioctl_ops = { .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd, - .vidioc_encoder_cmd = v4l2_m2m_ioctl_encoder_cmd, + .vidioc_encoder_cmd = apr_encoder_cmd, .vidioc_subscribe_event = apr_subscribe_event, .vidioc_unsubscribe_event = v4l2_event_unsubscribe, @@ -1680,7 +1934,7 @@ static const struct v4l2_ioctl_ops apr_decoder_ioctl_ops = { .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd, - .vidioc_decoder_cmd = v4l2_m2m_ioctl_decoder_cmd, + .vidioc_decoder_cmd = apr_decoder_cmd, .vidioc_subscribe_event = apr_subscribe_event, .vidioc_unsubscribe_event = v4l2_event_unsubscribe, @@ -1821,6 +2075,12 @@ static void apr_stop_streaming(struct vb2_queue *q) struct apr_ctx *ctx = vb2_get_drv_priv(q); struct vb2_v4l2_buffer *vbuf; + /* + * The engine still owns this context's jobs in the ring. The m2m core + * cancelled the job being prepared; wait for the ring ones, which the + * watchdog completes or fails within APR_TIMEOUT_MS of no progress. + */ + wait_event(ctx->apr->idle_wq, !atomic_read(&ctx->inflight)); for (;;) { vbuf = V4L2_TYPE_IS_OUTPUT(q->type) ? v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx) : @@ -1993,6 +2253,8 @@ static int apr_suspend(struct device *dev) struct apr_dev *apr = dev_get_drvdata(dev); v4l2_m2m_suspend(apr->m2m_dev); + /* No new job starts now; let the ring drain. */ + wait_event(apr->idle_wq, !atomic_read(&apr->outstanding)); return pm_runtime_force_suspend(dev); } @@ -2101,6 +2363,9 @@ static int apr_probe(struct platform_device *pdev) mutex_init(&apr->vdev_lock); spin_lock_init(&apr->hw_lock); INIT_DELAYED_WORK(&apr->watchdog, apr_watchdog); + init_waitqueue_head(&apr->idle_wq); + /* The ring always has room for every job the driver keeps in flight. */ + BUILD_BUG_ON(APR_MAX_INFLIGHT * APR_ENCODE_DESC_BYTES >= APR_RING_BYTES); platform_set_drvdata(pdev, apr); for (i = 0; i < apr->variant->num_regs; i++) { diff --git a/drivers/media/platform/apple/prores/apple-prores-test.c b/drivers/media/platform/apple/prores/apple-prores-test.c index f451b76d8c983..3ddb07e371a62 100644 --- a/drivers/media/platform/apple/prores/apple-prores-test.c +++ b/drivers/media/platform/apple/prores/apple-prores-test.c @@ -700,6 +700,100 @@ static void apr_test_ring_accounting(struct kunit *test) KUNIT_EXPECT_MEMEQ(test, ring, desc + 0x100, 0x80); } +static void apr_test_ring_space(struct kunit *test) +{ + const u32 r = APR_RING_BYTES, g = APR_RING_GENERATION; + + KUNIT_EXPECT_EQ(test, apr_ring_distance(0, 0x100, r), 0x100U); + KUNIT_EXPECT_EQ(test, apr_ring_distance(0x100, 0x100, r), 0U); + /* Across the end of the ring, in either generation. */ + KUNIT_EXPECT_EQ(test, apr_ring_distance(r - 0x80, g | 0x80, r), 0x100U); + KUNIT_EXPECT_EQ(test, apr_ring_distance(g | (r - 0x80), 0x80, r), 0x100U); + /* Same offset, other generation: a whole ring apart. */ + KUNIT_EXPECT_EQ(test, apr_ring_distance(0x100, g | 0x100, r), r); + + KUNIT_EXPECT_EQ(test, apr_ring_free(0, 0, r), r); + KUNIT_EXPECT_EQ(test, apr_ring_free(0, 0x800, r), r - 0x800); + KUNIT_EXPECT_EQ(test, apr_ring_free(r - 0x80, g | 0x80, r), r - 0x100); + KUNIT_EXPECT_EQ(test, apr_ring_free(g | 0x100, 0x100, r), 0U); + /* The deepest ring the driver builds still has room. */ + KUNIT_EXPECT_GT(test, apr_ring_free(0, APR_MAX_INFLIGHT * + APR_ENCODE_DESC_BYTES, r), + (u32)APR_ENCODE_DESC_BYTES); +} + +static void apr_test_ring_tail(struct kunit *test) +{ + const u32 r = APR_RING_BYTES, g = APR_RING_GENERATION; + + KUNIT_EXPECT_TRUE(test, apr_ring_passed(0, 0x100, 0x100, r)); + KUNIT_EXPECT_TRUE(test, apr_ring_passed(0, 0x100, 0x300, r)); + KUNIT_EXPECT_FALSE(test, apr_ring_passed(0, 0x100, 0x80, r)); + KUNIT_EXPECT_FALSE(test, apr_ring_passed(0, 0x100, 0, r)); + KUNIT_EXPECT_TRUE(test, apr_ring_passed(r - 0x80, g | 0x80, g | 0x80, r)); + KUNIT_EXPECT_FALSE(test, apr_ring_passed(r - 0x80, g | 0x80, r - 0x40, r)); + KUNIT_EXPECT_FALSE(test, apr_ring_passed(g | (r - 0x80), 0x80, 0x40, r)); + KUNIT_EXPECT_TRUE(test, apr_ring_passed(g | (r - 0x80), 0x80, 0x80, r)); + + KUNIT_EXPECT_TRUE(test, apr_ring_tail_valid(0x100, 0x100, 0x400, r)); + KUNIT_EXPECT_TRUE(test, apr_ring_tail_valid(0x100, 0x400, 0x400, r)); + KUNIT_EXPECT_TRUE(test, apr_ring_tail_valid(0x100, 0x280, 0x400, r)); + KUNIT_EXPECT_FALSE(test, apr_ring_tail_valid(0x100, 0x500, 0x400, r)); + KUNIT_EXPECT_FALSE(test, apr_ring_tail_valid(0x100, 0x80, 0x400, r)); + KUNIT_EXPECT_FALSE(test, apr_ring_tail_valid(0x100, g | 0x200, 0x400, r)); + KUNIT_EXPECT_FALSE(test, apr_ring_tail_valid(0, r, 0x400, r)); + KUNIT_EXPECT_FALSE(test, apr_ring_tail_valid(0, BIT(25), 0x400, r)); + KUNIT_EXPECT_TRUE(test, apr_ring_tail_valid(r - 0x80, g | 0x40, g | 0x80, r)); + KUNIT_EXPECT_FALSE(test, apr_ring_tail_valid(r - 0x80, 0x40, g | 0x80, r)); +} + +static void apr_test_inflight(struct kunit *test) +{ + const u32 r = APR_RING_BYTES; + u32 ends[APR_MAX_INFLIGHT], done, head, tail; + unsigned int i, retired; + + KUNIT_EXPECT_FALSE(test, apr_jobs_full(0, 0)); + KUNIT_EXPECT_FALSE(test, apr_jobs_full(APR_MAX_INFLIGHT - 1, 0)); + KUNIT_EXPECT_TRUE(test, apr_jobs_full(APR_MAX_INFLIGHT, 0)); + /* Encode stays alone at the end of the ring. */ + KUNIT_EXPECT_TRUE(test, apr_jobs_full(1, 1)); + KUNIT_EXPECT_TRUE(test, apr_jobs_full(3, 1)); + + /* A full ring of decode jobs straddling the wrap, retired in order. */ + done = r - 3 * APR_DECODE_DESC_BYTES - 0x80; + head = done; + for (i = 0; i < APR_MAX_INFLIGHT; i++) { + KUNIT_ASSERT_GT(test, apr_ring_free(done, head, r), + (u32)APR_DECODE_DESC_BYTES); + head = apr_ring_advance(head, APR_DECODE_DESC_BYTES, r); + ends[i] = head; + } + KUNIT_EXPECT_EQ(test, apr_ring_distance(done, head, r), + (u32)(APR_MAX_INFLIGHT * APR_DECODE_DESC_BYTES)); + /* The tail stops inside the fifth descriptor: four jobs are done. */ + tail = apr_ring_advance(ends[3], 0x40, r); + KUNIT_ASSERT_TRUE(test, apr_ring_tail_valid(done, tail, head, r)); + for (retired = 0; retired < APR_MAX_INFLIGHT; retired++) { + if (!apr_ring_passed(done, ends[retired], tail, r)) + break; + done = ends[retired]; + } + KUNIT_EXPECT_EQ(test, retired, 4U); + KUNIT_EXPECT_EQ(test, done, ends[3]); + /* Then the engine drains the ring. */ + tail = head; + KUNIT_ASSERT_TRUE(test, apr_ring_tail_valid(done, tail, head, r)); + for (; retired < APR_MAX_INFLIGHT; retired++) { + if (!apr_ring_passed(done, ends[retired], tail, r)) + break; + done = ends[retired]; + } + KUNIT_EXPECT_EQ(test, retired, (unsigned int)APR_MAX_INFLIGHT); + KUNIT_EXPECT_EQ(test, done, head); + KUNIT_EXPECT_EQ(test, apr_ring_free(done, head, r), r); +} + static struct kunit_case apr_test_cases[] = { KUNIT_CASE(apr_test_qmat_regs), KUNIT_CASE(apr_test_dc_scale), @@ -722,6 +816,9 @@ static struct kunit_case apr_test_cases[] = { KUNIT_CASE(apr_test_parse_interlaced), KUNIT_CASE(apr_test_parse_many_slices), KUNIT_CASE(apr_test_ring_accounting), + KUNIT_CASE(apr_test_ring_space), + KUNIT_CASE(apr_test_ring_tail), + KUNIT_CASE(apr_test_inflight), {} }; diff --git a/drivers/media/platform/apple/prores/apple-prores.h b/drivers/media/platform/apple/prores/apple-prores.h index d2e4d4ece3136..0c4c1bafba9bd 100644 --- a/drivers/media/platform/apple/prores/apple-prores.h +++ b/drivers/media/platform/apple/prores/apple-prores.h @@ -65,6 +65,21 @@ /* Kept for the encode descriptor tests and callers. */ #define APR_DESC_BYTES APR_ENCODE_DESC_BYTES +/* + * Jobs the driver keeps in the ring at once. The ring itself holds hundreds + * of descriptors; the limit bounds the buffers and runtime-PM references a + * timeout has to fail and the latency of a stream-off. Eight covers the + * decode gap to VideoToolbox (several frames outstanding) with slack for the + * scheduling delay between jobs. + */ +#define APR_MAX_INFLIGHT 8 +/* + * Encode stays at one job in the ring: every encode job uses the one shared + * statistics area, and nothing public shows that two encodes may overlap in + * it. Encode already runs at macOS speed with one job outstanding. + */ +#define APR_ENCODE_MAX_INFLIGHT 1 + #define APRI_T6041_FIRMWARE_NAME "apple/prores-t6041-init.bin" #define APRI_T6041_FIRMWARE_SIZE 2728 #define APRI_T6050_FIRMWARE_NAME "apple/prores-t6050-init.bin" @@ -218,6 +233,11 @@ static inline int apr_parse_t6041_init(const u8 *data, size_t size, return apr_parse_init(&apri_t6041, data, size, init); } u32 apr_ring_advance(u32 ptr, u32 bytes, u32 ring_bytes); +u32 apr_ring_distance(u32 from, u32 to, u32 ring_bytes); +u32 apr_ring_free(u32 done, u32 head, u32 ring_bytes); +bool apr_ring_passed(u32 done, u32 end, u32 tail, u32 ring_bytes); +bool apr_ring_tail_valid(u32 done, u32 tail, u32 head, u32 ring_bytes); +bool apr_jobs_full(unsigned int jobs, unsigned int encodes); void apr_ring_write(u8 *ring, u32 ring_bytes, u32 ptr, const u8 *desc, u32 desc_bytes); From 722d76615fa833c1f6b2782428f32e9c09e12f1e Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 08:24:16 +1100 Subject: [PATCH 2/3] media: apple: prores: retire decode jobs on the engine's finished-job id With several decode jobs in the ring, the driver gave a job's buffers back once the consumer tail had passed its descriptor. The tail moves when the engine fetches a descriptor, not when the frame is written, so buffers went back to vb2 while the engine still wrote them: wrong output on 4:4:4 clips and DART faults at the end of a run. The engine reports the id of the last decode job it has finished in the upper half of ST1; the decode command word already carries a 16-bit id. Give decode jobs device-wide ids that skip 0 (ST1 reads 0 after a reset), read ST1 after the status clear, and retire decode jobs in ring order once their id is reported finished. An encode descriptor carries no id, so an encode only enters an empty ring and completes on its interrupt, as before. On an M1 Pro (T6000) with eight jobs in the ring every clip decodes to the same bytes as with one job, with no DART faults; 1080p 422 HQ goes from about 2730 to 2920 fps, 4K 422 HQ from 761 to 775 fps. Signed-off-by: Ryan Murray --- .../platform/apple/prores/apple-prores-desc.c | 22 ++++++++++++ .../platform/apple/prores/apple-prores-drv.c | 34 +++++++++++++++---- .../platform/apple/prores/apple-prores-test.c | 16 +++++++++ .../platform/apple/prores/apple-prores.h | 4 +++ 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/drivers/media/platform/apple/prores/apple-prores-desc.c b/drivers/media/platform/apple/prores/apple-prores-desc.c index 8844574081bf9..020a641ce9523 100644 --- a/drivers/media/platform/apple/prores/apple-prores-desc.c +++ b/drivers/media/platform/apple/prores/apple-prores-desc.c @@ -725,6 +725,28 @@ bool apr_ring_tail_valid(u32 done, u32 tail, u32 head, u32 ring_bytes) } EXPORT_SYMBOL_IF_KUNIT(apr_ring_tail_valid); +/* + * Decode job ids are device-wide and never 0: the engine reports the id of + * the last job it finished, and that report reads 0 after a reset, before + * any job has run. + */ +u16 apr_next_job_id(u16 id) +{ + return id == U16_MAX ? 1 : id + 1; +} +EXPORT_SYMBOL_IF_KUNIT(apr_next_job_id); + +/* + * Has the engine finished job @id, given the last finished id @done_id? + * Jobs finish in ring order and far fewer than 2^15 are ever outstanding, + * so the signed distance decides, across the wrap as well. + */ +bool apr_job_id_done(u16 done_id, u16 id) +{ + return (u16)(done_id - id) < 0x8000; +} +EXPORT_SYMBOL_IF_KUNIT(apr_job_id_done); + /* No further job may enter the ring with @jobs in it, @encodes of them encodes. */ bool apr_jobs_full(unsigned int jobs, unsigned int encodes) { diff --git a/drivers/media/platform/apple/prores/apple-prores-drv.c b/drivers/media/platform/apple/prores/apple-prores-drv.c index a4045f808c806..d1fecc2385733 100644 --- a/drivers/media/platform/apple/prores/apple-prores-drv.c +++ b/drivers/media/platform/apple/prores/apple-prores-drv.c @@ -4,10 +4,14 @@ * * The driver writes job descriptors into a 128 KiB DMA ring and advances the * producer head; the engine works through the ring and reports progress with - * a consumer tail and a completion interrupt. Up to APR_MAX_INFLIGHT jobs sit - * in the ring at once: a job's buffers leave the m2m queues when its + * a consumer tail and a completion interrupt. Up to APR_MAX_INFLIGHT decode + * jobs sit in the ring at once: a job's buffers leave the m2m queues when its * descriptor is published, so v4l2-m2m can run the next job while the engine - * is busy, and they complete in ring order once the tail passes them. + * is busy. The tail moves when the engine fetches a descriptor, not when it + * has finished the frame, so decode jobs complete in ring order once the + * engine reports their job id as finished in ST1. An encode descriptor + * carries no job id: it only enters an empty ring and completes on its + * interrupt, as a lone job. * The encoder consumes semi-planar 4:2:2 or 4:4:4 frames and produces one * complete ProRes frame per buffer. The decoder exposes a second stateful * node with the queue directions reversed and linear color output. @@ -16,6 +20,7 @@ * engine under Linux, and on the m1n1 proxyclient ProRes experiment (MIT). */ #include +#include #include #include #include @@ -875,6 +880,7 @@ struct apr_job { struct vb2_v4l2_buffer *src; struct vb2_v4l2_buffer *dst; u32 end; /* ring pointer just past its descriptor */ + u16 id; /* decode job id in the descriptor */ u32 sequence; u32 bytes; /* decoded payload */ int err; /* forced result: engine error or cancellation */ @@ -917,6 +923,7 @@ struct apr_dev { struct apr_ctx *held; /* m2m job left running while the ring is full */ u32 head; /* next free ring offset */ u32 done; /* the engine has consumed the ring up to here */ + u16 next_id; /* id for the next decode job, never 0 */ bool ring_ready; /* engine registers programmed since power-up */ bool broken; /* timed out with no way to reset */ bool hung; /* ring state is inconsistent, reset now */ @@ -1020,6 +1027,7 @@ static void apr_hw_init(struct apr_dev *apr) /* Order the setup before any descriptor publication. */ wmb(); apr->head = 0; + apr->next_id = 1; apr->done = 0; apr->ring_ready = true; } @@ -1145,7 +1153,6 @@ static int apr_fill_decode_frame(struct apr_ctx *ctx, src->vb2_buf.planes[0].data_offset, .luma = output, .chroma = output + (u64)luma_stride * d->height, - .job_id = ctx->sequence, }; return 0; } @@ -1250,6 +1257,7 @@ static bool apr_take_done(struct apr_dev *apr, struct apr_job *out, u32 status, tail, st0, st1, st2; u32 detail[6]; unsigned int i; + u16 done_id; *n = 0; status = apr_read(apr, APR_REG_IRQ_STATUS); @@ -1288,10 +1296,15 @@ static bool apr_take_done(struct apr_dev *apr, struct apr_job *out, apr->hung = true; return true; } + /* Read after the W1C: a job finishing later raises DONE again. */ + done_id = FIELD_GET(APR_ST1_DONE_ID, apr_read(apr, APR_REG_ST1)); while (apr->njobs) { struct apr_job *job = &apr->jobs[apr->job_first]; - if (!apr_ring_passed(apr->done, job->end, tail, ring_bytes)) + if (job->ctx->decode ? + !apr_job_id_done(done_id, job->id) : + !(status & (APR_IRQ_DONE | APR_IRQ_ERROR)) || + !apr_ring_passed(apr->done, job->end, tail, ring_bytes)) break; out[(*n)++] = *job; apr->done = job->end; @@ -1471,9 +1484,11 @@ static void apr_device_run(void *priv) } spin_lock_irqsave(&apr->hw_lock, flags); - while (apr->resetting) { + /* An encode has no job id, so it only runs alone in the ring. */ + while (apr->resetting || (!ctx->decode && apr->njobs)) { spin_unlock_irqrestore(&apr->hw_lock, flags); - wait_event(apr->idle_wq, !READ_ONCE(apr->resetting)); + wait_event(apr->idle_wq, !READ_ONCE(apr->resetting) && + (ctx->decode || !READ_ONCE(apr->njobs))); spin_lock_irqsave(&apr->hw_lock, flags); } if (apr->broken) { @@ -1489,6 +1504,8 @@ static void apr_device_run(void *priv) apr_finish_job(apr, ctx, -EBUSY); return; } + if (ctx->decode) + decode.job_id = apr->next_id; ret = ctx->decode ? apr_build_decode_desc(&decode, desc) : apr->variant->build_desc(&f, desc); if (ret) { @@ -1504,6 +1521,7 @@ static void apr_device_run(void *priv) .src = v4l2_m2m_src_buf_remove(m2m), .dst = v4l2_m2m_dst_buf_remove(m2m), .end = apr->head, + .id = ctx->decode ? apr->next_id : 0, .sequence = ctx->sequence++, .bytes = ctx->dst_fmt.plane_fmt[0].sizeimage, }; @@ -1513,6 +1531,8 @@ static void apr_device_run(void *priv) apr->njobs++; if (!ctx->decode) apr->nencodes++; + if (ctx->decode) + apr->next_id = apr_next_job_id(apr->next_id); atomic_inc(&ctx->inflight); atomic_inc(&apr->outstanding); /* Descriptor and buffers must be visible before the head moves. */ diff --git a/drivers/media/platform/apple/prores/apple-prores-test.c b/drivers/media/platform/apple/prores/apple-prores-test.c index 3ddb07e371a62..3da8230dbc3f7 100644 --- a/drivers/media/platform/apple/prores/apple-prores-test.c +++ b/drivers/media/platform/apple/prores/apple-prores-test.c @@ -794,6 +794,21 @@ static void apr_test_inflight(struct kunit *test) KUNIT_EXPECT_EQ(test, apr_ring_free(done, head, r), r); } +static void apr_test_job_id(struct kunit *test) +{ + /* Ids are never 0, which ST1 reads after a reset. */ + KUNIT_EXPECT_EQ(test, apr_next_job_id(1), 2); + KUNIT_EXPECT_EQ(test, apr_next_job_id(U16_MAX), 1); + KUNIT_EXPECT_FALSE(test, apr_job_id_done(0, 1)); + KUNIT_EXPECT_TRUE(test, apr_job_id_done(1, 1)); + KUNIT_EXPECT_TRUE(test, apr_job_id_done(5, 3)); + KUNIT_EXPECT_FALSE(test, apr_job_id_done(3, 5)); + /* Across the wrap: 65535 finishes before 1 and 2. */ + KUNIT_EXPECT_TRUE(test, apr_job_id_done(1, U16_MAX)); + KUNIT_EXPECT_FALSE(test, apr_job_id_done(U16_MAX, 1)); + KUNIT_EXPECT_FALSE(test, apr_job_id_done(U16_MAX - 1, 2)); +} + static struct kunit_case apr_test_cases[] = { KUNIT_CASE(apr_test_qmat_regs), KUNIT_CASE(apr_test_dc_scale), @@ -819,6 +834,7 @@ static struct kunit_case apr_test_cases[] = { KUNIT_CASE(apr_test_ring_space), KUNIT_CASE(apr_test_ring_tail), KUNIT_CASE(apr_test_inflight), + KUNIT_CASE(apr_test_job_id), {} }; diff --git a/drivers/media/platform/apple/prores/apple-prores.h b/drivers/media/platform/apple/prores/apple-prores.h index 0c4c1bafba9bd..dc863ae6daf83 100644 --- a/drivers/media/platform/apple/prores/apple-prores.h +++ b/drivers/media/platform/apple/prores/apple-prores.h @@ -29,6 +29,8 @@ #define APR_IRQ_ERROR BIT(1) #define APR_REG_ST0 0x0014 #define APR_REG_ST1 0x0018 +/* Job id of the last descriptor the engine has finished (decode command 31:16). */ +#define APR_ST1_DONE_ID GENMASK(31, 16) #define APR_REG_ST2 0x001c #define APR_REG_DETAIL0 0x003c #define APR_REG_DETAIL1_LO 0x0040 @@ -237,6 +239,8 @@ u32 apr_ring_distance(u32 from, u32 to, u32 ring_bytes); u32 apr_ring_free(u32 done, u32 head, u32 ring_bytes); bool apr_ring_passed(u32 done, u32 end, u32 tail, u32 ring_bytes); bool apr_ring_tail_valid(u32 done, u32 tail, u32 head, u32 ring_bytes); +u16 apr_next_job_id(u16 id); +bool apr_job_id_done(u16 done_id, u16 id); bool apr_jobs_full(unsigned int jobs, unsigned int encodes); void apr_ring_write(u8 *ring, u32 ring_bytes, u32 ptr, const u8 *desc, u32 desc_bytes); From b8c05f917a2ab2dc2a4873d43bb53d7a69f45799 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 14:30:01 +1100 Subject: [PATCH 3/3] media: apple: prores: never give back a buffer the engine may still use Several paths could still hand buffers back while the engine owned them or would use them: - A DONE or ERROR latched after the last job was retired by id stayed set. An encode published into the then empty ring was taken for finished on it, because the tail passes an encode as soon as it is fetched. The engine is idle while the ring is empty, so clear both status bits before publishing into an empty ring. - ST1 = 0 counted as "finished" for every id in 0x8001..0xffff, and an id not issued yet retired the whole ring. 0 now means no progress, and an id at or after the next one to issue marks the ring hung, so the watchdog resets the engine before any buffer goes back. - A job that fails before it reaches the ring (a frame that does not parse, an interlaced frame) completed at once, ahead of up to seven earlier frames of the same context. As the drain's last source buffer it sent LAST and EOS first and the frames still in the ring were lost. It now waits until the context's jobs in the ring are given back. - apr_finish_job() removed the source and destination buffers outside hw_lock, so a STOP could see the destination half moved. Move both under the lock, as device_run does. Also correct the watchdog comments (it fires when no job finishes, not when the tail stops) and the ring test comment. Signed-off-by: Ryan Murray --- .../platform/apple/prores/apple-prores-desc.c | 16 +++++ .../platform/apple/prores/apple-prores-drv.c | 52 +++++++++++++--- .../platform/apple/prores/apple-prores-test.c | 60 ++++++++++++++++++- .../platform/apple/prores/apple-prores.h | 8 +++ 4 files changed, 126 insertions(+), 10 deletions(-) diff --git a/drivers/media/platform/apple/prores/apple-prores-desc.c b/drivers/media/platform/apple/prores/apple-prores-desc.c index 020a641ce9523..3ab8e6ca73dd3 100644 --- a/drivers/media/platform/apple/prores/apple-prores-desc.c +++ b/drivers/media/platform/apple/prores/apple-prores-desc.c @@ -747,6 +747,22 @@ bool apr_job_id_done(u16 done_id, u16 id) } EXPORT_SYMBOL_IF_KUNIT(apr_job_id_done); +/* + * Classify @done_id, the last finished id ST1 reports, against @next_id, the + * id the next decode job gets. 0 is what ST1 reads before any job finished + * since the reset. Ids from @next_id on, half the id space, are not issued + * yet; the other half are issued ids, finished or still in the ring. + */ +enum apr_done_id apr_job_id_check(u16 done_id, u16 next_id) +{ + if (!done_id) + return APR_DONE_ID_NONE; + if (apr_job_id_done(done_id, next_id)) + return APR_DONE_ID_FUTURE; + return APR_DONE_ID_ISSUED; +} +EXPORT_SYMBOL_IF_KUNIT(apr_job_id_check); + /* No further job may enter the ring with @jobs in it, @encodes of them encodes. */ bool apr_jobs_full(unsigned int jobs, unsigned int encodes) { diff --git a/drivers/media/platform/apple/prores/apple-prores-drv.c b/drivers/media/platform/apple/prores/apple-prores-drv.c index d1fecc2385733..1721beb0223e1 100644 --- a/drivers/media/platform/apple/prores/apple-prores-drv.c +++ b/drivers/media/platform/apple/prores/apple-prores-drv.c @@ -1204,7 +1204,7 @@ static noinline void apr_complete(struct apr_dev *apr, struct apr_job *job) * as soon as its in-flight count drops, so it is not touched after that. */ static void apr_retire(struct apr_dev *apr, struct apr_job *jobs, - unsigned int n) + unsigned int n) { unsigned int i; @@ -1219,18 +1219,28 @@ static void apr_retire(struct apr_dev *apr, struct apr_job *jobs, wake_up_all(&apr->idle_wq); } -/* Fail the current m2m job before its descriptor reached the ring. */ +/* + * Fail the current m2m job before its descriptor reached the ring. Earlier + * jobs of the context may still be in the ring: they are given back first, + * so this one cannot overtake them. That matters most when it is the drain's + * last source buffer, whose LAST flag and EOS end the stream. Sleeps. + */ static void apr_finish_job(struct apr_dev *apr, struct apr_ctx *ctx, int err) { struct v4l2_m2m_ctx *m2m = ctx->fh.m2m_ctx; struct apr_job job = { .ctx = ctx, .err = err, - .sequence = ctx->sequence++, }; + unsigned long flags; + wait_event(apr->idle_wq, !atomic_read(&ctx->inflight)); + /* Buffers move under hw_lock, which apr_stop_cmd() relies on. */ + spin_lock_irqsave(&apr->hw_lock, flags); + job.sequence = ctx->sequence++; job.src = v4l2_m2m_src_buf_remove(m2m); job.dst = v4l2_m2m_dst_buf_remove(m2m); + spin_unlock_irqrestore(&apr->hw_lock, flags); if (!WARN_ON(!job.src || !job.dst)) apr_complete(apr, &job); pm_runtime_mark_last_busy(apr->dev); @@ -1246,9 +1256,17 @@ static void apr_finish_job(struct apr_dev *apr, struct apr_ctx *ctx, int err) * DONE and ERROR are latched per engine, not per descriptor: the status and * detail registers do not say which descriptor failed. An ERROR therefore * fails every job in the ring at that moment; the engine keeps running and - * the watchdog resets it if the tail stops moving. Several descriptors may - * finish behind one interrupt, and a DONE may arrive for a job already taken - * here on the tail alone; both are fine. + * the watchdog resets it if no job finishes for APR_TIMEOUT_MS. Several + * descriptors may finish behind one interrupt. + * + * A decode job is finished once ST1 reports its id. ST1 reads 0 before any + * job finished since the reset, which is no progress; an id the driver has + * not issued yet means the engine and the ring disagree, so the ring is + * declared hung and the watchdog resets the engine before any buffer goes + * back. An encode has no id and retires on DONE once the tail passed it. + * A DONE still latched after the last job was taken here would make a new + * encode look finished as soon as it is fetched, so device_run clears the + * status before it publishes into an empty ring. */ static bool apr_take_done(struct apr_dev *apr, struct apr_job *out, unsigned int *n) @@ -1256,6 +1274,7 @@ static bool apr_take_done(struct apr_dev *apr, struct apr_job *out, u32 ring_bytes = apr->variant->ring_bytes; u32 status, tail, st0, st1, st2; u32 detail[6]; + enum apr_done_id state; unsigned int i; u16 done_id; @@ -1298,10 +1317,18 @@ static bool apr_take_done(struct apr_dev *apr, struct apr_job *out, } /* Read after the W1C: a job finishing later raises DONE again. */ done_id = FIELD_GET(APR_ST1_DONE_ID, apr_read(apr, APR_REG_ST1)); + state = apr_job_id_check(done_id, apr->next_id); + if (state == APR_DONE_ID_FUTURE && apr->njobs > apr->nencodes) { + dev_err_ratelimited(apr->dev, "finished id %#x not issued yet, next %#x\n", + done_id, apr->next_id); + apr->hung = true; + return true; + } while (apr->njobs) { struct apr_job *job = &apr->jobs[apr->job_first]; if (job->ctx->decode ? + state != APR_DONE_ID_ISSUED || !apr_job_id_done(done_id, job->id) : !(status & (APR_IRQ_DONE | APR_IRQ_ERROR)) || !apr_ring_passed(apr->done, job->end, tail, ring_bytes)) @@ -1358,8 +1385,8 @@ static irqreturn_t apr_irq(int irq, void *data) } /* - * Backstop for a lost interrupt and for hangs. A ring that stops moving for - * APR_TIMEOUT_MS fails every job in it. A hung engine is reset through its + * Backstop for a lost interrupt and for hangs. When no job finishes for + * APR_TIMEOUT_MS, every job in the ring fails. 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. */ @@ -1513,6 +1540,14 @@ static void apr_device_run(void *priv) apr_finish_job(apr, ctx, ret); return; } + /* + * With the ring empty the engine is idle, so a DONE or ERROR still + * latched belongs to a job already given back. Clear it before this + * job can be taken for finished on it. The clear reaches the engine + * before the head write below. + */ + if (!apr->njobs) + apr_write(apr, APR_REG_IRQ_STATUS, APR_IRQ_DONE | APR_IRQ_ERROR); apr_ring_write(apr->ring, ring_bytes, apr->head, desc, desc_bytes); apr->head = apr_ring_advance(apr->head, desc_bytes, ring_bytes); job = &apr->jobs[(apr->job_first + apr->njobs) % APR_MAX_INFLIGHT]; @@ -1568,7 +1603,6 @@ static void apr_job_abort(void *priv) struct apr_ctx *ctx = priv; struct apr_dev *apr = ctx->apr; unsigned long flags; - unsigned int i; /* diff --git a/drivers/media/platform/apple/prores/apple-prores-test.c b/drivers/media/platform/apple/prores/apple-prores-test.c index 3da8230dbc3f7..d0159ae07ca33 100644 --- a/drivers/media/platform/apple/prores/apple-prores-test.c +++ b/drivers/media/platform/apple/prores/apple-prores-test.c @@ -760,7 +760,11 @@ static void apr_test_inflight(struct kunit *test) KUNIT_EXPECT_TRUE(test, apr_jobs_full(1, 1)); KUNIT_EXPECT_TRUE(test, apr_jobs_full(3, 1)); - /* A full ring of decode jobs straddling the wrap, retired in order. */ + /* + * Ring space and tail arithmetic for a full ring straddling the wrap. + * Only the lone encode retires on the tail; decode jobs retire on + * their ids (apr_test_job_id). + */ done = r - 3 * APR_DECODE_DESC_BYTES - 0x80; head = done; for (i = 0; i < APR_MAX_INFLIGHT; i++) { @@ -807,6 +811,60 @@ static void apr_test_job_id(struct kunit *test) KUNIT_EXPECT_TRUE(test, apr_job_id_done(1, U16_MAX)); KUNIT_EXPECT_FALSE(test, apr_job_id_done(U16_MAX, 1)); KUNIT_EXPECT_FALSE(test, apr_job_id_done(U16_MAX - 1, 2)); + + /* ST1 = 0 is no progress, never "everything in 0x8001..0xffff done". */ + KUNIT_EXPECT_EQ(test, apr_job_id_check(0, 9), APR_DONE_ID_NONE); + KUNIT_EXPECT_EQ(test, apr_job_id_check(0, 0x8005), APR_DONE_ID_NONE); + KUNIT_EXPECT_TRUE(test, apr_job_id_done(0, 0x8001)); + /* Ids 1..8 outstanding: ids already issued, finished or not. */ + KUNIT_EXPECT_EQ(test, apr_job_id_check(1, 9), APR_DONE_ID_ISSUED); + KUNIT_EXPECT_EQ(test, apr_job_id_check(8, 9), APR_DONE_ID_ISSUED); + /* The other half of the id space reads as old ids: nothing retires. */ + for (u32 d = 0x8009; d <= U16_MAX; d += 0x7ff) { + KUNIT_EXPECT_EQ(test, apr_job_id_check(d, 9), APR_DONE_ID_ISSUED); + for (u16 id = 1; id < 9; id++) + KUNIT_EXPECT_FALSE(test, apr_job_id_done(d, id)); + } + KUNIT_EXPECT_EQ(test, apr_job_id_check(U16_MAX, 9), APR_DONE_ID_ISSUED); + KUNIT_EXPECT_FALSE(test, apr_job_id_done(U16_MAX, 1)); + /* An id not issued yet: the ring is hung, nothing retires. */ + KUNIT_EXPECT_EQ(test, apr_job_id_check(9, 9), APR_DONE_ID_FUTURE); + KUNIT_EXPECT_EQ(test, apr_job_id_check(10, 9), APR_DONE_ID_FUTURE); + KUNIT_EXPECT_EQ(test, apr_job_id_check(0x8008, 9), APR_DONE_ID_FUTURE); + /* First job after a reset: id 1 not issued until next_id moves on. */ + KUNIT_EXPECT_EQ(test, apr_job_id_check(1, 1), APR_DONE_ID_FUTURE); + + /* Eight jobs across the wrap: 0xfffc..0xffff, then 1..4 (0 skipped). */ + { + u16 ids[APR_MAX_INFLIGHT], next = 0xfffc; + unsigned int i, retired = 0; + + for (i = 0; i < APR_MAX_INFLIGHT; i++) { + ids[i] = next; + next = apr_next_job_id(next); + } + KUNIT_EXPECT_EQ(test, ids[4], 1); + KUNIT_EXPECT_EQ(test, next, 5); + /* ST1 skips ahead to 2: six jobs finished, in ring order. */ + KUNIT_EXPECT_EQ(test, apr_job_id_check(2, next), APR_DONE_ID_ISSUED); + while (retired < APR_MAX_INFLIGHT && + apr_job_id_done(2, ids[retired])) + retired++; + KUNIT_EXPECT_EQ(test, retired, 6U); + /* The same id again retires nothing more. */ + while (retired < APR_MAX_INFLIGHT && + apr_job_id_done(2, ids[retired])) + retired++; + KUNIT_EXPECT_EQ(test, retired, 6U); + /* A stale id from before the wrap retires nothing more either. */ + KUNIT_EXPECT_FALSE(test, apr_job_id_done(0xfffe, ids[6])); + KUNIT_EXPECT_EQ(test, apr_job_id_check(4, next), APR_DONE_ID_ISSUED); + KUNIT_EXPECT_TRUE(test, apr_job_id_done(4, ids[7])); + /* 5 = next_id: not issued, hung. */ + KUNIT_EXPECT_EQ(test, apr_job_id_check(5, next), APR_DONE_ID_FUTURE); + KUNIT_EXPECT_EQ(test, apr_job_id_check(0x8004, next), APR_DONE_ID_FUTURE); + KUNIT_EXPECT_EQ(test, apr_job_id_check(0x8005, next), APR_DONE_ID_ISSUED); + } } static struct kunit_case apr_test_cases[] = { diff --git a/drivers/media/platform/apple/prores/apple-prores.h b/drivers/media/platform/apple/prores/apple-prores.h index dc863ae6daf83..ac4d1dca5ab50 100644 --- a/drivers/media/platform/apple/prores/apple-prores.h +++ b/drivers/media/platform/apple/prores/apple-prores.h @@ -241,6 +241,14 @@ bool apr_ring_passed(u32 done, u32 end, u32 tail, u32 ring_bytes); bool apr_ring_tail_valid(u32 done, u32 tail, u32 head, u32 ring_bytes); u16 apr_next_job_id(u16 id); bool apr_job_id_done(u16 done_id, u16 id); + +enum apr_done_id { + APR_DONE_ID_NONE, /* nothing finished since the reset */ + APR_DONE_ID_ISSUED, /* an id the driver issued */ + APR_DONE_ID_FUTURE, /* an id not issued yet: the ring is hung */ +}; + +enum apr_done_id apr_job_id_check(u16 done_id, u16 next_id); bool apr_jobs_full(unsigned int jobs, unsigned int encodes); void apr_ring_write(u8 *ring, u32 ring_bytes, u32 ptr, const u8 *desc, u32 desc_bytes);