a8fba712e0e9d4543881d96952bd873451204050
[profile/ivi/libdrm.git] / linux-core / radeon_ms_fb.c
1 /*
2  * Copyright © 2007 David Airlie
3  * Copyright © 2007 Jerome Glisse
4  *
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation on the rights to use, copy, modify, merge,
11  * publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so,
13  * subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial
17  * portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NON-INFRINGEMENT.  IN NO EVENT SHALL ATI, VA LINUX SYSTEMS AND/OR
23  * THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  */
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/string.h>
32 #include <linux/mm.h>
33 #include <linux/tty.h>
34 #include <linux/slab.h>
35 #include <linux/delay.h>
36 #include <linux/fb.h>
37 #include <linux/init.h>
38
39 #include "drmP.h"
40 #include "drm.h"
41 #include "drm_crtc.h"
42 #include "radeon_ms.h"
43
44 struct radeonfb_par {
45         struct drm_device       *dev;
46         struct drm_crtc         *crtc;
47         struct drm_display_mode *fb_mode;
48 };
49
50 static int radeonfb_setcolreg(unsigned regno, unsigned red,
51                                unsigned green, unsigned blue,
52                                unsigned transp, struct fb_info *info)
53 {
54         struct radeonfb_par *par = info->par;
55         struct drm_framebuffer *fb = par->crtc->fb;
56         struct drm_crtc *crtc = par->crtc;
57
58         if (regno > 255) {
59                 return 1;
60         }
61         if (crtc->funcs->gamma_set) {
62                 crtc->funcs->gamma_set(crtc, red, green, blue, regno);
63         }
64         if (regno < 16) {
65                 switch (fb->depth) {
66                 case 15:
67                         fb->pseudo_palette[regno] = ((red & 0xf800) >>  1) |
68                                 ((green & 0xf800) >>  6) |
69                                 ((blue & 0xf800) >> 11);
70                         break;
71                 case 16:
72                         fb->pseudo_palette[regno] = (red & 0xf800) |
73                                 ((green & 0xfc00) >>  5) |
74                                 ((blue  & 0xf800) >> 11);
75                         break;
76                 case 24:
77                 case 32:
78                         fb->pseudo_palette[regno] = ((red & 0xff00) << 8) |
79                                 (green & 0xff00) |
80                                 ((blue  & 0xff00) >> 8);
81                         break;
82                 }
83         }
84         return 0;
85 }
86
87 static int radeonfb_check_var(struct fb_var_screeninfo *var,
88                                struct fb_info *info)
89 {
90         struct radeonfb_par *par = info->par;
91         struct drm_framebuffer *fb = par->crtc->fb;
92
93         if (!var->pixclock)
94                 return -EINVAL;
95
96         /* Need to resize the fb object !!! */
97         if (var->xres > fb->width || var->yres > fb->height) {
98                 DRM_ERROR("Requested width/height is greater than "
99                           "current fb object %dx%d > %dx%d\n",
100                           var->xres, var->yres, fb->width, fb->height);
101                 DRM_ERROR("Need resizing code.\n");
102                 return -EINVAL;
103         }
104
105         switch (var->bits_per_pixel) {
106         case 16:
107                 if (var->green.length == 5) {
108                         var->red.offset = 10;
109                         var->green.offset = 5;
110                         var->blue.offset = 0;
111                         var->red.length = 5;
112                         var->green.length = 5;
113                         var->blue.length = 5;
114                         var->transp.length = 0;
115                         var->transp.offset = 0;
116                 } else {
117                         var->red.offset = 11;
118                         var->green.offset = 6;
119                         var->blue.offset = 0;
120                         var->red.length = 5;
121                         var->green.length = 6;
122                         var->blue.length = 5;
123                         var->transp.length = 0;
124                         var->transp.offset = 0;
125                 }
126                 break;
127         case 32:
128                 if (var->transp.length) {
129                         var->red.offset = 16;
130                         var->green.offset = 8;
131                         var->blue.offset = 0;
132                         var->red.length = 8;
133                         var->green.length = 8;
134                         var->blue.length = 8;
135                         var->transp.length = 8;
136                         var->transp.offset = 24;
137                 } else {
138                         var->red.offset = 16;
139                         var->green.offset = 8;
140                         var->blue.offset = 0;
141                         var->red.length = 8;
142                         var->green.length = 8;
143                         var->blue.length = 8;
144                         var->transp.length = 0;
145                         var->transp.offset = 0;
146                 }
147                 break;
148         default:
149                 return -EINVAL; 
150         }
151         return 0;
152 }
153
154 static bool radeonfb_mode_equal(struct drm_display_mode *mode1,
155                                 struct drm_display_mode *mode2)
156 {
157         if (mode1->hdisplay == mode2->hdisplay &&
158             mode1->hsync_start == mode2->hsync_start &&
159             mode1->hsync_end == mode2->hsync_end &&
160             mode1->htotal == mode2->htotal &&
161             mode1->hskew == mode2->hskew &&
162             mode1->vdisplay == mode2->vdisplay &&
163             mode1->vsync_start == mode2->vsync_start &&
164             mode1->vsync_end == mode2->vsync_end &&
165             mode1->vtotal == mode2->vtotal &&
166             mode1->vscan == mode2->vscan &&
167             mode1->flags == mode2->flags) {
168                 /* FIXME: what about adding a margin for clock ? */
169                 if (mode1->clock == mode2->clock)
170                         return true;
171                 return false;
172         }
173         
174         return false;
175 }
176
177 static int radeonfb_set_par(struct fb_info *info)
178 {
179         struct radeonfb_par *par = info->par;
180         struct drm_framebuffer *fb = par->crtc->fb;
181         struct drm_device *dev = par->dev;
182         struct drm_display_mode *drm_mode, *search_mode;
183         struct drm_output *output;
184         struct fb_var_screeninfo *var = &info->var;
185         int found = 0;
186
187         switch (var->bits_per_pixel) {
188         case 16:
189                 fb->depth = (var->green.length == 6) ? 16 : 15;
190                 break;
191         case 32:
192                 fb->depth = (var->transp.length > 0) ? 32 : 24;
193                 break;
194         default:
195                 return -EINVAL; 
196         }
197         fb->bits_per_pixel = var->bits_per_pixel;
198
199         info->fix.line_length = fb->pitch;
200         info->fix.smem_len = info->fix.line_length * fb->height;
201         info->fix.visual = FB_VISUAL_TRUECOLOR;
202         info->screen_size = info->fix.smem_len; /* ??? */
203
204         /* Should we walk the output's modelist or just create our own ???
205          * For now, we create and destroy a mode based on the incoming 
206          * parameters. But there's commented out code below which scans 
207          * the output list too.
208          */
209         drm_mode = drm_mode_create(dev);
210         drm_mode->hdisplay = var->xres;
211         drm_mode->hsync_start = drm_mode->hdisplay + var->right_margin;
212         drm_mode->hsync_end = drm_mode->hsync_start + var->hsync_len;
213         drm_mode->htotal = drm_mode->hsync_end + var->left_margin;
214         drm_mode->vdisplay = var->yres;
215         drm_mode->vsync_start = drm_mode->vdisplay + var->lower_margin;
216         drm_mode->vsync_end = drm_mode->vsync_start + var->vsync_len;
217         drm_mode->vtotal = drm_mode->vsync_end + var->upper_margin;
218         drm_mode->clock = PICOS2KHZ(var->pixclock);
219         drm_mode->vrefresh = drm_mode_vrefresh(drm_mode);
220         drm_mode_set_name(drm_mode);
221         drm_mode_set_crtcinfo(drm_mode, CRTC_INTERLACE_HALVE_V);
222
223         list_for_each_entry(output, &dev->mode_config.output_list, head) {
224                 if (output->crtc == par->crtc)
225                         break;
226         }
227
228         drm_mode_debug_printmodeline(dev, drm_mode);    
229         list_for_each_entry(search_mode, &output->modes, head) {
230                 drm_mode_debug_printmodeline(dev, search_mode);
231                 if (radeonfb_mode_equal(drm_mode, search_mode)) {
232                         drm_mode_destroy(dev, drm_mode);
233                         drm_mode = search_mode;
234                         found = 1;
235                         break;
236                 }
237         }
238
239         if (!found) {
240                 if (par->fb_mode) {
241                         drm_mode_detachmode_crtc(dev, par->fb_mode);
242                 }
243                 par->fb_mode = drm_mode;
244                 drm_mode_debug_printmodeline(dev, drm_mode);
245                 /* attach mode */
246                 drm_mode_attachmode_crtc(dev, par->crtc, par->fb_mode);
247         }
248
249         if (par->crtc->enabled) {
250                 if (!drm_mode_equal(&par->crtc->mode, drm_mode)) {
251                         if (!drm_crtc_set_mode(par->crtc, drm_mode, 0, 0)) {
252                                 return -EINVAL;
253                         }
254                 }
255         }
256
257         return 0;
258 }
259
260 static struct fb_ops radeonfb_ops = {
261         .owner = THIS_MODULE,
262         //      .fb_open = radeonfb_open,
263         //      .fb_read = radeonfb_read,
264         //      .fb_write = radeonfb_write,
265         //      .fb_release = radeonfb_release,
266         //      .fb_ioctl = radeonfb_ioctl,
267         .fb_check_var = radeonfb_check_var,
268         .fb_set_par = radeonfb_set_par,
269         .fb_setcolreg = radeonfb_setcolreg,
270         .fb_fillrect = cfb_fillrect,
271         .fb_copyarea = cfb_copyarea,
272         .fb_imageblit = cfb_imageblit,
273 };
274
275 int radeonfb_probe(struct drm_device *dev, struct drm_crtc *crtc)
276 {
277         struct fb_info *info;
278         struct radeonfb_par *par;
279         struct device *device = &dev->pdev->dev; 
280         struct drm_framebuffer *fb;
281         struct drm_display_mode *mode = crtc->desired_mode;
282         int ret;
283
284         info = framebuffer_alloc(sizeof(struct radeonfb_par), device);
285         if (!info){
286                 DRM_INFO("[radeon_ms] framebuffer_alloc failed\n");
287                 return -EINVAL;
288         }
289
290         fb = drm_framebuffer_create(dev);
291         if (!fb) {
292                 framebuffer_release(info);
293                 DRM_ERROR("[radeon_ms] failed to allocate fb.\n");
294                 return -EINVAL;
295         }
296         crtc->fb = fb;
297
298         fb->width = crtc->desired_mode->hdisplay;
299         fb->height = crtc->desired_mode->vdisplay;
300         fb->bits_per_pixel = 32;
301         fb->pitch = fb->width * ((fb->bits_per_pixel + 1) / 8);
302         fb->depth = 24;
303         /* one page alignment should be fine for constraint (micro|macro tiling,
304          * bit depth, color buffer offset, ...) */
305         ret = drm_buffer_object_create(dev, fb->width * fb->height * 4, 
306                                        drm_bo_type_kernel,
307                                        DRM_BO_FLAG_READ |
308                                        DRM_BO_FLAG_WRITE |
309                                        DRM_BO_FLAG_NO_EVICT |
310                                        DRM_BO_FLAG_MEM_VRAM,
311                                        DRM_BO_HINT_DONT_FENCE,
312                                        1,
313                                        0,
314                                        &fb->bo);
315         if (ret || fb->bo == NULL) {
316                 DRM_ERROR("[radeon_ms] failed to allocate framebuffer\n");
317                 drm_framebuffer_destroy(fb);
318                 framebuffer_release(info);
319                 return -EINVAL;
320         }
321
322         DRM_INFO("[radeon_ms] framebuffer %dx%d at 0x%08lX\n",
323                  fb->width, fb->height, fb->bo->offset);
324
325         fb->fbdev = info;
326         par = info->par;
327         par->dev = dev;
328         par->crtc = crtc;
329         info->fbops = &radeonfb_ops;
330         strcpy(info->fix.id, "radeonfb");
331         info->fix.type = FB_TYPE_PACKED_PIXELS;
332         info->fix.visual = FB_VISUAL_TRUECOLOR;
333         info->fix.type_aux = 0;
334         info->fix.xpanstep = 8;
335         info->fix.ypanstep = 1;
336         info->fix.ywrapstep = 0;
337         info->fix.accel = FB_ACCEL_ATI_RADEON;
338         info->fix.type_aux = 0;
339         info->fix.mmio_start = 0;
340         info->fix.mmio_len = 0;
341         info->fix.line_length = fb->pitch;
342         info->fix.smem_start = fb->bo->offset + dev->mode_config.fb_base;
343         info->fix.smem_len = info->fix.line_length * fb->height;
344         info->flags = FBINFO_DEFAULT;
345         DRM_INFO("[radeon_ms] fb physical start : 0x%lX\n", info->fix.smem_start);
346         DRM_INFO("[radeon_ms] fb physical size  : %d\n", info->fix.smem_len);
347
348         ret = drm_bo_kmap(fb->bo, 0, fb->bo->num_pages, &fb->kmap);
349         if (ret) {
350                 DRM_ERROR("error mapping fb: %d\n", ret);
351         }
352         info->screen_base = fb->kmap.virtual;
353         info->screen_size = info->fix.smem_len; /* FIXME */
354         info->pseudo_palette = fb->pseudo_palette;
355         info->var.xres_virtual = fb->width;
356         info->var.yres_virtual = fb->height;
357         info->var.bits_per_pixel = fb->bits_per_pixel;
358         info->var.xoffset = 0;
359         info->var.yoffset = 0;
360         info->var.activate = FB_ACTIVATE_NOW;
361         info->var.height = -1;
362         info->var.width = -1;
363         info->var.vmode = FB_VMODE_NONINTERLACED;
364
365         info->var.xres = mode->hdisplay;
366         info->var.right_margin = mode->hsync_start - mode->hdisplay;
367         info->var.hsync_len = mode->hsync_end - mode->hsync_start;
368         info->var.left_margin = mode->htotal - mode->hsync_end;
369         info->var.yres = mode->vdisplay;
370         info->var.lower_margin = mode->vsync_start - mode->vdisplay;
371         info->var.vsync_len = mode->vsync_end - mode->vsync_start;
372         info->var.upper_margin = mode->vtotal - mode->vsync_end;
373         info->var.pixclock = 10000000 / mode->htotal * 1000 /
374                 mode->vtotal * 100;
375         /* avoid overflow */
376         info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh;
377
378         info->pixmap.size = 64*1024;
379         info->pixmap.buf_align = 8;
380         info->pixmap.access_align = 32;
381         info->pixmap.flags = FB_PIXMAP_SYSTEM;
382         info->pixmap.scan_align = 1;
383
384         DRM_DEBUG("fb depth is %d\n", fb->depth);
385         DRM_DEBUG("   pitch is %d\n", fb->pitch);
386         switch(fb->depth) {
387         case 15:
388                 info->var.red.offset = 10;
389                 info->var.green.offset = 5;
390                 info->var.blue.offset = 0;
391                 info->var.red.length = info->var.green.length =
392                         info->var.blue.length = 5;
393                 info->var.transp.offset = 15;
394                 info->var.transp.length = 1;
395                 break;
396         case 16:
397                 info->var.red.offset = 11;
398                 info->var.green.offset = 5;
399                 info->var.blue.offset = 0;
400                 info->var.red.length = 5;
401                 info->var.green.length = 6;
402                 info->var.blue.length = 5;
403                 info->var.transp.offset = 0;
404                 break;
405         case 24:
406                 info->var.red.offset = 16;
407                 info->var.green.offset = 8;
408                 info->var.blue.offset = 0;
409                 info->var.red.length = info->var.green.length =
410                         info->var.blue.length = 8;
411                 info->var.transp.offset = 0;
412                 info->var.transp.length = 0;
413                 break;
414         case 32:
415                 info->var.red.offset = 16;
416                 info->var.green.offset = 8;
417                 info->var.blue.offset = 0;
418                 info->var.red.length = info->var.green.length =
419                         info->var.blue.length = 8;
420                 info->var.transp.offset = 24;
421                 info->var.transp.length = 8;
422                 break;
423         default:
424                 DRM_ERROR("only support 15, 16, 24 or 32bits per pixel "
425                           "got %d\n", fb->depth);
426                 return -EINVAL;
427                 break;
428         }
429
430         if (register_framebuffer(info) < 0) {
431                 return -EINVAL;
432         }
433
434         DRM_INFO("[radeon_ms] fb%d: %s frame buffer device\n", info->node,
435                  info->fix.id);
436         return 0;
437 }
438 EXPORT_SYMBOL(radeonfb_probe);
439
440 int radeonfb_remove(struct drm_device *dev, struct drm_crtc *crtc)
441 {
442         struct drm_framebuffer *fb = crtc->fb;
443         struct fb_info *info = fb->fbdev;
444         
445         if (info) {
446                 unregister_framebuffer(info);
447                 drm_bo_kunmap(&fb->kmap);
448                 drm_bo_usage_deref_unlocked(&fb->bo);
449                 drm_framebuffer_destroy(fb);
450                 framebuffer_release(info);
451         }
452         return 0;
453 }
454 EXPORT_SYMBOL(radeonfb_remove);
455 MODULE_LICENSE("GPL");