1 /**************************************************************************
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
30 #include "ttm/ttm_placement.h"
32 bool vmw_fifo_have_3d(struct vmw_private
*dev_priv
)
34 __le32 __iomem
*fifo_mem
= dev_priv
->mmio_virt
;
35 uint32_t fifo_min
, hwversion
;
37 fifo_min
= ioread32(fifo_mem
+ SVGA_FIFO_MIN
);
38 if (fifo_min
<= SVGA_FIFO_3D_HWVERSION
* sizeof(unsigned int))
41 hwversion
= ioread32(fifo_mem
+ SVGA_FIFO_3D_HWVERSION
);
45 if (hwversion
< SVGA3D_HWVERSION_WS65_B1
)
51 int vmw_fifo_init(struct vmw_private
*dev_priv
, struct vmw_fifo_state
*fifo
)
53 __le32 __iomem
*fifo_mem
= dev_priv
->mmio_virt
;
59 fifo
->static_buffer_size
= VMWGFX_FIFO_STATIC_SIZE
;
60 fifo
->static_buffer
= vmalloc(fifo
->static_buffer_size
);
61 if (unlikely(fifo
->static_buffer
== NULL
))
64 fifo
->last_buffer_size
= VMWGFX_FIFO_STATIC_SIZE
;
65 fifo
->last_data_size
= 0;
66 fifo
->last_buffer_add
= false;
67 fifo
->last_buffer
= vmalloc(fifo
->last_buffer_size
);
68 if (unlikely(fifo
->last_buffer
== NULL
)) {
73 fifo
->dynamic_buffer
= NULL
;
74 fifo
->reserved_size
= 0;
75 fifo
->using_bounce_buffer
= false;
77 mutex_init(&fifo
->fifo_mutex
);
78 init_rwsem(&fifo
->rwsem
);
81 * Allow mapping the first page read-only to user-space.
84 DRM_INFO("width %d\n", vmw_read(dev_priv
, SVGA_REG_WIDTH
));
85 DRM_INFO("height %d\n", vmw_read(dev_priv
, SVGA_REG_HEIGHT
));
86 DRM_INFO("bpp %d\n", vmw_read(dev_priv
, SVGA_REG_BITS_PER_PIXEL
));
88 mutex_lock(&dev_priv
->hw_mutex
);
89 dev_priv
->enable_state
= vmw_read(dev_priv
, SVGA_REG_ENABLE
);
90 dev_priv
->config_done_state
= vmw_read(dev_priv
, SVGA_REG_CONFIG_DONE
);
91 vmw_write(dev_priv
, SVGA_REG_ENABLE
, 1);
94 if (dev_priv
->capabilities
& SVGA_CAP_EXTENDED_FIFO
)
95 min
= vmw_read(dev_priv
, SVGA_REG_MEM_REGS
);
101 iowrite32(min
, fifo_mem
+ SVGA_FIFO_MIN
);
102 iowrite32(dev_priv
->mmio_size
, fifo_mem
+ SVGA_FIFO_MAX
);
104 iowrite32(min
, fifo_mem
+ SVGA_FIFO_NEXT_CMD
);
105 iowrite32(min
, fifo_mem
+ SVGA_FIFO_STOP
);
106 iowrite32(0, fifo_mem
+ SVGA_FIFO_BUSY
);
109 vmw_write(dev_priv
, SVGA_REG_CONFIG_DONE
, 1);
110 mutex_unlock(&dev_priv
->hw_mutex
);
112 max
= ioread32(fifo_mem
+ SVGA_FIFO_MAX
);
113 min
= ioread32(fifo_mem
+ SVGA_FIFO_MIN
);
114 fifo
->capabilities
= ioread32(fifo_mem
+ SVGA_FIFO_CAPABILITIES
);
116 DRM_INFO("Fifo max 0x%08x min 0x%08x cap 0x%08x\n",
119 (unsigned int) fifo
->capabilities
);
121 atomic_set(&dev_priv
->fence_seq
, dev_priv
->last_read_sequence
);
122 iowrite32(dev_priv
->last_read_sequence
, fifo_mem
+ SVGA_FIFO_FENCE
);
124 return vmw_fifo_send_fence(dev_priv
, &dummy
);
126 vfree(fifo
->static_buffer
);
127 fifo
->static_buffer
= NULL
;
131 void vmw_fifo_ping_host(struct vmw_private
*dev_priv
, uint32_t reason
)
133 __le32 __iomem
*fifo_mem
= dev_priv
->mmio_virt
;
135 mutex_lock(&dev_priv
->hw_mutex
);
137 if (unlikely(ioread32(fifo_mem
+ SVGA_FIFO_BUSY
) == 0)) {
138 iowrite32(1, fifo_mem
+ SVGA_FIFO_BUSY
);
139 vmw_write(dev_priv
, SVGA_REG_SYNC
, reason
);
142 mutex_unlock(&dev_priv
->hw_mutex
);
145 void vmw_fifo_release(struct vmw_private
*dev_priv
, struct vmw_fifo_state
*fifo
)
147 __le32 __iomem
*fifo_mem
= dev_priv
->mmio_virt
;
149 mutex_lock(&dev_priv
->hw_mutex
);
151 while (vmw_read(dev_priv
, SVGA_REG_BUSY
) != 0)
152 vmw_write(dev_priv
, SVGA_REG_SYNC
, SVGA_SYNC_GENERIC
);
154 dev_priv
->last_read_sequence
= ioread32(fifo_mem
+ SVGA_FIFO_FENCE
);
156 vmw_write(dev_priv
, SVGA_REG_CONFIG_DONE
,
157 dev_priv
->config_done_state
);
158 vmw_write(dev_priv
, SVGA_REG_ENABLE
,
159 dev_priv
->enable_state
);
161 mutex_unlock(&dev_priv
->hw_mutex
);
163 if (likely(fifo
->last_buffer
!= NULL
)) {
164 vfree(fifo
->last_buffer
);
165 fifo
->last_buffer
= NULL
;
168 if (likely(fifo
->static_buffer
!= NULL
)) {
169 vfree(fifo
->static_buffer
);
170 fifo
->static_buffer
= NULL
;
173 if (likely(fifo
->dynamic_buffer
!= NULL
)) {
174 vfree(fifo
->dynamic_buffer
);
175 fifo
->dynamic_buffer
= NULL
;
179 static bool vmw_fifo_is_full(struct vmw_private
*dev_priv
, uint32_t bytes
)
181 __le32 __iomem
*fifo_mem
= dev_priv
->mmio_virt
;
182 uint32_t max
= ioread32(fifo_mem
+ SVGA_FIFO_MAX
);
183 uint32_t next_cmd
= ioread32(fifo_mem
+ SVGA_FIFO_NEXT_CMD
);
184 uint32_t min
= ioread32(fifo_mem
+ SVGA_FIFO_MIN
);
185 uint32_t stop
= ioread32(fifo_mem
+ SVGA_FIFO_STOP
);
187 return ((max
- next_cmd
) + (stop
- min
) <= bytes
);
190 static int vmw_fifo_wait_noirq(struct vmw_private
*dev_priv
,
191 uint32_t bytes
, bool interruptible
,
192 unsigned long timeout
)
195 unsigned long end_jiffies
= jiffies
+ timeout
;
198 DRM_INFO("Fifo wait noirq.\n");
201 prepare_to_wait(&dev_priv
->fifo_queue
, &__wait
,
203 TASK_INTERRUPTIBLE
: TASK_UNINTERRUPTIBLE
);
204 if (!vmw_fifo_is_full(dev_priv
, bytes
))
206 if (time_after_eq(jiffies
, end_jiffies
)) {
208 DRM_ERROR("SVGA device lockup.\n");
212 if (interruptible
&& signal_pending(current
)) {
217 finish_wait(&dev_priv
->fifo_queue
, &__wait
);
218 wake_up_all(&dev_priv
->fifo_queue
);
219 DRM_INFO("Fifo noirq exit.\n");
223 static int vmw_fifo_wait(struct vmw_private
*dev_priv
,
224 uint32_t bytes
, bool interruptible
,
225 unsigned long timeout
)
228 unsigned long irq_flags
;
230 if (likely(!vmw_fifo_is_full(dev_priv
, bytes
)))
233 vmw_fifo_ping_host(dev_priv
, SVGA_SYNC_FIFOFULL
);
234 if (!(dev_priv
->capabilities
& SVGA_CAP_IRQMASK
))
235 return vmw_fifo_wait_noirq(dev_priv
, bytes
,
236 interruptible
, timeout
);
238 mutex_lock(&dev_priv
->hw_mutex
);
239 if (atomic_add_return(1, &dev_priv
->fifo_queue_waiters
) > 0) {
240 spin_lock_irqsave(&dev_priv
->irq_lock
, irq_flags
);
241 outl(SVGA_IRQFLAG_FIFO_PROGRESS
,
242 dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
243 vmw_write(dev_priv
, SVGA_REG_IRQMASK
,
244 vmw_read(dev_priv
, SVGA_REG_IRQMASK
) |
245 SVGA_IRQFLAG_FIFO_PROGRESS
);
246 spin_unlock_irqrestore(&dev_priv
->irq_lock
, irq_flags
);
248 mutex_unlock(&dev_priv
->hw_mutex
);
251 ret
= wait_event_interruptible_timeout
252 (dev_priv
->fifo_queue
,
253 !vmw_fifo_is_full(dev_priv
, bytes
), timeout
);
255 ret
= wait_event_timeout
256 (dev_priv
->fifo_queue
,
257 !vmw_fifo_is_full(dev_priv
, bytes
), timeout
);
259 if (unlikely(ret
== 0))
261 else if (likely(ret
> 0))
264 mutex_lock(&dev_priv
->hw_mutex
);
265 if (atomic_dec_and_test(&dev_priv
->fifo_queue_waiters
)) {
266 spin_lock_irqsave(&dev_priv
->irq_lock
, irq_flags
);
267 vmw_write(dev_priv
, SVGA_REG_IRQMASK
,
268 vmw_read(dev_priv
, SVGA_REG_IRQMASK
) &
269 ~SVGA_IRQFLAG_FIFO_PROGRESS
);
270 spin_unlock_irqrestore(&dev_priv
->irq_lock
, irq_flags
);
272 mutex_unlock(&dev_priv
->hw_mutex
);
277 void *vmw_fifo_reserve(struct vmw_private
*dev_priv
, uint32_t bytes
)
279 struct vmw_fifo_state
*fifo_state
= &dev_priv
->fifo
;
280 __le32 __iomem
*fifo_mem
= dev_priv
->mmio_virt
;
284 uint32_t reserveable
= fifo_state
->capabilities
& SVGA_FIFO_CAP_RESERVE
;
287 mutex_lock(&fifo_state
->fifo_mutex
);
288 max
= ioread32(fifo_mem
+ SVGA_FIFO_MAX
);
289 min
= ioread32(fifo_mem
+ SVGA_FIFO_MIN
);
290 next_cmd
= ioread32(fifo_mem
+ SVGA_FIFO_NEXT_CMD
);
292 if (unlikely(bytes
>= (max
- min
)))
295 BUG_ON(fifo_state
->reserved_size
!= 0);
296 BUG_ON(fifo_state
->dynamic_buffer
!= NULL
);
298 fifo_state
->reserved_size
= bytes
;
301 uint32_t stop
= ioread32(fifo_mem
+ SVGA_FIFO_STOP
);
302 bool need_bounce
= false;
303 bool reserve_in_place
= false;
305 if (next_cmd
>= stop
) {
306 if (likely((next_cmd
+ bytes
< max
||
307 (next_cmd
+ bytes
== max
&& stop
> min
))))
308 reserve_in_place
= true;
310 else if (vmw_fifo_is_full(dev_priv
, bytes
)) {
311 ret
= vmw_fifo_wait(dev_priv
, bytes
,
313 if (unlikely(ret
!= 0))
320 if (likely((next_cmd
+ bytes
< stop
)))
321 reserve_in_place
= true;
323 ret
= vmw_fifo_wait(dev_priv
, bytes
,
325 if (unlikely(ret
!= 0))
330 if (reserve_in_place
) {
331 if (reserveable
|| bytes
<= sizeof(uint32_t)) {
332 fifo_state
->using_bounce_buffer
= false;
335 iowrite32(bytes
, fifo_mem
+
337 return fifo_mem
+ (next_cmd
>> 2);
344 fifo_state
->using_bounce_buffer
= true;
345 if (bytes
< fifo_state
->static_buffer_size
)
346 return fifo_state
->static_buffer
;
348 fifo_state
->dynamic_buffer
= vmalloc(bytes
);
349 return fifo_state
->dynamic_buffer
;
354 fifo_state
->reserved_size
= 0;
355 mutex_unlock(&fifo_state
->fifo_mutex
);
359 static void vmw_fifo_res_copy(struct vmw_fifo_state
*fifo_state
,
360 __le32 __iomem
*fifo_mem
,
362 uint32_t max
, uint32_t min
, uint32_t bytes
)
364 uint32_t chunk_size
= max
- next_cmd
;
366 uint32_t *buffer
= (fifo_state
->dynamic_buffer
!= NULL
) ?
367 fifo_state
->dynamic_buffer
: fifo_state
->static_buffer
;
369 if (bytes
< chunk_size
)
372 iowrite32(bytes
, fifo_mem
+ SVGA_FIFO_RESERVED
);
374 memcpy_toio(fifo_mem
+ (next_cmd
>> 2), buffer
, chunk_size
);
375 rest
= bytes
- chunk_size
;
377 memcpy_toio(fifo_mem
+ (min
>> 2), buffer
+ (chunk_size
>> 2),
381 static void vmw_fifo_slow_copy(struct vmw_fifo_state
*fifo_state
,
382 __le32 __iomem
*fifo_mem
,
384 uint32_t max
, uint32_t min
, uint32_t bytes
)
386 uint32_t *buffer
= (fifo_state
->dynamic_buffer
!= NULL
) ?
387 fifo_state
->dynamic_buffer
: fifo_state
->static_buffer
;
390 iowrite32(*buffer
++, fifo_mem
+ (next_cmd
>> 2));
391 next_cmd
+= sizeof(uint32_t);
392 if (unlikely(next_cmd
== max
))
395 iowrite32(next_cmd
, fifo_mem
+ SVGA_FIFO_NEXT_CMD
);
397 bytes
-= sizeof(uint32_t);
401 void vmw_fifo_commit(struct vmw_private
*dev_priv
, uint32_t bytes
)
403 struct vmw_fifo_state
*fifo_state
= &dev_priv
->fifo
;
404 __le32 __iomem
*fifo_mem
= dev_priv
->mmio_virt
;
405 uint32_t next_cmd
= ioread32(fifo_mem
+ SVGA_FIFO_NEXT_CMD
);
406 uint32_t max
= ioread32(fifo_mem
+ SVGA_FIFO_MAX
);
407 uint32_t min
= ioread32(fifo_mem
+ SVGA_FIFO_MIN
);
408 bool reserveable
= fifo_state
->capabilities
& SVGA_FIFO_CAP_RESERVE
;
410 BUG_ON((bytes
& 3) != 0);
411 BUG_ON(bytes
> fifo_state
->reserved_size
);
413 fifo_state
->reserved_size
= 0;
415 if (fifo_state
->using_bounce_buffer
) {
417 vmw_fifo_res_copy(fifo_state
, fifo_mem
,
418 next_cmd
, max
, min
, bytes
);
420 vmw_fifo_slow_copy(fifo_state
, fifo_mem
,
421 next_cmd
, max
, min
, bytes
);
423 if (fifo_state
->dynamic_buffer
) {
424 vfree(fifo_state
->dynamic_buffer
);
425 fifo_state
->dynamic_buffer
= NULL
;
430 down_write(&fifo_state
->rwsem
);
431 if (fifo_state
->using_bounce_buffer
|| reserveable
) {
434 next_cmd
-= max
- min
;
436 iowrite32(next_cmd
, fifo_mem
+ SVGA_FIFO_NEXT_CMD
);
440 iowrite32(0, fifo_mem
+ SVGA_FIFO_RESERVED
);
442 up_write(&fifo_state
->rwsem
);
443 vmw_fifo_ping_host(dev_priv
, SVGA_SYNC_GENERIC
);
444 mutex_unlock(&fifo_state
->fifo_mutex
);
447 int vmw_fifo_send_fence(struct vmw_private
*dev_priv
, uint32_t *sequence
)
449 struct vmw_fifo_state
*fifo_state
= &dev_priv
->fifo
;
450 struct svga_fifo_cmd_fence
*cmd_fence
;
453 uint32_t bytes
= sizeof(__le32
) + sizeof(*cmd_fence
);
455 fm
= vmw_fifo_reserve(dev_priv
, bytes
);
456 if (unlikely(fm
== NULL
)) {
457 *sequence
= atomic_read(&dev_priv
->fence_seq
);
459 (void)vmw_fallback_wait(dev_priv
, false, true, *sequence
,
465 *sequence
= atomic_add_return(1, &dev_priv
->fence_seq
);
466 } while (*sequence
== 0);
468 if (!(fifo_state
->capabilities
& SVGA_FIFO_CAP_FENCE
)) {
471 * Don't request hardware to send a fence. The
472 * waiting code in vmwgfx_irq.c will emulate this.
475 vmw_fifo_commit(dev_priv
, 0);
479 *(__le32
*) fm
= cpu_to_le32(SVGA_CMD_FENCE
);
480 cmd_fence
= (struct svga_fifo_cmd_fence
*)
481 ((unsigned long)fm
+ sizeof(__le32
));
483 iowrite32(*sequence
, &cmd_fence
->fence
);
484 fifo_state
->last_buffer_add
= true;
485 vmw_fifo_commit(dev_priv
, bytes
);
486 fifo_state
->last_buffer_add
= false;
493 * Map the first page of the FIFO read-only to user-space.
496 static int vmw_fifo_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
499 unsigned long address
= (unsigned long)vmf
->virtual_address
;
501 if (address
!= vma
->vm_start
)
502 return VM_FAULT_SIGBUS
;
504 ret
= vm_insert_pfn(vma
, address
, vma
->vm_pgoff
);
505 if (likely(ret
== -EBUSY
|| ret
== 0))
506 return VM_FAULT_NOPAGE
;
507 else if (ret
== -ENOMEM
)
510 return VM_FAULT_SIGBUS
;
513 static struct vm_operations_struct vmw_fifo_vm_ops
= {
514 .fault
= vmw_fifo_vm_fault
,
519 int vmw_fifo_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
521 struct drm_file
*file_priv
;
522 struct vmw_private
*dev_priv
;
524 file_priv
= (struct drm_file
*)filp
->private_data
;
525 dev_priv
= vmw_priv(file_priv
->minor
->dev
);
527 if (vma
->vm_pgoff
!= (dev_priv
->mmio_start
>> PAGE_SHIFT
) ||
528 (vma
->vm_end
- vma
->vm_start
) != PAGE_SIZE
)
531 vma
->vm_flags
&= ~(VM_WRITE
| VM_MAYWRITE
);
532 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
| VM_DONTEXPAND
| VM_SHARED
;
533 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
534 vma
->vm_page_prot
= ttm_io_prot(TTM_PL_FLAG_UNCACHED
,
536 vma
->vm_ops
= &vmw_fifo_vm_ops
;