xref: /linux/drivers/gpu/drm/i915/display/intel_fbdev_fb.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include <drm/drm_fb_helper.h>
7 
8 #include "gem/i915_gem_lmem.h"
9 
10 #include "i915_drv.h"
11 #include "intel_display_types.h"
12 #include "intel_fb.h"
13 #include "intel_fbdev_fb.h"
14 
15 struct intel_framebuffer *intel_fbdev_fb_alloc(struct drm_fb_helper *helper,
16 					       struct drm_fb_helper_surface_size *sizes)
17 {
18 	struct drm_framebuffer *fb;
19 	struct drm_device *dev = helper->dev;
20 	struct drm_i915_private *dev_priv = to_i915(dev);
21 	struct drm_mode_fb_cmd2 mode_cmd = {};
22 	struct drm_i915_gem_object *obj;
23 	int size;
24 
25 	/* we don't do packed 24bpp */
26 	if (sizes->surface_bpp == 24)
27 		sizes->surface_bpp = 32;
28 
29 	mode_cmd.width = sizes->surface_width;
30 	mode_cmd.height = sizes->surface_height;
31 
32 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
33 				    DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
34 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
35 							  sizes->surface_depth);
36 
37 	size = mode_cmd.pitches[0] * mode_cmd.height;
38 	size = PAGE_ALIGN(size);
39 
40 	obj = ERR_PTR(-ENODEV);
41 	if (HAS_LMEM(dev_priv)) {
42 		obj = i915_gem_object_create_lmem(dev_priv, size,
43 						  I915_BO_ALLOC_CONTIGUOUS |
44 						  I915_BO_ALLOC_USER);
45 	} else {
46 		/*
47 		 * If the FB is too big, just don't use it since fbdev is not very
48 		 * important and we should probably use that space with FBC or other
49 		 * features.
50 		 *
51 		 * Also skip stolen on MTL as Wa_22018444074 mitigation.
52 		 */
53 		if (!(IS_METEORLAKE(dev_priv)) && size * 2 < dev_priv->dsm.usable_size)
54 			obj = i915_gem_object_create_stolen(dev_priv, size);
55 		if (IS_ERR(obj))
56 			obj = i915_gem_object_create_shmem(dev_priv, size);
57 	}
58 
59 	if (IS_ERR(obj)) {
60 		drm_err(&dev_priv->drm, "failed to allocate framebuffer (%pe)\n", obj);
61 		return ERR_PTR(-ENOMEM);
62 	}
63 
64 	fb = intel_framebuffer_create(intel_bo_to_drm_bo(obj), &mode_cmd);
65 	i915_gem_object_put(obj);
66 
67 	return to_intel_framebuffer(fb);
68 }
69 
70 int intel_fbdev_fb_fill_info(struct drm_i915_private *i915, struct fb_info *info,
71 			     struct drm_gem_object *_obj, struct i915_vma *vma)
72 {
73 	struct drm_i915_gem_object *obj = to_intel_bo(_obj);
74 	struct i915_gem_ww_ctx ww;
75 	void __iomem *vaddr;
76 	int ret;
77 
78 	if (i915_gem_object_is_lmem(obj)) {
79 		struct intel_memory_region *mem = obj->mm.region;
80 
81 		/* Use fbdev's framebuffer from lmem for discrete */
82 		info->fix.smem_start =
83 			(unsigned long)(mem->io.start +
84 					i915_gem_object_get_dma_address(obj, 0) -
85 					mem->region.start);
86 		info->fix.smem_len = obj->base.size;
87 	} else {
88 		struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
89 
90 		/* Our framebuffer is the entirety of fbdev's system memory */
91 		info->fix.smem_start =
92 			(unsigned long)(ggtt->gmadr.start + i915_ggtt_offset(vma));
93 		info->fix.smem_len = vma->size;
94 	}
95 
96 	for_i915_gem_ww(&ww, ret, false) {
97 		ret = i915_gem_object_lock(vma->obj, &ww);
98 
99 		if (ret)
100 			continue;
101 
102 		vaddr = i915_vma_pin_iomap(vma);
103 		if (IS_ERR(vaddr)) {
104 			drm_err(&i915->drm,
105 				"Failed to remap framebuffer into virtual memory (%pe)\n", vaddr);
106 			ret = PTR_ERR(vaddr);
107 			continue;
108 		}
109 	}
110 
111 	if (ret)
112 		return ret;
113 
114 	info->screen_base = vaddr;
115 	info->screen_size = intel_bo_to_drm_bo(obj)->size;
116 
117 	return 0;
118 }
119