1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Hisilicon Hibmc SoC drm driver
3  *
4  * Based on the bochs drm driver.
5  *
6  * Copyright (c) 2016 Huawei Limited.
7  *
8  * Author:
9  *	Rongrong Zou <zourongrong@huawei.com>
10  *	Rongrong Zou <zourongrong@gmail.com>
11  *	Jianhua Li <lijianhua@huawei.com>
12  */
13 
14 #include <drm/drm_crtc.h>
15 #include <drm/drm_fb_helper.h>
16 #include <drm/drm_fourcc.h>
17 #include <drm/drm_gem_vram_helper.h>
18 #include <drm/drm_probe_helper.h>
19 
20 #include "hibmc_drm_drv.h"
21 
hibmcfb_create_object(struct hibmc_drm_private * priv,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object ** gobj_p)22 static int hibmcfb_create_object(
23 				struct hibmc_drm_private *priv,
24 				const struct drm_mode_fb_cmd2 *mode_cmd,
25 				struct drm_gem_object **gobj_p)
26 {
27 	struct drm_gem_object *gobj;
28 	struct drm_device *dev = priv->dev;
29 	u32 size;
30 	int ret = 0;
31 
32 	size = mode_cmd->pitches[0] * mode_cmd->height;
33 	ret = hibmc_gem_create(dev, size, true, &gobj);
34 	if (ret)
35 		return ret;
36 
37 	*gobj_p = gobj;
38 	return ret;
39 }
40 
41 static struct fb_ops hibmc_drm_fb_ops = {
42 	.owner = THIS_MODULE,
43 	.fb_check_var = drm_fb_helper_check_var,
44 	.fb_set_par = drm_fb_helper_set_par,
45 	.fb_fillrect = drm_fb_helper_sys_fillrect,
46 	.fb_copyarea = drm_fb_helper_sys_copyarea,
47 	.fb_imageblit = drm_fb_helper_sys_imageblit,
48 	.fb_pan_display = drm_fb_helper_pan_display,
49 	.fb_blank = drm_fb_helper_blank,
50 	.fb_setcmap = drm_fb_helper_setcmap,
51 };
52 
hibmc_drm_fb_create(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)53 static int hibmc_drm_fb_create(struct drm_fb_helper *helper,
54 			       struct drm_fb_helper_surface_size *sizes)
55 {
56 	struct hibmc_fbdev *hi_fbdev =
57 		container_of(helper, struct hibmc_fbdev, helper);
58 	struct hibmc_drm_private *priv = helper->dev->dev_private;
59 	struct fb_info *info;
60 	struct drm_mode_fb_cmd2 mode_cmd;
61 	struct drm_gem_object *gobj = NULL;
62 	int ret = 0;
63 	size_t size;
64 	unsigned int bytes_per_pixel;
65 	struct drm_gem_vram_object *gbo = NULL;
66 	void *base;
67 
68 	DRM_DEBUG_DRIVER("surface width(%d), height(%d) and bpp(%d)\n",
69 			 sizes->surface_width, sizes->surface_height,
70 			 sizes->surface_bpp);
71 
72 	bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
73 
74 	mode_cmd.width = sizes->surface_width;
75 	mode_cmd.height = sizes->surface_height;
76 	mode_cmd.pitches[0] = mode_cmd.width * bytes_per_pixel;
77 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
78 							  sizes->surface_depth);
79 
80 	size = PAGE_ALIGN(mode_cmd.pitches[0] * mode_cmd.height);
81 
82 	ret = hibmcfb_create_object(priv, &mode_cmd, &gobj);
83 	if (ret) {
84 		DRM_ERROR("failed to create fbcon backing object: %d\n", ret);
85 		return -ENOMEM;
86 	}
87 
88 	gbo = drm_gem_vram_of_gem(gobj);
89 
90 	ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
91 	if (ret) {
92 		DRM_ERROR("failed to pin fbcon: %d\n", ret);
93 		goto out_unref_gem;
94 	}
95 
96 	base = drm_gem_vram_kmap(gbo, true, NULL);
97 	if (IS_ERR(base)) {
98 		ret = PTR_ERR(base);
99 		DRM_ERROR("failed to kmap fbcon: %d\n", ret);
100 		goto out_unpin_bo;
101 	}
102 
103 	info = drm_fb_helper_alloc_fbi(helper);
104 	if (IS_ERR(info)) {
105 		ret = PTR_ERR(info);
106 		DRM_ERROR("failed to allocate fbi: %d\n", ret);
107 		goto out_release_fbi;
108 	}
109 
110 	hi_fbdev->fb = hibmc_framebuffer_init(priv->dev, &mode_cmd, gobj);
111 	if (IS_ERR(hi_fbdev->fb)) {
112 		ret = PTR_ERR(hi_fbdev->fb);
113 		hi_fbdev->fb = NULL;
114 		DRM_ERROR("failed to initialize framebuffer: %d\n", ret);
115 		goto out_release_fbi;
116 	}
117 
118 	priv->fbdev->size = size;
119 	hi_fbdev->helper.fb = &hi_fbdev->fb->fb;
120 
121 	info->fbops = &hibmc_drm_fb_ops;
122 
123 	drm_fb_helper_fill_info(info, &priv->fbdev->helper, sizes);
124 
125 	info->screen_base = base;
126 	info->screen_size = size;
127 
128 	info->fix.smem_start = gbo->bo.mem.bus.offset + gbo->bo.mem.bus.base;
129 	info->fix.smem_len = size;
130 	return 0;
131 
132 out_release_fbi:
133 	drm_gem_vram_kunmap(gbo);
134 out_unpin_bo:
135 	drm_gem_vram_unpin(gbo);
136 out_unref_gem:
137 	drm_gem_object_put_unlocked(gobj);
138 
139 	return ret;
140 }
141 
hibmc_fbdev_destroy(struct hibmc_fbdev * fbdev)142 static void hibmc_fbdev_destroy(struct hibmc_fbdev *fbdev)
143 {
144 	struct hibmc_framebuffer *gfb = fbdev->fb;
145 	struct drm_fb_helper *fbh = &fbdev->helper;
146 
147 	drm_fb_helper_unregister_fbi(fbh);
148 
149 	drm_fb_helper_fini(fbh);
150 
151 	if (gfb)
152 		drm_framebuffer_put(&gfb->fb);
153 }
154 
155 static const struct drm_fb_helper_funcs hibmc_fbdev_helper_funcs = {
156 	.fb_probe = hibmc_drm_fb_create,
157 };
158 
hibmc_fbdev_init(struct hibmc_drm_private * priv)159 int hibmc_fbdev_init(struct hibmc_drm_private *priv)
160 {
161 	int ret;
162 	struct fb_var_screeninfo *var;
163 	struct fb_fix_screeninfo *fix;
164 	struct hibmc_fbdev *hifbdev;
165 
166 	hifbdev = devm_kzalloc(priv->dev->dev, sizeof(*hifbdev), GFP_KERNEL);
167 	if (!hifbdev) {
168 		DRM_ERROR("failed to allocate hibmc_fbdev\n");
169 		return -ENOMEM;
170 	}
171 
172 	priv->fbdev = hifbdev;
173 	drm_fb_helper_prepare(priv->dev, &hifbdev->helper,
174 			      &hibmc_fbdev_helper_funcs);
175 
176 	/* Now just one crtc and one channel */
177 	ret = drm_fb_helper_init(priv->dev, &hifbdev->helper, 1);
178 	if (ret) {
179 		DRM_ERROR("failed to initialize fb helper: %d\n", ret);
180 		return ret;
181 	}
182 
183 	ret = drm_fb_helper_single_add_all_connectors(&hifbdev->helper);
184 	if (ret) {
185 		DRM_ERROR("failed to add all connectors: %d\n", ret);
186 		goto fini;
187 	}
188 
189 	ret = drm_fb_helper_initial_config(&hifbdev->helper, 16);
190 	if (ret) {
191 		DRM_ERROR("failed to setup initial conn config: %d\n", ret);
192 		goto fini;
193 	}
194 
195 	var = &hifbdev->helper.fbdev->var;
196 	fix = &hifbdev->helper.fbdev->fix;
197 
198 	DRM_DEBUG_DRIVER("Member of info->var is :\n"
199 			 "xres=%d\n"
200 			 "yres=%d\n"
201 			 "xres_virtual=%d\n"
202 			 "yres_virtual=%d\n"
203 			 "xoffset=%d\n"
204 			 "yoffset=%d\n"
205 			 "bits_per_pixel=%d\n"
206 			 "...\n", var->xres, var->yres, var->xres_virtual,
207 			 var->yres_virtual, var->xoffset, var->yoffset,
208 			 var->bits_per_pixel);
209 	DRM_DEBUG_DRIVER("Member of info->fix is :\n"
210 			 "smem_start=%lx\n"
211 			 "smem_len=%d\n"
212 			 "type=%d\n"
213 			 "type_aux=%d\n"
214 			 "visual=%d\n"
215 			 "xpanstep=%d\n"
216 			 "ypanstep=%d\n"
217 			 "ywrapstep=%d\n"
218 			 "line_length=%d\n"
219 			 "accel=%d\n"
220 			 "capabilities=%d\n"
221 			 "...\n", fix->smem_start, fix->smem_len, fix->type,
222 			 fix->type_aux, fix->visual, fix->xpanstep,
223 			 fix->ypanstep, fix->ywrapstep, fix->line_length,
224 			 fix->accel, fix->capabilities);
225 
226 	return 0;
227 
228 fini:
229 	drm_fb_helper_fini(&hifbdev->helper);
230 	return ret;
231 }
232 
hibmc_fbdev_fini(struct hibmc_drm_private * priv)233 void hibmc_fbdev_fini(struct hibmc_drm_private *priv)
234 {
235 	if (!priv->fbdev)
236 		return;
237 
238 	hibmc_fbdev_destroy(priv->fbdev);
239 	priv->fbdev = NULL;
240 }
241