Skip to content

Commit b93658f

Browse files
committed
drm/doc: Document drm_file.[hc]
Well, mostly drm_file.h, and clean up all related things: - I didnt' figure out the difference between preclose and postclose. The existing explanation in drm-internals.rst didn't convince me, since it's also really outdated - we clean up pending DRM events in the core nowadays. I put a FIXME in for the future. - Another FIXME is to have a macro for default fops. - Lots of links all around, main areas are to tie the overview in drm_file.c more into the callbacks in struct drm_device, and the other is to link render/primary node code to the right sections in drm-uapi.rst. - Also moved the open/close stuff to drm_drv.h from drm-internals.rst, seems like the better place for that information. Since that section was rather outdated this amounted to full-on rewrite. A big missing piece here is some overview graph, but I think better to wait with that one until drm_device and drm_driver are also fully documented. v2: Nits from Sean. Reviewed-by: Sean Paul <seanpaul@chromium.org> Reviewed-by: Liviu Dudau <Liviu.Dudau@arm.com> Signed-off-by: Daniel Vetter <daniel.vetter@intel.com> Link: http://patchwork.freedesktop.org/patch/msgid/20170308141257.12119-12-daniel.vetter@ffwll.ch
1 parent 7d52cb8 commit b93658f

5 files changed

Lines changed: 354 additions & 114 deletions

File tree

‎Documentation/gpu/drm-internals.rst‎

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -243,61 +243,15 @@ drivers.
243243
Open/Close, File Operations and IOCTLs
244244
======================================
245245

246-
Open and Close
247-
--------------
248-
249-
Open and close handlers. None of those methods are mandatory::
250-
251-
int (*firstopen) (struct drm_device *);
252-
void (*lastclose) (struct drm_device *);
253-
int (*open) (struct drm_device *, struct drm_file *);
254-
void (*preclose) (struct drm_device *, struct drm_file *);
255-
void (*postclose) (struct drm_device *, struct drm_file *);
256-
257-
The firstopen method is called by the DRM core for legacy UMS (User Mode
258-
Setting) drivers only when an application opens a device that has no
259-
other opened file handle. UMS drivers can implement it to acquire device
260-
resources. KMS drivers can't use the method and must acquire resources
261-
in the load method instead.
262-
263-
Similarly the lastclose method is called when the last application
264-
holding a file handle opened on the device closes it, for both UMS and
265-
KMS drivers. Additionally, the method is also called at module unload
266-
time or, for hot-pluggable devices, when the device is unplugged. The
267-
firstopen and lastclose calls can thus be unbalanced.
268-
269-
The open method is called every time the device is opened by an
270-
application. Drivers can allocate per-file private data in this method
271-
and store them in the struct :c:type:`struct drm_file
272-
<drm_file>` driver_priv field. Note that the open method is
273-
called before firstopen.
274-
275-
The close operation is split into preclose and postclose methods.
276-
Drivers must stop and cleanup all per-file operations in the preclose
277-
method. For instance pending vertical blanking and page flip events must
278-
be cancelled. No per-file operation is allowed on the file handle after
279-
returning from the preclose method.
280-
281-
Finally the postclose method is called as the last step of the close
282-
operation, right before calling the lastclose method if no other open
283-
file handle exists for the device. Drivers that have allocated per-file
284-
private data in the open method should free it here.
285-
286-
The lastclose method should restore CRTC and plane properties to default
287-
value, so that a subsequent open of the device will not inherit state
288-
from the previous user. It can also be used to execute delayed power
289-
switching state changes, e.g. in conjunction with the :ref:`vga_switcheroo`
290-
infrastructure. Beyond that KMS drivers should not do any
291-
further cleanup. Only legacy UMS drivers might need to clean up device
292-
state so that the vga console or an independent fbdev driver could take
293-
over.
294-
295246
File Operations
296247
---------------
297248

298249
.. kernel-doc:: drivers/gpu/drm/drm_file.c
299250
:doc: file operations
300251

252+
.. kernel-doc:: include/drm/drm_file.h
253+
:internal:
254+
301255
.. kernel-doc:: drivers/gpu/drm/drm_file.c
302256
:export:
303257

‎Documentation/gpu/drm-uapi.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ libdrm Device Lookup
2121
:doc: getunique and setversion story
2222

2323

24+
.. _drm_primary_node:
25+
2426
Primary Nodes, DRM Master and Authentication
2527
============================================
2628

@@ -103,6 +105,8 @@ is already rather painful for the DRM subsystem, with multiple different uAPIs
103105
for the same thing co-existing. If we add a few more complete mistakes into the
104106
mix every year it would be entirely unmanageable.
105107

108+
.. _drm_render_node:
109+
106110
Render nodes
107111
============
108112

‎drivers/gpu/drm/drm_file.c‎

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,29 @@ DEFINE_MUTEX(drm_global_mutex);
5050
*
5151
* Drivers must define the file operations structure that forms the DRM
5252
* userspace API entry point, even though most of those operations are
53-
* implemented in the DRM core. The mandatory functions are drm_open(),
53+
* implemented in the DRM core. The resulting &struct file_operations must be
54+
* stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
5455
* drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
55-
* (note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n). Drivers which
56-
* implement private ioctls that require 32/64 bit compatibility support must
57-
* provide their own .compat_ioctl() handler that processes private ioctls and
58-
* calls drm_compat_ioctl() for core ioctls.
56+
* Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
57+
* need to sprinkle #ifdef into the code. Drivers which implement private ioctls
58+
* that require 32/64 bit compatibility support must provide their own
59+
* &file_operations.compat_ioctl handler that processes private ioctls and calls
60+
* drm_compat_ioctl() for core ioctls.
5961
*
6062
* In addition drm_read() and drm_poll() provide support for DRM events. DRM
6163
* events are a generic and extensible means to send asynchronous events to
6264
* userspace through the file descriptor. They are used to send vblank event and
6365
* page flip completions by the KMS API. But drivers can also use it for their
6466
* own needs, e.g. to signal completion of rendering.
6567
*
68+
* For the driver-side event interface see drm_event_reserve_init() and
69+
* drm_send_event() as the main starting points.
70+
*
6671
* The memory mapping implementation will vary depending on how the driver
6772
* manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
6873
* function, modern drivers should use one of the provided memory-manager
69-
* specific implementations. For GEM-based drivers this is drm_gem_mmap().
74+
* specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
75+
* for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
7076
*
7177
* No other file operations are supported by the DRM userspace API. Overall the
7278
* following is an example #file_operations structure::
@@ -82,6 +88,9 @@ DEFINE_MUTEX(drm_global_mutex);
8288
* .llseek = no_llseek,
8389
* .mmap = drm_gem_mmap,
8490
* };
91+
*
92+
* FIXME: We should have a macro for this (and the CMA version) so that drivers
93+
* don't have to repeat it all the time.
8594
*/
8695

8796
static int drm_open_helper(struct file *filp, struct drm_minor *minor);
@@ -111,9 +120,9 @@ static int drm_setup(struct drm_device * dev)
111120
* @inode: device inode
112121
* @filp: file pointer.
113122
*
114-
* This function must be used by drivers as their .open() #file_operations
115-
* method. It looks up the correct DRM device and instantiates all the per-file
116-
* resources for it.
123+
* This function must be used by drivers as their &file_operations.open method.
124+
* It looks up the correct DRM device and instantiates all the per-file
125+
* resources for it. It also calls the &drm_driver.open driver callback.
117126
*
118127
* RETURNS:
119128
*
@@ -298,11 +307,6 @@ static void drm_events_release(struct drm_file *file_priv)
298307
spin_unlock_irqrestore(&dev->event_lock, flags);
299308
}
300309

301-
/*
302-
* drm_legacy_dev_reinit
303-
*
304-
* Reinitializes a legacy/ums drm device in it's lastclose function.
305-
*/
306310
static void drm_legacy_dev_reinit(struct drm_device *dev)
307311
{
308312
if (dev->irq_enabled)
@@ -327,15 +331,6 @@ static void drm_legacy_dev_reinit(struct drm_device *dev)
327331
DRM_DEBUG("lastclose completed\n");
328332
}
329333

330-
/*
331-
* Take down the DRM device.
332-
*
333-
* \param dev DRM device structure.
334-
*
335-
* Frees every resource in \p dev.
336-
*
337-
* \sa drm_device
338-
*/
339334
void drm_lastclose(struct drm_device * dev)
340335
{
341336
DRM_DEBUG("\n");
@@ -353,9 +348,11 @@ void drm_lastclose(struct drm_device * dev)
353348
* @inode: device inode
354349
* @filp: file pointer.
355350
*
356-
* This function must be used by drivers as their .release() #file_operations
357-
* method. It frees any resources associated with the open file, and if this is
358-
* the last open file for the DRM device also proceeds to call drm_lastclose().
351+
* This function must be used by drivers as their &file_operations.release
352+
* method. It frees any resources associated with the open file, and calls the
353+
* &drm_driver.preclose and &drm_driver.lastclose driver callbacks. If this is
354+
* the last open file for the DRM device also proceeds to call the
355+
* &drm_driver.lastclose driver callback.
359356
*
360357
* RETURNS:
361358
*
@@ -443,13 +440,13 @@ EXPORT_SYMBOL(drm_release);
443440
* @count: count in bytes to read
444441
* @offset: offset to read
445442
*
446-
* This function must be used by drivers as their .read() #file_operations
443+
* This function must be used by drivers as their &file_operations.read
447444
* method iff they use DRM events for asynchronous signalling to userspace.
448445
* Since events are used by the KMS API for vblank and page flip completion this
449446
* means all modern display drivers must use it.
450447
*
451-
* @offset is ignore, DRM events are read like a pipe. Therefore drivers also
452-
* must set the .llseek() #file_operation to no_llseek(). Polling support is
448+
* @offset is ignored, DRM events are read like a pipe. Therefore drivers also
449+
* must set the &file_operation.llseek to no_llseek(). Polling support is
453450
* provided by drm_poll().
454451
*
455452
* This function will only ever read a full event. Therefore userspace must
@@ -537,10 +534,10 @@ EXPORT_SYMBOL(drm_read);
537534
* @filp: file pointer
538535
* @wait: poll waiter table
539536
*
540-
* This function must be used by drivers as their .read() #file_operations
541-
* method iff they use DRM events for asynchronous signalling to userspace.
542-
* Since events are used by the KMS API for vblank and page flip completion this
543-
* means all modern display drivers must use it.
537+
* This function must be used by drivers as their &file_operations.read method
538+
* iff they use DRM events for asynchronous signalling to userspace. Since
539+
* events are used by the KMS API for vblank and page flip completion this means
540+
* all modern display drivers must use it.
544541
*
545542
* See also drm_read().
546543
*
@@ -650,7 +647,8 @@ EXPORT_SYMBOL(drm_event_reserve_init);
650647
* @p: tracking structure for the pending event
651648
*
652649
* This function frees the event @p initialized with drm_event_reserve_init()
653-
* and releases any allocated space.
650+
* and releases any allocated space. It is used to cancel an event when the
651+
* nonblocking operation could not be submitted and needed to be aborted.
654652
*/
655653
void drm_event_cancel_free(struct drm_device *dev,
656654
struct drm_pending_event *p)

‎include/drm/drm_drv.h‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ struct drm_mode_create_dumb;
6464
* structure for GEM drivers.
6565
*/
6666
struct drm_driver {
67-
6867
/**
6968
* @load:
7069
*
@@ -76,14 +75,94 @@ struct drm_driver {
7675
* See drm_dev_init() and drm_dev_register() for proper and
7776
* race-free way to set up a &struct drm_device.
7877
*
78+
* This is deprecated, do not use!
79+
*
7980
* Returns:
8081
*
8182
* Zero on success, non-zero value on failure.
8283
*/
8384
int (*load) (struct drm_device *, unsigned long flags);
85+
86+
/**
87+
* @open:
88+
*
89+
* Driver callback when a new &struct drm_file is opened. Useful for
90+
* setting up driver-private data structures like buffer allocators,
91+
* execution contexts or similar things. Such driver-private resources
92+
* must be released again in @postclose.
93+
*
94+
* Since the display/modeset side of DRM can only be owned by exactly
95+
* one &struct drm_file (see &drm_file.is_master and &drm_device.master)
96+
* there should never be a need to set up any modeset related resources
97+
* in this callback. Doing so would be a driver design bug.
98+
*
99+
* Returns:
100+
*
101+
* 0 on success, a negative error code on failure, which will be
102+
* promoted to userspace as the result of the open() system call.
103+
*/
84104
int (*open) (struct drm_device *, struct drm_file *);
105+
106+
/**
107+
* @preclose:
108+
*
109+
* One of the driver callbacks when a new &struct drm_file is closed.
110+
* Useful for tearing down driver-private data structures allocated in
111+
* @open like buffer allocators, execution contexts or similar things.
112+
*
113+
* Since the display/modeset side of DRM can only be owned by exactly
114+
* one &struct drm_file (see &drm_file.is_master and &drm_device.master)
115+
* there should never be a need to tear down any modeset related
116+
* resources in this callback. Doing so would be a driver design bug.
117+
*
118+
* FIXME: It is not really clear why there's both @preclose and
119+
* @postclose. Without a really good reason, use @postclose only.
120+
*/
85121
void (*preclose) (struct drm_device *, struct drm_file *file_priv);
122+
123+
/**
124+
* @postclose:
125+
*
126+
* One of the driver callbacks when a new &struct drm_file is closed.
127+
* Useful for tearing down driver-private data structures allocated in
128+
* @open like buffer allocators, execution contexts or similar things.
129+
*
130+
* Since the display/modeset side of DRM can only be owned by exactly
131+
* one &struct drm_file (see &drm_file.is_master and &drm_device.master)
132+
* there should never be a need to tear down any modeset related
133+
* resources in this callback. Doing so would be a driver design bug.
134+
*
135+
* FIXME: It is not really clear why there's both @preclose and
136+
* @postclose. Without a really good reason, use @postclose only.
137+
*/
86138
void (*postclose) (struct drm_device *, struct drm_file *);
139+
140+
/**
141+
* @lastclose:
142+
*
143+
* Called when the last &struct drm_file has been closed and there's
144+
* currently no userspace client for the &struct drm_device.
145+
*
146+
* Modern drivers should only use this to force-restore the fbdev
147+
* framebuffer using drm_fb_helper_restore_fbdev_mode_unlocked().
148+
* Anything else would indicate there's something seriously wrong.
149+
* Modern drivers can also use this to execute delayed power switching
150+
* state changes, e.g. in conjunction with the :ref:`vga_switcheroo`
151+
* infrastructure.
152+
*
153+
* This is called after @preclose and @postclose have been called.
154+
*
155+
* NOTE:
156+
*
157+
* All legacy drivers use this callback to de-initialize the hardware.
158+
* This is purely because of the shadow-attach model, where the DRM
159+
* kernel driver does not really own the hardware. Instead ownershipe is
160+
* handled with the help of userspace through an inheritedly racy dance
161+
* to set/unset the VT into raw mode.
162+
*
163+
* Legacy drivers initialize the hardware in the @firstopen callback,
164+
* which isn't even called for modern drivers.
165+
*/
87166
void (*lastclose) (struct drm_device *);
88167

89168
/**

0 commit comments

Comments
 (0)