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