Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Red Hat |
| 3 | * Author: Rob Clark <robdclark@gmail.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "msm_drv.h" |
Rob Clark | dd2da6e | 2013-11-30 16:12:10 -0500 | [diff] [blame] | 19 | #include "msm_kms.h" |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 20 | |
| 21 | #include "drm_crtc.h" |
| 22 | #include "drm_crtc_helper.h" |
| 23 | |
| 24 | struct msm_framebuffer { |
| 25 | struct drm_framebuffer base; |
| 26 | const struct msm_format *format; |
Stephane Viau | 7ca1271 | 2014-12-08 10:48:57 -0500 | [diff] [blame] | 27 | struct drm_gem_object *planes[MAX_PLANE]; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 28 | }; |
| 29 | #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base) |
| 30 | |
| 31 | |
| 32 | static int msm_framebuffer_create_handle(struct drm_framebuffer *fb, |
| 33 | struct drm_file *file_priv, |
| 34 | unsigned int *handle) |
| 35 | { |
| 36 | struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); |
| 37 | return drm_gem_handle_create(file_priv, |
| 38 | msm_fb->planes[0], handle); |
| 39 | } |
| 40 | |
| 41 | static void msm_framebuffer_destroy(struct drm_framebuffer *fb) |
| 42 | { |
| 43 | struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); |
| 44 | int i, n = drm_format_num_planes(fb->pixel_format); |
| 45 | |
| 46 | DBG("destroy: FB ID: %d (%p)", fb->base.id, fb); |
| 47 | |
| 48 | drm_framebuffer_cleanup(fb); |
| 49 | |
| 50 | for (i = 0; i < n; i++) { |
| 51 | struct drm_gem_object *bo = msm_fb->planes[i]; |
Markus Elfring | e73a856 | 2016-07-13 19:15:35 +0200 | [diff] [blame] | 52 | |
| 53 | drm_gem_object_unreference_unlocked(bo); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | kfree(msm_fb); |
| 57 | } |
| 58 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 59 | static const struct drm_framebuffer_funcs msm_framebuffer_funcs = { |
| 60 | .create_handle = msm_framebuffer_create_handle, |
| 61 | .destroy = msm_framebuffer_destroy, |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | #ifdef CONFIG_DEBUG_FS |
| 65 | void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m) |
| 66 | { |
| 67 | struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); |
| 68 | int i, n = drm_format_num_planes(fb->pixel_format); |
| 69 | |
| 70 | seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n", |
| 71 | fb->width, fb->height, (char *)&fb->pixel_format, |
Dave Airlie | 747a598 | 2016-04-15 15:10:35 +1000 | [diff] [blame] | 72 | drm_framebuffer_read_refcount(fb), fb->base.id); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 73 | |
| 74 | for (i = 0; i < n; i++) { |
| 75 | seq_printf(m, " %d: offset=%d pitch=%d, obj: ", |
| 76 | i, fb->offsets[i], fb->pitches[i]); |
| 77 | msm_gem_describe(msm_fb->planes[i], m); |
| 78 | } |
| 79 | } |
| 80 | #endif |
| 81 | |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 82 | /* prepare/pin all the fb's bo's for scanout. Note that it is not valid |
| 83 | * to prepare an fb more multiple different initiator 'id's. But that |
| 84 | * should be fine, since only the scanout (mdpN) side of things needs |
| 85 | * this, the gpu doesn't care about fb's. |
| 86 | */ |
| 87 | int msm_framebuffer_prepare(struct drm_framebuffer *fb, int id) |
| 88 | { |
| 89 | struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); |
| 90 | int ret, i, n = drm_format_num_planes(fb->pixel_format); |
| 91 | uint32_t iova; |
| 92 | |
| 93 | for (i = 0; i < n; i++) { |
| 94 | ret = msm_gem_get_iova(msm_fb->planes[i], id, &iova); |
| 95 | DBG("FB[%u]: iova[%d]: %08x (%d)", fb->base.id, i, iova, ret); |
| 96 | if (ret) |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | void msm_framebuffer_cleanup(struct drm_framebuffer *fb, int id) |
| 104 | { |
| 105 | struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); |
| 106 | int i, n = drm_format_num_planes(fb->pixel_format); |
| 107 | |
| 108 | for (i = 0; i < n; i++) |
| 109 | msm_gem_put_iova(msm_fb->planes[i], id); |
| 110 | } |
| 111 | |
| 112 | uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb, int id, int plane) |
| 113 | { |
| 114 | struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); |
Rob Clark | 3e2f29e | 2014-11-19 12:29:33 -0500 | [diff] [blame] | 115 | if (!msm_fb->planes[plane]) |
| 116 | return 0; |
Stephane Viau | f8d9b51 | 2014-12-08 10:48:58 -0500 | [diff] [blame] | 117 | return msm_gem_iova(msm_fb->planes[plane], id) + fb->offsets[plane]; |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 120 | struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane) |
| 121 | { |
| 122 | struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb); |
| 123 | return msm_fb->planes[plane]; |
| 124 | } |
| 125 | |
| 126 | const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb) |
| 127 | { |
Dhaval Patel | ccbcb3d | 2016-08-22 11:58:14 -0700 | [diff] [blame^] | 128 | return fb ? (to_msm_framebuffer(fb))->format : NULL; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 132 | struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 133 | { |
| 134 | struct drm_gem_object *bos[4] = {0}; |
| 135 | struct drm_framebuffer *fb; |
| 136 | int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format); |
| 137 | |
| 138 | for (i = 0; i < n; i++) { |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 139 | bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 140 | if (!bos[i]) { |
| 141 | ret = -ENXIO; |
| 142 | goto out_unref; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | fb = msm_framebuffer_init(dev, mode_cmd, bos); |
| 147 | if (IS_ERR(fb)) { |
| 148 | ret = PTR_ERR(fb); |
| 149 | goto out_unref; |
| 150 | } |
| 151 | |
| 152 | return fb; |
| 153 | |
| 154 | out_unref: |
| 155 | for (i = 0; i < n; i++) |
| 156 | drm_gem_object_unreference_unlocked(bos[i]); |
| 157 | return ERR_PTR(ret); |
| 158 | } |
| 159 | |
| 160 | struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 161 | const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 162 | { |
| 163 | struct msm_drm_private *priv = dev->dev_private; |
| 164 | struct msm_kms *kms = priv->kms; |
Stephane Viau | 7194b62 | 2015-05-05 09:47:57 -0400 | [diff] [blame] | 165 | struct msm_framebuffer *msm_fb = NULL; |
| 166 | struct drm_framebuffer *fb; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 167 | const struct msm_format *format; |
Lloyd Atkinson | 9a67349 | 2016-07-05 11:41:57 -0400 | [diff] [blame] | 168 | int ret, i, num_planes; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 169 | unsigned int hsub, vsub; |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 170 | bool is_modified = false; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 171 | |
| 172 | DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)", |
| 173 | dev, mode_cmd, mode_cmd->width, mode_cmd->height, |
| 174 | (char *)&mode_cmd->pixel_format); |
| 175 | |
Lloyd Atkinson | 9a67349 | 2016-07-05 11:41:57 -0400 | [diff] [blame] | 176 | num_planes = drm_format_num_planes(mode_cmd->pixel_format); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 177 | hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format); |
| 178 | vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format); |
| 179 | |
Lloyd Atkinson | 9a67349 | 2016-07-05 11:41:57 -0400 | [diff] [blame] | 180 | format = kms->funcs->get_format(kms, mode_cmd->pixel_format, |
| 181 | mode_cmd->modifier, num_planes); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 182 | if (!format) { |
| 183 | dev_err(dev->dev, "unsupported pixel format: %4.4s\n", |
| 184 | (char *)&mode_cmd->pixel_format); |
| 185 | ret = -EINVAL; |
| 186 | goto fail; |
| 187 | } |
| 188 | |
| 189 | msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL); |
| 190 | if (!msm_fb) { |
| 191 | ret = -ENOMEM; |
| 192 | goto fail; |
| 193 | } |
| 194 | |
| 195 | fb = &msm_fb->base; |
| 196 | |
| 197 | msm_fb->format = format; |
| 198 | |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 199 | if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) { |
| 200 | for (i = 0; i < ARRAY_SIZE(mode_cmd->modifier); i++) { |
| 201 | if (mode_cmd->modifier[i]) { |
| 202 | is_modified = true; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Lloyd Atkinson | 9a67349 | 2016-07-05 11:41:57 -0400 | [diff] [blame] | 208 | if (num_planes > ARRAY_SIZE(msm_fb->planes)) { |
Rob Clark | 10291bf | 2014-11-08 09:20:28 -0500 | [diff] [blame] | 209 | ret = -EINVAL; |
| 210 | goto fail; |
| 211 | } |
| 212 | |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 213 | if (is_modified) { |
| 214 | if (!kms->funcs->check_modified_format) { |
| 215 | dev_err(dev->dev, "can't check modified fb format\n"); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 216 | ret = -EINVAL; |
| 217 | goto fail; |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 218 | } else { |
| 219 | ret = kms->funcs->check_modified_format( |
| 220 | kms, msm_fb->format, mode_cmd, bos); |
| 221 | if (ret) |
| 222 | goto fail; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 223 | } |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 224 | } else { |
| 225 | for (i = 0; i < num_planes; i++) { |
| 226 | unsigned int width = mode_cmd->width / (i ? hsub : 1); |
| 227 | unsigned int height = mode_cmd->height / (i ? vsub : 1); |
| 228 | unsigned int min_size; |
| 229 | unsigned int cpp; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 230 | |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 231 | cpp = drm_format_plane_cpp(mode_cmd->pixel_format, i); |
| 232 | |
| 233 | min_size = (height - 1) * mode_cmd->pitches[i] |
| 234 | + width * cpp |
| 235 | + mode_cmd->offsets[i]; |
| 236 | |
| 237 | if (bos[i]->size < min_size) { |
| 238 | ret = -EINVAL; |
| 239 | goto fail; |
| 240 | } |
| 241 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 244 | for (i = 0; i < num_planes; i++) |
| 245 | msm_fb->planes[i] = bos[i]; |
| 246 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 247 | drm_helper_mode_fill_fb_struct(fb, mode_cmd); |
| 248 | |
| 249 | ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs); |
| 250 | if (ret) { |
| 251 | dev_err(dev->dev, "framebuffer init failed: %d\n", ret); |
| 252 | goto fail; |
| 253 | } |
| 254 | |
| 255 | DBG("create: FB ID: %d (%p)", fb->base.id, fb); |
| 256 | |
| 257 | return fb; |
| 258 | |
| 259 | fail: |
Stephane Viau | 7194b62 | 2015-05-05 09:47:57 -0400 | [diff] [blame] | 260 | kfree(msm_fb); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 261 | |
| 262 | return ERR_PTR(ret); |
| 263 | } |