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