sde_fence.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
  6. #include <linux/sync_file.h>
  7. #include <linux/dma-fence.h>
  8. #include "msm_drv.h"
  9. #include "sde_kms.h"
  10. #include "sde_fence.h"
  11. #define TIMELINE_VAL_LENGTH 128
  12. void *sde_sync_get(uint64_t fd)
  13. {
  14. /* force signed compare, fdget accepts an int argument */
  15. return (signed int)fd >= 0 ? sync_file_get_fence(fd) : NULL;
  16. }
  17. void sde_sync_put(void *fence)
  18. {
  19. if (fence)
  20. dma_fence_put(fence);
  21. }
  22. signed long sde_sync_wait(void *fnc, long timeout_ms)
  23. {
  24. struct dma_fence *fence = fnc;
  25. int rc;
  26. char timeline_str[TIMELINE_VAL_LENGTH];
  27. if (!fence)
  28. return -EINVAL;
  29. else if (dma_fence_is_signaled(fence))
  30. return timeout_ms ? msecs_to_jiffies(timeout_ms) : 1;
  31. rc = dma_fence_wait_timeout(fence, true,
  32. msecs_to_jiffies(timeout_ms));
  33. if (!rc || (rc == -EINVAL)) {
  34. if (fence->ops->timeline_value_str)
  35. fence->ops->timeline_value_str(fence,
  36. timeline_str, TIMELINE_VAL_LENGTH);
  37. SDE_ERROR(
  38. "fence driver name:%s timeline name:%s seqno:0x%x timeline:%s signaled:0x%x\n",
  39. fence->ops->get_driver_name(fence),
  40. fence->ops->get_timeline_name(fence),
  41. fence->seqno, timeline_str,
  42. fence->ops->signaled ?
  43. fence->ops->signaled(fence) : 0xffffffff);
  44. }
  45. return rc;
  46. }
  47. uint32_t sde_sync_get_name_prefix(void *fence)
  48. {
  49. const char *name;
  50. uint32_t i, prefix;
  51. struct dma_fence *f = fence;
  52. if (!fence)
  53. return 0;
  54. name = f->ops->get_driver_name(f);
  55. if (!name)
  56. return 0;
  57. prefix = 0x0;
  58. for (i = 0; i < sizeof(uint32_t) && name[i]; ++i)
  59. prefix = (prefix << CHAR_BIT) | name[i];
  60. return prefix;
  61. }
  62. /**
  63. * struct sde_fence - release/retire fence structure
  64. * @fence: base fence structure
  65. * @name: name of each fence- it is fence timeline + commit_count
  66. * @fence_list: list to associated this fence on timeline/context
  67. * @fd: fd attached to this fence - debugging purpose.
  68. */
  69. struct sde_fence {
  70. struct dma_fence base;
  71. struct sde_fence_context *ctx;
  72. char name[SDE_FENCE_NAME_SIZE];
  73. struct list_head fence_list;
  74. int fd;
  75. };
  76. static void sde_fence_destroy(struct kref *kref)
  77. {
  78. struct sde_fence_context *ctx;
  79. if (!kref) {
  80. SDE_ERROR("received invalid kref\n");
  81. return;
  82. }
  83. ctx = container_of(kref, struct sde_fence_context, kref);
  84. kfree(ctx);
  85. }
  86. static inline struct sde_fence *to_sde_fence(struct dma_fence *fence)
  87. {
  88. return container_of(fence, struct sde_fence, base);
  89. }
  90. static const char *sde_fence_get_driver_name(struct dma_fence *fence)
  91. {
  92. struct sde_fence *f = to_sde_fence(fence);
  93. return f->name;
  94. }
  95. static const char *sde_fence_get_timeline_name(struct dma_fence *fence)
  96. {
  97. struct sde_fence *f = to_sde_fence(fence);
  98. return f->ctx->name;
  99. }
  100. static bool sde_fence_enable_signaling(struct dma_fence *fence)
  101. {
  102. return true;
  103. }
  104. static bool sde_fence_signaled(struct dma_fence *fence)
  105. {
  106. struct sde_fence *f = to_sde_fence(fence);
  107. bool status;
  108. status = (int)((fence->seqno - f->ctx->done_count) <= 0);
  109. SDE_DEBUG("status:%d fence seq:%d and timeline:%d\n",
  110. status, fence->seqno, f->ctx->done_count);
  111. return status;
  112. }
  113. static void sde_fence_release(struct dma_fence *fence)
  114. {
  115. struct sde_fence *f;
  116. if (fence) {
  117. f = to_sde_fence(fence);
  118. kref_put(&f->ctx->kref, sde_fence_destroy);
  119. kfree(f);
  120. }
  121. }
  122. static void sde_fence_value_str(struct dma_fence *fence, char *str, int size)
  123. {
  124. if (!fence || !str)
  125. return;
  126. snprintf(str, size, "%d", fence->seqno);
  127. }
  128. static void sde_fence_timeline_value_str(struct dma_fence *fence, char *str,
  129. int size)
  130. {
  131. struct sde_fence *f = to_sde_fence(fence);
  132. if (!fence || !f->ctx || !str)
  133. return;
  134. snprintf(str, size, "%d", f->ctx->done_count);
  135. }
  136. static struct dma_fence_ops sde_fence_ops = {
  137. .get_driver_name = sde_fence_get_driver_name,
  138. .get_timeline_name = sde_fence_get_timeline_name,
  139. .enable_signaling = sde_fence_enable_signaling,
  140. .signaled = sde_fence_signaled,
  141. .wait = dma_fence_default_wait,
  142. .release = sde_fence_release,
  143. .fence_value_str = sde_fence_value_str,
  144. .timeline_value_str = sde_fence_timeline_value_str,
  145. };
  146. /**
  147. * _sde_fence_create_fd - create fence object and return an fd for it
  148. * This function is NOT thread-safe.
  149. * @timeline: Timeline to associate with fence
  150. * @val: Timeline value at which to signal the fence
  151. * Return: File descriptor on success, or error code on error
  152. */
  153. static int _sde_fence_create_fd(void *fence_ctx, uint32_t val)
  154. {
  155. struct sde_fence *sde_fence;
  156. struct sync_file *sync_file;
  157. signed int fd = -EINVAL;
  158. struct sde_fence_context *ctx = fence_ctx;
  159. if (!ctx) {
  160. SDE_ERROR("invalid context\n");
  161. goto exit;
  162. }
  163. sde_fence = kzalloc(sizeof(*sde_fence), GFP_KERNEL);
  164. if (!sde_fence)
  165. return -ENOMEM;
  166. sde_fence->ctx = fence_ctx;
  167. snprintf(sde_fence->name, SDE_FENCE_NAME_SIZE, "sde_fence:%s:%u",
  168. sde_fence->ctx->name, val);
  169. dma_fence_init(&sde_fence->base, &sde_fence_ops, &ctx->lock,
  170. ctx->context, val);
  171. kref_get(&ctx->kref);
  172. /* create fd */
  173. fd = get_unused_fd_flags(0);
  174. if (fd < 0) {
  175. SDE_ERROR("failed to get_unused_fd_flags(), %s\n",
  176. sde_fence->name);
  177. dma_fence_put(&sde_fence->base);
  178. goto exit;
  179. }
  180. /* create fence */
  181. sync_file = sync_file_create(&sde_fence->base);
  182. if (sync_file == NULL) {
  183. put_unused_fd(fd);
  184. fd = -EINVAL;
  185. SDE_ERROR("couldn't create fence, %s\n", sde_fence->name);
  186. dma_fence_put(&sde_fence->base);
  187. goto exit;
  188. }
  189. fd_install(fd, sync_file->file);
  190. sde_fence->fd = fd;
  191. spin_lock(&ctx->list_lock);
  192. list_add_tail(&sde_fence->fence_list, &ctx->fence_list_head);
  193. spin_unlock(&ctx->list_lock);
  194. exit:
  195. return fd;
  196. }
  197. struct sde_fence_context *sde_fence_init(const char *name, uint32_t drm_id)
  198. {
  199. struct sde_fence_context *ctx;
  200. if (!name) {
  201. SDE_ERROR("invalid argument(s)\n");
  202. return ERR_PTR(-EINVAL);
  203. }
  204. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  205. if (!ctx) {
  206. SDE_ERROR("failed to alloc fence ctx\n");
  207. return ERR_PTR(-ENOMEM);
  208. }
  209. strlcpy(ctx->name, name, ARRAY_SIZE(ctx->name));
  210. ctx->drm_id = drm_id;
  211. kref_init(&ctx->kref);
  212. ctx->context = dma_fence_context_alloc(1);
  213. spin_lock_init(&ctx->lock);
  214. spin_lock_init(&ctx->list_lock);
  215. INIT_LIST_HEAD(&ctx->fence_list_head);
  216. return ctx;
  217. }
  218. void sde_fence_deinit(struct sde_fence_context *ctx)
  219. {
  220. if (!ctx) {
  221. SDE_ERROR("invalid fence\n");
  222. return;
  223. }
  224. kref_put(&ctx->kref, sde_fence_destroy);
  225. }
  226. void sde_fence_prepare(struct sde_fence_context *ctx)
  227. {
  228. unsigned long flags;
  229. if (!ctx) {
  230. SDE_ERROR("invalid argument(s), fence %pK\n", ctx);
  231. } else {
  232. spin_lock_irqsave(&ctx->lock, flags);
  233. ++ctx->commit_count;
  234. spin_unlock_irqrestore(&ctx->lock, flags);
  235. }
  236. }
  237. static void _sde_fence_trigger(struct sde_fence_context *ctx,
  238. ktime_t ts, bool error)
  239. {
  240. unsigned long flags;
  241. struct sde_fence *fc, *next;
  242. bool is_signaled = false;
  243. kref_get(&ctx->kref);
  244. spin_lock(&ctx->list_lock);
  245. if (list_empty(&ctx->fence_list_head)) {
  246. SDE_DEBUG("nothing to trigger!\n");
  247. goto end;
  248. }
  249. list_for_each_entry_safe(fc, next, &ctx->fence_list_head, fence_list) {
  250. spin_lock_irqsave(&ctx->lock, flags);
  251. fc->base.error = error ? -EBUSY : 0;
  252. fc->base.timestamp = ts;
  253. is_signaled = dma_fence_is_signaled_locked(&fc->base);
  254. spin_unlock_irqrestore(&ctx->lock, flags);
  255. if (is_signaled) {
  256. list_del_init(&fc->fence_list);
  257. dma_fence_put(&fc->base);
  258. }
  259. }
  260. end:
  261. spin_unlock(&ctx->list_lock);
  262. kref_put(&ctx->kref, sde_fence_destroy);
  263. }
  264. int sde_fence_create(struct sde_fence_context *ctx, uint64_t *val,
  265. uint32_t offset)
  266. {
  267. uint32_t trigger_value;
  268. int fd = -1, rc = -EINVAL;
  269. unsigned long flags;
  270. struct sde_fence *fc;
  271. if (!ctx || !val) {
  272. SDE_ERROR("invalid argument(s), fence %d, pval %d\n",
  273. ctx != NULL, val != NULL);
  274. return rc;
  275. }
  276. /*
  277. * Allow created fences to have a constant offset with respect
  278. * to the timeline. This allows us to delay the fence signalling
  279. * w.r.t. the commit completion (e.g., an offset of +1 would
  280. * cause fences returned during a particular commit to signal
  281. * after an additional delay of one commit, rather than at the
  282. * end of the current one.
  283. */
  284. spin_lock_irqsave(&ctx->lock, flags);
  285. trigger_value = ctx->commit_count + offset;
  286. spin_unlock_irqrestore(&ctx->lock, flags);
  287. spin_lock(&ctx->list_lock);
  288. list_for_each_entry(fc, &ctx->fence_list_head, fence_list) {
  289. if (trigger_value == fc->base.seqno) {
  290. fd = fc->fd;
  291. *val = fd;
  292. break;
  293. }
  294. }
  295. spin_unlock(&ctx->list_lock);
  296. if (fd < 0) {
  297. fd = _sde_fence_create_fd(ctx, trigger_value);
  298. *val = fd;
  299. SDE_DEBUG("fd:%d trigger:%d commit:%d offset:%d\n",
  300. fd, trigger_value, ctx->commit_count, offset);
  301. }
  302. SDE_EVT32(ctx->drm_id, trigger_value, fd);
  303. rc = (fd >= 0) ? 0 : fd;
  304. return rc;
  305. }
  306. void sde_fence_signal(struct sde_fence_context *ctx, ktime_t ts,
  307. enum sde_fence_event fence_event)
  308. {
  309. unsigned long flags;
  310. if (!ctx) {
  311. SDE_ERROR("invalid ctx, %pK\n", ctx);
  312. return;
  313. }
  314. spin_lock_irqsave(&ctx->lock, flags);
  315. if (fence_event == SDE_FENCE_RESET_TIMELINE) {
  316. if ((int)(ctx->done_count - ctx->commit_count) < 0) {
  317. SDE_ERROR(
  318. "timeline reset attempt! done count:%d commit:%d\n",
  319. ctx->done_count, ctx->commit_count);
  320. ctx->done_count = ctx->commit_count;
  321. SDE_EVT32(ctx->drm_id, ctx->done_count,
  322. ctx->commit_count, ktime_to_us(ts),
  323. fence_event, SDE_EVTLOG_FATAL);
  324. } else {
  325. spin_unlock_irqrestore(&ctx->lock, flags);
  326. return;
  327. }
  328. } else if ((int)(ctx->done_count - ctx->commit_count) < 0) {
  329. ++ctx->done_count;
  330. SDE_DEBUG("fence_signal:done count:%d commit count:%d\n",
  331. ctx->done_count, ctx->commit_count);
  332. } else {
  333. SDE_ERROR("extra signal attempt! done count:%d commit:%d\n",
  334. ctx->done_count, ctx->commit_count);
  335. SDE_EVT32(ctx->drm_id, ctx->done_count, ctx->commit_count,
  336. ktime_to_us(ts), fence_event, SDE_EVTLOG_FATAL);
  337. spin_unlock_irqrestore(&ctx->lock, flags);
  338. return;
  339. }
  340. spin_unlock_irqrestore(&ctx->lock, flags);
  341. SDE_EVT32(ctx->drm_id, ctx->done_count, ctx->commit_count,
  342. ktime_to_us(ts));
  343. _sde_fence_trigger(ctx, ts, (fence_event == SDE_FENCE_SIGNAL_ERROR));
  344. }
  345. void sde_fence_timeline_status(struct sde_fence_context *ctx,
  346. struct drm_mode_object *drm_obj)
  347. {
  348. char *obj_name;
  349. if (!ctx || !drm_obj) {
  350. SDE_ERROR("invalid input params\n");
  351. return;
  352. }
  353. switch (drm_obj->type) {
  354. case DRM_MODE_OBJECT_CRTC:
  355. obj_name = "crtc";
  356. break;
  357. case DRM_MODE_OBJECT_CONNECTOR:
  358. obj_name = "connector";
  359. break;
  360. default:
  361. obj_name = "unknown";
  362. break;
  363. }
  364. SDE_ERROR("drm obj:%s id:%d type:0x%x done_count:%d commit_count:%d\n",
  365. obj_name, drm_obj->id, drm_obj->type, ctx->done_count,
  366. ctx->commit_count);
  367. }
  368. void sde_fence_list_dump(struct dma_fence *fence, struct seq_file **s)
  369. {
  370. char timeline_str[TIMELINE_VAL_LENGTH];
  371. if (fence->ops->timeline_value_str)
  372. fence->ops->timeline_value_str(fence,
  373. timeline_str, TIMELINE_VAL_LENGTH);
  374. seq_printf(*s, "fence name:%s timeline name:%s seqno:0x%x timeline:%s signaled:0x%x\n",
  375. fence->ops->get_driver_name(fence),
  376. fence->ops->get_timeline_name(fence),
  377. fence->seqno, timeline_str,
  378. fence->ops->signaled ?
  379. fence->ops->signaled(fence) : 0xffffffff);
  380. }
  381. void sde_debugfs_timeline_dump(struct sde_fence_context *ctx,
  382. struct drm_mode_object *drm_obj, struct seq_file **s)
  383. {
  384. char *obj_name;
  385. struct sde_fence *fc, *next;
  386. struct dma_fence *fence;
  387. if (!ctx || !drm_obj) {
  388. SDE_ERROR("invalid input params\n");
  389. return;
  390. }
  391. switch (drm_obj->type) {
  392. case DRM_MODE_OBJECT_CRTC:
  393. obj_name = "crtc";
  394. break;
  395. case DRM_MODE_OBJECT_CONNECTOR:
  396. obj_name = "connector";
  397. break;
  398. default:
  399. obj_name = "unknown";
  400. break;
  401. }
  402. seq_printf(*s, "drm obj:%s id:%d type:0x%x done_count:%d commit_count:%d\n",
  403. obj_name, drm_obj->id, drm_obj->type, ctx->done_count,
  404. ctx->commit_count);
  405. spin_lock(&ctx->list_lock);
  406. list_for_each_entry_safe(fc, next, &ctx->fence_list_head, fence_list) {
  407. fence = &fc->base;
  408. sde_fence_list_dump(fence, s);
  409. }
  410. spin_unlock(&ctx->list_lock);
  411. }