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 for (i = 0; i < GUC_MAX_ENGINE_CLASSES; i++) 308 for (j = 0; j < GUC_MAX_INSTANCES_PER_CLASS; j++) 309 eag->engine[i][j].last_cpu_ts = ktime_get(); 310 } 311 312 static u32 gpm_timestamp_shift(struct xe_gt *gt) 313 { 314 u32 reg; 315 316 reg = xe_mmio_read32(>->mmio, RPM_CONFIG0); 317 318 return 3 - REG_FIELD_GET(RPM_CONFIG0_CTC_SHIFT_PARAMETER_MASK, reg); 319 } 320 321 static bool is_function_valid(struct xe_guc *guc, unsigned int fn_id) 322 { 323 struct xe_device *xe = guc_to_xe(guc); 324 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 325 326 if (!IS_SRIOV_PF(xe) && fn_id) 327 return false; 328 329 if (engine_activity->num_functions && fn_id >= engine_activity->num_functions) 330 return false; 331 332 return true; 333 } 334 335 static int engine_activity_disable_function_stats(struct xe_guc *guc) 336 { 337 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 338 struct engine_activity_buffer *buffer = &engine_activity->function_buffer; 339 int ret; 340 341 if (!engine_activity->num_functions) 342 return 0; 343 344 ret = enable_function_engine_activity_stats(guc, false); 345 if (ret) 346 return ret; 347 348 free_engine_activity_buffers(buffer); 349 engine_activity->num_functions = 0; 350 351 return 0; 352 } 353 354 static int engine_activity_enable_function_stats(struct xe_guc *guc, int num_vfs) 355 { 356 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 357 struct engine_activity_buffer *buffer = &engine_activity->function_buffer; 358 int ret, i; 359 360 if (!num_vfs) 361 return 0; 362 363 /* This includes 1 PF and num_vfs */ 364 engine_activity->num_functions = num_vfs + 1; 365 366 ret = allocate_engine_activity_buffers(guc, buffer, engine_activity->num_functions); 367 if (ret) 368 return ret; 369 370 ret = enable_function_engine_activity_stats(guc, true); 371 if (ret) { 372 free_engine_activity_buffers(buffer); 373 engine_activity->num_functions = 0; 374 return ret; 375 } 376 377 for (i = 0; i < engine_activity->num_functions; i++) 378 engine_activity_set_cpu_ts(guc, i + 1); 379 380 return 0; 381 } 382 383 /** 384 * xe_guc_engine_activity_active_ticks - Get engine active ticks 385 * @guc: The GuC object 386 * @hwe: The hw_engine object 387 * @fn_id: function id to report on 388 * 389 * Return: accumulated ticks @hwe was active since engine activity stats were enabled. 390 */ 391 u64 xe_guc_engine_activity_active_ticks(struct xe_guc *guc, struct xe_hw_engine *hwe, 392 unsigned int fn_id) 393 { 394 if (!xe_guc_engine_activity_supported(guc)) 395 return 0; 396 397 if (!is_function_valid(guc, fn_id)) 398 return 0; 399 400 return get_engine_active_ticks(guc, hwe, fn_id); 401 } 402 403 /** 404 * xe_guc_engine_activity_total_ticks - Get engine total ticks 405 * @guc: The GuC object 406 * @hwe: The hw_engine object 407 * @fn_id: function id to report on 408 * 409 * Return: accumulated quanta of ticks allocated for the engine 410 */ 411 u64 xe_guc_engine_activity_total_ticks(struct xe_guc *guc, struct xe_hw_engine *hwe, 412 unsigned int fn_id) 413 { 414 if (!xe_guc_engine_activity_supported(guc)) 415 return 0; 416 417 if (!is_function_valid(guc, fn_id)) 418 return 0; 419 420 return get_engine_total_ticks(guc, hwe, fn_id); 421 } 422 423 /** 424 * xe_guc_engine_activity_supported - Check support for engine activity stats 425 * @guc: The GuC object 426 * 427 * Engine activity stats is supported from GuC interface version (1.14.1) 428 * 429 * Return: true if engine activity stats supported, false otherwise 430 */ 431 bool xe_guc_engine_activity_supported(struct xe_guc *guc) 432 { 433 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 434 435 return engine_activity->supported; 436 } 437 438 /** 439 * xe_guc_engine_activity_function_stats - Enable/Disable per-function engine activity stats 440 * @guc: The GuC object 441 * @num_vfs: number of vfs 442 * @enable: true to enable, false otherwise 443 * 444 * Return: 0 on success, negative error code otherwise 445 */ 446 int xe_guc_engine_activity_function_stats(struct xe_guc *guc, int num_vfs, bool enable) 447 { 448 if (!xe_guc_engine_activity_supported(guc)) 449 return 0; 450 451 if (enable) 452 return engine_activity_enable_function_stats(guc, num_vfs); 453 454 return engine_activity_disable_function_stats(guc); 455 } 456 457 /** 458 * xe_guc_engine_activity_enable_stats - Enable engine activity stats 459 * @guc: The GuC object 460 * 461 * Enable engine activity stats and set initial timestamps 462 */ 463 void xe_guc_engine_activity_enable_stats(struct xe_guc *guc) 464 { 465 int ret; 466 467 if (!xe_guc_engine_activity_supported(guc)) 468 return; 469 470 ret = enable_engine_activity_stats(guc); 471 if (ret) 472 xe_gt_err(guc_to_gt(guc), "failed to enable activity stats%d\n", ret); 473 else 474 engine_activity_set_cpu_ts(guc, 0); 475 } 476 477 static void engine_activity_fini(void *arg) 478 { 479 struct xe_guc_engine_activity *engine_activity = arg; 480 struct engine_activity_buffer *buffer = &engine_activity->device_buffer; 481 482 free_engine_activity_buffers(buffer); 483 } 484 485 /** 486 * xe_guc_engine_activity_init - Initialize the engine activity data 487 * @guc: The GuC object 488 * 489 * Return: 0 on success, negative error code otherwise. 490 */ 491 int xe_guc_engine_activity_init(struct xe_guc *guc) 492 { 493 struct xe_guc_engine_activity *engine_activity = &guc->engine_activity; 494 struct xe_gt *gt = guc_to_gt(guc); 495 int ret; 496 497 engine_activity->supported = is_engine_activity_supported(guc); 498 if (!engine_activity->supported) 499 return 0; 500 501 ret = allocate_engine_activity_group(guc); 502 if (ret) { 503 xe_gt_err(gt, "failed to allocate engine activity group (%pe)\n", ERR_PTR(ret)); 504 return ret; 505 } 506 507 ret = allocate_engine_activity_buffers(guc, &engine_activity->device_buffer, 1); 508 if (ret) { 509 xe_gt_err(gt, "failed to allocate engine activity buffers (%pe)\n", ERR_PTR(ret)); 510 return ret; 511 } 512 513 engine_activity->gpm_timestamp_shift = gpm_timestamp_shift(gt); 514 515 return devm_add_action_or_reset(gt_to_xe(gt)->drm.dev, engine_activity_fini, 516 engine_activity); 517 } 518