Add 'optional' property to connector configuration
[src/drm-lease-manager.git] / drm-lease-manager / lease-manager.c
1 /* Copyright 2020-2021 IGEL Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #define _GNU_SOURCE
17 #include "lease-manager.h"
18
19 #include "drm-lease.h"
20 #include "log.h"
21
22 #include <assert.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <poll.h>
26 #include <pthread.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <sys/stat.h>
32 #include <sys/sysmacros.h>
33 #include <unistd.h>
34 #include <xf86drm.h>
35 #include <xf86drmMode.h>
36
37 /* Number of resources, to be included in a DRM lease for each connector.
38  * Each connector needs both a CRTC and conector object:. */
39 #define DRM_OBJECTS_PER_CONNECTOR (2)
40
41 #define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0]))
42
43 struct lease {
44         struct lease_handle base;
45
46         bool is_granted;
47         uint32_t lessee_id;
48         int lease_fd;
49
50         uint32_t *object_ids;
51         int nobject_ids;
52
53         /* for lease transfer completion */
54         uint32_t crtc_id;
55         pthread_t transition_tid;
56         bool transition_running;
57 };
58
59 struct lm {
60         int drm_fd;
61         dev_t dev_id;
62
63         drmModeResPtr drm_resource;
64         drmModePlaneResPtr drm_plane_resource;
65         uint32_t available_crtcs;
66
67         char **connector_names;
68         int nconnectors;
69
70         struct lease **leases;
71         int nleases;
72 };
73
74 static const char *const connector_type_names[] = {
75     [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
76     [DRM_MODE_CONNECTOR_VGA] = "VGA",
77     [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
78     [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
79     [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
80     [DRM_MODE_CONNECTOR_Composite] = "Composite",
81     [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
82     [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
83     [DRM_MODE_CONNECTOR_Component] = "Component",
84     [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
85     [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
86     [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
87     [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
88     [DRM_MODE_CONNECTOR_TV] = "TV",
89     [DRM_MODE_CONNECTOR_eDP] = "eDP",
90     [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
91     [DRM_MODE_CONNECTOR_DSI] = "DSI",
92     [DRM_MODE_CONNECTOR_DPI] = "DPI",
93     [DRM_MODE_CONNECTOR_WRITEBACK] = "Writeback",
94 };
95
96 static char *drm_create_connector_name(drmModeConnectorPtr connector)
97 {
98         uint32_t type = connector->connector_type;
99         uint32_t id = connector->connector_type_id;
100
101         if (type >= ARRAY_LENGTH(connector_type_names))
102                 type = DRM_MODE_CONNECTOR_Unknown;
103
104         /* If the type is "Unknown", use the connector_id as the identify to
105          * guarantee that the name will be unique. */
106         if (type == DRM_MODE_CONNECTOR_Unknown)
107                 id = connector->connector_id;
108
109         char *name;
110         if (asprintf(&name, "%s-%d", connector_type_names[type], id) < 0)
111                 return NULL;
112
113         return name;
114 }
115
116 static char *drm_create_default_lease_name(struct lm *lm, int cindex)
117 {
118         char *connector_name = lm->connector_names[cindex];
119
120         char *name;
121         if (asprintf(&name, "card%d-%s", minor(lm->dev_id), connector_name) < 0)
122                 return NULL;
123
124         return name;
125 }
126
127 static int drm_get_encoder_crtc_index(struct lm *lm, drmModeEncoderPtr encoder)
128 {
129         uint32_t crtc_id = encoder->crtc_id;
130         if (!crtc_id)
131                 return -1;
132
133         // The CRTC index only makes sense if it is less than the number of
134         // bits in the encoder possible_crtcs bitmap, which is 32.
135         assert(lm->drm_resource->count_crtcs < 32);
136
137         for (int i = 0; i < lm->drm_resource->count_crtcs; i++) {
138                 if (lm->drm_resource->crtcs[i] == crtc_id)
139                         return i;
140         }
141         return -1;
142 }
143
144 static int drm_get_active_crtc_index(struct lm *lm,
145                                      drmModeConnectorPtr connector)
146 {
147         drmModeEncoder *encoder =
148             drmModeGetEncoder(lm->drm_fd, connector->encoder_id);
149         if (!encoder)
150                 return -1;
151
152         int crtc_idx = drm_get_encoder_crtc_index(lm, encoder);
153         drmModeFreeEncoder(encoder);
154         return crtc_idx;
155 }
156
157 static int drm_get_crtc_index(struct lm *lm, drmModeConnectorPtr connector)
158 {
159
160         // try the active CRTC first
161         int crtc_index = drm_get_active_crtc_index(lm, connector);
162         if (crtc_index != -1)
163                 return crtc_index;
164
165         // If not try the first available CRTC on the connector/encoder
166         for (int i = 0; i < connector->count_encoders; i++) {
167                 drmModeEncoder *encoder =
168                     drmModeGetEncoder(lm->drm_fd, connector->encoders[i]);
169
170                 assert(encoder);
171
172                 uint32_t usable_crtcs =
173                     lm->available_crtcs & encoder->possible_crtcs;
174                 int crtc = ffs(usable_crtcs);
175                 drmModeFreeEncoder(encoder);
176                 if (crtc == 0)
177                         continue;
178                 crtc_index = crtc - 1;
179                 lm->available_crtcs &= ~(1 << crtc_index);
180                 break;
181         }
182         return crtc_index;
183 }
184
185 static void drm_find_available_crtcs(struct lm *lm)
186 {
187         // Assume all CRTCS are available by default,
188         lm->available_crtcs = ~0;
189
190         // then remove any that are in use. */
191         for (int i = 0; i < lm->drm_resource->count_encoders; i++) {
192                 int enc_id = lm->drm_resource->encoders[i];
193                 drmModeEncoderPtr enc = drmModeGetEncoder(lm->drm_fd, enc_id);
194                 if (!enc)
195                         continue;
196
197                 int crtc_idx = drm_get_encoder_crtc_index(lm, enc);
198                 if (crtc_idx >= 0)
199                         lm->available_crtcs &= ~(1 << crtc_idx);
200
201                 drmModeFreeEncoder(enc);
202         }
203 }
204
205 static bool drm_find_connector(struct lm *lm, char *name, uint32_t *id)
206 {
207         int connectors = lm->drm_resource->count_connectors;
208
209         for (int i = 0; i < connectors; i++) {
210                 if (strcmp(lm->connector_names[i], name))
211                         continue;
212                 if (id)
213                         *id = lm->drm_resource->connectors[i];
214                 return true;
215         }
216         return false;
217 }
218
219 static bool lease_add_planes(struct lm *lm, struct lease *lease, int crtc_index)
220 {
221         for (uint32_t i = 0; i < lm->drm_plane_resource->count_planes; i++) {
222                 uint32_t plane_id = lm->drm_plane_resource->planes[i];
223                 drmModePlanePtr plane = drmModeGetPlane(lm->drm_fd, plane_id);
224
225                 assert(plane);
226
227                 // Exclude planes that can be used with multiple CRTCs for now
228                 if (plane->possible_crtcs == (1u << crtc_index)) {
229                         lease->object_ids[lease->nobject_ids++] = plane_id;
230                 }
231                 drmModeFreePlane(plane);
232         }
233         return true;
234 }
235
236 /* Lease transition
237  * Wait for a client to update the DRM framebuffer on the CRTC managed by
238  * a lease.  Once the framebuffer has been updated, it is safe to close
239  * the fd associated with the previous lease client, freeing the previous
240  * framebuffer if there are no other references to it. */
241 static void wait_for_fb_update(struct lease *lease, uint32_t old_fb)
242 {
243         uint32_t current_fb = old_fb;
244
245         struct pollfd drm_poll = {
246             .fd = lease->lease_fd,
247             .events = POLLIN,
248         };
249
250         while (current_fb == old_fb) {
251                 drmModeCrtcPtr crtc;
252                 if (poll(&drm_poll, 1, -1) < 0) {
253                         if (errno == EINTR)
254                                 continue;
255                         break;
256                 }
257
258                 crtc = drmModeGetCrtc(lease->lease_fd, lease->crtc_id);
259                 current_fb = crtc->buffer_id;
260                 drmModeFreeCrtc(crtc);
261         }
262 }
263
264 struct transition_ctx {
265         struct lease *lease;
266         int close_fd;
267         uint32_t old_fb;
268 };
269
270 static void transition_done(void *arg)
271 {
272         struct transition_ctx *ctx = arg;
273         close(ctx->close_fd);
274         free(ctx);
275 }
276
277 static void *finish_transition_task(void *arg)
278 {
279         struct transition_ctx *ctx = arg;
280         pthread_cleanup_push(transition_done, ctx);
281         wait_for_fb_update(ctx->lease, ctx->old_fb);
282         pthread_cleanup_pop(true);
283         return NULL;
284 }
285
286 static void close_after_lease_transition(struct lease *lease, int close_fd)
287 {
288         struct transition_ctx *ctx = calloc(1, sizeof(*ctx));
289
290         assert(ctx);
291
292         drmModeCrtcPtr crtc = drmModeGetCrtc(lease->lease_fd, lease->crtc_id);
293
294         ctx->lease = lease;
295         ctx->close_fd = close_fd;
296         ctx->old_fb = crtc->buffer_id;
297
298         drmModeFreeCrtc(crtc);
299
300         int ret = pthread_create(&lease->transition_tid, NULL,
301                                  finish_transition_task, ctx);
302
303         lease->transition_running = (ret == 0);
304 }
305
306 static void cancel_lease_transition_thread(struct lease *lease)
307 {
308
309         if (lease->transition_running) {
310                 pthread_cancel(lease->transition_tid);
311                 pthread_join(lease->transition_tid, NULL);
312         }
313
314         lease->transition_running = false;
315 }
316
317 static void lease_free(struct lease *lease)
318 {
319         free(lease->base.name);
320         free(lease->object_ids);
321         free(lease);
322 }
323
324 static struct lease *lease_create(struct lm *lm,
325                                   const struct lease_config *config)
326 {
327         struct lease *lease;
328
329         if (!config->lease_name) {
330                 ERROR_LOG("Mising lease name\n");
331                 return NULL;
332         }
333
334         lease = calloc(1, sizeof(struct lease));
335         if (!lease) {
336                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
337                 return NULL;
338         }
339
340         lease->base.name = strdup(config->lease_name);
341         if (!lease->base.name) {
342                 DEBUG_LOG("Can't create lease name: %s\n", strerror(errno));
343                 goto err;
344         }
345
346         int nconnectors =
347             config->nconnectors > 0 ? config->nconnectors : config->ncids;
348         int nobjects = lm->drm_plane_resource->count_planes +
349                        nconnectors * DRM_OBJECTS_PER_CONNECTOR;
350
351         lease->object_ids = calloc(nobjects, sizeof(uint32_t));
352         if (!lease->object_ids) {
353                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
354                 goto err;
355         }
356
357         for (int i = 0; i < nconnectors; i++) {
358                 uint32_t cid;
359                 struct connector_config *con_config = NULL;
360
361                 if (config->nconnectors > 0)
362                         con_config = &config->connectors[i];
363
364                 if (con_config) {
365                         char *connector_name = con_config->name;
366                         bool optional = con_config->optional;
367
368                         bool found =
369                             drm_find_connector(lm, connector_name, &cid);
370
371                         bool missing_mandatory = !found && !optional;
372                         bool missing_optional = !found && optional;
373
374                         if (missing_mandatory) {
375                                 ERROR_LOG("Lease: %s, "
376                                           "mandatory connector %s not found\n",
377                                           config->lease_name, connector_name);
378                                 goto err;
379                         } else if (missing_optional) {
380                                 WARN_LOG("Lease: %s, "
381                                          "unknown DRM connector: %s\n",
382                                          config->lease_name, connector_name);
383                                 continue;
384                         }
385                 } else {
386                         cid = config->connector_ids[i];
387                 }
388
389                 drmModeConnectorPtr connector =
390                     drmModeGetConnector(lm->drm_fd, cid);
391
392                 if (connector == NULL) {
393                         ERROR_LOG("Can't find connector id: %d\n", cid);
394                         goto err;
395                 }
396
397                 int crtc_index = drm_get_crtc_index(lm, connector);
398
399                 drmModeFreeConnector(connector);
400
401                 if (crtc_index < 0) {
402                         DEBUG_LOG("No crtc found for connector: %d, lease %s\n",
403                                   cid, lease->base.name);
404                         goto err;
405                 }
406
407                 if (!lease_add_planes(lm, lease, crtc_index))
408                         goto err;
409
410                 uint32_t crtc_id = lm->drm_resource->crtcs[crtc_index];
411                 lease->crtc_id = crtc_id;
412                 lease->object_ids[lease->nobject_ids++] = crtc_id;
413                 lease->object_ids[lease->nobject_ids++] = cid;
414         }
415         lease->is_granted = false;
416         lease->lease_fd = -1;
417
418         return lease;
419
420 err:
421         lease_free(lease);
422         return NULL;
423 }
424
425 static void destroy_default_lease_configs(int num_configs,
426                                           struct lease_config *configs)
427 {
428         for (int i = 0; i < num_configs; i++) {
429                 free(configs[i].connector_ids);
430                 free(configs[i].lease_name);
431         }
432
433         free(configs);
434 }
435
436 static int create_default_lease_configs(struct lm *lm,
437                                         struct lease_config **configs)
438 {
439         struct lease_config *def_configs;
440         int num_configs = lm->drm_resource->count_connectors;
441
442         if (num_configs < 0)
443                 return -1;
444
445         def_configs = calloc(num_configs, sizeof(*def_configs));
446         if (!def_configs) {
447                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
448                 return -1;
449         }
450
451         for (int i = 0; i < num_configs; i++) {
452                 uint32_t cid = lm->drm_resource->connectors[i];
453
454                 def_configs[i].connector_ids = malloc(sizeof(uint32_t));
455                 if (!def_configs[i].connector_ids) {
456                         DEBUG_LOG("Memory allocation failed: %s\n",
457                                   strerror(errno));
458                         goto err;
459                 }
460
461                 def_configs[i].lease_name =
462                     drm_create_default_lease_name(lm, i);
463
464                 def_configs[i].connector_ids[0] = cid;
465                 def_configs[i].ncids = 1;
466         }
467
468         *configs = def_configs;
469         return num_configs;
470
471 err:
472         destroy_default_lease_configs(num_configs, def_configs);
473         return -1;
474 }
475
476 static struct lm *drm_device_get_resources(const char *device)
477 {
478         struct lm *lm = calloc(1, sizeof(struct lm));
479         if (!lm) {
480                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
481                 return NULL;
482         }
483         lm->drm_fd = open(device, O_RDWR);
484         if (lm->drm_fd < 0) {
485                 ERROR_LOG("Cannot open DRM device (%s): %s\n", device,
486                           strerror(errno));
487                 goto err;
488         }
489
490         lm->drm_resource = drmModeGetResources(lm->drm_fd);
491         if (!lm->drm_resource) {
492                 ERROR_LOG("Invalid DRM device(%s)\n", device);
493                 DEBUG_LOG("drmModeGetResources failed: %s\n", strerror(errno));
494                 goto err;
495         }
496
497         lm->drm_plane_resource = drmModeGetPlaneResources(lm->drm_fd);
498         if (!lm->drm_plane_resource) {
499                 DEBUG_LOG("drmModeGetPlaneResources failed: %s\n",
500                           strerror(errno));
501                 goto err;
502         }
503
504         struct stat st;
505         if (fstat(lm->drm_fd, &st) < 0 || !S_ISCHR(st.st_mode)) {
506                 DEBUG_LOG("%s is not a valid device file\n", device);
507                 goto err;
508         }
509
510         lm->dev_id = st.st_rdev;
511
512         lm->nconnectors = lm->drm_resource->count_connectors;
513         lm->connector_names = calloc(lm->nconnectors, sizeof(char *));
514
515         for (int i = 0; i < lm->nconnectors; i++) {
516                 drmModeConnectorPtr connector;
517                 uint32_t cid = lm->drm_resource->connectors[i];
518
519                 connector = drmModeGetConnector(lm->drm_fd, cid);
520                 lm->connector_names[i] = drm_create_connector_name(connector);
521                 drmModeFreeConnector(connector);
522
523                 if (!lm->connector_names[i]) {
524                         DEBUG_LOG("Can't create name for connector %d: %s\n",
525                                   cid, strerror(errno));
526                         goto err;
527                 }
528         }
529         return lm;
530 err:
531         lm_destroy(lm);
532         return NULL;
533 }
534
535 static struct lm *drm_find_drm_device(const char *device)
536 {
537         drmDevicePtr devices[64];
538         int ndevs;
539         struct lm *lm = NULL;
540
541         if (device)
542                 return drm_device_get_resources(device);
543
544         ndevs = drmGetDevices2(0, devices, 64);
545
546         for (int i = 0; i < ndevs; i++) {
547                 lm = drm_device_get_resources(
548                     devices[i]->nodes[DRM_NODE_PRIMARY]);
549                 if (lm)
550                         break;
551         }
552
553         drmFreeDevices(devices, ndevs);
554
555         return lm;
556 }
557
558 static int lm_create_leases(struct lm *lm, int num_leases,
559                             const struct lease_config *configs)
560 {
561         lm->leases = calloc(num_leases, sizeof(struct lease *));
562         if (!lm->leases) {
563                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
564                 return -1;
565         }
566
567         drm_find_available_crtcs(lm);
568
569         for (int i = 0; i < num_leases; i++) {
570                 struct lease *lease = lease_create(lm, &configs[i]);
571                 if (!lease)
572                         continue;
573
574                 lm->leases[lm->nleases] = lease;
575                 lm->nleases++;
576         }
577         if (lm->nleases == 0)
578                 return -1;
579
580         return 0;
581 }
582
583 struct lm *lm_create_with_config(const char *device, int num_leases,
584                                  struct lease_config *configs)
585 {
586         struct lease_config *default_configs = NULL;
587         struct lm *lm = drm_find_drm_device(device);
588
589         if (!lm) {
590                 ERROR_LOG("No available DRM device found\n");
591                 return NULL;
592         }
593
594         if (configs == NULL || num_leases == 0) {
595                 num_leases = create_default_lease_configs(lm, &default_configs);
596                 if (num_leases < 0) {
597                         lm_destroy(lm);
598                         ERROR_LOG("DRM connector enumeration failed\n");
599                         return NULL;
600                 }
601                 configs = default_configs;
602         }
603
604         if (lm_create_leases(lm, num_leases, configs) < 0) {
605                 lm_destroy(lm);
606                 lm = NULL;
607         }
608
609         if (default_configs)
610                 destroy_default_lease_configs(num_leases, default_configs);
611         return lm;
612 }
613
614 struct lm *lm_create(const char *device)
615 {
616         return lm_create_with_config(device, 0, NULL);
617 }
618
619 void lm_destroy(struct lm *lm)
620 {
621         assert(lm);
622
623         for (int i = 0; i < lm->nleases; i++) {
624                 struct lease_handle *lease_handle = &lm->leases[i]->base;
625                 lm_lease_revoke(lm, lease_handle);
626                 lm_lease_close(lease_handle);
627                 lease_free(lm->leases[i]);
628         }
629
630         free(lm->leases);
631
632         for (int i = 0; i < lm->nconnectors; i++)
633                 free(lm->connector_names[i]);
634         free(lm->connector_names);
635
636         drmModeFreeResources(lm->drm_resource);
637         drmModeFreePlaneResources(lm->drm_plane_resource);
638         close(lm->drm_fd);
639         free(lm);
640 }
641
642 int lm_get_lease_handles(struct lm *lm, struct lease_handle ***handles)
643 {
644         assert(lm);
645         assert(handles);
646
647         *handles = (struct lease_handle **)lm->leases;
648         return lm->nleases;
649 }
650
651 int lm_lease_grant(struct lm *lm, struct lease_handle *handle)
652 {
653         assert(lm);
654         assert(handle);
655
656         struct lease *lease = (struct lease *)handle;
657         if (lease->is_granted) {
658                 /* Lease is already claimed */
659                 return -1;
660         }
661
662         int lease_fd =
663             drmModeCreateLease(lm->drm_fd, lease->object_ids,
664                                lease->nobject_ids, 0, &lease->lessee_id);
665         if (lease_fd < 0) {
666                 ERROR_LOG("drmModeCreateLease failed on lease %s: %s\n",
667                           lease->base.name, strerror(errno));
668                 return -1;
669         }
670
671         lease->is_granted = true;
672
673         int old_lease_fd = lease->lease_fd;
674         lease->lease_fd = lease_fd;
675
676         if (old_lease_fd >= 0)
677                 close_after_lease_transition(lease, old_lease_fd);
678
679         return lease_fd;
680 }
681
682 int lm_lease_transfer(struct lm *lm, struct lease_handle *handle)
683 {
684         assert(lm);
685         assert(handle);
686
687         struct lease *lease = (struct lease *)handle;
688         if (!lease->is_granted)
689                 return -1;
690
691         lm_lease_revoke(lm, handle);
692         if (lm_lease_grant(lm, handle) < 0) {
693                 lm_lease_close(handle);
694                 return -1;
695         }
696
697         return lease->lease_fd;
698 }
699
700 void lm_lease_revoke(struct lm *lm, struct lease_handle *handle)
701 {
702         assert(lm);
703         assert(handle);
704
705         struct lease *lease = (struct lease *)handle;
706
707         if (!lease->is_granted)
708                 return;
709
710         drmModeRevokeLease(lm->drm_fd, lease->lessee_id);
711         cancel_lease_transition_thread(lease);
712         lease->is_granted = false;
713 }
714
715 void lm_lease_close(struct lease_handle *handle)
716 {
717         assert(handle);
718
719         struct lease *lease = (struct lease *)handle;
720         if (lease->lease_fd >= 0)
721                 close(lease->lease_fd);
722         lease->lease_fd = -1;
723 }