blob: 1804f4142740902c83bca27b2cfbaf91ea2d55dc [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"
Ben Widawsky0136db52012-04-10 21:17:01 -070040
David Weinehall694c2822016-08-22 13:32:43 +030041static inline struct drm_i915_private *kdev_minor_to_i915(struct device *kdev)
David Weinehallc49d13e2016-08-22 13:32:42 +030042{
David Weinehall694c2822016-08-22 13:32:43 +030043 struct drm_minor *minor = dev_get_drvdata(kdev);
44 return to_i915(minor->dev);
David Weinehallc49d13e2016-08-22 13:32:42 +030045}
Dave Airlie14c8d1102013-10-11 14:45:30 +100046
Hunt Xu5ab36332012-07-01 03:45:07 +000047#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +030048static u32 calc_residency(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020049 i915_reg_t reg)
Ben Widawsky0136db52012-04-10 21:17:01 -070050{
Chris Wilson48d1c812019-01-14 14:21:13 +000051 intel_wakeref_t wakeref;
Chris Wilsond4225a52019-01-14 14:21:23 +000052 u64 res = 0;
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +000053
Daniele Ceraolo Spurioc447ff72019-06-13 16:21:55 -070054 with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref)
Andi Shytic1132362019-09-27 12:08:49 +010055 res = intel_rc6_residency_us(&dev_priv->gt.rc6, reg);
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +000056
57 return DIV_ROUND_CLOSEST_ULL(res, 1000);
Ben Widawsky0136db52012-04-10 21:17:01 -070058}
59
YueHaibing177f30c2021-05-28 18:04:03 +080060static ssize_t rc6_enable_show(struct device *kdev,
61 struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070062{
Chris Wilsonfb6db0f2017-12-01 11:30:30 +000063 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
64 unsigned int mask;
65
66 mask = 0;
67 if (HAS_RC6(dev_priv))
68 mask |= BIT(0);
69 if (HAS_RC6p(dev_priv))
70 mask |= BIT(1);
71 if (HAS_RC6pp(dev_priv))
72 mask |= BIT(2);
73
Xuezhi Zhang11cda492021-04-04 08:41:03 +000074 return sysfs_emit(buf, "%x\n", mask);
Ben Widawsky0136db52012-04-10 21:17:01 -070075}
76
YueHaibing177f30c2021-05-28 18:04:03 +080077static ssize_t rc6_residency_ms_show(struct device *kdev,
78 struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070079{
David Weinehall694c2822016-08-22 13:32:43 +030080 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
81 u32 rc6_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6);
Xuezhi Zhang11cda492021-04-04 08:41:03 +000082 return sysfs_emit(buf, "%u\n", rc6_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070083}
84
YueHaibing177f30c2021-05-28 18:04:03 +080085static ssize_t rc6p_residency_ms_show(struct device *kdev,
86 struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070087{
David Weinehall694c2822016-08-22 13:32:43 +030088 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
89 u32 rc6p_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6p);
Xuezhi Zhang11cda492021-04-04 08:41:03 +000090 return sysfs_emit(buf, "%u\n", rc6p_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070091}
92
YueHaibing177f30c2021-05-28 18:04:03 +080093static ssize_t rc6pp_residency_ms_show(struct device *kdev,
94 struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070095{
David Weinehall694c2822016-08-22 13:32:43 +030096 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
97 u32 rc6pp_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6pp);
Xuezhi Zhang11cda492021-04-04 08:41:03 +000098 return sysfs_emit(buf, "%u\n", rc6pp_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070099}
100
YueHaibing177f30c2021-05-28 18:04:03 +0800101static ssize_t media_rc6_residency_ms_show(struct device *kdev,
102 struct device_attribute *attr, char *buf)
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530103{
David Weinehall694c2822016-08-22 13:32:43 +0300104 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
105 u32 rc6_residency = calc_residency(dev_priv, VLV_GT_MEDIA_RC6);
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000106 return sysfs_emit(buf, "%u\n", rc6_residency);
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530107}
108
YueHaibing177f30c2021-05-28 18:04:03 +0800109static DEVICE_ATTR_RO(rc6_enable);
110static DEVICE_ATTR_RO(rc6_residency_ms);
111static DEVICE_ATTR_RO(rc6p_residency_ms);
112static DEVICE_ATTR_RO(rc6pp_residency_ms);
113static DEVICE_ATTR_RO(media_rc6_residency_ms);
Ben Widawsky0136db52012-04-10 21:17:01 -0700114
115static struct attribute *rc6_attrs[] = {
116 &dev_attr_rc6_enable.attr,
117 &dev_attr_rc6_residency_ms.attr,
Ben Widawsky0136db52012-04-10 21:17:01 -0700118 NULL
119};
120
Arvind Yadav0a7a0982017-07-03 16:38:25 +0530121static const struct attribute_group rc6_attr_group = {
Ben Widawsky0136db52012-04-10 21:17:01 -0700122 .name = power_group_name,
123 .attrs = rc6_attrs
124};
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700125
126static struct attribute *rc6p_attrs[] = {
127 &dev_attr_rc6p_residency_ms.attr,
128 &dev_attr_rc6pp_residency_ms.attr,
129 NULL
130};
131
Arvind Yadav0a7a0982017-07-03 16:38:25 +0530132static const struct attribute_group rc6p_attr_group = {
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700133 .name = power_group_name,
134 .attrs = rc6p_attrs
135};
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530136
137static struct attribute *media_rc6_attrs[] = {
138 &dev_attr_media_rc6_residency_ms.attr,
139 NULL
140};
141
Arvind Yadav0a7a0982017-07-03 16:38:25 +0530142static const struct attribute_group media_rc6_attr_group = {
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530143 .name = power_group_name,
144 .attrs = media_rc6_attrs
145};
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700146#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700147
Chris Wilson261ea7e2019-10-04 11:59:58 +0100148static int l3_access_valid(struct drm_i915_private *i915, loff_t offset)
Ben Widawsky84bc7582012-05-25 16:56:25 -0700149{
Chris Wilson261ea7e2019-10-04 11:59:58 +0100150 if (!HAS_L3_DPF(i915))
Ben Widawsky84bc7582012-05-25 16:56:25 -0700151 return -EPERM;
152
Chris Wilson261ea7e2019-10-04 11:59:58 +0100153 if (!IS_ALIGNED(offset, sizeof(u32)))
Ben Widawsky84bc7582012-05-25 16:56:25 -0700154 return -EINVAL;
155
156 if (offset >= GEN7_L3LOG_SIZE)
157 return -ENXIO;
158
159 return 0;
160}
161
162static ssize_t
163i915_l3_read(struct file *filp, struct kobject *kobj,
164 struct bin_attribute *attr, char *buf,
165 loff_t offset, size_t count)
166{
David Weinehallc49d13e2016-08-22 13:32:42 +0300167 struct device *kdev = kobj_to_dev(kobj);
Chris Wilson261ea7e2019-10-04 11:59:58 +0100168 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700169 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700170 int ret;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700171
Chris Wilson261ea7e2019-10-04 11:59:58 +0100172 ret = l3_access_valid(i915, offset);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700173 if (ret)
174 return ret;
175
Chris Wilson261ea7e2019-10-04 11:59:58 +0100176 count = round_down(count, sizeof(u32));
Dan Carpentere5ad4022013-09-20 14:20:18 +0300177 count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
Chris Wilson261ea7e2019-10-04 11:59:58 +0100178 memset(buf, 0, count);
Ben Widawsky33618ea2013-09-12 22:28:29 -0700179
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100180 spin_lock(&i915->gem.contexts.lock);
Chris Wilson261ea7e2019-10-04 11:59:58 +0100181 if (i915->l3_parity.remap_info[slice])
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700182 memcpy(buf,
Chris Wilson261ea7e2019-10-04 11:59:58 +0100183 i915->l3_parity.remap_info[slice] + offset / sizeof(u32),
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700184 count);
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100185 spin_unlock(&i915->gem.contexts.lock);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700186
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700187 return count;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700188}
189
190static ssize_t
191i915_l3_write(struct file *filp, struct kobject *kobj,
192 struct bin_attribute *attr, char *buf,
193 loff_t offset, size_t count)
194{
David Weinehallc49d13e2016-08-22 13:32:42 +0300195 struct device *kdev = kobj_to_dev(kobj);
Chris Wilson261ea7e2019-10-04 11:59:58 +0100196 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700197 int slice = (int)(uintptr_t)attr->private;
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100198 u32 *remap_info, *freeme = NULL;
Chris Wilson261ea7e2019-10-04 11:59:58 +0100199 struct i915_gem_context *ctx;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700200 int ret;
201
Chris Wilson261ea7e2019-10-04 11:59:58 +0100202 ret = l3_access_valid(i915, offset);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700203 if (ret)
204 return ret;
205
Chris Wilson261ea7e2019-10-04 11:59:58 +0100206 if (count < sizeof(u32))
207 return -EINVAL;
208
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100209 remap_info = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
210 if (!remap_info)
211 return -ENOMEM;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700212
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100213 spin_lock(&i915->gem.contexts.lock);
214
215 if (i915->l3_parity.remap_info[slice]) {
216 freeme = remap_info;
217 remap_info = i915->l3_parity.remap_info[slice];
218 } else {
219 i915->l3_parity.remap_info[slice] = remap_info;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700220 }
221
Chris Wilson261ea7e2019-10-04 11:59:58 +0100222 count = round_down(count, sizeof(u32));
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100223 memcpy(remap_info + offset / sizeof(u32), buf, count);
Chris Wilson261ea7e2019-10-04 11:59:58 +0100224
225 /* NB: We defer the remapping until we switch to the context */
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100226 list_for_each_entry(ctx, &i915->gem.contexts.list, link)
Chris Wilson261ea7e2019-10-04 11:59:58 +0100227 ctx->remap_slice |= BIT(slice);
228
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100229 spin_unlock(&i915->gem.contexts.lock);
230 kfree(freeme);
231
Chris Wilson261ea7e2019-10-04 11:59:58 +0100232 /*
233 * TODO: Ideally we really want a GPU reset here to make sure errors
Ben Widawsky84bc7582012-05-25 16:56:25 -0700234 * aren't propagated. Since I cannot find a stable way to reset the GPU
235 * at this point it is left as a TODO.
236 */
Ben Widawsky84bc7582012-05-25 16:56:25 -0700237
Chris Wilsona4e7ccd2019-10-04 14:40:09 +0100238 return count;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700239}
240
Bhumika Goyal59f3da12017-08-02 22:50:47 +0530241static const struct bin_attribute dpf_attrs = {
Ben Widawsky84bc7582012-05-25 16:56:25 -0700242 .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
243 .size = GEN7_L3LOG_SIZE,
244 .read = i915_l3_read,
245 .write = i915_l3_write,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700246 .mmap = NULL,
247 .private = (void *)0
248};
249
Bhumika Goyal59f3da12017-08-02 22:50:47 +0530250static const struct bin_attribute dpf_attrs_1 = {
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700251 .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
252 .size = GEN7_L3LOG_SIZE,
253 .read = i915_l3_read,
254 .write = i915_l3_write,
255 .mmap = NULL,
256 .private = (void *)1
Ben Widawsky84bc7582012-05-25 16:56:25 -0700257};
258
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200259static ssize_t gt_act_freq_mhz_show(struct device *kdev,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700260 struct device_attribute *attr, char *buf)
261{
Andi Shytie03512e2019-12-13 20:37:35 +0200262 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
263 struct intel_rps *rps = &i915->gt.rps;
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700264
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000265 return sysfs_emit(buf, "%d\n", intel_rps_read_actual_frequency(rps));
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200266}
267
268static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
269 struct device_attribute *attr, char *buf)
270{
Andi Shytie03512e2019-12-13 20:37:35 +0200271 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
272 struct intel_rps *rps = &i915->gt.rps;
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200273
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700274 return sysfs_emit(buf, "%d\n", intel_rps_get_requested_frequency(rps));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700275}
276
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100277static ssize_t gt_boost_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
278{
Andi Shytie03512e2019-12-13 20:37:35 +0200279 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
280 struct intel_rps *rps = &i915->gt.rps;
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100281
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000282 return sysfs_emit(buf, "%d\n", intel_gpu_freq(rps, rps->boost_freq));
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100283}
284
285static ssize_t gt_boost_freq_mhz_store(struct device *kdev,
286 struct device_attribute *attr,
287 const char *buf, size_t count)
288{
David Weinehall694c2822016-08-22 13:32:43 +0300289 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Andi Shyti3e7abf82019-10-24 22:16:41 +0100290 struct intel_rps *rps = &dev_priv->gt.rps;
Chris Wilson59cd31f2018-03-08 14:26:47 +0000291 bool boost = false;
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100292 ssize_t ret;
Chris Wilson59cd31f2018-03-08 14:26:47 +0000293 u32 val;
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100294
295 ret = kstrtou32(buf, 0, &val);
296 if (ret)
297 return ret;
298
299 /* Validate against (static) hardware limits */
Andi Shyti3e7abf82019-10-24 22:16:41 +0100300 val = intel_freq_opcode(rps, val);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100301 if (val < rps->min_freq || val > rps->max_freq)
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100302 return -EINVAL;
303
Chris Wilsonebb5eb72019-04-26 09:17:21 +0100304 mutex_lock(&rps->lock);
Chris Wilson59cd31f2018-03-08 14:26:47 +0000305 if (val != rps->boost_freq) {
306 rps->boost_freq = val;
307 boost = atomic_read(&rps->num_waiters);
308 }
Chris Wilsonebb5eb72019-04-26 09:17:21 +0100309 mutex_unlock(&rps->lock);
Chris Wilson59cd31f2018-03-08 14:26:47 +0000310 if (boost)
311 schedule_work(&rps->work);
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100312
313 return count;
314}
315
Chris Wilson97e4eed2013-08-26 16:18:54 +0100316static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
317 struct device_attribute *attr, char *buf)
318{
David Weinehall694c2822016-08-22 13:32:43 +0300319 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Andi Shyti3e7abf82019-10-24 22:16:41 +0100320 struct intel_rps *rps = &dev_priv->gt.rps;
Chris Wilson97e4eed2013-08-26 16:18:54 +0100321
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000322 return sysfs_emit(buf, "%d\n", intel_gpu_freq(rps, rps->efficient_freq));
Chris Wilson97e4eed2013-08-26 16:18:54 +0100323}
324
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700325static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
326{
David Weinehall694c2822016-08-22 13:32:43 +0300327 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700328 struct intel_gt *gt = &dev_priv->gt;
329 struct intel_rps *rps = &gt->rps;
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700330
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700331 return sysfs_emit(buf, "%d\n", intel_rps_get_max_frequency(rps));
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);
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700339 struct intel_gt *gt = &dev_priv->gt;
340 struct intel_rps *rps = &gt->rps;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700341 ssize_t ret;
Andi Shyti3e7abf82019-10-24 22:16:41 +0100342 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700343
344 ret = kstrtou32(buf, 0, &val);
345 if (ret)
346 return ret;
347
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700348 ret = intel_rps_set_max_frequency(rps, val);
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530349
Chris Wilson9fcee2f2017-01-26 10:19:19 +0000350 return ret ?: count;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700351}
352
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700353static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
354{
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700355 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
356 struct intel_gt *gt = &i915->gt;
357 struct intel_rps *rps = &gt->rps;
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700358
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700359 return sysfs_emit(buf, "%d\n", intel_rps_get_min_frequency(rps));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700360}
361
Ben Widawsky46ddf192012-09-12 18:12:07 -0700362static ssize_t gt_min_freq_mhz_store(struct device *kdev,
363 struct device_attribute *attr,
364 const char *buf, size_t count)
365{
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700366 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
367 struct intel_rps *rps = &i915->gt.rps;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700368 ssize_t ret;
Andi Shyti3e7abf82019-10-24 22:16:41 +0100369 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700370
371 ret = kstrtou32(buf, 0, &val);
372 if (ret)
373 return ret;
374
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700375 ret = intel_rps_set_min_frequency(rps, val);
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
Joe Perchesc828a892017-12-19 10:15:08 -0800380static DEVICE_ATTR_RO(gt_act_freq_mhz);
381static DEVICE_ATTR_RO(gt_cur_freq_mhz);
Joe Perchesb6b996b2017-12-19 10:15:07 -0800382static DEVICE_ATTR_RW(gt_boost_freq_mhz);
383static DEVICE_ATTR_RW(gt_max_freq_mhz);
384static DEVICE_ATTR_RW(gt_min_freq_mhz);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700385
Joe Perchesc828a892017-12-19 10:15:08 -0800386static DEVICE_ATTR_RO(vlv_rpe_freq_mhz);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700387
388static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
389static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
390static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
391static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
392
393/* For now we have a static number of RP states */
394static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
395{
David Weinehall694c2822016-08-22 13:32:43 +0300396 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Andi Shyti3e7abf82019-10-24 22:16:41 +0100397 struct intel_rps *rps = &dev_priv->gt.rps;
Akash Goelbc4d91f2015-02-26 16:09:47 +0530398 u32 val;
Ben Widawskyac6ae342012-09-07 19:43:44 -0700399
Akash Goelbc4d91f2015-02-26 16:09:47 +0530400 if (attr == &dev_attr_gt_RP0_freq_mhz)
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700401 val = intel_rps_get_rp0_frequency(rps);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530402 else if (attr == &dev_attr_gt_RP1_freq_mhz)
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700403 val = intel_rps_get_rp1_frequency(rps);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530404 else if (attr == &dev_attr_gt_RPn_freq_mhz)
Vinay Belgaumkar41e5c172021-07-30 13:21:17 -0700405 val = intel_rps_get_rpn_frequency(rps);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530406 else
Ben Widawskyac6ae342012-09-07 19:43:44 -0700407 BUG();
Akash Goelbc4d91f2015-02-26 16:09:47 +0530408
Xuezhi Zhang11cda492021-04-04 08:41:03 +0000409 return sysfs_emit(buf, "%d\n", val);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700410}
411
Jani Nikulae1215de2018-10-04 17:37:50 +0300412static const struct attribute * const gen6_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200413 &dev_attr_gt_act_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700414 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100415 &dev_attr_gt_boost_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700416 &dev_attr_gt_max_freq_mhz.attr,
417 &dev_attr_gt_min_freq_mhz.attr,
Ben Widawskyac6ae342012-09-07 19:43:44 -0700418 &dev_attr_gt_RP0_freq_mhz.attr,
419 &dev_attr_gt_RP1_freq_mhz.attr,
420 &dev_attr_gt_RPn_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700421 NULL,
422};
423
Jani Nikulae1215de2018-10-04 17:37:50 +0300424static const struct attribute * const vlv_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200425 &dev_attr_gt_act_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100426 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100427 &dev_attr_gt_boost_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100428 &dev_attr_gt_max_freq_mhz.attr,
429 &dev_attr_gt_min_freq_mhz.attr,
Deepak S74c4f622014-07-10 13:16:22 +0530430 &dev_attr_gt_RP0_freq_mhz.attr,
431 &dev_attr_gt_RP1_freq_mhz.attr,
432 &dev_attr_gt_RPn_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100433 &dev_attr_vlv_rpe_freq_mhz.attr,
434 NULL,
435};
436
Chris Wilson98a2f412016-10-12 10:05:18 +0100437#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
438
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300439static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
440 struct bin_attribute *attr, char *buf,
441 loff_t off, size_t count)
442{
443
Geliang Tang657fb5f2016-01-13 22:48:40 +0800444 struct device *kdev = kobj_to_dev(kobj);
Chris Wilson0e390372018-11-23 13:23:25 +0000445 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
Chris Wilson742379c2020-01-10 12:30:56 +0000446 struct i915_gpu_coredump *gpu;
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000447 ssize_t ret;
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300448
Chris Wilson0e390372018-11-23 13:23:25 +0000449 gpu = i915_first_error_state(i915);
Chris Wilsone6154e42018-12-07 11:05:54 +0000450 if (IS_ERR(gpu)) {
451 ret = PTR_ERR(gpu);
452 } else if (gpu) {
Chris Wilson742379c2020-01-10 12:30:56 +0000453 ret = i915_gpu_coredump_copy_to_buffer(gpu, buf, off, count);
454 i915_gpu_coredump_put(gpu);
Chris Wilson0e390372018-11-23 13:23:25 +0000455 } else {
456 const char *str = "No error state collected\n";
457 size_t len = strlen(str);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300458
Chris Wilson0e390372018-11-23 13:23:25 +0000459 ret = min_t(size_t, count, len - off);
460 memcpy(buf, str + off, ret);
461 }
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300462
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000463 return ret;
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300464}
465
466static ssize_t error_state_write(struct file *file, struct kobject *kobj,
467 struct bin_attribute *attr, char *buf,
468 loff_t off, size_t count)
469{
Geliang Tang657fb5f2016-01-13 22:48:40 +0800470 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300471 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300472
Wambui Karuga00376cc2020-01-31 12:34:12 +0300473 drm_dbg(&dev_priv->drm, "Resetting error state\n");
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000474 i915_reset_error_state(dev_priv);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300475
476 return count;
477}
478
Bhumika Goyal59f3da12017-08-02 22:50:47 +0530479static const struct bin_attribute error_state_attr = {
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300480 .attr.name = "error",
481 .attr.mode = S_IRUSR | S_IWUSR,
482 .size = 0,
483 .read = error_state_read,
484 .write = error_state_write,
485};
486
Chris Wilson98a2f412016-10-12 10:05:18 +0100487static void i915_setup_error_capture(struct device *kdev)
488{
489 if (sysfs_create_bin_file(&kdev->kobj, &error_state_attr))
490 DRM_ERROR("error_state sysfs setup failed\n");
491}
492
493static void i915_teardown_error_capture(struct device *kdev)
494{
495 sysfs_remove_bin_file(&kdev->kobj, &error_state_attr);
496}
497#else
498static void i915_setup_error_capture(struct device *kdev) {}
499static void i915_teardown_error_capture(struct device *kdev) {}
500#endif
501
David Weinehall694c2822016-08-22 13:32:43 +0300502void i915_setup_sysfs(struct drm_i915_private *dev_priv)
Ben Widawsky0136db52012-04-10 21:17:01 -0700503{
David Weinehall694c2822016-08-22 13:32:43 +0300504 struct device *kdev = dev_priv->drm.primary->kdev;
Ben Widawsky0136db52012-04-10 21:17:01 -0700505 int ret;
506
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700507#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +0300508 if (HAS_RC6(dev_priv)) {
509 ret = sysfs_merge_group(&kdev->kobj,
Daniel Vetter112abd22012-05-31 14:57:43 +0200510 &rc6_attr_group);
511 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300512 drm_err(&dev_priv->drm,
513 "RC6 residency sysfs setup failed\n");
Daniel Vetter112abd22012-05-31 14:57:43 +0200514 }
David Weinehall694c2822016-08-22 13:32:43 +0300515 if (HAS_RC6p(dev_priv)) {
516 ret = sysfs_merge_group(&kdev->kobj,
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700517 &rc6p_attr_group);
518 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300519 drm_err(&dev_priv->drm,
520 "RC6p residency sysfs setup failed\n");
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700521 }
David Weinehall694c2822016-08-22 13:32:43 +0300522 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
523 ret = sysfs_merge_group(&kdev->kobj,
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530524 &media_rc6_attr_group);
525 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300526 drm_err(&dev_priv->drm,
527 "Media RC6 residency sysfs setup failed\n");
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530528 }
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700529#endif
David Weinehall694c2822016-08-22 13:32:43 +0300530 if (HAS_L3_DPF(dev_priv)) {
531 ret = device_create_bin_file(kdev, &dpf_attrs);
Daniel Vetter112abd22012-05-31 14:57:43 +0200532 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300533 drm_err(&dev_priv->drm,
534 "l3 parity sysfs setup failed\n");
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700535
David Weinehall694c2822016-08-22 13:32:43 +0300536 if (NUM_L3_SLICES(dev_priv) > 1) {
537 ret = device_create_bin_file(kdev,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700538 &dpf_attrs_1);
539 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300540 drm_err(&dev_priv->drm,
541 "l3 parity slice 1 setup failed\n");
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700542 }
Daniel Vetter112abd22012-05-31 14:57:43 +0200543 }
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700544
Chris Wilson97e4eed2013-08-26 16:18:54 +0100545 ret = 0;
David Weinehall694c2822016-08-22 13:32:43 +0300546 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
547 ret = sysfs_create_files(&kdev->kobj, vlv_attrs);
Lucas De Marchi651e7d42021-06-05 21:50:49 -0700548 else if (GRAPHICS_VER(dev_priv) >= 6)
David Weinehall694c2822016-08-22 13:32:43 +0300549 ret = sysfs_create_files(&kdev->kobj, gen6_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100550 if (ret)
Wambui Karuga00376cc2020-01-31 12:34:12 +0300551 drm_err(&dev_priv->drm, "RPS sysfs setup failed\n");
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300552
Chris Wilson98a2f412016-10-12 10:05:18 +0100553 i915_setup_error_capture(kdev);
Chris Wilson4ec76db2020-02-28 13:17:10 +0000554
555 intel_engines_add_sysfs(dev_priv);
Ben Widawsky0136db52012-04-10 21:17:01 -0700556}
557
David Weinehall694c2822016-08-22 13:32:43 +0300558void i915_teardown_sysfs(struct drm_i915_private *dev_priv)
Ben Widawsky0136db52012-04-10 21:17:01 -0700559{
David Weinehall694c2822016-08-22 13:32:43 +0300560 struct device *kdev = dev_priv->drm.primary->kdev;
561
Chris Wilson98a2f412016-10-12 10:05:18 +0100562 i915_teardown_error_capture(kdev);
563
David Weinehall694c2822016-08-22 13:32:43 +0300564 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565 sysfs_remove_files(&kdev->kobj, vlv_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100566 else
David Weinehall694c2822016-08-22 13:32:43 +0300567 sysfs_remove_files(&kdev->kobj, gen6_attrs);
568 device_remove_bin_file(kdev, &dpf_attrs_1);
569 device_remove_bin_file(kdev, &dpf_attrs);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700570#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +0300571 sysfs_unmerge_group(&kdev->kobj, &rc6_attr_group);
572 sysfs_unmerge_group(&kdev->kobj, &rc6p_attr_group);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700573#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700574}