blob: 1d6b62eaa2bffb794ba24aecbc622845335b5e6f [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 <xf86drm.h>
22#include <xf86drmMode.h>
23
Roman Stratiienko13cc3662020-08-29 21:35:39 +030024#include <cinttypes>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020025#include <cstdint>
Roman Kovalivskyid07c3702019-11-04 17:54:31 +020026#include <string>
27
Roman Stratiienko4e994052022-02-09 17:40:35 +020028#include "drm/DrmAtomicStateManager.h"
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020029#include "drm/DrmPlane.h"
Roman Stratiienko5f21dbc2022-05-03 18:31:13 +030030#include "drm/ResourceManager.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 Stratiienko5f21dbc2022-05-03 18:31:13 +030036auto DrmDevice::CreateInstance(std::string const &path,
37 ResourceManager *res_man)
38 -> std::unique_ptr<DrmDevice> {
39 if (!IsKMSDev(path.c_str())) {
40 return {};
41 }
42
43 auto device = std::unique_ptr<DrmDevice>(new DrmDevice(res_man));
44
45 if (device->Init(path.c_str()) != 0) {
46 return {};
47 }
48
49 return device;
50}
51
52DrmDevice::DrmDevice(ResourceManager *res_man) : res_man_(res_man) {
Roman Stratiienko7d899112022-01-31 11:30:27 +020053 drm_fb_importer_ = std::make_unique<DrmFbImporter>(*this);
Sean Paul047b9b22015-07-28 14:15:42 -040054}
55
Roman Stratiienko3dacd472022-01-11 19:18:34 +020056auto DrmDevice::Init(const char *path) -> int {
Sean Paul6a55e9f2015-04-30 15:31:06 -040057 /* TODO: Use drmOpenControl here instead */
Roman Stratiienko76892782023-01-16 17:15:53 +020058 fd_ = MakeSharedFd(open(path, O_RDWR | O_CLOEXEC));
Roman Stratiienko7d899112022-01-31 11:30:27 +020059 if (!fd_) {
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +020060 // NOLINTNEXTLINE(concurrency-mt-unsafe): Fixme
Peter Collingbournec77052e2020-02-19 11:25:08 -080061 ALOGE("Failed to open dri %s: %s", path, strerror(errno));
Roman Stratiienko3dacd472022-01-11 19:18:34 +020062 return -ENODEV;
Sean Paul6a55e9f2015-04-30 15:31:06 -040063 }
64
Roman Stratiienko76892782023-01-16 17:15:53 +020065 int ret = drmSetClientCap(*GetFd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Roman Stratiienko7d899112022-01-31 11:30:27 +020066 if (ret != 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040067 ALOGE("Failed to set universal plane cap %d", ret);
Roman Stratiienko3dacd472022-01-11 19:18:34 +020068 return ret;
Sean Paul6a55e9f2015-04-30 15:31:06 -040069 }
70
Roman Stratiienko76892782023-01-16 17:15:53 +020071 ret = drmSetClientCap(*GetFd(), DRM_CLIENT_CAP_ATOMIC, 1);
Roman Stratiienko7d899112022-01-31 11:30:27 +020072 if (ret != 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040073 ALOGE("Failed to set atomic cap %d", ret);
Roman Stratiienko3dacd472022-01-11 19:18:34 +020074 return ret;
Sean Paul6a55e9f2015-04-30 15:31:06 -040075 }
76
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000077#ifdef DRM_CLIENT_CAP_WRITEBACK_CONNECTORS
Roman Stratiienko76892782023-01-16 17:15:53 +020078 ret = drmSetClientCap(*GetFd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1);
Roman Stratiienko7d899112022-01-31 11:30:27 +020079 if (ret != 0) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000080 ALOGI("Failed to set writeback cap %d", ret);
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +000081 }
82#endif
83
Roman Stratiienko8666dc92021-02-09 17:49:55 +020084 uint64_t cap_value = 0;
Roman Stratiienko76892782023-01-16 17:15:53 +020085 if (drmGetCap(*GetFd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value) != 0) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +020086 ALOGW("drmGetCap failed. Fallback to no modifier support.");
87 cap_value = 0;
88 }
89 HasAddFb2ModifiersSupport_ = cap_value != 0;
90
Roman Stratiienko76892782023-01-16 17:15:53 +020091 drmSetMaster(*GetFd());
92 if (drmIsMaster(*GetFd()) == 0) {
John Stultzfa002332021-02-02 01:34:45 +000093 ALOGE("DRM/KMS master access required");
Roman Stratiienko3dacd472022-01-11 19:18:34 +020094 return -EACCES;
Roman Stratiienko3b24cd92021-01-13 10:32:04 +020095 }
96
Roman Stratiienko76892782023-01-16 17:15:53 +020097 auto res = MakeDrmModeResUnique(*GetFd());
Sean Paul6a55e9f2015-04-30 15:31:06 -040098 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010099 ALOGE("Failed to get DrmDevice resources");
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200100 return -ENODEV;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400101 }
102
Sean Paulf72cccd2018-08-27 13:59:08 -0400103 min_resolution_ = std::pair<uint32_t, uint32_t>(res->min_width,
104 res->min_height);
105 max_resolution_ = std::pair<uint32_t, uint32_t>(res->max_width,
106 res->max_height);
Sean Paul406dbfc2016-02-10 15:35:17 -0800107
Roman Stratiienko10be8752022-01-30 20:28:46 +0200108 for (int i = 0; i < res->count_crtcs; ++i) {
109 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
110 auto crtc = DrmCrtc::CreateInstance(*this, res->crtcs[i], i);
111 if (crtc) {
112 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400113 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400114 }
115
Roman Stratiienko027987b2022-01-30 21:06:35 +0200116 for (int i = 0; i < res->count_encoders; ++i) {
117 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
118 auto enc = DrmEncoder::CreateInstance(*this, res->encoders[i], i);
119 if (enc) {
120 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400121 }
Alexandru Gheorghee8b668c2018-03-22 11:31:29 +0000122 }
123
Roman Stratiienko650299a2022-01-30 23:46:10 +0200124 for (int i = 0; i < res->count_connectors; ++i) {
125 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
126 auto conn = DrmConnector::CreateInstance(*this, res->connectors[i], i);
127
128 if (!conn) {
129 continue;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400130 }
131
Roman Stratiienko650299a2022-01-30 23:46:10 +0200132 if (conn->IsWriteback()) {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000133 writeback_connectors_.emplace_back(std::move(conn));
Roman Stratiienko650299a2022-01-30 23:46:10 +0200134 } else {
Alexandru Gheorgheb46b9302018-03-21 14:19:58 +0000135 connectors_.emplace_back(std::move(conn));
Roman Stratiienko650299a2022-01-30 23:46:10 +0200136 }
Robert Foss610d9892017-11-01 12:50:04 -0500137 }
138
Roman Stratiienko76892782023-01-16 17:15:53 +0200139 auto plane_res = MakeDrmModePlaneResUnique(*GetFd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400140 if (!plane_res) {
141 ALOGE("Failed to get plane resources");
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200142 return -ENOENT;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400143 }
144
145 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Roman Stratiienkob671fab2022-01-29 00:50:22 +0200146 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
147 auto plane = DrmPlane::CreateInstance(*this, plane_res->planes[i]);
148
149 if (plane) {
150 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400151 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400152 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400153
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200154 return 0;
Sean Paul877be972015-06-03 14:08:27 -0400155}
156
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300157auto DrmDevice::RegisterUserPropertyBlob(void *data, size_t length) const
158 -> DrmModeUserPropertyBlobUnique {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200159 struct drm_mode_create_blob create_blob {};
Sean Paul877be972015-06-03 14:08:27 -0400160 create_blob.length = length;
Roman Stratiienko7d899112022-01-31 11:30:27 +0200161 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
Sean Paul877be972015-06-03 14:08:27 -0400162 create_blob.data = (__u64)data;
163
Roman Stratiienko76892782023-01-16 17:15:53 +0200164 auto ret = drmIoctl(*GetFd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Roman Stratiienko7d899112022-01-31 11:30:27 +0200165 if (ret != 0) {
Sean Paul877be972015-06-03 14:08:27 -0400166 ALOGE("Failed to create mode property blob %d", ret);
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200167 return {};
Sean Paul877be972015-06-03 14:08:27 -0400168 }
Sean Paul877be972015-06-03 14:08:27 -0400169
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300170 return DrmModeUserPropertyBlobUnique(
171 new uint32_t(create_blob.blob_id), [this](const uint32_t *it) {
172 struct drm_mode_destroy_blob destroy_blob {};
173 destroy_blob.blob_id = (__u32)*it;
Roman Stratiienko76892782023-01-16 17:15:53 +0200174 auto err = drmIoctl(*GetFd(), DRM_IOCTL_MODE_DESTROYPROPBLOB,
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300175 &destroy_blob);
Roman Stratiienko6ede4662021-09-30 10:18:28 +0300176 if (err != 0) {
177 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", *it,
178 err);
179 }
180 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
181 delete it;
182 });
Sean Paul877be972015-06-03 14:08:27 -0400183}
184
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100185int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200186 const char *prop_name, DrmProperty *property) const {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200187 drmModeObjectPropertiesPtr props = nullptr;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400188
Roman Stratiienko76892782023-01-16 17:15:53 +0200189 props = drmModeObjectGetProperties(*GetFd(), obj_id, obj_type);
Roman Stratiienko7d899112022-01-31 11:30:27 +0200190 if (props == nullptr) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400191 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
192 return -ENODEV;
193 }
194
195 bool found = false;
196 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Roman Stratiienko7d899112022-01-31 11:30:27 +0200197 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
Roman Stratiienko76892782023-01-16 17:15:53 +0200198 drmModePropertyPtr p = drmModeGetProperty(*GetFd(), props->props[i]);
Roman Stratiienko7d899112022-01-31 11:30:27 +0200199 if (strcmp(p->name, prop_name) == 0) {
200 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
Roman Stratiienko7fd8f882021-09-29 12:46:54 +0300201 property->Init(obj_id, p, props->prop_values[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400202 found = true;
203 }
204 drmModeFreeProperty(p);
205 }
206
207 drmModeFreeObjectProperties(props);
208 return found ? 0 : -ENOENT;
209}
210
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200211std::string DrmDevice::GetName() const {
Roman Stratiienko76892782023-01-16 17:15:53 +0200212 auto *ver = drmGetVersion(*GetFd());
Roman Stratiienko7d899112022-01-31 11:30:27 +0200213 if (ver == nullptr) {
Roman Stratiienko76892782023-01-16 17:15:53 +0200214 ALOGW("Failed to get drm version for fd=%d", *GetFd());
Matvii Zorinef3c7972020-08-11 15:15:44 +0300215 return "generic";
216 }
217
218 std::string name(ver->name);
219 drmFreeVersion(ver);
220 return name;
221}
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300222
223auto DrmDevice::IsKMSDev(const char *path) -> bool {
Roman Stratiienko76892782023-01-16 17:15:53 +0200224 auto fd = MakeUniqueFd(open(path, O_RDWR | O_CLOEXEC));
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300225 if (!fd) {
226 return false;
227 }
228
Roman Stratiienko76892782023-01-16 17:15:53 +0200229 auto res = MakeDrmModeResUnique(*fd);
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300230 if (!res) {
231 return false;
232 }
233
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300234 auto is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
Roman Stratiienko56f4adc2021-09-29 12:47:12 +0300235 res->count_encoders > 0;
236
237 return is_kms;
238}
239
Roman Stratiienko7d899112022-01-31 11:30:27 +0200240auto DrmDevice::GetConnectors()
241 -> const std::vector<std::unique_ptr<DrmConnector>> & {
242 return connectors_;
243}
244
245auto DrmDevice::GetPlanes() -> const std::vector<std::unique_ptr<DrmPlane>> & {
246 return planes_;
247}
248
249auto DrmDevice::GetCrtcs() -> const std::vector<std::unique_ptr<DrmCrtc>> & {
250 return crtcs_;
251}
252
253auto DrmDevice::GetEncoders()
254 -> const std::vector<std::unique_ptr<DrmEncoder>> & {
255 return encoders_;
256}
257
Sean Paulf72cccd2018-08-27 13:59:08 -0400258} // namespace android