bpf: Prevent memory disambiguation attack
[linux/fpc-iii.git] / drivers / gpu / drm / vc4 / vc4_kms.c
blob4256f294c3468416580c03132726517bca134c21
1 /*
2 * Copyright (C) 2015 Broadcom
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 */
9 /**
10 * DOC: VC4 KMS
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).
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"
27 static void
28 vc4_atomic_complete_commit(struct drm_atomic_state *state)
30 struct drm_device *dev = state->dev;
31 struct vc4_dev *vc4 = to_vc4_dev(dev);
33 drm_atomic_helper_wait_for_fences(dev, state, false);
35 drm_atomic_helper_wait_for_dependencies(state);
37 drm_atomic_helper_commit_modeset_disables(dev, state);
39 drm_atomic_helper_commit_planes(dev, state, 0);
41 drm_atomic_helper_commit_modeset_enables(dev, state);
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.
50 state->legacy_cursor_update = false;
52 drm_atomic_helper_commit_hw_done(state);
54 drm_atomic_helper_wait_for_vblanks(dev, state);
56 drm_atomic_helper_cleanup_planes(dev, state);
58 drm_atomic_helper_commit_cleanup_done(state);
60 drm_atomic_state_put(state);
62 up(&vc4->async_modeset);
65 static void commit_work(struct work_struct *work)
67 struct drm_atomic_state *state = container_of(work,
68 struct drm_atomic_state,
69 commit_work);
70 vc4_atomic_complete_commit(state);
73 /**
74 * vc4_atomic_commit - commit validated state object
75 * @dev: DRM device
76 * @state: the driver state object
77 * @nonblock: nonblocking commit
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.
83 * RETURNS
84 * Zero for success or -errno.
86 static int vc4_atomic_commit(struct drm_device *dev,
87 struct drm_atomic_state *state,
88 bool nonblock)
90 struct vc4_dev *vc4 = to_vc4_dev(dev);
91 int ret;
93 ret = drm_atomic_helper_setup_commit(state, nonblock);
94 if (ret)
95 return ret;
97 INIT_WORK(&state->commit_work, commit_work);
99 ret = down_interruptible(&vc4->async_modeset);
100 if (ret)
101 return ret;
103 ret = drm_atomic_helper_prepare_planes(dev, state);
104 if (ret) {
105 up(&vc4->async_modeset);
106 return ret;
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;
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.
124 BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
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().
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.
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);
148 return 0;
151 static 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)
155 struct drm_mode_fb_cmd2 mode_cmd_local;
157 /* If the user didn't specify a modifier, use the
158 * vc4_set_tiling_ioctl() state for the BO.
160 if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
161 struct drm_gem_object *gem_obj;
162 struct vc4_bo *bo;
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);
171 bo = to_vc4_bo(gem_obj);
173 mode_cmd_local = *mode_cmd;
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;
182 drm_gem_object_put_unlocked(gem_obj);
184 mode_cmd = &mode_cmd_local;
187 return drm_gem_fb_create(dev, file_priv, mode_cmd);
190 static 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,
197 int vc4_kms_load(struct drm_device *dev)
199 struct vc4_dev *vc4 = to_vc4_dev(dev);
200 int ret;
202 sema_init(&vc4->async_modeset, 1);
204 /* Set support for vblank irq fast disable, before drm_vblank_init() */
205 dev->vblank_disable_immediate = true;
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;
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;
219 drm_mode_config_reset(dev);
221 if (dev->mode_config.num_connector)
222 drm_fb_cma_fbdev_init(dev, 32, 0);
224 drm_kms_helper_poll_init(dev);
226 return 0;