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