blob: 8b31f177d46256ef996f25037679cccccae4bdeb [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 -080032static void
Eric Anholtcf1b3722017-06-21 11:50:01 -070033vc4_atomic_complete_commit(struct drm_atomic_state *state)
Eric Anholtb501bac2015-11-30 12:34:01 -080034{
Eric Anholtb501bac2015-11-30 12:34:01 -080035 struct drm_device *dev = state->dev;
36 struct vc4_dev *vc4 = to_vc4_dev(dev);
37
Boris Brezillon34c8ea42017-06-02 10:32:08 +020038 drm_atomic_helper_wait_for_fences(dev, state, false);
39
40 drm_atomic_helper_wait_for_dependencies(state);
41
Eric Anholtb501bac2015-11-30 12:34:01 -080042 drm_atomic_helper_commit_modeset_disables(dev, state);
43
Liu Ying2b58e982016-08-29 17:12:03 +080044 drm_atomic_helper_commit_planes(dev, state, 0);
Eric Anholtb501bac2015-11-30 12:34:01 -080045
46 drm_atomic_helper_commit_modeset_enables(dev, state);
47
Eric Anholt6674a9042015-12-30 11:50:22 -080048 /* Make sure that drm_atomic_helper_wait_for_vblanks()
49 * actually waits for vblank. If we're doing a full atomic
50 * modeset (as opposed to a vc4_update_plane() short circuit),
51 * then we need to wait for scanout to be done with our
52 * display lists before we free it and potentially reallocate
53 * and overwrite the dlist memory with a new modeset.
54 */
55 state->legacy_cursor_update = false;
56
Boris Brezillon34c8ea42017-06-02 10:32:08 +020057 drm_atomic_helper_commit_hw_done(state);
58
Eric Anholtb501bac2015-11-30 12:34:01 -080059 drm_atomic_helper_wait_for_vblanks(dev, state);
60
61 drm_atomic_helper_cleanup_planes(dev, state);
62
Boris Brezillon34c8ea42017-06-02 10:32:08 +020063 drm_atomic_helper_commit_cleanup_done(state);
64
Chris Wilson08536952016-10-14 13:18:18 +010065 drm_atomic_state_put(state);
Eric Anholtb501bac2015-11-30 12:34:01 -080066
67 up(&vc4->async_modeset);
Eric Anholtb501bac2015-11-30 12:34:01 -080068}
69
Eric Anholtcf1b3722017-06-21 11:50:01 -070070static void commit_work(struct work_struct *work)
Eric Anholtb501bac2015-11-30 12:34:01 -080071{
Eric Anholtcf1b3722017-06-21 11:50:01 -070072 struct drm_atomic_state *state = container_of(work,
73 struct drm_atomic_state,
74 commit_work);
75 vc4_atomic_complete_commit(state);
Eric Anholtb501bac2015-11-30 12:34:01 -080076}
77
78/**
79 * vc4_atomic_commit - commit validated state object
80 * @dev: DRM device
81 * @state: the driver state object
Maarten Lankhorsteb639612016-04-26 16:11:44 +020082 * @nonblock: nonblocking commit
Eric Anholtb501bac2015-11-30 12:34:01 -080083 *
84 * This function commits a with drm_atomic_helper_check() pre-validated state
85 * object. This can still fail when e.g. the framebuffer reservation fails. For
86 * now this doesn't implement asynchronous commits.
87 *
88 * RETURNS
89 * Zero for success or -errno.
90 */
91static int vc4_atomic_commit(struct drm_device *dev,
92 struct drm_atomic_state *state,
Maarten Lankhorsteb639612016-04-26 16:11:44 +020093 bool nonblock)
Eric Anholtb501bac2015-11-30 12:34:01 -080094{
95 struct vc4_dev *vc4 = to_vc4_dev(dev);
96 int ret;
Eric Anholtb501bac2015-11-30 12:34:01 -080097
Boris Brezillon34c8ea42017-06-02 10:32:08 +020098 ret = drm_atomic_helper_setup_commit(state, nonblock);
99 if (ret)
100 return ret;
Derek Foreman26fc78f2016-11-24 12:11:55 -0600101
Eric Anholtcf1b3722017-06-21 11:50:01 -0700102 INIT_WORK(&state->commit_work, commit_work);
103
Derek Foreman26fc78f2016-11-24 12:11:55 -0600104 ret = down_interruptible(&vc4->async_modeset);
Eric Anholtcf1b3722017-06-21 11:50:01 -0700105 if (ret)
Derek Foreman26fc78f2016-11-24 12:11:55 -0600106 return ret;
Eric Anholtb501bac2015-11-30 12:34:01 -0800107
108 ret = drm_atomic_helper_prepare_planes(dev, state);
109 if (ret) {
Eric Anholtb501bac2015-11-30 12:34:01 -0800110 up(&vc4->async_modeset);
111 return ret;
112 }
113
Eric Anholt53ad0692017-06-21 11:50:00 -0700114 if (!nonblock) {
115 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
116 if (ret) {
117 drm_atomic_helper_cleanup_planes(dev, state);
Eric Anholt53ad0692017-06-21 11:50:00 -0700118 up(&vc4->async_modeset);
119 return ret;
120 }
121 }
122
Eric Anholtb501bac2015-11-30 12:34:01 -0800123 /*
124 * This is the point of no return - everything below never fails except
125 * when the hw goes bonghits. Which means we can commit the new state on
126 * the software side now.
127 */
128
Maarten Lankhorstd68bc0e2017-07-11 16:33:12 +0200129 BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
Eric Anholtb501bac2015-11-30 12:34:01 -0800130
131 /*
132 * Everything below can be run asynchronously without the need to grab
133 * any modeset locks at all under one condition: It must be guaranteed
134 * that the asynchronous work has either been cancelled (if the driver
135 * supports it, which at least requires that the framebuffers get
136 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
137 * before the new state gets committed on the software side with
138 * drm_atomic_helper_swap_state().
139 *
140 * This scheme allows new atomic state updates to be prepared and
141 * checked in parallel to the asynchronous completion of the previous
142 * update. Which is important since compositors need to figure out the
143 * composition of the next frame right after having submitted the
144 * current layout.
145 */
146
Chris Wilson08536952016-10-14 13:18:18 +0100147 drm_atomic_state_get(state);
Eric Anholtcf1b3722017-06-21 11:50:01 -0700148 if (nonblock)
149 queue_work(system_unbound_wq, &state->commit_work);
150 else
151 vc4_atomic_complete_commit(state);
Eric Anholtb501bac2015-11-30 12:34:01 -0800152
153 return 0;
154}
155
Eric Anholt83753112017-06-07 17:13:36 -0700156static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
157 struct drm_file *file_priv,
158 const struct drm_mode_fb_cmd2 *mode_cmd)
159{
160 struct drm_mode_fb_cmd2 mode_cmd_local;
161
162 /* If the user didn't specify a modifier, use the
163 * vc4_set_tiling_ioctl() state for the BO.
164 */
165 if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
166 struct drm_gem_object *gem_obj;
167 struct vc4_bo *bo;
168
169 gem_obj = drm_gem_object_lookup(file_priv,
170 mode_cmd->handles[0]);
171 if (!gem_obj) {
172 DRM_ERROR("Failed to look up GEM BO %d\n",
173 mode_cmd->handles[0]);
174 return ERR_PTR(-ENOENT);
175 }
176 bo = to_vc4_bo(gem_obj);
177
178 mode_cmd_local = *mode_cmd;
179
180 if (bo->t_format) {
181 mode_cmd_local.modifier[0] =
182 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
183 } else {
184 mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
185 }
186
Cihangir Akturk1d5494e2017-08-03 14:58:40 +0300187 drm_gem_object_put_unlocked(gem_obj);
Eric Anholt83753112017-06-07 17:13:36 -0700188
189 mode_cmd = &mode_cmd_local;
190 }
191
192 return drm_fb_cma_create(dev, file_priv, mode_cmd);
193}
194
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800195static const struct drm_mode_config_funcs vc4_mode_funcs = {
Derek Foreman48666d52015-07-02 11:19:54 -0500196 .output_poll_changed = vc4_output_poll_changed,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800197 .atomic_check = drm_atomic_helper_check,
Eric Anholtb501bac2015-11-30 12:34:01 -0800198 .atomic_commit = vc4_atomic_commit,
Eric Anholt83753112017-06-07 17:13:36 -0700199 .fb_create = vc4_fb_create,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800200};
201
202int vc4_kms_load(struct drm_device *dev)
203{
Derek Foreman48666d52015-07-02 11:19:54 -0500204 struct vc4_dev *vc4 = to_vc4_dev(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800205 int ret;
206
Eric Anholtb501bac2015-11-30 12:34:01 -0800207 sema_init(&vc4->async_modeset, 1);
208
Mario Kleiner7d2818f2017-06-22 03:28:11 +0200209 /* Set support for vblank irq fast disable, before drm_vblank_init() */
210 dev->vblank_disable_immediate = true;
211
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800212 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
213 if (ret < 0) {
214 dev_err(dev->dev, "failed to initialize vblank\n");
215 return ret;
216 }
217
218 dev->mode_config.max_width = 2048;
219 dev->mode_config.max_height = 2048;
220 dev->mode_config.funcs = &vc4_mode_funcs;
221 dev->mode_config.preferred_depth = 24;
Eric Anholtb501bac2015-11-30 12:34:01 -0800222 dev->mode_config.async_page_flip = true;
223
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800224 drm_mode_config_reset(dev);
225
Eric Anholt1e70bdc2017-04-28 15:42:22 -0700226 if (dev->mode_config.num_connector) {
227 vc4->fbdev = drm_fbdev_cma_init(dev, 32,
228 dev->mode_config.num_connector);
229 if (IS_ERR(vc4->fbdev))
230 vc4->fbdev = NULL;
231 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800232
233 drm_kms_helper_poll_init(dev);
234
235 return 0;
236}