1 /* SPDX-License-Identifier: MIT */ 2 /* 3 * Copyright © 2023 Intel Corporation 4 */ 5 6 #include "intel_fbdev_fb.h" 7 8 #include <drm/drm_fb_helper.h> 9 10 #include "xe_gt.h" 11 #include "xe_ttm_stolen_mgr.h" 12 13 #include "i915_drv.h" 14 #include "intel_display_types.h" 15 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 drm_i915_private *dev_priv = to_i915(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(dev_priv)) { 43 obj = xe_bo_create_pin_map(dev_priv, xe_device_get_root_tile(dev_priv), 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(&dev_priv->drm, "Allocated fbdev into stolen\n"); 50 else 51 drm_info(&dev_priv->drm, "Allocated fbdev into stolen failed: %li\n", PTR_ERR(obj)); 52 } 53 if (IS_ERR(obj)) { 54 obj = xe_bo_create_pin_map(dev_priv, xe_device_get_root_tile(dev_priv), NULL, size, 55 ttm_bo_type_kernel, XE_BO_FLAG_SCANOUT | 56 XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(dev_priv)) | 57 XE_BO_FLAG_PINNED); 58 } 59 60 if (IS_ERR(obj)) { 61 drm_err(&dev_priv->drm, "failed to allocate framebuffer (%pe)\n", obj); 62 fb = ERR_PTR(-ENOMEM); 63 goto err; 64 } 65 66 fb = intel_framebuffer_create(obj, &mode_cmd); 67 if (IS_ERR(fb)) { 68 xe_bo_unpin_map_no_vm(obj); 69 goto err; 70 } 71 72 drm_gem_object_put(intel_bo_to_drm_bo(obj)); 73 74 return to_intel_framebuffer(fb); 75 76 err: 77 return ERR_CAST(fb); 78 } 79 80 int intel_fbdev_fb_fill_info(struct drm_i915_private *i915, struct fb_info *info, 81 struct drm_i915_gem_object *obj, struct i915_vma *vma) 82 { 83 struct pci_dev *pdev = to_pci_dev(i915->drm.dev); 84 85 if (!(obj->flags & XE_BO_FLAG_SYSTEM)) { 86 if (obj->flags & XE_BO_FLAG_STOLEN) 87 info->fix.smem_start = xe_ttm_stolen_io_offset(obj, 0); 88 else 89 info->fix.smem_start = 90 pci_resource_start(pdev, 2) + 91 xe_bo_addr(obj, 0, XE_PAGE_SIZE); 92 93 info->fix.smem_len = obj->ttm.base.size; 94 } else { 95 /* XXX: Pure fiction, as the BO may not be physically accessible.. */ 96 info->fix.smem_start = 0; 97 info->fix.smem_len = obj->ttm.base.size; 98 } 99 XE_WARN_ON(iosys_map_is_null(&obj->vmap)); 100 101 info->screen_base = obj->vmap.vaddr_iomem; 102 info->screen_size = intel_bo_to_drm_bo(obj)->size; 103 104 return 0; 105 } 106