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;
70 static struct drm_crtc *find_crtc_by_node(struct drm_device *drm_dev,
71 struct device_node *node)
73 struct device_node *np_crtc;
74 struct drm_crtc *crtc;
76 np_crtc = of_get_parent(node);
77 if (!np_crtc || !of_device_is_available(np_crtc))
80 drm_for_each_crtc(crtc, drm_dev) {
81 if (crtc->port == np_crtc)
88 static struct drm_connector *find_connector_by_node(struct drm_device *drm_dev,
89 struct device_node *node)
91 struct device_node *np_connector;
92 struct drm_connector *connector;
94 np_connector = of_graph_get_remote_port_parent(node);
95 if (!np_connector || !of_device_is_available(np_connector))
98 drm_for_each_connector(connector, drm_dev) {
99 if (connector->port == np_connector)
106 static int init_loader_memory(struct drm_device *drm_dev)
108 struct rockchip_drm_private *private = drm_dev->dev_private;
109 struct rockchip_logo *logo;
110 struct device_node *np = drm_dev->dev->of_node;
111 struct device_node *node;
112 unsigned long nr_pages;
114 struct sg_table *sgt;
115 phys_addr_t start, size;
119 logo = devm_kmalloc(drm_dev->dev, sizeof(*logo), GFP_KERNEL);
123 node = of_parse_phandle(np, "memory-region", 0);
127 ret = of_address_to_resource(node, 0, &res);
131 size = resource_size(&res);
135 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
136 pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL);
140 while (i < nr_pages) {
141 pages[i] = phys_to_page(start);
145 sgt = drm_prime_pages_to_sg(pages, nr_pages);
151 if (private->domain) {
152 memset(&logo->mm, 0, sizeof(logo->mm));
153 ret = drm_mm_insert_node_generic(&private->mm, &logo->mm,
157 DRM_ERROR("out of I/O virtual memory: %d\n", ret);
161 logo->dma_addr = logo->mm.start;
163 logo->iommu_map_size = iommu_map_sg(private->domain,
164 logo->dma_addr, sgt->sgl,
165 sgt->nents, IOMMU_READ);
166 if (logo->iommu_map_size < size) {
167 DRM_ERROR("failed to map buffer");
169 goto err_remove_node;
172 dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, DMA_TO_DEVICE);
173 logo->dma_addr = sg_dma_address(sgt->sgl);
177 logo->start = res.start;
180 private->logo = logo;
185 drm_mm_remove_node(&logo->mm);
192 static struct drm_framebuffer *
193 get_framebuffer_by_node(struct drm_device *drm_dev, struct device_node *node)
195 struct rockchip_drm_private *private = drm_dev->dev_private;
196 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
200 if (WARN_ON(!private->logo))
203 if (of_property_read_u32(node, "logo,offset", &val)) {
204 pr_err("%s: failed to get logo,offset\n", __func__);
207 mode_cmd.offsets[0] = val;
209 if (of_property_read_u32(node, "logo,width", &val)) {
210 pr_err("%s: failed to get logo,width\n", __func__);
213 mode_cmd.width = val;
215 if (of_property_read_u32(node, "logo,height", &val)) {
216 pr_err("%s: failed to get logo,height\n", __func__);
219 mode_cmd.height = val;
221 if (of_property_read_u32(node, "logo,bpp", &val)) {
222 pr_err("%s: failed to get logo,bpp\n", __func__);
227 mode_cmd.pitches[0] = mode_cmd.width * bpp / 8;
231 mode_cmd.pixel_format = DRM_FORMAT_BGR565;
234 mode_cmd.pixel_format = DRM_FORMAT_BGR888;
237 mode_cmd.pixel_format = DRM_FORMAT_XBGR8888;
240 pr_err("%s: unsupport to logo bpp %d\n", __func__, bpp);
244 return rockchip_fb_alloc(drm_dev, &mode_cmd, NULL, private->logo, 1);
247 static struct rockchip_drm_mode_set *
248 of_parse_display_resource(struct drm_device *drm_dev, struct device_node *route)
250 struct rockchip_drm_mode_set *set;
251 struct device_node *connect;
252 struct drm_framebuffer *fb;
253 struct drm_connector *connector;
254 struct drm_crtc *crtc;
258 connect = of_parse_phandle(route, "connect", 0);
262 fb = get_framebuffer_by_node(drm_dev, route);
263 if (IS_ERR_OR_NULL(fb))
266 crtc = find_crtc_by_node(drm_dev, connect);
267 connector = find_connector_by_node(drm_dev, connect);
268 if (!crtc || !connector) {
269 dev_warn(drm_dev->dev,
270 "No available crtc or connector for display");
271 drm_framebuffer_unreference(fb);
275 set = kzalloc(sizeof(*set), GFP_KERNEL);
279 if (!of_property_read_u32(route, "video,hdisplay", &val))
282 if (!of_property_read_u32(route, "video,vdisplay", &val))
285 if (!of_property_read_u32(route, "video,vrefresh", &val))
288 if (!of_property_read_u32(route, "logo,ymirror", &val))
292 if (!of_property_read_string(route, "logo,mode", &string) &&
293 !strcmp(string, "fullscreen"))
298 set->connector = connector;
303 int setup_initial_state(struct drm_device *drm_dev,
304 struct drm_atomic_state *state,
305 struct rockchip_drm_mode_set *set)
307 struct drm_connector *connector = set->connector;
308 struct drm_crtc *crtc = set->crtc;
309 struct drm_crtc_state *crtc_state;
310 struct drm_connector_state *conn_state;
311 struct drm_plane_state *primary_state;
312 struct drm_display_mode *mode = NULL;
313 const struct drm_connector_helper_funcs *funcs;
314 bool is_crtc_enabled = true;
315 int hdisplay, vdisplay;
316 int fb_width, fb_height;
317 int found = 0, match = 0;
321 if (!set->hdisplay || !set->vdisplay || !set->vrefresh)
322 is_crtc_enabled = false;
324 conn_state = drm_atomic_get_connector_state(state, connector);
325 if (IS_ERR(conn_state))
326 return PTR_ERR(conn_state);
328 funcs = connector->helper_private;
329 conn_state->best_encoder = funcs->best_encoder(connector);
330 if (funcs->loader_protect)
331 funcs->loader_protect(connector, true);
332 connector->loader_protect = true;
333 num_modes = connector->funcs->fill_modes(connector, 4096, 4096);
335 dev_err(drm_dev->dev, "connector[%s] can't found any modes\n",
341 list_for_each_entry(mode, &connector->modes, head) {
342 if (mode->hdisplay == set->hdisplay &&
343 mode->vdisplay == set->vdisplay &&
344 drm_mode_vrefresh(mode) == set->vrefresh) {
352 list_for_each_entry(mode, &connector->modes, head) {
353 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
360 mode = list_first_entry_or_null(&connector->modes,
361 struct drm_display_mode,
364 pr_err("failed to find available modes\n");
372 crtc_state = drm_atomic_get_crtc_state(state, crtc);
373 if (IS_ERR(crtc_state)) {
374 ret = PTR_ERR(crtc_state);
378 drm_mode_copy(&crtc_state->adjusted_mode, mode);
379 if (!match || !is_crtc_enabled) {
380 set->mode_changed = true;
382 ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
386 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
390 crtc_state->active = true;
397 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
398 if (IS_ERR(primary_state)) {
399 ret = PTR_ERR(primary_state);
403 hdisplay = mode->hdisplay;
404 vdisplay = mode->vdisplay;
405 fb_width = set->fb->width;
406 fb_height = set->fb->height;
408 primary_state->crtc = crtc;
409 primary_state->src_x = 0;
410 primary_state->src_y = 0;
411 primary_state->src_w = fb_width << 16;
412 primary_state->src_h = fb_height << 16;
414 if (set->fb->width >= hdisplay) {
415 primary_state->crtc_x = 0;
416 primary_state->crtc_w = hdisplay;
418 primary_state->crtc_x = (hdisplay - fb_width) / 2;
419 primary_state->crtc_w = set->fb->width;
422 if (set->fb->height >= vdisplay) {
423 primary_state->crtc_y = 0;
424 primary_state->crtc_h = vdisplay;
426 primary_state->crtc_y = (vdisplay - fb_height) / 2;
427 primary_state->crtc_h = fb_height;
430 primary_state->crtc_x = 0;
431 primary_state->crtc_y = 0;
432 primary_state->crtc_w = hdisplay;
433 primary_state->crtc_h = vdisplay;
439 if (funcs->loader_protect)
440 funcs->loader_protect(connector, false);
441 connector->loader_protect = false;
445 static int update_state(struct drm_device *drm_dev,
446 struct drm_atomic_state *state,
447 struct rockchip_drm_mode_set *set,
448 unsigned int *plane_mask)
450 struct drm_mode_config *mode_config = &drm_dev->mode_config;
451 struct drm_crtc *crtc = set->crtc;
452 struct drm_connector *connector = set->connector;
453 struct drm_display_mode *mode = set->mode;
454 struct drm_plane_state *primary_state;
455 struct drm_crtc_state *crtc_state;
456 struct drm_connector_state *conn_state;
459 crtc_state = drm_atomic_get_crtc_state(state, crtc);
460 if (IS_ERR(crtc_state))
461 return PTR_ERR(crtc_state);
462 conn_state = drm_atomic_get_connector_state(state, connector);
463 if (IS_ERR(conn_state))
464 return PTR_ERR(conn_state);
466 if (set->mode_changed) {
467 ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
471 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
475 crtc_state->active = true;
477 const struct drm_encoder_helper_funcs *encoder_helper_funcs;
478 const struct drm_connector_helper_funcs *connector_helper_funcs;
479 struct rockchip_drm_private *priv = drm_dev->dev_private;
480 struct drm_encoder *encoder;
481 int pipe = drm_crtc_index(crtc);
483 connector_helper_funcs = connector->helper_private;
484 if (!priv->crtc_funcs[pipe] ||
485 !priv->crtc_funcs[pipe]->loader_protect ||
486 !connector_helper_funcs ||
487 !connector_helper_funcs->best_encoder)
489 encoder = connector_helper_funcs->best_encoder(connector);
492 encoder_helper_funcs = encoder->helper_private;
493 if (!encoder_helper_funcs->atomic_check)
495 ret = encoder_helper_funcs->atomic_check(encoder, crtc->state,
499 if (encoder_helper_funcs->mode_set)
500 encoder_helper_funcs->mode_set(encoder, mode, mode);
501 priv->crtc_funcs[pipe]->loader_protect(crtc, true);
504 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
505 if (IS_ERR(primary_state))
506 return PTR_ERR(primary_state);
508 crtc_state->plane_mask = 1 << drm_plane_index(crtc->primary);
509 *plane_mask |= crtc_state->plane_mask;
511 drm_atomic_set_fb_for_plane(primary_state, set->fb);
512 drm_framebuffer_unreference(set->fb);
513 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
518 * some vop maybe not support ymirror, but force use it now.
520 drm_atomic_plane_set_property(crtc->primary, primary_state,
521 mode_config->rotation_property,
527 static void show_loader_logo(struct drm_device *drm_dev)
529 struct drm_atomic_state *state;
530 struct device_node *np = drm_dev->dev->of_node;
531 struct drm_mode_config *mode_config = &drm_dev->mode_config;
532 struct device_node *root, *route;
533 struct rockchip_drm_mode_set *set, *tmp;
534 struct list_head mode_set_list;
535 unsigned plane_mask = 0;
538 root = of_get_child_by_name(np, "route");
540 dev_warn(drm_dev->dev, "failed to parse display resources\n");
544 if (init_loader_memory(drm_dev)) {
545 dev_warn(drm_dev->dev, "failed to parse loader memory\n");
549 INIT_LIST_HEAD(&mode_set_list);
550 drm_modeset_lock_all(drm_dev);
551 state = drm_atomic_state_alloc(drm_dev);
553 dev_err(drm_dev->dev, "failed to alloc atomic state\n");
558 state->acquire_ctx = mode_config->acquire_ctx;
560 for_each_child_of_node(root, route) {
561 if (!of_device_is_available(route))
564 set = of_parse_display_resource(drm_dev, route);
568 if (setup_initial_state(drm_dev, state, set)) {
569 drm_framebuffer_unreference(set->fb);
573 INIT_LIST_HEAD(&set->head);
574 list_add_tail(&set->head, &mode_set_list);
577 if (list_empty(&mode_set_list)) {
578 dev_warn(drm_dev->dev, "can't not find any loader display\n");
584 * The state save initial devices status, swap the state into
585 * drm deivces as old state, so if new state come, can compare
586 * with this state to judge which status need to update.
588 drm_atomic_helper_swap_state(drm_dev, state);
589 drm_atomic_state_free(state);
590 state = drm_atomic_helper_duplicate_state(drm_dev,
591 mode_config->acquire_ctx);
593 dev_err(drm_dev->dev, "failed to duplicate atomic state\n");
594 ret = PTR_ERR_OR_ZERO(state);
597 state->acquire_ctx = mode_config->acquire_ctx;
598 list_for_each_entry(set, &mode_set_list, head)
600 * We don't want to see any fail on update_state.
602 WARN_ON(update_state(drm_dev, state, set, &plane_mask));
604 ret = drm_atomic_commit(state);
605 drm_atomic_clean_old_fb(drm_dev, plane_mask, ret);
607 list_for_each_entry_safe(set, tmp, &mode_set_list, head) {
608 struct drm_crtc *crtc = set->crtc;
610 list_del(&set->head);
614 * primary plane state rotation is not BIT(0), but we only want
615 * it effect on logo display, userspace may not known to clean
616 * this property, would get unexpect display, so force set
617 * primary rotation to BIT(0).
619 if (!crtc->primary || !crtc->primary->state)
622 drm_atomic_plane_set_property(crtc->primary,
623 crtc->primary->state,
624 mode_config->rotation_property,
629 * Is possible get deadlock here?
631 WARN_ON(ret == -EDEADLK);
636 drm_modeset_unlock_all(drm_dev);
640 drm_atomic_state_free(state);
642 drm_modeset_unlock_all(drm_dev);
644 dev_err(drm_dev->dev, "failed to show loader logo\n");
648 * Attach a (component) device to the shared drm dma mapping from master drm
649 * device. This is used by the VOPs to map GEM buffers to a common DMA
652 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
655 struct rockchip_drm_private *private = drm_dev->dev_private;
658 if (!is_support_iommu)
661 ret = iommu_attach_device(private->domain, dev);
663 dev_err(dev, "Failed to attach iommu device\n");
670 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
673 struct rockchip_drm_private *private = drm_dev->dev_private;
674 struct iommu_domain *domain = private->domain;
676 if (!is_support_iommu)
679 iommu_detach_device(domain, dev);
682 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
683 const struct rockchip_crtc_funcs *crtc_funcs)
685 int pipe = drm_crtc_index(crtc);
686 struct rockchip_drm_private *priv = crtc->dev->dev_private;
688 if (pipe >= ROCKCHIP_MAX_CRTC)
691 priv->crtc_funcs[pipe] = crtc_funcs;
696 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
698 int pipe = drm_crtc_index(crtc);
699 struct rockchip_drm_private *priv = crtc->dev->dev_private;
701 if (pipe >= ROCKCHIP_MAX_CRTC)
704 priv->crtc_funcs[pipe] = NULL;
707 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
710 struct drm_crtc *crtc;
713 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
720 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
723 struct rockchip_drm_private *priv = dev->dev_private;
724 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
726 if (crtc && priv->crtc_funcs[pipe] &&
727 priv->crtc_funcs[pipe]->enable_vblank)
728 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
733 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
736 struct rockchip_drm_private *priv = dev->dev_private;
737 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
739 if (crtc && priv->crtc_funcs[pipe] &&
740 priv->crtc_funcs[pipe]->enable_vblank)
741 priv->crtc_funcs[pipe]->disable_vblank(crtc);
744 static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
746 struct rockchip_drm_private *private = drm_dev->dev_private;
747 struct iommu_domain_geometry *geometry;
750 if (!is_support_iommu)
753 private->domain = iommu_domain_alloc(&platform_bus_type);
754 if (!private->domain)
757 geometry = &private->domain->geometry;
758 start = geometry->aperture_start;
759 end = geometry->aperture_end;
761 DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
763 drm_mm_init(&private->mm, start, end - start + 1);
764 mutex_init(&private->mm_lock);
769 static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
771 struct rockchip_drm_private *private = drm_dev->dev_private;
773 if (!is_support_iommu)
776 drm_mm_takedown(&private->mm);
777 iommu_domain_free(private->domain);
780 #ifdef CONFIG_DEBUG_FS
781 static int rockchip_drm_mm_dump(struct seq_file *s, void *data)
783 struct drm_info_node *node = s->private;
784 struct drm_minor *minor = node->minor;
785 struct drm_device *drm_dev = minor->dev;
786 struct rockchip_drm_private *priv = drm_dev->dev_private;
789 mutex_lock(&priv->mm_lock);
791 ret = drm_mm_dump_table(s, &priv->mm);
793 mutex_unlock(&priv->mm_lock);
798 static int rockchip_drm_summary_show(struct seq_file *s, void *data)
800 struct drm_info_node *node = s->private;
801 struct drm_minor *minor = node->minor;
802 struct drm_device *drm_dev = minor->dev;
803 struct rockchip_drm_private *priv = drm_dev->dev_private;
804 struct drm_crtc *crtc;
806 drm_for_each_crtc(crtc, drm_dev) {
807 int pipe = drm_crtc_index(crtc);
809 if (priv->crtc_funcs[pipe] &&
810 priv->crtc_funcs[pipe]->debugfs_dump)
811 priv->crtc_funcs[pipe]->debugfs_dump(crtc, s);
817 static struct drm_info_list rockchip_debugfs_files[] = {
818 { "summary", rockchip_drm_summary_show, 0, NULL },
819 { "mm_dump", rockchip_drm_mm_dump, 0, NULL },
822 static int rockchip_drm_debugfs_init(struct drm_minor *minor)
824 struct drm_device *dev = minor->dev;
827 ret = drm_debugfs_create_files(rockchip_debugfs_files,
828 ARRAY_SIZE(rockchip_debugfs_files),
832 dev_err(dev->dev, "could not install rockchip_debugfs_list\n");
839 static void rockchip_drm_debugfs_cleanup(struct drm_minor *minor)
841 drm_debugfs_remove_files(rockchip_debugfs_files,
842 ARRAY_SIZE(rockchip_debugfs_files), minor);
846 static int rockchip_drm_bind(struct device *dev)
848 struct drm_device *drm_dev;
849 struct rockchip_drm_private *private;
852 drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
856 ret = drm_dev_set_unique(drm_dev, "%s", dev_name(dev));
860 dev_set_drvdata(dev, drm_dev);
862 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
868 mutex_init(&private->commit.lock);
869 INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
871 drm_dev->dev_private = private;
873 #ifdef CONFIG_DRM_DMA_SYNC
874 private->cpu_fence_context = fence_context_alloc(1);
875 atomic_set(&private->cpu_fence_seqno, 0);
878 drm_mode_config_init(drm_dev);
880 rockchip_drm_mode_config_init(drm_dev);
882 ret = rockchip_drm_init_iommu(drm_dev);
884 goto err_config_cleanup;
886 /* Try to bind all sub drivers. */
887 ret = component_bind_all(dev, drm_dev);
889 goto err_iommu_cleanup;
891 /* init kms poll for handling hpd */
892 drm_kms_helper_poll_init(drm_dev);
895 * enable drm irq mode.
896 * - with irq_enabled = true, we can use the vblank feature.
898 drm_dev->irq_enabled = true;
900 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
902 goto err_kms_helper_poll_fini;
905 * with vblank_disable_allowed = true, vblank interrupt will be disabled
906 * by drm timer once a current process gives up ownership of
907 * vblank event.(after drm_vblank_put function is called)
909 drm_dev->vblank_disable_allowed = true;
911 drm_mode_config_reset(drm_dev);
913 show_loader_logo(drm_dev);
915 ret = rockchip_drm_fbdev_init(drm_dev);
917 goto err_vblank_cleanup;
919 drm_dev->mode_config.allow_fb_modifiers = true;
921 ret = drm_dev_register(drm_dev, 0);
927 rockchip_drm_fbdev_fini(drm_dev);
929 drm_vblank_cleanup(drm_dev);
930 err_kms_helper_poll_fini:
931 drm_kms_helper_poll_fini(drm_dev);
932 component_unbind_all(dev, drm_dev);
934 rockchip_iommu_cleanup(drm_dev);
936 drm_mode_config_cleanup(drm_dev);
937 drm_dev->dev_private = NULL;
939 drm_dev_unref(drm_dev);
943 static void rockchip_drm_unbind(struct device *dev)
945 struct drm_device *drm_dev = dev_get_drvdata(dev);
947 rockchip_drm_fbdev_fini(drm_dev);
948 drm_vblank_cleanup(drm_dev);
949 drm_kms_helper_poll_fini(drm_dev);
950 component_unbind_all(dev, drm_dev);
951 rockchip_iommu_cleanup(drm_dev);
952 drm_mode_config_cleanup(drm_dev);
953 drm_dev->dev_private = NULL;
954 drm_dev_unregister(drm_dev);
955 drm_dev_unref(drm_dev);
956 dev_set_drvdata(dev, NULL);
959 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
960 struct drm_file *file_priv)
962 struct rockchip_drm_private *priv = crtc->dev->dev_private;
963 int pipe = drm_crtc_index(crtc);
965 if (pipe < ROCKCHIP_MAX_CRTC &&
966 priv->crtc_funcs[pipe] &&
967 priv->crtc_funcs[pipe]->cancel_pending_vblank)
968 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
971 int rockchip_drm_register_subdrv(struct drm_rockchip_subdrv *subdrv)
976 mutex_lock(&subdrv_list_mutex);
977 list_add_tail(&subdrv->list, &rockchip_drm_subdrv_list);
978 mutex_unlock(&subdrv_list_mutex);
982 EXPORT_SYMBOL_GPL(rockchip_drm_register_subdrv);
984 int rockchip_drm_unregister_subdrv(struct drm_rockchip_subdrv *subdrv)
989 mutex_lock(&subdrv_list_mutex);
990 list_del(&subdrv->list);
991 mutex_unlock(&subdrv_list_mutex);
995 EXPORT_SYMBOL_GPL(rockchip_drm_unregister_subdrv);
997 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
999 struct rockchip_drm_file_private *file_priv;
1000 struct drm_rockchip_subdrv *subdrv;
1003 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
1006 INIT_LIST_HEAD(&file_priv->gem_cpu_acquire_list);
1008 file->driver_priv = file_priv;
1010 mutex_lock(&subdrv_list_mutex);
1011 list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list) {
1012 ret = subdrv->open(dev, subdrv->dev, file);
1014 mutex_unlock(&subdrv_list_mutex);
1015 goto err_free_file_priv;
1018 mutex_unlock(&subdrv_list_mutex);
1029 static void rockchip_drm_preclose(struct drm_device *dev,
1030 struct drm_file *file_priv)
1032 struct rockchip_drm_file_private *file_private = file_priv->driver_priv;
1033 struct rockchip_gem_object_node *cur, *d;
1034 struct drm_rockchip_subdrv *subdrv;
1035 struct drm_crtc *crtc;
1037 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1038 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
1040 mutex_lock(&dev->struct_mutex);
1041 list_for_each_entry_safe(cur, d,
1042 &file_private->gem_cpu_acquire_list, list) {
1043 #ifdef CONFIG_DRM_DMA_SYNC
1044 BUG_ON(!cur->rockchip_gem_obj->acquire_fence);
1045 drm_fence_signal_and_put(&cur->rockchip_gem_obj->acquire_fence);
1047 drm_gem_object_unreference(&cur->rockchip_gem_obj->base);
1050 /* since we are deleting the whole list, just initialize the header
1051 * instead of calling list_del for every element
1053 INIT_LIST_HEAD(&file_private->gem_cpu_acquire_list);
1054 mutex_unlock(&dev->struct_mutex);
1056 mutex_lock(&subdrv_list_mutex);
1057 list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list)
1058 subdrv->close(dev, subdrv->dev, file_priv);
1059 mutex_unlock(&subdrv_list_mutex);
1062 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
1064 kfree(file->driver_priv);
1065 file->driver_priv = NULL;
1068 void rockchip_drm_lastclose(struct drm_device *dev)
1070 struct rockchip_drm_private *priv = dev->dev_private;
1073 drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev_helper);
1076 static const struct drm_ioctl_desc rockchip_ioctls[] = {
1077 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_gem_create_ioctl,
1078 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1079 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
1080 rockchip_gem_map_offset_ioctl,
1081 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1082 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_ACQUIRE,
1083 rockchip_gem_cpu_acquire_ioctl,
1084 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1085 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_RELEASE,
1086 rockchip_gem_cpu_release_ioctl,
1087 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
1088 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_GET_VER, rockchip_rga_get_ver_ioctl,
1089 DRM_AUTH | DRM_RENDER_ALLOW),
1090 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_SET_CMDLIST,
1091 rockchip_rga_set_cmdlist_ioctl,
1092 DRM_AUTH | DRM_RENDER_ALLOW),
1093 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_EXEC, rockchip_rga_exec_ioctl,
1094 DRM_AUTH | DRM_RENDER_ALLOW),
1097 static const struct file_operations rockchip_drm_driver_fops = {
1098 .owner = THIS_MODULE,
1100 .mmap = rockchip_gem_mmap,
1103 .unlocked_ioctl = drm_ioctl,
1104 #ifdef CONFIG_COMPAT
1105 .compat_ioctl = drm_compat_ioctl,
1107 .release = drm_release,
1110 const struct vm_operations_struct rockchip_drm_vm_ops = {
1111 .open = drm_gem_vm_open,
1112 .close = drm_gem_vm_close,
1115 static struct drm_driver rockchip_drm_driver = {
1116 .driver_features = DRIVER_MODESET | DRIVER_GEM |
1117 DRIVER_PRIME | DRIVER_ATOMIC |
1119 .preclose = rockchip_drm_preclose,
1120 .lastclose = rockchip_drm_lastclose,
1121 .get_vblank_counter = drm_vblank_no_hw_counter,
1122 .open = rockchip_drm_open,
1123 .postclose = rockchip_drm_postclose,
1124 .enable_vblank = rockchip_drm_crtc_enable_vblank,
1125 .disable_vblank = rockchip_drm_crtc_disable_vblank,
1126 .gem_vm_ops = &rockchip_drm_vm_ops,
1127 .gem_free_object = rockchip_gem_free_object,
1128 .dumb_create = rockchip_gem_dumb_create,
1129 .dumb_map_offset = rockchip_gem_dumb_map_offset,
1130 .dumb_destroy = drm_gem_dumb_destroy,
1131 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
1132 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
1133 .gem_prime_import = drm_gem_prime_import,
1134 .gem_prime_export = drm_gem_prime_export,
1135 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
1136 .gem_prime_import_sg_table = rockchip_gem_prime_import_sg_table,
1137 .gem_prime_vmap = rockchip_gem_prime_vmap,
1138 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
1139 .gem_prime_mmap = rockchip_gem_mmap_buf,
1140 #ifdef CONFIG_DEBUG_FS
1141 .debugfs_init = rockchip_drm_debugfs_init,
1142 .debugfs_cleanup = rockchip_drm_debugfs_cleanup,
1144 .ioctls = rockchip_ioctls,
1145 .num_ioctls = ARRAY_SIZE(rockchip_ioctls),
1146 .fops = &rockchip_drm_driver_fops,
1147 .name = DRIVER_NAME,
1148 .desc = DRIVER_DESC,
1149 .date = DRIVER_DATE,
1150 .major = DRIVER_MAJOR,
1151 .minor = DRIVER_MINOR,
1154 #ifdef CONFIG_PM_SLEEP
1155 void rockchip_drm_fb_suspend(struct drm_device *drm)
1157 struct rockchip_drm_private *priv = drm->dev_private;
1160 drm_fb_helper_set_suspend(priv->fbdev_helper, 1);
1164 void rockchip_drm_fb_resume(struct drm_device *drm)
1166 struct rockchip_drm_private *priv = drm->dev_private;
1169 drm_fb_helper_set_suspend(priv->fbdev_helper, 0);
1173 static int rockchip_drm_sys_suspend(struct device *dev)
1175 struct drm_device *drm = dev_get_drvdata(dev);
1176 struct rockchip_drm_private *priv = drm->dev_private;
1178 drm_kms_helper_poll_disable(drm);
1179 rockchip_drm_fb_suspend(drm);
1181 priv->state = drm_atomic_helper_suspend(drm);
1182 if (IS_ERR(priv->state)) {
1183 rockchip_drm_fb_resume(drm);
1184 drm_kms_helper_poll_enable(drm);
1185 return PTR_ERR(priv->state);
1191 static int rockchip_drm_sys_resume(struct device *dev)
1193 struct drm_device *drm = dev_get_drvdata(dev);
1194 struct rockchip_drm_private *priv = drm->dev_private;
1196 drm_atomic_helper_resume(drm, priv->state);
1197 rockchip_drm_fb_resume(drm);
1198 drm_kms_helper_poll_enable(drm);
1204 static const struct dev_pm_ops rockchip_drm_pm_ops = {
1205 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
1206 rockchip_drm_sys_resume)
1209 static int compare_of(struct device *dev, void *data)
1211 struct device_node *np = data;
1213 return dev->of_node == np;
1216 static void rockchip_add_endpoints(struct device *dev,
1217 struct component_match **match,
1218 struct device_node *port)
1220 struct device_node *ep, *remote;
1222 for_each_child_of_node(port, ep) {
1223 remote = of_graph_get_remote_port_parent(ep);
1224 if (!remote || !of_device_is_available(remote)) {
1225 of_node_put(remote);
1227 } else if (!of_device_is_available(remote->parent)) {
1228 dev_warn(dev, "parent device of %s is not available\n",
1230 of_node_put(remote);
1234 component_match_add(dev, match, compare_of, remote);
1235 of_node_put(remote);
1239 static const struct component_master_ops rockchip_drm_ops = {
1240 .bind = rockchip_drm_bind,
1241 .unbind = rockchip_drm_unbind,
1244 static int rockchip_drm_platform_probe(struct platform_device *pdev)
1246 struct device *dev = &pdev->dev;
1247 struct component_match *match = NULL;
1248 struct device_node *np = dev->of_node;
1249 struct device_node *port;
1255 * Bind the crtc ports first, so that
1256 * drm_of_find_possible_crtcs called from encoder .bind callbacks
1257 * works as expected.
1260 struct device_node *iommu;
1262 port = of_parse_phandle(np, "ports", i);
1266 if (!of_device_is_available(port->parent)) {
1271 iommu = of_parse_phandle(port->parent, "iommus", 0);
1272 if (!iommu || !of_device_is_available(iommu->parent)) {
1273 dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
1274 port->parent->full_name);
1276 * if there is a crtc not support iommu, force set all
1277 * crtc use non-iommu buffer.
1279 is_support_iommu = false;
1282 component_match_add(dev, &match, compare_of, port->parent);
1287 dev_err(dev, "missing 'ports' property\n");
1292 dev_err(dev, "No available vop found for display-subsystem.\n");
1296 * For each bound crtc, bind the encoders attached to its
1300 port = of_parse_phandle(np, "ports", i);
1304 if (!of_device_is_available(port->parent)) {
1309 rockchip_add_endpoints(dev, &match, port);
1313 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
1316 static int rockchip_drm_platform_remove(struct platform_device *pdev)
1318 component_master_del(&pdev->dev, &rockchip_drm_ops);
1323 static const struct of_device_id rockchip_drm_dt_ids[] = {
1324 { .compatible = "rockchip,display-subsystem", },
1327 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
1329 static struct platform_driver rockchip_drm_platform_driver = {
1330 .probe = rockchip_drm_platform_probe,
1331 .remove = rockchip_drm_platform_remove,
1333 .name = "rockchip-drm",
1334 .of_match_table = rockchip_drm_dt_ids,
1335 .pm = &rockchip_drm_pm_ops,
1339 module_platform_driver(rockchip_drm_platform_driver);
1341 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
1342 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
1343 MODULE_LICENSE("GPL v2");