xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_fb.c (revision 0f6253180151af85efabd9c64633c2365e1e18b9)
1*0f625318Schs /*	$NetBSD: amdgpu_fb.c,v 1.11 2021/12/20 20:34:58 chs Exp $	*/
2efa246c0Sriastradh 
3efa246c0Sriastradh /*
4efa246c0Sriastradh  * Copyright © 2007 David Airlie
5efa246c0Sriastradh  *
6efa246c0Sriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
7efa246c0Sriastradh  * copy of this software and associated documentation files (the "Software"),
8efa246c0Sriastradh  * to deal in the Software without restriction, including without limitation
9efa246c0Sriastradh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10efa246c0Sriastradh  * and/or sell copies of the Software, and to permit persons to whom the
11efa246c0Sriastradh  * Software is furnished to do so, subject to the following conditions:
12efa246c0Sriastradh  *
13efa246c0Sriastradh  * The above copyright notice and this permission notice (including the next
14efa246c0Sriastradh  * paragraph) shall be included in all copies or substantial portions of the
15efa246c0Sriastradh  * Software.
16efa246c0Sriastradh  *
17efa246c0Sriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18efa246c0Sriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19efa246c0Sriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20efa246c0Sriastradh  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21efa246c0Sriastradh  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22efa246c0Sriastradh  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23efa246c0Sriastradh  * DEALINGS IN THE SOFTWARE.
24efa246c0Sriastradh  *
25efa246c0Sriastradh  * Authors:
26efa246c0Sriastradh  *     David Airlie
27efa246c0Sriastradh  */
2841ec0267Sriastradh 
29efa246c0Sriastradh #include <sys/cdefs.h>
30*0f625318Schs __KERNEL_RCSID(0, "$NetBSD: amdgpu_fb.c,v 1.11 2021/12/20 20:34:58 chs Exp $");
31efa246c0Sriastradh 
32efa246c0Sriastradh #include <linux/module.h>
3341ec0267Sriastradh #include <linux/pm_runtime.h>
34efa246c0Sriastradh #include <linux/slab.h>
3541ec0267Sriastradh #include <linux/vga_switcheroo.h>
36efa246c0Sriastradh 
3741ec0267Sriastradh #include <drm/amdgpu_drm.h>
38efa246c0Sriastradh #include <drm/drm_crtc.h>
39efa246c0Sriastradh #include <drm/drm_crtc_helper.h>
4041ec0267Sriastradh #include <drm/drm_fb_helper.h>
4141ec0267Sriastradh #include <drm/drm_fourcc.h>
4241ec0267Sriastradh 
43efa246c0Sriastradh #include "amdgpu.h"
44efa246c0Sriastradh #include "cikd.h"
4541ec0267Sriastradh #include "amdgpu_gem.h"
46efa246c0Sriastradh 
4741ec0267Sriastradh #include "amdgpu_display.h"
48efa246c0Sriastradh 
490d50c49dSriastradh #ifdef __NetBSD__
500d50c49dSriastradh #include "amdgpufb.h"
510d50c49dSriastradh #endif
520d50c49dSriastradh 
531b46a69aSriastradh #include <linux/nbsd-namespace.h>
541b46a69aSriastradh 
55efa246c0Sriastradh /* object hierarchy -
56efa246c0Sriastradh    this contains a helper + a amdgpu fb
57efa246c0Sriastradh    the helper contains a pointer to amdgpu framebuffer baseclass.
58efa246c0Sriastradh */
5941ec0267Sriastradh 
600caae222Sriastradh #ifndef __NetBSD__
610caae222Sriastradh 
6241ec0267Sriastradh static int
amdgpufb_open(struct fb_info * info,int user)6341ec0267Sriastradh amdgpufb_open(struct fb_info *info, int user)
6441ec0267Sriastradh {
6541ec0267Sriastradh 	struct drm_fb_helper *fb_helper = info->par;
6641ec0267Sriastradh 	int ret = pm_runtime_get_sync(fb_helper->dev->dev);
6741ec0267Sriastradh 	if (ret < 0 && ret != -EACCES) {
6841ec0267Sriastradh 		pm_runtime_mark_last_busy(fb_helper->dev->dev);
6941ec0267Sriastradh 		pm_runtime_put_autosuspend(fb_helper->dev->dev);
7041ec0267Sriastradh 		return ret;
7141ec0267Sriastradh 	}
7241ec0267Sriastradh 	return 0;
7341ec0267Sriastradh }
7441ec0267Sriastradh 
7541ec0267Sriastradh static int
amdgpufb_release(struct fb_info * info,int user)7641ec0267Sriastradh amdgpufb_release(struct fb_info *info, int user)
7741ec0267Sriastradh {
7841ec0267Sriastradh 	struct drm_fb_helper *fb_helper = info->par;
7941ec0267Sriastradh 
8041ec0267Sriastradh 	pm_runtime_mark_last_busy(fb_helper->dev->dev);
8141ec0267Sriastradh 	pm_runtime_put_autosuspend(fb_helper->dev->dev);
8241ec0267Sriastradh 	return 0;
8341ec0267Sriastradh }
84efa246c0Sriastradh 
8541ec0267Sriastradh static const struct fb_ops amdgpufb_ops = {
86efa246c0Sriastradh 	.owner = THIS_MODULE,
8741ec0267Sriastradh 	DRM_FB_HELPER_DEFAULT_OPS,
8841ec0267Sriastradh 	.fb_open = amdgpufb_open,
8941ec0267Sriastradh 	.fb_release = amdgpufb_release,
90efa246c0Sriastradh 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
91efa246c0Sriastradh 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
92efa246c0Sriastradh 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
93efa246c0Sriastradh };
940caae222Sriastradh 
950d50c49dSriastradh #endif
96efa246c0Sriastradh 
97efa246c0Sriastradh 
amdgpu_align_pitch(struct amdgpu_device * adev,int width,int cpp,bool tiled)9841ec0267Sriastradh int amdgpu_align_pitch(struct amdgpu_device *adev, int width, int cpp, bool tiled)
99efa246c0Sriastradh {
100efa246c0Sriastradh 	int aligned = width;
101efa246c0Sriastradh 	int pitch_mask = 0;
102efa246c0Sriastradh 
10341ec0267Sriastradh 	switch (cpp) {
104efa246c0Sriastradh 	case 1:
105efa246c0Sriastradh 		pitch_mask = 255;
106efa246c0Sriastradh 		break;
107efa246c0Sriastradh 	case 2:
108efa246c0Sriastradh 		pitch_mask = 127;
109efa246c0Sriastradh 		break;
110efa246c0Sriastradh 	case 3:
111efa246c0Sriastradh 	case 4:
112efa246c0Sriastradh 		pitch_mask = 63;
113efa246c0Sriastradh 		break;
114efa246c0Sriastradh 	}
115efa246c0Sriastradh 
116efa246c0Sriastradh 	aligned += pitch_mask;
117efa246c0Sriastradh 	aligned &= ~pitch_mask;
11841ec0267Sriastradh 	return aligned * cpp;
119efa246c0Sriastradh }
120efa246c0Sriastradh 
amdgpufb_destroy_pinned_object(struct drm_gem_object * gobj)121efa246c0Sriastradh static void amdgpufb_destroy_pinned_object(struct drm_gem_object *gobj)
122efa246c0Sriastradh {
12341ec0267Sriastradh 	struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
124efa246c0Sriastradh 	int ret;
125efa246c0Sriastradh 
12641ec0267Sriastradh 	ret = amdgpu_bo_reserve(abo, true);
127efa246c0Sriastradh 	if (likely(ret == 0)) {
12841ec0267Sriastradh 		amdgpu_bo_kunmap(abo);
12941ec0267Sriastradh 		amdgpu_bo_unpin(abo);
13041ec0267Sriastradh 		amdgpu_bo_unreserve(abo);
131efa246c0Sriastradh 	}
13241ec0267Sriastradh 	drm_gem_object_put_unlocked(gobj);
133efa246c0Sriastradh }
134efa246c0Sriastradh 
amdgpufb_create_pinned_object(struct amdgpu_fbdev * rfbdev,struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object ** gobj_p)135efa246c0Sriastradh static int amdgpufb_create_pinned_object(struct amdgpu_fbdev *rfbdev,
136efa246c0Sriastradh 					 struct drm_mode_fb_cmd2 *mode_cmd,
137efa246c0Sriastradh 					 struct drm_gem_object **gobj_p)
138efa246c0Sriastradh {
13941ec0267Sriastradh 	const struct drm_format_info *info;
140efa246c0Sriastradh 	struct amdgpu_device *adev = rfbdev->adev;
141efa246c0Sriastradh 	struct drm_gem_object *gobj = NULL;
14241ec0267Sriastradh 	struct amdgpu_bo *abo = NULL;
143efa246c0Sriastradh 	bool fb_tiled = false; /* useful for testing */
14441ec0267Sriastradh 	u32 tiling_flags = 0, domain;
145efa246c0Sriastradh 	int ret;
146efa246c0Sriastradh 	int aligned_size, size;
147efa246c0Sriastradh 	int height = mode_cmd->height;
14841ec0267Sriastradh 	u32 cpp;
14941ec0267Sriastradh 	u64 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
15041ec0267Sriastradh 			       AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS     |
15141ec0267Sriastradh 			       AMDGPU_GEM_CREATE_VRAM_CLEARED 	     |
15241ec0267Sriastradh 			       AMDGPU_GEM_CREATE_CPU_GTT_USWC;
153efa246c0Sriastradh 
15441ec0267Sriastradh 	info = drm_get_format_info(adev->ddev, mode_cmd);
15541ec0267Sriastradh 	cpp = info->cpp[0];
156efa246c0Sriastradh 
157efa246c0Sriastradh 	/* need to align pitch with crtc limits */
15841ec0267Sriastradh 	mode_cmd->pitches[0] = amdgpu_align_pitch(adev, mode_cmd->width, cpp,
15941ec0267Sriastradh 						  fb_tiled);
16041ec0267Sriastradh 	domain = amdgpu_display_supported_domains(adev, flags);
161efa246c0Sriastradh 	height = ALIGN(mode_cmd->height, 8);
162efa246c0Sriastradh 	size = mode_cmd->pitches[0] * height;
163efa246c0Sriastradh 	aligned_size = ALIGN(size, PAGE_SIZE);
16441ec0267Sriastradh 	ret = amdgpu_gem_object_create(adev, aligned_size, 0, domain, flags,
16541ec0267Sriastradh 				       ttm_bo_type_kernel, NULL, &gobj);
166efa246c0Sriastradh 	if (ret) {
16741ec0267Sriastradh 		pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
168efa246c0Sriastradh 		return -ENOMEM;
169efa246c0Sriastradh 	}
17041ec0267Sriastradh 	abo = gem_to_amdgpu_bo(gobj);
171efa246c0Sriastradh 
172efa246c0Sriastradh 	if (fb_tiled)
173efa246c0Sriastradh 		tiling_flags = AMDGPU_TILING_SET(ARRAY_MODE, GRPH_ARRAY_2D_TILED_THIN1);
174efa246c0Sriastradh 
17541ec0267Sriastradh 	ret = amdgpu_bo_reserve(abo, false);
176efa246c0Sriastradh 	if (unlikely(ret != 0))
177efa246c0Sriastradh 		goto out_unref;
178efa246c0Sriastradh 
179efa246c0Sriastradh 	if (tiling_flags) {
18041ec0267Sriastradh 		ret = amdgpu_bo_set_tiling_flags(abo,
181efa246c0Sriastradh 						 tiling_flags);
182efa246c0Sriastradh 		if (ret)
183efa246c0Sriastradh 			dev_err(adev->dev, "FB failed to set tiling flags\n");
184efa246c0Sriastradh 	}
185efa246c0Sriastradh 
18641ec0267Sriastradh 	ret = amdgpu_bo_pin(abo, domain);
187efa246c0Sriastradh 	if (ret) {
18841ec0267Sriastradh 		amdgpu_bo_unreserve(abo);
189efa246c0Sriastradh 		goto out_unref;
190efa246c0Sriastradh 	}
19141ec0267Sriastradh 
19241ec0267Sriastradh 	ret = amdgpu_ttm_alloc_gart(&abo->tbo);
19341ec0267Sriastradh 	if (ret) {
19441ec0267Sriastradh 		amdgpu_bo_unreserve(abo);
19541ec0267Sriastradh 		dev_err(adev->dev, "%p bind failed\n", abo);
19641ec0267Sriastradh 		goto out_unref;
19741ec0267Sriastradh 	}
19841ec0267Sriastradh 
19941ec0267Sriastradh 	ret = amdgpu_bo_kmap(abo, NULL);
20041ec0267Sriastradh 	amdgpu_bo_unreserve(abo);
201efa246c0Sriastradh 	if (ret) {
202efa246c0Sriastradh 		goto out_unref;
203efa246c0Sriastradh 	}
204efa246c0Sriastradh 
205efa246c0Sriastradh 	*gobj_p = gobj;
206efa246c0Sriastradh 	return 0;
207efa246c0Sriastradh out_unref:
208efa246c0Sriastradh 	amdgpufb_destroy_pinned_object(gobj);
209efa246c0Sriastradh 	*gobj_p = NULL;
210efa246c0Sriastradh 	return ret;
211efa246c0Sriastradh }
212efa246c0Sriastradh 
amdgpufb_create(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)213efa246c0Sriastradh static int amdgpufb_create(struct drm_fb_helper *helper,
214efa246c0Sriastradh 			   struct drm_fb_helper_surface_size *sizes)
215efa246c0Sriastradh {
216efa246c0Sriastradh 	struct amdgpu_fbdev *rfbdev = (struct amdgpu_fbdev *)helper;
217efa246c0Sriastradh 	struct amdgpu_device *adev = rfbdev->adev;
218efa246c0Sriastradh 	struct fb_info *info;
219efa246c0Sriastradh 	struct drm_framebuffer *fb = NULL;
220efa246c0Sriastradh 	struct drm_mode_fb_cmd2 mode_cmd;
221efa246c0Sriastradh 	struct drm_gem_object *gobj = NULL;
22241ec0267Sriastradh 	struct amdgpu_bo *abo = NULL;
223efa246c0Sriastradh 	int ret;
224efa246c0Sriastradh 	unsigned long tmp;
225efa246c0Sriastradh 
226efa246c0Sriastradh 	mode_cmd.width = sizes->surface_width;
227efa246c0Sriastradh 	mode_cmd.height = sizes->surface_height;
228efa246c0Sriastradh 
229efa246c0Sriastradh 	if (sizes->surface_bpp == 24)
230efa246c0Sriastradh 		sizes->surface_bpp = 32;
231efa246c0Sriastradh 
232efa246c0Sriastradh 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
233efa246c0Sriastradh 							  sizes->surface_depth);
234efa246c0Sriastradh 
235efa246c0Sriastradh 	ret = amdgpufb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
236efa246c0Sriastradh 	if (ret) {
237efa246c0Sriastradh 		DRM_ERROR("failed to create fbcon object %d\n", ret);
238efa246c0Sriastradh 		return ret;
239efa246c0Sriastradh 	}
240efa246c0Sriastradh 
24141ec0267Sriastradh 	abo = gem_to_amdgpu_bo(gobj);
242efa246c0Sriastradh 
2430caae222Sriastradh #ifndef __NetBSD__
244efa246c0Sriastradh 	/* okay we have an object now allocate the framebuffer */
245efa246c0Sriastradh 	info = drm_fb_helper_alloc_fbi(helper);
246efa246c0Sriastradh 	if (IS_ERR(info)) {
247efa246c0Sriastradh 		ret = PTR_ERR(info);
24841ec0267Sriastradh 		goto out;
249efa246c0Sriastradh 	}
2500caae222Sriastradh #endif
251efa246c0Sriastradh 
25241ec0267Sriastradh 	ret = amdgpu_display_framebuffer_init(adev->ddev, &rfbdev->rfb,
25341ec0267Sriastradh 					      &mode_cmd, gobj);
254efa246c0Sriastradh 	if (ret) {
255efa246c0Sriastradh 		DRM_ERROR("failed to initialize framebuffer %d\n", ret);
25641ec0267Sriastradh 		goto out;
257efa246c0Sriastradh 	}
258efa246c0Sriastradh 
259efa246c0Sriastradh 	fb = &rfbdev->rfb.base;
260efa246c0Sriastradh 
261efa246c0Sriastradh 	/* setup helper */
262efa246c0Sriastradh 	rfbdev->helper.fb = fb;
263efa246c0Sriastradh 
2640caae222Sriastradh #ifdef __NetBSD__
2650caae222Sriastradh     {
2660caae222Sriastradh 	static const struct amdgpufb_attach_args zero_afa;
2670caae222Sriastradh 	struct amdgpufb_attach_args afa = zero_afa;
2680caae222Sriastradh 
2690caae222Sriastradh 	__USE(tmp);
2700caae222Sriastradh 	__USE(info);
2710caae222Sriastradh 
2720caae222Sriastradh 	afa.afa_fb_helper = helper;
2730caae222Sriastradh 	afa.afa_fb_sizes = *sizes;
2740caae222Sriastradh 	afa.afa_fb_ptr = amdgpu_bo_kptr(abo);
2750caae222Sriastradh 	afa.afa_fb_linebytes = mode_cmd.pitches[0];
2760caae222Sriastradh 
277*0f625318Schs 	KERNEL_LOCK(1, NULL);
2780caae222Sriastradh 	helper->fbdev = config_found(adev->ddev->dev, &afa, NULL,
2790caae222Sriastradh 	    CFARGS(.iattr = "amdgpufbbus"));
280*0f625318Schs 	KERNEL_UNLOCK_ONE(NULL);
2810caae222Sriastradh 	if (helper->fbdev == NULL) {
2820caae222Sriastradh 		DRM_ERROR("failed to attach amdgpufb\n");
2830caae222Sriastradh 		goto out;
2840caae222Sriastradh 	}
2850caae222Sriastradh     }
2860caae222Sriastradh #else  /* __NetBSD__ */
287efa246c0Sriastradh 	info->fbops = &amdgpufb_ops;
288efa246c0Sriastradh 
28941ec0267Sriastradh 	tmp = amdgpu_bo_gpu_offset(abo) - adev->gmc.vram_start;
29041ec0267Sriastradh 	info->fix.smem_start = adev->gmc.aper_base + tmp;
29141ec0267Sriastradh 	info->fix.smem_len = amdgpu_bo_size(abo);
29241ec0267Sriastradh 	info->screen_base = amdgpu_bo_kptr(abo);
29341ec0267Sriastradh 	info->screen_size = amdgpu_bo_size(abo);
294efa246c0Sriastradh 
29541ec0267Sriastradh 	drm_fb_helper_fill_info(info, &rfbdev->helper, sizes);
296efa246c0Sriastradh 
297efa246c0Sriastradh 	/* setup aperture base/size for vesafb takeover */
298efa246c0Sriastradh 	info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base;
29941ec0267Sriastradh 	info->apertures->ranges[0].size = adev->gmc.aper_size;
300efa246c0Sriastradh 
301efa246c0Sriastradh 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
302efa246c0Sriastradh 
303efa246c0Sriastradh 	if (info->screen_base == NULL) {
304efa246c0Sriastradh 		ret = -ENOSPC;
30541ec0267Sriastradh 		goto out;
306efa246c0Sriastradh 	}
307efa246c0Sriastradh 
308efa246c0Sriastradh 	DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
30941ec0267Sriastradh 	DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)adev->gmc.aper_base);
31041ec0267Sriastradh 	DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(abo));
31141ec0267Sriastradh 	DRM_INFO("fb depth is %d\n", fb->format->depth);
312efa246c0Sriastradh 	DRM_INFO("   pitch is %d\n", fb->pitches[0]);
313efa246c0Sriastradh 
314efa246c0Sriastradh 	vga_switcheroo_client_fb_set(adev->ddev->pdev, info);
3150d50c49dSriastradh #endif
316efa246c0Sriastradh 	return 0;
317efa246c0Sriastradh 
31841ec0267Sriastradh out:
31941ec0267Sriastradh 	if (abo) {
320efa246c0Sriastradh 
321efa246c0Sriastradh 	}
322efa246c0Sriastradh 	if (fb && ret) {
32341ec0267Sriastradh 		drm_gem_object_put_unlocked(gobj);
324efa246c0Sriastradh 		drm_framebuffer_unregister_private(fb);
325efa246c0Sriastradh 		drm_framebuffer_cleanup(fb);
326efa246c0Sriastradh 		kfree(fb);
327efa246c0Sriastradh 	}
328efa246c0Sriastradh 	return ret;
329efa246c0Sriastradh }
330efa246c0Sriastradh 
amdgpu_fbdev_destroy(struct drm_device * dev,struct amdgpu_fbdev * rfbdev)331efa246c0Sriastradh static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev)
332efa246c0Sriastradh {
333efa246c0Sriastradh 	struct amdgpu_framebuffer *rfb = &rfbdev->rfb;
334efa246c0Sriastradh 
335efa246c0Sriastradh 	drm_fb_helper_unregister_fbi(&rfbdev->helper);
336efa246c0Sriastradh 
33741ec0267Sriastradh 	if (rfb->base.obj[0]) {
33841ec0267Sriastradh 		amdgpufb_destroy_pinned_object(rfb->base.obj[0]);
33941ec0267Sriastradh 		rfb->base.obj[0] = NULL;
340efa246c0Sriastradh 		drm_framebuffer_unregister_private(&rfb->base);
341efa246c0Sriastradh 		drm_framebuffer_cleanup(&rfb->base);
34241ec0267Sriastradh 	}
34341ec0267Sriastradh 	drm_fb_helper_fini(&rfbdev->helper);
344efa246c0Sriastradh 
345efa246c0Sriastradh 	return 0;
346efa246c0Sriastradh }
347efa246c0Sriastradh 
348efa246c0Sriastradh static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = {
349efa246c0Sriastradh 	.fb_probe = amdgpufb_create,
350efa246c0Sriastradh };
351efa246c0Sriastradh 
amdgpu_fbdev_init(struct amdgpu_device * adev)352efa246c0Sriastradh int amdgpu_fbdev_init(struct amdgpu_device *adev)
353efa246c0Sriastradh {
354efa246c0Sriastradh 	struct amdgpu_fbdev *rfbdev;
355efa246c0Sriastradh 	int bpp_sel = 32;
356efa246c0Sriastradh 	int ret;
357efa246c0Sriastradh 
358efa246c0Sriastradh 	/* don't init fbdev on hw without DCE */
359efa246c0Sriastradh 	if (!adev->mode_info.mode_config_initialized)
360efa246c0Sriastradh 		return 0;
361efa246c0Sriastradh 
36241ec0267Sriastradh 	/* don't init fbdev if there are no connectors */
36341ec0267Sriastradh 	if (list_empty(&adev->ddev->mode_config.connector_list))
36441ec0267Sriastradh 		return 0;
36541ec0267Sriastradh 
366efa246c0Sriastradh 	/* select 8 bpp console on low vram cards */
36741ec0267Sriastradh 	if (adev->gmc.real_vram_size <= (32*1024*1024))
368efa246c0Sriastradh 		bpp_sel = 8;
369efa246c0Sriastradh 
370efa246c0Sriastradh 	rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL);
371efa246c0Sriastradh 	if (!rfbdev)
372efa246c0Sriastradh 		return -ENOMEM;
373efa246c0Sriastradh 
374efa246c0Sriastradh 	rfbdev->adev = adev;
375efa246c0Sriastradh 	adev->mode_info.rfbdev = rfbdev;
376efa246c0Sriastradh 
377efa246c0Sriastradh 	drm_fb_helper_prepare(adev->ddev, &rfbdev->helper,
378efa246c0Sriastradh 			&amdgpu_fb_helper_funcs);
379efa246c0Sriastradh 
380efa246c0Sriastradh 	ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper,
381efa246c0Sriastradh 				 AMDGPUFB_CONN_LIMIT);
382efa246c0Sriastradh 	if (ret) {
383efa246c0Sriastradh 		kfree(rfbdev);
384efa246c0Sriastradh 		return ret;
385efa246c0Sriastradh 	}
386efa246c0Sriastradh 
387efa246c0Sriastradh 	drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
388efa246c0Sriastradh 
389efa246c0Sriastradh 	/* disable all the possible outputs/crtcs before entering KMS mode */
39041ec0267Sriastradh 	if (!amdgpu_device_has_dc_support(adev))
391efa246c0Sriastradh 		drm_helper_disable_unused_functions(adev->ddev);
392efa246c0Sriastradh 
393efa246c0Sriastradh 	drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
394efa246c0Sriastradh 	return 0;
395efa246c0Sriastradh }
396efa246c0Sriastradh 
amdgpu_fbdev_fini(struct amdgpu_device * adev)397efa246c0Sriastradh void amdgpu_fbdev_fini(struct amdgpu_device *adev)
398efa246c0Sriastradh {
399efa246c0Sriastradh 	if (!adev->mode_info.rfbdev)
400efa246c0Sriastradh 		return;
401efa246c0Sriastradh 
402efa246c0Sriastradh 	amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev);
403efa246c0Sriastradh 	kfree(adev->mode_info.rfbdev);
404efa246c0Sriastradh 	adev->mode_info.rfbdev = NULL;
405efa246c0Sriastradh }
406efa246c0Sriastradh 
amdgpu_fbdev_set_suspend(struct amdgpu_device * adev,int state)407efa246c0Sriastradh void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state)
408efa246c0Sriastradh {
409efa246c0Sriastradh 	if (adev->mode_info.rfbdev)
41041ec0267Sriastradh 		drm_fb_helper_set_suspend_unlocked(&adev->mode_info.rfbdev->helper,
411efa246c0Sriastradh 						   state);
412efa246c0Sriastradh }
413efa246c0Sriastradh 
amdgpu_fbdev_total_size(struct amdgpu_device * adev)414efa246c0Sriastradh int amdgpu_fbdev_total_size(struct amdgpu_device *adev)
415efa246c0Sriastradh {
416efa246c0Sriastradh 	struct amdgpu_bo *robj;
417efa246c0Sriastradh 	int size = 0;
418efa246c0Sriastradh 
419efa246c0Sriastradh 	if (!adev->mode_info.rfbdev)
420efa246c0Sriastradh 		return 0;
421efa246c0Sriastradh 
42241ec0267Sriastradh 	robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.base.obj[0]);
423efa246c0Sriastradh 	size += amdgpu_bo_size(robj);
424efa246c0Sriastradh 	return size;
425efa246c0Sriastradh }
426efa246c0Sriastradh 
amdgpu_fbdev_robj_is_fb(struct amdgpu_device * adev,struct amdgpu_bo * robj)427efa246c0Sriastradh bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj)
428efa246c0Sriastradh {
429efa246c0Sriastradh 	if (!adev->mode_info.rfbdev)
430efa246c0Sriastradh 		return false;
43141ec0267Sriastradh 	if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.base.obj[0]))
432efa246c0Sriastradh 		return true;
433efa246c0Sriastradh 	return false;
434efa246c0Sriastradh }
435