drm_file.h revision 1.2 1 /* $NetBSD: drm_file.h,v 1.2 2021/12/18 23:45:46 riastradh Exp $ */
2
3 /*
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * Copyright (c) 2009-2010, Code Aurora Forum.
7 * All rights reserved.
8 *
9 * Author: Rickard E. (Rik) Faith <faith (at) valinux.com>
10 * Author: Gareth Hughes <gareth (at) valinux.com>
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice (including the next
20 * paragraph) shall be included in all copies or substantial portions of the
21 * Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 * OTHER DEALINGS IN THE SOFTWARE.
30 */
31
32 #ifndef _DRM_FILE_H_
33 #define _DRM_FILE_H_
34
35 #include <linux/types.h>
36 #include <linux/completion.h>
37 #include <linux/idr.h>
38
39 #include <uapi/drm/drm.h>
40
41 #include <drm/drm_prime.h>
42
43 struct dma_fence;
44 struct drm_file;
45 struct drm_device;
46 struct device;
47 struct file;
48
49 /*
50 * FIXME: Not sure we want to have drm_minor here in the end, but to avoid
51 * header include loops we need it here for now.
52 */
53
54 /* Note that the order of this enum is ABI (it determines
55 * /dev/dri/renderD* numbers).
56 */
57 enum drm_minor_type {
58 DRM_MINOR_PRIMARY,
59 DRM_MINOR_CONTROL,
60 DRM_MINOR_RENDER,
61 };
62
63 /**
64 * struct drm_minor - DRM device minor structure
65 *
66 * This structure represents a DRM minor number for device nodes in /dev.
67 * Entirely opaque to drivers and should never be inspected directly by drivers.
68 * Drivers instead should only interact with &struct drm_file and of course
69 * &struct drm_device, which is also where driver-private data and resources can
70 * be attached to.
71 */
72 struct drm_minor {
73 /* private: */
74 int index; /* Minor device number */
75 int type; /* Control or render */
76 struct device *kdev; /* Linux device */
77 struct drm_device *dev;
78
79 struct dentry *debugfs_root;
80
81 struct list_head debugfs_list;
82 struct mutex debugfs_lock; /* Protects debugfs_list. */
83 };
84
85 /**
86 * struct drm_pending_event - Event queued up for userspace to read
87 *
88 * This represents a DRM event. Drivers can use this as a generic completion
89 * mechanism, which supports kernel-internal &struct completion, &struct dma_fence
90 * and also the DRM-specific &struct drm_event delivery mechanism.
91 */
92 struct drm_pending_event {
93 /**
94 * @completion:
95 *
96 * Optional pointer to a kernel internal completion signalled when
97 * drm_send_event() is called, useful to internally synchronize with
98 * nonblocking operations.
99 */
100 struct completion *completion;
101
102 /**
103 * @completion_release:
104 *
105 * Optional callback currently only used by the atomic modeset helpers
106 * to clean up the reference count for the structure @completion is
107 * stored in.
108 */
109 void (*completion_release)(struct completion *completion);
110
111 /**
112 * @event:
113 *
114 * Pointer to the actual event that should be sent to userspace to be
115 * read using drm_read(). Can be optional, since nowadays events are
116 * also used to signal kernel internal threads with @completion or DMA
117 * transactions using @fence.
118 */
119 struct drm_event *event;
120
121 /**
122 * @fence:
123 *
124 * Optional DMA fence to unblock other hardware transactions which
125 * depend upon the nonblocking DRM operation this event represents.
126 */
127 struct dma_fence *fence;
128
129 /**
130 * @file_priv:
131 *
132 * &struct drm_file where @event should be delivered to. Only set when
133 * @event is set.
134 */
135 struct drm_file *file_priv;
136
137 /**
138 * @link:
139 *
140 * Double-linked list to keep track of this event. Can be used by the
141 * driver up to the point when it calls drm_send_event(), after that
142 * this list entry is owned by the core for its own book-keeping.
143 */
144 struct list_head link;
145
146 /**
147 * @pending_link:
148 *
149 * Entry on &drm_file.pending_event_list, to keep track of all pending
150 * events for @file_priv, to allow correct unwinding of them when
151 * userspace closes the file before the event is delivered.
152 */
153 struct list_head pending_link;
154 };
155
156 /**
157 * struct drm_file - DRM file private data
158 *
159 * This structure tracks DRM state per open file descriptor.
160 */
161 struct drm_file {
162 /**
163 * @authenticated:
164 *
165 * Whether the client is allowed to submit rendering, which for legacy
166 * nodes means it must be authenticated.
167 *
168 * See also the :ref:`section on primary nodes and authentication
169 * <drm_primary_node>`.
170 */
171 bool authenticated;
172
173 /**
174 * @stereo_allowed:
175 *
176 * True when the client has asked us to expose stereo 3D mode flags.
177 */
178 bool stereo_allowed;
179
180 /**
181 * @universal_planes:
182 *
183 * True if client understands CRTC primary planes and cursor planes
184 * in the plane list. Automatically set when @atomic is set.
185 */
186 bool universal_planes;
187
188 /** @atomic: True if client understands atomic properties. */
189 bool atomic;
190
191 /**
192 * @aspect_ratio_allowed:
193 *
194 * True, if client can handle picture aspect ratios, and has requested
195 * to pass this information along with the mode.
196 */
197 bool aspect_ratio_allowed;
198
199 /**
200 * @writeback_connectors:
201 *
202 * True if client understands writeback connectors
203 */
204 bool writeback_connectors;
205
206 /**
207 * @is_master:
208 *
209 * This client is the creator of @master. Protected by struct
210 * &drm_device.master_mutex.
211 *
212 * See also the :ref:`section on primary nodes and authentication
213 * <drm_primary_node>`.
214 */
215 bool is_master;
216
217 /**
218 * @master:
219 *
220 * Master this node is currently associated with. Only relevant if
221 * drm_is_primary_client() returns true. Note that this only
222 * matches &drm_device.master if the master is the currently active one.
223 *
224 * See also @authentication and @is_master and the :ref:`section on
225 * primary nodes and authentication <drm_primary_node>`.
226 */
227 struct drm_master *master;
228
229 /** @pid: Process that opened this file. */
230 struct pid *pid;
231
232 /** @magic: Authentication magic, see @authenticated. */
233 drm_magic_t magic;
234
235 /**
236 * @lhead:
237 *
238 * List of all open files of a DRM device, linked into
239 * &drm_device.filelist. Protected by &drm_device.filelist_mutex.
240 */
241 struct list_head lhead;
242
243 /** @minor: &struct drm_minor for this file. */
244 struct drm_minor *minor;
245
246 /**
247 * @object_idr:
248 *
249 * Mapping of mm object handles to object pointers. Used by the GEM
250 * subsystem. Protected by @table_lock.
251 */
252 struct idr object_idr;
253
254 /** @table_lock: Protects @object_idr. */
255 spinlock_t table_lock;
256
257 /** @syncobj_idr: Mapping of sync object handles to object pointers. */
258 struct idr syncobj_idr;
259 /** @syncobj_table_lock: Protects @syncobj_idr. */
260 spinlock_t syncobj_table_lock;
261
262 /** @filp: Pointer to the core file structure. */
263 struct file *filp;
264
265 /**
266 * @driver_priv:
267 *
268 * Optional pointer for driver private data. Can be allocated in
269 * &drm_driver.open and should be freed in &drm_driver.postclose.
270 */
271 void *driver_priv;
272
273 /**
274 * @fbs:
275 *
276 * List of &struct drm_framebuffer associated with this file, using the
277 * &drm_framebuffer.filp_head entry.
278 *
279 * Protected by @fbs_lock. Note that the @fbs list holds a reference on
280 * the framebuffer object to prevent it from untimely disappearing.
281 */
282 struct list_head fbs;
283
284 /** @fbs_lock: Protects @fbs. */
285 struct mutex fbs_lock;
286
287 /**
288 * @blobs:
289 *
290 * User-created blob properties; this retains a reference on the
291 * property.
292 *
293 * Protected by @drm_mode_config.blob_lock;
294 */
295 struct list_head blobs;
296
297 /** @event_wait: Waitqueue for new events added to @event_list. */
298 wait_queue_head_t event_wait;
299
300 /**
301 * @pending_event_list:
302 *
303 * List of pending &struct drm_pending_event, used to clean up pending
304 * events in case this file gets closed before the event is signalled.
305 * Uses the &drm_pending_event.pending_link entry.
306 *
307 * Protect by &drm_device.event_lock.
308 */
309 struct list_head pending_event_list;
310
311 /**
312 * @event_list:
313 *
314 * List of &struct drm_pending_event, ready for delivery to userspace
315 * through drm_read(). Uses the &drm_pending_event.link entry.
316 *
317 * Protect by &drm_device.event_lock.
318 */
319 struct list_head event_list;
320
321 /**
322 * @event_space:
323 *
324 * Available event space to prevent userspace from
325 * exhausting kernel memory. Currently limited to the fairly arbitrary
326 * value of 4KB.
327 */
328 int event_space;
329
330 /** @event_read_lock: Serializes drm_read(). */
331 struct mutex event_read_lock;
332
333 /**
334 * @prime:
335 *
336 * Per-file buffer caches used by the PRIME buffer sharing code.
337 */
338 struct drm_prime_file_private prime;
339
340 /* private: */
341 #if IS_ENABLED(CONFIG_DRM_LEGACY)
342 unsigned long lock_count; /* DRI1 legacy lock count */
343 #endif
344 };
345
346 /**
347 * drm_is_primary_client - is this an open file of the primary node
348 * @file_priv: DRM file
349 *
350 * Returns true if this is an open file of the primary node, i.e.
351 * &drm_file.minor of @file_priv is a primary minor.
352 *
353 * See also the :ref:`section on primary nodes and authentication
354 * <drm_primary_node>`.
355 */
356 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
357 {
358 return file_priv->minor->type == DRM_MINOR_PRIMARY;
359 }
360
361 /**
362 * drm_is_render_client - is this an open file of the render node
363 * @file_priv: DRM file
364 *
365 * Returns true if this is an open file of the render node, i.e.
366 * &drm_file.minor of @file_priv is a render minor.
367 *
368 * See also the :ref:`section on render nodes <drm_render_node>`.
369 */
370 static inline bool drm_is_render_client(const struct drm_file *file_priv)
371 {
372 return file_priv->minor->type == DRM_MINOR_RENDER;
373 }
374
375 int drm_open(struct inode *inode, struct file *filp);
376 ssize_t drm_read(struct file *filp, char __user *buffer,
377 size_t count, loff_t *offset);
378 int drm_release(struct inode *inode, struct file *filp);
379 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait);
380 int drm_event_reserve_init_locked(struct drm_device *dev,
381 struct drm_file *file_priv,
382 struct drm_pending_event *p,
383 struct drm_event *e);
384 int drm_event_reserve_init(struct drm_device *dev,
385 struct drm_file *file_priv,
386 struct drm_pending_event *p,
387 struct drm_event *e);
388 void drm_event_cancel_free(struct drm_device *dev,
389 struct drm_pending_event *p);
390 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);
391 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
392
393 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags);
394
395 #endif /* _DRM_FILE_H_ */
396