vmwgfx_fifo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_drv.h"
  28. #include "drmP.h"
  29. #include "ttm/ttm_placement.h"
  30. bool vmw_fifo_have_3d(struct vmw_private *dev_priv)
  31. {
  32. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  33. uint32_t fifo_min, hwversion;
  34. if (!(dev_priv->capabilities & SVGA_CAP_EXTENDED_FIFO))
  35. return false;
  36. fifo_min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  37. if (fifo_min <= SVGA_FIFO_3D_HWVERSION * sizeof(unsigned int))
  38. return false;
  39. hwversion = ioread32(fifo_mem + SVGA_FIFO_3D_HWVERSION);
  40. if (hwversion == 0)
  41. return false;
  42. if (hwversion < SVGA3D_HWVERSION_WS65_B1)
  43. return false;
  44. return true;
  45. }
  46. bool vmw_fifo_have_pitchlock(struct vmw_private *dev_priv)
  47. {
  48. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  49. uint32_t caps;
  50. if (!(dev_priv->capabilities & SVGA_CAP_EXTENDED_FIFO))
  51. return false;
  52. caps = ioread32(fifo_mem + SVGA_FIFO_CAPABILITIES);
  53. if (caps & SVGA_FIFO_CAP_PITCHLOCK)
  54. return true;
  55. return false;
  56. }
  57. int vmw_fifo_init(struct vmw_private *dev_priv, struct vmw_fifo_state *fifo)
  58. {
  59. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  60. uint32_t max;
  61. uint32_t min;
  62. uint32_t dummy;
  63. fifo->static_buffer_size = VMWGFX_FIFO_STATIC_SIZE;
  64. fifo->static_buffer = vmalloc(fifo->static_buffer_size);
  65. if (unlikely(fifo->static_buffer == NULL))
  66. return -ENOMEM;
  67. fifo->dynamic_buffer = NULL;
  68. fifo->reserved_size = 0;
  69. fifo->using_bounce_buffer = false;
  70. mutex_init(&fifo->fifo_mutex);
  71. init_rwsem(&fifo->rwsem);
  72. /*
  73. * Allow mapping the first page read-only to user-space.
  74. */
  75. DRM_INFO("width %d\n", vmw_read(dev_priv, SVGA_REG_WIDTH));
  76. DRM_INFO("height %d\n", vmw_read(dev_priv, SVGA_REG_HEIGHT));
  77. DRM_INFO("bpp %d\n", vmw_read(dev_priv, SVGA_REG_BITS_PER_PIXEL));
  78. mutex_lock(&dev_priv->hw_mutex);
  79. dev_priv->enable_state = vmw_read(dev_priv, SVGA_REG_ENABLE);
  80. dev_priv->config_done_state = vmw_read(dev_priv, SVGA_REG_CONFIG_DONE);
  81. dev_priv->traces_state = vmw_read(dev_priv, SVGA_REG_TRACES);
  82. vmw_write(dev_priv, SVGA_REG_ENABLE, 1);
  83. min = 4;
  84. if (dev_priv->capabilities & SVGA_CAP_EXTENDED_FIFO)
  85. min = vmw_read(dev_priv, SVGA_REG_MEM_REGS);
  86. min <<= 2;
  87. if (min < PAGE_SIZE)
  88. min = PAGE_SIZE;
  89. iowrite32(min, fifo_mem + SVGA_FIFO_MIN);
  90. iowrite32(dev_priv->mmio_size, fifo_mem + SVGA_FIFO_MAX);
  91. wmb();
  92. iowrite32(min, fifo_mem + SVGA_FIFO_NEXT_CMD);
  93. iowrite32(min, fifo_mem + SVGA_FIFO_STOP);
  94. iowrite32(0, fifo_mem + SVGA_FIFO_BUSY);
  95. mb();
  96. vmw_write(dev_priv, SVGA_REG_CONFIG_DONE, 1);
  97. mutex_unlock(&dev_priv->hw_mutex);
  98. max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  99. min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  100. fifo->capabilities = ioread32(fifo_mem + SVGA_FIFO_CAPABILITIES);
  101. DRM_INFO("Fifo max 0x%08x min 0x%08x cap 0x%08x\n",
  102. (unsigned int) max,
  103. (unsigned int) min,
  104. (unsigned int) fifo->capabilities);
  105. atomic_set(&dev_priv->marker_seq, dev_priv->last_read_seqno);
  106. iowrite32(dev_priv->last_read_seqno, fifo_mem + SVGA_FIFO_FENCE);
  107. vmw_marker_queue_init(&fifo->marker_queue);
  108. return vmw_fifo_send_fence(dev_priv, &dummy);
  109. }
  110. void vmw_fifo_ping_host(struct vmw_private *dev_priv, uint32_t reason)
  111. {
  112. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  113. mutex_lock(&dev_priv->hw_mutex);
  114. if (unlikely(ioread32(fifo_mem + SVGA_FIFO_BUSY) == 0)) {
  115. iowrite32(1, fifo_mem + SVGA_FIFO_BUSY);
  116. vmw_write(dev_priv, SVGA_REG_SYNC, reason);
  117. }
  118. mutex_unlock(&dev_priv->hw_mutex);
  119. }
  120. void vmw_fifo_release(struct vmw_private *dev_priv, struct vmw_fifo_state *fifo)
  121. {
  122. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  123. mutex_lock(&dev_priv->hw_mutex);
  124. while (vmw_read(dev_priv, SVGA_REG_BUSY) != 0)
  125. vmw_write(dev_priv, SVGA_REG_SYNC, SVGA_SYNC_GENERIC);
  126. dev_priv->last_read_seqno = ioread32(fifo_mem + SVGA_FIFO_FENCE);
  127. vmw_write(dev_priv, SVGA_REG_CONFIG_DONE,
  128. dev_priv->config_done_state);
  129. vmw_write(dev_priv, SVGA_REG_ENABLE,
  130. dev_priv->enable_state);
  131. vmw_write(dev_priv, SVGA_REG_TRACES,
  132. dev_priv->traces_state);
  133. mutex_unlock(&dev_priv->hw_mutex);
  134. vmw_marker_queue_takedown(&fifo->marker_queue);
  135. if (likely(fifo->static_buffer != NULL)) {
  136. vfree(fifo->static_buffer);
  137. fifo->static_buffer = NULL;
  138. }
  139. if (likely(fifo->dynamic_buffer != NULL)) {
  140. vfree(fifo->dynamic_buffer);
  141. fifo->dynamic_buffer = NULL;
  142. }
  143. }
  144. static bool vmw_fifo_is_full(struct vmw_private *dev_priv, uint32_t bytes)
  145. {
  146. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  147. uint32_t max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  148. uint32_t next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
  149. uint32_t min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  150. uint32_t stop = ioread32(fifo_mem + SVGA_FIFO_STOP);
  151. return ((max - next_cmd) + (stop - min) <= bytes);
  152. }
  153. static int vmw_fifo_wait_noirq(struct vmw_private *dev_priv,
  154. uint32_t bytes, bool interruptible,
  155. unsigned long timeout)
  156. {
  157. int ret = 0;
  158. unsigned long end_jiffies = jiffies + timeout;
  159. DEFINE_WAIT(__wait);
  160. DRM_INFO("Fifo wait noirq.\n");
  161. for (;;) {
  162. prepare_to_wait(&dev_priv->fifo_queue, &__wait,
  163. (interruptible) ?
  164. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  165. if (!vmw_fifo_is_full(dev_priv, bytes))
  166. break;
  167. if (time_after_eq(jiffies, end_jiffies)) {
  168. ret = -EBUSY;
  169. DRM_ERROR("SVGA device lockup.\n");
  170. break;
  171. }
  172. schedule_timeout(1);
  173. if (interruptible && signal_pending(current)) {
  174. ret = -ERESTARTSYS;
  175. break;
  176. }
  177. }
  178. finish_wait(&dev_priv->fifo_queue, &__wait);
  179. wake_up_all(&dev_priv->fifo_queue);
  180. DRM_INFO("Fifo noirq exit.\n");
  181. return ret;
  182. }
  183. static int vmw_fifo_wait(struct vmw_private *dev_priv,
  184. uint32_t bytes, bool interruptible,
  185. unsigned long timeout)
  186. {
  187. long ret = 1L;
  188. unsigned long irq_flags;
  189. if (likely(!vmw_fifo_is_full(dev_priv, bytes)))
  190. return 0;
  191. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_FIFOFULL);
  192. if (!(dev_priv->capabilities & SVGA_CAP_IRQMASK))
  193. return vmw_fifo_wait_noirq(dev_priv, bytes,
  194. interruptible, timeout);
  195. mutex_lock(&dev_priv->hw_mutex);
  196. if (atomic_add_return(1, &dev_priv->fifo_queue_waiters) > 0) {
  197. spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
  198. outl(SVGA_IRQFLAG_FIFO_PROGRESS,
  199. dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  200. vmw_write(dev_priv, SVGA_REG_IRQMASK,
  201. vmw_read(dev_priv, SVGA_REG_IRQMASK) |
  202. SVGA_IRQFLAG_FIFO_PROGRESS);
  203. spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
  204. }
  205. mutex_unlock(&dev_priv->hw_mutex);
  206. if (interruptible)
  207. ret = wait_event_interruptible_timeout
  208. (dev_priv->fifo_queue,
  209. !vmw_fifo_is_full(dev_priv, bytes), timeout);
  210. else
  211. ret = wait_event_timeout
  212. (dev_priv->fifo_queue,
  213. !vmw_fifo_is_full(dev_priv, bytes), timeout);
  214. if (unlikely(ret == 0))
  215. ret = -EBUSY;
  216. else if (likely(ret > 0))
  217. ret = 0;
  218. mutex_lock(&dev_priv->hw_mutex);
  219. if (atomic_dec_and_test(&dev_priv->fifo_queue_waiters)) {
  220. spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
  221. vmw_write(dev_priv, SVGA_REG_IRQMASK,
  222. vmw_read(dev_priv, SVGA_REG_IRQMASK) &
  223. ~SVGA_IRQFLAG_FIFO_PROGRESS);
  224. spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
  225. }
  226. mutex_unlock(&dev_priv->hw_mutex);
  227. return ret;
  228. }
  229. void *vmw_fifo_reserve(struct vmw_private *dev_priv, uint32_t bytes)
  230. {
  231. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  232. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  233. uint32_t max;
  234. uint32_t min;
  235. uint32_t next_cmd;
  236. uint32_t reserveable = fifo_state->capabilities & SVGA_FIFO_CAP_RESERVE;
  237. int ret;
  238. mutex_lock(&fifo_state->fifo_mutex);
  239. max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  240. min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  241. next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
  242. if (unlikely(bytes >= (max - min)))
  243. goto out_err;
  244. BUG_ON(fifo_state->reserved_size != 0);
  245. BUG_ON(fifo_state->dynamic_buffer != NULL);
  246. fifo_state->reserved_size = bytes;
  247. while (1) {
  248. uint32_t stop = ioread32(fifo_mem + SVGA_FIFO_STOP);
  249. bool need_bounce = false;
  250. bool reserve_in_place = false;
  251. if (next_cmd >= stop) {
  252. if (likely((next_cmd + bytes < max ||
  253. (next_cmd + bytes == max && stop > min))))
  254. reserve_in_place = true;
  255. else if (vmw_fifo_is_full(dev_priv, bytes)) {
  256. ret = vmw_fifo_wait(dev_priv, bytes,
  257. false, 3 * HZ);
  258. if (unlikely(ret != 0))
  259. goto out_err;
  260. } else
  261. need_bounce = true;
  262. } else {
  263. if (likely((next_cmd + bytes < stop)))
  264. reserve_in_place = true;
  265. else {
  266. ret = vmw_fifo_wait(dev_priv, bytes,
  267. false, 3 * HZ);
  268. if (unlikely(ret != 0))
  269. goto out_err;
  270. }
  271. }
  272. if (reserve_in_place) {
  273. if (reserveable || bytes <= sizeof(uint32_t)) {
  274. fifo_state->using_bounce_buffer = false;
  275. if (reserveable)
  276. iowrite32(bytes, fifo_mem +
  277. SVGA_FIFO_RESERVED);
  278. return fifo_mem + (next_cmd >> 2);
  279. } else {
  280. need_bounce = true;
  281. }
  282. }
  283. if (need_bounce) {
  284. fifo_state->using_bounce_buffer = true;
  285. if (bytes < fifo_state->static_buffer_size)
  286. return fifo_state->static_buffer;
  287. else {
  288. fifo_state->dynamic_buffer = vmalloc(bytes);
  289. return fifo_state->dynamic_buffer;
  290. }
  291. }
  292. }
  293. out_err:
  294. fifo_state->reserved_size = 0;
  295. mutex_unlock(&fifo_state->fifo_mutex);
  296. return NULL;
  297. }
  298. static void vmw_fifo_res_copy(struct vmw_fifo_state *fifo_state,
  299. __le32 __iomem *fifo_mem,
  300. uint32_t next_cmd,
  301. uint32_t max, uint32_t min, uint32_t bytes)
  302. {
  303. uint32_t chunk_size = max - next_cmd;
  304. uint32_t rest;
  305. uint32_t *buffer = (fifo_state->dynamic_buffer != NULL) ?
  306. fifo_state->dynamic_buffer : fifo_state->static_buffer;
  307. if (bytes < chunk_size)
  308. chunk_size = bytes;
  309. iowrite32(bytes, fifo_mem + SVGA_FIFO_RESERVED);
  310. mb();
  311. memcpy_toio(fifo_mem + (next_cmd >> 2), buffer, chunk_size);
  312. rest = bytes - chunk_size;
  313. if (rest)
  314. memcpy_toio(fifo_mem + (min >> 2), buffer + (chunk_size >> 2),
  315. rest);
  316. }
  317. static void vmw_fifo_slow_copy(struct vmw_fifo_state *fifo_state,
  318. __le32 __iomem *fifo_mem,
  319. uint32_t next_cmd,
  320. uint32_t max, uint32_t min, uint32_t bytes)
  321. {
  322. uint32_t *buffer = (fifo_state->dynamic_buffer != NULL) ?
  323. fifo_state->dynamic_buffer : fifo_state->static_buffer;
  324. while (bytes > 0) {
  325. iowrite32(*buffer++, fifo_mem + (next_cmd >> 2));
  326. next_cmd += sizeof(uint32_t);
  327. if (unlikely(next_cmd == max))
  328. next_cmd = min;
  329. mb();
  330. iowrite32(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
  331. mb();
  332. bytes -= sizeof(uint32_t);
  333. }
  334. }
  335. void vmw_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes)
  336. {
  337. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  338. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  339. uint32_t next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
  340. uint32_t max = ioread32(fifo_mem + SVGA_FIFO_MAX);
  341. uint32_t min = ioread32(fifo_mem + SVGA_FIFO_MIN);
  342. bool reserveable = fifo_state->capabilities & SVGA_FIFO_CAP_RESERVE;
  343. BUG_ON((bytes & 3) != 0);
  344. BUG_ON(bytes > fifo_state->reserved_size);
  345. fifo_state->reserved_size = 0;
  346. if (fifo_state->using_bounce_buffer) {
  347. if (reserveable)
  348. vmw_fifo_res_copy(fifo_state, fifo_mem,
  349. next_cmd, max, min, bytes);
  350. else
  351. vmw_fifo_slow_copy(fifo_state, fifo_mem,
  352. next_cmd, max, min, bytes);
  353. if (fifo_state->dynamic_buffer) {
  354. vfree(fifo_state->dynamic_buffer);
  355. fifo_state->dynamic_buffer = NULL;
  356. }
  357. }
  358. down_write(&fifo_state->rwsem);
  359. if (fifo_state->using_bounce_buffer || reserveable) {
  360. next_cmd += bytes;
  361. if (next_cmd >= max)
  362. next_cmd -= max - min;
  363. mb();
  364. iowrite32(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
  365. }
  366. if (reserveable)
  367. iowrite32(0, fifo_mem + SVGA_FIFO_RESERVED);
  368. mb();
  369. up_write(&fifo_state->rwsem);
  370. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  371. mutex_unlock(&fifo_state->fifo_mutex);
  372. }
  373. int vmw_fifo_send_fence(struct vmw_private *dev_priv, uint32_t *seqno)
  374. {
  375. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  376. struct svga_fifo_cmd_fence *cmd_fence;
  377. void *fm;
  378. int ret = 0;
  379. uint32_t bytes = sizeof(__le32) + sizeof(*cmd_fence);
  380. fm = vmw_fifo_reserve(dev_priv, bytes);
  381. if (unlikely(fm == NULL)) {
  382. *seqno = atomic_read(&dev_priv->marker_seq);
  383. ret = -ENOMEM;
  384. (void)vmw_fallback_wait(dev_priv, false, true, *seqno,
  385. false, 3*HZ);
  386. goto out_err;
  387. }
  388. do {
  389. *seqno = atomic_add_return(1, &dev_priv->marker_seq);
  390. } while (*seqno == 0);
  391. if (!(fifo_state->capabilities & SVGA_FIFO_CAP_FENCE)) {
  392. /*
  393. * Don't request hardware to send a fence. The
  394. * waiting code in vmwgfx_irq.c will emulate this.
  395. */
  396. vmw_fifo_commit(dev_priv, 0);
  397. return 0;
  398. }
  399. *(__le32 *) fm = cpu_to_le32(SVGA_CMD_FENCE);
  400. cmd_fence = (struct svga_fifo_cmd_fence *)
  401. ((unsigned long)fm + sizeof(__le32));
  402. iowrite32(*seqno, &cmd_fence->fence);
  403. vmw_fifo_commit(dev_priv, bytes);
  404. (void) vmw_marker_push(&fifo_state->marker_queue, *seqno);
  405. vmw_update_seqno(dev_priv, fifo_state);
  406. out_err:
  407. return ret;
  408. }