blob: 3d3640a3bd7894b740f8ccfa4ac3328c8b970e46 [file] [log] [blame]
kz15163460405de2006-09-28 01:41:18 -07001/*
kz15163460405de2006-09-28 01:41:18 -07002 * drm_irq.c -- IRQ IOCTL and function support
3 * Created: Fri Oct 18 2003 by anholt@FreeBSD.org
4 */
5/*
6 * Copyright 2003 Eric Anholt
miao chen - Sun Microsystems - Beijing China0035d212009-12-05 13:25:40 +08007 * Copyright (c) 2009, Intel Corporation.
kz15163460405de2006-09-28 01:41:18 -07008 * All Rights Reserved.
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice (including the next
18 * paragraph) shall be included in all copies or substantial portions of the
19 * Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 * Authors:
29 * Eric Anholt <anholt@FreeBSD.org>
30 *
31 */
32
zw161486e92e3a82007-05-09 19:58:49 -070033/*
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +080034 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
zw161486e92e3a82007-05-09 19:58:49 -070035 * Use is subject to license terms.
36 */
37
kz15163460405de2006-09-28 01:41:18 -070038#include "drmP.h"
39#include "drm.h"
cg149915d0538f62008-01-09 19:45:15 -080040#include "drm_io32.h"
kz15163460405de2006-09-28 01:41:18 -070041
42/*ARGSUSED*/
43int
44drm_irq_by_busid(DRM_IOCTL_ARGS)
45{
46 DRM_DEVICE;
47 drm_irq_busid_t irq;
48
cg149915d0538f62008-01-09 19:45:15 -080049 DRM_COPYFROM_WITH_RETURN(&irq, (void *)data, sizeof (irq));
kz15163460405de2006-09-28 01:41:18 -070050
51 if ((irq.busnum >> 8) != dev->pci_domain ||
52 (irq.busnum & 0xff) != dev->pci_bus ||
53 irq.devnum != dev->pci_slot ||
54 irq.funcnum != dev->pci_func)
cg149915d0538f62008-01-09 19:45:15 -080055 return (EINVAL);
kz15163460405de2006-09-28 01:41:18 -070056
57 irq.irq = dev->irq;
58
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +080059 DRM_DEBUG("%d:%d:%d => IRQ %d\n",
60 irq.busnum, irq.devnum, irq.funcnum, irq.irq);
61
cg149915d0538f62008-01-09 19:45:15 -080062 DRM_COPYTO_WITH_RETURN((void *)data, &irq, sizeof (irq));
kz15163460405de2006-09-28 01:41:18 -070063
64 return (0);
65}
66
cg149915d0538f62008-01-09 19:45:15 -080067
68static irqreturn_t
69drm_irq_handler_wrap(DRM_IRQ_ARGS)
70{
71 drm_device_t *dev = (void *)arg;
72 int ret;
73
74 mutex_enter(&dev->irq_lock);
75 ret = dev->driver->irq_handler(arg);
76 mutex_exit(&dev->irq_lock);
77
78 return (ret);
79}
80
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +080081static void vblank_disable_fn(void *arg)
82{
83 struct drm_device *dev = (struct drm_device *)arg;
84 int i;
85
Edward Shu85664792009-03-30 08:56:42 +080086 if (!dev->vblank_disable_allowed)
87 return;
88
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +080089 for (i = 0; i < dev->num_crtcs; i++) {
90 if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
91 atomic_read(&dev->vblank_enabled[i]) == 1) {
92 dev->last_vblank[i] =
93 dev->driver->get_vblank_counter(dev, i);
94 dev->driver->disable_vblank(dev, i);
95 atomic_set(&dev->vblank_enabled[i], 0);
96 DRM_DEBUG("disable vblank");
97 }
98 }
99}
100
Edward Shu85664792009-03-30 08:56:42 +0800101void
102drm_vblank_cleanup(struct drm_device *dev)
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800103{
Edward Shu85664792009-03-30 08:56:42 +0800104
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800105 /* Bail if the driver didn't call drm_vblank_init() */
106 if (dev->num_crtcs == 0)
107 return;
108
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800109 vblank_disable_fn((void *)dev);
110
111 drm_free(dev->vbl_queues, sizeof (wait_queue_head_t) * dev->num_crtcs,
112 DRM_MEM_DRIVER);
113 drm_free(dev->vbl_sigs, sizeof (struct drm_vbl_sig) * dev->num_crtcs,
114 DRM_MEM_DRIVER);
115 drm_free(dev->_vblank_count, sizeof (atomic_t) *
116 dev->num_crtcs, DRM_MEM_DRIVER);
117 drm_free(dev->vblank_refcount, sizeof (atomic_t) *
118 dev->num_crtcs, DRM_MEM_DRIVER);
119 drm_free(dev->vblank_enabled, sizeof (int) *
120 dev->num_crtcs, DRM_MEM_DRIVER);
121 drm_free(dev->last_vblank, sizeof (u32) * dev->num_crtcs,
122 DRM_MEM_DRIVER);
miao chen - Sun Microsystems - Beijing China0035d212009-12-05 13:25:40 +0800123 drm_free(dev->vblank_inmodeset, sizeof (*dev->vblank_inmodeset) *
124 dev->num_crtcs, DRM_MEM_DRIVER);
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800125 dev->num_crtcs = 0;
126}
127
128int
129drm_vblank_init(struct drm_device *dev, int num_crtcs)
130{
131 int i, ret = ENOMEM;
132
133 atomic_set(&dev->vbl_signal_pending, 0);
134 dev->num_crtcs = num_crtcs;
135
136
137 dev->vbl_queues = drm_alloc(sizeof (wait_queue_head_t) * num_crtcs,
138 DRM_MEM_DRIVER);
139 if (!dev->vbl_queues)
140 goto err;
141
142 dev->vbl_sigs = drm_alloc(sizeof (struct drm_vbl_sig) * num_crtcs,
143 DRM_MEM_DRIVER);
144 if (!dev->vbl_sigs)
145 goto err;
146
147 dev->_vblank_count = drm_alloc(sizeof (atomic_t) * num_crtcs,
148 DRM_MEM_DRIVER);
149 if (!dev->_vblank_count)
150 goto err;
151
152 dev->vblank_refcount = drm_alloc(sizeof (atomic_t) * num_crtcs,
153 DRM_MEM_DRIVER);
154 if (!dev->vblank_refcount)
155 goto err;
156
157 dev->vblank_enabled = drm_alloc(num_crtcs * sizeof (int),
158 DRM_MEM_DRIVER);
159 if (!dev->vblank_enabled)
160 goto err;
161
162 dev->last_vblank = drm_alloc(num_crtcs * sizeof (u32), DRM_MEM_DRIVER);
163 if (!dev->last_vblank)
164 goto err;
miao chen - Sun Microsystems - Beijing China0035d212009-12-05 13:25:40 +0800165
166 dev->vblank_inmodeset = drm_alloc(num_crtcs * sizeof (int),
167 DRM_MEM_DRIVER);
168 if (!dev->vblank_inmodeset)
169 goto err;
170
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800171 /* Zero per-crtc vblank stuff */
172 for (i = 0; i < num_crtcs; i++) {
173 DRM_INIT_WAITQUEUE(&dev->vbl_queues[i], DRM_INTR_PRI(dev));
174 TAILQ_INIT(&dev->vbl_sigs[i]);
175 atomic_set(&dev->_vblank_count[i], 0);
176 atomic_set(&dev->vblank_refcount[i], 0);
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800177 }
Edward Shu85664792009-03-30 08:56:42 +0800178
179 dev->vblank_disable_allowed = 1;
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800180 return (0);
181
182err:
183 DRM_ERROR("drm_vblank_init: alloc error");
184 drm_vblank_cleanup(dev);
185 return (ret);
186}
cg149915d0538f62008-01-09 19:45:15 -0800187
kz15163460405de2006-09-28 01:41:18 -0700188/*ARGSUSED*/
189static int
cg149915d0538f62008-01-09 19:45:15 -0800190drm_install_irq_handle(drm_device_t *dev)
kz15163460405de2006-09-28 01:41:18 -0700191{
cg149915d0538f62008-01-09 19:45:15 -0800192 dev_info_t *dip = dev->dip;
kz15163460405de2006-09-28 01:41:18 -0700193
kz15163460405de2006-09-28 01:41:18 -0700194 if (dip == NULL) {
195 DRM_ERROR("drm_install_irq_handle: cannot get vgatext's dip");
196 return (DDI_FAILURE);
197 }
198
199 if (ddi_intr_hilevel(dip, 0) != 0) {
200 DRM_ERROR("drm_install_irq_handle: "
cg149915d0538f62008-01-09 19:45:15 -0800201 "high-level interrupts are not supported");
kz15163460405de2006-09-28 01:41:18 -0700202 return (DDI_FAILURE);
203 }
204
205 if (ddi_get_iblock_cookie(dip, (uint_t)0,
206 &dev->intr_block) != DDI_SUCCESS) {
207 DRM_ERROR("drm_install_irq_handle: cannot get iblock cookie");
208 return (DDI_FAILURE);
209 }
210
kz15163460405de2006-09-28 01:41:18 -0700211 /* setup the interrupt handler */
kz15163460405de2006-09-28 01:41:18 -0700212 if (ddi_add_intr(dip, 0, &dev->intr_block,
cg149915d0538f62008-01-09 19:45:15 -0800213 (ddi_idevice_cookie_t *)NULL, drm_irq_handler_wrap,
214 (caddr_t)dev) != DDI_SUCCESS) {
kz15163460405de2006-09-28 01:41:18 -0700215 DRM_ERROR("drm_install_irq_handle: ddi_add_intr failed");
216 return (DDI_FAILURE);
217 }
kz15163460405de2006-09-28 01:41:18 -0700218
219 return (DDI_SUCCESS);
220}
221
222/*ARGSUSED*/
223int
cg149915d0538f62008-01-09 19:45:15 -0800224drm_irq_install(drm_device_t *dev)
kz15163460405de2006-09-28 01:41:18 -0700225{
226 int ret;
227
228 if (dev->dev_private == NULL) {
229 DRM_ERROR("drm_irq_install: dev_private is NULL");
cg149915d0538f62008-01-09 19:45:15 -0800230 return (EINVAL);
kz15163460405de2006-09-28 01:41:18 -0700231 }
232
233 if (dev->irq_enabled) {
234 DRM_ERROR("drm_irq_install: irq already enabled");
cg149915d0538f62008-01-09 19:45:15 -0800235 return (EBUSY);
kz15163460405de2006-09-28 01:41:18 -0700236 }
miao chen - Sun Microsystems - Beijing China0f7bfed2009-03-03 10:15:44 +0800237
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800238 DRM_DEBUG("drm_irq_install irq=%d\n", dev->irq);
cg149915d0538f62008-01-09 19:45:15 -0800239
kz15163460405de2006-09-28 01:41:18 -0700240 /* before installing handler */
miao chen - Sun Microsystems - Beijing China0f7bfed2009-03-03 10:15:44 +0800241 ret = dev->driver->irq_preinstall(dev);
242 if (ret)
243 return (EINVAL);
kz15163460405de2006-09-28 01:41:18 -0700244
245 /* install handler */
246 ret = drm_install_irq_handle(dev);
247 if (ret != DDI_SUCCESS) {
248 DRM_ERROR("drm_irq_install: drm_install_irq_handle failed");
249 return (ret);
250 }
251
252 /* after installing handler */
cg149915d0538f62008-01-09 19:45:15 -0800253 dev->driver->irq_postinstall(dev);
kz15163460405de2006-09-28 01:41:18 -0700254
miao chen - Sun Microsystems - Beijing China0f7bfed2009-03-03 10:15:44 +0800255 dev->irq_enabled = 1;
256 dev->context_flag = 0;
257
cg149915d0538f62008-01-09 19:45:15 -0800258 return (0);
kz15163460405de2006-09-28 01:41:18 -0700259}
260
261static void
262drm_uninstall_irq_handle(drm_device_t *dev)
263{
264 ASSERT(dev->dip);
265 ddi_remove_intr(dev->dip, 0, dev->intr_block);
266}
267
268
269/*ARGSUSED*/
270int
cg149915d0538f62008-01-09 19:45:15 -0800271drm_irq_uninstall(drm_device_t *dev)
kz15163460405de2006-09-28 01:41:18 -0700272{
Edward Shu85664792009-03-30 08:56:42 +0800273 int i;
zw161486e92e3a82007-05-09 19:58:49 -0700274 if (!dev->irq_enabled) {
cg149915d0538f62008-01-09 19:45:15 -0800275 return (EINVAL);
zw161486e92e3a82007-05-09 19:58:49 -0700276 }
kz15163460405de2006-09-28 01:41:18 -0700277 dev->irq_enabled = 0;
Edward Shu85664792009-03-30 08:56:42 +0800278
279 /*
280 * Wake up any waiters so they don't hang.
281 */
282 DRM_SPINLOCK(&dev->vbl_lock);
283 for (i = 0; i < dev->num_crtcs; i++) {
284 DRM_WAKEUP(&dev->vbl_queues[i]);
285 dev->vblank_enabled[i] = 0;
286 }
287 DRM_SPINUNLOCK(&dev->vbl_lock);
288
cg149915d0538f62008-01-09 19:45:15 -0800289 dev->driver->irq_uninstall(dev);
kz15163460405de2006-09-28 01:41:18 -0700290 drm_uninstall_irq_handle(dev);
zw161486e92e3a82007-05-09 19:58:49 -0700291 dev->locked_tasklet_func = NULL;
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800292
kz15163460405de2006-09-28 01:41:18 -0700293 return (DDI_SUCCESS);
294}
295
296/*ARGSUSED*/
297int
298drm_control(DRM_IOCTL_ARGS)
299{
300 DRM_DEVICE;
301 drm_control_t ctl;
302 int err;
303
cg149915d0538f62008-01-09 19:45:15 -0800304 DRM_COPYFROM_WITH_RETURN(&ctl, (void *)data, sizeof (ctl));
kz15163460405de2006-09-28 01:41:18 -0700305
306 switch (ctl.func) {
307 case DRM_INST_HANDLER:
308 /*
309 * Handle drivers whose DRM used to require IRQ setup but the
310 * no longer does.
311 */
312 return (drm_irq_install(dev));
313 case DRM_UNINST_HANDLER:
314 err = drm_irq_uninstall(dev);
315 return (err);
316 default:
cg149915d0538f62008-01-09 19:45:15 -0800317 return (EINVAL);
kz15163460405de2006-09-28 01:41:18 -0700318 }
319}
320
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800321u32
322drm_vblank_count(struct drm_device *dev, int crtc)
323{
324 return (atomic_read(&dev->_vblank_count[crtc]));
325}
326
327static void drm_update_vblank_count(struct drm_device *dev, int crtc)
328{
329 u32 cur_vblank, diff;
330 /*
331 * Interrupts were disabled prior to this call, so deal with counter
332 * wrap if needed.
333 * NOTE! It's possible we lost a full dev->max_vblank_count events
334 * here if the register is small or we had vblank interrupts off for
335 * a long time.
336 */
337 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
338 diff = cur_vblank - dev->last_vblank[crtc];
339 if (cur_vblank < dev->last_vblank[crtc]) {
340 diff += dev->max_vblank_count;
341 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
342 crtc, dev->last_vblank[crtc], cur_vblank, diff);
343 }
344
345 atomic_add(diff, &dev->_vblank_count[crtc]);
346}
347
348static timeout_id_t timer_id = NULL;
349
350int
351drm_vblank_get(struct drm_device *dev, int crtc)
352{
353 int ret = 0;
354
355 DRM_SPINLOCK(&dev->vbl_lock);
356
357 if (timer_id != NULL) {
358 (void) untimeout(timer_id);
359 timer_id = NULL;
360 }
361
362 /* Going from 0->1 means we have to enable interrupts again */
363 atomic_add(1, &dev->vblank_refcount[crtc]);
364 if (dev->vblank_refcount[crtc] == 1 &&
365 atomic_read(&dev->vblank_enabled[crtc]) == 0) {
366 ret = dev->driver->enable_vblank(dev, crtc);
367 if (ret)
368 atomic_dec(&dev->vblank_refcount[crtc]);
369 else {
370 atomic_set(&dev->vblank_enabled[crtc], 1);
371 drm_update_vblank_count(dev, crtc);
372 }
373 }
374 DRM_SPINUNLOCK(&dev->vbl_lock);
375
376 return (ret);
377}
378
379void
380drm_vblank_put(struct drm_device *dev, int crtc)
381{
382 DRM_SPINLOCK(&dev->vbl_lock);
383 /* Last user schedules interrupt disable */
384 atomic_dec(&dev->vblank_refcount[crtc]);
385
386 if (dev->vblank_refcount[crtc] == 0)
387 timer_id = timeout(vblank_disable_fn, (void *) dev, 5*DRM_HZ);
388
389 DRM_SPINUNLOCK(&dev->vbl_lock);
390}
391
miao chen - Sun Microsystems - Beijing China0035d212009-12-05 13:25:40 +0800392/*
393 * drm_modeset_ctl - handle vblank event counter changes across mode switch
394 * @DRM_IOCTL_ARGS: standard ioctl arguments
395 *
396 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
397 * ioctls around modesetting so that any lost vblank events are accounted for.
398 *
399 * Generally the counter will reset across mode sets. If interrupts are
400 * enabled around this call, we don't have to do anything since the counter
401 * will have already been incremented.
402 */
403/*ARGSUSED*/
404int
405drm_modeset_ctl(DRM_IOCTL_ARGS)
406{
407 DRM_DEVICE;
408 struct drm_modeset_ctl modeset;
409 int crtc, ret = 0;
410
411 /* If drm_vblank_init() hasn't been called yet, just no-op */
412 if (!dev->num_crtcs)
413 goto out;
414
415 DRM_COPYFROM_WITH_RETURN(&modeset, (void *)data,
416 sizeof (modeset));
417
418 crtc = modeset.crtc;
419 if (crtc >= dev->num_crtcs) {
420 ret = -EINVAL;
421 goto out;
422 }
423
424 /*
425 * To avoid all the problems that might happen if interrupts
426 * were enabled/disabled around or between these calls, we just
427 * have the kernel take a reference on the CRTC (just once though
428 * to avoid corrupting the count if multiple, mismatch calls occur),
429 * so that interrupts remain enabled in the interim.
430 */
431 switch (modeset.cmd) {
432 case _DRM_PRE_MODESET:
433 if (!dev->vblank_inmodeset[crtc]) {
434 dev->vblank_inmodeset[crtc] = 1;
435 ret = drm_vblank_get(dev, crtc);
436 }
437 break;
438 case _DRM_POST_MODESET:
439 if (dev->vblank_inmodeset[crtc]) {
440 DRM_SPINLOCK(&dev->vbl_lock);
441 dev->vblank_disable_allowed = 1;
442 dev->vblank_inmodeset[crtc] = 0;
443 DRM_SPINUNLOCK(&dev->vbl_lock);
444 drm_vblank_put(dev, crtc);
445 }
446 break;
447 default:
448 ret = -EINVAL;
449 break;
450 }
451
452out:
453 return (ret);
454}
455
kz15163460405de2006-09-28 01:41:18 -0700456/*ARGSUSED*/
457int
458drm_wait_vblank(DRM_IOCTL_ARGS)
459{
cg149915d0538f62008-01-09 19:45:15 -0800460 DRM_DEVICE;
461 drm_wait_vblank_t vblwait;
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800462 int ret, flags, crtc;
cg149915d0538f62008-01-09 19:45:15 -0800463 unsigned int sequence;
464
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800465 if (!dev->irq_enabled) {
miao chen - Sun Microsystems - Beijing China0035d212009-12-05 13:25:40 +0800466 DRM_ERROR("wait vblank, EINVAL");
cg149915d0538f62008-01-09 19:45:15 -0800467 return (EINVAL);
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800468 }
cg149915d0538f62008-01-09 19:45:15 -0800469#ifdef _MULTI_DATAMODEL
470 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
471 drm_wait_vblank_32_t vblwait32;
472 DRM_COPYFROM_WITH_RETURN(&vblwait32, (void *)data,
473 sizeof (vblwait32));
474 vblwait.request.type = vblwait32.request.type;
475 vblwait.request.sequence = vblwait32.request.sequence;
476 vblwait.request.signal = vblwait32.request.signal;
477 } else {
478#endif
479 DRM_COPYFROM_WITH_RETURN(&vblwait, (void *)data,
480 sizeof (vblwait));
481#ifdef _MULTI_DATAMODEL
482 }
483#endif
484
485 if (vblwait.request.type &
486 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
miao chen - Sun Microsystems - Beijing China0035d212009-12-05 13:25:40 +0800487 DRM_ERROR("drm_wait_vblank: wrong request type 0x%x",
cg149915d0538f62008-01-09 19:45:15 -0800488 vblwait.request.type);
489 return (EINVAL);
490 }
491
492 flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800493 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
miao chen - Sun Microsystems - Beijing China0035d212009-12-05 13:25:40 +0800494 if (crtc >= dev->num_crtcs) {
495 DRM_ERROR("wait vblank operation not support");
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800496 return (ENOTSUP);
miao chen - Sun Microsystems - Beijing China0035d212009-12-05 13:25:40 +0800497 }
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800498 ret = drm_vblank_get(dev, crtc);
499 if (ret) {
miao chen - Sun Microsystems - Beijing China0035d212009-12-05 13:25:40 +0800500 DRM_ERROR("can't get drm vblank %d", ret);
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800501 return (ret);
cg149915d0538f62008-01-09 19:45:15 -0800502 }
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800503 sequence = drm_vblank_count(dev, crtc);
cg149915d0538f62008-01-09 19:45:15 -0800504
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800505 switch (vblwait.request.type & _DRM_VBLANK_TYPES_MASK) {
506 case _DRM_VBLANK_RELATIVE:
cg149915d0538f62008-01-09 19:45:15 -0800507 vblwait.request.sequence += sequence;
508 vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800509 /*FALLTHROUGH*/
510 case _DRM_VBLANK_ABSOLUTE:
511 break;
512 default:
513 DRM_DEBUG("wait vblank return EINVAL");
cg149915d0538f62008-01-09 19:45:15 -0800514 return (EINVAL);
515 }
cg149915d0538f62008-01-09 19:45:15 -0800516
517 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
518 (sequence - vblwait.request.sequence) <= (1<<23)) {
519 vblwait.request.sequence = sequence + 1;
520 }
521
522 if (flags & _DRM_VBLANK_SIGNAL) {
523 /*
524 * Don't block process, send signal when vblank interrupt
525 */
miao chen - Sun Microsystems - Beijing China0035d212009-12-05 13:25:40 +0800526 DRM_ERROR("NOT SUPPORT YET, SHOULD BE ADDED");
cg149915d0538f62008-01-09 19:45:15 -0800527 cmn_err(CE_WARN, "NOT SUPPORT YET, SHOULD BE ADDED");
528 ret = EINVAL;
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800529 goto done;
cg149915d0538f62008-01-09 19:45:15 -0800530 } else {
531 /* block until vblank interupt */
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800532 /* shared code returns -errno */
533 DRM_WAIT_ON(ret, &dev->vbl_queues[crtc], 3 * DRM_HZ,
miao chen - Sun Microsystems - Beijing China0035d212009-12-05 13:25:40 +0800534 (((drm_vblank_count(dev, crtc)
535 - vblwait.request.sequence) <= (1 << 23)) ||
536 !dev->irq_enabled));
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800537 if (ret != EINTR) {
538 struct timeval now;
539 (void) uniqtime(&now);
540 vblwait.reply.tval_sec = now.tv_sec;
541 vblwait.reply.tval_usec = now.tv_usec;
542 vblwait.reply.sequence = drm_vblank_count(dev, crtc);
cg149915d0538f62008-01-09 19:45:15 -0800543 }
cg149915d0538f62008-01-09 19:45:15 -0800544 }
545
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800546done:
cg149915d0538f62008-01-09 19:45:15 -0800547#ifdef _MULTI_DATAMODEL
548 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
549 drm_wait_vblank_32_t vblwait32;
550 vblwait32.reply.type = vblwait.reply.type;
551 vblwait32.reply.sequence = vblwait.reply.sequence;
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800552 vblwait32.reply.tval_sec = (int32_t)vblwait.reply.tval_sec;
553 vblwait32.reply.tval_usec = (int32_t)vblwait.reply.tval_usec;
cg149915d0538f62008-01-09 19:45:15 -0800554 DRM_COPYTO_WITH_RETURN((void *)data, &vblwait32,
555 sizeof (vblwait32));
556 } else {
557#endif
558 DRM_COPYTO_WITH_RETURN((void *)data, &vblwait,
559 sizeof (vblwait));
560#ifdef _MULTI_DATAMODEL
561 }
562#endif
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800563
564 drm_vblank_put(dev, crtc);
cg149915d0538f62008-01-09 19:45:15 -0800565 return (ret);
kz15163460405de2006-09-28 01:41:18 -0700566}
zw161486e92e3a82007-05-09 19:58:49 -0700567
cg149915d0538f62008-01-09 19:45:15 -0800568
zw161486e92e3a82007-05-09 19:58:49 -0700569/*ARGSUSED*/
570void
571drm_vbl_send_signals(drm_device_t *dev)
572{
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800573 DRM_DEBUG("drm_vbl_send_signals");
574}
cg149915d0538f62008-01-09 19:45:15 -0800575
miao chen - Sun Microsystems - Beijing Chinad0231072009-02-17 16:22:10 +0800576void
577drm_handle_vblank(struct drm_device *dev, int crtc)
578{
579 atomic_inc(&dev->_vblank_count[crtc]);
580 DRM_WAKEUP(&dev->vbl_queues[crtc]);
zw161486e92e3a82007-05-09 19:58:49 -0700581}