Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1 | /* |
Lakshmi Narayana Kalavala | 89b6cbe | 2018-05-11 11:28:12 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 3 | * Copyright (C) 2013 Red Hat |
| 4 | * Author: Rob Clark <robdclark@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published by |
| 8 | * the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 19 | #include <linux/dma-mapping.h> |
| 20 | #include <linux/dma-buf.h> |
| 21 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 22 | #include "msm_drv.h" |
Rob Clark | dd2da6e | 2013-11-30 16:12:10 -0500 | [diff] [blame] | 23 | #include "msm_kms.h" |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 24 | |
| 25 | #include "drm_crtc.h" |
| 26 | #include "drm_crtc_helper.h" |
| 27 | |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 28 | #define MSM_FRAMEBUFFER_FLAG_KMAP BIT(0) |
| 29 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 30 | struct msm_framebuffer { |
| 31 | struct drm_framebuffer base; |
| 32 | const struct msm_format *format; |
Stephane Viau | 7ca1271 | 2014-12-08 10:48:57 -0500 | [diff] [blame] | 33 | struct drm_gem_object *planes[MAX_PLANE]; |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 34 | void *vaddr[MAX_PLANE]; |
| 35 | atomic_t kmap_count; |
| 36 | u32 flags; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 37 | }; |
| 38 | #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base) |
| 39 | |
| 40 | |
| 41 | static int msm_framebuffer_create_handle(struct drm_framebuffer *fb, |
| 42 | struct drm_file *file_priv, |
| 43 | unsigned int *handle) |
| 44 | { |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 45 | struct msm_framebuffer *msm_fb; |
| 46 | |
| 47 | if (!fb) { |
| 48 | DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0)); |
| 49 | return -EINVAL; |
| 50 | } |
| 51 | |
| 52 | msm_fb = to_msm_framebuffer(fb); |
| 53 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 54 | return drm_gem_handle_create(file_priv, |
| 55 | msm_fb->planes[0], handle); |
| 56 | } |
| 57 | |
| 58 | static void msm_framebuffer_destroy(struct drm_framebuffer *fb) |
| 59 | { |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 60 | struct msm_framebuffer *msm_fb; |
| 61 | int i, n; |
| 62 | |
| 63 | if (!fb) { |
| 64 | DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0)); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | msm_fb = to_msm_framebuffer(fb); |
| 69 | n = drm_format_num_planes(fb->pixel_format); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 70 | |
Lakshmi Narayana Kalavala | 89b6cbe | 2018-05-11 11:28:12 -0700 | [diff] [blame] | 71 | DBG("destroy: FB ID: %d (%pK)", fb->base.id, fb); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 72 | |
| 73 | drm_framebuffer_cleanup(fb); |
| 74 | |
| 75 | for (i = 0; i < n; i++) { |
| 76 | struct drm_gem_object *bo = msm_fb->planes[i]; |
Markus Elfring | e73a856 | 2016-07-13 19:15:35 +0200 | [diff] [blame] | 77 | |
| 78 | drm_gem_object_unreference_unlocked(bo); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | kfree(msm_fb); |
| 82 | } |
| 83 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 84 | static const struct drm_framebuffer_funcs msm_framebuffer_funcs = { |
| 85 | .create_handle = msm_framebuffer_create_handle, |
| 86 | .destroy = msm_framebuffer_destroy, |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | #ifdef CONFIG_DEBUG_FS |
| 90 | void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m) |
| 91 | { |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 92 | struct msm_framebuffer *msm_fb; |
| 93 | int i, n; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 94 | |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 95 | if (!fb) { |
| 96 | DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0)); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | msm_fb = to_msm_framebuffer(fb); |
| 101 | n = drm_format_num_planes(fb->pixel_format); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 102 | seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n", |
| 103 | fb->width, fb->height, (char *)&fb->pixel_format, |
Dave Airlie | 747a598 | 2016-04-15 15:10:35 +1000 | [diff] [blame] | 104 | drm_framebuffer_read_refcount(fb), fb->base.id); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 105 | |
| 106 | for (i = 0; i < n; i++) { |
| 107 | seq_printf(m, " %d: offset=%d pitch=%d, obj: ", |
| 108 | i, fb->offsets[i], fb->pitches[i]); |
| 109 | msm_gem_describe(msm_fb->planes[i], m); |
| 110 | } |
| 111 | } |
| 112 | #endif |
| 113 | |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 114 | void msm_framebuffer_set_kmap(struct drm_framebuffer *fb, bool enable) |
| 115 | { |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 116 | struct msm_framebuffer *msm_fb; |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 117 | |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 118 | if (!fb) { |
| 119 | DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0)); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | msm_fb = to_msm_framebuffer(fb); |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 124 | if (enable) |
| 125 | msm_fb->flags |= MSM_FRAMEBUFFER_FLAG_KMAP; |
| 126 | else |
| 127 | msm_fb->flags &= ~MSM_FRAMEBUFFER_FLAG_KMAP; |
| 128 | } |
| 129 | |
| 130 | static int msm_framebuffer_kmap(struct drm_framebuffer *fb) |
| 131 | { |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 132 | struct msm_framebuffer *msm_fb; |
| 133 | int i, n; |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 134 | struct drm_gem_object *bo; |
| 135 | |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 136 | if (!fb) { |
| 137 | DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0)); |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | |
| 141 | msm_fb = to_msm_framebuffer(fb); |
| 142 | n = drm_format_num_planes(fb->pixel_format); |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 143 | if (atomic_inc_return(&msm_fb->kmap_count) > 1) |
| 144 | return 0; |
| 145 | |
| 146 | for (i = 0; i < n; i++) { |
| 147 | bo = msm_framebuffer_bo(fb, i); |
| 148 | if (!bo || !bo->dma_buf) { |
| 149 | msm_fb->vaddr[i] = NULL; |
| 150 | continue; |
| 151 | } |
| 152 | dma_buf_begin_cpu_access(bo->dma_buf, DMA_BIDIRECTIONAL); |
| 153 | msm_fb->vaddr[i] = dma_buf_kmap(bo->dma_buf, 0); |
| 154 | DRM_INFO("FB[%u]: vaddr[%d]:%ux%u:0x%llx\n", fb->base.id, i, |
| 155 | fb->width, fb->height, (u64) msm_fb->vaddr[i]); |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static void msm_framebuffer_kunmap(struct drm_framebuffer *fb) |
| 162 | { |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 163 | struct msm_framebuffer *msm_fb; |
| 164 | int i, n; |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 165 | struct drm_gem_object *bo; |
| 166 | |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 167 | if (!fb) { |
| 168 | DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0)); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | msm_fb = to_msm_framebuffer(fb); |
| 173 | n = drm_format_num_planes(fb->pixel_format); |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 174 | if (atomic_dec_return(&msm_fb->kmap_count) > 0) |
| 175 | return; |
| 176 | |
| 177 | for (i = 0; i < n; i++) { |
| 178 | bo = msm_framebuffer_bo(fb, i); |
| 179 | if (!bo || !msm_fb->vaddr[i]) |
| 180 | continue; |
| 181 | if (bo->dma_buf) { |
| 182 | dma_buf_kunmap(bo->dma_buf, 0, msm_fb->vaddr[i]); |
| 183 | dma_buf_end_cpu_access(bo->dma_buf, DMA_BIDIRECTIONAL); |
| 184 | } |
| 185 | msm_fb->vaddr[i] = NULL; |
| 186 | } |
| 187 | } |
| 188 | |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 189 | /* prepare/pin all the fb's bo's for scanout. Note that it is not valid |
| 190 | * to prepare an fb more multiple different initiator 'id's. But that |
| 191 | * should be fine, since only the scanout (mdpN) side of things needs |
| 192 | * this, the gpu doesn't care about fb's. |
| 193 | */ |
Jordan Crouse | d8e9652 | 2017-02-13 10:14:16 -0700 | [diff] [blame] | 194 | int msm_framebuffer_prepare(struct drm_framebuffer *fb, |
| 195 | struct msm_gem_address_space *aspace) |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 196 | { |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 197 | struct msm_framebuffer *msm_fb; |
| 198 | int ret, i, n; |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 199 | uint32_t iova; |
| 200 | |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 201 | if (!fb) { |
| 202 | DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0)); |
| 203 | return -EINVAL; |
| 204 | } |
| 205 | |
| 206 | msm_fb = to_msm_framebuffer(fb); |
| 207 | n = drm_format_num_planes(fb->pixel_format); |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 208 | for (i = 0; i < n; i++) { |
Jordan Crouse | d8e9652 | 2017-02-13 10:14:16 -0700 | [diff] [blame] | 209 | ret = msm_gem_get_iova(msm_fb->planes[i], aspace, &iova); |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 210 | DBG("FB[%u]: iova[%d]: %08x (%d)", fb->base.id, i, iova, ret); |
| 211 | if (ret) |
| 212 | return ret; |
| 213 | } |
| 214 | |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 215 | if (msm_fb->flags & MSM_FRAMEBUFFER_FLAG_KMAP) |
| 216 | msm_framebuffer_kmap(fb); |
| 217 | |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
Jordan Crouse | d8e9652 | 2017-02-13 10:14:16 -0700 | [diff] [blame] | 221 | void msm_framebuffer_cleanup(struct drm_framebuffer *fb, |
| 222 | struct msm_gem_address_space *aspace) |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 223 | { |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 224 | struct msm_framebuffer *msm_fb; |
| 225 | int i, n; |
| 226 | |
| 227 | if (fb == NULL) { |
| 228 | DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0)); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | msm_fb = to_msm_framebuffer(fb); |
| 233 | n = drm_format_num_planes(fb->pixel_format); |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 234 | |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 235 | if (msm_fb->flags & MSM_FRAMEBUFFER_FLAG_KMAP) |
| 236 | msm_framebuffer_kunmap(fb); |
| 237 | |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 238 | for (i = 0; i < n; i++) |
Jordan Crouse | d8e9652 | 2017-02-13 10:14:16 -0700 | [diff] [blame] | 239 | msm_gem_put_iova(msm_fb->planes[i], aspace); |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 240 | } |
| 241 | |
Jordan Crouse | d8e9652 | 2017-02-13 10:14:16 -0700 | [diff] [blame] | 242 | uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb, |
| 243 | struct msm_gem_address_space *aspace, int plane) |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 244 | { |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 245 | struct msm_framebuffer *msm_fb; |
| 246 | |
| 247 | if (!fb) { |
| 248 | DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0)); |
| 249 | return -EINVAL; |
| 250 | } |
| 251 | |
| 252 | msm_fb = to_msm_framebuffer(fb); |
Rob Clark | 3e2f29e | 2014-11-19 12:29:33 -0500 | [diff] [blame] | 253 | if (!msm_fb->planes[plane]) |
| 254 | return 0; |
Jordan Crouse | d8e9652 | 2017-02-13 10:14:16 -0700 | [diff] [blame] | 255 | return msm_gem_iova(msm_fb->planes[plane], aspace) + fb->offsets[plane]; |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Abhijit Kulkarni | 1b3340c | 2017-06-22 12:39:37 -0700 | [diff] [blame] | 258 | uint32_t msm_framebuffer_phys(struct drm_framebuffer *fb, |
| 259 | int plane) |
| 260 | { |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 261 | struct msm_framebuffer *msm_fb; |
Abhijit Kulkarni | 1b3340c | 2017-06-22 12:39:37 -0700 | [diff] [blame] | 262 | dma_addr_t phys_addr; |
| 263 | |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 264 | if (!fb) { |
| 265 | DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0)); |
| 266 | return -EINVAL; |
| 267 | } |
| 268 | |
| 269 | msm_fb = to_msm_framebuffer(fb); |
Abhijit Kulkarni | 1b3340c | 2017-06-22 12:39:37 -0700 | [diff] [blame] | 270 | if (!msm_fb->planes[plane]) |
| 271 | return 0; |
| 272 | |
| 273 | phys_addr = msm_gem_get_dma_addr(msm_fb->planes[plane]); |
| 274 | if (!phys_addr) |
| 275 | return 0; |
| 276 | |
| 277 | return phys_addr + fb->offsets[plane]; |
| 278 | } |
| 279 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 280 | struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane) |
| 281 | { |
Harsh Sahu | 97b52a7 | 2017-10-13 11:32:28 -0700 | [diff] [blame] | 282 | struct msm_framebuffer *msm_fb; |
| 283 | |
| 284 | if (!fb) { |
| 285 | DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0)); |
| 286 | return ERR_PTR(-EINVAL); |
| 287 | } |
| 288 | |
| 289 | msm_fb = to_msm_framebuffer(fb); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 290 | return msm_fb->planes[plane]; |
| 291 | } |
| 292 | |
| 293 | const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb) |
| 294 | { |
Dhaval Patel | ccbcb3d | 2016-08-22 11:58:14 -0700 | [diff] [blame] | 295 | return fb ? (to_msm_framebuffer(fb))->format : NULL; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 299 | struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 300 | { |
| 301 | struct drm_gem_object *bos[4] = {0}; |
| 302 | struct drm_framebuffer *fb; |
| 303 | int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format); |
| 304 | |
| 305 | for (i = 0; i < n; i++) { |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 306 | bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 307 | if (!bos[i]) { |
| 308 | ret = -ENXIO; |
| 309 | goto out_unref; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | fb = msm_framebuffer_init(dev, mode_cmd, bos); |
| 314 | if (IS_ERR(fb)) { |
| 315 | ret = PTR_ERR(fb); |
| 316 | goto out_unref; |
| 317 | } |
| 318 | |
| 319 | return fb; |
| 320 | |
| 321 | out_unref: |
| 322 | for (i = 0; i < n; i++) |
| 323 | drm_gem_object_unreference_unlocked(bos[i]); |
| 324 | return ERR_PTR(ret); |
| 325 | } |
| 326 | |
| 327 | struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 328 | 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] | 329 | { |
| 330 | struct msm_drm_private *priv = dev->dev_private; |
| 331 | struct msm_kms *kms = priv->kms; |
Stephane Viau | 7194b62 | 2015-05-05 09:47:57 -0400 | [diff] [blame] | 332 | struct msm_framebuffer *msm_fb = NULL; |
| 333 | struct drm_framebuffer *fb; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 334 | const struct msm_format *format; |
Lloyd Atkinson | 9a67349 | 2016-07-05 11:41:57 -0400 | [diff] [blame] | 335 | int ret, i, num_planes; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 336 | unsigned int hsub, vsub; |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 337 | bool is_modified = false; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 338 | |
Lakshmi Narayana Kalavala | 89b6cbe | 2018-05-11 11:28:12 -0700 | [diff] [blame] | 339 | DBG("create framebuffer: dev=%pK, mode_cmd=%pK (%dx%d@%4.4s)", |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 340 | dev, mode_cmd, mode_cmd->width, mode_cmd->height, |
| 341 | (char *)&mode_cmd->pixel_format); |
| 342 | |
Lloyd Atkinson | 9a67349 | 2016-07-05 11:41:57 -0400 | [diff] [blame] | 343 | num_planes = drm_format_num_planes(mode_cmd->pixel_format); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 344 | hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format); |
| 345 | vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format); |
| 346 | |
Lloyd Atkinson | 9a67349 | 2016-07-05 11:41:57 -0400 | [diff] [blame] | 347 | format = kms->funcs->get_format(kms, mode_cmd->pixel_format, |
| 348 | mode_cmd->modifier, num_planes); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 349 | if (!format) { |
| 350 | dev_err(dev->dev, "unsupported pixel format: %4.4s\n", |
| 351 | (char *)&mode_cmd->pixel_format); |
| 352 | ret = -EINVAL; |
| 353 | goto fail; |
| 354 | } |
| 355 | |
| 356 | msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL); |
| 357 | if (!msm_fb) { |
| 358 | ret = -ENOMEM; |
| 359 | goto fail; |
| 360 | } |
| 361 | |
| 362 | fb = &msm_fb->base; |
| 363 | |
| 364 | msm_fb->format = format; |
Alan Kwong | 578cdaf | 2017-01-28 17:25:43 -0800 | [diff] [blame] | 365 | atomic_set(&msm_fb->kmap_count, 0); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 366 | |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 367 | if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) { |
| 368 | for (i = 0; i < ARRAY_SIZE(mode_cmd->modifier); i++) { |
| 369 | if (mode_cmd->modifier[i]) { |
| 370 | is_modified = true; |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Lloyd Atkinson | 9a67349 | 2016-07-05 11:41:57 -0400 | [diff] [blame] | 376 | if (num_planes > ARRAY_SIZE(msm_fb->planes)) { |
Rob Clark | 10291bf | 2014-11-08 09:20:28 -0500 | [diff] [blame] | 377 | ret = -EINVAL; |
| 378 | goto fail; |
| 379 | } |
| 380 | |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 381 | if (is_modified) { |
| 382 | if (!kms->funcs->check_modified_format) { |
| 383 | dev_err(dev->dev, "can't check modified fb format\n"); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 384 | ret = -EINVAL; |
| 385 | goto fail; |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 386 | } else { |
| 387 | ret = kms->funcs->check_modified_format( |
| 388 | kms, msm_fb->format, mode_cmd, bos); |
| 389 | if (ret) |
| 390 | goto fail; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 391 | } |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 392 | } else { |
| 393 | for (i = 0; i < num_planes; i++) { |
| 394 | unsigned int width = mode_cmd->width / (i ? hsub : 1); |
| 395 | unsigned int height = mode_cmd->height / (i ? vsub : 1); |
| 396 | unsigned int min_size; |
| 397 | unsigned int cpp; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 398 | |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 399 | cpp = drm_format_plane_cpp(mode_cmd->pixel_format, i); |
| 400 | |
| 401 | min_size = (height - 1) * mode_cmd->pitches[i] |
| 402 | + width * cpp |
| 403 | + mode_cmd->offsets[i]; |
| 404 | |
| 405 | if (bos[i]->size < min_size) { |
| 406 | ret = -EINVAL; |
| 407 | goto fail; |
| 408 | } |
| 409 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 410 | } |
| 411 | |
Lloyd Atkinson | 80be567 | 2016-05-25 15:13:23 -0400 | [diff] [blame] | 412 | for (i = 0; i < num_planes; i++) |
| 413 | msm_fb->planes[i] = bos[i]; |
| 414 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 415 | drm_helper_mode_fill_fb_struct(fb, mode_cmd); |
| 416 | |
| 417 | ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs); |
| 418 | if (ret) { |
| 419 | dev_err(dev->dev, "framebuffer init failed: %d\n", ret); |
| 420 | goto fail; |
| 421 | } |
| 422 | |
Lakshmi Narayana Kalavala | 89b6cbe | 2018-05-11 11:28:12 -0700 | [diff] [blame] | 423 | DBG("create: FB ID: %d (%pK)", fb->base.id, fb); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 424 | |
| 425 | return fb; |
| 426 | |
| 427 | fail: |
Stephane Viau | 7194b62 | 2015-05-05 09:47:57 -0400 | [diff] [blame] | 428 | kfree(msm_fb); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 429 | |
| 430 | return ERR_PTR(ret); |
| 431 | } |