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