1/* swc: drm.c
2 *
3 * Copyright (c) 2013-2020 Michael Forney
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#include "drm.h"
25#include "dmabuf.h"
26#include "event.h"
27#include "internal.h"
28#include "launch.h"
29#include "output.h"
30#include "plane.h"
31#include "screen.h"
32#include "util.h"
33#include "wayland_buffer.h"
34
35#include "wayland-drm-server-protocol.h"
36#include <dirent.h>
37#include <drm.h>
38#include <errno.h>
39#include <fcntl.h>
40#include <limits.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <strings.h>
45#include <unistd.h>
46#include <wayland-server.h>
47#include <wld/drm.h>
48#include <wld/wld.h>
49#include <xf86drm.h>
50
51struct swc_drm swc_drm;
52
53static struct {
54 char *path;
55
56 struct wl_global *global;
57 struct wl_global *dmabuf;
58 struct wl_event_source *event_source;
59} drm;
60
61static void
62authenticate(struct wl_client *client, struct wl_resource *resource,
63 uint32_t magic)
64{
65 wl_drm_send_authenticated(resource);
66}
67
68static void
69create_buffer(struct wl_client *client, struct wl_resource *resource,
70 uint32_t id, uint32_t name, int32_t width, int32_t height,
71 uint32_t stride, uint32_t format)
72{
73 wl_resource_post_error(
74 resource, WL_DRM_ERROR_INVALID_NAME,
75 "GEM names are not supported, use a PRIME fd instead");
76}
77
78static void
79create_planar_buffer(struct wl_client *client, struct wl_resource *resource,
80 uint32_t id, uint32_t name, int32_t width, int32_t height,
81 uint32_t format, int32_t offset0, int32_t stride0,
82 int32_t offset1, int32_t stride1, int32_t offset2,
83 int32_t stride2)
84{
85 wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_FORMAT,
86 "planar buffers are not supported\n");
87}
88
89static void
90create_prime_buffer(struct wl_client *client, struct wl_resource *resource,
91 uint32_t id, int32_t fd, int32_t width, int32_t height,
92 uint32_t format, int32_t offset0, int32_t stride0,
93 int32_t offset1, int32_t stride1, int32_t offset2,
94 int32_t stride2)
95{
96 struct wld_buffer *buffer;
97 struct wl_resource *buffer_resource;
98 union wld_object object = {.i = fd};
99
100 buffer = wld_import_buffer(swc.drm->context, WLD_DRM_OBJECT_PRIME_FD,
101 object, width, height, format, stride0);
102 close(fd);
103
104 if (!buffer) {
105 goto error0;
106 }
107
108 buffer_resource = wayland_buffer_create_resource(
109 client, wl_resource_get_version(resource), id, buffer);
110
111 if (!buffer_resource) {
112 goto error1;
113 }
114
115 return;
116
117error1:
118 wld_buffer_unreference(buffer);
119error0:
120 wl_resource_post_no_memory(resource);
121}
122
123static const struct wl_drm_interface drm_impl = {
124 .authenticate = authenticate,
125 .create_buffer = create_buffer,
126 .create_planar_buffer = create_planar_buffer,
127 .create_prime_buffer = create_prime_buffer,
128};
129
130static int
131select_card(const struct dirent *entry)
132{
133 unsigned num;
134 return sscanf(entry->d_name, "card%u", &num) == 1;
135}
136
137static bool
138find_primary_drm_device(char *path, size_t size)
139{
140 struct dirent **cards, *card = NULL;
141 int num_cards, ret;
142 unsigned index;
143 FILE *file;
144 unsigned char boot_vga;
145
146 num_cards = scandir("/dev/dri", &cards, &select_card, &alphasort);
147
148 if (num_cards == -1) {
149 return false;
150 }
151
152 for (index = 0; index < num_cards; ++index) {
153 snprintf(path, size, "/sys/class/drm/%s/device/boot_vga",
154 cards[index]->d_name);
155
156 if ((file = fopen(path, "r"))) {
157 ret = fscanf(file, "%hhu", &boot_vga);
158 fclose(file);
159
160 if (ret == 1 && boot_vga) {
161 free(card);
162 card = cards[index];
163 DEBUG("/dev/dri/%s is the primary GPU\n", card->d_name);
164 break;
165 }
166 }
167
168 if (!card) {
169 card = cards[index];
170 } else {
171 free(cards[index]);
172 }
173 }
174
175 free(cards);
176
177 if (!card) {
178 return false;
179 }
180
181 if (snprintf(path, size, "/dev/dri/%s", card->d_name) >= size) {
182 return false;
183 }
184
185 free(card);
186 return true;
187}
188
189static bool
190find_available_crtc(drmModeRes *resources, drmModeConnector *connector,
191 uint32_t taken_crtcs, int *crtc_index)
192{
193 int i, j;
194 uint32_t possible_crtcs;
195 drmModeEncoder *encoder;
196
197 for (i = 0; i < connector->count_encoders; ++i) {
198 encoder = drmModeGetEncoder(swc.drm->fd, connector->encoders[i]);
199 possible_crtcs = encoder->possible_crtcs;
200 drmModeFreeEncoder(encoder);
201
202 for (j = 0; j < resources->count_crtcs; ++j) {
203 if ((possible_crtcs & (1 << j)) && !(taken_crtcs & (1 << j))) {
204 *crtc_index = j;
205 return true;
206 }
207 }
208 }
209
210 return false;
211}
212
213static void
214handle_vblank(int fd, unsigned int sequence, unsigned int sec,
215 unsigned int usec, void *data)
216{
217}
218
219static void
220handle_page_flip(int fd, unsigned int sequence, unsigned int sec,
221 unsigned int usec, unsigned int crtc_id, void *data)
222{
223 struct drm_handler *handler = data;
224
225 handler->page_flip(handler, sec * 1000 + usec / 1000);
226}
227
228static drmEventContext event_context = {
229 .version = DRM_EVENT_CONTEXT_VERSION,
230 .vblank_handler = handle_vblank,
231 .page_flip_handler2 = handle_page_flip,
232};
233
234static int
235handle_data(int fd, uint32_t mask, void *data)
236{
237 drmHandleEvent(fd, &event_context);
238 return 1;
239}
240
241static void
242bind_drm(struct wl_client *client, void *data, uint32_t version, uint32_t id)
243{
244 struct wl_resource *resource;
245
246 resource = wl_resource_create(client, &wl_drm_interface, version, id);
247 if (!resource) {
248 wl_client_post_no_memory(client);
249 return;
250 }
251 wl_resource_set_implementation(resource, &drm_impl, NULL, NULL);
252
253 if (version >= 2) {
254 wl_drm_send_capabilities(resource, WL_DRM_CAPABILITY_PRIME);
255 }
256
257 wl_drm_send_device(resource, drm.path);
258 wl_drm_send_format(resource, WL_DRM_FORMAT_XRGB8888);
259 wl_drm_send_format(resource, WL_DRM_FORMAT_ARGB8888);
260}
261
262bool
263drm_initialize(void)
264{
265 uint64_t val;
266 char primary[PATH_MAX];
267
268 if (!find_primary_drm_device(primary, sizeof(primary))) {
269 ERROR("Could not find DRM device\n");
270 goto error0;
271 }
272
273 swc.drm->fd = launch_open_device(primary, O_RDWR | O_CLOEXEC);
274 if (swc.drm->fd == -1) {
275 ERROR("Could not open DRM device at %s\n", primary);
276 goto error0;
277 }
278 if (drmSetClientCap(swc.drm->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) < 0) {
279 ERROR("Could not enable DRM universal planes\n");
280 goto error1;
281 }
282 if (drmGetCap(swc.drm->fd, DRM_CAP_CURSOR_WIDTH, &val) < 0) {
283 val = 64;
284 }
285 swc.drm->cursor_w = val;
286 if (drmGetCap(swc.drm->fd, DRM_CAP_CURSOR_HEIGHT, &val) < 0) {
287 val = 64;
288 }
289 swc.drm->cursor_h = val;
290
291 drm.path = drmGetRenderDeviceNameFromFd(swc.drm->fd);
292 if (!drm.path) {
293 ERROR("Could not determine render node path\n");
294 goto error1;
295 }
296
297 if (!(swc.drm->context = wld_drm_create_context(swc.drm->fd))) {
298 ERROR("Could not create WLD DRM context\n");
299 goto error1;
300 }
301
302 if (!(swc.drm->renderer = wld_create_renderer(swc.drm->context))) {
303 ERROR("Could not create WLD DRM renderer\n");
304 goto error2;
305 }
306
307 drm.event_source = wl_event_loop_add_fd(
308 swc.event_loop, swc.drm->fd, WL_EVENT_READABLE, &handle_data, NULL);
309
310 if (!drm.event_source) {
311 ERROR("Could not create DRM event source\n");
312 goto error3;
313 }
314
315 if (!wld_drm_is_dumb(swc.drm->context)) {
316 drm.global = wl_global_create(swc.display, &wl_drm_interface, 2, NULL,
317 &bind_drm);
318 if (!drm.global) {
319 ERROR("Could not create wl_drm global\n");
320 goto error4;
321 }
322
323 drm.dmabuf = swc_dmabuf_create(swc.display);
324 if (!drm.dmabuf) {
325 WARNING("Could not create wp_linux_dmabuf global\n");
326 }
327 }
328
329 return true;
330
331error4:
332 wl_event_source_remove(drm.event_source);
333error3:
334 wld_destroy_renderer(swc.drm->renderer);
335error2:
336 wld_destroy_context(swc.drm->context);
337error1:
338 close(swc.drm->fd);
339error0:
340 return false;
341}
342
343void
344drm_finalize(void)
345{
346 if (drm.global) {
347 wl_global_destroy(drm.global);
348 }
349 wl_event_source_remove(drm.event_source);
350 wld_destroy_renderer(swc.drm->renderer);
351 wld_destroy_context(swc.drm->context);
352 free(drm.path);
353 close(swc.drm->fd);
354}
355
356bool
357drm_create_screens(struct wl_list *screens)
358{
359 drmModePlaneRes *plane_ids;
360 drmModeRes *resources;
361 drmModeConnector *connector;
362 struct plane *plane, *cursor_plane;
363 struct output *output;
364 uint32_t i, taken_crtcs = 0;
365 struct wl_list planes;
366
367 plane_ids = drmModeGetPlaneResources(swc.drm->fd);
368 if (!plane_ids) {
369 ERROR("Could not get DRM plane resources\n");
370 return false;
371 }
372 wl_list_init(&planes);
373 for (i = 0; i < plane_ids->count_planes; ++i) {
374 plane = plane_new(plane_ids->planes[i]);
375 if (plane) {
376 wl_list_insert(&planes, &plane->link);
377 }
378 }
379 drmModeFreePlaneResources(plane_ids);
380
381 resources = drmModeGetResources(swc.drm->fd);
382 if (!resources) {
383 ERROR("Could not get DRM resources\n");
384 return false;
385 }
386 for (i = 0; i < resources->count_connectors;
387 ++i, drmModeFreeConnector(connector)) {
388 connector = drmModeGetConnector(swc.drm->fd, resources->connectors[i]);
389
390 if (connector->connection == DRM_MODE_CONNECTED) {
391 int crtc_index;
392
393 if (!find_available_crtc(resources, connector, taken_crtcs,
394 &crtc_index)) {
395 WARNING("Could not find CRTC for connector %d\n", i);
396 continue;
397 }
398
399 cursor_plane = NULL;
400 wl_list_for_each(plane, &planes, link)
401 {
402 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
403 plane->possible_crtcs & 1 << crtc_index) {
404 wl_list_remove(&plane->link);
405 cursor_plane = plane;
406 break;
407 }
408 }
409 if (!cursor_plane) {
410 WARNING("Could not find cursor plane for CRTC %d\n",
411 crtc_index);
412 }
413
414 if (!(output = output_new(connector))) {
415 continue;
416 }
417
418 output->screen =
419 screen_new(resources->crtcs[crtc_index], output, cursor_plane);
420 output->screen->id = crtc_index;
421 taken_crtcs |= 1 << crtc_index;
422
423 wl_list_insert(screens, &output->screen->link);
424 }
425 }
426 drmModeFreeResources(resources);
427
428 return true;
429}
430
431enum { WLD_USER_OBJECT_FRAMEBUFFER = WLD_USER_ID };
432
433struct framebuffer {
434 struct wld_exporter exporter;
435 struct wld_destructor destructor;
436 uint32_t id;
437};
438
439static bool
440framebuffer_export(struct wld_exporter *exporter, struct wld_buffer *buffer,
441 uint32_t type, union wld_object *object)
442{
443 struct framebuffer *framebuffer =
444 wl_container_of(exporter, framebuffer, exporter);
445
446 switch (type) {
447 case WLD_USER_OBJECT_FRAMEBUFFER:
448 object->u32 = framebuffer->id;
449 break;
450 default:
451 return false;
452 }
453
454 return true;
455}
456
457static void
458framebuffer_destroy(struct wld_destructor *destructor)
459{
460 struct framebuffer *framebuffer =
461 wl_container_of(destructor, framebuffer, destructor);
462
463 drmModeRmFB(swc.drm->fd, framebuffer->id);
464 free(framebuffer);
465}
466
467uint32_t
468drm_get_framebuffer(struct wld_buffer *buffer)
469{
470 struct framebuffer *framebuffer;
471 union wld_object object;
472 int ret;
473
474 if (!buffer) {
475 return 0;
476 }
477
478 if (wld_export(buffer, WLD_USER_OBJECT_FRAMEBUFFER, &object)) {
479 return object.u32;
480 }
481
482 if (!wld_export(buffer, WLD_DRM_OBJECT_HANDLE, &object)) {
483 ERROR("Could not get buffer handle\n");
484 return 0;
485 }
486
487 if (!(framebuffer = malloc(sizeof(*framebuffer)))) {
488 return 0;
489 }
490
491 ret = drmModeAddFB2(swc.drm->fd, buffer->width, buffer->height,
492 buffer->format, (uint32_t[4]){object.u32},
493 (uint32_t[4]){buffer->pitch}, (uint32_t[4]){0},
494 &framebuffer->id, 0);
495 if (ret < 0) {
496 free(framebuffer);
497 return 0;
498 }
499
500 framebuffer->exporter.export = &framebuffer_export;
501 wld_buffer_add_exporter(buffer, &framebuffer->exporter);
502 framebuffer->destructor.destroy = &framebuffer_destroy;
503 wld_buffer_add_destructor(buffer, &framebuffer->destructor);
504
505 return framebuffer->id;
506}