1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2025 Intel Corporation 4 */ 5 6 #include <linux/anon_inodes.h> 7 #include <linux/fs.h> 8 #include <linux/poll.h> 9 #include <linux/types.h> 10 11 #include <drm/drm_drv.h> 12 #include <generated/xe_wa_oob.h> 13 #include <uapi/drm/xe_drm.h> 14 15 #include "xe_bo.h" 16 #include "xe_device.h" 17 #include "xe_eu_stall.h" 18 #include "xe_force_wake.h" 19 #include "xe_gt_mcr.h" 20 #include "xe_gt_printk.h" 21 #include "xe_gt_topology.h" 22 #include "xe_macros.h" 23 #include "xe_observation.h" 24 #include "xe_pm.h" 25 #include "xe_trace.h" 26 #include "xe_wa.h" 27 28 #include "regs/xe_eu_stall_regs.h" 29 #include "regs/xe_gt_regs.h" 30 31 #define POLL_PERIOD_MS 5 32 33 static size_t per_xecore_buf_size = SZ_512K; 34 35 struct per_xecore_buf { 36 /* Buffer vaddr */ 37 u8 *vaddr; 38 /* Write pointer */ 39 u32 write; 40 /* Read pointer */ 41 u32 read; 42 }; 43 44 struct xe_eu_stall_data_stream { 45 bool pollin; 46 bool enabled; 47 int wait_num_reports; 48 int sampling_rate_mult; 49 wait_queue_head_t poll_wq; 50 size_t data_record_size; 51 size_t per_xecore_buf_size; 52 53 struct xe_gt *gt; 54 struct xe_bo *bo; 55 struct per_xecore_buf *xecore_buf; 56 struct { 57 bool reported_to_user; 58 xe_dss_mask_t mask; 59 } data_drop; 60 struct delayed_work buf_poll_work; 61 }; 62 63 struct xe_eu_stall_gt { 64 /* Lock to protect stream */ 65 struct mutex stream_lock; 66 /* EU stall data stream */ 67 struct xe_eu_stall_data_stream *stream; 68 /* Workqueue to schedule buffer pointers polling work */ 69 struct workqueue_struct *buf_ptr_poll_wq; 70 }; 71 72 /** 73 * struct eu_stall_open_properties - EU stall sampling properties received 74 * from user space at open. 75 * @sampling_rate_mult: EU stall sampling rate multiplier. 76 * HW will sample every (sampling_rate_mult x 251) cycles. 77 * @wait_num_reports: Minimum number of EU stall data reports to unblock poll(). 78 * @gt: GT on which EU stall data will be captured. 79 */ 80 struct eu_stall_open_properties { 81 int sampling_rate_mult; 82 int wait_num_reports; 83 struct xe_gt *gt; 84 }; 85 86 /* 87 * EU stall data format for PVC 88 */ 89 struct xe_eu_stall_data_pvc { 90 __u64 ip_addr:29; /* Bits 0 to 28 */ 91 __u64 active_count:8; /* Bits 29 to 36 */ 92 __u64 other_count:8; /* Bits 37 to 44 */ 93 __u64 control_count:8; /* Bits 45 to 52 */ 94 __u64 pipestall_count:8; /* Bits 53 to 60 */ 95 __u64 send_count:8; /* Bits 61 to 68 */ 96 __u64 dist_acc_count:8; /* Bits 69 to 76 */ 97 __u64 sbid_count:8; /* Bits 77 to 84 */ 98 __u64 sync_count:8; /* Bits 85 to 92 */ 99 __u64 inst_fetch_count:8; /* Bits 93 to 100 */ 100 __u64 unused_bits:27; 101 __u64 unused[6]; 102 } __packed; 103 104 /* 105 * EU stall data format for Xe2 arch GPUs (LNL, BMG). 106 */ 107 struct xe_eu_stall_data_xe2 { 108 __u64 ip_addr:29; /* Bits 0 to 28 */ 109 __u64 tdr_count:8; /* Bits 29 to 36 */ 110 __u64 other_count:8; /* Bits 37 to 44 */ 111 __u64 control_count:8; /* Bits 45 to 52 */ 112 __u64 pipestall_count:8; /* Bits 53 to 60 */ 113 __u64 send_count:8; /* Bits 61 to 68 */ 114 __u64 dist_acc_count:8; /* Bits 69 to 76 */ 115 __u64 sbid_count:8; /* Bits 77 to 84 */ 116 __u64 sync_count:8; /* Bits 85 to 92 */ 117 __u64 inst_fetch_count:8; /* Bits 93 to 100 */ 118 __u64 active_count:8; /* Bits 101 to 108 */ 119 __u64 ex_id:3; /* Bits 109 to 111 */ 120 __u64 end_flag:1; /* Bit 112 */ 121 __u64 unused_bits:15; 122 __u64 unused[6]; 123 } __packed; 124 125 const u64 eu_stall_sampling_rates[] = {251, 251 * 2, 251 * 3, 251 * 4, 251 * 5, 251 * 6, 251 * 7}; 126 127 /** 128 * xe_eu_stall_get_sampling_rates - get EU stall sampling rates information. 129 * 130 * @num_rates: Pointer to a u32 to return the number of sampling rates. 131 * @rates: double u64 pointer to point to an array of sampling rates. 132 * 133 * Stores the number of sampling rates and pointer to the array of 134 * sampling rates in the input pointers. 135 * 136 * Returns: Size of the EU stall sampling rates array. 137 */ 138 size_t xe_eu_stall_get_sampling_rates(u32 *num_rates, const u64 **rates) 139 { 140 *num_rates = ARRAY_SIZE(eu_stall_sampling_rates); 141 *rates = eu_stall_sampling_rates; 142 143 return sizeof(eu_stall_sampling_rates); 144 } 145 146 /** 147 * xe_eu_stall_get_per_xecore_buf_size - get per XeCore buffer size. 148 * 149 * Returns: The per XeCore buffer size used to allocate the per GT 150 * EU stall data buffer. 151 */ 152 size_t xe_eu_stall_get_per_xecore_buf_size(void) 153 { 154 return per_xecore_buf_size; 155 } 156 157 /** 158 * xe_eu_stall_data_record_size - get EU stall data record size. 159 * 160 * @xe: Pointer to a Xe device. 161 * 162 * Returns: EU stall data record size. 163 */ 164 size_t xe_eu_stall_data_record_size(struct xe_device *xe) 165 { 166 size_t record_size = 0; 167 168 if (xe->info.platform == XE_PVC) 169 record_size = sizeof(struct xe_eu_stall_data_pvc); 170 else if (GRAPHICS_VER(xe) >= 20) 171 record_size = sizeof(struct xe_eu_stall_data_xe2); 172 173 xe_assert(xe, is_power_of_2(record_size)); 174 175 return record_size; 176 } 177 178 /** 179 * num_data_rows - Return the number of EU stall data rows of 64B each 180 * for a given data size. 181 * 182 * @data_size: EU stall data size 183 */ 184 static u32 num_data_rows(u32 data_size) 185 { 186 return data_size >> 6; 187 } 188 189 static void xe_eu_stall_fini(void *arg) 190 { 191 struct xe_gt *gt = arg; 192 193 destroy_workqueue(gt->eu_stall->buf_ptr_poll_wq); 194 mutex_destroy(>->eu_stall->stream_lock); 195 kfree(gt->eu_stall); 196 } 197 198 /** 199 * xe_eu_stall_init() - Allocate and initialize GT level EU stall data 200 * structure xe_eu_stall_gt within struct xe_gt. 201 * 202 * @gt: GT being initialized. 203 * 204 * Returns: zero on success or a negative error code. 205 */ 206 int xe_eu_stall_init(struct xe_gt *gt) 207 { 208 struct xe_device *xe = gt_to_xe(gt); 209 int ret; 210 211 gt->eu_stall = kzalloc(sizeof(*gt->eu_stall), GFP_KERNEL); 212 if (!gt->eu_stall) { 213 ret = -ENOMEM; 214 goto exit; 215 } 216 217 mutex_init(>->eu_stall->stream_lock); 218 219 gt->eu_stall->buf_ptr_poll_wq = alloc_ordered_workqueue("xe_eu_stall", 0); 220 if (!gt->eu_stall->buf_ptr_poll_wq) { 221 ret = -ENOMEM; 222 goto exit_free; 223 } 224 225 return devm_add_action_or_reset(xe->drm.dev, xe_eu_stall_fini, gt); 226 exit_free: 227 mutex_destroy(>->eu_stall->stream_lock); 228 kfree(gt->eu_stall); 229 exit: 230 return ret; 231 } 232 233 static int set_prop_eu_stall_sampling_rate(struct xe_device *xe, u64 value, 234 struct eu_stall_open_properties *props) 235 { 236 value = div_u64(value, 251); 237 if (value == 0 || value > 7) { 238 drm_dbg(&xe->drm, "Invalid EU stall sampling rate %llu\n", value); 239 return -EINVAL; 240 } 241 props->sampling_rate_mult = value; 242 return 0; 243 } 244 245 static int set_prop_eu_stall_wait_num_reports(struct xe_device *xe, u64 value, 246 struct eu_stall_open_properties *props) 247 { 248 props->wait_num_reports = value; 249 250 return 0; 251 } 252 253 static int set_prop_eu_stall_gt_id(struct xe_device *xe, u64 value, 254 struct eu_stall_open_properties *props) 255 { 256 if (value >= xe->info.gt_count) { 257 drm_dbg(&xe->drm, "Invalid GT ID %llu for EU stall sampling\n", value); 258 return -EINVAL; 259 } 260 props->gt = xe_device_get_gt(xe, value); 261 return 0; 262 } 263 264 typedef int (*set_eu_stall_property_fn)(struct xe_device *xe, u64 value, 265 struct eu_stall_open_properties *props); 266 267 static const set_eu_stall_property_fn xe_set_eu_stall_property_funcs[] = { 268 [DRM_XE_EU_STALL_PROP_SAMPLE_RATE] = set_prop_eu_stall_sampling_rate, 269 [DRM_XE_EU_STALL_PROP_WAIT_NUM_REPORTS] = set_prop_eu_stall_wait_num_reports, 270 [DRM_XE_EU_STALL_PROP_GT_ID] = set_prop_eu_stall_gt_id, 271 }; 272 273 static int xe_eu_stall_user_ext_set_property(struct xe_device *xe, u64 extension, 274 struct eu_stall_open_properties *props) 275 { 276 u64 __user *address = u64_to_user_ptr(extension); 277 struct drm_xe_ext_set_property ext; 278 int err; 279 u32 idx; 280 281 err = __copy_from_user(&ext, address, sizeof(ext)); 282 if (XE_IOCTL_DBG(xe, err)) 283 return -EFAULT; 284 285 if (XE_IOCTL_DBG(xe, ext.property >= ARRAY_SIZE(xe_set_eu_stall_property_funcs)) || 286 XE_IOCTL_DBG(xe, ext.pad)) 287 return -EINVAL; 288 289 idx = array_index_nospec(ext.property, ARRAY_SIZE(xe_set_eu_stall_property_funcs)); 290 return xe_set_eu_stall_property_funcs[idx](xe, ext.value, props); 291 } 292 293 typedef int (*xe_eu_stall_user_extension_fn)(struct xe_device *xe, u64 extension, 294 struct eu_stall_open_properties *props); 295 static const xe_eu_stall_user_extension_fn xe_eu_stall_user_extension_funcs[] = { 296 [DRM_XE_EU_STALL_EXTENSION_SET_PROPERTY] = xe_eu_stall_user_ext_set_property, 297 }; 298 299 #define MAX_USER_EXTENSIONS 5 300 static int xe_eu_stall_user_extensions(struct xe_device *xe, u64 extension, 301 int ext_number, struct eu_stall_open_properties *props) 302 { 303 u64 __user *address = u64_to_user_ptr(extension); 304 struct drm_xe_user_extension ext; 305 int err; 306 u32 idx; 307 308 if (XE_IOCTL_DBG(xe, ext_number >= MAX_USER_EXTENSIONS)) 309 return -E2BIG; 310 311 err = __copy_from_user(&ext, address, sizeof(ext)); 312 if (XE_IOCTL_DBG(xe, err)) 313 return -EFAULT; 314 315 if (XE_IOCTL_DBG(xe, ext.pad) || 316 XE_IOCTL_DBG(xe, ext.name >= ARRAY_SIZE(xe_eu_stall_user_extension_funcs))) 317 return -EINVAL; 318 319 idx = array_index_nospec(ext.name, ARRAY_SIZE(xe_eu_stall_user_extension_funcs)); 320 err = xe_eu_stall_user_extension_funcs[idx](xe, extension, props); 321 if (XE_IOCTL_DBG(xe, err)) 322 return err; 323 324 if (ext.next_extension) 325 return xe_eu_stall_user_extensions(xe, ext.next_extension, ++ext_number, props); 326 327 return 0; 328 } 329 330 /** 331 * buf_data_size - Calculate the number of bytes in a circular buffer 332 * given the read and write pointers and the size of 333 * the buffer. 334 * 335 * @buf_size: Size of the circular buffer 336 * @read_ptr: Read pointer with an additional overflow bit 337 * @write_ptr: Write pointer with an additional overflow bit 338 * 339 * Since the read and write pointers have an additional overflow bit, 340 * this function calculates the offsets from the pointers and use the 341 * offsets to calculate the data size in the buffer. 342 * 343 * Returns: number of bytes of data in the buffer 344 */ 345 static u32 buf_data_size(size_t buf_size, u32 read_ptr, u32 write_ptr) 346 { 347 u32 read_offset, write_offset, size = 0; 348 349 if (read_ptr == write_ptr) 350 goto exit; 351 352 read_offset = read_ptr & (buf_size - 1); 353 write_offset = write_ptr & (buf_size - 1); 354 355 if (write_offset > read_offset) 356 size = write_offset - read_offset; 357 else 358 size = buf_size - read_offset + write_offset; 359 exit: 360 return size; 361 } 362 363 /** 364 * eu_stall_data_buf_poll - Poll for EU stall data in the buffer. 365 * 366 * @stream: xe EU stall data stream instance 367 * 368 * Returns: true if the EU stall buffer contains minimum stall data as 369 * specified by the event report count, else false. 370 */ 371 static bool eu_stall_data_buf_poll(struct xe_eu_stall_data_stream *stream) 372 { 373 u32 read_ptr, write_ptr_reg, write_ptr, total_data = 0; 374 u32 buf_size = stream->per_xecore_buf_size; 375 struct per_xecore_buf *xecore_buf; 376 struct xe_gt *gt = stream->gt; 377 bool min_data_present = false; 378 u16 group, instance; 379 unsigned int xecore; 380 381 mutex_lock(>->eu_stall->stream_lock); 382 for_each_dss_steering(xecore, gt, group, instance) { 383 xecore_buf = &stream->xecore_buf[xecore]; 384 read_ptr = xecore_buf->read; 385 write_ptr_reg = xe_gt_mcr_unicast_read(gt, XEHPC_EUSTALL_REPORT, 386 group, instance); 387 write_ptr = REG_FIELD_GET(XEHPC_EUSTALL_REPORT_WRITE_PTR_MASK, write_ptr_reg); 388 write_ptr <<= 6; 389 write_ptr &= ((buf_size << 1) - 1); 390 if (!min_data_present) { 391 total_data += buf_data_size(buf_size, read_ptr, write_ptr); 392 if (num_data_rows(total_data) >= stream->wait_num_reports) 393 min_data_present = true; 394 } 395 if (write_ptr_reg & XEHPC_EUSTALL_REPORT_OVERFLOW_DROP) 396 set_bit(xecore, stream->data_drop.mask); 397 xecore_buf->write = write_ptr; 398 } 399 mutex_unlock(>->eu_stall->stream_lock); 400 401 return min_data_present; 402 } 403 404 static void clear_dropped_eviction_line_bit(struct xe_gt *gt, u16 group, u16 instance) 405 { 406 struct xe_device *xe = gt_to_xe(gt); 407 u32 write_ptr_reg; 408 409 /* On PVC, the overflow bit has to be cleared by writing 1 to it. 410 * On Xe2 and later GPUs, the bit has to be cleared by writing 0 to it. 411 */ 412 if (GRAPHICS_VER(xe) >= 20) 413 write_ptr_reg = _MASKED_BIT_DISABLE(XEHPC_EUSTALL_REPORT_OVERFLOW_DROP); 414 else 415 write_ptr_reg = _MASKED_BIT_ENABLE(XEHPC_EUSTALL_REPORT_OVERFLOW_DROP); 416 417 xe_gt_mcr_unicast_write(gt, XEHPC_EUSTALL_REPORT, write_ptr_reg, group, instance); 418 } 419 420 static int xe_eu_stall_data_buf_read(struct xe_eu_stall_data_stream *stream, 421 char __user *buf, size_t count, 422 size_t *total_data_size, struct xe_gt *gt, 423 u16 group, u16 instance, unsigned int xecore) 424 { 425 size_t read_data_size, copy_size, buf_size; 426 u32 read_ptr_reg, read_ptr, write_ptr; 427 u8 *xecore_start_vaddr, *read_vaddr; 428 struct per_xecore_buf *xecore_buf; 429 u32 read_offset, write_offset; 430 431 /* Hardware increments the read and write pointers such that they can 432 * overflow into one additional bit. For example, a 256KB size buffer 433 * offset pointer needs 18 bits. But HW uses 19 bits for the read and 434 * write pointers. This technique avoids wasting a slot in the buffer. 435 * Read and write offsets are calculated from the pointers in order to 436 * check if the write pointer has wrapped around the array. 437 */ 438 xecore_buf = &stream->xecore_buf[xecore]; 439 xecore_start_vaddr = xecore_buf->vaddr; 440 read_ptr = xecore_buf->read; 441 write_ptr = xecore_buf->write; 442 buf_size = stream->per_xecore_buf_size; 443 444 read_data_size = buf_data_size(buf_size, read_ptr, write_ptr); 445 /* Read only the data that the user space buffer can accommodate */ 446 read_data_size = min_t(size_t, count - *total_data_size, read_data_size); 447 if (read_data_size == 0) 448 goto exit_drop; 449 450 read_offset = read_ptr & (buf_size - 1); 451 write_offset = write_ptr & (buf_size - 1); 452 read_vaddr = xecore_start_vaddr + read_offset; 453 454 if (write_offset > read_offset) { 455 if (copy_to_user(buf + *total_data_size, read_vaddr, read_data_size)) 456 return -EFAULT; 457 } else { 458 if (read_data_size >= buf_size - read_offset) 459 copy_size = buf_size - read_offset; 460 else 461 copy_size = read_data_size; 462 if (copy_to_user(buf + *total_data_size, read_vaddr, copy_size)) 463 return -EFAULT; 464 if (copy_to_user(buf + *total_data_size + copy_size, 465 xecore_start_vaddr, read_data_size - copy_size)) 466 return -EFAULT; 467 } 468 469 *total_data_size += read_data_size; 470 read_ptr += read_data_size; 471 472 /* Read pointer can overflow into one additional bit */ 473 read_ptr &= (buf_size << 1) - 1; 474 read_ptr_reg = REG_FIELD_PREP(XEHPC_EUSTALL_REPORT1_READ_PTR_MASK, (read_ptr >> 6)); 475 read_ptr_reg = _MASKED_FIELD(XEHPC_EUSTALL_REPORT1_READ_PTR_MASK, read_ptr_reg); 476 xe_gt_mcr_unicast_write(gt, XEHPC_EUSTALL_REPORT1, read_ptr_reg, group, instance); 477 xecore_buf->read = read_ptr; 478 trace_xe_eu_stall_data_read(group, instance, read_ptr, write_ptr, 479 read_data_size, *total_data_size); 480 exit_drop: 481 /* Clear drop bit (if set) after any data was read or if the buffer was empty. 482 * Drop bit can be set even if the buffer is empty as the buffer may have been emptied 483 * in the previous read() and the data drop bit was set during the previous read(). 484 */ 485 if (test_bit(xecore, stream->data_drop.mask)) { 486 clear_dropped_eviction_line_bit(gt, group, instance); 487 clear_bit(xecore, stream->data_drop.mask); 488 } 489 return 0; 490 } 491 492 /** 493 * xe_eu_stall_stream_read_locked - copy EU stall counters data from the 494 * per xecore buffers to the userspace buffer 495 * @stream: A stream opened for EU stall count metrics 496 * @file: An xe EU stall data stream file 497 * @buf: destination buffer given by userspace 498 * @count: the number of bytes userspace wants to read 499 * 500 * Returns: Number of bytes copied or a negative error code 501 * If we've successfully copied any data then reporting that takes 502 * precedence over any internal error status, so the data isn't lost. 503 */ 504 static ssize_t xe_eu_stall_stream_read_locked(struct xe_eu_stall_data_stream *stream, 505 struct file *file, char __user *buf, 506 size_t count) 507 { 508 struct xe_gt *gt = stream->gt; 509 size_t total_size = 0; 510 u16 group, instance; 511 unsigned int xecore; 512 int ret = 0; 513 514 if (bitmap_weight(stream->data_drop.mask, XE_MAX_DSS_FUSE_BITS)) { 515 if (!stream->data_drop.reported_to_user) { 516 stream->data_drop.reported_to_user = true; 517 xe_gt_dbg(gt, "EU stall data dropped in XeCores: %*pb\n", 518 XE_MAX_DSS_FUSE_BITS, stream->data_drop.mask); 519 return -EIO; 520 } 521 stream->data_drop.reported_to_user = false; 522 } 523 524 for_each_dss_steering(xecore, gt, group, instance) { 525 ret = xe_eu_stall_data_buf_read(stream, buf, count, &total_size, 526 gt, group, instance, xecore); 527 if (ret || count == total_size) 528 break; 529 } 530 return total_size ?: (ret ?: -EAGAIN); 531 } 532 533 /* 534 * Userspace must enable the EU stall stream with DRM_XE_OBSERVATION_IOCTL_ENABLE 535 * before calling read(). 536 * 537 * Returns: The number of bytes copied or a negative error code on failure. 538 * -EIO if HW drops any EU stall data when the buffer is full. 539 */ 540 static ssize_t xe_eu_stall_stream_read(struct file *file, char __user *buf, 541 size_t count, loff_t *ppos) 542 { 543 struct xe_eu_stall_data_stream *stream = file->private_data; 544 struct xe_gt *gt = stream->gt; 545 ssize_t ret, aligned_count; 546 547 aligned_count = ALIGN_DOWN(count, stream->data_record_size); 548 if (aligned_count == 0) 549 return -EINVAL; 550 551 if (!stream->enabled) { 552 xe_gt_dbg(gt, "EU stall data stream not enabled to read\n"); 553 return -EINVAL; 554 } 555 556 if (!(file->f_flags & O_NONBLOCK)) { 557 do { 558 ret = wait_event_interruptible(stream->poll_wq, stream->pollin); 559 if (ret) 560 return -EINTR; 561 562 mutex_lock(>->eu_stall->stream_lock); 563 ret = xe_eu_stall_stream_read_locked(stream, file, buf, aligned_count); 564 mutex_unlock(>->eu_stall->stream_lock); 565 } while (ret == -EAGAIN); 566 } else { 567 mutex_lock(>->eu_stall->stream_lock); 568 ret = xe_eu_stall_stream_read_locked(stream, file, buf, aligned_count); 569 mutex_unlock(>->eu_stall->stream_lock); 570 } 571 572 /* 573 * This may not work correctly if the user buffer is very small. 574 * We don't want to block the next read() when there is data in the buffer 575 * now, but couldn't be accommodated in the small user buffer. 576 */ 577 stream->pollin = false; 578 579 return ret; 580 } 581 582 static void xe_eu_stall_stream_free(struct xe_eu_stall_data_stream *stream) 583 { 584 struct xe_gt *gt = stream->gt; 585 586 gt->eu_stall->stream = NULL; 587 kfree(stream); 588 } 589 590 static void xe_eu_stall_data_buf_destroy(struct xe_eu_stall_data_stream *stream) 591 { 592 xe_bo_unpin_map_no_vm(stream->bo); 593 kfree(stream->xecore_buf); 594 } 595 596 static int xe_eu_stall_data_buf_alloc(struct xe_eu_stall_data_stream *stream, 597 u16 last_xecore) 598 { 599 struct xe_tile *tile = stream->gt->tile; 600 struct xe_bo *bo; 601 u32 size; 602 603 stream->xecore_buf = kcalloc(last_xecore, sizeof(*stream->xecore_buf), GFP_KERNEL); 604 if (!stream->xecore_buf) 605 return -ENOMEM; 606 607 size = stream->per_xecore_buf_size * last_xecore; 608 609 bo = xe_bo_create_pin_map_at_aligned(tile->xe, tile, NULL, 610 size, ~0ull, ttm_bo_type_kernel, 611 XE_BO_FLAG_SYSTEM | XE_BO_FLAG_GGTT, SZ_64); 612 if (IS_ERR(bo)) { 613 kfree(stream->xecore_buf); 614 return PTR_ERR(bo); 615 } 616 617 XE_WARN_ON(!IS_ALIGNED(xe_bo_ggtt_addr(bo), SZ_64)); 618 stream->bo = bo; 619 620 return 0; 621 } 622 623 static int xe_eu_stall_stream_enable(struct xe_eu_stall_data_stream *stream) 624 { 625 u32 write_ptr_reg, write_ptr, read_ptr_reg, reg_value; 626 struct per_xecore_buf *xecore_buf; 627 struct xe_gt *gt = stream->gt; 628 u16 group, instance; 629 unsigned int fw_ref; 630 int xecore; 631 632 /* Take runtime pm ref and forcewake to disable RC6 */ 633 xe_pm_runtime_get(gt_to_xe(gt)); 634 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_RENDER); 635 if (!xe_force_wake_ref_has_domain(fw_ref, XE_FW_RENDER)) { 636 xe_gt_err(gt, "Failed to get RENDER forcewake\n"); 637 xe_pm_runtime_put(gt_to_xe(gt)); 638 return -ETIMEDOUT; 639 } 640 641 if (XE_WA(gt, 22016596838)) 642 xe_gt_mcr_multicast_write(gt, ROW_CHICKEN2, 643 _MASKED_BIT_ENABLE(DISABLE_DOP_GATING)); 644 645 for_each_dss_steering(xecore, gt, group, instance) { 646 write_ptr_reg = xe_gt_mcr_unicast_read(gt, XEHPC_EUSTALL_REPORT, group, instance); 647 /* Clear any drop bits set and not cleared in the previous session. */ 648 if (write_ptr_reg & XEHPC_EUSTALL_REPORT_OVERFLOW_DROP) 649 clear_dropped_eviction_line_bit(gt, group, instance); 650 write_ptr = REG_FIELD_GET(XEHPC_EUSTALL_REPORT_WRITE_PTR_MASK, write_ptr_reg); 651 read_ptr_reg = REG_FIELD_PREP(XEHPC_EUSTALL_REPORT1_READ_PTR_MASK, write_ptr); 652 read_ptr_reg = _MASKED_FIELD(XEHPC_EUSTALL_REPORT1_READ_PTR_MASK, read_ptr_reg); 653 /* Initialize the read pointer to the write pointer */ 654 xe_gt_mcr_unicast_write(gt, XEHPC_EUSTALL_REPORT1, read_ptr_reg, group, instance); 655 write_ptr <<= 6; 656 write_ptr &= (stream->per_xecore_buf_size << 1) - 1; 657 xecore_buf = &stream->xecore_buf[xecore]; 658 xecore_buf->write = write_ptr; 659 xecore_buf->read = write_ptr; 660 } 661 stream->data_drop.reported_to_user = false; 662 bitmap_zero(stream->data_drop.mask, XE_MAX_DSS_FUSE_BITS); 663 664 reg_value = _MASKED_FIELD(EUSTALL_MOCS | EUSTALL_SAMPLE_RATE, 665 REG_FIELD_PREP(EUSTALL_MOCS, gt->mocs.uc_index << 1) | 666 REG_FIELD_PREP(EUSTALL_SAMPLE_RATE, 667 stream->sampling_rate_mult)); 668 xe_gt_mcr_multicast_write(gt, XEHPC_EUSTALL_CTRL, reg_value); 669 /* GGTT addresses can never be > 32 bits */ 670 xe_gt_mcr_multicast_write(gt, XEHPC_EUSTALL_BASE_UPPER, 0); 671 reg_value = xe_bo_ggtt_addr(stream->bo); 672 reg_value |= REG_FIELD_PREP(XEHPC_EUSTALL_BASE_XECORE_BUF_SZ, 673 stream->per_xecore_buf_size / SZ_256K); 674 reg_value |= XEHPC_EUSTALL_BASE_ENABLE_SAMPLING; 675 xe_gt_mcr_multicast_write(gt, XEHPC_EUSTALL_BASE, reg_value); 676 677 return 0; 678 } 679 680 static void eu_stall_data_buf_poll_work_fn(struct work_struct *work) 681 { 682 struct xe_eu_stall_data_stream *stream = 683 container_of(work, typeof(*stream), buf_poll_work.work); 684 struct xe_gt *gt = stream->gt; 685 686 if (eu_stall_data_buf_poll(stream)) { 687 stream->pollin = true; 688 wake_up(&stream->poll_wq); 689 } 690 queue_delayed_work(gt->eu_stall->buf_ptr_poll_wq, 691 &stream->buf_poll_work, 692 msecs_to_jiffies(POLL_PERIOD_MS)); 693 } 694 695 static int xe_eu_stall_stream_init(struct xe_eu_stall_data_stream *stream, 696 struct eu_stall_open_properties *props) 697 { 698 unsigned int max_wait_num_reports, xecore, last_xecore, num_xecores; 699 struct per_xecore_buf *xecore_buf; 700 struct xe_gt *gt = stream->gt; 701 xe_dss_mask_t all_xecores; 702 u16 group, instance; 703 u32 vaddr_offset; 704 int ret; 705 706 bitmap_or(all_xecores, gt->fuse_topo.g_dss_mask, gt->fuse_topo.c_dss_mask, 707 XE_MAX_DSS_FUSE_BITS); 708 num_xecores = bitmap_weight(all_xecores, XE_MAX_DSS_FUSE_BITS); 709 last_xecore = xe_gt_topology_mask_last_dss(all_xecores) + 1; 710 711 max_wait_num_reports = num_data_rows(per_xecore_buf_size * num_xecores); 712 if (props->wait_num_reports == 0 || props->wait_num_reports > max_wait_num_reports) { 713 xe_gt_dbg(gt, "Invalid EU stall event report count %u\n", 714 props->wait_num_reports); 715 xe_gt_dbg(gt, "Minimum event report count is 1, maximum is %u\n", 716 max_wait_num_reports); 717 return -EINVAL; 718 } 719 720 init_waitqueue_head(&stream->poll_wq); 721 INIT_DELAYED_WORK(&stream->buf_poll_work, eu_stall_data_buf_poll_work_fn); 722 stream->per_xecore_buf_size = per_xecore_buf_size; 723 stream->sampling_rate_mult = props->sampling_rate_mult; 724 stream->wait_num_reports = props->wait_num_reports; 725 stream->data_record_size = xe_eu_stall_data_record_size(gt_to_xe(gt)); 726 727 ret = xe_eu_stall_data_buf_alloc(stream, last_xecore); 728 if (ret) 729 return ret; 730 731 for_each_dss_steering(xecore, gt, group, instance) { 732 xecore_buf = &stream->xecore_buf[xecore]; 733 vaddr_offset = xecore * stream->per_xecore_buf_size; 734 xecore_buf->vaddr = stream->bo->vmap.vaddr + vaddr_offset; 735 } 736 return 0; 737 } 738 739 static __poll_t xe_eu_stall_stream_poll_locked(struct xe_eu_stall_data_stream *stream, 740 struct file *file, poll_table *wait) 741 { 742 __poll_t events = 0; 743 744 poll_wait(file, &stream->poll_wq, wait); 745 746 if (stream->pollin) 747 events |= EPOLLIN; 748 749 return events; 750 } 751 752 static __poll_t xe_eu_stall_stream_poll(struct file *file, poll_table *wait) 753 { 754 struct xe_eu_stall_data_stream *stream = file->private_data; 755 struct xe_gt *gt = stream->gt; 756 __poll_t ret; 757 758 mutex_lock(>->eu_stall->stream_lock); 759 ret = xe_eu_stall_stream_poll_locked(stream, file, wait); 760 mutex_unlock(>->eu_stall->stream_lock); 761 762 return ret; 763 } 764 765 static int xe_eu_stall_enable_locked(struct xe_eu_stall_data_stream *stream) 766 { 767 struct xe_gt *gt = stream->gt; 768 int ret = 0; 769 770 if (stream->enabled) 771 return ret; 772 773 stream->enabled = true; 774 775 ret = xe_eu_stall_stream_enable(stream); 776 777 queue_delayed_work(gt->eu_stall->buf_ptr_poll_wq, 778 &stream->buf_poll_work, 779 msecs_to_jiffies(POLL_PERIOD_MS)); 780 return ret; 781 } 782 783 static int xe_eu_stall_disable_locked(struct xe_eu_stall_data_stream *stream) 784 { 785 struct xe_gt *gt = stream->gt; 786 787 if (!stream->enabled) 788 return 0; 789 790 stream->enabled = false; 791 792 xe_gt_mcr_multicast_write(gt, XEHPC_EUSTALL_BASE, 0); 793 794 cancel_delayed_work_sync(&stream->buf_poll_work); 795 796 if (XE_WA(gt, 22016596838)) 797 xe_gt_mcr_multicast_write(gt, ROW_CHICKEN2, 798 _MASKED_BIT_DISABLE(DISABLE_DOP_GATING)); 799 800 xe_force_wake_put(gt_to_fw(gt), XE_FW_RENDER); 801 xe_pm_runtime_put(gt_to_xe(gt)); 802 803 return 0; 804 } 805 806 static long xe_eu_stall_stream_ioctl_locked(struct xe_eu_stall_data_stream *stream, 807 unsigned int cmd, unsigned long arg) 808 { 809 switch (cmd) { 810 case DRM_XE_OBSERVATION_IOCTL_ENABLE: 811 return xe_eu_stall_enable_locked(stream); 812 case DRM_XE_OBSERVATION_IOCTL_DISABLE: 813 return xe_eu_stall_disable_locked(stream); 814 } 815 816 return -EINVAL; 817 } 818 819 static long xe_eu_stall_stream_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 820 { 821 struct xe_eu_stall_data_stream *stream = file->private_data; 822 struct xe_gt *gt = stream->gt; 823 long ret; 824 825 mutex_lock(>->eu_stall->stream_lock); 826 ret = xe_eu_stall_stream_ioctl_locked(stream, cmd, arg); 827 mutex_unlock(>->eu_stall->stream_lock); 828 829 return ret; 830 } 831 832 static int xe_eu_stall_stream_close(struct inode *inode, struct file *file) 833 { 834 struct xe_eu_stall_data_stream *stream = file->private_data; 835 struct xe_gt *gt = stream->gt; 836 837 drm_dev_put(>->tile->xe->drm); 838 839 mutex_lock(>->eu_stall->stream_lock); 840 xe_eu_stall_disable_locked(stream); 841 xe_eu_stall_data_buf_destroy(stream); 842 xe_eu_stall_stream_free(stream); 843 mutex_unlock(>->eu_stall->stream_lock); 844 845 return 0; 846 } 847 848 static const struct file_operations fops_eu_stall = { 849 .owner = THIS_MODULE, 850 .llseek = noop_llseek, 851 .release = xe_eu_stall_stream_close, 852 .poll = xe_eu_stall_stream_poll, 853 .read = xe_eu_stall_stream_read, 854 .unlocked_ioctl = xe_eu_stall_stream_ioctl, 855 .compat_ioctl = xe_eu_stall_stream_ioctl, 856 }; 857 858 static int xe_eu_stall_stream_open_locked(struct drm_device *dev, 859 struct eu_stall_open_properties *props, 860 struct drm_file *file) 861 { 862 struct xe_eu_stall_data_stream *stream; 863 struct xe_gt *gt = props->gt; 864 unsigned long f_flags = 0; 865 int ret, stream_fd; 866 867 /* Only one session can be active at any time */ 868 if (gt->eu_stall->stream) { 869 xe_gt_dbg(gt, "EU stall sampling session already active\n"); 870 return -EBUSY; 871 } 872 873 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 874 if (!stream) 875 return -ENOMEM; 876 877 gt->eu_stall->stream = stream; 878 stream->gt = gt; 879 880 ret = xe_eu_stall_stream_init(stream, props); 881 if (ret) { 882 xe_gt_dbg(gt, "EU stall stream init failed : %d\n", ret); 883 goto err_free; 884 } 885 886 stream_fd = anon_inode_getfd("[xe_eu_stall]", &fops_eu_stall, stream, f_flags); 887 if (stream_fd < 0) { 888 ret = stream_fd; 889 xe_gt_dbg(gt, "EU stall inode get fd failed : %d\n", ret); 890 goto err_destroy; 891 } 892 893 /* Take a reference on the driver that will be kept with stream_fd 894 * until its release. 895 */ 896 drm_dev_get(>->tile->xe->drm); 897 898 return stream_fd; 899 900 err_destroy: 901 xe_eu_stall_data_buf_destroy(stream); 902 err_free: 903 xe_eu_stall_stream_free(stream); 904 return ret; 905 } 906 907 /** 908 * xe_eu_stall_stream_open - Open a xe EU stall data stream fd 909 * 910 * @dev: DRM device pointer 911 * @data: pointer to first struct @drm_xe_ext_set_property in 912 * the chain of input properties from the user space. 913 * @file: DRM file pointer 914 * 915 * This function opens a EU stall data stream with input properties from 916 * the user space. 917 * 918 * Returns: EU stall data stream fd on success or a negative error code. 919 */ 920 int xe_eu_stall_stream_open(struct drm_device *dev, u64 data, struct drm_file *file) 921 { 922 struct xe_device *xe = to_xe_device(dev); 923 struct eu_stall_open_properties props = {}; 924 int ret; 925 926 if (!xe_eu_stall_supported_on_platform(xe)) { 927 drm_dbg(&xe->drm, "EU stall monitoring is not supported on this platform\n"); 928 return -ENODEV; 929 } 930 931 if (xe_observation_paranoid && !perfmon_capable()) { 932 drm_dbg(&xe->drm, "Insufficient privileges for EU stall monitoring\n"); 933 return -EACCES; 934 } 935 936 /* Initialize and set default values */ 937 props.wait_num_reports = 1; 938 props.sampling_rate_mult = 4; 939 940 ret = xe_eu_stall_user_extensions(xe, data, 0, &props); 941 if (ret) 942 return ret; 943 944 if (!props.gt) { 945 drm_dbg(&xe->drm, "GT ID not provided for EU stall sampling\n"); 946 return -EINVAL; 947 } 948 949 mutex_lock(&props.gt->eu_stall->stream_lock); 950 ret = xe_eu_stall_stream_open_locked(dev, &props, file); 951 mutex_unlock(&props.gt->eu_stall->stream_lock); 952 953 return ret; 954 } 955