xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_kms.c (revision 4da722ca)
1 /*
2  * Copyright (C) 2015 Broadcom
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 
9 /**
10  * DOC: VC4 KMS
11  *
12  * This is the general code for implementing KMS mode setting that
13  * doesn't clearly associate with any of the other objects (plane,
14  * crtc, HDMI encoder).
15  */
16 
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_crtc_helper.h>
21 #include <drm/drm_plane_helper.h>
22 #include <drm/drm_fb_cma_helper.h>
23 #include "vc4_drv.h"
24 
25 static void vc4_output_poll_changed(struct drm_device *dev)
26 {
27 	struct vc4_dev *vc4 = to_vc4_dev(dev);
28 
29 	drm_fbdev_cma_hotplug_event(vc4->fbdev);
30 }
31 
32 struct vc4_commit {
33 	struct drm_device *dev;
34 	struct drm_atomic_state *state;
35 	struct vc4_seqno_cb cb;
36 };
37 
38 static void
39 vc4_atomic_complete_commit(struct vc4_commit *c)
40 {
41 	struct drm_atomic_state *state = c->state;
42 	struct drm_device *dev = state->dev;
43 	struct vc4_dev *vc4 = to_vc4_dev(dev);
44 
45 	drm_atomic_helper_wait_for_fences(dev, state, false);
46 
47 	drm_atomic_helper_wait_for_dependencies(state);
48 
49 	drm_atomic_helper_commit_modeset_disables(dev, state);
50 
51 	drm_atomic_helper_commit_planes(dev, state, 0);
52 
53 	drm_atomic_helper_commit_modeset_enables(dev, state);
54 
55 	/* Make sure that drm_atomic_helper_wait_for_vblanks()
56 	 * actually waits for vblank.  If we're doing a full atomic
57 	 * modeset (as opposed to a vc4_update_plane() short circuit),
58 	 * then we need to wait for scanout to be done with our
59 	 * display lists before we free it and potentially reallocate
60 	 * and overwrite the dlist memory with a new modeset.
61 	 */
62 	state->legacy_cursor_update = false;
63 
64 	drm_atomic_helper_commit_hw_done(state);
65 
66 	drm_atomic_helper_wait_for_vblanks(dev, state);
67 
68 	drm_atomic_helper_cleanup_planes(dev, state);
69 
70 	drm_atomic_helper_commit_cleanup_done(state);
71 
72 	drm_atomic_state_put(state);
73 
74 	up(&vc4->async_modeset);
75 
76 	kfree(c);
77 }
78 
79 static void
80 vc4_atomic_complete_commit_seqno_cb(struct vc4_seqno_cb *cb)
81 {
82 	struct vc4_commit *c = container_of(cb, struct vc4_commit, cb);
83 
84 	vc4_atomic_complete_commit(c);
85 }
86 
87 static struct vc4_commit *commit_init(struct drm_atomic_state *state)
88 {
89 	struct vc4_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
90 
91 	if (!c)
92 		return NULL;
93 	c->dev = state->dev;
94 	c->state = state;
95 
96 	return c;
97 }
98 
99 /**
100  * vc4_atomic_commit - commit validated state object
101  * @dev: DRM device
102  * @state: the driver state object
103  * @nonblock: nonblocking commit
104  *
105  * This function commits a with drm_atomic_helper_check() pre-validated state
106  * object. This can still fail when e.g. the framebuffer reservation fails. For
107  * now this doesn't implement asynchronous commits.
108  *
109  * RETURNS
110  * Zero for success or -errno.
111  */
112 static int vc4_atomic_commit(struct drm_device *dev,
113 			     struct drm_atomic_state *state,
114 			     bool nonblock)
115 {
116 	struct vc4_dev *vc4 = to_vc4_dev(dev);
117 	int ret;
118 	int i;
119 	uint64_t wait_seqno = 0;
120 	struct vc4_commit *c;
121 	struct drm_plane *plane;
122 	struct drm_plane_state *new_state;
123 
124 	c = commit_init(state);
125 	if (!c)
126 		return -ENOMEM;
127 
128 	ret = drm_atomic_helper_setup_commit(state, nonblock);
129 	if (ret)
130 		return ret;
131 
132 	ret = down_interruptible(&vc4->async_modeset);
133 	if (ret) {
134 		kfree(c);
135 		return ret;
136 	}
137 
138 	ret = drm_atomic_helper_prepare_planes(dev, state);
139 	if (ret) {
140 		kfree(c);
141 		up(&vc4->async_modeset);
142 		return ret;
143 	}
144 
145 	for_each_plane_in_state(state, plane, new_state, i) {
146 		if ((plane->state->fb != new_state->fb) && new_state->fb) {
147 			struct drm_gem_cma_object *cma_bo =
148 				drm_fb_cma_get_gem_obj(new_state->fb, 0);
149 			struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
150 
151 			wait_seqno = max(bo->seqno, wait_seqno);
152 		}
153 	}
154 
155 	/*
156 	 * This is the point of no return - everything below never fails except
157 	 * when the hw goes bonghits. Which means we can commit the new state on
158 	 * the software side now.
159 	 */
160 
161 	drm_atomic_helper_swap_state(state, true);
162 
163 	/*
164 	 * Everything below can be run asynchronously without the need to grab
165 	 * any modeset locks at all under one condition: It must be guaranteed
166 	 * that the asynchronous work has either been cancelled (if the driver
167 	 * supports it, which at least requires that the framebuffers get
168 	 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
169 	 * before the new state gets committed on the software side with
170 	 * drm_atomic_helper_swap_state().
171 	 *
172 	 * This scheme allows new atomic state updates to be prepared and
173 	 * checked in parallel to the asynchronous completion of the previous
174 	 * update. Which is important since compositors need to figure out the
175 	 * composition of the next frame right after having submitted the
176 	 * current layout.
177 	 */
178 
179 	drm_atomic_state_get(state);
180 	if (nonblock) {
181 		vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
182 				   vc4_atomic_complete_commit_seqno_cb);
183 	} else {
184 		vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
185 		vc4_atomic_complete_commit(c);
186 	}
187 
188 	return 0;
189 }
190 
191 static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
192 					     struct drm_file *file_priv,
193 					     const struct drm_mode_fb_cmd2 *mode_cmd)
194 {
195 	struct drm_mode_fb_cmd2 mode_cmd_local;
196 
197 	/* If the user didn't specify a modifier, use the
198 	 * vc4_set_tiling_ioctl() state for the BO.
199 	 */
200 	if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
201 		struct drm_gem_object *gem_obj;
202 		struct vc4_bo *bo;
203 
204 		gem_obj = drm_gem_object_lookup(file_priv,
205 						mode_cmd->handles[0]);
206 		if (!gem_obj) {
207 			DRM_ERROR("Failed to look up GEM BO %d\n",
208 				  mode_cmd->handles[0]);
209 			return ERR_PTR(-ENOENT);
210 		}
211 		bo = to_vc4_bo(gem_obj);
212 
213 		mode_cmd_local = *mode_cmd;
214 
215 		if (bo->t_format) {
216 			mode_cmd_local.modifier[0] =
217 				DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
218 		} else {
219 			mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
220 		}
221 
222 		drm_gem_object_unreference_unlocked(gem_obj);
223 
224 		mode_cmd = &mode_cmd_local;
225 	}
226 
227 	return drm_fb_cma_create(dev, file_priv, mode_cmd);
228 }
229 
230 static const struct drm_mode_config_funcs vc4_mode_funcs = {
231 	.output_poll_changed = vc4_output_poll_changed,
232 	.atomic_check = drm_atomic_helper_check,
233 	.atomic_commit = vc4_atomic_commit,
234 	.fb_create = vc4_fb_create,
235 };
236 
237 int vc4_kms_load(struct drm_device *dev)
238 {
239 	struct vc4_dev *vc4 = to_vc4_dev(dev);
240 	int ret;
241 
242 	sema_init(&vc4->async_modeset, 1);
243 
244 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
245 	if (ret < 0) {
246 		dev_err(dev->dev, "failed to initialize vblank\n");
247 		return ret;
248 	}
249 
250 	dev->mode_config.max_width = 2048;
251 	dev->mode_config.max_height = 2048;
252 	dev->mode_config.funcs = &vc4_mode_funcs;
253 	dev->mode_config.preferred_depth = 24;
254 	dev->mode_config.async_page_flip = true;
255 
256 	drm_mode_config_reset(dev);
257 
258 	if (dev->mode_config.num_connector) {
259 		vc4->fbdev = drm_fbdev_cma_init(dev, 32,
260 						dev->mode_config.num_connector);
261 		if (IS_ERR(vc4->fbdev))
262 			vc4->fbdev = NULL;
263 	}
264 
265 	drm_kms_helper_poll_init(dev);
266 
267 	return 0;
268 }
269