blob: 7a54c9d60dcd72e31fc7fb520b4eaaf2d2358a5f [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
19#include "drmconnector.h"
20#include "drmcrtc.h"
21#include "drmencoder.h"
Sean Paul047b9b22015-07-28 14:15:42 -040022#include "drmeventlistener.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040023#include "drmplane.h"
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010024#include "drmdevice.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040025
Sean Paulf741c672016-05-11 13:49:38 -040026#include <cinttypes>
Sean Paul6a55e9f2015-04-30 15:31:06 -040027#include <errno.h>
28#include <fcntl.h>
29#include <stdint.h>
30#include <xf86drm.h>
31#include <xf86drmMode.h>
32
John Stultz9057a6f2018-04-26 12:05:55 -070033#include <log/log.h>
Sean Paul6a55e9f2015-04-30 15:31:06 -040034#include <cutils/properties.h>
35
36namespace android {
37
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010038DrmDevice::DrmDevice() : event_listener_(this) {
Sean Paul047b9b22015-07-28 14:15:42 -040039}
40
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010041DrmDevice::~DrmDevice() {
Sean Paul047b9b22015-07-28 14:15:42 -040042 event_listener_.Exit();
Sean Paul6a55e9f2015-04-30 15:31:06 -040043}
44
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 */
Zach Reiznerff30b522015-10-28 19:08:45 -070047 fd_.Set(open(path, O_RDWR));
48 if (fd() < 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040049 ALOGE("Failed to open dri- %s", 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
Zach Reiznerff30b522015-10-28 19:08:45 -070053 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040054 if (ret) {
55 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
Zach Reiznerff30b522015-10-28 19:08:45 -070059 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040060 if (ret) {
61 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
Zach Reiznerff30b522015-10-28 19:08:45 -070065 drmModeResPtr res = drmModeGetResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -040066 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010067 ALOGE("Failed to get DrmDevice resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010068 return std::make_tuple(-ENODEV, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -040069 }
70
Sean Paul406dbfc2016-02-10 15:35:17 -080071 min_resolution_ =
72 std::pair<uint32_t, uint32_t>(res->min_width, res->min_height);
73 max_resolution_ =
74 std::pair<uint32_t, uint32_t>(res->max_width, res->max_height);
75
Alexandru Gheorghec5463582018-03-27 15:52:02 +010076 // Assumes that the primary display will always be in the first
77 // drm_device opened.
78 bool found_primary = num_displays != 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -040079
80 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -070081 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040082 if (!c) {
83 ALOGE("Failed to get crtc %d", res->crtcs[i]);
84 ret = -ENODEV;
85 break;
86 }
87
Zach Reiznerff30b522015-10-28 19:08:45 -070088 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
Sean Paul6a55e9f2015-04-30 15:31:06 -040089 drmModeFreeCrtc(c);
90
Sean Paul877be972015-06-03 14:08:27 -040091 ret = crtc->Init();
92 if (ret) {
93 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -040094 break;
95 }
Zach Reiznerff30b522015-10-28 19:08:45 -070096 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -040097 }
98
99 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700100 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400101 if (!e) {
102 ALOGE("Failed to get encoder %d", res->encoders[i]);
103 ret = -ENODEV;
104 break;
105 }
106
107 std::vector<DrmCrtc *> possible_crtcs;
108 DrmCrtc *current_crtc = NULL;
Zach Reiznerff30b522015-10-28 19:08:45 -0700109 for (auto &crtc : crtcs_) {
110 if ((1 << crtc->pipe()) & e->possible_crtcs)
111 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400112
Zach Reiznerff30b522015-10-28 19:08:45 -0700113 if (crtc->id() == e->crtc_id)
114 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400115 }
116
Zach Reiznerff30b522015-10-28 19:08:45 -0700117 std::unique_ptr<DrmEncoder> enc(
118 new DrmEncoder(e, current_crtc, possible_crtcs));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400119
120 drmModeFreeEncoder(e);
121
Zach Reiznerff30b522015-10-28 19:08:45 -0700122 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400123 }
124
125 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700126 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400127 if (!c) {
128 ALOGE("Failed to get connector %d", res->connectors[i]);
129 ret = -ENODEV;
130 break;
131 }
132
133 std::vector<DrmEncoder *> possible_encoders;
134 DrmEncoder *current_encoder = NULL;
135 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700136 for (auto &encoder : encoders_) {
137 if (encoder->id() == c->encoders[j])
138 possible_encoders.push_back(encoder.get());
139 if (encoder->id() == c->encoder_id)
140 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400141 }
142 }
143
Zach Reiznerff30b522015-10-28 19:08:45 -0700144 std::unique_ptr<DrmConnector> conn(
145 new DrmConnector(this, c, current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400146
147 drmModeFreeConnector(c);
148
Sean Paul6a55e9f2015-04-30 15:31:06 -0400149 ret = conn->Init();
150 if (ret) {
151 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400152 break;
153 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400154
Robert Foss610d9892017-11-01 12:50:04 -0500155 connectors_.emplace_back(std::move(conn));
156 }
157
158 // First look for primary amongst internal connectors
159 for (auto &conn : connectors_) {
160 if (conn->internal() && !found_primary) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100161 conn->set_display(num_displays);
162 displays_[num_displays] = num_displays;
163 ++num_displays;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400164 found_primary = true;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100165 break;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400166 }
167 }
Robert Foss610d9892017-11-01 12:50:04 -0500168
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100169 // Then pick first available as primary and for the others assign
170 // consecutive display_numbers.
Robert Foss610d9892017-11-01 12:50:04 -0500171 for (auto &conn : connectors_) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100172 if (conn->external() || conn->internal()) {
173 if (!found_primary) {
174 conn->set_display(num_displays);
175 displays_[num_displays] = num_displays;
176 found_primary = true;
177 ++num_displays;
178 } else if (conn->display() < 0) {
179 conn->set_display(num_displays);
180 displays_[num_displays] = num_displays;
181 ++num_displays;
182 }
Robert Foss610d9892017-11-01 12:50:04 -0500183 }
184 }
185
Sean Paul6a55e9f2015-04-30 15:31:06 -0400186 if (res)
187 drmModeFreeResources(res);
188
189 // Catch-all for the above loops
190 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100191 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400192
Zach Reiznerff30b522015-10-28 19:08:45 -0700193 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400194 if (!plane_res) {
195 ALOGE("Failed to get plane resources");
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100196 return std::make_tuple(-ENOENT, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400197 }
198
199 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700200 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400201 if (!p) {
202 ALOGE("Failed to get plane %d", plane_res->planes[i]);
203 ret = -ENODEV;
204 break;
205 }
206
Zach Reiznerff30b522015-10-28 19:08:45 -0700207 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400208
209 drmModeFreePlane(p);
210
Sean Paul6a55e9f2015-04-30 15:31:06 -0400211 ret = plane->Init();
212 if (ret) {
213 ALOGE("Init plane %d failed", plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400214 break;
215 }
216
Zach Reiznerff30b522015-10-28 19:08:45 -0700217 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400218 }
219 drmModeFreePlaneResources(plane_res);
220 if (ret)
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100221 return std::make_tuple(ret, 0);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400222
Sean Paul047b9b22015-07-28 14:15:42 -0400223 ret = event_listener_.Init();
224 if (ret) {
225 ALOGE("Can't initialize event listener %d", ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100226 return std::make_tuple(ret, 0);
Sean Paul047b9b22015-07-28 14:15:42 -0400227 }
228
Zach Reiznerff30b522015-10-28 19:08:45 -0700229 for (auto &conn : connectors_) {
230 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400231 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700232 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100233 return std::make_tuple(ret, 0);
Sean Paul57355412015-09-19 09:14:34 -0400234 }
235 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100236 return std::make_tuple(ret, displays_.size());
237}
238
239bool DrmDevice::HandlesDisplay(int display) const {
240 return displays_.find(display) != displays_.end();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400241}
242
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100243DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700244 for (auto &conn : connectors_) {
245 if (conn->display() == display)
246 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400247 }
248 return NULL;
249}
250
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100251DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700252 for (auto &crtc : crtcs_) {
253 if (crtc->display() == display)
254 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400255 }
256 return NULL;
257}
258
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100259DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700260 for (auto &plane : planes_) {
261 if (plane->id() == id)
262 return plane.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400263 }
264 return NULL;
265}
266
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100267const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400268 return crtcs_;
269}
270
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100271uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400272 return ++mode_id_;
273}
274
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100275int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400276 /* First try to use the currently-bound crtc */
277 DrmCrtc *crtc = enc->crtc();
278 if (crtc && crtc->can_bind(display)) {
279 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000280 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400281 return 0;
282 }
283
284 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700285 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400286 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700287 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400288 continue;
289
Zach Reiznerff30b522015-10-28 19:08:45 -0700290 if (crtc->can_bind(display)) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700291 crtc->set_display(display);
Alexandru Gheorgheae4324c2018-03-21 12:06:20 +0000292 enc->set_crtc(crtc);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400293 return 0;
294 }
295 }
296
297 /* We can't use the encoder, but nothing went wrong, try another one */
298 return -EAGAIN;
299}
300
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100301int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400302 int display = connector->display();
303 /* Try to use current setup first */
304 if (connector->encoder()) {
305 int ret = TryEncoderForDisplay(display, connector->encoder());
306 if (!ret) {
307 return 0;
308 } else if (ret != -EAGAIN) {
309 ALOGE("Could not set mode %d/%d", display, ret);
310 return ret;
311 }
312 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400313
Zach Reiznerff30b522015-10-28 19:08:45 -0700314 for (DrmEncoder *enc : connector->possible_encoders()) {
315 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400316 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700317 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400318 return 0;
319 } else if (ret != -EAGAIN) {
320 ALOGE("Could not set mode %d/%d", display, ret);
321 return ret;
322 }
323 }
324 ALOGE("Could not find a suitable encoder/crtc for display %d",
325 connector->display());
326 return -ENODEV;
327}
328
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100329int DrmDevice::CreatePropertyBlob(void *data, size_t length,
330 uint32_t *blob_id) {
Sean Paul877be972015-06-03 14:08:27 -0400331 struct drm_mode_create_blob create_blob;
332 memset(&create_blob, 0, sizeof(create_blob));
333 create_blob.length = length;
334 create_blob.data = (__u64)data;
335
Zach Reiznerff30b522015-10-28 19:08:45 -0700336 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400337 if (ret) {
338 ALOGE("Failed to create mode property blob %d", ret);
339 return ret;
340 }
341 *blob_id = create_blob.blob_id;
342 return 0;
343}
344
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100345int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) {
Sean Paul57355412015-09-19 09:14:34 -0400346 if (!blob_id)
347 return 0;
348
Sean Paul877be972015-06-03 14:08:27 -0400349 struct drm_mode_destroy_blob destroy_blob;
350 memset(&destroy_blob, 0, sizeof(destroy_blob));
351 destroy_blob.blob_id = (__u32)blob_id;
Zach Reiznerff30b522015-10-28 19:08:45 -0700352 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
Sean Paul877be972015-06-03 14:08:27 -0400353 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400354 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
Sean Paul877be972015-06-03 14:08:27 -0400355 return ret;
356 }
357 return 0;
358}
359
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100360DrmEventListener *DrmDevice::event_listener() {
Sean Paul047b9b22015-07-28 14:15:42 -0400361 return &event_listener_;
362}
363
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100364int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
365 const char *prop_name, DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400366 drmModeObjectPropertiesPtr props;
367
Zach Reiznerff30b522015-10-28 19:08:45 -0700368 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400369 if (!props) {
370 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
371 return -ENODEV;
372 }
373
374 bool found = false;
375 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700376 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400377 if (!strcmp(p->name, prop_name)) {
378 property->Init(p, props->prop_values[i]);
379 found = true;
380 }
381 drmModeFreeProperty(p);
382 }
383
384 drmModeFreeObjectProperties(props);
385 return found ? 0 : -ENOENT;
386}
387
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100388int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
389 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400390 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
391}
392
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100393int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
394 DrmProperty *property) {
Sean Paul877be972015-06-03 14:08:27 -0400395 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
396}
397
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100398int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
399 const char *prop_name,
400 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400401 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
402 property);
403}
404}