|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Copyright © 2024 Intel Corporation |
| 4 | + */ |
| 5 | + |
| 6 | +#include "xe_gpufreqtracer.h" |
| 7 | + |
| 8 | +#include <linux/workqueue.h> |
| 9 | +#include <linux/timer.h> |
| 10 | +#include <linux/slab.h> |
| 11 | +#include <linux/jiffies.h> |
| 12 | +#include <linux/kernel.h> |
| 13 | +#include <linux/moduleparam.h> |
| 14 | +#include <linux/types.h> |
| 15 | +#include <linux/container_of.h> |
| 16 | +#include <linux/gfp.h> |
| 17 | + |
| 18 | +#include "xe_device.h" |
| 19 | +#include "xe_gt.h" |
| 20 | +#include "xe_gt_types.h" |
| 21 | +#include "xe_guc_pc.h" |
| 22 | +#include "xe_module.h" |
| 23 | + |
| 24 | +#define CREATE_TRACE_POINTS |
| 25 | +#include "xe_gpufreqtracer_trace.h" |
| 26 | + |
| 27 | +/** |
| 28 | + * struct xe_gpufreqtracer_gt_data - Per-GT frequency monitoring data |
| 29 | + * @gt: Reference to the GT |
| 30 | + * @timer: Timer for periodic monitoring |
| 31 | + * @work: Work item for frequency sampling |
| 32 | + * @last_frequency: Last reported frequency to avoid duplicate reports |
| 33 | + * @monitoring_active: Whether monitoring is currently active |
| 34 | + */ |
| 35 | +struct xe_gpufreqtracer_gt_data { |
| 36 | + struct xe_gt *gt; |
| 37 | + struct timer_list timer; |
| 38 | + struct work_struct work; |
| 39 | + u32 last_frequency; |
| 40 | + bool monitoring_active; |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * struct xe_gpufreqtracer_data - Per-device frequency tracer data |
| 45 | + * @xe: Reference to the XE device |
| 46 | + * @gt_data: Array of per-GT monitoring data |
| 47 | + */ |
| 48 | +struct xe_gpufreqtracer_data { |
| 49 | + struct xe_device *xe; |
| 50 | + struct xe_gpufreqtracer_gt_data *gt_data; |
| 51 | +}; |
| 52 | + |
| 53 | + |
| 54 | +/** |
| 55 | + * xe_gpufreqtracer_sample_work - Worker function to sample GPU frequency. |
| 56 | + * @work: Pointer to the work_struct representing the scheduled work. |
| 57 | + * |
| 58 | + * This function is executed in a workqueue context to periodically sample |
| 59 | + * the GPU frequency and perform any necessary tracing or logging operations. |
| 60 | + * It is part of the GPU frequency tracer subsystem. |
| 61 | + */ |
| 62 | +static void xe_gpufreqtracer_sample_work(struct work_struct *work) |
| 63 | +{ |
| 64 | + struct xe_gpufreqtracer_gt_data *gt_data = |
| 65 | + container_of(work, struct xe_gpufreqtracer_gt_data, work); |
| 66 | + struct xe_gt *gt = gt_data->gt; |
| 67 | + struct xe_guc_pc *pc = >->uc.guc.pc; |
| 68 | + u32 current_freq; |
| 69 | + |
| 70 | + if (!gt_data->monitoring_active) { |
| 71 | + drm_warn(>_to_xe(gt)->drm, "monitoring not active for GT%u, exiting", |
| 72 | + gt->info.id); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + current_freq = xe_guc_pc_get_act_freq(pc) * 1000; /* Convert MHz to KHz */ |
| 77 | + |
| 78 | + /* Only report if frequency has changed or this is the first sample */ |
| 79 | + if (current_freq != gt_data->last_frequency) { |
| 80 | + drm_dbg(>_to_xe(gt)->drm, "GT%u frequency changed, tracing %u KHz", |
| 81 | + gt->info.id, current_freq); |
| 82 | + trace_gpu_frequency(current_freq, gt->info.id); |
| 83 | + gt_data->last_frequency = current_freq; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * xe_gpufreqtracer_timer_callback - Timer callback for GPU frequency tracer |
| 89 | + * @timer: Pointer to the timer_list structure associated with this callback |
| 90 | + * |
| 91 | + * This function is invoked when the timer associated with the GPU frequency tracer expires. |
| 92 | + * It is responsible for handling periodic tasks related to GPU frequency tracing, such as |
| 93 | + * sampling or logging frequency data. |
| 94 | + */ |
| 95 | +static void xe_gpufreqtracer_timer_callback(struct timer_list *timer) |
| 96 | +{ |
| 97 | + struct xe_gpufreqtracer_gt_data *gt_data = |
| 98 | + container_of(timer, struct xe_gpufreqtracer_gt_data, timer); |
| 99 | + |
| 100 | + if (gt_data->monitoring_active) { |
| 101 | + queue_work(system_highpri_wq, >_data->work); |
| 102 | + mod_timer(>_data->timer, jiffies + |
| 103 | + msecs_to_jiffies(xe_modparam.gpufreq_monitoring_interval_ms)); |
| 104 | + } else { |
| 105 | + drm_warn(>_to_xe(gt_data->gt)->drm, "timer callback for GT%u but monitoring inactive", |
| 106 | + gt_data->gt->info.id); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * xe_gpufreqtracer_init - Initialize GPU frequency tracer for a device |
| 112 | + * @xe: The XE device |
| 113 | + * |
| 114 | + * Sets up the frequency tracer infrastructure for all GTs in the device. |
| 115 | + * |
| 116 | + * Return: 0 on success, negative error code on failure |
| 117 | + */ |
| 118 | +int xe_gpufreqtracer_init(struct xe_device *xe) |
| 119 | +{ |
| 120 | + struct xe_gpufreqtracer_data *tracer_data; |
| 121 | + struct xe_gt *gt; |
| 122 | + u8 tile_id; |
| 123 | + int ret = 0; |
| 124 | + |
| 125 | + tracer_data = kzalloc(sizeof(*tracer_data), GFP_KERNEL); |
| 126 | + if (!tracer_data) |
| 127 | + return -ENOMEM; |
| 128 | + |
| 129 | + tracer_data->xe = xe; |
| 130 | + |
| 131 | + /* Allocate GT data array based on actual GT count */ |
| 132 | + tracer_data->gt_data = kcalloc(xe->info.gt_count, |
| 133 | + sizeof(*tracer_data->gt_data), |
| 134 | + GFP_KERNEL); |
| 135 | + if (!tracer_data->gt_data) { |
| 136 | + ret = -ENOMEM; |
| 137 | + goto err_free_tracer; |
| 138 | + } |
| 139 | + |
| 140 | + /* Initialize per-GT data */ |
| 141 | + for_each_gt(gt, xe, tile_id) { |
| 142 | + struct xe_gpufreqtracer_gt_data *gt_data = |
| 143 | + &tracer_data->gt_data[gt->info.id]; |
| 144 | + |
| 145 | + drm_dbg(&xe->drm, "initializing GT%u (tile %u)", gt->info.id, tile_id); |
| 146 | + |
| 147 | + gt_data->gt = gt; |
| 148 | + gt_data->monitoring_active = false; |
| 149 | + gt_data->last_frequency = 0; |
| 150 | + |
| 151 | + INIT_WORK(>_data->work, xe_gpufreqtracer_sample_work); |
| 152 | + timer_setup(>_data->timer, xe_gpufreqtracer_timer_callback, 0); |
| 153 | + |
| 154 | + drm_dbg(&xe->drm, "GT%u initialized with global interval=%u ms", |
| 155 | + gt->info.id, xe_modparam.gpufreq_monitoring_interval_ms); |
| 156 | + } |
| 157 | + |
| 158 | + xe->gpufreqtracer_data = tracer_data; |
| 159 | + return 0; |
| 160 | + |
| 161 | +err_free_tracer: |
| 162 | + drm_err(&xe->drm, "initialization failed, freeing tracer data"); |
| 163 | + kfree(tracer_data); |
| 164 | + return ret; |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * xe_gpufreqtracer_fini - Cleanup GPU frequency tracer for a device |
| 169 | + * @xe: The XE device |
| 170 | + * |
| 171 | + * Stops all monitoring and cleans up tracer resources. |
| 172 | + */ |
| 173 | +void xe_gpufreqtracer_fini(struct xe_device *xe) |
| 174 | +{ |
| 175 | + struct xe_gpufreqtracer_data *tracer_data = xe->gpufreqtracer_data; |
| 176 | + struct xe_gt *gt; |
| 177 | + u8 tile_id; |
| 178 | + |
| 179 | + if (!tracer_data) { |
| 180 | + drm_warn(&xe->drm, "no tracer data found, nothing to cleanup"); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + /* Stop all monitoring */ |
| 185 | + for_each_gt(gt, xe, tile_id) { |
| 186 | + drm_dbg(&xe->drm, "stopping monitoring for GT%u", gt->info.id); |
| 187 | + xe_gpufreqtracer_stop_monitoring(gt); |
| 188 | + } |
| 189 | + |
| 190 | + kfree(tracer_data->gt_data); |
| 191 | + kfree(tracer_data); |
| 192 | + xe->gpufreqtracer_data = NULL; |
| 193 | +} |
| 194 | + |
| 195 | +/** |
| 196 | + * xe_gpufreqtracer_report_frequency_change - Report frequency change directly |
| 197 | + * @gt: The GT instance |
| 198 | + * @frequency_khz: The new frequency in KHz |
| 199 | + * |
| 200 | + * Reports a frequency change immediately through the tracepoint. |
| 201 | + */ |
| 202 | +void xe_gpufreqtracer_report_frequency_change(struct xe_gt *gt, u32 frequency_khz) |
| 203 | +{ |
| 204 | + drm_dbg(>_to_xe(gt)->drm, "direct frequency report for GT%u: %u KHz", |
| 205 | + gt->info.id, frequency_khz); |
| 206 | + |
| 207 | + if (frequency_khz > 0) { |
| 208 | + trace_gpu_frequency(frequency_khz, gt->info.id); |
| 209 | + drm_dbg(>_to_xe(gt)->drm, "traced frequency change for GT%u", gt->info.id); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +/** |
| 214 | + * xe_gpufreqtracer_start_monitoring - Start periodic frequency monitoring |
| 215 | + * @gt: The GT instance |
| 216 | + * |
| 217 | + * Starts periodic sampling of GPU frequency for the specified GT using the global |
| 218 | + * monitoring interval from module parameters. |
| 219 | + * |
| 220 | + * Return: 0 on success, negative error code on failure |
| 221 | + */ |
| 222 | +int xe_gpufreqtracer_start_monitoring(struct xe_gt *gt) |
| 223 | +{ |
| 224 | + struct xe_gpufreqtracer_data *tracer_data = gt_to_xe(gt)->gpufreqtracer_data; |
| 225 | + struct xe_gpufreqtracer_gt_data *gt_data; |
| 226 | + |
| 227 | + if (!tracer_data) { |
| 228 | + drm_warn(>_to_xe(gt)->drm, "no tracer data for GT%u, not supported", gt->info.id); |
| 229 | + return -EOPNOTSUPP; |
| 230 | + } |
| 231 | + |
| 232 | + if (gt->info.id >= gt_to_xe(gt)->info.gt_count) { |
| 233 | + drm_err(>_to_xe(gt)->drm, "invalid GT ID %u, max supported is %u", |
| 234 | + gt->info.id, gt_to_xe(gt)->info.gt_count - 1); |
| 235 | + return -EINVAL; |
| 236 | + } |
| 237 | + |
| 238 | + gt_data = &tracer_data->gt_data[gt->info.id]; |
| 239 | + |
| 240 | + if (gt_data->monitoring_active) { |
| 241 | + drm_warn(>_to_xe(gt)->drm, "monitoring already active for GT%u", gt->info.id); |
| 242 | + return -EALREADY; |
| 243 | + } |
| 244 | + |
| 245 | + gt_data->monitoring_active = true; |
| 246 | + gt_data->last_frequency = 0; |
| 247 | + |
| 248 | + /* Start the timer using global interval */ |
| 249 | + mod_timer(>_data->timer, jiffies + |
| 250 | + msecs_to_jiffies(xe_modparam.gpufreq_monitoring_interval_ms)); |
| 251 | + |
| 252 | + drm_dbg(>_to_xe(gt)->drm, "monitoring started for GT%u with interval %u ms", |
| 253 | + gt->info.id, xe_modparam.gpufreq_monitoring_interval_ms); |
| 254 | + |
| 255 | + return 0; |
| 256 | +} |
| 257 | + |
| 258 | +/** |
| 259 | + * xe_gpufreqtracer_stop_monitoring - Stop periodic frequency monitoring |
| 260 | + * @gt: The GT instance |
| 261 | + * |
| 262 | + * Stops periodic sampling of GPU frequency for the specified GT. |
| 263 | + */ |
| 264 | +void xe_gpufreqtracer_stop_monitoring(struct xe_gt *gt) |
| 265 | +{ |
| 266 | + struct xe_gpufreqtracer_data *tracer_data = gt_to_xe(gt)->gpufreqtracer_data; |
| 267 | + struct xe_gpufreqtracer_gt_data *gt_data; |
| 268 | + |
| 269 | + if (!tracer_data || gt->info.id >= gt_to_xe(gt)->info.gt_count) { |
| 270 | + drm_err(>_to_xe(gt)->drm, "invalid tracer data or GT ID %u for stop request", |
| 271 | + gt->info.id); |
| 272 | + return; |
| 273 | + } |
| 274 | + |
| 275 | + gt_data = &tracer_data->gt_data[gt->info.id]; |
| 276 | + |
| 277 | + if (!gt_data->monitoring_active) { |
| 278 | + drm_warn(>_to_xe(gt)->drm, "monitoring not active for GT%u, nothing to stop", |
| 279 | + gt->info.id); |
| 280 | + return; |
| 281 | + } |
| 282 | + |
| 283 | + gt_data->monitoring_active = false; |
| 284 | + |
| 285 | + del_timer_sync(>_data->timer); |
| 286 | + cancel_work_sync(>_data->work); |
| 287 | +} |
0 commit comments