dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / mediatek / mtk_drm_fb.c
blobe20fcaef28515b346b4e9b27bce0d59274271d4f
1 /*
2 * Copyright (c) 2015 MediaTek Inc.
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.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <drm/drmP.h>
15 #include <drm/drm_modeset_helper.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_gem.h>
18 #include <drm/drm_gem_framebuffer_helper.h>
19 #include <linux/dma-buf.h>
20 #include <linux/reservation.h>
22 #include "mtk_drm_drv.h"
23 #include "mtk_drm_fb.h"
24 #include "mtk_drm_gem.h"
26 static const struct drm_framebuffer_funcs mtk_drm_fb_funcs = {
27 .create_handle = drm_gem_fb_create_handle,
28 .destroy = drm_gem_fb_destroy,
31 static struct drm_framebuffer *mtk_drm_framebuffer_init(struct drm_device *dev,
32 const struct drm_mode_fb_cmd2 *mode,
33 struct drm_gem_object *obj)
35 struct drm_framebuffer *fb;
36 int ret;
38 if (drm_format_num_planes(mode->pixel_format) != 1)
39 return ERR_PTR(-EINVAL);
41 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
42 if (!fb)
43 return ERR_PTR(-ENOMEM);
45 drm_helper_mode_fill_fb_struct(dev, fb, mode);
47 fb->obj[0] = obj;
49 ret = drm_framebuffer_init(dev, fb, &mtk_drm_fb_funcs);
50 if (ret) {
51 DRM_ERROR("failed to initialize framebuffer\n");
52 kfree(fb);
53 return ERR_PTR(ret);
56 return fb;
60 * Wait for any exclusive fence in fb's gem object's reservation object.
62 * Returns -ERESTARTSYS if interrupted, else 0.
64 int mtk_fb_wait(struct drm_framebuffer *fb)
66 struct drm_gem_object *gem;
67 struct reservation_object *resv;
68 long ret;
70 if (!fb)
71 return 0;
73 gem = fb->obj[0];
74 if (!gem || !gem->dma_buf || !gem->dma_buf->resv)
75 return 0;
77 resv = gem->dma_buf->resv;
78 ret = reservation_object_wait_timeout_rcu(resv, false, true,
79 MAX_SCHEDULE_TIMEOUT);
80 /* MAX_SCHEDULE_TIMEOUT on success, -ERESTARTSYS if interrupted */
81 if (WARN_ON(ret < 0))
82 return ret;
84 return 0;
87 struct drm_framebuffer *mtk_drm_mode_fb_create(struct drm_device *dev,
88 struct drm_file *file,
89 const struct drm_mode_fb_cmd2 *cmd)
91 struct drm_framebuffer *fb;
92 struct drm_gem_object *gem;
93 unsigned int width = cmd->width;
94 unsigned int height = cmd->height;
95 unsigned int size, bpp;
96 int ret;
98 if (drm_format_num_planes(cmd->pixel_format) != 1)
99 return ERR_PTR(-EINVAL);
101 gem = drm_gem_object_lookup(file, cmd->handles[0]);
102 if (!gem)
103 return ERR_PTR(-ENOENT);
105 bpp = drm_format_plane_cpp(cmd->pixel_format, 0);
106 size = (height - 1) * cmd->pitches[0] + width * bpp;
107 size += cmd->offsets[0];
109 if (gem->size < size) {
110 ret = -EINVAL;
111 goto unreference;
114 fb = mtk_drm_framebuffer_init(dev, cmd, gem);
115 if (IS_ERR(fb)) {
116 ret = PTR_ERR(fb);
117 goto unreference;
120 return fb;
122 unreference:
123 drm_gem_object_put_unlocked(gem);
124 return ERR_PTR(ret);