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);
56 if (omap_gem_put_paddr(omap_fb->bo)) {
57 dev_err(dev->dev, "could not unmap!\n");
61 drm_gem_object_unreference_unlocked(omap_fb->bo);
67 static int omap_framebuffer_dirty(struct drm_framebuffer *fb,
68 struct drm_file *file_priv, unsigned flags, unsigned color,
69 struct drm_clip_rect *clips, unsigned num_clips)
73 for (i = 0; i < num_clips; i++) {
74 omap_framebuffer_flush(fb, clips[i].x1, clips[i].y1,
75 clips[i].x2 - clips[i].x1,
76 clips[i].y2 - clips[i].y1);
82 static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
83 .create_handle = omap_framebuffer_create_handle,
84 .destroy = omap_framebuffer_destroy,
85 .dirty = omap_framebuffer_dirty,
88 /* returns the buffer size */
89 int omap_framebuffer_get_buffer(struct drm_framebuffer *fb, int x, int y,
90 void **vaddr, dma_addr_t *paddr, unsigned int *screen_width)
92 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
93 int bpp = fb->bits_per_pixel / 8;
96 offset = (x * bpp) + (y * fb->pitch);
99 void *bo_vaddr = omap_gem_vaddr(omap_fb->bo);
100 /* note: we can only count on having a vaddr for buffers that
101 * are allocated physically contiguously to begin with (ie.
102 * dma_alloc_coherent()). But this should be ok because it
103 * is only used by legacy fbdev
106 *vaddr = bo_vaddr + offset;
109 *paddr = omap_fb->paddr + offset;
110 *screen_width = fb->pitch / bpp;
112 return omap_fb->size - offset;
115 struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb)
117 struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
121 /* iterate thru all the connectors, returning ones that are attached
124 struct drm_connector *omap_framebuffer_get_next_connector(
125 struct drm_framebuffer *fb, struct drm_connector *from)
127 struct drm_device *dev = fb->dev;
128 struct list_head *connector_list = &dev->mode_config.connector_list;
129 struct drm_connector *connector = from;
132 return list_first_entry(connector_list, typeof(*from), head);
135 list_for_each_entry_from(connector, connector_list, head) {
136 if (connector != from) {
137 struct drm_encoder *encoder = connector->encoder;
138 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
139 if (crtc && crtc->fb == fb) {
148 /* flush an area of the framebuffer (in case of manual update display that
149 * is not automatically flushed)
151 void omap_framebuffer_flush(struct drm_framebuffer *fb,
152 int x, int y, int w, int h)
154 struct drm_connector *connector = NULL;
156 VERB("flush: %d,%d %dx%d, fb=%p", x, y, w, h, fb);
158 while ((connector = omap_framebuffer_get_next_connector(fb, connector))) {
159 /* only consider connectors that are part of a chain */
160 if (connector->encoder && connector->encoder->crtc) {
161 /* TODO: maybe this should propagate thru the crtc who
162 * could do the coordinate translation..
164 struct drm_crtc *crtc = connector->encoder->crtc;
165 int cx = max(0, x - crtc->x);
166 int cy = max(0, y - crtc->y);
167 int cw = w + (x - crtc->x) - cx;
168 int ch = h + (y - crtc->y) - cy;
170 omap_connector_flush(connector, cx, cy, cw, ch);
175 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
176 struct drm_file *file, struct drm_mode_fb_cmd *mode_cmd)
178 struct drm_gem_object *bo;
179 struct drm_framebuffer *fb;
180 bo = drm_gem_object_lookup(dev, file, mode_cmd->handle);
182 return ERR_PTR(-ENOENT);
184 fb = omap_framebuffer_init(dev, mode_cmd, bo);
186 return ERR_PTR(-ENOMEM);
191 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
192 struct drm_mode_fb_cmd *mode_cmd, struct drm_gem_object *bo)
194 struct omap_framebuffer *omap_fb;
195 struct drm_framebuffer *fb = NULL;
198 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%d)",
199 dev, mode_cmd, mode_cmd->width, mode_cmd->height,
202 /* in case someone tries to feed us a completely bogus stride: */
203 mode_cmd->pitch = align_pitch(mode_cmd->pitch,
204 mode_cmd->width, mode_cmd->bpp);
206 omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
208 dev_err(dev->dev, "could not allocate fb\n");
213 ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
215 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
219 DBG("create: FB ID: %d (%p)", fb->base.id, fb);
221 size = PAGE_ALIGN(mode_cmd->pitch * mode_cmd->height);
224 DBG("using existing %d byte buffer (needed %d)", bo->size, size);
225 if (size > bo->size) {
226 dev_err(dev->dev, "provided buffer object is too small!\n");
230 /* for convenience of all the various callers who don't want
231 * to be bothered to allocate their own buffer..
233 union omap_gem_size gsize = {
236 DBG("allocating %d bytes for fb %d", size, dev->primary->index);
237 bo = omap_gem_new(dev, gsize, OMAP_BO_SCANOUT | OMAP_BO_WC);
239 dev_err(dev->dev, "failed to allocate buffer object\n");
245 omap_fb->size = size;
247 if (omap_gem_get_paddr(bo, &omap_fb->paddr, true)) {
248 dev_err(dev->dev, "could not map (paddr)!\n");
252 drm_helper_mode_fill_fb_struct(fb, mode_cmd);
258 omap_framebuffer_destroy(fb);