2 * Copyright (C) 2015 Broadcom
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.
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).
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_gem_framebuffer_helper.h>
26 struct vc4_ctm_state {
27 struct drm_private_state base;
28 struct drm_color_ctm *ctm;
32 static struct vc4_ctm_state *to_vc4_ctm_state(struct drm_private_state *priv)
34 return container_of(priv, struct vc4_ctm_state, base);
37 static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state,
38 struct drm_private_obj *manager)
40 struct drm_device *dev = state->dev;
41 struct vc4_dev *vc4 = dev->dev_private;
42 struct drm_private_state *priv_state;
45 ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx);
49 priv_state = drm_atomic_get_private_obj_state(state, manager);
50 if (IS_ERR(priv_state))
51 return ERR_CAST(priv_state);
53 return to_vc4_ctm_state(priv_state);
56 static struct drm_private_state *
57 vc4_ctm_duplicate_state(struct drm_private_obj *obj)
59 struct vc4_ctm_state *state;
61 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
65 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
70 static void vc4_ctm_destroy_state(struct drm_private_obj *obj,
71 struct drm_private_state *state)
73 struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state);
78 static const struct drm_private_state_funcs vc4_ctm_state_funcs = {
79 .atomic_duplicate_state = vc4_ctm_duplicate_state,
80 .atomic_destroy_state = vc4_ctm_destroy_state,
83 /* Converts a DRM S31.32 value to the HW S0.9 format. */
84 static u16 vc4_ctm_s31_32_to_s0_9(u64 in)
89 r = in & BIT_ULL(63) ? BIT(9) : 0;
91 if ((in & GENMASK_ULL(62, 32)) > 0) {
92 /* We have zero integer bits so we can only saturate here. */
95 /* Otherwise take the 9 most important fractional bits. */
96 r |= (in >> 23) & GENMASK(8, 0);
103 vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state)
105 struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state);
106 struct drm_color_ctm *ctm = ctm_state->ctm;
108 if (ctm_state->fifo) {
109 HVS_WRITE(SCALER_OLEDCOEF2,
110 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]),
111 SCALER_OLEDCOEF2_R_TO_R) |
112 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]),
113 SCALER_OLEDCOEF2_R_TO_G) |
114 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]),
115 SCALER_OLEDCOEF2_R_TO_B));
116 HVS_WRITE(SCALER_OLEDCOEF1,
117 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]),
118 SCALER_OLEDCOEF1_G_TO_R) |
119 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]),
120 SCALER_OLEDCOEF1_G_TO_G) |
121 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]),
122 SCALER_OLEDCOEF1_G_TO_B));
123 HVS_WRITE(SCALER_OLEDCOEF0,
124 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]),
125 SCALER_OLEDCOEF0_B_TO_R) |
126 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]),
127 SCALER_OLEDCOEF0_B_TO_G) |
128 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]),
129 SCALER_OLEDCOEF0_B_TO_B));
132 HVS_WRITE(SCALER_OLEDOFFS,
133 VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO));
137 vc4_atomic_complete_commit(struct drm_atomic_state *state)
139 struct drm_device *dev = state->dev;
140 struct vc4_dev *vc4 = to_vc4_dev(dev);
142 drm_atomic_helper_wait_for_fences(dev, state, false);
144 drm_atomic_helper_wait_for_dependencies(state);
146 drm_atomic_helper_commit_modeset_disables(dev, state);
148 vc4_ctm_commit(vc4, state);
150 drm_atomic_helper_commit_planes(dev, state, 0);
152 drm_atomic_helper_commit_modeset_enables(dev, state);
154 drm_atomic_helper_fake_vblank(state);
156 drm_atomic_helper_commit_hw_done(state);
158 drm_atomic_helper_wait_for_flip_done(dev, state);
160 drm_atomic_helper_cleanup_planes(dev, state);
162 drm_atomic_helper_commit_cleanup_done(state);
164 drm_atomic_state_put(state);
166 up(&vc4->async_modeset);
169 static void commit_work(struct work_struct *work)
171 struct drm_atomic_state *state = container_of(work,
172 struct drm_atomic_state,
174 vc4_atomic_complete_commit(state);
178 * vc4_atomic_commit - commit validated state object
180 * @state: the driver state object
181 * @nonblock: nonblocking commit
183 * This function commits a with drm_atomic_helper_check() pre-validated state
184 * object. This can still fail when e.g. the framebuffer reservation fails. For
185 * now this doesn't implement asynchronous commits.
188 * Zero for success or -errno.
190 static int vc4_atomic_commit(struct drm_device *dev,
191 struct drm_atomic_state *state,
194 struct vc4_dev *vc4 = to_vc4_dev(dev);
197 if (state->async_update) {
198 ret = down_interruptible(&vc4->async_modeset);
202 ret = drm_atomic_helper_prepare_planes(dev, state);
204 up(&vc4->async_modeset);
208 drm_atomic_helper_async_commit(dev, state);
210 drm_atomic_helper_cleanup_planes(dev, state);
212 up(&vc4->async_modeset);
217 /* We know for sure we don't want an async update here. Set
218 * state->legacy_cursor_update to false to prevent
219 * drm_atomic_helper_setup_commit() from auto-completing
222 state->legacy_cursor_update = false;
223 ret = drm_atomic_helper_setup_commit(state, nonblock);
227 INIT_WORK(&state->commit_work, commit_work);
229 ret = down_interruptible(&vc4->async_modeset);
233 ret = drm_atomic_helper_prepare_planes(dev, state);
235 up(&vc4->async_modeset);
240 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
242 drm_atomic_helper_cleanup_planes(dev, state);
243 up(&vc4->async_modeset);
249 * This is the point of no return - everything below never fails except
250 * when the hw goes bonghits. Which means we can commit the new state on
251 * the software side now.
254 BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
257 * Everything below can be run asynchronously without the need to grab
258 * any modeset locks at all under one condition: It must be guaranteed
259 * that the asynchronous work has either been cancelled (if the driver
260 * supports it, which at least requires that the framebuffers get
261 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
262 * before the new state gets committed on the software side with
263 * drm_atomic_helper_swap_state().
265 * This scheme allows new atomic state updates to be prepared and
266 * checked in parallel to the asynchronous completion of the previous
267 * update. Which is important since compositors need to figure out the
268 * composition of the next frame right after having submitted the
272 drm_atomic_state_get(state);
274 queue_work(system_unbound_wq, &state->commit_work);
276 vc4_atomic_complete_commit(state);
281 static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
282 struct drm_file *file_priv,
283 const struct drm_mode_fb_cmd2 *mode_cmd)
285 struct drm_mode_fb_cmd2 mode_cmd_local;
287 /* If the user didn't specify a modifier, use the
288 * vc4_set_tiling_ioctl() state for the BO.
290 if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
291 struct drm_gem_object *gem_obj;
294 gem_obj = drm_gem_object_lookup(file_priv,
295 mode_cmd->handles[0]);
297 DRM_DEBUG("Failed to look up GEM BO %d\n",
298 mode_cmd->handles[0]);
299 return ERR_PTR(-ENOENT);
301 bo = to_vc4_bo(gem_obj);
303 mode_cmd_local = *mode_cmd;
306 mode_cmd_local.modifier[0] =
307 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
309 mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
312 drm_gem_object_put_unlocked(gem_obj);
314 mode_cmd = &mode_cmd_local;
317 return drm_gem_fb_create(dev, file_priv, mode_cmd);
320 /* Our CTM has some peculiar limitations: we can only enable it for one CRTC
321 * at a time and the HW only supports S0.9 scalars. To account for the latter,
322 * we don't allow userland to set a CTM that we have no hope of approximating.
325 vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
327 struct vc4_dev *vc4 = to_vc4_dev(dev);
328 struct vc4_ctm_state *ctm_state = NULL;
329 struct drm_crtc *crtc;
330 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
331 struct drm_color_ctm *ctm;
334 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
335 /* CTM is being disabled. */
336 if (!new_crtc_state->ctm && old_crtc_state->ctm) {
337 ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
338 if (IS_ERR(ctm_state))
339 return PTR_ERR(ctm_state);
344 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
345 if (new_crtc_state->ctm == old_crtc_state->ctm)
349 ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
350 if (IS_ERR(ctm_state))
351 return PTR_ERR(ctm_state);
354 /* CTM is being enabled or the matrix changed. */
355 if (new_crtc_state->ctm) {
356 /* fifo is 1-based since 0 disables CTM. */
357 int fifo = to_vc4_crtc(crtc)->channel + 1;
359 /* Check userland isn't trying to turn on CTM for more
360 * than one CRTC at a time.
362 if (ctm_state->fifo && ctm_state->fifo != fifo) {
363 DRM_DEBUG_DRIVER("Too many CTM configured\n");
367 /* Check we can approximate the specified CTM.
368 * We disallow scalars |c| > 1.0 since the HW has
371 ctm = new_crtc_state->ctm->data;
372 for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) {
373 u64 val = ctm->matrix[i];
376 if (val > BIT_ULL(32))
380 ctm_state->fifo = fifo;
381 ctm_state->ctm = ctm;
389 vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
393 ret = vc4_ctm_atomic_check(dev, state);
397 return drm_atomic_helper_check(dev, state);
400 static const struct drm_mode_config_funcs vc4_mode_funcs = {
401 .atomic_check = vc4_atomic_check,
402 .atomic_commit = vc4_atomic_commit,
403 .fb_create = vc4_fb_create,
406 int vc4_kms_load(struct drm_device *dev)
408 struct vc4_dev *vc4 = to_vc4_dev(dev);
409 struct vc4_ctm_state *ctm_state;
412 sema_init(&vc4->async_modeset, 1);
414 /* Set support for vblank irq fast disable, before drm_vblank_init() */
415 dev->vblank_disable_immediate = true;
417 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
419 dev_err(dev->dev, "failed to initialize vblank\n");
423 dev->mode_config.max_width = 2048;
424 dev->mode_config.max_height = 2048;
425 dev->mode_config.funcs = &vc4_mode_funcs;
426 dev->mode_config.preferred_depth = 24;
427 dev->mode_config.async_page_flip = true;
428 dev->mode_config.allow_fb_modifiers = true;
430 drm_modeset_lock_init(&vc4->ctm_state_lock);
432 ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL);
435 drm_atomic_private_obj_init(&vc4->ctm_manager, &ctm_state->base,
436 &vc4_ctm_state_funcs);
438 drm_mode_config_reset(dev);
440 drm_kms_helper_poll_init(dev);