OSDN Git Service

drm/omap: pass rotation to dispc
[uclinux-h8/linux.git] / drivers / gpu / drm / omapdrm / omap_fb.c
1 /*
2  * drivers/gpu/drm/omapdrm/omap_fb.c
3  *
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
20 #include <linux/seq_file.h>
21
22 #include <drm/drm_crtc.h>
23 #include <drm/drm_crtc_helper.h>
24
25 #include "omap_dmm_tiler.h"
26 #include "omap_drv.h"
27
28 /*
29  * framebuffer funcs
30  */
31
32 static const u32 formats[] = {
33         /* 16bpp [A]RGB: */
34         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 */
41         /* 24bpp RGB: */
42         DRM_FORMAT_RGB888,   /* RGB24-888 */
43         /* 32bpp [A]RGB: */
44         DRM_FORMAT_RGBX8888, /* RGBx24-8888 */
45         DRM_FORMAT_XRGB8888, /* xRGB24-8888 */
46         DRM_FORMAT_RGBA8888, /* RGBA32-8888 */
47         DRM_FORMAT_ARGB8888, /* ARGB32-8888 */
48         /* YUV: */
49         DRM_FORMAT_NV12,
50         DRM_FORMAT_YUYV,
51         DRM_FORMAT_UYVY,
52 };
53
54 /* per-plane info for the fb: */
55 struct plane {
56         struct drm_gem_object *bo;
57         uint32_t pitch;
58         uint32_t offset;
59         dma_addr_t dma_addr;
60 };
61
62 #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
63
64 struct omap_framebuffer {
65         struct drm_framebuffer base;
66         int pin_count;
67         const struct drm_format_info *format;
68         struct plane planes[2];
69         /* lock for pinning (pin_count and planes.dma_addr) */
70         struct mutex lock;
71 };
72
73 static 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);
78         return drm_gem_handle_create(file_priv,
79                         omap_fb->planes[0].bo, handle);
80 }
81
82 static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
83 {
84         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
85         int i, n = fb->format->num_planes;
86
87         DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
88
89         drm_framebuffer_cleanup(fb);
90
91         for (i = 0; i < n; i++) {
92                 struct plane *plane = &omap_fb->planes[i];
93
94                 drm_gem_object_unreference_unlocked(plane->bo);
95         }
96
97         kfree(omap_fb);
98 }
99
100 static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
101         .create_handle = omap_framebuffer_create_handle,
102         .destroy = omap_framebuffer_destroy,
103 };
104
105 static uint32_t get_linear_addr(struct plane *plane,
106                 const struct drm_format_info *format, int n, int x, int y)
107 {
108         uint32_t offset;
109
110         offset = plane->offset
111                + (x * format->cpp[n] / (n == 0 ? 1 : format->hsub))
112                + (y * plane->pitch / (n == 0 ? 1 : format->vsub));
113
114         return plane->dma_addr + offset;
115 }
116
117 bool 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
125 /* Note: DRM rotates counter-clockwise, TILER & DSS rotates clockwise */
126 static 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
155 /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
156  */
157 void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
158                 struct drm_plane_state *state, struct omap_overlay_info *info)
159 {
160         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
161         const struct drm_format_info *format = omap_fb->format;
162         struct plane *plane = &omap_fb->planes[0];
163         uint32_t x, y, orient = 0;
164
165         info->fourcc = fb->format->format;
166
167         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;
173
174         /* 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;
180
181         if (omap_gem_flags(plane->bo) & OMAP_BO_TILED) {
182                 uint32_t w = state->src_w >> 16;
183                 uint32_t h = state->src_h >> 16;
184
185                 orient = drm_rotation_to_tiler(state->rotation);
186
187                 /* adjust x,y offset for invert: */
188                 if (orient & MASK_Y_INVERT)
189                         y += h - 1;
190                 if (orient & MASK_X_INVERT)
191                         x += w - 1;
192
193                 omap_gem_rotated_dma_addr(plane->bo, orient, x, y,
194                                           &info->paddr);
195                 info->rotation_type = OMAP_DSS_ROT_TILER;
196                 info->rotation = state->rotation ?: DRM_MODE_ROTATE_0;
197                 info->screen_width  = omap_gem_tiled_stride(plane->bo, orient);
198         } else {
199                 switch (state->rotation & DRM_MODE_ROTATE_MASK) {
200                 case 0:
201                 case DRM_MODE_ROTATE_0:
202                         /* OK */
203                         break;
204
205                 default:
206                         dev_warn(fb->dev->dev,
207                                 "rotation '%d' ignored for non-tiled fb\n",
208                                 state->rotation);
209                         break;
210                 }
211
212                 info->paddr         = get_linear_addr(plane, format, 0, x, y);
213                 info->rotation_type = OMAP_DSS_ROT_NONE;
214                 info->rotation      = DRM_MODE_ROTATE_0;
215                 info->screen_width  = plane->pitch;
216         }
217
218         /* convert to pixels: */
219         info->screen_width /= format->cpp[0];
220
221         if (fb->format->format == DRM_FORMAT_NV12) {
222                 plane = &omap_fb->planes[1];
223
224                 if (info->rotation_type == OMAP_DSS_ROT_TILER) {
225                         WARN_ON(!(omap_gem_flags(plane->bo) & OMAP_BO_TILED));
226                         omap_gem_rotated_dma_addr(plane->bo, orient, x/2, y/2,
227                                                   &info->p_uv_addr);
228                 } else {
229                         info->p_uv_addr = get_linear_addr(plane, format, 1, x, y);
230                 }
231         } else {
232                 info->p_uv_addr = 0;
233         }
234 }
235
236 /* pin, prepare for scanout: */
237 int omap_framebuffer_pin(struct drm_framebuffer *fb)
238 {
239         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
240         int ret, i, n = fb->format->num_planes;
241
242         mutex_lock(&omap_fb->lock);
243
244         if (omap_fb->pin_count > 0) {
245                 omap_fb->pin_count++;
246                 mutex_unlock(&omap_fb->lock);
247                 return 0;
248         }
249
250         for (i = 0; i < n; i++) {
251                 struct plane *plane = &omap_fb->planes[i];
252                 ret = omap_gem_pin(plane->bo, &plane->dma_addr);
253                 if (ret)
254                         goto fail;
255                 omap_gem_dma_sync_buffer(plane->bo, DMA_TO_DEVICE);
256         }
257
258         omap_fb->pin_count++;
259
260         mutex_unlock(&omap_fb->lock);
261
262         return 0;
263
264 fail:
265         for (i--; i >= 0; i--) {
266                 struct plane *plane = &omap_fb->planes[i];
267                 omap_gem_unpin(plane->bo);
268                 plane->dma_addr = 0;
269         }
270
271         mutex_unlock(&omap_fb->lock);
272
273         return ret;
274 }
275
276 /* unpin, no longer being scanned out: */
277 void omap_framebuffer_unpin(struct drm_framebuffer *fb)
278 {
279         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
280         int i, n = fb->format->num_planes;
281
282         mutex_lock(&omap_fb->lock);
283
284         omap_fb->pin_count--;
285
286         if (omap_fb->pin_count > 0) {
287                 mutex_unlock(&omap_fb->lock);
288                 return;
289         }
290
291         for (i = 0; i < n; i++) {
292                 struct plane *plane = &omap_fb->planes[i];
293                 omap_gem_unpin(plane->bo);
294                 plane->dma_addr = 0;
295         }
296
297         mutex_unlock(&omap_fb->lock);
298 }
299
300 /* iterate thru all the connectors, returning ones that are attached
301  * to the same fb..
302  */
303 struct drm_connector *omap_framebuffer_get_next_connector(
304                 struct drm_framebuffer *fb, struct drm_connector *from)
305 {
306         struct drm_device *dev = fb->dev;
307         struct list_head *connector_list = &dev->mode_config.connector_list;
308         struct drm_connector *connector = from;
309
310         if (!from)
311                 return list_first_entry_or_null(connector_list, typeof(*from),
312                                                 head);
313
314         list_for_each_entry_from(connector, connector_list, head) {
315                 if (connector != from) {
316                         struct drm_encoder *encoder = connector->encoder;
317                         struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
318                         if (crtc && crtc->primary->fb == fb)
319                                 return connector;
320
321                 }
322         }
323
324         return NULL;
325 }
326
327 #ifdef CONFIG_DEBUG_FS
328 void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
329 {
330         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
331         int i, n = fb->format->num_planes;
332
333         seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
334                         (char *)&fb->format->format);
335
336         for (i = 0; i < n; i++) {
337                 struct plane *plane = &omap_fb->planes[i];
338                 seq_printf(m, "   %d: offset=%d pitch=%d, obj: ",
339                                 i, plane->offset, plane->pitch);
340                 omap_gem_describe(plane->bo, m);
341         }
342 }
343 #endif
344
345 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
346                 struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
347 {
348         unsigned int num_planes = drm_format_num_planes(mode_cmd->pixel_format);
349         struct drm_gem_object *bos[4];
350         struct drm_framebuffer *fb;
351         int i;
352
353         for (i = 0; i < num_planes; i++) {
354                 bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
355                 if (!bos[i]) {
356                         fb = ERR_PTR(-ENOENT);
357                         goto error;
358                 }
359         }
360
361         fb = omap_framebuffer_init(dev, mode_cmd, bos);
362         if (IS_ERR(fb))
363                 goto error;
364
365         return fb;
366
367 error:
368         while (--i > 0)
369                 drm_gem_object_unreference_unlocked(bos[i]);
370
371         return fb;
372 }
373
374 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
375                 const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
376 {
377         const struct drm_format_info *format = NULL;
378         struct omap_framebuffer *omap_fb = NULL;
379         struct drm_framebuffer *fb = NULL;
380         unsigned int pitch = mode_cmd->pitches[0];
381         int ret, i;
382
383         DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
384                         dev, mode_cmd, mode_cmd->width, mode_cmd->height,
385                         (char *)&mode_cmd->pixel_format);
386
387         format = drm_format_info(mode_cmd->pixel_format);
388
389         for (i = 0; i < ARRAY_SIZE(formats); i++) {
390                 if (formats[i] == mode_cmd->pixel_format)
391                         break;
392         }
393
394         if (!format || i == ARRAY_SIZE(formats)) {
395                 dev_dbg(dev->dev, "unsupported pixel format: %4.4s\n",
396                         (char *)&mode_cmd->pixel_format);
397                 ret = -EINVAL;
398                 goto fail;
399         }
400
401         omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
402         if (!omap_fb) {
403                 ret = -ENOMEM;
404                 goto fail;
405         }
406
407         fb = &omap_fb->base;
408         omap_fb->format = format;
409         mutex_init(&omap_fb->lock);
410
411         /*
412          * The code below assumes that no format use more than two planes, and
413          * that the two planes of multiplane formats need the same number of
414          * bytes per pixel.
415          */
416         if (format->num_planes == 2 && pitch != mode_cmd->pitches[1]) {
417                 dev_dbg(dev->dev, "pitches differ between planes 0 and 1\n");
418                 ret = -EINVAL;
419                 goto fail;
420         }
421
422         if (pitch % format->cpp[0]) {
423                 dev_dbg(dev->dev,
424                         "buffer pitch (%u bytes) is not a multiple of pixel size (%u bytes)\n",
425                         pitch, format->cpp[0]);
426                 ret = -EINVAL;
427                 goto fail;
428         }
429
430         for (i = 0; i < format->num_planes; i++) {
431                 struct plane *plane = &omap_fb->planes[i];
432                 unsigned int vsub = i == 0 ? 1 : format->vsub;
433                 unsigned int size;
434
435                 size = pitch * mode_cmd->height / vsub;
436
437                 if (size > omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i]) {
438                         dev_dbg(dev->dev,
439                                 "provided buffer object is too small! %zu < %d\n",
440                                 bos[i]->size - mode_cmd->offsets[i], size);
441                         ret = -EINVAL;
442                         goto fail;
443                 }
444
445                 plane->bo     = bos[i];
446                 plane->offset = mode_cmd->offsets[i];
447                 plane->pitch  = pitch;
448                 plane->dma_addr  = 0;
449         }
450
451         drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
452
453         ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
454         if (ret) {
455                 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
456                 goto fail;
457         }
458
459         DBG("create: FB ID: %d (%p)", fb->base.id, fb);
460
461         return fb;
462
463 fail:
464         kfree(omap_fb);
465
466         return ERR_PTR(ret);
467 }