vmwgfx_fence.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2009-2025 Broadcom. All Rights Reserved. The term
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
  6. *
  7. **************************************************************************/
  8. #include "vmwgfx_drv.h"
  9. #define VMW_FENCE_WRAP (1 << 31)
  10. struct vmw_fence_manager {
  11. struct vmw_private *dev_priv;
  12. spinlock_t lock;
  13. struct list_head fence_list;
  14. bool fifo_down;
  15. u64 ctx;
  16. };
  17. struct vmw_user_fence {
  18. struct ttm_base_object base;
  19. struct vmw_fence_obj fence;
  20. };
  21. /**
  22. * struct vmw_event_fence_action - fence callback that delivers a DRM event.
  23. *
  24. * @base: For use with dma_fence_add_callback(...)
  25. * @event: A pointer to the pending event.
  26. * @dev: Pointer to a struct drm_device so we can access the event stuff.
  27. * @tv_sec: If non-null, the variable pointed to will be assigned
  28. * current time tv_sec val when the fence signals.
  29. * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
  30. * be assigned the current time tv_usec val when the fence signals.
  31. */
  32. struct vmw_event_fence_action {
  33. struct dma_fence_cb base;
  34. struct drm_pending_event *event;
  35. struct drm_device *dev;
  36. uint32_t *tv_sec;
  37. uint32_t *tv_usec;
  38. };
  39. static struct vmw_fence_manager *
  40. fman_from_fence(struct vmw_fence_obj *fence)
  41. {
  42. return container_of(fence->base.lock, struct vmw_fence_manager, lock);
  43. }
  44. static void vmw_fence_obj_destroy(struct dma_fence *f)
  45. {
  46. struct vmw_fence_obj *fence =
  47. container_of(f, struct vmw_fence_obj, base);
  48. struct vmw_fence_manager *fman = fman_from_fence(fence);
  49. if (!list_empty(&fence->head)) {
  50. /* The fence manager still has an implicit reference to this
  51. * fence via the fence list if head is set. Because the lock is
  52. * required to be held when the fence manager updates the fence
  53. * list either the fence will have been removed after we get
  54. * the lock below or we can safely remove it and the fence
  55. * manager will never see it. This implies the fence is being
  56. * deleted without being signaled which is dubious but valid
  57. * if there are no callbacks. The dma_fence code that calls
  58. * this hook will warn about deleted unsignaled with callbacks
  59. * so no need to warn again here.
  60. */
  61. spin_lock(&fman->lock);
  62. list_del_init(&fence->head);
  63. if (fence->waiter_added)
  64. vmw_seqno_waiter_remove(fman->dev_priv);
  65. spin_unlock(&fman->lock);
  66. }
  67. fence->destroy(fence);
  68. }
  69. static const char *vmw_fence_get_driver_name(struct dma_fence *f)
  70. {
  71. return "vmwgfx";
  72. }
  73. static const char *vmw_fence_get_timeline_name(struct dma_fence *f)
  74. {
  75. return "svga";
  76. }
  77. /* When we toggle signaling for the SVGA device there is a race period from
  78. * the time we first read the fence seqno to the time we enable interrupts.
  79. * If we miss the interrupt for a fence during this period its likely the driver
  80. * will stall. As a result we need to re-read the seqno after interrupts are
  81. * enabled. If interrupts were already enabled we just increment the number of
  82. * seqno waiters.
  83. */
  84. static bool vmw_fence_enable_signaling(struct dma_fence *f)
  85. {
  86. u32 seqno;
  87. struct vmw_fence_obj *fence =
  88. container_of(f, struct vmw_fence_obj, base);
  89. struct vmw_fence_manager *fman = fman_from_fence(fence);
  90. struct vmw_private *dev_priv = fman->dev_priv;
  91. check_for_race:
  92. seqno = vmw_fence_read(dev_priv);
  93. if (seqno - fence->base.seqno < VMW_FENCE_WRAP) {
  94. if (fence->waiter_added) {
  95. vmw_seqno_waiter_remove(dev_priv);
  96. fence->waiter_added = false;
  97. }
  98. return false;
  99. } else if (!fence->waiter_added) {
  100. fence->waiter_added = true;
  101. if (vmw_seqno_waiter_add(dev_priv))
  102. goto check_for_race;
  103. }
  104. return true;
  105. }
  106. static u32 __vmw_fences_update(struct vmw_fence_manager *fman);
  107. static const struct dma_fence_ops vmw_fence_ops = {
  108. .get_driver_name = vmw_fence_get_driver_name,
  109. .get_timeline_name = vmw_fence_get_timeline_name,
  110. .enable_signaling = vmw_fence_enable_signaling,
  111. .release = vmw_fence_obj_destroy,
  112. };
  113. struct vmw_fence_manager *vmw_fence_manager_init(struct vmw_private *dev_priv)
  114. {
  115. struct vmw_fence_manager *fman = kzalloc_obj(*fman);
  116. if (unlikely(!fman))
  117. return NULL;
  118. fman->dev_priv = dev_priv;
  119. spin_lock_init(&fman->lock);
  120. INIT_LIST_HEAD(&fman->fence_list);
  121. fman->fifo_down = true;
  122. fman->ctx = dma_fence_context_alloc(1);
  123. return fman;
  124. }
  125. void vmw_fence_manager_takedown(struct vmw_fence_manager *fman)
  126. {
  127. bool lists_empty;
  128. spin_lock(&fman->lock);
  129. lists_empty = list_empty(&fman->fence_list);
  130. spin_unlock(&fman->lock);
  131. BUG_ON(!lists_empty);
  132. kfree(fman);
  133. }
  134. static int vmw_fence_obj_init(struct vmw_fence_manager *fman,
  135. struct vmw_fence_obj *fence, u32 seqno,
  136. void (*destroy) (struct vmw_fence_obj *fence))
  137. {
  138. int ret = 0;
  139. dma_fence_init(&fence->base, &vmw_fence_ops, &fman->lock,
  140. fman->ctx, seqno);
  141. fence->destroy = destroy;
  142. spin_lock(&fman->lock);
  143. if (unlikely(fman->fifo_down)) {
  144. ret = -EBUSY;
  145. goto out_unlock;
  146. }
  147. /* This creates an implicit reference to the fence from the fence
  148. * manager. It will be dropped when the fence is signaled which is
  149. * expected to happen before deletion. The dtor has code to catch
  150. * the rare deletion before signaling case.
  151. */
  152. list_add_tail(&fence->head, &fman->fence_list);
  153. out_unlock:
  154. spin_unlock(&fman->lock);
  155. return ret;
  156. }
  157. static u32 __vmw_fences_update(struct vmw_fence_manager *fman)
  158. {
  159. struct vmw_fence_obj *fence, *next_fence;
  160. const bool cookie = dma_fence_begin_signalling();
  161. const u32 seqno = vmw_fence_read(fman->dev_priv);
  162. list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) {
  163. if (seqno - fence->base.seqno < VMW_FENCE_WRAP) {
  164. list_del_init(&fence->head);
  165. if (fence->waiter_added) {
  166. vmw_seqno_waiter_remove(fman->dev_priv);
  167. fence->waiter_added = false;
  168. }
  169. dma_fence_signal_locked(&fence->base);
  170. } else
  171. break;
  172. }
  173. dma_fence_end_signalling(cookie);
  174. atomic_set_release(&fman->dev_priv->last_read_seqno, seqno);
  175. return seqno;
  176. }
  177. u32 vmw_fences_update(struct vmw_fence_manager *fman)
  178. {
  179. u32 seqno;
  180. spin_lock(&fman->lock);
  181. seqno = __vmw_fences_update(fman);
  182. spin_unlock(&fman->lock);
  183. return seqno;
  184. }
  185. bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence)
  186. {
  187. struct vmw_fence_manager *fman = fman_from_fence(fence);
  188. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
  189. return true;
  190. vmw_fences_update(fman);
  191. return dma_fence_is_signaled(&fence->base);
  192. }
  193. int vmw_fence_obj_wait(struct vmw_fence_obj *fence, bool lazy,
  194. bool interruptible, unsigned long timeout)
  195. {
  196. long ret = dma_fence_wait_timeout(&fence->base, interruptible, timeout);
  197. if (likely(ret > 0))
  198. return 0;
  199. else if (ret == 0)
  200. return -EBUSY;
  201. else
  202. return ret;
  203. }
  204. static void vmw_fence_destroy(struct vmw_fence_obj *fence)
  205. {
  206. dma_fence_free(&fence->base);
  207. }
  208. int vmw_fence_create(struct vmw_fence_manager *fman,
  209. uint32_t seqno,
  210. struct vmw_fence_obj **p_fence)
  211. {
  212. struct vmw_fence_obj *fence;
  213. int ret;
  214. fence = kzalloc_obj(*fence);
  215. if (unlikely(!fence))
  216. return -ENOMEM;
  217. ret = vmw_fence_obj_init(fman, fence, seqno, vmw_fence_destroy);
  218. if (unlikely(ret != 0))
  219. goto out_err_init;
  220. *p_fence = fence;
  221. return 0;
  222. out_err_init:
  223. kfree(fence);
  224. return ret;
  225. }
  226. static void vmw_user_fence_destroy(struct vmw_fence_obj *fence)
  227. {
  228. struct vmw_user_fence *ufence =
  229. container_of(fence, struct vmw_user_fence, fence);
  230. ttm_base_object_kfree(ufence, base);
  231. }
  232. static void vmw_user_fence_base_release(struct ttm_base_object **p_base)
  233. {
  234. struct ttm_base_object *base = *p_base;
  235. struct vmw_user_fence *ufence =
  236. container_of(base, struct vmw_user_fence, base);
  237. struct vmw_fence_obj *fence = &ufence->fence;
  238. *p_base = NULL;
  239. vmw_fence_obj_unreference(&fence);
  240. }
  241. int vmw_user_fence_create(struct drm_file *file_priv,
  242. struct vmw_fence_manager *fman,
  243. uint32_t seqno,
  244. struct vmw_fence_obj **p_fence,
  245. uint32_t *p_handle)
  246. {
  247. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  248. struct vmw_user_fence *ufence;
  249. struct vmw_fence_obj *tmp;
  250. int ret;
  251. ufence = kzalloc_obj(*ufence);
  252. if (unlikely(!ufence)) {
  253. ret = -ENOMEM;
  254. goto out_no_object;
  255. }
  256. ret = vmw_fence_obj_init(fman, &ufence->fence, seqno,
  257. vmw_user_fence_destroy);
  258. if (unlikely(ret != 0)) {
  259. kfree(ufence);
  260. goto out_no_object;
  261. }
  262. /*
  263. * The base object holds a reference which is freed in
  264. * vmw_user_fence_base_release.
  265. */
  266. tmp = vmw_fence_obj_reference(&ufence->fence);
  267. ret = ttm_base_object_init(tfile, &ufence->base, false,
  268. VMW_RES_FENCE,
  269. &vmw_user_fence_base_release);
  270. if (unlikely(ret != 0)) {
  271. /*
  272. * Free the base object's reference
  273. */
  274. vmw_fence_obj_unreference(&tmp);
  275. goto out_err;
  276. }
  277. *p_fence = &ufence->fence;
  278. *p_handle = ufence->base.handle;
  279. return 0;
  280. out_err:
  281. tmp = &ufence->fence;
  282. vmw_fence_obj_unreference(&tmp);
  283. out_no_object:
  284. return ret;
  285. }
  286. /*
  287. * vmw_fence_fifo_down - signal all unsignaled fence objects.
  288. */
  289. void vmw_fence_fifo_down(struct vmw_fence_manager *fman)
  290. {
  291. int ret;
  292. /*
  293. * The list may be altered while we traverse it, so always
  294. * restart when we've released the fman->lock.
  295. */
  296. spin_lock(&fman->lock);
  297. fman->fifo_down = true;
  298. while (!list_empty(&fman->fence_list)) {
  299. struct vmw_fence_obj *fence =
  300. list_entry(fman->fence_list.prev, struct vmw_fence_obj,
  301. head);
  302. dma_fence_get(&fence->base);
  303. spin_unlock(&fman->lock);
  304. ret = vmw_fence_obj_wait(fence, false, false,
  305. VMW_FENCE_WAIT_TIMEOUT);
  306. if (unlikely(ret != 0)) {
  307. list_del_init(&fence->head);
  308. dma_fence_signal(&fence->base);
  309. }
  310. BUG_ON(!list_empty(&fence->head));
  311. dma_fence_put(&fence->base);
  312. spin_lock(&fman->lock);
  313. }
  314. spin_unlock(&fman->lock);
  315. }
  316. void vmw_fence_fifo_up(struct vmw_fence_manager *fman)
  317. {
  318. spin_lock(&fman->lock);
  319. fman->fifo_down = false;
  320. spin_unlock(&fman->lock);
  321. }
  322. /**
  323. * vmw_fence_obj_lookup - Look up a user-space fence object
  324. *
  325. * @tfile: A struct ttm_object_file identifying the caller.
  326. * @handle: A handle identifying the fence object.
  327. * @return: A struct vmw_user_fence base ttm object on success or
  328. * an error pointer on failure.
  329. *
  330. * The fence object is looked up and type-checked. The caller needs
  331. * to have opened the fence object first, but since that happens on
  332. * creation and fence objects aren't shareable, that's not an
  333. * issue currently.
  334. */
  335. static struct ttm_base_object *
  336. vmw_fence_obj_lookup(struct ttm_object_file *tfile, u32 handle)
  337. {
  338. struct ttm_base_object *base = ttm_base_object_lookup(tfile, handle);
  339. if (!base) {
  340. pr_err("Invalid fence object handle 0x%08lx.\n",
  341. (unsigned long)handle);
  342. return ERR_PTR(-EINVAL);
  343. }
  344. if (base->refcount_release != vmw_user_fence_base_release) {
  345. pr_err("Invalid fence object handle 0x%08lx.\n",
  346. (unsigned long)handle);
  347. ttm_base_object_unref(&base);
  348. return ERR_PTR(-EINVAL);
  349. }
  350. return base;
  351. }
  352. int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data,
  353. struct drm_file *file_priv)
  354. {
  355. struct drm_vmw_fence_wait_arg *arg =
  356. (struct drm_vmw_fence_wait_arg *)data;
  357. unsigned long timeout;
  358. struct ttm_base_object *base;
  359. struct vmw_fence_obj *fence;
  360. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  361. int ret;
  362. uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ);
  363. /*
  364. * 64-bit division not present on 32-bit systems, so do an
  365. * approximation. (Divide by 1000000).
  366. */
  367. wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) -
  368. (wait_timeout >> 26);
  369. if (!arg->cookie_valid) {
  370. arg->cookie_valid = 1;
  371. arg->kernel_cookie = jiffies + wait_timeout;
  372. }
  373. base = vmw_fence_obj_lookup(tfile, arg->handle);
  374. if (IS_ERR(base))
  375. return PTR_ERR(base);
  376. fence = &(container_of(base, struct vmw_user_fence, base)->fence);
  377. timeout = jiffies;
  378. if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) {
  379. ret = ((vmw_fence_obj_signaled(fence)) ?
  380. 0 : -EBUSY);
  381. goto out;
  382. }
  383. timeout = (unsigned long)arg->kernel_cookie - timeout;
  384. ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout);
  385. out:
  386. ttm_base_object_unref(&base);
  387. /*
  388. * Optionally unref the fence object.
  389. */
  390. if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF))
  391. return ttm_ref_object_base_unref(tfile, arg->handle);
  392. return ret;
  393. }
  394. int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data,
  395. struct drm_file *file_priv)
  396. {
  397. struct drm_vmw_fence_signaled_arg *arg =
  398. (struct drm_vmw_fence_signaled_arg *) data;
  399. struct ttm_base_object *base;
  400. struct vmw_fence_obj *fence;
  401. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  402. struct vmw_private *dev_priv = vmw_priv(dev);
  403. base = vmw_fence_obj_lookup(tfile, arg->handle);
  404. if (IS_ERR(base))
  405. return PTR_ERR(base);
  406. fence = &(container_of(base, struct vmw_user_fence, base)->fence);
  407. arg->signaled = vmw_fence_obj_signaled(fence);
  408. arg->signaled_flags = arg->flags;
  409. arg->passed_seqno = atomic_read_acquire(&dev_priv->last_read_seqno);
  410. ttm_base_object_unref(&base);
  411. return 0;
  412. }
  413. int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data,
  414. struct drm_file *file_priv)
  415. {
  416. struct drm_vmw_fence_arg *arg =
  417. (struct drm_vmw_fence_arg *) data;
  418. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  419. arg->handle);
  420. }
  421. /**
  422. * vmw_event_fence_action_seq_passed
  423. *
  424. * @f: The struct dma_fence which provides timestamp for the action event
  425. * @cb: The struct dma_fence_cb callback for the action event.
  426. *
  427. * This function is called when the seqno of the fence has passed
  428. * and it is always called from atomic context.
  429. * It queues the event on the submitter's event list.
  430. */
  431. static void vmw_event_fence_action_seq_passed(struct dma_fence *f,
  432. struct dma_fence_cb *cb)
  433. {
  434. struct vmw_event_fence_action *eaction =
  435. container_of(cb, struct vmw_event_fence_action, base);
  436. struct drm_device *dev = eaction->dev;
  437. struct drm_pending_event *event = eaction->event;
  438. if (unlikely(event == NULL))
  439. return;
  440. spin_lock_irq(&dev->event_lock);
  441. if (likely(eaction->tv_sec != NULL)) {
  442. struct timespec64 ts;
  443. ts = ktime_to_timespec64(f->timestamp);
  444. /* monotonic time, so no y2038 overflow */
  445. *eaction->tv_sec = ts.tv_sec;
  446. *eaction->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  447. }
  448. drm_send_event_locked(dev, eaction->event);
  449. eaction->event = NULL;
  450. spin_unlock_irq(&dev->event_lock);
  451. dma_fence_put(f);
  452. kfree(eaction);
  453. }
  454. /**
  455. * vmw_event_fence_action_queue - Post an event for sending when a fence
  456. * object seqno has passed.
  457. *
  458. * @file_priv: The file connection on which the event should be posted.
  459. * @fence: The fence object on which to post the event.
  460. * @event: Event to be posted. This event should've been alloced
  461. * using k[mz]alloc, and should've been completely initialized.
  462. * @tv_sec: If non-null, the variable pointed to will be assigned
  463. * current time tv_sec val when the fence signals.
  464. * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
  465. * be assigned the current time tv_usec val when the fence signals.
  466. * @interruptible: Interruptible waits if possible.
  467. *
  468. * As a side effect, the object pointed to by @event may have been
  469. * freed when this function returns. If this function returns with
  470. * an error code, the caller needs to free that object.
  471. */
  472. int vmw_event_fence_action_queue(struct drm_file *file_priv,
  473. struct vmw_fence_obj *fence,
  474. struct drm_pending_event *event,
  475. uint32_t *tv_sec,
  476. uint32_t *tv_usec,
  477. bool interruptible)
  478. {
  479. struct vmw_event_fence_action *eaction;
  480. struct vmw_fence_manager *fman = fman_from_fence(fence);
  481. eaction = kzalloc_obj(*eaction);
  482. if (unlikely(!eaction))
  483. return -ENOMEM;
  484. eaction->event = event;
  485. eaction->dev = &fman->dev_priv->drm;
  486. eaction->tv_sec = tv_sec;
  487. eaction->tv_usec = tv_usec;
  488. vmw_fence_obj_reference(fence); // Dropped in CB
  489. if (dma_fence_add_callback(&fence->base, &eaction->base,
  490. vmw_event_fence_action_seq_passed) < 0)
  491. vmw_event_fence_action_seq_passed(&fence->base, &eaction->base);
  492. return 0;
  493. }
  494. struct vmw_event_fence_pending {
  495. struct drm_pending_event base;
  496. struct drm_vmw_event_fence event;
  497. };
  498. static int vmw_event_fence_action_create(struct drm_file *file_priv,
  499. struct vmw_fence_obj *fence,
  500. uint32_t flags,
  501. uint64_t user_data,
  502. bool interruptible)
  503. {
  504. struct vmw_event_fence_pending *event;
  505. struct vmw_fence_manager *fman = fman_from_fence(fence);
  506. struct drm_device *dev = &fman->dev_priv->drm;
  507. int ret;
  508. event = kzalloc_obj(*event);
  509. if (unlikely(!event)) {
  510. DRM_ERROR("Failed to allocate an event.\n");
  511. ret = -ENOMEM;
  512. goto out_no_space;
  513. }
  514. event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED;
  515. event->event.base.length = sizeof(event->event);
  516. event->event.user_data = user_data;
  517. ret = drm_event_reserve_init(dev, file_priv, &event->base, &event->event.base);
  518. if (unlikely(ret != 0)) {
  519. DRM_ERROR("Failed to allocate event space for this file.\n");
  520. kfree(event);
  521. goto out_no_space;
  522. }
  523. if (flags & DRM_VMW_FE_FLAG_REQ_TIME)
  524. ret = vmw_event_fence_action_queue(file_priv, fence,
  525. &event->base,
  526. &event->event.tv_sec,
  527. &event->event.tv_usec,
  528. interruptible);
  529. else
  530. ret = vmw_event_fence_action_queue(file_priv, fence,
  531. &event->base,
  532. NULL,
  533. NULL,
  534. interruptible);
  535. if (ret != 0)
  536. goto out_no_queue;
  537. return 0;
  538. out_no_queue:
  539. drm_event_cancel_free(dev, &event->base);
  540. out_no_space:
  541. return ret;
  542. }
  543. int vmw_fence_event_ioctl(struct drm_device *dev, void *data,
  544. struct drm_file *file_priv)
  545. {
  546. struct vmw_private *dev_priv = vmw_priv(dev);
  547. struct drm_vmw_fence_event_arg *arg =
  548. (struct drm_vmw_fence_event_arg *) data;
  549. struct vmw_fence_obj *fence = NULL;
  550. struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
  551. struct ttm_object_file *tfile = vmw_fp->tfile;
  552. struct drm_vmw_fence_rep __user *user_fence_rep =
  553. (struct drm_vmw_fence_rep __user *)(unsigned long)
  554. arg->fence_rep;
  555. uint32_t handle;
  556. int ret;
  557. /*
  558. * Look up an existing fence object,
  559. * and if user-space wants a new reference,
  560. * add one.
  561. */
  562. if (arg->handle) {
  563. struct ttm_base_object *base =
  564. vmw_fence_obj_lookup(tfile, arg->handle);
  565. if (IS_ERR(base))
  566. return PTR_ERR(base);
  567. fence = &(container_of(base, struct vmw_user_fence,
  568. base)->fence);
  569. (void) vmw_fence_obj_reference(fence);
  570. if (user_fence_rep != NULL) {
  571. ret = ttm_ref_object_add(vmw_fp->tfile, base,
  572. NULL, false);
  573. if (unlikely(ret != 0)) {
  574. DRM_ERROR("Failed to reference a fence "
  575. "object.\n");
  576. goto out_no_ref_obj;
  577. }
  578. handle = base->handle;
  579. }
  580. ttm_base_object_unref(&base);
  581. }
  582. /*
  583. * Create a new fence object.
  584. */
  585. if (!fence) {
  586. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  587. &fence,
  588. (user_fence_rep) ?
  589. &handle : NULL);
  590. if (unlikely(ret != 0)) {
  591. DRM_ERROR("Fence event failed to create fence.\n");
  592. return ret;
  593. }
  594. }
  595. BUG_ON(fence == NULL);
  596. ret = vmw_event_fence_action_create(file_priv, fence,
  597. arg->flags,
  598. arg->user_data,
  599. true);
  600. if (unlikely(ret != 0)) {
  601. if (ret != -ERESTARTSYS)
  602. DRM_ERROR("Failed to attach event to fence.\n");
  603. goto out_no_create;
  604. }
  605. vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence,
  606. handle, -1);
  607. vmw_fence_obj_unreference(&fence);
  608. return 0;
  609. out_no_create:
  610. if (user_fence_rep != NULL)
  611. ttm_ref_object_base_unref(tfile, handle);
  612. out_no_ref_obj:
  613. vmw_fence_obj_unreference(&fence);
  614. return ret;
  615. }