Ben Widawsky | 0136db58 | 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 | 0136db58 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 33 | #include "i915_drv.h" |
| 34 | |
| 35 | static u32 calc_residency(struct drm_device *dev, const u32 reg) |
| 36 | { |
| 37 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 38 | u64 raw_time; /* 32b value may overflow during fixed point math */ |
| 39 | |
| 40 | if (!intel_enable_rc6(dev)) |
| 41 | return 0; |
| 42 | |
Ben Widawsky | a85d4bc | 2012-04-20 11:50:01 -0700 | [diff] [blame] | 43 | raw_time = I915_READ(reg) * 128ULL; |
| 44 | return DIV_ROUND_UP_ULL(raw_time, 100000); |
Ben Widawsky | 0136db58 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static ssize_t |
| 48 | show_rc6_mask(struct device *dev, struct device_attribute *attr, char *buf) |
| 49 | { |
| 50 | struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); |
| 51 | return snprintf(buf, PAGE_SIZE, "%x", intel_enable_rc6(dminor->dev)); |
| 52 | } |
| 53 | |
| 54 | static ssize_t |
| 55 | show_rc6_ms(struct device *dev, struct device_attribute *attr, char *buf) |
| 56 | { |
| 57 | struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); |
| 58 | u32 rc6_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6); |
| 59 | return snprintf(buf, PAGE_SIZE, "%u", rc6_residency); |
| 60 | } |
| 61 | |
| 62 | static ssize_t |
| 63 | show_rc6p_ms(struct device *dev, struct device_attribute *attr, char *buf) |
| 64 | { |
| 65 | struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); |
| 66 | u32 rc6p_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6p); |
| 67 | return snprintf(buf, PAGE_SIZE, "%u", rc6p_residency); |
| 68 | } |
| 69 | |
| 70 | static ssize_t |
| 71 | show_rc6pp_ms(struct device *dev, struct device_attribute *attr, char *buf) |
| 72 | { |
| 73 | struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); |
| 74 | u32 rc6pp_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6pp); |
| 75 | return snprintf(buf, PAGE_SIZE, "%u", rc6pp_residency); |
| 76 | } |
| 77 | |
| 78 | static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL); |
| 79 | static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL); |
| 80 | static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL); |
| 81 | static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL); |
| 82 | |
| 83 | static struct attribute *rc6_attrs[] = { |
| 84 | &dev_attr_rc6_enable.attr, |
| 85 | &dev_attr_rc6_residency_ms.attr, |
| 86 | &dev_attr_rc6p_residency_ms.attr, |
| 87 | &dev_attr_rc6pp_residency_ms.attr, |
| 88 | NULL |
| 89 | }; |
| 90 | |
| 91 | static struct attribute_group rc6_attr_group = { |
| 92 | .name = power_group_name, |
| 93 | .attrs = rc6_attrs |
| 94 | }; |
| 95 | |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 96 | static int l3_access_valid(struct drm_device *dev, loff_t offset) |
| 97 | { |
| 98 | if (!IS_IVYBRIDGE(dev)) |
| 99 | return -EPERM; |
| 100 | |
| 101 | if (offset % 4 != 0) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | if (offset >= GEN7_L3LOG_SIZE) |
| 105 | return -ENXIO; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static ssize_t |
| 111 | i915_l3_read(struct file *filp, struct kobject *kobj, |
| 112 | struct bin_attribute *attr, char *buf, |
| 113 | loff_t offset, size_t count) |
| 114 | { |
| 115 | struct device *dev = container_of(kobj, struct device, kobj); |
| 116 | struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); |
| 117 | struct drm_device *drm_dev = dminor->dev; |
| 118 | struct drm_i915_private *dev_priv = drm_dev->dev_private; |
| 119 | uint32_t misccpctl; |
| 120 | int i, ret; |
| 121 | |
| 122 | ret = l3_access_valid(drm_dev, offset); |
| 123 | if (ret) |
| 124 | return ret; |
| 125 | |
| 126 | ret = i915_mutex_lock_interruptible(drm_dev); |
| 127 | if (ret) |
| 128 | return ret; |
| 129 | |
| 130 | misccpctl = I915_READ(GEN7_MISCCPCTL); |
| 131 | I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE); |
| 132 | |
| 133 | for (i = offset; count >= 4 && i < GEN7_L3LOG_SIZE; i += 4, count -= 4) |
| 134 | *((uint32_t *)(&buf[i])) = I915_READ(GEN7_L3LOG_BASE + i); |
| 135 | |
| 136 | I915_WRITE(GEN7_MISCCPCTL, misccpctl); |
| 137 | |
| 138 | mutex_unlock(&drm_dev->struct_mutex); |
| 139 | |
| 140 | return i - offset; |
| 141 | } |
| 142 | |
| 143 | static ssize_t |
| 144 | i915_l3_write(struct file *filp, struct kobject *kobj, |
| 145 | struct bin_attribute *attr, char *buf, |
| 146 | loff_t offset, size_t count) |
| 147 | { |
| 148 | struct device *dev = container_of(kobj, struct device, kobj); |
| 149 | struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); |
| 150 | struct drm_device *drm_dev = dminor->dev; |
| 151 | struct drm_i915_private *dev_priv = drm_dev->dev_private; |
| 152 | u32 *temp = NULL; /* Just here to make handling failures easy */ |
| 153 | int ret; |
| 154 | |
| 155 | ret = l3_access_valid(drm_dev, offset); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | ret = i915_mutex_lock_interruptible(drm_dev); |
| 160 | if (ret) |
| 161 | return ret; |
| 162 | |
| 163 | if (!dev_priv->mm.l3_remap_info) { |
| 164 | temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL); |
| 165 | if (!temp) { |
| 166 | mutex_unlock(&drm_dev->struct_mutex); |
| 167 | return -ENOMEM; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | ret = i915_gpu_idle(drm_dev); |
| 172 | if (ret) { |
| 173 | kfree(temp); |
| 174 | mutex_unlock(&drm_dev->struct_mutex); |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | /* TODO: Ideally we really want a GPU reset here to make sure errors |
| 179 | * aren't propagated. Since I cannot find a stable way to reset the GPU |
| 180 | * at this point it is left as a TODO. |
| 181 | */ |
| 182 | if (temp) |
| 183 | dev_priv->mm.l3_remap_info = temp; |
| 184 | |
| 185 | memcpy(dev_priv->mm.l3_remap_info + (offset/4), |
| 186 | buf + (offset/4), |
| 187 | count); |
| 188 | |
| 189 | i915_gem_l3_remap(drm_dev); |
| 190 | |
| 191 | mutex_unlock(&drm_dev->struct_mutex); |
| 192 | |
| 193 | return count; |
| 194 | } |
| 195 | |
| 196 | static struct bin_attribute dpf_attrs = { |
| 197 | .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)}, |
| 198 | .size = GEN7_L3LOG_SIZE, |
| 199 | .read = i915_l3_read, |
| 200 | .write = i915_l3_write, |
| 201 | .mmap = NULL |
| 202 | }; |
| 203 | |
Ben Widawsky | 0136db58 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 204 | void i915_setup_sysfs(struct drm_device *dev) |
| 205 | { |
| 206 | int ret; |
| 207 | |
Daniel Vetter | 112abd2 | 2012-05-31 14:57:43 +0200 | [diff] [blame] | 208 | if (INTEL_INFO(dev)->gen >= 6) { |
| 209 | ret = sysfs_merge_group(&dev->primary->kdev.kobj, |
| 210 | &rc6_attr_group); |
| 211 | if (ret) |
| 212 | DRM_ERROR("RC6 residency sysfs setup failed\n"); |
| 213 | } |
Ben Widawsky | 0136db58 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 214 | |
Ben Widawsky | e1ef7cc | 2012-07-24 20:47:31 -0700 | [diff] [blame^] | 215 | if (HAS_L3_GPU_CACHE(dev)) { |
Daniel Vetter | 112abd2 | 2012-05-31 14:57:43 +0200 | [diff] [blame] | 216 | ret = device_create_bin_file(&dev->primary->kdev, &dpf_attrs); |
| 217 | if (ret) |
| 218 | DRM_ERROR("l3 parity sysfs setup failed\n"); |
| 219 | } |
Ben Widawsky | 0136db58 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void i915_teardown_sysfs(struct drm_device *dev) |
| 223 | { |
Ben Widawsky | 84bc758 | 2012-05-25 16:56:25 -0700 | [diff] [blame] | 224 | device_remove_bin_file(&dev->primary->kdev, &dpf_attrs); |
Ben Widawsky | 0136db58 | 2012-04-10 21:17:01 -0700 | [diff] [blame] | 225 | sysfs_unmerge_group(&dev->primary->kdev.kobj, &rc6_attr_group); |
| 226 | } |