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_fb_helper.h>
23 #include <drm/drm_fb_cma_helper.h>
24 #include <drm/drm_gem_framebuffer_helper.h>
28 struct vc4_ctm_state {
29 struct drm_private_state base;
30 struct drm_color_ctm *ctm;
34 static struct vc4_ctm_state *to_vc4_ctm_state(struct drm_private_state *priv)
36 return container_of(priv, struct vc4_ctm_state, base);
39 static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state,
40 struct drm_private_obj *manager)
42 struct drm_device *dev = state->dev;
43 struct vc4_dev *vc4 = dev->dev_private;
44 struct drm_private_state *priv_state;
47 ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx);
51 priv_state = drm_atomic_get_private_obj_state(state, manager);
52 if (IS_ERR(priv_state))
53 return ERR_CAST(priv_state);
55 return to_vc4_ctm_state(priv_state);
58 static struct drm_private_state *
59 vc4_ctm_duplicate_state(struct drm_private_obj *obj)
61 struct vc4_ctm_state *state;
63 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
67 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
72 static void vc4_ctm_destroy_state(struct drm_private_obj *obj,
73 struct drm_private_state *state)
75 struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state);
80 static const struct drm_private_state_funcs vc4_ctm_state_funcs = {
81 .atomic_duplicate_state = vc4_ctm_duplicate_state,
82 .atomic_destroy_state = vc4_ctm_destroy_state,
85 /* Converts a DRM S31.32 value to the HW S0.9 format. */
86 static u16 vc4_ctm_s31_32_to_s0_9(u64 in)
91 r = in & BIT_ULL(63) ? BIT(9) : 0;
93 if ((in & GENMASK_ULL(62, 32)) > 0) {
94 /* We have zero integer bits so we can only saturate here. */
97 /* Otherwise take the 9 most important fractional bits. */
98 r |= (in >> 23) & GENMASK(8, 0);
105 vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state)
107 struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state);
108 struct drm_color_ctm *ctm = ctm_state->ctm;
110 if (ctm_state->fifo) {
111 HVS_WRITE(SCALER_OLEDCOEF2,
112 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]),
113 SCALER_OLEDCOEF2_R_TO_R) |
114 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]),
115 SCALER_OLEDCOEF2_R_TO_G) |
116 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]),
117 SCALER_OLEDCOEF2_R_TO_B));
118 HVS_WRITE(SCALER_OLEDCOEF1,
119 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]),
120 SCALER_OLEDCOEF1_G_TO_R) |
121 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]),
122 SCALER_OLEDCOEF1_G_TO_G) |
123 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]),
124 SCALER_OLEDCOEF1_G_TO_B));
125 HVS_WRITE(SCALER_OLEDCOEF0,
126 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]),
127 SCALER_OLEDCOEF0_B_TO_R) |
128 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]),
129 SCALER_OLEDCOEF0_B_TO_G) |
130 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]),
131 SCALER_OLEDCOEF0_B_TO_B));
134 HVS_WRITE(SCALER_OLEDOFFS,
135 VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO));
139 vc4_atomic_complete_commit(struct drm_atomic_state *state)
141 struct drm_device *dev = state->dev;
142 struct vc4_dev *vc4 = to_vc4_dev(dev);
144 drm_atomic_helper_wait_for_fences(dev, state, false);
146 drm_atomic_helper_wait_for_dependencies(state);
148 drm_atomic_helper_commit_modeset_disables(dev, state);
150 vc4_ctm_commit(vc4, state);
152 drm_atomic_helper_commit_planes(dev, state, 0);
154 drm_atomic_helper_commit_modeset_enables(dev, state);
156 drm_atomic_helper_fake_vblank(state);
158 drm_atomic_helper_commit_hw_done(state);
160 drm_atomic_helper_wait_for_flip_done(dev, state);
162 drm_atomic_helper_cleanup_planes(dev, state);
164 drm_atomic_helper_commit_cleanup_done(state);
166 drm_atomic_state_put(state);
168 up(&vc4->async_modeset);
171 static void commit_work(struct work_struct *work)
173 struct drm_atomic_state *state = container_of(work,
174 struct drm_atomic_state,
176 vc4_atomic_complete_commit(state);
180 * vc4_atomic_commit - commit validated state object
182 * @state: the driver state object
183 * @nonblock: nonblocking commit
185 * This function commits a with drm_atomic_helper_check() pre-validated state
186 * object. This can still fail when e.g. the framebuffer reservation fails. For
187 * now this doesn't implement asynchronous commits.
190 * Zero for success or -errno.
192 static int vc4_atomic_commit(struct drm_device *dev,
193 struct drm_atomic_state *state,
196 struct vc4_dev *vc4 = to_vc4_dev(dev);
199 if (state->async_update) {
200 ret = down_interruptible(&vc4->async_modeset);
204 ret = drm_atomic_helper_prepare_planes(dev, state);
206 up(&vc4->async_modeset);
210 drm_atomic_helper_async_commit(dev, state);
212 drm_atomic_helper_cleanup_planes(dev, state);
214 up(&vc4->async_modeset);
219 ret = drm_atomic_helper_setup_commit(state, nonblock);
223 INIT_WORK(&state->commit_work, commit_work);
225 ret = down_interruptible(&vc4->async_modeset);
229 ret = drm_atomic_helper_prepare_planes(dev, state);
231 up(&vc4->async_modeset);
236 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
238 drm_atomic_helper_cleanup_planes(dev, state);
239 up(&vc4->async_modeset);
245 * This is the point of no return - everything below never fails except
246 * when the hw goes bonghits. Which means we can commit the new state on
247 * the software side now.
250 BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
253 * Everything below can be run asynchronously without the need to grab
254 * any modeset locks at all under one condition: It must be guaranteed
255 * that the asynchronous work has either been cancelled (if the driver
256 * supports it, which at least requires that the framebuffers get
257 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
258 * before the new state gets committed on the software side with
259 * drm_atomic_helper_swap_state().
261 * This scheme allows new atomic state updates to be prepared and
262 * checked in parallel to the asynchronous completion of the previous
263 * update. Which is important since compositors need to figure out the
264 * composition of the next frame right after having submitted the
268 drm_atomic_state_get(state);
270 queue_work(system_unbound_wq, &state->commit_work);
272 vc4_atomic_complete_commit(state);
277 static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
278 struct drm_file *file_priv,
279 const struct drm_mode_fb_cmd2 *mode_cmd)
281 struct drm_mode_fb_cmd2 mode_cmd_local;
283 /* If the user didn't specify a modifier, use the
284 * vc4_set_tiling_ioctl() state for the BO.
286 if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
287 struct drm_gem_object *gem_obj;
290 gem_obj = drm_gem_object_lookup(file_priv,
291 mode_cmd->handles[0]);
293 DRM_DEBUG("Failed to look up GEM BO %d\n",
294 mode_cmd->handles[0]);
295 return ERR_PTR(-ENOENT);
297 bo = to_vc4_bo(gem_obj);
299 mode_cmd_local = *mode_cmd;
302 mode_cmd_local.modifier[0] =
303 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
305 mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
308 drm_gem_object_put_unlocked(gem_obj);
310 mode_cmd = &mode_cmd_local;
313 return drm_gem_fb_create(dev, file_priv, mode_cmd);
316 /* Our CTM has some peculiar limitations: we can only enable it for one CRTC
317 * at a time and the HW only supports S0.9 scalars. To account for the latter,
318 * we don't allow userland to set a CTM that we have no hope of approximating.
321 vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
323 struct vc4_dev *vc4 = to_vc4_dev(dev);
324 struct vc4_ctm_state *ctm_state = NULL;
325 struct drm_crtc *crtc;
326 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
327 struct drm_color_ctm *ctm;
330 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
331 /* CTM is being disabled. */
332 if (!new_crtc_state->ctm && old_crtc_state->ctm) {
333 ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
334 if (IS_ERR(ctm_state))
335 return PTR_ERR(ctm_state);
340 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
341 if (new_crtc_state->ctm == old_crtc_state->ctm)
345 ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
346 if (IS_ERR(ctm_state))
347 return PTR_ERR(ctm_state);
350 /* CTM is being enabled or the matrix changed. */
351 if (new_crtc_state->ctm) {
352 /* fifo is 1-based since 0 disables CTM. */
353 int fifo = to_vc4_crtc(crtc)->channel + 1;
355 /* Check userland isn't trying to turn on CTM for more
356 * than one CRTC at a time.
358 if (ctm_state->fifo && ctm_state->fifo != fifo) {
359 DRM_DEBUG_DRIVER("Too many CTM configured\n");
363 /* Check we can approximate the specified CTM.
364 * We disallow scalars |c| > 1.0 since the HW has
367 ctm = new_crtc_state->ctm->data;
368 for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) {
369 u64 val = ctm->matrix[i];
372 if (val > BIT_ULL(32))
376 ctm_state->fifo = fifo;
377 ctm_state->ctm = ctm;
385 vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
389 ret = vc4_ctm_atomic_check(dev, state);
393 return drm_atomic_helper_check(dev, state);
396 static const struct drm_mode_config_funcs vc4_mode_funcs = {
397 .output_poll_changed = drm_fb_helper_output_poll_changed,
398 .atomic_check = vc4_atomic_check,
399 .atomic_commit = vc4_atomic_commit,
400 .fb_create = vc4_fb_create,
403 int vc4_kms_load(struct drm_device *dev)
405 struct vc4_dev *vc4 = to_vc4_dev(dev);
406 struct vc4_ctm_state *ctm_state;
409 sema_init(&vc4->async_modeset, 1);
411 /* Set support for vblank irq fast disable, before drm_vblank_init() */
412 dev->vblank_disable_immediate = true;
414 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
416 dev_err(dev->dev, "failed to initialize vblank\n");
420 dev->mode_config.max_width = 2048;
421 dev->mode_config.max_height = 2048;
422 dev->mode_config.funcs = &vc4_mode_funcs;
423 dev->mode_config.preferred_depth = 24;
424 dev->mode_config.async_page_flip = true;
425 dev->mode_config.allow_fb_modifiers = true;
427 drm_modeset_lock_init(&vc4->ctm_state_lock);
429 ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL);
432 drm_atomic_private_obj_init(&vc4->ctm_manager, &ctm_state->base,
433 &vc4_ctm_state_funcs);
435 drm_mode_config_reset(dev);
437 if (dev->mode_config.num_connector)
438 drm_fb_cma_fbdev_init(dev, 32, 0);
440 drm_kms_helper_poll_init(dev);