vmwgfx_fence.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2011-2023 VMware, Inc., Palo Alto, CA., USA
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include <linux/sched/signal.h>
  28. #include "vmwgfx_drv.h"
  29. #define VMW_FENCE_WRAP (1 << 31)
  30. struct vmw_fence_manager {
  31. struct vmw_private *dev_priv;
  32. spinlock_t lock;
  33. struct list_head fence_list;
  34. struct work_struct work;
  35. bool fifo_down;
  36. struct list_head cleanup_list;
  37. uint32_t pending_actions[VMW_ACTION_MAX];
  38. struct mutex goal_irq_mutex;
  39. bool goal_irq_on; /* Protected by @goal_irq_mutex */
  40. bool seqno_valid; /* Protected by @lock, and may not be set to true
  41. without the @goal_irq_mutex held. */
  42. u64 ctx;
  43. };
  44. struct vmw_user_fence {
  45. struct ttm_base_object base;
  46. struct vmw_fence_obj fence;
  47. };
  48. /**
  49. * struct vmw_event_fence_action - fence action that delivers a drm event.
  50. *
  51. * @action: A struct vmw_fence_action to hook up to a fence.
  52. * @event: A pointer to the pending event.
  53. * @fence: A referenced pointer to the fence to keep it alive while @action
  54. * hangs on it.
  55. * @dev: Pointer to a struct drm_device so we can access the event stuff.
  56. * @tv_sec: If non-null, the variable pointed to will be assigned
  57. * current time tv_sec val when the fence signals.
  58. * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
  59. * be assigned the current time tv_usec val when the fence signals.
  60. */
  61. struct vmw_event_fence_action {
  62. struct vmw_fence_action action;
  63. struct drm_pending_event *event;
  64. struct vmw_fence_obj *fence;
  65. struct drm_device *dev;
  66. uint32_t *tv_sec;
  67. uint32_t *tv_usec;
  68. };
  69. static struct vmw_fence_manager *
  70. fman_from_fence(struct vmw_fence_obj *fence)
  71. {
  72. return container_of(fence->base.lock, struct vmw_fence_manager, lock);
  73. }
  74. static u32 vmw_fence_goal_read(struct vmw_private *vmw)
  75. {
  76. if ((vmw->capabilities2 & SVGA_CAP2_EXTRA_REGS) != 0)
  77. return vmw_read(vmw, SVGA_REG_FENCE_GOAL);
  78. else
  79. return vmw_fifo_mem_read(vmw, SVGA_FIFO_FENCE_GOAL);
  80. }
  81. static void vmw_fence_goal_write(struct vmw_private *vmw, u32 value)
  82. {
  83. if ((vmw->capabilities2 & SVGA_CAP2_EXTRA_REGS) != 0)
  84. vmw_write(vmw, SVGA_REG_FENCE_GOAL, value);
  85. else
  86. vmw_fifo_mem_write(vmw, SVGA_FIFO_FENCE_GOAL, value);
  87. }
  88. /*
  89. * Note on fencing subsystem usage of irqs:
  90. * Typically the vmw_fences_update function is called
  91. *
  92. * a) When a new fence seqno has been submitted by the fifo code.
  93. * b) On-demand when we have waiters. Sleeping waiters will switch on the
  94. * ANY_FENCE irq and call vmw_fences_update function each time an ANY_FENCE
  95. * irq is received. When the last fence waiter is gone, that IRQ is masked
  96. * away.
  97. *
  98. * In situations where there are no waiters and we don't submit any new fences,
  99. * fence objects may not be signaled. This is perfectly OK, since there are
  100. * no consumers of the signaled data, but that is NOT ok when there are fence
  101. * actions attached to a fence. The fencing subsystem then makes use of the
  102. * FENCE_GOAL irq and sets the fence goal seqno to that of the next fence
  103. * which has an action attached, and each time vmw_fences_update is called,
  104. * the subsystem makes sure the fence goal seqno is updated.
  105. *
  106. * The fence goal seqno irq is on as long as there are unsignaled fence
  107. * objects with actions attached to them.
  108. */
  109. static void vmw_fence_obj_destroy(struct dma_fence *f)
  110. {
  111. struct vmw_fence_obj *fence =
  112. container_of(f, struct vmw_fence_obj, base);
  113. struct vmw_fence_manager *fman = fman_from_fence(fence);
  114. if (!list_empty(&fence->head)) {
  115. spin_lock(&fman->lock);
  116. list_del_init(&fence->head);
  117. spin_unlock(&fman->lock);
  118. }
  119. fence->destroy(fence);
  120. }
  121. static const char *vmw_fence_get_driver_name(struct dma_fence *f)
  122. {
  123. return "vmwgfx";
  124. }
  125. static const char *vmw_fence_get_timeline_name(struct dma_fence *f)
  126. {
  127. return "svga";
  128. }
  129. static bool vmw_fence_enable_signaling(struct dma_fence *f)
  130. {
  131. struct vmw_fence_obj *fence =
  132. container_of(f, struct vmw_fence_obj, base);
  133. struct vmw_fence_manager *fman = fman_from_fence(fence);
  134. struct vmw_private *dev_priv = fman->dev_priv;
  135. u32 seqno = vmw_fence_read(dev_priv);
  136. if (seqno - fence->base.seqno < VMW_FENCE_WRAP)
  137. return false;
  138. return true;
  139. }
  140. struct vmwgfx_wait_cb {
  141. struct dma_fence_cb base;
  142. struct task_struct *task;
  143. };
  144. static void
  145. vmwgfx_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
  146. {
  147. struct vmwgfx_wait_cb *wait =
  148. container_of(cb, struct vmwgfx_wait_cb, base);
  149. wake_up_process(wait->task);
  150. }
  151. static void __vmw_fences_update(struct vmw_fence_manager *fman);
  152. static long vmw_fence_wait(struct dma_fence *f, bool intr, signed long timeout)
  153. {
  154. struct vmw_fence_obj *fence =
  155. container_of(f, struct vmw_fence_obj, base);
  156. struct vmw_fence_manager *fman = fman_from_fence(fence);
  157. struct vmw_private *dev_priv = fman->dev_priv;
  158. struct vmwgfx_wait_cb cb;
  159. long ret = timeout;
  160. if (likely(vmw_fence_obj_signaled(fence)))
  161. return timeout;
  162. vmw_seqno_waiter_add(dev_priv);
  163. spin_lock(f->lock);
  164. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &f->flags))
  165. goto out;
  166. if (intr && signal_pending(current)) {
  167. ret = -ERESTARTSYS;
  168. goto out;
  169. }
  170. cb.base.func = vmwgfx_wait_cb;
  171. cb.task = current;
  172. list_add(&cb.base.node, &f->cb_list);
  173. for (;;) {
  174. __vmw_fences_update(fman);
  175. /*
  176. * We can use the barrier free __set_current_state() since
  177. * DMA_FENCE_FLAG_SIGNALED_BIT + wakeup is protected by the
  178. * fence spinlock.
  179. */
  180. if (intr)
  181. __set_current_state(TASK_INTERRUPTIBLE);
  182. else
  183. __set_current_state(TASK_UNINTERRUPTIBLE);
  184. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &f->flags)) {
  185. if (ret == 0 && timeout > 0)
  186. ret = 1;
  187. break;
  188. }
  189. if (intr && signal_pending(current)) {
  190. ret = -ERESTARTSYS;
  191. break;
  192. }
  193. if (ret == 0)
  194. break;
  195. spin_unlock(f->lock);
  196. ret = schedule_timeout(ret);
  197. spin_lock(f->lock);
  198. }
  199. __set_current_state(TASK_RUNNING);
  200. if (!list_empty(&cb.base.node))
  201. list_del(&cb.base.node);
  202. out:
  203. spin_unlock(f->lock);
  204. vmw_seqno_waiter_remove(dev_priv);
  205. return ret;
  206. }
  207. static const struct dma_fence_ops vmw_fence_ops = {
  208. .get_driver_name = vmw_fence_get_driver_name,
  209. .get_timeline_name = vmw_fence_get_timeline_name,
  210. .enable_signaling = vmw_fence_enable_signaling,
  211. .wait = vmw_fence_wait,
  212. .release = vmw_fence_obj_destroy,
  213. };
  214. /*
  215. * Execute signal actions on fences recently signaled.
  216. * This is done from a workqueue so we don't have to execute
  217. * signal actions from atomic context.
  218. */
  219. static void vmw_fence_work_func(struct work_struct *work)
  220. {
  221. struct vmw_fence_manager *fman =
  222. container_of(work, struct vmw_fence_manager, work);
  223. struct list_head list;
  224. struct vmw_fence_action *action, *next_action;
  225. bool seqno_valid;
  226. do {
  227. INIT_LIST_HEAD(&list);
  228. mutex_lock(&fman->goal_irq_mutex);
  229. spin_lock(&fman->lock);
  230. list_splice_init(&fman->cleanup_list, &list);
  231. seqno_valid = fman->seqno_valid;
  232. spin_unlock(&fman->lock);
  233. if (!seqno_valid && fman->goal_irq_on) {
  234. fman->goal_irq_on = false;
  235. vmw_goal_waiter_remove(fman->dev_priv);
  236. }
  237. mutex_unlock(&fman->goal_irq_mutex);
  238. if (list_empty(&list))
  239. return;
  240. /*
  241. * At this point, only we should be able to manipulate the
  242. * list heads of the actions we have on the private list.
  243. * hence fman::lock not held.
  244. */
  245. list_for_each_entry_safe(action, next_action, &list, head) {
  246. list_del_init(&action->head);
  247. if (action->cleanup)
  248. action->cleanup(action);
  249. }
  250. } while (1);
  251. }
  252. struct vmw_fence_manager *vmw_fence_manager_init(struct vmw_private *dev_priv)
  253. {
  254. struct vmw_fence_manager *fman = kzalloc(sizeof(*fman), GFP_KERNEL);
  255. if (unlikely(!fman))
  256. return NULL;
  257. fman->dev_priv = dev_priv;
  258. spin_lock_init(&fman->lock);
  259. INIT_LIST_HEAD(&fman->fence_list);
  260. INIT_LIST_HEAD(&fman->cleanup_list);
  261. INIT_WORK(&fman->work, &vmw_fence_work_func);
  262. fman->fifo_down = true;
  263. mutex_init(&fman->goal_irq_mutex);
  264. fman->ctx = dma_fence_context_alloc(1);
  265. return fman;
  266. }
  267. void vmw_fence_manager_takedown(struct vmw_fence_manager *fman)
  268. {
  269. bool lists_empty;
  270. (void) cancel_work_sync(&fman->work);
  271. spin_lock(&fman->lock);
  272. lists_empty = list_empty(&fman->fence_list) &&
  273. list_empty(&fman->cleanup_list);
  274. spin_unlock(&fman->lock);
  275. BUG_ON(!lists_empty);
  276. kfree(fman);
  277. }
  278. static int vmw_fence_obj_init(struct vmw_fence_manager *fman,
  279. struct vmw_fence_obj *fence, u32 seqno,
  280. void (*destroy) (struct vmw_fence_obj *fence))
  281. {
  282. int ret = 0;
  283. dma_fence_init(&fence->base, &vmw_fence_ops, &fman->lock,
  284. fman->ctx, seqno);
  285. INIT_LIST_HEAD(&fence->seq_passed_actions);
  286. fence->destroy = destroy;
  287. spin_lock(&fman->lock);
  288. if (unlikely(fman->fifo_down)) {
  289. ret = -EBUSY;
  290. goto out_unlock;
  291. }
  292. list_add_tail(&fence->head, &fman->fence_list);
  293. out_unlock:
  294. spin_unlock(&fman->lock);
  295. return ret;
  296. }
  297. static void vmw_fences_perform_actions(struct vmw_fence_manager *fman,
  298. struct list_head *list)
  299. {
  300. struct vmw_fence_action *action, *next_action;
  301. list_for_each_entry_safe(action, next_action, list, head) {
  302. list_del_init(&action->head);
  303. fman->pending_actions[action->type]--;
  304. if (action->seq_passed != NULL)
  305. action->seq_passed(action);
  306. /*
  307. * Add the cleanup action to the cleanup list so that
  308. * it will be performed by a worker task.
  309. */
  310. list_add_tail(&action->head, &fman->cleanup_list);
  311. }
  312. }
  313. /**
  314. * vmw_fence_goal_new_locked - Figure out a new device fence goal
  315. * seqno if needed.
  316. *
  317. * @fman: Pointer to a fence manager.
  318. * @passed_seqno: The seqno the device currently signals as passed.
  319. *
  320. * This function should be called with the fence manager lock held.
  321. * It is typically called when we have a new passed_seqno, and
  322. * we might need to update the fence goal. It checks to see whether
  323. * the current fence goal has already passed, and, in that case,
  324. * scans through all unsignaled fences to get the next fence object with an
  325. * action attached, and sets the seqno of that fence as a new fence goal.
  326. *
  327. * returns true if the device goal seqno was updated. False otherwise.
  328. */
  329. static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman,
  330. u32 passed_seqno)
  331. {
  332. u32 goal_seqno;
  333. struct vmw_fence_obj *fence, *next_fence;
  334. if (likely(!fman->seqno_valid))
  335. return false;
  336. goal_seqno = vmw_fence_goal_read(fman->dev_priv);
  337. if (likely(passed_seqno - goal_seqno >= VMW_FENCE_WRAP))
  338. return false;
  339. fman->seqno_valid = false;
  340. list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) {
  341. if (!list_empty(&fence->seq_passed_actions)) {
  342. fman->seqno_valid = true;
  343. vmw_fence_goal_write(fman->dev_priv,
  344. fence->base.seqno);
  345. break;
  346. }
  347. }
  348. return true;
  349. }
  350. /**
  351. * vmw_fence_goal_check_locked - Replace the device fence goal seqno if
  352. * needed.
  353. *
  354. * @fence: Pointer to a struct vmw_fence_obj the seqno of which should be
  355. * considered as a device fence goal.
  356. *
  357. * This function should be called with the fence manager lock held.
  358. * It is typically called when an action has been attached to a fence to
  359. * check whether the seqno of that fence should be used for a fence
  360. * goal interrupt. This is typically needed if the current fence goal is
  361. * invalid, or has a higher seqno than that of the current fence object.
  362. *
  363. * returns true if the device goal seqno was updated. False otherwise.
  364. */
  365. static bool vmw_fence_goal_check_locked(struct vmw_fence_obj *fence)
  366. {
  367. struct vmw_fence_manager *fman = fman_from_fence(fence);
  368. u32 goal_seqno;
  369. if (dma_fence_is_signaled_locked(&fence->base))
  370. return false;
  371. goal_seqno = vmw_fence_goal_read(fman->dev_priv);
  372. if (likely(fman->seqno_valid &&
  373. goal_seqno - fence->base.seqno < VMW_FENCE_WRAP))
  374. return false;
  375. vmw_fence_goal_write(fman->dev_priv, fence->base.seqno);
  376. fman->seqno_valid = true;
  377. return true;
  378. }
  379. static void __vmw_fences_update(struct vmw_fence_manager *fman)
  380. {
  381. struct vmw_fence_obj *fence, *next_fence;
  382. struct list_head action_list;
  383. bool needs_rerun;
  384. uint32_t seqno, new_seqno;
  385. seqno = vmw_fence_read(fman->dev_priv);
  386. rerun:
  387. list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) {
  388. if (seqno - fence->base.seqno < VMW_FENCE_WRAP) {
  389. list_del_init(&fence->head);
  390. dma_fence_signal_locked(&fence->base);
  391. INIT_LIST_HEAD(&action_list);
  392. list_splice_init(&fence->seq_passed_actions,
  393. &action_list);
  394. vmw_fences_perform_actions(fman, &action_list);
  395. } else
  396. break;
  397. }
  398. /*
  399. * Rerun if the fence goal seqno was updated, and the
  400. * hardware might have raced with that update, so that
  401. * we missed a fence_goal irq.
  402. */
  403. needs_rerun = vmw_fence_goal_new_locked(fman, seqno);
  404. if (unlikely(needs_rerun)) {
  405. new_seqno = vmw_fence_read(fman->dev_priv);
  406. if (new_seqno != seqno) {
  407. seqno = new_seqno;
  408. goto rerun;
  409. }
  410. }
  411. if (!list_empty(&fman->cleanup_list))
  412. (void) schedule_work(&fman->work);
  413. }
  414. void vmw_fences_update(struct vmw_fence_manager *fman)
  415. {
  416. spin_lock(&fman->lock);
  417. __vmw_fences_update(fman);
  418. spin_unlock(&fman->lock);
  419. }
  420. bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence)
  421. {
  422. struct vmw_fence_manager *fman = fman_from_fence(fence);
  423. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
  424. return true;
  425. vmw_fences_update(fman);
  426. return dma_fence_is_signaled(&fence->base);
  427. }
  428. int vmw_fence_obj_wait(struct vmw_fence_obj *fence, bool lazy,
  429. bool interruptible, unsigned long timeout)
  430. {
  431. long ret = dma_fence_wait_timeout(&fence->base, interruptible, timeout);
  432. if (likely(ret > 0))
  433. return 0;
  434. else if (ret == 0)
  435. return -EBUSY;
  436. else
  437. return ret;
  438. }
  439. static void vmw_fence_destroy(struct vmw_fence_obj *fence)
  440. {
  441. dma_fence_free(&fence->base);
  442. }
  443. int vmw_fence_create(struct vmw_fence_manager *fman,
  444. uint32_t seqno,
  445. struct vmw_fence_obj **p_fence)
  446. {
  447. struct vmw_fence_obj *fence;
  448. int ret;
  449. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  450. if (unlikely(!fence))
  451. return -ENOMEM;
  452. ret = vmw_fence_obj_init(fman, fence, seqno,
  453. vmw_fence_destroy);
  454. if (unlikely(ret != 0))
  455. goto out_err_init;
  456. *p_fence = fence;
  457. return 0;
  458. out_err_init:
  459. kfree(fence);
  460. return ret;
  461. }
  462. static void vmw_user_fence_destroy(struct vmw_fence_obj *fence)
  463. {
  464. struct vmw_user_fence *ufence =
  465. container_of(fence, struct vmw_user_fence, fence);
  466. ttm_base_object_kfree(ufence, base);
  467. }
  468. static void vmw_user_fence_base_release(struct ttm_base_object **p_base)
  469. {
  470. struct ttm_base_object *base = *p_base;
  471. struct vmw_user_fence *ufence =
  472. container_of(base, struct vmw_user_fence, base);
  473. struct vmw_fence_obj *fence = &ufence->fence;
  474. *p_base = NULL;
  475. vmw_fence_obj_unreference(&fence);
  476. }
  477. int vmw_user_fence_create(struct drm_file *file_priv,
  478. struct vmw_fence_manager *fman,
  479. uint32_t seqno,
  480. struct vmw_fence_obj **p_fence,
  481. uint32_t *p_handle)
  482. {
  483. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  484. struct vmw_user_fence *ufence;
  485. struct vmw_fence_obj *tmp;
  486. int ret;
  487. ufence = kzalloc(sizeof(*ufence), GFP_KERNEL);
  488. if (unlikely(!ufence)) {
  489. ret = -ENOMEM;
  490. goto out_no_object;
  491. }
  492. ret = vmw_fence_obj_init(fman, &ufence->fence, seqno,
  493. vmw_user_fence_destroy);
  494. if (unlikely(ret != 0)) {
  495. kfree(ufence);
  496. goto out_no_object;
  497. }
  498. /*
  499. * The base object holds a reference which is freed in
  500. * vmw_user_fence_base_release.
  501. */
  502. tmp = vmw_fence_obj_reference(&ufence->fence);
  503. ret = ttm_base_object_init(tfile, &ufence->base, false,
  504. VMW_RES_FENCE,
  505. &vmw_user_fence_base_release);
  506. if (unlikely(ret != 0)) {
  507. /*
  508. * Free the base object's reference
  509. */
  510. vmw_fence_obj_unreference(&tmp);
  511. goto out_err;
  512. }
  513. *p_fence = &ufence->fence;
  514. *p_handle = ufence->base.handle;
  515. return 0;
  516. out_err:
  517. tmp = &ufence->fence;
  518. vmw_fence_obj_unreference(&tmp);
  519. out_no_object:
  520. return ret;
  521. }
  522. /*
  523. * vmw_fence_fifo_down - signal all unsignaled fence objects.
  524. */
  525. void vmw_fence_fifo_down(struct vmw_fence_manager *fman)
  526. {
  527. struct list_head action_list;
  528. int ret;
  529. /*
  530. * The list may be altered while we traverse it, so always
  531. * restart when we've released the fman->lock.
  532. */
  533. spin_lock(&fman->lock);
  534. fman->fifo_down = true;
  535. while (!list_empty(&fman->fence_list)) {
  536. struct vmw_fence_obj *fence =
  537. list_entry(fman->fence_list.prev, struct vmw_fence_obj,
  538. head);
  539. dma_fence_get(&fence->base);
  540. spin_unlock(&fman->lock);
  541. ret = vmw_fence_obj_wait(fence, false, false,
  542. VMW_FENCE_WAIT_TIMEOUT);
  543. if (unlikely(ret != 0)) {
  544. list_del_init(&fence->head);
  545. dma_fence_signal(&fence->base);
  546. INIT_LIST_HEAD(&action_list);
  547. list_splice_init(&fence->seq_passed_actions,
  548. &action_list);
  549. vmw_fences_perform_actions(fman, &action_list);
  550. }
  551. BUG_ON(!list_empty(&fence->head));
  552. dma_fence_put(&fence->base);
  553. spin_lock(&fman->lock);
  554. }
  555. spin_unlock(&fman->lock);
  556. }
  557. void vmw_fence_fifo_up(struct vmw_fence_manager *fman)
  558. {
  559. spin_lock(&fman->lock);
  560. fman->fifo_down = false;
  561. spin_unlock(&fman->lock);
  562. }
  563. /**
  564. * vmw_fence_obj_lookup - Look up a user-space fence object
  565. *
  566. * @tfile: A struct ttm_object_file identifying the caller.
  567. * @handle: A handle identifying the fence object.
  568. * @return: A struct vmw_user_fence base ttm object on success or
  569. * an error pointer on failure.
  570. *
  571. * The fence object is looked up and type-checked. The caller needs
  572. * to have opened the fence object first, but since that happens on
  573. * creation and fence objects aren't shareable, that's not an
  574. * issue currently.
  575. */
  576. static struct ttm_base_object *
  577. vmw_fence_obj_lookup(struct ttm_object_file *tfile, u32 handle)
  578. {
  579. struct ttm_base_object *base = ttm_base_object_lookup(tfile, handle);
  580. if (!base) {
  581. pr_err("Invalid fence object handle 0x%08lx.\n",
  582. (unsigned long)handle);
  583. return ERR_PTR(-EINVAL);
  584. }
  585. if (base->refcount_release != vmw_user_fence_base_release) {
  586. pr_err("Invalid fence object handle 0x%08lx.\n",
  587. (unsigned long)handle);
  588. ttm_base_object_unref(&base);
  589. return ERR_PTR(-EINVAL);
  590. }
  591. return base;
  592. }
  593. int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data,
  594. struct drm_file *file_priv)
  595. {
  596. struct drm_vmw_fence_wait_arg *arg =
  597. (struct drm_vmw_fence_wait_arg *)data;
  598. unsigned long timeout;
  599. struct ttm_base_object *base;
  600. struct vmw_fence_obj *fence;
  601. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  602. int ret;
  603. uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ);
  604. /*
  605. * 64-bit division not present on 32-bit systems, so do an
  606. * approximation. (Divide by 1000000).
  607. */
  608. wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) -
  609. (wait_timeout >> 26);
  610. if (!arg->cookie_valid) {
  611. arg->cookie_valid = 1;
  612. arg->kernel_cookie = jiffies + wait_timeout;
  613. }
  614. base = vmw_fence_obj_lookup(tfile, arg->handle);
  615. if (IS_ERR(base))
  616. return PTR_ERR(base);
  617. fence = &(container_of(base, struct vmw_user_fence, base)->fence);
  618. timeout = jiffies;
  619. if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) {
  620. ret = ((vmw_fence_obj_signaled(fence)) ?
  621. 0 : -EBUSY);
  622. goto out;
  623. }
  624. timeout = (unsigned long)arg->kernel_cookie - timeout;
  625. ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout);
  626. out:
  627. ttm_base_object_unref(&base);
  628. /*
  629. * Optionally unref the fence object.
  630. */
  631. if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF))
  632. return ttm_ref_object_base_unref(tfile, arg->handle);
  633. return ret;
  634. }
  635. int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data,
  636. struct drm_file *file_priv)
  637. {
  638. struct drm_vmw_fence_signaled_arg *arg =
  639. (struct drm_vmw_fence_signaled_arg *) data;
  640. struct ttm_base_object *base;
  641. struct vmw_fence_obj *fence;
  642. struct vmw_fence_manager *fman;
  643. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  644. struct vmw_private *dev_priv = vmw_priv(dev);
  645. base = vmw_fence_obj_lookup(tfile, arg->handle);
  646. if (IS_ERR(base))
  647. return PTR_ERR(base);
  648. fence = &(container_of(base, struct vmw_user_fence, base)->fence);
  649. fman = fman_from_fence(fence);
  650. arg->signaled = vmw_fence_obj_signaled(fence);
  651. arg->signaled_flags = arg->flags;
  652. spin_lock(&fman->lock);
  653. arg->passed_seqno = dev_priv->last_read_seqno;
  654. spin_unlock(&fman->lock);
  655. ttm_base_object_unref(&base);
  656. return 0;
  657. }
  658. int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data,
  659. struct drm_file *file_priv)
  660. {
  661. struct drm_vmw_fence_arg *arg =
  662. (struct drm_vmw_fence_arg *) data;
  663. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  664. arg->handle);
  665. }
  666. /**
  667. * vmw_event_fence_action_seq_passed
  668. *
  669. * @action: The struct vmw_fence_action embedded in a struct
  670. * vmw_event_fence_action.
  671. *
  672. * This function is called when the seqno of the fence where @action is
  673. * attached has passed. It queues the event on the submitter's event list.
  674. * This function is always called from atomic context.
  675. */
  676. static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
  677. {
  678. struct vmw_event_fence_action *eaction =
  679. container_of(action, struct vmw_event_fence_action, action);
  680. struct drm_device *dev = eaction->dev;
  681. struct drm_pending_event *event = eaction->event;
  682. if (unlikely(event == NULL))
  683. return;
  684. spin_lock_irq(&dev->event_lock);
  685. if (likely(eaction->tv_sec != NULL)) {
  686. struct timespec64 ts;
  687. ktime_get_ts64(&ts);
  688. /* monotonic time, so no y2038 overflow */
  689. *eaction->tv_sec = ts.tv_sec;
  690. *eaction->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  691. }
  692. drm_send_event_locked(dev, eaction->event);
  693. eaction->event = NULL;
  694. spin_unlock_irq(&dev->event_lock);
  695. }
  696. /**
  697. * vmw_event_fence_action_cleanup
  698. *
  699. * @action: The struct vmw_fence_action embedded in a struct
  700. * vmw_event_fence_action.
  701. *
  702. * This function is the struct vmw_fence_action destructor. It's typically
  703. * called from a workqueue.
  704. */
  705. static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action)
  706. {
  707. struct vmw_event_fence_action *eaction =
  708. container_of(action, struct vmw_event_fence_action, action);
  709. vmw_fence_obj_unreference(&eaction->fence);
  710. kfree(eaction);
  711. }
  712. /**
  713. * vmw_fence_obj_add_action - Add an action to a fence object.
  714. *
  715. * @fence: The fence object.
  716. * @action: The action to add.
  717. *
  718. * Note that the action callbacks may be executed before this function
  719. * returns.
  720. */
  721. static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
  722. struct vmw_fence_action *action)
  723. {
  724. struct vmw_fence_manager *fman = fman_from_fence(fence);
  725. bool run_update = false;
  726. mutex_lock(&fman->goal_irq_mutex);
  727. spin_lock(&fman->lock);
  728. fman->pending_actions[action->type]++;
  729. if (dma_fence_is_signaled_locked(&fence->base)) {
  730. struct list_head action_list;
  731. INIT_LIST_HEAD(&action_list);
  732. list_add_tail(&action->head, &action_list);
  733. vmw_fences_perform_actions(fman, &action_list);
  734. } else {
  735. list_add_tail(&action->head, &fence->seq_passed_actions);
  736. /*
  737. * This function may set fman::seqno_valid, so it must
  738. * be run with the goal_irq_mutex held.
  739. */
  740. run_update = vmw_fence_goal_check_locked(fence);
  741. }
  742. spin_unlock(&fman->lock);
  743. if (run_update) {
  744. if (!fman->goal_irq_on) {
  745. fman->goal_irq_on = true;
  746. vmw_goal_waiter_add(fman->dev_priv);
  747. }
  748. vmw_fences_update(fman);
  749. }
  750. mutex_unlock(&fman->goal_irq_mutex);
  751. }
  752. /**
  753. * vmw_event_fence_action_queue - Post an event for sending when a fence
  754. * object seqno has passed.
  755. *
  756. * @file_priv: The file connection on which the event should be posted.
  757. * @fence: The fence object on which to post the event.
  758. * @event: Event to be posted. This event should've been alloced
  759. * using k[mz]alloc, and should've been completely initialized.
  760. * @tv_sec: If non-null, the variable pointed to will be assigned
  761. * current time tv_sec val when the fence signals.
  762. * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
  763. * be assigned the current time tv_usec val when the fence signals.
  764. * @interruptible: Interruptible waits if possible.
  765. *
  766. * As a side effect, the object pointed to by @event may have been
  767. * freed when this function returns. If this function returns with
  768. * an error code, the caller needs to free that object.
  769. */
  770. int vmw_event_fence_action_queue(struct drm_file *file_priv,
  771. struct vmw_fence_obj *fence,
  772. struct drm_pending_event *event,
  773. uint32_t *tv_sec,
  774. uint32_t *tv_usec,
  775. bool interruptible)
  776. {
  777. struct vmw_event_fence_action *eaction;
  778. struct vmw_fence_manager *fman = fman_from_fence(fence);
  779. eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
  780. if (unlikely(!eaction))
  781. return -ENOMEM;
  782. eaction->event = event;
  783. eaction->action.seq_passed = vmw_event_fence_action_seq_passed;
  784. eaction->action.cleanup = vmw_event_fence_action_cleanup;
  785. eaction->action.type = VMW_ACTION_EVENT;
  786. eaction->fence = vmw_fence_obj_reference(fence);
  787. eaction->dev = &fman->dev_priv->drm;
  788. eaction->tv_sec = tv_sec;
  789. eaction->tv_usec = tv_usec;
  790. vmw_fence_obj_add_action(fence, &eaction->action);
  791. return 0;
  792. }
  793. struct vmw_event_fence_pending {
  794. struct drm_pending_event base;
  795. struct drm_vmw_event_fence event;
  796. };
  797. static int vmw_event_fence_action_create(struct drm_file *file_priv,
  798. struct vmw_fence_obj *fence,
  799. uint32_t flags,
  800. uint64_t user_data,
  801. bool interruptible)
  802. {
  803. struct vmw_event_fence_pending *event;
  804. struct vmw_fence_manager *fman = fman_from_fence(fence);
  805. struct drm_device *dev = &fman->dev_priv->drm;
  806. int ret;
  807. event = kzalloc(sizeof(*event), GFP_KERNEL);
  808. if (unlikely(!event)) {
  809. DRM_ERROR("Failed to allocate an event.\n");
  810. ret = -ENOMEM;
  811. goto out_no_space;
  812. }
  813. event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED;
  814. event->event.base.length = sizeof(event->event);
  815. event->event.user_data = user_data;
  816. ret = drm_event_reserve_init(dev, file_priv, &event->base, &event->event.base);
  817. if (unlikely(ret != 0)) {
  818. DRM_ERROR("Failed to allocate event space for this file.\n");
  819. kfree(event);
  820. goto out_no_space;
  821. }
  822. if (flags & DRM_VMW_FE_FLAG_REQ_TIME)
  823. ret = vmw_event_fence_action_queue(file_priv, fence,
  824. &event->base,
  825. &event->event.tv_sec,
  826. &event->event.tv_usec,
  827. interruptible);
  828. else
  829. ret = vmw_event_fence_action_queue(file_priv, fence,
  830. &event->base,
  831. NULL,
  832. NULL,
  833. interruptible);
  834. if (ret != 0)
  835. goto out_no_queue;
  836. return 0;
  837. out_no_queue:
  838. drm_event_cancel_free(dev, &event->base);
  839. out_no_space:
  840. return ret;
  841. }
  842. int vmw_fence_event_ioctl(struct drm_device *dev, void *data,
  843. struct drm_file *file_priv)
  844. {
  845. struct vmw_private *dev_priv = vmw_priv(dev);
  846. struct drm_vmw_fence_event_arg *arg =
  847. (struct drm_vmw_fence_event_arg *) data;
  848. struct vmw_fence_obj *fence = NULL;
  849. struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
  850. struct ttm_object_file *tfile = vmw_fp->tfile;
  851. struct drm_vmw_fence_rep __user *user_fence_rep =
  852. (struct drm_vmw_fence_rep __user *)(unsigned long)
  853. arg->fence_rep;
  854. uint32_t handle;
  855. int ret;
  856. /*
  857. * Look up an existing fence object,
  858. * and if user-space wants a new reference,
  859. * add one.
  860. */
  861. if (arg->handle) {
  862. struct ttm_base_object *base =
  863. vmw_fence_obj_lookup(tfile, arg->handle);
  864. if (IS_ERR(base))
  865. return PTR_ERR(base);
  866. fence = &(container_of(base, struct vmw_user_fence,
  867. base)->fence);
  868. (void) vmw_fence_obj_reference(fence);
  869. if (user_fence_rep != NULL) {
  870. ret = ttm_ref_object_add(vmw_fp->tfile, base,
  871. NULL, false);
  872. if (unlikely(ret != 0)) {
  873. DRM_ERROR("Failed to reference a fence "
  874. "object.\n");
  875. goto out_no_ref_obj;
  876. }
  877. handle = base->handle;
  878. }
  879. ttm_base_object_unref(&base);
  880. }
  881. /*
  882. * Create a new fence object.
  883. */
  884. if (!fence) {
  885. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  886. &fence,
  887. (user_fence_rep) ?
  888. &handle : NULL);
  889. if (unlikely(ret != 0)) {
  890. DRM_ERROR("Fence event failed to create fence.\n");
  891. return ret;
  892. }
  893. }
  894. BUG_ON(fence == NULL);
  895. ret = vmw_event_fence_action_create(file_priv, fence,
  896. arg->flags,
  897. arg->user_data,
  898. true);
  899. if (unlikely(ret != 0)) {
  900. if (ret != -ERESTARTSYS)
  901. DRM_ERROR("Failed to attach event to fence.\n");
  902. goto out_no_create;
  903. }
  904. vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence,
  905. handle, -1);
  906. vmw_fence_obj_unreference(&fence);
  907. return 0;
  908. out_no_create:
  909. if (user_fence_rep != NULL)
  910. ttm_ref_object_base_unref(tfile, handle);
  911. out_no_ref_obj:
  912. vmw_fence_obj_unreference(&fence);
  913. return ret;
  914. }