1 // SPDX-License-Identifier: MIT
2 #include <linux/string.h>
3 #include <drm/drm_crtc.h>
4 #include <drm/drm_atomic_helper.h>
5 #include <drm/drm_vblank.h>
6 #include <drm/drm_vblank_work.h>
8 #include <nvif/class.h>
9 #include <nvif/cl0002.h>
10 #include <nvif/timer.h>
12 #include <nvhw/class/cl907d.h>
14 #include "nouveau_drv.h"
21 static const char * const nv50_crc_sources[] = {
22 [NV50_CRC_SOURCE_NONE] = "none",
23 [NV50_CRC_SOURCE_AUTO] = "auto",
24 [NV50_CRC_SOURCE_RG] = "rg",
25 [NV50_CRC_SOURCE_OUTP_ACTIVE] = "outp-active",
26 [NV50_CRC_SOURCE_OUTP_COMPLETE] = "outp-complete",
27 [NV50_CRC_SOURCE_OUTP_INACTIVE] = "outp-inactive",
30 static int nv50_crc_parse_source(const char *buf, enum nv50_crc_source *s)
35 *s = NV50_CRC_SOURCE_NONE;
39 i = match_string(nv50_crc_sources, ARRAY_SIZE(nv50_crc_sources), buf);
48 nv50_crc_verify_source(struct drm_crtc *crtc, const char *source_name,
51 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
52 enum nv50_crc_source source;
54 if (nv50_crc_parse_source(source_name, &source) < 0) {
55 NV_DEBUG(drm, "unknown source %s\n", source_name);
63 const char *const *nv50_crc_get_sources(struct drm_crtc *crtc, size_t *count)
65 *count = ARRAY_SIZE(nv50_crc_sources);
66 return nv50_crc_sources;
70 nv50_crc_program_ctx(struct nv50_head *head,
71 struct nv50_crc_notifier_ctx *ctx)
73 struct nv50_disp *disp = nv50_disp(head->base.base.dev);
74 struct nv50_core *core = disp->core;
75 u32 interlock[NV50_DISP_INTERLOCK__SIZE] = { 0 };
77 core->func->crc->set_ctx(head, ctx);
78 core->func->update(core, interlock, false);
81 static void nv50_crc_ctx_flip_work(struct kthread_work *base)
83 struct drm_vblank_work *work = to_drm_vblank_work(base);
84 struct nv50_crc *crc = container_of(work, struct nv50_crc, flip_work);
85 struct nv50_head *head = container_of(crc, struct nv50_head, crc);
86 struct drm_crtc *crtc = &head->base.base;
87 struct drm_device *dev = crtc->dev;
88 struct nv50_disp *disp = nv50_disp(dev);
89 const uint64_t start_vbl = drm_crtc_vblank_count(crtc);
91 u8 new_idx = crc->ctx_idx ^ 1;
94 * We don't want to accidentally wait for longer then the vblank, so
95 * try again for the next vblank if we don't grab the lock
97 if (!mutex_trylock(&disp->mutex)) {
98 drm_dbg_kms(dev, "Lock contended, delaying CRC ctx flip for %s\n", crtc->name);
99 drm_vblank_work_schedule(work, start_vbl + 1, true);
103 drm_dbg_kms(dev, "Flipping notifier ctx for %s (%d -> %d)\n",
104 crtc->name, crc->ctx_idx, new_idx);
106 nv50_crc_program_ctx(head, NULL);
107 nv50_crc_program_ctx(head, &crc->ctx[new_idx]);
108 mutex_unlock(&disp->mutex);
110 end_vbl = drm_crtc_vblank_count(crtc);
111 if (unlikely(end_vbl != start_vbl))
112 NV_ERROR(nouveau_drm(dev),
113 "Failed to flip CRC context on %s on time (%llu > %llu)\n",
114 crtc->name, end_vbl, start_vbl);
116 spin_lock_irq(&crc->lock);
117 crc->ctx_changed = true;
118 spin_unlock_irq(&crc->lock);
121 static inline void nv50_crc_reset_ctx(struct nv50_crc_notifier_ctx *ctx)
123 memset_io(ctx->mem.object.map.ptr, 0, ctx->mem.object.map.size);
127 nv50_crc_get_entries(struct nv50_head *head,
128 const struct nv50_crc_func *func,
129 enum nv50_crc_source source)
131 struct drm_crtc *crtc = &head->base.base;
132 struct nv50_crc *crc = &head->crc;
135 while (crc->entry_idx < func->num_entries) {
137 * While Nvidia's documentation says CRCs are written on each
138 * subsequent vblank after being enabled, in practice they
139 * aren't written immediately.
141 output_crc = func->get_entry(head, &crc->ctx[crc->ctx_idx],
142 source, crc->entry_idx);
146 drm_crtc_add_crc_entry(crtc, true, crc->frame, &output_crc);
152 void nv50_crc_handle_vblank(struct nv50_head *head)
154 struct drm_crtc *crtc = &head->base.base;
155 struct nv50_crc *crc = &head->crc;
156 const struct nv50_crc_func *func =
157 nv50_disp(head->base.base.dev)->core->func->crc;
158 struct nv50_crc_notifier_ctx *ctx;
159 bool need_reschedule = false;
165 * We don't lose events if we aren't able to report CRCs until the
166 * next vblank, so only report CRCs if the locks we need aren't
167 * contended to prevent missing an actual vblank event
169 if (!spin_trylock(&crc->lock))
175 ctx = &crc->ctx[crc->ctx_idx];
176 if (crc->ctx_changed && func->ctx_finished(head, ctx)) {
177 nv50_crc_get_entries(head, func, crc->src);
181 crc->ctx_changed = false;
184 * Unfortunately when notifier contexts are changed during CRC
185 * capture, we will inevitably lose the CRC entry for the
186 * frame where the hardware actually latched onto the first
187 * UPDATE. According to Nvidia's hardware engineers, there's
188 * no workaround for this.
190 * Now, we could try to be smart here and calculate the number
191 * of missed CRCs based on audit timestamps, but those were
192 * removed starting with volta. Since we always flush our
193 * updates back-to-back without waiting, we'll just be
194 * optimistic and assume we always miss exactly one frame.
196 drm_dbg_kms(head->base.base.dev,
197 "Notifier ctx flip for head-%d finished, lost CRC for frame %llu\n",
198 head->base.index, crc->frame);
201 nv50_crc_reset_ctx(ctx);
202 need_reschedule = true;
205 nv50_crc_get_entries(head, func, crc->src);
208 drm_vblank_work_schedule(&crc->flip_work,
209 drm_crtc_vblank_count(crtc)
210 + crc->flip_threshold
215 spin_unlock(&crc->lock);
218 static void nv50_crc_wait_ctx_finished(struct nv50_head *head,
219 const struct nv50_crc_func *func,
220 struct nv50_crc_notifier_ctx *ctx)
222 struct drm_device *dev = head->base.base.dev;
223 struct nouveau_drm *drm = nouveau_drm(dev);
226 ret = nvif_msec(&drm->client.device, 50,
227 if (func->ctx_finished(head, ctx)) break;);
228 if (ret == -ETIMEDOUT)
230 "CRC notifier ctx for head %d not finished after 50ms\n",
234 "CRC notifier ctx for head-%d finished after %lldns\n",
235 head->base.index, ret);
238 void nv50_crc_atomic_stop_reporting(struct drm_atomic_state *state)
240 struct drm_crtc_state *crtc_state;
241 struct drm_crtc *crtc;
244 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
245 struct nv50_head *head = nv50_head(crtc);
246 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
247 struct nv50_crc *crc = &head->crc;
252 spin_lock_irq(&crc->lock);
253 crc->src = NV50_CRC_SOURCE_NONE;
254 spin_unlock_irq(&crc->lock);
256 drm_crtc_vblank_put(crtc);
257 drm_vblank_work_cancel_sync(&crc->flip_work);
259 NV_ATOMIC(nouveau_drm(crtc->dev),
260 "CRC reporting on vblank for head-%d disabled\n",
263 /* CRC generation is still enabled in hw, we'll just report
264 * any remaining CRC entries ourselves after it gets disabled
270 void nv50_crc_atomic_init_notifier_contexts(struct drm_atomic_state *state)
272 struct drm_crtc_state *new_crtc_state;
273 struct drm_crtc *crtc;
276 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
277 struct nv50_head *head = nv50_head(crtc);
278 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
279 struct nv50_crc *crc = &head->crc;
286 crc->ctx_changed = false;
287 for (i = 0; i < ARRAY_SIZE(crc->ctx); i++)
288 nv50_crc_reset_ctx(&crc->ctx[i]);
292 void nv50_crc_atomic_release_notifier_contexts(struct drm_atomic_state *state)
294 const struct nv50_crc_func *func =
295 nv50_disp(state->dev)->core->func->crc;
296 struct drm_crtc_state *new_crtc_state;
297 struct drm_crtc *crtc;
300 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
301 struct nv50_head *head = nv50_head(crtc);
302 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
303 struct nv50_crc *crc = &head->crc;
304 struct nv50_crc_notifier_ctx *ctx = &crc->ctx[crc->ctx_idx];
309 if (crc->ctx_changed) {
310 nv50_crc_wait_ctx_finished(head, func, ctx);
311 ctx = &crc->ctx[crc->ctx_idx ^ 1];
313 nv50_crc_wait_ctx_finished(head, func, ctx);
317 void nv50_crc_atomic_start_reporting(struct drm_atomic_state *state)
319 struct drm_crtc_state *crtc_state;
320 struct drm_crtc *crtc;
323 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
324 struct nv50_head *head = nv50_head(crtc);
325 struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
326 struct nv50_crc *crc = &head->crc;
332 drm_crtc_vblank_get(crtc);
334 spin_lock_irq(&crc->lock);
335 vbl_count = drm_crtc_vblank_count(crtc);
336 crc->frame = vbl_count;
337 crc->src = asyh->crc.src;
338 drm_vblank_work_schedule(&crc->flip_work,
339 vbl_count + crc->flip_threshold,
341 spin_unlock_irq(&crc->lock);
343 NV_ATOMIC(nouveau_drm(crtc->dev),
344 "CRC reporting on vblank for head-%d enabled\n",
349 int nv50_crc_atomic_check_head(struct nv50_head *head,
350 struct nv50_head_atom *asyh,
351 struct nv50_head_atom *armh)
353 struct nv50_atom *atom = nv50_atom(asyh->state.state);
354 bool changed = armh->crc.src != asyh->crc.src;
356 if (!armh->crc.src && !asyh->crc.src) {
357 asyh->set.crc = false;
358 asyh->clr.crc = false;
362 if (drm_atomic_crtc_needs_modeset(&asyh->state) || changed) {
363 asyh->clr.crc = armh->crc.src && armh->state.active;
364 asyh->set.crc = asyh->crc.src && asyh->state.active;
366 asyh->set.or |= armh->or.crc_raster !=
369 if (asyh->clr.crc && asyh->set.crc)
370 atom->flush_disable = true;
372 asyh->set.crc = false;
373 asyh->clr.crc = false;
379 void nv50_crc_atomic_check_outp(struct nv50_atom *atom)
381 struct drm_crtc *crtc;
382 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
385 if (atom->flush_disable)
388 for_each_oldnew_crtc_in_state(&atom->state, crtc, old_crtc_state,
390 struct nv50_head_atom *armh = nv50_head_atom(old_crtc_state);
391 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
392 struct nv50_outp_atom *outp_atom;
393 struct nouveau_encoder *outp;
394 struct drm_encoder *encoder, *enc;
396 enc = nv50_head_atom_get_encoder(armh);
400 outp = nv50_real_outp(enc);
404 encoder = &outp->base.base;
410 * Re-programming ORs can't be done in the same flush as
413 list_for_each_entry(outp_atom, &atom->outp, head) {
414 if (outp_atom->encoder == encoder) {
415 if (outp_atom->set.mask) {
416 atom->flush_disable = true;
426 static enum nv50_crc_source_type
427 nv50_crc_source_type(struct nouveau_encoder *outp,
428 enum nv50_crc_source source)
430 struct dcb_output *dcbe = outp->dcb;
433 case NV50_CRC_SOURCE_NONE: return NV50_CRC_SOURCE_TYPE_NONE;
434 case NV50_CRC_SOURCE_RG: return NV50_CRC_SOURCE_TYPE_RG;
438 if (dcbe->location != DCB_LOC_ON_CHIP)
439 return NV50_CRC_SOURCE_TYPE_PIOR;
441 switch (dcbe->type) {
442 case DCB_OUTPUT_DP: return NV50_CRC_SOURCE_TYPE_SF;
443 case DCB_OUTPUT_ANALOG: return NV50_CRC_SOURCE_TYPE_DAC;
444 default: return NV50_CRC_SOURCE_TYPE_SOR;
448 void nv50_crc_atomic_set(struct nv50_head *head,
449 struct nv50_head_atom *asyh)
451 struct drm_crtc *crtc = &head->base.base;
452 struct drm_device *dev = crtc->dev;
453 struct nv50_crc *crc = &head->crc;
454 const struct nv50_crc_func *func = nv50_disp(dev)->core->func->crc;
455 struct nouveau_encoder *outp;
456 struct drm_encoder *encoder;
458 encoder = nv50_head_atom_get_encoder(asyh);
462 outp = nv50_real_outp(encoder);
466 func->set_src(head, outp->outp.or.id, nv50_crc_source_type(outp, asyh->crc.src),
467 &crc->ctx[crc->ctx_idx]);
470 void nv50_crc_atomic_clr(struct nv50_head *head)
472 const struct nv50_crc_func *func =
473 nv50_disp(head->base.base.dev)->core->func->crc;
475 func->set_src(head, 0, NV50_CRC_SOURCE_TYPE_NONE, NULL);
479 nv50_crc_raster_type(enum nv50_crc_source source)
482 case NV50_CRC_SOURCE_NONE:
483 case NV50_CRC_SOURCE_AUTO:
484 case NV50_CRC_SOURCE_RG:
485 case NV50_CRC_SOURCE_OUTP_ACTIVE:
486 return NV907D_HEAD_SET_CONTROL_OUTPUT_RESOURCE_CRC_MODE_ACTIVE_RASTER;
487 case NV50_CRC_SOURCE_OUTP_COMPLETE:
488 return NV907D_HEAD_SET_CONTROL_OUTPUT_RESOURCE_CRC_MODE_COMPLETE_RASTER;
489 case NV50_CRC_SOURCE_OUTP_INACTIVE:
490 return NV907D_HEAD_SET_CONTROL_OUTPUT_RESOURCE_CRC_MODE_NON_ACTIVE_RASTER;
496 /* We handle mapping the memory for CRC notifiers ourselves, since each
497 * notifier needs it's own handle
500 nv50_crc_ctx_init(struct nv50_head *head, struct nvif_mmu *mmu,
501 struct nv50_crc_notifier_ctx *ctx, size_t len, int idx)
503 struct nv50_core *core = nv50_disp(head->base.base.dev)->core;
506 ret = nvif_mem_ctor_map(mmu, "kmsCrcNtfy", NVIF_MEM_VRAM, len, &ctx->mem);
510 ret = nvif_object_ctor(&core->chan.base.user, "kmsCrcNtfyCtxDma",
511 NV50_DISP_HANDLE_CRC_CTX(head, idx),
513 &(struct nv_dma_v0) {
514 .target = NV_DMA_V0_TARGET_VRAM,
515 .access = NV_DMA_V0_ACCESS_RDWR,
516 .start = ctx->mem.addr,
517 .limit = ctx->mem.addr
519 }, sizeof(struct nv_dma_v0),
527 nvif_mem_dtor(&ctx->mem);
532 nv50_crc_ctx_fini(struct nv50_crc_notifier_ctx *ctx)
534 nvif_object_dtor(&ctx->ntfy);
535 nvif_mem_dtor(&ctx->mem);
538 int nv50_crc_set_source(struct drm_crtc *crtc, const char *source_str)
540 struct drm_device *dev = crtc->dev;
541 struct drm_atomic_state *state;
542 struct drm_modeset_acquire_ctx ctx;
543 struct nv50_head *head = nv50_head(crtc);
544 struct nv50_crc *crc = &head->crc;
545 const struct nv50_crc_func *func = nv50_disp(dev)->core->func->crc;
546 struct nvif_mmu *mmu = &nouveau_drm(dev)->client.mmu;
547 struct nv50_head_atom *asyh;
548 struct drm_crtc_state *crtc_state;
549 enum nv50_crc_source source;
550 int ret = 0, ctx_flags = 0, i;
552 ret = nv50_crc_parse_source(source_str, &source);
557 * Since we don't want the user to accidentally interrupt us as we're
561 ctx_flags |= DRM_MODESET_ACQUIRE_INTERRUPTIBLE;
562 drm_modeset_acquire_init(&ctx, ctx_flags);
564 state = drm_atomic_state_alloc(dev);
567 goto out_acquire_fini;
569 state->acquire_ctx = &ctx;
572 for (i = 0; i < ARRAY_SIZE(head->crc.ctx); i++) {
573 ret = nv50_crc_ctx_init(head, mmu, &crc->ctx[i],
574 func->notifier_len, i);
581 crtc_state = drm_atomic_get_crtc_state(state, &head->base.base);
582 if (IS_ERR(crtc_state)) {
583 ret = PTR_ERR(crtc_state);
589 asyh = nv50_head_atom(crtc_state);
590 asyh->crc.src = source;
591 asyh->or.crc_raster = nv50_crc_raster_type(source);
593 ret = drm_atomic_commit(state);
601 * If the user specified a custom flip threshold through
604 crc->flip_threshold = func->flip_threshold;
608 drm_modeset_drop_locks(&ctx);
610 if (!source || ret) {
611 for (i = 0; i < ARRAY_SIZE(crc->ctx); i++)
612 nv50_crc_ctx_fini(&crc->ctx[i]);
614 drm_atomic_state_put(state);
616 drm_modeset_acquire_fini(&ctx);
620 drm_atomic_state_clear(state);
621 drm_modeset_backoff(&ctx);
626 nv50_crc_debugfs_flip_threshold_get(struct seq_file *m, void *data)
628 struct nv50_head *head = m->private;
629 struct drm_crtc *crtc = &head->base.base;
630 struct nv50_crc *crc = &head->crc;
633 ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
637 seq_printf(m, "%d\n", crc->flip_threshold);
639 drm_modeset_unlock(&crtc->mutex);
644 nv50_crc_debugfs_flip_threshold_open(struct inode *inode, struct file *file)
646 return single_open(file, nv50_crc_debugfs_flip_threshold_get,
651 nv50_crc_debugfs_flip_threshold_set(struct file *file,
652 const char __user *ubuf, size_t len,
655 struct seq_file *m = file->private_data;
656 struct nv50_head *head = m->private;
657 struct nv50_head_atom *armh;
658 struct drm_crtc *crtc = &head->base.base;
659 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
660 struct nv50_crc *crc = &head->crc;
661 const struct nv50_crc_func *func =
662 nv50_disp(crtc->dev)->core->func->crc;
665 ret = kstrtoint_from_user(ubuf, len, 10, &value);
669 if (value > func->flip_threshold)
671 else if (value == -1)
672 value = func->flip_threshold;
676 ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
680 armh = nv50_head_atom(crtc->state);
687 "Changing CRC flip threshold for next capture on head-%d to %d\n",
688 head->base.index, value);
689 crc->flip_threshold = value;
693 drm_modeset_unlock(&crtc->mutex);
697 static const struct file_operations nv50_crc_flip_threshold_fops = {
698 .owner = THIS_MODULE,
699 .open = nv50_crc_debugfs_flip_threshold_open,
701 .write = nv50_crc_debugfs_flip_threshold_set,
702 .release = single_release,
705 int nv50_head_crc_late_register(struct nv50_head *head)
707 struct drm_crtc *crtc = &head->base.base;
708 const struct nv50_crc_func *func =
709 nv50_disp(crtc->dev)->core->func->crc;
712 if (!func || !crtc->debugfs_entry)
715 root = debugfs_create_dir("nv_crc", crtc->debugfs_entry);
716 debugfs_create_file("flip_threshold", 0644, root, head,
717 &nv50_crc_flip_threshold_fops);
723 nv50_crc_init_head(struct nv50_disp *disp, const struct nv50_crc_func *func,
724 struct nv50_head *head)
726 struct nv50_crc *crc = &head->crc;
728 crc->flip_threshold = func->flip_threshold;
729 spin_lock_init(&crc->lock);
730 drm_vblank_work_init(&crc->flip_work, &head->base.base,
731 nv50_crc_ctx_flip_work);
734 void nv50_crc_init(struct drm_device *dev)
736 struct nv50_disp *disp = nv50_disp(dev);
737 struct drm_crtc *crtc;
738 const struct nv50_crc_func *func = disp->core->func->crc;
743 drm_for_each_crtc(crtc, dev)
744 nv50_crc_init_head(disp, func, nv50_head(crtc));