xref: /linux/drivers/gpu/drm/xe/xe_gt_throttle.c (revision f2161d5f1aae21a42b0a64d87e10cb31db423f42)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include <drm/drm_managed.h>
7 
8 #include <regs/xe_gt_regs.h>
9 #include "xe_device.h"
10 #include "xe_gt.h"
11 #include "xe_gt_sysfs.h"
12 #include "xe_gt_throttle.h"
13 #include "xe_mmio.h"
14 #include "xe_platform_types.h"
15 #include "xe_pm.h"
16 
17 /**
18  * DOC: Xe GT Throttle
19  *
20  * The GT frequency may be throttled by hardware/firmware for various reasons
21  * that are provided through attributes under the ``freq0/throttle/`` directory.
22  * Their availability depend on the platform and some may not be visible if that
23  * reason is not available.
24  *
25  * The ``reasons`` attribute can be used by sysadmin to monitor all possible
26  * reasons for throttling and report them. It's preferred over monitoring
27  * ``status`` and then reading the reason from individual attributes since that
28  * is racy. If there's no throttling happening, "none" is returned.
29  *
30  * The following attributes are available on Crescent Island platform:
31  *
32  * - ``status``: Overall throttle status (0: no throttling, 1: throttling)
33  * - ``reasons``: Array of reasons causing throttling separated by space
34  * - ``reason_pl1``: package PL1
35  * - ``reason_pl2``: package PL2
36  * - ``reason_pl4``: package PL4
37  * - ``reason_prochot``: prochot
38  * - ``reason_soc_thermal``: SoC thermal
39  * - ``reason_mem_thermal``: Memory thermal
40  * - ``reason_vr_thermal``: VR thermal
41  * - ``reason_iccmax``: ICCMAX
42  * - ``reason_ratl``: RATL thermal algorithm
43  * - ``reason_soc_avg_thermal``: SoC average temp
44  * - ``reason_fastvmode``: VR is hitting FastVMode
45  * - ``reason_psys_pl1``: PSYS PL1
46  * - ``reason_psys_pl2``: PSYS PL2
47  * - ``reason_p0_freq``: P0 frequency
48  * - ``reason_psys_crit``: PSYS critical
49  *
50  * Other platforms support the following reasons:
51  *
52  * - ``status``: Overall throttle status (0: no throttling, 1: throttling)
53  * - ``reasons``: Array of reasons causing throttling separated by space
54  * - ``reason_pl1``: package PL1
55  * - ``reason_pl2``: package PL2
56  * - ``reason_pl4``: package PL4, Iccmax etc.
57  * - ``reason_thermal``: thermal
58  * - ``reason_prochot``: prochot
59  * - ``reason_ratl``: RATL hermal algorithm
60  * - ``reason_vr_thermalert``: VR THERMALERT
61  * - ``reason_vr_tdc``: VR TDC
62  */
63 
64 struct throttle_attribute {
65 	struct kobj_attribute attr;
66 	u32 mask;
67 };
68 
dev_to_gt(struct device * dev)69 static struct xe_gt *dev_to_gt(struct device *dev)
70 {
71 	return kobj_to_gt(dev->kobj.parent);
72 }
73 
throttle_to_gt(struct kobject * kobj)74 static struct xe_gt *throttle_to_gt(struct kobject *kobj)
75 {
76 	return dev_to_gt(kobj_to_dev(kobj));
77 }
78 
kobj_attribute_to_throttle(struct kobj_attribute * attr)79 static struct throttle_attribute *kobj_attribute_to_throttle(struct kobj_attribute *attr)
80 {
81 	return container_of(attr, struct throttle_attribute, attr);
82 }
83 
xe_gt_throttle_get_limit_reasons(struct xe_gt * gt)84 u32 xe_gt_throttle_get_limit_reasons(struct xe_gt *gt)
85 {
86 	struct xe_device *xe = gt_to_xe(gt);
87 	struct xe_reg reg;
88 	u32 val, mask;
89 
90 	if (xe_gt_is_media_type(gt))
91 		reg = MTL_MEDIA_PERF_LIMIT_REASONS;
92 	else
93 		reg = GT0_PERF_LIMIT_REASONS;
94 
95 	if (xe->info.platform == XE_CRESCENTISLAND)
96 		mask = CRI_PERF_LIMIT_REASONS_MASK;
97 	else
98 		mask = GT0_PERF_LIMIT_REASONS_MASK;
99 
100 	xe_pm_runtime_get(xe);
101 	val = xe_mmio_read32(&gt->mmio, reg) & mask;
102 	xe_pm_runtime_put(xe);
103 
104 	return val;
105 }
106 
is_throttled_by(struct xe_gt * gt,u32 mask)107 static bool is_throttled_by(struct xe_gt *gt, u32 mask)
108 {
109 	return xe_gt_throttle_get_limit_reasons(gt) & mask;
110 }
111 
reason_show(struct kobject * kobj,struct kobj_attribute * attr,char * buff)112 static ssize_t reason_show(struct kobject *kobj,
113 			   struct kobj_attribute *attr, char *buff)
114 {
115 	struct throttle_attribute *ta = kobj_attribute_to_throttle(attr);
116 	struct xe_gt *gt = throttle_to_gt(kobj);
117 
118 	return sysfs_emit(buff, "%u\n", is_throttled_by(gt, ta->mask));
119 }
120 
121 static const struct attribute_group *get_platform_throttle_group(struct xe_device *xe);
122 
reasons_show(struct kobject * kobj,struct kobj_attribute * attr,char * buff)123 static ssize_t reasons_show(struct kobject *kobj,
124 			    struct kobj_attribute *attr, char *buff)
125 {
126 	struct xe_gt *gt = throttle_to_gt(kobj);
127 	struct xe_device *xe = gt_to_xe(gt);
128 	const struct attribute_group *group;
129 	struct attribute **pother;
130 	ssize_t ret = 0;
131 	u32 reasons;
132 
133 	reasons = xe_gt_throttle_get_limit_reasons(gt);
134 	if (!reasons)
135 		goto ret_none;
136 
137 	group = get_platform_throttle_group(xe);
138 	for (pother = group->attrs; *pother; pother++) {
139 		struct kobj_attribute *kattr = container_of(*pother, struct kobj_attribute, attr);
140 		struct throttle_attribute *other_ta = kobj_attribute_to_throttle(kattr);
141 
142 		if (other_ta->mask != U32_MAX && reasons & other_ta->mask)
143 			ret += sysfs_emit_at(buff, ret, "%s ", (*pother)->name + strlen("reason_"));
144 	}
145 
146 	if (drm_WARN_ONCE(&xe->drm, !ret, "Unknown reason: %#x\n", reasons))
147 		goto ret_none;
148 
149 	/* Drop extra space from last iteration above */
150 	ret--;
151 	ret += sysfs_emit_at(buff, ret, "\n");
152 
153 	return ret;
154 
155 ret_none:
156 	return sysfs_emit(buff, "none\n");
157 }
158 
159 #define THROTTLE_ATTR_RO(name, _mask)				\
160 	struct throttle_attribute attr_##name =	{		\
161 		.attr = __ATTR(name, 0444, reason_show, NULL),	\
162 		.mask = _mask,					\
163 	}
164 
165 #define THROTTLE_ATTR_RO_FUNC(name, _mask, _show)		\
166 	struct throttle_attribute attr_##name =	{		\
167 		.attr = __ATTR(name, 0444, _show, NULL),	\
168 		.mask = _mask,					\
169 	}
170 
171 static THROTTLE_ATTR_RO_FUNC(reasons, 0, reasons_show);
172 static THROTTLE_ATTR_RO(status, U32_MAX);
173 static THROTTLE_ATTR_RO(reason_pl1, POWER_LIMIT_1_MASK);
174 static THROTTLE_ATTR_RO(reason_pl2, POWER_LIMIT_2_MASK);
175 static THROTTLE_ATTR_RO(reason_pl4, POWER_LIMIT_4_MASK);
176 static THROTTLE_ATTR_RO(reason_thermal, THERMAL_LIMIT_MASK);
177 static THROTTLE_ATTR_RO(reason_prochot, PROCHOT_MASK);
178 static THROTTLE_ATTR_RO(reason_ratl, RATL_MASK);
179 static THROTTLE_ATTR_RO(reason_vr_thermalert, VR_THERMALERT_MASK);
180 static THROTTLE_ATTR_RO(reason_vr_tdc, VR_TDC_MASK);
181 
182 static struct attribute *throttle_attrs[] = {
183 	&attr_reasons.attr.attr,
184 	&attr_status.attr.attr,
185 	&attr_reason_pl1.attr.attr,
186 	&attr_reason_pl2.attr.attr,
187 	&attr_reason_pl4.attr.attr,
188 	&attr_reason_thermal.attr.attr,
189 	&attr_reason_prochot.attr.attr,
190 	&attr_reason_ratl.attr.attr,
191 	&attr_reason_vr_thermalert.attr.attr,
192 	&attr_reason_vr_tdc.attr.attr,
193 	NULL
194 };
195 
196 static THROTTLE_ATTR_RO(reason_vr_thermal, VR_THERMAL_MASK);
197 static THROTTLE_ATTR_RO(reason_soc_thermal, SOC_THERMAL_LIMIT_MASK);
198 static THROTTLE_ATTR_RO(reason_mem_thermal, MEM_THERMAL_MASK);
199 static THROTTLE_ATTR_RO(reason_iccmax, ICCMAX_MASK);
200 static THROTTLE_ATTR_RO(reason_soc_avg_thermal, SOC_AVG_THERMAL_MASK);
201 static THROTTLE_ATTR_RO(reason_fastvmode, FASTVMODE_MASK);
202 static THROTTLE_ATTR_RO(reason_psys_pl1, PSYS_PL1_MASK);
203 static THROTTLE_ATTR_RO(reason_psys_pl2, PSYS_PL2_MASK);
204 static THROTTLE_ATTR_RO(reason_p0_freq, P0_FREQ_MASK);
205 static THROTTLE_ATTR_RO(reason_psys_crit, PSYS_CRIT_MASK);
206 
207 static struct attribute *cri_throttle_attrs[] = {
208 	/* Common */
209 	&attr_reasons.attr.attr,
210 	&attr_status.attr.attr,
211 	&attr_reason_pl1.attr.attr,
212 	&attr_reason_pl2.attr.attr,
213 	&attr_reason_pl4.attr.attr,
214 	&attr_reason_prochot.attr.attr,
215 	&attr_reason_ratl.attr.attr,
216 	/* CRI */
217 	&attr_reason_vr_thermal.attr.attr,
218 	&attr_reason_soc_thermal.attr.attr,
219 	&attr_reason_mem_thermal.attr.attr,
220 	&attr_reason_iccmax.attr.attr,
221 	&attr_reason_soc_avg_thermal.attr.attr,
222 	&attr_reason_fastvmode.attr.attr,
223 	&attr_reason_psys_pl1.attr.attr,
224 	&attr_reason_psys_pl2.attr.attr,
225 	&attr_reason_p0_freq.attr.attr,
226 	&attr_reason_psys_crit.attr.attr,
227 	NULL
228 };
229 
230 static const struct attribute_group throttle_group_attrs = {
231 	.name = "throttle",
232 	.attrs = throttle_attrs,
233 };
234 
235 static const struct attribute_group cri_throttle_group_attrs = {
236 	.name = "throttle",
237 	.attrs = cri_throttle_attrs,
238 };
239 
get_platform_throttle_group(struct xe_device * xe)240 static const struct attribute_group *get_platform_throttle_group(struct xe_device *xe)
241 {
242 	switch (xe->info.platform) {
243 	case XE_CRESCENTISLAND:
244 		return &cri_throttle_group_attrs;
245 	default:
246 		return &throttle_group_attrs;
247 	}
248 }
249 
gt_throttle_sysfs_fini(void * arg)250 static void gt_throttle_sysfs_fini(void *arg)
251 {
252 	struct xe_gt *gt = arg;
253 	struct xe_device *xe = gt_to_xe(gt);
254 	const struct attribute_group *group = get_platform_throttle_group(xe);
255 
256 	sysfs_remove_group(gt->freq, group);
257 }
258 
xe_gt_throttle_init(struct xe_gt * gt)259 int xe_gt_throttle_init(struct xe_gt *gt)
260 {
261 	struct xe_device *xe = gt_to_xe(gt);
262 	const struct attribute_group *group = get_platform_throttle_group(xe);
263 	int err;
264 
265 	err = sysfs_create_group(gt->freq, group);
266 	if (err)
267 		return err;
268 
269 	return devm_add_action_or_reset(xe->drm.dev, gt_throttle_sysfs_fini, gt);
270 }
271