10136db58SBen Widawsky /* 20136db58SBen Widawsky * Copyright © 2012 Intel Corporation 30136db58SBen Widawsky * 40136db58SBen Widawsky * Permission is hereby granted, free of charge, to any person obtaining a 50136db58SBen Widawsky * copy of this software and associated documentation files (the "Software"), 60136db58SBen Widawsky * to deal in the Software without restriction, including without limitation 70136db58SBen Widawsky * the rights to use, copy, modify, merge, publish, distribute, sublicense, 80136db58SBen Widawsky * and/or sell copies of the Software, and to permit persons to whom the 90136db58SBen Widawsky * Software is furnished to do so, subject to the following conditions: 100136db58SBen Widawsky * 110136db58SBen Widawsky * The above copyright notice and this permission notice (including the next 120136db58SBen Widawsky * paragraph) shall be included in all copies or substantial portions of the 130136db58SBen Widawsky * Software. 140136db58SBen Widawsky * 150136db58SBen Widawsky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 160136db58SBen Widawsky * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 170136db58SBen Widawsky * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 180136db58SBen Widawsky * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 190136db58SBen Widawsky * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 200136db58SBen Widawsky * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 210136db58SBen Widawsky * IN THE SOFTWARE. 220136db58SBen Widawsky * 230136db58SBen Widawsky * Authors: 240136db58SBen Widawsky * Ben Widawsky <ben@bwidawsk.net> 250136db58SBen Widawsky * 260136db58SBen Widawsky */ 270136db58SBen Widawsky 280136db58SBen Widawsky #include <linux/device.h> 290136db58SBen Widawsky #include <linux/module.h> 300136db58SBen Widawsky #include <linux/stat.h> 310136db58SBen Widawsky #include <linux/sysfs.h> 3284bc7581SBen Widawsky #include "intel_drv.h" 330136db58SBen Widawsky #include "i915_drv.h" 340136db58SBen Widawsky 350136db58SBen Widawsky static u32 calc_residency(struct drm_device *dev, const u32 reg) 360136db58SBen Widawsky { 370136db58SBen Widawsky struct drm_i915_private *dev_priv = dev->dev_private; 380136db58SBen Widawsky u64 raw_time; /* 32b value may overflow during fixed point math */ 390136db58SBen Widawsky 400136db58SBen Widawsky if (!intel_enable_rc6(dev)) 410136db58SBen Widawsky return 0; 420136db58SBen Widawsky 43a85d4bcbSBen Widawsky raw_time = I915_READ(reg) * 128ULL; 44a85d4bcbSBen Widawsky return DIV_ROUND_UP_ULL(raw_time, 100000); 450136db58SBen Widawsky } 460136db58SBen Widawsky 470136db58SBen Widawsky static ssize_t 480136db58SBen Widawsky show_rc6_mask(struct device *dev, struct device_attribute *attr, char *buf) 490136db58SBen Widawsky { 500136db58SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 510136db58SBen Widawsky return snprintf(buf, PAGE_SIZE, "%x", intel_enable_rc6(dminor->dev)); 520136db58SBen Widawsky } 530136db58SBen Widawsky 540136db58SBen Widawsky static ssize_t 550136db58SBen Widawsky show_rc6_ms(struct device *dev, struct device_attribute *attr, char *buf) 560136db58SBen Widawsky { 570136db58SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 580136db58SBen Widawsky u32 rc6_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6); 590136db58SBen Widawsky return snprintf(buf, PAGE_SIZE, "%u", rc6_residency); 600136db58SBen Widawsky } 610136db58SBen Widawsky 620136db58SBen Widawsky static ssize_t 630136db58SBen Widawsky show_rc6p_ms(struct device *dev, struct device_attribute *attr, char *buf) 640136db58SBen Widawsky { 650136db58SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 660136db58SBen Widawsky u32 rc6p_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6p); 670136db58SBen Widawsky return snprintf(buf, PAGE_SIZE, "%u", rc6p_residency); 680136db58SBen Widawsky } 690136db58SBen Widawsky 700136db58SBen Widawsky static ssize_t 710136db58SBen Widawsky show_rc6pp_ms(struct device *dev, struct device_attribute *attr, char *buf) 720136db58SBen Widawsky { 730136db58SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 740136db58SBen Widawsky u32 rc6pp_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6pp); 750136db58SBen Widawsky return snprintf(buf, PAGE_SIZE, "%u", rc6pp_residency); 760136db58SBen Widawsky } 770136db58SBen Widawsky 780136db58SBen Widawsky static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL); 790136db58SBen Widawsky static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL); 800136db58SBen Widawsky static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL); 810136db58SBen Widawsky static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL); 820136db58SBen Widawsky 830136db58SBen Widawsky static struct attribute *rc6_attrs[] = { 840136db58SBen Widawsky &dev_attr_rc6_enable.attr, 850136db58SBen Widawsky &dev_attr_rc6_residency_ms.attr, 860136db58SBen Widawsky &dev_attr_rc6p_residency_ms.attr, 870136db58SBen Widawsky &dev_attr_rc6pp_residency_ms.attr, 880136db58SBen Widawsky NULL 890136db58SBen Widawsky }; 900136db58SBen Widawsky 910136db58SBen Widawsky static struct attribute_group rc6_attr_group = { 920136db58SBen Widawsky .name = power_group_name, 930136db58SBen Widawsky .attrs = rc6_attrs 940136db58SBen Widawsky }; 950136db58SBen Widawsky 9684bc7581SBen Widawsky static int l3_access_valid(struct drm_device *dev, loff_t offset) 9784bc7581SBen Widawsky { 9884bc7581SBen Widawsky if (!IS_IVYBRIDGE(dev)) 9984bc7581SBen Widawsky return -EPERM; 10084bc7581SBen Widawsky 10184bc7581SBen Widawsky if (offset % 4 != 0) 10284bc7581SBen Widawsky return -EINVAL; 10384bc7581SBen Widawsky 10484bc7581SBen Widawsky if (offset >= GEN7_L3LOG_SIZE) 10584bc7581SBen Widawsky return -ENXIO; 10684bc7581SBen Widawsky 10784bc7581SBen Widawsky return 0; 10884bc7581SBen Widawsky } 10984bc7581SBen Widawsky 11084bc7581SBen Widawsky static ssize_t 11184bc7581SBen Widawsky i915_l3_read(struct file *filp, struct kobject *kobj, 11284bc7581SBen Widawsky struct bin_attribute *attr, char *buf, 11384bc7581SBen Widawsky loff_t offset, size_t count) 11484bc7581SBen Widawsky { 11584bc7581SBen Widawsky struct device *dev = container_of(kobj, struct device, kobj); 11684bc7581SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 11784bc7581SBen Widawsky struct drm_device *drm_dev = dminor->dev; 11884bc7581SBen Widawsky struct drm_i915_private *dev_priv = drm_dev->dev_private; 11984bc7581SBen Widawsky uint32_t misccpctl; 12084bc7581SBen Widawsky int i, ret; 12184bc7581SBen Widawsky 12284bc7581SBen Widawsky ret = l3_access_valid(drm_dev, offset); 12384bc7581SBen Widawsky if (ret) 12484bc7581SBen Widawsky return ret; 12584bc7581SBen Widawsky 12684bc7581SBen Widawsky ret = i915_mutex_lock_interruptible(drm_dev); 12784bc7581SBen Widawsky if (ret) 12884bc7581SBen Widawsky return ret; 12984bc7581SBen Widawsky 13084bc7581SBen Widawsky misccpctl = I915_READ(GEN7_MISCCPCTL); 13184bc7581SBen Widawsky I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE); 13284bc7581SBen Widawsky 13384bc7581SBen Widawsky for (i = offset; count >= 4 && i < GEN7_L3LOG_SIZE; i += 4, count -= 4) 13484bc7581SBen Widawsky *((uint32_t *)(&buf[i])) = I915_READ(GEN7_L3LOG_BASE + i); 13584bc7581SBen Widawsky 13684bc7581SBen Widawsky I915_WRITE(GEN7_MISCCPCTL, misccpctl); 13784bc7581SBen Widawsky 13884bc7581SBen Widawsky mutex_unlock(&drm_dev->struct_mutex); 13984bc7581SBen Widawsky 14084bc7581SBen Widawsky return i - offset; 14184bc7581SBen Widawsky } 14284bc7581SBen Widawsky 14384bc7581SBen Widawsky static ssize_t 14484bc7581SBen Widawsky i915_l3_write(struct file *filp, struct kobject *kobj, 14584bc7581SBen Widawsky struct bin_attribute *attr, char *buf, 14684bc7581SBen Widawsky loff_t offset, size_t count) 14784bc7581SBen Widawsky { 14884bc7581SBen Widawsky struct device *dev = container_of(kobj, struct device, kobj); 14984bc7581SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 15084bc7581SBen Widawsky struct drm_device *drm_dev = dminor->dev; 15184bc7581SBen Widawsky struct drm_i915_private *dev_priv = drm_dev->dev_private; 15284bc7581SBen Widawsky u32 *temp = NULL; /* Just here to make handling failures easy */ 15384bc7581SBen Widawsky int ret; 15484bc7581SBen Widawsky 15584bc7581SBen Widawsky ret = l3_access_valid(drm_dev, offset); 15684bc7581SBen Widawsky if (ret) 15784bc7581SBen Widawsky return ret; 15884bc7581SBen Widawsky 15984bc7581SBen Widawsky ret = i915_mutex_lock_interruptible(drm_dev); 16084bc7581SBen Widawsky if (ret) 16184bc7581SBen Widawsky return ret; 16284bc7581SBen Widawsky 16384bc7581SBen Widawsky if (!dev_priv->mm.l3_remap_info) { 16484bc7581SBen Widawsky temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL); 16584bc7581SBen Widawsky if (!temp) { 16684bc7581SBen Widawsky mutex_unlock(&drm_dev->struct_mutex); 16784bc7581SBen Widawsky return -ENOMEM; 16884bc7581SBen Widawsky } 16984bc7581SBen Widawsky } 17084bc7581SBen Widawsky 17184bc7581SBen Widawsky ret = i915_gpu_idle(drm_dev); 17284bc7581SBen Widawsky if (ret) { 17384bc7581SBen Widawsky kfree(temp); 17484bc7581SBen Widawsky mutex_unlock(&drm_dev->struct_mutex); 17584bc7581SBen Widawsky return ret; 17684bc7581SBen Widawsky } 17784bc7581SBen Widawsky 17884bc7581SBen Widawsky /* TODO: Ideally we really want a GPU reset here to make sure errors 17984bc7581SBen Widawsky * aren't propagated. Since I cannot find a stable way to reset the GPU 18084bc7581SBen Widawsky * at this point it is left as a TODO. 18184bc7581SBen Widawsky */ 18284bc7581SBen Widawsky if (temp) 18384bc7581SBen Widawsky dev_priv->mm.l3_remap_info = temp; 18484bc7581SBen Widawsky 18584bc7581SBen Widawsky memcpy(dev_priv->mm.l3_remap_info + (offset/4), 18684bc7581SBen Widawsky buf + (offset/4), 18784bc7581SBen Widawsky count); 18884bc7581SBen Widawsky 18984bc7581SBen Widawsky i915_gem_l3_remap(drm_dev); 19084bc7581SBen Widawsky 19184bc7581SBen Widawsky mutex_unlock(&drm_dev->struct_mutex); 19284bc7581SBen Widawsky 19384bc7581SBen Widawsky return count; 19484bc7581SBen Widawsky } 19584bc7581SBen Widawsky 19684bc7581SBen Widawsky static struct bin_attribute dpf_attrs = { 19784bc7581SBen Widawsky .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)}, 19884bc7581SBen Widawsky .size = GEN7_L3LOG_SIZE, 19984bc7581SBen Widawsky .read = i915_l3_read, 20084bc7581SBen Widawsky .write = i915_l3_write, 20184bc7581SBen Widawsky .mmap = NULL 20284bc7581SBen Widawsky }; 20384bc7581SBen Widawsky 2040136db58SBen Widawsky void i915_setup_sysfs(struct drm_device *dev) 2050136db58SBen Widawsky { 2060136db58SBen Widawsky int ret; 2070136db58SBen Widawsky 208112abd29SDaniel Vetter if (INTEL_INFO(dev)->gen >= 6) { 209112abd29SDaniel Vetter ret = sysfs_merge_group(&dev->primary->kdev.kobj, 210112abd29SDaniel Vetter &rc6_attr_group); 2110136db58SBen Widawsky if (ret) 21284bc7581SBen Widawsky DRM_ERROR("RC6 residency sysfs setup failed\n"); 213112abd29SDaniel Vetter } 21484bc7581SBen Widawsky 215*e1ef7cc2SBen Widawsky if (HAS_L3_GPU_CACHE(dev)) { 21684bc7581SBen Widawsky ret = device_create_bin_file(&dev->primary->kdev, &dpf_attrs); 21784bc7581SBen Widawsky if (ret) 21884bc7581SBen Widawsky DRM_ERROR("l3 parity sysfs setup failed\n"); 2190136db58SBen Widawsky } 220112abd29SDaniel Vetter } 2210136db58SBen Widawsky 2220136db58SBen Widawsky void i915_teardown_sysfs(struct drm_device *dev) 2230136db58SBen Widawsky { 22484bc7581SBen Widawsky device_remove_bin_file(&dev->primary->kdev, &dpf_attrs); 2250136db58SBen Widawsky sysfs_unmerge_group(&dev->primary->kdev.kobj, &rc6_attr_group); 2260136db58SBen Widawsky } 227