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.
17 #include <linux/dma-iommu.h>
20 #include <drm/drm_atomic.h>
21 #include <drm/drm_crtc_helper.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_sync_helper.h>
24 #include <drm/rockchip_drm.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/memblock.h>
28 #include <linux/module.h>
29 #include <linux/of_address.h>
30 #include <linux/of_graph.h>
31 #include <linux/component.h>
32 #include <linux/fence.h>
33 #include <linux/console.h>
35 #include "rockchip_drm_drv.h"
36 #include "rockchip_drm_fb.h"
37 #include "rockchip_drm_fbdev.h"
38 #include "rockchip_drm_gem.h"
39 #include "rockchip_drm_rga.h"
41 #define DRIVER_NAME "rockchip"
42 #define DRIVER_DESC "RockChip Soc DRM"
43 #define DRIVER_DATE "20140818"
44 #define DRIVER_MAJOR 1
45 #define DRIVER_MINOR 0
47 static LIST_HEAD(rockchip_drm_subdrv_list);
48 static DEFINE_MUTEX(subdrv_list_mutex);
50 struct rockchip_drm_mode_set {
51 struct list_head head;
52 struct drm_framebuffer *fb;
53 struct drm_connector *connector;
54 struct drm_crtc *crtc;
55 struct drm_display_mode *mode;
65 static struct drm_crtc *find_crtc_by_node(struct drm_device *drm_dev,
66 struct device_node *node)
68 struct device_node *np_crtc;
69 struct drm_crtc *crtc;
71 np_crtc = of_get_parent(node);
72 if (!np_crtc || !of_device_is_available(np_crtc))
75 drm_for_each_crtc(crtc, drm_dev) {
76 if (crtc->port == np_crtc)
83 static struct drm_connector *find_connector_by_node(struct drm_device *drm_dev,
84 struct device_node *node)
86 struct device_node *np_connector;
87 struct drm_connector *connector;
89 np_connector = of_graph_get_remote_port_parent(node);
90 if (!np_connector || !of_device_is_available(np_connector))
93 drm_for_each_connector(connector, drm_dev) {
94 if (connector->port == np_connector)
101 static int init_loader_memory(struct drm_device *drm_dev)
103 struct rockchip_drm_private *private = drm_dev->dev_private;
104 struct rockchip_logo *logo;
105 struct device_node *np = drm_dev->dev->of_node;
106 struct device_node *node;
107 unsigned long nr_pages;
109 struct sg_table *sgt;
110 DEFINE_DMA_ATTRS(attrs);
111 phys_addr_t start, size;
115 logo = devm_kmalloc(drm_dev->dev, sizeof(*logo), GFP_KERNEL);
119 node = of_parse_phandle(np, "memory-region", 0);
123 ret = of_address_to_resource(node, 0, &res);
127 size = resource_size(&res);
131 nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
132 pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL);
136 while (i < nr_pages) {
137 pages[i] = phys_to_page(start);
141 sgt = drm_prime_pages_to_sg(pages, nr_pages);
147 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
148 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
149 dma_map_sg_attrs(drm_dev->dev, sgt->sgl, sgt->nents,
150 DMA_TO_DEVICE, &attrs);
151 logo->dma_addr = sg_dma_address(sgt->sgl);
153 logo->start = res.start;
156 private->logo = logo;
161 static struct drm_framebuffer *
162 get_framebuffer_by_node(struct drm_device *drm_dev, struct device_node *node)
164 struct rockchip_drm_private *private = drm_dev->dev_private;
165 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
169 if (WARN_ON(!private->logo))
172 if (of_property_read_u32(node, "logo,offset", &val)) {
173 pr_err("%s: failed to get logo,offset\n", __func__);
176 mode_cmd.offsets[0] = val;
178 if (of_property_read_u32(node, "logo,width", &val)) {
179 pr_err("%s: failed to get logo,width\n", __func__);
182 mode_cmd.width = val;
184 if (of_property_read_u32(node, "logo,height", &val)) {
185 pr_err("%s: failed to get logo,height\n", __func__);
188 mode_cmd.height = val;
190 if (of_property_read_u32(node, "logo,bpp", &val)) {
191 pr_err("%s: failed to get logo,bpp\n", __func__);
196 mode_cmd.pitches[0] = mode_cmd.width * bpp / 8;
200 mode_cmd.pixel_format = DRM_FORMAT_BGR565;
203 mode_cmd.pixel_format = DRM_FORMAT_BGR888;
206 mode_cmd.pixel_format = DRM_FORMAT_XBGR8888;
209 pr_err("%s: unsupport to logo bpp %d\n", __func__, bpp);
213 return rockchip_fb_alloc(drm_dev, &mode_cmd, NULL, private->logo, 1);
216 static struct rockchip_drm_mode_set *
217 of_parse_display_resource(struct drm_device *drm_dev, struct device_node *route)
219 struct rockchip_drm_mode_set *set;
220 struct device_node *connect;
221 struct drm_framebuffer *fb;
222 struct drm_connector *connector;
223 struct drm_crtc *crtc;
226 connect = of_parse_phandle(route, "connect", 0);
230 fb = get_framebuffer_by_node(drm_dev, route);
231 if (IS_ERR_OR_NULL(fb))
234 crtc = find_crtc_by_node(drm_dev, connect);
235 connector = find_connector_by_node(drm_dev, connect);
236 if (!crtc || !connector) {
237 dev_warn(drm_dev->dev,
238 "No available crtc or connector for display");
239 drm_framebuffer_unreference(fb);
243 set = kzalloc(sizeof(*set), GFP_KERNEL);
247 if (!of_property_read_u32(route, "video,hdisplay", &val))
250 if (!of_property_read_u32(route, "video,vdisplay", &val))
253 if (!of_property_read_u32(route, "video,vrefresh", &val))
256 if (!of_property_read_u32(route, "logo,ymirror", &val))
261 set->connector = connector;
262 /* TODO: set display fullscreen or center */
268 int setup_initial_state(struct drm_device *drm_dev,
269 struct drm_atomic_state *state,
270 struct rockchip_drm_mode_set *set)
272 struct drm_connector *connector = set->connector;
273 struct drm_crtc *crtc = set->crtc;
274 struct drm_crtc_state *crtc_state;
275 struct drm_connector_state *conn_state;
276 struct drm_plane_state *primary_state;
277 struct drm_display_mode *mode = NULL;
278 const struct drm_connector_helper_funcs *funcs;
279 bool is_crtc_enabled = true;
280 int hdisplay, vdisplay;
281 int fb_width, fb_height;
282 int found = 0, match = 0;
286 if (!set->hdisplay || !set->vdisplay || !set->vrefresh)
287 is_crtc_enabled = false;
289 conn_state = drm_atomic_get_connector_state(state, connector);
290 if (IS_ERR(conn_state))
291 return PTR_ERR(conn_state);
293 funcs = connector->helper_private;
294 conn_state->best_encoder = funcs->best_encoder(connector);
295 num_modes = connector->funcs->fill_modes(connector, 4096, 4096);
297 dev_err(drm_dev->dev, "connector[%s] can't found any modes\n",
302 list_for_each_entry(mode, &connector->modes, head) {
303 if (mode->hdisplay == set->hdisplay &&
304 mode->vdisplay == set->vdisplay &&
305 drm_mode_vrefresh(mode) == set->vrefresh) {
313 list_for_each_entry(mode, &connector->modes, head) {
314 if (mode->type & DRM_MODE_TYPE_PREFERRED) {
321 mode = list_first_entry_or_null(&connector->modes,
322 struct drm_display_mode,
325 pr_err("failed to find available modes\n");
332 crtc_state = drm_atomic_get_crtc_state(state, crtc);
333 if (IS_ERR(crtc_state))
334 return PTR_ERR(crtc_state);
336 drm_mode_copy(&crtc_state->adjusted_mode, mode);
337 if (!match || !is_crtc_enabled) {
338 set->mode_changed = true;
340 ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
344 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
348 crtc_state->active = true;
353 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
354 if (IS_ERR(primary_state))
355 return PTR_ERR(primary_state);
357 hdisplay = mode->hdisplay;
358 vdisplay = mode->vdisplay;
359 fb_width = set->fb->width;
360 fb_height = set->fb->height;
362 primary_state->crtc = crtc;
363 primary_state->src_x = 0;
364 primary_state->src_y = 0;
365 primary_state->src_w = fb_width << 16;
366 primary_state->src_h = fb_height << 16;
368 if (set->fb->width >= hdisplay) {
369 primary_state->crtc_x = 0;
370 primary_state->crtc_w = hdisplay;
372 primary_state->crtc_x = (hdisplay - fb_width) / 2;
373 primary_state->crtc_w = set->fb->width;
376 if (set->fb->height >= vdisplay) {
377 primary_state->crtc_y = 0;
378 primary_state->crtc_h = vdisplay;
380 primary_state->crtc_y = (vdisplay - fb_height) / 2;
381 primary_state->crtc_h = fb_height;
384 primary_state->crtc_x = 0;
385 primary_state->crtc_y = 0;
386 primary_state->crtc_w = hdisplay;
387 primary_state->crtc_h = vdisplay;
393 static int update_state(struct drm_device *drm_dev,
394 struct drm_atomic_state *state,
395 struct rockchip_drm_mode_set *set,
396 unsigned int *plane_mask)
398 struct drm_mode_config *mode_config = &drm_dev->mode_config;
399 struct drm_crtc *crtc = set->crtc;
400 struct drm_connector *connector = set->connector;
401 struct drm_display_mode *mode = set->mode;
402 struct drm_plane_state *primary_state;
403 struct drm_crtc_state *crtc_state;
404 struct drm_connector_state *conn_state;
407 crtc_state = drm_atomic_get_crtc_state(state, crtc);
408 if (IS_ERR(crtc_state))
409 return PTR_ERR(crtc_state);
410 conn_state = drm_atomic_get_connector_state(state, connector);
411 if (IS_ERR(conn_state))
412 return PTR_ERR(conn_state);
414 if (set->mode_changed) {
415 ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
419 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
423 crtc_state->active = true;
425 const struct drm_crtc_helper_funcs *funcs;
427 funcs = crtc->helper_private;
428 if (!funcs || !funcs->enable)
433 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
434 if (IS_ERR(primary_state))
435 return PTR_ERR(primary_state);
437 crtc_state->plane_mask = 1 << drm_plane_index(crtc->primary);
438 *plane_mask |= crtc_state->plane_mask;
440 drm_atomic_set_fb_for_plane(primary_state, set->fb);
441 drm_framebuffer_unreference(set->fb);
442 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
447 * some vop maybe not support ymirror, but force use it now.
449 drm_atomic_plane_set_property(crtc->primary, primary_state,
450 mode_config->rotation_property,
456 static void show_loader_logo(struct drm_device *drm_dev)
458 struct drm_atomic_state *state;
459 struct device_node *np = drm_dev->dev->of_node;
460 struct drm_mode_config *mode_config = &drm_dev->mode_config;
461 struct device_node *root, *route;
462 struct rockchip_drm_mode_set *set, *tmp;
463 struct list_head mode_set_list;
464 unsigned plane_mask = 0;
467 root = of_get_child_by_name(np, "route");
469 dev_warn(drm_dev->dev, "failed to parse display resources\n");
473 if (init_loader_memory(drm_dev)) {
474 dev_warn(drm_dev->dev, "failed to parse loader memory\n");
478 INIT_LIST_HEAD(&mode_set_list);
479 drm_modeset_lock_all(drm_dev);
480 state = drm_atomic_state_alloc(drm_dev);
482 dev_err(drm_dev->dev, "failed to alloc atomic state\n");
487 state->acquire_ctx = mode_config->acquire_ctx;
489 for_each_child_of_node(root, route) {
490 set = of_parse_display_resource(drm_dev, route);
494 if (setup_initial_state(drm_dev, state, set)) {
495 drm_framebuffer_unreference(set->fb);
499 INIT_LIST_HEAD(&set->head);
500 list_add_tail(&set->head, &mode_set_list);
503 if (list_empty(&mode_set_list)) {
504 dev_warn(drm_dev->dev, "can't not find any loader display\n");
510 * The state save initial devices status, swap the state into
511 * drm deivces as old state, so if new state come, can compare
512 * with this state to judge which status need to update.
514 drm_atomic_helper_swap_state(drm_dev, state);
515 drm_atomic_state_free(state);
516 state = drm_atomic_helper_duplicate_state(drm_dev,
517 mode_config->acquire_ctx);
519 dev_err(drm_dev->dev, "failed to duplicate atomic state\n");
520 ret = PTR_ERR_OR_ZERO(state);
523 state->acquire_ctx = mode_config->acquire_ctx;
524 list_for_each_entry(set, &mode_set_list, head)
526 * We don't want to see any fail on update_state.
528 WARN_ON(update_state(drm_dev, state, set, &plane_mask));
530 ret = drm_atomic_commit(state);
531 drm_atomic_clean_old_fb(drm_dev, plane_mask, ret);
533 list_for_each_entry_safe(set, tmp, &mode_set_list, head) {
534 struct drm_crtc *crtc = set->crtc;
536 list_del(&set->head);
540 * primary plane state rotation is not BIT(0), but we only want
541 * it effect on logo display, userspace may not known to clean
542 * this property, would get unexpect display, so force set
543 * primary rotation to BIT(0).
545 if (!crtc->primary || !crtc->primary->state)
548 drm_atomic_plane_set_property(crtc->primary,
549 crtc->primary->state,
550 mode_config->rotation_property,
555 * Is possible get deadlock here?
557 WARN_ON(ret == -EDEADLK);
562 drm_modeset_unlock_all(drm_dev);
566 drm_atomic_state_free(state);
568 drm_modeset_unlock_all(drm_dev);
570 dev_err(drm_dev->dev, "failed to show loader logo\n");
574 * Attach a (component) device to the shared drm dma mapping from master drm
575 * device. This is used by the VOPs to map GEM buffers to a common DMA
578 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
581 struct rockchip_drm_private *private = drm_dev->dev_private;
582 struct iommu_domain *domain = private->domain;
585 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
589 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
590 ret = iommu_attach_device(domain, dev);
592 dev_err(dev, "Failed to attach iommu device\n");
596 if (!common_iommu_setup_dma_ops(dev, 0x10000000, SZ_2G, domain->ops)) {
597 dev_err(dev, "Failed to set dma_ops\n");
598 iommu_detach_device(domain, dev);
605 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
608 struct rockchip_drm_private *private = drm_dev->dev_private;
609 struct iommu_domain *domain = private->domain;
611 iommu_detach_device(domain, dev);
614 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
615 const struct rockchip_crtc_funcs *crtc_funcs)
617 int pipe = drm_crtc_index(crtc);
618 struct rockchip_drm_private *priv = crtc->dev->dev_private;
620 if (pipe > ROCKCHIP_MAX_CRTC)
623 priv->crtc_funcs[pipe] = crtc_funcs;
628 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
630 int pipe = drm_crtc_index(crtc);
631 struct rockchip_drm_private *priv = crtc->dev->dev_private;
633 if (pipe > ROCKCHIP_MAX_CRTC)
636 priv->crtc_funcs[pipe] = NULL;
639 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
642 struct drm_crtc *crtc;
645 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
652 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
655 struct rockchip_drm_private *priv = dev->dev_private;
656 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
658 if (crtc && priv->crtc_funcs[pipe] &&
659 priv->crtc_funcs[pipe]->enable_vblank)
660 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
665 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
668 struct rockchip_drm_private *priv = dev->dev_private;
669 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
671 if (crtc && priv->crtc_funcs[pipe] &&
672 priv->crtc_funcs[pipe]->enable_vblank)
673 priv->crtc_funcs[pipe]->disable_vblank(crtc);
676 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
678 struct rockchip_drm_private *private;
679 struct device *dev = drm_dev->dev;
680 struct drm_connector *connector;
681 struct iommu_group *group;
684 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
688 mutex_init(&private->commit.lock);
689 INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
691 drm_dev->dev_private = private;
693 #ifdef CONFIG_DRM_DMA_SYNC
694 private->cpu_fence_context = fence_context_alloc(1);
695 atomic_set(&private->cpu_fence_seqno, 0);
698 drm_mode_config_init(drm_dev);
700 rockchip_drm_mode_config_init(drm_dev);
702 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
704 if (!dev->dma_parms) {
706 goto err_config_cleanup;
709 private->domain = iommu_domain_alloc(&platform_bus_type);
710 if (!private->domain)
713 ret = iommu_get_dma_cookie(private->domain);
715 goto err_free_domain;
717 group = iommu_group_get(dev);
719 group = iommu_group_alloc();
721 dev_err(dev, "Failed to allocate IOMMU group\n");
725 ret = iommu_group_add_device(group, dev);
726 iommu_group_put(group);
728 dev_err(dev, "failed to add device to IOMMU group\n");
733 * Attach virtual iommu device, sub iommu device can share the same
736 ret = rockchip_drm_dma_attach_device(drm_dev, dev);
738 goto err_group_remove_device;
740 /* Try to bind all sub drivers. */
741 ret = component_bind_all(dev, drm_dev);
743 goto err_detach_device;
746 * All components are now added, we can publish the connector sysfs
747 * entries to userspace. This will generate hotplug events and so
748 * userspace will expect to be able to access DRM at this point.
750 list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
752 ret = drm_connector_register(connector);
754 dev_err(drm_dev->dev,
755 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
757 connector->name, ret);
762 /* init kms poll for handling hpd */
763 drm_kms_helper_poll_init(drm_dev);
766 * enable drm irq mode.
767 * - with irq_enabled = true, we can use the vblank feature.
769 drm_dev->irq_enabled = true;
771 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
773 goto err_kms_helper_poll_fini;
776 * with vblank_disable_allowed = true, vblank interrupt will be disabled
777 * by drm timer once a current process gives up ownership of
778 * vblank event.(after drm_vblank_put function is called)
780 drm_dev->vblank_disable_allowed = true;
782 drm_mode_config_reset(drm_dev);
784 show_loader_logo(drm_dev);
786 ret = rockchip_drm_fbdev_init(drm_dev);
788 goto err_vblank_cleanup;
790 drm_dev->mode_config.allow_fb_modifiers = true;
794 drm_vblank_cleanup(drm_dev);
795 err_kms_helper_poll_fini:
796 drm_kms_helper_poll_fini(drm_dev);
798 component_unbind_all(dev, drm_dev);
800 rockchip_drm_dma_detach_device(drm_dev, dev);
801 err_group_remove_device:
802 iommu_group_remove_device(dev);
804 iommu_put_dma_cookie(private->domain);
806 iommu_domain_free(private->domain);
808 drm_mode_config_cleanup(drm_dev);
809 drm_dev->dev_private = NULL;
813 static int rockchip_drm_unload(struct drm_device *drm_dev)
815 struct device *dev = drm_dev->dev;
816 struct rockchip_drm_private *private = drm_dev->dev_private;
818 rockchip_drm_fbdev_fini(drm_dev);
819 drm_vblank_cleanup(drm_dev);
820 drm_kms_helper_poll_fini(drm_dev);
821 component_unbind_all(dev, drm_dev);
822 rockchip_drm_dma_detach_device(drm_dev, dev);
823 iommu_group_remove_device(dev);
824 iommu_put_dma_cookie(private->domain);
825 iommu_domain_free(private->domain);
826 drm_mode_config_cleanup(drm_dev);
827 drm_dev->dev_private = NULL;
832 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
833 struct drm_file *file_priv)
835 struct rockchip_drm_private *priv = crtc->dev->dev_private;
836 int pipe = drm_crtc_index(crtc);
838 if (pipe < ROCKCHIP_MAX_CRTC &&
839 priv->crtc_funcs[pipe] &&
840 priv->crtc_funcs[pipe]->cancel_pending_vblank)
841 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
844 int rockchip_drm_register_subdrv(struct drm_rockchip_subdrv *subdrv)
849 mutex_lock(&subdrv_list_mutex);
850 list_add_tail(&subdrv->list, &rockchip_drm_subdrv_list);
851 mutex_unlock(&subdrv_list_mutex);
855 EXPORT_SYMBOL_GPL(rockchip_drm_register_subdrv);
857 int rockchip_drm_unregister_subdrv(struct drm_rockchip_subdrv *subdrv)
862 mutex_lock(&subdrv_list_mutex);
863 list_del(&subdrv->list);
864 mutex_unlock(&subdrv_list_mutex);
868 EXPORT_SYMBOL_GPL(rockchip_drm_unregister_subdrv);
870 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
872 struct rockchip_drm_file_private *file_priv;
873 struct drm_rockchip_subdrv *subdrv;
876 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
879 INIT_LIST_HEAD(&file_priv->gem_cpu_acquire_list);
881 file->driver_priv = file_priv;
883 mutex_lock(&subdrv_list_mutex);
884 list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list) {
885 ret = subdrv->open(dev, subdrv->dev, file);
887 mutex_unlock(&subdrv_list_mutex);
888 goto err_free_file_priv;
891 mutex_unlock(&subdrv_list_mutex);
902 static void rockchip_drm_preclose(struct drm_device *dev,
903 struct drm_file *file_priv)
905 struct rockchip_drm_file_private *file_private = file_priv->driver_priv;
906 struct rockchip_gem_object_node *cur, *d;
907 struct drm_rockchip_subdrv *subdrv;
908 struct drm_crtc *crtc;
910 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
911 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
913 mutex_lock(&dev->struct_mutex);
914 list_for_each_entry_safe(cur, d,
915 &file_private->gem_cpu_acquire_list, list) {
916 #ifdef CONFIG_DRM_DMA_SYNC
917 BUG_ON(!cur->rockchip_gem_obj->acquire_fence);
918 drm_fence_signal_and_put(&cur->rockchip_gem_obj->acquire_fence);
920 drm_gem_object_unreference(&cur->rockchip_gem_obj->base);
923 /* since we are deleting the whole list, just initialize the header
924 * instead of calling list_del for every element
926 INIT_LIST_HEAD(&file_private->gem_cpu_acquire_list);
927 mutex_unlock(&dev->struct_mutex);
929 mutex_lock(&subdrv_list_mutex);
930 list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list)
931 subdrv->close(dev, subdrv->dev, file_priv);
932 mutex_unlock(&subdrv_list_mutex);
935 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
937 kfree(file->driver_priv);
938 file->driver_priv = NULL;
941 void rockchip_drm_lastclose(struct drm_device *dev)
943 struct rockchip_drm_private *priv = dev->dev_private;
945 drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev_helper);
948 static const struct drm_ioctl_desc rockchip_ioctls[] = {
949 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_gem_create_ioctl,
950 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
951 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
952 rockchip_gem_map_offset_ioctl,
953 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
954 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_ACQUIRE,
955 rockchip_gem_cpu_acquire_ioctl,
956 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
957 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_RELEASE,
958 rockchip_gem_cpu_release_ioctl,
959 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
960 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_GET_VER, rockchip_rga_get_ver_ioctl,
961 DRM_AUTH | DRM_RENDER_ALLOW),
962 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_SET_CMDLIST,
963 rockchip_rga_set_cmdlist_ioctl,
964 DRM_AUTH | DRM_RENDER_ALLOW),
965 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_EXEC, rockchip_rga_exec_ioctl,
966 DRM_AUTH | DRM_RENDER_ALLOW),
969 static const struct file_operations rockchip_drm_driver_fops = {
970 .owner = THIS_MODULE,
972 .mmap = rockchip_gem_mmap,
975 .unlocked_ioctl = drm_ioctl,
977 .compat_ioctl = drm_compat_ioctl,
979 .release = drm_release,
982 const struct vm_operations_struct rockchip_drm_vm_ops = {
983 .open = drm_gem_vm_open,
984 .close = drm_gem_vm_close,
987 static struct drm_driver rockchip_drm_driver = {
988 .driver_features = DRIVER_MODESET | DRIVER_GEM |
989 DRIVER_PRIME | DRIVER_ATOMIC |
991 .load = rockchip_drm_load,
992 .unload = rockchip_drm_unload,
993 .preclose = rockchip_drm_preclose,
994 .lastclose = rockchip_drm_lastclose,
995 .get_vblank_counter = drm_vblank_no_hw_counter,
996 .open = rockchip_drm_open,
997 .postclose = rockchip_drm_postclose,
998 .enable_vblank = rockchip_drm_crtc_enable_vblank,
999 .disable_vblank = rockchip_drm_crtc_disable_vblank,
1000 .gem_vm_ops = &rockchip_drm_vm_ops,
1001 .gem_free_object = rockchip_gem_free_object,
1002 .dumb_create = rockchip_gem_dumb_create,
1003 .dumb_map_offset = rockchip_gem_dumb_map_offset,
1004 .dumb_destroy = drm_gem_dumb_destroy,
1005 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
1006 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
1007 .gem_prime_import = drm_gem_prime_import,
1008 .gem_prime_import_sg_table = rockchip_gem_prime_import_sg_table,
1009 .gem_prime_export = drm_gem_prime_export,
1010 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
1011 .gem_prime_vmap = rockchip_gem_prime_vmap,
1012 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
1013 .gem_prime_mmap = rockchip_gem_mmap_buf,
1014 .ioctls = rockchip_ioctls,
1015 .num_ioctls = ARRAY_SIZE(rockchip_ioctls),
1016 .fops = &rockchip_drm_driver_fops,
1017 .name = DRIVER_NAME,
1018 .desc = DRIVER_DESC,
1019 .date = DRIVER_DATE,
1020 .major = DRIVER_MAJOR,
1021 .minor = DRIVER_MINOR,
1024 #ifdef CONFIG_PM_SLEEP
1025 void rockchip_drm_fb_suspend(struct drm_device *drm)
1027 struct rockchip_drm_private *priv = drm->dev_private;
1030 drm_fb_helper_set_suspend(priv->fbdev_helper, 1);
1034 void rockchip_drm_fb_resume(struct drm_device *drm)
1036 struct rockchip_drm_private *priv = drm->dev_private;
1039 drm_fb_helper_set_suspend(priv->fbdev_helper, 0);
1043 static int rockchip_drm_sys_suspend(struct device *dev)
1045 struct drm_device *drm = dev_get_drvdata(dev);
1046 struct rockchip_drm_private *priv = drm->dev_private;
1048 drm_kms_helper_poll_disable(drm);
1049 rockchip_drm_fb_suspend(drm);
1051 priv->state = drm_atomic_helper_suspend(drm);
1052 if (IS_ERR(priv->state)) {
1053 rockchip_drm_fb_resume(drm);
1054 drm_kms_helper_poll_enable(drm);
1055 return PTR_ERR(priv->state);
1061 static int rockchip_drm_sys_resume(struct device *dev)
1063 struct drm_device *drm = dev_get_drvdata(dev);
1064 struct rockchip_drm_private *priv = drm->dev_private;
1066 drm_atomic_helper_resume(drm, priv->state);
1067 rockchip_drm_fb_resume(drm);
1068 drm_kms_helper_poll_enable(drm);
1074 static const struct dev_pm_ops rockchip_drm_pm_ops = {
1075 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
1076 rockchip_drm_sys_resume)
1079 static int compare_of(struct device *dev, void *data)
1081 struct device_node *np = data;
1083 return dev->of_node == np;
1086 static void rockchip_add_endpoints(struct device *dev,
1087 struct component_match **match,
1088 struct device_node *port)
1090 struct device_node *ep, *remote;
1092 for_each_child_of_node(port, ep) {
1093 remote = of_graph_get_remote_port_parent(ep);
1094 if (!remote || !of_device_is_available(remote)) {
1095 of_node_put(remote);
1097 } else if (!of_device_is_available(remote->parent)) {
1098 dev_warn(dev, "parent device of %s is not available\n",
1100 of_node_put(remote);
1104 component_match_add(dev, match, compare_of, remote);
1105 of_node_put(remote);
1109 static int rockchip_drm_bind(struct device *dev)
1111 struct drm_device *drm;
1114 drm = drm_dev_alloc(&rockchip_drm_driver, dev);
1118 ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
1122 ret = drm_dev_register(drm, 0);
1126 dev_set_drvdata(dev, drm);
1135 static void rockchip_drm_unbind(struct device *dev)
1137 struct drm_device *drm = dev_get_drvdata(dev);
1139 drm_dev_unregister(drm);
1141 dev_set_drvdata(dev, NULL);
1144 static const struct component_master_ops rockchip_drm_ops = {
1145 .bind = rockchip_drm_bind,
1146 .unbind = rockchip_drm_unbind,
1149 static int rockchip_drm_platform_probe(struct platform_device *pdev)
1151 struct device *dev = &pdev->dev;
1152 struct component_match *match = NULL;
1153 struct device_node *np = dev->of_node;
1154 struct device_node *port;
1160 * Bind the crtc ports first, so that
1161 * drm_of_find_possible_crtcs called from encoder .bind callbacks
1162 * works as expected.
1165 port = of_parse_phandle(np, "ports", i);
1169 if (!of_device_is_available(port->parent)) {
1174 component_match_add(dev, &match, compare_of, port->parent);
1179 dev_err(dev, "missing 'ports' property\n");
1184 dev_err(dev, "No available vop found for display-subsystem.\n");
1188 * For each bound crtc, bind the encoders attached to its
1192 port = of_parse_phandle(np, "ports", i);
1196 if (!of_device_is_available(port->parent)) {
1201 rockchip_add_endpoints(dev, &match, port);
1205 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
1208 static int rockchip_drm_platform_remove(struct platform_device *pdev)
1210 component_master_del(&pdev->dev, &rockchip_drm_ops);
1215 static const struct of_device_id rockchip_drm_dt_ids[] = {
1216 { .compatible = "rockchip,display-subsystem", },
1219 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
1221 static struct platform_driver rockchip_drm_platform_driver = {
1222 .probe = rockchip_drm_platform_probe,
1223 .remove = rockchip_drm_platform_remove,
1225 .name = "rockchip-drm",
1226 .of_match_table = rockchip_drm_dt_ids,
1227 .pm = &rockchip_drm_pm_ops,
1231 module_platform_driver(rockchip_drm_platform_driver);
1233 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
1234 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
1235 MODULE_LICENSE("GPL v2");