vmwgfx_fifo.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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_locked(struct vmw_private *dev_priv, uint32_t reason)
  131. {
  132. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  133. if (unlikely(ioread32(fifo_mem + SVGA_FIFO_BUSY) == 0)) {
  134. iowrite32(1, fifo_mem + SVGA_FIFO_BUSY);
  135. vmw_write(dev_priv, SVGA_REG_SYNC, reason);
  136. }
  137. }
  138. void vmw_fifo_ping_host(struct vmw_private *dev_priv, uint32_t reason)
  139. {
  140. mutex_lock(&dev_priv->hw_mutex);
  141. vmw_fifo_ping_host_locked(dev_priv, reason);
  142. mutex_unlock(&dev_priv->hw_mutex);
  143. }
  144. void vmw_fifo_release(struct vmw_private *dev_priv, struct vmw_fifo_state *fifo)
  145. {
  146. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  147. mutex_lock(&dev_priv->hw_mutex);
  148. vmw_write(dev_priv, SVGA_REG_SYNC, SVGA_SYNC_GENERIC);
  149. while (vmw_read(dev_priv, SVGA_REG_BUSY) != 0)
  150. ;
  151. dev_priv->last_read_seqno = ioread32(fifo_mem + SVGA_FIFO_FENCE);
  152. vmw_write(dev_priv, SVGA_REG_CONFIG_DONE,
  153. dev_priv->config_done_state);
  154. vmw_write(dev_priv, SVGA_REG_ENABLE,
  155. dev_priv->enable_state);
  156. vmw_write(dev_priv, SVGA_REG_TRACES,
  157. dev_priv->traces_state);
  158. mutex_unlock(&dev_priv->hw_mutex);
  159. vmw_marker_queue_takedown(&fifo->marker_queue);
  160. if (likely(fifo->static_buffer != NULL)) {
  161. vfree(fifo->static_buffer);
  162. fifo->static_buffer = NULL;
  163. }
  164. if (likely(fifo->dynamic_buffer != NULL)) {
  165. vfree(fifo->dynamic_buffer);
  166. fifo->dynamic_buffer = NULL;
  167. }
  168. }
  169. static bool vmw_fifo_is_full(struct vmw_private *dev_priv, uint32_t bytes)
  170. {
  171. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  172. uint32_t max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  173. uint32_t next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
  174. uint32_t min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  175. uint32_t stop = ioread32(fifo_mem + SVGA_FIFO_STOP);
  176. return ((max - next_cmd) + (stop - min) <= bytes);
  177. }
  178. static int vmw_fifo_wait_noirq(struct vmw_private *dev_priv,
  179. uint32_t bytes, bool interruptible,
  180. unsigned long timeout)
  181. {
  182. int ret = 0;
  183. unsigned long end_jiffies = jiffies + timeout;
  184. DEFINE_WAIT(__wait);
  185. DRM_INFO("Fifo wait noirq.\n");
  186. for (;;) {
  187. prepare_to_wait(&dev_priv->fifo_queue, &__wait,
  188. (interruptible) ?
  189. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  190. if (!vmw_fifo_is_full(dev_priv, bytes))
  191. break;
  192. if (time_after_eq(jiffies, end_jiffies)) {
  193. ret = -EBUSY;
  194. DRM_ERROR("SVGA device lockup.\n");
  195. break;
  196. }
  197. schedule_timeout(1);
  198. if (interruptible && signal_pending(current)) {
  199. ret = -ERESTARTSYS;
  200. break;
  201. }
  202. }
  203. finish_wait(&dev_priv->fifo_queue, &__wait);
  204. wake_up_all(&dev_priv->fifo_queue);
  205. DRM_INFO("Fifo noirq exit.\n");
  206. return ret;
  207. }
  208. static int vmw_fifo_wait(struct vmw_private *dev_priv,
  209. uint32_t bytes, bool interruptible,
  210. unsigned long timeout)
  211. {
  212. long ret = 1L;
  213. unsigned long irq_flags;
  214. if (likely(!vmw_fifo_is_full(dev_priv, bytes)))
  215. return 0;
  216. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_FIFOFULL);
  217. if (!(dev_priv->capabilities & SVGA_CAP_IRQMASK))
  218. return vmw_fifo_wait_noirq(dev_priv, bytes,
  219. interruptible, timeout);
  220. mutex_lock(&dev_priv->hw_mutex);
  221. if (atomic_add_return(1, &dev_priv->fifo_queue_waiters) > 0) {
  222. spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
  223. outl(SVGA_IRQFLAG_FIFO_PROGRESS,
  224. dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  225. dev_priv->irq_mask |= SVGA_IRQFLAG_FIFO_PROGRESS;
  226. vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
  227. spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
  228. }
  229. mutex_unlock(&dev_priv->hw_mutex);
  230. if (interruptible)
  231. ret = wait_event_interruptible_timeout
  232. (dev_priv->fifo_queue,
  233. !vmw_fifo_is_full(dev_priv, bytes), timeout);
  234. else
  235. ret = wait_event_timeout
  236. (dev_priv->fifo_queue,
  237. !vmw_fifo_is_full(dev_priv, bytes), timeout);
  238. if (unlikely(ret == 0))
  239. ret = -EBUSY;
  240. else if (likely(ret > 0))
  241. ret = 0;
  242. mutex_lock(&dev_priv->hw_mutex);
  243. if (atomic_dec_and_test(&dev_priv->fifo_queue_waiters)) {
  244. spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
  245. dev_priv->irq_mask &= ~SVGA_IRQFLAG_FIFO_PROGRESS;
  246. vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
  247. spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
  248. }
  249. mutex_unlock(&dev_priv->hw_mutex);
  250. return ret;
  251. }
  252. /**
  253. * Reserve @bytes number of bytes in the fifo.
  254. *
  255. * This function will return NULL (error) on two conditions:
  256. * If it timeouts waiting for fifo space, or if @bytes is larger than the
  257. * available fifo space.
  258. *
  259. * Returns:
  260. * Pointer to the fifo, or null on error (possible hardware hang).
  261. */
  262. void *vmw_fifo_reserve(struct vmw_private *dev_priv, uint32_t bytes)
  263. {
  264. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  265. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  266. uint32_t max;
  267. uint32_t min;
  268. uint32_t next_cmd;
  269. uint32_t reserveable = fifo_state->capabilities & SVGA_FIFO_CAP_RESERVE;
  270. int ret;
  271. mutex_lock(&fifo_state->fifo_mutex);
  272. max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  273. min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  274. next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
  275. if (unlikely(bytes >= (max - min)))
  276. goto out_err;
  277. BUG_ON(fifo_state->reserved_size != 0);
  278. BUG_ON(fifo_state->dynamic_buffer != NULL);
  279. fifo_state->reserved_size = bytes;
  280. while (1) {
  281. uint32_t stop = ioread32(fifo_mem + SVGA_FIFO_STOP);
  282. bool need_bounce = false;
  283. bool reserve_in_place = false;
  284. if (next_cmd >= stop) {
  285. if (likely((next_cmd + bytes < max ||
  286. (next_cmd + bytes == max && stop > min))))
  287. reserve_in_place = true;
  288. else if (vmw_fifo_is_full(dev_priv, bytes)) {
  289. ret = vmw_fifo_wait(dev_priv, bytes,
  290. false, 3 * HZ);
  291. if (unlikely(ret != 0))
  292. goto out_err;
  293. } else
  294. need_bounce = true;
  295. } else {
  296. if (likely((next_cmd + bytes < stop)))
  297. reserve_in_place = true;
  298. else {
  299. ret = vmw_fifo_wait(dev_priv, bytes,
  300. false, 3 * HZ);
  301. if (unlikely(ret != 0))
  302. goto out_err;
  303. }
  304. }
  305. if (reserve_in_place) {
  306. if (reserveable || bytes <= sizeof(uint32_t)) {
  307. fifo_state->using_bounce_buffer = false;
  308. if (reserveable)
  309. iowrite32(bytes, fifo_mem +
  310. SVGA_FIFO_RESERVED);
  311. return fifo_mem + (next_cmd >> 2);
  312. } else {
  313. need_bounce = true;
  314. }
  315. }
  316. if (need_bounce) {
  317. fifo_state->using_bounce_buffer = true;
  318. if (bytes < fifo_state->static_buffer_size)
  319. return fifo_state->static_buffer;
  320. else {
  321. fifo_state->dynamic_buffer = vmalloc(bytes);
  322. return fifo_state->dynamic_buffer;
  323. }
  324. }
  325. }
  326. out_err:
  327. fifo_state->reserved_size = 0;
  328. mutex_unlock(&fifo_state->fifo_mutex);
  329. return NULL;
  330. }
  331. static void vmw_fifo_res_copy(struct vmw_fifo_state *fifo_state,
  332. __le32 __iomem *fifo_mem,
  333. uint32_t next_cmd,
  334. uint32_t max, uint32_t min, uint32_t bytes)
  335. {
  336. uint32_t chunk_size = max - next_cmd;
  337. uint32_t rest;
  338. uint32_t *buffer = (fifo_state->dynamic_buffer != NULL) ?
  339. fifo_state->dynamic_buffer : fifo_state->static_buffer;
  340. if (bytes < chunk_size)
  341. chunk_size = bytes;
  342. iowrite32(bytes, fifo_mem + SVGA_FIFO_RESERVED);
  343. mb();
  344. memcpy_toio(fifo_mem + (next_cmd >> 2), buffer, chunk_size);
  345. rest = bytes - chunk_size;
  346. if (rest)
  347. memcpy_toio(fifo_mem + (min >> 2), buffer + (chunk_size >> 2),
  348. rest);
  349. }
  350. static void vmw_fifo_slow_copy(struct vmw_fifo_state *fifo_state,
  351. __le32 __iomem *fifo_mem,
  352. uint32_t next_cmd,
  353. uint32_t max, uint32_t min, uint32_t bytes)
  354. {
  355. uint32_t *buffer = (fifo_state->dynamic_buffer != NULL) ?
  356. fifo_state->dynamic_buffer : fifo_state->static_buffer;
  357. while (bytes > 0) {
  358. iowrite32(*buffer++, fifo_mem + (next_cmd >> 2));
  359. next_cmd += sizeof(uint32_t);
  360. if (unlikely(next_cmd == max))
  361. next_cmd = min;
  362. mb();
  363. iowrite32(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
  364. mb();
  365. bytes -= sizeof(uint32_t);
  366. }
  367. }
  368. void vmw_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes)
  369. {
  370. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  371. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  372. uint32_t next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
  373. uint32_t max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  374. uint32_t min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  375. bool reserveable = fifo_state->capabilities & SVGA_FIFO_CAP_RESERVE;
  376. BUG_ON((bytes & 3) != 0);
  377. BUG_ON(bytes > fifo_state->reserved_size);
  378. fifo_state->reserved_size = 0;
  379. if (fifo_state->using_bounce_buffer) {
  380. if (reserveable)
  381. vmw_fifo_res_copy(fifo_state, fifo_mem,
  382. next_cmd, max, min, bytes);
  383. else
  384. vmw_fifo_slow_copy(fifo_state, fifo_mem,
  385. next_cmd, max, min, bytes);
  386. if (fifo_state->dynamic_buffer) {
  387. vfree(fifo_state->dynamic_buffer);
  388. fifo_state->dynamic_buffer = NULL;
  389. }
  390. }
  391. down_write(&fifo_state->rwsem);
  392. if (fifo_state->using_bounce_buffer || reserveable) {
  393. next_cmd += bytes;
  394. if (next_cmd >= max)
  395. next_cmd -= max - min;
  396. mb();
  397. iowrite32(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
  398. }
  399. if (reserveable)
  400. iowrite32(0, fifo_mem + SVGA_FIFO_RESERVED);
  401. mb();
  402. up_write(&fifo_state->rwsem);
  403. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  404. mutex_unlock(&fifo_state->fifo_mutex);
  405. }
  406. int vmw_fifo_send_fence(struct vmw_private *dev_priv, uint32_t *seqno)
  407. {
  408. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  409. struct svga_fifo_cmd_fence *cmd_fence;
  410. void *fm;
  411. int ret = 0;
  412. uint32_t bytes = sizeof(__le32) + sizeof(*cmd_fence);
  413. fm = vmw_fifo_reserve(dev_priv, bytes);
  414. if (unlikely(fm == NULL)) {
  415. *seqno = atomic_read(&dev_priv->marker_seq);
  416. ret = -ENOMEM;
  417. (void)vmw_fallback_wait(dev_priv, false, true, *seqno,
  418. false, 3*HZ);
  419. goto out_err;
  420. }
  421. do {
  422. *seqno = atomic_add_return(1, &dev_priv->marker_seq);
  423. } while (*seqno == 0);
  424. if (!(fifo_state->capabilities & SVGA_FIFO_CAP_FENCE)) {
  425. /*
  426. * Don't request hardware to send a fence. The
  427. * waiting code in vmwgfx_irq.c will emulate this.
  428. */
  429. vmw_fifo_commit(dev_priv, 0);
  430. return 0;
  431. }
  432. *(__le32 *) fm = cpu_to_le32(SVGA_CMD_FENCE);
  433. cmd_fence = (struct svga_fifo_cmd_fence *)
  434. ((unsigned long)fm + sizeof(__le32));
  435. iowrite32(*seqno, &cmd_fence->fence);
  436. vmw_fifo_commit(dev_priv, bytes);
  437. (void) vmw_marker_push(&fifo_state->marker_queue, *seqno);
  438. vmw_update_seqno(dev_priv, fifo_state);
  439. out_err:
  440. return ret;
  441. }
  442. /**
  443. * vmw_fifo_emit_dummy_legacy_query - emits a dummy query to the fifo using
  444. * legacy query commands.
  445. *
  446. * @dev_priv: The device private structure.
  447. * @cid: The hardware context id used for the query.
  448. *
  449. * See the vmw_fifo_emit_dummy_query documentation.
  450. */
  451. static int vmw_fifo_emit_dummy_legacy_query(struct vmw_private *dev_priv,
  452. uint32_t cid)
  453. {
  454. /*
  455. * A query wait without a preceding query end will
  456. * actually finish all queries for this cid
  457. * without writing to the query result structure.
  458. */
  459. struct ttm_buffer_object *bo = dev_priv->dummy_query_bo;
  460. struct {
  461. SVGA3dCmdHeader header;
  462. SVGA3dCmdWaitForQuery body;
  463. } *cmd;
  464. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  465. if (unlikely(cmd == NULL)) {
  466. DRM_ERROR("Out of fifo space for dummy query.\n");
  467. return -ENOMEM;
  468. }
  469. cmd->header.id = SVGA_3D_CMD_WAIT_FOR_QUERY;
  470. cmd->header.size = sizeof(cmd->body);
  471. cmd->body.cid = cid;
  472. cmd->body.type = SVGA3D_QUERYTYPE_OCCLUSION;
  473. if (bo->mem.mem_type == TTM_PL_VRAM) {
  474. cmd->body.guestResult.gmrId = SVGA_GMR_FRAMEBUFFER;
  475. cmd->body.guestResult.offset = bo->offset;
  476. } else {
  477. cmd->body.guestResult.gmrId = bo->mem.start;
  478. cmd->body.guestResult.offset = 0;
  479. }
  480. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  481. return 0;
  482. }
  483. /**
  484. * vmw_fifo_emit_dummy_gb_query - emits a dummy query to the fifo using
  485. * guest-backed resource query commands.
  486. *
  487. * @dev_priv: The device private structure.
  488. * @cid: The hardware context id used for the query.
  489. *
  490. * See the vmw_fifo_emit_dummy_query documentation.
  491. */
  492. static int vmw_fifo_emit_dummy_gb_query(struct vmw_private *dev_priv,
  493. uint32_t cid)
  494. {
  495. /*
  496. * A query wait without a preceding query end will
  497. * actually finish all queries for this cid
  498. * without writing to the query result structure.
  499. */
  500. struct ttm_buffer_object *bo = dev_priv->dummy_query_bo;
  501. struct {
  502. SVGA3dCmdHeader header;
  503. SVGA3dCmdWaitForGBQuery body;
  504. } *cmd;
  505. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  506. if (unlikely(cmd == NULL)) {
  507. DRM_ERROR("Out of fifo space for dummy query.\n");
  508. return -ENOMEM;
  509. }
  510. cmd->header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
  511. cmd->header.size = sizeof(cmd->body);
  512. cmd->body.cid = cid;
  513. cmd->body.type = SVGA3D_QUERYTYPE_OCCLUSION;
  514. BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
  515. cmd->body.mobid = bo->mem.start;
  516. cmd->body.offset = 0;
  517. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  518. return 0;
  519. }
  520. /**
  521. * vmw_fifo_emit_dummy_gb_query - emits a dummy query to the fifo using
  522. * appropriate resource query commands.
  523. *
  524. * @dev_priv: The device private structure.
  525. * @cid: The hardware context id used for the query.
  526. *
  527. * This function is used to emit a dummy occlusion query with
  528. * no primitives rendered between query begin and query end.
  529. * It's used to provide a query barrier, in order to know that when
  530. * this query is finished, all preceding queries are also finished.
  531. *
  532. * A Query results structure should have been initialized at the start
  533. * of the dev_priv->dummy_query_bo buffer object. And that buffer object
  534. * must also be either reserved or pinned when this function is called.
  535. *
  536. * Returns -ENOMEM on failure to reserve fifo space.
  537. */
  538. int vmw_fifo_emit_dummy_query(struct vmw_private *dev_priv,
  539. uint32_t cid)
  540. {
  541. if (dev_priv->has_mob)
  542. return vmw_fifo_emit_dummy_gb_query(dev_priv, cid);
  543. return vmw_fifo_emit_dummy_legacy_query(dev_priv, cid);
  544. }