blob: 66eccfa91fea6f12fe1585a814ad91bce5d77d78 [file] [log] [blame]
Dominik Behr83010f82016-03-18 18:43:08 -07001/*
2 * Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#include <errno.h>
8#include <fcntl.h>
9#include <stdbool.h>
10#include <stddef.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/mman.h>
15#include <time.h>
16#include <unistd.h>
17
18#include "drm.h"
Dominik Behrd2cc4d22016-01-29 17:31:52 -080019#include "input.h"
Dominik Behr83010f82016-03-18 18:43:08 -070020#include "util.h"
21
22static drm_t* drm = NULL;
23
24static void drm_disable_crtc(drm_t* drm, drmModeCrtc* crtc)
25{
26 if (crtc) {
27 drmModeSetCrtc(drm->fd, crtc->crtc_id, 0, // buffer_id
28 0, 0, // x,y
29 NULL, // connectors
30 0, // connector_count
31 NULL); // mode
32 }
33}
34
35static drmModeCrtc* find_crtc_for_connector(drm_t* drm, drmModeConnector* connector)
36{
37 int i, j;
38 drmModeEncoder* encoder;
39 int32_t crtc_id;
40
41 if (connector->encoder_id)
42 encoder = drmModeGetEncoder(drm->fd, connector->encoder_id);
43 else
44 encoder = NULL;
45
46 if (encoder && encoder->crtc_id) {
47 crtc_id = encoder->crtc_id;
48 drmModeFreeEncoder(encoder);
49 return drmModeGetCrtc(drm->fd, crtc_id);
50 }
51
52 crtc_id = -1;
53 for (i = 0; i < connector->count_encoders; i++) {
54 encoder = drmModeGetEncoder(drm->fd, connector->encoders[i]);
55
56 if (encoder) {
57 for (j = 0; j < drm->resources->count_crtcs; j++) {
58 if (!(encoder->possible_crtcs & (1 << j)))
59 continue;
60 crtc_id = drm->resources->crtcs[j];
61 break;
62 }
63 if (crtc_id >= 0) {
64 drmModeFreeEncoder(encoder);
65 return drmModeGetCrtc(drm->fd, crtc_id);
66 }
67 }
68 }
69
70 return NULL;
71}
72
73static void drm_disable_non_main_crtcs(drm_t* drm)
74{
75 int i;
76 drmModeCrtc* crtc;
77
78 for (i = 0; i < drm->resources->count_connectors; i++) {
79 drmModeConnector* connector;
80
81 connector = drmModeGetConnector(drm->fd, drm->resources->connectors[i]);
82 crtc = find_crtc_for_connector(drm, connector);
83 if (crtc->crtc_id != drm->crtc->crtc_id)
84 drm_disable_crtc(drm, crtc);
85 drmModeFreeCrtc(crtc);
86 }
87}
88
89static int drm_is_primary_plane(drm_t* drm, uint32_t plane_id)
90{
91 uint32_t p;
92 bool found = false;
93 int ret = -1;
94
95 drmModeObjectPropertiesPtr props;
96 props = drmModeObjectGetProperties(drm->fd,
97 plane_id,
98 DRM_MODE_OBJECT_PLANE);
99 if (!props) {
100 LOG(ERROR, "Unable to get plane properties: %m");
101 return -1;
102 }
103
104 for (p = 0; p < props->count_props && !found; p++) {
105 drmModePropertyPtr prop;
106 prop = drmModeGetProperty(drm->fd, props->props[p]);
107 if (prop) {
108 if (strcmp("type", prop->name) == 0) {
109 found = true;
110 ret = (props->prop_values[p] == DRM_PLANE_TYPE_PRIMARY);
111 }
112 drmModeFreeProperty(prop);
113 }
114 }
115
116 drmModeFreeObjectProperties(props);
117
118 return ret;
119}
120
Dominik Behrb1abcba2016-04-14 14:57:21 -0700121/* Disable all planes except for primary on crtc we use. */
Dominik Behr83010f82016-03-18 18:43:08 -0700122static void drm_disable_non_primary_planes(drm_t* drm)
123{
124 int ret;
125
126 if (!drm->plane_resources)
127 return;
128
129 for (uint32_t p = 0; p < drm->plane_resources->count_planes; p++) {
130 drmModePlanePtr plane;
131 plane = drmModeGetPlane(drm->fd,
132 drm->plane_resources->planes[p]);
133 if (plane) {
134 int primary = drm_is_primary_plane(drm, plane->plane_id);
135 if (!(plane->crtc_id == drm->crtc->crtc_id && primary != 0)) {
136 ret = drmModeSetPlane(drm->fd, plane->plane_id, plane->crtc_id,
137 0, 0,
138 0, 0,
139 0, 0,
140 0, 0,
141 0, 0);
142 if (ret) {
143 LOG(WARNING, "Unable to disable plane: %m");
144 }
145 }
146 drmModeFreePlane(plane);
147 }
148 }
149}
150
151static bool drm_is_internal(unsigned type)
152{
153 unsigned t;
154 unsigned kInternalConnectors[] = {
155 DRM_MODE_CONNECTOR_LVDS,
156 DRM_MODE_CONNECTOR_eDP,
157 DRM_MODE_CONNECTOR_DSI,
158 };
159 for (t = 0; t < ARRAY_SIZE(kInternalConnectors); t++)
160 if (type == kInternalConnectors[t])
161 return true;
162 return false;
163}
164
165static drmModeConnector* find_first_connected_connector(drm_t* drm, bool internal, bool external)
166{
167 for (int i = 0; i < drm->resources->count_connectors; i++) {
168 drmModeConnector* connector;
169
170 connector = drmModeGetConnector(drm->fd, drm->resources->connectors[i]);
171 if (connector) {
172 bool is_internal = drm_is_internal(connector->connector_type);
173 if (!internal && is_internal)
174 continue;
175 if (!external && !is_internal)
176 continue;
177 if ((connector->count_modes > 0) &&
178 (connector->connection == DRM_MODE_CONNECTED))
179 return connector;
180
181 drmModeFreeConnector(connector);
182 }
183 }
184 return NULL;
185}
186
187static drmModeConnector* find_main_monitor(drm_t* drm, uint32_t* mode_index)
188{
189 int modes;
Dominik Behrd2cc4d22016-01-29 17:31:52 -0800190 int lid_state = input_check_lid_state();
Dominik Behr83010f82016-03-18 18:43:08 -0700191 drmModeConnector* main_monitor_connector = NULL;
192
193 /*
194 * Find the LVDS/eDP/DSI connectors. Those are the main screens.
195 */
Dominik Behrd2cc4d22016-01-29 17:31:52 -0800196 if (lid_state <= 0)
197 main_monitor_connector = find_first_connected_connector(drm, true, false);
Dominik Behr83010f82016-03-18 18:43:08 -0700198
199 /*
200 * Now try external connectors.
201 */
202 if (!main_monitor_connector)
203 main_monitor_connector =
204 find_first_connected_connector(drm, false, true);
205
206 /*
207 * If we still didn't find a connector, give up and return.
208 */
209 if (!main_monitor_connector)
210 return NULL;
211
212 *mode_index = 0;
213 for (modes = 0; modes < main_monitor_connector->count_modes; modes++) {
214 if (main_monitor_connector->modes[modes].type &
215 DRM_MODE_TYPE_PREFERRED) {
216 *mode_index = modes;
217 break;
218 }
219 }
220
221 return main_monitor_connector;
222}
223
224static void drm_fini(drm_t* drm)
225{
226 if (!drm)
227 return;
228
229 if (drm->fd >= 0) {
230 if (drm->crtc) {
Dominik Behr83010f82016-03-18 18:43:08 -0700231 drmModeFreeCrtc(drm->crtc);
232 drm->crtc = NULL;
233 }
234
235 if (drm->main_monitor_connector) {
236 drmModeFreeConnector(drm->main_monitor_connector);
237 drm->main_monitor_connector = NULL;
238 }
239
240 if (drm->plane_resources) {
241 drmModeFreePlaneResources(drm->plane_resources);
242 drm->plane_resources = NULL;
243 }
244
245 if (drm->resources) {
246 drmModeFreeResources(drm->resources);
247 drm->resources = NULL;
248 }
249
250 drmClose(drm->fd);
251 drm->fd = -1;
252 }
253
254 free(drm);
255}
256
257static bool drm_equal(drm_t* l, drm_t* r)
258{
Dominik Behrd4d56272016-03-31 18:55:17 -0700259 if (!l && !r)
260 return true;
261 if ((!l && r) || (l && !r))
262 return false;
Dominik Behr83010f82016-03-18 18:43:08 -0700263 if (!l->crtc && r->crtc)
264 return false;
265 if (l->crtc && !r->crtc)
266 return false;
267 if (l->crtc && r->crtc)
268 if (l->crtc->crtc_id != r->crtc->crtc_id)
269 return false;
270
271 if (!l->main_monitor_connector && r->main_monitor_connector)
272 return false;
273 if (l->main_monitor_connector && !r->main_monitor_connector)
274 return false;
275 if (l->main_monitor_connector && r->main_monitor_connector)
276 if (l->main_monitor_connector->connector_id != r->main_monitor_connector->connector_id)
277 return false;
278 return true;
279}
280
281static int drm_score(drm_t* drm)
282{
283 drmVersionPtr version;
284 int score = 0;
285
286 if (!drm)
287 return -1000000000;
288
289 if (!drm->main_monitor_connector)
290 return -1000000000;
291
292 if (drm_is_internal(drm->main_monitor_connector->connector_type))
293 score++;
294
295 version = drmGetVersion(drm->fd);
296 if (version) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700297 /* We would rather use any driver besides UDL. */
Dominik Behr83010f82016-03-18 18:43:08 -0700298 if (strcmp("udl", version->name) == 0)
299 score--;
300 if (strcmp("evdi", version->name) == 0)
301 score--;
Dominik Behrb1abcba2016-04-14 14:57:21 -0700302 /* VGEM should be ignored because it has no displays, but lets make sure. */
Dominik Behr83010f82016-03-18 18:43:08 -0700303 if (strcmp("vgem", version->name) == 0)
304 score -= 1000000;
305 drmFreeVersion(version);
306 }
307 return score;
308}
309
Dominik Behrb1abcba2016-04-14 14:57:21 -0700310/*
311 * Scan and find best DRM object to display frecon on.
312 * This object should be created with DRM master, and we will keep master till
313 * first mode set or explicit drop master.
314 */
Dominik Behr83010f82016-03-18 18:43:08 -0700315drm_t* drm_scan(void)
316{
317 unsigned i;
318 char* dev_name;
319 int ret;
320 drm_t *best_drm = NULL;
321
322 for (i = 0; i < DRM_MAX_MINOR; i++) {
323 drm_t* drm = calloc(1, sizeof(drm_t));
324
325 if (!drm)
326 return NULL;
327
328 ret = asprintf(&dev_name, DRM_DEV_NAME, DRM_DIR_NAME, i);
329 if (ret < 0) {
330 drm_fini(drm);
331 continue;
332 }
333
334 drm->fd = open(dev_name, O_RDWR, 0);
335 free(dev_name);
336 if (drm->fd < 0) {
337 drm_fini(drm);
338 continue;
339 }
340
341 drm->resources = drmModeGetResources(drm->fd);
342 if (!drm->resources) {
343 drm_fini(drm);
344 continue;
345 }
346
Dominik Behrb1abcba2016-04-14 14:57:21 -0700347 /* Expect at least one crtc so we do not try to run on VGEM. */
Dominik Behr83010f82016-03-18 18:43:08 -0700348 if (drm->resources->count_crtcs == 0 || drm->resources->count_connectors == 0) {
349 drm_fini(drm);
350 continue;
351 }
352
353 drm->main_monitor_connector = find_main_monitor(drm, &drm->selected_mode);
354 if (!drm->main_monitor_connector) {
355 drm_fini(drm);
356 continue;
357 }
358
359 drm->crtc = find_crtc_for_connector(drm, drm->main_monitor_connector);
360 if (!drm->crtc) {
361 drm_fini(drm);
362 continue;
363 }
364
365 drm->crtc->mode = drm->main_monitor_connector->modes[drm->selected_mode];
366
367 drm->plane_resources = drmModeGetPlaneResources(drm->fd);
368 drm->refcount = 1;
369
370 if (drm_score(drm) > drm_score(best_drm)) {
371 drm_fini(best_drm);
372 best_drm = drm;
373 }
374 }
375
376 if (best_drm) {
377 drmVersionPtr version;
378 version = drmGetVersion(best_drm->fd);
379 if (version) {
380 LOG(INFO,
381 "Frecon using drm driver %s, version %d.%d, date(%s), desc(%s)",
382 version->name,
383 version->version_major,
384 version->version_minor,
385 version->date,
386 version->desc);
387 drmFreeVersion(version);
388 }
Dominik Behr83010f82016-03-18 18:43:08 -0700389 }
390
391 return best_drm;
392}
393
394void drm_set(drm_t* drm_)
395{
396 if (drm) {
397 drm_delref(drm);
398 drm = NULL;
399 }
400 drm = drm_;
401}
402
403void drm_close(void)
404{
405 if (drm) {
406 drm_delref(drm);
407 drm = NULL;
408 }
409}
410
Dominik Behrb1abcba2016-04-14 14:57:21 -0700411void drm_delref(drm_t* drm)
Dominik Behr83010f82016-03-18 18:43:08 -0700412{
Dominik Behrd4d56272016-03-31 18:55:17 -0700413 if (!drm)
414 return;
Dominik Behr83010f82016-03-18 18:43:08 -0700415 if (drm->refcount) {
416 drm->refcount--;
417 } else {
418 LOG(ERROR, "Imbalanced drm_close()");
419 }
420 if (drm->refcount) {
421 return;
422 }
423
424 drm_fini(drm);
425}
426
427drm_t* drm_addref(void)
428{
429 if (drm) {
430 drm->refcount++;
431 return drm;
432 }
433
434 return NULL;
435}
436
Dominik Behrb1abcba2016-04-14 14:57:21 -0700437void drm_dropmaster(drm_t* drm)
438{
439 if (drm) {
440 drmDropMaster(drm->fd);
441 }
442}
443
Dominik Behr83010f82016-03-18 18:43:08 -0700444/*
Dominik Behrb1abcba2016-04-14 14:57:21 -0700445 * Returns true if connector/crtc/driver have changed and framebuffer object have to be re-created.
Dominik Behr83010f82016-03-18 18:43:08 -0700446 */
447bool drm_rescan(void)
448{
449 drm_t* ndrm;
450
Dominik Behrb1abcba2016-04-14 14:57:21 -0700451 /* In case we had master, drop master so the newly created object could have it. */
452 drm_dropmaster(drm);
Dominik Behr83010f82016-03-18 18:43:08 -0700453 ndrm = drm_scan();
454 if (ndrm) {
455 if (drm_equal(ndrm, drm)) {
456 drm_fini(ndrm);
457 } else {
458 drm_delref(drm);
459 drm = ndrm;
460 return true;
461 }
462 } else {
463 if (drm) {
Dominik Behrb1abcba2016-04-14 14:57:21 -0700464 drm_delref(drm); /* No usable monitor/drm object. */
Dominik Behr83010f82016-03-18 18:43:08 -0700465 drm = NULL;
466 return true;
467 }
468 }
469 return false;
470}
471
472bool drm_valid(drm_t* drm) {
473 return drm && drm->fd >= 0 && drm->resources && drm->main_monitor_connector && drm->crtc;
474}
475
476int32_t drm_setmode(drm_t* drm, uint32_t fb_id)
477{
478 int32_t ret;
479
480 ret = drmSetMaster(drm->fd);
481 if (ret)
482 LOG(ERROR, "drmSetMaster failed: %m");
483
484 ret = drmModeSetCrtc(drm->fd, drm->crtc->crtc_id,
485 fb_id,
486 0, 0, // x,y
487 &drm->main_monitor_connector->connector_id,
488 1, // connector_count
489 &drm->crtc->mode); // mode
490
491 if (ret) {
492 LOG(ERROR, "Unable to set crtc: %m");
493 drmDropMaster(drm->fd);
494 return ret;
495 }
496
497 ret = drmModeSetCursor(drm->fd, drm->crtc->crtc_id,
498 0, 0, 0);
499
500 if (ret)
501 LOG(ERROR, "Unable to hide cursor");
502
503 drm_disable_non_primary_planes(drm);
504 drm_disable_non_main_crtcs(drm);
505 drmDropMaster(drm->fd);
506 return ret;
507}
508
509bool drm_read_edid(drm_t* drm)
510{
511 if (drm->edid_found) {
512 return true;
513 }
514
515 for (int i = 0; i < drm->main_monitor_connector->count_props; i++) {
516 drmModePropertyPtr prop;
517 drmModePropertyBlobPtr blob_ptr;
518 prop = drmModeGetProperty(drm->fd, drm->main_monitor_connector->props[i]);
519 if (prop) {
520 if (strcmp(prop->name, "EDID") == 0) {
521 blob_ptr = drmModeGetPropertyBlob(drm->fd,
522 drm->main_monitor_connector->prop_values[i]);
523 if (blob_ptr) {
524 memcpy(&drm->edid, blob_ptr->data, EDID_SIZE);
525 drmModeFreePropertyBlob(blob_ptr);
526 return (drm->edid_found = true);
527 }
528 }
529 }
530 }
531
532 return false;
533}
534
535uint32_t drm_gethres(drm_t* drm)
536{
537 return drm->crtc->mode.hdisplay;
538}
539
540uint32_t drm_getvres(drm_t* drm)
541{
542 return drm->crtc->mode.vdisplay;
543}