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 <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 return ERR_PTR(-EINVAL); 228 229 q = xe_exec_queue_create(xe, migrate_vm, 230 BIT(hwe->logical_instance), 1, hwe, 231 flags, extensions); 232 } else { 233 q = xe_exec_queue_create_class(xe, gt, migrate_vm, 234 XE_ENGINE_CLASS_COPY, flags, 235 extensions); 236 } 237 xe_vm_put(migrate_vm); 238 239 return q; 240 } 241 242 void xe_exec_queue_destroy(struct kref *ref) 243 { 244 struct xe_exec_queue *q = container_of(ref, struct xe_exec_queue, refcount); 245 struct xe_exec_queue *eq, *next; 246 247 xe_exec_queue_last_fence_put_unlocked(q); 248 if (!(q->flags & EXEC_QUEUE_FLAG_BIND_ENGINE_CHILD)) { 249 list_for_each_entry_safe(eq, next, &q->multi_gt_list, 250 multi_gt_link) 251 xe_exec_queue_put(eq); 252 } 253 254 q->ops->fini(q); 255 } 256 257 void xe_exec_queue_fini(struct xe_exec_queue *q) 258 { 259 int i; 260 261 for (i = 0; i < q->width; ++i) 262 xe_lrc_put(q->lrc[i]); 263 __xe_exec_queue_free(q); 264 } 265 266 void xe_exec_queue_assign_name(struct xe_exec_queue *q, u32 instance) 267 { 268 switch (q->class) { 269 case XE_ENGINE_CLASS_RENDER: 270 snprintf(q->name, sizeof(q->name), "rcs%d", instance); 271 break; 272 case XE_ENGINE_CLASS_VIDEO_DECODE: 273 snprintf(q->name, sizeof(q->name), "vcs%d", instance); 274 break; 275 case XE_ENGINE_CLASS_VIDEO_ENHANCE: 276 snprintf(q->name, sizeof(q->name), "vecs%d", instance); 277 break; 278 case XE_ENGINE_CLASS_COPY: 279 snprintf(q->name, sizeof(q->name), "bcs%d", instance); 280 break; 281 case XE_ENGINE_CLASS_COMPUTE: 282 snprintf(q->name, sizeof(q->name), "ccs%d", instance); 283 break; 284 case XE_ENGINE_CLASS_OTHER: 285 snprintf(q->name, sizeof(q->name), "gsccs%d", instance); 286 break; 287 default: 288 XE_WARN_ON(q->class); 289 } 290 } 291 292 struct xe_exec_queue *xe_exec_queue_lookup(struct xe_file *xef, u32 id) 293 { 294 struct xe_exec_queue *q; 295 296 mutex_lock(&xef->exec_queue.lock); 297 q = xa_load(&xef->exec_queue.xa, id); 298 if (q) 299 xe_exec_queue_get(q); 300 mutex_unlock(&xef->exec_queue.lock); 301 302 return q; 303 } 304 305 enum xe_exec_queue_priority 306 xe_exec_queue_device_get_max_priority(struct xe_device *xe) 307 { 308 return capable(CAP_SYS_NICE) ? XE_EXEC_QUEUE_PRIORITY_HIGH : 309 XE_EXEC_QUEUE_PRIORITY_NORMAL; 310 } 311 312 static int exec_queue_set_priority(struct xe_device *xe, struct xe_exec_queue *q, 313 u64 value) 314 { 315 if (XE_IOCTL_DBG(xe, value > XE_EXEC_QUEUE_PRIORITY_HIGH)) 316 return -EINVAL; 317 318 if (XE_IOCTL_DBG(xe, value > xe_exec_queue_device_get_max_priority(xe))) 319 return -EPERM; 320 321 q->sched_props.priority = value; 322 return 0; 323 } 324 325 static bool xe_exec_queue_enforce_schedule_limit(void) 326 { 327 #if IS_ENABLED(CONFIG_DRM_XE_ENABLE_SCHEDTIMEOUT_LIMIT) 328 return true; 329 #else 330 return !capable(CAP_SYS_NICE); 331 #endif 332 } 333 334 static void 335 xe_exec_queue_get_prop_minmax(struct xe_hw_engine_class_intf *eclass, 336 enum xe_exec_queue_sched_prop prop, 337 u32 *min, u32 *max) 338 { 339 switch (prop) { 340 case XE_EXEC_QUEUE_JOB_TIMEOUT: 341 *min = eclass->sched_props.job_timeout_min; 342 *max = eclass->sched_props.job_timeout_max; 343 break; 344 case XE_EXEC_QUEUE_TIMESLICE: 345 *min = eclass->sched_props.timeslice_min; 346 *max = eclass->sched_props.timeslice_max; 347 break; 348 case XE_EXEC_QUEUE_PREEMPT_TIMEOUT: 349 *min = eclass->sched_props.preempt_timeout_min; 350 *max = eclass->sched_props.preempt_timeout_max; 351 break; 352 default: 353 break; 354 } 355 #if IS_ENABLED(CONFIG_DRM_XE_ENABLE_SCHEDTIMEOUT_LIMIT) 356 if (capable(CAP_SYS_NICE)) { 357 switch (prop) { 358 case XE_EXEC_QUEUE_JOB_TIMEOUT: 359 *min = XE_HW_ENGINE_JOB_TIMEOUT_MIN; 360 *max = XE_HW_ENGINE_JOB_TIMEOUT_MAX; 361 break; 362 case XE_EXEC_QUEUE_TIMESLICE: 363 *min = XE_HW_ENGINE_TIMESLICE_MIN; 364 *max = XE_HW_ENGINE_TIMESLICE_MAX; 365 break; 366 case XE_EXEC_QUEUE_PREEMPT_TIMEOUT: 367 *min = XE_HW_ENGINE_PREEMPT_TIMEOUT_MIN; 368 *max = XE_HW_ENGINE_PREEMPT_TIMEOUT_MAX; 369 break; 370 default: 371 break; 372 } 373 } 374 #endif 375 } 376 377 static int exec_queue_set_timeslice(struct xe_device *xe, struct xe_exec_queue *q, 378 u64 value) 379 { 380 u32 min = 0, max = 0; 381 382 xe_exec_queue_get_prop_minmax(q->hwe->eclass, 383 XE_EXEC_QUEUE_TIMESLICE, &min, &max); 384 385 if (xe_exec_queue_enforce_schedule_limit() && 386 !xe_hw_engine_timeout_in_range(value, min, max)) 387 return -EINVAL; 388 389 q->sched_props.timeslice_us = value; 390 return 0; 391 } 392 393 typedef int (*xe_exec_queue_set_property_fn)(struct xe_device *xe, 394 struct xe_exec_queue *q, 395 u64 value); 396 397 static const xe_exec_queue_set_property_fn exec_queue_set_property_funcs[] = { 398 [DRM_XE_EXEC_QUEUE_SET_PROPERTY_PRIORITY] = exec_queue_set_priority, 399 [DRM_XE_EXEC_QUEUE_SET_PROPERTY_TIMESLICE] = exec_queue_set_timeslice, 400 }; 401 402 static int exec_queue_user_ext_set_property(struct xe_device *xe, 403 struct xe_exec_queue *q, 404 u64 extension) 405 { 406 u64 __user *address = u64_to_user_ptr(extension); 407 struct drm_xe_ext_set_property ext; 408 int err; 409 u32 idx; 410 411 err = __copy_from_user(&ext, address, sizeof(ext)); 412 if (XE_IOCTL_DBG(xe, err)) 413 return -EFAULT; 414 415 if (XE_IOCTL_DBG(xe, ext.property >= 416 ARRAY_SIZE(exec_queue_set_property_funcs)) || 417 XE_IOCTL_DBG(xe, ext.pad) || 418 XE_IOCTL_DBG(xe, ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_PRIORITY && 419 ext.property != DRM_XE_EXEC_QUEUE_SET_PROPERTY_TIMESLICE)) 420 return -EINVAL; 421 422 idx = array_index_nospec(ext.property, ARRAY_SIZE(exec_queue_set_property_funcs)); 423 if (!exec_queue_set_property_funcs[idx]) 424 return -EINVAL; 425 426 return exec_queue_set_property_funcs[idx](xe, q, ext.value); 427 } 428 429 typedef int (*xe_exec_queue_user_extension_fn)(struct xe_device *xe, 430 struct xe_exec_queue *q, 431 u64 extension); 432 433 static const xe_exec_queue_user_extension_fn exec_queue_user_extension_funcs[] = { 434 [DRM_XE_EXEC_QUEUE_EXTENSION_SET_PROPERTY] = exec_queue_user_ext_set_property, 435 }; 436 437 #define MAX_USER_EXTENSIONS 16 438 static int exec_queue_user_extensions(struct xe_device *xe, struct xe_exec_queue *q, 439 u64 extensions, int ext_number) 440 { 441 u64 __user *address = u64_to_user_ptr(extensions); 442 struct drm_xe_user_extension ext; 443 int err; 444 u32 idx; 445 446 if (XE_IOCTL_DBG(xe, ext_number >= MAX_USER_EXTENSIONS)) 447 return -E2BIG; 448 449 err = __copy_from_user(&ext, address, sizeof(ext)); 450 if (XE_IOCTL_DBG(xe, err)) 451 return -EFAULT; 452 453 if (XE_IOCTL_DBG(xe, ext.pad) || 454 XE_IOCTL_DBG(xe, ext.name >= 455 ARRAY_SIZE(exec_queue_user_extension_funcs))) 456 return -EINVAL; 457 458 idx = array_index_nospec(ext.name, 459 ARRAY_SIZE(exec_queue_user_extension_funcs)); 460 err = exec_queue_user_extension_funcs[idx](xe, q, extensions); 461 if (XE_IOCTL_DBG(xe, err)) 462 return err; 463 464 if (ext.next_extension) 465 return exec_queue_user_extensions(xe, q, ext.next_extension, 466 ++ext_number); 467 468 return 0; 469 } 470 471 static u32 calc_validate_logical_mask(struct xe_device *xe, struct xe_gt *gt, 472 struct drm_xe_engine_class_instance *eci, 473 u16 width, u16 num_placements) 474 { 475 int len = width * num_placements; 476 int i, j, n; 477 u16 class; 478 u16 gt_id; 479 u32 return_mask = 0, prev_mask; 480 481 if (XE_IOCTL_DBG(xe, !xe_device_uc_enabled(xe) && 482 len > 1)) 483 return 0; 484 485 for (i = 0; i < width; ++i) { 486 u32 current_mask = 0; 487 488 for (j = 0; j < num_placements; ++j) { 489 struct xe_hw_engine *hwe; 490 491 n = j * width + i; 492 493 hwe = xe_hw_engine_lookup(xe, eci[n]); 494 if (XE_IOCTL_DBG(xe, !hwe)) 495 return 0; 496 497 if (XE_IOCTL_DBG(xe, xe_hw_engine_is_reserved(hwe))) 498 return 0; 499 500 if (XE_IOCTL_DBG(xe, n && eci[n].gt_id != gt_id) || 501 XE_IOCTL_DBG(xe, n && eci[n].engine_class != class)) 502 return 0; 503 504 class = eci[n].engine_class; 505 gt_id = eci[n].gt_id; 506 507 if (width == 1 || !i) 508 return_mask |= BIT(eci[n].engine_instance); 509 current_mask |= BIT(eci[n].engine_instance); 510 } 511 512 /* Parallel submissions must be logically contiguous */ 513 if (i && XE_IOCTL_DBG(xe, current_mask != prev_mask << 1)) 514 return 0; 515 516 prev_mask = current_mask; 517 } 518 519 return return_mask; 520 } 521 522 int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data, 523 struct drm_file *file) 524 { 525 struct xe_device *xe = to_xe_device(dev); 526 struct xe_file *xef = to_xe_file(file); 527 struct drm_xe_exec_queue_create *args = data; 528 struct drm_xe_engine_class_instance eci[XE_HW_ENGINE_MAX_INSTANCE]; 529 struct drm_xe_engine_class_instance __user *user_eci = 530 u64_to_user_ptr(args->instances); 531 struct xe_hw_engine *hwe; 532 struct xe_vm *vm; 533 struct xe_gt *gt; 534 struct xe_tile *tile; 535 struct xe_exec_queue *q = NULL; 536 u32 logical_mask; 537 u32 id; 538 u32 len; 539 int err; 540 541 if (XE_IOCTL_DBG(xe, args->flags) || 542 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) 543 return -EINVAL; 544 545 len = args->width * args->num_placements; 546 if (XE_IOCTL_DBG(xe, !len || len > XE_HW_ENGINE_MAX_INSTANCE)) 547 return -EINVAL; 548 549 err = __copy_from_user(eci, user_eci, 550 sizeof(struct drm_xe_engine_class_instance) * 551 len); 552 if (XE_IOCTL_DBG(xe, err)) 553 return -EFAULT; 554 555 if (XE_IOCTL_DBG(xe, eci[0].gt_id >= xe->info.gt_count)) 556 return -EINVAL; 557 558 if (eci[0].engine_class == DRM_XE_ENGINE_CLASS_VM_BIND) { 559 if (XE_IOCTL_DBG(xe, args->width != 1) || 560 XE_IOCTL_DBG(xe, args->num_placements != 1) || 561 XE_IOCTL_DBG(xe, eci[0].engine_instance != 0)) 562 return -EINVAL; 563 564 for_each_tile(tile, xe, id) { 565 struct xe_exec_queue *new; 566 u32 flags = EXEC_QUEUE_FLAG_VM; 567 568 if (id) 569 flags |= EXEC_QUEUE_FLAG_BIND_ENGINE_CHILD; 570 571 new = xe_exec_queue_create_bind(xe, tile, flags, 572 args->extensions); 573 if (IS_ERR(new)) { 574 err = PTR_ERR(new); 575 if (q) 576 goto put_exec_queue; 577 return err; 578 } 579 if (id == 0) 580 q = new; 581 else 582 list_add_tail(&new->multi_gt_list, 583 &q->multi_gt_link); 584 } 585 } else { 586 gt = xe_device_get_gt(xe, eci[0].gt_id); 587 logical_mask = calc_validate_logical_mask(xe, gt, eci, 588 args->width, 589 args->num_placements); 590 if (XE_IOCTL_DBG(xe, !logical_mask)) 591 return -EINVAL; 592 593 hwe = xe_hw_engine_lookup(xe, eci[0]); 594 if (XE_IOCTL_DBG(xe, !hwe)) 595 return -EINVAL; 596 597 vm = xe_vm_lookup(xef, args->vm_id); 598 if (XE_IOCTL_DBG(xe, !vm)) 599 return -ENOENT; 600 601 err = down_read_interruptible(&vm->lock); 602 if (err) { 603 xe_vm_put(vm); 604 return err; 605 } 606 607 if (XE_IOCTL_DBG(xe, xe_vm_is_closed_or_banned(vm))) { 608 up_read(&vm->lock); 609 xe_vm_put(vm); 610 return -ENOENT; 611 } 612 613 q = xe_exec_queue_create(xe, vm, logical_mask, 614 args->width, hwe, 0, 615 args->extensions); 616 up_read(&vm->lock); 617 xe_vm_put(vm); 618 if (IS_ERR(q)) 619 return PTR_ERR(q); 620 621 if (xe_vm_in_preempt_fence_mode(vm)) { 622 q->lr.context = dma_fence_context_alloc(1); 623 624 err = xe_vm_add_compute_exec_queue(vm, q); 625 if (XE_IOCTL_DBG(xe, err)) 626 goto put_exec_queue; 627 } 628 629 if (q->vm && q->hwe->hw_engine_group) { 630 err = xe_hw_engine_group_add_exec_queue(q->hwe->hw_engine_group, q); 631 if (err) 632 goto put_exec_queue; 633 } 634 } 635 636 mutex_lock(&xef->exec_queue.lock); 637 err = xa_alloc(&xef->exec_queue.xa, &id, q, xa_limit_32b, GFP_KERNEL); 638 mutex_unlock(&xef->exec_queue.lock); 639 if (err) 640 goto kill_exec_queue; 641 642 args->exec_queue_id = id; 643 q->xef = xe_file_get(xef); 644 645 return 0; 646 647 kill_exec_queue: 648 xe_exec_queue_kill(q); 649 put_exec_queue: 650 xe_exec_queue_put(q); 651 return err; 652 } 653 654 int xe_exec_queue_get_property_ioctl(struct drm_device *dev, void *data, 655 struct drm_file *file) 656 { 657 struct xe_device *xe = to_xe_device(dev); 658 struct xe_file *xef = to_xe_file(file); 659 struct drm_xe_exec_queue_get_property *args = data; 660 struct xe_exec_queue *q; 661 int ret; 662 663 if (XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) 664 return -EINVAL; 665 666 q = xe_exec_queue_lookup(xef, args->exec_queue_id); 667 if (XE_IOCTL_DBG(xe, !q)) 668 return -ENOENT; 669 670 switch (args->property) { 671 case DRM_XE_EXEC_QUEUE_GET_PROPERTY_BAN: 672 args->value = q->ops->reset_status(q); 673 ret = 0; 674 break; 675 default: 676 ret = -EINVAL; 677 } 678 679 xe_exec_queue_put(q); 680 681 return ret; 682 } 683 684 /** 685 * xe_exec_queue_is_lr() - Whether an exec_queue is long-running 686 * @q: The exec_queue 687 * 688 * Return: True if the exec_queue is long-running, false otherwise. 689 */ 690 bool xe_exec_queue_is_lr(struct xe_exec_queue *q) 691 { 692 return q->vm && xe_vm_in_lr_mode(q->vm) && 693 !(q->flags & EXEC_QUEUE_FLAG_VM); 694 } 695 696 static s32 xe_exec_queue_num_job_inflight(struct xe_exec_queue *q) 697 { 698 return q->lrc[0]->fence_ctx.next_seqno - xe_lrc_seqno(q->lrc[0]) - 1; 699 } 700 701 /** 702 * xe_exec_queue_ring_full() - Whether an exec_queue's ring is full 703 * @q: The exec_queue 704 * 705 * Return: True if the exec_queue's ring is full, false otherwise. 706 */ 707 bool xe_exec_queue_ring_full(struct xe_exec_queue *q) 708 { 709 struct xe_lrc *lrc = q->lrc[0]; 710 s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES; 711 712 return xe_exec_queue_num_job_inflight(q) >= max_job; 713 } 714 715 /** 716 * xe_exec_queue_is_idle() - Whether an exec_queue is idle. 717 * @q: The exec_queue 718 * 719 * FIXME: Need to determine what to use as the short-lived 720 * timeline lock for the exec_queues, so that the return value 721 * of this function becomes more than just an advisory 722 * snapshot in time. The timeline lock must protect the 723 * seqno from racing submissions on the same exec_queue. 724 * Typically vm->resv, but user-created timeline locks use the migrate vm 725 * and never grabs the migrate vm->resv so we have a race there. 726 * 727 * Return: True if the exec_queue is idle, false otherwise. 728 */ 729 bool xe_exec_queue_is_idle(struct xe_exec_queue *q) 730 { 731 if (xe_exec_queue_is_parallel(q)) { 732 int i; 733 734 for (i = 0; i < q->width; ++i) { 735 if (xe_lrc_seqno(q->lrc[i]) != 736 q->lrc[i]->fence_ctx.next_seqno - 1) 737 return false; 738 } 739 740 return true; 741 } 742 743 return xe_lrc_seqno(q->lrc[0]) == 744 q->lrc[0]->fence_ctx.next_seqno - 1; 745 } 746 747 /** 748 * xe_exec_queue_update_run_ticks() - Update run time in ticks for this exec queue 749 * from hw 750 * @q: The exec queue 751 * 752 * Update the timestamp saved by HW for this exec queue and save run ticks 753 * calculated by using the delta from last update. 754 */ 755 void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q) 756 { 757 struct xe_file *xef; 758 struct xe_lrc *lrc; 759 u32 old_ts, new_ts; 760 761 /* 762 * Jobs that are run during driver load may use an exec_queue, but are 763 * not associated with a user xe file, so avoid accumulating busyness 764 * for kernel specific work. 765 */ 766 if (!q->vm || !q->vm->xef) 767 return; 768 769 xef = q->vm->xef; 770 771 /* 772 * Only sample the first LRC. For parallel submission, all of them are 773 * scheduled together and we compensate that below by multiplying by 774 * width - this may introduce errors if that premise is not true and 775 * they don't exit 100% aligned. On the other hand, looping through 776 * the LRCs and reading them in different time could also introduce 777 * errors. 778 */ 779 lrc = q->lrc[0]; 780 new_ts = xe_lrc_update_timestamp(lrc, &old_ts); 781 xef->run_ticks[q->class] += (new_ts - old_ts) * q->width; 782 } 783 784 /** 785 * xe_exec_queue_kill - permanently stop all execution from an exec queue 786 * @q: The exec queue 787 * 788 * This function permanently stops all activity on an exec queue. If the queue 789 * is actively executing on the HW, it will be kicked off the engine; any 790 * pending jobs are discarded and all future submissions are rejected. 791 * This function is safe to call multiple times. 792 */ 793 void xe_exec_queue_kill(struct xe_exec_queue *q) 794 { 795 struct xe_exec_queue *eq = q, *next; 796 797 list_for_each_entry_safe(eq, next, &eq->multi_gt_list, 798 multi_gt_link) { 799 q->ops->kill(eq); 800 xe_vm_remove_compute_exec_queue(q->vm, eq); 801 } 802 803 q->ops->kill(q); 804 xe_vm_remove_compute_exec_queue(q->vm, q); 805 } 806 807 int xe_exec_queue_destroy_ioctl(struct drm_device *dev, void *data, 808 struct drm_file *file) 809 { 810 struct xe_device *xe = to_xe_device(dev); 811 struct xe_file *xef = to_xe_file(file); 812 struct drm_xe_exec_queue_destroy *args = data; 813 struct xe_exec_queue *q; 814 815 if (XE_IOCTL_DBG(xe, args->pad) || 816 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) 817 return -EINVAL; 818 819 mutex_lock(&xef->exec_queue.lock); 820 q = xa_erase(&xef->exec_queue.xa, args->exec_queue_id); 821 mutex_unlock(&xef->exec_queue.lock); 822 if (XE_IOCTL_DBG(xe, !q)) 823 return -ENOENT; 824 825 if (q->vm && q->hwe->hw_engine_group) 826 xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q); 827 828 xe_exec_queue_kill(q); 829 830 trace_xe_exec_queue_close(q); 831 xe_exec_queue_put(q); 832 833 return 0; 834 } 835 836 static void xe_exec_queue_last_fence_lockdep_assert(struct xe_exec_queue *q, 837 struct xe_vm *vm) 838 { 839 if (q->flags & EXEC_QUEUE_FLAG_VM) { 840 lockdep_assert_held(&vm->lock); 841 } else { 842 xe_vm_assert_held(vm); 843 lockdep_assert_held(&q->hwe->hw_engine_group->mode_sem); 844 } 845 } 846 847 /** 848 * xe_exec_queue_last_fence_put() - Drop ref to last fence 849 * @q: The exec queue 850 * @vm: The VM the engine does a bind or exec for 851 */ 852 void xe_exec_queue_last_fence_put(struct xe_exec_queue *q, struct xe_vm *vm) 853 { 854 xe_exec_queue_last_fence_lockdep_assert(q, vm); 855 856 xe_exec_queue_last_fence_put_unlocked(q); 857 } 858 859 /** 860 * xe_exec_queue_last_fence_put_unlocked() - Drop ref to last fence unlocked 861 * @q: The exec queue 862 * 863 * Only safe to be called from xe_exec_queue_destroy(). 864 */ 865 void xe_exec_queue_last_fence_put_unlocked(struct xe_exec_queue *q) 866 { 867 if (q->last_fence) { 868 dma_fence_put(q->last_fence); 869 q->last_fence = NULL; 870 } 871 } 872 873 /** 874 * xe_exec_queue_last_fence_get() - Get last fence 875 * @q: The exec queue 876 * @vm: The VM the engine does a bind or exec for 877 * 878 * Get last fence, takes a ref 879 * 880 * Returns: last fence if not signaled, dma fence stub if signaled 881 */ 882 struct dma_fence *xe_exec_queue_last_fence_get(struct xe_exec_queue *q, 883 struct xe_vm *vm) 884 { 885 struct dma_fence *fence; 886 887 xe_exec_queue_last_fence_lockdep_assert(q, vm); 888 889 if (q->last_fence && 890 test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags)) 891 xe_exec_queue_last_fence_put(q, vm); 892 893 fence = q->last_fence ? q->last_fence : dma_fence_get_stub(); 894 dma_fence_get(fence); 895 return fence; 896 } 897 898 /** 899 * xe_exec_queue_last_fence_get_for_resume() - Get last fence 900 * @q: The exec queue 901 * @vm: The VM the engine does a bind or exec for 902 * 903 * Get last fence, takes a ref. Only safe to be called in the context of 904 * resuming the hw engine group's long-running exec queue, when the group 905 * semaphore is held. 906 * 907 * Returns: last fence if not signaled, dma fence stub if signaled 908 */ 909 struct dma_fence *xe_exec_queue_last_fence_get_for_resume(struct xe_exec_queue *q, 910 struct xe_vm *vm) 911 { 912 struct dma_fence *fence; 913 914 lockdep_assert_held_write(&q->hwe->hw_engine_group->mode_sem); 915 916 if (q->last_fence && 917 test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &q->last_fence->flags)) 918 xe_exec_queue_last_fence_put_unlocked(q); 919 920 fence = q->last_fence ? q->last_fence : dma_fence_get_stub(); 921 dma_fence_get(fence); 922 return fence; 923 } 924 925 /** 926 * xe_exec_queue_last_fence_set() - Set last fence 927 * @q: The exec queue 928 * @vm: The VM the engine does a bind or exec for 929 * @fence: The fence 930 * 931 * Set the last fence for the engine. Increases reference count for fence, when 932 * closing engine xe_exec_queue_last_fence_put should be called. 933 */ 934 void xe_exec_queue_last_fence_set(struct xe_exec_queue *q, struct xe_vm *vm, 935 struct dma_fence *fence) 936 { 937 xe_exec_queue_last_fence_lockdep_assert(q, vm); 938 939 xe_exec_queue_last_fence_put(q, vm); 940 q->last_fence = dma_fence_get(fence); 941 } 942 943 /** 944 * xe_exec_queue_last_fence_test_dep - Test last fence dependency of queue 945 * @q: The exec queue 946 * @vm: The VM the engine does a bind or exec for 947 * 948 * Returns: 949 * -ETIME if there exists an unsignalled last fence dependency, zero otherwise. 950 */ 951 int xe_exec_queue_last_fence_test_dep(struct xe_exec_queue *q, struct xe_vm *vm) 952 { 953 struct dma_fence *fence; 954 int err = 0; 955 956 fence = xe_exec_queue_last_fence_get(q, vm); 957 if (fence) { 958 err = test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) ? 959 0 : -ETIME; 960 dma_fence_put(fence); 961 } 962 963 return err; 964 } 965