18c2ecf20Sopenharmony_ci=============
28c2ecf20Sopenharmony_ciDRM Internals
38c2ecf20Sopenharmony_ci=============
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciThis chapter documents DRM internals relevant to driver authors and
68c2ecf20Sopenharmony_cidevelopers working to add support for the latest features to existing
78c2ecf20Sopenharmony_cidrivers.
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ciFirst, we go over some typical driver initialization requirements, like
108c2ecf20Sopenharmony_cisetting up command buffers, creating an initial output configuration,
118c2ecf20Sopenharmony_ciand initializing core services. Subsequent sections cover core internals
128c2ecf20Sopenharmony_ciin more detail, providing implementation notes and examples.
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciThe DRM layer provides several services to graphics drivers, many of
158c2ecf20Sopenharmony_cithem driven by the application interfaces it provides through libdrm,
168c2ecf20Sopenharmony_cithe library that wraps most of the DRM ioctls. These include vblank
178c2ecf20Sopenharmony_cievent handling, memory management, output management, framebuffer
188c2ecf20Sopenharmony_cimanagement, command submission & fencing, suspend/resume support, and
198c2ecf20Sopenharmony_ciDMA services.
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciDriver Initialization
228c2ecf20Sopenharmony_ci=====================
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciAt the core of every DRM driver is a :c:type:`struct drm_driver
258c2ecf20Sopenharmony_ci<drm_driver>` structure. Drivers typically statically initialize
268c2ecf20Sopenharmony_cia drm_driver structure, and then pass it to
278c2ecf20Sopenharmony_cidrm_dev_alloc() to allocate a device instance. After the
288c2ecf20Sopenharmony_cidevice instance is fully initialized it can be registered (which makes
298c2ecf20Sopenharmony_ciit accessible from userspace) using drm_dev_register().
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ciThe :c:type:`struct drm_driver <drm_driver>` structure
328c2ecf20Sopenharmony_cicontains static information that describes the driver and features it
338c2ecf20Sopenharmony_cisupports, and pointers to methods that the DRM core will call to
348c2ecf20Sopenharmony_ciimplement the DRM API. We will first go through the :c:type:`struct
358c2ecf20Sopenharmony_cidrm_driver <drm_driver>` static information fields, and will
368c2ecf20Sopenharmony_cithen describe individual operations in details as they get used in later
378c2ecf20Sopenharmony_cisections.
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ciDriver Information
408c2ecf20Sopenharmony_ci------------------
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ciMajor, Minor and Patchlevel
438c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ciint major; int minor; int patchlevel;
468c2ecf20Sopenharmony_ciThe DRM core identifies driver versions by a major, minor and patch
478c2ecf20Sopenharmony_cilevel triplet. The information is printed to the kernel log at
488c2ecf20Sopenharmony_ciinitialization time and passed to userspace through the
498c2ecf20Sopenharmony_ciDRM_IOCTL_VERSION ioctl.
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciThe major and minor numbers are also used to verify the requested driver
528c2ecf20Sopenharmony_ciAPI version passed to DRM_IOCTL_SET_VERSION. When the driver API
538c2ecf20Sopenharmony_cichanges between minor versions, applications can call
548c2ecf20Sopenharmony_ciDRM_IOCTL_SET_VERSION to select a specific version of the API. If the
558c2ecf20Sopenharmony_cirequested major isn't equal to the driver major, or the requested minor
568c2ecf20Sopenharmony_ciis larger than the driver minor, the DRM_IOCTL_SET_VERSION call will
578c2ecf20Sopenharmony_cireturn an error. Otherwise the driver's set_version() method will be
588c2ecf20Sopenharmony_cicalled with the requested version.
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciName, Description and Date
618c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cichar \*name; char \*desc; char \*date;
648c2ecf20Sopenharmony_ciThe driver name is printed to the kernel log at initialization time,
658c2ecf20Sopenharmony_ciused for IRQ registration and passed to userspace through
668c2ecf20Sopenharmony_ciDRM_IOCTL_VERSION.
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciThe driver description is a purely informative string passed to
698c2ecf20Sopenharmony_ciuserspace through the DRM_IOCTL_VERSION ioctl and otherwise unused by
708c2ecf20Sopenharmony_cithe kernel.
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciThe driver date, formatted as YYYYMMDD, is meant to identify the date of
738c2ecf20Sopenharmony_cithe latest modification to the driver. However, as most drivers fail to
748c2ecf20Sopenharmony_ciupdate it, its value is mostly useless. The DRM core prints it to the
758c2ecf20Sopenharmony_cikernel log at initialization time and passes it to userspace through the
768c2ecf20Sopenharmony_ciDRM_IOCTL_VERSION ioctl.
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ciDevice Instance and Driver Handling
798c2ecf20Sopenharmony_ci-----------------------------------
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/drm/drm_drv.c
828c2ecf20Sopenharmony_ci   :doc: driver instance overview
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci.. kernel-doc:: include/drm/drm_device.h
858c2ecf20Sopenharmony_ci   :internal:
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci.. kernel-doc:: include/drm/drm_drv.h
888c2ecf20Sopenharmony_ci   :internal:
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/drm/drm_drv.c
918c2ecf20Sopenharmony_ci   :export:
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciDriver Load
948c2ecf20Sopenharmony_ci-----------
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ciComponent Helper Usage
978c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/drm/drm_drv.c
1008c2ecf20Sopenharmony_ci   :doc: component helper usage recommendations
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciIRQ Helper Library
1038c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/drm/drm_irq.c
1068c2ecf20Sopenharmony_ci   :doc: irq helpers
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/drm/drm_irq.c
1098c2ecf20Sopenharmony_ci   :export:
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ciMemory Manager Initialization
1128c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciEvery DRM driver requires a memory manager which must be initialized at
1158c2ecf20Sopenharmony_ciload time. DRM currently contains two memory managers, the Translation
1168c2ecf20Sopenharmony_ciTable Manager (TTM) and the Graphics Execution Manager (GEM). This
1178c2ecf20Sopenharmony_cidocument describes the use of the GEM memory manager only. See ? for
1188c2ecf20Sopenharmony_cidetails.
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ciMiscellaneous Device Configuration
1218c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciAnother task that may be necessary for PCI devices during configuration
1248c2ecf20Sopenharmony_ciis mapping the video BIOS. On many devices, the VBIOS describes device
1258c2ecf20Sopenharmony_ciconfiguration, LCD panel timings (if any), and contains flags indicating
1268c2ecf20Sopenharmony_cidevice state. Mapping the BIOS can be done using the pci_map_rom()
1278c2ecf20Sopenharmony_cicall, a convenience function that takes care of mapping the actual ROM,
1288c2ecf20Sopenharmony_ciwhether it has been shadowed into memory (typically at address 0xc0000)
1298c2ecf20Sopenharmony_cior exists on the PCI device in the ROM BAR. Note that after the ROM has
1308c2ecf20Sopenharmony_cibeen mapped and any necessary information has been extracted, it should
1318c2ecf20Sopenharmony_cibe unmapped; on many devices, the ROM address decoder is shared with
1328c2ecf20Sopenharmony_ciother BARs, so leaving it mapped could cause undesired behaviour like
1338c2ecf20Sopenharmony_cihangs or memory corruption.
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ciManaged Resources
1368c2ecf20Sopenharmony_ci-----------------
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/drm/drm_managed.c
1398c2ecf20Sopenharmony_ci   :doc: managed resources
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/drm/drm_managed.c
1428c2ecf20Sopenharmony_ci   :export:
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci.. kernel-doc:: include/drm/drm_managed.h
1458c2ecf20Sopenharmony_ci   :internal:
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ciBus-specific Device Registration and PCI Support
1488c2ecf20Sopenharmony_ci------------------------------------------------
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciA number of functions are provided to help with device registration. The
1518c2ecf20Sopenharmony_cifunctions deal with PCI and platform devices respectively and are only
1528c2ecf20Sopenharmony_ciprovided for historical reasons. These are all deprecated and shouldn't
1538c2ecf20Sopenharmony_cibe used in new drivers. Besides that there's a few helpers for pci
1548c2ecf20Sopenharmony_cidrivers.
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/drm/drm_pci.c
1578c2ecf20Sopenharmony_ci   :export:
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ciOpen/Close, File Operations and IOCTLs
1608c2ecf20Sopenharmony_ci======================================
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci.. _drm_driver_fops:
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ciFile Operations
1658c2ecf20Sopenharmony_ci---------------
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/drm/drm_file.c
1688c2ecf20Sopenharmony_ci   :doc: file operations
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci.. kernel-doc:: include/drm/drm_file.h
1718c2ecf20Sopenharmony_ci   :internal:
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/drm/drm_file.c
1748c2ecf20Sopenharmony_ci   :export:
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ciMisc Utilities
1778c2ecf20Sopenharmony_ci==============
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ciPrinter
1808c2ecf20Sopenharmony_ci-------
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci.. kernel-doc:: include/drm/drm_print.h
1838c2ecf20Sopenharmony_ci   :doc: print
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci.. kernel-doc:: include/drm/drm_print.h
1868c2ecf20Sopenharmony_ci   :internal:
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/drm/drm_print.c
1898c2ecf20Sopenharmony_ci   :export:
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ciUtilities
1928c2ecf20Sopenharmony_ci---------
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci.. kernel-doc:: include/drm/drm_util.h
1958c2ecf20Sopenharmony_ci   :doc: drm utils
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci.. kernel-doc:: include/drm/drm_util.h
1988c2ecf20Sopenharmony_ci   :internal:
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ciLegacy Support Code
2028c2ecf20Sopenharmony_ci===================
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ciThe section very briefly covers some of the old legacy support code
2058c2ecf20Sopenharmony_ciwhich is only used by old DRM drivers which have done a so-called
2068c2ecf20Sopenharmony_cishadow-attach to the underlying device instead of registering as a real
2078c2ecf20Sopenharmony_cidriver. This also includes some of the old generic buffer management and
2088c2ecf20Sopenharmony_cicommand submission code. Do not use any of this in new and modern
2098c2ecf20Sopenharmony_cidrivers.
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ciLegacy Suspend/Resume
2128c2ecf20Sopenharmony_ci---------------------
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ciThe DRM core provides some suspend/resume code, but drivers wanting full
2158c2ecf20Sopenharmony_cisuspend/resume support should provide save() and restore() functions.
2168c2ecf20Sopenharmony_ciThese are called at suspend, hibernate, or resume time, and should
2178c2ecf20Sopenharmony_ciperform any state save or restore required by your device across suspend
2188c2ecf20Sopenharmony_cior hibernate states.
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ciint (\*suspend) (struct drm_device \*, pm_message_t state); int
2218c2ecf20Sopenharmony_ci(\*resume) (struct drm_device \*);
2228c2ecf20Sopenharmony_ciThose are legacy suspend and resume methods which *only* work with the
2238c2ecf20Sopenharmony_cilegacy shadow-attach driver registration functions. New driver should
2248c2ecf20Sopenharmony_ciuse the power management interface provided by their bus type (usually
2258c2ecf20Sopenharmony_cithrough the :c:type:`struct device_driver <device_driver>`
2268c2ecf20Sopenharmony_cidev_pm_ops) and set these methods to NULL.
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ciLegacy DMA Services
2298c2ecf20Sopenharmony_ci-------------------
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ciThis should cover how DMA mapping etc. is supported by the core. These
2328c2ecf20Sopenharmony_cifunctions are deprecated and should not be used.
233