xe_query.c 13.5 KB
Newer Older
1 2 3 4 5
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2022 Intel Corporation
 */

Lucas De Marchi's avatar
Lucas De Marchi committed
6 7
#include "xe_query.h"

8
#include <linux/nospec.h>
9
#include <linux/sched/clock.h>
10

Lucas De Marchi's avatar
Lucas De Marchi committed
11 12 13
#include <drm/ttm/ttm_placement.h>
#include <drm/xe_drm.h>

14
#include "regs/xe_engine_regs.h"
15 16
#include "xe_bo.h"
#include "xe_device.h"
17
#include "xe_exec_queue.h"
18
#include "xe_ggtt.h"
Lucas De Marchi's avatar
Lucas De Marchi committed
19
#include "xe_gt.h"
20
#include "xe_guc_hwconfig.h"
Lucas De Marchi's avatar
Lucas De Marchi committed
21
#include "xe_macros.h"
22
#include "xe_mmio.h"
23
#include "xe_ttm_vram_mgr.h"
24

25
static const u16 xe_to_user_engine_class[] = {
26 27 28 29 30 31 32
	[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,
};

33 34 35 36 37 38 39 40
static const enum xe_engine_class user_to_xe_engine_class[] = {
	[DRM_XE_ENGINE_CLASS_RENDER] = XE_ENGINE_CLASS_RENDER,
	[DRM_XE_ENGINE_CLASS_COPY] = XE_ENGINE_CLASS_COPY,
	[DRM_XE_ENGINE_CLASS_VIDEO_DECODE] = XE_ENGINE_CLASS_VIDEO_DECODE,
	[DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE] = XE_ENGINE_CLASS_VIDEO_ENHANCE,
	[DRM_XE_ENGINE_CLASS_COMPUTE] = XE_ENGINE_CLASS_COMPUTE,
};

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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++;
		}

56
	return i * sizeof(struct drm_xe_query_engine_info);
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
typedef u64 (*__ktime_func_t)(void);
static __ktime_func_t __clock_id_to_func(clockid_t clk_id)
{
	/*
	 * Use logic same as the perf subsystem to allow user to select the
	 * reference clock id to be used for timestamps.
	 */
	switch (clk_id) {
	case CLOCK_MONOTONIC:
		return &ktime_get_ns;
	case CLOCK_MONOTONIC_RAW:
		return &ktime_get_raw_ns;
	case CLOCK_REALTIME:
		return &ktime_get_real_ns;
	case CLOCK_BOOTTIME:
		return &ktime_get_boottime_ns;
	case CLOCK_TAI:
		return &ktime_get_clocktai_ns;
	default:
		return NULL;
	}
}

static void
__read_timestamps(struct xe_gt *gt,
		  struct xe_reg lower_reg,
		  struct xe_reg upper_reg,
		  u64 *engine_ts,
		  u64 *cpu_ts,
		  u64 *cpu_delta,
		  __ktime_func_t cpu_clock)
{
	u32 upper, lower, old_upper, loop = 0;

	upper = xe_mmio_read32(gt, upper_reg);
	do {
		*cpu_delta = local_clock();
		*cpu_ts = cpu_clock();
		lower = xe_mmio_read32(gt, lower_reg);
		*cpu_delta = local_clock() - *cpu_delta;
		old_upper = upper;
		upper = xe_mmio_read32(gt, upper_reg);
	} while (upper != old_upper && loop++ < 2);

	*engine_ts = (u64)upper << 32 | lower;
}

static int
query_engine_cycles(struct xe_device *xe,
		    struct drm_xe_device_query *query)
{
	struct drm_xe_query_engine_cycles __user *query_ptr;
	struct drm_xe_engine_class_instance *eci;
	struct drm_xe_query_engine_cycles resp;
	size_t size = sizeof(resp);
	__ktime_func_t cpu_clock;
	struct xe_hw_engine *hwe;
	struct xe_gt *gt;

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

	query_ptr = u64_to_user_ptr(query->data);
	if (copy_from_user(&resp, query_ptr, size))
		return -EFAULT;

	cpu_clock = __clock_id_to_func(resp.clockid);
	if (!cpu_clock)
		return -EINVAL;

	eci = &resp.eci;
	if (eci->gt_id > XE_MAX_GT_PER_TILE)
		return -EINVAL;

	gt = xe_device_get_gt(xe, eci->gt_id);
	if (!gt)
		return -EINVAL;

	if (eci->engine_class >= ARRAY_SIZE(user_to_xe_engine_class))
		return -EINVAL;

	hwe = xe_gt_hw_engine(gt, user_to_xe_engine_class[eci->engine_class],
			      eci->engine_instance, true);
	if (!hwe)
		return -EINVAL;

	resp.engine_frequency = gt->info.clock_freq;

	xe_device_mem_access_get(xe);
	xe_force_wake_get(gt_to_fw(gt), XE_FORCEWAKE_ALL);

	__read_timestamps(gt,
			  RING_TIMESTAMP(hwe->mmio_base),
			  RING_TIMESTAMP_UDW(hwe->mmio_base),
			  &resp.engine_cycles,
			  &resp.cpu_timestamp,
			  &resp.cpu_delta,
			  cpu_clock);

	xe_force_wake_put(gt_to_fw(gt), XE_FORCEWAKE_ALL);
	xe_device_mem_access_put(xe);
	resp.width = 36;

	/* Only write to the output fields of user query */
	if (put_user(resp.engine_frequency, &query_ptr->engine_frequency))
		return -EFAULT;

	if (put_user(resp.cpu_timestamp, &query_ptr->cpu_timestamp))
		return -EFAULT;

	if (put_user(resp.cpu_delta, &query_ptr->cpu_delta))
		return -EFAULT;

	if (put_user(resp.engine_cycles, &query_ptr->engine_cycles))
		return -EFAULT;

	if (put_user(resp.width, &query_ptr->width))
		return -EFAULT;

	return 0;
}

185 186 187 188
static int query_engines(struct xe_device *xe,
			 struct drm_xe_device_query *query)
{
	size_t size = calc_hw_engine_info_size(xe);
189
	struct drm_xe_query_engine_info __user *query_ptr =
190
		u64_to_user_ptr(query->data);
191
	struct drm_xe_query_engine_info *hw_engine_info;
192 193 194 195 196 197 198 199 200
	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;
201
	} else if (XE_IOCTL_DBG(xe, query->size != size)) {
202 203 204 205
		return -EINVAL;
	}

	hw_engine_info = kmalloc(size, GFP_KERNEL);
206
	if (!hw_engine_info)
207 208 209 210 211 212 213
		return -ENOMEM;

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

214
			hw_engine_info[i].instance.engine_class =
215
				xe_to_user_engine_class[hwe->class];
216
			hw_engine_info[i].instance.engine_instance =
217
				hwe->logical_instance;
218 219 220
			hw_engine_info[i].instance.gt_id = gt->info.id;
			hw_engine_info[i].instance.pad = 0;
			memset(hw_engine_info->reserved, 0, sizeof(hw_engine_info->reserved));
221 222

			i++;
223 224 225 226 227 228 229 230 231 232 233
		}

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

	return 0;
}

234
static size_t calc_mem_regions_size(struct xe_device *xe)
235 236 237 238 239 240 241 242
{
	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++;

243
	return offsetof(struct drm_xe_query_mem_regions, mem_regions[num_managers]);
244 245
}

246
static int query_mem_regions(struct xe_device *xe,
247
			    struct drm_xe_device_query *query)
248
{
249
	size_t size = calc_mem_regions_size(xe);
250
	struct drm_xe_query_mem_regions *mem_regions;
251
	struct drm_xe_query_mem_regions __user *query_ptr =
252 253 254 255 256 257 258
		u64_to_user_ptr(query->data);
	struct ttm_resource_manager *man;
	int ret, i;

	if (query->size == 0) {
		query->size = size;
		return 0;
259
	} else if (XE_IOCTL_DBG(xe, query->size != size)) {
260 261 262
		return -EINVAL;
	}

263 264
	mem_regions = kzalloc(size, GFP_KERNEL);
	if (XE_IOCTL_DBG(xe, !mem_regions))
265 266 267
		return -ENOMEM;

	man = ttm_manager_type(&xe->ttm, XE_PL_TT);
268 269 270 271
	mem_regions->mem_regions[0].mem_class = DRM_XE_MEM_REGION_CLASS_SYSMEM;
	mem_regions->mem_regions[0].instance = 0;
	mem_regions->mem_regions[0].min_page_size = PAGE_SIZE;
	mem_regions->mem_regions[0].total_size = man->size << PAGE_SHIFT;
272
	if (perfmon_capable())
273 274
		mem_regions->mem_regions[0].used = ttm_resource_manager_usage(man);
	mem_regions->num_mem_regions = 1;
275 276 277 278

	for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
		man = ttm_manager_type(&xe->ttm, i);
		if (man) {
279
			mem_regions->mem_regions[mem_regions->num_mem_regions].mem_class =
280
				DRM_XE_MEM_REGION_CLASS_VRAM;
281 282 283
			mem_regions->mem_regions[mem_regions->num_mem_regions].instance =
				mem_regions->num_mem_regions;
			mem_regions->mem_regions[mem_regions->num_mem_regions].min_page_size =
284 285
				xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ?
				SZ_64K : PAGE_SIZE;
286
			mem_regions->mem_regions[mem_regions->num_mem_regions].total_size =
287
				man->size;
288 289

			if (perfmon_capable()) {
290
				xe_ttm_vram_get_used(man,
291 292 293 294
					&mem_regions->mem_regions
					[mem_regions->num_mem_regions].used,
					&mem_regions->mem_regions
					[mem_regions->num_mem_regions].cpu_visible_used);
295 296
			}

297
			mem_regions->mem_regions[mem_regions->num_mem_regions].cpu_visible_size =
298
				xe_ttm_vram_get_cpu_visible_size(man);
299
			mem_regions->num_mem_regions++;
300 301 302
		}
	}

303
	if (!copy_to_user(query_ptr, mem_regions, size))
304 305 306 307
		ret = 0;
	else
		ret = -ENOSPC;

308
	kfree(mem_regions);
309 310 311 312 313
	return ret;
}

static int query_config(struct xe_device *xe, struct drm_xe_device_query *query)
{
314
	const u32 num_params = DRM_XE_QUERY_CONFIG_MAX_EXEC_QUEUE_PRIORITY + 1;
315 316 317 318 319 320 321 322 323
	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;
324
	} else if (XE_IOCTL_DBG(xe, query->size != size)) {
325 326 327 328
		return -EINVAL;
	}

	config = kzalloc(size, GFP_KERNEL);
329
	if (!config)
330 331 332
		return -ENOMEM;

	config->num_params = num_params;
333
	config->info[DRM_XE_QUERY_CONFIG_REV_AND_DEVICE_ID] =
334
		xe->info.devid | (xe->info.revid << 16);
335
	if (xe_device_get_root_tile(xe)->mem.vram.usable_size)
336
		config->info[DRM_XE_QUERY_CONFIG_FLAGS] =
337
			DRM_XE_QUERY_CONFIG_FLAG_HAS_VRAM;
338
	config->info[DRM_XE_QUERY_CONFIG_MIN_ALIGNMENT] =
339
		xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K;
340 341
	config->info[DRM_XE_QUERY_CONFIG_VA_BITS] = xe->info.va_bits;
	config->info[DRM_XE_QUERY_CONFIG_MAX_EXEC_QUEUE_PRIORITY] =
342
		xe_exec_queue_device_get_max_priority(xe);
343 344 345 346 347 348 349 350 351 352

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

	return 0;
}

353
static int query_gt_list(struct xe_device *xe, struct drm_xe_device_query *query)
354 355
{
	struct xe_gt *gt;
356
	size_t size = sizeof(struct drm_xe_query_gt_list) +
357
		xe->info.gt_count * sizeof(struct drm_xe_gt);
358
	struct drm_xe_query_gt_list __user *query_ptr =
359
		u64_to_user_ptr(query->data);
360
	struct drm_xe_query_gt_list *gt_list;
361 362 363 364 365
	u8 id;

	if (query->size == 0) {
		query->size = size;
		return 0;
366
	} else if (XE_IOCTL_DBG(xe, query->size != size)) {
367 368 369
		return -EINVAL;
	}

370 371
	gt_list = kzalloc(size, GFP_KERNEL);
	if (!gt_list)
372 373
		return -ENOMEM;

374
	gt_list->num_gt = xe->info.gt_count;
375
	for_each_gt(gt, xe, id) {
376
		if (xe_gt_is_media_type(gt))
377
			gt_list->gt_list[id].type = DRM_XE_QUERY_GT_TYPE_MEDIA;
378
		else
379
			gt_list->gt_list[id].type = DRM_XE_QUERY_GT_TYPE_MAIN;
380 381
		gt_list->gt_list[id].gt_id = gt->info.id;
		gt_list->gt_list[id].clock_freq = gt->info.clock_freq;
382
		if (!IS_DGFX(xe))
383
			gt_list->gt_list[id].near_mem_regions = 0x1;
384
		else
385
			gt_list->gt_list[id].near_mem_regions =
386
				BIT(gt_to_tile(gt)->id) << 1;
387 388
		gt_list->gt_list[id].far_mem_regions = xe->info.mem_region_mask ^
			gt_list->gt_list[id].near_mem_regions;
389 390
	}

391 392
	if (copy_to_user(query_ptr, gt_list, size)) {
		kfree(gt_list);
393 394
		return -EFAULT;
	}
395
	kfree(gt_list);
396 397 398 399 400 401 402

	return 0;
}

static int query_hwconfig(struct xe_device *xe,
			  struct drm_xe_device_query *query)
{
403
	struct xe_gt *gt = xe_root_mmio_gt(xe);
404 405 406 407 408 409 410
	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;
411
	} else if (XE_IOCTL_DBG(xe, query->size != size)) {
412 413 414 415
		return -EINVAL;
	}

	hwconfig = kzalloc(size, GFP_KERNEL);
416
	if (!hwconfig)
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
		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)
{
434
	return xe->info.gt_count *
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
		(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;
470
	} else if (XE_IOCTL_DBG(xe, query->size != size)) {
471 472 473 474 475 476
		return -EINVAL;
	}

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

477
		topo.type = DRM_XE_TOPO_DSS_GEOMETRY;
478 479 480 481 482 483
		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);

484
		topo.type = DRM_XE_TOPO_DSS_COMPUTE;
485 486 487 488 489 490
		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);

491
		topo.type = DRM_XE_TOPO_EU_PER_DSS;
492 493 494 495 496 497 498 499 500 501 502 503 504
		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,
505
	query_mem_regions,
506
	query_config,
507
	query_gt_list,
508 509
	query_hwconfig,
	query_gt_topology,
510
	query_engine_cycles,
511 512 513 514 515 516 517 518
};

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;

519 520
	if (XE_IOCTL_DBG(xe, query->extensions) ||
	    XE_IOCTL_DBG(xe, query->reserved[0] || query->reserved[1]))
521 522
		return -EINVAL;

523
	if (XE_IOCTL_DBG(xe, query->query >= ARRAY_SIZE(xe_query_funcs)))
524 525 526
		return -EINVAL;

	idx = array_index_nospec(query->query, ARRAY_SIZE(xe_query_funcs));
527
	if (XE_IOCTL_DBG(xe, !xe_query_funcs[idx]))
528 529 530 531
		return -EINVAL;

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