1 /*
2 * Copyright (c) 2019, 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_CRTC_H__
31 #define __DRM_CRTC_H__
32 
33 #include <xf86drm.h>
34 #include <xf86drmMode.h>
35 #include <map>
36 #include <memory>
37 #include <vector>
38 #include <string>
39 #include <set>
40 #include <mutex>
41 
42 #include "drm_interface.h"
43 #include "drm_utils.h"
44 #include "drm_pp_manager.h"
45 #include "drm_property.h"
46 
47 namespace sde_drm {
48 
49 class DRMCrtc : public DRMObject {
50  public:
51   DRMCrtc(int fd, uint32_t crtc_index);
GetObjectId()52   uint32_t GetObjectId() override { return drm_crtc_->crtc_id; }
53   void InitAndParse(drmModeCrtc *crtc);
GetStatus()54   DRMStatus GetStatus() { return status_; }
55   void GetInfo(DRMCrtcInfo *info);
56   void SetModeBlobID(uint64_t blob_id);
57   bool ConfigureScalerLUT(uint32_t dir_lut_blob_id,
58                           uint32_t cir_lut_blob_id, uint32_t sep_lut_blob_id);
59   void PostValidate();
60   void PostCommit(bool success);
61   void Perform(DRMOps code, drmModeAtomicReq *req, va_list args);
GetIndex()62   int GetIndex() { return crtc_index_; }
63   void Dump();
64   void Lock();
65   void Unlock();
GetPPInfo(DRMPPFeatureInfo * info)66   void GetPPInfo(DRMPPFeatureInfo *info) {
67     if (pp_mgr_)
68       pp_mgr_->GetPPInfo(info);
69   }
70   ~DRMCrtc();
71 
72  private:
73   void ParseProperties();
74   void ParseCapabilities(uint64_t blob_id);
75   void ParseCompRatio(std::string line, bool real_time);
76   void SetROI(uint32_t num_roi, DRMRect *crtc_rois);
77   void SetSolidfillStages(const std::vector<DRMSolidfillStage> *solid_fills);
78   void ClearVotesCache();
79 
80   // Currently hardcoded to 10. In future we need to query bit depth from driver.
81   static const int kSolidFillHwBitDepth = 10;
82 
83   int fd_ = -1;
84   uint32_t crtc_index_ = {};
85   uint64_t mode_blob_id_ = 0;
86   drmModeCrtc *drm_crtc_ = {};
87   DRMStatus status_ = DRMStatus::FREE;
88   DRMCrtcInfo crtc_info_ = {};
89   DRMPropertyManager prop_mgr_ {};
90   bool is_lut_configured_ = false;
91   bool is_lut_validated_ = false;
92   bool is_lut_validation_in_progress_ = false;
93   struct sde_drm_roi_v1 roi_v1_;
94   struct sde_drm_dim_layer_v1  drm_dim_layer_v1_;
95   std::unique_ptr<DRMPPManager> pp_mgr_{};
96 };
97 
98 class DRMCrtcManager : public DRMObjectManager<DRMCrtc> {
99  public:
DRMCrtcManager(int fd)100   explicit DRMCrtcManager(int fd) : fd_(fd) {}
101   void Init(drmModeRes *res);
DeInit()102   void DeInit() {}
103   int Reserve(const std::set<uint32_t> &possible_crtc_indices, DRMDisplayToken *token);
104   void Free(DRMDisplayToken *token);
105   void Perform(DRMOps code, uint32_t obj_id, drmModeAtomicReq *req, va_list args);
106   int GetCrtcInfo(uint32_t crtc_id, DRMCrtcInfo *info);
107   void SetScalerLUT(const DRMScalerLUTInfo &lut_info);
108   void UnsetScalerLUT();
109   void GetPPInfo(uint32_t crtc_id, DRMPPFeatureInfo *info);
110   size_t ApplyDirtyProperties(drmModeAtomicReq *req, uint32_t crtc_id);
111 
112  private:
113   int fd_ = -1;
114   // GLobal Scaler LUT blobs
115   uint32_t dir_lut_blob_id_ = 0;
116   uint32_t cir_lut_blob_id_ = 0;
117   uint32_t sep_lut_blob_id_ = 0;
118   std::mutex lock_;
119 };
120 
121 }  // namespace sde_drm
122 
123 #endif  // __DRM_CRTC_H__
124