2 * drivers/gpu/drm/omapdrm/omap_drv.c
4 * Copyright (C) 2011 Texas Instruments
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/wait.h>
22 #include <drm/drm_atomic.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_fb_helper.h>
27 #include "omap_dmm_tiler.h"
30 #define DRIVER_NAME MODULE_NAME
31 #define DRIVER_DESC "OMAP DRM"
32 #define DRIVER_DATE "20110917"
33 #define DRIVER_MAJOR 1
34 #define DRIVER_MINOR 0
35 #define DRIVER_PATCHLEVEL 0
41 /* Notes about mapping DSS and DRM entities:
43 * encoder: manager.. with some extension to allow one primary CRTC
44 * and zero or more video CRTC's to be mapped to one encoder?
45 * connector: dssdev.. manager can be attached/detached from different
49 static void omap_fb_output_poll_changed(struct drm_device *dev)
51 struct omap_drm_private *priv = dev->dev_private;
54 drm_fb_helper_hotplug_event(priv->fbdev);
57 struct omap_atomic_state_commit {
58 struct work_struct work;
59 struct drm_device *dev;
60 struct drm_atomic_state *state;
64 static void omap_atomic_wait_for_completion(struct drm_device *dev,
65 struct drm_atomic_state *old_state)
67 struct drm_crtc_state *old_crtc_state;
68 struct drm_crtc *crtc;
72 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
73 if (!crtc->state->enable)
76 ret = omap_crtc_wait_pending(crtc);
80 "atomic complete timeout (pipe %u)!\n", i);
84 static void omap_atomic_complete(struct omap_atomic_state_commit *commit)
86 struct drm_device *dev = commit->dev;
87 struct omap_drm_private *priv = dev->dev_private;
88 struct drm_atomic_state *old_state = commit->state;
90 /* Apply the atomic update. */
91 priv->dispc_ops->runtime_get();
93 drm_atomic_helper_commit_modeset_disables(dev, old_state);
95 /* With the current dss dispc implementation we have to enable
96 * the new modeset before we can commit planes. The dispc ovl
97 * configuration relies on the video mode configuration been
98 * written into the HW when the ovl configuration is
101 * This approach is not ideal because after a mode change the
102 * plane update is executed only after the first vblank
103 * interrupt. The dispc implementation should be fixed so that
104 * it is able use uncommitted drm state information.
106 drm_atomic_helper_commit_modeset_enables(dev, old_state);
107 omap_atomic_wait_for_completion(dev, old_state);
109 drm_atomic_helper_commit_planes(dev, old_state, 0);
111 omap_atomic_wait_for_completion(dev, old_state);
113 drm_atomic_helper_cleanup_planes(dev, old_state);
115 priv->dispc_ops->runtime_put();
117 drm_atomic_state_put(old_state);
119 /* Complete the commit, wake up any waiter. */
120 spin_lock(&priv->commit.lock);
121 priv->commit.pending &= ~commit->crtcs;
122 spin_unlock(&priv->commit.lock);
124 wake_up_all(&priv->commit.wait);
129 static void omap_atomic_work(struct work_struct *work)
131 struct omap_atomic_state_commit *commit =
132 container_of(work, struct omap_atomic_state_commit, work);
134 omap_atomic_complete(commit);
137 static bool omap_atomic_is_pending(struct omap_drm_private *priv,
138 struct omap_atomic_state_commit *commit)
142 spin_lock(&priv->commit.lock);
143 pending = priv->commit.pending & commit->crtcs;
144 spin_unlock(&priv->commit.lock);
149 static int omap_atomic_commit(struct drm_device *dev,
150 struct drm_atomic_state *state, bool nonblock)
152 struct omap_drm_private *priv = dev->dev_private;
153 struct omap_atomic_state_commit *commit;
154 struct drm_crtc *crtc;
155 struct drm_crtc_state *crtc_state;
158 ret = drm_atomic_helper_prepare_planes(dev, state);
162 /* Allocate the commit object. */
163 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
164 if (commit == NULL) {
169 INIT_WORK(&commit->work, omap_atomic_work);
171 commit->state = state;
173 /* Wait until all affected CRTCs have completed previous commits and
174 * mark them as pending.
176 for_each_crtc_in_state(state, crtc, crtc_state, i)
177 commit->crtcs |= drm_crtc_mask(crtc);
179 wait_event(priv->commit.wait, !omap_atomic_is_pending(priv, commit));
181 spin_lock(&priv->commit.lock);
182 priv->commit.pending |= commit->crtcs;
183 spin_unlock(&priv->commit.lock);
185 /* Swap the state, this is the point of no return. */
186 drm_atomic_helper_swap_state(state, true);
188 drm_atomic_state_get(state);
190 schedule_work(&commit->work);
192 omap_atomic_complete(commit);
197 drm_atomic_helper_cleanup_planes(dev, state);
201 static const struct drm_mode_config_funcs omap_mode_config_funcs = {
202 .fb_create = omap_framebuffer_create,
203 .output_poll_changed = omap_fb_output_poll_changed,
204 .atomic_check = drm_atomic_helper_check,
205 .atomic_commit = omap_atomic_commit,
208 static int get_connector_type(struct omap_dss_device *dssdev)
210 switch (dssdev->type) {
211 case OMAP_DISPLAY_TYPE_HDMI:
212 return DRM_MODE_CONNECTOR_HDMIA;
213 case OMAP_DISPLAY_TYPE_DVI:
214 return DRM_MODE_CONNECTOR_DVID;
215 case OMAP_DISPLAY_TYPE_DSI:
216 return DRM_MODE_CONNECTOR_DSI;
218 return DRM_MODE_CONNECTOR_Unknown;
222 static void omap_disconnect_dssdevs(void)
224 struct omap_dss_device *dssdev = NULL;
226 for_each_dss_dev(dssdev)
227 dssdev->driver->disconnect(dssdev);
230 static int omap_connect_dssdevs(void)
233 struct omap_dss_device *dssdev = NULL;
235 if (!omapdss_stack_is_ready())
236 return -EPROBE_DEFER;
238 for_each_dss_dev(dssdev) {
239 r = dssdev->driver->connect(dssdev);
240 if (r == -EPROBE_DEFER) {
241 omap_dss_put_device(dssdev);
244 dev_warn(dssdev->dev, "could not connect display: %s\n",
253 * if we are deferring probe, we disconnect the devices we previously
256 omap_disconnect_dssdevs();
261 static int omap_modeset_init_properties(struct drm_device *dev)
263 struct omap_drm_private *priv = dev->dev_private;
265 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0, 3);
266 if (!priv->zorder_prop)
272 static int omap_modeset_init(struct drm_device *dev)
274 struct omap_drm_private *priv = dev->dev_private;
275 struct omap_dss_device *dssdev = NULL;
276 int num_ovls = priv->dispc_ops->get_num_ovls();
277 int num_mgrs = priv->dispc_ops->get_num_mgrs();
278 int num_crtcs, crtc_idx, plane_idx;
282 drm_mode_config_init(dev);
284 ret = omap_modeset_init_properties(dev);
289 * This function creates exactly one connector, encoder, crtc,
290 * and primary plane per each connected dss-device. Each
291 * connector->encoder->crtc chain is expected to be separate
292 * and each crtc is connect to a single dss-channel. If the
293 * configuration does not match the expectations or exceeds
294 * the available resources, the configuration is rejected.
297 for_each_dss_dev(dssdev)
298 if (omapdss_device_is_connected(dssdev))
301 if (num_crtcs > num_mgrs || num_crtcs > num_ovls ||
302 num_crtcs > ARRAY_SIZE(priv->crtcs) ||
303 num_crtcs > ARRAY_SIZE(priv->planes) ||
304 num_crtcs > ARRAY_SIZE(priv->encoders) ||
305 num_crtcs > ARRAY_SIZE(priv->connectors)) {
306 dev_err(dev->dev, "%s(): Too many connected displays\n",
311 /* All planes can be put to any CRTC */
312 plane_crtc_mask = (1 << num_crtcs) - 1;
318 for_each_dss_dev(dssdev) {
319 struct drm_connector *connector;
320 struct drm_encoder *encoder;
321 struct drm_plane *plane;
322 struct drm_crtc *crtc;
324 if (!omapdss_device_is_connected(dssdev))
327 encoder = omap_encoder_init(dev, dssdev);
331 connector = omap_connector_init(dev,
332 get_connector_type(dssdev), dssdev, encoder);
336 plane = omap_plane_init(dev, plane_idx, DRM_PLANE_TYPE_PRIMARY,
339 return PTR_ERR(plane);
341 crtc = omap_crtc_init(dev, plane, dssdev);
343 return PTR_ERR(crtc);
345 drm_mode_connector_attach_encoder(connector, encoder);
346 encoder->possible_crtcs = (1 << crtc_idx);
348 priv->crtcs[priv->num_crtcs++] = crtc;
349 priv->planes[priv->num_planes++] = plane;
350 priv->encoders[priv->num_encoders++] = encoder;
351 priv->connectors[priv->num_connectors++] = connector;
358 * Create normal planes for the remaining overlays:
360 for (; plane_idx < num_ovls; plane_idx++) {
361 struct drm_plane *plane;
363 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
366 plane = omap_plane_init(dev, plane_idx, DRM_PLANE_TYPE_OVERLAY,
369 return PTR_ERR(plane);
371 priv->planes[priv->num_planes++] = plane;
374 DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
375 priv->num_planes, priv->num_crtcs, priv->num_encoders,
376 priv->num_connectors);
378 dev->mode_config.min_width = 8;
379 dev->mode_config.min_height = 2;
381 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
382 * to fill in these limits properly on different OMAP generations..
384 dev->mode_config.max_width = 2048;
385 dev->mode_config.max_height = 2048;
387 dev->mode_config.funcs = &omap_mode_config_funcs;
389 drm_mode_config_reset(dev);
391 omap_drm_irq_install(dev);
401 static int ioctl_get_param(struct drm_device *dev, void *data,
402 struct drm_file *file_priv)
404 struct omap_drm_private *priv = dev->dev_private;
405 struct drm_omap_param *args = data;
407 DBG("%p: param=%llu", dev, args->param);
409 switch (args->param) {
410 case OMAP_PARAM_CHIPSET_ID:
411 args->value = priv->omaprev;
414 DBG("unknown parameter %lld", args->param);
421 static int ioctl_set_param(struct drm_device *dev, void *data,
422 struct drm_file *file_priv)
424 struct drm_omap_param *args = data;
426 switch (args->param) {
428 DBG("unknown parameter %lld", args->param);
435 #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
437 static int ioctl_gem_new(struct drm_device *dev, void *data,
438 struct drm_file *file_priv)
440 struct drm_omap_gem_new *args = data;
441 u32 flags = args->flags & OMAP_BO_USER_MASK;
443 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
444 args->size.bytes, flags);
446 return omap_gem_new_handle(dev, file_priv, args->size, flags,
450 static int ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
451 struct drm_file *file_priv)
453 struct drm_omap_gem_cpu_prep *args = data;
454 struct drm_gem_object *obj;
457 VERB("%p:%p: handle=%d, op=%x", dev, file_priv, args->handle, args->op);
459 obj = drm_gem_object_lookup(file_priv, args->handle);
463 ret = omap_gem_op_sync(obj, args->op);
466 ret = omap_gem_op_start(obj, args->op);
468 drm_gem_object_unreference_unlocked(obj);
473 static int ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
474 struct drm_file *file_priv)
476 struct drm_omap_gem_cpu_fini *args = data;
477 struct drm_gem_object *obj;
480 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
482 obj = drm_gem_object_lookup(file_priv, args->handle);
486 /* XXX flushy, flushy */
490 ret = omap_gem_op_finish(obj, args->op);
492 drm_gem_object_unreference_unlocked(obj);
497 static int ioctl_gem_info(struct drm_device *dev, void *data,
498 struct drm_file *file_priv)
500 struct drm_omap_gem_info *args = data;
501 struct drm_gem_object *obj;
504 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
506 obj = drm_gem_object_lookup(file_priv, args->handle);
510 args->size = omap_gem_mmap_size(obj);
511 args->offset = omap_gem_mmap_offset(obj);
513 drm_gem_object_unreference_unlocked(obj);
518 static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
519 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
520 DRM_AUTH | DRM_RENDER_ALLOW),
521 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
522 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
523 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
524 DRM_AUTH | DRM_RENDER_ALLOW),
525 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, ioctl_gem_cpu_prep,
526 DRM_AUTH | DRM_RENDER_ALLOW),
527 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, ioctl_gem_cpu_fini,
528 DRM_AUTH | DRM_RENDER_ALLOW),
529 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
530 DRM_AUTH | DRM_RENDER_ALLOW),
537 static int dev_open(struct drm_device *dev, struct drm_file *file)
539 file->driver_priv = NULL;
541 DBG("open: dev=%p, file=%p", dev, file);
547 * lastclose - clean up after all DRM clients have exited
550 * Take care of cleaning up after all DRM clients have exited. In the
551 * mode setting case, we want to restore the kernel's initial mode (just
552 * in case the last client left us in a bad state).
554 static void dev_lastclose(struct drm_device *dev)
558 /* we don't support vga_switcheroo.. so just make sure the fbdev
561 struct omap_drm_private *priv = dev->dev_private;
564 DBG("lastclose: dev=%p", dev);
566 /* need to restore default rotation state.. not sure
567 * if there is a cleaner way to restore properties to
568 * default state? Maybe a flag that properties should
569 * automatically be restored to default state on
572 for (i = 0; i < priv->num_crtcs; i++) {
573 struct drm_crtc *crtc = priv->crtcs[i];
575 if (!crtc->primary->rotation_property)
578 drm_object_property_set_value(&crtc->base,
579 crtc->primary->rotation_property,
583 for (i = 0; i < priv->num_planes; i++) {
584 struct drm_plane *plane = priv->planes[i];
586 if (!plane->rotation_property)
589 drm_object_property_set_value(&plane->base,
590 plane->rotation_property,
595 ret = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
597 DBG("failed to restore crtc mode");
601 static const struct vm_operations_struct omap_gem_vm_ops = {
602 .fault = omap_gem_fault,
603 .open = drm_gem_vm_open,
604 .close = drm_gem_vm_close,
607 static const struct file_operations omapdriver_fops = {
608 .owner = THIS_MODULE,
610 .unlocked_ioctl = drm_ioctl,
611 .release = drm_release,
612 .mmap = omap_gem_mmap,
615 .llseek = noop_llseek,
618 static struct drm_driver omap_drm_driver = {
619 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
620 DRIVER_ATOMIC | DRIVER_RENDER,
622 .lastclose = dev_lastclose,
623 #ifdef CONFIG_DEBUG_FS
624 .debugfs_init = omap_debugfs_init,
626 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
627 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
628 .gem_prime_export = omap_gem_prime_export,
629 .gem_prime_import = omap_gem_prime_import,
630 .gem_free_object = omap_gem_free_object,
631 .gem_vm_ops = &omap_gem_vm_ops,
632 .dumb_create = omap_gem_dumb_create,
633 .dumb_map_offset = omap_gem_dumb_map_offset,
634 .dumb_destroy = drm_gem_dumb_destroy,
636 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
637 .fops = &omapdriver_fops,
641 .major = DRIVER_MAJOR,
642 .minor = DRIVER_MINOR,
643 .patchlevel = DRIVER_PATCHLEVEL,
646 static int pdev_probe(struct platform_device *pdev)
648 struct omap_drm_platform_data *pdata = pdev->dev.platform_data;
649 struct omap_drm_private *priv;
650 struct drm_device *ddev;
654 DBG("%s", pdev->name);
656 if (omapdss_is_initialized() == false)
657 return -EPROBE_DEFER;
659 omap_crtc_pre_init();
661 ret = omap_connect_dssdevs();
663 goto err_crtc_uninit;
665 /* Allocate and initialize the driver private structure. */
666 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
669 goto err_disconnect_dssdevs;
672 priv->dispc_ops = dispc_get_ops();
674 priv->omaprev = pdata->omaprev;
675 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
677 init_waitqueue_head(&priv->commit.wait);
678 spin_lock_init(&priv->commit.lock);
679 spin_lock_init(&priv->list_lock);
680 INIT_LIST_HEAD(&priv->obj_list);
682 /* Allocate and initialize the DRM device. */
683 ddev = drm_dev_alloc(&omap_drm_driver, &pdev->dev);
689 ddev->dev_private = priv;
690 platform_set_drvdata(pdev, ddev);
694 ret = omap_modeset_init(ddev);
696 dev_err(&pdev->dev, "omap_modeset_init failed: ret=%d\n", ret);
697 goto err_free_drm_dev;
700 /* Initialize vblank handling, start with all CRTCs disabled. */
701 ret = drm_vblank_init(ddev, priv->num_crtcs);
703 dev_err(&pdev->dev, "could not init vblank\n");
704 goto err_cleanup_modeset;
707 for (i = 0; i < priv->num_crtcs; i++)
708 drm_crtc_vblank_off(priv->crtcs[i]);
710 priv->fbdev = omap_fbdev_init(ddev);
712 drm_kms_helper_poll_init(ddev);
715 * Register the DRM device with the core and the connectors with
718 ret = drm_dev_register(ddev, 0);
720 goto err_cleanup_helpers;
725 drm_kms_helper_poll_fini(ddev);
727 omap_fbdev_free(ddev);
729 drm_mode_config_cleanup(ddev);
730 omap_drm_irq_uninstall(ddev);
732 omap_gem_deinit(ddev);
735 destroy_workqueue(priv->wq);
737 err_disconnect_dssdevs:
738 omap_disconnect_dssdevs();
740 omap_crtc_pre_uninit();
744 static int pdev_remove(struct platform_device *pdev)
746 struct drm_device *ddev = platform_get_drvdata(pdev);
747 struct omap_drm_private *priv = ddev->dev_private;
751 drm_dev_unregister(ddev);
753 drm_kms_helper_poll_fini(ddev);
756 omap_fbdev_free(ddev);
758 drm_atomic_helper_shutdown(ddev);
760 drm_mode_config_cleanup(ddev);
762 omap_drm_irq_uninstall(ddev);
763 omap_gem_deinit(ddev);
767 destroy_workqueue(priv->wq);
770 omap_disconnect_dssdevs();
771 omap_crtc_pre_uninit();
776 #ifdef CONFIG_PM_SLEEP
777 static int omap_drm_suspend_all_displays(void)
779 struct omap_dss_device *dssdev = NULL;
781 for_each_dss_dev(dssdev) {
785 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
786 dssdev->driver->disable(dssdev);
787 dssdev->activate_after_resume = true;
789 dssdev->activate_after_resume = false;
796 static int omap_drm_resume_all_displays(void)
798 struct omap_dss_device *dssdev = NULL;
800 for_each_dss_dev(dssdev) {
804 if (dssdev->activate_after_resume) {
805 dssdev->driver->enable(dssdev);
806 dssdev->activate_after_resume = false;
813 static int omap_drm_suspend(struct device *dev)
815 struct drm_device *drm_dev = dev_get_drvdata(dev);
817 drm_kms_helper_poll_disable(drm_dev);
819 drm_modeset_lock_all(drm_dev);
820 omap_drm_suspend_all_displays();
821 drm_modeset_unlock_all(drm_dev);
826 static int omap_drm_resume(struct device *dev)
828 struct drm_device *drm_dev = dev_get_drvdata(dev);
830 drm_modeset_lock_all(drm_dev);
831 omap_drm_resume_all_displays();
832 drm_modeset_unlock_all(drm_dev);
834 drm_kms_helper_poll_enable(drm_dev);
836 return omap_gem_resume(dev);
840 static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
842 static struct platform_driver pdev = {
845 .pm = &omapdrm_pm_ops,
848 .remove = pdev_remove,
851 static struct platform_driver * const drivers[] = {
856 static int __init omap_drm_init(void)
860 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
863 static void __exit omap_drm_fini(void)
867 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
870 /* need late_initcall() so we load after dss_driver's are loaded */
871 late_initcall(omap_drm_init);
872 module_exit(omap_drm_fini);
875 MODULE_DESCRIPTION("OMAP DRM Display Driver");
876 MODULE_ALIAS("platform:" DRIVER_NAME);
877 MODULE_LICENSE("GPL v2");