1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2023-2024 Intel Corporation 4 */ 5 6 #include <linux/anon_inodes.h> 7 #include <linux/delay.h> 8 #include <linux/nospec.h> 9 #include <linux/poll.h> 10 11 #include <drm/drm_drv.h> 12 #include <drm/drm_managed.h> 13 #include <uapi/drm/xe_drm.h> 14 15 #include "abi/guc_actions_slpc_abi.h" 16 #include "instructions/xe_mi_commands.h" 17 #include "regs/xe_engine_regs.h" 18 #include "regs/xe_gt_regs.h" 19 #include "regs/xe_lrc_layout.h" 20 #include "regs/xe_oa_regs.h" 21 #include "xe_assert.h" 22 #include "xe_bb.h" 23 #include "xe_bo.h" 24 #include "xe_device.h" 25 #include "xe_exec_queue.h" 26 #include "xe_force_wake.h" 27 #include "xe_gt.h" 28 #include "xe_gt_mcr.h" 29 #include "xe_gt_printk.h" 30 #include "xe_guc_pc.h" 31 #include "xe_lrc.h" 32 #include "xe_macros.h" 33 #include "xe_mmio.h" 34 #include "xe_oa.h" 35 #include "xe_observation.h" 36 #include "xe_pm.h" 37 #include "xe_sched_job.h" 38 #include "xe_sriov.h" 39 #include "xe_sync.h" 40 41 #define DEFAULT_POLL_FREQUENCY_HZ 200 42 #define DEFAULT_POLL_PERIOD_NS (NSEC_PER_SEC / DEFAULT_POLL_FREQUENCY_HZ) 43 #define XE_OA_UNIT_INVALID U32_MAX 44 45 enum xe_oa_submit_deps { 46 XE_OA_SUBMIT_NO_DEPS, 47 XE_OA_SUBMIT_ADD_DEPS, 48 }; 49 50 enum xe_oa_user_extn_from { 51 XE_OA_USER_EXTN_FROM_OPEN, 52 XE_OA_USER_EXTN_FROM_CONFIG, 53 }; 54 55 struct xe_oa_reg { 56 struct xe_reg addr; 57 u32 value; 58 }; 59 60 struct xe_oa_config { 61 struct xe_oa *oa; 62 63 char uuid[UUID_STRING_LEN + 1]; 64 int id; 65 66 const struct xe_oa_reg *regs; 67 u32 regs_len; 68 69 struct attribute_group sysfs_metric; 70 struct attribute *attrs[2]; 71 struct kobj_attribute sysfs_metric_id; 72 73 struct kref ref; 74 struct rcu_head rcu; 75 }; 76 77 struct xe_oa_open_param { 78 struct xe_file *xef; 79 u32 oa_unit_id; 80 bool sample; 81 u32 metric_set; 82 enum xe_oa_format_name oa_format; 83 int period_exponent; 84 bool disabled; 85 int exec_queue_id; 86 int engine_instance; 87 struct xe_exec_queue *exec_q; 88 struct xe_hw_engine *hwe; 89 bool no_preempt; 90 struct drm_xe_sync __user *syncs_user; 91 int num_syncs; 92 struct xe_sync_entry *syncs; 93 }; 94 95 struct xe_oa_config_bo { 96 struct llist_node node; 97 98 struct xe_oa_config *oa_config; 99 struct xe_bb *bb; 100 }; 101 102 struct xe_oa_fence { 103 /* @base: dma fence base */ 104 struct dma_fence base; 105 /* @lock: lock for the fence */ 106 spinlock_t lock; 107 /* @work: work to signal @base */ 108 struct delayed_work work; 109 /* @cb: callback to schedule @work */ 110 struct dma_fence_cb cb; 111 }; 112 113 #define DRM_FMT(x) DRM_XE_OA_FMT_TYPE_##x 114 115 static const struct xe_oa_format oa_formats[] = { 116 [XE_OA_FORMAT_C4_B8] = { 7, 64, DRM_FMT(OAG) }, 117 [XE_OA_FORMAT_A12] = { 0, 64, DRM_FMT(OAG) }, 118 [XE_OA_FORMAT_A12_B8_C8] = { 2, 128, DRM_FMT(OAG) }, 119 [XE_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256, DRM_FMT(OAG) }, 120 [XE_OAR_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256, DRM_FMT(OAR) }, 121 [XE_OA_FORMAT_A24u40_A14u32_B8_C8] = { 5, 256, DRM_FMT(OAG) }, 122 [XE_OAC_FORMAT_A24u64_B8_C8] = { 1, 320, DRM_FMT(OAC), HDR_64_BIT }, 123 [XE_OAC_FORMAT_A22u32_R2u32_B8_C8] = { 2, 192, DRM_FMT(OAC), HDR_64_BIT }, 124 [XE_OAM_FORMAT_MPEC8u64_B8_C8] = { 1, 192, DRM_FMT(OAM_MPEC), HDR_64_BIT }, 125 [XE_OAM_FORMAT_MPEC8u32_B8_C8] = { 2, 128, DRM_FMT(OAM_MPEC), HDR_64_BIT }, 126 [XE_OA_FORMAT_PEC64u64] = { 1, 576, DRM_FMT(PEC), HDR_64_BIT, 1, 0 }, 127 [XE_OA_FORMAT_PEC64u64_B8_C8] = { 1, 640, DRM_FMT(PEC), HDR_64_BIT, 1, 1 }, 128 [XE_OA_FORMAT_PEC64u32] = { 1, 320, DRM_FMT(PEC), HDR_64_BIT }, 129 [XE_OA_FORMAT_PEC32u64_G1] = { 5, 320, DRM_FMT(PEC), HDR_64_BIT, 1, 0 }, 130 [XE_OA_FORMAT_PEC32u32_G1] = { 5, 192, DRM_FMT(PEC), HDR_64_BIT }, 131 [XE_OA_FORMAT_PEC32u64_G2] = { 6, 320, DRM_FMT(PEC), HDR_64_BIT, 1, 0 }, 132 [XE_OA_FORMAT_PEC32u32_G2] = { 6, 192, DRM_FMT(PEC), HDR_64_BIT }, 133 [XE_OA_FORMAT_PEC36u64_G1_32_G2_4] = { 3, 320, DRM_FMT(PEC), HDR_64_BIT, 1, 0 }, 134 [XE_OA_FORMAT_PEC36u64_G1_4_G2_32] = { 4, 320, DRM_FMT(PEC), HDR_64_BIT, 1, 0 }, 135 }; 136 137 static u32 xe_oa_circ_diff(struct xe_oa_stream *stream, u32 tail, u32 head) 138 { 139 return tail >= head ? tail - head : 140 tail + stream->oa_buffer.circ_size - head; 141 } 142 143 static u32 xe_oa_circ_incr(struct xe_oa_stream *stream, u32 ptr, u32 n) 144 { 145 return ptr + n >= stream->oa_buffer.circ_size ? 146 ptr + n - stream->oa_buffer.circ_size : ptr + n; 147 } 148 149 static void xe_oa_config_release(struct kref *ref) 150 { 151 struct xe_oa_config *oa_config = 152 container_of(ref, typeof(*oa_config), ref); 153 154 kfree(oa_config->regs); 155 156 kfree_rcu(oa_config, rcu); 157 } 158 159 static void xe_oa_config_put(struct xe_oa_config *oa_config) 160 { 161 if (!oa_config) 162 return; 163 164 kref_put(&oa_config->ref, xe_oa_config_release); 165 } 166 167 static struct xe_oa_config *xe_oa_config_get(struct xe_oa_config *oa_config) 168 { 169 return kref_get_unless_zero(&oa_config->ref) ? oa_config : NULL; 170 } 171 172 static struct xe_oa_config *xe_oa_get_oa_config(struct xe_oa *oa, int metrics_set) 173 { 174 struct xe_oa_config *oa_config; 175 176 rcu_read_lock(); 177 oa_config = idr_find(&oa->metrics_idr, metrics_set); 178 if (oa_config) 179 oa_config = xe_oa_config_get(oa_config); 180 rcu_read_unlock(); 181 182 return oa_config; 183 } 184 185 static void free_oa_config_bo(struct xe_oa_config_bo *oa_bo, struct dma_fence *last_fence) 186 { 187 xe_oa_config_put(oa_bo->oa_config); 188 xe_bb_free(oa_bo->bb, last_fence); 189 kfree(oa_bo); 190 } 191 192 static const struct xe_oa_regs *__oa_regs(struct xe_oa_stream *stream) 193 { 194 return &stream->hwe->oa_unit->regs; 195 } 196 197 static u32 xe_oa_hw_tail_read(struct xe_oa_stream *stream) 198 { 199 return xe_mmio_read32(&stream->gt->mmio, __oa_regs(stream)->oa_tail_ptr) & 200 OAG_OATAILPTR_MASK; 201 } 202 203 #define oa_report_header_64bit(__s) \ 204 ((__s)->oa_buffer.format->header == HDR_64_BIT) 205 206 static u64 oa_report_id(struct xe_oa_stream *stream, void *report) 207 { 208 return oa_report_header_64bit(stream) ? *(u64 *)report : *(u32 *)report; 209 } 210 211 static void oa_report_id_clear(struct xe_oa_stream *stream, u32 *report) 212 { 213 if (oa_report_header_64bit(stream)) 214 *(u64 *)report = 0; 215 else 216 *report = 0; 217 } 218 219 static u64 oa_timestamp(struct xe_oa_stream *stream, void *report) 220 { 221 return oa_report_header_64bit(stream) ? 222 *((u64 *)report + 1) : 223 *((u32 *)report + 1); 224 } 225 226 static void oa_timestamp_clear(struct xe_oa_stream *stream, u32 *report) 227 { 228 if (oa_report_header_64bit(stream)) 229 *(u64 *)&report[2] = 0; 230 else 231 report[1] = 0; 232 } 233 234 static bool xe_oa_buffer_check_unlocked(struct xe_oa_stream *stream) 235 { 236 u32 gtt_offset = xe_bo_ggtt_addr(stream->oa_buffer.bo); 237 int report_size = stream->oa_buffer.format->size; 238 u32 tail, hw_tail; 239 unsigned long flags; 240 bool pollin; 241 u32 partial_report_size; 242 243 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags); 244 245 hw_tail = xe_oa_hw_tail_read(stream); 246 hw_tail -= gtt_offset; 247 248 /* 249 * The tail pointer increases in 64 byte (cacheline size), not in report_size 250 * increments. Also report size may not be a power of 2. Compute potential 251 * partially landed report in OA buffer. 252 */ 253 partial_report_size = xe_oa_circ_diff(stream, hw_tail, stream->oa_buffer.tail); 254 partial_report_size %= report_size; 255 256 /* Subtract partial amount off the tail */ 257 hw_tail = xe_oa_circ_diff(stream, hw_tail, partial_report_size); 258 259 tail = hw_tail; 260 261 /* 262 * Walk the stream backward until we find a report with report id and timestamp 263 * not 0. We can't tell whether a report has fully landed in memory before the 264 * report id and timestamp of the following report have landed. 265 * 266 * This is assuming that the writes of the OA unit land in memory in the order 267 * they were written. If not : (╯°□°)╯︵ ┻━┻ 268 */ 269 while (xe_oa_circ_diff(stream, tail, stream->oa_buffer.tail) >= report_size) { 270 void *report = stream->oa_buffer.vaddr + tail; 271 272 if (oa_report_id(stream, report) || oa_timestamp(stream, report)) 273 break; 274 275 tail = xe_oa_circ_diff(stream, tail, report_size); 276 } 277 278 if (xe_oa_circ_diff(stream, hw_tail, tail) > report_size) 279 drm_dbg(&stream->oa->xe->drm, 280 "unlanded report(s) head=0x%x tail=0x%x hw_tail=0x%x\n", 281 stream->oa_buffer.head, tail, hw_tail); 282 283 stream->oa_buffer.tail = tail; 284 285 pollin = xe_oa_circ_diff(stream, stream->oa_buffer.tail, 286 stream->oa_buffer.head) >= report_size; 287 288 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags); 289 290 return pollin; 291 } 292 293 static enum hrtimer_restart xe_oa_poll_check_timer_cb(struct hrtimer *hrtimer) 294 { 295 struct xe_oa_stream *stream = 296 container_of(hrtimer, typeof(*stream), poll_check_timer); 297 298 if (xe_oa_buffer_check_unlocked(stream)) { 299 stream->pollin = true; 300 wake_up(&stream->poll_wq); 301 } 302 303 hrtimer_forward_now(hrtimer, ns_to_ktime(stream->poll_period_ns)); 304 305 return HRTIMER_RESTART; 306 } 307 308 static int xe_oa_append_report(struct xe_oa_stream *stream, char __user *buf, 309 size_t count, size_t *offset, const u8 *report) 310 { 311 int report_size = stream->oa_buffer.format->size; 312 int report_size_partial; 313 u8 *oa_buf_end; 314 315 if ((count - *offset) < report_size) 316 return -ENOSPC; 317 318 buf += *offset; 319 320 oa_buf_end = stream->oa_buffer.vaddr + stream->oa_buffer.circ_size; 321 report_size_partial = oa_buf_end - report; 322 323 if (report_size_partial < report_size) { 324 if (copy_to_user(buf, report, report_size_partial)) 325 return -EFAULT; 326 buf += report_size_partial; 327 328 if (copy_to_user(buf, stream->oa_buffer.vaddr, 329 report_size - report_size_partial)) 330 return -EFAULT; 331 } else if (copy_to_user(buf, report, report_size)) { 332 return -EFAULT; 333 } 334 335 *offset += report_size; 336 337 return 0; 338 } 339 340 static int xe_oa_append_reports(struct xe_oa_stream *stream, char __user *buf, 341 size_t count, size_t *offset) 342 { 343 int report_size = stream->oa_buffer.format->size; 344 u8 *oa_buf_base = stream->oa_buffer.vaddr; 345 u32 gtt_offset = xe_bo_ggtt_addr(stream->oa_buffer.bo); 346 size_t start_offset = *offset; 347 unsigned long flags; 348 u32 head, tail; 349 int ret = 0; 350 351 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags); 352 head = stream->oa_buffer.head; 353 tail = stream->oa_buffer.tail; 354 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags); 355 356 xe_assert(stream->oa->xe, 357 head < stream->oa_buffer.circ_size && tail < stream->oa_buffer.circ_size); 358 359 for (; xe_oa_circ_diff(stream, tail, head); 360 head = xe_oa_circ_incr(stream, head, report_size)) { 361 u8 *report = oa_buf_base + head; 362 363 ret = xe_oa_append_report(stream, buf, count, offset, report); 364 if (ret) 365 break; 366 367 if (!(stream->oa_buffer.circ_size % report_size)) { 368 /* Clear out report id and timestamp to detect unlanded reports */ 369 oa_report_id_clear(stream, (void *)report); 370 oa_timestamp_clear(stream, (void *)report); 371 } else { 372 u8 *oa_buf_end = stream->oa_buffer.vaddr + stream->oa_buffer.circ_size; 373 u32 part = oa_buf_end - report; 374 375 /* Zero out the entire report */ 376 if (report_size <= part) { 377 memset(report, 0, report_size); 378 } else { 379 memset(report, 0, part); 380 memset(oa_buf_base, 0, report_size - part); 381 } 382 } 383 } 384 385 if (start_offset != *offset) { 386 struct xe_reg oaheadptr = __oa_regs(stream)->oa_head_ptr; 387 388 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags); 389 xe_mmio_write32(&stream->gt->mmio, oaheadptr, 390 (head + gtt_offset) & OAG_OAHEADPTR_MASK); 391 stream->oa_buffer.head = head; 392 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags); 393 } 394 395 return ret; 396 } 397 398 static void xe_oa_init_oa_buffer(struct xe_oa_stream *stream) 399 { 400 struct xe_mmio *mmio = &stream->gt->mmio; 401 u32 gtt_offset = xe_bo_ggtt_addr(stream->oa_buffer.bo); 402 u32 oa_buf = gtt_offset | OABUFFER_SIZE_16M | OAG_OABUFFER_MEMORY_SELECT; 403 unsigned long flags; 404 405 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags); 406 407 xe_mmio_write32(mmio, __oa_regs(stream)->oa_status, 0); 408 xe_mmio_write32(mmio, __oa_regs(stream)->oa_head_ptr, 409 gtt_offset & OAG_OAHEADPTR_MASK); 410 stream->oa_buffer.head = 0; 411 /* 412 * PRM says: "This MMIO must be set before the OATAILPTR register and after the 413 * OAHEADPTR register. This is to enable proper functionality of the overflow bit". 414 */ 415 xe_mmio_write32(mmio, __oa_regs(stream)->oa_buffer, oa_buf); 416 xe_mmio_write32(mmio, __oa_regs(stream)->oa_tail_ptr, 417 gtt_offset & OAG_OATAILPTR_MASK); 418 419 /* Mark that we need updated tail pointer to read from */ 420 stream->oa_buffer.tail = 0; 421 422 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags); 423 424 /* Zero out the OA buffer since we rely on zero report id and timestamp fields */ 425 memset(stream->oa_buffer.vaddr, 0, stream->oa_buffer.bo->size); 426 } 427 428 static u32 __format_to_oactrl(const struct xe_oa_format *format, int counter_sel_mask) 429 { 430 return ((format->counter_select << (ffs(counter_sel_mask) - 1)) & counter_sel_mask) | 431 REG_FIELD_PREP(OA_OACONTROL_REPORT_BC_MASK, format->bc_report) | 432 REG_FIELD_PREP(OA_OACONTROL_COUNTER_SIZE_MASK, format->counter_size); 433 } 434 435 static u32 __oa_ccs_select(struct xe_oa_stream *stream) 436 { 437 u32 val; 438 439 if (stream->hwe->class != XE_ENGINE_CLASS_COMPUTE) 440 return 0; 441 442 val = REG_FIELD_PREP(OAG_OACONTROL_OA_CCS_SELECT_MASK, stream->hwe->instance); 443 xe_assert(stream->oa->xe, 444 REG_FIELD_GET(OAG_OACONTROL_OA_CCS_SELECT_MASK, val) == stream->hwe->instance); 445 return val; 446 } 447 448 static void xe_oa_enable(struct xe_oa_stream *stream) 449 { 450 const struct xe_oa_format *format = stream->oa_buffer.format; 451 const struct xe_oa_regs *regs; 452 u32 val; 453 454 /* 455 * BSpec: 46822: Bit 0. Even if stream->sample is 0, for OAR to function, the OA 456 * buffer must be correctly initialized 457 */ 458 xe_oa_init_oa_buffer(stream); 459 460 regs = __oa_regs(stream); 461 val = __format_to_oactrl(format, regs->oa_ctrl_counter_select_mask) | 462 __oa_ccs_select(stream) | OAG_OACONTROL_OA_COUNTER_ENABLE; 463 464 if (GRAPHICS_VER(stream->oa->xe) >= 20 && 465 stream->hwe->oa_unit->type == DRM_XE_OA_UNIT_TYPE_OAG) 466 val |= OAG_OACONTROL_OA_PES_DISAG_EN; 467 468 xe_mmio_write32(&stream->gt->mmio, regs->oa_ctrl, val); 469 } 470 471 static void xe_oa_disable(struct xe_oa_stream *stream) 472 { 473 struct xe_mmio *mmio = &stream->gt->mmio; 474 475 xe_mmio_write32(mmio, __oa_regs(stream)->oa_ctrl, 0); 476 if (xe_mmio_wait32(mmio, __oa_regs(stream)->oa_ctrl, 477 OAG_OACONTROL_OA_COUNTER_ENABLE, 0, 50000, NULL, false)) 478 drm_err(&stream->oa->xe->drm, 479 "wait for OA to be disabled timed out\n"); 480 481 if (GRAPHICS_VERx100(stream->oa->xe) <= 1270 && GRAPHICS_VERx100(stream->oa->xe) != 1260) { 482 /* <= XE_METEORLAKE except XE_PVC */ 483 xe_mmio_write32(mmio, OA_TLB_INV_CR, 1); 484 if (xe_mmio_wait32(mmio, OA_TLB_INV_CR, 1, 0, 50000, NULL, false)) 485 drm_err(&stream->oa->xe->drm, 486 "wait for OA tlb invalidate timed out\n"); 487 } 488 } 489 490 static int xe_oa_wait_unlocked(struct xe_oa_stream *stream) 491 { 492 /* We might wait indefinitely if periodic sampling is not enabled */ 493 if (!stream->periodic) 494 return -EINVAL; 495 496 return wait_event_interruptible(stream->poll_wq, 497 xe_oa_buffer_check_unlocked(stream)); 498 } 499 500 #define OASTATUS_RELEVANT_BITS (OASTATUS_MMIO_TRG_Q_FULL | OASTATUS_COUNTER_OVERFLOW | \ 501 OASTATUS_BUFFER_OVERFLOW | OASTATUS_REPORT_LOST) 502 503 static int __xe_oa_read(struct xe_oa_stream *stream, char __user *buf, 504 size_t count, size_t *offset) 505 { 506 /* Only clear our bits to avoid side-effects */ 507 stream->oa_status = xe_mmio_rmw32(&stream->gt->mmio, __oa_regs(stream)->oa_status, 508 OASTATUS_RELEVANT_BITS, 0); 509 /* 510 * Signal to userspace that there is non-zero OA status to read via 511 * @DRM_XE_OBSERVATION_IOCTL_STATUS observation stream fd ioctl 512 */ 513 if (stream->oa_status & OASTATUS_RELEVANT_BITS) 514 return -EIO; 515 516 return xe_oa_append_reports(stream, buf, count, offset); 517 } 518 519 static ssize_t xe_oa_read(struct file *file, char __user *buf, 520 size_t count, loff_t *ppos) 521 { 522 struct xe_oa_stream *stream = file->private_data; 523 size_t offset = 0; 524 int ret; 525 526 /* Can't read from disabled streams */ 527 if (!stream->enabled || !stream->sample) 528 return -EINVAL; 529 530 if (!(file->f_flags & O_NONBLOCK)) { 531 do { 532 ret = xe_oa_wait_unlocked(stream); 533 if (ret) 534 return ret; 535 536 mutex_lock(&stream->stream_lock); 537 ret = __xe_oa_read(stream, buf, count, &offset); 538 mutex_unlock(&stream->stream_lock); 539 } while (!offset && !ret); 540 } else { 541 mutex_lock(&stream->stream_lock); 542 ret = __xe_oa_read(stream, buf, count, &offset); 543 mutex_unlock(&stream->stream_lock); 544 } 545 546 /* 547 * Typically we clear pollin here in order to wait for the new hrtimer callback 548 * before unblocking. The exception to this is if __xe_oa_read returns -ENOSPC, 549 * which means that more OA data is available than could fit in the user provided 550 * buffer. In this case we want the next poll() call to not block. 551 * 552 * Also in case of -EIO, we have already waited for data before returning 553 * -EIO, so need to wait again 554 */ 555 if (ret != -ENOSPC && ret != -EIO) 556 stream->pollin = false; 557 558 /* Possible values for ret are 0, -EFAULT, -ENOSPC, -EIO, -EINVAL, ... */ 559 return offset ?: (ret ?: -EAGAIN); 560 } 561 562 static __poll_t xe_oa_poll_locked(struct xe_oa_stream *stream, 563 struct file *file, poll_table *wait) 564 { 565 __poll_t events = 0; 566 567 poll_wait(file, &stream->poll_wq, wait); 568 569 /* 570 * We don't explicitly check whether there's something to read here since this 571 * path may be hot depending on what else userspace is polling, or on the timeout 572 * in use. We rely on hrtimer xe_oa_poll_check_timer_cb to notify us when there 573 * are samples to read 574 */ 575 if (stream->pollin) 576 events |= EPOLLIN; 577 578 return events; 579 } 580 581 static __poll_t xe_oa_poll(struct file *file, poll_table *wait) 582 { 583 struct xe_oa_stream *stream = file->private_data; 584 __poll_t ret; 585 586 mutex_lock(&stream->stream_lock); 587 ret = xe_oa_poll_locked(stream, file, wait); 588 mutex_unlock(&stream->stream_lock); 589 590 return ret; 591 } 592 593 static void xe_oa_lock_vma(struct xe_exec_queue *q) 594 { 595 if (q->vm) { 596 down_read(&q->vm->lock); 597 xe_vm_lock(q->vm, false); 598 } 599 } 600 601 static void xe_oa_unlock_vma(struct xe_exec_queue *q) 602 { 603 if (q->vm) { 604 xe_vm_unlock(q->vm); 605 up_read(&q->vm->lock); 606 } 607 } 608 609 static struct dma_fence *xe_oa_submit_bb(struct xe_oa_stream *stream, enum xe_oa_submit_deps deps, 610 struct xe_bb *bb) 611 { 612 struct xe_exec_queue *q = stream->exec_q ?: stream->k_exec_q; 613 struct xe_sched_job *job; 614 struct dma_fence *fence; 615 int err = 0; 616 617 xe_oa_lock_vma(q); 618 619 job = xe_bb_create_job(q, bb); 620 if (IS_ERR(job)) { 621 err = PTR_ERR(job); 622 goto exit; 623 } 624 job->ggtt = true; 625 626 if (deps == XE_OA_SUBMIT_ADD_DEPS) { 627 for (int i = 0; i < stream->num_syncs && !err; i++) 628 err = xe_sync_entry_add_deps(&stream->syncs[i], job); 629 if (err) { 630 drm_dbg(&stream->oa->xe->drm, "xe_sync_entry_add_deps err %d\n", err); 631 goto err_put_job; 632 } 633 } 634 635 xe_sched_job_arm(job); 636 fence = dma_fence_get(&job->drm.s_fence->finished); 637 xe_sched_job_push(job); 638 639 xe_oa_unlock_vma(q); 640 641 return fence; 642 err_put_job: 643 xe_sched_job_put(job); 644 exit: 645 xe_oa_unlock_vma(q); 646 return ERR_PTR(err); 647 } 648 649 static void write_cs_mi_lri(struct xe_bb *bb, const struct xe_oa_reg *reg_data, u32 n_regs) 650 { 651 u32 i; 652 653 #define MI_LOAD_REGISTER_IMM_MAX_REGS (126) 654 655 for (i = 0; i < n_regs; i++) { 656 if ((i % MI_LOAD_REGISTER_IMM_MAX_REGS) == 0) { 657 u32 n_lri = min_t(u32, n_regs - i, 658 MI_LOAD_REGISTER_IMM_MAX_REGS); 659 660 bb->cs[bb->len++] = MI_LOAD_REGISTER_IMM | MI_LRI_NUM_REGS(n_lri); 661 } 662 bb->cs[bb->len++] = reg_data[i].addr.addr; 663 bb->cs[bb->len++] = reg_data[i].value; 664 } 665 } 666 667 static int num_lri_dwords(int num_regs) 668 { 669 int count = 0; 670 671 if (num_regs > 0) { 672 count += DIV_ROUND_UP(num_regs, MI_LOAD_REGISTER_IMM_MAX_REGS); 673 count += num_regs * 2; 674 } 675 676 return count; 677 } 678 679 static void xe_oa_free_oa_buffer(struct xe_oa_stream *stream) 680 { 681 xe_bo_unpin_map_no_vm(stream->oa_buffer.bo); 682 } 683 684 static void xe_oa_free_configs(struct xe_oa_stream *stream) 685 { 686 struct xe_oa_config_bo *oa_bo, *tmp; 687 688 xe_oa_config_put(stream->oa_config); 689 llist_for_each_entry_safe(oa_bo, tmp, stream->oa_config_bos.first, node) 690 free_oa_config_bo(oa_bo, stream->last_fence); 691 dma_fence_put(stream->last_fence); 692 } 693 694 static int xe_oa_load_with_lri(struct xe_oa_stream *stream, struct xe_oa_reg *reg_lri, u32 count) 695 { 696 struct dma_fence *fence; 697 struct xe_bb *bb; 698 int err; 699 700 bb = xe_bb_new(stream->gt, 2 * count + 1, false); 701 if (IS_ERR(bb)) { 702 err = PTR_ERR(bb); 703 goto exit; 704 } 705 706 write_cs_mi_lri(bb, reg_lri, count); 707 708 fence = xe_oa_submit_bb(stream, XE_OA_SUBMIT_NO_DEPS, bb); 709 if (IS_ERR(fence)) { 710 err = PTR_ERR(fence); 711 goto free_bb; 712 } 713 xe_bb_free(bb, fence); 714 dma_fence_put(fence); 715 716 return 0; 717 free_bb: 718 xe_bb_free(bb, NULL); 719 exit: 720 return err; 721 } 722 723 static int xe_oa_configure_oar_context(struct xe_oa_stream *stream, bool enable) 724 { 725 const struct xe_oa_format *format = stream->oa_buffer.format; 726 u32 oacontrol = __format_to_oactrl(format, OAR_OACONTROL_COUNTER_SEL_MASK) | 727 (enable ? OAR_OACONTROL_COUNTER_ENABLE : 0); 728 729 struct xe_oa_reg reg_lri[] = { 730 { 731 OACTXCONTROL(stream->hwe->mmio_base), 732 enable ? OA_COUNTER_RESUME : 0, 733 }, 734 { 735 OAR_OACONTROL, 736 oacontrol, 737 }, 738 { 739 RING_CONTEXT_CONTROL(stream->hwe->mmio_base), 740 _MASKED_FIELD(CTX_CTRL_OAC_CONTEXT_ENABLE, 741 enable ? CTX_CTRL_OAC_CONTEXT_ENABLE : 0) 742 }, 743 }; 744 745 return xe_oa_load_with_lri(stream, reg_lri, ARRAY_SIZE(reg_lri)); 746 } 747 748 static int xe_oa_configure_oac_context(struct xe_oa_stream *stream, bool enable) 749 { 750 const struct xe_oa_format *format = stream->oa_buffer.format; 751 u32 oacontrol = __format_to_oactrl(format, OAR_OACONTROL_COUNTER_SEL_MASK) | 752 (enable ? OAR_OACONTROL_COUNTER_ENABLE : 0); 753 struct xe_oa_reg reg_lri[] = { 754 { 755 OACTXCONTROL(stream->hwe->mmio_base), 756 enable ? OA_COUNTER_RESUME : 0, 757 }, 758 { 759 OAC_OACONTROL, 760 oacontrol 761 }, 762 { 763 RING_CONTEXT_CONTROL(stream->hwe->mmio_base), 764 _MASKED_FIELD(CTX_CTRL_OAC_CONTEXT_ENABLE, 765 enable ? CTX_CTRL_OAC_CONTEXT_ENABLE : 0) | 766 _MASKED_FIELD(CTX_CTRL_RUN_ALONE, enable ? CTX_CTRL_RUN_ALONE : 0), 767 }, 768 }; 769 770 /* Set ccs select to enable programming of OAC_OACONTROL */ 771 xe_mmio_write32(&stream->gt->mmio, __oa_regs(stream)->oa_ctrl, 772 __oa_ccs_select(stream)); 773 774 return xe_oa_load_with_lri(stream, reg_lri, ARRAY_SIZE(reg_lri)); 775 } 776 777 static int xe_oa_configure_oa_context(struct xe_oa_stream *stream, bool enable) 778 { 779 switch (stream->hwe->class) { 780 case XE_ENGINE_CLASS_RENDER: 781 return xe_oa_configure_oar_context(stream, enable); 782 case XE_ENGINE_CLASS_COMPUTE: 783 return xe_oa_configure_oac_context(stream, enable); 784 default: 785 /* Video engines do not support MI_REPORT_PERF_COUNT */ 786 return 0; 787 } 788 } 789 790 #define HAS_OA_BPC_REPORTING(xe) (GRAPHICS_VERx100(xe) >= 1255) 791 792 static u32 oag_configure_mmio_trigger(const struct xe_oa_stream *stream, bool enable) 793 { 794 return _MASKED_FIELD(OAG_OA_DEBUG_DISABLE_MMIO_TRG, 795 enable && stream && stream->sample ? 796 0 : OAG_OA_DEBUG_DISABLE_MMIO_TRG); 797 } 798 799 static void xe_oa_disable_metric_set(struct xe_oa_stream *stream) 800 { 801 struct xe_mmio *mmio = &stream->gt->mmio; 802 u32 sqcnt1; 803 804 /* 805 * Wa_1508761755:xehpsdv, dg2 806 * Enable thread stall DOP gating and EU DOP gating. 807 */ 808 if (stream->oa->xe->info.platform == XE_DG2) { 809 xe_gt_mcr_multicast_write(stream->gt, ROW_CHICKEN, 810 _MASKED_BIT_DISABLE(STALL_DOP_GATING_DISABLE)); 811 xe_gt_mcr_multicast_write(stream->gt, ROW_CHICKEN2, 812 _MASKED_BIT_DISABLE(DISABLE_DOP_GATING)); 813 } 814 815 xe_mmio_write32(mmio, __oa_regs(stream)->oa_debug, 816 oag_configure_mmio_trigger(stream, false)); 817 818 /* disable the context save/restore or OAR counters */ 819 if (stream->exec_q) 820 xe_oa_configure_oa_context(stream, false); 821 822 /* Make sure we disable noa to save power. */ 823 xe_mmio_rmw32(mmio, RPM_CONFIG1, GT_NOA_ENABLE, 0); 824 825 sqcnt1 = SQCNT1_PMON_ENABLE | 826 (HAS_OA_BPC_REPORTING(stream->oa->xe) ? SQCNT1_OABPC : 0); 827 828 /* Reset PMON Enable to save power. */ 829 xe_mmio_rmw32(mmio, XELPMP_SQCNT1, sqcnt1, 0); 830 } 831 832 static void xe_oa_stream_destroy(struct xe_oa_stream *stream) 833 { 834 struct xe_oa_unit *u = stream->hwe->oa_unit; 835 struct xe_gt *gt = stream->hwe->gt; 836 837 if (WARN_ON(stream != u->exclusive_stream)) 838 return; 839 840 WRITE_ONCE(u->exclusive_stream, NULL); 841 842 mutex_destroy(&stream->stream_lock); 843 844 xe_oa_disable_metric_set(stream); 845 xe_exec_queue_put(stream->k_exec_q); 846 847 xe_oa_free_oa_buffer(stream); 848 849 xe_force_wake_put(gt_to_fw(gt), XE_FORCEWAKE_ALL); 850 xe_pm_runtime_put(stream->oa->xe); 851 852 /* Wa_1509372804:pvc: Unset the override of GUCRC mode to enable rc6 */ 853 if (stream->override_gucrc) 854 xe_gt_WARN_ON(gt, xe_guc_pc_unset_gucrc_mode(>->uc.guc.pc)); 855 856 xe_oa_free_configs(stream); 857 xe_file_put(stream->xef); 858 } 859 860 static int xe_oa_alloc_oa_buffer(struct xe_oa_stream *stream) 861 { 862 struct xe_bo *bo; 863 864 BUILD_BUG_ON_NOT_POWER_OF_2(XE_OA_BUFFER_SIZE); 865 BUILD_BUG_ON(XE_OA_BUFFER_SIZE < SZ_128K || XE_OA_BUFFER_SIZE > SZ_16M); 866 867 bo = xe_bo_create_pin_map(stream->oa->xe, stream->gt->tile, NULL, 868 XE_OA_BUFFER_SIZE, ttm_bo_type_kernel, 869 XE_BO_FLAG_SYSTEM | XE_BO_FLAG_GGTT); 870 if (IS_ERR(bo)) 871 return PTR_ERR(bo); 872 873 stream->oa_buffer.bo = bo; 874 /* mmap implementation requires OA buffer to be in system memory */ 875 xe_assert(stream->oa->xe, bo->vmap.is_iomem == 0); 876 stream->oa_buffer.vaddr = bo->vmap.vaddr; 877 return 0; 878 } 879 880 static struct xe_oa_config_bo * 881 __xe_oa_alloc_config_buffer(struct xe_oa_stream *stream, struct xe_oa_config *oa_config) 882 { 883 struct xe_oa_config_bo *oa_bo; 884 size_t config_length; 885 struct xe_bb *bb; 886 887 oa_bo = kzalloc(sizeof(*oa_bo), GFP_KERNEL); 888 if (!oa_bo) 889 return ERR_PTR(-ENOMEM); 890 891 config_length = num_lri_dwords(oa_config->regs_len); 892 config_length = ALIGN(sizeof(u32) * config_length, XE_PAGE_SIZE) / sizeof(u32); 893 894 bb = xe_bb_new(stream->gt, config_length, false); 895 if (IS_ERR(bb)) 896 goto err_free; 897 898 write_cs_mi_lri(bb, oa_config->regs, oa_config->regs_len); 899 900 oa_bo->bb = bb; 901 oa_bo->oa_config = xe_oa_config_get(oa_config); 902 llist_add(&oa_bo->node, &stream->oa_config_bos); 903 904 return oa_bo; 905 err_free: 906 kfree(oa_bo); 907 return ERR_CAST(bb); 908 } 909 910 static struct xe_oa_config_bo * 911 xe_oa_alloc_config_buffer(struct xe_oa_stream *stream, struct xe_oa_config *oa_config) 912 { 913 struct xe_oa_config_bo *oa_bo; 914 915 /* Look for the buffer in the already allocated BOs attached to the stream */ 916 llist_for_each_entry(oa_bo, stream->oa_config_bos.first, node) { 917 if (oa_bo->oa_config == oa_config && 918 memcmp(oa_bo->oa_config->uuid, oa_config->uuid, 919 sizeof(oa_config->uuid)) == 0) 920 goto out; 921 } 922 923 oa_bo = __xe_oa_alloc_config_buffer(stream, oa_config); 924 out: 925 return oa_bo; 926 } 927 928 static void xe_oa_update_last_fence(struct xe_oa_stream *stream, struct dma_fence *fence) 929 { 930 dma_fence_put(stream->last_fence); 931 stream->last_fence = dma_fence_get(fence); 932 } 933 934 static void xe_oa_fence_work_fn(struct work_struct *w) 935 { 936 struct xe_oa_fence *ofence = container_of(w, typeof(*ofence), work.work); 937 938 /* Signal fence to indicate new OA configuration is active */ 939 dma_fence_signal(&ofence->base); 940 dma_fence_put(&ofence->base); 941 } 942 943 static void xe_oa_config_cb(struct dma_fence *fence, struct dma_fence_cb *cb) 944 { 945 /* Additional empirical delay needed for NOA programming after registers are written */ 946 #define NOA_PROGRAM_ADDITIONAL_DELAY_US 500 947 948 struct xe_oa_fence *ofence = container_of(cb, typeof(*ofence), cb); 949 950 INIT_DELAYED_WORK(&ofence->work, xe_oa_fence_work_fn); 951 queue_delayed_work(system_unbound_wq, &ofence->work, 952 usecs_to_jiffies(NOA_PROGRAM_ADDITIONAL_DELAY_US)); 953 dma_fence_put(fence); 954 } 955 956 static const char *xe_oa_get_driver_name(struct dma_fence *fence) 957 { 958 return "xe_oa"; 959 } 960 961 static const char *xe_oa_get_timeline_name(struct dma_fence *fence) 962 { 963 return "unbound"; 964 } 965 966 static const struct dma_fence_ops xe_oa_fence_ops = { 967 .get_driver_name = xe_oa_get_driver_name, 968 .get_timeline_name = xe_oa_get_timeline_name, 969 }; 970 971 static int xe_oa_emit_oa_config(struct xe_oa_stream *stream, struct xe_oa_config *config) 972 { 973 #define NOA_PROGRAM_ADDITIONAL_DELAY_US 500 974 struct xe_oa_config_bo *oa_bo; 975 struct xe_oa_fence *ofence; 976 int i, err, num_signal = 0; 977 struct dma_fence *fence; 978 979 ofence = kzalloc(sizeof(*ofence), GFP_KERNEL); 980 if (!ofence) { 981 err = -ENOMEM; 982 goto exit; 983 } 984 985 oa_bo = xe_oa_alloc_config_buffer(stream, config); 986 if (IS_ERR(oa_bo)) { 987 err = PTR_ERR(oa_bo); 988 goto exit; 989 } 990 991 /* Emit OA configuration batch */ 992 fence = xe_oa_submit_bb(stream, XE_OA_SUBMIT_ADD_DEPS, oa_bo->bb); 993 if (IS_ERR(fence)) { 994 err = PTR_ERR(fence); 995 goto exit; 996 } 997 998 /* Point of no return: initialize and set fence to signal */ 999 spin_lock_init(&ofence->lock); 1000 dma_fence_init(&ofence->base, &xe_oa_fence_ops, &ofence->lock, 0, 0); 1001 1002 for (i = 0; i < stream->num_syncs; i++) { 1003 if (stream->syncs[i].flags & DRM_XE_SYNC_FLAG_SIGNAL) 1004 num_signal++; 1005 xe_sync_entry_signal(&stream->syncs[i], &ofence->base); 1006 } 1007 1008 /* Additional dma_fence_get in case we dma_fence_wait */ 1009 if (!num_signal) 1010 dma_fence_get(&ofence->base); 1011 1012 /* Update last fence too before adding callback */ 1013 xe_oa_update_last_fence(stream, fence); 1014 1015 /* Add job fence callback to schedule work to signal ofence->base */ 1016 err = dma_fence_add_callback(fence, &ofence->cb, xe_oa_config_cb); 1017 xe_gt_assert(stream->gt, !err || err == -ENOENT); 1018 if (err == -ENOENT) 1019 xe_oa_config_cb(fence, &ofence->cb); 1020 1021 /* If nothing needs to be signaled we wait synchronously */ 1022 if (!num_signal) { 1023 dma_fence_wait(&ofence->base, false); 1024 dma_fence_put(&ofence->base); 1025 } 1026 1027 /* Done with syncs */ 1028 for (i = 0; i < stream->num_syncs; i++) 1029 xe_sync_entry_cleanup(&stream->syncs[i]); 1030 kfree(stream->syncs); 1031 1032 return 0; 1033 exit: 1034 kfree(ofence); 1035 return err; 1036 } 1037 1038 static u32 oag_report_ctx_switches(const struct xe_oa_stream *stream) 1039 { 1040 /* If user didn't require OA reports, ask HW not to emit ctx switch reports */ 1041 return _MASKED_FIELD(OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS, 1042 stream->sample ? 1043 0 : OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS); 1044 } 1045 1046 static int xe_oa_enable_metric_set(struct xe_oa_stream *stream) 1047 { 1048 struct xe_mmio *mmio = &stream->gt->mmio; 1049 u32 oa_debug, sqcnt1; 1050 int ret; 1051 1052 /* 1053 * Wa_1508761755:xehpsdv, dg2 1054 * EU NOA signals behave incorrectly if EU clock gating is enabled. 1055 * Disable thread stall DOP gating and EU DOP gating. 1056 */ 1057 if (stream->oa->xe->info.platform == XE_DG2) { 1058 xe_gt_mcr_multicast_write(stream->gt, ROW_CHICKEN, 1059 _MASKED_BIT_ENABLE(STALL_DOP_GATING_DISABLE)); 1060 xe_gt_mcr_multicast_write(stream->gt, ROW_CHICKEN2, 1061 _MASKED_BIT_ENABLE(DISABLE_DOP_GATING)); 1062 } 1063 1064 /* Disable clk ratio reports */ 1065 oa_debug = OAG_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS | 1066 OAG_OA_DEBUG_INCLUDE_CLK_RATIO; 1067 1068 if (GRAPHICS_VER(stream->oa->xe) >= 20) 1069 oa_debug |= 1070 /* The three bits below are needed to get PEC counters running */ 1071 OAG_OA_DEBUG_START_TRIGGER_SCOPE_CONTROL | 1072 OAG_OA_DEBUG_DISABLE_START_TRG_2_COUNT_QUAL | 1073 OAG_OA_DEBUG_DISABLE_START_TRG_1_COUNT_QUAL; 1074 1075 xe_mmio_write32(mmio, __oa_regs(stream)->oa_debug, 1076 _MASKED_BIT_ENABLE(oa_debug) | 1077 oag_report_ctx_switches(stream) | 1078 oag_configure_mmio_trigger(stream, true)); 1079 1080 xe_mmio_write32(mmio, __oa_regs(stream)->oa_ctx_ctrl, stream->periodic ? 1081 (OAG_OAGLBCTXCTRL_COUNTER_RESUME | 1082 OAG_OAGLBCTXCTRL_TIMER_ENABLE | 1083 REG_FIELD_PREP(OAG_OAGLBCTXCTRL_TIMER_PERIOD_MASK, 1084 stream->period_exponent)) : 0); 1085 1086 /* 1087 * Initialize Super Queue Internal Cnt Register 1088 * Set PMON Enable in order to collect valid metrics 1089 * Enable bytes per clock reporting 1090 */ 1091 sqcnt1 = SQCNT1_PMON_ENABLE | 1092 (HAS_OA_BPC_REPORTING(stream->oa->xe) ? SQCNT1_OABPC : 0); 1093 1094 xe_mmio_rmw32(mmio, XELPMP_SQCNT1, 0, sqcnt1); 1095 1096 /* Configure OAR/OAC */ 1097 if (stream->exec_q) { 1098 ret = xe_oa_configure_oa_context(stream, true); 1099 if (ret) 1100 return ret; 1101 } 1102 1103 return xe_oa_emit_oa_config(stream, stream->oa_config); 1104 } 1105 1106 static int decode_oa_format(struct xe_oa *oa, u64 fmt, enum xe_oa_format_name *name) 1107 { 1108 u32 counter_size = FIELD_GET(DRM_XE_OA_FORMAT_MASK_COUNTER_SIZE, fmt); 1109 u32 counter_sel = FIELD_GET(DRM_XE_OA_FORMAT_MASK_COUNTER_SEL, fmt); 1110 u32 bc_report = FIELD_GET(DRM_XE_OA_FORMAT_MASK_BC_REPORT, fmt); 1111 u32 type = FIELD_GET(DRM_XE_OA_FORMAT_MASK_FMT_TYPE, fmt); 1112 int idx; 1113 1114 for_each_set_bit(idx, oa->format_mask, __XE_OA_FORMAT_MAX) { 1115 const struct xe_oa_format *f = &oa->oa_formats[idx]; 1116 1117 if (counter_size == f->counter_size && bc_report == f->bc_report && 1118 type == f->type && counter_sel == f->counter_select) { 1119 *name = idx; 1120 return 0; 1121 } 1122 } 1123 1124 return -EINVAL; 1125 } 1126 1127 static int xe_oa_set_prop_oa_unit_id(struct xe_oa *oa, u64 value, 1128 struct xe_oa_open_param *param) 1129 { 1130 if (value >= oa->oa_unit_ids) { 1131 drm_dbg(&oa->xe->drm, "OA unit ID out of range %lld\n", value); 1132 return -EINVAL; 1133 } 1134 param->oa_unit_id = value; 1135 return 0; 1136 } 1137 1138 static int xe_oa_set_prop_sample_oa(struct xe_oa *oa, u64 value, 1139 struct xe_oa_open_param *param) 1140 { 1141 param->sample = value; 1142 return 0; 1143 } 1144 1145 static int xe_oa_set_prop_metric_set(struct xe_oa *oa, u64 value, 1146 struct xe_oa_open_param *param) 1147 { 1148 param->metric_set = value; 1149 return 0; 1150 } 1151 1152 static int xe_oa_set_prop_oa_format(struct xe_oa *oa, u64 value, 1153 struct xe_oa_open_param *param) 1154 { 1155 int ret = decode_oa_format(oa, value, ¶m->oa_format); 1156 1157 if (ret) { 1158 drm_dbg(&oa->xe->drm, "Unsupported OA report format %#llx\n", value); 1159 return ret; 1160 } 1161 return 0; 1162 } 1163 1164 static int xe_oa_set_prop_oa_exponent(struct xe_oa *oa, u64 value, 1165 struct xe_oa_open_param *param) 1166 { 1167 #define OA_EXPONENT_MAX 31 1168 1169 if (value > OA_EXPONENT_MAX) { 1170 drm_dbg(&oa->xe->drm, "OA timer exponent too high (> %u)\n", OA_EXPONENT_MAX); 1171 return -EINVAL; 1172 } 1173 param->period_exponent = value; 1174 return 0; 1175 } 1176 1177 static int xe_oa_set_prop_disabled(struct xe_oa *oa, u64 value, 1178 struct xe_oa_open_param *param) 1179 { 1180 param->disabled = value; 1181 return 0; 1182 } 1183 1184 static int xe_oa_set_prop_exec_queue_id(struct xe_oa *oa, u64 value, 1185 struct xe_oa_open_param *param) 1186 { 1187 param->exec_queue_id = value; 1188 return 0; 1189 } 1190 1191 static int xe_oa_set_prop_engine_instance(struct xe_oa *oa, u64 value, 1192 struct xe_oa_open_param *param) 1193 { 1194 param->engine_instance = value; 1195 return 0; 1196 } 1197 1198 static int xe_oa_set_no_preempt(struct xe_oa *oa, u64 value, 1199 struct xe_oa_open_param *param) 1200 { 1201 param->no_preempt = value; 1202 return 0; 1203 } 1204 1205 static int xe_oa_set_prop_num_syncs(struct xe_oa *oa, u64 value, 1206 struct xe_oa_open_param *param) 1207 { 1208 param->num_syncs = value; 1209 return 0; 1210 } 1211 1212 static int xe_oa_set_prop_syncs_user(struct xe_oa *oa, u64 value, 1213 struct xe_oa_open_param *param) 1214 { 1215 param->syncs_user = u64_to_user_ptr(value); 1216 return 0; 1217 } 1218 1219 static int xe_oa_set_prop_ret_inval(struct xe_oa *oa, u64 value, 1220 struct xe_oa_open_param *param) 1221 { 1222 return -EINVAL; 1223 } 1224 1225 typedef int (*xe_oa_set_property_fn)(struct xe_oa *oa, u64 value, 1226 struct xe_oa_open_param *param); 1227 static const xe_oa_set_property_fn xe_oa_set_property_funcs_open[] = { 1228 [DRM_XE_OA_PROPERTY_OA_UNIT_ID] = xe_oa_set_prop_oa_unit_id, 1229 [DRM_XE_OA_PROPERTY_SAMPLE_OA] = xe_oa_set_prop_sample_oa, 1230 [DRM_XE_OA_PROPERTY_OA_METRIC_SET] = xe_oa_set_prop_metric_set, 1231 [DRM_XE_OA_PROPERTY_OA_FORMAT] = xe_oa_set_prop_oa_format, 1232 [DRM_XE_OA_PROPERTY_OA_PERIOD_EXPONENT] = xe_oa_set_prop_oa_exponent, 1233 [DRM_XE_OA_PROPERTY_OA_DISABLED] = xe_oa_set_prop_disabled, 1234 [DRM_XE_OA_PROPERTY_EXEC_QUEUE_ID] = xe_oa_set_prop_exec_queue_id, 1235 [DRM_XE_OA_PROPERTY_OA_ENGINE_INSTANCE] = xe_oa_set_prop_engine_instance, 1236 [DRM_XE_OA_PROPERTY_NO_PREEMPT] = xe_oa_set_no_preempt, 1237 [DRM_XE_OA_PROPERTY_NUM_SYNCS] = xe_oa_set_prop_num_syncs, 1238 [DRM_XE_OA_PROPERTY_SYNCS] = xe_oa_set_prop_syncs_user, 1239 }; 1240 1241 static const xe_oa_set_property_fn xe_oa_set_property_funcs_config[] = { 1242 [DRM_XE_OA_PROPERTY_OA_UNIT_ID] = xe_oa_set_prop_ret_inval, 1243 [DRM_XE_OA_PROPERTY_SAMPLE_OA] = xe_oa_set_prop_ret_inval, 1244 [DRM_XE_OA_PROPERTY_OA_METRIC_SET] = xe_oa_set_prop_metric_set, 1245 [DRM_XE_OA_PROPERTY_OA_FORMAT] = xe_oa_set_prop_ret_inval, 1246 [DRM_XE_OA_PROPERTY_OA_PERIOD_EXPONENT] = xe_oa_set_prop_ret_inval, 1247 [DRM_XE_OA_PROPERTY_OA_DISABLED] = xe_oa_set_prop_ret_inval, 1248 [DRM_XE_OA_PROPERTY_EXEC_QUEUE_ID] = xe_oa_set_prop_ret_inval, 1249 [DRM_XE_OA_PROPERTY_OA_ENGINE_INSTANCE] = xe_oa_set_prop_ret_inval, 1250 [DRM_XE_OA_PROPERTY_NO_PREEMPT] = xe_oa_set_prop_ret_inval, 1251 [DRM_XE_OA_PROPERTY_NUM_SYNCS] = xe_oa_set_prop_num_syncs, 1252 [DRM_XE_OA_PROPERTY_SYNCS] = xe_oa_set_prop_syncs_user, 1253 }; 1254 1255 static int xe_oa_user_ext_set_property(struct xe_oa *oa, enum xe_oa_user_extn_from from, 1256 u64 extension, struct xe_oa_open_param *param) 1257 { 1258 u64 __user *address = u64_to_user_ptr(extension); 1259 struct drm_xe_ext_set_property ext; 1260 int err; 1261 u32 idx; 1262 1263 err = __copy_from_user(&ext, address, sizeof(ext)); 1264 if (XE_IOCTL_DBG(oa->xe, err)) 1265 return -EFAULT; 1266 1267 BUILD_BUG_ON(ARRAY_SIZE(xe_oa_set_property_funcs_open) != 1268 ARRAY_SIZE(xe_oa_set_property_funcs_config)); 1269 1270 if (XE_IOCTL_DBG(oa->xe, ext.property >= ARRAY_SIZE(xe_oa_set_property_funcs_open)) || 1271 XE_IOCTL_DBG(oa->xe, ext.pad)) 1272 return -EINVAL; 1273 1274 idx = array_index_nospec(ext.property, ARRAY_SIZE(xe_oa_set_property_funcs_open)); 1275 1276 if (from == XE_OA_USER_EXTN_FROM_CONFIG) 1277 return xe_oa_set_property_funcs_config[idx](oa, ext.value, param); 1278 else 1279 return xe_oa_set_property_funcs_open[idx](oa, ext.value, param); 1280 } 1281 1282 typedef int (*xe_oa_user_extension_fn)(struct xe_oa *oa, enum xe_oa_user_extn_from from, 1283 u64 extension, struct xe_oa_open_param *param); 1284 static const xe_oa_user_extension_fn xe_oa_user_extension_funcs[] = { 1285 [DRM_XE_OA_EXTENSION_SET_PROPERTY] = xe_oa_user_ext_set_property, 1286 }; 1287 1288 #define MAX_USER_EXTENSIONS 16 1289 static int xe_oa_user_extensions(struct xe_oa *oa, enum xe_oa_user_extn_from from, u64 extension, 1290 int ext_number, struct xe_oa_open_param *param) 1291 { 1292 u64 __user *address = u64_to_user_ptr(extension); 1293 struct drm_xe_user_extension ext; 1294 int err; 1295 u32 idx; 1296 1297 if (XE_IOCTL_DBG(oa->xe, ext_number >= MAX_USER_EXTENSIONS)) 1298 return -E2BIG; 1299 1300 err = __copy_from_user(&ext, address, sizeof(ext)); 1301 if (XE_IOCTL_DBG(oa->xe, err)) 1302 return -EFAULT; 1303 1304 if (XE_IOCTL_DBG(oa->xe, ext.pad) || 1305 XE_IOCTL_DBG(oa->xe, ext.name >= ARRAY_SIZE(xe_oa_user_extension_funcs))) 1306 return -EINVAL; 1307 1308 idx = array_index_nospec(ext.name, ARRAY_SIZE(xe_oa_user_extension_funcs)); 1309 err = xe_oa_user_extension_funcs[idx](oa, from, extension, param); 1310 if (XE_IOCTL_DBG(oa->xe, err)) 1311 return err; 1312 1313 if (ext.next_extension) 1314 return xe_oa_user_extensions(oa, from, ext.next_extension, ++ext_number, param); 1315 1316 return 0; 1317 } 1318 1319 static int xe_oa_parse_syncs(struct xe_oa *oa, struct xe_oa_open_param *param) 1320 { 1321 int ret, num_syncs, num_ufence = 0; 1322 1323 if (param->num_syncs && !param->syncs_user) { 1324 drm_dbg(&oa->xe->drm, "num_syncs specified without sync array\n"); 1325 ret = -EINVAL; 1326 goto exit; 1327 } 1328 1329 if (param->num_syncs) { 1330 param->syncs = kcalloc(param->num_syncs, sizeof(*param->syncs), GFP_KERNEL); 1331 if (!param->syncs) { 1332 ret = -ENOMEM; 1333 goto exit; 1334 } 1335 } 1336 1337 for (num_syncs = 0; num_syncs < param->num_syncs; num_syncs++) { 1338 ret = xe_sync_entry_parse(oa->xe, param->xef, ¶m->syncs[num_syncs], 1339 ¶m->syncs_user[num_syncs], 0); 1340 if (ret) 1341 goto err_syncs; 1342 1343 if (xe_sync_is_ufence(¶m->syncs[num_syncs])) 1344 num_ufence++; 1345 } 1346 1347 if (XE_IOCTL_DBG(oa->xe, num_ufence > 1)) { 1348 ret = -EINVAL; 1349 goto err_syncs; 1350 } 1351 1352 return 0; 1353 1354 err_syncs: 1355 while (num_syncs--) 1356 xe_sync_entry_cleanup(¶m->syncs[num_syncs]); 1357 kfree(param->syncs); 1358 exit: 1359 return ret; 1360 } 1361 1362 static void xe_oa_stream_enable(struct xe_oa_stream *stream) 1363 { 1364 stream->pollin = false; 1365 1366 xe_oa_enable(stream); 1367 1368 if (stream->sample) 1369 hrtimer_start(&stream->poll_check_timer, 1370 ns_to_ktime(stream->poll_period_ns), 1371 HRTIMER_MODE_REL_PINNED); 1372 } 1373 1374 static void xe_oa_stream_disable(struct xe_oa_stream *stream) 1375 { 1376 xe_oa_disable(stream); 1377 1378 if (stream->sample) 1379 hrtimer_cancel(&stream->poll_check_timer); 1380 } 1381 1382 static int xe_oa_enable_preempt_timeslice(struct xe_oa_stream *stream) 1383 { 1384 struct xe_exec_queue *q = stream->exec_q; 1385 int ret1, ret2; 1386 1387 /* Best effort recovery: try to revert both to original, irrespective of error */ 1388 ret1 = q->ops->set_timeslice(q, stream->hwe->eclass->sched_props.timeslice_us); 1389 ret2 = q->ops->set_preempt_timeout(q, stream->hwe->eclass->sched_props.preempt_timeout_us); 1390 if (ret1 || ret2) 1391 goto err; 1392 return 0; 1393 err: 1394 drm_dbg(&stream->oa->xe->drm, "%s failed ret1 %d ret2 %d\n", __func__, ret1, ret2); 1395 return ret1 ?: ret2; 1396 } 1397 1398 static int xe_oa_disable_preempt_timeslice(struct xe_oa_stream *stream) 1399 { 1400 struct xe_exec_queue *q = stream->exec_q; 1401 int ret; 1402 1403 /* Setting values to 0 will disable timeslice and preempt_timeout */ 1404 ret = q->ops->set_timeslice(q, 0); 1405 if (ret) 1406 goto err; 1407 1408 ret = q->ops->set_preempt_timeout(q, 0); 1409 if (ret) 1410 goto err; 1411 1412 return 0; 1413 err: 1414 xe_oa_enable_preempt_timeslice(stream); 1415 drm_dbg(&stream->oa->xe->drm, "%s failed %d\n", __func__, ret); 1416 return ret; 1417 } 1418 1419 static int xe_oa_enable_locked(struct xe_oa_stream *stream) 1420 { 1421 if (stream->enabled) 1422 return 0; 1423 1424 if (stream->no_preempt) { 1425 int ret = xe_oa_disable_preempt_timeslice(stream); 1426 1427 if (ret) 1428 return ret; 1429 } 1430 1431 xe_oa_stream_enable(stream); 1432 1433 stream->enabled = true; 1434 return 0; 1435 } 1436 1437 static int xe_oa_disable_locked(struct xe_oa_stream *stream) 1438 { 1439 int ret = 0; 1440 1441 if (!stream->enabled) 1442 return 0; 1443 1444 xe_oa_stream_disable(stream); 1445 1446 if (stream->no_preempt) 1447 ret = xe_oa_enable_preempt_timeslice(stream); 1448 1449 stream->enabled = false; 1450 return ret; 1451 } 1452 1453 static long xe_oa_config_locked(struct xe_oa_stream *stream, u64 arg) 1454 { 1455 struct xe_oa_open_param param = {}; 1456 long ret = stream->oa_config->id; 1457 struct xe_oa_config *config; 1458 int err; 1459 1460 err = xe_oa_user_extensions(stream->oa, XE_OA_USER_EXTN_FROM_CONFIG, arg, 0, ¶m); 1461 if (err) 1462 return err; 1463 1464 config = xe_oa_get_oa_config(stream->oa, param.metric_set); 1465 if (!config) 1466 return -ENODEV; 1467 1468 param.xef = stream->xef; 1469 err = xe_oa_parse_syncs(stream->oa, ¶m); 1470 if (err) 1471 goto err_config_put; 1472 1473 stream->num_syncs = param.num_syncs; 1474 stream->syncs = param.syncs; 1475 1476 err = xe_oa_emit_oa_config(stream, config); 1477 if (!err) { 1478 config = xchg(&stream->oa_config, config); 1479 drm_dbg(&stream->oa->xe->drm, "changed to oa config uuid=%s\n", 1480 stream->oa_config->uuid); 1481 } 1482 1483 err_config_put: 1484 xe_oa_config_put(config); 1485 1486 return err ?: ret; 1487 } 1488 1489 static long xe_oa_status_locked(struct xe_oa_stream *stream, unsigned long arg) 1490 { 1491 struct drm_xe_oa_stream_status status = {}; 1492 void __user *uaddr = (void __user *)arg; 1493 1494 /* Map from register to uapi bits */ 1495 if (stream->oa_status & OASTATUS_REPORT_LOST) 1496 status.oa_status |= DRM_XE_OASTATUS_REPORT_LOST; 1497 if (stream->oa_status & OASTATUS_BUFFER_OVERFLOW) 1498 status.oa_status |= DRM_XE_OASTATUS_BUFFER_OVERFLOW; 1499 if (stream->oa_status & OASTATUS_COUNTER_OVERFLOW) 1500 status.oa_status |= DRM_XE_OASTATUS_COUNTER_OVERFLOW; 1501 if (stream->oa_status & OASTATUS_MMIO_TRG_Q_FULL) 1502 status.oa_status |= DRM_XE_OASTATUS_MMIO_TRG_Q_FULL; 1503 1504 if (copy_to_user(uaddr, &status, sizeof(status))) 1505 return -EFAULT; 1506 1507 return 0; 1508 } 1509 1510 static long xe_oa_info_locked(struct xe_oa_stream *stream, unsigned long arg) 1511 { 1512 struct drm_xe_oa_stream_info info = { .oa_buf_size = XE_OA_BUFFER_SIZE, }; 1513 void __user *uaddr = (void __user *)arg; 1514 1515 if (copy_to_user(uaddr, &info, sizeof(info))) 1516 return -EFAULT; 1517 1518 return 0; 1519 } 1520 1521 static long xe_oa_ioctl_locked(struct xe_oa_stream *stream, 1522 unsigned int cmd, 1523 unsigned long arg) 1524 { 1525 switch (cmd) { 1526 case DRM_XE_OBSERVATION_IOCTL_ENABLE: 1527 return xe_oa_enable_locked(stream); 1528 case DRM_XE_OBSERVATION_IOCTL_DISABLE: 1529 return xe_oa_disable_locked(stream); 1530 case DRM_XE_OBSERVATION_IOCTL_CONFIG: 1531 return xe_oa_config_locked(stream, arg); 1532 case DRM_XE_OBSERVATION_IOCTL_STATUS: 1533 return xe_oa_status_locked(stream, arg); 1534 case DRM_XE_OBSERVATION_IOCTL_INFO: 1535 return xe_oa_info_locked(stream, arg); 1536 } 1537 1538 return -EINVAL; 1539 } 1540 1541 static long xe_oa_ioctl(struct file *file, 1542 unsigned int cmd, 1543 unsigned long arg) 1544 { 1545 struct xe_oa_stream *stream = file->private_data; 1546 long ret; 1547 1548 mutex_lock(&stream->stream_lock); 1549 ret = xe_oa_ioctl_locked(stream, cmd, arg); 1550 mutex_unlock(&stream->stream_lock); 1551 1552 return ret; 1553 } 1554 1555 static void xe_oa_destroy_locked(struct xe_oa_stream *stream) 1556 { 1557 if (stream->enabled) 1558 xe_oa_disable_locked(stream); 1559 1560 xe_oa_stream_destroy(stream); 1561 1562 if (stream->exec_q) 1563 xe_exec_queue_put(stream->exec_q); 1564 1565 kfree(stream); 1566 } 1567 1568 static int xe_oa_release(struct inode *inode, struct file *file) 1569 { 1570 struct xe_oa_stream *stream = file->private_data; 1571 struct xe_gt *gt = stream->gt; 1572 1573 xe_pm_runtime_get(gt_to_xe(gt)); 1574 mutex_lock(>->oa.gt_lock); 1575 xe_oa_destroy_locked(stream); 1576 mutex_unlock(>->oa.gt_lock); 1577 xe_pm_runtime_put(gt_to_xe(gt)); 1578 1579 /* Release the reference the OA stream kept on the driver */ 1580 drm_dev_put(>_to_xe(gt)->drm); 1581 1582 return 0; 1583 } 1584 1585 static int xe_oa_mmap(struct file *file, struct vm_area_struct *vma) 1586 { 1587 struct xe_oa_stream *stream = file->private_data; 1588 struct xe_bo *bo = stream->oa_buffer.bo; 1589 unsigned long start = vma->vm_start; 1590 int i, ret; 1591 1592 if (xe_observation_paranoid && !perfmon_capable()) { 1593 drm_dbg(&stream->oa->xe->drm, "Insufficient privilege to map OA buffer\n"); 1594 return -EACCES; 1595 } 1596 1597 /* Can mmap the entire OA buffer or nothing (no partial OA buffer mmaps) */ 1598 if (vma->vm_end - vma->vm_start != XE_OA_BUFFER_SIZE) { 1599 drm_dbg(&stream->oa->xe->drm, "Wrong mmap size, must be OA buffer size\n"); 1600 return -EINVAL; 1601 } 1602 1603 /* 1604 * Only support VM_READ, enforce MAP_PRIVATE by checking for 1605 * VM_MAYSHARE, don't copy the vma on fork 1606 */ 1607 if (vma->vm_flags & (VM_WRITE | VM_EXEC | VM_SHARED | VM_MAYSHARE)) { 1608 drm_dbg(&stream->oa->xe->drm, "mmap must be read only\n"); 1609 return -EINVAL; 1610 } 1611 vm_flags_mod(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_DONTCOPY, 1612 VM_MAYWRITE | VM_MAYEXEC); 1613 1614 xe_assert(stream->oa->xe, bo->ttm.ttm->num_pages == vma_pages(vma)); 1615 for (i = 0; i < bo->ttm.ttm->num_pages; i++) { 1616 ret = remap_pfn_range(vma, start, page_to_pfn(bo->ttm.ttm->pages[i]), 1617 PAGE_SIZE, vma->vm_page_prot); 1618 if (ret) 1619 break; 1620 1621 start += PAGE_SIZE; 1622 } 1623 1624 return ret; 1625 } 1626 1627 static const struct file_operations xe_oa_fops = { 1628 .owner = THIS_MODULE, 1629 .release = xe_oa_release, 1630 .poll = xe_oa_poll, 1631 .read = xe_oa_read, 1632 .unlocked_ioctl = xe_oa_ioctl, 1633 .mmap = xe_oa_mmap, 1634 }; 1635 1636 static bool engine_supports_mi_query(struct xe_hw_engine *hwe) 1637 { 1638 return hwe->class == XE_ENGINE_CLASS_RENDER || 1639 hwe->class == XE_ENGINE_CLASS_COMPUTE; 1640 } 1641 1642 static bool xe_oa_find_reg_in_lri(u32 *state, u32 reg, u32 *offset, u32 end) 1643 { 1644 u32 idx = *offset; 1645 u32 len = min(MI_LRI_LEN(state[idx]) + idx, end); 1646 bool found = false; 1647 1648 idx++; 1649 for (; idx < len; idx += 2) { 1650 if (state[idx] == reg) { 1651 found = true; 1652 break; 1653 } 1654 } 1655 1656 *offset = idx; 1657 return found; 1658 } 1659 1660 #define IS_MI_LRI_CMD(x) (REG_FIELD_GET(MI_OPCODE, (x)) == \ 1661 REG_FIELD_GET(MI_OPCODE, MI_LOAD_REGISTER_IMM)) 1662 1663 static u32 xe_oa_context_image_offset(struct xe_oa_stream *stream, u32 reg) 1664 { 1665 struct xe_lrc *lrc = stream->exec_q->lrc[0]; 1666 u32 len = (xe_gt_lrc_size(stream->gt, stream->hwe->class) + 1667 lrc->ring.size) / sizeof(u32); 1668 u32 offset = xe_lrc_regs_offset(lrc) / sizeof(u32); 1669 u32 *state = (u32 *)lrc->bo->vmap.vaddr; 1670 1671 if (drm_WARN_ON(&stream->oa->xe->drm, !state)) 1672 return U32_MAX; 1673 1674 for (; offset < len; ) { 1675 if (IS_MI_LRI_CMD(state[offset])) { 1676 /* 1677 * We expect reg-value pairs in MI_LRI command, so 1678 * MI_LRI_LEN() should be even 1679 */ 1680 drm_WARN_ON(&stream->oa->xe->drm, 1681 MI_LRI_LEN(state[offset]) & 0x1); 1682 1683 if (xe_oa_find_reg_in_lri(state, reg, &offset, len)) 1684 break; 1685 } else { 1686 offset++; 1687 } 1688 } 1689 1690 return offset < len ? offset : U32_MAX; 1691 } 1692 1693 static int xe_oa_set_ctx_ctrl_offset(struct xe_oa_stream *stream) 1694 { 1695 struct xe_reg reg = OACTXCONTROL(stream->hwe->mmio_base); 1696 u32 offset = stream->oa->ctx_oactxctrl_offset[stream->hwe->class]; 1697 1698 /* Do this only once. Failure is stored as offset of U32_MAX */ 1699 if (offset) 1700 goto exit; 1701 1702 offset = xe_oa_context_image_offset(stream, reg.addr); 1703 stream->oa->ctx_oactxctrl_offset[stream->hwe->class] = offset; 1704 1705 drm_dbg(&stream->oa->xe->drm, "%s oa ctx control at 0x%08x dword offset\n", 1706 stream->hwe->name, offset); 1707 exit: 1708 return offset && offset != U32_MAX ? 0 : -ENODEV; 1709 } 1710 1711 static int xe_oa_stream_init(struct xe_oa_stream *stream, 1712 struct xe_oa_open_param *param) 1713 { 1714 struct xe_oa_unit *u = param->hwe->oa_unit; 1715 struct xe_gt *gt = param->hwe->gt; 1716 unsigned int fw_ref; 1717 int ret; 1718 1719 stream->exec_q = param->exec_q; 1720 stream->poll_period_ns = DEFAULT_POLL_PERIOD_NS; 1721 stream->hwe = param->hwe; 1722 stream->gt = stream->hwe->gt; 1723 stream->oa_buffer.format = &stream->oa->oa_formats[param->oa_format]; 1724 1725 stream->sample = param->sample; 1726 stream->periodic = param->period_exponent > 0; 1727 stream->period_exponent = param->period_exponent; 1728 stream->no_preempt = param->no_preempt; 1729 1730 stream->xef = xe_file_get(param->xef); 1731 stream->num_syncs = param->num_syncs; 1732 stream->syncs = param->syncs; 1733 1734 /* 1735 * For Xe2+, when overrun mode is enabled, there are no partial reports at the end 1736 * of buffer, making the OA buffer effectively a non-power-of-2 size circular 1737 * buffer whose size, circ_size, is a multiple of the report size 1738 */ 1739 if (GRAPHICS_VER(stream->oa->xe) >= 20 && 1740 stream->hwe->oa_unit->type == DRM_XE_OA_UNIT_TYPE_OAG && stream->sample) 1741 stream->oa_buffer.circ_size = 1742 XE_OA_BUFFER_SIZE - XE_OA_BUFFER_SIZE % stream->oa_buffer.format->size; 1743 else 1744 stream->oa_buffer.circ_size = XE_OA_BUFFER_SIZE; 1745 1746 if (stream->exec_q && engine_supports_mi_query(stream->hwe)) { 1747 /* If we don't find the context offset, just return error */ 1748 ret = xe_oa_set_ctx_ctrl_offset(stream); 1749 if (ret) { 1750 drm_err(&stream->oa->xe->drm, 1751 "xe_oa_set_ctx_ctrl_offset failed for %s\n", 1752 stream->hwe->name); 1753 goto exit; 1754 } 1755 } 1756 1757 stream->oa_config = xe_oa_get_oa_config(stream->oa, param->metric_set); 1758 if (!stream->oa_config) { 1759 drm_dbg(&stream->oa->xe->drm, "Invalid OA config id=%i\n", param->metric_set); 1760 ret = -EINVAL; 1761 goto exit; 1762 } 1763 1764 /* 1765 * Wa_1509372804:pvc 1766 * 1767 * GuC reset of engines causes OA to lose configuration 1768 * state. Prevent this by overriding GUCRC mode. 1769 */ 1770 if (stream->oa->xe->info.platform == XE_PVC) { 1771 ret = xe_guc_pc_override_gucrc_mode(>->uc.guc.pc, 1772 SLPC_GUCRC_MODE_GUCRC_NO_RC6); 1773 if (ret) 1774 goto err_free_configs; 1775 1776 stream->override_gucrc = true; 1777 } 1778 1779 /* Take runtime pm ref and forcewake to disable RC6 */ 1780 xe_pm_runtime_get(stream->oa->xe); 1781 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FORCEWAKE_ALL); 1782 if (!xe_force_wake_ref_has_domain(fw_ref, XE_FORCEWAKE_ALL)) { 1783 ret = -ETIMEDOUT; 1784 goto err_fw_put; 1785 } 1786 1787 ret = xe_oa_alloc_oa_buffer(stream); 1788 if (ret) 1789 goto err_fw_put; 1790 1791 stream->k_exec_q = xe_exec_queue_create(stream->oa->xe, NULL, 1792 BIT(stream->hwe->logical_instance), 1, 1793 stream->hwe, EXEC_QUEUE_FLAG_KERNEL, 0); 1794 if (IS_ERR(stream->k_exec_q)) { 1795 ret = PTR_ERR(stream->k_exec_q); 1796 drm_err(&stream->oa->xe->drm, "gt%d, hwe %s, xe_exec_queue_create failed=%d", 1797 stream->gt->info.id, stream->hwe->name, ret); 1798 goto err_free_oa_buf; 1799 } 1800 1801 ret = xe_oa_enable_metric_set(stream); 1802 if (ret) { 1803 drm_dbg(&stream->oa->xe->drm, "Unable to enable metric set\n"); 1804 goto err_put_k_exec_q; 1805 } 1806 1807 drm_dbg(&stream->oa->xe->drm, "opening stream oa config uuid=%s\n", 1808 stream->oa_config->uuid); 1809 1810 WRITE_ONCE(u->exclusive_stream, stream); 1811 1812 hrtimer_init(&stream->poll_check_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1813 stream->poll_check_timer.function = xe_oa_poll_check_timer_cb; 1814 init_waitqueue_head(&stream->poll_wq); 1815 1816 spin_lock_init(&stream->oa_buffer.ptr_lock); 1817 mutex_init(&stream->stream_lock); 1818 1819 return 0; 1820 1821 err_put_k_exec_q: 1822 xe_oa_disable_metric_set(stream); 1823 xe_exec_queue_put(stream->k_exec_q); 1824 err_free_oa_buf: 1825 xe_oa_free_oa_buffer(stream); 1826 err_fw_put: 1827 xe_force_wake_put(gt_to_fw(gt), fw_ref); 1828 xe_pm_runtime_put(stream->oa->xe); 1829 if (stream->override_gucrc) 1830 xe_gt_WARN_ON(gt, xe_guc_pc_unset_gucrc_mode(>->uc.guc.pc)); 1831 err_free_configs: 1832 xe_oa_free_configs(stream); 1833 exit: 1834 xe_file_put(stream->xef); 1835 return ret; 1836 } 1837 1838 static int xe_oa_stream_open_ioctl_locked(struct xe_oa *oa, 1839 struct xe_oa_open_param *param) 1840 { 1841 struct xe_oa_stream *stream; 1842 int stream_fd; 1843 int ret; 1844 1845 /* We currently only allow exclusive access */ 1846 if (param->hwe->oa_unit->exclusive_stream) { 1847 drm_dbg(&oa->xe->drm, "OA unit already in use\n"); 1848 ret = -EBUSY; 1849 goto exit; 1850 } 1851 1852 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 1853 if (!stream) { 1854 ret = -ENOMEM; 1855 goto exit; 1856 } 1857 1858 stream->oa = oa; 1859 ret = xe_oa_stream_init(stream, param); 1860 if (ret) 1861 goto err_free; 1862 1863 if (!param->disabled) { 1864 ret = xe_oa_enable_locked(stream); 1865 if (ret) 1866 goto err_destroy; 1867 } 1868 1869 stream_fd = anon_inode_getfd("[xe_oa]", &xe_oa_fops, stream, 0); 1870 if (stream_fd < 0) { 1871 ret = stream_fd; 1872 goto err_disable; 1873 } 1874 1875 /* Hold a reference on the drm device till stream_fd is released */ 1876 drm_dev_get(&stream->oa->xe->drm); 1877 1878 return stream_fd; 1879 err_disable: 1880 if (!param->disabled) 1881 xe_oa_disable_locked(stream); 1882 err_destroy: 1883 xe_oa_stream_destroy(stream); 1884 err_free: 1885 kfree(stream); 1886 exit: 1887 return ret; 1888 } 1889 1890 /** 1891 * xe_oa_timestamp_frequency - Return OA timestamp frequency 1892 * @gt: @xe_gt 1893 * 1894 * OA timestamp frequency = CS timestamp frequency in most platforms. On some 1895 * platforms OA unit ignores the CTC_SHIFT and the 2 timestamps differ. In such 1896 * cases, return the adjusted CS timestamp frequency to the user. 1897 */ 1898 u32 xe_oa_timestamp_frequency(struct xe_gt *gt) 1899 { 1900 u32 reg, shift; 1901 1902 /* 1903 * Wa_18013179988:dg2 1904 * Wa_14015568240:pvc 1905 * Wa_14015846243:mtl 1906 */ 1907 switch (gt_to_xe(gt)->info.platform) { 1908 case XE_DG2: 1909 case XE_PVC: 1910 case XE_METEORLAKE: 1911 xe_pm_runtime_get(gt_to_xe(gt)); 1912 reg = xe_mmio_read32(>->mmio, RPM_CONFIG0); 1913 xe_pm_runtime_put(gt_to_xe(gt)); 1914 1915 shift = REG_FIELD_GET(RPM_CONFIG0_CTC_SHIFT_PARAMETER_MASK, reg); 1916 return gt->info.reference_clock << (3 - shift); 1917 1918 default: 1919 return gt->info.reference_clock; 1920 } 1921 } 1922 1923 static u64 oa_exponent_to_ns(struct xe_gt *gt, int exponent) 1924 { 1925 u64 nom = (2ULL << exponent) * NSEC_PER_SEC; 1926 u32 den = xe_oa_timestamp_frequency(gt); 1927 1928 return div_u64(nom + den - 1, den); 1929 } 1930 1931 static bool engine_supports_oa_format(const struct xe_hw_engine *hwe, int type) 1932 { 1933 switch (hwe->oa_unit->type) { 1934 case DRM_XE_OA_UNIT_TYPE_OAG: 1935 return type == DRM_XE_OA_FMT_TYPE_OAG || type == DRM_XE_OA_FMT_TYPE_OAR || 1936 type == DRM_XE_OA_FMT_TYPE_OAC || type == DRM_XE_OA_FMT_TYPE_PEC; 1937 case DRM_XE_OA_UNIT_TYPE_OAM: 1938 return type == DRM_XE_OA_FMT_TYPE_OAM || type == DRM_XE_OA_FMT_TYPE_OAM_MPEC; 1939 default: 1940 return false; 1941 } 1942 } 1943 1944 /** 1945 * xe_oa_unit_id - Return OA unit ID for a hardware engine 1946 * @hwe: @xe_hw_engine 1947 * 1948 * Return OA unit ID for a hardware engine when available 1949 */ 1950 u16 xe_oa_unit_id(struct xe_hw_engine *hwe) 1951 { 1952 return hwe->oa_unit && hwe->oa_unit->num_engines ? 1953 hwe->oa_unit->oa_unit_id : U16_MAX; 1954 } 1955 1956 static int xe_oa_assign_hwe(struct xe_oa *oa, struct xe_oa_open_param *param) 1957 { 1958 struct xe_gt *gt; 1959 int i, ret = 0; 1960 1961 if (param->exec_q) { 1962 /* When we have an exec_q, get hwe from the exec_q */ 1963 param->hwe = xe_gt_hw_engine(param->exec_q->gt, param->exec_q->class, 1964 param->engine_instance, true); 1965 } else { 1966 struct xe_hw_engine *hwe; 1967 enum xe_hw_engine_id id; 1968 1969 /* Else just get the first hwe attached to the oa unit */ 1970 for_each_gt(gt, oa->xe, i) { 1971 for_each_hw_engine(hwe, gt, id) { 1972 if (xe_oa_unit_id(hwe) == param->oa_unit_id) { 1973 param->hwe = hwe; 1974 goto out; 1975 } 1976 } 1977 } 1978 } 1979 out: 1980 if (!param->hwe || xe_oa_unit_id(param->hwe) != param->oa_unit_id) { 1981 drm_dbg(&oa->xe->drm, "Unable to find hwe (%d, %d) for OA unit ID %d\n", 1982 param->exec_q ? param->exec_q->class : -1, 1983 param->engine_instance, param->oa_unit_id); 1984 ret = -EINVAL; 1985 } 1986 1987 return ret; 1988 } 1989 1990 /** 1991 * xe_oa_stream_open_ioctl - Opens an OA stream 1992 * @dev: @drm_device 1993 * @data: pointer to struct @drm_xe_oa_config 1994 * @file: @drm_file 1995 * 1996 * The functions opens an OA stream. An OA stream, opened with specified 1997 * properties, enables OA counter samples to be collected, either 1998 * periodically (time based sampling), or on request (using OA queries) 1999 */ 2000 int xe_oa_stream_open_ioctl(struct drm_device *dev, u64 data, struct drm_file *file) 2001 { 2002 struct xe_device *xe = to_xe_device(dev); 2003 struct xe_oa *oa = &xe->oa; 2004 struct xe_file *xef = to_xe_file(file); 2005 struct xe_oa_open_param param = {}; 2006 const struct xe_oa_format *f; 2007 bool privileged_op = true; 2008 int ret; 2009 2010 if (!oa->xe) { 2011 drm_dbg(&xe->drm, "xe oa interface not available for this system\n"); 2012 return -ENODEV; 2013 } 2014 2015 param.xef = xef; 2016 ret = xe_oa_user_extensions(oa, XE_OA_USER_EXTN_FROM_OPEN, data, 0, ¶m); 2017 if (ret) 2018 return ret; 2019 2020 if (param.exec_queue_id > 0) { 2021 param.exec_q = xe_exec_queue_lookup(xef, param.exec_queue_id); 2022 if (XE_IOCTL_DBG(oa->xe, !param.exec_q)) 2023 return -ENOENT; 2024 2025 if (XE_IOCTL_DBG(oa->xe, param.exec_q->width > 1)) 2026 return -EOPNOTSUPP; 2027 } 2028 2029 /* 2030 * Query based sampling (using MI_REPORT_PERF_COUNT) with OAR/OAC, 2031 * without global stream access, can be an unprivileged operation 2032 */ 2033 if (param.exec_q && !param.sample) 2034 privileged_op = false; 2035 2036 if (param.no_preempt) { 2037 if (!param.exec_q) { 2038 drm_dbg(&oa->xe->drm, "Preemption disable without exec_q!\n"); 2039 ret = -EINVAL; 2040 goto err_exec_q; 2041 } 2042 privileged_op = true; 2043 } 2044 2045 if (privileged_op && xe_observation_paranoid && !perfmon_capable()) { 2046 drm_dbg(&oa->xe->drm, "Insufficient privileges to open xe OA stream\n"); 2047 ret = -EACCES; 2048 goto err_exec_q; 2049 } 2050 2051 if (!param.exec_q && !param.sample) { 2052 drm_dbg(&oa->xe->drm, "Only OA report sampling supported\n"); 2053 ret = -EINVAL; 2054 goto err_exec_q; 2055 } 2056 2057 ret = xe_oa_assign_hwe(oa, ¶m); 2058 if (ret) 2059 goto err_exec_q; 2060 2061 f = &oa->oa_formats[param.oa_format]; 2062 if (!param.oa_format || !f->size || 2063 !engine_supports_oa_format(param.hwe, f->type)) { 2064 drm_dbg(&oa->xe->drm, "Invalid OA format %d type %d size %d for class %d\n", 2065 param.oa_format, f->type, f->size, param.hwe->class); 2066 ret = -EINVAL; 2067 goto err_exec_q; 2068 } 2069 2070 if (param.period_exponent > 0) { 2071 u64 oa_period, oa_freq_hz; 2072 2073 /* Requesting samples from OAG buffer is a privileged operation */ 2074 if (!param.sample) { 2075 drm_dbg(&oa->xe->drm, "OA_EXPONENT specified without SAMPLE_OA\n"); 2076 ret = -EINVAL; 2077 goto err_exec_q; 2078 } 2079 oa_period = oa_exponent_to_ns(param.hwe->gt, param.period_exponent); 2080 oa_freq_hz = div64_u64(NSEC_PER_SEC, oa_period); 2081 drm_dbg(&oa->xe->drm, "Using periodic sampling freq %lld Hz\n", oa_freq_hz); 2082 } 2083 2084 ret = xe_oa_parse_syncs(oa, ¶m); 2085 if (ret) 2086 goto err_exec_q; 2087 2088 mutex_lock(¶m.hwe->gt->oa.gt_lock); 2089 ret = xe_oa_stream_open_ioctl_locked(oa, ¶m); 2090 mutex_unlock(¶m.hwe->gt->oa.gt_lock); 2091 if (ret < 0) 2092 goto err_sync_cleanup; 2093 2094 return ret; 2095 2096 err_sync_cleanup: 2097 while (param.num_syncs--) 2098 xe_sync_entry_cleanup(¶m.syncs[param.num_syncs]); 2099 kfree(param.syncs); 2100 err_exec_q: 2101 if (param.exec_q) 2102 xe_exec_queue_put(param.exec_q); 2103 return ret; 2104 } 2105 2106 static bool xe_oa_is_valid_flex_addr(struct xe_oa *oa, u32 addr) 2107 { 2108 static const struct xe_reg flex_eu_regs[] = { 2109 EU_PERF_CNTL0, 2110 EU_PERF_CNTL1, 2111 EU_PERF_CNTL2, 2112 EU_PERF_CNTL3, 2113 EU_PERF_CNTL4, 2114 EU_PERF_CNTL5, 2115 EU_PERF_CNTL6, 2116 }; 2117 int i; 2118 2119 for (i = 0; i < ARRAY_SIZE(flex_eu_regs); i++) { 2120 if (flex_eu_regs[i].addr == addr) 2121 return true; 2122 } 2123 return false; 2124 } 2125 2126 static bool xe_oa_reg_in_range_table(u32 addr, const struct xe_mmio_range *table) 2127 { 2128 while (table->start && table->end) { 2129 if (addr >= table->start && addr <= table->end) 2130 return true; 2131 2132 table++; 2133 } 2134 2135 return false; 2136 } 2137 2138 static const struct xe_mmio_range xehp_oa_b_counters[] = { 2139 { .start = 0xdc48, .end = 0xdc48 }, /* OAA_ENABLE_REG */ 2140 { .start = 0xdd00, .end = 0xdd48 }, /* OAG_LCE0_0 - OAA_LENABLE_REG */ 2141 {} 2142 }; 2143 2144 static const struct xe_mmio_range gen12_oa_b_counters[] = { 2145 { .start = 0x2b2c, .end = 0x2b2c }, /* OAG_OA_PESS */ 2146 { .start = 0xd900, .end = 0xd91c }, /* OAG_OASTARTTRIG[1-8] */ 2147 { .start = 0xd920, .end = 0xd93c }, /* OAG_OAREPORTTRIG1[1-8] */ 2148 { .start = 0xd940, .end = 0xd97c }, /* OAG_CEC[0-7][0-1] */ 2149 { .start = 0xdc00, .end = 0xdc3c }, /* OAG_SCEC[0-7][0-1] */ 2150 { .start = 0xdc40, .end = 0xdc40 }, /* OAG_SPCTR_CNF */ 2151 { .start = 0xdc44, .end = 0xdc44 }, /* OAA_DBG_REG */ 2152 {} 2153 }; 2154 2155 static const struct xe_mmio_range mtl_oam_b_counters[] = { 2156 { .start = 0x393000, .end = 0x39301c }, /* OAM_STARTTRIG1[1-8] */ 2157 { .start = 0x393020, .end = 0x39303c }, /* OAM_REPORTTRIG1[1-8] */ 2158 { .start = 0x393040, .end = 0x39307c }, /* OAM_CEC[0-7][0-1] */ 2159 { .start = 0x393200, .end = 0x39323C }, /* MPES[0-7] */ 2160 {} 2161 }; 2162 2163 static const struct xe_mmio_range xe2_oa_b_counters[] = { 2164 { .start = 0x393200, .end = 0x39323C }, /* MPES_0_MPES_SAG - MPES_7_UPPER_MPES_SAG */ 2165 { .start = 0x394200, .end = 0x39423C }, /* MPES_0_MPES_SCMI0 - MPES_7_UPPER_MPES_SCMI0 */ 2166 { .start = 0x394A00, .end = 0x394A3C }, /* MPES_0_MPES_SCMI1 - MPES_7_UPPER_MPES_SCMI1 */ 2167 {}, 2168 }; 2169 2170 static bool xe_oa_is_valid_b_counter_addr(struct xe_oa *oa, u32 addr) 2171 { 2172 return xe_oa_reg_in_range_table(addr, xehp_oa_b_counters) || 2173 xe_oa_reg_in_range_table(addr, gen12_oa_b_counters) || 2174 xe_oa_reg_in_range_table(addr, mtl_oam_b_counters) || 2175 (GRAPHICS_VER(oa->xe) >= 20 && 2176 xe_oa_reg_in_range_table(addr, xe2_oa_b_counters)); 2177 } 2178 2179 static const struct xe_mmio_range mtl_oa_mux_regs[] = { 2180 { .start = 0x0d00, .end = 0x0d04 }, /* RPM_CONFIG[0-1] */ 2181 { .start = 0x0d0c, .end = 0x0d2c }, /* NOA_CONFIG[0-8] */ 2182 { .start = 0x9840, .end = 0x9840 }, /* GDT_CHICKEN_BITS */ 2183 { .start = 0x9884, .end = 0x9888 }, /* NOA_WRITE */ 2184 { .start = 0x38d100, .end = 0x38d114}, /* VISACTL */ 2185 {} 2186 }; 2187 2188 static const struct xe_mmio_range gen12_oa_mux_regs[] = { 2189 { .start = 0x0d00, .end = 0x0d04 }, /* RPM_CONFIG[0-1] */ 2190 { .start = 0x0d0c, .end = 0x0d2c }, /* NOA_CONFIG[0-8] */ 2191 { .start = 0x9840, .end = 0x9840 }, /* GDT_CHICKEN_BITS */ 2192 { .start = 0x9884, .end = 0x9888 }, /* NOA_WRITE */ 2193 { .start = 0x20cc, .end = 0x20cc }, /* WAIT_FOR_RC6_EXIT */ 2194 {} 2195 }; 2196 2197 static const struct xe_mmio_range xe2_oa_mux_regs[] = { 2198 { .start = 0x5194, .end = 0x5194 }, /* SYS_MEM_LAT_MEASURE_MERTF_GRP_3D */ 2199 { .start = 0x8704, .end = 0x8704 }, /* LMEM_LAT_MEASURE_MCFG_GRP */ 2200 { .start = 0xB1BC, .end = 0xB1BC }, /* L3_BANK_LAT_MEASURE_LBCF_GFX */ 2201 { .start = 0xE18C, .end = 0xE18C }, /* SAMPLER_MODE */ 2202 { .start = 0xE590, .end = 0xE590 }, /* TDL_LSC_LAT_MEASURE_TDL_GFX */ 2203 { .start = 0x13000, .end = 0x137FC }, /* PES_0_PESL0 - PES_63_UPPER_PESL3 */ 2204 {}, 2205 }; 2206 2207 static bool xe_oa_is_valid_mux_addr(struct xe_oa *oa, u32 addr) 2208 { 2209 if (GRAPHICS_VER(oa->xe) >= 20) 2210 return xe_oa_reg_in_range_table(addr, xe2_oa_mux_regs); 2211 else if (GRAPHICS_VERx100(oa->xe) >= 1270) 2212 return xe_oa_reg_in_range_table(addr, mtl_oa_mux_regs); 2213 else 2214 return xe_oa_reg_in_range_table(addr, gen12_oa_mux_regs); 2215 } 2216 2217 static bool xe_oa_is_valid_config_reg_addr(struct xe_oa *oa, u32 addr) 2218 { 2219 return xe_oa_is_valid_flex_addr(oa, addr) || 2220 xe_oa_is_valid_b_counter_addr(oa, addr) || 2221 xe_oa_is_valid_mux_addr(oa, addr); 2222 } 2223 2224 static struct xe_oa_reg * 2225 xe_oa_alloc_regs(struct xe_oa *oa, bool (*is_valid)(struct xe_oa *oa, u32 addr), 2226 u32 __user *regs, u32 n_regs) 2227 { 2228 struct xe_oa_reg *oa_regs; 2229 int err; 2230 u32 i; 2231 2232 oa_regs = kmalloc_array(n_regs, sizeof(*oa_regs), GFP_KERNEL); 2233 if (!oa_regs) 2234 return ERR_PTR(-ENOMEM); 2235 2236 for (i = 0; i < n_regs; i++) { 2237 u32 addr, value; 2238 2239 err = get_user(addr, regs); 2240 if (err) 2241 goto addr_err; 2242 2243 if (!is_valid(oa, addr)) { 2244 drm_dbg(&oa->xe->drm, "Invalid oa_reg address: %X\n", addr); 2245 err = -EINVAL; 2246 goto addr_err; 2247 } 2248 2249 err = get_user(value, regs + 1); 2250 if (err) 2251 goto addr_err; 2252 2253 oa_regs[i].addr = XE_REG(addr); 2254 oa_regs[i].value = value; 2255 2256 regs += 2; 2257 } 2258 2259 return oa_regs; 2260 2261 addr_err: 2262 kfree(oa_regs); 2263 return ERR_PTR(err); 2264 } 2265 2266 static ssize_t show_dynamic_id(struct kobject *kobj, 2267 struct kobj_attribute *attr, 2268 char *buf) 2269 { 2270 struct xe_oa_config *oa_config = 2271 container_of(attr, typeof(*oa_config), sysfs_metric_id); 2272 2273 return sysfs_emit(buf, "%d\n", oa_config->id); 2274 } 2275 2276 static int create_dynamic_oa_sysfs_entry(struct xe_oa *oa, 2277 struct xe_oa_config *oa_config) 2278 { 2279 sysfs_attr_init(&oa_config->sysfs_metric_id.attr); 2280 oa_config->sysfs_metric_id.attr.name = "id"; 2281 oa_config->sysfs_metric_id.attr.mode = 0444; 2282 oa_config->sysfs_metric_id.show = show_dynamic_id; 2283 oa_config->sysfs_metric_id.store = NULL; 2284 2285 oa_config->attrs[0] = &oa_config->sysfs_metric_id.attr; 2286 oa_config->attrs[1] = NULL; 2287 2288 oa_config->sysfs_metric.name = oa_config->uuid; 2289 oa_config->sysfs_metric.attrs = oa_config->attrs; 2290 2291 return sysfs_create_group(oa->metrics_kobj, &oa_config->sysfs_metric); 2292 } 2293 2294 /** 2295 * xe_oa_add_config_ioctl - Adds one OA config 2296 * @dev: @drm_device 2297 * @data: pointer to struct @drm_xe_oa_config 2298 * @file: @drm_file 2299 * 2300 * The functions adds an OA config to the set of OA configs maintained in 2301 * the kernel. The config determines which OA metrics are collected for an 2302 * OA stream. 2303 */ 2304 int xe_oa_add_config_ioctl(struct drm_device *dev, u64 data, struct drm_file *file) 2305 { 2306 struct xe_device *xe = to_xe_device(dev); 2307 struct xe_oa *oa = &xe->oa; 2308 struct drm_xe_oa_config param; 2309 struct drm_xe_oa_config *arg = ¶m; 2310 struct xe_oa_config *oa_config, *tmp; 2311 struct xe_oa_reg *regs; 2312 int err, id; 2313 2314 if (!oa->xe) { 2315 drm_dbg(&xe->drm, "xe oa interface not available for this system\n"); 2316 return -ENODEV; 2317 } 2318 2319 if (xe_observation_paranoid && !perfmon_capable()) { 2320 drm_dbg(&oa->xe->drm, "Insufficient privileges to add xe OA config\n"); 2321 return -EACCES; 2322 } 2323 2324 err = __copy_from_user(¶m, u64_to_user_ptr(data), sizeof(param)); 2325 if (XE_IOCTL_DBG(oa->xe, err)) 2326 return -EFAULT; 2327 2328 if (XE_IOCTL_DBG(oa->xe, arg->extensions) || 2329 XE_IOCTL_DBG(oa->xe, !arg->regs_ptr) || 2330 XE_IOCTL_DBG(oa->xe, !arg->n_regs)) 2331 return -EINVAL; 2332 2333 oa_config = kzalloc(sizeof(*oa_config), GFP_KERNEL); 2334 if (!oa_config) 2335 return -ENOMEM; 2336 2337 oa_config->oa = oa; 2338 kref_init(&oa_config->ref); 2339 2340 if (!uuid_is_valid(arg->uuid)) { 2341 drm_dbg(&oa->xe->drm, "Invalid uuid format for OA config\n"); 2342 err = -EINVAL; 2343 goto reg_err; 2344 } 2345 2346 /* Last character in oa_config->uuid will be 0 because oa_config is kzalloc */ 2347 memcpy(oa_config->uuid, arg->uuid, sizeof(arg->uuid)); 2348 2349 oa_config->regs_len = arg->n_regs; 2350 regs = xe_oa_alloc_regs(oa, xe_oa_is_valid_config_reg_addr, 2351 u64_to_user_ptr(arg->regs_ptr), 2352 arg->n_regs); 2353 if (IS_ERR(regs)) { 2354 drm_dbg(&oa->xe->drm, "Failed to create OA config for mux_regs\n"); 2355 err = PTR_ERR(regs); 2356 goto reg_err; 2357 } 2358 oa_config->regs = regs; 2359 2360 err = mutex_lock_interruptible(&oa->metrics_lock); 2361 if (err) 2362 goto reg_err; 2363 2364 /* We shouldn't have too many configs, so this iteration shouldn't be too costly */ 2365 idr_for_each_entry(&oa->metrics_idr, tmp, id) { 2366 if (!strcmp(tmp->uuid, oa_config->uuid)) { 2367 drm_dbg(&oa->xe->drm, "OA config already exists with this uuid\n"); 2368 err = -EADDRINUSE; 2369 goto sysfs_err; 2370 } 2371 } 2372 2373 err = create_dynamic_oa_sysfs_entry(oa, oa_config); 2374 if (err) { 2375 drm_dbg(&oa->xe->drm, "Failed to create sysfs entry for OA config\n"); 2376 goto sysfs_err; 2377 } 2378 2379 oa_config->id = idr_alloc(&oa->metrics_idr, oa_config, 1, 0, GFP_KERNEL); 2380 if (oa_config->id < 0) { 2381 drm_dbg(&oa->xe->drm, "Failed to create sysfs entry for OA config\n"); 2382 err = oa_config->id; 2383 goto sysfs_err; 2384 } 2385 2386 mutex_unlock(&oa->metrics_lock); 2387 2388 drm_dbg(&oa->xe->drm, "Added config %s id=%i\n", oa_config->uuid, oa_config->id); 2389 2390 return oa_config->id; 2391 2392 sysfs_err: 2393 mutex_unlock(&oa->metrics_lock); 2394 reg_err: 2395 xe_oa_config_put(oa_config); 2396 drm_dbg(&oa->xe->drm, "Failed to add new OA config\n"); 2397 return err; 2398 } 2399 2400 /** 2401 * xe_oa_remove_config_ioctl - Removes one OA config 2402 * @dev: @drm_device 2403 * @data: pointer to struct @drm_xe_observation_param 2404 * @file: @drm_file 2405 */ 2406 int xe_oa_remove_config_ioctl(struct drm_device *dev, u64 data, struct drm_file *file) 2407 { 2408 struct xe_device *xe = to_xe_device(dev); 2409 struct xe_oa *oa = &xe->oa; 2410 struct xe_oa_config *oa_config; 2411 u64 arg, *ptr = u64_to_user_ptr(data); 2412 int ret; 2413 2414 if (!oa->xe) { 2415 drm_dbg(&xe->drm, "xe oa interface not available for this system\n"); 2416 return -ENODEV; 2417 } 2418 2419 if (xe_observation_paranoid && !perfmon_capable()) { 2420 drm_dbg(&oa->xe->drm, "Insufficient privileges to remove xe OA config\n"); 2421 return -EACCES; 2422 } 2423 2424 ret = get_user(arg, ptr); 2425 if (XE_IOCTL_DBG(oa->xe, ret)) 2426 return ret; 2427 2428 ret = mutex_lock_interruptible(&oa->metrics_lock); 2429 if (ret) 2430 return ret; 2431 2432 oa_config = idr_find(&oa->metrics_idr, arg); 2433 if (!oa_config) { 2434 drm_dbg(&oa->xe->drm, "Failed to remove unknown OA config\n"); 2435 ret = -ENOENT; 2436 goto err_unlock; 2437 } 2438 2439 WARN_ON(arg != oa_config->id); 2440 2441 sysfs_remove_group(oa->metrics_kobj, &oa_config->sysfs_metric); 2442 idr_remove(&oa->metrics_idr, arg); 2443 2444 mutex_unlock(&oa->metrics_lock); 2445 2446 drm_dbg(&oa->xe->drm, "Removed config %s id=%i\n", oa_config->uuid, oa_config->id); 2447 2448 xe_oa_config_put(oa_config); 2449 2450 return 0; 2451 2452 err_unlock: 2453 mutex_unlock(&oa->metrics_lock); 2454 return ret; 2455 } 2456 2457 /** 2458 * xe_oa_register - Xe OA registration 2459 * @xe: @xe_device 2460 * 2461 * Exposes the metrics sysfs directory upon completion of module initialization 2462 */ 2463 void xe_oa_register(struct xe_device *xe) 2464 { 2465 struct xe_oa *oa = &xe->oa; 2466 2467 if (!oa->xe) 2468 return; 2469 2470 oa->metrics_kobj = kobject_create_and_add("metrics", 2471 &xe->drm.primary->kdev->kobj); 2472 } 2473 2474 /** 2475 * xe_oa_unregister - Xe OA de-registration 2476 * @xe: @xe_device 2477 */ 2478 void xe_oa_unregister(struct xe_device *xe) 2479 { 2480 struct xe_oa *oa = &xe->oa; 2481 2482 if (!oa->metrics_kobj) 2483 return; 2484 2485 kobject_put(oa->metrics_kobj); 2486 oa->metrics_kobj = NULL; 2487 } 2488 2489 static u32 num_oa_units_per_gt(struct xe_gt *gt) 2490 { 2491 return 1; 2492 } 2493 2494 static u32 __hwe_oam_unit(struct xe_hw_engine *hwe) 2495 { 2496 if (GRAPHICS_VERx100(gt_to_xe(hwe->gt)) >= 1270) { 2497 /* 2498 * There's 1 SAMEDIA gt and 1 OAM per SAMEDIA gt. All media slices 2499 * within the gt use the same OAM. All MTL/LNL SKUs list 1 SA MEDIA 2500 */ 2501 xe_gt_WARN_ON(hwe->gt, hwe->gt->info.type != XE_GT_TYPE_MEDIA); 2502 2503 return 0; 2504 } 2505 2506 return XE_OA_UNIT_INVALID; 2507 } 2508 2509 static u32 __hwe_oa_unit(struct xe_hw_engine *hwe) 2510 { 2511 switch (hwe->class) { 2512 case XE_ENGINE_CLASS_RENDER: 2513 case XE_ENGINE_CLASS_COMPUTE: 2514 return 0; 2515 2516 case XE_ENGINE_CLASS_VIDEO_DECODE: 2517 case XE_ENGINE_CLASS_VIDEO_ENHANCE: 2518 return __hwe_oam_unit(hwe); 2519 2520 default: 2521 return XE_OA_UNIT_INVALID; 2522 } 2523 } 2524 2525 static struct xe_oa_regs __oam_regs(u32 base) 2526 { 2527 return (struct xe_oa_regs) { 2528 base, 2529 OAM_HEAD_POINTER(base), 2530 OAM_TAIL_POINTER(base), 2531 OAM_BUFFER(base), 2532 OAM_CONTEXT_CONTROL(base), 2533 OAM_CONTROL(base), 2534 OAM_DEBUG(base), 2535 OAM_STATUS(base), 2536 OAM_CONTROL_COUNTER_SEL_MASK, 2537 }; 2538 } 2539 2540 static struct xe_oa_regs __oag_regs(void) 2541 { 2542 return (struct xe_oa_regs) { 2543 0, 2544 OAG_OAHEADPTR, 2545 OAG_OATAILPTR, 2546 OAG_OABUFFER, 2547 OAG_OAGLBCTXCTRL, 2548 OAG_OACONTROL, 2549 OAG_OA_DEBUG, 2550 OAG_OASTATUS, 2551 OAG_OACONTROL_OA_COUNTER_SEL_MASK, 2552 }; 2553 } 2554 2555 static void __xe_oa_init_oa_units(struct xe_gt *gt) 2556 { 2557 const u32 mtl_oa_base[] = { 0x13000 }; 2558 int i, num_units = gt->oa.num_oa_units; 2559 2560 for (i = 0; i < num_units; i++) { 2561 struct xe_oa_unit *u = >->oa.oa_unit[i]; 2562 2563 if (gt->info.type != XE_GT_TYPE_MEDIA) { 2564 u->regs = __oag_regs(); 2565 u->type = DRM_XE_OA_UNIT_TYPE_OAG; 2566 } else if (GRAPHICS_VERx100(gt_to_xe(gt)) >= 1270) { 2567 u->regs = __oam_regs(mtl_oa_base[i]); 2568 u->type = DRM_XE_OA_UNIT_TYPE_OAM; 2569 } 2570 2571 /* Ensure MMIO trigger remains disabled till there is a stream */ 2572 xe_mmio_write32(>->mmio, u->regs.oa_debug, 2573 oag_configure_mmio_trigger(NULL, false)); 2574 2575 /* Set oa_unit_ids now to ensure ids remain contiguous */ 2576 u->oa_unit_id = gt_to_xe(gt)->oa.oa_unit_ids++; 2577 } 2578 } 2579 2580 static int xe_oa_init_gt(struct xe_gt *gt) 2581 { 2582 u32 num_oa_units = num_oa_units_per_gt(gt); 2583 struct xe_hw_engine *hwe; 2584 enum xe_hw_engine_id id; 2585 struct xe_oa_unit *u; 2586 2587 u = drmm_kcalloc(>_to_xe(gt)->drm, num_oa_units, sizeof(*u), GFP_KERNEL); 2588 if (!u) 2589 return -ENOMEM; 2590 2591 for_each_hw_engine(hwe, gt, id) { 2592 u32 index = __hwe_oa_unit(hwe); 2593 2594 hwe->oa_unit = NULL; 2595 if (index < num_oa_units) { 2596 u[index].num_engines++; 2597 hwe->oa_unit = &u[index]; 2598 } 2599 } 2600 2601 /* 2602 * Fused off engines can result in oa_unit's with num_engines == 0. These units 2603 * will appear in OA unit query, but no OA streams can be opened on them. 2604 */ 2605 gt->oa.num_oa_units = num_oa_units; 2606 gt->oa.oa_unit = u; 2607 2608 __xe_oa_init_oa_units(gt); 2609 2610 drmm_mutex_init(>_to_xe(gt)->drm, >->oa.gt_lock); 2611 2612 return 0; 2613 } 2614 2615 static int xe_oa_init_oa_units(struct xe_oa *oa) 2616 { 2617 struct xe_gt *gt; 2618 int i, ret; 2619 2620 for_each_gt(gt, oa->xe, i) { 2621 ret = xe_oa_init_gt(gt); 2622 if (ret) 2623 return ret; 2624 } 2625 2626 return 0; 2627 } 2628 2629 static void oa_format_add(struct xe_oa *oa, enum xe_oa_format_name format) 2630 { 2631 __set_bit(format, oa->format_mask); 2632 } 2633 2634 static void xe_oa_init_supported_formats(struct xe_oa *oa) 2635 { 2636 if (GRAPHICS_VER(oa->xe) >= 20) { 2637 /* Xe2+ */ 2638 oa_format_add(oa, XE_OAM_FORMAT_MPEC8u64_B8_C8); 2639 oa_format_add(oa, XE_OAM_FORMAT_MPEC8u32_B8_C8); 2640 oa_format_add(oa, XE_OA_FORMAT_PEC64u64); 2641 oa_format_add(oa, XE_OA_FORMAT_PEC64u64_B8_C8); 2642 oa_format_add(oa, XE_OA_FORMAT_PEC64u32); 2643 oa_format_add(oa, XE_OA_FORMAT_PEC32u64_G1); 2644 oa_format_add(oa, XE_OA_FORMAT_PEC32u32_G1); 2645 oa_format_add(oa, XE_OA_FORMAT_PEC32u64_G2); 2646 oa_format_add(oa, XE_OA_FORMAT_PEC32u32_G2); 2647 oa_format_add(oa, XE_OA_FORMAT_PEC36u64_G1_32_G2_4); 2648 oa_format_add(oa, XE_OA_FORMAT_PEC36u64_G1_4_G2_32); 2649 } else if (GRAPHICS_VERx100(oa->xe) >= 1270) { 2650 /* XE_METEORLAKE */ 2651 oa_format_add(oa, XE_OAR_FORMAT_A32u40_A4u32_B8_C8); 2652 oa_format_add(oa, XE_OA_FORMAT_A24u40_A14u32_B8_C8); 2653 oa_format_add(oa, XE_OAC_FORMAT_A24u64_B8_C8); 2654 oa_format_add(oa, XE_OAC_FORMAT_A22u32_R2u32_B8_C8); 2655 oa_format_add(oa, XE_OAM_FORMAT_MPEC8u64_B8_C8); 2656 oa_format_add(oa, XE_OAM_FORMAT_MPEC8u32_B8_C8); 2657 } else if (GRAPHICS_VERx100(oa->xe) >= 1255) { 2658 /* XE_DG2, XE_PVC */ 2659 oa_format_add(oa, XE_OAR_FORMAT_A32u40_A4u32_B8_C8); 2660 oa_format_add(oa, XE_OA_FORMAT_A24u40_A14u32_B8_C8); 2661 oa_format_add(oa, XE_OAC_FORMAT_A24u64_B8_C8); 2662 oa_format_add(oa, XE_OAC_FORMAT_A22u32_R2u32_B8_C8); 2663 } else { 2664 /* Gen12+ */ 2665 xe_assert(oa->xe, GRAPHICS_VER(oa->xe) >= 12); 2666 oa_format_add(oa, XE_OA_FORMAT_A12); 2667 oa_format_add(oa, XE_OA_FORMAT_A12_B8_C8); 2668 oa_format_add(oa, XE_OA_FORMAT_A32u40_A4u32_B8_C8); 2669 oa_format_add(oa, XE_OA_FORMAT_C4_B8); 2670 } 2671 } 2672 2673 /** 2674 * xe_oa_init - OA initialization during device probe 2675 * @xe: @xe_device 2676 * 2677 * Return: 0 on success or a negative error code on failure 2678 */ 2679 int xe_oa_init(struct xe_device *xe) 2680 { 2681 struct xe_oa *oa = &xe->oa; 2682 int ret; 2683 2684 /* Support OA only with GuC submission and Gen12+ */ 2685 if (!xe_device_uc_enabled(xe) || GRAPHICS_VER(xe) < 12) 2686 return 0; 2687 2688 if (IS_SRIOV_VF(xe)) 2689 return 0; 2690 2691 oa->xe = xe; 2692 oa->oa_formats = oa_formats; 2693 2694 drmm_mutex_init(&oa->xe->drm, &oa->metrics_lock); 2695 idr_init_base(&oa->metrics_idr, 1); 2696 2697 ret = xe_oa_init_oa_units(oa); 2698 if (ret) { 2699 drm_err(&xe->drm, "OA initialization failed (%pe)\n", ERR_PTR(ret)); 2700 goto exit; 2701 } 2702 2703 xe_oa_init_supported_formats(oa); 2704 return 0; 2705 exit: 2706 oa->xe = NULL; 2707 return ret; 2708 } 2709 2710 static int destroy_config(int id, void *p, void *data) 2711 { 2712 xe_oa_config_put(p); 2713 return 0; 2714 } 2715 2716 /** 2717 * xe_oa_fini - OA de-initialization during device remove 2718 * @xe: @xe_device 2719 */ 2720 void xe_oa_fini(struct xe_device *xe) 2721 { 2722 struct xe_oa *oa = &xe->oa; 2723 2724 if (!oa->xe) 2725 return; 2726 2727 idr_for_each(&oa->metrics_idr, destroy_config, oa); 2728 idr_destroy(&oa->metrics_idr); 2729 2730 oa->xe = NULL; 2731 } 2732