vmwgfx_fence.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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))
  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))
  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)) {
  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. /**
  581. * vmw_fence_obj_lookup - Look up a user-space fence object
  582. *
  583. * @tfile: A struct ttm_object_file identifying the caller.
  584. * @handle: A handle identifying the fence object.
  585. * @return: A struct vmw_user_fence base ttm object on success or
  586. * an error pointer on failure.
  587. *
  588. * The fence object is looked up and type-checked. The caller needs
  589. * to have opened the fence object first, but since that happens on
  590. * creation and fence objects aren't shareable, that's not an
  591. * issue currently.
  592. */
  593. static struct ttm_base_object *
  594. vmw_fence_obj_lookup(struct ttm_object_file *tfile, u32 handle)
  595. {
  596. struct ttm_base_object *base = ttm_base_object_lookup(tfile, handle);
  597. if (!base) {
  598. pr_err("Invalid fence object handle 0x%08lx.\n",
  599. (unsigned long)handle);
  600. return ERR_PTR(-EINVAL);
  601. }
  602. if (base->refcount_release != vmw_user_fence_base_release) {
  603. pr_err("Invalid fence object handle 0x%08lx.\n",
  604. (unsigned long)handle);
  605. ttm_base_object_unref(&base);
  606. return ERR_PTR(-EINVAL);
  607. }
  608. return base;
  609. }
  610. int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data,
  611. struct drm_file *file_priv)
  612. {
  613. struct drm_vmw_fence_wait_arg *arg =
  614. (struct drm_vmw_fence_wait_arg *)data;
  615. unsigned long timeout;
  616. struct ttm_base_object *base;
  617. struct vmw_fence_obj *fence;
  618. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  619. int ret;
  620. uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ);
  621. /*
  622. * 64-bit division not present on 32-bit systems, so do an
  623. * approximation. (Divide by 1000000).
  624. */
  625. wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) -
  626. (wait_timeout >> 26);
  627. if (!arg->cookie_valid) {
  628. arg->cookie_valid = 1;
  629. arg->kernel_cookie = jiffies + wait_timeout;
  630. }
  631. base = vmw_fence_obj_lookup(tfile, arg->handle);
  632. if (IS_ERR(base))
  633. return PTR_ERR(base);
  634. fence = &(container_of(base, struct vmw_user_fence, base)->fence);
  635. timeout = jiffies;
  636. if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) {
  637. ret = ((vmw_fence_obj_signaled(fence)) ?
  638. 0 : -EBUSY);
  639. goto out;
  640. }
  641. timeout = (unsigned long)arg->kernel_cookie - timeout;
  642. ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout);
  643. out:
  644. ttm_base_object_unref(&base);
  645. /*
  646. * Optionally unref the fence object.
  647. */
  648. if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF))
  649. return ttm_ref_object_base_unref(tfile, arg->handle,
  650. TTM_REF_USAGE);
  651. return ret;
  652. }
  653. int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data,
  654. struct drm_file *file_priv)
  655. {
  656. struct drm_vmw_fence_signaled_arg *arg =
  657. (struct drm_vmw_fence_signaled_arg *) data;
  658. struct ttm_base_object *base;
  659. struct vmw_fence_obj *fence;
  660. struct vmw_fence_manager *fman;
  661. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  662. struct vmw_private *dev_priv = vmw_priv(dev);
  663. base = vmw_fence_obj_lookup(tfile, arg->handle);
  664. if (IS_ERR(base))
  665. return PTR_ERR(base);
  666. fence = &(container_of(base, struct vmw_user_fence, base)->fence);
  667. fman = fman_from_fence(fence);
  668. arg->signaled = vmw_fence_obj_signaled(fence);
  669. arg->signaled_flags = arg->flags;
  670. spin_lock_irq(&fman->lock);
  671. arg->passed_seqno = dev_priv->last_read_seqno;
  672. spin_unlock_irq(&fman->lock);
  673. ttm_base_object_unref(&base);
  674. return 0;
  675. }
  676. int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data,
  677. struct drm_file *file_priv)
  678. {
  679. struct drm_vmw_fence_arg *arg =
  680. (struct drm_vmw_fence_arg *) data;
  681. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  682. arg->handle,
  683. TTM_REF_USAGE);
  684. }
  685. /**
  686. * vmw_event_fence_action_seq_passed
  687. *
  688. * @action: The struct vmw_fence_action embedded in a struct
  689. * vmw_event_fence_action.
  690. *
  691. * This function is called when the seqno of the fence where @action is
  692. * attached has passed. It queues the event on the submitter's event list.
  693. * This function is always called from atomic context, and may be called
  694. * from irq context.
  695. */
  696. static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
  697. {
  698. struct vmw_event_fence_action *eaction =
  699. container_of(action, struct vmw_event_fence_action, action);
  700. struct drm_device *dev = eaction->dev;
  701. struct drm_pending_event *event = eaction->event;
  702. struct drm_file *file_priv;
  703. unsigned long irq_flags;
  704. if (unlikely(event == NULL))
  705. return;
  706. file_priv = event->file_priv;
  707. spin_lock_irqsave(&dev->event_lock, irq_flags);
  708. if (likely(eaction->tv_sec != NULL)) {
  709. struct timeval tv;
  710. do_gettimeofday(&tv);
  711. *eaction->tv_sec = tv.tv_sec;
  712. *eaction->tv_usec = tv.tv_usec;
  713. }
  714. drm_send_event_locked(dev, eaction->event);
  715. eaction->event = NULL;
  716. spin_unlock_irqrestore(&dev->event_lock, irq_flags);
  717. }
  718. /**
  719. * vmw_event_fence_action_cleanup
  720. *
  721. * @action: The struct vmw_fence_action embedded in a struct
  722. * vmw_event_fence_action.
  723. *
  724. * This function is the struct vmw_fence_action destructor. It's typically
  725. * called from a workqueue.
  726. */
  727. static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action)
  728. {
  729. struct vmw_event_fence_action *eaction =
  730. container_of(action, struct vmw_event_fence_action, action);
  731. vmw_fence_obj_unreference(&eaction->fence);
  732. kfree(eaction);
  733. }
  734. /**
  735. * vmw_fence_obj_add_action - Add an action to a fence object.
  736. *
  737. * @fence - The fence object.
  738. * @action - The action to add.
  739. *
  740. * Note that the action callbacks may be executed before this function
  741. * returns.
  742. */
  743. static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
  744. struct vmw_fence_action *action)
  745. {
  746. struct vmw_fence_manager *fman = fman_from_fence(fence);
  747. unsigned long irq_flags;
  748. bool run_update = false;
  749. mutex_lock(&fman->goal_irq_mutex);
  750. spin_lock_irqsave(&fman->lock, irq_flags);
  751. fman->pending_actions[action->type]++;
  752. if (dma_fence_is_signaled_locked(&fence->base)) {
  753. struct list_head action_list;
  754. INIT_LIST_HEAD(&action_list);
  755. list_add_tail(&action->head, &action_list);
  756. vmw_fences_perform_actions(fman, &action_list);
  757. } else {
  758. list_add_tail(&action->head, &fence->seq_passed_actions);
  759. /*
  760. * This function may set fman::seqno_valid, so it must
  761. * be run with the goal_irq_mutex held.
  762. */
  763. run_update = vmw_fence_goal_check_locked(fence);
  764. }
  765. spin_unlock_irqrestore(&fman->lock, irq_flags);
  766. if (run_update) {
  767. if (!fman->goal_irq_on) {
  768. fman->goal_irq_on = true;
  769. vmw_goal_waiter_add(fman->dev_priv);
  770. }
  771. vmw_fences_update(fman);
  772. }
  773. mutex_unlock(&fman->goal_irq_mutex);
  774. }
  775. /**
  776. * vmw_event_fence_action_create - Post an event for sending when a fence
  777. * object seqno has passed.
  778. *
  779. * @file_priv: The file connection on which the event should be posted.
  780. * @fence: The fence object on which to post the event.
  781. * @event: Event to be posted. This event should've been alloced
  782. * using k[mz]alloc, and should've been completely initialized.
  783. * @interruptible: Interruptible waits if possible.
  784. *
  785. * As a side effect, the object pointed to by @event may have been
  786. * freed when this function returns. If this function returns with
  787. * an error code, the caller needs to free that object.
  788. */
  789. int vmw_event_fence_action_queue(struct drm_file *file_priv,
  790. struct vmw_fence_obj *fence,
  791. struct drm_pending_event *event,
  792. uint32_t *tv_sec,
  793. uint32_t *tv_usec,
  794. bool interruptible)
  795. {
  796. struct vmw_event_fence_action *eaction;
  797. struct vmw_fence_manager *fman = fman_from_fence(fence);
  798. eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
  799. if (unlikely(!eaction))
  800. return -ENOMEM;
  801. eaction->event = event;
  802. eaction->action.seq_passed = vmw_event_fence_action_seq_passed;
  803. eaction->action.cleanup = vmw_event_fence_action_cleanup;
  804. eaction->action.type = VMW_ACTION_EVENT;
  805. eaction->fence = vmw_fence_obj_reference(fence);
  806. eaction->dev = fman->dev_priv->dev;
  807. eaction->tv_sec = tv_sec;
  808. eaction->tv_usec = tv_usec;
  809. vmw_fence_obj_add_action(fence, &eaction->action);
  810. return 0;
  811. }
  812. struct vmw_event_fence_pending {
  813. struct drm_pending_event base;
  814. struct drm_vmw_event_fence event;
  815. };
  816. static int vmw_event_fence_action_create(struct drm_file *file_priv,
  817. struct vmw_fence_obj *fence,
  818. uint32_t flags,
  819. uint64_t user_data,
  820. bool interruptible)
  821. {
  822. struct vmw_event_fence_pending *event;
  823. struct vmw_fence_manager *fman = fman_from_fence(fence);
  824. struct drm_device *dev = fman->dev_priv->dev;
  825. int ret;
  826. event = kzalloc(sizeof(*event), GFP_KERNEL);
  827. if (unlikely(!event)) {
  828. DRM_ERROR("Failed to allocate an event.\n");
  829. ret = -ENOMEM;
  830. goto out_no_space;
  831. }
  832. event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED;
  833. event->event.base.length = sizeof(*event);
  834. event->event.user_data = user_data;
  835. ret = drm_event_reserve_init(dev, file_priv, &event->base, &event->event.base);
  836. if (unlikely(ret != 0)) {
  837. DRM_ERROR("Failed to allocate event space for this file.\n");
  838. kfree(event);
  839. goto out_no_space;
  840. }
  841. if (flags & DRM_VMW_FE_FLAG_REQ_TIME)
  842. ret = vmw_event_fence_action_queue(file_priv, fence,
  843. &event->base,
  844. &event->event.tv_sec,
  845. &event->event.tv_usec,
  846. interruptible);
  847. else
  848. ret = vmw_event_fence_action_queue(file_priv, fence,
  849. &event->base,
  850. NULL,
  851. NULL,
  852. interruptible);
  853. if (ret != 0)
  854. goto out_no_queue;
  855. return 0;
  856. out_no_queue:
  857. drm_event_cancel_free(dev, &event->base);
  858. out_no_space:
  859. return ret;
  860. }
  861. int vmw_fence_event_ioctl(struct drm_device *dev, void *data,
  862. struct drm_file *file_priv)
  863. {
  864. struct vmw_private *dev_priv = vmw_priv(dev);
  865. struct drm_vmw_fence_event_arg *arg =
  866. (struct drm_vmw_fence_event_arg *) data;
  867. struct vmw_fence_obj *fence = NULL;
  868. struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
  869. struct ttm_object_file *tfile = vmw_fp->tfile;
  870. struct drm_vmw_fence_rep __user *user_fence_rep =
  871. (struct drm_vmw_fence_rep __user *)(unsigned long)
  872. arg->fence_rep;
  873. uint32_t handle;
  874. int ret;
  875. /*
  876. * Look up an existing fence object,
  877. * and if user-space wants a new reference,
  878. * add one.
  879. */
  880. if (arg->handle) {
  881. struct ttm_base_object *base =
  882. vmw_fence_obj_lookup(tfile, arg->handle);
  883. if (IS_ERR(base))
  884. return PTR_ERR(base);
  885. fence = &(container_of(base, struct vmw_user_fence,
  886. base)->fence);
  887. (void) vmw_fence_obj_reference(fence);
  888. if (user_fence_rep != NULL) {
  889. ret = ttm_ref_object_add(vmw_fp->tfile, base,
  890. TTM_REF_USAGE, NULL, false);
  891. if (unlikely(ret != 0)) {
  892. DRM_ERROR("Failed to reference a fence "
  893. "object.\n");
  894. goto out_no_ref_obj;
  895. }
  896. handle = base->hash.key;
  897. }
  898. ttm_base_object_unref(&base);
  899. }
  900. /*
  901. * Create a new fence object.
  902. */
  903. if (!fence) {
  904. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  905. &fence,
  906. (user_fence_rep) ?
  907. &handle : NULL);
  908. if (unlikely(ret != 0)) {
  909. DRM_ERROR("Fence event failed to create fence.\n");
  910. return ret;
  911. }
  912. }
  913. BUG_ON(fence == NULL);
  914. ret = vmw_event_fence_action_create(file_priv, fence,
  915. arg->flags,
  916. arg->user_data,
  917. true);
  918. if (unlikely(ret != 0)) {
  919. if (ret != -ERESTARTSYS)
  920. DRM_ERROR("Failed to attach event to fence.\n");
  921. goto out_no_create;
  922. }
  923. vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence,
  924. handle);
  925. vmw_fence_obj_unreference(&fence);
  926. return 0;
  927. out_no_create:
  928. if (user_fence_rep != NULL)
  929. ttm_ref_object_base_unref(tfile, handle, TTM_REF_USAGE);
  930. out_no_ref_obj:
  931. vmw_fence_obj_unreference(&fence);
  932. return ret;
  933. }