• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	 * Note that allocated entries might be NULL as a transient state when
307 	 * creating or deleting a handle.
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 mutex 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 mutex 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 
403 /**
404  * drm_is_primary_client - is this an open file of the primary node
405  * @file_priv: DRM file
406  *
407  * Returns true if this is an open file of the primary node, i.e.
408  * &drm_file.minor of @file_priv is a primary minor.
409  *
410  * See also the :ref:`section on primary nodes and authentication
411  * <drm_primary_node>`.
412  */
drm_is_primary_client(const struct drm_file * file_priv)413 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
414 {
415 	return file_priv->minor->type == DRM_MINOR_PRIMARY;
416 }
417 
418 /**
419  * drm_is_render_client - is this an open file of the render node
420  * @file_priv: DRM file
421  *
422  * Returns true if this is an open file of the render node, i.e.
423  * &drm_file.minor of @file_priv is a render minor.
424  *
425  * See also the :ref:`section on render nodes <drm_render_node>`.
426  */
drm_is_render_client(const struct drm_file * file_priv)427 static inline bool drm_is_render_client(const struct drm_file *file_priv)
428 {
429 	return file_priv->minor->type == DRM_MINOR_RENDER;
430 }
431 
432 /**
433  * drm_is_accel_client - is this an open file of the compute acceleration node
434  * @file_priv: DRM file
435  *
436  * Returns true if this is an open file of the compute acceleration node, i.e.
437  * &drm_file.minor of @file_priv is a accel minor.
438  *
439  * See also :doc:`Introduction to compute accelerators subsystem
440  * </accel/introduction>`.
441  */
drm_is_accel_client(const struct drm_file * file_priv)442 static inline bool drm_is_accel_client(const struct drm_file *file_priv)
443 {
444 	return file_priv->minor->type == DRM_MINOR_ACCEL;
445 }
446 
447 void drm_file_update_pid(struct drm_file *);
448 
449 struct drm_minor *drm_minor_acquire(struct xarray *minors_xa, unsigned int minor_id);
450 void drm_minor_release(struct drm_minor *minor);
451 
452 int drm_open(struct inode *inode, struct file *filp);
453 int drm_open_helper(struct file *filp, struct drm_minor *minor);
454 ssize_t drm_read(struct file *filp, char __user *buffer,
455 		 size_t count, loff_t *offset);
456 int drm_release(struct inode *inode, struct file *filp);
457 int drm_release_noglobal(struct inode *inode, struct file *filp);
458 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait);
459 int drm_event_reserve_init_locked(struct drm_device *dev,
460 				  struct drm_file *file_priv,
461 				  struct drm_pending_event *p,
462 				  struct drm_event *e);
463 int drm_event_reserve_init(struct drm_device *dev,
464 			   struct drm_file *file_priv,
465 			   struct drm_pending_event *p,
466 			   struct drm_event *e);
467 void drm_event_cancel_free(struct drm_device *dev,
468 			   struct drm_pending_event *p);
469 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);
470 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
471 void drm_send_event_timestamp_locked(struct drm_device *dev,
472 				     struct drm_pending_event *e,
473 				     ktime_t timestamp);
474 
475 /**
476  * struct drm_memory_stats - GEM object stats associated
477  * @shared: Total size of GEM objects shared between processes
478  * @private: Total size of GEM objects
479  * @resident: Total size of GEM objects backing pages
480  * @purgeable: Total size of GEM objects that can be purged (resident and not active)
481  * @active: Total size of GEM objects active on one or more engines
482  *
483  * Used by drm_print_memory_stats()
484  */
485 struct drm_memory_stats {
486 	u64 shared;
487 	u64 private;
488 	u64 resident;
489 	u64 purgeable;
490 	u64 active;
491 };
492 
493 enum drm_gem_object_status;
494 
495 void drm_print_memory_stats(struct drm_printer *p,
496 			    const struct drm_memory_stats *stats,
497 			    enum drm_gem_object_status supported_status,
498 			    const char *region);
499 
500 void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file);
501 void drm_show_fdinfo(struct seq_file *m, struct file *f);
502 
503 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags);
504 
505 #endif /* _DRM_FILE_H_ */
506