vmwgfx_fence.c 29 KB

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