Home | History | Annotate | Line # | Download | only in amdgpu
amdgpu_fb.c revision 1.8
      1 /*	$NetBSD: amdgpu_fb.c,v 1.8 2021/12/18 23:44:58 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright  2007 David Airlie
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     23  * DEALINGS IN THE SOFTWARE.
     24  *
     25  * Authors:
     26  *     David Airlie
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: amdgpu_fb.c,v 1.8 2021/12/18 23:44:58 riastradh Exp $");
     31 
     32 #include <linux/module.h>
     33 #include <linux/pm_runtime.h>
     34 #include <linux/slab.h>
     35 #include <linux/vga_switcheroo.h>
     36 
     37 #include <drm/amdgpu_drm.h>
     38 #include <drm/drm_crtc.h>
     39 #include <drm/drm_crtc_helper.h>
     40 #include <drm/drm_fb_helper.h>
     41 #include <drm/drm_fourcc.h>
     42 
     43 #include "amdgpu.h"
     44 #include "cikd.h"
     45 #include "amdgpu_gem.h"
     46 
     47 #include "amdgpu_display.h"
     48 
     49 #ifdef __NetBSD__
     50 #include "amdgpufb.h"
     51 #endif
     52 
     53 #include <linux/nbsd-namespace.h>
     54 
     55 /* object hierarchy -
     56    this contains a helper + a amdgpu fb
     57    the helper contains a pointer to amdgpu framebuffer baseclass.
     58 */
     59 
     60 static int
     61 amdgpufb_open(struct fb_info *info, int user)
     62 {
     63 	struct drm_fb_helper *fb_helper = info->par;
     64 	int ret = pm_runtime_get_sync(fb_helper->dev->dev);
     65 	if (ret < 0 && ret != -EACCES) {
     66 		pm_runtime_mark_last_busy(fb_helper->dev->dev);
     67 		pm_runtime_put_autosuspend(fb_helper->dev->dev);
     68 		return ret;
     69 	}
     70 	return 0;
     71 }
     72 
     73 static int
     74 amdgpufb_release(struct fb_info *info, int user)
     75 {
     76 	struct drm_fb_helper *fb_helper = info->par;
     77 
     78 	pm_runtime_mark_last_busy(fb_helper->dev->dev);
     79 	pm_runtime_put_autosuspend(fb_helper->dev->dev);
     80 	return 0;
     81 }
     82 
     83 #ifndef __NetBSD__
     84 static const struct fb_ops amdgpufb_ops = {
     85 	.owner = THIS_MODULE,
     86 	DRM_FB_HELPER_DEFAULT_OPS,
     87 	.fb_open = amdgpufb_open,
     88 	.fb_release = amdgpufb_release,
     89 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
     90 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
     91 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
     92 };
     93 #endif
     94 
     95 
     96 int amdgpu_align_pitch(struct amdgpu_device *adev, int width, int cpp, bool tiled)
     97 {
     98 	int aligned = width;
     99 	int pitch_mask = 0;
    100 
    101 	switch (cpp) {
    102 	case 1:
    103 		pitch_mask = 255;
    104 		break;
    105 	case 2:
    106 		pitch_mask = 127;
    107 		break;
    108 	case 3:
    109 	case 4:
    110 		pitch_mask = 63;
    111 		break;
    112 	}
    113 
    114 	aligned += pitch_mask;
    115 	aligned &= ~pitch_mask;
    116 	return aligned * cpp;
    117 }
    118 
    119 static void amdgpufb_destroy_pinned_object(struct drm_gem_object *gobj)
    120 {
    121 	struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
    122 	int ret;
    123 
    124 	ret = amdgpu_bo_reserve(abo, true);
    125 	if (likely(ret == 0)) {
    126 		amdgpu_bo_kunmap(abo);
    127 		amdgpu_bo_unpin(abo);
    128 		amdgpu_bo_unreserve(abo);
    129 	}
    130 	drm_gem_object_put_unlocked(gobj);
    131 }
    132 
    133 static int amdgpufb_create_pinned_object(struct amdgpu_fbdev *rfbdev,
    134 					 struct drm_mode_fb_cmd2 *mode_cmd,
    135 					 struct drm_gem_object **gobj_p)
    136 {
    137 	const struct drm_format_info *info;
    138 	struct amdgpu_device *adev = rfbdev->adev;
    139 	struct drm_gem_object *gobj = NULL;
    140 	struct amdgpu_bo *abo = NULL;
    141 	bool fb_tiled = false; /* useful for testing */
    142 	u32 tiling_flags = 0, domain;
    143 	int ret;
    144 	int aligned_size, size;
    145 	int height = mode_cmd->height;
    146 	u32 cpp;
    147 	u64 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
    148 			       AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS     |
    149 			       AMDGPU_GEM_CREATE_VRAM_CLEARED 	     |
    150 			       AMDGPU_GEM_CREATE_CPU_GTT_USWC;
    151 
    152 	info = drm_get_format_info(adev->ddev, mode_cmd);
    153 	cpp = info->cpp[0];
    154 
    155 	/* need to align pitch with crtc limits */
    156 	mode_cmd->pitches[0] = amdgpu_align_pitch(adev, mode_cmd->width, cpp,
    157 						  fb_tiled);
    158 	domain = amdgpu_display_supported_domains(adev, flags);
    159 	height = ALIGN(mode_cmd->height, 8);
    160 	size = mode_cmd->pitches[0] * height;
    161 	aligned_size = ALIGN(size, PAGE_SIZE);
    162 	ret = amdgpu_gem_object_create(adev, aligned_size, 0, domain, flags,
    163 				       ttm_bo_type_kernel, NULL, &gobj);
    164 	if (ret) {
    165 		pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
    166 		return -ENOMEM;
    167 	}
    168 	abo = gem_to_amdgpu_bo(gobj);
    169 
    170 	if (fb_tiled)
    171 		tiling_flags = AMDGPU_TILING_SET(ARRAY_MODE, GRPH_ARRAY_2D_TILED_THIN1);
    172 
    173 	ret = amdgpu_bo_reserve(abo, false);
    174 	if (unlikely(ret != 0))
    175 		goto out_unref;
    176 
    177 	if (tiling_flags) {
    178 		ret = amdgpu_bo_set_tiling_flags(abo,
    179 						 tiling_flags);
    180 		if (ret)
    181 			dev_err(adev->dev, "FB failed to set tiling flags\n");
    182 	}
    183 
    184 	ret = amdgpu_bo_pin(abo, domain);
    185 	if (ret) {
    186 		amdgpu_bo_unreserve(abo);
    187 		goto out_unref;
    188 	}
    189 
    190 	ret = amdgpu_ttm_alloc_gart(&abo->tbo);
    191 	if (ret) {
    192 		amdgpu_bo_unreserve(abo);
    193 		dev_err(adev->dev, "%p bind failed\n", abo);
    194 		goto out_unref;
    195 	}
    196 
    197 	ret = amdgpu_bo_kmap(abo, NULL);
    198 	amdgpu_bo_unreserve(abo);
    199 	if (ret) {
    200 		goto out_unref;
    201 	}
    202 
    203 	*gobj_p = gobj;
    204 	return 0;
    205 out_unref:
    206 	amdgpufb_destroy_pinned_object(gobj);
    207 	*gobj_p = NULL;
    208 	return ret;
    209 }
    210 
    211 static int amdgpufb_create(struct drm_fb_helper *helper,
    212 			   struct drm_fb_helper_surface_size *sizes)
    213 {
    214 	struct amdgpu_fbdev *rfbdev = (struct amdgpu_fbdev *)helper;
    215 	struct amdgpu_device *adev = rfbdev->adev;
    216 #ifndef __NetBSD__
    217 	struct fb_info *info;
    218 #endif
    219 	struct drm_framebuffer *fb = NULL;
    220 	struct drm_mode_fb_cmd2 mode_cmd;
    221 	struct drm_gem_object *gobj = NULL;
    222 	struct amdgpu_bo *abo = NULL;
    223 	int ret;
    224 #ifndef __NetBSD__
    225 	unsigned long tmp;
    226 #endif
    227 
    228 	mode_cmd.width = sizes->surface_width;
    229 	mode_cmd.height = sizes->surface_height;
    230 
    231 	if (sizes->surface_bpp == 24)
    232 		sizes->surface_bpp = 32;
    233 
    234 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
    235 							  sizes->surface_depth);
    236 
    237 	ret = amdgpufb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
    238 	if (ret) {
    239 		DRM_ERROR("failed to create fbcon object %d\n", ret);
    240 		return ret;
    241 	}
    242 
    243 	abo = gem_to_amdgpu_bo(gobj);
    244 
    245 #ifdef __NetBSD__
    246 	ret = amdgpu_framebuffer_init(adev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
    247 	if (ret) {
    248 		DRM_ERROR("failed to initialize framebuffer %d\n", ret);
    249 		goto out_unref;
    250 	}
    251 
    252 	(void)memset(rbo->kptr, 0, amdgpu_bo_size(rbo));
    253 
    254     {
    255 	static const struct amdgpufb_attach_args zero_afa;
    256 	struct amdgpufb_attach_args afa = zero_afa;
    257 
    258 	afa.afa_fb_helper = helper;
    259 	afa.afa_fb_sizes = *sizes;
    260 	afa.afa_fb_ptr = rbo->kptr;
    261 	afa.afa_fb_linebytes = mode_cmd.pitches[0];
    262 
    263 	helper->fbdev = config_found(adev->ddev->dev, &afa, NULL,
    264 	    CFARGS(.iattr = "amdgpufbbus"));
    265 	if (helper->fbdev == NULL) {
    266 		DRM_ERROR("failed to attach amdgpufb\n");
    267 		goto out_unref;
    268 	}
    269     }
    270 	fb = &rfbdev->rfb.base;
    271 	rfbdev->helper.fb = fb;
    272 #else  /* __NetBSD__ */
    273 	/* okay we have an object now allocate the framebuffer */
    274 	info = drm_fb_helper_alloc_fbi(helper);
    275 	if (IS_ERR(info)) {
    276 		ret = PTR_ERR(info);
    277 		goto out;
    278 	}
    279 
    280 	ret = amdgpu_display_framebuffer_init(adev->ddev, &rfbdev->rfb,
    281 					      &mode_cmd, gobj);
    282 	if (ret) {
    283 		DRM_ERROR("failed to initialize framebuffer %d\n", ret);
    284 		goto out;
    285 	}
    286 
    287 	fb = &rfbdev->rfb.base;
    288 
    289 	/* setup helper */
    290 	rfbdev->helper.fb = fb;
    291 
    292 	info->fbops = &amdgpufb_ops;
    293 
    294 	tmp = amdgpu_bo_gpu_offset(abo) - adev->gmc.vram_start;
    295 	info->fix.smem_start = adev->gmc.aper_base + tmp;
    296 	info->fix.smem_len = amdgpu_bo_size(abo);
    297 	info->screen_base = amdgpu_bo_kptr(abo);
    298 	info->screen_size = amdgpu_bo_size(abo);
    299 
    300 	drm_fb_helper_fill_info(info, &rfbdev->helper, sizes);
    301 
    302 	/* setup aperture base/size for vesafb takeover */
    303 	info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base;
    304 	info->apertures->ranges[0].size = adev->gmc.aper_size;
    305 
    306 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
    307 
    308 	if (info->screen_base == NULL) {
    309 		ret = -ENOSPC;
    310 		goto out;
    311 	}
    312 
    313 	DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
    314 	DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)adev->gmc.aper_base);
    315 	DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(abo));
    316 	DRM_INFO("fb depth is %d\n", fb->format->depth);
    317 	DRM_INFO("   pitch is %d\n", fb->pitches[0]);
    318 
    319 	vga_switcheroo_client_fb_set(adev->ddev->pdev, info);
    320 #endif
    321 	return 0;
    322 
    323 out:
    324 	if (abo) {
    325 
    326 	}
    327 	if (fb && ret) {
    328 		drm_gem_object_put_unlocked(gobj);
    329 		drm_framebuffer_unregister_private(fb);
    330 		drm_framebuffer_cleanup(fb);
    331 		kfree(fb);
    332 	}
    333 	return ret;
    334 }
    335 
    336 static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev)
    337 {
    338 	struct amdgpu_framebuffer *rfb = &rfbdev->rfb;
    339 #ifdef __NetBSD__
    340 	int ret;
    341 #endif
    342 
    343 #ifdef __NetBSD__
    344 	/* XXX errno NetBSD->Linux */
    345 	ret = -config_detach(rfbdev->helper.fbdev, DETACH_FORCE);
    346 	if (ret) {
    347 		DRM_ERROR("failed to detach amdgpufb: %d\n", ret);
    348 		return ret;
    349 	}
    350 	rfbdev->helper.fbdev = NULL;
    351 #else
    352 	drm_fb_helper_unregister_fbi(&rfbdev->helper);
    353 #endif
    354 
    355 	if (rfb->base.obj[0]) {
    356 		amdgpufb_destroy_pinned_object(rfb->base.obj[0]);
    357 		rfb->base.obj[0] = NULL;
    358 		drm_framebuffer_unregister_private(&rfb->base);
    359 		drm_framebuffer_cleanup(&rfb->base);
    360 	}
    361 	drm_fb_helper_fini(&rfbdev->helper);
    362 
    363 	return 0;
    364 }
    365 
    366 static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = {
    367 	.fb_probe = amdgpufb_create,
    368 };
    369 
    370 int amdgpu_fbdev_init(struct amdgpu_device *adev)
    371 {
    372 	struct amdgpu_fbdev *rfbdev;
    373 	int bpp_sel = 32;
    374 	int ret;
    375 
    376 	/* don't init fbdev on hw without DCE */
    377 	if (!adev->mode_info.mode_config_initialized)
    378 		return 0;
    379 
    380 	/* don't init fbdev if there are no connectors */
    381 	if (list_empty(&adev->ddev->mode_config.connector_list))
    382 		return 0;
    383 
    384 	/* select 8 bpp console on low vram cards */
    385 	if (adev->gmc.real_vram_size <= (32*1024*1024))
    386 		bpp_sel = 8;
    387 
    388 	rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL);
    389 	if (!rfbdev)
    390 		return -ENOMEM;
    391 
    392 	rfbdev->adev = adev;
    393 	adev->mode_info.rfbdev = rfbdev;
    394 
    395 	drm_fb_helper_prepare(adev->ddev, &rfbdev->helper,
    396 			&amdgpu_fb_helper_funcs);
    397 
    398 	ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper,
    399 				 AMDGPUFB_CONN_LIMIT);
    400 	if (ret) {
    401 		kfree(rfbdev);
    402 		return ret;
    403 	}
    404 
    405 	drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
    406 
    407 	/* disable all the possible outputs/crtcs before entering KMS mode */
    408 	if (!amdgpu_device_has_dc_support(adev))
    409 		drm_helper_disable_unused_functions(adev->ddev);
    410 
    411 	drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
    412 	return 0;
    413 }
    414 
    415 void amdgpu_fbdev_fini(struct amdgpu_device *adev)
    416 {
    417 	if (!adev->mode_info.rfbdev)
    418 		return;
    419 
    420 	amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev);
    421 	kfree(adev->mode_info.rfbdev);
    422 	adev->mode_info.rfbdev = NULL;
    423 }
    424 
    425 void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state)
    426 {
    427 	if (adev->mode_info.rfbdev)
    428 		drm_fb_helper_set_suspend_unlocked(&adev->mode_info.rfbdev->helper,
    429 						   state);
    430 }
    431 
    432 int amdgpu_fbdev_total_size(struct amdgpu_device *adev)
    433 {
    434 	struct amdgpu_bo *robj;
    435 	int size = 0;
    436 
    437 	if (!adev->mode_info.rfbdev)
    438 		return 0;
    439 
    440 	robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.base.obj[0]);
    441 	size += amdgpu_bo_size(robj);
    442 	return size;
    443 }
    444 
    445 bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj)
    446 {
    447 	if (!adev->mode_info.rfbdev)
    448 		return false;
    449 	if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.base.obj[0]))
    450 		return true;
    451 	return false;
    452 }
    453