summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/xe/xe_query.c
blob: f9f21bd1bfd72d395976f515146080d3e5d9a48a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2022 Intel Corporation
 */

#include <drm/xe_drm.h>
#include <drm/ttm/ttm_placement.h>
#include <linux/nospec.h>

#include "xe_bo.h"
#include "xe_device.h"
#include "xe_gt.h"
#include "xe_macros.h"
#include "xe_query.h"
#include "xe_ggtt.h"
#include "xe_guc_hwconfig.h"

static const enum xe_engine_class xe_to_user_engine_class[] = {
	[XE_ENGINE_CLASS_RENDER] = DRM_XE_ENGINE_CLASS_RENDER,
	[XE_ENGINE_CLASS_COPY] = DRM_XE_ENGINE_CLASS_COPY,
	[XE_ENGINE_CLASS_VIDEO_DECODE] = DRM_XE_ENGINE_CLASS_VIDEO_DECODE,
	[XE_ENGINE_CLASS_VIDEO_ENHANCE] = DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE,
	[XE_ENGINE_CLASS_COMPUTE] = DRM_XE_ENGINE_CLASS_COMPUTE,
};

static size_t calc_hw_engine_info_size(struct xe_device *xe)
{
	struct xe_hw_engine *hwe;
	enum xe_hw_engine_id id;
	struct xe_gt *gt;
	u8 gt_id;
	int i = 0;

	for_each_gt(gt, xe, gt_id)
		for_each_hw_engine(hwe, gt, id) {
			if (xe_hw_engine_is_reserved(hwe))
				continue;
			i++;
		}

	return i * sizeof(struct drm_xe_engine_class_instance);
}

static int query_engines(struct xe_device *xe,
			 struct drm_xe_device_query *query)
{
	size_t size = calc_hw_engine_info_size(xe);
	struct drm_xe_engine_class_instance __user *query_ptr =
		u64_to_user_ptr(query->data);
	struct drm_xe_engine_class_instance *hw_engine_info;
	struct xe_hw_engine *hwe;
	enum xe_hw_engine_id id;
	struct xe_gt *gt;
	u8 gt_id;
	int i = 0;

	if (query->size == 0) {
		query->size = size;
		return 0;
	} else if (XE_IOCTL_ERR(xe, query->size != size)) {
		return -EINVAL;
	}

	hw_engine_info = kmalloc(size, GFP_KERNEL);
	if (XE_IOCTL_ERR(xe, !hw_engine_info))
		return -ENOMEM;

	for_each_gt(gt, xe, gt_id)
		for_each_hw_engine(hwe, gt, id) {
			if (xe_hw_engine_is_reserved(hwe))
				continue;

			hw_engine_info[i].engine_class =
				xe_to_user_engine_class[hwe->class];
			hw_engine_info[i].engine_instance =
				hwe->logical_instance;
			hw_engine_info[i++].gt_id = gt->info.id;
		}

	if (copy_to_user(query_ptr, hw_engine_info, size)) {
		kfree(hw_engine_info);
		return -EFAULT;
	}
	kfree(hw_engine_info);

	return 0;
}

static size_t calc_memory_usage_size(struct xe_device *xe)
{
	u32 num_managers = 1;
	int i;

	for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i)
		if (ttm_manager_type(&xe->ttm, i))
			num_managers++;

	return offsetof(struct drm_xe_query_mem_usage, regions[num_managers]);
}

static int query_memory_usage(struct xe_device *xe,
			      struct drm_xe_device_query *query)
{
	size_t size = calc_memory_usage_size(xe);
	struct drm_xe_query_mem_usage *usage;
	struct drm_xe_query_mem_usage __user *query_ptr =
		u64_to_user_ptr(query->data);
	struct ttm_resource_manager *man;
	int ret, i;

	if (query->size == 0) {
		query->size = size;
		return 0;
	} else if (XE_IOCTL_ERR(xe, query->size != size)) {
		return -EINVAL;
	}

	usage = kzalloc(size, GFP_KERNEL);
	if (XE_IOCTL_ERR(xe, !usage))
		return -ENOMEM;

	man = ttm_manager_type(&xe->ttm, XE_PL_TT);
	usage->regions[0].mem_class = XE_MEM_REGION_CLASS_SYSMEM;
	usage->regions[0].instance = 0;
	usage->regions[0].min_page_size = PAGE_SIZE;
	usage->regions[0].max_page_size = PAGE_SIZE;
	usage->regions[0].total_size = man->size << PAGE_SHIFT;
	usage->regions[0].used = ttm_resource_manager_usage(man);
	usage->num_regions = 1;

	for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
		man = ttm_manager_type(&xe->ttm, i);
		if (man) {
			usage->regions[usage->num_regions].mem_class =
				XE_MEM_REGION_CLASS_VRAM;
			usage->regions[usage->num_regions].instance =
				usage->num_regions;
			usage->regions[usage->num_regions].min_page_size =
				xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ?
				SZ_64K : PAGE_SIZE;
			usage->regions[usage->num_regions].max_page_size =
				SZ_1G;
			usage->regions[usage->num_regions].total_size =
				man->size;
			usage->regions[usage->num_regions++].used =
				ttm_resource_manager_usage(man);
		}
	}

	if (!copy_to_user(query_ptr, usage, size))
		ret = 0;
	else
		ret = -ENOSPC;

	kfree(usage);
	return ret;
}

static int query_config(struct xe_device *xe, struct drm_xe_device_query *query)
{
	u32 num_params = XE_QUERY_CONFIG_NUM_PARAM;
	size_t size =
		sizeof(struct drm_xe_query_config) + num_params * sizeof(u64);
	struct drm_xe_query_config __user *query_ptr =
		u64_to_user_ptr(query->data);
	struct drm_xe_query_config *config;

	if (query->size == 0) {
		query->size = size;
		return 0;
	} else if (XE_IOCTL_ERR(xe, query->size != size)) {
		return -EINVAL;
	}

	config = kzalloc(size, GFP_KERNEL);
	if (XE_IOCTL_ERR(xe, !config))
		return -ENOMEM;

	config->num_params = num_params;
	config->info[XE_QUERY_CONFIG_REV_AND_DEVICE_ID] =
		xe->info.devid | (xe->info.revid << 16);
	if (to_gt(xe)->mem.vram.size)
		config->info[XE_QUERY_CONFIG_FLAGS] =
			XE_QUERY_CONFIG_FLAGS_HAS_VRAM;
	if (xe->info.enable_guc)
		config->info[XE_QUERY_CONFIG_FLAGS] |=
			XE_QUERY_CONFIG_FLAGS_USE_GUC;
	config->info[XE_QUERY_CONFIG_MIN_ALIGNEMENT] =
		xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K;
	config->info[XE_QUERY_CONFIG_VA_BITS] = 12 +
		(9 * (xe->info.vm_max_level + 1));
	config->info[XE_QUERY_CONFIG_GT_COUNT] = xe->info.tile_count;
	config->info[XE_QUERY_CONFIG_MEM_REGION_COUNT] =
		hweight_long(xe->info.mem_region_mask);

	if (copy_to_user(query_ptr, config, size)) {
		kfree(config);
		return -EFAULT;
	}
	kfree(config);

	return 0;
}

static int query_gts(struct xe_device *xe, struct drm_xe_device_query *query)
{
	struct xe_gt *gt;
	size_t size = sizeof(struct drm_xe_query_gts) +
		xe->info.tile_count * sizeof(struct drm_xe_query_gt);
	struct drm_xe_query_gts __user *query_ptr =
		u64_to_user_ptr(query->data);
	struct drm_xe_query_gts *gts;
	u8 id;

	if (query->size == 0) {
		query->size = size;
		return 0;
	} else if (XE_IOCTL_ERR(xe, query->size != size)) {
		return -EINVAL;
	}

	gts = kzalloc(size, GFP_KERNEL);
	if (XE_IOCTL_ERR(xe, !gts))
		return -ENOMEM;

	gts->num_gt = xe->info.tile_count;
	for_each_gt(gt, xe, id) {
		if (id == 0)
			gts->gts[id].type = XE_QUERY_GT_TYPE_MAIN;
		else if (xe_gt_is_media_type(gt))
			gts->gts[id].type = XE_QUERY_GT_TYPE_MEDIA;
		else
			gts->gts[id].type = XE_QUERY_GT_TYPE_REMOTE;
		gts->gts[id].instance = id;
		gts->gts[id].clock_freq = gt->info.clock_freq;
		if (!IS_DGFX(xe))
			gts->gts[id].native_mem_regions = 0x1;
		else
			gts->gts[id].native_mem_regions =
				BIT(gt->info.vram_id) << 1;
		gts->gts[id].slow_mem_regions = xe->info.mem_region_mask ^
			gts->gts[id].native_mem_regions;
	}

	if (copy_to_user(query_ptr, gts, size)) {
		kfree(gts);
		return -EFAULT;
	}
	kfree(gts);

	return 0;
}

static int query_hwconfig(struct xe_device *xe,
			  struct drm_xe_device_query *query)
{
	struct xe_gt *gt = xe_device_get_gt(xe, 0);
	size_t size = xe_guc_hwconfig_size(&gt->uc.guc);
	void __user *query_ptr = u64_to_user_ptr(query->data);
	void *hwconfig;

	if (query->size == 0) {
		query->size = size;
		return 0;
	} else if (XE_IOCTL_ERR(xe, query->size != size)) {
		return -EINVAL;
	}

	hwconfig = kzalloc(size, GFP_KERNEL);
	if (XE_IOCTL_ERR(xe, !hwconfig))
		return -ENOMEM;

	xe_device_mem_access_get(xe);
	xe_guc_hwconfig_copy(&gt->uc.guc, hwconfig);
	xe_device_mem_access_put(xe);

	if (copy_to_user(query_ptr, hwconfig, size)) {
		kfree(hwconfig);
		return -EFAULT;
	}
	kfree(hwconfig);

	return 0;
}

static size_t calc_topo_query_size(struct xe_device *xe)
{
	return xe->info.tile_count *
		(3 * sizeof(struct drm_xe_query_topology_mask) +
		 sizeof_field(struct xe_gt, fuse_topo.g_dss_mask) +
		 sizeof_field(struct xe_gt, fuse_topo.c_dss_mask) +
		 sizeof_field(struct xe_gt, fuse_topo.eu_mask_per_dss));
}

static void __user *copy_mask(void __user *ptr,
			      struct drm_xe_query_topology_mask *topo,
			      void *mask, size_t mask_size)
{
	topo->num_bytes = mask_size;

	if (copy_to_user(ptr, topo, sizeof(*topo)))
		return ERR_PTR(-EFAULT);
	ptr += sizeof(topo);

	if (copy_to_user(ptr, mask, mask_size))
		return ERR_PTR(-EFAULT);
	ptr += mask_size;

	return ptr;
}

static int query_gt_topology(struct xe_device *xe,
			     struct drm_xe_device_query *query)
{
	void __user *query_ptr = u64_to_user_ptr(query->data);
	size_t size = calc_topo_query_size(xe);
	struct drm_xe_query_topology_mask topo;
	struct xe_gt *gt;
	int id;

	if (query->size == 0) {
		query->size = size;
		return 0;
	} else if (XE_IOCTL_ERR(xe, query->size != size)) {
		return -EINVAL;
	}

	for_each_gt(gt, xe, id) {
		topo.gt_id = id;

		topo.type = XE_TOPO_DSS_GEOMETRY;
		query_ptr = copy_mask(query_ptr, &topo,
				      gt->fuse_topo.g_dss_mask,
				      sizeof(gt->fuse_topo.g_dss_mask));
		if (IS_ERR(query_ptr))
			return PTR_ERR(query_ptr);

		topo.type = XE_TOPO_DSS_COMPUTE;
		query_ptr = copy_mask(query_ptr, &topo,
				      gt->fuse_topo.c_dss_mask,
				      sizeof(gt->fuse_topo.c_dss_mask));
		if (IS_ERR(query_ptr))
			return PTR_ERR(query_ptr);

		topo.type = XE_TOPO_EU_PER_DSS;
		query_ptr = copy_mask(query_ptr, &topo,
				      gt->fuse_topo.eu_mask_per_dss,
				      sizeof(gt->fuse_topo.eu_mask_per_dss));
		if (IS_ERR(query_ptr))
			return PTR_ERR(query_ptr);
	}

	return 0;
}

static int (* const xe_query_funcs[])(struct xe_device *xe,
				      struct drm_xe_device_query *query) = {
	query_engines,
	query_memory_usage,
	query_config,
	query_gts,
	query_hwconfig,
	query_gt_topology,
};

int xe_query_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
{
	struct xe_device *xe = to_xe_device(dev);
	struct drm_xe_device_query *query = data;
	u32 idx;

	if (XE_IOCTL_ERR(xe, query->extensions != 0))
		return -EINVAL;

	if (XE_IOCTL_ERR(xe, query->query > ARRAY_SIZE(xe_query_funcs)))
		return -EINVAL;

	idx = array_index_nospec(query->query, ARRAY_SIZE(xe_query_funcs));
	if (XE_IOCTL_ERR(xe, !xe_query_funcs[idx]))
		return -EINVAL;

	return xe_query_funcs[idx](xe, query);
}