1 /* drm_dma.c -- DMA IOCTL and function support
2 * Created: Fri Oct 18 2003 by anholt@FreeBSD.org */
4 * Copyright 2003 Eric Anholt
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
27 * Eric Anholt <anholt@FreeBSD.org>
32 int DRM(irq_by_busid)( DRM_IOCTL_ARGS )
37 DRM_COPY_FROM_USER_IOCTL(irq, (drm_irq_busid_t *)data, sizeof(irq));
39 if ((irq.busnum >> 8) != dev->pci_domain ||
40 (irq.busnum & 0xff) != dev->pci_bus ||
41 irq.devnum != dev->pci_slot ||
42 irq.funcnum != dev->pci_func)
47 DRM_DEBUG("%d:%d:%d => IRQ %d\n",
48 irq.busnum, irq.devnum, irq.funcnum, irq.irq);
50 DRM_COPY_TO_USER_IOCTL( (drm_irq_busid_t *)data, irq, sizeof(irq) );
55 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
57 DRM(irq_handler_wrap)(DRM_IRQ_ARGS)
59 drm_device_t *dev = (drm_device_t *)arg;
61 DRM_SPINLOCK(&dev->irq_lock);
62 DRM(irq_handler)(arg);
63 DRM_SPINUNLOCK(&dev->irq_lock);
67 int DRM(irq_install)(drm_device_t *dev)
71 if (dev->irq == 0 || dev->dev_private == NULL)
72 return DRM_ERR(EINVAL);
75 if (dev->irq_enabled) {
77 return DRM_ERR(EBUSY);
82 DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
84 dev->context_flag = 0;
86 dev->dma->next_buffer = NULL;
87 dev->dma->this_buffer = NULL;
90 TASK_INIT(&dev->task, 0, DRM(dma_immediate_bh), dev);
93 DRM_SPININIT(dev->irq_lock, "DRM IRQ lock");
95 /* Before installing handler */
96 DRM(driver_irq_preinstall)( dev );
101 dev->irqr = bus_alloc_resource_any(dev->device, SYS_RES_IRQ,
102 &dev->irqrid, RF_SHAREABLE);
107 #if __FreeBSD_version < 500000
108 retcode = bus_setup_intr(dev->device, dev->irqr, INTR_TYPE_TTY,
109 DRM(irq_handler), dev, &dev->irqh);
111 retcode = bus_setup_intr(dev->device, dev->irqr, INTR_TYPE_TTY | INTR_MPSAFE,
112 DRM(irq_handler_wrap), dev, &dev->irqh);
116 #elif defined(__NetBSD__)
117 if (pci_intr_map(&dev->pa, &dev->ih) != 0) {
121 dev->irqh = pci_intr_establish(&dev->pa.pa_pc, dev->ih, IPL_TTY,
122 (irqreturn_t (*)(DRM_IRQ_ARGS))DRM(irq_handler), dev);
129 /* After installing handler */
130 DRM(driver_irq_postinstall)( dev );
135 dev->irq_enabled = 0;
137 if (dev->irqrid != 0) {
138 bus_release_resource(dev->device, SYS_RES_IRQ, dev->irqrid,
143 DRM_SPINUNINIT(dev->irq_lock);
148 /* XXX: This function needs to be called with the device lock held. In some
149 * cases it isn't, so far.
151 int DRM(irq_uninstall)( drm_device_t *dev )
155 if (!dev->irq_enabled)
156 return DRM_ERR(EINVAL);
158 dev->irq_enabled = 0;
159 irqrid = dev->irqrid;
162 DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
164 DRM(driver_irq_uninstall)( dev );
167 bus_teardown_intr(dev->device, dev->irqr, dev->irqh);
168 bus_release_resource(dev->device, SYS_RES_IRQ, irqrid, dev->irqr);
169 #elif defined(__NetBSD__)
170 pci_intr_disestablish(&dev->pa.pa_pc, dev->irqh);
172 DRM_SPINUNINIT(dev->irq_lock);
177 int DRM(control)( DRM_IOCTL_ARGS )
183 DRM_COPY_FROM_USER_IOCTL( ctl, (drm_control_t *) data, sizeof(ctl) );
185 switch ( ctl.func ) {
186 case DRM_INST_HANDLER:
187 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
189 return DRM_ERR(EINVAL);
190 return DRM(irq_install)(dev);
191 case DRM_UNINST_HANDLER:
193 err = DRM(irq_uninstall)( dev );
197 return DRM_ERR(EINVAL);
202 int DRM(wait_vblank)( DRM_IOCTL_ARGS )
205 drm_wait_vblank_t vblwait;
209 if (!dev->irq_enabled)
210 return DRM_ERR(EINVAL);
212 DRM_COPY_FROM_USER_IOCTL( vblwait, (drm_wait_vblank_t *)data,
215 if (vblwait.request.type & _DRM_VBLANK_RELATIVE) {
216 vblwait.request.sequence += atomic_read(&dev->vbl_received);
217 vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
220 flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
221 if (flags & _DRM_VBLANK_SIGNAL) {
223 drm_vbl_sig_t *vbl_sig = DRM_MALLOC(sizeof(drm_vbl_sig_t));
226 bzero(vbl_sig, sizeof(*vbl_sig));
228 vbl_sig->sequence = vblwait.request.sequence;
229 vbl_sig->signo = vblwait.request.signal;
230 vbl_sig->pid = DRM_CURRENTPID;
232 vblwait.reply.sequence = atomic_read(&dev->vbl_received);
234 DRM_SPINLOCK(&dev->irq_lock);
235 TAILQ_INSERT_HEAD(&dev->vbl_sig_list, vbl_sig, link);
236 DRM_SPINUNLOCK(&dev->irq_lock);
241 ret = DRM(vblank_wait)(dev, &vblwait.request.sequence);
244 vblwait.reply.tval_sec = now.tv_sec;
245 vblwait.reply.tval_usec = now.tv_usec;
248 DRM_COPY_TO_USER_IOCTL( (drm_wait_vblank_t *)data, vblwait,
254 void DRM(vbl_send_signals)(drm_device_t *dev)
259 void DRM(vbl_send_signals)( drm_device_t *dev )
261 drm_vbl_sig_t *vbl_sig;
262 unsigned int vbl_seq = atomic_read( &dev->vbl_received );
265 vbl_sig = TAILQ_FIRST(&dev->vbl_sig_list);
266 while (vbl_sig != NULL) {
267 drm_vbl_sig_t *next = TAILQ_NEXT(vbl_sig, link);
269 if ( ( vbl_seq - vbl_sig->sequence ) <= (1<<23) ) {
270 p = pfind(vbl_sig->pid);
272 psignal(p, vbl_sig->signo);
274 TAILQ_REMOVE(&dev->vbl_sig_list, vbl_sig, link);
275 DRM_FREE(vbl_sig,sizeof(*vbl_sig));
282 #endif /* __HAVE_VBL_IRQ */