xref: /linux/drivers/gpu/drm/i915/display/intel_fbdev_fb.c (revision c745b15c1f9cea5680c2906ae868302108f8daf0)
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 
14 struct drm_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 fb;
67 }
68 
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 		info->fix.smem_len = obj->base.size;
84 	} else {
85 		struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
86 
87 		/* Our framebuffer is the entirety of fbdev's system memory */
88 		info->fix.smem_start =
89 			(unsigned long)(ggtt->gmadr.start + i915_ggtt_offset(vma));
90 		info->fix.smem_len = vma->size;
91 	}
92 
93 	for_i915_gem_ww(&ww, ret, false) {
94 		ret = i915_gem_object_lock(vma->obj, &ww);
95 
96 		if (ret)
97 			continue;
98 
99 		vaddr = i915_vma_pin_iomap(vma);
100 		if (IS_ERR(vaddr)) {
101 			drm_err(&i915->drm,
102 				"Failed to remap framebuffer into virtual memory (%pe)\n", vaddr);
103 			ret = PTR_ERR(vaddr);
104 			continue;
105 		}
106 	}
107 
108 	if (ret)
109 		return ret;
110 
111 	info->screen_base = vaddr;
112 	info->screen_size = intel_bo_to_drm_bo(obj)->size;
113 
114 	return 0;
115 }
116