1 // SPDX-License-Identifier: MIT
3 * Copyright © 2023 Intel Corporation
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>
13 #include "xe_bo_types.h"
14 #include "xe_device_types.h"
15 #include "xe_drm_client.h"
19 * xe_drm_client_alloc() - Allocate drm client
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.
26 * Return: pointer to client struct or NULL if can't allocate
28 struct xe_drm_client *xe_drm_client_alloc(void)
30 struct xe_drm_client *client;
32 client = kzalloc(sizeof(*client), GFP_KERNEL);
36 kref_init(&client->kref);
39 spin_lock_init(&client->bos_lock);
40 INIT_LIST_HEAD(&client->bos_list);
46 * __xe_drm_client_free() - Free client struct
47 * @kref: The reference
49 * This frees client struct. Call this API when xe device is closed
54 void __xe_drm_client_free(struct kref *kref)
56 struct xe_drm_client *client =
57 container_of(kref, typeof(*client), kref);
64 * xe_drm_client_add_bo() - Add BO for tracking client mem usage
65 * @client: The drm client ptr
68 * Add all BO created by individual drm client by calling this function.
69 * This helps in tracking client memory usage.
73 void xe_drm_client_add_bo(struct xe_drm_client *client,
76 XE_WARN_ON(bo->client);
77 XE_WARN_ON(!list_empty(&bo->client_link));
79 spin_lock(&client->bos_lock);
80 bo->client = xe_drm_client_get(client);
81 list_add_tail(&bo->client_link, &client->bos_list);
82 spin_unlock(&client->bos_lock);
86 * xe_drm_client_remove_bo() - Remove BO for tracking client mem usage
89 * Remove all BO removed by individual drm client by calling this function.
90 * This helps in tracking client memory usage.
94 void xe_drm_client_remove_bo(struct xe_bo *bo)
96 struct xe_drm_client *client = bo->client;
98 spin_lock(&client->bos_lock);
99 list_del(&bo->client_link);
100 spin_unlock(&client->bos_lock);
102 xe_drm_client_put(client);
105 static void bo_meminfo(struct xe_bo *bo,
106 struct drm_memory_stats stats[TTM_NUM_MEM_TYPES])
111 if (bo->placement.placement)
112 mem_type = bo->placement.placement->mem_type;
116 if (drm_gem_object_is_shared_for_memory_stats(&bo->ttm.base))
117 stats[mem_type].shared += sz;
119 stats[mem_type].private += sz;
121 if (xe_bo_has_pages(bo)) {
122 stats[mem_type].resident += sz;
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;
132 static void show_meminfo(struct drm_printer *p, struct drm_file *file)
134 struct drm_memory_stats stats[TTM_NUM_MEM_TYPES] = {};
135 struct xe_file *xef = file->driver_priv;
136 struct ttm_device *bdev = &xef->xe->ttm;
137 struct ttm_resource_manager *man;
138 struct xe_drm_client *client;
139 struct drm_gem_object *obj;
144 client = xef->client;
146 /* Public objects. */
147 spin_lock(&file->table_lock);
148 idr_for_each_entry(&file->object_idr, obj, id) {
149 struct xe_bo *bo = gem_to_xe_bo(obj);
151 bo_meminfo(bo, stats);
153 spin_unlock(&file->table_lock);
155 /* Internal objects. */
156 spin_lock(&client->bos_lock);
157 list_for_each_entry(bo, &client->bos_list, client_link) {
158 if (!kref_get_unless_zero(&bo->ttm.base.refcount))
160 bo_meminfo(bo, stats);
163 spin_unlock(&client->bos_lock);
165 for (mem_type = XE_PL_SYSTEM; mem_type < TTM_NUM_MEM_TYPES; ++mem_type) {
166 if (!xe_mem_type_to_name[mem_type])
169 man = ttm_manager_type(bdev, mem_type);
172 drm_print_memory_stats(p,
174 DRM_GEM_OBJECT_RESIDENT |
175 (mem_type != XE_PL_SYSTEM ? 0 :
176 DRM_GEM_OBJECT_PURGEABLE),
177 xe_mem_type_to_name[mem_type]);
183 * xe_drm_client_fdinfo() - Callback for fdinfo interface
184 * @p: The drm_printer ptr
185 * @file: The drm_file ptr
187 * This is callabck for drm fdinfo interface. Register this callback
188 * in drm driver ops for show_fdinfo.
192 void xe_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file)
194 show_meminfo(p, file);