vmwgfx_fence.c 30 KB

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