2 * Copyright 2003 Eric Anholt
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * Eric Anholt <anholt@FreeBSD.org>
29 * Support code for handling setup/teardown of interrupt handlers and
30 * handing interrupt handlers off to the drivers.
36 int drm_irq_by_busid(struct drm_device *dev, void *data,
37 struct drm_file *file_priv)
39 struct drm_irq_busid *irq = data;
41 if ((irq->busnum >> 8) != dev->pci_domain ||
42 (irq->busnum & 0xff) != dev->pci_bus ||
43 irq->devnum != dev->pci_slot ||
44 irq->funcnum != dev->pci_func)
49 DRM_DEBUG("%d:%d:%d => IRQ %d\n",
50 irq->busnum, irq->devnum, irq->funcnum, irq->irq);
56 drm_irq_handler_wrap(DRM_IRQ_ARGS)
58 struct drm_device *dev = arg;
60 DRM_SPINLOCK(&dev->irq_lock);
61 dev->driver->irq_handler(arg);
62 DRM_SPINUNLOCK(&dev->irq_lock);
65 static void vblank_disable_fn(void *arg)
67 struct drm_device *dev = (struct drm_device *)arg;
70 if (callout_pending(&dev->vblank_disable_timer)) {
71 /* callout was reset */
74 if (!callout_active(&dev->vblank_disable_timer)) {
75 /* callout was stopped */
78 callout_deactivate(&dev->vblank_disable_timer);
80 DRM_DEBUG("vblank_disable_allowed=%d\n", dev->vblank_disable_allowed);
81 if (!dev->vblank_disable_allowed)
84 for (i = 0; i < dev->num_crtcs; i++) {
85 if (atomic_read(&dev->vblank[i].refcount) == 0 &&
86 dev->vblank[i].enabled) {
87 DRM_DEBUG("disabling vblank on crtc %d\n", i);
89 dev->driver->get_vblank_counter(dev, i);
90 dev->driver->disable_vblank(dev, i);
91 dev->vblank[i].enabled = 0;
96 void drm_vblank_cleanup(struct drm_device *dev)
98 unsigned long irqflags;
100 /* Bail if the driver didn't call drm_vblank_init() */
101 if (dev->num_crtcs == 0)
104 DRM_SPINLOCK_IRQSAVE(&dev->vbl_lock, irqflags);
105 callout_stop(&dev->vblank_disable_timer);
106 DRM_SPINUNLOCK_IRQRESTORE(&dev->vbl_lock, irqflags);
108 callout_drain(&dev->vblank_disable_timer);
110 vblank_disable_fn((void *)dev);
112 free(dev->vblank, DRM_MEM_DRIVER);
117 int drm_vblank_init(struct drm_device *dev, int num_crtcs)
121 callout_init_mtx(&dev->vblank_disable_timer, &dev->vbl_lock, 0);
122 atomic_set(&dev->vbl_signal_pending, 0);
123 dev->num_crtcs = num_crtcs;
125 dev->vblank = malloc(sizeof(struct drm_vblank_info) * num_crtcs,
126 DRM_MEM_DRIVER, M_NOWAIT | M_ZERO);
132 /* Zero per-crtc vblank stuff */
133 for (i = 0; i < num_crtcs; i++) {
134 DRM_INIT_WAITQUEUE(&dev->vblank[i].queue);
135 TAILQ_INIT(&dev->vblank[i].sigs);
136 atomic_set(&dev->vblank[i].count, 0);
137 atomic_set(&dev->vblank[i].refcount, 0);
140 dev->vblank_disable_allowed = 0;
145 drm_vblank_cleanup(dev);
149 int drm_irq_install(struct drm_device *dev)
153 if (dev->irq == 0 || dev->dev_private == NULL)
156 DRM_DEBUG("irq=%d\n", dev->irq);
159 if (dev->irq_enabled) {
163 dev->irq_enabled = 1;
165 dev->context_flag = 0;
167 /* Before installing handler */
168 dev->driver->irq_preinstall(dev);
171 /* Install handler */
172 #if __FreeBSD_version >= 700031
173 retcode = bus_setup_intr(dev->device, dev->irqr,
174 INTR_TYPE_TTY | INTR_MPSAFE,
175 NULL, drm_irq_handler_wrap, dev, &dev->irqh);
177 retcode = bus_setup_intr(dev->device, dev->irqr,
178 INTR_TYPE_TTY | INTR_MPSAFE,
179 drm_irq_handler_wrap, dev, &dev->irqh);
184 /* After installing handler */
186 dev->driver->irq_postinstall(dev);
192 dev->irq_enabled = 0;
198 int drm_irq_uninstall(struct drm_device *dev)
200 if (!dev->irq_enabled)
203 dev->irq_enabled = 0;
205 DRM_DEBUG("irq=%d\n", dev->irq);
207 dev->driver->irq_uninstall(dev);
210 bus_teardown_intr(dev->device, dev->irqr, dev->irqh);
216 int drm_control(struct drm_device *dev, void *data, struct drm_file *file_priv)
218 struct drm_control *ctl = data;
222 case DRM_INST_HANDLER:
223 /* Handle drivers whose DRM used to require IRQ setup but the
226 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
228 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
229 ctl->irq != dev->irq)
231 return drm_irq_install(dev);
232 case DRM_UNINST_HANDLER:
233 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
236 err = drm_irq_uninstall(dev);
244 u32 drm_vblank_count(struct drm_device *dev, int crtc)
246 return atomic_read(&dev->vblank[crtc].count);
249 static void drm_update_vblank_count(struct drm_device *dev, int crtc)
251 u32 cur_vblank, diff;
254 * Interrupts were disabled prior to this call, so deal with counter
256 * NOTE! It's possible we lost a full dev->max_vblank_count events
257 * here if the register is small or we had vblank interrupts off for
260 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
261 diff = cur_vblank - dev->vblank[crtc].last;
262 if (cur_vblank < dev->vblank[crtc].last) {
263 diff += dev->max_vblank_count;
265 DRM_DEBUG("vblank[%d].last=0x%x, cur_vblank=0x%x => diff=0x%x\n",
266 crtc, dev->vblank[crtc].last, cur_vblank, diff);
269 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
272 atomic_add(diff, &dev->vblank[crtc].count);
275 int drm_vblank_get(struct drm_device *dev, int crtc)
277 unsigned long irqflags;
280 DRM_SPINLOCK_IRQSAVE(&dev->vbl_lock, irqflags);
281 /* Going from 0->1 means we have to enable interrupts again */
282 atomic_add_acq_int(&dev->vblank[crtc].refcount, 1);
283 DRM_DEBUG("vblank refcount = %d\n", dev->vblank[crtc].refcount);
284 if (dev->vblank[crtc].refcount == 1 &&
285 !dev->vblank[crtc].enabled) {
286 ret = dev->driver->enable_vblank(dev, crtc);
288 atomic_dec(&dev->vblank[crtc].refcount);
290 dev->vblank[crtc].enabled = 1;
291 drm_update_vblank_count(dev, crtc);
294 DRM_SPINUNLOCK_IRQRESTORE(&dev->vbl_lock, irqflags);
299 void drm_vblank_put(struct drm_device *dev, int crtc)
301 unsigned long irqflags;
303 DRM_SPINLOCK_IRQSAVE(&dev->vbl_lock, irqflags);
304 /* Last user schedules interrupt disable */
305 atomic_subtract_acq_int(&dev->vblank[crtc].refcount, 1);
306 DRM_DEBUG("vblank refcount = %d\n", dev->vblank[crtc].refcount);
307 if (dev->vblank[crtc].refcount == 0)
308 callout_reset(&dev->vblank_disable_timer, 5 * DRM_HZ,
309 (timeout_t *)vblank_disable_fn, (void *)dev);
310 DRM_SPINUNLOCK_IRQRESTORE(&dev->vbl_lock, irqflags);
313 int drm_modeset_ctl(struct drm_device *dev, void *data,
314 struct drm_file *file_priv)
316 struct drm_modeset_ctl *modeset = data;
317 unsigned long irqflags;
320 DRM_DEBUG("num_crtcs=%d\n", dev->num_crtcs);
321 /* If drm_vblank_init() hasn't been called yet, just no-op */
325 crtc = modeset->crtc;
326 DRM_DEBUG("crtc=%d\n", crtc);
327 if (crtc >= dev->num_crtcs) {
333 * To avoid all the problems that might happen if interrupts
334 * were enabled/disabled around or between these calls, we just
335 * have the kernel take a reference on the CRTC (just once though
336 * to avoid corrupting the count if multiple, mismatch calls occur),
337 * so that interrupts remain enabled in the interim.
339 switch (modeset->cmd) {
340 case _DRM_PRE_MODESET:
341 DRM_DEBUG("pre-modeset\n");
342 if (!dev->vblank[crtc].inmodeset) {
343 dev->vblank[crtc].inmodeset = 1;
344 drm_vblank_get(dev, crtc);
347 case _DRM_POST_MODESET:
348 DRM_DEBUG("post-modeset\n");
349 if (dev->vblank[crtc].inmodeset) {
350 DRM_SPINLOCK_IRQSAVE(&dev->vbl_lock, irqflags);
351 dev->vblank_disable_allowed = 1;
352 dev->vblank[crtc].inmodeset = 0;
353 DRM_SPINUNLOCK_IRQRESTORE(&dev->vbl_lock, irqflags);
354 drm_vblank_put(dev, crtc);
366 int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_priv)
368 union drm_wait_vblank *vblwait = data;
369 unsigned int flags, seq, crtc;
372 if (!dev->irq_enabled)
375 if (vblwait->request.type &
376 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
377 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
378 vblwait->request.type,
379 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
383 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
384 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
386 if (crtc >= dev->num_crtcs)
389 ret = drm_vblank_get(dev, crtc);
391 DRM_ERROR("failed to acquire vblank counter, %d\n", ret);
394 seq = drm_vblank_count(dev, crtc);
396 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
397 case _DRM_VBLANK_RELATIVE:
398 vblwait->request.sequence += seq;
399 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
400 case _DRM_VBLANK_ABSOLUTE:
407 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
408 (seq - vblwait->request.sequence) <= (1<<23)) {
409 vblwait->request.sequence = seq + 1;
412 if (flags & _DRM_VBLANK_SIGNAL) {
414 drm_vbl_sig_t *vbl_sig = malloc(sizeof(drm_vbl_sig_t),
415 DRM_MEM_DRIVER, M_NOWAIT | M_ZERO);
419 vbl_sig->sequence = vblwait->request.sequence;
420 vbl_sig->signo = vblwait->request.signal;
421 vbl_sig->pid = DRM_CURRENTPID;
423 vblwait->reply.sequence = atomic_read(&dev->vbl_received);
425 DRM_SPINLOCK(&dev->vbl_lock);
426 TAILQ_INSERT_HEAD(&dev->vbl_sig_list, vbl_sig, link);
427 DRM_SPINUNLOCK(&dev->vbl_lock);
432 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
433 vblwait->request.sequence, crtc);
434 for ( ret = 0 ; !ret && !((drm_vblank_count(dev, crtc) -
435 vblwait->request.sequence) <= (1 << 23)) ; ) {
436 mtx_lock(&dev->irq_lock);
437 if (!((drm_vblank_count(dev, crtc) -
438 vblwait->request.sequence) <= (1 << 23)))
439 ret = mtx_sleep(&dev->vblank[crtc].queue,
440 &dev->irq_lock, PCATCH, "vblwtq",
442 mtx_unlock(&dev->irq_lock);
445 DRM_DEBUG("return = %d\n", ret);
450 vblwait->reply.tval_sec = now.tv_sec;
451 vblwait->reply.tval_usec = now.tv_usec;
452 vblwait->reply.sequence = drm_vblank_count(dev, crtc);
453 DRM_DEBUG("returning %d to client\n",
454 vblwait->reply.sequence);
456 DRM_DEBUG("vblank wait interrupted by signal\n");
461 drm_vblank_put(dev, crtc);
465 void drm_vbl_send_signals(struct drm_device *dev, int crtc)
470 void drm_vbl_send_signals(struct drm_device *dev, int crtc )
472 drm_vbl_sig_t *vbl_sig;
473 unsigned int vbl_seq = atomic_read( &dev->vbl_received );
476 vbl_sig = TAILQ_FIRST(&dev->vbl_sig_list);
477 while (vbl_sig != NULL) {
478 drm_vbl_sig_t *next = TAILQ_NEXT(vbl_sig, link);
480 if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
481 p = pfind(vbl_sig->pid);
483 psignal(p, vbl_sig->signo);
485 TAILQ_REMOVE(&dev->vbl_sig_list, vbl_sig, link);
486 DRM_FREE(vbl_sig,sizeof(*vbl_sig));
493 void drm_handle_vblank(struct drm_device *dev, int crtc)
495 atomic_inc(&dev->vblank[crtc].count);
496 DRM_WAKEUP(&dev->vblank[crtc].queue);
497 drm_vbl_send_signals(dev, crtc);