chiark / gitweb /
terminal: grdev: schedule virtual frame events if hw doesn't support it
[elogind.git] / src / libsystemd-terminal / grdev-drm.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <libudev.h>
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #include <sys/ioctl.h>
28 #include <sys/mman.h>
29 #include <sys/types.h>
30 #include <systemd/sd-bus.h>
31 #include <systemd/sd-event.h>
32 #include <unistd.h>
33
34 /* Yuck! DRM headers need system headers included first.. but we have to
35  * include it before shared/missing.h to avoid redefining ioctl bits */
36 #include <drm.h>
37 #include <drm_fourcc.h>
38 #include <drm_mode.h>
39
40 #include "bus-util.h"
41 #include "hashmap.h"
42 #include "grdev.h"
43 #include "grdev-internal.h"
44 #include "macro.h"
45 #include "udev-util.h"
46 #include "util.h"
47
48 #define GRDRM_MAX_TRIES (16)
49
50 typedef struct grdrm_object grdrm_object;
51 typedef struct grdrm_plane grdrm_plane;
52 typedef struct grdrm_connector grdrm_connector;
53 typedef struct grdrm_encoder grdrm_encoder;
54 typedef struct grdrm_crtc grdrm_crtc;
55
56 typedef struct grdrm_fb grdrm_fb;
57 typedef struct grdrm_pipe grdrm_pipe;
58 typedef struct grdrm_card grdrm_card;
59 typedef struct unmanaged_card unmanaged_card;
60 typedef struct managed_card managed_card;
61
62 /*
63  * Objects
64  */
65
66 enum {
67         GRDRM_TYPE_CRTC,
68         GRDRM_TYPE_ENCODER,
69         GRDRM_TYPE_CONNECTOR,
70         GRDRM_TYPE_PLANE,
71         GRDRM_TYPE_CNT
72 };
73
74 struct grdrm_object {
75         grdrm_card *card;
76         uint32_t id;
77         uint32_t index;
78         unsigned int type;
79         void (*free_fn) (grdrm_object *object);
80
81         bool present : 1;
82         bool assigned : 1;
83 };
84
85 struct grdrm_plane {
86         grdrm_object object;
87
88         struct {
89                 uint32_t used_crtc;
90                 uint32_t used_fb;
91                 uint32_t gamma_size;
92
93                 uint32_t n_crtcs;
94                 uint32_t max_crtcs;
95                 uint32_t *crtcs;
96                 uint32_t n_formats;
97                 uint32_t max_formats;
98                 uint32_t *formats;
99         } kern;
100 };
101
102 struct grdrm_connector {
103         grdrm_object object;
104
105         struct {
106                 uint32_t type;
107                 uint32_t type_id;
108                 uint32_t used_encoder;
109                 uint32_t connection;
110                 uint32_t mm_width;
111                 uint32_t mm_height;
112                 uint32_t subpixel;
113
114                 uint32_t n_encoders;
115                 uint32_t max_encoders;
116                 uint32_t *encoders;
117                 uint32_t n_modes;
118                 uint32_t max_modes;
119                 struct drm_mode_modeinfo *modes;
120                 uint32_t n_props;
121                 uint32_t max_props;
122                 uint32_t *prop_ids;
123                 uint64_t *prop_values;
124         } kern;
125 };
126
127 struct grdrm_encoder {
128         grdrm_object object;
129
130         struct {
131                 uint32_t type;
132                 uint32_t used_crtc;
133
134                 uint32_t n_crtcs;
135                 uint32_t max_crtcs;
136                 uint32_t *crtcs;
137                 uint32_t n_clones;
138                 uint32_t max_clones;
139                 uint32_t *clones;
140         } kern;
141 };
142
143 struct grdrm_crtc {
144         grdrm_object object;
145
146         struct {
147                 uint32_t used_fb;
148                 uint32_t fb_offset_x;
149                 uint32_t fb_offset_y;
150                 uint32_t gamma_size;
151
152                 uint32_t n_used_connectors;
153                 uint32_t max_used_connectors;
154                 uint32_t *used_connectors;
155
156                 bool mode_set;
157                 struct drm_mode_modeinfo mode;
158         } kern;
159
160         struct {
161                 bool set;
162                 uint32_t fb;
163                 uint32_t fb_x;
164                 uint32_t fb_y;
165                 uint32_t gamma;
166
167                 uint32_t n_connectors;
168                 uint32_t *connectors;
169
170                 bool mode_set;
171                 struct drm_mode_modeinfo mode;
172         } old;
173
174         struct {
175                 struct drm_mode_modeinfo mode;
176                 uint32_t n_connectors;
177                 uint32_t max_connectors;
178                 uint32_t *connectors;
179         } set;
180
181         grdrm_pipe *pipe;
182
183         bool applied : 1;
184 };
185
186 #define GRDRM_OBJECT_INIT(_card, _id, _index, _type, _free_fn) ((grdrm_object){ \
187                 .card = (_card), \
188                 .id = (_id), \
189                 .index = (_index), \
190                 .type = (_type), \
191                 .free_fn = (_free_fn), \
192         })
193
194 grdrm_object *grdrm_find_object(grdrm_card *card, uint32_t id);
195 int grdrm_object_add(grdrm_object *object);
196 grdrm_object *grdrm_object_free(grdrm_object *object);
197
198 DEFINE_TRIVIAL_CLEANUP_FUNC(grdrm_object*, grdrm_object_free);
199
200 int grdrm_plane_new(grdrm_plane **out, grdrm_card *card, uint32_t id, uint32_t index);
201 int grdrm_connector_new(grdrm_connector **out, grdrm_card *card, uint32_t id, uint32_t index);
202 int grdrm_encoder_new(grdrm_encoder **out, grdrm_card *card, uint32_t id, uint32_t index);
203 int grdrm_crtc_new(grdrm_crtc **out, grdrm_card *card, uint32_t id, uint32_t index);
204
205 #define plane_from_object(_obj) container_of((_obj), grdrm_plane, object)
206 #define connector_from_object(_obj) container_of((_obj), grdrm_connector, object)
207 #define encoder_from_object(_obj) container_of((_obj), grdrm_encoder, object)
208 #define crtc_from_object(_obj) container_of((_obj), grdrm_crtc, object)
209
210 /*
211  * Framebuffers
212  */
213
214 struct grdrm_fb {
215         grdev_fb base;
216         grdrm_card *card;
217         uint32_t id;
218         uint32_t handles[4];
219         uint32_t offsets[4];
220         uint32_t sizes[4];
221         uint32_t flipid;
222 };
223
224 static int grdrm_fb_new(grdrm_fb **out, grdrm_card *card, const struct drm_mode_modeinfo *mode);
225 grdrm_fb *grdrm_fb_free(grdrm_fb *fb);
226
227 DEFINE_TRIVIAL_CLEANUP_FUNC(grdrm_fb*, grdrm_fb_free);
228
229 #define fb_from_base(_fb) container_of((_fb), grdrm_fb, base)
230
231 /*
232  * Pipes
233  */
234
235 struct grdrm_pipe {
236         grdev_pipe base;
237         grdrm_crtc *crtc;
238         uint32_t counter;
239 };
240
241 #define grdrm_pipe_from_base(_e) container_of((_e), grdrm_pipe, base)
242
243 #define GRDRM_PIPE_NAME_MAX (GRDRM_CARD_NAME_MAX + 1 + DECIMAL_STR_MAX(uint32_t))
244
245 static const grdev_pipe_vtable grdrm_pipe_vtable;
246
247 static int grdrm_pipe_new(grdrm_pipe **out, grdrm_crtc *crtc, struct drm_mode_modeinfo *mode, size_t n_fbs);
248
249 /*
250  * Cards
251  */
252
253 struct grdrm_card {
254         grdev_card base;
255
256         int fd;
257         sd_event_source *fd_src;
258
259         uint32_t n_crtcs;
260         uint32_t n_encoders;
261         uint32_t n_connectors;
262         uint32_t n_planes;
263         uint32_t max_ids;
264         Hashmap *object_map;
265
266         bool async_hotplug : 1;
267         bool running : 1;
268         bool ready : 1;
269         bool cap_dumb : 1;
270         bool cap_monotonic : 1;
271 };
272
273 struct unmanaged_card {
274         grdrm_card card;
275         char *devnode;
276 };
277
278 struct managed_card {
279         grdrm_card card;
280         dev_t devnum;
281
282         sd_bus_slot *slot_pause_device;
283         sd_bus_slot *slot_resume_device;
284         sd_bus_slot *slot_take_device;
285
286         bool requested : 1;             /* TakeDevice() was sent */
287         bool acquired : 1;              /* TakeDevice() was successful */
288         bool master : 1;                /* we are DRM-Master */
289 };
290
291 #define grdrm_card_from_base(_e) container_of((_e), grdrm_card, base)
292 #define unmanaged_card_from_base(_e) \
293         container_of(grdrm_card_from_base(_e), unmanaged_card, card)
294 #define managed_card_from_base(_e) \
295         container_of(grdrm_card_from_base(_e), managed_card, card)
296
297 #define GRDRM_CARD_INIT(_vtable, _session) ((grdrm_card){ \
298                 .base = GRDEV_CARD_INIT((_vtable), (_session)), \
299                 .fd = -1, \
300                 .max_ids = 32, \
301         })
302
303 #define GRDRM_CARD_NAME_MAX (6 + DECIMAL_STR_MAX(unsigned) * 2)
304
305 static const grdev_card_vtable unmanaged_card_vtable;
306 static const grdev_card_vtable managed_card_vtable;
307
308 static int grdrm_card_open(grdrm_card *card, int dev_fd);
309 static void grdrm_card_close(grdrm_card *card);
310 static bool grdrm_card_async(grdrm_card *card, int r);
311
312 /*
313  * The page-flip event of the kernel provides 64bit of arbitrary user-data. As
314  * drivers tend to drop events on intermediate deep mode-sets or because we
315  * might receive events during session activation, we try to avoid allocaing
316  * dynamic data on those events. Instead, we safe the CRTC id plus a 32bit
317  * counter in there. This way, we only get 32bit counters, not 64bit, but that
318  * should be more than enough. On the bright side, we no longer care whether we
319  * lose events. No memory leaks will occur.
320  * Modern DRM drivers might be fixed to no longer leak events, but we want to
321  * be safe. And associating dynamically allocated data with those events is
322  * kinda ugly, anyway.
323  */
324
325 static uint64_t grdrm_encode_vblank_data(uint32_t id, uint32_t counter) {
326         return id | ((uint64_t)counter << 32);
327 }
328
329 static void grdrm_decode_vblank_data(uint64_t data, uint32_t *out_id, uint32_t *out_counter) {
330         if (out_id)
331                 *out_id = data & 0xffffffffU;
332         if (out_counter)
333                 *out_counter = (data >> 32) & 0xffffffffU;
334 }
335
336 static bool grdrm_modes_compatible(const struct drm_mode_modeinfo *a, const struct drm_mode_modeinfo *b) {
337         assert(a);
338         assert(b);
339
340         /* Test whether both modes are compatible according to our internal
341          * assumptions on modes. This comparison is highly dependent on how
342          * we treat modes in grdrm. If we export mode details, we need to
343          * make this comparison much stricter. */
344
345         if (a->hdisplay != b->hdisplay)
346                 return false;
347         if (a->vdisplay != b->vdisplay)
348                 return false;
349         if (a->vrefresh != b->vrefresh)
350                 return false;
351
352         return true;
353 }
354
355 /*
356  * Objects
357  */
358
359 grdrm_object *grdrm_find_object(grdrm_card *card, uint32_t id) {
360         assert_return(card, NULL);
361
362         return id > 0 ? hashmap_get(card->object_map, UINT32_TO_PTR(id)) : NULL;
363 }
364
365 int grdrm_object_add(grdrm_object *object) {
366         int r;
367
368         assert(object);
369         assert(object->card);
370         assert(object->id > 0);
371         assert(IN_SET(object->type, GRDRM_TYPE_CRTC, GRDRM_TYPE_ENCODER, GRDRM_TYPE_CONNECTOR, GRDRM_TYPE_PLANE));
372         assert(object->free_fn);
373
374         if (object->index >= 32)
375                 log_debug("grdrm: %s: object index exceeds 32bit masks: type=%u, index=%" PRIu32,
376                           object->card->base.name, object->type, object->index);
377
378         r = hashmap_put(object->card->object_map, UINT32_TO_PTR(object->id), object);
379         if (r < 0)
380                 return r;
381
382         return 0;
383 }
384
385 grdrm_object *grdrm_object_free(grdrm_object *object) {
386         if (!object)
387                 return NULL;
388
389         assert(object->card);
390         assert(object->id > 0);
391         assert(IN_SET(object->type, GRDRM_TYPE_CRTC, GRDRM_TYPE_ENCODER, GRDRM_TYPE_CONNECTOR, GRDRM_TYPE_PLANE));
392         assert(object->free_fn);
393
394         hashmap_remove_value(object->card->object_map, UINT32_TO_PTR(object->id), object);
395
396         object->free_fn(object);
397         return NULL;
398 }
399
400 /*
401  * Planes
402  */
403
404 static void plane_free(grdrm_object *object) {
405         grdrm_plane *plane = plane_from_object(object);
406
407         free(plane->kern.formats);
408         free(plane->kern.crtcs);
409         free(plane);
410 }
411
412 int grdrm_plane_new(grdrm_plane **out, grdrm_card *card, uint32_t id, uint32_t index) {
413         _cleanup_(grdrm_object_freep) grdrm_object *object = NULL;
414         grdrm_plane *plane;
415         int r;
416
417         assert(card);
418
419         plane = new0(grdrm_plane, 1);
420         if (!plane)
421                 return -ENOMEM;
422
423         object = &plane->object;
424         *object = GRDRM_OBJECT_INIT(card, id, index, GRDRM_TYPE_PLANE, plane_free);
425
426         plane->kern.max_crtcs = 32;
427         plane->kern.crtcs = new0(uint32_t, plane->kern.max_crtcs);
428         if (!plane->kern.crtcs)
429                 return -ENOMEM;
430
431         plane->kern.max_formats = 32;
432         plane->kern.formats = new0(uint32_t, plane->kern.max_formats);
433         if (!plane->kern.formats)
434                 return -ENOMEM;
435
436         r = grdrm_object_add(object);
437         if (r < 0)
438                 return r;
439
440         if (out)
441                 *out = plane;
442         object = NULL;
443         return 0;
444 }
445
446 static int grdrm_plane_resync(grdrm_plane *plane) {
447         grdrm_card *card = plane->object.card;
448         size_t tries;
449         int r;
450
451         assert(plane);
452
453         for (tries = 0; tries < GRDRM_MAX_TRIES; ++tries) {
454                 struct drm_mode_get_plane res;
455                 grdrm_object *object;
456                 bool resized = false;
457                 Iterator iter;
458
459                 zero(res);
460                 res.plane_id = plane->object.id;
461                 res.format_type_ptr = PTR_TO_UINT64(plane->kern.formats);
462                 res.count_format_types = plane->kern.max_formats;
463
464                 r = ioctl(card->fd, DRM_IOCTL_MODE_GETPLANE, &res);
465                 if (r < 0) {
466                         r = -errno;
467                         if (r == -ENOENT) {
468                                 card->async_hotplug = true;
469                                 r = 0;
470                                 log_debug("grdrm: %s: plane %u removed during resync", card->base.name, plane->object.id);
471                         } else {
472                                 log_debug("grdrm: %s: cannot retrieve plane %u: %m", card->base.name, plane->object.id);
473                         }
474
475                         return r;
476                 }
477
478                 plane->kern.n_crtcs = 0;
479                 memzero(plane->kern.crtcs, sizeof(uint32_t) * plane->kern.max_crtcs);
480
481                 HASHMAP_FOREACH(object, card->object_map, iter) {
482                         if (object->type != GRDRM_TYPE_CRTC || object->index >= 32)
483                                 continue;
484                         if (!(res.possible_crtcs & (1 << object->index)))
485                                 continue;
486                         if (plane->kern.n_crtcs >= 32) {
487                                 log_debug("grdrm: %s: possible_crtcs of plane %" PRIu32 " exceeds 32bit mask",
488                                           card->base.name, plane->object.id);
489                                 continue;
490                         }
491
492                         plane->kern.crtcs[plane->kern.n_crtcs++] = object->id;
493                 }
494
495                 if (res.count_format_types > plane->kern.max_formats) {
496                         uint32_t max, *t;
497
498                         max = ALIGN_POWER2(res.count_format_types);
499                         if (!max || max > UINT16_MAX) {
500                                 log_debug("grdrm: %s: excessive plane resource limit: %" PRIu32, card->base.name, max);
501                                 return -ERANGE;
502                         }
503
504                         t = realloc(plane->kern.formats, sizeof(*t) * max);
505                         if (!t)
506                                 return -ENOMEM;
507
508                         plane->kern.formats = t;
509                         plane->kern.max_formats = max;
510                         resized = true;
511                 }
512
513                 if (resized)
514                         continue;
515
516                 plane->kern.n_formats = res.count_format_types;
517                 plane->kern.used_crtc = res.crtc_id;
518                 plane->kern.used_fb = res.fb_id;
519                 plane->kern.gamma_size = res.gamma_size;
520
521                 break;
522         }
523
524         if (tries >= GRDRM_MAX_TRIES) {
525                 log_debug("grdrm: %s: plane %u not settled for retrieval", card->base.name, plane->object.id);
526                 return -EFAULT;
527         }
528
529         return 0;
530 }
531
532 /*
533  * Connectors
534  */
535
536 static void connector_free(grdrm_object *object) {
537         grdrm_connector *connector = connector_from_object(object);
538
539         free(connector->kern.prop_values);
540         free(connector->kern.prop_ids);
541         free(connector->kern.modes);
542         free(connector->kern.encoders);
543         free(connector);
544 }
545
546 int grdrm_connector_new(grdrm_connector **out, grdrm_card *card, uint32_t id, uint32_t index) {
547         _cleanup_(grdrm_object_freep) grdrm_object *object = NULL;
548         grdrm_connector *connector;
549         int r;
550
551         assert(card);
552
553         connector = new0(grdrm_connector, 1);
554         if (!connector)
555                 return -ENOMEM;
556
557         object = &connector->object;
558         *object = GRDRM_OBJECT_INIT(card, id, index, GRDRM_TYPE_CONNECTOR, connector_free);
559
560         connector->kern.max_encoders = 32;
561         connector->kern.encoders = new0(uint32_t, connector->kern.max_encoders);
562         if (!connector->kern.encoders)
563                 return -ENOMEM;
564
565         connector->kern.max_modes = 32;
566         connector->kern.modes = new0(struct drm_mode_modeinfo, connector->kern.max_modes);
567         if (!connector->kern.modes)
568                 return -ENOMEM;
569
570         connector->kern.max_props = 32;
571         connector->kern.prop_ids = new0(uint32_t, connector->kern.max_props);
572         connector->kern.prop_values = new0(uint64_t, connector->kern.max_props);
573         if (!connector->kern.prop_ids || !connector->kern.prop_values)
574                 return -ENOMEM;
575
576         r = grdrm_object_add(object);
577         if (r < 0)
578                 return r;
579
580         if (out)
581                 *out = connector;
582         object = NULL;
583         return 0;
584 }
585
586 static int grdrm_connector_resync(grdrm_connector *connector) {
587         grdrm_card *card = connector->object.card;
588         size_t tries;
589         int r;
590
591         assert(connector);
592
593         for (tries = 0; tries < GRDRM_MAX_TRIES; ++tries) {
594                 struct drm_mode_get_connector res;
595                 bool resized = false;
596                 uint32_t max;
597
598                 zero(res);
599                 res.connector_id = connector->object.id;
600                 res.encoders_ptr = PTR_TO_UINT64(connector->kern.encoders);
601                 res.props_ptr = PTR_TO_UINT64(connector->kern.prop_ids);
602                 res.prop_values_ptr = PTR_TO_UINT64(connector->kern.prop_values);
603                 res.count_encoders = connector->kern.max_encoders;
604                 res.count_props = connector->kern.max_props;
605
606                 /* Retrieve modes only if we have none. This avoids expensive
607                  * EDID reads in the kernel, that can slow down resyncs
608                  * considerably! */
609                 if (connector->kern.n_modes == 0) {
610                         res.modes_ptr = PTR_TO_UINT64(connector->kern.modes);
611                         res.count_modes = connector->kern.max_modes;
612                 }
613
614                 r = ioctl(card->fd, DRM_IOCTL_MODE_GETCONNECTOR, &res);
615                 if (r < 0) {
616                         r = -errno;
617                         if (r == -ENOENT) {
618                                 card->async_hotplug = true;
619                                 r = 0;
620                                 log_debug("grdrm: %s: connector %u removed during resync", card->base.name, connector->object.id);
621                         } else {
622                                 log_debug("grdrm: %s: cannot retrieve connector %u: %m", card->base.name, connector->object.id);
623                         }
624
625                         return r;
626                 }
627
628                 if (res.count_encoders > connector->kern.max_encoders) {
629                         uint32_t *t;
630
631                         max = ALIGN_POWER2(res.count_encoders);
632                         if (!max || max > UINT16_MAX) {
633                                 log_debug("grdrm: %s: excessive connector resource limit: %" PRIu32, card->base.name, max);
634                                 return -ERANGE;
635                         }
636
637                         t = realloc(connector->kern.encoders, sizeof(*t) * max);
638                         if (!t)
639                                 return -ENOMEM;
640
641                         connector->kern.encoders = t;
642                         connector->kern.max_encoders = max;
643                         resized = true;
644                 }
645
646                 if (res.count_modes > connector->kern.max_modes) {
647                         struct drm_mode_modeinfo *t;
648
649                         max = ALIGN_POWER2(res.count_modes);
650                         if (!max || max > UINT16_MAX) {
651                                 log_debug("grdrm: %s: excessive connector resource limit: %" PRIu32, card->base.name, max);
652                                 return -ERANGE;
653                         }
654
655                         t = realloc(connector->kern.modes, sizeof(*t) * max);
656                         if (!t)
657                                 return -ENOMEM;
658
659                         connector->kern.modes = t;
660                         connector->kern.max_modes = max;
661                         resized = true;
662                 }
663
664                 if (res.count_props > connector->kern.max_props) {
665                         uint32_t *tids;
666                         uint64_t *tvals;
667
668                         max = ALIGN_POWER2(res.count_props);
669                         if (!max || max > UINT16_MAX) {
670                                 log_debug("grdrm: %s: excessive connector resource limit: %" PRIu32, card->base.name, max);
671                                 return -ERANGE;
672                         }
673
674                         tids = realloc(connector->kern.prop_ids, sizeof(*tids) * max);
675                         if (!tids)
676                                 return -ENOMEM;
677                         connector->kern.prop_ids = tids;
678
679                         tvals = realloc(connector->kern.prop_values, sizeof(*tvals) * max);
680                         if (!tvals)
681                                 return -ENOMEM;
682                         connector->kern.prop_values = tvals;
683
684                         connector->kern.max_props = max;
685                         resized = true;
686                 }
687
688                 if (resized)
689                         continue;
690
691                 connector->kern.n_encoders = res.count_encoders;
692                 connector->kern.n_modes = res.count_modes;
693                 connector->kern.n_props = res.count_props;
694                 connector->kern.type = res.connector_type;
695                 connector->kern.type_id = res.connector_type_id;
696                 connector->kern.used_encoder = res.encoder_id;
697                 connector->kern.connection = res.connection;
698                 connector->kern.mm_width = res.mm_width;
699                 connector->kern.mm_height = res.mm_height;
700                 connector->kern.subpixel = res.subpixel;
701
702                 break;
703         }
704
705         if (tries >= GRDRM_MAX_TRIES) {
706                 log_debug("grdrm: %s: connector %u not settled for retrieval", card->base.name, connector->object.id);
707                 return -EFAULT;
708         }
709
710         return 0;
711 }
712
713 /*
714  * Encoders
715  */
716
717 static void encoder_free(grdrm_object *object) {
718         grdrm_encoder *encoder = encoder_from_object(object);
719
720         free(encoder->kern.clones);
721         free(encoder->kern.crtcs);
722         free(encoder);
723 }
724
725 int grdrm_encoder_new(grdrm_encoder **out, grdrm_card *card, uint32_t id, uint32_t index) {
726         _cleanup_(grdrm_object_freep) grdrm_object *object = NULL;
727         grdrm_encoder *encoder;
728         int r;
729
730         assert(card);
731
732         encoder = new0(grdrm_encoder, 1);
733         if (!encoder)
734                 return -ENOMEM;
735
736         object = &encoder->object;
737         *object = GRDRM_OBJECT_INIT(card, id, index, GRDRM_TYPE_ENCODER, encoder_free);
738
739         encoder->kern.max_crtcs = 32;
740         encoder->kern.crtcs = new0(uint32_t, encoder->kern.max_crtcs);
741         if (!encoder->kern.crtcs)
742                 return -ENOMEM;
743
744         encoder->kern.max_clones = 32;
745         encoder->kern.clones = new0(uint32_t, encoder->kern.max_clones);
746         if (!encoder->kern.clones)
747                 return -ENOMEM;
748
749         r = grdrm_object_add(object);
750         if (r < 0)
751                 return r;
752
753         if (out)
754                 *out = encoder;
755         object = NULL;
756         return 0;
757 }
758
759 static int grdrm_encoder_resync(grdrm_encoder *encoder) {
760         grdrm_card *card = encoder->object.card;
761         struct drm_mode_get_encoder res;
762         grdrm_object *object;
763         Iterator iter;
764         int r;
765
766         assert(encoder);
767
768         zero(res);
769         res.encoder_id = encoder->object.id;
770
771         r = ioctl(card->fd, DRM_IOCTL_MODE_GETENCODER, &res);
772         if (r < 0) {
773                 r = -errno;
774                 if (r == -ENOENT) {
775                         card->async_hotplug = true;
776                         r = 0;
777                         log_debug("grdrm: %s: encoder %u removed during resync", card->base.name, encoder->object.id);
778                 } else {
779                         log_debug("grdrm: %s: cannot retrieve encoder %u: %m", card->base.name, encoder->object.id);
780                 }
781
782                 return r;
783         }
784
785         encoder->kern.type = res.encoder_type;
786         encoder->kern.used_crtc = res.crtc_id;
787
788         encoder->kern.n_crtcs = 0;
789         memzero(encoder->kern.crtcs, sizeof(uint32_t) * encoder->kern.max_crtcs);
790
791         HASHMAP_FOREACH(object, card->object_map, iter) {
792                 if (object->type != GRDRM_TYPE_CRTC || object->index >= 32)
793                         continue;
794                 if (!(res.possible_crtcs & (1 << object->index)))
795                         continue;
796                 if (encoder->kern.n_crtcs >= 32) {
797                         log_debug("grdrm: %s: possible_crtcs exceeds 32bit mask", card->base.name);
798                         continue;
799                 }
800
801                 encoder->kern.crtcs[encoder->kern.n_crtcs++] = object->id;
802         }
803
804         encoder->kern.n_clones = 0;
805         memzero(encoder->kern.clones, sizeof(uint32_t) * encoder->kern.max_clones);
806
807         HASHMAP_FOREACH(object, card->object_map, iter) {
808                 if (object->type != GRDRM_TYPE_ENCODER || object->index >= 32)
809                         continue;
810                 if (!(res.possible_clones & (1 << object->index)))
811                         continue;
812                 if (encoder->kern.n_clones >= 32) {
813                         log_debug("grdrm: %s: possible_encoders exceeds 32bit mask", card->base.name);
814                         continue;
815                 }
816
817                 encoder->kern.clones[encoder->kern.n_clones++] = object->id;
818         }
819
820         return 0;
821 }
822
823 /*
824  * Crtcs
825  */
826
827 static void crtc_free(grdrm_object *object) {
828         grdrm_crtc *crtc = crtc_from_object(object);
829
830         if (crtc->pipe)
831                 grdev_pipe_free(&crtc->pipe->base);
832         free(crtc->set.connectors);
833         free(crtc->old.connectors);
834         free(crtc->kern.used_connectors);
835         free(crtc);
836 }
837
838 int grdrm_crtc_new(grdrm_crtc **out, grdrm_card *card, uint32_t id, uint32_t index) {
839         _cleanup_(grdrm_object_freep) grdrm_object *object = NULL;
840         grdrm_crtc *crtc;
841         int r;
842
843         assert(card);
844
845         crtc = new0(grdrm_crtc, 1);
846         if (!crtc)
847                 return -ENOMEM;
848
849         object = &crtc->object;
850         *object = GRDRM_OBJECT_INIT(card, id, index, GRDRM_TYPE_CRTC, crtc_free);
851
852         crtc->kern.max_used_connectors = 32;
853         crtc->kern.used_connectors = new0(uint32_t, crtc->kern.max_used_connectors);
854         if (!crtc->kern.used_connectors)
855                 return -ENOMEM;
856
857         crtc->old.connectors = new0(uint32_t, crtc->kern.max_used_connectors);
858         if (!crtc->old.connectors)
859                 return -ENOMEM;
860
861         r = grdrm_object_add(object);
862         if (r < 0)
863                 return r;
864
865         if (out)
866                 *out = crtc;
867         object = NULL;
868         return 0;
869 }
870
871 static int grdrm_crtc_resync(grdrm_crtc *crtc) {
872         grdrm_card *card = crtc->object.card;
873         struct drm_mode_crtc res = { .crtc_id = crtc->object.id };
874         int r;
875
876         assert(crtc);
877
878         /* make sure we can cache any combination later */
879         if (card->n_connectors > crtc->kern.max_used_connectors) {
880                 uint32_t max, *t;
881
882                 max = ALIGN_POWER2(card->n_connectors);
883                 if (!max)
884                         return -ENOMEM;
885
886                 t = realloc_multiply(crtc->kern.used_connectors, sizeof(*t), max);
887                 if (!t)
888                         return -ENOMEM;
889
890                 crtc->kern.used_connectors = t;
891                 crtc->kern.max_used_connectors = max;
892
893                 if (!crtc->old.set) {
894                         crtc->old.connectors = calloc(sizeof(*t), max);
895                         if (!crtc->old.connectors)
896                                 return -ENOMEM;
897                 }
898         }
899
900         /* GETCRTC doesn't return connectors. We have to read all
901          * encoder-state and deduce the setup ourselves.. */
902         crtc->kern.n_used_connectors = 0;
903
904         r = ioctl(card->fd, DRM_IOCTL_MODE_GETCRTC, &res);
905         if (r < 0) {
906                 r = -errno;
907                 if (r == -ENOENT) {
908                         card->async_hotplug = true;
909                         r = 0;
910                         log_debug("grdrm: %s: crtc %u removed during resync", card->base.name, crtc->object.id);
911                 } else {
912                         log_debug("grdrm: %s: cannot retrieve crtc %u: %m", card->base.name, crtc->object.id);
913                 }
914
915                 return r;
916         }
917
918         crtc->kern.used_fb = res.fb_id;
919         crtc->kern.fb_offset_x = res.x;
920         crtc->kern.fb_offset_y = res.y;
921         crtc->kern.gamma_size = res.gamma_size;
922         crtc->kern.mode_set = res.mode_valid;
923         crtc->kern.mode = res.mode;
924
925         return 0;
926 }
927
928 static void grdrm_crtc_assign(grdrm_crtc *crtc, grdrm_connector *connector) {
929         uint32_t n_connectors;
930         int r;
931
932         assert(crtc);
933         assert(!crtc->object.assigned);
934         assert(!connector || !connector->object.assigned);
935
936         /* always mark both as assigned; even if assignments cannot be set */
937         crtc->object.assigned = true;
938         if (connector)
939                 connector->object.assigned = true;
940
941         /* we will support hw clone mode in the future */
942         n_connectors = connector ? 1 : 0;
943
944         /* bail out if configuration is preserved */
945         if (crtc->set.n_connectors == n_connectors &&
946             (n_connectors == 0 || crtc->set.connectors[0] == connector->object.id))
947                 return;
948
949         crtc->applied = false;
950         crtc->set.n_connectors = 0;
951
952         if (n_connectors > crtc->set.max_connectors) {
953                 uint32_t max, *t;
954
955                 max = ALIGN_POWER2(n_connectors);
956                 if (!max) {
957                         r = -ENOMEM;
958                         goto error;
959                 }
960
961                 t = realloc(crtc->set.connectors, sizeof(*t) * max);
962                 if (!t) {
963                         r = -ENOMEM;
964                         goto error;
965                 }
966
967                 crtc->set.connectors = t;
968                 crtc->set.max_connectors = max;
969         }
970
971         if (connector) {
972                 struct drm_mode_modeinfo *m, *pref = NULL;
973                 uint32_t i;
974
975                 for (i = 0; i < connector->kern.n_modes; ++i) {
976                         m = &connector->kern.modes[i];
977
978                         /* ignore 3D modes by default */
979                         if (m->flags & DRM_MODE_FLAG_3D_MASK)
980                                 continue;
981
982                         if (!pref) {
983                                 pref = m;
984                                 continue;
985                         }
986
987                         /* use PREFERRED over non-PREFERRED */
988                         if ((pref->type & DRM_MODE_TYPE_PREFERRED) &&
989                             !(m->type & DRM_MODE_TYPE_PREFERRED))
990                                 continue;
991
992                         /* use DRIVER over non-PREFERRED|DRIVER */
993                         if ((pref->type & DRM_MODE_TYPE_DRIVER) &&
994                             !(m->type & (DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED)))
995                                 continue;
996
997                         /* always prefer higher resolution */
998                         if (pref->hdisplay > m->hdisplay ||
999                             (pref->hdisplay == m->hdisplay && pref->vdisplay > m->vdisplay))
1000                                 continue;
1001
1002                         pref = m;
1003                 }
1004
1005                 if (pref) {
1006                         crtc->set.mode = *pref;
1007                         crtc->set.n_connectors = 1;
1008                         crtc->set.connectors[0] = connector->object.id;
1009                         log_debug("grdrm: %s: assigned connector %" PRIu32 " to crtc %" PRIu32 " with mode %s",
1010                                   crtc->object.card->base.name, connector->object.id, crtc->object.id, pref->name);
1011                 } else {
1012                         log_debug("grdrm: %s: connector %" PRIu32 " to be assigned but has no valid mode",
1013                                   crtc->object.card->base.name, connector->object.id);
1014                 }
1015         }
1016
1017         return;
1018
1019 error:
1020         log_debug("grdrm: %s: cannot assign crtc %" PRIu32 ": %s",
1021                   crtc->object.card->base.name, crtc->object.id, strerror(-r));
1022 }
1023
1024 static void grdrm_crtc_expose(grdrm_crtc *crtc) {
1025         grdrm_pipe *pipe;
1026         grdrm_fb *fb;
1027         size_t i;
1028         int r;
1029
1030         assert(crtc);
1031         assert(crtc->object.assigned);
1032
1033         if (crtc->set.n_connectors < 1) {
1034                 if (crtc->pipe)
1035                         grdev_pipe_free(&crtc->pipe->base);
1036                 crtc->pipe = NULL;
1037                 return;
1038         }
1039
1040         pipe = crtc->pipe;
1041         if (pipe) {
1042                 if (pipe->base.width != crtc->set.mode.hdisplay ||
1043                     pipe->base.height != crtc->set.mode.vdisplay ||
1044                     pipe->base.vrefresh != crtc->set.mode.vrefresh) {
1045                         grdev_pipe_free(&pipe->base);
1046                         crtc->pipe = NULL;
1047                         pipe = NULL;
1048                 }
1049         }
1050
1051         if (crtc->pipe) {
1052                 pipe->base.front = NULL;
1053                 pipe->base.back = NULL;
1054                 for (i = 0; i < pipe->base.max_fbs; ++i) {
1055                         fb = fb_from_base(pipe->base.fbs[i]);
1056                         if (fb->id == crtc->kern.used_fb)
1057                                 pipe->base.front = &fb->base;
1058                         else if (!fb->flipid)
1059                                 pipe->base.back = &fb->base;
1060                 }
1061         } else {
1062                 r = grdrm_pipe_new(&pipe, crtc, &crtc->set.mode, 2);
1063                 if (r < 0) {
1064                         log_debug("grdrm: %s: cannot create pipe for crtc %" PRIu32 ": %s",
1065                                   crtc->object.card->base.name, crtc->object.id, strerror(-r));
1066                         return;
1067                 }
1068
1069                 for (i = 0; i < pipe->base.max_fbs; ++i) {
1070                         r = grdrm_fb_new(&fb, crtc->object.card, &crtc->set.mode);
1071                         if (r < 0) {
1072                                 log_debug("grdrm: %s: cannot allocate framebuffer for crtc %" PRIu32 ": %s",
1073                                           crtc->object.card->base.name, crtc->object.id, strerror(-r));
1074                                 grdev_pipe_free(&pipe->base);
1075                                 return;
1076                         }
1077
1078                         pipe->base.fbs[i] = &fb->base;
1079                 }
1080
1081                 pipe->base.front = NULL;
1082                 pipe->base.back = pipe->base.fbs[0];
1083                 crtc->pipe = pipe;
1084         }
1085
1086         grdev_pipe_ready(&crtc->pipe->base, true);
1087 }
1088
1089 static void grdrm_crtc_commit_deep(grdrm_crtc *crtc, grdev_fb **slot) {
1090         struct drm_mode_crtc set_crtc = { .crtc_id = crtc->object.id };
1091         grdrm_card *card = crtc->object.card;
1092         grdrm_pipe *pipe = crtc->pipe;
1093         grdrm_fb *fb = fb_from_base(*slot);
1094         size_t i;
1095         int r;
1096
1097         assert(crtc);
1098         assert(slot);
1099         assert(*slot);
1100         assert(pipe);
1101
1102         set_crtc.set_connectors_ptr = PTR_TO_UINT64(crtc->set.connectors);
1103         set_crtc.count_connectors = crtc->set.n_connectors;
1104         set_crtc.fb_id = fb->id;
1105         set_crtc.x = 0;
1106         set_crtc.y = 0;
1107         set_crtc.mode_valid = 1;
1108         set_crtc.mode = crtc->set.mode;
1109
1110         r = ioctl(card->fd, DRM_IOCTL_MODE_SETCRTC, &set_crtc);
1111         if (r < 0) {
1112                 r = -errno;
1113                 log_debug("grdrm: %s: cannot set crtc %" PRIu32 ": %m",
1114                           card->base.name, crtc->object.id);
1115
1116                 grdrm_card_async(card, r);
1117                 return;
1118         }
1119
1120         if (!crtc->applied) {
1121                 log_debug("grdrm: %s: crtc %" PRIu32 " applied via deep modeset",
1122                           card->base.name, crtc->object.id);
1123                 crtc->applied = true;
1124         }
1125
1126         *slot = NULL;
1127         pipe->base.front = &fb->base;
1128         fb->flipid = 0;
1129         ++pipe->counter;
1130         pipe->base.flipping = false;
1131         pipe->base.flip = false;
1132
1133         /* We cannot schedule dummy page-flips on pipes, hence, the
1134          * application would have to schedule their own frame-timers.
1135          * To avoid duplicating that everywhere, we schedule our own
1136          * timer and raise a fake FRAME event when it fires. */
1137         grdev_pipe_schedule(&pipe->base, 1);
1138
1139         if (!pipe->base.back) {
1140                 for (i = 0; i < pipe->base.max_fbs; ++i) {
1141                         if (!pipe->base.fbs[i])
1142                                 continue;
1143
1144                         fb = fb_from_base(pipe->base.fbs[i]);
1145                         if (&fb->base == pipe->base.front)
1146                                 continue;
1147
1148                         fb->flipid = 0;
1149                         pipe->base.back = &fb->base;
1150                         break;
1151                 }
1152         }
1153 }
1154
1155 static int grdrm_crtc_commit_flip(grdrm_crtc *crtc, grdev_fb **slot) {
1156         struct drm_mode_crtc_page_flip page_flip = { .crtc_id = crtc->object.id };
1157         grdrm_card *card = crtc->object.card;
1158         grdrm_pipe *pipe = crtc->pipe;
1159         grdrm_fb *fb = fb_from_base(*slot);
1160         uint32_t cnt;
1161         size_t i;
1162         int r;
1163
1164         assert(crtc);
1165         assert(slot);
1166         assert(*slot);
1167         assert(pipe);
1168
1169         if (!crtc->applied && !grdrm_modes_compatible(&crtc->kern.mode, &crtc->set.mode))
1170                 return 0;
1171
1172         cnt = ++pipe->counter ? : ++pipe->counter;
1173         page_flip.fb_id = fb->id;
1174         page_flip.flags = DRM_MODE_PAGE_FLIP_EVENT;
1175         page_flip.user_data = grdrm_encode_vblank_data(crtc->object.id, cnt);
1176
1177         r = ioctl(card->fd, DRM_IOCTL_MODE_PAGE_FLIP, &page_flip);
1178         if (r < 0) {
1179                 r = -errno;
1180                 log_debug("grdrm: %s: cannot schedule page-flip on crtc %" PRIu32 ": %m",
1181                           card->base.name, crtc->object.id);
1182
1183                 if (grdrm_card_async(card, r))
1184                         return r;
1185
1186                 return 0;
1187         }
1188
1189         if (!crtc->applied) {
1190                 log_debug("grdrm: %s: crtc %" PRIu32 " applied via page flip",
1191                           card->base.name, crtc->object.id);
1192                 crtc->applied = true;
1193         }
1194
1195         pipe->base.flipping = true;
1196         pipe->base.flip = false;
1197         pipe->counter = cnt;
1198         fb->flipid = cnt;
1199         *slot = NULL;
1200
1201         /* Raise fake FRAME event if it takes longer than 2
1202          * frames to receive the pageflip event. We assume the
1203          * queue ran over or some other error happened. */
1204         grdev_pipe_schedule(&pipe->base, 2);
1205
1206         if (!pipe->base.back) {
1207                 for (i = 0; i < pipe->base.max_fbs; ++i) {
1208                         if (!pipe->base.fbs[i])
1209                                 continue;
1210
1211                         fb = fb_from_base(pipe->base.fbs[i]);
1212                         if (&fb->base == pipe->base.front)
1213                                 continue;
1214                         if (fb->flipid)
1215                                 continue;
1216
1217                         pipe->base.back = &fb->base;
1218                         break;
1219                 }
1220         }
1221
1222         return 1;
1223 }
1224
1225 static void grdrm_crtc_commit(grdrm_crtc *crtc) {
1226         struct drm_mode_crtc set_crtc = { .crtc_id = crtc->object.id };
1227         grdrm_card *card = crtc->object.card;
1228         grdrm_pipe *pipe;
1229         grdev_fb **slot;
1230         int r;
1231
1232         assert(crtc);
1233         assert(crtc->object.assigned);
1234
1235         pipe = crtc->pipe;
1236         if (!pipe) {
1237                 /* If a crtc is not assigned any connector, we want any
1238                  * previous setup to be cleared, so make sure the CRTC is
1239                  * disabled. Otherwise, there might be content on the CRTC
1240                  * while we run, which is not what we want.
1241                  * If you want to avoid modesets on specific CRTCs, you should
1242                  * still keep their assignment, but never enable the resulting
1243                  * pipe. This way, we wouldn't touch it at all. */
1244                 if (!crtc->applied) {
1245                         crtc->applied = true;
1246                         r = ioctl(card->fd, DRM_IOCTL_MODE_SETCRTC, &set_crtc);
1247                         if (r < 0) {
1248                                 r = -errno;
1249                                 log_debug("grdrm: %s: cannot shutdown crtc %" PRIu32 ": %m",
1250                                           card->base.name, crtc->object.id);
1251
1252                                 grdrm_card_async(card, r);
1253                                 return;
1254                         }
1255
1256                         log_debug("grdrm: %s: crtc %" PRIu32 " applied via shutdown",
1257                                   card->base.name, crtc->object.id);
1258                 }
1259
1260                 return;
1261         }
1262
1263         /* we always fully ignore disabled pipes */
1264         if (!pipe->base.enabled)
1265                 return;
1266
1267         assert(crtc->set.n_connectors > 0);
1268
1269         if (pipe->base.flip)
1270                 slot = &pipe->base.back;
1271         else if (!crtc->applied)
1272                 slot = &pipe->base.front;
1273         else
1274                 return;
1275
1276         if (!*slot)
1277                 return;
1278
1279         r = grdrm_crtc_commit_flip(crtc, slot);
1280         if (r == 0) {
1281                 /* in case we couldn't page-flip, perform deep modeset */
1282                 grdrm_crtc_commit_deep(crtc, slot);
1283         }
1284 }
1285
1286 static void grdrm_crtc_restore(grdrm_crtc *crtc) {
1287         struct drm_mode_crtc set_crtc = { .crtc_id = crtc->object.id };
1288         grdrm_card *card = crtc->object.card;
1289         int r;
1290
1291         if (!crtc->old.set)
1292                 return;
1293
1294         set_crtc.set_connectors_ptr = PTR_TO_UINT64(crtc->old.connectors);
1295         set_crtc.count_connectors = crtc->old.n_connectors;
1296         set_crtc.fb_id = crtc->old.fb;
1297         set_crtc.x = crtc->old.fb_x;
1298         set_crtc.y = crtc->old.fb_y;
1299         set_crtc.gamma_size = crtc->old.gamma;
1300         set_crtc.mode_valid = crtc->old.mode_set;
1301         set_crtc.mode = crtc->old.mode;
1302
1303         r = ioctl(card->fd, DRM_IOCTL_MODE_SETCRTC, &set_crtc);
1304         if (r < 0) {
1305                 r = -errno;
1306                 log_debug("grdrm: %s: cannot restore crtc %" PRIu32 ": %m",
1307                           card->base.name, crtc->object.id);
1308
1309                 grdrm_card_async(card, r);
1310                 return;
1311         }
1312
1313         if (crtc->pipe) {
1314                 ++crtc->pipe->counter;
1315                 crtc->pipe->base.front = NULL;
1316                 crtc->pipe->base.flipping = false;
1317         }
1318
1319         log_debug("grdrm: %s: crtc %" PRIu32 " restored", card->base.name, crtc->object.id);
1320 }
1321
1322 static void grdrm_crtc_flip_complete(grdrm_crtc *crtc, uint32_t counter, struct drm_event_vblank *event) {
1323         bool flipped = false;
1324         grdrm_pipe *pipe;
1325         grdrm_fb *back = NULL;
1326         size_t i;
1327
1328         assert(crtc);
1329         assert(event);
1330
1331         pipe = crtc->pipe;
1332         if (!pipe)
1333                 return;
1334
1335         /* We got a page-flip event. To be safe, we reset all FBs on the same
1336          * pipe that have smaller flipids than the flip we got as we know they
1337          * are executed in order. We need to do this to guarantee
1338          * queue-overflows or other missed events don't cause starvation.
1339          * Furthermore, if we find the exact FB this event is for, *and* this
1340          * is the most recent event, we mark it as front FB and raise a
1341          * frame event. */
1342
1343         for (i = 0; i < pipe->base.max_fbs; ++i) {
1344                 grdrm_fb *fb;
1345
1346                 if (!pipe->base.fbs[i])
1347                         continue;
1348
1349                 fb = fb_from_base(pipe->base.fbs[i]);
1350                 if (counter != 0 && counter == pipe->counter && fb->flipid == counter) {
1351                         pipe->base.front = &fb->base;
1352                         flipped = true;
1353                 }
1354
1355                 if (counter - fb->flipid < UINT16_MAX) {
1356                         fb->flipid = 0;
1357                         back = fb;
1358                 } else if (fb->flipid == 0) {
1359                         back = fb;
1360                 }
1361         }
1362
1363         if (!pipe->base.back)
1364                 pipe->base.back = &back->base;
1365
1366         if (flipped) {
1367                 crtc->pipe->base.flipping = false;
1368                 grdev_pipe_frame(&pipe->base);
1369         }
1370 }
1371
1372 /*
1373  * Framebuffers
1374  */
1375
1376 static int grdrm_fb_new(grdrm_fb **out, grdrm_card *card, const struct drm_mode_modeinfo *mode) {
1377         _cleanup_(grdrm_fb_freep) grdrm_fb *fb = NULL;
1378         struct drm_mode_create_dumb create_dumb = { };
1379         struct drm_mode_map_dumb map_dumb = { };
1380         struct drm_mode_fb_cmd2 add_fb = { };
1381         unsigned int i;
1382         int r;
1383
1384         assert_return(out, -EINVAL);
1385         assert_return(card, -EINVAL);
1386
1387         fb = new0(grdrm_fb, 1);
1388         if (!fb)
1389                 return -ENOMEM;
1390
1391         /* TODO: we should choose a compatible format of the previous CRTC
1392          * setting to allow page-flip to it. Only choose fallback if the
1393          * previous setting was crap (non xrgb32'ish). */
1394
1395         fb->card = card;
1396         fb->base.format = DRM_FORMAT_XRGB8888;
1397         fb->base.width = mode->hdisplay;
1398         fb->base.height = mode->vdisplay;
1399
1400         for (i = 0; i < ELEMENTSOF(fb->base.maps); ++i)
1401                 fb->base.maps[i] = MAP_FAILED;
1402
1403         create_dumb.width = fb->base.width;
1404         create_dumb.height = fb->base.height;
1405         create_dumb.bpp = 32;
1406
1407         r = ioctl(card->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
1408         if (r < 0) {
1409                 r = -errno;
1410                 log_debug("grdrm: %s: cannot create dumb buffer %" PRIu32 "x%" PRIu32": %m",
1411                           card->base.name, fb->base.width, fb->base.height);
1412                 return r;
1413         }
1414
1415         fb->handles[0] = create_dumb.handle;
1416         fb->base.strides[0] = create_dumb.pitch;
1417         fb->sizes[0] = create_dumb.size;
1418
1419         map_dumb.handle = fb->handles[0];
1420
1421         r = ioctl(card->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
1422         if (r < 0) {
1423                 r = -errno;
1424                 log_debug("grdrm: %s: cannot map dumb buffer %" PRIu32 "x%" PRIu32": %m",
1425                           card->base.name, fb->base.width, fb->base.height);
1426                 return r;
1427         }
1428
1429         fb->base.maps[0] = mmap(0, fb->sizes[0], PROT_WRITE, MAP_SHARED, card->fd, map_dumb.offset);
1430         if (fb->base.maps[0] == MAP_FAILED) {
1431                 r = -errno;
1432                 log_debug("grdrm: %s: cannot memory-map dumb buffer %" PRIu32 "x%" PRIu32": %m",
1433                           card->base.name, fb->base.width, fb->base.height);
1434                 return r;
1435         }
1436
1437         memzero(fb->base.maps[0], fb->sizes[0]);
1438
1439         add_fb.width = fb->base.width;
1440         add_fb.height = fb->base.height;
1441         add_fb.pixel_format = fb->base.format;
1442         add_fb.flags = 0;
1443         memcpy(add_fb.handles, fb->handles, sizeof(fb->handles));
1444         memcpy(add_fb.pitches, fb->base.strides, sizeof(fb->base.strides));
1445         memcpy(add_fb.offsets, fb->offsets, sizeof(fb->offsets));
1446
1447         r = ioctl(card->fd, DRM_IOCTL_MODE_ADDFB2, &add_fb);
1448         if (r < 0) {
1449                 r = -errno;
1450                 log_debug("grdrm: %s: cannot add framebuffer %" PRIu32 "x%" PRIu32": %m",
1451                           card->base.name, fb->base.width, fb->base.height);
1452                 return r;
1453         }
1454
1455         fb->id = add_fb.fb_id;
1456
1457         *out = fb;
1458         fb = NULL;
1459         return 0;
1460 }
1461
1462 grdrm_fb *grdrm_fb_free(grdrm_fb *fb) {
1463         unsigned int i;
1464
1465         if (!fb)
1466                 return NULL;
1467
1468         assert(fb->card);
1469
1470         if (fb->id > 0 && fb->card->fd >= 0)
1471                 ioctl(fb->card->fd, DRM_IOCTL_MODE_RMFB, fb->id);
1472
1473         for (i = 0; i < ELEMENTSOF(fb->handles); ++i) {
1474                 struct drm_mode_destroy_dumb destroy_dumb = { };
1475
1476                 if (fb->base.maps[i] != MAP_FAILED)
1477                         munmap(fb->base.maps[i], fb->sizes[i]);
1478
1479                 if (fb->handles[i] > 0 && fb->card->fd >= 0) {
1480                         destroy_dumb.handle = fb->handles[i];
1481                         ioctl(fb->card->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
1482                 }
1483         }
1484
1485         free(fb);
1486
1487         return NULL;
1488 }
1489
1490 /*
1491  * Pipes
1492  */
1493
1494 static void grdrm_pipe_name(char *out, grdrm_crtc *crtc) {
1495         /* @out must be at least of size GRDRM_PIPE_NAME_MAX */
1496         sprintf(out, "%s/%" PRIu32, crtc->object.card->base.name, crtc->object.id);
1497 }
1498
1499 static int grdrm_pipe_new(grdrm_pipe **out, grdrm_crtc *crtc, struct drm_mode_modeinfo *mode, size_t n_fbs) {
1500         _cleanup_(grdev_pipe_freep) grdev_pipe *basepipe = NULL;
1501         grdrm_card *card = crtc->object.card;
1502         char name[GRDRM_PIPE_NAME_MAX];
1503         grdrm_pipe *pipe;
1504         int r;
1505
1506         assert_return(crtc, -EINVAL);
1507         assert_return(grdev_is_drm_card(&card->base), -EINVAL);
1508
1509         pipe = new0(grdrm_pipe, 1);
1510         if (!pipe)
1511                 return -ENOMEM;
1512
1513         basepipe = &pipe->base;
1514         pipe->base = GRDEV_PIPE_INIT(&grdrm_pipe_vtable, &card->base);
1515         pipe->crtc = crtc;
1516         pipe->base.width = mode->hdisplay;
1517         pipe->base.height = mode->vdisplay;
1518         pipe->base.vrefresh = mode->vrefresh ? : 25;
1519
1520         grdrm_pipe_name(name, crtc);
1521         r = grdev_pipe_add(&pipe->base, name, n_fbs);
1522         if (r < 0)
1523                 return r;
1524
1525         if (out)
1526                 *out = pipe;
1527         basepipe = NULL;
1528         return 0;
1529 }
1530
1531 static void grdrm_pipe_free(grdev_pipe *basepipe) {
1532         grdrm_pipe *pipe = grdrm_pipe_from_base(basepipe);
1533         size_t i;
1534
1535         assert(pipe->crtc);
1536
1537         for (i = 0; i < pipe->base.max_fbs; ++i)
1538                 if (pipe->base.fbs[i])
1539                         grdrm_fb_free(fb_from_base(pipe->base.fbs[i]));
1540
1541         free(pipe);
1542 }
1543
1544 static const grdev_pipe_vtable grdrm_pipe_vtable = {
1545         .free                   = grdrm_pipe_free,
1546 };
1547
1548 /*
1549  * Cards
1550  */
1551
1552 static void grdrm_name(char *out, dev_t devnum) {
1553         /* @out must be at least of size GRDRM_CARD_NAME_MAX */
1554         sprintf(out, "drm/%u:%u", major(devnum), minor(devnum));
1555 }
1556
1557 static void grdrm_card_print(grdrm_card *card) {
1558         grdrm_object *object;
1559         grdrm_crtc *crtc;
1560         grdrm_encoder *encoder;
1561         grdrm_connector *connector;
1562         grdrm_plane *plane;
1563         Iterator iter;
1564         uint32_t i;
1565         char *p, *buf;
1566
1567         log_debug("grdrm: %s: state dump", card->base.name);
1568
1569         log_debug("  crtcs:");
1570         HASHMAP_FOREACH(object, card->object_map, iter) {
1571                 if (object->type != GRDRM_TYPE_CRTC)
1572                         continue;
1573
1574                 crtc = crtc_from_object(object);
1575                 log_debug("    (id: %u index: %d)", object->id, object->index);
1576
1577                 if (crtc->kern.mode_set)
1578                         log_debug("      mode: %dx%d", crtc->kern.mode.hdisplay, crtc->kern.mode.vdisplay);
1579                 else
1580                         log_debug("      mode: <none>");
1581         }
1582
1583         log_debug("  encoders:");
1584         HASHMAP_FOREACH(object, card->object_map, iter) {
1585                 if (object->type != GRDRM_TYPE_ENCODER)
1586                         continue;
1587
1588                 encoder = encoder_from_object(object);
1589                 log_debug("    (id: %u index: %d)", object->id, object->index);
1590
1591                 if (encoder->kern.used_crtc)
1592                         log_debug("      crtc: %u", encoder->kern.used_crtc);
1593                 else
1594                         log_debug("      crtc: <none>");
1595
1596                 buf = malloc((DECIMAL_STR_MAX(uint32_t) + 1) * encoder->kern.n_crtcs + 1);
1597                 if (buf) {
1598                         buf[0] = 0;
1599                         p = buf;
1600
1601                         for (i = 0; i < encoder->kern.n_crtcs; ++i)
1602                                 p += sprintf(p, " %" PRIu32, encoder->kern.crtcs[i]);
1603
1604                         log_debug("      possible crtcs:%s", buf);
1605                         free(buf);
1606                 }
1607
1608                 buf = malloc((DECIMAL_STR_MAX(uint32_t) + 1) * encoder->kern.n_clones + 1);
1609                 if (buf) {
1610                         buf[0] = 0;
1611                         p = buf;
1612
1613                         for (i = 0; i < encoder->kern.n_clones; ++i)
1614                                 p += sprintf(p, " %" PRIu32, encoder->kern.clones[i]);
1615
1616                         log_debug("      possible clones:%s", buf);
1617                         free(buf);
1618                 }
1619         }
1620
1621         log_debug("  connectors:");
1622         HASHMAP_FOREACH(object, card->object_map, iter) {
1623                 if (object->type != GRDRM_TYPE_CONNECTOR)
1624                         continue;
1625
1626                 connector = connector_from_object(object);
1627                 log_debug("    (id: %u index: %d)", object->id, object->index);
1628                 log_debug("      type: %" PRIu32 "-%" PRIu32 " connection: %" PRIu32 " subpixel: %" PRIu32 " extents: %" PRIu32 "x%" PRIu32,
1629                           connector->kern.type, connector->kern.type_id, connector->kern.connection, connector->kern.subpixel,
1630                           connector->kern.mm_width, connector->kern.mm_height);
1631
1632                 if (connector->kern.used_encoder)
1633                         log_debug("      encoder: %" PRIu32, connector->kern.used_encoder);
1634                 else
1635                         log_debug("      encoder: <none>");
1636
1637                 buf = malloc((DECIMAL_STR_MAX(uint32_t) + 1) * connector->kern.n_encoders + 1);
1638                 if (buf) {
1639                         buf[0] = 0;
1640                         p = buf;
1641
1642                         for (i = 0; i < connector->kern.n_encoders; ++i)
1643                                 p += sprintf(p, " %" PRIu32, connector->kern.encoders[i]);
1644
1645                         log_debug("      possible encoders:%s", buf);
1646                         free(buf);
1647                 }
1648
1649                 for (i = 0; i < connector->kern.n_modes; ++i) {
1650                         struct drm_mode_modeinfo *mode = &connector->kern.modes[i];
1651                         log_debug("      mode: %" PRIu32 "x%" PRIu32, mode->hdisplay, mode->vdisplay);
1652                 }
1653         }
1654
1655         log_debug("  planes:");
1656         HASHMAP_FOREACH(object, card->object_map, iter) {
1657                 if (object->type != GRDRM_TYPE_PLANE)
1658                         continue;
1659
1660                 plane = plane_from_object(object);
1661                 log_debug("    (id: %u index: %d)", object->id, object->index);
1662                 log_debug("      gamma-size: %" PRIu32, plane->kern.gamma_size);
1663
1664                 if (plane->kern.used_crtc)
1665                         log_debug("      crtc: %" PRIu32, plane->kern.used_crtc);
1666                 else
1667                         log_debug("      crtc: <none>");
1668
1669                 buf = malloc((DECIMAL_STR_MAX(uint32_t) + 1) * plane->kern.n_crtcs + 1);
1670                 if (buf) {
1671                         buf[0] = 0;
1672                         p = buf;
1673
1674                         for (i = 0; i < plane->kern.n_crtcs; ++i)
1675                                 p += sprintf(p, " %" PRIu32, plane->kern.crtcs[i]);
1676
1677                         log_debug("      possible crtcs:%s", buf);
1678                         free(buf);
1679                 }
1680
1681                 buf = malloc((DECIMAL_STR_MAX(unsigned int) + 3) * plane->kern.n_formats + 1);
1682                 if (buf) {
1683                         buf[0] = 0;
1684                         p = buf;
1685
1686                         for (i = 0; i < plane->kern.n_formats; ++i)
1687                                 p += sprintf(p, " 0x%x", (unsigned int)plane->kern.formats[i]);
1688
1689                         log_debug("      possible formats:%s", buf);
1690                         free(buf);
1691                 }
1692         }
1693 }
1694
1695 static int grdrm_card_resync(grdrm_card *card) {
1696         _cleanup_free_ uint32_t *crtc_ids = NULL, *encoder_ids = NULL, *connector_ids = NULL, *plane_ids = NULL;
1697         uint32_t allocated = 0;
1698         grdrm_object *object;
1699         Iterator iter;
1700         size_t tries;
1701         int r;
1702
1703         assert(card);
1704
1705         card->async_hotplug = false;
1706         allocated = 0;
1707
1708         /* mark existing objects for possible removal */
1709         HASHMAP_FOREACH(object, card->object_map, iter)
1710                 object->present = false;
1711
1712         for (tries = 0; tries < GRDRM_MAX_TRIES; ++tries) {
1713                 struct drm_mode_get_plane_res pres;
1714                 struct drm_mode_card_res res;
1715                 uint32_t i, max;
1716
1717                 if (allocated < card->max_ids) {
1718                         free(crtc_ids);
1719                         free(encoder_ids);
1720                         free(connector_ids);
1721                         free(plane_ids);
1722                         crtc_ids = new0(uint32_t, card->max_ids);
1723                         encoder_ids = new0(uint32_t, card->max_ids);
1724                         connector_ids = new0(uint32_t, card->max_ids);
1725                         plane_ids = new0(uint32_t, card->max_ids);
1726
1727                         if (!crtc_ids || !encoder_ids || !connector_ids || !plane_ids)
1728                                 return -ENOMEM;
1729
1730                         allocated = card->max_ids;
1731                 }
1732
1733                 zero(res);
1734                 res.crtc_id_ptr = PTR_TO_UINT64(crtc_ids);
1735                 res.connector_id_ptr = PTR_TO_UINT64(connector_ids);
1736                 res.encoder_id_ptr = PTR_TO_UINT64(encoder_ids);
1737                 res.count_crtcs = allocated;
1738                 res.count_encoders = allocated;
1739                 res.count_connectors = allocated;
1740
1741                 r = ioctl(card->fd, DRM_IOCTL_MODE_GETRESOURCES, &res);
1742                 if (r < 0) {
1743                         r = -errno;
1744                         log_debug("grdrm: %s: cannot retrieve drm resources: %m", card->base.name);
1745                         return r;
1746                 }
1747
1748                 zero(pres);
1749                 pres.plane_id_ptr = PTR_TO_UINT64(plane_ids);
1750                 pres.count_planes = allocated;
1751
1752                 r = ioctl(card->fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &pres);
1753                 if (r < 0) {
1754                         r = -errno;
1755                         log_debug("grdrm: %s: cannot retrieve drm plane-resources: %m", card->base.name);
1756                         return r;
1757                 }
1758
1759                 max = MAX(MAX(res.count_crtcs, res.count_encoders),
1760                           MAX(res.count_connectors, pres.count_planes));
1761                 if (max > allocated) {
1762                         uint32_t n;
1763
1764                         n = ALIGN_POWER2(max);
1765                         if (!n || n > UINT16_MAX) {
1766                                 log_debug("grdrm: %s: excessive DRM resource limit: %" PRIu32, card->base.name, max);
1767                                 return -ERANGE;
1768                         }
1769
1770                         /* retry with resized buffers */
1771                         card->max_ids = n;
1772                         continue;
1773                 }
1774
1775                 /* mark available objects as present */
1776
1777                 for (i = 0; i < res.count_crtcs; ++i) {
1778                         object = grdrm_find_object(card, crtc_ids[i]);
1779                         if (object && object->type == GRDRM_TYPE_CRTC) {
1780                                 object->present = true;
1781                                 object->index = i;
1782                                 crtc_ids[i] = 0;
1783                         }
1784                 }
1785
1786                 for (i = 0; i < res.count_encoders; ++i) {
1787                         object = grdrm_find_object(card, encoder_ids[i]);
1788                         if (object && object->type == GRDRM_TYPE_ENCODER) {
1789                                 object->present = true;
1790                                 object->index = i;
1791                                 encoder_ids[i] = 0;
1792                         }
1793                 }
1794
1795                 for (i = 0; i < res.count_connectors; ++i) {
1796                         object = grdrm_find_object(card, connector_ids[i]);
1797                         if (object && object->type == GRDRM_TYPE_CONNECTOR) {
1798                                 object->present = true;
1799                                 object->index = i;
1800                                 connector_ids[i] = 0;
1801                         }
1802                 }
1803
1804                 for (i = 0; i < pres.count_planes; ++i) {
1805                         object = grdrm_find_object(card, plane_ids[i]);
1806                         if (object && object->type == GRDRM_TYPE_PLANE) {
1807                                 object->present = true;
1808                                 object->index = i;
1809                                 plane_ids[i] = 0;
1810                         }
1811                 }
1812
1813                 /* drop removed objects */
1814
1815                 HASHMAP_FOREACH(object, card->object_map, iter)
1816                         if (!object->present)
1817                                 grdrm_object_free(object);
1818
1819                 /* add new objects */
1820
1821                 card->n_crtcs = res.count_crtcs;
1822                 for (i = 0; i < res.count_crtcs; ++i) {
1823                         if (crtc_ids[i] < 1)
1824                                 continue;
1825
1826                         r = grdrm_crtc_new(NULL, card, crtc_ids[i], i);
1827                         if (r < 0)
1828                                 return r;
1829                 }
1830
1831                 card->n_encoders = res.count_encoders;
1832                 for (i = 0; i < res.count_encoders; ++i) {
1833                         if (encoder_ids[i] < 1)
1834                                 continue;
1835
1836                         r = grdrm_encoder_new(NULL, card, encoder_ids[i], i);
1837                         if (r < 0)
1838                                 return r;
1839                 }
1840
1841                 card->n_connectors = res.count_connectors;
1842                 for (i = 0; i < res.count_connectors; ++i) {
1843                         if (connector_ids[i] < 1)
1844                                 continue;
1845
1846                         r = grdrm_connector_new(NULL, card, connector_ids[i], i);
1847                         if (r < 0)
1848                                 return r;
1849                 }
1850
1851                 card->n_planes = pres.count_planes;
1852                 for (i = 0; i < pres.count_planes; ++i) {
1853                         if (plane_ids[i] < 1)
1854                                 continue;
1855
1856                         r = grdrm_plane_new(NULL, card, plane_ids[i], i);
1857                         if (r < 0)
1858                                 return r;
1859                 }
1860
1861                 /* re-sync objects after object_map is synced */
1862
1863                 HASHMAP_FOREACH(object, card->object_map, iter) {
1864                         switch (object->type) {
1865                         case GRDRM_TYPE_CRTC:
1866                                 r = grdrm_crtc_resync(crtc_from_object(object));
1867                                 break;
1868                         case GRDRM_TYPE_ENCODER:
1869                                 r = grdrm_encoder_resync(encoder_from_object(object));
1870                                 break;
1871                         case GRDRM_TYPE_CONNECTOR:
1872                                 r = grdrm_connector_resync(connector_from_object(object));
1873                                 break;
1874                         case GRDRM_TYPE_PLANE:
1875                                 r = grdrm_plane_resync(plane_from_object(object));
1876                                 break;
1877                         default:
1878                                 assert_not_reached("grdrm: invalid object type");
1879                                 r = 0;
1880                         }
1881
1882                         if (r < 0)
1883                                 return r;
1884
1885                         if (card->async_hotplug)
1886                                 break;
1887                 }
1888
1889                 /* if modeset objects change during sync, start over */
1890                 if (card->async_hotplug) {
1891                         card->async_hotplug = false;
1892                         continue;
1893                 }
1894
1895                 /* cache crtc/connector relationship */
1896                 HASHMAP_FOREACH(object, card->object_map, iter) {
1897                         grdrm_connector *connector;
1898                         grdrm_encoder *encoder;
1899                         grdrm_crtc *crtc;
1900
1901                         if (object->type != GRDRM_TYPE_CONNECTOR)
1902                                 continue;
1903
1904                         connector = connector_from_object(object);
1905                         if (connector->kern.connection != 1 || connector->kern.used_encoder < 1)
1906                                 continue;
1907
1908                         object = grdrm_find_object(card, connector->kern.used_encoder);
1909                         if (!object || object->type != GRDRM_TYPE_ENCODER)
1910                                 continue;
1911
1912                         encoder = encoder_from_object(object);
1913                         if (encoder->kern.used_crtc < 1)
1914                                 continue;
1915
1916                         object = grdrm_find_object(card, encoder->kern.used_crtc);
1917                         if (!object || object->type != GRDRM_TYPE_CRTC)
1918                                 continue;
1919
1920                         crtc = crtc_from_object(object);
1921                         assert(crtc->kern.n_used_connectors < crtc->kern.max_used_connectors);
1922                         crtc->kern.used_connectors[crtc->kern.n_used_connectors++] = connector->object.id;
1923                 }
1924
1925                 /* cache old crtc settings for later restore */
1926                 HASHMAP_FOREACH(object, card->object_map, iter) {
1927                         grdrm_crtc *crtc;
1928
1929                         if (object->type != GRDRM_TYPE_CRTC)
1930                                 continue;
1931
1932                         crtc = crtc_from_object(object);
1933
1934                         /* Save data if it is the first time we refresh the CRTC. This data can
1935                          * be used optionally to restore any previous configuration. For
1936                          * instance, it allows us to restore VT configurations after we close
1937                          * our session again. */
1938                         if (!crtc->old.set) {
1939                                 crtc->old.fb = crtc->kern.used_fb;
1940                                 crtc->old.fb_x = crtc->kern.fb_offset_x;
1941                                 crtc->old.fb_y = crtc->kern.fb_offset_y;
1942                                 crtc->old.gamma = crtc->kern.gamma_size;
1943                                 crtc->old.n_connectors = crtc->kern.n_used_connectors;
1944                                 if (crtc->old.n_connectors)
1945                                         memcpy(crtc->old.connectors, crtc->kern.used_connectors, sizeof(uint32_t) * crtc->old.n_connectors);
1946                                 crtc->old.mode_set = crtc->kern.mode_set;
1947                                 crtc->old.mode = crtc->kern.mode;
1948                                 crtc->old.set = true;
1949                         }
1950                 }
1951
1952                 /* everything synced */
1953                 break;
1954         }
1955
1956         if (tries >= GRDRM_MAX_TRIES) {
1957                 /*
1958                  * Ugh! We were unable to sync the DRM card state due to heavy
1959                  * hotplugging. This should never happen, so print a debug
1960                  * message and bail out. The next uevent will trigger
1961                  * this again.
1962                  */
1963
1964                 log_debug("grdrm: %s: hotplug-storm when syncing card", card->base.name);
1965                 return -EFAULT;
1966         }
1967
1968         return 0;
1969 }
1970
1971 static bool card_configure_crtc(grdrm_crtc *crtc, grdrm_connector *connector) {
1972         grdrm_card *card = crtc->object.card;
1973         grdrm_encoder *encoder;
1974         grdrm_object *object;
1975         uint32_t i, j;
1976
1977         if (crtc->object.assigned || connector->object.assigned)
1978                 return false;
1979         if (connector->kern.connection != 1)
1980                 return false;
1981
1982         for (i = 0; i < connector->kern.n_encoders; ++i) {
1983                 object = grdrm_find_object(card, connector->kern.encoders[i]);
1984                 if (!object || object->type != GRDRM_TYPE_ENCODER)
1985                         continue;
1986
1987                 encoder = encoder_from_object(object);
1988                 for (j = 0; j < encoder->kern.n_crtcs; ++j) {
1989                         if (encoder->kern.crtcs[j] == crtc->object.id) {
1990                                 grdrm_crtc_assign(crtc, connector);
1991                                 return true;
1992                         }
1993                 }
1994         }
1995
1996         return false;
1997 }
1998
1999 static void grdrm_card_configure(grdrm_card *card) {
2000         /*
2001          * Modeset Configuration
2002          * This is where we update our modeset configuration and assign
2003          * connectors to CRTCs. This means, each connector that we want to
2004          * enable needs a CRTC, disabled (or unavailable) connectors are left
2005          * alone in the dark. Once all CRTCs are assigned, the remaining CRTCs
2006          * are disabled.
2007          * Sounds trivial, but there're several caveats:
2008          *
2009          *   * Multiple connectors can be driven by the same CRTC. This is
2010          *     known as 'hardware clone mode'. Advantage over software clone
2011          *     mode is that only a single CRTC is needed to drive multiple
2012          *     displays. However, few hardware supports this and it's a huge
2013          *     headache to configure on dynamic demands. Therefore, we only
2014          *     support it if configured statically beforehand.
2015          *
2016          *   * CRTCs are not created equal. Some might be much more poweful
2017          *     than others, including more advanced plane support. So far, our
2018          *     CRTC selection is random. You need to supply static
2019          *     configuration if you want special setups. So far, there is no
2020          *     proper way to do advanced CRTC selection on dynamic demands. It
2021          *     is not really clear which demands require what CRTC, so, like
2022          *     everyone else, we do random CRTC selection unless explicitly
2023          *     states otherwise.
2024          *
2025          *   * Each Connector has a list of possible encoders that can drive
2026          *     it, and each encoder has a list of possible CRTCs. If this graph
2027          *     is a tree, assignment is trivial. However, if not, we cannot
2028          *     reliably decide on configurations beforehand. The encoder is
2029          *     always selected by the kernel, so we have to actually set a mode
2030          *     to know which encoder is used. There is no way to ask the kernel
2031          *     whether a given configuration is possible. This will change with
2032          *     atomic-modesetting, but until then, we keep our configurations
2033          *     simple and assume they work all just fine. If one fails
2034          *     unexpectedly, we print a warning and disable it.
2035          *
2036          * Configuring a card consists of several steps:
2037          *
2038          *  1) First of all, we apply any user-configuration. If a user wants
2039          *     a fixed configuration, we apply it and preserve it.
2040          *     So far, we don't support user configuration files, so this step
2041          *     is skipped.
2042          *
2043          *  2) Secondly, we need to apply any quirks from hwdb. Some hardware
2044          *     might only support limited configurations or require special
2045          *     CRTC/Connector mappings. We read this from hwdb and apply it, if
2046          *     present.
2047          *     So far, we don't support this as there is no known quirk, so
2048          *     this step is skipped.
2049          *
2050          *  3) As deep modesets are expensive, we try to avoid them if
2051          *     possible. Therefore, we read the current configuration from the
2052          *     kernel and try to preserve it, if compatible with our demands.
2053          *     If not, we break it and reassign it in a following step.
2054          *
2055          *  4) The main step involves configuring all remaining objects. By
2056          *     default, all available connectors are enabled, except for those
2057          *     disabled by user-configuration. We lookup a suitable CRTC for
2058          *     each connector and assign them. As there might be more
2059          *     connectors than CRTCs, we apply some ordering so users can
2060          *     select which connectors are more important right now.
2061          *     So far, we only apply the default ordering, more might be added
2062          *     in the future.
2063          */
2064
2065         grdrm_object *object;
2066         grdrm_crtc *crtc;
2067         Iterator i, j;
2068
2069         /* clear assignments */
2070         HASHMAP_FOREACH(object, card->object_map, i)
2071                 object->assigned = false;
2072
2073         /* preserve existing configurations */
2074         HASHMAP_FOREACH(object, card->object_map, i) {
2075                 if (object->type != GRDRM_TYPE_CRTC || object->assigned)
2076                         continue;
2077
2078                 crtc = crtc_from_object(object);
2079
2080                 if (crtc->applied) {
2081                         /* If our mode is set, preserve it. If no connector is
2082                          * set, modeset either failed or the pipe is unused. In
2083                          * both cases, leave it alone. It might be tried again
2084                          * below in case there're remaining connectors.
2085                          * Otherwise, try restoring the assignments. If they
2086                          * are no longer valid, leave the pipe untouched. */
2087
2088                         if (crtc->set.n_connectors < 1)
2089                                 continue;
2090
2091                         assert(crtc->set.n_connectors == 1);
2092
2093                         object = grdrm_find_object(card, crtc->set.connectors[0]);
2094                         if (!object || object->type != GRDRM_TYPE_CONNECTOR)
2095                                 continue;
2096
2097                         card_configure_crtc(crtc, connector_from_object(object));
2098                 } else if (crtc->kern.mode_set && crtc->kern.n_used_connectors != 1) {
2099                         /* If our mode is not set on the pipe, we know the kern
2100                          * information is valid. Try keeping it. If it's not
2101                          * possible, leave the pipe untouched for later
2102                          * assignements. */
2103
2104                         object = grdrm_find_object(card, crtc->kern.used_connectors[0]);
2105                         if (!object || object->type != GRDRM_TYPE_CONNECTOR)
2106                                 continue;
2107
2108                         card_configure_crtc(crtc, connector_from_object(object));
2109                 }
2110         }
2111
2112         /* assign remaining objects */
2113         HASHMAP_FOREACH(object, card->object_map, i) {
2114                 if (object->type != GRDRM_TYPE_CRTC || object->assigned)
2115                         continue;
2116
2117                 crtc = crtc_from_object(object);
2118
2119                 HASHMAP_FOREACH(object, card->object_map, j) {
2120                         if (object->type != GRDRM_TYPE_CONNECTOR)
2121                                 continue;
2122
2123                         if (card_configure_crtc(crtc, connector_from_object(object)))
2124                                 break;
2125                 }
2126
2127                 if (!crtc->object.assigned)
2128                         grdrm_crtc_assign(crtc, NULL);
2129         }
2130
2131         /* expose configuration */
2132         HASHMAP_FOREACH(object, card->object_map, i) {
2133                 if (object->type != GRDRM_TYPE_CRTC)
2134                         continue;
2135
2136                 grdrm_crtc_expose(crtc_from_object(object));
2137         }
2138 }
2139
2140 static void grdrm_card_hotplug(grdrm_card *card) {
2141         int r;
2142
2143         assert(card);
2144
2145         if (!card->running)
2146                 return;
2147
2148         card->ready = false;
2149         r = grdrm_card_resync(card);
2150         if (r < 0) {
2151                 log_debug("grdrm: %s/%s: cannot re-sync card: %s",
2152                           card->base.session->name, card->base.name, strerror(-r));
2153                 return;
2154         }
2155
2156         grdev_session_pin(card->base.session);
2157
2158         grdrm_card_print(card);
2159         grdrm_card_configure(card);
2160         card->ready = true;
2161
2162         grdev_session_unpin(card->base.session);
2163 }
2164
2165 static int grdrm_card_io_fn(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2166         grdrm_card *card = userdata;
2167         struct drm_event_vblank *vblank;
2168         struct drm_event *event;
2169         uint32_t id, counter;
2170         grdrm_object *object;
2171         char buf[4096];
2172         ssize_t l, i;
2173
2174         if (revents & (EPOLLHUP | EPOLLERR)) {
2175                 /* Immediately close device on HUP; no need to flush pending
2176                  * data.. there're no events we care about here. */
2177                 log_debug("grdrm: %s/%s: HUP", card->base.session->name, card->base.name);
2178                 grdrm_card_close(card);
2179                 return 0;
2180         }
2181
2182         if (revents & (EPOLLIN)) {
2183                 l = read(card->fd, buf, sizeof(buf));
2184                 if (l < 0) {
2185                         if (errno == EAGAIN || errno == EINTR)
2186                                 return 0;
2187
2188                         log_debug("grdrm: %s/%s: read error: %m", card->base.session->name, card->base.name);
2189                         grdrm_card_close(card);
2190                         return 0;
2191                 } else if ((size_t)l < sizeof(*event)) {
2192                         log_debug("grdrm: %s/%s: short read of %zd bytes", card->base.session->name, card->base.name, l);
2193                         return 0;
2194                 }
2195
2196                 for (i = 0; i < l; i += event->length) {
2197                         event = (void*)&buf[i];
2198
2199                         if (i + event->length > l) {
2200                                 log_debug("grdrm: %s/%s: truncated event", card->base.session->name, card->base.name);
2201                                 break;
2202                         }
2203
2204                         switch (event->type) {
2205                         case DRM_EVENT_FLIP_COMPLETE:
2206                                 vblank = (void*)event;
2207                                 if (event->length < sizeof(*vblank)) {
2208                                         log_debug("grdrm: %s/%s: truncated vblank event", card->base.session->name, card->base.name);
2209                                         break;
2210                                 }
2211
2212                                 grdrm_decode_vblank_data(vblank->user_data, &id, &counter);
2213                                 object = grdrm_find_object(card, id);
2214                                 if (!object || object->type != GRDRM_TYPE_CRTC)
2215                                         break;
2216
2217                                 grdrm_crtc_flip_complete(crtc_from_object(object), counter, vblank);
2218                                 break;
2219                         }
2220                 }
2221         }
2222
2223         return 0;
2224 }
2225
2226 static int grdrm_card_add(grdrm_card *card, const char *name) {
2227         assert(card);
2228         assert(card->fd < 0);
2229
2230         card->object_map = hashmap_new(&trivial_hash_ops);
2231         if (!card->object_map)
2232                 return -ENOMEM;
2233
2234         return grdev_card_add(&card->base, name);
2235 }
2236
2237 static void grdrm_card_destroy(grdrm_card *card) {
2238         assert(card);
2239         assert(!card->running);
2240         assert(card->fd < 0);
2241         assert(hashmap_size(card->object_map) == 0);
2242
2243         hashmap_free(card->object_map);
2244 }
2245
2246 static void grdrm_card_commit(grdev_card *basecard) {
2247         grdrm_card *card = grdrm_card_from_base(basecard);
2248         grdrm_object *object;
2249         Iterator iter;
2250
2251         HASHMAP_FOREACH(object, card->object_map, iter) {
2252                 if (!card->ready)
2253                         break;
2254
2255                 if (object->type != GRDRM_TYPE_CRTC)
2256                         continue;
2257
2258                 grdrm_crtc_commit(crtc_from_object(object));
2259         }
2260 }
2261
2262 static void grdrm_card_restore(grdev_card *basecard) {
2263         grdrm_card *card = grdrm_card_from_base(basecard);
2264         grdrm_object *object;
2265         Iterator iter;
2266
2267         HASHMAP_FOREACH(object, card->object_map, iter) {
2268                 if (!card->ready)
2269                         break;
2270
2271                 if (object->type != GRDRM_TYPE_CRTC)
2272                         continue;
2273
2274                 grdrm_crtc_restore(crtc_from_object(object));
2275         }
2276 }
2277
2278 static void grdrm_card_enable(grdrm_card *card) {
2279         assert(card);
2280
2281         if (card->fd < 0 || card->running)
2282                 return;
2283
2284         /* ignore cards without DUMB_BUFFER capability */
2285         if (!card->cap_dumb)
2286                 return;
2287
2288         assert(card->fd_src);
2289
2290         log_debug("grdrm: %s/%s: enable", card->base.session->name, card->base.name);
2291
2292         card->running = true;
2293         sd_event_source_set_enabled(card->fd_src, SD_EVENT_ON);
2294         grdrm_card_hotplug(card);
2295 }
2296
2297 static void grdrm_card_disable(grdrm_card *card) {
2298         grdrm_object *object;
2299         Iterator iter;
2300
2301         assert(card);
2302
2303         if (card->fd < 0 || !card->running)
2304                 return;
2305
2306         assert(card->fd_src);
2307
2308         log_debug("grdrm: %s/%s: disable", card->base.session->name, card->base.name);
2309
2310         card->running = false;
2311         card->ready = false;
2312         sd_event_source_set_enabled(card->fd_src, SD_EVENT_OFF);
2313
2314         /* stop all pipes */
2315         HASHMAP_FOREACH(object, card->object_map, iter) {
2316                 grdrm_crtc *crtc;
2317
2318                 if (object->type != GRDRM_TYPE_CRTC)
2319                         continue;
2320
2321                 crtc = crtc_from_object(object);
2322                 crtc->applied = false;
2323                 if (crtc->pipe)
2324                         grdev_pipe_ready(&crtc->pipe->base, false);
2325         }
2326 }
2327
2328 static int grdrm_card_open(grdrm_card *card, int dev_fd) {
2329         _cleanup_(grdev_session_unpinp) grdev_session *pin = NULL;
2330         _cleanup_close_ int fd = dev_fd;
2331         struct drm_get_cap cap;
2332         int r, flags;
2333
2334         assert(card);
2335         assert(dev_fd >= 0);
2336         assert(card->fd != dev_fd);
2337
2338         pin = grdev_session_pin(card->base.session);
2339         grdrm_card_close(card);
2340
2341         log_debug("grdrm: %s/%s: open", card->base.session->name, card->base.name);
2342
2343         r = fd_nonblock(fd, true);
2344         if (r < 0)
2345                 return r;
2346
2347         r = fd_cloexec(fd, true);
2348         if (r < 0)
2349                 return r;
2350
2351         flags = fcntl(fd, F_GETFL, 0);
2352         if (flags < 0)
2353                 return -errno;
2354         if ((flags & O_ACCMODE) != O_RDWR)
2355                 return -EACCES;
2356
2357         r = sd_event_add_io(card->base.session->context->event,
2358                             &card->fd_src,
2359                             fd,
2360                             EPOLLHUP | EPOLLERR | EPOLLIN,
2361                             grdrm_card_io_fn,
2362                             card);
2363         if (r < 0)
2364                 return r;
2365
2366         sd_event_source_set_enabled(card->fd_src, SD_EVENT_OFF);
2367
2368         card->fd = fd;
2369         fd = -1;
2370
2371         /* cache DUMB_BUFFER capability */
2372         cap.capability = DRM_CAP_DUMB_BUFFER;
2373         cap.value = 0;
2374         r = ioctl(card->fd, DRM_IOCTL_GET_CAP, &cap);
2375         card->cap_dumb = r >= 0 && cap.value;
2376         if (r < 0)
2377                 log_debug("grdrm: %s/%s: cannot retrieve DUMB_BUFFER capability: %s",
2378                           card->base.session->name, card->base.name, strerror(-r));
2379         else if (!card->cap_dumb)
2380                 log_debug("grdrm: %s/%s: DUMB_BUFFER capability not supported",
2381                           card->base.session->name, card->base.name);
2382
2383         /* cache TIMESTAMP_MONOTONIC capability */
2384         cap.capability = DRM_CAP_TIMESTAMP_MONOTONIC;
2385         cap.value = 0;
2386         r = ioctl(card->fd, DRM_IOCTL_GET_CAP, &cap);
2387         card->cap_monotonic = r >= 0 && cap.value;
2388         if (r < 0)
2389                 log_debug("grdrm: %s/%s: cannot retrieve TIMESTAMP_MONOTONIC capability: %s",
2390                           card->base.session->name, card->base.name, strerror(-r));
2391         else if (!card->cap_monotonic)
2392                 log_debug("grdrm: %s/%s: TIMESTAMP_MONOTONIC is disabled globally, fix this NOW!",
2393                           card->base.session->name, card->base.name);
2394
2395         return 0;
2396 }
2397
2398 static void grdrm_card_close(grdrm_card *card) {
2399         grdrm_object *object;
2400
2401         if (card->fd < 0)
2402                 return;
2403
2404         log_debug("grdrm: %s/%s: close", card->base.session->name, card->base.name);
2405
2406         grdrm_card_disable(card);
2407
2408         card->fd_src = sd_event_source_unref(card->fd_src);
2409         card->fd = safe_close(card->fd);
2410
2411         grdev_session_pin(card->base.session);
2412         while ((object = hashmap_first(card->object_map)))
2413                 grdrm_object_free(object);
2414         grdev_session_unpin(card->base.session);
2415 }
2416
2417 static bool grdrm_card_async(grdrm_card *card, int r) {
2418         switch (r) {
2419         case -EACCES:
2420                 /* If we get EACCES on runtime DRM calls, we lost DRM-Master
2421                  * (or we did something terribly wrong). Immediately disable
2422                  * the card, so we stop all pipes and wait to be activated
2423                  * again. */
2424                 grdrm_card_disable(card);
2425                 break;
2426         case -ENOENT:
2427                 /* DRM objects can be hotplugged at any time. If an object is
2428                  * removed that we use, we remember that state so a following
2429                  * call can test for this.
2430                  * Note that we also get a uevent as followup, this will resync
2431                  * the whole device. */
2432                 card->async_hotplug = true;
2433                 break;
2434         }
2435
2436         return !card->ready;
2437 }
2438
2439 /*
2440  * Unmanaged Cards
2441  * The unmanaged DRM card opens the device node for a given DRM device
2442  * directly (/dev/dri/cardX) and thus needs sufficient privileges. It opens
2443  * the device only if we really require it and releases it as soon as we're
2444  * disabled or closed.
2445  * The unmanaged element can be used in all situations where you have direct
2446  * access to DRM device nodes. Unlike managed DRM elements, it can be used
2447  * outside of user sessions and in emergency situations where logind is not
2448  * available.
2449  */
2450
2451 static void unmanaged_card_enable(grdev_card *basecard) {
2452         unmanaged_card *cu = unmanaged_card_from_base(basecard);
2453         int r, fd;
2454
2455         if (cu->card.fd < 0) {
2456                 /* try open on activation if it failed during allocation */
2457                 fd = open(cu->devnode, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
2458                 if (fd < 0) {
2459                         /* not fatal; simply ignore the device */
2460                         log_debug("grdrm: %s/%s: cannot open node %s: %m",
2461                                   basecard->session->name, basecard->name, cu->devnode);
2462                         return;
2463                 }
2464
2465                 /* we might already be DRM-Master by open(); that's fine */
2466
2467                 r = grdrm_card_open(&cu->card, fd);
2468                 if (r < 0) {
2469                         log_debug("grdrm: %s/%s: cannot open: %s",
2470                                   basecard->session->name, basecard->name, strerror(-r));
2471                         return;
2472                 }
2473         }
2474
2475         r = ioctl(cu->card.fd, DRM_IOCTL_SET_MASTER, 0);
2476         if (r < 0) {
2477                 log_debug("grdrm: %s/%s: cannot acquire DRM-Master: %m",
2478                           basecard->session->name, basecard->name);
2479                 return;
2480         }
2481
2482         grdrm_card_enable(&cu->card);
2483 }
2484
2485 static void unmanaged_card_disable(grdev_card *basecard) {
2486         unmanaged_card *cu = unmanaged_card_from_base(basecard);
2487
2488         grdrm_card_disable(&cu->card);
2489 }
2490
2491 static int unmanaged_card_new(grdev_card **out, grdev_session *session, struct udev_device *ud) {
2492         _cleanup_(grdev_card_freep) grdev_card *basecard = NULL;
2493         char name[GRDRM_CARD_NAME_MAX];
2494         unmanaged_card *cu;
2495         const char *devnode;
2496         dev_t devnum;
2497         int r, fd;
2498
2499         assert_return(session, -EINVAL);
2500         assert_return(ud, -EINVAL);
2501
2502         devnode = udev_device_get_devnode(ud);
2503         devnum = udev_device_get_devnum(ud);
2504         if (!devnode || devnum == 0)
2505                 return -ENODEV;
2506
2507         grdrm_name(name, devnum);
2508
2509         cu = new0(unmanaged_card, 1);
2510         if (!cu)
2511                 return -ENOMEM;
2512
2513         basecard = &cu->card.base;
2514         cu->card = GRDRM_CARD_INIT(&unmanaged_card_vtable, session);
2515
2516         cu->devnode = strdup(devnode);
2517         if (!cu->devnode)
2518                 return -ENOMEM;
2519
2520         r = grdrm_card_add(&cu->card, name);
2521         if (r < 0)
2522                 return r;
2523
2524         /* try to open but ignore errors */
2525         fd = open(cu->devnode, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
2526         if (fd < 0) {
2527                 /* not fatal; allow uaccess based control on activation */
2528                 log_debug("grdrm: %s/%s: cannot open node %s: %m",
2529                           basecard->session->name, basecard->name, cu->devnode);
2530         } else {
2531                 /* We might get DRM-Master implicitly on open(); drop it immediately
2532                  * so we acquire it only once we're actually enabled. */
2533                 ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
2534
2535                 r = grdrm_card_open(&cu->card, fd);
2536                 if (r < 0)
2537                         log_debug("grdrm: %s/%s: cannot open: %s",
2538                                   basecard->session->name, basecard->name, strerror(-r));
2539         }
2540
2541         if (out)
2542                 *out = basecard;
2543         basecard = NULL;
2544         return 0;
2545 }
2546
2547 static void unmanaged_card_free(grdev_card *basecard) {
2548         unmanaged_card *cu = unmanaged_card_from_base(basecard);
2549
2550         assert(!basecard->enabled);
2551
2552         grdrm_card_close(&cu->card);
2553         grdrm_card_destroy(&cu->card);
2554         free(cu->devnode);
2555         free(cu);
2556 }
2557
2558 static const grdev_card_vtable unmanaged_card_vtable = {
2559         .free                   = unmanaged_card_free,
2560         .enable                 = unmanaged_card_enable,
2561         .disable                = unmanaged_card_disable,
2562         .commit                 = grdrm_card_commit,
2563         .restore                = grdrm_card_restore,
2564 };
2565
2566 /*
2567  * Managed Cards
2568  * The managed DRM card uses systemd-logind to acquire DRM devices. This
2569  * means, we do not open the device node /dev/dri/cardX directly. Instead,
2570  * logind passes us a file-descriptor whenever our session is activated. Thus,
2571  * we don't need access to the device node directly.
2572  * Furthermore, whenever the session is put asleep, logind revokes the
2573  * file-descriptor so we loose access to the device.
2574  * Managed DRM cards should be preferred over unmanaged DRM cards whenever
2575  * you run inside a user session with exclusive device access.
2576  */
2577
2578 static void managed_card_enable(grdev_card *card) {
2579         managed_card *cm = managed_card_from_base(card);
2580
2581         /* If the device is manually re-enabled, we try to resume our card
2582          * management. Note that we have no control over DRM-Master and the fd,
2583          * so we have to take over the state from the last logind event. */
2584
2585         if (cm->master)
2586                 grdrm_card_enable(&cm->card);
2587 }
2588
2589 static void managed_card_disable(grdev_card *card) {
2590         managed_card *cm = managed_card_from_base(card);
2591
2592         /* If the device is manually disabled, we keep the FD but put our card
2593          * management asleep. This way, we can wake up at any time, but don't
2594          * touch the device while asleep. */
2595
2596         grdrm_card_disable(&cm->card);
2597 }
2598
2599 static int managed_card_pause_device_fn(sd_bus *bus,
2600                                         sd_bus_message *signal,
2601                                         void *userdata,
2602                                         sd_bus_error *ret_error) {
2603         managed_card *cm = userdata;
2604         grdev_session *session = cm->card.base.session;
2605         uint32_t major, minor;
2606         const char *mode;
2607         int r;
2608
2609         /*
2610          * We get PauseDevice() signals from logind whenever a device we
2611          * requested was, or is about to be, paused. Arguments are major/minor
2612          * number of the device and the mode of the operation.
2613          * In case the event is not about our device, we ignore it. Otherwise,
2614          * we treat it as asynchronous DRM-DROP-MASTER. Note that we might have
2615          * already handled an EACCES error from a modeset ioctl, in which case
2616          * we already disabled the device.
2617          *
2618          * @mode can be one of the following:
2619          *   "pause": The device is about to be paused. We must react
2620          *            immediately and respond with PauseDeviceComplete(). Once
2621          *            we replied, logind will pause the device. Note that
2622          *            logind might apply any kind of timeout and force pause
2623          *            the device if we don't respond in a timely manner. In
2624          *            this case, we will receive a second PauseDevice event
2625          *            with @mode set to "force" (or similar).
2626          *   "force": The device was disabled forecfully by logind. DRM-Master
2627          *            was already dropped. This is just an asynchronous
2628          *            notification so we can put the device asleep (in case
2629          *            we didn't already notice the dropped DRM-Master).
2630          *    "gone": This is like "force" but is sent if the device was
2631          *            paused due to a device-removal event.
2632          *
2633          * We always handle PauseDevice signals as "force" as we properly
2634          * support asynchronously dropping DRM-Master, anyway. But in case
2635          * logind sent mode "pause", we also call PauseDeviceComplete() to
2636          * immediately acknowledge the request.
2637          */
2638
2639         r = sd_bus_message_read(signal, "uus", &major, &minor, &mode);
2640         if (r < 0) {
2641                 log_debug("grdrm: %s/%s: erroneous PauseDevice signal",
2642                           session->name, cm->card.base.name);
2643                 return 0;
2644         }
2645
2646         /* not our device? */
2647         if (makedev(major, minor) != cm->devnum)
2648                 return 0;
2649
2650         cm->master = false;
2651         grdrm_card_disable(&cm->card);
2652
2653         if (streq(mode, "pause")) {
2654                 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2655
2656                 /*
2657                  * Sending PauseDeviceComplete() is racy if logind triggers the
2658                  * timeout. That is, if we take too long and logind pauses the
2659                  * device by sending a forced PauseDevice, our
2660                  * PauseDeviceComplete call will be stray. That's fine, though.
2661                  * logind ignores such stray calls. Only if logind also sent a
2662                  * further PauseDevice() signal, it might match our call
2663                  * incorrectly to the newer PauseDevice(). That's fine, too, as
2664                  * we handle that event asynchronously, anyway. Therefore,
2665                  * whatever happens, we're fine. Yay!
2666                  */
2667
2668                 r = sd_bus_message_new_method_call(session->context->sysbus,
2669                                                    &m,
2670                                                    "org.freedesktop.login1",
2671                                                    session->path,
2672                                                    "org.freedesktop.login1.Session",
2673                                                    "PauseDeviceComplete");
2674                 if (r >= 0) {
2675                         r = sd_bus_message_append(m, "uu", major, minor);
2676                         if (r >= 0)
2677                                 r = sd_bus_send(session->context->sysbus, m, NULL);
2678                 }
2679
2680                 if (r < 0)
2681                         log_debug("grdrm: %s/%s: cannot send PauseDeviceComplete: %s",
2682                                   session->name, cm->card.base.name, strerror(-r));
2683         }
2684
2685         return 0;
2686 }
2687
2688 static int managed_card_resume_device_fn(sd_bus *bus,
2689                                          sd_bus_message *signal,
2690                                          void *userdata,
2691                                          sd_bus_error *ret_error) {
2692         managed_card *cm = userdata;
2693         grdev_session *session = cm->card.base.session;
2694         uint32_t major, minor;
2695         int r, fd;
2696
2697         /*
2698          * We get ResumeDevice signals whenever logind resumed a previously
2699          * paused device. The arguments contain the major/minor number of the
2700          * related device and a new file-descriptor for the freshly opened
2701          * device-node.
2702          * If the signal is not about our device, we simply ignore it.
2703          * Otherwise, we immediately resume the device. Note that we drop the
2704          * new file-descriptor as we already have one from TakeDevice(). logind
2705          * preserves the file-context across pause/resume for DRM but only
2706          * drops/acquires DRM-Master accordingly. This way, our context (like
2707          * DRM-FBs and BOs) is preserved.
2708          */
2709
2710         r = sd_bus_message_read(signal, "uuh", &major, &minor, &fd);
2711         if (r < 0) {
2712                 log_debug("grdrm: %s/%s: erroneous ResumeDevice signal",
2713                           session->name, cm->card.base.name);
2714                 return 0;
2715         }
2716
2717         /* not our device? */
2718         if (makedev(major, minor) != cm->devnum)
2719                 return 0;
2720
2721         if (cm->card.fd < 0) {
2722                 /* This shouldn't happen. We should already own an FD from
2723                  * TakeDevice(). However, lets be safe and use this FD in case
2724                  * we really don't have one. There is no harm in doing this
2725                  * and our code works fine this way. */
2726                 fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
2727                 if (fd < 0) {
2728                         log_debug("grdrm: %s/%s: cannot duplicate fd: %m",
2729                                   session->name, cm->card.base.name);
2730                         return 0;
2731                 }
2732
2733                 r = grdrm_card_open(&cm->card, fd);
2734                 if (r < 0) {
2735                         log_debug("grdrm: %s/%s: cannot open: %s",
2736                                   session->name, cm->card.base.name, strerror(-r));
2737                         return 0;
2738                 }
2739         }
2740
2741         cm->master = true;
2742         if (cm->card.base.enabled)
2743                 grdrm_card_enable(&cm->card);
2744
2745         return 0;
2746 }
2747
2748 static int managed_card_setup_bus(managed_card *cm) {
2749         grdev_session *session = cm->card.base.session;
2750         _cleanup_free_ char *match = NULL;
2751         int r;
2752
2753         match = strjoin("type='signal',"
2754                         "sender='org.freedesktop.login1',"
2755                         "interface='org.freedesktop.login1.Session',"
2756                         "member='PauseDevice',"
2757                         "path='", session->path, "'",
2758                         NULL);
2759         if (!match)
2760                 return -ENOMEM;
2761
2762         r = sd_bus_add_match(session->context->sysbus,
2763                              &cm->slot_pause_device,
2764                              match,
2765                              managed_card_pause_device_fn,
2766                              cm);
2767         if (r < 0)
2768                 return r;
2769
2770         free(match);
2771         match = strjoin("type='signal',"
2772                         "sender='org.freedesktop.login1',"
2773                         "interface='org.freedesktop.login1.Session',"
2774                         "member='ResumeDevice',"
2775                         "path='", session->path, "'",
2776                         NULL);
2777         if (!match)
2778                 return -ENOMEM;
2779
2780         r = sd_bus_add_match(session->context->sysbus,
2781                              &cm->slot_resume_device,
2782                              match,
2783                              managed_card_resume_device_fn,
2784                              cm);
2785         if (r < 0)
2786                 return r;
2787
2788         return 0;
2789 }
2790
2791 static int managed_card_take_device_fn(sd_bus *bus,
2792                                        sd_bus_message *reply,
2793                                        void *userdata,
2794                                        sd_bus_error *ret_error) {
2795         managed_card *cm = userdata;
2796         grdev_session *session = cm->card.base.session;
2797         int r, paused, fd;
2798
2799         cm->slot_take_device = sd_bus_slot_unref(cm->slot_take_device);
2800
2801         if (sd_bus_message_is_method_error(reply, NULL)) {
2802                 const sd_bus_error *error = sd_bus_message_get_error(reply);
2803
2804                 log_debug("grdrm: %s/%s: TakeDevice failed: %s: %s",
2805                           session->name, cm->card.base.name, error->name, error->message);
2806                 return 0;
2807         }
2808
2809         cm->acquired = true;
2810
2811         r = sd_bus_message_read(reply, "hb", &fd, &paused);
2812         if (r < 0) {
2813                 log_debug("grdrm: %s/%s: erroneous TakeDevice reply",
2814                           session->name, cm->card.base.name);
2815                 return 0;
2816         }
2817
2818         fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
2819         if (fd < 0) {
2820                 log_debug("grdrm: %s/%s: cannot duplicate fd: %m",
2821                           session->name, cm->card.base.name);
2822                 return 0;
2823         }
2824
2825         r = grdrm_card_open(&cm->card, fd);
2826         if (r < 0) {
2827                 log_debug("grdrm: %s/%s: cannot open: %s",
2828                           session->name, cm->card.base.name, strerror(-r));
2829                 return 0;
2830         }
2831
2832         if (!paused && cm->card.base.enabled)
2833                 grdrm_card_enable(&cm->card);
2834
2835         return 0;
2836 }
2837
2838 static void managed_card_take_device(managed_card *cm) {
2839         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2840         grdev_session *session = cm->card.base.session;
2841         int r;
2842
2843         r = sd_bus_message_new_method_call(session->context->sysbus,
2844                                            &m,
2845                                            "org.freedesktop.login1",
2846                                            session->path,
2847                                            "org.freedesktop.login1.Session",
2848                                            "TakeDevice");
2849         if (r < 0)
2850                 goto error;
2851
2852         r = sd_bus_message_append(m, "uu", major(cm->devnum), minor(cm->devnum));
2853         if (r < 0)
2854                 goto error;
2855
2856         r = sd_bus_call_async(session->context->sysbus,
2857                               &cm->slot_take_device,
2858                               m,
2859                               managed_card_take_device_fn,
2860                               cm,
2861                               0);
2862         if (r < 0)
2863                 goto error;
2864
2865         cm->requested = true;
2866         return;
2867
2868 error:
2869         log_debug("grdrm: %s/%s: cannot send TakeDevice request: %s",
2870                   session->name, cm->card.base.name, strerror(-r));
2871 }
2872
2873 static void managed_card_release_device(managed_card *cm) {
2874         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2875         grdev_session *session = cm->card.base.session;
2876         int r;
2877
2878         /*
2879          * If TakeDevice() is pending or was successful, make sure to
2880          * release the device again. We don't care for return-values,
2881          * so send it without waiting or callbacks.
2882          * If a failed TakeDevice() is pending, but someone else took
2883          * the device on the same bus-connection, we might incorrectly
2884          * release their device. This is an unlikely race, though.
2885          * Furthermore, you really shouldn't have two users of the
2886          * controller-API on the same session, on the same devices, *AND* on
2887          * the same bus-connection. So we don't care for that race..
2888          */
2889
2890         grdrm_card_close(&cm->card);
2891         cm->requested = false;
2892
2893         if (!cm->acquired && !cm->slot_take_device)
2894                 return;
2895
2896         cm->slot_take_device = sd_bus_slot_unref(cm->slot_take_device);
2897         cm->acquired = false;
2898
2899         r = sd_bus_message_new_method_call(session->context->sysbus,
2900                                            &m,
2901                                            "org.freedesktop.login1",
2902                                            session->path,
2903                                            "org.freedesktop.login1.Session",
2904                                            "ReleaseDevice");
2905         if (r >= 0) {
2906                 r = sd_bus_message_append(m, "uu", major(cm->devnum), minor(cm->devnum));
2907                 if (r >= 0)
2908                         r = sd_bus_send(session->context->sysbus, m, NULL);
2909         }
2910
2911         if (r < 0 && r != -ENOTCONN)
2912                 log_debug("grdrm: %s/%s: cannot send ReleaseDevice: %s",
2913                           session->name, cm->card.base.name, strerror(-r));
2914 }
2915
2916 static int managed_card_new(grdev_card **out, grdev_session *session, struct udev_device *ud) {
2917         _cleanup_(grdev_card_freep) grdev_card *basecard = NULL;
2918         char name[GRDRM_CARD_NAME_MAX];
2919         managed_card *cm;
2920         dev_t devnum;
2921         int r;
2922
2923         assert_return(session, -EINVAL);
2924         assert_return(session->managed, -EINVAL);
2925         assert_return(session->context->sysbus, -EINVAL);
2926         assert_return(ud, -EINVAL);
2927
2928         devnum = udev_device_get_devnum(ud);
2929         if (devnum == 0)
2930                 return -ENODEV;
2931
2932         grdrm_name(name, devnum);
2933
2934         cm = new0(managed_card, 1);
2935         if (!cm)
2936                 return -ENOMEM;
2937
2938         basecard = &cm->card.base;
2939         cm->card = GRDRM_CARD_INIT(&managed_card_vtable, session);
2940         cm->devnum = devnum;
2941
2942         r = managed_card_setup_bus(cm);
2943         if (r < 0)
2944                 return r;
2945
2946         r = grdrm_card_add(&cm->card, name);
2947         if (r < 0)
2948                 return r;
2949
2950         managed_card_take_device(cm);
2951
2952         if (out)
2953                 *out = basecard;
2954         basecard = NULL;
2955         return 0;
2956 }
2957
2958 static void managed_card_free(grdev_card *basecard) {
2959         managed_card *cm = managed_card_from_base(basecard);
2960
2961         assert(!basecard->enabled);
2962
2963         managed_card_release_device(cm);
2964         cm->slot_resume_device = sd_bus_slot_unref(cm->slot_resume_device);
2965         cm->slot_pause_device = sd_bus_slot_unref(cm->slot_pause_device);
2966         grdrm_card_destroy(&cm->card);
2967         free(cm);
2968 }
2969
2970 static const grdev_card_vtable managed_card_vtable = {
2971         .free                   = managed_card_free,
2972         .enable                 = managed_card_enable,
2973         .disable                = managed_card_disable,
2974         .commit                 = grdrm_card_commit,
2975         .restore                = grdrm_card_restore,
2976 };
2977
2978 /*
2979  * Generic Constructor
2980  * Instead of relying on the caller to choose between managed and unmanaged
2981  * DRM devices, the grdev_drm_new() constructor does that for you (by
2982  * looking at session->managed).
2983  */
2984
2985 bool grdev_is_drm_card(grdev_card *basecard) {
2986         return basecard && (basecard->vtable == &unmanaged_card_vtable ||
2987                             basecard->vtable == &managed_card_vtable);
2988 }
2989
2990 grdev_card *grdev_find_drm_card(grdev_session *session, dev_t devnum) {
2991         char name[GRDRM_CARD_NAME_MAX];
2992
2993         assert_return(session, NULL);
2994         assert_return(devnum != 0, NULL);
2995
2996         grdrm_name(name, devnum);
2997         return grdev_find_card(session, name);
2998 }
2999
3000 int grdev_drm_card_new(grdev_card **out, grdev_session *session, struct udev_device *ud) {
3001         assert_return(session, -EINVAL);
3002         assert_return(ud, -EINVAL);
3003
3004         return session->managed ? managed_card_new(out, session, ud) : unmanaged_card_new(out, session, ud);
3005 }
3006
3007 void grdev_drm_card_hotplug(grdev_card *basecard, struct udev_device *ud) {
3008         const char *p, *action;
3009         grdrm_card *card;
3010         dev_t devnum;
3011
3012         assert(basecard);
3013         assert(grdev_is_drm_card(basecard));
3014         assert(ud);
3015
3016         card = grdrm_card_from_base(basecard);
3017
3018         action = udev_device_get_action(ud);
3019         if (!action || streq(action, "add") || streq(action, "remove")) {
3020                 /* If we get add/remove events on DRM nodes without devnum, we
3021                  * got hotplugged DRM objects so refresh the device. */
3022                 devnum = udev_device_get_devnum(ud);
3023                 if (devnum == 0)
3024                         grdrm_card_hotplug(card);
3025         } else if (streq_ptr(action, "change")) {
3026                 /* A change event with HOTPLUG=1 is sent whenever a connector
3027                  * changed state. Refresh the device to update our state. */
3028                 p = udev_device_get_property_value(ud, "HOTPLUG");
3029                 if (streq_ptr(p, "1"))
3030                         grdrm_card_hotplug(card);
3031         }
3032 }