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,
43 static int drm_setup(drm_device_t * dev)
48 if (dev->driver->firstopen) {
49 ret = dev->driver->firstopen(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)) {
60 i = drm_dma_setup(dev);
65 for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++)
66 atomic_set(&dev->counts[i], 0);
68 for (i = 0; i < DRM_HASH_SIZE; i++) {
69 dev->magiclist[i].head = NULL;
70 dev->magiclist[i].tail = NULL;
73 dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist), DRM_MEM_CTXLIST);
74 if (dev->ctxlist == NULL)
76 memset(dev->ctxlist, 0, sizeof(*dev->ctxlist));
77 INIT_LIST_HEAD(&dev->ctxlist->head);
80 dev->sigdata.lock = dev->lock.hw_lock = NULL;
81 init_waitqueue_head(&dev->lock.lock_queue);
83 dev->queue_reserved = 0;
85 dev->queuelist = NULL;
87 dev->context_flag = 0;
88 dev->interrupt_flag = 0;
90 dev->last_context = 0;
92 dev->last_checked = 0;
93 init_waitqueue_head(&dev->context_wait);
99 dev->buf_async = NULL;
100 init_waitqueue_head(&dev->buf_readers);
101 init_waitqueue_head(&dev->buf_writers);
106 * The kernel's context could be created here, but is now created
107 * in drm_dma_enqueue. This is more resource-efficient for
108 * hardware that does not do DMA, but may mean that
109 * drm_select_queue fails between the time the interrupt is
110 * initialized and the time the queues are initialized.
119 * \param inode device inode
120 * \param filp file pointer.
121 * \return zero on success or a negative number on failure.
123 * Searches the DRM device with the same minor number, calls open_helper(), and
124 * increments the device open count. If the open count was previous at zero,
125 * i.e., it's the first that the device is open, then calls setup().
127 int drm_open(struct inode *inode, struct file *filp)
129 drm_device_t *dev = NULL;
130 int minor = iminor(inode);
133 if (!((minor >= 0) && (minor < drm_cards_limit)))
136 if (!drm_heads[minor])
139 if (!(dev = drm_heads[minor]->dev))
142 retcode = drm_open_helper(inode, filp, dev);
144 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
145 spin_lock(&dev->count_lock);
146 if (!dev->open_count++) {
147 spin_unlock(&dev->count_lock);
148 return drm_setup(dev);
150 spin_unlock(&dev->count_lock);
156 EXPORT_SYMBOL(drm_open);
161 * \param inode device inode
162 * \param filp file pointer.
163 * \return zero on success or a negative number on failure.
165 * If the hardware lock is held then free it, and take it again for the kernel
166 * context since it's necessary to reclaim buffers. Unlink the file private
167 * data from its list and free it. Decreases the open count and if it reaches
168 * zero calls drm_lastclose().
170 int drm_release(struct inode *inode, struct file *filp)
172 drm_file_t *priv = filp->private_data;
177 dev = priv->head->dev;
179 DRM_DEBUG("open_count = %d\n", dev->open_count);
181 if (dev->driver->preclose)
182 dev->driver->preclose(dev, filp);
184 /* ========================================================
185 * Begin inline drm_release
188 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
189 current->pid, (long)old_encode_dev(priv->head->device),
192 if (priv->lock_count && dev->lock.hw_lock &&
193 _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) &&
194 dev->lock.filp == filp) {
195 DRM_DEBUG("File %p released, freeing lock for context %d\n",
196 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
198 if (dev->driver->reclaim_buffers_locked)
199 dev->driver->reclaim_buffers_locked(dev, filp);
201 drm_lock_free(dev, &dev->lock.hw_lock->lock,
202 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
204 /* FIXME: may require heavy-handed reset of
205 hardware at this point, possibly
206 processed via a callback to the X
208 } else if (dev->driver->reclaim_buffers_locked && priv->lock_count
209 && dev->lock.hw_lock) {
210 /* The lock is required to reclaim buffers */
211 DECLARE_WAITQUEUE(entry, current);
213 add_wait_queue(&dev->lock.lock_queue, &entry);
215 __set_current_state(TASK_INTERRUPTIBLE);
216 if (!dev->lock.hw_lock) {
217 /* Device has been unregistered */
221 if (drm_lock_take(&dev->lock.hw_lock->lock,
222 DRM_KERNEL_CONTEXT)) {
223 dev->lock.filp = filp;
224 dev->lock.lock_time = jiffies;
225 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
226 break; /* Got lock */
230 if (signal_pending(current)) {
231 retcode = -ERESTARTSYS;
235 __set_current_state(TASK_RUNNING);
236 remove_wait_queue(&dev->lock.lock_queue, &entry);
238 dev->driver->reclaim_buffers_locked(dev, filp);
239 drm_lock_free(dev, &dev->lock.hw_lock->lock,
244 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
245 !dev->driver->reclaim_buffers_locked) {
246 dev->driver->reclaim_buffers(dev, filp);
249 drm_fasync(-1, filp, 0);
251 down(&dev->ctxlist_sem);
252 if (dev->ctxlist && (!list_empty(&dev->ctxlist->head))) {
253 drm_ctx_list_t *pos, *n;
255 list_for_each_entry_safe(pos, n, &dev->ctxlist->head, head) {
256 if (pos->tag == priv &&
257 pos->handle != DRM_KERNEL_CONTEXT) {
258 if (dev->driver->context_dtor)
259 dev->driver->context_dtor(dev,
262 drm_ctxbitmap_free(dev, pos->handle);
264 list_del(&pos->head);
265 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
270 up(&dev->ctxlist_sem);
272 down(&dev->struct_sem);
273 if (priv->remove_auth_on_close == 1) {
274 drm_file_t *temp = dev->file_first;
276 temp->authenticated = 0;
281 priv->prev->next = priv->next;
283 dev->file_first = priv->next;
286 priv->next->prev = priv->prev;
288 dev->file_last = priv->prev;
290 up(&dev->struct_sem);
292 if (dev->driver->postclose)
293 dev->driver->postclose(dev, priv);
295 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
297 /* ========================================================
298 * End inline drm_release
301 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
302 spin_lock(&dev->count_lock);
303 if (!--dev->open_count) {
304 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
305 DRM_ERROR("Device busy: %d %d\n",
306 atomic_read(&dev->ioctl_count), dev->blocked);
307 spin_unlock(&dev->count_lock);
311 spin_unlock(&dev->count_lock);
313 return drm_lastclose(dev);
315 spin_unlock(&dev->count_lock);
322 EXPORT_SYMBOL(drm_release);
325 * Check whether DRI will run on this CPU.
327 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
329 static int drm_cpu_valid(void)
331 #if defined(__i386__)
332 if (boot_cpu_data.x86 == 3)
333 return 0; /* No cmpxchg on a 386 */
335 #if defined(__sparc__) && !defined(__sparc_v9__)
336 return 0; /* No cmpxchg before v9 sparc. */
342 * Called whenever a process opens /dev/drm.
344 * \param inode device inode.
345 * \param filp file pointer.
347 * \return zero on success or a negative number on failure.
349 * Creates and initializes a drm_file structure for the file private data in \p
350 * filp and add it into the double linked list in \p dev.
352 static int drm_open_helper(struct inode *inode, struct file *filp,
355 int minor = iminor(inode);
359 if (filp->f_flags & O_EXCL)
360 return -EBUSY; /* No exclusive opens */
361 if (!drm_cpu_valid())
364 DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
366 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
370 memset(priv, 0, sizeof(*priv));
371 filp->private_data = priv;
372 priv->uid = current->euid;
373 priv->pid = current->pid;
375 priv->head = drm_heads[minor];
376 priv->ioctl_count = 0;
377 priv->authenticated = capable(CAP_SYS_ADMIN);
378 priv->lock_count = 0;
380 if (dev->driver->open) {
381 ret = dev->driver->open(dev, priv);
386 down(&dev->struct_sem);
387 if (!dev->file_last) {
390 dev->file_first = priv;
391 dev->file_last = priv;
394 priv->prev = dev->file_last;
395 dev->file_last->next = priv;
396 dev->file_last = priv;
398 up(&dev->struct_sem);
405 struct pci_dev *pci_dev;
406 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
408 dev->hose = pci_dev->sysdata;
409 pci_dev_put(pci_dev);
412 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
414 dev->hose = b->sysdata;
421 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
422 filp->private_data = NULL;
427 int drm_flush(struct file *filp)
429 drm_file_t *priv = filp->private_data;
430 drm_device_t *dev = priv->head->dev;
432 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
433 current->pid, (long)old_encode_dev(priv->head->device),
438 EXPORT_SYMBOL(drm_flush);
441 int drm_fasync(int fd, struct file *filp, int on)
443 drm_file_t *priv = filp->private_data;
444 drm_device_t *dev = priv->head->dev;
447 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
448 (long)old_encode_dev(priv->head->device));
449 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
455 EXPORT_SYMBOL(drm_fasync);
458 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
463 EXPORT_SYMBOL(drm_poll);