c32f44096feccbccf474c3e2ca0619696950f15e
[safe/jmp/linux-2.6] / drivers / gpu / drm / radeon / radeon_fb.c
1 /*
2  * Copyright © 2007 David Airlie
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *     David Airlie
25  */
26     /*
27      *  Modularization
28      */
29
30 #include <linux/module.h>
31 #include <linux/fb.h>
32
33 #include "drmP.h"
34 #include "drm.h"
35 #include "drm_crtc.h"
36 #include "drm_crtc_helper.h"
37 #include "radeon_drm.h"
38 #include "radeon.h"
39
40 #include "drm_fb_helper.h"
41
42 struct radeon_fb_device {
43         struct drm_fb_helper helper;
44         struct radeon_framebuffer       *rfb;
45         struct radeon_device            *rdev;
46 };
47
48 static struct fb_ops radeonfb_ops = {
49         .owner = THIS_MODULE,
50         .fb_check_var = drm_fb_helper_check_var,
51         .fb_set_par = drm_fb_helper_set_par,
52         .fb_setcolreg = drm_fb_helper_setcolreg,
53         .fb_fillrect = cfb_fillrect,
54         .fb_copyarea = cfb_copyarea,
55         .fb_imageblit = cfb_imageblit,
56         .fb_pan_display = drm_fb_helper_pan_display,
57         .fb_blank = drm_fb_helper_blank,
58         .fb_setcmap = drm_fb_helper_setcmap,
59 };
60
61 /**
62  * Curretly it is assumed that the old framebuffer is reused.
63  *
64  * LOCKING
65  * caller should hold the mode config lock.
66  *
67  */
68 int radeonfb_resize(struct drm_device *dev, struct drm_crtc *crtc)
69 {
70         struct fb_info *info;
71         struct drm_framebuffer *fb;
72         struct drm_display_mode *mode = crtc->desired_mode;
73
74         fb = crtc->fb;
75         if (fb == NULL) {
76                 return 1;
77         }
78         info = fb->fbdev;
79         if (info == NULL) {
80                 return 1;
81         }
82         if (mode == NULL) {
83                 return 1;
84         }
85         info->var.xres = mode->hdisplay;
86         info->var.right_margin = mode->hsync_start - mode->hdisplay;
87         info->var.hsync_len = mode->hsync_end - mode->hsync_start;
88         info->var.left_margin = mode->htotal - mode->hsync_end;
89         info->var.yres = mode->vdisplay;
90         info->var.lower_margin = mode->vsync_start - mode->vdisplay;
91         info->var.vsync_len = mode->vsync_end - mode->vsync_start;
92         info->var.upper_margin = mode->vtotal - mode->vsync_end;
93         info->var.pixclock = 10000000 / mode->htotal * 1000 / mode->vtotal * 100;
94         /* avoid overflow */
95         info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh;
96
97         return 0;
98 }
99 EXPORT_SYMBOL(radeonfb_resize);
100
101 static int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
102 {
103         int aligned = width;
104         int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
105         int pitch_mask = 0;
106
107         switch (bpp / 8) {
108         case 1:
109                 pitch_mask = align_large ? 255 : 127;
110                 break;
111         case 2:
112                 pitch_mask = align_large ? 127 : 31;
113                 break;
114         case 3:
115         case 4:
116                 pitch_mask = align_large ? 63 : 15;
117                 break;
118         }
119
120         aligned += pitch_mask;
121         aligned &= ~pitch_mask;
122         return aligned;
123 }
124
125 static struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
126         .gamma_set = radeon_crtc_fb_gamma_set,
127 };
128
129 int radeonfb_create(struct drm_device *dev,
130                     uint32_t fb_width, uint32_t fb_height,
131                     uint32_t surface_width, uint32_t surface_height,
132                     uint32_t surface_depth, uint32_t surface_bpp,
133                     struct drm_framebuffer **fb_p)
134 {
135         struct radeon_device *rdev = dev->dev_private;
136         struct fb_info *info;
137         struct radeon_fb_device *rfbdev;
138         struct drm_framebuffer *fb = NULL;
139         struct radeon_framebuffer *rfb;
140         struct drm_mode_fb_cmd mode_cmd;
141         struct drm_gem_object *gobj = NULL;
142         struct radeon_object *robj = NULL;
143         struct device *device = &rdev->pdev->dev;
144         int size, aligned_size, ret;
145         u64 fb_gpuaddr;
146         void *fbptr = NULL;
147         unsigned long tmp;
148         bool fb_tiled = false; /* useful for testing */
149         u32 tiling_flags = 0;
150         int crtc_count;
151
152         mode_cmd.width = surface_width;
153         mode_cmd.height = surface_height;
154         mode_cmd.bpp = surface_bpp;
155         /* need to align pitch with crtc limits */
156         mode_cmd.pitch = radeon_align_pitch(rdev, mode_cmd.width, mode_cmd.bpp, fb_tiled) * ((mode_cmd.bpp + 1) / 8);
157         mode_cmd.depth = surface_depth;
158
159         size = mode_cmd.pitch * mode_cmd.height;
160         aligned_size = ALIGN(size, PAGE_SIZE);
161
162         ret = radeon_gem_object_create(rdev, aligned_size, 0,
163                         RADEON_GEM_DOMAIN_VRAM,
164                         false, ttm_bo_type_kernel,
165                         false, &gobj);
166         if (ret) {
167                 printk(KERN_ERR "failed to allocate framebuffer (%d %d)\n",
168                        surface_width, surface_height);
169                 ret = -ENOMEM;
170                 goto out;
171         }
172         robj = gobj->driver_private;
173
174         if (fb_tiled)
175                 tiling_flags = RADEON_TILING_MACRO;
176
177 #ifdef __BIG_ENDIAN
178         switch (mode_cmd.bpp) {
179         case 32:
180                 tiling_flags |= RADEON_TILING_SWAP_32BIT;
181                 break;
182         case 16:
183                 tiling_flags |= RADEON_TILING_SWAP_16BIT;
184         default:
185                 break;
186         }
187 #endif
188
189         if (tiling_flags)
190                 radeon_object_set_tiling_flags(robj, tiling_flags | RADEON_TILING_SURFACE, mode_cmd.pitch);
191         mutex_lock(&rdev->ddev->struct_mutex);
192         fb = radeon_framebuffer_create(rdev->ddev, &mode_cmd, gobj);
193         if (fb == NULL) {
194                 DRM_ERROR("failed to allocate fb.\n");
195                 ret = -ENOMEM;
196                 goto out_unref;
197         }
198         ret = radeon_object_pin(robj, RADEON_GEM_DOMAIN_VRAM, &fb_gpuaddr);
199         if (ret) {
200                 printk(KERN_ERR "failed to pin framebuffer\n");
201                 ret = -ENOMEM;
202                 goto out_unref;
203         }
204
205         list_add(&fb->filp_head, &rdev->ddev->mode_config.fb_kernel_list);
206
207         *fb_p = fb;
208         rfb = to_radeon_framebuffer(fb);
209         rdev->fbdev_rfb = rfb;
210         rdev->fbdev_robj = robj;
211
212         info = framebuffer_alloc(sizeof(struct radeon_fb_device), device);
213         if (info == NULL) {
214                 ret = -ENOMEM;
215                 goto out_unref;
216         }
217
218         rdev->fbdev_info = info;
219         rfbdev = info->par;
220         rfbdev->helper.funcs = &radeon_fb_helper_funcs;
221         rfbdev->helper.dev = dev;
222         if (rdev->flags & RADEON_SINGLE_CRTC)
223                 crtc_count = 1;
224         else
225                 crtc_count = 2;
226         ret = drm_fb_helper_init_crtc_count(&rfbdev->helper, crtc_count,
227                                             RADEONFB_CONN_LIMIT);
228         if (ret)
229                 goto out_unref;
230
231         if (fb_tiled)
232                 radeon_object_check_tiling(robj, 0, 0);
233
234         ret = radeon_object_kmap(robj, &fbptr);
235         if (ret) {
236                 goto out_unref;
237         }
238
239         memset_io(fbptr, 0, aligned_size);
240
241         strcpy(info->fix.id, "radeondrmfb");
242
243         drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
244
245         info->flags = FBINFO_DEFAULT;
246         info->fbops = &radeonfb_ops;
247
248         tmp = fb_gpuaddr - rdev->mc.vram_location;
249         info->fix.smem_start = rdev->mc.aper_base + tmp;
250         info->fix.smem_len = size;
251         info->screen_base = fbptr;
252         info->screen_size = size;
253
254         drm_fb_helper_fill_var(info, fb, fb_width, fb_height);
255
256         /* setup aperture base/size for vesafb takeover */
257         info->aperture_base = rdev->ddev->mode_config.fb_base;
258         info->aperture_size = rdev->mc.real_vram_size;
259
260         info->fix.mmio_start = 0;
261         info->fix.mmio_len = 0;
262         info->pixmap.size = 64*1024;
263         info->pixmap.buf_align = 8;
264         info->pixmap.access_align = 32;
265         info->pixmap.flags = FB_PIXMAP_SYSTEM;
266         info->pixmap.scan_align = 1;
267         if (info->screen_base == NULL) {
268                 ret = -ENOSPC;
269                 goto out_unref;
270         }
271         DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
272         DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)rdev->mc.aper_base);
273         DRM_INFO("size %lu\n", (unsigned long)size);
274         DRM_INFO("fb depth is %d\n", fb->depth);
275         DRM_INFO("   pitch is %d\n", fb->pitch);
276
277         fb->fbdev = info;
278         rfbdev->rfb = rfb;
279         rfbdev->rdev = rdev;
280
281         mutex_unlock(&rdev->ddev->struct_mutex);
282         return 0;
283
284 out_unref:
285         if (robj) {
286                 radeon_object_kunmap(robj);
287         }
288         if (fb && ret) {
289                 list_del(&fb->filp_head);
290                 drm_gem_object_unreference(gobj);
291                 drm_framebuffer_cleanup(fb);
292                 kfree(fb);
293         }
294         drm_gem_object_unreference(gobj);
295         mutex_unlock(&rdev->ddev->struct_mutex);
296 out:
297         return ret;
298 }
299
300 static char *mode_option;
301 int radeon_parse_options(char *options)
302 {
303         char *this_opt;
304
305         if (!options || !*options)
306                 return 0;
307
308         while ((this_opt = strsep(&options, ",")) != NULL) {
309                 if (!*this_opt)
310                         continue;
311                 mode_option = this_opt;
312         }
313         return 0;
314 }
315
316 int radeonfb_probe(struct drm_device *dev)
317 {
318         return drm_fb_helper_single_fb_probe(dev, &radeonfb_create);
319 }
320
321 int radeonfb_remove(struct drm_device *dev, struct drm_framebuffer *fb)
322 {
323         struct fb_info *info;
324         struct radeon_framebuffer *rfb = to_radeon_framebuffer(fb);
325         struct radeon_object *robj;
326
327         if (!fb) {
328                 return -EINVAL;
329         }
330         info = fb->fbdev;
331         if (info) {
332                 struct radeon_fb_device *rfbdev = info->par;
333                 robj = rfb->obj->driver_private;
334                 unregister_framebuffer(info);
335                 radeon_object_kunmap(robj);
336                 radeon_object_unpin(robj);
337                 drm_fb_helper_free(&rfbdev->helper);
338                 framebuffer_release(info);
339         }
340
341         printk(KERN_INFO "unregistered panic notifier\n");
342
343         return 0;
344 }
345 EXPORT_SYMBOL(radeonfb_remove);
346 MODULE_LICENSE("GPL");