blob: 705425defba0ccf32331f63d40ac6522ce751dc6 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
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 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +020026#include <linux/module.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020027#include <linux/fb.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020028
29#include "drmP.h"
30#include "drm.h"
31#include "drm_crtc.h"
32#include "drm_crtc_helper.h"
33#include "radeon_drm.h"
34#include "radeon.h"
35
Dave Airlie785b93e2009-08-28 15:46:53 +100036#include "drm_fb_helper.h"
37
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100038#include <linux/vga_switcheroo.h>
39
Dave Airlie38651672010-03-30 05:34:13 +000040/* object hierarchy -
41 this contains a helper + a radeon fb
42 the helper contains a pointer to radeon framebuffer baseclass.
43*/
Dave Airlie8be48d92010-03-30 05:34:14 +000044struct radeon_fbdev {
Dave Airlie785b93e2009-08-28 15:46:53 +100045 struct drm_fb_helper helper;
Dave Airlie38651672010-03-30 05:34:13 +000046 struct radeon_framebuffer rfb;
47 struct list_head fbdev_list;
48 struct radeon_device *rdev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020049};
50
Jerome Glisse771fe6b2009-06-05 14:42:42 +020051static struct fb_ops radeonfb_ops = {
52 .owner = THIS_MODULE,
Michel Dänzerc88f9f02009-09-15 17:09:30 +020053 .fb_check_var = drm_fb_helper_check_var,
Dave Airlie785b93e2009-08-28 15:46:53 +100054 .fb_set_par = drm_fb_helper_set_par,
55 .fb_setcolreg = drm_fb_helper_setcolreg,
Jerome Glisse771fe6b2009-06-05 14:42:42 +020056 .fb_fillrect = cfb_fillrect,
57 .fb_copyarea = cfb_copyarea,
58 .fb_imageblit = cfb_imageblit,
Dave Airlie785b93e2009-08-28 15:46:53 +100059 .fb_pan_display = drm_fb_helper_pan_display,
60 .fb_blank = drm_fb_helper_blank,
Dave Airlie068143d2009-10-05 09:58:02 +100061 .fb_setcmap = drm_fb_helper_setcmap,
Jerome Glisse771fe6b2009-06-05 14:42:42 +020062};
63
Jerome Glisse771fe6b2009-06-05 14:42:42 +020064
Dave Airliee024e112009-06-24 09:48:08 +100065static int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020066{
67 int aligned = width;
Dave Airliee024e112009-06-24 09:48:08 +100068 int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020069 int pitch_mask = 0;
70
71 switch (bpp / 8) {
72 case 1:
73 pitch_mask = align_large ? 255 : 127;
74 break;
75 case 2:
76 pitch_mask = align_large ? 127 : 31;
77 break;
78 case 3:
79 case 4:
80 pitch_mask = align_large ? 63 : 15;
81 break;
82 }
83
84 aligned += pitch_mask;
85 aligned &= ~pitch_mask;
86 return aligned;
87}
88
Dave Airlie785b93e2009-08-28 15:46:53 +100089static struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
90 .gamma_set = radeon_crtc_fb_gamma_set,
Dave Airlieb8c00ac2009-10-06 13:54:01 +100091 .gamma_get = radeon_crtc_fb_gamma_get,
Dave Airlie785b93e2009-08-28 15:46:53 +100092};
93
Dave Airlie8be48d92010-03-30 05:34:14 +000094static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020095{
Dave Airlie8be48d92010-03-30 05:34:14 +000096 struct radeon_bo *rbo = gobj->driver_private;
97 int ret;
98
99 ret = radeon_bo_reserve(rbo, false);
100 if (likely(ret == 0)) {
101 radeon_bo_kunmap(rbo);
102 radeon_bo_unreserve(rbo);
103 }
104 drm_gem_object_unreference_unlocked(gobj);
105}
106
107static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
108 struct drm_mode_fb_cmd *mode_cmd,
109 struct drm_gem_object **gobj_p)
110{
111 struct radeon_device *rdev = rfbdev->rdev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200112 struct drm_gem_object *gobj = NULL;
Jerome Glisse4c788672009-11-20 14:29:23 +0100113 struct radeon_bo *rbo = NULL;
Dave Airliee024e112009-06-24 09:48:08 +1000114 bool fb_tiled = false; /* useful for testing */
Michel Dänzerc88f9f02009-09-15 17:09:30 +0200115 u32 tiling_flags = 0;
Dave Airlie8be48d92010-03-30 05:34:14 +0000116 int ret;
117 int aligned_size, size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200118
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200119 /* need to align pitch with crtc limits */
Dave Airlie8be48d92010-03-30 05:34:14 +0000120 mode_cmd->pitch = radeon_align_pitch(rdev, mode_cmd->width, mode_cmd->bpp, fb_tiled) * ((mode_cmd->bpp + 1) / 8);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200121
Dave Airlie8be48d92010-03-30 05:34:14 +0000122 size = mode_cmd->pitch * mode_cmd->height;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200123 aligned_size = ALIGN(size, PAGE_SIZE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200124 ret = radeon_gem_object_create(rdev, aligned_size, 0,
Dave Airlie8be48d92010-03-30 05:34:14 +0000125 RADEON_GEM_DOMAIN_VRAM,
126 false, ttm_bo_type_kernel,
127 &gobj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200128 if (ret) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000129 printk(KERN_ERR "failed to allocate framebuffer (%d)\n",
130 aligned_size);
131 return -ENOMEM;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200132 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100133 rbo = gobj->driver_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200134
Dave Airliee024e112009-06-24 09:48:08 +1000135 if (fb_tiled)
Michel Dänzerc88f9f02009-09-15 17:09:30 +0200136 tiling_flags = RADEON_TILING_MACRO;
137
138#ifdef __BIG_ENDIAN
Dave Airlie8be48d92010-03-30 05:34:14 +0000139 switch (mode_cmd->bpp) {
Michel Dänzerc88f9f02009-09-15 17:09:30 +0200140 case 32:
141 tiling_flags |= RADEON_TILING_SWAP_32BIT;
142 break;
143 case 16:
144 tiling_flags |= RADEON_TILING_SWAP_16BIT;
145 default:
146 break;
147 }
148#endif
149
Jerome Glisse4c788672009-11-20 14:29:23 +0100150 if (tiling_flags) {
151 ret = radeon_bo_set_tiling_flags(rbo,
Dave Airlie8be48d92010-03-30 05:34:14 +0000152 tiling_flags | RADEON_TILING_SURFACE,
153 mode_cmd->pitch);
Jerome Glisse4c788672009-11-20 14:29:23 +0100154 if (ret)
155 dev_err(rdev->dev, "FB failed to set tiling flags\n");
156 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000157
Dave Airlie38651672010-03-30 05:34:13 +0000158
Jerome Glisse4c788672009-11-20 14:29:23 +0100159 ret = radeon_bo_reserve(rbo, false);
160 if (unlikely(ret != 0))
161 goto out_unref;
Dave Airlie8be48d92010-03-30 05:34:14 +0000162 ret = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, NULL);
Jerome Glissef92e93e2009-06-22 18:15:58 +0200163 if (ret) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100164 radeon_bo_unreserve(rbo);
165 goto out_unref;
166 }
167 if (fb_tiled)
168 radeon_bo_check_tiling(rbo, 0, 0);
Dave Airlie8be48d92010-03-30 05:34:14 +0000169 ret = radeon_bo_kmap(rbo, NULL);
Jerome Glisse4c788672009-11-20 14:29:23 +0100170 radeon_bo_unreserve(rbo);
171 if (ret) {
Jerome Glissef92e93e2009-06-22 18:15:58 +0200172 goto out_unref;
173 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200174
Dave Airlie8be48d92010-03-30 05:34:14 +0000175 *gobj_p = gobj;
176 return 0;
177out_unref:
178 radeonfb_destroy_pinned_object(gobj);
179 *gobj_p = NULL;
180 return ret;
181}
182
183static int radeonfb_create(struct radeon_fbdev *rfbdev,
184 struct drm_fb_helper_surface_size *sizes)
185{
186 struct radeon_device *rdev = rfbdev->rdev;
187 struct fb_info *info;
188 struct drm_framebuffer *fb = NULL;
189 struct drm_mode_fb_cmd mode_cmd;
190 struct drm_gem_object *gobj = NULL;
191 struct radeon_bo *rbo = NULL;
192 struct device *device = &rdev->pdev->dev;
193 int ret;
194 unsigned long tmp;
195
196 mode_cmd.width = sizes->surface_width;
197 mode_cmd.height = sizes->surface_height;
198
199 /* avivo can't scanout real 24bpp */
200 if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
201 sizes->surface_bpp = 32;
202
203 mode_cmd.bpp = sizes->surface_bpp;
204 mode_cmd.depth = sizes->surface_depth;
205
206 ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
207 rbo = gobj->driver_private;
208
209 /* okay we have an object now allocate the framebuffer */
210 info = framebuffer_alloc(0, device);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200211 if (info == NULL) {
212 ret = -ENOMEM;
213 goto out_unref;
214 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000215
Dave Airlie8be48d92010-03-30 05:34:14 +0000216 info->par = rfbdev;
217
218 radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
219
Dave Airlie38651672010-03-30 05:34:13 +0000220 fb = &rfbdev->rfb.base;
221
222 /* setup helper */
223 rfbdev->helper.fb = fb;
224 rfbdev->helper.fbdev = info;
Dave Airlie785b93e2009-08-28 15:46:53 +1000225 rfbdev->helper.funcs = &radeon_fb_helper_funcs;
Dave Airlie38651672010-03-30 05:34:13 +0000226
Dave Airlie8be48d92010-03-30 05:34:14 +0000227 memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
Dave Airliebf8e8282009-08-17 10:20:47 +1000228
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200229 strcpy(info->fix.id, "radeondrmfb");
Dave Airlie785b93e2009-08-28 15:46:53 +1000230
Dave Airlie068143d2009-10-05 09:58:02 +1000231 drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
Dave Airlie785b93e2009-08-28 15:46:53 +1000232
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200233 info->flags = FBINFO_DEFAULT;
234 info->fbops = &radeonfb_ops;
Dave Airlie785b93e2009-08-28 15:46:53 +1000235
Dave Airlie8be48d92010-03-30 05:34:14 +0000236 tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
Jerome Glissef92e93e2009-06-22 18:15:58 +0200237 info->fix.smem_start = rdev->mc.aper_base + tmp;
Dave Airlie8be48d92010-03-30 05:34:14 +0000238 info->fix.smem_len = radeon_bo_size(rbo);
239 info->screen_base = rbo->kptr;
240 info->screen_size = radeon_bo_size(rbo);
Dave Airlie785b93e2009-08-28 15:46:53 +1000241
Dave Airlie38651672010-03-30 05:34:13 +0000242 drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
Dave Airlieed8f0d92009-07-29 17:07:38 +1000243
244 /* setup aperture base/size for vesafb takeover */
245 info->aperture_base = rdev->ddev->mode_config.fb_base;
246 info->aperture_size = rdev->mc.real_vram_size;
247
Michel Dänzer696d4df2009-06-23 16:12:53 +0200248 info->fix.mmio_start = 0;
249 info->fix.mmio_len = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200250 info->pixmap.size = 64*1024;
251 info->pixmap.buf_align = 8;
252 info->pixmap.access_align = 32;
253 info->pixmap.flags = FB_PIXMAP_SYSTEM;
254 info->pixmap.scan_align = 1;
255 if (info->screen_base == NULL) {
256 ret = -ENOSPC;
257 goto out_unref;
258 }
259 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
260 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base);
Dave Airlie8be48d92010-03-30 05:34:14 +0000261 DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200262 DRM_INFO("fb depth is %d\n", fb->depth);
263 DRM_INFO(" pitch is %d\n", fb->pitch);
264
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000265 vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200266 return 0;
267
268out_unref:
Jerome Glisse4c788672009-11-20 14:29:23 +0100269 if (rbo) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000270
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200271 }
Jerome Glissef92e93e2009-06-22 18:15:58 +0200272 if (fb && ret) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200273 drm_gem_object_unreference(gobj);
274 drm_framebuffer_cleanup(fb);
275 kfree(fb);
276 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000277
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200278out:
279 return ret;
280}
281
Dave Airlie8be48d92010-03-30 05:34:14 +0000282static int radeon_fb_find_or_create_single(struct drm_fb_helper *helper,
283 struct drm_fb_helper_surface_size *sizes)
Dave Airlie38651672010-03-30 05:34:13 +0000284{
Dave Airlie8be48d92010-03-30 05:34:14 +0000285 struct radeon_fbdev *rfbdev = (struct radeon_fbdev *)helper;
Dave Airlie38651672010-03-30 05:34:13 +0000286 int new_fb = 0;
287 int ret;
288
Dave Airlie8be48d92010-03-30 05:34:14 +0000289 if (!helper->fb) {
290 ret = radeonfb_create(rfbdev, sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000291 if (ret)
292 return ret;
Dave Airlie38651672010-03-30 05:34:13 +0000293 new_fb = 1;
Dave Airlie38651672010-03-30 05:34:13 +0000294 }
Dave Airlie38651672010-03-30 05:34:13 +0000295 return new_fb;
296}
297
Dave Airlied50ba252009-09-23 14:44:08 +1000298static char *mode_option;
299int radeon_parse_options(char *options)
300{
301 char *this_opt;
302
303 if (!options || !*options)
304 return 0;
305
306 while ((this_opt = strsep(&options, ",")) != NULL) {
307 if (!*this_opt)
308 continue;
309 mode_option = this_opt;
310 }
311 return 0;
312}
313
Dave Airlie8be48d92010-03-30 05:34:14 +0000314static int radeonfb_probe(struct radeon_fbdev *rfbdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200315{
Dave Airlie8be48d92010-03-30 05:34:14 +0000316 struct radeon_device *rdev = rfbdev->rdev;
Dave Airlie47381152009-11-18 13:39:34 +1000317 int bpp_sel = 32;
318
319 /* select 8 bpp console on RN50 or 16MB cards */
320 if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
321 bpp_sel = 8;
322
Dave Airlie8be48d92010-03-30 05:34:14 +0000323 return drm_fb_helper_single_fb_probe(&rfbdev->helper, bpp_sel);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200324}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200325
Dave Airlie38651672010-03-30 05:34:13 +0000326void radeonfb_hotplug(struct drm_device *dev)
327{
Dave Airlie8be48d92010-03-30 05:34:14 +0000328 struct radeon_device *rdev = dev->dev_private;
329 int max_width, max_height;
Dave Airlie38651672010-03-30 05:34:13 +0000330
Dave Airlie8be48d92010-03-30 05:34:14 +0000331 max_width = rdev->mode_info.rfbdev->rfb.base.width;
332 max_height = rdev->mode_info.rfbdev->rfb.base.height;
333 drm_helper_fb_hotplug_event(&rdev->mode_info.rfbdev->helper, max_width, max_height);
334
335 radeonfb_probe(rdev->mode_info.rfbdev);
Dave Airlie38651672010-03-30 05:34:13 +0000336}
337
Dave Airlie8be48d92010-03-30 05:34:14 +0000338static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200339{
340 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000341 struct radeon_framebuffer *rfb = &rfbdev->rfb;
Jerome Glisse4c788672009-11-20 14:29:23 +0100342 struct radeon_bo *rbo;
343 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200344
Dave Airlie8be48d92010-03-30 05:34:14 +0000345 if (rfbdev->helper.fbdev) {
346 info = rfbdev->helper.fbdev;
347 unregister_framebuffer(info);
348 framebuffer_release(info);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200349 }
350
Dave Airlie8be48d92010-03-30 05:34:14 +0000351 if (rfb->obj) {
352 rbo = rfb->obj->driver_private;
353 r = radeon_bo_reserve(rbo, false);
354 if (likely(r == 0)) {
355 radeon_bo_kunmap(rbo);
356 radeon_bo_unpin(rbo);
357 radeon_bo_unreserve(rbo);
358 }
359 drm_gem_object_unreference_unlocked(rfb->obj);
360 }
Dave Airlie38651672010-03-30 05:34:13 +0000361 drm_fb_helper_free(&rfbdev->helper);
362 drm_framebuffer_cleanup(&rfb->base);
Dave Airlie785b93e2009-08-28 15:46:53 +1000363
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200364 return 0;
365}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200366MODULE_LICENSE("GPL");
Dave Airlie38651672010-03-30 05:34:13 +0000367
368int radeon_fbdev_init(struct radeon_device *rdev)
369{
Dave Airlie8be48d92010-03-30 05:34:14 +0000370 struct radeon_fbdev *rfbdev;
371
372 rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
373 if (!rfbdev)
374 return -ENOMEM;
375
376 rfbdev->rdev = rdev;
377 rdev->mode_info.rfbdev = rfbdev;
378
379 drm_fb_helper_init_crtc_count(rdev->ddev, &rfbdev->helper,
380 rdev->num_crtc,
381 RADEONFB_CONN_LIMIT);
382 rfbdev->helper.fb_probe = radeon_fb_find_or_create_single;
383 drm_fb_helper_initial_config(&rfbdev->helper);
384 radeonfb_probe(rfbdev);
Dave Airlie38651672010-03-30 05:34:13 +0000385 return 0;
Dave Airlie8be48d92010-03-30 05:34:14 +0000386
Dave Airlie38651672010-03-30 05:34:13 +0000387}
388
389void radeon_fbdev_fini(struct radeon_device *rdev)
390{
Dave Airlie8be48d92010-03-30 05:34:14 +0000391 if (!rdev->mode_info.rfbdev)
392 return;
393
Dave Airlie38651672010-03-30 05:34:13 +0000394 radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
Dave Airlie8be48d92010-03-30 05:34:14 +0000395 kfree(rdev->mode_info.rfbdev);
Dave Airlie38651672010-03-30 05:34:13 +0000396 rdev->mode_info.rfbdev = NULL;
397}
398
399void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
400{
401 fb_set_suspend(rdev->mode_info.rfbdev->helper.fbdev, state);
402}
403
404int radeon_fbdev_total_size(struct radeon_device *rdev)
405{
406 struct radeon_bo *robj;
407 int size = 0;
408
409 robj = rdev->mode_info.rfbdev->rfb.obj->driver_private;
410 size += radeon_bo_size(robj);
411 return size;
412}
413
414bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
415{
416 if (robj == rdev->mode_info.rfbdev->rfb.obj->driver_private)
417 return true;
418 return false;
419}
420