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_crtc_helper.h>
21 #include <drm/drm_fb_helper.h>
22 #include <drm/drm_sync_helper.h>
23 #include <drm/rockchip_drm.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/module.h>
27 #include <linux/of_graph.h>
28 #include <linux/component.h>
29 #include <linux/fence.h>
30 #include <linux/console.h>
32 #include "rockchip_drm_drv.h"
33 #include "rockchip_drm_fb.h"
34 #include "rockchip_drm_fbdev.h"
35 #include "rockchip_drm_gem.h"
36 #include "rockchip_drm_rga.h"
38 #define DRIVER_NAME "rockchip"
39 #define DRIVER_DESC "RockChip Soc DRM"
40 #define DRIVER_DATE "20140818"
41 #define DRIVER_MAJOR 1
42 #define DRIVER_MINOR 0
44 static LIST_HEAD(rockchip_drm_subdrv_list);
45 static DEFINE_MUTEX(subdrv_list_mutex);
48 * Attach a (component) device to the shared drm dma mapping from master drm
49 * device. This is used by the VOPs to map GEM buffers to a common DMA
52 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
55 struct rockchip_drm_private *private = drm_dev->dev_private;
56 struct iommu_domain *domain = private->domain;
59 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
63 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
64 ret = iommu_attach_device(domain, dev);
66 dev_err(dev, "Failed to attach iommu device\n");
70 if (!common_iommu_setup_dma_ops(dev, 0x10000000, SZ_2G, domain->ops)) {
71 dev_err(dev, "Failed to set dma_ops\n");
72 iommu_detach_device(domain, dev);
79 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
82 struct rockchip_drm_private *private = drm_dev->dev_private;
83 struct iommu_domain *domain = private->domain;
85 iommu_detach_device(domain, dev);
88 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
89 const struct rockchip_crtc_funcs *crtc_funcs)
91 int pipe = drm_crtc_index(crtc);
92 struct rockchip_drm_private *priv = crtc->dev->dev_private;
94 if (pipe > ROCKCHIP_MAX_CRTC)
97 priv->crtc_funcs[pipe] = crtc_funcs;
102 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
104 int pipe = drm_crtc_index(crtc);
105 struct rockchip_drm_private *priv = crtc->dev->dev_private;
107 if (pipe > ROCKCHIP_MAX_CRTC)
110 priv->crtc_funcs[pipe] = NULL;
113 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
116 struct drm_crtc *crtc;
119 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
126 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
129 struct rockchip_drm_private *priv = dev->dev_private;
130 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
132 if (crtc && priv->crtc_funcs[pipe] &&
133 priv->crtc_funcs[pipe]->enable_vblank)
134 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
139 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
142 struct rockchip_drm_private *priv = dev->dev_private;
143 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
145 if (crtc && priv->crtc_funcs[pipe] &&
146 priv->crtc_funcs[pipe]->enable_vblank)
147 priv->crtc_funcs[pipe]->disable_vblank(crtc);
150 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
152 struct rockchip_drm_private *private;
153 struct device *dev = drm_dev->dev;
154 struct drm_connector *connector;
155 struct iommu_group *group;
158 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
162 mutex_init(&private->commit.lock);
163 INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
165 drm_dev->dev_private = private;
167 #ifdef CONFIG_DRM_DMA_SYNC
168 private->cpu_fence_context = fence_context_alloc(1);
169 atomic_set(&private->cpu_fence_seqno, 0);
172 drm_mode_config_init(drm_dev);
174 rockchip_drm_mode_config_init(drm_dev);
176 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
178 if (!dev->dma_parms) {
180 goto err_config_cleanup;
183 private->domain = iommu_domain_alloc(&platform_bus_type);
184 if (!private->domain)
187 ret = iommu_get_dma_cookie(private->domain);
189 goto err_free_domain;
191 group = iommu_group_get(dev);
193 group = iommu_group_alloc();
195 dev_err(dev, "Failed to allocate IOMMU group\n");
199 ret = iommu_group_add_device(group, dev);
200 iommu_group_put(group);
202 dev_err(dev, "failed to add device to IOMMU group\n");
207 * Attach virtual iommu device, sub iommu device can share the same
210 ret = rockchip_drm_dma_attach_device(drm_dev, dev);
212 goto err_group_remove_device;
214 /* Try to bind all sub drivers. */
215 ret = component_bind_all(dev, drm_dev);
217 goto err_detach_device;
220 * All components are now added, we can publish the connector sysfs
221 * entries to userspace. This will generate hotplug events and so
222 * userspace will expect to be able to access DRM at this point.
224 list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
226 ret = drm_connector_register(connector);
228 dev_err(drm_dev->dev,
229 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
231 connector->name, ret);
236 /* init kms poll for handling hpd */
237 drm_kms_helper_poll_init(drm_dev);
240 * enable drm irq mode.
241 * - with irq_enabled = true, we can use the vblank feature.
243 drm_dev->irq_enabled = true;
245 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
247 goto err_kms_helper_poll_fini;
250 * with vblank_disable_allowed = true, vblank interrupt will be disabled
251 * by drm timer once a current process gives up ownership of
252 * vblank event.(after drm_vblank_put function is called)
254 drm_dev->vblank_disable_allowed = true;
256 drm_mode_config_reset(drm_dev);
258 ret = rockchip_drm_fbdev_init(drm_dev);
260 goto err_vblank_cleanup;
264 drm_vblank_cleanup(drm_dev);
265 err_kms_helper_poll_fini:
266 drm_kms_helper_poll_fini(drm_dev);
268 component_unbind_all(dev, drm_dev);
270 rockchip_drm_dma_detach_device(drm_dev, dev);
271 err_group_remove_device:
272 iommu_group_remove_device(dev);
274 iommu_put_dma_cookie(private->domain);
276 iommu_domain_free(private->domain);
278 drm_mode_config_cleanup(drm_dev);
279 drm_dev->dev_private = NULL;
283 static int rockchip_drm_unload(struct drm_device *drm_dev)
285 struct device *dev = drm_dev->dev;
286 struct rockchip_drm_private *private = drm_dev->dev_private;
288 rockchip_drm_fbdev_fini(drm_dev);
289 drm_vblank_cleanup(drm_dev);
290 drm_kms_helper_poll_fini(drm_dev);
291 component_unbind_all(dev, drm_dev);
292 rockchip_drm_dma_detach_device(drm_dev, dev);
293 iommu_group_remove_device(dev);
294 iommu_put_dma_cookie(private->domain);
295 iommu_domain_free(private->domain);
296 drm_mode_config_cleanup(drm_dev);
297 drm_dev->dev_private = NULL;
302 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
303 struct drm_file *file_priv)
305 struct rockchip_drm_private *priv = crtc->dev->dev_private;
306 int pipe = drm_crtc_index(crtc);
308 if (pipe < ROCKCHIP_MAX_CRTC &&
309 priv->crtc_funcs[pipe] &&
310 priv->crtc_funcs[pipe]->cancel_pending_vblank)
311 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
314 int rockchip_drm_register_subdrv(struct drm_rockchip_subdrv *subdrv)
319 mutex_lock(&subdrv_list_mutex);
320 list_add_tail(&subdrv->list, &rockchip_drm_subdrv_list);
321 mutex_unlock(&subdrv_list_mutex);
325 EXPORT_SYMBOL_GPL(rockchip_drm_register_subdrv);
327 int rockchip_drm_unregister_subdrv(struct drm_rockchip_subdrv *subdrv)
332 mutex_lock(&subdrv_list_mutex);
333 list_del(&subdrv->list);
334 mutex_unlock(&subdrv_list_mutex);
338 EXPORT_SYMBOL_GPL(rockchip_drm_unregister_subdrv);
340 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
342 struct rockchip_drm_file_private *file_priv;
343 struct drm_rockchip_subdrv *subdrv;
346 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
349 INIT_LIST_HEAD(&file_priv->gem_cpu_acquire_list);
351 file->driver_priv = file_priv;
353 mutex_lock(&subdrv_list_mutex);
354 list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list) {
355 ret = subdrv->open(dev, subdrv->dev, file);
357 mutex_unlock(&subdrv_list_mutex);
358 goto err_free_file_priv;
361 mutex_unlock(&subdrv_list_mutex);
372 static void rockchip_drm_preclose(struct drm_device *dev,
373 struct drm_file *file_priv)
375 struct rockchip_drm_file_private *file_private = file_priv->driver_priv;
376 struct rockchip_gem_object_node *cur, *d;
377 struct drm_rockchip_subdrv *subdrv;
378 struct drm_crtc *crtc;
380 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
381 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
383 mutex_lock(&dev->struct_mutex);
384 list_for_each_entry_safe(cur, d,
385 &file_private->gem_cpu_acquire_list, list) {
386 #ifdef CONFIG_DRM_DMA_SYNC
387 BUG_ON(!cur->rockchip_gem_obj->acquire_fence);
388 drm_fence_signal_and_put(&cur->rockchip_gem_obj->acquire_fence);
390 drm_gem_object_unreference(&cur->rockchip_gem_obj->base);
393 /* since we are deleting the whole list, just initialize the header
394 * instead of calling list_del for every element
396 INIT_LIST_HEAD(&file_private->gem_cpu_acquire_list);
397 mutex_unlock(&dev->struct_mutex);
399 mutex_lock(&subdrv_list_mutex);
400 list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list)
401 subdrv->close(dev, subdrv->dev, file_priv);
402 mutex_unlock(&subdrv_list_mutex);
405 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
407 kfree(file->driver_priv);
408 file->driver_priv = NULL;
411 void rockchip_drm_lastclose(struct drm_device *dev)
413 struct rockchip_drm_private *priv = dev->dev_private;
415 drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev_helper);
418 static const struct drm_ioctl_desc rockchip_ioctls[] = {
419 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_gem_create_ioctl,
420 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
421 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
422 rockchip_gem_map_offset_ioctl,
423 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
424 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_ACQUIRE,
425 rockchip_gem_cpu_acquire_ioctl,
426 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
427 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_RELEASE,
428 rockchip_gem_cpu_release_ioctl,
429 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
430 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_GET_VER, rockchip_rga_get_ver_ioctl,
431 DRM_AUTH | DRM_RENDER_ALLOW),
432 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_SET_CMDLIST,
433 rockchip_rga_set_cmdlist_ioctl,
434 DRM_AUTH | DRM_RENDER_ALLOW),
435 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_EXEC, rockchip_rga_exec_ioctl,
436 DRM_AUTH | DRM_RENDER_ALLOW),
439 static const struct file_operations rockchip_drm_driver_fops = {
440 .owner = THIS_MODULE,
442 .mmap = rockchip_gem_mmap,
445 .unlocked_ioctl = drm_ioctl,
447 .compat_ioctl = drm_compat_ioctl,
449 .release = drm_release,
452 const struct vm_operations_struct rockchip_drm_vm_ops = {
453 .open = drm_gem_vm_open,
454 .close = drm_gem_vm_close,
457 static struct drm_driver rockchip_drm_driver = {
458 .driver_features = DRIVER_MODESET | DRIVER_GEM |
459 DRIVER_PRIME | DRIVER_ATOMIC |
461 .load = rockchip_drm_load,
462 .unload = rockchip_drm_unload,
463 .preclose = rockchip_drm_preclose,
464 .lastclose = rockchip_drm_lastclose,
465 .get_vblank_counter = drm_vblank_no_hw_counter,
466 .open = rockchip_drm_open,
467 .postclose = rockchip_drm_postclose,
468 .enable_vblank = rockchip_drm_crtc_enable_vblank,
469 .disable_vblank = rockchip_drm_crtc_disable_vblank,
470 .gem_vm_ops = &rockchip_drm_vm_ops,
471 .gem_free_object = rockchip_gem_free_object,
472 .dumb_create = rockchip_gem_dumb_create,
473 .dumb_map_offset = rockchip_gem_dumb_map_offset,
474 .dumb_destroy = drm_gem_dumb_destroy,
475 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
476 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
477 .gem_prime_import = drm_gem_prime_import,
478 .gem_prime_import_sg_table = rockchip_gem_prime_import_sg_table,
479 .gem_prime_export = drm_gem_prime_export,
480 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
481 .gem_prime_vmap = rockchip_gem_prime_vmap,
482 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
483 .gem_prime_mmap = rockchip_gem_mmap_buf,
484 .ioctls = rockchip_ioctls,
485 .num_ioctls = ARRAY_SIZE(rockchip_ioctls),
486 .fops = &rockchip_drm_driver_fops,
490 .major = DRIVER_MAJOR,
491 .minor = DRIVER_MINOR,
494 #ifdef CONFIG_PM_SLEEP
495 void rockchip_drm_fb_suspend(struct drm_device *drm)
497 struct rockchip_drm_private *priv = drm->dev_private;
500 drm_fb_helper_set_suspend(priv->fbdev_helper, 1);
504 void rockchip_drm_fb_resume(struct drm_device *drm)
506 struct rockchip_drm_private *priv = drm->dev_private;
509 drm_fb_helper_set_suspend(priv->fbdev_helper, 0);
513 static int rockchip_drm_sys_suspend(struct device *dev)
515 struct drm_device *drm = dev_get_drvdata(dev);
516 struct rockchip_drm_private *priv = drm->dev_private;
518 drm_kms_helper_poll_disable(drm);
519 rockchip_drm_fb_suspend(drm);
521 priv->state = drm_atomic_helper_suspend(drm);
522 if (IS_ERR(priv->state)) {
523 rockchip_drm_fb_resume(drm);
524 drm_kms_helper_poll_enable(drm);
525 return PTR_ERR(priv->state);
531 static int rockchip_drm_sys_resume(struct device *dev)
533 struct drm_device *drm = dev_get_drvdata(dev);
534 struct rockchip_drm_private *priv = drm->dev_private;
536 drm_atomic_helper_resume(drm, priv->state);
537 rockchip_drm_fb_resume(drm);
538 drm_kms_helper_poll_enable(drm);
544 static const struct dev_pm_ops rockchip_drm_pm_ops = {
545 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
546 rockchip_drm_sys_resume)
549 static int compare_of(struct device *dev, void *data)
551 struct device_node *np = data;
553 return dev->of_node == np;
556 static void rockchip_add_endpoints(struct device *dev,
557 struct component_match **match,
558 struct device_node *port)
560 struct device_node *ep, *remote;
562 for_each_child_of_node(port, ep) {
563 remote = of_graph_get_remote_port_parent(ep);
564 if (!remote || !of_device_is_available(remote)) {
567 } else if (!of_device_is_available(remote->parent)) {
568 dev_warn(dev, "parent device of %s is not available\n",
574 component_match_add(dev, match, compare_of, remote);
579 static int rockchip_drm_bind(struct device *dev)
581 struct drm_device *drm;
584 drm = drm_dev_alloc(&rockchip_drm_driver, dev);
588 ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
592 ret = drm_dev_register(drm, 0);
596 dev_set_drvdata(dev, drm);
605 static void rockchip_drm_unbind(struct device *dev)
607 struct drm_device *drm = dev_get_drvdata(dev);
609 drm_dev_unregister(drm);
611 dev_set_drvdata(dev, NULL);
614 static const struct component_master_ops rockchip_drm_ops = {
615 .bind = rockchip_drm_bind,
616 .unbind = rockchip_drm_unbind,
619 static int rockchip_drm_platform_probe(struct platform_device *pdev)
621 struct device *dev = &pdev->dev;
622 struct component_match *match = NULL;
623 struct device_node *np = dev->of_node;
624 struct device_node *port;
630 * Bind the crtc ports first, so that
631 * drm_of_find_possible_crtcs called from encoder .bind callbacks
635 port = of_parse_phandle(np, "ports", i);
639 if (!of_device_is_available(port->parent)) {
644 component_match_add(dev, &match, compare_of, port->parent);
649 dev_err(dev, "missing 'ports' property\n");
654 dev_err(dev, "No available vop found for display-subsystem.\n");
658 * For each bound crtc, bind the encoders attached to its
662 port = of_parse_phandle(np, "ports", i);
666 if (!of_device_is_available(port->parent)) {
671 rockchip_add_endpoints(dev, &match, port);
675 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
678 static int rockchip_drm_platform_remove(struct platform_device *pdev)
680 component_master_del(&pdev->dev, &rockchip_drm_ops);
685 static const struct of_device_id rockchip_drm_dt_ids[] = {
686 { .compatible = "rockchip,display-subsystem", },
689 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
691 static struct platform_driver rockchip_drm_platform_driver = {
692 .probe = rockchip_drm_platform_probe,
693 .remove = rockchip_drm_platform_remove,
695 .name = "rockchip-drm",
696 .of_match_table = rockchip_drm_dt_ids,
697 .pm = &rockchip_drm_pm_ops,
701 module_platform_driver(rockchip_drm_platform_driver);
703 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
704 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
705 MODULE_LICENSE("GPL v2");