vmwgfx_fifo.c 20 KB

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