vmwgfx_fence.c 32 KB

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