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
28 #include <linux/device.h>
29 #include <linux/module.h>
30 #include <linux/stat.h>
31 #include <linux/sysfs.h>
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"
39 #include "i915_sysfs.h"
41 struct drm_i915_private *kdev_minor_to_i915(struct device *kdev)
43 struct drm_minor *minor = dev_get_drvdata(kdev);
44 return to_i915(minor->dev);
47 static int l3_access_valid(struct drm_i915_private *i915, loff_t offset)
49 if (!HAS_L3_DPF(i915))
52 if (!IS_ALIGNED(offset, sizeof(u32)))
55 if (offset >= GEN7_L3LOG_SIZE)
62 i915_l3_read(struct file *filp, struct kobject *kobj,
63 struct bin_attribute *attr, char *buf,
64 loff_t offset, size_t count)
66 struct device *kdev = kobj_to_dev(kobj);
67 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
68 int slice = (int)(uintptr_t)attr->private;
71 ret = l3_access_valid(i915, offset);
75 count = round_down(count, sizeof(u32));
76 count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
77 memset(buf, 0, count);
79 spin_lock(&i915->gem.contexts.lock);
80 if (i915->l3_parity.remap_info[slice])
82 i915->l3_parity.remap_info[slice] + offset / sizeof(u32),
84 spin_unlock(&i915->gem.contexts.lock);
90 i915_l3_write(struct file *filp, struct kobject *kobj,
91 struct bin_attribute *attr, char *buf,
92 loff_t offset, size_t count)
94 struct device *kdev = kobj_to_dev(kobj);
95 struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
96 int slice = (int)(uintptr_t)attr->private;
97 u32 *remap_info, *freeme = NULL;
98 struct i915_gem_context *ctx;
101 ret = l3_access_valid(i915, offset);
105 if (count < sizeof(u32))
108 remap_info = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
112 spin_lock(&i915->gem.contexts.lock);
114 if (i915->l3_parity.remap_info[slice]) {
116 remap_info = i915->l3_parity.remap_info[slice];
118 i915->l3_parity.remap_info[slice] = remap_info;
121 count = round_down(count, sizeof(u32));
122 memcpy(remap_info + offset / sizeof(u32), buf, count);
124 /* NB: We defer the remapping until we switch to the context */
125 list_for_each_entry(ctx, &i915->gem.contexts.list, link)
126 ctx->remap_slice |= BIT(slice);
128 spin_unlock(&i915->gem.contexts.lock);
132 * TODO: Ideally we really want a GPU reset here to make sure errors
133 * aren't propagated. Since I cannot find a stable way to reset the GPU
134 * at this point it is left as a TODO.
140 static const struct bin_attribute dpf_attrs = {
141 .attr = {.name = "l3_parity", .mode = (S_IRUSR | S_IWUSR)},
142 .size = GEN7_L3LOG_SIZE,
143 .read = i915_l3_read,
144 .write = i915_l3_write,
149 static const struct bin_attribute dpf_attrs_1 = {
150 .attr = {.name = "l3_parity_slice_1", .mode = (S_IRUSR | S_IWUSR)},
151 .size = GEN7_L3LOG_SIZE,
152 .read = i915_l3_read,
153 .write = i915_l3_write,
158 void i915_setup_sysfs(struct drm_i915_private *dev_priv)
160 struct device *kdev = dev_priv->drm.primary->kdev;
163 if (HAS_L3_DPF(dev_priv)) {
164 ret = device_create_bin_file(kdev, &dpf_attrs);
166 drm_err(&dev_priv->drm,
167 "l3 parity sysfs setup failed\n");
169 if (NUM_L3_SLICES(dev_priv) > 1) {
170 ret = device_create_bin_file(kdev,
173 drm_err(&dev_priv->drm,
174 "l3 parity slice 1 setup failed\n");
178 dev_priv->sysfs_gt = kobject_create_and_add("gt", &kdev->kobj);
179 if (!dev_priv->sysfs_gt)
180 drm_warn(&dev_priv->drm,
181 "failed to register GT sysfs directory\n");
183 i915_gpu_error_sysfs_setup(dev_priv);
185 intel_engines_add_sysfs(dev_priv);
188 void i915_teardown_sysfs(struct drm_i915_private *dev_priv)
190 struct device *kdev = dev_priv->drm.primary->kdev;
192 i915_gpu_error_sysfs_teardown(dev_priv);
194 device_remove_bin_file(kdev, &dpf_attrs_1);
195 device_remove_bin_file(kdev, &dpf_attrs);
197 kobject_put(dev_priv->sysfs_gt);