]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/gpu/drm/drm_fops.c
drm/i915: add GEM GTT mapping support
[net-next-2.6.git] / drivers / gpu / drm / drm_fops.c
CommitLineData
1da177e4 1/**
b5e89ed5 2 * \file drm_fops.c
1da177e4 3 * File operations for DRM
b5e89ed5 4 *
1da177e4
LT
5 * \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"
d985c108 38#include "drm_sarea.h"
1da177e4 39#include <linux/poll.h>
70ffa16e 40#include <linux/smp_lock.h>
1da177e4 41
b5e89ed5 42static int drm_open_helper(struct inode *inode, struct file *filp,
84b1fd10 43 struct drm_device * dev);
c94f7029 44
84b1fd10 45static int drm_setup(struct drm_device * dev)
1da177e4
LT
46{
47 int i;
48 int ret;
49
22eae947
DA
50 if (dev->driver->firstopen) {
51 ret = dev->driver->firstopen(dev);
b5e89ed5 52 if (ret != 0)
1da177e4
LT
53 return ret;
54 }
55
b5e89ed5
DA
56 atomic_set(&dev->ioctl_count, 0);
57 atomic_set(&dev->vma_count, 0);
1da177e4 58 dev->buf_use = 0;
b5e89ed5 59 atomic_set(&dev->buf_alloc, 0);
1da177e4 60
b5e89ed5
DA
61 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
62 i = drm_dma_setup(dev);
63 if (i < 0)
1da177e4
LT
64 return i;
65 }
66
3d77461e 67 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
b5e89ed5 68 atomic_set(&dev->counts[i], 0);
1da177e4 69
11d9c2fd 70 dev->sigdata.lock = NULL;
7c1c2871 71
1da177e4
LT
72 dev->queue_count = 0;
73 dev->queue_reserved = 0;
74 dev->queue_slots = 0;
75 dev->queuelist = NULL;
1da177e4
LT
76 dev->context_flag = 0;
77 dev->interrupt_flag = 0;
78 dev->dma_flag = 0;
79 dev->last_context = 0;
80 dev->last_switch = 0;
81 dev->last_checked = 0;
b5e89ed5 82 init_waitqueue_head(&dev->context_wait);
1da177e4
LT
83 dev->if_version = 0;
84
85 dev->ctx_start = 0;
86 dev->lck_start = 0;
87
1da177e4 88 dev->buf_async = NULL;
b5e89ed5
DA
89 init_waitqueue_head(&dev->buf_readers);
90 init_waitqueue_head(&dev->buf_writers);
1da177e4 91
b5e89ed5 92 DRM_DEBUG("\n");
1da177e4
LT
93
94 /*
95 * The kernel's context could be created here, but is now created
b5e89ed5 96 * in drm_dma_enqueue. This is more resource-efficient for
1da177e4
LT
97 * hardware that does not do DMA, but may mean that
98 * drm_select_queue fails between the time the interrupt is
99 * initialized and the time the queues are initialized.
100 */
1da177e4
LT
101
102 return 0;
103}
104
105/**
106 * Open file.
b5e89ed5 107 *
1da177e4
LT
108 * \param inode device inode
109 * \param filp file pointer.
110 * \return zero on success or a negative number on failure.
111 *
112 * Searches the DRM device with the same minor number, calls open_helper(), and
113 * increments the device open count. If the open count was previous at zero,
114 * i.e., it's the first that the device is open, then calls setup().
115 */
b5e89ed5 116int drm_open(struct inode *inode, struct file *filp)
1da177e4 117{
84b1fd10 118 struct drm_device *dev = NULL;
2c14f28b
DA
119 int minor_id = iminor(inode);
120 struct drm_minor *minor;
1da177e4
LT
121 int retcode = 0;
122
2c14f28b
DA
123 minor = idr_find(&drm_minors_idr, minor_id);
124 if (!minor)
1da177e4 125 return -ENODEV;
b5e89ed5 126
2c14f28b 127 if (!(dev = minor->dev))
1da177e4 128 return -ENODEV;
b5e89ed5
DA
129
130 retcode = drm_open_helper(inode, filp, dev);
131 if (!retcode) {
132 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
133 spin_lock(&dev->count_lock);
134 if (!dev->open_count++) {
135 spin_unlock(&dev->count_lock);
a2c0a97b
JB
136 retcode = drm_setup(dev);
137 goto out;
1da177e4 138 }
b5e89ed5 139 spin_unlock(&dev->count_lock);
1da177e4
LT
140 }
141
a2c0a97b
JB
142out:
143 mutex_lock(&dev->struct_mutex);
144 if (dev->dev_mapping == NULL)
145 dev->dev_mapping = inode->i_mapping;
146 else if (dev->dev_mapping != inode->i_mapping)
147 WARN(1, "dev->dev_mapping not inode mapping (%p expected %p)\n",
148 dev->dev_mapping, inode->i_mapping);
149 mutex_unlock(&dev->struct_mutex);
150
1da177e4
LT
151 return retcode;
152}
153EXPORT_SYMBOL(drm_open);
154
d985c108
DA
155/**
156 * File \c open operation.
157 *
158 * \param inode device inode.
159 * \param filp file pointer.
160 *
161 * Puts the dev->fops corresponding to the device minor number into
162 * \p filp, call the \c open method, and restore the file operations.
163 */
164int drm_stub_open(struct inode *inode, struct file *filp)
165{
84b1fd10 166 struct drm_device *dev = NULL;
2c14f28b
DA
167 struct drm_minor *minor;
168 int minor_id = iminor(inode);
d985c108 169 int err = -ENODEV;
99ac48f5 170 const struct file_operations *old_fops;
d985c108
DA
171
172 DRM_DEBUG("\n");
173
70ffa16e
JC
174 /* BKL pushdown: note that nothing else serializes idr_find() */
175 lock_kernel();
2c14f28b
DA
176 minor = idr_find(&drm_minors_idr, minor_id);
177 if (!minor)
70ffa16e 178 goto out;
d985c108 179
2c14f28b 180 if (!(dev = minor->dev))
70ffa16e 181 goto out;
d985c108
DA
182
183 old_fops = filp->f_op;
184 filp->f_op = fops_get(&dev->driver->fops);
185 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
186 fops_put(filp->f_op);
187 filp->f_op = fops_get(old_fops);
188 }
189 fops_put(old_fops);
190
70ffa16e
JC
191out:
192 unlock_kernel();
d985c108
DA
193 return err;
194}
195
196/**
197 * Check whether DRI will run on this CPU.
198 *
199 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
200 */
201static int drm_cpu_valid(void)
202{
203#if defined(__i386__)
204 if (boot_cpu_data.x86 == 3)
205 return 0; /* No cmpxchg on a 386 */
206#endif
207#if defined(__sparc__) && !defined(__sparc_v9__)
208 return 0; /* No cmpxchg before v9 sparc. */
209#endif
210 return 1;
211}
212
213/**
214 * Called whenever a process opens /dev/drm.
215 *
216 * \param inode device inode.
217 * \param filp file pointer.
218 * \param dev device.
219 * \return zero on success or a negative number on failure.
220 *
221 * Creates and initializes a drm_file structure for the file private data in \p
222 * filp and add it into the double linked list in \p dev.
223 */
224static int drm_open_helper(struct inode *inode, struct file *filp,
84b1fd10 225 struct drm_device * dev)
d985c108 226{
2c14f28b 227 int minor_id = iminor(inode);
84b1fd10 228 struct drm_file *priv;
d985c108
DA
229 int ret;
230
231 if (filp->f_flags & O_EXCL)
232 return -EBUSY; /* No exclusive opens */
233 if (!drm_cpu_valid())
234 return -EINVAL;
235
2c14f28b 236 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
d985c108
DA
237
238 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
239 if (!priv)
240 return -ENOMEM;
241
242 memset(priv, 0, sizeof(*priv));
243 filp->private_data = priv;
6c340eac 244 priv->filp = filp;
2df68b43 245 priv->uid = current_euid();
ba25f9dc 246 priv->pid = task_pid_nr(current);
2c14f28b 247 priv->minor = idr_find(&drm_minors_idr, minor_id);
d985c108
DA
248 priv->ioctl_count = 0;
249 /* for compatibility root is always authenticated */
250 priv->authenticated = capable(CAP_SYS_ADMIN);
251 priv->lock_count = 0;
252
bd1b331f
DA
253 INIT_LIST_HEAD(&priv->lhead);
254
673a394b
EA
255 if (dev->driver->driver_features & DRIVER_GEM)
256 drm_gem_open(dev, priv);
257
d985c108
DA
258 if (dev->driver->open) {
259 ret = dev->driver->open(dev, priv);
260 if (ret < 0)
261 goto out_free;
262 }
263
7c1c2871
DA
264
265 /* if there is no current master make this fd it */
30e2fb18 266 mutex_lock(&dev->struct_mutex);
7c1c2871
DA
267 if (!priv->minor->master) {
268 /* create a new master */
269 priv->minor->master = drm_master_create(priv->minor);
270 if (!priv->minor->master) {
271 ret = -ENOMEM;
272 goto out_free;
273 }
274
275 priv->is_master = 1;
276 /* take another reference for the copy in the local file priv */
277 priv->master = drm_master_get(priv->minor->master);
278
279 priv->authenticated = 1;
280
281 mutex_unlock(&dev->struct_mutex);
282 if (dev->driver->master_create) {
283 ret = dev->driver->master_create(dev, priv->master);
284 if (ret) {
285 mutex_lock(&dev->struct_mutex);
286 /* drop both references if this fails */
287 drm_master_put(&priv->minor->master);
288 drm_master_put(&priv->master);
289 mutex_unlock(&dev->struct_mutex);
290 goto out_free;
291 }
292 }
293 } else {
294 /* get a reference to the master */
295 priv->master = drm_master_get(priv->minor->master);
296 mutex_unlock(&dev->struct_mutex);
297 }
bd1b331f 298
7c1c2871 299 mutex_lock(&dev->struct_mutex);
bd1b331f 300 list_add(&priv->lhead, &dev->filelist);
30e2fb18 301 mutex_unlock(&dev->struct_mutex);
d985c108
DA
302
303#ifdef __alpha__
304 /*
305 * Default the hose
306 */
307 if (!dev->hose) {
308 struct pci_dev *pci_dev;
309 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
310 if (pci_dev) {
311 dev->hose = pci_dev->sysdata;
312 pci_dev_put(pci_dev);
313 }
314 if (!dev->hose) {
315 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
316 if (b)
317 dev->hose = b->sysdata;
318 }
319 }
320#endif
321
322 return 0;
323 out_free:
324 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
325 filp->private_data = NULL;
326 return ret;
327}
328
329/** No-op. */
330int drm_fasync(int fd, struct file *filp, int on)
331{
84b1fd10 332 struct drm_file *priv = filp->private_data;
2c14f28b 333 struct drm_device *dev = priv->minor->dev;
d985c108
DA
334 int retcode;
335
336 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
2c14f28b 337 (long)old_encode_dev(priv->minor->device));
d985c108
DA
338 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
339 if (retcode < 0)
340 return retcode;
341 return 0;
342}
343EXPORT_SYMBOL(drm_fasync);
344
7c1c2871
DA
345/*
346 * Reclaim locked buffers; note that this may be a bad idea if the current
347 * context doesn't have the hw lock...
348 */
349static void drm_reclaim_locked_buffers(struct drm_device *dev, struct file *f)
350{
351 struct drm_file *file_priv = f->private_data;
352
353 if (drm_i_have_hw_lock(dev, file_priv)) {
354 dev->driver->reclaim_buffers_locked(dev, file_priv);
355 } else {
356 unsigned long _end = jiffies + 3 * DRM_HZ;
357 int locked = 0;
358
359 drm_idlelock_take(&file_priv->master->lock);
360
361 /*
362 * Wait for a while.
363 */
364 do {
365 spin_lock_bh(&file_priv->master->lock.spinlock);
366 locked = file_priv->master->lock.idle_has_lock;
367 spin_unlock_bh(&file_priv->master->lock.spinlock);
368 if (locked)
369 break;
370 schedule();
371 } while (!time_after_eq(jiffies, _end));
372
373 if (!locked) {
374 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
375 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
376 "\tI will go on reclaiming the buffers anyway.\n");
377 }
378
379 dev->driver->reclaim_buffers_locked(dev, file_priv);
380 drm_idlelock_release(&file_priv->master->lock);
381 }
382}
383
384static void drm_master_release(struct drm_device *dev, struct file *filp)
385{
386 struct drm_file *file_priv = filp->private_data;
387
388 if (dev->driver->reclaim_buffers_locked &&
389 file_priv->master->lock.hw_lock)
390 drm_reclaim_locked_buffers(dev, filp);
391
392 if (dev->driver->reclaim_buffers_idlelocked &&
393 file_priv->master->lock.hw_lock) {
394 drm_idlelock_take(&file_priv->master->lock);
395 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
396 drm_idlelock_release(&file_priv->master->lock);
397 }
398
399
400 if (drm_i_have_hw_lock(dev, file_priv)) {
401 DRM_DEBUG("File %p released, freeing lock for context %d\n",
402 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
403 drm_lock_free(&file_priv->master->lock,
404 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
405 }
406
407 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
408 !dev->driver->reclaim_buffers_locked) {
409 dev->driver->reclaim_buffers(dev, file_priv);
410 }
411}
412
1da177e4
LT
413/**
414 * Release file.
415 *
416 * \param inode device inode
6c340eac 417 * \param file_priv DRM file private.
1da177e4
LT
418 * \return zero on success or a negative number on failure.
419 *
420 * If the hardware lock is held then free it, and take it again for the kernel
421 * context since it's necessary to reclaim buffers. Unlink the file private
422 * data from its list and free it. Decreases the open count and if it reaches
22eae947 423 * zero calls drm_lastclose().
1da177e4 424 */
b5e89ed5 425int drm_release(struct inode *inode, struct file *filp)
1da177e4 426{
6c340eac 427 struct drm_file *file_priv = filp->private_data;
2c14f28b 428 struct drm_device *dev = file_priv->minor->dev;
1da177e4
LT
429 int retcode = 0;
430
431 lock_kernel();
1da177e4 432
b5e89ed5 433 DRM_DEBUG("open_count = %d\n", dev->open_count);
1da177e4 434
22eae947 435 if (dev->driver->preclose)
6c340eac 436 dev->driver->preclose(dev, file_priv);
1da177e4
LT
437
438 /* ========================================================
439 * Begin inline drm_release
440 */
441
b5e89ed5 442 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
ba25f9dc 443 task_pid_nr(current),
2c14f28b 444 (long)old_encode_dev(file_priv->minor->device),
b5e89ed5
DA
445 dev->open_count);
446
7c1c2871
DA
447 /* if the master has gone away we can't do anything with the lock */
448 if (file_priv->minor->master)
449 drm_master_release(dev, filp);
1da177e4 450
673a394b
EA
451 if (dev->driver->driver_features & DRIVER_GEM)
452 drm_gem_release(dev, file_priv);
453
30e2fb18 454 mutex_lock(&dev->ctxlist_mutex);
bd1b331f 455 if (!list_empty(&dev->ctxlist)) {
55910517 456 struct drm_ctx_list *pos, *n;
1da177e4 457
bd1b331f 458 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
6c340eac 459 if (pos->tag == file_priv &&
b5e89ed5 460 pos->handle != DRM_KERNEL_CONTEXT) {
1da177e4 461 if (dev->driver->context_dtor)
b5e89ed5
DA
462 dev->driver->context_dtor(dev,
463 pos->handle);
1da177e4 464
b5e89ed5 465 drm_ctxbitmap_free(dev, pos->handle);
1da177e4 466
b5e89ed5
DA
467 list_del(&pos->head);
468 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
1da177e4
LT
469 --dev->ctx_count;
470 }
471 }
472 }
30e2fb18 473 mutex_unlock(&dev->ctxlist_mutex);
1da177e4 474
30e2fb18 475 mutex_lock(&dev->struct_mutex);
7c1c2871
DA
476
477 if (file_priv->is_master) {
84b1fd10 478 struct drm_file *temp;
7c1c2871
DA
479 list_for_each_entry(temp, &dev->filelist, lhead) {
480 if ((temp->master == file_priv->master) &&
481 (temp != file_priv))
482 temp->authenticated = 0;
483 }
bd1b331f 484
7c1c2871
DA
485 if (file_priv->minor->master == file_priv->master) {
486 /* drop the reference held my the minor */
487 drm_master_put(&file_priv->minor->master);
488 }
1da177e4 489 }
7c1c2871
DA
490
491 /* drop the reference held my the file priv */
492 drm_master_put(&file_priv->master);
493 file_priv->is_master = 0;
6c340eac 494 list_del(&file_priv->lhead);
30e2fb18 495 mutex_unlock(&dev->struct_mutex);
b5e89ed5 496
22eae947 497 if (dev->driver->postclose)
6c340eac
EA
498 dev->driver->postclose(dev, file_priv);
499 drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
1da177e4
LT
500
501 /* ========================================================
502 * End inline drm_release
503 */
504
b5e89ed5
DA
505 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
506 spin_lock(&dev->count_lock);
507 if (!--dev->open_count) {
7c1c2871
DA
508 if (atomic_read(&dev->ioctl_count)) {
509 DRM_ERROR("Device busy: %d\n",
510 atomic_read(&dev->ioctl_count));
b5e89ed5 511 spin_unlock(&dev->count_lock);
1da177e4
LT
512 unlock_kernel();
513 return -EBUSY;
514 }
b5e89ed5 515 spin_unlock(&dev->count_lock);
1da177e4 516 unlock_kernel();
22eae947 517 return drm_lastclose(dev);
1da177e4 518 }
b5e89ed5 519 spin_unlock(&dev->count_lock);
1da177e4
LT
520
521 unlock_kernel();
522
523 return retcode;
524}
525EXPORT_SYMBOL(drm_release);
526
1da177e4
LT
527/** No-op. */
528unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
529{
530 return 0;
531}
b5e89ed5 532EXPORT_SYMBOL(drm_poll);