blob: f5e3521518583dfa35d7748744ca1415b5ce96c0 [file] [log] [blame]
Sean Paul6a55e9f2015-04-30 15:31:06 -04001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010017#define LOG_TAG "hwc-drm-device"
Sean Paul6a55e9f2015-04-30 15:31:06 -040018
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include "DrmDevice.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040020
Sean Paul6a55e9f2015-04-30 15:31:06 -040021#include <fcntl.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040022#include <xf86drm.h>
23#include <xf86drmMode.h>
24
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020025#include <algorithm>
26#include <array>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020027#include <cerrno>
Roman Stratiienko13cc3662020-08-29 21:35:39 +030028#include <cinttypes>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020029#include <cstdint>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030030#include <sstream>
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020031#include <string>
32
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020033#include "drm/DrmPlane.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020034#include "utils/log.h"
35#include "utils/properties.h"
36
Sean Paul6a55e9f2015-04-30 15:31:06 -040037namespace android {
38
Roman Stratiienko1e053b42021-10-25 22:54:20 +030039DrmDevice::DrmDevice() {
Roman Stratiienko8666dc92021-02-09 17:49:55 +020040 self.reset(this);
41 mDrmFbImporter = std::make_unique<DrmFbImporter>(self);
Sean Paul047b9b22015-07-28 14:15:42 -040042}
43
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020044// NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
Alexandru Gheorghec5463582018-03-27 15:52:02 +010045std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040046 /* TODO: Use drmOpenControl here instead */
Roman Stratiienko0fade372021-02-20 13:59:55 +020047 fd_ = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
Zach Reiznerff30b522015-10-28 19:08:45 -070048 if (fd() < 0) {
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020049 // NOLINTNEXTLINE(concurrency-mt-unsafe): Fixme
Peter Collingbournec77052e2020-02-19 11:25:08 -080050 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Alexandru Gheorghec5463582018-03-27 15:52:02 +010051 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040052 }
53
Zach Reiznerff30b522015-10-28 19:08:45 -070054 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040055 if (ret) {
56 ALOGE("Failed to set universal plane cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010057 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040058 }
59
Zach Reiznerff30b522015-10-28 19:08:45 -070060 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040061 if (ret) {
62 ALOGE("Failed to set atomic cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010063 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040064 }
65
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000066#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
67 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
68 if (ret) {
69 ALOGI("Failed to set writeback cap %d", ret);
70 ret = 0;
71 }
72#endif
73
Roman Stratiienko8666dc92021-02-09 17:49:55 +020074 uint64_t cap_value = 0;
75 if (drmGetCap(fd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value)) {
76 ALOGW("drmGetCap failed. Fallback to no modifier support.");
77 cap_value = 0;
78 }
79 HasAddFb2ModifiersSupport_ = cap_value != 0;
80
John Stultzfa002332021-02-02 01:34:45 +000081 drmSetMaster(fd());
82 if (!drmIsMaster(fd())) {
83 ALOGE("DRM/KMS master access required");
84 return std::make_tuple(-EACCES, 0);
Roman Stratiienko3b24cd92021-01-13 10:32:04 +020085 }
86
Roman Stratiienko3e8ce572021-09-29 12:46:28 +030087 auto res = MakeDrmModeResUnique(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -040088 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010089 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010090 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040091 }
92
Sean Paulf72cccd2018-08-27 13:59:08 -040093 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
94 res->min_height);
95 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
96 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -080097
Roman Stratiienko10be8752022-01-30 20:28:46 +020098 for (int i = 0; i < res->count_crtcs; ++i) {
99 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
100 auto crtc = DrmCrtc::CreateInstance(*this, res->crtcs[i], i);
101 if (crtc) {
102 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400103 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400104 }
105
Roman Stratiienko027987b2022-01-30 21:06:35 +0200106 for (int i = 0; i < res->count_encoders; ++i) {
107 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
108 auto enc = DrmEncoder::CreateInstance(*this, res->encoders[i], i);
109 if (enc) {
110 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400111 }
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000112 }
113
Sean Paul6a55e9f2015-04-30 15:31:06 -0400114 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300115 auto c = MakeDrmModeConnectorUnique(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400116 if (!c) {
117 ALOGE("Failed to get connector %d", res->connectors[i]);
118 ret = -ENODEV;
119 break;
120 }
121
122 std::vector<DrmEncoder *> possible_encoders;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200123 DrmEncoder *current_encoder = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400124 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700125 for (auto &encoder : encoders_) {
Roman Stratiienko027987b2022-01-30 21:06:35 +0200126 if (encoder->GetId() == c->encoders[j])
Zach Reiznerff30b522015-10-28 19:08:45 -0700127 possible_encoders.push_back(encoder.get());
Roman Stratiienko027987b2022-01-30 21:06:35 +0200128 if (encoder->GetId() == c->encoder_id)
Zach Reiznerff30b522015-10-28 19:08:45 -0700129 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400130 }
131 }
132
Zach Reiznerff30b522015-10-28 19:08:45 -0700133 std::unique_ptr<DrmConnector> conn(
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300134 new DrmConnector(this, c.get(), current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400135
Sean Paul6a55e9f2015-04-30 15:31:06 -0400136 ret = conn->Init();
137 if (ret) {
138 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400139 break;
140 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400141
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000142 if (conn->writeback())
143 writeback_connectors_.emplace_back(std::move(conn));
144 else
145 connectors_.emplace_back(std::move(conn));
Robert Foss610d9892017-11-01 12:50:04 -0500146 }
147
Roman Stratiienkoedb97ed2022-01-29 19:53:14 +0200148 auto add_displays = [this, &num_displays](bool internal, bool connected) {
149 for (auto &conn : connectors_) {
150 bool is_connected = conn->state() == DRM_MODE_CONNECTED;
151 if ((internal ? conn->internal() : conn->external()) &&
152 (connected ? is_connected : !is_connected)) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100153 conn->set_display(num_displays);
154 displays_[num_displays] = num_displays;
155 ++num_displays;
156 }
Robert Foss610d9892017-11-01 12:50:04 -0500157 }
Roman Stratiienkoedb97ed2022-01-29 19:53:14 +0200158 };
159
160 /* Put internal first to ensure Primary display will be internal
161 * in case at least 1 internal is available
162 */
163 add_displays(/*internal = */ true, /*connected = */ true);
164 add_displays(/*internal = */ false, /*connected = */ true);
165 add_displays(/*internal = */ true, /*connected = */ false);
166 add_displays(/*internal = */ false, /*connected = */ false);
Robert Foss610d9892017-11-01 12:50:04 -0500167
Sean Paul6a55e9f2015-04-30 15:31:06 -0400168 // Catch-all for the above loops
169 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100170 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400171
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300172 auto plane_res = MakeDrmModePlaneResUnique(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400173 if (!plane_res) {
174 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100175 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400176 }
177
178 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Roman Stratiienkob671fab2022-01-29 00:50:22 +0200179 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
180 auto plane = DrmPlane::CreateInstance(*this, plane_res->planes[i]);
181
182 if (plane) {
183 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400184 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400185 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400186
Zach Reiznerff30b522015-10-28 19:08:45 -0700187 for (auto &conn : connectors_) {
188 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400189 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700190 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100191 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400192 }
193 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100194 return std::make_tuple(ret, displays_.size());
195}
196
197bool DrmDevice::HandlesDisplay(int display) const {
198 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400199}
200
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100201DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200202 for (const auto &conn : connectors_) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700203 if (conn->display() == display)
204 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400205 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200206 return nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400207}
208
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100209DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Roman Stratiienko10be8752022-01-30 20:28:46 +0200210 return bound_crtcs_.at(display);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400211}
212
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100213const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400214 return crtcs_;
215}
216
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100217uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400218 return ++mode_id_;
219}
220
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100221int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400222 /* First try to use the currently-bound crtc */
Roman Stratiienko027987b2022-01-30 21:06:35 +0200223 auto *crtc = FindCrtcById(enc->GetCurrentCrtcId());
Roman Stratiienko10be8752022-01-30 20:28:46 +0200224 if (crtc && bound_crtcs_.count(display) == 0) {
225 bound_crtcs_[display] = crtc;
Roman Stratiienko027987b2022-01-30 21:06:35 +0200226 bound_encoders_[crtc] = enc;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400227 return 0;
228 }
229
230 /* Try to find a possible crtc which will work */
Roman Stratiienko027987b2022-01-30 21:06:35 +0200231 for (auto &crtc : crtcs_) {
232 /* Crtc not supported or we've already tried this earlier */
233 if (!enc->SupportsCrtc(*crtc) || crtc->GetId() == enc->GetCurrentCrtcId()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400234 continue;
Roman Stratiienko027987b2022-01-30 21:06:35 +0200235 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400236
Roman Stratiienko10be8752022-01-30 20:28:46 +0200237 if (bound_crtcs_.count(display) == 0) {
Roman Stratiienko027987b2022-01-30 21:06:35 +0200238 bound_crtcs_[display] = crtc.get();
239 bound_encoders_[crtc.get()] = enc;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400240 return 0;
241 }
242 }
243
244 /* We can't use the encoder, but nothing went wrong, try another one */
245 return -EAGAIN;
246}
247
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100248int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400249 int display = connector->display();
250 /* Try to use current setup first */
251 if (connector->encoder()) {
252 int ret = TryEncoderForDisplay(display, connector->encoder());
253 if (!ret) {
254 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200255 }
256
257 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400258 ALOGE("Could not set mode %d/%d", display, ret);
259 return ret;
260 }
261 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400262
Zach Reiznerff30b522015-10-28 19:08:45 -0700263 for (DrmEncoder *enc : connector->possible_encoders()) {
264 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400265 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700266 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400267 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200268 }
269
270 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400271 ALOGE("Could not set mode %d/%d", display, ret);
272 return ret;
273 }
274 }
275 ALOGE("Could not find a suitable encoder/crtc for display %d",
276 connector->display());
277 return -ENODEV;
278}
279
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300280auto DrmDevice::RegisterUserPropertyBlob(void *data, size_t length) const
281 -> DrmModeUserPropertyBlobUnique {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200282 struct drm_mode_create_blob create_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400283 create_blob.length = length;
284 create_blob.data = (__u64)data;
285
Zach Reiznerff30b522015-10-28 19:08:45 -0700286 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400287 if (ret) {
288 ALOGE("Failed to create mode property blob %d", ret);
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200289 return {};
Sean Paul877be972015-06-03 14:08:27 -0400290 }
Sean Paul877be972015-06-03 14:08:27 -0400291
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300292 return DrmModeUserPropertyBlobUnique(
293 new uint32_t(create_blob.blob_id), [this](const uint32_t *it) {
294 struct drm_mode_destroy_blob destroy_blob {};
295 destroy_blob.blob_id = (__u32)*it;
296 int err = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
297 if (err != 0) {
298 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", *it,
299 err);
300 }
301 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
302 delete it;
303 });
Sean Paul877be972015-06-03 14:08:27 -0400304}
305
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100306int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200307 const char *prop_name, DrmProperty *property) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200308 drmModeObjectPropertiesPtr props = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400309
Zach Reiznerff30b522015-10-28 19:08:45 -0700310 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400311 if (!props) {
312 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
313 return -ENODEV;
314 }
315
316 bool found = false;
317 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700318 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400319 if (!strcmp(p->name, prop_name)) {
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300320 property->Init(obj_id, p, props->prop_values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400321 found = true;
322 }
323 drmModeFreeProperty(p);
324 }
325
326 drmModeFreeObjectProperties(props);
327 return found ? 0 : -ENOENT;
328}
329
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100330int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
331 const char *prop_name,
Roman Stratiienko0b063882021-09-30 10:15:05 +0300332 DrmProperty *property) const {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400333 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
334 property);
335}
Matvii Zorinef3c7972020-08-11 15:15:44 +0300336
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200337std::string DrmDevice::GetName() const {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200338 auto *ver = drmGetVersion(fd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300339 if (!ver) {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200340 ALOGW("Failed to get drm version for fd=%d", fd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300341 return "generic";
342 }
343
344 std::string name(ver->name);
345 drmFreeVersion(ver);
346 return name;
347}
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300348
349auto DrmDevice::IsKMSDev(const char *path) -> bool {
350 auto fd = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
351 if (!fd) {
352 return false;
353 }
354
355 auto res = MakeDrmModeResUnique(fd.Get());
356 if (!res) {
357 return false;
358 }
359
360 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
361 res->count_encoders > 0;
362
363 return is_kms;
364}
365
Sean Paulf72cccd2018-08-27 13:59:08 -0400366} // namespace android