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_to_drm(rdev), mode_cmd->pixel_format, 71 mode_cmd->modifier[0]); 72 cpp = info->cpp[0]; 73 74 /* need to align pitch with crtc limits */ 75 mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, cpp, 76 fb_tiled); 77 78 if (rdev->family >= CHIP_R600) 79 height = ALIGN(mode_cmd->height, 8); 80 size = mode_cmd->pitches[0] * height; 81 aligned_size = ALIGN(size, PAGE_SIZE); 82 ret = radeon_gem_object_create(rdev, aligned_size, 0, 83 RADEON_GEM_DOMAIN_VRAM, 84 0, true, &gobj); 85 if (ret) { 86 pr_err("failed to allocate framebuffer (%d)\n", aligned_size); 87 return -ENOMEM; 88 } 89 rbo = gem_to_radeon_bo(gobj); 90 91 if (fb_tiled) 92 tiling_flags = RADEON_TILING_MACRO; 93 94 #ifdef __BIG_ENDIAN 95 switch (cpp) { 96 case 4: 97 tiling_flags |= RADEON_TILING_SWAP_32BIT; 98 break; 99 case 2: 100 tiling_flags |= RADEON_TILING_SWAP_16BIT; 101 break; 102 default: 103 break; 104 } 105 #endif 106 107 if (tiling_flags) { 108 ret = radeon_bo_set_tiling_flags(rbo, 109 tiling_flags | RADEON_TILING_SURFACE, 110 mode_cmd->pitches[0]); 111 if (ret) 112 dev_err(rdev->dev, "FB failed to set tiling flags\n"); 113 } 114 115 ret = radeon_bo_reserve(rbo, false); 116 if (unlikely(ret != 0)) 117 goto err_radeon_fbdev_destroy_pinned_object; 118 /* Only 27 bit offset for legacy CRTC */ 119 ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM, 120 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, 121 NULL); 122 if (ret) { 123 radeon_bo_unreserve(rbo); 124 goto err_radeon_fbdev_destroy_pinned_object; 125 } 126 if (fb_tiled) 127 radeon_bo_check_tiling(rbo, 0, 0); 128 ret = radeon_bo_kmap(rbo, NULL); 129 radeon_bo_unreserve(rbo); 130 if (ret) 131 goto err_radeon_fbdev_destroy_pinned_object; 132 133 *gobj_p = gobj; 134 return 0; 135 136 err_radeon_fbdev_destroy_pinned_object: 137 radeon_fbdev_destroy_pinned_object(gobj); 138 *gobj_p = NULL; 139 return ret; 140 } 141 142 /* 143 * Fbdev ops and struct fb_ops 144 */ 145 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_to_drm(rdev)->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_to_drm(rdev)->dev); 160 pm_runtime_put_autosuspend(rdev_to_drm(rdev)->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_to_drm(rdev)->dev); 170 pm_runtime_put_autosuspend(rdev_to_drm(rdev)->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 193 static const struct fb_ops radeon_fbdev_fb_ops = { 194 .owner = THIS_MODULE, 195 .fb_open = radeon_fbdev_fb_open, 196 .fb_release = radeon_fbdev_fb_release, 197 FB_DEFAULT_IOMEM_OPS, 198 DRM_FB_HELPER_DEFAULT_OPS, 199 .fb_destroy = radeon_fbdev_fb_destroy, 200 }; 201 202 static const struct drm_fb_helper_funcs radeon_fbdev_fb_helper_funcs = { 203 }; 204 205 int radeon_fbdev_driver_fbdev_probe(struct drm_fb_helper *fb_helper, 206 struct drm_fb_helper_surface_size *sizes) 207 { 208 struct radeon_device *rdev = fb_helper->dev->dev_private; 209 struct drm_mode_fb_cmd2 mode_cmd = { }; 210 struct fb_info *info; 211 struct drm_gem_object *gobj; 212 struct radeon_bo *rbo; 213 struct drm_framebuffer *fb; 214 int ret; 215 unsigned long tmp; 216 217 mode_cmd.width = sizes->surface_width; 218 mode_cmd.height = sizes->surface_height; 219 220 /* avivo can't scanout real 24bpp */ 221 if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev)) 222 sizes->surface_bpp = 32; 223 224 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 225 sizes->surface_depth); 226 227 ret = radeon_fbdev_create_pinned_object(fb_helper, &mode_cmd, &gobj); 228 if (ret) { 229 DRM_ERROR("failed to create fbcon object %d\n", ret); 230 return ret; 231 } 232 rbo = gem_to_radeon_bo(gobj); 233 234 fb = kzalloc(sizeof(*fb), GFP_KERNEL); 235 if (!fb) { 236 ret = -ENOMEM; 237 goto err_radeon_fbdev_destroy_pinned_object; 238 } 239 ret = radeon_framebuffer_init(rdev_to_drm(rdev), fb, &mode_cmd, gobj); 240 if (ret) { 241 DRM_ERROR("failed to initialize framebuffer %d\n", ret); 242 goto err_kfree; 243 } 244 245 /* setup helper */ 246 fb_helper->funcs = &radeon_fbdev_fb_helper_funcs; 247 fb_helper->fb = fb; 248 249 /* okay we have an object now allocate the framebuffer */ 250 info = drm_fb_helper_alloc_info(fb_helper); 251 if (IS_ERR(info)) { 252 ret = PTR_ERR(info); 253 goto err_drm_framebuffer_unregister_private; 254 } 255 256 info->fbops = &radeon_fbdev_fb_ops; 257 258 /* radeon resume is fragile and needs a vt switch to help it along */ 259 info->skip_vt_switch = false; 260 261 drm_fb_helper_fill_info(info, fb_helper, sizes); 262 263 tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start; 264 info->fix.smem_start = rdev->mc.aper_base + tmp; 265 info->fix.smem_len = radeon_bo_size(rbo); 266 info->screen_base = (__force void __iomem *)rbo->kptr; 267 info->screen_size = radeon_bo_size(rbo); 268 269 memset_io(info->screen_base, 0, info->screen_size); 270 271 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ 272 273 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start); 274 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base); 275 DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo)); 276 DRM_INFO("fb depth is %d\n", fb->format->depth); 277 DRM_INFO(" pitch is %d\n", fb->pitches[0]); 278 279 return 0; 280 281 err_drm_framebuffer_unregister_private: 282 fb_helper->fb = NULL; 283 drm_framebuffer_unregister_private(fb); 284 drm_framebuffer_cleanup(fb); 285 err_kfree: 286 kfree(fb); 287 err_radeon_fbdev_destroy_pinned_object: 288 radeon_fbdev_destroy_pinned_object(gobj); 289 return ret; 290 } 291 292 bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj) 293 { 294 struct drm_fb_helper *fb_helper = rdev_to_drm(rdev)->fb_helper; 295 struct drm_gem_object *gobj; 296 297 if (!fb_helper) 298 return false; 299 300 gobj = drm_gem_fb_get_obj(fb_helper->fb, 0); 301 if (!gobj) 302 return false; 303 if (gobj != &robj->tbo.base) 304 return false; 305 306 return true; 307 } 308