Merge git://git.kernel.org/pub/scm/linux/kernel/git/cmetcalf/linux-tile
[sfrench/cifs-2.6.git] / Documentation / gpu / drm-uapi.rst
1 ===================
2 Userland interfaces
3 ===================
4
5 The DRM core exports several interfaces to applications, generally
6 intended to be used through corresponding libdrm wrapper functions. In
7 addition, drivers export device-specific interfaces for use by userspace
8 drivers & device-aware applications through ioctls and sysfs files.
9
10 External interfaces include: memory mapping, context management, DMA
11 operations, AGP management, vblank control, fence management, memory
12 management, and output management.
13
14 Cover generic ioctls and sysfs layout here. We only need high-level
15 info, since man pages should cover the rest.
16
17 libdrm Device Lookup
18 ====================
19
20 .. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
21    :doc: getunique and setversion story
22
23
24 Primary Nodes, DRM Master and Authentication
25 ============================================
26
27 .. kernel-doc:: drivers/gpu/drm/drm_auth.c
28    :doc: master and authentication
29
30 .. kernel-doc:: drivers/gpu/drm/drm_auth.c
31    :export:
32
33 .. kernel-doc:: include/drm/drm_auth.h
34    :internal:
35
36 Open-Source Userspace Requirements
37 ==================================
38
39 The DRM subsystem has stricter requirements than most other kernel subsystems on
40 what the userspace side for new uAPI needs to look like. This section here
41 explains what exactly those requirements are, and why they exist.
42
43 The short summary is that any addition of DRM uAPI requires corresponding
44 open-sourced userspace patches, and those patches must be reviewed and ready for
45 merging into a suitable and canonical upstream project.
46
47 GFX devices (both display and render/GPU side) are really complex bits of
48 hardware, with userspace and kernel by necessity having to work together really
49 closely.  The interfaces, for rendering and modesetting, must be extremely wide
50 and flexible, and therefore it is almost always impossible to precisely define
51 them for every possible corner case. This in turn makes it really practically
52 infeasible to differentiate between behaviour that's required by userspace, and
53 which must not be changed to avoid regressions, and behaviour which is only an
54 accidental artifact of the current implementation.
55
56 Without access to the full source code of all userspace users that means it
57 becomes impossible to change the implementation details, since userspace could
58 depend upon the accidental behaviour of the current implementation in minute
59 details. And debugging such regressions without access to source code is pretty
60 much impossible. As a consequence this means:
61
62 - The Linux kernel's "no regression" policy holds in practice only for
63   open-source userspace of the DRM subsystem. DRM developers are perfectly fine
64   if closed-source blob drivers in userspace use the same uAPI as the open
65   drivers, but they must do so in the exact same way as the open drivers.
66   Creative (ab)use of the interfaces will, and in the past routinely has, lead
67   to breakage.
68
69 - Any new userspace interface must have an open-source implementation as
70   demonstration vehicle.
71
72 The other reason for requiring open-source userspace is uAPI review. Since the
73 kernel and userspace parts of a GFX stack must work together so closely, code
74 review can only assess whether a new interface achieves its goals by looking at
75 both sides. Making sure that the interface indeed covers the use-case fully
76 leads to a few additional requirements:
77
78 - The open-source userspace must not be a toy/test application, but the real
79   thing. Specifically it needs to handle all the usual error and corner cases.
80   These are often the places where new uAPI falls apart and hence essential to
81   assess the fitness of a proposed interface.
82
83 - The userspace side must be fully reviewed and tested to the standards of that
84   userspace project. For e.g. mesa this means piglit testcases and review on the
85   mailing list. This is again to ensure that the new interface actually gets the
86   job done.
87
88 - The userspace patches must be against the canonical upstream, not some vendor
89   fork. This is to make sure that no one cheats on the review and testing
90   requirements by doing a quick fork.
91
92 - The kernel patch can only be merged after all the above requirements are met,
93   but it **must** be merged **before** the userspace patches land. uAPI always flows
94   from the kernel, doing things the other way round risks divergence of the uAPI
95   definitions and header files.
96
97 These are fairly steep requirements, but have grown out from years of shared
98 pain and experience with uAPI added hastily, and almost always regretted about
99 just as fast. GFX devices change really fast, requiring a paradigm shift and
100 entire new set of uAPI interfaces every few years at least. Together with the
101 Linux kernel's guarantee to keep existing userspace running for 10+ years this
102 is already rather painful for the DRM subsystem, with multiple different uAPIs
103 for the same thing co-existing. If we add a few more complete mistakes into the
104 mix every year it would be entirely unmanageable.
105
106 Render nodes
107 ============
108
109 DRM core provides multiple character-devices for user-space to use.
110 Depending on which device is opened, user-space can perform a different
111 set of operations (mainly ioctls). The primary node is always created
112 and called card<num>. Additionally, a currently unused control node,
113 called controlD<num> is also created. The primary node provides all
114 legacy operations and historically was the only interface used by
115 userspace. With KMS, the control node was introduced. However, the
116 planned KMS control interface has never been written and so the control
117 node stays unused to date.
118
119 With the increased use of offscreen renderers and GPGPU applications,
120 clients no longer require running compositors or graphics servers to
121 make use of a GPU. But the DRM API required unprivileged clients to
122 authenticate to a DRM-Master prior to getting GPU access. To avoid this
123 step and to grant clients GPU access without authenticating, render
124 nodes were introduced. Render nodes solely serve render clients, that
125 is, no modesetting or privileged ioctls can be issued on render nodes.
126 Only non-global rendering commands are allowed. If a driver supports
127 render nodes, it must advertise it via the DRIVER_RENDER DRM driver
128 capability. If not supported, the primary node must be used for render
129 clients together with the legacy drmAuth authentication procedure.
130
131 If a driver advertises render node support, DRM core will create a
132 separate render node called renderD<num>. There will be one render node
133 per device. No ioctls except PRIME-related ioctls will be allowed on
134 this node. Especially GEM_OPEN will be explicitly prohibited. Render
135 nodes are designed to avoid the buffer-leaks, which occur if clients
136 guess the flink names or mmap offsets on the legacy interface.
137 Additionally to this basic interface, drivers must mark their
138 driver-dependent render-only ioctls as DRM_RENDER_ALLOW so render
139 clients can use them. Driver authors must be careful not to allow any
140 privileged ioctls on render nodes.
141
142 With render nodes, user-space can now control access to the render node
143 via basic file-system access-modes. A running graphics server which
144 authenticates clients on the privileged primary/legacy node is no longer
145 required. Instead, a client can open the render node and is immediately
146 granted GPU access. Communication between clients (or servers) is done
147 via PRIME. FLINK from render node to legacy node is not supported. New
148 clients must not use the insecure FLINK interface.
149
150 Besides dropping all modeset/global ioctls, render nodes also drop the
151 DRM-Master concept. There is no reason to associate render clients with
152 a DRM-Master as they are independent of any graphics server. Besides,
153 they must work without any running master, anyway. Drivers must be able
154 to run without a master object if they support render nodes. If, on the
155 other hand, a driver requires shared state between clients which is
156 visible to user-space and accessible beyond open-file boundaries, they
157 cannot support render nodes.
158
159 Validating changes with IGT
160 ===========================
161
162 There's a collection of tests that aims to cover the whole functionality of
163 DRM drivers and that can be used to check that changes to DRM drivers or the
164 core don't regress existing functionality. This test suite is called IGT and
165 its code can be found in https://cgit.freedesktop.org/drm/igt-gpu-tools/.
166
167 To build IGT, start by installing its build dependencies. In Debian-based
168 systems::
169
170         # apt-get build-dep intel-gpu-tools
171
172 And in Fedora-based systems::
173
174         # dnf builddep intel-gpu-tools
175
176 Then clone the repository::
177
178         $ git clone git://anongit.freedesktop.org/drm/igt-gpu-tools
179
180 Configure the build system and start the build::
181
182         $ cd igt-gpu-tools && ./autogen.sh && make -j6
183
184 Download the piglit dependency::
185
186         $ ./scripts/run-tests.sh -d
187
188 And run the tests::
189
190         $ ./scripts/run-tests.sh -t kms -t core -s
191
192 run-tests.sh is a wrapper around piglit that will execute the tests matching
193 the -t options. A report in HTML format will be available in
194 ./results/html/index.html. Results can be compared with piglit.
195
196 VBlank event handling
197 =====================
198
199 The DRM core exposes two vertical blank related ioctls:
200
201 DRM_IOCTL_WAIT_VBLANK
202     This takes a struct drm_wait_vblank structure as its argument, and
203     it is used to block or request a signal when a specified vblank
204     event occurs.
205
206 DRM_IOCTL_MODESET_CTL
207     This was only used for user-mode-settind drivers around modesetting
208     changes to allow the kernel to update the vblank interrupt after
209     mode setting, since on many devices the vertical blank counter is
210     reset to 0 at some point during modeset. Modern drivers should not
211     call this any more since with kernel mode setting it is a no-op.
212
213 This second part of the GPU Driver Developer's Guide documents driver
214 code, implementation details and also all the driver-specific userspace
215 interfaces. Especially since all hardware-acceleration interfaces to
216 userspace are driver specific for efficiency and other reasons these
217 interfaces can be rather substantial. Hence every driver has its own
218 chapter.
219
220 Testing and validation
221 ======================
222
223 .. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
224    :doc: CRC ABI