122554020SJani Nikula============= 2ca00c2b9SJani NikulaDRM Internals 3ca00c2b9SJani Nikula============= 4ca00c2b9SJani Nikula 5ca00c2b9SJani NikulaThis chapter documents DRM internals relevant to driver authors and 6ca00c2b9SJani Nikuladevelopers working to add support for the latest features to existing 7ca00c2b9SJani Nikuladrivers. 8ca00c2b9SJani Nikula 9ca00c2b9SJani NikulaFirst, we go over some typical driver initialization requirements, like 10ca00c2b9SJani Nikulasetting up command buffers, creating an initial output configuration, 11ca00c2b9SJani Nikulaand initializing core services. Subsequent sections cover core internals 12ca00c2b9SJani Nikulain more detail, providing implementation notes and examples. 13ca00c2b9SJani Nikula 14ca00c2b9SJani NikulaThe DRM layer provides several services to graphics drivers, many of 15ca00c2b9SJani Nikulathem driven by the application interfaces it provides through libdrm, 16ca00c2b9SJani Nikulathe library that wraps most of the DRM ioctls. These include vblank 17ca00c2b9SJani Nikulaevent handling, memory management, output management, framebuffer 18ca00c2b9SJani Nikulamanagement, command submission & fencing, suspend/resume support, and 19ca00c2b9SJani NikulaDMA services. 20ca00c2b9SJani Nikula 21ca00c2b9SJani NikulaDriver Initialization 2222554020SJani Nikula===================== 23ca00c2b9SJani Nikula 24ca00c2b9SJani NikulaAt the core of every DRM driver is a :c:type:`struct drm_driver 25ca00c2b9SJani Nikula<drm_driver>` structure. Drivers typically statically initialize 26ca00c2b9SJani Nikulaa drm_driver structure, and then pass it to 276acc942cSDaniel Vetterdrm_dev_alloc() to allocate a device instance. After the 28ca00c2b9SJani Nikuladevice instance is fully initialized it can be registered (which makes 296acc942cSDaniel Vetterit accessible from userspace) using drm_dev_register(). 30ca00c2b9SJani Nikula 31ca00c2b9SJani NikulaThe :c:type:`struct drm_driver <drm_driver>` structure 32ca00c2b9SJani Nikulacontains static information that describes the driver and features it 33ca00c2b9SJani Nikulasupports, and pointers to methods that the DRM core will call to 34ca00c2b9SJani Nikulaimplement the DRM API. We will first go through the :c:type:`struct 35ca00c2b9SJani Nikuladrm_driver <drm_driver>` static information fields, and will 36ca00c2b9SJani Nikulathen describe individual operations in details as they get used in later 37ca00c2b9SJani Nikulasections. 38ca00c2b9SJani Nikula 39ca00c2b9SJani NikulaDriver Information 4022554020SJani Nikula------------------ 41ca00c2b9SJani Nikula 42ca00c2b9SJani NikulaMajor, Minor and Patchlevel 432fa91d15SJani Nikula~~~~~~~~~~~~~~~~~~~~~~~~~~~ 44ca00c2b9SJani Nikula 45ca00c2b9SJani Nikulaint major; int minor; int patchlevel; 46ca00c2b9SJani NikulaThe DRM core identifies driver versions by a major, minor and patch 47ca00c2b9SJani Nikulalevel triplet. The information is printed to the kernel log at 48ca00c2b9SJani Nikulainitialization time and passed to userspace through the 49ca00c2b9SJani NikulaDRM_IOCTL_VERSION ioctl. 50ca00c2b9SJani Nikula 51ca00c2b9SJani NikulaThe major and minor numbers are also used to verify the requested driver 52ca00c2b9SJani NikulaAPI version passed to DRM_IOCTL_SET_VERSION. When the driver API 53ca00c2b9SJani Nikulachanges between minor versions, applications can call 54ca00c2b9SJani NikulaDRM_IOCTL_SET_VERSION to select a specific version of the API. If the 55ca00c2b9SJani Nikularequested major isn't equal to the driver major, or the requested minor 56ca00c2b9SJani Nikulais larger than the driver minor, the DRM_IOCTL_SET_VERSION call will 57ca00c2b9SJani Nikulareturn an error. Otherwise the driver's set_version() method will be 58ca00c2b9SJani Nikulacalled with the requested version. 59ca00c2b9SJani Nikula 607fb8af67SJani NikulaName and Description 617fb8af67SJani Nikula~~~~~~~~~~~~~~~~~~~~ 62ca00c2b9SJani Nikula 63ca00c2b9SJani Nikulachar \*name; char \*desc; char \*date; 64ca00c2b9SJani NikulaThe driver name is printed to the kernel log at initialization time, 65ca00c2b9SJani Nikulaused for IRQ registration and passed to userspace through 66ca00c2b9SJani NikulaDRM_IOCTL_VERSION. 67ca00c2b9SJani Nikula 68ca00c2b9SJani NikulaThe driver description is a purely informative string passed to 69ca00c2b9SJani Nikulauserspace through the DRM_IOCTL_VERSION ioctl and otherwise unused by 70ca00c2b9SJani Nikulathe kernel. 71ca00c2b9SJani Nikula 7252506b09SThomas ZimmermannModule Initialization 7352506b09SThomas Zimmermann--------------------- 7452506b09SThomas Zimmermann 7552506b09SThomas Zimmermann.. kernel-doc:: include/drm/drm_module.h 7652506b09SThomas Zimmermann :doc: overview 7752506b09SThomas Zimmermann 78ca00c2b9SJani NikulaDevice Instance and Driver Handling 7922554020SJani Nikula----------------------------------- 80ca00c2b9SJani Nikula 81ca00c2b9SJani Nikula.. kernel-doc:: drivers/gpu/drm/drm_drv.c 82ca00c2b9SJani Nikula :doc: driver instance overview 83ca00c2b9SJani Nikula 843214a166SDaniel Vetter.. kernel-doc:: include/drm/drm_device.h 853214a166SDaniel Vetter :internal: 863214a166SDaniel Vetter 876c4789edSDaniel Vetter.. kernel-doc:: include/drm/drm_drv.h 886c4789edSDaniel Vetter :internal: 896c4789edSDaniel Vetter 901ea35768SDaniel Vetter.. kernel-doc:: drivers/gpu/drm/drm_drv.c 911ea35768SDaniel Vetter :export: 921ea35768SDaniel Vetter 93ca00c2b9SJani NikulaDriver Load 9422554020SJani Nikula----------- 95ca00c2b9SJani Nikula 9686ab67dfSDaniel VetterComponent Helper Usage 9786ab67dfSDaniel Vetter~~~~~~~~~~~~~~~~~~~~~~ 9886ab67dfSDaniel Vetter 9986ab67dfSDaniel Vetter.. kernel-doc:: drivers/gpu/drm/drm_drv.c 10086ab67dfSDaniel Vetter :doc: component helper usage recommendations 101ca00c2b9SJani Nikula 102ca00c2b9SJani NikulaMemory Manager Initialization 1032fa91d15SJani Nikula~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 104ca00c2b9SJani Nikula 105ca00c2b9SJani NikulaEvery DRM driver requires a memory manager which must be initialized at 106ca00c2b9SJani Nikulaload time. DRM currently contains two memory managers, the Translation 107ca00c2b9SJani NikulaTable Manager (TTM) and the Graphics Execution Manager (GEM). This 108ca00c2b9SJani Nikuladocument describes the use of the GEM memory manager only. See ? for 109ca00c2b9SJani Nikuladetails. 110ca00c2b9SJani Nikula 111ca00c2b9SJani NikulaMiscellaneous Device Configuration 1122fa91d15SJani Nikula~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 113ca00c2b9SJani Nikula 114ca00c2b9SJani NikulaAnother task that may be necessary for PCI devices during configuration 115ca00c2b9SJani Nikulais mapping the video BIOS. On many devices, the VBIOS describes device 116ca00c2b9SJani Nikulaconfiguration, LCD panel timings (if any), and contains flags indicating 117ca00c2b9SJani Nikuladevice state. Mapping the BIOS can be done using the pci_map_rom() 118ca00c2b9SJani Nikulacall, a convenience function that takes care of mapping the actual ROM, 119ca00c2b9SJani Nikulawhether it has been shadowed into memory (typically at address 0xc0000) 120ca00c2b9SJani Nikulaor exists on the PCI device in the ROM BAR. Note that after the ROM has 121ca00c2b9SJani Nikulabeen mapped and any necessary information has been extracted, it should 122ca00c2b9SJani Nikulabe unmapped; on many devices, the ROM address decoder is shared with 123ca00c2b9SJani Nikulaother BARs, so leaving it mapped could cause undesired behaviour like 124ca00c2b9SJani Nikulahangs or memory corruption. 125ca00c2b9SJani Nikula 126c6603c74SDaniel VetterManaged Resources 127c6603c74SDaniel Vetter----------------- 128c6603c74SDaniel Vetter 129c6603c74SDaniel Vetter.. kernel-doc:: drivers/gpu/drm/drm_managed.c 130c6603c74SDaniel Vetter :doc: managed resources 131c6603c74SDaniel Vetter 1329e1ed9fbSDaniel Vetter.. kernel-doc:: drivers/gpu/drm/drm_managed.c 1339e1ed9fbSDaniel Vetter :export: 1349e1ed9fbSDaniel Vetter 1359e1ed9fbSDaniel Vetter.. kernel-doc:: include/drm/drm_managed.h 1369e1ed9fbSDaniel Vetter :internal: 1379e1ed9fbSDaniel Vetter 138ca00c2b9SJani NikulaOpen/Close, File Operations and IOCTLs 13922554020SJani Nikula====================================== 140ca00c2b9SJani Nikula 141bb2eaba6SDaniel Vetter.. _drm_driver_fops: 142bb2eaba6SDaniel Vetter 143ca00c2b9SJani NikulaFile Operations 14422554020SJani Nikula--------------- 145ca00c2b9SJani Nikula 1469acdac68SDaniel Vetter.. kernel-doc:: drivers/gpu/drm/drm_file.c 147ca00c2b9SJani Nikula :doc: file operations 148ca00c2b9SJani Nikula 149b93658f8SDaniel Vetter.. kernel-doc:: include/drm/drm_file.h 150b93658f8SDaniel Vetter :internal: 151b93658f8SDaniel Vetter 1529acdac68SDaniel Vetter.. kernel-doc:: drivers/gpu/drm/drm_file.c 153ca00c2b9SJani Nikula :export: 154ca00c2b9SJani Nikula 155d8187177SRob ClarkMisc Utilities 156d8187177SRob Clark============== 157d8187177SRob Clark 158d8187177SRob ClarkPrinter 159d8187177SRob Clark------- 160d8187177SRob Clark 161d8187177SRob Clark.. kernel-doc:: include/drm/drm_print.h 162d8187177SRob Clark :doc: print 163d8187177SRob Clark 164d8187177SRob Clark.. kernel-doc:: include/drm/drm_print.h 165d8187177SRob Clark :internal: 166d8187177SRob Clark 1672d5e836dSDaniel Vetter.. kernel-doc:: drivers/gpu/drm/drm_print.c 168d8187177SRob Clark :export: 169d8187177SRob Clark 170e9eafcb5SSam RavnborgUtilities 171e9eafcb5SSam Ravnborg--------- 172e9eafcb5SSam Ravnborg 173e9eafcb5SSam Ravnborg.. kernel-doc:: include/drm/drm_util.h 174e9eafcb5SSam Ravnborg :doc: drm utils 175e9eafcb5SSam Ravnborg 176e9eafcb5SSam Ravnborg.. kernel-doc:: include/drm/drm_util.h 177e9eafcb5SSam Ravnborg :internal: 178e9eafcb5SSam Ravnborg 179d8187177SRob Clark 1806fde8eecSJosé ExpósitoUnit testing 1816fde8eecSJosé Expósito============ 1826fde8eecSJosé Expósito 1836fde8eecSJosé ExpósitoKUnit 1846fde8eecSJosé Expósito----- 1856fde8eecSJosé Expósito 1866fde8eecSJosé ExpósitoKUnit (Kernel unit testing framework) provides a common framework for unit tests 1876fde8eecSJosé Expósitowithin the Linux kernel. 1886fde8eecSJosé Expósito 1896fde8eecSJosé ExpósitoThis section covers the specifics for the DRM subsystem. For general information 1906fde8eecSJosé Expósitoabout KUnit, please refer to Documentation/dev-tools/kunit/start.rst. 1916fde8eecSJosé Expósito 1926fde8eecSJosé ExpósitoHow to run the tests? 1936fde8eecSJosé Expósito~~~~~~~~~~~~~~~~~~~~~ 1946fde8eecSJosé Expósito 1956fde8eecSJosé ExpósitoIn order to facilitate running the test suite, a configuration file is present 1966fde8eecSJosé Expósitoin ``drivers/gpu/drm/tests/.kunitconfig``. It can be used by ``kunit.py`` as 1976fde8eecSJosé Expósitofollows: 1986fde8eecSJosé Expósito 1996fde8eecSJosé Expósito.. code-block:: bash 2006fde8eecSJosé Expósito 2016fde8eecSJosé Expósito $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/tests \ 2026fde8eecSJosé Expósito --kconfig_add CONFIG_VIRTIO_UML=y \ 2036fde8eecSJosé Expósito --kconfig_add CONFIG_UML_PCI_OVER_VIRTIO=y 2046fde8eecSJosé Expósito 2056fde8eecSJosé Expósito.. note:: 2066fde8eecSJosé Expósito The configuration included in ``.kunitconfig`` should be as generic as 2076fde8eecSJosé Expósito possible. 2086fde8eecSJosé Expósito ``CONFIG_VIRTIO_UML`` and ``CONFIG_UML_PCI_OVER_VIRTIO`` are not 2096fde8eecSJosé Expósito included in it because they are only required for User Mode Linux. 2106fde8eecSJosé Expósito 211*099b79f9SMaxime RipardKUnit Coverage Rules 212*099b79f9SMaxime Ripard~~~~~~~~~~~~~~~~~~~~ 213*099b79f9SMaxime Ripard 214*099b79f9SMaxime RipardKUnit support is gradually added to the DRM framework and helpers. There's no 215*099b79f9SMaxime Ripardgeneral requirement for the framework and helpers to have KUnit tests at the 216*099b79f9SMaxime Ripardmoment. However, patches that are affecting a function or helper already 217*099b79f9SMaxime Ripardcovered by KUnit tests must provide tests if the change calls for one. 2186fde8eecSJosé Expósito 219ca00c2b9SJani NikulaLegacy Support Code 22022554020SJani Nikula=================== 221ca00c2b9SJani Nikula 222ca00c2b9SJani NikulaThe section very briefly covers some of the old legacy support code 223ca00c2b9SJani Nikulawhich is only used by old DRM drivers which have done a so-called 224ca00c2b9SJani Nikulashadow-attach to the underlying device instead of registering as a real 225ca00c2b9SJani Nikuladriver. This also includes some of the old generic buffer management and 226ca00c2b9SJani Nikulacommand submission code. Do not use any of this in new and modern 227ca00c2b9SJani Nikuladrivers. 228ca00c2b9SJani Nikula 229ca00c2b9SJani NikulaLegacy Suspend/Resume 23022554020SJani Nikula--------------------- 231ca00c2b9SJani Nikula 232ca00c2b9SJani NikulaThe DRM core provides some suspend/resume code, but drivers wanting full 233ca00c2b9SJani Nikulasuspend/resume support should provide save() and restore() functions. 234ca00c2b9SJani NikulaThese are called at suspend, hibernate, or resume time, and should 235ca00c2b9SJani Nikulaperform any state save or restore required by your device across suspend 236ca00c2b9SJani Nikulaor hibernate states. 237ca00c2b9SJani Nikula 238ca00c2b9SJani Nikulaint (\*suspend) (struct drm_device \*, pm_message_t state); int 239ca00c2b9SJani Nikula(\*resume) (struct drm_device \*); 240ca00c2b9SJani NikulaThose are legacy suspend and resume methods which *only* work with the 241ca00c2b9SJani Nikulalegacy shadow-attach driver registration functions. New driver should 242ca00c2b9SJani Nikulause the power management interface provided by their bus type (usually 243ca00c2b9SJani Nikulathrough the :c:type:`struct device_driver <device_driver>` 244ca00c2b9SJani Nikuladev_pm_ops) and set these methods to NULL. 245ca00c2b9SJani Nikula 246ca00c2b9SJani NikulaLegacy DMA Services 24722554020SJani Nikula------------------- 248ca00c2b9SJani Nikula 249ca00c2b9SJani NikulaThis should cover how DMA mapping etc. is supported by the core. These 250ca00c2b9SJani Nikulafunctions are deprecated and should not be used. 251