xref: /titanic_50/usr/src/uts/common/io/drm/drm_irq.c (revision 2ef9abdc6ea9bad985430325b12b90938a8cd18f)
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 	dev->irq_enabled = 1;
225 	DRM_DEBUG("drm_irq_install irq=%d\n", dev->irq);
226 	dev->context_flag = 0;
227 
228 	/* before installing handler */
229 	dev->driver->irq_preinstall(dev);
230 
231 	/* install handler */
232 	ret  = drm_install_irq_handle(dev);
233 	if (ret != DDI_SUCCESS) {
234 		DRM_ERROR("drm_irq_install: drm_install_irq_handle failed");
235 		return (ret);
236 	}
237 
238 	/* after installing handler */
239 	dev->driver->irq_postinstall(dev);
240 
241 	return (0);
242 }
243 
244 static void
245 drm_uninstall_irq_handle(drm_device_t *dev)
246 {
247 	ASSERT(dev->dip);
248 	ddi_remove_intr(dev->dip, 0, dev->intr_block);
249 }
250 
251 
252 /*ARGSUSED*/
253 int
254 drm_irq_uninstall(drm_device_t *dev)
255 {
256 
257 	if (!dev->irq_enabled) {
258 		return (EINVAL);
259 	}
260 	dev->irq_enabled = 0;
261 	dev->driver->irq_uninstall(dev);
262 	drm_uninstall_irq_handle(dev);
263 	dev->locked_tasklet_func = NULL;
264 
265 	drm_vblank_cleanup(dev);
266 
267 	return (DDI_SUCCESS);
268 }
269 
270 /*ARGSUSED*/
271 int
272 drm_control(DRM_IOCTL_ARGS)
273 {
274 	DRM_DEVICE;
275 	drm_control_t ctl;
276 	int err;
277 
278 	DRM_COPYFROM_WITH_RETURN(&ctl, (void *)data, sizeof (ctl));
279 
280 	switch (ctl.func) {
281 	case DRM_INST_HANDLER:
282 		/*
283 		 * Handle drivers whose DRM used to require IRQ setup but the
284 		 * no longer does.
285 		 */
286 		return (drm_irq_install(dev));
287 	case DRM_UNINST_HANDLER:
288 		err = drm_irq_uninstall(dev);
289 		return (err);
290 	default:
291 		return (EINVAL);
292 	}
293 }
294 
295 u32
296 drm_vblank_count(struct drm_device *dev, int crtc)
297 {
298 	return (atomic_read(&dev->_vblank_count[crtc]));
299 }
300 
301 static void drm_update_vblank_count(struct drm_device *dev, int crtc)
302 {
303 	u32 cur_vblank, diff;
304 	/*
305 	 * Interrupts were disabled prior to this call, so deal with counter
306 	 * wrap if needed.
307 	 * NOTE!  It's possible we lost a full dev->max_vblank_count events
308 	 * here if the register is small or we had vblank interrupts off for
309 	 * a long time.
310 	 */
311 	cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
312 	diff = cur_vblank - dev->last_vblank[crtc];
313 	if (cur_vblank < dev->last_vblank[crtc]) {
314 		diff += dev->max_vblank_count;
315 	DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
316 	    crtc, dev->last_vblank[crtc], cur_vblank, diff);
317 	}
318 
319 	atomic_add(diff, &dev->_vblank_count[crtc]);
320 }
321 
322 static timeout_id_t timer_id = NULL;
323 
324 int
325 drm_vblank_get(struct drm_device *dev, int crtc)
326 {
327 	int ret = 0;
328 
329 	DRM_SPINLOCK(&dev->vbl_lock);
330 
331 	if (timer_id != NULL) {
332 		(void) untimeout(timer_id);
333 		timer_id = NULL;
334 	}
335 
336 	/* Going from 0->1 means we have to enable interrupts again */
337 	atomic_add(1, &dev->vblank_refcount[crtc]);
338 	if (dev->vblank_refcount[crtc] == 1 &&
339 	    atomic_read(&dev->vblank_enabled[crtc]) == 0) {
340 		ret = dev->driver->enable_vblank(dev, crtc);
341 		if (ret)
342 			atomic_dec(&dev->vblank_refcount[crtc]);
343 		else {
344 			atomic_set(&dev->vblank_enabled[crtc], 1);
345 			drm_update_vblank_count(dev, crtc);
346 		}
347 	}
348 	DRM_SPINUNLOCK(&dev->vbl_lock);
349 
350 	return (ret);
351 }
352 
353 void
354 drm_vblank_put(struct drm_device *dev, int crtc)
355 {
356 	DRM_SPINLOCK(&dev->vbl_lock);
357 	/* Last user schedules interrupt disable */
358 	atomic_dec(&dev->vblank_refcount[crtc]);
359 
360 	if (dev->vblank_refcount[crtc] == 0)
361 		timer_id = timeout(vblank_disable_fn, (void *) dev, 5*DRM_HZ);
362 
363 	DRM_SPINUNLOCK(&dev->vbl_lock);
364 }
365 
366 /*ARGSUSED*/
367 int
368 drm_wait_vblank(DRM_IOCTL_ARGS)
369 {
370 	DRM_DEVICE;
371 	drm_wait_vblank_t vblwait;
372 	int ret, flags, crtc;
373 	unsigned int	sequence;
374 
375 	if (!dev->irq_enabled) {
376 		DRM_DEBUG("wait vblank, EINVAL");
377 		return (EINVAL);
378 	}
379 #ifdef _MULTI_DATAMODEL
380 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
381 		drm_wait_vblank_32_t vblwait32;
382 		DRM_COPYFROM_WITH_RETURN(&vblwait32, (void *)data,
383 		    sizeof (vblwait32));
384 		vblwait.request.type = vblwait32.request.type;
385 		vblwait.request.sequence = vblwait32.request.sequence;
386 		vblwait.request.signal = vblwait32.request.signal;
387 	} else {
388 #endif
389 		DRM_COPYFROM_WITH_RETURN(&vblwait, (void *)data,
390 		    sizeof (vblwait));
391 #ifdef _MULTI_DATAMODEL
392 	}
393 #endif
394 
395 	if (vblwait.request.type &
396 	    ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
397 		cmn_err(CE_WARN, "drm_wait_vblank: wrong request type 0x%x",
398 		    vblwait.request.type);
399 		return (EINVAL);
400 	}
401 
402 	flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
403 	crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
404 	if (crtc >= dev->num_crtcs)
405 		return (ENOTSUP);
406 
407 	ret = drm_vblank_get(dev, crtc);
408 	if (ret) {
409 		DRM_DEBUG("can't get drm vblank");
410 		return (ret);
411 	}
412 	sequence = drm_vblank_count(dev, crtc);
413 
414 	switch (vblwait.request.type & _DRM_VBLANK_TYPES_MASK) {
415 	case _DRM_VBLANK_RELATIVE:
416 		vblwait.request.sequence += sequence;
417 		vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
418 		/*FALLTHROUGH*/
419 	case _DRM_VBLANK_ABSOLUTE:
420 		break;
421 	default:
422 		DRM_DEBUG("wait vblank return EINVAL");
423 		return (EINVAL);
424 	}
425 
426 	if ((flags & _DRM_VBLANK_NEXTONMISS) &&
427 	    (sequence - vblwait.request.sequence) <= (1<<23)) {
428 		vblwait.request.sequence = sequence + 1;
429 	}
430 
431 	if (flags & _DRM_VBLANK_SIGNAL) {
432 		/*
433 		 * Don't block process, send signal when vblank interrupt
434 		 */
435 		DRM_DEBUG("NOT SUPPORT YET, SHOULD BE ADDED");
436 		cmn_err(CE_WARN, "NOT SUPPORT YET, SHOULD BE ADDED");
437 		ret = EINVAL;
438 		goto done;
439 	} else {
440 		/* block until vblank interupt */
441 		/* shared code returns -errno */
442 		DRM_WAIT_ON(ret, &dev->vbl_queues[crtc], 3 * DRM_HZ,
443 		    ((drm_vblank_count(dev, crtc)
444 		    - vblwait.request.sequence) <= (1 << 23)));
445 		if (ret != EINTR) {
446 			struct timeval now;
447 			(void) uniqtime(&now);
448 			vblwait.reply.tval_sec = now.tv_sec;
449 			vblwait.reply.tval_usec = now.tv_usec;
450 			vblwait.reply.sequence = drm_vblank_count(dev, crtc);
451 		}
452 	}
453 
454 done:
455 #ifdef _MULTI_DATAMODEL
456 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
457 		drm_wait_vblank_32_t vblwait32;
458 		vblwait32.reply.type = vblwait.reply.type;
459 		vblwait32.reply.sequence = vblwait.reply.sequence;
460 		vblwait32.reply.tval_sec = (int32_t)vblwait.reply.tval_sec;
461 		vblwait32.reply.tval_usec = (int32_t)vblwait.reply.tval_usec;
462 		DRM_COPYTO_WITH_RETURN((void *)data, &vblwait32,
463 		    sizeof (vblwait32));
464 	} else {
465 #endif
466 		DRM_COPYTO_WITH_RETURN((void *)data, &vblwait,
467 		    sizeof (vblwait));
468 #ifdef _MULTI_DATAMODEL
469 	}
470 #endif
471 
472 	drm_vblank_put(dev, crtc);
473 	return (ret);
474 }
475 
476 
477 /*ARGSUSED*/
478 void
479 drm_vbl_send_signals(drm_device_t *dev)
480 {
481 	DRM_DEBUG("drm_vbl_send_signals");
482 }
483 
484 void
485 drm_handle_vblank(struct drm_device *dev, int crtc)
486 {
487 	atomic_inc(&dev->_vblank_count[crtc]);
488 	DRM_WAKEUP(&dev->vbl_queues[crtc]);
489 	drm_vbl_send_signals(dev);
490 }
491 
492 /*
493  * Schedule a tasklet to call back a driver hook with the HW lock held.
494  *
495  * \param dev DRM device.
496  * \param func Driver callback.
497  *
498  * This is intended for triggering actions that require the HW lock from an
499  * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
500  * completes. Note that the callback may be called from interrupt or process
501  * context, it must not make any assumptions about this. Also, the HW lock will
502  * be held with the kernel context or any client context.
503  */
504 
505 void
506 drm_locked_tasklet(drm_device_t *dev, void (*func)(drm_device_t *))
507 {
508 	mutex_enter(&dev->tasklet_lock);
509 
510 	if (dev->locked_tasklet_func) {
511 		mutex_exit(&dev->tasklet_lock);
512 		return;
513 	}
514 
515 	dev->locked_tasklet_func = func;
516 
517 	mutex_exit(&dev->tasklet_lock);
518 }
519