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