blob: e82e78fa42b07875c78802b1f29fc6a42af41f02 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_fops.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * File operations for DRM
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Daryll Strauss <daryll@valinux.com>
7 * \author Gareth Hughes <gareth@valinux.com>
8 */
9
10/*
11 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
12 *
13 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 * All Rights Reserved.
16 *
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:
23 *
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
26 * Software.
27 *
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.
35 */
36
37#include "drmP.h"
Dave Airlied985c102006-01-02 21:32:48 +110038#include "drm_sarea.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/poll.h>
40
Dave Airlieb5e89ed2005-09-25 14:28:13 +100041static int drm_open_helper(struct inode *inode, struct file *filp,
42 drm_device_t * dev);
Dave Airliec94f7022005-07-07 21:03:38 +100043
Dave Airlieb5e89ed2005-09-25 14:28:13 +100044static int drm_setup(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Dave Airlied985c102006-01-02 21:32:48 +110046 drm_local_map_t *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int i;
48 int ret;
Dave Airlie11d9c2f2007-02-18 17:13:39 +110049 u32 sareapage;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Dave Airlie22eae942005-11-10 22:16:34 +110051 if (dev->driver->firstopen) {
52 ret = dev->driver->firstopen(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100053 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return ret;
55 }
56
Thomas Hellstrom1f4eccf2006-08-18 16:37:10 +100057 dev->magicfree.next = NULL;
58
Dave Airlied985c102006-01-02 21:32:48 +110059 /* prebuild the SAREA */
Andrew Morton4b560fd2007-03-19 09:08:21 +110060 sareapage = max_t(unsigned, SAREA_MAX, PAGE_SIZE);
Dave Airlie11d9c2f2007-02-18 17:13:39 +110061 i = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK, &map);
Dave Airlied985c102006-01-02 21:32:48 +110062 if (i != 0)
63 return i;
64
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 atomic_set(&dev->ioctl_count, 0);
66 atomic_set(&dev->vma_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 dev->buf_use = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100068 atomic_set(&dev->buf_alloc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dave Airlieb5e89ed2005-09-25 14:28:13 +100070 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
71 i = drm_dma_setup(dev);
72 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return i;
74 }
75
Dave Airlie3d774612006-08-07 20:07:43 +100076 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077 atomic_set(&dev->counts[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100079 drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
80 INIT_LIST_HEAD(&dev->magicfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Dave Airliebd1b3312007-05-26 05:01:51 +100082 INIT_LIST_HEAD(&dev->ctxlist);
83 INIT_LIST_HEAD(&dev->vmalist);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Dave Airlie11d9c2f2007-02-18 17:13:39 +110085 dev->sigdata.lock = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100086 init_waitqueue_head(&dev->lock.lock_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 dev->queue_count = 0;
88 dev->queue_reserved = 0;
89 dev->queue_slots = 0;
90 dev->queuelist = NULL;
91 dev->irq_enabled = 0;
92 dev->context_flag = 0;
93 dev->interrupt_flag = 0;
94 dev->dma_flag = 0;
95 dev->last_context = 0;
96 dev->last_switch = 0;
97 dev->last_checked = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 init_waitqueue_head(&dev->context_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 dev->if_version = 0;
100
101 dev->ctx_start = 0;
102 dev->lck_start = 0;
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 dev->buf_async = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000105 init_waitqueue_head(&dev->buf_readers);
106 init_waitqueue_head(&dev->buf_writers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000108 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 /*
111 * The kernel's context could be created here, but is now created
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000112 * in drm_dma_enqueue. This is more resource-efficient for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * hardware that does not do DMA, but may mean that
114 * drm_select_queue fails between the time the interrupt is
115 * initialized and the time the queues are initialized.
116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 return 0;
119}
120
121/**
122 * Open file.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000123 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * \param inode device inode
125 * \param filp file pointer.
126 * \return zero on success or a negative number on failure.
127 *
128 * Searches the DRM device with the same minor number, calls open_helper(), and
129 * increments the device open count. If the open count was previous at zero,
130 * i.e., it's the first that the device is open, then calls setup().
131 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132int drm_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 drm_device_t *dev = NULL;
135 int minor = iminor(inode);
136 int retcode = 0;
137
138 if (!((minor >= 0) && (minor < drm_cards_limit)))
139 return -ENODEV;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (!drm_heads[minor])
142 return -ENODEV;
143
144 if (!(dev = drm_heads[minor]->dev))
145 return -ENODEV;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000146
147 retcode = drm_open_helper(inode, filp, dev);
148 if (!retcode) {
149 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
150 spin_lock(&dev->count_lock);
151 if (!dev->open_count++) {
152 spin_unlock(&dev->count_lock);
153 return drm_setup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000155 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
158 return retcode;
159}
160EXPORT_SYMBOL(drm_open);
161
162/**
Dave Airlied985c102006-01-02 21:32:48 +1100163 * File \c open operation.
164 *
165 * \param inode device inode.
166 * \param filp file pointer.
167 *
168 * Puts the dev->fops corresponding to the device minor number into
169 * \p filp, call the \c open method, and restore the file operations.
170 */
171int drm_stub_open(struct inode *inode, struct file *filp)
172{
173 drm_device_t *dev = NULL;
174 int minor = iminor(inode);
175 int err = -ENODEV;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800176 const struct file_operations *old_fops;
Dave Airlied985c102006-01-02 21:32:48 +1100177
178 DRM_DEBUG("\n");
179
180 if (!((minor >= 0) && (minor < drm_cards_limit)))
181 return -ENODEV;
182
183 if (!drm_heads[minor])
184 return -ENODEV;
185
186 if (!(dev = drm_heads[minor]->dev))
187 return -ENODEV;
188
189 old_fops = filp->f_op;
190 filp->f_op = fops_get(&dev->driver->fops);
191 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
192 fops_put(filp->f_op);
193 filp->f_op = fops_get(old_fops);
194 }
195 fops_put(old_fops);
196
197 return err;
198}
199
200/**
201 * Check whether DRI will run on this CPU.
202 *
203 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
204 */
205static int drm_cpu_valid(void)
206{
207#if defined(__i386__)
208 if (boot_cpu_data.x86 == 3)
209 return 0; /* No cmpxchg on a 386 */
210#endif
211#if defined(__sparc__) && !defined(__sparc_v9__)
212 return 0; /* No cmpxchg before v9 sparc. */
213#endif
214 return 1;
215}
216
217/**
218 * Called whenever a process opens /dev/drm.
219 *
220 * \param inode device inode.
221 * \param filp file pointer.
222 * \param dev device.
223 * \return zero on success or a negative number on failure.
224 *
225 * Creates and initializes a drm_file structure for the file private data in \p
226 * filp and add it into the double linked list in \p dev.
227 */
228static int drm_open_helper(struct inode *inode, struct file *filp,
229 drm_device_t * dev)
230{
231 int minor = iminor(inode);
232 drm_file_t *priv;
233 int ret;
234
235 if (filp->f_flags & O_EXCL)
236 return -EBUSY; /* No exclusive opens */
237 if (!drm_cpu_valid())
238 return -EINVAL;
239
240 DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
241
242 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
243 if (!priv)
244 return -ENOMEM;
245
246 memset(priv, 0, sizeof(*priv));
247 filp->private_data = priv;
248 priv->uid = current->euid;
249 priv->pid = current->pid;
250 priv->minor = minor;
251 priv->head = drm_heads[minor];
252 priv->ioctl_count = 0;
253 /* for compatibility root is always authenticated */
254 priv->authenticated = capable(CAP_SYS_ADMIN);
255 priv->lock_count = 0;
256
Dave Airliebd1b3312007-05-26 05:01:51 +1000257 INIT_LIST_HEAD(&priv->lhead);
258
Dave Airlied985c102006-01-02 21:32:48 +1100259 if (dev->driver->open) {
260 ret = dev->driver->open(dev, priv);
261 if (ret < 0)
262 goto out_free;
263 }
264
Dave Airlie30e2fb12006-02-02 19:37:46 +1100265 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000266 if (list_empty(&dev->filelist))
Dave Airlied985c102006-01-02 21:32:48 +1100267 priv->master = 1;
Dave Airliebd1b3312007-05-26 05:01:51 +1000268
269 list_add(&priv->lhead, &dev->filelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100270 mutex_unlock(&dev->struct_mutex);
Dave Airlied985c102006-01-02 21:32:48 +1100271
272#ifdef __alpha__
273 /*
274 * Default the hose
275 */
276 if (!dev->hose) {
277 struct pci_dev *pci_dev;
278 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
279 if (pci_dev) {
280 dev->hose = pci_dev->sysdata;
281 pci_dev_put(pci_dev);
282 }
283 if (!dev->hose) {
284 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
285 if (b)
286 dev->hose = b->sysdata;
287 }
288 }
289#endif
290
291 return 0;
292 out_free:
293 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
294 filp->private_data = NULL;
295 return ret;
296}
297
298/** No-op. */
299int drm_fasync(int fd, struct file *filp, int on)
300{
301 drm_file_t *priv = filp->private_data;
302 drm_device_t *dev = priv->head->dev;
303 int retcode;
304
305 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
306 (long)old_encode_dev(priv->head->device));
307 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
308 if (retcode < 0)
309 return retcode;
310 return 0;
311}
312EXPORT_SYMBOL(drm_fasync);
313
314/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * Release file.
316 *
317 * \param inode device inode
318 * \param filp file pointer.
319 * \return zero on success or a negative number on failure.
320 *
321 * If the hardware lock is held then free it, and take it again for the kernel
322 * context since it's necessary to reclaim buffers. Unlink the file private
323 * data from its list and free it. Decreases the open count and if it reaches
Dave Airlie22eae942005-11-10 22:16:34 +1100324 * zero calls drm_lastclose().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326int drm_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 drm_file_t *priv = filp->private_data;
329 drm_device_t *dev;
330 int retcode = 0;
331
332 lock_kernel();
333 dev = priv->head->dev;
334
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000335 DRM_DEBUG("open_count = %d\n", dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Dave Airlie22eae942005-11-10 22:16:34 +1100337 if (dev->driver->preclose)
338 dev->driver->preclose(dev, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* ========================================================
341 * Begin inline drm_release
342 */
343
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000344 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
345 current->pid, (long)old_encode_dev(priv->head->device),
346 dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100348 if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
349 if (drm_i_have_hw_lock(filp)) {
350 dev->driver->reclaim_buffers_locked(dev, filp);
351 } else {
352 unsigned long _end=jiffies + 3*DRM_HZ;
353 int locked = 0;
354
355 drm_idlelock_take(&dev->lock);
356
357 /*
358 * Wait for a while.
359 */
360
361 do{
362 spin_lock(&dev->lock.spinlock);
363 locked = dev->lock.idle_has_lock;
364 spin_unlock(&dev->lock.spinlock);
365 if (locked)
366 break;
367 schedule();
368 } while (!time_after_eq(jiffies, _end));
369
370 if (!locked) {
371 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
372 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
373 "\tI will go on reclaiming the buffers anyway.\n");
374 }
375
376 dev->driver->reclaim_buffers_locked(dev, filp);
377 drm_idlelock_release(&dev->lock);
378 }
379 }
380
381 if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
382
383 drm_idlelock_take(&dev->lock);
384 dev->driver->reclaim_buffers_idlelocked(dev, filp);
385 drm_idlelock_release(&dev->lock);
386
387 }
388
389 if (drm_i_have_hw_lock(filp)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000390 DRM_DEBUG("File %p released, freeing lock for context %d\n",
391 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
392
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100393 drm_lock_free(&dev->lock,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000394 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000396
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100397
Dave Airlie22eae942005-11-10 22:16:34 +1100398 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
399 !dev->driver->reclaim_buffers_locked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 dev->driver->reclaim_buffers(dev, filp);
401 }
402
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000403 drm_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Dave Airlie30e2fb12006-02-02 19:37:46 +1100405 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000406 if (!list_empty(&dev->ctxlist)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 drm_ctx_list_t *pos, *n;
408
Dave Airliebd1b3312007-05-26 05:01:51 +1000409 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 if (pos->tag == priv &&
411 pos->handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (dev->driver->context_dtor)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000413 dev->driver->context_dtor(dev,
414 pos->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000416 drm_ctxbitmap_free(dev, pos->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418 list_del(&pos->head);
419 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 --dev->ctx_count;
421 }
422 }
423 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100424 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Dave Airlie30e2fb12006-02-02 19:37:46 +1100426 mutex_lock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000427 if (priv->remove_auth_on_close == 1) {
Dave Airliebd1b3312007-05-26 05:01:51 +1000428 drm_file_t *temp;
429
430 list_for_each_entry(temp, &dev->filelist, lhead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 temp->authenticated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000433 list_del(&priv->lhead);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100434 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000435
Dave Airlie22eae942005-11-10 22:16:34 +1100436 if (dev->driver->postclose)
437 dev->driver->postclose(dev, priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000438 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /* ========================================================
441 * End inline drm_release
442 */
443
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000444 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
445 spin_lock(&dev->count_lock);
446 if (!--dev->open_count) {
447 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
448 DRM_ERROR("Device busy: %d %d\n",
449 atomic_read(&dev->ioctl_count), dev->blocked);
450 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 unlock_kernel();
452 return -EBUSY;
453 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000454 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 unlock_kernel();
Dave Airlie22eae942005-11-10 22:16:34 +1100456 return drm_lastclose(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 unlock_kernel();
461
462 return retcode;
463}
464EXPORT_SYMBOL(drm_release);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/** No-op. */
467unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
468{
469 return 0;
470}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000471EXPORT_SYMBOL(drm_poll);