i915_sysfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. #include <linux/device.h>
  28. #include <linux/module.h>
  29. #include <linux/stat.h>
  30. #include <linux/sysfs.h>
  31. #include "intel_drv.h"
  32. #include "i915_drv.h"
  33. #define dev_to_drm_minor(d) dev_get_drvdata((d))
  34. #ifdef CONFIG_PM
  35. static u32 calc_residency(struct drm_device *dev,
  36. i915_reg_t reg)
  37. {
  38. struct drm_i915_private *dev_priv = dev->dev_private;
  39. u64 raw_time; /* 32b value may overflow during fixed point math */
  40. u64 units = 128ULL, div = 100000ULL;
  41. u32 ret;
  42. if (!intel_enable_rc6(dev))
  43. return 0;
  44. intel_runtime_pm_get(dev_priv);
  45. /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
  46. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  47. units = 1;
  48. div = dev_priv->czclk_freq;
  49. if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
  50. units <<= 8;
  51. } else if (IS_BROXTON(dev)) {
  52. units = 1;
  53. div = 1200; /* 833.33ns */
  54. }
  55. raw_time = I915_READ(reg) * units;
  56. ret = DIV_ROUND_UP_ULL(raw_time, div);
  57. intel_runtime_pm_put(dev_priv);
  58. return ret;
  59. }
  60. static ssize_t
  61. show_rc6_mask(struct device *kdev, struct device_attribute *attr, char *buf)
  62. {
  63. struct drm_minor *dminor = dev_to_drm_minor(kdev);
  64. return snprintf(buf, PAGE_SIZE, "%x\n", intel_enable_rc6(dminor->dev));
  65. }
  66. static ssize_t
  67. show_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
  68. {
  69. struct drm_minor *dminor = dev_get_drvdata(kdev);
  70. u32 rc6_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6);
  71. return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
  72. }
  73. static ssize_t
  74. show_rc6p_ms(struct device *kdev, struct device_attribute *attr, char *buf)
  75. {
  76. struct drm_minor *dminor = dev_to_drm_minor(kdev);
  77. u32 rc6p_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6p);
  78. return snprintf(buf, PAGE_SIZE, "%u\n", rc6p_residency);
  79. }
  80. static ssize_t
  81. show_rc6pp_ms(struct device *kdev, struct device_attribute *attr, char *buf)
  82. {
  83. struct drm_minor *dminor = dev_to_drm_minor(kdev);
  84. u32 rc6pp_residency = calc_residency(dminor->dev, GEN6_GT_GFX_RC6pp);
  85. return snprintf(buf, PAGE_SIZE, "%u\n", rc6pp_residency);
  86. }
  87. static ssize_t
  88. show_media_rc6_ms(struct device *kdev, struct device_attribute *attr, char *buf)
  89. {
  90. struct drm_minor *dminor = dev_get_drvdata(kdev);
  91. u32 rc6_residency = calc_residency(dminor->dev, VLV_GT_MEDIA_RC6);
  92. return snprintf(buf, PAGE_SIZE, "%u\n", rc6_residency);
  93. }
  94. static DEVICE_ATTR(rc6_enable, S_IRUGO, show_rc6_mask, NULL);
  95. static DEVICE_ATTR(rc6_residency_ms, S_IRUGO, show_rc6_ms, NULL);
  96. static DEVICE_ATTR(rc6p_residency_ms, S_IRUGO, show_rc6p_ms, NULL);
  97. static DEVICE_ATTR(rc6pp_residency_ms, S_IRUGO, show_rc6pp_ms, NULL);
  98. static DEVICE_ATTR(media_rc6_residency_ms, S_IRUGO, show_media_rc6_ms, NULL);
  99. static struct attribute *rc6_attrs[] = {
  100. &dev_attr_rc6_enable.attr,
  101. &dev_attr_rc6_residency_ms.attr,
  102. NULL
  103. };
  104. static struct attribute_group rc6_attr_group = {
  105. .name = power_group_name,
  106. .attrs = rc6_attrs
  107. };
  108. static struct attribute *rc6p_attrs[] = {
  109. &dev_attr_rc6p_residency_ms.attr,
  110. &dev_attr_rc6pp_residency_ms.attr,
  111. NULL
  112. };
  113. static struct attribute_group rc6p_attr_group = {
  114. .name = power_group_name,
  115. .attrs = rc6p_attrs
  116. };
  117. static struct attribute *media_rc6_attrs[] = {
  118. &dev_attr_media_rc6_residency_ms.attr,
  119. NULL
  120. };
  121. static struct attribute_group media_rc6_attr_group = {
  122. .name = power_group_name,
  123. .attrs = media_rc6_attrs
  124. };
  125. #endif
  126. static int l3_access_valid(struct drm_device *dev, loff_t offset)
  127. {
  128. if (!HAS_L3_DPF(dev))
  129. return -EPERM;
  130. if (offset % 4 != 0)
  131. return -EINVAL;
  132. if (offset >= GEN7_L3LOG_SIZE)
  133. return -ENXIO;
  134. return 0;
  135. }
  136. static ssize_t
  137. i915_l3_read(struct file *filp, struct kobject *kobj,
  138. struct bin_attribute *attr, char *buf,
  139. loff_t offset, size_t count)
  140. {
  141. struct device *dev = kobj_to_dev(kobj);
  142. struct drm_minor *dminor = dev_to_drm_minor(dev);
  143. struct drm_device *drm_dev = dminor->dev;
  144. struct drm_i915_private *dev_priv = drm_dev->dev_private;
  145. int slice = (int)(uintptr_t)attr->private;
  146. int ret;
  147. count = round_down(count, 4);
  148. ret = l3_access_valid(drm_dev, offset);
  149. if (ret)
  150. return ret;
  151. count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
  152. ret = i915_mutex_lock_interruptible(drm_dev);
  153. if (ret)
  154. return ret;
  155. if (dev_priv->l3_parity.remap_info[slice])
  156. memcpy(buf,
  157. dev_priv->l3_parity.remap_info[slice] + (offset/4),
  158. count);
  159. else
  160. memset(buf, 0, count);
  161. mutex_unlock(&drm_dev->struct_mutex);
  162. return count;
  163. }
  164. static ssize_t
  165. i915_l3_write(struct file *filp, struct kobject *kobj,
  166. struct bin_attribute *attr, char *buf,
  167. loff_t offset, size_t count)
  168. {
  169. struct device *dev = kobj_to_dev(kobj);
  170. struct drm_minor *dminor = dev_to_drm_minor(dev);
  171. struct drm_device *drm_dev = dminor->dev;
  172. struct drm_i915_private *dev_priv = drm_dev->dev_private;
  173. struct intel_context *ctx;
  174. u32 *temp = NULL; /* Just here to make handling failures easy */
  175. int slice = (int)(uintptr_t)attr->private;
  176. int ret;
  177. if (!HAS_HW_CONTEXTS(drm_dev))
  178. return -ENXIO;
  179. ret = l3_access_valid(drm_dev, offset);
  180. if (ret)
  181. return ret;
  182. ret = i915_mutex_lock_interruptible(drm_dev);
  183. if (ret)
  184. return ret;
  185. if (!dev_priv->l3_parity.remap_info[slice]) {
  186. temp = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
  187. if (!temp) {
  188. mutex_unlock(&drm_dev->struct_mutex);
  189. return -ENOMEM;
  190. }
  191. }
  192. ret = i915_gpu_idle(drm_dev);
  193. if (ret) {
  194. kfree(temp);
  195. mutex_unlock(&drm_dev->struct_mutex);
  196. return ret;
  197. }
  198. /* TODO: Ideally we really want a GPU reset here to make sure errors
  199. * aren't propagated. Since I cannot find a stable way to reset the GPU
  200. * at this point it is left as a TODO.
  201. */
  202. if (temp)
  203. dev_priv->l3_parity.remap_info[slice] = temp;
  204. memcpy(dev_priv->l3_parity.remap_info[slice] + (offset/4), buf, count);
  205. /* NB: We defer the remapping until we switch to the context */
  206. list_for_each_entry(ctx, &dev_priv->context_list, link)
  207. ctx->remap_slice |= (1<<slice);
  208. mutex_unlock(&drm_dev->struct_mutex);
  209. return count;
  210. }
  211. static struct bin_attribute dpf_attrs = {
  212. .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
  213. .size = GEN7_L3LOG_SIZE,
  214. .read = i915_l3_read,
  215. .write = i915_l3_write,
  216. .mmap = NULL,
  217. .private = (void *)0
  218. };
  219. static struct bin_attribute dpf_attrs_1 = {
  220. .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
  221. .size = GEN7_L3LOG_SIZE,
  222. .read = i915_l3_read,
  223. .write = i915_l3_write,
  224. .mmap = NULL,
  225. .private = (void *)1
  226. };
  227. static ssize_t gt_act_freq_mhz_show(struct device *kdev,
  228. struct device_attribute *attr, char *buf)
  229. {
  230. struct drm_minor *minor = dev_to_drm_minor(kdev);
  231. struct drm_device *dev = minor->dev;
  232. struct drm_i915_private *dev_priv = dev->dev_private;
  233. int ret;
  234. flush_delayed_work(&dev_priv->rps.delayed_resume_work);
  235. intel_runtime_pm_get(dev_priv);
  236. mutex_lock(&dev_priv->rps.hw_lock);
  237. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  238. u32 freq;
  239. freq = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  240. ret = intel_gpu_freq(dev_priv, (freq >> 8) & 0xff);
  241. } else {
  242. u32 rpstat = I915_READ(GEN6_RPSTAT1);
  243. if (IS_GEN9(dev_priv))
  244. ret = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
  245. else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  246. ret = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
  247. else
  248. ret = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
  249. ret = intel_gpu_freq(dev_priv, ret);
  250. }
  251. mutex_unlock(&dev_priv->rps.hw_lock);
  252. intel_runtime_pm_put(dev_priv);
  253. return snprintf(buf, PAGE_SIZE, "%d\n", ret);
  254. }
  255. static ssize_t gt_cur_freq_mhz_show(struct device *kdev,
  256. struct device_attribute *attr, char *buf)
  257. {
  258. struct drm_minor *minor = dev_to_drm_minor(kdev);
  259. struct drm_device *dev = minor->dev;
  260. struct drm_i915_private *dev_priv = dev->dev_private;
  261. int ret;
  262. flush_delayed_work(&dev_priv->rps.delayed_resume_work);
  263. intel_runtime_pm_get(dev_priv);
  264. mutex_lock(&dev_priv->rps.hw_lock);
  265. ret = intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq);
  266. mutex_unlock(&dev_priv->rps.hw_lock);
  267. intel_runtime_pm_put(dev_priv);
  268. return snprintf(buf, PAGE_SIZE, "%d\n", ret);
  269. }
  270. static ssize_t vlv_rpe_freq_mhz_show(struct device *kdev,
  271. struct device_attribute *attr, char *buf)
  272. {
  273. struct drm_minor *minor = dev_to_drm_minor(kdev);
  274. struct drm_device *dev = minor->dev;
  275. struct drm_i915_private *dev_priv = dev->dev_private;
  276. return snprintf(buf, PAGE_SIZE,
  277. "%d\n",
  278. intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq));
  279. }
  280. static ssize_t gt_max_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
  281. {
  282. struct drm_minor *minor = dev_to_drm_minor(kdev);
  283. struct drm_device *dev = minor->dev;
  284. struct drm_i915_private *dev_priv = dev->dev_private;
  285. int ret;
  286. flush_delayed_work(&dev_priv->rps.delayed_resume_work);
  287. mutex_lock(&dev_priv->rps.hw_lock);
  288. ret = intel_gpu_freq(dev_priv, dev_priv->rps.max_freq_softlimit);
  289. mutex_unlock(&dev_priv->rps.hw_lock);
  290. return snprintf(buf, PAGE_SIZE, "%d\n", ret);
  291. }
  292. static ssize_t gt_max_freq_mhz_store(struct device *kdev,
  293. struct device_attribute *attr,
  294. const char *buf, size_t count)
  295. {
  296. struct drm_minor *minor = dev_to_drm_minor(kdev);
  297. struct drm_device *dev = minor->dev;
  298. struct drm_i915_private *dev_priv = dev->dev_private;
  299. u32 val;
  300. ssize_t ret;
  301. ret = kstrtou32(buf, 0, &val);
  302. if (ret)
  303. return ret;
  304. flush_delayed_work(&dev_priv->rps.delayed_resume_work);
  305. intel_runtime_pm_get(dev_priv);
  306. mutex_lock(&dev_priv->rps.hw_lock);
  307. val = intel_freq_opcode(dev_priv, val);
  308. if (val < dev_priv->rps.min_freq ||
  309. val > dev_priv->rps.max_freq ||
  310. val < dev_priv->rps.min_freq_softlimit) {
  311. mutex_unlock(&dev_priv->rps.hw_lock);
  312. intel_runtime_pm_put(dev_priv);
  313. return -EINVAL;
  314. }
  315. if (val > dev_priv->rps.rp0_freq)
  316. DRM_DEBUG("User requested overclocking to %d\n",
  317. intel_gpu_freq(dev_priv, val));
  318. dev_priv->rps.max_freq_softlimit = val;
  319. val = clamp_t(int, dev_priv->rps.cur_freq,
  320. dev_priv->rps.min_freq_softlimit,
  321. dev_priv->rps.max_freq_softlimit);
  322. /* We still need *_set_rps to process the new max_delay and
  323. * update the interrupt limits and PMINTRMSK even though
  324. * frequency request may be unchanged. */
  325. intel_set_rps(dev, val);
  326. mutex_unlock(&dev_priv->rps.hw_lock);
  327. intel_runtime_pm_put(dev_priv);
  328. return count;
  329. }
  330. static ssize_t gt_min_freq_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
  331. {
  332. struct drm_minor *minor = dev_to_drm_minor(kdev);
  333. struct drm_device *dev = minor->dev;
  334. struct drm_i915_private *dev_priv = dev->dev_private;
  335. int ret;
  336. flush_delayed_work(&dev_priv->rps.delayed_resume_work);
  337. mutex_lock(&dev_priv->rps.hw_lock);
  338. ret = intel_gpu_freq(dev_priv, dev_priv->rps.min_freq_softlimit);
  339. mutex_unlock(&dev_priv->rps.hw_lock);
  340. return snprintf(buf, PAGE_SIZE, "%d\n", ret);
  341. }
  342. static ssize_t gt_min_freq_mhz_store(struct device *kdev,
  343. struct device_attribute *attr,
  344. const char *buf, size_t count)
  345. {
  346. struct drm_minor *minor = dev_to_drm_minor(kdev);
  347. struct drm_device *dev = minor->dev;
  348. struct drm_i915_private *dev_priv = dev->dev_private;
  349. u32 val;
  350. ssize_t ret;
  351. ret = kstrtou32(buf, 0, &val);
  352. if (ret)
  353. return ret;
  354. flush_delayed_work(&dev_priv->rps.delayed_resume_work);
  355. intel_runtime_pm_get(dev_priv);
  356. mutex_lock(&dev_priv->rps.hw_lock);
  357. val = intel_freq_opcode(dev_priv, val);
  358. if (val < dev_priv->rps.min_freq ||
  359. val > dev_priv->rps.max_freq ||
  360. val > dev_priv->rps.max_freq_softlimit) {
  361. mutex_unlock(&dev_priv->rps.hw_lock);
  362. intel_runtime_pm_put(dev_priv);
  363. return -EINVAL;
  364. }
  365. dev_priv->rps.min_freq_softlimit = val;
  366. val = clamp_t(int, dev_priv->rps.cur_freq,
  367. dev_priv->rps.min_freq_softlimit,
  368. dev_priv->rps.max_freq_softlimit);
  369. /* We still need *_set_rps to process the new min_delay and
  370. * update the interrupt limits and PMINTRMSK even though
  371. * frequency request may be unchanged. */
  372. intel_set_rps(dev, val);
  373. mutex_unlock(&dev_priv->rps.hw_lock);
  374. intel_runtime_pm_put(dev_priv);
  375. return count;
  376. }
  377. static DEVICE_ATTR(gt_act_freq_mhz, S_IRUGO, gt_act_freq_mhz_show, NULL);
  378. static DEVICE_ATTR(gt_cur_freq_mhz, S_IRUGO, gt_cur_freq_mhz_show, NULL);
  379. static DEVICE_ATTR(gt_max_freq_mhz, S_IRUGO | S_IWUSR, gt_max_freq_mhz_show, gt_max_freq_mhz_store);
  380. static DEVICE_ATTR(gt_min_freq_mhz, S_IRUGO | S_IWUSR, gt_min_freq_mhz_show, gt_min_freq_mhz_store);
  381. static DEVICE_ATTR(vlv_rpe_freq_mhz, S_IRUGO, vlv_rpe_freq_mhz_show, NULL);
  382. static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf);
  383. static DEVICE_ATTR(gt_RP0_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
  384. static DEVICE_ATTR(gt_RP1_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
  385. static DEVICE_ATTR(gt_RPn_freq_mhz, S_IRUGO, gt_rp_mhz_show, NULL);
  386. /* For now we have a static number of RP states */
  387. static ssize_t gt_rp_mhz_show(struct device *kdev, struct device_attribute *attr, char *buf)
  388. {
  389. struct drm_minor *minor = dev_to_drm_minor(kdev);
  390. struct drm_device *dev = minor->dev;
  391. struct drm_i915_private *dev_priv = dev->dev_private;
  392. u32 val;
  393. if (attr == &dev_attr_gt_RP0_freq_mhz)
  394. val = intel_gpu_freq(dev_priv, dev_priv->rps.rp0_freq);
  395. else if (attr == &dev_attr_gt_RP1_freq_mhz)
  396. val = intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq);
  397. else if (attr == &dev_attr_gt_RPn_freq_mhz)
  398. val = intel_gpu_freq(dev_priv, dev_priv->rps.min_freq);
  399. else
  400. BUG();
  401. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  402. }
  403. static const struct attribute *gen6_attrs[] = {
  404. &dev_attr_gt_act_freq_mhz.attr,
  405. &dev_attr_gt_cur_freq_mhz.attr,
  406. &dev_attr_gt_max_freq_mhz.attr,
  407. &dev_attr_gt_min_freq_mhz.attr,
  408. &dev_attr_gt_RP0_freq_mhz.attr,
  409. &dev_attr_gt_RP1_freq_mhz.attr,
  410. &dev_attr_gt_RPn_freq_mhz.attr,
  411. NULL,
  412. };
  413. static const struct attribute *vlv_attrs[] = {
  414. &dev_attr_gt_act_freq_mhz.attr,
  415. &dev_attr_gt_cur_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. static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
  425. struct bin_attribute *attr, char *buf,
  426. loff_t off, size_t count)
  427. {
  428. struct device *kdev = kobj_to_dev(kobj);
  429. struct drm_minor *minor = dev_to_drm_minor(kdev);
  430. struct drm_device *dev = minor->dev;
  431. struct i915_error_state_file_priv error_priv;
  432. struct drm_i915_error_state_buf error_str;
  433. ssize_t ret_count = 0;
  434. int ret;
  435. memset(&error_priv, 0, sizeof(error_priv));
  436. ret = i915_error_state_buf_init(&error_str, to_i915(dev), count, off);
  437. if (ret)
  438. return ret;
  439. error_priv.dev = dev;
  440. i915_error_state_get(dev, &error_priv);
  441. ret = i915_error_state_to_str(&error_str, &error_priv);
  442. if (ret)
  443. goto out;
  444. ret_count = count < error_str.bytes ? count : error_str.bytes;
  445. memcpy(buf, error_str.buf, ret_count);
  446. out:
  447. i915_error_state_put(&error_priv);
  448. i915_error_state_buf_release(&error_str);
  449. return ret ?: ret_count;
  450. }
  451. static ssize_t error_state_write(struct file *file, struct kobject *kobj,
  452. struct bin_attribute *attr, char *buf,
  453. loff_t off, size_t count)
  454. {
  455. struct device *kdev = kobj_to_dev(kobj);
  456. struct drm_minor *minor = dev_to_drm_minor(kdev);
  457. struct drm_device *dev = minor->dev;
  458. int ret;
  459. DRM_DEBUG_DRIVER("Resetting error state\n");
  460. ret = mutex_lock_interruptible(&dev->struct_mutex);
  461. if (ret)
  462. return ret;
  463. i915_destroy_error_state(dev);
  464. mutex_unlock(&dev->struct_mutex);
  465. return count;
  466. }
  467. static 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. void i915_setup_sysfs(struct drm_device *dev)
  475. {
  476. int ret;
  477. #ifdef CONFIG_PM
  478. if (HAS_RC6(dev)) {
  479. ret = sysfs_merge_group(&dev->primary->kdev->kobj,
  480. &rc6_attr_group);
  481. if (ret)
  482. DRM_ERROR("RC6 residency sysfs setup failed\n");
  483. }
  484. if (HAS_RC6p(dev)) {
  485. ret = sysfs_merge_group(&dev->primary->kdev->kobj,
  486. &rc6p_attr_group);
  487. if (ret)
  488. DRM_ERROR("RC6p residency sysfs setup failed\n");
  489. }
  490. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  491. ret = sysfs_merge_group(&dev->primary->kdev->kobj,
  492. &media_rc6_attr_group);
  493. if (ret)
  494. DRM_ERROR("Media RC6 residency sysfs setup failed\n");
  495. }
  496. #endif
  497. if (HAS_L3_DPF(dev)) {
  498. ret = device_create_bin_file(dev->primary->kdev, &dpf_attrs);
  499. if (ret)
  500. DRM_ERROR("l3 parity sysfs setup failed\n");
  501. if (NUM_L3_SLICES(dev) > 1) {
  502. ret = device_create_bin_file(dev->primary->kdev,
  503. &dpf_attrs_1);
  504. if (ret)
  505. DRM_ERROR("l3 parity slice 1 setup failed\n");
  506. }
  507. }
  508. ret = 0;
  509. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  510. ret = sysfs_create_files(&dev->primary->kdev->kobj, vlv_attrs);
  511. else if (INTEL_INFO(dev)->gen >= 6)
  512. ret = sysfs_create_files(&dev->primary->kdev->kobj, gen6_attrs);
  513. if (ret)
  514. DRM_ERROR("RPS sysfs setup failed\n");
  515. ret = sysfs_create_bin_file(&dev->primary->kdev->kobj,
  516. &error_state_attr);
  517. if (ret)
  518. DRM_ERROR("error_state sysfs setup failed\n");
  519. }
  520. void i915_teardown_sysfs(struct drm_device *dev)
  521. {
  522. sysfs_remove_bin_file(&dev->primary->kdev->kobj, &error_state_attr);
  523. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  524. sysfs_remove_files(&dev->primary->kdev->kobj, vlv_attrs);
  525. else
  526. sysfs_remove_files(&dev->primary->kdev->kobj, gen6_attrs);
  527. device_remove_bin_file(dev->primary->kdev, &dpf_attrs_1);
  528. device_remove_bin_file(dev->primary->kdev, &dpf_attrs);
  529. #ifdef CONFIG_PM
  530. sysfs_unmerge_group(&dev->primary->kdev->kobj, &rc6_attr_group);
  531. sysfs_unmerge_group(&dev->primary->kdev->kobj, &rc6p_attr_group);
  532. #endif
  533. }