vmwgfx_fence.c 32 KB

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