summaryrefslogtreecommitdiff
path: root/sde-drm/drm_panel_feature_mgr.cpp
blob: 51230daa9213ec19536d1f26e101331434d612a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* Copyright (c) 2021, The Linux Foundataion. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above
*       copyright notice, this list of conditions and the following
*       disclaimer in the documentation and/or other materials provided
*       with the distribution.
*     * Neither the name of The Linux Foundation nor the names of its
*       contributors may be used to endorse or promote products derived
*       from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

#include <sstream>
#include <errno.h>
#include <string>
#include <drm_logger.h>
#include <cstring>
#include <regex>
#include <inttypes.h>

#include "drm_panel_feature_mgr.h"

#define __CLASS__ "DRMPanelFeatureMgr"

namespace sde_drm {

using std::map;
using std::vector;
using std::mutex;
using std::lock_guard;

static DRMPanelFeatureMgr panel_feature_mgr;

DRMPanelFeatureMgrIntf *GetPanelFeatureManagerIntf() {
  return &panel_feature_mgr;
}

void DRMPanelFeatureMgr::Init(int fd, drmModeRes* res) {
  lock_guard<mutex> lock(lock_);

  if (!res || (fd < 0)) {
    DRM_LOGE("Invalid arguments for init - fd %d and DRM resources pointer 0x%p", fd, (void *)res);
    return;
  }

  drm_res_ = res;
  dev_fd_ = fd;

  for (int i = 0; i < res->count_crtcs; i++) {
    drmModeCrtc *crtc = drmModeGetCrtc(dev_fd_, res->crtcs[i]);
    if (crtc) {
      int err = InitObjectProps(crtc->crtc_id, DRM_MODE_OBJECT_CRTC);
      if (err) {
        DRM_LOGE("Failed to get crtc props %d", crtc->crtc_id);
      }
      drmModeFreeCrtc(crtc);
    }
  }

  for (int i = 0; i < res->count_connectors; i++) {
    drmModeConnector *conn = drmModeGetConnector(dev_fd_, res->connectors[i]);
    if (conn) {
      int err = InitObjectProps(conn->connector_id, DRM_MODE_OBJECT_CONNECTOR);
      if (err) {
        DRM_LOGE("Failed to get conn %d properties", conn->connector_id);
      }
      drmModeFreeConnector(conn);
    }
  }

  drm_property_map_[kDRMPanelFeatureDsppRCInfo] = DRMProperty::DSPP_CAPABILITIES;
  drm_property_map_[kDRMPanelFeatureRCInit] = DRMProperty::DSPP_RC_MASK_V1;
  drm_prop_type_map_[kDRMPanelFeatureRCInit] = DRMPropType::kPropBlob;
  drm_prop_type_map_[kDRMPanelFeatureDsppRCInfo] = DRMPropType::kPropRange;


  feature_info_tbl_[kDRMPanelFeatureRCInit] = DRMPanelFeatureInfo {
      kDRMPanelFeatureRCInit, DRM_MODE_OBJECT_CRTC, UINT32_MAX, 1, sizeof(msm_rc_mask_cfg), 0};

  feature_info_tbl_[kDRMPanelFeatureDsppRCInfo] = DRMPanelFeatureInfo {
    kDRMPanelFeatureDsppRCInfo, DRM_MODE_OBJECT_CRTC, UINT32_MAX, 1, 64, 0};
}

void DRMPanelFeatureMgr::DeInit() {
  int ret = 0;
  for (int i = 0; i < kDRMPanelFeatureMax; i++) {
    DRMPanelFeatureID prop_id = static_cast<DRMPanelFeatureID>(i);
    if (drm_prop_blob_ids_map_[prop_id]) {
      ret = drmModeDestroyPropertyBlob(dev_fd_, drm_prop_blob_ids_map_[prop_id]);
      if (ret) {
        DRM_LOGE("failed to destroy blob for feature %d, ret = %d", prop_id, ret);
        return;
      } else {
        drm_prop_blob_ids_map_[prop_id] = 0;
      }
    }
  }
}

int DRMPanelFeatureMgr::InitObjectProps(int obj_id, int obj_type) {
  if (dev_fd_ < 0 || obj_id < 0) {
    DRM_LOGE("Invalid dev_fd_ %d or crtc_id %d", dev_fd_, obj_id);
    return -EINVAL;
  }

  drmModeObjectProperties *props =
          drmModeObjectGetProperties(dev_fd_, obj_id, obj_type);
  if (!props || !props->props || !props->prop_values) {
    DRM_LOGE("Failed to get props for obj_id:%d obj_type:%d", obj_id, obj_type);
    drmModeFreeObjectProperties(props);
    return -EINVAL;
  }

  for (uint32_t j = 0; j < props->count_props; j++) {
    drmModePropertyRes *info = drmModeGetProperty(dev_fd_, props->props[j]);
    if (!info) {
      continue;
    }

    std::string property_name(info->name);
    DRMProperty prop_enum = prop_mgr_.GetPropertyEnum(property_name);
    if (prop_enum == DRMProperty::INVALID) {
      DRM_LOGD("DRMProperty %s missing from global property mapping", info->name);
      drmModeFreeProperty(info);
      continue;
    }

    prop_mgr_.SetPropertyId(prop_enum, info->prop_id);
    drmModeFreeProperty(info);
  }

  drmModeFreeObjectProperties(props);
  return 0;
}

void DRMPanelFeatureMgr::ParseDsppCapabilities(uint32_t blob_id, std::vector<int> *values,
                                            uint32_t *size, const std::string str) {
  drmModePropertyBlobRes *blob = drmModeGetPropertyBlob(dev_fd_, blob_id);
  if (!blob) {
    DRM_LOGW("Unable to find blob for id %d", blob_id);
    return;
  }

  if (!blob->data) {
    DRM_LOGW("Invalid blob - no data for for blob-id %d", blob_id);
    return;
  }

  char *fmt_str = new char[blob->length + 1];
  std::memcpy(fmt_str, blob->data, blob->length);
  fmt_str[blob->length] = '\0';
  std::stringstream stream(fmt_str);
  std::string line = {};
  // Search for panel feature property pattern. Which is defined as rc0=1, rc1=1
  const std::regex exp(str + "(\\d+)=1");
  std::smatch sm;
  while (std::getline(stream, line)) {
    std::regex_match(line, sm, exp);
    // smatch shall include full line as a match followed by the hw block # as a match
    if (sm.size() == 2) {
      std::string tmpstr(sm[1]);
      int temp = atoi(tmpstr.c_str());  // atoi safe to use due to regex success
      values->push_back(temp);
    }
  }

  *size = sizeof(int) * values->size();
  delete[] fmt_str;
}

void DRMPanelFeatureMgr::ParseCapabilities(uint32_t blob_id, char* value, uint32_t max_len,
                                           const std::string str) {
  drmModePropertyBlobRes *blob = drmModeGetPropertyBlob(dev_fd_, blob_id);
  if (!blob) {
    DRM_LOGW("Unable to find blob for id %d", blob_id);
    return;
  }

  if (!blob->data) {
    DRM_LOGW("Invalid blob - no data for for blob-id %d", blob_id);
    return;
  }

  char *fmt_str = new char[blob->length + 1];
  std::memcpy(fmt_str, blob->data, blob->length);
  fmt_str[blob->length] = '\0';
  std::stringstream stream(fmt_str);
  std::string line = {};
  std::string val = {};
  const std::string goal = str + "=";
  while (std::getline(stream, line)) {
    if (line.find(goal) != std::string::npos) {
      val = std::string(line, goal.length());
    }
  }

  if (max_len <= val.size()) {
    DRM_LOGW("Insufficient size max_len: %d actual size: %zu", max_len, val.size());
    return;
  }
  std::copy(val.begin(), val.end(), value);
  value[val.size()] = '\0';
  delete[] fmt_str;
}

void DRMPanelFeatureMgr::GetPanelFeatureInfo(DRMPanelFeatureInfo *info) {
  lock_guard<mutex> lock(lock_);

  if (!info) {
    DRM_LOGE("Invalid input, DRMPanelFeatureInfo is NULL");
    return;
  }

  if (info->prop_id > kDRMPanelFeatureMax) {
    DRM_LOGE("Invalid feature id %d", info->prop_id);
    return;
  }

  DRMProperty prop_enum = drm_property_map_[info->prop_id];
  if (!prop_mgr_.IsPropertyAvailable(prop_enum)) {
    DRM_LOGE("Property id is not available for DRMProperty: %d feature-id: %d",
             prop_enum, info->prop_id);
    return;
  }

  // memory is not allocated by client - populate default property info
  if (!info->prop_ptr) {
    *info = feature_info_tbl_[info->prop_id];
    return;
  }

  drmModeObjectProperties *props =
          drmModeObjectGetProperties(dev_fd_, info->obj_id, info->obj_type);
  if (!props || !props->props || !props->prop_values) {
    drmModeFreeObjectProperties(props);
    DRM_LOGE("Failed to Get properties for obj: %d type:%d", info->obj_id, info->obj_type);
    return;
  }

  for (uint32_t j = 0; j < props->count_props; j++) {
    drmModePropertyRes *property = drmModeGetProperty(dev_fd_, props->props[j]);
    if (!property) {
      continue;
    }

    std::string property_name(property->name);
    if (prop_enum != prop_mgr_.GetPropertyEnum(property_name)) {
      drmModeFreeProperty(property);
      continue;
    }
    else if (info->prop_id == kDRMPanelFeatureDsppRCInfo) {
      ParseDsppCapabilities(props->prop_values[j],
              reinterpret_cast<std::vector<int> *> (info->prop_ptr), &(info->prop_size), "rc");
    } else if (drm_prop_type_map_[info->prop_id] == DRMPropType::kPropBlob) {
      drmModePropertyBlobRes *blob = drmModeGetPropertyBlob(dev_fd_, props->prop_values[j]);
      if (!blob || !blob->data || !blob->length) {
        return;
      }
      uint8_t *src_begin = reinterpret_cast<uint8_t *> (blob->data);
      uint8_t *src_end = src_begin + blob->length;
      uint8_t *dst = reinterpret_cast<uint8_t *> (info->prop_ptr);
      std::copy(src_begin, src_end, dst);
    } else {
      uint8_t *src_begin = reinterpret_cast<uint8_t *> (props->prop_values[j]);
      uint8_t *src_end = src_begin + info->prop_size;
      uint8_t *dst = reinterpret_cast<uint8_t *> (info->prop_ptr);
      std::copy(src_begin, src_end, dst);
    }

    drmModeFreeProperty(property);
  }

  drmModeFreeObjectProperties(props);
}

void DRMPanelFeatureMgr::CachePanelFeature(const DRMPanelFeatureInfo &info) {
  lock_guard<mutex> lock(lock_);

  if (info.prop_id >= kDRMPanelFeatureMax || info.obj_id == UINT32_MAX) {
    DRM_LOGE("invalid property info to set id %d value ptr %" PRIu64 , info.prop_id, info.prop_ptr);
    return;
  }

  dirty_features_[info.prop_id].first = true;
  dirty_features_[info.prop_id].second = info;
}

void DRMPanelFeatureMgr::CommitPanelFeatures(drmModeAtomicReq *req, const DRMDisplayToken &tok) {
  int ret = 0;

  lock_guard<mutex> lock(lock_);
  for (auto it = dirty_features_.begin(); it != dirty_features_.end(); it++) {
    DRMPanelFeatureInfo &info = it->second;

    if (!it->first)
        continue;

    if (info.prop_id >= kDRMPanelFeatureMax) {
      DRM_LOGE("invalid property info to set id %d value ptr %" PRIu64 , info.prop_id, info.prop_ptr);
      continue;
    }

    // Commit only features meant for the given DisplayToken
    if (tok.crtc_id != info.obj_id && tok.conn_id != info.obj_id) {
      continue;
    }

    uint32_t prop_id = prop_mgr_.GetPropertyId(drm_property_map_[info.prop_id]);
    uint64_t value = 0;

    if (DRMPropType::kPropBlob == drm_prop_type_map_[info.prop_id]) {
      uint32_t blob_id = 0;
      if (!info.prop_ptr) {
        // Reset the feature.
        ret = drmModeAtomicAddProperty(req, info.obj_id, prop_id, 0);
        if (ret < 0) {
          DRM_LOGE("failed to add property ret:%d, obj_id:%d prop_id:%u value:%" PRIu64,
                    ret, info.obj_id, prop_id, value);
          return;
        }
        continue;
      }

      ret = drmModeCreatePropertyBlob(dev_fd_, reinterpret_cast<void *> (info.prop_ptr),
              info.prop_size, &blob_id);
      if (ret || blob_id == 0) {
        DRM_LOGE("failed to create blob ret %d, id = %d prop_ptr:%" PRIu64 " prop_sz:%d",
                ret, blob_id, info.prop_ptr, info.prop_size);
        return;
      }

      if (drm_prop_blob_ids_map_[info.prop_id]) {
        ret = drmModeDestroyPropertyBlob(dev_fd_, drm_prop_blob_ids_map_[info.prop_id]);
        if (ret) {
          DRM_LOGE("failed to destroy blob for feature %d, ret = %d", info.prop_id, ret);
          return;
        }
      }
      drm_prop_blob_ids_map_[info.prop_id] = blob_id;

      value = blob_id;
    } else if (info.prop_size == sizeof(uint64_t)) {
      value = (reinterpret_cast<uint64_t *> (info.prop_ptr))[0];
    } else {
      DRM_LOGE("Unsupported property type id = %d size:%d", info.prop_id, info.prop_size);
    }

    ret = drmModeAtomicAddProperty(req, info.obj_id, prop_id, value);
    if (ret < 0) {
      DRM_LOGE("failed to add property ret:%d, obj_id:%d prop_id:%x value:%" PRIu64,
                ret, info.obj_id, prop_id, value);
    }
    *it = {};
  }
}

}  // namespace sde_drm