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 __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
47namespace sdm {
48class HWInfoInterface;
49
50class HWDeviceDRM : public HWInterface {
51 public:
52  explicit HWDeviceDRM(BufferSyncHandler *buffer_sync_handler, BufferAllocator *buffer_allocator,
53                       HWInfoInterface *hw_info_intf);
54  virtual ~HWDeviceDRM() {}
55  virtual DisplayError Init();
56  virtual DisplayError Deinit();
57
58 protected:
59  // From HWInterface
60  virtual DisplayError GetActiveConfig(uint32_t *active_config);
61  virtual DisplayError GetNumDisplayAttributes(uint32_t *count);
62  virtual DisplayError GetDisplayAttributes(uint32_t index,
63                                            HWDisplayAttributes *display_attributes);
64  virtual DisplayError GetHWPanelInfo(HWPanelInfo *panel_info);
65  virtual DisplayError SetDisplayAttributes(uint32_t index);
66  virtual DisplayError SetDisplayAttributes(const HWDisplayAttributes &display_attributes);
67  virtual DisplayError GetConfigIndex(uint32_t mode, uint32_t *index);
68  virtual DisplayError PowerOn();
69  virtual DisplayError PowerOff();
70  virtual DisplayError Doze();
71  virtual DisplayError DozeSuspend();
72  virtual DisplayError Standby();
73  virtual DisplayError Validate(HWLayers *hw_layers);
74  virtual DisplayError Commit(HWLayers *hw_layers);
75  virtual DisplayError Flush();
76  virtual DisplayError GetPPFeaturesVersion(PPFeatureVersion *vers);
77  virtual DisplayError SetPPFeatures(PPFeaturesConfig *feature_list);
78  virtual DisplayError SetVSyncState(bool enable);
79  virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
80  virtual DisplayError SetDisplayMode(const HWDisplayMode hw_display_mode);
81  virtual DisplayError SetRefreshRate(uint32_t refresh_rate);
82  virtual DisplayError SetPanelBrightness(int level);
83  virtual DisplayError CachePanelBrightness(int level);
84  virtual DisplayError GetHWScanInfo(HWScanInfo *scan_info);
85  virtual DisplayError GetVideoFormat(uint32_t config_index, uint32_t *video_format);
86  virtual DisplayError GetMaxCEAFormat(uint32_t *max_cea_format);
87  virtual DisplayError SetCursorPosition(HWLayers *hw_layers, int x, int y);
88  virtual DisplayError OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level);
89  virtual DisplayError GetPanelBrightness(int *level);
90  virtual DisplayError SetAutoRefresh(bool enable) { return kErrorNone; }
91  virtual DisplayError SetS3DMode(HWS3DMode s3d_mode);
92  virtual DisplayError SetScaleLutConfig(HWScaleLutInfo *lut_info);
93  virtual DisplayError SetMixerAttributes(const HWMixerAttributes &mixer_attributes);
94  virtual DisplayError GetMixerAttributes(HWMixerAttributes *mixer_attributes);
95
96  enum {
97    kHWEventVSync,
98    kHWEventBlank,
99  };
100
101  static const int kMaxStringLength = 1024;
102  static const int kNumPhysicalDisplays = 2;
103  static const int kMaxSysfsCommandLength = 12;
104  static constexpr const char *kBrightnessNode =
105    "/sys/class/backlight/panel0-backlight/brightness";
106
107  DisplayError SetFormat(const LayerBufferFormat &source, uint32_t *target);
108  DisplayError SetStride(HWDeviceType device_type, LayerBufferFormat format, uint32_t width,
109                         uint32_t *target);
110  DisplayError PopulateDisplayAttributes();
111  void PopulateHWPanelInfo();
112  void GetHWDisplayPortAndMode();
113  void GetHWPanelMaxBrightness();
114  void ResetDisplayParams();
115  bool EnableHotPlugDetection(int enable);
116  void UpdateMixerAttributes();
117  void InitializeConfigs();
118  void SetBlending(const LayerBlending &source, sde_drm::DRMBlendType *target);
119  void SetSrcConfig(const LayerBuffer &input_buffer, uint32_t *config);
120  void SetRect(const LayerRect &source, sde_drm::DRMRect *target);
121  DisplayError DefaultCommit(HWLayers *hw_layers);
122  DisplayError AtomicCommit(HWLayers *hw_layers);
123  void SetupAtomic(HWLayers *hw_layers, bool validate);
124
125  class Registry {
126   public:
127    explicit Registry(BufferAllocator *buffer_allocator) : buffer_allocator_(buffer_allocator) {}
128    // Call on each validate and commit to register layer buffers
129    void RegisterCurrent(HWLayers *hw_layers);
130    // Call at the end of draw cycle to clear the next slot for business
131    void UnregisterNext();
132    // Call on display disconnect to release all gem handles and fb_ids
133    void Clear();
134    // Finds an fb_id corresponding to an fd in current map
135    uint32_t GetFbId(int fd);
136
137   private:
138    static const int kCycleDelay = 1;  // N cycle delay before destroy
139    // fd to fb_id map. fd is used as key only for a single draw cycle between
140    // prepare and commit. It should not be used for caching in future due to fd recycling
141    std::unordered_map<int, uint32_t> hashmap_[kCycleDelay] {};
142    int current_index_ = 0;
143    BufferAllocator *buffer_allocator_ = {};
144  };
145
146  HWResourceInfo hw_resource_ = {};
147  HWPanelInfo hw_panel_info_ = {};
148  HWInfoInterface *hw_info_intf_ = {};
149  BufferSyncHandler *buffer_sync_handler_ = {};
150  HWDeviceType device_type_ = {};
151  const char *device_name_ = {};
152  bool synchronous_commit_ = false;
153  HWDisplayAttributes display_attributes_ = {};
154  HWMixerAttributes mixer_attributes_ = {};
155  sde_drm::DRMManagerInterface *drm_mgr_intf_ = {};
156  sde_drm::DRMAtomicReqInterface *drm_atomic_intf_ = {};
157  sde_drm::DRMDisplayToken token_ = {};
158  drmModeModeInfo current_mode_ = {};
159  bool default_mode_ = false;
160  sde_drm::DRMConnectorInfo connector_info_ = {};
161  std::string interface_str_ = "DSI";
162  HWScaleDRM *hw_scale_ = {};
163  Registry registry_;
164};
165
166}  // namespace sdm
167
168#endif  // __HW_DEVICE_DRM_H__
169