9 #include "tdm_nexell.h"
10 #include <tdm_helper.h>
11 #include <tbm_drm_helper.h>
14 #define TDM_NEXELL_ENABLE_HWC 0 // disable tdm_hwc
16 #define TDM_NEXELL_NAME "nexell"
18 static tdm_nexell_data *nexell_data;
21 static struct udev_device *
22 _tdm_find_primary_gpu(void)
25 struct udev_enumerate *e;
26 struct udev_list_entry *entry;
27 const char *path, *id;
28 struct udev_device *device, *drm_device, *pci;
32 TDM_ERR("fail to initialize udev context\n");
36 e = udev_enumerate_new(udev);
37 udev_enumerate_add_match_subsystem(e, "drm");
38 udev_enumerate_add_match_sysname(e, "card[0-9]*");
40 udev_enumerate_scan_devices(e);
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);
48 pci = udev_device_get_parent_with_subsystem_devtype(device,
51 id = udev_device_get_sysattr_value(pci, "boot_vga");
52 if (id && !strcmp(id, "1")) {
54 udev_device_unref(drm_device);
63 udev_device_unref(device);
66 udev_enumerate_unref(e);
71 _tdm_nexell_udev_fd_handler(int fd, tdm_event_loop_mask mask, void *user_data)
73 tdm_nexell_data *edata = (tdm_nexell_data*)user_data;
74 struct udev_device *dev;
80 dev = udev_monitor_receive_device(edata->uevent_monitor);
82 TDM_ERR("couldn't receive device");
83 return TDM_ERROR_OPERATION_FAILED;
86 udev_devnum = udev_device_get_devnum(dev);
88 ret = fstat(edata->drm_fd, &s);
90 TDM_ERR("fstat failed");
91 udev_device_unref(dev);
92 return TDM_ERROR_OPERATION_FAILED;
95 hotplug = udev_device_get_property_value(dev, "HOTPLUG");
97 if (memcmp(&s.st_rdev, &udev_devnum, sizeof (dev_t)) == 0 &&
98 hotplug && atoi(hotplug) == 1)
101 tdm_nexell_display_update_output_status(edata);
104 udev_device_unref(dev);
106 return TDM_ERROR_NONE;
110 _tdm_nexell_udev_init(tdm_nexell_data *edata)
112 struct udev *u = NULL;
113 struct udev_monitor *mon = NULL;
117 TDM_ERR("couldn't create udev");
121 mon = udev_monitor_new_from_netlink(u, "udev");
123 TDM_ERR("couldn't create udev monitor");
127 if (udev_monitor_filter_add_match_subsystem_devtype(mon, "drm", "drm_minor") > 0 ||
128 udev_monitor_enable_receiving(mon) < 0) {
129 TDM_ERR("add match subsystem failed");
133 edata->uevent_source =
134 tdm_event_loop_add_fd_handler(edata->dpy, udev_monitor_get_fd(mon),
135 TDM_EVENT_LOOP_READABLE,
136 _tdm_nexell_udev_fd_handler,
138 if (!edata->uevent_source) {
139 TDM_ERR("couldn't create udev event source");
143 edata->uevent_monitor = mon;
145 TDM_INFO("hotplug monitor created");
150 udev_monitor_unref(mon);
156 _tdm_nexell_udev_deinit(tdm_nexell_data *edata)
158 if (edata->uevent_source) {
159 tdm_event_loop_source_remove(edata->uevent_source);
160 edata->uevent_source = NULL;
163 if (edata->uevent_monitor) {
164 struct udev *u = udev_monitor_get_udev(edata->uevent_monitor);
165 udev_monitor_unref(edata->uevent_monitor);
167 edata->uevent_monitor = NULL;
168 TDM_INFO("hotplug monitor destroyed");
174 _tdm_nexell_open_drm(void)
178 fd = drmOpen(TDM_NEXELL_NAME, NULL);
180 TDM_WRN("Cannot open '%s' drm", TDM_NEXELL_NAME);
185 struct udev_device *drm_device = NULL;
186 const char *filename;
187 TDM_WRN("Cannot open drm device.. search by udev");
189 drm_device = _tdm_find_primary_gpu();
190 if (drm_device == NULL) {
191 TDM_ERR("fail to find drm device\n");
195 filename = udev_device_get_devnode(drm_device);
197 fd = open(filename, O_RDWR | O_CLOEXEC);
199 TDM_ERR("Cannot open drm device(%s)\n", filename);
201 TDM_DBG("open drm device (name:%s, fd:%d)", filename, fd);
203 udev_device_unref(drm_device);
211 tdm_nexell_deinit(tdm_backend_data *bdata)
213 if (nexell_data != bdata)
219 _tdm_nexell_udev_deinit(nexell_data);
222 tdm_nexell_display_destroy_output_list(nexell_data);
223 tdm_nexell_data_destroy_buffer_list(nexell_data);
225 if (nexell_data->plane_res)
226 drmModeFreePlaneResources(nexell_data->plane_res);
227 if (nexell_data->mode_res)
228 drmModeFreeResources(nexell_data->mode_res);
229 if (nexell_data->drm_fd >= 0)
230 close(nexell_data->drm_fd);
231 if (nexell_data->scaler_fd >= 0)
232 close(nexell_data->scaler_fd);
239 tdm_nexell_init(tdm_display *dpy, tdm_error *error)
241 tdm_func_display nexell_func_display;
242 tdm_func_output nexell_func_output;
243 tdm_func_layer nexell_func_layer;
244 tdm_func_hwc nexell_func_hwc;
245 tdm_func_hwc_window nexell_func_hwc_window;
247 tdm_func_pp nexell_func_pp;
252 #define TDM_CHANGE_DEBUG_LOGLEVEL
253 #ifdef TDM_CHANGE_DEBUG_LOGLEVEL
254 // show DBG log level
255 tdm_log_set_debug_level(4);
259 TDM_ERR("display is null");
261 *error = TDM_ERROR_INVALID_PARAMETER;
266 TDM_ERR("failed: init twice");
268 *error = TDM_ERROR_BAD_REQUEST;
272 nexell_data = calloc(1, sizeof(tdm_nexell_data));
274 TDM_ERR("alloc failed");
276 *error = TDM_ERROR_OUT_OF_MEMORY;
280 str = getenv("TDM_HWC");
283 nexell_data->hwc_mode = strtol(str, &end, 10);;
286 /* enable the tdm_hwc */
287 nexell_data->hwc_mode = TDM_NEXELL_ENABLE_HWC;
289 LIST_INITHEAD(&nexell_data->output_list);
290 LIST_INITHEAD(&nexell_data->buffer_list);
292 memset(&nexell_func_display, 0, sizeof(nexell_func_display));
293 nexell_func_display.display_get_capability = nexell_display_get_capability;
294 nexell_func_display.display_get_pp_capability = nexell_display_get_pp_capability;
295 nexell_func_display.display_get_outputs = nexell_display_get_outputs;
296 nexell_func_display.display_get_fd = nexell_display_get_fd;
297 nexell_func_display.display_handle_events = nexell_display_handle_events;
298 nexell_func_display.display_create_pp = nexell_display_create_pp;
300 memset(&nexell_func_output, 0, sizeof(nexell_func_output));
301 nexell_func_output.output_get_capability = nexell_output_get_capability;
302 nexell_func_output.output_get_layers = nexell_output_get_layers;
303 nexell_func_output.output_set_property = nexell_output_set_property;
304 nexell_func_output.output_get_property = nexell_output_get_property;
305 nexell_func_output.output_wait_vblank = nexell_output_wait_vblank;
306 nexell_func_output.output_set_vblank_handler = nexell_output_set_vblank_handler;
307 nexell_func_output.output_commit = nexell_output_commit;
308 nexell_func_output.output_set_commit_handler = nexell_output_set_commit_handler;
309 nexell_func_output.output_set_dpms = nexell_output_set_dpms;
310 nexell_func_output.output_get_dpms = nexell_output_get_dpms;
311 nexell_func_output.output_set_mode = nexell_output_set_mode;
312 nexell_func_output.output_get_mode = nexell_output_get_mode;
313 nexell_func_output.output_set_mirror = NULL;
314 nexell_func_output.output_unset_mirror = NULL;
316 nexell_func_output.output_set_status_handler = nexell_output_set_status_handler;
319 if (nexell_data->hwc_mode) {
320 nexell_func_output.output_get_hwc = nexell_output_get_hwc;
322 memset(&nexell_func_hwc, 0, sizeof(nexell_func_hwc));
323 nexell_func_hwc.hwc_create_window = nexell_hwc_create_window;
324 nexell_func_hwc.hwc_get_video_supported_formats = nexell_hwc_get_video_supported_formats;
325 nexell_func_hwc.hwc_get_video_available_properties = NULL;
326 nexell_func_hwc.hwc_get_capabilities = nexell_hwc_get_capabilities;
327 nexell_func_hwc.hwc_get_available_properties = nexell_hwc_get_available_properties;
328 nexell_func_hwc.hwc_get_client_target_buffer_queue = nexell_hwc_get_client_target_buffer_queue;
329 nexell_func_hwc.hwc_set_client_target_buffer = nexell_hwc_set_client_target_buffer;
330 nexell_func_hwc.hwc_set_client_target_acquire_fence = NULL;
331 nexell_func_hwc.hwc_validate = nexell_hwc_validate;
332 nexell_func_hwc.hwc_get_changed_composition_types = nexell_hwc_get_changed_composition_types;
333 nexell_func_hwc.hwc_accept_validation = nexell_hwc_accept_validation;
334 nexell_func_hwc.hwc_commit = nexell_hwc_commit;
335 nexell_func_hwc.hwc_set_commit_handler = nexell_hwc_set_commit_handler;
336 nexell_func_hwc.hwc_get_commit_fence = NULL;
337 nexell_func_hwc.hwc_get_release_fences = NULL;
339 memset(&nexell_func_hwc_window, 0, sizeof(nexell_func_hwc_window));
340 nexell_func_hwc_window.hwc_window_destroy = nexell_hwc_window_destroy;
341 nexell_func_hwc_window.hwc_window_acquire_buffer_queue = nexell_hwc_window_acquire_buffer_queue;
342 nexell_func_hwc_window.hwc_window_release_buffer_queue = nexell_hwc_window_release_buffer_queue;
343 nexell_func_hwc_window.hwc_window_set_composition_type = nexell_hwc_window_set_composition_type;
344 nexell_func_hwc_window.hwc_window_set_buffer_damage = nexell_hwc_window_set_buffer_damage;
345 nexell_func_hwc_window.hwc_window_set_info = nexell_hwc_window_set_info;
346 nexell_func_hwc_window.hwc_window_set_buffer = nexell_hwc_window_set_buffer;
347 nexell_func_hwc_window.hwc_window_set_property = nexell_hwc_window_set_property;
348 nexell_func_hwc_window.hwc_window_get_property = nexell_hwc_window_get_property;
349 nexell_func_hwc_window.hwc_window_get_constraints = nexell_hwc_window_get_constraints;
350 nexell_func_hwc_window.hwc_window_set_name = nexell_hwc_window_set_name;
351 nexell_func_hwc_window.hwc_window_set_cursor_image = nexell_hwc_window_set_cursor_image;
354 memset(&nexell_func_layer, 0, sizeof(nexell_func_layer));
355 nexell_func_layer.layer_get_capability = nexell_layer_get_capability;
356 nexell_func_layer.layer_set_property = nexell_layer_set_property;
357 nexell_func_layer.layer_get_property = nexell_layer_get_property;
358 nexell_func_layer.layer_set_info = nexell_layer_set_info;
359 nexell_func_layer.layer_get_info = nexell_layer_get_info;
360 nexell_func_layer.layer_set_buffer = nexell_layer_set_buffer;
361 nexell_func_layer.layer_unset_buffer = nexell_layer_unset_buffer;
364 memset(&nexell_func_pp, 0, sizeof(nexell_func_pp));
365 nexell_func_pp.pp_destroy = nexell_pp_destroy;
366 nexell_func_pp.pp_set_info = nexell_pp_set_info;
367 nexell_func_pp.pp_attach = nexell_pp_attach;
368 nexell_func_pp.pp_commit = nexell_pp_commit;
369 nexell_func_pp.pp_set_done_handler = nexell_pp_set_done_handler;
372 ret = tdm_backend_register_func_display(dpy, &nexell_func_display);
373 if (ret != TDM_ERROR_NONE)
376 ret = tdm_backend_register_func_output(dpy, &nexell_func_output);
377 if (ret != TDM_ERROR_NONE)
380 if (nexell_data->hwc_mode) {
381 ret = tdm_backend_register_func_hwc(dpy, &nexell_func_hwc);
382 if (ret != TDM_ERROR_NONE)
385 ret = tdm_backend_register_func_hwc_window(dpy, &nexell_func_hwc_window);
386 if (ret != TDM_ERROR_NONE)
390 ret = tdm_backend_register_func_layer(dpy, &nexell_func_layer);
391 if (ret != TDM_ERROR_NONE)
395 ret = tdm_backend_register_func_pp(dpy, &nexell_func_pp);
396 if (ret != TDM_ERROR_NONE)
400 nexell_data->dpy = dpy;
402 /* The drm master fd can be opened by a tbm backend module in
403 * tbm_bufmgr_init() time. In this case, we just get it from tbm.
405 nexell_data->drm_fd = tbm_drm_helper_get_master_fd();
406 if (nexell_data->drm_fd < 0) {
407 nexell_data->drm_fd = _tdm_nexell_open_drm();
409 if (nexell_data->drm_fd < 0) {
410 ret = TDM_ERROR_OPERATION_FAILED;
414 tbm_drm_helper_set_tbm_master_fd(nexell_data->drm_fd);
417 TDM_INFO("master fd(%d)", nexell_data->drm_fd);
419 nexell_data->scaler_fd = open("/dev/scaler", O_RDWR);
420 if (nexell_data->scaler_fd < 0)
421 TDM_ERR("scaler open fail. cannot use pp.");
424 _tdm_nexell_udev_init(nexell_data);
427 #if LIBDRM_MAJOR_VERSION >= 2 && LIBDRM_MINOR_VERSION >= 4 && LIBDRM_MICRO_VERSION >= 47
428 if (drmSetClientCap(nexell_data->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) < 0) {
429 TDM_WRN("Set DRM_CLIENT_CAP_UNIVERSAL_PLANES failed");
431 TDM_INFO("has universal planes");
432 nexell_data->has_universal_plane = 1;
436 nexell_data->mode_res = drmModeGetResources(nexell_data->drm_fd);
437 if (!nexell_data->mode_res) {
438 TDM_ERR("no drm resource: %m");
439 ret = TDM_ERROR_OPERATION_FAILED;
443 nexell_data->plane_res = drmModeGetPlaneResources(nexell_data->drm_fd);
444 if (!nexell_data->plane_res) {
445 TDM_ERR("no drm plane resource: %m");
446 ret = TDM_ERROR_OPERATION_FAILED;
450 if (nexell_data->plane_res->count_planes <= 0) {
451 TDM_ERR("no drm plane resource");
452 ret = TDM_ERROR_OPERATION_FAILED;
456 ret = tdm_nexell_display_create_output_list(nexell_data);
457 if (ret != TDM_ERROR_NONE)
460 ret = tdm_nexell_display_create_layer_list(nexell_data);
461 if (ret != TDM_ERROR_NONE)
465 *error = TDM_ERROR_NONE;
467 TDM_INFO("init success!");
469 return (tdm_backend_data *)nexell_data;
474 tdm_nexell_deinit(nexell_data);
476 TDM_ERR("init failed!");
480 tdm_backend_module tdm_backend_module_data = {
483 TDM_BACKEND_SET_ABI_VERSION(2, 0),