blob: cd5d9f3fe0e0969bde67f9ffb61779de1055e105 [file] [log] [blame]
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001/**************************************************************************
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
28#include "vmwgfx_drv.h"
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
30#include <drm/ttm/ttm_placement.h>
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000031
Jakob Bornecrantz8e19a952010-01-30 03:38:06 +000032bool vmw_fifo_have_3d(struct vmw_private *dev_priv)
33{
34 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
35 uint32_t fifo_min, hwversion;
Thomas Hellstromebd4c6f2011-11-28 13:19:08 +010036 const struct vmw_fifo_state *fifo = &dev_priv->fifo;
Jakob Bornecrantz8e19a952010-01-30 03:38:06 +000037
Thomas Hellstromd8c08b22012-11-21 12:18:31 +010038 if (!(dev_priv->capabilities & SVGA_CAP_3D))
39 return false;
40
41 if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS) {
42 uint32_t result;
43
44 if (!dev_priv->has_mob)
45 return false;
46
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -080047 spin_lock(&dev_priv->cap_lock);
Thomas Hellstromd8c08b22012-11-21 12:18:31 +010048 vmw_write(dev_priv, SVGA_REG_DEV_CAP, SVGA3D_DEVCAP_3D);
49 result = vmw_read(dev_priv, SVGA_REG_DEV_CAP);
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -080050 spin_unlock(&dev_priv->cap_lock);
Thomas Hellstromd8c08b22012-11-21 12:18:31 +010051
52 return (result != 0);
53 }
54
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +020055 if (!(dev_priv->capabilities & SVGA_CAP_EXTENDED_FIFO))
56 return false;
57
Jakob Bornecrantz8e19a952010-01-30 03:38:06 +000058 fifo_min = ioread32(fifo_mem + SVGA_FIFO_MIN);
59 if (fifo_min <= SVGA_FIFO_3D_HWVERSION * sizeof(unsigned int))
60 return false;
61
Thomas Hellstromebd4c6f2011-11-28 13:19:08 +010062 hwversion = ioread32(fifo_mem +
63 ((fifo->capabilities &
64 SVGA_FIFO_CAP_3D_HWVERSION_REVISED) ?
65 SVGA_FIFO_3D_HWVERSION_REVISED :
66 SVGA_FIFO_3D_HWVERSION));
67
Jakob Bornecrantz8e19a952010-01-30 03:38:06 +000068 if (hwversion == 0)
69 return false;
70
Thomas Hellstromb7b70022011-10-04 20:13:23 +020071 if (hwversion < SVGA3D_HWVERSION_WS8_B1)
Jakob Bornecrantz8e19a952010-01-30 03:38:06 +000072 return false;
73
Jakob Bornecrantz01e81412011-10-04 20:13:24 +020074 /* Non-Screen Object path does not support surfaces */
75 if (!dev_priv->sou_priv)
76 return false;
77
Jakob Bornecrantz8e19a952010-01-30 03:38:06 +000078 return true;
79}
80
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +020081bool vmw_fifo_have_pitchlock(struct vmw_private *dev_priv)
82{
83 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
84 uint32_t caps;
85
86 if (!(dev_priv->capabilities & SVGA_CAP_EXTENDED_FIFO))
87 return false;
88
89 caps = ioread32(fifo_mem + SVGA_FIFO_CAPABILITIES);
90 if (caps & SVGA_FIFO_CAP_PITCHLOCK)
91 return true;
92
93 return false;
94}
95
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000096int vmw_fifo_init(struct vmw_private *dev_priv, struct vmw_fifo_state *fifo)
97{
98 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
99 uint32_t max;
100 uint32_t min;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000101
102 fifo->static_buffer_size = VMWGFX_FIFO_STATIC_SIZE;
103 fifo->static_buffer = vmalloc(fifo->static_buffer_size);
104 if (unlikely(fifo->static_buffer == NULL))
105 return -ENOMEM;
106
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000107 fifo->dynamic_buffer = NULL;
108 fifo->reserved_size = 0;
109 fifo->using_bounce_buffer = false;
110
Thomas Hellstrom85b9e482010-02-08 09:57:25 +0000111 mutex_init(&fifo->fifo_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000112 init_rwsem(&fifo->rwsem);
113
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000114 DRM_INFO("width %d\n", vmw_read(dev_priv, SVGA_REG_WIDTH));
115 DRM_INFO("height %d\n", vmw_read(dev_priv, SVGA_REG_HEIGHT));
116 DRM_INFO("bpp %d\n", vmw_read(dev_priv, SVGA_REG_BITS_PER_PIXEL));
117
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000118 dev_priv->enable_state = vmw_read(dev_priv, SVGA_REG_ENABLE);
119 dev_priv->config_done_state = vmw_read(dev_priv, SVGA_REG_CONFIG_DONE);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200120 dev_priv->traces_state = vmw_read(dev_priv, SVGA_REG_TRACES);
Thomas Hellstrom153b3d52015-06-25 10:47:43 -0700121
122 vmw_write(dev_priv, SVGA_REG_ENABLE, SVGA_REG_ENABLE_ENABLE_HIDE);
123 vmw_write(dev_priv, SVGA_REG_TRACES, 0);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000124
125 min = 4;
126 if (dev_priv->capabilities & SVGA_CAP_EXTENDED_FIFO)
127 min = vmw_read(dev_priv, SVGA_REG_MEM_REGS);
128 min <<= 2;
129
130 if (min < PAGE_SIZE)
131 min = PAGE_SIZE;
132
133 iowrite32(min, fifo_mem + SVGA_FIFO_MIN);
134 iowrite32(dev_priv->mmio_size, fifo_mem + SVGA_FIFO_MAX);
135 wmb();
136 iowrite32(min, fifo_mem + SVGA_FIFO_NEXT_CMD);
137 iowrite32(min, fifo_mem + SVGA_FIFO_STOP);
138 iowrite32(0, fifo_mem + SVGA_FIFO_BUSY);
139 mb();
140
141 vmw_write(dev_priv, SVGA_REG_CONFIG_DONE, 1);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000142
143 max = ioread32(fifo_mem + SVGA_FIFO_MAX);
144 min = ioread32(fifo_mem + SVGA_FIFO_MIN);
145 fifo->capabilities = ioread32(fifo_mem + SVGA_FIFO_CAPABILITIES);
146
147 DRM_INFO("Fifo max 0x%08x min 0x%08x cap 0x%08x\n",
148 (unsigned int) max,
149 (unsigned int) min,
150 (unsigned int) fifo->capabilities);
151
Thomas Hellstrom6bcd8d3c2011-09-01 20:18:42 +0000152 atomic_set(&dev_priv->marker_seq, dev_priv->last_read_seqno);
153 iowrite32(dev_priv->last_read_seqno, fifo_mem + SVGA_FIFO_FENCE);
154 vmw_marker_queue_init(&fifo->marker_queue);
Thomas Hellstrom153b3d52015-06-25 10:47:43 -0700155
156 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000157}
158
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -0800159void vmw_fifo_ping_host(struct vmw_private *dev_priv, uint32_t reason)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000160{
161 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -0800162 static DEFINE_SPINLOCK(ping_lock);
163 unsigned long irq_flags;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000164
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -0800165 /*
166 * The ping_lock is needed because we don't have an atomic
167 * test-and-set of the SVGA_FIFO_BUSY register.
168 */
169 spin_lock_irqsave(&ping_lock, irq_flags);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000170 if (unlikely(ioread32(fifo_mem + SVGA_FIFO_BUSY) == 0)) {
171 iowrite32(1, fifo_mem + SVGA_FIFO_BUSY);
172 vmw_write(dev_priv, SVGA_REG_SYNC, reason);
173 }
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -0800174 spin_unlock_irqrestore(&ping_lock, irq_flags);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000175}
176
177void vmw_fifo_release(struct vmw_private *dev_priv, struct vmw_fifo_state *fifo)
178{
179 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
180
Thomas Hellstromf01ea0c2014-08-28 11:53:23 +0200181 vmw_write(dev_priv, SVGA_REG_SYNC, SVGA_SYNC_GENERIC);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000182 while (vmw_read(dev_priv, SVGA_REG_BUSY) != 0)
Thomas Hellstromf01ea0c2014-08-28 11:53:23 +0200183 ;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000184
Thomas Hellstrom6bcd8d3c2011-09-01 20:18:42 +0000185 dev_priv->last_read_seqno = ioread32(fifo_mem + SVGA_FIFO_FENCE);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000186
187 vmw_write(dev_priv, SVGA_REG_CONFIG_DONE,
188 dev_priv->config_done_state);
189 vmw_write(dev_priv, SVGA_REG_ENABLE,
190 dev_priv->enable_state);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200191 vmw_write(dev_priv, SVGA_REG_TRACES,
192 dev_priv->traces_state);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000193
Thomas Hellstrom6bcd8d3c2011-09-01 20:18:42 +0000194 vmw_marker_queue_takedown(&fifo->marker_queue);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000195
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000196 if (likely(fifo->static_buffer != NULL)) {
197 vfree(fifo->static_buffer);
198 fifo->static_buffer = NULL;
199 }
200
201 if (likely(fifo->dynamic_buffer != NULL)) {
202 vfree(fifo->dynamic_buffer);
203 fifo->dynamic_buffer = NULL;
204 }
205}
206
207static bool vmw_fifo_is_full(struct vmw_private *dev_priv, uint32_t bytes)
208{
209 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
210 uint32_t max = ioread32(fifo_mem + SVGA_FIFO_MAX);
211 uint32_t next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
212 uint32_t min = ioread32(fifo_mem + SVGA_FIFO_MIN);
213 uint32_t stop = ioread32(fifo_mem + SVGA_FIFO_STOP);
214
215 return ((max - next_cmd) + (stop - min) <= bytes);
216}
217
218static int vmw_fifo_wait_noirq(struct vmw_private *dev_priv,
219 uint32_t bytes, bool interruptible,
220 unsigned long timeout)
221{
222 int ret = 0;
223 unsigned long end_jiffies = jiffies + timeout;
224 DEFINE_WAIT(__wait);
225
226 DRM_INFO("Fifo wait noirq.\n");
227
228 for (;;) {
229 prepare_to_wait(&dev_priv->fifo_queue, &__wait,
230 (interruptible) ?
231 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
232 if (!vmw_fifo_is_full(dev_priv, bytes))
233 break;
234 if (time_after_eq(jiffies, end_jiffies)) {
235 ret = -EBUSY;
236 DRM_ERROR("SVGA device lockup.\n");
237 break;
238 }
239 schedule_timeout(1);
240 if (interruptible && signal_pending(current)) {
Thomas Hellstrom3d3a5b32009-12-08 12:59:34 +0100241 ret = -ERESTARTSYS;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000242 break;
243 }
244 }
245 finish_wait(&dev_priv->fifo_queue, &__wait);
246 wake_up_all(&dev_priv->fifo_queue);
247 DRM_INFO("Fifo noirq exit.\n");
248 return ret;
249}
250
251static int vmw_fifo_wait(struct vmw_private *dev_priv,
252 uint32_t bytes, bool interruptible,
253 unsigned long timeout)
254{
255 long ret = 1L;
256 unsigned long irq_flags;
257
258 if (likely(!vmw_fifo_is_full(dev_priv, bytes)))
259 return 0;
260
261 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_FIFOFULL);
262 if (!(dev_priv->capabilities & SVGA_CAP_IRQMASK))
263 return vmw_fifo_wait_noirq(dev_priv, bytes,
264 interruptible, timeout);
265
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -0800266 spin_lock(&dev_priv->waiter_lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000267 if (atomic_add_return(1, &dev_priv->fifo_queue_waiters) > 0) {
268 spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
269 outl(SVGA_IRQFLAG_FIFO_PROGRESS,
270 dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200271 dev_priv->irq_mask |= SVGA_IRQFLAG_FIFO_PROGRESS;
272 vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000273 spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
274 }
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -0800275 spin_unlock(&dev_priv->waiter_lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000276
277 if (interruptible)
278 ret = wait_event_interruptible_timeout
279 (dev_priv->fifo_queue,
280 !vmw_fifo_is_full(dev_priv, bytes), timeout);
281 else
282 ret = wait_event_timeout
283 (dev_priv->fifo_queue,
284 !vmw_fifo_is_full(dev_priv, bytes), timeout);
285
Thomas Hellstrom3d3a5b32009-12-08 12:59:34 +0100286 if (unlikely(ret == 0))
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000287 ret = -EBUSY;
288 else if (likely(ret > 0))
289 ret = 0;
290
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -0800291 spin_lock(&dev_priv->waiter_lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000292 if (atomic_dec_and_test(&dev_priv->fifo_queue_waiters)) {
293 spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200294 dev_priv->irq_mask &= ~SVGA_IRQFLAG_FIFO_PROGRESS;
295 vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000296 spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
297 }
Thomas Hellstrom496eb6f2015-01-14 02:33:39 -0800298 spin_unlock(&dev_priv->waiter_lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000299
300 return ret;
301}
302
Jakob Bornecrantzde12d442011-10-04 20:13:13 +0200303/**
304 * Reserve @bytes number of bytes in the fifo.
305 *
306 * This function will return NULL (error) on two conditions:
307 * If it timeouts waiting for fifo space, or if @bytes is larger than the
308 * available fifo space.
309 *
310 * Returns:
311 * Pointer to the fifo, or null on error (possible hardware hang).
312 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000313void *vmw_fifo_reserve(struct vmw_private *dev_priv, uint32_t bytes)
314{
315 struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
316 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
317 uint32_t max;
318 uint32_t min;
319 uint32_t next_cmd;
320 uint32_t reserveable = fifo_state->capabilities & SVGA_FIFO_CAP_RESERVE;
321 int ret;
322
Thomas Hellstrom85b9e482010-02-08 09:57:25 +0000323 mutex_lock(&fifo_state->fifo_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000324 max = ioread32(fifo_mem + SVGA_FIFO_MAX);
325 min = ioread32(fifo_mem + SVGA_FIFO_MIN);
326 next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
327
328 if (unlikely(bytes >= (max - min)))
329 goto out_err;
330
331 BUG_ON(fifo_state->reserved_size != 0);
332 BUG_ON(fifo_state->dynamic_buffer != NULL);
333
334 fifo_state->reserved_size = bytes;
335
336 while (1) {
337 uint32_t stop = ioread32(fifo_mem + SVGA_FIFO_STOP);
338 bool need_bounce = false;
339 bool reserve_in_place = false;
340
341 if (next_cmd >= stop) {
342 if (likely((next_cmd + bytes < max ||
343 (next_cmd + bytes == max && stop > min))))
344 reserve_in_place = true;
345
346 else if (vmw_fifo_is_full(dev_priv, bytes)) {
347 ret = vmw_fifo_wait(dev_priv, bytes,
348 false, 3 * HZ);
349 if (unlikely(ret != 0))
350 goto out_err;
351 } else
352 need_bounce = true;
353
354 } else {
355
356 if (likely((next_cmd + bytes < stop)))
357 reserve_in_place = true;
358 else {
359 ret = vmw_fifo_wait(dev_priv, bytes,
360 false, 3 * HZ);
361 if (unlikely(ret != 0))
362 goto out_err;
363 }
364 }
365
366 if (reserve_in_place) {
367 if (reserveable || bytes <= sizeof(uint32_t)) {
368 fifo_state->using_bounce_buffer = false;
369
370 if (reserveable)
371 iowrite32(bytes, fifo_mem +
372 SVGA_FIFO_RESERVED);
373 return fifo_mem + (next_cmd >> 2);
374 } else {
375 need_bounce = true;
376 }
377 }
378
379 if (need_bounce) {
380 fifo_state->using_bounce_buffer = true;
381 if (bytes < fifo_state->static_buffer_size)
382 return fifo_state->static_buffer;
383 else {
384 fifo_state->dynamic_buffer = vmalloc(bytes);
385 return fifo_state->dynamic_buffer;
386 }
387 }
388 }
389out_err:
390 fifo_state->reserved_size = 0;
Thomas Hellstrom85b9e482010-02-08 09:57:25 +0000391 mutex_unlock(&fifo_state->fifo_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000392 return NULL;
393}
394
395static void vmw_fifo_res_copy(struct vmw_fifo_state *fifo_state,
396 __le32 __iomem *fifo_mem,
397 uint32_t next_cmd,
398 uint32_t max, uint32_t min, uint32_t bytes)
399{
400 uint32_t chunk_size = max - next_cmd;
401 uint32_t rest;
402 uint32_t *buffer = (fifo_state->dynamic_buffer != NULL) ?
403 fifo_state->dynamic_buffer : fifo_state->static_buffer;
404
405 if (bytes < chunk_size)
406 chunk_size = bytes;
407
408 iowrite32(bytes, fifo_mem + SVGA_FIFO_RESERVED);
409 mb();
410 memcpy_toio(fifo_mem + (next_cmd >> 2), buffer, chunk_size);
411 rest = bytes - chunk_size;
412 if (rest)
413 memcpy_toio(fifo_mem + (min >> 2), buffer + (chunk_size >> 2),
414 rest);
415}
416
417static void vmw_fifo_slow_copy(struct vmw_fifo_state *fifo_state,
418 __le32 __iomem *fifo_mem,
419 uint32_t next_cmd,
420 uint32_t max, uint32_t min, uint32_t bytes)
421{
422 uint32_t *buffer = (fifo_state->dynamic_buffer != NULL) ?
423 fifo_state->dynamic_buffer : fifo_state->static_buffer;
424
425 while (bytes > 0) {
426 iowrite32(*buffer++, fifo_mem + (next_cmd >> 2));
427 next_cmd += sizeof(uint32_t);
428 if (unlikely(next_cmd == max))
429 next_cmd = min;
430 mb();
431 iowrite32(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
432 mb();
433 bytes -= sizeof(uint32_t);
434 }
435}
436
437void vmw_fifo_commit(struct vmw_private *dev_priv, uint32_t bytes)
438{
439 struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
440 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
441 uint32_t next_cmd = ioread32(fifo_mem + SVGA_FIFO_NEXT_CMD);
442 uint32_t max = ioread32(fifo_mem + SVGA_FIFO_MAX);
443 uint32_t min = ioread32(fifo_mem + SVGA_FIFO_MIN);
444 bool reserveable = fifo_state->capabilities & SVGA_FIFO_CAP_RESERVE;
445
446 BUG_ON((bytes & 3) != 0);
447 BUG_ON(bytes > fifo_state->reserved_size);
448
449 fifo_state->reserved_size = 0;
450
451 if (fifo_state->using_bounce_buffer) {
452 if (reserveable)
453 vmw_fifo_res_copy(fifo_state, fifo_mem,
454 next_cmd, max, min, bytes);
455 else
456 vmw_fifo_slow_copy(fifo_state, fifo_mem,
457 next_cmd, max, min, bytes);
458
459 if (fifo_state->dynamic_buffer) {
460 vfree(fifo_state->dynamic_buffer);
461 fifo_state->dynamic_buffer = NULL;
462 }
463
464 }
465
Thomas Hellstrom85b9e482010-02-08 09:57:25 +0000466 down_write(&fifo_state->rwsem);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000467 if (fifo_state->using_bounce_buffer || reserveable) {
468 next_cmd += bytes;
469 if (next_cmd >= max)
470 next_cmd -= max - min;
471 mb();
472 iowrite32(next_cmd, fifo_mem + SVGA_FIFO_NEXT_CMD);
473 }
474
475 if (reserveable)
476 iowrite32(0, fifo_mem + SVGA_FIFO_RESERVED);
477 mb();
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000478 up_write(&fifo_state->rwsem);
Thomas Hellstrom85b9e482010-02-08 09:57:25 +0000479 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
480 mutex_unlock(&fifo_state->fifo_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000481}
482
Thomas Hellstrom6bcd8d3c2011-09-01 20:18:42 +0000483int vmw_fifo_send_fence(struct vmw_private *dev_priv, uint32_t *seqno)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000484{
485 struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
486 struct svga_fifo_cmd_fence *cmd_fence;
487 void *fm;
488 int ret = 0;
489 uint32_t bytes = sizeof(__le32) + sizeof(*cmd_fence);
490
491 fm = vmw_fifo_reserve(dev_priv, bytes);
492 if (unlikely(fm == NULL)) {
Thomas Hellstrom6bcd8d3c2011-09-01 20:18:42 +0000493 *seqno = atomic_read(&dev_priv->marker_seq);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000494 ret = -ENOMEM;
Thomas Hellstrom6bcd8d3c2011-09-01 20:18:42 +0000495 (void)vmw_fallback_wait(dev_priv, false, true, *seqno,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000496 false, 3*HZ);
497 goto out_err;
498 }
499
500 do {
Thomas Hellstrom6bcd8d3c2011-09-01 20:18:42 +0000501 *seqno = atomic_add_return(1, &dev_priv->marker_seq);
502 } while (*seqno == 0);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000503
504 if (!(fifo_state->capabilities & SVGA_FIFO_CAP_FENCE)) {
505
506 /*
507 * Don't request hardware to send a fence. The
508 * waiting code in vmwgfx_irq.c will emulate this.
509 */
510
511 vmw_fifo_commit(dev_priv, 0);
512 return 0;
513 }
514
515 *(__le32 *) fm = cpu_to_le32(SVGA_CMD_FENCE);
516 cmd_fence = (struct svga_fifo_cmd_fence *)
517 ((unsigned long)fm + sizeof(__le32));
518
Thomas Hellstrom6bcd8d3c2011-09-01 20:18:42 +0000519 iowrite32(*seqno, &cmd_fence->fence);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000520 vmw_fifo_commit(dev_priv, bytes);
Thomas Hellstrom6bcd8d3c2011-09-01 20:18:42 +0000521 (void) vmw_marker_push(&fifo_state->marker_queue, *seqno);
522 vmw_update_seqno(dev_priv, fifo_state);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000523
524out_err:
525 return ret;
526}
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200527
528/**
Thomas Hellstromddcda242012-11-21 11:26:55 +0100529 * vmw_fifo_emit_dummy_legacy_query - emits a dummy query to the fifo using
530 * legacy query commands.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200531 *
532 * @dev_priv: The device private structure.
533 * @cid: The hardware context id used for the query.
534 *
Thomas Hellstromddcda242012-11-21 11:26:55 +0100535 * See the vmw_fifo_emit_dummy_query documentation.
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200536 */
Thomas Hellstromddcda242012-11-21 11:26:55 +0100537static int vmw_fifo_emit_dummy_legacy_query(struct vmw_private *dev_priv,
538 uint32_t cid)
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200539{
540 /*
541 * A query wait without a preceding query end will
542 * actually finish all queries for this cid
543 * without writing to the query result structure.
544 */
545
546 struct ttm_buffer_object *bo = dev_priv->dummy_query_bo;
547 struct {
548 SVGA3dCmdHeader header;
549 SVGA3dCmdWaitForQuery body;
550 } *cmd;
551
552 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
553
554 if (unlikely(cmd == NULL)) {
555 DRM_ERROR("Out of fifo space for dummy query.\n");
556 return -ENOMEM;
557 }
558
559 cmd->header.id = SVGA_3D_CMD_WAIT_FOR_QUERY;
560 cmd->header.size = sizeof(cmd->body);
561 cmd->body.cid = cid;
562 cmd->body.type = SVGA3D_QUERYTYPE_OCCLUSION;
563
564 if (bo->mem.mem_type == TTM_PL_VRAM) {
565 cmd->body.guestResult.gmrId = SVGA_GMR_FRAMEBUFFER;
566 cmd->body.guestResult.offset = bo->offset;
567 } else {
568 cmd->body.guestResult.gmrId = bo->mem.start;
569 cmd->body.guestResult.offset = 0;
570 }
571
572 vmw_fifo_commit(dev_priv, sizeof(*cmd));
573
574 return 0;
575}
Thomas Hellstromddcda242012-11-21 11:26:55 +0100576
577/**
578 * vmw_fifo_emit_dummy_gb_query - emits a dummy query to the fifo using
579 * guest-backed resource query commands.
580 *
581 * @dev_priv: The device private structure.
582 * @cid: The hardware context id used for the query.
583 *
584 * See the vmw_fifo_emit_dummy_query documentation.
585 */
586static int vmw_fifo_emit_dummy_gb_query(struct vmw_private *dev_priv,
587 uint32_t cid)
588{
589 /*
590 * A query wait without a preceding query end will
591 * actually finish all queries for this cid
592 * without writing to the query result structure.
593 */
594
595 struct ttm_buffer_object *bo = dev_priv->dummy_query_bo;
596 struct {
597 SVGA3dCmdHeader header;
598 SVGA3dCmdWaitForGBQuery body;
599 } *cmd;
600
601 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
602
603 if (unlikely(cmd == NULL)) {
604 DRM_ERROR("Out of fifo space for dummy query.\n");
605 return -ENOMEM;
606 }
607
608 cmd->header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
609 cmd->header.size = sizeof(cmd->body);
610 cmd->body.cid = cid;
611 cmd->body.type = SVGA3D_QUERYTYPE_OCCLUSION;
612 BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
613 cmd->body.mobid = bo->mem.start;
614 cmd->body.offset = 0;
615
616 vmw_fifo_commit(dev_priv, sizeof(*cmd));
617
618 return 0;
619}
620
621
622/**
623 * vmw_fifo_emit_dummy_gb_query - emits a dummy query to the fifo using
624 * appropriate resource query commands.
625 *
626 * @dev_priv: The device private structure.
627 * @cid: The hardware context id used for the query.
628 *
629 * This function is used to emit a dummy occlusion query with
630 * no primitives rendered between query begin and query end.
631 * It's used to provide a query barrier, in order to know that when
632 * this query is finished, all preceding queries are also finished.
633 *
634 * A Query results structure should have been initialized at the start
635 * of the dev_priv->dummy_query_bo buffer object. And that buffer object
636 * must also be either reserved or pinned when this function is called.
637 *
638 * Returns -ENOMEM on failure to reserve fifo space.
639 */
640int vmw_fifo_emit_dummy_query(struct vmw_private *dev_priv,
641 uint32_t cid)
642{
643 if (dev_priv->has_mob)
644 return vmw_fifo_emit_dummy_gb_query(dev_priv, cid);
645
646 return vmw_fifo_emit_dummy_legacy_query(dev_priv, cid);
647}