1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2023 Intel Corporation 4 */ 5 6 #include <drm/drm_print.h> 7 #include <drm/xe_drm.h> 8 #include <linux/kernel.h> 9 #include <linux/slab.h> 10 #include <linux/types.h> 11 12 #include "xe_bo.h" 13 #include "xe_bo_types.h" 14 #include "xe_device_types.h" 15 #include "xe_drm_client.h" 16 #include "xe_trace.h" 17 18 /** 19 * xe_drm_client_alloc() - Allocate drm client 20 * @void: No arg 21 * 22 * Allocate drm client struct to track client memory against 23 * same till client life. Call this API whenever new client 24 * has opened xe device. 25 * 26 * Return: pointer to client struct or NULL if can't allocate 27 */ 28 struct xe_drm_client *xe_drm_client_alloc(void) 29 { 30 struct xe_drm_client *client; 31 32 client = kzalloc(sizeof(*client), GFP_KERNEL); 33 if (!client) 34 return NULL; 35 36 kref_init(&client->kref); 37 38 #ifdef CONFIG_PROC_FS 39 spin_lock_init(&client->bos_lock); 40 INIT_LIST_HEAD(&client->bos_list); 41 #endif 42 return client; 43 } 44 45 /** 46 * __xe_drm_client_free() - Free client struct 47 * @kref: The reference 48 * 49 * This frees client struct. Call this API when xe device is closed 50 * by drm client. 51 * 52 * Return: void 53 */ 54 void __xe_drm_client_free(struct kref *kref) 55 { 56 struct xe_drm_client *client = 57 container_of(kref, typeof(*client), kref); 58 59 kfree(client); 60 } 61 62 #ifdef CONFIG_PROC_FS 63 /** 64 * xe_drm_client_add_bo() - Add BO for tracking client mem usage 65 * @client: The drm client ptr 66 * @bo: The xe BO ptr 67 * 68 * Add all BO created by individual drm client by calling this function. 69 * This helps in tracking client memory usage. 70 * 71 * Return: void 72 */ 73 void xe_drm_client_add_bo(struct xe_drm_client *client, 74 struct xe_bo *bo) 75 { 76 XE_WARN_ON(bo->client); 77 XE_WARN_ON(!list_empty(&bo->client_link)); 78 79 spin_lock(&client->bos_lock); 80 bo->client = xe_drm_client_get(client); 81 list_add_tail_rcu(&bo->client_link, &client->bos_list); 82 spin_unlock(&client->bos_lock); 83 } 84 85 /** 86 * xe_drm_client_remove_bo() - Remove BO for tracking client mem usage 87 * @bo: The xe BO ptr 88 * 89 * Remove all BO removed by individual drm client by calling this function. 90 * This helps in tracking client memory usage. 91 * 92 * Return: void 93 */ 94 void xe_drm_client_remove_bo(struct xe_bo *bo) 95 { 96 struct xe_drm_client *client = bo->client; 97 98 spin_lock(&client->bos_lock); 99 list_del_rcu(&bo->client_link); 100 spin_unlock(&client->bos_lock); 101 102 xe_drm_client_put(client); 103 } 104 105 static void bo_meminfo(struct xe_bo *bo, 106 struct drm_memory_stats stats[TTM_NUM_MEM_TYPES]) 107 { 108 u64 sz = bo->size; 109 u32 mem_type; 110 111 if (bo->placement.placement) 112 mem_type = bo->placement.placement->mem_type; 113 else 114 mem_type = XE_PL_TT; 115 116 if (drm_gem_object_is_shared_for_memory_stats(&bo->ttm.base)) 117 stats[mem_type].shared += sz; 118 else 119 stats[mem_type].private += sz; 120 121 if (xe_bo_has_pages(bo)) { 122 stats[mem_type].resident += sz; 123 124 if (!dma_resv_test_signaled(bo->ttm.base.resv, 125 DMA_RESV_USAGE_BOOKKEEP)) 126 stats[mem_type].active += sz; 127 else if (mem_type == XE_PL_SYSTEM) 128 stats[mem_type].purgeable += sz; 129 } 130 } 131 132 static void show_meminfo(struct drm_printer *p, struct drm_file *file) 133 { 134 static const char *const mem_type_to_name[TTM_NUM_MEM_TYPES] = { 135 [XE_PL_SYSTEM] = "system", 136 [XE_PL_TT] = "gtt", 137 [XE_PL_VRAM0] = "vram0", 138 [XE_PL_VRAM1] = "vram1", 139 [4 ... 6] = NULL, 140 [XE_PL_STOLEN] = "stolen" 141 }; 142 struct drm_memory_stats stats[TTM_NUM_MEM_TYPES] = {}; 143 struct xe_file *xef = file->driver_priv; 144 struct ttm_device *bdev = &xef->xe->ttm; 145 struct ttm_resource_manager *man; 146 struct xe_drm_client *client; 147 struct drm_gem_object *obj; 148 struct xe_bo *bo; 149 unsigned int id; 150 u32 mem_type; 151 152 client = xef->client; 153 154 /* Public objects. */ 155 spin_lock(&file->table_lock); 156 idr_for_each_entry(&file->object_idr, obj, id) { 157 struct xe_bo *bo = gem_to_xe_bo(obj); 158 159 bo_meminfo(bo, stats); 160 } 161 spin_unlock(&file->table_lock); 162 163 /* Internal objects. */ 164 spin_lock(&client->bos_lock); 165 list_for_each_entry_rcu(bo, &client->bos_list, client_link) { 166 if (!bo || !kref_get_unless_zero(&bo->ttm.base.refcount)) 167 continue; 168 bo_meminfo(bo, stats); 169 xe_bo_put(bo); 170 } 171 spin_unlock(&client->bos_lock); 172 173 for (mem_type = XE_PL_SYSTEM; mem_type < TTM_NUM_MEM_TYPES; ++mem_type) { 174 if (!mem_type_to_name[mem_type]) 175 continue; 176 177 man = ttm_manager_type(bdev, mem_type); 178 179 if (man) { 180 drm_print_memory_stats(p, 181 &stats[mem_type], 182 DRM_GEM_OBJECT_RESIDENT | 183 (mem_type != XE_PL_SYSTEM ? 0 : 184 DRM_GEM_OBJECT_PURGEABLE), 185 mem_type_to_name[mem_type]); 186 } 187 } 188 } 189 190 /** 191 * xe_drm_client_fdinfo() - Callback for fdinfo interface 192 * @p: The drm_printer ptr 193 * @file: The drm_file ptr 194 * 195 * This is callabck for drm fdinfo interface. Register this callback 196 * in drm driver ops for show_fdinfo. 197 * 198 * Return: void 199 */ 200 void xe_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file) 201 { 202 show_meminfo(p, file); 203 } 204 #endif 205