aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/drm_irq.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpu/drm/drm_irq.c')
-rw-r--r--drivers/gpu/drm/drm_irq.c464
1 files changed, 387 insertions, 77 deletions
diff --git a/drivers/gpu/drm/drm_irq.c b/drivers/gpu/drm/drm_irq.c
index 53f0e5af1cc8..4091b9e291f9 100644
--- a/drivers/gpu/drm/drm_irq.c
+++ b/drivers/gpu/drm/drm_irq.c
@@ -63,7 +63,7 @@ int drm_irq_by_busid(struct drm_device *dev, void *data,
63 p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn)) 63 p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
64 return -EINVAL; 64 return -EINVAL;
65 65
66 p->irq = dev->irq; 66 p->irq = dev->pdev->irq;
67 67
68 DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum, 68 DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
69 p->irq); 69 p->irq);
@@ -71,25 +71,137 @@ int drm_irq_by_busid(struct drm_device *dev, void *data,
71 return 0; 71 return 0;
72} 72}
73 73
74static void vblank_disable_fn(unsigned long arg)
75{
76 struct drm_device *dev = (struct drm_device *)arg;
77 unsigned long irqflags;
78 int i;
79
80 if (!dev->vblank_disable_allowed)
81 return;
82
83 for (i = 0; i < dev->num_crtcs; i++) {
84 spin_lock_irqsave(&dev->vbl_lock, irqflags);
85 if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
86 dev->vblank_enabled[i]) {
87 DRM_DEBUG("disabling vblank on crtc %d\n", i);
88 dev->last_vblank[i] =
89 dev->driver->get_vblank_counter(dev, i);
90 dev->driver->disable_vblank(dev, i);
91 dev->vblank_enabled[i] = 0;
92 }
93 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
94 }
95}
96
97static void drm_vblank_cleanup(struct drm_device *dev)
98{
99 /* Bail if the driver didn't call drm_vblank_init() */
100 if (dev->num_crtcs == 0)
101 return;
102
103 del_timer(&dev->vblank_disable_timer);
104
105 vblank_disable_fn((unsigned long)dev);
106
107 drm_free(dev->vbl_queue, sizeof(*dev->vbl_queue) * dev->num_crtcs,
108 DRM_MEM_DRIVER);
109 drm_free(dev->vbl_sigs, sizeof(*dev->vbl_sigs) * dev->num_crtcs,
110 DRM_MEM_DRIVER);
111 drm_free(dev->_vblank_count, sizeof(*dev->_vblank_count) *
112 dev->num_crtcs, DRM_MEM_DRIVER);
113 drm_free(dev->vblank_refcount, sizeof(*dev->vblank_refcount) *
114 dev->num_crtcs, DRM_MEM_DRIVER);
115 drm_free(dev->vblank_enabled, sizeof(*dev->vblank_enabled) *
116 dev->num_crtcs, DRM_MEM_DRIVER);
117 drm_free(dev->last_vblank, sizeof(*dev->last_vblank) * dev->num_crtcs,
118 DRM_MEM_DRIVER);
119 drm_free(dev->vblank_inmodeset, sizeof(*dev->vblank_inmodeset) *
120 dev->num_crtcs, DRM_MEM_DRIVER);
121
122 dev->num_crtcs = 0;
123}
124
125int drm_vblank_init(struct drm_device *dev, int num_crtcs)
126{
127 int i, ret = -ENOMEM;
128
129 setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
130 (unsigned long)dev);
131 spin_lock_init(&dev->vbl_lock);
132 atomic_set(&dev->vbl_signal_pending, 0);
133 dev->num_crtcs = num_crtcs;
134
135 dev->vbl_queue = drm_alloc(sizeof(wait_queue_head_t) * num_crtcs,
136 DRM_MEM_DRIVER);
137 if (!dev->vbl_queue)
138 goto err;
139
140 dev->vbl_sigs = drm_alloc(sizeof(struct list_head) * num_crtcs,
141 DRM_MEM_DRIVER);
142 if (!dev->vbl_sigs)
143 goto err;
144
145 dev->_vblank_count = drm_alloc(sizeof(atomic_t) * num_crtcs,
146 DRM_MEM_DRIVER);
147 if (!dev->_vblank_count)
148 goto err;
149
150 dev->vblank_refcount = drm_alloc(sizeof(atomic_t) * num_crtcs,
151 DRM_MEM_DRIVER);
152 if (!dev->vblank_refcount)
153 goto err;
154
155 dev->vblank_enabled = drm_calloc(num_crtcs, sizeof(int),
156 DRM_MEM_DRIVER);
157 if (!dev->vblank_enabled)
158 goto err;
159
160 dev->last_vblank = drm_calloc(num_crtcs, sizeof(u32), DRM_MEM_DRIVER);
161 if (!dev->last_vblank)
162 goto err;
163
164 dev->vblank_inmodeset = drm_calloc(num_crtcs, sizeof(int),
165 DRM_MEM_DRIVER);
166 if (!dev->vblank_inmodeset)
167 goto err;
168
169 /* Zero per-crtc vblank stuff */
170 for (i = 0; i < num_crtcs; i++) {
171 init_waitqueue_head(&dev->vbl_queue[i]);
172 INIT_LIST_HEAD(&dev->vbl_sigs[i]);
173 atomic_set(&dev->_vblank_count[i], 0);
174 atomic_set(&dev->vblank_refcount[i], 0);
175 }
176
177 dev->vblank_disable_allowed = 0;
178
179 return 0;
180
181err:
182 drm_vblank_cleanup(dev);
183 return ret;
184}
185EXPORT_SYMBOL(drm_vblank_init);
186
74/** 187/**
75 * Install IRQ handler. 188 * Install IRQ handler.
76 * 189 *
77 * \param dev DRM device. 190 * \param dev DRM device.
78 * \param irq IRQ number.
79 * 191 *
80 * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver 192 * Initializes the IRQ related data. Installs the handler, calling the driver
81 * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions 193 * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
82 * before and after the installation. 194 * before and after the installation.
83 */ 195 */
84static int drm_irq_install(struct drm_device * dev) 196int drm_irq_install(struct drm_device *dev)
85{ 197{
86 int ret; 198 int ret = 0;
87 unsigned long sh_flags = 0; 199 unsigned long sh_flags = 0;
88 200
89 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) 201 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
90 return -EINVAL; 202 return -EINVAL;
91 203
92 if (dev->irq == 0) 204 if (dev->pdev->irq == 0)
93 return -EINVAL; 205 return -EINVAL;
94 206
95 mutex_lock(&dev->struct_mutex); 207 mutex_lock(&dev->struct_mutex);
@@ -107,18 +219,7 @@ static int drm_irq_install(struct drm_device * dev)
107 dev->irq_enabled = 1; 219 dev->irq_enabled = 1;
108 mutex_unlock(&dev->struct_mutex); 220 mutex_unlock(&dev->struct_mutex);
109 221
110 DRM_DEBUG("irq=%d\n", dev->irq); 222 DRM_DEBUG("irq=%d\n", dev->pdev->irq);
111
112 if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
113 init_waitqueue_head(&dev->vbl_queue);
114
115 spin_lock_init(&dev->vbl_lock);
116
117 INIT_LIST_HEAD(&dev->vbl_sigs);
118 INIT_LIST_HEAD(&dev->vbl_sigs2);
119
120 dev->vbl_pending = 0;
121 }
122 223
123 /* Before installing handler */ 224 /* Before installing handler */
124 dev->driver->irq_preinstall(dev); 225 dev->driver->irq_preinstall(dev);
@@ -127,8 +228,9 @@ static int drm_irq_install(struct drm_device * dev)
127 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED)) 228 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
128 sh_flags = IRQF_SHARED; 229 sh_flags = IRQF_SHARED;
129 230
130 ret = request_irq(dev->irq, dev->driver->irq_handler, 231 ret = request_irq(drm_dev_to_irq(dev), dev->driver->irq_handler,
131 sh_flags, dev->devname, dev); 232 sh_flags, dev->devname, dev);
233
132 if (ret < 0) { 234 if (ret < 0) {
133 mutex_lock(&dev->struct_mutex); 235 mutex_lock(&dev->struct_mutex);
134 dev->irq_enabled = 0; 236 dev->irq_enabled = 0;
@@ -137,10 +239,16 @@ static int drm_irq_install(struct drm_device * dev)
137 } 239 }
138 240
139 /* After installing handler */ 241 /* After installing handler */
140 dev->driver->irq_postinstall(dev); 242 ret = dev->driver->irq_postinstall(dev);
243 if (ret < 0) {
244 mutex_lock(&dev->struct_mutex);
245 dev->irq_enabled = 0;
246 mutex_unlock(&dev->struct_mutex);
247 }
141 248
142 return 0; 249 return ret;
143} 250}
251EXPORT_SYMBOL(drm_irq_install);
144 252
145/** 253/**
146 * Uninstall the IRQ handler. 254 * Uninstall the IRQ handler.
@@ -164,17 +272,18 @@ int drm_irq_uninstall(struct drm_device * dev)
164 if (!irq_enabled) 272 if (!irq_enabled)
165 return -EINVAL; 273 return -EINVAL;
166 274
167 DRM_DEBUG("irq=%d\n", dev->irq); 275 DRM_DEBUG("irq=%d\n", dev->pdev->irq);
168 276
169 dev->driver->irq_uninstall(dev); 277 dev->driver->irq_uninstall(dev);
170 278
171 free_irq(dev->irq, dev); 279 free_irq(dev->pdev->irq, dev);
280
281 drm_vblank_cleanup(dev);
172 282
173 dev->locked_tasklet_func = NULL; 283 dev->locked_tasklet_func = NULL;
174 284
175 return 0; 285 return 0;
176} 286}
177
178EXPORT_SYMBOL(drm_irq_uninstall); 287EXPORT_SYMBOL(drm_irq_uninstall);
179 288
180/** 289/**
@@ -201,7 +310,7 @@ int drm_control(struct drm_device *dev, void *data,
201 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) 310 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
202 return 0; 311 return 0;
203 if (dev->if_version < DRM_IF_VERSION(1, 2) && 312 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
204 ctl->irq != dev->irq) 313 ctl->irq != dev->pdev->irq)
205 return -EINVAL; 314 return -EINVAL;
206 return drm_irq_install(dev); 315 return drm_irq_install(dev);
207 case DRM_UNINST_HANDLER: 316 case DRM_UNINST_HANDLER:
@@ -214,6 +323,174 @@ int drm_control(struct drm_device *dev, void *data,
214} 323}
215 324
216/** 325/**
326 * drm_vblank_count - retrieve "cooked" vblank counter value
327 * @dev: DRM device
328 * @crtc: which counter to retrieve
329 *
330 * Fetches the "cooked" vblank count value that represents the number of
331 * vblank events since the system was booted, including lost events due to
332 * modesetting activity.
333 */
334u32 drm_vblank_count(struct drm_device *dev, int crtc)
335{
336 return atomic_read(&dev->_vblank_count[crtc]);
337}
338EXPORT_SYMBOL(drm_vblank_count);
339
340/**
341 * drm_update_vblank_count - update the master vblank counter
342 * @dev: DRM device
343 * @crtc: counter to update
344 *
345 * Call back into the driver to update the appropriate vblank counter
346 * (specified by @crtc). Deal with wraparound, if it occurred, and
347 * update the last read value so we can deal with wraparound on the next
348 * call if necessary.
349 *
350 * Only necessary when going from off->on, to account for frames we
351 * didn't get an interrupt for.
352 *
353 * Note: caller must hold dev->vbl_lock since this reads & writes
354 * device vblank fields.
355 */
356static void drm_update_vblank_count(struct drm_device *dev, int crtc)
357{
358 u32 cur_vblank, diff;
359
360 /*
361 * Interrupts were disabled prior to this call, so deal with counter
362 * wrap if needed.
363 * NOTE! It's possible we lost a full dev->max_vblank_count events
364 * here if the register is small or we had vblank interrupts off for
365 * a long time.
366 */
367 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
368 diff = cur_vblank - dev->last_vblank[crtc];
369 if (cur_vblank < dev->last_vblank[crtc]) {
370 diff += dev->max_vblank_count;
371
372 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
373 crtc, dev->last_vblank[crtc], cur_vblank, diff);
374 }
375
376 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
377 crtc, diff);
378
379 atomic_add(diff, &dev->_vblank_count[crtc]);
380}
381
382/**
383 * drm_vblank_get - get a reference count on vblank events
384 * @dev: DRM device
385 * @crtc: which CRTC to own
386 *
387 * Acquire a reference count on vblank events to avoid having them disabled
388 * while in use.
389 *
390 * RETURNS
391 * Zero on success, nonzero on failure.
392 */
393int drm_vblank_get(struct drm_device *dev, int crtc)
394{
395 unsigned long irqflags;
396 int ret = 0;
397
398 spin_lock_irqsave(&dev->vbl_lock, irqflags);
399 /* Going from 0->1 means we have to enable interrupts again */
400 if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1 &&
401 !dev->vblank_enabled[crtc]) {
402 ret = dev->driver->enable_vblank(dev, crtc);
403 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
404 if (ret)
405 atomic_dec(&dev->vblank_refcount[crtc]);
406 else {
407 dev->vblank_enabled[crtc] = 1;
408 drm_update_vblank_count(dev, crtc);
409 }
410 }
411 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
412
413 return ret;
414}
415EXPORT_SYMBOL(drm_vblank_get);
416
417/**
418 * drm_vblank_put - give up ownership of vblank events
419 * @dev: DRM device
420 * @crtc: which counter to give up
421 *
422 * Release ownership of a given vblank counter, turning off interrupts
423 * if possible.
424 */
425void drm_vblank_put(struct drm_device *dev, int crtc)
426{
427 /* Last user schedules interrupt disable */
428 if (atomic_dec_and_test(&dev->vblank_refcount[crtc]))
429 mod_timer(&dev->vblank_disable_timer, jiffies + 5*DRM_HZ);
430}
431EXPORT_SYMBOL(drm_vblank_put);
432
433/**
434 * drm_modeset_ctl - handle vblank event counter changes across mode switch
435 * @DRM_IOCTL_ARGS: standard ioctl arguments
436 *
437 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
438 * ioctls around modesetting so that any lost vblank events are accounted for.
439 *
440 * Generally the counter will reset across mode sets. If interrupts are
441 * enabled around this call, we don't have to do anything since the counter
442 * will have already been incremented.
443 */
444int drm_modeset_ctl(struct drm_device *dev, void *data,
445 struct drm_file *file_priv)
446{
447 struct drm_modeset_ctl *modeset = data;
448 unsigned long irqflags;
449 int crtc, ret = 0;
450
451 /* If drm_vblank_init() hasn't been called yet, just no-op */
452 if (!dev->num_crtcs)
453 goto out;
454
455 crtc = modeset->crtc;
456 if (crtc >= dev->num_crtcs) {
457 ret = -EINVAL;
458 goto out;
459 }
460
461 /*
462 * To avoid all the problems that might happen if interrupts
463 * were enabled/disabled around or between these calls, we just
464 * have the kernel take a reference on the CRTC (just once though
465 * to avoid corrupting the count if multiple, mismatch calls occur),
466 * so that interrupts remain enabled in the interim.
467 */
468 switch (modeset->cmd) {
469 case _DRM_PRE_MODESET:
470 if (!dev->vblank_inmodeset[crtc]) {
471 dev->vblank_inmodeset[crtc] = 1;
472 drm_vblank_get(dev, crtc);
473 }
474 break;
475 case _DRM_POST_MODESET:
476 if (dev->vblank_inmodeset[crtc]) {
477 spin_lock_irqsave(&dev->vbl_lock, irqflags);
478 dev->vblank_disable_allowed = 1;
479 dev->vblank_inmodeset[crtc] = 0;
480 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
481 drm_vblank_put(dev, crtc);
482 }
483 break;
484 default:
485 ret = -EINVAL;
486 break;
487 }
488
489out:
490 return ret;
491}
492
493/**
217 * Wait for VBLANK. 494 * Wait for VBLANK.
218 * 495 *
219 * \param inode device inode. 496 * \param inode device inode.
@@ -232,14 +509,14 @@ int drm_control(struct drm_device *dev, void *data,
232 * 509 *
233 * If a signal is not requested, then calls vblank_wait(). 510 * If a signal is not requested, then calls vblank_wait().
234 */ 511 */
235int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_priv) 512int drm_wait_vblank(struct drm_device *dev, void *data,
513 struct drm_file *file_priv)
236{ 514{
237 union drm_wait_vblank *vblwait = data; 515 union drm_wait_vblank *vblwait = data;
238 struct timeval now;
239 int ret = 0; 516 int ret = 0;
240 unsigned int flags, seq; 517 unsigned int flags, seq, crtc;
241 518
242 if ((!dev->irq) || (!dev->irq_enabled)) 519 if ((!dev->pdev->irq) || (!dev->irq_enabled))
243 return -EINVAL; 520 return -EINVAL;
244 521
245 if (vblwait->request.type & 522 if (vblwait->request.type &
@@ -251,13 +528,17 @@ int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_pr
251 } 528 }
252 529
253 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK; 530 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
531 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
254 532
255 if (!drm_core_check_feature(dev, (flags & _DRM_VBLANK_SECONDARY) ? 533 if (crtc >= dev->num_crtcs)
256 DRIVER_IRQ_VBL2 : DRIVER_IRQ_VBL))
257 return -EINVAL; 534 return -EINVAL;
258 535
259 seq = atomic_read((flags & _DRM_VBLANK_SECONDARY) ? &dev->vbl_received2 536 ret = drm_vblank_get(dev, crtc);
260 : &dev->vbl_received); 537 if (ret) {
538 DRM_ERROR("failed to acquire vblank counter, %d\n", ret);
539 return ret;
540 }
541 seq = drm_vblank_count(dev, crtc);
261 542
262 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) { 543 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
263 case _DRM_VBLANK_RELATIVE: 544 case _DRM_VBLANK_RELATIVE:
@@ -266,7 +547,8 @@ int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_pr
266 case _DRM_VBLANK_ABSOLUTE: 547 case _DRM_VBLANK_ABSOLUTE:
267 break; 548 break;
268 default: 549 default:
269 return -EINVAL; 550 ret = -EINVAL;
551 goto done;
270 } 552 }
271 553
272 if ((flags & _DRM_VBLANK_NEXTONMISS) && 554 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
@@ -276,8 +558,7 @@ int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_pr
276 558
277 if (flags & _DRM_VBLANK_SIGNAL) { 559 if (flags & _DRM_VBLANK_SIGNAL) {
278 unsigned long irqflags; 560 unsigned long irqflags;
279 struct list_head *vbl_sigs = (flags & _DRM_VBLANK_SECONDARY) 561 struct list_head *vbl_sigs = &dev->vbl_sigs[crtc];
280 ? &dev->vbl_sigs2 : &dev->vbl_sigs;
281 struct drm_vbl_sig *vbl_sig; 562 struct drm_vbl_sig *vbl_sig;
282 563
283 spin_lock_irqsave(&dev->vbl_lock, irqflags); 564 spin_lock_irqsave(&dev->vbl_lock, irqflags);
@@ -298,22 +579,29 @@ int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_pr
298 } 579 }
299 } 580 }
300 581
301 if (dev->vbl_pending >= 100) { 582 if (atomic_read(&dev->vbl_signal_pending) >= 100) {
302 spin_unlock_irqrestore(&dev->vbl_lock, irqflags); 583 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
303 return -EBUSY; 584 ret = -EBUSY;
585 goto done;
304 } 586 }
305 587
306 dev->vbl_pending++;
307
308 spin_unlock_irqrestore(&dev->vbl_lock, irqflags); 588 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
309 589
310 if (! 590 vbl_sig = drm_calloc(1, sizeof(struct drm_vbl_sig),
311 (vbl_sig = 591 DRM_MEM_DRIVER);
312 drm_alloc(sizeof(struct drm_vbl_sig), DRM_MEM_DRIVER))) { 592 if (!vbl_sig) {
313 return -ENOMEM; 593 ret = -ENOMEM;
594 goto done;
595 }
596
597 ret = drm_vblank_get(dev, crtc);
598 if (ret) {
599 drm_free(vbl_sig, sizeof(struct drm_vbl_sig),
600 DRM_MEM_DRIVER);
601 return ret;
314 } 602 }
315 603
316 memset((void *)vbl_sig, 0, sizeof(*vbl_sig)); 604 atomic_inc(&dev->vbl_signal_pending);
317 605
318 vbl_sig->sequence = vblwait->request.sequence; 606 vbl_sig->sequence = vblwait->request.sequence;
319 vbl_sig->info.si_signo = vblwait->request.signal; 607 vbl_sig->info.si_signo = vblwait->request.signal;
@@ -327,20 +615,29 @@ int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_pr
327 615
328 vblwait->reply.sequence = seq; 616 vblwait->reply.sequence = seq;
329 } else { 617 } else {
330 if (flags & _DRM_VBLANK_SECONDARY) { 618 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
331 if (dev->driver->vblank_wait2) 619 vblwait->request.sequence, crtc);
332 ret = dev->driver->vblank_wait2(dev, &vblwait->request.sequence); 620 DRM_WAIT_ON(ret, dev->vbl_queue[crtc], 3 * DRM_HZ,
333 } else if (dev->driver->vblank_wait) 621 ((drm_vblank_count(dev, crtc)
334 ret = 622 - vblwait->request.sequence) <= (1 << 23)));
335 dev->driver->vblank_wait(dev, 623
336 &vblwait->request.sequence); 624 if (ret != -EINTR) {
337 625 struct timeval now;
338 do_gettimeofday(&now); 626
339 vblwait->reply.tval_sec = now.tv_sec; 627 do_gettimeofday(&now);
340 vblwait->reply.tval_usec = now.tv_usec; 628
629 vblwait->reply.tval_sec = now.tv_sec;
630 vblwait->reply.tval_usec = now.tv_usec;
631 vblwait->reply.sequence = drm_vblank_count(dev, crtc);
632 DRM_DEBUG("returning %d to client\n",
633 vblwait->reply.sequence);
634 } else {
635 DRM_DEBUG("vblank wait interrupted by signal\n");
636 }
341 } 637 }
342 638
343 done: 639done:
640 drm_vblank_put(dev, crtc);
344 return ret; 641 return ret;
345} 642}
346 643
@@ -348,44 +645,57 @@ int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_pr
348 * Send the VBLANK signals. 645 * Send the VBLANK signals.
349 * 646 *
350 * \param dev DRM device. 647 * \param dev DRM device.
648 * \param crtc CRTC where the vblank event occurred
351 * 649 *
352 * Sends a signal for each task in drm_device::vbl_sigs and empties the list. 650 * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
353 * 651 *
354 * If a signal is not requested, then calls vblank_wait(). 652 * If a signal is not requested, then calls vblank_wait().
355 */ 653 */
356void drm_vbl_send_signals(struct drm_device * dev) 654static void drm_vbl_send_signals(struct drm_device *dev, int crtc)
357{ 655{
656 struct drm_vbl_sig *vbl_sig, *tmp;
657 struct list_head *vbl_sigs;
658 unsigned int vbl_seq;
358 unsigned long flags; 659 unsigned long flags;
359 int i;
360 660
361 spin_lock_irqsave(&dev->vbl_lock, flags); 661 spin_lock_irqsave(&dev->vbl_lock, flags);
362 662
363 for (i = 0; i < 2; i++) { 663 vbl_sigs = &dev->vbl_sigs[crtc];
364 struct drm_vbl_sig *vbl_sig, *tmp; 664 vbl_seq = drm_vblank_count(dev, crtc);
365 struct list_head *vbl_sigs = i ? &dev->vbl_sigs2 : &dev->vbl_sigs;
366 unsigned int vbl_seq = atomic_read(i ? &dev->vbl_received2 :
367 &dev->vbl_received);
368 665
369 list_for_each_entry_safe(vbl_sig, tmp, vbl_sigs, head) { 666 list_for_each_entry_safe(vbl_sig, tmp, vbl_sigs, head) {
370 if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) { 667 if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
371 vbl_sig->info.si_code = vbl_seq; 668 vbl_sig->info.si_code = vbl_seq;
372 send_sig_info(vbl_sig->info.si_signo, 669 send_sig_info(vbl_sig->info.si_signo,
373 &vbl_sig->info, vbl_sig->task); 670 &vbl_sig->info, vbl_sig->task);
374 671
375 list_del(&vbl_sig->head); 672 list_del(&vbl_sig->head);
376
377 drm_free(vbl_sig, sizeof(*vbl_sig),
378 DRM_MEM_DRIVER);
379 673
380 dev->vbl_pending--; 674 drm_free(vbl_sig, sizeof(*vbl_sig),
381 } 675 DRM_MEM_DRIVER);
382 } 676 atomic_dec(&dev->vbl_signal_pending);
677 drm_vblank_put(dev, crtc);
678 }
383 } 679 }
384 680
385 spin_unlock_irqrestore(&dev->vbl_lock, flags); 681 spin_unlock_irqrestore(&dev->vbl_lock, flags);
386} 682}
387 683
388EXPORT_SYMBOL(drm_vbl_send_signals); 684/**
685 * drm_handle_vblank - handle a vblank event
686 * @dev: DRM device
687 * @crtc: where this event occurred
688 *
689 * Drivers should call this routine in their vblank interrupt handlers to
690 * update the vblank counter and send any signals that may be pending.
691 */
692void drm_handle_vblank(struct drm_device *dev, int crtc)
693{
694 atomic_inc(&dev->_vblank_count[crtc]);
695 DRM_WAKEUP(&dev->vbl_queue[crtc]);
696 drm_vbl_send_signals(dev, crtc);
697}
698EXPORT_SYMBOL(drm_handle_vblank);
389 699
390/** 700/**
391 * Tasklet wrapper function. 701 * Tasklet wrapper function.