vmwgfx_fence.c 31 KB

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