1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * 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)
34 * vmw_thread_fn - Deferred (process context) irq handler
37 * @arg: Closure argument. Pointer to a struct drm_device cast to void *
39 * This function implements the deferred part of irq processing.
40 * The function is guaranteed to run at least once after the
41 * vmw_irq_handler has returned with IRQ_WAKE_THREAD.
44 static irqreturn_t
vmw_thread_fn(int irq
, void *arg
)
46 struct drm_device
*dev
= (struct drm_device
*)arg
;
47 struct vmw_private
*dev_priv
= vmw_priv(dev
);
48 irqreturn_t ret
= IRQ_NONE
;
50 if (test_and_clear_bit(VMW_IRQTHREAD_FENCE
,
51 dev_priv
->irqthread_pending
)) {
52 vmw_fences_update(dev_priv
->fman
);
53 wake_up_all(&dev_priv
->fence_queue
);
57 if (test_and_clear_bit(VMW_IRQTHREAD_CMDBUF
,
58 dev_priv
->irqthread_pending
)) {
59 vmw_cmdbuf_irqthread(dev_priv
->cman
);
67 * vmw_irq_handler irq handler
70 * @arg: Closure argument. Pointer to a struct drm_device cast to void *
72 * This function implements the quick part of irq processing.
73 * The function performs fast actions like clearing the device interrupt
74 * flags and also reasonably quick actions like waking processes waiting for
75 * FIFO space. Other IRQ actions are deferred to the IRQ thread.
77 static irqreturn_t
vmw_irq_handler(int irq
, void *arg
)
79 struct drm_device
*dev
= (struct drm_device
*)arg
;
80 struct vmw_private
*dev_priv
= vmw_priv(dev
);
81 uint32_t status
, masked_status
;
82 irqreturn_t ret
= IRQ_HANDLED
;
84 status
= inl(dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
85 masked_status
= status
& READ_ONCE(dev_priv
->irq_mask
);
88 outl(status
, dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
93 if (masked_status
& SVGA_IRQFLAG_FIFO_PROGRESS
)
94 wake_up_all(&dev_priv
->fifo_queue
);
96 if ((masked_status
& (SVGA_IRQFLAG_ANY_FENCE
|
97 SVGA_IRQFLAG_FENCE_GOAL
)) &&
98 !test_and_set_bit(VMW_IRQTHREAD_FENCE
, dev_priv
->irqthread_pending
))
99 ret
= IRQ_WAKE_THREAD
;
101 if ((masked_status
& (SVGA_IRQFLAG_COMMAND_BUFFER
|
102 SVGA_IRQFLAG_ERROR
)) &&
103 !test_and_set_bit(VMW_IRQTHREAD_CMDBUF
,
104 dev_priv
->irqthread_pending
))
105 ret
= IRQ_WAKE_THREAD
;
110 static bool vmw_fifo_idle(struct vmw_private
*dev_priv
, uint32_t seqno
)
113 return (vmw_read(dev_priv
, SVGA_REG_BUSY
) == 0);
116 void vmw_update_seqno(struct vmw_private
*dev_priv
,
117 struct vmw_fifo_state
*fifo_state
)
119 u32
*fifo_mem
= dev_priv
->mmio_virt
;
120 uint32_t seqno
= vmw_mmio_read(fifo_mem
+ SVGA_FIFO_FENCE
);
122 if (dev_priv
->last_read_seqno
!= seqno
) {
123 dev_priv
->last_read_seqno
= seqno
;
124 vmw_marker_pull(&fifo_state
->marker_queue
, seqno
);
125 vmw_fences_update(dev_priv
->fman
);
129 bool vmw_seqno_passed(struct vmw_private
*dev_priv
,
132 struct vmw_fifo_state
*fifo_state
;
135 if (likely(dev_priv
->last_read_seqno
- seqno
< VMW_FENCE_WRAP
))
138 fifo_state
= &dev_priv
->fifo
;
139 vmw_update_seqno(dev_priv
, fifo_state
);
140 if (likely(dev_priv
->last_read_seqno
- seqno
< VMW_FENCE_WRAP
))
143 if (!(fifo_state
->capabilities
& SVGA_FIFO_CAP_FENCE
) &&
144 vmw_fifo_idle(dev_priv
, seqno
))
148 * Then check if the seqno is higher than what we've actually
149 * emitted. Then the fence is stale and signaled.
152 ret
= ((atomic_read(&dev_priv
->marker_seq
) - seqno
)
158 int vmw_fallback_wait(struct vmw_private
*dev_priv
,
163 unsigned long timeout
)
165 struct vmw_fifo_state
*fifo_state
= &dev_priv
->fifo
;
170 unsigned long end_jiffies
= jiffies
+ timeout
;
171 bool (*wait_condition
)(struct vmw_private
*, uint32_t);
174 wait_condition
= (fifo_idle
) ? &vmw_fifo_idle
:
178 * Block command submission while waiting for idle.
182 down_read(&fifo_state
->rwsem
);
183 if (dev_priv
->cman
) {
184 ret
= vmw_cmdbuf_idle(dev_priv
->cman
, interruptible
,
191 signal_seq
= atomic_read(&dev_priv
->marker_seq
);
195 prepare_to_wait(&dev_priv
->fence_queue
, &__wait
,
197 TASK_INTERRUPTIBLE
: TASK_UNINTERRUPTIBLE
);
198 if (wait_condition(dev_priv
, seqno
))
200 if (time_after_eq(jiffies
, end_jiffies
)) {
201 DRM_ERROR("SVGA device lockup.\n");
206 else if ((++count
& 0x0F) == 0) {
208 * FIXME: Use schedule_hr_timeout here for
209 * newer kernels and lower CPU utilization.
212 __set_current_state(TASK_RUNNING
);
214 __set_current_state((interruptible
) ?
216 TASK_UNINTERRUPTIBLE
);
218 if (interruptible
&& signal_pending(current
)) {
223 finish_wait(&dev_priv
->fence_queue
, &__wait
);
224 if (ret
== 0 && fifo_idle
) {
225 u32
*fifo_mem
= dev_priv
->mmio_virt
;
227 vmw_mmio_write(signal_seq
, fifo_mem
+ SVGA_FIFO_FENCE
);
229 wake_up_all(&dev_priv
->fence_queue
);
232 up_read(&fifo_state
->rwsem
);
237 void vmw_generic_waiter_add(struct vmw_private
*dev_priv
,
238 u32 flag
, int *waiter_count
)
240 spin_lock_bh(&dev_priv
->waiter_lock
);
241 if ((*waiter_count
)++ == 0) {
242 outl(flag
, dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
243 dev_priv
->irq_mask
|= flag
;
244 vmw_write(dev_priv
, SVGA_REG_IRQMASK
, dev_priv
->irq_mask
);
246 spin_unlock_bh(&dev_priv
->waiter_lock
);
249 void vmw_generic_waiter_remove(struct vmw_private
*dev_priv
,
250 u32 flag
, int *waiter_count
)
252 spin_lock_bh(&dev_priv
->waiter_lock
);
253 if (--(*waiter_count
) == 0) {
254 dev_priv
->irq_mask
&= ~flag
;
255 vmw_write(dev_priv
, SVGA_REG_IRQMASK
, dev_priv
->irq_mask
);
257 spin_unlock_bh(&dev_priv
->waiter_lock
);
260 void vmw_seqno_waiter_add(struct vmw_private
*dev_priv
)
262 vmw_generic_waiter_add(dev_priv
, SVGA_IRQFLAG_ANY_FENCE
,
263 &dev_priv
->fence_queue_waiters
);
266 void vmw_seqno_waiter_remove(struct vmw_private
*dev_priv
)
268 vmw_generic_waiter_remove(dev_priv
, SVGA_IRQFLAG_ANY_FENCE
,
269 &dev_priv
->fence_queue_waiters
);
272 void vmw_goal_waiter_add(struct vmw_private
*dev_priv
)
274 vmw_generic_waiter_add(dev_priv
, SVGA_IRQFLAG_FENCE_GOAL
,
275 &dev_priv
->goal_queue_waiters
);
278 void vmw_goal_waiter_remove(struct vmw_private
*dev_priv
)
280 vmw_generic_waiter_remove(dev_priv
, SVGA_IRQFLAG_FENCE_GOAL
,
281 &dev_priv
->goal_queue_waiters
);
284 int vmw_wait_seqno(struct vmw_private
*dev_priv
,
285 bool lazy
, uint32_t seqno
,
286 bool interruptible
, unsigned long timeout
)
289 struct vmw_fifo_state
*fifo
= &dev_priv
->fifo
;
291 if (likely(dev_priv
->last_read_seqno
- seqno
< VMW_FENCE_WRAP
))
294 if (likely(vmw_seqno_passed(dev_priv
, seqno
)))
297 vmw_fifo_ping_host(dev_priv
, SVGA_SYNC_GENERIC
);
299 if (!(fifo
->capabilities
& SVGA_FIFO_CAP_FENCE
))
300 return vmw_fallback_wait(dev_priv
, lazy
, true, seqno
,
301 interruptible
, timeout
);
303 if (!(dev_priv
->capabilities
& SVGA_CAP_IRQMASK
))
304 return vmw_fallback_wait(dev_priv
, lazy
, false, seqno
,
305 interruptible
, timeout
);
307 vmw_seqno_waiter_add(dev_priv
);
310 ret
= wait_event_interruptible_timeout
311 (dev_priv
->fence_queue
,
312 vmw_seqno_passed(dev_priv
, seqno
),
315 ret
= wait_event_timeout
316 (dev_priv
->fence_queue
,
317 vmw_seqno_passed(dev_priv
, seqno
),
320 vmw_seqno_waiter_remove(dev_priv
);
322 if (unlikely(ret
== 0))
324 else if (likely(ret
> 0))
330 static void vmw_irq_preinstall(struct drm_device
*dev
)
332 struct vmw_private
*dev_priv
= vmw_priv(dev
);
335 status
= inl(dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
336 outl(status
, dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
339 void vmw_irq_uninstall(struct drm_device
*dev
)
341 struct vmw_private
*dev_priv
= vmw_priv(dev
);
344 if (!(dev_priv
->capabilities
& SVGA_CAP_IRQMASK
))
347 if (!dev
->irq_enabled
)
350 vmw_write(dev_priv
, SVGA_REG_IRQMASK
, 0);
352 status
= inl(dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
353 outl(status
, dev_priv
->io_start
+ VMWGFX_IRQSTATUS_PORT
);
355 dev
->irq_enabled
= false;
356 free_irq(dev
->irq
, dev
);
360 * vmw_irq_install - Install the irq handlers
362 * @dev: Pointer to the drm device.
363 * @irq: The irq number.
364 * Return: Zero if successful. Negative number otherwise.
366 int vmw_irq_install(struct drm_device
*dev
, int irq
)
370 if (dev
->irq_enabled
)
373 vmw_irq_preinstall(dev
);
375 ret
= request_threaded_irq(irq
, vmw_irq_handler
, vmw_thread_fn
,
376 IRQF_SHARED
, VMWGFX_DRIVER_NAME
, dev
);
380 dev
->irq_enabled
= true;