blob: e9ffefb720de849e4d05a264553694d439872d8d [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
Jesse Barnese454a052013-09-26 17:55:58 -070050 /* On VLV, residency time is in CZ units rather than 1.28us */
51 if (IS_VALLEYVIEW(dev)) {
52 u32 clkctl2;
53
54 clkctl2 = I915_READ(VLV_CLK_CTL2) >>
55 CLK_CTL2_CZCOUNT_30NS_SHIFT;
56 if (!clkctl2) {
57 WARN(!clkctl2, "bogus CZ count value");
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020058 ret = 0;
59 goto out;
Jesse Barnese454a052013-09-26 17:55:58 -070060 }
61 units = DIV_ROUND_UP_ULL(30ULL * bias, (u64)clkctl2);
62 if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
63 units <<= 8;
64
65 div = 1000000ULL * bias;
66 }
67
68 raw_time = I915_READ(reg) * units;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -020069 ret = DIV_ROUND_UP_ULL(raw_time, div);
70
71out:
72 intel_runtime_pm_put(dev_priv);
73 return ret;
Ben Widawsky0136db582012-04-10 21:17:01 -070074}
75
76static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070077show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -070078{
Dave Airlie14c8d112013-10-11 14:45:30 +100079 struct drm_minor *dminor = dev_to_drm_minor(kdev);
Jani Nikula3e2a1552013-02-14 10:42:11 +020080 return snprintf(buf, PAGE_SIZE, "%x\n", intel_enable_rc6(dminor->dev));
Ben Widawsky0136db582012-04-10 21:17:01 -070081}
82
83static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070084show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -070085{
Dave Airlie5bdebb12013-10-11 14:07:25 +100086 struct drm_minor *dminor = dev_get_drvdata(kdev);
Ben Widawsky0136db582012-04-10 21:17:01 -070087 u32 rc6_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6);
Jani Nikula3e2a1552013-02-14 10:42:11 +020088 return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
Ben Widawsky0136db582012-04-10 21:17:01 -070089}
90
91static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070092show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -070093{
Dave Airlie14c8d112013-10-11 14:45:30 +100094 struct drm_minor *dminor = dev_to_drm_minor(kdev);
Ben Widawsky0136db582012-04-10 21:17:01 -070095 u32 rc6p_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6p);
Jesse Barnes5ffd4942013-09-11 13:43:20 -070096 if (IS_VALLEYVIEW(dminor->dev))
97 rc6p_residency = 0;
Jani Nikula3e2a1552013-02-14 10:42:11 +020098 return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency);
Ben Widawsky0136db582012-04-10 21:17:01 -070099}
100
101static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -0700102show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -0700103{
Dave Airlie14c8d112013-10-11 14:45:30 +1000104 struct drm_minor *dminor = dev_to_drm_minor(kdev);
Ben Widawsky0136db582012-04-10 21:17:01 -0700105 u32 rc6pp_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6pp);
Jesse Barnes5ffd4942013-09-11 13:43:20 -0700106 if (IS_VALLEYVIEW(dminor->dev))
107 rc6pp_residency = 0;
Jani Nikula3e2a1552013-02-14 10:42:11 +0200108 return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency);
Ben Widawsky0136db582012-04-10 21:17:01 -0700109}
110
111static 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);
115
116static struct attribute *rc6_attrs[] = {
117 &dev_attr_rc6_enable.attr,
118 &dev_attr_rc6_residency_ms.attr,
119 &dev_attr_rc6p_residency_ms.attr,
120 &dev_attr_rc6pp_residency_ms.attr,
121 NULL
122};
123
124static struct attribute_group rc6_attr_group = {
125 .name = power_group_name,
126 .attrs = rc6_attrs
127};
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700128#endif
Ben Widawsky0136db582012-04-10 21:17:01 -0700129
Ben Widawsky84bc7582012-05-25 16:56:25 -0700130static int l3_access_valid(struct drm_device *dev, loff_t offset)
131{
Ben Widawsky040d2ba2013-09-19 11:01:40 -0700132 if (!HAS_L3_DPF(dev))
Ben Widawsky84bc7582012-05-25 16:56:25 -0700133 return -EPERM;
134
135 if (offset % 4 != 0)
136 return -EINVAL;
137
138 if (offset >= GEN7_L3LOG_SIZE)
139 return -ENXIO;
140
141 return 0;
142}
143
144static ssize_t
145i915_l3_read(struct file *filp, struct kobject *kobj,
146 struct bin_attribute *attr, char *buf,
147 loff_t offset, size_t count)
148{
149 struct device *dev = container_of(kobj, struct device, kobj);
Dave Airlie14c8d112013-10-11 14:45:30 +1000150 struct drm_minor *dminor = dev_to_drm_minor(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700151 struct drm_device *drm_dev = dminor->dev;
152 struct drm_i915_private *dev_priv = drm_dev->dev_private;
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700153 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700154 int ret;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700155
Ben Widawsky1c3dcd12013-09-12 22:28:28 -0700156 count = round_down(count, 4);
157
Ben Widawsky84bc7582012-05-25 16:56:25 -0700158 ret = l3_access_valid(drm_dev, offset);
159 if (ret)
160 return ret;
161
Dan Carpentere5ad4022013-09-20 14:20:18 +0300162 count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
Ben Widawsky33618ea2013-09-12 22:28:29 -0700163
Ben Widawsky84bc7582012-05-25 16:56:25 -0700164 ret = i915_mutex_lock_interruptible(drm_dev);
165 if (ret)
166 return ret;
167
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700168 if (dev_priv->l3_parity.remap_info[slice])
169 memcpy(buf,
170 dev_priv->l3_parity.remap_info[slice] + (offset/4),
171 count);
172 else
173 memset(buf, 0, count);
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700174
Ben Widawsky84bc7582012-05-25 16:56:25 -0700175 mutex_unlock(&drm_dev->struct_mutex);
176
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700177 return count;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700178}
179
180static ssize_t
181i915_l3_write(struct file *filp, struct kobject *kobj,
182 struct bin_attribute *attr, char *buf,
183 loff_t offset, size_t count)
184{
185 struct device *dev = container_of(kobj, struct device, kobj);
Dave Airlie14c8d112013-10-11 14:45:30 +1000186 struct drm_minor *dminor = dev_to_drm_minor(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700187 struct drm_device *drm_dev = dminor->dev;
188 struct drm_i915_private *dev_priv = drm_dev->dev_private;
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700189 struct i915_hw_context *ctx;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700190 u32 *temp = NULL; /* Just here to make handling failures easy */
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700191 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700192 int ret;
193
Ben Widawsky8245be32013-11-06 13:56:29 -0200194 if (!HAS_HW_CONTEXTS(drm_dev))
195 return -ENXIO;
196
Ben Widawsky84bc7582012-05-25 16:56:25 -0700197 ret = l3_access_valid(drm_dev, offset);
198 if (ret)
199 return ret;
200
201 ret = i915_mutex_lock_interruptible(drm_dev);
202 if (ret)
203 return ret;
204
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700205 if (!dev_priv->l3_parity.remap_info[slice]) {
Ben Widawsky84bc7582012-05-25 16:56:25 -0700206 temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
207 if (!temp) {
208 mutex_unlock(&drm_dev->struct_mutex);
209 return -ENOMEM;
210 }
211 }
212
213 ret = i915_gpu_idle(drm_dev);
214 if (ret) {
215 kfree(temp);
216 mutex_unlock(&drm_dev->struct_mutex);
217 return ret;
218 }
219
220 /* TODO: Ideally we really want a GPU reset here to make sure errors
221 * aren't propagated. Since I cannot find a stable way to reset the GPU
222 * at this point it is left as a TODO.
223 */
224 if (temp)
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700225 dev_priv->l3_parity.remap_info[slice] = temp;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700226
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700227 memcpy(dev_priv->l3_parity.remap_info[slice] + (offset/4), buf, count);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700228
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700229 /* NB: We defer the remapping until we switch to the context */
230 list_for_each_entry(ctx, &dev_priv->context_list, link)
231 ctx->remap_slice |= (1<<slice);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700232
233 mutex_unlock(&drm_dev->struct_mutex);
234
235 return count;
236}
237
238static struct bin_attribute dpf_attrs = {
239 .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
240 .size = GEN7_L3LOG_SIZE,
241 .read = i915_l3_read,
242 .write = i915_l3_write,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700243 .mmap = NULL,
244 .private = (void *)0
245};
246
247static struct bin_attribute dpf_attrs_1 = {
248 .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
249 .size = GEN7_L3LOG_SIZE,
250 .read = i915_l3_read,
251 .write = i915_l3_write,
252 .mmap = NULL,
253 .private = (void *)1
Ben Widawsky84bc7582012-05-25 16:56:25 -0700254};
255
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700256static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
257 struct device_attribute *attr, char *buf)
258{
Dave Airlie14c8d112013-10-11 14:45:30 +1000259 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700260 struct drm_device *dev = minor->dev;
261 struct drm_i915_private *dev_priv = dev->dev_private;
262 int ret;
263
Tom O'Rourke5c9669c2013-09-16 14:56:43 -0700264 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
265
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700266 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes177006a2013-05-02 10:48:07 -0700267 if (IS_VALLEYVIEW(dev_priv->dev)) {
268 u32 freq;
Jani Nikula64936252013-05-22 15:36:20 +0300269 freq = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Ville Syrjälä2ec38152013-11-05 22:42:29 +0200270 ret = vlv_gpu_freq(dev_priv, (freq >> 8) & 0xff);
Jesse Barnes177006a2013-05-02 10:48:07 -0700271 } else {
Jesse Barnes0a073b82013-04-17 15:54:58 -0700272 ret = dev_priv->rps.cur_delay * GT_FREQUENCY_MULTIPLIER;
Jesse Barnes177006a2013-05-02 10:48:07 -0700273 }
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700274 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700275
Jani Nikula3e2a1552013-02-14 10:42:11 +0200276 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700277}
278
Chris Wilson97e4eed2013-08-26 16:18:54 +0100279static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
280 struct device_attribute *attr, char *buf)
281{
Dave Airlie14c8d112013-10-11 14:45:30 +1000282 struct drm_minor *minor = dev_to_drm_minor(kdev);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100283 struct drm_device *dev = minor->dev;
284 struct drm_i915_private *dev_priv = dev->dev_private;
285
286 return snprintf(buf, PAGE_SIZE, "%d\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +0200287 vlv_gpu_freq(dev_priv, dev_priv->rps.rpe_delay));
Chris Wilson97e4eed2013-08-26 16:18:54 +0100288}
289
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700290static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
291{
Dave Airlie14c8d112013-10-11 14:45:30 +1000292 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700293 struct drm_device *dev = minor->dev;
294 struct drm_i915_private *dev_priv = dev->dev_private;
295 int ret;
296
Tom O'Rourke5c9669c2013-09-16 14:56:43 -0700297 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
298
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700299 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700300 if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä2ec38152013-11-05 22:42:29 +0200301 ret = vlv_gpu_freq(dev_priv, dev_priv->rps.max_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700302 else
303 ret = dev_priv->rps.max_delay * GT_FREQUENCY_MULTIPLIER;
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700304 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700305
Jani Nikula3e2a1552013-02-14 10:42:11 +0200306 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700307}
308
Ben Widawsky46ddf192012-09-12 18:12:07 -0700309static ssize_t gt_max_freq_mhz_store(struct device *kdev,
310 struct device_attribute *attr,
311 const char *buf, size_t count)
312{
Dave Airlie14c8d112013-10-11 14:45:30 +1000313 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700314 struct drm_device *dev = minor->dev;
315 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky31c77382013-04-05 14:29:22 -0700316 u32 val, rp_state_cap, hw_max, hw_min, non_oc_max;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700317 ssize_t ret;
318
319 ret = kstrtou32(buf, 0, &val);
320 if (ret)
321 return ret;
322
Tom O'Rourke5c9669c2013-09-16 14:56:43 -0700323 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
324
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700325 mutex_lock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700326
Jesse Barnes0a073b82013-04-17 15:54:58 -0700327 if (IS_VALLEYVIEW(dev_priv->dev)) {
Ville Syrjälä2ec38152013-11-05 22:42:29 +0200328 val = vlv_freq_opcode(dev_priv, val);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700329
Jesse Barnes0a073b82013-04-17 15:54:58 -0700330 hw_max = valleyview_rps_max_freq(dev_priv);
331 hw_min = valleyview_rps_min_freq(dev_priv);
332 non_oc_max = hw_max;
333 } else {
334 val /= GT_FREQUENCY_MULTIPLIER;
335
336 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
337 hw_max = dev_priv->rps.hw_max;
338 non_oc_max = (rp_state_cap & 0xff);
339 hw_min = ((rp_state_cap & 0xff0000) >> 16);
340 }
341
342 if (val < hw_min || val > hw_max ||
343 val < dev_priv->rps.min_delay) {
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700344 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700345 return -EINVAL;
346 }
347
Ben Widawsky31c77382013-04-05 14:29:22 -0700348 if (val > non_oc_max)
349 DRM_DEBUG("User requested overclocking to %d\n",
350 val * GT_FREQUENCY_MULTIPLIER);
351
Ben Widawsky46ddf192012-09-12 18:12:07 -0700352 dev_priv->rps.max_delay = val;
353
Chris Wilson6917c7b2013-11-06 13:56:26 -0200354 if (dev_priv->rps.cur_delay > val) {
355 if (IS_VALLEYVIEW(dev))
356 valleyview_set_rps(dev, val);
357 else
358 gen6_set_rps(dev, val);
Ben Widawsky5a953ad2014-03-19 18:31:09 -0700359 } else if (!IS_VALLEYVIEW(dev)) {
360 /* We still need gen6_set_rps to process the new max_delay and
361 * update the interrupt limits even though frequency request is
362 * unchanged. */
Jeff McGeeb8a5ff82014-02-04 11:37:01 -0600363 gen6_set_rps(dev, dev_priv->rps.cur_delay);
Ben Widawsky5a953ad2014-03-19 18:31:09 -0700364 }
Chris Wilson6917c7b2013-11-06 13:56:26 -0200365
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700366 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700367
368 return count;
369}
370
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700371static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
372{
Dave Airlie14c8d112013-10-11 14:45:30 +1000373 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700374 struct drm_device *dev = minor->dev;
375 struct drm_i915_private *dev_priv = dev->dev_private;
376 int ret;
377
Tom O'Rourke5c9669c2013-09-16 14:56:43 -0700378 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
379
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700380 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700381 if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä2ec38152013-11-05 22:42:29 +0200382 ret = vlv_gpu_freq(dev_priv, dev_priv->rps.min_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700383 else
384 ret = dev_priv->rps.min_delay * GT_FREQUENCY_MULTIPLIER;
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700385 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700386
Jani Nikula3e2a1552013-02-14 10:42:11 +0200387 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700388}
389
Ben Widawsky46ddf192012-09-12 18:12:07 -0700390static ssize_t gt_min_freq_mhz_store(struct device *kdev,
391 struct device_attribute *attr,
392 const char *buf, size_t count)
393{
Dave Airlie14c8d112013-10-11 14:45:30 +1000394 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700395 struct drm_device *dev = minor->dev;
396 struct drm_i915_private *dev_priv = dev->dev_private;
397 u32 val, rp_state_cap, hw_max, hw_min;
398 ssize_t ret;
399
400 ret = kstrtou32(buf, 0, &val);
401 if (ret)
402 return ret;
403
Tom O'Rourke5c9669c2013-09-16 14:56:43 -0700404 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
405
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700406 mutex_lock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700407
Jesse Barnes0a073b82013-04-17 15:54:58 -0700408 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2ec38152013-11-05 22:42:29 +0200409 val = vlv_freq_opcode(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700410
411 hw_max = valleyview_rps_max_freq(dev_priv);
412 hw_min = valleyview_rps_min_freq(dev_priv);
413 } else {
414 val /= GT_FREQUENCY_MULTIPLIER;
415
416 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
417 hw_max = dev_priv->rps.hw_max;
418 hw_min = ((rp_state_cap & 0xff0000) >> 16);
419 }
Ben Widawsky46ddf192012-09-12 18:12:07 -0700420
421 if (val < hw_min || val > hw_max || val > dev_priv->rps.max_delay) {
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700422 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700423 return -EINVAL;
424 }
425
Chris Wilson6917c7b2013-11-06 13:56:26 -0200426 dev_priv->rps.min_delay = val;
427
Jesse Barnes0a073b82013-04-17 15:54:58 -0700428 if (dev_priv->rps.cur_delay < val) {
429 if (IS_VALLEYVIEW(dev))
430 valleyview_set_rps(dev, val);
431 else
Chris Wilson6917c7b2013-11-06 13:56:26 -0200432 gen6_set_rps(dev, val);
Ben Widawsky5a953ad2014-03-19 18:31:09 -0700433 } else if (!IS_VALLEYVIEW(dev)) {
434 /* We still need gen6_set_rps to process the new min_delay and
435 * update the interrupt limits even though frequency request is
436 * unchanged. */
Jeff McGeeb8a5ff82014-02-04 11:37:01 -0600437 gen6_set_rps(dev, dev_priv->rps.cur_delay);
Ben Widawsky5a953ad2014-03-19 18:31:09 -0700438 }
Ben Widawsky46ddf192012-09-12 18:12:07 -0700439
Jesse Barnes4fc688c2012-11-02 11:14:01 -0700440 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700441
442 return count;
443
444}
445
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700446static DEVICE_ATTR(gt_cur_freq_mhz, S_IRUGO, gt_cur_freq_mhz_show, NULL);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700447static DEVICE_ATTR(gt_max_freq_mhz, S_IRUGO | S_IWUSR, gt_max_freq_mhz_show, gt_max_freq_mhz_store);
448static 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 -0700449
Chris Wilson97e4eed2013-08-26 16:18:54 +0100450static DEVICE_ATTR(vlv_rpe_freq_mhz, S_IRUGO, vlv_rpe_freq_mhz_show, NULL);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700451
452static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
453static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
454static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
455static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
456
457/* For now we have a static number of RP states */
458static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
459{
Dave Airlie14c8d112013-10-11 14:45:30 +1000460 struct drm_minor *minor = dev_to_drm_minor(kdev);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700461 struct drm_device *dev = minor->dev;
462 struct drm_i915_private *dev_priv = dev->dev_private;
463 u32 val, rp_state_cap;
464 ssize_t ret;
465
466 ret = mutex_lock_interruptible(&dev->struct_mutex);
467 if (ret)
468 return ret;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -0200469 intel_runtime_pm_get(dev_priv);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700470 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Paulo Zanonic8c8fb32013-11-27 18:21:54 -0200471 intel_runtime_pm_put(dev_priv);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700472 mutex_unlock(&dev->struct_mutex);
473
474 if (attr == &dev_attr_gt_RP0_freq_mhz) {
475 val = ((rp_state_cap & 0x0000ff) >> 0) * GT_FREQUENCY_MULTIPLIER;
476 } else if (attr == &dev_attr_gt_RP1_freq_mhz) {
477 val = ((rp_state_cap & 0x00ff00) >> 8) * GT_FREQUENCY_MULTIPLIER;
478 } else if (attr == &dev_attr_gt_RPn_freq_mhz) {
479 val = ((rp_state_cap & 0xff0000) >> 16) * GT_FREQUENCY_MULTIPLIER;
480 } else {
481 BUG();
482 }
Jani Nikula3e2a1552013-02-14 10:42:11 +0200483 return snprintf(buf, PAGE_SIZE, "%d\n", val);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700484}
485
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700486static const struct attribute *gen6_attrs[] = {
487 &dev_attr_gt_cur_freq_mhz.attr,
488 &dev_attr_gt_max_freq_mhz.attr,
489 &dev_attr_gt_min_freq_mhz.attr,
Ben Widawskyac6ae342012-09-07 19:43:44 -0700490 &dev_attr_gt_RP0_freq_mhz.attr,
491 &dev_attr_gt_RP1_freq_mhz.attr,
492 &dev_attr_gt_RPn_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700493 NULL,
494};
495
Chris Wilson97e4eed2013-08-26 16:18:54 +0100496static const struct attribute *vlv_attrs[] = {
497 &dev_attr_gt_cur_freq_mhz.attr,
498 &dev_attr_gt_max_freq_mhz.attr,
499 &dev_attr_gt_min_freq_mhz.attr,
500 &dev_attr_vlv_rpe_freq_mhz.attr,
501 NULL,
502};
503
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300504static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
505 struct bin_attribute *attr, char *buf,
506 loff_t off, size_t count)
507{
508
509 struct device *kdev = container_of(kobj, struct device, kobj);
Dave Airlie14c8d112013-10-11 14:45:30 +1000510 struct drm_minor *minor = dev_to_drm_minor(kdev);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300511 struct drm_device *dev = minor->dev;
512 struct i915_error_state_file_priv error_priv;
513 struct drm_i915_error_state_buf error_str;
514 ssize_t ret_count = 0;
515 int ret;
516
517 memset(&error_priv, 0, sizeof(error_priv));
518
519 ret = i915_error_state_buf_init(&error_str, count, off);
520 if (ret)
521 return ret;
522
523 error_priv.dev = dev;
524 i915_error_state_get(dev, &error_priv);
525
526 ret = i915_error_state_to_str(&error_str, &error_priv);
527 if (ret)
528 goto out;
529
530 ret_count = count < error_str.bytes ? count : error_str.bytes;
531
532 memcpy(buf, error_str.buf, ret_count);
533out:
534 i915_error_state_put(&error_priv);
535 i915_error_state_buf_release(&error_str);
536
537 return ret ?: ret_count;
538}
539
540static ssize_t error_state_write(struct file *file, struct kobject *kobj,
541 struct bin_attribute *attr, char *buf,
542 loff_t off, size_t count)
543{
544 struct device *kdev = container_of(kobj, struct device, kobj);
Dave Airlie14c8d112013-10-11 14:45:30 +1000545 struct drm_minor *minor = dev_to_drm_minor(kdev);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300546 struct drm_device *dev = minor->dev;
547 int ret;
548
549 DRM_DEBUG_DRIVER("Resetting error state\n");
550
551 ret = mutex_lock_interruptible(&dev->struct_mutex);
552 if (ret)
553 return ret;
554
555 i915_destroy_error_state(dev);
556 mutex_unlock(&dev->struct_mutex);
557
558 return count;
559}
560
561static struct bin_attribute error_state_attr = {
562 .attr.name = "error",
563 .attr.mode = S_IRUSR | S_IWUSR,
564 .size = 0,
565 .read = error_state_read,
566 .write = error_state_write,
567};
568
Ben Widawsky0136db582012-04-10 21:17:01 -0700569void i915_setup_sysfs(struct drm_device *dev)
570{
571 int ret;
572
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700573#ifdef CONFIG_PM
Daniel Vetter112abd22012-05-31 14:57:43 +0200574 if (INTEL_INFO(dev)->gen >= 6) {
Dave Airlie5bdebb12013-10-11 14:07:25 +1000575 ret = sysfs_merge_group(&dev->primary->kdev->kobj,
Daniel Vetter112abd22012-05-31 14:57:43 +0200576 &rc6_attr_group);
577 if (ret)
578 DRM_ERROR("RC6 residency sysfs setup failed\n");
579 }
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700580#endif
Ben Widawsky040d2ba2013-09-19 11:01:40 -0700581 if (HAS_L3_DPF(dev)) {
Dave Airlie5bdebb12013-10-11 14:07:25 +1000582 ret = device_create_bin_file(dev->primary->kdev, &dpf_attrs);
Daniel Vetter112abd22012-05-31 14:57:43 +0200583 if (ret)
584 DRM_ERROR("l3 parity sysfs setup failed\n");
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700585
586 if (NUM_L3_SLICES(dev) > 1) {
Dave Airlie5bdebb12013-10-11 14:07:25 +1000587 ret = device_create_bin_file(dev->primary->kdev,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700588 &dpf_attrs_1);
589 if (ret)
590 DRM_ERROR("l3 parity slice 1 setup failed\n");
591 }
Daniel Vetter112abd22012-05-31 14:57:43 +0200592 }
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700593
Chris Wilson97e4eed2013-08-26 16:18:54 +0100594 ret = 0;
595 if (IS_VALLEYVIEW(dev))
Dave Airlie5bdebb12013-10-11 14:07:25 +1000596 ret = sysfs_create_files(&dev->primary->kdev->kobj, vlv_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100597 else if (INTEL_INFO(dev)->gen >= 6)
Dave Airlie5bdebb12013-10-11 14:07:25 +1000598 ret = sysfs_create_files(&dev->primary->kdev->kobj, gen6_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100599 if (ret)
600 DRM_ERROR("RPS sysfs setup failed\n");
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300601
Dave Airlie5bdebb12013-10-11 14:07:25 +1000602 ret = sysfs_create_bin_file(&dev->primary->kdev->kobj,
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300603 &error_state_attr);
604 if (ret)
605 DRM_ERROR("error_state sysfs setup failed\n");
Ben Widawsky0136db582012-04-10 21:17:01 -0700606}
607
608void i915_teardown_sysfs(struct drm_device *dev)
609{
Dave Airlie5bdebb12013-10-11 14:07:25 +1000610 sysfs_remove_bin_file(&dev->primary->kdev->kobj, &error_state_attr);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100611 if (IS_VALLEYVIEW(dev))
Dave Airlie5bdebb12013-10-11 14:07:25 +1000612 sysfs_remove_files(&dev->primary->kdev->kobj, vlv_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100613 else
Dave Airlie5bdebb12013-10-11 14:07:25 +1000614 sysfs_remove_files(&dev->primary->kdev->kobj, gen6_attrs);
615 device_remove_bin_file(dev->primary->kdev, &dpf_attrs_1);
616 device_remove_bin_file(dev->primary->kdev, &dpf_attrs);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700617#ifdef CONFIG_PM
Dave Airlie5bdebb12013-10-11 14:07:25 +1000618 sysfs_unmerge_group(&dev->primary->kdev->kobj, &rc6_attr_group);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700619#endif
Ben Widawsky0136db582012-04-10 21:17:01 -0700620}