1 /*
2 * Copyright (c) 2017-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 __HW_DEVICE_DRM_H__
31 #define __HW_DEVICE_DRM_H__
32 
33 #include <drm_interface.h>
34 #include <errno.h>
35 #include <pthread.h>
36 #include <xf86drmMode.h>
37 #include <string>
38 #include <unordered_map>
39 #include <vector>
40 #include <memory>
41 
42 #include "hw_interface.h"
43 #include "hw_scale_drm.h"
44 #include "hw_color_manager_drm.h"
45 
46 #define IOCTL_LOGE(ioctl, type) \
47   DLOGE("ioctl %s, device = %d errno = %d, desc = %s", #ioctl, type, errno, strerror(errno))
48 
49 #define UI_FBID_LIMIT 3
50 #define VIDEO_FBID_LIMIT 16
51 #define OFFLINE_ROTATOR_FBID_LIMIT 2
52 
53 namespace sdm {
54 class HWInfoInterface;
55 
56 struct SDECsc {
57   struct sde_drm_csc_v1 csc_v1 = {};
58   // More here, maybe in a union
59 };
60 
61 class HWDeviceDRM : public HWInterface {
62  public:
63   HWDeviceDRM(BufferSyncHandler *buffer_sync_handler, BufferAllocator *buffer_allocator,
64                        HWInfoInterface *hw_info_intf);
~HWDeviceDRM()65   virtual ~HWDeviceDRM() {}
66   virtual DisplayError Init();
67   virtual DisplayError Deinit();
68   void GetDRMDisplayToken(sde_drm::DRMDisplayToken *token) const;
IsPrimaryDisplay()69   bool IsPrimaryDisplay() const { return hw_panel_info_.is_primary_panel; }
70 
71  protected:
72   // From HWInterface
73   virtual DisplayError GetDisplayId(int32_t *display_id);
74   virtual DisplayError GetActiveConfig(uint32_t *active_config);
GetDefaultConfig(uint32_t * default_config)75   virtual DisplayError GetDefaultConfig(uint32_t *default_config) { return kErrorNotSupported; }
76   virtual DisplayError GetNumDisplayAttributes(uint32_t *count);
77   virtual DisplayError GetDisplayAttributes(uint32_t index,
78                                             HWDisplayAttributes *display_attributes);
79   virtual DisplayError GetHWPanelInfo(HWPanelInfo *panel_info);
80   virtual DisplayError SetDisplayAttributes(uint32_t index);
81   virtual DisplayError SetDisplayAttributes(const HWDisplayAttributes &display_attributes);
82   virtual DisplayError GetConfigIndex(char *mode, uint32_t *index);
83   virtual DisplayError PowerOn(const HWQosData &qos_data, int *release_fence);
84   virtual DisplayError PowerOff(bool teardown);
85   virtual DisplayError Doze(const HWQosData &qos_data, int *release_fence);
86   virtual DisplayError DozeSuspend(const HWQosData &qos_data, int *release_fence);
87   virtual DisplayError Standby();
88   virtual DisplayError Validate(HWLayers *hw_layers);
89   virtual DisplayError Commit(HWLayers *hw_layers);
90   virtual DisplayError Flush(HWLayers *hw_layers);
91   virtual DisplayError GetPPFeaturesVersion(PPFeatureVersion *vers);
92   virtual DisplayError SetPPFeatures(PPFeaturesConfig *feature_list);
93   // This API is no longer supported, expectation is to call the correct API on HWEvents
94   virtual DisplayError SetVSyncState(bool enable);
95   virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
96   virtual DisplayError SetDisplayMode(const HWDisplayMode hw_display_mode);
97   virtual DisplayError SetRefreshRate(uint32_t refresh_rate);
98   virtual DisplayError SetPanelBrightness(int level);
99   virtual DisplayError GetHWScanInfo(HWScanInfo *scan_info);
100   virtual DisplayError GetVideoFormat(uint32_t config_index, uint32_t *video_format);
101   virtual DisplayError GetMaxCEAFormat(uint32_t *max_cea_format);
102   virtual DisplayError SetCursorPosition(HWLayers *hw_layers, int x, int y);
103   virtual DisplayError OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level);
104   virtual DisplayError GetPanelBrightness(int *level);
SetAutoRefresh(bool enable)105   virtual DisplayError SetAutoRefresh(bool enable) { autorefresh_ = enable; return kErrorNone; }
106   virtual DisplayError SetS3DMode(HWS3DMode s3d_mode);
107   virtual DisplayError SetScaleLutConfig(HWScaleLutInfo *lut_info);
108   virtual DisplayError UnsetScaleLutConfig();
109   virtual DisplayError SetMixerAttributes(const HWMixerAttributes &mixer_attributes);
110   virtual DisplayError GetMixerAttributes(HWMixerAttributes *mixer_attributes);
111   virtual void InitializeConfigs();
112   virtual DisplayError DumpDebugData();
113   virtual void PopulateHWPanelInfo();
SetDppsFeature(void * payload,size_t size)114   virtual DisplayError SetDppsFeature(void *payload, size_t size) { return kErrorNotSupported; }
GetDppsFeatureInfo(void * payload,size_t size)115   virtual DisplayError GetDppsFeatureInfo(void *payload, size_t size) { return kErrorNotSupported; }
TeardownConcurrentWriteback(void)116   virtual DisplayError TeardownConcurrentWriteback(void) { return kErrorNotSupported; }
HandleSecureEvent(SecureEvent secure_event,HWLayers * hw_layers)117   virtual DisplayError HandleSecureEvent(SecureEvent secure_event, HWLayers *hw_layers) {
118     return kErrorNotSupported;
119   }
ControlIdlePowerCollapse(bool enable,bool synchronous)120   virtual DisplayError ControlIdlePowerCollapse(bool enable, bool synchronous) {
121     return kErrorNotSupported;
122   }
SetDisplayDppsAdROI(void * payload)123   virtual DisplayError SetDisplayDppsAdROI(void *payload) { return kErrorNotSupported; }
124   virtual DisplayError SetDynamicDSIClock(uint64_t bit_clk_rate);
125   virtual DisplayError GetDynamicDSIClock(uint64_t *bit_clk_rate);
126   virtual DisplayError GetDisplayIdentificationData(uint8_t *out_port, uint32_t *out_data_size,
127                                                     uint8_t *out_data);
128 
129   enum {
130     kHWEventVSync,
131     kHWEventBlank,
132   };
133 
134   static const int kMaxStringLength = 1024;
135   static const int kNumPhysicalDisplays = 2;
136   static const int kMaxSysfsCommandLength = 12;
137   static constexpr const char *kBrightnessNode =
138     "/sys/class/backlight/panel0-backlight/brightness";
139 
140   DisplayError SetFormat(const LayerBufferFormat &source, uint32_t *target);
141   DisplayError SetStride(HWDeviceType device_type, LayerBufferFormat format, uint32_t width,
142                          uint32_t *target);
143   DisplayError PopulateDisplayAttributes(uint32_t index);
144   void GetHWDisplayPortAndMode();
145   void GetHWPanelMaxBrightness();
146   bool EnableHotPlugDetection(int enable);
147   void UpdateMixerAttributes();
148   void SetSolidfillStages();
149   void AddSolidfillStage(const HWSolidfillStage &sf, uint32_t plane_alpha);
150   void ClearSolidfillStages();
151   void SetBlending(const LayerBlending &source, sde_drm::DRMBlendType *target);
152   void SetSrcConfig(const LayerBuffer &input_buffer, const HWRotatorMode &mode, uint32_t *config);
153   void SelectCscType(const LayerBuffer &input_buffer, sde_drm::DRMCscType *type);
154   void SetRect(const LayerRect &source, sde_drm::DRMRect *target);
155   void SetRotation(LayerTransform transform, const HWRotatorMode &mode, uint32_t* rot_bit_mask);
156   DisplayError DefaultCommit(HWLayers *hw_layers);
157   DisplayError AtomicCommit(HWLayers *hw_layers);
158   void SetupAtomic(HWLayers *hw_layers, bool validate);
159   void SetSecureConfig(const LayerBuffer &input_buffer, sde_drm::DRMSecureMode *fb_secure_mode,
160                        sde_drm::DRMSecurityLevel *security_level);
IsResolutionSwitchEnabled()161   bool IsResolutionSwitchEnabled() const { return resolution_switch_enabled_; }
162   void SetTopology(sde_drm::DRMTopology drm_topology, HWTopology *hw_topology);
163   void SetMultiRectMode(const uint32_t flags, sde_drm::DRMMultiRectMode *target);
164   void SetSsppTonemapFeatures(HWPipeInfo *pipe_info);
165   void SetDGMCsc(const HWPipeCscInfo &dgm_csc_info, SDECsc *csc);
166   void SetDGMCscV1(const HWCsc &dgm_csc, sde_drm_csc_v1 *csc_v1);
167   void SetSsppLutFeatures(HWPipeInfo *pipe_info);
168   void AddDimLayerIfNeeded();
169   DisplayError NullCommit(bool synchronous, bool retain_planes);
170   void DumpConnectorModeInfo();
171   void SetFullROI();
172   void SetQOSData(const HWQosData &qos_data);
173 
174   class Registry {
175    public:
176     explicit Registry(BufferAllocator *buffer_allocator);
177     // Called on each Validate and Commit to map the handle_id to fb_id of each layer buffer.
178     void Register(HWLayers *hw_layers);
179     // Called on display disconnect to clear output buffer map and remove fb_ids.
180     void Clear();
181     // Create the fd_id for the given buffer.
182     int CreateFbId(LayerBuffer *buffer, uint32_t *fb_id);
183     // Find handle_id in the layer map. Else create fb_id and add <handle_id,fb_id> in map.
184     void MapBufferToFbId(Layer* layer, LayerBuffer* buffer);
185     // Find handle_id in output buffer map. Else create fb_id and add <handle_id,fb_id> in map.
186     void MapOutputBufferToFbId(LayerBuffer* buffer);
187     // Find fb_id for given handle_id in the layer map.
188     uint32_t GetFbId(Layer *layer, uint64_t handle_id);
189     // Find fb_id for given handle_id in output buffer map.
190     uint32_t GetOutputFbId(uint64_t handle_id);
191 
192    private:
193     bool disable_fbid_cache_ = false;
194     std::unordered_map<uint64_t, std::shared_ptr<LayerBufferObject>> output_buffer_map_ {};
195     BufferAllocator *buffer_allocator_ = {};
196     uint8_t fbid_cache_limit_ = UI_FBID_LIMIT;
197   };
198 
199  protected:
200   const char *device_name_ = {};
201   bool default_mode_ = false;
202   int32_t display_id_ = -1;
203   sde_drm::DRMDisplayType disp_type_ = {};
204   HWInfoInterface *hw_info_intf_ = {};
205   BufferSyncHandler *buffer_sync_handler_ = {};
206   int dev_fd_ = -1;
207   Registry registry_;
208   sde_drm::DRMDisplayToken token_ = {};
209   HWResourceInfo hw_resource_ = {};
210   HWPanelInfo hw_panel_info_ = {};
211   HWScaleDRM *hw_scale_ = {};
212   sde_drm::DRMManagerInterface *drm_mgr_intf_ = {};
213   sde_drm::DRMAtomicReqInterface *drm_atomic_intf_ = {};
214   std::vector<HWDisplayAttributes> display_attributes_ = {};
215   uint32_t current_mode_index_ = 0;
216   sde_drm::DRMConnectorInfo connector_info_ = {};
217   bool first_cycle_ = true;
218   int64_t release_fence_ = -1;
219   int64_t retire_fence_ = -1;
220   HWMixerAttributes mixer_attributes_ = {};
221   std::vector<sde_drm::DRMSolidfillStage> solid_fills_ {};
222   bool secure_display_active_ = false;
223   uint64_t debug_dump_count_ = 0;
224   bool synchronous_commit_ = false;
225   uint32_t topology_control_ = 0;
226   uint32_t vrefresh_ = 0;
227   uint64_t bit_clk_rate_ = 0;
228   bool update_mode_ = false;
229 
230  private:
231   std::string interface_str_ = "DSI";
232   bool resolution_switch_enabled_ = false;
233   bool autorefresh_ = false;
234   std::unique_ptr<HWColorManagerDrm> hw_color_mgr_ = {};
235   bool pending_doze_ = false;
236 };
237 
238 }  // namespace sdm
239 
240 #endif  // __HW_DEVICE_DRM_H__
241