18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * SPDX-License-Identifier: MIT
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright © 2017-2018 Intel Corporation
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef __I915_PMU_H__
88c2ecf20Sopenharmony_ci#define __I915_PMU_H__
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/hrtimer.h>
118c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
128c2ecf20Sopenharmony_ci#include <linux/spinlock_types.h>
138c2ecf20Sopenharmony_ci#include <uapi/drm/i915_drm.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistruct drm_i915_private;
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cienum {
188c2ecf20Sopenharmony_ci	__I915_SAMPLE_FREQ_ACT = 0,
198c2ecf20Sopenharmony_ci	__I915_SAMPLE_FREQ_REQ,
208c2ecf20Sopenharmony_ci	__I915_SAMPLE_RC6,
218c2ecf20Sopenharmony_ci	__I915_SAMPLE_RC6_LAST_REPORTED,
228c2ecf20Sopenharmony_ci	__I915_NUM_PMU_SAMPLERS
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/**
268c2ecf20Sopenharmony_ci * How many different events we track in the global PMU mask.
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * It is also used to know to needed number of event reference counters.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci#define I915_PMU_MASK_BITS \
318c2ecf20Sopenharmony_ci	((1 << I915_PMU_SAMPLE_BITS) + \
328c2ecf20Sopenharmony_ci	 (I915_PMU_LAST + 1 - __I915_PMU_OTHER(0)))
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define I915_ENGINE_SAMPLE_COUNT (I915_SAMPLE_SEMA + 1)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistruct i915_pmu_sample {
378c2ecf20Sopenharmony_ci	u64 cur;
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistruct i915_pmu {
418c2ecf20Sopenharmony_ci	/**
428c2ecf20Sopenharmony_ci	 * @cpuhp: Struct used for CPU hotplug handling.
438c2ecf20Sopenharmony_ci	 */
448c2ecf20Sopenharmony_ci	struct {
458c2ecf20Sopenharmony_ci		struct hlist_node node;
468c2ecf20Sopenharmony_ci		enum cpuhp_state slot;
478c2ecf20Sopenharmony_ci	} cpuhp;
488c2ecf20Sopenharmony_ci	/**
498c2ecf20Sopenharmony_ci	 * @base: PMU base.
508c2ecf20Sopenharmony_ci	 */
518c2ecf20Sopenharmony_ci	struct pmu base;
528c2ecf20Sopenharmony_ci	/**
538c2ecf20Sopenharmony_ci	 * @name: Name as registered with perf core.
548c2ecf20Sopenharmony_ci	 */
558c2ecf20Sopenharmony_ci	const char *name;
568c2ecf20Sopenharmony_ci	/**
578c2ecf20Sopenharmony_ci	 * @lock: Lock protecting enable mask and ref count handling.
588c2ecf20Sopenharmony_ci	 */
598c2ecf20Sopenharmony_ci	spinlock_t lock;
608c2ecf20Sopenharmony_ci	/**
618c2ecf20Sopenharmony_ci	 * @timer: Timer for internal i915 PMU sampling.
628c2ecf20Sopenharmony_ci	 */
638c2ecf20Sopenharmony_ci	struct hrtimer timer;
648c2ecf20Sopenharmony_ci	/**
658c2ecf20Sopenharmony_ci	 * @enable: Bitmask of all currently enabled events.
668c2ecf20Sopenharmony_ci	 *
678c2ecf20Sopenharmony_ci	 * Bits are derived from uAPI event numbers in a way that low 16 bits
688c2ecf20Sopenharmony_ci	 * correspond to engine event _sample_ _type_ (I915_SAMPLE_QUEUED is
698c2ecf20Sopenharmony_ci	 * bit 0), and higher bits correspond to other events (for instance
708c2ecf20Sopenharmony_ci	 * I915_PMU_ACTUAL_FREQUENCY is bit 16 etc).
718c2ecf20Sopenharmony_ci	 *
728c2ecf20Sopenharmony_ci	 * In other words, low 16 bits are not per engine but per engine
738c2ecf20Sopenharmony_ci	 * sampler type, while the upper bits are directly mapped to other
748c2ecf20Sopenharmony_ci	 * event types.
758c2ecf20Sopenharmony_ci	 */
768c2ecf20Sopenharmony_ci	u64 enable;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/**
798c2ecf20Sopenharmony_ci	 * @timer_last:
808c2ecf20Sopenharmony_ci	 *
818c2ecf20Sopenharmony_ci	 * Timestmap of the previous timer invocation.
828c2ecf20Sopenharmony_ci	 */
838c2ecf20Sopenharmony_ci	ktime_t timer_last;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/**
868c2ecf20Sopenharmony_ci	 * @enable_count: Reference counts for the enabled events.
878c2ecf20Sopenharmony_ci	 *
888c2ecf20Sopenharmony_ci	 * Array indices are mapped in the same way as bits in the @enable field
898c2ecf20Sopenharmony_ci	 * and they are used to control sampling on/off when multiple clients
908c2ecf20Sopenharmony_ci	 * are using the PMU API.
918c2ecf20Sopenharmony_ci	 */
928c2ecf20Sopenharmony_ci	unsigned int enable_count[I915_PMU_MASK_BITS];
938c2ecf20Sopenharmony_ci	/**
948c2ecf20Sopenharmony_ci	 * @timer_enabled: Should the internal sampling timer be running.
958c2ecf20Sopenharmony_ci	 */
968c2ecf20Sopenharmony_ci	bool timer_enabled;
978c2ecf20Sopenharmony_ci	/**
988c2ecf20Sopenharmony_ci	 * @sample: Current and previous (raw) counters for sampling events.
998c2ecf20Sopenharmony_ci	 *
1008c2ecf20Sopenharmony_ci	 * These counters are updated from the i915 PMU sampling timer.
1018c2ecf20Sopenharmony_ci	 *
1028c2ecf20Sopenharmony_ci	 * Only global counters are held here, while the per-engine ones are in
1038c2ecf20Sopenharmony_ci	 * struct intel_engine_cs.
1048c2ecf20Sopenharmony_ci	 */
1058c2ecf20Sopenharmony_ci	struct i915_pmu_sample sample[__I915_NUM_PMU_SAMPLERS];
1068c2ecf20Sopenharmony_ci	/**
1078c2ecf20Sopenharmony_ci	 * @sleep_last: Last time GT parked for RC6 estimation.
1088c2ecf20Sopenharmony_ci	 */
1098c2ecf20Sopenharmony_ci	ktime_t sleep_last;
1108c2ecf20Sopenharmony_ci	/**
1118c2ecf20Sopenharmony_ci	 * @events_attr_group: Device events attribute group.
1128c2ecf20Sopenharmony_ci	 */
1138c2ecf20Sopenharmony_ci	struct attribute_group events_attr_group;
1148c2ecf20Sopenharmony_ci	/**
1158c2ecf20Sopenharmony_ci	 * @i915_attr: Memory block holding device attributes.
1168c2ecf20Sopenharmony_ci	 */
1178c2ecf20Sopenharmony_ci	void *i915_attr;
1188c2ecf20Sopenharmony_ci	/**
1198c2ecf20Sopenharmony_ci	 * @pmu_attr: Memory block holding device attributes.
1208c2ecf20Sopenharmony_ci	 */
1218c2ecf20Sopenharmony_ci	void *pmu_attr;
1228c2ecf20Sopenharmony_ci};
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#ifdef CONFIG_PERF_EVENTS
1258c2ecf20Sopenharmony_civoid i915_pmu_register(struct drm_i915_private *i915);
1268c2ecf20Sopenharmony_civoid i915_pmu_unregister(struct drm_i915_private *i915);
1278c2ecf20Sopenharmony_civoid i915_pmu_gt_parked(struct drm_i915_private *i915);
1288c2ecf20Sopenharmony_civoid i915_pmu_gt_unparked(struct drm_i915_private *i915);
1298c2ecf20Sopenharmony_ci#else
1308c2ecf20Sopenharmony_cistatic inline void i915_pmu_register(struct drm_i915_private *i915) {}
1318c2ecf20Sopenharmony_cistatic inline void i915_pmu_unregister(struct drm_i915_private *i915) {}
1328c2ecf20Sopenharmony_cistatic inline void i915_pmu_gt_parked(struct drm_i915_private *i915) {}
1338c2ecf20Sopenharmony_cistatic inline void i915_pmu_gt_unparked(struct drm_i915_private *i915) {}
1348c2ecf20Sopenharmony_ci#endif
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci#endif
137