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 35*5ab3633dSHunt Xu #ifdef CONFIG_PM 360136db58SBen Widawsky static u32 calc_residency(struct drm_device *dev, const u32 reg) 370136db58SBen Widawsky { 380136db58SBen Widawsky struct drm_i915_private *dev_priv = dev->dev_private; 390136db58SBen Widawsky u64 raw_time; /* 32b value may overflow during fixed point math */ 400136db58SBen Widawsky 410136db58SBen Widawsky if (!intel_enable_rc6(dev)) 420136db58SBen Widawsky return 0; 430136db58SBen Widawsky 44a85d4bcbSBen Widawsky raw_time = I915_READ(reg) * 128ULL; 45a85d4bcbSBen Widawsky return DIV_ROUND_UP_ULL(raw_time, 100000); 460136db58SBen Widawsky } 470136db58SBen Widawsky 480136db58SBen Widawsky static ssize_t 490136db58SBen Widawsky show_rc6_mask(struct device *dev, struct device_attribute *attr, char *buf) 500136db58SBen Widawsky { 510136db58SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 520136db58SBen Widawsky return snprintf(buf, PAGE_SIZE, "%x", intel_enable_rc6(dminor->dev)); 530136db58SBen Widawsky } 540136db58SBen Widawsky 550136db58SBen Widawsky static ssize_t 560136db58SBen Widawsky show_rc6_ms(struct device *dev, struct device_attribute *attr, char *buf) 570136db58SBen Widawsky { 580136db58SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 590136db58SBen Widawsky u32 rc6_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6); 600136db58SBen Widawsky return snprintf(buf, PAGE_SIZE, "%u", rc6_residency); 610136db58SBen Widawsky } 620136db58SBen Widawsky 630136db58SBen Widawsky static ssize_t 640136db58SBen Widawsky show_rc6p_ms(struct device *dev, struct device_attribute *attr, char *buf) 650136db58SBen Widawsky { 660136db58SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 670136db58SBen Widawsky u32 rc6p_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6p); 680136db58SBen Widawsky return snprintf(buf, PAGE_SIZE, "%u", rc6p_residency); 690136db58SBen Widawsky } 700136db58SBen Widawsky 710136db58SBen Widawsky static ssize_t 720136db58SBen Widawsky show_rc6pp_ms(struct device *dev, struct device_attribute *attr, char *buf) 730136db58SBen Widawsky { 740136db58SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 750136db58SBen Widawsky u32 rc6pp_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6pp); 760136db58SBen Widawsky return snprintf(buf, PAGE_SIZE, "%u", rc6pp_residency); 770136db58SBen Widawsky } 780136db58SBen Widawsky 790136db58SBen Widawsky static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL); 800136db58SBen Widawsky static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL); 810136db58SBen Widawsky static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL); 820136db58SBen Widawsky static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL); 830136db58SBen Widawsky 840136db58SBen Widawsky static struct attribute *rc6_attrs[] = { 850136db58SBen Widawsky &dev_attr_rc6_enable.attr, 860136db58SBen Widawsky &dev_attr_rc6_residency_ms.attr, 870136db58SBen Widawsky &dev_attr_rc6p_residency_ms.attr, 880136db58SBen Widawsky &dev_attr_rc6pp_residency_ms.attr, 890136db58SBen Widawsky NULL 900136db58SBen Widawsky }; 910136db58SBen Widawsky 920136db58SBen Widawsky static struct attribute_group rc6_attr_group = { 930136db58SBen Widawsky .name = power_group_name, 940136db58SBen Widawsky .attrs = rc6_attrs 950136db58SBen Widawsky }; 960136db58SBen Widawsky 9784bc7581SBen Widawsky static int l3_access_valid(struct drm_device *dev, loff_t offset) 9884bc7581SBen Widawsky { 9984bc7581SBen Widawsky if (!IS_IVYBRIDGE(dev)) 10084bc7581SBen Widawsky return -EPERM; 10184bc7581SBen Widawsky 10284bc7581SBen Widawsky if (offset % 4 != 0) 10384bc7581SBen Widawsky return -EINVAL; 10484bc7581SBen Widawsky 10584bc7581SBen Widawsky if (offset >= GEN7_L3LOG_SIZE) 10684bc7581SBen Widawsky return -ENXIO; 10784bc7581SBen Widawsky 10884bc7581SBen Widawsky return 0; 10984bc7581SBen Widawsky } 11084bc7581SBen Widawsky 11184bc7581SBen Widawsky static ssize_t 11284bc7581SBen Widawsky i915_l3_read(struct file *filp, struct kobject *kobj, 11384bc7581SBen Widawsky struct bin_attribute *attr, char *buf, 11484bc7581SBen Widawsky loff_t offset, size_t count) 11584bc7581SBen Widawsky { 11684bc7581SBen Widawsky struct device *dev = container_of(kobj, struct device, kobj); 11784bc7581SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 11884bc7581SBen Widawsky struct drm_device *drm_dev = dminor->dev; 11984bc7581SBen Widawsky struct drm_i915_private *dev_priv = drm_dev->dev_private; 12084bc7581SBen Widawsky uint32_t misccpctl; 12184bc7581SBen Widawsky int i, ret; 12284bc7581SBen Widawsky 12384bc7581SBen Widawsky ret = l3_access_valid(drm_dev, offset); 12484bc7581SBen Widawsky if (ret) 12584bc7581SBen Widawsky return ret; 12684bc7581SBen Widawsky 12784bc7581SBen Widawsky ret = i915_mutex_lock_interruptible(drm_dev); 12884bc7581SBen Widawsky if (ret) 12984bc7581SBen Widawsky return ret; 13084bc7581SBen Widawsky 13184bc7581SBen Widawsky misccpctl = I915_READ(GEN7_MISCCPCTL); 13284bc7581SBen Widawsky I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE); 13384bc7581SBen Widawsky 13484bc7581SBen Widawsky for (i = offset; count >= 4 && i < GEN7_L3LOG_SIZE; i += 4, count -= 4) 13584bc7581SBen Widawsky *((uint32_t *)(&buf[i])) = I915_READ(GEN7_L3LOG_BASE + i); 13684bc7581SBen Widawsky 13784bc7581SBen Widawsky I915_WRITE(GEN7_MISCCPCTL, misccpctl); 13884bc7581SBen Widawsky 13984bc7581SBen Widawsky mutex_unlock(&drm_dev->struct_mutex); 14084bc7581SBen Widawsky 14184bc7581SBen Widawsky return i - offset; 14284bc7581SBen Widawsky } 14384bc7581SBen Widawsky 14484bc7581SBen Widawsky static ssize_t 14584bc7581SBen Widawsky i915_l3_write(struct file *filp, struct kobject *kobj, 14684bc7581SBen Widawsky struct bin_attribute *attr, char *buf, 14784bc7581SBen Widawsky loff_t offset, size_t count) 14884bc7581SBen Widawsky { 14984bc7581SBen Widawsky struct device *dev = container_of(kobj, struct device, kobj); 15084bc7581SBen Widawsky struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev); 15184bc7581SBen Widawsky struct drm_device *drm_dev = dminor->dev; 15284bc7581SBen Widawsky struct drm_i915_private *dev_priv = drm_dev->dev_private; 15384bc7581SBen Widawsky u32 *temp = NULL; /* Just here to make handling failures easy */ 15484bc7581SBen Widawsky int ret; 15584bc7581SBen Widawsky 15684bc7581SBen Widawsky ret = l3_access_valid(drm_dev, offset); 15784bc7581SBen Widawsky if (ret) 15884bc7581SBen Widawsky return ret; 15984bc7581SBen Widawsky 16084bc7581SBen Widawsky ret = i915_mutex_lock_interruptible(drm_dev); 16184bc7581SBen Widawsky if (ret) 16284bc7581SBen Widawsky return ret; 16384bc7581SBen Widawsky 16484bc7581SBen Widawsky if (!dev_priv->mm.l3_remap_info) { 16584bc7581SBen Widawsky temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL); 16684bc7581SBen Widawsky if (!temp) { 16784bc7581SBen Widawsky mutex_unlock(&drm_dev->struct_mutex); 16884bc7581SBen Widawsky return -ENOMEM; 16984bc7581SBen Widawsky } 17084bc7581SBen Widawsky } 17184bc7581SBen Widawsky 17284bc7581SBen Widawsky ret = i915_gpu_idle(drm_dev); 17384bc7581SBen Widawsky if (ret) { 17484bc7581SBen Widawsky kfree(temp); 17584bc7581SBen Widawsky mutex_unlock(&drm_dev->struct_mutex); 17684bc7581SBen Widawsky return ret; 17784bc7581SBen Widawsky } 17884bc7581SBen Widawsky 17984bc7581SBen Widawsky /* TODO: Ideally we really want a GPU reset here to make sure errors 18084bc7581SBen Widawsky * aren't propagated. Since I cannot find a stable way to reset the GPU 18184bc7581SBen Widawsky * at this point it is left as a TODO. 18284bc7581SBen Widawsky */ 18384bc7581SBen Widawsky if (temp) 18484bc7581SBen Widawsky dev_priv->mm.l3_remap_info = temp; 18584bc7581SBen Widawsky 18684bc7581SBen Widawsky memcpy(dev_priv->mm.l3_remap_info + (offset/4), 18784bc7581SBen Widawsky buf + (offset/4), 18884bc7581SBen Widawsky count); 18984bc7581SBen Widawsky 19084bc7581SBen Widawsky i915_gem_l3_remap(drm_dev); 19184bc7581SBen Widawsky 19284bc7581SBen Widawsky mutex_unlock(&drm_dev->struct_mutex); 19384bc7581SBen Widawsky 19484bc7581SBen Widawsky return count; 19584bc7581SBen Widawsky } 19684bc7581SBen Widawsky 19784bc7581SBen Widawsky static struct bin_attribute dpf_attrs = { 19884bc7581SBen Widawsky .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)}, 19984bc7581SBen Widawsky .size = GEN7_L3LOG_SIZE, 20084bc7581SBen Widawsky .read = i915_l3_read, 20184bc7581SBen Widawsky .write = i915_l3_write, 20284bc7581SBen Widawsky .mmap = NULL 20384bc7581SBen Widawsky }; 20484bc7581SBen Widawsky 2050136db58SBen Widawsky void i915_setup_sysfs(struct drm_device *dev) 2060136db58SBen Widawsky { 2070136db58SBen Widawsky int ret; 2080136db58SBen Widawsky 209112abd29SDaniel Vetter if (INTEL_INFO(dev)->gen >= 6) { 210112abd29SDaniel Vetter ret = sysfs_merge_group(&dev->primary->kdev.kobj, 211112abd29SDaniel Vetter &rc6_attr_group); 2120136db58SBen Widawsky if (ret) 21384bc7581SBen Widawsky DRM_ERROR("RC6 residency sysfs setup failed\n"); 214112abd29SDaniel Vetter } 21584bc7581SBen Widawsky 216112abd29SDaniel Vetter if (IS_IVYBRIDGE(dev)) { 21784bc7581SBen Widawsky ret = device_create_bin_file(&dev->primary->kdev, &dpf_attrs); 21884bc7581SBen Widawsky if (ret) 21984bc7581SBen Widawsky DRM_ERROR("l3 parity sysfs setup failed\n"); 2200136db58SBen Widawsky } 221112abd29SDaniel Vetter } 2220136db58SBen Widawsky 2230136db58SBen Widawsky void i915_teardown_sysfs(struct drm_device *dev) 2240136db58SBen Widawsky { 22584bc7581SBen Widawsky device_remove_bin_file(&dev->primary->kdev, &dpf_attrs); 2260136db58SBen Widawsky sysfs_unmerge_group(&dev->primary->kdev.kobj, &rc6_attr_group); 2270136db58SBen Widawsky } 228*5ab3633dSHunt Xu #else 229*5ab3633dSHunt Xu void i915_setup_sysfs(struct drm_device *dev) 230*5ab3633dSHunt Xu { 231*5ab3633dSHunt Xu return; 232*5ab3633dSHunt Xu } 233*5ab3633dSHunt Xu 234*5ab3633dSHunt Xu void i915_teardown_sysfs(struct drm_device *dev) 235*5ab3633dSHunt Xu { 236*5ab3633dSHunt Xu return; 237*5ab3633dSHunt Xu } 238*5ab3633dSHunt Xu #endif /* CONFIG_PM */ 239