vmwgfx_fence.c 31 KB

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