xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_kms.c (revision 6674a904)
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 
17c8b75bcaSEric Anholt #include "drm_crtc.h"
18b501baccSEric Anholt #include "drm_atomic.h"
19c8b75bcaSEric Anholt #include "drm_atomic_helper.h"
20c8b75bcaSEric Anholt #include "drm_crtc_helper.h"
21c8b75bcaSEric Anholt #include "drm_plane_helper.h"
22c8b75bcaSEric Anholt #include "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 	if (vc4->fbdev)
3048666d56SDerek Foreman 		drm_fbdev_cma_hotplug_event(vc4->fbdev);
3148666d56SDerek Foreman }
3248666d56SDerek Foreman 
33b501baccSEric Anholt struct vc4_commit {
34b501baccSEric Anholt 	struct drm_device *dev;
35b501baccSEric Anholt 	struct drm_atomic_state *state;
36b501baccSEric Anholt 	struct vc4_seqno_cb cb;
37b501baccSEric Anholt };
38b501baccSEric Anholt 
39b501baccSEric Anholt static void
40b501baccSEric Anholt vc4_atomic_complete_commit(struct vc4_commit *c)
41b501baccSEric Anholt {
42b501baccSEric Anholt 	struct drm_atomic_state *state = c->state;
43b501baccSEric Anholt 	struct drm_device *dev = state->dev;
44b501baccSEric Anholt 	struct vc4_dev *vc4 = to_vc4_dev(dev);
45b501baccSEric Anholt 
46b501baccSEric Anholt 	drm_atomic_helper_commit_modeset_disables(dev, state);
47b501baccSEric Anholt 
48b501baccSEric Anholt 	drm_atomic_helper_commit_planes(dev, state, false);
49b501baccSEric Anholt 
50b501baccSEric Anholt 	drm_atomic_helper_commit_modeset_enables(dev, state);
51b501baccSEric Anholt 
526674a904SEric Anholt 	/* Make sure that drm_atomic_helper_wait_for_vblanks()
536674a904SEric Anholt 	 * actually waits for vblank.  If we're doing a full atomic
546674a904SEric Anholt 	 * modeset (as opposed to a vc4_update_plane() short circuit),
556674a904SEric Anholt 	 * then we need to wait for scanout to be done with our
566674a904SEric Anholt 	 * display lists before we free it and potentially reallocate
576674a904SEric Anholt 	 * and overwrite the dlist memory with a new modeset.
586674a904SEric Anholt 	 */
596674a904SEric Anholt 	state->legacy_cursor_update = false;
606674a904SEric Anholt 
61b501baccSEric Anholt 	drm_atomic_helper_wait_for_vblanks(dev, state);
62b501baccSEric Anholt 
63b501baccSEric Anholt 	drm_atomic_helper_cleanup_planes(dev, state);
64b501baccSEric Anholt 
65b501baccSEric Anholt 	drm_atomic_state_free(state);
66b501baccSEric Anholt 
67b501baccSEric Anholt 	up(&vc4->async_modeset);
68b501baccSEric Anholt 
69b501baccSEric Anholt 	kfree(c);
70b501baccSEric Anholt }
71b501baccSEric Anholt 
72b501baccSEric Anholt static void
73b501baccSEric Anholt vc4_atomic_complete_commit_seqno_cb(struct vc4_seqno_cb *cb)
74b501baccSEric Anholt {
75b501baccSEric Anholt 	struct vc4_commit *c = container_of(cb, struct vc4_commit, cb);
76b501baccSEric Anholt 
77b501baccSEric Anholt 	vc4_atomic_complete_commit(c);
78b501baccSEric Anholt }
79b501baccSEric Anholt 
80b501baccSEric Anholt static struct vc4_commit *commit_init(struct drm_atomic_state *state)
81b501baccSEric Anholt {
82b501baccSEric Anholt 	struct vc4_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
83b501baccSEric Anholt 
84b501baccSEric Anholt 	if (!c)
85b501baccSEric Anholt 		return NULL;
86b501baccSEric Anholt 	c->dev = state->dev;
87b501baccSEric Anholt 	c->state = state;
88b501baccSEric Anholt 
89b501baccSEric Anholt 	return c;
90b501baccSEric Anholt }
91b501baccSEric Anholt 
92b501baccSEric Anholt /**
93b501baccSEric Anholt  * vc4_atomic_commit - commit validated state object
94b501baccSEric Anholt  * @dev: DRM device
95b501baccSEric Anholt  * @state: the driver state object
96b501baccSEric Anholt  * @async: asynchronous commit
97b501baccSEric Anholt  *
98b501baccSEric Anholt  * This function commits a with drm_atomic_helper_check() pre-validated state
99b501baccSEric Anholt  * object. This can still fail when e.g. the framebuffer reservation fails. For
100b501baccSEric Anholt  * now this doesn't implement asynchronous commits.
101b501baccSEric Anholt  *
102b501baccSEric Anholt  * RETURNS
103b501baccSEric Anholt  * Zero for success or -errno.
104b501baccSEric Anholt  */
105b501baccSEric Anholt static int vc4_atomic_commit(struct drm_device *dev,
106b501baccSEric Anholt 			     struct drm_atomic_state *state,
107b501baccSEric Anholt 			     bool async)
108b501baccSEric Anholt {
109b501baccSEric Anholt 	struct vc4_dev *vc4 = to_vc4_dev(dev);
110b501baccSEric Anholt 	int ret;
111b501baccSEric Anholt 	int i;
112b501baccSEric Anholt 	uint64_t wait_seqno = 0;
113b501baccSEric Anholt 	struct vc4_commit *c;
114b501baccSEric Anholt 
115b501baccSEric Anholt 	c = commit_init(state);
116b501baccSEric Anholt 	if (!c)
117b501baccSEric Anholt 		return -ENOMEM;
118b501baccSEric Anholt 
119b501baccSEric Anholt 	/* Make sure that any outstanding modesets have finished. */
120b501baccSEric Anholt 	ret = down_interruptible(&vc4->async_modeset);
121b501baccSEric Anholt 	if (ret) {
122b501baccSEric Anholt 		kfree(c);
123b501baccSEric Anholt 		return ret;
124b501baccSEric Anholt 	}
125b501baccSEric Anholt 
126b501baccSEric Anholt 	ret = drm_atomic_helper_prepare_planes(dev, state);
127b501baccSEric Anholt 	if (ret) {
128b501baccSEric Anholt 		kfree(c);
129b501baccSEric Anholt 		up(&vc4->async_modeset);
130b501baccSEric Anholt 		return ret;
131b501baccSEric Anholt 	}
132b501baccSEric Anholt 
133b501baccSEric Anholt 	for (i = 0; i < dev->mode_config.num_total_plane; i++) {
134b501baccSEric Anholt 		struct drm_plane *plane = state->planes[i];
135b501baccSEric Anholt 		struct drm_plane_state *new_state = state->plane_states[i];
136b501baccSEric Anholt 
137b501baccSEric Anholt 		if (!plane)
138b501baccSEric Anholt 			continue;
139b501baccSEric Anholt 
140b501baccSEric Anholt 		if ((plane->state->fb != new_state->fb) && new_state->fb) {
141b501baccSEric Anholt 			struct drm_gem_cma_object *cma_bo =
142b501baccSEric Anholt 				drm_fb_cma_get_gem_obj(new_state->fb, 0);
143b501baccSEric Anholt 			struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
144b501baccSEric Anholt 
145b501baccSEric Anholt 			wait_seqno = max(bo->seqno, wait_seqno);
146b501baccSEric Anholt 		}
147b501baccSEric Anholt 	}
148b501baccSEric Anholt 
149b501baccSEric Anholt 	/*
150b501baccSEric Anholt 	 * This is the point of no return - everything below never fails except
151b501baccSEric Anholt 	 * when the hw goes bonghits. Which means we can commit the new state on
152b501baccSEric Anholt 	 * the software side now.
153b501baccSEric Anholt 	 */
154b501baccSEric Anholt 
155b501baccSEric Anholt 	drm_atomic_helper_swap_state(dev, state);
156b501baccSEric Anholt 
157b501baccSEric Anholt 	/*
158b501baccSEric Anholt 	 * Everything below can be run asynchronously without the need to grab
159b501baccSEric Anholt 	 * any modeset locks at all under one condition: It must be guaranteed
160b501baccSEric Anholt 	 * that the asynchronous work has either been cancelled (if the driver
161b501baccSEric Anholt 	 * supports it, which at least requires that the framebuffers get
162b501baccSEric Anholt 	 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
163b501baccSEric Anholt 	 * before the new state gets committed on the software side with
164b501baccSEric Anholt 	 * drm_atomic_helper_swap_state().
165b501baccSEric Anholt 	 *
166b501baccSEric Anholt 	 * This scheme allows new atomic state updates to be prepared and
167b501baccSEric Anholt 	 * checked in parallel to the asynchronous completion of the previous
168b501baccSEric Anholt 	 * update. Which is important since compositors need to figure out the
169b501baccSEric Anholt 	 * composition of the next frame right after having submitted the
170b501baccSEric Anholt 	 * current layout.
171b501baccSEric Anholt 	 */
172b501baccSEric Anholt 
173b501baccSEric Anholt 	if (async) {
174b501baccSEric Anholt 		vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
175b501baccSEric Anholt 				   vc4_atomic_complete_commit_seqno_cb);
176b501baccSEric Anholt 	} else {
177b501baccSEric Anholt 		vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
178b501baccSEric Anholt 		vc4_atomic_complete_commit(c);
179b501baccSEric Anholt 	}
180b501baccSEric Anholt 
181b501baccSEric Anholt 	return 0;
182b501baccSEric Anholt }
183b501baccSEric Anholt 
184c8b75bcaSEric Anholt static const struct drm_mode_config_funcs vc4_mode_funcs = {
18548666d56SDerek Foreman 	.output_poll_changed = vc4_output_poll_changed,
186c8b75bcaSEric Anholt 	.atomic_check = drm_atomic_helper_check,
187b501baccSEric Anholt 	.atomic_commit = vc4_atomic_commit,
188c8b75bcaSEric Anholt 	.fb_create = drm_fb_cma_create,
189c8b75bcaSEric Anholt };
190c8b75bcaSEric Anholt 
191c8b75bcaSEric Anholt int vc4_kms_load(struct drm_device *dev)
192c8b75bcaSEric Anholt {
19348666d56SDerek Foreman 	struct vc4_dev *vc4 = to_vc4_dev(dev);
194c8b75bcaSEric Anholt 	int ret;
195c8b75bcaSEric Anholt 
196b501baccSEric Anholt 	sema_init(&vc4->async_modeset, 1);
197b501baccSEric Anholt 
198c8b75bcaSEric Anholt 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
199c8b75bcaSEric Anholt 	if (ret < 0) {
200c8b75bcaSEric Anholt 		dev_err(dev->dev, "failed to initialize vblank\n");
201c8b75bcaSEric Anholt 		return ret;
202c8b75bcaSEric Anholt 	}
203c8b75bcaSEric Anholt 
204c8b75bcaSEric Anholt 	dev->mode_config.max_width = 2048;
205c8b75bcaSEric Anholt 	dev->mode_config.max_height = 2048;
206c8b75bcaSEric Anholt 	dev->mode_config.funcs = &vc4_mode_funcs;
207c8b75bcaSEric Anholt 	dev->mode_config.preferred_depth = 24;
208b501baccSEric Anholt 	dev->mode_config.async_page_flip = true;
209b501baccSEric Anholt 
21098a44504SDerek Foreman 	dev->vblank_disable_allowed = true;
211c8b75bcaSEric Anholt 
212c8b75bcaSEric Anholt 	drm_mode_config_reset(dev);
213c8b75bcaSEric Anholt 
21448666d56SDerek Foreman 	vc4->fbdev = drm_fbdev_cma_init(dev, 32,
215c8b75bcaSEric Anholt 					dev->mode_config.num_crtc,
216c8b75bcaSEric Anholt 					dev->mode_config.num_connector);
21748666d56SDerek Foreman 	if (IS_ERR(vc4->fbdev))
21848666d56SDerek Foreman 		vc4->fbdev = NULL;
219c8b75bcaSEric Anholt 
220c8b75bcaSEric Anholt 	drm_kms_helper_poll_init(dev);
221c8b75bcaSEric Anholt 
222c8b75bcaSEric Anholt 	return 0;
223c8b75bcaSEric Anholt }
224