blob: 8f4d5ffc32bef716e99283b748fd52daf454a3f2 [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
29 if (vc4->fbdev)
30 drm_fbdev_cma_hotplug_event(vc4->fbdev);
31}
32
Eric Anholtb501bac2015-11-30 12:34:01 -080033struct vc4_commit {
34 struct drm_device *dev;
35 struct drm_atomic_state *state;
36 struct vc4_seqno_cb cb;
37};
38
39static void
40vc4_atomic_complete_commit(struct vc4_commit *c)
41{
42 struct drm_atomic_state *state = c->state;
43 struct drm_device *dev = state->dev;
44 struct vc4_dev *vc4 = to_vc4_dev(dev);
45
46 drm_atomic_helper_commit_modeset_disables(dev, state);
47
48 drm_atomic_helper_commit_planes(dev, state, false);
49
50 drm_atomic_helper_commit_modeset_enables(dev, state);
51
Eric Anholt6674a902015-12-30 11:50:22 -080052 /* Make sure that drm_atomic_helper_wait_for_vblanks()
53 * actually waits for vblank. If we're doing a full atomic
54 * modeset (as opposed to a vc4_update_plane() short circuit),
55 * then we need to wait for scanout to be done with our
56 * display lists before we free it and potentially reallocate
57 * and overwrite the dlist memory with a new modeset.
58 */
59 state->legacy_cursor_update = false;
60
Eric Anholtb501bac2015-11-30 12:34:01 -080061 drm_atomic_helper_wait_for_vblanks(dev, state);
62
63 drm_atomic_helper_cleanup_planes(dev, state);
64
65 drm_atomic_state_free(state);
66
67 up(&vc4->async_modeset);
68
69 kfree(c);
70}
71
72static void
73vc4_atomic_complete_commit_seqno_cb(struct vc4_seqno_cb *cb)
74{
75 struct vc4_commit *c = container_of(cb, struct vc4_commit, cb);
76
77 vc4_atomic_complete_commit(c);
78}
79
80static struct vc4_commit *commit_init(struct drm_atomic_state *state)
81{
82 struct vc4_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
83
84 if (!c)
85 return NULL;
86 c->dev = state->dev;
87 c->state = state;
88
89 return c;
90}
91
92/**
93 * vc4_atomic_commit - commit validated state object
94 * @dev: DRM device
95 * @state: the driver state object
Maarten Lankhorsteb639612016-04-26 16:11:44 +020096 * @nonblock: nonblocking commit
Eric Anholtb501bac2015-11-30 12:34:01 -080097 *
98 * This function commits a with drm_atomic_helper_check() pre-validated state
99 * object. This can still fail when e.g. the framebuffer reservation fails. For
100 * now this doesn't implement asynchronous commits.
101 *
102 * RETURNS
103 * Zero for success or -errno.
104 */
105static int vc4_atomic_commit(struct drm_device *dev,
106 struct drm_atomic_state *state,
Maarten Lankhorsteb639612016-04-26 16:11:44 +0200107 bool nonblock)
Eric Anholtb501bac2015-11-30 12:34:01 -0800108{
109 struct vc4_dev *vc4 = to_vc4_dev(dev);
110 int ret;
111 int i;
112 uint64_t wait_seqno = 0;
113 struct vc4_commit *c;
Daniel Vetter833cd782016-06-02 00:06:28 +0200114 struct drm_plane *plane;
115 struct drm_plane_state *new_state;
Eric Anholtb501bac2015-11-30 12:34:01 -0800116
117 c = commit_init(state);
118 if (!c)
119 return -ENOMEM;
120
121 /* Make sure that any outstanding modesets have finished. */
122 ret = down_interruptible(&vc4->async_modeset);
123 if (ret) {
124 kfree(c);
125 return ret;
126 }
127
128 ret = drm_atomic_helper_prepare_planes(dev, state);
129 if (ret) {
130 kfree(c);
131 up(&vc4->async_modeset);
132 return ret;
133 }
134
Daniel Vetter833cd782016-06-02 00:06:28 +0200135 for_each_plane_in_state(state, plane, new_state, i) {
Eric Anholtb501bac2015-11-30 12:34:01 -0800136 if ((plane->state->fb != new_state->fb) && new_state->fb) {
137 struct drm_gem_cma_object *cma_bo =
138 drm_fb_cma_get_gem_obj(new_state->fb, 0);
139 struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
140
141 wait_seqno = max(bo->seqno, wait_seqno);
142 }
143 }
144
145 /*
146 * This is the point of no return - everything below never fails except
147 * when the hw goes bonghits. Which means we can commit the new state on
148 * the software side now.
149 */
150
Daniel Vetter5e84c262016-06-10 00:06:32 +0200151 drm_atomic_helper_swap_state(state, true);
Eric Anholtb501bac2015-11-30 12:34:01 -0800152
153 /*
154 * Everything below can be run asynchronously without the need to grab
155 * any modeset locks at all under one condition: It must be guaranteed
156 * that the asynchronous work has either been cancelled (if the driver
157 * supports it, which at least requires that the framebuffers get
158 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
159 * before the new state gets committed on the software side with
160 * drm_atomic_helper_swap_state().
161 *
162 * This scheme allows new atomic state updates to be prepared and
163 * checked in parallel to the asynchronous completion of the previous
164 * update. Which is important since compositors need to figure out the
165 * composition of the next frame right after having submitted the
166 * current layout.
167 */
168
Maarten Lankhorsteb639612016-04-26 16:11:44 +0200169 if (nonblock) {
Eric Anholtb501bac2015-11-30 12:34:01 -0800170 vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
171 vc4_atomic_complete_commit_seqno_cb);
172 } else {
173 vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
174 vc4_atomic_complete_commit(c);
175 }
176
177 return 0;
178}
179
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800180static const struct drm_mode_config_funcs vc4_mode_funcs = {
Derek Foreman48666d52015-07-02 11:19:54 -0500181 .output_poll_changed = vc4_output_poll_changed,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800182 .atomic_check = drm_atomic_helper_check,
Eric Anholtb501bac2015-11-30 12:34:01 -0800183 .atomic_commit = vc4_atomic_commit,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800184 .fb_create = drm_fb_cma_create,
185};
186
187int vc4_kms_load(struct drm_device *dev)
188{
Derek Foreman48666d52015-07-02 11:19:54 -0500189 struct vc4_dev *vc4 = to_vc4_dev(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800190 int ret;
191
Eric Anholtb501bac2015-11-30 12:34:01 -0800192 sema_init(&vc4->async_modeset, 1);
193
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800194 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
195 if (ret < 0) {
196 dev_err(dev->dev, "failed to initialize vblank\n");
197 return ret;
198 }
199
200 dev->mode_config.max_width = 2048;
201 dev->mode_config.max_height = 2048;
202 dev->mode_config.funcs = &vc4_mode_funcs;
203 dev->mode_config.preferred_depth = 24;
Eric Anholtb501bac2015-11-30 12:34:01 -0800204 dev->mode_config.async_page_flip = true;
205
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800206 drm_mode_config_reset(dev);
207
Derek Foreman48666d52015-07-02 11:19:54 -0500208 vc4->fbdev = drm_fbdev_cma_init(dev, 32,
209 dev->mode_config.num_crtc,
210 dev->mode_config.num_connector);
211 if (IS_ERR(vc4->fbdev))
212 vc4->fbdev = NULL;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800213
214 drm_kms_helper_poll_init(dev);
215
216 return 0;
217}