blob: d1ae7c9136c1432cf6923c6d82cf0e6704a46eb0 [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 Stratiienko19c162f2022-02-01 09:35:08 +020033#include "compositor/DrmDisplayCompositor.h"
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020034#include "drm/DrmPlane.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020035#include "utils/log.h"
36#include "utils/properties.h"
37
Sean Paul6a55e9f2015-04-30 15:31:06 -040038namespace android {
39
Roman Stratiienko1e053b42021-10-25 22:54:20 +030040DrmDevice::DrmDevice() {
Roman Stratiienko7d899112022-01-31 11:30:27 +020041 drm_fb_importer_ = std::make_unique<DrmFbImporter>(*this);
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));
Roman Stratiienko7d899112022-01-31 11:30:27 +020048 if (!fd_) {
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
Roman Stratiienko7d899112022-01-31 11:30:27 +020054 int ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
55 if (ret != 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040056 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
Roman Stratiienko7d899112022-01-31 11:30:27 +020060 ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_ATOMIC, 1);
61 if (ret != 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040062 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
Roman Stratiienko7d899112022-01-31 11:30:27 +020067 ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
68 if (ret != 0) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000069 ALOGI("Failed to set writeback cap %d", ret);
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000070 }
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 Stratiienko7d899112022-01-31 11:30:27 +0200128 auto plane_res = MakeDrmModePlaneResUnique(GetFd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400129 if (!plane_res) {
130 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100131 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400132 }
133
134 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Roman Stratiienkob671fab2022-01-29 00:50:22 +0200135 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
136 auto plane = DrmPlane::CreateInstance(*this, plane_res->planes[i]);
137
138 if (plane) {
139 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400140 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400141 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400142
Roman Stratiienkocad8e0c2022-01-31 16:40:16 +0200143 auto add_displays = [this, &num_displays](bool internal, bool connected) {
144 for (auto &conn : connectors_) {
145 bool is_connected = conn->IsConnected();
146 if ((internal ? conn->IsInternal() : conn->IsExternal()) &&
147 (connected ? is_connected : !is_connected)) {
148 auto pipe = DrmDisplayPipeline::CreatePipeline(*conn);
149 if (pipe) {
150 pipelines_[num_displays] = std::move(pipe);
151 ++num_displays;
152 }
153 }
Sean Paul57355412015-09-19 09:14:34 -0400154 }
Roman Stratiienkocad8e0c2022-01-31 16:40:16 +0200155 };
156
157 /* Put internal first to ensure Primary display will be internal
158 * in case at least 1 internal is available
159 */
160 add_displays(/*internal = */ true, /*connected = */ true);
161 add_displays(/*internal = */ false, /*connected = */ true);
162 add_displays(/*internal = */ true, /*connected = */ false);
163 add_displays(/*internal = */ false, /*connected = */ false);
164
165 return std::make_tuple(0, pipelines_.size());
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100166}
167
168bool DrmDevice::HandlesDisplay(int display) const {
Roman Stratiienkocad8e0c2022-01-31 16:40:16 +0200169 return pipelines_.count(display) != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400170}
171
Roman Stratiienkocad8e0c2022-01-31 16:40:16 +0200172auto DrmDevice::GetDisplayId(DrmConnector *conn) -> int {
173 for (auto &dpipe : pipelines_) {
174 if (dpipe.second->connector->Get() == conn) {
175 return dpipe.first;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400176 }
177 }
Roman Stratiienkocad8e0c2022-01-31 16:40:16 +0200178 return -1;
Sean Paul877be972015-06-03 14:08:27 -0400179}
180
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300181auto DrmDevice::RegisterUserPropertyBlob(void *data, size_t length) const
182 -> DrmModeUserPropertyBlobUnique {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200183 struct drm_mode_create_blob create_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400184 create_blob.length = length;
Roman Stratiienko7d899112022-01-31 11:30:27 +0200185 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
Sean Paul877be972015-06-03 14:08:27 -0400186 create_blob.data = (__u64)data;
187
Roman Stratiienko7d899112022-01-31 11:30:27 +0200188 int ret = drmIoctl(GetFd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
189 if (ret != 0) {
Sean Paul877be972015-06-03 14:08:27 -0400190 ALOGE("Failed to create mode property blob %d", ret);
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200191 return {};
Sean Paul877be972015-06-03 14:08:27 -0400192 }
Sean Paul877be972015-06-03 14:08:27 -0400193
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300194 return DrmModeUserPropertyBlobUnique(
195 new uint32_t(create_blob.blob_id), [this](const uint32_t *it) {
196 struct drm_mode_destroy_blob destroy_blob {};
197 destroy_blob.blob_id = (__u32)*it;
Roman Stratiienko7d899112022-01-31 11:30:27 +0200198 int err = drmIoctl(GetFd(), DRM_IOCTL_MODE_DESTROYPROPBLOB,
199 &destroy_blob);
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300200 if (err != 0) {
201 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", *it,
202 err);
203 }
204 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
205 delete it;
206 });
Sean Paul877be972015-06-03 14:08:27 -0400207}
208
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100209int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200210 const char *prop_name, DrmProperty *property) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200211 drmModeObjectPropertiesPtr props = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400212
Roman Stratiienko7d899112022-01-31 11:30:27 +0200213 props = drmModeObjectGetProperties(GetFd(), obj_id, obj_type);
214 if (props == nullptr) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400215 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
216 return -ENODEV;
217 }
218
219 bool found = false;
220 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Roman Stratiienko7d899112022-01-31 11:30:27 +0200221 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
222 drmModePropertyPtr p = drmModeGetProperty(GetFd(), props->props[i]);
223 if (strcmp(p->name, prop_name) == 0) {
224 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300225 property->Init(obj_id, p, props->prop_values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400226 found = true;
227 }
228 drmModeFreeProperty(p);
229 }
230
231 drmModeFreeObjectProperties(props);
232 return found ? 0 : -ENOENT;
233}
234
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200235std::string DrmDevice::GetName() const {
Roman Stratiienko7d899112022-01-31 11:30:27 +0200236 auto *ver = drmGetVersion(GetFd());
237 if (ver == nullptr) {
238 ALOGW("Failed to get drm version for fd=%d", GetFd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300239 return "generic";
240 }
241
242 std::string name(ver->name);
243 drmFreeVersion(ver);
244 return name;
245}
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300246
247auto DrmDevice::IsKMSDev(const char *path) -> bool {
248 auto fd = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
249 if (!fd) {
250 return false;
251 }
252
253 auto res = MakeDrmModeResUnique(fd.Get());
254 if (!res) {
255 return false;
256 }
257
258 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
259 res->count_encoders > 0;
260
261 return is_kms;
262}
263
Roman Stratiienko7d899112022-01-31 11:30:27 +0200264auto DrmDevice::GetConnectors()
265 -> const std::vector<std::unique_ptr<DrmConnector>> & {
266 return connectors_;
267}
268
269auto DrmDevice::GetPlanes() -> const std::vector<std::unique_ptr<DrmPlane>> & {
270 return planes_;
271}
272
273auto DrmDevice::GetCrtcs() -> const std::vector<std::unique_ptr<DrmCrtc>> & {
274 return crtcs_;
275}
276
277auto DrmDevice::GetEncoders()
278 -> const std::vector<std::unique_ptr<DrmEncoder>> & {
279 return encoders_;
280}
281
Sean Paulf72cccd2018-08-27 13:59:08 -0400282} // namespace android