blob: c2c9f82b2df1e9dcd1691f1d6272b5349169b45c [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
Daniel Vetter833cd782016-06-02 00:06:28 +0200145 for_each_plane_in_state(state, plane, new_state, i) {
Eric Anholtb501bac2015-11-30 12:34:01 -0800146 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
Daniel Vetter5e84c262016-06-10 00:06:32 +0200161 drm_atomic_helper_swap_state(state, true);
Eric Anholtb501bac2015-11-30 12:34:01 -0800162
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
Chris Wilson08536952016-10-14 13:18:18 +0100179 drm_atomic_state_get(state);
Maarten Lankhorsteb639612016-04-26 16:11:44 +0200180 if (nonblock) {
Eric Anholtb501bac2015-11-30 12:34:01 -0800181 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
Eric Anholt83753112017-06-07 17:13:36 -0700191static 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
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800230static const struct drm_mode_config_funcs vc4_mode_funcs = {
Derek Foreman48666d52015-07-02 11:19:54 -0500231 .output_poll_changed = vc4_output_poll_changed,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800232 .atomic_check = drm_atomic_helper_check,
Eric Anholtb501bac2015-11-30 12:34:01 -0800233 .atomic_commit = vc4_atomic_commit,
Eric Anholt83753112017-06-07 17:13:36 -0700234 .fb_create = vc4_fb_create,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800235};
236
237int vc4_kms_load(struct drm_device *dev)
238{
Derek Foreman48666d52015-07-02 11:19:54 -0500239 struct vc4_dev *vc4 = to_vc4_dev(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800240 int ret;
241
Eric Anholtb501bac2015-11-30 12:34:01 -0800242 sema_init(&vc4->async_modeset, 1);
243
Mario Kleiner7d2818f2017-06-22 03:28:11 +0200244 /* Set support for vblank irq fast disable, before drm_vblank_init() */
245 dev->vblank_disable_immediate = true;
246
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800247 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
248 if (ret < 0) {
249 dev_err(dev->dev, "failed to initialize vblank\n");
250 return ret;
251 }
252
253 dev->mode_config.max_width = 2048;
254 dev->mode_config.max_height = 2048;
255 dev->mode_config.funcs = &vc4_mode_funcs;
256 dev->mode_config.preferred_depth = 24;
Eric Anholtb501bac2015-11-30 12:34:01 -0800257 dev->mode_config.async_page_flip = true;
258
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800259 drm_mode_config_reset(dev);
260
Eric Anholt1e70bdc2017-04-28 15:42:22 -0700261 if (dev->mode_config.num_connector) {
262 vc4->fbdev = drm_fbdev_cma_init(dev, 32,
263 dev->mode_config.num_connector);
264 if (IS_ERR(vc4->fbdev))
265 vc4->fbdev = NULL;
266 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800267
268 drm_kms_helper_poll_init(dev);
269
270 return 0;
271}