blob: 247626885f49d22cfb94f6f89b1a459c4b0500a8 [file] [log] [blame]
Ben Widawsky0136db582012-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 Widawsky0136db582012-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 Airlie14c8d112013-10-11 14:45:30 +100036
Hunt Xu5ab36332012-07-01 03:45:07 +000037#ifdef CONFIG_PM
Ben Widawsky0136db582012-04-10 21:17:01 -070038static u32 calc_residency(struct drm_device *dev, const u32 reg)
39{
40 struct drm_i915_private *dev_priv = dev->dev_private;
41 u64 raw_time; /* 32b value may overflow during fixed point math */
Jesse Barnese454a052013-09-26 17:55:58 -070042 u64 units = 128ULL, div = 100000ULL, bias = 100ULL;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020043 u32 ret;
Ben Widawsky0136db582012-04-10 21:17:01 -070044
45 if (!intel_enable_rc6(dev))
46 return 0;
47
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020048 intel_runtime_pm_get(dev_priv);
49
Mika Kuoppala542a6b22014-07-09 14:55:56 +030050 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
Jesse Barnese454a052013-09-26 17:55:58 -070051 if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläf78ae63f2015-01-19 13:50:52 +020052 u32 clk_reg, czcount_30ns;
Jesse Barnese454a052013-09-26 17:55:58 -070053
Mika Kuoppala542a6b22014-07-09 14:55:56 +030054 if (IS_CHERRYVIEW(dev))
Ville Syrjäläf78ae63f2015-01-19 13:50:52 +020055 clk_reg = CHV_CLK_CTL1;
Mika Kuoppala542a6b22014-07-09 14:55:56 +030056 else
Ville Syrjäläf78ae63f2015-01-19 13:50:52 +020057 clk_reg = VLV_CLK_CTL2;
Mika Kuoppala542a6b22014-07-09 14:55:56 +030058
Ville Syrjäläf78ae63f2015-01-19 13:50:52 +020059 czcount_30ns = I915_READ(clk_reg) >> CLK_CTL2_CZCOUNT_30NS_SHIFT;
Mika Kuoppala542a6b22014-07-09 14:55:56 +030060
61 if (!czcount_30ns) {
62 WARN(!czcount_30ns, "bogus CZ count value");
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020063 ret = 0;
64 goto out;
Jesse Barnese454a052013-09-26 17:55:58 -070065 }
Mika Kuoppala542a6b22014-07-09 14:55:56 +030066
67 units = 0;
68 div = 1000000ULL;
69
70 if (IS_CHERRYVIEW(dev)) {
71 /* Special case for 320Mhz */
72 if (czcount_30ns == 1) {
73 div = 10000000ULL;
74 units = 3125ULL;
75 } else {
76 /* chv counts are one less */
77 czcount_30ns += 1;
78 }
79 }
80
81 if (units == 0)
82 units = DIV_ROUND_UP_ULL(30ULL * bias,
83 (u64)czcount_30ns);
84
Jesse Barnese454a052013-09-26 17:55:58 -070085 if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
86 units <<= 8;
87
Mika Kuoppala542a6b22014-07-09 14:55:56 +030088 div = div * bias;
Jesse Barnese454a052013-09-26 17:55:58 -070089 }
90
91 raw_time = I915_READ(reg) * units;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020092 ret = DIV_ROUND_UP_ULL(raw_time, div);
93
94out:
95 intel_runtime_pm_put(dev_priv);
96 return ret;
Ben Widawsky0136db582012-04-10 21:17:01 -070097}
98
99static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -0700100show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -0700101{
Dave Airlie14c8d112013-10-11 14:45:30 +1000102 struct drm_minor *dminor = dev_to_drm_minor(kdev);
Jani Nikula3e2a1552013-02-14 10:42:11 +0200103 return snprintf(buf, PAGE_SIZE, "%x\n", intel_enable_rc6(dminor->dev));
Ben Widawsky0136db582012-04-10 21:17:01 -0700104}
105
106static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -0700107show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -0700108{
Dave Airlie5bdebb12013-10-11 14:07:25 +1000109 struct drm_minor *dminor = dev_get_drvdata(kdev);
Ben Widawsky0136db582012-04-10 21:17:01 -0700110 u32 rc6_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6);
Jani Nikula3e2a1552013-02-14 10:42:11 +0200111 return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
Ben Widawsky0136db582012-04-10 21:17:01 -0700112}
113
114static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -0700115show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -0700116{
Dave Airlie14c8d112013-10-11 14:45:30 +1000117 struct drm_minor *dminor = dev_to_drm_minor(kdev);
Ben Widawsky0136db582012-04-10 21:17:01 -0700118 u32 rc6p_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6p);
Jani Nikula3e2a1552013-02-14 10:42:11 +0200119 return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency);
Ben Widawsky0136db582012-04-10 21:17:01 -0700120}
121
122static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -0700123show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -0700124{
Dave Airlie14c8d112013-10-11 14:45:30 +1000125 struct drm_minor *dminor = dev_to_drm_minor(kdev);
Ben Widawsky0136db582012-04-10 21:17:01 -0700126 u32 rc6pp_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6pp);
Jani Nikula3e2a1552013-02-14 10:42:11 +0200127 return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency);
Ben Widawsky0136db582012-04-10 21:17:01 -0700128}
129
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530130static ssize_t
131show_media_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
132{
133 struct drm_minor *dminor = dev_get_drvdata(kdev);
134 u32 rc6_residency = calc_residency(dminor->dev, VLV_GT_MEDIA_RC6);
135 return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
136}
137
Ben Widawsky0136db582012-04-10 21:17:01 -0700138static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL);
139static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL);
140static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL);
141static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL);
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530142static DEVICE_ATTR(media_rc6_residency_ms, S_IRUGO, show_media_rc6_ms, NULL);
Ben Widawsky0136db582012-04-10 21:17:01 -0700143
144static struct attribute *rc6_attrs[] = {
145 &dev_attr_rc6_enable.attr,
146 &dev_attr_rc6_residency_ms.attr,
Ben Widawsky0136db582012-04-10 21:17:01 -0700147 NULL
148};
149
150static struct attribute_group rc6_attr_group = {
151 .name = power_group_name,
152 .attrs = rc6_attrs
153};
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700154
155static struct attribute *rc6p_attrs[] = {
156 &dev_attr_rc6p_residency_ms.attr,
157 &dev_attr_rc6pp_residency_ms.attr,
158 NULL
159};
160
161static struct attribute_group rc6p_attr_group = {
162 .name = power_group_name,
163 .attrs = rc6p_attrs
164};
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530165
166static struct attribute *media_rc6_attrs[] = {
167 &dev_attr_media_rc6_residency_ms.attr,
168 NULL
169};
170
171static struct attribute_group media_rc6_attr_group = {
172 .name = power_group_name,
173 .attrs = media_rc6_attrs
174};
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700175#endif
Ben Widawsky0136db582012-04-10 21:17:01 -0700176
Ben Widawsky84bc7582012-05-25 16:56:25 -0700177static int l3_access_valid(struct drm_device *dev, loff_t offset)
178{
Ben Widawsky040d2ba2013-09-19 11:01:40 -0700179 if (!HAS_L3_DPF(dev))
Ben Widawsky84bc7582012-05-25 16:56:25 -0700180 return -EPERM;
181
182 if (offset % 4 != 0)
183 return -EINVAL;
184
185 if (offset >= GEN7_L3LOG_SIZE)
186 return -ENXIO;
187
188 return 0;
189}
190
191static ssize_t
192i915_l3_read(struct file *filp, struct kobject *kobj,
193 struct bin_attribute *attr, char *buf,
194 loff_t offset, size_t count)
195{
196 struct device *dev = container_of(kobj, struct device, kobj);
Dave Airlie14c8d112013-10-11 14:45:30 +1000197 struct drm_minor *dminor = dev_to_drm_minor(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700198 struct drm_device *drm_dev = dminor->dev;
199 struct drm_i915_private *dev_priv = drm_dev->dev_private;
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700200 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700201 int ret;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700202
Ben Widawsky1c3dcd12013-09-12 22:28:28 -0700203 count = round_down(count, 4);
204
Ben Widawsky84bc7582012-05-25 16:56:25 -0700205 ret = l3_access_valid(drm_dev, offset);
206 if (ret)
207 return ret;
208
Dan Carpentere5ad4022013-09-20 14:20:18 +0300209 count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
Ben Widawsky33618ea2013-09-12 22:28:29 -0700210
Ben Widawsky84bc7582012-05-25 16:56:25 -0700211 ret = i915_mutex_lock_interruptible(drm_dev);
212 if (ret)
213 return ret;
214
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700215 if (dev_priv->l3_parity.remap_info[slice])
216 memcpy(buf,
217 dev_priv->l3_parity.remap_info[slice] + (offset/4),
218 count);
219 else
220 memset(buf, 0, count);
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700221
Ben Widawsky84bc7582012-05-25 16:56:25 -0700222 mutex_unlock(&drm_dev->struct_mutex);
223
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700224 return count;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700225}
226
227static ssize_t
228i915_l3_write(struct file *filp, struct kobject *kobj,
229 struct bin_attribute *attr, char *buf,
230 loff_t offset, size_t count)
231{
232 struct device *dev = container_of(kobj, struct device, kobj);
Dave Airlie14c8d112013-10-11 14:45:30 +1000233 struct drm_minor *dminor = dev_to_drm_minor(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700234 struct drm_device *drm_dev = dminor->dev;
235 struct drm_i915_private *dev_priv = drm_dev->dev_private;
Oscar Mateo273497e2014-05-22 14:13:37 +0100236 struct intel_context *ctx;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700237 u32 *temp = NULL; /* Just here to make handling failures easy */
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700238 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700239 int ret;
240
Ben Widawsky8245be32013-11-06 13:56:29 -0200241 if (!HAS_HW_CONTEXTS(drm_dev))
242 return -ENXIO;
243
Ben Widawsky84bc7582012-05-25 16:56:25 -0700244 ret = l3_access_valid(drm_dev, offset);
245 if (ret)
246 return ret;
247
248 ret = i915_mutex_lock_interruptible(drm_dev);
249 if (ret)
250 return ret;
251
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700252 if (!dev_priv->l3_parity.remap_info[slice]) {
Ben Widawsky84bc7582012-05-25 16:56:25 -0700253 temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
254 if (!temp) {
255 mutex_unlock(&drm_dev->struct_mutex);
256 return -ENOMEM;
257 }
258 }
259
260 ret = i915_gpu_idle(drm_dev);
261 if (ret) {
262 kfree(temp);
263 mutex_unlock(&drm_dev->struct_mutex);
264 return ret;
265 }
266
267 /* TODO: Ideally we really want a GPU reset here to make sure errors
268 * aren't propagated. Since I cannot find a stable way to reset the GPU
269 * at this point it is left as a TODO.
270 */
271 if (temp)
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700272 dev_priv->l3_parity.remap_info[slice] = temp;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700273
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700274 memcpy(dev_priv->l3_parity.remap_info[slice] + (offset/4), buf, count);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700275
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700276 /* NB: We defer the remapping until we switch to the context */
277 list_for_each_entry(ctx, &dev_priv->context_list, link)
278 ctx->remap_slice |= (1<<slice);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700279
280 mutex_unlock(&drm_dev->struct_mutex);
281
282 return count;
283}
284
285static struct bin_attribute dpf_attrs = {
286 .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
287 .size = GEN7_L3LOG_SIZE,
288 .read = i915_l3_read,
289 .write = i915_l3_write,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700290 .mmap = NULL,
291 .private = (void *)0
292};
293
294static struct bin_attribute dpf_attrs_1 = {
295 .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
296 .size = GEN7_L3LOG_SIZE,
297 .read = i915_l3_read,
298 .write = i915_l3_write,
299 .mmap = NULL,
300 .private = (void *)1
Ben Widawsky84bc7582012-05-25 16:56:25 -0700301};
302
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200303static ssize_t gt_act_freq_mhz_show(struct device *kdev,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700304 struct device_attribute *attr, char *buf)
305{
Dave Airlie14c8d112013-10-11 14:45:30 +1000306 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700307 struct drm_device *dev = minor->dev;
308 struct drm_i915_private *dev_priv = dev->dev_private;
309 int ret;
310
Tom O'Rourke5c9669c2013-09-16 14:56:43 -0700311 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
312
Imre Deakd46c0512014-04-14 20:24:27 +0300313 intel_runtime_pm_get(dev_priv);
314
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700315 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes177006a2013-05-02 10:48:07 -0700316 if (IS_VALLEYVIEW(dev_priv->dev)) {
317 u32 freq;
Jani Nikula64936252013-05-22 15:36:20 +0300318 freq = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200319 ret = intel_gpu_freq(dev_priv, (freq >> 8) & 0xff);
Jesse Barnes177006a2013-05-02 10:48:07 -0700320 } else {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200321 u32 rpstat = I915_READ(GEN6_RPSTAT1);
Akash Goeled64d662015-03-06 11:07:22 +0530322 if (IS_GEN9(dev_priv))
323 ret = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
324 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200325 ret = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
326 else
327 ret = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200328 ret = intel_gpu_freq(dev_priv, ret);
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200329 }
330 mutex_unlock(&dev_priv->rps.hw_lock);
331
332 intel_runtime_pm_put(dev_priv);
333
334 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
335}
336
337static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
338 struct device_attribute *attr, char *buf)
339{
340 struct drm_minor *minor = dev_to_drm_minor(kdev);
341 struct drm_device *dev = minor->dev;
342 struct drm_i915_private *dev_priv = dev->dev_private;
343 int ret;
344
345 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
346
347 intel_runtime_pm_get(dev_priv);
348
349 mutex_lock(&dev_priv->rps.hw_lock);
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200350 ret = intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq);
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700351 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700352
Imre Deakd46c0512014-04-14 20:24:27 +0300353 intel_runtime_pm_put(dev_priv);
354
Jani Nikula3e2a1552013-02-14 10:42:11 +0200355 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700356}
357
Chris Wilson97e4eed2013-08-26 16:18:54 +0100358static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
359 struct device_attribute *attr, char *buf)
360{
Dave Airlie14c8d112013-10-11 14:45:30 +1000361 struct drm_minor *minor = dev_to_drm_minor(kdev);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100362 struct drm_device *dev = minor->dev;
363 struct drm_i915_private *dev_priv = dev->dev_private;
364
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200365 return snprintf(buf, PAGE_SIZE,
366 "%d\n",
367 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq));
Chris Wilson97e4eed2013-08-26 16:18:54 +0100368}
369
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700370static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
371{
Dave Airlie14c8d112013-10-11 14:45:30 +1000372 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700373 struct drm_device *dev = minor->dev;
374 struct drm_i915_private *dev_priv = dev->dev_private;
375 int ret;
376
Tom O'Rourke5c9669c2013-09-16 14:56:43 -0700377 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
378
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700379 mutex_lock(&dev_priv->rps.hw_lock);
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200380 ret = intel_gpu_freq(dev_priv, dev_priv->rps.max_freq_softlimit);
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700381 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700382
Jani Nikula3e2a1552013-02-14 10:42:11 +0200383 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700384}
385
Ben Widawsky46ddf192012-09-12 18:12:07 -0700386static ssize_t gt_max_freq_mhz_store(struct device *kdev,
387 struct device_attribute *attr,
388 const char *buf, size_t count)
389{
Dave Airlie14c8d112013-10-11 14:45:30 +1000390 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700391 struct drm_device *dev = minor->dev;
392 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700393 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700394 ssize_t ret;
395
396 ret = kstrtou32(buf, 0, &val);
397 if (ret)
398 return ret;
399
Tom O'Rourke5c9669c2013-09-16 14:56:43 -0700400 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
401
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700402 mutex_lock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700403
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200404 val = intel_freq_opcode(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700405
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700406 if (val < dev_priv->rps.min_freq ||
407 val > dev_priv->rps.max_freq ||
Ben Widawskyb39fb292014-03-19 18:31:11 -0700408 val < dev_priv->rps.min_freq_softlimit) {
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700409 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700410 return -EINVAL;
411 }
412
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700413 if (val > dev_priv->rps.rp0_freq)
Ben Widawsky31c77382013-04-05 14:29:22 -0700414 DRM_DEBUG("User requested overclocking to %d\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200415 intel_gpu_freq(dev_priv, val));
Ben Widawsky31c77382013-04-05 14:29:22 -0700416
Ben Widawskyb39fb292014-03-19 18:31:11 -0700417 dev_priv->rps.max_freq_softlimit = val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700418
Ville Syrjäläf745a802015-01-23 21:04:23 +0200419 val = clamp_t(int, dev_priv->rps.cur_freq,
420 dev_priv->rps.min_freq_softlimit,
421 dev_priv->rps.max_freq_softlimit);
422
423 /* We still need *_set_rps to process the new max_delay and
424 * update the interrupt limits and PMINTRMSK even though
425 * frequency request may be unchanged. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +0200426 intel_set_rps(dev, val);
Chris Wilson6917c7b2013-11-06 13:56:26 -0200427
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700428 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700429
430 return count;
431}
432
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700433static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
434{
Dave Airlie14c8d112013-10-11 14:45:30 +1000435 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700436 struct drm_device *dev = minor->dev;
437 struct drm_i915_private *dev_priv = dev->dev_private;
438 int ret;
439
Tom O'Rourke5c9669c2013-09-16 14:56:43 -0700440 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
441
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700442 mutex_lock(&dev_priv->rps.hw_lock);
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200443 ret = intel_gpu_freq(dev_priv, dev_priv->rps.min_freq_softlimit);
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700444 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700445
Jani Nikula3e2a1552013-02-14 10:42:11 +0200446 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700447}
448
Ben Widawsky46ddf192012-09-12 18:12:07 -0700449static ssize_t gt_min_freq_mhz_store(struct device *kdev,
450 struct device_attribute *attr,
451 const char *buf, size_t count)
452{
Dave Airlie14c8d112013-10-11 14:45:30 +1000453 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700454 struct drm_device *dev = minor->dev;
455 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700456 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700457 ssize_t ret;
458
459 ret = kstrtou32(buf, 0, &val);
460 if (ret)
461 return ret;
462
Tom O'Rourke5c9669c2013-09-16 14:56:43 -0700463 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
464
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700465 mutex_lock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700466
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200467 val = intel_freq_opcode(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700468
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700469 if (val < dev_priv->rps.min_freq ||
470 val > dev_priv->rps.max_freq ||
471 val > dev_priv->rps.max_freq_softlimit) {
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700472 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700473 return -EINVAL;
474 }
475
Ben Widawskyb39fb292014-03-19 18:31:11 -0700476 dev_priv->rps.min_freq_softlimit = val;
Chris Wilson6917c7b2013-11-06 13:56:26 -0200477
Ville Syrjäläf745a802015-01-23 21:04:23 +0200478 val = clamp_t(int, dev_priv->rps.cur_freq,
479 dev_priv->rps.min_freq_softlimit,
480 dev_priv->rps.max_freq_softlimit);
481
482 /* We still need *_set_rps to process the new min_delay and
483 * update the interrupt limits and PMINTRMSK even though
484 * frequency request may be unchanged. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +0200485 intel_set_rps(dev, val);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700486
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700487 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700488
489 return count;
490
491}
492
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200493static DEVICE_ATTR(gt_act_freq_mhz, S_IRUGO, gt_act_freq_mhz_show, NULL);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700494static DEVICE_ATTR(gt_cur_freq_mhz, S_IRUGO, gt_cur_freq_mhz_show, NULL);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700495static DEVICE_ATTR(gt_max_freq_mhz, S_IRUGO | S_IWUSR, gt_max_freq_mhz_show, gt_max_freq_mhz_store);
496static 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 -0700497
Chris Wilson97e4eed2013-08-26 16:18:54 +0100498static DEVICE_ATTR(vlv_rpe_freq_mhz, S_IRUGO, vlv_rpe_freq_mhz_show, NULL);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700499
500static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
501static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
502static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
503static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
504
505/* For now we have a static number of RP states */
506static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
507{
Dave Airlie14c8d112013-10-11 14:45:30 +1000508 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700509 struct drm_device *dev = minor->dev;
510 struct drm_i915_private *dev_priv = dev->dev_private;
Akash Goelbc4d91f2015-02-26 16:09:47 +0530511 u32 val;
Ben Widawskyac6ae342012-09-07 19:43:44 -0700512
Akash Goelbc4d91f2015-02-26 16:09:47 +0530513 if (attr == &dev_attr_gt_RP0_freq_mhz)
514 val = intel_gpu_freq(dev_priv, dev_priv->rps.rp0_freq);
515 else if (attr == &dev_attr_gt_RP1_freq_mhz)
516 val = intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq);
517 else if (attr == &dev_attr_gt_RPn_freq_mhz)
518 val = intel_gpu_freq(dev_priv, dev_priv->rps.min_freq);
519 else
Ben Widawskyac6ae342012-09-07 19:43:44 -0700520 BUG();
Akash Goelbc4d91f2015-02-26 16:09:47 +0530521
Jani Nikula3e2a1552013-02-14 10:42:11 +0200522 return snprintf(buf, PAGE_SIZE, "%d\n", val);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700523}
524
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700525static const struct attribute *gen6_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200526 &dev_attr_gt_act_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700527 &dev_attr_gt_cur_freq_mhz.attr,
528 &dev_attr_gt_max_freq_mhz.attr,
529 &dev_attr_gt_min_freq_mhz.attr,
Ben Widawskyac6ae342012-09-07 19:43:44 -0700530 &dev_attr_gt_RP0_freq_mhz.attr,
531 &dev_attr_gt_RP1_freq_mhz.attr,
532 &dev_attr_gt_RPn_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700533 NULL,
534};
535
Chris Wilson97e4eed2013-08-26 16:18:54 +0100536static const struct attribute *vlv_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200537 &dev_attr_gt_act_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100538 &dev_attr_gt_cur_freq_mhz.attr,
539 &dev_attr_gt_max_freq_mhz.attr,
540 &dev_attr_gt_min_freq_mhz.attr,
Deepak S74c4f622014-07-10 13:16:22 +0530541 &dev_attr_gt_RP0_freq_mhz.attr,
542 &dev_attr_gt_RP1_freq_mhz.attr,
543 &dev_attr_gt_RPn_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100544 &dev_attr_vlv_rpe_freq_mhz.attr,
545 NULL,
546};
547
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300548static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
549 struct bin_attribute *attr, char *buf,
550 loff_t off, size_t count)
551{
552
553 struct device *kdev = container_of(kobj, struct device, kobj);
Dave Airlie14c8d112013-10-11 14:45:30 +1000554 struct drm_minor *minor = dev_to_drm_minor(kdev);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300555 struct drm_device *dev = minor->dev;
556 struct i915_error_state_file_priv error_priv;
557 struct drm_i915_error_state_buf error_str;
558 ssize_t ret_count = 0;
559 int ret;
560
561 memset(&error_priv, 0, sizeof(error_priv));
562
Chris Wilson0a4cd7c2014-08-22 14:41:39 +0100563 ret = i915_error_state_buf_init(&error_str, to_i915(dev), count, off);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300564 if (ret)
565 return ret;
566
567 error_priv.dev = dev;
568 i915_error_state_get(dev, &error_priv);
569
570 ret = i915_error_state_to_str(&error_str, &error_priv);
571 if (ret)
572 goto out;
573
574 ret_count = count < error_str.bytes ? count : error_str.bytes;
575
576 memcpy(buf, error_str.buf, ret_count);
577out:
578 i915_error_state_put(&error_priv);
579 i915_error_state_buf_release(&error_str);
580
581 return ret ?: ret_count;
582}
583
584static ssize_t error_state_write(struct file *file, struct kobject *kobj,
585 struct bin_attribute *attr, char *buf,
586 loff_t off, size_t count)
587{
588 struct device *kdev = container_of(kobj, struct device, kobj);
Dave Airlie14c8d112013-10-11 14:45:30 +1000589 struct drm_minor *minor = dev_to_drm_minor(kdev);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300590 struct drm_device *dev = minor->dev;
591 int ret;
592
593 DRM_DEBUG_DRIVER("Resetting error state\n");
594
595 ret = mutex_lock_interruptible(&dev->struct_mutex);
596 if (ret)
597 return ret;
598
599 i915_destroy_error_state(dev);
600 mutex_unlock(&dev->struct_mutex);
601
602 return count;
603}
604
605static struct bin_attribute error_state_attr = {
606 .attr.name = "error",
607 .attr.mode = S_IRUSR | S_IWUSR,
608 .size = 0,
609 .read = error_state_read,
610 .write = error_state_write,
611};
612
Ben Widawsky0136db582012-04-10 21:17:01 -0700613void i915_setup_sysfs(struct drm_device *dev)
614{
615 int ret;
616
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700617#ifdef CONFIG_PM
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700618 if (HAS_RC6(dev)) {
Dave Airlie5bdebb12013-10-11 14:07:25 +1000619 ret = sysfs_merge_group(&dev->primary->kdev->kobj,
Daniel Vetter112abd22012-05-31 14:57:43 +0200620 &rc6_attr_group);
621 if (ret)
622 DRM_ERROR("RC6 residency sysfs setup failed\n");
623 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700624 if (HAS_RC6p(dev)) {
625 ret = sysfs_merge_group(&dev->primary->kdev->kobj,
626 &rc6p_attr_group);
627 if (ret)
628 DRM_ERROR("RC6p residency sysfs setup failed\n");
629 }
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530630 if (IS_VALLEYVIEW(dev)) {
631 ret = sysfs_merge_group(&dev->primary->kdev->kobj,
632 &media_rc6_attr_group);
633 if (ret)
634 DRM_ERROR("Media RC6 residency sysfs setup failed\n");
635 }
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700636#endif
Ben Widawsky040d2ba2013-09-19 11:01:40 -0700637 if (HAS_L3_DPF(dev)) {
Dave Airlie5bdebb12013-10-11 14:07:25 +1000638 ret = device_create_bin_file(dev->primary->kdev, &dpf_attrs);
Daniel Vetter112abd22012-05-31 14:57:43 +0200639 if (ret)
640 DRM_ERROR("l3 parity sysfs setup failed\n");
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700641
642 if (NUM_L3_SLICES(dev) > 1) {
Dave Airlie5bdebb12013-10-11 14:07:25 +1000643 ret = device_create_bin_file(dev->primary->kdev,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700644 &dpf_attrs_1);
645 if (ret)
646 DRM_ERROR("l3 parity slice 1 setup failed\n");
647 }
Daniel Vetter112abd22012-05-31 14:57:43 +0200648 }
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700649
Chris Wilson97e4eed2013-08-26 16:18:54 +0100650 ret = 0;
651 if (IS_VALLEYVIEW(dev))
Dave Airlie5bdebb12013-10-11 14:07:25 +1000652 ret = sysfs_create_files(&dev->primary->kdev->kobj, vlv_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100653 else if (INTEL_INFO(dev)->gen >= 6)
Dave Airlie5bdebb12013-10-11 14:07:25 +1000654 ret = sysfs_create_files(&dev->primary->kdev->kobj, gen6_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100655 if (ret)
656 DRM_ERROR("RPS sysfs setup failed\n");
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300657
Dave Airlie5bdebb12013-10-11 14:07:25 +1000658 ret = sysfs_create_bin_file(&dev->primary->kdev->kobj,
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300659 &error_state_attr);
660 if (ret)
661 DRM_ERROR("error_state sysfs setup failed\n");
Ben Widawsky0136db582012-04-10 21:17:01 -0700662}
663
664void i915_teardown_sysfs(struct drm_device *dev)
665{
Dave Airlie5bdebb12013-10-11 14:07:25 +1000666 sysfs_remove_bin_file(&dev->primary->kdev->kobj, &error_state_attr);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100667 if (IS_VALLEYVIEW(dev))
Dave Airlie5bdebb12013-10-11 14:07:25 +1000668 sysfs_remove_files(&dev->primary->kdev->kobj, vlv_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100669 else
Dave Airlie5bdebb12013-10-11 14:07:25 +1000670 sysfs_remove_files(&dev->primary->kdev->kobj, gen6_attrs);
671 device_remove_bin_file(dev->primary->kdev, &dpf_attrs_1);
672 device_remove_bin_file(dev->primary->kdev, &dpf_attrs);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700673#ifdef CONFIG_PM
Dave Airlie5bdebb12013-10-11 14:07:25 +1000674 sysfs_unmerge_group(&dev->primary->kdev->kobj, &rc6_attr_group);
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700675 sysfs_unmerge_group(&dev->primary->kdev->kobj, &rc6p_attr_group);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700676#endif
Ben Widawsky0136db582012-04-10 21:17:01 -0700677}