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