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