1 /*
2 * \author Rickard E. (Rik) Faith <faith@valinux.com>
3 * \author Daryll Strauss <daryll@valinux.com>
4 * \author Gareth Hughes <gareth@valinux.com>
5 */
6
7 /*
8 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
9 *
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
23 * Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 * OTHER DEALINGS IN THE SOFTWARE.
32 */
33
34 #include <linux/anon_inodes.h>
35 #include <linux/dma-fence.h>
36 #include <linux/file.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/poll.h>
40 #include <linux/slab.h>
41
42 #include <drm/drm_client.h>
43 #include <drm/drm_drv.h>
44 #include <drm/drm_file.h>
45 #include <drm/drm_gem.h>
46 #include <drm/drm_print.h>
47
48 #include "drm_crtc_internal.h"
49 #include "drm_internal.h"
50 #include "drm_legacy.h"
51
52 /* from BKL pushdown */
53 DEFINE_MUTEX(drm_global_mutex);
54
drm_dev_needs_global_mutex(struct drm_device * dev)55 bool drm_dev_needs_global_mutex(struct drm_device *dev)
56 {
57 /*
58 * Legacy drivers rely on all kinds of BKL locking semantics, don't
59 * bother. They also still need BKL locking for their ioctls, so better
60 * safe than sorry.
61 */
62 if (drm_core_check_feature(dev, DRIVER_LEGACY))
63 return true;
64
65 /*
66 * The deprecated ->load callback must be called after the driver is
67 * already registered. This means such drivers rely on the BKL to make
68 * sure an open can't proceed until the driver is actually fully set up.
69 * Similar hilarity holds for the unload callback.
70 */
71 if (dev->driver->load || dev->driver->unload)
72 return true;
73
74 /*
75 * Drivers with the lastclose callback assume that it's synchronized
76 * against concurrent opens, which again needs the BKL. The proper fix
77 * is to use the drm_client infrastructure with proper locking for each
78 * client.
79 */
80 if (dev->driver->lastclose)
81 return true;
82
83 return false;
84 }
85
86 /**
87 * DOC: file operations
88 *
89 * Drivers must define the file operations structure that forms the DRM
90 * userspace API entry point, even though most of those operations are
91 * implemented in the DRM core. The resulting &struct file_operations must be
92 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
93 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
94 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
95 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
96 * that require 32/64 bit compatibility support must provide their own
97 * &file_operations.compat_ioctl handler that processes private ioctls and calls
98 * drm_compat_ioctl() for core ioctls.
99 *
100 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
101 * events are a generic and extensible means to send asynchronous events to
102 * userspace through the file descriptor. They are used to send vblank event and
103 * page flip completions by the KMS API. But drivers can also use it for their
104 * own needs, e.g. to signal completion of rendering.
105 *
106 * For the driver-side event interface see drm_event_reserve_init() and
107 * drm_send_event() as the main starting points.
108 *
109 * The memory mapping implementation will vary depending on how the driver
110 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
111 * function, modern drivers should use one of the provided memory-manager
112 * specific implementations. For GEM-based drivers this is drm_gem_mmap().
113 *
114 * No other file operations are supported by the DRM userspace API. Overall the
115 * following is an example &file_operations structure::
116 *
117 * static const example_drm_fops = {
118 * .owner = THIS_MODULE,
119 * .open = drm_open,
120 * .release = drm_release,
121 * .unlocked_ioctl = drm_ioctl,
122 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
123 * .poll = drm_poll,
124 * .read = drm_read,
125 * .llseek = no_llseek,
126 * .mmap = drm_gem_mmap,
127 * };
128 *
129 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
130 * DMA based drivers there is the DEFINE_DRM_GEM_DMA_FOPS() macro to make this
131 * simpler.
132 *
133 * The driver's &file_operations must be stored in &drm_driver.fops.
134 *
135 * For driver-private IOCTL handling see the more detailed discussion in
136 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
137 */
138
139 /**
140 * drm_file_alloc - allocate file context
141 * @minor: minor to allocate on
142 *
143 * This allocates a new DRM file context. It is not linked into any context and
144 * can be used by the caller freely. Note that the context keeps a pointer to
145 * @minor, so it must be freed before @minor is.
146 *
147 * RETURNS:
148 * Pointer to newly allocated context, ERR_PTR on failure.
149 */
drm_file_alloc(struct drm_minor * minor)150 struct drm_file *drm_file_alloc(struct drm_minor *minor)
151 {
152 static atomic64_t ident = ATOMIC64_INIT(0);
153 struct drm_device *dev = minor->dev;
154 struct drm_file *file;
155 int ret;
156
157 file = kzalloc(sizeof(*file), GFP_KERNEL);
158 if (!file)
159 return ERR_PTR(-ENOMEM);
160
161 /* Get a unique identifier for fdinfo: */
162 file->client_id = atomic64_inc_return(&ident);
163 #ifdef __linux__
164 rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
165 #endif
166 file->minor = minor;
167
168 /* for compatibility root is always authenticated */
169 file->authenticated = capable(CAP_SYS_ADMIN);
170
171 INIT_LIST_HEAD(&file->lhead);
172 INIT_LIST_HEAD(&file->fbs);
173 rw_init(&file->fbs_lock, "fbslk");
174 INIT_LIST_HEAD(&file->blobs);
175 INIT_LIST_HEAD(&file->pending_event_list);
176 INIT_LIST_HEAD(&file->event_list);
177 init_waitqueue_head(&file->event_wait);
178 file->event_space = 4096; /* set aside 4k for event buffer */
179
180 mtx_init(&file->master_lookup_lock, IPL_NONE);
181 rw_init(&file->event_read_lock, "evread");
182
183 if (drm_core_check_feature(dev, DRIVER_GEM))
184 drm_gem_open(dev, file);
185
186 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
187 drm_syncobj_open(file);
188
189 drm_prime_init_file_private(&file->prime);
190
191 if (dev->driver->open) {
192 ret = dev->driver->open(dev, file);
193 if (ret < 0)
194 goto out_prime_destroy;
195 }
196
197 return file;
198
199 out_prime_destroy:
200 drm_prime_destroy_file_private(&file->prime);
201 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
202 drm_syncobj_release(file);
203 if (drm_core_check_feature(dev, DRIVER_GEM))
204 drm_gem_release(dev, file);
205 put_pid(rcu_access_pointer(file->pid));
206 kfree(file);
207
208 return ERR_PTR(ret);
209 }
210
drm_events_release(struct drm_file * file_priv)211 static void drm_events_release(struct drm_file *file_priv)
212 {
213 struct drm_device *dev = file_priv->minor->dev;
214 struct drm_pending_event *e, *et;
215 unsigned long flags;
216
217 spin_lock_irqsave(&dev->event_lock, flags);
218
219 /* Unlink pending events */
220 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
221 pending_link) {
222 list_del(&e->pending_link);
223 e->file_priv = NULL;
224 }
225
226 /* Remove unconsumed events */
227 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
228 list_del(&e->link);
229 kfree(e);
230 }
231
232 spin_unlock_irqrestore(&dev->event_lock, flags);
233 }
234
235 /**
236 * drm_file_free - free file context
237 * @file: context to free, or NULL
238 *
239 * This destroys and deallocates a DRM file context previously allocated via
240 * drm_file_alloc(). The caller must make sure to unlink it from any contexts
241 * before calling this.
242 *
243 * If NULL is passed, this is a no-op.
244 */
drm_file_free(struct drm_file * file)245 void drm_file_free(struct drm_file *file)
246 {
247 struct drm_device *dev;
248
249 if (!file)
250 return;
251
252 dev = file->minor->dev;
253
254 #ifdef __linux__
255 drm_dbg_core(dev, "comm=\"%s\", pid=%d, dev=0x%lx, open_count=%d\n",
256 current->comm, task_pid_nr(current),
257 (long)old_encode_dev(file->minor->kdev->devt),
258 atomic_read(&dev->open_count));
259 #else
260 drm_dbg_core(dev, "pid=%d, dev=0x%lx, open_count=%d\n",
261 curproc->p_p->ps_pid, (long)&dev->dev,
262 atomic_read(&dev->open_count));
263 #endif
264
265 #ifdef CONFIG_DRM_LEGACY
266 if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
267 dev->driver->preclose)
268 dev->driver->preclose(dev, file);
269 #endif
270
271 if (drm_core_check_feature(dev, DRIVER_LEGACY))
272 drm_legacy_lock_release(dev, file->filp);
273
274 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
275 drm_legacy_reclaim_buffers(dev, file);
276
277 drm_events_release(file);
278
279 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
280 drm_fb_release(file);
281 drm_property_destroy_user_blobs(dev, file);
282 }
283
284 if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
285 drm_syncobj_release(file);
286
287 if (drm_core_check_feature(dev, DRIVER_GEM))
288 drm_gem_release(dev, file);
289
290 drm_legacy_ctxbitmap_flush(dev, file);
291
292 if (drm_is_primary_client(file))
293 drm_master_release(file);
294
295 if (dev->driver->postclose)
296 dev->driver->postclose(dev, file);
297
298 drm_prime_destroy_file_private(&file->prime);
299
300 WARN_ON(!list_empty(&file->event_list));
301
302 put_pid(rcu_access_pointer(file->pid));
303 kfree(file);
304 }
305
306 #ifdef __linux__
307
drm_close_helper(struct file * filp)308 static void drm_close_helper(struct file *filp)
309 {
310 struct drm_file *file_priv = filp->private_data;
311 struct drm_device *dev = file_priv->minor->dev;
312
313 mutex_lock(&dev->filelist_mutex);
314 list_del(&file_priv->lhead);
315 mutex_unlock(&dev->filelist_mutex);
316
317 drm_file_free(file_priv);
318 }
319
320 /*
321 * Check whether DRI will run on this CPU.
322 *
323 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
324 */
drm_cpu_valid(void)325 static int drm_cpu_valid(void)
326 {
327 #if defined(__sparc__) && !defined(__sparc_v9__)
328 return 0; /* No cmpxchg before v9 sparc. */
329 #endif
330 return 1;
331 }
332
333 /*
334 * Called whenever a process opens a drm node
335 *
336 * \param filp file pointer.
337 * \param minor acquired minor-object.
338 * \return zero on success or a negative number on failure.
339 *
340 * Creates and initializes a drm_file structure for the file private data in \p
341 * filp and add it into the double linked list in \p dev.
342 */
drm_open_helper(struct file * filp,struct drm_minor * minor)343 int drm_open_helper(struct file *filp, struct drm_minor *minor)
344 {
345 struct drm_device *dev = minor->dev;
346 struct drm_file *priv;
347 int ret;
348
349 if (filp->f_flags & O_EXCL)
350 return -EBUSY; /* No exclusive opens */
351 if (!drm_cpu_valid())
352 return -EINVAL;
353 if (dev->switch_power_state != DRM_SWITCH_POWER_ON &&
354 dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
355 return -EINVAL;
356
357 drm_dbg_core(dev, "comm=\"%s\", pid=%d, minor=%d\n",
358 current->comm, task_pid_nr(current), minor->index);
359
360 priv = drm_file_alloc(minor);
361 if (IS_ERR(priv))
362 return PTR_ERR(priv);
363
364 if (drm_is_primary_client(priv)) {
365 ret = drm_master_open(priv);
366 if (ret) {
367 drm_file_free(priv);
368 return ret;
369 }
370 }
371
372 filp->private_data = priv;
373 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
374 priv->filp = filp;
375
376 mutex_lock(&dev->filelist_mutex);
377 list_add(&priv->lhead, &dev->filelist);
378 mutex_unlock(&dev->filelist_mutex);
379
380 #ifdef CONFIG_DRM_LEGACY
381 #ifdef __alpha__
382 /*
383 * Default the hose
384 */
385 if (!dev->hose) {
386 struct pci_dev *pci_dev;
387
388 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
389 if (pci_dev) {
390 dev->hose = pci_dev->sysdata;
391 pci_dev_put(pci_dev);
392 }
393 if (!dev->hose) {
394 struct pci_bus *b = list_entry(pci_root_buses.next,
395 struct pci_bus, node);
396 if (b)
397 dev->hose = b->sysdata;
398 }
399 }
400 #endif
401 #endif
402
403 return 0;
404 }
405
406 #endif /* __linux__ */
407
408 /**
409 * drm_open - open method for DRM file
410 * @inode: device inode
411 * @filp: file pointer.
412 *
413 * This function must be used by drivers as their &file_operations.open method.
414 * It looks up the correct DRM device and instantiates all the per-file
415 * resources for it. It also calls the &drm_driver.open driver callback.
416 *
417 * RETURNS:
418 *
419 * 0 on success or negative errno value on failure.
420 */
421 #ifdef __linux__
drm_open(struct inode * inode,struct file * filp)422 int drm_open(struct inode *inode, struct file *filp)
423 {
424 struct drm_device *dev;
425 struct drm_minor *minor;
426 int retcode;
427 int need_setup = 0;
428
429 minor = drm_minor_acquire(iminor(inode));
430 if (IS_ERR(minor))
431 return PTR_ERR(minor);
432
433 dev = minor->dev;
434 if (drm_dev_needs_global_mutex(dev))
435 mutex_lock(&drm_global_mutex);
436
437 if (!atomic_fetch_inc(&dev->open_count))
438 need_setup = 1;
439
440 /* share address_space across all char-devs of a single device */
441 filp->f_mapping = dev->anon_inode->i_mapping;
442
443 retcode = drm_open_helper(filp, minor);
444 if (retcode)
445 goto err_undo;
446 if (need_setup) {
447 retcode = drm_legacy_setup(dev);
448 if (retcode) {
449 drm_close_helper(filp);
450 goto err_undo;
451 }
452 }
453
454 if (drm_dev_needs_global_mutex(dev))
455 mutex_unlock(&drm_global_mutex);
456
457 return 0;
458
459 err_undo:
460 atomic_dec(&dev->open_count);
461 if (drm_dev_needs_global_mutex(dev))
462 mutex_unlock(&drm_global_mutex);
463 drm_minor_release(minor);
464 return retcode;
465 }
466 EXPORT_SYMBOL(drm_open);
467 #endif
468
drm_lastclose(struct drm_device * dev)469 void drm_lastclose(struct drm_device * dev)
470 {
471 drm_dbg_core(dev, "\n");
472
473 if (dev->driver->lastclose)
474 dev->driver->lastclose(dev);
475 drm_dbg_core(dev, "driver lastclose completed\n");
476
477 if (drm_core_check_feature(dev, DRIVER_LEGACY))
478 drm_legacy_dev_reinit(dev);
479
480 drm_client_dev_restore(dev);
481 }
482
483 /**
484 * drm_release - release method for DRM file
485 * @inode: device inode
486 * @filp: file pointer.
487 *
488 * This function must be used by drivers as their &file_operations.release
489 * method. It frees any resources associated with the open file, and calls the
490 * &drm_driver.postclose driver callback. If this is the last open file for the
491 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
492 *
493 * RETURNS:
494 *
495 * Always succeeds and returns 0.
496 */
drm_release(struct inode * inode,struct file * filp)497 int drm_release(struct inode *inode, struct file *filp)
498 {
499 STUB();
500 return -ENOSYS;
501 #ifdef notyet
502 struct drm_file *file_priv = filp->private_data;
503 struct drm_minor *minor = file_priv->minor;
504 struct drm_device *dev = minor->dev;
505
506 if (drm_dev_needs_global_mutex(dev))
507 mutex_lock(&drm_global_mutex);
508
509 drm_dbg_core(dev, "open_count = %d\n", atomic_read(&dev->open_count));
510
511 drm_close_helper(filp);
512
513 if (atomic_dec_and_test(&dev->open_count))
514 drm_lastclose(dev);
515
516 if (drm_dev_needs_global_mutex(dev))
517 mutex_unlock(&drm_global_mutex);
518
519 drm_minor_release(minor);
520
521 return 0;
522 #endif
523 }
524 EXPORT_SYMBOL(drm_release);
525
drm_file_update_pid(struct drm_file * filp)526 void drm_file_update_pid(struct drm_file *filp)
527 {
528 #ifdef notyet
529 struct drm_device *dev;
530 struct pid *pid, *old;
531 #endif
532
533 /*
534 * Master nodes need to keep the original ownership in order for
535 * drm_master_check_perm to keep working correctly. (See comment in
536 * drm_auth.c.)
537 */
538 if (filp->was_master)
539 return;
540
541 STUB();
542 #ifdef notyet
543 pid = task_tgid(current);
544
545 /*
546 * Quick unlocked check since the model is a single handover followed by
547 * exclusive repeated use.
548 */
549 if (pid == rcu_access_pointer(filp->pid))
550 return;
551
552 dev = filp->minor->dev;
553 mutex_lock(&dev->filelist_mutex);
554 old = rcu_replace_pointer(filp->pid, pid, 1);
555 mutex_unlock(&dev->filelist_mutex);
556
557 if (pid != old) {
558 get_pid(pid);
559 synchronize_rcu();
560 put_pid(old);
561 }
562 #endif
563 }
564
565 /**
566 * drm_release_noglobal - release method for DRM file
567 * @inode: device inode
568 * @filp: file pointer.
569 *
570 * This function may be used by drivers as their &file_operations.release
571 * method. It frees any resources associated with the open file prior to taking
572 * the drm_global_mutex, which then calls the &drm_driver.postclose driver
573 * callback. If this is the last open file for the DRM device also proceeds to
574 * call the &drm_driver.lastclose driver callback.
575 *
576 * RETURNS:
577 *
578 * Always succeeds and returns 0.
579 */
drm_release_noglobal(struct inode * inode,struct file * filp)580 int drm_release_noglobal(struct inode *inode, struct file *filp)
581 {
582 STUB();
583 return -ENOSYS;
584 #ifdef notyet
585 struct drm_file *file_priv = filp->private_data;
586 struct drm_minor *minor = file_priv->minor;
587 struct drm_device *dev = minor->dev;
588
589 drm_close_helper(filp);
590
591 if (atomic_dec_and_mutex_lock(&dev->open_count, &drm_global_mutex)) {
592 drm_lastclose(dev);
593 mutex_unlock(&drm_global_mutex);
594 }
595
596 drm_minor_release(minor);
597
598 return 0;
599 #endif
600 }
601 EXPORT_SYMBOL(drm_release_noglobal);
602
603 /**
604 * drm_read - read method for DRM file
605 * @filp: file pointer
606 * @buffer: userspace destination pointer for the read
607 * @count: count in bytes to read
608 * @offset: offset to read
609 *
610 * This function must be used by drivers as their &file_operations.read
611 * method if they use DRM events for asynchronous signalling to userspace.
612 * Since events are used by the KMS API for vblank and page flip completion this
613 * means all modern display drivers must use it.
614 *
615 * @offset is ignored, DRM events are read like a pipe. Polling support is
616 * provided by drm_poll().
617 *
618 * This function will only ever read a full event. Therefore userspace must
619 * supply a big enough buffer to fit any event to ensure forward progress. Since
620 * the maximum event space is currently 4K it's recommended to just use that for
621 * safety.
622 *
623 * RETURNS:
624 *
625 * Number of bytes read (always aligned to full events, and can be 0) or a
626 * negative error code on failure.
627 */
drm_read(struct file * filp,char __user * buffer,size_t count,loff_t * offset)628 ssize_t drm_read(struct file *filp, char __user *buffer,
629 size_t count, loff_t *offset)
630 {
631 STUB();
632 return -ENOSYS;
633 #ifdef notyet
634 struct drm_file *file_priv = filp->private_data;
635 struct drm_device *dev = file_priv->minor->dev;
636 ssize_t ret;
637
638 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
639 if (ret)
640 return ret;
641
642 for (;;) {
643 struct drm_pending_event *e = NULL;
644
645 spin_lock_irq(&dev->event_lock);
646 if (!list_empty(&file_priv->event_list)) {
647 e = list_first_entry(&file_priv->event_list,
648 struct drm_pending_event, link);
649 file_priv->event_space += e->event->length;
650 list_del(&e->link);
651 }
652 spin_unlock_irq(&dev->event_lock);
653
654 if (e == NULL) {
655 if (ret)
656 break;
657
658 if (filp->f_flags & O_NONBLOCK) {
659 ret = -EAGAIN;
660 break;
661 }
662
663 mutex_unlock(&file_priv->event_read_lock);
664 ret = wait_event_interruptible(file_priv->event_wait,
665 !list_empty(&file_priv->event_list));
666 if (ret >= 0)
667 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
668 if (ret)
669 return ret;
670 } else {
671 unsigned length = e->event->length;
672
673 if (length > count - ret) {
674 put_back_event:
675 spin_lock_irq(&dev->event_lock);
676 file_priv->event_space -= length;
677 list_add(&e->link, &file_priv->event_list);
678 spin_unlock_irq(&dev->event_lock);
679 wake_up_interruptible_poll(&file_priv->event_wait,
680 EPOLLIN | EPOLLRDNORM);
681 break;
682 }
683
684 if (copy_to_user(buffer + ret, e->event, length)) {
685 if (ret == 0)
686 ret = -EFAULT;
687 goto put_back_event;
688 }
689
690 ret += length;
691 kfree(e);
692 }
693 }
694 mutex_unlock(&file_priv->event_read_lock);
695
696 return ret;
697 #endif
698 }
699 EXPORT_SYMBOL(drm_read);
700
701 #ifdef notyet
702 /**
703 * drm_poll - poll method for DRM file
704 * @filp: file pointer
705 * @wait: poll waiter table
706 *
707 * This function must be used by drivers as their &file_operations.read method
708 * if they use DRM events for asynchronous signalling to userspace. Since
709 * events are used by the KMS API for vblank and page flip completion this means
710 * all modern display drivers must use it.
711 *
712 * See also drm_read().
713 *
714 * RETURNS:
715 *
716 * Mask of POLL flags indicating the current status of the file.
717 */
drm_poll(struct file * filp,struct poll_table_struct * wait)718 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
719 {
720 struct drm_file *file_priv = filp->private_data;
721 __poll_t mask = 0;
722
723 poll_wait(filp, &file_priv->event_wait, wait);
724
725 if (!list_empty(&file_priv->event_list))
726 mask |= EPOLLIN | EPOLLRDNORM;
727
728 return mask;
729 }
730 EXPORT_SYMBOL(drm_poll);
731 #endif
732
733 /**
734 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
735 * @dev: DRM device
736 * @file_priv: DRM file private data
737 * @p: tracking structure for the pending event
738 * @e: actual event data to deliver to userspace
739 *
740 * This function prepares the passed in event for eventual delivery. If the event
741 * doesn't get delivered (because the IOCTL fails later on, before queuing up
742 * anything) then the even must be cancelled and freed using
743 * drm_event_cancel_free(). Successfully initialized events should be sent out
744 * using drm_send_event() or drm_send_event_locked() to signal completion of the
745 * asynchronous event to userspace.
746 *
747 * If callers embedded @p into a larger structure it must be allocated with
748 * kmalloc and @p must be the first member element.
749 *
750 * This is the locked version of drm_event_reserve_init() for callers which
751 * already hold &drm_device.event_lock.
752 *
753 * RETURNS:
754 *
755 * 0 on success or a negative error code on failure.
756 */
drm_event_reserve_init_locked(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)757 int drm_event_reserve_init_locked(struct drm_device *dev,
758 struct drm_file *file_priv,
759 struct drm_pending_event *p,
760 struct drm_event *e)
761 {
762 if (file_priv->event_space < e->length)
763 return -ENOMEM;
764
765 file_priv->event_space -= e->length;
766
767 p->event = e;
768 list_add(&p->pending_link, &file_priv->pending_event_list);
769 p->file_priv = file_priv;
770
771 return 0;
772 }
773 EXPORT_SYMBOL(drm_event_reserve_init_locked);
774
775 /**
776 * drm_event_reserve_init - init a DRM event and reserve space for it
777 * @dev: DRM device
778 * @file_priv: DRM file private data
779 * @p: tracking structure for the pending event
780 * @e: actual event data to deliver to userspace
781 *
782 * This function prepares the passed in event for eventual delivery. If the event
783 * doesn't get delivered (because the IOCTL fails later on, before queuing up
784 * anything) then the even must be cancelled and freed using
785 * drm_event_cancel_free(). Successfully initialized events should be sent out
786 * using drm_send_event() or drm_send_event_locked() to signal completion of the
787 * asynchronous event to userspace.
788 *
789 * If callers embedded @p into a larger structure it must be allocated with
790 * kmalloc and @p must be the first member element.
791 *
792 * Callers which already hold &drm_device.event_lock should use
793 * drm_event_reserve_init_locked() instead.
794 *
795 * RETURNS:
796 *
797 * 0 on success or a negative error code on failure.
798 */
drm_event_reserve_init(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)799 int drm_event_reserve_init(struct drm_device *dev,
800 struct drm_file *file_priv,
801 struct drm_pending_event *p,
802 struct drm_event *e)
803 {
804 unsigned long flags;
805 int ret;
806
807 spin_lock_irqsave(&dev->event_lock, flags);
808 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
809 spin_unlock_irqrestore(&dev->event_lock, flags);
810
811 return ret;
812 }
813 EXPORT_SYMBOL(drm_event_reserve_init);
814
815 /**
816 * drm_event_cancel_free - free a DRM event and release its space
817 * @dev: DRM device
818 * @p: tracking structure for the pending event
819 *
820 * This function frees the event @p initialized with drm_event_reserve_init()
821 * and releases any allocated space. It is used to cancel an event when the
822 * nonblocking operation could not be submitted and needed to be aborted.
823 */
drm_event_cancel_free(struct drm_device * dev,struct drm_pending_event * p)824 void drm_event_cancel_free(struct drm_device *dev,
825 struct drm_pending_event *p)
826 {
827 unsigned long flags;
828
829 spin_lock_irqsave(&dev->event_lock, flags);
830 if (p->file_priv) {
831 p->file_priv->event_space += p->event->length;
832 list_del(&p->pending_link);
833 }
834 spin_unlock_irqrestore(&dev->event_lock, flags);
835
836 if (p->fence)
837 dma_fence_put(p->fence);
838
839 kfree(p);
840 }
841 EXPORT_SYMBOL(drm_event_cancel_free);
842
drm_send_event_helper(struct drm_device * dev,struct drm_pending_event * e,ktime_t timestamp)843 static void drm_send_event_helper(struct drm_device *dev,
844 struct drm_pending_event *e, ktime_t timestamp)
845 {
846 assert_spin_locked(&dev->event_lock);
847
848 if (e->completion) {
849 complete_all(e->completion);
850 e->completion_release(e->completion);
851 e->completion = NULL;
852 }
853
854 if (e->fence) {
855 if (timestamp)
856 dma_fence_signal_timestamp(e->fence, timestamp);
857 else
858 dma_fence_signal(e->fence);
859 dma_fence_put(e->fence);
860 }
861
862 if (!e->file_priv) {
863 kfree(e);
864 return;
865 }
866
867 list_del(&e->pending_link);
868 list_add_tail(&e->link,
869 &e->file_priv->event_list);
870 wake_up_interruptible_poll(&e->file_priv->event_wait,
871 EPOLLIN | EPOLLRDNORM);
872 #ifdef __OpenBSD__
873 selwakeup(&e->file_priv->rsel);
874 #endif
875 }
876
877 /**
878 * drm_send_event_timestamp_locked - send DRM event to file descriptor
879 * @dev: DRM device
880 * @e: DRM event to deliver
881 * @timestamp: timestamp to set for the fence event in kernel's CLOCK_MONOTONIC
882 * time domain
883 *
884 * This function sends the event @e, initialized with drm_event_reserve_init(),
885 * to its associated userspace DRM file. Callers must already hold
886 * &drm_device.event_lock.
887 *
888 * Note that the core will take care of unlinking and disarming events when the
889 * corresponding DRM file is closed. Drivers need not worry about whether the
890 * DRM file for this event still exists and can call this function upon
891 * completion of the asynchronous work unconditionally.
892 */
drm_send_event_timestamp_locked(struct drm_device * dev,struct drm_pending_event * e,ktime_t timestamp)893 void drm_send_event_timestamp_locked(struct drm_device *dev,
894 struct drm_pending_event *e, ktime_t timestamp)
895 {
896 drm_send_event_helper(dev, e, timestamp);
897 }
898 EXPORT_SYMBOL(drm_send_event_timestamp_locked);
899
900 /**
901 * drm_send_event_locked - send DRM event to file descriptor
902 * @dev: DRM device
903 * @e: DRM event to deliver
904 *
905 * This function sends the event @e, initialized with drm_event_reserve_init(),
906 * to its associated userspace DRM file. Callers must already hold
907 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
908 *
909 * Note that the core will take care of unlinking and disarming events when the
910 * corresponding DRM file is closed. Drivers need not worry about whether the
911 * DRM file for this event still exists and can call this function upon
912 * completion of the asynchronous work unconditionally.
913 */
drm_send_event_locked(struct drm_device * dev,struct drm_pending_event * e)914 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
915 {
916 drm_send_event_helper(dev, e, 0);
917 }
918 EXPORT_SYMBOL(drm_send_event_locked);
919
920 /**
921 * drm_send_event - send DRM event to file descriptor
922 * @dev: DRM device
923 * @e: DRM event to deliver
924 *
925 * This function sends the event @e, initialized with drm_event_reserve_init(),
926 * to its associated userspace DRM file. This function acquires
927 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
928 * hold this lock.
929 *
930 * Note that the core will take care of unlinking and disarming events when the
931 * corresponding DRM file is closed. Drivers need not worry about whether the
932 * DRM file for this event still exists and can call this function upon
933 * completion of the asynchronous work unconditionally.
934 */
drm_send_event(struct drm_device * dev,struct drm_pending_event * e)935 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
936 {
937 unsigned long irqflags;
938
939 spin_lock_irqsave(&dev->event_lock, irqflags);
940 drm_send_event_helper(dev, e, 0);
941 spin_unlock_irqrestore(&dev->event_lock, irqflags);
942 }
943 EXPORT_SYMBOL(drm_send_event);
944
print_size(struct drm_printer * p,const char * stat,const char * region,u64 sz)945 static void print_size(struct drm_printer *p, const char *stat,
946 const char *region, u64 sz)
947 {
948 const char *units[] = {"", " KiB", " MiB"};
949 unsigned u;
950
951 for (u = 0; u < ARRAY_SIZE(units) - 1; u++) {
952 if (sz < SZ_1K)
953 break;
954 sz = div_u64(sz, SZ_1K);
955 }
956
957 drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
958 }
959
960 /**
961 * drm_print_memory_stats - A helper to print memory stats
962 * @p: The printer to print output to
963 * @stats: The collected memory stats
964 * @supported_status: Bitmask of optional stats which are available
965 * @region: The memory region
966 *
967 */
drm_print_memory_stats(struct drm_printer * p,const struct drm_memory_stats * stats,enum drm_gem_object_status supported_status,const char * region)968 void drm_print_memory_stats(struct drm_printer *p,
969 const struct drm_memory_stats *stats,
970 enum drm_gem_object_status supported_status,
971 const char *region)
972 {
973 print_size(p, "total", region, stats->private + stats->shared);
974 print_size(p, "shared", region, stats->shared);
975 print_size(p, "active", region, stats->active);
976
977 if (supported_status & DRM_GEM_OBJECT_RESIDENT)
978 print_size(p, "resident", region, stats->resident);
979
980 if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
981 print_size(p, "purgeable", region, stats->purgeable);
982 }
983 EXPORT_SYMBOL(drm_print_memory_stats);
984
985 /**
986 * drm_show_memory_stats - Helper to collect and show standard fdinfo memory stats
987 * @p: the printer to print output to
988 * @file: the DRM file
989 *
990 * Helper to iterate over GEM objects with a handle allocated in the specified
991 * file.
992 */
drm_show_memory_stats(struct drm_printer * p,struct drm_file * file)993 void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
994 {
995 struct drm_gem_object *obj;
996 struct drm_memory_stats status = {};
997 enum drm_gem_object_status supported_status = 0;
998 int id;
999
1000 spin_lock(&file->table_lock);
1001 idr_for_each_entry (&file->object_idr, obj, id) {
1002 enum drm_gem_object_status s = 0;
1003
1004 if (obj->funcs && obj->funcs->status) {
1005 s = obj->funcs->status(obj);
1006 supported_status = DRM_GEM_OBJECT_RESIDENT |
1007 DRM_GEM_OBJECT_PURGEABLE;
1008 }
1009
1010 if (obj->handle_count > 1) {
1011 status.shared += obj->size;
1012 } else {
1013 status.private += obj->size;
1014 }
1015
1016 if (s & DRM_GEM_OBJECT_RESIDENT) {
1017 status.resident += obj->size;
1018 } else {
1019 /* If already purged or not yet backed by pages, don't
1020 * count it as purgeable:
1021 */
1022 s &= ~DRM_GEM_OBJECT_PURGEABLE;
1023 }
1024
1025 if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) {
1026 status.active += obj->size;
1027
1028 /* If still active, don't count as purgeable: */
1029 s &= ~DRM_GEM_OBJECT_PURGEABLE;
1030 }
1031
1032 if (s & DRM_GEM_OBJECT_PURGEABLE)
1033 status.purgeable += obj->size;
1034 }
1035 spin_unlock(&file->table_lock);
1036
1037 drm_print_memory_stats(p, &status, supported_status, "memory");
1038 }
1039 EXPORT_SYMBOL(drm_show_memory_stats);
1040
1041 /**
1042 * drm_show_fdinfo - helper for drm file fops
1043 * @m: output stream
1044 * @f: the device file instance
1045 *
1046 * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
1047 * process using the GPU. See also &drm_driver.show_fdinfo.
1048 *
1049 * For text output format description please see Documentation/gpu/drm-usage-stats.rst
1050 */
drm_show_fdinfo(struct seq_file * m,struct file * f)1051 void drm_show_fdinfo(struct seq_file *m, struct file *f)
1052 {
1053 STUB();
1054 #ifdef notyet
1055 struct drm_file *file = f->private_data;
1056 struct drm_device *dev = file->minor->dev;
1057 struct drm_printer p = drm_seq_file_printer(m);
1058
1059 drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
1060 drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
1061
1062 if (dev_is_pci(dev->dev)) {
1063 struct pci_dev *pdev = to_pci_dev(dev->dev);
1064
1065 drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
1066 pci_domain_nr(pdev->bus), pdev->bus->number,
1067 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1068 }
1069
1070 if (dev->driver->show_fdinfo)
1071 dev->driver->show_fdinfo(&p, file);
1072 #endif
1073 }
1074 EXPORT_SYMBOL(drm_show_fdinfo);
1075
1076 /**
1077 * mock_drm_getfile - Create a new struct file for the drm device
1078 * @minor: drm minor to wrap (e.g. #drm_device.primary)
1079 * @flags: file creation mode (O_RDWR etc)
1080 *
1081 * This create a new struct file that wraps a DRM file context around a
1082 * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
1083 * invoking userspace. The struct file may be operated on using its f_op
1084 * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
1085 * to userspace facing functions as an internal/anonymous client.
1086 *
1087 * RETURNS:
1088 * Pointer to newly created struct file, ERR_PTR on failure.
1089 */
mock_drm_getfile(struct drm_minor * minor,unsigned int flags)1090 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
1091 {
1092 STUB();
1093 return ERR_PTR(-ENOSYS);
1094 #ifdef notyet
1095 struct drm_device *dev = minor->dev;
1096 struct drm_file *priv;
1097 struct file *file;
1098
1099 priv = drm_file_alloc(minor);
1100 if (IS_ERR(priv))
1101 return ERR_CAST(priv);
1102
1103 file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
1104 if (IS_ERR(file)) {
1105 drm_file_free(priv);
1106 return file;
1107 }
1108
1109 /* Everyone shares a single global address space */
1110 file->f_mapping = dev->anon_inode->i_mapping;
1111
1112 drm_dev_get(dev);
1113 priv->filp = file;
1114
1115 return file;
1116 #endif
1117 }
1118 EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);
1119