vmwgfx_cmd.c 18 KB

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