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>
31 #include "rockchip_drm_drv.h"
32 #include "rockchip_drm_fb.h"
33 #include "rockchip_drm_fbdev.h"
34 #include "rockchip_drm_gem.h"
35 #include "rockchip_drm_rga.h"
37 #define DRIVER_NAME "rockchip"
38 #define DRIVER_DESC "RockChip Soc DRM"
39 #define DRIVER_DATE "20140818"
40 #define DRIVER_MAJOR 1
41 #define DRIVER_MINOR 0
43 static LIST_HEAD(rockchip_drm_subdrv_list);
44 static DEFINE_MUTEX(subdrv_list_mutex);
47 * Attach a (component) device to the shared drm dma mapping from master drm
48 * device. This is used by the VOPs to map GEM buffers to a common DMA
51 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
54 struct rockchip_drm_private *private = drm_dev->dev_private;
55 struct iommu_domain *domain = private->domain;
58 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
62 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
63 ret = iommu_attach_device(domain, dev);
65 dev_err(dev, "Failed to attach iommu device\n");
69 if (!common_iommu_setup_dma_ops(dev, 0x10000000, SZ_2G, domain->ops)) {
70 dev_err(dev, "Failed to set dma_ops\n");
71 iommu_detach_device(domain, dev);
78 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
81 struct rockchip_drm_private *private = drm_dev->dev_private;
82 struct iommu_domain *domain = private->domain;
84 iommu_detach_device(domain, dev);
87 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
88 const struct rockchip_crtc_funcs *crtc_funcs)
90 int pipe = drm_crtc_index(crtc);
91 struct rockchip_drm_private *priv = crtc->dev->dev_private;
93 if (pipe > ROCKCHIP_MAX_CRTC)
96 priv->crtc_funcs[pipe] = crtc_funcs;
101 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
103 int pipe = drm_crtc_index(crtc);
104 struct rockchip_drm_private *priv = crtc->dev->dev_private;
106 if (pipe > ROCKCHIP_MAX_CRTC)
109 priv->crtc_funcs[pipe] = NULL;
112 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
115 struct drm_crtc *crtc;
118 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
125 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
128 struct rockchip_drm_private *priv = dev->dev_private;
129 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
131 if (crtc && priv->crtc_funcs[pipe] &&
132 priv->crtc_funcs[pipe]->enable_vblank)
133 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
138 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
141 struct rockchip_drm_private *priv = dev->dev_private;
142 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
144 if (crtc && priv->crtc_funcs[pipe] &&
145 priv->crtc_funcs[pipe]->enable_vblank)
146 priv->crtc_funcs[pipe]->disable_vblank(crtc);
149 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
151 struct rockchip_drm_private *private;
152 struct device *dev = drm_dev->dev;
153 struct drm_connector *connector;
154 struct iommu_group *group;
157 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
161 mutex_init(&private->commit.lock);
162 INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
164 drm_dev->dev_private = private;
166 #ifdef CONFIG_DRM_DMA_SYNC
167 private->cpu_fence_context = fence_context_alloc(1);
168 atomic_set(&private->cpu_fence_seqno, 0);
171 drm_mode_config_init(drm_dev);
173 rockchip_drm_mode_config_init(drm_dev);
175 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
177 if (!dev->dma_parms) {
179 goto err_config_cleanup;
182 private->domain = iommu_domain_alloc(&platform_bus_type);
183 if (!private->domain)
186 ret = iommu_get_dma_cookie(private->domain);
188 goto err_free_domain;
190 group = iommu_group_get(dev);
192 group = iommu_group_alloc();
194 dev_err(dev, "Failed to allocate IOMMU group\n");
198 ret = iommu_group_add_device(group, dev);
199 iommu_group_put(group);
201 dev_err(dev, "failed to add device to IOMMU group\n");
206 * Attach virtual iommu device, sub iommu device can share the same
209 ret = rockchip_drm_dma_attach_device(drm_dev, dev);
211 goto err_group_remove_device;
213 /* Try to bind all sub drivers. */
214 ret = component_bind_all(dev, drm_dev);
216 goto err_detach_device;
219 * All components are now added, we can publish the connector sysfs
220 * entries to userspace. This will generate hotplug events and so
221 * userspace will expect to be able to access DRM at this point.
223 list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
225 ret = drm_connector_register(connector);
227 dev_err(drm_dev->dev,
228 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
230 connector->name, ret);
235 /* init kms poll for handling hpd */
236 drm_kms_helper_poll_init(drm_dev);
239 * enable drm irq mode.
240 * - with irq_enabled = true, we can use the vblank feature.
242 drm_dev->irq_enabled = true;
244 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
246 goto err_kms_helper_poll_fini;
249 * with vblank_disable_allowed = true, vblank interrupt will be disabled
250 * by drm timer once a current process gives up ownership of
251 * vblank event.(after drm_vblank_put function is called)
253 drm_dev->vblank_disable_allowed = true;
255 drm_mode_config_reset(drm_dev);
257 ret = rockchip_drm_fbdev_init(drm_dev);
259 goto err_vblank_cleanup;
263 drm_vblank_cleanup(drm_dev);
264 err_kms_helper_poll_fini:
265 drm_kms_helper_poll_fini(drm_dev);
267 component_unbind_all(dev, drm_dev);
269 rockchip_drm_dma_detach_device(drm_dev, dev);
270 err_group_remove_device:
271 iommu_group_remove_device(dev);
273 iommu_put_dma_cookie(private->domain);
275 iommu_domain_free(private->domain);
277 drm_mode_config_cleanup(drm_dev);
278 drm_dev->dev_private = NULL;
282 static int rockchip_drm_unload(struct drm_device *drm_dev)
284 struct device *dev = drm_dev->dev;
285 struct rockchip_drm_private *private = drm_dev->dev_private;
287 rockchip_drm_fbdev_fini(drm_dev);
288 drm_vblank_cleanup(drm_dev);
289 drm_kms_helper_poll_fini(drm_dev);
290 component_unbind_all(dev, drm_dev);
291 rockchip_drm_dma_detach_device(drm_dev, dev);
292 iommu_group_remove_device(dev);
293 iommu_put_dma_cookie(private->domain);
294 iommu_domain_free(private->domain);
295 drm_mode_config_cleanup(drm_dev);
296 drm_dev->dev_private = NULL;
301 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
302 struct drm_file *file_priv)
304 struct rockchip_drm_private *priv = crtc->dev->dev_private;
305 int pipe = drm_crtc_index(crtc);
307 if (pipe < ROCKCHIP_MAX_CRTC &&
308 priv->crtc_funcs[pipe] &&
309 priv->crtc_funcs[pipe]->cancel_pending_vblank)
310 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
313 int rockchip_drm_register_subdrv(struct drm_rockchip_subdrv *subdrv)
318 mutex_lock(&subdrv_list_mutex);
319 list_add_tail(&subdrv->list, &rockchip_drm_subdrv_list);
320 mutex_unlock(&subdrv_list_mutex);
324 EXPORT_SYMBOL_GPL(rockchip_drm_register_subdrv);
326 int rockchip_drm_unregister_subdrv(struct drm_rockchip_subdrv *subdrv)
331 mutex_lock(&subdrv_list_mutex);
332 list_del(&subdrv->list);
333 mutex_unlock(&subdrv_list_mutex);
337 EXPORT_SYMBOL_GPL(rockchip_drm_unregister_subdrv);
339 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
341 struct rockchip_drm_file_private *file_priv;
342 struct drm_rockchip_subdrv *subdrv;
345 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
348 INIT_LIST_HEAD(&file_priv->gem_cpu_acquire_list);
350 file->driver_priv = file_priv;
352 mutex_lock(&subdrv_list_mutex);
353 list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list) {
354 ret = subdrv->open(dev, subdrv->dev, file);
356 mutex_unlock(&subdrv_list_mutex);
357 goto err_free_file_priv;
360 mutex_unlock(&subdrv_list_mutex);
371 static void rockchip_drm_preclose(struct drm_device *dev,
372 struct drm_file *file_priv)
374 struct rockchip_drm_file_private *file_private = file_priv->driver_priv;
375 struct rockchip_gem_object_node *cur, *d;
376 struct drm_rockchip_subdrv *subdrv;
377 struct drm_crtc *crtc;
379 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
380 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
382 mutex_lock(&dev->struct_mutex);
383 list_for_each_entry_safe(cur, d,
384 &file_private->gem_cpu_acquire_list, list) {
385 #ifdef CONFIG_DRM_DMA_SYNC
386 BUG_ON(!cur->rockchip_gem_obj->acquire_fence);
387 drm_fence_signal_and_put(&cur->rockchip_gem_obj->acquire_fence);
389 drm_gem_object_unreference(&cur->rockchip_gem_obj->base);
392 /* since we are deleting the whole list, just initialize the header
393 * instead of calling list_del for every element
395 INIT_LIST_HEAD(&file_private->gem_cpu_acquire_list);
396 mutex_unlock(&dev->struct_mutex);
398 mutex_lock(&subdrv_list_mutex);
399 list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list)
400 subdrv->close(dev, subdrv->dev, file_priv);
401 mutex_unlock(&subdrv_list_mutex);
404 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
406 kfree(file->driver_priv);
407 file->driver_priv = NULL;
410 void rockchip_drm_lastclose(struct drm_device *dev)
412 struct rockchip_drm_private *priv = dev->dev_private;
414 drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
417 static const struct drm_ioctl_desc rockchip_ioctls[] = {
418 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_gem_create_ioctl,
419 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
420 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
421 rockchip_gem_map_offset_ioctl,
422 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
423 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_ACQUIRE,
424 rockchip_gem_cpu_acquire_ioctl,
425 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
426 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_RELEASE,
427 rockchip_gem_cpu_release_ioctl,
428 DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
429 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_GET_VER, rockchip_rga_get_ver_ioctl,
430 DRM_AUTH | DRM_RENDER_ALLOW),
431 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_SET_CMDLIST,
432 rockchip_rga_set_cmdlist_ioctl,
433 DRM_AUTH | DRM_RENDER_ALLOW),
434 DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_EXEC, rockchip_rga_exec_ioctl,
435 DRM_AUTH | DRM_RENDER_ALLOW),
438 static const struct file_operations rockchip_drm_driver_fops = {
439 .owner = THIS_MODULE,
441 .mmap = rockchip_gem_mmap,
444 .unlocked_ioctl = drm_ioctl,
446 .compat_ioctl = drm_compat_ioctl,
448 .release = drm_release,
451 const struct vm_operations_struct rockchip_drm_vm_ops = {
452 .open = drm_gem_vm_open,
453 .close = drm_gem_vm_close,
456 static struct drm_driver rockchip_drm_driver = {
457 .driver_features = DRIVER_MODESET | DRIVER_GEM |
458 DRIVER_PRIME | DRIVER_ATOMIC |
460 .load = rockchip_drm_load,
461 .unload = rockchip_drm_unload,
462 .preclose = rockchip_drm_preclose,
463 .lastclose = rockchip_drm_lastclose,
464 .get_vblank_counter = drm_vblank_no_hw_counter,
465 .open = rockchip_drm_open,
466 .postclose = rockchip_drm_postclose,
467 .enable_vblank = rockchip_drm_crtc_enable_vblank,
468 .disable_vblank = rockchip_drm_crtc_disable_vblank,
469 .gem_vm_ops = &rockchip_drm_vm_ops,
470 .gem_free_object = rockchip_gem_free_object,
471 .dumb_create = rockchip_gem_dumb_create,
472 .dumb_map_offset = rockchip_gem_dumb_map_offset,
473 .dumb_destroy = drm_gem_dumb_destroy,
474 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
475 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
476 .gem_prime_import = drm_gem_prime_import,
477 .gem_prime_import_sg_table = rockchip_gem_prime_import_sg_table,
478 .gem_prime_export = drm_gem_prime_export,
479 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
480 .gem_prime_vmap = rockchip_gem_prime_vmap,
481 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
482 .gem_prime_mmap = rockchip_gem_mmap_buf,
483 .ioctls = rockchip_ioctls,
484 .num_ioctls = ARRAY_SIZE(rockchip_ioctls),
485 .fops = &rockchip_drm_driver_fops,
489 .major = DRIVER_MAJOR,
490 .minor = DRIVER_MINOR,
493 #ifdef CONFIG_PM_SLEEP
494 static int rockchip_drm_sys_suspend(struct device *dev)
496 struct drm_device *drm = dev_get_drvdata(dev);
497 struct drm_connector *connector;
502 drm_modeset_lock_all(drm);
503 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
504 int old_dpms = connector->dpms;
506 if (connector->funcs->dpms)
507 connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
509 /* Set the old mode back to the connector for resume */
510 connector->dpms = old_dpms;
512 drm_modeset_unlock_all(drm);
517 static int rockchip_drm_sys_resume(struct device *dev)
519 struct drm_device *drm = dev_get_drvdata(dev);
520 struct drm_connector *connector;
521 enum drm_connector_status status;
522 bool changed = false;
527 drm_modeset_lock_all(drm);
528 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
529 int desired_mode = connector->dpms;
532 * at suspend time, we save dpms to connector->dpms,
533 * restore the old_dpms, and at current time, the connector
534 * dpms status must be DRM_MODE_DPMS_OFF.
536 connector->dpms = DRM_MODE_DPMS_OFF;
539 * If the connector has been disconnected during suspend,
540 * disconnect it from the encoder and leave it off. We'll notify
541 * userspace at the end.
543 if (desired_mode == DRM_MODE_DPMS_ON) {
544 status = connector->funcs->detect(connector, true);
545 if (status == connector_status_disconnected) {
546 connector->encoder = NULL;
547 connector->status = status;
552 if (connector->funcs->dpms)
553 connector->funcs->dpms(connector, desired_mode);
555 drm_modeset_unlock_all(drm);
557 drm_helper_resume_force_mode(drm);
560 drm_kms_helper_hotplug_event(drm);
566 static const struct dev_pm_ops rockchip_drm_pm_ops = {
567 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
568 rockchip_drm_sys_resume)
571 static int compare_of(struct device *dev, void *data)
573 struct device_node *np = data;
575 return dev->of_node == np;
578 static void rockchip_add_endpoints(struct device *dev,
579 struct component_match **match,
580 struct device_node *port)
582 struct device_node *ep, *remote;
584 for_each_child_of_node(port, ep) {
585 remote = of_graph_get_remote_port_parent(ep);
586 if (!remote || !of_device_is_available(remote)) {
589 } else if (!of_device_is_available(remote->parent)) {
590 dev_warn(dev, "parent device of %s is not available\n",
596 component_match_add(dev, match, compare_of, remote);
601 static int rockchip_drm_bind(struct device *dev)
603 struct drm_device *drm;
606 drm = drm_dev_alloc(&rockchip_drm_driver, dev);
610 ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
614 ret = drm_dev_register(drm, 0);
618 dev_set_drvdata(dev, drm);
627 static void rockchip_drm_unbind(struct device *dev)
629 struct drm_device *drm = dev_get_drvdata(dev);
631 drm_dev_unregister(drm);
633 dev_set_drvdata(dev, NULL);
636 static const struct component_master_ops rockchip_drm_ops = {
637 .bind = rockchip_drm_bind,
638 .unbind = rockchip_drm_unbind,
641 static int rockchip_drm_platform_probe(struct platform_device *pdev)
643 struct device *dev = &pdev->dev;
644 struct component_match *match = NULL;
645 struct device_node *np = dev->of_node;
646 struct device_node *port;
652 * Bind the crtc ports first, so that
653 * drm_of_find_possible_crtcs called from encoder .bind callbacks
657 port = of_parse_phandle(np, "ports", i);
661 if (!of_device_is_available(port->parent)) {
666 component_match_add(dev, &match, compare_of, port->parent);
671 dev_err(dev, "missing 'ports' property\n");
676 dev_err(dev, "No available vop found for display-subsystem.\n");
680 * For each bound crtc, bind the encoders attached to its
684 port = of_parse_phandle(np, "ports", i);
688 if (!of_device_is_available(port->parent)) {
693 rockchip_add_endpoints(dev, &match, port);
697 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
700 static int rockchip_drm_platform_remove(struct platform_device *pdev)
702 component_master_del(&pdev->dev, &rockchip_drm_ops);
707 static const struct of_device_id rockchip_drm_dt_ids[] = {
708 { .compatible = "rockchip,display-subsystem", },
711 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
713 static struct platform_driver rockchip_drm_platform_driver = {
714 .probe = rockchip_drm_platform_probe,
715 .remove = rockchip_drm_platform_remove,
717 .name = "rockchip-drm",
718 .of_match_table = rockchip_drm_dt_ids,
719 .pm = &rockchip_drm_pm_ops,
723 module_platform_driver(rockchip_drm_platform_driver);
725 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
726 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
727 MODULE_LICENSE("GPL v2");