blob: 5375c3978baf79b1aa9c101bab08d288cdaf3508 [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) {
231 int32_t ret;
232
233 ret = drmSetMaster(drm->fd);
234 if (ret)
235 LOG(ERROR, "drmSetMaster in fini failed: %m");
236 drm_disable_crtc(drm, drm->crtc);
237 drmModeFreeCrtc(drm->crtc);
238 drm->crtc = NULL;
239 }
240
241 if (drm->main_monitor_connector) {
242 drmModeFreeConnector(drm->main_monitor_connector);
243 drm->main_monitor_connector = NULL;
244 }
245
246 if (drm->plane_resources) {
247 drmModeFreePlaneResources(drm->plane_resources);
248 drm->plane_resources = NULL;
249 }
250
251 if (drm->resources) {
252 drmModeFreeResources(drm->resources);
253 drm->resources = NULL;
254 }
255
256 drmClose(drm->fd);
257 drm->fd = -1;
258 }
259
260 free(drm);
261}
262
263static bool drm_equal(drm_t* l, drm_t* r)
264{
265 if (!l->crtc && r->crtc)
266 return false;
267 if (l->crtc && !r->crtc)
268 return false;
269 if (l->crtc && r->crtc)
270 if (l->crtc->crtc_id != r->crtc->crtc_id)
271 return false;
272
273 if (!l->main_monitor_connector && r->main_monitor_connector)
274 return false;
275 if (l->main_monitor_connector && !r->main_monitor_connector)
276 return false;
277 if (l->main_monitor_connector && r->main_monitor_connector)
278 if (l->main_monitor_connector->connector_id != r->main_monitor_connector->connector_id)
279 return false;
280 return true;
281}
282
283static int drm_score(drm_t* drm)
284{
285 drmVersionPtr version;
286 int score = 0;
287
288 if (!drm)
289 return -1000000000;
290
291 if (!drm->main_monitor_connector)
292 return -1000000000;
293
294 if (drm_is_internal(drm->main_monitor_connector->connector_type))
295 score++;
296
297 version = drmGetVersion(drm->fd);
298 if (version) {
299 /* we would rather use any driver besides UDL */
300 if (strcmp("udl", version->name) == 0)
301 score--;
302 if (strcmp("evdi", version->name) == 0)
303 score--;
304 /* VGEM should be ignored because it has no displays, but lets make sure */
305 if (strcmp("vgem", version->name) == 0)
306 score -= 1000000;
307 drmFreeVersion(version);
308 }
309 return score;
310}
311
312drm_t* drm_scan(void)
313{
314 unsigned i;
315 char* dev_name;
316 int ret;
317 drm_t *best_drm = NULL;
318
319 for (i = 0; i < DRM_MAX_MINOR; i++) {
320 drm_t* drm = calloc(1, sizeof(drm_t));
321
322 if (!drm)
323 return NULL;
324
325 ret = asprintf(&dev_name, DRM_DEV_NAME, DRM_DIR_NAME, i);
326 if (ret < 0) {
327 drm_fini(drm);
328 continue;
329 }
330
331 drm->fd = open(dev_name, O_RDWR, 0);
332 free(dev_name);
333 if (drm->fd < 0) {
334 drm_fini(drm);
335 continue;
336 }
337
338 drm->resources = drmModeGetResources(drm->fd);
339 if (!drm->resources) {
340 drm_fini(drm);
341 continue;
342 }
343
344 /* expect at least one crtc so we do not try to run on VGEM */
345 if (drm->resources->count_crtcs == 0 || drm->resources->count_connectors == 0) {
346 drm_fini(drm);
347 continue;
348 }
349
350 drm->main_monitor_connector = find_main_monitor(drm, &drm->selected_mode);
351 if (!drm->main_monitor_connector) {
352 drm_fini(drm);
353 continue;
354 }
355
356 drm->crtc = find_crtc_for_connector(drm, drm->main_monitor_connector);
357 if (!drm->crtc) {
358 drm_fini(drm);
359 continue;
360 }
361
362 drm->crtc->mode = drm->main_monitor_connector->modes[drm->selected_mode];
363
364 drm->plane_resources = drmModeGetPlaneResources(drm->fd);
365 drm->refcount = 1;
366
367 if (drm_score(drm) > drm_score(best_drm)) {
368 drm_fini(best_drm);
369 best_drm = drm;
370 }
371 }
372
373 if (best_drm) {
374 drmVersionPtr version;
375 version = drmGetVersion(best_drm->fd);
376 if (version) {
377 LOG(INFO,
378 "Frecon using drm driver %s, version %d.%d, date(%s), desc(%s)",
379 version->name,
380 version->version_major,
381 version->version_minor,
382 version->date,
383 version->desc);
384 drmFreeVersion(version);
385 }
386 drmDropMaster(best_drm->fd);
387 }
388
389 return best_drm;
390}
391
392void drm_set(drm_t* drm_)
393{
394 if (drm) {
395 drm_delref(drm);
396 drm = NULL;
397 }
398 drm = drm_;
399}
400
401void drm_close(void)
402{
403 if (drm) {
404 drm_delref(drm);
405 drm = NULL;
406 }
407}
408
409void drm_delref(drm_t *drm)
410{
411 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}