Loading...
1/*
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"
18#include "drm_atomic.h"
19#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
25static 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
33struct 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
52 /* 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
61 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
96 * @async: asynchronous commit
97 *
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,
107 bool async)
108{
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;
114
115 c = commit_init(state);
116 if (!c)
117 return -ENOMEM;
118
119 /* Make sure that any outstanding modesets have finished. */
120 ret = down_interruptible(&vc4->async_modeset);
121 if (ret) {
122 kfree(c);
123 return ret;
124 }
125
126 ret = drm_atomic_helper_prepare_planes(dev, state);
127 if (ret) {
128 kfree(c);
129 up(&vc4->async_modeset);
130 return ret;
131 }
132
133 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
134 struct drm_plane *plane = state->planes[i];
135 struct drm_plane_state *new_state = state->plane_states[i];
136
137 if (!plane)
138 continue;
139
140 if ((plane->state->fb != new_state->fb) && new_state->fb) {
141 struct drm_gem_cma_object *cma_bo =
142 drm_fb_cma_get_gem_obj(new_state->fb, 0);
143 struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
144
145 wait_seqno = max(bo->seqno, wait_seqno);
146 }
147 }
148
149 /*
150 * This is the point of no return - everything below never fails except
151 * when the hw goes bonghits. Which means we can commit the new state on
152 * the software side now.
153 */
154
155 drm_atomic_helper_swap_state(dev, state);
156
157 /*
158 * Everything below can be run asynchronously without the need to grab
159 * any modeset locks at all under one condition: It must be guaranteed
160 * that the asynchronous work has either been cancelled (if the driver
161 * supports it, which at least requires that the framebuffers get
162 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
163 * before the new state gets committed on the software side with
164 * drm_atomic_helper_swap_state().
165 *
166 * This scheme allows new atomic state updates to be prepared and
167 * checked in parallel to the asynchronous completion of the previous
168 * update. Which is important since compositors need to figure out the
169 * composition of the next frame right after having submitted the
170 * current layout.
171 */
172
173 if (async) {
174 vc4_queue_seqno_cb(dev, &c->cb, wait_seqno,
175 vc4_atomic_complete_commit_seqno_cb);
176 } else {
177 vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false);
178 vc4_atomic_complete_commit(c);
179 }
180
181 return 0;
182}
183
184static const struct drm_mode_config_funcs vc4_mode_funcs = {
185 .output_poll_changed = vc4_output_poll_changed,
186 .atomic_check = drm_atomic_helper_check,
187 .atomic_commit = vc4_atomic_commit,
188 .fb_create = drm_fb_cma_create,
189};
190
191int vc4_kms_load(struct drm_device *dev)
192{
193 struct vc4_dev *vc4 = to_vc4_dev(dev);
194 int ret;
195
196 sema_init(&vc4->async_modeset, 1);
197
198 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
199 if (ret < 0) {
200 dev_err(dev->dev, "failed to initialize vblank\n");
201 return ret;
202 }
203
204 dev->mode_config.max_width = 2048;
205 dev->mode_config.max_height = 2048;
206 dev->mode_config.funcs = &vc4_mode_funcs;
207 dev->mode_config.preferred_depth = 24;
208 dev->mode_config.async_page_flip = true;
209
210 dev->vblank_disable_allowed = true;
211
212 drm_mode_config_reset(dev);
213
214 vc4->fbdev = drm_fbdev_cma_init(dev, 32,
215 dev->mode_config.num_crtc,
216 dev->mode_config.num_connector);
217 if (IS_ERR(vc4->fbdev))
218 vc4->fbdev = NULL;
219
220 drm_kms_helper_poll_init(dev);
221
222 return 0;
223}
1/*
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/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_helper.h>
23#include <drm/drm_fb_cma_helper.h>
24#include <drm/drm_gem_framebuffer_helper.h>
25#include "vc4_drv.h"
26
27static void
28vc4_atomic_complete_commit(struct drm_atomic_state *state)
29{
30 struct drm_device *dev = state->dev;
31 struct vc4_dev *vc4 = to_vc4_dev(dev);
32
33 drm_atomic_helper_wait_for_fences(dev, state, false);
34
35 drm_atomic_helper_wait_for_dependencies(state);
36
37 drm_atomic_helper_commit_modeset_disables(dev, state);
38
39 drm_atomic_helper_commit_planes(dev, state, 0);
40
41 drm_atomic_helper_commit_modeset_enables(dev, state);
42
43 /* Make sure that drm_atomic_helper_wait_for_vblanks()
44 * actually waits for vblank. If we're doing a full atomic
45 * modeset (as opposed to a vc4_update_plane() short circuit),
46 * then we need to wait for scanout to be done with our
47 * display lists before we free it and potentially reallocate
48 * and overwrite the dlist memory with a new modeset.
49 */
50 state->legacy_cursor_update = false;
51
52 drm_atomic_helper_commit_hw_done(state);
53
54 drm_atomic_helper_wait_for_vblanks(dev, state);
55
56 drm_atomic_helper_cleanup_planes(dev, state);
57
58 drm_atomic_helper_commit_cleanup_done(state);
59
60 drm_atomic_state_put(state);
61
62 up(&vc4->async_modeset);
63}
64
65static void commit_work(struct work_struct *work)
66{
67 struct drm_atomic_state *state = container_of(work,
68 struct drm_atomic_state,
69 commit_work);
70 vc4_atomic_complete_commit(state);
71}
72
73/**
74 * vc4_atomic_commit - commit validated state object
75 * @dev: DRM device
76 * @state: the driver state object
77 * @nonblock: nonblocking commit
78 *
79 * This function commits a with drm_atomic_helper_check() pre-validated state
80 * object. This can still fail when e.g. the framebuffer reservation fails. For
81 * now this doesn't implement asynchronous commits.
82 *
83 * RETURNS
84 * Zero for success or -errno.
85 */
86static int vc4_atomic_commit(struct drm_device *dev,
87 struct drm_atomic_state *state,
88 bool nonblock)
89{
90 struct vc4_dev *vc4 = to_vc4_dev(dev);
91 int ret;
92
93 ret = drm_atomic_helper_setup_commit(state, nonblock);
94 if (ret)
95 return ret;
96
97 INIT_WORK(&state->commit_work, commit_work);
98
99 ret = down_interruptible(&vc4->async_modeset);
100 if (ret)
101 return ret;
102
103 ret = drm_atomic_helper_prepare_planes(dev, state);
104 if (ret) {
105 up(&vc4->async_modeset);
106 return ret;
107 }
108
109 if (!nonblock) {
110 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
111 if (ret) {
112 drm_atomic_helper_cleanup_planes(dev, state);
113 up(&vc4->async_modeset);
114 return ret;
115 }
116 }
117
118 /*
119 * This is the point of no return - everything below never fails except
120 * when the hw goes bonghits. Which means we can commit the new state on
121 * the software side now.
122 */
123
124 BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
125
126 /*
127 * Everything below can be run asynchronously without the need to grab
128 * any modeset locks at all under one condition: It must be guaranteed
129 * that the asynchronous work has either been cancelled (if the driver
130 * supports it, which at least requires that the framebuffers get
131 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
132 * before the new state gets committed on the software side with
133 * drm_atomic_helper_swap_state().
134 *
135 * This scheme allows new atomic state updates to be prepared and
136 * checked in parallel to the asynchronous completion of the previous
137 * update. Which is important since compositors need to figure out the
138 * composition of the next frame right after having submitted the
139 * current layout.
140 */
141
142 drm_atomic_state_get(state);
143 if (nonblock)
144 queue_work(system_unbound_wq, &state->commit_work);
145 else
146 vc4_atomic_complete_commit(state);
147
148 return 0;
149}
150
151static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
152 struct drm_file *file_priv,
153 const struct drm_mode_fb_cmd2 *mode_cmd)
154{
155 struct drm_mode_fb_cmd2 mode_cmd_local;
156
157 /* If the user didn't specify a modifier, use the
158 * vc4_set_tiling_ioctl() state for the BO.
159 */
160 if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
161 struct drm_gem_object *gem_obj;
162 struct vc4_bo *bo;
163
164 gem_obj = drm_gem_object_lookup(file_priv,
165 mode_cmd->handles[0]);
166 if (!gem_obj) {
167 DRM_DEBUG("Failed to look up GEM BO %d\n",
168 mode_cmd->handles[0]);
169 return ERR_PTR(-ENOENT);
170 }
171 bo = to_vc4_bo(gem_obj);
172
173 mode_cmd_local = *mode_cmd;
174
175 if (bo->t_format) {
176 mode_cmd_local.modifier[0] =
177 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
178 } else {
179 mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
180 }
181
182 drm_gem_object_put_unlocked(gem_obj);
183
184 mode_cmd = &mode_cmd_local;
185 }
186
187 return drm_gem_fb_create(dev, file_priv, mode_cmd);
188}
189
190static const struct drm_mode_config_funcs vc4_mode_funcs = {
191 .output_poll_changed = drm_fb_helper_output_poll_changed,
192 .atomic_check = drm_atomic_helper_check,
193 .atomic_commit = vc4_atomic_commit,
194 .fb_create = vc4_fb_create,
195};
196
197int vc4_kms_load(struct drm_device *dev)
198{
199 struct vc4_dev *vc4 = to_vc4_dev(dev);
200 int ret;
201
202 sema_init(&vc4->async_modeset, 1);
203
204 /* Set support for vblank irq fast disable, before drm_vblank_init() */
205 dev->vblank_disable_immediate = true;
206
207 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
208 if (ret < 0) {
209 dev_err(dev->dev, "failed to initialize vblank\n");
210 return ret;
211 }
212
213 dev->mode_config.max_width = 2048;
214 dev->mode_config.max_height = 2048;
215 dev->mode_config.funcs = &vc4_mode_funcs;
216 dev->mode_config.preferred_depth = 24;
217 dev->mode_config.async_page_flip = true;
218 dev->mode_config.allow_fb_modifiers = true;
219
220 drm_mode_config_reset(dev);
221
222 if (dev->mode_config.num_connector)
223 drm_fb_cma_fbdev_init(dev, 32, 0);
224
225 drm_kms_helper_poll_init(dev);
226
227 return 0;
228}