Home | History | Annotate | Line # | Download | only in i915
      1  1.2  riastrad /*	$NetBSD: i915_sysfs.c,v 1.3 2021/12/18 23:45:28 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright  2012 Intel Corporation
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     14  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     15  1.1  riastrad  * Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  1.1  riastrad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     23  1.1  riastrad  * IN THE SOFTWARE.
     24  1.1  riastrad  *
     25  1.1  riastrad  * Authors:
     26  1.1  riastrad  *    Ben Widawsky <ben (at) bwidawsk.net>
     27  1.1  riastrad  *
     28  1.1  riastrad  */
     29  1.1  riastrad 
     30  1.2  riastrad #include <sys/cdefs.h>
     31  1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: i915_sysfs.c,v 1.3 2021/12/18 23:45:28 riastradh Exp $");
     32  1.2  riastrad 
     33  1.1  riastrad #include <linux/device.h>
     34  1.1  riastrad #include <linux/module.h>
     35  1.1  riastrad #include <linux/stat.h>
     36  1.1  riastrad #include <linux/sysfs.h>
     37  1.3  riastrad 
     38  1.3  riastrad #include "gt/intel_rc6.h"
     39  1.3  riastrad #include "gt/intel_rps.h"
     40  1.3  riastrad 
     41  1.1  riastrad #include "i915_drv.h"
     42  1.3  riastrad #include "i915_sysfs.h"
     43  1.3  riastrad #include "intel_pm.h"
     44  1.3  riastrad #include "intel_sideband.h"
     45  1.1  riastrad 
     46  1.3  riastrad static inline struct drm_i915_private *kdev_minor_to_i915(struct device *kdev)
     47  1.3  riastrad {
     48  1.3  riastrad 	struct drm_minor *minor = dev_get_drvdata(kdev);
     49  1.3  riastrad 	return to_i915(minor->dev);
     50  1.3  riastrad }
     51  1.2  riastrad 
     52  1.1  riastrad #ifdef CONFIG_PM
     53  1.3  riastrad static u32 calc_residency(struct drm_i915_private *dev_priv,
     54  1.3  riastrad 			  i915_reg_t reg)
     55  1.1  riastrad {
     56  1.3  riastrad 	intel_wakeref_t wakeref;
     57  1.3  riastrad 	u64 res = 0;
     58  1.2  riastrad 
     59  1.3  riastrad 	with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref)
     60  1.3  riastrad 		res = intel_rc6_residency_us(&dev_priv->gt.rc6, reg);
     61  1.2  riastrad 
     62  1.3  riastrad 	return DIV_ROUND_CLOSEST_ULL(res, 1000);
     63  1.1  riastrad }
     64  1.1  riastrad 
     65  1.1  riastrad static ssize_t
     66  1.1  riastrad show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
     67  1.1  riastrad {
     68  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
     69  1.3  riastrad 	unsigned int mask;
     70  1.3  riastrad 
     71  1.3  riastrad 	mask = 0;
     72  1.3  riastrad 	if (HAS_RC6(dev_priv))
     73  1.3  riastrad 		mask |= BIT(0);
     74  1.3  riastrad 	if (HAS_RC6p(dev_priv))
     75  1.3  riastrad 		mask |= BIT(1);
     76  1.3  riastrad 	if (HAS_RC6pp(dev_priv))
     77  1.3  riastrad 		mask |= BIT(2);
     78  1.3  riastrad 
     79  1.3  riastrad 	return snprintf(buf, PAGE_SIZE, "%x\n", mask);
     80  1.1  riastrad }
     81  1.1  riastrad 
     82  1.1  riastrad static ssize_t
     83  1.1  riastrad show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
     84  1.1  riastrad {
     85  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
     86  1.3  riastrad 	u32 rc6_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6);
     87  1.2  riastrad 	return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
     88  1.1  riastrad }
     89  1.1  riastrad 
     90  1.1  riastrad static ssize_t
     91  1.1  riastrad show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf)
     92  1.1  riastrad {
     93  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
     94  1.3  riastrad 	u32 rc6p_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6p);
     95  1.2  riastrad 	return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency);
     96  1.1  riastrad }
     97  1.1  riastrad 
     98  1.1  riastrad static ssize_t
     99  1.1  riastrad show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf)
    100  1.1  riastrad {
    101  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
    102  1.3  riastrad 	u32 rc6pp_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6pp);
    103  1.2  riastrad 	return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency);
    104  1.2  riastrad }
    105  1.2  riastrad 
    106  1.2  riastrad static ssize_t
    107  1.2  riastrad show_media_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
    108  1.2  riastrad {
    109  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
    110  1.3  riastrad 	u32 rc6_residency = calc_residency(dev_priv, VLV_GT_MEDIA_RC6);
    111  1.2  riastrad 	return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
    112  1.1  riastrad }
    113  1.1  riastrad 
    114  1.1  riastrad static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL);
    115  1.1  riastrad static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL);
    116  1.1  riastrad static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL);
    117  1.1  riastrad static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL);
    118  1.2  riastrad static DEVICE_ATTR(media_rc6_residency_ms, S_IRUGO, show_media_rc6_ms, NULL);
    119  1.1  riastrad 
    120  1.1  riastrad static struct attribute *rc6_attrs[] = {
    121  1.1  riastrad 	&dev_attr_rc6_enable.attr,
    122  1.1  riastrad 	&dev_attr_rc6_residency_ms.attr,
    123  1.2  riastrad 	NULL
    124  1.2  riastrad };
    125  1.2  riastrad 
    126  1.3  riastrad static const struct attribute_group rc6_attr_group = {
    127  1.2  riastrad 	.name = power_group_name,
    128  1.2  riastrad 	.attrs =  rc6_attrs
    129  1.2  riastrad };
    130  1.2  riastrad 
    131  1.2  riastrad static struct attribute *rc6p_attrs[] = {
    132  1.1  riastrad 	&dev_attr_rc6p_residency_ms.attr,
    133  1.1  riastrad 	&dev_attr_rc6pp_residency_ms.attr,
    134  1.1  riastrad 	NULL
    135  1.1  riastrad };
    136  1.1  riastrad 
    137  1.3  riastrad static const struct attribute_group rc6p_attr_group = {
    138  1.2  riastrad 	.name = power_group_name,
    139  1.2  riastrad 	.attrs =  rc6p_attrs
    140  1.2  riastrad };
    141  1.2  riastrad 
    142  1.2  riastrad static struct attribute *media_rc6_attrs[] = {
    143  1.2  riastrad 	&dev_attr_media_rc6_residency_ms.attr,
    144  1.2  riastrad 	NULL
    145  1.2  riastrad };
    146  1.2  riastrad 
    147  1.3  riastrad static const struct attribute_group media_rc6_attr_group = {
    148  1.1  riastrad 	.name = power_group_name,
    149  1.2  riastrad 	.attrs =  media_rc6_attrs
    150  1.1  riastrad };
    151  1.1  riastrad #endif
    152  1.1  riastrad 
    153  1.3  riastrad static int l3_access_valid(struct drm_i915_private *i915, loff_t offset)
    154  1.1  riastrad {
    155  1.3  riastrad 	if (!HAS_L3_DPF(i915))
    156  1.1  riastrad 		return -EPERM;
    157  1.1  riastrad 
    158  1.3  riastrad 	if (!IS_ALIGNED(offset, sizeof(u32)))
    159  1.1  riastrad 		return -EINVAL;
    160  1.1  riastrad 
    161  1.1  riastrad 	if (offset >= GEN7_L3LOG_SIZE)
    162  1.1  riastrad 		return -ENXIO;
    163  1.1  riastrad 
    164  1.1  riastrad 	return 0;
    165  1.1  riastrad }
    166  1.1  riastrad 
    167  1.1  riastrad static ssize_t
    168  1.1  riastrad i915_l3_read(struct file *filp, struct kobject *kobj,
    169  1.1  riastrad 	     struct bin_attribute *attr, char *buf,
    170  1.1  riastrad 	     loff_t offset, size_t count)
    171  1.1  riastrad {
    172  1.3  riastrad 	struct device *kdev = kobj_to_dev(kobj);
    173  1.3  riastrad 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
    174  1.2  riastrad 	int slice = (int)(uintptr_t)attr->private;
    175  1.2  riastrad 	int ret;
    176  1.2  riastrad 
    177  1.3  riastrad 	ret = l3_access_valid(i915, offset);
    178  1.1  riastrad 	if (ret)
    179  1.1  riastrad 		return ret;
    180  1.1  riastrad 
    181  1.3  riastrad 	count = round_down(count, sizeof(u32));
    182  1.2  riastrad 	count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
    183  1.3  riastrad 	memset(buf, 0, count);
    184  1.2  riastrad 
    185  1.3  riastrad 	spin_lock(&i915->gem.contexts.lock);
    186  1.3  riastrad 	if (i915->l3_parity.remap_info[slice])
    187  1.2  riastrad 		memcpy(buf,
    188  1.3  riastrad 		       i915->l3_parity.remap_info[slice] + offset / sizeof(u32),
    189  1.2  riastrad 		       count);
    190  1.3  riastrad 	spin_unlock(&i915->gem.contexts.lock);
    191  1.1  riastrad 
    192  1.2  riastrad 	return count;
    193  1.1  riastrad }
    194  1.1  riastrad 
    195  1.1  riastrad static ssize_t
    196  1.1  riastrad i915_l3_write(struct file *filp, struct kobject *kobj,
    197  1.1  riastrad 	      struct bin_attribute *attr, char *buf,
    198  1.1  riastrad 	      loff_t offset, size_t count)
    199  1.1  riastrad {
    200  1.3  riastrad 	struct device *kdev = kobj_to_dev(kobj);
    201  1.3  riastrad 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
    202  1.2  riastrad 	int slice = (int)(uintptr_t)attr->private;
    203  1.3  riastrad 	u32 *remap_info, *freeme = NULL;
    204  1.3  riastrad 	struct i915_gem_context *ctx;
    205  1.1  riastrad 	int ret;
    206  1.1  riastrad 
    207  1.3  riastrad 	ret = l3_access_valid(i915, offset);
    208  1.1  riastrad 	if (ret)
    209  1.1  riastrad 		return ret;
    210  1.1  riastrad 
    211  1.3  riastrad 	if (count < sizeof(u32))
    212  1.3  riastrad 		return -EINVAL;
    213  1.1  riastrad 
    214  1.3  riastrad 	remap_info = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
    215  1.3  riastrad 	if (!remap_info)
    216  1.3  riastrad 		return -ENOMEM;
    217  1.3  riastrad 
    218  1.3  riastrad 	spin_lock(&i915->gem.contexts.lock);
    219  1.3  riastrad 
    220  1.3  riastrad 	if (i915->l3_parity.remap_info[slice]) {
    221  1.3  riastrad 		freeme = remap_info;
    222  1.3  riastrad 		remap_info = i915->l3_parity.remap_info[slice];
    223  1.3  riastrad 	} else {
    224  1.3  riastrad 		i915->l3_parity.remap_info[slice] = remap_info;
    225  1.1  riastrad 	}
    226  1.1  riastrad 
    227  1.3  riastrad 	count = round_down(count, sizeof(u32));
    228  1.3  riastrad 	memcpy(remap_info + offset / sizeof(u32), buf, count);
    229  1.3  riastrad 
    230  1.3  riastrad 	/* NB: We defer the remapping until we switch to the context */
    231  1.3  riastrad 	list_for_each_entry(ctx, &i915->gem.contexts.list, link)
    232  1.3  riastrad 		ctx->remap_slice |= BIT(slice);
    233  1.3  riastrad 
    234  1.3  riastrad 	spin_unlock(&i915->gem.contexts.lock);
    235  1.3  riastrad 	kfree(freeme);
    236  1.1  riastrad 
    237  1.3  riastrad 	/*
    238  1.3  riastrad 	 * TODO: Ideally we really want a GPU reset here to make sure errors
    239  1.1  riastrad 	 * aren't propagated. Since I cannot find a stable way to reset the GPU
    240  1.1  riastrad 	 * at this point it is left as a TODO.
    241  1.1  riastrad 	*/
    242  1.1  riastrad 
    243  1.1  riastrad 	return count;
    244  1.1  riastrad }
    245  1.1  riastrad 
    246  1.3  riastrad static const struct bin_attribute dpf_attrs = {
    247  1.1  riastrad 	.attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
    248  1.1  riastrad 	.size = GEN7_L3LOG_SIZE,
    249  1.1  riastrad 	.read = i915_l3_read,
    250  1.1  riastrad 	.write = i915_l3_write,
    251  1.2  riastrad 	.mmap = NULL,
    252  1.2  riastrad 	.private = (void *)0
    253  1.2  riastrad };
    254  1.2  riastrad 
    255  1.3  riastrad static const struct bin_attribute dpf_attrs_1 = {
    256  1.2  riastrad 	.attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
    257  1.2  riastrad 	.size = GEN7_L3LOG_SIZE,
    258  1.2  riastrad 	.read = i915_l3_read,
    259  1.2  riastrad 	.write = i915_l3_write,
    260  1.2  riastrad 	.mmap = NULL,
    261  1.2  riastrad 	.private = (void *)1
    262  1.1  riastrad };
    263  1.1  riastrad 
    264  1.2  riastrad static ssize_t gt_act_freq_mhz_show(struct device *kdev,
    265  1.2  riastrad 				    struct device_attribute *attr, char *buf)
    266  1.2  riastrad {
    267  1.3  riastrad 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
    268  1.3  riastrad 	struct intel_rps *rps = &i915->gt.rps;
    269  1.2  riastrad 
    270  1.3  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n",
    271  1.3  riastrad 			intel_rps_read_actual_frequency(rps));
    272  1.3  riastrad }
    273  1.2  riastrad 
    274  1.3  riastrad static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
    275  1.3  riastrad 				    struct device_attribute *attr, char *buf)
    276  1.3  riastrad {
    277  1.3  riastrad 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
    278  1.3  riastrad 	struct intel_rps *rps = &i915->gt.rps;
    279  1.2  riastrad 
    280  1.3  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n",
    281  1.3  riastrad 			intel_gpu_freq(rps, rps->cur_freq));
    282  1.3  riastrad }
    283  1.2  riastrad 
    284  1.3  riastrad static ssize_t gt_boost_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
    285  1.3  riastrad {
    286  1.3  riastrad 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
    287  1.3  riastrad 	struct intel_rps *rps = &i915->gt.rps;
    288  1.2  riastrad 
    289  1.3  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n",
    290  1.3  riastrad 			intel_gpu_freq(rps, rps->boost_freq));
    291  1.2  riastrad }
    292  1.2  riastrad 
    293  1.3  riastrad static ssize_t gt_boost_freq_mhz_store(struct device *kdev,
    294  1.3  riastrad 				       struct device_attribute *attr,
    295  1.3  riastrad 				       const char *buf, size_t count)
    296  1.1  riastrad {
    297  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
    298  1.3  riastrad 	struct intel_rps *rps = &dev_priv->gt.rps;
    299  1.3  riastrad 	bool boost = false;
    300  1.3  riastrad 	ssize_t ret;
    301  1.3  riastrad 	u32 val;
    302  1.1  riastrad 
    303  1.3  riastrad 	ret = kstrtou32(buf, 0, &val);
    304  1.3  riastrad 	if (ret)
    305  1.3  riastrad 		return ret;
    306  1.2  riastrad 
    307  1.3  riastrad 	/* Validate against (static) hardware limits */
    308  1.3  riastrad 	val = intel_freq_opcode(rps, val);
    309  1.3  riastrad 	if (val < rps->min_freq || val > rps->max_freq)
    310  1.3  riastrad 		return -EINVAL;
    311  1.2  riastrad 
    312  1.3  riastrad 	mutex_lock(&rps->lock);
    313  1.3  riastrad 	if (val != rps->boost_freq) {
    314  1.3  riastrad 		rps->boost_freq = val;
    315  1.3  riastrad 		boost = atomic_read(&rps->num_waiters);
    316  1.3  riastrad 	}
    317  1.3  riastrad 	mutex_unlock(&rps->lock);
    318  1.3  riastrad 	if (boost)
    319  1.3  riastrad 		schedule_work(&rps->work);
    320  1.1  riastrad 
    321  1.3  riastrad 	return count;
    322  1.2  riastrad }
    323  1.2  riastrad 
    324  1.2  riastrad static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
    325  1.2  riastrad 				     struct device_attribute *attr, char *buf)
    326  1.2  riastrad {
    327  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
    328  1.3  riastrad 	struct intel_rps *rps = &dev_priv->gt.rps;
    329  1.3  riastrad 
    330  1.3  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n",
    331  1.3  riastrad 			intel_gpu_freq(rps, rps->efficient_freq));
    332  1.1  riastrad }
    333  1.1  riastrad 
    334  1.1  riastrad static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
    335  1.1  riastrad {
    336  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
    337  1.3  riastrad 	struct intel_rps *rps = &dev_priv->gt.rps;
    338  1.1  riastrad 
    339  1.3  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n",
    340  1.3  riastrad 			intel_gpu_freq(rps, rps->max_freq_softlimit));
    341  1.1  riastrad }
    342  1.1  riastrad 
    343  1.1  riastrad static ssize_t gt_max_freq_mhz_store(struct device *kdev,
    344  1.1  riastrad 				     struct device_attribute *attr,
    345  1.1  riastrad 				     const char *buf, size_t count)
    346  1.1  riastrad {
    347  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
    348  1.3  riastrad 	struct intel_rps *rps = &dev_priv->gt.rps;
    349  1.3  riastrad 	ssize_t ret;
    350  1.2  riastrad 	u32 val;
    351  1.1  riastrad 
    352  1.1  riastrad 	ret = kstrtou32(buf, 0, &val);
    353  1.1  riastrad 	if (ret)
    354  1.1  riastrad 		return ret;
    355  1.1  riastrad 
    356  1.3  riastrad 	mutex_lock(&rps->lock);
    357  1.1  riastrad 
    358  1.3  riastrad 	val = intel_freq_opcode(rps, val);
    359  1.3  riastrad 	if (val < rps->min_freq ||
    360  1.3  riastrad 	    val > rps->max_freq ||
    361  1.3  riastrad 	    val < rps->min_freq_softlimit) {
    362  1.3  riastrad 		ret = -EINVAL;
    363  1.3  riastrad 		goto unlock;
    364  1.1  riastrad 	}
    365  1.1  riastrad 
    366  1.3  riastrad 	if (val > rps->rp0_freq)
    367  1.2  riastrad 		DRM_DEBUG("User requested overclocking to %d\n",
    368  1.3  riastrad 			  intel_gpu_freq(rps, val));
    369  1.2  riastrad 
    370  1.3  riastrad 	rps->max_freq_softlimit = val;
    371  1.2  riastrad 
    372  1.3  riastrad 	val = clamp_t(int, rps->cur_freq,
    373  1.3  riastrad 		      rps->min_freq_softlimit,
    374  1.3  riastrad 		      rps->max_freq_softlimit);
    375  1.2  riastrad 
    376  1.3  riastrad 	/*
    377  1.3  riastrad 	 * We still need *_set_rps to process the new max_delay and
    378  1.2  riastrad 	 * update the interrupt limits and PMINTRMSK even though
    379  1.3  riastrad 	 * frequency request may be unchanged.
    380  1.3  riastrad 	 */
    381  1.3  riastrad 	intel_rps_set(rps, val);
    382  1.1  riastrad 
    383  1.3  riastrad unlock:
    384  1.3  riastrad 	mutex_unlock(&rps->lock);
    385  1.1  riastrad 
    386  1.3  riastrad 	return ret ?: count;
    387  1.1  riastrad }
    388  1.1  riastrad 
    389  1.1  riastrad static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
    390  1.1  riastrad {
    391  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
    392  1.3  riastrad 	struct intel_rps *rps = &dev_priv->gt.rps;
    393  1.1  riastrad 
    394  1.3  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n",
    395  1.3  riastrad 			intel_gpu_freq(rps, rps->min_freq_softlimit));
    396  1.1  riastrad }
    397  1.1  riastrad 
    398  1.1  riastrad static ssize_t gt_min_freq_mhz_store(struct device *kdev,
    399  1.1  riastrad 				     struct device_attribute *attr,
    400  1.1  riastrad 				     const char *buf, size_t count)
    401  1.1  riastrad {
    402  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
    403  1.3  riastrad 	struct intel_rps *rps = &dev_priv->gt.rps;
    404  1.3  riastrad 	ssize_t ret;
    405  1.2  riastrad 	u32 val;
    406  1.1  riastrad 
    407  1.1  riastrad 	ret = kstrtou32(buf, 0, &val);
    408  1.1  riastrad 	if (ret)
    409  1.1  riastrad 		return ret;
    410  1.1  riastrad 
    411  1.3  riastrad 	mutex_lock(&rps->lock);
    412  1.1  riastrad 
    413  1.3  riastrad 	val = intel_freq_opcode(rps, val);
    414  1.3  riastrad 	if (val < rps->min_freq ||
    415  1.3  riastrad 	    val > rps->max_freq ||
    416  1.3  riastrad 	    val > rps->max_freq_softlimit) {
    417  1.3  riastrad 		ret = -EINVAL;
    418  1.3  riastrad 		goto unlock;
    419  1.1  riastrad 	}
    420  1.1  riastrad 
    421  1.3  riastrad 	rps->min_freq_softlimit = val;
    422  1.1  riastrad 
    423  1.3  riastrad 	val = clamp_t(int, rps->cur_freq,
    424  1.3  riastrad 		      rps->min_freq_softlimit,
    425  1.3  riastrad 		      rps->max_freq_softlimit);
    426  1.2  riastrad 
    427  1.3  riastrad 	/*
    428  1.3  riastrad 	 * We still need *_set_rps to process the new min_delay and
    429  1.2  riastrad 	 * update the interrupt limits and PMINTRMSK even though
    430  1.3  riastrad 	 * frequency request may be unchanged.
    431  1.3  riastrad 	 */
    432  1.3  riastrad 	intel_rps_set(rps, val);
    433  1.1  riastrad 
    434  1.3  riastrad unlock:
    435  1.3  riastrad 	mutex_unlock(&rps->lock);
    436  1.1  riastrad 
    437  1.3  riastrad 	return ret ?: count;
    438  1.1  riastrad }
    439  1.1  riastrad 
    440  1.3  riastrad static DEVICE_ATTR_RO(gt_act_freq_mhz);
    441  1.3  riastrad static DEVICE_ATTR_RO(gt_cur_freq_mhz);
    442  1.3  riastrad static DEVICE_ATTR_RW(gt_boost_freq_mhz);
    443  1.3  riastrad static DEVICE_ATTR_RW(gt_max_freq_mhz);
    444  1.3  riastrad static DEVICE_ATTR_RW(gt_min_freq_mhz);
    445  1.1  riastrad 
    446  1.3  riastrad static DEVICE_ATTR_RO(vlv_rpe_freq_mhz);
    447  1.1  riastrad 
    448  1.1  riastrad static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
    449  1.1  riastrad static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
    450  1.1  riastrad static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
    451  1.1  riastrad static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
    452  1.1  riastrad 
    453  1.1  riastrad /* For now we have a static number of RP states */
    454  1.1  riastrad static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
    455  1.1  riastrad {
    456  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
    457  1.3  riastrad 	struct intel_rps *rps = &dev_priv->gt.rps;
    458  1.2  riastrad 	u32 val;
    459  1.1  riastrad 
    460  1.2  riastrad 	if (attr == &dev_attr_gt_RP0_freq_mhz)
    461  1.3  riastrad 		val = intel_gpu_freq(rps, rps->rp0_freq);
    462  1.2  riastrad 	else if (attr == &dev_attr_gt_RP1_freq_mhz)
    463  1.3  riastrad 		val = intel_gpu_freq(rps, rps->rp1_freq);
    464  1.2  riastrad 	else if (attr == &dev_attr_gt_RPn_freq_mhz)
    465  1.3  riastrad 		val = intel_gpu_freq(rps, rps->min_freq);
    466  1.2  riastrad 	else
    467  1.2  riastrad 		BUG();
    468  1.1  riastrad 
    469  1.2  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
    470  1.1  riastrad }
    471  1.1  riastrad 
    472  1.3  riastrad static const struct attribute * const gen6_attrs[] = {
    473  1.2  riastrad 	&dev_attr_gt_act_freq_mhz.attr,
    474  1.2  riastrad 	&dev_attr_gt_cur_freq_mhz.attr,
    475  1.3  riastrad 	&dev_attr_gt_boost_freq_mhz.attr,
    476  1.2  riastrad 	&dev_attr_gt_max_freq_mhz.attr,
    477  1.2  riastrad 	&dev_attr_gt_min_freq_mhz.attr,
    478  1.2  riastrad 	&dev_attr_gt_RP0_freq_mhz.attr,
    479  1.2  riastrad 	&dev_attr_gt_RP1_freq_mhz.attr,
    480  1.2  riastrad 	&dev_attr_gt_RPn_freq_mhz.attr,
    481  1.2  riastrad 	NULL,
    482  1.2  riastrad };
    483  1.2  riastrad 
    484  1.3  riastrad static const struct attribute * const vlv_attrs[] = {
    485  1.2  riastrad 	&dev_attr_gt_act_freq_mhz.attr,
    486  1.1  riastrad 	&dev_attr_gt_cur_freq_mhz.attr,
    487  1.3  riastrad 	&dev_attr_gt_boost_freq_mhz.attr,
    488  1.1  riastrad 	&dev_attr_gt_max_freq_mhz.attr,
    489  1.1  riastrad 	&dev_attr_gt_min_freq_mhz.attr,
    490  1.1  riastrad 	&dev_attr_gt_RP0_freq_mhz.attr,
    491  1.1  riastrad 	&dev_attr_gt_RP1_freq_mhz.attr,
    492  1.1  riastrad 	&dev_attr_gt_RPn_freq_mhz.attr,
    493  1.2  riastrad 	&dev_attr_vlv_rpe_freq_mhz.attr,
    494  1.1  riastrad 	NULL,
    495  1.1  riastrad };
    496  1.1  riastrad 
    497  1.3  riastrad #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
    498  1.3  riastrad 
    499  1.2  riastrad static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
    500  1.2  riastrad 				struct bin_attribute *attr, char *buf,
    501  1.2  riastrad 				loff_t off, size_t count)
    502  1.2  riastrad {
    503  1.2  riastrad 
    504  1.3  riastrad 	struct device *kdev = kobj_to_dev(kobj);
    505  1.3  riastrad 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
    506  1.3  riastrad 	struct i915_gpu_coredump *gpu;
    507  1.3  riastrad 	ssize_t ret;
    508  1.2  riastrad 
    509  1.3  riastrad 	gpu = i915_first_error_state(i915);
    510  1.3  riastrad 	if (IS_ERR(gpu)) {
    511  1.3  riastrad 		ret = PTR_ERR(gpu);
    512  1.3  riastrad 	} else if (gpu) {
    513  1.3  riastrad 		ret = i915_gpu_coredump_copy_to_buffer(gpu, buf, off, count);
    514  1.3  riastrad 		i915_gpu_coredump_put(gpu);
    515  1.3  riastrad 	} else {
    516  1.3  riastrad 		const char *str = "No error state collected\n";
    517  1.3  riastrad 		size_t len = strlen(str);
    518  1.2  riastrad 
    519  1.3  riastrad 		ret = min_t(size_t, count, len - off);
    520  1.3  riastrad 		memcpy(buf, str + off, ret);
    521  1.3  riastrad 	}
    522  1.2  riastrad 
    523  1.3  riastrad 	return ret;
    524  1.2  riastrad }
    525  1.2  riastrad 
    526  1.2  riastrad static ssize_t error_state_write(struct file *file, struct kobject *kobj,
    527  1.2  riastrad 				 struct bin_attribute *attr, char *buf,
    528  1.2  riastrad 				 loff_t off, size_t count)
    529  1.2  riastrad {
    530  1.3  riastrad 	struct device *kdev = kobj_to_dev(kobj);
    531  1.3  riastrad 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
    532  1.2  riastrad 
    533  1.2  riastrad 	DRM_DEBUG_DRIVER("Resetting error state\n");
    534  1.3  riastrad 	i915_reset_error_state(dev_priv);
    535  1.2  riastrad 
    536  1.2  riastrad 	return count;
    537  1.2  riastrad }
    538  1.2  riastrad 
    539  1.3  riastrad static const struct bin_attribute error_state_attr = {
    540  1.2  riastrad 	.attr.name = "error",
    541  1.2  riastrad 	.attr.mode = S_IRUSR | S_IWUSR,
    542  1.2  riastrad 	.size = 0,
    543  1.2  riastrad 	.read = error_state_read,
    544  1.2  riastrad 	.write = error_state_write,
    545  1.2  riastrad };
    546  1.2  riastrad 
    547  1.3  riastrad static void i915_setup_error_capture(struct device *kdev)
    548  1.1  riastrad {
    549  1.3  riastrad 	if (sysfs_create_bin_file(&kdev->kobj, &error_state_attr))
    550  1.3  riastrad 		DRM_ERROR("error_state sysfs setup failed\n");
    551  1.3  riastrad }
    552  1.3  riastrad 
    553  1.3  riastrad static void i915_teardown_error_capture(struct device *kdev)
    554  1.3  riastrad {
    555  1.3  riastrad 	sysfs_remove_bin_file(&kdev->kobj, &error_state_attr);
    556  1.3  riastrad }
    557  1.3  riastrad #else
    558  1.3  riastrad static void i915_setup_error_capture(struct device *kdev) {}
    559  1.3  riastrad static void i915_teardown_error_capture(struct device *kdev) {}
    560  1.3  riastrad #endif
    561  1.3  riastrad 
    562  1.3  riastrad void i915_setup_sysfs(struct drm_i915_private *dev_priv)
    563  1.3  riastrad {
    564  1.3  riastrad 	struct device *kdev = dev_priv->drm.primary->kdev;
    565  1.1  riastrad 	int ret;
    566  1.1  riastrad 
    567  1.1  riastrad #ifdef CONFIG_PM
    568  1.3  riastrad 	if (HAS_RC6(dev_priv)) {
    569  1.3  riastrad 		ret = sysfs_merge_group(&kdev->kobj,
    570  1.1  riastrad 					&rc6_attr_group);
    571  1.1  riastrad 		if (ret)
    572  1.1  riastrad 			DRM_ERROR("RC6 residency sysfs setup failed\n");
    573  1.1  riastrad 	}
    574  1.3  riastrad 	if (HAS_RC6p(dev_priv)) {
    575  1.3  riastrad 		ret = sysfs_merge_group(&kdev->kobj,
    576  1.2  riastrad 					&rc6p_attr_group);
    577  1.2  riastrad 		if (ret)
    578  1.2  riastrad 			DRM_ERROR("RC6p residency sysfs setup failed\n");
    579  1.2  riastrad 	}
    580  1.3  riastrad 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
    581  1.3  riastrad 		ret = sysfs_merge_group(&kdev->kobj,
    582  1.2  riastrad 					&media_rc6_attr_group);
    583  1.2  riastrad 		if (ret)
    584  1.2  riastrad 			DRM_ERROR("Media RC6 residency sysfs setup failed\n");
    585  1.2  riastrad 	}
    586  1.1  riastrad #endif
    587  1.3  riastrad 	if (HAS_L3_DPF(dev_priv)) {
    588  1.3  riastrad 		ret = device_create_bin_file(kdev, &dpf_attrs);
    589  1.1  riastrad 		if (ret)
    590  1.1  riastrad 			DRM_ERROR("l3 parity sysfs setup failed\n");
    591  1.2  riastrad 
    592  1.3  riastrad 		if (NUM_L3_SLICES(dev_priv) > 1) {
    593  1.3  riastrad 			ret = device_create_bin_file(kdev,
    594  1.2  riastrad 						     &dpf_attrs_1);
    595  1.2  riastrad 			if (ret)
    596  1.2  riastrad 				DRM_ERROR("l3 parity slice 1 setup failed\n");
    597  1.2  riastrad 		}
    598  1.1  riastrad 	}
    599  1.1  riastrad 
    600  1.2  riastrad 	ret = 0;
    601  1.3  riastrad 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
    602  1.3  riastrad 		ret = sysfs_create_files(&kdev->kobj, vlv_attrs);
    603  1.3  riastrad 	else if (INTEL_GEN(dev_priv) >= 6)
    604  1.3  riastrad 		ret = sysfs_create_files(&kdev->kobj, gen6_attrs);
    605  1.2  riastrad 	if (ret)
    606  1.2  riastrad 		DRM_ERROR("RPS sysfs setup failed\n");
    607  1.2  riastrad 
    608  1.3  riastrad 	i915_setup_error_capture(kdev);
    609  1.1  riastrad }
    610  1.1  riastrad 
    611  1.3  riastrad void i915_teardown_sysfs(struct drm_i915_private *dev_priv)
    612  1.1  riastrad {
    613  1.3  riastrad 	struct device *kdev = dev_priv->drm.primary->kdev;
    614  1.3  riastrad 
    615  1.3  riastrad 	i915_teardown_error_capture(kdev);
    616  1.3  riastrad 
    617  1.3  riastrad 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
    618  1.3  riastrad 		sysfs_remove_files(&kdev->kobj, vlv_attrs);
    619  1.2  riastrad 	else
    620  1.3  riastrad 		sysfs_remove_files(&kdev->kobj, gen6_attrs);
    621  1.3  riastrad 	device_remove_bin_file(kdev,  &dpf_attrs_1);
    622  1.3  riastrad 	device_remove_bin_file(kdev,  &dpf_attrs);
    623  1.1  riastrad #ifdef CONFIG_PM
    624  1.3  riastrad 	sysfs_unmerge_group(&kdev->kobj, &rc6_attr_group);
    625  1.3  riastrad 	sysfs_unmerge_group(&kdev->kobj, &rc6p_attr_group);
    626  1.1  riastrad #endif
    627  1.1  riastrad }
    628