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
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/>.
23 #include "drm_crtc_helper.h"
30 #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
32 struct omap_framebuffer {
33 struct drm_framebuffer base;
34 struct drm_gem_object *bo;
39 static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
40 struct drm_file *file_priv,
43 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
44 return drm_gem_handle_create(file_priv, omap_fb->bo, handle);
47 static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
49 struct drm_device *dev = fb->dev;
50 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
52 DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
54 drm_framebuffer_cleanup(fb);
57 if (omap_fb->paddr && omap_gem_put_paddr(omap_fb->bo))
58 dev_err(dev->dev, "could not unmap!\n");
59 drm_gem_object_unreference_unlocked(omap_fb->bo);
65 static int omap_framebuffer_dirty(struct drm_framebuffer *fb,
66 struct drm_file *file_priv, unsigned flags, unsigned color,
67 struct drm_clip_rect *clips, unsigned num_clips)
71 for (i = 0; i < num_clips; i++) {
72 omap_framebuffer_flush(fb, clips[i].x1, clips[i].y1,
73 clips[i].x2 - clips[i].x1,
74 clips[i].y2 - clips[i].y1);
80 static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
81 .create_handle = omap_framebuffer_create_handle,
82 .destroy = omap_framebuffer_destroy,
83 .dirty = omap_framebuffer_dirty,
86 /* returns the buffer size */
87 int omap_framebuffer_get_buffer(struct drm_framebuffer *fb, int x, int y,
88 void **vaddr, dma_addr_t *paddr, unsigned int *screen_width)
90 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
91 int bpp = fb->bits_per_pixel / 8;
94 offset = (x * bpp) + (y * fb->pitch);
97 void *bo_vaddr = omap_gem_vaddr(omap_fb->bo);
98 /* note: we can only count on having a vaddr for buffers that
99 * are allocated physically contiguously to begin with (ie.
100 * dma_alloc_coherent()). But this should be ok because it
101 * is only used by legacy fbdev
103 BUG_ON(IS_ERR_OR_NULL(bo_vaddr));
104 *vaddr = bo_vaddr + offset;
107 *paddr = omap_fb->paddr + offset;
108 *screen_width = fb->pitch / bpp;
110 return omap_fb->size - offset;
113 struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb)
115 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
119 /* iterate thru all the connectors, returning ones that are attached
122 struct drm_connector *omap_framebuffer_get_next_connector(
123 struct drm_framebuffer *fb, struct drm_connector *from)
125 struct drm_device *dev = fb->dev;
126 struct list_head *connector_list = &dev->mode_config.connector_list;
127 struct drm_connector *connector = from;
130 return list_first_entry(connector_list, typeof(*from), head);
133 list_for_each_entry_from(connector, connector_list, head) {
134 if (connector != from) {
135 struct drm_encoder *encoder = connector->encoder;
136 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
137 if (crtc && crtc->fb == fb) {
146 /* flush an area of the framebuffer (in case of manual update display that
147 * is not automatically flushed)
149 void omap_framebuffer_flush(struct drm_framebuffer *fb,
150 int x, int y, int w, int h)
152 struct drm_connector *connector = NULL;
154 VERB("flush: %d,%d %dx%d, fb=%p", x, y, w, h, fb);
156 while ((connector = omap_framebuffer_get_next_connector(fb, connector))) {
157 /* only consider connectors that are part of a chain */
158 if (connector->encoder && connector->encoder->crtc) {
159 /* TODO: maybe this should propagate thru the crtc who
160 * could do the coordinate translation..
162 struct drm_crtc *crtc = connector->encoder->crtc;
163 int cx = max(0, x - crtc->x);
164 int cy = max(0, y - crtc->y);
165 int cw = w + (x - crtc->x) - cx;
166 int ch = h + (y - crtc->y) - cy;
168 omap_connector_flush(connector, cx, cy, cw, ch);
173 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
174 struct drm_file *file, struct drm_mode_fb_cmd *mode_cmd)
176 struct drm_gem_object *bo;
177 struct drm_framebuffer *fb;
178 bo = drm_gem_object_lookup(dev, file, mode_cmd->handle);
180 return ERR_PTR(-ENOENT);
182 fb = omap_framebuffer_init(dev, mode_cmd, bo);
184 return ERR_PTR(-ENOMEM);
189 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
190 struct drm_mode_fb_cmd *mode_cmd, struct drm_gem_object *bo)
192 struct omap_framebuffer *omap_fb;
193 struct drm_framebuffer *fb = NULL;
196 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%d)",
197 dev, mode_cmd, mode_cmd->width, mode_cmd->height,
200 /* in case someone tries to feed us a completely bogus stride: */
201 mode_cmd->pitch = align_pitch(mode_cmd->pitch,
202 mode_cmd->width, mode_cmd->bpp);
204 omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
206 dev_err(dev->dev, "could not allocate fb\n");
211 ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
213 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
217 DBG("create: FB ID: %d (%p)", fb->base.id, fb);
219 size = PAGE_ALIGN(mode_cmd->pitch * mode_cmd->height);
221 if (size > bo->size) {
222 dev_err(dev->dev, "provided buffer object is too small!\n");
227 omap_fb->size = size;
229 if (omap_gem_get_paddr(bo, &omap_fb->paddr, true)) {
230 dev_err(dev->dev, "could not map (paddr)!\n");
234 drm_helper_mode_fill_fb_struct(fb, mode_cmd);
240 omap_framebuffer_destroy(fb);