1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2022 Intel Corporation 4 */ 5 6 #include "xe_sa.h" 7 8 #include <linux/kernel.h> 9 10 #include <drm/drm_managed.h> 11 12 #include "xe_bo.h" 13 #include "xe_device.h" 14 #include "xe_map.h" 15 16 static void xe_sa_bo_manager_fini(struct drm_device *drm, void *arg) 17 { 18 struct xe_sa_manager *sa_manager = arg; 19 struct xe_bo *bo = sa_manager->bo; 20 21 if (!bo) { 22 drm_err(drm, "no bo for sa manager\n"); 23 return; 24 } 25 26 drm_suballoc_manager_fini(&sa_manager->base); 27 28 if (bo->vmap.is_iomem) 29 kvfree(sa_manager->cpu_ptr); 30 31 xe_bo_unpin_map_no_vm(bo); 32 sa_manager->bo = NULL; 33 } 34 35 struct xe_sa_manager *xe_sa_bo_manager_init(struct xe_tile *tile, u32 size, u32 align) 36 { 37 struct xe_device *xe = tile_to_xe(tile); 38 u32 managed_size = size - SZ_4K; 39 struct xe_bo *bo; 40 int ret; 41 42 struct xe_sa_manager *sa_manager = drmm_kzalloc(&tile_to_xe(tile)->drm, 43 sizeof(*sa_manager), 44 GFP_KERNEL); 45 if (!sa_manager) 46 return ERR_PTR(-ENOMEM); 47 48 sa_manager->bo = NULL; 49 50 bo = xe_bo_create_pin_map(xe, tile, NULL, size, ttm_bo_type_kernel, 51 XE_BO_FLAG_VRAM_IF_DGFX(tile) | 52 XE_BO_FLAG_GGTT | 53 XE_BO_FLAG_GGTT_INVALIDATE); 54 if (IS_ERR(bo)) { 55 drm_err(&xe->drm, "failed to allocate bo for sa manager: %ld\n", 56 PTR_ERR(bo)); 57 return (struct xe_sa_manager *)bo; 58 } 59 sa_manager->bo = bo; 60 61 drm_suballoc_manager_init(&sa_manager->base, managed_size, align); 62 sa_manager->gpu_addr = xe_bo_ggtt_addr(bo); 63 64 if (bo->vmap.is_iomem) { 65 sa_manager->cpu_ptr = kvzalloc(managed_size, GFP_KERNEL); 66 if (!sa_manager->cpu_ptr) { 67 xe_bo_unpin_map_no_vm(sa_manager->bo); 68 sa_manager->bo = NULL; 69 return ERR_PTR(-ENOMEM); 70 } 71 } else { 72 sa_manager->cpu_ptr = bo->vmap.vaddr; 73 memset(sa_manager->cpu_ptr, 0, bo->ttm.base.size); 74 } 75 76 ret = drmm_add_action_or_reset(&xe->drm, xe_sa_bo_manager_fini, 77 sa_manager); 78 if (ret) 79 return ERR_PTR(ret); 80 81 return sa_manager; 82 } 83 84 struct drm_suballoc *xe_sa_bo_new(struct xe_sa_manager *sa_manager, 85 unsigned int size) 86 { 87 return drm_suballoc_new(&sa_manager->base, size, GFP_KERNEL, true, 0); 88 } 89 90 void xe_sa_bo_flush_write(struct drm_suballoc *sa_bo) 91 { 92 struct xe_sa_manager *sa_manager = to_xe_sa_manager(sa_bo->manager); 93 struct xe_device *xe = tile_to_xe(sa_manager->bo->tile); 94 95 if (!sa_manager->bo->vmap.is_iomem) 96 return; 97 98 xe_map_memcpy_to(xe, &sa_manager->bo->vmap, drm_suballoc_soffset(sa_bo), 99 xe_sa_bo_cpu_addr(sa_bo), 100 drm_suballoc_size(sa_bo)); 101 } 102 103 void xe_sa_bo_free(struct drm_suballoc *sa_bo, 104 struct dma_fence *fence) 105 { 106 drm_suballoc_free(sa_bo, fence); 107 } 108