drm: Remove users of drm_format_info_plane_cpp
[sfrench/cifs-2.6.git] / drivers / gpu / drm / rockchip / rockchip_drm_fb.c
1 /*
2  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3  * Author:Mark Yao <mark.yao@rock-chips.com>
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/kernel.h>
16 #include <drm/drm.h>
17 #include <drm/drmP.h>
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_gem_framebuffer_helper.h>
21 #include <drm/drm_probe_helper.h>
22
23 #include "rockchip_drm_drv.h"
24 #include "rockchip_drm_fb.h"
25 #include "rockchip_drm_gem.h"
26 #include "rockchip_drm_psr.h"
27
28 static int rockchip_drm_fb_dirty(struct drm_framebuffer *fb,
29                                  struct drm_file *file,
30                                  unsigned int flags, unsigned int color,
31                                  struct drm_clip_rect *clips,
32                                  unsigned int num_clips)
33 {
34         rockchip_drm_psr_flush_all(fb->dev);
35         return 0;
36 }
37
38 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
39         .destroy       = drm_gem_fb_destroy,
40         .create_handle = drm_gem_fb_create_handle,
41         .dirty         = rockchip_drm_fb_dirty,
42 };
43
44 static struct drm_framebuffer *
45 rockchip_fb_alloc(struct drm_device *dev, const struct drm_mode_fb_cmd2 *mode_cmd,
46                   struct drm_gem_object **obj, unsigned int num_planes)
47 {
48         struct drm_framebuffer *fb;
49         int ret;
50         int i;
51
52         fb = kzalloc(sizeof(*fb), GFP_KERNEL);
53         if (!fb)
54                 return ERR_PTR(-ENOMEM);
55
56         drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
57
58         for (i = 0; i < num_planes; i++)
59                 fb->obj[i] = obj[i];
60
61         ret = drm_framebuffer_init(dev, fb, &rockchip_drm_fb_funcs);
62         if (ret) {
63                 DRM_DEV_ERROR(dev->dev,
64                               "Failed to initialize framebuffer: %d\n",
65                               ret);
66                 kfree(fb);
67                 return ERR_PTR(ret);
68         }
69
70         return fb;
71 }
72
73 static struct drm_framebuffer *
74 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
75                         const struct drm_mode_fb_cmd2 *mode_cmd)
76 {
77         const struct drm_format_info *info = drm_get_format_info(dev,
78                                                                  mode_cmd);
79         struct drm_framebuffer *fb;
80         struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
81         struct drm_gem_object *obj;
82         int num_planes = min_t(int, info->num_planes, ROCKCHIP_MAX_FB_BUFFER);
83         int ret;
84         int i;
85
86         for (i = 0; i < num_planes; i++) {
87                 unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
88                 unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
89                 unsigned int min_size;
90
91                 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
92                 if (!obj) {
93                         DRM_DEV_ERROR(dev->dev,
94                                       "Failed to lookup GEM object\n");
95                         ret = -ENXIO;
96                         goto err_gem_object_unreference;
97                 }
98
99                 min_size = (height - 1) * mode_cmd->pitches[i] +
100                         mode_cmd->offsets[i] +
101                         width * info->cpp[i];
102
103                 if (obj->size < min_size) {
104                         drm_gem_object_put_unlocked(obj);
105                         ret = -EINVAL;
106                         goto err_gem_object_unreference;
107                 }
108                 objs[i] = obj;
109         }
110
111         fb = rockchip_fb_alloc(dev, mode_cmd, objs, i);
112         if (IS_ERR(fb)) {
113                 ret = PTR_ERR(fb);
114                 goto err_gem_object_unreference;
115         }
116
117         return fb;
118
119 err_gem_object_unreference:
120         for (i--; i >= 0; i--)
121                 drm_gem_object_put_unlocked(objs[i]);
122         return ERR_PTR(ret);
123 }
124
125 static void
126 rockchip_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
127 {
128         struct drm_device *dev = old_state->dev;
129
130         rockchip_drm_psr_inhibit_get_state(old_state);
131
132         drm_atomic_helper_commit_modeset_disables(dev, old_state);
133
134         drm_atomic_helper_commit_modeset_enables(dev, old_state);
135
136         drm_atomic_helper_commit_planes(dev, old_state,
137                                         DRM_PLANE_COMMIT_ACTIVE_ONLY);
138
139         rockchip_drm_psr_inhibit_put_state(old_state);
140
141         drm_atomic_helper_commit_hw_done(old_state);
142
143         drm_atomic_helper_wait_for_vblanks(dev, old_state);
144
145         drm_atomic_helper_cleanup_planes(dev, old_state);
146 }
147
148 static const struct drm_mode_config_helper_funcs rockchip_mode_config_helpers = {
149         .atomic_commit_tail = rockchip_atomic_helper_commit_tail_rpm,
150 };
151
152 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
153         .fb_create = rockchip_user_fb_create,
154         .output_poll_changed = drm_fb_helper_output_poll_changed,
155         .atomic_check = drm_atomic_helper_check,
156         .atomic_commit = drm_atomic_helper_commit,
157 };
158
159 struct drm_framebuffer *
160 rockchip_drm_framebuffer_init(struct drm_device *dev,
161                               const struct drm_mode_fb_cmd2 *mode_cmd,
162                               struct drm_gem_object *obj)
163 {
164         struct drm_framebuffer *fb;
165
166         fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1);
167         if (IS_ERR(fb))
168                 return ERR_CAST(fb);
169
170         return fb;
171 }
172
173 void rockchip_drm_mode_config_init(struct drm_device *dev)
174 {
175         dev->mode_config.min_width = 0;
176         dev->mode_config.min_height = 0;
177
178         /*
179          * set max width and height as default value(4096x4096).
180          * this value would be used to check framebuffer size limitation
181          * at drm_mode_addfb().
182          */
183         dev->mode_config.max_width = 4096;
184         dev->mode_config.max_height = 4096;
185
186         dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
187         dev->mode_config.helper_private = &rockchip_mode_config_helpers;
188 }