xref: /openbmc/linux/drivers/gpu/drm/i915/i915_sysfs.c (revision 1c966dd26b2e46a9d089fcb7e36f649000670e64)
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 
355ab3633dSHunt Xu #ifdef CONFIG_PM
360136db58SBen Widawsky static u32 calc_residency(struct drm_device *dev, const u32 reg)
370136db58SBen Widawsky {
380136db58SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
390136db58SBen Widawsky 	u64 raw_time; /* 32b value may overflow during fixed point math */
400136db58SBen Widawsky 
410136db58SBen Widawsky 	if (!intel_enable_rc6(dev))
420136db58SBen Widawsky 		return 0;
430136db58SBen Widawsky 
44a85d4bcbSBen Widawsky 	raw_time = I915_READ(reg) * 128ULL;
45a85d4bcbSBen Widawsky 	return DIV_ROUND_UP_ULL(raw_time, 100000);
460136db58SBen Widawsky }
470136db58SBen Widawsky 
480136db58SBen Widawsky static ssize_t
49dbdfd8e9SBen Widawsky show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
500136db58SBen Widawsky {
51dbdfd8e9SBen Widawsky 	struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev);
523e2a1556SJani Nikula 	return snprintf(buf, PAGE_SIZE, "%x\n", intel_enable_rc6(dminor->dev));
530136db58SBen Widawsky }
540136db58SBen Widawsky 
550136db58SBen Widawsky static ssize_t
56dbdfd8e9SBen Widawsky show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
570136db58SBen Widawsky {
58dbdfd8e9SBen Widawsky 	struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev);
590136db58SBen Widawsky 	u32 rc6_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6);
603e2a1556SJani Nikula 	return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
610136db58SBen Widawsky }
620136db58SBen Widawsky 
630136db58SBen Widawsky static ssize_t
64dbdfd8e9SBen Widawsky show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf)
650136db58SBen Widawsky {
66dbdfd8e9SBen Widawsky 	struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev);
670136db58SBen Widawsky 	u32 rc6p_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6p);
683e2a1556SJani Nikula 	return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency);
690136db58SBen Widawsky }
700136db58SBen Widawsky 
710136db58SBen Widawsky static ssize_t
72dbdfd8e9SBen Widawsky show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf)
730136db58SBen Widawsky {
74dbdfd8e9SBen Widawsky 	struct drm_minor *dminor = container_of(kdev, struct drm_minor, kdev);
750136db58SBen Widawsky 	u32 rc6pp_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6pp);
763e2a1556SJani Nikula 	return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency);
770136db58SBen Widawsky }
780136db58SBen Widawsky 
790136db58SBen Widawsky static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL);
800136db58SBen Widawsky static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL);
810136db58SBen Widawsky static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL);
820136db58SBen Widawsky static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL);
830136db58SBen Widawsky 
840136db58SBen Widawsky static struct attribute *rc6_attrs[] = {
850136db58SBen Widawsky 	&dev_attr_rc6_enable.attr,
860136db58SBen Widawsky 	&dev_attr_rc6_residency_ms.attr,
870136db58SBen Widawsky 	&dev_attr_rc6p_residency_ms.attr,
880136db58SBen Widawsky 	&dev_attr_rc6pp_residency_ms.attr,
890136db58SBen Widawsky 	NULL
900136db58SBen Widawsky };
910136db58SBen Widawsky 
920136db58SBen Widawsky static struct attribute_group rc6_attr_group = {
930136db58SBen Widawsky 	.name = power_group_name,
940136db58SBen Widawsky 	.attrs =  rc6_attrs
950136db58SBen Widawsky };
968c3f929bSBen Widawsky #endif
970136db58SBen Widawsky 
9884bc7581SBen Widawsky static int l3_access_valid(struct drm_device *dev, loff_t offset)
9984bc7581SBen Widawsky {
100ebf69cb8SDaniel Vetter 	if (!HAS_L3_GPU_CACHE(dev))
10184bc7581SBen Widawsky 		return -EPERM;
10284bc7581SBen Widawsky 
10384bc7581SBen Widawsky 	if (offset % 4 != 0)
10484bc7581SBen Widawsky 		return -EINVAL;
10584bc7581SBen Widawsky 
10684bc7581SBen Widawsky 	if (offset >= GEN7_L3LOG_SIZE)
10784bc7581SBen Widawsky 		return -ENXIO;
10884bc7581SBen Widawsky 
10984bc7581SBen Widawsky 	return 0;
11084bc7581SBen Widawsky }
11184bc7581SBen Widawsky 
11284bc7581SBen Widawsky static ssize_t
11384bc7581SBen Widawsky i915_l3_read(struct file *filp, struct kobject *kobj,
11484bc7581SBen Widawsky 	     struct bin_attribute *attr, char *buf,
11584bc7581SBen Widawsky 	     loff_t offset, size_t count)
11684bc7581SBen Widawsky {
11784bc7581SBen Widawsky 	struct device *dev = container_of(kobj, struct device, kobj);
11884bc7581SBen Widawsky 	struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev);
11984bc7581SBen Widawsky 	struct drm_device *drm_dev = dminor->dev;
12084bc7581SBen Widawsky 	struct drm_i915_private *dev_priv = drm_dev->dev_private;
12184bc7581SBen Widawsky 	uint32_t misccpctl;
12284bc7581SBen Widawsky 	int i, ret;
12384bc7581SBen Widawsky 
1241c3dcd1cSBen Widawsky 	count = round_down(count, 4);
1251c3dcd1cSBen Widawsky 
12684bc7581SBen Widawsky 	ret = l3_access_valid(drm_dev, offset);
12784bc7581SBen Widawsky 	if (ret)
12884bc7581SBen Widawsky 		return ret;
12984bc7581SBen Widawsky 
13033618ea5SBen Widawsky 	count = min_t(int, GEN7_L3LOG_SIZE-offset, count);
13133618ea5SBen Widawsky 
13284bc7581SBen Widawsky 	ret = i915_mutex_lock_interruptible(drm_dev);
13384bc7581SBen Widawsky 	if (ret)
13484bc7581SBen Widawsky 		return ret;
13584bc7581SBen Widawsky 
136*1c966dd2SBen Widawsky 	if (IS_HASWELL(drm_dev)) {
137*1c966dd2SBen Widawsky 		if (dev_priv->l3_parity.remap_info)
138*1c966dd2SBen Widawsky 			memcpy(buf,
139*1c966dd2SBen Widawsky 			       dev_priv->l3_parity.remap_info + (offset/4),
140*1c966dd2SBen Widawsky 			       count);
141*1c966dd2SBen Widawsky 		else
142*1c966dd2SBen Widawsky 			memset(buf, 0, count);
143*1c966dd2SBen Widawsky 
144*1c966dd2SBen Widawsky 		goto out;
145*1c966dd2SBen Widawsky 	}
146*1c966dd2SBen Widawsky 
14784bc7581SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
14884bc7581SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
14984bc7581SBen Widawsky 
15033618ea5SBen Widawsky 	for (i = 0; i < count; i += 4)
15133618ea5SBen Widawsky 		*((uint32_t *)(&buf[i])) = I915_READ(GEN7_L3LOG_BASE + offset + i);
15284bc7581SBen Widawsky 
15384bc7581SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
15484bc7581SBen Widawsky 
155*1c966dd2SBen Widawsky out:
15684bc7581SBen Widawsky 	mutex_unlock(&drm_dev->struct_mutex);
15784bc7581SBen Widawsky 
158*1c966dd2SBen Widawsky 	return count;
15984bc7581SBen Widawsky }
16084bc7581SBen Widawsky 
16184bc7581SBen Widawsky static ssize_t
16284bc7581SBen Widawsky i915_l3_write(struct file *filp, struct kobject *kobj,
16384bc7581SBen Widawsky 	      struct bin_attribute *attr, char *buf,
16484bc7581SBen Widawsky 	      loff_t offset, size_t count)
16584bc7581SBen Widawsky {
16684bc7581SBen Widawsky 	struct device *dev = container_of(kobj, struct device, kobj);
16784bc7581SBen Widawsky 	struct drm_minor *dminor = container_of(dev, struct drm_minor, kdev);
16884bc7581SBen Widawsky 	struct drm_device *drm_dev = dminor->dev;
16984bc7581SBen Widawsky 	struct drm_i915_private *dev_priv = drm_dev->dev_private;
17084bc7581SBen Widawsky 	u32 *temp = NULL; /* Just here to make handling failures easy */
17184bc7581SBen Widawsky 	int ret;
17284bc7581SBen Widawsky 
17384bc7581SBen Widawsky 	ret = l3_access_valid(drm_dev, offset);
17484bc7581SBen Widawsky 	if (ret)
17584bc7581SBen Widawsky 		return ret;
17684bc7581SBen Widawsky 
17784bc7581SBen Widawsky 	ret = i915_mutex_lock_interruptible(drm_dev);
17884bc7581SBen Widawsky 	if (ret)
17984bc7581SBen Widawsky 		return ret;
18084bc7581SBen Widawsky 
181a4da4fa4SDaniel Vetter 	if (!dev_priv->l3_parity.remap_info) {
18284bc7581SBen Widawsky 		temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
18384bc7581SBen Widawsky 		if (!temp) {
18484bc7581SBen Widawsky 			mutex_unlock(&drm_dev->struct_mutex);
18584bc7581SBen Widawsky 			return -ENOMEM;
18684bc7581SBen Widawsky 		}
18784bc7581SBen Widawsky 	}
18884bc7581SBen Widawsky 
18984bc7581SBen Widawsky 	ret = i915_gpu_idle(drm_dev);
19084bc7581SBen Widawsky 	if (ret) {
19184bc7581SBen Widawsky 		kfree(temp);
19284bc7581SBen Widawsky 		mutex_unlock(&drm_dev->struct_mutex);
19384bc7581SBen Widawsky 		return ret;
19484bc7581SBen Widawsky 	}
19584bc7581SBen Widawsky 
19684bc7581SBen Widawsky 	/* TODO: Ideally we really want a GPU reset here to make sure errors
19784bc7581SBen Widawsky 	 * aren't propagated. Since I cannot find a stable way to reset the GPU
19884bc7581SBen Widawsky 	 * at this point it is left as a TODO.
19984bc7581SBen Widawsky 	*/
20084bc7581SBen Widawsky 	if (temp)
201a4da4fa4SDaniel Vetter 		dev_priv->l3_parity.remap_info = temp;
20284bc7581SBen Widawsky 
20333618ea5SBen Widawsky 	memcpy(dev_priv->l3_parity.remap_info + (offset/4), buf, count);
20484bc7581SBen Widawsky 
20584bc7581SBen Widawsky 	i915_gem_l3_remap(drm_dev);
20684bc7581SBen Widawsky 
20784bc7581SBen Widawsky 	mutex_unlock(&drm_dev->struct_mutex);
20884bc7581SBen Widawsky 
20984bc7581SBen Widawsky 	return count;
21084bc7581SBen Widawsky }
21184bc7581SBen Widawsky 
21284bc7581SBen Widawsky static struct bin_attribute dpf_attrs = {
21384bc7581SBen Widawsky 	.attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
21484bc7581SBen Widawsky 	.size = GEN7_L3LOG_SIZE,
21584bc7581SBen Widawsky 	.read = i915_l3_read,
21684bc7581SBen Widawsky 	.write = i915_l3_write,
21784bc7581SBen Widawsky 	.mmap = NULL
21884bc7581SBen Widawsky };
21984bc7581SBen Widawsky 
220df6eedc8SBen Widawsky static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
221df6eedc8SBen Widawsky 				    struct device_attribute *attr, char *buf)
222df6eedc8SBen Widawsky {
223df6eedc8SBen Widawsky 	struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev);
224df6eedc8SBen Widawsky 	struct drm_device *dev = minor->dev;
225df6eedc8SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
226df6eedc8SBen Widawsky 	int ret;
227df6eedc8SBen Widawsky 
2284fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
229177006a1SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
230177006a1SJesse Barnes 		u32 freq;
23164936258SJani Nikula 		freq = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
232177006a1SJesse Barnes 		ret = vlv_gpu_freq(dev_priv->mem_freq, (freq >> 8) & 0xff);
233177006a1SJesse Barnes 	} else {
234df6eedc8SBen Widawsky 		ret = dev_priv->rps.cur_delay * GT_FREQUENCY_MULTIPLIER;
235177006a1SJesse Barnes 	}
2364fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
237df6eedc8SBen Widawsky 
2383e2a1556SJani Nikula 	return snprintf(buf, PAGE_SIZE, "%d\n", ret);
239df6eedc8SBen Widawsky }
240df6eedc8SBen Widawsky 
24197e4eed7SChris Wilson static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
24297e4eed7SChris Wilson 				     struct device_attribute *attr, char *buf)
24397e4eed7SChris Wilson {
24497e4eed7SChris Wilson 	struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev);
24597e4eed7SChris Wilson 	struct drm_device *dev = minor->dev;
24697e4eed7SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
24797e4eed7SChris Wilson 
24897e4eed7SChris Wilson 	return snprintf(buf, PAGE_SIZE, "%d\n",
24997e4eed7SChris Wilson 			vlv_gpu_freq(dev_priv->mem_freq,
25097e4eed7SChris Wilson 				     dev_priv->rps.rpe_delay));
25197e4eed7SChris Wilson }
25297e4eed7SChris Wilson 
253df6eedc8SBen Widawsky static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
254df6eedc8SBen Widawsky {
255df6eedc8SBen Widawsky 	struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev);
256df6eedc8SBen Widawsky 	struct drm_device *dev = minor->dev;
257df6eedc8SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
258df6eedc8SBen Widawsky 	int ret;
259df6eedc8SBen Widawsky 
2604fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
2610a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
2620a073b84SJesse Barnes 		ret = vlv_gpu_freq(dev_priv->mem_freq, dev_priv->rps.max_delay);
2630a073b84SJesse Barnes 	else
264182642b0SMika Kuoppala 		ret = dev_priv->rps.max_delay * GT_FREQUENCY_MULTIPLIER;
2654fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
266df6eedc8SBen Widawsky 
2673e2a1556SJani Nikula 	return snprintf(buf, PAGE_SIZE, "%d\n", ret);
268df6eedc8SBen Widawsky }
269df6eedc8SBen Widawsky 
27046ddf194SBen Widawsky static ssize_t gt_max_freq_mhz_store(struct device *kdev,
27146ddf194SBen Widawsky 				     struct device_attribute *attr,
27246ddf194SBen Widawsky 				     const char *buf, size_t count)
27346ddf194SBen Widawsky {
27446ddf194SBen Widawsky 	struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev);
27546ddf194SBen Widawsky 	struct drm_device *dev = minor->dev;
27646ddf194SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
27731c77388SBen Widawsky 	u32 val, rp_state_cap, hw_max, hw_min, non_oc_max;
27846ddf194SBen Widawsky 	ssize_t ret;
27946ddf194SBen Widawsky 
28046ddf194SBen Widawsky 	ret = kstrtou32(buf, 0, &val);
28146ddf194SBen Widawsky 	if (ret)
28246ddf194SBen Widawsky 		return ret;
28346ddf194SBen Widawsky 
2844fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
28546ddf194SBen Widawsky 
2860a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev)) {
2870a073b84SJesse Barnes 		val = vlv_freq_opcode(dev_priv->mem_freq, val);
2880a073b84SJesse Barnes 
2890a073b84SJesse Barnes 		hw_max = valleyview_rps_max_freq(dev_priv);
2900a073b84SJesse Barnes 		hw_min = valleyview_rps_min_freq(dev_priv);
2910a073b84SJesse Barnes 		non_oc_max = hw_max;
2920a073b84SJesse Barnes 	} else {
2930a073b84SJesse Barnes 		val /= GT_FREQUENCY_MULTIPLIER;
2940a073b84SJesse Barnes 
29546ddf194SBen Widawsky 		rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
29631c77388SBen Widawsky 		hw_max = dev_priv->rps.hw_max;
29731c77388SBen Widawsky 		non_oc_max = (rp_state_cap & 0xff);
29846ddf194SBen Widawsky 		hw_min = ((rp_state_cap & 0xff0000) >> 16);
2990a073b84SJesse Barnes 	}
30046ddf194SBen Widawsky 
3010a073b84SJesse Barnes 	if (val < hw_min || val > hw_max ||
3020a073b84SJesse Barnes 	    val < dev_priv->rps.min_delay) {
3034fc688ceSJesse Barnes 		mutex_unlock(&dev_priv->rps.hw_lock);
30446ddf194SBen Widawsky 		return -EINVAL;
30546ddf194SBen Widawsky 	}
30646ddf194SBen Widawsky 
30731c77388SBen Widawsky 	if (val > non_oc_max)
30831c77388SBen Widawsky 		DRM_DEBUG("User requested overclocking to %d\n",
30931c77388SBen Widawsky 			  val * GT_FREQUENCY_MULTIPLIER);
31031c77388SBen Widawsky 
3110a073b84SJesse Barnes 	if (dev_priv->rps.cur_delay > val) {
3120a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev_priv->dev))
3130a073b84SJesse Barnes 			valleyview_set_rps(dev_priv->dev, val);
3140a073b84SJesse Barnes 		else
31546ddf194SBen Widawsky 			gen6_set_rps(dev_priv->dev, val);
3160a073b84SJesse Barnes 	}
31746ddf194SBen Widawsky 
31846ddf194SBen Widawsky 	dev_priv->rps.max_delay = val;
31946ddf194SBen Widawsky 
3204fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
32146ddf194SBen Widawsky 
32246ddf194SBen Widawsky 	return count;
32346ddf194SBen Widawsky }
32446ddf194SBen Widawsky 
325df6eedc8SBen Widawsky static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
326df6eedc8SBen Widawsky {
327df6eedc8SBen Widawsky 	struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev);
328df6eedc8SBen Widawsky 	struct drm_device *dev = minor->dev;
329df6eedc8SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
330df6eedc8SBen Widawsky 	int ret;
331df6eedc8SBen Widawsky 
3324fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
3330a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
3340a073b84SJesse Barnes 		ret = vlv_gpu_freq(dev_priv->mem_freq, dev_priv->rps.min_delay);
3350a073b84SJesse Barnes 	else
336df6eedc8SBen Widawsky 		ret = dev_priv->rps.min_delay * GT_FREQUENCY_MULTIPLIER;
3374fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
338df6eedc8SBen Widawsky 
3393e2a1556SJani Nikula 	return snprintf(buf, PAGE_SIZE, "%d\n", ret);
340df6eedc8SBen Widawsky }
341df6eedc8SBen Widawsky 
34246ddf194SBen Widawsky static ssize_t gt_min_freq_mhz_store(struct device *kdev,
34346ddf194SBen Widawsky 				     struct device_attribute *attr,
34446ddf194SBen Widawsky 				     const char *buf, size_t count)
34546ddf194SBen Widawsky {
34646ddf194SBen Widawsky 	struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev);
34746ddf194SBen Widawsky 	struct drm_device *dev = minor->dev;
34846ddf194SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
34946ddf194SBen Widawsky 	u32 val, rp_state_cap, hw_max, hw_min;
35046ddf194SBen Widawsky 	ssize_t ret;
35146ddf194SBen Widawsky 
35246ddf194SBen Widawsky 	ret = kstrtou32(buf, 0, &val);
35346ddf194SBen Widawsky 	if (ret)
35446ddf194SBen Widawsky 		return ret;
35546ddf194SBen Widawsky 
3564fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
35746ddf194SBen Widawsky 
3580a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
3590a073b84SJesse Barnes 		val = vlv_freq_opcode(dev_priv->mem_freq, val);
3600a073b84SJesse Barnes 
3610a073b84SJesse Barnes 		hw_max = valleyview_rps_max_freq(dev_priv);
3620a073b84SJesse Barnes 		hw_min = valleyview_rps_min_freq(dev_priv);
3630a073b84SJesse Barnes 	} else {
3640a073b84SJesse Barnes 		val /= GT_FREQUENCY_MULTIPLIER;
3650a073b84SJesse Barnes 
36646ddf194SBen Widawsky 		rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
36731c77388SBen Widawsky 		hw_max = dev_priv->rps.hw_max;
36846ddf194SBen Widawsky 		hw_min = ((rp_state_cap & 0xff0000) >> 16);
3690a073b84SJesse Barnes 	}
37046ddf194SBen Widawsky 
37146ddf194SBen Widawsky 	if (val < hw_min || val > hw_max || val > dev_priv->rps.max_delay) {
3724fc688ceSJesse Barnes 		mutex_unlock(&dev_priv->rps.hw_lock);
37346ddf194SBen Widawsky 		return -EINVAL;
37446ddf194SBen Widawsky 	}
37546ddf194SBen Widawsky 
3760a073b84SJesse Barnes 	if (dev_priv->rps.cur_delay < val) {
3770a073b84SJesse Barnes 		if (IS_VALLEYVIEW(dev))
3780a073b84SJesse Barnes 			valleyview_set_rps(dev, val);
3790a073b84SJesse Barnes 		else
38046ddf194SBen Widawsky 			gen6_set_rps(dev_priv->dev, val);
3810a073b84SJesse Barnes 	}
38246ddf194SBen Widawsky 
38346ddf194SBen Widawsky 	dev_priv->rps.min_delay = val;
38446ddf194SBen Widawsky 
3854fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
38646ddf194SBen Widawsky 
38746ddf194SBen Widawsky 	return count;
38846ddf194SBen Widawsky 
38946ddf194SBen Widawsky }
39046ddf194SBen Widawsky 
391df6eedc8SBen Widawsky static DEVICE_ATTR(gt_cur_freq_mhz, S_IRUGO, gt_cur_freq_mhz_show, NULL);
39246ddf194SBen Widawsky static DEVICE_ATTR(gt_max_freq_mhz, S_IRUGO | S_IWUSR, gt_max_freq_mhz_show, gt_max_freq_mhz_store);
39346ddf194SBen Widawsky static DEVICE_ATTR(gt_min_freq_mhz, S_IRUGO | S_IWUSR, gt_min_freq_mhz_show, gt_min_freq_mhz_store);
394df6eedc8SBen Widawsky 
39597e4eed7SChris Wilson static DEVICE_ATTR(vlv_rpe_freq_mhz, S_IRUGO, vlv_rpe_freq_mhz_show, NULL);
396ac6ae347SBen Widawsky 
397ac6ae347SBen Widawsky static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
398ac6ae347SBen Widawsky static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
399ac6ae347SBen Widawsky static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
400ac6ae347SBen Widawsky static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
401ac6ae347SBen Widawsky 
402ac6ae347SBen Widawsky /* For now we have a static number of RP states */
403ac6ae347SBen Widawsky static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
404ac6ae347SBen Widawsky {
405ac6ae347SBen Widawsky 	struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev);
406ac6ae347SBen Widawsky 	struct drm_device *dev = minor->dev;
407ac6ae347SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
408ac6ae347SBen Widawsky 	u32 val, rp_state_cap;
409ac6ae347SBen Widawsky 	ssize_t ret;
410ac6ae347SBen Widawsky 
411ac6ae347SBen Widawsky 	ret = mutex_lock_interruptible(&dev->struct_mutex);
412ac6ae347SBen Widawsky 	if (ret)
413ac6ae347SBen Widawsky 		return ret;
414ac6ae347SBen Widawsky 	rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
415ac6ae347SBen Widawsky 	mutex_unlock(&dev->struct_mutex);
416ac6ae347SBen Widawsky 
417ac6ae347SBen Widawsky 	if (attr == &dev_attr_gt_RP0_freq_mhz) {
418ac6ae347SBen Widawsky 		val = ((rp_state_cap & 0x0000ff) >> 0) * GT_FREQUENCY_MULTIPLIER;
419ac6ae347SBen Widawsky 	} else if (attr == &dev_attr_gt_RP1_freq_mhz) {
420ac6ae347SBen Widawsky 		val = ((rp_state_cap & 0x00ff00) >> 8) * GT_FREQUENCY_MULTIPLIER;
421ac6ae347SBen Widawsky 	} else if (attr == &dev_attr_gt_RPn_freq_mhz) {
422ac6ae347SBen Widawsky 		val = ((rp_state_cap & 0xff0000) >> 16) * GT_FREQUENCY_MULTIPLIER;
423ac6ae347SBen Widawsky 	} else {
424ac6ae347SBen Widawsky 		BUG();
425ac6ae347SBen Widawsky 	}
4263e2a1556SJani Nikula 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
427ac6ae347SBen Widawsky }
428ac6ae347SBen Widawsky 
429df6eedc8SBen Widawsky static const struct attribute *gen6_attrs[] = {
430df6eedc8SBen Widawsky 	&dev_attr_gt_cur_freq_mhz.attr,
431df6eedc8SBen Widawsky 	&dev_attr_gt_max_freq_mhz.attr,
432df6eedc8SBen Widawsky 	&dev_attr_gt_min_freq_mhz.attr,
433ac6ae347SBen Widawsky 	&dev_attr_gt_RP0_freq_mhz.attr,
434ac6ae347SBen Widawsky 	&dev_attr_gt_RP1_freq_mhz.attr,
435ac6ae347SBen Widawsky 	&dev_attr_gt_RPn_freq_mhz.attr,
436df6eedc8SBen Widawsky 	NULL,
437df6eedc8SBen Widawsky };
438df6eedc8SBen Widawsky 
43997e4eed7SChris Wilson static const struct attribute *vlv_attrs[] = {
44097e4eed7SChris Wilson 	&dev_attr_gt_cur_freq_mhz.attr,
44197e4eed7SChris Wilson 	&dev_attr_gt_max_freq_mhz.attr,
44297e4eed7SChris Wilson 	&dev_attr_gt_min_freq_mhz.attr,
44397e4eed7SChris Wilson 	&dev_attr_vlv_rpe_freq_mhz.attr,
44497e4eed7SChris Wilson 	NULL,
44597e4eed7SChris Wilson };
44697e4eed7SChris Wilson 
447ef86ddceSMika Kuoppala static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
448ef86ddceSMika Kuoppala 				struct bin_attribute *attr, char *buf,
449ef86ddceSMika Kuoppala 				loff_t off, size_t count)
450ef86ddceSMika Kuoppala {
451ef86ddceSMika Kuoppala 
452ef86ddceSMika Kuoppala 	struct device *kdev = container_of(kobj, struct device, kobj);
453ef86ddceSMika Kuoppala 	struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev);
454ef86ddceSMika Kuoppala 	struct drm_device *dev = minor->dev;
455ef86ddceSMika Kuoppala 	struct i915_error_state_file_priv error_priv;
456ef86ddceSMika Kuoppala 	struct drm_i915_error_state_buf error_str;
457ef86ddceSMika Kuoppala 	ssize_t ret_count = 0;
458ef86ddceSMika Kuoppala 	int ret;
459ef86ddceSMika Kuoppala 
460ef86ddceSMika Kuoppala 	memset(&error_priv, 0, sizeof(error_priv));
461ef86ddceSMika Kuoppala 
462ef86ddceSMika Kuoppala 	ret = i915_error_state_buf_init(&error_str, count, off);
463ef86ddceSMika Kuoppala 	if (ret)
464ef86ddceSMika Kuoppala 		return ret;
465ef86ddceSMika Kuoppala 
466ef86ddceSMika Kuoppala 	error_priv.dev = dev;
467ef86ddceSMika Kuoppala 	i915_error_state_get(dev, &error_priv);
468ef86ddceSMika Kuoppala 
469ef86ddceSMika Kuoppala 	ret = i915_error_state_to_str(&error_str, &error_priv);
470ef86ddceSMika Kuoppala 	if (ret)
471ef86ddceSMika Kuoppala 		goto out;
472ef86ddceSMika Kuoppala 
473ef86ddceSMika Kuoppala 	ret_count = count < error_str.bytes ? count : error_str.bytes;
474ef86ddceSMika Kuoppala 
475ef86ddceSMika Kuoppala 	memcpy(buf, error_str.buf, ret_count);
476ef86ddceSMika Kuoppala out:
477ef86ddceSMika Kuoppala 	i915_error_state_put(&error_priv);
478ef86ddceSMika Kuoppala 	i915_error_state_buf_release(&error_str);
479ef86ddceSMika Kuoppala 
480ef86ddceSMika Kuoppala 	return ret ?: ret_count;
481ef86ddceSMika Kuoppala }
482ef86ddceSMika Kuoppala 
483ef86ddceSMika Kuoppala static ssize_t error_state_write(struct file *file, struct kobject *kobj,
484ef86ddceSMika Kuoppala 				 struct bin_attribute *attr, char *buf,
485ef86ddceSMika Kuoppala 				 loff_t off, size_t count)
486ef86ddceSMika Kuoppala {
487ef86ddceSMika Kuoppala 	struct device *kdev = container_of(kobj, struct device, kobj);
488ef86ddceSMika Kuoppala 	struct drm_minor *minor = container_of(kdev, struct drm_minor, kdev);
489ef86ddceSMika Kuoppala 	struct drm_device *dev = minor->dev;
490ef86ddceSMika Kuoppala 	int ret;
491ef86ddceSMika Kuoppala 
492ef86ddceSMika Kuoppala 	DRM_DEBUG_DRIVER("Resetting error state\n");
493ef86ddceSMika Kuoppala 
494ef86ddceSMika Kuoppala 	ret = mutex_lock_interruptible(&dev->struct_mutex);
495ef86ddceSMika Kuoppala 	if (ret)
496ef86ddceSMika Kuoppala 		return ret;
497ef86ddceSMika Kuoppala 
498ef86ddceSMika Kuoppala 	i915_destroy_error_state(dev);
499ef86ddceSMika Kuoppala 	mutex_unlock(&dev->struct_mutex);
500ef86ddceSMika Kuoppala 
501ef86ddceSMika Kuoppala 	return count;
502ef86ddceSMika Kuoppala }
503ef86ddceSMika Kuoppala 
504ef86ddceSMika Kuoppala static struct bin_attribute error_state_attr = {
505ef86ddceSMika Kuoppala 	.attr.name = "error",
506ef86ddceSMika Kuoppala 	.attr.mode = S_IRUSR | S_IWUSR,
507ef86ddceSMika Kuoppala 	.size = 0,
508ef86ddceSMika Kuoppala 	.read = error_state_read,
509ef86ddceSMika Kuoppala 	.write = error_state_write,
510ef86ddceSMika Kuoppala };
511ef86ddceSMika Kuoppala 
5120136db58SBen Widawsky void i915_setup_sysfs(struct drm_device *dev)
5130136db58SBen Widawsky {
5140136db58SBen Widawsky 	int ret;
5150136db58SBen Widawsky 
5168c3f929bSBen Widawsky #ifdef CONFIG_PM
517112abd29SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
518112abd29SDaniel Vetter 		ret = sysfs_merge_group(&dev->primary->kdev.kobj,
519112abd29SDaniel Vetter 					&rc6_attr_group);
5200136db58SBen Widawsky 		if (ret)
52184bc7581SBen Widawsky 			DRM_ERROR("RC6 residency sysfs setup failed\n");
522112abd29SDaniel Vetter 	}
5238c3f929bSBen Widawsky #endif
524e1ef7cc2SBen Widawsky 	if (HAS_L3_GPU_CACHE(dev)) {
52584bc7581SBen Widawsky 		ret = device_create_bin_file(&dev->primary->kdev, &dpf_attrs);
52684bc7581SBen Widawsky 		if (ret)
52784bc7581SBen Widawsky 			DRM_ERROR("l3 parity sysfs setup failed\n");
5280136db58SBen Widawsky 	}
529df6eedc8SBen Widawsky 
53097e4eed7SChris Wilson 	ret = 0;
53197e4eed7SChris Wilson 	if (IS_VALLEYVIEW(dev))
53297e4eed7SChris Wilson 		ret = sysfs_create_files(&dev->primary->kdev.kobj, vlv_attrs);
53397e4eed7SChris Wilson 	else if (INTEL_INFO(dev)->gen >= 6)
534df6eedc8SBen Widawsky 		ret = sysfs_create_files(&dev->primary->kdev.kobj, gen6_attrs);
535df6eedc8SBen Widawsky 	if (ret)
53697e4eed7SChris Wilson 		DRM_ERROR("RPS sysfs setup failed\n");
537ef86ddceSMika Kuoppala 
538ef86ddceSMika Kuoppala 	ret = sysfs_create_bin_file(&dev->primary->kdev.kobj,
539ef86ddceSMika Kuoppala 				    &error_state_attr);
540ef86ddceSMika Kuoppala 	if (ret)
541ef86ddceSMika Kuoppala 		DRM_ERROR("error_state sysfs setup failed\n");
542112abd29SDaniel Vetter }
5430136db58SBen Widawsky 
5440136db58SBen Widawsky void i915_teardown_sysfs(struct drm_device *dev)
5450136db58SBen Widawsky {
546ef86ddceSMika Kuoppala 	sysfs_remove_bin_file(&dev->primary->kdev.kobj, &error_state_attr);
54797e4eed7SChris Wilson 	if (IS_VALLEYVIEW(dev))
54897e4eed7SChris Wilson 		sysfs_remove_files(&dev->primary->kdev.kobj, vlv_attrs);
54997e4eed7SChris Wilson 	else
550df6eedc8SBen Widawsky 		sysfs_remove_files(&dev->primary->kdev.kobj, gen6_attrs);
55184bc7581SBen Widawsky 	device_remove_bin_file(&dev->primary->kdev,  &dpf_attrs);
552853c70e8SBen Widawsky #ifdef CONFIG_PM
5530136db58SBen Widawsky 	sysfs_unmerge_group(&dev->primary->kdev.kobj, &rc6_attr_group);
554853c70e8SBen Widawsky #endif
5550136db58SBen Widawsky }
556