vmwgfx_fifo.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. __le32 __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. mutex_lock(&dev_priv->hw_mutex);
  42. vmw_write(dev_priv, SVGA_REG_DEV_CAP, SVGA3D_DEVCAP_3D);
  43. result = vmw_read(dev_priv, SVGA_REG_DEV_CAP);
  44. mutex_unlock(&dev_priv->hw_mutex);
  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. /* Non-Screen Object path does not support surfaces */
  62. if (!dev_priv->sou_priv)
  63. return false;
  64. return true;
  65. }
  66. bool vmw_fifo_have_pitchlock(struct vmw_private *dev_priv)
  67. {
  68. __le32 __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. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  80. uint32_t max;
  81. uint32_t min;
  82. uint32_t dummy;
  83. fifo->static_buffer_size = VMWGFX_FIFO_STATIC_SIZE;
  84. fifo->static_buffer = vmalloc(fifo->static_buffer_size);
  85. if (unlikely(fifo->static_buffer == NULL))
  86. return -ENOMEM;
  87. fifo->dynamic_buffer = NULL;
  88. fifo->reserved_size = 0;
  89. fifo->using_bounce_buffer = false;
  90. mutex_init(&fifo->fifo_mutex);
  91. init_rwsem(&fifo->rwsem);
  92. /*
  93. * Allow mapping the first page read-only to user-space.
  94. */
  95. DRM_INFO("width %d\n", vmw_read(dev_priv, SVGA_REG_WIDTH));
  96. DRM_INFO("height %d\n", vmw_read(dev_priv, SVGA_REG_HEIGHT));
  97. DRM_INFO("bpp %d\n", vmw_read(dev_priv, SVGA_REG_BITS_PER_PIXEL));
  98. mutex_lock(&dev_priv->hw_mutex);
  99. dev_priv->enable_state = vmw_read(dev_priv, SVGA_REG_ENABLE);
  100. dev_priv->config_done_state = vmw_read(dev_priv, SVGA_REG_CONFIG_DONE);
  101. dev_priv->traces_state = vmw_read(dev_priv, SVGA_REG_TRACES);
  102. vmw_write(dev_priv, SVGA_REG_ENABLE, 1);
  103. min = 4;
  104. if (dev_priv->capabilities & SVGA_CAP_EXTENDED_FIFO)
  105. min = vmw_read(dev_priv, SVGA_REG_MEM_REGS);
  106. min <<= 2;
  107. if (min < PAGE_SIZE)
  108. min = PAGE_SIZE;
  109. iowrite32(min, fifo_mem + SVGA_FIFO_MIN);
  110. iowrite32(dev_priv->mmio_size, fifo_mem + SVGA_FIFO_MAX);
  111. wmb();
  112. iowrite32(min, fifo_mem + SVGA_FIFO_NEXT_CMD);
  113. iowrite32(min, fifo_mem + SVGA_FIFO_STOP);
  114. iowrite32(0, fifo_mem + SVGA_FIFO_BUSY);
  115. mb();
  116. vmw_write(dev_priv, SVGA_REG_CONFIG_DONE, 1);
  117. mutex_unlock(&dev_priv->hw_mutex);
  118. max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  119. min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  120. fifo->capabilities = ioread32(fifo_mem + SVGA_FIFO_CAPABILITIES);
  121. DRM_INFO("Fifo max 0x%08x min 0x%08x cap 0x%08x\n",
  122. (unsigned int) max,
  123. (unsigned int) min,
  124. (unsigned int) fifo->capabilities);
  125. atomic_set(&dev_priv->marker_seq, dev_priv->last_read_seqno);
  126. iowrite32(dev_priv->last_read_seqno, fifo_mem + SVGA_FIFO_FENCE);
  127. vmw_marker_queue_init(&fifo->marker_queue);
  128. return vmw_fifo_send_fence(dev_priv, &dummy);
  129. }
  130. void vmw_fifo_ping_host(struct vmw_private *dev_priv, uint32_t reason)
  131. {
  132. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  133. mutex_lock(&dev_priv->hw_mutex);
  134. if (unlikely(ioread32(fifo_mem + SVGA_FIFO_BUSY) == 0)) {
  135. iowrite32(1, fifo_mem + SVGA_FIFO_BUSY);
  136. vmw_write(dev_priv, SVGA_REG_SYNC, reason);
  137. }
  138. mutex_unlock(&dev_priv->hw_mutex);
  139. }
  140. void vmw_fifo_release(struct vmw_private *dev_priv, struct vmw_fifo_state *fifo)
  141. {
  142. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  143. mutex_lock(&dev_priv->hw_mutex);
  144. while (vmw_read(dev_priv, SVGA_REG_BUSY) != 0)
  145. vmw_write(dev_priv, SVGA_REG_SYNC, SVGA_SYNC_GENERIC);
  146. dev_priv->last_read_seqno = ioread32(fifo_mem + SVGA_FIFO_FENCE);
  147. vmw_write(dev_priv, SVGA_REG_CONFIG_DONE,
  148. dev_priv->config_done_state);
  149. vmw_write(dev_priv, SVGA_REG_ENABLE,
  150. dev_priv->enable_state);
  151. vmw_write(dev_priv, SVGA_REG_TRACES,
  152. dev_priv->traces_state);
  153. mutex_unlock(&dev_priv->hw_mutex);
  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. __le32 __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. mutex_lock(&dev_priv->hw_mutex);
  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. mutex_unlock(&dev_priv->hw_mutex);
  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. mutex_lock(&dev_priv->hw_mutex);
  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. mutex_unlock(&dev_priv->hw_mutex);
  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. void *vmw_fifo_reserve(struct vmw_private *dev_priv, uint32_t bytes)
  258. {
  259. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  260. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  261. uint32_t max;
  262. uint32_t min;
  263. uint32_t next_cmd;
  264. uint32_t reserveable = fifo_state->capabilities & SVGA_FIFO_CAP_RESERVE;
  265. int ret;
  266. mutex_lock(&fifo_state->fifo_mutex);
  267. max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  268. min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  269. next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
  270. if (unlikely(bytes >= (max - min)))
  271. goto out_err;
  272. BUG_ON(fifo_state->reserved_size != 0);
  273. BUG_ON(fifo_state->dynamic_buffer != NULL);
  274. fifo_state->reserved_size = bytes;
  275. while (1) {
  276. uint32_t stop = ioread32(fifo_mem + SVGA_FIFO_STOP);
  277. bool need_bounce = false;
  278. bool reserve_in_place = false;
  279. if (next_cmd >= stop) {
  280. if (likely((next_cmd + bytes < max ||
  281. (next_cmd + bytes == max && stop > min))))
  282. reserve_in_place = true;
  283. else if (vmw_fifo_is_full(dev_priv, bytes)) {
  284. ret = vmw_fifo_wait(dev_priv, bytes,
  285. false, 3 * HZ);
  286. if (unlikely(ret != 0))
  287. goto out_err;
  288. } else
  289. need_bounce = true;
  290. } else {
  291. if (likely((next_cmd + bytes < stop)))
  292. reserve_in_place = true;
  293. else {
  294. ret = vmw_fifo_wait(dev_priv, bytes,
  295. false, 3 * HZ);
  296. if (unlikely(ret != 0))
  297. goto out_err;
  298. }
  299. }
  300. if (reserve_in_place) {
  301. if (reserveable || bytes <= sizeof(uint32_t)) {
  302. fifo_state->using_bounce_buffer = false;
  303. if (reserveable)
  304. iowrite32(bytes, fifo_mem +
  305. SVGA_FIFO_RESERVED);
  306. return fifo_mem + (next_cmd >> 2);
  307. } else {
  308. need_bounce = true;
  309. }
  310. }
  311. if (need_bounce) {
  312. fifo_state->using_bounce_buffer = true;
  313. if (bytes < fifo_state->static_buffer_size)
  314. return fifo_state->static_buffer;
  315. else {
  316. fifo_state->dynamic_buffer = vmalloc(bytes);
  317. return fifo_state->dynamic_buffer;
  318. }
  319. }
  320. }
  321. out_err:
  322. fifo_state->reserved_size = 0;
  323. mutex_unlock(&fifo_state->fifo_mutex);
  324. return NULL;
  325. }
  326. static void vmw_fifo_res_copy(struct vmw_fifo_state *fifo_state,
  327. __le32 __iomem *fifo_mem,
  328. uint32_t next_cmd,
  329. uint32_t max, uint32_t min, uint32_t bytes)
  330. {
  331. uint32_t chunk_size = max - next_cmd;
  332. uint32_t rest;
  333. uint32_t *buffer = (fifo_state->dynamic_buffer != NULL) ?
  334. fifo_state->dynamic_buffer : fifo_state->static_buffer;
  335. if (bytes < chunk_size)
  336. chunk_size = bytes;
  337. iowrite32(bytes, fifo_mem + SVGA_FIFO_RESERVED);
  338. mb();
  339. memcpy_toio(fifo_mem + (next_cmd >> 2), buffer, chunk_size);
  340. rest = bytes - chunk_size;
  341. if (rest)
  342. memcpy_toio(fifo_mem + (min >> 2), buffer + (chunk_size >> 2),
  343. rest);
  344. }
  345. static void vmw_fifo_slow_copy(struct vmw_fifo_state *fifo_state,
  346. __le32 __iomem *fifo_mem,
  347. uint32_t next_cmd,
  348. uint32_t max, uint32_t min, uint32_t bytes)
  349. {
  350. uint32_t *buffer = (fifo_state->dynamic_buffer != NULL) ?
  351. fifo_state->dynamic_buffer : fifo_state->static_buffer;
  352. while (bytes > 0) {
  353. iowrite32(*buffer++, fifo_mem + (next_cmd >> 2));
  354. next_cmd += sizeof(uint32_t);
  355. if (unlikely(next_cmd == max))
  356. next_cmd = min;
  357. mb();
  358. iowrite32(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
  359. mb();
  360. bytes -= sizeof(uint32_t);
  361. }
  362. }
  363. void vmw_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes)
  364. {
  365. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  366. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  367. uint32_t next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
  368. uint32_t max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  369. uint32_t min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  370. bool reserveable = fifo_state->capabilities & SVGA_FIFO_CAP_RESERVE;
  371. BUG_ON((bytes & 3) != 0);
  372. BUG_ON(bytes > fifo_state->reserved_size);
  373. fifo_state->reserved_size = 0;
  374. if (fifo_state->using_bounce_buffer) {
  375. if (reserveable)
  376. vmw_fifo_res_copy(fifo_state, fifo_mem,
  377. next_cmd, max, min, bytes);
  378. else
  379. vmw_fifo_slow_copy(fifo_state, fifo_mem,
  380. next_cmd, max, min, bytes);
  381. if (fifo_state->dynamic_buffer) {
  382. vfree(fifo_state->dynamic_buffer);
  383. fifo_state->dynamic_buffer = NULL;
  384. }
  385. }
  386. down_write(&fifo_state->rwsem);
  387. if (fifo_state->using_bounce_buffer || reserveable) {
  388. next_cmd += bytes;
  389. if (next_cmd >= max)
  390. next_cmd -= max - min;
  391. mb();
  392. iowrite32(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
  393. }
  394. if (reserveable)
  395. iowrite32(0, fifo_mem + SVGA_FIFO_RESERVED);
  396. mb();
  397. up_write(&fifo_state->rwsem);
  398. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  399. mutex_unlock(&fifo_state->fifo_mutex);
  400. }
  401. int vmw_fifo_send_fence(struct vmw_private *dev_priv, uint32_t *seqno)
  402. {
  403. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  404. struct svga_fifo_cmd_fence *cmd_fence;
  405. void *fm;
  406. int ret = 0;
  407. uint32_t bytes = sizeof(__le32) + sizeof(*cmd_fence);
  408. fm = vmw_fifo_reserve(dev_priv, bytes);
  409. if (unlikely(fm == NULL)) {
  410. *seqno = atomic_read(&dev_priv->marker_seq);
  411. ret = -ENOMEM;
  412. (void)vmw_fallback_wait(dev_priv, false, true, *seqno,
  413. false, 3*HZ);
  414. goto out_err;
  415. }
  416. do {
  417. *seqno = atomic_add_return(1, &dev_priv->marker_seq);
  418. } while (*seqno == 0);
  419. if (!(fifo_state->capabilities & SVGA_FIFO_CAP_FENCE)) {
  420. /*
  421. * Don't request hardware to send a fence. The
  422. * waiting code in vmwgfx_irq.c will emulate this.
  423. */
  424. vmw_fifo_commit(dev_priv, 0);
  425. return 0;
  426. }
  427. *(__le32 *) fm = cpu_to_le32(SVGA_CMD_FENCE);
  428. cmd_fence = (struct svga_fifo_cmd_fence *)
  429. ((unsigned long)fm + sizeof(__le32));
  430. iowrite32(*seqno, &cmd_fence->fence);
  431. vmw_fifo_commit(dev_priv, bytes);
  432. (void) vmw_marker_push(&fifo_state->marker_queue, *seqno);
  433. vmw_update_seqno(dev_priv, fifo_state);
  434. out_err:
  435. return ret;
  436. }
  437. /**
  438. * vmw_fifo_emit_dummy_legacy_query - emits a dummy query to the fifo using
  439. * legacy query commands.
  440. *
  441. * @dev_priv: The device private structure.
  442. * @cid: The hardware context id used for the query.
  443. *
  444. * See the vmw_fifo_emit_dummy_query documentation.
  445. */
  446. static int vmw_fifo_emit_dummy_legacy_query(struct vmw_private *dev_priv,
  447. uint32_t cid)
  448. {
  449. /*
  450. * A query wait without a preceding query end will
  451. * actually finish all queries for this cid
  452. * without writing to the query result structure.
  453. */
  454. struct ttm_buffer_object *bo = dev_priv->dummy_query_bo;
  455. struct {
  456. SVGA3dCmdHeader header;
  457. SVGA3dCmdWaitForQuery body;
  458. } *cmd;
  459. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  460. if (unlikely(cmd == NULL)) {
  461. DRM_ERROR("Out of fifo space for dummy query.\n");
  462. return -ENOMEM;
  463. }
  464. cmd->header.id = SVGA_3D_CMD_WAIT_FOR_QUERY;
  465. cmd->header.size = sizeof(cmd->body);
  466. cmd->body.cid = cid;
  467. cmd->body.type = SVGA3D_QUERYTYPE_OCCLUSION;
  468. if (bo->mem.mem_type == TTM_PL_VRAM) {
  469. cmd->body.guestResult.gmrId = SVGA_GMR_FRAMEBUFFER;
  470. cmd->body.guestResult.offset = bo->offset;
  471. } else {
  472. cmd->body.guestResult.gmrId = bo->mem.start;
  473. cmd->body.guestResult.offset = 0;
  474. }
  475. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  476. return 0;
  477. }
  478. /**
  479. * vmw_fifo_emit_dummy_gb_query - emits a dummy query to the fifo using
  480. * guest-backed resource query commands.
  481. *
  482. * @dev_priv: The device private structure.
  483. * @cid: The hardware context id used for the query.
  484. *
  485. * See the vmw_fifo_emit_dummy_query documentation.
  486. */
  487. static int vmw_fifo_emit_dummy_gb_query(struct vmw_private *dev_priv,
  488. uint32_t cid)
  489. {
  490. /*
  491. * A query wait without a preceding query end will
  492. * actually finish all queries for this cid
  493. * without writing to the query result structure.
  494. */
  495. struct ttm_buffer_object *bo = dev_priv->dummy_query_bo;
  496. struct {
  497. SVGA3dCmdHeader header;
  498. SVGA3dCmdWaitForGBQuery body;
  499. } *cmd;
  500. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  501. if (unlikely(cmd == NULL)) {
  502. DRM_ERROR("Out of fifo space for dummy query.\n");
  503. return -ENOMEM;
  504. }
  505. cmd->header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
  506. cmd->header.size = sizeof(cmd->body);
  507. cmd->body.cid = cid;
  508. cmd->body.type = SVGA3D_QUERYTYPE_OCCLUSION;
  509. BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
  510. cmd->body.mobid = bo->mem.start;
  511. cmd->body.offset = 0;
  512. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  513. return 0;
  514. }
  515. /**
  516. * vmw_fifo_emit_dummy_gb_query - emits a dummy query to the fifo using
  517. * appropriate resource query commands.
  518. *
  519. * @dev_priv: The device private structure.
  520. * @cid: The hardware context id used for the query.
  521. *
  522. * This function is used to emit a dummy occlusion query with
  523. * no primitives rendered between query begin and query end.
  524. * It's used to provide a query barrier, in order to know that when
  525. * this query is finished, all preceding queries are also finished.
  526. *
  527. * A Query results structure should have been initialized at the start
  528. * of the dev_priv->dummy_query_bo buffer object. And that buffer object
  529. * must also be either reserved or pinned when this function is called.
  530. *
  531. * Returns -ENOMEM on failure to reserve fifo space.
  532. */
  533. int vmw_fifo_emit_dummy_query(struct vmw_private *dev_priv,
  534. uint32_t cid)
  535. {
  536. if (dev_priv->has_mob)
  537. return vmw_fifo_emit_dummy_gb_query(dev_priv, cid);
  538. return vmw_fifo_emit_dummy_legacy_query(dev_priv, cid);
  539. }