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_CREATE_VRAM_IF_DGFX(tile) | 52 XE_BO_CREATE_GGTT_BIT); 53 if (IS_ERR(bo)) { 54 drm_err(&xe->drm, "failed to allocate bo for sa manager: %ld\n", 55 PTR_ERR(bo)); 56 return (struct xe_sa_manager *)bo; 57 } 58 sa_manager->bo = bo; 59 60 drm_suballoc_manager_init(&sa_manager->base, managed_size, align); 61 sa_manager->gpu_addr = xe_bo_ggtt_addr(bo); 62 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); 69 } 70 } else { 71 sa_manager->cpu_ptr = bo->vmap.vaddr; 72 memset(sa_manager->cpu_ptr, 0, bo->ttm.base.size); 73 } 74 75 ret = drmm_add_action_or_reset(&xe->drm, xe_sa_bo_manager_fini, 76 sa_manager); 77 if (ret) 78 return ERR_PTR(ret); 79 80 return sa_manager; 81 } 82 83 struct drm_suballoc *xe_sa_bo_new(struct xe_sa_manager *sa_manager, 84 unsigned int size) 85 { 86 return drm_suballoc_new(&sa_manager->base, size, GFP_KERNEL, true, 0); 87 } 88 89 void xe_sa_bo_flush_write(struct drm_suballoc *sa_bo) 90 { 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); 93 94 if (!sa_manager->bo->vmap.is_iomem) 95 return; 96 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)); 100 } 101 102 void xe_sa_bo_free(struct drm_suballoc *sa_bo, 103 struct dma_fence *fence) 104 { 105 drm_suballoc_free(sa_bo, fence); 106 } 107