| 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(bo: rbo, no_intr: false); |
| 47 | if (likely(ret == 0)) { |
| 48 | radeon_bo_kunmap(bo: rbo); |
| 49 | radeon_bo_unpin(bo: rbo); |
| 50 | radeon_bo_unreserve(bo: rbo); |
| 51 | } |
| 52 | drm_gem_object_put(obj: 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(dev: rdev_to_drm(rdev), 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, width: mode_cmd->width, cpp, |
| 75 | tiled: 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, size: aligned_size, alignment: 0, |
| 82 | RADEON_GEM_DOMAIN_VRAM, |
| 83 | flags: 0, kernel: true, obj: &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(bo: rbo, |
| 108 | tiling_flags: tiling_flags | RADEON_TILING_SURFACE, |
| 109 | pitch: 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(bo: rbo, no_intr: 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(bo: rbo, RADEON_GEM_DOMAIN_VRAM, |
| 119 | ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, |
| 120 | NULL); |
| 121 | if (ret) { |
| 122 | radeon_bo_unreserve(bo: rbo); |
| 123 | goto err_radeon_fbdev_destroy_pinned_object; |
| 124 | } |
| 125 | if (fb_tiled) |
| 126 | radeon_bo_check_tiling(bo: rbo, has_moved: 0, force_drop: 0); |
| 127 | ret = radeon_bo_kmap(bo: rbo, NULL); |
| 128 | radeon_bo_unreserve(bo: 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 | static int radeon_fbdev_fb_open(struct fb_info *info, int user) |
| 146 | { |
| 147 | struct drm_fb_helper *fb_helper = info->par; |
| 148 | struct radeon_device *rdev = fb_helper->dev->dev_private; |
| 149 | int ret; |
| 150 | |
| 151 | ret = pm_runtime_get_sync(dev: rdev_to_drm(rdev)->dev); |
| 152 | if (ret < 0 && ret != -EACCES) |
| 153 | goto err_pm_runtime_mark_last_busy; |
| 154 | |
| 155 | return 0; |
| 156 | |
| 157 | err_pm_runtime_mark_last_busy: |
| 158 | pm_runtime_mark_last_busy(dev: rdev_to_drm(rdev)->dev); |
| 159 | pm_runtime_put_autosuspend(dev: rdev_to_drm(rdev)->dev); |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | static int radeon_fbdev_fb_release(struct fb_info *info, int user) |
| 164 | { |
| 165 | struct drm_fb_helper *fb_helper = info->par; |
| 166 | struct radeon_device *rdev = fb_helper->dev->dev_private; |
| 167 | |
| 168 | pm_runtime_mark_last_busy(dev: rdev_to_drm(rdev)->dev); |
| 169 | pm_runtime_put_autosuspend(dev: rdev_to_drm(rdev)->dev); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static void radeon_fbdev_fb_destroy(struct fb_info *info) |
| 175 | { |
| 176 | struct drm_fb_helper *fb_helper = info->par; |
| 177 | struct drm_framebuffer *fb = fb_helper->fb; |
| 178 | struct drm_gem_object *gobj = drm_gem_fb_get_obj(fb, plane: 0); |
| 179 | |
| 180 | drm_fb_helper_fini(helper: fb_helper); |
| 181 | |
| 182 | drm_framebuffer_unregister_private(fb); |
| 183 | drm_framebuffer_cleanup(fb); |
| 184 | kfree(objp: fb); |
| 185 | radeon_fbdev_destroy_pinned_object(gobj); |
| 186 | |
| 187 | drm_client_release(client: &fb_helper->client); |
| 188 | drm_fb_helper_unprepare(fb_helper); |
| 189 | kfree(objp: fb_helper); |
| 190 | } |
| 191 | |
| 192 | static const struct fb_ops radeon_fbdev_fb_ops = { |
| 193 | .owner = THIS_MODULE, |
| 194 | .fb_open = radeon_fbdev_fb_open, |
| 195 | .fb_release = radeon_fbdev_fb_release, |
| 196 | FB_DEFAULT_IOMEM_OPS, |
| 197 | DRM_FB_HELPER_DEFAULT_OPS, |
| 198 | .fb_destroy = radeon_fbdev_fb_destroy, |
| 199 | }; |
| 200 | |
| 201 | static const struct drm_fb_helper_funcs radeon_fbdev_fb_helper_funcs = { |
| 202 | }; |
| 203 | |
| 204 | int radeon_fbdev_driver_fbdev_probe(struct drm_fb_helper *fb_helper, |
| 205 | struct drm_fb_helper_surface_size *sizes) |
| 206 | { |
| 207 | struct radeon_device *rdev = fb_helper->dev->dev_private; |
| 208 | struct drm_mode_fb_cmd2 mode_cmd = { }; |
| 209 | struct fb_info *info; |
| 210 | struct drm_gem_object *gobj; |
| 211 | struct radeon_bo *rbo; |
| 212 | struct drm_framebuffer *fb; |
| 213 | int ret; |
| 214 | unsigned long tmp; |
| 215 | |
| 216 | mode_cmd.width = sizes->surface_width; |
| 217 | mode_cmd.height = sizes->surface_height; |
| 218 | |
| 219 | /* avivo can't scanout real 24bpp */ |
| 220 | if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev)) |
| 221 | sizes->surface_bpp = 32; |
| 222 | |
| 223 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp: sizes->surface_bpp, |
| 224 | depth: sizes->surface_depth); |
| 225 | |
| 226 | ret = radeon_fbdev_create_pinned_object(fb_helper, mode_cmd: &mode_cmd, gobj_p: &gobj); |
| 227 | if (ret) { |
| 228 | DRM_ERROR("failed to create fbcon object %d\n" , ret); |
| 229 | return ret; |
| 230 | } |
| 231 | rbo = gem_to_radeon_bo(gobj); |
| 232 | |
| 233 | fb = kzalloc(sizeof(*fb), GFP_KERNEL); |
| 234 | if (!fb) { |
| 235 | ret = -ENOMEM; |
| 236 | goto err_radeon_fbdev_destroy_pinned_object; |
| 237 | } |
| 238 | ret = radeon_framebuffer_init(dev: rdev_to_drm(rdev), rfb: fb, mode_cmd: &mode_cmd, obj: gobj); |
| 239 | if (ret) { |
| 240 | DRM_ERROR("failed to initialize framebuffer %d\n" , ret); |
| 241 | goto err_kfree; |
| 242 | } |
| 243 | |
| 244 | /* setup helper */ |
| 245 | fb_helper->funcs = &radeon_fbdev_fb_helper_funcs; |
| 246 | fb_helper->fb = fb; |
| 247 | |
| 248 | /* okay we have an object now allocate the framebuffer */ |
| 249 | info = drm_fb_helper_alloc_info(fb_helper); |
| 250 | if (IS_ERR(ptr: info)) { |
| 251 | ret = PTR_ERR(ptr: info); |
| 252 | goto err_drm_framebuffer_unregister_private; |
| 253 | } |
| 254 | |
| 255 | info->fbops = &radeon_fbdev_fb_ops; |
| 256 | |
| 257 | /* radeon resume is fragile and needs a vt switch to help it along */ |
| 258 | info->skip_vt_switch = false; |
| 259 | |
| 260 | drm_fb_helper_fill_info(info, fb_helper, sizes); |
| 261 | |
| 262 | tmp = radeon_bo_gpu_offset(bo: rbo) - rdev->mc.vram_start; |
| 263 | info->fix.smem_start = rdev->mc.aper_base + tmp; |
| 264 | info->fix.smem_len = radeon_bo_size(bo: rbo); |
| 265 | info->screen_base = (__force void __iomem *)rbo->kptr; |
| 266 | info->screen_size = radeon_bo_size(bo: rbo); |
| 267 | |
| 268 | memset_io(info->screen_base, 0, info->screen_size); |
| 269 | |
| 270 | /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ |
| 271 | |
| 272 | DRM_INFO("fb mappable at 0x%lX\n" , info->fix.smem_start); |
| 273 | DRM_INFO("vram apper at 0x%lX\n" , (unsigned long)rdev->mc.aper_base); |
| 274 | DRM_INFO("size %lu\n" , (unsigned long)radeon_bo_size(rbo)); |
| 275 | DRM_INFO("fb depth is %d\n" , fb->format->depth); |
| 276 | DRM_INFO(" pitch is %d\n" , fb->pitches[0]); |
| 277 | |
| 278 | return 0; |
| 279 | |
| 280 | err_drm_framebuffer_unregister_private: |
| 281 | fb_helper->fb = NULL; |
| 282 | drm_framebuffer_unregister_private(fb); |
| 283 | drm_framebuffer_cleanup(fb); |
| 284 | err_kfree: |
| 285 | kfree(objp: fb); |
| 286 | err_radeon_fbdev_destroy_pinned_object: |
| 287 | radeon_fbdev_destroy_pinned_object(gobj); |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj) |
| 292 | { |
| 293 | struct drm_fb_helper *fb_helper = rdev_to_drm(rdev)->fb_helper; |
| 294 | struct drm_gem_object *gobj; |
| 295 | |
| 296 | if (!fb_helper) |
| 297 | return false; |
| 298 | |
| 299 | gobj = drm_gem_fb_get_obj(fb: fb_helper->fb, plane: 0); |
| 300 | if (!gobj) |
| 301 | return false; |
| 302 | if (gobj != &robj->tbo.base) |
| 303 | return false; |
| 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |