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