1/* $NetBSD: radeon_fb.c,v 1.15 2021/12/20 20:34:58 chs Exp $ */ 2 3/* 4 * Copyright �� 2007 David Airlie 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 * DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: 26 * David Airlie 27 */ 28 29#include <sys/cdefs.h> 30__KERNEL_RCSID(0, "$NetBSD: radeon_fb.c,v 1.15 2021/12/20 20:34:58 chs Exp $"); 31 32#include <linux/module.h> 33#include <linux/pci.h> 34#include <linux/pm_runtime.h> 35#include <linux/slab.h> 36#include <linux/vga_switcheroo.h> 37 38#include <drm/drm_crtc.h> 39#include <drm/drm_crtc_helper.h> 40#include <drm/drm_fb_helper.h> 41#include <drm/drm_fourcc.h> 42#include <drm/radeon_drm.h> 43 44#include "radeon.h" 45 46#ifdef __NetBSD__ 47#include "radeondrmkmsfb.h" 48 49#include <linux/nbsd-namespace.h> 50#endif 51 52/* object hierarchy - 53 * this contains a helper + a radeon fb 54 * the helper contains a pointer to radeon framebuffer baseclass. 55 */ 56struct radeon_fbdev { 57 struct drm_fb_helper helper; /* must be first */ 58 struct drm_framebuffer fb; 59 struct radeon_device *rdev; 60}; 61 62#ifndef __NetBSD__ 63static int 64radeonfb_open(struct fb_info *info, int user) 65{ 66 struct radeon_fbdev *rfbdev = info->par; 67 struct radeon_device *rdev = rfbdev->rdev; 68 int ret = pm_runtime_get_sync(rdev->ddev->dev); 69 if (ret < 0 && ret != -EACCES) { 70 pm_runtime_mark_last_busy(rdev->ddev->dev); 71 pm_runtime_put_autosuspend(rdev->ddev->dev); 72 return ret; 73 } 74 return 0; 75} 76 77static int 78radeonfb_release(struct fb_info *info, int user) 79{ 80 struct radeon_fbdev *rfbdev = info->par; 81 struct radeon_device *rdev = rfbdev->rdev; 82 83 pm_runtime_mark_last_busy(rdev->ddev->dev); 84 pm_runtime_put_autosuspend(rdev->ddev->dev); 85 return 0; 86} 87 88static const struct fb_ops radeonfb_ops = { 89 .owner = THIS_MODULE, 90 DRM_FB_HELPER_DEFAULT_OPS, 91 .fb_open = radeonfb_open, 92 .fb_release = radeonfb_release, 93 .fb_fillrect = drm_fb_helper_cfb_fillrect, 94 .fb_copyarea = drm_fb_helper_cfb_copyarea, 95 .fb_imageblit = drm_fb_helper_cfb_imageblit, 96}; 97#endif 98 99 100int radeon_align_pitch(struct radeon_device *rdev, int width, int cpp, bool tiled) 101{ 102 int aligned = width; 103 int align_large = (ASIC_IS_AVIVO(rdev)) || tiled; 104 int pitch_mask = 0; 105 106 switch (cpp) { 107 case 1: 108 pitch_mask = align_large ? 255 : 127; 109 break; 110 case 2: 111 pitch_mask = align_large ? 127 : 31; 112 break; 113 case 3: 114 case 4: 115 pitch_mask = align_large ? 63 : 15; 116 break; 117 } 118 119 aligned += pitch_mask; 120 aligned &= ~pitch_mask; 121 return aligned * cpp; 122} 123 124static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj) 125{ 126 struct radeon_bo *rbo = gem_to_radeon_bo(gobj); 127 int ret; 128 129 ret = radeon_bo_reserve(rbo, false); 130 if (likely(ret == 0)) { 131 radeon_bo_kunmap(rbo); 132 radeon_bo_unpin(rbo); 133 radeon_bo_unreserve(rbo); 134 } 135 drm_gem_object_put_unlocked(gobj); 136} 137 138static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev, 139 struct drm_mode_fb_cmd2 *mode_cmd, 140 struct drm_gem_object **gobj_p) 141{ 142 const struct drm_format_info *info; 143 struct radeon_device *rdev = rfbdev->rdev; 144 struct drm_gem_object *gobj = NULL; 145 struct radeon_bo *rbo = NULL; 146 bool fb_tiled = false; /* useful for testing */ 147 u32 tiling_flags = 0; 148 int ret; 149 int aligned_size, size; 150 int height = mode_cmd->height; 151 u32 cpp; 152 153 info = drm_get_format_info(rdev->ddev, mode_cmd); 154 cpp = info->cpp[0]; 155 156 /* need to align pitch with crtc limits */ 157 mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, cpp, 158 fb_tiled); 159 160 if (rdev->family >= CHIP_R600) 161 height = ALIGN(mode_cmd->height, 8); 162 size = mode_cmd->pitches[0] * height; 163 aligned_size = ALIGN(size, PAGE_SIZE); 164 ret = radeon_gem_object_create(rdev, aligned_size, 0, 165 RADEON_GEM_DOMAIN_VRAM, 166 0, true, &gobj); 167 if (ret) { 168 pr_err("failed to allocate framebuffer (%d)\n", aligned_size); 169 return -ENOMEM; 170 } 171 rbo = gem_to_radeon_bo(gobj); 172 173 if (fb_tiled) 174 tiling_flags = RADEON_TILING_MACRO; 175 176#ifdef __BIG_ENDIAN 177 switch (cpp) { 178 case 4: 179 tiling_flags |= RADEON_TILING_SWAP_32BIT; 180 break; 181 case 2: 182 tiling_flags |= RADEON_TILING_SWAP_16BIT; 183 default: 184 break; 185 } 186#endif 187 188 if (tiling_flags) { 189 ret = radeon_bo_set_tiling_flags(rbo, 190 tiling_flags | RADEON_TILING_SURFACE, 191 mode_cmd->pitches[0]); 192 if (ret) 193 dev_err(rdev->dev, "FB failed to set tiling flags\n"); 194 } 195 196 197 ret = radeon_bo_reserve(rbo, false); 198 if (unlikely(ret != 0)) 199 goto out_unref; 200 /* Only 27 bit offset for legacy CRTC */ 201 ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM, 202 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, 203 NULL); 204 if (ret) { 205 radeon_bo_unreserve(rbo); 206 goto out_unref; 207 } 208 if (fb_tiled) 209 radeon_bo_check_tiling(rbo, 0, 0); 210 ret = radeon_bo_kmap(rbo, NULL); 211 radeon_bo_unreserve(rbo); 212 if (ret) { 213 goto out_unref; 214 } 215 216 *gobj_p = gobj; 217 return 0; 218out_unref: 219 radeonfb_destroy_pinned_object(gobj); 220 *gobj_p = NULL; 221 return ret; 222} 223 224static int radeonfb_create(struct drm_fb_helper *helper, 225 struct drm_fb_helper_surface_size *sizes) 226{ 227 struct radeon_fbdev *rfbdev = 228 container_of(helper, struct radeon_fbdev, helper); 229 struct radeon_device *rdev = rfbdev->rdev; 230#ifndef __NetBSD__ 231 struct fb_info *info; 232#endif 233 struct drm_framebuffer *fb = NULL; 234 struct drm_mode_fb_cmd2 mode_cmd; 235 struct drm_gem_object *gobj = NULL; 236 struct radeon_bo *rbo = NULL; 237 int ret; 238#ifndef __NetBSD__ 239 unsigned long tmp; 240#endif 241 242 mode_cmd.width = sizes->surface_width; 243 mode_cmd.height = sizes->surface_height; 244 245 /* avivo can't scanout real 24bpp */ 246 if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev)) 247 sizes->surface_bpp = 32; 248 249 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 250 sizes->surface_depth); 251 252 ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj); 253 if (ret) { 254 DRM_ERROR("failed to create fbcon object %d\n", ret); 255 return ret; 256 } 257 258 rbo = gem_to_radeon_bo(gobj); 259 260#ifdef __NetBSD__ 261 ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->fb, &mode_cmd, gobj); 262 if (ret) { 263 DRM_ERROR("failed to initialize framebuffer %d\n", ret); 264 goto out; 265 } 266 267 (void)memset(rbo->kptr, 0, radeon_bo_size(rbo)); 268 269 { 270 static const struct radeonfb_attach_args zero_rfa; 271 struct radeonfb_attach_args rfa = zero_rfa; 272 273 rfa.rfa_fb_helper = helper; 274 rfa.rfa_fb_sizes = *sizes; 275 rfa.rfa_fb_ptr = rbo->kptr; 276 rfa.rfa_fb_linebytes = mode_cmd.pitches[0]; 277 278 KERNEL_LOCK(1, NULL); 279 helper->fbdev = config_found(rdev->ddev->dev, &rfa, NULL, 280 CFARGS(.iattr = "radeonfbbus")); 281 KERNEL_UNLOCK_ONE(NULL); 282 if (helper->fbdev == NULL) { 283 DRM_ERROR("failed to attach genfb\n"); 284 goto out; 285 } 286 } 287 fb = &rfbdev->fb; 288 rfbdev->helper.fb = fb; 289#else 290 /* okay we have an object now allocate the framebuffer */ 291 info = drm_fb_helper_alloc_fbi(helper); 292 if (IS_ERR(info)) { 293 ret = PTR_ERR(info); 294 goto out; 295 } 296 297 /* radeon resume is fragile and needs a vt switch to help it along */ 298 info->skip_vt_switch = false; 299 300 ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->fb, &mode_cmd, gobj); 301 if (ret) { 302 DRM_ERROR("failed to initialize framebuffer %d\n", ret); 303 goto out; 304 } 305 306 fb = &rfbdev->fb; 307 308 /* setup helper */ 309 rfbdev->helper.fb = fb; 310 311 memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo)); 312 313 info->fbops = &radeonfb_ops; 314 315 tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start; 316 info->fix.smem_start = rdev->mc.aper_base + tmp; 317 info->fix.smem_len = radeon_bo_size(rbo); 318 info->screen_base = rbo->kptr; 319 info->screen_size = radeon_bo_size(rbo); 320 321 drm_fb_helper_fill_info(info, &rfbdev->helper, sizes); 322 323 /* setup aperture base/size for vesafb takeover */ 324 info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base; 325 info->apertures->ranges[0].size = rdev->mc.aper_size; 326 327 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ 328 329 if (info->screen_base == NULL) { 330 ret = -ENOSPC; 331 goto out; 332 } 333 334 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start); 335 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base); 336 DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo)); 337 DRM_INFO("fb depth is %d\n", fb->format->depth); 338 DRM_INFO(" pitch is %d\n", fb->pitches[0]); 339 340 vga_switcheroo_client_fb_set(rdev->ddev->pdev, info); 341#endif 342 return 0; 343 344out: 345 if (rbo) { 346 347 } 348 if (fb && ret) { 349 drm_gem_object_put_unlocked(gobj); 350 drm_framebuffer_unregister_private(fb); 351 drm_framebuffer_cleanup(fb); 352 kfree(fb); 353 } 354 return ret; 355} 356 357static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev) 358{ 359 struct drm_framebuffer *fb = &rfbdev->fb; 360 361 drm_fb_helper_unregister_fbi(&rfbdev->helper); 362 363 if (fb->obj[0]) { 364 radeonfb_destroy_pinned_object(fb->obj[0]); 365 fb->obj[0] = NULL; 366 drm_framebuffer_unregister_private(fb); 367 drm_framebuffer_cleanup(fb); 368 } 369 drm_fb_helper_fini(&rfbdev->helper); 370 371 return 0; 372} 373 374static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = { 375 .fb_probe = radeonfb_create, 376}; 377 378int radeon_fbdev_init(struct radeon_device *rdev) 379{ 380 struct radeon_fbdev *rfbdev; 381 int bpp_sel = 32; 382 int ret; 383 384 /* don't enable fbdev if no connectors */ 385 if (list_empty(&rdev->ddev->mode_config.connector_list)) 386 return 0; 387 388 /* select 8 bpp console on 8MB cards, or 16 bpp on RN50 or 32MB */ 389 if (rdev->mc.real_vram_size <= (8*1024*1024)) 390 bpp_sel = 8; 391 else if (ASIC_IS_RN50(rdev) || 392 rdev->mc.real_vram_size <= (32*1024*1024)) 393 bpp_sel = 16; 394 395 rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL); 396 if (!rfbdev) 397 return -ENOMEM; 398 399 rfbdev->rdev = rdev; 400 rdev->mode_info.rfbdev = rfbdev; 401 402 drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper, 403 &radeon_fb_helper_funcs); 404 405 ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper, 406 RADEONFB_CONN_LIMIT); 407 if (ret) 408 goto free; 409 410 ret = drm_fb_helper_single_add_all_connectors(&rfbdev->helper); 411 if (ret) 412 goto fini; 413 414 /* disable all the possible outputs/crtcs before entering KMS mode */ 415 drm_helper_disable_unused_functions(rdev->ddev); 416 417 ret = drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel); 418 if (ret) 419 goto fini; 420 421 return 0; 422 423fini: 424 drm_fb_helper_fini(&rfbdev->helper); 425free: 426 kfree(rfbdev); 427 return ret; 428} 429 430void radeon_fbdev_fini(struct radeon_device *rdev) 431{ 432 if (!rdev->mode_info.rfbdev) 433 return; 434 435 radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev); 436 kfree(rdev->mode_info.rfbdev); 437 rdev->mode_info.rfbdev = NULL; 438} 439 440void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state) 441{ 442#ifndef __NetBSD__ /* XXX radeon fb suspend */ 443 if (rdev->mode_info.rfbdev) 444 drm_fb_helper_set_suspend(&rdev->mode_info.rfbdev->helper, state); 445#endif 446} 447 448bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj) 449{ 450 if (!rdev->mode_info.rfbdev) 451 return false; 452 453 if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->fb.obj[0])) 454 return true; 455 return false; 456} 457 458void radeon_fb_add_connector(struct radeon_device *rdev, struct drm_connector *connector) 459{ 460 if (rdev->mode_info.rfbdev) 461 drm_fb_helper_add_one_connector(&rdev->mode_info.rfbdev->helper, connector); 462} 463 464void radeon_fb_remove_connector(struct radeon_device *rdev, struct drm_connector *connector) 465{ 466 if (rdev->mode_info.rfbdev) 467 drm_fb_helper_remove_one_connector(&rdev->mode_info.rfbdev->helper, connector); 468} 469