1 /*
2 * Copyright (c) 2017, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *   * Redistributions of source code must retain the above copyright
8 *     notice, this list of conditions and the following disclaimer.
9 *   * Redistributions in binary form must reproduce the above
10 *     copyright notice, this list of conditions and the following
11 *     disclaimer in the documentation and/or other materials provided
12 *     with the distribution.
13 *   * Neither the name of The Linux Foundation nor the names of its
14 *     contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #ifndef __DRM_INTERFACE_H__
31 #define __DRM_INTERFACE_H__
32 
33 #include <map>
34 #include <string>
35 #include <utility>
36 #include <vector>
37 
38 #include "xf86drm.h"
39 #include "xf86drmMode.h"
40 
41 namespace sde_drm {
42 /*
43  * Drm Atomic Operation Codes
44  */
45 enum struct DRMOps {
46   /*
47    * Op: Sets plane source crop
48    * Arg: uint32_t - Plane ID
49    *      DRMRect  - Source Rectangle
50    */
51   PLANE_SET_SRC_RECT,
52   /*
53    * Op: Sets plane destination rect
54    * Arg: uint32_t - Plane ID
55    *      DRMRect - Dst Rectangle
56    */
57   PLANE_SET_DST_RECT,
58   /*
59    * Op: Sets plane zorder
60    * Arg: uint32_t - Plane ID
61    *      uint32_t - zorder
62    */
63   PLANE_SET_ZORDER,
64   /*
65    * Op: Sets plane rotation flags
66    * Arg: uint32_t - Plane ID
67    *      uint32_t - bit mask of rotation flags (See drm_mode.h for enums)
68    */
69   PLANE_SET_ROTATION,
70   /*
71    * Op: Sets plane alpha
72    * Arg: uint32_t - Plane ID
73    *      uint32_t - alpha value
74    */
75   PLANE_SET_ALPHA,
76   /*
77    * Op: Sets the blend type
78    * Arg: uint32_t - Plane ID
79    *      uint32_t - blend type (see DRMBlendType)
80    */
81   PLANE_SET_BLEND_TYPE,
82   /*
83    * Op: Sets horizontal decimation
84    * Arg: uint32_t - Plane ID
85    *      uint32_t - decimation factor
86    */
87   PLANE_SET_H_DECIMATION,
88   /*
89    * Op: Sets vertical decimation
90    * Arg: uint32_t - Plane ID
91    *      uint32_t - decimation factor
92    */
93   PLANE_SET_V_DECIMATION,
94   /*
95    * Op: Sets frame buffer ID for plane. Set together with CRTC.
96    * Arg: uint32_t - Plane ID
97    *      uint32_t - Framebuffer ID
98    */
99   PLANE_SET_FB_ID,
100   /*
101    * Op: Sets the crtc for this plane. Set together with FB_ID.
102    * Arg: uint32_t - Plane ID
103    *      uint32_t - CRTC ID
104    */
105   PLANE_SET_CRTC,
106   /*
107    * Op: Sets acquire fence for this plane's buffer. Set together with FB_ID, CRTC.
108    * Arg: uint32_t - Plane ID
109    *      uint32_t - Input fence
110    */
111   PLANE_SET_INPUT_FENCE,
112   /*
113    * Op: Activate or deactivate a CRTC
114    * Arg: uint32_t - CRTC ID
115    *      uint32_t - 1 to enable, 0 to disable
116    */
117   CRTC_SET_ACTIVE,
118   /*
119    * Op: Sets display mode
120    * Arg: uint32_t - CRTC ID
121    *      drmModeModeInfo* - Pointer to display mode
122    */
123   CRTC_SET_MODE,
124   /*
125    * Op: Sets an offset indicating when a release fence should be signalled.
126    * Arg: uint32_t - offset
127    *      0: non-speculative, default
128    *      1: speculative
129    */
130   CRTC_SET_OUTPUT_FENCE_OFFSET,
131   /*
132    * Op: Returns release fence for this frame. Should be called after Commit() on
133    * DRMAtomicReqInterface.
134    * Arg: uint32_t - CRTC ID
135    *      int * - Pointer to an integer that will hold the returned fence
136    */
137   CRTC_GET_RELEASE_FENCE,
138   /*
139    * Op: Sets PP feature
140    * Arg: uint32_t - CRTC ID
141    *      DRMPPFeatureInfo * - PP feature data pointer
142    */
143   CRTC_SET_POST_PROC,
144   /*
145    * Op: Returns retire fence for this commit. Should be called after Commit() on
146    * DRMAtomicReqInterface.
147    * Arg: uint32_t - Connector ID
148    *      int * - Pointer to an integer that will hold the returned fence
149    */
150   CONNECTOR_GET_RETIRE_FENCE,
151   /*
152    * Op: Sets writeback connector destination rect
153    * Arg: uint32_t - Connector ID
154    *      DRMRect - Dst Rectangle
155    */
156   CONNECTOR_SET_OUTPUT_RECT,
157   /*
158    * Op: Sets frame buffer ID for writeback connector.
159    * Arg: uint32_t - Connector ID
160    *      uint32_t - Framebuffer ID
161    */
162   CONNECTOR_SET_OUTPUT_FB_ID,
163 };
164 
165 enum struct DRMBlendType {
166   UNDEFINED = 0,
167   OPAQUE = 1,
168   PREMULTIPLIED = 2,
169   COVERAGE = 3,
170 };
171 
172 /* Display type to identify a suitable connector */
173 enum struct DRMDisplayType {
174   PERIPHERAL,
175   TV,
176   VIRTUAL,
177 };
178 
179 struct DRMRect {
180   uint32_t left;    // Left-most pixel coordinate.
181   uint32_t top;     // Top-most pixel coordinate.
182   uint32_t right;   // Right-most pixel coordinate.
183   uint32_t bottom;  // Bottom-most pixel coordinate.
184 };
185 
186 //------------------------------------------------------------------------
187 // DRM Info Query Types
188 //------------------------------------------------------------------------
189 
190 enum struct QSEEDVersion {
191   V1,
192   V2,
193   V3,
194 };
195 
196 /* Per CRTC Resource Info*/
197 struct DRMCrtcInfo {
198   bool has_src_split;
199   uint32_t max_blend_stages;
200   QSEEDVersion qseed_version;
201 };
202 
203 enum struct DRMPlaneType {
204   // Has CSC and scaling capability
205   VIG = 0,
206   // Has scaling capability but no CSC
207   RGB,
208   // No scaling support
209   DMA,
210   // Supports a small dimension and doesn't use a CRTC stage
211   CURSOR,
212   MAX,
213 };
214 
215 struct DRMPlaneTypeInfo {
216   // FourCC format enum and modifier
217   std::vector<std::pair<uint32_t, uint64_t>> formats_supported;
218   uint32_t max_linewidth;
219   uint32_t max_upscale;
220   uint32_t max_downscale;
221   uint32_t max_horizontal_deci;
222   uint32_t max_vertical_deci;
223 };
224 
225 /* All DRM Planes Info*/
226 struct DRMPlanesInfo {
227   // Plane id and plane type sorted by highest to lowest priority
228   std::vector<std::pair<uint32_t, DRMPlaneType>> planes;
229   // Plane type and type info
230   std::map<DRMPlaneType, DRMPlaneTypeInfo> types;
231 };
232 
233 enum struct DRMTopology {
234   UNKNOWN,  // To be compat with driver defs in sde_kms.h
235   SINGLE_LM,
236   DUAL_LM,
237   PPSPLIT,
238   DUAL_LM_MERGE,
239 };
240 
241 enum struct DRMPanelMode {
242   VIDEO,
243   COMMAND,
244 };
245 
246 /* Per Connector Info*/
247 struct DRMConnectorInfo {
248   uint32_t mmWidth;
249   uint32_t mmHeight;
250   uint32_t type;
251   uint32_t num_modes;
252   drmModeModeInfo *modes;
253   DRMTopology topology;
254   std::string panel_name;
255   DRMPanelMode panel_mode;
256   bool is_primary;
257   // Valid only if DRMPanelMode is VIDEO
258   bool dynamic_fps;
259   // FourCC format enum and modifier
260   std::vector<std::pair<uint32_t, uint64_t>> formats_supported;
261   // Valid only if type is DRM_MODE_CONNECTOR_VIRTUAL
262   uint32_t max_linewidth;
263 };
264 
265 /* Identifier token for a display */
266 struct DRMDisplayToken {
267   uint32_t conn_id;
268   uint32_t crtc_id;
269 };
270 
271 enum DRMPPFeatureID {
272   kFeaturePcc,
273   kFeatureIgc,
274   kFeaturePgc,
275   kFeatureMixerGc,
276   kFeaturePaV2,
277   kFeatureDither,
278   kFeatureGamut,
279   kFeaturePADither,
280   kPPFeaturesMax,
281 };
282 
283 enum DRMPPPropType {
284   kPropEnum,
285   kPropRange,
286   kPropBlob,
287   kPropTypeMax,
288 };
289 
290 struct DRMPPFeatureInfo {
291   DRMPPFeatureID id;
292   DRMPPPropType type;
293   uint32_t version;
294   uint32_t payload_size;
295   void *payload;
296 };
297 
298 /* DRM Atomic Request Property Set.
299  *
300  * Helper class to create and populate atomic properties of DRM components
301  * when rendered in DRM atomic mode */
302 class DRMAtomicReqInterface {
303  public:
~DRMAtomicReqInterface()304   virtual ~DRMAtomicReqInterface() {}
305   /* Perform request operation.
306    *
307    * [input]: opcode: operation code from DRMOps list.
308    *          var_arg: arguments for DRMOps's can differ in number and
309    *          data type. Refer above DRMOps to details.
310    * [return]: Error code if the API fails, 0 on success.
311    */
312   virtual int Perform(DRMOps opcode, ...) = 0;
313 
314   /*
315    * Commit the params set via Perform(). Also resets the properties after commit. Needs to be
316    * called every frame.
317    * [input]: synchronous: Determines if the call should block until a h/w flip
318    * [return]: Error code if the API fails, 0 on success.
319    */
320   virtual int Commit(bool synchronous) = 0;
321   /*
322    * Validate the params set via Perform().
323    * [return]: Error code if the API fails, 0 on success.
324    */
325   virtual int Validate() = 0;
326 };
327 
328 class DRMManagerInterface;
329 
330 /* Populates a singleton instance of DRMManager */
331 typedef int (*GetDRMManager)(int fd, DRMManagerInterface **intf);
332 
333 /* Destroy DRMManager instance */
334 typedef int (*DestroyDRMManager)();
335 
336 /*
337  * DRM Manager Interface - Any class which plans to implement helper function for vendor
338  * specific DRM driver implementation must implement the below interface routines to work
339  * with SDM.
340  */
341 
342 class DRMManagerInterface {
343  public:
~DRMManagerInterface()344   virtual ~DRMManagerInterface() {}
345 
346   /*
347    * Since SDM completely manages the planes. GetPlanesInfo will provide all
348    * the plane information.
349    * [output]: DRMPlanesInfo: Resource Info for planes.
350    */
351   virtual void GetPlanesInfo(DRMPlanesInfo *info) = 0;
352 
353   /*
354    * Will provide all the information of a selected crtc.
355    * [input]: Use crtc id 0 to obtain system wide info
356    * [output]: DRMCrtcInfo: Resource Info for the given CRTC id.
357    */
358   virtual void GetCrtcInfo(uint32_t crtc_id, DRMCrtcInfo *info) = 0;
359 
360   /*
361    * Will provide all the information of a selected connector.
362    * [output]: DRMConnectorInfo: Resource Info for the given connector id
363    */
364   virtual void GetConnectorInfo(uint32_t conn_id, DRMConnectorInfo *info) = 0;
365 
366   /*
367    * Will query post propcessing feature info of a CRTC.
368    * [output]: DRMPPFeatureInfo: CRTC post processing feature info
369    */
370    virtual void GetCrtcPPInfo(uint32_t crtc_id, DRMPPFeatureInfo &info) = 0;
371   /*
372    * Register a logical display to receive a token.
373    * Each display pipeline in DRM is identified by its CRTC and Connector(s).
374    * On display connect(bootup or hotplug), clients should invoke this interface to
375    * establish the pipeline for the display and should get a DisplayToken
376    * populated with crtc and connnector(s) id's. Here onwards, Client should
377    * use this token to represent the display for any Perform operations if
378    * needed.
379    *
380    * [input]: disp_type - Peripheral / TV / Virtual
381    * [output]: DRMDisplayToken - CRTC and Connector id's for the display
382    * [return]: 0 on success, a negative error value otherwise
383    */
384   virtual int RegisterDisplay(DRMDisplayType disp_type, DRMDisplayToken *tok) = 0;
385 
386   /* Client should invoke this interface on display disconnect.
387    * [input]: DRMDisplayToken - identifier for the display.
388    */
389   virtual void UnregisterDisplay(const DRMDisplayToken &token) = 0;
390 
391   /*
392    * Creates and returns an instance of DRMAtomicReqInterface corresponding to a display token
393    * returned as part of RegisterDisplay API. Needs to be called per display.
394    * [input]: DRMDisplayToken that identifies a display pipeline
395    * [output]: Pointer to an instance of DRMAtomicReqInterface.
396    * [return]: Error code if the API fails, 0 on success.
397    */
398   virtual int CreateAtomicReq(const DRMDisplayToken &token, DRMAtomicReqInterface **intf) = 0;
399 
400   /*
401    * Destroys the instance of DRMAtomicReqInterface
402    * [input]: Pointer to a DRMAtomicReqInterface
403    * [return]: Error code if the API fails, 0 on success.
404    */
405   virtual int DestroyAtomicReq(DRMAtomicReqInterface *intf) = 0;
406 };
407 }  // namespace sde_drm
408 #endif  // __DRM_INTERFACE_H__
409