xref: /linux/drivers/gpu/drm/xe/display/intel_fbdev_fb.c (revision 3fd6c59042dbba50391e30862beac979491145fe)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include <drm/drm_fb_helper.h>
7 
8 #include "intel_display_types.h"
9 #include "intel_fb.h"
10 #include "intel_fbdev_fb.h"
11 #include "xe_bo.h"
12 #include "xe_ttm_stolen_mgr.h"
13 #include "xe_wa.h"
14 
15 #include <generated/xe_wa_oob.h>
16 
intel_fbdev_fb_alloc(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)17 struct intel_framebuffer *intel_fbdev_fb_alloc(struct drm_fb_helper *helper,
18 					       struct drm_fb_helper_surface_size *sizes)
19 {
20 	struct drm_framebuffer *fb;
21 	struct drm_device *dev = helper->dev;
22 	struct xe_device *xe = to_xe_device(dev);
23 	struct drm_mode_fb_cmd2 mode_cmd = {};
24 	struct xe_bo *obj;
25 	int size;
26 
27 	/* we don't do packed 24bpp */
28 	if (sizes->surface_bpp == 24)
29 		sizes->surface_bpp = 32;
30 
31 	mode_cmd.width = sizes->surface_width;
32 	mode_cmd.height = sizes->surface_height;
33 
34 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
35 				    DIV_ROUND_UP(sizes->surface_bpp, 8), XE_PAGE_SIZE);
36 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
37 							  sizes->surface_depth);
38 
39 	size = mode_cmd.pitches[0] * mode_cmd.height;
40 	size = PAGE_ALIGN(size);
41 	obj = ERR_PTR(-ENODEV);
42 
43 	if (!IS_DGFX(xe) && !XE_WA(xe_root_mmio_gt(xe), 22019338487_display)) {
44 		obj = xe_bo_create_pin_map(xe, xe_device_get_root_tile(xe),
45 					   NULL, size,
46 					   ttm_bo_type_kernel, XE_BO_FLAG_SCANOUT |
47 					   XE_BO_FLAG_STOLEN |
48 					   XE_BO_FLAG_PINNED);
49 		if (!IS_ERR(obj))
50 			drm_info(&xe->drm, "Allocated fbdev into stolen\n");
51 		else
52 			drm_info(&xe->drm, "Allocated fbdev into stolen failed: %li\n", PTR_ERR(obj));
53 	}
54 
55 	if (IS_ERR(obj)) {
56 		obj = xe_bo_create_pin_map(xe, xe_device_get_root_tile(xe), NULL, size,
57 					   ttm_bo_type_kernel, XE_BO_FLAG_SCANOUT |
58 					   XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |
59 					   XE_BO_FLAG_PINNED);
60 	}
61 
62 	if (IS_ERR(obj)) {
63 		drm_err(&xe->drm, "failed to allocate framebuffer (%pe)\n", obj);
64 		fb = ERR_PTR(-ENOMEM);
65 		goto err;
66 	}
67 
68 	fb = intel_framebuffer_create(&obj->ttm.base, &mode_cmd);
69 	if (IS_ERR(fb)) {
70 		xe_bo_unpin_map_no_vm(obj);
71 		goto err;
72 	}
73 
74 	drm_gem_object_put(&obj->ttm.base);
75 
76 	return to_intel_framebuffer(fb);
77 
78 err:
79 	return ERR_CAST(fb);
80 }
81 
intel_fbdev_fb_fill_info(struct drm_i915_private * i915,struct fb_info * info,struct drm_gem_object * _obj,struct i915_vma * vma)82 int intel_fbdev_fb_fill_info(struct drm_i915_private *i915, struct fb_info *info,
83 			      struct drm_gem_object *_obj, struct i915_vma *vma)
84 {
85 	struct xe_bo *obj = gem_to_xe_bo(_obj);
86 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
87 
88 	if (!(obj->flags & XE_BO_FLAG_SYSTEM)) {
89 		if (obj->flags & XE_BO_FLAG_STOLEN)
90 			info->fix.smem_start = xe_ttm_stolen_io_offset(obj, 0);
91 		else
92 			info->fix.smem_start =
93 				pci_resource_start(pdev, 2) +
94 				xe_bo_addr(obj, 0, XE_PAGE_SIZE);
95 
96 		info->fix.smem_len = obj->ttm.base.size;
97 	} else {
98 		/* XXX: Pure fiction, as the BO may not be physically accessible.. */
99 		info->fix.smem_start = 0;
100 		info->fix.smem_len = obj->ttm.base.size;
101 	}
102 	XE_WARN_ON(iosys_map_is_null(&obj->vmap));
103 
104 	info->screen_base = obj->vmap.vaddr_iomem;
105 	info->screen_size = obj->ttm.base.size;
106 
107 	return 0;
108 }
109