xref: /openbmc/linux/drivers/gpu/drm/i915/i915_sysfs.c (revision c6acb1e7)
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Ben Widawsky <ben@bwidawsk.net>
25  *
26  */
27 
28 #include <linux/device.h>
29 #include <linux/module.h>
30 #include <linux/stat.h>
31 #include <linux/sysfs.h>
32 
33 #include "gt/intel_rc6.h"
34 #include "gt/intel_rps.h"
35 #include "gt/sysfs_engines.h"
36 
37 #include "i915_drv.h"
38 #include "i915_sysfs.h"
39 #include "intel_pm.h"
40 
41 static inline struct drm_i915_private *kdev_minor_to_i915(struct device *kdev)
42 {
43 	struct drm_minor *minor = dev_get_drvdata(kdev);
44 	return to_i915(minor->dev);
45 }
46 
47 #ifdef CONFIG_PM
48 static u32 calc_residency(struct drm_i915_private *dev_priv,
49 			  i915_reg_t reg)
50 {
51 	intel_wakeref_t wakeref;
52 	u64 res = 0;
53 
54 	with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref)
55 		res = intel_rc6_residency_us(&dev_priv->gt.rc6, reg);
56 
57 	return DIV_ROUND_CLOSEST_ULL(res, 1000);
58 }
59 
60 static ssize_t rc6_enable_show(struct device *kdev,
61 			       struct device_attribute *attr, char *buf)
62 {
63 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
64 	unsigned int mask;
65 
66 	mask = 0;
67 	if (HAS_RC6(dev_priv))
68 		mask |= BIT(0);
69 	if (HAS_RC6p(dev_priv))
70 		mask |= BIT(1);
71 	if (HAS_RC6pp(dev_priv))
72 		mask |= BIT(2);
73 
74 	return sysfs_emit(buf, "%x\n", mask);
75 }
76 
77 static ssize_t rc6_residency_ms_show(struct device *kdev,
78 				     struct device_attribute *attr, char *buf)
79 {
80 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
81 	u32 rc6_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6);
82 	return sysfs_emit(buf, "%u\n", rc6_residency);
83 }
84 
85 static ssize_t rc6p_residency_ms_show(struct device *kdev,
86 				      struct device_attribute *attr, char *buf)
87 {
88 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
89 	u32 rc6p_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6p);
90 	return sysfs_emit(buf, "%u\n", rc6p_residency);
91 }
92 
93 static ssize_t rc6pp_residency_ms_show(struct device *kdev,
94 				       struct device_attribute *attr, char *buf)
95 {
96 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
97 	u32 rc6pp_residency = calc_residency(dev_priv, GEN6_GT_GFX_RC6pp);
98 	return sysfs_emit(buf, "%u\n", rc6pp_residency);
99 }
100 
101 static ssize_t media_rc6_residency_ms_show(struct device *kdev,
102 					   struct device_attribute *attr, char *buf)
103 {
104 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
105 	u32 rc6_residency = calc_residency(dev_priv, VLV_GT_MEDIA_RC6);
106 	return sysfs_emit(buf, "%u\n", rc6_residency);
107 }
108 
109 static DEVICE_ATTR_RO(rc6_enable);
110 static DEVICE_ATTR_RO(rc6_residency_ms);
111 static DEVICE_ATTR_RO(rc6p_residency_ms);
112 static DEVICE_ATTR_RO(rc6pp_residency_ms);
113 static DEVICE_ATTR_RO(media_rc6_residency_ms);
114 
115 static struct attribute *rc6_attrs[] = {
116 	&dev_attr_rc6_enable.attr,
117 	&dev_attr_rc6_residency_ms.attr,
118 	NULL
119 };
120 
121 static const struct attribute_group rc6_attr_group = {
122 	.name = power_group_name,
123 	.attrs =  rc6_attrs
124 };
125 
126 static struct attribute *rc6p_attrs[] = {
127 	&dev_attr_rc6p_residency_ms.attr,
128 	&dev_attr_rc6pp_residency_ms.attr,
129 	NULL
130 };
131 
132 static const struct attribute_group rc6p_attr_group = {
133 	.name = power_group_name,
134 	.attrs =  rc6p_attrs
135 };
136 
137 static struct attribute *media_rc6_attrs[] = {
138 	&dev_attr_media_rc6_residency_ms.attr,
139 	NULL
140 };
141 
142 static const struct attribute_group media_rc6_attr_group = {
143 	.name = power_group_name,
144 	.attrs =  media_rc6_attrs
145 };
146 #endif
147 
148 static int l3_access_valid(struct drm_i915_private *i915, loff_t offset)
149 {
150 	if (!HAS_L3_DPF(i915))
151 		return -EPERM;
152 
153 	if (!IS_ALIGNED(offset, sizeof(u32)))
154 		return -EINVAL;
155 
156 	if (offset >= GEN7_L3LOG_SIZE)
157 		return -ENXIO;
158 
159 	return 0;
160 }
161 
162 static ssize_t
163 i915_l3_read(struct file *filp, struct kobject *kobj,
164 	     struct bin_attribute *attr, char *buf,
165 	     loff_t offset, size_t count)
166 {
167 	struct device *kdev = kobj_to_dev(kobj);
168 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
169 	int slice = (int)(uintptr_t)attr->private;
170 	int ret;
171 
172 	ret = l3_access_valid(i915, offset);
173 	if (ret)
174 		return ret;
175 
176 	count = round_down(count, sizeof(u32));
177 	count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
178 	memset(buf, 0, count);
179 
180 	spin_lock(&i915->gem.contexts.lock);
181 	if (i915->l3_parity.remap_info[slice])
182 		memcpy(buf,
183 		       i915->l3_parity.remap_info[slice] + offset / sizeof(u32),
184 		       count);
185 	spin_unlock(&i915->gem.contexts.lock);
186 
187 	return count;
188 }
189 
190 static ssize_t
191 i915_l3_write(struct file *filp, struct kobject *kobj,
192 	      struct bin_attribute *attr, char *buf,
193 	      loff_t offset, size_t count)
194 {
195 	struct device *kdev = kobj_to_dev(kobj);
196 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
197 	int slice = (int)(uintptr_t)attr->private;
198 	u32 *remap_info, *freeme = NULL;
199 	struct i915_gem_context *ctx;
200 	int ret;
201 
202 	ret = l3_access_valid(i915, offset);
203 	if (ret)
204 		return ret;
205 
206 	if (count < sizeof(u32))
207 		return -EINVAL;
208 
209 	remap_info = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
210 	if (!remap_info)
211 		return -ENOMEM;
212 
213 	spin_lock(&i915->gem.contexts.lock);
214 
215 	if (i915->l3_parity.remap_info[slice]) {
216 		freeme = remap_info;
217 		remap_info = i915->l3_parity.remap_info[slice];
218 	} else {
219 		i915->l3_parity.remap_info[slice] = remap_info;
220 	}
221 
222 	count = round_down(count, sizeof(u32));
223 	memcpy(remap_info + offset / sizeof(u32), buf, count);
224 
225 	/* NB: We defer the remapping until we switch to the context */
226 	list_for_each_entry(ctx, &i915->gem.contexts.list, link)
227 		ctx->remap_slice |= BIT(slice);
228 
229 	spin_unlock(&i915->gem.contexts.lock);
230 	kfree(freeme);
231 
232 	/*
233 	 * TODO: Ideally we really want a GPU reset here to make sure errors
234 	 * aren't propagated. Since I cannot find a stable way to reset the GPU
235 	 * at this point it is left as a TODO.
236 	*/
237 
238 	return count;
239 }
240 
241 static const struct bin_attribute dpf_attrs = {
242 	.attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
243 	.size = GEN7_L3LOG_SIZE,
244 	.read = i915_l3_read,
245 	.write = i915_l3_write,
246 	.mmap = NULL,
247 	.private = (void *)0
248 };
249 
250 static const struct bin_attribute dpf_attrs_1 = {
251 	.attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
252 	.size = GEN7_L3LOG_SIZE,
253 	.read = i915_l3_read,
254 	.write = i915_l3_write,
255 	.mmap = NULL,
256 	.private = (void *)1
257 };
258 
259 static ssize_t gt_act_freq_mhz_show(struct device *kdev,
260 				    struct device_attribute *attr, char *buf)
261 {
262 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
263 	struct intel_rps *rps = &i915->gt.rps;
264 
265 	return sysfs_emit(buf, "%d\n", intel_rps_read_actual_frequency(rps));
266 }
267 
268 static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
269 				    struct device_attribute *attr, char *buf)
270 {
271 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
272 	struct intel_rps *rps = &i915->gt.rps;
273 
274 	return sysfs_emit(buf, "%d\n", intel_rps_get_requested_frequency(rps));
275 }
276 
277 static ssize_t gt_boost_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
278 {
279 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
280 	struct intel_rps *rps = &i915->gt.rps;
281 
282 	return sysfs_emit(buf, "%d\n", intel_gpu_freq(rps, rps->boost_freq));
283 }
284 
285 static ssize_t gt_boost_freq_mhz_store(struct device *kdev,
286 				       struct device_attribute *attr,
287 				       const char *buf, size_t count)
288 {
289 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
290 	struct intel_rps *rps = &dev_priv->gt.rps;
291 	bool boost = false;
292 	ssize_t ret;
293 	u32 val;
294 
295 	ret = kstrtou32(buf, 0, &val);
296 	if (ret)
297 		return ret;
298 
299 	/* Validate against (static) hardware limits */
300 	val = intel_freq_opcode(rps, val);
301 	if (val < rps->min_freq || val > rps->max_freq)
302 		return -EINVAL;
303 
304 	mutex_lock(&rps->lock);
305 	if (val != rps->boost_freq) {
306 		rps->boost_freq = val;
307 		boost = atomic_read(&rps->num_waiters);
308 	}
309 	mutex_unlock(&rps->lock);
310 	if (boost)
311 		schedule_work(&rps->work);
312 
313 	return count;
314 }
315 
316 static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
317 				     struct device_attribute *attr, char *buf)
318 {
319 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
320 	struct intel_rps *rps = &dev_priv->gt.rps;
321 
322 	return sysfs_emit(buf, "%d\n", intel_gpu_freq(rps, rps->efficient_freq));
323 }
324 
325 static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
326 {
327 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
328 	struct intel_gt *gt = &dev_priv->gt;
329 	struct intel_rps *rps = &gt->rps;
330 
331 	return sysfs_emit(buf, "%d\n", intel_rps_get_max_frequency(rps));
332 }
333 
334 static ssize_t gt_max_freq_mhz_store(struct device *kdev,
335 				     struct device_attribute *attr,
336 				     const char *buf, size_t count)
337 {
338 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
339 	struct intel_gt *gt = &dev_priv->gt;
340 	struct intel_rps *rps = &gt->rps;
341 	ssize_t ret;
342 	u32 val;
343 
344 	ret = kstrtou32(buf, 0, &val);
345 	if (ret)
346 		return ret;
347 
348 	ret = intel_rps_set_max_frequency(rps, val);
349 
350 	return ret ?: count;
351 }
352 
353 static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
354 {
355 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
356 	struct intel_gt *gt = &i915->gt;
357 	struct intel_rps *rps = &gt->rps;
358 
359 	return sysfs_emit(buf, "%d\n", intel_rps_get_min_frequency(rps));
360 }
361 
362 static ssize_t gt_min_freq_mhz_store(struct device *kdev,
363 				     struct device_attribute *attr,
364 				     const char *buf, size_t count)
365 {
366 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
367 	struct intel_rps *rps = &i915->gt.rps;
368 	ssize_t ret;
369 	u32 val;
370 
371 	ret = kstrtou32(buf, 0, &val);
372 	if (ret)
373 		return ret;
374 
375 	ret = intel_rps_set_min_frequency(rps, val);
376 
377 	return ret ?: count;
378 }
379 
380 static DEVICE_ATTR_RO(gt_act_freq_mhz);
381 static DEVICE_ATTR_RO(gt_cur_freq_mhz);
382 static DEVICE_ATTR_RW(gt_boost_freq_mhz);
383 static DEVICE_ATTR_RW(gt_max_freq_mhz);
384 static DEVICE_ATTR_RW(gt_min_freq_mhz);
385 
386 static DEVICE_ATTR_RO(vlv_rpe_freq_mhz);
387 
388 static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
389 static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
390 static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
391 static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
392 
393 /* For now we have a static number of RP states */
394 static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
395 {
396 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
397 	struct intel_rps *rps = &dev_priv->gt.rps;
398 	u32 val;
399 
400 	if (attr == &dev_attr_gt_RP0_freq_mhz)
401 		val = intel_rps_get_rp0_frequency(rps);
402 	else if (attr == &dev_attr_gt_RP1_freq_mhz)
403 		val = intel_rps_get_rp1_frequency(rps);
404 	else if (attr == &dev_attr_gt_RPn_freq_mhz)
405 		val = intel_rps_get_rpn_frequency(rps);
406 	else
407 		BUG();
408 
409 	return sysfs_emit(buf, "%d\n", val);
410 }
411 
412 static const struct attribute * const gen6_attrs[] = {
413 	&dev_attr_gt_act_freq_mhz.attr,
414 	&dev_attr_gt_cur_freq_mhz.attr,
415 	&dev_attr_gt_boost_freq_mhz.attr,
416 	&dev_attr_gt_max_freq_mhz.attr,
417 	&dev_attr_gt_min_freq_mhz.attr,
418 	&dev_attr_gt_RP0_freq_mhz.attr,
419 	&dev_attr_gt_RP1_freq_mhz.attr,
420 	&dev_attr_gt_RPn_freq_mhz.attr,
421 	NULL,
422 };
423 
424 static const struct attribute * const vlv_attrs[] = {
425 	&dev_attr_gt_act_freq_mhz.attr,
426 	&dev_attr_gt_cur_freq_mhz.attr,
427 	&dev_attr_gt_boost_freq_mhz.attr,
428 	&dev_attr_gt_max_freq_mhz.attr,
429 	&dev_attr_gt_min_freq_mhz.attr,
430 	&dev_attr_gt_RP0_freq_mhz.attr,
431 	&dev_attr_gt_RP1_freq_mhz.attr,
432 	&dev_attr_gt_RPn_freq_mhz.attr,
433 	&dev_attr_vlv_rpe_freq_mhz.attr,
434 	NULL,
435 };
436 
437 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
438 
439 static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
440 				struct bin_attribute *attr, char *buf,
441 				loff_t off, size_t count)
442 {
443 
444 	struct device *kdev = kobj_to_dev(kobj);
445 	struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
446 	struct i915_gpu_coredump *gpu;
447 	ssize_t ret;
448 
449 	gpu = i915_first_error_state(i915);
450 	if (IS_ERR(gpu)) {
451 		ret = PTR_ERR(gpu);
452 	} else if (gpu) {
453 		ret = i915_gpu_coredump_copy_to_buffer(gpu, buf, off, count);
454 		i915_gpu_coredump_put(gpu);
455 	} else {
456 		const char *str = "No error state collected\n";
457 		size_t len = strlen(str);
458 
459 		ret = min_t(size_t, count, len - off);
460 		memcpy(buf, str + off, ret);
461 	}
462 
463 	return ret;
464 }
465 
466 static ssize_t error_state_write(struct file *file, struct kobject *kobj,
467 				 struct bin_attribute *attr, char *buf,
468 				 loff_t off, size_t count)
469 {
470 	struct device *kdev = kobj_to_dev(kobj);
471 	struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
472 
473 	drm_dbg(&dev_priv->drm, "Resetting error state\n");
474 	i915_reset_error_state(dev_priv);
475 
476 	return count;
477 }
478 
479 static const struct bin_attribute error_state_attr = {
480 	.attr.name = "error",
481 	.attr.mode = S_IRUSR | S_IWUSR,
482 	.size = 0,
483 	.read = error_state_read,
484 	.write = error_state_write,
485 };
486 
487 static void i915_setup_error_capture(struct device *kdev)
488 {
489 	if (sysfs_create_bin_file(&kdev->kobj, &error_state_attr))
490 		DRM_ERROR("error_state sysfs setup failed\n");
491 }
492 
493 static void i915_teardown_error_capture(struct device *kdev)
494 {
495 	sysfs_remove_bin_file(&kdev->kobj, &error_state_attr);
496 }
497 #else
498 static void i915_setup_error_capture(struct device *kdev) {}
499 static void i915_teardown_error_capture(struct device *kdev) {}
500 #endif
501 
502 void i915_setup_sysfs(struct drm_i915_private *dev_priv)
503 {
504 	struct device *kdev = dev_priv->drm.primary->kdev;
505 	int ret;
506 
507 #ifdef CONFIG_PM
508 	if (HAS_RC6(dev_priv)) {
509 		ret = sysfs_merge_group(&kdev->kobj,
510 					&rc6_attr_group);
511 		if (ret)
512 			drm_err(&dev_priv->drm,
513 				"RC6 residency sysfs setup failed\n");
514 	}
515 	if (HAS_RC6p(dev_priv)) {
516 		ret = sysfs_merge_group(&kdev->kobj,
517 					&rc6p_attr_group);
518 		if (ret)
519 			drm_err(&dev_priv->drm,
520 				"RC6p residency sysfs setup failed\n");
521 	}
522 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
523 		ret = sysfs_merge_group(&kdev->kobj,
524 					&media_rc6_attr_group);
525 		if (ret)
526 			drm_err(&dev_priv->drm,
527 				"Media RC6 residency sysfs setup failed\n");
528 	}
529 #endif
530 	if (HAS_L3_DPF(dev_priv)) {
531 		ret = device_create_bin_file(kdev, &dpf_attrs);
532 		if (ret)
533 			drm_err(&dev_priv->drm,
534 				"l3 parity sysfs setup failed\n");
535 
536 		if (NUM_L3_SLICES(dev_priv) > 1) {
537 			ret = device_create_bin_file(kdev,
538 						     &dpf_attrs_1);
539 			if (ret)
540 				drm_err(&dev_priv->drm,
541 					"l3 parity slice 1 setup failed\n");
542 		}
543 	}
544 
545 	ret = 0;
546 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
547 		ret = sysfs_create_files(&kdev->kobj, vlv_attrs);
548 	else if (GRAPHICS_VER(dev_priv) >= 6)
549 		ret = sysfs_create_files(&kdev->kobj, gen6_attrs);
550 	if (ret)
551 		drm_err(&dev_priv->drm, "RPS sysfs setup failed\n");
552 
553 	i915_setup_error_capture(kdev);
554 
555 	intel_engines_add_sysfs(dev_priv);
556 }
557 
558 void i915_teardown_sysfs(struct drm_i915_private *dev_priv)
559 {
560 	struct device *kdev = dev_priv->drm.primary->kdev;
561 
562 	i915_teardown_error_capture(kdev);
563 
564 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565 		sysfs_remove_files(&kdev->kobj, vlv_attrs);
566 	else
567 		sysfs_remove_files(&kdev->kobj, gen6_attrs);
568 	device_remove_bin_file(kdev,  &dpf_attrs_1);
569 	device_remove_bin_file(kdev,  &dpf_attrs);
570 #ifdef CONFIG_PM
571 	sysfs_unmerge_group(&kdev->kobj, &rc6_attr_group);
572 	sysfs_unmerge_group(&kdev->kobj, &rc6p_attr_group);
573 #endif
574 }
575