Ensure that we always hold the lock when executing vblank_disable_fn().
[dragonfly.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 /** @file drm_irq.c
29  * Support code for handling setup/teardown of interrupt handlers and
30  * handing interrupt handlers off to the drivers.
31  */
32
33 #include "dev/drm/drmP.h"
34 #include "dev/drm/drm.h"
35
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;
40
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;
46
47         irq->irq = dev->irq;
48
49         DRM_DEBUG("%d:%d:%d => IRQ %d\n",
50             irq->busnum, irq->devnum, irq->funcnum, irq->irq);
51
52         return 0;
53 }
54
55 static void vblank_disable_fn(void *arg)
56 {
57         struct drm_device *dev = (struct drm_device *)arg;
58         int i;
59
60         DRM_SPINLOCK(&dev->vbl_lock);
61         
62         if (callout_pending(&dev->vblank_disable_timer)) {
63                 /* callout was reset */
64                 DRM_SPINUNLOCK(&dev->vbl_lock);
65                 return;
66         }
67         if (!callout_active(&dev->vblank_disable_timer)) {
68                 /* callout was stopped */
69                 DRM_SPINUNLOCK(&dev->vbl_lock);
70                 return;
71         }
72         callout_deactivate(&dev->vblank_disable_timer);
73
74         DRM_DEBUG("vblank_disable: %s\n", dev->vblank_disable_allowed ?
75                 "allowed" : "denied");
76         if (!dev->vblank_disable_allowed) {
77                 DRM_SPINUNLOCK(&dev->vbl_lock);
78                 return;
79         }
80
81         for (i = 0; i < dev->num_crtcs; i++) {
82                 if (atomic_read(&dev->vblank[i].refcount) == 0 &&
83                     dev->vblank[i].enabled && !dev->vblank[i].inmodeset) {
84                         DRM_DEBUG("disabling vblank on crtc %d\n", i);
85                         dev->vblank[i].last =
86                             dev->driver->get_vblank_counter(dev, i);
87                         dev->driver->disable_vblank(dev, i);
88                         dev->vblank[i].enabled = 0;
89                 }
90         }
91         DRM_SPINUNLOCK(&dev->vbl_lock);
92 }
93
94 void drm_vblank_cleanup(struct drm_device *dev)
95 {
96         /* Bail if the driver didn't call drm_vblank_init() */
97         if (dev->num_crtcs == 0)
98                 return;
99
100         DRM_SPINLOCK(&dev->vbl_lock);
101         callout_stop(&dev->vblank_disable_timer);
102         DRM_SPINUNLOCK(&dev->vbl_lock);
103
104         vblank_disable_fn((void *)dev);
105
106         free(dev->vblank, DRM_MEM_DRIVER);
107
108         dev->num_crtcs = 0;
109 }
110
111 int drm_vblank_init(struct drm_device *dev, int num_crtcs)
112 {
113         int i, ret = ENOMEM;
114
115         callout_init(&dev->vblank_disable_timer);
116         dev->num_crtcs = num_crtcs;
117
118         dev->vblank = malloc(sizeof(struct drm_vblank_info) * num_crtcs,
119             DRM_MEM_DRIVER, M_NOWAIT | M_ZERO);
120         if (!dev->vblank)
121             goto err;
122
123         DRM_DEBUG("\n");
124
125         /* Zero per-crtc vblank stuff */
126         for (i = 0; i < num_crtcs; i++) {
127                 DRM_INIT_WAITQUEUE(&dev->vblank[i].queue);
128                 atomic_set(&dev->vblank[i].count, 0);
129                 atomic_set(&dev->vblank[i].refcount, 0);
130         }
131
132         dev->vblank_disable_allowed = 0;
133
134         return 0;
135
136 err:
137         drm_vblank_cleanup(dev);
138         return ret;
139 }
140
141 int drm_irq_install(struct drm_device *dev)
142 {
143         int crtc, retcode;
144
145         if (dev->irq == 0 || dev->dev_private == NULL)
146                 return EINVAL;
147
148         DRM_DEBUG("irq=%d\n", dev->irq);
149
150         DRM_LOCK();
151         if (dev->irq_enabled) {
152                 DRM_UNLOCK();
153                 return EBUSY;
154         }
155         dev->irq_enabled = 1;
156
157         dev->context_flag = 0;
158
159         /* Before installing handler */
160         dev->driver->irq_preinstall(dev);
161         DRM_UNLOCK();
162
163         /* Install handler */
164         retcode = bus_setup_intr(dev->device, dev->irqr, INTR_MPSAFE,
165                                  dev->driver->irq_handler, dev, &dev->irqh,
166                                  &dev->irq_lock);
167         if (retcode != 0)
168                 goto err;
169
170         /* After installing handler */
171         DRM_LOCK();
172         dev->driver->irq_postinstall(dev);
173         DRM_UNLOCK();
174         if (dev->driver->enable_vblank) {
175                 DRM_SPINLOCK(&dev->vbl_lock);
176                 for( crtc = 0 ; crtc < dev->num_crtcs ; crtc++) {
177                         if (dev->driver->enable_vblank(dev, crtc) == 0) {
178                                 dev->vblank[crtc].enabled = 1;
179                         }
180                 }
181                 callout_reset(&dev->vblank_disable_timer, 5 * DRM_HZ,
182                     (timeout_t *)vblank_disable_fn, (void *)dev);
183                 DRM_SPINUNLOCK(&dev->vbl_lock);
184         }
185
186         return 0;
187 err:
188         DRM_LOCK();
189         dev->irq_enabled = 0;
190         DRM_UNLOCK();
191
192         return retcode;
193 }
194
195 int drm_irq_uninstall(struct drm_device *dev)
196 {
197         int crtc;
198
199         if (!dev->irq_enabled)
200                 return EINVAL;
201
202         dev->irq_enabled = 0;
203
204         /*
205         * Wake up any waiters so they don't hang.
206         */
207         DRM_SPINLOCK(&dev->vbl_lock);
208         for (crtc = 0; crtc < dev->num_crtcs; crtc++) {
209                 if (dev->vblank[crtc].enabled) {
210                         DRM_WAKEUP(&dev->vblank[crtc].queue);
211                         dev->vblank[crtc].last =
212                             dev->driver->get_vblank_counter(dev, crtc);
213                         dev->vblank[crtc].enabled = 0;
214                 }
215         }
216         DRM_SPINUNLOCK(&dev->vbl_lock);
217
218         DRM_DEBUG("irq=%d\n", dev->irq);
219
220         dev->driver->irq_uninstall(dev);
221
222         DRM_UNLOCK();
223         bus_teardown_intr(dev->device, dev->irqr, dev->irqh);
224         DRM_LOCK();
225
226         return 0;
227 }
228
229 int drm_control(struct drm_device *dev, void *data, struct drm_file *file_priv)
230 {
231         struct drm_control *ctl = data;
232         int err;
233
234         switch (ctl->func) {
235         case DRM_INST_HANDLER:
236                 /* Handle drivers whose DRM used to require IRQ setup but the
237                  * no longer does.
238                  */
239                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
240                         return 0;
241                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
242                     ctl->irq != dev->irq)
243                         return EINVAL;
244                 return drm_irq_install(dev);
245         case DRM_UNINST_HANDLER:
246                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
247                         return 0;
248                 DRM_LOCK();
249                 err = drm_irq_uninstall(dev);
250                 DRM_UNLOCK();
251                 return err;
252         default:
253                 return EINVAL;
254         }
255 }
256
257 u32 drm_vblank_count(struct drm_device *dev, int crtc)
258 {
259         return atomic_read(&dev->vblank[crtc].count);
260 }
261
262 static void drm_update_vblank_count(struct drm_device *dev, int crtc)
263 {
264         u32 cur_vblank, diff;
265
266         /*
267          * Interrupts were disabled prior to this call, so deal with counter
268          * wrap if needed.
269          * NOTE!  It's possible we lost a full dev->max_vblank_count events
270          * here if the register is small or we had vblank interrupts off for
271          * a long time.
272          */
273         cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
274         diff = cur_vblank - dev->vblank[crtc].last;
275         if (cur_vblank < dev->vblank[crtc].last) {
276                 diff += dev->max_vblank_count;
277
278                 DRM_DEBUG("vblank[%d].last=0x%x, cur_vblank=0x%x => diff=0x%x\n",
279                     crtc, dev->vblank[crtc].last, cur_vblank, diff);
280         }
281
282         DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
283             crtc, diff);
284
285         atomic_add(diff, &dev->vblank[crtc].count);
286 }
287
288 int drm_vblank_get(struct drm_device *dev, int crtc)
289 {
290         int ret = 0;
291
292         DRM_SPINLOCK(&dev->vbl_lock);
293         /* Going from 0->1 means we have to enable interrupts again */
294         atomic_add_acq_int(&dev->vblank[crtc].refcount, 1);
295         if (dev->vblank[crtc].refcount == 1 &&
296             !dev->vblank[crtc].enabled) {
297                 ret = dev->driver->enable_vblank(dev, crtc);
298                 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
299                 if (ret)
300                         atomic_dec(&dev->vblank[crtc].refcount);
301                 else {
302                         dev->vblank[crtc].enabled = 1;
303                         drm_update_vblank_count(dev, crtc);
304                 }
305         }
306         DRM_SPINUNLOCK(&dev->vbl_lock);
307
308         return ret;
309 }
310
311 void drm_vblank_put(struct drm_device *dev, int crtc)
312 {
313         KASSERT(atomic_read(&dev->vblank[crtc].refcount) > 0,
314             ("invalid refcount"));
315
316         /* Last user schedules interrupt disable */
317         atomic_subtract_acq_int(&dev->vblank[crtc].refcount, 1);
318
319         DRM_SPINLOCK(&dev->vbl_lock);
320         if (dev->vblank[crtc].refcount == 0)
321             callout_reset(&dev->vblank_disable_timer, 5 * DRM_HZ,
322                 (timeout_t *)vblank_disable_fn, (void *)dev);
323         DRM_SPINUNLOCK(&dev->vbl_lock);
324 }
325
326 int drm_modeset_ctl(struct drm_device *dev, void *data,
327                     struct drm_file *file_priv)
328 {
329         struct drm_modeset_ctl *modeset = data;
330         int crtc, ret = 0;
331
332         DRM_DEBUG("num_crtcs=%d\n", dev->num_crtcs);
333         /* If drm_vblank_init() hasn't been called yet, just no-op */
334         if (!dev->num_crtcs)
335                 goto out;
336
337         crtc = modeset->crtc;
338         DRM_DEBUG("crtc=%d\n", crtc);
339         if (crtc >= dev->num_crtcs) {
340                 ret = EINVAL;
341                 goto out;
342         }
343
344         /*
345          * To avoid all the problems that might happen if interrupts
346          * were enabled/disabled around or between these calls, we just
347          * have the kernel take a reference on the CRTC (just once though
348          * to avoid corrupting the count if multiple, mismatch calls occur),
349          * so that interrupts remain enabled in the interim.
350          */
351         switch (modeset->cmd) {
352         case _DRM_PRE_MODESET:
353                 DRM_DEBUG("pre-modeset\n");
354                 if (!dev->vblank[crtc].inmodeset) {
355                         dev->vblank[crtc].inmodeset = 0x1;
356                         if (drm_vblank_get(dev, crtc) == 0)
357                                 dev->vblank[crtc].inmodeset |= 0x2;
358                 }
359                 break;
360         case _DRM_POST_MODESET:
361                 DRM_DEBUG("post-modeset\n");
362                 if (dev->vblank[crtc].inmodeset) {
363                         DRM_SPINLOCK(&dev->vbl_lock);
364                         dev->vblank_disable_allowed = 1;
365                         DRM_SPINUNLOCK(&dev->vbl_lock);
366
367                         if (dev->vblank[crtc].inmodeset & 0x2)
368                                 drm_vblank_put(dev, crtc);
369
370                         dev->vblank[crtc].inmodeset = 0;
371                 }
372                 break;
373         default:
374                 ret = EINVAL;
375                 break;
376         }
377
378 out:
379         return ret;
380 }
381
382 int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_priv)
383 {
384         union drm_wait_vblank *vblwait = data;
385         unsigned int flags, seq, crtc;
386         int ret = 0;
387
388         if (!dev->irq_enabled)
389                 return EINVAL;
390
391         if (vblwait->request.type &
392             ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
393                 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
394                     vblwait->request.type,
395                     (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
396                 return EINVAL;
397         }
398
399         flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
400         crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
401
402         if (crtc >= dev->num_crtcs)
403                 return EINVAL;
404
405         ret = drm_vblank_get(dev, crtc);
406         if (ret) {
407                 DRM_ERROR("failed to acquire vblank counter, %d\n", ret);
408                 return ret;
409         }
410         seq = drm_vblank_count(dev, crtc);
411
412         switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
413         case _DRM_VBLANK_RELATIVE:
414                 vblwait->request.sequence += seq;
415                 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
416         case _DRM_VBLANK_ABSOLUTE:
417                 break;
418         default:
419                 ret = EINVAL;
420                 goto done;
421         }
422
423         if ((flags & _DRM_VBLANK_NEXTONMISS) &&
424             (seq - vblwait->request.sequence) <= (1<<23)) {
425                 vblwait->request.sequence = seq + 1;
426         }
427
428         if (flags & _DRM_VBLANK_SIGNAL) {
429                 /* There have never been any consumers */
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                     !dev->irq_enabled) ; ) {
437                         lwkt_serialize_enter(&dev->irq_lock);
438                         if (!(((drm_vblank_count(dev, crtc) -
439                             vblwait->request.sequence) <= (1 << 23)) ||
440                             !dev->irq_enabled))
441                                 ret = serialize_sleep(&dev->vblank[crtc].queue,
442                                     &dev->irq_lock, PCATCH, "vblwtq",
443                                     DRM_HZ);
444                         lwkt_serialize_exit(&dev->irq_lock);
445                 }
446
447                 if (ret != EINTR && ret != ERESTART) {
448                         struct timeval now;
449
450                         microtime(&now);
451                         vblwait->reply.tval_sec = now.tv_sec;
452                         vblwait->reply.tval_usec = now.tv_usec;
453                         vblwait->reply.sequence = drm_vblank_count(dev, crtc);
454                         DRM_DEBUG("returning %d to client, irq_enabled %d\n",
455                             vblwait->reply.sequence, dev->irq_enabled);
456                 } else {
457                         DRM_DEBUG("vblank wait interrupted by signal\n");
458                 }
459         }
460
461 done:
462         drm_vblank_put(dev, crtc);
463         return ret;
464 }
465
466 void drm_handle_vblank(struct drm_device *dev, int crtc)
467 {
468         atomic_inc(&dev->vblank[crtc].count);
469         DRM_WAKEUP(&dev->vblank[crtc].queue);
470 }
471