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).
18 #include "drm_atomic.h"
19 #include "drm_atomic_helper.h"
20 #include "drm_crtc_helper.h"
21 #include "drm_plane_helper.h"
22 #include "drm_fb_cma_helper.h"
25 static void vc4_output_poll_changed(struct drm_device *dev)
27 struct vc4_dev *vc4 = to_vc4_dev(dev);
30 drm_fbdev_cma_hotplug_event(vc4->fbdev);
34 struct drm_device *dev;
35 struct drm_atomic_state *state;
36 struct vc4_seqno_cb cb;
40 vc4_atomic_complete_commit(struct vc4_commit *c)
42 struct drm_atomic_state *state = c->state;
43 struct drm_device *dev = state->dev;
44 struct vc4_dev *vc4 = to_vc4_dev(dev);
46 drm_atomic_helper_commit_modeset_disables(dev, state);
48 drm_atomic_helper_commit_planes(dev, state, false);
50 drm_atomic_helper_commit_modeset_enables(dev, state);
52 drm_atomic_helper_wait_for_vblanks(dev, state);
54 drm_atomic_helper_cleanup_planes(dev, state);
56 drm_atomic_state_free(state);
58 up(&vc4->async_modeset);
64 vc4_atomic_complete_commit_seqno_cb(struct vc4_seqno_cb *cb)
66 struct vc4_commit *c = container_of(cb, struct vc4_commit, cb);
68 vc4_atomic_complete_commit(c);
71 static struct vc4_commit *commit_init(struct drm_atomic_state *state)
73 struct vc4_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
84 * vc4_atomic_commit - commit validated state object
86 * @state: the driver state object
87 * @async: asynchronous commit
89 * This function commits a with drm_atomic_helper_check() pre-validated state
90 * object. This can still fail when e.g. the framebuffer reservation fails. For
91 * now this doesn't implement asynchronous commits.
94 * Zero for success or -errno.
96 static int vc4_atomic_commit(struct drm_device *dev,
97 struct drm_atomic_state *state,
100 struct vc4_dev *vc4 = to_vc4_dev(dev);
103 uint64_t wait_seqno = 0;
104 struct vc4_commit *c;
106 c = commit_init(state);
110 /* Make sure that any outstanding modesets have finished. */
111 ret = down_interruptible(&vc4->async_modeset);
117 ret = drm_atomic_helper_prepare_planes(dev, state);
120 up(&vc4->async_modeset);
124 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
125 struct drm_plane *plane = state->planes[i];
126 struct drm_plane_state *new_state = state->plane_states[i];
131 if ((plane->state->fb != new_state->fb) && new_state->fb) {
132 struct drm_gem_cma_object *cma_bo =
133 drm_fb_cma_get_gem_obj(new_state->fb, 0);
134 struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
136 wait_seqno = max(bo->seqno, wait_seqno);
141 * This is the point of no return - everything below never fails except
142 * when the hw goes bonghits. Which means we can commit the new state on
143 * the software side now.
146 drm_atomic_helper_swap_state(dev, state);
149 * Everything below can be run asynchronously without the need to grab
150 * any modeset locks at all under one condition: It must be guaranteed
151 * that the asynchronous work has either been cancelled (if the driver
152 * supports it, which at least requires that the framebuffers get
153 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
154 * before the new state gets committed on the software side with
155 * drm_atomic_helper_swap_state().
157 * This scheme allows new atomic state updates to be prepared and
158 * checked in parallel to the asynchronous completion of the previous
159 * update. Which is important since compositors need to figure out the
160 * composition of the next frame right after having submitted the
165 vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
166 vc4_atomic_complete_commit_seqno_cb);
168 vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
169 vc4_atomic_complete_commit(c);
175 static const struct drm_mode_config_funcs vc4_mode_funcs = {
176 .output_poll_changed = vc4_output_poll_changed,
177 .atomic_check = drm_atomic_helper_check,
178 .atomic_commit = vc4_atomic_commit,
179 .fb_create = drm_fb_cma_create,
182 int vc4_kms_load(struct drm_device *dev)
184 struct vc4_dev *vc4 = to_vc4_dev(dev);
187 sema_init(&vc4->async_modeset, 1);
189 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
191 dev_err(dev->dev, "failed to initialize vblank\n");
195 dev->mode_config.max_width = 2048;
196 dev->mode_config.max_height = 2048;
197 dev->mode_config.funcs = &vc4_mode_funcs;
198 dev->mode_config.preferred_depth = 24;
199 dev->mode_config.async_page_flip = true;
201 dev->vblank_disable_allowed = true;
203 drm_mode_config_reset(dev);
205 vc4->fbdev = drm_fbdev_cma_init(dev, 32,
206 dev->mode_config.num_crtc,
207 dev->mode_config.num_connector);
208 if (IS_ERR(vc4->fbdev))
211 drm_kms_helper_poll_init(dev);