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