1*4c5d0fc5SRodrigo Siqueira==================== 2*4c5d0fc5SRodrigo SiqueiraDC Programming Model 3*4c5d0fc5SRodrigo Siqueira==================== 4*4c5d0fc5SRodrigo Siqueira 5*4c5d0fc5SRodrigo SiqueiraIn the :ref:`Display Core Next (DCN) <dcn_overview>` and :ref:`DCN Block 6*4c5d0fc5SRodrigo Siqueira<dcn_blocks>` pages, you learned about the hardware components and how they 7*4c5d0fc5SRodrigo Siqueirainteract with each other. On this page, the focus is shifted to the display 8*4c5d0fc5SRodrigo Siqueiracode architecture. Hence, it is reasonable to remind the reader that the code 9*4c5d0fc5SRodrigo Siqueirain DC is shared with other OSes; for this reason, DC provides a set of 10*4c5d0fc5SRodrigo Siqueiraabstractions and operations to connect different APIs with the hardware 11*4c5d0fc5SRodrigo Siqueiraconfiguration. See DC as a service available for a Display Manager (amdgpu_dm) 12*4c5d0fc5SRodrigo Siqueirato access and configure DCN/DCE hardware (DCE is also part of DC, but for 13*4c5d0fc5SRodrigo Siqueirasimplicity's sake, this documentation only examines DCN). 14*4c5d0fc5SRodrigo Siqueira 15*4c5d0fc5SRodrigo Siqueira.. note:: 16*4c5d0fc5SRodrigo Siqueira For this page, we will use the term GPU to refers to dGPU and APU. 17*4c5d0fc5SRodrigo Siqueira 18*4c5d0fc5SRodrigo SiqueiraOverview 19*4c5d0fc5SRodrigo Siqueira======== 20*4c5d0fc5SRodrigo Siqueira 21*4c5d0fc5SRodrigo SiqueiraFrom the display hardware perspective, it is plausible to expect that if a 22*4c5d0fc5SRodrigo Siqueiraproblem is well-defined, it will probably be implemented at the hardware level. 23*4c5d0fc5SRodrigo SiqueiraOn the other hand, when there are multiple ways of achieving something without 24*4c5d0fc5SRodrigo Siqueiraa very well-defined scope, the solution is usually implemented as a policy at 25*4c5d0fc5SRodrigo Siqueirathe DC level. In other words, some policies are defined in the DC core 26*4c5d0fc5SRodrigo Siqueira(resource management, power optimization, image quality, etc.), and the others 27*4c5d0fc5SRodrigo Siqueiraimplemented in hardware are enabled via DC configuration. 28*4c5d0fc5SRodrigo Siqueira 29*4c5d0fc5SRodrigo SiqueiraIn terms of hardware management, DCN has multiple instances of the same block 30*4c5d0fc5SRodrigo Siqueira(e.g., HUBP, DPP, MPC, etc), and during the driver execution, it might be 31*4c5d0fc5SRodrigo Siqueiranecessary to use some of these instances. The core has policies in place for 32*4c5d0fc5SRodrigo Siqueirahandling those instances. Regarding resource management, the DC objective is 33*4c5d0fc5SRodrigo Siqueiraquite simple: minimize the hardware shuffle when the driver performs some 34*4c5d0fc5SRodrigo Siqueiraactions. When the state changes from A to B, the transition is considered 35*4c5d0fc5SRodrigo Siqueiraeasier to maneuver if the hardware resource is still used for the same set of 36*4c5d0fc5SRodrigo Siqueiradriver objects. Usually, adding and removing a resource to a `pipe_ctx` (more 37*4c5d0fc5SRodrigo Siqueiradetails below) is not a problem; however, moving a resource from one `pipe_ctx` 38*4c5d0fc5SRodrigo Siqueirato another should be avoided. 39*4c5d0fc5SRodrigo Siqueira 40*4c5d0fc5SRodrigo SiqueiraAnother area of influence for DC is power optimization, which has a myriad of 41*4c5d0fc5SRodrigo Siqueiraarrangement possibilities. In some way, just displaying an image via DCN should 42*4c5d0fc5SRodrigo Siqueirabe relatively straightforward; however, showing it with the best power 43*4c5d0fc5SRodrigo Siqueirafootprint is more desirable, but it has many associated challenges. 44*4c5d0fc5SRodrigo SiqueiraUnfortunately, there is no straight-forward analytic way to determine if a 45*4c5d0fc5SRodrigo Siqueiraconfiguration is the best one for the context due to the enormous variety of 46*4c5d0fc5SRodrigo Siqueiravariables related to this problem (e.g., many different DCN/DCE hardware 47*4c5d0fc5SRodrigo Siqueiraversions, different displays configurations, etc.) for this reason DC 48*4c5d0fc5SRodrigo Siqueiraimplements a dedicated library for trying some configuration and verify if it 49*4c5d0fc5SRodrigo Siqueirais possible to support it or not. This type of policy is extremely complex to 50*4c5d0fc5SRodrigo Siqueiracreate and maintain, and amdgpu driver relies on Display Mode Library (DML) to 51*4c5d0fc5SRodrigo Siqueiragenerate the best decisions. 52*4c5d0fc5SRodrigo Siqueira 53*4c5d0fc5SRodrigo SiqueiraIn summary, DC must deal with the complexity of handling multiple scenarios and 54*4c5d0fc5SRodrigo Siqueiradetermine policies to manage them. All of the above information is conveyed to 55*4c5d0fc5SRodrigo Siqueiragive the reader some idea about the complexity of driving a display from the 56*4c5d0fc5SRodrigo Siqueiradriver's perspective. This page hopes to allow the reader to better navigate 57*4c5d0fc5SRodrigo Siqueiraover the amdgpu display code. 58*4c5d0fc5SRodrigo Siqueira 59*4c5d0fc5SRodrigo SiqueiraDisplay Driver Architecture Overview 60*4c5d0fc5SRodrigo Siqueira==================================== 61*4c5d0fc5SRodrigo Siqueira 62*4c5d0fc5SRodrigo SiqueiraThe diagram below provides an overview of the display driver architecture; 63*4c5d0fc5SRodrigo Siqueiranotice it illustrates the software layers adopted by DC: 64*4c5d0fc5SRodrigo Siqueira 65*4c5d0fc5SRodrigo Siqueira.. kernel-figure:: dc-components.svg 66*4c5d0fc5SRodrigo Siqueira 67*4c5d0fc5SRodrigo SiqueiraThe first layer of the diagram is the high-level DC API represented by the 68*4c5d0fc5SRodrigo Siqueira`dc.h` file; below it are two big blocks represented by Core and Link. Next is 69*4c5d0fc5SRodrigo Siqueirathe hardware configuration block; the main file describing it is 70*4c5d0fc5SRodrigo Siqueirathe`hw_sequencer.h`, where the implementation of the callbacks can be found in 71*4c5d0fc5SRodrigo Siqueirathe hardware sequencer folder. Almost at the end, you can see the block level 72*4c5d0fc5SRodrigo SiqueiraAPI (`dc/inc/hw`), which represents each DCN low-level block, such as HUBP, 73*4c5d0fc5SRodrigo SiqueiraDPP, MPC, OPTC, etc. Notice on the left side of the diagram that we have a 74*4c5d0fc5SRodrigo Siqueiradifferent set of layers representing the interaction with the DMUB 75*4c5d0fc5SRodrigo Siqueiramicrocontroller. 76*4c5d0fc5SRodrigo Siqueira 77*4c5d0fc5SRodrigo SiqueiraBasic Objects 78*4c5d0fc5SRodrigo Siqueira------------- 79*4c5d0fc5SRodrigo Siqueira 80*4c5d0fc5SRodrigo SiqueiraThe below diagram outlines the basic display objects. In particular, pay 81*4c5d0fc5SRodrigo Siqueiraattention to the names in the boxes since they represent a data structure in 82*4c5d0fc5SRodrigo Siqueirathe driver: 83*4c5d0fc5SRodrigo Siqueira 84*4c5d0fc5SRodrigo Siqueira.. kernel-figure:: dc-arch-overview.svg 85*4c5d0fc5SRodrigo Siqueira 86*4c5d0fc5SRodrigo SiqueiraLet's start with the central block in the image, `dc`. The `dc` struct is 87*4c5d0fc5SRodrigo Siqueirainitialized per GPU; for example, one GPU has one `dc` instance, two GPUs have 88*4c5d0fc5SRodrigo Siqueiratwo `dc` instances, and so forth. In other words we have one 'dc' per 'amdgpu' 89*4c5d0fc5SRodrigo Siqueirainstance. In some ways, this object behaves like the `Singleton` pattern. 90*4c5d0fc5SRodrigo Siqueira 91*4c5d0fc5SRodrigo SiqueiraAfter the `dc` block in the diagram, you can see the `dc_link` component, which 92*4c5d0fc5SRodrigo Siqueirais a low-level abstraction for the connector. One interesting aspect of the 93*4c5d0fc5SRodrigo Siqueiraimage is that connectors are not part of the DCN block; they are defined by the 94*4c5d0fc5SRodrigo Siqueiraplatform/board and not by the SoC. The `dc_link` struct is the high-level data 95*4c5d0fc5SRodrigo Siqueiracontainer with information such as connected sinks, connection status, signal 96*4c5d0fc5SRodrigo Siqueiratypes, etc. After `dc_link`, there is the `dc_sink`, which is the object that 97*4c5d0fc5SRodrigo Siqueirarepresents the connected display. 98*4c5d0fc5SRodrigo Siqueira 99*4c5d0fc5SRodrigo Siqueira.. note:: 100*4c5d0fc5SRodrigo Siqueira For historical reasons, we used the name `dc_link`, which gives the 101*4c5d0fc5SRodrigo Siqueira wrong impression that this abstraction only deals with physical connections 102*4c5d0fc5SRodrigo Siqueira that the developer can easily manipulate. However, this also covers 103*4c5d0fc5SRodrigo Siqueira conections like eDP or cases where the output is connected to other devices. 104*4c5d0fc5SRodrigo Siqueira 105*4c5d0fc5SRodrigo SiqueiraThere are two structs that are not represented in the diagram since they were 106*4c5d0fc5SRodrigo Siqueiraelaborated in the DCN overview page (check the DCN block diagram :ref:`Display 107*4c5d0fc5SRodrigo SiqueiraCore Next (DCN) <dcn_overview>`); still, it is worth bringing back for this 108*4c5d0fc5SRodrigo Siqueiraoverview which is `dc_stream` and `dc_state`. The `dc_stream` stores many 109*4c5d0fc5SRodrigo Siqueiraproperties associated with the data transmission, but most importantly, it 110*4c5d0fc5SRodrigo Siqueirarepresents the data flow from the connector to the display. Next we have 111*4c5d0fc5SRodrigo Siqueira`dc_state`, which represents the logic state within the hardware at the moment; 112*4c5d0fc5SRodrigo Siqueira`dc_state` is composed of `dc_stream` and `dc_plane`. The `dc_stream` is the DC 113*4c5d0fc5SRodrigo Siqueiraversion of `drm_crtc` and represents the post-blending pipeline. 114*4c5d0fc5SRodrigo Siqueira 115*4c5d0fc5SRodrigo SiqueiraSpeaking of the `dc_plane` data structure (first part of the diagram), you can 116*4c5d0fc5SRodrigo Siqueirathink about it as an abstraction similar to `drm_plane` that represents the 117*4c5d0fc5SRodrigo Siqueirapre-blending portion of the pipeline. This image was probably processed by GFX 118*4c5d0fc5SRodrigo Siqueiraand is ready to be composited under a `dc_stream`. Normally, the driver may 119*4c5d0fc5SRodrigo Siqueirahave one or more `dc_plane` connected to the same `dc_stream`, which defines a 120*4c5d0fc5SRodrigo Siqueiracomposition at the DC level. 121*4c5d0fc5SRodrigo Siqueira 122*4c5d0fc5SRodrigo SiqueiraBasic Operations 123*4c5d0fc5SRodrigo Siqueira---------------- 124*4c5d0fc5SRodrigo Siqueira 125*4c5d0fc5SRodrigo SiqueiraNow that we have covered the basic objects, it is time to examine some of the 126*4c5d0fc5SRodrigo Siqueirabasic hardware/software operations. Let's start with the `dc_create()` 127*4c5d0fc5SRodrigo Siqueirafunction, which directly works with the `dc` data struct; this function behaves 128*4c5d0fc5SRodrigo Siqueiralike a constructor responsible for the basic software initialization and 129*4c5d0fc5SRodrigo Siqueirapreparing for enabling other parts of the API. It is important to highlight 130*4c5d0fc5SRodrigo Siqueirathat this operation does not touch any hardware configuration; it is only a 131*4c5d0fc5SRodrigo Siqueirasoftware initialization. 132*4c5d0fc5SRodrigo Siqueira 133*4c5d0fc5SRodrigo SiqueiraNext, we have the `dc_hardware_init()`, which also relies on the `dc` data 134*4c5d0fc5SRodrigo Siqueirastruct. Its main function is to put the hardware in a valid state. It is worth 135*4c5d0fc5SRodrigo Siqueirahighlighting that the hardware might initialize in an unknown state, and it is 136*4c5d0fc5SRodrigo Siqueiraa requirement to put it in a valid state; this function has multiple callbacks 137*4c5d0fc5SRodrigo Siqueirafor the hardware-specific initialization, whereas `dc_hardware_init` does the 138*4c5d0fc5SRodrigo Siqueirahardware initialization and is the first point where we touch hardware. 139*4c5d0fc5SRodrigo Siqueira 140*4c5d0fc5SRodrigo SiqueiraThe `dc_get_link_at_index` is an operation that depends on the `dc_link` data 141*4c5d0fc5SRodrigo Siqueirastructure. This function retrieves and enumerates all the `dc_links` available 142*4c5d0fc5SRodrigo Siqueiraon the device; this is required since this information is not part of the SoC 143*4c5d0fc5SRodrigo Siqueiradefinition but depends on the board configuration. As soon as the `dc_link` is 144*4c5d0fc5SRodrigo Siqueirainitialized, it is useful to figure out if any of them are already connected to 145*4c5d0fc5SRodrigo Siqueirathe display by using the `dc_link_detect()` function. After the driver figures 146*4c5d0fc5SRodrigo Siqueiraout if any display is connected to the device, the challenging phase starts: 147*4c5d0fc5SRodrigo Siqueiraconfiguring the monitor to show something. Nonetheless, dealing with the ideal 148*4c5d0fc5SRodrigo Siqueiraconfiguration is not a DC task since this is the Display Manager (`amdgpu_dm`) 149*4c5d0fc5SRodrigo Siqueiraresponsibility which in turn is responsible for dealing with the atomic 150*4c5d0fc5SRodrigo Siqueiracommits. The only interface DC provides to the configuration phase is the 151*4c5d0fc5SRodrigo Siqueirafunction `dc_validate_with_context` that receives the configuration information 152*4c5d0fc5SRodrigo Siqueiraand, based on that, validates whether the hardware can support it or not. It is 153*4c5d0fc5SRodrigo Siqueiraimportant to add that even if the display supports some specific configuration, 154*4c5d0fc5SRodrigo Siqueirait does not mean the DCN hardware can support it. 155*4c5d0fc5SRodrigo Siqueira 156*4c5d0fc5SRodrigo SiqueiraAfter the DM and DC agree upon the configuration, the stream configuration 157*4c5d0fc5SRodrigo Siqueiraphase starts. This task activates one or more `dc_stream` at this phase, and in 158*4c5d0fc5SRodrigo Siqueirathe best-case scenario, you might be able to turn the display on with a black 159*4c5d0fc5SRodrigo Siqueirascreen (it does not show anything yet since it does not have any plane 160*4c5d0fc5SRodrigo Siqueiraassociated with it). The final step would be to call the 161*4c5d0fc5SRodrigo Siqueira`dc_update_planes_and_stream,` which will add or remove planes. 162*4c5d0fc5SRodrigo Siqueira 163