1 /*        $NetBSD: drm_file.h,v 1.8 2021/12/19 12:23:42 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@valinux.com>
10  * Author: Gareth Hughes <gareth@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 #ifdef __NetBSD__             /* XXX */
44 #include <drm/drm_wait_netbsd.h>
45 #endif
46 
47 struct dma_fence;
48 struct drm_file;
49 struct drm_device;
50 struct device;
51 struct file;
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 order of this enum is ABI (it determines
59  * /dev/dri/renderD* numbers).
60  */
61 enum drm_minor_type {
62           DRM_MINOR_PRIMARY,
63           DRM_MINOR_CONTROL,
64           DRM_MINOR_RENDER,
65 };
66 
67 /**
68  * struct drm_minor - DRM device minor structure
69  *
70  * This structure represents a DRM minor number for device nodes in /dev.
71  * Entirely opaque to drivers and should never be inspected directly by drivers.
72  * Drivers instead should only interact with &struct drm_file and of course
73  * &struct drm_device, which is also where driver-private data and resources can
74  * be attached to.
75  */
76 struct drm_minor {
77           /* private: */
78           int index;                              /* Minor device number */
79           int type;                       /* Control or render */
80           struct device *kdev;                    /* Linux device */
81           struct drm_device *dev;
82 
83 #ifndef __NetBSD__            /* XXX debugfs */
84           struct dentry *debugfs_root;
85 
86           struct list_head debugfs_list;
87           struct mutex debugfs_lock; /* Protects debugfs_list. */
88 #endif
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            * @is_master:
214            *
215            * This client is the creator of @master. Protected by struct
216            * &drm_device.master_mutex.
217            *
218            * See also the :ref:`section on primary nodes and authentication
219            * <drm_primary_node>`.
220            */
221           bool is_master;
222 
223           /**
224            * @master:
225            *
226            * Master this node is currently associated with. Only relevant if
227            * drm_is_primary_client() returns true. Note that this only
228            * matches &drm_device.master if the master is the currently active one.
229            *
230            * See also @authentication and @is_master and the :ref:`section on
231            * primary nodes and authentication <drm_primary_node>`.
232            */
233           struct drm_master *master;
234 
235 #ifndef __NetBSD__
236           /** @pid: Process that opened this file. */
237           struct pid *pid;
238 #endif
239 
240           /** @magic: Authentication magic, see @authenticated. */
241           drm_magic_t magic;
242 
243           /**
244            * @lhead:
245            *
246            * List of all open files of a DRM device, linked into
247            * &drm_device.filelist. Protected by &drm_device.filelist_mutex.
248            */
249           struct list_head lhead;
250 
251           /** @minor: &struct drm_minor for this file. */
252           struct drm_minor *minor;
253 
254           /**
255            * @object_idr:
256            *
257            * Mapping of mm object handles to object pointers. Used by the GEM
258            * subsystem. Protected by @table_lock.
259            */
260           struct idr object_idr;
261 
262           /** @table_lock: Protects @object_idr. */
263           spinlock_t table_lock;
264 
265           /** @syncobj_idr: Mapping of sync object handles to object pointers. */
266           struct idr syncobj_idr;
267           /** @syncobj_table_lock: Protects @syncobj_idr. */
268           spinlock_t syncobj_table_lock;
269 
270           /** @filp: Pointer to the core file structure. */
271           struct file *filp;
272 
273           /**
274            * @driver_priv:
275            *
276            * Optional pointer for driver private data. Can be allocated in
277            * &drm_driver.open and should be freed in &drm_driver.postclose.
278            */
279           void *driver_priv;
280 
281           /**
282            * @fbs:
283            *
284            * List of &struct drm_framebuffer associated with this file, using the
285            * &drm_framebuffer.filp_head entry.
286            *
287            * Protected by @fbs_lock. Note that the @fbs list holds a reference on
288            * the framebuffer object to prevent it from untimely disappearing.
289            */
290           struct list_head fbs;
291 
292           /** @fbs_lock: Protects @fbs. */
293           struct mutex fbs_lock;
294 
295           /**
296            * @blobs:
297            *
298            * User-created blob properties; this retains a reference on the
299            * property.
300            *
301            * Protected by @drm_mode_config.blob_lock;
302            */
303           struct list_head blobs;
304 
305           /** @event_wait: Waitqueue for new events added to @event_list. */
306 #ifdef __NetBSD__
307           drm_waitqueue_t event_wait;
308           struct selinfo event_selq;
309 #else
310           wait_queue_head_t event_wait;
311 #endif
312 
313           /**
314            * @pending_event_list:
315            *
316            * List of pending &struct drm_pending_event, used to clean up pending
317            * events in case this file gets closed before the event is signalled.
318            * Uses the &drm_pending_event.pending_link entry.
319            *
320            * Protect by &drm_device.event_lock.
321            */
322           struct list_head pending_event_list;
323 
324           /**
325            * @event_list:
326            *
327            * List of &struct drm_pending_event, ready for delivery to userspace
328            * through drm_read(). Uses the &drm_pending_event.link entry.
329            *
330            * Protect by &drm_device.event_lock.
331            */
332           struct list_head event_list;
333 
334           /**
335            * @event_space:
336            *
337            * Available event space to prevent userspace from
338            * exhausting kernel memory. Currently limited to the fairly arbitrary
339            * value of 4KB.
340            */
341           int event_space;
342 
343           /** @event_read_lock: Serializes drm_read(). */
344 #ifdef __NetBSD__
345           struct lwp *event_read_lock;
346           drm_waitqueue_t event_read_wq;
347 #else
348           struct mutex event_read_lock;
349 #endif
350 
351           /**
352            * @prime:
353            *
354            * Per-file buffer caches used by the PRIME buffer sharing code.
355            */
356           struct drm_prime_file_private prime;
357 
358           /* private: */
359 #if IS_ENABLED(CONFIG_DRM_LEGACY)
360           unsigned long lock_count; /* DRI1 legacy lock count */
361 #endif
362 };
363 
364 /**
365  * drm_is_primary_client - is this an open file of the primary node
366  * @file_priv: DRM file
367  *
368  * Returns true if this is an open file of the primary node, i.e.
369  * &drm_file.minor of @file_priv is a primary minor.
370  *
371  * See also the :ref:`section on primary nodes and authentication
372  * <drm_primary_node>`.
373  */
drm_is_primary_client(const struct drm_file * file_priv)374 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
375 {
376           return file_priv->minor->type == DRM_MINOR_PRIMARY;
377 }
378 
379 /**
380  * drm_is_render_client - is this an open file of the render node
381  * @file_priv: DRM file
382  *
383  * Returns true if this is an open file of the render node, i.e.
384  * &drm_file.minor of @file_priv is a render minor.
385  *
386  * See also the :ref:`section on render nodes <drm_render_node>`.
387  */
drm_is_render_client(const struct drm_file * file_priv)388 static inline bool drm_is_render_client(const struct drm_file *file_priv)
389 {
390           return file_priv->minor->type == DRM_MINOR_RENDER;
391 }
392 
393 #ifdef __NetBSD__
394 extern const struct fileops drm_fileops;
395 int drm_open_file(struct drm_file *, void *, struct drm_minor *);
396 void drm_close_file(struct drm_file *);
397 #else
398 int drm_open(struct inode *inode, struct file *filp);
399 ssize_t drm_read(struct file *filp, char __user *buffer,
400                      size_t count, loff_t *offset);
401 int drm_release(struct inode *inode, struct file *filp);
402 __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait);
403 #endif
404 int drm_event_reserve_init_locked(struct drm_device *dev,
405                                           struct drm_file *file_priv,
406                                           struct drm_pending_event *p,
407                                           struct drm_event *e);
408 int drm_event_reserve_init(struct drm_device *dev,
409                                  struct drm_file *file_priv,
410                                  struct drm_pending_event *p,
411                                  struct drm_event *e);
412 void drm_event_cancel_free(struct drm_device *dev,
413                                  struct drm_pending_event *p);
414 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);
415 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
416 
417 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags);
418 
419 #endif /* _DRM_FILE_H_ */
420