xref: /linux/include/drm/drm_file.h (revision fec2c3c01f1ca0cd2706941e78b9972e7f9474c0)
1 /*
2  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4  * Copyright (c) 2009-2010, Code Aurora Forum.
5  * All rights reserved.
6  *
7  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
8  * Author: Gareth Hughes <gareth@valinux.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice (including the next
18  * paragraph) shall be included in all copies or substantial portions of the
19  * Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 #ifndef _DRM_FILE_H_
31 #define _DRM_FILE_H_
32 
33 #include <linux/types.h>
34 #include <linux/completion.h>
35 #include <linux/idr.h>
36 #include <linux/xarray.h>
37 
38 #include <uapi/drm/drm.h>
39 
40 #include <drm/drm_prime.h>
41 
42 struct dma_fence;
43 struct drm_file;
44 struct drm_device;
45 struct drm_printer;
46 struct device;
47 struct file;
48 
49 extern struct xarray drm_minors_xa;
50 
51 /*
52  * FIXME: Not sure we want to have drm_minor here in the end, but to avoid
53  * header include loops we need it here for now.
54  */
55 
56 /* Note that the values of this enum are ABI (it determines
57  * /dev/dri/renderD* numbers).
58  *
59  * Setting DRM_MINOR_ACCEL to 32 gives enough space for more drm minors to
60  * be implemented before we hit any future
61  */
62 enum drm_minor_type {
63 	DRM_MINOR_PRIMARY = 0,
64 	DRM_MINOR_CONTROL = 1,
65 	DRM_MINOR_RENDER = 2,
66 	DRM_MINOR_ACCEL = 32,
67 };
68 
69 /**
70  * struct drm_minor - DRM device minor structure
71  *
72  * This structure represents a DRM minor number for device nodes in /dev.
73  * Entirely opaque to drivers and should never be inspected directly by drivers.
74  * Drivers instead should only interact with &struct drm_file and of course
75  * &struct drm_device, which is also where driver-private data and resources can
76  * be attached to.
77  */
78 struct drm_minor {
79 	/* private: */
80 	int index;			/* Minor device number */
81 	int type;                       /* Control or render or accel */
82 	struct device *kdev;		/* Linux device */
83 	struct drm_device *dev;
84 
85 	struct dentry *debugfs_symlink;
86 	struct dentry *debugfs_root;
87 };
88 
89 /**
90  * struct drm_pending_event - Event queued up for userspace to read
91  *
92  * This represents a DRM event. Drivers can use this as a generic completion
93  * mechanism, which supports kernel-internal &struct completion, &struct dma_fence
94  * and also the DRM-specific &struct drm_event delivery mechanism.
95  */
96 struct drm_pending_event {
97 	/**
98 	 * @completion:
99 	 *
100 	 * Optional pointer to a kernel internal completion signalled when
101 	 * drm_send_event() is called, useful to internally synchronize with
102 	 * nonblocking operations.
103 	 */
104 	struct completion *completion;
105 
106 	/**
107 	 * @completion_release:
108 	 *
109 	 * Optional callback currently only used by the atomic modeset helpers
110 	 * to clean up the reference count for the structure @completion is
111 	 * stored in.
112 	 */
113 	void (*completion_release)(struct completion *completion);
114 
115 	/**
116 	 * @event:
117 	 *
118 	 * Pointer to the actual event that should be sent to userspace to be
119 	 * read using drm_read(). Can be optional, since nowadays events are
120 	 * also used to signal kernel internal threads with @completion or DMA
121 	 * transactions using @fence.
122 	 */
123 	struct drm_event *event;
124 
125 	/**
126 	 * @fence:
127 	 *
128 	 * Optional DMA fence to unblock other hardware transactions which
129 	 * depend upon the nonblocking DRM operation this event represents.
130 	 */
131 	struct dma_fence *fence;
132 
133 	/**
134 	 * @file_priv:
135 	 *
136 	 * &struct drm_file where @event should be delivered to. Only set when
137 	 * @event is set.
138 	 */
139 	struct drm_file *file_priv;
140 
141 	/**
142 	 * @link:
143 	 *
144 	 * Double-linked list to keep track of this event. Can be used by the
145 	 * driver up to the point when it calls drm_send_event(), after that
146 	 * this list entry is owned by the core for its own book-keeping.
147 	 */
148 	struct list_head link;
149 
150 	/**
151 	 * @pending_link:
152 	 *
153 	 * Entry on &drm_file.pending_event_list, to keep track of all pending
154 	 * events for @file_priv, to allow correct unwinding of them when
155 	 * userspace closes the file before the event is delivered.
156 	 */
157 	struct list_head pending_link;
158 };
159 
160 /**
161  * struct drm_file - DRM file private data
162  *
163  * This structure tracks DRM state per open file descriptor.
164  */
165 struct drm_file {
166 	/**
167 	 * @authenticated:
168 	 *
169 	 * Whether the client is allowed to submit rendering, which for legacy
170 	 * nodes means it must be authenticated.
171 	 *
172 	 * See also the :ref:`section on primary nodes and authentication
173 	 * <drm_primary_node>`.
174 	 */
175 	bool authenticated;
176 
177 	/**
178 	 * @stereo_allowed:
179 	 *
180 	 * True when the client has asked us to expose stereo 3D mode flags.
181 	 */
182 	bool stereo_allowed;
183 
184 	/**
185 	 * @universal_planes:
186 	 *
187 	 * True if client understands CRTC primary planes and cursor planes
188 	 * in the plane list. Automatically set when @atomic is set.
189 	 */
190 	bool universal_planes;
191 
192 	/** @atomic: True if client understands atomic properties. */
193 	bool atomic;
194 
195 	/**
196 	 * @aspect_ratio_allowed:
197 	 *
198 	 * True, if client can handle picture aspect ratios, and has requested
199 	 * to pass this information along with the mode.
200 	 */
201 	bool aspect_ratio_allowed;
202 
203 	/**
204 	 * @writeback_connectors:
205 	 *
206 	 * True if client understands writeback connectors
207 	 */
208 	bool writeback_connectors;
209 
210 	/**
211 	 * @plane_color_pipeline:
212 	 *
213 	 * True if client understands plane color pipelines
214 	 */
215 	bool plane_color_pipeline;
216 
217 	/**
218 	 * @was_master:
219 	 *
220 	 * This client has or had, master capability. Protected by struct
221 	 * &drm_device.master_mutex.
222 	 *
223 	 * This is used to ensure that CAP_SYS_ADMIN is not enforced, if the
224 	 * client is or was master in the past.
225 	 */
226 	bool was_master;
227 
228 	/**
229 	 * @is_master:
230 	 *
231 	 * This client is the creator of @master. Protected by struct
232 	 * &drm_device.master_mutex.
233 	 *
234 	 * See also the :ref:`section on primary nodes and authentication
235 	 * <drm_primary_node>`.
236 	 */
237 	bool is_master;
238 
239 	/**
240 	 * @supports_virtualized_cursor_plane:
241 	 *
242 	 * This client is capable of handling the cursor plane with the
243 	 * restrictions imposed on it by the virtualized drivers.
244 	 *
245 	 * This implies that the cursor plane has to behave like a cursor
246 	 * i.e. track cursor movement. It also requires setting of the
247 	 * hotspot properties by the client on the cursor plane.
248 	 */
249 	bool supports_virtualized_cursor_plane;
250 
251 	/**
252 	 * @master:
253 	 *
254 	 * Master this node is currently associated with. Protected by struct
255 	 * &drm_device.master_mutex, and serialized by @master_lookup_lock.
256 	 *
257 	 * Only relevant if drm_is_primary_client() returns true. Note that
258 	 * this only matches &drm_device.master if the master is the currently
259 	 * active one.
260 	 *
261 	 * To update @master, both &drm_device.master_mutex and
262 	 * @master_lookup_lock need to be held, therefore holding either of
263 	 * them is safe and enough for the read side.
264 	 *
265 	 * When dereferencing this pointer, either hold struct
266 	 * &drm_device.master_mutex for the duration of the pointer's use, or
267 	 * use drm_file_get_master() if struct &drm_device.master_mutex is not
268 	 * currently held and there is no other need to hold it. This prevents
269 	 * @master from being freed during use.
270 	 *
271 	 * See also @authentication and @is_master and the :ref:`section on
272 	 * primary nodes and authentication <drm_primary_node>`.
273 	 */
274 	struct drm_master *master;
275 
276 	/** @master_lookup_lock: Serializes @master. */
277 	spinlock_t master_lookup_lock;
278 
279 	/**
280 	 * @pid: Process that is using this file.
281 	 *
282 	 * Must only be dereferenced under a rcu_read_lock or equivalent.
283 	 *
284 	 * Updates are guarded with dev->filelist_mutex and reference must be
285 	 * dropped after a RCU grace period to accommodate lockless readers.
286 	 */
287 	struct pid __rcu *pid;
288 
289 	/** @client_id: A unique id for fdinfo */
290 	u64 client_id;
291 
292 	/** @magic: Authentication magic, see @authenticated. */
293 	drm_magic_t magic;
294 
295 	/**
296 	 * @lhead:
297 	 *
298 	 * List of all open files of a DRM device, linked into
299 	 * &drm_device.filelist. Protected by &drm_device.filelist_mutex.
300 	 */
301 	struct list_head lhead;
302 
303 	/** @minor: &struct drm_minor for this file. */
304 	struct drm_minor *minor;
305 
306 	/**
307 	 * @object_idr:
308 	 *
309 	 * Mapping of mm object handles to object pointers. Used by the GEM
310 	 * subsystem. Protected by @table_lock.
311 	 *
312 	 * Note that allocated entries might be NULL as a transient state when
313 	 * creating or deleting a handle.
314 	 */
315 	struct idr object_idr;
316 
317 	/** @table_lock: Protects @object_idr. */
318 	spinlock_t table_lock;
319 
320 	/** @syncobj_xa: Mapping of sync object handles to object pointers. */
321 	struct xarray syncobj_xa;
322 
323 	/** @filp: Pointer to the core file structure. */
324 	struct file *filp;
325 
326 	/**
327 	 * @driver_priv:
328 	 *
329 	 * Optional pointer for driver private data. Can be allocated in
330 	 * &drm_driver.open and should be freed in &drm_driver.postclose.
331 	 */
332 	void *driver_priv;
333 
334 	/**
335 	 * @fbs:
336 	 *
337 	 * List of &struct drm_framebuffer associated with this file, using the
338 	 * &drm_framebuffer.filp_head entry.
339 	 *
340 	 * Protected by @fbs_lock. Note that the @fbs list holds a reference on
341 	 * the framebuffer object to prevent it from untimely disappearing.
342 	 */
343 	struct list_head fbs;
344 
345 	/** @fbs_lock: Protects @fbs. */
346 	struct mutex fbs_lock;
347 
348 	/**
349 	 * @blobs:
350 	 *
351 	 * User-created blob properties; this retains a reference on the
352 	 * property.
353 	 *
354 	 * Protected by @drm_mode_config.blob_lock;
355 	 */
356 	struct list_head blobs;
357 
358 	/** @event_wait: Waitqueue for new events added to @event_list. */
359 	wait_queue_head_t event_wait;
360 
361 	/**
362 	 * @pending_event_list:
363 	 *
364 	 * List of pending &struct drm_pending_event, used to clean up pending
365 	 * events in case this file gets closed before the event is signalled.
366 	 * Uses the &drm_pending_event.pending_link entry.
367 	 *
368 	 * Protect by &drm_device.event_lock.
369 	 */
370 	struct list_head pending_event_list;
371 
372 	/**
373 	 * @event_list:
374 	 *
375 	 * List of &struct drm_pending_event, ready for delivery to userspace
376 	 * through drm_read(). Uses the &drm_pending_event.link entry.
377 	 *
378 	 * Protect by &drm_device.event_lock.
379 	 */
380 	struct list_head event_list;
381 
382 	/**
383 	 * @event_space:
384 	 *
385 	 * Available event space to prevent userspace from
386 	 * exhausting kernel memory. Currently limited to the fairly arbitrary
387 	 * value of 4KB.
388 	 */
389 	int event_space;
390 
391 	/** @event_read_lock: Serializes drm_read(). */
392 	struct mutex event_read_lock;
393 
394 	/**
395 	 * @prime:
396 	 *
397 	 * Per-file buffer caches used by the PRIME buffer sharing code.
398 	 */
399 	struct drm_prime_file_private prime;
400 
401 	/**
402 	 * @client_name:
403 	 *
404 	 * Userspace-provided name; useful for accounting and debugging.
405 	 */
406 	const char *client_name;
407 
408 	/**
409 	 * @client_name_lock: Protects @client_name.
410 	 */
411 	struct mutex client_name_lock;
412 
413 	/**
414 	 * @debugfs_client:
415 	 *
416 	 * debugfs directory for each client under a drm node.
417 	 */
418 	struct dentry *debugfs_client;
419 };
420 
421 /**
422  * drm_is_primary_client - is this an open file of the primary node
423  * @file_priv: DRM file
424  *
425  * Returns true if this is an open file of the primary node, i.e.
426  * &drm_file.minor of @file_priv is a primary minor.
427  *
428  * See also the :ref:`section on primary nodes and authentication
429  * <drm_primary_node>`.
430  */
431 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
432 {
433 	return file_priv->minor->type == DRM_MINOR_PRIMARY;
434 }
435 
436 /**
437  * drm_is_render_client - is this an open file of the render node
438  * @file_priv: DRM file
439  *
440  * Returns true if this is an open file of the render node, i.e.
441  * &drm_file.minor of @file_priv is a render minor.
442  *
443  * See also the :ref:`section on render nodes <drm_render_node>`.
444  */
445 static inline bool drm_is_render_client(const struct drm_file *file_priv)
446 {
447 	return file_priv->minor->type == DRM_MINOR_RENDER;
448 }
449 
450 /**
451  * drm_is_accel_client - is this an open file of the compute acceleration node
452  * @file_priv: DRM file
453  *
454  * Returns true if this is an open file of the compute acceleration node, i.e.
455  * &drm_file.minor of @file_priv is a accel minor.
456  *
457  * See also :doc:`Introduction to compute accelerators subsystem
458  * </accel/introduction>`.
459  */
460 static inline bool drm_is_accel_client(const struct drm_file *file_priv)
461 {
462 	return file_priv->minor->type == DRM_MINOR_ACCEL;
463 }
464 
465 __printf(2, 3)
466 void drm_file_err(struct drm_file *file_priv, const char *fmt, ...);
467 
468 void drm_file_update_pid(struct drm_file *);
469 
470 struct drm_minor *drm_minor_acquire(struct xarray *minors_xa, unsigned int minor_id);
471 void drm_minor_release(struct drm_minor *minor);
472 
473 int drm_open(struct inode *inode, struct file *filp);
474 int drm_open_helper(struct file *filp, struct drm_minor *minor);
475 ssize_t drm_read(struct file *filp, char __user *buffer,
476 		 size_t count, loff_t *offset);
477 int drm_release(struct inode *inode, struct file *filp);
478 int drm_release_noglobal(struct inode *inode, struct file *filp);
479 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait);
480 int drm_event_reserve_init_locked(struct drm_device *dev,
481 				  struct drm_file *file_priv,
482 				  struct drm_pending_event *p,
483 				  struct drm_event *e);
484 int drm_event_reserve_init(struct drm_device *dev,
485 			   struct drm_file *file_priv,
486 			   struct drm_pending_event *p,
487 			   struct drm_event *e);
488 void drm_event_cancel_free(struct drm_device *dev,
489 			   struct drm_pending_event *p);
490 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);
491 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
492 void drm_send_event_timestamp_locked(struct drm_device *dev,
493 				     struct drm_pending_event *e,
494 				     ktime_t timestamp);
495 
496 /**
497  * struct drm_memory_stats - GEM object stats associated
498  * @shared: Total size of GEM objects shared between processes
499  * @private: Total size of GEM objects
500  * @resident: Total size of GEM objects backing pages
501  * @purgeable: Total size of GEM objects that can be purged (resident and not active)
502  * @active: Total size of GEM objects active on one or more engines
503  *
504  * Used by drm_print_memory_stats()
505  */
506 struct drm_memory_stats {
507 	u64 shared;
508 	u64 private;
509 	u64 resident;
510 	u64 purgeable;
511 	u64 active;
512 };
513 
514 enum drm_gem_object_status;
515 
516 int drm_memory_stats_is_zero(const struct drm_memory_stats *stats);
517 void drm_fdinfo_print_size(struct drm_printer *p,
518 			   const char *prefix,
519 			   const char *stat,
520 			   const char *region,
521 			   u64 sz);
522 void drm_print_memory_stats(struct drm_printer *p,
523 			    const struct drm_memory_stats *stats,
524 			    enum drm_gem_object_status supported_status,
525 			    const char *region);
526 
527 void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file);
528 void drm_show_fdinfo(struct seq_file *m, struct file *f);
529 
530 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags);
531 
532 #endif /* _DRM_FILE_H_ */
533