blob: c701a173bd48bee8f00e08a535b4c73753a88c6c [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
Hunt Xu5ab36332012-07-01 03:45:07 +000035#ifdef CONFIG_PM
Ben Widawsky0136db582012-04-10 21:17:01 -070036static u32 calc_residency(struct drm_device *dev, const u32 reg)
37{
38 struct drm_i915_private *dev_priv = dev->dev_private;
39 u64 raw_time; /* 32b value may overflow during fixed point math */
40
41 if (!intel_enable_rc6(dev))
42 return 0;
43
Ben Widawskya85d4bc2012-04-20 11:50:01 -070044 raw_time = I915_READ(reg) * 128ULL;
45 return DIV_ROUND_UP_ULL(raw_time, 100000);
Ben Widawsky0136db582012-04-10 21:17:01 -070046}
47
48static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070049show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -070050{
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070051 struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev);
Ben Widawsky0136db582012-04-10 21:17:01 -070052 return snprintf(buf, PAGE_SIZE, "%x", intel_enable_rc6(dminor->dev));
53}
54
55static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070056show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -070057{
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070058 struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev);
Ben Widawsky0136db582012-04-10 21:17:01 -070059 u32 rc6_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6);
60 return snprintf(buf, PAGE_SIZE, "%u", rc6_residency);
61}
62
63static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070064show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -070065{
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070066 struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev);
Ben Widawsky0136db582012-04-10 21:17:01 -070067 u32 rc6p_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6p);
68 return snprintf(buf, PAGE_SIZE, "%u", rc6p_residency);
69}
70
71static ssize_t
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070072show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf)
Ben Widawsky0136db582012-04-10 21:17:01 -070073{
Ben Widawskydbdfd8e2012-09-07 19:43:38 -070074 struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev);
Ben Widawsky0136db582012-04-10 21:17:01 -070075 u32 rc6pp_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6pp);
76 return snprintf(buf, PAGE_SIZE, "%u", rc6pp_residency);
77}
78
79static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL);
80static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL);
81static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL);
82static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL);
83
84static struct attribute *rc6_attrs[] = {
85 &dev_attr_rc6_enable.attr,
86 &dev_attr_rc6_residency_ms.attr,
87 &dev_attr_rc6p_residency_ms.attr,
88 &dev_attr_rc6pp_residency_ms.attr,
89 NULL
90};
91
92static struct attribute_group rc6_attr_group = {
93 .name = power_group_name,
94 .attrs = rc6_attrs
95};
Ben Widawsky8c3f9292012-09-02 00:24:40 -070096#endif
Ben Widawsky0136db582012-04-10 21:17:01 -070097
Ben Widawsky84bc7582012-05-25 16:56:25 -070098static int l3_access_valid(struct drm_device *dev, loff_t offset)
99{
100 if (!IS_IVYBRIDGE(dev))
101 return -EPERM;
102
103 if (offset % 4 != 0)
104 return -EINVAL;
105
106 if (offset >= GEN7_L3LOG_SIZE)
107 return -ENXIO;
108
109 return 0;
110}
111
112static ssize_t
113i915_l3_read(struct file *filp, struct kobject *kobj,
114 struct bin_attribute *attr, char *buf,
115 loff_t offset, size_t count)
116{
117 struct device *dev = container_of(kobj, struct device, kobj);
118 struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev);
119 struct drm_device *drm_dev = dminor->dev;
120 struct drm_i915_private *dev_priv = drm_dev->dev_private;
121 uint32_t misccpctl;
122 int i, ret;
123
124 ret = l3_access_valid(drm_dev, offset);
125 if (ret)
126 return ret;
127
128 ret = i915_mutex_lock_interruptible(drm_dev);
129 if (ret)
130 return ret;
131
132 misccpctl = I915_READ(GEN7_MISCCPCTL);
133 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
134
135 for (i = offset; count >= 4 && i < GEN7_L3LOG_SIZE; i += 4, count -= 4)
136 *((uint32_t *)(&buf[i])) = I915_READ(GEN7_L3LOG_BASE + i);
137
138 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
139
140 mutex_unlock(&drm_dev->struct_mutex);
141
142 return i - offset;
143}
144
145static ssize_t
146i915_l3_write(struct file *filp, struct kobject *kobj,
147 struct bin_attribute *attr, char *buf,
148 loff_t offset, size_t count)
149{
150 struct device *dev = container_of(kobj, struct device, kobj);
151 struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev);
152 struct drm_device *drm_dev = dminor->dev;
153 struct drm_i915_private *dev_priv = drm_dev->dev_private;
154 u32 *temp = NULL; /* Just here to make handling failures easy */
155 int ret;
156
157 ret = l3_access_valid(drm_dev, offset);
158 if (ret)
159 return ret;
160
161 ret = i915_mutex_lock_interruptible(drm_dev);
162 if (ret)
163 return ret;
164
165 if (!dev_priv->mm.l3_remap_info) {
166 temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
167 if (!temp) {
168 mutex_unlock(&drm_dev->struct_mutex);
169 return -ENOMEM;
170 }
171 }
172
173 ret = i915_gpu_idle(drm_dev);
174 if (ret) {
175 kfree(temp);
176 mutex_unlock(&drm_dev->struct_mutex);
177 return ret;
178 }
179
180 /* TODO: Ideally we really want a GPU reset here to make sure errors
181 * aren't propagated. Since I cannot find a stable way to reset the GPU
182 * at this point it is left as a TODO.
183 */
184 if (temp)
185 dev_priv->mm.l3_remap_info = temp;
186
187 memcpy(dev_priv->mm.l3_remap_info + (offset/4),
188 buf + (offset/4),
189 count);
190
191 i915_gem_l3_remap(drm_dev);
192
193 mutex_unlock(&drm_dev->struct_mutex);
194
195 return count;
196}
197
198static struct bin_attribute dpf_attrs = {
199 .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
200 .size = GEN7_L3LOG_SIZE,
201 .read = i915_l3_read,
202 .write = i915_l3_write,
203 .mmap = NULL
204};
205
Ben Widawsky0136db582012-04-10 21:17:01 -0700206void i915_setup_sysfs(struct drm_device *dev)
207{
208 int ret;
209
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700210#ifdef CONFIG_PM
Daniel Vetter112abd22012-05-31 14:57:43 +0200211 if (INTEL_INFO(dev)->gen >= 6) {
212 ret = sysfs_merge_group(&dev->primary->kdev.kobj,
213 &rc6_attr_group);
214 if (ret)
215 DRM_ERROR("RC6 residency sysfs setup failed\n");
216 }
Ben Widawsky8c3f9292012-09-02 00:24:40 -0700217#endif
Ben Widawskye1ef7cc2012-07-24 20:47:31 -0700218 if (HAS_L3_GPU_CACHE(dev)) {
Daniel Vetter112abd22012-05-31 14:57:43 +0200219 ret = device_create_bin_file(&dev->primary->kdev, &dpf_attrs);
220 if (ret)
221 DRM_ERROR("l3 parity sysfs setup failed\n");
222 }
Ben Widawsky0136db582012-04-10 21:17:01 -0700223}
224
225void i915_teardown_sysfs(struct drm_device *dev)
226{
Ben Widawsky84bc7582012-05-25 16:56:25 -0700227 device_remove_bin_file(&dev->primary->kdev, &dpf_attrs);
Ben Widawsky0136db582012-04-10 21:17:01 -0700228 sysfs_unmerge_group(&dev->primary->kdev.kobj, &rc6_attr_group);
229}