blob: 2d482f677f563e8ec4a54cb9beef3b969f7b2476 [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{
Ben Widawsky0136db52012-04-10 21:17:01 -070045 u64 raw_time; /* 32b value may overflow during fixed point math */
Ville Syrjälä2cc9fab2015-09-28 23:43:43 +030046 u64 units = 128ULL, div = 100000ULL;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020047 u32 ret;
Ben Widawsky0136db52012-04-10 21:17:01 -070048
Chris Wilsondc979972016-05-10 14:10:04 +010049 if (!intel_enable_rc6())
Ben Widawsky0136db52012-04-10 21:17:01 -070050 return 0;
51
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020052 intel_runtime_pm_get(dev_priv);
53
Mika Kuoppala542a6b22014-07-09 14:55:56 +030054 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
David Weinehall694c2822016-08-22 13:32:43 +030055 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä2cc9fab2015-09-28 23:43:43 +030056 units = 1;
57 div = dev_priv->czclk_freq;
Mika Kuoppala542a6b22014-07-09 14:55:56 +030058
Jesse Barnese454a052013-09-26 17:55:58 -070059 if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
60 units <<= 8;
David Weinehall694c2822016-08-22 13:32:43 +030061 } else if (IS_BROXTON(dev_priv)) {
Imre Deakd8135102015-09-29 16:28:46 +030062 units = 1;
63 div = 1200; /* 833.33ns */
Jesse Barnese454a052013-09-26 17:55:58 -070064 }
65
66 raw_time = I915_READ(reg) * units;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020067 ret = DIV_ROUND_UP_ULL(raw_time, div);
68
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020069 intel_runtime_pm_put(dev_priv);
70 return ret;
Ben Widawsky0136db52012-04-10 21:17:01 -070071}
72
73static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070074show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070075{
Chris Wilsondc979972016-05-10 14:10:04 +010076 return snprintf(buf, PAGE_SIZE, "%x\n", intel_enable_rc6());
Ben Widawsky0136db52012-04-10 21:17:01 -070077}
78
79static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070080show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070081{
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, GEN6_GT_GFX_RC6);
Jani Nikula3e2a1552013-02-14 10:42:11 +020084 return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070085}
86
87static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070088show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070089{
David Weinehall694c2822016-08-22 13:32:43 +030090 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
91 u32 rc6p_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6p);
Jani Nikula3e2a1552013-02-14 10:42:11 +020092 return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070093}
94
95static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070096show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070097{
David Weinehall694c2822016-08-22 13:32:43 +030098 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
99 u32 rc6pp_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6pp);
Jani Nikula3e2a1552013-02-14 10:42:11 +0200100 return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -0700101}
102
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530103static ssize_t
104show_media_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
105{
David Weinehall694c2822016-08-22 13:32:43 +0300106 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
107 u32 rc6_residency = calc_residency(dev_priv, VLV_GT_MEDIA_RC6);
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530108 return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
109}
110
Ben Widawsky0136db52012-04-10 21:17:01 -0700111static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL);
112static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL);
113static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL);
114static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL);
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530115static DEVICE_ATTR(media_rc6_residency_ms, S_IRUGO, show_media_rc6_ms, NULL);
Ben Widawsky0136db52012-04-10 21:17:01 -0700116
117static struct attribute *rc6_attrs[] = {
118 &dev_attr_rc6_enable.attr,
119 &dev_attr_rc6_residency_ms.attr,
Ben Widawsky0136db52012-04-10 21:17:01 -0700120 NULL
121};
122
123static struct attribute_group rc6_attr_group = {
124 .name = power_group_name,
125 .attrs = rc6_attrs
126};
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700127
128static struct attribute *rc6p_attrs[] = {
129 &dev_attr_rc6p_residency_ms.attr,
130 &dev_attr_rc6pp_residency_ms.attr,
131 NULL
132};
133
134static struct attribute_group rc6p_attr_group = {
135 .name = power_group_name,
136 .attrs = rc6p_attrs
137};
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530138
139static struct attribute *media_rc6_attrs[] = {
140 &dev_attr_media_rc6_residency_ms.attr,
141 NULL
142};
143
144static struct attribute_group media_rc6_attr_group = {
145 .name = power_group_name,
146 .attrs = media_rc6_attrs
147};
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700148#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700149
David Weinehall694c2822016-08-22 13:32:43 +0300150static int l3_access_valid(struct drm_i915_private *dev_priv, loff_t offset)
Ben Widawsky84bc7582012-05-25 16:56:25 -0700151{
David Weinehall694c2822016-08-22 13:32:43 +0300152 if (!HAS_L3_DPF(dev_priv))
Ben Widawsky84bc7582012-05-25 16:56:25 -0700153 return -EPERM;
154
155 if (offset % 4 != 0)
156 return -EINVAL;
157
158 if (offset >= GEN7_L3LOG_SIZE)
159 return -ENXIO;
160
161 return 0;
162}
163
164static ssize_t
165i915_l3_read(struct file *filp, struct kobject *kobj,
166 struct bin_attribute *attr, char *buf,
167 loff_t offset, size_t count)
168{
David Weinehallc49d13e2016-08-22 13:32:42 +0300169 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300170 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
171 struct drm_device *dev = &dev_priv->drm;
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700172 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700173 int ret;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700174
Ben Widawsky1c3dcd12013-09-12 22:28:28 -0700175 count = round_down(count, 4);
176
David Weinehall694c2822016-08-22 13:32:43 +0300177 ret = l3_access_valid(dev_priv, offset);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700178 if (ret)
179 return ret;
180
Dan Carpentere5ad4022013-09-20 14:20:18 +0300181 count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
Ben Widawsky33618ea2013-09-12 22:28:29 -0700182
David Weinehallc49d13e2016-08-22 13:32:42 +0300183 ret = i915_mutex_lock_interruptible(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700184 if (ret)
185 return ret;
186
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700187 if (dev_priv->l3_parity.remap_info[slice])
188 memcpy(buf,
189 dev_priv->l3_parity.remap_info[slice] + (offset/4),
190 count);
191 else
192 memset(buf, 0, count);
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700193
David Weinehallc49d13e2016-08-22 13:32:42 +0300194 mutex_unlock(&dev->struct_mutex);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700195
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700196 return count;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700197}
198
199static ssize_t
200i915_l3_write(struct file *filp, struct kobject *kobj,
201 struct bin_attribute *attr, char *buf,
202 loff_t offset, size_t count)
203{
David Weinehallc49d13e2016-08-22 13:32:42 +0300204 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300205 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
206 struct drm_device *dev = &dev_priv->drm;
Chris Wilsone2efd132016-05-24 14:53:34 +0100207 struct i915_gem_context *ctx;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700208 u32 *temp = NULL; /* Just here to make handling failures easy */
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700209 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700210 int ret;
211
David Weinehall694c2822016-08-22 13:32:43 +0300212 if (!HAS_HW_CONTEXTS(dev_priv))
Ben Widawsky8245be32013-11-06 13:56:29 -0200213 return -ENXIO;
214
David Weinehall694c2822016-08-22 13:32:43 +0300215 ret = l3_access_valid(dev_priv, offset);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700216 if (ret)
217 return ret;
218
David Weinehallc49d13e2016-08-22 13:32:42 +0300219 ret = i915_mutex_lock_interruptible(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700220 if (ret)
221 return ret;
222
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700223 if (!dev_priv->l3_parity.remap_info[slice]) {
Ben Widawsky84bc7582012-05-25 16:56:25 -0700224 temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
225 if (!temp) {
David Weinehallc49d13e2016-08-22 13:32:42 +0300226 mutex_unlock(&dev->struct_mutex);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700227 return -ENOMEM;
228 }
229 }
230
Ben Widawsky84bc7582012-05-25 16:56:25 -0700231 /* TODO: Ideally we really want a GPU reset here to make sure errors
232 * aren't propagated. Since I cannot find a stable way to reset the GPU
233 * at this point it is left as a TODO.
234 */
235 if (temp)
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700236 dev_priv->l3_parity.remap_info[slice] = temp;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700237
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700238 memcpy(dev_priv->l3_parity.remap_info[slice] + (offset/4), buf, count);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700239
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700240 /* NB: We defer the remapping until we switch to the context */
241 list_for_each_entry(ctx, &dev_priv->context_list, link)
242 ctx->remap_slice |= (1<<slice);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700243
David Weinehallc49d13e2016-08-22 13:32:42 +0300244 mutex_unlock(&dev->struct_mutex);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700245
246 return count;
247}
248
249static struct bin_attribute dpf_attrs = {
250 .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
251 .size = GEN7_L3LOG_SIZE,
252 .read = i915_l3_read,
253 .write = i915_l3_write,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700254 .mmap = NULL,
255 .private = (void *)0
256};
257
258static struct bin_attribute dpf_attrs_1 = {
259 .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
260 .size = GEN7_L3LOG_SIZE,
261 .read = i915_l3_read,
262 .write = i915_l3_write,
263 .mmap = NULL,
264 .private = (void *)1
Ben Widawsky84bc7582012-05-25 16:56:25 -0700265};
266
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200267static ssize_t gt_act_freq_mhz_show(struct device *kdev,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700268 struct device_attribute *attr, char *buf)
269{
David Weinehall694c2822016-08-22 13:32:43 +0300270 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700271 int ret;
272
Imre Deakd46c0512014-04-14 20:24:27 +0300273 intel_runtime_pm_get(dev_priv);
274
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700275 mutex_lock(&dev_priv->rps.hw_lock);
Wayne Boyer666a4532015-12-09 12:29:35 -0800276 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes177006a2013-05-02 10:48:07 -0700277 u32 freq;
Jani Nikula64936252013-05-22 15:36:20 +0300278 freq = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200279 ret = intel_gpu_freq(dev_priv, (freq >> 8) & 0xff);
Jesse Barnes177006a2013-05-02 10:48:07 -0700280 } else {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200281 u32 rpstat = I915_READ(GEN6_RPSTAT1);
Akash Goeled64d662015-03-06 11:07:22 +0530282 if (IS_GEN9(dev_priv))
283 ret = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
284 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200285 ret = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
286 else
287 ret = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200288 ret = intel_gpu_freq(dev_priv, ret);
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200289 }
290 mutex_unlock(&dev_priv->rps.hw_lock);
291
292 intel_runtime_pm_put(dev_priv);
293
294 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
295}
296
297static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
298 struct device_attribute *attr, char *buf)
299{
David Weinehall694c2822016-08-22 13:32:43 +0300300 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200301
Chris Wilson62e1baa2016-07-13 09:10:36 +0100302 return snprintf(buf, PAGE_SIZE, "%d\n",
303 intel_gpu_freq(dev_priv,
304 dev_priv->rps.cur_freq));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700305}
306
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100307static ssize_t gt_boost_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
308{
David Weinehall694c2822016-08-22 13:32:43 +0300309 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100310
311 return snprintf(buf, PAGE_SIZE, "%d\n",
Chris Wilson62e1baa2016-07-13 09:10:36 +0100312 intel_gpu_freq(dev_priv,
313 dev_priv->rps.boost_freq));
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100314}
315
316static ssize_t gt_boost_freq_mhz_store(struct device *kdev,
317 struct device_attribute *attr,
318 const char *buf, size_t count)
319{
David Weinehall694c2822016-08-22 13:32:43 +0300320 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100321 u32 val;
322 ssize_t ret;
323
324 ret = kstrtou32(buf, 0, &val);
325 if (ret)
326 return ret;
327
328 /* Validate against (static) hardware limits */
329 val = intel_freq_opcode(dev_priv, val);
330 if (val < dev_priv->rps.min_freq || val > dev_priv->rps.max_freq)
331 return -EINVAL;
332
333 mutex_lock(&dev_priv->rps.hw_lock);
334 dev_priv->rps.boost_freq = val;
335 mutex_unlock(&dev_priv->rps.hw_lock);
336
337 return count;
338}
339
Chris Wilson97e4eed2013-08-26 16:18:54 +0100340static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
341 struct device_attribute *attr, char *buf)
342{
David Weinehall694c2822016-08-22 13:32:43 +0300343 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100344
Chris Wilson62e1baa2016-07-13 09:10:36 +0100345 return snprintf(buf, PAGE_SIZE, "%d\n",
346 intel_gpu_freq(dev_priv,
347 dev_priv->rps.efficient_freq));
Chris Wilson97e4eed2013-08-26 16:18:54 +0100348}
349
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700350static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
351{
David Weinehall694c2822016-08-22 13:32:43 +0300352 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700353
Chris Wilson62e1baa2016-07-13 09:10:36 +0100354 return snprintf(buf, PAGE_SIZE, "%d\n",
355 intel_gpu_freq(dev_priv,
356 dev_priv->rps.max_freq_softlimit));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700357}
358
Ben Widawsky46ddf192012-09-12 18:12:07 -0700359static ssize_t gt_max_freq_mhz_store(struct device *kdev,
360 struct device_attribute *attr,
361 const char *buf, size_t count)
362{
David Weinehall694c2822016-08-22 13:32:43 +0300363 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700364 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700365 ssize_t ret;
366
367 ret = kstrtou32(buf, 0, &val);
368 if (ret)
369 return ret;
370
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530371 intel_runtime_pm_get(dev_priv);
372
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700373 mutex_lock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700374
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200375 val = intel_freq_opcode(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700376
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700377 if (val < dev_priv->rps.min_freq ||
378 val > dev_priv->rps.max_freq ||
Ben Widawskyb39fb292014-03-19 18:31:11 -0700379 val < dev_priv->rps.min_freq_softlimit) {
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700380 mutex_unlock(&dev_priv->rps.hw_lock);
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530381 intel_runtime_pm_put(dev_priv);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700382 return -EINVAL;
383 }
384
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700385 if (val > dev_priv->rps.rp0_freq)
Ben Widawsky31c77382013-04-05 14:29:22 -0700386 DRM_DEBUG("User requested overclocking to %d\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200387 intel_gpu_freq(dev_priv, val));
Ben Widawsky31c77382013-04-05 14:29:22 -0700388
Ben Widawskyb39fb292014-03-19 18:31:11 -0700389 dev_priv->rps.max_freq_softlimit = val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700390
Ville Syrjäläf745a802015-01-23 21:04:23 +0200391 val = clamp_t(int, dev_priv->rps.cur_freq,
392 dev_priv->rps.min_freq_softlimit,
393 dev_priv->rps.max_freq_softlimit);
394
395 /* We still need *_set_rps to process the new max_delay and
396 * update the interrupt limits and PMINTRMSK even though
397 * frequency request may be unchanged. */
Chris Wilsondc979972016-05-10 14:10:04 +0100398 intel_set_rps(dev_priv, val);
Chris Wilson6917c7b2013-11-06 13:56:26 -0200399
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700400 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700401
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530402 intel_runtime_pm_put(dev_priv);
403
Ben Widawsky46ddf192012-09-12 18:12:07 -0700404 return count;
405}
406
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700407static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
408{
David Weinehall694c2822016-08-22 13:32:43 +0300409 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700410
Chris Wilson62e1baa2016-07-13 09:10:36 +0100411 return snprintf(buf, PAGE_SIZE, "%d\n",
412 intel_gpu_freq(dev_priv,
413 dev_priv->rps.min_freq_softlimit));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700414}
415
Ben Widawsky46ddf192012-09-12 18:12:07 -0700416static ssize_t gt_min_freq_mhz_store(struct device *kdev,
417 struct device_attribute *attr,
418 const char *buf, size_t count)
419{
David Weinehall694c2822016-08-22 13:32:43 +0300420 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700421 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700422 ssize_t ret;
423
424 ret = kstrtou32(buf, 0, &val);
425 if (ret)
426 return ret;
427
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530428 intel_runtime_pm_get(dev_priv);
429
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700430 mutex_lock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700431
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200432 val = intel_freq_opcode(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700433
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700434 if (val < dev_priv->rps.min_freq ||
435 val > dev_priv->rps.max_freq ||
436 val > dev_priv->rps.max_freq_softlimit) {
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700437 mutex_unlock(&dev_priv->rps.hw_lock);
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530438 intel_runtime_pm_put(dev_priv);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700439 return -EINVAL;
440 }
441
Ben Widawskyb39fb292014-03-19 18:31:11 -0700442 dev_priv->rps.min_freq_softlimit = val;
Chris Wilson6917c7b2013-11-06 13:56:26 -0200443
Ville Syrjäläf745a802015-01-23 21:04:23 +0200444 val = clamp_t(int, dev_priv->rps.cur_freq,
445 dev_priv->rps.min_freq_softlimit,
446 dev_priv->rps.max_freq_softlimit);
447
448 /* We still need *_set_rps to process the new min_delay and
449 * update the interrupt limits and PMINTRMSK even though
450 * frequency request may be unchanged. */
Chris Wilsondc979972016-05-10 14:10:04 +0100451 intel_set_rps(dev_priv, val);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700452
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700453 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700454
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530455 intel_runtime_pm_put(dev_priv);
456
Ben Widawsky46ddf192012-09-12 18:12:07 -0700457 return count;
458
459}
460
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200461static DEVICE_ATTR(gt_act_freq_mhz, S_IRUGO, gt_act_freq_mhz_show, NULL);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700462static DEVICE_ATTR(gt_cur_freq_mhz, S_IRUGO, gt_cur_freq_mhz_show, NULL);
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100463static DEVICE_ATTR(gt_boost_freq_mhz, S_IRUGO, gt_boost_freq_mhz_show, gt_boost_freq_mhz_store);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700464static DEVICE_ATTR(gt_max_freq_mhz, S_IRUGO | S_IWUSR, gt_max_freq_mhz_show, gt_max_freq_mhz_store);
465static 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 -0700466
Chris Wilson97e4eed2013-08-26 16:18:54 +0100467static DEVICE_ATTR(vlv_rpe_freq_mhz, S_IRUGO, vlv_rpe_freq_mhz_show, NULL);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700468
469static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
470static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
471static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
472static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
473
474/* For now we have a static number of RP states */
475static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
476{
David Weinehall694c2822016-08-22 13:32:43 +0300477 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530478 u32 val;
Ben Widawskyac6ae342012-09-07 19:43:44 -0700479
Akash Goelbc4d91f2015-02-26 16:09:47 +0530480 if (attr == &dev_attr_gt_RP0_freq_mhz)
481 val = intel_gpu_freq(dev_priv, dev_priv->rps.rp0_freq);
482 else if (attr == &dev_attr_gt_RP1_freq_mhz)
483 val = intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq);
484 else if (attr == &dev_attr_gt_RPn_freq_mhz)
485 val = intel_gpu_freq(dev_priv, dev_priv->rps.min_freq);
486 else
Ben Widawskyac6ae342012-09-07 19:43:44 -0700487 BUG();
Akash Goelbc4d91f2015-02-26 16:09:47 +0530488
Jani Nikula3e2a1552013-02-14 10:42:11 +0200489 return snprintf(buf, PAGE_SIZE, "%d\n", val);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700490}
491
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700492static const struct attribute *gen6_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200493 &dev_attr_gt_act_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700494 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100495 &dev_attr_gt_boost_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700496 &dev_attr_gt_max_freq_mhz.attr,
497 &dev_attr_gt_min_freq_mhz.attr,
Ben Widawskyac6ae342012-09-07 19:43:44 -0700498 &dev_attr_gt_RP0_freq_mhz.attr,
499 &dev_attr_gt_RP1_freq_mhz.attr,
500 &dev_attr_gt_RPn_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700501 NULL,
502};
503
Chris Wilson97e4eed2013-08-26 16:18:54 +0100504static const struct attribute *vlv_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200505 &dev_attr_gt_act_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100506 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100507 &dev_attr_gt_boost_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100508 &dev_attr_gt_max_freq_mhz.attr,
509 &dev_attr_gt_min_freq_mhz.attr,
Deepak S74c4f622014-07-10 13:16:22 +0530510 &dev_attr_gt_RP0_freq_mhz.attr,
511 &dev_attr_gt_RP1_freq_mhz.attr,
512 &dev_attr_gt_RPn_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100513 &dev_attr_vlv_rpe_freq_mhz.attr,
514 NULL,
515};
516
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300517static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
518 struct bin_attribute *attr, char *buf,
519 loff_t off, size_t count)
520{
521
Geliang Tang657fb5f2016-01-13 22:48:40 +0800522 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300523 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
524 struct drm_device *dev = &dev_priv->drm;
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300525 struct i915_error_state_file_priv error_priv;
526 struct drm_i915_error_state_buf error_str;
527 ssize_t ret_count = 0;
528 int ret;
529
530 memset(&error_priv, 0, sizeof(error_priv));
531
Chris Wilson0a4cd7c2014-08-22 14:41:39 +0100532 ret = i915_error_state_buf_init(&error_str, to_i915(dev), count, off);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300533 if (ret)
534 return ret;
535
536 error_priv.dev = dev;
537 i915_error_state_get(dev, &error_priv);
538
539 ret = i915_error_state_to_str(&error_str, &error_priv);
540 if (ret)
541 goto out;
542
543 ret_count = count < error_str.bytes ? count : error_str.bytes;
544
545 memcpy(buf, error_str.buf, ret_count);
546out:
547 i915_error_state_put(&error_priv);
548 i915_error_state_buf_release(&error_str);
549
550 return ret ?: ret_count;
551}
552
553static ssize_t error_state_write(struct file *file, struct kobject *kobj,
554 struct bin_attribute *attr, char *buf,
555 loff_t off, size_t count)
556{
Geliang Tang657fb5f2016-01-13 22:48:40 +0800557 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300558 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
559 struct drm_device *dev = &dev_priv->drm;
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300560 int ret;
561
562 DRM_DEBUG_DRIVER("Resetting error state\n");
563
564 ret = mutex_lock_interruptible(&dev->struct_mutex);
565 if (ret)
566 return ret;
567
568 i915_destroy_error_state(dev);
569 mutex_unlock(&dev->struct_mutex);
570
571 return count;
572}
573
574static struct bin_attribute error_state_attr = {
575 .attr.name = "error",
576 .attr.mode = S_IRUSR | S_IWUSR,
577 .size = 0,
578 .read = error_state_read,
579 .write = error_state_write,
580};
581
David Weinehall694c2822016-08-22 13:32:43 +0300582void i915_setup_sysfs(struct drm_i915_private *dev_priv)
Ben Widawsky0136db52012-04-10 21:17:01 -0700583{
David Weinehall694c2822016-08-22 13:32:43 +0300584 struct device *kdev = dev_priv->drm.primary->kdev;
Ben Widawsky0136db52012-04-10 21:17:01 -0700585 int ret;
586
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700587#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +0300588 if (HAS_RC6(dev_priv)) {
589 ret = sysfs_merge_group(&kdev->kobj,
Daniel Vetter112abd22012-05-31 14:57:43 +0200590 &rc6_attr_group);
591 if (ret)
592 DRM_ERROR("RC6 residency sysfs setup failed\n");
593 }
David Weinehall694c2822016-08-22 13:32:43 +0300594 if (HAS_RC6p(dev_priv)) {
595 ret = sysfs_merge_group(&kdev->kobj,
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700596 &rc6p_attr_group);
597 if (ret)
598 DRM_ERROR("RC6p residency sysfs setup failed\n");
599 }
David Weinehall694c2822016-08-22 13:32:43 +0300600 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
601 ret = sysfs_merge_group(&kdev->kobj,
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530602 &media_rc6_attr_group);
603 if (ret)
604 DRM_ERROR("Media RC6 residency sysfs setup failed\n");
605 }
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700606#endif
David Weinehall694c2822016-08-22 13:32:43 +0300607 if (HAS_L3_DPF(dev_priv)) {
608 ret = device_create_bin_file(kdev, &dpf_attrs);
Daniel Vetter112abd22012-05-31 14:57:43 +0200609 if (ret)
610 DRM_ERROR("l3 parity sysfs setup failed\n");
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700611
David Weinehall694c2822016-08-22 13:32:43 +0300612 if (NUM_L3_SLICES(dev_priv) > 1) {
613 ret = device_create_bin_file(kdev,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700614 &dpf_attrs_1);
615 if (ret)
616 DRM_ERROR("l3 parity slice 1 setup failed\n");
617 }
Daniel Vetter112abd22012-05-31 14:57:43 +0200618 }
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700619
Chris Wilson97e4eed2013-08-26 16:18:54 +0100620 ret = 0;
David Weinehall694c2822016-08-22 13:32:43 +0300621 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
622 ret = sysfs_create_files(&kdev->kobj, vlv_attrs);
623 else if (INTEL_GEN(dev_priv) >= 6)
624 ret = sysfs_create_files(&kdev->kobj, gen6_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100625 if (ret)
626 DRM_ERROR("RPS sysfs setup failed\n");
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300627
David Weinehall694c2822016-08-22 13:32:43 +0300628 ret = sysfs_create_bin_file(&kdev->kobj,
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300629 &error_state_attr);
630 if (ret)
631 DRM_ERROR("error_state sysfs setup failed\n");
Ben Widawsky0136db52012-04-10 21:17:01 -0700632}
633
David Weinehall694c2822016-08-22 13:32:43 +0300634void i915_teardown_sysfs(struct drm_i915_private *dev_priv)
Ben Widawsky0136db52012-04-10 21:17:01 -0700635{
David Weinehall694c2822016-08-22 13:32:43 +0300636 struct device *kdev = dev_priv->drm.primary->kdev;
637
638 sysfs_remove_bin_file(&kdev->kobj, &error_state_attr);
639 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
640 sysfs_remove_files(&kdev->kobj, vlv_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100641 else
David Weinehall694c2822016-08-22 13:32:43 +0300642 sysfs_remove_files(&kdev->kobj, gen6_attrs);
643 device_remove_bin_file(kdev, &dpf_attrs_1);
644 device_remove_bin_file(kdev, &dpf_attrs);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700645#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +0300646 sysfs_unmerge_group(&kdev->kobj, &rc6_attr_group);
647 sysfs_unmerge_group(&kdev->kobj, &rc6p_attr_group);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700648#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700649}