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