1c8b75bcaSEric Anholt /* 2c8b75bcaSEric Anholt * Copyright (C) 2015 Broadcom 3c8b75bcaSEric Anholt * 4c8b75bcaSEric Anholt * This program is free software; you can redistribute it and/or modify 5c8b75bcaSEric Anholt * it under the terms of the GNU General Public License version 2 as 6c8b75bcaSEric Anholt * published by the Free Software Foundation. 7c8b75bcaSEric Anholt */ 8c8b75bcaSEric Anholt 9c8b75bcaSEric Anholt /** 10c8b75bcaSEric Anholt * DOC: VC4 KMS 11c8b75bcaSEric Anholt * 12c8b75bcaSEric Anholt * This is the general code for implementing KMS mode setting that 13c8b75bcaSEric Anholt * doesn't clearly associate with any of the other objects (plane, 14c8b75bcaSEric Anholt * crtc, HDMI encoder). 15c8b75bcaSEric Anholt */ 16c8b75bcaSEric Anholt 17b7e8e25bSMasahiro Yamada #include <drm/drm_crtc.h> 18b7e8e25bSMasahiro Yamada #include <drm/drm_atomic.h> 19b7e8e25bSMasahiro Yamada #include <drm/drm_atomic_helper.h> 20b7e8e25bSMasahiro Yamada #include <drm/drm_crtc_helper.h> 21b7e8e25bSMasahiro Yamada #include <drm/drm_plane_helper.h> 22b7e8e25bSMasahiro Yamada #include <drm/drm_fb_cma_helper.h> 239762477cSNoralf Trønnes #include <drm/drm_gem_framebuffer_helper.h> 24c8b75bcaSEric Anholt #include "vc4_drv.h" 25c8b75bcaSEric Anholt 2648666d56SDerek Foreman static void vc4_output_poll_changed(struct drm_device *dev) 2748666d56SDerek Foreman { 2848666d56SDerek Foreman struct vc4_dev *vc4 = to_vc4_dev(dev); 2948666d56SDerek Foreman 3048666d56SDerek Foreman drm_fbdev_cma_hotplug_event(vc4->fbdev); 3148666d56SDerek Foreman } 3248666d56SDerek Foreman 33b501baccSEric Anholt static void 34cf1b372eSEric Anholt vc4_atomic_complete_commit(struct drm_atomic_state *state) 35b501baccSEric Anholt { 36b501baccSEric Anholt struct drm_device *dev = state->dev; 37b501baccSEric Anholt struct vc4_dev *vc4 = to_vc4_dev(dev); 38b501baccSEric Anholt 3934c8ea40SBoris Brezillon drm_atomic_helper_wait_for_fences(dev, state, false); 4034c8ea40SBoris Brezillon 4134c8ea40SBoris Brezillon drm_atomic_helper_wait_for_dependencies(state); 4234c8ea40SBoris Brezillon 43b501baccSEric Anholt drm_atomic_helper_commit_modeset_disables(dev, state); 44b501baccSEric Anholt 452b58e98dSLiu Ying drm_atomic_helper_commit_planes(dev, state, 0); 46b501baccSEric Anholt 47b501baccSEric Anholt drm_atomic_helper_commit_modeset_enables(dev, state); 48b501baccSEric Anholt 496674a904SEric Anholt /* Make sure that drm_atomic_helper_wait_for_vblanks() 506674a904SEric Anholt * actually waits for vblank. If we're doing a full atomic 516674a904SEric Anholt * modeset (as opposed to a vc4_update_plane() short circuit), 526674a904SEric Anholt * then we need to wait for scanout to be done with our 536674a904SEric Anholt * display lists before we free it and potentially reallocate 546674a904SEric Anholt * and overwrite the dlist memory with a new modeset. 556674a904SEric Anholt */ 566674a904SEric Anholt state->legacy_cursor_update = false; 576674a904SEric Anholt 5834c8ea40SBoris Brezillon drm_atomic_helper_commit_hw_done(state); 5934c8ea40SBoris Brezillon 60b501baccSEric Anholt drm_atomic_helper_wait_for_vblanks(dev, state); 61b501baccSEric Anholt 62b501baccSEric Anholt drm_atomic_helper_cleanup_planes(dev, state); 63b501baccSEric Anholt 6434c8ea40SBoris Brezillon drm_atomic_helper_commit_cleanup_done(state); 6534c8ea40SBoris Brezillon 660853695cSChris Wilson drm_atomic_state_put(state); 67b501baccSEric Anholt 68b501baccSEric Anholt up(&vc4->async_modeset); 69b501baccSEric Anholt } 70b501baccSEric Anholt 71cf1b372eSEric Anholt static void commit_work(struct work_struct *work) 72b501baccSEric Anholt { 73cf1b372eSEric Anholt struct drm_atomic_state *state = container_of(work, 74cf1b372eSEric Anholt struct drm_atomic_state, 75cf1b372eSEric Anholt commit_work); 76cf1b372eSEric Anholt vc4_atomic_complete_commit(state); 77b501baccSEric Anholt } 78b501baccSEric Anholt 79b501baccSEric Anholt /** 80b501baccSEric Anholt * vc4_atomic_commit - commit validated state object 81b501baccSEric Anholt * @dev: DRM device 82b501baccSEric Anholt * @state: the driver state object 83eb63961bSMaarten Lankhorst * @nonblock: nonblocking commit 84b501baccSEric Anholt * 85b501baccSEric Anholt * This function commits a with drm_atomic_helper_check() pre-validated state 86b501baccSEric Anholt * object. This can still fail when e.g. the framebuffer reservation fails. For 87b501baccSEric Anholt * now this doesn't implement asynchronous commits. 88b501baccSEric Anholt * 89b501baccSEric Anholt * RETURNS 90b501baccSEric Anholt * Zero for success or -errno. 91b501baccSEric Anholt */ 92b501baccSEric Anholt static int vc4_atomic_commit(struct drm_device *dev, 93b501baccSEric Anholt struct drm_atomic_state *state, 94eb63961bSMaarten Lankhorst bool nonblock) 95b501baccSEric Anholt { 96b501baccSEric Anholt struct vc4_dev *vc4 = to_vc4_dev(dev); 97b501baccSEric Anholt int ret; 98b501baccSEric Anholt 9934c8ea40SBoris Brezillon ret = drm_atomic_helper_setup_commit(state, nonblock); 10034c8ea40SBoris Brezillon if (ret) 10134c8ea40SBoris Brezillon return ret; 10226fc78f6SDerek Foreman 103cf1b372eSEric Anholt INIT_WORK(&state->commit_work, commit_work); 104cf1b372eSEric Anholt 105b501baccSEric Anholt ret = down_interruptible(&vc4->async_modeset); 106cf1b372eSEric Anholt if (ret) 107b501baccSEric Anholt return ret; 108b501baccSEric Anholt 109b501baccSEric Anholt ret = drm_atomic_helper_prepare_planes(dev, state); 110b501baccSEric Anholt if (ret) { 111b501baccSEric Anholt up(&vc4->async_modeset); 112b501baccSEric Anholt return ret; 113b501baccSEric Anholt } 114b501baccSEric Anholt 11553ad0694SEric Anholt if (!nonblock) { 11653ad0694SEric Anholt ret = drm_atomic_helper_wait_for_fences(dev, state, true); 11753ad0694SEric Anholt if (ret) { 11853ad0694SEric Anholt drm_atomic_helper_cleanup_planes(dev, state); 11953ad0694SEric Anholt up(&vc4->async_modeset); 12053ad0694SEric Anholt return ret; 12153ad0694SEric Anholt } 12253ad0694SEric Anholt } 12353ad0694SEric Anholt 124b501baccSEric Anholt /* 125b501baccSEric Anholt * This is the point of no return - everything below never fails except 126b501baccSEric Anholt * when the hw goes bonghits. Which means we can commit the new state on 127b501baccSEric Anholt * the software side now. 128b501baccSEric Anholt */ 129b501baccSEric Anholt 130d68bc0e7SMaarten Lankhorst BUG_ON(drm_atomic_helper_swap_state(state, false) < 0); 131b501baccSEric Anholt 132b501baccSEric Anholt /* 133b501baccSEric Anholt * Everything below can be run asynchronously without the need to grab 134b501baccSEric Anholt * any modeset locks at all under one condition: It must be guaranteed 135b501baccSEric Anholt * that the asynchronous work has either been cancelled (if the driver 136b501baccSEric Anholt * supports it, which at least requires that the framebuffers get 137b501baccSEric Anholt * cleaned up with drm_atomic_helper_cleanup_planes()) or completed 138b501baccSEric Anholt * before the new state gets committed on the software side with 139b501baccSEric Anholt * drm_atomic_helper_swap_state(). 140b501baccSEric Anholt * 141b501baccSEric Anholt * This scheme allows new atomic state updates to be prepared and 142b501baccSEric Anholt * checked in parallel to the asynchronous completion of the previous 143b501baccSEric Anholt * update. Which is important since compositors need to figure out the 144b501baccSEric Anholt * composition of the next frame right after having submitted the 145b501baccSEric Anholt * current layout. 146b501baccSEric Anholt */ 147b501baccSEric Anholt 1480853695cSChris Wilson drm_atomic_state_get(state); 149cf1b372eSEric Anholt if (nonblock) 150cf1b372eSEric Anholt queue_work(system_unbound_wq, &state->commit_work); 151cf1b372eSEric Anholt else 152cf1b372eSEric Anholt vc4_atomic_complete_commit(state); 153b501baccSEric Anholt 154b501baccSEric Anholt return 0; 155b501baccSEric Anholt } 156b501baccSEric Anholt 15783753117SEric Anholt static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev, 15883753117SEric Anholt struct drm_file *file_priv, 15983753117SEric Anholt const struct drm_mode_fb_cmd2 *mode_cmd) 16083753117SEric Anholt { 16183753117SEric Anholt struct drm_mode_fb_cmd2 mode_cmd_local; 16283753117SEric Anholt 16383753117SEric Anholt /* If the user didn't specify a modifier, use the 16483753117SEric Anholt * vc4_set_tiling_ioctl() state for the BO. 16583753117SEric Anholt */ 16683753117SEric Anholt if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) { 16783753117SEric Anholt struct drm_gem_object *gem_obj; 16883753117SEric Anholt struct vc4_bo *bo; 16983753117SEric Anholt 17083753117SEric Anholt gem_obj = drm_gem_object_lookup(file_priv, 17183753117SEric Anholt mode_cmd->handles[0]); 17283753117SEric Anholt if (!gem_obj) { 173fb95992aSEric Anholt DRM_DEBUG("Failed to look up GEM BO %d\n", 17483753117SEric Anholt mode_cmd->handles[0]); 17583753117SEric Anholt return ERR_PTR(-ENOENT); 17683753117SEric Anholt } 17783753117SEric Anholt bo = to_vc4_bo(gem_obj); 17883753117SEric Anholt 17983753117SEric Anholt mode_cmd_local = *mode_cmd; 18083753117SEric Anholt 18183753117SEric Anholt if (bo->t_format) { 18283753117SEric Anholt mode_cmd_local.modifier[0] = 18383753117SEric Anholt DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED; 18483753117SEric Anholt } else { 18583753117SEric Anholt mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE; 18683753117SEric Anholt } 18783753117SEric Anholt 1881d5494e9SCihangir Akturk drm_gem_object_put_unlocked(gem_obj); 18983753117SEric Anholt 19083753117SEric Anholt mode_cmd = &mode_cmd_local; 19183753117SEric Anholt } 19283753117SEric Anholt 1939762477cSNoralf Trønnes return drm_gem_fb_create(dev, file_priv, mode_cmd); 19483753117SEric Anholt } 19583753117SEric Anholt 196c8b75bcaSEric Anholt static const struct drm_mode_config_funcs vc4_mode_funcs = { 19748666d56SDerek Foreman .output_poll_changed = vc4_output_poll_changed, 198c8b75bcaSEric Anholt .atomic_check = drm_atomic_helper_check, 199b501baccSEric Anholt .atomic_commit = vc4_atomic_commit, 20083753117SEric Anholt .fb_create = vc4_fb_create, 201c8b75bcaSEric Anholt }; 202c8b75bcaSEric Anholt 203c8b75bcaSEric Anholt int vc4_kms_load(struct drm_device *dev) 204c8b75bcaSEric Anholt { 20548666d56SDerek Foreman struct vc4_dev *vc4 = to_vc4_dev(dev); 206c8b75bcaSEric Anholt int ret; 207c8b75bcaSEric Anholt 208b501baccSEric Anholt sema_init(&vc4->async_modeset, 1); 209b501baccSEric Anholt 2107d2818f5SMario Kleiner /* Set support for vblank irq fast disable, before drm_vblank_init() */ 2117d2818f5SMario Kleiner dev->vblank_disable_immediate = true; 2127d2818f5SMario Kleiner 213c8b75bcaSEric Anholt ret = drm_vblank_init(dev, dev->mode_config.num_crtc); 214c8b75bcaSEric Anholt if (ret < 0) { 215c8b75bcaSEric Anholt dev_err(dev->dev, "failed to initialize vblank\n"); 216c8b75bcaSEric Anholt return ret; 217c8b75bcaSEric Anholt } 218c8b75bcaSEric Anholt 219c8b75bcaSEric Anholt dev->mode_config.max_width = 2048; 220c8b75bcaSEric Anholt dev->mode_config.max_height = 2048; 221c8b75bcaSEric Anholt dev->mode_config.funcs = &vc4_mode_funcs; 222c8b75bcaSEric Anholt dev->mode_config.preferred_depth = 24; 223b501baccSEric Anholt dev->mode_config.async_page_flip = true; 224b501baccSEric Anholt 225c8b75bcaSEric Anholt drm_mode_config_reset(dev); 226c8b75bcaSEric Anholt 2271e70bdcbSEric Anholt if (dev->mode_config.num_connector) { 22848666d56SDerek Foreman vc4->fbdev = drm_fbdev_cma_init(dev, 32, 229c8b75bcaSEric Anholt dev->mode_config.num_connector); 23048666d56SDerek Foreman if (IS_ERR(vc4->fbdev)) 23148666d56SDerek Foreman vc4->fbdev = NULL; 2321e70bdcbSEric Anholt } 233c8b75bcaSEric Anholt 234c8b75bcaSEric Anholt drm_kms_helper_poll_init(dev); 235c8b75bcaSEric Anholt 236c8b75bcaSEric Anholt return 0; 237c8b75bcaSEric Anholt } 238