vmwgfx_fence.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. struct vmw_fence_manager *fman = fence->fman;
  413. kfree(fence);
  414. /*
  415. * Free kernel space accounting.
  416. */
  417. ttm_mem_global_free(vmw_mem_glob(fman->dev_priv),
  418. fman->fence_size);
  419. }
  420. int vmw_fence_create(struct vmw_fence_manager *fman,
  421. uint32_t seqno,
  422. uint32_t mask,
  423. struct vmw_fence_obj **p_fence)
  424. {
  425. struct ttm_mem_global *mem_glob = vmw_mem_glob(fman->dev_priv);
  426. struct vmw_fence_obj *fence;
  427. int ret;
  428. ret = ttm_mem_global_alloc(mem_glob, fman->fence_size,
  429. false, false);
  430. if (unlikely(ret != 0))
  431. return ret;
  432. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  433. if (unlikely(fence == NULL)) {
  434. ret = -ENOMEM;
  435. goto out_no_object;
  436. }
  437. ret = vmw_fence_obj_init(fman, fence, seqno, mask,
  438. vmw_fence_destroy);
  439. if (unlikely(ret != 0))
  440. goto out_err_init;
  441. *p_fence = fence;
  442. return 0;
  443. out_err_init:
  444. kfree(fence);
  445. out_no_object:
  446. ttm_mem_global_free(mem_glob, fman->fence_size);
  447. return ret;
  448. }
  449. static void vmw_user_fence_destroy(struct vmw_fence_obj *fence)
  450. {
  451. struct vmw_user_fence *ufence =
  452. container_of(fence, struct vmw_user_fence, fence);
  453. struct vmw_fence_manager *fman = fence->fman;
  454. kfree(ufence);
  455. /*
  456. * Free kernel space accounting.
  457. */
  458. ttm_mem_global_free(vmw_mem_glob(fman->dev_priv),
  459. fman->user_fence_size);
  460. }
  461. static void vmw_user_fence_base_release(struct ttm_base_object **p_base)
  462. {
  463. struct ttm_base_object *base = *p_base;
  464. struct vmw_user_fence *ufence =
  465. container_of(base, struct vmw_user_fence, base);
  466. struct vmw_fence_obj *fence = &ufence->fence;
  467. *p_base = NULL;
  468. vmw_fence_obj_unreference(&fence);
  469. }
  470. int vmw_user_fence_create(struct drm_file *file_priv,
  471. struct vmw_fence_manager *fman,
  472. uint32_t seqno,
  473. uint32_t mask,
  474. struct vmw_fence_obj **p_fence,
  475. uint32_t *p_handle)
  476. {
  477. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  478. struct vmw_user_fence *ufence;
  479. struct vmw_fence_obj *tmp;
  480. struct ttm_mem_global *mem_glob = vmw_mem_glob(fman->dev_priv);
  481. int ret;
  482. /*
  483. * Kernel memory space accounting, since this object may
  484. * be created by a user-space request.
  485. */
  486. ret = ttm_mem_global_alloc(mem_glob, fman->user_fence_size,
  487. false, false);
  488. if (unlikely(ret != 0))
  489. return ret;
  490. ufence = kzalloc(sizeof(*ufence), GFP_KERNEL);
  491. if (unlikely(ufence == NULL)) {
  492. ret = -ENOMEM;
  493. goto out_no_object;
  494. }
  495. ret = vmw_fence_obj_init(fman, &ufence->fence, seqno,
  496. mask, vmw_user_fence_destroy);
  497. if (unlikely(ret != 0)) {
  498. kfree(ufence);
  499. goto out_no_object;
  500. }
  501. /*
  502. * The base object holds a reference which is freed in
  503. * vmw_user_fence_base_release.
  504. */
  505. tmp = vmw_fence_obj_reference(&ufence->fence);
  506. ret = ttm_base_object_init(tfile, &ufence->base, false,
  507. VMW_RES_FENCE,
  508. &vmw_user_fence_base_release, NULL);
  509. if (unlikely(ret != 0)) {
  510. /*
  511. * Free the base object's reference
  512. */
  513. vmw_fence_obj_unreference(&tmp);
  514. goto out_err;
  515. }
  516. *p_fence = &ufence->fence;
  517. *p_handle = ufence->base.hash.key;
  518. return 0;
  519. out_err:
  520. tmp = &ufence->fence;
  521. vmw_fence_obj_unreference(&tmp);
  522. out_no_object:
  523. ttm_mem_global_free(mem_glob, fman->user_fence_size);
  524. return ret;
  525. }
  526. /**
  527. * vmw_fence_fifo_down - signal all unsignaled fence objects.
  528. */
  529. void vmw_fence_fifo_down(struct vmw_fence_manager *fman)
  530. {
  531. unsigned long irq_flags;
  532. struct list_head action_list;
  533. int ret;
  534. /*
  535. * The list may be altered while we traverse it, so always
  536. * restart when we've released the fman->lock.
  537. */
  538. spin_lock_irqsave(&fman->lock, irq_flags);
  539. fman->fifo_down = true;
  540. while (!list_empty(&fman->fence_list)) {
  541. struct vmw_fence_obj *fence =
  542. list_entry(fman->fence_list.prev, struct vmw_fence_obj,
  543. head);
  544. kref_get(&fence->kref);
  545. spin_unlock_irq(&fman->lock);
  546. ret = vmw_fence_obj_wait(fence, fence->signal_mask,
  547. false, false,
  548. VMW_FENCE_WAIT_TIMEOUT);
  549. if (unlikely(ret != 0)) {
  550. list_del_init(&fence->head);
  551. fence->signaled |= DRM_VMW_FENCE_FLAG_EXEC;
  552. INIT_LIST_HEAD(&action_list);
  553. list_splice_init(&fence->seq_passed_actions,
  554. &action_list);
  555. vmw_fences_perform_actions(fman, &action_list);
  556. wake_up_all(&fence->queue);
  557. }
  558. spin_lock_irq(&fman->lock);
  559. BUG_ON(!list_empty(&fence->head));
  560. kref_put(&fence->kref, vmw_fence_obj_destroy_locked);
  561. }
  562. spin_unlock_irqrestore(&fman->lock, irq_flags);
  563. }
  564. void vmw_fence_fifo_up(struct vmw_fence_manager *fman)
  565. {
  566. unsigned long irq_flags;
  567. spin_lock_irqsave(&fman->lock, irq_flags);
  568. fman->fifo_down = false;
  569. spin_unlock_irqrestore(&fman->lock, irq_flags);
  570. }
  571. int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data,
  572. struct drm_file *file_priv)
  573. {
  574. struct drm_vmw_fence_wait_arg *arg =
  575. (struct drm_vmw_fence_wait_arg *)data;
  576. unsigned long timeout;
  577. struct ttm_base_object *base;
  578. struct vmw_fence_obj *fence;
  579. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  580. int ret;
  581. uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ);
  582. /*
  583. * 64-bit division not present on 32-bit systems, so do an
  584. * approximation. (Divide by 1000000).
  585. */
  586. wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) -
  587. (wait_timeout >> 26);
  588. if (!arg->cookie_valid) {
  589. arg->cookie_valid = 1;
  590. arg->kernel_cookie = jiffies + wait_timeout;
  591. }
  592. base = ttm_base_object_lookup(tfile, arg->handle);
  593. if (unlikely(base == NULL)) {
  594. printk(KERN_ERR "Wait invalid fence object handle "
  595. "0x%08lx.\n",
  596. (unsigned long)arg->handle);
  597. return -EINVAL;
  598. }
  599. fence = &(container_of(base, struct vmw_user_fence, base)->fence);
  600. timeout = jiffies;
  601. if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) {
  602. ret = ((vmw_fence_obj_signaled(fence, arg->flags)) ?
  603. 0 : -EBUSY);
  604. goto out;
  605. }
  606. timeout = (unsigned long)arg->kernel_cookie - timeout;
  607. ret = vmw_fence_obj_wait(fence, arg->flags, arg->lazy, true, timeout);
  608. out:
  609. ttm_base_object_unref(&base);
  610. /*
  611. * Optionally unref the fence object.
  612. */
  613. if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF))
  614. return ttm_ref_object_base_unref(tfile, arg->handle,
  615. TTM_REF_USAGE);
  616. return ret;
  617. }
  618. int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data,
  619. struct drm_file *file_priv)
  620. {
  621. struct drm_vmw_fence_signaled_arg *arg =
  622. (struct drm_vmw_fence_signaled_arg *) data;
  623. struct ttm_base_object *base;
  624. struct vmw_fence_obj *fence;
  625. struct vmw_fence_manager *fman;
  626. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  627. struct vmw_private *dev_priv = vmw_priv(dev);
  628. base = ttm_base_object_lookup(tfile, arg->handle);
  629. if (unlikely(base == NULL)) {
  630. printk(KERN_ERR "Fence signaled invalid fence object handle "
  631. "0x%08lx.\n",
  632. (unsigned long)arg->handle);
  633. return -EINVAL;
  634. }
  635. fence = &(container_of(base, struct vmw_user_fence, base)->fence);
  636. fman = fence->fman;
  637. arg->signaled = vmw_fence_obj_signaled(fence, arg->flags);
  638. spin_lock_irq(&fman->lock);
  639. arg->signaled_flags = fence->signaled;
  640. arg->passed_seqno = dev_priv->last_read_seqno;
  641. spin_unlock_irq(&fman->lock);
  642. ttm_base_object_unref(&base);
  643. return 0;
  644. }
  645. int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data,
  646. struct drm_file *file_priv)
  647. {
  648. struct drm_vmw_fence_arg *arg =
  649. (struct drm_vmw_fence_arg *) data;
  650. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  651. arg->handle,
  652. TTM_REF_USAGE);
  653. }
  654. /**
  655. * vmw_event_fence_fpriv_gone - Remove references to struct drm_file objects
  656. *
  657. * @fman: Pointer to a struct vmw_fence_manager
  658. * @event_list: Pointer to linked list of struct vmw_event_fence_action objects
  659. * with pointers to a struct drm_file object about to be closed.
  660. *
  661. * This function removes all pending fence events with references to a
  662. * specific struct drm_file object about to be closed. The caller is required
  663. * to pass a list of all struct vmw_event_fence_action objects with such
  664. * events attached. This function is typically called before the
  665. * struct drm_file object's event management is taken down.
  666. */
  667. void vmw_event_fence_fpriv_gone(struct vmw_fence_manager *fman,
  668. struct list_head *event_list)
  669. {
  670. struct vmw_event_fence_action *eaction;
  671. struct drm_pending_event *event;
  672. unsigned long irq_flags;
  673. while (1) {
  674. spin_lock_irqsave(&fman->lock, irq_flags);
  675. if (list_empty(event_list))
  676. goto out_unlock;
  677. eaction = list_first_entry(event_list,
  678. struct vmw_event_fence_action,
  679. fpriv_head);
  680. list_del_init(&eaction->fpriv_head);
  681. event = eaction->event;
  682. eaction->event = NULL;
  683. spin_unlock_irqrestore(&fman->lock, irq_flags);
  684. event->destroy(event);
  685. }
  686. out_unlock:
  687. spin_unlock_irqrestore(&fman->lock, irq_flags);
  688. }
  689. /**
  690. * vmw_event_fence_action_seq_passed
  691. *
  692. * @action: The struct vmw_fence_action embedded in a struct
  693. * vmw_event_fence_action.
  694. *
  695. * This function is called when the seqno of the fence where @action is
  696. * attached has passed. It queues the event on the submitter's event list.
  697. * This function is always called from atomic context, and may be called
  698. * from irq context.
  699. */
  700. static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action)
  701. {
  702. struct vmw_event_fence_action *eaction =
  703. container_of(action, struct vmw_event_fence_action, action);
  704. struct drm_device *dev = eaction->dev;
  705. struct drm_pending_event *event = eaction->event;
  706. struct drm_file *file_priv;
  707. unsigned long irq_flags;
  708. if (unlikely(event == NULL))
  709. return;
  710. file_priv = event->file_priv;
  711. spin_lock_irqsave(&dev->event_lock, irq_flags);
  712. if (likely(eaction->tv_sec != NULL)) {
  713. struct timeval tv;
  714. do_gettimeofday(&tv);
  715. *eaction->tv_sec = tv.tv_sec;
  716. *eaction->tv_usec = tv.tv_usec;
  717. }
  718. list_del_init(&eaction->fpriv_head);
  719. list_add_tail(&eaction->event->link, &file_priv->event_list);
  720. eaction->event = NULL;
  721. wake_up_all(&file_priv->event_wait);
  722. spin_unlock_irqrestore(&dev->event_lock, irq_flags);
  723. }
  724. /**
  725. * vmw_event_fence_action_cleanup
  726. *
  727. * @action: The struct vmw_fence_action embedded in a struct
  728. * vmw_event_fence_action.
  729. *
  730. * This function is the struct vmw_fence_action destructor. It's typically
  731. * called from a workqueue.
  732. */
  733. static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action)
  734. {
  735. struct vmw_event_fence_action *eaction =
  736. container_of(action, struct vmw_event_fence_action, action);
  737. struct vmw_fence_manager *fman = eaction->fence->fman;
  738. unsigned long irq_flags;
  739. spin_lock_irqsave(&fman->lock, irq_flags);
  740. list_del(&eaction->fpriv_head);
  741. spin_unlock_irqrestore(&fman->lock, irq_flags);
  742. vmw_fence_obj_unreference(&eaction->fence);
  743. kfree(eaction);
  744. }
  745. /**
  746. * vmw_fence_obj_add_action - Add an action to a fence object.
  747. *
  748. * @fence - The fence object.
  749. * @action - The action to add.
  750. *
  751. * Note that the action callbacks may be executed before this function
  752. * returns.
  753. */
  754. void vmw_fence_obj_add_action(struct vmw_fence_obj *fence,
  755. struct vmw_fence_action *action)
  756. {
  757. struct vmw_fence_manager *fman = fence->fman;
  758. unsigned long irq_flags;
  759. bool run_update = false;
  760. mutex_lock(&fman->goal_irq_mutex);
  761. spin_lock_irqsave(&fman->lock, irq_flags);
  762. fman->pending_actions[action->type]++;
  763. if (fence->signaled & DRM_VMW_FENCE_FLAG_EXEC) {
  764. struct list_head action_list;
  765. INIT_LIST_HEAD(&action_list);
  766. list_add_tail(&action->head, &action_list);
  767. vmw_fences_perform_actions(fman, &action_list);
  768. } else {
  769. list_add_tail(&action->head, &fence->seq_passed_actions);
  770. /*
  771. * This function may set fman::seqno_valid, so it must
  772. * be run with the goal_irq_mutex held.
  773. */
  774. run_update = vmw_fence_goal_check_locked(fence);
  775. }
  776. spin_unlock_irqrestore(&fman->lock, irq_flags);
  777. if (run_update) {
  778. if (!fman->goal_irq_on) {
  779. fman->goal_irq_on = true;
  780. vmw_goal_waiter_add(fman->dev_priv);
  781. }
  782. vmw_fences_update(fman);
  783. }
  784. mutex_unlock(&fman->goal_irq_mutex);
  785. }
  786. /**
  787. * vmw_event_fence_action_create - Post an event for sending when a fence
  788. * object seqno has passed.
  789. *
  790. * @file_priv: The file connection on which the event should be posted.
  791. * @fence: The fence object on which to post the event.
  792. * @event: Event to be posted. This event should've been alloced
  793. * using k[mz]alloc, and should've been completely initialized.
  794. * @interruptible: Interruptible waits if possible.
  795. *
  796. * As a side effect, the object pointed to by @event may have been
  797. * freed when this function returns. If this function returns with
  798. * an error code, the caller needs to free that object.
  799. */
  800. int vmw_event_fence_action_queue(struct drm_file *file_priv,
  801. struct vmw_fence_obj *fence,
  802. struct drm_pending_event *event,
  803. uint32_t *tv_sec,
  804. uint32_t *tv_usec,
  805. bool interruptible)
  806. {
  807. struct vmw_event_fence_action *eaction;
  808. struct vmw_fence_manager *fman = fence->fman;
  809. struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
  810. unsigned long irq_flags;
  811. eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
  812. if (unlikely(eaction == NULL))
  813. return -ENOMEM;
  814. eaction->event = event;
  815. eaction->action.seq_passed = vmw_event_fence_action_seq_passed;
  816. eaction->action.cleanup = vmw_event_fence_action_cleanup;
  817. eaction->action.type = VMW_ACTION_EVENT;
  818. eaction->fence = vmw_fence_obj_reference(fence);
  819. eaction->dev = fman->dev_priv->dev;
  820. eaction->tv_sec = tv_sec;
  821. eaction->tv_usec = tv_usec;
  822. spin_lock_irqsave(&fman->lock, irq_flags);
  823. list_add_tail(&eaction->fpriv_head, &vmw_fp->fence_events);
  824. spin_unlock_irqrestore(&fman->lock, irq_flags);
  825. vmw_fence_obj_add_action(fence, &eaction->action);
  826. return 0;
  827. }
  828. struct vmw_event_fence_pending {
  829. struct drm_pending_event base;
  830. struct drm_vmw_event_fence event;
  831. };
  832. int vmw_event_fence_action_create(struct drm_file *file_priv,
  833. struct vmw_fence_obj *fence,
  834. uint32_t flags,
  835. uint64_t user_data,
  836. bool interruptible)
  837. {
  838. struct vmw_event_fence_pending *event;
  839. struct drm_device *dev = fence->fman->dev_priv->dev;
  840. unsigned long irq_flags;
  841. int ret;
  842. spin_lock_irqsave(&dev->event_lock, irq_flags);
  843. ret = (file_priv->event_space < sizeof(event->event)) ? -EBUSY : 0;
  844. if (likely(ret == 0))
  845. file_priv->event_space -= sizeof(event->event);
  846. spin_unlock_irqrestore(&dev->event_lock, irq_flags);
  847. if (unlikely(ret != 0)) {
  848. DRM_ERROR("Failed to allocate event space for this file.\n");
  849. goto out_no_space;
  850. }
  851. event = kzalloc(sizeof(event->event), GFP_KERNEL);
  852. if (unlikely(event == NULL)) {
  853. DRM_ERROR("Failed to allocate an event.\n");
  854. ret = -ENOMEM;
  855. goto out_no_event;
  856. }
  857. event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED;
  858. event->event.base.length = sizeof(*event);
  859. event->event.user_data = user_data;
  860. event->base.event = &event->event.base;
  861. event->base.file_priv = file_priv;
  862. event->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
  863. if (flags & DRM_VMW_FE_FLAG_REQ_TIME)
  864. ret = vmw_event_fence_action_queue(file_priv, fence,
  865. &event->base,
  866. &event->event.tv_sec,
  867. &event->event.tv_usec,
  868. interruptible);
  869. else
  870. ret = vmw_event_fence_action_queue(file_priv, fence,
  871. &event->base,
  872. NULL,
  873. NULL,
  874. interruptible);
  875. if (ret != 0)
  876. goto out_no_queue;
  877. out_no_queue:
  878. event->base.destroy(&event->base);
  879. out_no_event:
  880. spin_lock_irqsave(&dev->event_lock, irq_flags);
  881. file_priv->event_space += sizeof(*event);
  882. spin_unlock_irqrestore(&dev->event_lock, irq_flags);
  883. out_no_space:
  884. return ret;
  885. }
  886. int vmw_fence_event_ioctl(struct drm_device *dev, void *data,
  887. struct drm_file *file_priv)
  888. {
  889. struct vmw_private *dev_priv = vmw_priv(dev);
  890. struct drm_vmw_fence_event_arg *arg =
  891. (struct drm_vmw_fence_event_arg *) data;
  892. struct vmw_fence_obj *fence = NULL;
  893. struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
  894. struct drm_vmw_fence_rep __user *user_fence_rep =
  895. (struct drm_vmw_fence_rep __user *)(unsigned long)
  896. arg->fence_rep;
  897. uint32_t handle;
  898. int ret;
  899. /*
  900. * Look up an existing fence object,
  901. * and if user-space wants a new reference,
  902. * add one.
  903. */
  904. if (arg->handle) {
  905. struct ttm_base_object *base =
  906. ttm_base_object_lookup(vmw_fp->tfile, arg->handle);
  907. if (unlikely(base == NULL)) {
  908. DRM_ERROR("Fence event invalid fence object handle "
  909. "0x%08lx.\n",
  910. (unsigned long)arg->handle);
  911. return -EINVAL;
  912. }
  913. fence = &(container_of(base, struct vmw_user_fence,
  914. base)->fence);
  915. (void) vmw_fence_obj_reference(fence);
  916. if (user_fence_rep != NULL) {
  917. bool existed;
  918. ret = ttm_ref_object_add(vmw_fp->tfile, base,
  919. TTM_REF_USAGE, &existed);
  920. if (unlikely(ret != 0)) {
  921. DRM_ERROR("Failed to reference a fence "
  922. "object.\n");
  923. goto out_no_ref_obj;
  924. }
  925. handle = base->hash.key;
  926. }
  927. ttm_base_object_unref(&base);
  928. }
  929. /*
  930. * Create a new fence object.
  931. */
  932. if (!fence) {
  933. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  934. &fence,
  935. (user_fence_rep) ?
  936. &handle : NULL);
  937. if (unlikely(ret != 0)) {
  938. DRM_ERROR("Fence event failed to create fence.\n");
  939. return ret;
  940. }
  941. }
  942. BUG_ON(fence == NULL);
  943. if (arg->flags & DRM_VMW_FE_FLAG_REQ_TIME)
  944. ret = vmw_event_fence_action_create(file_priv, fence,
  945. arg->flags,
  946. arg->user_data,
  947. true);
  948. else
  949. ret = vmw_event_fence_action_create(file_priv, fence,
  950. arg->flags,
  951. arg->user_data,
  952. true);
  953. if (unlikely(ret != 0)) {
  954. if (ret != -ERESTARTSYS)
  955. DRM_ERROR("Failed to attach event to fence.\n");
  956. goto out_no_create;
  957. }
  958. vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence,
  959. handle);
  960. vmw_fence_obj_unreference(&fence);
  961. return 0;
  962. out_no_create:
  963. if (user_fence_rep != NULL)
  964. ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  965. handle, TTM_REF_USAGE);
  966. out_no_ref_obj:
  967. vmw_fence_obj_unreference(&fence);
  968. return ret;
  969. }