vmwgfx_fence.c 30 KB

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