]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/libdrm.git/blob - bsd-core/drm_irq.c
libdrm_radeon: add radeon_bo_is_referenced_by_cs function
[glsdk/libdrm.git] / bsd-core / drm_irq.c
1 /*-
2  * Copyright 2003 Eric Anholt
3  * All Rights Reserved.
4  *
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:
11  *
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
14  * Software.
15  *
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.
22  *
23  * Authors:
24  *    Eric Anholt <anholt@FreeBSD.org>
25  *
26  */
28 /** @file drm_irq.c
29  * Support code for handling setup/teardown of interrupt handlers and
30  * handing interrupt handlers off to the drivers.
31  */
33 #include "drmP.h"
34 #include "drm.h"
36 int drm_irq_by_busid(struct drm_device *dev, void *data,
37                      struct drm_file *file_priv)
38 {
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)
45                 return EINVAL;
47         irq->irq = dev->irq;
49         DRM_DEBUG("%d:%d:%d => IRQ %d\n",
50             irq->busnum, irq->devnum, irq->funcnum, irq->irq);
52         return 0;
53 }
55 static irqreturn_t
56 drm_irq_handler_wrap(DRM_IRQ_ARGS)
57 {
58         struct drm_device *dev = arg;
60         DRM_SPINLOCK(&dev->irq_lock);
61         dev->driver->irq_handler(arg);
62         DRM_SPINUNLOCK(&dev->irq_lock);
63 }
65 static void vblank_disable_fn(void *arg)
66 {
67         struct drm_device *dev = (struct drm_device *)arg;
68         int i;
70         if (callout_pending(&dev->vblank_disable_timer)) {
71                 /* callout was reset */
72                 return;
73         }
74         if (!callout_active(&dev->vblank_disable_timer)) {
75                 /* callout was stopped */
76                 return;
77         }
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)
82                 return;
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);
88                         dev->vblank[i].last =
89                             dev->driver->get_vblank_counter(dev, i);
90                         dev->driver->disable_vblank(dev, i);
91                         dev->vblank[i].enabled = 0;
92                 }
93         }
94 }
96 void drm_vblank_cleanup(struct drm_device *dev)
97 {
98         unsigned long irqflags;
100         /* Bail if the driver didn't call drm_vblank_init() */
101         if (dev->num_crtcs == 0)
102                 return;
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);
114         dev->num_crtcs = 0;
117 int drm_vblank_init(struct drm_device *dev, int num_crtcs)
119         int i, ret = ENOMEM;
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);
127         if (!dev->vblank)
128             goto err;
130         DRM_DEBUG("\n");
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);
138         }
140         dev->vblank_disable_allowed = 0;
142         return 0;
144 err:
145         drm_vblank_cleanup(dev);
146         return ret;
149 int drm_irq_install(struct drm_device *dev)
151         int retcode;
153         if (dev->irq == 0 || dev->dev_private == NULL)
154                 return EINVAL;
156         DRM_DEBUG("irq=%d\n", dev->irq);
158         DRM_LOCK();
159         if (dev->irq_enabled) {
160                 DRM_UNLOCK();
161                 return EBUSY;
162         }
163         dev->irq_enabled = 1;
165         dev->context_flag = 0;
167         /* Before installing handler */
168         dev->driver->irq_preinstall(dev);
169         DRM_UNLOCK();
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);
176 #else
177         retcode = bus_setup_intr(dev->device, dev->irqr,
178                                  INTR_TYPE_TTY | INTR_MPSAFE,
179                                  drm_irq_handler_wrap, dev, &dev->irqh);
180 #endif
181         if (retcode != 0)
182                 goto err;
184         /* After installing handler */
185         DRM_LOCK();
186         dev->driver->irq_postinstall(dev);
187         DRM_UNLOCK();
189         return 0;
190 err:
191         DRM_LOCK();
192         dev->irq_enabled = 0;
193         DRM_UNLOCK();
195         return retcode;
198 int drm_irq_uninstall(struct drm_device *dev)
200         if (!dev->irq_enabled)
201                 return EINVAL;
203         dev->irq_enabled = 0;
205         DRM_DEBUG("irq=%d\n", dev->irq);
207         dev->driver->irq_uninstall(dev);
209         DRM_UNLOCK();
210         bus_teardown_intr(dev->device, dev->irqr, dev->irqh);
211         DRM_LOCK();
213         return 0;
216 int drm_control(struct drm_device *dev, void *data, struct drm_file *file_priv)
218         struct drm_control *ctl = data;
219         int err;
221         switch (ctl->func) {
222         case DRM_INST_HANDLER:
223                 /* Handle drivers whose DRM used to require IRQ setup but the
224                  * no longer does.
225                  */
226                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
227                         return 0;
228                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
229                     ctl->irq != dev->irq)
230                         return EINVAL;
231                 return drm_irq_install(dev);
232         case DRM_UNINST_HANDLER:
233                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
234                         return 0;
235                 DRM_LOCK();
236                 err = drm_irq_uninstall(dev);
237                 DRM_UNLOCK();
238                 return err;
239         default:
240                 return EINVAL;
241         }
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;
253         /*
254          * Interrupts were disabled prior to this call, so deal with counter
255          * wrap if needed.
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
258          * a long time.
259          */
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);
267         }
269         DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
270             crtc, diff);
272         atomic_add(diff, &dev->vblank[crtc].count);
275 int drm_vblank_get(struct drm_device *dev, int crtc)
277         unsigned long irqflags;
278         int ret = 0;
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);
287                 if (ret)
288                         atomic_dec(&dev->vblank[crtc].refcount);
289                 else {
290                         dev->vblank[crtc].enabled = 1;
291                         drm_update_vblank_count(dev, crtc);
292                 }
293         }
294         DRM_SPINUNLOCK_IRQRESTORE(&dev->vbl_lock, irqflags);
296         return ret;
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;
318         int crtc, ret = 0;
320         DRM_DEBUG("num_crtcs=%d\n", dev->num_crtcs);
321         /* If drm_vblank_init() hasn't been called yet, just no-op */
322         if (!dev->num_crtcs)
323                 goto out;
325         crtc = modeset->crtc;
326         DRM_DEBUG("crtc=%d\n", crtc);
327         if (crtc >= dev->num_crtcs) {
328                 ret = EINVAL;
329                 goto out;
330         }
332         /*
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.
338          */
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);
345                 }
346                 break;
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);
355                 }
356                 break;
357         default:
358                 ret = EINVAL;
359                 break;
360         }
362 out:
363         return ret;
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;
370         int ret = 0;
372         if (!dev->irq_enabled)
373                 return EINVAL;
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));
380                 return EINVAL;
381         }
383         flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
384         crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
386         if (crtc >= dev->num_crtcs)
387                 return EINVAL;
389         ret = drm_vblank_get(dev, crtc);
390         if (ret) {
391                 DRM_ERROR("failed to acquire vblank counter, %d\n", ret);
392                 return ret;
393         }
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:
401                 break;
402         default:
403                 ret = EINVAL;
404                 goto done;
405         }
407         if ((flags & _DRM_VBLANK_NEXTONMISS) &&
408             (seq - vblwait->request.sequence) <= (1<<23)) {
409                 vblwait->request.sequence = seq + 1;
410         }
412         if (flags & _DRM_VBLANK_SIGNAL) {
413 #if 0 /* disabled */
414                 drm_vbl_sig_t *vbl_sig = malloc(sizeof(drm_vbl_sig_t),
415                     DRM_MEM_DRIVER, M_NOWAIT | M_ZERO);
416                 if (vbl_sig == NULL)
417                         return ENOMEM;
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);
424                 
425                 DRM_SPINLOCK(&dev->vbl_lock);
426                 TAILQ_INSERT_HEAD(&dev->vbl_sig_list, vbl_sig, link);
427                 DRM_SPINUNLOCK(&dev->vbl_lock);
428                 ret = 0;
429 #endif
430                 ret = EINVAL;
431         } else {
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",
441                                     3 * DRM_HZ);
442                         mtx_unlock(&dev->irq_lock);
443                 }
445                 DRM_DEBUG("return = %d\n", ret);
446                 if (ret != EINTR) {
447                         struct timeval now;
449                         microtime(&now);
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);
455                 } else {
456                         DRM_DEBUG("vblank wait interrupted by signal\n");
457                 }
458         }
460 done:
461         drm_vblank_put(dev, crtc);
462         return ret;
465 void drm_vbl_send_signals(struct drm_device *dev, int crtc)
469 #if 0 /* disabled */
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 );
474         struct proc *p;
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);
482                         if (p != NULL)
483                                 psignal(p, vbl_sig->signo);
485                         TAILQ_REMOVE(&dev->vbl_sig_list, vbl_sig, link);
486                         DRM_FREE(vbl_sig,sizeof(*vbl_sig));
487                 }
488                 vbl_sig = next;
489         }
491 #endif
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);