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 <linux/dma-mapping.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/module.h>
26 #include <linux/of_graph.h>
27 #include <linux/component.h>
28 #include <linux/fence.h>
30 #include <drm/rockchip_drm.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"
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
44 * Attach a (component) device to the shared drm dma mapping from master drm
45 * device. This is used by the VOPs to map GEM buffers to a common DMA
48 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
51 struct rockchip_drm_private *private = drm_dev->dev_private;
52 struct iommu_domain *domain = private->domain;
55 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
59 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
60 ret = iommu_attach_device(domain, dev);
62 dev_err(dev, "Failed to attach iommu device\n");
66 if (!common_iommu_setup_dma_ops(dev, 0x10000000, SZ_2G, domain->ops)) {
67 dev_err(dev, "Failed to set dma_ops\n");
68 iommu_detach_device(domain, dev);
75 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
78 struct rockchip_drm_private *private = drm_dev->dev_private;
79 struct iommu_domain *domain = private->domain;
81 iommu_detach_device(domain, dev);
84 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
85 const struct rockchip_crtc_funcs *crtc_funcs)
87 int pipe = drm_crtc_index(crtc);
88 struct rockchip_drm_private *priv = crtc->dev->dev_private;
90 if (pipe > ROCKCHIP_MAX_CRTC)
93 priv->crtc_funcs[pipe] = crtc_funcs;
98 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
100 int pipe = drm_crtc_index(crtc);
101 struct rockchip_drm_private *priv = crtc->dev->dev_private;
103 if (pipe > ROCKCHIP_MAX_CRTC)
106 priv->crtc_funcs[pipe] = NULL;
109 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
112 struct drm_crtc *crtc;
115 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
122 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
125 struct rockchip_drm_private *priv = dev->dev_private;
126 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
128 if (crtc && priv->crtc_funcs[pipe] &&
129 priv->crtc_funcs[pipe]->enable_vblank)
130 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
135 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
138 struct rockchip_drm_private *priv = dev->dev_private;
139 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
141 if (crtc && priv->crtc_funcs[pipe] &&
142 priv->crtc_funcs[pipe]->enable_vblank)
143 priv->crtc_funcs[pipe]->disable_vblank(crtc);
146 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
148 struct rockchip_drm_private *private;
149 struct device *dev = drm_dev->dev;
150 struct drm_connector *connector;
151 struct iommu_group *group;
154 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
158 mutex_init(&private->commit.lock);
159 INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
161 drm_dev->dev_private = private;
163 #ifdef CONFIG_DRM_DMA_SYNC
164 private->cpu_fence_context = fence_context_alloc(1);
165 atomic_set(&private->cpu_fence_seqno, 0);
168 drm_mode_config_init(drm_dev);
170 rockchip_drm_mode_config_init(drm_dev);
172 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
174 if (!dev->dma_parms) {
176 goto err_config_cleanup;
179 private->domain = iommu_domain_alloc(&platform_bus_type);
180 if (!private->domain)
183 ret = iommu_get_dma_cookie(private->domain);
185 goto err_free_domain;
187 group = iommu_group_get(dev);
189 group = iommu_group_alloc();
191 dev_err(dev, "Failed to allocate IOMMU group\n");
195 ret = iommu_group_add_device(group, dev);
196 iommu_group_put(group);
198 dev_err(dev, "failed to add device to IOMMU group\n");
203 * Attach virtual iommu device, sub iommu device can share the same
206 ret = rockchip_drm_dma_attach_device(drm_dev, dev);
208 goto err_group_remove_device;
210 /* Try to bind all sub drivers. */
211 ret = component_bind_all(dev, drm_dev);
213 goto err_detach_device;
216 * All components are now added, we can publish the connector sysfs
217 * entries to userspace. This will generate hotplug events and so
218 * userspace will expect to be able to access DRM at this point.
220 list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
222 ret = drm_connector_register(connector);
224 dev_err(drm_dev->dev,
225 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
227 connector->name, ret);
232 /* init kms poll for handling hpd */
233 drm_kms_helper_poll_init(drm_dev);
236 * enable drm irq mode.
237 * - with irq_enabled = true, we can use the vblank feature.
239 drm_dev->irq_enabled = true;
241 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
243 goto err_kms_helper_poll_fini;
246 * with vblank_disable_allowed = true, vblank interrupt will be disabled
247 * by drm timer once a current process gives up ownership of
248 * vblank event.(after drm_vblank_put function is called)
250 drm_dev->vblank_disable_allowed = true;
252 drm_mode_config_reset(drm_dev);
254 ret = rockchip_drm_fbdev_init(drm_dev);
256 goto err_vblank_cleanup;
260 drm_vblank_cleanup(drm_dev);
261 err_kms_helper_poll_fini:
262 drm_kms_helper_poll_fini(drm_dev);
264 component_unbind_all(dev, drm_dev);
266 rockchip_drm_dma_detach_device(drm_dev, dev);
267 err_group_remove_device:
268 iommu_group_remove_device(dev);
270 iommu_put_dma_cookie(private->domain);
272 iommu_domain_free(private->domain);
274 drm_mode_config_cleanup(drm_dev);
275 drm_dev->dev_private = NULL;
279 static int rockchip_drm_unload(struct drm_device *drm_dev)
281 struct device *dev = drm_dev->dev;
282 struct rockchip_drm_private *private = drm_dev->dev_private;
284 rockchip_drm_fbdev_fini(drm_dev);
285 drm_vblank_cleanup(drm_dev);
286 drm_kms_helper_poll_fini(drm_dev);
287 component_unbind_all(dev, drm_dev);
288 rockchip_drm_dma_detach_device(drm_dev, dev);
289 iommu_group_remove_device(dev);
290 iommu_put_dma_cookie(private->domain);
291 iommu_domain_free(private->domain);
292 drm_mode_config_cleanup(drm_dev);
293 drm_dev->dev_private = NULL;
298 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
299 struct drm_file *file_priv)
301 struct rockchip_drm_private *priv = crtc->dev->dev_private;
302 int pipe = drm_crtc_index(crtc);
304 if (pipe < ROCKCHIP_MAX_CRTC &&
305 priv->crtc_funcs[pipe] &&
306 priv->crtc_funcs[pipe]->cancel_pending_vblank)
307 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
310 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
312 struct rockchip_drm_file_private *file_priv;
314 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
317 INIT_LIST_HEAD(&file_priv->gem_cpu_acquire_list);
319 file->driver_priv = file_priv;
324 static void rockchip_drm_preclose(struct drm_device *dev,
325 struct drm_file *file_priv)
327 struct rockchip_drm_file_private *file_private = file_priv->driver_priv;
328 struct rockchip_gem_object_node *cur, *d;
329 struct drm_crtc *crtc;
331 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
332 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
334 mutex_lock(&dev->struct_mutex);
335 list_for_each_entry_safe(cur, d,
336 &file_private->gem_cpu_acquire_list, list) {
337 #ifdef CONFIG_DRM_DMA_SYNC
338 BUG_ON(!cur->rockchip_gem_obj->acquire_fence);
339 drm_fence_signal_and_put(&cur->rockchip_gem_obj->acquire_fence);
341 drm_gem_object_unreference(&cur->rockchip_gem_obj->base);
344 /* since we are deleting the whole list, just initialize the header
345 * instead of calling list_del for every element
347 INIT_LIST_HEAD(&file_private->gem_cpu_acquire_list);
348 mutex_unlock(&dev->struct_mutex);
351 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
353 kfree(file->driver_priv);
354 file->driver_priv = NULL;
357 void rockchip_drm_lastclose(struct drm_device *dev)
359 struct rockchip_drm_private *priv = dev->dev_private;
361 drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
364 static const struct drm_ioctl_desc rockchip_ioctls[] = {
365 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_gem_create_ioctl,
366 DRM_UNLOCKED | DRM_AUTH),
367 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
368 rockchip_gem_map_offset_ioctl,
369 DRM_UNLOCKED | DRM_AUTH),
370 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_ACQUIRE,
371 rockchip_gem_cpu_acquire_ioctl,
372 DRM_UNLOCKED | DRM_AUTH),
373 DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_RELEASE,
374 rockchip_gem_cpu_release_ioctl,
375 DRM_UNLOCKED | DRM_AUTH),
378 static const struct file_operations rockchip_drm_driver_fops = {
379 .owner = THIS_MODULE,
381 .mmap = rockchip_gem_mmap,
384 .unlocked_ioctl = drm_ioctl,
386 .compat_ioctl = drm_compat_ioctl,
388 .release = drm_release,
391 const struct vm_operations_struct rockchip_drm_vm_ops = {
392 .open = drm_gem_vm_open,
393 .close = drm_gem_vm_close,
396 static struct drm_driver rockchip_drm_driver = {
397 .driver_features = DRIVER_MODESET | DRIVER_GEM |
398 DRIVER_PRIME | DRIVER_ATOMIC,
399 .load = rockchip_drm_load,
400 .unload = rockchip_drm_unload,
401 .preclose = rockchip_drm_preclose,
402 .lastclose = rockchip_drm_lastclose,
403 .get_vblank_counter = drm_vblank_no_hw_counter,
404 .open = rockchip_drm_open,
405 .postclose = rockchip_drm_postclose,
406 .enable_vblank = rockchip_drm_crtc_enable_vblank,
407 .disable_vblank = rockchip_drm_crtc_disable_vblank,
408 .gem_vm_ops = &rockchip_drm_vm_ops,
409 .gem_free_object = rockchip_gem_free_object,
410 .dumb_create = rockchip_gem_dumb_create,
411 .dumb_map_offset = rockchip_gem_dumb_map_offset,
412 .dumb_destroy = drm_gem_dumb_destroy,
413 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
414 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
415 .gem_prime_import = drm_gem_prime_import,
416 .gem_prime_export = drm_gem_prime_export,
417 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
418 .gem_prime_vmap = rockchip_gem_prime_vmap,
419 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
420 .gem_prime_mmap = rockchip_gem_mmap_buf,
421 .ioctls = rockchip_ioctls,
422 .num_ioctls = ARRAY_SIZE(rockchip_ioctls),
423 .fops = &rockchip_drm_driver_fops,
427 .major = DRIVER_MAJOR,
428 .minor = DRIVER_MINOR,
431 #ifdef CONFIG_PM_SLEEP
432 static int rockchip_drm_sys_suspend(struct device *dev)
434 struct drm_device *drm = dev_get_drvdata(dev);
435 struct drm_connector *connector;
440 drm_modeset_lock_all(drm);
441 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
442 int old_dpms = connector->dpms;
444 if (connector->funcs->dpms)
445 connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
447 /* Set the old mode back to the connector for resume */
448 connector->dpms = old_dpms;
450 drm_modeset_unlock_all(drm);
455 static int rockchip_drm_sys_resume(struct device *dev)
457 struct drm_device *drm = dev_get_drvdata(dev);
458 struct drm_connector *connector;
459 enum drm_connector_status status;
460 bool changed = false;
465 drm_modeset_lock_all(drm);
466 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
467 int desired_mode = connector->dpms;
470 * at suspend time, we save dpms to connector->dpms,
471 * restore the old_dpms, and at current time, the connector
472 * dpms status must be DRM_MODE_DPMS_OFF.
474 connector->dpms = DRM_MODE_DPMS_OFF;
477 * If the connector has been disconnected during suspend,
478 * disconnect it from the encoder and leave it off. We'll notify
479 * userspace at the end.
481 if (desired_mode == DRM_MODE_DPMS_ON) {
482 status = connector->funcs->detect(connector, true);
483 if (status == connector_status_disconnected) {
484 connector->encoder = NULL;
485 connector->status = status;
490 if (connector->funcs->dpms)
491 connector->funcs->dpms(connector, desired_mode);
493 drm_modeset_unlock_all(drm);
495 drm_helper_resume_force_mode(drm);
498 drm_kms_helper_hotplug_event(drm);
504 static const struct dev_pm_ops rockchip_drm_pm_ops = {
505 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
506 rockchip_drm_sys_resume)
510 * @node: device tree node containing encoder input ports
511 * @encoder: drm_encoder
513 int rockchip_drm_encoder_get_mux_id(struct device_node *node,
514 struct drm_encoder *encoder)
516 struct device_node *ep;
517 struct drm_crtc *crtc = encoder->crtc;
518 struct of_endpoint endpoint;
519 struct device_node *port;
525 for_each_endpoint_of_node(node, ep) {
526 port = of_graph_get_remote_port(ep);
528 if (port == crtc->port) {
529 ret = of_graph_parse_endpoint(ep, &endpoint);
531 return ret ?: endpoint.id;
537 EXPORT_SYMBOL_GPL(rockchip_drm_encoder_get_mux_id);
539 static int compare_of(struct device *dev, void *data)
541 struct device_node *np = data;
543 return dev->of_node == np;
546 static void rockchip_add_endpoints(struct device *dev,
547 struct component_match **match,
548 struct device_node *port)
550 struct device_node *ep, *remote;
552 for_each_child_of_node(port, ep) {
553 remote = of_graph_get_remote_port_parent(ep);
554 if (!remote || !of_device_is_available(remote)) {
557 } else if (!of_device_is_available(remote->parent)) {
558 dev_warn(dev, "parent device of %s is not available\n",
564 component_match_add(dev, match, compare_of, remote);
569 static int rockchip_drm_bind(struct device *dev)
571 struct drm_device *drm;
574 drm = drm_dev_alloc(&rockchip_drm_driver, dev);
578 ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
582 ret = drm_dev_register(drm, 0);
586 dev_set_drvdata(dev, drm);
595 static void rockchip_drm_unbind(struct device *dev)
597 struct drm_device *drm = dev_get_drvdata(dev);
599 drm_dev_unregister(drm);
601 dev_set_drvdata(dev, NULL);
604 static const struct component_master_ops rockchip_drm_ops = {
605 .bind = rockchip_drm_bind,
606 .unbind = rockchip_drm_unbind,
609 static int rockchip_drm_platform_probe(struct platform_device *pdev)
611 struct device *dev = &pdev->dev;
612 struct component_match *match = NULL;
613 struct device_node *np = dev->of_node;
614 struct device_node *port;
620 * Bind the crtc ports first, so that
621 * drm_of_find_possible_crtcs called from encoder .bind callbacks
625 port = of_parse_phandle(np, "ports", i);
629 if (!of_device_is_available(port->parent)) {
634 component_match_add(dev, &match, compare_of, port->parent);
639 dev_err(dev, "missing 'ports' property\n");
644 dev_err(dev, "No available vop found for display-subsystem.\n");
648 * For each bound crtc, bind the encoders attached to its
652 port = of_parse_phandle(np, "ports", i);
656 if (!of_device_is_available(port->parent)) {
661 rockchip_add_endpoints(dev, &match, port);
665 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
668 static int rockchip_drm_platform_remove(struct platform_device *pdev)
670 component_master_del(&pdev->dev, &rockchip_drm_ops);
675 static const struct of_device_id rockchip_drm_dt_ids[] = {
676 { .compatible = "rockchip,display-subsystem", },
679 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
681 static struct platform_driver rockchip_drm_platform_driver = {
682 .probe = rockchip_drm_platform_probe,
683 .remove = rockchip_drm_platform_remove,
685 .name = "rockchip-drm",
686 .of_match_table = rockchip_drm_dt_ids,
687 .pm = &rockchip_drm_pm_ops,
691 module_platform_driver(rockchip_drm_platform_driver);
693 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
694 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
695 MODULE_LICENSE("GPL v2");