1=============
2DRM Internals
3=============
4
5This chapter documents DRM internals relevant to driver authors and
6developers working to add support for the latest features to existing
7drivers.
8
9First, we go over some typical driver initialization requirements, like
10setting up command buffers, creating an initial output configuration,
11and initializing core services. Subsequent sections cover core internals
12in more detail, providing implementation notes and examples.
13
14The DRM layer provides several services to graphics drivers, many of
15them driven by the application interfaces it provides through libdrm,
16the library that wraps most of the DRM ioctls. These include vblank
17event handling, memory management, output management, framebuffer
18management, command submission & fencing, suspend/resume support, and
19DMA services.
20
21Driver Initialization
22=====================
23
24At the core of every DRM driver is a :c:type:`struct drm_driver
25<drm_driver>` structure. Drivers typically statically initialize
26a drm_driver structure, and then pass it to
27drm_dev_alloc() to allocate a device instance. After the
28device instance is fully initialized it can be registered (which makes
29it accessible from userspace) using drm_dev_register().
30
31The :c:type:`struct drm_driver <drm_driver>` structure
32contains static information that describes the driver and features it
33supports, and pointers to methods that the DRM core will call to
34implement the DRM API. We will first go through the :c:type:`struct
35drm_driver <drm_driver>` static information fields, and will
36then describe individual operations in details as they get used in later
37sections.
38
39Driver Information
40------------------
41
42Major, Minor and Patchlevel
43~~~~~~~~~~~~~~~~~~~~~~~~~~~
44
45int major; int minor; int patchlevel;
46The DRM core identifies driver versions by a major, minor and patch
47level triplet. The information is printed to the kernel log at
48initialization time and passed to userspace through the
49DRM_IOCTL_VERSION ioctl.
50
51The major and minor numbers are also used to verify the requested driver
52API version passed to DRM_IOCTL_SET_VERSION. When the driver API
53changes between minor versions, applications can call
54DRM_IOCTL_SET_VERSION to select a specific version of the API. If the
55requested major isn't equal to the driver major, or the requested minor
56is larger than the driver minor, the DRM_IOCTL_SET_VERSION call will
57return an error. Otherwise the driver's set_version() method will be
58called with the requested version.
59
60Name and Description
61~~~~~~~~~~~~~~~~~~~~
62
63char \*name; char \*desc; char \*date;
64The driver name is printed to the kernel log at initialization time,
65used for IRQ registration and passed to userspace through
66DRM_IOCTL_VERSION.
67
68The driver description is a purely informative string passed to
69userspace through the DRM_IOCTL_VERSION ioctl and otherwise unused by
70the kernel.
71
72Module Initialization
73---------------------
74
75.. kernel-doc:: include/drm/drm_module.h
76   :doc: overview
77
78Device Instance and Driver Handling
79-----------------------------------
80
81.. kernel-doc:: drivers/gpu/drm/drm_drv.c
82   :doc: driver instance overview
83
84.. kernel-doc:: include/drm/drm_device.h
85   :internal:
86
87.. kernel-doc:: include/drm/drm_drv.h
88   :internal:
89
90.. kernel-doc:: drivers/gpu/drm/drm_drv.c
91   :export:
92
93Driver Load
94-----------
95
96Component Helper Usage
97~~~~~~~~~~~~~~~~~~~~~~
98
99.. kernel-doc:: drivers/gpu/drm/drm_drv.c
100   :doc: component helper usage recommendations
101
102Memory Manager Initialization
103~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104
105Every DRM driver requires a memory manager which must be initialized at
106load time. DRM currently contains two memory managers, the Translation
107Table Manager (TTM) and the Graphics Execution Manager (GEM). This
108document describes the use of the GEM memory manager only. See ? for
109details.
110
111Miscellaneous Device Configuration
112~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113
114Another task that may be necessary for PCI devices during configuration
115is mapping the video BIOS. On many devices, the VBIOS describes device
116configuration, LCD panel timings (if any), and contains flags indicating
117device state. Mapping the BIOS can be done using the pci_map_rom()
118call, a convenience function that takes care of mapping the actual ROM,
119whether it has been shadowed into memory (typically at address 0xc0000)
120or exists on the PCI device in the ROM BAR. Note that after the ROM has
121been mapped and any necessary information has been extracted, it should
122be unmapped; on many devices, the ROM address decoder is shared with
123other BARs, so leaving it mapped could cause undesired behaviour like
124hangs or memory corruption.
125
126Managed Resources
127-----------------
128
129.. kernel-doc:: drivers/gpu/drm/drm_managed.c
130   :doc: managed resources
131
132.. kernel-doc:: drivers/gpu/drm/drm_managed.c
133   :export:
134
135.. kernel-doc:: include/drm/drm_managed.h
136   :internal:
137
138Open/Close, File Operations and IOCTLs
139======================================
140
141.. _drm_driver_fops:
142
143File Operations
144---------------
145
146.. kernel-doc:: drivers/gpu/drm/drm_file.c
147   :doc: file operations
148
149.. kernel-doc:: include/drm/drm_file.h
150   :internal:
151
152.. kernel-doc:: drivers/gpu/drm/drm_file.c
153   :export:
154
155Misc Utilities
156==============
157
158Printer
159-------
160
161.. kernel-doc:: include/drm/drm_print.h
162   :doc: print
163
164.. kernel-doc:: include/drm/drm_print.h
165   :internal:
166
167.. kernel-doc:: drivers/gpu/drm/drm_print.c
168   :export:
169
170Utilities
171---------
172
173.. kernel-doc:: include/drm/drm_util.h
174   :doc: drm utils
175
176.. kernel-doc:: include/drm/drm_util.h
177   :internal:
178
179
180Unit testing
181============
182
183KUnit
184-----
185
186KUnit (Kernel unit testing framework) provides a common framework for unit tests
187within the Linux kernel.
188
189This section covers the specifics for the DRM subsystem. For general information
190about KUnit, please refer to Documentation/dev-tools/kunit/start.rst.
191
192How to run the tests?
193~~~~~~~~~~~~~~~~~~~~~
194
195In order to facilitate running the test suite, a configuration file is present
196in ``drivers/gpu/drm/tests/.kunitconfig``. It can be used by ``kunit.py`` as
197follows:
198
199.. code-block:: bash
200
201	$ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/tests \
202		--kconfig_add CONFIG_VIRTIO_UML=y \
203		--kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y
204
205.. note::
206	The configuration included in ``.kunitconfig`` should be as generic as
207	possible.
208	``CONFIG_VIRTIO_UML`` and ``CONFIG_UML_PCI_OVER_VIRTIO`` are not
209	included in it because they are only required for User Mode Linux.
210
211KUnit Coverage Rules
212~~~~~~~~~~~~~~~~~~~~
213
214KUnit support is gradually added to the DRM framework and helpers. There's no
215general requirement for the framework and helpers to have KUnit tests at the
216moment. However, patches that are affecting a function or helper already
217covered by KUnit tests must provide tests if the change calls for one.
218
219Legacy Support Code
220===================
221
222The section very briefly covers some of the old legacy support code
223which is only used by old DRM drivers which have done a so-called
224shadow-attach to the underlying device instead of registering as a real
225driver. This also includes some of the old generic buffer management and
226command submission code. Do not use any of this in new and modern
227drivers.
228
229Legacy Suspend/Resume
230---------------------
231
232The DRM core provides some suspend/resume code, but drivers wanting full
233suspend/resume support should provide save() and restore() functions.
234These are called at suspend, hibernate, or resume time, and should
235perform any state save or restore required by your device across suspend
236or hibernate states.
237
238int (\*suspend) (struct drm_device \*, pm_message_t state); int
239(\*resume) (struct drm_device \*);
240Those are legacy suspend and resume methods which *only* work with the
241legacy shadow-attach driver registration functions. New driver should
242use the power management interface provided by their bus type (usually
243through the :c:type:`struct device_driver <device_driver>`
244dev_pm_ops) and set these methods to NULL.
245
246Legacy DMA Services
247-------------------
248
249This should cover how DMA mapping etc. is supported by the core. These
250functions are deprecated and should not be used.
251