blob: b099e09ccc32e90b99e6f28247808d3b78ee9282 [file] [log] [blame]
Ben Widawsky0136db52012-04-10 21:17:01 -07001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Ben Widawsky <ben@bwidawsk.net>
25 *
26 */
27
28#include <linux/device.h>
29#include <linux/module.h>
30#include <linux/stat.h>
31#include <linux/sysfs.h>
Chris Wilson56c50982019-04-26 09:17:22 +010032
Andi Shytic1132362019-09-27 12:08:49 +010033#include "gt/intel_rc6.h"
Andi Shyti3e7abf82019-10-24 22:16:41 +010034#include "gt/intel_rps.h"
Chris Wilson4ec76db2020-02-28 13:17:10 +000035#include "gt/sysfs_engines.h"
Andi Shytic1132362019-09-27 12:08:49 +010036
Ben Widawsky0136db52012-04-10 21:17:01 -070037#include "i915_drv.h"
Jani Nikulabe682612019-08-08 16:42:45 +030038#include "i915_sysfs.h"
Jani Nikulaecbb5fb2019-04-29 15:29:37 +030039#include "intel_pm.h"
40#include "intel_sideband.h"
Ben Widawsky0136db52012-04-10 21:17:01 -070041
David Weinehall694c2822016-08-22 13:32:43 +030042static inline struct drm_i915_private *kdev_minor_to_i915(struct device *kdev)
David Weinehallc49d13e2016-08-22 13:32:42 +030043{
David Weinehall694c2822016-08-22 13:32:43 +030044 struct drm_minor *minor = dev_get_drvdata(kdev);
45 return to_i915(minor->dev);
David Weinehallc49d13e2016-08-22 13:32:42 +030046}
Dave Airlie14c8d1102013-10-11 14:45:30 +100047
Hunt Xu5ab36332012-07-01 03:45:07 +000048#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +030049static u32 calc_residency(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020050 i915_reg_t reg)
Ben Widawsky0136db52012-04-10 21:17:01 -070051{
Chris Wilson48d1c812019-01-14 14:21:13 +000052 intel_wakeref_t wakeref;
Chris Wilsond4225a52019-01-14 14:21:23 +000053 u64 res = 0;
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +000054
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -070055 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref)
Andi Shytic1132362019-09-27 12:08:49 +010056 res = intel_rc6_residency_us(&dev_priv->gt.rc6, reg);
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +000057
58 return DIV_ROUND_CLOSEST_ULL(res, 1000);
Ben Widawsky0136db52012-04-10 21:17:01 -070059}
60
61static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070062show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070063{
Chris Wilsonfb6db0f2017-12-01 11:30:30 +000064 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
65 unsigned int mask;
66
67 mask = 0;
68 if (HAS_RC6(dev_priv))
69 mask |= BIT(0);
70 if (HAS_RC6p(dev_priv))
71 mask |= BIT(1);
72 if (HAS_RC6pp(dev_priv))
73 mask |= BIT(2);
74
Xuezhi Zhang11cda492021-04-04 08:41:03 +000075 return sysfs_emit(buf, "%x\n", mask);
Ben Widawsky0136db52012-04-10 21:17:01 -070076}
77
78static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070079show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070080{
David Weinehall694c2822016-08-22 13:32:43 +030081 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
82 u32 rc6_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6);
Xuezhi Zhang11cda492021-04-04 08:41:03 +000083 return sysfs_emit(buf, "%u\n", rc6_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070084}
85
86static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070087show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070088{
David Weinehall694c2822016-08-22 13:32:43 +030089 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
90 u32 rc6p_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6p);
Xuezhi Zhang11cda492021-04-04 08:41:03 +000091 return sysfs_emit(buf, "%u\n", rc6p_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070092}
93
94static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070095show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070096{
David Weinehall694c2822016-08-22 13:32:43 +030097 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
98 u32 rc6pp_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6pp);
Xuezhi Zhang11cda492021-04-04 08:41:03 +000099 return sysfs_emit(buf, "%u\n", rc6pp_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -0700100}
101
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530102static ssize_t
103show_media_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
104{
David Weinehall694c2822016-08-22 13:32:43 +0300105 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
106 u32 rc6_residency = calc_residency(dev_priv, VLV_GT_MEDIA_RC6);
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000107 return sysfs_emit(buf, "%u\n", rc6_residency);
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530108}
109
Ben Widawsky0136db52012-04-10 21:17:01 -0700110static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL);
111static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL);
112static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL);
113static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL);
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530114static DEVICE_ATTR(media_rc6_residency_ms, S_IRUGO, show_media_rc6_ms, NULL);
Ben Widawsky0136db52012-04-10 21:17:01 -0700115
116static struct attribute *rc6_attrs[] = {
117 &dev_attr_rc6_enable.attr,
118 &dev_attr_rc6_residency_ms.attr,
Ben Widawsky0136db52012-04-10 21:17:01 -0700119 NULL
120};
121
Arvind Yadav0a7a0982017-07-03 16:38:25 +0530122static const struct attribute_group rc6_attr_group = {
Ben Widawsky0136db52012-04-10 21:17:01 -0700123 .name = power_group_name,
124 .attrs = rc6_attrs
125};
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700126
127static struct attribute *rc6p_attrs[] = {
128 &dev_attr_rc6p_residency_ms.attr,
129 &dev_attr_rc6pp_residency_ms.attr,
130 NULL
131};
132
Arvind Yadav0a7a0982017-07-03 16:38:25 +0530133static const struct attribute_group rc6p_attr_group = {
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700134 .name = power_group_name,
135 .attrs = rc6p_attrs
136};
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530137
138static struct attribute *media_rc6_attrs[] = {
139 &dev_attr_media_rc6_residency_ms.attr,
140 NULL
141};
142
Arvind Yadav0a7a0982017-07-03 16:38:25 +0530143static const struct attribute_group media_rc6_attr_group = {
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530144 .name = power_group_name,
145 .attrs = media_rc6_attrs
146};
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700147#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700148
Chris Wilson261ea7e2019-10-04 11:59:58 +0100149static int l3_access_valid(struct drm_i915_private *i915, loff_t offset)
Ben Widawsky84bc7582012-05-25 16:56:25 -0700150{
Chris Wilson261ea7e2019-10-04 11:59:58 +0100151 if (!HAS_L3_DPF(i915))
Ben Widawsky84bc7582012-05-25 16:56:25 -0700152 return -EPERM;
153
Chris Wilson261ea7e2019-10-04 11:59:58 +0100154 if (!IS_ALIGNED(offset, sizeof(u32)))
Ben Widawsky84bc7582012-05-25 16:56:25 -0700155 return -EINVAL;
156
157 if (offset >= GEN7_L3LOG_SIZE)
158 return -ENXIO;
159
160 return 0;
161}
162
163static ssize_t
164i915_l3_read(struct file *filp, struct kobject *kobj,
165 struct bin_attribute *attr, char *buf,
166 loff_t offset, size_t count)
167{
David Weinehallc49d13e2016-08-22 13:32:42 +0300168 struct device *kdev = kobj_to_dev(kobj);
Chris Wilson261ea7e2019-10-04 11:59:58 +0100169 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700170 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700171 int ret;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700172
Chris Wilson261ea7e2019-10-04 11:59:58 +0100173 ret = l3_access_valid(i915, offset);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700174 if (ret)
175 return ret;
176
Chris Wilson261ea7e2019-10-04 11:59:58 +0100177 count = round_down(count, sizeof(u32));
Dan Carpentere5ad4022013-09-20 14:20:18 +0300178 count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
Chris Wilson261ea7e2019-10-04 11:59:58 +0100179 memset(buf, 0, count);
Ben Widawsky33618ea2013-09-12 22:28:29 -0700180
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100181 spin_lock(&i915->gem.contexts.lock);
Chris Wilson261ea7e2019-10-04 11:59:58 +0100182 if (i915->l3_parity.remap_info[slice])
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700183 memcpy(buf,
Chris Wilson261ea7e2019-10-04 11:59:58 +0100184 i915->l3_parity.remap_info[slice] + offset / sizeof(u32),
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700185 count);
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100186 spin_unlock(&i915->gem.contexts.lock);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700187
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700188 return count;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700189}
190
191static ssize_t
192i915_l3_write(struct file *filp, struct kobject *kobj,
193 struct bin_attribute *attr, char *buf,
194 loff_t offset, size_t count)
195{
David Weinehallc49d13e2016-08-22 13:32:42 +0300196 struct device *kdev = kobj_to_dev(kobj);
Chris Wilson261ea7e2019-10-04 11:59:58 +0100197 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700198 int slice = (int)(uintptr_t)attr->private;
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100199 u32 *remap_info, *freeme = NULL;
Chris Wilson261ea7e2019-10-04 11:59:58 +0100200 struct i915_gem_context *ctx;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700201 int ret;
202
Chris Wilson261ea7e2019-10-04 11:59:58 +0100203 ret = l3_access_valid(i915, offset);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700204 if (ret)
205 return ret;
206
Chris Wilson261ea7e2019-10-04 11:59:58 +0100207 if (count < sizeof(u32))
208 return -EINVAL;
209
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100210 remap_info = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
211 if (!remap_info)
212 return -ENOMEM;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700213
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100214 spin_lock(&i915->gem.contexts.lock);
215
216 if (i915->l3_parity.remap_info[slice]) {
217 freeme = remap_info;
218 remap_info = i915->l3_parity.remap_info[slice];
219 } else {
220 i915->l3_parity.remap_info[slice] = remap_info;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700221 }
222
Chris Wilson261ea7e2019-10-04 11:59:58 +0100223 count = round_down(count, sizeof(u32));
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100224 memcpy(remap_info + offset / sizeof(u32), buf, count);
Chris Wilson261ea7e2019-10-04 11:59:58 +0100225
226 /* NB: We defer the remapping until we switch to the context */
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100227 list_for_each_entry(ctx, &i915->gem.contexts.list, link)
Chris Wilson261ea7e2019-10-04 11:59:58 +0100228 ctx->remap_slice |= BIT(slice);
229
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100230 spin_unlock(&i915->gem.contexts.lock);
231 kfree(freeme);
232
Chris Wilson261ea7e2019-10-04 11:59:58 +0100233 /*
234 * TODO: Ideally we really want a GPU reset here to make sure errors
Ben Widawsky84bc7582012-05-25 16:56:25 -0700235 * aren't propagated. Since I cannot find a stable way to reset the GPU
236 * at this point it is left as a TODO.
237 */
Ben Widawsky84bc7582012-05-25 16:56:25 -0700238
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100239 return count;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700240}
241
Bhumika Goyal59f3da12017-08-02 22:50:47 +0530242static const struct bin_attribute dpf_attrs = {
Ben Widawsky84bc7582012-05-25 16:56:25 -0700243 .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
244 .size = GEN7_L3LOG_SIZE,
245 .read = i915_l3_read,
246 .write = i915_l3_write,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700247 .mmap = NULL,
248 .private = (void *)0
249};
250
Bhumika Goyal59f3da12017-08-02 22:50:47 +0530251static const struct bin_attribute dpf_attrs_1 = {
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700252 .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
253 .size = GEN7_L3LOG_SIZE,
254 .read = i915_l3_read,
255 .write = i915_l3_write,
256 .mmap = NULL,
257 .private = (void *)1
Ben Widawsky84bc7582012-05-25 16:56:25 -0700258};
259
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200260static ssize_t gt_act_freq_mhz_show(struct device *kdev,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700261 struct device_attribute *attr, char *buf)
262{
Andi Shytie03512e2019-12-13 20:37:35 +0200263 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
264 struct intel_rps *rps = &i915->gt.rps;
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700265
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000266 return sysfs_emit(buf, "%d\n", intel_rps_read_actual_frequency(rps));
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200267}
268
269static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
270 struct device_attribute *attr, char *buf)
271{
Andi Shytie03512e2019-12-13 20:37:35 +0200272 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
273 struct intel_rps *rps = &i915->gt.rps;
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200274
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000275 return sysfs_emit(buf, "%d\n", intel_gpu_freq(rps, rps->cur_freq));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700276}
277
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100278static ssize_t gt_boost_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
279{
Andi Shytie03512e2019-12-13 20:37:35 +0200280 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
281 struct intel_rps *rps = &i915->gt.rps;
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100282
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000283 return sysfs_emit(buf, "%d\n", intel_gpu_freq(rps, rps->boost_freq));
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100284}
285
286static ssize_t gt_boost_freq_mhz_store(struct device *kdev,
287 struct device_attribute *attr,
288 const char *buf, size_t count)
289{
David Weinehall694c2822016-08-22 13:32:43 +0300290 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Andi Shyti3e7abf82019-10-24 22:16:41 +0100291 struct intel_rps *rps = &dev_priv->gt.rps;
Chris Wilson59cd31f2018-03-08 14:26:47 +0000292 bool boost = false;
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100293 ssize_t ret;
Chris Wilson59cd31f2018-03-08 14:26:47 +0000294 u32 val;
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100295
296 ret = kstrtou32(buf, 0, &val);
297 if (ret)
298 return ret;
299
300 /* Validate against (static) hardware limits */
Andi Shyti3e7abf82019-10-24 22:16:41 +0100301 val = intel_freq_opcode(rps, val);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100302 if (val < rps->min_freq || val > rps->max_freq)
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100303 return -EINVAL;
304
Chris Wilsonebb5eb72019-04-26 09:17:21 +0100305 mutex_lock(&rps->lock);
Chris Wilson59cd31f2018-03-08 14:26:47 +0000306 if (val != rps->boost_freq) {
307 rps->boost_freq = val;
308 boost = atomic_read(&rps->num_waiters);
309 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +0100310 mutex_unlock(&rps->lock);
Chris Wilson59cd31f2018-03-08 14:26:47 +0000311 if (boost)
312 schedule_work(&rps->work);
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100313
314 return count;
315}
316
Chris Wilson97e4eed2013-08-26 16:18:54 +0100317static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
318 struct device_attribute *attr, char *buf)
319{
David Weinehall694c2822016-08-22 13:32:43 +0300320 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Andi Shyti3e7abf82019-10-24 22:16:41 +0100321 struct intel_rps *rps = &dev_priv->gt.rps;
Chris Wilson97e4eed2013-08-26 16:18:54 +0100322
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000323 return sysfs_emit(buf, "%d\n", intel_gpu_freq(rps, rps->efficient_freq));
Chris Wilson97e4eed2013-08-26 16:18:54 +0100324}
325
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700326static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
327{
David Weinehall694c2822016-08-22 13:32:43 +0300328 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Andi Shyti3e7abf82019-10-24 22:16:41 +0100329 struct intel_rps *rps = &dev_priv->gt.rps;
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700330
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000331 return sysfs_emit(buf, "%d\n", intel_gpu_freq(rps, rps->max_freq_softlimit));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700332}
333
Ben Widawsky46ddf192012-09-12 18:12:07 -0700334static ssize_t gt_max_freq_mhz_store(struct device *kdev,
335 struct device_attribute *attr,
336 const char *buf, size_t count)
337{
David Weinehall694c2822016-08-22 13:32:43 +0300338 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Andi Shyti3e7abf82019-10-24 22:16:41 +0100339 struct intel_rps *rps = &dev_priv->gt.rps;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700340 ssize_t ret;
Andi Shyti3e7abf82019-10-24 22:16:41 +0100341 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700342
343 ret = kstrtou32(buf, 0, &val);
344 if (ret)
345 return ret;
346
Chris Wilsonebb5eb72019-04-26 09:17:21 +0100347 mutex_lock(&rps->lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700348
Andi Shyti3e7abf82019-10-24 22:16:41 +0100349 val = intel_freq_opcode(rps, val);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100350 if (val < rps->min_freq ||
351 val > rps->max_freq ||
352 val < rps->min_freq_softlimit) {
Chris Wilsonebb5eb72019-04-26 09:17:21 +0100353 ret = -EINVAL;
354 goto unlock;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700355 }
356
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100357 if (val > rps->rp0_freq)
Ben Widawsky31c77382013-04-05 14:29:22 -0700358 DRM_DEBUG("User requested overclocking to %d\n",
Andi Shyti3e7abf82019-10-24 22:16:41 +0100359 intel_gpu_freq(rps, val));
Ben Widawsky31c77382013-04-05 14:29:22 -0700360
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100361 rps->max_freq_softlimit = val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700362
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100363 val = clamp_t(int, rps->cur_freq,
364 rps->min_freq_softlimit,
365 rps->max_freq_softlimit);
Ville Syrjäläf745a802015-01-23 21:04:23 +0200366
Andi Shyti3e7abf82019-10-24 22:16:41 +0100367 /*
368 * We still need *_set_rps to process the new max_delay and
Ville Syrjäläf745a802015-01-23 21:04:23 +0200369 * update the interrupt limits and PMINTRMSK even though
Andi Shyti3e7abf82019-10-24 22:16:41 +0100370 * frequency request may be unchanged.
371 */
372 intel_rps_set(rps, val);
Chris Wilson6917c7b2013-11-06 13:56:26 -0200373
Chris Wilsonebb5eb72019-04-26 09:17:21 +0100374unlock:
375 mutex_unlock(&rps->lock);
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530376
Chris Wilson9fcee2f2017-01-26 10:19:19 +0000377 return ret ?: count;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700378}
379
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700380static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
381{
David Weinehall694c2822016-08-22 13:32:43 +0300382 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Andi Shyti3e7abf82019-10-24 22:16:41 +0100383 struct intel_rps *rps = &dev_priv->gt.rps;
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700384
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000385 return sysfs_emit(buf, "%d\n", intel_gpu_freq(rps, rps->min_freq_softlimit));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700386}
387
Ben Widawsky46ddf192012-09-12 18:12:07 -0700388static ssize_t gt_min_freq_mhz_store(struct device *kdev,
389 struct device_attribute *attr,
390 const char *buf, size_t count)
391{
David Weinehall694c2822016-08-22 13:32:43 +0300392 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Andi Shyti3e7abf82019-10-24 22:16:41 +0100393 struct intel_rps *rps = &dev_priv->gt.rps;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700394 ssize_t ret;
Andi Shyti3e7abf82019-10-24 22:16:41 +0100395 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700396
397 ret = kstrtou32(buf, 0, &val);
398 if (ret)
399 return ret;
400
Chris Wilsonebb5eb72019-04-26 09:17:21 +0100401 mutex_lock(&rps->lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700402
Andi Shyti3e7abf82019-10-24 22:16:41 +0100403 val = intel_freq_opcode(rps, val);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100404 if (val < rps->min_freq ||
405 val > rps->max_freq ||
406 val > rps->max_freq_softlimit) {
Chris Wilsonebb5eb72019-04-26 09:17:21 +0100407 ret = -EINVAL;
408 goto unlock;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700409 }
410
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100411 rps->min_freq_softlimit = val;
Chris Wilson6917c7b2013-11-06 13:56:26 -0200412
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100413 val = clamp_t(int, rps->cur_freq,
414 rps->min_freq_softlimit,
415 rps->max_freq_softlimit);
Ville Syrjäläf745a802015-01-23 21:04:23 +0200416
Andi Shyti3e7abf82019-10-24 22:16:41 +0100417 /*
418 * We still need *_set_rps to process the new min_delay and
Ville Syrjäläf745a802015-01-23 21:04:23 +0200419 * update the interrupt limits and PMINTRMSK even though
Andi Shyti3e7abf82019-10-24 22:16:41 +0100420 * frequency request may be unchanged.
421 */
422 intel_rps_set(rps, val);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700423
Chris Wilsonebb5eb72019-04-26 09:17:21 +0100424unlock:
425 mutex_unlock(&rps->lock);
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530426
Chris Wilson9fcee2f2017-01-26 10:19:19 +0000427 return ret ?: count;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700428}
429
Joe Perchesc828a892017-12-19 10:15:08 -0800430static DEVICE_ATTR_RO(gt_act_freq_mhz);
431static DEVICE_ATTR_RO(gt_cur_freq_mhz);
Joe Perchesb6b996b2017-12-19 10:15:07 -0800432static DEVICE_ATTR_RW(gt_boost_freq_mhz);
433static DEVICE_ATTR_RW(gt_max_freq_mhz);
434static DEVICE_ATTR_RW(gt_min_freq_mhz);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700435
Joe Perchesc828a892017-12-19 10:15:08 -0800436static DEVICE_ATTR_RO(vlv_rpe_freq_mhz);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700437
438static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
439static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
440static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
441static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
442
443/* For now we have a static number of RP states */
444static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
445{
David Weinehall694c2822016-08-22 13:32:43 +0300446 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Andi Shyti3e7abf82019-10-24 22:16:41 +0100447 struct intel_rps *rps = &dev_priv->gt.rps;
Akash Goelbc4d91f2015-02-26 16:09:47 +0530448 u32 val;
Ben Widawskyac6ae342012-09-07 19:43:44 -0700449
Akash Goelbc4d91f2015-02-26 16:09:47 +0530450 if (attr == &dev_attr_gt_RP0_freq_mhz)
Andi Shyti3e7abf82019-10-24 22:16:41 +0100451 val = intel_gpu_freq(rps, rps->rp0_freq);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530452 else if (attr == &dev_attr_gt_RP1_freq_mhz)
Andi Shyti3e7abf82019-10-24 22:16:41 +0100453 val = intel_gpu_freq(rps, rps->rp1_freq);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530454 else if (attr == &dev_attr_gt_RPn_freq_mhz)
Andi Shyti3e7abf82019-10-24 22:16:41 +0100455 val = intel_gpu_freq(rps, rps->min_freq);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530456 else
Ben Widawskyac6ae342012-09-07 19:43:44 -0700457 BUG();
Akash Goelbc4d91f2015-02-26 16:09:47 +0530458
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000459 return sysfs_emit(buf, "%d\n", val);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700460}
461
Jani Nikulae1215de2018-10-04 17:37:50 +0300462static const struct attribute * const gen6_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200463 &dev_attr_gt_act_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700464 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100465 &dev_attr_gt_boost_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700466 &dev_attr_gt_max_freq_mhz.attr,
467 &dev_attr_gt_min_freq_mhz.attr,
Ben Widawskyac6ae342012-09-07 19:43:44 -0700468 &dev_attr_gt_RP0_freq_mhz.attr,
469 &dev_attr_gt_RP1_freq_mhz.attr,
470 &dev_attr_gt_RPn_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700471 NULL,
472};
473
Jani Nikulae1215de2018-10-04 17:37:50 +0300474static const struct attribute * const vlv_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200475 &dev_attr_gt_act_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100476 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100477 &dev_attr_gt_boost_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100478 &dev_attr_gt_max_freq_mhz.attr,
479 &dev_attr_gt_min_freq_mhz.attr,
Deepak S74c4f622014-07-10 13:16:22 +0530480 &dev_attr_gt_RP0_freq_mhz.attr,
481 &dev_attr_gt_RP1_freq_mhz.attr,
482 &dev_attr_gt_RPn_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100483 &dev_attr_vlv_rpe_freq_mhz.attr,
484 NULL,
485};
486
Chris Wilson98a2f412016-10-12 10:05:18 +0100487#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
488
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300489static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
490 struct bin_attribute *attr, char *buf,
491 loff_t off, size_t count)
492{
493
Geliang Tang657fb5f2016-01-13 22:48:40 +0800494 struct device *kdev = kobj_to_dev(kobj);
Chris Wilson0e390372018-11-23 13:23:25 +0000495 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
Chris Wilson742379c2020-01-10 12:30:56 +0000496 struct i915_gpu_coredump *gpu;
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000497 ssize_t ret;
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300498
Chris Wilson0e390372018-11-23 13:23:25 +0000499 gpu = i915_first_error_state(i915);
Chris Wilsone6154e42018-12-07 11:05:54 +0000500 if (IS_ERR(gpu)) {
501 ret = PTR_ERR(gpu);
502 } else if (gpu) {
Chris Wilson742379c2020-01-10 12:30:56 +0000503 ret = i915_gpu_coredump_copy_to_buffer(gpu, buf, off, count);
504 i915_gpu_coredump_put(gpu);
Chris Wilson0e390372018-11-23 13:23:25 +0000505 } else {
506 const char *str = "No error state collected\n";
507 size_t len = strlen(str);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300508
Chris Wilson0e390372018-11-23 13:23:25 +0000509 ret = min_t(size_t, count, len - off);
510 memcpy(buf, str + off, ret);
511 }
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300512
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000513 return ret;
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300514}
515
516static ssize_t error_state_write(struct file *file, struct kobject *kobj,
517 struct bin_attribute *attr, char *buf,
518 loff_t off, size_t count)
519{
Geliang Tang657fb5f2016-01-13 22:48:40 +0800520 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300521 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300522
Wambui Karuga00376cc2020-01-31 12:34:12 +0300523 drm_dbg(&dev_priv->drm, "Resetting error state\n");
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000524 i915_reset_error_state(dev_priv);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300525
526 return count;
527}
528
Bhumika Goyal59f3da12017-08-02 22:50:47 +0530529static const struct bin_attribute error_state_attr = {
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300530 .attr.name = "error",
531 .attr.mode = S_IRUSR | S_IWUSR,
532 .size = 0,
533 .read = error_state_read,
534 .write = error_state_write,
535};
536
Chris Wilson98a2f412016-10-12 10:05:18 +0100537static void i915_setup_error_capture(struct device *kdev)
538{
539 if (sysfs_create_bin_file(&kdev->kobj, &error_state_attr))
540 DRM_ERROR("error_state sysfs setup failed\n");
541}
542
543static void i915_teardown_error_capture(struct device *kdev)
544{
545 sysfs_remove_bin_file(&kdev->kobj, &error_state_attr);
546}
547#else
548static void i915_setup_error_capture(struct device *kdev) {}
549static void i915_teardown_error_capture(struct device *kdev) {}
550#endif
551
David Weinehall694c2822016-08-22 13:32:43 +0300552void i915_setup_sysfs(struct drm_i915_private *dev_priv)
Ben Widawsky0136db52012-04-10 21:17:01 -0700553{
David Weinehall694c2822016-08-22 13:32:43 +0300554 struct device *kdev = dev_priv->drm.primary->kdev;
Ben Widawsky0136db52012-04-10 21:17:01 -0700555 int ret;
556
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700557#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +0300558 if (HAS_RC6(dev_priv)) {
559 ret = sysfs_merge_group(&kdev->kobj,
Daniel Vetter112abd22012-05-31 14:57:43 +0200560 &rc6_attr_group);
561 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300562 drm_err(&dev_priv->drm,
563 "RC6 residency sysfs setup failed\n");
Daniel Vetter112abd22012-05-31 14:57:43 +0200564 }
David Weinehall694c2822016-08-22 13:32:43 +0300565 if (HAS_RC6p(dev_priv)) {
566 ret = sysfs_merge_group(&kdev->kobj,
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700567 &rc6p_attr_group);
568 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300569 drm_err(&dev_priv->drm,
570 "RC6p residency sysfs setup failed\n");
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700571 }
David Weinehall694c2822016-08-22 13:32:43 +0300572 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
573 ret = sysfs_merge_group(&kdev->kobj,
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530574 &media_rc6_attr_group);
575 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300576 drm_err(&dev_priv->drm,
577 "Media RC6 residency sysfs setup failed\n");
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530578 }
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700579#endif
David Weinehall694c2822016-08-22 13:32:43 +0300580 if (HAS_L3_DPF(dev_priv)) {
581 ret = device_create_bin_file(kdev, &dpf_attrs);
Daniel Vetter112abd22012-05-31 14:57:43 +0200582 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300583 drm_err(&dev_priv->drm,
584 "l3 parity sysfs setup failed\n");
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700585
David Weinehall694c2822016-08-22 13:32:43 +0300586 if (NUM_L3_SLICES(dev_priv) > 1) {
587 ret = device_create_bin_file(kdev,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700588 &dpf_attrs_1);
589 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300590 drm_err(&dev_priv->drm,
591 "l3 parity slice 1 setup failed\n");
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700592 }
Daniel Vetter112abd22012-05-31 14:57:43 +0200593 }
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700594
Chris Wilson97e4eed2013-08-26 16:18:54 +0100595 ret = 0;
David Weinehall694c2822016-08-22 13:32:43 +0300596 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
597 ret = sysfs_create_files(&kdev->kobj, vlv_attrs);
Lucas De Marchi651e7d42021-06-05 21:50:49 -0700598 else if (GRAPHICS_VER(dev_priv) >= 6)
David Weinehall694c2822016-08-22 13:32:43 +0300599 ret = sysfs_create_files(&kdev->kobj, gen6_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100600 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300601 drm_err(&dev_priv->drm, "RPS sysfs setup failed\n");
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300602
Chris Wilson98a2f412016-10-12 10:05:18 +0100603 i915_setup_error_capture(kdev);
Chris Wilson4ec76db2020-02-28 13:17:10 +0000604
605 intel_engines_add_sysfs(dev_priv);
Ben Widawsky0136db52012-04-10 21:17:01 -0700606}
607
David Weinehall694c2822016-08-22 13:32:43 +0300608void i915_teardown_sysfs(struct drm_i915_private *dev_priv)
Ben Widawsky0136db52012-04-10 21:17:01 -0700609{
David Weinehall694c2822016-08-22 13:32:43 +0300610 struct device *kdev = dev_priv->drm.primary->kdev;
611
Chris Wilson98a2f412016-10-12 10:05:18 +0100612 i915_teardown_error_capture(kdev);
613
David Weinehall694c2822016-08-22 13:32:43 +0300614 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
615 sysfs_remove_files(&kdev->kobj, vlv_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100616 else
David Weinehall694c2822016-08-22 13:32:43 +0300617 sysfs_remove_files(&kdev->kobj, gen6_attrs);
618 device_remove_bin_file(kdev, &dpf_attrs_1);
619 device_remove_bin_file(kdev, &dpf_attrs);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700620#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +0300621 sysfs_unmerge_group(&kdev->kobj, &rc6_attr_group);
622 sysfs_unmerge_group(&kdev->kobj, &rc6p_attr_group);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700623#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700624}