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_types.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 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 guard(xe_pm_runtime)(xe);
101 return xe_mmio_read32(>->mmio, reg) & mask;
102 }
103
is_throttled_by(struct xe_gt * gt,u32 mask)104 static bool is_throttled_by(struct xe_gt *gt, u32 mask)
105 {
106 return xe_gt_throttle_get_limit_reasons(gt) & mask;
107 }
108
reason_show(struct kobject * kobj,struct kobj_attribute * attr,char * buff)109 static ssize_t reason_show(struct kobject *kobj,
110 struct kobj_attribute *attr, char *buff)
111 {
112 struct throttle_attribute *ta = kobj_attribute_to_throttle(attr);
113 struct xe_gt *gt = throttle_to_gt(kobj);
114
115 return sysfs_emit(buff, "%u\n", is_throttled_by(gt, ta->mask));
116 }
117
118 static const struct attribute_group *get_platform_throttle_group(struct xe_device *xe);
119
reasons_show(struct kobject * kobj,struct kobj_attribute * attr,char * buff)120 static ssize_t reasons_show(struct kobject *kobj,
121 struct kobj_attribute *attr, char *buff)
122 {
123 struct xe_gt *gt = throttle_to_gt(kobj);
124 struct xe_device *xe = gt_to_xe(gt);
125 const struct attribute_group *group;
126 struct attribute **pother;
127 ssize_t ret = 0;
128 u32 reasons;
129
130 reasons = xe_gt_throttle_get_limit_reasons(gt);
131 if (!reasons)
132 goto ret_none;
133
134 group = get_platform_throttle_group(xe);
135 for (pother = group->attrs; *pother; pother++) {
136 struct kobj_attribute *kattr = container_of(*pother, struct kobj_attribute, attr);
137 struct throttle_attribute *other_ta = kobj_attribute_to_throttle(kattr);
138
139 if (other_ta->mask != U32_MAX && reasons & other_ta->mask)
140 ret += sysfs_emit_at(buff, ret, "%s ", (*pother)->name + strlen("reason_"));
141 }
142
143 if (drm_WARN_ONCE(&xe->drm, !ret, "Unknown reason: %#x\n", reasons))
144 goto ret_none;
145
146 /* Drop extra space from last iteration above */
147 ret--;
148 ret += sysfs_emit_at(buff, ret, "\n");
149
150 return ret;
151
152 ret_none:
153 return sysfs_emit(buff, "none\n");
154 }
155
156 #define THROTTLE_ATTR_RO(name, _mask) \
157 struct throttle_attribute attr_##name = { \
158 .attr = __ATTR(name, 0444, reason_show, NULL), \
159 .mask = _mask, \
160 }
161
162 #define THROTTLE_ATTR_RO_FUNC(name, _mask, _show) \
163 struct throttle_attribute attr_##name = { \
164 .attr = __ATTR(name, 0444, _show, NULL), \
165 .mask = _mask, \
166 }
167
168 static THROTTLE_ATTR_RO_FUNC(reasons, 0, reasons_show);
169 static THROTTLE_ATTR_RO(status, U32_MAX);
170 static THROTTLE_ATTR_RO(reason_pl1, POWER_LIMIT_1_MASK);
171 static THROTTLE_ATTR_RO(reason_pl2, POWER_LIMIT_2_MASK);
172 static THROTTLE_ATTR_RO(reason_pl4, POWER_LIMIT_4_MASK);
173 static THROTTLE_ATTR_RO(reason_thermal, THERMAL_LIMIT_MASK);
174 static THROTTLE_ATTR_RO(reason_prochot, PROCHOT_MASK);
175 static THROTTLE_ATTR_RO(reason_ratl, RATL_MASK);
176 static THROTTLE_ATTR_RO(reason_vr_thermalert, VR_THERMALERT_MASK);
177 static THROTTLE_ATTR_RO(reason_vr_tdc, VR_TDC_MASK);
178
179 static struct attribute *throttle_attrs[] = {
180 &attr_reasons.attr.attr,
181 &attr_status.attr.attr,
182 &attr_reason_pl1.attr.attr,
183 &attr_reason_pl2.attr.attr,
184 &attr_reason_pl4.attr.attr,
185 &attr_reason_thermal.attr.attr,
186 &attr_reason_prochot.attr.attr,
187 &attr_reason_ratl.attr.attr,
188 &attr_reason_vr_thermalert.attr.attr,
189 &attr_reason_vr_tdc.attr.attr,
190 NULL
191 };
192
193 static THROTTLE_ATTR_RO(reason_vr_thermal, VR_THERMAL_MASK);
194 static THROTTLE_ATTR_RO(reason_soc_thermal, SOC_THERMAL_LIMIT_MASK);
195 static THROTTLE_ATTR_RO(reason_mem_thermal, MEM_THERMAL_MASK);
196 static THROTTLE_ATTR_RO(reason_iccmax, ICCMAX_MASK);
197 static THROTTLE_ATTR_RO(reason_soc_avg_thermal, SOC_AVG_THERMAL_MASK);
198 static THROTTLE_ATTR_RO(reason_fastvmode, FASTVMODE_MASK);
199 static THROTTLE_ATTR_RO(reason_psys_pl1, PSYS_PL1_MASK);
200 static THROTTLE_ATTR_RO(reason_psys_pl2, PSYS_PL2_MASK);
201 static THROTTLE_ATTR_RO(reason_p0_freq, P0_FREQ_MASK);
202 static THROTTLE_ATTR_RO(reason_psys_crit, PSYS_CRIT_MASK);
203
204 static struct attribute *cri_throttle_attrs[] = {
205 /* Common */
206 &attr_reasons.attr.attr,
207 &attr_status.attr.attr,
208 &attr_reason_pl1.attr.attr,
209 &attr_reason_pl2.attr.attr,
210 &attr_reason_pl4.attr.attr,
211 &attr_reason_prochot.attr.attr,
212 &attr_reason_ratl.attr.attr,
213 /* CRI */
214 &attr_reason_vr_thermal.attr.attr,
215 &attr_reason_soc_thermal.attr.attr,
216 &attr_reason_mem_thermal.attr.attr,
217 &attr_reason_iccmax.attr.attr,
218 &attr_reason_soc_avg_thermal.attr.attr,
219 &attr_reason_fastvmode.attr.attr,
220 &attr_reason_psys_pl1.attr.attr,
221 &attr_reason_psys_pl2.attr.attr,
222 &attr_reason_p0_freq.attr.attr,
223 &attr_reason_psys_crit.attr.attr,
224 NULL
225 };
226
227 static const struct attribute_group throttle_group_attrs = {
228 .name = "throttle",
229 .attrs = throttle_attrs,
230 };
231
232 static const struct attribute_group cri_throttle_group_attrs = {
233 .name = "throttle",
234 .attrs = cri_throttle_attrs,
235 };
236
get_platform_throttle_group(struct xe_device * xe)237 static const struct attribute_group *get_platform_throttle_group(struct xe_device *xe)
238 {
239 switch (xe->info.platform) {
240 case XE_CRESCENTISLAND:
241 return &cri_throttle_group_attrs;
242 default:
243 return &throttle_group_attrs;
244 }
245 }
246
gt_throttle_sysfs_fini(void * arg)247 static void gt_throttle_sysfs_fini(void *arg)
248 {
249 struct xe_gt *gt = arg;
250 struct xe_device *xe = gt_to_xe(gt);
251 const struct attribute_group *group = get_platform_throttle_group(xe);
252
253 sysfs_remove_group(gt->freq, group);
254 }
255
xe_gt_throttle_init(struct xe_gt * gt)256 int xe_gt_throttle_init(struct xe_gt *gt)
257 {
258 struct xe_device *xe = gt_to_xe(gt);
259 const struct attribute_group *group = get_platform_throttle_group(xe);
260 int err;
261
262 err = sysfs_create_group(gt->freq, group);
263 if (err)
264 return err;
265
266 return devm_add_action_or_reset(xe->drm.dev, gt_throttle_sysfs_fini, gt);
267 }
268