xref: /titanic_44/usr/src/uts/common/io/drm/drm_irq.c (revision 31979bfa048dcd2ef7f20d5f6afd58702333b725)
1 /*
2  * 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
7  * All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Authors:
28  *    Eric Anholt <anholt@FreeBSD.org>
29  *
30  */
31 
32 /*
33  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
34  * Use is subject to license terms.
35  */
36 
37 #include "drmP.h"
38 #include "drm.h"
39 #include "drm_io32.h"
40 
41 /*ARGSUSED*/
42 int
43 drm_irq_by_busid(DRM_IOCTL_ARGS)
44 {
45 	DRM_DEVICE;
46 	drm_irq_busid_t irq;
47 
48 	DRM_COPYFROM_WITH_RETURN(&irq, (void *)data, sizeof (irq));
49 
50 	if ((irq.busnum >> 8) != dev->pci_domain ||
51 	    (irq.busnum & 0xff) != dev->pci_bus ||
52 	    irq.devnum != dev->pci_slot ||
53 	    irq.funcnum != dev->pci_func)
54 		return (EINVAL);
55 
56 	irq.irq = dev->irq;
57 
58 	DRM_DEBUG("%d:%d:%d => IRQ %d\n",
59 	    irq.busnum, irq.devnum, irq.funcnum, irq.irq);
60 
61 	DRM_COPYTO_WITH_RETURN((void *)data, &irq, sizeof (irq));
62 
63 	return (0);
64 }
65 
66 
67 static irqreturn_t
68 drm_irq_handler_wrap(DRM_IRQ_ARGS)
69 {
70 	drm_device_t *dev = (void *)arg;
71 	int	ret;
72 
73 	mutex_enter(&dev->irq_lock);
74 	ret = dev->driver->irq_handler(arg);
75 	mutex_exit(&dev->irq_lock);
76 
77 	return (ret);
78 }
79 
80 static void vblank_disable_fn(void *arg)
81 {
82 	struct drm_device *dev = (struct drm_device *)arg;
83 	int i;
84 
85 	for (i = 0; i < dev->num_crtcs; i++) {
86 		if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
87 		    atomic_read(&dev->vblank_enabled[i]) == 1) {
88 			dev->last_vblank[i] =
89 			    dev->driver->get_vblank_counter(dev, i);
90 			dev->driver->disable_vblank(dev, i);
91 			atomic_set(&dev->vblank_enabled[i], 0);
92 			DRM_DEBUG("disable vblank");
93 		}
94 	}
95 }
96 
97 static 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 
104 	vblank_disable_fn((void *)dev);
105 
106 	drm_free(dev->vbl_queues, sizeof (wait_queue_head_t) * dev->num_crtcs,
107 	    DRM_MEM_DRIVER);
108 	drm_free(dev->vbl_sigs, sizeof (struct drm_vbl_sig) * dev->num_crtcs,
109 	    DRM_MEM_DRIVER);
110 	drm_free(dev->_vblank_count, sizeof (atomic_t) *
111 	    dev->num_crtcs, DRM_MEM_DRIVER);
112 	drm_free(dev->vblank_refcount, sizeof (atomic_t) *
113 	    dev->num_crtcs, DRM_MEM_DRIVER);
114 	drm_free(dev->vblank_enabled, sizeof (int) *
115 	    dev->num_crtcs, DRM_MEM_DRIVER);
116 	drm_free(dev->last_vblank, sizeof (u32) * dev->num_crtcs,
117 	    DRM_MEM_DRIVER);
118 	dev->num_crtcs = 0;
119 }
120 
121 int
122 drm_vblank_init(struct drm_device *dev, int num_crtcs)
123 {
124 	int i, ret = ENOMEM;
125 
126 	atomic_set(&dev->vbl_signal_pending, 0);
127 	dev->num_crtcs = num_crtcs;
128 
129 
130 	dev->vbl_queues = drm_alloc(sizeof (wait_queue_head_t) * num_crtcs,
131 	    DRM_MEM_DRIVER);
132 	if (!dev->vbl_queues)
133 		goto err;
134 
135 	dev->vbl_sigs = drm_alloc(sizeof (struct drm_vbl_sig) * num_crtcs,
136 	    DRM_MEM_DRIVER);
137 	if (!dev->vbl_sigs)
138 		goto err;
139 
140 	dev->_vblank_count = drm_alloc(sizeof (atomic_t) * num_crtcs,
141 	    DRM_MEM_DRIVER);
142 	if (!dev->_vblank_count)
143 		goto err;
144 
145 	dev->vblank_refcount = drm_alloc(sizeof (atomic_t) * num_crtcs,
146 	    DRM_MEM_DRIVER);
147 	if (!dev->vblank_refcount)
148 		goto err;
149 
150 	dev->vblank_enabled = drm_alloc(num_crtcs * sizeof (int),
151 	    DRM_MEM_DRIVER);
152 	if (!dev->vblank_enabled)
153 		goto err;
154 
155 	dev->last_vblank = drm_alloc(num_crtcs * sizeof (u32), DRM_MEM_DRIVER);
156 	if (!dev->last_vblank)
157 		goto err;
158 	/* Zero per-crtc vblank stuff */
159 	for (i = 0; i < num_crtcs; i++) {
160 		DRM_INIT_WAITQUEUE(&dev->vbl_queues[i], DRM_INTR_PRI(dev));
161 		TAILQ_INIT(&dev->vbl_sigs[i]);
162 		atomic_set(&dev->_vblank_count[i], 0);
163 		atomic_set(&dev->vblank_refcount[i], 0);
164 		atomic_set(&dev->vblank_enabled[i], 1);
165 	}
166 	vblank_disable_fn((void *)dev);
167 	return (0);
168 
169 err:
170 	DRM_ERROR("drm_vblank_init: alloc error");
171 	drm_vblank_cleanup(dev);
172 	return (ret);
173 }
174 
175 /*ARGSUSED*/
176 static int
177 drm_install_irq_handle(drm_device_t *dev)
178 {
179 	dev_info_t *dip = dev->dip;
180 
181 	if (dip == NULL) {
182 		DRM_ERROR("drm_install_irq_handle: cannot get vgatext's dip");
183 		return (DDI_FAILURE);
184 	}
185 
186 	if (ddi_intr_hilevel(dip, 0) != 0) {
187 		DRM_ERROR("drm_install_irq_handle: "
188 		    "high-level interrupts are not supported");
189 		return (DDI_FAILURE);
190 	}
191 
192 	if (ddi_get_iblock_cookie(dip, (uint_t)0,
193 	    &dev->intr_block) != DDI_SUCCESS) {
194 		DRM_ERROR("drm_install_irq_handle: cannot get iblock cookie");
195 		return (DDI_FAILURE);
196 	}
197 
198 	/* setup the interrupt handler */
199 	if (ddi_add_intr(dip, 0, &dev->intr_block,
200 	    (ddi_idevice_cookie_t *)NULL, drm_irq_handler_wrap,
201 	    (caddr_t)dev) != DDI_SUCCESS) {
202 		DRM_ERROR("drm_install_irq_handle: ddi_add_intr failed");
203 		return (DDI_FAILURE);
204 	}
205 
206 	return (DDI_SUCCESS);
207 }
208 
209 /*ARGSUSED*/
210 int
211 drm_irq_install(drm_device_t *dev)
212 {
213 	int ret;
214 
215 	if (dev->dev_private == NULL) {
216 		DRM_ERROR("drm_irq_install: dev_private is NULL");
217 		return (EINVAL);
218 	}
219 
220 	if (dev->irq_enabled) {
221 		DRM_ERROR("drm_irq_install: irq already enabled");
222 		return (EBUSY);
223 	}
224 
225 	DRM_DEBUG("drm_irq_install irq=%d\n", dev->irq);
226 
227 	/* before installing handler */
228 	ret = dev->driver->irq_preinstall(dev);
229 	if (ret)
230 		return (EINVAL);
231 
232 	/* install handler */
233 	ret  = drm_install_irq_handle(dev);
234 	if (ret != DDI_SUCCESS) {
235 		DRM_ERROR("drm_irq_install: drm_install_irq_handle failed");
236 		return (ret);
237 	}
238 
239 	/* after installing handler */
240 	dev->driver->irq_postinstall(dev);
241 
242 	dev->irq_enabled = 1;
243 	dev->context_flag = 0;
244 
245 	return (0);
246 }
247 
248 static void
249 drm_uninstall_irq_handle(drm_device_t *dev)
250 {
251 	ASSERT(dev->dip);
252 	ddi_remove_intr(dev->dip, 0, dev->intr_block);
253 }
254 
255 
256 /*ARGSUSED*/
257 int
258 drm_irq_uninstall(drm_device_t *dev)
259 {
260 
261 	if (!dev->irq_enabled) {
262 		return (EINVAL);
263 	}
264 	dev->irq_enabled = 0;
265 	dev->driver->irq_uninstall(dev);
266 	drm_uninstall_irq_handle(dev);
267 	dev->locked_tasklet_func = NULL;
268 
269 	drm_vblank_cleanup(dev);
270 
271 	return (DDI_SUCCESS);
272 }
273 
274 /*ARGSUSED*/
275 int
276 drm_control(DRM_IOCTL_ARGS)
277 {
278 	DRM_DEVICE;
279 	drm_control_t ctl;
280 	int err;
281 
282 	DRM_COPYFROM_WITH_RETURN(&ctl, (void *)data, sizeof (ctl));
283 
284 	switch (ctl.func) {
285 	case DRM_INST_HANDLER:
286 		/*
287 		 * Handle drivers whose DRM used to require IRQ setup but the
288 		 * no longer does.
289 		 */
290 		return (drm_irq_install(dev));
291 	case DRM_UNINST_HANDLER:
292 		err = drm_irq_uninstall(dev);
293 		return (err);
294 	default:
295 		return (EINVAL);
296 	}
297 }
298 
299 u32
300 drm_vblank_count(struct drm_device *dev, int crtc)
301 {
302 	return (atomic_read(&dev->_vblank_count[crtc]));
303 }
304 
305 static void drm_update_vblank_count(struct drm_device *dev, int crtc)
306 {
307 	u32 cur_vblank, diff;
308 	/*
309 	 * Interrupts were disabled prior to this call, so deal with counter
310 	 * wrap if needed.
311 	 * NOTE!  It's possible we lost a full dev->max_vblank_count events
312 	 * here if the register is small or we had vblank interrupts off for
313 	 * a long time.
314 	 */
315 	cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
316 	diff = cur_vblank - dev->last_vblank[crtc];
317 	if (cur_vblank < dev->last_vblank[crtc]) {
318 		diff += dev->max_vblank_count;
319 	DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
320 	    crtc, dev->last_vblank[crtc], cur_vblank, diff);
321 	}
322 
323 	atomic_add(diff, &dev->_vblank_count[crtc]);
324 }
325 
326 static timeout_id_t timer_id = NULL;
327 
328 int
329 drm_vblank_get(struct drm_device *dev, int crtc)
330 {
331 	int ret = 0;
332 
333 	DRM_SPINLOCK(&dev->vbl_lock);
334 
335 	if (timer_id != NULL) {
336 		(void) untimeout(timer_id);
337 		timer_id = NULL;
338 	}
339 
340 	/* Going from 0->1 means we have to enable interrupts again */
341 	atomic_add(1, &dev->vblank_refcount[crtc]);
342 	if (dev->vblank_refcount[crtc] == 1 &&
343 	    atomic_read(&dev->vblank_enabled[crtc]) == 0) {
344 		ret = dev->driver->enable_vblank(dev, crtc);
345 		if (ret)
346 			atomic_dec(&dev->vblank_refcount[crtc]);
347 		else {
348 			atomic_set(&dev->vblank_enabled[crtc], 1);
349 			drm_update_vblank_count(dev, crtc);
350 		}
351 	}
352 	DRM_SPINUNLOCK(&dev->vbl_lock);
353 
354 	return (ret);
355 }
356 
357 void
358 drm_vblank_put(struct drm_device *dev, int crtc)
359 {
360 	DRM_SPINLOCK(&dev->vbl_lock);
361 	/* Last user schedules interrupt disable */
362 	atomic_dec(&dev->vblank_refcount[crtc]);
363 
364 	if (dev->vblank_refcount[crtc] == 0)
365 		timer_id = timeout(vblank_disable_fn, (void *) dev, 5*DRM_HZ);
366 
367 	DRM_SPINUNLOCK(&dev->vbl_lock);
368 }
369 
370 /*ARGSUSED*/
371 int
372 drm_wait_vblank(DRM_IOCTL_ARGS)
373 {
374 	DRM_DEVICE;
375 	drm_wait_vblank_t vblwait;
376 	int ret, flags, crtc;
377 	unsigned int	sequence;
378 
379 	if (!dev->irq_enabled) {
380 		DRM_DEBUG("wait vblank, EINVAL");
381 		return (EINVAL);
382 	}
383 #ifdef _MULTI_DATAMODEL
384 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
385 		drm_wait_vblank_32_t vblwait32;
386 		DRM_COPYFROM_WITH_RETURN(&vblwait32, (void *)data,
387 		    sizeof (vblwait32));
388 		vblwait.request.type = vblwait32.request.type;
389 		vblwait.request.sequence = vblwait32.request.sequence;
390 		vblwait.request.signal = vblwait32.request.signal;
391 	} else {
392 #endif
393 		DRM_COPYFROM_WITH_RETURN(&vblwait, (void *)data,
394 		    sizeof (vblwait));
395 #ifdef _MULTI_DATAMODEL
396 	}
397 #endif
398 
399 	if (vblwait.request.type &
400 	    ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
401 		cmn_err(CE_WARN, "drm_wait_vblank: wrong request type 0x%x",
402 		    vblwait.request.type);
403 		return (EINVAL);
404 	}
405 
406 	flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
407 	crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
408 	if (crtc >= dev->num_crtcs)
409 		return (ENOTSUP);
410 
411 	ret = drm_vblank_get(dev, crtc);
412 	if (ret) {
413 		DRM_DEBUG("can't get drm vblank");
414 		return (ret);
415 	}
416 	sequence = drm_vblank_count(dev, crtc);
417 
418 	switch (vblwait.request.type & _DRM_VBLANK_TYPES_MASK) {
419 	case _DRM_VBLANK_RELATIVE:
420 		vblwait.request.sequence += sequence;
421 		vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
422 		/*FALLTHROUGH*/
423 	case _DRM_VBLANK_ABSOLUTE:
424 		break;
425 	default:
426 		DRM_DEBUG("wait vblank return EINVAL");
427 		return (EINVAL);
428 	}
429 
430 	if ((flags & _DRM_VBLANK_NEXTONMISS) &&
431 	    (sequence - vblwait.request.sequence) <= (1<<23)) {
432 		vblwait.request.sequence = sequence + 1;
433 	}
434 
435 	if (flags & _DRM_VBLANK_SIGNAL) {
436 		/*
437 		 * Don't block process, send signal when vblank interrupt
438 		 */
439 		DRM_DEBUG("NOT SUPPORT YET, SHOULD BE ADDED");
440 		cmn_err(CE_WARN, "NOT SUPPORT YET, SHOULD BE ADDED");
441 		ret = EINVAL;
442 		goto done;
443 	} else {
444 		/* block until vblank interupt */
445 		/* shared code returns -errno */
446 		DRM_WAIT_ON(ret, &dev->vbl_queues[crtc], 3 * DRM_HZ,
447 		    ((drm_vblank_count(dev, crtc)
448 		    - vblwait.request.sequence) <= (1 << 23)));
449 		if (ret != EINTR) {
450 			struct timeval now;
451 			(void) uniqtime(&now);
452 			vblwait.reply.tval_sec = now.tv_sec;
453 			vblwait.reply.tval_usec = now.tv_usec;
454 			vblwait.reply.sequence = drm_vblank_count(dev, crtc);
455 		}
456 	}
457 
458 done:
459 #ifdef _MULTI_DATAMODEL
460 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
461 		drm_wait_vblank_32_t vblwait32;
462 		vblwait32.reply.type = vblwait.reply.type;
463 		vblwait32.reply.sequence = vblwait.reply.sequence;
464 		vblwait32.reply.tval_sec = (int32_t)vblwait.reply.tval_sec;
465 		vblwait32.reply.tval_usec = (int32_t)vblwait.reply.tval_usec;
466 		DRM_COPYTO_WITH_RETURN((void *)data, &vblwait32,
467 		    sizeof (vblwait32));
468 	} else {
469 #endif
470 		DRM_COPYTO_WITH_RETURN((void *)data, &vblwait,
471 		    sizeof (vblwait));
472 #ifdef _MULTI_DATAMODEL
473 	}
474 #endif
475 
476 	drm_vblank_put(dev, crtc);
477 	return (ret);
478 }
479 
480 
481 /*ARGSUSED*/
482 void
483 drm_vbl_send_signals(drm_device_t *dev)
484 {
485 	DRM_DEBUG("drm_vbl_send_signals");
486 }
487 
488 void
489 drm_handle_vblank(struct drm_device *dev, int crtc)
490 {
491 	atomic_inc(&dev->_vblank_count[crtc]);
492 	DRM_WAKEUP(&dev->vbl_queues[crtc]);
493 	drm_vbl_send_signals(dev);
494 }
495 
496 /*
497  * Schedule a tasklet to call back a driver hook with the HW lock held.
498  *
499  * \param dev DRM device.
500  * \param func Driver callback.
501  *
502  * This is intended for triggering actions that require the HW lock from an
503  * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
504  * completes. Note that the callback may be called from interrupt or process
505  * context, it must not make any assumptions about this. Also, the HW lock will
506  * be held with the kernel context or any client context.
507  */
508 
509 void
510 drm_locked_tasklet(drm_device_t *dev, void (*func)(drm_device_t *))
511 {
512 	mutex_enter(&dev->tasklet_lock);
513 
514 	if (dev->locked_tasklet_func) {
515 		mutex_exit(&dev->tasklet_lock);
516 		return;
517 	}
518 
519 	dev->locked_tasklet_func = func;
520 
521 	mutex_exit(&dev->tasklet_lock);
522 }
523