vmwgfx_fence.c 32 KB

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