8b720f88803136dadedcd6940c575b4e3881ee0f
[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 #include "amd.h"
44
45
46 static int radeonfb_setcolreg(unsigned regno, unsigned red,
47                                unsigned green, unsigned blue,
48                                unsigned transp, struct fb_info *info)
49 {
50         struct amd_fb *par = info->par;
51         struct drm_framebuffer *fb = par->fb;
52         struct drm_crtc *crtc = par->crtc;
53
54         if (regno > 255) {
55                 return 1;
56         }
57         if (crtc->funcs->gamma_set) {
58                 crtc->funcs->gamma_set(crtc, red, green, blue, regno);
59         }
60         if (regno < 16) {
61                 switch (fb->depth) {
62                 case 15:
63                         fb->pseudo_palette[regno] = ((red & 0xf800) >>  1) |
64                                 ((green & 0xf800) >>  6) |
65                                 ((blue & 0xf800) >> 11);
66                         break;
67                 case 16:
68                         fb->pseudo_palette[regno] = (red & 0xf800) |
69                                 ((green & 0xfc00) >>  5) |
70                                 ((blue  & 0xf800) >> 11);
71                         break;
72                 case 24:
73                 case 32:
74                         fb->pseudo_palette[regno] = ((red & 0xff00) << 8) |
75                                 (green & 0xff00) |
76                                 ((blue  & 0xff00) >> 8);
77                         break;
78                 }
79         }
80         return 0;
81 }
82
83 static int radeonfb_check_var(struct fb_var_screeninfo *var,
84                                struct fb_info *info)
85 {
86         struct amd_fb *par = info->par;
87         struct drm_framebuffer *fb = par->fb;
88
89         if (!var->pixclock)
90                 return -EINVAL;
91
92         /* Need to resize the fb object !!! */
93         if (var->xres > fb->width || var->yres > fb->height) {
94                 DRM_ERROR("Requested width/height is greater than "
95                           "current fb object %dx%d > %dx%d\n",
96                           var->xres, var->yres, fb->width, fb->height);
97                 DRM_ERROR("Need resizing code.\n");
98                 return -EINVAL;
99         }
100
101         switch (var->bits_per_pixel) {
102         case 16:
103                 if (var->green.length == 5) {
104                         var->red.offset = 10;
105                         var->green.offset = 5;
106                         var->blue.offset = 0;
107                         var->red.length = 5;
108                         var->green.length = 5;
109                         var->blue.length = 5;
110                         var->transp.length = 0;
111                         var->transp.offset = 0;
112                 } else {
113                         var->red.offset = 11;
114                         var->green.offset = 6;
115                         var->blue.offset = 0;
116                         var->red.length = 5;
117                         var->green.length = 6;
118                         var->blue.length = 5;
119                         var->transp.length = 0;
120                         var->transp.offset = 0;
121                 }
122                 break;
123         case 32:
124                 if (var->transp.length) {
125                         var->red.offset = 16;
126                         var->green.offset = 8;
127                         var->blue.offset = 0;
128                         var->red.length = 8;
129                         var->green.length = 8;
130                         var->blue.length = 8;
131                         var->transp.length = 8;
132                         var->transp.offset = 24;
133                 } else {
134                         var->red.offset = 16;
135                         var->green.offset = 8;
136                         var->blue.offset = 0;
137                         var->red.length = 8;
138                         var->green.length = 8;
139                         var->blue.length = 8;
140                         var->transp.length = 0;
141                         var->transp.offset = 0;
142                 }
143                 break;
144         default:
145                 return -EINVAL; 
146         }
147         return 0;
148 }
149
150 static bool radeonfb_mode_equal(struct drm_display_mode *mode1,
151                                 struct drm_display_mode *mode2)
152 {
153         if (mode1->hdisplay == mode2->hdisplay &&
154             mode1->hsync_start == mode2->hsync_start &&
155             mode1->hsync_end == mode2->hsync_end &&
156             mode1->htotal == mode2->htotal &&
157             mode1->hskew == mode2->hskew &&
158             mode1->vdisplay == mode2->vdisplay &&
159             mode1->vsync_start == mode2->vsync_start &&
160             mode1->vsync_end == mode2->vsync_end &&
161             mode1->vtotal == mode2->vtotal &&
162             mode1->vscan == mode2->vscan &&
163             mode1->flags == mode2->flags) {
164                 /* FIXME: what about adding a margin for clock ? */
165                 if (mode1->clock == mode2->clock)
166                         return true;
167                 return false;
168         }
169         
170         return false;
171 }
172
173 static int radeonfb_set_par(struct fb_info *info)
174 {
175         struct amd_fb *par = info->par;
176         struct drm_framebuffer *fb = par->fb;
177         struct drm_device *dev = par->dev;
178         struct drm_display_mode *drm_mode, *search_mode;
179         struct drm_output *output;
180         struct fb_var_screeninfo *var = &info->var;
181         int found = 0;
182
183         switch (var->bits_per_pixel) {
184         case 16:
185                 fb->depth = (var->green.length == 6) ? 16 : 15;
186                 break;
187         case 32:
188                 fb->depth = (var->transp.length > 0) ? 32 : 24;
189                 break;
190         default:
191                 return -EINVAL; 
192         }
193         fb->bits_per_pixel = var->bits_per_pixel;
194
195         info->fix.line_length = fb->pitch;
196         info->fix.smem_len = info->fix.line_length * fb->height;
197         info->fix.visual = FB_VISUAL_TRUECOLOR;
198         info->screen_size = info->fix.smem_len; /* ??? */
199
200         /* Should we walk the output's modelist or just create our own ???
201          * For now, we create and destroy a mode based on the incoming 
202          * parameters. But there's commented out code below which scans 
203          * the output list too.
204          */
205         drm_mode = drm_mode_create(dev);
206         drm_mode->hdisplay = var->xres;
207         drm_mode->hsync_start = drm_mode->hdisplay + var->right_margin;
208         drm_mode->hsync_end = drm_mode->hsync_start + var->hsync_len;
209         drm_mode->htotal = drm_mode->hsync_end + var->left_margin;
210         drm_mode->vdisplay = var->yres;
211         drm_mode->vsync_start = drm_mode->vdisplay + var->lower_margin;
212         drm_mode->vsync_end = drm_mode->vsync_start + var->vsync_len;
213         drm_mode->vtotal = drm_mode->vsync_end + var->upper_margin;
214         drm_mode->clock = PICOS2KHZ(var->pixclock);
215         drm_mode->vrefresh = drm_mode_vrefresh(drm_mode);
216         drm_mode_set_name(drm_mode);
217         drm_mode_set_crtcinfo(drm_mode, CRTC_INTERLACE_HALVE_V);
218
219         list_for_each_entry(output, &dev->mode_config.output_list, head) {
220                 if (output->crtc == par->crtc)
221                         break;
222         }
223
224         drm_mode_debug_printmodeline(dev, drm_mode);    
225         list_for_each_entry(search_mode, &output->modes, head) {
226                 drm_mode_debug_printmodeline(dev, search_mode);
227                 if (radeonfb_mode_equal(drm_mode, search_mode)) {
228                         drm_mode_destroy(dev, drm_mode);
229                         drm_mode = search_mode;
230                         found = 1;
231                         break;
232                 }
233         }
234
235         if (!found) {
236                 if (par->fb_mode) {
237                         drm_mode_detachmode_crtc(dev, par->fb_mode);
238                 }
239                 par->fb_mode = drm_mode;
240                 drm_mode_debug_printmodeline(dev, drm_mode);
241                 /* attach mode */
242                 drm_mode_attachmode_crtc(dev, par->crtc, par->fb_mode);
243         }
244
245         if (par->crtc->enabled) {
246                 if (!drm_mode_equal(&par->crtc->mode, drm_mode)) {
247                         par->crtc->fb = par->fb;
248                         if (!drm_crtc_set_mode(par->crtc, drm_mode, 0, 0)) {
249                                 return -EINVAL;
250                         }
251                 }
252         }
253
254         return 0;
255 }
256
257 static struct fb_ops radeonfb_ops = {
258         .owner = THIS_MODULE,
259         //      .fb_open = radeonfb_open,
260         //      .fb_read = radeonfb_read,
261         //      .fb_write = radeonfb_write,
262         //      .fb_release = radeonfb_release,
263         //      .fb_ioctl = radeonfb_ioctl,
264         .fb_check_var = radeonfb_check_var,
265         .fb_set_par = radeonfb_set_par,
266         .fb_setcolreg = radeonfb_setcolreg,
267         .fb_fillrect = cfb_fillrect,
268         .fb_copyarea = cfb_copyarea,
269         .fb_imageblit = cfb_imageblit,
270 };
271
272 int radeonfb_probe(struct drm_device *dev, struct drm_crtc *crtc)
273 {
274         struct drm_radeon_private *dev_priv = dev->dev_private;
275         struct fb_info *info;
276         struct amd_fb *par;
277         struct device *device = &dev->pdev->dev; 
278         struct drm_framebuffer *fb;
279         struct drm_display_mode *mode = crtc->desired_mode;
280         int ret;
281
282         info = framebuffer_alloc(sizeof(struct amd_fb), device);
283         if (!info){
284                 DRM_INFO("[radeon_ms] framebuffer_alloc failed\n");
285                 return -EINVAL;
286         }
287
288         fb = drm_framebuffer_create(dev);
289         if (!fb) {
290                 framebuffer_release(info);
291                 DRM_ERROR("[radeon_ms] failed to allocate fb.\n");
292                 return -EINVAL;
293         }
294         crtc->fb = fb;
295
296         fb->width = crtc->desired_mode->hdisplay;
297         fb->height = crtc->desired_mode->vdisplay;
298         fb->bits_per_pixel = 32;
299         fb->pitch = fb->width * ((fb->bits_per_pixel + 1) / 8);
300         fb->depth = 24;
301         /* one page alignment should be fine for constraint (micro|macro tiling,
302          * bit depth, color buffer offset, ...) */
303         ret = drm_buffer_object_create(dev, fb->width * fb->height * 4, 
304                                        drm_bo_type_kernel,
305                                        DRM_BO_FLAG_READ |
306                                        DRM_BO_FLAG_WRITE |
307                                        DRM_BO_FLAG_NO_EVICT |
308                                        DRM_BO_FLAG_MEM_VRAM,
309                                        DRM_BO_HINT_DONT_FENCE,
310                                        1,
311                                        0,
312                                        &fb->bo);
313         if (ret || fb->bo == NULL) {
314                 DRM_ERROR("[radeon_ms] failed to allocate framebuffer\n");
315                 drm_framebuffer_destroy(fb);
316                 framebuffer_release(info);
317                 return -EINVAL;
318         }
319
320         DRM_INFO("[radeon_ms] framebuffer %dx%d at 0x%08lX\n",
321                  fb->width, fb->height, fb->bo->offset);
322
323         fb->fbdev = info;
324         par = info->par;
325         dev_priv->fb = par;
326         par->dev = dev;
327         par->crtc = crtc;
328         par->fb = fb;
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_radeon_private *dev_priv = dev->dev_private;
443         struct amd_fb *fb = dev_priv->fb;
444         struct fb_info *info;
445
446         if (fb == NULL || fb->fb == NULL || fb->fb->fbdev == NULL) {
447                 DRM_INFO("[radeon_ms] %s: no crtc, or fb or fbdev\n",
448                          __func__);
449                 return 0;
450         }
451         info = fb->fb->fbdev;
452         unregister_framebuffer(info);
453         drm_bo_kunmap(&fb->fb->kmap);
454         drm_bo_usage_deref_unlocked(&fb->fb->bo);
455         drm_framebuffer_destroy(fb->fb);
456         framebuffer_release(info);
457         dev_priv->fb = NULL;
458         return 0;
459 }
460 EXPORT_SYMBOL(radeonfb_remove);
461 MODULE_LICENSE("GPL");