fix drm driver name
[platform/adaptation/nexell/libtdm-nexell.git] / src / tdm_nexell.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #if HAVE_UDEV
6 #include <libudev.h>
7 #endif
8
9 #include "tdm_nexell.h"
10 #include <tdm_helper.h>
11 #include <tbm_drm_helper.h>
12
13 //#define ENABLE_PP
14 #define TDM_NEXELL_ENABLE_HWC 0 // disable tdm_hwc
15
16 #define TDM_NEXELL_NAME "nexell"
17
18 static tdm_nexell_data *nexell_data;
19
20 #ifdef HAVE_UDEV
21 static struct udev_device *
22 _tdm_find_primary_gpu(void)
23 {
24         struct udev *udev;
25         struct udev_enumerate *e;
26         struct udev_list_entry *entry;
27         const char *path, *id;
28         struct udev_device *device, *drm_device, *pci;
29
30         udev = udev_new();
31         if (!udev) {
32                 TDM_ERR("fail to initialize udev context\n");
33                 return NULL;
34         }
35
36         e = udev_enumerate_new(udev);
37         udev_enumerate_add_match_subsystem(e, "drm");
38         udev_enumerate_add_match_sysname(e, "card[0-9]*");
39
40         udev_enumerate_scan_devices(e);
41         drm_device = NULL;
42         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
43                 path = udev_list_entry_get_name(entry);
44                 device = udev_device_new_from_syspath(udev, path);
45                 if (!device)
46                         continue;
47
48                 pci = udev_device_get_parent_with_subsystem_devtype(device,
49                                 "pci", NULL);
50                 if (pci) {
51                         id = udev_device_get_sysattr_value(pci, "boot_vga");
52                         if (id && !strcmp(id, "1")) {
53                                 if (drm_device)
54                                         udev_device_unref(drm_device);
55                                 drm_device = device;
56                                 break;
57                         }
58                 }
59
60                 if (!drm_device)
61                         drm_device = device;
62                 else
63                         udev_device_unref(device);
64         }
65
66         udev_enumerate_unref(e);
67         return drm_device;
68 }
69
70 static tdm_error
71 _tdm_nexell_udev_fd_handler(int fd, tdm_event_loop_mask mask, void *user_data)
72 {
73         tdm_nexell_data *edata = (tdm_nexell_data*)user_data;
74         struct udev_device *dev;
75         const char *hotplug;
76         struct stat s;
77         dev_t udev_devnum;
78         int ret;
79
80         dev = udev_monitor_receive_device(edata->uevent_monitor);
81         if (!dev) {
82                 TDM_ERR("couldn't receive device");
83                 return TDM_ERROR_OPERATION_FAILED;
84         }
85
86         udev_devnum = udev_device_get_devnum(dev);
87
88         ret = fstat(edata->drm_fd, &s);
89         if (ret == -1) {
90                 TDM_ERR("fstat failed");
91                 return TDM_ERROR_OPERATION_FAILED;
92         }
93
94         hotplug = udev_device_get_property_value(dev, "HOTPLUG");
95
96         if (memcmp(&s.st_rdev, &udev_devnum, sizeof (dev_t)) == 0 &&
97                 hotplug && atoi(hotplug) == 1)
98         {
99                 TDM_INFO("HotPlug");
100                 tdm_nexell_display_update_output_status(edata);
101         }
102
103         udev_device_unref(dev);
104
105         return TDM_ERROR_NONE;
106 }
107
108 static void
109 _tdm_nexell_udev_init(tdm_nexell_data *edata)
110 {
111         struct udev *u = NULL;
112         struct udev_monitor *mon = NULL;
113
114         u = udev_new();
115         if (!u) {
116                 TDM_ERR("couldn't create udev");
117                 goto failed;
118         }
119
120         mon = udev_monitor_new_from_netlink(u, "udev");
121         if (!mon) {
122                 TDM_ERR("couldn't create udev monitor");
123                 goto failed;
124         }
125
126         if (udev_monitor_filter_add_match_subsystem_devtype(mon, "drm", "drm_minor") > 0 ||
127             udev_monitor_enable_receiving(mon) < 0) {
128                 TDM_ERR("add match subsystem failed");
129                 goto failed;
130         }
131
132         edata->uevent_source =
133                 tdm_event_loop_add_fd_handler(edata->dpy, udev_monitor_get_fd(mon),
134                                               TDM_EVENT_LOOP_READABLE,
135                                               _tdm_nexell_udev_fd_handler,
136                                               edata, NULL);
137         if (!edata->uevent_source) {
138                 TDM_ERR("couldn't create udev event source");
139                 goto failed;
140         }
141
142         edata->uevent_monitor = mon;
143
144         TDM_INFO("hotplug monitor created");
145
146         return;
147 failed:
148         if (mon)
149                 udev_monitor_unref(mon);
150         if (u)
151                 udev_unref(u);
152 }
153
154 static void
155 _tdm_nexell_udev_deinit(tdm_nexell_data *edata)
156 {
157         if (edata->uevent_source) {
158                 tdm_event_loop_source_remove(edata->uevent_source);
159                 edata->uevent_source = NULL;
160         }
161
162         if (edata->uevent_monitor) {
163                 struct udev *u = udev_monitor_get_udev(edata->uevent_monitor);
164                 udev_monitor_unref(edata->uevent_monitor);
165                 udev_unref(u);
166                 edata->uevent_monitor = NULL;
167                 TDM_INFO("hotplug monitor destroyed");
168         }
169 }
170 #endif
171
172 static int
173 _tdm_nexell_open_drm(void)
174 {
175         int fd = -1;
176
177         fd = drmOpen(TDM_NEXELL_NAME, NULL);
178         if (fd < 0) {
179                 TDM_WRN("Cannot open '%s' drm", TDM_NEXELL_NAME);
180         }
181
182 #ifdef HAVE_UDEV
183         if (fd < 0) {
184                 struct udev_device *drm_device = NULL;
185                 const char *filename;
186                 TDM_WRN("Cannot open drm device.. search by udev");
187
188                 drm_device = _tdm_find_primary_gpu();
189                 if (drm_device == NULL) {
190                         TDM_ERR("fail to find drm device\n");
191                         goto close_l;
192                 }
193
194                 filename = udev_device_get_devnode(drm_device);
195
196                 fd = open(filename, O_RDWR | O_CLOEXEC);
197                 if (fd < 0)
198                         TDM_ERR("Cannot open drm device(%s)\n", filename);
199
200                 TDM_DBG("open drm device (name:%s, fd:%d)", filename, fd);
201
202                 udev_device_unref(drm_device);
203         }
204 close_l:
205 #endif
206         return fd;
207 }
208
209 void
210 tdm_nexell_deinit(tdm_backend_data *bdata)
211 {
212         if (nexell_data != bdata)
213                 return;
214
215         TDM_INFO("deinit");
216
217 #ifdef HAVE_UDEV
218         _tdm_nexell_udev_deinit(nexell_data);
219 #endif
220
221         tdm_nexell_display_destroy_output_list(nexell_data);
222         tdm_nexell_data_destroy_buffer_list(nexell_data);
223
224         if (nexell_data->plane_res)
225                 drmModeFreePlaneResources(nexell_data->plane_res);
226         if (nexell_data->mode_res)
227                 drmModeFreeResources(nexell_data->mode_res);
228         if (nexell_data->drm_fd >= 0)
229                 close(nexell_data->drm_fd);
230         if (nexell_data->scaler_fd >= 0)
231                 close(nexell_data->scaler_fd);
232
233         free(nexell_data);
234         nexell_data = NULL;
235 }
236
237 tdm_backend_data *
238 tdm_nexell_init(tdm_display *dpy, tdm_error *error)
239 {
240         tdm_func_display nexell_func_display;
241         tdm_func_output nexell_func_output;
242         tdm_func_layer nexell_func_layer;
243         tdm_func_hwc nexell_func_hwc;
244         tdm_func_hwc_window nexell_func_hwc_window;
245 #ifdef ENABLE_PP
246         tdm_func_pp nexell_func_pp;
247 #endif
248         tdm_error ret;
249         char *str;
250
251 #define TDM_CHANGE_DEBUG_LOGLEVEL
252 #ifdef TDM_CHANGE_DEBUG_LOGLEVEL
253         // show DBG log level
254         tdm_log_set_debug_level(4);
255 #endif
256
257         if (!dpy) {
258                 TDM_ERR("display is null");
259                 if (error)
260                         *error = TDM_ERROR_INVALID_PARAMETER;
261                 return NULL;
262         }
263
264         if (nexell_data) {
265                 TDM_ERR("failed: init twice");
266                 if (error)
267                         *error = TDM_ERROR_BAD_REQUEST;
268                 return NULL;
269         }
270
271         nexell_data = calloc(1, sizeof(tdm_nexell_data));
272         if (!nexell_data) {
273                 TDM_ERR("alloc failed");
274                 if (error)
275                         *error = TDM_ERROR_OUT_OF_MEMORY;
276                 return NULL;
277         }
278
279         str = getenv("TDM_HWC");
280         if (str) {
281                 char *end;
282                 nexell_data->hwc_mode = strtol(str, &end, 10);;
283         }
284
285         /* enable the tdm_hwc */
286         nexell_data->hwc_mode = TDM_NEXELL_ENABLE_HWC;
287
288         LIST_INITHEAD(&nexell_data->output_list);
289         LIST_INITHEAD(&nexell_data->buffer_list);
290
291         memset(&nexell_func_display, 0, sizeof(nexell_func_display));
292         nexell_func_display.display_get_capability = nexell_display_get_capability;
293         nexell_func_display.display_get_pp_capability = nexell_display_get_pp_capability;
294         nexell_func_display.display_get_outputs = nexell_display_get_outputs;
295         nexell_func_display.display_get_fd = nexell_display_get_fd;
296         nexell_func_display.display_handle_events = nexell_display_handle_events;
297         nexell_func_display.display_create_pp = nexell_display_create_pp;
298
299         memset(&nexell_func_output, 0, sizeof(nexell_func_output));
300         nexell_func_output.output_get_capability = nexell_output_get_capability;
301         nexell_func_output.output_get_layers = nexell_output_get_layers;
302         nexell_func_output.output_set_property = nexell_output_set_property;
303         nexell_func_output.output_get_property = nexell_output_get_property;
304         nexell_func_output.output_wait_vblank = nexell_output_wait_vblank;
305         nexell_func_output.output_set_vblank_handler = nexell_output_set_vblank_handler;
306         nexell_func_output.output_commit = nexell_output_commit;
307         nexell_func_output.output_set_commit_handler = nexell_output_set_commit_handler;
308         nexell_func_output.output_set_dpms = nexell_output_set_dpms;
309         nexell_func_output.output_get_dpms = nexell_output_get_dpms;
310         nexell_func_output.output_set_mode = nexell_output_set_mode;
311         nexell_func_output.output_get_mode = nexell_output_get_mode;
312         nexell_func_output.output_set_mirror = NULL;
313         nexell_func_output.output_unset_mirror = NULL;
314 #ifdef HAVE_UDEV
315         nexell_func_output.output_set_status_handler = nexell_output_set_status_handler;
316 #endif
317
318         if (nexell_data->hwc_mode) {
319                 nexell_func_output.output_get_hwc = nexell_output_get_hwc;
320
321                 memset(&nexell_func_hwc, 0, sizeof(nexell_func_hwc));
322                 nexell_func_hwc.hwc_create_window = nexell_hwc_create_window;
323                 nexell_func_hwc.hwc_get_video_supported_formats = nexell_hwc_get_video_supported_formats;
324                 nexell_func_hwc.hwc_get_video_available_properties = NULL;
325                 nexell_func_hwc.hwc_get_capabilities = nexell_hwc_get_capabilities;
326                 nexell_func_hwc.hwc_get_available_properties = nexell_hwc_get_available_properties;
327                 nexell_func_hwc.hwc_get_client_target_buffer_queue = nexell_hwc_get_client_target_buffer_queue;
328                 nexell_func_hwc.hwc_set_client_target_buffer = nexell_hwc_set_client_target_buffer;
329                 nexell_func_hwc.hwc_set_client_target_acquire_fence = NULL;
330                 nexell_func_hwc.hwc_validate = nexell_hwc_validate;
331                 nexell_func_hwc.hwc_get_changed_composition_types = nexell_hwc_get_changed_composition_types;
332                 nexell_func_hwc.hwc_accept_validation = nexell_hwc_accept_validation;
333                 nexell_func_hwc.hwc_commit = nexell_hwc_commit;
334                 nexell_func_hwc.hwc_set_commit_handler = nexell_hwc_set_commit_handler;
335                 nexell_func_hwc.hwc_get_commit_fence = NULL;
336                 nexell_func_hwc.hwc_get_release_fences = NULL;
337
338                 memset(&nexell_func_hwc_window, 0, sizeof(nexell_func_hwc_window));
339                 nexell_func_hwc_window.hwc_window_destroy = nexell_hwc_window_destroy;
340                 nexell_func_hwc_window.hwc_window_acquire_buffer_queue = nexell_hwc_window_acquire_buffer_queue;
341                 nexell_func_hwc_window.hwc_window_release_buffer_queue = nexell_hwc_window_release_buffer_queue;
342                 nexell_func_hwc_window.hwc_window_set_composition_type = nexell_hwc_window_set_composition_type;
343                 nexell_func_hwc_window.hwc_window_set_buffer_damage = nexell_hwc_window_set_buffer_damage;
344                 nexell_func_hwc_window.hwc_window_set_info = nexell_hwc_window_set_info;
345                 nexell_func_hwc_window.hwc_window_set_buffer = nexell_hwc_window_set_buffer;
346                 nexell_func_hwc_window.hwc_window_set_property = nexell_hwc_window_set_property;
347                 nexell_func_hwc_window.hwc_window_get_property = nexell_hwc_window_get_property;
348                 nexell_func_hwc_window.hwc_window_get_constraints = nexell_hwc_window_get_constraints;
349                 nexell_func_hwc_window.hwc_window_set_name = nexell_hwc_window_set_name;
350                 nexell_func_hwc_window.hwc_window_set_cursor_image = nexell_hwc_window_set_cursor_image;
351         }
352
353         memset(&nexell_func_layer, 0, sizeof(nexell_func_layer));
354         nexell_func_layer.layer_get_capability = nexell_layer_get_capability;
355         nexell_func_layer.layer_set_property = nexell_layer_set_property;
356         nexell_func_layer.layer_get_property = nexell_layer_get_property;
357         nexell_func_layer.layer_set_info = nexell_layer_set_info;
358         nexell_func_layer.layer_get_info = nexell_layer_get_info;
359         nexell_func_layer.layer_set_buffer = nexell_layer_set_buffer;
360         nexell_func_layer.layer_unset_buffer = nexell_layer_unset_buffer;
361
362 #ifdef ENABLE_PP
363         memset(&nexell_func_pp, 0, sizeof(nexell_func_pp));
364         nexell_func_pp.pp_destroy = nexell_pp_destroy;
365         nexell_func_pp.pp_set_info = nexell_pp_set_info;
366         nexell_func_pp.pp_attach = nexell_pp_attach;
367         nexell_func_pp.pp_commit = nexell_pp_commit;
368         nexell_func_pp.pp_set_done_handler = nexell_pp_set_done_handler;
369 #endif
370
371         ret = tdm_backend_register_func_display(dpy, &nexell_func_display);
372         if (ret != TDM_ERROR_NONE)
373                 goto failed;
374
375         ret = tdm_backend_register_func_output(dpy, &nexell_func_output);
376         if (ret != TDM_ERROR_NONE)
377                 goto failed;
378
379         if (nexell_data->hwc_mode) {
380                 ret = tdm_backend_register_func_hwc(dpy, &nexell_func_hwc);
381                 if (ret != TDM_ERROR_NONE)
382                         goto failed;
383
384                 ret = tdm_backend_register_func_hwc_window(dpy, &nexell_func_hwc_window);
385                 if (ret != TDM_ERROR_NONE)
386                         goto failed;
387         }
388
389         ret = tdm_backend_register_func_layer(dpy, &nexell_func_layer);
390         if (ret != TDM_ERROR_NONE)
391                 goto failed;
392
393 #ifdef ENABLE_PP
394         ret = tdm_backend_register_func_pp(dpy, &nexell_func_pp);
395         if (ret != TDM_ERROR_NONE)
396                 goto failed;
397 #endif
398
399         nexell_data->dpy = dpy;
400
401         /* The drm master fd can be opened by a tbm backend module in
402          * tbm_bufmgr_init() time. In this case, we just get it from tbm.
403          */
404         nexell_data->drm_fd = tbm_drm_helper_get_master_fd();
405         if (nexell_data->drm_fd < 0) {
406                 nexell_data->drm_fd = _tdm_nexell_open_drm();
407
408                 if (nexell_data->drm_fd < 0) {
409                         ret = TDM_ERROR_OPERATION_FAILED;
410                         goto failed;
411                 }
412
413                 tbm_drm_helper_set_tbm_master_fd(nexell_data->drm_fd);
414         }
415
416         TDM_INFO("master fd(%d)", nexell_data->drm_fd);
417
418         nexell_data->scaler_fd = open("/dev/scaler", O_RDWR);
419         if (nexell_data->scaler_fd < 0)
420                 TDM_ERR("scaler open fail. cannot use pp.");
421
422 #ifdef HAVE_UDEV
423         _tdm_nexell_udev_init(nexell_data);
424 #endif
425
426 #if LIBDRM_MAJOR_VERSION >= 2 && LIBDRM_MINOR_VERSION >= 4  && LIBDRM_MICRO_VERSION >= 47
427         if (drmSetClientCap(nexell_data->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) < 0) {
428                 TDM_WRN("Set DRM_CLIENT_CAP_UNIVERSAL_PLANES failed");
429         } else {
430                 TDM_INFO("has universal planes");
431                 nexell_data->has_universal_plane = 1;
432         }
433 #endif
434
435         nexell_data->mode_res = drmModeGetResources(nexell_data->drm_fd);
436         if (!nexell_data->mode_res) {
437                 TDM_ERR("no drm resource: %m");
438                 ret = TDM_ERROR_OPERATION_FAILED;
439                 goto failed;
440         }
441
442         nexell_data->plane_res = drmModeGetPlaneResources(nexell_data->drm_fd);
443         if (!nexell_data->plane_res) {
444                 TDM_ERR("no drm plane resource: %m");
445                 ret = TDM_ERROR_OPERATION_FAILED;
446                 goto failed;
447         }
448
449         if (nexell_data->plane_res->count_planes <= 0) {
450                 TDM_ERR("no drm plane resource");
451                 ret = TDM_ERROR_OPERATION_FAILED;
452                 goto failed;
453         }
454
455         ret = tdm_nexell_display_create_output_list(nexell_data);
456         if (ret != TDM_ERROR_NONE)
457                 goto failed;
458
459         ret = tdm_nexell_display_create_layer_list(nexell_data);
460         if (ret != TDM_ERROR_NONE)
461                 goto failed;
462
463         if (error)
464                 *error = TDM_ERROR_NONE;
465
466         TDM_INFO("init success!");
467
468         return (tdm_backend_data *)nexell_data;
469 failed:
470         if (error)
471                 *error = ret;
472
473         tdm_nexell_deinit(nexell_data);
474
475         TDM_ERR("init failed!");
476         return NULL;
477 }
478
479 tdm_backend_module tdm_backend_module_data = {
480         "drm",
481         "Samsung",
482         TDM_BACKEND_SET_ABI_VERSION(2, 0),
483         tdm_nexell_init,
484         tdm_nexell_deinit
485 };