1*4882a593Smuzhiyun============= 2*4882a593SmuzhiyunDRM Internals 3*4882a593Smuzhiyun============= 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunThis chapter documents DRM internals relevant to driver authors and 6*4882a593Smuzhiyundevelopers working to add support for the latest features to existing 7*4882a593Smuzhiyundrivers. 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunFirst, we go over some typical driver initialization requirements, like 10*4882a593Smuzhiyunsetting up command buffers, creating an initial output configuration, 11*4882a593Smuzhiyunand initializing core services. Subsequent sections cover core internals 12*4882a593Smuzhiyunin more detail, providing implementation notes and examples. 13*4882a593Smuzhiyun 14*4882a593SmuzhiyunThe DRM layer provides several services to graphics drivers, many of 15*4882a593Smuzhiyunthem driven by the application interfaces it provides through libdrm, 16*4882a593Smuzhiyunthe library that wraps most of the DRM ioctls. These include vblank 17*4882a593Smuzhiyunevent handling, memory management, output management, framebuffer 18*4882a593Smuzhiyunmanagement, command submission & fencing, suspend/resume support, and 19*4882a593SmuzhiyunDMA services. 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunDriver Initialization 22*4882a593Smuzhiyun===================== 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunAt the core of every DRM driver is a :c:type:`struct drm_driver 25*4882a593Smuzhiyun<drm_driver>` structure. Drivers typically statically initialize 26*4882a593Smuzhiyuna drm_driver structure, and then pass it to 27*4882a593Smuzhiyundrm_dev_alloc() to allocate a device instance. After the 28*4882a593Smuzhiyundevice instance is fully initialized it can be registered (which makes 29*4882a593Smuzhiyunit accessible from userspace) using drm_dev_register(). 30*4882a593Smuzhiyun 31*4882a593SmuzhiyunThe :c:type:`struct drm_driver <drm_driver>` structure 32*4882a593Smuzhiyuncontains static information that describes the driver and features it 33*4882a593Smuzhiyunsupports, and pointers to methods that the DRM core will call to 34*4882a593Smuzhiyunimplement the DRM API. We will first go through the :c:type:`struct 35*4882a593Smuzhiyundrm_driver <drm_driver>` static information fields, and will 36*4882a593Smuzhiyunthen describe individual operations in details as they get used in later 37*4882a593Smuzhiyunsections. 38*4882a593Smuzhiyun 39*4882a593SmuzhiyunDriver Information 40*4882a593Smuzhiyun------------------ 41*4882a593Smuzhiyun 42*4882a593SmuzhiyunMajor, Minor and Patchlevel 43*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~ 44*4882a593Smuzhiyun 45*4882a593Smuzhiyunint major; int minor; int patchlevel; 46*4882a593SmuzhiyunThe DRM core identifies driver versions by a major, minor and patch 47*4882a593Smuzhiyunlevel triplet. The information is printed to the kernel log at 48*4882a593Smuzhiyuninitialization time and passed to userspace through the 49*4882a593SmuzhiyunDRM_IOCTL_VERSION ioctl. 50*4882a593Smuzhiyun 51*4882a593SmuzhiyunThe major and minor numbers are also used to verify the requested driver 52*4882a593SmuzhiyunAPI version passed to DRM_IOCTL_SET_VERSION. When the driver API 53*4882a593Smuzhiyunchanges between minor versions, applications can call 54*4882a593SmuzhiyunDRM_IOCTL_SET_VERSION to select a specific version of the API. If the 55*4882a593Smuzhiyunrequested major isn't equal to the driver major, or the requested minor 56*4882a593Smuzhiyunis larger than the driver minor, the DRM_IOCTL_SET_VERSION call will 57*4882a593Smuzhiyunreturn an error. Otherwise the driver's set_version() method will be 58*4882a593Smuzhiyuncalled with the requested version. 59*4882a593Smuzhiyun 60*4882a593SmuzhiyunName, Description and Date 61*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~ 62*4882a593Smuzhiyun 63*4882a593Smuzhiyunchar \*name; char \*desc; char \*date; 64*4882a593SmuzhiyunThe driver name is printed to the kernel log at initialization time, 65*4882a593Smuzhiyunused for IRQ registration and passed to userspace through 66*4882a593SmuzhiyunDRM_IOCTL_VERSION. 67*4882a593Smuzhiyun 68*4882a593SmuzhiyunThe driver description is a purely informative string passed to 69*4882a593Smuzhiyunuserspace through the DRM_IOCTL_VERSION ioctl and otherwise unused by 70*4882a593Smuzhiyunthe kernel. 71*4882a593Smuzhiyun 72*4882a593SmuzhiyunThe driver date, formatted as YYYYMMDD, is meant to identify the date of 73*4882a593Smuzhiyunthe latest modification to the driver. However, as most drivers fail to 74*4882a593Smuzhiyunupdate it, its value is mostly useless. The DRM core prints it to the 75*4882a593Smuzhiyunkernel log at initialization time and passes it to userspace through the 76*4882a593SmuzhiyunDRM_IOCTL_VERSION ioctl. 77*4882a593Smuzhiyun 78*4882a593SmuzhiyunDevice Instance and Driver Handling 79*4882a593Smuzhiyun----------------------------------- 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun.. kernel-doc:: drivers/gpu/drm/drm_drv.c 82*4882a593Smuzhiyun :doc: driver instance overview 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun.. kernel-doc:: include/drm/drm_device.h 85*4882a593Smuzhiyun :internal: 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun.. kernel-doc:: include/drm/drm_drv.h 88*4882a593Smuzhiyun :internal: 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun.. kernel-doc:: drivers/gpu/drm/drm_drv.c 91*4882a593Smuzhiyun :export: 92*4882a593Smuzhiyun 93*4882a593SmuzhiyunDriver Load 94*4882a593Smuzhiyun----------- 95*4882a593Smuzhiyun 96*4882a593SmuzhiyunComponent Helper Usage 97*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~ 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun.. kernel-doc:: drivers/gpu/drm/drm_drv.c 100*4882a593Smuzhiyun :doc: component helper usage recommendations 101*4882a593Smuzhiyun 102*4882a593SmuzhiyunIRQ Helper Library 103*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~ 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun.. kernel-doc:: drivers/gpu/drm/drm_irq.c 106*4882a593Smuzhiyun :doc: irq helpers 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun.. kernel-doc:: drivers/gpu/drm/drm_irq.c 109*4882a593Smuzhiyun :export: 110*4882a593Smuzhiyun 111*4882a593SmuzhiyunMemory Manager Initialization 112*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 113*4882a593Smuzhiyun 114*4882a593SmuzhiyunEvery DRM driver requires a memory manager which must be initialized at 115*4882a593Smuzhiyunload time. DRM currently contains two memory managers, the Translation 116*4882a593SmuzhiyunTable Manager (TTM) and the Graphics Execution Manager (GEM). This 117*4882a593Smuzhiyundocument describes the use of the GEM memory manager only. See ? for 118*4882a593Smuzhiyundetails. 119*4882a593Smuzhiyun 120*4882a593SmuzhiyunMiscellaneous Device Configuration 121*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 122*4882a593Smuzhiyun 123*4882a593SmuzhiyunAnother task that may be necessary for PCI devices during configuration 124*4882a593Smuzhiyunis mapping the video BIOS. On many devices, the VBIOS describes device 125*4882a593Smuzhiyunconfiguration, LCD panel timings (if any), and contains flags indicating 126*4882a593Smuzhiyundevice state. Mapping the BIOS can be done using the pci_map_rom() 127*4882a593Smuzhiyuncall, a convenience function that takes care of mapping the actual ROM, 128*4882a593Smuzhiyunwhether it has been shadowed into memory (typically at address 0xc0000) 129*4882a593Smuzhiyunor exists on the PCI device in the ROM BAR. Note that after the ROM has 130*4882a593Smuzhiyunbeen mapped and any necessary information has been extracted, it should 131*4882a593Smuzhiyunbe unmapped; on many devices, the ROM address decoder is shared with 132*4882a593Smuzhiyunother BARs, so leaving it mapped could cause undesired behaviour like 133*4882a593Smuzhiyunhangs or memory corruption. 134*4882a593Smuzhiyun 135*4882a593SmuzhiyunManaged Resources 136*4882a593Smuzhiyun----------------- 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun.. kernel-doc:: drivers/gpu/drm/drm_managed.c 139*4882a593Smuzhiyun :doc: managed resources 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun.. kernel-doc:: drivers/gpu/drm/drm_managed.c 142*4882a593Smuzhiyun :export: 143*4882a593Smuzhiyun 144*4882a593Smuzhiyun.. kernel-doc:: include/drm/drm_managed.h 145*4882a593Smuzhiyun :internal: 146*4882a593Smuzhiyun 147*4882a593SmuzhiyunBus-specific Device Registration and PCI Support 148*4882a593Smuzhiyun------------------------------------------------ 149*4882a593Smuzhiyun 150*4882a593SmuzhiyunA number of functions are provided to help with device registration. The 151*4882a593Smuzhiyunfunctions deal with PCI and platform devices respectively and are only 152*4882a593Smuzhiyunprovided for historical reasons. These are all deprecated and shouldn't 153*4882a593Smuzhiyunbe used in new drivers. Besides that there's a few helpers for pci 154*4882a593Smuzhiyundrivers. 155*4882a593Smuzhiyun 156*4882a593Smuzhiyun.. kernel-doc:: drivers/gpu/drm/drm_pci.c 157*4882a593Smuzhiyun :export: 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunOpen/Close, File Operations and IOCTLs 160*4882a593Smuzhiyun====================================== 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun.. _drm_driver_fops: 163*4882a593Smuzhiyun 164*4882a593SmuzhiyunFile Operations 165*4882a593Smuzhiyun--------------- 166*4882a593Smuzhiyun 167*4882a593Smuzhiyun.. kernel-doc:: drivers/gpu/drm/drm_file.c 168*4882a593Smuzhiyun :doc: file operations 169*4882a593Smuzhiyun 170*4882a593Smuzhiyun.. kernel-doc:: include/drm/drm_file.h 171*4882a593Smuzhiyun :internal: 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun.. kernel-doc:: drivers/gpu/drm/drm_file.c 174*4882a593Smuzhiyun :export: 175*4882a593Smuzhiyun 176*4882a593SmuzhiyunMisc Utilities 177*4882a593Smuzhiyun============== 178*4882a593Smuzhiyun 179*4882a593SmuzhiyunPrinter 180*4882a593Smuzhiyun------- 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun.. kernel-doc:: include/drm/drm_print.h 183*4882a593Smuzhiyun :doc: print 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun.. kernel-doc:: include/drm/drm_print.h 186*4882a593Smuzhiyun :internal: 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun.. kernel-doc:: drivers/gpu/drm/drm_print.c 189*4882a593Smuzhiyun :export: 190*4882a593Smuzhiyun 191*4882a593SmuzhiyunUtilities 192*4882a593Smuzhiyun--------- 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun.. kernel-doc:: include/drm/drm_util.h 195*4882a593Smuzhiyun :doc: drm utils 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun.. kernel-doc:: include/drm/drm_util.h 198*4882a593Smuzhiyun :internal: 199*4882a593Smuzhiyun 200*4882a593Smuzhiyun 201*4882a593SmuzhiyunLegacy Support Code 202*4882a593Smuzhiyun=================== 203*4882a593Smuzhiyun 204*4882a593SmuzhiyunThe section very briefly covers some of the old legacy support code 205*4882a593Smuzhiyunwhich is only used by old DRM drivers which have done a so-called 206*4882a593Smuzhiyunshadow-attach to the underlying device instead of registering as a real 207*4882a593Smuzhiyundriver. This also includes some of the old generic buffer management and 208*4882a593Smuzhiyuncommand submission code. Do not use any of this in new and modern 209*4882a593Smuzhiyundrivers. 210*4882a593Smuzhiyun 211*4882a593SmuzhiyunLegacy Suspend/Resume 212*4882a593Smuzhiyun--------------------- 213*4882a593Smuzhiyun 214*4882a593SmuzhiyunThe DRM core provides some suspend/resume code, but drivers wanting full 215*4882a593Smuzhiyunsuspend/resume support should provide save() and restore() functions. 216*4882a593SmuzhiyunThese are called at suspend, hibernate, or resume time, and should 217*4882a593Smuzhiyunperform any state save or restore required by your device across suspend 218*4882a593Smuzhiyunor hibernate states. 219*4882a593Smuzhiyun 220*4882a593Smuzhiyunint (\*suspend) (struct drm_device \*, pm_message_t state); int 221*4882a593Smuzhiyun(\*resume) (struct drm_device \*); 222*4882a593SmuzhiyunThose are legacy suspend and resume methods which *only* work with the 223*4882a593Smuzhiyunlegacy shadow-attach driver registration functions. New driver should 224*4882a593Smuzhiyunuse the power management interface provided by their bus type (usually 225*4882a593Smuzhiyunthrough the :c:type:`struct device_driver <device_driver>` 226*4882a593Smuzhiyundev_pm_ops) and set these methods to NULL. 227*4882a593Smuzhiyun 228*4882a593SmuzhiyunLegacy DMA Services 229*4882a593Smuzhiyun------------------- 230*4882a593Smuzhiyun 231*4882a593SmuzhiyunThis should cover how DMA mapping etc. is supported by the core. These 232*4882a593Smuzhiyunfunctions are deprecated and should not be used. 233