drm/ttm: remove cpu_address member from ttm_tt
[linux-2.6-block.git] / 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 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/fb.h>
29 #include <linux/pm_runtime.h>
30
31 #include <drm/drmP.h>
32 #include <drm/drm_crtc.h>
33 #include <drm/drm_crtc_helper.h>
34 #include <drm/radeon_drm.h>
35 #include "radeon.h"
36
37 #include <drm/drm_fb_helper.h>
38
39 #include <linux/vga_switcheroo.h>
40
41 /* object hierarchy -
42  * this contains a helper + a radeon fb
43  * the helper contains a pointer to radeon framebuffer baseclass.
44  */
45 struct radeon_fbdev {
46         struct drm_fb_helper helper;
47         struct radeon_framebuffer rfb;
48         struct radeon_device *rdev;
49 };
50
51 static int
52 radeonfb_open(struct fb_info *info, int user)
53 {
54         struct radeon_fbdev *rfbdev = info->par;
55         struct radeon_device *rdev = rfbdev->rdev;
56         int ret = pm_runtime_get_sync(rdev->ddev->dev);
57         if (ret < 0 && ret != -EACCES) {
58                 pm_runtime_mark_last_busy(rdev->ddev->dev);
59                 pm_runtime_put_autosuspend(rdev->ddev->dev);
60                 return ret;
61         }
62         return 0;
63 }
64
65 static int
66 radeonfb_release(struct fb_info *info, int user)
67 {
68         struct radeon_fbdev *rfbdev = info->par;
69         struct radeon_device *rdev = rfbdev->rdev;
70
71         pm_runtime_mark_last_busy(rdev->ddev->dev);
72         pm_runtime_put_autosuspend(rdev->ddev->dev);
73         return 0;
74 }
75
76 static struct fb_ops radeonfb_ops = {
77         .owner = THIS_MODULE,
78         .fb_open = radeonfb_open,
79         .fb_release = radeonfb_release,
80         .fb_check_var = drm_fb_helper_check_var,
81         .fb_set_par = drm_fb_helper_set_par,
82         .fb_fillrect = drm_fb_helper_cfb_fillrect,
83         .fb_copyarea = drm_fb_helper_cfb_copyarea,
84         .fb_imageblit = drm_fb_helper_cfb_imageblit,
85         .fb_pan_display = drm_fb_helper_pan_display,
86         .fb_blank = drm_fb_helper_blank,
87         .fb_setcmap = drm_fb_helper_setcmap,
88         .fb_debug_enter = drm_fb_helper_debug_enter,
89         .fb_debug_leave = drm_fb_helper_debug_leave,
90 };
91
92
93 int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
94 {
95         int aligned = width;
96         int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
97         int pitch_mask = 0;
98
99         switch (bpp / 8) {
100         case 1:
101                 pitch_mask = align_large ? 255 : 127;
102                 break;
103         case 2:
104                 pitch_mask = align_large ? 127 : 31;
105                 break;
106         case 3:
107         case 4:
108                 pitch_mask = align_large ? 63 : 15;
109                 break;
110         }
111
112         aligned += pitch_mask;
113         aligned &= ~pitch_mask;
114         return aligned;
115 }
116
117 static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
118 {
119         struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
120         int ret;
121
122         ret = radeon_bo_reserve(rbo, false);
123         if (likely(ret == 0)) {
124                 radeon_bo_kunmap(rbo);
125                 radeon_bo_unpin(rbo);
126                 radeon_bo_unreserve(rbo);
127         }
128         drm_gem_object_unreference_unlocked(gobj);
129 }
130
131 static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
132                                          struct drm_mode_fb_cmd2 *mode_cmd,
133                                          struct drm_gem_object **gobj_p)
134 {
135         struct radeon_device *rdev = rfbdev->rdev;
136         struct drm_gem_object *gobj = NULL;
137         struct radeon_bo *rbo = NULL;
138         bool fb_tiled = false; /* useful for testing */
139         u32 tiling_flags = 0;
140         int ret;
141         int aligned_size, size;
142         int height = mode_cmd->height;
143         u32 bpp, depth;
144
145         drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
146
147         /* need to align pitch with crtc limits */
148         mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, bpp,
149                                                   fb_tiled) * ((bpp + 1) / 8);
150
151         if (rdev->family >= CHIP_R600)
152                 height = ALIGN(mode_cmd->height, 8);
153         size = mode_cmd->pitches[0] * height;
154         aligned_size = ALIGN(size, PAGE_SIZE);
155         ret = radeon_gem_object_create(rdev, aligned_size, 0,
156                                        RADEON_GEM_DOMAIN_VRAM,
157                                        0, true, &gobj);
158         if (ret) {
159                 printk(KERN_ERR "failed to allocate framebuffer (%d)\n",
160                        aligned_size);
161                 return -ENOMEM;
162         }
163         rbo = gem_to_radeon_bo(gobj);
164
165         if (fb_tiled)
166                 tiling_flags = RADEON_TILING_MACRO;
167
168 #ifdef __BIG_ENDIAN
169         switch (bpp) {
170         case 32:
171                 tiling_flags |= RADEON_TILING_SWAP_32BIT;
172                 break;
173         case 16:
174                 tiling_flags |= RADEON_TILING_SWAP_16BIT;
175         default:
176                 break;
177         }
178 #endif
179
180         if (tiling_flags) {
181                 ret = radeon_bo_set_tiling_flags(rbo,
182                                                  tiling_flags | RADEON_TILING_SURFACE,
183                                                  mode_cmd->pitches[0]);
184                 if (ret)
185                         dev_err(rdev->dev, "FB failed to set tiling flags\n");
186         }
187
188
189         ret = radeon_bo_reserve(rbo, false);
190         if (unlikely(ret != 0))
191                 goto out_unref;
192         /* Only 27 bit offset for legacy CRTC */
193         ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
194                                        ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
195                                        NULL);
196         if (ret) {
197                 radeon_bo_unreserve(rbo);
198                 goto out_unref;
199         }
200         if (fb_tiled)
201                 radeon_bo_check_tiling(rbo, 0, 0);
202         ret = radeon_bo_kmap(rbo, NULL);
203         radeon_bo_unreserve(rbo);
204         if (ret) {
205                 goto out_unref;
206         }
207
208         *gobj_p = gobj;
209         return 0;
210 out_unref:
211         radeonfb_destroy_pinned_object(gobj);
212         *gobj_p = NULL;
213         return ret;
214 }
215
216 static int radeonfb_create(struct drm_fb_helper *helper,
217                            struct drm_fb_helper_surface_size *sizes)
218 {
219         struct radeon_fbdev *rfbdev =
220                 container_of(helper, struct radeon_fbdev, helper);
221         struct radeon_device *rdev = rfbdev->rdev;
222         struct fb_info *info;
223         struct drm_framebuffer *fb = NULL;
224         struct drm_mode_fb_cmd2 mode_cmd;
225         struct drm_gem_object *gobj = NULL;
226         struct radeon_bo *rbo = NULL;
227         int ret;
228         unsigned long tmp;
229
230         mode_cmd.width = sizes->surface_width;
231         mode_cmd.height = sizes->surface_height;
232
233         /* avivo can't scanout real 24bpp */
234         if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
235                 sizes->surface_bpp = 32;
236
237         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
238                                                           sizes->surface_depth);
239
240         ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
241         if (ret) {
242                 DRM_ERROR("failed to create fbcon object %d\n", ret);
243                 return ret;
244         }
245
246         rbo = gem_to_radeon_bo(gobj);
247
248         /* okay we have an object now allocate the framebuffer */
249         info = drm_fb_helper_alloc_fbi(helper);
250         if (IS_ERR(info)) {
251                 ret = PTR_ERR(info);
252                 goto out_unref;
253         }
254
255         info->par = rfbdev;
256         info->skip_vt_switch = true;
257
258         ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
259         if (ret) {
260                 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
261                 goto out_destroy_fbi;
262         }
263
264         fb = &rfbdev->rfb.base;
265
266         /* setup helper */
267         rfbdev->helper.fb = fb;
268
269         memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
270
271         strcpy(info->fix.id, "radeondrmfb");
272
273         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
274
275         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
276         info->fbops = &radeonfb_ops;
277
278         tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
279         info->fix.smem_start = rdev->mc.aper_base + tmp;
280         info->fix.smem_len = radeon_bo_size(rbo);
281         info->screen_base = rbo->kptr;
282         info->screen_size = radeon_bo_size(rbo);
283
284         drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
285
286         /* setup aperture base/size for vesafb takeover */
287         info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
288         info->apertures->ranges[0].size = rdev->mc.aper_size;
289
290         /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
291
292         if (info->screen_base == NULL) {
293                 ret = -ENOSPC;
294                 goto out_destroy_fbi;
295         }
296
297         DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
298         DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)rdev->mc.aper_base);
299         DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
300         DRM_INFO("fb depth is %d\n", fb->depth);
301         DRM_INFO("   pitch is %d\n", fb->pitches[0]);
302
303         vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
304         return 0;
305
306 out_destroy_fbi:
307         drm_fb_helper_release_fbi(helper);
308 out_unref:
309         if (rbo) {
310
311         }
312         if (fb && ret) {
313                 drm_gem_object_unreference_unlocked(gobj);
314                 drm_framebuffer_unregister_private(fb);
315                 drm_framebuffer_cleanup(fb);
316                 kfree(fb);
317         }
318         return ret;
319 }
320
321 void radeon_fb_output_poll_changed(struct radeon_device *rdev)
322 {
323         if (rdev->mode_info.rfbdev)
324                 drm_fb_helper_hotplug_event(&rdev->mode_info.rfbdev->helper);
325 }
326
327 static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
328 {
329         struct radeon_framebuffer *rfb = &rfbdev->rfb;
330
331         drm_fb_helper_unregister_fbi(&rfbdev->helper);
332         drm_fb_helper_release_fbi(&rfbdev->helper);
333
334         if (rfb->obj) {
335                 radeonfb_destroy_pinned_object(rfb->obj);
336                 rfb->obj = NULL;
337         }
338         drm_fb_helper_fini(&rfbdev->helper);
339         drm_framebuffer_unregister_private(&rfb->base);
340         drm_framebuffer_cleanup(&rfb->base);
341
342         return 0;
343 }
344
345 static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
346         .gamma_set = radeon_crtc_fb_gamma_set,
347         .gamma_get = radeon_crtc_fb_gamma_get,
348         .fb_probe = radeonfb_create,
349 };
350
351 int radeon_fbdev_init(struct radeon_device *rdev)
352 {
353         struct radeon_fbdev *rfbdev;
354         int bpp_sel = 32;
355         int ret;
356
357         /* don't enable fbdev if no connectors */
358         if (list_empty(&rdev->ddev->mode_config.connector_list))
359                 return 0;
360
361         /* select 8 bpp console on RN50 or 16MB cards */
362         if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
363                 bpp_sel = 8;
364
365         rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
366         if (!rfbdev)
367                 return -ENOMEM;
368
369         rfbdev->rdev = rdev;
370         rdev->mode_info.rfbdev = rfbdev;
371
372         drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
373                               &radeon_fb_helper_funcs);
374
375         ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
376                                  rdev->num_crtc,
377                                  RADEONFB_CONN_LIMIT);
378         if (ret)
379                 goto free;
380
381         ret = drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
382         if (ret)
383                 goto fini;
384
385         /* disable all the possible outputs/crtcs before entering KMS mode */
386         drm_helper_disable_unused_functions(rdev->ddev);
387
388         ret = drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
389         if (ret)
390                 goto fini;
391
392         return 0;
393
394 fini:
395         drm_fb_helper_fini(&rfbdev->helper);
396 free:
397         kfree(rfbdev);
398         return ret;
399 }
400
401 void radeon_fbdev_fini(struct radeon_device *rdev)
402 {
403         if (!rdev->mode_info.rfbdev)
404                 return;
405
406         radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
407         kfree(rdev->mode_info.rfbdev);
408         rdev->mode_info.rfbdev = NULL;
409 }
410
411 void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
412 {
413         if (rdev->mode_info.rfbdev)
414                 fb_set_suspend(rdev->mode_info.rfbdev->helper.fbdev, state);
415 }
416
417 bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
418 {
419         if (!rdev->mode_info.rfbdev)
420                 return false;
421
422         if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj))
423                 return true;
424         return false;
425 }
426
427 void radeon_fb_add_connector(struct radeon_device *rdev, struct drm_connector *connector)
428 {
429         if (rdev->mode_info.rfbdev)
430                 drm_fb_helper_add_one_connector(&rdev->mode_info.rfbdev->helper, connector);
431 }
432
433 void radeon_fb_remove_connector(struct radeon_device *rdev, struct drm_connector *connector)
434 {
435         if (rdev->mode_info.rfbdev)
436                 drm_fb_helper_remove_one_connector(&rdev->mode_info.rfbdev->helper, connector);
437 }
438
439 void radeon_fbdev_restore_mode(struct radeon_device *rdev)
440 {
441         struct radeon_fbdev *rfbdev = rdev->mode_info.rfbdev;
442         struct drm_fb_helper *fb_helper;
443         int ret;
444
445         if (!rfbdev)
446                 return;
447
448         fb_helper = &rfbdev->helper;
449
450         ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
451         if (ret)
452                 DRM_DEBUG("failed to restore crtc mode\n");
453 }