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