blob: 4093e0e38a90719c851a62708f174aa8cac63cbb [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Rob Clark8bb0daf2013-02-11 12:43:09 -05002 * drivers/gpu/drm/omapdrm/omap_fb.c
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 *
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
Arnd Bergmann2d802452016-05-11 18:01:45 +020020#include <linux/seq_file.h>
21
Laurent Pinchart2d278f52015-03-05 21:31:37 +020022#include <drm/drm_crtc.h>
23#include <drm/drm_crtc_helper.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060024
Laurent Pinchart2d278f52015-03-05 21:31:37 +020025#include "omap_dmm_tiler.h"
26#include "omap_drv.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060027
Rob Clarkcd5351f2011-11-12 12:09:40 -060028/*
29 * framebuffer funcs
30 */
31
Tomi Valkeinenc2e52e32017-05-04 11:29:58 +030032static const u32 formats[] = {
Rob Clarkae43d7c2012-01-16 12:51:15 -060033 /* 16bpp [A]RGB: */
Tomi Valkeinenc2e52e32017-05-04 11:29:58 +030034 DRM_FORMAT_RGB565, /* RGB16-565 */
35 DRM_FORMAT_RGBX4444, /* RGB12x-4444 */
36 DRM_FORMAT_XRGB4444, /* xRGB12-4444 */
37 DRM_FORMAT_RGBA4444, /* RGBA12-4444 */
38 DRM_FORMAT_ARGB4444, /* ARGB16-4444 */
39 DRM_FORMAT_XRGB1555, /* xRGB15-1555 */
40 DRM_FORMAT_ARGB1555, /* ARGB16-1555 */
Rob Clarkae43d7c2012-01-16 12:51:15 -060041 /* 24bpp RGB: */
Tomi Valkeinenc2e52e32017-05-04 11:29:58 +030042 DRM_FORMAT_RGB888, /* RGB24-888 */
Rob Clarkae43d7c2012-01-16 12:51:15 -060043 /* 32bpp [A]RGB: */
Tomi Valkeinenc2e52e32017-05-04 11:29:58 +030044 DRM_FORMAT_RGBX8888, /* RGBx24-8888 */
45 DRM_FORMAT_XRGB8888, /* xRGB24-8888 */
46 DRM_FORMAT_RGBA8888, /* RGBA32-8888 */
47 DRM_FORMAT_ARGB8888, /* ARGB32-8888 */
Rob Clarkae43d7c2012-01-16 12:51:15 -060048 /* YUV: */
Tomi Valkeinenc2e52e32017-05-04 11:29:58 +030049 DRM_FORMAT_NV12,
50 DRM_FORMAT_YUYV,
51 DRM_FORMAT_UYVY,
Rob Clarkae43d7c2012-01-16 12:51:15 -060052};
53
Rob Clark9a0774e2012-01-16 12:51:17 -060054/* per-plane info for the fb: */
55struct plane {
56 struct drm_gem_object *bo;
57 uint32_t pitch;
58 uint32_t offset;
Laurent Pinchart16869082017-04-21 00:33:51 +030059 dma_addr_t dma_addr;
Rob Clark9a0774e2012-01-16 12:51:17 -060060};
61
Rob Clarkcd5351f2011-11-12 12:09:40 -060062#define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
63
64struct omap_framebuffer {
65 struct drm_framebuffer base;
Tomi Valkeinenf36eb5a2014-09-03 19:25:54 +000066 int pin_count;
Laurent Pinchartc9028b32016-06-07 00:45:35 +030067 const struct drm_format_info *format;
Laurent Pinchart4d20dfc2016-03-26 19:24:01 +020068 struct plane planes[2];
Laurent Pinchart16869082017-04-21 00:33:51 +030069 /* lock for pinning (pin_count and planes.dma_addr) */
Tomi Valkeinenf524ab72015-06-04 10:56:33 +030070 struct mutex lock;
Rob Clarkcd5351f2011-11-12 12:09:40 -060071};
72
73static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
74 struct drm_file *file_priv,
75 unsigned int *handle)
76{
77 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
Rob Clark9a0774e2012-01-16 12:51:17 -060078 return drm_gem_handle_create(file_priv,
79 omap_fb->planes[0].bo, handle);
Rob Clarkcd5351f2011-11-12 12:09:40 -060080}
81
82static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
83{
Rob Clarkcd5351f2011-11-12 12:09:40 -060084 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +020085 int i, n = fb->format->num_planes;
Rob Clarkcd5351f2011-11-12 12:09:40 -060086
87 DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
88
89 drm_framebuffer_cleanup(fb);
90
Rob Clark9a0774e2012-01-16 12:51:17 -060091 for (i = 0; i < n; i++) {
92 struct plane *plane = &omap_fb->planes[i];
Markus Elfring76e4c322016-07-22 08:28:31 +020093
94 drm_gem_object_unreference_unlocked(plane->bo);
Rob Clarkcd5351f2011-11-12 12:09:40 -060095 }
96
97 kfree(omap_fb);
98}
99
Rob Clarkcd5351f2011-11-12 12:09:40 -0600100static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
101 .create_handle = omap_framebuffer_create_handle,
102 .destroy = omap_framebuffer_destroy,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600103};
104
Rob Clark3c810c62012-08-15 15:18:01 -0500105static uint32_t get_linear_addr(struct plane *plane,
Laurent Pinchartc9028b32016-06-07 00:45:35 +0300106 const struct drm_format_info *format, int n, int x, int y)
Rob Clark3c810c62012-08-15 15:18:01 -0500107{
108 uint32_t offset;
109
Laurent Pinchartc9028b32016-06-07 00:45:35 +0300110 offset = plane->offset
111 + (x * format->cpp[n] / (n == 0 ? 1 : format->hsub))
112 + (y * plane->pitch / (n == 0 ? 1 : format->vsub));
Rob Clark3c810c62012-08-15 15:18:01 -0500113
Laurent Pinchart16869082017-04-21 00:33:51 +0300114 return plane->dma_addr + offset;
Rob Clark3c810c62012-08-15 15:18:01 -0500115}
116
Tomi Valkeinenbfeece52015-08-27 13:09:22 +0300117bool omap_framebuffer_supports_rotation(struct drm_framebuffer *fb)
118{
119 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
120 struct plane *plane = &omap_fb->planes[0];
121
122 return omap_gem_flags(plane->bo) & OMAP_BO_TILED;
123}
124
Tomi Valkeinen8958aeb2017-05-15 12:57:46 +0300125/* Note: DRM rotates counter-clockwise, TILER & DSS rotates clockwise */
126static uint32_t drm_rotation_to_tiler(unsigned int drm_rot)
127{
128 uint32_t orient;
129
130 switch (drm_rot & DRM_MODE_ROTATE_MASK) {
131 default:
132 case DRM_MODE_ROTATE_0:
133 orient = 0;
134 break;
135 case DRM_MODE_ROTATE_90:
136 orient = MASK_XY_FLIP | MASK_X_INVERT;
137 break;
138 case DRM_MODE_ROTATE_180:
139 orient = MASK_X_INVERT | MASK_Y_INVERT;
140 break;
141 case DRM_MODE_ROTATE_270:
142 orient = MASK_XY_FLIP | MASK_Y_INVERT;
143 break;
144 }
145
146 if (drm_rot & DRM_MODE_REFLECT_X)
147 orient ^= MASK_X_INVERT;
148
149 if (drm_rot & DRM_MODE_REFLECT_Y)
150 orient ^= MASK_Y_INVERT;
151
152 return orient;
153}
154
Rob Clark9a0774e2012-01-16 12:51:17 -0600155/* update ovl info for scanout, handles cases of multi-planar fb's, etc.
156 */
Rob Clark3c810c62012-08-15 15:18:01 -0500157void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
Tomi Valkeinen218ed532017-05-15 13:40:08 +0300158 struct drm_plane_state *state, struct omap_overlay_info *info)
Rob Clark9a0774e2012-01-16 12:51:17 -0600159{
160 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
Laurent Pinchartc9028b32016-06-07 00:45:35 +0300161 const struct drm_format_info *format = omap_fb->format;
Rob Clark9a0774e2012-01-16 12:51:17 -0600162 struct plane *plane = &omap_fb->planes[0];
Rob Clark3c810c62012-08-15 15:18:01 -0500163 uint32_t x, y, orient = 0;
Rob Clark9a0774e2012-01-16 12:51:17 -0600164
Tomi Valkeinen41aff422017-05-04 11:31:56 +0300165 info->fourcc = fb->format->format;
Rob Clark9a0774e2012-01-16 12:51:17 -0600166
Tomi Valkeinen218ed532017-05-15 13:40:08 +0300167 info->pos_x = state->crtc_x;
168 info->pos_y = state->crtc_y;
169 info->out_width = state->crtc_w;
170 info->out_height = state->crtc_h;
171 info->width = state->src_w >> 16;
172 info->height = state->src_h >> 16;
Rob Clark3c810c62012-08-15 15:18:01 -0500173
Tomi Valkeinen218ed532017-05-15 13:40:08 +0300174 /* DSS driver wants the w & h in rotated orientation */
175 if (drm_rotation_90_or_270(state->rotation))
176 swap(info->width, info->height);
177
178 x = state->src_x >> 16;
179 y = state->src_y >> 16;
Rob Clark3c810c62012-08-15 15:18:01 -0500180
181 if (omap_gem_flags(plane->bo) & OMAP_BO_TILED) {
Tomi Valkeinen218ed532017-05-15 13:40:08 +0300182 uint32_t w = state->src_w >> 16;
183 uint32_t h = state->src_h >> 16;
Rob Clark3c810c62012-08-15 15:18:01 -0500184
Tomi Valkeinen218ed532017-05-15 13:40:08 +0300185 orient = drm_rotation_to_tiler(state->rotation);
Rob Clark3c810c62012-08-15 15:18:01 -0500186
Tomi Valkeinen218ed532017-05-15 13:40:08 +0300187 /* adjust x,y offset for invert: */
Rob Clark3c810c62012-08-15 15:18:01 -0500188 if (orient & MASK_Y_INVERT)
189 y += h - 1;
190 if (orient & MASK_X_INVERT)
191 x += w - 1;
192
Laurent Pinchart16869082017-04-21 00:33:51 +0300193 omap_gem_rotated_dma_addr(plane->bo, orient, x, y,
194 &info->paddr);
Rob Clark3c810c62012-08-15 15:18:01 -0500195 info->rotation_type = OMAP_DSS_ROT_TILER;
196 info->screen_width = omap_gem_tiled_stride(plane->bo, orient);
197 } else {
Tomi Valkeinen218ed532017-05-15 13:40:08 +0300198 switch (state->rotation & DRM_MODE_ROTATE_MASK) {
Tomi Valkeinen5ac96342014-04-08 16:18:41 +0300199 case 0:
Robert Fossc2c446a2017-05-19 16:50:17 -0400200 case DRM_MODE_ROTATE_0:
Tomi Valkeinen5ac96342014-04-08 16:18:41 +0300201 /* OK */
202 break;
203
204 default:
205 dev_warn(fb->dev->dev,
206 "rotation '%d' ignored for non-tiled fb\n",
Tomi Valkeinen218ed532017-05-15 13:40:08 +0300207 state->rotation);
Tomi Valkeinen5ac96342014-04-08 16:18:41 +0300208 break;
209 }
210
Rob Clark3c810c62012-08-15 15:18:01 -0500211 info->paddr = get_linear_addr(plane, format, 0, x, y);
Tomi Valkeinen517a8a952017-05-03 14:14:27 +0300212 info->rotation_type = OMAP_DSS_ROT_NONE;
Rob Clark3c810c62012-08-15 15:18:01 -0500213 info->screen_width = plane->pitch;
214 }
215
216 /* convert to pixels: */
Laurent Pinchartc9028b32016-06-07 00:45:35 +0300217 info->screen_width /= format->cpp[0];
Rob Clark9a0774e2012-01-16 12:51:17 -0600218
Tomi Valkeinenc2e52e32017-05-04 11:29:58 +0300219 if (fb->format->format == DRM_FORMAT_NV12) {
Rob Clark9a0774e2012-01-16 12:51:17 -0600220 plane = &omap_fb->planes[1];
Rob Clark3c810c62012-08-15 15:18:01 -0500221
222 if (info->rotation_type == OMAP_DSS_ROT_TILER) {
223 WARN_ON(!(omap_gem_flags(plane->bo) & OMAP_BO_TILED));
Laurent Pinchart16869082017-04-21 00:33:51 +0300224 omap_gem_rotated_dma_addr(plane->bo, orient, x/2, y/2,
225 &info->p_uv_addr);
Rob Clark3c810c62012-08-15 15:18:01 -0500226 } else {
227 info->p_uv_addr = get_linear_addr(plane, format, 1, x, y);
228 }
Rob Clark9a0774e2012-01-16 12:51:17 -0600229 } else {
230 info->p_uv_addr = 0;
231 }
232}
233
Rob Clark5833bd22013-08-07 13:41:21 -0400234/* pin, prepare for scanout: */
235int omap_framebuffer_pin(struct drm_framebuffer *fb)
Rob Clarkb33f34d2012-03-05 10:48:35 -0600236{
Rob Clark5833bd22013-08-07 13:41:21 -0400237 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200238 int ret, i, n = fb->format->num_planes;
Rob Clarkb33f34d2012-03-05 10:48:35 -0600239
Tomi Valkeinenf524ab72015-06-04 10:56:33 +0300240 mutex_lock(&omap_fb->lock);
241
Tomi Valkeinenf36eb5a2014-09-03 19:25:54 +0000242 if (omap_fb->pin_count > 0) {
243 omap_fb->pin_count++;
Tomi Valkeinenf524ab72015-06-04 10:56:33 +0300244 mutex_unlock(&omap_fb->lock);
Tomi Valkeinenf36eb5a2014-09-03 19:25:54 +0000245 return 0;
246 }
247
Rob Clark5833bd22013-08-07 13:41:21 -0400248 for (i = 0; i < n; i++) {
249 struct plane *plane = &omap_fb->planes[i];
Laurent Pinchartbc20c852017-04-21 00:33:52 +0300250 ret = omap_gem_pin(plane->bo, &plane->dma_addr);
Rob Clark5833bd22013-08-07 13:41:21 -0400251 if (ret)
252 goto fail;
Laurent Pinchartd61ce7d2017-04-21 00:33:55 +0300253 omap_gem_dma_sync_buffer(plane->bo, DMA_TO_DEVICE);
Rob Clarkb33f34d2012-03-05 10:48:35 -0600254 }
255
Tomi Valkeinenf36eb5a2014-09-03 19:25:54 +0000256 omap_fb->pin_count++;
257
Tomi Valkeinenf524ab72015-06-04 10:56:33 +0300258 mutex_unlock(&omap_fb->lock);
259
Rob Clark5833bd22013-08-07 13:41:21 -0400260 return 0;
261
262fail:
263 for (i--; i >= 0; i--) {
264 struct plane *plane = &omap_fb->planes[i];
Laurent Pinchartbc20c852017-04-21 00:33:52 +0300265 omap_gem_unpin(plane->bo);
Laurent Pinchart16869082017-04-21 00:33:51 +0300266 plane->dma_addr = 0;
Rob Clarkb33f34d2012-03-05 10:48:35 -0600267 }
268
Tomi Valkeinenf524ab72015-06-04 10:56:33 +0300269 mutex_unlock(&omap_fb->lock);
270
Rob Clarkb33f34d2012-03-05 10:48:35 -0600271 return ret;
272}
273
Rob Clark5833bd22013-08-07 13:41:21 -0400274/* unpin, no longer being scanned out: */
Tomi Valkeinen9c368502015-04-28 14:01:35 +0300275void omap_framebuffer_unpin(struct drm_framebuffer *fb)
Rob Clark5833bd22013-08-07 13:41:21 -0400276{
277 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200278 int i, n = fb->format->num_planes;
Rob Clark5833bd22013-08-07 13:41:21 -0400279
Tomi Valkeinenf524ab72015-06-04 10:56:33 +0300280 mutex_lock(&omap_fb->lock);
281
Tomi Valkeinenf36eb5a2014-09-03 19:25:54 +0000282 omap_fb->pin_count--;
283
Tomi Valkeinenf524ab72015-06-04 10:56:33 +0300284 if (omap_fb->pin_count > 0) {
285 mutex_unlock(&omap_fb->lock);
Tomi Valkeinen9c368502015-04-28 14:01:35 +0300286 return;
Tomi Valkeinenf524ab72015-06-04 10:56:33 +0300287 }
Tomi Valkeinenf36eb5a2014-09-03 19:25:54 +0000288
Rob Clark5833bd22013-08-07 13:41:21 -0400289 for (i = 0; i < n; i++) {
290 struct plane *plane = &omap_fb->planes[i];
Laurent Pinchartbc20c852017-04-21 00:33:52 +0300291 omap_gem_unpin(plane->bo);
Laurent Pinchart16869082017-04-21 00:33:51 +0300292 plane->dma_addr = 0;
Rob Clark5833bd22013-08-07 13:41:21 -0400293 }
294
Tomi Valkeinenf524ab72015-06-04 10:56:33 +0300295 mutex_unlock(&omap_fb->lock);
Rob Clark5833bd22013-08-07 13:41:21 -0400296}
297
Rob Clarkcd5351f2011-11-12 12:09:40 -0600298/* iterate thru all the connectors, returning ones that are attached
299 * to the same fb..
300 */
301struct drm_connector *omap_framebuffer_get_next_connector(
302 struct drm_framebuffer *fb, struct drm_connector *from)
303{
304 struct drm_device *dev = fb->dev;
305 struct list_head *connector_list = &dev->mode_config.connector_list;
306 struct drm_connector *connector = from;
307
YAMANE Toshiakiddcd49e2012-11-14 19:32:17 +0900308 if (!from)
Laurent Pinchart06fb2202013-12-24 12:58:01 +0100309 return list_first_entry_or_null(connector_list, typeof(*from),
310 head);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600311
312 list_for_each_entry_from(connector, connector_list, head) {
313 if (connector != from) {
314 struct drm_encoder *encoder = connector->encoder;
315 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
Matt Roperf4510a22014-04-01 15:22:40 -0700316 if (crtc && crtc->primary->fb == fb)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600317 return connector;
YAMANE Toshiakiddcd49e2012-11-14 19:32:17 +0900318
Rob Clarkcd5351f2011-11-12 12:09:40 -0600319 }
320 }
321
322 return NULL;
323}
324
Rob Clarkf6b60362012-03-05 10:48:36 -0600325#ifdef CONFIG_DEBUG_FS
326void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
327{
328 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200329 int i, n = fb->format->num_planes;
Rob Clarkf6b60362012-03-05 10:48:36 -0600330
331 seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200332 (char *)&fb->format->format);
Rob Clarkf6b60362012-03-05 10:48:36 -0600333
334 for (i = 0; i < n; i++) {
335 struct plane *plane = &omap_fb->planes[i];
336 seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
337 i, plane->offset, plane->pitch);
338 omap_gem_describe(plane->bo, m);
339 }
340}
341#endif
342
Rob Clarkcd5351f2011-11-12 12:09:40 -0600343struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200344 struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600345{
Laurent Pincharta39c94e2016-03-26 19:51:57 +0200346 unsigned int num_planes = drm_format_num_planes(mode_cmd->pixel_format);
Rob Clarkae43d7c2012-01-16 12:51:15 -0600347 struct drm_gem_object *bos[4];
Rob Clarkcd5351f2011-11-12 12:09:40 -0600348 struct drm_framebuffer *fb;
Laurent Pincharta39c94e2016-03-26 19:51:57 +0200349 int i;
Rob Clarkae43d7c2012-01-16 12:51:15 -0600350
Laurent Pincharta39c94e2016-03-26 19:51:57 +0200351 for (i = 0; i < num_planes; i++) {
352 bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
353 if (!bos[i]) {
354 fb = ERR_PTR(-ENOENT);
355 goto error;
356 }
357 }
Rob Clarkae43d7c2012-01-16 12:51:15 -0600358
359 fb = omap_framebuffer_init(dev, mode_cmd, bos);
Laurent Pincharta39c94e2016-03-26 19:51:57 +0200360 if (IS_ERR(fb))
361 goto error;
362
363 return fb;
364
365error:
366 while (--i > 0)
367 drm_gem_object_unreference_unlocked(bos[i]);
368
Rob Clarkcd5351f2011-11-12 12:09:40 -0600369 return fb;
370}
371
372struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200373 const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600374{
Laurent Pinchartc9028b32016-06-07 00:45:35 +0300375 const struct drm_format_info *format = NULL;
Tomi Valkeinen925e4942014-09-25 19:24:27 +0000376 struct omap_framebuffer *omap_fb = NULL;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600377 struct drm_framebuffer *fb = NULL;
Laurent Pinchart6941e3d2016-03-26 20:01:13 +0200378 unsigned int pitch = mode_cmd->pitches[0];
Laurent Pinchartc9028b32016-06-07 00:45:35 +0300379 int ret, i;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600380
Rob Clarkae43d7c2012-01-16 12:51:15 -0600381 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
Rob Clarkcd5351f2011-11-12 12:09:40 -0600382 dev, mode_cmd, mode_cmd->width, mode_cmd->height,
Rob Clarkae43d7c2012-01-16 12:51:15 -0600383 (char *)&mode_cmd->pixel_format);
384
Laurent Pinchartc9028b32016-06-07 00:45:35 +0300385 format = drm_format_info(mode_cmd->pixel_format);
386
Rob Clarkae43d7c2012-01-16 12:51:15 -0600387 for (i = 0; i < ARRAY_SIZE(formats); i++) {
Tomi Valkeinenc2e52e32017-05-04 11:29:58 +0300388 if (formats[i] == mode_cmd->pixel_format)
Rob Clarkae43d7c2012-01-16 12:51:15 -0600389 break;
Rob Clarkae43d7c2012-01-16 12:51:15 -0600390 }
391
Tomi Valkeinenc2e52e32017-05-04 11:29:58 +0300392 if (!format || i == ARRAY_SIZE(formats)) {
Laurent Pincharta078a3d2016-03-26 20:02:49 +0200393 dev_dbg(dev->dev, "unsupported pixel format: %4.4s\n",
394 (char *)&mode_cmd->pixel_format);
Rob Clarkae43d7c2012-01-16 12:51:15 -0600395 ret = -EINVAL;
396 goto fail;
397 }
Rob Clarkcd5351f2011-11-12 12:09:40 -0600398
Rob Clarkcd5351f2011-11-12 12:09:40 -0600399 omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
400 if (!omap_fb) {
Rob Clarkae43d7c2012-01-16 12:51:15 -0600401 ret = -ENOMEM;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600402 goto fail;
403 }
404
405 fb = &omap_fb->base;
Rob Clark9a0774e2012-01-16 12:51:17 -0600406 omap_fb->format = format;
Tomi Valkeinenf524ab72015-06-04 10:56:33 +0300407 mutex_init(&omap_fb->lock);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600408
Laurent Pinchart6941e3d2016-03-26 20:01:13 +0200409 /*
410 * The code below assumes that no format use more than two planes, and
411 * that the two planes of multiplane formats need the same number of
412 * bytes per pixel.
413 */
414 if (format->num_planes == 2 && pitch != mode_cmd->pitches[1]) {
Laurent Pincharta078a3d2016-03-26 20:02:49 +0200415 dev_dbg(dev->dev, "pitches differ between planes 0 and 1\n");
Laurent Pinchart6941e3d2016-03-26 20:01:13 +0200416 ret = -EINVAL;
417 goto fail;
418 }
419
420 if (pitch % format->cpp[0]) {
Laurent Pincharta078a3d2016-03-26 20:02:49 +0200421 dev_dbg(dev->dev,
Laurent Pinchart6941e3d2016-03-26 20:01:13 +0200422 "buffer pitch (%u bytes) is not a multiple of pixel size (%u bytes)\n",
423 pitch, format->cpp[0]);
424 ret = -EINVAL;
425 goto fail;
426 }
427
Laurent Pinchartc9028b32016-06-07 00:45:35 +0300428 for (i = 0; i < format->num_planes; i++) {
Rob Clark9a0774e2012-01-16 12:51:17 -0600429 struct plane *plane = &omap_fb->planes[i];
Laurent Pinchartc9028b32016-06-07 00:45:35 +0300430 unsigned int vsub = i == 0 ? 1 : format->vsub;
431 unsigned int size;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600432
Laurent Pinchartc9028b32016-06-07 00:45:35 +0300433 size = pitch * mode_cmd->height / vsub;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600434
Laurent Pinchart6941e3d2016-03-26 20:01:13 +0200435 if (size > omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i]) {
Laurent Pincharta078a3d2016-03-26 20:02:49 +0200436 dev_dbg(dev->dev,
Tomi Valkeinen2150c192017-02-21 09:57:12 +0200437 "provided buffer object is too small! %zu < %d\n",
Laurent Pinchart6941e3d2016-03-26 20:01:13 +0200438 bos[i]->size - mode_cmd->offsets[i], size);
Tomi Valkeinenbe4f2352016-01-08 15:51:02 +0200439 ret = -EINVAL;
440 goto fail;
441 }
442
Rob Clark9a0774e2012-01-16 12:51:17 -0600443 plane->bo = bos[i];
444 plane->offset = mode_cmd->offsets[i];
Rob Clark9f18c952012-03-05 10:48:34 -0600445 plane->pitch = pitch;
Laurent Pinchart16869082017-04-21 00:33:51 +0300446 plane->dma_addr = 0;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600447 }
448
Ville Syrjäläa3f913c2016-12-14 22:48:59 +0200449 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600450
Daniel Vetterc7d73f62012-12-13 23:38:38 +0100451 ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
452 if (ret) {
453 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
454 goto fail;
455 }
456
457 DBG("create: FB ID: %d (%p)", fb->base.id, fb);
458
Rob Clarkcd5351f2011-11-12 12:09:40 -0600459 return fb;
460
461fail:
Tomi Valkeinen925e4942014-09-25 19:24:27 +0000462 kfree(omap_fb);
YAMANE Toshiakiddcd49e2012-11-14 19:32:17 +0900463
Rob Clarkae43d7c2012-01-16 12:51:15 -0600464 return ERR_PTR(ret);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600465}