]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/drm/drm_irq.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / drm / 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  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /** @file drm_irq.c
32  * Support code for handling setup/teardown of interrupt handlers and
33  * handing interrupt handlers off to the drivers.
34  */
35
36 #include "dev/drm/drmP.h"
37 #include "dev/drm/drm.h"
38
39 int drm_irq_by_busid(struct drm_device *dev, void *data,
40                      struct drm_file *file_priv)
41 {
42         struct drm_irq_busid *irq = data;
43
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)
48                 return EINVAL;
49
50         irq->irq = dev->irq;
51
52         DRM_DEBUG("%d:%d:%d => IRQ %d\n",
53             irq->busnum, irq->devnum, irq->funcnum, irq->irq);
54
55         return 0;
56 }
57
58 static irqreturn_t
59 drm_irq_handler_wrap(DRM_IRQ_ARGS)
60 {
61         struct drm_device *dev = arg;
62
63         DRM_SPINLOCK(&dev->irq_lock);
64         dev->driver->irq_handler(arg);
65         DRM_SPINUNLOCK(&dev->irq_lock);
66 }
67
68 static void vblank_disable_fn(void *arg)
69 {
70         struct drm_device *dev = (struct drm_device *)arg;
71         int i;
72
73         if (callout_pending(&dev->vblank_disable_timer)) {
74                 /* callout was reset */
75                 return;
76         }
77         if (!callout_active(&dev->vblank_disable_timer)) {
78                 /* callout was stopped */
79                 return;
80         }
81         callout_deactivate(&dev->vblank_disable_timer);
82
83         DRM_DEBUG("vblank_disable_allowed=%d\n", dev->vblank_disable_allowed);
84         if (!dev->vblank_disable_allowed)
85                 return;
86
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);
91                         dev->vblank[i].last =
92                             dev->driver->get_vblank_counter(dev, i);
93                         dev->driver->disable_vblank(dev, i);
94                         dev->vblank[i].enabled = 0;
95                 }
96         }
97 }
98
99 void drm_vblank_cleanup(struct drm_device *dev)
100 {
101         unsigned long irqflags;
102
103         /* Bail if the driver didn't call drm_vblank_init() */
104         if (dev->num_crtcs == 0)
105                 return;
106
107         DRM_SPINLOCK_IRQSAVE(&dev->vbl_lock, irqflags);
108         callout_stop(&dev->vblank_disable_timer);
109         DRM_SPINUNLOCK_IRQRESTORE(&dev->vbl_lock, irqflags);
110
111         callout_drain(&dev->vblank_disable_timer);
112
113         vblank_disable_fn((void *)dev);
114
115         free(dev->vblank, DRM_MEM_DRIVER);
116
117         dev->num_crtcs = 0;
118 }
119
120 int drm_vblank_init(struct drm_device *dev, int num_crtcs)
121 {
122         int i, ret = ENOMEM;
123
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;
127
128         dev->vblank = malloc(sizeof(struct drm_vblank_info) * num_crtcs,
129             DRM_MEM_DRIVER, M_NOWAIT | M_ZERO);
130         if (!dev->vblank)
131             goto err;
132
133         DRM_DEBUG("\n");
134
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);
141         }
142
143         dev->vblank_disable_allowed = 0;
144
145         return 0;
146
147 err:
148         drm_vblank_cleanup(dev);
149         return ret;
150 }
151
152 int drm_irq_install(struct drm_device *dev)
153 {
154         int retcode;
155
156         if (dev->irq == 0 || dev->dev_private == NULL)
157                 return EINVAL;
158
159         DRM_DEBUG("irq=%d\n", dev->irq);
160
161         DRM_LOCK();
162         if (dev->irq_enabled) {
163                 DRM_UNLOCK();
164                 return EBUSY;
165         }
166         dev->irq_enabled = 1;
167
168         dev->context_flag = 0;
169
170         /* Before installing handler */
171         dev->driver->irq_preinstall(dev);
172         DRM_UNLOCK();
173
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);
179 #else
180         retcode = bus_setup_intr(dev->device, dev->irqr,
181                                  INTR_TYPE_TTY | INTR_MPSAFE,
182                                  drm_irq_handler_wrap, dev, &dev->irqh);
183 #endif
184         if (retcode != 0)
185                 goto err;
186
187         /* After installing handler */
188         DRM_LOCK();
189         dev->driver->irq_postinstall(dev);
190         DRM_UNLOCK();
191
192         return 0;
193 err:
194         DRM_LOCK();
195         dev->irq_enabled = 0;
196         DRM_UNLOCK();
197
198         return retcode;
199 }
200
201 int drm_irq_uninstall(struct drm_device *dev)
202 {
203         if (!dev->irq_enabled)
204                 return EINVAL;
205
206         dev->irq_enabled = 0;
207
208         DRM_DEBUG("irq=%d\n", dev->irq);
209
210         dev->driver->irq_uninstall(dev);
211
212         DRM_UNLOCK();
213         bus_teardown_intr(dev->device, dev->irqr, dev->irqh);
214         DRM_LOCK();
215
216         return 0;
217 }
218
219 int drm_control(struct drm_device *dev, void *data, struct drm_file *file_priv)
220 {
221         struct drm_control *ctl = data;
222         int err;
223
224         switch (ctl->func) {
225         case DRM_INST_HANDLER:
226                 /* Handle drivers whose DRM used to require IRQ setup but the
227                  * no longer does.
228                  */
229                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
230                         return 0;
231                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
232                     ctl->irq != dev->irq)
233                         return EINVAL;
234                 return drm_irq_install(dev);
235         case DRM_UNINST_HANDLER:
236                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
237                         return 0;
238                 DRM_LOCK();
239                 err = drm_irq_uninstall(dev);
240                 DRM_UNLOCK();
241                 return err;
242         default:
243                 return EINVAL;
244         }
245 }
246
247 u32 drm_vblank_count(struct drm_device *dev, int crtc)
248 {
249         return atomic_read(&dev->vblank[crtc].count);
250 }
251
252 static void drm_update_vblank_count(struct drm_device *dev, int crtc)
253 {
254         u32 cur_vblank, diff;
255
256         /*
257          * Interrupts were disabled prior to this call, so deal with counter
258          * wrap if needed.
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
261          * a long time.
262          */
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;
267
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);
270         }
271
272         DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
273             crtc, diff);
274
275         atomic_add(diff, &dev->vblank[crtc].count);
276 }
277
278 int drm_vblank_get(struct drm_device *dev, int crtc)
279 {
280         unsigned long irqflags;
281         int ret = 0;
282
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);
290                 if (ret)
291                         atomic_dec(&dev->vblank[crtc].refcount);
292                 else {
293                         dev->vblank[crtc].enabled = 1;
294                         drm_update_vblank_count(dev, crtc);
295                 }
296         }
297         DRM_SPINUNLOCK_IRQRESTORE(&dev->vbl_lock, irqflags);
298
299         return ret;
300 }
301
302 void drm_vblank_put(struct drm_device *dev, int crtc)
303 {
304         unsigned long irqflags;
305
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);
314 }
315
316 int drm_modeset_ctl(struct drm_device *dev, void *data,
317                     struct drm_file *file_priv)
318 {
319         struct drm_modeset_ctl *modeset = data;
320         unsigned long irqflags;
321         int crtc, ret = 0;
322
323         DRM_DEBUG("num_crtcs=%d\n", dev->num_crtcs);
324         /* If drm_vblank_init() hasn't been called yet, just no-op */
325         if (!dev->num_crtcs)
326                 goto out;
327
328         crtc = modeset->crtc;
329         DRM_DEBUG("crtc=%d\n", crtc);
330         if (crtc >= dev->num_crtcs) {
331                 ret = EINVAL;
332                 goto out;
333         }
334
335         /*
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.
341          */
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);
348                 }
349                 break;
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);
358                 }
359                 break;
360         default:
361                 ret = EINVAL;
362                 break;
363         }
364
365 out:
366         return ret;
367 }
368
369 int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_priv)
370 {
371         union drm_wait_vblank *vblwait = data;
372         unsigned int flags, seq, crtc;
373         int ret = 0;
374
375         if (!dev->irq_enabled)
376                 return EINVAL;
377
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));
383                 return EINVAL;
384         }
385
386         flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
387         crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
388
389         if (crtc >= dev->num_crtcs)
390                 return EINVAL;
391
392         ret = drm_vblank_get(dev, crtc);
393         if (ret) {
394                 DRM_ERROR("failed to acquire vblank counter, %d\n", ret);
395                 return ret;
396         }
397         seq = drm_vblank_count(dev, crtc);
398
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:
404                 break;
405         default:
406                 ret = EINVAL;
407                 goto done;
408         }
409
410         if ((flags & _DRM_VBLANK_NEXTONMISS) &&
411             (seq - vblwait->request.sequence) <= (1<<23)) {
412                 vblwait->request.sequence = seq + 1;
413         }
414
415         if (flags & _DRM_VBLANK_SIGNAL) {
416 #if 0 /* disabled */
417                 drm_vbl_sig_t *vbl_sig = malloc(sizeof(drm_vbl_sig_t),
418                     DRM_MEM_DRIVER, M_NOWAIT | M_ZERO);
419                 if (vbl_sig == NULL)
420                         return ENOMEM;
421
422                 vbl_sig->sequence = vblwait->request.sequence;
423                 vbl_sig->signo = vblwait->request.signal;
424                 vbl_sig->pid = DRM_CURRENTPID;
425
426                 vblwait->reply.sequence = atomic_read(&dev->vbl_received);
427                 
428                 DRM_SPINLOCK(&dev->vbl_lock);
429                 TAILQ_INSERT_HEAD(&dev->vbl_sig_list, vbl_sig, link);
430                 DRM_SPINUNLOCK(&dev->vbl_lock);
431                 ret = 0;
432 #endif
433                 ret = EINVAL;
434         } else {
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",
444                                     3 * DRM_HZ);
445                         mtx_unlock(&dev->irq_lock);
446                 }
447
448                 DRM_DEBUG("return = %d\n", ret);
449                 if (ret != EINTR) {
450                         struct timeval now;
451
452                         microtime(&now);
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);
458                 } else {
459                         DRM_DEBUG("vblank wait interrupted by signal\n");
460                 }
461         }
462
463 done:
464         drm_vblank_put(dev, crtc);
465         return ret;
466 }
467
468 void drm_vbl_send_signals(struct drm_device *dev, int crtc)
469 {
470 }
471
472 #if 0 /* disabled */
473 void drm_vbl_send_signals(struct drm_device *dev, int crtc )
474 {
475         drm_vbl_sig_t *vbl_sig;
476         unsigned int vbl_seq = atomic_read( &dev->vbl_received );
477         struct proc *p;
478
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);
482
483                 if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
484                         p = pfind(vbl_sig->pid);
485                         if (p != NULL)
486                                 psignal(p, vbl_sig->signo);
487
488                         TAILQ_REMOVE(&dev->vbl_sig_list, vbl_sig, link);
489                         DRM_FREE(vbl_sig,sizeof(*vbl_sig));
490                 }
491                 vbl_sig = next;
492         }
493 }
494 #endif
495
496 void drm_handle_vblank(struct drm_device *dev, int crtc)
497 {
498         atomic_inc(&dev->vblank[crtc].count);
499         DRM_WAKEUP(&dev->vblank[crtc].queue);
500         drm_vbl_send_signals(dev, crtc);
501 }
502