1 /**************************************************************************
3 * Copyright © 2009-2015 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(int irq
, void *arg
)
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 status
= inl(dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
40 masked_status
= status
& READ_ONCE(dev_priv
->irq_mask
);
43 outl(status
, dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
48 if (masked_status
& (SVGA_IRQFLAG_ANY_FENCE
|
49 SVGA_IRQFLAG_FENCE_GOAL
)) {
50 vmw_fences_update(dev_priv
->fman
);
51 wake_up_all(&dev_priv
->fence_queue
);
54 if (masked_status
& SVGA_IRQFLAG_FIFO_PROGRESS
)
55 wake_up_all(&dev_priv
->fifo_queue
);
57 if (masked_status
& (SVGA_IRQFLAG_COMMAND_BUFFER
|
59 vmw_cmdbuf_tasklet_schedule(dev_priv
->cman
);
64 static bool vmw_fifo_idle(struct vmw_private
*dev_priv
, uint32_t seqno
)
67 return (vmw_read(dev_priv
, SVGA_REG_BUSY
) == 0);
70 void vmw_update_seqno(struct vmw_private
*dev_priv
,
71 struct vmw_fifo_state
*fifo_state
)
73 u32
*fifo_mem
= dev_priv
->mmio_virt
;
74 uint32_t seqno
= vmw_mmio_read(fifo_mem
+ SVGA_FIFO_FENCE
);
76 if (dev_priv
->last_read_seqno
!= seqno
) {
77 dev_priv
->last_read_seqno
= seqno
;
78 vmw_marker_pull(&fifo_state
->marker_queue
, seqno
);
79 vmw_fences_update(dev_priv
->fman
);
83 bool vmw_seqno_passed(struct vmw_private
*dev_priv
,
86 struct vmw_fifo_state
*fifo_state
;
89 if (likely(dev_priv
->last_read_seqno
- seqno
< VMW_FENCE_WRAP
))
92 fifo_state
= &dev_priv
->fifo
;
93 vmw_update_seqno(dev_priv
, fifo_state
);
94 if (likely(dev_priv
->last_read_seqno
- seqno
< VMW_FENCE_WRAP
))
97 if (!(fifo_state
->capabilities
& SVGA_FIFO_CAP_FENCE
) &&
98 vmw_fifo_idle(dev_priv
, seqno
))
102 * Then check if the seqno is higher than what we've actually
103 * emitted. Then the fence is stale and signaled.
106 ret
= ((atomic_read(&dev_priv
->marker_seq
) - seqno
)
112 int vmw_fallback_wait(struct vmw_private
*dev_priv
,
117 unsigned long timeout
)
119 struct vmw_fifo_state
*fifo_state
= &dev_priv
->fifo
;
124 unsigned long end_jiffies
= jiffies
+ timeout
;
125 bool (*wait_condition
)(struct vmw_private
*, uint32_t);
128 wait_condition
= (fifo_idle
) ? &vmw_fifo_idle
:
132 * Block command submission while waiting for idle.
136 down_read(&fifo_state
->rwsem
);
137 if (dev_priv
->cman
) {
138 ret
= vmw_cmdbuf_idle(dev_priv
->cman
, interruptible
,
145 signal_seq
= atomic_read(&dev_priv
->marker_seq
);
149 prepare_to_wait(&dev_priv
->fence_queue
, &__wait
,
151 TASK_INTERRUPTIBLE
: TASK_UNINTERRUPTIBLE
);
152 if (wait_condition(dev_priv
, seqno
))
154 if (time_after_eq(jiffies
, end_jiffies
)) {
155 DRM_ERROR("SVGA device lockup.\n");
160 else if ((++count
& 0x0F) == 0) {
162 * FIXME: Use schedule_hr_timeout here for
163 * newer kernels and lower CPU utilization.
166 __set_current_state(TASK_RUNNING
);
168 __set_current_state((interruptible
) ?
170 TASK_UNINTERRUPTIBLE
);
172 if (interruptible
&& signal_pending(current
)) {
177 finish_wait(&dev_priv
->fence_queue
, &__wait
);
178 if (ret
== 0 && fifo_idle
) {
179 u32
*fifo_mem
= dev_priv
->mmio_virt
;
181 vmw_mmio_write(signal_seq
, fifo_mem
+ SVGA_FIFO_FENCE
);
183 wake_up_all(&dev_priv
->fence_queue
);
186 up_read(&fifo_state
->rwsem
);
191 void vmw_generic_waiter_add(struct vmw_private
*dev_priv
,
192 u32 flag
, int *waiter_count
)
194 spin_lock_bh(&dev_priv
->waiter_lock
);
195 if ((*waiter_count
)++ == 0) {
196 outl(flag
, dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
197 dev_priv
->irq_mask
|= flag
;
198 vmw_write(dev_priv
, SVGA_REG_IRQMASK
, dev_priv
->irq_mask
);
200 spin_unlock_bh(&dev_priv
->waiter_lock
);
203 void vmw_generic_waiter_remove(struct vmw_private
*dev_priv
,
204 u32 flag
, int *waiter_count
)
206 spin_lock_bh(&dev_priv
->waiter_lock
);
207 if (--(*waiter_count
) == 0) {
208 dev_priv
->irq_mask
&= ~flag
;
209 vmw_write(dev_priv
, SVGA_REG_IRQMASK
, dev_priv
->irq_mask
);
211 spin_unlock_bh(&dev_priv
->waiter_lock
);
214 void vmw_seqno_waiter_add(struct vmw_private
*dev_priv
)
216 vmw_generic_waiter_add(dev_priv
, SVGA_IRQFLAG_ANY_FENCE
,
217 &dev_priv
->fence_queue_waiters
);
220 void vmw_seqno_waiter_remove(struct vmw_private
*dev_priv
)
222 vmw_generic_waiter_remove(dev_priv
, SVGA_IRQFLAG_ANY_FENCE
,
223 &dev_priv
->fence_queue_waiters
);
226 void vmw_goal_waiter_add(struct vmw_private
*dev_priv
)
228 vmw_generic_waiter_add(dev_priv
, SVGA_IRQFLAG_FENCE_GOAL
,
229 &dev_priv
->goal_queue_waiters
);
232 void vmw_goal_waiter_remove(struct vmw_private
*dev_priv
)
234 vmw_generic_waiter_remove(dev_priv
, SVGA_IRQFLAG_FENCE_GOAL
,
235 &dev_priv
->goal_queue_waiters
);
238 int vmw_wait_seqno(struct vmw_private
*dev_priv
,
239 bool lazy
, uint32_t seqno
,
240 bool interruptible
, unsigned long timeout
)
243 struct vmw_fifo_state
*fifo
= &dev_priv
->fifo
;
245 if (likely(dev_priv
->last_read_seqno
- seqno
< VMW_FENCE_WRAP
))
248 if (likely(vmw_seqno_passed(dev_priv
, seqno
)))
251 vmw_fifo_ping_host(dev_priv
, SVGA_SYNC_GENERIC
);
253 if (!(fifo
->capabilities
& SVGA_FIFO_CAP_FENCE
))
254 return vmw_fallback_wait(dev_priv
, lazy
, true, seqno
,
255 interruptible
, timeout
);
257 if (!(dev_priv
->capabilities
& SVGA_CAP_IRQMASK
))
258 return vmw_fallback_wait(dev_priv
, lazy
, false, seqno
,
259 interruptible
, timeout
);
261 vmw_seqno_waiter_add(dev_priv
);
264 ret
= wait_event_interruptible_timeout
265 (dev_priv
->fence_queue
,
266 vmw_seqno_passed(dev_priv
, seqno
),
269 ret
= wait_event_timeout
270 (dev_priv
->fence_queue
,
271 vmw_seqno_passed(dev_priv
, seqno
),
274 vmw_seqno_waiter_remove(dev_priv
);
276 if (unlikely(ret
== 0))
278 else if (likely(ret
> 0))
284 void vmw_irq_preinstall(struct drm_device
*dev
)
286 struct vmw_private
*dev_priv
= vmw_priv(dev
);
289 if (!(dev_priv
->capabilities
& SVGA_CAP_IRQMASK
))
292 status
= inl(dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
293 outl(status
, dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
296 int vmw_irq_postinstall(struct drm_device
*dev
)
301 void vmw_irq_uninstall(struct drm_device
*dev
)
303 struct vmw_private
*dev_priv
= vmw_priv(dev
);
306 if (!(dev_priv
->capabilities
& SVGA_CAP_IRQMASK
))
309 vmw_write(dev_priv
, SVGA_REG_IRQMASK
, 0);
311 status
= inl(dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
312 outl(status
, dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);