blob: 4ac894d993cd80472777b2a3bacc2fcf0dd52810 [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
17#include "drm_crtc.h"
Eric Anholtb501bac2015-11-30 12:34:01 -080018#include "drm_atomic.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080019#include "drm_atomic_helper.h"
20#include "drm_crtc_helper.h"
21#include "drm_plane_helper.h"
22#include "drm_fb_cma_helper.h"
23#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
45 drm_atomic_helper_commit_modeset_disables(dev, state);
46
47 drm_atomic_helper_commit_planes(dev, state, false);
48
49 drm_atomic_helper_commit_modeset_enables(dev, state);
50
Eric Anholt6674a902015-12-30 11:50:22 -080051 /* Make sure that drm_atomic_helper_wait_for_vblanks()
52 * actually waits for vblank. If we're doing a full atomic
53 * modeset (as opposed to a vc4_update_plane() short circuit),
54 * then we need to wait for scanout to be done with our
55 * display lists before we free it and potentially reallocate
56 * and overwrite the dlist memory with a new modeset.
57 */
58 state->legacy_cursor_update = false;
59
Eric Anholtb501bac2015-11-30 12:34:01 -080060 drm_atomic_helper_wait_for_vblanks(dev, state);
61
62 drm_atomic_helper_cleanup_planes(dev, state);
63
64 drm_atomic_state_free(state);
65
66 up(&vc4->async_modeset);
67
68 kfree(c);
69}
70
71static void
72vc4_atomic_complete_commit_seqno_cb(struct vc4_seqno_cb *cb)
73{
74 struct vc4_commit *c = container_of(cb, struct vc4_commit, cb);
75
76 vc4_atomic_complete_commit(c);
77}
78
79static struct vc4_commit *commit_init(struct drm_atomic_state *state)
80{
81 struct vc4_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
82
83 if (!c)
84 return NULL;
85 c->dev = state->dev;
86 c->state = state;
87
88 return c;
89}
90
91/**
92 * vc4_atomic_commit - commit validated state object
93 * @dev: DRM device
94 * @state: the driver state object
Maarten Lankhorsteb639612016-04-26 16:11:44 +020095 * @nonblock: nonblocking commit
Eric Anholtb501bac2015-11-30 12:34:01 -080096 *
97 * This function commits a with drm_atomic_helper_check() pre-validated state
98 * object. This can still fail when e.g. the framebuffer reservation fails. For
99 * now this doesn't implement asynchronous commits.
100 *
101 * RETURNS
102 * Zero for success or -errno.
103 */
104static int vc4_atomic_commit(struct drm_device *dev,
105 struct drm_atomic_state *state,
Maarten Lankhorsteb639612016-04-26 16:11:44 +0200106 bool nonblock)
Eric Anholtb501bac2015-11-30 12:34:01 -0800107{
108 struct vc4_dev *vc4 = to_vc4_dev(dev);
109 int ret;
110 int i;
111 uint64_t wait_seqno = 0;
112 struct vc4_commit *c;
Daniel Vetter833cd782016-06-02 00:06:28 +0200113 struct drm_plane *plane;
114 struct drm_plane_state *new_state;
Eric Anholtb501bac2015-11-30 12:34:01 -0800115
116 c = commit_init(state);
117 if (!c)
118 return -ENOMEM;
119
120 /* Make sure that any outstanding modesets have finished. */
Robert Fosse7c31f62016-05-03 13:48:20 -0400121 if (nonblock) {
122 ret = down_trylock(&vc4->async_modeset);
123 if (ret) {
124 kfree(c);
125 return -EBUSY;
126 }
127 } else {
128 ret = down_interruptible(&vc4->async_modeset);
129 if (ret) {
130 kfree(c);
131 return ret;
132 }
Eric Anholtb501bac2015-11-30 12:34:01 -0800133 }
134
135 ret = drm_atomic_helper_prepare_planes(dev, state);
136 if (ret) {
137 kfree(c);
138 up(&vc4->async_modeset);
139 return ret;
140 }
141
Daniel Vetter833cd782016-06-02 00:06:28 +0200142 for_each_plane_in_state(state, plane, new_state, i) {
Eric Anholtb501bac2015-11-30 12:34:01 -0800143 if ((plane->state->fb != new_state->fb) && new_state->fb) {
144 struct drm_gem_cma_object *cma_bo =
145 drm_fb_cma_get_gem_obj(new_state->fb, 0);
146 struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
147
148 wait_seqno = max(bo->seqno, wait_seqno);
149 }
150 }
151
152 /*
153 * This is the point of no return - everything below never fails except
154 * when the hw goes bonghits. Which means we can commit the new state on
155 * the software side now.
156 */
157
Daniel Vetter5e84c262016-06-10 00:06:32 +0200158 drm_atomic_helper_swap_state(state, true);
Eric Anholtb501bac2015-11-30 12:34:01 -0800159
160 /*
161 * Everything below can be run asynchronously without the need to grab
162 * any modeset locks at all under one condition: It must be guaranteed
163 * that the asynchronous work has either been cancelled (if the driver
164 * supports it, which at least requires that the framebuffers get
165 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
166 * before the new state gets committed on the software side with
167 * drm_atomic_helper_swap_state().
168 *
169 * This scheme allows new atomic state updates to be prepared and
170 * checked in parallel to the asynchronous completion of the previous
171 * update. Which is important since compositors need to figure out the
172 * composition of the next frame right after having submitted the
173 * current layout.
174 */
175
Maarten Lankhorsteb639612016-04-26 16:11:44 +0200176 if (nonblock) {
Eric Anholtb501bac2015-11-30 12:34:01 -0800177 vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
178 vc4_atomic_complete_commit_seqno_cb);
179 } else {
180 vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
181 vc4_atomic_complete_commit(c);
182 }
183
184 return 0;
185}
186
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800187static const struct drm_mode_config_funcs vc4_mode_funcs = {
Derek Foreman48666d52015-07-02 11:19:54 -0500188 .output_poll_changed = vc4_output_poll_changed,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800189 .atomic_check = drm_atomic_helper_check,
Eric Anholtb501bac2015-11-30 12:34:01 -0800190 .atomic_commit = vc4_atomic_commit,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800191 .fb_create = drm_fb_cma_create,
192};
193
194int vc4_kms_load(struct drm_device *dev)
195{
Derek Foreman48666d52015-07-02 11:19:54 -0500196 struct vc4_dev *vc4 = to_vc4_dev(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800197 int ret;
198
Eric Anholtb501bac2015-11-30 12:34:01 -0800199 sema_init(&vc4->async_modeset, 1);
200
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800201 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
202 if (ret < 0) {
203 dev_err(dev->dev, "failed to initialize vblank\n");
204 return ret;
205 }
206
207 dev->mode_config.max_width = 2048;
208 dev->mode_config.max_height = 2048;
209 dev->mode_config.funcs = &vc4_mode_funcs;
210 dev->mode_config.preferred_depth = 24;
Eric Anholtb501bac2015-11-30 12:34:01 -0800211 dev->mode_config.async_page_flip = true;
212
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800213 drm_mode_config_reset(dev);
214
Derek Foreman48666d52015-07-02 11:19:54 -0500215 vc4->fbdev = drm_fbdev_cma_init(dev, 32,
216 dev->mode_config.num_crtc,
217 dev->mode_config.num_connector);
218 if (IS_ERR(vc4->fbdev))
219 vc4->fbdev = NULL;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800220
221 drm_kms_helper_poll_init(dev);
222
223 return 0;
224}