kgsl_sync.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2019, 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/file.h>
  7. #include <linux/slab.h>
  8. #include <linux/soc/qcom/msm_hw_fence.h>
  9. #include <linux/sync_file.h>
  10. #include "kgsl_device.h"
  11. #include "kgsl_sync.h"
  12. static const struct dma_fence_ops kgsl_sync_fence_ops;
  13. static struct kgsl_sync_fence *kgsl_sync_fence_create(
  14. struct kgsl_context *context,
  15. unsigned int timestamp)
  16. {
  17. struct kgsl_sync_fence *kfence;
  18. struct kgsl_sync_timeline *ktimeline = context->ktimeline;
  19. unsigned long flags;
  20. /* Get a refcount to the timeline. Put when released */
  21. if (!kref_get_unless_zero(&ktimeline->kref))
  22. return NULL;
  23. kfence = kzalloc(sizeof(*kfence), GFP_KERNEL);
  24. if (kfence == NULL) {
  25. kgsl_sync_timeline_put(ktimeline);
  26. return NULL;
  27. }
  28. kfence->parent = ktimeline;
  29. kfence->context_id = context->id;
  30. kfence->timestamp = timestamp;
  31. dma_fence_init(&kfence->fence, &kgsl_sync_fence_ops, &ktimeline->lock,
  32. ktimeline->fence_context, timestamp);
  33. /*
  34. * sync_file_create() takes a refcount to the fence. This refcount is
  35. * put when the fence is signaled.
  36. */
  37. kfence->sync_file = sync_file_create(&kfence->fence);
  38. if (kfence->sync_file == NULL) {
  39. kgsl_sync_timeline_put(ktimeline);
  40. dev_err(context->device->dev, "Create sync_file failed\n");
  41. kfree(kfence);
  42. return NULL;
  43. }
  44. spin_lock_irqsave(&ktimeline->lock, flags);
  45. list_add_tail(&kfence->child_list, &ktimeline->child_list_head);
  46. spin_unlock_irqrestore(&ktimeline->lock, flags);
  47. return kfence;
  48. }
  49. static void kgsl_sync_fence_release(struct dma_fence *fence)
  50. {
  51. struct kgsl_sync_fence *kfence = (struct kgsl_sync_fence *)fence;
  52. if (test_bit(MSM_HW_FENCE_FLAG_ENABLED_BIT, &fence->flags))
  53. msm_hw_fence_destroy(kfence->hw_fence_handle, fence);
  54. kgsl_sync_timeline_put(kfence->parent);
  55. kfree(kfence);
  56. }
  57. /* Called with ktimeline->lock held */
  58. static bool kgsl_sync_fence_has_signaled(struct dma_fence *fence)
  59. {
  60. struct kgsl_sync_fence *kfence = (struct kgsl_sync_fence *)fence;
  61. struct kgsl_sync_timeline *ktimeline = kfence->parent;
  62. unsigned int ts = kfence->timestamp;
  63. return (timestamp_cmp(ktimeline->last_timestamp, ts) >= 0);
  64. }
  65. static bool kgsl_enable_signaling(struct dma_fence *fence)
  66. {
  67. return !kgsl_sync_fence_has_signaled(fence);
  68. }
  69. struct kgsl_sync_fence_event_priv {
  70. struct kgsl_context *context;
  71. unsigned int timestamp;
  72. };
  73. /**
  74. * kgsl_sync_fence_event_cb - Event callback for a fence timestamp event
  75. * @device - The KGSL device that expired the timestamp
  76. * @context- Pointer to the context that owns the event
  77. * @priv: Private data for the callback
  78. * @result - Result of the event (retired or canceled)
  79. *
  80. * Signal a fence following the expiration of a timestamp
  81. */
  82. static void kgsl_sync_fence_event_cb(struct kgsl_device *device,
  83. struct kgsl_event_group *group, void *priv, int result)
  84. {
  85. struct kgsl_sync_fence_event_priv *ev = priv;
  86. kgsl_sync_timeline_signal(ev->context->ktimeline, ev->timestamp);
  87. kgsl_context_put(ev->context);
  88. kfree(ev);
  89. }
  90. static int _add_fence_event(struct kgsl_device *device,
  91. struct kgsl_context *context, unsigned int timestamp)
  92. {
  93. struct kgsl_sync_fence_event_priv *event;
  94. int ret;
  95. event = kmalloc(sizeof(*event), GFP_KERNEL);
  96. if (event == NULL)
  97. return -ENOMEM;
  98. /*
  99. * Increase the refcount for the context to keep it through the
  100. * callback
  101. */
  102. if (!_kgsl_context_get(context)) {
  103. kfree(event);
  104. return -ENOENT;
  105. }
  106. event->context = context;
  107. event->timestamp = timestamp;
  108. ret = kgsl_add_event(device, &context->events, timestamp,
  109. kgsl_sync_fence_event_cb, event);
  110. if (ret) {
  111. kgsl_context_put(context);
  112. kfree(event);
  113. }
  114. return ret;
  115. }
  116. /* Only to be used if creating a related event failed */
  117. static void kgsl_sync_cancel(struct kgsl_sync_fence *kfence)
  118. {
  119. spin_lock(&kfence->parent->lock);
  120. if (!list_empty(&kfence->child_list)) {
  121. list_del_init(&kfence->child_list);
  122. dma_fence_put(&kfence->fence);
  123. }
  124. spin_unlock(&kfence->parent->lock);
  125. }
  126. /**
  127. * kgsl_add_fence_event - Create a new fence event
  128. * @device - KGSL device to create the event on
  129. * @timestamp - Timestamp to trigger the event
  130. * @data - Return fence fd stored in struct kgsl_timestamp_event_fence
  131. * @len - length of the fence event
  132. * @owner - driver instance that owns this event
  133. * @returns 0 on success or error code on error
  134. *
  135. * Create a fence and register an event to signal the fence when
  136. * the timestamp expires
  137. */
  138. int kgsl_add_fence_event(struct kgsl_device *device,
  139. u32 context_id, u32 timestamp, void __user *data, int len,
  140. struct kgsl_device_private *owner)
  141. {
  142. struct kgsl_timestamp_event_fence priv;
  143. struct kgsl_context *context;
  144. struct kgsl_sync_fence *kfence = NULL;
  145. int ret = -EINVAL;
  146. unsigned int cur;
  147. bool retired = false;
  148. priv.fence_fd = -1;
  149. if (len != sizeof(priv))
  150. return -EINVAL;
  151. context = kgsl_context_get_owner(owner, context_id);
  152. if (context == NULL)
  153. return -EINVAL;
  154. if (test_bit(KGSL_CONTEXT_PRIV_INVALID, &context->priv))
  155. goto out;
  156. kfence = kgsl_sync_fence_create(context, timestamp);
  157. if (kfence == NULL) {
  158. ret = -ENOMEM;
  159. goto out;
  160. }
  161. priv.fence_fd = get_unused_fd_flags(0);
  162. if (priv.fence_fd < 0) {
  163. dev_crit_ratelimited(device->dev,
  164. "Unable to get a file descriptor: %d\n",
  165. priv.fence_fd);
  166. ret = priv.fence_fd;
  167. goto out;
  168. }
  169. /*
  170. * If the timestamp hasn't expired yet create an event to trigger it.
  171. * Otherwise, just signal the fence - there is no reason to go through
  172. * the effort of creating a fence we don't need.
  173. */
  174. kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED, &cur);
  175. retired = timestamp_cmp(cur, timestamp) >= 0;
  176. if (retired) {
  177. ret = 0;
  178. kgsl_sync_timeline_signal(context->ktimeline, cur);
  179. } else {
  180. ret = _add_fence_event(device, context, timestamp);
  181. if (ret)
  182. goto out;
  183. }
  184. if (copy_to_user(data, &priv, sizeof(priv))) {
  185. ret = -EFAULT;
  186. goto out;
  187. }
  188. if (!retired)
  189. device->ftbl->create_hw_fence(device, kfence);
  190. fd_install(priv.fence_fd, kfence->sync_file->file);
  191. out:
  192. kgsl_context_put(context);
  193. if (ret) {
  194. if (priv.fence_fd >= 0)
  195. put_unused_fd(priv.fence_fd);
  196. if (kfence) {
  197. kgsl_sync_cancel(kfence);
  198. /*
  199. * Put the refcount of sync file. This will release
  200. * kfence->fence as well.
  201. */
  202. fput(kfence->sync_file->file);
  203. }
  204. }
  205. return ret;
  206. }
  207. static void kgsl_sync_timeline_value_str(struct dma_fence *fence,
  208. char *str, int size)
  209. {
  210. struct kgsl_sync_fence *kfence = (struct kgsl_sync_fence *)fence;
  211. struct kgsl_sync_timeline *ktimeline = kfence->parent;
  212. struct kgsl_context *context = NULL;
  213. unsigned long flags;
  214. int ret = 0;
  215. unsigned int timestamp_retired;
  216. unsigned int timestamp_queued;
  217. if (!kref_get_unless_zero(&ktimeline->kref))
  218. return;
  219. if (!ktimeline->device)
  220. goto put_timeline;
  221. spin_lock_irqsave(&ktimeline->lock, flags);
  222. ret = _kgsl_context_get(ktimeline->context);
  223. context = ret ? ktimeline->context : NULL;
  224. spin_unlock_irqrestore(&ktimeline->lock, flags);
  225. /* Get the last signaled timestamp if the context is not valid */
  226. timestamp_queued = ktimeline->last_timestamp;
  227. timestamp_retired = timestamp_queued;
  228. if (context) {
  229. kgsl_readtimestamp(ktimeline->device, context,
  230. KGSL_TIMESTAMP_RETIRED, &timestamp_retired);
  231. kgsl_readtimestamp(ktimeline->device, context,
  232. KGSL_TIMESTAMP_QUEUED, &timestamp_queued);
  233. kgsl_context_put(context);
  234. }
  235. snprintf(str, size, "%u queued:%u retired:%u",
  236. ktimeline->last_timestamp,
  237. timestamp_queued, timestamp_retired);
  238. put_timeline:
  239. kgsl_sync_timeline_put(ktimeline);
  240. }
  241. static void kgsl_sync_fence_value_str(struct dma_fence *fence,
  242. char *str, int size)
  243. {
  244. struct kgsl_sync_fence *kfence = (struct kgsl_sync_fence *)fence;
  245. snprintf(str, size, "%u", kfence->timestamp);
  246. }
  247. static const char *kgsl_sync_fence_driver_name(struct dma_fence *fence)
  248. {
  249. return "kgsl-timeline";
  250. }
  251. static const char *kgsl_sync_timeline_name(struct dma_fence *fence)
  252. {
  253. struct kgsl_sync_fence *kfence = (struct kgsl_sync_fence *)fence;
  254. struct kgsl_sync_timeline *ktimeline = kfence->parent;
  255. return ktimeline->name;
  256. }
  257. int kgsl_sync_timeline_create(struct kgsl_context *context)
  258. {
  259. struct kgsl_sync_timeline *ktimeline;
  260. /* Put context at detach time */
  261. if (!_kgsl_context_get(context))
  262. return -ENOENT;
  263. ktimeline = kzalloc(sizeof(*ktimeline), GFP_KERNEL);
  264. if (ktimeline == NULL) {
  265. kgsl_context_put(context);
  266. return -ENOMEM;
  267. }
  268. kref_init(&ktimeline->kref);
  269. snprintf(ktimeline->name, sizeof(ktimeline->name),
  270. "%s_%u-%.15s(%d)-%.15s(%d)",
  271. context->device->name, context->id,
  272. current->group_leader->comm, current->group_leader->pid,
  273. current->comm, current->pid);
  274. ktimeline->fence_context = dma_fence_context_alloc(1);
  275. ktimeline->last_timestamp = 0;
  276. INIT_LIST_HEAD(&ktimeline->child_list_head);
  277. spin_lock_init(&ktimeline->lock);
  278. ktimeline->device = context->device;
  279. /*
  280. * The context pointer is valid till detach time, where we put the
  281. * refcount on the context
  282. */
  283. ktimeline->context = context;
  284. context->ktimeline = ktimeline;
  285. return 0;
  286. }
  287. void kgsl_sync_timeline_signal(struct kgsl_sync_timeline *ktimeline,
  288. unsigned int timestamp)
  289. {
  290. unsigned long flags;
  291. struct kgsl_sync_fence *kfence, *next;
  292. if (!kref_get_unless_zero(&ktimeline->kref))
  293. return;
  294. spin_lock_irqsave(&ktimeline->lock, flags);
  295. if (timestamp_cmp(timestamp, ktimeline->last_timestamp) > 0)
  296. ktimeline->last_timestamp = timestamp;
  297. list_for_each_entry_safe(kfence, next, &ktimeline->child_list_head,
  298. child_list) {
  299. if (dma_fence_is_signaled_locked(&kfence->fence)) {
  300. list_del_init(&kfence->child_list);
  301. dma_fence_put(&kfence->fence);
  302. }
  303. }
  304. spin_unlock_irqrestore(&ktimeline->lock, flags);
  305. kgsl_sync_timeline_put(ktimeline);
  306. }
  307. void kgsl_sync_timeline_detach(struct kgsl_sync_timeline *ktimeline)
  308. {
  309. unsigned long flags;
  310. struct kgsl_context *context = ktimeline->context;
  311. /* Set context pointer to NULL and drop our refcount on the context */
  312. spin_lock_irqsave(&ktimeline->lock, flags);
  313. ktimeline->context = NULL;
  314. spin_unlock_irqrestore(&ktimeline->lock, flags);
  315. kgsl_context_put(context);
  316. }
  317. static void kgsl_sync_timeline_destroy(struct kref *kref)
  318. {
  319. struct kgsl_sync_timeline *ktimeline =
  320. container_of(kref, struct kgsl_sync_timeline, kref);
  321. kfree(ktimeline);
  322. }
  323. void kgsl_sync_timeline_put(struct kgsl_sync_timeline *ktimeline)
  324. {
  325. if (ktimeline)
  326. kref_put(&ktimeline->kref, kgsl_sync_timeline_destroy);
  327. }
  328. static const struct dma_fence_ops kgsl_sync_fence_ops = {
  329. .get_driver_name = kgsl_sync_fence_driver_name,
  330. .get_timeline_name = kgsl_sync_timeline_name,
  331. .enable_signaling = kgsl_enable_signaling,
  332. .signaled = kgsl_sync_fence_has_signaled,
  333. .wait = dma_fence_default_wait,
  334. .release = kgsl_sync_fence_release,
  335. .fence_value_str = kgsl_sync_fence_value_str,
  336. .timeline_value_str = kgsl_sync_timeline_value_str,
  337. };
  338. static void kgsl_sync_fence_callback(struct dma_fence *fence,
  339. struct dma_fence_cb *cb)
  340. {
  341. struct kgsl_sync_fence_cb *kcb = (struct kgsl_sync_fence_cb *)cb;
  342. kcb->func(kcb->priv);
  343. }
  344. bool is_kgsl_fence(struct dma_fence *f)
  345. {
  346. if (f->ops == &kgsl_sync_fence_ops)
  347. return true;
  348. return false;
  349. }
  350. static void kgsl_count_hw_fences(struct kgsl_drawobj_sync_event *event, struct dma_fence *fence)
  351. {
  352. /*
  353. * Even one unsignaled sw-only fence in this sync object means we can't send this sync
  354. * object to the hardware
  355. */
  356. if (event->syncobj->flags & KGSL_SYNCOBJ_SW)
  357. return;
  358. if (!test_bit(MSM_HW_FENCE_FLAG_ENABLED_BIT, &fence->flags)) {
  359. /* Ignore software fences that are already signaled */
  360. if (!dma_fence_is_signaled(fence))
  361. event->syncobj->flags |= KGSL_SYNCOBJ_SW;
  362. } else {
  363. event->syncobj->num_hw_fence++;
  364. }
  365. }
  366. void kgsl_get_fence_info(struct kgsl_drawobj_sync_event *event)
  367. {
  368. unsigned int num_fences;
  369. struct dma_fence *fence, **fences;
  370. struct dma_fence_array *array;
  371. struct event_fence_info *info_ptr = event->priv;
  372. int i;
  373. fence = event->handle->fence;
  374. array = to_dma_fence_array(fence);
  375. if (array != NULL) {
  376. num_fences = array->num_fences;
  377. fences = array->fences;
  378. } else {
  379. num_fences = 1;
  380. fences = &fence;
  381. }
  382. if (!info_ptr)
  383. goto count;
  384. info_ptr->fences = kcalloc(num_fences, sizeof(struct fence_info),
  385. GFP_KERNEL);
  386. if (info_ptr->fences == NULL)
  387. goto count;
  388. info_ptr->num_fences = num_fences;
  389. for (i = 0; i < num_fences; i++) {
  390. struct dma_fence *f = fences[i];
  391. struct fence_info *fi = &info_ptr->fences[i];
  392. int len;
  393. len = scnprintf(fi->name, sizeof(fi->name), "%s %s",
  394. f->ops->get_driver_name(f),
  395. f->ops->get_timeline_name(f));
  396. if (f->ops->fence_value_str) {
  397. len += scnprintf(fi->name + len, sizeof(fi->name) - len,
  398. ": ");
  399. f->ops->fence_value_str(f, fi->name + len,
  400. sizeof(fi->name) - len);
  401. }
  402. kgsl_count_hw_fences(event, f);
  403. }
  404. return;
  405. count:
  406. for (i = 0; i < num_fences; i++)
  407. kgsl_count_hw_fences(event, fences[i]);
  408. }
  409. struct kgsl_sync_fence_cb *kgsl_sync_fence_async_wait(int fd,
  410. bool (*func)(void *priv), void *priv)
  411. {
  412. struct kgsl_sync_fence_cb *kcb;
  413. struct dma_fence *fence;
  414. int status;
  415. fence = sync_file_get_fence(fd);
  416. if (fence == NULL)
  417. return ERR_PTR(-EINVAL);
  418. /* create the callback */
  419. kcb = kzalloc(sizeof(*kcb), GFP_KERNEL);
  420. if (kcb == NULL) {
  421. dma_fence_put(fence);
  422. return ERR_PTR(-ENOMEM);
  423. }
  424. kcb->fence = fence;
  425. kcb->priv = priv;
  426. kcb->func = func;
  427. /* if status then error or signaled */
  428. status = dma_fence_add_callback(fence, &kcb->fence_cb,
  429. kgsl_sync_fence_callback);
  430. if (status) {
  431. kfree(kcb);
  432. if (!dma_fence_is_signaled(fence))
  433. kcb = ERR_PTR(status);
  434. else
  435. kcb = NULL;
  436. dma_fence_put(fence);
  437. }
  438. return kcb;
  439. }
  440. /*
  441. * Cancel the fence async callback.
  442. */
  443. void kgsl_sync_fence_async_cancel(struct kgsl_sync_fence_cb *kcb)
  444. {
  445. if (kcb == NULL)
  446. return;
  447. dma_fence_remove_callback(kcb->fence, &kcb->fence_cb);
  448. }
  449. struct kgsl_syncsource {
  450. struct kref refcount;
  451. char name[32];
  452. int id;
  453. struct kgsl_process_private *private;
  454. struct list_head child_list_head;
  455. spinlock_t lock;
  456. };
  457. struct kgsl_syncsource_fence {
  458. struct dma_fence fence;
  459. struct kgsl_syncsource *parent;
  460. struct list_head child_list;
  461. };
  462. static const struct dma_fence_ops kgsl_syncsource_fence_ops;
  463. long kgsl_ioctl_syncsource_create(struct kgsl_device_private *dev_priv,
  464. unsigned int cmd, void *data)
  465. {
  466. struct kgsl_syncsource *syncsource = NULL;
  467. struct kgsl_syncsource_create *param = data;
  468. int ret = -EINVAL;
  469. int id = 0;
  470. struct kgsl_process_private *private = dev_priv->process_priv;
  471. if (!kgsl_process_private_get(private))
  472. return ret;
  473. syncsource = kzalloc(sizeof(*syncsource), GFP_KERNEL);
  474. if (syncsource == NULL) {
  475. ret = -ENOMEM;
  476. goto out;
  477. }
  478. kref_init(&syncsource->refcount);
  479. snprintf(syncsource->name, sizeof(syncsource->name),
  480. "kgsl-syncsource-pid-%d", current->group_leader->pid);
  481. syncsource->private = private;
  482. INIT_LIST_HEAD(&syncsource->child_list_head);
  483. spin_lock_init(&syncsource->lock);
  484. idr_preload(GFP_KERNEL);
  485. spin_lock(&private->syncsource_lock);
  486. id = idr_alloc(&private->syncsource_idr, syncsource, 1, 0, GFP_NOWAIT);
  487. if (id > 0) {
  488. syncsource->id = id;
  489. param->id = id;
  490. ret = 0;
  491. } else {
  492. ret = id;
  493. }
  494. spin_unlock(&private->syncsource_lock);
  495. idr_preload_end();
  496. out:
  497. if (ret) {
  498. kgsl_process_private_put(private);
  499. kfree(syncsource);
  500. }
  501. return ret;
  502. }
  503. static struct kgsl_syncsource *
  504. kgsl_syncsource_get(struct kgsl_process_private *private, int id)
  505. {
  506. int result = 0;
  507. struct kgsl_syncsource *syncsource = NULL;
  508. spin_lock(&private->syncsource_lock);
  509. syncsource = idr_find(&private->syncsource_idr, id);
  510. if (syncsource)
  511. result = kref_get_unless_zero(&syncsource->refcount);
  512. spin_unlock(&private->syncsource_lock);
  513. return result ? syncsource : NULL;
  514. }
  515. static void kgsl_syncsource_destroy(struct kref *kref)
  516. {
  517. struct kgsl_syncsource *syncsource = container_of(kref,
  518. struct kgsl_syncsource,
  519. refcount);
  520. struct kgsl_process_private *private = syncsource->private;
  521. /* Done with process private. Release the refcount */
  522. kgsl_process_private_put(private);
  523. kfree(syncsource);
  524. }
  525. void kgsl_syncsource_put(struct kgsl_syncsource *syncsource)
  526. {
  527. if (syncsource)
  528. kref_put(&syncsource->refcount, kgsl_syncsource_destroy);
  529. }
  530. static void kgsl_syncsource_cleanup(struct kgsl_process_private *private,
  531. struct kgsl_syncsource *syncsource)
  532. {
  533. struct kgsl_syncsource_fence *sfence, *next;
  534. unsigned long flags;
  535. /* Signal all fences to release any callbacks */
  536. spin_lock_irqsave(&syncsource->lock, flags);
  537. list_for_each_entry_safe(sfence, next, &syncsource->child_list_head,
  538. child_list) {
  539. dma_fence_signal_locked(&sfence->fence);
  540. list_del_init(&sfence->child_list);
  541. }
  542. spin_unlock_irqrestore(&syncsource->lock, flags);
  543. /* put reference from syncsource creation */
  544. kgsl_syncsource_put(syncsource);
  545. }
  546. long kgsl_ioctl_syncsource_destroy(struct kgsl_device_private *dev_priv,
  547. unsigned int cmd, void *data)
  548. {
  549. struct kgsl_syncsource_destroy *param = data;
  550. struct kgsl_syncsource *syncsource = NULL;
  551. struct kgsl_process_private *private = dev_priv->process_priv;
  552. spin_lock(&private->syncsource_lock);
  553. syncsource = idr_find(&private->syncsource_idr, param->id);
  554. if (syncsource == NULL) {
  555. spin_unlock(&private->syncsource_lock);
  556. return -EINVAL;
  557. }
  558. if (syncsource->id != 0) {
  559. idr_remove(&private->syncsource_idr, syncsource->id);
  560. syncsource->id = 0;
  561. }
  562. spin_unlock(&private->syncsource_lock);
  563. kgsl_syncsource_cleanup(private, syncsource);
  564. return 0;
  565. }
  566. long kgsl_ioctl_syncsource_create_fence(struct kgsl_device_private *dev_priv,
  567. unsigned int cmd, void *data)
  568. {
  569. struct kgsl_syncsource_create_fence *param = data;
  570. struct kgsl_syncsource *syncsource = NULL;
  571. int ret = -EINVAL;
  572. struct kgsl_syncsource_fence *sfence = NULL;
  573. struct sync_file *sync_file = NULL;
  574. int fd = -1;
  575. unsigned long flags;
  576. /*
  577. * Take a refcount that is released when the fence is released
  578. * (or if fence can't be added to the syncsource).
  579. */
  580. syncsource = kgsl_syncsource_get(dev_priv->process_priv,
  581. param->id);
  582. if (syncsource == NULL)
  583. goto out;
  584. sfence = kzalloc(sizeof(*sfence), GFP_KERNEL);
  585. if (sfence == NULL) {
  586. ret = -ENOMEM;
  587. goto out;
  588. }
  589. sfence->parent = syncsource;
  590. /* Use a new fence context for each fence */
  591. dma_fence_init(&sfence->fence, &kgsl_syncsource_fence_ops,
  592. &syncsource->lock, dma_fence_context_alloc(1), 1);
  593. sync_file = sync_file_create(&sfence->fence);
  594. if (sync_file == NULL) {
  595. dev_err(dev_priv->device->dev,
  596. "Create sync_file failed\n");
  597. ret = -ENOMEM;
  598. goto out;
  599. }
  600. fd = get_unused_fd_flags(0);
  601. if (fd < 0) {
  602. ret = -EBADF;
  603. goto out;
  604. }
  605. ret = 0;
  606. fd_install(fd, sync_file->file);
  607. param->fence_fd = fd;
  608. spin_lock_irqsave(&syncsource->lock, flags);
  609. list_add_tail(&sfence->child_list, &syncsource->child_list_head);
  610. spin_unlock_irqrestore(&syncsource->lock, flags);
  611. out:
  612. /*
  613. * We're transferring ownership of the fence to the sync file.
  614. * The sync file takes an extra refcount when it is created, so put
  615. * our refcount.
  616. */
  617. if (sync_file)
  618. dma_fence_put(&sfence->fence);
  619. if (ret) {
  620. if (sync_file)
  621. fput(sync_file->file);
  622. else if (sfence)
  623. dma_fence_put(&sfence->fence);
  624. else
  625. kgsl_syncsource_put(syncsource);
  626. }
  627. return ret;
  628. }
  629. static int kgsl_syncsource_signal(struct kgsl_syncsource *syncsource,
  630. struct dma_fence *fence)
  631. {
  632. struct kgsl_syncsource_fence *sfence, *next;
  633. int ret = -EINVAL;
  634. unsigned long flags;
  635. spin_lock_irqsave(&syncsource->lock, flags);
  636. list_for_each_entry_safe(sfence, next, &syncsource->child_list_head,
  637. child_list) {
  638. if (fence == &sfence->fence) {
  639. dma_fence_signal_locked(fence);
  640. list_del_init(&sfence->child_list);
  641. ret = 0;
  642. break;
  643. }
  644. }
  645. spin_unlock_irqrestore(&syncsource->lock, flags);
  646. return ret;
  647. }
  648. long kgsl_ioctl_syncsource_signal_fence(struct kgsl_device_private *dev_priv,
  649. unsigned int cmd, void *data)
  650. {
  651. int ret = -EINVAL;
  652. struct kgsl_syncsource_signal_fence *param = data;
  653. struct kgsl_syncsource *syncsource = NULL;
  654. struct dma_fence *fence = NULL;
  655. syncsource = kgsl_syncsource_get(dev_priv->process_priv,
  656. param->id);
  657. if (syncsource == NULL)
  658. goto out;
  659. fence = sync_file_get_fence(param->fence_fd);
  660. if (fence == NULL) {
  661. ret = -EBADF;
  662. goto out;
  663. }
  664. ret = kgsl_syncsource_signal(syncsource, fence);
  665. out:
  666. if (fence)
  667. dma_fence_put(fence);
  668. if (syncsource)
  669. kgsl_syncsource_put(syncsource);
  670. return ret;
  671. }
  672. static void kgsl_syncsource_fence_release(struct dma_fence *fence)
  673. {
  674. struct kgsl_syncsource_fence *sfence =
  675. (struct kgsl_syncsource_fence *)fence;
  676. /* Signal if it's not signaled yet */
  677. kgsl_syncsource_signal(sfence->parent, fence);
  678. /* Release the refcount on the syncsource */
  679. kgsl_syncsource_put(sfence->parent);
  680. kfree(sfence);
  681. }
  682. void kgsl_syncsource_process_release_syncsources(
  683. struct kgsl_process_private *private)
  684. {
  685. struct kgsl_syncsource *syncsource;
  686. int next = 0;
  687. while (1) {
  688. spin_lock(&private->syncsource_lock);
  689. syncsource = idr_get_next(&private->syncsource_idr, &next);
  690. if (syncsource == NULL) {
  691. spin_unlock(&private->syncsource_lock);
  692. break;
  693. }
  694. if (syncsource->id != 0) {
  695. idr_remove(&private->syncsource_idr, syncsource->id);
  696. syncsource->id = 0;
  697. }
  698. spin_unlock(&private->syncsource_lock);
  699. kgsl_syncsource_cleanup(private, syncsource);
  700. next = next + 1;
  701. }
  702. }
  703. static const char *kgsl_syncsource_get_timeline_name(struct dma_fence *fence)
  704. {
  705. struct kgsl_syncsource_fence *sfence =
  706. (struct kgsl_syncsource_fence *)fence;
  707. struct kgsl_syncsource *syncsource = sfence->parent;
  708. return syncsource->name;
  709. }
  710. static bool kgsl_syncsource_enable_signaling(struct dma_fence *fence)
  711. {
  712. return true;
  713. }
  714. static const char *kgsl_syncsource_driver_name(struct dma_fence *fence)
  715. {
  716. return "kgsl-syncsource-timeline";
  717. }
  718. static void kgsl_syncsource_fence_value_str(struct dma_fence *fence,
  719. char *str, int size)
  720. {
  721. /*
  722. * Each fence is independent of the others on the same timeline.
  723. * We use a different context for each of them.
  724. */
  725. snprintf(str, size, "%llu", fence->context);
  726. }
  727. static const struct dma_fence_ops kgsl_syncsource_fence_ops = {
  728. .get_driver_name = kgsl_syncsource_driver_name,
  729. .get_timeline_name = kgsl_syncsource_get_timeline_name,
  730. .enable_signaling = kgsl_syncsource_enable_signaling,
  731. .wait = dma_fence_default_wait,
  732. .release = kgsl_syncsource_fence_release,
  733. .fence_value_str = kgsl_syncsource_fence_value_str,
  734. };