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