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 
9 #include <drm/drm.h>
10 #include <drm/drm_atomic.h>
11 #include <drm/drm_damage_helper.h>
12 #include <drm/drm_fb_helper.h>
13 #include <drm/drm_fourcc.h>
14 #include <drm/drm_gem_framebuffer_helper.h>
15 #include <drm/drm_probe_helper.h>
16 
17 #include "rockchip_drm_drv.h"
18 #include "rockchip_drm_fb.h"
19 #include "rockchip_drm_gem.h"
20 #include "rockchip_drm_psr.h"
21 
22 static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
23 	.destroy       = drm_gem_fb_destroy,
24 	.create_handle = drm_gem_fb_create_handle,
25 	.dirty	       = drm_atomic_helper_dirtyfb,
26 };
27 
28 static struct drm_framebuffer *
29 rockchip_fb_alloc(struct drm_device *dev, const struct drm_mode_fb_cmd2 *mode_cmd,
30 		  struct drm_gem_object **obj, unsigned int num_planes)
31 {
32 	struct drm_framebuffer *fb;
33 	int ret;
34 	int i;
35 
36 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
37 	if (!fb)
38 		return ERR_PTR(-ENOMEM);
39 
40 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
41 
42 	for (i = 0; i < num_planes; i++)
43 		fb->obj[i] = obj[i];
44 
45 	ret = drm_framebuffer_init(dev, fb, &rockchip_drm_fb_funcs);
46 	if (ret) {
47 		DRM_DEV_ERROR(dev->dev,
48 			      "Failed to initialize framebuffer: %d\n",
49 			      ret);
50 		kfree(fb);
51 		return ERR_PTR(ret);
52 	}
53 
54 	return fb;
55 }
56 
57 static struct drm_framebuffer *
58 rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
59 			const struct drm_mode_fb_cmd2 *mode_cmd)
60 {
61 	const struct drm_format_info *info = drm_get_format_info(dev,
62 								 mode_cmd);
63 	struct drm_framebuffer *fb;
64 	struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
65 	struct drm_gem_object *obj;
66 	int num_planes = min_t(int, info->num_planes, ROCKCHIP_MAX_FB_BUFFER);
67 	int ret;
68 	int i;
69 
70 	for (i = 0; i < num_planes; i++) {
71 		unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
72 		unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
73 		unsigned int min_size;
74 
75 		obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
76 		if (!obj) {
77 			DRM_DEV_ERROR(dev->dev,
78 				      "Failed to lookup GEM object\n");
79 			ret = -ENXIO;
80 			goto err_gem_object_unreference;
81 		}
82 
83 		min_size = (height - 1) * mode_cmd->pitches[i] +
84 			mode_cmd->offsets[i] +
85 			width * info->cpp[i];
86 
87 		if (obj->size < min_size) {
88 			drm_gem_object_put_unlocked(obj);
89 			ret = -EINVAL;
90 			goto err_gem_object_unreference;
91 		}
92 		objs[i] = obj;
93 	}
94 
95 	fb = rockchip_fb_alloc(dev, mode_cmd, objs, i);
96 	if (IS_ERR(fb)) {
97 		ret = PTR_ERR(fb);
98 		goto err_gem_object_unreference;
99 	}
100 
101 	return fb;
102 
103 err_gem_object_unreference:
104 	for (i--; i >= 0; i--)
105 		drm_gem_object_put_unlocked(objs[i]);
106 	return ERR_PTR(ret);
107 }
108 
109 static void
110 rockchip_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
111 {
112 	struct drm_device *dev = old_state->dev;
113 
114 	rockchip_drm_psr_inhibit_get_state(old_state);
115 
116 	drm_atomic_helper_commit_modeset_disables(dev, old_state);
117 
118 	drm_atomic_helper_commit_modeset_enables(dev, old_state);
119 
120 	drm_atomic_helper_commit_planes(dev, old_state,
121 					DRM_PLANE_COMMIT_ACTIVE_ONLY);
122 
123 	rockchip_drm_psr_inhibit_put_state(old_state);
124 
125 	drm_atomic_helper_commit_hw_done(old_state);
126 
127 	drm_atomic_helper_wait_for_vblanks(dev, old_state);
128 
129 	drm_atomic_helper_cleanup_planes(dev, old_state);
130 }
131 
132 static const struct drm_mode_config_helper_funcs rockchip_mode_config_helpers = {
133 	.atomic_commit_tail = rockchip_atomic_helper_commit_tail_rpm,
134 };
135 
136 static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
137 	.fb_create = rockchip_user_fb_create,
138 	.output_poll_changed = drm_fb_helper_output_poll_changed,
139 	.atomic_check = drm_atomic_helper_check,
140 	.atomic_commit = drm_atomic_helper_commit,
141 };
142 
143 struct drm_framebuffer *
144 rockchip_drm_framebuffer_init(struct drm_device *dev,
145 			      const struct drm_mode_fb_cmd2 *mode_cmd,
146 			      struct drm_gem_object *obj)
147 {
148 	struct drm_framebuffer *fb;
149 
150 	fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1);
151 	if (IS_ERR(fb))
152 		return ERR_CAST(fb);
153 
154 	return fb;
155 }
156 
157 void rockchip_drm_mode_config_init(struct drm_device *dev)
158 {
159 	dev->mode_config.min_width = 0;
160 	dev->mode_config.min_height = 0;
161 
162 	/*
163 	 * set max width and height as default value(4096x4096).
164 	 * this value would be used to check framebuffer size limitation
165 	 * at drm_mode_addfb().
166 	 */
167 	dev->mode_config.max_width = 4096;
168 	dev->mode_config.max_height = 4096;
169 
170 	dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
171 	dev->mode_config.helper_private = &rockchip_mode_config_helpers;
172 }
173