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>
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Support code for handling setup/teardown of interrupt handlers and
33 * handing interrupt handlers off to the drivers.
36 #include "dev/drm/drmP.h"
37 #include "dev/drm/drm.h"
39 int drm_irq_by_busid(struct drm_device *dev, void *data,
40 struct drm_file *file_priv)
42 struct drm_irq_busid *irq = data;
44 if ((irq->busnum >> 8) != dev->pci_domain ||
45 (irq->busnum & 0xff) != dev->pci_bus ||
46 irq->devnum != dev->pci_slot ||
47 irq->funcnum != dev->pci_func)
52 DRM_DEBUG("%d:%d:%d => IRQ %d\n",
53 irq->busnum, irq->devnum, irq->funcnum, irq->irq);
59 drm_irq_handler_wrap(DRM_IRQ_ARGS)
61 struct drm_device *dev = arg;
63 DRM_SPINLOCK(&dev->irq_lock);
64 dev->driver->irq_handler(arg);
65 DRM_SPINUNLOCK(&dev->irq_lock);
68 static void vblank_disable_fn(void *arg)
70 struct drm_device *dev = (struct drm_device *)arg;
73 if (callout_pending(&dev->vblank_disable_timer)) {
74 /* callout was reset */
77 if (!callout_active(&dev->vblank_disable_timer)) {
78 /* callout was stopped */
81 callout_deactivate(&dev->vblank_disable_timer);
83 DRM_DEBUG("vblank_disable_allowed=%d\n", dev->vblank_disable_allowed);
84 if (!dev->vblank_disable_allowed)
87 for (i = 0; i < dev->num_crtcs; i++) {
88 if (atomic_read(&dev->vblank[i].refcount) == 0 &&
89 dev->vblank[i].enabled) {
90 DRM_DEBUG("disabling vblank on crtc %d\n", i);
92 dev->driver->get_vblank_counter(dev, i);
93 dev->driver->disable_vblank(dev, i);
94 dev->vblank[i].enabled = 0;
99 void drm_vblank_cleanup(struct drm_device *dev)
101 unsigned long irqflags;
103 /* Bail if the driver didn't call drm_vblank_init() */
104 if (dev->num_crtcs == 0)
107 DRM_SPINLOCK_IRQSAVE(&dev->vbl_lock, irqflags);
108 callout_stop(&dev->vblank_disable_timer);
109 DRM_SPINUNLOCK_IRQRESTORE(&dev->vbl_lock, irqflags);
111 callout_drain(&dev->vblank_disable_timer);
113 vblank_disable_fn((void *)dev);
115 free(dev->vblank, DRM_MEM_DRIVER);
120 int drm_vblank_init(struct drm_device *dev, int num_crtcs)
124 callout_init_mtx(&dev->vblank_disable_timer, &dev->vbl_lock, 0);
125 atomic_set(&dev->vbl_signal_pending, 0);
126 dev->num_crtcs = num_crtcs;
128 dev->vblank = malloc(sizeof(struct drm_vblank_info) * num_crtcs,
129 DRM_MEM_DRIVER, M_NOWAIT | M_ZERO);
135 /* Zero per-crtc vblank stuff */
136 for (i = 0; i < num_crtcs; i++) {
137 DRM_INIT_WAITQUEUE(&dev->vblank[i].queue);
138 TAILQ_INIT(&dev->vblank[i].sigs);
139 atomic_set(&dev->vblank[i].count, 0);
140 atomic_set(&dev->vblank[i].refcount, 0);
143 dev->vblank_disable_allowed = 0;
148 drm_vblank_cleanup(dev);
152 int drm_irq_install(struct drm_device *dev)
156 if (dev->irq == 0 || dev->dev_private == NULL)
159 DRM_DEBUG("irq=%d\n", dev->irq);
162 if (dev->irq_enabled) {
166 dev->irq_enabled = 1;
168 dev->context_flag = 0;
170 /* Before installing handler */
171 dev->driver->irq_preinstall(dev);
174 /* Install handler */
175 #if __FreeBSD_version >= 700031
176 retcode = bus_setup_intr(dev->device, dev->irqr,
177 INTR_TYPE_TTY | INTR_MPSAFE,
178 NULL, drm_irq_handler_wrap, dev, &dev->irqh);
180 retcode = bus_setup_intr(dev->device, dev->irqr,
181 INTR_TYPE_TTY | INTR_MPSAFE,
182 drm_irq_handler_wrap, dev, &dev->irqh);
187 /* After installing handler */
189 dev->driver->irq_postinstall(dev);
195 dev->irq_enabled = 0;
201 int drm_irq_uninstall(struct drm_device *dev)
203 if (!dev->irq_enabled)
206 dev->irq_enabled = 0;
208 DRM_DEBUG("irq=%d\n", dev->irq);
210 dev->driver->irq_uninstall(dev);
213 bus_teardown_intr(dev->device, dev->irqr, dev->irqh);
219 int drm_control(struct drm_device *dev, void *data, struct drm_file *file_priv)
221 struct drm_control *ctl = data;
225 case DRM_INST_HANDLER:
226 /* Handle drivers whose DRM used to require IRQ setup but the
229 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
231 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
232 ctl->irq != dev->irq)
234 return drm_irq_install(dev);
235 case DRM_UNINST_HANDLER:
236 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
239 err = drm_irq_uninstall(dev);
247 u32 drm_vblank_count(struct drm_device *dev, int crtc)
249 return atomic_read(&dev->vblank[crtc].count);
252 static void drm_update_vblank_count(struct drm_device *dev, int crtc)
254 u32 cur_vblank, diff;
257 * Interrupts were disabled prior to this call, so deal with counter
259 * NOTE! It's possible we lost a full dev->max_vblank_count events
260 * here if the register is small or we had vblank interrupts off for
263 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
264 diff = cur_vblank - dev->vblank[crtc].last;
265 if (cur_vblank < dev->vblank[crtc].last) {
266 diff += dev->max_vblank_count;
268 DRM_DEBUG("vblank[%d].last=0x%x, cur_vblank=0x%x => diff=0x%x\n",
269 crtc, dev->vblank[crtc].last, cur_vblank, diff);
272 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
275 atomic_add(diff, &dev->vblank[crtc].count);
278 int drm_vblank_get(struct drm_device *dev, int crtc)
280 unsigned long irqflags;
283 DRM_SPINLOCK_IRQSAVE(&dev->vbl_lock, irqflags);
284 /* Going from 0->1 means we have to enable interrupts again */
285 atomic_add_acq_int(&dev->vblank[crtc].refcount, 1);
286 DRM_DEBUG("vblank refcount = %d\n", dev->vblank[crtc].refcount);
287 if (dev->vblank[crtc].refcount == 1 &&
288 !dev->vblank[crtc].enabled) {
289 ret = dev->driver->enable_vblank(dev, crtc);
291 atomic_dec(&dev->vblank[crtc].refcount);
293 dev->vblank[crtc].enabled = 1;
294 drm_update_vblank_count(dev, crtc);
297 DRM_SPINUNLOCK_IRQRESTORE(&dev->vbl_lock, irqflags);
302 void drm_vblank_put(struct drm_device *dev, int crtc)
304 unsigned long irqflags;
306 DRM_SPINLOCK_IRQSAVE(&dev->vbl_lock, irqflags);
307 /* Last user schedules interrupt disable */
308 atomic_subtract_acq_int(&dev->vblank[crtc].refcount, 1);
309 DRM_DEBUG("vblank refcount = %d\n", dev->vblank[crtc].refcount);
310 if (dev->vblank[crtc].refcount == 0)
311 callout_reset(&dev->vblank_disable_timer, 5 * DRM_HZ,
312 (timeout_t *)vblank_disable_fn, (void *)dev);
313 DRM_SPINUNLOCK_IRQRESTORE(&dev->vbl_lock, irqflags);
316 int drm_modeset_ctl(struct drm_device *dev, void *data,
317 struct drm_file *file_priv)
319 struct drm_modeset_ctl *modeset = data;
320 unsigned long irqflags;
323 DRM_DEBUG("num_crtcs=%d\n", dev->num_crtcs);
324 /* If drm_vblank_init() hasn't been called yet, just no-op */
328 crtc = modeset->crtc;
329 DRM_DEBUG("crtc=%d\n", crtc);
330 if (crtc >= dev->num_crtcs) {
336 * To avoid all the problems that might happen if interrupts
337 * were enabled/disabled around or between these calls, we just
338 * have the kernel take a reference on the CRTC (just once though
339 * to avoid corrupting the count if multiple, mismatch calls occur),
340 * so that interrupts remain enabled in the interim.
342 switch (modeset->cmd) {
343 case _DRM_PRE_MODESET:
344 DRM_DEBUG("pre-modeset\n");
345 if (!dev->vblank[crtc].inmodeset) {
346 dev->vblank[crtc].inmodeset = 1;
347 drm_vblank_get(dev, crtc);
350 case _DRM_POST_MODESET:
351 DRM_DEBUG("post-modeset\n");
352 if (dev->vblank[crtc].inmodeset) {
353 DRM_SPINLOCK_IRQSAVE(&dev->vbl_lock, irqflags);
354 dev->vblank_disable_allowed = 1;
355 dev->vblank[crtc].inmodeset = 0;
356 DRM_SPINUNLOCK_IRQRESTORE(&dev->vbl_lock, irqflags);
357 drm_vblank_put(dev, crtc);
369 int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_priv)
371 union drm_wait_vblank *vblwait = data;
372 unsigned int flags, seq, crtc;
375 if (!dev->irq_enabled)
378 if (vblwait->request.type &
379 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
380 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
381 vblwait->request.type,
382 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
386 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
387 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
389 if (crtc >= dev->num_crtcs)
392 ret = drm_vblank_get(dev, crtc);
394 DRM_ERROR("failed to acquire vblank counter, %d\n", ret);
397 seq = drm_vblank_count(dev, crtc);
399 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
400 case _DRM_VBLANK_RELATIVE:
401 vblwait->request.sequence += seq;
402 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
403 case _DRM_VBLANK_ABSOLUTE:
410 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
411 (seq - vblwait->request.sequence) <= (1<<23)) {
412 vblwait->request.sequence = seq + 1;
415 if (flags & _DRM_VBLANK_SIGNAL) {
417 drm_vbl_sig_t *vbl_sig = malloc(sizeof(drm_vbl_sig_t),
418 DRM_MEM_DRIVER, M_NOWAIT | M_ZERO);
422 vbl_sig->sequence = vblwait->request.sequence;
423 vbl_sig->signo = vblwait->request.signal;
424 vbl_sig->pid = DRM_CURRENTPID;
426 vblwait->reply.sequence = atomic_read(&dev->vbl_received);
428 DRM_SPINLOCK(&dev->vbl_lock);
429 TAILQ_INSERT_HEAD(&dev->vbl_sig_list, vbl_sig, link);
430 DRM_SPINUNLOCK(&dev->vbl_lock);
435 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
436 vblwait->request.sequence, crtc);
437 for ( ret = 0 ; !ret && !((drm_vblank_count(dev, crtc) -
438 vblwait->request.sequence) <= (1 << 23)) ; ) {
439 mtx_lock(&dev->irq_lock);
440 if (!((drm_vblank_count(dev, crtc) -
441 vblwait->request.sequence) <= (1 << 23)))
442 ret = mtx_sleep(&dev->vblank[crtc].queue,
443 &dev->irq_lock, PCATCH, "vblwtq",
445 mtx_unlock(&dev->irq_lock);
448 DRM_DEBUG("return = %d\n", ret);
453 vblwait->reply.tval_sec = now.tv_sec;
454 vblwait->reply.tval_usec = now.tv_usec;
455 vblwait->reply.sequence = drm_vblank_count(dev, crtc);
456 DRM_DEBUG("returning %d to client\n",
457 vblwait->reply.sequence);
459 DRM_DEBUG("vblank wait interrupted by signal\n");
464 drm_vblank_put(dev, crtc);
468 void drm_vbl_send_signals(struct drm_device *dev, int crtc)
473 void drm_vbl_send_signals(struct drm_device *dev, int crtc )
475 drm_vbl_sig_t *vbl_sig;
476 unsigned int vbl_seq = atomic_read( &dev->vbl_received );
479 vbl_sig = TAILQ_FIRST(&dev->vbl_sig_list);
480 while (vbl_sig != NULL) {
481 drm_vbl_sig_t *next = TAILQ_NEXT(vbl_sig, link);
483 if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
484 p = pfind(vbl_sig->pid);
486 psignal(p, vbl_sig->signo);
488 TAILQ_REMOVE(&dev->vbl_sig_list, vbl_sig, link);
489 DRM_FREE(vbl_sig,sizeof(*vbl_sig));
496 void drm_handle_vblank(struct drm_device *dev, int crtc)
498 atomic_inc(&dev->vblank[crtc].count);
499 DRM_WAKEUP(&dev->vblank[crtc].queue);
500 drm_vbl_send_signals(dev, crtc);