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 **************************************************************************/
29 #include "vmwgfx_drv.h"
31 #define VMW_FENCE_WRAP (1 << 24)
33 irqreturn_t
vmw_irq_handler(DRM_IRQ_ARGS
)
35 struct drm_device
*dev
= (struct drm_device
*)arg
;
36 struct vmw_private
*dev_priv
= vmw_priv(dev
);
37 uint32_t status
, masked_status
;
39 spin_lock(&dev_priv
->irq_lock
);
40 status
= inl(dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
41 masked_status
= status
& dev_priv
->irq_mask
;
42 spin_unlock(&dev_priv
->irq_lock
);
45 outl(status
, dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
50 if (masked_status
& (SVGA_IRQFLAG_ANY_FENCE
|
51 SVGA_IRQFLAG_FENCE_GOAL
)) {
52 vmw_fences_update(dev_priv
->fman
);
53 wake_up_all(&dev_priv
->fence_queue
);
56 if (masked_status
& SVGA_IRQFLAG_FIFO_PROGRESS
)
57 wake_up_all(&dev_priv
->fifo_queue
);
63 static bool vmw_fifo_idle(struct vmw_private
*dev_priv
, uint32_t seqno
)
67 mutex_lock(&dev_priv
->hw_mutex
);
68 busy
= vmw_read(dev_priv
, SVGA_REG_BUSY
);
69 mutex_unlock(&dev_priv
->hw_mutex
);
74 void vmw_update_seqno(struct vmw_private
*dev_priv
,
75 struct vmw_fifo_state
*fifo_state
)
77 __le32 __iomem
*fifo_mem
= dev_priv
->mmio_virt
;
78 uint32_t seqno
= ioread32(fifo_mem
+ SVGA_FIFO_FENCE
);
80 if (dev_priv
->last_read_seqno
!= seqno
) {
81 dev_priv
->last_read_seqno
= seqno
;
82 vmw_marker_pull(&fifo_state
->marker_queue
, seqno
);
83 vmw_fences_update(dev_priv
->fman
);
87 bool vmw_seqno_passed(struct vmw_private
*dev_priv
,
90 struct vmw_fifo_state
*fifo_state
;
93 if (likely(dev_priv
->last_read_seqno
- seqno
< VMW_FENCE_WRAP
))
96 fifo_state
= &dev_priv
->fifo
;
97 vmw_update_seqno(dev_priv
, fifo_state
);
98 if (likely(dev_priv
->last_read_seqno
- seqno
< VMW_FENCE_WRAP
))
101 if (!(fifo_state
->capabilities
& SVGA_FIFO_CAP_FENCE
) &&
102 vmw_fifo_idle(dev_priv
, seqno
))
106 * Then check if the seqno is higher than what we've actually
107 * emitted. Then the fence is stale and signaled.
110 ret
= ((atomic_read(&dev_priv
->marker_seq
) - seqno
)
116 int vmw_fallback_wait(struct vmw_private
*dev_priv
,
121 unsigned long timeout
)
123 struct vmw_fifo_state
*fifo_state
= &dev_priv
->fifo
;
128 unsigned long end_jiffies
= jiffies
+ timeout
;
129 bool (*wait_condition
)(struct vmw_private
*, uint32_t);
132 wait_condition
= (fifo_idle
) ? &vmw_fifo_idle
:
136 * Block command submission while waiting for idle.
140 down_read(&fifo_state
->rwsem
);
141 signal_seq
= atomic_read(&dev_priv
->marker_seq
);
145 prepare_to_wait(&dev_priv
->fence_queue
, &__wait
,
147 TASK_INTERRUPTIBLE
: TASK_UNINTERRUPTIBLE
);
148 if (wait_condition(dev_priv
, seqno
))
150 if (time_after_eq(jiffies
, end_jiffies
)) {
151 DRM_ERROR("SVGA device lockup.\n");
156 else if ((++count
& 0x0F) == 0) {
158 * FIXME: Use schedule_hr_timeout here for
159 * newer kernels and lower CPU utilization.
162 __set_current_state(TASK_RUNNING
);
164 __set_current_state((interruptible
) ?
166 TASK_UNINTERRUPTIBLE
);
168 if (interruptible
&& signal_pending(current
)) {
173 finish_wait(&dev_priv
->fence_queue
, &__wait
);
174 if (ret
== 0 && fifo_idle
) {
175 __le32 __iomem
*fifo_mem
= dev_priv
->mmio_virt
;
176 iowrite32(signal_seq
, fifo_mem
+ SVGA_FIFO_FENCE
);
178 wake_up_all(&dev_priv
->fence_queue
);
180 up_read(&fifo_state
->rwsem
);
185 void vmw_seqno_waiter_add(struct vmw_private
*dev_priv
)
187 mutex_lock(&dev_priv
->hw_mutex
);
188 if (dev_priv
->fence_queue_waiters
++ == 0) {
189 unsigned long irq_flags
;
191 spin_lock_irqsave(&dev_priv
->irq_lock
, irq_flags
);
192 outl(SVGA_IRQFLAG_ANY_FENCE
,
193 dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
194 dev_priv
->irq_mask
|= SVGA_IRQFLAG_ANY_FENCE
;
195 vmw_write(dev_priv
, SVGA_REG_IRQMASK
, dev_priv
->irq_mask
);
196 spin_unlock_irqrestore(&dev_priv
->irq_lock
, irq_flags
);
198 mutex_unlock(&dev_priv
->hw_mutex
);
201 void vmw_seqno_waiter_remove(struct vmw_private
*dev_priv
)
203 mutex_lock(&dev_priv
->hw_mutex
);
204 if (--dev_priv
->fence_queue_waiters
== 0) {
205 unsigned long irq_flags
;
207 spin_lock_irqsave(&dev_priv
->irq_lock
, irq_flags
);
208 dev_priv
->irq_mask
&= ~SVGA_IRQFLAG_ANY_FENCE
;
209 vmw_write(dev_priv
, SVGA_REG_IRQMASK
, dev_priv
->irq_mask
);
210 spin_unlock_irqrestore(&dev_priv
->irq_lock
, irq_flags
);
212 mutex_unlock(&dev_priv
->hw_mutex
);
216 void vmw_goal_waiter_add(struct vmw_private
*dev_priv
)
218 mutex_lock(&dev_priv
->hw_mutex
);
219 if (dev_priv
->goal_queue_waiters
++ == 0) {
220 unsigned long irq_flags
;
222 spin_lock_irqsave(&dev_priv
->irq_lock
, irq_flags
);
223 outl(SVGA_IRQFLAG_FENCE_GOAL
,
224 dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
225 dev_priv
->irq_mask
|= SVGA_IRQFLAG_FENCE_GOAL
;
226 vmw_write(dev_priv
, SVGA_REG_IRQMASK
, dev_priv
->irq_mask
);
227 spin_unlock_irqrestore(&dev_priv
->irq_lock
, irq_flags
);
229 mutex_unlock(&dev_priv
->hw_mutex
);
232 void vmw_goal_waiter_remove(struct vmw_private
*dev_priv
)
234 mutex_lock(&dev_priv
->hw_mutex
);
235 if (--dev_priv
->goal_queue_waiters
== 0) {
236 unsigned long irq_flags
;
238 spin_lock_irqsave(&dev_priv
->irq_lock
, irq_flags
);
239 dev_priv
->irq_mask
&= ~SVGA_IRQFLAG_FENCE_GOAL
;
240 vmw_write(dev_priv
, SVGA_REG_IRQMASK
, dev_priv
->irq_mask
);
241 spin_unlock_irqrestore(&dev_priv
->irq_lock
, irq_flags
);
243 mutex_unlock(&dev_priv
->hw_mutex
);
246 int vmw_wait_seqno(struct vmw_private
*dev_priv
,
247 bool lazy
, uint32_t seqno
,
248 bool interruptible
, unsigned long timeout
)
251 struct vmw_fifo_state
*fifo
= &dev_priv
->fifo
;
253 if (likely(dev_priv
->last_read_seqno
- seqno
< VMW_FENCE_WRAP
))
256 if (likely(vmw_seqno_passed(dev_priv
, seqno
)))
259 vmw_fifo_ping_host(dev_priv
, SVGA_SYNC_GENERIC
);
261 if (!(fifo
->capabilities
& SVGA_FIFO_CAP_FENCE
))
262 return vmw_fallback_wait(dev_priv
, lazy
, true, seqno
,
263 interruptible
, timeout
);
265 if (!(dev_priv
->capabilities
& SVGA_CAP_IRQMASK
))
266 return vmw_fallback_wait(dev_priv
, lazy
, false, seqno
,
267 interruptible
, timeout
);
269 vmw_seqno_waiter_add(dev_priv
);
272 ret
= wait_event_interruptible_timeout
273 (dev_priv
->fence_queue
,
274 vmw_seqno_passed(dev_priv
, seqno
),
277 ret
= wait_event_timeout
278 (dev_priv
->fence_queue
,
279 vmw_seqno_passed(dev_priv
, seqno
),
282 vmw_seqno_waiter_remove(dev_priv
);
284 if (unlikely(ret
== 0))
286 else if (likely(ret
> 0))
292 void vmw_irq_preinstall(struct drm_device
*dev
)
294 struct vmw_private
*dev_priv
= vmw_priv(dev
);
297 if (!(dev_priv
->capabilities
& SVGA_CAP_IRQMASK
))
300 spin_lock_init(&dev_priv
->irq_lock
);
301 status
= inl(dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
302 outl(status
, dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
305 int vmw_irq_postinstall(struct drm_device
*dev
)
310 void vmw_irq_uninstall(struct drm_device
*dev
)
312 struct vmw_private
*dev_priv
= vmw_priv(dev
);
315 if (!(dev_priv
->capabilities
& SVGA_CAP_IRQMASK
))
318 mutex_lock(&dev_priv
->hw_mutex
);
319 vmw_write(dev_priv
, SVGA_REG_IRQMASK
, 0);
320 mutex_unlock(&dev_priv
->hw_mutex
);
322 status
= inl(dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
323 outl(status
, dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);