memcg: always create memsw files if CONFIG_CGROUP_MEM_RES_CTLR_SWAP
[linux-2.6.git] / drivers / staging / omapdrm / omap_fb.c
blob04b235b6724abc12cb79d06d14d51bfe0ae2b526
1 /*
2 * drivers/staging/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
14 * more details.
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 "omap_drv.h"
22 #include "drm_crtc.h"
23 #include "drm_crtc_helper.h"
26 * framebuffer funcs
29 /* per-format info: */
30 struct format {
31 enum omap_color_mode dss_format;
32 uint32_t pixel_format;
33 struct {
34 int stride_bpp; /* this times width is stride */
35 int sub_y; /* sub-sample in y dimension */
36 } planes[4];
37 bool yuv;
40 static const struct format formats[] = {
41 /* 16bpp [A]RGB: */
42 { OMAP_DSS_COLOR_RGB16, DRM_FORMAT_RGB565, {{2, 1}}, false }, /* RGB16-565 */
43 { OMAP_DSS_COLOR_RGB12U, DRM_FORMAT_RGBX4444, {{2, 1}}, false }, /* RGB12x-4444 */
44 { OMAP_DSS_COLOR_RGBX16, DRM_FORMAT_XRGB4444, {{2, 1}}, false }, /* xRGB12-4444 */
45 { OMAP_DSS_COLOR_RGBA16, DRM_FORMAT_RGBA4444, {{2, 1}}, false }, /* RGBA12-4444 */
46 { OMAP_DSS_COLOR_ARGB16, DRM_FORMAT_ARGB4444, {{2, 1}}, false }, /* ARGB16-4444 */
47 { OMAP_DSS_COLOR_XRGB16_1555, DRM_FORMAT_XRGB1555, {{2, 1}}, false }, /* xRGB15-1555 */
48 { OMAP_DSS_COLOR_ARGB16_1555, DRM_FORMAT_ARGB1555, {{2, 1}}, false }, /* ARGB16-1555 */
49 /* 24bpp RGB: */
50 { OMAP_DSS_COLOR_RGB24P, DRM_FORMAT_RGB888, {{3, 1}}, false }, /* RGB24-888 */
51 /* 32bpp [A]RGB: */
52 { OMAP_DSS_COLOR_RGBX32, DRM_FORMAT_RGBX8888, {{4, 1}}, false }, /* RGBx24-8888 */
53 { OMAP_DSS_COLOR_RGB24U, DRM_FORMAT_XRGB8888, {{4, 1}}, false }, /* xRGB24-8888 */
54 { OMAP_DSS_COLOR_RGBA32, DRM_FORMAT_RGBA8888, {{4, 1}}, false }, /* RGBA32-8888 */
55 { OMAP_DSS_COLOR_ARGB32, DRM_FORMAT_ARGB8888, {{4, 1}}, false }, /* ARGB32-8888 */
56 /* YUV: */
57 { OMAP_DSS_COLOR_NV12, DRM_FORMAT_NV12, {{1, 1}, {1, 2}}, true },
58 { OMAP_DSS_COLOR_YUV2, DRM_FORMAT_YUYV, {{2, 1}}, true },
59 { OMAP_DSS_COLOR_UYVY, DRM_FORMAT_UYVY, {{2, 1}}, true },
62 /* convert from overlay's pixel formats bitmask to an array of fourcc's */
63 uint32_t omap_framebuffer_get_formats(uint32_t *pixel_formats,
64 uint32_t max_formats, enum omap_color_mode supported_modes)
66 uint32_t nformats = 0;
67 int i = 0;
69 for (i = 0; i < ARRAY_SIZE(formats) && nformats < max_formats; i++)
70 if (formats[i].dss_format & supported_modes)
71 pixel_formats[nformats++] = formats[i].pixel_format;
73 return nformats;
76 /* per-plane info for the fb: */
77 struct plane {
78 struct drm_gem_object *bo;
79 uint32_t pitch;
80 uint32_t offset;
81 dma_addr_t paddr;
84 #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
86 struct omap_framebuffer {
87 struct drm_framebuffer base;
88 const struct format *format;
89 struct plane planes[4];
92 static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
93 struct drm_file *file_priv,
94 unsigned int *handle)
96 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
97 return drm_gem_handle_create(file_priv,
98 omap_fb->planes[0].bo, handle);
101 static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
103 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
104 int i, n = drm_format_num_planes(fb->pixel_format);
106 DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
108 drm_framebuffer_cleanup(fb);
110 for (i = 0; i < n; i++) {
111 struct plane *plane = &omap_fb->planes[i];
112 if (plane->bo)
113 drm_gem_object_unreference_unlocked(plane->bo);
116 kfree(omap_fb);
119 static int omap_framebuffer_dirty(struct drm_framebuffer *fb,
120 struct drm_file *file_priv, unsigned flags, unsigned color,
121 struct drm_clip_rect *clips, unsigned num_clips)
123 int i;
125 for (i = 0; i < num_clips; i++) {
126 omap_framebuffer_flush(fb, clips[i].x1, clips[i].y1,
127 clips[i].x2 - clips[i].x1,
128 clips[i].y2 - clips[i].y1);
131 return 0;
134 static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
135 .create_handle = omap_framebuffer_create_handle,
136 .destroy = omap_framebuffer_destroy,
137 .dirty = omap_framebuffer_dirty,
140 /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
142 void omap_framebuffer_update_scanout(struct drm_framebuffer *fb, int x, int y,
143 struct omap_overlay_info *info)
145 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
146 const struct format *format = omap_fb->format;
147 struct plane *plane = &omap_fb->planes[0];
148 unsigned int offset;
150 offset = plane->offset +
151 (x * format->planes[0].stride_bpp) +
152 (y * plane->pitch / format->planes[0].sub_y);
154 info->color_mode = format->dss_format;
155 info->paddr = plane->paddr + offset;
156 info->screen_width = plane->pitch / format->planes[0].stride_bpp;
158 if (format->dss_format == OMAP_DSS_COLOR_NV12) {
159 plane = &omap_fb->planes[1];
160 offset = plane->offset +
161 (x * format->planes[1].stride_bpp) +
162 (y * plane->pitch / format->planes[1].sub_y);
163 info->p_uv_addr = plane->paddr + offset;
164 } else {
165 info->p_uv_addr = 0;
169 /* Call for unpin 'a' (if not NULL), and pin 'b' (if not NULL). Although
170 * buffers to unpin are just just pushed to the unpin fifo so that the
171 * caller can defer unpin until vblank.
173 * Note if this fails (ie. something went very wrong!), all buffers are
174 * unpinned, and the caller disables the overlay. We could have tried
175 * to revert back to the previous set of pinned buffers but if things are
176 * hosed there is no guarantee that would succeed.
178 int omap_framebuffer_replace(struct drm_framebuffer *a,
179 struct drm_framebuffer *b, void *arg,
180 void (*unpin)(void *arg, struct drm_gem_object *bo))
182 int ret = 0, i, na, nb;
183 struct omap_framebuffer *ofba = to_omap_framebuffer(a);
184 struct omap_framebuffer *ofbb = to_omap_framebuffer(b);
186 na = a ? drm_format_num_planes(a->pixel_format) : 0;
187 nb = b ? drm_format_num_planes(b->pixel_format) : 0;
189 for (i = 0; i < max(na, nb); i++) {
190 struct plane *pa, *pb;
192 pa = (i < na) ? &ofba->planes[i] : NULL;
193 pb = (i < nb) ? &ofbb->planes[i] : NULL;
195 if (pa) {
196 unpin(arg, pa->bo);
197 pa->paddr = 0;
200 if (pb && !ret)
201 ret = omap_gem_get_paddr(pb->bo, &pb->paddr, true);
204 if (ret) {
205 /* something went wrong.. unpin what has been pinned */
206 for (i = 0; i < nb; i++) {
207 struct plane *pb = &ofba->planes[i];
208 if (pb->paddr) {
209 unpin(arg, pb->bo);
210 pb->paddr = 0;
215 return ret;
218 struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb, int p)
220 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
221 if (p >= drm_format_num_planes(fb->pixel_format))
222 return NULL;
223 return omap_fb->planes[p].bo;
226 /* iterate thru all the connectors, returning ones that are attached
227 * to the same fb..
229 struct drm_connector *omap_framebuffer_get_next_connector(
230 struct drm_framebuffer *fb, struct drm_connector *from)
232 struct drm_device *dev = fb->dev;
233 struct list_head *connector_list = &dev->mode_config.connector_list;
234 struct drm_connector *connector = from;
236 if (!from) {
237 return list_first_entry(connector_list, typeof(*from), head);
240 list_for_each_entry_from(connector, connector_list, head) {
241 if (connector != from) {
242 struct drm_encoder *encoder = connector->encoder;
243 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
244 if (crtc && crtc->fb == fb) {
245 return connector;
250 return NULL;
253 /* flush an area of the framebuffer (in case of manual update display that
254 * is not automatically flushed)
256 void omap_framebuffer_flush(struct drm_framebuffer *fb,
257 int x, int y, int w, int h)
259 struct drm_connector *connector = NULL;
261 VERB("flush: %d,%d %dx%d, fb=%p", x, y, w, h, fb);
263 while ((connector = omap_framebuffer_get_next_connector(fb, connector))) {
264 /* only consider connectors that are part of a chain */
265 if (connector->encoder && connector->encoder->crtc) {
266 /* TODO: maybe this should propagate thru the crtc who
267 * could do the coordinate translation..
269 struct drm_crtc *crtc = connector->encoder->crtc;
270 int cx = max(0, x - crtc->x);
271 int cy = max(0, y - crtc->y);
272 int cw = w + (x - crtc->x) - cx;
273 int ch = h + (y - crtc->y) - cy;
275 omap_connector_flush(connector, cx, cy, cw, ch);
280 #ifdef CONFIG_DEBUG_FS
281 void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
283 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
284 int i, n = drm_format_num_planes(fb->pixel_format);
286 seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
287 (char *)&fb->pixel_format);
289 for (i = 0; i < n; i++) {
290 struct plane *plane = &omap_fb->planes[i];
291 seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
292 i, plane->offset, plane->pitch);
293 omap_gem_describe(plane->bo, m);
296 #endif
298 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
299 struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
301 struct drm_gem_object *bos[4];
302 struct drm_framebuffer *fb;
303 int ret;
305 ret = objects_lookup(dev, file, mode_cmd->pixel_format,
306 bos, mode_cmd->handles);
307 if (ret)
308 return ERR_PTR(ret);
310 fb = omap_framebuffer_init(dev, mode_cmd, bos);
311 if (IS_ERR(fb)) {
312 int i, n = drm_format_num_planes(mode_cmd->pixel_format);
313 for (i = 0; i < n; i++)
314 drm_gem_object_unreference_unlocked(bos[i]);
315 return fb;
317 return fb;
320 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
321 struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
323 struct omap_framebuffer *omap_fb;
324 struct drm_framebuffer *fb = NULL;
325 const struct format *format = NULL;
326 int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
328 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
329 dev, mode_cmd, mode_cmd->width, mode_cmd->height,
330 (char *)&mode_cmd->pixel_format);
332 for (i = 0; i < ARRAY_SIZE(formats); i++) {
333 if (formats[i].pixel_format == mode_cmd->pixel_format) {
334 format = &formats[i];
335 break;
339 if (!format) {
340 dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
341 (char *)&mode_cmd->pixel_format);
342 ret = -EINVAL;
343 goto fail;
346 omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
347 if (!omap_fb) {
348 dev_err(dev->dev, "could not allocate fb\n");
349 ret = -ENOMEM;
350 goto fail;
353 fb = &omap_fb->base;
354 ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
355 if (ret) {
356 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
357 goto fail;
360 DBG("create: FB ID: %d (%p)", fb->base.id, fb);
362 omap_fb->format = format;
364 for (i = 0; i < n; i++) {
365 struct plane *plane = &omap_fb->planes[i];
366 int size, pitch = mode_cmd->pitches[i];
368 if (pitch < (mode_cmd->width * format->planes[i].stride_bpp)) {
369 dev_err(dev->dev, "provided buffer pitch is too small! %d < %d\n",
370 pitch, mode_cmd->width * format->planes[i].stride_bpp);
371 ret = -EINVAL;
372 goto fail;
375 size = pitch * mode_cmd->height / format->planes[i].sub_y;
377 if (size > (bos[i]->size - mode_cmd->offsets[i])) {
378 dev_err(dev->dev, "provided buffer object is too small! %d < %d\n",
379 bos[i]->size - mode_cmd->offsets[i], size);
380 ret = -EINVAL;
381 goto fail;
384 plane->bo = bos[i];
385 plane->offset = mode_cmd->offsets[i];
386 plane->pitch = pitch;
387 plane->paddr = 0;
390 drm_helper_mode_fill_fb_struct(fb, mode_cmd);
392 return fb;
394 fail:
395 if (fb) {
396 omap_framebuffer_destroy(fb);
398 return ERR_PTR(ret);