vmwgfx_fence.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2011-2014 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. int num_fence_objects;
  32. struct vmw_private *dev_priv;
  33. spinlock_t lock;
  34. struct list_head fence_list;
  35. struct work_struct work;
  36. bool fifo_down;
  37. struct list_head cleanup_list;
  38. uint32_t pending_actions[VMW_ACTION_MAX];
  39. struct mutex goal_irq_mutex;
  40. bool goal_irq_on; /* Protected by @goal_irq_mutex */
  41. bool seqno_valid; /* Protected by @lock, and may not be set to true
  42. without the @goal_irq_mutex held. */
  43. u64 ctx;
  44. };
  45. struct vmw_user_fence {
  46. struct ttm_base_object base;
  47. struct vmw_fence_obj fence;
  48. };
  49. /**
  50. * struct vmw_event_fence_action - fence action that delivers a drm event.
  51. *
  52. * @action: A struct vmw_fence_action to hook up to a fence.
  53. * @event: A pointer to the pending event.
  54. * @fence: A referenced pointer to the fence to keep it alive while @action
  55. * hangs on it.
  56. * @dev: Pointer to a struct drm_device so we can access the event stuff.
  57. * @tv_sec: If non-null, the variable pointed to will be assigned
  58. * current time tv_sec val when the fence signals.
  59. * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
  60. * be assigned the current time tv_usec val when the fence signals.
  61. */
  62. struct vmw_event_fence_action {
  63. struct vmw_fence_action action;
  64. struct drm_pending_event *event;
  65. struct vmw_fence_obj *fence;
  66. struct drm_device *dev;
  67. uint32_t *tv_sec;
  68. uint32_t *tv_usec;
  69. };
  70. static struct vmw_fence_manager *
  71. fman_from_fence(struct vmw_fence_obj *fence)
  72. {
  73. return container_of(fence->base.lock, struct vmw_fence_manager, lock);
  74. }
  75. static u32 vmw_fence_goal_read(struct vmw_private *vmw)
  76. {
  77. if ((vmw->capabilities2 & SVGA_CAP2_EXTRA_REGS) != 0)
  78. return vmw_read(vmw, SVGA_REG_FENCE_GOAL);
  79. else
  80. return vmw_fifo_mem_read(vmw, SVGA_FIFO_FENCE_GOAL);
  81. }
  82. static void vmw_fence_goal_write(struct vmw_private *vmw, u32 value)
  83. {
  84. if ((vmw->capabilities2 & SVGA_CAP2_EXTRA_REGS) != 0)
  85. vmw_write(vmw, SVGA_REG_FENCE_GOAL, value);
  86. else
  87. vmw_fifo_mem_write(vmw, SVGA_FIFO_FENCE_GOAL, value);
  88. }
  89. /*
  90. * Note on fencing subsystem usage of irqs:
  91. * Typically the vmw_fences_update function is called
  92. *
  93. * a) When a new fence seqno has been submitted by the fifo code.
  94. * b) On-demand when we have waiters. Sleeping waiters will switch on the
  95. * ANY_FENCE irq and call vmw_fences_update function each time an ANY_FENCE
  96. * irq is received. When the last fence waiter is gone, that IRQ is masked
  97. * away.
  98. *
  99. * In situations where there are no waiters and we don't submit any new fences,
  100. * fence objects may not be signaled. This is perfectly OK, since there are
  101. * no consumers of the signaled data, but that is NOT ok when there are fence
  102. * actions attached to a fence. The fencing subsystem then makes use of the
  103. * FENCE_GOAL irq and sets the fence goal seqno to that of the next fence
  104. * which has an action attached, and each time vmw_fences_update is called,
  105. * the subsystem makes sure the fence goal seqno is updated.
  106. *
  107. * The fence goal seqno irq is on as long as there are unsignaled fence
  108. * objects with actions attached to them.
  109. */
  110. static void vmw_fence_obj_destroy(struct dma_fence *f)
  111. {
  112. struct vmw_fence_obj *fence =
  113. container_of(f, struct vmw_fence_obj, base);
  114. struct vmw_fence_manager *fman = fman_from_fence(fence);
  115. spin_lock(&fman->lock);
  116. list_del_init(&fence->head);
  117. --fman->num_fence_objects;
  118. spin_unlock(&fman->lock);
  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. ++fman->num_fence_objects;
  294. out_unlock:
  295. spin_unlock(&fman->lock);
  296. return ret;
  297. }
  298. static void vmw_fences_perform_actions(struct vmw_fence_manager *fman,
  299. struct list_head *list)
  300. {
  301. struct vmw_fence_action *action, *next_action;
  302. list_for_each_entry_safe(action, next_action, list, head) {
  303. list_del_init(&action->head);
  304. fman->pending_actions[action->type]--;
  305. if (action->seq_passed != NULL)
  306. action->seq_passed(action);
  307. /*
  308. * Add the cleanup action to the cleanup list so that
  309. * it will be performed by a worker task.
  310. */
  311. list_add_tail(&action->head, &fman->cleanup_list);
  312. }
  313. }
  314. /**
  315. * vmw_fence_goal_new_locked - Figure out a new device fence goal
  316. * seqno if needed.
  317. *
  318. * @fman: Pointer to a fence manager.
  319. * @passed_seqno: The seqno the device currently signals as passed.
  320. *
  321. * This function should be called with the fence manager lock held.
  322. * It is typically called when we have a new passed_seqno, and
  323. * we might need to update the fence goal. It checks to see whether
  324. * the current fence goal has already passed, and, in that case,
  325. * scans through all unsignaled fences to get the next fence object with an
  326. * action attached, and sets the seqno of that fence as a new fence goal.
  327. *
  328. * returns true if the device goal seqno was updated. False otherwise.
  329. */
  330. static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman,
  331. u32 passed_seqno)
  332. {
  333. u32 goal_seqno;
  334. struct vmw_fence_obj *fence;
  335. if (likely(!fman->seqno_valid))
  336. return false;
  337. goal_seqno = vmw_fence_goal_read(fman->dev_priv);
  338. if (likely(passed_seqno - goal_seqno >= VMW_FENCE_WRAP))
  339. return false;
  340. fman->seqno_valid = false;
  341. list_for_each_entry(fence, &fman->fence_list, head) {
  342. if (!list_empty(&fence->seq_passed_actions)) {
  343. fman->seqno_valid = true;
  344. vmw_fence_goal_write(fman->dev_priv,
  345. fence->base.seqno);
  346. break;
  347. }
  348. }
  349. return true;
  350. }
  351. /**
  352. * vmw_fence_goal_check_locked - Replace the device fence goal seqno if
  353. * needed.
  354. *
  355. * @fence: Pointer to a struct vmw_fence_obj the seqno of which should be
  356. * considered as a device fence goal.
  357. *
  358. * This function should be called with the fence manager lock held.
  359. * It is typically called when an action has been attached to a fence to
  360. * check whether the seqno of that fence should be used for a fence
  361. * goal interrupt. This is typically needed if the current fence goal is
  362. * invalid, or has a higher seqno than that of the current fence object.
  363. *
  364. * returns true if the device goal seqno was updated. False otherwise.
  365. */
  366. static bool vmw_fence_goal_check_locked(struct vmw_fence_obj *fence)
  367. {
  368. struct vmw_fence_manager *fman = fman_from_fence(fence);
  369. u32 goal_seqno;
  370. if (dma_fence_is_signaled_locked(&fence->base))
  371. return false;
  372. goal_seqno = vmw_fence_goal_read(fman->dev_priv);
  373. if (likely(fman->seqno_valid &&
  374. goal_seqno - fence->base.seqno < VMW_FENCE_WRAP))
  375. return false;
  376. vmw_fence_goal_write(fman->dev_priv, fence->base.seqno);
  377. fman->seqno_valid = true;
  378. return true;
  379. }
  380. static void __vmw_fences_update(struct vmw_fence_manager *fman)
  381. {
  382. struct vmw_fence_obj *fence, *next_fence;
  383. struct list_head action_list;
  384. bool needs_rerun;
  385. uint32_t seqno, new_seqno;
  386. seqno = vmw_fence_read(fman->dev_priv);
  387. rerun:
  388. list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) {
  389. if (seqno - fence->base.seqno < VMW_FENCE_WRAP) {
  390. list_del_init(&fence->head);
  391. dma_fence_signal_locked(&fence->base);
  392. INIT_LIST_HEAD(&action_list);
  393. list_splice_init(&fence->seq_passed_actions,
  394. &action_list);
  395. vmw_fences_perform_actions(fman, &action_list);
  396. } else
  397. break;
  398. }
  399. /*
  400. * Rerun if the fence goal seqno was updated, and the
  401. * hardware might have raced with that update, so that
  402. * we missed a fence_goal irq.
  403. */
  404. needs_rerun = vmw_fence_goal_new_locked(fman, seqno);
  405. if (unlikely(needs_rerun)) {
  406. new_seqno = vmw_fence_read(fman->dev_priv);
  407. if (new_seqno != seqno) {
  408. seqno = new_seqno;
  409. goto rerun;
  410. }
  411. }
  412. if (!list_empty(&fman->cleanup_list))
  413. (void) schedule_work(&fman->work);
  414. }
  415. void vmw_fences_update(struct vmw_fence_manager *fman)
  416. {
  417. spin_lock(&fman->lock);
  418. __vmw_fences_update(fman);
  419. spin_unlock(&fman->lock);
  420. }
  421. bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence)
  422. {
  423. struct vmw_fence_manager *fman = fman_from_fence(fence);
  424. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
  425. return true;
  426. vmw_fences_update(fman);
  427. return dma_fence_is_signaled(&fence->base);
  428. }
  429. int vmw_fence_obj_wait(struct vmw_fence_obj *fence, bool lazy,
  430. bool interruptible, unsigned long timeout)
  431. {
  432. long ret = dma_fence_wait_timeout(&fence->base, interruptible, timeout);
  433. if (likely(ret > 0))
  434. return 0;
  435. else if (ret == 0)
  436. return -EBUSY;
  437. else
  438. return ret;
  439. }
  440. static void vmw_fence_destroy(struct vmw_fence_obj *fence)
  441. {
  442. dma_fence_free(&fence->base);
  443. }
  444. int vmw_fence_create(struct vmw_fence_manager *fman,
  445. uint32_t seqno,
  446. struct vmw_fence_obj **p_fence)
  447. {
  448. struct vmw_fence_obj *fence;
  449. int ret;
  450. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  451. if (unlikely(!fence))
  452. return -ENOMEM;
  453. ret = vmw_fence_obj_init(fman, fence, seqno,
  454. vmw_fence_destroy);
  455. if (unlikely(ret != 0))
  456. goto out_err_init;
  457. *p_fence = fence;
  458. return 0;
  459. out_err_init:
  460. kfree(fence);
  461. return ret;
  462. }
  463. static void vmw_user_fence_destroy(struct vmw_fence_obj *fence)
  464. {
  465. struct vmw_user_fence *ufence =
  466. container_of(fence, struct vmw_user_fence, fence);
  467. ttm_base_object_kfree(ufence, base);
  468. }
  469. static void vmw_user_fence_base_release(struct ttm_base_object **p_base)
  470. {
  471. struct ttm_base_object *base = *p_base;
  472. struct vmw_user_fence *ufence =
  473. container_of(base, struct vmw_user_fence, base);
  474. struct vmw_fence_obj *fence = &ufence->fence;
  475. *p_base = NULL;
  476. vmw_fence_obj_unreference(&fence);
  477. }
  478. int vmw_user_fence_create(struct drm_file *file_priv,
  479. struct vmw_fence_manager *fman,
  480. uint32_t seqno,
  481. struct vmw_fence_obj **p_fence,
  482. uint32_t *p_handle)
  483. {
  484. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  485. struct vmw_user_fence *ufence;
  486. struct vmw_fence_obj *tmp;
  487. int ret;
  488. ufence = kzalloc(sizeof(*ufence), GFP_KERNEL);
  489. if (unlikely(!ufence)) {
  490. ret = -ENOMEM;
  491. goto out_no_object;
  492. }
  493. ret = vmw_fence_obj_init(fman, &ufence->fence, seqno,
  494. vmw_user_fence_destroy);
  495. if (unlikely(ret != 0)) {
  496. kfree(ufence);
  497. goto out_no_object;
  498. }
  499. /*
  500. * The base object holds a reference which is freed in
  501. * vmw_user_fence_base_release.
  502. */
  503. tmp = vmw_fence_obj_reference(&ufence->fence);
  504. ret = ttm_base_object_init(tfile, &ufence->base, false,
  505. VMW_RES_FENCE,
  506. &vmw_user_fence_base_release);
  507. if (unlikely(ret != 0)) {
  508. /*
  509. * Free the base object's reference
  510. */
  511. vmw_fence_obj_unreference(&tmp);
  512. goto out_err;
  513. }
  514. *p_fence = &ufence->fence;
  515. *p_handle = ufence->base.handle;
  516. return 0;
  517. out_err:
  518. tmp = &ufence->fence;
  519. vmw_fence_obj_unreference(&tmp);
  520. out_no_object:
  521. return ret;
  522. }
  523. /*
  524. * vmw_fence_fifo_down - signal all unsignaled fence objects.
  525. */
  526. void vmw_fence_fifo_down(struct vmw_fence_manager *fman)
  527. {
  528. struct list_head action_list;
  529. int ret;
  530. /*
  531. * The list may be altered while we traverse it, so always
  532. * restart when we've released the fman->lock.
  533. */
  534. spin_lock(&fman->lock);
  535. fman->fifo_down = true;
  536. while (!list_empty(&fman->fence_list)) {
  537. struct vmw_fence_obj *fence =
  538. list_entry(fman->fence_list.prev, struct vmw_fence_obj,
  539. head);
  540. dma_fence_get(&fence->base);
  541. spin_unlock(&fman->lock);
  542. ret = vmw_fence_obj_wait(fence, false, false,
  543. VMW_FENCE_WAIT_TIMEOUT);
  544. if (unlikely(ret != 0)) {
  545. list_del_init(&fence->head);
  546. dma_fence_signal(&fence->base);
  547. INIT_LIST_HEAD(&action_list);
  548. list_splice_init(&fence->seq_passed_actions,
  549. &action_list);
  550. vmw_fences_perform_actions(fman, &action_list);
  551. }
  552. BUG_ON(!list_empty(&fence->head));
  553. dma_fence_put(&fence->base);
  554. spin_lock(&fman->lock);
  555. }
  556. spin_unlock(&fman->lock);
  557. }
  558. void vmw_fence_fifo_up(struct vmw_fence_manager *fman)
  559. {
  560. spin_lock(&fman->lock);
  561. fman->fifo_down = false;
  562. spin_unlock(&fman->lock);
  563. }
  564. /**
  565. * vmw_fence_obj_lookup - Look up a user-space fence object
  566. *
  567. * @tfile: A struct ttm_object_file identifying the caller.
  568. * @handle: A handle identifying the fence object.
  569. * @return: A struct vmw_user_fence base ttm object on success or
  570. * an error pointer on failure.
  571. *
  572. * The fence object is looked up and type-checked. The caller needs
  573. * to have opened the fence object first, but since that happens on
  574. * creation and fence objects aren't shareable, that's not an
  575. * issue currently.
  576. */
  577. static struct ttm_base_object *
  578. vmw_fence_obj_lookup(struct ttm_object_file *tfile, u32 handle)
  579. {
  580. struct ttm_base_object *base = ttm_base_object_lookup(tfile, handle);
  581. if (!base) {
  582. pr_err("Invalid fence object handle 0x%08lx.\n",
  583. (unsigned long)handle);
  584. return ERR_PTR(-EINVAL);
  585. }
  586. if (base->refcount_release != vmw_user_fence_base_release) {
  587. pr_err("Invalid fence object handle 0x%08lx.\n",
  588. (unsigned long)handle);
  589. ttm_base_object_unref(&base);
  590. return ERR_PTR(-EINVAL);
  591. }
  592. return base;
  593. }
  594. int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data,
  595. struct drm_file *file_priv)
  596. {
  597. struct drm_vmw_fence_wait_arg *arg =
  598. (struct drm_vmw_fence_wait_arg *)data;
  599. unsigned long timeout;
  600. struct ttm_base_object *base;
  601. struct vmw_fence_obj *fence;
  602. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  603. int ret;
  604. uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ);
  605. /*
  606. * 64-bit division not present on 32-bit systems, so do an
  607. * approximation. (Divide by 1000000).
  608. */
  609. wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) -
  610. (wait_timeout >> 26);
  611. if (!arg->cookie_valid) {
  612. arg->cookie_valid = 1;
  613. arg->kernel_cookie = jiffies + wait_timeout;
  614. }
  615. base = vmw_fence_obj_lookup(tfile, arg->handle);
  616. if (IS_ERR(base))
  617. return PTR_ERR(base);
  618. fence = &(container_of(base, struct vmw_user_fence, base)->fence);
  619. timeout = jiffies;
  620. if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) {
  621. ret = ((vmw_fence_obj_signaled(fence)) ?
  622. 0 : -EBUSY);
  623. goto out;
  624. }
  625. timeout = (unsigned long)arg->kernel_cookie - timeout;
  626. ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout);
  627. out:
  628. ttm_base_object_unref(&base);
  629. /*
  630. * Optionally unref the fence object.
  631. */
  632. if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF))
  633. return ttm_ref_object_base_unref(tfile, arg->handle);
  634. return ret;
  635. }
  636. int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data,
  637. struct drm_file *file_priv)
  638. {
  639. struct drm_vmw_fence_signaled_arg *arg =
  640. (struct drm_vmw_fence_signaled_arg *) data;
  641. struct ttm_base_object *base;
  642. struct vmw_fence_obj *fence;
  643. struct vmw_fence_manager *fman;
  644. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  645. struct vmw_private *dev_priv = vmw_priv(dev);
  646. base = vmw_fence_obj_lookup(tfile, arg->handle);
  647. if (IS_ERR(base))
  648. return PTR_ERR(base);
  649. fence = &(container_of(base, struct vmw_user_fence, base)->fence);
  650. fman = fman_from_fence(fence);
  651. arg->signaled = vmw_fence_obj_signaled(fence);
  652. arg->signaled_flags = arg->flags;
  653. spin_lock(&fman->lock);
  654. arg->passed_seqno = dev_priv->last_read_seqno;
  655. spin_unlock(&fman->lock);
  656. ttm_base_object_unref(&base);
  657. return 0;
  658. }
  659. int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data,
  660. struct drm_file *file_priv)
  661. {
  662. struct drm_vmw_fence_arg *arg =
  663. (struct drm_vmw_fence_arg *) data;
  664. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  665. arg->handle);
  666. }
  667. /**
  668. * vmw_event_fence_action_seq_passed
  669. *
  670. * @action: The struct vmw_fence_action embedded in a struct
  671. * vmw_event_fence_action.
  672. *
  673. * This function is called when the seqno of the fence where @action is
  674. * attached has passed. It queues the event on the submitter's event list.
  675. * This function is always called from atomic context.
  676. */
  677. static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
  678. {
  679. struct vmw_event_fence_action *eaction =
  680. container_of(action, struct vmw_event_fence_action, action);
  681. struct drm_device *dev = eaction->dev;
  682. struct drm_pending_event *event = eaction->event;
  683. if (unlikely(event == NULL))
  684. return;
  685. spin_lock_irq(&dev->event_lock);
  686. if (likely(eaction->tv_sec != NULL)) {
  687. struct timespec64 ts;
  688. ktime_get_ts64(&ts);
  689. /* monotonic time, so no y2038 overflow */
  690. *eaction->tv_sec = ts.tv_sec;
  691. *eaction->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  692. }
  693. drm_send_event_locked(dev, eaction->event);
  694. eaction->event = NULL;
  695. spin_unlock_irq(&dev->event_lock);
  696. }
  697. /**
  698. * vmw_event_fence_action_cleanup
  699. *
  700. * @action: The struct vmw_fence_action embedded in a struct
  701. * vmw_event_fence_action.
  702. *
  703. * This function is the struct vmw_fence_action destructor. It's typically
  704. * called from a workqueue.
  705. */
  706. static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action)
  707. {
  708. struct vmw_event_fence_action *eaction =
  709. container_of(action, struct vmw_event_fence_action, action);
  710. vmw_fence_obj_unreference(&eaction->fence);
  711. kfree(eaction);
  712. }
  713. /**
  714. * vmw_fence_obj_add_action - Add an action to a fence object.
  715. *
  716. * @fence: The fence object.
  717. * @action: The action to add.
  718. *
  719. * Note that the action callbacks may be executed before this function
  720. * returns.
  721. */
  722. static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
  723. struct vmw_fence_action *action)
  724. {
  725. struct vmw_fence_manager *fman = fman_from_fence(fence);
  726. bool run_update = false;
  727. mutex_lock(&fman->goal_irq_mutex);
  728. spin_lock(&fman->lock);
  729. fman->pending_actions[action->type]++;
  730. if (dma_fence_is_signaled_locked(&fence->base)) {
  731. struct list_head action_list;
  732. INIT_LIST_HEAD(&action_list);
  733. list_add_tail(&action->head, &action_list);
  734. vmw_fences_perform_actions(fman, &action_list);
  735. } else {
  736. list_add_tail(&action->head, &fence->seq_passed_actions);
  737. /*
  738. * This function may set fman::seqno_valid, so it must
  739. * be run with the goal_irq_mutex held.
  740. */
  741. run_update = vmw_fence_goal_check_locked(fence);
  742. }
  743. spin_unlock(&fman->lock);
  744. if (run_update) {
  745. if (!fman->goal_irq_on) {
  746. fman->goal_irq_on = true;
  747. vmw_goal_waiter_add(fman->dev_priv);
  748. }
  749. vmw_fences_update(fman);
  750. }
  751. mutex_unlock(&fman->goal_irq_mutex);
  752. }
  753. /**
  754. * vmw_event_fence_action_queue - Post an event for sending when a fence
  755. * object seqno has passed.
  756. *
  757. * @file_priv: The file connection on which the event should be posted.
  758. * @fence: The fence object on which to post the event.
  759. * @event: Event to be posted. This event should've been alloced
  760. * using k[mz]alloc, and should've been completely initialized.
  761. * @tv_sec: If non-null, the variable pointed to will be assigned
  762. * current time tv_sec val when the fence signals.
  763. * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will
  764. * be assigned the current time tv_usec val when the fence signals.
  765. * @interruptible: Interruptible waits if possible.
  766. *
  767. * As a side effect, the object pointed to by @event may have been
  768. * freed when this function returns. If this function returns with
  769. * an error code, the caller needs to free that object.
  770. */
  771. int vmw_event_fence_action_queue(struct drm_file *file_priv,
  772. struct vmw_fence_obj *fence,
  773. struct drm_pending_event *event,
  774. uint32_t *tv_sec,
  775. uint32_t *tv_usec,
  776. bool interruptible)
  777. {
  778. struct vmw_event_fence_action *eaction;
  779. struct vmw_fence_manager *fman = fman_from_fence(fence);
  780. eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
  781. if (unlikely(!eaction))
  782. return -ENOMEM;
  783. eaction->event = event;
  784. eaction->action.seq_passed = vmw_event_fence_action_seq_passed;
  785. eaction->action.cleanup = vmw_event_fence_action_cleanup;
  786. eaction->action.type = VMW_ACTION_EVENT;
  787. eaction->fence = vmw_fence_obj_reference(fence);
  788. eaction->dev = &fman->dev_priv->drm;
  789. eaction->tv_sec = tv_sec;
  790. eaction->tv_usec = tv_usec;
  791. vmw_fence_obj_add_action(fence, &eaction->action);
  792. return 0;
  793. }
  794. struct vmw_event_fence_pending {
  795. struct drm_pending_event base;
  796. struct drm_vmw_event_fence event;
  797. };
  798. static int vmw_event_fence_action_create(struct drm_file *file_priv,
  799. struct vmw_fence_obj *fence,
  800. uint32_t flags,
  801. uint64_t user_data,
  802. bool interruptible)
  803. {
  804. struct vmw_event_fence_pending *event;
  805. struct vmw_fence_manager *fman = fman_from_fence(fence);
  806. struct drm_device *dev = &fman->dev_priv->drm;
  807. int ret;
  808. event = kzalloc(sizeof(*event), GFP_KERNEL);
  809. if (unlikely(!event)) {
  810. DRM_ERROR("Failed to allocate an event.\n");
  811. ret = -ENOMEM;
  812. goto out_no_space;
  813. }
  814. event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED;
  815. event->event.base.length = sizeof(*event);
  816. event->event.user_data = user_data;
  817. ret = drm_event_reserve_init(dev, file_priv, &event->base, &event->event.base);
  818. if (unlikely(ret != 0)) {
  819. DRM_ERROR("Failed to allocate event space for this file.\n");
  820. kfree(event);
  821. goto out_no_space;
  822. }
  823. if (flags & DRM_VMW_FE_FLAG_REQ_TIME)
  824. ret = vmw_event_fence_action_queue(file_priv, fence,
  825. &event->base,
  826. &event->event.tv_sec,
  827. &event->event.tv_usec,
  828. interruptible);
  829. else
  830. ret = vmw_event_fence_action_queue(file_priv, fence,
  831. &event->base,
  832. NULL,
  833. NULL,
  834. interruptible);
  835. if (ret != 0)
  836. goto out_no_queue;
  837. return 0;
  838. out_no_queue:
  839. drm_event_cancel_free(dev, &event->base);
  840. out_no_space:
  841. return ret;
  842. }
  843. int vmw_fence_event_ioctl(struct drm_device *dev, void *data,
  844. struct drm_file *file_priv)
  845. {
  846. struct vmw_private *dev_priv = vmw_priv(dev);
  847. struct drm_vmw_fence_event_arg *arg =
  848. (struct drm_vmw_fence_event_arg *) data;
  849. struct vmw_fence_obj *fence = NULL;
  850. struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
  851. struct ttm_object_file *tfile = vmw_fp->tfile;
  852. struct drm_vmw_fence_rep __user *user_fence_rep =
  853. (struct drm_vmw_fence_rep __user *)(unsigned long)
  854. arg->fence_rep;
  855. uint32_t handle;
  856. int ret;
  857. /*
  858. * Look up an existing fence object,
  859. * and if user-space wants a new reference,
  860. * add one.
  861. */
  862. if (arg->handle) {
  863. struct ttm_base_object *base =
  864. vmw_fence_obj_lookup(tfile, arg->handle);
  865. if (IS_ERR(base))
  866. return PTR_ERR(base);
  867. fence = &(container_of(base, struct vmw_user_fence,
  868. base)->fence);
  869. (void) vmw_fence_obj_reference(fence);
  870. if (user_fence_rep != NULL) {
  871. ret = ttm_ref_object_add(vmw_fp->tfile, base,
  872. NULL, false);
  873. if (unlikely(ret != 0)) {
  874. DRM_ERROR("Failed to reference a fence "
  875. "object.\n");
  876. goto out_no_ref_obj;
  877. }
  878. handle = base->handle;
  879. }
  880. ttm_base_object_unref(&base);
  881. }
  882. /*
  883. * Create a new fence object.
  884. */
  885. if (!fence) {
  886. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  887. &fence,
  888. (user_fence_rep) ?
  889. &handle : NULL);
  890. if (unlikely(ret != 0)) {
  891. DRM_ERROR("Fence event failed to create fence.\n");
  892. return ret;
  893. }
  894. }
  895. BUG_ON(fence == NULL);
  896. ret = vmw_event_fence_action_create(file_priv, fence,
  897. arg->flags,
  898. arg->user_data,
  899. true);
  900. if (unlikely(ret != 0)) {
  901. if (ret != -ERESTARTSYS)
  902. DRM_ERROR("Failed to attach event to fence.\n");
  903. goto out_no_create;
  904. }
  905. vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence,
  906. handle, -1);
  907. vmw_fence_obj_unreference(&fence);
  908. return 0;
  909. out_no_create:
  910. if (user_fence_rep != NULL)
  911. ttm_ref_object_base_unref(tfile, handle);
  912. out_no_ref_obj:
  913. vmw_fence_obj_unreference(&fence);
  914. return ret;
  915. }