1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2022 Intel Corporation
4 */
5
6 #include "xe_query.h"
7
8 #include <linux/nospec.h>
9 #include <linux/sched/clock.h>
10
11 #include <drm/ttm/ttm_placement.h>
12 #include <generated/xe_wa_oob.h>
13 #include <uapi/drm/xe_drm.h>
14
15 #include "regs/xe_engine_regs.h"
16 #include "regs/xe_gt_regs.h"
17 #include "xe_bo.h"
18 #include "xe_device.h"
19 #include "xe_eu_stall.h"
20 #include "xe_exec_queue.h"
21 #include "xe_force_wake.h"
22 #include "xe_ggtt.h"
23 #include "xe_gt.h"
24 #include "xe_guc_hwconfig.h"
25 #include "xe_macros.h"
26 #include "xe_mmio.h"
27 #include "xe_oa.h"
28 #include "xe_pxp.h"
29 #include "xe_ttm_vram_mgr.h"
30 #include "xe_wa.h"
31
32 static const u16 xe_to_user_engine_class[] = {
33 [XE_ENGINE_CLASS_RENDER] = DRM_XE_ENGINE_CLASS_RENDER,
34 [XE_ENGINE_CLASS_COPY] = DRM_XE_ENGINE_CLASS_COPY,
35 [XE_ENGINE_CLASS_VIDEO_DECODE] = DRM_XE_ENGINE_CLASS_VIDEO_DECODE,
36 [XE_ENGINE_CLASS_VIDEO_ENHANCE] = DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE,
37 [XE_ENGINE_CLASS_COMPUTE] = DRM_XE_ENGINE_CLASS_COMPUTE,
38 };
39
40 static const enum xe_engine_class user_to_xe_engine_class[] = {
41 [DRM_XE_ENGINE_CLASS_RENDER] = XE_ENGINE_CLASS_RENDER,
42 [DRM_XE_ENGINE_CLASS_COPY] = XE_ENGINE_CLASS_COPY,
43 [DRM_XE_ENGINE_CLASS_VIDEO_DECODE] = XE_ENGINE_CLASS_VIDEO_DECODE,
44 [DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE] = XE_ENGINE_CLASS_VIDEO_ENHANCE,
45 [DRM_XE_ENGINE_CLASS_COMPUTE] = XE_ENGINE_CLASS_COMPUTE,
46 };
47
calc_hw_engine_info_size(struct xe_device * xe)48 static size_t calc_hw_engine_info_size(struct xe_device *xe)
49 {
50 struct xe_hw_engine *hwe;
51 enum xe_hw_engine_id id;
52 struct xe_gt *gt;
53 u8 gt_id;
54 int i = 0;
55
56 for_each_gt(gt, xe, gt_id)
57 for_each_hw_engine(hwe, gt, id) {
58 if (xe_hw_engine_is_reserved(hwe))
59 continue;
60 i++;
61 }
62
63 return sizeof(struct drm_xe_query_engines) +
64 i * sizeof(struct drm_xe_engine);
65 }
66
67 typedef u64 (*__ktime_func_t)(void);
__clock_id_to_func(clockid_t clk_id)68 static __ktime_func_t __clock_id_to_func(clockid_t clk_id)
69 {
70 /*
71 * Use logic same as the perf subsystem to allow user to select the
72 * reference clock id to be used for timestamps.
73 */
74 switch (clk_id) {
75 case CLOCK_MONOTONIC:
76 return &ktime_get_ns;
77 case CLOCK_MONOTONIC_RAW:
78 return &ktime_get_raw_ns;
79 case CLOCK_REALTIME:
80 return &ktime_get_real_ns;
81 case CLOCK_BOOTTIME:
82 return &ktime_get_boottime_ns;
83 case CLOCK_TAI:
84 return &ktime_get_clocktai_ns;
85 default:
86 return NULL;
87 }
88 }
89
90 static void
hwe_read_timestamp(struct xe_hw_engine * hwe,u64 * engine_ts,u64 * cpu_ts,u64 * cpu_delta,__ktime_func_t cpu_clock)91 hwe_read_timestamp(struct xe_hw_engine *hwe, u64 *engine_ts, u64 *cpu_ts,
92 u64 *cpu_delta, __ktime_func_t cpu_clock)
93 {
94 struct xe_mmio *mmio = &hwe->gt->mmio;
95 u32 upper, lower, old_upper, loop = 0;
96 struct xe_reg upper_reg = RING_TIMESTAMP_UDW(hwe->mmio_base),
97 lower_reg = RING_TIMESTAMP(hwe->mmio_base);
98
99 upper = xe_mmio_read32(mmio, upper_reg);
100 do {
101 *cpu_delta = local_clock();
102 *cpu_ts = cpu_clock();
103 lower = xe_mmio_read32(mmio, lower_reg);
104 *cpu_delta = local_clock() - *cpu_delta;
105 old_upper = upper;
106 upper = xe_mmio_read32(mmio, upper_reg);
107 } while (upper != old_upper && loop++ < 2);
108
109 *engine_ts = (u64)upper << 32 | lower;
110 }
111
112 static int
query_engine_cycles(struct xe_device * xe,struct drm_xe_device_query * query)113 query_engine_cycles(struct xe_device *xe,
114 struct drm_xe_device_query *query)
115 {
116 struct drm_xe_query_engine_cycles __user *query_ptr;
117 struct drm_xe_engine_class_instance *eci;
118 struct drm_xe_query_engine_cycles resp;
119 size_t size = sizeof(resp);
120 __ktime_func_t cpu_clock;
121 struct xe_hw_engine *hwe;
122 struct xe_gt *gt;
123 unsigned int fw_ref;
124
125 if (IS_SRIOV_VF(xe))
126 return -EOPNOTSUPP;
127
128 if (query->size == 0) {
129 query->size = size;
130 return 0;
131 } else if (XE_IOCTL_DBG(xe, query->size != size)) {
132 return -EINVAL;
133 }
134
135 query_ptr = u64_to_user_ptr(query->data);
136 if (copy_from_user(&resp, query_ptr, size))
137 return -EFAULT;
138
139 cpu_clock = __clock_id_to_func(resp.clockid);
140 if (!cpu_clock)
141 return -EINVAL;
142
143 eci = &resp.eci;
144 if (eci->gt_id >= xe->info.max_gt_per_tile)
145 return -EINVAL;
146
147 gt = xe_device_get_gt(xe, eci->gt_id);
148 if (!gt)
149 return -EINVAL;
150
151 if (eci->engine_class >= ARRAY_SIZE(user_to_xe_engine_class))
152 return -EINVAL;
153
154 hwe = xe_gt_hw_engine(gt, user_to_xe_engine_class[eci->engine_class],
155 eci->engine_instance, true);
156 if (!hwe)
157 return -EINVAL;
158
159 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FORCEWAKE_ALL);
160 if (!xe_force_wake_ref_has_domain(fw_ref, XE_FORCEWAKE_ALL)) {
161 xe_force_wake_put(gt_to_fw(gt), fw_ref);
162 return -EIO;
163 }
164
165 hwe_read_timestamp(hwe, &resp.engine_cycles, &resp.cpu_timestamp,
166 &resp.cpu_delta, cpu_clock);
167
168 xe_force_wake_put(gt_to_fw(gt), fw_ref);
169
170 if (GRAPHICS_VER(xe) >= 20)
171 resp.width = 64;
172 else
173 resp.width = 36;
174
175 /* Only write to the output fields of user query */
176 if (put_user(resp.cpu_timestamp, &query_ptr->cpu_timestamp) ||
177 put_user(resp.cpu_delta, &query_ptr->cpu_delta) ||
178 put_user(resp.engine_cycles, &query_ptr->engine_cycles) ||
179 put_user(resp.width, &query_ptr->width))
180 return -EFAULT;
181
182 return 0;
183 }
184
query_engines(struct xe_device * xe,struct drm_xe_device_query * query)185 static int query_engines(struct xe_device *xe,
186 struct drm_xe_device_query *query)
187 {
188 size_t size = calc_hw_engine_info_size(xe);
189 struct drm_xe_query_engines __user *query_ptr =
190 u64_to_user_ptr(query->data);
191 struct drm_xe_query_engines *engines;
192 struct xe_hw_engine *hwe;
193 enum xe_hw_engine_id id;
194 struct xe_gt *gt;
195 u8 gt_id;
196 int i = 0;
197
198 if (query->size == 0) {
199 query->size = size;
200 return 0;
201 } else if (XE_IOCTL_DBG(xe, query->size != size)) {
202 return -EINVAL;
203 }
204
205 engines = kzalloc(size, GFP_KERNEL);
206 if (!engines)
207 return -ENOMEM;
208
209 for_each_gt(gt, xe, gt_id)
210 for_each_hw_engine(hwe, gt, id) {
211 if (xe_hw_engine_is_reserved(hwe))
212 continue;
213
214 engines->engines[i].instance.engine_class =
215 xe_to_user_engine_class[hwe->class];
216 engines->engines[i].instance.engine_instance =
217 hwe->logical_instance;
218 engines->engines[i].instance.gt_id = gt->info.id;
219
220 i++;
221 }
222
223 engines->num_engines = i;
224
225 if (copy_to_user(query_ptr, engines, size)) {
226 kfree(engines);
227 return -EFAULT;
228 }
229 kfree(engines);
230
231 return 0;
232 }
233
calc_mem_regions_size(struct xe_device * xe)234 static size_t calc_mem_regions_size(struct xe_device *xe)
235 {
236 u32 num_managers = 1;
237 int i;
238
239 for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i)
240 if (ttm_manager_type(&xe->ttm, i))
241 num_managers++;
242
243 return offsetof(struct drm_xe_query_mem_regions, mem_regions[num_managers]);
244 }
245
query_mem_regions(struct xe_device * xe,struct drm_xe_device_query * query)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 u64_to_user_ptr(query->data);
253 struct ttm_resource_manager *man;
254 int ret, i;
255
256 if (query->size == 0) {
257 query->size = size;
258 return 0;
259 } else if (XE_IOCTL_DBG(xe, query->size != size)) {
260 return -EINVAL;
261 }
262
263 mem_regions = kzalloc(size, GFP_KERNEL);
264 if (XE_IOCTL_DBG(xe, !mem_regions))
265 return -ENOMEM;
266
267 man = ttm_manager_type(&xe->ttm, XE_PL_TT);
268 mem_regions->mem_regions[0].mem_class = DRM_XE_MEM_REGION_CLASS_SYSMEM;
269 /*
270 * The instance needs to be a unique number that represents the index
271 * in the placement mask used at xe_gem_create_ioctl() for the
272 * xe_bo_create() placement.
273 */
274 mem_regions->mem_regions[0].instance = 0;
275 mem_regions->mem_regions[0].min_page_size = PAGE_SIZE;
276 mem_regions->mem_regions[0].total_size = man->size << PAGE_SHIFT;
277 if (perfmon_capable())
278 mem_regions->mem_regions[0].used = ttm_resource_manager_usage(man);
279 mem_regions->num_mem_regions = 1;
280
281 for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
282 man = ttm_manager_type(&xe->ttm, i);
283 if (man) {
284 mem_regions->mem_regions[mem_regions->num_mem_regions].mem_class =
285 DRM_XE_MEM_REGION_CLASS_VRAM;
286 mem_regions->mem_regions[mem_regions->num_mem_regions].instance =
287 mem_regions->num_mem_regions;
288 mem_regions->mem_regions[mem_regions->num_mem_regions].min_page_size =
289 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ?
290 SZ_64K : PAGE_SIZE;
291 mem_regions->mem_regions[mem_regions->num_mem_regions].total_size =
292 man->size;
293
294 if (perfmon_capable()) {
295 xe_ttm_vram_get_used(man,
296 &mem_regions->mem_regions
297 [mem_regions->num_mem_regions].used,
298 &mem_regions->mem_regions
299 [mem_regions->num_mem_regions].cpu_visible_used);
300 }
301
302 mem_regions->mem_regions[mem_regions->num_mem_regions].cpu_visible_size =
303 xe_ttm_vram_get_cpu_visible_size(man);
304 mem_regions->num_mem_regions++;
305 }
306 }
307
308 if (!copy_to_user(query_ptr, mem_regions, size))
309 ret = 0;
310 else
311 ret = -ENOSPC;
312
313 kfree(mem_regions);
314 return ret;
315 }
316
query_config(struct xe_device * xe,struct drm_xe_device_query * query)317 static int query_config(struct xe_device *xe, struct drm_xe_device_query *query)
318 {
319 const u32 num_params = DRM_XE_QUERY_CONFIG_MAX_EXEC_QUEUE_PRIORITY + 1;
320 size_t size =
321 sizeof(struct drm_xe_query_config) + num_params * sizeof(u64);
322 struct drm_xe_query_config __user *query_ptr =
323 u64_to_user_ptr(query->data);
324 struct drm_xe_query_config *config;
325
326 if (query->size == 0) {
327 query->size = size;
328 return 0;
329 } else if (XE_IOCTL_DBG(xe, query->size != size)) {
330 return -EINVAL;
331 }
332
333 config = kzalloc(size, GFP_KERNEL);
334 if (!config)
335 return -ENOMEM;
336
337 config->num_params = num_params;
338 config->info[DRM_XE_QUERY_CONFIG_REV_AND_DEVICE_ID] =
339 xe->info.devid | (xe->info.revid << 16);
340 if (xe_device_get_root_tile(xe)->mem.vram.usable_size)
341 config->info[DRM_XE_QUERY_CONFIG_FLAGS] |=
342 DRM_XE_QUERY_CONFIG_FLAG_HAS_VRAM;
343 if (xe->info.has_usm && IS_ENABLED(CONFIG_DRM_XE_GPUSVM))
344 config->info[DRM_XE_QUERY_CONFIG_FLAGS] |=
345 DRM_XE_QUERY_CONFIG_FLAG_HAS_CPU_ADDR_MIRROR;
346 config->info[DRM_XE_QUERY_CONFIG_FLAGS] |=
347 DRM_XE_QUERY_CONFIG_FLAG_HAS_LOW_LATENCY;
348 config->info[DRM_XE_QUERY_CONFIG_MIN_ALIGNMENT] =
349 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K;
350 config->info[DRM_XE_QUERY_CONFIG_VA_BITS] = xe->info.va_bits;
351 config->info[DRM_XE_QUERY_CONFIG_MAX_EXEC_QUEUE_PRIORITY] =
352 xe_exec_queue_device_get_max_priority(xe);
353
354 if (copy_to_user(query_ptr, config, size)) {
355 kfree(config);
356 return -EFAULT;
357 }
358 kfree(config);
359
360 return 0;
361 }
362
query_gt_list(struct xe_device * xe,struct drm_xe_device_query * query)363 static int query_gt_list(struct xe_device *xe, struct drm_xe_device_query *query)
364 {
365 struct xe_gt *gt;
366 size_t size = sizeof(struct drm_xe_query_gt_list) +
367 xe->info.gt_count * sizeof(struct drm_xe_gt);
368 struct drm_xe_query_gt_list __user *query_ptr =
369 u64_to_user_ptr(query->data);
370 struct drm_xe_query_gt_list *gt_list;
371 int iter = 0;
372 u8 id;
373
374 if (query->size == 0) {
375 query->size = size;
376 return 0;
377 } else if (XE_IOCTL_DBG(xe, query->size != size)) {
378 return -EINVAL;
379 }
380
381 gt_list = kzalloc(size, GFP_KERNEL);
382 if (!gt_list)
383 return -ENOMEM;
384
385 gt_list->num_gt = xe->info.gt_count;
386
387 for_each_gt(gt, xe, id) {
388 if (xe_gt_is_media_type(gt))
389 gt_list->gt_list[iter].type = DRM_XE_QUERY_GT_TYPE_MEDIA;
390 else
391 gt_list->gt_list[iter].type = DRM_XE_QUERY_GT_TYPE_MAIN;
392 gt_list->gt_list[iter].tile_id = gt_to_tile(gt)->id;
393 gt_list->gt_list[iter].gt_id = gt->info.id;
394 gt_list->gt_list[iter].reference_clock = gt->info.reference_clock;
395 /*
396 * The mem_regions indexes in the mask below need to
397 * directly identify the struct
398 * drm_xe_query_mem_regions' instance constructed at
399 * query_mem_regions()
400 *
401 * For our current platforms:
402 * Bit 0 -> System Memory
403 * Bit 1 -> VRAM0 on Tile0
404 * Bit 2 -> VRAM1 on Tile1
405 * However the uAPI is generic and it's userspace's
406 * responsibility to check the mem_class, without any
407 * assumption.
408 */
409 if (!IS_DGFX(xe))
410 gt_list->gt_list[iter].near_mem_regions = 0x1;
411 else
412 gt_list->gt_list[iter].near_mem_regions =
413 BIT(gt_to_tile(gt)->id) << 1;
414 gt_list->gt_list[iter].far_mem_regions = xe->info.mem_region_mask ^
415 gt_list->gt_list[iter].near_mem_regions;
416
417 gt_list->gt_list[iter].ip_ver_major =
418 REG_FIELD_GET(GMD_ID_ARCH_MASK, gt->info.gmdid);
419 gt_list->gt_list[iter].ip_ver_minor =
420 REG_FIELD_GET(GMD_ID_RELEASE_MASK, gt->info.gmdid);
421 gt_list->gt_list[iter].ip_ver_rev =
422 REG_FIELD_GET(GMD_ID_REVID, gt->info.gmdid);
423
424 iter++;
425 }
426
427 if (copy_to_user(query_ptr, gt_list, size)) {
428 kfree(gt_list);
429 return -EFAULT;
430 }
431 kfree(gt_list);
432
433 return 0;
434 }
435
query_hwconfig(struct xe_device * xe,struct drm_xe_device_query * query)436 static int query_hwconfig(struct xe_device *xe,
437 struct drm_xe_device_query *query)
438 {
439 struct xe_gt *gt = xe_root_mmio_gt(xe);
440 size_t size = xe_guc_hwconfig_size(>->uc.guc);
441 void __user *query_ptr = u64_to_user_ptr(query->data);
442 void *hwconfig;
443
444 if (query->size == 0) {
445 query->size = size;
446 return 0;
447 } else if (XE_IOCTL_DBG(xe, query->size != size)) {
448 return -EINVAL;
449 }
450
451 hwconfig = kzalloc(size, GFP_KERNEL);
452 if (!hwconfig)
453 return -ENOMEM;
454
455 xe_guc_hwconfig_copy(>->uc.guc, hwconfig);
456
457 if (copy_to_user(query_ptr, hwconfig, size)) {
458 kfree(hwconfig);
459 return -EFAULT;
460 }
461 kfree(hwconfig);
462
463 return 0;
464 }
465
calc_topo_query_size(struct xe_device * xe)466 static size_t calc_topo_query_size(struct xe_device *xe)
467 {
468 struct xe_gt *gt;
469 size_t query_size = 0;
470 int id;
471
472 for_each_gt(gt, xe, id) {
473 query_size += 3 * sizeof(struct drm_xe_query_topology_mask) +
474 sizeof_field(struct xe_gt, fuse_topo.g_dss_mask) +
475 sizeof_field(struct xe_gt, fuse_topo.c_dss_mask) +
476 sizeof_field(struct xe_gt, fuse_topo.eu_mask_per_dss);
477
478 /* L3bank mask may not be available for some GTs */
479 if (!XE_WA(gt, no_media_l3))
480 query_size += sizeof(struct drm_xe_query_topology_mask) +
481 sizeof_field(struct xe_gt, fuse_topo.l3_bank_mask);
482 }
483
484 return query_size;
485 }
486
copy_mask(void __user ** ptr,struct drm_xe_query_topology_mask * topo,void * mask,size_t mask_size)487 static int copy_mask(void __user **ptr,
488 struct drm_xe_query_topology_mask *topo,
489 void *mask, size_t mask_size)
490 {
491 topo->num_bytes = mask_size;
492
493 if (copy_to_user(*ptr, topo, sizeof(*topo)))
494 return -EFAULT;
495 *ptr += sizeof(topo);
496
497 if (copy_to_user(*ptr, mask, mask_size))
498 return -EFAULT;
499 *ptr += mask_size;
500
501 return 0;
502 }
503
query_gt_topology(struct xe_device * xe,struct drm_xe_device_query * query)504 static int query_gt_topology(struct xe_device *xe,
505 struct drm_xe_device_query *query)
506 {
507 void __user *query_ptr = u64_to_user_ptr(query->data);
508 size_t size = calc_topo_query_size(xe);
509 struct drm_xe_query_topology_mask topo;
510 struct xe_gt *gt;
511 int id;
512
513 if (query->size == 0) {
514 query->size = size;
515 return 0;
516 } else if (XE_IOCTL_DBG(xe, query->size != size)) {
517 return -EINVAL;
518 }
519
520 for_each_gt(gt, xe, id) {
521 int err;
522
523 topo.gt_id = id;
524
525 topo.type = DRM_XE_TOPO_DSS_GEOMETRY;
526 err = copy_mask(&query_ptr, &topo, gt->fuse_topo.g_dss_mask,
527 sizeof(gt->fuse_topo.g_dss_mask));
528 if (err)
529 return err;
530
531 topo.type = DRM_XE_TOPO_DSS_COMPUTE;
532 err = copy_mask(&query_ptr, &topo, gt->fuse_topo.c_dss_mask,
533 sizeof(gt->fuse_topo.c_dss_mask));
534 if (err)
535 return err;
536
537 /*
538 * If the kernel doesn't have a way to obtain a correct L3bank
539 * mask, then it's better to omit L3 from the query rather than
540 * reporting bogus or zeroed information to userspace.
541 */
542 if (!XE_WA(gt, no_media_l3)) {
543 topo.type = DRM_XE_TOPO_L3_BANK;
544 err = copy_mask(&query_ptr, &topo, gt->fuse_topo.l3_bank_mask,
545 sizeof(gt->fuse_topo.l3_bank_mask));
546 if (err)
547 return err;
548 }
549
550 topo.type = gt->fuse_topo.eu_type == XE_GT_EU_TYPE_SIMD16 ?
551 DRM_XE_TOPO_SIMD16_EU_PER_DSS :
552 DRM_XE_TOPO_EU_PER_DSS;
553 err = copy_mask(&query_ptr, &topo,
554 gt->fuse_topo.eu_mask_per_dss,
555 sizeof(gt->fuse_topo.eu_mask_per_dss));
556 if (err)
557 return err;
558 }
559
560 return 0;
561 }
562
563 static int
query_uc_fw_version(struct xe_device * xe,struct drm_xe_device_query * query)564 query_uc_fw_version(struct xe_device *xe, struct drm_xe_device_query *query)
565 {
566 struct drm_xe_query_uc_fw_version __user *query_ptr = u64_to_user_ptr(query->data);
567 size_t size = sizeof(struct drm_xe_query_uc_fw_version);
568 struct drm_xe_query_uc_fw_version resp;
569 struct xe_uc_fw_version *version = NULL;
570
571 if (query->size == 0) {
572 query->size = size;
573 return 0;
574 } else if (XE_IOCTL_DBG(xe, query->size != size)) {
575 return -EINVAL;
576 }
577
578 if (copy_from_user(&resp, query_ptr, size))
579 return -EFAULT;
580
581 if (XE_IOCTL_DBG(xe, resp.pad || resp.pad2 || resp.reserved))
582 return -EINVAL;
583
584 switch (resp.uc_type) {
585 case XE_QUERY_UC_TYPE_GUC_SUBMISSION: {
586 struct xe_guc *guc = &xe->tiles[0].primary_gt->uc.guc;
587
588 version = &guc->fw.versions.found[XE_UC_FW_VER_COMPATIBILITY];
589 break;
590 }
591 case XE_QUERY_UC_TYPE_HUC: {
592 struct xe_gt *media_gt = NULL;
593 struct xe_huc *huc;
594
595 if (MEDIA_VER(xe) >= 13) {
596 struct xe_tile *tile;
597 u8 gt_id;
598
599 for_each_tile(tile, xe, gt_id) {
600 if (tile->media_gt) {
601 media_gt = tile->media_gt;
602 break;
603 }
604 }
605 } else {
606 media_gt = xe->tiles[0].primary_gt;
607 }
608
609 if (!media_gt)
610 break;
611
612 huc = &media_gt->uc.huc;
613 if (huc->fw.status == XE_UC_FIRMWARE_RUNNING)
614 version = &huc->fw.versions.found[XE_UC_FW_VER_RELEASE];
615 break;
616 }
617 default:
618 return -EINVAL;
619 }
620
621 if (version) {
622 resp.branch_ver = 0;
623 resp.major_ver = version->major;
624 resp.minor_ver = version->minor;
625 resp.patch_ver = version->patch;
626 } else {
627 return -ENODEV;
628 }
629
630 if (copy_to_user(query_ptr, &resp, size))
631 return -EFAULT;
632
633 return 0;
634 }
635
calc_oa_unit_query_size(struct xe_device * xe)636 static size_t calc_oa_unit_query_size(struct xe_device *xe)
637 {
638 size_t size = sizeof(struct drm_xe_query_oa_units);
639 struct xe_gt *gt;
640 int i, id;
641
642 for_each_gt(gt, xe, id) {
643 for (i = 0; i < gt->oa.num_oa_units; i++) {
644 size += sizeof(struct drm_xe_oa_unit);
645 size += gt->oa.oa_unit[i].num_engines *
646 sizeof(struct drm_xe_engine_class_instance);
647 }
648 }
649
650 return size;
651 }
652
query_oa_units(struct xe_device * xe,struct drm_xe_device_query * query)653 static int query_oa_units(struct xe_device *xe,
654 struct drm_xe_device_query *query)
655 {
656 void __user *query_ptr = u64_to_user_ptr(query->data);
657 size_t size = calc_oa_unit_query_size(xe);
658 struct drm_xe_query_oa_units *qoa;
659 enum xe_hw_engine_id hwe_id;
660 struct drm_xe_oa_unit *du;
661 struct xe_hw_engine *hwe;
662 struct xe_oa_unit *u;
663 int gt_id, i, j, ret;
664 struct xe_gt *gt;
665 u8 *pdu;
666
667 if (query->size == 0) {
668 query->size = size;
669 return 0;
670 } else if (XE_IOCTL_DBG(xe, query->size != size)) {
671 return -EINVAL;
672 }
673
674 qoa = kzalloc(size, GFP_KERNEL);
675 if (!qoa)
676 return -ENOMEM;
677
678 pdu = (u8 *)&qoa->oa_units[0];
679 for_each_gt(gt, xe, gt_id) {
680 for (i = 0; i < gt->oa.num_oa_units; i++) {
681 u = >->oa.oa_unit[i];
682 du = (struct drm_xe_oa_unit *)pdu;
683
684 du->oa_unit_id = u->oa_unit_id;
685 du->oa_unit_type = u->type;
686 du->oa_timestamp_freq = xe_oa_timestamp_frequency(gt);
687 du->capabilities = DRM_XE_OA_CAPS_BASE | DRM_XE_OA_CAPS_SYNCS |
688 DRM_XE_OA_CAPS_OA_BUFFER_SIZE |
689 DRM_XE_OA_CAPS_WAIT_NUM_REPORTS |
690 DRM_XE_OA_CAPS_OAM;
691 j = 0;
692 for_each_hw_engine(hwe, gt, hwe_id) {
693 if (!xe_hw_engine_is_reserved(hwe) &&
694 xe_oa_unit_id(hwe) == u->oa_unit_id) {
695 du->eci[j].engine_class =
696 xe_to_user_engine_class[hwe->class];
697 du->eci[j].engine_instance = hwe->logical_instance;
698 du->eci[j].gt_id = gt->info.id;
699 j++;
700 }
701 }
702 du->num_engines = j;
703 pdu += sizeof(*du) + j * sizeof(du->eci[0]);
704 qoa->num_oa_units++;
705 }
706 }
707
708 ret = copy_to_user(query_ptr, qoa, size);
709 kfree(qoa);
710
711 return ret ? -EFAULT : 0;
712 }
713
query_pxp_status(struct xe_device * xe,struct drm_xe_device_query * query)714 static int query_pxp_status(struct xe_device *xe, struct drm_xe_device_query *query)
715 {
716 struct drm_xe_query_pxp_status __user *query_ptr = u64_to_user_ptr(query->data);
717 size_t size = sizeof(struct drm_xe_query_pxp_status);
718 struct drm_xe_query_pxp_status resp = { 0 };
719 int ret;
720
721 if (query->size == 0) {
722 query->size = size;
723 return 0;
724 } else if (XE_IOCTL_DBG(xe, query->size != size)) {
725 return -EINVAL;
726 }
727
728 ret = xe_pxp_get_readiness_status(xe->pxp);
729 if (ret < 0)
730 return ret;
731
732 resp.status = ret;
733 resp.supported_session_types = BIT(DRM_XE_PXP_TYPE_HWDRM);
734
735 if (copy_to_user(query_ptr, &resp, size))
736 return -EFAULT;
737
738 return 0;
739 }
740
query_eu_stall(struct xe_device * xe,struct drm_xe_device_query * query)741 static int query_eu_stall(struct xe_device *xe,
742 struct drm_xe_device_query *query)
743 {
744 void __user *query_ptr = u64_to_user_ptr(query->data);
745 struct drm_xe_query_eu_stall *info;
746 size_t size, array_size;
747 const u64 *rates;
748 u32 num_rates;
749 int ret;
750
751 if (!xe_eu_stall_supported_on_platform(xe)) {
752 drm_dbg(&xe->drm, "EU stall monitoring is not supported on this platform\n");
753 return -ENODEV;
754 }
755
756 array_size = xe_eu_stall_get_sampling_rates(&num_rates, &rates);
757 size = sizeof(struct drm_xe_query_eu_stall) + array_size;
758
759 if (query->size == 0) {
760 query->size = size;
761 return 0;
762 } else if (XE_IOCTL_DBG(xe, query->size != size)) {
763 return -EINVAL;
764 }
765
766 info = kzalloc(size, GFP_KERNEL);
767 if (!info)
768 return -ENOMEM;
769
770 info->num_sampling_rates = num_rates;
771 info->capabilities = DRM_XE_EU_STALL_CAPS_BASE;
772 info->record_size = xe_eu_stall_data_record_size(xe);
773 info->per_xecore_buf_size = xe_eu_stall_get_per_xecore_buf_size();
774 memcpy(info->sampling_rates, rates, array_size);
775
776 ret = copy_to_user(query_ptr, info, size);
777 kfree(info);
778
779 return ret ? -EFAULT : 0;
780 }
781
782 static int (* const xe_query_funcs[])(struct xe_device *xe,
783 struct drm_xe_device_query *query) = {
784 query_engines,
785 query_mem_regions,
786 query_config,
787 query_gt_list,
788 query_hwconfig,
789 query_gt_topology,
790 query_engine_cycles,
791 query_uc_fw_version,
792 query_oa_units,
793 query_pxp_status,
794 query_eu_stall,
795 };
796
xe_query_ioctl(struct drm_device * dev,void * data,struct drm_file * file)797 int xe_query_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
798 {
799 struct xe_device *xe = to_xe_device(dev);
800 struct drm_xe_device_query *query = data;
801 u32 idx;
802
803 if (XE_IOCTL_DBG(xe, query->extensions) ||
804 XE_IOCTL_DBG(xe, query->reserved[0] || query->reserved[1]))
805 return -EINVAL;
806
807 if (XE_IOCTL_DBG(xe, query->query >= ARRAY_SIZE(xe_query_funcs)))
808 return -EINVAL;
809
810 idx = array_index_nospec(query->query, ARRAY_SIZE(xe_query_funcs));
811 if (XE_IOCTL_DBG(xe, !xe_query_funcs[idx]))
812 return -EINVAL;
813
814 return xe_query_funcs[idx](xe, query);
815 }
816