1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/**************************************************************************
libtdm_vc4
Copyright 2017 Samsung Electronics co., Ltd. All Rights Reserved.
Contact: SooChan Lim <sc1.lim@samsung.com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sub license, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice (including the
next paragraph) shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_UDEV
#include <libudev.h>
#endif
#include "tdm_backend_vc4.h"
#if HAVE_UDEV
#include <libudev.h>
#endif
#define TDM_DRM_NAME "vc4"
#ifdef HAVE_UDEV
static struct udev_device *
_tdm_find_primary_gpu(void)
{
struct udev *udev;
struct udev_enumerate *e;
struct udev_list_entry *entry;
const char *path, *id;
struct udev_device *device, *drm_device, *pci;
udev = udev_new();
if (!udev) {
TDM_BACKEND_ERR("fail to initialize udev context\n");
return NULL;
}
e = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(e, "drm");
udev_enumerate_add_match_sysname(e, "card[0-9]*");
udev_enumerate_scan_devices(e);
drm_device = NULL;
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
path = udev_list_entry_get_name(entry);
device = udev_device_new_from_syspath(udev, path);
if (!device)
continue;
pci = udev_device_get_parent_with_subsystem_devtype(device, "pci", NULL);
if (pci) {
id = udev_device_get_sysattr_value(pci, "boot_vga");
if (id && !strcmp(id, "1")) {
if (drm_device)
udev_device_unref(drm_device);
drm_device = device;
break;
}
}
if (!drm_device)
drm_device = device;
else
udev_device_unref(device);
}
udev_enumerate_unref(e);
return drm_device;
}
static hal_tdm_error
_tdm_vc4_udev_fd_handler(int fd, hal_tdm_event_loop_mask mask, void *user_data)
{
tdm_vc4_display *display_data = (tdm_vc4_display*)user_data;
struct udev_device *dev;
const char *hotplug;
struct stat s;
dev_t udev_devnum;
int ret;
dev = udev_monitor_receive_device(display_data->uevent_monitor);
if (!dev) {
TDM_BACKEND_ERR("couldn't receive device");
return HAL_TDM_ERROR_OPERATION_FAILED;
}
udev_devnum = udev_device_get_devnum(dev);
ret = fstat(display_data->drm_fd, &s);
if (ret == -1) {
TDM_BACKEND_ERR("fstat failed");
udev_device_unref(dev);
return HAL_TDM_ERROR_OPERATION_FAILED;
}
hotplug = udev_device_get_property_value(dev, "HOTPLUG");
if (memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)) == 0 &&
hotplug && atoi(hotplug) == 1) {
TDM_BACKEND_INFO("HotPlug");
tdm_vc4_display_update_output_status(display_data);
}
udev_device_unref(dev);
return HAL_TDM_ERROR_NONE;
}
static struct udev_monitor *
_tdm_vc4_create_udev_monitor()
{
struct udev *u = NULL;
struct udev_monitor *mon = NULL;
u = udev_new();
if (!u) {
TDM_BACKEND_ERR("couldn't create udev");
goto failed;
}
mon = udev_monitor_new_from_netlink(u, "udev");
if (!mon) {
TDM_BACKEND_ERR("couldn't create udev monitor");
goto failed;
}
if (udev_monitor_filter_add_match_subsystem_devtype(mon, "drm", "drm_minor") > 0 ||
udev_monitor_enable_receiving(mon) < 0) {
TDM_BACKEND_ERR("add match subsystem failed");
goto failed;
}
TDM_BACKEND_INFO("hotplug monitor created");
return mon;
failed:
if (mon)
udev_monitor_unref(mon);
if (u)
udev_unref(u);
return NULL;
}
static void
_tdm_vc4_destroy_udev_monitor(struct udev_monitor *mon)
{
struct udev *u;
if (!mon)
return;
u = udev_monitor_get_udev(mon);
udev_monitor_unref(mon);
udev_unref(u);
TDM_BACKEND_INFO("hotplug monitor destroyed");
}
#endif
static int
_tdm_vc4_open_drm(void)
{
int fd = -1;
fd = drmOpen(TDM_DRM_NAME, NULL);
if (fd < 0)
TDM_BACKEND_WRN("Cannot open '%s' drm", TDM_DRM_NAME);
#ifdef HAVE_UDEV
if (fd < 0) {
struct udev_device *drm_device = NULL;
const char *filename;
TDM_BACKEND_WRN("Cannot open drm device.. search by udev");
drm_device = _tdm_find_primary_gpu();
if (drm_device == NULL) {
TDM_BACKEND_ERR("fail to find drm device\n");
goto close_l;
}
filename = udev_device_get_devnode(drm_device);
fd = open(filename, O_RDWR | O_CLOEXEC);
if (fd < 0)
TDM_BACKEND_ERR("Cannot open drm device(%s)\n", filename);
TDM_BACKEND_DBG("open drm device (name:%s, fd:%d)", filename, fd);
udev_device_unref(drm_device);
}
close_l:
#endif
return fd;
}
static void
_tdm_vc4_display_deinitialize(tdm_vc4_display *display_data)
{
tdm_vc4_display_destroy_output_list(display_data);
tdm_vc4_display_destroy_buffer_list(display_data);
if (display_data->plane_res)
drmModeFreePlaneResources(display_data->plane_res);
if (display_data->mode_res)
drmModeFreeResources(display_data->mode_res);
}
static hal_tdm_error
_tdm_vc4_display_initialize(tdm_vc4_display *display_data)
{
hal_tdm_error ret;
#if LIBDRM_MAJOR_VERSION >= 2 && LIBDRM_MINOR_VERSION >= 4 && LIBDRM_MICRO_VERSION >= 47
if (drmSetClientCap(display_data->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) < 0) {
TDM_BACKEND_WRN("Set DRM_CLIENT_CAP_UNIVERSAL_PLANES failed");
} else {
TDM_BACKEND_INFO("has universal planes");
display_data->has_universal_plane = 1;
}
if (drmSetClientCap(display_data->drm_fd, DRM_CLIENT_CAP_ATOMIC, 1) < 0) {
TDM_BACKEND_WRN("Set DRM_CLIENT_CAP_ATOMIC failed");
} else {
TDM_BACKEND_INFO("has atomic");
display_data->has_atomic = 1;
}
#endif
display_data->mode_res = drmModeGetResources(display_data->drm_fd);
if (!display_data->mode_res) {
TDM_BACKEND_ERR("no drm resource: %m");
ret = HAL_TDM_ERROR_OPERATION_FAILED;
goto failed;
}
display_data->plane_res = drmModeGetPlaneResources(display_data->drm_fd);
if (!display_data->plane_res) {
TDM_BACKEND_ERR("no drm plane resource: %m");
ret = HAL_TDM_ERROR_OPERATION_FAILED;
goto failed;
}
if (display_data->plane_res->count_planes <= 0) {
TDM_BACKEND_ERR("no drm plane resource");
ret = HAL_TDM_ERROR_OPERATION_FAILED;
goto failed;
}
ret = tdm_vc4_display_create_output_list(display_data);
if (ret != HAL_TDM_ERROR_NONE)
goto failed;
ret = tdm_vc4_display_create_layer_list(display_data);
if (ret != HAL_TDM_ERROR_NONE)
goto failed;
return ret;
failed:
_tdm_vc4_display_deinitialize(display_data);
return ret;
}
static hal_tdm_error
_tdm_vc4_master_drm_fd_handler(hal_tdm_fd master_drm_fd, void *user_data)
{
tdm_vc4_display *display_data = (tdm_vc4_display *) user_data;
hal_tdm_error ret;
TDM_BACKEND_RETURN_VAL_IF_FAIL(display_data != NULL, HAL_TDM_ERROR_INVALID_PARAMETER);
display_data->drm_fd = master_drm_fd;
TDM_BACKEND_INFO("Get the master drm_fd(%d)!\n", display_data->drm_fd);
// initialize display with a master drm_fd
ret = _tdm_vc4_display_initialize(display_data);
if (ret != HAL_TDM_ERROR_NONE) {
TDM_BACKEND_ERR("fail to _tdm_vc4_display_initialize!\n");
return HAL_TDM_ERROR_OPERATION_FAILED;
}
return HAL_TDM_ERROR_NONE;
}
int
hal_backend_tdm_vc4_exit(void *data)
{
hal_tdm_backend_data *backend_data = (hal_tdm_backend_data *)data;
tdm_vc4_display *display_data;
TDM_BACKEND_INFO("deinit");
TDM_BACKEND_RETURN_VAL_IF_FAIL(backend_data != NULL, -1);
display_data = (tdm_vc4_display *)backend_data->display;
TDM_BACKEND_RETURN_VAL_IF_FAIL(display_data != NULL, -1);
_tdm_vc4_display_deinitialize(display_data);
#ifdef HAVE_UDEV
if (display_data->uevent_monitor)
_tdm_vc4_destroy_udev_monitor(display_data->uevent_monitor);
#endif
if (display_data->drm_fd >= 0)
close(display_data->drm_fd);
free(backend_data->display);
backend_data->display = NULL;
return HAL_TDM_ERROR_NONE;
}
static int
hal_backend_tdm_vc4_init(void **data)
{
hal_tdm_backend_data *backend_data = NULL;
tdm_vc4_display *display_data = NULL;
hal_tdm_error ret;
int drm_fd;
#ifdef HAVE_UDEV
static struct udev_monitor *mon;
hal_tdm_event_source *udev_event_source;
#endif
if (!data) {
TDM_BACKEND_ERR("data is NULL");
return -1;
}
backend_data = *(hal_tdm_backend_data **)data;
if (!backend_data) {
TDM_BACKEND_ERR("backend_data is NULL");
return -1;
}
/* allocate a hal_tdm_display */
display_data = calloc(1, sizeof(struct _tdm_vc4_display));
if (!display_data) {
TDM_BACKEND_ERR("fail to alloc display_data!\n");
goto failed;
}
backend_data->display = (hal_tdm_display *)display_data;
LIST_INITHEAD(&display_data->output_list);
LIST_INITHEAD(&display_data->buffer_list);
// check if drm_fd is master fd.
drm_fd = _tdm_vc4_open_drm();
if (drm_fd < 0) {
ret = HAL_TDM_ERROR_OPERATION_FAILED;
goto failed;
}
// set true when backend has a drm_device.
backend_data->has_drm_device = 1;
if (drmIsMaster(drm_fd)) {
// drm_fd is a master drm_fd.
backend_data->drm_info.drm_fd = drm_fd;
backend_data->drm_info.is_master = 1;
display_data->drm_fd = drm_fd;
TDM_BACKEND_INFO("Get the master drm_fd(%d)!\n", display_data->drm_fd);
// initialize display with a master drm_fd
ret = _tdm_vc4_display_initialize(display_data);
if (ret != HAL_TDM_ERROR_NONE) {
TDM_BACKEND_ERR("fail to _tdm_vc4_display_initialize!\n");
goto failed;
}
} else {
// drm_fd is not a master drm_fd.
// request a master drm_fd
close(drm_fd);
backend_data->drm_info.drm_fd = -1;
backend_data->drm_info.is_master = 0;
backend_data->drm_info.master_drm_fd_func = _tdm_vc4_master_drm_fd_handler;
backend_data->drm_info.user_data = display_data;
TDM_BACKEND_INFO("A backend requests an master drm_fd.\n");
}
#ifdef HAVE_UDEV
mon = _tdm_vc4_create_udev_monitor();
if (!mon) {
ret = HAL_TDM_ERROR_OPERATION_FAILED;
goto failed;
}
display_data->uevent_monitor = mon;
/* alloc and register udev_event_source */
udev_event_source = calloc(1, sizeof(struct _hal_tdm_event_source));
if (!udev_event_source) {
TDM_BACKEND_ERR("fail to alloc udev_event_source!\n");
goto failed;
}
udev_event_source->event_fd = udev_monitor_get_fd(mon);
udev_event_source->func = _tdm_vc4_udev_fd_handler;
udev_event_source->user_data = display_data;
backend_data->event_sources[0] = udev_event_source;
backend_data->num_event_sources++;
#endif
backend_data->display_funcs->display_get_capability = vc4_display_get_capability;
backend_data->display_funcs->display_get_outputs = vc4_display_get_outputs;
backend_data->display_funcs->display_get_fd = vc4_display_get_fd;
backend_data->display_funcs->display_handle_events = vc4_display_handle_events;
backend_data->output_funcs->output_get_capability = vc4_output_get_capability;
backend_data->output_funcs->output_set_property = vc4_output_set_property;
backend_data->output_funcs->output_get_property = vc4_output_get_property;
backend_data->output_funcs->output_wait_vblank = vc4_output_wait_vblank;
backend_data->output_funcs->output_set_vblank_handler = vc4_output_set_vblank_handler;
backend_data->output_funcs->output_set_dpms = vc4_output_set_dpms;
backend_data->output_funcs->output_get_dpms = vc4_output_get_dpms;
backend_data->output_funcs->output_set_mode = vc4_output_set_mode;
backend_data->output_funcs->output_get_mode = vc4_output_get_mode;
backend_data->output_funcs->output_set_mirror = vc4_output_set_mirror;
backend_data->output_funcs->output_unset_mirror = vc4_output_unset_mirror;
#ifdef HAVE_UDEV
backend_data->output_funcs->output_set_status_handler = vc4_output_set_status_handler;
#endif
backend_data->output_funcs->output_get_hwc = vc4_output_get_hwc;
backend_data->hwc_funcs->hwc_create_window = vc4_hwc_create_window;
backend_data->hwc_funcs->hwc_get_video_supported_formats = vc4_hwc_get_video_supported_formats;
backend_data->hwc_funcs->hwc_get_video_available_properties = NULL;
backend_data->hwc_funcs->hwc_get_capabilities = vc4_hwc_get_capabilities;
backend_data->hwc_funcs->hwc_get_available_properties = vc4_hwc_get_available_properties;
backend_data->hwc_funcs->hwc_get_client_target_buffer_queue = vc4_hwc_get_client_target_buffer_queue;
backend_data->hwc_funcs->hwc_set_client_target_buffer = vc4_hwc_set_client_target_buffer;
backend_data->hwc_funcs->hwc_set_client_target_buffer_info = vc4_hwc_set_client_target_buffer_info;
backend_data->hwc_funcs->hwc_set_client_target_acquire_fence = vc4_hwc_set_client_target_acquire_fence;
backend_data->hwc_funcs->hwc_validate = vc4_hwc_validate;
backend_data->hwc_funcs->hwc_get_changed_composition_types = vc4_hwc_get_changed_composition_types;
backend_data->hwc_funcs->hwc_accept_validation = vc4_hwc_accept_validation;
backend_data->hwc_funcs->hwc_commit = vc4_hwc_commit;
backend_data->hwc_funcs->hwc_set_commit_handler = vc4_hwc_set_commit_handler;
backend_data->hwc_funcs->hwc_get_commit_fence = vc4_hwc_get_commit_fence;
backend_data->hwc_funcs->hwc_get_release_fences = vc4_hwc_get_release_fences;
backend_data->hwc_window_funcs->hwc_window_destroy = vc4_hwc_window_destroy;
backend_data->hwc_window_funcs->hwc_window_acquire_buffer_queue = NULL; // no need
backend_data->hwc_window_funcs->hwc_window_release_buffer_queue = NULL; // no need
backend_data->hwc_window_funcs->hwc_window_set_composition_type = vc4_hwc_window_set_composition_type;
backend_data->hwc_window_funcs->hwc_window_set_buffer_damage = vc4_hwc_window_set_buffer_damage;
backend_data->hwc_window_funcs->hwc_window_set_info = vc4_hwc_window_set_info;
backend_data->hwc_window_funcs->hwc_window_set_buffer = vc4_hwc_window_set_buffer;
backend_data->hwc_window_funcs->hwc_window_set_property = vc4_hwc_window_set_property;
backend_data->hwc_window_funcs->hwc_window_get_property = vc4_hwc_window_get_property;
backend_data->hwc_window_funcs->hwc_window_get_constraints = vc4_hwc_window_get_constraints;
backend_data->hwc_window_funcs->hwc_window_set_name = vc4_hwc_window_set_name;
backend_data->hwc_window_funcs->hwc_window_set_cursor_image = vc4_hwc_window_set_cursor_image;
backend_data->hwc_window_funcs->hwc_window_set_acquire_fence = vc4_hwc_window_set_acquire_fence;
TDM_BACKEND_INFO("init success!");
return HAL_TDM_ERROR_NONE;
failed:
TDM_BACKEND_ERR("init failed!");
hal_backend_tdm_vc4_exit((void *)backend_data);
return -1;
}
hal_backend hal_backend_tdm_data = {
.name = "vc4",
.vendor = "Samsung",
.init = hal_backend_tdm_vc4_init,
.exit = hal_backend_tdm_vc4_exit,
.major_version = 1,
.minor_version = 0,
};
|