xref: /linux/drivers/gpu/drm/xe/display/intel_fbdev_fb.c (revision e2683c8868d03382da7e1ce8453b543a043066d1)
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,
60 						XE_BO_FLAG_FORCE_WC |
61 						XE_BO_FLAG_STOLEN |
62 						XE_BO_FLAG_GGTT,
63 						false);
64 		if (!IS_ERR(obj))
65 			drm_info(&xe->drm, "Allocated fbdev into stolen\n");
66 		else
67 			drm_info(&xe->drm, "Allocated fbdev into stolen failed: %li\n", PTR_ERR(obj));
68 	} else {
69 		drm_info(&xe->drm, "Allocating fbdev: Stolen memory not preferred.\n");
70 	}
71 
72 	if (IS_ERR(obj)) {
73 		obj = xe_bo_create_pin_map_novm(xe, xe_device_get_root_tile(xe), size,
74 						ttm_bo_type_kernel,
75 						XE_BO_FLAG_FORCE_WC |
76 						XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |
77 						XE_BO_FLAG_GGTT,
78 						false);
79 	}
80 
81 	if (IS_ERR(obj)) {
82 		drm_err(&xe->drm, "failed to allocate framebuffer (%pe)\n", obj);
83 		return ERR_PTR(-ENOMEM);
84 	}
85 
86 	return &obj->ttm.base;
87 }
88 
89 void intel_fbdev_fb_bo_destroy(struct drm_gem_object *obj)
90 {
91 	xe_bo_unpin_map_no_vm(gem_to_xe_bo(obj));
92 }
93 
94 int intel_fbdev_fb_fill_info(struct drm_device *drm, struct fb_info *info,
95 			     struct drm_gem_object *_obj, struct i915_vma *vma)
96 {
97 	struct xe_bo *obj = gem_to_xe_bo(_obj);
98 	struct pci_dev *pdev = to_pci_dev(drm->dev);
99 
100 	if (!(obj->flags & XE_BO_FLAG_SYSTEM)) {
101 		if (obj->flags & XE_BO_FLAG_STOLEN)
102 			info->fix.smem_start = xe_ttm_stolen_io_offset(obj, 0);
103 		else
104 			info->fix.smem_start =
105 				pci_resource_start(pdev, 2) +
106 				xe_bo_addr(obj, 0, XE_PAGE_SIZE);
107 
108 		info->fix.smem_len = obj->ttm.base.size;
109 	} else {
110 		/* XXX: Pure fiction, as the BO may not be physically accessible.. */
111 		info->fix.smem_start = 0;
112 		info->fix.smem_len = obj->ttm.base.size;
113 	}
114 	XE_WARN_ON(iosys_map_is_null(&obj->vmap));
115 
116 	info->screen_base = obj->vmap.vaddr_iomem;
117 	info->screen_size = obj->ttm.base.size;
118 
119 	return 0;
120 }
121