blob: 29dd95f5452b72dc3ea9a34629664461e0101863 [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 Stratiienko7d899112022-01-31 11:30:27 +020040 drm_fb_importer_ = std::make_unique<DrmFbImporter>(*this);
Sean Paul047b9b22015-07-28 14:15:42 -040041}
42
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020043// NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
Alexandru Gheorghec5463582018-03-27 15:52:02 +010044std::tuple<int, int> DrmDevice::Init(const char *path, int num_displays) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040045 /* TODO: Use drmOpenControl here instead */
Roman Stratiienko0fade372021-02-20 13:59:55 +020046 fd_ = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
Roman Stratiienko7d899112022-01-31 11:30:27 +020047 if (!fd_) {
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020048 // NOLINTNEXTLINE(concurrency-mt-unsafe): Fixme
Peter Collingbournec77052e2020-02-19 11:25:08 -080049 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Alexandru Gheorghec5463582018-03-27 15:52:02 +010050 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040051 }
52
Roman Stratiienko7d899112022-01-31 11:30:27 +020053 int ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
54 if (ret != 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040055 ALOGE("Failed to set universal plane cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010056 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040057 }
58
Roman Stratiienko7d899112022-01-31 11:30:27 +020059 ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_ATOMIC, 1);
60 if (ret != 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040061 ALOGE("Failed to set atomic cap %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010062 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040063 }
64
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000065#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
Roman Stratiienko7d899112022-01-31 11:30:27 +020066 ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
67 if (ret != 0) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000068 ALOGI("Failed to set writeback cap %d", ret);
69 ret = 0;
70 }
71#endif
72
Roman Stratiienko8666dc92021-02-09 17:49:55 +020073 uint64_t cap_value = 0;
Roman Stratiienko7d899112022-01-31 11:30:27 +020074 if (drmGetCap(GetFd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value) != 0) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +020075 ALOGW("drmGetCap failed. Fallback to no modifier support.");
76 cap_value = 0;
77 }
78 HasAddFb2ModifiersSupport_ = cap_value != 0;
79
Roman Stratiienko7d899112022-01-31 11:30:27 +020080 drmSetMaster(GetFd());
81 if (drmIsMaster(GetFd()) == 0) {
John Stultzfa002332021-02-02 01:34:45 +000082 ALOGE("DRM/KMS master access required");
83 return std::make_tuple(-EACCES, 0);
Roman Stratiienko3b24cd92021-01-13 10:32:04 +020084 }
85
Roman Stratiienko7d899112022-01-31 11:30:27 +020086 auto res = MakeDrmModeResUnique(GetFd());
Sean Paul6a55e9f2015-04-30 15:31:06 -040087 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010088 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010089 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040090 }
91
Sean Paulf72cccd2018-08-27 13:59:08 -040092 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
93 res->min_height);
94 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
95 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -080096
Roman Stratiienko10be8752022-01-30 20:28:46 +020097 for (int i = 0; i < res->count_crtcs; ++i) {
98 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
99 auto crtc = DrmCrtc::CreateInstance(*this, res->crtcs[i], i);
100 if (crtc) {
101 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400102 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400103 }
104
Roman Stratiienko027987b2022-01-30 21:06:35 +0200105 for (int i = 0; i < res->count_encoders; ++i) {
106 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
107 auto enc = DrmEncoder::CreateInstance(*this, res->encoders[i], i);
108 if (enc) {
109 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400110 }
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000111 }
112
Roman Stratiienko650299a2022-01-30 23:46:10 +0200113 for (int i = 0; i < res->count_connectors; ++i) {
114 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
115 auto conn = DrmConnector::CreateInstance(*this, res->connectors[i], i);
116
117 if (!conn) {
118 continue;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400119 }
120
Roman Stratiienko650299a2022-01-30 23:46:10 +0200121 if (conn->IsWriteback()) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000122 writeback_connectors_.emplace_back(std::move(conn));
Roman Stratiienko650299a2022-01-30 23:46:10 +0200123 } else {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000124 connectors_.emplace_back(std::move(conn));
Roman Stratiienko650299a2022-01-30 23:46:10 +0200125 }
Robert Foss610d9892017-11-01 12:50:04 -0500126 }
127
Roman Stratiienkoedb97ed2022-01-29 19:53:14 +0200128 auto add_displays = [this, &num_displays](bool internal, bool connected) {
129 for (auto &conn : connectors_) {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200130 bool is_connected = conn->IsConnected();
131 if ((internal ? conn->IsInternal() : conn->IsExternal()) &&
Roman Stratiienkoedb97ed2022-01-29 19:53:14 +0200132 (connected ? is_connected : !is_connected)) {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200133 bound_connectors_[num_displays] = conn.get();
134 connectors_to_display_id_[conn.get()] = num_displays;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100135 ++num_displays;
136 }
Robert Foss610d9892017-11-01 12:50:04 -0500137 }
Roman Stratiienkoedb97ed2022-01-29 19:53:14 +0200138 };
139
140 /* Put internal first to ensure Primary display will be internal
141 * in case at least 1 internal is available
142 */
143 add_displays(/*internal = */ true, /*connected = */ true);
144 add_displays(/*internal = */ false, /*connected = */ true);
145 add_displays(/*internal = */ true, /*connected = */ false);
146 add_displays(/*internal = */ false, /*connected = */ false);
Robert Foss610d9892017-11-01 12:50:04 -0500147
Sean Paul6a55e9f2015-04-30 15:31:06 -0400148 // Catch-all for the above loops
Roman Stratiienko7d899112022-01-31 11:30:27 +0200149 if (ret != 0)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100150 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400151
Roman Stratiienko7d899112022-01-31 11:30:27 +0200152 auto plane_res = MakeDrmModePlaneResUnique(GetFd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400153 if (!plane_res) {
154 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100155 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400156 }
157
158 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Roman Stratiienkob671fab2022-01-29 00:50:22 +0200159 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
160 auto plane = DrmPlane::CreateInstance(*this, plane_res->planes[i]);
161
162 if (plane) {
163 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400164 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400165 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400166
Zach Reiznerff30b522015-10-28 19:08:45 -0700167 for (auto &conn : connectors_) {
168 ret = CreateDisplayPipe(conn.get());
Roman Stratiienko7d899112022-01-31 11:30:27 +0200169 if (ret != 0) {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200170 ALOGE("Failed CreateDisplayPipe %d with %d", conn->GetId(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100171 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400172 }
173 }
Roman Stratiienko650299a2022-01-30 23:46:10 +0200174 return std::make_tuple(ret, bound_connectors_.size());
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100175}
176
177bool DrmDevice::HandlesDisplay(int display) const {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200178 return bound_connectors_.count(display) != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400179}
180
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100181DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200182 return bound_connectors_.at(display);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400183}
184
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100185DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Roman Stratiienko10be8752022-01-30 20:28:46 +0200186 return bound_crtcs_.at(display);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400187}
188
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100189int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400190 /* First try to use the currently-bound crtc */
Roman Stratiienko027987b2022-01-30 21:06:35 +0200191 auto *crtc = FindCrtcById(enc->GetCurrentCrtcId());
Roman Stratiienko7d899112022-01-31 11:30:27 +0200192 if (crtc != nullptr && bound_crtcs_.count(display) == 0) {
Roman Stratiienko10be8752022-01-30 20:28:46 +0200193 bound_crtcs_[display] = crtc;
Roman Stratiienko027987b2022-01-30 21:06:35 +0200194 bound_encoders_[crtc] = enc;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400195 return 0;
196 }
197
198 /* Try to find a possible crtc which will work */
Roman Stratiienko027987b2022-01-30 21:06:35 +0200199 for (auto &crtc : crtcs_) {
200 /* Crtc not supported or we've already tried this earlier */
201 if (!enc->SupportsCrtc(*crtc) || crtc->GetId() == enc->GetCurrentCrtcId()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400202 continue;
Roman Stratiienko027987b2022-01-30 21:06:35 +0200203 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400204
Roman Stratiienko10be8752022-01-30 20:28:46 +0200205 if (bound_crtcs_.count(display) == 0) {
Roman Stratiienko027987b2022-01-30 21:06:35 +0200206 bound_crtcs_[display] = crtc.get();
207 bound_encoders_[crtc.get()] = enc;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400208 return 0;
209 }
210 }
211
212 /* We can't use the encoder, but nothing went wrong, try another one */
213 return -EAGAIN;
214}
215
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100216int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200217 int display = connectors_to_display_id_.at(connector);
Sean Paul877be972015-06-03 14:08:27 -0400218 /* Try to use current setup first */
Roman Stratiienko650299a2022-01-30 23:46:10 +0200219 auto *enc0 = FindEncoderById(connector->GetCurrentEncoderId());
220 if (enc0 != nullptr && encoders_to_display_id_.count(enc0) == 0) {
221 int ret = TryEncoderForDisplay(display, enc0);
Roman Stratiienko7d899112022-01-31 11:30:27 +0200222 if (ret == 0) {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200223 encoders_to_display_id_[enc0] = display;
Sean Paul877be972015-06-03 14:08:27 -0400224 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200225 }
226
227 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400228 ALOGE("Could not set mode %d/%d", display, ret);
229 return ret;
230 }
231 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400232
Roman Stratiienko650299a2022-01-30 23:46:10 +0200233 for (auto &enc : encoders_) {
234 if (!connector->SupportsEncoder(*enc) ||
235 encoders_to_display_id_.count(enc.get()) != 0) {
236 continue;
237 }
238
239 int ret = TryEncoderForDisplay(display, enc.get());
Roman Stratiienko7d899112022-01-31 11:30:27 +0200240 if (ret == 0) {
Roman Stratiienko650299a2022-01-30 23:46:10 +0200241 encoders_to_display_id_[enc.get()] = display;
Sean Paul877be972015-06-03 14:08:27 -0400242 return 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200243 }
244
245 if (ret != -EAGAIN) {
Sean Paul877be972015-06-03 14:08:27 -0400246 ALOGE("Could not set mode %d/%d", display, ret);
247 return ret;
248 }
249 }
Roman Stratiienko650299a2022-01-30 23:46:10 +0200250 ALOGE("Could not find a suitable encoder/crtc for display %d", display);
Sean Paul877be972015-06-03 14:08:27 -0400251 return -ENODEV;
252}
253
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300254auto DrmDevice::RegisterUserPropertyBlob(void *data, size_t length) const
255 -> DrmModeUserPropertyBlobUnique {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200256 struct drm_mode_create_blob create_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400257 create_blob.length = length;
Roman Stratiienko7d899112022-01-31 11:30:27 +0200258 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
Sean Paul877be972015-06-03 14:08:27 -0400259 create_blob.data = (__u64)data;
260
Roman Stratiienko7d899112022-01-31 11:30:27 +0200261 int ret = drmIoctl(GetFd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
262 if (ret != 0) {
Sean Paul877be972015-06-03 14:08:27 -0400263 ALOGE("Failed to create mode property blob %d", ret);
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200264 return {};
Sean Paul877be972015-06-03 14:08:27 -0400265 }
Sean Paul877be972015-06-03 14:08:27 -0400266
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300267 return DrmModeUserPropertyBlobUnique(
268 new uint32_t(create_blob.blob_id), [this](const uint32_t *it) {
269 struct drm_mode_destroy_blob destroy_blob {};
270 destroy_blob.blob_id = (__u32)*it;
Roman Stratiienko7d899112022-01-31 11:30:27 +0200271 int err = drmIoctl(GetFd(), DRM_IOCTL_MODE_DESTROYPROPBLOB,
272 &destroy_blob);
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300273 if (err != 0) {
274 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", *it,
275 err);
276 }
277 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
278 delete it;
279 });
Sean Paul877be972015-06-03 14:08:27 -0400280}
281
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100282int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200283 const char *prop_name, DrmProperty *property) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200284 drmModeObjectPropertiesPtr props = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400285
Roman Stratiienko7d899112022-01-31 11:30:27 +0200286 props = drmModeObjectGetProperties(GetFd(), obj_id, obj_type);
287 if (props == nullptr) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400288 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
289 return -ENODEV;
290 }
291
292 bool found = false;
293 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Roman Stratiienko7d899112022-01-31 11:30:27 +0200294 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
295 drmModePropertyPtr p = drmModeGetProperty(GetFd(), props->props[i]);
296 if (strcmp(p->name, prop_name) == 0) {
297 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300298 property->Init(obj_id, p, props->prop_values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400299 found = true;
300 }
301 drmModeFreeProperty(p);
302 }
303
304 drmModeFreeObjectProperties(props);
305 return found ? 0 : -ENOENT;
306}
307
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200308std::string DrmDevice::GetName() const {
Roman Stratiienko7d899112022-01-31 11:30:27 +0200309 auto *ver = drmGetVersion(GetFd());
310 if (ver == nullptr) {
311 ALOGW("Failed to get drm version for fd=%d", GetFd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300312 return "generic";
313 }
314
315 std::string name(ver->name);
316 drmFreeVersion(ver);
317 return name;
318}
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300319
320auto DrmDevice::IsKMSDev(const char *path) -> bool {
321 auto fd = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
322 if (!fd) {
323 return false;
324 }
325
326 auto res = MakeDrmModeResUnique(fd.Get());
327 if (!res) {
328 return false;
329 }
330
331 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
332 res->count_encoders > 0;
333
334 return is_kms;
335}
336
Roman Stratiienko7d899112022-01-31 11:30:27 +0200337auto DrmDevice::GetConnectors()
338 -> const std::vector<std::unique_ptr<DrmConnector>> & {
339 return connectors_;
340}
341
342auto DrmDevice::GetPlanes() -> const std::vector<std::unique_ptr<DrmPlane>> & {
343 return planes_;
344}
345
346auto DrmDevice::GetCrtcs() -> const std::vector<std::unique_ptr<DrmCrtc>> & {
347 return crtcs_;
348}
349
350auto DrmDevice::GetEncoders()
351 -> const std::vector<std::unique_ptr<DrmEncoder>> & {
352 return encoders_;
353}
354
Sean Paulf72cccd2018-08-27 13:59:08 -0400355} // namespace android