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> 32*56c5098fSChris Wilson 3384bc7581SBen Widawsky #include "intel_drv.h" 34*56c5098fSChris Wilson #include "intel_sideband.h" 350136db58SBen Widawsky #include "i915_drv.h" 360136db58SBen Widawsky 37694c2828SDavid Weinehall static inline struct drm_i915_private *kdev_minor_to_i915(struct device *kdev) 38c49d13eeSDavid Weinehall { 39694c2828SDavid Weinehall struct drm_minor *minor = dev_get_drvdata(kdev); 40694c2828SDavid Weinehall return to_i915(minor->dev); 41c49d13eeSDavid Weinehall } 4214c8d110SDave Airlie 435ab3633dSHunt Xu #ifdef CONFIG_PM 44694c2828SDavid Weinehall static u32 calc_residency(struct drm_i915_private *dev_priv, 45f0f59a00SVille Syrjälä i915_reg_t reg) 460136db58SBen Widawsky { 4748d1c812SChris Wilson intel_wakeref_t wakeref; 48d4225a53SChris Wilson u64 res = 0; 4936cc8b96STvrtko Ursulin 50d4225a53SChris Wilson with_intel_runtime_pm(dev_priv, wakeref) 5136cc8b96STvrtko Ursulin res = intel_rc6_residency_us(dev_priv, reg); 5236cc8b96STvrtko Ursulin 5336cc8b96STvrtko Ursulin return DIV_ROUND_CLOSEST_ULL(res, 1000); 540136db58SBen Widawsky } 550136db58SBen Widawsky 560136db58SBen Widawsky static ssize_t 57dbdfd8e9SBen Widawsky show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf) 580136db58SBen Widawsky { 59fb6db0f5SChris Wilson struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 60fb6db0f5SChris Wilson unsigned int mask; 61fb6db0f5SChris Wilson 62fb6db0f5SChris Wilson mask = 0; 63fb6db0f5SChris Wilson if (HAS_RC6(dev_priv)) 64fb6db0f5SChris Wilson mask |= BIT(0); 65fb6db0f5SChris Wilson if (HAS_RC6p(dev_priv)) 66fb6db0f5SChris Wilson mask |= BIT(1); 67fb6db0f5SChris Wilson if (HAS_RC6pp(dev_priv)) 68fb6db0f5SChris Wilson mask |= BIT(2); 69fb6db0f5SChris Wilson 70fb6db0f5SChris Wilson return snprintf(buf, PAGE_SIZE, "%x\n", mask); 710136db58SBen Widawsky } 720136db58SBen Widawsky 730136db58SBen Widawsky static ssize_t 74dbdfd8e9SBen Widawsky show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf) 750136db58SBen Widawsky { 76694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 77694c2828SDavid Weinehall u32 rc6_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6); 783e2a1556SJani Nikula return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency); 790136db58SBen Widawsky } 800136db58SBen Widawsky 810136db58SBen Widawsky static ssize_t 82dbdfd8e9SBen Widawsky show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf) 830136db58SBen Widawsky { 84694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 85694c2828SDavid Weinehall u32 rc6p_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6p); 863e2a1556SJani Nikula return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency); 870136db58SBen Widawsky } 880136db58SBen Widawsky 890136db58SBen Widawsky static ssize_t 90dbdfd8e9SBen Widawsky show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf) 910136db58SBen Widawsky { 92694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 93694c2828SDavid Weinehall u32 rc6pp_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6pp); 943e2a1556SJani Nikula return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency); 950136db58SBen Widawsky } 960136db58SBen Widawsky 97626ad6f3SVille Syrjälä static ssize_t 98626ad6f3SVille Syrjälä show_media_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf) 99626ad6f3SVille Syrjälä { 100694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 101694c2828SDavid Weinehall u32 rc6_residency = calc_residency(dev_priv, VLV_GT_MEDIA_RC6); 102626ad6f3SVille Syrjälä return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency); 103626ad6f3SVille Syrjälä } 104626ad6f3SVille Syrjälä 1050136db58SBen Widawsky static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL); 1060136db58SBen Widawsky static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL); 1070136db58SBen Widawsky static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL); 1080136db58SBen Widawsky static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL); 109626ad6f3SVille Syrjälä static DEVICE_ATTR(media_rc6_residency_ms, S_IRUGO, show_media_rc6_ms, NULL); 1100136db58SBen Widawsky 1110136db58SBen Widawsky static struct attribute *rc6_attrs[] = { 1120136db58SBen Widawsky &dev_attr_rc6_enable.attr, 1130136db58SBen Widawsky &dev_attr_rc6_residency_ms.attr, 1140136db58SBen Widawsky NULL 1150136db58SBen Widawsky }; 1160136db58SBen Widawsky 1170a7a0986SArvind Yadav static const struct attribute_group rc6_attr_group = { 1180136db58SBen Widawsky .name = power_group_name, 1190136db58SBen Widawsky .attrs = rc6_attrs 1200136db58SBen Widawsky }; 12158abf1daSRodrigo Vivi 12258abf1daSRodrigo Vivi static struct attribute *rc6p_attrs[] = { 12358abf1daSRodrigo Vivi &dev_attr_rc6p_residency_ms.attr, 12458abf1daSRodrigo Vivi &dev_attr_rc6pp_residency_ms.attr, 12558abf1daSRodrigo Vivi NULL 12658abf1daSRodrigo Vivi }; 12758abf1daSRodrigo Vivi 1280a7a0986SArvind Yadav static const struct attribute_group rc6p_attr_group = { 12958abf1daSRodrigo Vivi .name = power_group_name, 13058abf1daSRodrigo Vivi .attrs = rc6p_attrs 13158abf1daSRodrigo Vivi }; 132626ad6f3SVille Syrjälä 133626ad6f3SVille Syrjälä static struct attribute *media_rc6_attrs[] = { 134626ad6f3SVille Syrjälä &dev_attr_media_rc6_residency_ms.attr, 135626ad6f3SVille Syrjälä NULL 136626ad6f3SVille Syrjälä }; 137626ad6f3SVille Syrjälä 1380a7a0986SArvind Yadav static const struct attribute_group media_rc6_attr_group = { 139626ad6f3SVille Syrjälä .name = power_group_name, 140626ad6f3SVille Syrjälä .attrs = media_rc6_attrs 141626ad6f3SVille Syrjälä }; 1428c3f929bSBen Widawsky #endif 1430136db58SBen Widawsky 144694c2828SDavid Weinehall static int l3_access_valid(struct drm_i915_private *dev_priv, loff_t offset) 14584bc7581SBen Widawsky { 146694c2828SDavid Weinehall if (!HAS_L3_DPF(dev_priv)) 14784bc7581SBen Widawsky return -EPERM; 14884bc7581SBen Widawsky 14984bc7581SBen Widawsky if (offset % 4 != 0) 15084bc7581SBen Widawsky return -EINVAL; 15184bc7581SBen Widawsky 15284bc7581SBen Widawsky if (offset >= GEN7_L3LOG_SIZE) 15384bc7581SBen Widawsky return -ENXIO; 15484bc7581SBen Widawsky 15584bc7581SBen Widawsky return 0; 15684bc7581SBen Widawsky } 15784bc7581SBen Widawsky 15884bc7581SBen Widawsky static ssize_t 15984bc7581SBen Widawsky i915_l3_read(struct file *filp, struct kobject *kobj, 16084bc7581SBen Widawsky struct bin_attribute *attr, char *buf, 16184bc7581SBen Widawsky loff_t offset, size_t count) 16284bc7581SBen Widawsky { 163c49d13eeSDavid Weinehall struct device *kdev = kobj_to_dev(kobj); 164694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 165694c2828SDavid Weinehall struct drm_device *dev = &dev_priv->drm; 16635a85ac6SBen Widawsky int slice = (int)(uintptr_t)attr->private; 1673ccfd19dSBen Widawsky int ret; 16884bc7581SBen Widawsky 1691c3dcd1cSBen Widawsky count = round_down(count, 4); 1701c3dcd1cSBen Widawsky 171694c2828SDavid Weinehall ret = l3_access_valid(dev_priv, offset); 17284bc7581SBen Widawsky if (ret) 17384bc7581SBen Widawsky return ret; 17484bc7581SBen Widawsky 175e5ad4026SDan Carpenter count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count); 17633618ea5SBen Widawsky 177c49d13eeSDavid Weinehall ret = i915_mutex_lock_interruptible(dev); 17884bc7581SBen Widawsky if (ret) 17984bc7581SBen Widawsky return ret; 18084bc7581SBen Widawsky 18135a85ac6SBen Widawsky if (dev_priv->l3_parity.remap_info[slice]) 1821c966dd2SBen Widawsky memcpy(buf, 18335a85ac6SBen Widawsky dev_priv->l3_parity.remap_info[slice] + (offset/4), 1841c966dd2SBen Widawsky count); 1851c966dd2SBen Widawsky else 1861c966dd2SBen Widawsky memset(buf, 0, count); 1871c966dd2SBen Widawsky 188c49d13eeSDavid Weinehall mutex_unlock(&dev->struct_mutex); 18984bc7581SBen Widawsky 1901c966dd2SBen Widawsky return count; 19184bc7581SBen Widawsky } 19284bc7581SBen Widawsky 19384bc7581SBen Widawsky static ssize_t 19484bc7581SBen Widawsky i915_l3_write(struct file *filp, struct kobject *kobj, 19584bc7581SBen Widawsky struct bin_attribute *attr, char *buf, 19684bc7581SBen Widawsky loff_t offset, size_t count) 19784bc7581SBen Widawsky { 198c49d13eeSDavid Weinehall struct device *kdev = kobj_to_dev(kobj); 199694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 200694c2828SDavid Weinehall struct drm_device *dev = &dev_priv->drm; 201e2efd130SChris Wilson struct i915_gem_context *ctx; 20235a85ac6SBen Widawsky int slice = (int)(uintptr_t)attr->private; 203cefcff8fSJoonas Lahtinen u32 **remap_info; 20484bc7581SBen Widawsky int ret; 20584bc7581SBen Widawsky 206694c2828SDavid Weinehall ret = l3_access_valid(dev_priv, offset); 20784bc7581SBen Widawsky if (ret) 20884bc7581SBen Widawsky return ret; 20984bc7581SBen Widawsky 210c49d13eeSDavid Weinehall ret = i915_mutex_lock_interruptible(dev); 21184bc7581SBen Widawsky if (ret) 21284bc7581SBen Widawsky return ret; 21384bc7581SBen Widawsky 214cefcff8fSJoonas Lahtinen remap_info = &dev_priv->l3_parity.remap_info[slice]; 215cefcff8fSJoonas Lahtinen if (!*remap_info) { 216cefcff8fSJoonas Lahtinen *remap_info = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL); 217cefcff8fSJoonas Lahtinen if (!*remap_info) { 218cefcff8fSJoonas Lahtinen ret = -ENOMEM; 219cefcff8fSJoonas Lahtinen goto out; 22084bc7581SBen Widawsky } 22184bc7581SBen Widawsky } 22284bc7581SBen Widawsky 22384bc7581SBen Widawsky /* TODO: Ideally we really want a GPU reset here to make sure errors 22484bc7581SBen Widawsky * aren't propagated. Since I cannot find a stable way to reset the GPU 22584bc7581SBen Widawsky * at this point it is left as a TODO. 22684bc7581SBen Widawsky */ 227cefcff8fSJoonas Lahtinen memcpy(*remap_info + (offset/4), buf, count); 22884bc7581SBen Widawsky 2293ccfd19dSBen Widawsky /* NB: We defer the remapping until we switch to the context */ 230829a0af2SChris Wilson list_for_each_entry(ctx, &dev_priv->contexts.list, link) 2313ccfd19dSBen Widawsky ctx->remap_slice |= (1<<slice); 23284bc7581SBen Widawsky 233cefcff8fSJoonas Lahtinen ret = count; 234cefcff8fSJoonas Lahtinen 235cefcff8fSJoonas Lahtinen out: 236c49d13eeSDavid Weinehall mutex_unlock(&dev->struct_mutex); 23784bc7581SBen Widawsky 238cefcff8fSJoonas Lahtinen return ret; 23984bc7581SBen Widawsky } 24084bc7581SBen Widawsky 24159f3da1eSBhumika Goyal static const struct bin_attribute dpf_attrs = { 24284bc7581SBen Widawsky .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)}, 24384bc7581SBen Widawsky .size = GEN7_L3LOG_SIZE, 24484bc7581SBen Widawsky .read = i915_l3_read, 24584bc7581SBen Widawsky .write = i915_l3_write, 24635a85ac6SBen Widawsky .mmap = NULL, 24735a85ac6SBen Widawsky .private = (void *)0 24835a85ac6SBen Widawsky }; 24935a85ac6SBen Widawsky 25059f3da1eSBhumika Goyal static const struct bin_attribute dpf_attrs_1 = { 25135a85ac6SBen Widawsky .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)}, 25235a85ac6SBen Widawsky .size = GEN7_L3LOG_SIZE, 25335a85ac6SBen Widawsky .read = i915_l3_read, 25435a85ac6SBen Widawsky .write = i915_l3_write, 25535a85ac6SBen Widawsky .mmap = NULL, 25635a85ac6SBen Widawsky .private = (void *)1 25784bc7581SBen Widawsky }; 25884bc7581SBen Widawsky 259c8c972ebSVille Syrjälä static ssize_t gt_act_freq_mhz_show(struct device *kdev, 260df6eedc8SBen Widawsky struct device_attribute *attr, char *buf) 261df6eedc8SBen Widawsky { 262694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 26348d1c812SChris Wilson intel_wakeref_t wakeref; 264337fa6e0SChris Wilson u32 freq; 265df6eedc8SBen Widawsky 26648d1c812SChris Wilson wakeref = intel_runtime_pm_get(dev_priv); 267d46c0517SImre Deak 268666a4537SWayne Boyer if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) { 269337fa6e0SChris Wilson vlv_punit_get(dev_priv); 27064936258SJani Nikula freq = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS); 271337fa6e0SChris Wilson vlv_punit_put(dev_priv); 272337fa6e0SChris Wilson 273337fa6e0SChris Wilson freq = (freq >> 8) & 0xff; 274177006a1SJesse Barnes } else { 275337fa6e0SChris Wilson freq = intel_get_cagf(dev_priv, I915_READ(GEN6_RPSTAT1)); 276c8c972ebSVille Syrjälä } 277c8c972ebSVille Syrjälä 27848d1c812SChris Wilson intel_runtime_pm_put(dev_priv, wakeref); 279c8c972ebSVille Syrjälä 280337fa6e0SChris Wilson return snprintf(buf, PAGE_SIZE, "%d\n", intel_gpu_freq(dev_priv, freq)); 281c8c972ebSVille Syrjälä } 282c8c972ebSVille Syrjälä 283c8c972ebSVille Syrjälä static ssize_t gt_cur_freq_mhz_show(struct device *kdev, 284c8c972ebSVille Syrjälä struct device_attribute *attr, char *buf) 285c8c972ebSVille Syrjälä { 286694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 287c8c972ebSVille Syrjälä 28862e1baa1SChris Wilson return snprintf(buf, PAGE_SIZE, "%d\n", 28962e1baa1SChris Wilson intel_gpu_freq(dev_priv, 290562d9baeSSagar Arun Kamble dev_priv->gt_pm.rps.cur_freq)); 291df6eedc8SBen Widawsky } 292df6eedc8SBen Widawsky 29329ecd78dSChris Wilson static ssize_t gt_boost_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf) 29429ecd78dSChris Wilson { 295694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 29629ecd78dSChris Wilson 29729ecd78dSChris Wilson return snprintf(buf, PAGE_SIZE, "%d\n", 29862e1baa1SChris Wilson intel_gpu_freq(dev_priv, 299562d9baeSSagar Arun Kamble dev_priv->gt_pm.rps.boost_freq)); 30029ecd78dSChris Wilson } 30129ecd78dSChris Wilson 30229ecd78dSChris Wilson static ssize_t gt_boost_freq_mhz_store(struct device *kdev, 30329ecd78dSChris Wilson struct device_attribute *attr, 30429ecd78dSChris Wilson const char *buf, size_t count) 30529ecd78dSChris Wilson { 306694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 307562d9baeSSagar Arun Kamble struct intel_rps *rps = &dev_priv->gt_pm.rps; 30859cd31f1SChris Wilson bool boost = false; 30929ecd78dSChris Wilson ssize_t ret; 31059cd31f1SChris Wilson u32 val; 31129ecd78dSChris Wilson 31229ecd78dSChris Wilson ret = kstrtou32(buf, 0, &val); 31329ecd78dSChris Wilson if (ret) 31429ecd78dSChris Wilson return ret; 31529ecd78dSChris Wilson 31629ecd78dSChris Wilson /* Validate against (static) hardware limits */ 31729ecd78dSChris Wilson val = intel_freq_opcode(dev_priv, val); 318562d9baeSSagar Arun Kamble if (val < rps->min_freq || val > rps->max_freq) 31929ecd78dSChris Wilson return -EINVAL; 32029ecd78dSChris Wilson 321ebb5eb7dSChris Wilson mutex_lock(&rps->lock); 32259cd31f1SChris Wilson if (val != rps->boost_freq) { 323562d9baeSSagar Arun Kamble rps->boost_freq = val; 32459cd31f1SChris Wilson boost = atomic_read(&rps->num_waiters); 32559cd31f1SChris Wilson } 326ebb5eb7dSChris Wilson mutex_unlock(&rps->lock); 32759cd31f1SChris Wilson if (boost) 32859cd31f1SChris Wilson schedule_work(&rps->work); 32929ecd78dSChris Wilson 33029ecd78dSChris Wilson return count; 33129ecd78dSChris Wilson } 33229ecd78dSChris Wilson 33397e4eed7SChris Wilson static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev, 33497e4eed7SChris Wilson struct device_attribute *attr, char *buf) 33597e4eed7SChris Wilson { 336694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 33797e4eed7SChris Wilson 33862e1baa1SChris Wilson return snprintf(buf, PAGE_SIZE, "%d\n", 33962e1baa1SChris Wilson intel_gpu_freq(dev_priv, 340562d9baeSSagar Arun Kamble dev_priv->gt_pm.rps.efficient_freq)); 34197e4eed7SChris Wilson } 34297e4eed7SChris Wilson 343df6eedc8SBen Widawsky static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf) 344df6eedc8SBen Widawsky { 345694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 346df6eedc8SBen Widawsky 34762e1baa1SChris Wilson return snprintf(buf, PAGE_SIZE, "%d\n", 34862e1baa1SChris Wilson intel_gpu_freq(dev_priv, 349562d9baeSSagar Arun Kamble dev_priv->gt_pm.rps.max_freq_softlimit)); 350df6eedc8SBen Widawsky } 351df6eedc8SBen Widawsky 35246ddf194SBen Widawsky static ssize_t gt_max_freq_mhz_store(struct device *kdev, 35346ddf194SBen Widawsky struct device_attribute *attr, 35446ddf194SBen Widawsky const char *buf, size_t count) 35546ddf194SBen Widawsky { 356694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 357562d9baeSSagar Arun Kamble struct intel_rps *rps = &dev_priv->gt_pm.rps; 35848d1c812SChris Wilson intel_wakeref_t wakeref; 3592a5913a8SBen Widawsky u32 val; 36046ddf194SBen Widawsky ssize_t ret; 36146ddf194SBen Widawsky 36246ddf194SBen Widawsky ret = kstrtou32(buf, 0, &val); 36346ddf194SBen Widawsky if (ret) 36446ddf194SBen Widawsky return ret; 36546ddf194SBen Widawsky 36648d1c812SChris Wilson wakeref = intel_runtime_pm_get(dev_priv); 367ebb5eb7dSChris Wilson mutex_lock(&rps->lock); 36846ddf194SBen Widawsky 3697c59a9c1SVille Syrjälä val = intel_freq_opcode(dev_priv, val); 370562d9baeSSagar Arun Kamble if (val < rps->min_freq || 371562d9baeSSagar Arun Kamble val > rps->max_freq || 372562d9baeSSagar Arun Kamble val < rps->min_freq_softlimit) { 373ebb5eb7dSChris Wilson ret = -EINVAL; 374ebb5eb7dSChris Wilson goto unlock; 37546ddf194SBen Widawsky } 37646ddf194SBen Widawsky 377562d9baeSSagar Arun Kamble if (val > rps->rp0_freq) 37831c77388SBen Widawsky DRM_DEBUG("User requested overclocking to %d\n", 3797c59a9c1SVille Syrjälä intel_gpu_freq(dev_priv, val)); 38031c77388SBen Widawsky 381562d9baeSSagar Arun Kamble rps->max_freq_softlimit = val; 38246ddf194SBen Widawsky 383562d9baeSSagar Arun Kamble val = clamp_t(int, rps->cur_freq, 384562d9baeSSagar Arun Kamble rps->min_freq_softlimit, 385562d9baeSSagar Arun Kamble rps->max_freq_softlimit); 386f745a80eSVille Syrjälä 387f745a80eSVille Syrjälä /* We still need *_set_rps to process the new max_delay and 388f745a80eSVille Syrjälä * update the interrupt limits and PMINTRMSK even though 389f745a80eSVille Syrjälä * frequency request may be unchanged. */ 3909fcee2f7SChris Wilson ret = intel_set_rps(dev_priv, val); 3916917c7b9SChris Wilson 392ebb5eb7dSChris Wilson unlock: 393ebb5eb7dSChris Wilson mutex_unlock(&rps->lock); 39448d1c812SChris Wilson intel_runtime_pm_put(dev_priv, wakeref); 395933bfb44SSagar Arun Kamble 3969fcee2f7SChris Wilson return ret ?: count; 39746ddf194SBen Widawsky } 39846ddf194SBen Widawsky 399df6eedc8SBen Widawsky static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf) 400df6eedc8SBen Widawsky { 401694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 402df6eedc8SBen Widawsky 40362e1baa1SChris Wilson return snprintf(buf, PAGE_SIZE, "%d\n", 40462e1baa1SChris Wilson intel_gpu_freq(dev_priv, 405562d9baeSSagar Arun Kamble dev_priv->gt_pm.rps.min_freq_softlimit)); 406df6eedc8SBen Widawsky } 407df6eedc8SBen Widawsky 40846ddf194SBen Widawsky static ssize_t gt_min_freq_mhz_store(struct device *kdev, 40946ddf194SBen Widawsky struct device_attribute *attr, 41046ddf194SBen Widawsky const char *buf, size_t count) 41146ddf194SBen Widawsky { 412694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 413562d9baeSSagar Arun Kamble struct intel_rps *rps = &dev_priv->gt_pm.rps; 41448d1c812SChris Wilson intel_wakeref_t wakeref; 4152a5913a8SBen Widawsky u32 val; 41646ddf194SBen Widawsky ssize_t ret; 41746ddf194SBen Widawsky 41846ddf194SBen Widawsky ret = kstrtou32(buf, 0, &val); 41946ddf194SBen Widawsky if (ret) 42046ddf194SBen Widawsky return ret; 42146ddf194SBen Widawsky 42248d1c812SChris Wilson wakeref = intel_runtime_pm_get(dev_priv); 423ebb5eb7dSChris Wilson mutex_lock(&rps->lock); 42446ddf194SBen Widawsky 4257c59a9c1SVille Syrjälä val = intel_freq_opcode(dev_priv, val); 426562d9baeSSagar Arun Kamble if (val < rps->min_freq || 427562d9baeSSagar Arun Kamble val > rps->max_freq || 428562d9baeSSagar Arun Kamble val > rps->max_freq_softlimit) { 429ebb5eb7dSChris Wilson ret = -EINVAL; 430ebb5eb7dSChris Wilson goto unlock; 43146ddf194SBen Widawsky } 43246ddf194SBen Widawsky 433562d9baeSSagar Arun Kamble rps->min_freq_softlimit = val; 4346917c7b9SChris Wilson 435562d9baeSSagar Arun Kamble val = clamp_t(int, rps->cur_freq, 436562d9baeSSagar Arun Kamble rps->min_freq_softlimit, 437562d9baeSSagar Arun Kamble rps->max_freq_softlimit); 438f745a80eSVille Syrjälä 439f745a80eSVille Syrjälä /* We still need *_set_rps to process the new min_delay and 440f745a80eSVille Syrjälä * update the interrupt limits and PMINTRMSK even though 441f745a80eSVille Syrjälä * frequency request may be unchanged. */ 4429fcee2f7SChris Wilson ret = intel_set_rps(dev_priv, val); 44346ddf194SBen Widawsky 444ebb5eb7dSChris Wilson unlock: 445ebb5eb7dSChris Wilson mutex_unlock(&rps->lock); 44648d1c812SChris Wilson intel_runtime_pm_put(dev_priv, wakeref); 447933bfb44SSagar Arun Kamble 4489fcee2f7SChris Wilson return ret ?: count; 44946ddf194SBen Widawsky } 45046ddf194SBen Widawsky 451c828a892SJoe Perches static DEVICE_ATTR_RO(gt_act_freq_mhz); 452c828a892SJoe Perches static DEVICE_ATTR_RO(gt_cur_freq_mhz); 453b6b996b6SJoe Perches static DEVICE_ATTR_RW(gt_boost_freq_mhz); 454b6b996b6SJoe Perches static DEVICE_ATTR_RW(gt_max_freq_mhz); 455b6b996b6SJoe Perches static DEVICE_ATTR_RW(gt_min_freq_mhz); 456df6eedc8SBen Widawsky 457c828a892SJoe Perches static DEVICE_ATTR_RO(vlv_rpe_freq_mhz); 458ac6ae347SBen Widawsky 459ac6ae347SBen Widawsky static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf); 460ac6ae347SBen Widawsky static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL); 461ac6ae347SBen Widawsky static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL); 462ac6ae347SBen Widawsky static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL); 463ac6ae347SBen Widawsky 464ac6ae347SBen Widawsky /* For now we have a static number of RP states */ 465ac6ae347SBen Widawsky static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf) 466ac6ae347SBen Widawsky { 467694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 468562d9baeSSagar Arun Kamble struct intel_rps *rps = &dev_priv->gt_pm.rps; 469bc4d91f6SAkash Goel u32 val; 470ac6ae347SBen Widawsky 471bc4d91f6SAkash Goel if (attr == &dev_attr_gt_RP0_freq_mhz) 472562d9baeSSagar Arun Kamble val = intel_gpu_freq(dev_priv, rps->rp0_freq); 473bc4d91f6SAkash Goel else if (attr == &dev_attr_gt_RP1_freq_mhz) 474562d9baeSSagar Arun Kamble val = intel_gpu_freq(dev_priv, rps->rp1_freq); 475bc4d91f6SAkash Goel else if (attr == &dev_attr_gt_RPn_freq_mhz) 476562d9baeSSagar Arun Kamble val = intel_gpu_freq(dev_priv, rps->min_freq); 47774c4f62bSDeepak S else 478ac6ae347SBen Widawsky BUG(); 479bc4d91f6SAkash Goel 4803e2a1556SJani Nikula return snprintf(buf, PAGE_SIZE, "%d\n", val); 481ac6ae347SBen Widawsky } 482ac6ae347SBen Widawsky 483e1215de8SJani Nikula static const struct attribute * const gen6_attrs[] = { 484c8c972ebSVille Syrjälä &dev_attr_gt_act_freq_mhz.attr, 485df6eedc8SBen Widawsky &dev_attr_gt_cur_freq_mhz.attr, 48629ecd78dSChris Wilson &dev_attr_gt_boost_freq_mhz.attr, 487df6eedc8SBen Widawsky &dev_attr_gt_max_freq_mhz.attr, 488df6eedc8SBen Widawsky &dev_attr_gt_min_freq_mhz.attr, 489ac6ae347SBen Widawsky &dev_attr_gt_RP0_freq_mhz.attr, 490ac6ae347SBen Widawsky &dev_attr_gt_RP1_freq_mhz.attr, 491ac6ae347SBen Widawsky &dev_attr_gt_RPn_freq_mhz.attr, 492df6eedc8SBen Widawsky NULL, 493df6eedc8SBen Widawsky }; 494df6eedc8SBen Widawsky 495e1215de8SJani Nikula static const struct attribute * const vlv_attrs[] = { 496c8c972ebSVille Syrjälä &dev_attr_gt_act_freq_mhz.attr, 49797e4eed7SChris Wilson &dev_attr_gt_cur_freq_mhz.attr, 49829ecd78dSChris Wilson &dev_attr_gt_boost_freq_mhz.attr, 49997e4eed7SChris Wilson &dev_attr_gt_max_freq_mhz.attr, 50097e4eed7SChris Wilson &dev_attr_gt_min_freq_mhz.attr, 50174c4f62bSDeepak S &dev_attr_gt_RP0_freq_mhz.attr, 50274c4f62bSDeepak S &dev_attr_gt_RP1_freq_mhz.attr, 50374c4f62bSDeepak S &dev_attr_gt_RPn_freq_mhz.attr, 50497e4eed7SChris Wilson &dev_attr_vlv_rpe_freq_mhz.attr, 50597e4eed7SChris Wilson NULL, 50697e4eed7SChris Wilson }; 50797e4eed7SChris Wilson 50898a2f411SChris Wilson #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR) 50998a2f411SChris Wilson 510ef86ddceSMika Kuoppala static ssize_t error_state_read(struct file *filp, struct kobject *kobj, 511ef86ddceSMika Kuoppala struct bin_attribute *attr, char *buf, 512ef86ddceSMika Kuoppala loff_t off, size_t count) 513ef86ddceSMika Kuoppala { 514ef86ddceSMika Kuoppala 515657fb5fbSGeliang Tang struct device *kdev = kobj_to_dev(kobj); 5160e39037bSChris Wilson struct drm_i915_private *i915 = kdev_minor_to_i915(kdev); 5175a4c6f1bSChris Wilson struct i915_gpu_state *gpu; 5185a4c6f1bSChris Wilson ssize_t ret; 519ef86ddceSMika Kuoppala 5200e39037bSChris Wilson gpu = i915_first_error_state(i915); 521e6154e4cSChris Wilson if (IS_ERR(gpu)) { 522e6154e4cSChris Wilson ret = PTR_ERR(gpu); 523e6154e4cSChris Wilson } else if (gpu) { 5240e39037bSChris Wilson ret = i915_gpu_state_copy_to_buffer(gpu, buf, off, count); 5255a4c6f1bSChris Wilson i915_gpu_state_put(gpu); 5260e39037bSChris Wilson } else { 5270e39037bSChris Wilson const char *str = "No error state collected\n"; 5280e39037bSChris Wilson size_t len = strlen(str); 5290e39037bSChris Wilson 5300e39037bSChris Wilson ret = min_t(size_t, count, len - off); 5310e39037bSChris Wilson memcpy(buf, str + off, ret); 5320e39037bSChris Wilson } 533ef86ddceSMika Kuoppala 5345a4c6f1bSChris Wilson return ret; 535ef86ddceSMika Kuoppala } 536ef86ddceSMika Kuoppala 537ef86ddceSMika Kuoppala static ssize_t error_state_write(struct file *file, struct kobject *kobj, 538ef86ddceSMika Kuoppala struct bin_attribute *attr, char *buf, 539ef86ddceSMika Kuoppala loff_t off, size_t count) 540ef86ddceSMika Kuoppala { 541657fb5fbSGeliang Tang struct device *kdev = kobj_to_dev(kobj); 542694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev); 543ef86ddceSMika Kuoppala 544ef86ddceSMika Kuoppala DRM_DEBUG_DRIVER("Resetting error state\n"); 5455a4c6f1bSChris Wilson i915_reset_error_state(dev_priv); 546ef86ddceSMika Kuoppala 547ef86ddceSMika Kuoppala return count; 548ef86ddceSMika Kuoppala } 549ef86ddceSMika Kuoppala 55059f3da1eSBhumika Goyal static const struct bin_attribute error_state_attr = { 551ef86ddceSMika Kuoppala .attr.name = "error", 552ef86ddceSMika Kuoppala .attr.mode = S_IRUSR | S_IWUSR, 553ef86ddceSMika Kuoppala .size = 0, 554ef86ddceSMika Kuoppala .read = error_state_read, 555ef86ddceSMika Kuoppala .write = error_state_write, 556ef86ddceSMika Kuoppala }; 557ef86ddceSMika Kuoppala 55898a2f411SChris Wilson static void i915_setup_error_capture(struct device *kdev) 55998a2f411SChris Wilson { 56098a2f411SChris Wilson if (sysfs_create_bin_file(&kdev->kobj, &error_state_attr)) 56198a2f411SChris Wilson DRM_ERROR("error_state sysfs setup failed\n"); 56298a2f411SChris Wilson } 56398a2f411SChris Wilson 56498a2f411SChris Wilson static void i915_teardown_error_capture(struct device *kdev) 56598a2f411SChris Wilson { 56698a2f411SChris Wilson sysfs_remove_bin_file(&kdev->kobj, &error_state_attr); 56798a2f411SChris Wilson } 56898a2f411SChris Wilson #else 56998a2f411SChris Wilson static void i915_setup_error_capture(struct device *kdev) {} 57098a2f411SChris Wilson static void i915_teardown_error_capture(struct device *kdev) {} 57198a2f411SChris Wilson #endif 57298a2f411SChris Wilson 573694c2828SDavid Weinehall void i915_setup_sysfs(struct drm_i915_private *dev_priv) 5740136db58SBen Widawsky { 575694c2828SDavid Weinehall struct device *kdev = dev_priv->drm.primary->kdev; 5760136db58SBen Widawsky int ret; 5770136db58SBen Widawsky 5788c3f929bSBen Widawsky #ifdef CONFIG_PM 579694c2828SDavid Weinehall if (HAS_RC6(dev_priv)) { 580694c2828SDavid Weinehall ret = sysfs_merge_group(&kdev->kobj, 581112abd29SDaniel Vetter &rc6_attr_group); 5820136db58SBen Widawsky if (ret) 58384bc7581SBen Widawsky DRM_ERROR("RC6 residency sysfs setup failed\n"); 584112abd29SDaniel Vetter } 585694c2828SDavid Weinehall if (HAS_RC6p(dev_priv)) { 586694c2828SDavid Weinehall ret = sysfs_merge_group(&kdev->kobj, 58758abf1daSRodrigo Vivi &rc6p_attr_group); 58858abf1daSRodrigo Vivi if (ret) 58958abf1daSRodrigo Vivi DRM_ERROR("RC6p residency sysfs setup failed\n"); 59058abf1daSRodrigo Vivi } 591694c2828SDavid Weinehall if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) { 592694c2828SDavid Weinehall ret = sysfs_merge_group(&kdev->kobj, 593626ad6f3SVille Syrjälä &media_rc6_attr_group); 594626ad6f3SVille Syrjälä if (ret) 595626ad6f3SVille Syrjälä DRM_ERROR("Media RC6 residency sysfs setup failed\n"); 596626ad6f3SVille Syrjälä } 5978c3f929bSBen Widawsky #endif 598694c2828SDavid Weinehall if (HAS_L3_DPF(dev_priv)) { 599694c2828SDavid Weinehall ret = device_create_bin_file(kdev, &dpf_attrs); 60084bc7581SBen Widawsky if (ret) 60184bc7581SBen Widawsky DRM_ERROR("l3 parity sysfs setup failed\n"); 60235a85ac6SBen Widawsky 603694c2828SDavid Weinehall if (NUM_L3_SLICES(dev_priv) > 1) { 604694c2828SDavid Weinehall ret = device_create_bin_file(kdev, 60535a85ac6SBen Widawsky &dpf_attrs_1); 60635a85ac6SBen Widawsky if (ret) 60735a85ac6SBen Widawsky DRM_ERROR("l3 parity slice 1 setup failed\n"); 60835a85ac6SBen Widawsky } 6090136db58SBen Widawsky } 610df6eedc8SBen Widawsky 61197e4eed7SChris Wilson ret = 0; 612694c2828SDavid Weinehall if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) 613694c2828SDavid Weinehall ret = sysfs_create_files(&kdev->kobj, vlv_attrs); 614694c2828SDavid Weinehall else if (INTEL_GEN(dev_priv) >= 6) 615694c2828SDavid Weinehall ret = sysfs_create_files(&kdev->kobj, gen6_attrs); 616df6eedc8SBen Widawsky if (ret) 61797e4eed7SChris Wilson DRM_ERROR("RPS sysfs setup failed\n"); 618ef86ddceSMika Kuoppala 61998a2f411SChris Wilson i915_setup_error_capture(kdev); 620112abd29SDaniel Vetter } 6210136db58SBen Widawsky 622694c2828SDavid Weinehall void i915_teardown_sysfs(struct drm_i915_private *dev_priv) 6230136db58SBen Widawsky { 624694c2828SDavid Weinehall struct device *kdev = dev_priv->drm.primary->kdev; 625694c2828SDavid Weinehall 62698a2f411SChris Wilson i915_teardown_error_capture(kdev); 62798a2f411SChris Wilson 628694c2828SDavid Weinehall if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) 629694c2828SDavid Weinehall sysfs_remove_files(&kdev->kobj, vlv_attrs); 63097e4eed7SChris Wilson else 631694c2828SDavid Weinehall sysfs_remove_files(&kdev->kobj, gen6_attrs); 632694c2828SDavid Weinehall device_remove_bin_file(kdev, &dpf_attrs_1); 633694c2828SDavid Weinehall device_remove_bin_file(kdev, &dpf_attrs); 634853c70e8SBen Widawsky #ifdef CONFIG_PM 635694c2828SDavid Weinehall sysfs_unmerge_group(&kdev->kobj, &rc6_attr_group); 636694c2828SDavid Weinehall sysfs_unmerge_group(&kdev->kobj, &rc6p_attr_group); 637853c70e8SBen Widawsky #endif 6380136db58SBen Widawsky } 639