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