1===================
2Userland interfaces
3===================
4
5The DRM core exports several interfaces to applications, generally
6intended to be used through corresponding libdrm wrapper functions. In
7addition, drivers export device-specific interfaces for use by userspace
8drivers & device-aware applications through ioctls and sysfs files.
9
10External interfaces include: memory mapping, context management, DMA
11operations, AGP management, vblank control, fence management, memory
12management, and output management.
13
14Cover generic ioctls and sysfs layout here. We only need high-level
15info, since man pages should cover the rest.
16
17libdrm Device Lookup
18====================
19
20.. kernel-doc:: drivers/gpu/drm/drm_ioctl.c
21   :doc: getunique and setversion story
22
23
24Primary 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
36Open-Source Userspace Requirements
37==================================
38
39The DRM subsystem has stricter requirements than most other kernel subsystems on
40what the userspace side for new uAPI needs to look like. This section here
41explains what exactly those requirements are, and why they exist.
42
43The short summary is that any addition of DRM uAPI requires corresponding
44open-sourced userspace patches, and those patches must be reviewed and ready for
45merging into a suitable and canonical upstream project.
46
47GFX devices (both display and render/GPU side) are really complex bits of
48hardware, with userspace and kernel by necessity having to work together really
49closely.  The interfaces, for rendering and modesetting, must be extremely wide
50and flexible, and therefore it is almost always impossible to precisely define
51them for every possible corner case. This in turn makes it really practically
52infeasible to differentiate between behaviour that's required by userspace, and
53which must not be changed to avoid regressions, and behaviour which is only an
54accidental artifact of the current implementation.
55
56Without access to the full source code of all userspace users that means it
57becomes impossible to change the implementation details, since userspace could
58depend upon the accidental behaviour of the current implementation in minute
59details. And debugging such regressions without access to source code is pretty
60much 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
72The other reason for requiring open-source userspace is uAPI review. Since the
73kernel and userspace parts of a GFX stack must work together so closely, code
74review can only assess whether a new interface achieves its goals by looking at
75both sides. Making sure that the interface indeed covers the use-case fully
76leads 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
97These are fairly steep requirements, but have grown out from years of shared
98pain and experience with uAPI added hastily, and almost always regretted about
99just as fast. GFX devices change really fast, requiring a paradigm shift and
100entire new set of uAPI interfaces every few years at least. Together with the
101Linux kernel's guarantee to keep existing userspace running for 10+ years this
102is already rather painful for the DRM subsystem, with multiple different uAPIs
103for the same thing co-existing. If we add a few more complete mistakes into the
104mix every year it would be entirely unmanageable.
105
106Render nodes
107============
108
109DRM core provides multiple character-devices for user-space to use.
110Depending on which device is opened, user-space can perform a different
111set of operations (mainly ioctls). The primary node is always created
112and called card<num>. Additionally, a currently unused control node,
113called controlD<num> is also created. The primary node provides all
114legacy operations and historically was the only interface used by
115userspace. With KMS, the control node was introduced. However, the
116planned KMS control interface has never been written and so the control
117node stays unused to date.
118
119With the increased use of offscreen renderers and GPGPU applications,
120clients no longer require running compositors or graphics servers to
121make use of a GPU. But the DRM API required unprivileged clients to
122authenticate to a DRM-Master prior to getting GPU access. To avoid this
123step and to grant clients GPU access without authenticating, render
124nodes were introduced. Render nodes solely serve render clients, that
125is, no modesetting or privileged ioctls can be issued on render nodes.
126Only non-global rendering commands are allowed. If a driver supports
127render nodes, it must advertise it via the DRIVER_RENDER DRM driver
128capability. If not supported, the primary node must be used for render
129clients together with the legacy drmAuth authentication procedure.
130
131If a driver advertises render node support, DRM core will create a
132separate render node called renderD<num>. There will be one render node
133per device. No ioctls except PRIME-related ioctls will be allowed on
134this node. Especially GEM_OPEN will be explicitly prohibited. Render
135nodes are designed to avoid the buffer-leaks, which occur if clients
136guess the flink names or mmap offsets on the legacy interface.
137Additionally to this basic interface, drivers must mark their
138driver-dependent render-only ioctls as DRM_RENDER_ALLOW so render
139clients can use them. Driver authors must be careful not to allow any
140privileged ioctls on render nodes.
141
142With render nodes, user-space can now control access to the render node
143via basic file-system access-modes. A running graphics server which
144authenticates clients on the privileged primary/legacy node is no longer
145required. Instead, a client can open the render node and is immediately
146granted GPU access. Communication between clients (or servers) is done
147via PRIME. FLINK from render node to legacy node is not supported. New
148clients must not use the insecure FLINK interface.
149
150Besides dropping all modeset/global ioctls, render nodes also drop the
151DRM-Master concept. There is no reason to associate render clients with
152a DRM-Master as they are independent of any graphics server. Besides,
153they must work without any running master, anyway. Drivers must be able
154to run without a master object if they support render nodes. If, on the
155other hand, a driver requires shared state between clients which is
156visible to user-space and accessible beyond open-file boundaries, they
157cannot support render nodes.
158
159Validating changes with IGT
160===========================
161
162There's a collection of tests that aims to cover the whole functionality of
163DRM drivers and that can be used to check that changes to DRM drivers or the
164core don't regress existing functionality. This test suite is called IGT and
165its code can be found in https://cgit.freedesktop.org/drm/igt-gpu-tools/.
166
167To build IGT, start by installing its build dependencies. In Debian-based
168systems::
169
170	# apt-get build-dep intel-gpu-tools
171
172And in Fedora-based systems::
173
174	# dnf builddep intel-gpu-tools
175
176Then clone the repository::
177
178	$ git clone git://anongit.freedesktop.org/drm/igt-gpu-tools
179
180Configure the build system and start the build::
181
182	$ cd igt-gpu-tools && ./autogen.sh && make -j6
183
184Download the piglit dependency::
185
186	$ ./scripts/run-tests.sh -d
187
188And run the tests::
189
190	$ ./scripts/run-tests.sh -t kms -t core -s
191
192run-tests.sh is a wrapper around piglit that will execute the tests matching
193the -t options. A report in HTML format will be available in
194./results/html/index.html. Results can be compared with piglit.
195
196VBlank event handling
197=====================
198
199The DRM core exposes two vertical blank related ioctls:
200
201DRM_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
206DRM_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
213This second part of the GPU Driver Developer's Guide documents driver
214code, implementation details and also all the driver-specific userspace
215interfaces. Especially since all hardware-acceleration interfaces to
216userspace are driver specific for efficiency and other reasons these
217interfaces can be rather substantial. Hence every driver has its own
218chapter.
219
220Testing and validation
221======================
222
223.. kernel-doc:: drivers/gpu/drm/drm_debugfs_crc.c
224   :doc: CRC ABI
225