blob: d47281b4b1c1179ccfcd49384d17c1b620376b49 [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
Dave Airlie5bdebb12013-10-11 14:07:25 +100035#define dev_to_drm_minor(d) dev_get_drvdata((d))
Dave Airlie14c8d1102013-10-11 14:45:30 +100036
Hunt Xu5ab36332012-07-01 03:45:07 +000037#ifdef CONFIG_PM
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020038static u32 calc_residency(struct drm_device *dev,
39 i915_reg_t reg)
Ben Widawsky0136db52012-04-10 21:17:01 -070040{
Chris Wilsonfac5e232016-07-04 11:34:36 +010041 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky0136db52012-04-10 21:17:01 -070042 u64 raw_time; /* 32b value may overflow during fixed point math */
Ville Syrjälä2cc9fab2015-09-28 23:43:43 +030043 u64 units = 128ULL, div = 100000ULL;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020044 u32 ret;
Ben Widawsky0136db52012-04-10 21:17:01 -070045
Chris Wilsondc979972016-05-10 14:10:04 +010046 if (!intel_enable_rc6())
Ben Widawsky0136db52012-04-10 21:17:01 -070047 return 0;
48
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020049 intel_runtime_pm_get(dev_priv);
50
Mika Kuoppala542a6b22014-07-09 14:55:56 +030051 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
Wayne Boyer666a4532015-12-09 12:29:35 -080052 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä2cc9fab2015-09-28 23:43:43 +030053 units = 1;
54 div = dev_priv->czclk_freq;
Mika Kuoppala542a6b22014-07-09 14:55:56 +030055
Jesse Barnese454a052013-09-26 17:55:58 -070056 if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
57 units <<= 8;
Imre Deakd8135102015-09-29 16:28:46 +030058 } else if (IS_BROXTON(dev)) {
59 units = 1;
60 div = 1200; /* 833.33ns */
Jesse Barnese454a052013-09-26 17:55:58 -070061 }
62
63 raw_time = I915_READ(reg) * units;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020064 ret = DIV_ROUND_UP_ULL(raw_time, div);
65
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020066 intel_runtime_pm_put(dev_priv);
67 return ret;
Ben Widawsky0136db52012-04-10 21:17:01 -070068}
69
70static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070071show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070072{
Chris Wilsondc979972016-05-10 14:10:04 +010073 return snprintf(buf, PAGE_SIZE, "%x\n", intel_enable_rc6());
Ben Widawsky0136db52012-04-10 21:17:01 -070074}
75
76static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070077show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070078{
Dave Airlie5bdebb12013-10-11 14:07:25 +100079 struct drm_minor *dminor = dev_get_drvdata(kdev);
Ben Widawsky0136db52012-04-10 21:17:01 -070080 u32 rc6_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6);
Jani Nikula3e2a1552013-02-14 10:42:11 +020081 return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070082}
83
84static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070085show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070086{
Dave Airlie14c8d1102013-10-11 14:45:30 +100087 struct drm_minor *dminor = dev_to_drm_minor(kdev);
Ben Widawsky0136db52012-04-10 21:17:01 -070088 u32 rc6p_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6p);
Jani Nikula3e2a1552013-02-14 10:42:11 +020089 return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070090}
91
92static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070093show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070094{
Dave Airlie14c8d1102013-10-11 14:45:30 +100095 struct drm_minor *dminor = dev_to_drm_minor(kdev);
Ben Widawsky0136db52012-04-10 21:17:01 -070096 u32 rc6pp_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6pp);
Jani Nikula3e2a1552013-02-14 10:42:11 +020097 return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070098}
99
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530100static ssize_t
101show_media_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
102{
103 struct drm_minor *dminor = dev_get_drvdata(kdev);
104 u32 rc6_residency = calc_residency(dminor->dev, VLV_GT_MEDIA_RC6);
105 return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
106}
107
Ben Widawsky0136db52012-04-10 21:17:01 -0700108static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL);
109static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL);
110static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL);
111static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL);
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530112static DEVICE_ATTR(media_rc6_residency_ms, S_IRUGO, show_media_rc6_ms, NULL);
Ben Widawsky0136db52012-04-10 21:17:01 -0700113
114static struct attribute *rc6_attrs[] = {
115 &dev_attr_rc6_enable.attr,
116 &dev_attr_rc6_residency_ms.attr,
Ben Widawsky0136db52012-04-10 21:17:01 -0700117 NULL
118};
119
120static struct attribute_group rc6_attr_group = {
121 .name = power_group_name,
122 .attrs = rc6_attrs
123};
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700124
125static struct attribute *rc6p_attrs[] = {
126 &dev_attr_rc6p_residency_ms.attr,
127 &dev_attr_rc6pp_residency_ms.attr,
128 NULL
129};
130
131static struct attribute_group rc6p_attr_group = {
132 .name = power_group_name,
133 .attrs = rc6p_attrs
134};
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530135
136static struct attribute *media_rc6_attrs[] = {
137 &dev_attr_media_rc6_residency_ms.attr,
138 NULL
139};
140
141static struct attribute_group media_rc6_attr_group = {
142 .name = power_group_name,
143 .attrs = media_rc6_attrs
144};
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700145#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700146
Ben Widawsky84bc7582012-05-25 16:56:25 -0700147static int l3_access_valid(struct drm_device *dev, loff_t offset)
148{
Ben Widawsky040d2ba2013-09-19 11:01:40 -0700149 if (!HAS_L3_DPF(dev))
Ben Widawsky84bc7582012-05-25 16:56:25 -0700150 return -EPERM;
151
152 if (offset % 4 != 0)
153 return -EINVAL;
154
155 if (offset >= GEN7_L3LOG_SIZE)
156 return -ENXIO;
157
158 return 0;
159}
160
161static ssize_t
162i915_l3_read(struct file *filp, struct kobject *kobj,
163 struct bin_attribute *attr, char *buf,
164 loff_t offset, size_t count)
165{
Geliang Tang657fb5f2016-01-13 22:48:40 +0800166 struct device *dev = kobj_to_dev(kobj);
Dave Airlie14c8d1102013-10-11 14:45:30 +1000167 struct drm_minor *dminor = dev_to_drm_minor(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700168 struct drm_device *drm_dev = dminor->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100169 struct drm_i915_private *dev_priv = to_i915(drm_dev);
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700170 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700171 int ret;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700172
Ben Widawsky1c3dcd12013-09-12 22:28:28 -0700173 count = round_down(count, 4);
174
Ben Widawsky84bc7582012-05-25 16:56:25 -0700175 ret = l3_access_valid(drm_dev, offset);
176 if (ret)
177 return ret;
178
Dan Carpentere5ad4022013-09-20 14:20:18 +0300179 count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
Ben Widawsky33618ea2013-09-12 22:28:29 -0700180
Ben Widawsky84bc7582012-05-25 16:56:25 -0700181 ret = i915_mutex_lock_interruptible(drm_dev);
182 if (ret)
183 return ret;
184
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700185 if (dev_priv->l3_parity.remap_info[slice])
186 memcpy(buf,
187 dev_priv->l3_parity.remap_info[slice] + (offset/4),
188 count);
189 else
190 memset(buf, 0, count);
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700191
Ben Widawsky84bc7582012-05-25 16:56:25 -0700192 mutex_unlock(&drm_dev->struct_mutex);
193
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700194 return count;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700195}
196
197static ssize_t
198i915_l3_write(struct file *filp, struct kobject *kobj,
199 struct bin_attribute *attr, char *buf,
200 loff_t offset, size_t count)
201{
Geliang Tang657fb5f2016-01-13 22:48:40 +0800202 struct device *dev = kobj_to_dev(kobj);
Dave Airlie14c8d1102013-10-11 14:45:30 +1000203 struct drm_minor *dminor = dev_to_drm_minor(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700204 struct drm_device *drm_dev = dminor->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100205 struct drm_i915_private *dev_priv = to_i915(drm_dev);
Chris Wilsone2efd132016-05-24 14:53:34 +0100206 struct i915_gem_context *ctx;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700207 u32 *temp = NULL; /* Just here to make handling failures easy */
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700208 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700209 int ret;
210
Ben Widawsky8245be32013-11-06 13:56:29 -0200211 if (!HAS_HW_CONTEXTS(drm_dev))
212 return -ENXIO;
213
Ben Widawsky84bc7582012-05-25 16:56:25 -0700214 ret = l3_access_valid(drm_dev, offset);
215 if (ret)
216 return ret;
217
218 ret = i915_mutex_lock_interruptible(drm_dev);
219 if (ret)
220 return ret;
221
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700222 if (!dev_priv->l3_parity.remap_info[slice]) {
Ben Widawsky84bc7582012-05-25 16:56:25 -0700223 temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
224 if (!temp) {
225 mutex_unlock(&drm_dev->struct_mutex);
226 return -ENOMEM;
227 }
228 }
229
Ben Widawsky84bc7582012-05-25 16:56:25 -0700230 /* TODO: Ideally we really want a GPU reset here to make sure errors
231 * aren't propagated. Since I cannot find a stable way to reset the GPU
232 * at this point it is left as a TODO.
233 */
234 if (temp)
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700235 dev_priv->l3_parity.remap_info[slice] = temp;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700236
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700237 memcpy(dev_priv->l3_parity.remap_info[slice] + (offset/4), buf, count);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700238
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700239 /* NB: We defer the remapping until we switch to the context */
240 list_for_each_entry(ctx, &dev_priv->context_list, link)
241 ctx->remap_slice |= (1<<slice);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700242
243 mutex_unlock(&drm_dev->struct_mutex);
244
245 return count;
246}
247
248static struct bin_attribute dpf_attrs = {
249 .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
250 .size = GEN7_L3LOG_SIZE,
251 .read = i915_l3_read,
252 .write = i915_l3_write,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700253 .mmap = NULL,
254 .private = (void *)0
255};
256
257static struct bin_attribute dpf_attrs_1 = {
258 .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
259 .size = GEN7_L3LOG_SIZE,
260 .read = i915_l3_read,
261 .write = i915_l3_write,
262 .mmap = NULL,
263 .private = (void *)1
Ben Widawsky84bc7582012-05-25 16:56:25 -0700264};
265
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200266static ssize_t gt_act_freq_mhz_show(struct device *kdev,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700267 struct device_attribute *attr, char *buf)
268{
Dave Airlie14c8d1102013-10-11 14:45:30 +1000269 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700270 struct drm_device *dev = minor->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100271 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700272 int ret;
273
Imre Deakd46c0512014-04-14 20:24:27 +0300274 intel_runtime_pm_get(dev_priv);
275
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700276 mutex_lock(&dev_priv->rps.hw_lock);
Wayne Boyer666a4532015-12-09 12:29:35 -0800277 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes177006a2013-05-02 10:48:07 -0700278 u32 freq;
Jani Nikula64936252013-05-22 15:36:20 +0300279 freq = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200280 ret = intel_gpu_freq(dev_priv, (freq >> 8) & 0xff);
Jesse Barnes177006a2013-05-02 10:48:07 -0700281 } else {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200282 u32 rpstat = I915_READ(GEN6_RPSTAT1);
Akash Goeled64d662015-03-06 11:07:22 +0530283 if (IS_GEN9(dev_priv))
284 ret = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
285 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200286 ret = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
287 else
288 ret = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200289 ret = intel_gpu_freq(dev_priv, ret);
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200290 }
291 mutex_unlock(&dev_priv->rps.hw_lock);
292
293 intel_runtime_pm_put(dev_priv);
294
295 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
296}
297
298static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
299 struct device_attribute *attr, char *buf)
300{
301 struct drm_minor *minor = dev_to_drm_minor(kdev);
302 struct drm_device *dev = minor->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100303 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200304
Chris Wilson62e1baa2016-07-13 09:10:36 +0100305 return snprintf(buf, PAGE_SIZE, "%d\n",
306 intel_gpu_freq(dev_priv,
307 dev_priv->rps.cur_freq));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700308}
309
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100310static ssize_t gt_boost_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
311{
312 struct drm_minor *minor = dev_to_drm_minor(kdev);
313 struct drm_i915_private *dev_priv = to_i915(minor->dev);
314
315 return snprintf(buf, PAGE_SIZE, "%d\n",
Chris Wilson62e1baa2016-07-13 09:10:36 +0100316 intel_gpu_freq(dev_priv,
317 dev_priv->rps.boost_freq));
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100318}
319
320static ssize_t gt_boost_freq_mhz_store(struct device *kdev,
321 struct device_attribute *attr,
322 const char *buf, size_t count)
323{
324 struct drm_minor *minor = dev_to_drm_minor(kdev);
325 struct drm_device *dev = minor->dev;
326 struct drm_i915_private *dev_priv = dev->dev_private;
327 u32 val;
328 ssize_t ret;
329
330 ret = kstrtou32(buf, 0, &val);
331 if (ret)
332 return ret;
333
334 /* Validate against (static) hardware limits */
335 val = intel_freq_opcode(dev_priv, val);
336 if (val < dev_priv->rps.min_freq || val > dev_priv->rps.max_freq)
337 return -EINVAL;
338
339 mutex_lock(&dev_priv->rps.hw_lock);
340 dev_priv->rps.boost_freq = val;
341 mutex_unlock(&dev_priv->rps.hw_lock);
342
343 return count;
344}
345
Chris Wilson97e4eed2013-08-26 16:18:54 +0100346static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
347 struct device_attribute *attr, char *buf)
348{
Dave Airlie14c8d1102013-10-11 14:45:30 +1000349 struct drm_minor *minor = dev_to_drm_minor(kdev);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100350 struct drm_device *dev = minor->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100351 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100352
Chris Wilson62e1baa2016-07-13 09:10:36 +0100353 return snprintf(buf, PAGE_SIZE, "%d\n",
354 intel_gpu_freq(dev_priv,
355 dev_priv->rps.efficient_freq));
Chris Wilson97e4eed2013-08-26 16:18:54 +0100356}
357
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700358static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
359{
Dave Airlie14c8d1102013-10-11 14:45:30 +1000360 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700361 struct drm_device *dev = minor->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100362 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700363
Chris Wilson62e1baa2016-07-13 09:10:36 +0100364 return snprintf(buf, PAGE_SIZE, "%d\n",
365 intel_gpu_freq(dev_priv,
366 dev_priv->rps.max_freq_softlimit));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700367}
368
Ben Widawsky46ddf192012-09-12 18:12:07 -0700369static ssize_t gt_max_freq_mhz_store(struct device *kdev,
370 struct device_attribute *attr,
371 const char *buf, size_t count)
372{
Dave Airlie14c8d1102013-10-11 14:45:30 +1000373 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700374 struct drm_device *dev = minor->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100375 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700376 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700377 ssize_t ret;
378
379 ret = kstrtou32(buf, 0, &val);
380 if (ret)
381 return ret;
382
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530383 intel_runtime_pm_get(dev_priv);
384
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700385 mutex_lock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700386
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200387 val = intel_freq_opcode(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700388
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700389 if (val < dev_priv->rps.min_freq ||
390 val > dev_priv->rps.max_freq ||
Ben Widawskyb39fb292014-03-19 18:31:11 -0700391 val < dev_priv->rps.min_freq_softlimit) {
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700392 mutex_unlock(&dev_priv->rps.hw_lock);
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530393 intel_runtime_pm_put(dev_priv);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700394 return -EINVAL;
395 }
396
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700397 if (val > dev_priv->rps.rp0_freq)
Ben Widawsky31c77382013-04-05 14:29:22 -0700398 DRM_DEBUG("User requested overclocking to %d\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200399 intel_gpu_freq(dev_priv, val));
Ben Widawsky31c77382013-04-05 14:29:22 -0700400
Ben Widawskyb39fb292014-03-19 18:31:11 -0700401 dev_priv->rps.max_freq_softlimit = val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700402
Ville Syrjäläf745a802015-01-23 21:04:23 +0200403 val = clamp_t(int, dev_priv->rps.cur_freq,
404 dev_priv->rps.min_freq_softlimit,
405 dev_priv->rps.max_freq_softlimit);
406
407 /* We still need *_set_rps to process the new max_delay and
408 * update the interrupt limits and PMINTRMSK even though
409 * frequency request may be unchanged. */
Chris Wilsondc979972016-05-10 14:10:04 +0100410 intel_set_rps(dev_priv, val);
Chris Wilson6917c7b2013-11-06 13:56:26 -0200411
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700412 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700413
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530414 intel_runtime_pm_put(dev_priv);
415
Ben Widawsky46ddf192012-09-12 18:12:07 -0700416 return count;
417}
418
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700419static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
420{
Dave Airlie14c8d1102013-10-11 14:45:30 +1000421 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700422 struct drm_device *dev = minor->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100423 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700424
Chris Wilson62e1baa2016-07-13 09:10:36 +0100425 return snprintf(buf, PAGE_SIZE, "%d\n",
426 intel_gpu_freq(dev_priv,
427 dev_priv->rps.min_freq_softlimit));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700428}
429
Ben Widawsky46ddf192012-09-12 18:12:07 -0700430static ssize_t gt_min_freq_mhz_store(struct device *kdev,
431 struct device_attribute *attr,
432 const char *buf, size_t count)
433{
Dave Airlie14c8d1102013-10-11 14:45:30 +1000434 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700435 struct drm_device *dev = minor->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100436 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700437 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700438 ssize_t ret;
439
440 ret = kstrtou32(buf, 0, &val);
441 if (ret)
442 return ret;
443
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530444 intel_runtime_pm_get(dev_priv);
445
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700446 mutex_lock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700447
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200448 val = intel_freq_opcode(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700449
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700450 if (val < dev_priv->rps.min_freq ||
451 val > dev_priv->rps.max_freq ||
452 val > dev_priv->rps.max_freq_softlimit) {
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700453 mutex_unlock(&dev_priv->rps.hw_lock);
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530454 intel_runtime_pm_put(dev_priv);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700455 return -EINVAL;
456 }
457
Ben Widawskyb39fb292014-03-19 18:31:11 -0700458 dev_priv->rps.min_freq_softlimit = val;
Chris Wilson6917c7b2013-11-06 13:56:26 -0200459
Ville Syrjäläf745a802015-01-23 21:04:23 +0200460 val = clamp_t(int, dev_priv->rps.cur_freq,
461 dev_priv->rps.min_freq_softlimit,
462 dev_priv->rps.max_freq_softlimit);
463
464 /* We still need *_set_rps to process the new min_delay and
465 * update the interrupt limits and PMINTRMSK even though
466 * frequency request may be unchanged. */
Chris Wilsondc979972016-05-10 14:10:04 +0100467 intel_set_rps(dev_priv, val);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700468
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700469 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700470
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530471 intel_runtime_pm_put(dev_priv);
472
Ben Widawsky46ddf192012-09-12 18:12:07 -0700473 return count;
474
475}
476
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200477static DEVICE_ATTR(gt_act_freq_mhz, S_IRUGO, gt_act_freq_mhz_show, NULL);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700478static DEVICE_ATTR(gt_cur_freq_mhz, S_IRUGO, gt_cur_freq_mhz_show, NULL);
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100479static 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 -0700480static DEVICE_ATTR(gt_max_freq_mhz, S_IRUGO | S_IWUSR, gt_max_freq_mhz_show, gt_max_freq_mhz_store);
481static 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 -0700482
Chris Wilson97e4eed2013-08-26 16:18:54 +0100483static DEVICE_ATTR(vlv_rpe_freq_mhz, S_IRUGO, vlv_rpe_freq_mhz_show, NULL);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700484
485static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
486static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
487static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
488static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
489
490/* For now we have a static number of RP states */
491static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
492{
Dave Airlie14c8d1102013-10-11 14:45:30 +1000493 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700494 struct drm_device *dev = minor->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100495 struct drm_i915_private *dev_priv = to_i915(dev);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530496 u32 val;
Ben Widawskyac6ae342012-09-07 19:43:44 -0700497
Akash Goelbc4d91f2015-02-26 16:09:47 +0530498 if (attr == &dev_attr_gt_RP0_freq_mhz)
499 val = intel_gpu_freq(dev_priv, dev_priv->rps.rp0_freq);
500 else if (attr == &dev_attr_gt_RP1_freq_mhz)
501 val = intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq);
502 else if (attr == &dev_attr_gt_RPn_freq_mhz)
503 val = intel_gpu_freq(dev_priv, dev_priv->rps.min_freq);
504 else
Ben Widawskyac6ae342012-09-07 19:43:44 -0700505 BUG();
Akash Goelbc4d91f2015-02-26 16:09:47 +0530506
Jani Nikula3e2a1552013-02-14 10:42:11 +0200507 return snprintf(buf, PAGE_SIZE, "%d\n", val);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700508}
509
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700510static const struct attribute *gen6_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200511 &dev_attr_gt_act_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700512 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100513 &dev_attr_gt_boost_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700514 &dev_attr_gt_max_freq_mhz.attr,
515 &dev_attr_gt_min_freq_mhz.attr,
Ben Widawskyac6ae342012-09-07 19:43:44 -0700516 &dev_attr_gt_RP0_freq_mhz.attr,
517 &dev_attr_gt_RP1_freq_mhz.attr,
518 &dev_attr_gt_RPn_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700519 NULL,
520};
521
Chris Wilson97e4eed2013-08-26 16:18:54 +0100522static const struct attribute *vlv_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200523 &dev_attr_gt_act_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100524 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100525 &dev_attr_gt_boost_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100526 &dev_attr_gt_max_freq_mhz.attr,
527 &dev_attr_gt_min_freq_mhz.attr,
Deepak S74c4f622014-07-10 13:16:22 +0530528 &dev_attr_gt_RP0_freq_mhz.attr,
529 &dev_attr_gt_RP1_freq_mhz.attr,
530 &dev_attr_gt_RPn_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100531 &dev_attr_vlv_rpe_freq_mhz.attr,
532 NULL,
533};
534
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300535static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
536 struct bin_attribute *attr, char *buf,
537 loff_t off, size_t count)
538{
539
Geliang Tang657fb5f2016-01-13 22:48:40 +0800540 struct device *kdev = kobj_to_dev(kobj);
Dave Airlie14c8d1102013-10-11 14:45:30 +1000541 struct drm_minor *minor = dev_to_drm_minor(kdev);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300542 struct drm_device *dev = minor->dev;
543 struct i915_error_state_file_priv error_priv;
544 struct drm_i915_error_state_buf error_str;
545 ssize_t ret_count = 0;
546 int ret;
547
548 memset(&error_priv, 0, sizeof(error_priv));
549
Chris Wilson0a4cd7c2014-08-22 14:41:39 +0100550 ret = i915_error_state_buf_init(&error_str, to_i915(dev), count, off);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300551 if (ret)
552 return ret;
553
554 error_priv.dev = dev;
555 i915_error_state_get(dev, &error_priv);
556
557 ret = i915_error_state_to_str(&error_str, &error_priv);
558 if (ret)
559 goto out;
560
561 ret_count = count < error_str.bytes ? count : error_str.bytes;
562
563 memcpy(buf, error_str.buf, ret_count);
564out:
565 i915_error_state_put(&error_priv);
566 i915_error_state_buf_release(&error_str);
567
568 return ret ?: ret_count;
569}
570
571static ssize_t error_state_write(struct file *file, struct kobject *kobj,
572 struct bin_attribute *attr, char *buf,
573 loff_t off, size_t count)
574{
Geliang Tang657fb5f2016-01-13 22:48:40 +0800575 struct device *kdev = kobj_to_dev(kobj);
Dave Airlie14c8d1102013-10-11 14:45:30 +1000576 struct drm_minor *minor = dev_to_drm_minor(kdev);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300577 struct drm_device *dev = minor->dev;
578 int ret;
579
580 DRM_DEBUG_DRIVER("Resetting error state\n");
581
582 ret = mutex_lock_interruptible(&dev->struct_mutex);
583 if (ret)
584 return ret;
585
586 i915_destroy_error_state(dev);
587 mutex_unlock(&dev->struct_mutex);
588
589 return count;
590}
591
592static struct bin_attribute error_state_attr = {
593 .attr.name = "error",
594 .attr.mode = S_IRUSR | S_IWUSR,
595 .size = 0,
596 .read = error_state_read,
597 .write = error_state_write,
598};
599
Ben Widawsky0136db52012-04-10 21:17:01 -0700600void i915_setup_sysfs(struct drm_device *dev)
601{
602 int ret;
603
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700604#ifdef CONFIG_PM
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700605 if (HAS_RC6(dev)) {
Dave Airlie5bdebb12013-10-11 14:07:25 +1000606 ret = sysfs_merge_group(&dev->primary->kdev->kobj,
Daniel Vetter112abd22012-05-31 14:57:43 +0200607 &rc6_attr_group);
608 if (ret)
609 DRM_ERROR("RC6 residency sysfs setup failed\n");
610 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700611 if (HAS_RC6p(dev)) {
612 ret = sysfs_merge_group(&dev->primary->kdev->kobj,
613 &rc6p_attr_group);
614 if (ret)
615 DRM_ERROR("RC6p residency sysfs setup failed\n");
616 }
Wayne Boyer666a4532015-12-09 12:29:35 -0800617 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530618 ret = sysfs_merge_group(&dev->primary->kdev->kobj,
619 &media_rc6_attr_group);
620 if (ret)
621 DRM_ERROR("Media RC6 residency sysfs setup failed\n");
622 }
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700623#endif
Ben Widawsky040d2ba2013-09-19 11:01:40 -0700624 if (HAS_L3_DPF(dev)) {
Dave Airlie5bdebb12013-10-11 14:07:25 +1000625 ret = device_create_bin_file(dev->primary->kdev, &dpf_attrs);
Daniel Vetter112abd22012-05-31 14:57:43 +0200626 if (ret)
627 DRM_ERROR("l3 parity sysfs setup failed\n");
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700628
629 if (NUM_L3_SLICES(dev) > 1) {
Dave Airlie5bdebb12013-10-11 14:07:25 +1000630 ret = device_create_bin_file(dev->primary->kdev,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700631 &dpf_attrs_1);
632 if (ret)
633 DRM_ERROR("l3 parity slice 1 setup failed\n");
634 }
Daniel Vetter112abd22012-05-31 14:57:43 +0200635 }
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700636
Chris Wilson97e4eed2013-08-26 16:18:54 +0100637 ret = 0;
Wayne Boyer666a4532015-12-09 12:29:35 -0800638 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Dave Airlie5bdebb12013-10-11 14:07:25 +1000639 ret = sysfs_create_files(&dev->primary->kdev->kobj, vlv_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100640 else if (INTEL_INFO(dev)->gen >= 6)
Dave Airlie5bdebb12013-10-11 14:07:25 +1000641 ret = sysfs_create_files(&dev->primary->kdev->kobj, gen6_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100642 if (ret)
643 DRM_ERROR("RPS sysfs setup failed\n");
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300644
Dave Airlie5bdebb12013-10-11 14:07:25 +1000645 ret = sysfs_create_bin_file(&dev->primary->kdev->kobj,
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300646 &error_state_attr);
647 if (ret)
648 DRM_ERROR("error_state sysfs setup failed\n");
Ben Widawsky0136db52012-04-10 21:17:01 -0700649}
650
651void i915_teardown_sysfs(struct drm_device *dev)
652{
Dave Airlie5bdebb12013-10-11 14:07:25 +1000653 sysfs_remove_bin_file(&dev->primary->kdev->kobj, &error_state_attr);
Wayne Boyer666a4532015-12-09 12:29:35 -0800654 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Dave Airlie5bdebb12013-10-11 14:07:25 +1000655 sysfs_remove_files(&dev->primary->kdev->kobj, vlv_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100656 else
Dave Airlie5bdebb12013-10-11 14:07:25 +1000657 sysfs_remove_files(&dev->primary->kdev->kobj, gen6_attrs);
658 device_remove_bin_file(dev->primary->kdev, &dpf_attrs_1);
659 device_remove_bin_file(dev->primary->kdev, &dpf_attrs);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700660#ifdef CONFIG_PM
Dave Airlie5bdebb12013-10-11 14:07:25 +1000661 sysfs_unmerge_group(&dev->primary->kdev->kobj, &rc6_attr_group);
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700662 sysfs_unmerge_group(&dev->primary->kdev->kobj, &rc6p_attr_group);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700663#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700664}