blob: 058ac7d9920f7a07cffe39e90c3dd0bc116963ed [file] [log] [blame]
Russell King96f60e32012-08-15 13:59:49 +01001/*
2 * Copyright (C) 2012 Russell King
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
Daniel Vetterfcd70cd2019-01-17 22:03:34 +01008#include <drm/drm_modeset_helper.h>
Russell King96f60e32012-08-15 13:59:49 +01009#include <drm/drm_fb_helper.h>
Daniel Stoneecb8a942018-03-30 15:11:33 +010010#include <drm/drm_gem_framebuffer_helper.h>
Russell King96f60e32012-08-15 13:59:49 +010011#include "armada_drm.h"
12#include "armada_fb.h"
13#include "armada_gem.h"
14#include "armada_hw.h"
15
Russell King96f60e32012-08-15 13:59:49 +010016static const struct drm_framebuffer_funcs armada_fb_funcs = {
Daniel Stoneecb8a942018-03-30 15:11:33 +010017 .destroy = drm_gem_fb_destroy,
18 .create_handle = drm_gem_fb_create_handle,
Russell King96f60e32012-08-15 13:59:49 +010019};
20
21struct armada_framebuffer *armada_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020022 const struct drm_mode_fb_cmd2 *mode, struct armada_gem_object *obj)
Russell King96f60e32012-08-15 13:59:49 +010023{
24 struct armada_framebuffer *dfb;
25 uint8_t format, config;
26 int ret;
27
28 switch (mode->pixel_format) {
29#define FMT(drm, fmt, mod) \
30 case DRM_FORMAT_##drm: \
31 format = CFG_##fmt; \
32 config = mod; \
33 break
34 FMT(RGB565, 565, CFG_SWAPRB);
35 FMT(BGR565, 565, 0);
36 FMT(ARGB1555, 1555, CFG_SWAPRB);
37 FMT(ABGR1555, 1555, 0);
38 FMT(RGB888, 888PACK, CFG_SWAPRB);
39 FMT(BGR888, 888PACK, 0);
40 FMT(XRGB8888, X888, CFG_SWAPRB);
41 FMT(XBGR8888, X888, 0);
42 FMT(ARGB8888, 8888, CFG_SWAPRB);
43 FMT(ABGR8888, 8888, 0);
44 FMT(YUYV, 422PACK, CFG_YUV2RGB | CFG_SWAPYU | CFG_SWAPUV);
45 FMT(UYVY, 422PACK, CFG_YUV2RGB);
46 FMT(VYUY, 422PACK, CFG_YUV2RGB | CFG_SWAPUV);
47 FMT(YVYU, 422PACK, CFG_YUV2RGB | CFG_SWAPYU);
48 FMT(YUV422, 422, CFG_YUV2RGB);
49 FMT(YVU422, 422, CFG_YUV2RGB | CFG_SWAPUV);
50 FMT(YUV420, 420, CFG_YUV2RGB);
51 FMT(YVU420, 420, CFG_YUV2RGB | CFG_SWAPUV);
52 FMT(C8, PSEUDO8, 0);
53#undef FMT
54 default:
55 return ERR_PTR(-EINVAL);
56 }
57
58 dfb = kzalloc(sizeof(*dfb), GFP_KERNEL);
59 if (!dfb) {
60 DRM_ERROR("failed to allocate Armada fb object\n");
61 return ERR_PTR(-ENOMEM);
62 }
63
64 dfb->fmt = format;
65 dfb->mod = config;
Daniel Stoneecb8a942018-03-30 15:11:33 +010066 dfb->fb.obj[0] = &obj->obj;
Russell King96f60e32012-08-15 13:59:49 +010067
Ville Syrjäläa3f913c2016-12-14 22:48:59 +020068 drm_helper_mode_fill_fb_struct(dev, &dfb->fb, mode);
Russell King96f60e32012-08-15 13:59:49 +010069
70 ret = drm_framebuffer_init(dev, &dfb->fb, &armada_fb_funcs);
71 if (ret) {
72 kfree(dfb);
73 return ERR_PTR(ret);
74 }
75
76 /*
77 * Take a reference on our object as we're successful - the
78 * caller already holds a reference, which keeps us safe for
79 * the above call, but the caller will drop their reference
80 * to it. Hence we need to take our own reference.
81 */
Haneen Mohammed4c3cf372017-09-20 12:54:48 -060082 drm_gem_object_get(&obj->obj);
Russell King96f60e32012-08-15 13:59:49 +010083
84 return dfb;
85}
86
Russell King3382a6b2018-07-30 11:52:34 +010087struct drm_framebuffer *armada_fb_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020088 struct drm_file *dfile, const struct drm_mode_fb_cmd2 *mode)
Russell King96f60e32012-08-15 13:59:49 +010089{
90 struct armada_gem_object *obj;
91 struct armada_framebuffer *dfb;
92 int ret;
93
94 DRM_DEBUG_DRIVER("w%u h%u pf%08x f%u p%u,%u,%u\n",
95 mode->width, mode->height, mode->pixel_format,
96 mode->flags, mode->pitches[0], mode->pitches[1],
97 mode->pitches[2]);
98
99 /* We can only handle a single plane at the moment */
100 if (drm_format_num_planes(mode->pixel_format) > 1 &&
101 (mode->handles[0] != mode->handles[1] ||
102 mode->handles[0] != mode->handles[2])) {
103 ret = -EINVAL;
104 goto err;
105 }
106
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100107 obj = armada_gem_object_lookup(dfile, mode->handles[0]);
Russell King96f60e32012-08-15 13:59:49 +0100108 if (!obj) {
109 ret = -ENOENT;
110 goto err;
111 }
112
113 if (obj->obj.import_attach && !obj->sgt) {
114 ret = armada_gem_map_import(obj);
115 if (ret)
116 goto err_unref;
117 }
118
119 /* Framebuffer objects must have a valid device address for scanout */
Christoph Hellwigb4005852017-05-22 10:46:22 +0200120 if (!obj->mapped) {
Russell King96f60e32012-08-15 13:59:49 +0100121 ret = -EINVAL;
122 goto err_unref;
123 }
124
125 dfb = armada_framebuffer_create(dev, mode, obj);
126 if (IS_ERR(dfb)) {
127 ret = PTR_ERR(dfb);
128 goto err;
129 }
130
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600131 drm_gem_object_put_unlocked(&obj->obj);
Russell King96f60e32012-08-15 13:59:49 +0100132
133 return &dfb->fb;
134
135 err_unref:
Haneen Mohammed4c3cf372017-09-20 12:54:48 -0600136 drm_gem_object_put_unlocked(&obj->obj);
Russell King96f60e32012-08-15 13:59:49 +0100137 err:
138 DRM_ERROR("failed to initialize framebuffer: %d\n", ret);
139 return ERR_PTR(ret);
140}