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>
3256c5098fSChris Wilson
330d6419e9SMatt Roper #include "gt/intel_gt_regs.h"
34c1132367SAndi Shyti #include "gt/intel_rc6.h"
353e7abf81SAndi Shyti #include "gt/intel_rps.h"
364ec76dbeSChris Wilson #include "gt/sysfs_engines.h"
37c1132367SAndi Shyti
380136db58SBen Widawsky #include "i915_drv.h"
39be68261dSJani Nikula #include "i915_sysfs.h"
400136db58SBen Widawsky
kdev_minor_to_i915(struct device * kdev)41b770bcfaSAndi Shyti struct drm_i915_private *kdev_minor_to_i915(struct device *kdev)
42c49d13eeSDavid Weinehall {
43694c2828SDavid Weinehall struct drm_minor *minor = dev_get_drvdata(kdev);
44694c2828SDavid Weinehall return to_i915(minor->dev);
45c49d13eeSDavid Weinehall }
4614c8d110SDave Airlie
l3_access_valid(struct drm_i915_private * i915,loff_t offset)47261ea7e2SChris Wilson static int l3_access_valid(struct drm_i915_private *i915, loff_t offset)
4884bc7581SBen Widawsky {
49261ea7e2SChris Wilson if (!HAS_L3_DPF(i915))
5084bc7581SBen Widawsky return -EPERM;
5184bc7581SBen Widawsky
52261ea7e2SChris Wilson if (!IS_ALIGNED(offset, sizeof(u32)))
5384bc7581SBen Widawsky return -EINVAL;
5484bc7581SBen Widawsky
5584bc7581SBen Widawsky if (offset >= GEN7_L3LOG_SIZE)
5684bc7581SBen Widawsky return -ENXIO;
5784bc7581SBen Widawsky
5884bc7581SBen Widawsky return 0;
5984bc7581SBen Widawsky }
6084bc7581SBen Widawsky
6184bc7581SBen Widawsky static ssize_t
i915_l3_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t offset,size_t count)6284bc7581SBen Widawsky i915_l3_read(struct file *filp, struct kobject *kobj,
6384bc7581SBen Widawsky struct bin_attribute *attr, char *buf,
6484bc7581SBen Widawsky loff_t offset, size_t count)
6584bc7581SBen Widawsky {
66c49d13eeSDavid Weinehall struct device *kdev = kobj_to_dev(kobj);
67261ea7e2SChris Wilson struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
6835a85ac6SBen Widawsky int slice = (int)(uintptr_t)attr->private;
693ccfd19dSBen Widawsky int ret;
7084bc7581SBen Widawsky
71261ea7e2SChris Wilson ret = l3_access_valid(i915, offset);
7284bc7581SBen Widawsky if (ret)
7384bc7581SBen Widawsky return ret;
7484bc7581SBen Widawsky
75261ea7e2SChris Wilson count = round_down(count, sizeof(u32));
76e5ad4026SDan Carpenter count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
771c966dd2SBen Widawsky memset(buf, 0, count);
781c966dd2SBen Widawsky
79a4e7ccdaSChris Wilson spin_lock(&i915->gem.contexts.lock);
80261ea7e2SChris Wilson if (i915->l3_parity.remap_info[slice])
81261ea7e2SChris Wilson memcpy(buf,
82261ea7e2SChris Wilson i915->l3_parity.remap_info[slice] + offset / sizeof(u32),
83261ea7e2SChris Wilson count);
84a4e7ccdaSChris Wilson spin_unlock(&i915->gem.contexts.lock);
8584bc7581SBen Widawsky
861c966dd2SBen Widawsky return count;
8784bc7581SBen Widawsky }
8884bc7581SBen Widawsky
8984bc7581SBen Widawsky static ssize_t
i915_l3_write(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t offset,size_t count)9084bc7581SBen Widawsky i915_l3_write(struct file *filp, struct kobject *kobj,
9184bc7581SBen Widawsky struct bin_attribute *attr, char *buf,
9284bc7581SBen Widawsky loff_t offset, size_t count)
9384bc7581SBen Widawsky {
94c49d13eeSDavid Weinehall struct device *kdev = kobj_to_dev(kobj);
95261ea7e2SChris Wilson struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
9635a85ac6SBen Widawsky int slice = (int)(uintptr_t)attr->private;
97a4e7ccdaSChris Wilson u32 *remap_info, *freeme = NULL;
98261ea7e2SChris Wilson struct i915_gem_context *ctx;
9984bc7581SBen Widawsky int ret;
10084bc7581SBen Widawsky
101261ea7e2SChris Wilson ret = l3_access_valid(i915, offset);
10284bc7581SBen Widawsky if (ret)
10384bc7581SBen Widawsky return ret;
10484bc7581SBen Widawsky
105261ea7e2SChris Wilson if (count < sizeof(u32))
106261ea7e2SChris Wilson return -EINVAL;
107261ea7e2SChris Wilson
108a4e7ccdaSChris Wilson remap_info = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
109a4e7ccdaSChris Wilson if (!remap_info)
110a4e7ccdaSChris Wilson return -ENOMEM;
11184bc7581SBen Widawsky
112a4e7ccdaSChris Wilson spin_lock(&i915->gem.contexts.lock);
113a4e7ccdaSChris Wilson
114a4e7ccdaSChris Wilson if (i915->l3_parity.remap_info[slice]) {
115a4e7ccdaSChris Wilson freeme = remap_info;
116a4e7ccdaSChris Wilson remap_info = i915->l3_parity.remap_info[slice];
117a4e7ccdaSChris Wilson } else {
118a4e7ccdaSChris Wilson i915->l3_parity.remap_info[slice] = remap_info;
11984bc7581SBen Widawsky }
12084bc7581SBen Widawsky
121261ea7e2SChris Wilson count = round_down(count, sizeof(u32));
122a4e7ccdaSChris Wilson memcpy(remap_info + offset / sizeof(u32), buf, count);
123261ea7e2SChris Wilson
124261ea7e2SChris Wilson /* NB: We defer the remapping until we switch to the context */
125a4e7ccdaSChris Wilson list_for_each_entry(ctx, &i915->gem.contexts.list, link)
126261ea7e2SChris Wilson ctx->remap_slice |= BIT(slice);
127261ea7e2SChris Wilson
128a4e7ccdaSChris Wilson spin_unlock(&i915->gem.contexts.lock);
129a4e7ccdaSChris Wilson kfree(freeme);
130a4e7ccdaSChris Wilson
131261ea7e2SChris Wilson /*
132261ea7e2SChris Wilson * TODO: Ideally we really want a GPU reset here to make sure errors
13384bc7581SBen Widawsky * aren't propagated. Since I cannot find a stable way to reset the GPU
13484bc7581SBen Widawsky * at this point it is left as a TODO.
13584bc7581SBen Widawsky */
13684bc7581SBen Widawsky
137a4e7ccdaSChris Wilson return count;
13884bc7581SBen Widawsky }
13984bc7581SBen Widawsky
14059f3da1eSBhumika Goyal static const struct bin_attribute dpf_attrs = {
14184bc7581SBen Widawsky .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
14284bc7581SBen Widawsky .size = GEN7_L3LOG_SIZE,
14384bc7581SBen Widawsky .read = i915_l3_read,
14484bc7581SBen Widawsky .write = i915_l3_write,
14535a85ac6SBen Widawsky .mmap = NULL,
14635a85ac6SBen Widawsky .private = (void *)0
14735a85ac6SBen Widawsky };
14835a85ac6SBen Widawsky
14959f3da1eSBhumika Goyal static const struct bin_attribute dpf_attrs_1 = {
15035a85ac6SBen Widawsky .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
15135a85ac6SBen Widawsky .size = GEN7_L3LOG_SIZE,
15235a85ac6SBen Widawsky .read = i915_l3_read,
15335a85ac6SBen Widawsky .write = i915_l3_write,
15435a85ac6SBen Widawsky .mmap = NULL,
15535a85ac6SBen Widawsky .private = (void *)1
15684bc7581SBen Widawsky };
15784bc7581SBen Widawsky
15898a2f411SChris Wilson #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
15998a2f411SChris Wilson
error_state_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)160ef86ddceSMika Kuoppala static ssize_t error_state_read(struct file *filp, struct kobject *kobj,
161ef86ddceSMika Kuoppala struct bin_attribute *attr, char *buf,
162ef86ddceSMika Kuoppala loff_t off, size_t count)
163ef86ddceSMika Kuoppala {
164ef86ddceSMika Kuoppala
165657fb5fbSGeliang Tang struct device *kdev = kobj_to_dev(kobj);
1660e39037bSChris Wilson struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
167742379c0SChris Wilson struct i915_gpu_coredump *gpu;
1683304033aSAlan Previn ssize_t ret = 0;
1693304033aSAlan Previn
1703304033aSAlan Previn /*
1713304033aSAlan Previn * FIXME: Concurrent clients triggering resets and reading + clearing
1723304033aSAlan Previn * dumps can cause inconsistent sysfs reads when a user calls in with a
1733304033aSAlan Previn * non-zero offset to complete a prior partial read but the
1743304033aSAlan Previn * gpu_coredump has been cleared or replaced.
1753304033aSAlan Previn */
176ef86ddceSMika Kuoppala
1770e39037bSChris Wilson gpu = i915_first_error_state(i915);
178e6154e4cSChris Wilson if (IS_ERR(gpu)) {
179e6154e4cSChris Wilson ret = PTR_ERR(gpu);
180e6154e4cSChris Wilson } else if (gpu) {
181742379c0SChris Wilson ret = i915_gpu_coredump_copy_to_buffer(gpu, buf, off, count);
182742379c0SChris Wilson i915_gpu_coredump_put(gpu);
1830e39037bSChris Wilson } else {
1840e39037bSChris Wilson const char *str = "No error state collected\n";
1850e39037bSChris Wilson size_t len = strlen(str);
1860e39037bSChris Wilson
1873304033aSAlan Previn if (off < len) {
1880e39037bSChris Wilson ret = min_t(size_t, count, len - off);
1890e39037bSChris Wilson memcpy(buf, str + off, ret);
1900e39037bSChris Wilson }
1913304033aSAlan Previn }
192ef86ddceSMika Kuoppala
1935a4c6f1bSChris Wilson return ret;
194ef86ddceSMika Kuoppala }
195ef86ddceSMika Kuoppala
error_state_write(struct file * file,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)196ef86ddceSMika Kuoppala static ssize_t error_state_write(struct file *file, struct kobject *kobj,
197ef86ddceSMika Kuoppala struct bin_attribute *attr, char *buf,
198ef86ddceSMika Kuoppala loff_t off, size_t count)
199ef86ddceSMika Kuoppala {
200657fb5fbSGeliang Tang struct device *kdev = kobj_to_dev(kobj);
201694c2828SDavid Weinehall struct drm_i915_private *dev_priv = kdev_minor_to_i915(kdev);
202ef86ddceSMika Kuoppala
20300376ccfSWambui Karuga drm_dbg(&dev_priv->drm, "Resetting error state\n");
2045a4c6f1bSChris Wilson i915_reset_error_state(dev_priv);
205ef86ddceSMika Kuoppala
206ef86ddceSMika Kuoppala return count;
207ef86ddceSMika Kuoppala }
208ef86ddceSMika Kuoppala
20959f3da1eSBhumika Goyal static const struct bin_attribute error_state_attr = {
210ef86ddceSMika Kuoppala .attr.name = "error",
211ef86ddceSMika Kuoppala .attr.mode = S_IRUSR | S_IWUSR,
212ef86ddceSMika Kuoppala .size = 0,
213ef86ddceSMika Kuoppala .read = error_state_read,
214ef86ddceSMika Kuoppala .write = error_state_write,
215ef86ddceSMika Kuoppala };
216ef86ddceSMika Kuoppala
i915_setup_error_capture(struct device * kdev)21798a2f411SChris Wilson static void i915_setup_error_capture(struct device *kdev)
21898a2f411SChris Wilson {
21998a2f411SChris Wilson if (sysfs_create_bin_file(&kdev->kobj, &error_state_attr))
220*a10234fdSTvrtko Ursulin drm_err(&kdev_minor_to_i915(kdev)->drm,
221*a10234fdSTvrtko Ursulin "error_state sysfs setup failed\n");
22298a2f411SChris Wilson }
22398a2f411SChris Wilson
i915_teardown_error_capture(struct device * kdev)22498a2f411SChris Wilson static void i915_teardown_error_capture(struct device *kdev)
22598a2f411SChris Wilson {
22698a2f411SChris Wilson sysfs_remove_bin_file(&kdev->kobj, &error_state_attr);
22798a2f411SChris Wilson }
22898a2f411SChris Wilson #else
i915_setup_error_capture(struct device * kdev)22998a2f411SChris Wilson static void i915_setup_error_capture(struct device *kdev) {}
i915_teardown_error_capture(struct device * kdev)23098a2f411SChris Wilson static void i915_teardown_error_capture(struct device *kdev) {}
23198a2f411SChris Wilson #endif
23298a2f411SChris Wilson
i915_setup_sysfs(struct drm_i915_private * dev_priv)233694c2828SDavid Weinehall void i915_setup_sysfs(struct drm_i915_private *dev_priv)
2340136db58SBen Widawsky {
235694c2828SDavid Weinehall struct device *kdev = dev_priv->drm.primary->kdev;
2360136db58SBen Widawsky int ret;
2370136db58SBen Widawsky
238694c2828SDavid Weinehall if (HAS_L3_DPF(dev_priv)) {
239694c2828SDavid Weinehall ret = device_create_bin_file(kdev, &dpf_attrs);
24084bc7581SBen Widawsky if (ret)
24100376ccfSWambui Karuga drm_err(&dev_priv->drm,
24200376ccfSWambui Karuga "l3 parity sysfs setup failed\n");
24335a85ac6SBen Widawsky
244694c2828SDavid Weinehall if (NUM_L3_SLICES(dev_priv) > 1) {
245694c2828SDavid Weinehall ret = device_create_bin_file(kdev,
24635a85ac6SBen Widawsky &dpf_attrs_1);
24735a85ac6SBen Widawsky if (ret)
24800376ccfSWambui Karuga drm_err(&dev_priv->drm,
24900376ccfSWambui Karuga "l3 parity slice 1 setup failed\n");
25035a85ac6SBen Widawsky }
2510136db58SBen Widawsky }
252df6eedc8SBen Widawsky
253b770bcfaSAndi Shyti dev_priv->sysfs_gt = kobject_create_and_add("gt", &kdev->kobj);
254b770bcfaSAndi Shyti if (!dev_priv->sysfs_gt)
255b770bcfaSAndi Shyti drm_warn(&dev_priv->drm,
256b770bcfaSAndi Shyti "failed to register GT sysfs directory\n");
257b770bcfaSAndi Shyti
25898a2f411SChris Wilson i915_setup_error_capture(kdev);
2594ec76dbeSChris Wilson
2604ec76dbeSChris Wilson intel_engines_add_sysfs(dev_priv);
261112abd29SDaniel Vetter }
2620136db58SBen Widawsky
i915_teardown_sysfs(struct drm_i915_private * dev_priv)263694c2828SDavid Weinehall void i915_teardown_sysfs(struct drm_i915_private *dev_priv)
2640136db58SBen Widawsky {
265694c2828SDavid Weinehall struct device *kdev = dev_priv->drm.primary->kdev;
266694c2828SDavid Weinehall
26798a2f411SChris Wilson i915_teardown_error_capture(kdev);
26898a2f411SChris Wilson
269694c2828SDavid Weinehall device_remove_bin_file(kdev, &dpf_attrs_1);
270694c2828SDavid Weinehall device_remove_bin_file(kdev, &dpf_attrs);
27169d6bf5cSAshutosh Dixit
27269d6bf5cSAshutosh Dixit kobject_put(dev_priv->sysfs_gt);
2730136db58SBen Widawsky }
274