1244756Sdelphij/* 2244756Sdelphij * Copyright �� 2007 David Airlie 3244756Sdelphij * 4244756Sdelphij * Permission is hereby granted, free of charge, to any person obtaining a 5244756Sdelphij * copy of this software and associated documentation files (the "Software"), 6244756Sdelphij * to deal in the Software without restriction, including without limitation 7244756Sdelphij * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8244756Sdelphij * and/or sell copies of the Software, and to permit persons to whom the 9244756Sdelphij * Software is furnished to do so, subject to the following conditions: 10244756Sdelphij * 11244756Sdelphij * The above copyright notice and this permission notice (including the next 12244756Sdelphij * paragraph) shall be included in all copies or substantial portions of the 13244756Sdelphij * Software. 14244756Sdelphij * 15244756Sdelphij * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16244756Sdelphij * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17244756Sdelphij * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18244756Sdelphij * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19244756Sdelphij * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20244756Sdelphij * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21244756Sdelphij * DEALINGS IN THE SOFTWARE. 22244756Sdelphij * 23244756Sdelphij * Authors: 24244756Sdelphij * David Airlie 25244756Sdelphij */ 26244756Sdelphij 27244756Sdelphij#include <sys/cdefs.h> 28244756Sdelphij__FBSDID("$FreeBSD$"); 29244756Sdelphij 30244756Sdelphij#include <machine/_inttypes.h> 31244756Sdelphij 32244756Sdelphij#include <dev/drm2/drmP.h> 33244756Sdelphij#include <dev/drm2/drm_crtc.h> 34244756Sdelphij#include <dev/drm2/drm_crtc_helper.h> 35244756Sdelphij#include <dev/drm2/radeon/radeon_drm.h> 36244756Sdelphij#include "radeon.h" 37244756Sdelphij 38244756Sdelphij#include <dev/drm2/drm_fb_helper.h> 39244756Sdelphij 40244756Sdelphij/* object hierarchy - 41244756Sdelphij this contains a helper + a radeon fb 42244756Sdelphij the helper contains a pointer to radeon framebuffer baseclass. 43244756Sdelphij*/ 44244756Sdelphijstruct radeon_fbdev { 45244756Sdelphij struct drm_fb_helper helper; 46244756Sdelphij struct radeon_framebuffer rfb; 47244756Sdelphij struct list_head fbdev_list; 48244756Sdelphij struct radeon_device *rdev; 49244756Sdelphij}; 50244756Sdelphij 51244756Sdelphij#if defined(__linux__) 52244756Sdelphijstatic struct fb_ops radeonfb_ops = { 53244756Sdelphij .owner = THIS_MODULE, 54244756Sdelphij .fb_check_var = drm_fb_helper_check_var, 55244756Sdelphij .fb_set_par = drm_fb_helper_set_par, 56244756Sdelphij .fb_fillrect = cfb_fillrect, 57244756Sdelphij .fb_copyarea = cfb_copyarea, 58244756Sdelphij .fb_imageblit = cfb_imageblit, 59244756Sdelphij .fb_pan_display = drm_fb_helper_pan_display, 60244756Sdelphij .fb_blank = drm_fb_helper_blank, 61244756Sdelphij .fb_setcmap = drm_fb_helper_setcmap, 62244756Sdelphij .fb_debug_enter = drm_fb_helper_debug_enter, 63244756Sdelphij .fb_debug_leave = drm_fb_helper_debug_leave, 64244756Sdelphij}; 65244756Sdelphij#endif 66244756Sdelphij 67244756Sdelphij 68244756Sdelphijint radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled) 69244756Sdelphij{ 70244756Sdelphij int aligned = width; 71244756Sdelphij int align_large = (ASIC_IS_AVIVO(rdev)) || tiled; 72244756Sdelphij int pitch_mask = 0; 73244756Sdelphij 74244756Sdelphij switch (bpp / 8) { 75244756Sdelphij case 1: 76244756Sdelphij pitch_mask = align_large ? 255 : 127; 77244756Sdelphij break; 78244756Sdelphij case 2: 79244756Sdelphij pitch_mask = align_large ? 127 : 31; 80244756Sdelphij break; 81244756Sdelphij case 3: 82244756Sdelphij case 4: 83244756Sdelphij pitch_mask = align_large ? 63 : 15; 84244756Sdelphij break; 85244756Sdelphij } 86244756Sdelphij 87244756Sdelphij aligned += pitch_mask; 88244756Sdelphij aligned &= ~pitch_mask; 89244756Sdelphij return aligned; 90244756Sdelphij} 91244756Sdelphij 92244756Sdelphijstatic void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj) 93244756Sdelphij{ 94244756Sdelphij struct radeon_bo *rbo = gem_to_radeon_bo(gobj); 95244756Sdelphij int ret; 96244756Sdelphij 97244756Sdelphij ret = radeon_bo_reserve(rbo, false); 98244756Sdelphij if (likely(ret == 0)) { 99244756Sdelphij radeon_bo_kunmap(rbo); 100244756Sdelphij radeon_bo_unpin(rbo); 101244756Sdelphij radeon_bo_unreserve(rbo); 102244756Sdelphij } 103244756Sdelphij drm_gem_object_unreference_unlocked(gobj); 104244756Sdelphij} 105244756Sdelphij 106244756Sdelphijstatic int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev, 107244756Sdelphij struct drm_mode_fb_cmd2 *mode_cmd, 108244756Sdelphij struct drm_gem_object **gobj_p) 109244756Sdelphij{ 110244756Sdelphij struct radeon_device *rdev = rfbdev->rdev; 111244756Sdelphij struct drm_gem_object *gobj = NULL; 112244756Sdelphij struct radeon_bo *rbo = NULL; 113244756Sdelphij bool fb_tiled = false; /* useful for testing */ 114244756Sdelphij u32 tiling_flags = 0; 115244756Sdelphij int ret; 116244756Sdelphij int aligned_size, size; 117244756Sdelphij int height = mode_cmd->height; 118244756Sdelphij u32 bpp, depth; 119244756Sdelphij 120244756Sdelphij drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp); 121244756Sdelphij 122244756Sdelphij /* need to align pitch with crtc limits */ 123244756Sdelphij mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, bpp, 124244756Sdelphij fb_tiled) * ((bpp + 1) / 8); 125244756Sdelphij 126244756Sdelphij if (rdev->family >= CHIP_R600) 127244756Sdelphij height = roundup2(mode_cmd->height, 8); 128244756Sdelphij size = mode_cmd->pitches[0] * height; 129244756Sdelphij aligned_size = roundup2(size, PAGE_SIZE); 130244756Sdelphij ret = radeon_gem_object_create(rdev, aligned_size, 0, 131244756Sdelphij RADEON_GEM_DOMAIN_VRAM, 132244756Sdelphij false, true, 133244756Sdelphij &gobj); 134244756Sdelphij if (ret) { 135244756Sdelphij DRM_ERROR("failed to allocate framebuffer (%d)\n", 136244756Sdelphij aligned_size); 137244756Sdelphij return -ENOMEM; 138244756Sdelphij } 139244756Sdelphij rbo = gem_to_radeon_bo(gobj); 140244756Sdelphij 141244756Sdelphij if (fb_tiled) 142244756Sdelphij tiling_flags = RADEON_TILING_MACRO; 143244756Sdelphij 144244756Sdelphij#ifdef __BIG_ENDIAN 145244756Sdelphij switch (bpp) { 146244756Sdelphij case 32: 147244756Sdelphij tiling_flags |= RADEON_TILING_SWAP_32BIT; 148244756Sdelphij break; 149244756Sdelphij case 16: 150244756Sdelphij tiling_flags |= RADEON_TILING_SWAP_16BIT; 151244756Sdelphij default: 152244756Sdelphij break; 153244756Sdelphij } 154244756Sdelphij#endif 155244756Sdelphij 156244756Sdelphij if (tiling_flags) { 157244756Sdelphij ret = radeon_bo_set_tiling_flags(rbo, 158244756Sdelphij tiling_flags | RADEON_TILING_SURFACE, 159244756Sdelphij mode_cmd->pitches[0]); 160244756Sdelphij if (ret) 161244756Sdelphij dev_err(rdev->dev, "FB failed to set tiling flags\n"); 162244756Sdelphij } 163244756Sdelphij 164244756Sdelphij 165244756Sdelphij ret = radeon_bo_reserve(rbo, false); 166244756Sdelphij if (unlikely(ret != 0)) 167244756Sdelphij goto out_unref; 168244756Sdelphij /* Only 27 bit offset for legacy CRTC */ 169244756Sdelphij ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM, 170244756Sdelphij ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, 171244756Sdelphij NULL); 172244756Sdelphij if (ret) { 173244756Sdelphij radeon_bo_unreserve(rbo); 174244756Sdelphij goto out_unref; 175244756Sdelphij } 176244756Sdelphij if (fb_tiled) 177244756Sdelphij radeon_bo_check_tiling(rbo, 0, 0); 178244756Sdelphij ret = radeon_bo_kmap(rbo, NULL); 179244756Sdelphij radeon_bo_unreserve(rbo); 180244756Sdelphij if (ret) { 181244756Sdelphij goto out_unref; 182244756Sdelphij } 183244756Sdelphij 184244756Sdelphij *gobj_p = gobj; 185244756Sdelphij return 0; 186244756Sdelphijout_unref: 187244756Sdelphij radeonfb_destroy_pinned_object(gobj); 188244756Sdelphij *gobj_p = NULL; 189244756Sdelphij return ret; 190244756Sdelphij} 191244756Sdelphij 192244756Sdelphijstatic int radeonfb_create(struct radeon_fbdev *rfbdev, 193244756Sdelphij struct drm_fb_helper_surface_size *sizes) 194244756Sdelphij{ 195244756Sdelphij struct radeon_device *rdev = rfbdev->rdev; 196244756Sdelphij struct fb_info *info; 197244756Sdelphij struct drm_framebuffer *fb = NULL; 198244756Sdelphij struct drm_mode_fb_cmd2 mode_cmd; 199244756Sdelphij struct drm_gem_object *gobj = NULL; 200244756Sdelphij struct radeon_bo *rbo = NULL; 201244756Sdelphij int ret; 202244756Sdelphij unsigned long tmp; 203244756Sdelphij 204244756Sdelphij mode_cmd.width = sizes->surface_width; 205244756Sdelphij mode_cmd.height = sizes->surface_height; 206244756Sdelphij 207244756Sdelphij /* avivo can't scanout real 24bpp */ 208244756Sdelphij if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev)) 209244756Sdelphij sizes->surface_bpp = 32; 210244756Sdelphij 211244756Sdelphij mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 212244756Sdelphij sizes->surface_depth); 213244756Sdelphij 214244756Sdelphij ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj); 215244756Sdelphij if (ret) { 216244756Sdelphij DRM_ERROR("failed to create fbcon object %d\n", ret); 217244756Sdelphij return ret; 218244756Sdelphij } 219244756Sdelphij 220244756Sdelphij rbo = gem_to_radeon_bo(gobj); 221244756Sdelphij 222244756Sdelphij info = malloc(sizeof(*info), DRM_MEM_KMS, M_WAITOK | M_ZERO); 223244756Sdelphij 224244756Sdelphij ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj); 225244756Sdelphij if (ret) { 226244756Sdelphij DRM_ERROR("failed to initalise framebuffer %d\n", ret); 227244756Sdelphij goto out_unref; 228244756Sdelphij } 229244756Sdelphij 230244756Sdelphij fb = &rfbdev->rfb.base; 231244756Sdelphij 232244756Sdelphij /* setup helper */ 233244756Sdelphij rfbdev->helper.fb = fb; 234244756Sdelphij rfbdev->helper.fbdev = info; 235244756Sdelphij 236244756Sdelphij memset(rbo->kptr, 0x0, radeon_bo_size(rbo)); 237244756Sdelphij 238244756Sdelphij tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start; 239244756Sdelphij info->fb_size = radeon_bo_size(rbo); 240244756Sdelphij info->fb_bpp = sizes->surface_bpp; 241244756Sdelphij info->fb_width = sizes->surface_width; 242244756Sdelphij info->fb_height = sizes->surface_height; 243244756Sdelphij info->fb_pbase = rdev->mc.aper_base + tmp; 244244756Sdelphij info->fb_vbase = (vm_offset_t)rbo->kptr; 245244756Sdelphij 246244756Sdelphij DRM_INFO("fb mappable at 0x%" PRIXPTR "\n", info->fb_pbase); 247244756Sdelphij DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base); 248244756Sdelphij DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo)); 249244756Sdelphij DRM_INFO("fb depth is %d\n", fb->depth); 250244756Sdelphij DRM_INFO(" pitch is %d\n", fb->pitches[0]); 251244756Sdelphij 252244756Sdelphij return 0; 253244756Sdelphij 254244756Sdelphijout_unref: 255244756Sdelphij if (rbo) { 256244756Sdelphij /* TODO? dumbbell@ */ 257244756Sdelphij } 258244756Sdelphij if (fb && ret) { 259244756Sdelphij drm_gem_object_unreference(gobj); 260244756Sdelphij drm_framebuffer_cleanup(fb); 261244756Sdelphij free(fb, DRM_MEM_DRIVER); /* XXX malloc'd in radeon_user_framebuffer_create? */ 262244756Sdelphij } 263244756Sdelphij return ret; 264244756Sdelphij} 265244756Sdelphij 266244756Sdelphijstatic int radeon_fb_find_or_create_single(struct drm_fb_helper *helper, 267244756Sdelphij struct drm_fb_helper_surface_size *sizes) 268244756Sdelphij{ 269244756Sdelphij struct radeon_fbdev *rfbdev = (struct radeon_fbdev *)helper; 270244756Sdelphij int new_fb = 0; 271244756Sdelphij int ret; 272244756Sdelphij 273244756Sdelphij if (!helper->fb) { 274244756Sdelphij ret = radeonfb_create(rfbdev, sizes); 275244756Sdelphij if (ret) 276244756Sdelphij return ret; 277244756Sdelphij new_fb = 1; 278244756Sdelphij } 279244756Sdelphij return new_fb; 280244756Sdelphij} 281244756Sdelphij 282244756Sdelphijvoid radeon_fb_output_poll_changed(struct radeon_device *rdev) 283244756Sdelphij{ 284244756Sdelphij drm_fb_helper_hotplug_event(&rdev->mode_info.rfbdev->helper); 285244756Sdelphij} 286244756Sdelphij 287244756Sdelphijstatic int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev) 288244756Sdelphij{ 289244756Sdelphij struct fb_info *info; 290244756Sdelphij struct radeon_framebuffer *rfb = &rfbdev->rfb; 291244756Sdelphij 292244756Sdelphij if (rfbdev->helper.fbdev) { 293244756Sdelphij info = rfbdev->helper.fbdev; 294244756Sdelphij free(info, DRM_MEM_KMS); 295244756Sdelphij } 296 297 if (rfb->obj) { 298 DRM_UNLOCK(dev); /* Work around lock recursion. dumbbell@ */ 299 radeonfb_destroy_pinned_object(rfb->obj); 300 DRM_LOCK(dev); 301 rfb->obj = NULL; 302 } 303 drm_fb_helper_fini(&rfbdev->helper); 304 drm_framebuffer_cleanup(&rfb->base); 305 306 return 0; 307} 308 309static struct drm_fb_helper_funcs radeon_fb_helper_funcs = { 310 .gamma_set = radeon_crtc_fb_gamma_set, 311 .gamma_get = radeon_crtc_fb_gamma_get, 312 .fb_probe = radeon_fb_find_or_create_single, 313}; 314 315int radeon_fbdev_init(struct radeon_device *rdev) 316{ 317 struct radeon_fbdev *rfbdev; 318 int bpp_sel = 32; 319 int ret; 320 321 /* select 8 bpp console on RN50 or 16MB cards */ 322 if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024)) 323 bpp_sel = 8; 324 325 rfbdev = malloc(sizeof(struct radeon_fbdev), 326 DRM_MEM_DRIVER, M_WAITOK | M_ZERO); 327 if (!rfbdev) 328 return -ENOMEM; 329 330 rfbdev->rdev = rdev; 331 rdev->mode_info.rfbdev = rfbdev; 332 rfbdev->helper.funcs = &radeon_fb_helper_funcs; 333 334 ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper, 335 rdev->num_crtc, 336 RADEONFB_CONN_LIMIT); 337 if (ret) { 338 free(rfbdev, DRM_MEM_DRIVER); 339 return ret; 340 } 341 342 drm_fb_helper_single_add_all_connectors(&rfbdev->helper); 343 drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel); 344 return 0; 345} 346 347void radeon_fbdev_fini(struct radeon_device *rdev) 348{ 349 if (!rdev->mode_info.rfbdev) 350 return; 351 352 radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev); 353 free(rdev->mode_info.rfbdev, DRM_MEM_DRIVER); 354 rdev->mode_info.rfbdev = NULL; 355} 356 357void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state) 358{ 359#ifdef DUMBBELL_WIP 360 fb_set_suspend(rdev->mode_info.rfbdev->helper.fbdev, state); 361#endif /* DUMBBELL_WIP */ 362} 363 364int radeon_fbdev_total_size(struct radeon_device *rdev) 365{ 366 struct radeon_bo *robj; 367 int size = 0; 368 369 robj = gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj); 370 size += radeon_bo_size(robj); 371 return size; 372} 373 374bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj) 375{ 376 if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj)) 377 return true; 378 return false; 379} 380 381struct fb_info * 382radeon_fb_helper_getinfo(device_t kdev) 383{ 384 struct drm_device *dev; 385 struct radeon_device *rdev; 386 struct radeon_fbdev *rfbdev; 387 struct fb_info *info; 388 389 dev = device_get_softc(kdev); 390 rdev = dev->dev_private; 391 rfbdev = rdev->mode_info.rfbdev; 392 if (rfbdev == NULL) 393 return (NULL); 394 395 info = rfbdev->helper.fbdev; 396 397 return (info); 398} 399