1 // SPDX-License-Identifier: MIT
3 * Copyright © 2022 Intel Corporation
8 #include <linux/kernel.h>
10 #include <drm/drm_managed.h>
13 #include "xe_device.h"
16 static void xe_sa_bo_manager_fini(struct drm_device *drm, void *arg)
18 struct xe_sa_manager *sa_manager = arg;
19 struct xe_bo *bo = sa_manager->bo;
22 drm_err(drm, "no bo for sa manager\n");
26 drm_suballoc_manager_fini(&sa_manager->base);
28 if (bo->vmap.is_iomem)
29 kvfree(sa_manager->cpu_ptr);
31 xe_bo_unpin_map_no_vm(bo);
32 sa_manager->bo = NULL;
35 struct xe_sa_manager *xe_sa_bo_manager_init(struct xe_tile *tile, u32 size, u32 align)
37 struct xe_device *xe = tile_to_xe(tile);
38 u32 managed_size = size - SZ_4K;
42 struct xe_sa_manager *sa_manager = drmm_kzalloc(&tile_to_xe(tile)->drm,
46 return ERR_PTR(-ENOMEM);
48 sa_manager->bo = NULL;
50 bo = xe_bo_create_pin_map(xe, tile, NULL, size, ttm_bo_type_kernel,
51 XE_BO_CREATE_VRAM_IF_DGFX(tile) |
52 XE_BO_CREATE_GGTT_BIT);
54 drm_err(&xe->drm, "failed to allocate bo for sa manager: %ld\n",
56 return (struct xe_sa_manager *)bo;
60 drm_suballoc_manager_init(&sa_manager->base, managed_size, align);
61 sa_manager->gpu_addr = xe_bo_ggtt_addr(bo);
63 if (bo->vmap.is_iomem) {
64 sa_manager->cpu_ptr = kvzalloc(managed_size, GFP_KERNEL);
65 if (!sa_manager->cpu_ptr) {
66 xe_bo_unpin_map_no_vm(sa_manager->bo);
67 sa_manager->bo = NULL;
68 return ERR_PTR(-ENOMEM);
71 sa_manager->cpu_ptr = bo->vmap.vaddr;
72 memset(sa_manager->cpu_ptr, 0, bo->ttm.base.size);
75 ret = drmm_add_action_or_reset(&xe->drm, xe_sa_bo_manager_fini,
83 struct drm_suballoc *xe_sa_bo_new(struct xe_sa_manager *sa_manager,
86 return drm_suballoc_new(&sa_manager->base, size, GFP_KERNEL, true, 0);
89 void xe_sa_bo_flush_write(struct drm_suballoc *sa_bo)
91 struct xe_sa_manager *sa_manager = to_xe_sa_manager(sa_bo->manager);
92 struct xe_device *xe = tile_to_xe(sa_manager->bo->tile);
94 if (!sa_manager->bo->vmap.is_iomem)
97 xe_map_memcpy_to(xe, &sa_manager->bo->vmap, drm_suballoc_soffset(sa_bo),
98 xe_sa_bo_cpu_addr(sa_bo),
99 drm_suballoc_size(sa_bo));
102 void xe_sa_bo_free(struct drm_suballoc *sa_bo,
103 struct dma_fence *fence)
105 drm_suballoc_free(sa_bo, fence);