vmwgfx_fifo.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 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 "vmwgfx_drv.h"
  28. #include <drm/drmP.h>
  29. #include <drm/ttm/ttm_placement.h>
  30. bool vmw_fifo_have_3d(struct vmw_private *dev_priv)
  31. {
  32. u32 __iomem *fifo_mem = dev_priv->mmio_virt;
  33. uint32_t fifo_min, hwversion;
  34. const struct vmw_fifo_state *fifo = &dev_priv->fifo;
  35. if (!(dev_priv->capabilities & SVGA_CAP_3D))
  36. return false;
  37. if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS) {
  38. uint32_t result;
  39. if (!dev_priv->has_mob)
  40. return false;
  41. spin_lock(&dev_priv->cap_lock);
  42. vmw_write(dev_priv, SVGA_REG_DEV_CAP, SVGA3D_DEVCAP_3D);
  43. result = vmw_read(dev_priv, SVGA_REG_DEV_CAP);
  44. spin_unlock(&dev_priv->cap_lock);
  45. return (result != 0);
  46. }
  47. if (!(dev_priv->capabilities & SVGA_CAP_EXTENDED_FIFO))
  48. return false;
  49. fifo_min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  50. if (fifo_min <= SVGA_FIFO_3D_HWVERSION * sizeof(unsigned int))
  51. return false;
  52. hwversion = ioread32(fifo_mem +
  53. ((fifo->capabilities &
  54. SVGA_FIFO_CAP_3D_HWVERSION_REVISED) ?
  55. SVGA_FIFO_3D_HWVERSION_REVISED :
  56. SVGA_FIFO_3D_HWVERSION));
  57. if (hwversion == 0)
  58. return false;
  59. if (hwversion < SVGA3D_HWVERSION_WS8_B1)
  60. return false;
  61. /* Legacy Display Unit does not support surfaces */
  62. if (dev_priv->active_display_unit == vmw_du_legacy)
  63. return false;
  64. return true;
  65. }
  66. bool vmw_fifo_have_pitchlock(struct vmw_private *dev_priv)
  67. {
  68. u32 __iomem *fifo_mem = dev_priv->mmio_virt;
  69. uint32_t caps;
  70. if (!(dev_priv->capabilities & SVGA_CAP_EXTENDED_FIFO))
  71. return false;
  72. caps = ioread32(fifo_mem + SVGA_FIFO_CAPABILITIES);
  73. if (caps & SVGA_FIFO_CAP_PITCHLOCK)
  74. return true;
  75. return false;
  76. }
  77. int vmw_fifo_init(struct vmw_private *dev_priv, struct vmw_fifo_state *fifo)
  78. {
  79. u32 __iomem *fifo_mem = dev_priv->mmio_virt;
  80. uint32_t max;
  81. uint32_t min;
  82. fifo->static_buffer_size = VMWGFX_FIFO_STATIC_SIZE;
  83. fifo->static_buffer = vmalloc(fifo->static_buffer_size);
  84. if (unlikely(fifo->static_buffer == NULL))
  85. return -ENOMEM;
  86. fifo->dynamic_buffer = NULL;
  87. fifo->reserved_size = 0;
  88. fifo->using_bounce_buffer = false;
  89. mutex_init(&fifo->fifo_mutex);
  90. init_rwsem(&fifo->rwsem);
  91. DRM_INFO("width %d\n", vmw_read(dev_priv, SVGA_REG_WIDTH));
  92. DRM_INFO("height %d\n", vmw_read(dev_priv, SVGA_REG_HEIGHT));
  93. DRM_INFO("bpp %d\n", vmw_read(dev_priv, SVGA_REG_BITS_PER_PIXEL));
  94. dev_priv->enable_state = vmw_read(dev_priv, SVGA_REG_ENABLE);
  95. dev_priv->config_done_state = vmw_read(dev_priv, SVGA_REG_CONFIG_DONE);
  96. dev_priv->traces_state = vmw_read(dev_priv, SVGA_REG_TRACES);
  97. vmw_write(dev_priv, SVGA_REG_ENABLE, SVGA_REG_ENABLE_ENABLE_HIDE);
  98. vmw_write(dev_priv, SVGA_REG_TRACES, 0);
  99. min = 4;
  100. if (dev_priv->capabilities & SVGA_CAP_EXTENDED_FIFO)
  101. min = vmw_read(dev_priv, SVGA_REG_MEM_REGS);
  102. min <<= 2;
  103. if (min < PAGE_SIZE)
  104. min = PAGE_SIZE;
  105. iowrite32(min, fifo_mem + SVGA_FIFO_MIN);
  106. iowrite32(dev_priv->mmio_size, fifo_mem + SVGA_FIFO_MAX);
  107. wmb();
  108. iowrite32(min, fifo_mem + SVGA_FIFO_NEXT_CMD);
  109. iowrite32(min, fifo_mem + SVGA_FIFO_STOP);
  110. iowrite32(0, fifo_mem + SVGA_FIFO_BUSY);
  111. mb();
  112. vmw_write(dev_priv, SVGA_REG_CONFIG_DONE, 1);
  113. max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  114. min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  115. fifo->capabilities = ioread32(fifo_mem + SVGA_FIFO_CAPABILITIES);
  116. DRM_INFO("Fifo max 0x%08x min 0x%08x cap 0x%08x\n",
  117. (unsigned int) max,
  118. (unsigned int) min,
  119. (unsigned int) fifo->capabilities);
  120. atomic_set(&dev_priv->marker_seq, dev_priv->last_read_seqno);
  121. iowrite32(dev_priv->last_read_seqno, fifo_mem + SVGA_FIFO_FENCE);
  122. vmw_marker_queue_init(&fifo->marker_queue);
  123. return 0;
  124. }
  125. void vmw_fifo_ping_host(struct vmw_private *dev_priv, uint32_t reason)
  126. {
  127. u32 __iomem *fifo_mem = dev_priv->mmio_virt;
  128. static DEFINE_SPINLOCK(ping_lock);
  129. unsigned long irq_flags;
  130. /*
  131. * The ping_lock is needed because we don't have an atomic
  132. * test-and-set of the SVGA_FIFO_BUSY register.
  133. */
  134. spin_lock_irqsave(&ping_lock, irq_flags);
  135. if (unlikely(ioread32(fifo_mem + SVGA_FIFO_BUSY) == 0)) {
  136. iowrite32(1, fifo_mem + SVGA_FIFO_BUSY);
  137. vmw_write(dev_priv, SVGA_REG_SYNC, reason);
  138. }
  139. spin_unlock_irqrestore(&ping_lock, irq_flags);
  140. }
  141. void vmw_fifo_release(struct vmw_private *dev_priv, struct vmw_fifo_state *fifo)
  142. {
  143. u32 __iomem *fifo_mem = dev_priv->mmio_virt;
  144. vmw_write(dev_priv, SVGA_REG_SYNC, SVGA_SYNC_GENERIC);
  145. while (vmw_read(dev_priv, SVGA_REG_BUSY) != 0)
  146. ;
  147. dev_priv->last_read_seqno = ioread32(fifo_mem + SVGA_FIFO_FENCE);
  148. vmw_write(dev_priv, SVGA_REG_CONFIG_DONE,
  149. dev_priv->config_done_state);
  150. vmw_write(dev_priv, SVGA_REG_ENABLE,
  151. dev_priv->enable_state);
  152. vmw_write(dev_priv, SVGA_REG_TRACES,
  153. dev_priv->traces_state);
  154. vmw_marker_queue_takedown(&fifo->marker_queue);
  155. if (likely(fifo->static_buffer != NULL)) {
  156. vfree(fifo->static_buffer);
  157. fifo->static_buffer = NULL;
  158. }
  159. if (likely(fifo->dynamic_buffer != NULL)) {
  160. vfree(fifo->dynamic_buffer);
  161. fifo->dynamic_buffer = NULL;
  162. }
  163. }
  164. static bool vmw_fifo_is_full(struct vmw_private *dev_priv, uint32_t bytes)
  165. {
  166. u32 __iomem *fifo_mem = dev_priv->mmio_virt;
  167. uint32_t max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  168. uint32_t next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
  169. uint32_t min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  170. uint32_t stop = ioread32(fifo_mem + SVGA_FIFO_STOP);
  171. return ((max - next_cmd) + (stop - min) <= bytes);
  172. }
  173. static int vmw_fifo_wait_noirq(struct vmw_private *dev_priv,
  174. uint32_t bytes, bool interruptible,
  175. unsigned long timeout)
  176. {
  177. int ret = 0;
  178. unsigned long end_jiffies = jiffies + timeout;
  179. DEFINE_WAIT(__wait);
  180. DRM_INFO("Fifo wait noirq.\n");
  181. for (;;) {
  182. prepare_to_wait(&dev_priv->fifo_queue, &__wait,
  183. (interruptible) ?
  184. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  185. if (!vmw_fifo_is_full(dev_priv, bytes))
  186. break;
  187. if (time_after_eq(jiffies, end_jiffies)) {
  188. ret = -EBUSY;
  189. DRM_ERROR("SVGA device lockup.\n");
  190. break;
  191. }
  192. schedule_timeout(1);
  193. if (interruptible && signal_pending(current)) {
  194. ret = -ERESTARTSYS;
  195. break;
  196. }
  197. }
  198. finish_wait(&dev_priv->fifo_queue, &__wait);
  199. wake_up_all(&dev_priv->fifo_queue);
  200. DRM_INFO("Fifo noirq exit.\n");
  201. return ret;
  202. }
  203. static int vmw_fifo_wait(struct vmw_private *dev_priv,
  204. uint32_t bytes, bool interruptible,
  205. unsigned long timeout)
  206. {
  207. long ret = 1L;
  208. unsigned long irq_flags;
  209. if (likely(!vmw_fifo_is_full(dev_priv, bytes)))
  210. return 0;
  211. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_FIFOFULL);
  212. if (!(dev_priv->capabilities & SVGA_CAP_IRQMASK))
  213. return vmw_fifo_wait_noirq(dev_priv, bytes,
  214. interruptible, timeout);
  215. spin_lock(&dev_priv->waiter_lock);
  216. if (atomic_add_return(1, &dev_priv->fifo_queue_waiters) > 0) {
  217. spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
  218. outl(SVGA_IRQFLAG_FIFO_PROGRESS,
  219. dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  220. dev_priv->irq_mask |= SVGA_IRQFLAG_FIFO_PROGRESS;
  221. vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
  222. spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
  223. }
  224. spin_unlock(&dev_priv->waiter_lock);
  225. if (interruptible)
  226. ret = wait_event_interruptible_timeout
  227. (dev_priv->fifo_queue,
  228. !vmw_fifo_is_full(dev_priv, bytes), timeout);
  229. else
  230. ret = wait_event_timeout
  231. (dev_priv->fifo_queue,
  232. !vmw_fifo_is_full(dev_priv, bytes), timeout);
  233. if (unlikely(ret == 0))
  234. ret = -EBUSY;
  235. else if (likely(ret > 0))
  236. ret = 0;
  237. spin_lock(&dev_priv->waiter_lock);
  238. if (atomic_dec_and_test(&dev_priv->fifo_queue_waiters)) {
  239. spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
  240. dev_priv->irq_mask &= ~SVGA_IRQFLAG_FIFO_PROGRESS;
  241. vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
  242. spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
  243. }
  244. spin_unlock(&dev_priv->waiter_lock);
  245. return ret;
  246. }
  247. /**
  248. * Reserve @bytes number of bytes in the fifo.
  249. *
  250. * This function will return NULL (error) on two conditions:
  251. * If it timeouts waiting for fifo space, or if @bytes is larger than the
  252. * available fifo space.
  253. *
  254. * Returns:
  255. * Pointer to the fifo, or null on error (possible hardware hang).
  256. */
  257. static void *vmw_local_fifo_reserve(struct vmw_private *dev_priv,
  258. uint32_t bytes)
  259. {
  260. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  261. u32 __iomem *fifo_mem = dev_priv->mmio_virt;
  262. uint32_t max;
  263. uint32_t min;
  264. uint32_t next_cmd;
  265. uint32_t reserveable = fifo_state->capabilities & SVGA_FIFO_CAP_RESERVE;
  266. int ret;
  267. mutex_lock(&fifo_state->fifo_mutex);
  268. max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  269. min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  270. next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
  271. if (unlikely(bytes >= (max - min)))
  272. goto out_err;
  273. BUG_ON(fifo_state->reserved_size != 0);
  274. BUG_ON(fifo_state->dynamic_buffer != NULL);
  275. fifo_state->reserved_size = bytes;
  276. while (1) {
  277. uint32_t stop = ioread32(fifo_mem + SVGA_FIFO_STOP);
  278. bool need_bounce = false;
  279. bool reserve_in_place = false;
  280. if (next_cmd >= stop) {
  281. if (likely((next_cmd + bytes < max ||
  282. (next_cmd + bytes == max && stop > min))))
  283. reserve_in_place = true;
  284. else if (vmw_fifo_is_full(dev_priv, bytes)) {
  285. ret = vmw_fifo_wait(dev_priv, bytes,
  286. false, 3 * HZ);
  287. if (unlikely(ret != 0))
  288. goto out_err;
  289. } else
  290. need_bounce = true;
  291. } else {
  292. if (likely((next_cmd + bytes < stop)))
  293. reserve_in_place = true;
  294. else {
  295. ret = vmw_fifo_wait(dev_priv, bytes,
  296. false, 3 * HZ);
  297. if (unlikely(ret != 0))
  298. goto out_err;
  299. }
  300. }
  301. if (reserve_in_place) {
  302. if (reserveable || bytes <= sizeof(uint32_t)) {
  303. fifo_state->using_bounce_buffer = false;
  304. if (reserveable)
  305. iowrite32(bytes, fifo_mem +
  306. SVGA_FIFO_RESERVED);
  307. return (void __force *) (fifo_mem +
  308. (next_cmd >> 2));
  309. } else {
  310. need_bounce = true;
  311. }
  312. }
  313. if (need_bounce) {
  314. fifo_state->using_bounce_buffer = true;
  315. if (bytes < fifo_state->static_buffer_size)
  316. return fifo_state->static_buffer;
  317. else {
  318. fifo_state->dynamic_buffer = vmalloc(bytes);
  319. return fifo_state->dynamic_buffer;
  320. }
  321. }
  322. }
  323. out_err:
  324. fifo_state->reserved_size = 0;
  325. mutex_unlock(&fifo_state->fifo_mutex);
  326. return NULL;
  327. }
  328. void *vmw_fifo_reserve(struct vmw_private *dev_priv, uint32_t bytes)
  329. {
  330. void *ret;
  331. if (dev_priv->cman)
  332. ret = vmw_cmdbuf_reserve(dev_priv->cman, bytes,
  333. SVGA3D_INVALID_ID, false, NULL);
  334. else
  335. ret = vmw_local_fifo_reserve(dev_priv, bytes);
  336. if (IS_ERR_OR_NULL(ret)) {
  337. DRM_ERROR("Fifo reserve failure of %u bytes.\n",
  338. (unsigned) bytes);
  339. dump_stack();
  340. return NULL;
  341. }
  342. return ret;
  343. }
  344. static void vmw_fifo_res_copy(struct vmw_fifo_state *fifo_state,
  345. u32 __iomem *fifo_mem,
  346. uint32_t next_cmd,
  347. uint32_t max, uint32_t min, uint32_t bytes)
  348. {
  349. uint32_t chunk_size = max - next_cmd;
  350. uint32_t rest;
  351. uint32_t *buffer = (fifo_state->dynamic_buffer != NULL) ?
  352. fifo_state->dynamic_buffer : fifo_state->static_buffer;
  353. if (bytes < chunk_size)
  354. chunk_size = bytes;
  355. iowrite32(bytes, fifo_mem + SVGA_FIFO_RESERVED);
  356. mb();
  357. memcpy_toio(fifo_mem + (next_cmd >> 2), buffer, chunk_size);
  358. rest = bytes - chunk_size;
  359. if (rest)
  360. memcpy_toio(fifo_mem + (min >> 2), buffer + (chunk_size >> 2),
  361. rest);
  362. }
  363. static void vmw_fifo_slow_copy(struct vmw_fifo_state *fifo_state,
  364. u32 __iomem *fifo_mem,
  365. uint32_t next_cmd,
  366. uint32_t max, uint32_t min, uint32_t bytes)
  367. {
  368. uint32_t *buffer = (fifo_state->dynamic_buffer != NULL) ?
  369. fifo_state->dynamic_buffer : fifo_state->static_buffer;
  370. while (bytes > 0) {
  371. iowrite32(*buffer++, fifo_mem + (next_cmd >> 2));
  372. next_cmd += sizeof(uint32_t);
  373. if (unlikely(next_cmd == max))
  374. next_cmd = min;
  375. mb();
  376. iowrite32(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
  377. mb();
  378. bytes -= sizeof(uint32_t);
  379. }
  380. }
  381. static void vmw_local_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes)
  382. {
  383. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  384. u32 __iomem *fifo_mem = dev_priv->mmio_virt;
  385. uint32_t next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
  386. uint32_t max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  387. uint32_t min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  388. bool reserveable = fifo_state->capabilities & SVGA_FIFO_CAP_RESERVE;
  389. BUG_ON((bytes & 3) != 0);
  390. BUG_ON(bytes > fifo_state->reserved_size);
  391. fifo_state->reserved_size = 0;
  392. if (fifo_state->using_bounce_buffer) {
  393. if (reserveable)
  394. vmw_fifo_res_copy(fifo_state, fifo_mem,
  395. next_cmd, max, min, bytes);
  396. else
  397. vmw_fifo_slow_copy(fifo_state, fifo_mem,
  398. next_cmd, max, min, bytes);
  399. if (fifo_state->dynamic_buffer) {
  400. vfree(fifo_state->dynamic_buffer);
  401. fifo_state->dynamic_buffer = NULL;
  402. }
  403. }
  404. down_write(&fifo_state->rwsem);
  405. if (fifo_state->using_bounce_buffer || reserveable) {
  406. next_cmd += bytes;
  407. if (next_cmd >= max)
  408. next_cmd -= max - min;
  409. mb();
  410. iowrite32(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
  411. }
  412. if (reserveable)
  413. iowrite32(0, fifo_mem + SVGA_FIFO_RESERVED);
  414. mb();
  415. up_write(&fifo_state->rwsem);
  416. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  417. mutex_unlock(&fifo_state->fifo_mutex);
  418. }
  419. void vmw_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes)
  420. {
  421. if (dev_priv->cman)
  422. vmw_cmdbuf_commit(dev_priv->cman, bytes, NULL, false);
  423. else
  424. vmw_local_fifo_commit(dev_priv, bytes);
  425. }
  426. /**
  427. * vmw_fifo_commit_flush - Commit fifo space and flush any buffered commands.
  428. *
  429. * @dev_priv: Pointer to device private structure.
  430. * @bytes: Number of bytes to commit.
  431. */
  432. static void vmw_fifo_commit_flush(struct vmw_private *dev_priv, uint32_t bytes)
  433. {
  434. if (dev_priv->cman)
  435. vmw_cmdbuf_commit(dev_priv->cman, bytes, NULL, true);
  436. else
  437. vmw_local_fifo_commit(dev_priv, bytes);
  438. }
  439. /**
  440. * vmw_fifo_flush - Flush any buffered commands and make sure command processing
  441. * starts.
  442. *
  443. * @dev_priv: Pointer to device private structure.
  444. * @interruptible: Whether to wait interruptible if function needs to sleep.
  445. */
  446. int vmw_fifo_flush(struct vmw_private *dev_priv, bool interruptible)
  447. {
  448. might_sleep();
  449. if (dev_priv->cman)
  450. return vmw_cmdbuf_cur_flush(dev_priv->cman, interruptible);
  451. else
  452. return 0;
  453. }
  454. int vmw_fifo_send_fence(struct vmw_private *dev_priv, uint32_t *seqno)
  455. {
  456. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  457. struct svga_fifo_cmd_fence *cmd_fence;
  458. u32 *fm;
  459. int ret = 0;
  460. uint32_t bytes = sizeof(u32) + sizeof(*cmd_fence);
  461. fm = vmw_fifo_reserve(dev_priv, bytes);
  462. if (unlikely(fm == NULL)) {
  463. *seqno = atomic_read(&dev_priv->marker_seq);
  464. ret = -ENOMEM;
  465. (void)vmw_fallback_wait(dev_priv, false, true, *seqno,
  466. false, 3*HZ);
  467. goto out_err;
  468. }
  469. do {
  470. *seqno = atomic_add_return(1, &dev_priv->marker_seq);
  471. } while (*seqno == 0);
  472. if (!(fifo_state->capabilities & SVGA_FIFO_CAP_FENCE)) {
  473. /*
  474. * Don't request hardware to send a fence. The
  475. * waiting code in vmwgfx_irq.c will emulate this.
  476. */
  477. vmw_fifo_commit(dev_priv, 0);
  478. return 0;
  479. }
  480. *fm++ = SVGA_CMD_FENCE;
  481. cmd_fence = (struct svga_fifo_cmd_fence *) fm;
  482. cmd_fence->fence = *seqno;
  483. vmw_fifo_commit_flush(dev_priv, bytes);
  484. (void) vmw_marker_push(&fifo_state->marker_queue, *seqno);
  485. vmw_update_seqno(dev_priv, fifo_state);
  486. out_err:
  487. return ret;
  488. }
  489. /**
  490. * vmw_fifo_emit_dummy_legacy_query - emits a dummy query to the fifo using
  491. * legacy query commands.
  492. *
  493. * @dev_priv: The device private structure.
  494. * @cid: The hardware context id used for the query.
  495. *
  496. * See the vmw_fifo_emit_dummy_query documentation.
  497. */
  498. static int vmw_fifo_emit_dummy_legacy_query(struct vmw_private *dev_priv,
  499. uint32_t cid)
  500. {
  501. /*
  502. * A query wait without a preceding query end will
  503. * actually finish all queries for this cid
  504. * without writing to the query result structure.
  505. */
  506. struct ttm_buffer_object *bo = &dev_priv->dummy_query_bo->base;
  507. struct {
  508. SVGA3dCmdHeader header;
  509. SVGA3dCmdWaitForQuery body;
  510. } *cmd;
  511. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  512. if (unlikely(cmd == NULL)) {
  513. DRM_ERROR("Out of fifo space for dummy query.\n");
  514. return -ENOMEM;
  515. }
  516. cmd->header.id = SVGA_3D_CMD_WAIT_FOR_QUERY;
  517. cmd->header.size = sizeof(cmd->body);
  518. cmd->body.cid = cid;
  519. cmd->body.type = SVGA3D_QUERYTYPE_OCCLUSION;
  520. if (bo->mem.mem_type == TTM_PL_VRAM) {
  521. cmd->body.guestResult.gmrId = SVGA_GMR_FRAMEBUFFER;
  522. cmd->body.guestResult.offset = bo->offset;
  523. } else {
  524. cmd->body.guestResult.gmrId = bo->mem.start;
  525. cmd->body.guestResult.offset = 0;
  526. }
  527. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  528. return 0;
  529. }
  530. /**
  531. * vmw_fifo_emit_dummy_gb_query - emits a dummy query to the fifo using
  532. * guest-backed resource query commands.
  533. *
  534. * @dev_priv: The device private structure.
  535. * @cid: The hardware context id used for the query.
  536. *
  537. * See the vmw_fifo_emit_dummy_query documentation.
  538. */
  539. static int vmw_fifo_emit_dummy_gb_query(struct vmw_private *dev_priv,
  540. uint32_t cid)
  541. {
  542. /*
  543. * A query wait without a preceding query end will
  544. * actually finish all queries for this cid
  545. * without writing to the query result structure.
  546. */
  547. struct ttm_buffer_object *bo = &dev_priv->dummy_query_bo->base;
  548. struct {
  549. SVGA3dCmdHeader header;
  550. SVGA3dCmdWaitForGBQuery body;
  551. } *cmd;
  552. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  553. if (unlikely(cmd == NULL)) {
  554. DRM_ERROR("Out of fifo space for dummy query.\n");
  555. return -ENOMEM;
  556. }
  557. cmd->header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
  558. cmd->header.size = sizeof(cmd->body);
  559. cmd->body.cid = cid;
  560. cmd->body.type = SVGA3D_QUERYTYPE_OCCLUSION;
  561. BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
  562. cmd->body.mobid = bo->mem.start;
  563. cmd->body.offset = 0;
  564. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  565. return 0;
  566. }
  567. /**
  568. * vmw_fifo_emit_dummy_gb_query - emits a dummy query to the fifo using
  569. * appropriate resource query commands.
  570. *
  571. * @dev_priv: The device private structure.
  572. * @cid: The hardware context id used for the query.
  573. *
  574. * This function is used to emit a dummy occlusion query with
  575. * no primitives rendered between query begin and query end.
  576. * It's used to provide a query barrier, in order to know that when
  577. * this query is finished, all preceding queries are also finished.
  578. *
  579. * A Query results structure should have been initialized at the start
  580. * of the dev_priv->dummy_query_bo buffer object. And that buffer object
  581. * must also be either reserved or pinned when this function is called.
  582. *
  583. * Returns -ENOMEM on failure to reserve fifo space.
  584. */
  585. int vmw_fifo_emit_dummy_query(struct vmw_private *dev_priv,
  586. uint32_t cid)
  587. {
  588. if (dev_priv->has_mob)
  589. return vmw_fifo_emit_dummy_gb_query(dev_priv, cid);
  590. return vmw_fifo_emit_dummy_legacy_query(dev_priv, cid);
  591. }