vmwgfx_fifo.c 19 KB

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