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(&drm_minors_xa, 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 get_pid(pid);
555 old = rcu_replace_pointer(filp->pid, pid, 1);
556 mutex_unlock(&dev->filelist_mutex);
557
558 synchronize_rcu();
559 put_pid(old);
560 #endif
561 }
562
563 /**
564 * drm_release_noglobal - release method for DRM file
565 * @inode: device inode
566 * @filp: file pointer.
567 *
568 * This function may be used by drivers as their &file_operations.release
569 * method. It frees any resources associated with the open file prior to taking
570 * the drm_global_mutex, which then calls the &drm_driver.postclose driver
571 * callback. If this is the last open file for the DRM device also proceeds to
572 * call the &drm_driver.lastclose driver callback.
573 *
574 * RETURNS:
575 *
576 * Always succeeds and returns 0.
577 */
drm_release_noglobal(struct inode * inode,struct file * filp)578 int drm_release_noglobal(struct inode *inode, struct file *filp)
579 {
580 STUB();
581 return -ENOSYS;
582 #ifdef notyet
583 struct drm_file *file_priv = filp->private_data;
584 struct drm_minor *minor = file_priv->minor;
585 struct drm_device *dev = minor->dev;
586
587 drm_close_helper(filp);
588
589 if (atomic_dec_and_mutex_lock(&dev->open_count, &drm_global_mutex)) {
590 drm_lastclose(dev);
591 mutex_unlock(&drm_global_mutex);
592 }
593
594 drm_minor_release(minor);
595
596 return 0;
597 #endif
598 }
599 EXPORT_SYMBOL(drm_release_noglobal);
600
601 /**
602 * drm_read - read method for DRM file
603 * @filp: file pointer
604 * @buffer: userspace destination pointer for the read
605 * @count: count in bytes to read
606 * @offset: offset to read
607 *
608 * This function must be used by drivers as their &file_operations.read
609 * method if they use DRM events for asynchronous signalling to userspace.
610 * Since events are used by the KMS API for vblank and page flip completion this
611 * means all modern display drivers must use it.
612 *
613 * @offset is ignored, DRM events are read like a pipe. Polling support is
614 * provided by drm_poll().
615 *
616 * This function will only ever read a full event. Therefore userspace must
617 * supply a big enough buffer to fit any event to ensure forward progress. Since
618 * the maximum event space is currently 4K it's recommended to just use that for
619 * safety.
620 *
621 * RETURNS:
622 *
623 * Number of bytes read (always aligned to full events, and can be 0) or a
624 * negative error code on failure.
625 */
drm_read(struct file * filp,char __user * buffer,size_t count,loff_t * offset)626 ssize_t drm_read(struct file *filp, char __user *buffer,
627 size_t count, loff_t *offset)
628 {
629 STUB();
630 return -ENOSYS;
631 #ifdef notyet
632 struct drm_file *file_priv = filp->private_data;
633 struct drm_device *dev = file_priv->minor->dev;
634 ssize_t ret;
635
636 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
637 if (ret)
638 return ret;
639
640 for (;;) {
641 struct drm_pending_event *e = NULL;
642
643 spin_lock_irq(&dev->event_lock);
644 if (!list_empty(&file_priv->event_list)) {
645 e = list_first_entry(&file_priv->event_list,
646 struct drm_pending_event, link);
647 file_priv->event_space += e->event->length;
648 list_del(&e->link);
649 }
650 spin_unlock_irq(&dev->event_lock);
651
652 if (e == NULL) {
653 if (ret)
654 break;
655
656 if (filp->f_flags & O_NONBLOCK) {
657 ret = -EAGAIN;
658 break;
659 }
660
661 mutex_unlock(&file_priv->event_read_lock);
662 ret = wait_event_interruptible(file_priv->event_wait,
663 !list_empty(&file_priv->event_list));
664 if (ret >= 0)
665 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
666 if (ret)
667 return ret;
668 } else {
669 unsigned length = e->event->length;
670
671 if (length > count - ret) {
672 put_back_event:
673 spin_lock_irq(&dev->event_lock);
674 file_priv->event_space -= length;
675 list_add(&e->link, &file_priv->event_list);
676 spin_unlock_irq(&dev->event_lock);
677 wake_up_interruptible_poll(&file_priv->event_wait,
678 EPOLLIN | EPOLLRDNORM);
679 break;
680 }
681
682 if (copy_to_user(buffer + ret, e->event, length)) {
683 if (ret == 0)
684 ret = -EFAULT;
685 goto put_back_event;
686 }
687
688 ret += length;
689 kfree(e);
690 }
691 }
692 mutex_unlock(&file_priv->event_read_lock);
693
694 return ret;
695 #endif
696 }
697 EXPORT_SYMBOL(drm_read);
698
699 #ifdef notyet
700 /**
701 * drm_poll - poll method for DRM file
702 * @filp: file pointer
703 * @wait: poll waiter table
704 *
705 * This function must be used by drivers as their &file_operations.read method
706 * if they use DRM events for asynchronous signalling to userspace. Since
707 * events are used by the KMS API for vblank and page flip completion this means
708 * all modern display drivers must use it.
709 *
710 * See also drm_read().
711 *
712 * RETURNS:
713 *
714 * Mask of POLL flags indicating the current status of the file.
715 */
drm_poll(struct file * filp,struct poll_table_struct * wait)716 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
717 {
718 struct drm_file *file_priv = filp->private_data;
719 __poll_t mask = 0;
720
721 poll_wait(filp, &file_priv->event_wait, wait);
722
723 if (!list_empty(&file_priv->event_list))
724 mask |= EPOLLIN | EPOLLRDNORM;
725
726 return mask;
727 }
728 EXPORT_SYMBOL(drm_poll);
729 #endif
730
731 /**
732 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
733 * @dev: DRM device
734 * @file_priv: DRM file private data
735 * @p: tracking structure for the pending event
736 * @e: actual event data to deliver to userspace
737 *
738 * This function prepares the passed in event for eventual delivery. If the event
739 * doesn't get delivered (because the IOCTL fails later on, before queuing up
740 * anything) then the even must be cancelled and freed using
741 * drm_event_cancel_free(). Successfully initialized events should be sent out
742 * using drm_send_event() or drm_send_event_locked() to signal completion of the
743 * asynchronous event to userspace.
744 *
745 * If callers embedded @p into a larger structure it must be allocated with
746 * kmalloc and @p must be the first member element.
747 *
748 * This is the locked version of drm_event_reserve_init() for callers which
749 * already hold &drm_device.event_lock.
750 *
751 * RETURNS:
752 *
753 * 0 on success or a negative error code on failure.
754 */
drm_event_reserve_init_locked(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)755 int drm_event_reserve_init_locked(struct drm_device *dev,
756 struct drm_file *file_priv,
757 struct drm_pending_event *p,
758 struct drm_event *e)
759 {
760 if (file_priv->event_space < e->length)
761 return -ENOMEM;
762
763 file_priv->event_space -= e->length;
764
765 p->event = e;
766 list_add(&p->pending_link, &file_priv->pending_event_list);
767 p->file_priv = file_priv;
768
769 return 0;
770 }
771 EXPORT_SYMBOL(drm_event_reserve_init_locked);
772
773 /**
774 * drm_event_reserve_init - init a DRM event and reserve space for it
775 * @dev: DRM device
776 * @file_priv: DRM file private data
777 * @p: tracking structure for the pending event
778 * @e: actual event data to deliver to userspace
779 *
780 * This function prepares the passed in event for eventual delivery. If the event
781 * doesn't get delivered (because the IOCTL fails later on, before queuing up
782 * anything) then the even must be cancelled and freed using
783 * drm_event_cancel_free(). Successfully initialized events should be sent out
784 * using drm_send_event() or drm_send_event_locked() to signal completion of the
785 * asynchronous event to userspace.
786 *
787 * If callers embedded @p into a larger structure it must be allocated with
788 * kmalloc and @p must be the first member element.
789 *
790 * Callers which already hold &drm_device.event_lock should use
791 * drm_event_reserve_init_locked() instead.
792 *
793 * RETURNS:
794 *
795 * 0 on success or a negative error code on failure.
796 */
drm_event_reserve_init(struct drm_device * dev,struct drm_file * file_priv,struct drm_pending_event * p,struct drm_event * e)797 int drm_event_reserve_init(struct drm_device *dev,
798 struct drm_file *file_priv,
799 struct drm_pending_event *p,
800 struct drm_event *e)
801 {
802 unsigned long flags;
803 int ret;
804
805 spin_lock_irqsave(&dev->event_lock, flags);
806 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
807 spin_unlock_irqrestore(&dev->event_lock, flags);
808
809 return ret;
810 }
811 EXPORT_SYMBOL(drm_event_reserve_init);
812
813 /**
814 * drm_event_cancel_free - free a DRM event and release its space
815 * @dev: DRM device
816 * @p: tracking structure for the pending event
817 *
818 * This function frees the event @p initialized with drm_event_reserve_init()
819 * and releases any allocated space. It is used to cancel an event when the
820 * nonblocking operation could not be submitted and needed to be aborted.
821 */
drm_event_cancel_free(struct drm_device * dev,struct drm_pending_event * p)822 void drm_event_cancel_free(struct drm_device *dev,
823 struct drm_pending_event *p)
824 {
825 unsigned long flags;
826
827 spin_lock_irqsave(&dev->event_lock, flags);
828 if (p->file_priv) {
829 p->file_priv->event_space += p->event->length;
830 list_del(&p->pending_link);
831 }
832 spin_unlock_irqrestore(&dev->event_lock, flags);
833
834 if (p->fence)
835 dma_fence_put(p->fence);
836
837 kfree(p);
838 }
839 EXPORT_SYMBOL(drm_event_cancel_free);
840
drm_send_event_helper(struct drm_device * dev,struct drm_pending_event * e,ktime_t timestamp)841 static void drm_send_event_helper(struct drm_device *dev,
842 struct drm_pending_event *e, ktime_t timestamp)
843 {
844 assert_spin_locked(&dev->event_lock);
845
846 if (e->completion) {
847 complete_all(e->completion);
848 e->completion_release(e->completion);
849 e->completion = NULL;
850 }
851
852 if (e->fence) {
853 if (timestamp)
854 dma_fence_signal_timestamp(e->fence, timestamp);
855 else
856 dma_fence_signal(e->fence);
857 dma_fence_put(e->fence);
858 }
859
860 if (!e->file_priv) {
861 kfree(e);
862 return;
863 }
864
865 list_del(&e->pending_link);
866 list_add_tail(&e->link,
867 &e->file_priv->event_list);
868 wake_up_interruptible_poll(&e->file_priv->event_wait,
869 EPOLLIN | EPOLLRDNORM);
870 #ifdef __OpenBSD__
871 selwakeup(&e->file_priv->rsel);
872 #endif
873 }
874
875 /**
876 * drm_send_event_timestamp_locked - send DRM event to file descriptor
877 * @dev: DRM device
878 * @e: DRM event to deliver
879 * @timestamp: timestamp to set for the fence event in kernel's CLOCK_MONOTONIC
880 * time domain
881 *
882 * This function sends the event @e, initialized with drm_event_reserve_init(),
883 * to its associated userspace DRM file. Callers must already hold
884 * &drm_device.event_lock.
885 *
886 * Note that the core will take care of unlinking and disarming events when the
887 * corresponding DRM file is closed. Drivers need not worry about whether the
888 * DRM file for this event still exists and can call this function upon
889 * completion of the asynchronous work unconditionally.
890 */
drm_send_event_timestamp_locked(struct drm_device * dev,struct drm_pending_event * e,ktime_t timestamp)891 void drm_send_event_timestamp_locked(struct drm_device *dev,
892 struct drm_pending_event *e, ktime_t timestamp)
893 {
894 drm_send_event_helper(dev, e, timestamp);
895 }
896 EXPORT_SYMBOL(drm_send_event_timestamp_locked);
897
898 /**
899 * drm_send_event_locked - send DRM event to file descriptor
900 * @dev: DRM device
901 * @e: DRM event to deliver
902 *
903 * This function sends the event @e, initialized with drm_event_reserve_init(),
904 * to its associated userspace DRM file. Callers must already hold
905 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
906 *
907 * Note that the core will take care of unlinking and disarming events when the
908 * corresponding DRM file is closed. Drivers need not worry about whether the
909 * DRM file for this event still exists and can call this function upon
910 * completion of the asynchronous work unconditionally.
911 */
drm_send_event_locked(struct drm_device * dev,struct drm_pending_event * e)912 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
913 {
914 drm_send_event_helper(dev, e, 0);
915 }
916 EXPORT_SYMBOL(drm_send_event_locked);
917
918 /**
919 * drm_send_event - send DRM event to file descriptor
920 * @dev: DRM device
921 * @e: DRM event to deliver
922 *
923 * This function sends the event @e, initialized with drm_event_reserve_init(),
924 * to its associated userspace DRM file. This function acquires
925 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
926 * hold this lock.
927 *
928 * Note that the core will take care of unlinking and disarming events when the
929 * corresponding DRM file is closed. Drivers need not worry about whether the
930 * DRM file for this event still exists and can call this function upon
931 * completion of the asynchronous work unconditionally.
932 */
drm_send_event(struct drm_device * dev,struct drm_pending_event * e)933 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
934 {
935 unsigned long irqflags;
936
937 spin_lock_irqsave(&dev->event_lock, irqflags);
938 drm_send_event_helper(dev, e, 0);
939 spin_unlock_irqrestore(&dev->event_lock, irqflags);
940 }
941 EXPORT_SYMBOL(drm_send_event);
942
print_size(struct drm_printer * p,const char * stat,const char * region,u64 sz)943 static void print_size(struct drm_printer *p, const char *stat,
944 const char *region, u64 sz)
945 {
946 const char *units[] = {"", " KiB", " MiB"};
947 unsigned u;
948
949 for (u = 0; u < ARRAY_SIZE(units) - 1; u++) {
950 if (sz < SZ_1K)
951 break;
952 sz = div_u64(sz, SZ_1K);
953 }
954
955 drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
956 }
957
958 /**
959 * drm_print_memory_stats - A helper to print memory stats
960 * @p: The printer to print output to
961 * @stats: The collected memory stats
962 * @supported_status: Bitmask of optional stats which are available
963 * @region: The memory region
964 *
965 */
drm_print_memory_stats(struct drm_printer * p,const struct drm_memory_stats * stats,enum drm_gem_object_status supported_status,const char * region)966 void drm_print_memory_stats(struct drm_printer *p,
967 const struct drm_memory_stats *stats,
968 enum drm_gem_object_status supported_status,
969 const char *region)
970 {
971 print_size(p, "total", region, stats->private + stats->shared);
972 print_size(p, "shared", region, stats->shared);
973 print_size(p, "active", region, stats->active);
974
975 if (supported_status & DRM_GEM_OBJECT_RESIDENT)
976 print_size(p, "resident", region, stats->resident);
977
978 if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
979 print_size(p, "purgeable", region, stats->purgeable);
980 }
981 EXPORT_SYMBOL(drm_print_memory_stats);
982
983 /**
984 * drm_show_memory_stats - Helper to collect and show standard fdinfo memory stats
985 * @p: the printer to print output to
986 * @file: the DRM file
987 *
988 * Helper to iterate over GEM objects with a handle allocated in the specified
989 * file.
990 */
drm_show_memory_stats(struct drm_printer * p,struct drm_file * file)991 void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
992 {
993 struct drm_gem_object *obj;
994 struct drm_memory_stats status = {};
995 enum drm_gem_object_status supported_status = 0;
996 int id;
997
998 spin_lock(&file->table_lock);
999 idr_for_each_entry (&file->object_idr, obj, id) {
1000 enum drm_gem_object_status s = 0;
1001
1002 if (obj->funcs && obj->funcs->status) {
1003 s = obj->funcs->status(obj);
1004 supported_status = DRM_GEM_OBJECT_RESIDENT |
1005 DRM_GEM_OBJECT_PURGEABLE;
1006 }
1007
1008 if (obj->handle_count > 1) {
1009 status.shared += obj->size;
1010 } else {
1011 status.private += obj->size;
1012 }
1013
1014 if (s & DRM_GEM_OBJECT_RESIDENT) {
1015 status.resident += obj->size;
1016 } else {
1017 /* If already purged or not yet backed by pages, don't
1018 * count it as purgeable:
1019 */
1020 s &= ~DRM_GEM_OBJECT_PURGEABLE;
1021 }
1022
1023 if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) {
1024 status.active += obj->size;
1025
1026 /* If still active, don't count as purgeable: */
1027 s &= ~DRM_GEM_OBJECT_PURGEABLE;
1028 }
1029
1030 if (s & DRM_GEM_OBJECT_PURGEABLE)
1031 status.purgeable += obj->size;
1032 }
1033 spin_unlock(&file->table_lock);
1034
1035 drm_print_memory_stats(p, &status, supported_status, "memory");
1036 }
1037 EXPORT_SYMBOL(drm_show_memory_stats);
1038
1039 /**
1040 * drm_show_fdinfo - helper for drm file fops
1041 * @m: output stream
1042 * @f: the device file instance
1043 *
1044 * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
1045 * process using the GPU. See also &drm_driver.show_fdinfo.
1046 *
1047 * For text output format description please see Documentation/gpu/drm-usage-stats.rst
1048 */
drm_show_fdinfo(struct seq_file * m,struct file * f)1049 void drm_show_fdinfo(struct seq_file *m, struct file *f)
1050 {
1051 STUB();
1052 #ifdef notyet
1053 struct drm_file *file = f->private_data;
1054 struct drm_device *dev = file->minor->dev;
1055 struct drm_printer p = drm_seq_file_printer(m);
1056
1057 drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
1058 drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
1059
1060 if (dev_is_pci(dev->dev)) {
1061 struct pci_dev *pdev = to_pci_dev(dev->dev);
1062
1063 drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
1064 pci_domain_nr(pdev->bus), pdev->bus->number,
1065 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1066 }
1067
1068 if (dev->driver->show_fdinfo)
1069 dev->driver->show_fdinfo(&p, file);
1070 #endif
1071 }
1072 EXPORT_SYMBOL(drm_show_fdinfo);
1073
1074 /**
1075 * mock_drm_getfile - Create a new struct file for the drm device
1076 * @minor: drm minor to wrap (e.g. #drm_device.primary)
1077 * @flags: file creation mode (O_RDWR etc)
1078 *
1079 * This create a new struct file that wraps a DRM file context around a
1080 * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
1081 * invoking userspace. The struct file may be operated on using its f_op
1082 * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
1083 * to userspace facing functions as an internal/anonymous client.
1084 *
1085 * RETURNS:
1086 * Pointer to newly created struct file, ERR_PTR on failure.
1087 */
mock_drm_getfile(struct drm_minor * minor,unsigned int flags)1088 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
1089 {
1090 STUB();
1091 return ERR_PTR(-ENOSYS);
1092 #ifdef notyet
1093 struct drm_device *dev = minor->dev;
1094 struct drm_file *priv;
1095 struct file *file;
1096
1097 priv = drm_file_alloc(minor);
1098 if (IS_ERR(priv))
1099 return ERR_CAST(priv);
1100
1101 file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
1102 if (IS_ERR(file)) {
1103 drm_file_free(priv);
1104 return file;
1105 }
1106
1107 /* Everyone shares a single global address space */
1108 file->f_mapping = dev->anon_inode->i_mapping;
1109
1110 drm_dev_get(dev);
1111 priv->filp = file;
1112
1113 return file;
1114 #endif
1115 }
1116 EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);
1117