crc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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>
  7. #include <nvif/class.h>
  8. #include <nvif/cl0002.h>
  9. #include <nvif/timer.h>
  10. #include <nvhw/class/cl907d.h>
  11. #include "nouveau_drv.h"
  12. #include "core.h"
  13. #include "head.h"
  14. #include "wndw.h"
  15. #include "handles.h"
  16. #include "crc.h"
  17. static const char * const nv50_crc_sources[] = {
  18. [NV50_CRC_SOURCE_NONE] = "none",
  19. [NV50_CRC_SOURCE_AUTO] = "auto",
  20. [NV50_CRC_SOURCE_RG] = "rg",
  21. [NV50_CRC_SOURCE_OUTP_ACTIVE] = "outp-active",
  22. [NV50_CRC_SOURCE_OUTP_COMPLETE] = "outp-complete",
  23. [NV50_CRC_SOURCE_OUTP_INACTIVE] = "outp-inactive",
  24. };
  25. static int nv50_crc_parse_source(const char *buf, enum nv50_crc_source *s)
  26. {
  27. int i;
  28. if (!buf) {
  29. *s = NV50_CRC_SOURCE_NONE;
  30. return 0;
  31. }
  32. i = match_string(nv50_crc_sources, ARRAY_SIZE(nv50_crc_sources), buf);
  33. if (i < 0)
  34. return i;
  35. *s = i;
  36. return 0;
  37. }
  38. int
  39. nv50_crc_verify_source(struct drm_crtc *crtc, const char *source_name,
  40. size_t *values_cnt)
  41. {
  42. struct nouveau_drm *drm = nouveau_drm(crtc->dev);
  43. enum nv50_crc_source source;
  44. if (nv50_crc_parse_source(source_name, &source) < 0) {
  45. NV_DEBUG(drm, "unknown source %s\n", source_name);
  46. return -EINVAL;
  47. }
  48. *values_cnt = 1;
  49. return 0;
  50. }
  51. const char *const *nv50_crc_get_sources(struct drm_crtc *crtc, size_t *count)
  52. {
  53. *count = ARRAY_SIZE(nv50_crc_sources);
  54. return nv50_crc_sources;
  55. }
  56. static void
  57. nv50_crc_program_ctx(struct nv50_head *head,
  58. struct nv50_crc_notifier_ctx *ctx)
  59. {
  60. struct nv50_disp *disp = nv50_disp(head->base.base.dev);
  61. struct nv50_core *core = disp->core;
  62. u32 interlock[NV50_DISP_INTERLOCK__SIZE] = { 0 };
  63. core->func->crc->set_ctx(head, ctx);
  64. core->func->update(core, interlock, false);
  65. }
  66. static void nv50_crc_ctx_flip_work(struct kthread_work *base)
  67. {
  68. struct drm_vblank_work *work = to_drm_vblank_work(base);
  69. struct nv50_crc *crc = container_of(work, struct nv50_crc, flip_work);
  70. struct nv50_head *head = container_of(crc, struct nv50_head, crc);
  71. struct drm_crtc *crtc = &head->base.base;
  72. struct drm_device *dev = crtc->dev;
  73. struct nv50_disp *disp = nv50_disp(dev);
  74. const uint64_t start_vbl = drm_crtc_vblank_count(crtc);
  75. uint64_t end_vbl;
  76. u8 new_idx = crc->ctx_idx ^ 1;
  77. /*
  78. * We don't want to accidentally wait for longer then the vblank, so
  79. * try again for the next vblank if we don't grab the lock
  80. */
  81. if (!mutex_trylock(&disp->mutex)) {
  82. drm_dbg_kms(dev, "Lock contended, delaying CRC ctx flip for %s\n", crtc->name);
  83. drm_vblank_work_schedule(work, start_vbl + 1, true);
  84. return;
  85. }
  86. drm_dbg_kms(dev, "Flipping notifier ctx for %s (%d -> %d)\n",
  87. crtc->name, crc->ctx_idx, new_idx);
  88. nv50_crc_program_ctx(head, NULL);
  89. nv50_crc_program_ctx(head, &crc->ctx[new_idx]);
  90. mutex_unlock(&disp->mutex);
  91. end_vbl = drm_crtc_vblank_count(crtc);
  92. if (unlikely(end_vbl != start_vbl))
  93. NV_ERROR(nouveau_drm(dev),
  94. "Failed to flip CRC context on %s on time (%llu > %llu)\n",
  95. crtc->name, end_vbl, start_vbl);
  96. spin_lock_irq(&crc->lock);
  97. crc->ctx_changed = true;
  98. spin_unlock_irq(&crc->lock);
  99. }
  100. static inline void nv50_crc_reset_ctx(struct nv50_crc_notifier_ctx *ctx)
  101. {
  102. memset_io(ctx->mem.object.map.ptr, 0, ctx->mem.object.map.size);
  103. }
  104. static void
  105. nv50_crc_get_entries(struct nv50_head *head,
  106. const struct nv50_crc_func *func,
  107. enum nv50_crc_source source)
  108. {
  109. struct drm_crtc *crtc = &head->base.base;
  110. struct nv50_crc *crc = &head->crc;
  111. u32 output_crc;
  112. while (crc->entry_idx < func->num_entries) {
  113. /*
  114. * While Nvidia's documentation says CRCs are written on each
  115. * subsequent vblank after being enabled, in practice they
  116. * aren't written immediately.
  117. */
  118. output_crc = func->get_entry(head, &crc->ctx[crc->ctx_idx],
  119. source, crc->entry_idx);
  120. if (!output_crc)
  121. return;
  122. drm_crtc_add_crc_entry(crtc, true, crc->frame, &output_crc);
  123. crc->frame++;
  124. crc->entry_idx++;
  125. }
  126. }
  127. void nv50_crc_handle_vblank(struct nv50_head *head)
  128. {
  129. struct drm_crtc *crtc = &head->base.base;
  130. struct nv50_crc *crc = &head->crc;
  131. const struct nv50_crc_func *func =
  132. nv50_disp(head->base.base.dev)->core->func->crc;
  133. struct nv50_crc_notifier_ctx *ctx;
  134. bool need_reschedule = false;
  135. if (!func)
  136. return;
  137. /*
  138. * We don't lose events if we aren't able to report CRCs until the
  139. * next vblank, so only report CRCs if the locks we need aren't
  140. * contended to prevent missing an actual vblank event
  141. */
  142. if (!spin_trylock(&crc->lock))
  143. return;
  144. if (!crc->src)
  145. goto out;
  146. ctx = &crc->ctx[crc->ctx_idx];
  147. if (crc->ctx_changed && func->ctx_finished(head, ctx)) {
  148. nv50_crc_get_entries(head, func, crc->src);
  149. crc->ctx_idx ^= 1;
  150. crc->entry_idx = 0;
  151. crc->ctx_changed = false;
  152. /*
  153. * Unfortunately when notifier contexts are changed during CRC
  154. * capture, we will inevitably lose the CRC entry for the
  155. * frame where the hardware actually latched onto the first
  156. * UPDATE. According to Nvidia's hardware engineers, there's
  157. * no workaround for this.
  158. *
  159. * Now, we could try to be smart here and calculate the number
  160. * of missed CRCs based on audit timestamps, but those were
  161. * removed starting with volta. Since we always flush our
  162. * updates back-to-back without waiting, we'll just be
  163. * optimistic and assume we always miss exactly one frame.
  164. */
  165. drm_dbg_kms(head->base.base.dev,
  166. "Notifier ctx flip for head-%d finished, lost CRC for frame %llu\n",
  167. head->base.index, crc->frame);
  168. crc->frame++;
  169. nv50_crc_reset_ctx(ctx);
  170. need_reschedule = true;
  171. }
  172. nv50_crc_get_entries(head, func, crc->src);
  173. if (need_reschedule)
  174. drm_vblank_work_schedule(&crc->flip_work,
  175. drm_crtc_vblank_count(crtc)
  176. + crc->flip_threshold
  177. - crc->entry_idx,
  178. true);
  179. out:
  180. spin_unlock(&crc->lock);
  181. }
  182. static void nv50_crc_wait_ctx_finished(struct nv50_head *head,
  183. const struct nv50_crc_func *func,
  184. struct nv50_crc_notifier_ctx *ctx)
  185. {
  186. struct drm_device *dev = head->base.base.dev;
  187. struct nouveau_drm *drm = nouveau_drm(dev);
  188. s64 ret;
  189. ret = nvif_msec(&drm->client.device, 50,
  190. if (func->ctx_finished(head, ctx)) break;);
  191. if (ret == -ETIMEDOUT)
  192. NV_ERROR(drm,
  193. "CRC notifier ctx for head %d not finished after 50ms\n",
  194. head->base.index);
  195. else if (ret)
  196. NV_ATOMIC(drm,
  197. "CRC notifier ctx for head-%d finished after %lldns\n",
  198. head->base.index, ret);
  199. }
  200. void nv50_crc_atomic_stop_reporting(struct drm_atomic_state *state)
  201. {
  202. struct drm_crtc_state *crtc_state;
  203. struct drm_crtc *crtc;
  204. int i;
  205. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  206. struct nv50_head *head = nv50_head(crtc);
  207. struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
  208. struct nv50_crc *crc = &head->crc;
  209. if (!asyh->clr.crc)
  210. continue;
  211. spin_lock_irq(&crc->lock);
  212. crc->src = NV50_CRC_SOURCE_NONE;
  213. spin_unlock_irq(&crc->lock);
  214. drm_crtc_vblank_put(crtc);
  215. drm_vblank_work_cancel_sync(&crc->flip_work);
  216. NV_ATOMIC(nouveau_drm(crtc->dev),
  217. "CRC reporting on vblank for head-%d disabled\n",
  218. head->base.index);
  219. /* CRC generation is still enabled in hw, we'll just report
  220. * any remaining CRC entries ourselves after it gets disabled
  221. * in hardware
  222. */
  223. }
  224. }
  225. void nv50_crc_atomic_init_notifier_contexts(struct drm_atomic_state *state)
  226. {
  227. struct drm_crtc_state *new_crtc_state;
  228. struct drm_crtc *crtc;
  229. int i;
  230. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  231. struct nv50_head *head = nv50_head(crtc);
  232. struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
  233. struct nv50_crc *crc = &head->crc;
  234. int i;
  235. if (!asyh->set.crc)
  236. continue;
  237. crc->entry_idx = 0;
  238. crc->ctx_changed = false;
  239. for (i = 0; i < ARRAY_SIZE(crc->ctx); i++)
  240. nv50_crc_reset_ctx(&crc->ctx[i]);
  241. }
  242. }
  243. void nv50_crc_atomic_release_notifier_contexts(struct drm_atomic_state *state)
  244. {
  245. const struct nv50_crc_func *func =
  246. nv50_disp(state->dev)->core->func->crc;
  247. struct drm_crtc_state *new_crtc_state;
  248. struct drm_crtc *crtc;
  249. int i;
  250. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  251. struct nv50_head *head = nv50_head(crtc);
  252. struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
  253. struct nv50_crc *crc = &head->crc;
  254. struct nv50_crc_notifier_ctx *ctx = &crc->ctx[crc->ctx_idx];
  255. if (!asyh->clr.crc)
  256. continue;
  257. if (crc->ctx_changed) {
  258. nv50_crc_wait_ctx_finished(head, func, ctx);
  259. ctx = &crc->ctx[crc->ctx_idx ^ 1];
  260. }
  261. nv50_crc_wait_ctx_finished(head, func, ctx);
  262. }
  263. }
  264. void nv50_crc_atomic_start_reporting(struct drm_atomic_state *state)
  265. {
  266. struct drm_crtc_state *crtc_state;
  267. struct drm_crtc *crtc;
  268. int i;
  269. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  270. struct nv50_head *head = nv50_head(crtc);
  271. struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
  272. struct nv50_crc *crc = &head->crc;
  273. u64 vbl_count;
  274. if (!asyh->set.crc)
  275. continue;
  276. drm_crtc_vblank_get(crtc);
  277. spin_lock_irq(&crc->lock);
  278. vbl_count = drm_crtc_vblank_count(crtc);
  279. crc->frame = vbl_count;
  280. crc->src = asyh->crc.src;
  281. drm_vblank_work_schedule(&crc->flip_work,
  282. vbl_count + crc->flip_threshold,
  283. true);
  284. spin_unlock_irq(&crc->lock);
  285. NV_ATOMIC(nouveau_drm(crtc->dev),
  286. "CRC reporting on vblank for head-%d enabled\n",
  287. head->base.index);
  288. }
  289. }
  290. int nv50_crc_atomic_check_head(struct nv50_head *head,
  291. struct nv50_head_atom *asyh,
  292. struct nv50_head_atom *armh)
  293. {
  294. struct nv50_atom *atom = nv50_atom(asyh->state.state);
  295. bool changed = armh->crc.src != asyh->crc.src;
  296. if (!armh->crc.src && !asyh->crc.src) {
  297. asyh->set.crc = false;
  298. asyh->clr.crc = false;
  299. return 0;
  300. }
  301. if (drm_atomic_crtc_needs_modeset(&asyh->state) || changed) {
  302. asyh->clr.crc = armh->crc.src && armh->state.active;
  303. asyh->set.crc = asyh->crc.src && asyh->state.active;
  304. if (changed)
  305. asyh->set.or |= armh->or.crc_raster !=
  306. asyh->or.crc_raster;
  307. if (asyh->clr.crc && asyh->set.crc)
  308. atom->flush_disable = true;
  309. } else {
  310. asyh->set.crc = false;
  311. asyh->clr.crc = false;
  312. }
  313. return 0;
  314. }
  315. void nv50_crc_atomic_check_outp(struct nv50_atom *atom)
  316. {
  317. struct drm_crtc *crtc;
  318. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  319. int i;
  320. if (atom->flush_disable)
  321. return;
  322. for_each_oldnew_crtc_in_state(&atom->state, crtc, old_crtc_state,
  323. new_crtc_state, i) {
  324. struct nv50_head_atom *armh = nv50_head_atom(old_crtc_state);
  325. struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
  326. struct nv50_outp_atom *outp_atom;
  327. struct nouveau_encoder *outp;
  328. struct drm_encoder *encoder, *enc;
  329. enc = nv50_head_atom_get_encoder(armh);
  330. if (!enc)
  331. continue;
  332. outp = nv50_real_outp(enc);
  333. if (!outp)
  334. continue;
  335. encoder = &outp->base.base;
  336. if (!asyh->clr.crc)
  337. continue;
  338. /*
  339. * Re-programming ORs can't be done in the same flush as
  340. * disabling CRCs
  341. */
  342. list_for_each_entry(outp_atom, &atom->outp, head) {
  343. if (outp_atom->encoder == encoder) {
  344. if (outp_atom->set.mask) {
  345. atom->flush_disable = true;
  346. return;
  347. } else {
  348. break;
  349. }
  350. }
  351. }
  352. }
  353. }
  354. static enum nv50_crc_source_type
  355. nv50_crc_source_type(struct nouveau_encoder *outp,
  356. enum nv50_crc_source source)
  357. {
  358. struct dcb_output *dcbe = outp->dcb;
  359. switch (source) {
  360. case NV50_CRC_SOURCE_NONE: return NV50_CRC_SOURCE_TYPE_NONE;
  361. case NV50_CRC_SOURCE_RG: return NV50_CRC_SOURCE_TYPE_RG;
  362. default: break;
  363. }
  364. if (dcbe->location != DCB_LOC_ON_CHIP)
  365. return NV50_CRC_SOURCE_TYPE_PIOR;
  366. switch (dcbe->type) {
  367. case DCB_OUTPUT_DP: return NV50_CRC_SOURCE_TYPE_SF;
  368. case DCB_OUTPUT_ANALOG: return NV50_CRC_SOURCE_TYPE_DAC;
  369. default: return NV50_CRC_SOURCE_TYPE_SOR;
  370. }
  371. }
  372. void nv50_crc_atomic_set(struct nv50_head *head,
  373. struct nv50_head_atom *asyh)
  374. {
  375. struct drm_crtc *crtc = &head->base.base;
  376. struct drm_device *dev = crtc->dev;
  377. struct nv50_crc *crc = &head->crc;
  378. const struct nv50_crc_func *func = nv50_disp(dev)->core->func->crc;
  379. struct nouveau_encoder *outp;
  380. struct drm_encoder *encoder;
  381. encoder = nv50_head_atom_get_encoder(asyh);
  382. if (!encoder)
  383. return;
  384. outp = nv50_real_outp(encoder);
  385. if (!outp)
  386. return;
  387. func->set_src(head, outp->or, nv50_crc_source_type(outp, asyh->crc.src),
  388. &crc->ctx[crc->ctx_idx]);
  389. }
  390. void nv50_crc_atomic_clr(struct nv50_head *head)
  391. {
  392. const struct nv50_crc_func *func =
  393. nv50_disp(head->base.base.dev)->core->func->crc;
  394. func->set_src(head, 0, NV50_CRC_SOURCE_TYPE_NONE, NULL);
  395. }
  396. static inline int
  397. nv50_crc_raster_type(enum nv50_crc_source source)
  398. {
  399. switch (source) {
  400. case NV50_CRC_SOURCE_NONE:
  401. case NV50_CRC_SOURCE_AUTO:
  402. case NV50_CRC_SOURCE_RG:
  403. case NV50_CRC_SOURCE_OUTP_ACTIVE:
  404. return NV907D_HEAD_SET_CONTROL_OUTPUT_RESOURCE_CRC_MODE_ACTIVE_RASTER;
  405. case NV50_CRC_SOURCE_OUTP_COMPLETE:
  406. return NV907D_HEAD_SET_CONTROL_OUTPUT_RESOURCE_CRC_MODE_COMPLETE_RASTER;
  407. case NV50_CRC_SOURCE_OUTP_INACTIVE:
  408. return NV907D_HEAD_SET_CONTROL_OUTPUT_RESOURCE_CRC_MODE_NON_ACTIVE_RASTER;
  409. }
  410. return 0;
  411. }
  412. /* We handle mapping the memory for CRC notifiers ourselves, since each
  413. * notifier needs it's own handle
  414. */
  415. static inline int
  416. nv50_crc_ctx_init(struct nv50_head *head, struct nvif_mmu *mmu,
  417. struct nv50_crc_notifier_ctx *ctx, size_t len, int idx)
  418. {
  419. struct nv50_core *core = nv50_disp(head->base.base.dev)->core;
  420. int ret;
  421. ret = nvif_mem_ctor_map(mmu, "kmsCrcNtfy", NVIF_MEM_VRAM, len, &ctx->mem);
  422. if (ret)
  423. return ret;
  424. ret = nvif_object_ctor(&core->chan.base.user, "kmsCrcNtfyCtxDma",
  425. NV50_DISP_HANDLE_CRC_CTX(head, idx),
  426. NV_DMA_IN_MEMORY,
  427. &(struct nv_dma_v0) {
  428. .target = NV_DMA_V0_TARGET_VRAM,
  429. .access = NV_DMA_V0_ACCESS_RDWR,
  430. .start = ctx->mem.addr,
  431. .limit = ctx->mem.addr
  432. + ctx->mem.size - 1,
  433. }, sizeof(struct nv_dma_v0),
  434. &ctx->ntfy);
  435. if (ret)
  436. goto fail_fini;
  437. return 0;
  438. fail_fini:
  439. nvif_mem_dtor(&ctx->mem);
  440. return ret;
  441. }
  442. static inline void
  443. nv50_crc_ctx_fini(struct nv50_crc_notifier_ctx *ctx)
  444. {
  445. nvif_object_dtor(&ctx->ntfy);
  446. nvif_mem_dtor(&ctx->mem);
  447. }
  448. int nv50_crc_set_source(struct drm_crtc *crtc, const char *source_str)
  449. {
  450. struct drm_device *dev = crtc->dev;
  451. struct drm_atomic_state *state;
  452. struct drm_modeset_acquire_ctx ctx;
  453. struct nv50_head *head = nv50_head(crtc);
  454. struct nv50_crc *crc = &head->crc;
  455. const struct nv50_crc_func *func = nv50_disp(dev)->core->func->crc;
  456. struct nvif_mmu *mmu = &nouveau_drm(dev)->client.mmu;
  457. struct nv50_head_atom *asyh;
  458. struct drm_crtc_state *crtc_state;
  459. enum nv50_crc_source source;
  460. int ret = 0, ctx_flags = 0, i;
  461. ret = nv50_crc_parse_source(source_str, &source);
  462. if (ret)
  463. return ret;
  464. /*
  465. * Since we don't want the user to accidentally interrupt us as we're
  466. * disabling CRCs
  467. */
  468. if (source)
  469. ctx_flags |= DRM_MODESET_ACQUIRE_INTERRUPTIBLE;
  470. drm_modeset_acquire_init(&ctx, ctx_flags);
  471. state = drm_atomic_state_alloc(dev);
  472. if (!state) {
  473. ret = -ENOMEM;
  474. goto out_acquire_fini;
  475. }
  476. state->acquire_ctx = &ctx;
  477. if (source) {
  478. for (i = 0; i < ARRAY_SIZE(head->crc.ctx); i++) {
  479. ret = nv50_crc_ctx_init(head, mmu, &crc->ctx[i],
  480. func->notifier_len, i);
  481. if (ret)
  482. goto out_ctx_fini;
  483. }
  484. }
  485. retry:
  486. crtc_state = drm_atomic_get_crtc_state(state, &head->base.base);
  487. if (IS_ERR(crtc_state)) {
  488. ret = PTR_ERR(crtc_state);
  489. if (ret == -EDEADLK)
  490. goto deadlock;
  491. else if (ret)
  492. goto out_drop_locks;
  493. }
  494. asyh = nv50_head_atom(crtc_state);
  495. asyh->crc.src = source;
  496. asyh->or.crc_raster = nv50_crc_raster_type(source);
  497. ret = drm_atomic_commit(state);
  498. if (ret == -EDEADLK)
  499. goto deadlock;
  500. else if (ret)
  501. goto out_drop_locks;
  502. if (!source) {
  503. /*
  504. * If the user specified a custom flip threshold through
  505. * debugfs, reset it
  506. */
  507. crc->flip_threshold = func->flip_threshold;
  508. }
  509. out_drop_locks:
  510. drm_modeset_drop_locks(&ctx);
  511. out_ctx_fini:
  512. if (!source || ret) {
  513. for (i = 0; i < ARRAY_SIZE(crc->ctx); i++)
  514. nv50_crc_ctx_fini(&crc->ctx[i]);
  515. }
  516. drm_atomic_state_put(state);
  517. out_acquire_fini:
  518. drm_modeset_acquire_fini(&ctx);
  519. return ret;
  520. deadlock:
  521. drm_atomic_state_clear(state);
  522. drm_modeset_backoff(&ctx);
  523. goto retry;
  524. }
  525. static int
  526. nv50_crc_debugfs_flip_threshold_get(struct seq_file *m, void *data)
  527. {
  528. struct nv50_head *head = m->private;
  529. struct drm_crtc *crtc = &head->base.base;
  530. struct nv50_crc *crc = &head->crc;
  531. int ret;
  532. ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
  533. if (ret)
  534. return ret;
  535. seq_printf(m, "%d\n", crc->flip_threshold);
  536. drm_modeset_unlock(&crtc->mutex);
  537. return ret;
  538. }
  539. static int
  540. nv50_crc_debugfs_flip_threshold_open(struct inode *inode, struct file *file)
  541. {
  542. return single_open(file, nv50_crc_debugfs_flip_threshold_get,
  543. inode->i_private);
  544. }
  545. static ssize_t
  546. nv50_crc_debugfs_flip_threshold_set(struct file *file,
  547. const char __user *ubuf, size_t len,
  548. loff_t *offp)
  549. {
  550. struct seq_file *m = file->private_data;
  551. struct nv50_head *head = m->private;
  552. struct nv50_head_atom *armh;
  553. struct drm_crtc *crtc = &head->base.base;
  554. struct nouveau_drm *drm = nouveau_drm(crtc->dev);
  555. struct nv50_crc *crc = &head->crc;
  556. const struct nv50_crc_func *func =
  557. nv50_disp(crtc->dev)->core->func->crc;
  558. int value, ret;
  559. ret = kstrtoint_from_user(ubuf, len, 10, &value);
  560. if (ret)
  561. return ret;
  562. if (value > func->flip_threshold)
  563. return -EINVAL;
  564. else if (value == -1)
  565. value = func->flip_threshold;
  566. else if (value < -1)
  567. return -EINVAL;
  568. ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
  569. if (ret)
  570. return ret;
  571. armh = nv50_head_atom(crtc->state);
  572. if (armh->crc.src) {
  573. ret = -EBUSY;
  574. goto out;
  575. }
  576. NV_DEBUG(drm,
  577. "Changing CRC flip threshold for next capture on head-%d to %d\n",
  578. head->base.index, value);
  579. crc->flip_threshold = value;
  580. ret = len;
  581. out:
  582. drm_modeset_unlock(&crtc->mutex);
  583. return ret;
  584. }
  585. static const struct file_operations nv50_crc_flip_threshold_fops = {
  586. .owner = THIS_MODULE,
  587. .open = nv50_crc_debugfs_flip_threshold_open,
  588. .read = seq_read,
  589. .write = nv50_crc_debugfs_flip_threshold_set,
  590. .release = single_release,
  591. };
  592. int nv50_head_crc_late_register(struct nv50_head *head)
  593. {
  594. struct drm_crtc *crtc = &head->base.base;
  595. const struct nv50_crc_func *func =
  596. nv50_disp(crtc->dev)->core->func->crc;
  597. struct dentry *root;
  598. if (!func || !crtc->debugfs_entry)
  599. return 0;
  600. root = debugfs_create_dir("nv_crc", crtc->debugfs_entry);
  601. debugfs_create_file("flip_threshold", 0644, root, head,
  602. &nv50_crc_flip_threshold_fops);
  603. return 0;
  604. }
  605. static inline void
  606. nv50_crc_init_head(struct nv50_disp *disp, const struct nv50_crc_func *func,
  607. struct nv50_head *head)
  608. {
  609. struct nv50_crc *crc = &head->crc;
  610. crc->flip_threshold = func->flip_threshold;
  611. spin_lock_init(&crc->lock);
  612. drm_vblank_work_init(&crc->flip_work, &head->base.base,
  613. nv50_crc_ctx_flip_work);
  614. }
  615. void nv50_crc_init(struct drm_device *dev)
  616. {
  617. struct nv50_disp *disp = nv50_disp(dev);
  618. struct drm_crtc *crtc;
  619. const struct nv50_crc_func *func = disp->core->func->crc;
  620. if (!func)
  621. return;
  622. drm_for_each_crtc(crtc, dev)
  623. nv50_crc_init_head(disp, func, nv50_head(crtc));
  624. }