1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2021 Intel Corporation 4 */ 5 6 #include "xe_exec_queue.h" 7 8 #include <linux/nospec.h> 9 10 #include <drm/drm_device.h> 11 #include <drm/drm_file.h> 12 #include <uapi/drm/xe_drm.h> 13 14 #include "xe_device.h" 15 #include "xe_gt.h" 16 #include "xe_hw_engine_class_sysfs.h" 17 #include "xe_hw_engine_group.h" 18 #include "xe_hw_fence.h" 19 #include "xe_lrc.h" 20 #include "xe_macros.h" 21 #include "xe_migrate.h" 22 #include "xe_pm.h" 23 #include "xe_ring_ops_types.h" 24 #include "xe_trace.h" 25 #include "xe_vm.h" 26 27 enum xe_exec_queue_sched_prop { 28 XE_EXEC_QUEUE_JOB_TIMEOUT = 0, 29 XE_EXEC_QUEUE_TIMESLICE = 1, 30 XE_EXEC_QUEUE_PREEMPT_TIMEOUT = 2, 31 XE_EXEC_QUEUE_SCHED_PROP_MAX = 3, 32 }; 33 34 static int exec_queue_user_extensions(struct xe_device *xe, struct xe_exec_queue *q, 35 u64 extensions, int ext_number); 36 37 static void __xe_exec_queue_free(struct xe_exec_queue *q) 38 { 39 if (q->vm) 40 xe_vm_put(q->vm); 41 42 if (q->xef) 43 xe_file_put(q->xef); 44 45 kfree(q); 46 } 47 48 static struct xe_exec_queue *__xe_exec_queue_alloc(struct xe_device *xe, 49 struct xe_vm *vm, 50 u32 logical_mask, 51 u16 width, struct xe_hw_engine *hwe, 52 u32 flags, u64 extensions) 53 { 54 struct xe_exec_queue *q; 55 struct xe_gt *gt = hwe->gt; 56 int err; 57 58 /* only kernel queues can be permanent */ 59 XE_WARN_ON((flags & EXEC_QUEUE_FLAG_PERMANENT) && !(flags & EXEC_QUEUE_FLAG_KERNEL)); 60 61 q = kzalloc(struct_size(q, lrc, width), GFP_KERNEL); 62 if (!q) 63 return ERR_PTR(-ENOMEM); 64 65 kref_init(&q->refcount); 66 q->flags = flags; 67 q->hwe = hwe; 68 q->gt = gt; 69 q->class = hwe->class; 70 q->width = width; 71 q->logical_mask = logical_mask; 72 q->fence_irq = >->fence_irq[hwe->class]; 73 q->ring_ops = gt->ring_ops[hwe->class]; 74 q->ops = gt->exec_queue_ops; 75 INIT_LIST_HEAD(&q->lr.link); 76 INIT_LIST_HEAD(&q->multi_gt_link); 77 INIT_LIST_HEAD(&q->hw_engine_group_link); 78 79 q->sched_props.timeslice_us = hwe->eclass->sched_props.timeslice_us; 80 q->sched_props.preempt_timeout_us = 81 hwe->eclass->sched_props.preempt_timeout_us; 82 q->sched_props.job_timeout_ms = 83 hwe->eclass->sched_props.job_timeout_ms; 84 if (q->flags & EXEC_QUEUE_FLAG_KERNEL && 85 q->flags & EXEC_QUEUE_FLAG_HIGH_PRIORITY) 86 q->sched_props.priority = XE_EXEC_QUEUE_PRIORITY_KERNEL; 87 else 88 q->sched_props.priority = XE_EXEC_QUEUE_PRIORITY_NORMAL; 89 90 if (vm) 91 q->vm = xe_vm_get(vm); 92 93 if (extensions) { 94 /* 95 * may set q->usm, must come before xe_lrc_create(), 96 * may overwrite q->sched_props, must come before q->ops->init() 97 */ 98 err = exec_queue_user_extensions(xe, q, extensions, 0); 99 if (err) { 100 __xe_exec_queue_free(q); 101 return ERR_PTR(err); 102 } 103 } 104 105 return q; 106 } 107 108 static int __xe_exec_queue_init(struct xe_exec_queue *q) 109 { 110 struct xe_vm *vm = q->vm; 111 int i, err; 112 113 if (vm) { 114 err = xe_vm_lock(vm, true); 115 if (err) 116 return err; 117 } 118 119 for (i = 0; i < q->width; ++i) { 120 q->lrc[i] = xe_lrc_create(q->hwe, q->vm, SZ_16K); 121 if (IS_ERR(q->lrc[i])) { 122 err = PTR_ERR(q->lrc[i]); 123 goto err_unlock; 124 } 125 } 126 127 if (vm) 128 xe_vm_unlock(vm); 129 130 err = q->ops->init(q); 131 if (err) 132 goto err_lrc; 133 134 return 0; 135 136 err_unlock: 137 if (vm) 138 xe_vm_unlock(vm); 139 err_lrc: 140 for (i = i - 1; i >= 0; --i) 141 xe_lrc_put(q->lrc[i]); 142 return err; 143 } 144 145 struct xe_exec_queue *xe_exec_queue_create(struct xe_device *xe, struct xe_vm *vm, 146 u32 logical_mask, u16 width, 147 struct xe_hw_engine *hwe, u32 flags, 148 u64 extensions) 149 { 150 struct xe_exec_queue *q; 151 int err; 152 153 q = __xe_exec_queue_alloc(xe, vm, logical_mask, width, hwe, flags, 154 extensions); 155 if (IS_ERR(q)) 156 return q; 157 158 err = __xe_exec_queue_init(q); 159 if (err) 160 goto err_post_alloc; 161 162 return q; 163 164 err_post_alloc: 165 __xe_exec_queue_free(q); 166 return ERR_PTR(err); 167 } 168 169 struct xe_exec_queue *xe_exec_queue_create_class(struct xe_device *xe, struct xe_gt *gt, 170 struct xe_vm *vm, 171 enum xe_engine_class class, 172 u32 flags, u64 extensions) 173 { 174 struct xe_hw_engine *hwe, *hwe0 = NULL; 175 enum xe_hw_engine_id id; 176 u32 logical_mask = 0; 177 178 for_each_hw_engine(hwe, gt, id) { 179 if (xe_hw_engine_is_reserved(hwe)) 180 continue; 181 182 if (hwe->class == class) { 183 logical_mask |= BIT(hwe->logical_instance); 184 if (!hwe0) 185 hwe0 = hwe; 186 } 187 } 188 189 if (!logical_mask) 190 return ERR_PTR(-ENODEV); 191 192 return xe_exec_queue_create(xe, vm, logical_mask, 1, hwe0, flags, extensions); 193 } 194 195 /** 196 * xe_exec_queue_create_bind() - Create bind exec queue. 197 * @xe: Xe device. 198 * @tile: tile which bind exec queue belongs to. 199 * @flags: exec queue creation flags 200 * @extensions: exec queue creation extensions 201 * 202 * Normalize bind exec queue creation. Bind exec queue is tied to migration VM 203 * for access to physical memory required for page table programming. On a 204 * faulting devices the reserved copy engine instance must be used to avoid 205 * deadlocking (user binds cannot get stuck behind faults as kernel binds which 206 * resolve faults depend on user binds). On non-faulting devices any copy engine 207 * can be used. 208 * 209 * Returns exec queue on success, ERR_PTR on failure 210 */ 211 struct xe_exec_queue *xe_exec_queue_create_bind(struct xe_device *xe, 212 struct xe_tile *tile, 213 u32 flags, u64 extensions) 214 { 215 struct xe_gt *gt = tile->primary_gt; 216 struct xe_exec_queue *q; 217 struct xe_vm *migrate_vm; 218 219 migrate_vm = xe_migrate_get_vm(tile->migrate); 220 if (xe->info.has_usm) { 221 struct xe_hw_engine *hwe = xe_gt_hw_engine(gt, 222 XE_ENGINE_CLASS_COPY, 223 gt->usm.reserved_bcs_instance, 224 false); 225 226 if (!hwe) { 227 xe_vm_put(migrate_vm); 228 return ERR_PTR(-EINVAL); 229 } 230 231 q = xe_exec_queue_create(xe, migrate_vm, 232 BIT(hwe->logical_instance), 1, hwe, 233 flags, extensions); 234 } else { 235 q = xe_exec_queue_create_class(xe, gt, migrate_vm, 236 XE_ENGINE_CLASS_COPY, flags, 237 extensions); 238 } 239 xe_vm_put(migrate_vm); 240 241 return q; 242 } 243 244 void xe_exec_queue_destroy(struct kref *ref) 245 { 246 struct xe_exec_queue *q = container_of(ref, struct xe_exec_queue, refcount); 247 struct xe_exec_queue *eq, *next; 248 249 xe_exec_queue_last_fence_put_unlocked(q); 250 if (!(q->flags & EXEC_QUEUE_FLAG_BIND_ENGINE_CHILD)) { 251 list_for_each_entry_safe(eq, next, &q->multi_gt_list, 252 multi_gt_link) 253 xe_exec_queue_put(eq); 254 } 255 256 q->ops->fini(q); 257 } 258 259 void xe_exec_queue_fini(struct xe_exec_queue *q) 260 { 261 int i; 262 263 for (i = 0; i < q->width; ++i) 264 xe_lrc_put(q->lrc[i]); 265 __xe_exec_queue_free(q); 266 } 267 268 void xe_exec_queue_assign_name(struct xe_exec_queue *q, u32 instance) 269 { 270 switch (q->class) { 271 case XE_ENGINE_CLASS_RENDER: 272 snprintf(q->name, sizeof(q->name), "rcs%d", instance); 273 break; 274 case XE_ENGINE_CLASS_VIDEO_DECODE: 275 snprintf(q->name, sizeof(q->name), "vcs%d", instance); 276 break; 277 case XE_ENGINE_CLASS_VIDEO_ENHANCE: 278 snprintf(q->name, sizeof(q->name), "vecs%d", instance); 279 break; 280 case XE_ENGINE_CLASS_COPY: 281 snprintf(q->name, sizeof(q->name), "bcs%d", instance); 282 break; 283 case XE_ENGINE_CLASS_COMPUTE: 284 snprintf(q->name, sizeof(q->name), "ccs%d", instance); 285 break; 286 case XE_ENGINE_CLASS_OTHER: 287 snprintf(q->name, sizeof(q->name), "gsccs%d", instance); 288 break; 289 default: 290 XE_WARN_ON(q->class); 291 } 292 } 293 294 struct xe_exec_queue *xe_exec_queue_lookup(struct xe_file *xef, u32 id) 295 { 296 struct xe_exec_queue *q; 297 298 mutex_lock(&xef->exec_queue.lock); 299 q = xa_load(&xef->exec_queue.xa, id); 300 if (q) 301 xe_exec_queue_get(q); 302 mutex_unlock(&xef->exec_queue.lock); 303 304 return q; 305 } 306 307 enum xe_exec_queue_priority 308 xe_exec_queue_device_get_max_priority(struct xe_device *xe) 309 { 310 return capable(CAP_SYS_NICE) ? XE_EXEC_QUEUE_PRIORITY_HIGH : 311 XE_EXEC_QUEUE_PRIORITY_NORMAL; 312 } 313 314 static int exec_queue_set_priority(struct xe_device *xe, struct xe_exec_queue *q, 315 u64 value) 316 { 317 if (XE_IOCTL_DBG(xe, value > XE_EXEC_QUEUE_PRIORITY_HIGH)) 318 return -EINVAL; 319 320 if (XE_IOCTL_DBG(xe, value > xe_exec_queue_device_get_max_priority(xe))) 321 return -EPERM; 322 323 q->sched_props.priority = value; 324 return 0; 325 } 326 327 static bool xe_exec_queue_enforce_schedule_limit(void) 328 { 329 #if IS_ENABLED(CONFIG_DRM_XE_ENABLE_SCHEDTIMEOUT_LIMIT) 330 return true; 331 #else 332 return !capable(CAP_SYS_NICE); 333 #endif 334 } 335 336 static void 337 xe_exec_queue_get_prop_minmax(struct xe_hw_engine_class_intf *eclass, 338 enum xe_exec_queue_sched_prop prop, 339 u32 *min, u32 *max) 340 { 341 switch (prop) { 342 case XE_EXEC_QUEUE_JOB_TIMEOUT: 343 *min = eclass->sched_props.job_timeout_min; 344 *max = eclass->sched_props.job_timeout_max; 345 break; 346 case XE_EXEC_QUEUE_TIMESLICE: 347 *min = eclass->sched_props.timeslice_min; 348 *max = eclass->sched_props.timeslice_max; 349 break; 350 case XE_EXEC_QUEUE_PREEMPT_TIMEOUT: 351 *min = eclass->sched_props.preempt_timeout_min; 352 *max = eclass->sched_props.preempt_timeout_max; 353 break; 354 default: 355 break; 356 } 357 #if IS_ENABLED(CONFIG_DRM_XE_ENABLE_SCHEDTIMEOUT_LIMIT) 358 if (capable(CAP_SYS_NICE)) { 359 switch (prop) { 360 case XE_EXEC_QUEUE_JOB_TIMEOUT: 361 *min = XE_HW_ENGINE_JOB_TIMEOUT_MIN; 362 *max = XE_HW_ENGINE_JOB_TIMEOUT_MAX; 363 break; 364 case XE_EXEC_QUEUE_TIMESLICE: 365 *min = XE_HW_ENGINE_TIMESLICE_MIN; 366 *max = XE_HW_ENGINE_TIMESLICE_MAX; 367 break; 368 case XE_EXEC_QUEUE_PREEMPT_TIMEOUT: 369 *min = XE_HW_ENGINE_PREEMPT_TIMEOUT_MIN; 370 *max = XE_HW_ENGINE_PREEMPT_TIMEOUT_MAX; 371 break; 372 default: 373 break; 374 } 375 } 376 #endif 377 } 378 379 static int exec_queue_set_timeslice(struct xe_device *xe, struct xe_exec_queue *q, 380 u64 value) 381 { 382 u32 min = 0, max = 0; 383 384 xe_exec_queue_get_prop_minmax(q->hwe->eclass, 385 XE_EXEC_QUEUE_TIMESLICE, &min, &max); 386 387 if (xe_exec_queue_enforce_schedule_limit() && 388 !xe_hw_engine_timeout_in_range(value, min, max)) 389 return -EINVAL; 390 391 q->sched_props.timeslice_us = value; 392 return 0; 393 } 394 395 typedef int (*xe_exec_queue_set_property_fn)(struct xe_device *xe, 396 struct xe_exec_queue *q, 397 u64 value); 398 399 static const xe_exec_queue_set_property_fn exec_queue_set_property_funcs[] = { 400 [DRM_XE_EXEC_QUEUE_SET_PROPERTY_PRIORITY] = exec_queue_set_priority, 401 [DRM_XE_EXEC_QUEUE_SET_PROPERTY_TIMESLICE] = exec_queue_set_timeslice, 402 }; 403 404 static int exec_queue_user_ext_set_property(struct xe_device *xe, 405 struct xe_exec_queue *q, 406 u64 extension) 407 { 408 u64 __user *address = u64_to_user_ptr(extension); 409 struct drm_xe_ext_set_property ext; 410 int err; 411 u32 idx; 412 413 err = __copy_from_user(&ext, address, sizeof(ext)); 414 if (XE_IOCTL_DBG(xe, err)) 415 return -EFAULT; 416 417 if (XE_IOCTL_DBG(xe, ext.property >= 418 ARRAY_SIZE(exec_queue_set_property_funcs)) || 419 XE_IOCTL_DBG(xe, ext.pad) || 420 XE_IOCTL_DBG(xe, ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_PRIORITY && 421 ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_TIMESLICE)) 422 return -EINVAL; 423 424 idx = array_index_nospec(ext.property, ARRAY_SIZE(exec_queue_set_property_funcs)); 425 if (!exec_queue_set_property_funcs[idx]) 426 return -EINVAL; 427 428 return exec_queue_set_property_funcs[idx](xe, q, ext.value); 429 } 430 431 typedef int (*xe_exec_queue_user_extension_fn)(struct xe_device *xe, 432 struct xe_exec_queue *q, 433 u64 extension); 434 435 static const xe_exec_queue_user_extension_fn exec_queue_user_extension_funcs[] = { 436 [DRM_XE_EXEC_QUEUE_EXTENSION_SET_PROPERTY] = exec_queue_user_ext_set_property, 437 }; 438 439 #define MAX_USER_EXTENSIONS 16 440 static int exec_queue_user_extensions(struct xe_device *xe, struct xe_exec_queue *q, 441 u64 extensions, int ext_number) 442 { 443 u64 __user *address = u64_to_user_ptr(extensions); 444 struct drm_xe_user_extension ext; 445 int err; 446 u32 idx; 447 448 if (XE_IOCTL_DBG(xe, ext_number >= MAX_USER_EXTENSIONS)) 449 return -E2BIG; 450 451 err = __copy_from_user(&ext, address, sizeof(ext)); 452 if (XE_IOCTL_DBG(xe, err)) 453 return -EFAULT; 454 455 if (XE_IOCTL_DBG(xe, ext.pad) || 456 XE_IOCTL_DBG(xe, ext.name >= 457 ARRAY_SIZE(exec_queue_user_extension_funcs))) 458 return -EINVAL; 459 460 idx = array_index_nospec(ext.name, 461 ARRAY_SIZE(exec_queue_user_extension_funcs)); 462 err = exec_queue_user_extension_funcs[idx](xe, q, extensions); 463 if (XE_IOCTL_DBG(xe, err)) 464 return err; 465 466 if (ext.next_extension) 467 return exec_queue_user_extensions(xe, q, ext.next_extension, 468 ++ext_number); 469 470 return 0; 471 } 472 473 static u32 calc_validate_logical_mask(struct xe_device *xe, struct xe_gt *gt, 474 struct drm_xe_engine_class_instance *eci, 475 u16 width, u16 num_placements) 476 { 477 int len = width * num_placements; 478 int i, j, n; 479 u16 class; 480 u16 gt_id; 481 u32 return_mask = 0, prev_mask; 482 483 if (XE_IOCTL_DBG(xe, !xe_device_uc_enabled(xe) && 484 len > 1)) 485 return 0; 486 487 for (i = 0; i < width; ++i) { 488 u32 current_mask = 0; 489 490 for (j = 0; j < num_placements; ++j) { 491 struct xe_hw_engine *hwe; 492 493 n = j * width + i; 494 495 hwe = xe_hw_engine_lookup(xe, eci[n]); 496 if (XE_IOCTL_DBG(xe, !hwe)) 497 return 0; 498 499 if (XE_IOCTL_DBG(xe, xe_hw_engine_is_reserved(hwe))) 500 return 0; 501 502 if (XE_IOCTL_DBG(xe, n && eci[n].gt_id != gt_id) || 503 XE_IOCTL_DBG(xe, n && eci[n].engine_class != class)) 504 return 0; 505 506 class = eci[n].engine_class; 507 gt_id = eci[n].gt_id; 508 509 if (width == 1 || !i) 510 return_mask |= BIT(eci[n].engine_instance); 511 current_mask |= BIT(eci[n].engine_instance); 512 } 513 514 /* Parallel submissions must be logically contiguous */ 515 if (i && XE_IOCTL_DBG(xe, current_mask != prev_mask << 1)) 516 return 0; 517 518 prev_mask = current_mask; 519 } 520 521 return return_mask; 522 } 523 524 int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data, 525 struct drm_file *file) 526 { 527 struct xe_device *xe = to_xe_device(dev); 528 struct xe_file *xef = to_xe_file(file); 529 struct drm_xe_exec_queue_create *args = data; 530 struct drm_xe_engine_class_instance eci[XE_HW_ENGINE_MAX_INSTANCE]; 531 struct drm_xe_engine_class_instance __user *user_eci = 532 u64_to_user_ptr(args->instances); 533 struct xe_hw_engine *hwe; 534 struct xe_vm *vm; 535 struct xe_gt *gt; 536 struct xe_tile *tile; 537 struct xe_exec_queue *q = NULL; 538 u32 logical_mask; 539 u32 id; 540 u32 len; 541 int err; 542 543 if (XE_IOCTL_DBG(xe, args->flags) || 544 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) 545 return -EINVAL; 546 547 len = args->width * args->num_placements; 548 if (XE_IOCTL_DBG(xe, !len || len > XE_HW_ENGINE_MAX_INSTANCE)) 549 return -EINVAL; 550 551 err = __copy_from_user(eci, user_eci, 552 sizeof(struct drm_xe_engine_class_instance) * 553 len); 554 if (XE_IOCTL_DBG(xe, err)) 555 return -EFAULT; 556 557 if (XE_IOCTL_DBG(xe, eci[0].gt_id >= xe->info.gt_count)) 558 return -EINVAL; 559 560 if (eci[0].engine_class == DRM_XE_ENGINE_CLASS_VM_BIND) { 561 if (XE_IOCTL_DBG(xe, args->width != 1) || 562 XE_IOCTL_DBG(xe, args->num_placements != 1) || 563 XE_IOCTL_DBG(xe, eci[0].engine_instance != 0)) 564 return -EINVAL; 565 566 for_each_tile(tile, xe, id) { 567 struct xe_exec_queue *new; 568 u32 flags = EXEC_QUEUE_FLAG_VM; 569 570 if (id) 571 flags |= EXEC_QUEUE_FLAG_BIND_ENGINE_CHILD; 572 573 new = xe_exec_queue_create_bind(xe, tile, flags, 574 args->extensions); 575 if (IS_ERR(new)) { 576 err = PTR_ERR(new); 577 if (q) 578 goto put_exec_queue; 579 return err; 580 } 581 if (id == 0) 582 q = new; 583 else 584 list_add_tail(&new->multi_gt_list, 585 &q->multi_gt_link); 586 } 587 } else { 588 gt = xe_device_get_gt(xe, eci[0].gt_id); 589 logical_mask = calc_validate_logical_mask(xe, gt, eci, 590 args->width, 591 args->num_placements); 592 if (XE_IOCTL_DBG(xe, !logical_mask)) 593 return -EINVAL; 594 595 hwe = xe_hw_engine_lookup(xe, eci[0]); 596 if (XE_IOCTL_DBG(xe, !hwe)) 597 return -EINVAL; 598 599 vm = xe_vm_lookup(xef, args->vm_id); 600 if (XE_IOCTL_DBG(xe, !vm)) 601 return -ENOENT; 602 603 err = down_read_interruptible(&vm->lock); 604 if (err) { 605 xe_vm_put(vm); 606 return err; 607 } 608 609 if (XE_IOCTL_DBG(xe, xe_vm_is_closed_or_banned(vm))) { 610 up_read(&vm->lock); 611 xe_vm_put(vm); 612 return -ENOENT; 613 } 614 615 q = xe_exec_queue_create(xe, vm, logical_mask, 616 args->width, hwe, 0, 617 args->extensions); 618 up_read(&vm->lock); 619 xe_vm_put(vm); 620 if (IS_ERR(q)) 621 return PTR_ERR(q); 622 623 if (xe_vm_in_preempt_fence_mode(vm)) { 624 q->lr.context = dma_fence_context_alloc(1); 625 626 err = xe_vm_add_compute_exec_queue(vm, q); 627 if (XE_IOCTL_DBG(xe, err)) 628 goto put_exec_queue; 629 } 630 631 if (q->vm && q->hwe->hw_engine_group) { 632 err = xe_hw_engine_group_add_exec_queue(q->hwe->hw_engine_group, q); 633 if (err) 634 goto put_exec_queue; 635 } 636 } 637 638 mutex_lock(&xef->exec_queue.lock); 639 err = xa_alloc(&xef->exec_queue.xa, &id, q, xa_limit_32b, GFP_KERNEL); 640 mutex_unlock(&xef->exec_queue.lock); 641 if (err) 642 goto kill_exec_queue; 643 644 args->exec_queue_id = id; 645 q->xef = xe_file_get(xef); 646 647 return 0; 648 649 kill_exec_queue: 650 xe_exec_queue_kill(q); 651 put_exec_queue: 652 xe_exec_queue_put(q); 653 return err; 654 } 655 656 int xe_exec_queue_get_property_ioctl(struct drm_device *dev, void *data, 657 struct drm_file *file) 658 { 659 struct xe_device *xe = to_xe_device(dev); 660 struct xe_file *xef = to_xe_file(file); 661 struct drm_xe_exec_queue_get_property *args = data; 662 struct xe_exec_queue *q; 663 int ret; 664 665 if (XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) 666 return -EINVAL; 667 668 q = xe_exec_queue_lookup(xef, args->exec_queue_id); 669 if (XE_IOCTL_DBG(xe, !q)) 670 return -ENOENT; 671 672 switch (args->property) { 673 case DRM_XE_EXEC_QUEUE_GET_PROPERTY_BAN: 674 args->value = q->ops->reset_status(q); 675 ret = 0; 676 break; 677 default: 678 ret = -EINVAL; 679 } 680 681 xe_exec_queue_put(q); 682 683 return ret; 684 } 685 686 /** 687 * xe_exec_queue_is_lr() - Whether an exec_queue is long-running 688 * @q: The exec_queue 689 * 690 * Return: True if the exec_queue is long-running, false otherwise. 691 */ 692 bool xe_exec_queue_is_lr(struct xe_exec_queue *q) 693 { 694 return q->vm && xe_vm_in_lr_mode(q->vm) && 695 !(q->flags & EXEC_QUEUE_FLAG_VM); 696 } 697 698 static s32 xe_exec_queue_num_job_inflight(struct xe_exec_queue *q) 699 { 700 return q->lrc[0]->fence_ctx.next_seqno - xe_lrc_seqno(q->lrc[0]) - 1; 701 } 702 703 /** 704 * xe_exec_queue_ring_full() - Whether an exec_queue's ring is full 705 * @q: The exec_queue 706 * 707 * Return: True if the exec_queue's ring is full, false otherwise. 708 */ 709 bool xe_exec_queue_ring_full(struct xe_exec_queue *q) 710 { 711 struct xe_lrc *lrc = q->lrc[0]; 712 s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES; 713 714 return xe_exec_queue_num_job_inflight(q) >= max_job; 715 } 716 717 /** 718 * xe_exec_queue_is_idle() - Whether an exec_queue is idle. 719 * @q: The exec_queue 720 * 721 * FIXME: Need to determine what to use as the short-lived 722 * timeline lock for the exec_queues, so that the return value 723 * of this function becomes more than just an advisory 724 * snapshot in time. The timeline lock must protect the 725 * seqno from racing submissions on the same exec_queue. 726 * Typically vm->resv, but user-created timeline locks use the migrate vm 727 * and never grabs the migrate vm->resv so we have a race there. 728 * 729 * Return: True if the exec_queue is idle, false otherwise. 730 */ 731 bool xe_exec_queue_is_idle(struct xe_exec_queue *q) 732 { 733 if (xe_exec_queue_is_parallel(q)) { 734 int i; 735 736 for (i = 0; i < q->width; ++i) { 737 if (xe_lrc_seqno(q->lrc[i]) != 738 q->lrc[i]->fence_ctx.next_seqno - 1) 739 return false; 740 } 741 742 return true; 743 } 744 745 return xe_lrc_seqno(q->lrc[0]) == 746 q->lrc[0]->fence_ctx.next_seqno - 1; 747 } 748 749 /** 750 * xe_exec_queue_update_run_ticks() - Update run time in ticks for this exec queue 751 * from hw 752 * @q: The exec queue 753 * 754 * Update the timestamp saved by HW for this exec queue and save run ticks 755 * calculated by using the delta from last update. 756 */ 757 void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q) 758 { 759 struct xe_file *xef; 760 struct xe_lrc *lrc; 761 u32 old_ts, new_ts; 762 763 /* 764 * Jobs that are run during driver load may use an exec_queue, but are 765 * not associated with a user xe file, so avoid accumulating busyness 766 * for kernel specific work. 767 */ 768 if (!q->vm || !q->vm->xef) 769 return; 770 771 xef = q->vm->xef; 772 773 /* 774 * Only sample the first LRC. For parallel submission, all of them are 775 * scheduled together and we compensate that below by multiplying by 776 * width - this may introduce errors if that premise is not true and 777 * they don't exit 100% aligned. On the other hand, looping through 778 * the LRCs and reading them in different time could also introduce 779 * errors. 780 */ 781 lrc = q->lrc[0]; 782 new_ts = xe_lrc_update_timestamp(lrc, &old_ts); 783 xef->run_ticks[q->class] += (new_ts - old_ts) * q->width; 784 } 785 786 /** 787 * xe_exec_queue_kill - permanently stop all execution from an exec queue 788 * @q: The exec queue 789 * 790 * This function permanently stops all activity on an exec queue. If the queue 791 * is actively executing on the HW, it will be kicked off the engine; any 792 * pending jobs are discarded and all future submissions are rejected. 793 * This function is safe to call multiple times. 794 */ 795 void xe_exec_queue_kill(struct xe_exec_queue *q) 796 { 797 struct xe_exec_queue *eq = q, *next; 798 799 list_for_each_entry_safe(eq, next, &eq->multi_gt_list, 800 multi_gt_link) { 801 q->ops->kill(eq); 802 xe_vm_remove_compute_exec_queue(q->vm, eq); 803 } 804 805 q->ops->kill(q); 806 xe_vm_remove_compute_exec_queue(q->vm, q); 807 } 808 809 int xe_exec_queue_destroy_ioctl(struct drm_device *dev, void *data, 810 struct drm_file *file) 811 { 812 struct xe_device *xe = to_xe_device(dev); 813 struct xe_file *xef = to_xe_file(file); 814 struct drm_xe_exec_queue_destroy *args = data; 815 struct xe_exec_queue *q; 816 817 if (XE_IOCTL_DBG(xe, args->pad) || 818 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) 819 return -EINVAL; 820 821 mutex_lock(&xef->exec_queue.lock); 822 q = xa_erase(&xef->exec_queue.xa, args->exec_queue_id); 823 mutex_unlock(&xef->exec_queue.lock); 824 if (XE_IOCTL_DBG(xe, !q)) 825 return -ENOENT; 826 827 if (q->vm && q->hwe->hw_engine_group) 828 xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q); 829 830 xe_exec_queue_kill(q); 831 832 trace_xe_exec_queue_close(q); 833 xe_exec_queue_put(q); 834 835 return 0; 836 } 837 838 static void xe_exec_queue_last_fence_lockdep_assert(struct xe_exec_queue *q, 839 struct xe_vm *vm) 840 { 841 if (q->flags & EXEC_QUEUE_FLAG_VM) { 842 lockdep_assert_held(&vm->lock); 843 } else { 844 xe_vm_assert_held(vm); 845 lockdep_assert_held(&q->hwe->hw_engine_group->mode_sem); 846 } 847 } 848 849 /** 850 * xe_exec_queue_last_fence_put() - Drop ref to last fence 851 * @q: The exec queue 852 * @vm: The VM the engine does a bind or exec for 853 */ 854 void xe_exec_queue_last_fence_put(struct xe_exec_queue *q, struct xe_vm *vm) 855 { 856 xe_exec_queue_last_fence_lockdep_assert(q, vm); 857 858 xe_exec_queue_last_fence_put_unlocked(q); 859 } 860 861 /** 862 * xe_exec_queue_last_fence_put_unlocked() - Drop ref to last fence unlocked 863 * @q: The exec queue 864 * 865 * Only safe to be called from xe_exec_queue_destroy(). 866 */ 867 void xe_exec_queue_last_fence_put_unlocked(struct xe_exec_queue *q) 868 { 869 if (q->last_fence) { 870 dma_fence_put(q->last_fence); 871 q->last_fence = NULL; 872 } 873 } 874 875 /** 876 * xe_exec_queue_last_fence_get() - Get last fence 877 * @q: The exec queue 878 * @vm: The VM the engine does a bind or exec for 879 * 880 * Get last fence, takes a ref 881 * 882 * Returns: last fence if not signaled, dma fence stub if signaled 883 */ 884 struct dma_fence *xe_exec_queue_last_fence_get(struct xe_exec_queue *q, 885 struct xe_vm *vm) 886 { 887 struct dma_fence *fence; 888 889 xe_exec_queue_last_fence_lockdep_assert(q, vm); 890 891 if (q->last_fence && 892 test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags)) 893 xe_exec_queue_last_fence_put(q, vm); 894 895 fence = q->last_fence ? q->last_fence : dma_fence_get_stub(); 896 dma_fence_get(fence); 897 return fence; 898 } 899 900 /** 901 * xe_exec_queue_last_fence_get_for_resume() - Get last fence 902 * @q: The exec queue 903 * @vm: The VM the engine does a bind or exec for 904 * 905 * Get last fence, takes a ref. Only safe to be called in the context of 906 * resuming the hw engine group's long-running exec queue, when the group 907 * semaphore is held. 908 * 909 * Returns: last fence if not signaled, dma fence stub if signaled 910 */ 911 struct dma_fence *xe_exec_queue_last_fence_get_for_resume(struct xe_exec_queue *q, 912 struct xe_vm *vm) 913 { 914 struct dma_fence *fence; 915 916 lockdep_assert_held_write(&q->hwe->hw_engine_group->mode_sem); 917 918 if (q->last_fence && 919 test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags)) 920 xe_exec_queue_last_fence_put_unlocked(q); 921 922 fence = q->last_fence ? q->last_fence : dma_fence_get_stub(); 923 dma_fence_get(fence); 924 return fence; 925 } 926 927 /** 928 * xe_exec_queue_last_fence_set() - Set last fence 929 * @q: The exec queue 930 * @vm: The VM the engine does a bind or exec for 931 * @fence: The fence 932 * 933 * Set the last fence for the engine. Increases reference count for fence, when 934 * closing engine xe_exec_queue_last_fence_put should be called. 935 */ 936 void xe_exec_queue_last_fence_set(struct xe_exec_queue *q, struct xe_vm *vm, 937 struct dma_fence *fence) 938 { 939 xe_exec_queue_last_fence_lockdep_assert(q, vm); 940 941 xe_exec_queue_last_fence_put(q, vm); 942 q->last_fence = dma_fence_get(fence); 943 } 944 945 /** 946 * xe_exec_queue_last_fence_test_dep - Test last fence dependency of queue 947 * @q: The exec queue 948 * @vm: The VM the engine does a bind or exec for 949 * 950 * Returns: 951 * -ETIME if there exists an unsignalled last fence dependency, zero otherwise. 952 */ 953 int xe_exec_queue_last_fence_test_dep(struct xe_exec_queue *q, struct xe_vm *vm) 954 { 955 struct dma_fence *fence; 956 int err = 0; 957 958 fence = xe_exec_queue_last_fence_get(q, vm); 959 if (fence) { 960 err = test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) ? 961 0 : -ETIME; 962 dma_fence_put(fence); 963 } 964 965 return err; 966 } 967