sde_fence.c 12 KB

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