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