blob: 1eae32b4505bdc51b3e9d8267beb34e1ccfc64e8 [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 Gheorghe0f5abd72018-05-01 14:37:10 +010045int DrmDevice::Init() {
Sean Paul6a55e9f2015-04-30 15:31:06 -040046 char path[PROPERTY_VALUE_MAX];
47 property_get("hwc.drm.device", path, "/dev/dri/card0");
48
49 /* TODO: Use drmOpenControl here instead */
Zach Reiznerff30b522015-10-28 19:08:45 -070050 fd_.Set(open(path, O_RDWR));
51 if (fd() < 0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040052 ALOGE("Failed to open dri- %s", strerror(-errno));
53 return -ENODEV;
54 }
55
Zach Reiznerff30b522015-10-28 19:08:45 -070056 int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040057 if (ret) {
58 ALOGE("Failed to set universal plane cap %d", ret);
59 return ret;
60 }
61
Zach Reiznerff30b522015-10-28 19:08:45 -070062 ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
Sean Paul6a55e9f2015-04-30 15:31:06 -040063 if (ret) {
64 ALOGE("Failed to set atomic cap %d", ret);
65 return ret;
66 }
67
Zach Reiznerff30b522015-10-28 19:08:45 -070068 drmModeResPtr res = drmModeGetResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -040069 if (!res) {
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010070 ALOGE("Failed to get DrmDevice resources");
Sean Paul6a55e9f2015-04-30 15:31:06 -040071 return -ENODEV;
72 }
73
Sean Paul406dbfc2016-02-10 15:35:17 -080074 min_resolution_ =
75 std::pair<uint32_t, uint32_t>(res->min_width, res->min_height);
76 max_resolution_ =
77 std::pair<uint32_t, uint32_t>(res->max_width, res->max_height);
78
Sean Paul6a55e9f2015-04-30 15:31:06 -040079 bool found_primary = false;
80 int display_num = 1;
81
82 for (int i = 0; !ret && i < res->count_crtcs; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -070083 drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -040084 if (!c) {
85 ALOGE("Failed to get crtc %d", res->crtcs[i]);
86 ret = -ENODEV;
87 break;
88 }
89
Zach Reiznerff30b522015-10-28 19:08:45 -070090 std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
Sean Paul6a55e9f2015-04-30 15:31:06 -040091 drmModeFreeCrtc(c);
92
Sean Paul877be972015-06-03 14:08:27 -040093 ret = crtc->Init();
94 if (ret) {
95 ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
Sean Paul877be972015-06-03 14:08:27 -040096 break;
97 }
Zach Reiznerff30b522015-10-28 19:08:45 -070098 crtcs_.emplace_back(std::move(crtc));
Sean Paul6a55e9f2015-04-30 15:31:06 -040099 }
100
101 for (int i = 0; !ret && i < res->count_encoders; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700102 drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400103 if (!e) {
104 ALOGE("Failed to get encoder %d", res->encoders[i]);
105 ret = -ENODEV;
106 break;
107 }
108
109 std::vector<DrmCrtc *> possible_crtcs;
110 DrmCrtc *current_crtc = NULL;
Zach Reiznerff30b522015-10-28 19:08:45 -0700111 for (auto &crtc : crtcs_) {
112 if ((1 << crtc->pipe()) & e->possible_crtcs)
113 possible_crtcs.push_back(crtc.get());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400114
Zach Reiznerff30b522015-10-28 19:08:45 -0700115 if (crtc->id() == e->crtc_id)
116 current_crtc = crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400117 }
118
Zach Reiznerff30b522015-10-28 19:08:45 -0700119 std::unique_ptr<DrmEncoder> enc(
120 new DrmEncoder(e, current_crtc, possible_crtcs));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400121
122 drmModeFreeEncoder(e);
123
Zach Reiznerff30b522015-10-28 19:08:45 -0700124 encoders_.emplace_back(std::move(enc));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400125 }
126
127 for (int i = 0; !ret && i < res->count_connectors; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700128 drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400129 if (!c) {
130 ALOGE("Failed to get connector %d", res->connectors[i]);
131 ret = -ENODEV;
132 break;
133 }
134
135 std::vector<DrmEncoder *> possible_encoders;
136 DrmEncoder *current_encoder = NULL;
137 for (int j = 0; j < c->count_encoders; ++j) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700138 for (auto &encoder : encoders_) {
139 if (encoder->id() == c->encoders[j])
140 possible_encoders.push_back(encoder.get());
141 if (encoder->id() == c->encoder_id)
142 current_encoder = encoder.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400143 }
144 }
145
Zach Reiznerff30b522015-10-28 19:08:45 -0700146 std::unique_ptr<DrmConnector> conn(
147 new DrmConnector(this, c, current_encoder, possible_encoders));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400148
149 drmModeFreeConnector(c);
150
Sean Paul6a55e9f2015-04-30 15:31:06 -0400151 ret = conn->Init();
152 if (ret) {
153 ALOGE("Init connector %d failed", res->connectors[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400154 break;
155 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400156
Robert Foss610d9892017-11-01 12:50:04 -0500157 connectors_.emplace_back(std::move(conn));
158 }
159
160 // First look for primary amongst internal connectors
161 for (auto &conn : connectors_) {
162 if (conn->internal() && !found_primary) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400163 conn->set_display(0);
164 found_primary = true;
165 } else {
166 conn->set_display(display_num);
167 ++display_num;
168 }
169 }
Robert Foss610d9892017-11-01 12:50:04 -0500170
171 // Then look for primary amongst external connectors
172 for (auto &conn : connectors_) {
173 if (conn->external() && !found_primary) {
174 conn->set_display(0);
175 found_primary = true;
176 }
177 }
178
Sean Paul6a55e9f2015-04-30 15:31:06 -0400179 if (res)
180 drmModeFreeResources(res);
181
182 // Catch-all for the above loops
183 if (ret)
184 return ret;
185
Zach Reiznerff30b522015-10-28 19:08:45 -0700186 drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
Sean Paul6a55e9f2015-04-30 15:31:06 -0400187 if (!plane_res) {
188 ALOGE("Failed to get plane resources");
189 return -ENOENT;
190 }
191
192 for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700193 drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400194 if (!p) {
195 ALOGE("Failed to get plane %d", plane_res->planes[i]);
196 ret = -ENODEV;
197 break;
198 }
199
Zach Reiznerff30b522015-10-28 19:08:45 -0700200 std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400201
202 drmModeFreePlane(p);
203
Sean Paul6a55e9f2015-04-30 15:31:06 -0400204 ret = plane->Init();
205 if (ret) {
206 ALOGE("Init plane %d failed", plane_res->planes[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400207 break;
208 }
209
Zach Reiznerff30b522015-10-28 19:08:45 -0700210 planes_.emplace_back(std::move(plane));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400211 }
212 drmModeFreePlaneResources(plane_res);
213 if (ret)
214 return ret;
215
Sean Paul047b9b22015-07-28 14:15:42 -0400216 ret = event_listener_.Init();
217 if (ret) {
218 ALOGE("Can't initialize event listener %d", ret);
219 return ret;
220 }
221
Zach Reiznerff30b522015-10-28 19:08:45 -0700222 for (auto &conn : connectors_) {
223 ret = CreateDisplayPipe(conn.get());
Sean Paul57355412015-09-19 09:14:34 -0400224 if (ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700225 ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
Sean Paul57355412015-09-19 09:14:34 -0400226 return ret;
227 }
228 }
229 return 0;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400230}
231
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100232DrmConnector *DrmDevice::GetConnectorForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700233 for (auto &conn : connectors_) {
234 if (conn->display() == display)
235 return conn.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400236 }
237 return NULL;
238}
239
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100240DrmCrtc *DrmDevice::GetCrtcForDisplay(int display) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700241 for (auto &crtc : crtcs_) {
242 if (crtc->display() == display)
243 return crtc.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400244 }
245 return NULL;
246}
247
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100248DrmPlane *DrmDevice::GetPlane(uint32_t id) const {
Zach Reiznerff30b522015-10-28 19:08:45 -0700249 for (auto &plane : planes_) {
250 if (plane->id() == id)
251 return plane.get();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400252 }
253 return NULL;
254}
255
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100256const std::vector<std::unique_ptr<DrmCrtc>> &DrmDevice::crtcs() const {
Robert Foss0690c1c2016-10-20 11:07:57 -0400257 return crtcs_;
258}
259
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100260uint32_t DrmDevice::next_mode_id() {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400261 return ++mode_id_;
262}
263
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100264int DrmDevice::TryEncoderForDisplay(int display, DrmEncoder *enc) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400265 /* First try to use the currently-bound crtc */
266 DrmCrtc *crtc = enc->crtc();
267 if (crtc && crtc->can_bind(display)) {
268 crtc->set_display(display);
269 return 0;
270 }
271
272 /* Try to find a possible crtc which will work */
Zach Reiznerff30b522015-10-28 19:08:45 -0700273 for (DrmCrtc *crtc : enc->possible_crtcs()) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400274 /* We've already tried this earlier */
Zach Reiznerff30b522015-10-28 19:08:45 -0700275 if (crtc == enc->crtc())
Sean Paul6a55e9f2015-04-30 15:31:06 -0400276 continue;
277
Zach Reiznerff30b522015-10-28 19:08:45 -0700278 if (crtc->can_bind(display)) {
279 enc->set_crtc(crtc);
280 crtc->set_display(display);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400281 return 0;
282 }
283 }
284
285 /* We can't use the encoder, but nothing went wrong, try another one */
286 return -EAGAIN;
287}
288
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100289int DrmDevice::CreateDisplayPipe(DrmConnector *connector) {
Sean Paul877be972015-06-03 14:08:27 -0400290 int display = connector->display();
291 /* Try to use current setup first */
292 if (connector->encoder()) {
293 int ret = TryEncoderForDisplay(display, connector->encoder());
294 if (!ret) {
295 return 0;
296 } else if (ret != -EAGAIN) {
297 ALOGE("Could not set mode %d/%d", display, ret);
298 return ret;
299 }
300 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400301
Zach Reiznerff30b522015-10-28 19:08:45 -0700302 for (DrmEncoder *enc : connector->possible_encoders()) {
303 int ret = TryEncoderForDisplay(display, enc);
Sean Paul877be972015-06-03 14:08:27 -0400304 if (!ret) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700305 connector->set_encoder(enc);
Sean Paul877be972015-06-03 14:08:27 -0400306 return 0;
307 } else if (ret != -EAGAIN) {
308 ALOGE("Could not set mode %d/%d", display, ret);
309 return ret;
310 }
311 }
312 ALOGE("Could not find a suitable encoder/crtc for display %d",
313 connector->display());
314 return -ENODEV;
315}
316
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100317int DrmDevice::CreatePropertyBlob(void *data, size_t length,
318 uint32_t *blob_id) {
Sean Paul877be972015-06-03 14:08:27 -0400319 struct drm_mode_create_blob create_blob;
320 memset(&create_blob, 0, sizeof(create_blob));
321 create_blob.length = length;
322 create_blob.data = (__u64)data;
323
Zach Reiznerff30b522015-10-28 19:08:45 -0700324 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
Sean Paul877be972015-06-03 14:08:27 -0400325 if (ret) {
326 ALOGE("Failed to create mode property blob %d", ret);
327 return ret;
328 }
329 *blob_id = create_blob.blob_id;
330 return 0;
331}
332
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100333int DrmDevice::DestroyPropertyBlob(uint32_t blob_id) {
Sean Paul57355412015-09-19 09:14:34 -0400334 if (!blob_id)
335 return 0;
336
Sean Paul877be972015-06-03 14:08:27 -0400337 struct drm_mode_destroy_blob destroy_blob;
338 memset(&destroy_blob, 0, sizeof(destroy_blob));
339 destroy_blob.blob_id = (__u32)blob_id;
Zach Reiznerff30b522015-10-28 19:08:45 -0700340 int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
Sean Paul877be972015-06-03 14:08:27 -0400341 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400342 ALOGE("Failed to destroy mode property blob %" PRIu32 "/%d", blob_id, ret);
Sean Paul877be972015-06-03 14:08:27 -0400343 return ret;
344 }
345 return 0;
346}
347
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100348DrmEventListener *DrmDevice::event_listener() {
Sean Paul047b9b22015-07-28 14:15:42 -0400349 return &event_listener_;
350}
351
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100352int DrmDevice::GetProperty(uint32_t obj_id, uint32_t obj_type,
353 const char *prop_name, DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400354 drmModeObjectPropertiesPtr props;
355
Zach Reiznerff30b522015-10-28 19:08:45 -0700356 props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400357 if (!props) {
358 ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
359 return -ENODEV;
360 }
361
362 bool found = false;
363 for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
Zach Reiznerff30b522015-10-28 19:08:45 -0700364 drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400365 if (!strcmp(p->name, prop_name)) {
366 property->Init(p, props->prop_values[i]);
367 found = true;
368 }
369 drmModeFreeProperty(p);
370 }
371
372 drmModeFreeObjectProperties(props);
373 return found ? 0 : -ENOENT;
374}
375
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100376int DrmDevice::GetPlaneProperty(const DrmPlane &plane, const char *prop_name,
377 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400378 return GetProperty(plane.id(), DRM_MODE_OBJECT_PLANE, prop_name, property);
379}
380
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100381int DrmDevice::GetCrtcProperty(const DrmCrtc &crtc, const char *prop_name,
382 DrmProperty *property) {
Sean Paul877be972015-06-03 14:08:27 -0400383 return GetProperty(crtc.id(), DRM_MODE_OBJECT_CRTC, prop_name, property);
384}
385
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100386int DrmDevice::GetConnectorProperty(const DrmConnector &connector,
387 const char *prop_name,
388 DrmProperty *property) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400389 return GetProperty(connector.id(), DRM_MODE_OBJECT_CONNECTOR, prop_name,
390 property);
391}
392}