3 * File operations for DRM
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Daryll Strauss <daryll@valinux.com>
7 * \author Gareth Hughes <gareth@valinux.com>
11 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
13 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 * All Rights Reserved.
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
24 * The above copyright notice and this permission notice (including the next
25 * paragraph) shall be included in all copies or substantial portions of the
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 * OTHER DEALINGS IN THE SOFTWARE.
38 #include <linux/poll.h>
40 static int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t *dev);
42 static int drm_setup( drm_device_t *dev )
47 if (dev->driver->presetup)
49 ret=dev->driver->presetup(dev);
54 atomic_set( &dev->ioctl_count, 0 );
55 atomic_set( &dev->vma_count, 0 );
57 atomic_set( &dev->buf_alloc, 0 );
59 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
61 i = drm_dma_setup( dev );
66 for ( i = 0 ; i < DRM_ARRAY_SIZE(dev->counts) ; i++ )
67 atomic_set( &dev->counts[i], 0 );
69 for ( i = 0 ; i < DRM_HASH_SIZE ; i++ ) {
70 dev->magiclist[i].head = NULL;
71 dev->magiclist[i].tail = NULL;
74 dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist),
76 if(dev->ctxlist == NULL) return -ENOMEM;
77 memset(dev->ctxlist, 0, sizeof(*dev->ctxlist));
78 INIT_LIST_HEAD(&dev->ctxlist->head);
81 dev->sigdata.lock = dev->lock.hw_lock = NULL;
82 init_waitqueue_head( &dev->lock.lock_queue );
84 dev->queue_reserved = 0;
86 dev->queuelist = NULL;
88 dev->context_flag = 0;
89 dev->interrupt_flag = 0;
91 dev->last_context = 0;
93 dev->last_checked = 0;
94 init_waitqueue_head( &dev->context_wait );
100 dev->buf_rp = dev->buf;
101 dev->buf_wp = dev->buf;
102 dev->buf_end = dev->buf + DRM_BSZ;
103 dev->buf_async = NULL;
104 init_waitqueue_head( &dev->buf_readers );
105 init_waitqueue_head( &dev->buf_writers );
110 * The kernel's context could be created here, but is now created
111 * in drm_dma_enqueue. This is more resource-efficient for
112 * hardware that does not do DMA, but may mean that
113 * drm_select_queue fails between the time the interrupt is
114 * initialized and the time the queues are initialized.
116 if (dev->driver->postsetup)
117 dev->driver->postsetup(dev);
125 * \param inode device inode
126 * \param filp file pointer.
127 * \return zero on success or a negative number on failure.
129 * Searches the DRM device with the same minor number, calls open_helper(), and
130 * increments the device open count. If the open count was previous at zero,
131 * i.e., it's the first that the device is open, then calls setup().
133 int drm_open( struct inode *inode, struct file *filp )
135 drm_device_t *dev = NULL;
136 int minor = iminor(inode);
139 if (!((minor >= 0) && (minor < drm_cards_limit)))
142 if (!drm_heads[minor])
145 if (!(dev = drm_heads[minor]->dev))
148 retcode = drm_open_helper( inode, filp, dev );
150 atomic_inc( &dev->counts[_DRM_STAT_OPENS] );
151 spin_lock( &dev->count_lock );
152 if ( !dev->open_count++ ) {
153 spin_unlock( &dev->count_lock );
154 return drm_setup( dev );
156 spin_unlock( &dev->count_lock );
161 EXPORT_SYMBOL(drm_open);
166 * \param inode device inode
167 * \param filp file pointer.
168 * \return zero on success or a negative number on failure.
170 * If the hardware lock is held then free it, and take it again for the kernel
171 * context since it's necessary to reclaim buffers. Unlink the file private
172 * data from its list and free it. Decreases the open count and if it reaches
173 * zero calls takedown().
175 int drm_release( struct inode *inode, struct file *filp )
177 drm_file_t *priv = filp->private_data;
182 dev = priv->head->dev;
184 DRM_DEBUG( "open_count = %d\n", dev->open_count );
186 if (dev->driver->prerelease)
187 dev->driver->prerelease(dev, filp);
189 /* ========================================================
190 * Begin inline drm_release
193 DRM_DEBUG( "pid = %d, device = 0x%lx, open_count = %d\n",
194 current->pid, (long)old_encode_dev(priv->head->device), dev->open_count );
196 if ( priv->lock_count && dev->lock.hw_lock &&
197 _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) &&
198 dev->lock.filp == filp ) {
199 DRM_DEBUG( "File %p released, freeing lock for context %d\n",
201 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
203 if (dev->driver->release)
204 dev->driver->release(dev, filp);
206 drm_lock_free( dev, &dev->lock.hw_lock->lock,
207 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
209 /* FIXME: may require heavy-handed reset of
210 hardware at this point, possibly
211 processed via a callback to the X
214 else if ( dev->driver->release && priv->lock_count && dev->lock.hw_lock ) {
215 /* The lock is required to reclaim buffers */
216 DECLARE_WAITQUEUE( entry, current );
218 add_wait_queue( &dev->lock.lock_queue, &entry );
220 __set_current_state(TASK_INTERRUPTIBLE);
221 if ( !dev->lock.hw_lock ) {
222 /* Device has been unregistered */
226 if ( drm_lock_take( &dev->lock.hw_lock->lock,
227 DRM_KERNEL_CONTEXT ) ) {
228 dev->lock.filp = filp;
229 dev->lock.lock_time = jiffies;
230 atomic_inc( &dev->counts[_DRM_STAT_LOCKS] );
231 break; /* Got lock */
235 if ( signal_pending( current ) ) {
236 retcode = -ERESTARTSYS;
240 __set_current_state(TASK_RUNNING);
241 remove_wait_queue( &dev->lock.lock_queue, &entry );
243 if (dev->driver->release)
244 dev->driver->release(dev, filp);
245 drm_lock_free( dev, &dev->lock.hw_lock->lock,
246 DRM_KERNEL_CONTEXT );
250 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) && !dev->driver->release)
252 dev->driver->reclaim_buffers(dev, filp);
255 drm_fasync( -1, filp, 0 );
257 down( &dev->ctxlist_sem );
258 if ( dev->ctxlist && (!list_empty(&dev->ctxlist->head))) {
259 drm_ctx_list_t *pos, *n;
261 list_for_each_entry_safe( pos, n, &dev->ctxlist->head, head ) {
262 if ( pos->tag == priv &&
263 pos->handle != DRM_KERNEL_CONTEXT ) {
264 if (dev->driver->context_dtor)
265 dev->driver->context_dtor(dev, pos->handle);
267 drm_ctxbitmap_free( dev, pos->handle );
269 list_del( &pos->head );
270 drm_free( pos, sizeof(*pos), DRM_MEM_CTXLIST );
275 up( &dev->ctxlist_sem );
277 down( &dev->struct_sem );
278 if ( priv->remove_auth_on_close == 1 ) {
279 drm_file_t *temp = dev->file_first;
281 temp->authenticated = 0;
286 priv->prev->next = priv->next;
288 dev->file_first = priv->next;
291 priv->next->prev = priv->prev;
293 dev->file_last = priv->prev;
295 up( &dev->struct_sem );
297 if (dev->driver->free_filp_priv)
298 dev->driver->free_filp_priv(dev, priv);
300 drm_free( priv, sizeof(*priv), DRM_MEM_FILES );
302 /* ========================================================
303 * End inline drm_release
306 atomic_inc( &dev->counts[_DRM_STAT_CLOSES] );
307 spin_lock( &dev->count_lock );
308 if ( !--dev->open_count ) {
309 if ( atomic_read( &dev->ioctl_count ) || dev->blocked ) {
310 DRM_ERROR( "Device busy: %d %d\n",
311 atomic_read( &dev->ioctl_count ),
313 spin_unlock( &dev->count_lock );
317 spin_unlock( &dev->count_lock );
319 return drm_takedown( dev );
321 spin_unlock( &dev->count_lock );
327 EXPORT_SYMBOL(drm_release);
330 * Called whenever a process opens /dev/drm.
332 * \param inode device inode.
333 * \param filp file pointer.
335 * \return zero on success or a negative number on failure.
337 * Creates and initializes a drm_file structure for the file private data in \p
338 * filp and add it into the double linked list in \p dev.
340 static int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t *dev)
342 int minor = iminor(inode);
346 if (filp->f_flags & O_EXCL) return -EBUSY; /* No exclusive opens */
347 if (!drm_cpu_valid()) return -EINVAL;
349 DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
351 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
352 if(!priv) return -ENOMEM;
354 memset(priv, 0, sizeof(*priv));
355 filp->private_data = priv;
356 priv->uid = current->euid;
357 priv->pid = current->pid;
359 priv->head = drm_heads[minor];
360 priv->ioctl_count = 0;
361 priv->authenticated = capable(CAP_SYS_ADMIN);
362 priv->lock_count = 0;
364 if (dev->driver->open_helper) {
365 ret=dev->driver->open_helper(dev, priv);
370 down(&dev->struct_sem);
371 if (!dev->file_last) {
374 dev->file_first = priv;
375 dev->file_last = priv;
378 priv->prev = dev->file_last;
379 dev->file_last->next = priv;
380 dev->file_last = priv;
382 up(&dev->struct_sem);
389 struct pci_dev *pci_dev;
390 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
392 dev->hose = pci_dev->sysdata;
393 pci_dev_put(pci_dev);
396 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
397 if (b) dev->hose = b->sysdata;
404 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
405 filp->private_data=NULL;
410 int drm_flush(struct file *filp)
412 drm_file_t *priv = filp->private_data;
413 drm_device_t *dev = priv->head->dev;
415 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
416 current->pid, (long)old_encode_dev(priv->head->device), dev->open_count);
419 EXPORT_SYMBOL(drm_flush);
422 int drm_fasync(int fd, struct file *filp, int on)
424 drm_file_t *priv = filp->private_data;
425 drm_device_t *dev = priv->head->dev;
428 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, (long)old_encode_dev(priv->head->device));
429 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
430 if (retcode < 0) return retcode;
433 EXPORT_SYMBOL(drm_fasync);
436 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
440 EXPORT_SYMBOL(drm_poll);