linux/drivers/gpu/drm/amd/amdgpu/amdgpu_fb.c
<<
>>
Prefs
   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/module.h>
  28#include <linux/pm_runtime.h>
  29#include <linux/slab.h>
  30#include <linux/vga_switcheroo.h>
  31
  32#include <drm/amdgpu_drm.h>
  33#include <drm/drm_crtc.h>
  34#include <drm/drm_crtc_helper.h>
  35#include <drm/drm_fb_helper.h>
  36#include <drm/drm_fourcc.h>
  37
  38#include "amdgpu.h"
  39#include "cikd.h"
  40#include "amdgpu_gem.h"
  41
  42#include "amdgpu_display.h"
  43
  44/* object hierarchy -
  45   this contains a helper + a amdgpu fb
  46   the helper contains a pointer to amdgpu framebuffer baseclass.
  47*/
  48
  49static int
  50amdgpufb_open(struct fb_info *info, int user)
  51{
  52        struct drm_fb_helper *fb_helper = info->par;
  53        int ret = pm_runtime_get_sync(fb_helper->dev->dev);
  54        if (ret < 0 && ret != -EACCES) {
  55                pm_runtime_mark_last_busy(fb_helper->dev->dev);
  56                pm_runtime_put_autosuspend(fb_helper->dev->dev);
  57                return ret;
  58        }
  59        return 0;
  60}
  61
  62static int
  63amdgpufb_release(struct fb_info *info, int user)
  64{
  65        struct drm_fb_helper *fb_helper = info->par;
  66
  67        pm_runtime_mark_last_busy(fb_helper->dev->dev);
  68        pm_runtime_put_autosuspend(fb_helper->dev->dev);
  69        return 0;
  70}
  71
  72static struct fb_ops amdgpufb_ops = {
  73        .owner = THIS_MODULE,
  74        DRM_FB_HELPER_DEFAULT_OPS,
  75        .fb_open = amdgpufb_open,
  76        .fb_release = amdgpufb_release,
  77        .fb_fillrect = drm_fb_helper_cfb_fillrect,
  78        .fb_copyarea = drm_fb_helper_cfb_copyarea,
  79        .fb_imageblit = drm_fb_helper_cfb_imageblit,
  80};
  81
  82
  83int amdgpu_align_pitch(struct amdgpu_device *adev, int width, int cpp, bool tiled)
  84{
  85        int aligned = width;
  86        int pitch_mask = 0;
  87
  88        switch (cpp) {
  89        case 1:
  90                pitch_mask = 255;
  91                break;
  92        case 2:
  93                pitch_mask = 127;
  94                break;
  95        case 3:
  96        case 4:
  97                pitch_mask = 63;
  98                break;
  99        }
 100
 101        aligned += pitch_mask;
 102        aligned &= ~pitch_mask;
 103        return aligned * cpp;
 104}
 105
 106static void amdgpufb_destroy_pinned_object(struct drm_gem_object *gobj)
 107{
 108        struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
 109        int ret;
 110
 111        ret = amdgpu_bo_reserve(abo, true);
 112        if (likely(ret == 0)) {
 113                amdgpu_bo_kunmap(abo);
 114                amdgpu_bo_unpin(abo);
 115                amdgpu_bo_unreserve(abo);
 116        }
 117        drm_gem_object_put_unlocked(gobj);
 118}
 119
 120static int amdgpufb_create_pinned_object(struct amdgpu_fbdev *rfbdev,
 121                                         struct drm_mode_fb_cmd2 *mode_cmd,
 122                                         struct drm_gem_object **gobj_p)
 123{
 124        const struct drm_format_info *info;
 125        struct amdgpu_device *adev = rfbdev->adev;
 126        struct drm_gem_object *gobj = NULL;
 127        struct amdgpu_bo *abo = NULL;
 128        bool fb_tiled = false; /* useful for testing */
 129        u32 tiling_flags = 0, domain;
 130        int ret;
 131        int aligned_size, size;
 132        int height = mode_cmd->height;
 133        u32 cpp;
 134
 135        info = drm_get_format_info(adev->ddev, mode_cmd);
 136        cpp = info->cpp[0];
 137
 138        /* need to align pitch with crtc limits */
 139        mode_cmd->pitches[0] = amdgpu_align_pitch(adev, mode_cmd->width, cpp,
 140                                                  fb_tiled);
 141        domain = amdgpu_display_supported_domains(adev);
 142
 143        height = ALIGN(mode_cmd->height, 8);
 144        size = mode_cmd->pitches[0] * height;
 145        aligned_size = ALIGN(size, PAGE_SIZE);
 146        ret = amdgpu_gem_object_create(adev, aligned_size, 0, domain,
 147                                       AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
 148                                       AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
 149                                       AMDGPU_GEM_CREATE_VRAM_CLEARED,
 150                                       ttm_bo_type_kernel, NULL, &gobj);
 151        if (ret) {
 152                pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
 153                return -ENOMEM;
 154        }
 155        abo = gem_to_amdgpu_bo(gobj);
 156
 157        if (fb_tiled)
 158                tiling_flags = AMDGPU_TILING_SET(ARRAY_MODE, GRPH_ARRAY_2D_TILED_THIN1);
 159
 160        ret = amdgpu_bo_reserve(abo, false);
 161        if (unlikely(ret != 0))
 162                goto out_unref;
 163
 164        if (tiling_flags) {
 165                ret = amdgpu_bo_set_tiling_flags(abo,
 166                                                 tiling_flags);
 167                if (ret)
 168                        dev_err(adev->dev, "FB failed to set tiling flags\n");
 169        }
 170
 171
 172        ret = amdgpu_bo_pin(abo, domain);
 173        if (ret) {
 174                amdgpu_bo_unreserve(abo);
 175                goto out_unref;
 176        }
 177
 178        ret = amdgpu_ttm_alloc_gart(&abo->tbo);
 179        if (ret) {
 180                amdgpu_bo_unreserve(abo);
 181                dev_err(adev->dev, "%p bind failed\n", abo);
 182                goto out_unref;
 183        }
 184
 185        ret = amdgpu_bo_kmap(abo, NULL);
 186        amdgpu_bo_unreserve(abo);
 187        if (ret) {
 188                goto out_unref;
 189        }
 190
 191        *gobj_p = gobj;
 192        return 0;
 193out_unref:
 194        amdgpufb_destroy_pinned_object(gobj);
 195        *gobj_p = NULL;
 196        return ret;
 197}
 198
 199static int amdgpufb_create(struct drm_fb_helper *helper,
 200                           struct drm_fb_helper_surface_size *sizes)
 201{
 202        struct amdgpu_fbdev *rfbdev = (struct amdgpu_fbdev *)helper;
 203        struct amdgpu_device *adev = rfbdev->adev;
 204        struct fb_info *info;
 205        struct drm_framebuffer *fb = NULL;
 206        struct drm_mode_fb_cmd2 mode_cmd;
 207        struct drm_gem_object *gobj = NULL;
 208        struct amdgpu_bo *abo = NULL;
 209        int ret;
 210        unsigned long tmp;
 211
 212        mode_cmd.width = sizes->surface_width;
 213        mode_cmd.height = sizes->surface_height;
 214
 215        if (sizes->surface_bpp == 24)
 216                sizes->surface_bpp = 32;
 217
 218        mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
 219                                                          sizes->surface_depth);
 220
 221        ret = amdgpufb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
 222        if (ret) {
 223                DRM_ERROR("failed to create fbcon object %d\n", ret);
 224                return ret;
 225        }
 226
 227        abo = gem_to_amdgpu_bo(gobj);
 228
 229        /* okay we have an object now allocate the framebuffer */
 230        info = drm_fb_helper_alloc_fbi(helper);
 231        if (IS_ERR(info)) {
 232                ret = PTR_ERR(info);
 233                goto out;
 234        }
 235
 236        ret = amdgpu_display_framebuffer_init(adev->ddev, &rfbdev->rfb,
 237                                              &mode_cmd, gobj);
 238        if (ret) {
 239                DRM_ERROR("failed to initialize framebuffer %d\n", ret);
 240                goto out;
 241        }
 242
 243        fb = &rfbdev->rfb.base;
 244
 245        /* setup helper */
 246        rfbdev->helper.fb = fb;
 247
 248        info->fbops = &amdgpufb_ops;
 249
 250        tmp = amdgpu_bo_gpu_offset(abo) - adev->gmc.vram_start;
 251        info->fix.smem_start = adev->gmc.aper_base + tmp;
 252        info->fix.smem_len = amdgpu_bo_size(abo);
 253        info->screen_base = amdgpu_bo_kptr(abo);
 254        info->screen_size = amdgpu_bo_size(abo);
 255
 256        drm_fb_helper_fill_info(info, &rfbdev->helper, sizes);
 257
 258        /* setup aperture base/size for vesafb takeover */
 259        info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base;
 260        info->apertures->ranges[0].size = adev->gmc.aper_size;
 261
 262        /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
 263
 264        if (info->screen_base == NULL) {
 265                ret = -ENOSPC;
 266                goto out;
 267        }
 268
 269        DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
 270        DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)adev->gmc.aper_base);
 271        DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(abo));
 272        DRM_INFO("fb depth is %d\n", fb->format->depth);
 273        DRM_INFO("   pitch is %d\n", fb->pitches[0]);
 274
 275        vga_switcheroo_client_fb_set(adev->ddev->pdev, info);
 276        return 0;
 277
 278out:
 279        if (abo) {
 280
 281        }
 282        if (fb && ret) {
 283                drm_gem_object_put_unlocked(gobj);
 284                drm_framebuffer_unregister_private(fb);
 285                drm_framebuffer_cleanup(fb);
 286                kfree(fb);
 287        }
 288        return ret;
 289}
 290
 291static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev)
 292{
 293        struct amdgpu_framebuffer *rfb = &rfbdev->rfb;
 294
 295        drm_fb_helper_unregister_fbi(&rfbdev->helper);
 296
 297        if (rfb->base.obj[0]) {
 298                amdgpufb_destroy_pinned_object(rfb->base.obj[0]);
 299                rfb->base.obj[0] = NULL;
 300                drm_framebuffer_unregister_private(&rfb->base);
 301                drm_framebuffer_cleanup(&rfb->base);
 302        }
 303        drm_fb_helper_fini(&rfbdev->helper);
 304
 305        return 0;
 306}
 307
 308static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = {
 309        .fb_probe = amdgpufb_create,
 310};
 311
 312int amdgpu_fbdev_init(struct amdgpu_device *adev)
 313{
 314        struct amdgpu_fbdev *rfbdev;
 315        int bpp_sel = 32;
 316        int ret;
 317
 318        /* don't init fbdev on hw without DCE */
 319        if (!adev->mode_info.mode_config_initialized)
 320                return 0;
 321
 322        /* don't init fbdev if there are no connectors */
 323        if (list_empty(&adev->ddev->mode_config.connector_list))
 324                return 0;
 325
 326        /* select 8 bpp console on low vram cards */
 327        if (adev->gmc.real_vram_size <= (32*1024*1024))
 328                bpp_sel = 8;
 329
 330        rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL);
 331        if (!rfbdev)
 332                return -ENOMEM;
 333
 334        rfbdev->adev = adev;
 335        adev->mode_info.rfbdev = rfbdev;
 336
 337        drm_fb_helper_prepare(adev->ddev, &rfbdev->helper,
 338                        &amdgpu_fb_helper_funcs);
 339
 340        ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper,
 341                                 AMDGPUFB_CONN_LIMIT);
 342        if (ret) {
 343                kfree(rfbdev);
 344                return ret;
 345        }
 346
 347        drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
 348
 349        /* disable all the possible outputs/crtcs before entering KMS mode */
 350        if (!amdgpu_device_has_dc_support(adev))
 351                drm_helper_disable_unused_functions(adev->ddev);
 352
 353        drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
 354        return 0;
 355}
 356
 357void amdgpu_fbdev_fini(struct amdgpu_device *adev)
 358{
 359        if (!adev->mode_info.rfbdev)
 360                return;
 361
 362        amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev);
 363        kfree(adev->mode_info.rfbdev);
 364        adev->mode_info.rfbdev = NULL;
 365}
 366
 367void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state)
 368{
 369        if (adev->mode_info.rfbdev)
 370                drm_fb_helper_set_suspend_unlocked(&adev->mode_info.rfbdev->helper,
 371                                                   state);
 372}
 373
 374int amdgpu_fbdev_total_size(struct amdgpu_device *adev)
 375{
 376        struct amdgpu_bo *robj;
 377        int size = 0;
 378
 379        if (!adev->mode_info.rfbdev)
 380                return 0;
 381
 382        robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.base.obj[0]);
 383        size += amdgpu_bo_size(robj);
 384        return size;
 385}
 386
 387bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj)
 388{
 389        if (!adev->mode_info.rfbdev)
 390                return false;
 391        if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.base.obj[0]))
 392                return true;
 393        return false;
 394}
 395