xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_kms.c (revision 833cd78a)
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
96eb63961bSMaarten Lankhorst  * @nonblock: nonblocking 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,
107eb63961bSMaarten Lankhorst 			     bool nonblock)
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;
114833cd78aSDaniel Vetter 	struct drm_plane *plane;
115833cd78aSDaniel Vetter 	struct drm_plane_state *new_state;
116b501baccSEric Anholt 
117b501baccSEric Anholt 	c = commit_init(state);
118b501baccSEric Anholt 	if (!c)
119b501baccSEric Anholt 		return -ENOMEM;
120b501baccSEric Anholt 
121b501baccSEric Anholt 	/* Make sure that any outstanding modesets have finished. */
122b501baccSEric Anholt 	ret = down_interruptible(&vc4->async_modeset);
123b501baccSEric Anholt 	if (ret) {
124b501baccSEric Anholt 		kfree(c);
125b501baccSEric Anholt 		return ret;
126b501baccSEric Anholt 	}
127b501baccSEric Anholt 
128b501baccSEric Anholt 	ret = drm_atomic_helper_prepare_planes(dev, state);
129b501baccSEric Anholt 	if (ret) {
130b501baccSEric Anholt 		kfree(c);
131b501baccSEric Anholt 		up(&vc4->async_modeset);
132b501baccSEric Anholt 		return ret;
133b501baccSEric Anholt 	}
134b501baccSEric Anholt 
135833cd78aSDaniel Vetter 	for_each_plane_in_state(state, plane, new_state, i) {
136b501baccSEric Anholt 		if ((plane->state->fb != new_state->fb) && new_state->fb) {
137b501baccSEric Anholt 			struct drm_gem_cma_object *cma_bo =
138b501baccSEric Anholt 				drm_fb_cma_get_gem_obj(new_state->fb, 0);
139b501baccSEric Anholt 			struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
140b501baccSEric Anholt 
141b501baccSEric Anholt 			wait_seqno = max(bo->seqno, wait_seqno);
142b501baccSEric Anholt 		}
143b501baccSEric Anholt 	}
144b501baccSEric Anholt 
145b501baccSEric Anholt 	/*
146b501baccSEric Anholt 	 * This is the point of no return - everything below never fails except
147b501baccSEric Anholt 	 * when the hw goes bonghits. Which means we can commit the new state on
148b501baccSEric Anholt 	 * the software side now.
149b501baccSEric Anholt 	 */
150b501baccSEric Anholt 
151b501baccSEric Anholt 	drm_atomic_helper_swap_state(dev, state);
152b501baccSEric Anholt 
153b501baccSEric Anholt 	/*
154b501baccSEric Anholt 	 * Everything below can be run asynchronously without the need to grab
155b501baccSEric Anholt 	 * any modeset locks at all under one condition: It must be guaranteed
156b501baccSEric Anholt 	 * that the asynchronous work has either been cancelled (if the driver
157b501baccSEric Anholt 	 * supports it, which at least requires that the framebuffers get
158b501baccSEric Anholt 	 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
159b501baccSEric Anholt 	 * before the new state gets committed on the software side with
160b501baccSEric Anholt 	 * drm_atomic_helper_swap_state().
161b501baccSEric Anholt 	 *
162b501baccSEric Anholt 	 * This scheme allows new atomic state updates to be prepared and
163b501baccSEric Anholt 	 * checked in parallel to the asynchronous completion of the previous
164b501baccSEric Anholt 	 * update. Which is important since compositors need to figure out the
165b501baccSEric Anholt 	 * composition of the next frame right after having submitted the
166b501baccSEric Anholt 	 * current layout.
167b501baccSEric Anholt 	 */
168b501baccSEric Anholt 
169eb63961bSMaarten Lankhorst 	if (nonblock) {
170b501baccSEric Anholt 		vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
171b501baccSEric Anholt 				   vc4_atomic_complete_commit_seqno_cb);
172b501baccSEric Anholt 	} else {
173b501baccSEric Anholt 		vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
174b501baccSEric Anholt 		vc4_atomic_complete_commit(c);
175b501baccSEric Anholt 	}
176b501baccSEric Anholt 
177b501baccSEric Anholt 	return 0;
178b501baccSEric Anholt }
179b501baccSEric Anholt 
180c8b75bcaSEric Anholt static const struct drm_mode_config_funcs vc4_mode_funcs = {
18148666d56SDerek Foreman 	.output_poll_changed = vc4_output_poll_changed,
182c8b75bcaSEric Anholt 	.atomic_check = drm_atomic_helper_check,
183b501baccSEric Anholt 	.atomic_commit = vc4_atomic_commit,
184c8b75bcaSEric Anholt 	.fb_create = drm_fb_cma_create,
185c8b75bcaSEric Anholt };
186c8b75bcaSEric Anholt 
187c8b75bcaSEric Anholt int vc4_kms_load(struct drm_device *dev)
188c8b75bcaSEric Anholt {
18948666d56SDerek Foreman 	struct vc4_dev *vc4 = to_vc4_dev(dev);
190c8b75bcaSEric Anholt 	int ret;
191c8b75bcaSEric Anholt 
192b501baccSEric Anholt 	sema_init(&vc4->async_modeset, 1);
193b501baccSEric Anholt 
194c8b75bcaSEric Anholt 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
195c8b75bcaSEric Anholt 	if (ret < 0) {
196c8b75bcaSEric Anholt 		dev_err(dev->dev, "failed to initialize vblank\n");
197c8b75bcaSEric Anholt 		return ret;
198c8b75bcaSEric Anholt 	}
199c8b75bcaSEric Anholt 
200c8b75bcaSEric Anholt 	dev->mode_config.max_width = 2048;
201c8b75bcaSEric Anholt 	dev->mode_config.max_height = 2048;
202c8b75bcaSEric Anholt 	dev->mode_config.funcs = &vc4_mode_funcs;
203c8b75bcaSEric Anholt 	dev->mode_config.preferred_depth = 24;
204b501baccSEric Anholt 	dev->mode_config.async_page_flip = true;
205b501baccSEric Anholt 
206c8b75bcaSEric Anholt 	drm_mode_config_reset(dev);
207c8b75bcaSEric Anholt 
20848666d56SDerek Foreman 	vc4->fbdev = drm_fbdev_cma_init(dev, 32,
209c8b75bcaSEric Anholt 					dev->mode_config.num_crtc,
210c8b75bcaSEric Anholt 					dev->mode_config.num_connector);
21148666d56SDerek Foreman 	if (IS_ERR(vc4->fbdev))
21248666d56SDerek Foreman 		vc4->fbdev = NULL;
213c8b75bcaSEric Anholt 
214c8b75bcaSEric Anholt 	drm_kms_helper_poll_init(dev);
215c8b75bcaSEric Anholt 
216c8b75bcaSEric Anholt 	return 0;
217c8b75bcaSEric Anholt }
218