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