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