xref: /linux/drivers/gpu/drm/xe/display/intel_fbdev_fb.c (revision d31ed58e2988d4a733242d6900920c9af39db8bb)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include <linux/fb.h>
7 
8 #include "intel_fbdev_fb.h"
9 #include "xe_bo.h"
10 #include "xe_ttm_stolen_mgr.h"
11 #include "xe_wa.h"
12 
13 #include <generated/xe_device_wa_oob.h>
14 
15 /*
16  * FIXME: There shouldn't be any reason to have XE_PAGE_SIZE stride
17  * alignment. The same 64 as i915 uses should be fine, and we shouldn't need to
18  * have driver specific values. However, dropping the stride alignment to 64
19  * leads to underflowing the bo pin count in the atomic cleanup work.
20  */
21 u32 intel_fbdev_fb_pitch_align(u32 stride)
22 {
23 	return ALIGN(stride, XE_PAGE_SIZE);
24 }
25 
26 bool intel_fbdev_fb_prefer_stolen(struct drm_device *drm, unsigned int size)
27 {
28 	struct xe_device *xe = to_xe_device(drm);
29 	struct ttm_resource_manager *stolen;
30 
31 	stolen = ttm_manager_type(&xe->ttm, XE_PL_STOLEN);
32 	if (!stolen)
33 		return false;
34 
35 	if (IS_DGFX(xe))
36 		return false;
37 
38 	if (XE_DEVICE_WA(xe, 22019338487_display))
39 		return false;
40 
41 	/*
42 	 * If the FB is too big, just don't use it since fbdev is not very
43 	 * important and we should probably use that space with FBC or other
44 	 * features.
45 	 */
46 	return stolen->size >= size * 2;
47 }
48 
49 struct drm_gem_object *intel_fbdev_fb_bo_create(struct drm_device *drm, int size)
50 {
51 	struct xe_device *xe = to_xe_device(drm);
52 	struct xe_bo *obj;
53 
54 	obj = ERR_PTR(-ENODEV);
55 
56 	if (intel_fbdev_fb_prefer_stolen(drm, size)) {
57 		obj = xe_bo_create_pin_map_novm(xe, xe_device_get_root_tile(xe),
58 						size,
59 						ttm_bo_type_kernel, XE_BO_FLAG_SCANOUT |
60 						XE_BO_FLAG_STOLEN |
61 						XE_BO_FLAG_GGTT, false);
62 		if (!IS_ERR(obj))
63 			drm_info(&xe->drm, "Allocated fbdev into stolen\n");
64 		else
65 			drm_info(&xe->drm, "Allocated fbdev into stolen failed: %li\n", PTR_ERR(obj));
66 	} else {
67 		drm_info(&xe->drm, "Allocating fbdev: Stolen memory not preferred.\n");
68 	}
69 
70 	if (IS_ERR(obj)) {
71 		obj = xe_bo_create_pin_map_novm(xe, xe_device_get_root_tile(xe), size,
72 						ttm_bo_type_kernel, XE_BO_FLAG_SCANOUT |
73 						XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |
74 						XE_BO_FLAG_GGTT, false);
75 	}
76 
77 	if (IS_ERR(obj)) {
78 		drm_err(&xe->drm, "failed to allocate framebuffer (%pe)\n", obj);
79 		return ERR_PTR(-ENOMEM);
80 	}
81 
82 	return &obj->ttm.base;
83 }
84 
85 void intel_fbdev_fb_bo_destroy(struct drm_gem_object *obj)
86 {
87 	xe_bo_unpin_map_no_vm(gem_to_xe_bo(obj));
88 }
89 
90 int intel_fbdev_fb_fill_info(struct drm_device *drm, struct fb_info *info,
91 			     struct drm_gem_object *_obj, struct i915_vma *vma)
92 {
93 	struct xe_bo *obj = gem_to_xe_bo(_obj);
94 	struct pci_dev *pdev = to_pci_dev(drm->dev);
95 
96 	if (!(obj->flags & XE_BO_FLAG_SYSTEM)) {
97 		if (obj->flags & XE_BO_FLAG_STOLEN)
98 			info->fix.smem_start = xe_ttm_stolen_io_offset(obj, 0);
99 		else
100 			info->fix.smem_start =
101 				pci_resource_start(pdev, 2) +
102 				xe_bo_addr(obj, 0, XE_PAGE_SIZE);
103 
104 		info->fix.smem_len = obj->ttm.base.size;
105 	} else {
106 		/* XXX: Pure fiction, as the BO may not be physically accessible.. */
107 		info->fix.smem_start = 0;
108 		info->fix.smem_len = obj->ttm.base.size;
109 	}
110 	XE_WARN_ON(iosys_map_is_null(&obj->vmap));
111 
112 	info->screen_base = obj->vmap.vaddr_iomem;
113 	info->screen_size = obj->ttm.base.size;
114 
115 	return 0;
116 }
117