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 35694c2828SDavid Weinehall static inline struct drm_i915_private *kdev_minor_to_i915(struct device *kdev) 36c49d13eeSDavid Weinehall { 37694c2828SDavid Weinehall struct drm_minor *minor = dev_get_drvdata(kdev); 38694c2828SDavid Weinehall return to_i915(minor->dev); 39c49d13eeSDavid Weinehall } 4014c8d110SDave Airlie 415ab3633dSHunt Xu #ifdef CONFIG_PM 42694c2828SDavid Weinehall static u32 calc_residency(struct drm_i915_private *dev_priv, 43f0f59a00SVille Syrjälä i915_reg_t reg) 440136db58SBen Widawsky { 450136db58SBen Widawsky u64 raw_time; /* 32b value may overflow during fixed point math */ 462cc9fab1SVille Syrjälä u64 units = 128ULL, div = 100000ULL; 47c8c8fb33SPaulo Zanoni u32 ret; 480136db58SBen Widawsky 49dc97997aSChris Wilson if (!intel_enable_rc6()) 500136db58SBen Widawsky return 0; 510136db58SBen Widawsky 52c8c8fb33SPaulo Zanoni intel_runtime_pm_get(dev_priv); 53c8c8fb33SPaulo Zanoni 54542a6b20SMika Kuoppala /* On VLV and CHV, residency time is in CZ units rather than 1.28us */ 55694c2828SDavid Weinehall if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) { 562cc9fab1SVille Syrjälä units = 1; 572cc9fab1SVille Syrjälä div = dev_priv->czclk_freq; 58542a6b20SMika Kuoppala 59e454a05dSJesse Barnes if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH) 60e454a05dSJesse Barnes units <<= 8; 61254e0931SMichel Thierry } else if (IS_GEN9_LP(dev_priv)) { 62d8135109SImre Deak units = 1; 63d8135109SImre Deak div = 1200; /* 833.33ns */ 64e454a05dSJesse Barnes } 65e454a05dSJesse Barnes 66e454a05dSJesse Barnes raw_time = I915_READ(reg) * units; 67c8c8fb33SPaulo Zanoni ret = DIV_ROUND_UP_ULL(raw_time, div); 68c8c8fb33SPaulo Zanoni 69c8c8fb33SPaulo Zanoni intel_runtime_pm_put(dev_priv); 70c8c8fb33SPaulo Zanoni return ret; 710136db58SBen Widawsky } 720136db58SBen Widawsky 730136db58SBen Widawsky static ssize_t 74dbdfd8e9SBen Widawsky show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf) 750136db58SBen Widawsky { 76dc97997aSChris Wilson return snprintf(buf, PAGE_SIZE, "%x\n", intel_enable_rc6()); 770136db58SBen Widawsky } 780136db58SBen Widawsky 790136db58SBen Widawsky static ssize_t 80dbdfd8e9SBen Widawsky show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf) 810136db58SBen Widawsky { 82694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 83694c2828SDavid Weinehall u32 rc6_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6); 843e2a1556SJani Nikula return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency); 850136db58SBen Widawsky } 860136db58SBen Widawsky 870136db58SBen Widawsky static ssize_t 88dbdfd8e9SBen Widawsky show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf) 890136db58SBen Widawsky { 90694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 91694c2828SDavid Weinehall u32 rc6p_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6p); 923e2a1556SJani Nikula return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency); 930136db58SBen Widawsky } 940136db58SBen Widawsky 950136db58SBen Widawsky static ssize_t 96dbdfd8e9SBen Widawsky show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf) 970136db58SBen Widawsky { 98694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 99694c2828SDavid Weinehall u32 rc6pp_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6pp); 1003e2a1556SJani Nikula return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency); 1010136db58SBen Widawsky } 1020136db58SBen Widawsky 103626ad6f3SVille Syrjälä static ssize_t 104626ad6f3SVille Syrjälä show_media_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf) 105626ad6f3SVille Syrjälä { 106694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 107694c2828SDavid Weinehall u32 rc6_residency = calc_residency(dev_priv, VLV_GT_MEDIA_RC6); 108626ad6f3SVille Syrjälä return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency); 109626ad6f3SVille Syrjälä } 110626ad6f3SVille Syrjälä 1110136db58SBen Widawsky static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL); 1120136db58SBen Widawsky static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL); 1130136db58SBen Widawsky static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL); 1140136db58SBen Widawsky static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL); 115626ad6f3SVille Syrjälä static DEVICE_ATTR(media_rc6_residency_ms, S_IRUGO, show_media_rc6_ms, NULL); 1160136db58SBen Widawsky 1170136db58SBen Widawsky static struct attribute *rc6_attrs[] = { 1180136db58SBen Widawsky &dev_attr_rc6_enable.attr, 1190136db58SBen Widawsky &dev_attr_rc6_residency_ms.attr, 1200136db58SBen Widawsky NULL 1210136db58SBen Widawsky }; 1220136db58SBen Widawsky 1230136db58SBen Widawsky static struct attribute_group rc6_attr_group = { 1240136db58SBen Widawsky .name = power_group_name, 1250136db58SBen Widawsky .attrs = rc6_attrs 1260136db58SBen Widawsky }; 12758abf1daSRodrigo Vivi 12858abf1daSRodrigo Vivi static struct attribute *rc6p_attrs[] = { 12958abf1daSRodrigo Vivi &dev_attr_rc6p_residency_ms.attr, 13058abf1daSRodrigo Vivi &dev_attr_rc6pp_residency_ms.attr, 13158abf1daSRodrigo Vivi NULL 13258abf1daSRodrigo Vivi }; 13358abf1daSRodrigo Vivi 13458abf1daSRodrigo Vivi static struct attribute_group rc6p_attr_group = { 13558abf1daSRodrigo Vivi .name = power_group_name, 13658abf1daSRodrigo Vivi .attrs = rc6p_attrs 13758abf1daSRodrigo Vivi }; 138626ad6f3SVille Syrjälä 139626ad6f3SVille Syrjälä static struct attribute *media_rc6_attrs[] = { 140626ad6f3SVille Syrjälä &dev_attr_media_rc6_residency_ms.attr, 141626ad6f3SVille Syrjälä NULL 142626ad6f3SVille Syrjälä }; 143626ad6f3SVille Syrjälä 144626ad6f3SVille Syrjälä static struct attribute_group media_rc6_attr_group = { 145626ad6f3SVille Syrjälä .name = power_group_name, 146626ad6f3SVille Syrjälä .attrs = media_rc6_attrs 147626ad6f3SVille Syrjälä }; 1488c3f929bSBen Widawsky #endif 1490136db58SBen Widawsky 150694c2828SDavid Weinehall static int l3_access_valid(struct drm_i915_private *dev_priv, loff_t offset) 15184bc7581SBen Widawsky { 152694c2828SDavid Weinehall if (!HAS_L3_DPF(dev_priv)) 15384bc7581SBen Widawsky return -EPERM; 15484bc7581SBen Widawsky 15584bc7581SBen Widawsky if (offset % 4 != 0) 15684bc7581SBen Widawsky return -EINVAL; 15784bc7581SBen Widawsky 15884bc7581SBen Widawsky if (offset >= GEN7_L3LOG_SIZE) 15984bc7581SBen Widawsky return -ENXIO; 16084bc7581SBen Widawsky 16184bc7581SBen Widawsky return 0; 16284bc7581SBen Widawsky } 16384bc7581SBen Widawsky 16484bc7581SBen Widawsky static ssize_t 16584bc7581SBen Widawsky i915_l3_read(struct file *filp, struct kobject *kobj, 16684bc7581SBen Widawsky struct bin_attribute *attr, char *buf, 16784bc7581SBen Widawsky loff_t offset, size_t count) 16884bc7581SBen Widawsky { 169c49d13eeSDavid Weinehall struct device *kdev = kobj_to_dev(kobj); 170694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 171694c2828SDavid Weinehall struct drm_device *dev = &dev_priv->drm; 17235a85ac6SBen Widawsky int slice = (int)(uintptr_t)attr->private; 1733ccfd19dSBen Widawsky int ret; 17484bc7581SBen Widawsky 1751c3dcd1cSBen Widawsky count = round_down(count, 4); 1761c3dcd1cSBen Widawsky 177694c2828SDavid Weinehall ret = l3_access_valid(dev_priv, offset); 17884bc7581SBen Widawsky if (ret) 17984bc7581SBen Widawsky return ret; 18084bc7581SBen Widawsky 181e5ad4026SDan Carpenter count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count); 18233618ea5SBen Widawsky 183c49d13eeSDavid Weinehall ret = i915_mutex_lock_interruptible(dev); 18484bc7581SBen Widawsky if (ret) 18584bc7581SBen Widawsky return ret; 18684bc7581SBen Widawsky 18735a85ac6SBen Widawsky if (dev_priv->l3_parity.remap_info[slice]) 1881c966dd2SBen Widawsky memcpy(buf, 18935a85ac6SBen Widawsky dev_priv->l3_parity.remap_info[slice] + (offset/4), 1901c966dd2SBen Widawsky count); 1911c966dd2SBen Widawsky else 1921c966dd2SBen Widawsky memset(buf, 0, count); 1931c966dd2SBen Widawsky 194c49d13eeSDavid Weinehall mutex_unlock(&dev->struct_mutex); 19584bc7581SBen Widawsky 1961c966dd2SBen Widawsky return count; 19784bc7581SBen Widawsky } 19884bc7581SBen Widawsky 19984bc7581SBen Widawsky static ssize_t 20084bc7581SBen Widawsky i915_l3_write(struct file *filp, struct kobject *kobj, 20184bc7581SBen Widawsky struct bin_attribute *attr, char *buf, 20284bc7581SBen Widawsky loff_t offset, size_t count) 20384bc7581SBen Widawsky { 204c49d13eeSDavid Weinehall struct device *kdev = kobj_to_dev(kobj); 205694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 206694c2828SDavid Weinehall struct drm_device *dev = &dev_priv->drm; 207e2efd130SChris Wilson struct i915_gem_context *ctx; 20884bc7581SBen Widawsky u32 *temp = NULL; /* Just here to make handling failures easy */ 20935a85ac6SBen Widawsky int slice = (int)(uintptr_t)attr->private; 21084bc7581SBen Widawsky int ret; 21184bc7581SBen Widawsky 212694c2828SDavid Weinehall if (!HAS_HW_CONTEXTS(dev_priv)) 2138245be31SBen Widawsky return -ENXIO; 2148245be31SBen Widawsky 215694c2828SDavid Weinehall ret = l3_access_valid(dev_priv, offset); 21684bc7581SBen Widawsky if (ret) 21784bc7581SBen Widawsky return ret; 21884bc7581SBen Widawsky 219c49d13eeSDavid Weinehall ret = i915_mutex_lock_interruptible(dev); 22084bc7581SBen Widawsky if (ret) 22184bc7581SBen Widawsky return ret; 22284bc7581SBen Widawsky 22335a85ac6SBen Widawsky if (!dev_priv->l3_parity.remap_info[slice]) { 22484bc7581SBen Widawsky temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL); 22584bc7581SBen Widawsky if (!temp) { 226c49d13eeSDavid Weinehall mutex_unlock(&dev->struct_mutex); 22784bc7581SBen Widawsky return -ENOMEM; 22884bc7581SBen Widawsky } 22984bc7581SBen Widawsky } 23084bc7581SBen Widawsky 23184bc7581SBen Widawsky /* TODO: Ideally we really want a GPU reset here to make sure errors 23284bc7581SBen Widawsky * aren't propagated. Since I cannot find a stable way to reset the GPU 23384bc7581SBen Widawsky * at this point it is left as a TODO. 23484bc7581SBen Widawsky */ 23584bc7581SBen Widawsky if (temp) 23635a85ac6SBen Widawsky dev_priv->l3_parity.remap_info[slice] = temp; 23784bc7581SBen Widawsky 23835a85ac6SBen Widawsky memcpy(dev_priv->l3_parity.remap_info[slice] + (offset/4), buf, count); 23984bc7581SBen Widawsky 2403ccfd19dSBen Widawsky /* NB: We defer the remapping until we switch to the context */ 2413ccfd19dSBen Widawsky list_for_each_entry(ctx, &dev_priv->context_list, link) 2423ccfd19dSBen Widawsky ctx->remap_slice |= (1<<slice); 24384bc7581SBen Widawsky 244c49d13eeSDavid Weinehall mutex_unlock(&dev->struct_mutex); 24584bc7581SBen Widawsky 24684bc7581SBen Widawsky return count; 24784bc7581SBen Widawsky } 24884bc7581SBen Widawsky 24984bc7581SBen Widawsky static struct bin_attribute dpf_attrs = { 25084bc7581SBen Widawsky .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)}, 25184bc7581SBen Widawsky .size = GEN7_L3LOG_SIZE, 25284bc7581SBen Widawsky .read = i915_l3_read, 25384bc7581SBen Widawsky .write = i915_l3_write, 25435a85ac6SBen Widawsky .mmap = NULL, 25535a85ac6SBen Widawsky .private = (void *)0 25635a85ac6SBen Widawsky }; 25735a85ac6SBen Widawsky 25835a85ac6SBen Widawsky static struct bin_attribute dpf_attrs_1 = { 25935a85ac6SBen Widawsky .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)}, 26035a85ac6SBen Widawsky .size = GEN7_L3LOG_SIZE, 26135a85ac6SBen Widawsky .read = i915_l3_read, 26235a85ac6SBen Widawsky .write = i915_l3_write, 26335a85ac6SBen Widawsky .mmap = NULL, 26435a85ac6SBen Widawsky .private = (void *)1 26584bc7581SBen Widawsky }; 26684bc7581SBen Widawsky 267c8c972ebSVille Syrjälä static ssize_t gt_act_freq_mhz_show(struct device *kdev, 268df6eedc8SBen Widawsky struct device_attribute *attr, char *buf) 269df6eedc8SBen Widawsky { 270694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 271df6eedc8SBen Widawsky int ret; 272df6eedc8SBen Widawsky 273d46c0517SImre Deak intel_runtime_pm_get(dev_priv); 274d46c0517SImre Deak 2754fc688ceSJesse Barnes mutex_lock(&dev_priv->rps.hw_lock); 276666a4537SWayne Boyer if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) { 277177006a1SJesse Barnes u32 freq; 27864936258SJani Nikula freq = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS); 2797c59a9c1SVille Syrjälä ret = intel_gpu_freq(dev_priv, (freq >> 8) & 0xff); 280177006a1SJesse Barnes } else { 281c8c972ebSVille Syrjälä u32 rpstat = I915_READ(GEN6_RPSTAT1); 282ed64d66fSAkash Goel if (IS_GEN9(dev_priv)) 283ed64d66fSAkash Goel ret = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT; 284ed64d66fSAkash Goel else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) 285c8c972ebSVille Syrjälä ret = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT; 286c8c972ebSVille Syrjälä else 287c8c972ebSVille Syrjälä ret = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT; 2887c59a9c1SVille Syrjälä ret = intel_gpu_freq(dev_priv, ret); 289c8c972ebSVille Syrjälä } 290c8c972ebSVille Syrjälä mutex_unlock(&dev_priv->rps.hw_lock); 291c8c972ebSVille Syrjälä 292c8c972ebSVille Syrjälä intel_runtime_pm_put(dev_priv); 293c8c972ebSVille Syrjälä 294c8c972ebSVille Syrjälä return snprintf(buf, PAGE_SIZE, "%d\n", ret); 295c8c972ebSVille Syrjälä } 296c8c972ebSVille Syrjälä 297c8c972ebSVille Syrjälä static ssize_t gt_cur_freq_mhz_show(struct device *kdev, 298c8c972ebSVille Syrjälä struct device_attribute *attr, char *buf) 299c8c972ebSVille Syrjälä { 300694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 301c8c972ebSVille Syrjälä 30262e1baa1SChris Wilson return snprintf(buf, PAGE_SIZE, "%d\n", 30362e1baa1SChris Wilson intel_gpu_freq(dev_priv, 30462e1baa1SChris Wilson dev_priv->rps.cur_freq)); 305df6eedc8SBen Widawsky } 306df6eedc8SBen Widawsky 30729ecd78dSChris Wilson static ssize_t gt_boost_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf) 30829ecd78dSChris Wilson { 309694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 31029ecd78dSChris Wilson 31129ecd78dSChris Wilson return snprintf(buf, PAGE_SIZE, "%d\n", 31262e1baa1SChris Wilson intel_gpu_freq(dev_priv, 31362e1baa1SChris Wilson dev_priv->rps.boost_freq)); 31429ecd78dSChris Wilson } 31529ecd78dSChris Wilson 31629ecd78dSChris Wilson static ssize_t gt_boost_freq_mhz_store(struct device *kdev, 31729ecd78dSChris Wilson struct device_attribute *attr, 31829ecd78dSChris Wilson const char *buf, size_t count) 31929ecd78dSChris Wilson { 320694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 32129ecd78dSChris Wilson u32 val; 32229ecd78dSChris Wilson ssize_t ret; 32329ecd78dSChris Wilson 32429ecd78dSChris Wilson ret = kstrtou32(buf, 0, &val); 32529ecd78dSChris Wilson if (ret) 32629ecd78dSChris Wilson return ret; 32729ecd78dSChris Wilson 32829ecd78dSChris Wilson /* Validate against (static) hardware limits */ 32929ecd78dSChris Wilson val = intel_freq_opcode(dev_priv, val); 33029ecd78dSChris Wilson if (val < dev_priv->rps.min_freq || val > dev_priv->rps.max_freq) 33129ecd78dSChris Wilson return -EINVAL; 33229ecd78dSChris Wilson 33329ecd78dSChris Wilson mutex_lock(&dev_priv->rps.hw_lock); 33429ecd78dSChris Wilson dev_priv->rps.boost_freq = val; 33529ecd78dSChris Wilson mutex_unlock(&dev_priv->rps.hw_lock); 33629ecd78dSChris Wilson 33729ecd78dSChris Wilson return count; 33829ecd78dSChris Wilson } 33929ecd78dSChris Wilson 34097e4eed7SChris Wilson static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev, 34197e4eed7SChris Wilson struct device_attribute *attr, char *buf) 34297e4eed7SChris Wilson { 343694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 34497e4eed7SChris Wilson 34562e1baa1SChris Wilson return snprintf(buf, PAGE_SIZE, "%d\n", 34662e1baa1SChris Wilson intel_gpu_freq(dev_priv, 34762e1baa1SChris Wilson dev_priv->rps.efficient_freq)); 34897e4eed7SChris Wilson } 34997e4eed7SChris Wilson 350df6eedc8SBen Widawsky static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf) 351df6eedc8SBen Widawsky { 352694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 353df6eedc8SBen Widawsky 35462e1baa1SChris Wilson return snprintf(buf, PAGE_SIZE, "%d\n", 35562e1baa1SChris Wilson intel_gpu_freq(dev_priv, 35662e1baa1SChris Wilson dev_priv->rps.max_freq_softlimit)); 357df6eedc8SBen Widawsky } 358df6eedc8SBen Widawsky 35946ddf194SBen Widawsky static ssize_t gt_max_freq_mhz_store(struct device *kdev, 36046ddf194SBen Widawsky struct device_attribute *attr, 36146ddf194SBen Widawsky const char *buf, size_t count) 36246ddf194SBen Widawsky { 363694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 3642a5913a8SBen Widawsky u32 val; 36546ddf194SBen Widawsky ssize_t ret; 36646ddf194SBen Widawsky 36746ddf194SBen Widawsky ret = kstrtou32(buf, 0, &val); 36846ddf194SBen Widawsky if (ret) 36946ddf194SBen Widawsky return ret; 37046ddf194SBen Widawsky 371933bfb44SSagar Arun Kamble intel_runtime_pm_get(dev_priv); 372933bfb44SSagar Arun Kamble 3734fc688ceSJesse Barnes mutex_lock(&dev_priv->rps.hw_lock); 37446ddf194SBen Widawsky 3757c59a9c1SVille Syrjälä val = intel_freq_opcode(dev_priv, val); 3760a073b84SJesse Barnes 3772a5913a8SBen Widawsky if (val < dev_priv->rps.min_freq || 3782a5913a8SBen Widawsky val > dev_priv->rps.max_freq || 379b39fb297SBen Widawsky val < dev_priv->rps.min_freq_softlimit) { 3804fc688ceSJesse Barnes mutex_unlock(&dev_priv->rps.hw_lock); 381933bfb44SSagar Arun Kamble intel_runtime_pm_put(dev_priv); 38246ddf194SBen Widawsky return -EINVAL; 38346ddf194SBen Widawsky } 38446ddf194SBen Widawsky 3852a5913a8SBen Widawsky if (val > dev_priv->rps.rp0_freq) 38631c77388SBen Widawsky DRM_DEBUG("User requested overclocking to %d\n", 3877c59a9c1SVille Syrjälä intel_gpu_freq(dev_priv, val)); 38831c77388SBen Widawsky 389b39fb297SBen Widawsky dev_priv->rps.max_freq_softlimit = val; 39046ddf194SBen Widawsky 391f745a80eSVille Syrjälä val = clamp_t(int, dev_priv->rps.cur_freq, 392f745a80eSVille Syrjälä dev_priv->rps.min_freq_softlimit, 393f745a80eSVille Syrjälä dev_priv->rps.max_freq_softlimit); 394f745a80eSVille Syrjälä 395f745a80eSVille Syrjälä /* We still need *_set_rps to process the new max_delay and 396f745a80eSVille Syrjälä * update the interrupt limits and PMINTRMSK even though 397f745a80eSVille Syrjälä * frequency request may be unchanged. */ 3989fcee2f7SChris Wilson ret = intel_set_rps(dev_priv, val); 3996917c7b9SChris Wilson 4004fc688ceSJesse Barnes mutex_unlock(&dev_priv->rps.hw_lock); 40146ddf194SBen Widawsky 402933bfb44SSagar Arun Kamble intel_runtime_pm_put(dev_priv); 403933bfb44SSagar Arun Kamble 4049fcee2f7SChris Wilson return ret ?: count; 40546ddf194SBen Widawsky } 40646ddf194SBen Widawsky 407df6eedc8SBen Widawsky static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf) 408df6eedc8SBen Widawsky { 409694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 410df6eedc8SBen Widawsky 41162e1baa1SChris Wilson return snprintf(buf, PAGE_SIZE, "%d\n", 41262e1baa1SChris Wilson intel_gpu_freq(dev_priv, 41362e1baa1SChris Wilson dev_priv->rps.min_freq_softlimit)); 414df6eedc8SBen Widawsky } 415df6eedc8SBen Widawsky 41646ddf194SBen Widawsky static ssize_t gt_min_freq_mhz_store(struct device *kdev, 41746ddf194SBen Widawsky struct device_attribute *attr, 41846ddf194SBen Widawsky const char *buf, size_t count) 41946ddf194SBen Widawsky { 420694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 4212a5913a8SBen Widawsky u32 val; 42246ddf194SBen Widawsky ssize_t ret; 42346ddf194SBen Widawsky 42446ddf194SBen Widawsky ret = kstrtou32(buf, 0, &val); 42546ddf194SBen Widawsky if (ret) 42646ddf194SBen Widawsky return ret; 42746ddf194SBen Widawsky 428933bfb44SSagar Arun Kamble intel_runtime_pm_get(dev_priv); 429933bfb44SSagar Arun Kamble 4304fc688ceSJesse Barnes mutex_lock(&dev_priv->rps.hw_lock); 43146ddf194SBen Widawsky 4327c59a9c1SVille Syrjälä val = intel_freq_opcode(dev_priv, val); 4330a073b84SJesse Barnes 4342a5913a8SBen Widawsky if (val < dev_priv->rps.min_freq || 4352a5913a8SBen Widawsky val > dev_priv->rps.max_freq || 4362a5913a8SBen Widawsky val > dev_priv->rps.max_freq_softlimit) { 4374fc688ceSJesse Barnes mutex_unlock(&dev_priv->rps.hw_lock); 438933bfb44SSagar Arun Kamble intel_runtime_pm_put(dev_priv); 43946ddf194SBen Widawsky return -EINVAL; 44046ddf194SBen Widawsky } 44146ddf194SBen Widawsky 442b39fb297SBen Widawsky dev_priv->rps.min_freq_softlimit = val; 4436917c7b9SChris Wilson 444f745a80eSVille Syrjälä val = clamp_t(int, dev_priv->rps.cur_freq, 445f745a80eSVille Syrjälä dev_priv->rps.min_freq_softlimit, 446f745a80eSVille Syrjälä dev_priv->rps.max_freq_softlimit); 447f745a80eSVille Syrjälä 448f745a80eSVille Syrjälä /* We still need *_set_rps to process the new min_delay and 449f745a80eSVille Syrjälä * update the interrupt limits and PMINTRMSK even though 450f745a80eSVille Syrjälä * frequency request may be unchanged. */ 4519fcee2f7SChris Wilson ret = intel_set_rps(dev_priv, val); 45246ddf194SBen Widawsky 4534fc688ceSJesse Barnes mutex_unlock(&dev_priv->rps.hw_lock); 45446ddf194SBen Widawsky 455933bfb44SSagar Arun Kamble intel_runtime_pm_put(dev_priv); 456933bfb44SSagar Arun Kamble 4579fcee2f7SChris Wilson return ret ?: count; 45846ddf194SBen Widawsky } 45946ddf194SBen Widawsky 460c8c972ebSVille Syrjälä static DEVICE_ATTR(gt_act_freq_mhz, S_IRUGO, gt_act_freq_mhz_show, NULL); 461df6eedc8SBen Widawsky static DEVICE_ATTR(gt_cur_freq_mhz, S_IRUGO, gt_cur_freq_mhz_show, NULL); 46273a79871SMika Kuoppala static DEVICE_ATTR(gt_boost_freq_mhz, S_IRUGO | S_IWUSR, gt_boost_freq_mhz_show, gt_boost_freq_mhz_store); 46346ddf194SBen Widawsky static DEVICE_ATTR(gt_max_freq_mhz, S_IRUGO | S_IWUSR, gt_max_freq_mhz_show, gt_max_freq_mhz_store); 46446ddf194SBen Widawsky static DEVICE_ATTR(gt_min_freq_mhz, S_IRUGO | S_IWUSR, gt_min_freq_mhz_show, gt_min_freq_mhz_store); 465df6eedc8SBen Widawsky 46697e4eed7SChris Wilson static DEVICE_ATTR(vlv_rpe_freq_mhz, S_IRUGO, vlv_rpe_freq_mhz_show, NULL); 467ac6ae347SBen Widawsky 468ac6ae347SBen Widawsky static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf); 469ac6ae347SBen Widawsky static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL); 470ac6ae347SBen Widawsky static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL); 471ac6ae347SBen Widawsky static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL); 472ac6ae347SBen Widawsky 473ac6ae347SBen Widawsky /* For now we have a static number of RP states */ 474ac6ae347SBen Widawsky static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf) 475ac6ae347SBen Widawsky { 476694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 477bc4d91f6SAkash Goel u32 val; 478ac6ae347SBen Widawsky 479bc4d91f6SAkash Goel if (attr == &dev_attr_gt_RP0_freq_mhz) 4807c59a9c1SVille Syrjälä val = intel_gpu_freq(dev_priv, dev_priv->rps.rp0_freq); 481bc4d91f6SAkash Goel else if (attr == &dev_attr_gt_RP1_freq_mhz) 4827c59a9c1SVille Syrjälä val = intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq); 483bc4d91f6SAkash Goel else if (attr == &dev_attr_gt_RPn_freq_mhz) 4847c59a9c1SVille Syrjälä val = intel_gpu_freq(dev_priv, dev_priv->rps.min_freq); 48574c4f62bSDeepak S else 486ac6ae347SBen Widawsky BUG(); 487bc4d91f6SAkash Goel 4883e2a1556SJani Nikula return snprintf(buf, PAGE_SIZE, "%d\n", val); 489ac6ae347SBen Widawsky } 490ac6ae347SBen Widawsky 491df6eedc8SBen Widawsky static const struct attribute *gen6_attrs[] = { 492c8c972ebSVille Syrjälä &dev_attr_gt_act_freq_mhz.attr, 493df6eedc8SBen Widawsky &dev_attr_gt_cur_freq_mhz.attr, 49429ecd78dSChris Wilson &dev_attr_gt_boost_freq_mhz.attr, 495df6eedc8SBen Widawsky &dev_attr_gt_max_freq_mhz.attr, 496df6eedc8SBen Widawsky &dev_attr_gt_min_freq_mhz.attr, 497ac6ae347SBen Widawsky &dev_attr_gt_RP0_freq_mhz.attr, 498ac6ae347SBen Widawsky &dev_attr_gt_RP1_freq_mhz.attr, 499ac6ae347SBen Widawsky &dev_attr_gt_RPn_freq_mhz.attr, 500df6eedc8SBen Widawsky NULL, 501df6eedc8SBen Widawsky }; 502df6eedc8SBen Widawsky 50397e4eed7SChris Wilson static const struct attribute *vlv_attrs[] = { 504c8c972ebSVille Syrjälä &dev_attr_gt_act_freq_mhz.attr, 50597e4eed7SChris Wilson &dev_attr_gt_cur_freq_mhz.attr, 50629ecd78dSChris Wilson &dev_attr_gt_boost_freq_mhz.attr, 50797e4eed7SChris Wilson &dev_attr_gt_max_freq_mhz.attr, 50897e4eed7SChris Wilson &dev_attr_gt_min_freq_mhz.attr, 50974c4f62bSDeepak S &dev_attr_gt_RP0_freq_mhz.attr, 51074c4f62bSDeepak S &dev_attr_gt_RP1_freq_mhz.attr, 51174c4f62bSDeepak S &dev_attr_gt_RPn_freq_mhz.attr, 51297e4eed7SChris Wilson &dev_attr_vlv_rpe_freq_mhz.attr, 51397e4eed7SChris Wilson NULL, 51497e4eed7SChris Wilson }; 51597e4eed7SChris Wilson 51698a2f411SChris Wilson #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR) 51798a2f411SChris Wilson 518ef86ddceSMika Kuoppala static ssize_t error_state_read(struct file *filp, struct kobject *kobj, 519ef86ddceSMika Kuoppala struct bin_attribute *attr, char *buf, 520ef86ddceSMika Kuoppala loff_t off, size_t count) 521ef86ddceSMika Kuoppala { 522ef86ddceSMika Kuoppala 523657fb5fbSGeliang Tang struct device *kdev = kobj_to_dev(kobj); 524694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 525ef86ddceSMika Kuoppala struct drm_i915_error_state_buf error_str; 526*5a4c6f1bSChris Wilson struct i915_gpu_state *gpu; 527*5a4c6f1bSChris Wilson ssize_t ret; 528ef86ddceSMika Kuoppala 529*5a4c6f1bSChris Wilson ret = i915_error_state_buf_init(&error_str, dev_priv, count, off); 530ef86ddceSMika Kuoppala if (ret) 531ef86ddceSMika Kuoppala return ret; 532ef86ddceSMika Kuoppala 533*5a4c6f1bSChris Wilson gpu = i915_first_error_state(dev_priv); 534*5a4c6f1bSChris Wilson ret = i915_error_state_to_str(&error_str, gpu); 535ef86ddceSMika Kuoppala if (ret) 536ef86ddceSMika Kuoppala goto out; 537ef86ddceSMika Kuoppala 538*5a4c6f1bSChris Wilson ret = count < error_str.bytes ? count : error_str.bytes; 539*5a4c6f1bSChris Wilson memcpy(buf, error_str.buf, ret); 540ef86ddceSMika Kuoppala 541ef86ddceSMika Kuoppala out: 542*5a4c6f1bSChris Wilson i915_gpu_state_put(gpu); 543ef86ddceSMika Kuoppala i915_error_state_buf_release(&error_str); 544ef86ddceSMika Kuoppala 545*5a4c6f1bSChris Wilson return ret; 546ef86ddceSMika Kuoppala } 547ef86ddceSMika Kuoppala 548ef86ddceSMika Kuoppala static ssize_t error_state_write(struct file *file, struct kobject *kobj, 549ef86ddceSMika Kuoppala struct bin_attribute *attr, char *buf, 550ef86ddceSMika Kuoppala loff_t off, size_t count) 551ef86ddceSMika Kuoppala { 552657fb5fbSGeliang Tang struct device *kdev = kobj_to_dev(kobj); 553694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 554ef86ddceSMika Kuoppala 555ef86ddceSMika Kuoppala DRM_DEBUG_DRIVER("Resetting error state\n"); 556*5a4c6f1bSChris Wilson i915_reset_error_state(dev_priv); 557ef86ddceSMika Kuoppala 558ef86ddceSMika Kuoppala return count; 559ef86ddceSMika Kuoppala } 560ef86ddceSMika Kuoppala 561ef86ddceSMika Kuoppala static struct bin_attribute error_state_attr = { 562ef86ddceSMika Kuoppala .attr.name = "error", 563ef86ddceSMika Kuoppala .attr.mode = S_IRUSR | S_IWUSR, 564ef86ddceSMika Kuoppala .size = 0, 565ef86ddceSMika Kuoppala .read = error_state_read, 566ef86ddceSMika Kuoppala .write = error_state_write, 567ef86ddceSMika Kuoppala }; 568ef86ddceSMika Kuoppala 56998a2f411SChris Wilson static void i915_setup_error_capture(struct device *kdev) 57098a2f411SChris Wilson { 57198a2f411SChris Wilson if (sysfs_create_bin_file(&kdev->kobj, &error_state_attr)) 57298a2f411SChris Wilson DRM_ERROR("error_state sysfs setup failed\n"); 57398a2f411SChris Wilson } 57498a2f411SChris Wilson 57598a2f411SChris Wilson static void i915_teardown_error_capture(struct device *kdev) 57698a2f411SChris Wilson { 57798a2f411SChris Wilson sysfs_remove_bin_file(&kdev->kobj, &error_state_attr); 57898a2f411SChris Wilson } 57998a2f411SChris Wilson #else 58098a2f411SChris Wilson static void i915_setup_error_capture(struct device *kdev) {} 58198a2f411SChris Wilson static void i915_teardown_error_capture(struct device *kdev) {} 58298a2f411SChris Wilson #endif 58398a2f411SChris Wilson 584694c2828SDavid Weinehall void i915_setup_sysfs(struct drm_i915_private *dev_priv) 5850136db58SBen Widawsky { 586694c2828SDavid Weinehall struct device *kdev = dev_priv->drm.primary->kdev; 5870136db58SBen Widawsky int ret; 5880136db58SBen Widawsky 5898c3f929bSBen Widawsky #ifdef CONFIG_PM 590694c2828SDavid Weinehall if (HAS_RC6(dev_priv)) { 591694c2828SDavid Weinehall ret = sysfs_merge_group(&kdev->kobj, 592112abd29SDaniel Vetter &rc6_attr_group); 5930136db58SBen Widawsky if (ret) 59484bc7581SBen Widawsky DRM_ERROR("RC6 residency sysfs setup failed\n"); 595112abd29SDaniel Vetter } 596694c2828SDavid Weinehall if (HAS_RC6p(dev_priv)) { 597694c2828SDavid Weinehall ret = sysfs_merge_group(&kdev->kobj, 59858abf1daSRodrigo Vivi &rc6p_attr_group); 59958abf1daSRodrigo Vivi if (ret) 60058abf1daSRodrigo Vivi DRM_ERROR("RC6p residency sysfs setup failed\n"); 60158abf1daSRodrigo Vivi } 602694c2828SDavid Weinehall if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) { 603694c2828SDavid Weinehall ret = sysfs_merge_group(&kdev->kobj, 604626ad6f3SVille Syrjälä &media_rc6_attr_group); 605626ad6f3SVille Syrjälä if (ret) 606626ad6f3SVille Syrjälä DRM_ERROR("Media RC6 residency sysfs setup failed\n"); 607626ad6f3SVille Syrjälä } 6088c3f929bSBen Widawsky #endif 609694c2828SDavid Weinehall if (HAS_L3_DPF(dev_priv)) { 610694c2828SDavid Weinehall ret = device_create_bin_file(kdev, &dpf_attrs); 61184bc7581SBen Widawsky if (ret) 61284bc7581SBen Widawsky DRM_ERROR("l3 parity sysfs setup failed\n"); 61335a85ac6SBen Widawsky 614694c2828SDavid Weinehall if (NUM_L3_SLICES(dev_priv) > 1) { 615694c2828SDavid Weinehall ret = device_create_bin_file(kdev, 61635a85ac6SBen Widawsky &dpf_attrs_1); 61735a85ac6SBen Widawsky if (ret) 61835a85ac6SBen Widawsky DRM_ERROR("l3 parity slice 1 setup failed\n"); 61935a85ac6SBen Widawsky } 6200136db58SBen Widawsky } 621df6eedc8SBen Widawsky 62297e4eed7SChris Wilson ret = 0; 623694c2828SDavid Weinehall if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) 624694c2828SDavid Weinehall ret = sysfs_create_files(&kdev->kobj, vlv_attrs); 625694c2828SDavid Weinehall else if (INTEL_GEN(dev_priv) >= 6) 626694c2828SDavid Weinehall ret = sysfs_create_files(&kdev->kobj, gen6_attrs); 627df6eedc8SBen Widawsky if (ret) 62897e4eed7SChris Wilson DRM_ERROR("RPS sysfs setup failed\n"); 629ef86ddceSMika Kuoppala 63098a2f411SChris Wilson i915_setup_error_capture(kdev); 631112abd29SDaniel Vetter } 6320136db58SBen Widawsky 633694c2828SDavid Weinehall void i915_teardown_sysfs(struct drm_i915_private *dev_priv) 6340136db58SBen Widawsky { 635694c2828SDavid Weinehall struct device *kdev = dev_priv->drm.primary->kdev; 636694c2828SDavid Weinehall 63798a2f411SChris Wilson i915_teardown_error_capture(kdev); 63898a2f411SChris Wilson 639694c2828SDavid Weinehall if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) 640694c2828SDavid Weinehall sysfs_remove_files(&kdev->kobj, vlv_attrs); 64197e4eed7SChris Wilson else 642694c2828SDavid Weinehall sysfs_remove_files(&kdev->kobj, gen6_attrs); 643694c2828SDavid Weinehall device_remove_bin_file(kdev, &dpf_attrs_1); 644694c2828SDavid Weinehall device_remove_bin_file(kdev, &dpf_attrs); 645853c70e8SBen Widawsky #ifdef CONFIG_PM 646694c2828SDavid Weinehall sysfs_unmerge_group(&kdev->kobj, &rc6_attr_group); 647694c2828SDavid Weinehall sysfs_unmerge_group(&kdev->kobj, &rc6p_attr_group); 648853c70e8SBen Widawsky #endif 6490136db58SBen Widawsky } 650