Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 32 | #include "intel_drv.h" |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 33 | #include "i915_drv.h" |
| 34 | |
Hunt Xu | 5ab3633 | 2012-07-01 03:45:07 +0000 | [diff] [blame] | 35 | #ifdef CONFIG_PM |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 36 | static 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 */ |
Jesse Barnes | e454a05 | 2013-09-26 17:55:58 -0700 | [diff] [blame] | 40 | u64 units = 128ULL, div = 100000ULL, bias = 100ULL; |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 41 | |
| 42 | if (!intel_enable_rc6(dev)) |
| 43 | return 0; |
| 44 | |
Jesse Barnes | e454a05 | 2013-09-26 17:55:58 -0700 | [diff] [blame] | 45 | /* On VLV, residency time is in CZ units rather than 1.28us */ |
| 46 | if (IS_VALLEYVIEW(dev)) { |
| 47 | u32 clkctl2; |
| 48 | |
| 49 | clkctl2 = I915_READ(VLV_CLK_CTL2) >> |
| 50 | CLK_CTL2_CZCOUNT_30NS_SHIFT; |
| 51 | if (!clkctl2) { |
| 52 | WARN(!clkctl2, "bogus CZ count value"); |
| 53 | return 0; |
| 54 | } |
| 55 | units = DIV_ROUND_UP_ULL(30ULL * bias, (u64)clkctl2); |
| 56 | if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH) |
| 57 | units <<= 8; |
| 58 | |
| 59 | div = 1000000ULL * bias; |
| 60 | } |
| 61 | |
| 62 | raw_time = I915_READ(reg) * units; |
| 63 | return DIV_ROUND_UP_ULL(raw_time, div); |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static ssize_t |
Ben Widawsky | dbdfd8e | 2012-09-07 19:43:38 -0700 | [diff] [blame] | 67 | show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf) |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 68 | { |
Ben Widawsky | dbdfd8e | 2012-09-07 19:43:38 -0700 | [diff] [blame] | 69 | struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev); |
Jani Nikula | 3e2a155 | 2013-02-14 10:42:11 +0200 | [diff] [blame] | 70 | return snprintf(buf, PAGE_SIZE, "%x\n", intel_enable_rc6(dminor->dev)); |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static ssize_t |
Ben Widawsky | dbdfd8e | 2012-09-07 19:43:38 -0700 | [diff] [blame] | 74 | show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf) |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 75 | { |
Ben Widawsky | dbdfd8e | 2012-09-07 19:43:38 -0700 | [diff] [blame] | 76 | struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev); |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 77 | u32 rc6_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6); |
Jani Nikula | 3e2a155 | 2013-02-14 10:42:11 +0200 | [diff] [blame] | 78 | return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency); |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static ssize_t |
Ben Widawsky | dbdfd8e | 2012-09-07 19:43:38 -0700 | [diff] [blame] | 82 | show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf) |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 83 | { |
Ben Widawsky | dbdfd8e | 2012-09-07 19:43:38 -0700 | [diff] [blame] | 84 | struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev); |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 85 | u32 rc6p_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6p); |
Jesse Barnes | 5ffd494 | 2013-09-11 13:43:20 -0700 | [diff] [blame] | 86 | if (IS_VALLEYVIEW(dminor->dev)) |
| 87 | rc6p_residency = 0; |
Jani Nikula | 3e2a155 | 2013-02-14 10:42:11 +0200 | [diff] [blame] | 88 | return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency); |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static ssize_t |
Ben Widawsky | dbdfd8e | 2012-09-07 19:43:38 -0700 | [diff] [blame] | 92 | show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf) |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 93 | { |
Ben Widawsky | dbdfd8e | 2012-09-07 19:43:38 -0700 | [diff] [blame] | 94 | struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev); |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 95 | u32 rc6pp_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6pp); |
Jesse Barnes | 5ffd494 | 2013-09-11 13:43:20 -0700 | [diff] [blame] | 96 | if (IS_VALLEYVIEW(dminor->dev)) |
| 97 | rc6pp_residency = 0; |
Jani Nikula | 3e2a155 | 2013-02-14 10:42:11 +0200 | [diff] [blame] | 98 | return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency); |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL); |
| 102 | static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL); |
| 103 | static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL); |
| 104 | static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL); |
| 105 | |
| 106 | static struct attribute *rc6_attrs[] = { |
| 107 | &dev_attr_rc6_enable.attr, |
| 108 | &dev_attr_rc6_residency_ms.attr, |
| 109 | &dev_attr_rc6p_residency_ms.attr, |
| 110 | &dev_attr_rc6pp_residency_ms.attr, |
| 111 | NULL |
| 112 | }; |
| 113 | |
| 114 | static struct attribute_group rc6_attr_group = { |
| 115 | .name = power_group_name, |
| 116 | .attrs = rc6_attrs |
| 117 | }; |
Ben Widawsky | 8c3f929 | 2012-09-02 00:24:40 -0700 | [diff] [blame] | 118 | #endif |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 119 | |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 120 | static int l3_access_valid(struct drm_device *dev, loff_t offset) |
| 121 | { |
Ben Widawsky | 040d2ba | 2013-09-19 11:01:40 -0700 | [diff] [blame] | 122 | if (!HAS_L3_DPF(dev)) |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 123 | return -EPERM; |
| 124 | |
| 125 | if (offset % 4 != 0) |
| 126 | return -EINVAL; |
| 127 | |
| 128 | if (offset >= GEN7_L3LOG_SIZE) |
| 129 | return -ENXIO; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static ssize_t |
| 135 | i915_l3_read(struct file *filp, struct kobject *kobj, |
| 136 | struct bin_attribute *attr, char *buf, |
| 137 | loff_t offset, size_t count) |
| 138 | { |
| 139 | struct device *dev = container_of(kobj, struct device, kobj); |
| 140 | struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); |
| 141 | struct drm_device *drm_dev = dminor->dev; |
| 142 | struct drm_i915_private *dev_priv = drm_dev->dev_private; |
Ben Widawsky | 35a85ac | 2013-09-19 11:13:41 -0700 | [diff] [blame] | 143 | int slice = (int)(uintptr_t)attr->private; |
Ben Widawsky | 3ccfd19 | 2013-09-18 19:03:18 -0700 | [diff] [blame] | 144 | int ret; |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 145 | |
Ben Widawsky | 1c3dcd1 | 2013-09-12 22:28:28 -0700 | [diff] [blame] | 146 | count = round_down(count, 4); |
| 147 | |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 148 | ret = l3_access_valid(drm_dev, offset); |
| 149 | if (ret) |
| 150 | return ret; |
| 151 | |
Dan Carpenter | e5ad402 | 2013-09-20 14:20:18 +0300 | [diff] [blame] | 152 | count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count); |
Ben Widawsky | 33618ea | 2013-09-12 22:28:29 -0700 | [diff] [blame] | 153 | |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 154 | ret = i915_mutex_lock_interruptible(drm_dev); |
| 155 | if (ret) |
| 156 | return ret; |
| 157 | |
Ben Widawsky | 3ccfd19 | 2013-09-18 19:03:18 -0700 | [diff] [blame] | 158 | if (dev_priv->l3_parity.remap_info[slice]) |
| 159 | memcpy(buf, |
| 160 | dev_priv->l3_parity.remap_info[slice] + (offset/4), |
| 161 | count); |
| 162 | else |
| 163 | memset(buf, 0, count); |
Ben Widawsky | 1c966dd | 2013-09-17 21:12:42 -0700 | [diff] [blame] | 164 | |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 165 | mutex_unlock(&drm_dev->struct_mutex); |
| 166 | |
Ben Widawsky | 1c966dd | 2013-09-17 21:12:42 -0700 | [diff] [blame] | 167 | return count; |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static ssize_t |
| 171 | i915_l3_write(struct file *filp, struct kobject *kobj, |
| 172 | struct bin_attribute *attr, char *buf, |
| 173 | loff_t offset, size_t count) |
| 174 | { |
| 175 | struct device *dev = container_of(kobj, struct device, kobj); |
| 176 | struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); |
| 177 | struct drm_device *drm_dev = dminor->dev; |
| 178 | struct drm_i915_private *dev_priv = drm_dev->dev_private; |
Ben Widawsky | 3ccfd19 | 2013-09-18 19:03:18 -0700 | [diff] [blame] | 179 | struct i915_hw_context *ctx; |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 180 | u32 *temp = NULL; /* Just here to make handling failures easy */ |
Ben Widawsky | 35a85ac | 2013-09-19 11:13:41 -0700 | [diff] [blame] | 181 | int slice = (int)(uintptr_t)attr->private; |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 182 | int ret; |
| 183 | |
| 184 | ret = l3_access_valid(drm_dev, offset); |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | |
Ben Widawsky | 3ccfd19 | 2013-09-18 19:03:18 -0700 | [diff] [blame] | 188 | if (dev_priv->hw_contexts_disabled) |
| 189 | return -ENXIO; |
| 190 | |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 191 | ret = i915_mutex_lock_interruptible(drm_dev); |
| 192 | if (ret) |
| 193 | return ret; |
| 194 | |
Ben Widawsky | 35a85ac | 2013-09-19 11:13:41 -0700 | [diff] [blame] | 195 | if (!dev_priv->l3_parity.remap_info[slice]) { |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 196 | temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL); |
| 197 | if (!temp) { |
| 198 | mutex_unlock(&drm_dev->struct_mutex); |
| 199 | return -ENOMEM; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | ret = i915_gpu_idle(drm_dev); |
| 204 | if (ret) { |
| 205 | kfree(temp); |
| 206 | mutex_unlock(&drm_dev->struct_mutex); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | /* TODO: Ideally we really want a GPU reset here to make sure errors |
| 211 | * aren't propagated. Since I cannot find a stable way to reset the GPU |
| 212 | * at this point it is left as a TODO. |
| 213 | */ |
| 214 | if (temp) |
Ben Widawsky | 35a85ac | 2013-09-19 11:13:41 -0700 | [diff] [blame] | 215 | dev_priv->l3_parity.remap_info[slice] = temp; |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 216 | |
Ben Widawsky | 35a85ac | 2013-09-19 11:13:41 -0700 | [diff] [blame] | 217 | memcpy(dev_priv->l3_parity.remap_info[slice] + (offset/4), buf, count); |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 218 | |
Ben Widawsky | 3ccfd19 | 2013-09-18 19:03:18 -0700 | [diff] [blame] | 219 | /* NB: We defer the remapping until we switch to the context */ |
| 220 | list_for_each_entry(ctx, &dev_priv->context_list, link) |
| 221 | ctx->remap_slice |= (1<<slice); |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 222 | |
| 223 | mutex_unlock(&drm_dev->struct_mutex); |
| 224 | |
| 225 | return count; |
| 226 | } |
| 227 | |
| 228 | static struct bin_attribute dpf_attrs = { |
| 229 | .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)}, |
| 230 | .size = GEN7_L3LOG_SIZE, |
| 231 | .read = i915_l3_read, |
| 232 | .write = i915_l3_write, |
Ben Widawsky | 35a85ac | 2013-09-19 11:13:41 -0700 | [diff] [blame] | 233 | .mmap = NULL, |
| 234 | .private = (void *)0 |
| 235 | }; |
| 236 | |
| 237 | static struct bin_attribute dpf_attrs_1 = { |
| 238 | .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)}, |
| 239 | .size = GEN7_L3LOG_SIZE, |
| 240 | .read = i915_l3_read, |
| 241 | .write = i915_l3_write, |
| 242 | .mmap = NULL, |
| 243 | .private = (void *)1 |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 246 | static ssize_t gt_cur_freq_mhz_show(struct device *kdev, |
| 247 | struct device_attribute *attr, char *buf) |
| 248 | { |
| 249 | struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev); |
| 250 | struct drm_device *dev = minor->dev; |
| 251 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 252 | int ret; |
| 253 | |
Tom O'Rourke | 5c9669c | 2013-09-16 14:56:43 -0700 | [diff] [blame] | 254 | flush_delayed_work(&dev_priv->rps.delayed_resume_work); |
| 255 | |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 256 | mutex_lock(&dev_priv->rps.hw_lock); |
Jesse Barnes | 177006a | 2013-05-02 10:48:07 -0700 | [diff] [blame] | 257 | if (IS_VALLEYVIEW(dev_priv->dev)) { |
| 258 | u32 freq; |
Jani Nikula | 6493625 | 2013-05-22 15:36:20 +0300 | [diff] [blame] | 259 | freq = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS); |
Ville Syrjälä | 2ec3815 | 2013-11-05 22:42:29 +0200 | [diff] [blame^] | 260 | ret = vlv_gpu_freq(dev_priv, (freq >> 8) & 0xff); |
Jesse Barnes | 177006a | 2013-05-02 10:48:07 -0700 | [diff] [blame] | 261 | } else { |
Jesse Barnes | 0a073b8 | 2013-04-17 15:54:58 -0700 | [diff] [blame] | 262 | ret = dev_priv->rps.cur_delay * GT_FREQUENCY_MULTIPLIER; |
Jesse Barnes | 177006a | 2013-05-02 10:48:07 -0700 | [diff] [blame] | 263 | } |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 264 | mutex_unlock(&dev_priv->rps.hw_lock); |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 265 | |
Jani Nikula | 3e2a155 | 2013-02-14 10:42:11 +0200 | [diff] [blame] | 266 | return snprintf(buf, PAGE_SIZE, "%d\n", ret); |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Chris Wilson | 97e4eed | 2013-08-26 16:18:54 +0100 | [diff] [blame] | 269 | static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev, |
| 270 | struct device_attribute *attr, char *buf) |
| 271 | { |
| 272 | struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev); |
| 273 | struct drm_device *dev = minor->dev; |
| 274 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 275 | |
| 276 | return snprintf(buf, PAGE_SIZE, "%d\n", |
Ville Syrjälä | 2ec3815 | 2013-11-05 22:42:29 +0200 | [diff] [blame^] | 277 | vlv_gpu_freq(dev_priv, dev_priv->rps.rpe_delay)); |
Chris Wilson | 97e4eed | 2013-08-26 16:18:54 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 280 | static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf) |
| 281 | { |
| 282 | struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev); |
| 283 | struct drm_device *dev = minor->dev; |
| 284 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 285 | int ret; |
| 286 | |
Tom O'Rourke | 5c9669c | 2013-09-16 14:56:43 -0700 | [diff] [blame] | 287 | flush_delayed_work(&dev_priv->rps.delayed_resume_work); |
| 288 | |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 289 | mutex_lock(&dev_priv->rps.hw_lock); |
Jesse Barnes | 0a073b8 | 2013-04-17 15:54:58 -0700 | [diff] [blame] | 290 | if (IS_VALLEYVIEW(dev_priv->dev)) |
Ville Syrjälä | 2ec3815 | 2013-11-05 22:42:29 +0200 | [diff] [blame^] | 291 | ret = vlv_gpu_freq(dev_priv, dev_priv->rps.max_delay); |
Jesse Barnes | 0a073b8 | 2013-04-17 15:54:58 -0700 | [diff] [blame] | 292 | else |
| 293 | ret = dev_priv->rps.max_delay * GT_FREQUENCY_MULTIPLIER; |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 294 | mutex_unlock(&dev_priv->rps.hw_lock); |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 295 | |
Jani Nikula | 3e2a155 | 2013-02-14 10:42:11 +0200 | [diff] [blame] | 296 | return snprintf(buf, PAGE_SIZE, "%d\n", ret); |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 299 | static ssize_t gt_max_freq_mhz_store(struct device *kdev, |
| 300 | struct device_attribute *attr, |
| 301 | const char *buf, size_t count) |
| 302 | { |
| 303 | struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev); |
| 304 | struct drm_device *dev = minor->dev; |
| 305 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 31c7738 | 2013-04-05 14:29:22 -0700 | [diff] [blame] | 306 | u32 val, rp_state_cap, hw_max, hw_min, non_oc_max; |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 307 | ssize_t ret; |
| 308 | |
| 309 | ret = kstrtou32(buf, 0, &val); |
| 310 | if (ret) |
| 311 | return ret; |
| 312 | |
Tom O'Rourke | 5c9669c | 2013-09-16 14:56:43 -0700 | [diff] [blame] | 313 | flush_delayed_work(&dev_priv->rps.delayed_resume_work); |
| 314 | |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 315 | mutex_lock(&dev_priv->rps.hw_lock); |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 316 | |
Jesse Barnes | 0a073b8 | 2013-04-17 15:54:58 -0700 | [diff] [blame] | 317 | if (IS_VALLEYVIEW(dev_priv->dev)) { |
Ville Syrjälä | 2ec3815 | 2013-11-05 22:42:29 +0200 | [diff] [blame^] | 318 | val = vlv_freq_opcode(dev_priv, val); |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 319 | |
Jesse Barnes | 0a073b8 | 2013-04-17 15:54:58 -0700 | [diff] [blame] | 320 | hw_max = valleyview_rps_max_freq(dev_priv); |
| 321 | hw_min = valleyview_rps_min_freq(dev_priv); |
| 322 | non_oc_max = hw_max; |
| 323 | } else { |
| 324 | val /= GT_FREQUENCY_MULTIPLIER; |
| 325 | |
| 326 | rp_state_cap = I915_READ(GEN6_RP_STATE_CAP); |
| 327 | hw_max = dev_priv->rps.hw_max; |
| 328 | non_oc_max = (rp_state_cap & 0xff); |
| 329 | hw_min = ((rp_state_cap & 0xff0000) >> 16); |
| 330 | } |
| 331 | |
| 332 | if (val < hw_min || val > hw_max || |
| 333 | val < dev_priv->rps.min_delay) { |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 334 | mutex_unlock(&dev_priv->rps.hw_lock); |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 335 | return -EINVAL; |
| 336 | } |
| 337 | |
Ben Widawsky | 31c7738 | 2013-04-05 14:29:22 -0700 | [diff] [blame] | 338 | if (val > non_oc_max) |
| 339 | DRM_DEBUG("User requested overclocking to %d\n", |
| 340 | val * GT_FREQUENCY_MULTIPLIER); |
| 341 | |
Jesse Barnes | 0a073b8 | 2013-04-17 15:54:58 -0700 | [diff] [blame] | 342 | if (dev_priv->rps.cur_delay > val) { |
| 343 | if (IS_VALLEYVIEW(dev_priv->dev)) |
| 344 | valleyview_set_rps(dev_priv->dev, val); |
| 345 | else |
| 346 | gen6_set_rps(dev_priv->dev, val); |
| 347 | } |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 348 | |
| 349 | dev_priv->rps.max_delay = val; |
| 350 | |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 351 | mutex_unlock(&dev_priv->rps.hw_lock); |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 352 | |
| 353 | return count; |
| 354 | } |
| 355 | |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 356 | static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf) |
| 357 | { |
| 358 | struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev); |
| 359 | struct drm_device *dev = minor->dev; |
| 360 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 361 | int ret; |
| 362 | |
Tom O'Rourke | 5c9669c | 2013-09-16 14:56:43 -0700 | [diff] [blame] | 363 | flush_delayed_work(&dev_priv->rps.delayed_resume_work); |
| 364 | |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 365 | mutex_lock(&dev_priv->rps.hw_lock); |
Jesse Barnes | 0a073b8 | 2013-04-17 15:54:58 -0700 | [diff] [blame] | 366 | if (IS_VALLEYVIEW(dev_priv->dev)) |
Ville Syrjälä | 2ec3815 | 2013-11-05 22:42:29 +0200 | [diff] [blame^] | 367 | ret = vlv_gpu_freq(dev_priv, dev_priv->rps.min_delay); |
Jesse Barnes | 0a073b8 | 2013-04-17 15:54:58 -0700 | [diff] [blame] | 368 | else |
| 369 | ret = dev_priv->rps.min_delay * GT_FREQUENCY_MULTIPLIER; |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 370 | mutex_unlock(&dev_priv->rps.hw_lock); |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 371 | |
Jani Nikula | 3e2a155 | 2013-02-14 10:42:11 +0200 | [diff] [blame] | 372 | return snprintf(buf, PAGE_SIZE, "%d\n", ret); |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 375 | static ssize_t gt_min_freq_mhz_store(struct device *kdev, |
| 376 | struct device_attribute *attr, |
| 377 | const char *buf, size_t count) |
| 378 | { |
| 379 | struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev); |
| 380 | struct drm_device *dev = minor->dev; |
| 381 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 382 | u32 val, rp_state_cap, hw_max, hw_min; |
| 383 | ssize_t ret; |
| 384 | |
| 385 | ret = kstrtou32(buf, 0, &val); |
| 386 | if (ret) |
| 387 | return ret; |
| 388 | |
Tom O'Rourke | 5c9669c | 2013-09-16 14:56:43 -0700 | [diff] [blame] | 389 | flush_delayed_work(&dev_priv->rps.delayed_resume_work); |
| 390 | |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 391 | mutex_lock(&dev_priv->rps.hw_lock); |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 392 | |
Jesse Barnes | 0a073b8 | 2013-04-17 15:54:58 -0700 | [diff] [blame] | 393 | if (IS_VALLEYVIEW(dev)) { |
Ville Syrjälä | 2ec3815 | 2013-11-05 22:42:29 +0200 | [diff] [blame^] | 394 | val = vlv_freq_opcode(dev_priv, val); |
Jesse Barnes | 0a073b8 | 2013-04-17 15:54:58 -0700 | [diff] [blame] | 395 | |
| 396 | hw_max = valleyview_rps_max_freq(dev_priv); |
| 397 | hw_min = valleyview_rps_min_freq(dev_priv); |
| 398 | } else { |
| 399 | val /= GT_FREQUENCY_MULTIPLIER; |
| 400 | |
| 401 | rp_state_cap = I915_READ(GEN6_RP_STATE_CAP); |
| 402 | hw_max = dev_priv->rps.hw_max; |
| 403 | hw_min = ((rp_state_cap & 0xff0000) >> 16); |
| 404 | } |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 405 | |
| 406 | if (val < hw_min || val > hw_max || val > dev_priv->rps.max_delay) { |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 407 | mutex_unlock(&dev_priv->rps.hw_lock); |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 408 | return -EINVAL; |
| 409 | } |
| 410 | |
Jesse Barnes | 0a073b8 | 2013-04-17 15:54:58 -0700 | [diff] [blame] | 411 | if (dev_priv->rps.cur_delay < val) { |
| 412 | if (IS_VALLEYVIEW(dev)) |
| 413 | valleyview_set_rps(dev, val); |
| 414 | else |
| 415 | gen6_set_rps(dev_priv->dev, val); |
| 416 | } |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 417 | |
| 418 | dev_priv->rps.min_delay = val; |
| 419 | |
Jesse Barnes | 4fc688c | 2012-11-02 11:14:01 -0700 | [diff] [blame] | 420 | mutex_unlock(&dev_priv->rps.hw_lock); |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 421 | |
| 422 | return count; |
| 423 | |
| 424 | } |
| 425 | |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 426 | static DEVICE_ATTR(gt_cur_freq_mhz, S_IRUGO, gt_cur_freq_mhz_show, NULL); |
Ben Widawsky | 46ddf19 | 2012-09-12 18:12:07 -0700 | [diff] [blame] | 427 | static DEVICE_ATTR(gt_max_freq_mhz, S_IRUGO | S_IWUSR, gt_max_freq_mhz_show, gt_max_freq_mhz_store); |
| 428 | static DEVICE_ATTR(gt_min_freq_mhz, S_IRUGO | S_IWUSR, gt_min_freq_mhz_show, gt_min_freq_mhz_store); |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 429 | |
Chris Wilson | 97e4eed | 2013-08-26 16:18:54 +0100 | [diff] [blame] | 430 | static DEVICE_ATTR(vlv_rpe_freq_mhz, S_IRUGO, vlv_rpe_freq_mhz_show, NULL); |
Ben Widawsky | ac6ae34 | 2012-09-07 19:43:44 -0700 | [diff] [blame] | 431 | |
| 432 | static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf); |
| 433 | static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL); |
| 434 | static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL); |
| 435 | static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL); |
| 436 | |
| 437 | /* For now we have a static number of RP states */ |
| 438 | static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf) |
| 439 | { |
| 440 | struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev); |
| 441 | struct drm_device *dev = minor->dev; |
| 442 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 443 | u32 val, rp_state_cap; |
| 444 | ssize_t ret; |
| 445 | |
| 446 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 447 | if (ret) |
| 448 | return ret; |
| 449 | rp_state_cap = I915_READ(GEN6_RP_STATE_CAP); |
| 450 | mutex_unlock(&dev->struct_mutex); |
| 451 | |
| 452 | if (attr == &dev_attr_gt_RP0_freq_mhz) { |
| 453 | val = ((rp_state_cap & 0x0000ff) >> 0) * GT_FREQUENCY_MULTIPLIER; |
| 454 | } else if (attr == &dev_attr_gt_RP1_freq_mhz) { |
| 455 | val = ((rp_state_cap & 0x00ff00) >> 8) * GT_FREQUENCY_MULTIPLIER; |
| 456 | } else if (attr == &dev_attr_gt_RPn_freq_mhz) { |
| 457 | val = ((rp_state_cap & 0xff0000) >> 16) * GT_FREQUENCY_MULTIPLIER; |
| 458 | } else { |
| 459 | BUG(); |
| 460 | } |
Jani Nikula | 3e2a155 | 2013-02-14 10:42:11 +0200 | [diff] [blame] | 461 | return snprintf(buf, PAGE_SIZE, "%d\n", val); |
Ben Widawsky | ac6ae34 | 2012-09-07 19:43:44 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 464 | static const struct attribute *gen6_attrs[] = { |
| 465 | &dev_attr_gt_cur_freq_mhz.attr, |
| 466 | &dev_attr_gt_max_freq_mhz.attr, |
| 467 | &dev_attr_gt_min_freq_mhz.attr, |
Ben Widawsky | ac6ae34 | 2012-09-07 19:43:44 -0700 | [diff] [blame] | 468 | &dev_attr_gt_RP0_freq_mhz.attr, |
| 469 | &dev_attr_gt_RP1_freq_mhz.attr, |
| 470 | &dev_attr_gt_RPn_freq_mhz.attr, |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 471 | NULL, |
| 472 | }; |
| 473 | |
Chris Wilson | 97e4eed | 2013-08-26 16:18:54 +0100 | [diff] [blame] | 474 | static const struct attribute *vlv_attrs[] = { |
| 475 | &dev_attr_gt_cur_freq_mhz.attr, |
| 476 | &dev_attr_gt_max_freq_mhz.attr, |
| 477 | &dev_attr_gt_min_freq_mhz.attr, |
| 478 | &dev_attr_vlv_rpe_freq_mhz.attr, |
| 479 | NULL, |
| 480 | }; |
| 481 | |
Mika Kuoppala | ef86ddc | 2013-06-06 17:38:54 +0300 | [diff] [blame] | 482 | static ssize_t error_state_read(struct file *filp, struct kobject *kobj, |
| 483 | struct bin_attribute *attr, char *buf, |
| 484 | loff_t off, size_t count) |
| 485 | { |
| 486 | |
| 487 | struct device *kdev = container_of(kobj, struct device, kobj); |
| 488 | struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev); |
| 489 | struct drm_device *dev = minor->dev; |
| 490 | struct i915_error_state_file_priv error_priv; |
| 491 | struct drm_i915_error_state_buf error_str; |
| 492 | ssize_t ret_count = 0; |
| 493 | int ret; |
| 494 | |
| 495 | memset(&error_priv, 0, sizeof(error_priv)); |
| 496 | |
| 497 | ret = i915_error_state_buf_init(&error_str, count, off); |
| 498 | if (ret) |
| 499 | return ret; |
| 500 | |
| 501 | error_priv.dev = dev; |
| 502 | i915_error_state_get(dev, &error_priv); |
| 503 | |
| 504 | ret = i915_error_state_to_str(&error_str, &error_priv); |
| 505 | if (ret) |
| 506 | goto out; |
| 507 | |
| 508 | ret_count = count < error_str.bytes ? count : error_str.bytes; |
| 509 | |
| 510 | memcpy(buf, error_str.buf, ret_count); |
| 511 | out: |
| 512 | i915_error_state_put(&error_priv); |
| 513 | i915_error_state_buf_release(&error_str); |
| 514 | |
| 515 | return ret ?: ret_count; |
| 516 | } |
| 517 | |
| 518 | static ssize_t error_state_write(struct file *file, struct kobject *kobj, |
| 519 | struct bin_attribute *attr, char *buf, |
| 520 | loff_t off, size_t count) |
| 521 | { |
| 522 | struct device *kdev = container_of(kobj, struct device, kobj); |
| 523 | struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev); |
| 524 | struct drm_device *dev = minor->dev; |
| 525 | int ret; |
| 526 | |
| 527 | DRM_DEBUG_DRIVER("Resetting error state\n"); |
| 528 | |
| 529 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 530 | if (ret) |
| 531 | return ret; |
| 532 | |
| 533 | i915_destroy_error_state(dev); |
| 534 | mutex_unlock(&dev->struct_mutex); |
| 535 | |
| 536 | return count; |
| 537 | } |
| 538 | |
| 539 | static struct bin_attribute error_state_attr = { |
| 540 | .attr.name = "error", |
| 541 | .attr.mode = S_IRUSR | S_IWUSR, |
| 542 | .size = 0, |
| 543 | .read = error_state_read, |
| 544 | .write = error_state_write, |
| 545 | }; |
| 546 | |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 547 | void i915_setup_sysfs(struct drm_device *dev) |
| 548 | { |
| 549 | int ret; |
| 550 | |
Ben Widawsky | 8c3f929 | 2012-09-02 00:24:40 -0700 | [diff] [blame] | 551 | #ifdef CONFIG_PM |
Daniel Vetter | 112abd2 | 2012-05-31 14:57:43 +0200 | [diff] [blame] | 552 | if (INTEL_INFO(dev)->gen >= 6) { |
| 553 | ret = sysfs_merge_group(&dev->primary->kdev.kobj, |
| 554 | &rc6_attr_group); |
| 555 | if (ret) |
| 556 | DRM_ERROR("RC6 residency sysfs setup failed\n"); |
| 557 | } |
Ben Widawsky | 8c3f929 | 2012-09-02 00:24:40 -0700 | [diff] [blame] | 558 | #endif |
Ben Widawsky | 040d2ba | 2013-09-19 11:01:40 -0700 | [diff] [blame] | 559 | if (HAS_L3_DPF(dev)) { |
Daniel Vetter | 112abd2 | 2012-05-31 14:57:43 +0200 | [diff] [blame] | 560 | ret = device_create_bin_file(&dev->primary->kdev, &dpf_attrs); |
| 561 | if (ret) |
| 562 | DRM_ERROR("l3 parity sysfs setup failed\n"); |
Ben Widawsky | 35a85ac | 2013-09-19 11:13:41 -0700 | [diff] [blame] | 563 | |
| 564 | if (NUM_L3_SLICES(dev) > 1) { |
| 565 | ret = device_create_bin_file(&dev->primary->kdev, |
| 566 | &dpf_attrs_1); |
| 567 | if (ret) |
| 568 | DRM_ERROR("l3 parity slice 1 setup failed\n"); |
| 569 | } |
Daniel Vetter | 112abd2 | 2012-05-31 14:57:43 +0200 | [diff] [blame] | 570 | } |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 571 | |
Chris Wilson | 97e4eed | 2013-08-26 16:18:54 +0100 | [diff] [blame] | 572 | ret = 0; |
| 573 | if (IS_VALLEYVIEW(dev)) |
| 574 | ret = sysfs_create_files(&dev->primary->kdev.kobj, vlv_attrs); |
| 575 | else if (INTEL_INFO(dev)->gen >= 6) |
Ben Widawsky | df6eedc | 2012-09-07 19:43:40 -0700 | [diff] [blame] | 576 | ret = sysfs_create_files(&dev->primary->kdev.kobj, gen6_attrs); |
Chris Wilson | 97e4eed | 2013-08-26 16:18:54 +0100 | [diff] [blame] | 577 | if (ret) |
| 578 | DRM_ERROR("RPS sysfs setup failed\n"); |
Mika Kuoppala | ef86ddc | 2013-06-06 17:38:54 +0300 | [diff] [blame] | 579 | |
| 580 | ret = sysfs_create_bin_file(&dev->primary->kdev.kobj, |
| 581 | &error_state_attr); |
| 582 | if (ret) |
| 583 | DRM_ERROR("error_state sysfs setup failed\n"); |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void i915_teardown_sysfs(struct drm_device *dev) |
| 587 | { |
Mika Kuoppala | ef86ddc | 2013-06-06 17:38:54 +0300 | [diff] [blame] | 588 | sysfs_remove_bin_file(&dev->primary->kdev.kobj, &error_state_attr); |
Chris Wilson | 97e4eed | 2013-08-26 16:18:54 +0100 | [diff] [blame] | 589 | if (IS_VALLEYVIEW(dev)) |
| 590 | sysfs_remove_files(&dev->primary->kdev.kobj, vlv_attrs); |
| 591 | else |
| 592 | sysfs_remove_files(&dev->primary->kdev.kobj, gen6_attrs); |
Ben Widawsky | 35a85ac | 2013-09-19 11:13:41 -0700 | [diff] [blame] | 593 | device_remove_bin_file(&dev->primary->kdev, &dpf_attrs_1); |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 594 | device_remove_bin_file(&dev->primary->kdev, &dpf_attrs); |
Ben Widawsky | 853c70e | 2012-09-19 10:50:19 -0700 | [diff] [blame] | 595 | #ifdef CONFIG_PM |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 596 | sysfs_unmerge_group(&dev->primary->kdev.kobj, &rc6_attr_group); |
Ben Widawsky | 853c70e | 2012-09-19 10:50:19 -0700 | [diff] [blame] | 597 | #endif |
Ben Widawsky | 0136db5 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 598 | } |