vmwgfx_fifo.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. return fifo_state->dynamic_buffer;
  303. }
  304. }
  305. }
  306. out_err:
  307. fifo_state->reserved_size = 0;
  308. mutex_unlock(&fifo_state->fifo_mutex);
  309. return NULL;
  310. }
  311. void *vmw_fifo_reserve_dx(struct vmw_private *dev_priv, uint32_t bytes,
  312. int ctx_id)
  313. {
  314. void *ret;
  315. if (dev_priv->cman)
  316. ret = vmw_cmdbuf_reserve(dev_priv->cman, bytes,
  317. ctx_id, false, NULL);
  318. else if (ctx_id == SVGA3D_INVALID_ID)
  319. ret = vmw_local_fifo_reserve(dev_priv, bytes);
  320. else {
  321. WARN(1, "Command buffer has not been allocated.\n");
  322. ret = NULL;
  323. }
  324. if (IS_ERR_OR_NULL(ret)) {
  325. DRM_ERROR("Fifo reserve failure of %u bytes.\n",
  326. (unsigned) bytes);
  327. dump_stack();
  328. return NULL;
  329. }
  330. return ret;
  331. }
  332. static void vmw_fifo_res_copy(struct vmw_fifo_state *fifo_state,
  333. u32 *fifo_mem,
  334. uint32_t next_cmd,
  335. uint32_t max, uint32_t min, uint32_t bytes)
  336. {
  337. uint32_t chunk_size = max - next_cmd;
  338. uint32_t rest;
  339. uint32_t *buffer = (fifo_state->dynamic_buffer != NULL) ?
  340. fifo_state->dynamic_buffer : fifo_state->static_buffer;
  341. if (bytes < chunk_size)
  342. chunk_size = bytes;
  343. vmw_mmio_write(bytes, fifo_mem + SVGA_FIFO_RESERVED);
  344. mb();
  345. memcpy(fifo_mem + (next_cmd >> 2), buffer, chunk_size);
  346. rest = bytes - chunk_size;
  347. if (rest)
  348. memcpy(fifo_mem + (min >> 2), buffer + (chunk_size >> 2), rest);
  349. }
  350. static void vmw_fifo_slow_copy(struct vmw_fifo_state *fifo_state,
  351. u32 *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. vmw_mmio_write(*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. vmw_mmio_write(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
  364. mb();
  365. bytes -= sizeof(uint32_t);
  366. }
  367. }
  368. static void vmw_local_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes)
  369. {
  370. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  371. u32 *fifo_mem = dev_priv->mmio_virt;
  372. uint32_t next_cmd = vmw_mmio_read(fifo_mem + SVGA_FIFO_NEXT_CMD);
  373. uint32_t max = vmw_mmio_read(fifo_mem + SVGA_FIFO_MAX);
  374. uint32_t min = vmw_mmio_read(fifo_mem + SVGA_FIFO_MIN);
  375. bool reserveable = fifo_state->capabilities & SVGA_FIFO_CAP_RESERVE;
  376. if (fifo_state->dx)
  377. bytes += sizeof(struct vmw_temp_set_context);
  378. fifo_state->dx = false;
  379. BUG_ON((bytes & 3) != 0);
  380. BUG_ON(bytes > fifo_state->reserved_size);
  381. fifo_state->reserved_size = 0;
  382. if (fifo_state->using_bounce_buffer) {
  383. if (reserveable)
  384. vmw_fifo_res_copy(fifo_state, fifo_mem,
  385. next_cmd, max, min, bytes);
  386. else
  387. vmw_fifo_slow_copy(fifo_state, fifo_mem,
  388. next_cmd, max, min, bytes);
  389. if (fifo_state->dynamic_buffer) {
  390. vfree(fifo_state->dynamic_buffer);
  391. fifo_state->dynamic_buffer = NULL;
  392. }
  393. }
  394. down_write(&fifo_state->rwsem);
  395. if (fifo_state->using_bounce_buffer || reserveable) {
  396. next_cmd += bytes;
  397. if (next_cmd >= max)
  398. next_cmd -= max - min;
  399. mb();
  400. vmw_mmio_write(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
  401. }
  402. if (reserveable)
  403. vmw_mmio_write(0, fifo_mem + SVGA_FIFO_RESERVED);
  404. mb();
  405. up_write(&fifo_state->rwsem);
  406. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  407. mutex_unlock(&fifo_state->fifo_mutex);
  408. }
  409. void vmw_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes)
  410. {
  411. if (dev_priv->cman)
  412. vmw_cmdbuf_commit(dev_priv->cman, bytes, NULL, false);
  413. else
  414. vmw_local_fifo_commit(dev_priv, bytes);
  415. }
  416. /**
  417. * vmw_fifo_commit_flush - Commit fifo space and flush any buffered commands.
  418. *
  419. * @dev_priv: Pointer to device private structure.
  420. * @bytes: Number of bytes to commit.
  421. */
  422. void vmw_fifo_commit_flush(struct vmw_private *dev_priv, uint32_t bytes)
  423. {
  424. if (dev_priv->cman)
  425. vmw_cmdbuf_commit(dev_priv->cman, bytes, NULL, true);
  426. else
  427. vmw_local_fifo_commit(dev_priv, bytes);
  428. }
  429. /**
  430. * vmw_fifo_flush - Flush any buffered commands and make sure command processing
  431. * starts.
  432. *
  433. * @dev_priv: Pointer to device private structure.
  434. * @interruptible: Whether to wait interruptible if function needs to sleep.
  435. */
  436. int vmw_fifo_flush(struct vmw_private *dev_priv, bool interruptible)
  437. {
  438. might_sleep();
  439. if (dev_priv->cman)
  440. return vmw_cmdbuf_cur_flush(dev_priv->cman, interruptible);
  441. else
  442. return 0;
  443. }
  444. int vmw_fifo_send_fence(struct vmw_private *dev_priv, uint32_t *seqno)
  445. {
  446. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  447. struct svga_fifo_cmd_fence *cmd_fence;
  448. u32 *fm;
  449. int ret = 0;
  450. uint32_t bytes = sizeof(u32) + sizeof(*cmd_fence);
  451. fm = vmw_fifo_reserve(dev_priv, bytes);
  452. if (unlikely(fm == NULL)) {
  453. *seqno = atomic_read(&dev_priv->marker_seq);
  454. ret = -ENOMEM;
  455. (void)vmw_fallback_wait(dev_priv, false, true, *seqno,
  456. false, 3*HZ);
  457. goto out_err;
  458. }
  459. do {
  460. *seqno = atomic_add_return(1, &dev_priv->marker_seq);
  461. } while (*seqno == 0);
  462. if (!(fifo_state->capabilities & SVGA_FIFO_CAP_FENCE)) {
  463. /*
  464. * Don't request hardware to send a fence. The
  465. * waiting code in vmwgfx_irq.c will emulate this.
  466. */
  467. vmw_fifo_commit(dev_priv, 0);
  468. return 0;
  469. }
  470. *fm++ = SVGA_CMD_FENCE;
  471. cmd_fence = (struct svga_fifo_cmd_fence *) fm;
  472. cmd_fence->fence = *seqno;
  473. vmw_fifo_commit_flush(dev_priv, bytes);
  474. (void) vmw_marker_push(&fifo_state->marker_queue, *seqno);
  475. vmw_update_seqno(dev_priv, fifo_state);
  476. out_err:
  477. return ret;
  478. }
  479. /**
  480. * vmw_fifo_emit_dummy_legacy_query - emits a dummy query to the fifo using
  481. * legacy query commands.
  482. *
  483. * @dev_priv: The device private structure.
  484. * @cid: The hardware context id used for the query.
  485. *
  486. * See the vmw_fifo_emit_dummy_query documentation.
  487. */
  488. static int vmw_fifo_emit_dummy_legacy_query(struct vmw_private *dev_priv,
  489. uint32_t cid)
  490. {
  491. /*
  492. * A query wait without a preceding query end will
  493. * actually finish all queries for this cid
  494. * without writing to the query result structure.
  495. */
  496. struct ttm_buffer_object *bo = &dev_priv->dummy_query_bo->base;
  497. struct {
  498. SVGA3dCmdHeader header;
  499. SVGA3dCmdWaitForQuery body;
  500. } *cmd;
  501. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  502. if (unlikely(cmd == NULL)) {
  503. DRM_ERROR("Out of fifo space for dummy query.\n");
  504. return -ENOMEM;
  505. }
  506. cmd->header.id = SVGA_3D_CMD_WAIT_FOR_QUERY;
  507. cmd->header.size = sizeof(cmd->body);
  508. cmd->body.cid = cid;
  509. cmd->body.type = SVGA3D_QUERYTYPE_OCCLUSION;
  510. if (bo->mem.mem_type == TTM_PL_VRAM) {
  511. cmd->body.guestResult.gmrId = SVGA_GMR_FRAMEBUFFER;
  512. cmd->body.guestResult.offset = bo->offset;
  513. } else {
  514. cmd->body.guestResult.gmrId = bo->mem.start;
  515. cmd->body.guestResult.offset = 0;
  516. }
  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. * guest-backed resource query commands.
  523. *
  524. * @dev_priv: The device private structure.
  525. * @cid: The hardware context id used for the query.
  526. *
  527. * See the vmw_fifo_emit_dummy_query documentation.
  528. */
  529. static int vmw_fifo_emit_dummy_gb_query(struct vmw_private *dev_priv,
  530. uint32_t cid)
  531. {
  532. /*
  533. * A query wait without a preceding query end will
  534. * actually finish all queries for this cid
  535. * without writing to the query result structure.
  536. */
  537. struct ttm_buffer_object *bo = &dev_priv->dummy_query_bo->base;
  538. struct {
  539. SVGA3dCmdHeader header;
  540. SVGA3dCmdWaitForGBQuery body;
  541. } *cmd;
  542. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  543. if (unlikely(cmd == NULL)) {
  544. DRM_ERROR("Out of fifo space for dummy query.\n");
  545. return -ENOMEM;
  546. }
  547. cmd->header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
  548. cmd->header.size = sizeof(cmd->body);
  549. cmd->body.cid = cid;
  550. cmd->body.type = SVGA3D_QUERYTYPE_OCCLUSION;
  551. BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
  552. cmd->body.mobid = bo->mem.start;
  553. cmd->body.offset = 0;
  554. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  555. return 0;
  556. }
  557. /**
  558. * vmw_fifo_emit_dummy_gb_query - emits a dummy query to the fifo using
  559. * appropriate resource query commands.
  560. *
  561. * @dev_priv: The device private structure.
  562. * @cid: The hardware context id used for the query.
  563. *
  564. * This function is used to emit a dummy occlusion query with
  565. * no primitives rendered between query begin and query end.
  566. * It's used to provide a query barrier, in order to know that when
  567. * this query is finished, all preceding queries are also finished.
  568. *
  569. * A Query results structure should have been initialized at the start
  570. * of the dev_priv->dummy_query_bo buffer object. And that buffer object
  571. * must also be either reserved or pinned when this function is called.
  572. *
  573. * Returns -ENOMEM on failure to reserve fifo space.
  574. */
  575. int vmw_fifo_emit_dummy_query(struct vmw_private *dev_priv,
  576. uint32_t cid)
  577. {
  578. if (dev_priv->has_mob)
  579. return vmw_fifo_emit_dummy_gb_query(dev_priv, cid);
  580. return vmw_fifo_emit_dummy_legacy_query(dev_priv, cid);
  581. }
  582. void *vmw_fifo_reserve(struct vmw_private *dev_priv, uint32_t bytes)
  583. {
  584. return vmw_fifo_reserve_dx(dev_priv, bytes, SVGA3D_INVALID_ID);
  585. }