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_setup( drm_device_t *dev )
45 if (dev->driver->presetup)
47 ret=dev->driver->presetup(dev);
52 atomic_set( &dev->ioctl_count, 0 );
53 atomic_set( &dev->vma_count, 0 );
55 atomic_set( &dev->buf_alloc, 0 );
57 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
59 i = drm_dma_setup( dev );
64 for ( i = 0 ; i < DRM_ARRAY_SIZE(dev->counts) ; i++ )
65 atomic_set( &dev->counts[i], 0 );
67 for ( i = 0 ; i < DRM_HASH_SIZE ; i++ ) {
68 dev->magiclist[i].head = NULL;
69 dev->magiclist[i].tail = NULL;
72 dev->maplist = drm_alloc(sizeof(*dev->maplist),
74 if(dev->maplist == NULL) return -ENOMEM;
75 memset(dev->maplist, 0, sizeof(*dev->maplist));
76 INIT_LIST_HEAD(&dev->maplist->head);
78 dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist),
80 if(dev->ctxlist == NULL) return -ENOMEM;
81 memset(dev->ctxlist, 0, sizeof(*dev->ctxlist));
82 INIT_LIST_HEAD(&dev->ctxlist->head);
85 dev->sigdata.lock = dev->lock.hw_lock = NULL;
86 init_waitqueue_head( &dev->lock.lock_queue );
88 dev->queue_reserved = 0;
90 dev->queuelist = NULL;
92 dev->context_flag = 0;
93 dev->interrupt_flag = 0;
95 dev->last_context = 0;
97 dev->last_checked = 0;
98 init_waitqueue_head( &dev->context_wait );
104 dev->buf_rp = dev->buf;
105 dev->buf_wp = dev->buf;
106 dev->buf_end = dev->buf + DRM_BSZ;
107 dev->buf_async = NULL;
108 init_waitqueue_head( &dev->buf_readers );
109 init_waitqueue_head( &dev->buf_writers );
114 * The kernel's context could be created here, but is now created
115 * in drm_dma_enqueue. This is more resource-efficient for
116 * hardware that does not do DMA, but may mean that
117 * drm_select_queue fails between the time the interrupt is
118 * initialized and the time the queues are initialized.
120 if (dev->driver->postsetup)
121 dev->driver->postsetup(dev);
129 * \param inode device inode
130 * \param filp file pointer.
131 * \return zero on success or a negative number on failure.
133 * Searches the DRM device with the same minor number, calls open_helper(), and
134 * increments the device open count. If the open count was previous at zero,
135 * i.e., it's the first that the device is open, then calls setup().
137 int drm_open( struct inode *inode, struct file *filp )
139 drm_device_t *dev = NULL;
140 int minor = iminor(inode);
143 if (!((minor >= 0) && (minor < drm_cards_limit)))
146 dev = drm_minors[minor].dev;
150 retcode = drm_open_helper( inode, filp, dev );
152 atomic_inc( &dev->counts[_DRM_STAT_OPENS] );
153 spin_lock( &dev->count_lock );
154 if ( !dev->open_count++ ) {
155 spin_unlock( &dev->count_lock );
156 return drm_setup( dev );
158 spin_unlock( &dev->count_lock );
163 EXPORT_SYMBOL(drm_open);
168 * \param inode device inode
169 * \param filp file pointer.
170 * \return zero on success or a negative number on failure.
172 * If the hardware lock is held then free it, and take it again for the kernel
173 * context since it's necessary to reclaim buffers. Unlink the file private
174 * data from its list and free it. Decreases the open count and if it reaches
175 * zero calls takedown().
177 int drm_release( struct inode *inode, struct file *filp )
179 drm_file_t *priv = filp->private_data;
186 DRM_DEBUG( "open_count = %d\n", dev->open_count );
188 if (dev->driver->prerelease)
189 dev->driver->prerelease(dev, filp);
191 /* ========================================================
192 * Begin inline drm_release
195 DRM_DEBUG( "pid = %d, device = 0x%lx, open_count = %d\n",
196 current->pid, (long)old_encode_dev(dev->device), dev->open_count );
198 if ( priv->lock_count && dev->lock.hw_lock &&
199 _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) &&
200 dev->lock.filp == filp ) {
201 DRM_DEBUG( "File %p released, freeing lock for context %d\n",
203 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
205 if (dev->driver->release)
206 dev->driver->release(dev, filp);
208 drm_lock_free( dev, &dev->lock.hw_lock->lock,
209 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
211 /* FIXME: may require heavy-handed reset of
212 hardware at this point, possibly
213 processed via a callback to the X
216 else if ( dev->driver->release && priv->lock_count && dev->lock.hw_lock ) {
217 /* The lock is required to reclaim buffers */
218 DECLARE_WAITQUEUE( entry, current );
220 add_wait_queue( &dev->lock.lock_queue, &entry );
222 __set_current_state(TASK_INTERRUPTIBLE);
223 if ( !dev->lock.hw_lock ) {
224 /* Device has been unregistered */
228 if ( drm_lock_take( &dev->lock.hw_lock->lock,
229 DRM_KERNEL_CONTEXT ) ) {
230 dev->lock.filp = filp;
231 dev->lock.lock_time = jiffies;
232 atomic_inc( &dev->counts[_DRM_STAT_LOCKS] );
233 break; /* Got lock */
237 if ( signal_pending( current ) ) {
238 retcode = -ERESTARTSYS;
242 __set_current_state(TASK_RUNNING);
243 remove_wait_queue( &dev->lock.lock_queue, &entry );
245 if (dev->driver->release)
246 dev->driver->release(dev, filp);
247 drm_lock_free( dev, &dev->lock.hw_lock->lock,
248 DRM_KERNEL_CONTEXT );
252 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
254 dev->driver->reclaim_buffers(dev, filp);
257 drm_fasync( -1, filp, 0 );
259 down( &dev->ctxlist_sem );
260 if ( !list_empty( &dev->ctxlist->head ) ) {
261 drm_ctx_list_t *pos, *n;
263 list_for_each_entry_safe( pos, n, &dev->ctxlist->head, head ) {
264 if ( pos->tag == priv &&
265 pos->handle != DRM_KERNEL_CONTEXT ) {
266 if (dev->driver->context_dtor)
267 dev->driver->context_dtor(dev, pos->handle);
269 drm_ctxbitmap_free( dev, pos->handle );
271 list_del( &pos->head );
272 drm_free( pos, sizeof(*pos), DRM_MEM_CTXLIST );
277 up( &dev->ctxlist_sem );
279 down( &dev->struct_sem );
280 if ( priv->remove_auth_on_close == 1 ) {
281 drm_file_t *temp = dev->file_first;
283 temp->authenticated = 0;
288 priv->prev->next = priv->next;
290 dev->file_first = priv->next;
293 priv->next->prev = priv->prev;
295 dev->file_last = priv->prev;
297 up( &dev->struct_sem );
299 if (dev->driver->free_filp_priv)
300 dev->driver->free_filp_priv(dev, priv);
302 drm_free( priv, sizeof(*priv), DRM_MEM_FILES );
304 /* ========================================================
305 * End inline drm_release
308 atomic_inc( &dev->counts[_DRM_STAT_CLOSES] );
309 spin_lock( &dev->count_lock );
310 if ( !--dev->open_count ) {
311 if ( atomic_read( &dev->ioctl_count ) || dev->blocked ) {
312 DRM_ERROR( "Device busy: %d %d\n",
313 atomic_read( &dev->ioctl_count ),
315 spin_unlock( &dev->count_lock );
319 spin_unlock( &dev->count_lock );
321 return drm_takedown( dev );
323 spin_unlock( &dev->count_lock );
329 EXPORT_SYMBOL(drm_release);
332 * Called whenever a process opens /dev/drm.
334 * \param inode device inode.
335 * \param filp file pointer.
337 * \return zero on success or a negative number on failure.
339 * Creates and initializes a drm_file structure for the file private data in \p
340 * filp and add it into the double linked list in \p dev.
342 int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t *dev)
344 int minor = iminor(inode);
348 if (filp->f_flags & O_EXCL) return -EBUSY; /* No exclusive opens */
349 if (!drm_cpu_valid()) return -EINVAL;
351 DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
353 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
354 if(!priv) return -ENOMEM;
356 memset(priv, 0, sizeof(*priv));
357 filp->private_data = priv;
358 priv->uid = current->euid;
359 priv->pid = current->pid;
362 priv->ioctl_count = 0;
363 priv->authenticated = capable(CAP_SYS_ADMIN);
364 priv->lock_count = 0;
366 if (dev->driver->open_helper) {
367 ret=dev->driver->open_helper(dev, priv);
372 down(&dev->struct_sem);
373 if (!dev->file_last) {
376 dev->file_first = priv;
377 dev->file_last = priv;
380 priv->prev = dev->file_last;
381 dev->file_last->next = priv;
382 dev->file_last = priv;
384 up(&dev->struct_sem);
391 struct pci_dev *pci_dev;
392 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
394 dev->hose = pci_dev->sysdata;
395 pci_dev_put(pci_dev);
398 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
399 if (b) dev->hose = b->sysdata;
406 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
407 filp->private_data=NULL;
412 int drm_flush(struct file *filp)
414 drm_file_t *priv = filp->private_data;
415 drm_device_t *dev = priv->dev;
417 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
418 current->pid, (long)old_encode_dev(dev->device), dev->open_count);
421 EXPORT_SYMBOL(drm_flush);
424 int drm_fasync(int fd, struct file *filp, int on)
426 drm_file_t *priv = filp->private_data;
427 drm_device_t *dev = priv->dev;
430 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, (long)old_encode_dev(dev->device));
431 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
432 if (retcode < 0) return retcode;
435 EXPORT_SYMBOL(drm_fasync);
438 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
442 EXPORT_SYMBOL(drm_poll);
446 ssize_t drm_read(struct file *filp, char __user *buf, size_t count, loff_t *off)