Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / staging / omapdrm / omap_fb.c
1 /*
2  * drivers/staging/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 "omap_drv.h"
21
22 #include "drm_crtc.h"
23 #include "drm_crtc_helper.h"
24
25
26 /*
27  * framebuffer funcs
28  */
29
30 #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
31
32 struct omap_framebuffer {
33         struct drm_framebuffer base;
34         struct drm_gem_object *bo;
35         int size;
36         dma_addr_t paddr;
37 };
38
39 static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
40                 struct drm_file *file_priv,
41                 unsigned int *handle)
42 {
43         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
44     return drm_gem_handle_create(file_priv, omap_fb->bo, handle);
45 }
46
47 static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
48 {
49         struct drm_device *dev = fb->dev;
50         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
51
52         DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
53
54         drm_framebuffer_cleanup(fb);
55
56         if (omap_gem_put_paddr(omap_fb->bo)) {
57                 dev_err(dev->dev, "could not unmap!\n");
58         }
59
60         if (omap_fb->bo) {
61                 drm_gem_object_unreference_unlocked(omap_fb->bo);
62         }
63
64         kfree(omap_fb);
65 }
66
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)
70 {
71         int i;
72
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);
77         }
78
79         return 0;
80 }
81
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,
86 };
87
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)
91 {
92         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
93         int bpp = fb->bits_per_pixel / 8;
94         unsigned long offset;
95
96         offset = (x * bpp) + (y * fb->pitch);
97
98         if (vaddr) {
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
104                  */
105                 BUG_ON(!bo_vaddr);
106                 *vaddr = bo_vaddr + offset;
107         }
108
109         *paddr = omap_fb->paddr + offset;
110         *screen_width = fb->pitch / bpp;
111
112         return omap_fb->size - offset;
113 }
114
115 struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb)
116 {
117         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
118         return omap_fb->bo;
119 }
120
121 /* iterate thru all the connectors, returning ones that are attached
122  * to the same fb..
123  */
124 struct drm_connector *omap_framebuffer_get_next_connector(
125                 struct drm_framebuffer *fb, struct drm_connector *from)
126 {
127         struct drm_device *dev = fb->dev;
128         struct list_head *connector_list = &dev->mode_config.connector_list;
129         struct drm_connector *connector = from;
130
131         if (!from) {
132                 return list_first_entry(connector_list, typeof(*from), head);
133         }
134
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) {
140                                 return connector;
141                         }
142                 }
143         }
144
145         return NULL;
146 }
147
148 /* flush an area of the framebuffer (in case of manual update display that
149  * is not automatically flushed)
150  */
151 void omap_framebuffer_flush(struct drm_framebuffer *fb,
152                 int x, int y, int w, int h)
153 {
154         struct drm_connector *connector = NULL;
155
156         VERB("flush: %d,%d %dx%d, fb=%p", x, y, w, h, fb);
157
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..
163                          */
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;
169
170                         omap_connector_flush(connector, cx, cy, cw, ch);
171                 }
172         }
173 }
174
175 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
176                 struct drm_file *file, struct drm_mode_fb_cmd *mode_cmd)
177 {
178         struct drm_gem_object *bo;
179         struct drm_framebuffer *fb;
180         bo = drm_gem_object_lookup(dev, file, mode_cmd->handle);
181         if (!bo) {
182                 return ERR_PTR(-ENOENT);
183         }
184         fb = omap_framebuffer_init(dev, mode_cmd, bo);
185         if (!fb) {
186                 return ERR_PTR(-ENOMEM);
187         }
188         return fb;
189 }
190
191 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
192                 struct drm_mode_fb_cmd *mode_cmd, struct drm_gem_object *bo)
193 {
194         struct omap_framebuffer *omap_fb;
195         struct drm_framebuffer *fb = NULL;
196         int size, ret;
197
198         DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%d)",
199                         dev, mode_cmd, mode_cmd->width, mode_cmd->height,
200                         mode_cmd->bpp);
201
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);
205
206         omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
207         if (!omap_fb) {
208                 dev_err(dev->dev, "could not allocate fb\n");
209                 goto fail;
210         }
211
212         fb = &omap_fb->base;
213         ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
214         if (ret) {
215                 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
216                 goto fail;
217         }
218
219         DBG("create: FB ID: %d (%p)", fb->base.id, fb);
220
221         size = PAGE_ALIGN(mode_cmd->pitch * mode_cmd->height);
222
223         if (bo) {
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");
227                         goto fail;
228                 }
229         } else {
230                 /* for convenience of all the various callers who don't want
231                  * to be bothered to allocate their own buffer..
232                  */
233                 union omap_gem_size gsize = {
234                                 .bytes = size,
235                 };
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);
238                 if (!bo) {
239                         dev_err(dev->dev, "failed to allocate buffer object\n");
240                         goto fail;
241                 }
242         }
243
244         omap_fb->bo = bo;
245         omap_fb->size = size;
246
247         if (omap_gem_get_paddr(bo, &omap_fb->paddr, true)) {
248                 dev_err(dev->dev, "could not map (paddr)!\n");
249                 goto fail;
250         }
251
252         drm_helper_mode_fill_fb_struct(fb, mode_cmd);
253
254         return fb;
255
256 fail:
257         if (fb) {
258                 omap_framebuffer_destroy(fb);
259         }
260         return NULL;
261 }