vmwgfx_fence.c 31 KB

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