blob: 5e160d419a1f28c473c53a9fb9058c5cdb2ac94f [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 Stratiienko5f21dbc2022-05-03 18:31:13 +030031#include "drm/ResourceManager.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020032#include "utils/log.h"
33#include "utils/properties.h"
34
Sean Paul6a55e9f2015-04-30 15:31:06 -040035namespace android {
36
Roman Stratiienko5f21dbc2022-05-03 18:31:13 +030037auto DrmDevice::CreateInstance(std::string const &path,
38 ResourceManager *res_man)
39 -> std::unique_ptr<DrmDevice> {
40 if (!IsKMSDev(path.c_str())) {
41 return {};
42 }
43
44 auto device = std::unique_ptr<DrmDevice>(new DrmDevice(res_man));
45
46 if (device->Init(path.c_str()) != 0) {
47 return {};
48 }
49
50 return device;
51}
52
53DrmDevice::DrmDevice(ResourceManager *res_man) : res_man_(res_man) {
Roman Stratiienko7d899112022-01-31 11:30:27 +020054 drm_fb_importer_ = std::make_unique<DrmFbImporter>(*this);
Sean Paul047b9b22015-07-28 14:15:42 -040055}
56
Roman Stratiienko3dacd472022-01-11 19:18:34 +020057auto DrmDevice::Init(const char *path) -> int {
Sean Paul6a55e9f2015-04-30 15:31:06 -040058 /* TODO: Use drmOpenControl here instead */
Roman Stratiienko0fade372021-02-20 13:59:55 +020059 fd_ = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
Roman Stratiienko7d899112022-01-31 11:30:27 +020060 if (!fd_) {
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020061 // NOLINTNEXTLINE(concurrency-mt-unsafe): Fixme
Peter Collingbournec77052e2020-02-19 11:25:08 -080062 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Roman Stratiienko3dacd472022-01-11 19:18:34 +020063 return -ENODEV;
Sean Paul6a55e9f2015-04-30 15:31:06 -040064 }
65
Roman Stratiienko7d899112022-01-31 11:30:27 +020066 int ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
67 if (ret != 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040068 ALOGE("Failed to set universal plane cap %d", ret);
Roman Stratiienko3dacd472022-01-11 19:18:34 +020069 return ret;
Sean Paul6a55e9f2015-04-30 15:31:06 -040070 }
71
Roman Stratiienko7d899112022-01-31 11:30:27 +020072 ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_ATOMIC, 1);
73 if (ret != 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040074 ALOGE("Failed to set atomic cap %d", ret);
Roman Stratiienko3dacd472022-01-11 19:18:34 +020075 return ret;
Sean Paul6a55e9f2015-04-30 15:31:06 -040076 }
77
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000078#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
Roman Stratiienko7d899112022-01-31 11:30:27 +020079 ret = drmSetClientCap(GetFd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
80 if (ret != 0) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000081 ALOGI("Failed to set writeback cap %d", ret);
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000082 }
83#endif
84
Roman Stratiienko8666dc92021-02-09 17:49:55 +020085 uint64_t cap_value = 0;
Roman Stratiienko7d899112022-01-31 11:30:27 +020086 if (drmGetCap(GetFd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value) != 0) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +020087 ALOGW("drmGetCap failed. Fallback to no modifier support.");
88 cap_value = 0;
89 }
90 HasAddFb2ModifiersSupport_ = cap_value != 0;
91
Roman Stratiienko7d899112022-01-31 11:30:27 +020092 drmSetMaster(GetFd());
93 if (drmIsMaster(GetFd()) == 0) {
John Stultzfa002332021-02-02 01:34:45 +000094 ALOGE("DRM/KMS master access required");
Roman Stratiienko3dacd472022-01-11 19:18:34 +020095 return -EACCES;
Roman Stratiienko3b24cd92021-01-13 10:32:04 +020096 }
97
Roman Stratiienko7d899112022-01-31 11:30:27 +020098 auto res = MakeDrmModeResUnique(GetFd());
Sean Paul6a55e9f2015-04-30 15:31:06 -040099 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100100 ALOGE("Failed to get DrmDevice resources");
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200101 return -ENODEV;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400102 }
103
Sean Paulf72cccd2018-08-27 13:59:08 -0400104 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
105 res->min_height);
106 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
107 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -0800108
Roman Stratiienko10be8752022-01-30 20:28:46 +0200109 for (int i = 0; i < res->count_crtcs; ++i) {
110 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
111 auto crtc = DrmCrtc::CreateInstance(*this, res->crtcs[i], i);
112 if (crtc) {
113 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400114 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400115 }
116
Roman Stratiienko027987b2022-01-30 21:06:35 +0200117 for (int i = 0; i < res->count_encoders; ++i) {
118 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
119 auto enc = DrmEncoder::CreateInstance(*this, res->encoders[i], i);
120 if (enc) {
121 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400122 }
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000123 }
124
Roman Stratiienko650299a2022-01-30 23:46:10 +0200125 for (int i = 0; i < res->count_connectors; ++i) {
126 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
127 auto conn = DrmConnector::CreateInstance(*this, res->connectors[i], i);
128
129 if (!conn) {
130 continue;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400131 }
132
Roman Stratiienko650299a2022-01-30 23:46:10 +0200133 if (conn->IsWriteback()) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000134 writeback_connectors_.emplace_back(std::move(conn));
Roman Stratiienko650299a2022-01-30 23:46:10 +0200135 } else {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000136 connectors_.emplace_back(std::move(conn));
Roman Stratiienko650299a2022-01-30 23:46:10 +0200137 }
Robert Foss610d9892017-11-01 12:50:04 -0500138 }
139
Roman Stratiienko7d899112022-01-31 11:30:27 +0200140 auto plane_res = MakeDrmModePlaneResUnique(GetFd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400141 if (!plane_res) {
142 ALOGE("Failed to get plane resources");
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200143 return -ENOENT;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400144 }
145
146 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Roman Stratiienkob671fab2022-01-29 00:50:22 +0200147 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
148 auto plane = DrmPlane::CreateInstance(*this, plane_res->planes[i]);
149
150 if (plane) {
151 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400152 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400153 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400154
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200155 return 0;
Sean Paul877be972015-06-03 14:08:27 -0400156}
157
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300158auto DrmDevice::RegisterUserPropertyBlob(void *data, size_t length) const
159 -> DrmModeUserPropertyBlobUnique {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200160 struct drm_mode_create_blob create_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400161 create_blob.length = length;
Roman Stratiienko7d899112022-01-31 11:30:27 +0200162 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
Sean Paul877be972015-06-03 14:08:27 -0400163 create_blob.data = (__u64)data;
164
Roman Stratiienko7d899112022-01-31 11:30:27 +0200165 int ret = drmIoctl(GetFd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
166 if (ret != 0) {
Sean Paul877be972015-06-03 14:08:27 -0400167 ALOGE("Failed to create mode property blob %d", ret);
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200168 return {};
Sean Paul877be972015-06-03 14:08:27 -0400169 }
Sean Paul877be972015-06-03 14:08:27 -0400170
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300171 return DrmModeUserPropertyBlobUnique(
172 new uint32_t(create_blob.blob_id), [this](const uint32_t *it) {
173 struct drm_mode_destroy_blob destroy_blob {};
174 destroy_blob.blob_id = (__u32)*it;
Roman Stratiienko7d899112022-01-31 11:30:27 +0200175 int err = drmIoctl(GetFd(), DRM_IOCTL_MODE_DESTROYPROPBLOB,
176 &destroy_blob);
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300177 if (err != 0) {
178 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", *it,
179 err);
180 }
181 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
182 delete it;
183 });
Sean Paul877be972015-06-03 14:08:27 -0400184}
185
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100186int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200187 const char *prop_name, DrmProperty *property) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200188 drmModeObjectPropertiesPtr props = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400189
Roman Stratiienko7d899112022-01-31 11:30:27 +0200190 props = drmModeObjectGetProperties(GetFd(), obj_id, obj_type);
191 if (props == nullptr) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400192 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
193 return -ENODEV;
194 }
195
196 bool found = false;
197 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Roman Stratiienko7d899112022-01-31 11:30:27 +0200198 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
199 drmModePropertyPtr p = drmModeGetProperty(GetFd(), props->props[i]);
200 if (strcmp(p->name, prop_name) == 0) {
201 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300202 property->Init(obj_id, p, props->prop_values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400203 found = true;
204 }
205 drmModeFreeProperty(p);
206 }
207
208 drmModeFreeObjectProperties(props);
209 return found ? 0 : -ENOENT;
210}
211
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200212std::string DrmDevice::GetName() const {
Roman Stratiienko7d899112022-01-31 11:30:27 +0200213 auto *ver = drmGetVersion(GetFd());
214 if (ver == nullptr) {
215 ALOGW("Failed to get drm version for fd=%d", GetFd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300216 return "generic";
217 }
218
219 std::string name(ver->name);
220 drmFreeVersion(ver);
221 return name;
222}
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300223
224auto DrmDevice::IsKMSDev(const char *path) -> bool {
225 auto fd = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
226 if (!fd) {
227 return false;
228 }
229
230 auto res = MakeDrmModeResUnique(fd.Get());
231 if (!res) {
232 return false;
233 }
234
235 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
236 res->count_encoders > 0;
237
238 return is_kms;
239}
240
Roman Stratiienko7d899112022-01-31 11:30:27 +0200241auto DrmDevice::GetConnectors()
242 -> const std::vector<std::unique_ptr<DrmConnector>> & {
243 return connectors_;
244}
245
246auto DrmDevice::GetPlanes() -> const std::vector<std::unique_ptr<DrmPlane>> & {
247 return planes_;
248}
249
250auto DrmDevice::GetCrtcs() -> const std::vector<std::unique_ptr<DrmCrtc>> & {
251 return crtcs_;
252}
253
254auto DrmDevice::GetEncoders()
255 -> const std::vector<std::unique_ptr<DrmEncoder>> & {
256 return encoders_;
257}
258
Sean Paulf72cccd2018-08-27 13:59:08 -0400259} // namespace android