]>
Commit | Line | Data |
---|---|---|
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" | |
38 | #include <linux/poll.h> | |
5a0e3ad6 | 39 | #include <linux/slab.h> |
70ffa16e | 40 | #include <linux/smp_lock.h> |
1da177e4 | 41 | |
58374713 AB |
42 | /* from BKL pushdown: note that nothing else serializes idr_find() */ |
43 | DEFINE_MUTEX(drm_global_mutex); | |
e3461a2b | 44 | EXPORT_SYMBOL(drm_global_mutex); |
58374713 | 45 | |
b5e89ed5 | 46 | static int drm_open_helper(struct inode *inode, struct file *filp, |
84b1fd10 | 47 | struct drm_device * dev); |
c94f7029 | 48 | |
84b1fd10 | 49 | static int drm_setup(struct drm_device * dev) |
1da177e4 LT |
50 | { |
51 | int i; | |
52 | int ret; | |
53 | ||
22eae947 DA |
54 | if (dev->driver->firstopen) { |
55 | ret = dev->driver->firstopen(dev); | |
b5e89ed5 | 56 | if (ret != 0) |
1da177e4 LT |
57 | return ret; |
58 | } | |
59 | ||
b5e89ed5 DA |
60 | atomic_set(&dev->ioctl_count, 0); |
61 | atomic_set(&dev->vma_count, 0); | |
1da177e4 | 62 | |
f453ba04 DA |
63 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) && |
64 | !drm_core_check_feature(dev, DRIVER_MODESET)) { | |
65 | dev->buf_use = 0; | |
66 | atomic_set(&dev->buf_alloc, 0); | |
67 | ||
b5e89ed5 DA |
68 | i = drm_dma_setup(dev); |
69 | if (i < 0) | |
1da177e4 LT |
70 | return i; |
71 | } | |
72 | ||
3d77461e | 73 | for (i = 0; i < ARRAY_SIZE(dev->counts); i++) |
b5e89ed5 | 74 | atomic_set(&dev->counts[i], 0); |
1da177e4 | 75 | |
11d9c2fd | 76 | dev->sigdata.lock = NULL; |
7c1c2871 | 77 | |
1da177e4 LT |
78 | dev->queue_count = 0; |
79 | dev->queue_reserved = 0; | |
80 | dev->queue_slots = 0; | |
81 | dev->queuelist = NULL; | |
1da177e4 LT |
82 | dev->context_flag = 0; |
83 | dev->interrupt_flag = 0; | |
84 | dev->dma_flag = 0; | |
85 | dev->last_context = 0; | |
86 | dev->last_switch = 0; | |
87 | dev->last_checked = 0; | |
b5e89ed5 | 88 | init_waitqueue_head(&dev->context_wait); |
1da177e4 LT |
89 | dev->if_version = 0; |
90 | ||
91 | dev->ctx_start = 0; | |
92 | dev->lck_start = 0; | |
93 | ||
1da177e4 | 94 | dev->buf_async = NULL; |
b5e89ed5 DA |
95 | init_waitqueue_head(&dev->buf_readers); |
96 | init_waitqueue_head(&dev->buf_writers); | |
1da177e4 | 97 | |
b5e89ed5 | 98 | DRM_DEBUG("\n"); |
1da177e4 LT |
99 | |
100 | /* | |
101 | * The kernel's context could be created here, but is now created | |
b5e89ed5 | 102 | * in drm_dma_enqueue. This is more resource-efficient for |
1da177e4 LT |
103 | * hardware that does not do DMA, but may mean that |
104 | * drm_select_queue fails between the time the interrupt is | |
105 | * initialized and the time the queues are initialized. | |
106 | */ | |
1da177e4 LT |
107 | |
108 | return 0; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Open file. | |
b5e89ed5 | 113 | * |
1da177e4 LT |
114 | * \param inode device inode |
115 | * \param filp file pointer. | |
116 | * \return zero on success or a negative number on failure. | |
117 | * | |
118 | * Searches the DRM device with the same minor number, calls open_helper(), and | |
119 | * increments the device open count. If the open count was previous at zero, | |
120 | * i.e., it's the first that the device is open, then calls setup(). | |
121 | */ | |
b5e89ed5 | 122 | int drm_open(struct inode *inode, struct file *filp) |
1da177e4 | 123 | { |
84b1fd10 | 124 | struct drm_device *dev = NULL; |
2c14f28b DA |
125 | int minor_id = iminor(inode); |
126 | struct drm_minor *minor; | |
1da177e4 LT |
127 | int retcode = 0; |
128 | ||
2c14f28b DA |
129 | minor = idr_find(&drm_minors_idr, minor_id); |
130 | if (!minor) | |
1da177e4 | 131 | return -ENODEV; |
b5e89ed5 | 132 | |
2c14f28b | 133 | if (!(dev = minor->dev)) |
1da177e4 | 134 | return -ENODEV; |
b5e89ed5 DA |
135 | |
136 | retcode = drm_open_helper(inode, filp, dev); | |
137 | if (!retcode) { | |
138 | atomic_inc(&dev->counts[_DRM_STAT_OPENS]); | |
139 | spin_lock(&dev->count_lock); | |
140 | if (!dev->open_count++) { | |
141 | spin_unlock(&dev->count_lock); | |
a2c0a97b JB |
142 | retcode = drm_setup(dev); |
143 | goto out; | |
1da177e4 | 144 | } |
b5e89ed5 | 145 | spin_unlock(&dev->count_lock); |
1da177e4 | 146 | } |
a2c0a97b | 147 | out: |
da584058 CW |
148 | if (!retcode) { |
149 | mutex_lock(&dev->struct_mutex); | |
150 | if (minor->type == DRM_MINOR_LEGACY) { | |
151 | if (dev->dev_mapping == NULL) | |
152 | dev->dev_mapping = inode->i_mapping; | |
153 | else if (dev->dev_mapping != inode->i_mapping) | |
154 | retcode = -ENODEV; | |
155 | } | |
156 | mutex_unlock(&dev->struct_mutex); | |
f453ba04 | 157 | } |
a2c0a97b | 158 | |
1da177e4 LT |
159 | return retcode; |
160 | } | |
161 | EXPORT_SYMBOL(drm_open); | |
162 | ||
d985c108 DA |
163 | /** |
164 | * File \c open operation. | |
165 | * | |
166 | * \param inode device inode. | |
167 | * \param filp file pointer. | |
168 | * | |
169 | * Puts the dev->fops corresponding to the device minor number into | |
170 | * \p filp, call the \c open method, and restore the file operations. | |
171 | */ | |
172 | int drm_stub_open(struct inode *inode, struct file *filp) | |
173 | { | |
84b1fd10 | 174 | struct drm_device *dev = NULL; |
2c14f28b DA |
175 | struct drm_minor *minor; |
176 | int minor_id = iminor(inode); | |
d985c108 | 177 | int err = -ENODEV; |
99ac48f5 | 178 | const struct file_operations *old_fops; |
d985c108 DA |
179 | |
180 | DRM_DEBUG("\n"); | |
181 | ||
58374713 | 182 | mutex_lock(&drm_global_mutex); |
2c14f28b DA |
183 | minor = idr_find(&drm_minors_idr, minor_id); |
184 | if (!minor) | |
70ffa16e | 185 | goto out; |
d985c108 | 186 | |
2c14f28b | 187 | if (!(dev = minor->dev)) |
70ffa16e | 188 | goto out; |
d985c108 DA |
189 | |
190 | old_fops = filp->f_op; | |
191 | filp->f_op = fops_get(&dev->driver->fops); | |
f41ced8f LP |
192 | if (filp->f_op == NULL) { |
193 | filp->f_op = old_fops; | |
194 | goto out; | |
195 | } | |
d985c108 DA |
196 | if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) { |
197 | fops_put(filp->f_op); | |
198 | filp->f_op = fops_get(old_fops); | |
199 | } | |
200 | fops_put(old_fops); | |
201 | ||
70ffa16e | 202 | out: |
58374713 | 203 | mutex_unlock(&drm_global_mutex); |
d985c108 DA |
204 | return err; |
205 | } | |
206 | ||
207 | /** | |
208 | * Check whether DRI will run on this CPU. | |
209 | * | |
210 | * \return non-zero if the DRI will run on this CPU, or zero otherwise. | |
211 | */ | |
212 | static int drm_cpu_valid(void) | |
213 | { | |
214 | #if defined(__i386__) | |
215 | if (boot_cpu_data.x86 == 3) | |
216 | return 0; /* No cmpxchg on a 386 */ | |
217 | #endif | |
218 | #if defined(__sparc__) && !defined(__sparc_v9__) | |
219 | return 0; /* No cmpxchg before v9 sparc. */ | |
220 | #endif | |
221 | return 1; | |
222 | } | |
223 | ||
224 | /** | |
225 | * Called whenever a process opens /dev/drm. | |
226 | * | |
227 | * \param inode device inode. | |
228 | * \param filp file pointer. | |
229 | * \param dev device. | |
230 | * \return zero on success or a negative number on failure. | |
231 | * | |
232 | * Creates and initializes a drm_file structure for the file private data in \p | |
233 | * filp and add it into the double linked list in \p dev. | |
234 | */ | |
235 | static int drm_open_helper(struct inode *inode, struct file *filp, | |
84b1fd10 | 236 | struct drm_device * dev) |
d985c108 | 237 | { |
2c14f28b | 238 | int minor_id = iminor(inode); |
84b1fd10 | 239 | struct drm_file *priv; |
d985c108 DA |
240 | int ret; |
241 | ||
242 | if (filp->f_flags & O_EXCL) | |
243 | return -EBUSY; /* No exclusive opens */ | |
244 | if (!drm_cpu_valid()) | |
245 | return -EINVAL; | |
246 | ||
2c14f28b | 247 | DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id); |
d985c108 | 248 | |
6ebc22e6 | 249 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
d985c108 DA |
250 | if (!priv) |
251 | return -ENOMEM; | |
252 | ||
d985c108 | 253 | filp->private_data = priv; |
6c340eac | 254 | priv->filp = filp; |
2df68b43 | 255 | priv->uid = current_euid(); |
ba25f9dc | 256 | priv->pid = task_pid_nr(current); |
2c14f28b | 257 | priv->minor = idr_find(&drm_minors_idr, minor_id); |
d985c108 DA |
258 | priv->ioctl_count = 0; |
259 | /* for compatibility root is always authenticated */ | |
260 | priv->authenticated = capable(CAP_SYS_ADMIN); | |
261 | priv->lock_count = 0; | |
262 | ||
bd1b331f | 263 | INIT_LIST_HEAD(&priv->lhead); |
f453ba04 | 264 | INIT_LIST_HEAD(&priv->fbs); |
c9a9c5e0 KH |
265 | INIT_LIST_HEAD(&priv->event_list); |
266 | init_waitqueue_head(&priv->event_wait); | |
267 | priv->event_space = 4096; /* set aside 4k for event buffer */ | |
bd1b331f | 268 | |
673a394b EA |
269 | if (dev->driver->driver_features & DRIVER_GEM) |
270 | drm_gem_open(dev, priv); | |
271 | ||
d985c108 DA |
272 | if (dev->driver->open) { |
273 | ret = dev->driver->open(dev, priv); | |
274 | if (ret < 0) | |
275 | goto out_free; | |
276 | } | |
277 | ||
7c1c2871 DA |
278 | |
279 | /* if there is no current master make this fd it */ | |
30e2fb18 | 280 | mutex_lock(&dev->struct_mutex); |
7c1c2871 DA |
281 | if (!priv->minor->master) { |
282 | /* create a new master */ | |
283 | priv->minor->master = drm_master_create(priv->minor); | |
284 | if (!priv->minor->master) { | |
dba5ed0c | 285 | mutex_unlock(&dev->struct_mutex); |
7c1c2871 DA |
286 | ret = -ENOMEM; |
287 | goto out_free; | |
288 | } | |
289 | ||
290 | priv->is_master = 1; | |
291 | /* take another reference for the copy in the local file priv */ | |
292 | priv->master = drm_master_get(priv->minor->master); | |
293 | ||
294 | priv->authenticated = 1; | |
295 | ||
296 | mutex_unlock(&dev->struct_mutex); | |
297 | if (dev->driver->master_create) { | |
298 | ret = dev->driver->master_create(dev, priv->master); | |
299 | if (ret) { | |
300 | mutex_lock(&dev->struct_mutex); | |
301 | /* drop both references if this fails */ | |
302 | drm_master_put(&priv->minor->master); | |
303 | drm_master_put(&priv->master); | |
304 | mutex_unlock(&dev->struct_mutex); | |
305 | goto out_free; | |
306 | } | |
307 | } | |
862302ff TH |
308 | mutex_lock(&dev->struct_mutex); |
309 | if (dev->driver->master_set) { | |
310 | ret = dev->driver->master_set(dev, priv, true); | |
311 | if (ret) { | |
312 | /* drop both references if this fails */ | |
313 | drm_master_put(&priv->minor->master); | |
314 | drm_master_put(&priv->master); | |
315 | mutex_unlock(&dev->struct_mutex); | |
316 | goto out_free; | |
317 | } | |
318 | } | |
319 | mutex_unlock(&dev->struct_mutex); | |
7c1c2871 DA |
320 | } else { |
321 | /* get a reference to the master */ | |
322 | priv->master = drm_master_get(priv->minor->master); | |
323 | mutex_unlock(&dev->struct_mutex); | |
324 | } | |
bd1b331f | 325 | |
7c1c2871 | 326 | mutex_lock(&dev->struct_mutex); |
bd1b331f | 327 | list_add(&priv->lhead, &dev->filelist); |
30e2fb18 | 328 | mutex_unlock(&dev->struct_mutex); |
d985c108 DA |
329 | |
330 | #ifdef __alpha__ | |
331 | /* | |
332 | * Default the hose | |
333 | */ | |
334 | if (!dev->hose) { | |
335 | struct pci_dev *pci_dev; | |
336 | pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL); | |
337 | if (pci_dev) { | |
338 | dev->hose = pci_dev->sysdata; | |
339 | pci_dev_put(pci_dev); | |
340 | } | |
341 | if (!dev->hose) { | |
342 | struct pci_bus *b = pci_bus_b(pci_root_buses.next); | |
343 | if (b) | |
344 | dev->hose = b->sysdata; | |
345 | } | |
346 | } | |
347 | #endif | |
348 | ||
349 | return 0; | |
350 | out_free: | |
9a298b2a | 351 | kfree(priv); |
d985c108 DA |
352 | filp->private_data = NULL; |
353 | return ret; | |
354 | } | |
355 | ||
356 | /** No-op. */ | |
357 | int drm_fasync(int fd, struct file *filp, int on) | |
358 | { | |
84b1fd10 | 359 | struct drm_file *priv = filp->private_data; |
2c14f28b | 360 | struct drm_device *dev = priv->minor->dev; |
d985c108 DA |
361 | |
362 | DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, | |
2c14f28b | 363 | (long)old_encode_dev(priv->minor->device)); |
60aa4924 | 364 | return fasync_helper(fd, filp, on, &dev->buf_async); |
d985c108 DA |
365 | } |
366 | EXPORT_SYMBOL(drm_fasync); | |
367 | ||
7c1c2871 DA |
368 | /* |
369 | * Reclaim locked buffers; note that this may be a bad idea if the current | |
370 | * context doesn't have the hw lock... | |
371 | */ | |
372 | static void drm_reclaim_locked_buffers(struct drm_device *dev, struct file *f) | |
373 | { | |
374 | struct drm_file *file_priv = f->private_data; | |
375 | ||
376 | if (drm_i_have_hw_lock(dev, file_priv)) { | |
377 | dev->driver->reclaim_buffers_locked(dev, file_priv); | |
378 | } else { | |
379 | unsigned long _end = jiffies + 3 * DRM_HZ; | |
380 | int locked = 0; | |
381 | ||
382 | drm_idlelock_take(&file_priv->master->lock); | |
383 | ||
384 | /* | |
385 | * Wait for a while. | |
386 | */ | |
387 | do { | |
388 | spin_lock_bh(&file_priv->master->lock.spinlock); | |
389 | locked = file_priv->master->lock.idle_has_lock; | |
390 | spin_unlock_bh(&file_priv->master->lock.spinlock); | |
391 | if (locked) | |
392 | break; | |
393 | schedule(); | |
394 | } while (!time_after_eq(jiffies, _end)); | |
395 | ||
396 | if (!locked) { | |
397 | DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n" | |
398 | "\tdriver to use reclaim_buffers_idlelocked() instead.\n" | |
399 | "\tI will go on reclaiming the buffers anyway.\n"); | |
400 | } | |
401 | ||
402 | dev->driver->reclaim_buffers_locked(dev, file_priv); | |
403 | drm_idlelock_release(&file_priv->master->lock); | |
404 | } | |
405 | } | |
406 | ||
407 | static void drm_master_release(struct drm_device *dev, struct file *filp) | |
408 | { | |
409 | struct drm_file *file_priv = filp->private_data; | |
410 | ||
411 | if (dev->driver->reclaim_buffers_locked && | |
412 | file_priv->master->lock.hw_lock) | |
413 | drm_reclaim_locked_buffers(dev, filp); | |
414 | ||
415 | if (dev->driver->reclaim_buffers_idlelocked && | |
416 | file_priv->master->lock.hw_lock) { | |
417 | drm_idlelock_take(&file_priv->master->lock); | |
418 | dev->driver->reclaim_buffers_idlelocked(dev, file_priv); | |
419 | drm_idlelock_release(&file_priv->master->lock); | |
420 | } | |
421 | ||
422 | ||
423 | if (drm_i_have_hw_lock(dev, file_priv)) { | |
424 | DRM_DEBUG("File %p released, freeing lock for context %d\n", | |
425 | filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock)); | |
426 | drm_lock_free(&file_priv->master->lock, | |
427 | _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock)); | |
428 | } | |
429 | ||
430 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) && | |
431 | !dev->driver->reclaim_buffers_locked) { | |
432 | dev->driver->reclaim_buffers(dev, file_priv); | |
433 | } | |
434 | } | |
435 | ||
c9a9c5e0 KH |
436 | static void drm_events_release(struct drm_file *file_priv) |
437 | { | |
438 | struct drm_device *dev = file_priv->minor->dev; | |
439 | struct drm_pending_event *e, *et; | |
440 | struct drm_pending_vblank_event *v, *vt; | |
441 | unsigned long flags; | |
442 | ||
443 | spin_lock_irqsave(&dev->event_lock, flags); | |
444 | ||
445 | /* Remove pending flips */ | |
446 | list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link) | |
447 | if (v->base.file_priv == file_priv) { | |
448 | list_del(&v->base.link); | |
449 | drm_vblank_put(dev, v->pipe); | |
450 | v->base.destroy(&v->base); | |
451 | } | |
452 | ||
453 | /* Remove unconsumed events */ | |
454 | list_for_each_entry_safe(e, et, &file_priv->event_list, link) | |
455 | e->destroy(e); | |
456 | ||
457 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
458 | } | |
459 | ||
1da177e4 LT |
460 | /** |
461 | * Release file. | |
462 | * | |
463 | * \param inode device inode | |
6c340eac | 464 | * \param file_priv DRM file private. |
1da177e4 LT |
465 | * \return zero on success or a negative number on failure. |
466 | * | |
467 | * If the hardware lock is held then free it, and take it again for the kernel | |
468 | * context since it's necessary to reclaim buffers. Unlink the file private | |
469 | * data from its list and free it. Decreases the open count and if it reaches | |
22eae947 | 470 | * zero calls drm_lastclose(). |
1da177e4 | 471 | */ |
b5e89ed5 | 472 | int drm_release(struct inode *inode, struct file *filp) |
1da177e4 | 473 | { |
6c340eac | 474 | struct drm_file *file_priv = filp->private_data; |
2c14f28b | 475 | struct drm_device *dev = file_priv->minor->dev; |
1da177e4 LT |
476 | int retcode = 0; |
477 | ||
58374713 | 478 | mutex_lock(&drm_global_mutex); |
1da177e4 | 479 | |
b5e89ed5 | 480 | DRM_DEBUG("open_count = %d\n", dev->open_count); |
1da177e4 | 481 | |
22eae947 | 482 | if (dev->driver->preclose) |
6c340eac | 483 | dev->driver->preclose(dev, file_priv); |
1da177e4 LT |
484 | |
485 | /* ======================================================== | |
486 | * Begin inline drm_release | |
487 | */ | |
488 | ||
b5e89ed5 | 489 | DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", |
ba25f9dc | 490 | task_pid_nr(current), |
2c14f28b | 491 | (long)old_encode_dev(file_priv->minor->device), |
b5e89ed5 DA |
492 | dev->open_count); |
493 | ||
7c1c2871 DA |
494 | /* if the master has gone away we can't do anything with the lock */ |
495 | if (file_priv->minor->master) | |
496 | drm_master_release(dev, filp); | |
1da177e4 | 497 | |
c9a9c5e0 KH |
498 | drm_events_release(file_priv); |
499 | ||
673a394b EA |
500 | if (dev->driver->driver_features & DRIVER_GEM) |
501 | drm_gem_release(dev, file_priv); | |
502 | ||
ea39f835 KH |
503 | if (dev->driver->driver_features & DRIVER_MODESET) |
504 | drm_fb_release(file_priv); | |
505 | ||
30e2fb18 | 506 | mutex_lock(&dev->ctxlist_mutex); |
bd1b331f | 507 | if (!list_empty(&dev->ctxlist)) { |
55910517 | 508 | struct drm_ctx_list *pos, *n; |
1da177e4 | 509 | |
bd1b331f | 510 | list_for_each_entry_safe(pos, n, &dev->ctxlist, head) { |
6c340eac | 511 | if (pos->tag == file_priv && |
b5e89ed5 | 512 | pos->handle != DRM_KERNEL_CONTEXT) { |
1da177e4 | 513 | if (dev->driver->context_dtor) |
b5e89ed5 DA |
514 | dev->driver->context_dtor(dev, |
515 | pos->handle); | |
1da177e4 | 516 | |
b5e89ed5 | 517 | drm_ctxbitmap_free(dev, pos->handle); |
1da177e4 | 518 | |
b5e89ed5 | 519 | list_del(&pos->head); |
9a298b2a | 520 | kfree(pos); |
1da177e4 LT |
521 | --dev->ctx_count; |
522 | } | |
523 | } | |
524 | } | |
30e2fb18 | 525 | mutex_unlock(&dev->ctxlist_mutex); |
1da177e4 | 526 | |
30e2fb18 | 527 | mutex_lock(&dev->struct_mutex); |
7c1c2871 DA |
528 | |
529 | if (file_priv->is_master) { | |
fda714c2 | 530 | struct drm_master *master = file_priv->master; |
84b1fd10 | 531 | struct drm_file *temp; |
7c1c2871 DA |
532 | list_for_each_entry(temp, &dev->filelist, lhead) { |
533 | if ((temp->master == file_priv->master) && | |
534 | (temp != file_priv)) | |
535 | temp->authenticated = 0; | |
536 | } | |
bd1b331f | 537 | |
fda714c2 TH |
538 | /** |
539 | * Since the master is disappearing, so is the | |
540 | * possibility to lock. | |
541 | */ | |
542 | ||
543 | if (master->lock.hw_lock) { | |
544 | if (dev->sigdata.lock == master->lock.hw_lock) | |
545 | dev->sigdata.lock = NULL; | |
546 | master->lock.hw_lock = NULL; | |
547 | master->lock.file_priv = NULL; | |
548 | wake_up_interruptible_all(&master->lock.lock_queue); | |
549 | } | |
550 | ||
7c1c2871 DA |
551 | if (file_priv->minor->master == file_priv->master) { |
552 | /* drop the reference held my the minor */ | |
862302ff TH |
553 | if (dev->driver->master_drop) |
554 | dev->driver->master_drop(dev, file_priv, true); | |
7c1c2871 DA |
555 | drm_master_put(&file_priv->minor->master); |
556 | } | |
1da177e4 | 557 | } |
7c1c2871 DA |
558 | |
559 | /* drop the reference held my the file priv */ | |
560 | drm_master_put(&file_priv->master); | |
561 | file_priv->is_master = 0; | |
6c340eac | 562 | list_del(&file_priv->lhead); |
30e2fb18 | 563 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 564 | |
22eae947 | 565 | if (dev->driver->postclose) |
6c340eac | 566 | dev->driver->postclose(dev, file_priv); |
9a298b2a | 567 | kfree(file_priv); |
1da177e4 LT |
568 | |
569 | /* ======================================================== | |
570 | * End inline drm_release | |
571 | */ | |
572 | ||
b5e89ed5 DA |
573 | atomic_inc(&dev->counts[_DRM_STAT_CLOSES]); |
574 | spin_lock(&dev->count_lock); | |
575 | if (!--dev->open_count) { | |
7c1c2871 DA |
576 | if (atomic_read(&dev->ioctl_count)) { |
577 | DRM_ERROR("Device busy: %d\n", | |
578 | atomic_read(&dev->ioctl_count)); | |
58374713 AB |
579 | retcode = -EBUSY; |
580 | goto out; | |
1da177e4 | 581 | } |
58374713 | 582 | retcode = drm_lastclose(dev); |
1da177e4 | 583 | } |
58374713 | 584 | out: |
b5e89ed5 | 585 | spin_unlock(&dev->count_lock); |
58374713 | 586 | mutex_unlock(&drm_global_mutex); |
1da177e4 LT |
587 | |
588 | return retcode; | |
589 | } | |
590 | EXPORT_SYMBOL(drm_release); | |
591 | ||
c9a9c5e0 KH |
592 | static bool |
593 | drm_dequeue_event(struct drm_file *file_priv, | |
594 | size_t total, size_t max, struct drm_pending_event **out) | |
595 | { | |
596 | struct drm_device *dev = file_priv->minor->dev; | |
597 | struct drm_pending_event *e; | |
598 | unsigned long flags; | |
599 | bool ret = false; | |
600 | ||
601 | spin_lock_irqsave(&dev->event_lock, flags); | |
602 | ||
603 | *out = NULL; | |
604 | if (list_empty(&file_priv->event_list)) | |
605 | goto out; | |
606 | e = list_first_entry(&file_priv->event_list, | |
607 | struct drm_pending_event, link); | |
608 | if (e->event->length + total > max) | |
609 | goto out; | |
610 | ||
611 | file_priv->event_space += e->event->length; | |
612 | list_del(&e->link); | |
613 | *out = e; | |
614 | ret = true; | |
615 | ||
616 | out: | |
617 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
618 | return ret; | |
619 | } | |
620 | ||
621 | ssize_t drm_read(struct file *filp, char __user *buffer, | |
622 | size_t count, loff_t *offset) | |
623 | { | |
624 | struct drm_file *file_priv = filp->private_data; | |
625 | struct drm_pending_event *e; | |
626 | size_t total; | |
627 | ssize_t ret; | |
628 | ||
629 | ret = wait_event_interruptible(file_priv->event_wait, | |
630 | !list_empty(&file_priv->event_list)); | |
631 | if (ret < 0) | |
632 | return ret; | |
633 | ||
634 | total = 0; | |
635 | while (drm_dequeue_event(file_priv, total, count, &e)) { | |
636 | if (copy_to_user(buffer + total, | |
637 | e->event, e->event->length)) { | |
638 | total = -EFAULT; | |
639 | break; | |
640 | } | |
641 | ||
642 | total += e->event->length; | |
643 | e->destroy(e); | |
644 | } | |
645 | ||
646 | return total; | |
647 | } | |
648 | EXPORT_SYMBOL(drm_read); | |
649 | ||
1da177e4 LT |
650 | unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait) |
651 | { | |
c9a9c5e0 KH |
652 | struct drm_file *file_priv = filp->private_data; |
653 | unsigned int mask = 0; | |
654 | ||
655 | poll_wait(filp, &file_priv->event_wait, wait); | |
656 | ||
657 | if (!list_empty(&file_priv->event_list)) | |
658 | mask |= POLLIN | POLLRDNORM; | |
659 | ||
660 | return mask; | |
1da177e4 | 661 | } |
b5e89ed5 | 662 | EXPORT_SYMBOL(drm_poll); |