blob: 1eef3fae4db31312f788372d72470ad2e9b38f5c [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>
Ben Widawsky84bc7582012-05-25 16:56:25 -070032#include "intel_drv.h"
Ben Widawsky0136db52012-04-10 21:17:01 -070033#include "i915_drv.h"
34
David Weinehall694c2822016-08-22 13:32:43 +030035static inline struct drm_i915_private *kdev_minor_to_i915(struct device *kdev)
David Weinehallc49d13e2016-08-22 13:32:42 +030036{
David Weinehall694c2822016-08-22 13:32:43 +030037 struct drm_minor *minor = dev_get_drvdata(kdev);
38 return to_i915(minor->dev);
David Weinehallc49d13e2016-08-22 13:32:42 +030039}
Dave Airlie14c8d1102013-10-11 14:45:30 +100040
Hunt Xu5ab36332012-07-01 03:45:07 +000041#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +030042static u32 calc_residency(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020043 i915_reg_t reg)
Ben Widawsky0136db52012-04-10 21:17:01 -070044{
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +020045 return DIV_ROUND_CLOSEST_ULL(intel_rc6_residency_us(dev_priv, reg),
46 1000);
Ben Widawsky0136db52012-04-10 21:17:01 -070047}
48
49static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070050show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070051{
Chris Wilsondc979972016-05-10 14:10:04 +010052 return snprintf(buf, PAGE_SIZE, "%x\n", intel_enable_rc6());
Ben Widawsky0136db52012-04-10 21:17:01 -070053}
54
55static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070056show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070057{
David Weinehall694c2822016-08-22 13:32:43 +030058 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
59 u32 rc6_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6);
Jani Nikula3e2a1552013-02-14 10:42:11 +020060 return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070061}
62
63static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070064show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070065{
David Weinehall694c2822016-08-22 13:32:43 +030066 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
67 u32 rc6p_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6p);
Jani Nikula3e2a1552013-02-14 10:42:11 +020068 return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070069}
70
71static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070072show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070073{
David Weinehall694c2822016-08-22 13:32:43 +030074 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
75 u32 rc6pp_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6pp);
Jani Nikula3e2a1552013-02-14 10:42:11 +020076 return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070077}
78
Ville Syrjälä626ad6f2015-02-26 21:10:27 +053079static ssize_t
80show_media_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
81{
David Weinehall694c2822016-08-22 13:32:43 +030082 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
83 u32 rc6_residency = calc_residency(dev_priv, VLV_GT_MEDIA_RC6);
Ville Syrjälä626ad6f2015-02-26 21:10:27 +053084 return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
85}
86
Ben Widawsky0136db52012-04-10 21:17:01 -070087static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL);
88static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL);
89static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL);
90static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL);
Ville Syrjälä626ad6f2015-02-26 21:10:27 +053091static DEVICE_ATTR(media_rc6_residency_ms, S_IRUGO, show_media_rc6_ms, NULL);
Ben Widawsky0136db52012-04-10 21:17:01 -070092
93static struct attribute *rc6_attrs[] = {
94 &dev_attr_rc6_enable.attr,
95 &dev_attr_rc6_residency_ms.attr,
Ben Widawsky0136db52012-04-10 21:17:01 -070096 NULL
97};
98
99static struct attribute_group rc6_attr_group = {
100 .name = power_group_name,
101 .attrs = rc6_attrs
102};
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700103
104static struct attribute *rc6p_attrs[] = {
105 &dev_attr_rc6p_residency_ms.attr,
106 &dev_attr_rc6pp_residency_ms.attr,
107 NULL
108};
109
110static struct attribute_group rc6p_attr_group = {
111 .name = power_group_name,
112 .attrs = rc6p_attrs
113};
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530114
115static struct attribute *media_rc6_attrs[] = {
116 &dev_attr_media_rc6_residency_ms.attr,
117 NULL
118};
119
120static struct attribute_group media_rc6_attr_group = {
121 .name = power_group_name,
122 .attrs = media_rc6_attrs
123};
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700124#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700125
David Weinehall694c2822016-08-22 13:32:43 +0300126static int l3_access_valid(struct drm_i915_private *dev_priv, loff_t offset)
Ben Widawsky84bc7582012-05-25 16:56:25 -0700127{
David Weinehall694c2822016-08-22 13:32:43 +0300128 if (!HAS_L3_DPF(dev_priv))
Ben Widawsky84bc7582012-05-25 16:56:25 -0700129 return -EPERM;
130
131 if (offset % 4 != 0)
132 return -EINVAL;
133
134 if (offset >= GEN7_L3LOG_SIZE)
135 return -ENXIO;
136
137 return 0;
138}
139
140static ssize_t
141i915_l3_read(struct file *filp, struct kobject *kobj,
142 struct bin_attribute *attr, char *buf,
143 loff_t offset, size_t count)
144{
David Weinehallc49d13e2016-08-22 13:32:42 +0300145 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300146 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
147 struct drm_device *dev = &dev_priv->drm;
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700148 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700149 int ret;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700150
Ben Widawsky1c3dcd12013-09-12 22:28:28 -0700151 count = round_down(count, 4);
152
David Weinehall694c2822016-08-22 13:32:43 +0300153 ret = l3_access_valid(dev_priv, offset);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700154 if (ret)
155 return ret;
156
Dan Carpentere5ad4022013-09-20 14:20:18 +0300157 count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
Ben Widawsky33618ea2013-09-12 22:28:29 -0700158
David Weinehallc49d13e2016-08-22 13:32:42 +0300159 ret = i915_mutex_lock_interruptible(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700160 if (ret)
161 return ret;
162
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700163 if (dev_priv->l3_parity.remap_info[slice])
164 memcpy(buf,
165 dev_priv->l3_parity.remap_info[slice] + (offset/4),
166 count);
167 else
168 memset(buf, 0, count);
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700169
David Weinehallc49d13e2016-08-22 13:32:42 +0300170 mutex_unlock(&dev->struct_mutex);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700171
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700172 return count;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700173}
174
175static ssize_t
176i915_l3_write(struct file *filp, struct kobject *kobj,
177 struct bin_attribute *attr, char *buf,
178 loff_t offset, size_t count)
179{
David Weinehallc49d13e2016-08-22 13:32:42 +0300180 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300181 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
182 struct drm_device *dev = &dev_priv->drm;
Chris Wilsone2efd132016-05-24 14:53:34 +0100183 struct i915_gem_context *ctx;
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700184 int slice = (int)(uintptr_t)attr->private;
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300185 u32 **remap_info;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700186 int ret;
187
David Weinehall694c2822016-08-22 13:32:43 +0300188 ret = l3_access_valid(dev_priv, offset);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700189 if (ret)
190 return ret;
191
David Weinehallc49d13e2016-08-22 13:32:42 +0300192 ret = i915_mutex_lock_interruptible(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700193 if (ret)
194 return ret;
195
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300196 remap_info = &dev_priv->l3_parity.remap_info[slice];
197 if (!*remap_info) {
198 *remap_info = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
199 if (!*remap_info) {
200 ret = -ENOMEM;
201 goto out;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700202 }
203 }
204
Ben Widawsky84bc7582012-05-25 16:56:25 -0700205 /* TODO: Ideally we really want a GPU reset here to make sure errors
206 * aren't propagated. Since I cannot find a stable way to reset the GPU
207 * at this point it is left as a TODO.
208 */
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300209 memcpy(*remap_info + (offset/4), buf, count);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700210
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700211 /* NB: We defer the remapping until we switch to the context */
212 list_for_each_entry(ctx, &dev_priv->context_list, link)
213 ctx->remap_slice |= (1<<slice);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700214
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300215 ret = count;
216
217out:
David Weinehallc49d13e2016-08-22 13:32:42 +0300218 mutex_unlock(&dev->struct_mutex);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700219
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300220 return ret;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700221}
222
223static struct bin_attribute dpf_attrs = {
224 .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
225 .size = GEN7_L3LOG_SIZE,
226 .read = i915_l3_read,
227 .write = i915_l3_write,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700228 .mmap = NULL,
229 .private = (void *)0
230};
231
232static struct bin_attribute dpf_attrs_1 = {
233 .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
234 .size = GEN7_L3LOG_SIZE,
235 .read = i915_l3_read,
236 .write = i915_l3_write,
237 .mmap = NULL,
238 .private = (void *)1
Ben Widawsky84bc7582012-05-25 16:56:25 -0700239};
240
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200241static ssize_t gt_act_freq_mhz_show(struct device *kdev,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700242 struct device_attribute *attr, char *buf)
243{
David Weinehall694c2822016-08-22 13:32:43 +0300244 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700245 int ret;
246
Imre Deakd46c0512014-04-14 20:24:27 +0300247 intel_runtime_pm_get(dev_priv);
248
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700249 mutex_lock(&dev_priv->rps.hw_lock);
Wayne Boyer666a4532015-12-09 12:29:35 -0800250 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes177006a2013-05-02 10:48:07 -0700251 u32 freq;
Jani Nikula64936252013-05-22 15:36:20 +0300252 freq = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200253 ret = intel_gpu_freq(dev_priv, (freq >> 8) & 0xff);
Jesse Barnes177006a2013-05-02 10:48:07 -0700254 } else {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200255 u32 rpstat = I915_READ(GEN6_RPSTAT1);
Akash Goeled64d662015-03-06 11:07:22 +0530256 if (IS_GEN9(dev_priv))
257 ret = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
258 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200259 ret = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
260 else
261 ret = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200262 ret = intel_gpu_freq(dev_priv, ret);
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200263 }
264 mutex_unlock(&dev_priv->rps.hw_lock);
265
266 intel_runtime_pm_put(dev_priv);
267
268 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
269}
270
271static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
272 struct device_attribute *attr, char *buf)
273{
David Weinehall694c2822016-08-22 13:32:43 +0300274 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200275
Chris Wilson62e1baa2016-07-13 09:10:36 +0100276 return snprintf(buf, PAGE_SIZE, "%d\n",
277 intel_gpu_freq(dev_priv,
278 dev_priv->rps.cur_freq));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700279}
280
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100281static ssize_t gt_boost_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
282{
David Weinehall694c2822016-08-22 13:32:43 +0300283 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100284
285 return snprintf(buf, PAGE_SIZE, "%d\n",
Chris Wilson62e1baa2016-07-13 09:10:36 +0100286 intel_gpu_freq(dev_priv,
287 dev_priv->rps.boost_freq));
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100288}
289
290static ssize_t gt_boost_freq_mhz_store(struct device *kdev,
291 struct device_attribute *attr,
292 const char *buf, size_t count)
293{
David Weinehall694c2822016-08-22 13:32:43 +0300294 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100295 u32 val;
296 ssize_t ret;
297
298 ret = kstrtou32(buf, 0, &val);
299 if (ret)
300 return ret;
301
302 /* Validate against (static) hardware limits */
303 val = intel_freq_opcode(dev_priv, val);
304 if (val < dev_priv->rps.min_freq || val > dev_priv->rps.max_freq)
305 return -EINVAL;
306
307 mutex_lock(&dev_priv->rps.hw_lock);
308 dev_priv->rps.boost_freq = val;
309 mutex_unlock(&dev_priv->rps.hw_lock);
310
311 return count;
312}
313
Chris Wilson97e4eed2013-08-26 16:18:54 +0100314static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
315 struct device_attribute *attr, char *buf)
316{
David Weinehall694c2822016-08-22 13:32:43 +0300317 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100318
Chris Wilson62e1baa2016-07-13 09:10:36 +0100319 return snprintf(buf, PAGE_SIZE, "%d\n",
320 intel_gpu_freq(dev_priv,
321 dev_priv->rps.efficient_freq));
Chris Wilson97e4eed2013-08-26 16:18:54 +0100322}
323
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700324static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
325{
David Weinehall694c2822016-08-22 13:32:43 +0300326 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700327
Chris Wilson62e1baa2016-07-13 09:10:36 +0100328 return snprintf(buf, PAGE_SIZE, "%d\n",
329 intel_gpu_freq(dev_priv,
330 dev_priv->rps.max_freq_softlimit));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700331}
332
Ben Widawsky46ddf192012-09-12 18:12:07 -0700333static ssize_t gt_max_freq_mhz_store(struct device *kdev,
334 struct device_attribute *attr,
335 const char *buf, size_t count)
336{
David Weinehall694c2822016-08-22 13:32:43 +0300337 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700338 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700339 ssize_t ret;
340
341 ret = kstrtou32(buf, 0, &val);
342 if (ret)
343 return ret;
344
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530345 intel_runtime_pm_get(dev_priv);
346
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700347 mutex_lock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700348
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200349 val = intel_freq_opcode(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700350
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700351 if (val < dev_priv->rps.min_freq ||
352 val > dev_priv->rps.max_freq ||
Ben Widawskyb39fb292014-03-19 18:31:11 -0700353 val < dev_priv->rps.min_freq_softlimit) {
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700354 mutex_unlock(&dev_priv->rps.hw_lock);
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530355 intel_runtime_pm_put(dev_priv);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700356 return -EINVAL;
357 }
358
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700359 if (val > dev_priv->rps.rp0_freq)
Ben Widawsky31c77382013-04-05 14:29:22 -0700360 DRM_DEBUG("User requested overclocking to %d\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200361 intel_gpu_freq(dev_priv, val));
Ben Widawsky31c77382013-04-05 14:29:22 -0700362
Ben Widawskyb39fb292014-03-19 18:31:11 -0700363 dev_priv->rps.max_freq_softlimit = val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700364
Ville Syrjäläf745a802015-01-23 21:04:23 +0200365 val = clamp_t(int, dev_priv->rps.cur_freq,
366 dev_priv->rps.min_freq_softlimit,
367 dev_priv->rps.max_freq_softlimit);
368
369 /* We still need *_set_rps to process the new max_delay and
370 * update the interrupt limits and PMINTRMSK even though
371 * frequency request may be unchanged. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +0000372 ret = intel_set_rps(dev_priv, val);
Chris Wilson6917c7b2013-11-06 13:56:26 -0200373
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700374 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700375
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530376 intel_runtime_pm_put(dev_priv);
377
Chris Wilson9fcee2f2017-01-26 10:19:19 +0000378 return ret ?: count;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700379}
380
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700381static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
382{
David Weinehall694c2822016-08-22 13:32:43 +0300383 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700384
Chris Wilson62e1baa2016-07-13 09:10:36 +0100385 return snprintf(buf, PAGE_SIZE, "%d\n",
386 intel_gpu_freq(dev_priv,
387 dev_priv->rps.min_freq_softlimit));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700388}
389
Ben Widawsky46ddf192012-09-12 18:12:07 -0700390static ssize_t gt_min_freq_mhz_store(struct device *kdev,
391 struct device_attribute *attr,
392 const char *buf, size_t count)
393{
David Weinehall694c2822016-08-22 13:32:43 +0300394 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700395 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700396 ssize_t ret;
397
398 ret = kstrtou32(buf, 0, &val);
399 if (ret)
400 return ret;
401
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530402 intel_runtime_pm_get(dev_priv);
403
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700404 mutex_lock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700405
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200406 val = intel_freq_opcode(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700407
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700408 if (val < dev_priv->rps.min_freq ||
409 val > dev_priv->rps.max_freq ||
410 val > dev_priv->rps.max_freq_softlimit) {
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700411 mutex_unlock(&dev_priv->rps.hw_lock);
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530412 intel_runtime_pm_put(dev_priv);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700413 return -EINVAL;
414 }
415
Ben Widawskyb39fb292014-03-19 18:31:11 -0700416 dev_priv->rps.min_freq_softlimit = val;
Chris Wilson6917c7b2013-11-06 13:56:26 -0200417
Ville Syrjäläf745a802015-01-23 21:04:23 +0200418 val = clamp_t(int, dev_priv->rps.cur_freq,
419 dev_priv->rps.min_freq_softlimit,
420 dev_priv->rps.max_freq_softlimit);
421
422 /* We still need *_set_rps to process the new min_delay and
423 * update the interrupt limits and PMINTRMSK even though
424 * frequency request may be unchanged. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +0000425 ret = intel_set_rps(dev_priv, val);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700426
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700427 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700428
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530429 intel_runtime_pm_put(dev_priv);
430
Chris Wilson9fcee2f2017-01-26 10:19:19 +0000431 return ret ?: count;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700432}
433
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200434static DEVICE_ATTR(gt_act_freq_mhz, S_IRUGO, gt_act_freq_mhz_show, NULL);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700435static DEVICE_ATTR(gt_cur_freq_mhz, S_IRUGO, gt_cur_freq_mhz_show, NULL);
Mika Kuoppala73a79872016-12-14 14:26:20 +0200436static DEVICE_ATTR(gt_boost_freq_mhz, S_IRUGO | S_IWUSR, gt_boost_freq_mhz_show, gt_boost_freq_mhz_store);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700437static DEVICE_ATTR(gt_max_freq_mhz, S_IRUGO | S_IWUSR, gt_max_freq_mhz_show, gt_max_freq_mhz_store);
438static DEVICE_ATTR(gt_min_freq_mhz, S_IRUGO | S_IWUSR, gt_min_freq_mhz_show, gt_min_freq_mhz_store);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700439
Chris Wilson97e4eed2013-08-26 16:18:54 +0100440static DEVICE_ATTR(vlv_rpe_freq_mhz, S_IRUGO, vlv_rpe_freq_mhz_show, NULL);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700441
442static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
443static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
444static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
445static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
446
447/* For now we have a static number of RP states */
448static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
449{
David Weinehall694c2822016-08-22 13:32:43 +0300450 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530451 u32 val;
Ben Widawskyac6ae342012-09-07 19:43:44 -0700452
Akash Goelbc4d91f2015-02-26 16:09:47 +0530453 if (attr == &dev_attr_gt_RP0_freq_mhz)
454 val = intel_gpu_freq(dev_priv, dev_priv->rps.rp0_freq);
455 else if (attr == &dev_attr_gt_RP1_freq_mhz)
456 val = intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq);
457 else if (attr == &dev_attr_gt_RPn_freq_mhz)
458 val = intel_gpu_freq(dev_priv, dev_priv->rps.min_freq);
459 else
Ben Widawskyac6ae342012-09-07 19:43:44 -0700460 BUG();
Akash Goelbc4d91f2015-02-26 16:09:47 +0530461
Jani Nikula3e2a1552013-02-14 10:42:11 +0200462 return snprintf(buf, PAGE_SIZE, "%d\n", val);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700463}
464
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700465static const struct attribute *gen6_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200466 &dev_attr_gt_act_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700467 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100468 &dev_attr_gt_boost_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700469 &dev_attr_gt_max_freq_mhz.attr,
470 &dev_attr_gt_min_freq_mhz.attr,
Ben Widawskyac6ae342012-09-07 19:43:44 -0700471 &dev_attr_gt_RP0_freq_mhz.attr,
472 &dev_attr_gt_RP1_freq_mhz.attr,
473 &dev_attr_gt_RPn_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700474 NULL,
475};
476
Chris Wilson97e4eed2013-08-26 16:18:54 +0100477static const struct attribute *vlv_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200478 &dev_attr_gt_act_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100479 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100480 &dev_attr_gt_boost_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100481 &dev_attr_gt_max_freq_mhz.attr,
482 &dev_attr_gt_min_freq_mhz.attr,
Deepak S74c4f622014-07-10 13:16:22 +0530483 &dev_attr_gt_RP0_freq_mhz.attr,
484 &dev_attr_gt_RP1_freq_mhz.attr,
485 &dev_attr_gt_RPn_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100486 &dev_attr_vlv_rpe_freq_mhz.attr,
487 NULL,
488};
489
Chris Wilson98a2f412016-10-12 10:05:18 +0100490#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
491
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300492static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
493 struct bin_attribute *attr, char *buf,
494 loff_t off, size_t count)
495{
496
Geliang Tang657fb5f2016-01-13 22:48:40 +0800497 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300498 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300499 struct drm_i915_error_state_buf error_str;
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000500 struct i915_gpu_state *gpu;
501 ssize_t ret;
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300502
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000503 ret = i915_error_state_buf_init(&error_str, dev_priv, count, off);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300504 if (ret)
505 return ret;
506
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000507 gpu = i915_first_error_state(dev_priv);
508 ret = i915_error_state_to_str(&error_str, gpu);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300509 if (ret)
510 goto out;
511
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000512 ret = count < error_str.bytes ? count : error_str.bytes;
513 memcpy(buf, error_str.buf, ret);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300514
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300515out:
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000516 i915_gpu_state_put(gpu);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300517 i915_error_state_buf_release(&error_str);
518
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000519 return ret;
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300520}
521
522static ssize_t error_state_write(struct file *file, struct kobject *kobj,
523 struct bin_attribute *attr, char *buf,
524 loff_t off, size_t count)
525{
Geliang Tang657fb5f2016-01-13 22:48:40 +0800526 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300527 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300528
529 DRM_DEBUG_DRIVER("Resetting error state\n");
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000530 i915_reset_error_state(dev_priv);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300531
532 return count;
533}
534
535static struct bin_attribute error_state_attr = {
536 .attr.name = "error",
537 .attr.mode = S_IRUSR | S_IWUSR,
538 .size = 0,
539 .read = error_state_read,
540 .write = error_state_write,
541};
542
Chris Wilson98a2f412016-10-12 10:05:18 +0100543static void i915_setup_error_capture(struct device *kdev)
544{
545 if (sysfs_create_bin_file(&kdev->kobj, &error_state_attr))
546 DRM_ERROR("error_state sysfs setup failed\n");
547}
548
549static void i915_teardown_error_capture(struct device *kdev)
550{
551 sysfs_remove_bin_file(&kdev->kobj, &error_state_attr);
552}
553#else
554static void i915_setup_error_capture(struct device *kdev) {}
555static void i915_teardown_error_capture(struct device *kdev) {}
556#endif
557
David Weinehall694c2822016-08-22 13:32:43 +0300558void i915_setup_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;
Ben Widawsky0136db52012-04-10 21:17:01 -0700561 int ret;
562
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700563#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +0300564 if (HAS_RC6(dev_priv)) {
565 ret = sysfs_merge_group(&kdev->kobj,
Daniel Vetter112abd22012-05-31 14:57:43 +0200566 &rc6_attr_group);
567 if (ret)
568 DRM_ERROR("RC6 residency sysfs setup failed\n");
569 }
David Weinehall694c2822016-08-22 13:32:43 +0300570 if (HAS_RC6p(dev_priv)) {
571 ret = sysfs_merge_group(&kdev->kobj,
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700572 &rc6p_attr_group);
573 if (ret)
574 DRM_ERROR("RC6p residency sysfs setup failed\n");
575 }
David Weinehall694c2822016-08-22 13:32:43 +0300576 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
577 ret = sysfs_merge_group(&kdev->kobj,
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530578 &media_rc6_attr_group);
579 if (ret)
580 DRM_ERROR("Media RC6 residency sysfs setup failed\n");
581 }
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700582#endif
David Weinehall694c2822016-08-22 13:32:43 +0300583 if (HAS_L3_DPF(dev_priv)) {
584 ret = device_create_bin_file(kdev, &dpf_attrs);
Daniel Vetter112abd22012-05-31 14:57:43 +0200585 if (ret)
586 DRM_ERROR("l3 parity sysfs setup failed\n");
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700587
David Weinehall694c2822016-08-22 13:32:43 +0300588 if (NUM_L3_SLICES(dev_priv) > 1) {
589 ret = device_create_bin_file(kdev,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700590 &dpf_attrs_1);
591 if (ret)
592 DRM_ERROR("l3 parity slice 1 setup failed\n");
593 }
Daniel Vetter112abd22012-05-31 14:57:43 +0200594 }
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700595
Chris Wilson97e4eed2013-08-26 16:18:54 +0100596 ret = 0;
David Weinehall694c2822016-08-22 13:32:43 +0300597 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
598 ret = sysfs_create_files(&kdev->kobj, vlv_attrs);
599 else if (INTEL_GEN(dev_priv) >= 6)
600 ret = sysfs_create_files(&kdev->kobj, gen6_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100601 if (ret)
602 DRM_ERROR("RPS sysfs setup failed\n");
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300603
Chris Wilson98a2f412016-10-12 10:05:18 +0100604 i915_setup_error_capture(kdev);
Ben Widawsky0136db52012-04-10 21:17:01 -0700605}
606
David Weinehall694c2822016-08-22 13:32:43 +0300607void i915_teardown_sysfs(struct drm_i915_private *dev_priv)
Ben Widawsky0136db52012-04-10 21:17:01 -0700608{
David Weinehall694c2822016-08-22 13:32:43 +0300609 struct device *kdev = dev_priv->drm.primary->kdev;
610
Chris Wilson98a2f412016-10-12 10:05:18 +0100611 i915_teardown_error_capture(kdev);
612
David Weinehall694c2822016-08-22 13:32:43 +0300613 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
614 sysfs_remove_files(&kdev->kobj, vlv_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100615 else
David Weinehall694c2822016-08-22 13:32:43 +0300616 sysfs_remove_files(&kdev->kobj, gen6_attrs);
617 device_remove_bin_file(kdev, &dpf_attrs_1);
618 device_remove_bin_file(kdev, &dpf_attrs);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700619#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +0300620 sysfs_unmerge_group(&kdev->kobj, &rc6_attr_group);
621 sysfs_unmerge_group(&kdev->kobj, &rc6p_attr_group);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700622#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700623}