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 #include <linux/fb.h> 28 #include <linux/pci.h> 29 #include <linux/pm_runtime.h> 30 #include <linux/vga_switcheroo.h> 31 32 #include <drm/drm_crtc_helper.h> 33 #include <drm/drm_drv.h> 34 #include <drm/drm_fb_helper.h> 35 #include <drm/drm_fourcc.h> 36 #include <drm/drm_framebuffer.h> 37 #include <drm/drm_gem_framebuffer_helper.h> 38 39 #include "radeon.h" 40 41 static void radeon_fbdev_destroy_pinned_object(struct drm_gem_object *gobj) 42 { 43 struct radeon_bo *rbo = gem_to_radeon_bo(gobj); 44 int ret; 45 46 ret = radeon_bo_reserve(rbo, false); 47 if (likely(ret == 0)) { 48 radeon_bo_kunmap(rbo); 49 radeon_bo_unpin(rbo); 50 radeon_bo_unreserve(rbo); 51 } 52 drm_gem_object_put(gobj); 53 } 54 55 static int radeon_fbdev_create_pinned_object(struct drm_fb_helper *fb_helper, 56 struct drm_mode_fb_cmd2 *mode_cmd, 57 struct drm_gem_object **gobj_p) 58 { 59 const struct drm_format_info *info; 60 struct radeon_device *rdev = fb_helper->dev->dev_private; 61 struct drm_gem_object *gobj = NULL; 62 struct radeon_bo *rbo = NULL; 63 bool fb_tiled = false; /* useful for testing */ 64 u32 tiling_flags = 0; 65 int ret; 66 int aligned_size, size; 67 int height = mode_cmd->height; 68 u32 cpp; 69 70 info = drm_get_format_info(rdev->ddev, mode_cmd); 71 cpp = info->cpp[0]; 72 73 /* need to align pitch with crtc limits */ 74 mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, cpp, 75 fb_tiled); 76 77 if (rdev->family >= CHIP_R600) 78 height = ALIGN(mode_cmd->height, 8); 79 size = mode_cmd->pitches[0] * height; 80 aligned_size = ALIGN(size, PAGE_SIZE); 81 ret = radeon_gem_object_create(rdev, aligned_size, 0, 82 RADEON_GEM_DOMAIN_VRAM, 83 0, true, &gobj); 84 if (ret) { 85 pr_err("failed to allocate framebuffer (%d)\n", aligned_size); 86 return -ENOMEM; 87 } 88 rbo = gem_to_radeon_bo(gobj); 89 90 if (fb_tiled) 91 tiling_flags = RADEON_TILING_MACRO; 92 93 #ifdef __BIG_ENDIAN 94 switch (cpp) { 95 case 4: 96 tiling_flags |= RADEON_TILING_SWAP_32BIT; 97 break; 98 case 2: 99 tiling_flags |= RADEON_TILING_SWAP_16BIT; 100 break; 101 default: 102 break; 103 } 104 #endif 105 106 if (tiling_flags) { 107 ret = radeon_bo_set_tiling_flags(rbo, 108 tiling_flags | RADEON_TILING_SURFACE, 109 mode_cmd->pitches[0]); 110 if (ret) 111 dev_err(rdev->dev, "FB failed to set tiling flags\n"); 112 } 113 114 ret = radeon_bo_reserve(rbo, false); 115 if (unlikely(ret != 0)) 116 goto err_radeon_fbdev_destroy_pinned_object; 117 /* Only 27 bit offset for legacy CRTC */ 118 ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM, 119 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, 120 NULL); 121 if (ret) { 122 radeon_bo_unreserve(rbo); 123 goto err_radeon_fbdev_destroy_pinned_object; 124 } 125 if (fb_tiled) 126 radeon_bo_check_tiling(rbo, 0, 0); 127 ret = radeon_bo_kmap(rbo, NULL); 128 radeon_bo_unreserve(rbo); 129 if (ret) 130 goto err_radeon_fbdev_destroy_pinned_object; 131 132 *gobj_p = gobj; 133 return 0; 134 135 err_radeon_fbdev_destroy_pinned_object: 136 radeon_fbdev_destroy_pinned_object(gobj); 137 *gobj_p = NULL; 138 return ret; 139 } 140 141 /* 142 * Fbdev ops and struct fb_ops 143 */ 144 145 #ifdef __linux__ 146 static int radeon_fbdev_fb_open(struct fb_info *info, int user) 147 { 148 struct drm_fb_helper *fb_helper = info->par; 149 struct radeon_device *rdev = fb_helper->dev->dev_private; 150 int ret; 151 152 ret = pm_runtime_get_sync(rdev->ddev->dev); 153 if (ret < 0 && ret != -EACCES) 154 goto err_pm_runtime_mark_last_busy; 155 156 return 0; 157 158 err_pm_runtime_mark_last_busy: 159 pm_runtime_mark_last_busy(rdev->ddev->dev); 160 pm_runtime_put_autosuspend(rdev->ddev->dev); 161 return ret; 162 } 163 164 static int radeon_fbdev_fb_release(struct fb_info *info, int user) 165 { 166 struct drm_fb_helper *fb_helper = info->par; 167 struct radeon_device *rdev = fb_helper->dev->dev_private; 168 169 pm_runtime_mark_last_busy(rdev->ddev->dev); 170 pm_runtime_put_autosuspend(rdev->ddev->dev); 171 172 return 0; 173 } 174 175 static void radeon_fbdev_fb_destroy(struct fb_info *info) 176 { 177 struct drm_fb_helper *fb_helper = info->par; 178 struct drm_framebuffer *fb = fb_helper->fb; 179 struct drm_gem_object *gobj = drm_gem_fb_get_obj(fb, 0); 180 181 drm_fb_helper_fini(fb_helper); 182 183 drm_framebuffer_unregister_private(fb); 184 drm_framebuffer_cleanup(fb); 185 kfree(fb); 186 radeon_fbdev_destroy_pinned_object(gobj); 187 188 drm_client_release(&fb_helper->client); 189 drm_fb_helper_unprepare(fb_helper); 190 kfree(fb_helper); 191 } 192 #endif /* __linux__ */ 193 194 static const struct fb_ops radeon_fbdev_fb_ops = { 195 #ifdef notyet 196 .owner = THIS_MODULE, 197 .fb_open = radeon_fbdev_fb_open, 198 .fb_release = radeon_fbdev_fb_release, 199 FB_DEFAULT_IOMEM_OPS, 200 DRM_FB_HELPER_DEFAULT_OPS, 201 .fb_destroy = radeon_fbdev_fb_destroy, 202 #else 203 DRM_FB_HELPER_DEFAULT_OPS, 204 #endif 205 }; 206 207 /* 208 * Fbdev helpers and struct drm_fb_helper_funcs 209 */ 210 211 static int radeon_fbdev_fb_helper_fb_probe(struct drm_fb_helper *fb_helper, 212 struct drm_fb_helper_surface_size *sizes) 213 { 214 struct radeon_device *rdev = fb_helper->dev->dev_private; 215 struct drm_mode_fb_cmd2 mode_cmd = { }; 216 struct fb_info *info; 217 struct rasops_info *ri = &rdev->ro; 218 struct drm_gem_object *gobj; 219 struct radeon_bo *rbo; 220 struct drm_framebuffer *fb; 221 int ret; 222 unsigned long tmp; 223 224 mode_cmd.width = sizes->surface_width; 225 mode_cmd.height = sizes->surface_height; 226 227 /* avivo can't scanout real 24bpp */ 228 if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev)) 229 sizes->surface_bpp = 32; 230 231 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 232 sizes->surface_depth); 233 234 ret = radeon_fbdev_create_pinned_object(fb_helper, &mode_cmd, &gobj); 235 if (ret) { 236 DRM_ERROR("failed to create fbcon object %d\n", ret); 237 return ret; 238 } 239 rbo = gem_to_radeon_bo(gobj); 240 241 fb = kzalloc(sizeof(*fb), GFP_KERNEL); 242 if (!fb) { 243 ret = -ENOMEM; 244 goto err_radeon_fbdev_destroy_pinned_object; 245 } 246 ret = radeon_framebuffer_init(rdev->ddev, fb, &mode_cmd, gobj); 247 if (ret) { 248 DRM_ERROR("failed to initialize framebuffer %d\n", ret); 249 goto err_kfree; 250 } 251 252 /* setup helper */ 253 fb_helper->fb = fb; 254 255 /* okay we have an object now allocate the framebuffer */ 256 info = drm_fb_helper_alloc_info(fb_helper); 257 if (IS_ERR(info)) { 258 ret = PTR_ERR(info); 259 goto err_drm_framebuffer_unregister_private; 260 } 261 262 info->fbops = &radeon_fbdev_fb_ops; 263 264 /* radeon resume is fragile and needs a vt switch to help it along */ 265 info->skip_vt_switch = false; 266 267 drm_fb_helper_fill_info(info, fb_helper, sizes); 268 269 tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start; 270 info->fix.smem_start = rdev->mc.aper_base + tmp; 271 info->fix.smem_len = radeon_bo_size(rbo); 272 info->screen_base = (__force void __iomem *)rbo->kptr; 273 info->screen_size = radeon_bo_size(rbo); 274 275 memset_io(info->screen_base, 0, info->screen_size); 276 277 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ 278 279 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start); 280 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base); 281 DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo)); 282 DRM_INFO("fb depth is %d\n", fb->format->depth); 283 DRM_INFO(" pitch is %d\n", fb->pitches[0]); 284 285 ri->ri_bits = rbo->kptr; 286 ri->ri_depth = fb->format->cpp[0] * 8; 287 ri->ri_stride = fb->pitches[0]; 288 ri->ri_width = sizes->fb_width; 289 ri->ri_height = sizes->fb_height; 290 291 switch (fb->format->format) { 292 case DRM_FORMAT_XRGB8888: 293 ri->ri_rnum = 8; 294 ri->ri_rpos = 16; 295 ri->ri_gnum = 8; 296 ri->ri_gpos = 8; 297 ri->ri_bnum = 8; 298 ri->ri_bpos = 0; 299 break; 300 case DRM_FORMAT_RGB565: 301 ri->ri_rnum = 5; 302 ri->ri_rpos = 11; 303 ri->ri_gnum = 6; 304 ri->ri_gpos = 5; 305 ri->ri_bnum = 5; 306 ri->ri_bpos = 0; 307 break; 308 } 309 310 return 0; 311 312 err_drm_framebuffer_unregister_private: 313 fb_helper->fb = NULL; 314 drm_framebuffer_unregister_private(fb); 315 drm_framebuffer_cleanup(fb); 316 err_kfree: 317 kfree(fb); 318 err_radeon_fbdev_destroy_pinned_object: 319 radeon_fbdev_destroy_pinned_object(gobj); 320 return ret; 321 } 322 323 static const struct drm_fb_helper_funcs radeon_fbdev_fb_helper_funcs = { 324 .fb_probe = radeon_fbdev_fb_helper_fb_probe, 325 }; 326 327 /* 328 * Fbdev client and struct drm_client_funcs 329 */ 330 331 static void radeon_fbdev_client_unregister(struct drm_client_dev *client) 332 { 333 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 334 struct drm_device *dev = fb_helper->dev; 335 struct radeon_device *rdev = dev->dev_private; 336 337 if (fb_helper->info) { 338 vga_switcheroo_client_fb_set(rdev->pdev, NULL); 339 drm_helper_force_disable_all(dev); 340 drm_fb_helper_unregister_info(fb_helper); 341 } else { 342 drm_client_release(&fb_helper->client); 343 drm_fb_helper_unprepare(fb_helper); 344 kfree(fb_helper); 345 } 346 } 347 348 #ifdef __sparc64__ 349 void radeondrm_setcolor(void *, u_int, u_int8_t, u_int8_t, u_int8_t); 350 #endif 351 352 void radeondrm_burner_cb(void *); 353 354 static int radeon_fbdev_client_restore(struct drm_client_dev *client) 355 { 356 #ifdef __sparc64__ 357 struct radeon_device *rdev = client->dev->dev_private; 358 fbwscons_setcolormap(&rdev->sf, radeondrm_setcolor); 359 #endif 360 drm_fb_helper_lastclose(client->dev); 361 vga_switcheroo_process_delayed_switch(); 362 363 return 0; 364 } 365 366 static int radeon_fbdev_client_hotplug(struct drm_client_dev *client) 367 { 368 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 369 struct drm_device *dev = client->dev; 370 struct radeon_device *rdev = dev->dev_private; 371 int ret; 372 373 if (dev->fb_helper) 374 return drm_fb_helper_hotplug_event(dev->fb_helper); 375 376 ret = drm_fb_helper_init(dev, fb_helper); 377 if (ret) 378 goto err_drm_err; 379 380 task_set(&rdev->burner_task, radeondrm_burner_cb, rdev); 381 382 #ifdef __sparc64__ 383 { 384 struct drm_connector_list_iter conn_iter; 385 struct drm_connector *connector; 386 struct drm_cmdline_mode *mode; 387 388 drm_connector_list_iter_begin(fb_helper->dev, &conn_iter); 389 drm_client_for_each_connector_iter(connector, &conn_iter) { 390 mode = &connector->cmdline_mode; 391 392 mode->specified = true; 393 mode->xres = rdev->sf.sf_width; 394 mode->yres = rdev->sf.sf_height; 395 mode->bpp_specified = true; 396 mode->bpp = rdev->sf.sf_depth; 397 } 398 drm_connector_list_iter_end(&conn_iter); 399 } 400 #endif 401 402 if (!drm_drv_uses_atomic_modeset(dev)) 403 drm_helper_disable_unused_functions(dev); 404 405 ret = drm_fb_helper_initial_config(fb_helper); 406 if (ret) 407 goto err_drm_fb_helper_fini; 408 409 vga_switcheroo_client_fb_set(rdev->pdev, fb_helper->info); 410 411 return 0; 412 413 err_drm_fb_helper_fini: 414 drm_fb_helper_fini(fb_helper); 415 err_drm_err: 416 drm_err(dev, "Failed to setup radeon fbdev emulation (ret=%d)\n", ret); 417 return ret; 418 } 419 420 static const struct drm_client_funcs radeon_fbdev_client_funcs = { 421 .owner = THIS_MODULE, 422 .unregister = radeon_fbdev_client_unregister, 423 .restore = radeon_fbdev_client_restore, 424 .hotplug = radeon_fbdev_client_hotplug, 425 }; 426 427 void radeon_fbdev_setup(struct radeon_device *rdev) 428 { 429 struct drm_fb_helper *fb_helper; 430 int bpp_sel = 32; 431 int ret; 432 433 if (rdev->mc.real_vram_size <= (8 * 1024 * 1024)) 434 bpp_sel = 8; 435 else if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32 * 1024 * 1024)) 436 bpp_sel = 16; 437 438 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); 439 if (!fb_helper) 440 return; 441 drm_fb_helper_prepare(rdev->ddev, fb_helper, bpp_sel, &radeon_fbdev_fb_helper_funcs); 442 443 ret = drm_client_init(rdev->ddev, &fb_helper->client, "radeon-fbdev", 444 &radeon_fbdev_client_funcs); 445 if (ret) { 446 drm_err(rdev->ddev, "Failed to register client: %d\n", ret); 447 goto err_drm_client_init; 448 } 449 450 drm_client_register(&fb_helper->client); 451 452 return; 453 454 err_drm_client_init: 455 drm_fb_helper_unprepare(fb_helper); 456 kfree(fb_helper); 457 } 458 459 void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state) 460 { 461 if (rdev->ddev->fb_helper) 462 drm_fb_helper_set_suspend(rdev->ddev->fb_helper, state); 463 } 464 465 bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj) 466 { 467 struct drm_fb_helper *fb_helper = rdev->ddev->fb_helper; 468 struct drm_gem_object *gobj; 469 470 if (!fb_helper) 471 return false; 472 473 gobj = drm_gem_fb_get_obj(fb_helper->fb, 0); 474 if (!gobj) 475 return false; 476 if (gobj != &robj->tbo.base) 477 return false; 478 479 return true; 480 } 481 482 void 483 radeondrm_burner(void *v, u_int on, u_int flags) 484 { 485 struct rasops_info *ri = v; 486 struct radeon_device *rdev = ri->ri_hw; 487 488 task_del(systq, &rdev->burner_task); 489 490 if (on) 491 rdev->burner_fblank = FB_BLANK_UNBLANK; 492 else { 493 if (flags & WSDISPLAY_BURN_VBLANK) 494 rdev->burner_fblank = FB_BLANK_VSYNC_SUSPEND; 495 else 496 rdev->burner_fblank = FB_BLANK_NORMAL; 497 } 498 499 /* 500 * Setting the DPMS mode may sleep while waiting for vblank so 501 * hand things off to a taskq. 502 */ 503 task_add(systq, &rdev->burner_task); 504 } 505 506 void 507 radeondrm_burner_cb(void *arg1) 508 { 509 struct radeon_device *rdev = arg1; 510 struct drm_fb_helper *helper = rdev->ddev->fb_helper; 511 512 drm_fb_helper_blank(rdev->burner_fblank, helper->info); 513 } 514