1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2025 Intel Corporation 4 */ 5 6 #include <drm/drm_managed.h> 7 8 #include "abi/guc_actions_abi.h" 9 #include "regs/xe_gt_regs.h" 10 11 #include "xe_bo.h" 12 #include "xe_force_wake.h" 13 #include "xe_gt_printk.h" 14 #include "xe_guc.h" 15 #include "xe_guc_engine_activity.h" 16 #include "xe_guc_ct.h" 17 #include "xe_hw_engine.h" 18 #include "xe_map.h" 19 #include "xe_mmio.h" 20 #include "xe_sriov_pf_helpers.h" 21 #include "xe_trace_guc.h" 22 23 #define TOTAL_QUANTA 0x8000 24 25 static struct iosys_map engine_activity_map(struct xe_guc *guc, struct xe_hw_engine *hwe, 26 unsigned int index) 27 { 28 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 29 struct engine_activity_buffer *buffer; 30 u16 guc_class = xe_engine_class_to_guc_class(hwe->class); 31 size_t offset; 32 33 if (engine_activity->num_functions) { 34 buffer = &engine_activity->function_buffer; 35 offset = sizeof(struct guc_engine_activity_data) * index; 36 } else { 37 buffer = &engine_activity->device_buffer; 38 offset = 0; 39 } 40 41 offset += offsetof(struct guc_engine_activity_data, 42 engine_activity[guc_class][hwe->logical_instance]); 43 44 return IOSYS_MAP_INIT_OFFSET(&buffer->activity_bo->vmap, offset); 45 } 46 47 static struct iosys_map engine_metadata_map(struct xe_guc *guc, 48 unsigned int index) 49 { 50 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 51 struct engine_activity_buffer *buffer; 52 size_t offset; 53 54 if (engine_activity->num_functions) { 55 buffer = &engine_activity->function_buffer; 56 offset = sizeof(struct guc_engine_activity_metadata) * index; 57 } else { 58 buffer = &engine_activity->device_buffer; 59 offset = 0; 60 } 61 62 return IOSYS_MAP_INIT_OFFSET(&buffer->metadata_bo->vmap, offset); 63 } 64 65 static int allocate_engine_activity_group(struct xe_guc *guc) 66 { 67 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 68 struct xe_device *xe = guc_to_xe(guc); 69 u32 num_activity_group; 70 71 /* 72 * An additional activity group is allocated for PF 73 */ 74 num_activity_group = IS_SRIOV_PF(xe) ? xe_sriov_pf_get_totalvfs(xe) + 1 : 1; 75 76 engine_activity->eag = drmm_kcalloc(&xe->drm, num_activity_group, 77 sizeof(struct engine_activity_group), GFP_KERNEL); 78 79 if (!engine_activity->eag) 80 return -ENOMEM; 81 82 engine_activity->num_activity_group = num_activity_group; 83 84 return 0; 85 } 86 87 static int allocate_engine_activity_buffers(struct xe_guc *guc, 88 struct engine_activity_buffer *buffer, 89 int count) 90 { 91 u32 metadata_size = sizeof(struct guc_engine_activity_metadata) * count; 92 u32 size = sizeof(struct guc_engine_activity_data) * count; 93 struct xe_gt *gt = guc_to_gt(guc); 94 struct xe_tile *tile = gt_to_tile(gt); 95 struct xe_bo *bo, *metadata_bo; 96 97 metadata_bo = xe_bo_create_pin_map(gt_to_xe(gt), tile, NULL, PAGE_ALIGN(metadata_size), 98 ttm_bo_type_kernel, XE_BO_FLAG_SYSTEM | 99 XE_BO_FLAG_GGTT | XE_BO_FLAG_GGTT_INVALIDATE); 100 101 if (IS_ERR(metadata_bo)) 102 return PTR_ERR(metadata_bo); 103 104 bo = xe_bo_create_pin_map(gt_to_xe(gt), tile, NULL, PAGE_ALIGN(size), 105 ttm_bo_type_kernel, XE_BO_FLAG_VRAM_IF_DGFX(tile) | 106 XE_BO_FLAG_GGTT | XE_BO_FLAG_GGTT_INVALIDATE); 107 108 if (IS_ERR(bo)) { 109 xe_bo_unpin_map_no_vm(metadata_bo); 110 return PTR_ERR(bo); 111 } 112 113 buffer->metadata_bo = metadata_bo; 114 buffer->activity_bo = bo; 115 return 0; 116 } 117 118 static void free_engine_activity_buffers(struct engine_activity_buffer *buffer) 119 { 120 xe_bo_unpin_map_no_vm(buffer->metadata_bo); 121 xe_bo_unpin_map_no_vm(buffer->activity_bo); 122 } 123 124 static bool is_engine_activity_supported(struct xe_guc *guc) 125 { 126 struct xe_uc_fw_version *version = &guc->fw.versions.found[XE_UC_FW_VER_COMPATIBILITY]; 127 struct xe_uc_fw_version required = { 1, 14, 1 }; 128 struct xe_gt *gt = guc_to_gt(guc); 129 130 if (IS_SRIOV_VF(gt_to_xe(gt))) { 131 xe_gt_info(gt, "engine activity stats not supported on VFs\n"); 132 return false; 133 } 134 135 /* engine activity stats is supported from GuC interface version (1.14.1) */ 136 if (GUC_SUBMIT_VER(guc) < MAKE_GUC_VER_STRUCT(required)) { 137 xe_gt_info(gt, 138 "engine activity stats unsupported in GuC interface v%u.%u.%u, need v%u.%u.%u or higher\n", 139 version->major, version->minor, version->patch, required.major, 140 required.minor, required.patch); 141 return false; 142 } 143 144 return true; 145 } 146 147 static struct engine_activity *hw_engine_to_engine_activity(struct xe_hw_engine *hwe, 148 unsigned int index) 149 { 150 struct xe_guc *guc = &hwe->gt->uc.guc; 151 struct engine_activity_group *eag = &guc->engine_activity.eag[index]; 152 u16 guc_class = xe_engine_class_to_guc_class(hwe->class); 153 154 return &eag->engine[guc_class][hwe->logical_instance]; 155 } 156 157 static u64 cpu_ns_to_guc_tsc_tick(ktime_t ns, u32 freq) 158 { 159 return mul_u64_u32_div(ns, freq, NSEC_PER_SEC); 160 } 161 162 #define read_engine_activity_record(xe_, map_, field_) \ 163 xe_map_rd_field(xe_, map_, 0, struct guc_engine_activity, field_) 164 165 #define read_metadata_record(xe_, map_, field_) \ 166 xe_map_rd_field(xe_, map_, 0, struct guc_engine_activity_metadata, field_) 167 168 static u64 get_engine_active_ticks(struct xe_guc *guc, struct xe_hw_engine *hwe, 169 unsigned int index) 170 { 171 struct engine_activity *ea = hw_engine_to_engine_activity(hwe, index); 172 struct guc_engine_activity *cached_activity = &ea->activity; 173 struct guc_engine_activity_metadata *cached_metadata = &ea->metadata; 174 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 175 struct iosys_map activity_map, metadata_map; 176 struct xe_device *xe = guc_to_xe(guc); 177 struct xe_gt *gt = guc_to_gt(guc); 178 u32 last_update_tick, global_change_num; 179 u64 active_ticks, gpm_ts; 180 u16 change_num; 181 182 activity_map = engine_activity_map(guc, hwe, index); 183 metadata_map = engine_metadata_map(guc, index); 184 global_change_num = read_metadata_record(xe, &metadata_map, global_change_num); 185 186 /* GuC has not initialized activity data yet, return 0 */ 187 if (!global_change_num) 188 goto update; 189 190 if (global_change_num == cached_metadata->global_change_num) 191 goto update; 192 193 cached_metadata->global_change_num = global_change_num; 194 change_num = read_engine_activity_record(xe, &activity_map, change_num); 195 196 if (!change_num || change_num == cached_activity->change_num) 197 goto update; 198 199 /* read engine activity values */ 200 last_update_tick = read_engine_activity_record(xe, &activity_map, last_update_tick); 201 active_ticks = read_engine_activity_record(xe, &activity_map, active_ticks); 202 203 /* activity calculations */ 204 ea->running = !!last_update_tick; 205 ea->total += active_ticks - cached_activity->active_ticks; 206 ea->active = 0; 207 208 /* cache the counter */ 209 cached_activity->change_num = change_num; 210 cached_activity->last_update_tick = last_update_tick; 211 cached_activity->active_ticks = active_ticks; 212 213 update: 214 if (ea->running) { 215 gpm_ts = xe_mmio_read64_2x32(>->mmio, MISC_STATUS_0) >> 216 engine_activity->gpm_timestamp_shift; 217 ea->active = lower_32_bits(gpm_ts) - cached_activity->last_update_tick; 218 } 219 220 trace_xe_guc_engine_activity(xe, ea, hwe->name, hwe->instance); 221 222 return ea->total + ea->active; 223 } 224 225 static u64 get_engine_total_ticks(struct xe_guc *guc, struct xe_hw_engine *hwe, unsigned int index) 226 { 227 struct engine_activity *ea = hw_engine_to_engine_activity(hwe, index); 228 struct guc_engine_activity_metadata *cached_metadata = &ea->metadata; 229 struct guc_engine_activity *cached_activity = &ea->activity; 230 struct iosys_map activity_map, metadata_map; 231 struct xe_device *xe = guc_to_xe(guc); 232 ktime_t now, cpu_delta; 233 u64 numerator; 234 u16 quanta_ratio; 235 236 activity_map = engine_activity_map(guc, hwe, index); 237 metadata_map = engine_metadata_map(guc, index); 238 239 if (!cached_metadata->guc_tsc_frequency_hz) 240 cached_metadata->guc_tsc_frequency_hz = read_metadata_record(xe, &metadata_map, 241 guc_tsc_frequency_hz); 242 243 quanta_ratio = read_engine_activity_record(xe, &activity_map, quanta_ratio); 244 cached_activity->quanta_ratio = quanta_ratio; 245 246 /* Total ticks calculations */ 247 now = ktime_get(); 248 cpu_delta = now - ea->last_cpu_ts; 249 ea->last_cpu_ts = now; 250 numerator = (ea->quanta_remainder_ns + cpu_delta) * cached_activity->quanta_ratio; 251 ea->quanta_ns += numerator / TOTAL_QUANTA; 252 ea->quanta_remainder_ns = numerator % TOTAL_QUANTA; 253 ea->quanta = cpu_ns_to_guc_tsc_tick(ea->quanta_ns, cached_metadata->guc_tsc_frequency_hz); 254 255 trace_xe_guc_engine_activity(xe, ea, hwe->name, hwe->instance); 256 257 return ea->quanta; 258 } 259 260 static int enable_engine_activity_stats(struct xe_guc *guc) 261 { 262 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 263 struct engine_activity_buffer *buffer = &engine_activity->device_buffer; 264 u32 action[] = { 265 XE_GUC_ACTION_SET_DEVICE_ENGINE_ACTIVITY_BUFFER, 266 xe_bo_ggtt_addr(buffer->metadata_bo), 267 0, 268 xe_bo_ggtt_addr(buffer->activity_bo), 269 0, 270 }; 271 272 /* Blocking here to ensure the buffers are ready before reading them */ 273 return xe_guc_ct_send_block(&guc->ct, action, ARRAY_SIZE(action)); 274 } 275 276 static int enable_function_engine_activity_stats(struct xe_guc *guc, bool enable) 277 { 278 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 279 u32 metadata_ggtt_addr = 0, ggtt_addr = 0, num_functions = 0; 280 struct engine_activity_buffer *buffer = &engine_activity->function_buffer; 281 u32 action[6]; 282 int len = 0; 283 284 if (enable) { 285 metadata_ggtt_addr = xe_bo_ggtt_addr(buffer->metadata_bo); 286 ggtt_addr = xe_bo_ggtt_addr(buffer->activity_bo); 287 num_functions = engine_activity->num_functions; 288 } 289 290 action[len++] = XE_GUC_ACTION_SET_FUNCTION_ENGINE_ACTIVITY_BUFFER; 291 action[len++] = num_functions; 292 action[len++] = metadata_ggtt_addr; 293 action[len++] = 0; 294 action[len++] = ggtt_addr; 295 action[len++] = 0; 296 297 /* Blocking here to ensure the buffers are ready before reading them */ 298 return xe_guc_ct_send_block(&guc->ct, action, ARRAY_SIZE(action)); 299 } 300 301 static void engine_activity_set_cpu_ts(struct xe_guc *guc, unsigned int index) 302 { 303 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 304 struct engine_activity_group *eag = &engine_activity->eag[index]; 305 int i, j; 306 307 xe_gt_assert(guc_to_gt(guc), index < engine_activity->num_activity_group); 308 309 for (i = 0; i < GUC_MAX_ENGINE_CLASSES; i++) 310 for (j = 0; j < GUC_MAX_INSTANCES_PER_CLASS; j++) 311 eag->engine[i][j].last_cpu_ts = ktime_get(); 312 } 313 314 static u32 gpm_timestamp_shift(struct xe_gt *gt) 315 { 316 u32 reg; 317 318 reg = xe_mmio_read32(>->mmio, RPM_CONFIG0); 319 320 return 3 - REG_FIELD_GET(RPM_CONFIG0_CTC_SHIFT_PARAMETER_MASK, reg); 321 } 322 323 static bool is_function_valid(struct xe_guc *guc, unsigned int fn_id) 324 { 325 struct xe_device *xe = guc_to_xe(guc); 326 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 327 328 if (!IS_SRIOV_PF(xe) && fn_id) 329 return false; 330 331 if (engine_activity->num_functions && fn_id >= engine_activity->num_functions) 332 return false; 333 334 return true; 335 } 336 337 static int engine_activity_disable_function_stats(struct xe_guc *guc) 338 { 339 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 340 struct engine_activity_buffer *buffer = &engine_activity->function_buffer; 341 int ret; 342 343 if (!engine_activity->num_functions) 344 return 0; 345 346 ret = enable_function_engine_activity_stats(guc, false); 347 if (ret) 348 return ret; 349 350 free_engine_activity_buffers(buffer); 351 engine_activity->num_functions = 0; 352 353 return 0; 354 } 355 356 static int engine_activity_enable_function_stats(struct xe_guc *guc, int num_vfs) 357 { 358 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 359 struct engine_activity_buffer *buffer = &engine_activity->function_buffer; 360 int ret, i; 361 362 if (!num_vfs) 363 return 0; 364 365 /* This includes 1 PF and num_vfs */ 366 engine_activity->num_functions = num_vfs + 1; 367 368 ret = allocate_engine_activity_buffers(guc, buffer, engine_activity->num_functions); 369 if (ret) 370 return ret; 371 372 ret = enable_function_engine_activity_stats(guc, true); 373 if (ret) { 374 free_engine_activity_buffers(buffer); 375 engine_activity->num_functions = 0; 376 return ret; 377 } 378 379 /* skip PF as it was already setup */ 380 for (i = 1; i < engine_activity->num_functions; i++) 381 engine_activity_set_cpu_ts(guc, i); 382 383 return 0; 384 } 385 386 /** 387 * xe_guc_engine_activity_active_ticks - Get engine active ticks 388 * @guc: The GuC object 389 * @hwe: The hw_engine object 390 * @fn_id: function id to report on 391 * 392 * Return: accumulated ticks @hwe was active since engine activity stats were enabled. 393 */ 394 u64 xe_guc_engine_activity_active_ticks(struct xe_guc *guc, struct xe_hw_engine *hwe, 395 unsigned int fn_id) 396 { 397 if (!xe_guc_engine_activity_supported(guc)) 398 return 0; 399 400 if (!is_function_valid(guc, fn_id)) 401 return 0; 402 403 return get_engine_active_ticks(guc, hwe, fn_id); 404 } 405 406 /** 407 * xe_guc_engine_activity_total_ticks - Get engine total ticks 408 * @guc: The GuC object 409 * @hwe: The hw_engine object 410 * @fn_id: function id to report on 411 * 412 * Return: accumulated quanta of ticks allocated for the engine 413 */ 414 u64 xe_guc_engine_activity_total_ticks(struct xe_guc *guc, struct xe_hw_engine *hwe, 415 unsigned int fn_id) 416 { 417 if (!xe_guc_engine_activity_supported(guc)) 418 return 0; 419 420 if (!is_function_valid(guc, fn_id)) 421 return 0; 422 423 return get_engine_total_ticks(guc, hwe, fn_id); 424 } 425 426 /** 427 * xe_guc_engine_activity_supported - Check support for engine activity stats 428 * @guc: The GuC object 429 * 430 * Engine activity stats is supported from GuC interface version (1.14.1) 431 * 432 * Return: true if engine activity stats supported, false otherwise 433 */ 434 bool xe_guc_engine_activity_supported(struct xe_guc *guc) 435 { 436 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 437 438 return engine_activity->supported; 439 } 440 441 /** 442 * xe_guc_engine_activity_function_stats - Enable/Disable per-function engine activity stats 443 * @guc: The GuC object 444 * @num_vfs: number of vfs 445 * @enable: true to enable, false otherwise 446 * 447 * Return: 0 on success, negative error code otherwise 448 */ 449 int xe_guc_engine_activity_function_stats(struct xe_guc *guc, int num_vfs, bool enable) 450 { 451 if (!xe_guc_engine_activity_supported(guc)) 452 return 0; 453 454 if (enable) 455 return engine_activity_enable_function_stats(guc, num_vfs); 456 457 return engine_activity_disable_function_stats(guc); 458 } 459 460 /** 461 * xe_guc_engine_activity_enable_stats - Enable engine activity stats 462 * @guc: The GuC object 463 * 464 * Enable engine activity stats and set initial timestamps 465 */ 466 void xe_guc_engine_activity_enable_stats(struct xe_guc *guc) 467 { 468 int ret; 469 470 if (!xe_guc_engine_activity_supported(guc)) 471 return; 472 473 ret = enable_engine_activity_stats(guc); 474 if (ret) 475 xe_gt_err(guc_to_gt(guc), "failed to enable activity stats%d\n", ret); 476 else 477 engine_activity_set_cpu_ts(guc, 0); 478 } 479 480 static void engine_activity_fini(void *arg) 481 { 482 struct xe_guc_engine_activity *engine_activity = arg; 483 struct engine_activity_buffer *buffer = &engine_activity->device_buffer; 484 485 free_engine_activity_buffers(buffer); 486 } 487 488 /** 489 * xe_guc_engine_activity_init - Initialize the engine activity data 490 * @guc: The GuC object 491 * 492 * Return: 0 on success, negative error code otherwise. 493 */ 494 int xe_guc_engine_activity_init(struct xe_guc *guc) 495 { 496 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 497 struct xe_gt *gt = guc_to_gt(guc); 498 int ret; 499 500 engine_activity->supported = is_engine_activity_supported(guc); 501 if (!engine_activity->supported) 502 return 0; 503 504 ret = allocate_engine_activity_group(guc); 505 if (ret) { 506 xe_gt_err(gt, "failed to allocate engine activity group (%pe)\n", ERR_PTR(ret)); 507 return ret; 508 } 509 510 ret = allocate_engine_activity_buffers(guc, &engine_activity->device_buffer, 1); 511 if (ret) { 512 xe_gt_err(gt, "failed to allocate engine activity buffers (%pe)\n", ERR_PTR(ret)); 513 return ret; 514 } 515 516 engine_activity->gpm_timestamp_shift = gpm_timestamp_shift(gt); 517 518 return devm_add_action_or_reset(gt_to_xe(gt)->drm.dev, engine_activity_fini, 519 engine_activity); 520 } 521