2 * drivers/gpu/drm/omapdrm/omap_fb.c
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
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.
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
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/>.
20 #include <linux/seq_file.h>
22 #include <drm/drm_crtc.h>
23 #include <drm/drm_crtc_helper.h>
25 #include "omap_dmm_tiler.h"
32 static const u32 formats[] = {
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 */
42 DRM_FORMAT_RGB888, /* RGB24-888 */
44 DRM_FORMAT_RGBX8888, /* RGBx24-8888 */
45 DRM_FORMAT_XRGB8888, /* xRGB24-8888 */
46 DRM_FORMAT_RGBA8888, /* RGBA32-8888 */
47 DRM_FORMAT_ARGB8888, /* ARGB32-8888 */
54 /* per-plane info for the fb: */
56 struct drm_gem_object *bo;
62 #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
64 struct omap_framebuffer {
65 struct drm_framebuffer base;
67 const struct drm_format_info *format;
68 struct plane planes[2];
69 /* lock for pinning (pin_count and planes.dma_addr) */
73 static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
74 struct drm_file *file_priv,
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);
82 static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
84 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
85 int i, n = fb->format->num_planes;
87 DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
89 drm_framebuffer_cleanup(fb);
91 for (i = 0; i < n; i++) {
92 struct plane *plane = &omap_fb->planes[i];
94 drm_gem_object_unreference_unlocked(plane->bo);
100 static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
101 .create_handle = omap_framebuffer_create_handle,
102 .destroy = omap_framebuffer_destroy,
105 static uint32_t get_linear_addr(struct plane *plane,
106 const struct drm_format_info *format, int n, int x, int y)
110 offset = plane->offset
111 + (x * format->cpp[n] / (n == 0 ? 1 : format->hsub))
112 + (y * plane->pitch / (n == 0 ? 1 : format->vsub));
114 return plane->dma_addr + offset;
117 bool omap_framebuffer_supports_rotation(struct drm_framebuffer *fb)
119 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
120 struct plane *plane = &omap_fb->planes[0];
122 return omap_gem_flags(plane->bo) & OMAP_BO_TILED;
125 /* Note: DRM rotates counter-clockwise, TILER & DSS rotates clockwise */
126 static uint32_t drm_rotation_to_tiler(unsigned int drm_rot)
130 switch (drm_rot & DRM_MODE_ROTATE_MASK) {
132 case DRM_MODE_ROTATE_0:
135 case DRM_MODE_ROTATE_90:
136 orient = MASK_XY_FLIP | MASK_X_INVERT;
138 case DRM_MODE_ROTATE_180:
139 orient = MASK_X_INVERT | MASK_Y_INVERT;
141 case DRM_MODE_ROTATE_270:
142 orient = MASK_XY_FLIP | MASK_Y_INVERT;
146 if (drm_rot & DRM_MODE_REFLECT_X)
147 orient ^= MASK_X_INVERT;
149 if (drm_rot & DRM_MODE_REFLECT_Y)
150 orient ^= MASK_Y_INVERT;
155 /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
157 void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
158 struct drm_plane_state *state, struct omap_overlay_info *info)
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;
165 info->fourcc = fb->format->format;
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;
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);
178 x = state->src_x >> 16;
179 y = state->src_y >> 16;
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;
185 orient = drm_rotation_to_tiler(state->rotation);
187 /* adjust x,y offset for invert: */
188 if (orient & MASK_Y_INVERT)
190 if (orient & MASK_X_INVERT)
193 omap_gem_rotated_dma_addr(plane->bo, orient, x, y,
195 info->rotation_type = OMAP_DSS_ROT_TILER;
196 info->screen_width = omap_gem_tiled_stride(plane->bo, orient);
198 switch (state->rotation & DRM_MODE_ROTATE_MASK) {
200 case DRM_MODE_ROTATE_0:
205 dev_warn(fb->dev->dev,
206 "rotation '%d' ignored for non-tiled fb\n",
211 info->paddr = get_linear_addr(plane, format, 0, x, y);
212 info->rotation_type = OMAP_DSS_ROT_NONE;
213 info->screen_width = plane->pitch;
216 /* convert to pixels: */
217 info->screen_width /= format->cpp[0];
219 if (fb->format->format == DRM_FORMAT_NV12) {
220 plane = &omap_fb->planes[1];
222 if (info->rotation_type == OMAP_DSS_ROT_TILER) {
223 WARN_ON(!(omap_gem_flags(plane->bo) & OMAP_BO_TILED));
224 omap_gem_rotated_dma_addr(plane->bo, orient, x/2, y/2,
227 info->p_uv_addr = get_linear_addr(plane, format, 1, x, y);
234 /* pin, prepare for scanout: */
235 int omap_framebuffer_pin(struct drm_framebuffer *fb)
237 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
238 int ret, i, n = fb->format->num_planes;
240 mutex_lock(&omap_fb->lock);
242 if (omap_fb->pin_count > 0) {
243 omap_fb->pin_count++;
244 mutex_unlock(&omap_fb->lock);
248 for (i = 0; i < n; i++) {
249 struct plane *plane = &omap_fb->planes[i];
250 ret = omap_gem_pin(plane->bo, &plane->dma_addr);
253 omap_gem_dma_sync_buffer(plane->bo, DMA_TO_DEVICE);
256 omap_fb->pin_count++;
258 mutex_unlock(&omap_fb->lock);
263 for (i--; i >= 0; i--) {
264 struct plane *plane = &omap_fb->planes[i];
265 omap_gem_unpin(plane->bo);
269 mutex_unlock(&omap_fb->lock);
274 /* unpin, no longer being scanned out: */
275 void omap_framebuffer_unpin(struct drm_framebuffer *fb)
277 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
278 int i, n = fb->format->num_planes;
280 mutex_lock(&omap_fb->lock);
282 omap_fb->pin_count--;
284 if (omap_fb->pin_count > 0) {
285 mutex_unlock(&omap_fb->lock);
289 for (i = 0; i < n; i++) {
290 struct plane *plane = &omap_fb->planes[i];
291 omap_gem_unpin(plane->bo);
295 mutex_unlock(&omap_fb->lock);
298 /* iterate thru all the connectors, returning ones that are attached
301 struct drm_connector *omap_framebuffer_get_next_connector(
302 struct drm_framebuffer *fb, struct drm_connector *from)
304 struct drm_device *dev = fb->dev;
305 struct list_head *connector_list = &dev->mode_config.connector_list;
306 struct drm_connector *connector = from;
309 return list_first_entry_or_null(connector_list, typeof(*from),
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;
316 if (crtc && crtc->primary->fb == fb)
325 #ifdef CONFIG_DEBUG_FS
326 void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
328 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
329 int i, n = fb->format->num_planes;
331 seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
332 (char *)&fb->format->format);
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);
343 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
344 struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
346 unsigned int num_planes = drm_format_num_planes(mode_cmd->pixel_format);
347 struct drm_gem_object *bos[4];
348 struct drm_framebuffer *fb;
351 for (i = 0; i < num_planes; i++) {
352 bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
354 fb = ERR_PTR(-ENOENT);
359 fb = omap_framebuffer_init(dev, mode_cmd, bos);
367 drm_gem_object_unreference_unlocked(bos[i]);
372 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
373 const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
375 const struct drm_format_info *format = NULL;
376 struct omap_framebuffer *omap_fb = NULL;
377 struct drm_framebuffer *fb = NULL;
378 unsigned int pitch = mode_cmd->pitches[0];
381 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
382 dev, mode_cmd, mode_cmd->width, mode_cmd->height,
383 (char *)&mode_cmd->pixel_format);
385 format = drm_format_info(mode_cmd->pixel_format);
387 for (i = 0; i < ARRAY_SIZE(formats); i++) {
388 if (formats[i] == mode_cmd->pixel_format)
392 if (!format || i == ARRAY_SIZE(formats)) {
393 dev_dbg(dev->dev, "unsupported pixel format: %4.4s\n",
394 (char *)&mode_cmd->pixel_format);
399 omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
406 omap_fb->format = format;
407 mutex_init(&omap_fb->lock);
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
414 if (format->num_planes == 2 && pitch != mode_cmd->pitches[1]) {
415 dev_dbg(dev->dev, "pitches differ between planes 0 and 1\n");
420 if (pitch % format->cpp[0]) {
422 "buffer pitch (%u bytes) is not a multiple of pixel size (%u bytes)\n",
423 pitch, format->cpp[0]);
428 for (i = 0; i < format->num_planes; i++) {
429 struct plane *plane = &omap_fb->planes[i];
430 unsigned int vsub = i == 0 ? 1 : format->vsub;
433 size = pitch * mode_cmd->height / vsub;
435 if (size > omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i]) {
437 "provided buffer object is too small! %zu < %d\n",
438 bos[i]->size - mode_cmd->offsets[i], size);
444 plane->offset = mode_cmd->offsets[i];
445 plane->pitch = pitch;
449 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
451 ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
453 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
457 DBG("create: FB ID: %d (%p)", fb->base.id, fb);