2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Ben Widawsky <ben@bwidawsk.net>
28 #include <linux/device.h>
29 #include <linux/module.h>
30 #include <linux/stat.h>
31 #include <linux/sysfs.h>
32 #include "intel_drv.h"
35 static inline struct drm_i915_private
*kdev_minor_to_i915(struct device
*kdev
)
37 struct drm_minor
*minor
= dev_get_drvdata(kdev
);
38 return to_i915(minor
->dev
);
42 static u32
calc_residency(struct drm_i915_private
*dev_priv
,
47 intel_runtime_pm_get(dev_priv
);
48 res
= intel_rc6_residency_us(dev_priv
, reg
);
49 intel_runtime_pm_put(dev_priv
);
51 return DIV_ROUND_CLOSEST_ULL(res
, 1000);
55 show_rc6_mask(struct device
*kdev
, struct device_attribute
*attr
, char *buf
)
57 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
61 if (HAS_RC6(dev_priv
))
63 if (HAS_RC6p(dev_priv
))
65 if (HAS_RC6pp(dev_priv
))
68 return snprintf(buf
, PAGE_SIZE
, "%x\n", mask
);
72 show_rc6_ms(struct device
*kdev
, struct device_attribute
*attr
, char *buf
)
74 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
75 u32 rc6_residency
= calc_residency(dev_priv
, GEN6_GT_GFX_RC6
);
76 return snprintf(buf
, PAGE_SIZE
, "%u\n", rc6_residency
);
80 show_rc6p_ms(struct device
*kdev
, struct device_attribute
*attr
, char *buf
)
82 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
83 u32 rc6p_residency
= calc_residency(dev_priv
, GEN6_GT_GFX_RC6p
);
84 return snprintf(buf
, PAGE_SIZE
, "%u\n", rc6p_residency
);
88 show_rc6pp_ms(struct device
*kdev
, struct device_attribute
*attr
, char *buf
)
90 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
91 u32 rc6pp_residency
= calc_residency(dev_priv
, GEN6_GT_GFX_RC6pp
);
92 return snprintf(buf
, PAGE_SIZE
, "%u\n", rc6pp_residency
);
96 show_media_rc6_ms(struct device
*kdev
, struct device_attribute
*attr
, char *buf
)
98 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
99 u32 rc6_residency
= calc_residency(dev_priv
, VLV_GT_MEDIA_RC6
);
100 return snprintf(buf
, PAGE_SIZE
, "%u\n", rc6_residency
);
103 static DEVICE_ATTR(rc6_enable
, S_IRUGO
, show_rc6_mask
, NULL
);
104 static DEVICE_ATTR(rc6_residency_ms
, S_IRUGO
, show_rc6_ms
, NULL
);
105 static DEVICE_ATTR(rc6p_residency_ms
, S_IRUGO
, show_rc6p_ms
, NULL
);
106 static DEVICE_ATTR(rc6pp_residency_ms
, S_IRUGO
, show_rc6pp_ms
, NULL
);
107 static DEVICE_ATTR(media_rc6_residency_ms
, S_IRUGO
, show_media_rc6_ms
, NULL
);
109 static struct attribute
*rc6_attrs
[] = {
110 &dev_attr_rc6_enable
.attr
,
111 &dev_attr_rc6_residency_ms
.attr
,
115 static const struct attribute_group rc6_attr_group
= {
116 .name
= power_group_name
,
120 static struct attribute
*rc6p_attrs
[] = {
121 &dev_attr_rc6p_residency_ms
.attr
,
122 &dev_attr_rc6pp_residency_ms
.attr
,
126 static const struct attribute_group rc6p_attr_group
= {
127 .name
= power_group_name
,
131 static struct attribute
*media_rc6_attrs
[] = {
132 &dev_attr_media_rc6_residency_ms
.attr
,
136 static const struct attribute_group media_rc6_attr_group
= {
137 .name
= power_group_name
,
138 .attrs
= media_rc6_attrs
142 static int l3_access_valid(struct drm_i915_private
*dev_priv
, loff_t offset
)
144 if (!HAS_L3_DPF(dev_priv
))
150 if (offset
>= GEN7_L3LOG_SIZE
)
157 i915_l3_read(struct file
*filp
, struct kobject
*kobj
,
158 struct bin_attribute
*attr
, char *buf
,
159 loff_t offset
, size_t count
)
161 struct device
*kdev
= kobj_to_dev(kobj
);
162 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
163 struct drm_device
*dev
= &dev_priv
->drm
;
164 int slice
= (int)(uintptr_t)attr
->private;
167 count
= round_down(count
, 4);
169 ret
= l3_access_valid(dev_priv
, offset
);
173 count
= min_t(size_t, GEN7_L3LOG_SIZE
- offset
, count
);
175 ret
= i915_mutex_lock_interruptible(dev
);
179 if (dev_priv
->l3_parity
.remap_info
[slice
])
181 dev_priv
->l3_parity
.remap_info
[slice
] + (offset
/4),
184 memset(buf
, 0, count
);
186 mutex_unlock(&dev
->struct_mutex
);
192 i915_l3_write(struct file
*filp
, struct kobject
*kobj
,
193 struct bin_attribute
*attr
, char *buf
,
194 loff_t offset
, size_t count
)
196 struct device
*kdev
= kobj_to_dev(kobj
);
197 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
198 struct drm_device
*dev
= &dev_priv
->drm
;
199 struct i915_gem_context
*ctx
;
200 int slice
= (int)(uintptr_t)attr
->private;
204 ret
= l3_access_valid(dev_priv
, offset
);
208 ret
= i915_mutex_lock_interruptible(dev
);
212 remap_info
= &dev_priv
->l3_parity
.remap_info
[slice
];
214 *remap_info
= kzalloc(GEN7_L3LOG_SIZE
, GFP_KERNEL
);
221 /* TODO: Ideally we really want a GPU reset here to make sure errors
222 * aren't propagated. Since I cannot find a stable way to reset the GPU
223 * at this point it is left as a TODO.
225 memcpy(*remap_info
+ (offset
/4), buf
, count
);
227 /* NB: We defer the remapping until we switch to the context */
228 list_for_each_entry(ctx
, &dev_priv
->contexts
.list
, link
)
229 ctx
->remap_slice
|= (1<<slice
);
234 mutex_unlock(&dev
->struct_mutex
);
239 static const struct bin_attribute dpf_attrs
= {
240 .attr
= {.name
= "l3_parity", .mode
= (S_IRUSR
| S_IWUSR
)},
241 .size
= GEN7_L3LOG_SIZE
,
242 .read
= i915_l3_read
,
243 .write
= i915_l3_write
,
248 static const struct bin_attribute dpf_attrs_1
= {
249 .attr
= {.name
= "l3_parity_slice_1", .mode
= (S_IRUSR
| S_IWUSR
)},
250 .size
= GEN7_L3LOG_SIZE
,
251 .read
= i915_l3_read
,
252 .write
= i915_l3_write
,
257 static ssize_t
gt_act_freq_mhz_show(struct device
*kdev
,
258 struct device_attribute
*attr
, char *buf
)
260 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
263 intel_runtime_pm_get(dev_priv
);
265 mutex_lock(&dev_priv
->pcu_lock
);
266 if (IS_VALLEYVIEW(dev_priv
) || IS_CHERRYVIEW(dev_priv
)) {
268 freq
= vlv_punit_read(dev_priv
, PUNIT_REG_GPU_FREQ_STS
);
269 ret
= intel_gpu_freq(dev_priv
, (freq
>> 8) & 0xff);
271 ret
= intel_gpu_freq(dev_priv
,
272 intel_get_cagf(dev_priv
,
273 I915_READ(GEN6_RPSTAT1
)));
275 mutex_unlock(&dev_priv
->pcu_lock
);
277 intel_runtime_pm_put(dev_priv
);
279 return snprintf(buf
, PAGE_SIZE
, "%d\n", ret
);
282 static ssize_t
gt_cur_freq_mhz_show(struct device
*kdev
,
283 struct device_attribute
*attr
, char *buf
)
285 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
287 return snprintf(buf
, PAGE_SIZE
, "%d\n",
288 intel_gpu_freq(dev_priv
,
289 dev_priv
->gt_pm
.rps
.cur_freq
));
292 static ssize_t
gt_boost_freq_mhz_show(struct device
*kdev
, struct device_attribute
*attr
, char *buf
)
294 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
296 return snprintf(buf
, PAGE_SIZE
, "%d\n",
297 intel_gpu_freq(dev_priv
,
298 dev_priv
->gt_pm
.rps
.boost_freq
));
301 static ssize_t
gt_boost_freq_mhz_store(struct device
*kdev
,
302 struct device_attribute
*attr
,
303 const char *buf
, size_t count
)
305 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
306 struct intel_rps
*rps
= &dev_priv
->gt_pm
.rps
;
311 ret
= kstrtou32(buf
, 0, &val
);
315 /* Validate against (static) hardware limits */
316 val
= intel_freq_opcode(dev_priv
, val
);
317 if (val
< rps
->min_freq
|| val
> rps
->max_freq
)
320 mutex_lock(&dev_priv
->pcu_lock
);
321 if (val
!= rps
->boost_freq
) {
322 rps
->boost_freq
= val
;
323 boost
= atomic_read(&rps
->num_waiters
);
325 mutex_unlock(&dev_priv
->pcu_lock
);
327 schedule_work(&rps
->work
);
332 static ssize_t
vlv_rpe_freq_mhz_show(struct device
*kdev
,
333 struct device_attribute
*attr
, char *buf
)
335 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
337 return snprintf(buf
, PAGE_SIZE
, "%d\n",
338 intel_gpu_freq(dev_priv
,
339 dev_priv
->gt_pm
.rps
.efficient_freq
));
342 static ssize_t
gt_max_freq_mhz_show(struct device
*kdev
, struct device_attribute
*attr
, char *buf
)
344 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
346 return snprintf(buf
, PAGE_SIZE
, "%d\n",
347 intel_gpu_freq(dev_priv
,
348 dev_priv
->gt_pm
.rps
.max_freq_softlimit
));
351 static ssize_t
gt_max_freq_mhz_store(struct device
*kdev
,
352 struct device_attribute
*attr
,
353 const char *buf
, size_t count
)
355 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
356 struct intel_rps
*rps
= &dev_priv
->gt_pm
.rps
;
360 ret
= kstrtou32(buf
, 0, &val
);
364 intel_runtime_pm_get(dev_priv
);
366 mutex_lock(&dev_priv
->pcu_lock
);
368 val
= intel_freq_opcode(dev_priv
, val
);
370 if (val
< rps
->min_freq
||
371 val
> rps
->max_freq
||
372 val
< rps
->min_freq_softlimit
) {
373 mutex_unlock(&dev_priv
->pcu_lock
);
374 intel_runtime_pm_put(dev_priv
);
378 if (val
> rps
->rp0_freq
)
379 DRM_DEBUG("User requested overclocking to %d\n",
380 intel_gpu_freq(dev_priv
, val
));
382 rps
->max_freq_softlimit
= val
;
384 val
= clamp_t(int, rps
->cur_freq
,
385 rps
->min_freq_softlimit
,
386 rps
->max_freq_softlimit
);
388 /* We still need *_set_rps to process the new max_delay and
389 * update the interrupt limits and PMINTRMSK even though
390 * frequency request may be unchanged. */
391 ret
= intel_set_rps(dev_priv
, val
);
393 mutex_unlock(&dev_priv
->pcu_lock
);
395 intel_runtime_pm_put(dev_priv
);
400 static ssize_t
gt_min_freq_mhz_show(struct device
*kdev
, struct device_attribute
*attr
, char *buf
)
402 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
404 return snprintf(buf
, PAGE_SIZE
, "%d\n",
405 intel_gpu_freq(dev_priv
,
406 dev_priv
->gt_pm
.rps
.min_freq_softlimit
));
409 static ssize_t
gt_min_freq_mhz_store(struct device
*kdev
,
410 struct device_attribute
*attr
,
411 const char *buf
, size_t count
)
413 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
414 struct intel_rps
*rps
= &dev_priv
->gt_pm
.rps
;
418 ret
= kstrtou32(buf
, 0, &val
);
422 intel_runtime_pm_get(dev_priv
);
424 mutex_lock(&dev_priv
->pcu_lock
);
426 val
= intel_freq_opcode(dev_priv
, val
);
428 if (val
< rps
->min_freq
||
429 val
> rps
->max_freq
||
430 val
> rps
->max_freq_softlimit
) {
431 mutex_unlock(&dev_priv
->pcu_lock
);
432 intel_runtime_pm_put(dev_priv
);
436 rps
->min_freq_softlimit
= val
;
438 val
= clamp_t(int, rps
->cur_freq
,
439 rps
->min_freq_softlimit
,
440 rps
->max_freq_softlimit
);
442 /* We still need *_set_rps to process the new min_delay and
443 * update the interrupt limits and PMINTRMSK even though
444 * frequency request may be unchanged. */
445 ret
= intel_set_rps(dev_priv
, val
);
447 mutex_unlock(&dev_priv
->pcu_lock
);
449 intel_runtime_pm_put(dev_priv
);
454 static DEVICE_ATTR_RO(gt_act_freq_mhz
);
455 static DEVICE_ATTR_RO(gt_cur_freq_mhz
);
456 static DEVICE_ATTR_RW(gt_boost_freq_mhz
);
457 static DEVICE_ATTR_RW(gt_max_freq_mhz
);
458 static DEVICE_ATTR_RW(gt_min_freq_mhz
);
460 static DEVICE_ATTR_RO(vlv_rpe_freq_mhz
);
462 static ssize_t
gt_rp_mhz_show(struct device
*kdev
, struct device_attribute
*attr
, char *buf
);
463 static DEVICE_ATTR(gt_RP0_freq_mhz
, S_IRUGO
, gt_rp_mhz_show
, NULL
);
464 static DEVICE_ATTR(gt_RP1_freq_mhz
, S_IRUGO
, gt_rp_mhz_show
, NULL
);
465 static DEVICE_ATTR(gt_RPn_freq_mhz
, S_IRUGO
, gt_rp_mhz_show
, NULL
);
467 /* For now we have a static number of RP states */
468 static ssize_t
gt_rp_mhz_show(struct device
*kdev
, struct device_attribute
*attr
, char *buf
)
470 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
471 struct intel_rps
*rps
= &dev_priv
->gt_pm
.rps
;
474 if (attr
== &dev_attr_gt_RP0_freq_mhz
)
475 val
= intel_gpu_freq(dev_priv
, rps
->rp0_freq
);
476 else if (attr
== &dev_attr_gt_RP1_freq_mhz
)
477 val
= intel_gpu_freq(dev_priv
, rps
->rp1_freq
);
478 else if (attr
== &dev_attr_gt_RPn_freq_mhz
)
479 val
= intel_gpu_freq(dev_priv
, rps
->min_freq
);
483 return snprintf(buf
, PAGE_SIZE
, "%d\n", val
);
486 static const struct attribute
*gen6_attrs
[] = {
487 &dev_attr_gt_act_freq_mhz
.attr
,
488 &dev_attr_gt_cur_freq_mhz
.attr
,
489 &dev_attr_gt_boost_freq_mhz
.attr
,
490 &dev_attr_gt_max_freq_mhz
.attr
,
491 &dev_attr_gt_min_freq_mhz
.attr
,
492 &dev_attr_gt_RP0_freq_mhz
.attr
,
493 &dev_attr_gt_RP1_freq_mhz
.attr
,
494 &dev_attr_gt_RPn_freq_mhz
.attr
,
498 static const struct attribute
*vlv_attrs
[] = {
499 &dev_attr_gt_act_freq_mhz
.attr
,
500 &dev_attr_gt_cur_freq_mhz
.attr
,
501 &dev_attr_gt_boost_freq_mhz
.attr
,
502 &dev_attr_gt_max_freq_mhz
.attr
,
503 &dev_attr_gt_min_freq_mhz
.attr
,
504 &dev_attr_gt_RP0_freq_mhz
.attr
,
505 &dev_attr_gt_RP1_freq_mhz
.attr
,
506 &dev_attr_gt_RPn_freq_mhz
.attr
,
507 &dev_attr_vlv_rpe_freq_mhz
.attr
,
511 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
513 static ssize_t
error_state_read(struct file
*filp
, struct kobject
*kobj
,
514 struct bin_attribute
*attr
, char *buf
,
515 loff_t off
, size_t count
)
518 struct device
*kdev
= kobj_to_dev(kobj
);
519 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
520 struct drm_i915_error_state_buf error_str
;
521 struct i915_gpu_state
*gpu
;
524 ret
= i915_error_state_buf_init(&error_str
, dev_priv
, count
, off
);
528 gpu
= i915_first_error_state(dev_priv
);
529 ret
= i915_error_state_to_str(&error_str
, gpu
);
533 ret
= count
< error_str
.bytes
? count
: error_str
.bytes
;
534 memcpy(buf
, error_str
.buf
, ret
);
537 i915_gpu_state_put(gpu
);
538 i915_error_state_buf_release(&error_str
);
543 static ssize_t
error_state_write(struct file
*file
, struct kobject
*kobj
,
544 struct bin_attribute
*attr
, char *buf
,
545 loff_t off
, size_t count
)
547 struct device
*kdev
= kobj_to_dev(kobj
);
548 struct drm_i915_private
*dev_priv
= kdev_minor_to_i915(kdev
);
550 DRM_DEBUG_DRIVER("Resetting error state\n");
551 i915_reset_error_state(dev_priv
);
556 static const struct bin_attribute error_state_attr
= {
557 .attr
.name
= "error",
558 .attr
.mode
= S_IRUSR
| S_IWUSR
,
560 .read
= error_state_read
,
561 .write
= error_state_write
,
564 static void i915_setup_error_capture(struct device
*kdev
)
566 if (sysfs_create_bin_file(&kdev
->kobj
, &error_state_attr
))
567 DRM_ERROR("error_state sysfs setup failed\n");
570 static void i915_teardown_error_capture(struct device
*kdev
)
572 sysfs_remove_bin_file(&kdev
->kobj
, &error_state_attr
);
575 static void i915_setup_error_capture(struct device
*kdev
) {}
576 static void i915_teardown_error_capture(struct device
*kdev
) {}
579 void i915_setup_sysfs(struct drm_i915_private
*dev_priv
)
581 struct device
*kdev
= dev_priv
->drm
.primary
->kdev
;
585 if (HAS_RC6(dev_priv
)) {
586 ret
= sysfs_merge_group(&kdev
->kobj
,
589 DRM_ERROR("RC6 residency sysfs setup failed\n");
591 if (HAS_RC6p(dev_priv
)) {
592 ret
= sysfs_merge_group(&kdev
->kobj
,
595 DRM_ERROR("RC6p residency sysfs setup failed\n");
597 if (IS_VALLEYVIEW(dev_priv
) || IS_CHERRYVIEW(dev_priv
)) {
598 ret
= sysfs_merge_group(&kdev
->kobj
,
599 &media_rc6_attr_group
);
601 DRM_ERROR("Media RC6 residency sysfs setup failed\n");
604 if (HAS_L3_DPF(dev_priv
)) {
605 ret
= device_create_bin_file(kdev
, &dpf_attrs
);
607 DRM_ERROR("l3 parity sysfs setup failed\n");
609 if (NUM_L3_SLICES(dev_priv
) > 1) {
610 ret
= device_create_bin_file(kdev
,
613 DRM_ERROR("l3 parity slice 1 setup failed\n");
618 if (IS_VALLEYVIEW(dev_priv
) || IS_CHERRYVIEW(dev_priv
))
619 ret
= sysfs_create_files(&kdev
->kobj
, vlv_attrs
);
620 else if (INTEL_GEN(dev_priv
) >= 6)
621 ret
= sysfs_create_files(&kdev
->kobj
, gen6_attrs
);
623 DRM_ERROR("RPS sysfs setup failed\n");
625 i915_setup_error_capture(kdev
);
628 void i915_teardown_sysfs(struct drm_i915_private
*dev_priv
)
630 struct device
*kdev
= dev_priv
->drm
.primary
->kdev
;
632 i915_teardown_error_capture(kdev
);
634 if (IS_VALLEYVIEW(dev_priv
) || IS_CHERRYVIEW(dev_priv
))
635 sysfs_remove_files(&kdev
->kobj
, vlv_attrs
);
637 sysfs_remove_files(&kdev
->kobj
, gen6_attrs
);
638 device_remove_bin_file(kdev
, &dpf_attrs_1
);
639 device_remove_bin_file(kdev
, &dpf_attrs
);
641 sysfs_unmerge_group(&kdev
->kobj
, &rc6_attr_group
);
642 sysfs_unmerge_group(&kdev
->kobj
, &rc6p_attr_group
);