vmwgfx_fence.c 31 KB

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