1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2023-2024 Intel Corporation
4 */
5
6 #include <linux/anon_inodes.h>
7 #include <linux/delay.h>
8 #include <linux/nospec.h>
9 #include <linux/poll.h>
10
11 #include <drm/drm_drv.h>
12 #include <drm/drm_managed.h>
13 #include <uapi/drm/xe_drm.h>
14
15 #include <generated/xe_wa_oob.h>
16
17 #include "abi/guc_actions_slpc_abi.h"
18 #include "instructions/xe_mi_commands.h"
19 #include "regs/xe_engine_regs.h"
20 #include "regs/xe_gt_regs.h"
21 #include "regs/xe_oa_regs.h"
22 #include "xe_assert.h"
23 #include "xe_bb.h"
24 #include "xe_bo.h"
25 #include "xe_device.h"
26 #include "xe_exec_queue.h"
27 #include "xe_force_wake.h"
28 #include "xe_gt.h"
29 #include "xe_gt_mcr.h"
30 #include "xe_gt_printk.h"
31 #include "xe_guc_pc.h"
32 #include "xe_macros.h"
33 #include "xe_mmio.h"
34 #include "xe_oa.h"
35 #include "xe_observation.h"
36 #include "xe_pm.h"
37 #include "xe_sched_job.h"
38 #include "xe_sriov.h"
39 #include "xe_sync.h"
40 #include "xe_wa.h"
41
42 #define DEFAULT_POLL_FREQUENCY_HZ 200
43 #define DEFAULT_POLL_PERIOD_NS (NSEC_PER_SEC / DEFAULT_POLL_FREQUENCY_HZ)
44 #define XE_OA_UNIT_INVALID U32_MAX
45
46 enum xe_oam_unit_type {
47 XE_OAM_UNIT_SAG,
48 XE_OAM_UNIT_SCMI_0,
49 XE_OAM_UNIT_SCMI_1,
50 };
51
52 enum xe_oa_submit_deps {
53 XE_OA_SUBMIT_NO_DEPS,
54 XE_OA_SUBMIT_ADD_DEPS,
55 };
56
57 enum xe_oa_user_extn_from {
58 XE_OA_USER_EXTN_FROM_OPEN,
59 XE_OA_USER_EXTN_FROM_CONFIG,
60 };
61
62 struct xe_oa_reg {
63 struct xe_reg addr;
64 u32 value;
65 };
66
67 struct xe_oa_config {
68 struct xe_oa *oa;
69
70 char uuid[UUID_STRING_LEN + 1];
71 int id;
72
73 const struct xe_oa_reg *regs;
74 u32 regs_len;
75
76 struct attribute_group sysfs_metric;
77 struct attribute *attrs[2];
78 struct kobj_attribute sysfs_metric_id;
79
80 struct kref ref;
81 struct rcu_head rcu;
82 };
83
84 struct xe_oa_open_param {
85 struct xe_file *xef;
86 struct xe_oa_unit *oa_unit;
87 bool sample;
88 u32 metric_set;
89 enum xe_oa_format_name oa_format;
90 int period_exponent;
91 bool disabled;
92 int exec_queue_id;
93 int engine_instance;
94 struct xe_exec_queue *exec_q;
95 struct xe_hw_engine *hwe;
96 bool no_preempt;
97 struct drm_xe_sync __user *syncs_user;
98 int num_syncs;
99 struct xe_sync_entry *syncs;
100 size_t oa_buffer_size;
101 int wait_num_reports;
102 };
103
104 struct xe_oa_config_bo {
105 struct llist_node node;
106
107 struct xe_oa_config *oa_config;
108 struct xe_bb *bb;
109 };
110
111 struct xe_oa_fence {
112 /* @base: dma fence base */
113 struct dma_fence base;
114 /* @lock: lock for the fence */
115 spinlock_t lock;
116 /* @work: work to signal @base */
117 struct delayed_work work;
118 /* @cb: callback to schedule @work */
119 struct dma_fence_cb cb;
120 };
121
122 #define DRM_FMT(x) DRM_XE_OA_FMT_TYPE_##x
123
124 static const struct xe_oa_format oa_formats[] = {
125 [XE_OA_FORMAT_C4_B8] = { 7, 64, DRM_FMT(OAG) },
126 [XE_OA_FORMAT_A12] = { 0, 64, DRM_FMT(OAG) },
127 [XE_OA_FORMAT_A12_B8_C8] = { 2, 128, DRM_FMT(OAG) },
128 [XE_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256, DRM_FMT(OAG) },
129 [XE_OAR_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256, DRM_FMT(OAR) },
130 [XE_OA_FORMAT_A24u40_A14u32_B8_C8] = { 5, 256, DRM_FMT(OAG) },
131 [XE_OAC_FORMAT_A24u64_B8_C8] = { 1, 320, DRM_FMT(OAC), HDR_64_BIT },
132 [XE_OAC_FORMAT_A22u32_R2u32_B8_C8] = { 2, 192, DRM_FMT(OAC), HDR_64_BIT },
133 [XE_OAM_FORMAT_MPEC8u64_B8_C8] = { 1, 192, DRM_FMT(OAM_MPEC), HDR_64_BIT },
134 [XE_OAM_FORMAT_MPEC8u32_B8_C8] = { 2, 128, DRM_FMT(OAM_MPEC), HDR_64_BIT },
135 [XE_OA_FORMAT_PEC64u64] = { 1, 576, DRM_FMT(PEC), HDR_64_BIT, 1, 0 },
136 [XE_OA_FORMAT_PEC64u64_B8_C8] = { 1, 640, DRM_FMT(PEC), HDR_64_BIT, 1, 1 },
137 [XE_OA_FORMAT_PEC64u32] = { 1, 320, DRM_FMT(PEC), HDR_64_BIT },
138 [XE_OA_FORMAT_PEC32u64_G1] = { 5, 320, DRM_FMT(PEC), HDR_64_BIT, 1, 0 },
139 [XE_OA_FORMAT_PEC32u32_G1] = { 5, 192, DRM_FMT(PEC), HDR_64_BIT },
140 [XE_OA_FORMAT_PEC32u64_G2] = { 6, 320, DRM_FMT(PEC), HDR_64_BIT, 1, 0 },
141 [XE_OA_FORMAT_PEC32u32_G2] = { 6, 192, DRM_FMT(PEC), HDR_64_BIT },
142 [XE_OA_FORMAT_PEC36u64_G1_32_G2_4] = { 3, 320, DRM_FMT(PEC), HDR_64_BIT, 1, 0 },
143 [XE_OA_FORMAT_PEC36u64_G1_4_G2_32] = { 4, 320, DRM_FMT(PEC), HDR_64_BIT, 1, 0 },
144 };
145
xe_oa_circ_diff(struct xe_oa_stream * stream,u32 tail,u32 head)146 static u32 xe_oa_circ_diff(struct xe_oa_stream *stream, u32 tail, u32 head)
147 {
148 return tail >= head ? tail - head :
149 tail + stream->oa_buffer.circ_size - head;
150 }
151
xe_oa_circ_incr(struct xe_oa_stream * stream,u32 ptr,u32 n)152 static u32 xe_oa_circ_incr(struct xe_oa_stream *stream, u32 ptr, u32 n)
153 {
154 return ptr + n >= stream->oa_buffer.circ_size ?
155 ptr + n - stream->oa_buffer.circ_size : ptr + n;
156 }
157
xe_oa_config_release(struct kref * ref)158 static void xe_oa_config_release(struct kref *ref)
159 {
160 struct xe_oa_config *oa_config =
161 container_of(ref, typeof(*oa_config), ref);
162
163 kfree(oa_config->regs);
164
165 kfree_rcu(oa_config, rcu);
166 }
167
xe_oa_config_put(struct xe_oa_config * oa_config)168 static void xe_oa_config_put(struct xe_oa_config *oa_config)
169 {
170 if (!oa_config)
171 return;
172
173 kref_put(&oa_config->ref, xe_oa_config_release);
174 }
175
xe_oa_config_get(struct xe_oa_config * oa_config)176 static struct xe_oa_config *xe_oa_config_get(struct xe_oa_config *oa_config)
177 {
178 return kref_get_unless_zero(&oa_config->ref) ? oa_config : NULL;
179 }
180
xe_oa_get_oa_config(struct xe_oa * oa,int metrics_set)181 static struct xe_oa_config *xe_oa_get_oa_config(struct xe_oa *oa, int metrics_set)
182 {
183 struct xe_oa_config *oa_config;
184
185 rcu_read_lock();
186 oa_config = idr_find(&oa->metrics_idr, metrics_set);
187 if (oa_config)
188 oa_config = xe_oa_config_get(oa_config);
189 rcu_read_unlock();
190
191 return oa_config;
192 }
193
free_oa_config_bo(struct xe_oa_config_bo * oa_bo,struct dma_fence * last_fence)194 static void free_oa_config_bo(struct xe_oa_config_bo *oa_bo, struct dma_fence *last_fence)
195 {
196 xe_oa_config_put(oa_bo->oa_config);
197 xe_bb_free(oa_bo->bb, last_fence);
198 kfree(oa_bo);
199 }
200
__oa_regs(struct xe_oa_stream * stream)201 static const struct xe_oa_regs *__oa_regs(struct xe_oa_stream *stream)
202 {
203 return &stream->oa_unit->regs;
204 }
205
xe_oa_hw_tail_read(struct xe_oa_stream * stream)206 static u32 xe_oa_hw_tail_read(struct xe_oa_stream *stream)
207 {
208 return xe_mmio_read32(&stream->gt->mmio, __oa_regs(stream)->oa_tail_ptr) &
209 OAG_OATAILPTR_MASK;
210 }
211
212 #define oa_report_header_64bit(__s) \
213 ((__s)->oa_buffer.format->header == HDR_64_BIT)
214
oa_report_id(struct xe_oa_stream * stream,void * report)215 static u64 oa_report_id(struct xe_oa_stream *stream, void *report)
216 {
217 return oa_report_header_64bit(stream) ? *(u64 *)report : *(u32 *)report;
218 }
219
oa_report_id_clear(struct xe_oa_stream * stream,u32 * report)220 static void oa_report_id_clear(struct xe_oa_stream *stream, u32 *report)
221 {
222 if (oa_report_header_64bit(stream))
223 *(u64 *)report = 0;
224 else
225 *report = 0;
226 }
227
oa_timestamp(struct xe_oa_stream * stream,void * report)228 static u64 oa_timestamp(struct xe_oa_stream *stream, void *report)
229 {
230 return oa_report_header_64bit(stream) ?
231 *((u64 *)report + 1) :
232 *((u32 *)report + 1);
233 }
234
oa_timestamp_clear(struct xe_oa_stream * stream,u32 * report)235 static void oa_timestamp_clear(struct xe_oa_stream *stream, u32 *report)
236 {
237 if (oa_report_header_64bit(stream))
238 *(u64 *)&report[2] = 0;
239 else
240 report[1] = 0;
241 }
242
xe_oa_buffer_check_unlocked(struct xe_oa_stream * stream)243 static bool xe_oa_buffer_check_unlocked(struct xe_oa_stream *stream)
244 {
245 u32 gtt_offset = xe_bo_ggtt_addr(stream->oa_buffer.bo);
246 u32 tail, hw_tail, partial_report_size, available;
247 int report_size = stream->oa_buffer.format->size;
248 unsigned long flags;
249
250 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
251
252 hw_tail = xe_oa_hw_tail_read(stream);
253 hw_tail -= gtt_offset;
254
255 /*
256 * The tail pointer increases in 64 byte (cacheline size), not in report_size
257 * increments. Also report size may not be a power of 2. Compute potential
258 * partially landed report in OA buffer.
259 */
260 partial_report_size = xe_oa_circ_diff(stream, hw_tail, stream->oa_buffer.tail);
261 partial_report_size %= report_size;
262
263 /* Subtract partial amount off the tail */
264 hw_tail = xe_oa_circ_diff(stream, hw_tail, partial_report_size);
265
266 tail = hw_tail;
267
268 /*
269 * Walk the stream backward until we find a report with report id and timestamp
270 * not 0. We can't tell whether a report has fully landed in memory before the
271 * report id and timestamp of the following report have landed.
272 *
273 * This is assuming that the writes of the OA unit land in memory in the order
274 * they were written. If not : (╯°□°)╯︵ ┻━┻
275 */
276 while (xe_oa_circ_diff(stream, tail, stream->oa_buffer.tail) >= report_size) {
277 void *report = stream->oa_buffer.vaddr + tail;
278
279 if (oa_report_id(stream, report) || oa_timestamp(stream, report))
280 break;
281
282 tail = xe_oa_circ_diff(stream, tail, report_size);
283 }
284
285 if (xe_oa_circ_diff(stream, hw_tail, tail) > report_size)
286 drm_dbg(&stream->oa->xe->drm,
287 "unlanded report(s) head=0x%x tail=0x%x hw_tail=0x%x\n",
288 stream->oa_buffer.head, tail, hw_tail);
289
290 stream->oa_buffer.tail = tail;
291
292 available = xe_oa_circ_diff(stream, stream->oa_buffer.tail, stream->oa_buffer.head);
293 stream->pollin = available >= stream->wait_num_reports * report_size;
294
295 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
296
297 return stream->pollin;
298 }
299
xe_oa_poll_check_timer_cb(struct hrtimer * hrtimer)300 static enum hrtimer_restart xe_oa_poll_check_timer_cb(struct hrtimer *hrtimer)
301 {
302 struct xe_oa_stream *stream =
303 container_of(hrtimer, typeof(*stream), poll_check_timer);
304
305 if (xe_oa_buffer_check_unlocked(stream))
306 wake_up(&stream->poll_wq);
307
308 hrtimer_forward_now(hrtimer, ns_to_ktime(stream->poll_period_ns));
309
310 return HRTIMER_RESTART;
311 }
312
xe_oa_append_report(struct xe_oa_stream * stream,char __user * buf,size_t count,size_t * offset,const u8 * report)313 static int xe_oa_append_report(struct xe_oa_stream *stream, char __user *buf,
314 size_t count, size_t *offset, const u8 *report)
315 {
316 int report_size = stream->oa_buffer.format->size;
317 int report_size_partial;
318 u8 *oa_buf_end;
319
320 if ((count - *offset) < report_size)
321 return -ENOSPC;
322
323 buf += *offset;
324
325 oa_buf_end = stream->oa_buffer.vaddr + stream->oa_buffer.circ_size;
326 report_size_partial = oa_buf_end - report;
327
328 if (report_size_partial < report_size) {
329 if (copy_to_user(buf, report, report_size_partial))
330 return -EFAULT;
331 buf += report_size_partial;
332
333 if (copy_to_user(buf, stream->oa_buffer.vaddr,
334 report_size - report_size_partial))
335 return -EFAULT;
336 } else if (copy_to_user(buf, report, report_size)) {
337 return -EFAULT;
338 }
339
340 *offset += report_size;
341
342 return 0;
343 }
344
xe_oa_append_reports(struct xe_oa_stream * stream,char __user * buf,size_t count,size_t * offset)345 static int xe_oa_append_reports(struct xe_oa_stream *stream, char __user *buf,
346 size_t count, size_t *offset)
347 {
348 int report_size = stream->oa_buffer.format->size;
349 u8 *oa_buf_base = stream->oa_buffer.vaddr;
350 u32 gtt_offset = xe_bo_ggtt_addr(stream->oa_buffer.bo);
351 size_t start_offset = *offset;
352 unsigned long flags;
353 u32 head, tail;
354 int ret = 0;
355
356 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
357 head = stream->oa_buffer.head;
358 tail = stream->oa_buffer.tail;
359 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
360
361 xe_assert(stream->oa->xe,
362 head < stream->oa_buffer.circ_size && tail < stream->oa_buffer.circ_size);
363
364 for (; xe_oa_circ_diff(stream, tail, head);
365 head = xe_oa_circ_incr(stream, head, report_size)) {
366 u8 *report = oa_buf_base + head;
367
368 ret = xe_oa_append_report(stream, buf, count, offset, report);
369 if (ret)
370 break;
371
372 if (!(stream->oa_buffer.circ_size % report_size)) {
373 /* Clear out report id and timestamp to detect unlanded reports */
374 oa_report_id_clear(stream, (void *)report);
375 oa_timestamp_clear(stream, (void *)report);
376 } else {
377 u8 *oa_buf_end = stream->oa_buffer.vaddr + stream->oa_buffer.circ_size;
378 u32 part = oa_buf_end - report;
379
380 /* Zero out the entire report */
381 if (report_size <= part) {
382 memset(report, 0, report_size);
383 } else {
384 memset(report, 0, part);
385 memset(oa_buf_base, 0, report_size - part);
386 }
387 }
388 }
389
390 if (start_offset != *offset) {
391 struct xe_reg oaheadptr = __oa_regs(stream)->oa_head_ptr;
392
393 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
394 xe_mmio_write32(&stream->gt->mmio, oaheadptr,
395 (head + gtt_offset) & OAG_OAHEADPTR_MASK);
396 stream->oa_buffer.head = head;
397 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
398 }
399
400 return ret;
401 }
402
xe_oa_init_oa_buffer(struct xe_oa_stream * stream)403 static void xe_oa_init_oa_buffer(struct xe_oa_stream *stream)
404 {
405 u32 gtt_offset = xe_bo_ggtt_addr(stream->oa_buffer.bo);
406 int size_exponent = __ffs(xe_bo_size(stream->oa_buffer.bo));
407 u32 oa_buf = gtt_offset | OAG_OABUFFER_MEMORY_SELECT;
408 struct xe_mmio *mmio = &stream->gt->mmio;
409 unsigned long flags;
410
411 /*
412 * If oa buffer size is more than 16MB (exponent greater than 24), the
413 * oa buffer size field is multiplied by 8 in xe_oa_enable_metric_set.
414 */
415 oa_buf |= REG_FIELD_PREP(OABUFFER_SIZE_MASK,
416 size_exponent > 24 ? size_exponent - 20 : size_exponent - 17);
417
418 spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
419
420 xe_mmio_write32(mmio, __oa_regs(stream)->oa_status, 0);
421 xe_mmio_write32(mmio, __oa_regs(stream)->oa_head_ptr,
422 gtt_offset & OAG_OAHEADPTR_MASK);
423 stream->oa_buffer.head = 0;
424 /*
425 * PRM says: "This MMIO must be set before the OATAILPTR register and after the
426 * OAHEADPTR register. This is to enable proper functionality of the overflow bit".
427 */
428 xe_mmio_write32(mmio, __oa_regs(stream)->oa_buffer, oa_buf);
429 xe_mmio_write32(mmio, __oa_regs(stream)->oa_tail_ptr,
430 gtt_offset & OAG_OATAILPTR_MASK);
431
432 /* Mark that we need updated tail pointer to read from */
433 stream->oa_buffer.tail = 0;
434
435 spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
436
437 /* Zero out the OA buffer since we rely on zero report id and timestamp fields */
438 memset(stream->oa_buffer.vaddr, 0, xe_bo_size(stream->oa_buffer.bo));
439 }
440
__format_to_oactrl(const struct xe_oa_format * format,int counter_sel_mask)441 static u32 __format_to_oactrl(const struct xe_oa_format *format, int counter_sel_mask)
442 {
443 return ((format->counter_select << (ffs(counter_sel_mask) - 1)) & counter_sel_mask) |
444 REG_FIELD_PREP(OA_OACONTROL_REPORT_BC_MASK, format->bc_report) |
445 REG_FIELD_PREP(OA_OACONTROL_COUNTER_SIZE_MASK, format->counter_size);
446 }
447
__oa_ccs_select(struct xe_oa_stream * stream)448 static u32 __oa_ccs_select(struct xe_oa_stream *stream)
449 {
450 u32 val;
451
452 if (stream->hwe->class != XE_ENGINE_CLASS_COMPUTE)
453 return 0;
454
455 val = REG_FIELD_PREP(OAG_OACONTROL_OA_CCS_SELECT_MASK, stream->hwe->instance);
456 xe_assert(stream->oa->xe,
457 REG_FIELD_GET(OAG_OACONTROL_OA_CCS_SELECT_MASK, val) == stream->hwe->instance);
458 return val;
459 }
460
__oactrl_used_bits(struct xe_oa_stream * stream)461 static u32 __oactrl_used_bits(struct xe_oa_stream *stream)
462 {
463 return stream->oa_unit->type == DRM_XE_OA_UNIT_TYPE_OAG ?
464 OAG_OACONTROL_USED_BITS : OAM_OACONTROL_USED_BITS;
465 }
466
xe_oa_enable(struct xe_oa_stream * stream)467 static void xe_oa_enable(struct xe_oa_stream *stream)
468 {
469 const struct xe_oa_format *format = stream->oa_buffer.format;
470 const struct xe_oa_regs *regs;
471 u32 val;
472
473 /*
474 * BSpec: 46822: Bit 0. Even if stream->sample is 0, for OAR to function, the OA
475 * buffer must be correctly initialized
476 */
477 xe_oa_init_oa_buffer(stream);
478
479 regs = __oa_regs(stream);
480 val = __format_to_oactrl(format, regs->oa_ctrl_counter_select_mask) |
481 __oa_ccs_select(stream) | OAG_OACONTROL_OA_COUNTER_ENABLE;
482
483 if (GRAPHICS_VER(stream->oa->xe) >= 20 &&
484 stream->oa_unit->type == DRM_XE_OA_UNIT_TYPE_OAG)
485 val |= OAG_OACONTROL_OA_PES_DISAG_EN;
486
487 xe_mmio_rmw32(&stream->gt->mmio, regs->oa_ctrl, __oactrl_used_bits(stream), val);
488 }
489
xe_oa_disable(struct xe_oa_stream * stream)490 static void xe_oa_disable(struct xe_oa_stream *stream)
491 {
492 struct xe_mmio *mmio = &stream->gt->mmio;
493
494 xe_mmio_rmw32(mmio, __oa_regs(stream)->oa_ctrl, __oactrl_used_bits(stream), 0);
495 if (xe_mmio_wait32(mmio, __oa_regs(stream)->oa_ctrl,
496 OAG_OACONTROL_OA_COUNTER_ENABLE, 0, 50000, NULL, false))
497 drm_err(&stream->oa->xe->drm,
498 "wait for OA to be disabled timed out\n");
499
500 if (GRAPHICS_VERx100(stream->oa->xe) <= 1270 && GRAPHICS_VERx100(stream->oa->xe) != 1260) {
501 /* <= XE_METEORLAKE except XE_PVC */
502 xe_mmio_write32(mmio, OA_TLB_INV_CR, 1);
503 if (xe_mmio_wait32(mmio, OA_TLB_INV_CR, 1, 0, 50000, NULL, false))
504 drm_err(&stream->oa->xe->drm,
505 "wait for OA tlb invalidate timed out\n");
506 }
507 }
508
xe_oa_wait_unlocked(struct xe_oa_stream * stream)509 static int xe_oa_wait_unlocked(struct xe_oa_stream *stream)
510 {
511 /* We might wait indefinitely if periodic sampling is not enabled */
512 if (!stream->periodic)
513 return -EINVAL;
514
515 return wait_event_interruptible(stream->poll_wq,
516 xe_oa_buffer_check_unlocked(stream));
517 }
518
519 #define OASTATUS_RELEVANT_BITS (OASTATUS_MMIO_TRG_Q_FULL | OASTATUS_COUNTER_OVERFLOW | \
520 OASTATUS_BUFFER_OVERFLOW | OASTATUS_REPORT_LOST)
521
__xe_oa_read(struct xe_oa_stream * stream,char __user * buf,size_t count,size_t * offset)522 static int __xe_oa_read(struct xe_oa_stream *stream, char __user *buf,
523 size_t count, size_t *offset)
524 {
525 /* Only clear our bits to avoid side-effects */
526 stream->oa_status = xe_mmio_rmw32(&stream->gt->mmio, __oa_regs(stream)->oa_status,
527 OASTATUS_RELEVANT_BITS, 0);
528 /*
529 * Signal to userspace that there is non-zero OA status to read via
530 * @DRM_XE_OBSERVATION_IOCTL_STATUS observation stream fd ioctl
531 */
532 if (stream->oa_status & OASTATUS_RELEVANT_BITS)
533 return -EIO;
534
535 return xe_oa_append_reports(stream, buf, count, offset);
536 }
537
xe_oa_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)538 static ssize_t xe_oa_read(struct file *file, char __user *buf,
539 size_t count, loff_t *ppos)
540 {
541 struct xe_oa_stream *stream = file->private_data;
542 size_t offset = 0;
543 int ret;
544
545 /* Can't read from disabled streams */
546 if (!stream->enabled || !stream->sample)
547 return -EINVAL;
548
549 if (!(file->f_flags & O_NONBLOCK)) {
550 do {
551 ret = xe_oa_wait_unlocked(stream);
552 if (ret)
553 return ret;
554
555 mutex_lock(&stream->stream_lock);
556 ret = __xe_oa_read(stream, buf, count, &offset);
557 mutex_unlock(&stream->stream_lock);
558 } while (!offset && !ret);
559 } else {
560 xe_oa_buffer_check_unlocked(stream);
561 mutex_lock(&stream->stream_lock);
562 ret = __xe_oa_read(stream, buf, count, &offset);
563 mutex_unlock(&stream->stream_lock);
564 }
565
566 /*
567 * Typically we clear pollin here in order to wait for the new hrtimer callback
568 * before unblocking. The exception to this is if __xe_oa_read returns -ENOSPC,
569 * which means that more OA data is available than could fit in the user provided
570 * buffer. In this case we want the next poll() call to not block.
571 *
572 * Also in case of -EIO, we have already waited for data before returning
573 * -EIO, so need to wait again
574 */
575 if (ret != -ENOSPC && ret != -EIO)
576 stream->pollin = false;
577
578 /* Possible values for ret are 0, -EFAULT, -ENOSPC, -EIO, -EINVAL, ... */
579 return offset ?: (ret ?: -EAGAIN);
580 }
581
xe_oa_poll_locked(struct xe_oa_stream * stream,struct file * file,poll_table * wait)582 static __poll_t xe_oa_poll_locked(struct xe_oa_stream *stream,
583 struct file *file, poll_table *wait)
584 {
585 __poll_t events = 0;
586
587 poll_wait(file, &stream->poll_wq, wait);
588
589 /*
590 * We don't explicitly check whether there's something to read here since this
591 * path may be hot depending on what else userspace is polling, or on the timeout
592 * in use. We rely on hrtimer xe_oa_poll_check_timer_cb to notify us when there
593 * are samples to read
594 */
595 if (stream->pollin)
596 events |= EPOLLIN;
597
598 return events;
599 }
600
xe_oa_poll(struct file * file,poll_table * wait)601 static __poll_t xe_oa_poll(struct file *file, poll_table *wait)
602 {
603 struct xe_oa_stream *stream = file->private_data;
604 __poll_t ret;
605
606 mutex_lock(&stream->stream_lock);
607 ret = xe_oa_poll_locked(stream, file, wait);
608 mutex_unlock(&stream->stream_lock);
609
610 return ret;
611 }
612
xe_oa_lock_vma(struct xe_exec_queue * q)613 static void xe_oa_lock_vma(struct xe_exec_queue *q)
614 {
615 if (q->vm) {
616 down_read(&q->vm->lock);
617 xe_vm_lock(q->vm, false);
618 }
619 }
620
xe_oa_unlock_vma(struct xe_exec_queue * q)621 static void xe_oa_unlock_vma(struct xe_exec_queue *q)
622 {
623 if (q->vm) {
624 xe_vm_unlock(q->vm);
625 up_read(&q->vm->lock);
626 }
627 }
628
xe_oa_submit_bb(struct xe_oa_stream * stream,enum xe_oa_submit_deps deps,struct xe_bb * bb)629 static struct dma_fence *xe_oa_submit_bb(struct xe_oa_stream *stream, enum xe_oa_submit_deps deps,
630 struct xe_bb *bb)
631 {
632 struct xe_exec_queue *q = stream->exec_q ?: stream->k_exec_q;
633 struct xe_sched_job *job;
634 struct dma_fence *fence;
635 int err = 0;
636
637 xe_oa_lock_vma(q);
638
639 job = xe_bb_create_job(q, bb);
640 if (IS_ERR(job)) {
641 err = PTR_ERR(job);
642 goto exit;
643 }
644 job->ggtt = true;
645
646 if (deps == XE_OA_SUBMIT_ADD_DEPS) {
647 for (int i = 0; i < stream->num_syncs && !err; i++)
648 err = xe_sync_entry_add_deps(&stream->syncs[i], job);
649 if (err) {
650 drm_dbg(&stream->oa->xe->drm, "xe_sync_entry_add_deps err %d\n", err);
651 goto err_put_job;
652 }
653 }
654
655 xe_sched_job_arm(job);
656 fence = dma_fence_get(&job->drm.s_fence->finished);
657 xe_sched_job_push(job);
658
659 xe_oa_unlock_vma(q);
660
661 return fence;
662 err_put_job:
663 xe_sched_job_put(job);
664 exit:
665 xe_oa_unlock_vma(q);
666 return ERR_PTR(err);
667 }
668
write_cs_mi_lri(struct xe_bb * bb,const struct xe_oa_reg * reg_data,u32 n_regs)669 static void write_cs_mi_lri(struct xe_bb *bb, const struct xe_oa_reg *reg_data, u32 n_regs)
670 {
671 u32 i;
672
673 #define MI_LOAD_REGISTER_IMM_MAX_REGS (126)
674
675 for (i = 0; i < n_regs; i++) {
676 if ((i % MI_LOAD_REGISTER_IMM_MAX_REGS) == 0) {
677 u32 n_lri = min_t(u32, n_regs - i,
678 MI_LOAD_REGISTER_IMM_MAX_REGS);
679
680 bb->cs[bb->len++] = MI_LOAD_REGISTER_IMM | MI_LRI_NUM_REGS(n_lri);
681 }
682 bb->cs[bb->len++] = reg_data[i].addr.addr;
683 bb->cs[bb->len++] = reg_data[i].value;
684 }
685 }
686
num_lri_dwords(int num_regs)687 static int num_lri_dwords(int num_regs)
688 {
689 int count = 0;
690
691 if (num_regs > 0) {
692 count += DIV_ROUND_UP(num_regs, MI_LOAD_REGISTER_IMM_MAX_REGS);
693 count += num_regs * 2;
694 }
695
696 return count;
697 }
698
xe_oa_free_oa_buffer(struct xe_oa_stream * stream)699 static void xe_oa_free_oa_buffer(struct xe_oa_stream *stream)
700 {
701 xe_bo_unpin_map_no_vm(stream->oa_buffer.bo);
702 }
703
xe_oa_free_configs(struct xe_oa_stream * stream)704 static void xe_oa_free_configs(struct xe_oa_stream *stream)
705 {
706 struct xe_oa_config_bo *oa_bo, *tmp;
707
708 xe_oa_config_put(stream->oa_config);
709 llist_for_each_entry_safe(oa_bo, tmp, stream->oa_config_bos.first, node)
710 free_oa_config_bo(oa_bo, stream->last_fence);
711 dma_fence_put(stream->last_fence);
712 }
713
xe_oa_load_with_lri(struct xe_oa_stream * stream,struct xe_oa_reg * reg_lri,u32 count)714 static int xe_oa_load_with_lri(struct xe_oa_stream *stream, struct xe_oa_reg *reg_lri, u32 count)
715 {
716 struct dma_fence *fence;
717 struct xe_bb *bb;
718 int err;
719
720 bb = xe_bb_new(stream->gt, 2 * count + 1, false);
721 if (IS_ERR(bb)) {
722 err = PTR_ERR(bb);
723 goto exit;
724 }
725
726 write_cs_mi_lri(bb, reg_lri, count);
727
728 fence = xe_oa_submit_bb(stream, XE_OA_SUBMIT_NO_DEPS, bb);
729 if (IS_ERR(fence)) {
730 err = PTR_ERR(fence);
731 goto free_bb;
732 }
733 xe_bb_free(bb, fence);
734 dma_fence_put(fence);
735
736 return 0;
737 free_bb:
738 xe_bb_free(bb, NULL);
739 exit:
740 return err;
741 }
742
xe_oa_configure_oar_context(struct xe_oa_stream * stream,bool enable)743 static int xe_oa_configure_oar_context(struct xe_oa_stream *stream, bool enable)
744 {
745 const struct xe_oa_format *format = stream->oa_buffer.format;
746 u32 oacontrol = __format_to_oactrl(format, OAR_OACONTROL_COUNTER_SEL_MASK) |
747 (enable ? OAR_OACONTROL_COUNTER_ENABLE : 0);
748
749 struct xe_oa_reg reg_lri[] = {
750 {
751 OACTXCONTROL(stream->hwe->mmio_base),
752 enable ? OA_COUNTER_RESUME : 0,
753 },
754 {
755 OAR_OACONTROL,
756 oacontrol,
757 },
758 {
759 RING_CONTEXT_CONTROL(stream->hwe->mmio_base),
760 _MASKED_FIELD(CTX_CTRL_OAC_CONTEXT_ENABLE,
761 enable ? CTX_CTRL_OAC_CONTEXT_ENABLE : 0)
762 },
763 };
764
765 return xe_oa_load_with_lri(stream, reg_lri, ARRAY_SIZE(reg_lri));
766 }
767
xe_oa_configure_oac_context(struct xe_oa_stream * stream,bool enable)768 static int xe_oa_configure_oac_context(struct xe_oa_stream *stream, bool enable)
769 {
770 const struct xe_oa_format *format = stream->oa_buffer.format;
771 u32 oacontrol = __format_to_oactrl(format, OAR_OACONTROL_COUNTER_SEL_MASK) |
772 (enable ? OAR_OACONTROL_COUNTER_ENABLE : 0);
773 struct xe_oa_reg reg_lri[] = {
774 {
775 OACTXCONTROL(stream->hwe->mmio_base),
776 enable ? OA_COUNTER_RESUME : 0,
777 },
778 {
779 OAC_OACONTROL,
780 oacontrol
781 },
782 {
783 RING_CONTEXT_CONTROL(stream->hwe->mmio_base),
784 _MASKED_FIELD(CTX_CTRL_OAC_CONTEXT_ENABLE,
785 enable ? CTX_CTRL_OAC_CONTEXT_ENABLE : 0) |
786 _MASKED_FIELD(CTX_CTRL_RUN_ALONE, enable ? CTX_CTRL_RUN_ALONE : 0),
787 },
788 };
789
790 /* Set ccs select to enable programming of OAC_OACONTROL */
791 xe_mmio_write32(&stream->gt->mmio, __oa_regs(stream)->oa_ctrl,
792 __oa_ccs_select(stream));
793
794 return xe_oa_load_with_lri(stream, reg_lri, ARRAY_SIZE(reg_lri));
795 }
796
xe_oa_configure_oa_context(struct xe_oa_stream * stream,bool enable)797 static int xe_oa_configure_oa_context(struct xe_oa_stream *stream, bool enable)
798 {
799 switch (stream->hwe->class) {
800 case XE_ENGINE_CLASS_RENDER:
801 return xe_oa_configure_oar_context(stream, enable);
802 case XE_ENGINE_CLASS_COMPUTE:
803 return xe_oa_configure_oac_context(stream, enable);
804 default:
805 /* Video engines do not support MI_REPORT_PERF_COUNT */
806 return 0;
807 }
808 }
809
810 #define HAS_OA_BPC_REPORTING(xe) (GRAPHICS_VERx100(xe) >= 1255)
811
oag_configure_mmio_trigger(const struct xe_oa_stream * stream,bool enable)812 static u32 oag_configure_mmio_trigger(const struct xe_oa_stream *stream, bool enable)
813 {
814 return _MASKED_FIELD(OAG_OA_DEBUG_DISABLE_MMIO_TRG,
815 enable && stream && stream->sample ?
816 0 : OAG_OA_DEBUG_DISABLE_MMIO_TRG);
817 }
818
xe_oa_disable_metric_set(struct xe_oa_stream * stream)819 static void xe_oa_disable_metric_set(struct xe_oa_stream *stream)
820 {
821 struct xe_mmio *mmio = &stream->gt->mmio;
822 u32 sqcnt1;
823
824 /* Enable thread stall DOP gating and EU DOP gating. */
825 if (XE_WA(stream->gt, 1508761755)) {
826 xe_gt_mcr_multicast_write(stream->gt, ROW_CHICKEN,
827 _MASKED_BIT_DISABLE(STALL_DOP_GATING_DISABLE));
828 xe_gt_mcr_multicast_write(stream->gt, ROW_CHICKEN2,
829 _MASKED_BIT_DISABLE(DISABLE_DOP_GATING));
830 }
831
832 xe_mmio_write32(mmio, __oa_regs(stream)->oa_debug,
833 oag_configure_mmio_trigger(stream, false));
834
835 /* disable the context save/restore or OAR counters */
836 if (stream->exec_q)
837 xe_oa_configure_oa_context(stream, false);
838
839 /* Make sure we disable noa to save power. */
840 xe_mmio_rmw32(mmio, RPM_CONFIG1, GT_NOA_ENABLE, 0);
841
842 sqcnt1 = SQCNT1_PMON_ENABLE |
843 (HAS_OA_BPC_REPORTING(stream->oa->xe) ? SQCNT1_OABPC : 0);
844
845 /* Reset PMON Enable to save power. */
846 xe_mmio_rmw32(mmio, XELPMP_SQCNT1, sqcnt1, 0);
847
848 if ((stream->oa_unit->type == DRM_XE_OA_UNIT_TYPE_OAM ||
849 stream->oa_unit->type == DRM_XE_OA_UNIT_TYPE_OAM_SAG) &&
850 GRAPHICS_VER(stream->oa->xe) >= 30)
851 xe_mmio_rmw32(mmio, OAM_COMPRESSION_T3_CONTROL, OAM_LAT_MEASURE_ENABLE, 0);
852 }
853
xe_oa_stream_destroy(struct xe_oa_stream * stream)854 static void xe_oa_stream_destroy(struct xe_oa_stream *stream)
855 {
856 struct xe_oa_unit *u = stream->oa_unit;
857 struct xe_gt *gt = stream->hwe->gt;
858
859 if (WARN_ON(stream != u->exclusive_stream))
860 return;
861
862 WRITE_ONCE(u->exclusive_stream, NULL);
863
864 mutex_destroy(&stream->stream_lock);
865
866 xe_oa_disable_metric_set(stream);
867 xe_exec_queue_put(stream->k_exec_q);
868
869 xe_oa_free_oa_buffer(stream);
870
871 xe_force_wake_put(gt_to_fw(gt), XE_FORCEWAKE_ALL);
872 xe_pm_runtime_put(stream->oa->xe);
873
874 /* Wa_1509372804:pvc: Unset the override of GUCRC mode to enable rc6 */
875 if (stream->override_gucrc)
876 xe_gt_WARN_ON(gt, xe_guc_pc_unset_gucrc_mode(>->uc.guc.pc));
877
878 xe_oa_free_configs(stream);
879 xe_file_put(stream->xef);
880 }
881
xe_oa_alloc_oa_buffer(struct xe_oa_stream * stream,size_t size)882 static int xe_oa_alloc_oa_buffer(struct xe_oa_stream *stream, size_t size)
883 {
884 struct xe_bo *bo;
885
886 bo = xe_bo_create_pin_map(stream->oa->xe, stream->gt->tile, NULL,
887 size, ttm_bo_type_kernel,
888 XE_BO_FLAG_SYSTEM | XE_BO_FLAG_GGTT);
889 if (IS_ERR(bo))
890 return PTR_ERR(bo);
891
892 stream->oa_buffer.bo = bo;
893 /* mmap implementation requires OA buffer to be in system memory */
894 xe_assert(stream->oa->xe, bo->vmap.is_iomem == 0);
895 stream->oa_buffer.vaddr = bo->vmap.vaddr;
896 return 0;
897 }
898
899 static struct xe_oa_config_bo *
__xe_oa_alloc_config_buffer(struct xe_oa_stream * stream,struct xe_oa_config * oa_config)900 __xe_oa_alloc_config_buffer(struct xe_oa_stream *stream, struct xe_oa_config *oa_config)
901 {
902 struct xe_oa_config_bo *oa_bo;
903 size_t config_length;
904 struct xe_bb *bb;
905
906 oa_bo = kzalloc(sizeof(*oa_bo), GFP_KERNEL);
907 if (!oa_bo)
908 return ERR_PTR(-ENOMEM);
909
910 config_length = num_lri_dwords(oa_config->regs_len);
911 config_length = ALIGN(sizeof(u32) * config_length, XE_PAGE_SIZE) / sizeof(u32);
912
913 bb = xe_bb_new(stream->gt, config_length, false);
914 if (IS_ERR(bb))
915 goto err_free;
916
917 write_cs_mi_lri(bb, oa_config->regs, oa_config->regs_len);
918
919 oa_bo->bb = bb;
920 oa_bo->oa_config = xe_oa_config_get(oa_config);
921 llist_add(&oa_bo->node, &stream->oa_config_bos);
922
923 return oa_bo;
924 err_free:
925 kfree(oa_bo);
926 return ERR_CAST(bb);
927 }
928
929 static struct xe_oa_config_bo *
xe_oa_alloc_config_buffer(struct xe_oa_stream * stream,struct xe_oa_config * oa_config)930 xe_oa_alloc_config_buffer(struct xe_oa_stream *stream, struct xe_oa_config *oa_config)
931 {
932 struct xe_oa_config_bo *oa_bo;
933
934 /* Look for the buffer in the already allocated BOs attached to the stream */
935 llist_for_each_entry(oa_bo, stream->oa_config_bos.first, node) {
936 if (oa_bo->oa_config == oa_config &&
937 memcmp(oa_bo->oa_config->uuid, oa_config->uuid,
938 sizeof(oa_config->uuid)) == 0)
939 goto out;
940 }
941
942 oa_bo = __xe_oa_alloc_config_buffer(stream, oa_config);
943 out:
944 return oa_bo;
945 }
946
xe_oa_update_last_fence(struct xe_oa_stream * stream,struct dma_fence * fence)947 static void xe_oa_update_last_fence(struct xe_oa_stream *stream, struct dma_fence *fence)
948 {
949 dma_fence_put(stream->last_fence);
950 stream->last_fence = dma_fence_get(fence);
951 }
952
xe_oa_fence_work_fn(struct work_struct * w)953 static void xe_oa_fence_work_fn(struct work_struct *w)
954 {
955 struct xe_oa_fence *ofence = container_of(w, typeof(*ofence), work.work);
956
957 /* Signal fence to indicate new OA configuration is active */
958 dma_fence_signal(&ofence->base);
959 dma_fence_put(&ofence->base);
960 }
961
xe_oa_config_cb(struct dma_fence * fence,struct dma_fence_cb * cb)962 static void xe_oa_config_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
963 {
964 /* Additional empirical delay needed for NOA programming after registers are written */
965 #define NOA_PROGRAM_ADDITIONAL_DELAY_US 500
966
967 struct xe_oa_fence *ofence = container_of(cb, typeof(*ofence), cb);
968
969 INIT_DELAYED_WORK(&ofence->work, xe_oa_fence_work_fn);
970 queue_delayed_work(system_unbound_wq, &ofence->work,
971 usecs_to_jiffies(NOA_PROGRAM_ADDITIONAL_DELAY_US));
972 dma_fence_put(fence);
973 }
974
xe_oa_get_driver_name(struct dma_fence * fence)975 static const char *xe_oa_get_driver_name(struct dma_fence *fence)
976 {
977 return "xe_oa";
978 }
979
xe_oa_get_timeline_name(struct dma_fence * fence)980 static const char *xe_oa_get_timeline_name(struct dma_fence *fence)
981 {
982 return "unbound";
983 }
984
985 static const struct dma_fence_ops xe_oa_fence_ops = {
986 .get_driver_name = xe_oa_get_driver_name,
987 .get_timeline_name = xe_oa_get_timeline_name,
988 };
989
xe_oa_emit_oa_config(struct xe_oa_stream * stream,struct xe_oa_config * config)990 static int xe_oa_emit_oa_config(struct xe_oa_stream *stream, struct xe_oa_config *config)
991 {
992 #define NOA_PROGRAM_ADDITIONAL_DELAY_US 500
993 struct xe_oa_config_bo *oa_bo;
994 struct xe_oa_fence *ofence;
995 int i, err, num_signal = 0;
996 struct dma_fence *fence;
997
998 ofence = kzalloc(sizeof(*ofence), GFP_KERNEL);
999 if (!ofence) {
1000 err = -ENOMEM;
1001 goto exit;
1002 }
1003
1004 oa_bo = xe_oa_alloc_config_buffer(stream, config);
1005 if (IS_ERR(oa_bo)) {
1006 err = PTR_ERR(oa_bo);
1007 goto exit;
1008 }
1009
1010 /* Emit OA configuration batch */
1011 fence = xe_oa_submit_bb(stream, XE_OA_SUBMIT_ADD_DEPS, oa_bo->bb);
1012 if (IS_ERR(fence)) {
1013 err = PTR_ERR(fence);
1014 goto exit;
1015 }
1016
1017 /* Point of no return: initialize and set fence to signal */
1018 spin_lock_init(&ofence->lock);
1019 dma_fence_init(&ofence->base, &xe_oa_fence_ops, &ofence->lock, 0, 0);
1020
1021 for (i = 0; i < stream->num_syncs; i++) {
1022 if (stream->syncs[i].flags & DRM_XE_SYNC_FLAG_SIGNAL)
1023 num_signal++;
1024 xe_sync_entry_signal(&stream->syncs[i], &ofence->base);
1025 }
1026
1027 /* Additional dma_fence_get in case we dma_fence_wait */
1028 if (!num_signal)
1029 dma_fence_get(&ofence->base);
1030
1031 /* Update last fence too before adding callback */
1032 xe_oa_update_last_fence(stream, fence);
1033
1034 /* Add job fence callback to schedule work to signal ofence->base */
1035 err = dma_fence_add_callback(fence, &ofence->cb, xe_oa_config_cb);
1036 xe_gt_assert(stream->gt, !err || err == -ENOENT);
1037 if (err == -ENOENT)
1038 xe_oa_config_cb(fence, &ofence->cb);
1039
1040 /* If nothing needs to be signaled we wait synchronously */
1041 if (!num_signal) {
1042 dma_fence_wait(&ofence->base, false);
1043 dma_fence_put(&ofence->base);
1044 }
1045
1046 /* Done with syncs */
1047 for (i = 0; i < stream->num_syncs; i++)
1048 xe_sync_entry_cleanup(&stream->syncs[i]);
1049 kfree(stream->syncs);
1050
1051 return 0;
1052 exit:
1053 kfree(ofence);
1054 return err;
1055 }
1056
oag_report_ctx_switches(const struct xe_oa_stream * stream)1057 static u32 oag_report_ctx_switches(const struct xe_oa_stream *stream)
1058 {
1059 /* If user didn't require OA reports, ask HW not to emit ctx switch reports */
1060 return _MASKED_FIELD(OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS,
1061 stream->sample ?
1062 0 : OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS);
1063 }
1064
oag_buf_size_select(const struct xe_oa_stream * stream)1065 static u32 oag_buf_size_select(const struct xe_oa_stream *stream)
1066 {
1067 return _MASKED_FIELD(OAG_OA_DEBUG_BUF_SIZE_SELECT,
1068 xe_bo_size(stream->oa_buffer.bo) > SZ_16M ?
1069 OAG_OA_DEBUG_BUF_SIZE_SELECT : 0);
1070 }
1071
xe_oa_enable_metric_set(struct xe_oa_stream * stream)1072 static int xe_oa_enable_metric_set(struct xe_oa_stream *stream)
1073 {
1074 struct xe_mmio *mmio = &stream->gt->mmio;
1075 u32 oa_debug, sqcnt1;
1076 int ret;
1077
1078 /*
1079 * EU NOA signals behave incorrectly if EU clock gating is enabled.
1080 * Disable thread stall DOP gating and EU DOP gating.
1081 */
1082 if (XE_WA(stream->gt, 1508761755)) {
1083 xe_gt_mcr_multicast_write(stream->gt, ROW_CHICKEN,
1084 _MASKED_BIT_ENABLE(STALL_DOP_GATING_DISABLE));
1085 xe_gt_mcr_multicast_write(stream->gt, ROW_CHICKEN2,
1086 _MASKED_BIT_ENABLE(DISABLE_DOP_GATING));
1087 }
1088
1089 /* Disable clk ratio reports */
1090 oa_debug = OAG_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
1091 OAG_OA_DEBUG_INCLUDE_CLK_RATIO;
1092
1093 if (GRAPHICS_VER(stream->oa->xe) >= 20)
1094 oa_debug |=
1095 /* The three bits below are needed to get PEC counters running */
1096 OAG_OA_DEBUG_START_TRIGGER_SCOPE_CONTROL |
1097 OAG_OA_DEBUG_DISABLE_START_TRG_2_COUNT_QUAL |
1098 OAG_OA_DEBUG_DISABLE_START_TRG_1_COUNT_QUAL;
1099
1100 xe_mmio_write32(mmio, __oa_regs(stream)->oa_debug,
1101 _MASKED_BIT_ENABLE(oa_debug) |
1102 oag_report_ctx_switches(stream) |
1103 oag_buf_size_select(stream) |
1104 oag_configure_mmio_trigger(stream, true));
1105
1106 xe_mmio_write32(mmio, __oa_regs(stream)->oa_ctx_ctrl, stream->periodic ?
1107 (OAG_OAGLBCTXCTRL_COUNTER_RESUME |
1108 OAG_OAGLBCTXCTRL_TIMER_ENABLE |
1109 REG_FIELD_PREP(OAG_OAGLBCTXCTRL_TIMER_PERIOD_MASK,
1110 stream->period_exponent)) : 0);
1111
1112 /*
1113 * Initialize Super Queue Internal Cnt Register
1114 * Set PMON Enable in order to collect valid metrics
1115 * Enable bytes per clock reporting
1116 */
1117 sqcnt1 = SQCNT1_PMON_ENABLE |
1118 (HAS_OA_BPC_REPORTING(stream->oa->xe) ? SQCNT1_OABPC : 0);
1119 xe_mmio_rmw32(mmio, XELPMP_SQCNT1, 0, sqcnt1);
1120
1121 if ((stream->oa_unit->type == DRM_XE_OA_UNIT_TYPE_OAM ||
1122 stream->oa_unit->type == DRM_XE_OA_UNIT_TYPE_OAM_SAG) &&
1123 GRAPHICS_VER(stream->oa->xe) >= 30)
1124 xe_mmio_rmw32(mmio, OAM_COMPRESSION_T3_CONTROL, 0, OAM_LAT_MEASURE_ENABLE);
1125
1126 /* Configure OAR/OAC */
1127 if (stream->exec_q) {
1128 ret = xe_oa_configure_oa_context(stream, true);
1129 if (ret)
1130 return ret;
1131 }
1132
1133 return xe_oa_emit_oa_config(stream, stream->oa_config);
1134 }
1135
decode_oa_format(struct xe_oa * oa,u64 fmt,enum xe_oa_format_name * name)1136 static int decode_oa_format(struct xe_oa *oa, u64 fmt, enum xe_oa_format_name *name)
1137 {
1138 u32 counter_size = FIELD_GET(DRM_XE_OA_FORMAT_MASK_COUNTER_SIZE, fmt);
1139 u32 counter_sel = FIELD_GET(DRM_XE_OA_FORMAT_MASK_COUNTER_SEL, fmt);
1140 u32 bc_report = FIELD_GET(DRM_XE_OA_FORMAT_MASK_BC_REPORT, fmt);
1141 u32 type = FIELD_GET(DRM_XE_OA_FORMAT_MASK_FMT_TYPE, fmt);
1142 int idx;
1143
1144 for_each_set_bit(idx, oa->format_mask, __XE_OA_FORMAT_MAX) {
1145 const struct xe_oa_format *f = &oa->oa_formats[idx];
1146
1147 if (counter_size == f->counter_size && bc_report == f->bc_report &&
1148 type == f->type && counter_sel == f->counter_select) {
1149 *name = idx;
1150 return 0;
1151 }
1152 }
1153
1154 return -EINVAL;
1155 }
1156
xe_oa_lookup_oa_unit(struct xe_oa * oa,u32 oa_unit_id)1157 static struct xe_oa_unit *xe_oa_lookup_oa_unit(struct xe_oa *oa, u32 oa_unit_id)
1158 {
1159 struct xe_gt *gt;
1160 int gt_id, i;
1161
1162 for_each_gt(gt, oa->xe, gt_id) {
1163 for (i = 0; i < gt->oa.num_oa_units; i++) {
1164 struct xe_oa_unit *u = >->oa.oa_unit[i];
1165
1166 if (u->oa_unit_id == oa_unit_id)
1167 return u;
1168 }
1169 }
1170
1171 return NULL;
1172 }
1173
xe_oa_set_prop_oa_unit_id(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1174 static int xe_oa_set_prop_oa_unit_id(struct xe_oa *oa, u64 value,
1175 struct xe_oa_open_param *param)
1176 {
1177 param->oa_unit = xe_oa_lookup_oa_unit(oa, value);
1178 if (!param->oa_unit) {
1179 drm_dbg(&oa->xe->drm, "OA unit ID out of range %lld\n", value);
1180 return -EINVAL;
1181 }
1182 return 0;
1183 }
1184
xe_oa_set_prop_sample_oa(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1185 static int xe_oa_set_prop_sample_oa(struct xe_oa *oa, u64 value,
1186 struct xe_oa_open_param *param)
1187 {
1188 param->sample = value;
1189 return 0;
1190 }
1191
xe_oa_set_prop_metric_set(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1192 static int xe_oa_set_prop_metric_set(struct xe_oa *oa, u64 value,
1193 struct xe_oa_open_param *param)
1194 {
1195 param->metric_set = value;
1196 return 0;
1197 }
1198
xe_oa_set_prop_oa_format(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1199 static int xe_oa_set_prop_oa_format(struct xe_oa *oa, u64 value,
1200 struct xe_oa_open_param *param)
1201 {
1202 int ret = decode_oa_format(oa, value, ¶m->oa_format);
1203
1204 if (ret) {
1205 drm_dbg(&oa->xe->drm, "Unsupported OA report format %#llx\n", value);
1206 return ret;
1207 }
1208 return 0;
1209 }
1210
xe_oa_set_prop_oa_exponent(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1211 static int xe_oa_set_prop_oa_exponent(struct xe_oa *oa, u64 value,
1212 struct xe_oa_open_param *param)
1213 {
1214 #define OA_EXPONENT_MAX 31
1215
1216 if (value > OA_EXPONENT_MAX) {
1217 drm_dbg(&oa->xe->drm, "OA timer exponent too high (> %u)\n", OA_EXPONENT_MAX);
1218 return -EINVAL;
1219 }
1220 param->period_exponent = value;
1221 return 0;
1222 }
1223
xe_oa_set_prop_disabled(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1224 static int xe_oa_set_prop_disabled(struct xe_oa *oa, u64 value,
1225 struct xe_oa_open_param *param)
1226 {
1227 param->disabled = value;
1228 return 0;
1229 }
1230
xe_oa_set_prop_exec_queue_id(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1231 static int xe_oa_set_prop_exec_queue_id(struct xe_oa *oa, u64 value,
1232 struct xe_oa_open_param *param)
1233 {
1234 param->exec_queue_id = value;
1235 return 0;
1236 }
1237
xe_oa_set_prop_engine_instance(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1238 static int xe_oa_set_prop_engine_instance(struct xe_oa *oa, u64 value,
1239 struct xe_oa_open_param *param)
1240 {
1241 param->engine_instance = value;
1242 return 0;
1243 }
1244
xe_oa_set_no_preempt(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1245 static int xe_oa_set_no_preempt(struct xe_oa *oa, u64 value,
1246 struct xe_oa_open_param *param)
1247 {
1248 param->no_preempt = value;
1249 return 0;
1250 }
1251
xe_oa_set_prop_num_syncs(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1252 static int xe_oa_set_prop_num_syncs(struct xe_oa *oa, u64 value,
1253 struct xe_oa_open_param *param)
1254 {
1255 param->num_syncs = value;
1256 return 0;
1257 }
1258
xe_oa_set_prop_syncs_user(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1259 static int xe_oa_set_prop_syncs_user(struct xe_oa *oa, u64 value,
1260 struct xe_oa_open_param *param)
1261 {
1262 param->syncs_user = u64_to_user_ptr(value);
1263 return 0;
1264 }
1265
xe_oa_set_prop_oa_buffer_size(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1266 static int xe_oa_set_prop_oa_buffer_size(struct xe_oa *oa, u64 value,
1267 struct xe_oa_open_param *param)
1268 {
1269 if (!is_power_of_2(value) || value < SZ_128K || value > SZ_128M) {
1270 drm_dbg(&oa->xe->drm, "OA buffer size invalid %llu\n", value);
1271 return -EINVAL;
1272 }
1273 param->oa_buffer_size = value;
1274 return 0;
1275 }
1276
xe_oa_set_prop_wait_num_reports(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1277 static int xe_oa_set_prop_wait_num_reports(struct xe_oa *oa, u64 value,
1278 struct xe_oa_open_param *param)
1279 {
1280 if (!value) {
1281 drm_dbg(&oa->xe->drm, "wait_num_reports %llu\n", value);
1282 return -EINVAL;
1283 }
1284 param->wait_num_reports = value;
1285 return 0;
1286 }
1287
xe_oa_set_prop_ret_inval(struct xe_oa * oa,u64 value,struct xe_oa_open_param * param)1288 static int xe_oa_set_prop_ret_inval(struct xe_oa *oa, u64 value,
1289 struct xe_oa_open_param *param)
1290 {
1291 return -EINVAL;
1292 }
1293
1294 typedef int (*xe_oa_set_property_fn)(struct xe_oa *oa, u64 value,
1295 struct xe_oa_open_param *param);
1296 static const xe_oa_set_property_fn xe_oa_set_property_funcs_open[] = {
1297 [DRM_XE_OA_PROPERTY_OA_UNIT_ID] = xe_oa_set_prop_oa_unit_id,
1298 [DRM_XE_OA_PROPERTY_SAMPLE_OA] = xe_oa_set_prop_sample_oa,
1299 [DRM_XE_OA_PROPERTY_OA_METRIC_SET] = xe_oa_set_prop_metric_set,
1300 [DRM_XE_OA_PROPERTY_OA_FORMAT] = xe_oa_set_prop_oa_format,
1301 [DRM_XE_OA_PROPERTY_OA_PERIOD_EXPONENT] = xe_oa_set_prop_oa_exponent,
1302 [DRM_XE_OA_PROPERTY_OA_DISABLED] = xe_oa_set_prop_disabled,
1303 [DRM_XE_OA_PROPERTY_EXEC_QUEUE_ID] = xe_oa_set_prop_exec_queue_id,
1304 [DRM_XE_OA_PROPERTY_OA_ENGINE_INSTANCE] = xe_oa_set_prop_engine_instance,
1305 [DRM_XE_OA_PROPERTY_NO_PREEMPT] = xe_oa_set_no_preempt,
1306 [DRM_XE_OA_PROPERTY_NUM_SYNCS] = xe_oa_set_prop_num_syncs,
1307 [DRM_XE_OA_PROPERTY_SYNCS] = xe_oa_set_prop_syncs_user,
1308 [DRM_XE_OA_PROPERTY_OA_BUFFER_SIZE] = xe_oa_set_prop_oa_buffer_size,
1309 [DRM_XE_OA_PROPERTY_WAIT_NUM_REPORTS] = xe_oa_set_prop_wait_num_reports,
1310 };
1311
1312 static const xe_oa_set_property_fn xe_oa_set_property_funcs_config[] = {
1313 [DRM_XE_OA_PROPERTY_OA_UNIT_ID] = xe_oa_set_prop_ret_inval,
1314 [DRM_XE_OA_PROPERTY_SAMPLE_OA] = xe_oa_set_prop_ret_inval,
1315 [DRM_XE_OA_PROPERTY_OA_METRIC_SET] = xe_oa_set_prop_metric_set,
1316 [DRM_XE_OA_PROPERTY_OA_FORMAT] = xe_oa_set_prop_ret_inval,
1317 [DRM_XE_OA_PROPERTY_OA_PERIOD_EXPONENT] = xe_oa_set_prop_ret_inval,
1318 [DRM_XE_OA_PROPERTY_OA_DISABLED] = xe_oa_set_prop_ret_inval,
1319 [DRM_XE_OA_PROPERTY_EXEC_QUEUE_ID] = xe_oa_set_prop_ret_inval,
1320 [DRM_XE_OA_PROPERTY_OA_ENGINE_INSTANCE] = xe_oa_set_prop_ret_inval,
1321 [DRM_XE_OA_PROPERTY_NO_PREEMPT] = xe_oa_set_prop_ret_inval,
1322 [DRM_XE_OA_PROPERTY_NUM_SYNCS] = xe_oa_set_prop_num_syncs,
1323 [DRM_XE_OA_PROPERTY_SYNCS] = xe_oa_set_prop_syncs_user,
1324 [DRM_XE_OA_PROPERTY_OA_BUFFER_SIZE] = xe_oa_set_prop_ret_inval,
1325 [DRM_XE_OA_PROPERTY_WAIT_NUM_REPORTS] = xe_oa_set_prop_ret_inval,
1326 };
1327
xe_oa_user_ext_set_property(struct xe_oa * oa,enum xe_oa_user_extn_from from,u64 extension,struct xe_oa_open_param * param)1328 static int xe_oa_user_ext_set_property(struct xe_oa *oa, enum xe_oa_user_extn_from from,
1329 u64 extension, struct xe_oa_open_param *param)
1330 {
1331 u64 __user *address = u64_to_user_ptr(extension);
1332 struct drm_xe_ext_set_property ext;
1333 int err;
1334 u32 idx;
1335
1336 err = copy_from_user(&ext, address, sizeof(ext));
1337 if (XE_IOCTL_DBG(oa->xe, err))
1338 return -EFAULT;
1339
1340 BUILD_BUG_ON(ARRAY_SIZE(xe_oa_set_property_funcs_open) !=
1341 ARRAY_SIZE(xe_oa_set_property_funcs_config));
1342
1343 if (XE_IOCTL_DBG(oa->xe, ext.property >= ARRAY_SIZE(xe_oa_set_property_funcs_open)) ||
1344 XE_IOCTL_DBG(oa->xe, ext.pad))
1345 return -EINVAL;
1346
1347 idx = array_index_nospec(ext.property, ARRAY_SIZE(xe_oa_set_property_funcs_open));
1348
1349 if (from == XE_OA_USER_EXTN_FROM_CONFIG)
1350 return xe_oa_set_property_funcs_config[idx](oa, ext.value, param);
1351 else
1352 return xe_oa_set_property_funcs_open[idx](oa, ext.value, param);
1353 }
1354
1355 typedef int (*xe_oa_user_extension_fn)(struct xe_oa *oa, enum xe_oa_user_extn_from from,
1356 u64 extension, struct xe_oa_open_param *param);
1357 static const xe_oa_user_extension_fn xe_oa_user_extension_funcs[] = {
1358 [DRM_XE_OA_EXTENSION_SET_PROPERTY] = xe_oa_user_ext_set_property,
1359 };
1360
1361 #define MAX_USER_EXTENSIONS 16
xe_oa_user_extensions(struct xe_oa * oa,enum xe_oa_user_extn_from from,u64 extension,int ext_number,struct xe_oa_open_param * param)1362 static int xe_oa_user_extensions(struct xe_oa *oa, enum xe_oa_user_extn_from from, u64 extension,
1363 int ext_number, struct xe_oa_open_param *param)
1364 {
1365 u64 __user *address = u64_to_user_ptr(extension);
1366 struct drm_xe_user_extension ext;
1367 int err;
1368 u32 idx;
1369
1370 if (XE_IOCTL_DBG(oa->xe, ext_number >= MAX_USER_EXTENSIONS))
1371 return -E2BIG;
1372
1373 err = copy_from_user(&ext, address, sizeof(ext));
1374 if (XE_IOCTL_DBG(oa->xe, err))
1375 return -EFAULT;
1376
1377 if (XE_IOCTL_DBG(oa->xe, ext.pad) ||
1378 XE_IOCTL_DBG(oa->xe, ext.name >= ARRAY_SIZE(xe_oa_user_extension_funcs)))
1379 return -EINVAL;
1380
1381 idx = array_index_nospec(ext.name, ARRAY_SIZE(xe_oa_user_extension_funcs));
1382 err = xe_oa_user_extension_funcs[idx](oa, from, extension, param);
1383 if (XE_IOCTL_DBG(oa->xe, err))
1384 return err;
1385
1386 if (ext.next_extension)
1387 return xe_oa_user_extensions(oa, from, ext.next_extension, ++ext_number, param);
1388
1389 return 0;
1390 }
1391
xe_oa_parse_syncs(struct xe_oa * oa,struct xe_oa_open_param * param)1392 static int xe_oa_parse_syncs(struct xe_oa *oa, struct xe_oa_open_param *param)
1393 {
1394 int ret, num_syncs, num_ufence = 0;
1395
1396 if (param->num_syncs && !param->syncs_user) {
1397 drm_dbg(&oa->xe->drm, "num_syncs specified without sync array\n");
1398 ret = -EINVAL;
1399 goto exit;
1400 }
1401
1402 if (param->num_syncs) {
1403 param->syncs = kcalloc(param->num_syncs, sizeof(*param->syncs), GFP_KERNEL);
1404 if (!param->syncs) {
1405 ret = -ENOMEM;
1406 goto exit;
1407 }
1408 }
1409
1410 for (num_syncs = 0; num_syncs < param->num_syncs; num_syncs++) {
1411 ret = xe_sync_entry_parse(oa->xe, param->xef, ¶m->syncs[num_syncs],
1412 ¶m->syncs_user[num_syncs], 0);
1413 if (ret)
1414 goto err_syncs;
1415
1416 if (xe_sync_is_ufence(¶m->syncs[num_syncs]))
1417 num_ufence++;
1418 }
1419
1420 if (XE_IOCTL_DBG(oa->xe, num_ufence > 1)) {
1421 ret = -EINVAL;
1422 goto err_syncs;
1423 }
1424
1425 return 0;
1426
1427 err_syncs:
1428 while (num_syncs--)
1429 xe_sync_entry_cleanup(¶m->syncs[num_syncs]);
1430 kfree(param->syncs);
1431 exit:
1432 return ret;
1433 }
1434
xe_oa_stream_enable(struct xe_oa_stream * stream)1435 static void xe_oa_stream_enable(struct xe_oa_stream *stream)
1436 {
1437 stream->pollin = false;
1438
1439 xe_oa_enable(stream);
1440
1441 if (stream->sample)
1442 hrtimer_start(&stream->poll_check_timer,
1443 ns_to_ktime(stream->poll_period_ns),
1444 HRTIMER_MODE_REL_PINNED);
1445 }
1446
xe_oa_stream_disable(struct xe_oa_stream * stream)1447 static void xe_oa_stream_disable(struct xe_oa_stream *stream)
1448 {
1449 xe_oa_disable(stream);
1450
1451 if (stream->sample)
1452 hrtimer_cancel(&stream->poll_check_timer);
1453 }
1454
xe_oa_enable_preempt_timeslice(struct xe_oa_stream * stream)1455 static int xe_oa_enable_preempt_timeslice(struct xe_oa_stream *stream)
1456 {
1457 struct xe_exec_queue *q = stream->exec_q;
1458 int ret1, ret2;
1459
1460 /* Best effort recovery: try to revert both to original, irrespective of error */
1461 ret1 = q->ops->set_timeslice(q, stream->hwe->eclass->sched_props.timeslice_us);
1462 ret2 = q->ops->set_preempt_timeout(q, stream->hwe->eclass->sched_props.preempt_timeout_us);
1463 if (ret1 || ret2)
1464 goto err;
1465 return 0;
1466 err:
1467 drm_dbg(&stream->oa->xe->drm, "%s failed ret1 %d ret2 %d\n", __func__, ret1, ret2);
1468 return ret1 ?: ret2;
1469 }
1470
xe_oa_disable_preempt_timeslice(struct xe_oa_stream * stream)1471 static int xe_oa_disable_preempt_timeslice(struct xe_oa_stream *stream)
1472 {
1473 struct xe_exec_queue *q = stream->exec_q;
1474 int ret;
1475
1476 /* Setting values to 0 will disable timeslice and preempt_timeout */
1477 ret = q->ops->set_timeslice(q, 0);
1478 if (ret)
1479 goto err;
1480
1481 ret = q->ops->set_preempt_timeout(q, 0);
1482 if (ret)
1483 goto err;
1484
1485 return 0;
1486 err:
1487 xe_oa_enable_preempt_timeslice(stream);
1488 drm_dbg(&stream->oa->xe->drm, "%s failed %d\n", __func__, ret);
1489 return ret;
1490 }
1491
xe_oa_enable_locked(struct xe_oa_stream * stream)1492 static int xe_oa_enable_locked(struct xe_oa_stream *stream)
1493 {
1494 if (stream->enabled)
1495 return 0;
1496
1497 if (stream->no_preempt) {
1498 int ret = xe_oa_disable_preempt_timeslice(stream);
1499
1500 if (ret)
1501 return ret;
1502 }
1503
1504 xe_oa_stream_enable(stream);
1505
1506 stream->enabled = true;
1507 return 0;
1508 }
1509
xe_oa_disable_locked(struct xe_oa_stream * stream)1510 static int xe_oa_disable_locked(struct xe_oa_stream *stream)
1511 {
1512 int ret = 0;
1513
1514 if (!stream->enabled)
1515 return 0;
1516
1517 xe_oa_stream_disable(stream);
1518
1519 if (stream->no_preempt)
1520 ret = xe_oa_enable_preempt_timeslice(stream);
1521
1522 stream->enabled = false;
1523 return ret;
1524 }
1525
xe_oa_config_locked(struct xe_oa_stream * stream,u64 arg)1526 static long xe_oa_config_locked(struct xe_oa_stream *stream, u64 arg)
1527 {
1528 struct xe_oa_open_param param = {};
1529 long ret = stream->oa_config->id;
1530 struct xe_oa_config *config;
1531 int err;
1532
1533 err = xe_oa_user_extensions(stream->oa, XE_OA_USER_EXTN_FROM_CONFIG, arg, 0, ¶m);
1534 if (err)
1535 return err;
1536
1537 config = xe_oa_get_oa_config(stream->oa, param.metric_set);
1538 if (!config)
1539 return -ENODEV;
1540
1541 param.xef = stream->xef;
1542 err = xe_oa_parse_syncs(stream->oa, ¶m);
1543 if (err)
1544 goto err_config_put;
1545
1546 stream->num_syncs = param.num_syncs;
1547 stream->syncs = param.syncs;
1548
1549 err = xe_oa_emit_oa_config(stream, config);
1550 if (!err) {
1551 config = xchg(&stream->oa_config, config);
1552 drm_dbg(&stream->oa->xe->drm, "changed to oa config uuid=%s\n",
1553 stream->oa_config->uuid);
1554 }
1555
1556 err_config_put:
1557 xe_oa_config_put(config);
1558
1559 return err ?: ret;
1560 }
1561
xe_oa_status_locked(struct xe_oa_stream * stream,unsigned long arg)1562 static long xe_oa_status_locked(struct xe_oa_stream *stream, unsigned long arg)
1563 {
1564 struct drm_xe_oa_stream_status status = {};
1565 void __user *uaddr = (void __user *)arg;
1566
1567 /* Map from register to uapi bits */
1568 if (stream->oa_status & OASTATUS_REPORT_LOST)
1569 status.oa_status |= DRM_XE_OASTATUS_REPORT_LOST;
1570 if (stream->oa_status & OASTATUS_BUFFER_OVERFLOW)
1571 status.oa_status |= DRM_XE_OASTATUS_BUFFER_OVERFLOW;
1572 if (stream->oa_status & OASTATUS_COUNTER_OVERFLOW)
1573 status.oa_status |= DRM_XE_OASTATUS_COUNTER_OVERFLOW;
1574 if (stream->oa_status & OASTATUS_MMIO_TRG_Q_FULL)
1575 status.oa_status |= DRM_XE_OASTATUS_MMIO_TRG_Q_FULL;
1576
1577 if (copy_to_user(uaddr, &status, sizeof(status)))
1578 return -EFAULT;
1579
1580 return 0;
1581 }
1582
xe_oa_info_locked(struct xe_oa_stream * stream,unsigned long arg)1583 static long xe_oa_info_locked(struct xe_oa_stream *stream, unsigned long arg)
1584 {
1585 struct drm_xe_oa_stream_info info = { .oa_buf_size = xe_bo_size(stream->oa_buffer.bo), };
1586 void __user *uaddr = (void __user *)arg;
1587
1588 if (copy_to_user(uaddr, &info, sizeof(info)))
1589 return -EFAULT;
1590
1591 return 0;
1592 }
1593
xe_oa_ioctl_locked(struct xe_oa_stream * stream,unsigned int cmd,unsigned long arg)1594 static long xe_oa_ioctl_locked(struct xe_oa_stream *stream,
1595 unsigned int cmd,
1596 unsigned long arg)
1597 {
1598 switch (cmd) {
1599 case DRM_XE_OBSERVATION_IOCTL_ENABLE:
1600 return xe_oa_enable_locked(stream);
1601 case DRM_XE_OBSERVATION_IOCTL_DISABLE:
1602 return xe_oa_disable_locked(stream);
1603 case DRM_XE_OBSERVATION_IOCTL_CONFIG:
1604 return xe_oa_config_locked(stream, arg);
1605 case DRM_XE_OBSERVATION_IOCTL_STATUS:
1606 return xe_oa_status_locked(stream, arg);
1607 case DRM_XE_OBSERVATION_IOCTL_INFO:
1608 return xe_oa_info_locked(stream, arg);
1609 }
1610
1611 return -EINVAL;
1612 }
1613
xe_oa_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1614 static long xe_oa_ioctl(struct file *file,
1615 unsigned int cmd,
1616 unsigned long arg)
1617 {
1618 struct xe_oa_stream *stream = file->private_data;
1619 long ret;
1620
1621 mutex_lock(&stream->stream_lock);
1622 ret = xe_oa_ioctl_locked(stream, cmd, arg);
1623 mutex_unlock(&stream->stream_lock);
1624
1625 return ret;
1626 }
1627
xe_oa_destroy_locked(struct xe_oa_stream * stream)1628 static void xe_oa_destroy_locked(struct xe_oa_stream *stream)
1629 {
1630 if (stream->enabled)
1631 xe_oa_disable_locked(stream);
1632
1633 xe_oa_stream_destroy(stream);
1634
1635 if (stream->exec_q)
1636 xe_exec_queue_put(stream->exec_q);
1637
1638 kfree(stream);
1639 }
1640
xe_oa_release(struct inode * inode,struct file * file)1641 static int xe_oa_release(struct inode *inode, struct file *file)
1642 {
1643 struct xe_oa_stream *stream = file->private_data;
1644 struct xe_gt *gt = stream->gt;
1645
1646 xe_pm_runtime_get(gt_to_xe(gt));
1647 mutex_lock(>->oa.gt_lock);
1648 xe_oa_destroy_locked(stream);
1649 mutex_unlock(>->oa.gt_lock);
1650 xe_pm_runtime_put(gt_to_xe(gt));
1651
1652 /* Release the reference the OA stream kept on the driver */
1653 drm_dev_put(>_to_xe(gt)->drm);
1654
1655 return 0;
1656 }
1657
xe_oa_mmap(struct file * file,struct vm_area_struct * vma)1658 static int xe_oa_mmap(struct file *file, struct vm_area_struct *vma)
1659 {
1660 struct xe_oa_stream *stream = file->private_data;
1661 struct xe_bo *bo = stream->oa_buffer.bo;
1662 unsigned long start = vma->vm_start;
1663 int i, ret;
1664
1665 if (xe_observation_paranoid && !perfmon_capable()) {
1666 drm_dbg(&stream->oa->xe->drm, "Insufficient privilege to map OA buffer\n");
1667 return -EACCES;
1668 }
1669
1670 /* Can mmap the entire OA buffer or nothing (no partial OA buffer mmaps) */
1671 if (vma->vm_end - vma->vm_start != xe_bo_size(stream->oa_buffer.bo)) {
1672 drm_dbg(&stream->oa->xe->drm, "Wrong mmap size, must be OA buffer size\n");
1673 return -EINVAL;
1674 }
1675
1676 /*
1677 * Only support VM_READ, enforce MAP_PRIVATE by checking for
1678 * VM_MAYSHARE, don't copy the vma on fork
1679 */
1680 if (vma->vm_flags & (VM_WRITE | VM_EXEC | VM_SHARED | VM_MAYSHARE)) {
1681 drm_dbg(&stream->oa->xe->drm, "mmap must be read only\n");
1682 return -EINVAL;
1683 }
1684 vm_flags_mod(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_DONTCOPY,
1685 VM_MAYWRITE | VM_MAYEXEC);
1686
1687 xe_assert(stream->oa->xe, bo->ttm.ttm->num_pages == vma_pages(vma));
1688 for (i = 0; i < bo->ttm.ttm->num_pages; i++) {
1689 ret = remap_pfn_range(vma, start, page_to_pfn(bo->ttm.ttm->pages[i]),
1690 PAGE_SIZE, vma->vm_page_prot);
1691 if (ret)
1692 break;
1693
1694 start += PAGE_SIZE;
1695 }
1696
1697 return ret;
1698 }
1699
1700 static const struct file_operations xe_oa_fops = {
1701 .owner = THIS_MODULE,
1702 .release = xe_oa_release,
1703 .poll = xe_oa_poll,
1704 .read = xe_oa_read,
1705 .unlocked_ioctl = xe_oa_ioctl,
1706 .mmap = xe_oa_mmap,
1707 };
1708
xe_oa_stream_init(struct xe_oa_stream * stream,struct xe_oa_open_param * param)1709 static int xe_oa_stream_init(struct xe_oa_stream *stream,
1710 struct xe_oa_open_param *param)
1711 {
1712 struct xe_gt *gt = param->hwe->gt;
1713 unsigned int fw_ref;
1714 int ret;
1715
1716 stream->exec_q = param->exec_q;
1717 stream->poll_period_ns = DEFAULT_POLL_PERIOD_NS;
1718 stream->oa_unit = param->oa_unit;
1719 stream->hwe = param->hwe;
1720 stream->gt = stream->hwe->gt;
1721 stream->oa_buffer.format = &stream->oa->oa_formats[param->oa_format];
1722
1723 stream->sample = param->sample;
1724 stream->periodic = param->period_exponent >= 0;
1725 stream->period_exponent = param->period_exponent;
1726 stream->no_preempt = param->no_preempt;
1727 stream->wait_num_reports = param->wait_num_reports;
1728
1729 stream->xef = xe_file_get(param->xef);
1730 stream->num_syncs = param->num_syncs;
1731 stream->syncs = param->syncs;
1732
1733 /*
1734 * For Xe2+, when overrun mode is enabled, there are no partial reports at the end
1735 * of buffer, making the OA buffer effectively a non-power-of-2 size circular
1736 * buffer whose size, circ_size, is a multiple of the report size
1737 */
1738 if (GRAPHICS_VER(stream->oa->xe) >= 20 &&
1739 stream->oa_unit->type == DRM_XE_OA_UNIT_TYPE_OAG && stream->sample)
1740 stream->oa_buffer.circ_size =
1741 param->oa_buffer_size -
1742 param->oa_buffer_size % stream->oa_buffer.format->size;
1743 else
1744 stream->oa_buffer.circ_size = param->oa_buffer_size;
1745
1746 stream->oa_config = xe_oa_get_oa_config(stream->oa, param->metric_set);
1747 if (!stream->oa_config) {
1748 drm_dbg(&stream->oa->xe->drm, "Invalid OA config id=%i\n", param->metric_set);
1749 ret = -EINVAL;
1750 goto exit;
1751 }
1752
1753 /*
1754 * GuC reset of engines causes OA to lose configuration
1755 * state. Prevent this by overriding GUCRC mode.
1756 */
1757 if (XE_WA(stream->gt, 1509372804)) {
1758 ret = xe_guc_pc_override_gucrc_mode(>->uc.guc.pc,
1759 SLPC_GUCRC_MODE_GUCRC_NO_RC6);
1760 if (ret)
1761 goto err_free_configs;
1762
1763 stream->override_gucrc = true;
1764 }
1765
1766 /* Take runtime pm ref and forcewake to disable RC6 */
1767 xe_pm_runtime_get(stream->oa->xe);
1768 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FORCEWAKE_ALL);
1769 if (!xe_force_wake_ref_has_domain(fw_ref, XE_FORCEWAKE_ALL)) {
1770 ret = -ETIMEDOUT;
1771 goto err_fw_put;
1772 }
1773
1774 ret = xe_oa_alloc_oa_buffer(stream, param->oa_buffer_size);
1775 if (ret)
1776 goto err_fw_put;
1777
1778 stream->k_exec_q = xe_exec_queue_create(stream->oa->xe, NULL,
1779 BIT(stream->hwe->logical_instance), 1,
1780 stream->hwe, EXEC_QUEUE_FLAG_KERNEL, 0);
1781 if (IS_ERR(stream->k_exec_q)) {
1782 ret = PTR_ERR(stream->k_exec_q);
1783 drm_err(&stream->oa->xe->drm, "gt%d, hwe %s, xe_exec_queue_create failed=%d",
1784 stream->gt->info.id, stream->hwe->name, ret);
1785 goto err_free_oa_buf;
1786 }
1787
1788 ret = xe_oa_enable_metric_set(stream);
1789 if (ret) {
1790 drm_dbg(&stream->oa->xe->drm, "Unable to enable metric set\n");
1791 goto err_put_k_exec_q;
1792 }
1793
1794 drm_dbg(&stream->oa->xe->drm, "opening stream oa config uuid=%s\n",
1795 stream->oa_config->uuid);
1796
1797 WRITE_ONCE(stream->oa_unit->exclusive_stream, stream);
1798
1799 hrtimer_setup(&stream->poll_check_timer, xe_oa_poll_check_timer_cb, CLOCK_MONOTONIC,
1800 HRTIMER_MODE_REL);
1801 init_waitqueue_head(&stream->poll_wq);
1802
1803 spin_lock_init(&stream->oa_buffer.ptr_lock);
1804 mutex_init(&stream->stream_lock);
1805
1806 return 0;
1807
1808 err_put_k_exec_q:
1809 xe_oa_disable_metric_set(stream);
1810 xe_exec_queue_put(stream->k_exec_q);
1811 err_free_oa_buf:
1812 xe_oa_free_oa_buffer(stream);
1813 err_fw_put:
1814 xe_force_wake_put(gt_to_fw(gt), fw_ref);
1815 xe_pm_runtime_put(stream->oa->xe);
1816 if (stream->override_gucrc)
1817 xe_gt_WARN_ON(gt, xe_guc_pc_unset_gucrc_mode(>->uc.guc.pc));
1818 err_free_configs:
1819 xe_oa_free_configs(stream);
1820 exit:
1821 xe_file_put(stream->xef);
1822 return ret;
1823 }
1824
xe_oa_stream_open_ioctl_locked(struct xe_oa * oa,struct xe_oa_open_param * param)1825 static int xe_oa_stream_open_ioctl_locked(struct xe_oa *oa,
1826 struct xe_oa_open_param *param)
1827 {
1828 struct xe_oa_stream *stream;
1829 int stream_fd;
1830 int ret;
1831
1832 /* We currently only allow exclusive access */
1833 if (param->oa_unit->exclusive_stream) {
1834 drm_dbg(&oa->xe->drm, "OA unit already in use\n");
1835 ret = -EBUSY;
1836 goto exit;
1837 }
1838
1839 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
1840 if (!stream) {
1841 ret = -ENOMEM;
1842 goto exit;
1843 }
1844
1845 stream->oa = oa;
1846 ret = xe_oa_stream_init(stream, param);
1847 if (ret)
1848 goto err_free;
1849
1850 if (!param->disabled) {
1851 ret = xe_oa_enable_locked(stream);
1852 if (ret)
1853 goto err_destroy;
1854 }
1855
1856 stream_fd = anon_inode_getfd("[xe_oa]", &xe_oa_fops, stream, 0);
1857 if (stream_fd < 0) {
1858 ret = stream_fd;
1859 goto err_disable;
1860 }
1861
1862 /* Hold a reference on the drm device till stream_fd is released */
1863 drm_dev_get(&stream->oa->xe->drm);
1864
1865 return stream_fd;
1866 err_disable:
1867 if (!param->disabled)
1868 xe_oa_disable_locked(stream);
1869 err_destroy:
1870 xe_oa_stream_destroy(stream);
1871 err_free:
1872 kfree(stream);
1873 exit:
1874 return ret;
1875 }
1876
1877 /**
1878 * xe_oa_timestamp_frequency - Return OA timestamp frequency
1879 * @gt: @xe_gt
1880 *
1881 * OA timestamp frequency = CS timestamp frequency in most platforms. On some
1882 * platforms OA unit ignores the CTC_SHIFT and the 2 timestamps differ. In such
1883 * cases, return the adjusted CS timestamp frequency to the user.
1884 */
xe_oa_timestamp_frequency(struct xe_gt * gt)1885 u32 xe_oa_timestamp_frequency(struct xe_gt *gt)
1886 {
1887 u32 reg, shift;
1888
1889 if (XE_WA(gt, 18013179988) || XE_WA(gt, 14015568240)) {
1890 xe_pm_runtime_get(gt_to_xe(gt));
1891 reg = xe_mmio_read32(>->mmio, RPM_CONFIG0);
1892 xe_pm_runtime_put(gt_to_xe(gt));
1893
1894 shift = REG_FIELD_GET(RPM_CONFIG0_CTC_SHIFT_PARAMETER_MASK, reg);
1895 return gt->info.reference_clock << (3 - shift);
1896 } else {
1897 return gt->info.reference_clock;
1898 }
1899 }
1900
oa_exponent_to_ns(struct xe_gt * gt,int exponent)1901 static u64 oa_exponent_to_ns(struct xe_gt *gt, int exponent)
1902 {
1903 u64 nom = (2ULL << exponent) * NSEC_PER_SEC;
1904 u32 den = xe_oa_timestamp_frequency(gt);
1905
1906 return div_u64(nom + den - 1, den);
1907 }
1908
oa_unit_supports_oa_format(struct xe_oa_open_param * param,int type)1909 static bool oa_unit_supports_oa_format(struct xe_oa_open_param *param, int type)
1910 {
1911 switch (param->oa_unit->type) {
1912 case DRM_XE_OA_UNIT_TYPE_OAG:
1913 return type == DRM_XE_OA_FMT_TYPE_OAG || type == DRM_XE_OA_FMT_TYPE_OAR ||
1914 type == DRM_XE_OA_FMT_TYPE_OAC || type == DRM_XE_OA_FMT_TYPE_PEC;
1915 case DRM_XE_OA_UNIT_TYPE_OAM:
1916 case DRM_XE_OA_UNIT_TYPE_OAM_SAG:
1917 return type == DRM_XE_OA_FMT_TYPE_OAM || type == DRM_XE_OA_FMT_TYPE_OAM_MPEC;
1918 default:
1919 return false;
1920 }
1921 }
1922
1923 /**
1924 * xe_oa_unit_id - Return OA unit ID for a hardware engine
1925 * @hwe: @xe_hw_engine
1926 *
1927 * Return OA unit ID for a hardware engine when available
1928 */
xe_oa_unit_id(struct xe_hw_engine * hwe)1929 u16 xe_oa_unit_id(struct xe_hw_engine *hwe)
1930 {
1931 return hwe->oa_unit && hwe->oa_unit->num_engines ?
1932 hwe->oa_unit->oa_unit_id : U16_MAX;
1933 }
1934
1935 /* A hwe must be assigned to stream/oa_unit for batch submissions */
xe_oa_assign_hwe(struct xe_oa * oa,struct xe_oa_open_param * param)1936 static int xe_oa_assign_hwe(struct xe_oa *oa, struct xe_oa_open_param *param)
1937 {
1938 struct xe_hw_engine *hwe;
1939 enum xe_hw_engine_id id;
1940 int ret = 0;
1941
1942 /* If not provided, OA unit defaults to OA unit 0 as per uapi */
1943 if (!param->oa_unit)
1944 param->oa_unit = &xe_root_mmio_gt(oa->xe)->oa.oa_unit[0];
1945
1946 /* When we have an exec_q, get hwe from the exec_q */
1947 if (param->exec_q) {
1948 param->hwe = xe_gt_hw_engine(param->exec_q->gt, param->exec_q->class,
1949 param->engine_instance, true);
1950 if (!param->hwe || param->hwe->oa_unit != param->oa_unit)
1951 goto err;
1952 goto out;
1953 }
1954
1955 /* Else just get the first hwe attached to the oa unit */
1956 for_each_hw_engine(hwe, param->oa_unit->gt, id) {
1957 if (hwe->oa_unit == param->oa_unit) {
1958 param->hwe = hwe;
1959 goto out;
1960 }
1961 }
1962
1963 /* If we still didn't find a hwe, just get one with a valid oa_unit from the same gt */
1964 for_each_hw_engine(hwe, param->oa_unit->gt, id) {
1965 if (!hwe->oa_unit)
1966 continue;
1967
1968 param->hwe = hwe;
1969 goto out;
1970 }
1971 err:
1972 drm_dbg(&oa->xe->drm, "Unable to find hwe (%d, %d) for OA unit ID %d\n",
1973 param->exec_q ? param->exec_q->class : -1,
1974 param->engine_instance, param->oa_unit->oa_unit_id);
1975 ret = -EINVAL;
1976 out:
1977 return ret;
1978 }
1979
1980 /**
1981 * xe_oa_stream_open_ioctl - Opens an OA stream
1982 * @dev: @drm_device
1983 * @data: pointer to struct @drm_xe_oa_config
1984 * @file: @drm_file
1985 *
1986 * The functions opens an OA stream. An OA stream, opened with specified
1987 * properties, enables OA counter samples to be collected, either
1988 * periodically (time based sampling), or on request (using OA queries)
1989 */
xe_oa_stream_open_ioctl(struct drm_device * dev,u64 data,struct drm_file * file)1990 int xe_oa_stream_open_ioctl(struct drm_device *dev, u64 data, struct drm_file *file)
1991 {
1992 struct xe_device *xe = to_xe_device(dev);
1993 struct xe_oa *oa = &xe->oa;
1994 struct xe_file *xef = to_xe_file(file);
1995 struct xe_oa_open_param param = {};
1996 const struct xe_oa_format *f;
1997 bool privileged_op = true;
1998 int ret;
1999
2000 if (!oa->xe) {
2001 drm_dbg(&xe->drm, "xe oa interface not available for this system\n");
2002 return -ENODEV;
2003 }
2004
2005 param.xef = xef;
2006 param.period_exponent = -1;
2007 ret = xe_oa_user_extensions(oa, XE_OA_USER_EXTN_FROM_OPEN, data, 0, ¶m);
2008 if (ret)
2009 return ret;
2010
2011 if (param.exec_queue_id > 0) {
2012 param.exec_q = xe_exec_queue_lookup(xef, param.exec_queue_id);
2013 if (XE_IOCTL_DBG(oa->xe, !param.exec_q))
2014 return -ENOENT;
2015
2016 if (XE_IOCTL_DBG(oa->xe, param.exec_q->width > 1))
2017 return -EOPNOTSUPP;
2018 }
2019
2020 /*
2021 * Query based sampling (using MI_REPORT_PERF_COUNT) with OAR/OAC,
2022 * without global stream access, can be an unprivileged operation
2023 */
2024 if (param.exec_q && !param.sample)
2025 privileged_op = false;
2026
2027 if (param.no_preempt) {
2028 if (!param.exec_q) {
2029 drm_dbg(&oa->xe->drm, "Preemption disable without exec_q!\n");
2030 ret = -EINVAL;
2031 goto err_exec_q;
2032 }
2033 privileged_op = true;
2034 }
2035
2036 if (privileged_op && xe_observation_paranoid && !perfmon_capable()) {
2037 drm_dbg(&oa->xe->drm, "Insufficient privileges to open xe OA stream\n");
2038 ret = -EACCES;
2039 goto err_exec_q;
2040 }
2041
2042 if (!param.exec_q && !param.sample) {
2043 drm_dbg(&oa->xe->drm, "Only OA report sampling supported\n");
2044 ret = -EINVAL;
2045 goto err_exec_q;
2046 }
2047
2048 ret = xe_oa_assign_hwe(oa, ¶m);
2049 if (ret)
2050 goto err_exec_q;
2051
2052 f = &oa->oa_formats[param.oa_format];
2053 if (!param.oa_format || !f->size ||
2054 !oa_unit_supports_oa_format(¶m, f->type)) {
2055 drm_dbg(&oa->xe->drm, "Invalid OA format %d type %d size %d for class %d\n",
2056 param.oa_format, f->type, f->size, param.hwe->class);
2057 ret = -EINVAL;
2058 goto err_exec_q;
2059 }
2060
2061 if (param.period_exponent >= 0) {
2062 u64 oa_period, oa_freq_hz;
2063
2064 /* Requesting samples from OAG buffer is a privileged operation */
2065 if (!param.sample) {
2066 drm_dbg(&oa->xe->drm, "OA_EXPONENT specified without SAMPLE_OA\n");
2067 ret = -EINVAL;
2068 goto err_exec_q;
2069 }
2070 oa_period = oa_exponent_to_ns(param.hwe->gt, param.period_exponent);
2071 oa_freq_hz = div64_u64(NSEC_PER_SEC, oa_period);
2072 drm_dbg(&oa->xe->drm, "Using periodic sampling freq %lld Hz\n", oa_freq_hz);
2073 }
2074
2075 if (!param.oa_buffer_size)
2076 param.oa_buffer_size = DEFAULT_XE_OA_BUFFER_SIZE;
2077
2078 if (!param.wait_num_reports)
2079 param.wait_num_reports = 1;
2080 if (param.wait_num_reports > param.oa_buffer_size / f->size) {
2081 drm_dbg(&oa->xe->drm, "wait_num_reports %d\n", param.wait_num_reports);
2082 ret = -EINVAL;
2083 goto err_exec_q;
2084 }
2085
2086 ret = xe_oa_parse_syncs(oa, ¶m);
2087 if (ret)
2088 goto err_exec_q;
2089
2090 mutex_lock(¶m.hwe->gt->oa.gt_lock);
2091 ret = xe_oa_stream_open_ioctl_locked(oa, ¶m);
2092 mutex_unlock(¶m.hwe->gt->oa.gt_lock);
2093 if (ret < 0)
2094 goto err_sync_cleanup;
2095
2096 return ret;
2097
2098 err_sync_cleanup:
2099 while (param.num_syncs--)
2100 xe_sync_entry_cleanup(¶m.syncs[param.num_syncs]);
2101 kfree(param.syncs);
2102 err_exec_q:
2103 if (param.exec_q)
2104 xe_exec_queue_put(param.exec_q);
2105 return ret;
2106 }
2107
xe_oa_is_valid_flex_addr(struct xe_oa * oa,u32 addr)2108 static bool xe_oa_is_valid_flex_addr(struct xe_oa *oa, u32 addr)
2109 {
2110 static const struct xe_reg flex_eu_regs[] = {
2111 EU_PERF_CNTL0,
2112 EU_PERF_CNTL1,
2113 EU_PERF_CNTL2,
2114 EU_PERF_CNTL3,
2115 EU_PERF_CNTL4,
2116 EU_PERF_CNTL5,
2117 EU_PERF_CNTL6,
2118 };
2119 int i;
2120
2121 for (i = 0; i < ARRAY_SIZE(flex_eu_regs); i++) {
2122 if (flex_eu_regs[i].addr == addr)
2123 return true;
2124 }
2125 return false;
2126 }
2127
xe_oa_reg_in_range_table(u32 addr,const struct xe_mmio_range * table)2128 static bool xe_oa_reg_in_range_table(u32 addr, const struct xe_mmio_range *table)
2129 {
2130 while (table->start && table->end) {
2131 if (addr >= table->start && addr <= table->end)
2132 return true;
2133
2134 table++;
2135 }
2136
2137 return false;
2138 }
2139
2140 static const struct xe_mmio_range xehp_oa_b_counters[] = {
2141 { .start = 0xdc48, .end = 0xdc48 }, /* OAA_ENABLE_REG */
2142 { .start = 0xdd00, .end = 0xdd48 }, /* OAG_LCE0_0 - OAA_LENABLE_REG */
2143 {}
2144 };
2145
2146 static const struct xe_mmio_range gen12_oa_b_counters[] = {
2147 { .start = 0x2b2c, .end = 0x2b2c }, /* OAG_OA_PESS */
2148 { .start = 0xd900, .end = 0xd91c }, /* OAG_OASTARTTRIG[1-8] */
2149 { .start = 0xd920, .end = 0xd93c }, /* OAG_OAREPORTTRIG1[1-8] */
2150 { .start = 0xd940, .end = 0xd97c }, /* OAG_CEC[0-7][0-1] */
2151 { .start = 0xdc00, .end = 0xdc3c }, /* OAG_SCEC[0-7][0-1] */
2152 { .start = 0xdc40, .end = 0xdc40 }, /* OAG_SPCTR_CNF */
2153 { .start = 0xdc44, .end = 0xdc44 }, /* OAA_DBG_REG */
2154 {}
2155 };
2156
2157 static const struct xe_mmio_range mtl_oam_b_counters[] = {
2158 { .start = 0x393000, .end = 0x39301c }, /* OAM_STARTTRIG1[1-8] */
2159 { .start = 0x393020, .end = 0x39303c }, /* OAM_REPORTTRIG1[1-8] */
2160 { .start = 0x393040, .end = 0x39307c }, /* OAM_CEC[0-7][0-1] */
2161 { .start = 0x393200, .end = 0x39323C }, /* MPES[0-7] */
2162 {}
2163 };
2164
2165 static const struct xe_mmio_range xe2_oa_b_counters[] = {
2166 { .start = 0x393200, .end = 0x39323C }, /* MPES_0_MPES_SAG - MPES_7_UPPER_MPES_SAG */
2167 { .start = 0x394200, .end = 0x39423C }, /* MPES_0_MPES_SCMI0 - MPES_7_UPPER_MPES_SCMI0 */
2168 { .start = 0x394A00, .end = 0x394A3C }, /* MPES_0_MPES_SCMI1 - MPES_7_UPPER_MPES_SCMI1 */
2169 {},
2170 };
2171
xe_oa_is_valid_b_counter_addr(struct xe_oa * oa,u32 addr)2172 static bool xe_oa_is_valid_b_counter_addr(struct xe_oa *oa, u32 addr)
2173 {
2174 return xe_oa_reg_in_range_table(addr, xehp_oa_b_counters) ||
2175 xe_oa_reg_in_range_table(addr, gen12_oa_b_counters) ||
2176 xe_oa_reg_in_range_table(addr, mtl_oam_b_counters) ||
2177 (GRAPHICS_VER(oa->xe) >= 20 &&
2178 xe_oa_reg_in_range_table(addr, xe2_oa_b_counters));
2179 }
2180
2181 static const struct xe_mmio_range mtl_oa_mux_regs[] = {
2182 { .start = 0x0d00, .end = 0x0d04 }, /* RPM_CONFIG[0-1] */
2183 { .start = 0x0d0c, .end = 0x0d2c }, /* NOA_CONFIG[0-8] */
2184 { .start = 0x9840, .end = 0x9840 }, /* GDT_CHICKEN_BITS */
2185 { .start = 0x9884, .end = 0x9888 }, /* NOA_WRITE */
2186 { .start = 0x38d100, .end = 0x38d114}, /* VISACTL */
2187 {}
2188 };
2189
2190 static const struct xe_mmio_range gen12_oa_mux_regs[] = {
2191 { .start = 0x0d00, .end = 0x0d04 }, /* RPM_CONFIG[0-1] */
2192 { .start = 0x0d0c, .end = 0x0d2c }, /* NOA_CONFIG[0-8] */
2193 { .start = 0x9840, .end = 0x9840 }, /* GDT_CHICKEN_BITS */
2194 { .start = 0x9884, .end = 0x9888 }, /* NOA_WRITE */
2195 { .start = 0x20cc, .end = 0x20cc }, /* WAIT_FOR_RC6_EXIT */
2196 {}
2197 };
2198
2199 static const struct xe_mmio_range xe2_oa_mux_regs[] = {
2200 { .start = 0x5194, .end = 0x5194 }, /* SYS_MEM_LAT_MEASURE_MERTF_GRP_3D */
2201 { .start = 0x8704, .end = 0x8704 }, /* LMEM_LAT_MEASURE_MCFG_GRP */
2202 { .start = 0xB01C, .end = 0xB01C }, /* LNCF_MISC_CONFIG_REGISTER0 */
2203 { .start = 0xB1BC, .end = 0xB1BC }, /* L3_BANK_LAT_MEASURE_LBCF_GFX */
2204 { .start = 0xD0E0, .end = 0xD0F4 }, /* VISACTL */
2205 { .start = 0xE18C, .end = 0xE18C }, /* SAMPLER_MODE */
2206 { .start = 0xE590, .end = 0xE590 }, /* TDL_LSC_LAT_MEASURE_TDL_GFX */
2207 { .start = 0x13000, .end = 0x137FC }, /* PES_0_PESL0 - PES_63_UPPER_PESL3 */
2208 {},
2209 };
2210
xe_oa_is_valid_mux_addr(struct xe_oa * oa,u32 addr)2211 static bool xe_oa_is_valid_mux_addr(struct xe_oa *oa, u32 addr)
2212 {
2213 if (GRAPHICS_VER(oa->xe) >= 20)
2214 return xe_oa_reg_in_range_table(addr, xe2_oa_mux_regs);
2215 else if (GRAPHICS_VERx100(oa->xe) >= 1270)
2216 return xe_oa_reg_in_range_table(addr, mtl_oa_mux_regs);
2217 else
2218 return xe_oa_reg_in_range_table(addr, gen12_oa_mux_regs);
2219 }
2220
xe_oa_is_valid_config_reg_addr(struct xe_oa * oa,u32 addr)2221 static bool xe_oa_is_valid_config_reg_addr(struct xe_oa *oa, u32 addr)
2222 {
2223 return xe_oa_is_valid_flex_addr(oa, addr) ||
2224 xe_oa_is_valid_b_counter_addr(oa, addr) ||
2225 xe_oa_is_valid_mux_addr(oa, addr);
2226 }
2227
2228 static struct xe_oa_reg *
xe_oa_alloc_regs(struct xe_oa * oa,bool (* is_valid)(struct xe_oa * oa,u32 addr),u32 __user * regs,u32 n_regs)2229 xe_oa_alloc_regs(struct xe_oa *oa, bool (*is_valid)(struct xe_oa *oa, u32 addr),
2230 u32 __user *regs, u32 n_regs)
2231 {
2232 struct xe_oa_reg *oa_regs;
2233 int err;
2234 u32 i;
2235
2236 oa_regs = kmalloc_array(n_regs, sizeof(*oa_regs), GFP_KERNEL);
2237 if (!oa_regs)
2238 return ERR_PTR(-ENOMEM);
2239
2240 for (i = 0; i < n_regs; i++) {
2241 u32 addr, value;
2242
2243 err = get_user(addr, regs);
2244 if (err)
2245 goto addr_err;
2246
2247 if (!is_valid(oa, addr)) {
2248 drm_dbg(&oa->xe->drm, "Invalid oa_reg address: %X\n", addr);
2249 err = -EINVAL;
2250 goto addr_err;
2251 }
2252
2253 err = get_user(value, regs + 1);
2254 if (err)
2255 goto addr_err;
2256
2257 oa_regs[i].addr = XE_REG(addr);
2258 oa_regs[i].value = value;
2259
2260 regs += 2;
2261 }
2262
2263 return oa_regs;
2264
2265 addr_err:
2266 kfree(oa_regs);
2267 return ERR_PTR(err);
2268 }
2269 ALLOW_ERROR_INJECTION(xe_oa_alloc_regs, ERRNO);
2270
show_dynamic_id(struct kobject * kobj,struct kobj_attribute * attr,char * buf)2271 static ssize_t show_dynamic_id(struct kobject *kobj,
2272 struct kobj_attribute *attr,
2273 char *buf)
2274 {
2275 struct xe_oa_config *oa_config =
2276 container_of(attr, typeof(*oa_config), sysfs_metric_id);
2277
2278 return sysfs_emit(buf, "%d\n", oa_config->id);
2279 }
2280
create_dynamic_oa_sysfs_entry(struct xe_oa * oa,struct xe_oa_config * oa_config)2281 static int create_dynamic_oa_sysfs_entry(struct xe_oa *oa,
2282 struct xe_oa_config *oa_config)
2283 {
2284 sysfs_attr_init(&oa_config->sysfs_metric_id.attr);
2285 oa_config->sysfs_metric_id.attr.name = "id";
2286 oa_config->sysfs_metric_id.attr.mode = 0444;
2287 oa_config->sysfs_metric_id.show = show_dynamic_id;
2288 oa_config->sysfs_metric_id.store = NULL;
2289
2290 oa_config->attrs[0] = &oa_config->sysfs_metric_id.attr;
2291 oa_config->attrs[1] = NULL;
2292
2293 oa_config->sysfs_metric.name = oa_config->uuid;
2294 oa_config->sysfs_metric.attrs = oa_config->attrs;
2295
2296 return sysfs_create_group(oa->metrics_kobj, &oa_config->sysfs_metric);
2297 }
2298
2299 /**
2300 * xe_oa_add_config_ioctl - Adds one OA config
2301 * @dev: @drm_device
2302 * @data: pointer to struct @drm_xe_oa_config
2303 * @file: @drm_file
2304 *
2305 * The functions adds an OA config to the set of OA configs maintained in
2306 * the kernel. The config determines which OA metrics are collected for an
2307 * OA stream.
2308 */
xe_oa_add_config_ioctl(struct drm_device * dev,u64 data,struct drm_file * file)2309 int xe_oa_add_config_ioctl(struct drm_device *dev, u64 data, struct drm_file *file)
2310 {
2311 struct xe_device *xe = to_xe_device(dev);
2312 struct xe_oa *oa = &xe->oa;
2313 struct drm_xe_oa_config param;
2314 struct drm_xe_oa_config *arg = ¶m;
2315 struct xe_oa_config *oa_config, *tmp;
2316 struct xe_oa_reg *regs;
2317 int err, id;
2318
2319 if (!oa->xe) {
2320 drm_dbg(&xe->drm, "xe oa interface not available for this system\n");
2321 return -ENODEV;
2322 }
2323
2324 if (xe_observation_paranoid && !perfmon_capable()) {
2325 drm_dbg(&oa->xe->drm, "Insufficient privileges to add xe OA config\n");
2326 return -EACCES;
2327 }
2328
2329 err = copy_from_user(¶m, u64_to_user_ptr(data), sizeof(param));
2330 if (XE_IOCTL_DBG(oa->xe, err))
2331 return -EFAULT;
2332
2333 if (XE_IOCTL_DBG(oa->xe, arg->extensions) ||
2334 XE_IOCTL_DBG(oa->xe, !arg->regs_ptr) ||
2335 XE_IOCTL_DBG(oa->xe, !arg->n_regs))
2336 return -EINVAL;
2337
2338 oa_config = kzalloc(sizeof(*oa_config), GFP_KERNEL);
2339 if (!oa_config)
2340 return -ENOMEM;
2341
2342 oa_config->oa = oa;
2343 kref_init(&oa_config->ref);
2344
2345 if (!uuid_is_valid(arg->uuid)) {
2346 drm_dbg(&oa->xe->drm, "Invalid uuid format for OA config\n");
2347 err = -EINVAL;
2348 goto reg_err;
2349 }
2350
2351 /* Last character in oa_config->uuid will be 0 because oa_config is kzalloc */
2352 memcpy(oa_config->uuid, arg->uuid, sizeof(arg->uuid));
2353
2354 oa_config->regs_len = arg->n_regs;
2355 regs = xe_oa_alloc_regs(oa, xe_oa_is_valid_config_reg_addr,
2356 u64_to_user_ptr(arg->regs_ptr),
2357 arg->n_regs);
2358 if (IS_ERR(regs)) {
2359 drm_dbg(&oa->xe->drm, "Failed to create OA config for mux_regs\n");
2360 err = PTR_ERR(regs);
2361 goto reg_err;
2362 }
2363 oa_config->regs = regs;
2364
2365 err = mutex_lock_interruptible(&oa->metrics_lock);
2366 if (err)
2367 goto reg_err;
2368
2369 /* We shouldn't have too many configs, so this iteration shouldn't be too costly */
2370 idr_for_each_entry(&oa->metrics_idr, tmp, id) {
2371 if (!strcmp(tmp->uuid, oa_config->uuid)) {
2372 drm_dbg(&oa->xe->drm, "OA config already exists with this uuid\n");
2373 err = -EADDRINUSE;
2374 goto sysfs_err;
2375 }
2376 }
2377
2378 err = create_dynamic_oa_sysfs_entry(oa, oa_config);
2379 if (err) {
2380 drm_dbg(&oa->xe->drm, "Failed to create sysfs entry for OA config\n");
2381 goto sysfs_err;
2382 }
2383
2384 oa_config->id = idr_alloc(&oa->metrics_idr, oa_config, 1, 0, GFP_KERNEL);
2385 if (oa_config->id < 0) {
2386 drm_dbg(&oa->xe->drm, "Failed to create sysfs entry for OA config\n");
2387 err = oa_config->id;
2388 goto sysfs_err;
2389 }
2390
2391 mutex_unlock(&oa->metrics_lock);
2392
2393 drm_dbg(&oa->xe->drm, "Added config %s id=%i\n", oa_config->uuid, oa_config->id);
2394
2395 return oa_config->id;
2396
2397 sysfs_err:
2398 mutex_unlock(&oa->metrics_lock);
2399 reg_err:
2400 xe_oa_config_put(oa_config);
2401 drm_dbg(&oa->xe->drm, "Failed to add new OA config\n");
2402 return err;
2403 }
2404
2405 /**
2406 * xe_oa_remove_config_ioctl - Removes one OA config
2407 * @dev: @drm_device
2408 * @data: pointer to struct @drm_xe_observation_param
2409 * @file: @drm_file
2410 */
xe_oa_remove_config_ioctl(struct drm_device * dev,u64 data,struct drm_file * file)2411 int xe_oa_remove_config_ioctl(struct drm_device *dev, u64 data, struct drm_file *file)
2412 {
2413 struct xe_device *xe = to_xe_device(dev);
2414 struct xe_oa *oa = &xe->oa;
2415 struct xe_oa_config *oa_config;
2416 u64 arg, *ptr = u64_to_user_ptr(data);
2417 int ret;
2418
2419 if (!oa->xe) {
2420 drm_dbg(&xe->drm, "xe oa interface not available for this system\n");
2421 return -ENODEV;
2422 }
2423
2424 if (xe_observation_paranoid && !perfmon_capable()) {
2425 drm_dbg(&oa->xe->drm, "Insufficient privileges to remove xe OA config\n");
2426 return -EACCES;
2427 }
2428
2429 ret = get_user(arg, ptr);
2430 if (XE_IOCTL_DBG(oa->xe, ret))
2431 return ret;
2432
2433 ret = mutex_lock_interruptible(&oa->metrics_lock);
2434 if (ret)
2435 return ret;
2436
2437 oa_config = idr_find(&oa->metrics_idr, arg);
2438 if (!oa_config) {
2439 drm_dbg(&oa->xe->drm, "Failed to remove unknown OA config\n");
2440 ret = -ENOENT;
2441 goto err_unlock;
2442 }
2443
2444 WARN_ON(arg != oa_config->id);
2445
2446 sysfs_remove_group(oa->metrics_kobj, &oa_config->sysfs_metric);
2447 idr_remove(&oa->metrics_idr, arg);
2448
2449 mutex_unlock(&oa->metrics_lock);
2450
2451 drm_dbg(&oa->xe->drm, "Removed config %s id=%i\n", oa_config->uuid, oa_config->id);
2452
2453 xe_oa_config_put(oa_config);
2454
2455 return 0;
2456
2457 err_unlock:
2458 mutex_unlock(&oa->metrics_lock);
2459 return ret;
2460 }
2461
xe_oa_unregister(void * arg)2462 static void xe_oa_unregister(void *arg)
2463 {
2464 struct xe_oa *oa = arg;
2465
2466 if (!oa->metrics_kobj)
2467 return;
2468
2469 kobject_put(oa->metrics_kobj);
2470 oa->metrics_kobj = NULL;
2471 }
2472
2473 /**
2474 * xe_oa_register - Xe OA registration
2475 * @xe: @xe_device
2476 *
2477 * Exposes the metrics sysfs directory upon completion of module initialization
2478 */
xe_oa_register(struct xe_device * xe)2479 int xe_oa_register(struct xe_device *xe)
2480 {
2481 struct xe_oa *oa = &xe->oa;
2482
2483 if (!oa->xe)
2484 return 0;
2485
2486 oa->metrics_kobj = kobject_create_and_add("metrics",
2487 &xe->drm.primary->kdev->kobj);
2488 if (!oa->metrics_kobj)
2489 return -ENOMEM;
2490
2491 return devm_add_action_or_reset(xe->drm.dev, xe_oa_unregister, oa);
2492 }
2493
num_oa_units_per_gt(struct xe_gt * gt)2494 static u32 num_oa_units_per_gt(struct xe_gt *gt)
2495 {
2496 if (xe_gt_is_main_type(gt) || GRAPHICS_VER(gt_to_xe(gt)) < 20)
2497 return 1;
2498 else if (!IS_DGFX(gt_to_xe(gt)))
2499 return XE_OAM_UNIT_SCMI_0 + 1; /* SAG + SCMI_0 */
2500 else
2501 return XE_OAM_UNIT_SCMI_1 + 1; /* SAG + SCMI_0 + SCMI_1 */
2502 }
2503
__hwe_oam_unit(struct xe_hw_engine * hwe)2504 static u32 __hwe_oam_unit(struct xe_hw_engine *hwe)
2505 {
2506 if (GRAPHICS_VERx100(gt_to_xe(hwe->gt)) < 1270)
2507 return XE_OA_UNIT_INVALID;
2508
2509 xe_gt_WARN_ON(hwe->gt, xe_gt_is_main_type(hwe->gt));
2510
2511 if (GRAPHICS_VER(gt_to_xe(hwe->gt)) < 20)
2512 return 0;
2513 /*
2514 * XE_OAM_UNIT_SAG has only GSCCS attached to it, but only on some platforms. Also
2515 * GSCCS cannot be used to submit batches to program the OAM unit. Therefore we don't
2516 * assign an OA unit to GSCCS. This means that XE_OAM_UNIT_SAG is exposed as an OA
2517 * unit without attached engines. Fused off engines can also result in oa_unit's with
2518 * num_engines == 0. OA streams can be opened on all OA units.
2519 */
2520 else if (hwe->engine_id == XE_HW_ENGINE_GSCCS0)
2521 return XE_OA_UNIT_INVALID;
2522 else if (!IS_DGFX(gt_to_xe(hwe->gt)))
2523 return XE_OAM_UNIT_SCMI_0;
2524 else if (hwe->class == XE_ENGINE_CLASS_VIDEO_DECODE)
2525 return (hwe->instance / 2 & 0x1) + 1;
2526 else if (hwe->class == XE_ENGINE_CLASS_VIDEO_ENHANCE)
2527 return (hwe->instance & 0x1) + 1;
2528
2529 return XE_OA_UNIT_INVALID;
2530 }
2531
__hwe_oa_unit(struct xe_hw_engine * hwe)2532 static u32 __hwe_oa_unit(struct xe_hw_engine *hwe)
2533 {
2534 switch (hwe->class) {
2535 case XE_ENGINE_CLASS_RENDER:
2536 case XE_ENGINE_CLASS_COMPUTE:
2537 return 0;
2538
2539 case XE_ENGINE_CLASS_VIDEO_DECODE:
2540 case XE_ENGINE_CLASS_VIDEO_ENHANCE:
2541 case XE_ENGINE_CLASS_OTHER:
2542 return __hwe_oam_unit(hwe);
2543
2544 default:
2545 return XE_OA_UNIT_INVALID;
2546 }
2547 }
2548
__oam_regs(u32 base)2549 static struct xe_oa_regs __oam_regs(u32 base)
2550 {
2551 return (struct xe_oa_regs) {
2552 base,
2553 OAM_HEAD_POINTER(base),
2554 OAM_TAIL_POINTER(base),
2555 OAM_BUFFER(base),
2556 OAM_CONTEXT_CONTROL(base),
2557 OAM_CONTROL(base),
2558 OAM_DEBUG(base),
2559 OAM_STATUS(base),
2560 OAM_CONTROL_COUNTER_SEL_MASK,
2561 };
2562 }
2563
__oag_regs(void)2564 static struct xe_oa_regs __oag_regs(void)
2565 {
2566 return (struct xe_oa_regs) {
2567 0,
2568 OAG_OAHEADPTR,
2569 OAG_OATAILPTR,
2570 OAG_OABUFFER,
2571 OAG_OAGLBCTXCTRL,
2572 OAG_OACONTROL,
2573 OAG_OA_DEBUG,
2574 OAG_OASTATUS,
2575 OAG_OACONTROL_OA_COUNTER_SEL_MASK,
2576 };
2577 }
2578
__xe_oa_init_oa_units(struct xe_gt * gt)2579 static void __xe_oa_init_oa_units(struct xe_gt *gt)
2580 {
2581 /* Actual address is MEDIA_GT_GSI_OFFSET + oam_base_addr[i] */
2582 const u32 oam_base_addr[] = {
2583 [XE_OAM_UNIT_SAG] = 0x13000,
2584 [XE_OAM_UNIT_SCMI_0] = 0x14000,
2585 [XE_OAM_UNIT_SCMI_1] = 0x14800,
2586 };
2587 int i, num_units = gt->oa.num_oa_units;
2588
2589 for (i = 0; i < num_units; i++) {
2590 struct xe_oa_unit *u = >->oa.oa_unit[i];
2591
2592 if (xe_gt_is_main_type(gt)) {
2593 u->regs = __oag_regs();
2594 u->type = DRM_XE_OA_UNIT_TYPE_OAG;
2595 } else {
2596 xe_gt_assert(gt, GRAPHICS_VERx100(gt_to_xe(gt)) >= 1270);
2597 u->regs = __oam_regs(oam_base_addr[i]);
2598 u->type = i == XE_OAM_UNIT_SAG && GRAPHICS_VER(gt_to_xe(gt)) >= 20 ?
2599 DRM_XE_OA_UNIT_TYPE_OAM_SAG : DRM_XE_OA_UNIT_TYPE_OAM;
2600 }
2601
2602 u->gt = gt;
2603
2604 xe_mmio_write32(>->mmio, u->regs.oa_ctrl, 0);
2605
2606 /* Ensure MMIO trigger remains disabled till there is a stream */
2607 xe_mmio_write32(>->mmio, u->regs.oa_debug,
2608 oag_configure_mmio_trigger(NULL, false));
2609
2610 /* Set oa_unit_ids now to ensure ids remain contiguous */
2611 u->oa_unit_id = gt_to_xe(gt)->oa.oa_unit_ids++;
2612 }
2613 }
2614
xe_oa_init_gt(struct xe_gt * gt)2615 static int xe_oa_init_gt(struct xe_gt *gt)
2616 {
2617 u32 num_oa_units = num_oa_units_per_gt(gt);
2618 struct xe_hw_engine *hwe;
2619 enum xe_hw_engine_id id;
2620 struct xe_oa_unit *u;
2621
2622 u = drmm_kcalloc(>_to_xe(gt)->drm, num_oa_units, sizeof(*u), GFP_KERNEL);
2623 if (!u)
2624 return -ENOMEM;
2625
2626 for_each_hw_engine(hwe, gt, id) {
2627 u32 index = __hwe_oa_unit(hwe);
2628
2629 hwe->oa_unit = NULL;
2630 if (index < num_oa_units) {
2631 u[index].num_engines++;
2632 hwe->oa_unit = &u[index];
2633 }
2634 }
2635
2636 gt->oa.num_oa_units = num_oa_units;
2637 gt->oa.oa_unit = u;
2638
2639 __xe_oa_init_oa_units(gt);
2640
2641 drmm_mutex_init(>_to_xe(gt)->drm, >->oa.gt_lock);
2642
2643 return 0;
2644 }
2645
xe_oa_print_gt_oa_units(struct xe_gt * gt)2646 static void xe_oa_print_gt_oa_units(struct xe_gt *gt)
2647 {
2648 enum xe_hw_engine_id hwe_id;
2649 struct xe_hw_engine *hwe;
2650 struct xe_oa_unit *u;
2651 char buf[256];
2652 int i, n;
2653
2654 for (i = 0; i < gt->oa.num_oa_units; i++) {
2655 u = >->oa.oa_unit[i];
2656 buf[0] = '\0';
2657 n = 0;
2658
2659 for_each_hw_engine(hwe, gt, hwe_id)
2660 if (xe_oa_unit_id(hwe) == u->oa_unit_id)
2661 n += scnprintf(buf + n, sizeof(buf) - n, "%s ", hwe->name);
2662
2663 xe_gt_dbg(gt, "oa_unit %d, type %d, Engines: %s\n", u->oa_unit_id, u->type, buf);
2664 }
2665 }
2666
xe_oa_print_oa_units(struct xe_oa * oa)2667 static void xe_oa_print_oa_units(struct xe_oa *oa)
2668 {
2669 struct xe_gt *gt;
2670 int gt_id;
2671
2672 for_each_gt(gt, oa->xe, gt_id)
2673 xe_oa_print_gt_oa_units(gt);
2674 }
2675
xe_oa_init_oa_units(struct xe_oa * oa)2676 static int xe_oa_init_oa_units(struct xe_oa *oa)
2677 {
2678 struct xe_gt *gt;
2679 int i, ret;
2680
2681 /* Needed for OAM implementation here */
2682 BUILD_BUG_ON(XE_OAM_UNIT_SAG != 0);
2683 BUILD_BUG_ON(XE_OAM_UNIT_SCMI_0 != 1);
2684 BUILD_BUG_ON(XE_OAM_UNIT_SCMI_1 != 2);
2685
2686 for_each_gt(gt, oa->xe, i) {
2687 ret = xe_oa_init_gt(gt);
2688 if (ret)
2689 return ret;
2690 }
2691
2692 xe_oa_print_oa_units(oa);
2693
2694 return 0;
2695 }
2696
oa_format_add(struct xe_oa * oa,enum xe_oa_format_name format)2697 static void oa_format_add(struct xe_oa *oa, enum xe_oa_format_name format)
2698 {
2699 __set_bit(format, oa->format_mask);
2700 }
2701
xe_oa_init_supported_formats(struct xe_oa * oa)2702 static void xe_oa_init_supported_formats(struct xe_oa *oa)
2703 {
2704 if (GRAPHICS_VER(oa->xe) >= 20) {
2705 /* Xe2+ */
2706 oa_format_add(oa, XE_OAM_FORMAT_MPEC8u64_B8_C8);
2707 oa_format_add(oa, XE_OAM_FORMAT_MPEC8u32_B8_C8);
2708 oa_format_add(oa, XE_OA_FORMAT_PEC64u64);
2709 oa_format_add(oa, XE_OA_FORMAT_PEC64u64_B8_C8);
2710 oa_format_add(oa, XE_OA_FORMAT_PEC64u32);
2711 oa_format_add(oa, XE_OA_FORMAT_PEC32u64_G1);
2712 oa_format_add(oa, XE_OA_FORMAT_PEC32u32_G1);
2713 oa_format_add(oa, XE_OA_FORMAT_PEC32u64_G2);
2714 oa_format_add(oa, XE_OA_FORMAT_PEC32u32_G2);
2715 oa_format_add(oa, XE_OA_FORMAT_PEC36u64_G1_32_G2_4);
2716 oa_format_add(oa, XE_OA_FORMAT_PEC36u64_G1_4_G2_32);
2717 } else if (GRAPHICS_VERx100(oa->xe) >= 1270) {
2718 /* XE_METEORLAKE */
2719 oa_format_add(oa, XE_OAR_FORMAT_A32u40_A4u32_B8_C8);
2720 oa_format_add(oa, XE_OA_FORMAT_A24u40_A14u32_B8_C8);
2721 oa_format_add(oa, XE_OAC_FORMAT_A24u64_B8_C8);
2722 oa_format_add(oa, XE_OAC_FORMAT_A22u32_R2u32_B8_C8);
2723 oa_format_add(oa, XE_OAM_FORMAT_MPEC8u64_B8_C8);
2724 oa_format_add(oa, XE_OAM_FORMAT_MPEC8u32_B8_C8);
2725 } else if (GRAPHICS_VERx100(oa->xe) >= 1255) {
2726 /* XE_DG2, XE_PVC */
2727 oa_format_add(oa, XE_OAR_FORMAT_A32u40_A4u32_B8_C8);
2728 oa_format_add(oa, XE_OA_FORMAT_A24u40_A14u32_B8_C8);
2729 oa_format_add(oa, XE_OAC_FORMAT_A24u64_B8_C8);
2730 oa_format_add(oa, XE_OAC_FORMAT_A22u32_R2u32_B8_C8);
2731 } else {
2732 /* Gen12+ */
2733 xe_assert(oa->xe, GRAPHICS_VER(oa->xe) >= 12);
2734 oa_format_add(oa, XE_OA_FORMAT_A12);
2735 oa_format_add(oa, XE_OA_FORMAT_A12_B8_C8);
2736 oa_format_add(oa, XE_OA_FORMAT_A32u40_A4u32_B8_C8);
2737 oa_format_add(oa, XE_OA_FORMAT_C4_B8);
2738 }
2739 }
2740
destroy_config(int id,void * p,void * data)2741 static int destroy_config(int id, void *p, void *data)
2742 {
2743 xe_oa_config_put(p);
2744
2745 return 0;
2746 }
2747
xe_oa_fini(void * arg)2748 static void xe_oa_fini(void *arg)
2749 {
2750 struct xe_device *xe = arg;
2751 struct xe_oa *oa = &xe->oa;
2752
2753 if (!oa->xe)
2754 return;
2755
2756 idr_for_each(&oa->metrics_idr, destroy_config, oa);
2757 idr_destroy(&oa->metrics_idr);
2758
2759 oa->xe = NULL;
2760 }
2761
2762 /**
2763 * xe_oa_init - OA initialization during device probe
2764 * @xe: @xe_device
2765 *
2766 * Return: 0 on success or a negative error code on failure
2767 */
xe_oa_init(struct xe_device * xe)2768 int xe_oa_init(struct xe_device *xe)
2769 {
2770 struct xe_oa *oa = &xe->oa;
2771 int ret;
2772
2773 /* Support OA only with GuC submission and Gen12+ */
2774 if (!xe_device_uc_enabled(xe) || GRAPHICS_VER(xe) < 12)
2775 return 0;
2776
2777 if (IS_SRIOV_VF(xe))
2778 return 0;
2779
2780 oa->xe = xe;
2781 oa->oa_formats = oa_formats;
2782
2783 drmm_mutex_init(&oa->xe->drm, &oa->metrics_lock);
2784 idr_init_base(&oa->metrics_idr, 1);
2785
2786 ret = xe_oa_init_oa_units(oa);
2787 if (ret) {
2788 drm_err(&xe->drm, "OA initialization failed (%pe)\n", ERR_PTR(ret));
2789 goto exit;
2790 }
2791
2792 xe_oa_init_supported_formats(oa);
2793
2794 return devm_add_action_or_reset(xe->drm.dev, xe_oa_fini, xe);
2795
2796 exit:
2797 oa->xe = NULL;
2798 return ret;
2799 }
2800