xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_kms.c (revision 1d5494e9)
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>
23c8b75bcaSEric Anholt #include "vc4_drv.h"
24c8b75bcaSEric Anholt 
2548666d56SDerek Foreman static void vc4_output_poll_changed(struct drm_device *dev)
2648666d56SDerek Foreman {
2748666d56SDerek Foreman 	struct vc4_dev *vc4 = to_vc4_dev(dev);
2848666d56SDerek Foreman 
2948666d56SDerek Foreman 	drm_fbdev_cma_hotplug_event(vc4->fbdev);
3048666d56SDerek Foreman }
3148666d56SDerek Foreman 
32b501baccSEric Anholt static void
33cf1b372eSEric Anholt vc4_atomic_complete_commit(struct drm_atomic_state *state)
34b501baccSEric Anholt {
35b501baccSEric Anholt 	struct drm_device *dev = state->dev;
36b501baccSEric Anholt 	struct vc4_dev *vc4 = to_vc4_dev(dev);
37b501baccSEric Anholt 
3834c8ea40SBoris Brezillon 	drm_atomic_helper_wait_for_fences(dev, state, false);
3934c8ea40SBoris Brezillon 
4034c8ea40SBoris Brezillon 	drm_atomic_helper_wait_for_dependencies(state);
4134c8ea40SBoris Brezillon 
42b501baccSEric Anholt 	drm_atomic_helper_commit_modeset_disables(dev, state);
43b501baccSEric Anholt 
442b58e98dSLiu Ying 	drm_atomic_helper_commit_planes(dev, state, 0);
45b501baccSEric Anholt 
46b501baccSEric Anholt 	drm_atomic_helper_commit_modeset_enables(dev, state);
47b501baccSEric Anholt 
486674a904SEric Anholt 	/* Make sure that drm_atomic_helper_wait_for_vblanks()
496674a904SEric Anholt 	 * actually waits for vblank.  If we're doing a full atomic
506674a904SEric Anholt 	 * modeset (as opposed to a vc4_update_plane() short circuit),
516674a904SEric Anholt 	 * then we need to wait for scanout to be done with our
526674a904SEric Anholt 	 * display lists before we free it and potentially reallocate
536674a904SEric Anholt 	 * and overwrite the dlist memory with a new modeset.
546674a904SEric Anholt 	 */
556674a904SEric Anholt 	state->legacy_cursor_update = false;
566674a904SEric Anholt 
5734c8ea40SBoris Brezillon 	drm_atomic_helper_commit_hw_done(state);
5834c8ea40SBoris Brezillon 
59b501baccSEric Anholt 	drm_atomic_helper_wait_for_vblanks(dev, state);
60b501baccSEric Anholt 
61b501baccSEric Anholt 	drm_atomic_helper_cleanup_planes(dev, state);
62b501baccSEric Anholt 
6334c8ea40SBoris Brezillon 	drm_atomic_helper_commit_cleanup_done(state);
6434c8ea40SBoris Brezillon 
650853695cSChris Wilson 	drm_atomic_state_put(state);
66b501baccSEric Anholt 
67b501baccSEric Anholt 	up(&vc4->async_modeset);
68b501baccSEric Anholt }
69b501baccSEric Anholt 
70cf1b372eSEric Anholt static void commit_work(struct work_struct *work)
71b501baccSEric Anholt {
72cf1b372eSEric Anholt 	struct drm_atomic_state *state = container_of(work,
73cf1b372eSEric Anholt 						      struct drm_atomic_state,
74cf1b372eSEric Anholt 						      commit_work);
75cf1b372eSEric Anholt 	vc4_atomic_complete_commit(state);
76b501baccSEric Anholt }
77b501baccSEric Anholt 
78b501baccSEric Anholt /**
79b501baccSEric Anholt  * vc4_atomic_commit - commit validated state object
80b501baccSEric Anholt  * @dev: DRM device
81b501baccSEric Anholt  * @state: the driver state object
82eb63961bSMaarten Lankhorst  * @nonblock: nonblocking commit
83b501baccSEric Anholt  *
84b501baccSEric Anholt  * This function commits a with drm_atomic_helper_check() pre-validated state
85b501baccSEric Anholt  * object. This can still fail when e.g. the framebuffer reservation fails. For
86b501baccSEric Anholt  * now this doesn't implement asynchronous commits.
87b501baccSEric Anholt  *
88b501baccSEric Anholt  * RETURNS
89b501baccSEric Anholt  * Zero for success or -errno.
90b501baccSEric Anholt  */
91b501baccSEric Anholt static int vc4_atomic_commit(struct drm_device *dev,
92b501baccSEric Anholt 			     struct drm_atomic_state *state,
93eb63961bSMaarten Lankhorst 			     bool nonblock)
94b501baccSEric Anholt {
95b501baccSEric Anholt 	struct vc4_dev *vc4 = to_vc4_dev(dev);
96b501baccSEric Anholt 	int ret;
97b501baccSEric Anholt 
9834c8ea40SBoris Brezillon 	ret = drm_atomic_helper_setup_commit(state, nonblock);
9934c8ea40SBoris Brezillon 	if (ret)
10034c8ea40SBoris Brezillon 		return ret;
10126fc78f6SDerek Foreman 
102cf1b372eSEric Anholt 	INIT_WORK(&state->commit_work, commit_work);
103cf1b372eSEric Anholt 
104b501baccSEric Anholt 	ret = down_interruptible(&vc4->async_modeset);
105cf1b372eSEric Anholt 	if (ret)
106b501baccSEric Anholt 		return ret;
107b501baccSEric Anholt 
108b501baccSEric Anholt 	ret = drm_atomic_helper_prepare_planes(dev, state);
109b501baccSEric Anholt 	if (ret) {
110b501baccSEric Anholt 		up(&vc4->async_modeset);
111b501baccSEric Anholt 		return ret;
112b501baccSEric Anholt 	}
113b501baccSEric Anholt 
11453ad0694SEric Anholt 	if (!nonblock) {
11553ad0694SEric Anholt 		ret = drm_atomic_helper_wait_for_fences(dev, state, true);
11653ad0694SEric Anholt 		if (ret) {
11753ad0694SEric Anholt 			drm_atomic_helper_cleanup_planes(dev, state);
11853ad0694SEric Anholt 			up(&vc4->async_modeset);
11953ad0694SEric Anholt 			return ret;
12053ad0694SEric Anholt 		}
12153ad0694SEric Anholt 	}
12253ad0694SEric Anholt 
123b501baccSEric Anholt 	/*
124b501baccSEric Anholt 	 * This is the point of no return - everything below never fails except
125b501baccSEric Anholt 	 * when the hw goes bonghits. Which means we can commit the new state on
126b501baccSEric Anholt 	 * the software side now.
127b501baccSEric Anholt 	 */
128b501baccSEric Anholt 
129d68bc0e7SMaarten Lankhorst 	BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
130b501baccSEric Anholt 
131b501baccSEric Anholt 	/*
132b501baccSEric Anholt 	 * Everything below can be run asynchronously without the need to grab
133b501baccSEric Anholt 	 * any modeset locks at all under one condition: It must be guaranteed
134b501baccSEric Anholt 	 * that the asynchronous work has either been cancelled (if the driver
135b501baccSEric Anholt 	 * supports it, which at least requires that the framebuffers get
136b501baccSEric Anholt 	 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
137b501baccSEric Anholt 	 * before the new state gets committed on the software side with
138b501baccSEric Anholt 	 * drm_atomic_helper_swap_state().
139b501baccSEric Anholt 	 *
140b501baccSEric Anholt 	 * This scheme allows new atomic state updates to be prepared and
141b501baccSEric Anholt 	 * checked in parallel to the asynchronous completion of the previous
142b501baccSEric Anholt 	 * update. Which is important since compositors need to figure out the
143b501baccSEric Anholt 	 * composition of the next frame right after having submitted the
144b501baccSEric Anholt 	 * current layout.
145b501baccSEric Anholt 	 */
146b501baccSEric Anholt 
1470853695cSChris Wilson 	drm_atomic_state_get(state);
148cf1b372eSEric Anholt 	if (nonblock)
149cf1b372eSEric Anholt 		queue_work(system_unbound_wq, &state->commit_work);
150cf1b372eSEric Anholt 	else
151cf1b372eSEric Anholt 		vc4_atomic_complete_commit(state);
152b501baccSEric Anholt 
153b501baccSEric Anholt 	return 0;
154b501baccSEric Anholt }
155b501baccSEric Anholt 
15683753117SEric Anholt static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
15783753117SEric Anholt 					     struct drm_file *file_priv,
15883753117SEric Anholt 					     const struct drm_mode_fb_cmd2 *mode_cmd)
15983753117SEric Anholt {
16083753117SEric Anholt 	struct drm_mode_fb_cmd2 mode_cmd_local;
16183753117SEric Anholt 
16283753117SEric Anholt 	/* If the user didn't specify a modifier, use the
16383753117SEric Anholt 	 * vc4_set_tiling_ioctl() state for the BO.
16483753117SEric Anholt 	 */
16583753117SEric Anholt 	if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
16683753117SEric Anholt 		struct drm_gem_object *gem_obj;
16783753117SEric Anholt 		struct vc4_bo *bo;
16883753117SEric Anholt 
16983753117SEric Anholt 		gem_obj = drm_gem_object_lookup(file_priv,
17083753117SEric Anholt 						mode_cmd->handles[0]);
17183753117SEric Anholt 		if (!gem_obj) {
17283753117SEric Anholt 			DRM_ERROR("Failed to look up GEM BO %d\n",
17383753117SEric Anholt 				  mode_cmd->handles[0]);
17483753117SEric Anholt 			return ERR_PTR(-ENOENT);
17583753117SEric Anholt 		}
17683753117SEric Anholt 		bo = to_vc4_bo(gem_obj);
17783753117SEric Anholt 
17883753117SEric Anholt 		mode_cmd_local = *mode_cmd;
17983753117SEric Anholt 
18083753117SEric Anholt 		if (bo->t_format) {
18183753117SEric Anholt 			mode_cmd_local.modifier[0] =
18283753117SEric Anholt 				DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
18383753117SEric Anholt 		} else {
18483753117SEric Anholt 			mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
18583753117SEric Anholt 		}
18683753117SEric Anholt 
1871d5494e9SCihangir Akturk 		drm_gem_object_put_unlocked(gem_obj);
18883753117SEric Anholt 
18983753117SEric Anholt 		mode_cmd = &mode_cmd_local;
19083753117SEric Anholt 	}
19183753117SEric Anholt 
19283753117SEric Anholt 	return drm_fb_cma_create(dev, file_priv, mode_cmd);
19383753117SEric Anholt }
19483753117SEric Anholt 
195c8b75bcaSEric Anholt static const struct drm_mode_config_funcs vc4_mode_funcs = {
19648666d56SDerek Foreman 	.output_poll_changed = vc4_output_poll_changed,
197c8b75bcaSEric Anholt 	.atomic_check = drm_atomic_helper_check,
198b501baccSEric Anholt 	.atomic_commit = vc4_atomic_commit,
19983753117SEric Anholt 	.fb_create = vc4_fb_create,
200c8b75bcaSEric Anholt };
201c8b75bcaSEric Anholt 
202c8b75bcaSEric Anholt int vc4_kms_load(struct drm_device *dev)
203c8b75bcaSEric Anholt {
20448666d56SDerek Foreman 	struct vc4_dev *vc4 = to_vc4_dev(dev);
205c8b75bcaSEric Anholt 	int ret;
206c8b75bcaSEric Anholt 
207b501baccSEric Anholt 	sema_init(&vc4->async_modeset, 1);
208b501baccSEric Anholt 
2097d2818f5SMario Kleiner 	/* Set support for vblank irq fast disable, before drm_vblank_init() */
2107d2818f5SMario Kleiner 	dev->vblank_disable_immediate = true;
2117d2818f5SMario Kleiner 
212c8b75bcaSEric Anholt 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
213c8b75bcaSEric Anholt 	if (ret < 0) {
214c8b75bcaSEric Anholt 		dev_err(dev->dev, "failed to initialize vblank\n");
215c8b75bcaSEric Anholt 		return ret;
216c8b75bcaSEric Anholt 	}
217c8b75bcaSEric Anholt 
218c8b75bcaSEric Anholt 	dev->mode_config.max_width = 2048;
219c8b75bcaSEric Anholt 	dev->mode_config.max_height = 2048;
220c8b75bcaSEric Anholt 	dev->mode_config.funcs = &vc4_mode_funcs;
221c8b75bcaSEric Anholt 	dev->mode_config.preferred_depth = 24;
222b501baccSEric Anholt 	dev->mode_config.async_page_flip = true;
223b501baccSEric Anholt 
224c8b75bcaSEric Anholt 	drm_mode_config_reset(dev);
225c8b75bcaSEric Anholt 
2261e70bdcbSEric Anholt 	if (dev->mode_config.num_connector) {
22748666d56SDerek Foreman 		vc4->fbdev = drm_fbdev_cma_init(dev, 32,
228c8b75bcaSEric Anholt 						dev->mode_config.num_connector);
22948666d56SDerek Foreman 		if (IS_ERR(vc4->fbdev))
23048666d56SDerek Foreman 			vc4->fbdev = NULL;
2311e70bdcbSEric Anholt 	}
232c8b75bcaSEric Anholt 
233c8b75bcaSEric Anholt 	drm_kms_helper_poll_init(dev);
234c8b75bcaSEric Anholt 
235c8b75bcaSEric Anholt 	return 0;
236c8b75bcaSEric Anholt }
237