blob: fd4589e3c6bec0cf04cce7f1cebfa2abf52ea622 [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 Stratiienko13cc3662020-08-29 21:35:39 +030025#include <cinttypes>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020026#include <cstdint>
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020027#include <string>
28
Roman Stratiienko4e994052022-02-09 17:40:35 +020029#include "drm/DrmAtomicStateManager.h"
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020030#include "drm/DrmPlane.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020031#include "utils/log.h"
32#include "utils/properties.h"
33
Sean Paul6a55e9f2015-04-30 15:31:06 -040034namespace android {
35
Roman Stratiienko1e053b42021-10-25 22:54:20 +030036DrmDevice::DrmDevice() {
Roman Stratiienko7d899112022-01-31 11:30:27 +020037 drm_fb_importer_ = std::make_unique<DrmFbImporter>(*this);
Sean Paul047b9b22015-07-28 14:15:42 -040038}
39
Roman Stratiienko3dacd472022-01-11 19:18:34 +020040auto DrmDevice::Init(const char *path) -> int {
Sean Paul6a55e9f2015-04-30 15:31:06 -040041 /* TODO: Use drmOpenControl here instead */
Roman Stratiienko0fade372021-02-20 13:59:55 +020042 fd_ = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
Roman Stratiienko7d899112022-01-31 11:30:27 +020043 if (!fd_) {
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020044 // NOLINTNEXTLINE(concurrency-mt-unsafe): Fixme
Peter Collingbournec77052e2020-02-19 11:25:08 -080045 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Roman Stratiienko3dacd472022-01-11 19:18:34 +020046 return -ENODEV;
Sean Paul6a55e9f2015-04-30 15:31:06 -040047 }
48
Roman Stratiienko7d899112022-01-31 11:30:27 +020049 int ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
50 if (ret != 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040051 ALOGE("Failed to set universal plane cap %d", ret);
Roman Stratiienko3dacd472022-01-11 19:18:34 +020052 return ret;
Sean Paul6a55e9f2015-04-30 15:31:06 -040053 }
54
Roman Stratiienko7d899112022-01-31 11:30:27 +020055 ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_ATOMIC, 1);
56 if (ret != 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040057 ALOGE("Failed to set atomic cap %d", ret);
Roman Stratiienko3dacd472022-01-11 19:18:34 +020058 return ret;
Sean Paul6a55e9f2015-04-30 15:31:06 -040059 }
60
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000061#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
Roman Stratiienko7d899112022-01-31 11:30:27 +020062 ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
63 if (ret != 0) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000064 ALOGI("Failed to set writeback cap %d", ret);
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000065 }
66#endif
67
Roman Stratiienko8666dc92021-02-09 17:49:55 +020068 uint64_t cap_value = 0;
Roman Stratiienko7d899112022-01-31 11:30:27 +020069 if (drmGetCap(GetFd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value) != 0) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +020070 ALOGW("drmGetCap failed. Fallback to no modifier support.");
71 cap_value = 0;
72 }
73 HasAddFb2ModifiersSupport_ = cap_value != 0;
74
Roman Stratiienko7d899112022-01-31 11:30:27 +020075 drmSetMaster(GetFd());
76 if (drmIsMaster(GetFd()) == 0) {
John Stultzfa002332021-02-02 01:34:45 +000077 ALOGE("DRM/KMS master access required");
Roman Stratiienko3dacd472022-01-11 19:18:34 +020078 return -EACCES;
Roman Stratiienko3b24cd92021-01-13 10:32:04 +020079 }
80
Roman Stratiienko7d899112022-01-31 11:30:27 +020081 auto res = MakeDrmModeResUnique(GetFd());
Sean Paul6a55e9f2015-04-30 15:31:06 -040082 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010083 ALOGE("Failed to get DrmDevice resources");
Roman Stratiienko3dacd472022-01-11 19:18:34 +020084 return -ENODEV;
Sean Paul6a55e9f2015-04-30 15:31:06 -040085 }
86
Sean Paulf72cccd2018-08-27 13:59:08 -040087 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
88 res->min_height);
89 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
90 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -080091
Roman Stratiienko10be8752022-01-30 20:28:46 +020092 for (int i = 0; i < res->count_crtcs; ++i) {
93 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
94 auto crtc = DrmCrtc::CreateInstance(*this, res->crtcs[i], i);
95 if (crtc) {
96 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -040097 }
Sean Paul6a55e9f2015-04-30 15:31:06 -040098 }
99
Roman Stratiienko027987b2022-01-30 21:06:35 +0200100 for (int i = 0; i < res->count_encoders; ++i) {
101 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
102 auto enc = DrmEncoder::CreateInstance(*this, res->encoders[i], i);
103 if (enc) {
104 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400105 }
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000106 }
107
Roman Stratiienko650299a2022-01-30 23:46:10 +0200108 for (int i = 0; i < res->count_connectors; ++i) {
109 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
110 auto conn = DrmConnector::CreateInstance(*this, res->connectors[i], i);
111
112 if (!conn) {
113 continue;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400114 }
115
Roman Stratiienko650299a2022-01-30 23:46:10 +0200116 if (conn->IsWriteback()) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000117 writeback_connectors_.emplace_back(std::move(conn));
Roman Stratiienko650299a2022-01-30 23:46:10 +0200118 } else {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000119 connectors_.emplace_back(std::move(conn));
Roman Stratiienko650299a2022-01-30 23:46:10 +0200120 }
Robert Foss610d9892017-11-01 12:50:04 -0500121 }
122
Roman Stratiienko7d899112022-01-31 11:30:27 +0200123 auto plane_res = MakeDrmModePlaneResUnique(GetFd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400124 if (!plane_res) {
125 ALOGE("Failed to get plane resources");
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200126 return -ENOENT;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400127 }
128
129 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Roman Stratiienkob671fab2022-01-29 00:50:22 +0200130 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
131 auto plane = DrmPlane::CreateInstance(*this, plane_res->planes[i]);
132
133 if (plane) {
134 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400135 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400136 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400137
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200138 return 0;
Sean Paul877be972015-06-03 14:08:27 -0400139}
140
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300141auto DrmDevice::RegisterUserPropertyBlob(void *data, size_t length) const
142 -> DrmModeUserPropertyBlobUnique {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200143 struct drm_mode_create_blob create_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400144 create_blob.length = length;
Roman Stratiienko7d899112022-01-31 11:30:27 +0200145 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
Sean Paul877be972015-06-03 14:08:27 -0400146 create_blob.data = (__u64)data;
147
Roman Stratiienko7d899112022-01-31 11:30:27 +0200148 int ret = drmIoctl(GetFd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
149 if (ret != 0) {
Sean Paul877be972015-06-03 14:08:27 -0400150 ALOGE("Failed to create mode property blob %d", ret);
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200151 return {};
Sean Paul877be972015-06-03 14:08:27 -0400152 }
Sean Paul877be972015-06-03 14:08:27 -0400153
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300154 return DrmModeUserPropertyBlobUnique(
155 new uint32_t(create_blob.blob_id), [this](const uint32_t *it) {
156 struct drm_mode_destroy_blob destroy_blob {};
157 destroy_blob.blob_id = (__u32)*it;
Roman Stratiienko7d899112022-01-31 11:30:27 +0200158 int err = drmIoctl(GetFd(), DRM_IOCTL_MODE_DESTROYPROPBLOB,
159 &destroy_blob);
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300160 if (err != 0) {
161 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", *it,
162 err);
163 }
164 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
165 delete it;
166 });
Sean Paul877be972015-06-03 14:08:27 -0400167}
168
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100169int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200170 const char *prop_name, DrmProperty *property) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200171 drmModeObjectPropertiesPtr props = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400172
Roman Stratiienko7d899112022-01-31 11:30:27 +0200173 props = drmModeObjectGetProperties(GetFd(), obj_id, obj_type);
174 if (props == nullptr) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400175 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
176 return -ENODEV;
177 }
178
179 bool found = false;
180 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Roman Stratiienko7d899112022-01-31 11:30:27 +0200181 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
182 drmModePropertyPtr p = drmModeGetProperty(GetFd(), props->props[i]);
183 if (strcmp(p->name, prop_name) == 0) {
184 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300185 property->Init(obj_id, p, props->prop_values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400186 found = true;
187 }
188 drmModeFreeProperty(p);
189 }
190
191 drmModeFreeObjectProperties(props);
192 return found ? 0 : -ENOENT;
193}
194
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200195std::string DrmDevice::GetName() const {
Roman Stratiienko7d899112022-01-31 11:30:27 +0200196 auto *ver = drmGetVersion(GetFd());
197 if (ver == nullptr) {
198 ALOGW("Failed to get drm version for fd=%d", GetFd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300199 return "generic";
200 }
201
202 std::string name(ver->name);
203 drmFreeVersion(ver);
204 return name;
205}
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300206
207auto DrmDevice::IsKMSDev(const char *path) -> bool {
208 auto fd = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
209 if (!fd) {
210 return false;
211 }
212
213 auto res = MakeDrmModeResUnique(fd.Get());
214 if (!res) {
215 return false;
216 }
217
218 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
219 res->count_encoders > 0;
220
221 return is_kms;
222}
223
Roman Stratiienko7d899112022-01-31 11:30:27 +0200224auto DrmDevice::GetConnectors()
225 -> const std::vector<std::unique_ptr<DrmConnector>> & {
226 return connectors_;
227}
228
229auto DrmDevice::GetPlanes() -> const std::vector<std::unique_ptr<DrmPlane>> & {
230 return planes_;
231}
232
233auto DrmDevice::GetCrtcs() -> const std::vector<std::unique_ptr<DrmCrtc>> & {
234 return crtcs_;
235}
236
237auto DrmDevice::GetEncoders()
238 -> const std::vector<std::unique_ptr<DrmEncoder>> & {
239 return encoders_;
240}
241
Sean Paulf72cccd2018-08-27 13:59:08 -0400242} // namespace android