0b50c5b3b56465ab384fd2e4af88f81190176d45
[linux-2.6.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_fb->bo) {
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);
60         }
61
62         kfree(omap_fb);
63 }
64
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)
68 {
69         int i;
70
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);
75         }
76
77         return 0;
78 }
79
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,
84 };
85
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)
89 {
90         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
91         int bpp = fb->bits_per_pixel / 8;
92         unsigned long offset;
93
94         offset = (x * bpp) + (y * fb->pitch);
95
96         if (vaddr) {
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
102                  */
103                 BUG_ON(IS_ERR_OR_NULL(bo_vaddr));
104                 *vaddr = bo_vaddr + offset;
105         }
106
107         *paddr = omap_fb->paddr + offset;
108         *screen_width = fb->pitch / bpp;
109
110         return omap_fb->size - offset;
111 }
112
113 struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb)
114 {
115         struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
116         return omap_fb->bo;
117 }
118
119 /* iterate thru all the connectors, returning ones that are attached
120  * to the same fb..
121  */
122 struct drm_connector *omap_framebuffer_get_next_connector(
123                 struct drm_framebuffer *fb, struct drm_connector *from)
124 {
125         struct drm_device *dev = fb->dev;
126         struct list_head *connector_list = &dev->mode_config.connector_list;
127         struct drm_connector *connector = from;
128
129         if (!from) {
130                 return list_first_entry(connector_list, typeof(*from), head);
131         }
132
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) {
138                                 return connector;
139                         }
140                 }
141         }
142
143         return NULL;
144 }
145
146 /* flush an area of the framebuffer (in case of manual update display that
147  * is not automatically flushed)
148  */
149 void omap_framebuffer_flush(struct drm_framebuffer *fb,
150                 int x, int y, int w, int h)
151 {
152         struct drm_connector *connector = NULL;
153
154         VERB("flush: %d,%d %dx%d, fb=%p", x, y, w, h, fb);
155
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..
161                          */
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;
167
168                         omap_connector_flush(connector, cx, cy, cw, ch);
169                 }
170         }
171 }
172
173 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
174                 struct drm_file *file, struct drm_mode_fb_cmd *mode_cmd)
175 {
176         struct drm_gem_object *bo;
177         struct drm_framebuffer *fb;
178         bo = drm_gem_object_lookup(dev, file, mode_cmd->handle);
179         if (!bo) {
180                 return ERR_PTR(-ENOENT);
181         }
182         fb = omap_framebuffer_init(dev, mode_cmd, bo);
183         if (!fb) {
184                 return ERR_PTR(-ENOMEM);
185         }
186         return fb;
187 }
188
189 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
190                 struct drm_mode_fb_cmd *mode_cmd, struct drm_gem_object *bo)
191 {
192         struct omap_framebuffer *omap_fb;
193         struct drm_framebuffer *fb = NULL;
194         int size, ret;
195
196         DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%d)",
197                         dev, mode_cmd, mode_cmd->width, mode_cmd->height,
198                         mode_cmd->bpp);
199
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);
203
204         omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
205         if (!omap_fb) {
206                 dev_err(dev->dev, "could not allocate fb\n");
207                 goto fail;
208         }
209
210         fb = &omap_fb->base;
211         ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
212         if (ret) {
213                 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
214                 goto fail;
215         }
216
217         DBG("create: FB ID: %d (%p)", fb->base.id, fb);
218
219         size = PAGE_ALIGN(mode_cmd->pitch * mode_cmd->height);
220
221         if (size > bo->size) {
222                 dev_err(dev->dev, "provided buffer object is too small!\n");
223                 goto fail;
224         }
225
226         omap_fb->bo = bo;
227         omap_fb->size = size;
228
229         if (omap_gem_get_paddr(bo, &omap_fb->paddr, true)) {
230                 dev_err(dev->dev, "could not map (paddr)!\n");
231                 goto fail;
232         }
233
234         drm_helper_mode_fill_fb_struct(fb, mode_cmd);
235
236         return fb;
237
238 fail:
239         if (fb) {
240                 omap_framebuffer_destroy(fb);
241         }
242         return NULL;
243 }