Loading...
1// SPDX-License-Identifier: MIT
2/*
3 * Copyright © 2023 Intel Corporation
4 */
5
6#include "xe_gt_freq.h"
7
8#include <linux/kobject.h>
9#include <linux/sysfs.h>
10
11#include <drm/drm_managed.h>
12#include <drm/drm_print.h>
13
14#include "xe_gt_sysfs.h"
15#include "xe_gt_throttle.h"
16#include "xe_gt_types.h"
17#include "xe_guc_pc.h"
18#include "xe_pm.h"
19
20/**
21 * DOC: Xe GT Frequency Management
22 *
23 * This component is responsible for the raw GT frequency management, including
24 * the sysfs API.
25 *
26 * Underneath, Xe enables GuC SLPC automated frequency management. GuC is then
27 * allowed to request PCODE any frequency between the Minimum and the Maximum
28 * selected by this component. Furthermore, it is important to highlight that
29 * PCODE is the ultimate decision maker of the actual running frequency, based
30 * on thermal and other running conditions.
31 *
32 * Xe's Freq provides a sysfs API for frequency management:
33 *
34 * device/tile#/gt#/freq0/<item>_freq *read-only* files:
35 * - act_freq: The actual resolved frequency decided by PCODE.
36 * - cur_freq: The current one requested by GuC PC to the PCODE.
37 * - rpn_freq: The Render Performance (RP) N level, which is the minimal one.
38 * - rpe_freq: The Render Performance (RP) E level, which is the efficient one.
39 * - rp0_freq: The Render Performance (RP) 0 level, which is the maximum one.
40 *
41 * device/tile#/gt#/freq0/<item>_freq *read-write* files:
42 * - min_freq: Min frequency request.
43 * - max_freq: Max frequency request.
44 * If max <= min, then freq_min becomes a fixed frequency request.
45 */
46
47static struct xe_guc_pc *
48dev_to_pc(struct device *dev)
49{
50 return &kobj_to_gt(dev->kobj.parent)->uc.guc.pc;
51}
52
53static struct xe_device *
54dev_to_xe(struct device *dev)
55{
56 return gt_to_xe(kobj_to_gt(dev->kobj.parent));
57}
58
59static ssize_t act_freq_show(struct device *dev,
60 struct device_attribute *attr, char *buf)
61{
62 struct xe_guc_pc *pc = dev_to_pc(dev);
63 u32 freq;
64
65 xe_pm_runtime_get(dev_to_xe(dev));
66 freq = xe_guc_pc_get_act_freq(pc);
67 xe_pm_runtime_put(dev_to_xe(dev));
68
69 return sysfs_emit(buf, "%d\n", freq);
70}
71static DEVICE_ATTR_RO(act_freq);
72
73static ssize_t cur_freq_show(struct device *dev,
74 struct device_attribute *attr, char *buf)
75{
76 struct xe_guc_pc *pc = dev_to_pc(dev);
77 u32 freq;
78 ssize_t ret;
79
80 xe_pm_runtime_get(dev_to_xe(dev));
81 ret = xe_guc_pc_get_cur_freq(pc, &freq);
82 xe_pm_runtime_put(dev_to_xe(dev));
83 if (ret)
84 return ret;
85
86 return sysfs_emit(buf, "%d\n", freq);
87}
88static DEVICE_ATTR_RO(cur_freq);
89
90static ssize_t rp0_freq_show(struct device *dev,
91 struct device_attribute *attr, char *buf)
92{
93 struct xe_guc_pc *pc = dev_to_pc(dev);
94 u32 freq;
95
96 xe_pm_runtime_get(dev_to_xe(dev));
97 freq = xe_guc_pc_get_rp0_freq(pc);
98 xe_pm_runtime_put(dev_to_xe(dev));
99
100 return sysfs_emit(buf, "%d\n", freq);
101}
102static DEVICE_ATTR_RO(rp0_freq);
103
104static ssize_t rpe_freq_show(struct device *dev,
105 struct device_attribute *attr, char *buf)
106{
107 struct xe_guc_pc *pc = dev_to_pc(dev);
108 u32 freq;
109
110 xe_pm_runtime_get(dev_to_xe(dev));
111 freq = xe_guc_pc_get_rpe_freq(pc);
112 xe_pm_runtime_put(dev_to_xe(dev));
113
114 return sysfs_emit(buf, "%d\n", freq);
115}
116static DEVICE_ATTR_RO(rpe_freq);
117
118static ssize_t rpn_freq_show(struct device *dev,
119 struct device_attribute *attr, char *buf)
120{
121 struct xe_guc_pc *pc = dev_to_pc(dev);
122
123 return sysfs_emit(buf, "%d\n", xe_guc_pc_get_rpn_freq(pc));
124}
125static DEVICE_ATTR_RO(rpn_freq);
126
127static ssize_t min_freq_show(struct device *dev,
128 struct device_attribute *attr, char *buf)
129{
130 struct xe_guc_pc *pc = dev_to_pc(dev);
131 u32 freq;
132 ssize_t ret;
133
134 xe_pm_runtime_get(dev_to_xe(dev));
135 ret = xe_guc_pc_get_min_freq(pc, &freq);
136 xe_pm_runtime_put(dev_to_xe(dev));
137 if (ret)
138 return ret;
139
140 return sysfs_emit(buf, "%d\n", freq);
141}
142
143static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
144 const char *buff, size_t count)
145{
146 struct xe_guc_pc *pc = dev_to_pc(dev);
147 u32 freq;
148 ssize_t ret;
149
150 ret = kstrtou32(buff, 0, &freq);
151 if (ret)
152 return ret;
153
154 xe_pm_runtime_get(dev_to_xe(dev));
155 ret = xe_guc_pc_set_min_freq(pc, freq);
156 xe_pm_runtime_put(dev_to_xe(dev));
157 if (ret)
158 return ret;
159
160 return count;
161}
162static DEVICE_ATTR_RW(min_freq);
163
164static ssize_t max_freq_show(struct device *dev,
165 struct device_attribute *attr, char *buf)
166{
167 struct xe_guc_pc *pc = dev_to_pc(dev);
168 u32 freq;
169 ssize_t ret;
170
171 xe_pm_runtime_get(dev_to_xe(dev));
172 ret = xe_guc_pc_get_max_freq(pc, &freq);
173 xe_pm_runtime_put(dev_to_xe(dev));
174 if (ret)
175 return ret;
176
177 return sysfs_emit(buf, "%d\n", freq);
178}
179
180static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
181 const char *buff, size_t count)
182{
183 struct xe_guc_pc *pc = dev_to_pc(dev);
184 u32 freq;
185 ssize_t ret;
186
187 ret = kstrtou32(buff, 0, &freq);
188 if (ret)
189 return ret;
190
191 xe_pm_runtime_get(dev_to_xe(dev));
192 ret = xe_guc_pc_set_max_freq(pc, freq);
193 xe_pm_runtime_put(dev_to_xe(dev));
194 if (ret)
195 return ret;
196
197 return count;
198}
199static DEVICE_ATTR_RW(max_freq);
200
201static const struct attribute *freq_attrs[] = {
202 &dev_attr_act_freq.attr,
203 &dev_attr_cur_freq.attr,
204 &dev_attr_rp0_freq.attr,
205 &dev_attr_rpe_freq.attr,
206 &dev_attr_rpn_freq.attr,
207 &dev_attr_min_freq.attr,
208 &dev_attr_max_freq.attr,
209 NULL
210};
211
212static void freq_fini(void *arg)
213{
214 struct kobject *kobj = arg;
215
216 sysfs_remove_files(kobj, freq_attrs);
217 kobject_put(kobj);
218}
219
220/**
221 * xe_gt_freq_init - Initialize Xe Freq component
222 * @gt: Xe GT object
223 *
224 * It needs to be initialized after GT Sysfs and GuC PC components are ready.
225 *
226 * Returns: Returns error value for failure and 0 for success.
227 */
228int xe_gt_freq_init(struct xe_gt *gt)
229{
230 struct xe_device *xe = gt_to_xe(gt);
231 int err;
232
233 if (xe->info.skip_guc_pc)
234 return 0;
235
236 gt->freq = kobject_create_and_add("freq0", gt->sysfs);
237 if (!gt->freq)
238 return -ENOMEM;
239
240 err = sysfs_create_files(gt->freq, freq_attrs);
241 if (err)
242 return err;
243
244 err = devm_add_action_or_reset(xe->drm.dev, freq_fini, gt->freq);
245 if (err)
246 return err;
247
248 return xe_gt_throttle_init(gt);
249}
1// SPDX-License-Identifier: MIT
2/*
3 * Copyright © 2023 Intel Corporation
4 */
5
6#include "xe_gt_freq.h"
7
8#include <linux/kobject.h>
9#include <linux/sysfs.h>
10
11#include <drm/drm_managed.h>
12#include <drm/drm_print.h>
13
14#include "xe_device_types.h"
15#include "xe_gt_sysfs.h"
16#include "xe_gt_throttle_sysfs.h"
17#include "xe_guc_pc.h"
18
19/**
20 * DOC: Xe GT Frequency Management
21 *
22 * This component is responsible for the raw GT frequency management, including
23 * the sysfs API.
24 *
25 * Underneath, Xe enables GuC SLPC automated frequency management. GuC is then
26 * allowed to request PCODE any frequency between the Minimum and the Maximum
27 * selected by this component. Furthermore, it is important to highlight that
28 * PCODE is the ultimate decision maker of the actual running frequency, based
29 * on thermal and other running conditions.
30 *
31 * Xe's Freq provides a sysfs API for frequency management:
32 *
33 * device/tile#/gt#/freq0/<item>_freq *read-only* files:
34 * - act_freq: The actual resolved frequency decided by PCODE.
35 * - cur_freq: The current one requested by GuC PC to the PCODE.
36 * - rpn_freq: The Render Performance (RP) N level, which is the minimal one.
37 * - rpe_freq: The Render Performance (RP) E level, which is the efficient one.
38 * - rp0_freq: The Render Performance (RP) 0 level, which is the maximum one.
39 *
40 * device/tile#/gt#/freq0/<item>_freq *read-write* files:
41 * - min_freq: Min frequency request.
42 * - max_freq: Max frequency request.
43 * If max <= min, then freq_min becomes a fixed frequency request.
44 */
45
46static struct xe_guc_pc *
47dev_to_pc(struct device *dev)
48{
49 return &kobj_to_gt(dev->kobj.parent)->uc.guc.pc;
50}
51
52static ssize_t act_freq_show(struct device *dev,
53 struct device_attribute *attr, char *buf)
54{
55 struct xe_guc_pc *pc = dev_to_pc(dev);
56
57 return sysfs_emit(buf, "%d\n", xe_guc_pc_get_act_freq(pc));
58}
59static DEVICE_ATTR_RO(act_freq);
60
61static ssize_t cur_freq_show(struct device *dev,
62 struct device_attribute *attr, char *buf)
63{
64 struct xe_guc_pc *pc = dev_to_pc(dev);
65 u32 freq;
66 ssize_t ret;
67
68 ret = xe_guc_pc_get_cur_freq(pc, &freq);
69 if (ret)
70 return ret;
71
72 return sysfs_emit(buf, "%d\n", freq);
73}
74static DEVICE_ATTR_RO(cur_freq);
75
76static ssize_t rp0_freq_show(struct device *dev,
77 struct device_attribute *attr, char *buf)
78{
79 struct xe_guc_pc *pc = dev_to_pc(dev);
80
81 return sysfs_emit(buf, "%d\n", xe_guc_pc_get_rp0_freq(pc));
82}
83static DEVICE_ATTR_RO(rp0_freq);
84
85static ssize_t rpe_freq_show(struct device *dev,
86 struct device_attribute *attr, char *buf)
87{
88 struct xe_guc_pc *pc = dev_to_pc(dev);
89
90 return sysfs_emit(buf, "%d\n", xe_guc_pc_get_rpe_freq(pc));
91}
92static DEVICE_ATTR_RO(rpe_freq);
93
94static ssize_t rpn_freq_show(struct device *dev,
95 struct device_attribute *attr, char *buf)
96{
97 struct xe_guc_pc *pc = dev_to_pc(dev);
98
99 return sysfs_emit(buf, "%d\n", xe_guc_pc_get_rpn_freq(pc));
100}
101static DEVICE_ATTR_RO(rpn_freq);
102
103static ssize_t min_freq_show(struct device *dev,
104 struct device_attribute *attr, char *buf)
105{
106 struct xe_guc_pc *pc = dev_to_pc(dev);
107 u32 freq;
108 ssize_t ret;
109
110 ret = xe_guc_pc_get_min_freq(pc, &freq);
111 if (ret)
112 return ret;
113
114 return sysfs_emit(buf, "%d\n", freq);
115}
116
117static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
118 const char *buff, size_t count)
119{
120 struct xe_guc_pc *pc = dev_to_pc(dev);
121 u32 freq;
122 ssize_t ret;
123
124 ret = kstrtou32(buff, 0, &freq);
125 if (ret)
126 return ret;
127
128 ret = xe_guc_pc_set_min_freq(pc, freq);
129 if (ret)
130 return ret;
131
132 return count;
133}
134static DEVICE_ATTR_RW(min_freq);
135
136static ssize_t max_freq_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
138{
139 struct xe_guc_pc *pc = dev_to_pc(dev);
140 u32 freq;
141 ssize_t ret;
142
143 ret = xe_guc_pc_get_max_freq(pc, &freq);
144 if (ret)
145 return ret;
146
147 return sysfs_emit(buf, "%d\n", freq);
148}
149
150static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
151 const char *buff, size_t count)
152{
153 struct xe_guc_pc *pc = dev_to_pc(dev);
154 u32 freq;
155 ssize_t ret;
156
157 ret = kstrtou32(buff, 0, &freq);
158 if (ret)
159 return ret;
160
161 ret = xe_guc_pc_set_max_freq(pc, freq);
162 if (ret)
163 return ret;
164
165 return count;
166}
167static DEVICE_ATTR_RW(max_freq);
168
169static const struct attribute *freq_attrs[] = {
170 &dev_attr_act_freq.attr,
171 &dev_attr_cur_freq.attr,
172 &dev_attr_rp0_freq.attr,
173 &dev_attr_rpe_freq.attr,
174 &dev_attr_rpn_freq.attr,
175 &dev_attr_min_freq.attr,
176 &dev_attr_max_freq.attr,
177 NULL
178};
179
180static void freq_fini(struct drm_device *drm, void *arg)
181{
182 struct kobject *kobj = arg;
183
184 sysfs_remove_files(kobj, freq_attrs);
185 kobject_put(kobj);
186}
187
188/**
189 * xe_gt_freq_init - Initialize Xe Freq component
190 * @gt: Xe GT object
191 *
192 * It needs to be initialized after GT Sysfs and GuC PC components are ready.
193 */
194void xe_gt_freq_init(struct xe_gt *gt)
195{
196 struct xe_device *xe = gt_to_xe(gt);
197 int err;
198
199 if (xe->info.skip_guc_pc)
200 return;
201
202 gt->freq = kobject_create_and_add("freq0", gt->sysfs);
203 if (!gt->freq) {
204 drm_warn(&xe->drm, "failed to add freq0 directory to %s\n",
205 kobject_name(gt->sysfs));
206 return;
207 }
208
209 err = drmm_add_action_or_reset(&xe->drm, freq_fini, gt->freq);
210 if (err) {
211 drm_warn(&xe->drm, "%s: drmm_add_action_or_reset failed, err: %d\n",
212 __func__, err);
213 return;
214 }
215
216 err = sysfs_create_files(gt->freq, freq_attrs);
217 if (err)
218 drm_warn(&xe->drm, "failed to add freq attrs to %s, err: %d\n",
219 kobject_name(gt->freq), err);
220
221 xe_gt_throttle_sysfs_init(gt);
222}