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