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