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