1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
7 #include <linux/dma-mapping.h>
8 #include <linux/platform_device.h>
10 #include <linux/sort.h>
11 #include <linux/sys_soc.h>
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_bridge.h>
16 #include <drm/drm_bridge_connector.h>
17 #include <drm/drm_drv.h>
18 #include <drm/drm_file.h>
19 #include <drm/drm_ioctl.h>
20 #include <drm/drm_panel.h>
21 #include <drm/drm_prime.h>
22 #include <drm/drm_probe_helper.h>
23 #include <drm/drm_vblank.h>
25 #include "omap_dmm_tiler.h"
27 #include "omap_fbdev.h"
29 #define DRIVER_NAME MODULE_NAME
30 #define DRIVER_DESC "OMAP DRM"
31 #define DRIVER_MAJOR 1
32 #define DRIVER_MINOR 0
33 #define DRIVER_PATCHLEVEL 0
39 /* Notes about mapping DSS and DRM entities:
41 * encoder: manager.. with some extension to allow one primary CRTC
42 * and zero or more video CRTC's to be mapped to one encoder?
43 * connector: dssdev.. manager can be attached/detached from different
47 static void omap_atomic_wait_for_completion(struct drm_device *dev,
48 struct drm_atomic_state *old_state)
50 struct drm_crtc_state *new_crtc_state;
51 struct drm_crtc *crtc;
55 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
56 if (!new_crtc_state->active)
59 ret = omap_crtc_wait_pending(crtc);
63 "atomic complete timeout (pipe %u)!\n", i);
67 static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
69 struct drm_device *dev = old_state->dev;
70 struct omap_drm_private *priv = dev->dev_private;
72 dispc_runtime_get(priv->dispc);
74 /* Apply the atomic update. */
75 drm_atomic_helper_commit_modeset_disables(dev, old_state);
77 if (priv->omaprev != 0x3430) {
78 /* With the current dss dispc implementation we have to enable
79 * the new modeset before we can commit planes. The dispc ovl
80 * configuration relies on the video mode configuration been
81 * written into the HW when the ovl configuration is
84 * This approach is not ideal because after a mode change the
85 * plane update is executed only after the first vblank
86 * interrupt. The dispc implementation should be fixed so that
87 * it is able use uncommitted drm state information.
89 drm_atomic_helper_commit_modeset_enables(dev, old_state);
90 omap_atomic_wait_for_completion(dev, old_state);
92 drm_atomic_helper_commit_planes(dev, old_state, 0);
94 drm_atomic_helper_commit_hw_done(old_state);
97 * OMAP3 DSS seems to have issues with the work-around above,
98 * resulting in endless sync losts if a crtc is enabled without
99 * a plane. For now, skip the WA for OMAP3.
101 drm_atomic_helper_commit_planes(dev, old_state, 0);
103 drm_atomic_helper_commit_modeset_enables(dev, old_state);
105 drm_atomic_helper_commit_hw_done(old_state);
109 * Wait for completion of the page flips to ensure that old buffers
110 * can't be touched by the hardware anymore before cleaning up planes.
112 omap_atomic_wait_for_completion(dev, old_state);
114 drm_atomic_helper_cleanup_planes(dev, old_state);
116 dispc_runtime_put(priv->dispc);
119 static int drm_atomic_state_normalized_zpos_cmp(const void *a, const void *b)
121 const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
122 const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
124 if (sa->normalized_zpos != sb->normalized_zpos)
125 return sa->normalized_zpos - sb->normalized_zpos;
127 return sa->plane->base.id - sb->plane->base.id;
131 * This replaces the drm_atomic_normalize_zpos to handle the dual overlay case.
133 * Since both halves need to be 'appear' side by side the zpos is
134 * recalculated when dealing with dual overlay cases so that the other
135 * planes zpos is consistent.
137 static int omap_atomic_update_normalize_zpos(struct drm_device *dev,
138 struct drm_atomic_state *state)
140 struct drm_crtc *crtc;
141 struct drm_crtc_state *old_state, *new_state;
142 struct drm_plane *plane;
144 int total_planes = dev->mode_config.num_total_plane;
145 struct drm_plane_state **states;
148 states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
152 for_each_oldnew_crtc_in_state(state, crtc, old_state, new_state, c) {
153 if (old_state->plane_mask == new_state->plane_mask &&
154 !new_state->zpos_changed)
157 /* Reset plane increment and index value for every crtc */
161 * Normalization process might create new states for planes
162 * which normalized_zpos has to be recalculated.
164 drm_for_each_plane_mask(plane, dev, new_state->plane_mask) {
165 struct drm_plane_state *plane_state =
166 drm_atomic_get_plane_state(new_state->state,
168 if (IS_ERR(plane_state)) {
169 ret = PTR_ERR(plane_state);
172 states[n++] = plane_state;
175 sort(states, n, sizeof(*states),
176 drm_atomic_state_normalized_zpos_cmp, NULL);
178 for (i = 0, inc = 0; i < n; i++) {
179 plane = states[i]->plane;
181 states[i]->normalized_zpos = i + inc;
182 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] updated normalized zpos value %d\n",
183 plane->base.id, plane->name,
184 states[i]->normalized_zpos);
186 if (is_omap_plane_dual_overlay(states[i]))
189 new_state->zpos_changed = true;
197 static int omap_atomic_check(struct drm_device *dev,
198 struct drm_atomic_state *state)
202 ret = drm_atomic_helper_check(dev, state);
206 if (dev->mode_config.normalize_zpos) {
207 ret = omap_atomic_update_normalize_zpos(dev, state);
215 static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
216 .atomic_commit_tail = omap_atomic_commit_tail,
219 static const struct drm_mode_config_funcs omap_mode_config_funcs = {
220 .fb_create = omap_framebuffer_create,
221 .atomic_check = omap_atomic_check,
222 .atomic_commit = drm_atomic_helper_commit,
225 /* Global/shared object state funcs */
228 * This is a helper that returns the private state currently in operation.
229 * Note that this would return the "old_state" if called in the atomic check
230 * path, and the "new_state" after the atomic swap has been done.
232 struct omap_global_state *
233 omap_get_existing_global_state(struct omap_drm_private *priv)
235 return to_omap_global_state(priv->glob_obj.state);
239 * This acquires the modeset lock set aside for global state, creates
240 * a new duplicated private object state.
242 struct omap_global_state *__must_check
243 omap_get_global_state(struct drm_atomic_state *s)
245 struct omap_drm_private *priv = s->dev->dev_private;
246 struct drm_private_state *priv_state;
248 priv_state = drm_atomic_get_private_obj_state(s, &priv->glob_obj);
249 if (IS_ERR(priv_state))
250 return ERR_CAST(priv_state);
252 return to_omap_global_state(priv_state);
255 static struct drm_private_state *
256 omap_global_duplicate_state(struct drm_private_obj *obj)
258 struct omap_global_state *state;
260 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
264 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
269 static void omap_global_destroy_state(struct drm_private_obj *obj,
270 struct drm_private_state *state)
272 struct omap_global_state *omap_state = to_omap_global_state(state);
277 static const struct drm_private_state_funcs omap_global_state_funcs = {
278 .atomic_duplicate_state = omap_global_duplicate_state,
279 .atomic_destroy_state = omap_global_destroy_state,
282 static int omap_global_obj_init(struct drm_device *dev)
284 struct omap_drm_private *priv = dev->dev_private;
285 struct omap_global_state *state;
287 state = kzalloc(sizeof(*state), GFP_KERNEL);
291 drm_atomic_private_obj_init(dev, &priv->glob_obj, &state->base,
292 &omap_global_state_funcs);
296 static void omap_global_obj_fini(struct omap_drm_private *priv)
298 drm_atomic_private_obj_fini(&priv->glob_obj);
301 static void omap_disconnect_pipelines(struct drm_device *ddev)
303 struct omap_drm_private *priv = ddev->dev_private;
306 for (i = 0; i < priv->num_pipes; i++) {
307 struct omap_drm_pipeline *pipe = &priv->pipes[i];
309 omapdss_device_disconnect(priv->dss, pipe->output);
311 omapdss_device_put(pipe->output);
315 memset(&priv->channels, 0, sizeof(priv->channels));
320 static int omap_connect_pipelines(struct drm_device *ddev)
322 struct omap_drm_private *priv = ddev->dev_private;
323 struct omap_dss_device *output = NULL;
326 for_each_dss_output(output) {
327 r = omapdss_device_connect(priv->dss, output);
328 if (r == -EPROBE_DEFER) {
329 omapdss_device_put(output);
332 dev_warn(output->dev, "could not connect output %s\n",
335 struct omap_drm_pipeline *pipe;
337 pipe = &priv->pipes[priv->num_pipes++];
338 pipe->output = omapdss_device_get(output);
340 if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
341 /* To balance the 'for_each_dss_output' loop */
342 omapdss_device_put(output);
351 static int omap_compare_pipelines(const void *a, const void *b)
353 const struct omap_drm_pipeline *pipe1 = a;
354 const struct omap_drm_pipeline *pipe2 = b;
356 if (pipe1->alias_id > pipe2->alias_id)
358 else if (pipe1->alias_id < pipe2->alias_id)
363 static int omap_modeset_init_properties(struct drm_device *dev)
365 struct omap_drm_private *priv = dev->dev_private;
366 unsigned int num_planes = dispc_get_num_ovls(priv->dispc);
368 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
370 if (!priv->zorder_prop)
376 static int omap_display_id(struct omap_dss_device *output)
378 struct device_node *node = NULL;
380 if (output->bridge) {
381 struct drm_bridge *bridge = output->bridge;
383 while (drm_bridge_get_next_bridge(bridge))
384 bridge = drm_bridge_get_next_bridge(bridge);
386 node = bridge->of_node;
389 return node ? of_alias_get_id(node, "display") : -ENODEV;
392 static int omap_modeset_init(struct drm_device *dev)
394 struct omap_drm_private *priv = dev->dev_private;
395 int num_ovls = dispc_get_num_ovls(priv->dispc);
396 int num_mgrs = dispc_get_num_mgrs(priv->dispc);
401 if (!omapdss_stack_is_ready())
402 return -EPROBE_DEFER;
404 ret = omap_modeset_init_properties(dev);
409 * This function creates exactly one connector, encoder, crtc,
410 * and primary plane per each connected dss-device. Each
411 * connector->encoder->crtc chain is expected to be separate
412 * and each crtc is connect to a single dss-channel. If the
413 * configuration does not match the expectations or exceeds
414 * the available resources, the configuration is rejected.
416 ret = omap_connect_pipelines(dev);
420 if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
421 dev_err(dev->dev, "%s(): Too many connected displays\n",
426 /* Create all planes first. They can all be put to any CRTC. */
427 plane_crtc_mask = (1 << priv->num_pipes) - 1;
429 for (i = 0; i < num_ovls; i++) {
430 enum drm_plane_type type = i < priv->num_pipes
431 ? DRM_PLANE_TYPE_PRIMARY
432 : DRM_PLANE_TYPE_OVERLAY;
433 struct drm_plane *plane;
435 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
438 plane = omap_plane_init(dev, i, type, plane_crtc_mask);
440 return PTR_ERR(plane);
442 priv->planes[priv->num_planes++] = plane;
446 * Create the encoders, attach the bridges and get the pipeline alias
449 for (i = 0; i < priv->num_pipes; i++) {
450 struct omap_drm_pipeline *pipe = &priv->pipes[i];
453 pipe->encoder = omap_encoder_init(dev, pipe->output);
457 if (pipe->output->bridge) {
458 ret = drm_bridge_attach(pipe->encoder,
459 pipe->output->bridge, NULL,
460 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
465 id = omap_display_id(pipe->output);
466 pipe->alias_id = id >= 0 ? id : i;
469 /* Sort the pipelines by DT aliases. */
470 sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
471 omap_compare_pipelines, NULL);
474 * Populate the pipeline lookup table by DISPC channel. Only one display
475 * is allowed per channel.
477 for (i = 0; i < priv->num_pipes; ++i) {
478 struct omap_drm_pipeline *pipe = &priv->pipes[i];
479 enum omap_channel channel = pipe->output->dispc_channel;
481 if (WARN_ON(priv->channels[channel] != NULL))
484 priv->channels[channel] = pipe;
487 /* Create the connectors and CRTCs. */
488 for (i = 0; i < priv->num_pipes; i++) {
489 struct omap_drm_pipeline *pipe = &priv->pipes[i];
490 struct drm_encoder *encoder = pipe->encoder;
491 struct drm_crtc *crtc;
493 pipe->connector = drm_bridge_connector_init(dev, encoder);
494 if (IS_ERR(pipe->connector)) {
496 "unable to create bridge connector for %s\n",
498 return PTR_ERR(pipe->connector);
501 drm_connector_attach_encoder(pipe->connector, encoder);
503 crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
505 return PTR_ERR(crtc);
507 encoder->possible_crtcs = 1 << i;
511 DBG("registered %u planes, %u crtcs/encoders/connectors\n",
512 priv->num_planes, priv->num_pipes);
514 dev->mode_config.min_width = 8;
515 dev->mode_config.min_height = 2;
518 * Note: these values are used for multiple independent things:
519 * connector mode filtering, buffer sizes, crtc sizes...
520 * Use big enough values here to cover all use cases, and do more
521 * specific checking in the respective code paths.
523 dev->mode_config.max_width = 8192;
524 dev->mode_config.max_height = 8192;
526 /* We want the zpos to be normalized */
527 dev->mode_config.normalize_zpos = true;
529 dev->mode_config.funcs = &omap_mode_config_funcs;
530 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
532 drm_mode_config_reset(dev);
534 omap_drm_irq_install(dev);
539 static void omap_modeset_fini(struct drm_device *ddev)
541 omap_drm_irq_uninstall(ddev);
543 drm_mode_config_cleanup(ddev);
551 static int ioctl_get_param(struct drm_device *dev, void *data,
552 struct drm_file *file_priv)
554 struct omap_drm_private *priv = dev->dev_private;
555 struct drm_omap_param *args = data;
557 DBG("%p: param=%llu", dev, args->param);
559 switch (args->param) {
560 case OMAP_PARAM_CHIPSET_ID:
561 args->value = priv->omaprev;
564 DBG("unknown parameter %lld", args->param);
571 #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
573 static int ioctl_gem_new(struct drm_device *dev, void *data,
574 struct drm_file *file_priv)
576 struct drm_omap_gem_new *args = data;
577 u32 flags = args->flags & OMAP_BO_USER_MASK;
579 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
580 args->size.bytes, flags);
582 return omap_gem_new_handle(dev, file_priv, args->size, flags,
586 static int ioctl_gem_info(struct drm_device *dev, void *data,
587 struct drm_file *file_priv)
589 struct drm_omap_gem_info *args = data;
590 struct drm_gem_object *obj;
593 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
595 obj = drm_gem_object_lookup(file_priv, args->handle);
599 args->size = omap_gem_mmap_size(obj);
600 args->offset = omap_gem_mmap_offset(obj);
602 drm_gem_object_put(obj);
607 static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
608 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
610 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, drm_invalid_op,
611 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
612 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
614 /* Deprecated, to be removed. */
615 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
617 /* Deprecated, to be removed. */
618 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
620 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
628 static int dev_open(struct drm_device *dev, struct drm_file *file)
630 file->driver_priv = NULL;
632 DBG("open: dev=%p, file=%p", dev, file);
637 DEFINE_DRM_GEM_FOPS(omapdriver_fops);
639 static const struct drm_driver omap_drm_driver = {
640 .driver_features = DRIVER_MODESET | DRIVER_GEM |
641 DRIVER_ATOMIC | DRIVER_RENDER,
643 #ifdef CONFIG_DEBUG_FS
644 .debugfs_init = omap_debugfs_init,
646 .gem_prime_import = omap_gem_prime_import,
647 .dumb_create = omap_gem_dumb_create,
648 .dumb_map_offset = omap_gem_dumb_map_offset,
649 OMAP_FBDEV_DRIVER_OPS,
651 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
652 .fops = &omapdriver_fops,
655 .major = DRIVER_MAJOR,
656 .minor = DRIVER_MINOR,
657 .patchlevel = DRIVER_PATCHLEVEL,
660 static const struct soc_device_attribute omapdrm_soc_devices[] = {
661 { .family = "OMAP3", .data = (void *)0x3430 },
662 { .family = "OMAP4", .data = (void *)0x4430 },
663 { .family = "OMAP5", .data = (void *)0x5430 },
664 { .family = "DRA7", .data = (void *)0x0752 },
668 static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
670 const struct soc_device_attribute *soc;
671 struct dss_pdata *pdata = dev->platform_data;
672 struct drm_device *ddev;
675 DBG("%s", dev_name(dev));
677 if (drm_firmware_drivers_only())
680 /* Allocate and initialize the DRM device. */
681 ddev = drm_dev_alloc(&omap_drm_driver, dev);
683 return PTR_ERR(ddev);
686 ddev->dev_private = priv;
689 priv->dss = pdata->dss;
690 priv->dispc = dispc_get_dispc(priv->dss);
692 priv->dss->mgr_ops_priv = priv;
694 soc = soc_device_match(omapdrm_soc_devices);
695 priv->omaprev = soc ? (uintptr_t)soc->data : 0;
696 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
699 goto err_alloc_workqueue;
702 mutex_init(&priv->list_lock);
703 INIT_LIST_HEAD(&priv->obj_list);
705 /* Get memory bandwidth limits */
706 priv->max_bandwidth = dispc_get_memory_bandwidth_limit(priv->dispc);
710 drm_mode_config_init(ddev);
712 ret = omap_global_obj_init(ddev);
716 ret = omap_hwoverlays_init(priv);
718 goto err_free_priv_obj;
720 ret = omap_modeset_init(ddev);
722 dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
723 goto err_free_overlays;
726 /* Initialize vblank handling, start with all CRTCs disabled. */
727 ret = drm_vblank_init(ddev, priv->num_pipes);
729 dev_err(priv->dev, "could not init vblank\n");
730 goto err_cleanup_modeset;
733 drm_kms_helper_poll_init(ddev);
736 * Register the DRM device with the core and the connectors with
739 ret = drm_dev_register(ddev, 0);
741 goto err_cleanup_helpers;
743 omap_fbdev_setup(ddev);
748 drm_kms_helper_poll_fini(ddev);
750 omap_modeset_fini(ddev);
752 omap_hwoverlays_destroy(priv);
754 omap_global_obj_fini(priv);
756 drm_mode_config_cleanup(ddev);
757 omap_gem_deinit(ddev);
758 destroy_workqueue(priv->wq);
760 omap_disconnect_pipelines(ddev);
765 static void omapdrm_cleanup(struct omap_drm_private *priv)
767 struct drm_device *ddev = priv->ddev;
771 drm_dev_unregister(ddev);
773 drm_kms_helper_poll_fini(ddev);
775 drm_atomic_helper_shutdown(ddev);
777 omap_modeset_fini(ddev);
778 omap_hwoverlays_destroy(priv);
779 omap_global_obj_fini(priv);
780 drm_mode_config_cleanup(ddev);
781 omap_gem_deinit(ddev);
783 destroy_workqueue(priv->wq);
785 omap_disconnect_pipelines(ddev);
790 static int pdev_probe(struct platform_device *pdev)
792 struct omap_drm_private *priv;
795 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
797 dev_err(&pdev->dev, "Failed to set the DMA mask\n");
801 /* Allocate and initialize the driver private structure. */
802 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
806 platform_set_drvdata(pdev, priv);
808 ret = omapdrm_init(priv, &pdev->dev);
815 static void pdev_remove(struct platform_device *pdev)
817 struct omap_drm_private *priv = platform_get_drvdata(pdev);
819 omapdrm_cleanup(priv);
823 static void pdev_shutdown(struct platform_device *pdev)
825 struct omap_drm_private *priv = platform_get_drvdata(pdev);
827 drm_atomic_helper_shutdown(priv->ddev);
830 #ifdef CONFIG_PM_SLEEP
831 static int omap_drm_suspend(struct device *dev)
833 struct omap_drm_private *priv = dev_get_drvdata(dev);
834 struct drm_device *drm_dev = priv->ddev;
836 return drm_mode_config_helper_suspend(drm_dev);
839 static int omap_drm_resume(struct device *dev)
841 struct omap_drm_private *priv = dev_get_drvdata(dev);
842 struct drm_device *drm_dev = priv->ddev;
844 drm_mode_config_helper_resume(drm_dev);
846 return omap_gem_resume(drm_dev);
850 static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
852 static struct platform_driver pdev = {
855 .pm = &omapdrm_pm_ops,
858 .remove = pdev_remove,
859 .shutdown = pdev_shutdown,
862 static struct platform_driver * const drivers[] = {
867 static int __init omap_drm_init(void)
877 r = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
886 static void __exit omap_drm_fini(void)
890 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
895 module_init(omap_drm_init);
896 module_exit(omap_drm_fini);
900 MODULE_DESCRIPTION("OMAP DRM Display Driver");
901 MODULE_ALIAS("platform:" DRIVER_NAME);
902 MODULE_LICENSE("GPL v2");