GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / gpu / drm / mediatek / mtk_drm_fb.c
1 /*
2  * Copyright (c) 2015 MediaTek Inc.
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  *
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.
12  */
13
14 #include <drm/drmP.h>
15 #include <drm/drm_crtc_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>
21
22 #include "mtk_drm_drv.h"
23 #include "mtk_drm_fb.h"
24 #include "mtk_drm_gem.h"
25
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,
29 };
30
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)
34 {
35         struct drm_framebuffer *fb;
36         int ret;
37
38         if (drm_format_num_planes(mode->pixel_format) != 1)
39                 return ERR_PTR(-EINVAL);
40
41         fb = kzalloc(sizeof(*fb), GFP_KERNEL);
42         if (!fb)
43                 return ERR_PTR(-ENOMEM);
44
45         drm_helper_mode_fill_fb_struct(dev, fb, mode);
46
47         fb->obj[0] = obj;
48
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);
54         }
55
56         return fb;
57 }
58
59 /*
60  * Wait for any exclusive fence in fb's gem object's reservation object.
61  *
62  * Returns -ERESTARTSYS if interrupted, else 0.
63  */
64 int mtk_fb_wait(struct drm_framebuffer *fb)
65 {
66         struct drm_gem_object *gem;
67         struct reservation_object *resv;
68         long ret;
69
70         if (!fb)
71                 return 0;
72
73         gem = fb->obj[0];
74         if (!gem || !gem->dma_buf || !gem->dma_buf->resv)
75                 return 0;
76
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;
83
84         return 0;
85 }
86
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)
90 {
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;
97
98         if (drm_format_num_planes(cmd->pixel_format) != 1)
99                 return ERR_PTR(-EINVAL);
100
101         gem = drm_gem_object_lookup(file, cmd->handles[0]);
102         if (!gem)
103                 return ERR_PTR(-ENOENT);
104
105         bpp = drm_format_plane_cpp(cmd->pixel_format, 0);
106         size = (height - 1) * cmd->pitches[0] + width * bpp;
107         size += cmd->offsets[0];
108
109         if (gem->size < size) {
110                 ret = -EINVAL;
111                 goto unreference;
112         }
113
114         fb = mtk_drm_framebuffer_init(dev, cmd, gem);
115         if (IS_ERR(fb)) {
116                 ret = PTR_ERR(fb);
117                 goto unreference;
118         }
119
120         return fb;
121
122 unreference:
123         drm_gem_object_put_unlocked(gem);
124         return ERR_PTR(ret);
125 }