blob: 86fcdb0bd75f3ca188bc103b1f342d194dfda7c9 [file] [log] [blame]
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001/*
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
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090017#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>
Eric Anholtc8b75bc2015-03-02 13:01:12 -080023#include "vc4_drv.h"
24
Derek Foreman48666d52015-07-02 11:19:54 -050025static void vc4_output_poll_changed(struct drm_device *dev)
26{
27 struct vc4_dev *vc4 = to_vc4_dev(dev);
28
Markus Elfring63fe9bb2016-07-15 21:15:37 +020029 drm_fbdev_cma_hotplug_event(vc4->fbdev);
Derek Foreman48666d52015-07-02 11:19:54 -050030}
31
Eric Anholtb501bac2015-11-30 12:34:01 -080032struct vc4_commit {
33 struct drm_device *dev;
34 struct drm_atomic_state *state;
35 struct vc4_seqno_cb cb;
36};
37
38static void
39vc4_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
Boris Brezillon34c8ea42017-06-02 10:32:08 +020045 drm_atomic_helper_wait_for_fences(dev, state, false);
46
47 drm_atomic_helper_wait_for_dependencies(state);
48
Eric Anholtb501bac2015-11-30 12:34:01 -080049 drm_atomic_helper_commit_modeset_disables(dev, state);
50
Liu Ying2b58e982016-08-29 17:12:03 +080051 drm_atomic_helper_commit_planes(dev, state, 0);
Eric Anholtb501bac2015-11-30 12:34:01 -080052
53 drm_atomic_helper_commit_modeset_enables(dev, state);
54
Eric Anholt6674a902015-12-30 11:50:22 -080055 /* 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
Boris Brezillon34c8ea42017-06-02 10:32:08 +020064 drm_atomic_helper_commit_hw_done(state);
65
Eric Anholtb501bac2015-11-30 12:34:01 -080066 drm_atomic_helper_wait_for_vblanks(dev, state);
67
68 drm_atomic_helper_cleanup_planes(dev, state);
69
Boris Brezillon34c8ea42017-06-02 10:32:08 +020070 drm_atomic_helper_commit_cleanup_done(state);
71
Chris Wilson08536952016-10-14 13:18:18 +010072 drm_atomic_state_put(state);
Eric Anholtb501bac2015-11-30 12:34:01 -080073
74 up(&vc4->async_modeset);
75
76 kfree(c);
77}
78
79static void
80vc4_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
87static 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
Maarten Lankhorsteb639612016-04-26 16:11:44 +0200103 * @nonblock: nonblocking commit
Eric Anholtb501bac2015-11-30 12:34:01 -0800104 *
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 */
112static int vc4_atomic_commit(struct drm_device *dev,
113 struct drm_atomic_state *state,
Maarten Lankhorsteb639612016-04-26 16:11:44 +0200114 bool nonblock)
Eric Anholtb501bac2015-11-30 12:34:01 -0800115{
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;
Daniel Vetter833cd782016-06-02 00:06:28 +0200121 struct drm_plane *plane;
122 struct drm_plane_state *new_state;
Eric Anholtb501bac2015-11-30 12:34:01 -0800123
124 c = commit_init(state);
125 if (!c)
126 return -ENOMEM;
127
Boris Brezillon34c8ea42017-06-02 10:32:08 +0200128 ret = drm_atomic_helper_setup_commit(state, nonblock);
129 if (ret)
130 return ret;
Derek Foreman26fc78f2016-11-24 12:11:55 -0600131
Derek Foreman26fc78f2016-11-24 12:11:55 -0600132 ret = down_interruptible(&vc4->async_modeset);
133 if (ret) {
134 kfree(c);
135 return ret;
Eric Anholtb501bac2015-11-30 12:34:01 -0800136 }
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
Eric Anholt53ad0692017-06-21 11:50:00 -0700145 if (!nonblock) {
146 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
147 if (ret) {
148 drm_atomic_helper_cleanup_planes(dev, state);
149 kfree(c);
150 up(&vc4->async_modeset);
151 return ret;
152 }
153 }
154
Daniel Vetter833cd782016-06-02 00:06:28 +0200155 for_each_plane_in_state(state, plane, new_state, i) {
Eric Anholtb501bac2015-11-30 12:34:01 -0800156 if ((plane->state->fb != new_state->fb) && new_state->fb) {
157 struct drm_gem_cma_object *cma_bo =
158 drm_fb_cma_get_gem_obj(new_state->fb, 0);
159 struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
160
161 wait_seqno = max(bo->seqno, wait_seqno);
162 }
163 }
164
165 /*
166 * This is the point of no return - everything below never fails except
167 * when the hw goes bonghits. Which means we can commit the new state on
168 * the software side now.
169 */
170
Daniel Vetter5e84c262016-06-10 00:06:32 +0200171 drm_atomic_helper_swap_state(state, true);
Eric Anholtb501bac2015-11-30 12:34:01 -0800172
173 /*
174 * Everything below can be run asynchronously without the need to grab
175 * any modeset locks at all under one condition: It must be guaranteed
176 * that the asynchronous work has either been cancelled (if the driver
177 * supports it, which at least requires that the framebuffers get
178 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
179 * before the new state gets committed on the software side with
180 * drm_atomic_helper_swap_state().
181 *
182 * This scheme allows new atomic state updates to be prepared and
183 * checked in parallel to the asynchronous completion of the previous
184 * update. Which is important since compositors need to figure out the
185 * composition of the next frame right after having submitted the
186 * current layout.
187 */
188
Chris Wilson08536952016-10-14 13:18:18 +0100189 drm_atomic_state_get(state);
Maarten Lankhorsteb639612016-04-26 16:11:44 +0200190 if (nonblock) {
Eric Anholtb501bac2015-11-30 12:34:01 -0800191 vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
192 vc4_atomic_complete_commit_seqno_cb);
193 } else {
194 vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
195 vc4_atomic_complete_commit(c);
196 }
197
198 return 0;
199}
200
Eric Anholt83753112017-06-07 17:13:36 -0700201static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
202 struct drm_file *file_priv,
203 const struct drm_mode_fb_cmd2 *mode_cmd)
204{
205 struct drm_mode_fb_cmd2 mode_cmd_local;
206
207 /* If the user didn't specify a modifier, use the
208 * vc4_set_tiling_ioctl() state for the BO.
209 */
210 if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
211 struct drm_gem_object *gem_obj;
212 struct vc4_bo *bo;
213
214 gem_obj = drm_gem_object_lookup(file_priv,
215 mode_cmd->handles[0]);
216 if (!gem_obj) {
217 DRM_ERROR("Failed to look up GEM BO %d\n",
218 mode_cmd->handles[0]);
219 return ERR_PTR(-ENOENT);
220 }
221 bo = to_vc4_bo(gem_obj);
222
223 mode_cmd_local = *mode_cmd;
224
225 if (bo->t_format) {
226 mode_cmd_local.modifier[0] =
227 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
228 } else {
229 mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
230 }
231
232 drm_gem_object_unreference_unlocked(gem_obj);
233
234 mode_cmd = &mode_cmd_local;
235 }
236
237 return drm_fb_cma_create(dev, file_priv, mode_cmd);
238}
239
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800240static const struct drm_mode_config_funcs vc4_mode_funcs = {
Derek Foreman48666d52015-07-02 11:19:54 -0500241 .output_poll_changed = vc4_output_poll_changed,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800242 .atomic_check = drm_atomic_helper_check,
Eric Anholtb501bac2015-11-30 12:34:01 -0800243 .atomic_commit = vc4_atomic_commit,
Eric Anholt83753112017-06-07 17:13:36 -0700244 .fb_create = vc4_fb_create,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800245};
246
247int vc4_kms_load(struct drm_device *dev)
248{
Derek Foreman48666d52015-07-02 11:19:54 -0500249 struct vc4_dev *vc4 = to_vc4_dev(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800250 int ret;
251
Eric Anholtb501bac2015-11-30 12:34:01 -0800252 sema_init(&vc4->async_modeset, 1);
253
Mario Kleiner7d2818f2017-06-22 03:28:11 +0200254 /* Set support for vblank irq fast disable, before drm_vblank_init() */
255 dev->vblank_disable_immediate = true;
256
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800257 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
258 if (ret < 0) {
259 dev_err(dev->dev, "failed to initialize vblank\n");
260 return ret;
261 }
262
263 dev->mode_config.max_width = 2048;
264 dev->mode_config.max_height = 2048;
265 dev->mode_config.funcs = &vc4_mode_funcs;
266 dev->mode_config.preferred_depth = 24;
Eric Anholtb501bac2015-11-30 12:34:01 -0800267 dev->mode_config.async_page_flip = true;
268
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800269 drm_mode_config_reset(dev);
270
Eric Anholt1e70bdc2017-04-28 15:42:22 -0700271 if (dev->mode_config.num_connector) {
272 vc4->fbdev = drm_fbdev_cma_init(dev, 32,
273 dev->mode_config.num_connector);
274 if (IS_ERR(vc4->fbdev))
275 vc4->fbdev = NULL;
276 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800277
278 drm_kms_helper_poll_init(dev);
279
280 return 0;
281}