blob: c0cfe7ae2ba5aecbe3daaf8bccce3eee362753a8 [file] [log] [blame]
Ben Widawsky0136db52012-04-10 21:17:01 -07001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Ben Widawsky <ben@bwidawsk.net>
25 *
26 */
27
28#include <linux/device.h>
29#include <linux/module.h>
30#include <linux/stat.h>
31#include <linux/sysfs.h>
Ben Widawsky84bc7582012-05-25 16:56:25 -070032#include "intel_drv.h"
Ben Widawsky0136db52012-04-10 21:17:01 -070033#include "i915_drv.h"
34
David Weinehall694c2822016-08-22 13:32:43 +030035static inline struct drm_i915_private *kdev_minor_to_i915(struct device *kdev)
David Weinehallc49d13e2016-08-22 13:32:42 +030036{
David Weinehall694c2822016-08-22 13:32:43 +030037 struct drm_minor *minor = dev_get_drvdata(kdev);
38 return to_i915(minor->dev);
David Weinehallc49d13e2016-08-22 13:32:42 +030039}
Dave Airlie14c8d1102013-10-11 14:45:30 +100040
Hunt Xu5ab36332012-07-01 03:45:07 +000041#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +030042static u32 calc_residency(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020043 i915_reg_t reg)
Ben Widawsky0136db52012-04-10 21:17:01 -070044{
Tvrtko Ursulin36cc8b92017-11-21 18:18:51 +000045 u64 res;
46
47 intel_runtime_pm_get(dev_priv);
48 res = intel_rc6_residency_us(dev_priv, reg);
49 intel_runtime_pm_put(dev_priv);
50
51 return DIV_ROUND_CLOSEST_ULL(res, 1000);
Ben Widawsky0136db52012-04-10 21:17:01 -070052}
53
54static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070055show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070056{
Chris Wilsonfb6db0f2017-12-01 11:30:30 +000057 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
58 unsigned int mask;
59
60 mask = 0;
61 if (HAS_RC6(dev_priv))
62 mask |= BIT(0);
63 if (HAS_RC6p(dev_priv))
64 mask |= BIT(1);
65 if (HAS_RC6pp(dev_priv))
66 mask |= BIT(2);
67
68 return snprintf(buf, PAGE_SIZE, "%x\n", mask);
Ben Widawsky0136db52012-04-10 21:17:01 -070069}
70
71static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070072show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070073{
David Weinehall694c2822016-08-22 13:32:43 +030074 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
75 u32 rc6_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6);
Jani Nikula3e2a1552013-02-14 10:42:11 +020076 return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070077}
78
79static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070080show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070081{
David Weinehall694c2822016-08-22 13:32:43 +030082 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
83 u32 rc6p_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6p);
Jani Nikula3e2a1552013-02-14 10:42:11 +020084 return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070085}
86
87static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070088show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db52012-04-10 21:17:01 -070089{
David Weinehall694c2822016-08-22 13:32:43 +030090 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
91 u32 rc6pp_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6pp);
Jani Nikula3e2a1552013-02-14 10:42:11 +020092 return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency);
Ben Widawsky0136db52012-04-10 21:17:01 -070093}
94
Ville Syrjälä626ad6f2015-02-26 21:10:27 +053095static ssize_t
96show_media_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
97{
David Weinehall694c2822016-08-22 13:32:43 +030098 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
99 u32 rc6_residency = calc_residency(dev_priv, VLV_GT_MEDIA_RC6);
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530100 return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
101}
102
Ben Widawsky0136db52012-04-10 21:17:01 -0700103static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL);
104static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL);
105static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL);
106static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL);
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530107static DEVICE_ATTR(media_rc6_residency_ms, S_IRUGO, show_media_rc6_ms, NULL);
Ben Widawsky0136db52012-04-10 21:17:01 -0700108
109static struct attribute *rc6_attrs[] = {
110 &dev_attr_rc6_enable.attr,
111 &dev_attr_rc6_residency_ms.attr,
Ben Widawsky0136db52012-04-10 21:17:01 -0700112 NULL
113};
114
Arvind Yadav0a7a0982017-07-03 16:38:25 +0530115static const struct attribute_group rc6_attr_group = {
Ben Widawsky0136db52012-04-10 21:17:01 -0700116 .name = power_group_name,
117 .attrs = rc6_attrs
118};
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700119
120static struct attribute *rc6p_attrs[] = {
121 &dev_attr_rc6p_residency_ms.attr,
122 &dev_attr_rc6pp_residency_ms.attr,
123 NULL
124};
125
Arvind Yadav0a7a0982017-07-03 16:38:25 +0530126static const struct attribute_group rc6p_attr_group = {
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700127 .name = power_group_name,
128 .attrs = rc6p_attrs
129};
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530130
131static struct attribute *media_rc6_attrs[] = {
132 &dev_attr_media_rc6_residency_ms.attr,
133 NULL
134};
135
Arvind Yadav0a7a0982017-07-03 16:38:25 +0530136static const struct attribute_group media_rc6_attr_group = {
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530137 .name = power_group_name,
138 .attrs = media_rc6_attrs
139};
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700140#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700141
David Weinehall694c2822016-08-22 13:32:43 +0300142static int l3_access_valid(struct drm_i915_private *dev_priv, loff_t offset)
Ben Widawsky84bc7582012-05-25 16:56:25 -0700143{
David Weinehall694c2822016-08-22 13:32:43 +0300144 if (!HAS_L3_DPF(dev_priv))
Ben Widawsky84bc7582012-05-25 16:56:25 -0700145 return -EPERM;
146
147 if (offset % 4 != 0)
148 return -EINVAL;
149
150 if (offset >= GEN7_L3LOG_SIZE)
151 return -ENXIO;
152
153 return 0;
154}
155
156static ssize_t
157i915_l3_read(struct file *filp, struct kobject *kobj,
158 struct bin_attribute *attr, char *buf,
159 loff_t offset, size_t count)
160{
David Weinehallc49d13e2016-08-22 13:32:42 +0300161 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300162 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
163 struct drm_device *dev = &dev_priv->drm;
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700164 int slice = (int)(uintptr_t)attr->private;
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700165 int ret;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700166
Ben Widawsky1c3dcd12013-09-12 22:28:28 -0700167 count = round_down(count, 4);
168
David Weinehall694c2822016-08-22 13:32:43 +0300169 ret = l3_access_valid(dev_priv, offset);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700170 if (ret)
171 return ret;
172
Dan Carpentere5ad4022013-09-20 14:20:18 +0300173 count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
Ben Widawsky33618ea2013-09-12 22:28:29 -0700174
David Weinehallc49d13e2016-08-22 13:32:42 +0300175 ret = i915_mutex_lock_interruptible(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700176 if (ret)
177 return ret;
178
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700179 if (dev_priv->l3_parity.remap_info[slice])
180 memcpy(buf,
181 dev_priv->l3_parity.remap_info[slice] + (offset/4),
182 count);
183 else
184 memset(buf, 0, count);
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700185
David Weinehallc49d13e2016-08-22 13:32:42 +0300186 mutex_unlock(&dev->struct_mutex);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700187
Ben Widawsky1c966dd2013-09-17 21:12:42 -0700188 return count;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700189}
190
191static ssize_t
192i915_l3_write(struct file *filp, struct kobject *kobj,
193 struct bin_attribute *attr, char *buf,
194 loff_t offset, size_t count)
195{
David Weinehallc49d13e2016-08-22 13:32:42 +0300196 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300197 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
198 struct drm_device *dev = &dev_priv->drm;
Chris Wilsone2efd132016-05-24 14:53:34 +0100199 struct i915_gem_context *ctx;
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700200 int slice = (int)(uintptr_t)attr->private;
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300201 u32 **remap_info;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700202 int ret;
203
David Weinehall694c2822016-08-22 13:32:43 +0300204 ret = l3_access_valid(dev_priv, offset);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700205 if (ret)
206 return ret;
207
David Weinehallc49d13e2016-08-22 13:32:42 +0300208 ret = i915_mutex_lock_interruptible(dev);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700209 if (ret)
210 return ret;
211
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300212 remap_info = &dev_priv->l3_parity.remap_info[slice];
213 if (!*remap_info) {
214 *remap_info = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
215 if (!*remap_info) {
216 ret = -ENOMEM;
217 goto out;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700218 }
219 }
220
Ben Widawsky84bc7582012-05-25 16:56:25 -0700221 /* TODO: Ideally we really want a GPU reset here to make sure errors
222 * aren't propagated. Since I cannot find a stable way to reset the GPU
223 * at this point it is left as a TODO.
224 */
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300225 memcpy(*remap_info + (offset/4), buf, count);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700226
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700227 /* NB: We defer the remapping until we switch to the context */
Chris Wilson829a0af2017-06-20 12:05:45 +0100228 list_for_each_entry(ctx, &dev_priv->contexts.list, link)
Ben Widawsky3ccfd192013-09-18 19:03:18 -0700229 ctx->remap_slice |= (1<<slice);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700230
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300231 ret = count;
232
233out:
David Weinehallc49d13e2016-08-22 13:32:42 +0300234 mutex_unlock(&dev->struct_mutex);
Ben Widawsky84bc7582012-05-25 16:56:25 -0700235
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300236 return ret;
Ben Widawsky84bc7582012-05-25 16:56:25 -0700237}
238
Bhumika Goyal59f3da12017-08-02 22:50:47 +0530239static const struct bin_attribute dpf_attrs = {
Ben Widawsky84bc7582012-05-25 16:56:25 -0700240 .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
241 .size = GEN7_L3LOG_SIZE,
242 .read = i915_l3_read,
243 .write = i915_l3_write,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700244 .mmap = NULL,
245 .private = (void *)0
246};
247
Bhumika Goyal59f3da12017-08-02 22:50:47 +0530248static const struct bin_attribute dpf_attrs_1 = {
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700249 .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
250 .size = GEN7_L3LOG_SIZE,
251 .read = i915_l3_read,
252 .write = i915_l3_write,
253 .mmap = NULL,
254 .private = (void *)1
Ben Widawsky84bc7582012-05-25 16:56:25 -0700255};
256
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200257static ssize_t gt_act_freq_mhz_show(struct device *kdev,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700258 struct device_attribute *attr, char *buf)
259{
David Weinehall694c2822016-08-22 13:32:43 +0300260 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700261 int ret;
262
Imre Deakd46c0512014-04-14 20:24:27 +0300263 intel_runtime_pm_get(dev_priv);
264
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100265 mutex_lock(&dev_priv->pcu_lock);
Wayne Boyer666a4532015-12-09 12:29:35 -0800266 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jesse Barnes177006a2013-05-02 10:48:07 -0700267 u32 freq;
Jani Nikula64936252013-05-22 15:36:20 +0300268 freq = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200269 ret = intel_gpu_freq(dev_priv, (freq >> 8) & 0xff);
Jesse Barnes177006a2013-05-02 10:48:07 -0700270 } else {
Tvrtko Ursulinc84b2702017-11-21 18:18:44 +0000271 ret = intel_gpu_freq(dev_priv,
272 intel_get_cagf(dev_priv,
273 I915_READ(GEN6_RPSTAT1)));
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200274 }
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100275 mutex_unlock(&dev_priv->pcu_lock);
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200276
277 intel_runtime_pm_put(dev_priv);
278
279 return snprintf(buf, PAGE_SIZE, "%d\n", ret);
280}
281
282static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
283 struct device_attribute *attr, char *buf)
284{
David Weinehall694c2822016-08-22 13:32:43 +0300285 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200286
Chris Wilson62e1baa2016-07-13 09:10:36 +0100287 return snprintf(buf, PAGE_SIZE, "%d\n",
288 intel_gpu_freq(dev_priv,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100289 dev_priv->gt_pm.rps.cur_freq));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700290}
291
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100292static ssize_t gt_boost_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
293{
David Weinehall694c2822016-08-22 13:32:43 +0300294 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100295
296 return snprintf(buf, PAGE_SIZE, "%d\n",
Chris Wilson62e1baa2016-07-13 09:10:36 +0100297 intel_gpu_freq(dev_priv,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100298 dev_priv->gt_pm.rps.boost_freq));
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100299}
300
301static ssize_t gt_boost_freq_mhz_store(struct device *kdev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
304{
David Weinehall694c2822016-08-22 13:32:43 +0300305 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100306 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Chris Wilson59cd31f2018-03-08 14:26:47 +0000307 bool boost = false;
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100308 ssize_t ret;
Chris Wilson59cd31f2018-03-08 14:26:47 +0000309 u32 val;
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100310
311 ret = kstrtou32(buf, 0, &val);
312 if (ret)
313 return ret;
314
315 /* Validate against (static) hardware limits */
316 val = intel_freq_opcode(dev_priv, val);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100317 if (val < rps->min_freq || val > rps->max_freq)
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100318 return -EINVAL;
319
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100320 mutex_lock(&dev_priv->pcu_lock);
Chris Wilson59cd31f2018-03-08 14:26:47 +0000321 if (val != rps->boost_freq) {
322 rps->boost_freq = val;
323 boost = atomic_read(&rps->num_waiters);
324 }
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100325 mutex_unlock(&dev_priv->pcu_lock);
Chris Wilson59cd31f2018-03-08 14:26:47 +0000326 if (boost)
327 schedule_work(&rps->work);
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100328
329 return count;
330}
331
Chris Wilson97e4eed2013-08-26 16:18:54 +0100332static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
333 struct device_attribute *attr, char *buf)
334{
David Weinehall694c2822016-08-22 13:32:43 +0300335 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100336
Chris Wilson62e1baa2016-07-13 09:10:36 +0100337 return snprintf(buf, PAGE_SIZE, "%d\n",
338 intel_gpu_freq(dev_priv,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100339 dev_priv->gt_pm.rps.efficient_freq));
Chris Wilson97e4eed2013-08-26 16:18:54 +0100340}
341
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700342static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
343{
David Weinehall694c2822016-08-22 13:32:43 +0300344 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700345
Chris Wilson62e1baa2016-07-13 09:10:36 +0100346 return snprintf(buf, PAGE_SIZE, "%d\n",
347 intel_gpu_freq(dev_priv,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100348 dev_priv->gt_pm.rps.max_freq_softlimit));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700349}
350
Ben Widawsky46ddf192012-09-12 18:12:07 -0700351static ssize_t gt_max_freq_mhz_store(struct device *kdev,
352 struct device_attribute *attr,
353 const char *buf, size_t count)
354{
David Weinehall694c2822016-08-22 13:32:43 +0300355 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100356 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700357 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700358 ssize_t ret;
359
360 ret = kstrtou32(buf, 0, &val);
361 if (ret)
362 return ret;
363
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530364 intel_runtime_pm_get(dev_priv);
365
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100366 mutex_lock(&dev_priv->pcu_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700367
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200368 val = intel_freq_opcode(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700369
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100370 if (val < rps->min_freq ||
371 val > rps->max_freq ||
372 val < rps->min_freq_softlimit) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100373 mutex_unlock(&dev_priv->pcu_lock);
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530374 intel_runtime_pm_put(dev_priv);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700375 return -EINVAL;
376 }
377
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100378 if (val > rps->rp0_freq)
Ben Widawsky31c77382013-04-05 14:29:22 -0700379 DRM_DEBUG("User requested overclocking to %d\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200380 intel_gpu_freq(dev_priv, val));
Ben Widawsky31c77382013-04-05 14:29:22 -0700381
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100382 rps->max_freq_softlimit = val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700383
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100384 val = clamp_t(int, rps->cur_freq,
385 rps->min_freq_softlimit,
386 rps->max_freq_softlimit);
Ville Syrjäläf745a802015-01-23 21:04:23 +0200387
388 /* We still need *_set_rps to process the new max_delay and
389 * update the interrupt limits and PMINTRMSK even though
390 * frequency request may be unchanged. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +0000391 ret = intel_set_rps(dev_priv, val);
Chris Wilson6917c7b2013-11-06 13:56:26 -0200392
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100393 mutex_unlock(&dev_priv->pcu_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700394
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530395 intel_runtime_pm_put(dev_priv);
396
Chris Wilson9fcee2f2017-01-26 10:19:19 +0000397 return ret ?: count;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700398}
399
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700400static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
401{
David Weinehall694c2822016-08-22 13:32:43 +0300402 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700403
Chris Wilson62e1baa2016-07-13 09:10:36 +0100404 return snprintf(buf, PAGE_SIZE, "%d\n",
405 intel_gpu_freq(dev_priv,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100406 dev_priv->gt_pm.rps.min_freq_softlimit));
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700407}
408
Ben Widawsky46ddf192012-09-12 18:12:07 -0700409static ssize_t gt_min_freq_mhz_store(struct device *kdev,
410 struct device_attribute *attr,
411 const char *buf, size_t count)
412{
David Weinehall694c2822016-08-22 13:32:43 +0300413 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100414 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Ben Widawsky2a5913a2014-03-19 18:31:13 -0700415 u32 val;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700416 ssize_t ret;
417
418 ret = kstrtou32(buf, 0, &val);
419 if (ret)
420 return ret;
421
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530422 intel_runtime_pm_get(dev_priv);
423
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100424 mutex_lock(&dev_priv->pcu_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700425
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +0200426 val = intel_freq_opcode(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -0700427
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100428 if (val < rps->min_freq ||
429 val > rps->max_freq ||
430 val > rps->max_freq_softlimit) {
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100431 mutex_unlock(&dev_priv->pcu_lock);
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530432 intel_runtime_pm_put(dev_priv);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700433 return -EINVAL;
434 }
435
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100436 rps->min_freq_softlimit = val;
Chris Wilson6917c7b2013-11-06 13:56:26 -0200437
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100438 val = clamp_t(int, rps->cur_freq,
439 rps->min_freq_softlimit,
440 rps->max_freq_softlimit);
Ville Syrjäläf745a802015-01-23 21:04:23 +0200441
442 /* We still need *_set_rps to process the new min_delay and
443 * update the interrupt limits and PMINTRMSK even though
444 * frequency request may be unchanged. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +0000445 ret = intel_set_rps(dev_priv, val);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700446
Sagar Arun Kamble9f817502017-10-10 22:30:05 +0100447 mutex_unlock(&dev_priv->pcu_lock);
Ben Widawsky46ddf192012-09-12 18:12:07 -0700448
Sagar Arun Kamble933bfb42016-02-08 22:47:11 +0530449 intel_runtime_pm_put(dev_priv);
450
Chris Wilson9fcee2f2017-01-26 10:19:19 +0000451 return ret ?: count;
Ben Widawsky46ddf192012-09-12 18:12:07 -0700452}
453
Joe Perchesc828a892017-12-19 10:15:08 -0800454static DEVICE_ATTR_RO(gt_act_freq_mhz);
455static DEVICE_ATTR_RO(gt_cur_freq_mhz);
Joe Perchesb6b996b2017-12-19 10:15:07 -0800456static DEVICE_ATTR_RW(gt_boost_freq_mhz);
457static DEVICE_ATTR_RW(gt_max_freq_mhz);
458static DEVICE_ATTR_RW(gt_min_freq_mhz);
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700459
Joe Perchesc828a892017-12-19 10:15:08 -0800460static DEVICE_ATTR_RO(vlv_rpe_freq_mhz);
Ben Widawskyac6ae342012-09-07 19:43:44 -0700461
462static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
463static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
464static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
465static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
466
467/* For now we have a static number of RP states */
468static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
469{
David Weinehall694c2822016-08-22 13:32:43 +0300470 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100471 struct intel_rps *rps = &dev_priv->gt_pm.rps;
Akash Goelbc4d91f2015-02-26 16:09:47 +0530472 u32 val;
Ben Widawskyac6ae342012-09-07 19:43:44 -0700473
Akash Goelbc4d91f2015-02-26 16:09:47 +0530474 if (attr == &dev_attr_gt_RP0_freq_mhz)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100475 val = intel_gpu_freq(dev_priv, rps->rp0_freq);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530476 else if (attr == &dev_attr_gt_RP1_freq_mhz)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100477 val = intel_gpu_freq(dev_priv, rps->rp1_freq);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530478 else if (attr == &dev_attr_gt_RPn_freq_mhz)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100479 val = intel_gpu_freq(dev_priv, rps->min_freq);
Akash Goelbc4d91f2015-02-26 16:09:47 +0530480 else
Ben Widawskyac6ae342012-09-07 19:43:44 -0700481 BUG();
Akash Goelbc4d91f2015-02-26 16:09:47 +0530482
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
Jani Nikulae1215de2018-10-04 17:37:50 +0300486static const struct attribute * const gen6_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200487 &dev_attr_gt_act_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700488 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100489 &dev_attr_gt_boost_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700490 &dev_attr_gt_max_freq_mhz.attr,
491 &dev_attr_gt_min_freq_mhz.attr,
Ben Widawskyac6ae342012-09-07 19:43:44 -0700492 &dev_attr_gt_RP0_freq_mhz.attr,
493 &dev_attr_gt_RP1_freq_mhz.attr,
494 &dev_attr_gt_RPn_freq_mhz.attr,
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700495 NULL,
496};
497
Jani Nikulae1215de2018-10-04 17:37:50 +0300498static const struct attribute * const vlv_attrs[] = {
Ville Syrjäläc8c972e2015-01-23 21:04:24 +0200499 &dev_attr_gt_act_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100500 &dev_attr_gt_cur_freq_mhz.attr,
Chris Wilson29ecd78d2016-07-13 09:10:35 +0100501 &dev_attr_gt_boost_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100502 &dev_attr_gt_max_freq_mhz.attr,
503 &dev_attr_gt_min_freq_mhz.attr,
Deepak S74c4f622014-07-10 13:16:22 +0530504 &dev_attr_gt_RP0_freq_mhz.attr,
505 &dev_attr_gt_RP1_freq_mhz.attr,
506 &dev_attr_gt_RPn_freq_mhz.attr,
Chris Wilson97e4eed2013-08-26 16:18:54 +0100507 &dev_attr_vlv_rpe_freq_mhz.attr,
508 NULL,
509};
510
Chris Wilson98a2f412016-10-12 10:05:18 +0100511#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
512
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300513static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
514 struct bin_attribute *attr, char *buf,
515 loff_t off, size_t count)
516{
517
Geliang Tang657fb5f2016-01-13 22:48:40 +0800518 struct device *kdev = kobj_to_dev(kobj);
Chris Wilson0e390372018-11-23 13:23:25 +0000519 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000520 struct i915_gpu_state *gpu;
521 ssize_t ret;
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300522
Chris Wilson0e390372018-11-23 13:23:25 +0000523 gpu = i915_first_error_state(i915);
Chris Wilsone6154e42018-12-07 11:05:54 +0000524 if (IS_ERR(gpu)) {
525 ret = PTR_ERR(gpu);
526 } else if (gpu) {
Chris Wilson0e390372018-11-23 13:23:25 +0000527 ret = i915_gpu_state_copy_to_buffer(gpu, buf, off, count);
528 i915_gpu_state_put(gpu);
529 } else {
530 const char *str = "No error state collected\n";
531 size_t len = strlen(str);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300532
Chris Wilson0e390372018-11-23 13:23:25 +0000533 ret = min_t(size_t, count, len - off);
534 memcpy(buf, str + off, ret);
535 }
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300536
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000537 return ret;
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300538}
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{
Geliang Tang657fb5f2016-01-13 22:48:40 +0800544 struct device *kdev = kobj_to_dev(kobj);
David Weinehall694c2822016-08-22 13:32:43 +0300545 struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300546
547 DRM_DEBUG_DRIVER("Resetting error state\n");
Chris Wilson5a4c6f12017-02-14 16:46:11 +0000548 i915_reset_error_state(dev_priv);
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300549
550 return count;
551}
552
Bhumika Goyal59f3da12017-08-02 22:50:47 +0530553static const struct bin_attribute error_state_attr = {
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300554 .attr.name = "error",
555 .attr.mode = S_IRUSR | S_IWUSR,
556 .size = 0,
557 .read = error_state_read,
558 .write = error_state_write,
559};
560
Chris Wilson98a2f412016-10-12 10:05:18 +0100561static void i915_setup_error_capture(struct device *kdev)
562{
563 if (sysfs_create_bin_file(&kdev->kobj, &error_state_attr))
564 DRM_ERROR("error_state sysfs setup failed\n");
565}
566
567static void i915_teardown_error_capture(struct device *kdev)
568{
569 sysfs_remove_bin_file(&kdev->kobj, &error_state_attr);
570}
571#else
572static void i915_setup_error_capture(struct device *kdev) {}
573static void i915_teardown_error_capture(struct device *kdev) {}
574#endif
575
David Weinehall694c2822016-08-22 13:32:43 +0300576void i915_setup_sysfs(struct drm_i915_private *dev_priv)
Ben Widawsky0136db52012-04-10 21:17:01 -0700577{
David Weinehall694c2822016-08-22 13:32:43 +0300578 struct device *kdev = dev_priv->drm.primary->kdev;
Ben Widawsky0136db52012-04-10 21:17:01 -0700579 int ret;
580
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700581#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +0300582 if (HAS_RC6(dev_priv)) {
583 ret = sysfs_merge_group(&kdev->kobj,
Daniel Vetter112abd22012-05-31 14:57:43 +0200584 &rc6_attr_group);
585 if (ret)
586 DRM_ERROR("RC6 residency sysfs setup failed\n");
587 }
David Weinehall694c2822016-08-22 13:32:43 +0300588 if (HAS_RC6p(dev_priv)) {
589 ret = sysfs_merge_group(&kdev->kobj,
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -0700590 &rc6p_attr_group);
591 if (ret)
592 DRM_ERROR("RC6p residency sysfs setup failed\n");
593 }
David Weinehall694c2822016-08-22 13:32:43 +0300594 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
595 ret = sysfs_merge_group(&kdev->kobj,
Ville Syrjälä626ad6f2015-02-26 21:10:27 +0530596 &media_rc6_attr_group);
597 if (ret)
598 DRM_ERROR("Media RC6 residency sysfs setup failed\n");
599 }
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700600#endif
David Weinehall694c2822016-08-22 13:32:43 +0300601 if (HAS_L3_DPF(dev_priv)) {
602 ret = device_create_bin_file(kdev, &dpf_attrs);
Daniel Vetter112abd22012-05-31 14:57:43 +0200603 if (ret)
604 DRM_ERROR("l3 parity sysfs setup failed\n");
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700605
David Weinehall694c2822016-08-22 13:32:43 +0300606 if (NUM_L3_SLICES(dev_priv) > 1) {
607 ret = device_create_bin_file(kdev,
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700608 &dpf_attrs_1);
609 if (ret)
610 DRM_ERROR("l3 parity slice 1 setup failed\n");
611 }
Daniel Vetter112abd22012-05-31 14:57:43 +0200612 }
Ben Widawskydf6eedc2012-09-07 19:43:40 -0700613
Chris Wilson97e4eed2013-08-26 16:18:54 +0100614 ret = 0;
David Weinehall694c2822016-08-22 13:32:43 +0300615 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
616 ret = sysfs_create_files(&kdev->kobj, vlv_attrs);
617 else if (INTEL_GEN(dev_priv) >= 6)
618 ret = sysfs_create_files(&kdev->kobj, gen6_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100619 if (ret)
620 DRM_ERROR("RPS sysfs setup failed\n");
Mika Kuoppalaef86ddc2013-06-06 17:38:54 +0300621
Chris Wilson98a2f412016-10-12 10:05:18 +0100622 i915_setup_error_capture(kdev);
Ben Widawsky0136db52012-04-10 21:17:01 -0700623}
624
David Weinehall694c2822016-08-22 13:32:43 +0300625void i915_teardown_sysfs(struct drm_i915_private *dev_priv)
Ben Widawsky0136db52012-04-10 21:17:01 -0700626{
David Weinehall694c2822016-08-22 13:32:43 +0300627 struct device *kdev = dev_priv->drm.primary->kdev;
628
Chris Wilson98a2f412016-10-12 10:05:18 +0100629 i915_teardown_error_capture(kdev);
630
David Weinehall694c2822016-08-22 13:32:43 +0300631 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
632 sysfs_remove_files(&kdev->kobj, vlv_attrs);
Chris Wilson97e4eed2013-08-26 16:18:54 +0100633 else
David Weinehall694c2822016-08-22 13:32:43 +0300634 sysfs_remove_files(&kdev->kobj, gen6_attrs);
635 device_remove_bin_file(kdev, &dpf_attrs_1);
636 device_remove_bin_file(kdev, &dpf_attrs);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700637#ifdef CONFIG_PM
David Weinehall694c2822016-08-22 13:32:43 +0300638 sysfs_unmerge_group(&kdev->kobj, &rc6_attr_group);
639 sysfs_unmerge_group(&kdev->kobj, &rc6p_attr_group);
Ben Widawsky853c70e2012-09-19 10:50:19 -0700640#endif
Ben Widawsky0136db52012-04-10 21:17:01 -0700641}