linux/drivers/gpu/drm/armada/armada_fb.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) 2012 Russell King
   4 */
   5#include <drm/drm_modeset_helper.h>
   6#include <drm/drm_fb_helper.h>
   7#include <drm/drm_gem_framebuffer_helper.h>
   8#include "armada_drm.h"
   9#include "armada_fb.h"
  10#include "armada_gem.h"
  11#include "armada_hw.h"
  12
  13static const struct drm_framebuffer_funcs armada_fb_funcs = {
  14        .destroy        = drm_gem_fb_destroy,
  15        .create_handle  = drm_gem_fb_create_handle,
  16};
  17
  18struct armada_framebuffer *armada_framebuffer_create(struct drm_device *dev,
  19        const struct drm_mode_fb_cmd2 *mode, struct armada_gem_object *obj)
  20{
  21        struct armada_framebuffer *dfb;
  22        uint8_t format, config;
  23        int ret;
  24
  25        switch (mode->pixel_format) {
  26#define FMT(drm, fmt, mod)              \
  27        case DRM_FORMAT_##drm:          \
  28                format = CFG_##fmt;     \
  29                config = mod;           \
  30                break
  31        FMT(RGB565,     565,            CFG_SWAPRB);
  32        FMT(BGR565,     565,            0);
  33        FMT(ARGB1555,   1555,           CFG_SWAPRB);
  34        FMT(ABGR1555,   1555,           0);
  35        FMT(RGB888,     888PACK,        CFG_SWAPRB);
  36        FMT(BGR888,     888PACK,        0);
  37        FMT(XRGB8888,   X888,           CFG_SWAPRB);
  38        FMT(XBGR8888,   X888,           0);
  39        FMT(ARGB8888,   8888,           CFG_SWAPRB);
  40        FMT(ABGR8888,   8888,           0);
  41        FMT(YUYV,       422PACK,        CFG_YUV2RGB | CFG_SWAPYU | CFG_SWAPUV);
  42        FMT(UYVY,       422PACK,        CFG_YUV2RGB);
  43        FMT(VYUY,       422PACK,        CFG_YUV2RGB | CFG_SWAPUV);
  44        FMT(YVYU,       422PACK,        CFG_YUV2RGB | CFG_SWAPYU);
  45        FMT(YUV422,     422,            CFG_YUV2RGB);
  46        FMT(YVU422,     422,            CFG_YUV2RGB | CFG_SWAPUV);
  47        FMT(YUV420,     420,            CFG_YUV2RGB);
  48        FMT(YVU420,     420,            CFG_YUV2RGB | CFG_SWAPUV);
  49        FMT(C8,         PSEUDO8,        0);
  50#undef FMT
  51        default:
  52                return ERR_PTR(-EINVAL);
  53        }
  54
  55        dfb = kzalloc(sizeof(*dfb), GFP_KERNEL);
  56        if (!dfb) {
  57                DRM_ERROR("failed to allocate Armada fb object\n");
  58                return ERR_PTR(-ENOMEM);
  59        }
  60
  61        dfb->fmt = format;
  62        dfb->mod = config;
  63        dfb->fb.obj[0] = &obj->obj;
  64
  65        drm_helper_mode_fill_fb_struct(dev, &dfb->fb, mode);
  66
  67        ret = drm_framebuffer_init(dev, &dfb->fb, &armada_fb_funcs);
  68        if (ret) {
  69                kfree(dfb);
  70                return ERR_PTR(ret);
  71        }
  72
  73        /*
  74         * Take a reference on our object as we're successful - the
  75         * caller already holds a reference, which keeps us safe for
  76         * the above call, but the caller will drop their reference
  77         * to it.  Hence we need to take our own reference.
  78         */
  79        drm_gem_object_get(&obj->obj);
  80
  81        return dfb;
  82}
  83
  84struct drm_framebuffer *armada_fb_create(struct drm_device *dev,
  85        struct drm_file *dfile, const struct drm_mode_fb_cmd2 *mode)
  86{
  87        const struct drm_format_info *info = drm_get_format_info(dev, mode);
  88        struct armada_gem_object *obj;
  89        struct armada_framebuffer *dfb;
  90        int ret;
  91
  92        DRM_DEBUG_DRIVER("w%u h%u pf%08x f%u p%u,%u,%u\n",
  93                mode->width, mode->height, mode->pixel_format,
  94                mode->flags, mode->pitches[0], mode->pitches[1],
  95                mode->pitches[2]);
  96
  97        /* We can only handle a single plane at the moment */
  98        if (info->num_planes > 1 &&
  99            (mode->handles[0] != mode->handles[1] ||
 100             mode->handles[0] != mode->handles[2])) {
 101                ret = -EINVAL;
 102                goto err;
 103        }
 104
 105        obj = armada_gem_object_lookup(dfile, mode->handles[0]);
 106        if (!obj) {
 107                ret = -ENOENT;
 108                goto err;
 109        }
 110
 111        if (obj->obj.import_attach && !obj->sgt) {
 112                ret = armada_gem_map_import(obj);
 113                if (ret)
 114                        goto err_unref;
 115        }
 116
 117        /* Framebuffer objects must have a valid device address for scanout */
 118        if (!obj->mapped) {
 119                ret = -EINVAL;
 120                goto err_unref;
 121        }
 122
 123        dfb = armada_framebuffer_create(dev, mode, obj);
 124        if (IS_ERR(dfb)) {
 125                ret = PTR_ERR(dfb);
 126                goto err;
 127        }
 128
 129        drm_gem_object_put_unlocked(&obj->obj);
 130
 131        return &dfb->fb;
 132
 133 err_unref:
 134        drm_gem_object_put_unlocked(&obj->obj);
 135 err:
 136        DRM_ERROR("failed to initialize framebuffer: %d\n", ret);
 137        return ERR_PTR(ret);
 138}
 139