2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author:Mark Yao <mark.yao@rock-chips.com>
5 * based on exynos_drm_drv.c
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_crtc_helper.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_sync_helper.h>
22 #include <drm/rockchip_drm.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/dma-iommu.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/memblock.h>
27 #include <linux/module.h>
28 #include <linux/of_address.h>
29 #include <linux/of_graph.h>
30 #include <linux/component.h>
31 #include <linux/fence.h>
32 #include <linux/console.h>
33 #include <linux/iommu.h>
35 #include <drm/rockchip_drm.h>
37 #include "rockchip_drm_drv.h"
38 #include "rockchip_drm_fb.h"
39 #include "rockchip_drm_fbdev.h"
40 #include "rockchip_drm_gem.h"
41 #include "rockchip_drm_rga.h"
43 #define DRIVER_NAME "rockchip"
44 #define DRIVER_DESC "RockChip Soc DRM"
45 #define DRIVER_DATE "20140818"
46 #define DRIVER_MAJOR 1
47 #define DRIVER_MINOR 0
49 static bool is_support_iommu = true;
51 static LIST_HEAD(rockchip_drm_subdrv_list);
52 static DEFINE_MUTEX(subdrv_list_mutex);
53 static struct drm_driver rockchip_drm_driver;
55 struct rockchip_drm_mode_set {
56 struct list_head head;
57 struct drm_framebuffer *fb;
58 struct drm_connector *connector;
59 struct drm_crtc *crtc;
60 struct drm_display_mode *mode;
71 static struct drm_crtc *find_crtc_by_node(struct drm_device *drm_dev,
72 struct device_node *node)
74 struct device_node *np_crtc;
75 struct drm_crtc *crtc;
77 np_crtc = of_get_parent(node);
78 if (!np_crtc || !of_device_is_available(np_crtc))
81 drm_for_each_crtc(crtc, drm_dev) {
82 if (crtc->port == np_crtc)
89 static struct drm_connector *find_connector_by_node(struct drm_device *drm_dev,
90 struct device_node *node)
92 struct device_node *np_connector;
93 struct drm_connector *connector;
95 np_connector = of_graph_get_remote_port_parent(node);
96 if (!np_connector || !of_device_is_available(np_connector))
99 drm_for_each_connector(connector, drm_dev) {
100 if (connector->port == np_connector)
107 void rockchip_free_loader_memory(struct drm_device *drm)
109 struct rockchip_drm_private *private = drm->dev_private;
110 struct rockchip_logo *logo;
113 if (!private || !private->logo || --private->logo->count)
116 logo = private->logo;
117 start = phys_to_virt(logo->start);
118 end = phys_to_virt(logo->size);
120 if (private->domain) {
121 iommu_unmap(private->domain, logo->dma_addr,
122 logo->iommu_map_size);
123 drm_mm_remove_node(&logo->mm);
125 dma_unmap_sg(drm->dev, logo->sgt->sgl,
126 logo->sgt->nents, DMA_TO_DEVICE);
128 sg_free_table(logo->sgt);
129 memblock_free(logo->start, logo->size);
130 free_reserved_area(start, end, -1, "drm_logo");
132 private->logo = NULL;
135 static int init_loader_memory(struct drm_device *drm_dev)
137 struct rockchip_drm_private *private = drm_dev->dev_private;
138 struct rockchip_logo *logo;
139 struct device_node *np = drm_dev->dev->of_node;
140 struct device_node *node;
141 unsigned long nr_pages;
143 struct sg_table *sgt;
144 phys_addr_t start, size;
148 node = of_parse_phandle(np, "memory-region", 0);
152 ret = of_address_to_resource(node, 0, &res);
156 size = resource_size(&res);
160 logo = kmalloc(sizeof(*logo), GFP_KERNEL);
164 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
165 pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL);
169 while (i < nr_pages) {
170 pages[i] = phys_to_page(start);
174 sgt = drm_prime_pages_to_sg(pages, nr_pages);
180 if (private->domain) {
181 memset(&logo->mm, 0, sizeof(logo->mm));
182 ret = drm_mm_insert_node_generic(&private->mm, &logo->mm,
186 DRM_ERROR("out of I/O virtual memory: %d\n", ret);
190 logo->dma_addr = logo->mm.start;
192 logo->iommu_map_size = iommu_map_sg(private->domain,
193 logo->dma_addr, sgt->sgl,
194 sgt->nents, IOMMU_READ);
195 if (logo->iommu_map_size < size) {
196 DRM_ERROR("failed to map buffer");
198 goto err_remove_node;
201 dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, DMA_TO_DEVICE);
202 logo->dma_addr = sg_dma_address(sgt->sgl);
206 logo->start = res.start;
209 private->logo = logo;
214 drm_mm_remove_node(&logo->mm);
223 static struct drm_framebuffer *
224 get_framebuffer_by_node(struct drm_device *drm_dev, struct device_node *node)
226 struct rockchip_drm_private *private = drm_dev->dev_private;
227 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
231 if (WARN_ON(!private->logo))
234 if (of_property_read_u32(node, "logo,offset", &val)) {
235 pr_err("%s: failed to get logo,offset\n", __func__);
238 mode_cmd.offsets[0] = val;
240 if (of_property_read_u32(node, "logo,width", &val)) {
241 pr_err("%s: failed to get logo,width\n", __func__);
244 mode_cmd.width = val;
246 if (of_property_read_u32(node, "logo,height", &val)) {
247 pr_err("%s: failed to get logo,height\n", __func__);
250 mode_cmd.height = val;
252 if (of_property_read_u32(node, "logo,bpp", &val)) {
253 pr_err("%s: failed to get logo,bpp\n", __func__);
258 mode_cmd.pitches[0] = mode_cmd.width * bpp / 8;
262 mode_cmd.pixel_format = DRM_FORMAT_BGR565;
265 mode_cmd.pixel_format = DRM_FORMAT_BGR888;
268 mode_cmd.pixel_format = DRM_FORMAT_XBGR8888;
271 pr_err("%s: unsupport to logo bpp %d\n", __func__, bpp);
275 return rockchip_fb_alloc(drm_dev, &mode_cmd, NULL, private->logo, 1);
278 static struct rockchip_drm_mode_set *
279 of_parse_display_resource(struct drm_device *drm_dev, struct device_node *route)
281 struct rockchip_drm_mode_set *set;
282 struct device_node *connect;
283 struct drm_framebuffer *fb;
284 struct drm_connector *connector;
285 struct drm_crtc *crtc;
289 connect = of_parse_phandle(route, "connect", 0);
293 fb = get_framebuffer_by_node(drm_dev, route);
294 if (IS_ERR_OR_NULL(fb))
297 crtc = find_crtc_by_node(drm_dev, connect);
298 connector = find_connector_by_node(drm_dev, connect);
299 if (!crtc || !connector) {
300 dev_warn(drm_dev->dev,
301 "No available crtc or connector for display");
302 drm_framebuffer_unreference(fb);
306 set = kzalloc(sizeof(*set), GFP_KERNEL);
310 if (!of_property_read_u32(route, "video,hdisplay", &val))
313 if (!of_property_read_u32(route, "video,vdisplay", &val))
316 if (!of_property_read_u32(route, "video,vrefresh", &val))
319 if (!of_property_read_u32(route, "logo,ymirror", &val))
323 if (!of_property_read_string(route, "logo,mode", &string) &&
324 !strcmp(string, "fullscreen"))
329 set->connector = connector;
334 int setup_initial_state(struct drm_device *drm_dev,
335 struct drm_atomic_state *state,
336 struct rockchip_drm_mode_set *set)
338 struct drm_connector *connector = set->connector;
339 struct drm_crtc *crtc = set->crtc;
340 struct drm_crtc_state *crtc_state;
341 struct drm_connector_state *conn_state;
342 struct drm_plane_state *primary_state;
343 struct drm_display_mode *mode = NULL;
344 const struct drm_connector_helper_funcs *funcs;
345 bool is_crtc_enabled = true;
346 int hdisplay, vdisplay;
347 int fb_width, fb_height;
348 int found = 0, match = 0;
352 if (!set->hdisplay || !set->vdisplay || !set->vrefresh)
353 is_crtc_enabled = false;
355 conn_state = drm_atomic_get_connector_state(state, connector);
356 if (IS_ERR(conn_state))
357 return PTR_ERR(conn_state);
359 funcs = connector->helper_private;
360 conn_state->best_encoder = funcs->best_encoder(connector);
361 if (funcs->loader_protect)
362 funcs->loader_protect(connector, true);
363 connector->loader_protect = true;
364 num_modes = connector->funcs->fill_modes(connector, 4096, 4096);
366 dev_err(drm_dev->dev, "connector[%s] can't found any modes\n",
372 list_for_each_entry(mode, &connector->modes, head) {
373 if (mode->hdisplay == set->hdisplay &&
374 mode->vdisplay == set->vdisplay &&
375 drm_mode_vrefresh(mode) == set->vrefresh) {
383 list_for_each_entry(mode, &connector->modes, head) {
384 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
391 mode = list_first_entry_or_null(&connector->modes,
392 struct drm_display_mode,
395 pr_err("failed to find available modes\n");
403 crtc_state = drm_atomic_get_crtc_state(state, crtc);
404 if (IS_ERR(crtc_state)) {
405 ret = PTR_ERR(crtc_state);
409 drm_mode_copy(&crtc_state->adjusted_mode, mode);
410 if (!match || !is_crtc_enabled) {
411 set->mode_changed = true;
413 ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
417 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
421 crtc_state->active = true;
428 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
429 if (IS_ERR(primary_state)) {
430 ret = PTR_ERR(primary_state);
434 hdisplay = mode->hdisplay;
435 vdisplay = mode->vdisplay;
436 fb_width = set->fb->width;
437 fb_height = set->fb->height;
439 primary_state->crtc = crtc;
440 primary_state->src_x = 0;
441 primary_state->src_y = 0;
442 primary_state->src_w = fb_width << 16;
443 primary_state->src_h = fb_height << 16;
445 if (set->fb->width >= hdisplay) {
446 primary_state->crtc_x = 0;
447 primary_state->crtc_w = hdisplay;
449 primary_state->crtc_x = (hdisplay - fb_width) / 2;
450 primary_state->crtc_w = set->fb->width;
453 if (set->fb->height >= vdisplay) {
454 primary_state->crtc_y = 0;
455 primary_state->crtc_h = vdisplay;
457 primary_state->crtc_y = (vdisplay - fb_height) / 2;
458 primary_state->crtc_h = fb_height;
461 primary_state->crtc_x = 0;
462 primary_state->crtc_y = 0;
463 primary_state->crtc_w = hdisplay;
464 primary_state->crtc_h = vdisplay;
470 if (funcs->loader_protect)
471 funcs->loader_protect(connector, false);
472 connector->loader_protect = false;
476 static int update_state(struct drm_device *drm_dev,
477 struct drm_atomic_state *state,
478 struct rockchip_drm_mode_set *set,
479 unsigned int *plane_mask)
481 struct drm_mode_config *mode_config = &drm_dev->mode_config;
482 struct drm_crtc *crtc = set->crtc;
483 struct drm_connector *connector = set->connector;
484 struct drm_display_mode *mode = set->mode;
485 struct drm_plane_state *primary_state;
486 struct drm_crtc_state *crtc_state;
487 struct drm_connector_state *conn_state;
490 crtc_state = drm_atomic_get_crtc_state(state, crtc);
491 if (IS_ERR(crtc_state))
492 return PTR_ERR(crtc_state);
493 conn_state = drm_atomic_get_connector_state(state, connector);
494 if (IS_ERR(conn_state))
495 return PTR_ERR(conn_state);
497 if (set->mode_changed) {
498 ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
502 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
506 crtc_state->active = true;
508 const struct drm_encoder_helper_funcs *encoder_helper_funcs;
509 const struct drm_connector_helper_funcs *connector_helper_funcs;
510 struct rockchip_drm_private *priv = drm_dev->dev_private;
511 struct drm_encoder *encoder;
512 int pipe = drm_crtc_index(crtc);
514 connector_helper_funcs = connector->helper_private;
515 if (!priv->crtc_funcs[pipe] ||
516 !priv->crtc_funcs[pipe]->loader_protect ||
517 !connector_helper_funcs ||
518 !connector_helper_funcs->best_encoder)
520 encoder = connector_helper_funcs->best_encoder(connector);
523 encoder_helper_funcs = encoder->helper_private;
524 if (!encoder_helper_funcs->atomic_check)
526 ret = encoder_helper_funcs->atomic_check(encoder, crtc->state,
530 if (encoder_helper_funcs->mode_set)
531 encoder_helper_funcs->mode_set(encoder, mode, mode);
532 priv->crtc_funcs[pipe]->loader_protect(crtc, true);
535 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
536 if (IS_ERR(primary_state))
537 return PTR_ERR(primary_state);
539 crtc_state->plane_mask = 1 << drm_plane_index(crtc->primary);
540 *plane_mask |= crtc_state->plane_mask;
542 drm_atomic_set_fb_for_plane(primary_state, set->fb);
543 drm_framebuffer_unreference(set->fb);
544 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
549 * some vop maybe not support ymirror, but force use it now.
551 drm_atomic_plane_set_property(crtc->primary, primary_state,
552 mode_config->rotation_property,
558 static void show_loader_logo(struct drm_device *drm_dev)
560 struct drm_atomic_state *state;
561 struct device_node *np = drm_dev->dev->of_node;
562 struct drm_mode_config *mode_config = &drm_dev->mode_config;
563 struct device_node *root, *route;
564 struct rockchip_drm_mode_set *set, *tmp;
565 struct list_head mode_set_list;
566 unsigned plane_mask = 0;
569 root = of_get_child_by_name(np, "route");
571 dev_warn(drm_dev->dev, "failed to parse display resources\n");
575 if (init_loader_memory(drm_dev)) {
576 dev_warn(drm_dev->dev, "failed to parse loader memory\n");
580 INIT_LIST_HEAD(&mode_set_list);
581 drm_modeset_lock_all(drm_dev);
582 state = drm_atomic_state_alloc(drm_dev);
584 dev_err(drm_dev->dev, "failed to alloc atomic state\n");
589 state->acquire_ctx = mode_config->acquire_ctx;
591 for_each_child_of_node(root, route) {
592 if (!of_device_is_available(route))
595 set = of_parse_display_resource(drm_dev, route);
599 if (setup_initial_state(drm_dev, state, set)) {
600 drm_framebuffer_unreference(set->fb);
604 INIT_LIST_HEAD(&set->head);
605 list_add_tail(&set->head, &mode_set_list);
608 if (list_empty(&mode_set_list)) {
609 dev_warn(drm_dev->dev, "can't not find any loader display\n");
615 * The state save initial devices status, swap the state into
616 * drm deivces as old state, so if new state come, can compare
617 * with this state to judge which status need to update.
619 drm_atomic_helper_swap_state(drm_dev, state);
620 drm_atomic_state_free(state);
621 state = drm_atomic_helper_duplicate_state(drm_dev,
622 mode_config->acquire_ctx);
624 dev_err(drm_dev->dev, "failed to duplicate atomic state\n");
625 ret = PTR_ERR_OR_ZERO(state);
628 state->acquire_ctx = mode_config->acquire_ctx;
629 list_for_each_entry(set, &mode_set_list, head)
631 * We don't want to see any fail on update_state.
633 WARN_ON(update_state(drm_dev, state, set, &plane_mask));
635 ret = drm_atomic_commit(state);
636 drm_atomic_clean_old_fb(drm_dev, plane_mask, ret);
638 list_for_each_entry_safe(set, tmp, &mode_set_list, head) {
639 struct drm_crtc *crtc = set->crtc;
641 list_del(&set->head);
645 * primary plane state rotation is not BIT(0), but we only want
646 * it effect on logo display, userspace may not known to clean
647 * this property, would get unexpect display, so force set
648 * primary rotation to BIT(0).
650 if (!crtc->primary || !crtc->primary->state)
653 drm_atomic_plane_set_property(crtc->primary,
654 crtc->primary->state,
655 mode_config->rotation_property,
660 * Is possible get deadlock here?
662 WARN_ON(ret == -EDEADLK);
667 rockchip_free_loader_memory(drm_dev);
669 drm_modeset_unlock_all(drm_dev);
673 drm_atomic_state_free(state);
675 drm_modeset_unlock_all(drm_dev);
677 dev_err(drm_dev->dev, "failed to show loader logo\n");
682 * Attach a (component) device to the shared drm dma mapping from master drm
683 * device. This is used by the VOPs to map GEM buffers to a common DMA
686 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
689 struct rockchip_drm_private *private = drm_dev->dev_private;
692 if (!is_support_iommu)
695 ret = iommu_attach_device(private->domain, dev);
697 dev_err(dev, "Failed to attach iommu device\n");
704 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
707 struct rockchip_drm_private *private = drm_dev->dev_private;
708 struct iommu_domain *domain = private->domain;
710 if (!is_support_iommu)
713 iommu_detach_device(domain, dev);
716 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
717 const struct rockchip_crtc_funcs *crtc_funcs)
719 int pipe = drm_crtc_index(crtc);
720 struct rockchip_drm_private *priv = crtc->dev->dev_private;
722 if (pipe >= ROCKCHIP_MAX_CRTC)
725 priv->crtc_funcs[pipe] = crtc_funcs;
730 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
732 int pipe = drm_crtc_index(crtc);
733 struct rockchip_drm_private *priv = crtc->dev->dev_private;
735 if (pipe >= ROCKCHIP_MAX_CRTC)
738 priv->crtc_funcs[pipe] = NULL;
741 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
744 struct drm_crtc *crtc;
747 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
754 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
757 struct rockchip_drm_private *priv = dev->dev_private;
758 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
760 if (crtc && priv->crtc_funcs[pipe] &&
761 priv->crtc_funcs[pipe]->enable_vblank)
762 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
767 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
770 struct rockchip_drm_private *priv = dev->dev_private;
771 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
773 if (crtc && priv->crtc_funcs[pipe] &&
774 priv->crtc_funcs[pipe]->enable_vblank)
775 priv->crtc_funcs[pipe]->disable_vblank(crtc);
778 static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
780 struct rockchip_drm_private *private = drm_dev->dev_private;
781 struct iommu_domain_geometry *geometry;
784 if (!is_support_iommu)
787 private->domain = iommu_domain_alloc(&platform_bus_type);
788 if (!private->domain)
791 geometry = &private->domain->geometry;
792 start = geometry->aperture_start;
793 end = geometry->aperture_end;
795 DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
797 drm_mm_init(&private->mm, start, end - start + 1);
798 mutex_init(&private->mm_lock);
803 static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
805 struct rockchip_drm_private *private = drm_dev->dev_private;
807 if (!is_support_iommu)
810 drm_mm_takedown(&private->mm);
811 iommu_domain_free(private->domain);
814 #ifdef CONFIG_DEBUG_FS
815 static int rockchip_drm_mm_dump(struct seq_file *s, void *data)
817 struct drm_info_node *node = s->private;
818 struct drm_minor *minor = node->minor;
819 struct drm_device *drm_dev = minor->dev;
820 struct rockchip_drm_private *priv = drm_dev->dev_private;
823 mutex_lock(&priv->mm_lock);
825 ret = drm_mm_dump_table(s, &priv->mm);
827 mutex_unlock(&priv->mm_lock);
832 static int rockchip_drm_summary_show(struct seq_file *s, void *data)
834 struct drm_info_node *node = s->private;
835 struct drm_minor *minor = node->minor;
836 struct drm_device *drm_dev = minor->dev;
837 struct rockchip_drm_private *priv = drm_dev->dev_private;
838 struct drm_crtc *crtc;
840 drm_for_each_crtc(crtc, drm_dev) {
841 int pipe = drm_crtc_index(crtc);
843 if (priv->crtc_funcs[pipe] &&
844 priv->crtc_funcs[pipe]->debugfs_dump)
845 priv->crtc_funcs[pipe]->debugfs_dump(crtc, s);
851 static struct drm_info_list rockchip_debugfs_files[] = {
852 { "summary", rockchip_drm_summary_show, 0, NULL },
853 { "mm_dump", rockchip_drm_mm_dump, 0, NULL },
856 static int rockchip_drm_debugfs_init(struct drm_minor *minor)
858 struct drm_device *dev = minor->dev;
861 ret = drm_debugfs_create_files(rockchip_debugfs_files,
862 ARRAY_SIZE(rockchip_debugfs_files),
866 dev_err(dev->dev, "could not install rockchip_debugfs_list\n");
873 static void rockchip_drm_debugfs_cleanup(struct drm_minor *minor)
875 drm_debugfs_remove_files(rockchip_debugfs_files,
876 ARRAY_SIZE(rockchip_debugfs_files), minor);
880 static int rockchip_drm_bind(struct device *dev)
882 struct drm_device *drm_dev;
883 struct rockchip_drm_private *private;
886 drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
890 ret = drm_dev_set_unique(drm_dev, "%s", dev_name(dev));
894 dev_set_drvdata(dev, drm_dev);
896 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
902 mutex_init(&private->commit.lock);
903 INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
905 drm_dev->dev_private = private;
907 #ifdef CONFIG_DRM_DMA_SYNC
908 private->cpu_fence_context = fence_context_alloc(1);
909 atomic_set(&private->cpu_fence_seqno, 0);
912 drm_mode_config_init(drm_dev);
914 rockchip_drm_mode_config_init(drm_dev);
916 ret = rockchip_drm_init_iommu(drm_dev);
918 goto err_config_cleanup;
920 /* Try to bind all sub drivers. */
921 ret = component_bind_all(dev, drm_dev);
923 goto err_iommu_cleanup;
925 /* init kms poll for handling hpd */
926 drm_kms_helper_poll_init(drm_dev);
929 * enable drm irq mode.
930 * - with irq_enabled = true, we can use the vblank feature.
932 drm_dev->irq_enabled = true;
934 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
936 goto err_kms_helper_poll_fini;
939 * with vblank_disable_allowed = true, vblank interrupt will be disabled
940 * by drm timer once a current process gives up ownership of
941 * vblank event.(after drm_vblank_put function is called)
943 drm_dev->vblank_disable_allowed = true;
945 drm_mode_config_reset(drm_dev);
948 show_loader_logo(drm_dev);
951 ret = rockchip_drm_fbdev_init(drm_dev);
953 goto err_vblank_cleanup;
955 drm_dev->mode_config.allow_fb_modifiers = true;
957 ret = drm_dev_register(drm_dev, 0);
963 rockchip_drm_fbdev_fini(drm_dev);
965 drm_vblank_cleanup(drm_dev);
966 err_kms_helper_poll_fini:
967 drm_kms_helper_poll_fini(drm_dev);
968 component_unbind_all(dev, drm_dev);
970 rockchip_iommu_cleanup(drm_dev);
972 drm_mode_config_cleanup(drm_dev);
973 drm_dev->dev_private = NULL;
975 drm_dev_unref(drm_dev);
979 static void rockchip_drm_unbind(struct device *dev)
981 struct drm_device *drm_dev = dev_get_drvdata(dev);
983 rockchip_drm_fbdev_fini(drm_dev);
984 drm_vblank_cleanup(drm_dev);
985 drm_kms_helper_poll_fini(drm_dev);
986 component_unbind_all(dev, drm_dev);
987 rockchip_iommu_cleanup(drm_dev);
988 drm_mode_config_cleanup(drm_dev);
989 drm_dev->dev_private = NULL;
990 drm_dev_unregister(drm_dev);
991 drm_dev_unref(drm_dev);
992 dev_set_drvdata(dev, NULL);
995 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
996 struct drm_file *file_priv)
998 struct rockchip_drm_private *priv = crtc->dev->dev_private;
999 int pipe = drm_crtc_index(crtc);
1001 if (pipe < ROCKCHIP_MAX_CRTC &&
1002 priv->crtc_funcs[pipe] &&
1003 priv->crtc_funcs[pipe]->cancel_pending_vblank)
1004 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
1007 int rockchip_drm_register_subdrv(struct drm_rockchip_subdrv *subdrv)
1012 mutex_lock(&subdrv_list_mutex);
1013 list_add_tail(&subdrv->list, &rockchip_drm_subdrv_list);
1014 mutex_unlock(&subdrv_list_mutex);
1018 EXPORT_SYMBOL_GPL(rockchip_drm_register_subdrv);
1020 int rockchip_drm_unregister_subdrv(struct drm_rockchip_subdrv *subdrv)
1025 mutex_lock(&subdrv_list_mutex);
1026 list_del(&subdrv->list);
1027 mutex_unlock(&subdrv_list_mutex);
1031 EXPORT_SYMBOL_GPL(rockchip_drm_unregister_subdrv);
1033 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
1035 struct rockchip_drm_file_private *file_priv;
1036 struct drm_rockchip_subdrv *subdrv;
1039 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
1042 INIT_LIST_HEAD(&file_priv->gem_cpu_acquire_list);
1044 file->driver_priv = file_priv;
1046 mutex_lock(&subdrv_list_mutex);
1047 list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list) {
1048 ret = subdrv->open(dev, subdrv->dev, file);
1050 mutex_unlock(&subdrv_list_mutex);
1051 goto err_free_file_priv;
1054 mutex_unlock(&subdrv_list_mutex);
1065 static void rockchip_drm_preclose(struct drm_device *dev,
1066 struct drm_file *file_priv)
1068 struct rockchip_drm_file_private *file_private = file_priv->driver_priv;
1069 struct rockchip_gem_object_node *cur, *d;
1070 struct drm_rockchip_subdrv *subdrv;
1071 struct drm_crtc *crtc;
1073 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1074 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
1076 mutex_lock(&dev->struct_mutex);
1077 list_for_each_entry_safe(cur, d,
1078 &file_private->gem_cpu_acquire_list, list) {
1079 #ifdef CONFIG_DRM_DMA_SYNC
1080 BUG_ON(!cur->rockchip_gem_obj->acquire_fence);
1081 drm_fence_signal_and_put(&cur->rockchip_gem_obj->acquire_fence);
1083 drm_gem_object_unreference(&cur->rockchip_gem_obj->base);
1086 /* since we are deleting the whole list, just initialize the header
1087 * instead of calling list_del for every element
1089 INIT_LIST_HEAD(&file_private->gem_cpu_acquire_list);
1090 mutex_unlock(&dev->struct_mutex);
1092 mutex_lock(&subdrv_list_mutex);
1093 list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list)
1094 subdrv->close(dev, subdrv->dev, file_priv);
1095 mutex_unlock(&subdrv_list_mutex);
1098 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
1100 kfree(file->driver_priv);
1101 file->driver_priv = NULL;
1104 void rockchip_drm_lastclose(struct drm_device *dev)
1106 struct rockchip_drm_private *priv = dev->dev_private;
1109 drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev_helper);
1112 static const struct drm_ioctl_desc rockchip_ioctls[] = {
1113 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_gem_create_ioctl,
1114 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1115 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
1116 rockchip_gem_map_offset_ioctl,
1117 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1118 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_ACQUIRE,
1119 rockchip_gem_cpu_acquire_ioctl,
1120 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1121 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_RELEASE,
1122 rockchip_gem_cpu_release_ioctl,
1123 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1124 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_GET_VER, rockchip_rga_get_ver_ioctl,
1125 DRM_AUTH | DRM_RENDER_ALLOW),
1126 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_SET_CMDLIST,
1127 rockchip_rga_set_cmdlist_ioctl,
1128 DRM_AUTH | DRM_RENDER_ALLOW),
1129 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_EXEC, rockchip_rga_exec_ioctl,
1130 DRM_AUTH | DRM_RENDER_ALLOW),
1133 static const struct file_operations rockchip_drm_driver_fops = {
1134 .owner = THIS_MODULE,
1136 .mmap = rockchip_gem_mmap,
1139 .unlocked_ioctl = drm_ioctl,
1140 #ifdef CONFIG_COMPAT
1141 .compat_ioctl = drm_compat_ioctl,
1143 .release = drm_release,
1146 const struct vm_operations_struct rockchip_drm_vm_ops = {
1147 .open = drm_gem_vm_open,
1148 .close = drm_gem_vm_close,
1151 static struct drm_driver rockchip_drm_driver = {
1152 .driver_features = DRIVER_MODESET | DRIVER_GEM |
1153 DRIVER_PRIME | DRIVER_ATOMIC |
1155 .preclose = rockchip_drm_preclose,
1156 .lastclose = rockchip_drm_lastclose,
1157 .get_vblank_counter = drm_vblank_no_hw_counter,
1158 .open = rockchip_drm_open,
1159 .postclose = rockchip_drm_postclose,
1160 .enable_vblank = rockchip_drm_crtc_enable_vblank,
1161 .disable_vblank = rockchip_drm_crtc_disable_vblank,
1162 .gem_vm_ops = &rockchip_drm_vm_ops,
1163 .gem_free_object = rockchip_gem_free_object,
1164 .dumb_create = rockchip_gem_dumb_create,
1165 .dumb_map_offset = rockchip_gem_dumb_map_offset,
1166 .dumb_destroy = drm_gem_dumb_destroy,
1167 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
1168 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
1169 .gem_prime_import = drm_gem_prime_import,
1170 .gem_prime_export = drm_gem_prime_export,
1171 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
1172 .gem_prime_import_sg_table = rockchip_gem_prime_import_sg_table,
1173 .gem_prime_vmap = rockchip_gem_prime_vmap,
1174 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
1175 .gem_prime_mmap = rockchip_gem_mmap_buf,
1176 #ifdef CONFIG_DEBUG_FS
1177 .debugfs_init = rockchip_drm_debugfs_init,
1178 .debugfs_cleanup = rockchip_drm_debugfs_cleanup,
1180 .ioctls = rockchip_ioctls,
1181 .num_ioctls = ARRAY_SIZE(rockchip_ioctls),
1182 .fops = &rockchip_drm_driver_fops,
1183 .name = DRIVER_NAME,
1184 .desc = DRIVER_DESC,
1185 .date = DRIVER_DATE,
1186 .major = DRIVER_MAJOR,
1187 .minor = DRIVER_MINOR,
1190 #ifdef CONFIG_PM_SLEEP
1191 void rockchip_drm_fb_suspend(struct drm_device *drm)
1193 struct rockchip_drm_private *priv = drm->dev_private;
1196 drm_fb_helper_set_suspend(priv->fbdev_helper, 1);
1200 void rockchip_drm_fb_resume(struct drm_device *drm)
1202 struct rockchip_drm_private *priv = drm->dev_private;
1205 drm_fb_helper_set_suspend(priv->fbdev_helper, 0);
1209 static int rockchip_drm_sys_suspend(struct device *dev)
1211 struct drm_device *drm = dev_get_drvdata(dev);
1212 struct rockchip_drm_private *priv = drm->dev_private;
1214 drm_kms_helper_poll_disable(drm);
1215 rockchip_drm_fb_suspend(drm);
1217 priv->state = drm_atomic_helper_suspend(drm);
1218 if (IS_ERR(priv->state)) {
1219 rockchip_drm_fb_resume(drm);
1220 drm_kms_helper_poll_enable(drm);
1221 return PTR_ERR(priv->state);
1227 static int rockchip_drm_sys_resume(struct device *dev)
1229 struct drm_device *drm = dev_get_drvdata(dev);
1230 struct rockchip_drm_private *priv = drm->dev_private;
1232 drm_atomic_helper_resume(drm, priv->state);
1233 rockchip_drm_fb_resume(drm);
1234 drm_kms_helper_poll_enable(drm);
1240 static const struct dev_pm_ops rockchip_drm_pm_ops = {
1241 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
1242 rockchip_drm_sys_resume)
1245 static int compare_of(struct device *dev, void *data)
1247 struct device_node *np = data;
1249 return dev->of_node == np;
1252 static void rockchip_add_endpoints(struct device *dev,
1253 struct component_match **match,
1254 struct device_node *port)
1256 struct device_node *ep, *remote;
1258 for_each_child_of_node(port, ep) {
1259 remote = of_graph_get_remote_port_parent(ep);
1260 if (!remote || !of_device_is_available(remote)) {
1261 of_node_put(remote);
1263 } else if (!of_device_is_available(remote->parent)) {
1264 dev_warn(dev, "parent device of %s is not available\n",
1266 of_node_put(remote);
1270 component_match_add(dev, match, compare_of, remote);
1271 of_node_put(remote);
1275 static const struct component_master_ops rockchip_drm_ops = {
1276 .bind = rockchip_drm_bind,
1277 .unbind = rockchip_drm_unbind,
1280 static int rockchip_drm_platform_probe(struct platform_device *pdev)
1282 struct device *dev = &pdev->dev;
1283 struct component_match *match = NULL;
1284 struct device_node *np = dev->of_node;
1285 struct device_node *port;
1291 * Bind the crtc ports first, so that
1292 * drm_of_find_possible_crtcs called from encoder .bind callbacks
1293 * works as expected.
1296 struct device_node *iommu;
1298 port = of_parse_phandle(np, "ports", i);
1302 if (!of_device_is_available(port->parent)) {
1307 iommu = of_parse_phandle(port->parent, "iommus", 0);
1308 if (!iommu || !of_device_is_available(iommu->parent)) {
1309 dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
1310 port->parent->full_name);
1312 * if there is a crtc not support iommu, force set all
1313 * crtc use non-iommu buffer.
1315 is_support_iommu = false;
1318 component_match_add(dev, &match, compare_of, port->parent);
1323 dev_err(dev, "missing 'ports' property\n");
1328 dev_err(dev, "No available vop found for display-subsystem.\n");
1332 * For each bound crtc, bind the encoders attached to its
1336 port = of_parse_phandle(np, "ports", i);
1340 if (!of_device_is_available(port->parent)) {
1345 rockchip_add_endpoints(dev, &match, port);
1349 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
1352 static int rockchip_drm_platform_remove(struct platform_device *pdev)
1354 component_master_del(&pdev->dev, &rockchip_drm_ops);
1359 static const struct of_device_id rockchip_drm_dt_ids[] = {
1360 { .compatible = "rockchip,display-subsystem", },
1363 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
1365 static struct platform_driver rockchip_drm_platform_driver = {
1366 .probe = rockchip_drm_platform_probe,
1367 .remove = rockchip_drm_platform_remove,
1369 .name = "rockchip-drm",
1370 .of_match_table = rockchip_drm_dt_ids,
1371 .pm = &rockchip_drm_pm_ops,
1375 module_platform_driver(rockchip_drm_platform_driver);
1377 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
1378 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
1379 MODULE_LICENSE("GPL v2");