blob: 667499fc2d527ff25a92e8652d16415850af0d66 [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
121/* disable all planes except for primary on crtc we use */
122static 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) {
297 /* we would rather use any driver besides UDL */
298 if (strcmp("udl", version->name) == 0)
299 score--;
300 if (strcmp("evdi", version->name) == 0)
301 score--;
302 /* VGEM should be ignored because it has no displays, but lets make sure */
303 if (strcmp("vgem", version->name) == 0)
304 score -= 1000000;
305 drmFreeVersion(version);
306 }
307 return score;
308}
309
310drm_t* drm_scan(void)
311{
312 unsigned i;
313 char* dev_name;
314 int ret;
315 drm_t *best_drm = NULL;
316
317 for (i = 0; i < DRM_MAX_MINOR; i++) {
318 drm_t* drm = calloc(1, sizeof(drm_t));
319
320 if (!drm)
321 return NULL;
322
323 ret = asprintf(&dev_name, DRM_DEV_NAME, DRM_DIR_NAME, i);
324 if (ret < 0) {
325 drm_fini(drm);
326 continue;
327 }
328
329 drm->fd = open(dev_name, O_RDWR, 0);
330 free(dev_name);
331 if (drm->fd < 0) {
332 drm_fini(drm);
333 continue;
334 }
335
336 drm->resources = drmModeGetResources(drm->fd);
337 if (!drm->resources) {
338 drm_fini(drm);
339 continue;
340 }
341
342 /* expect at least one crtc so we do not try to run on VGEM */
343 if (drm->resources->count_crtcs == 0 || drm->resources->count_connectors == 0) {
344 drm_fini(drm);
345 continue;
346 }
347
348 drm->main_monitor_connector = find_main_monitor(drm, &drm->selected_mode);
349 if (!drm->main_monitor_connector) {
350 drm_fini(drm);
351 continue;
352 }
353
354 drm->crtc = find_crtc_for_connector(drm, drm->main_monitor_connector);
355 if (!drm->crtc) {
356 drm_fini(drm);
357 continue;
358 }
359
360 drm->crtc->mode = drm->main_monitor_connector->modes[drm->selected_mode];
361
362 drm->plane_resources = drmModeGetPlaneResources(drm->fd);
363 drm->refcount = 1;
364
365 if (drm_score(drm) > drm_score(best_drm)) {
366 drm_fini(best_drm);
367 best_drm = drm;
368 }
369 }
370
371 if (best_drm) {
372 drmVersionPtr version;
373 version = drmGetVersion(best_drm->fd);
374 if (version) {
375 LOG(INFO,
376 "Frecon using drm driver %s, version %d.%d, date(%s), desc(%s)",
377 version->name,
378 version->version_major,
379 version->version_minor,
380 version->date,
381 version->desc);
382 drmFreeVersion(version);
383 }
384 drmDropMaster(best_drm->fd);
385 }
386
387 return best_drm;
388}
389
390void drm_set(drm_t* drm_)
391{
392 if (drm) {
393 drm_delref(drm);
394 drm = NULL;
395 }
396 drm = drm_;
397}
398
399void drm_close(void)
400{
401 if (drm) {
402 drm_delref(drm);
403 drm = NULL;
404 }
405}
406
407void drm_delref(drm_t *drm)
408{
Dominik Behrd4d56272016-03-31 18:55:17 -0700409 if (!drm)
410 return;
Dominik Behr83010f82016-03-18 18:43:08 -0700411 if (drm->refcount) {
412 drm->refcount--;
413 } else {
414 LOG(ERROR, "Imbalanced drm_close()");
415 }
416 if (drm->refcount) {
417 return;
418 }
419
420 drm_fini(drm);
421}
422
423drm_t* drm_addref(void)
424{
425 if (drm) {
426 drm->refcount++;
427 return drm;
428 }
429
430 return NULL;
431}
432
433/*
434 * returns true if connector/crtc/driver have changed and framebuffer object have to be re-create
435 */
436bool drm_rescan(void)
437{
438 drm_t* ndrm;
439
440 ndrm = drm_scan();
441 if (ndrm) {
442 if (drm_equal(ndrm, drm)) {
443 drm_fini(ndrm);
444 } else {
445 drm_delref(drm);
446 drm = ndrm;
447 return true;
448 }
449 } else {
450 if (drm) {
451 drm_delref(drm); /* no usable monitor/drm object */
452 drm = NULL;
453 return true;
454 }
455 }
456 return false;
457}
458
459bool drm_valid(drm_t* drm) {
460 return drm && drm->fd >= 0 && drm->resources && drm->main_monitor_connector && drm->crtc;
461}
462
463int32_t drm_setmode(drm_t* drm, uint32_t fb_id)
464{
465 int32_t ret;
466
467 ret = drmSetMaster(drm->fd);
468 if (ret)
469 LOG(ERROR, "drmSetMaster failed: %m");
470
471 ret = drmModeSetCrtc(drm->fd, drm->crtc->crtc_id,
472 fb_id,
473 0, 0, // x,y
474 &drm->main_monitor_connector->connector_id,
475 1, // connector_count
476 &drm->crtc->mode); // mode
477
478 if (ret) {
479 LOG(ERROR, "Unable to set crtc: %m");
480 drmDropMaster(drm->fd);
481 return ret;
482 }
483
484 ret = drmModeSetCursor(drm->fd, drm->crtc->crtc_id,
485 0, 0, 0);
486
487 if (ret)
488 LOG(ERROR, "Unable to hide cursor");
489
490 drm_disable_non_primary_planes(drm);
491 drm_disable_non_main_crtcs(drm);
492 drmDropMaster(drm->fd);
493 return ret;
494}
495
496bool drm_read_edid(drm_t* drm)
497{
498 if (drm->edid_found) {
499 return true;
500 }
501
502 for (int i = 0; i < drm->main_monitor_connector->count_props; i++) {
503 drmModePropertyPtr prop;
504 drmModePropertyBlobPtr blob_ptr;
505 prop = drmModeGetProperty(drm->fd, drm->main_monitor_connector->props[i]);
506 if (prop) {
507 if (strcmp(prop->name, "EDID") == 0) {
508 blob_ptr = drmModeGetPropertyBlob(drm->fd,
509 drm->main_monitor_connector->prop_values[i]);
510 if (blob_ptr) {
511 memcpy(&drm->edid, blob_ptr->data, EDID_SIZE);
512 drmModeFreePropertyBlob(blob_ptr);
513 return (drm->edid_found = true);
514 }
515 }
516 }
517 }
518
519 return false;
520}
521
522uint32_t drm_gethres(drm_t* drm)
523{
524 return drm->crtc->mode.hdisplay;
525}
526
527uint32_t drm_getvres(drm_t* drm)
528{
529 return drm->crtc->mode.vdisplay;
530}