1 // SPDX-License-Identifier: MIT
3 * Copyright © 2023 Intel Corporation
6 #include <drm/drm_managed.h>
8 #include "regs/xe_gt_regs.h"
11 #include "xe_gt_ccs_mode.h"
12 #include "xe_gt_printk.h"
13 #include "xe_gt_sysfs.h"
17 static void __xe_gt_apply_ccs_mode(struct xe_gt *gt, u32 num_engines)
19 u32 mode = CCS_MODE_CSLICE_0_3_MASK; /* disable all by default */
20 int num_slices = hweight32(CCS_MASK(gt));
21 struct xe_device *xe = gt_to_xe(gt);
22 int width, cslice = 0;
25 xe_assert(xe, xe_gt_ccs_mode_enabled(gt));
27 xe_assert(xe, num_engines && num_engines <= num_slices);
28 xe_assert(xe, !(num_slices % num_engines));
31 * Loop over all available slices and assign each a user engine.
32 * For example, if there are four compute slices available, the
33 * assignment of compute slices to compute engines would be,
35 * With 1 engine (ccs0):
36 * slice 0, 1, 2, 3: ccs0
38 * With 2 engines (ccs0, ccs1):
42 * With 4 engines (ccs0, ccs1, ccs2, ccs3):
48 for (width = num_slices / num_engines; width; width--) {
49 struct xe_hw_engine *hwe;
50 enum xe_hw_engine_id id;
52 for_each_hw_engine(hwe, gt, id) {
53 if (hwe->class != XE_ENGINE_CLASS_COMPUTE)
56 if (hwe->logical_instance >= num_engines)
59 config |= BIT(hwe->instance) << XE_HW_ENGINE_CCS0;
61 /* If a slice is fused off, leave disabled */
62 while ((CCS_MASK(gt) & BIT(cslice)) == 0)
65 mode &= ~CCS_MODE_CSLICE(cslice, CCS_MODE_CSLICE_MASK);
66 mode |= CCS_MODE_CSLICE(cslice, hwe->instance);
72 * Mask bits need to be set for the register. Though only Xe2+
73 * platforms require setting of mask bits, it won't harm for older
74 * platforms as these bits are unused there.
76 mode |= CCS_MODE_CSLICE_0_3_MASK << 16;
77 xe_mmio_write32(>->mmio, CCS_MODE, mode);
79 xe_gt_dbg(gt, "CCS_MODE=%x config:%08x, num_engines:%d, num_slices:%d\n",
80 mode, config, num_engines, num_slices);
83 void xe_gt_apply_ccs_mode(struct xe_gt *gt)
85 if (!gt->ccs_mode || IS_SRIOV_VF(gt_to_xe(gt)))
88 __xe_gt_apply_ccs_mode(gt, gt->ccs_mode);
92 num_cslices_show(struct device *kdev,
93 struct device_attribute *attr, char *buf)
95 struct xe_gt *gt = kobj_to_gt(&kdev->kobj);
97 return sysfs_emit(buf, "%u\n", hweight32(CCS_MASK(gt)));
100 static DEVICE_ATTR_RO(num_cslices);
103 ccs_mode_show(struct device *kdev,
104 struct device_attribute *attr, char *buf)
106 struct xe_gt *gt = kobj_to_gt(&kdev->kobj);
108 return sysfs_emit(buf, "%u\n", gt->ccs_mode);
112 ccs_mode_store(struct device *kdev, struct device_attribute *attr,
113 const char *buff, size_t count)
115 struct xe_gt *gt = kobj_to_gt(&kdev->kobj);
116 struct xe_device *xe = gt_to_xe(gt);
117 u32 num_engines, num_slices;
121 xe_gt_dbg(gt, "Can't change compute mode when running as %s\n",
122 xe_sriov_mode_to_string(xe_device_sriov_mode(xe)));
126 ret = kstrtou32(buff, 0, &num_engines);
131 * Ensure number of engines specified is valid and there is an
132 * exact multiple of engines for slices.
134 num_slices = hweight32(CCS_MASK(gt));
135 if (!num_engines || num_engines > num_slices || num_slices % num_engines) {
136 xe_gt_dbg(gt, "Invalid compute config, %d engines %d slices\n",
137 num_engines, num_slices);
141 /* CCS mode can only be updated when there are no drm clients */
142 mutex_lock(&xe->drm.filelist_mutex);
143 if (!list_empty(&xe->drm.filelist)) {
144 mutex_unlock(&xe->drm.filelist_mutex);
145 xe_gt_dbg(gt, "Rejecting compute mode change as there are active drm clients\n");
149 if (gt->ccs_mode != num_engines) {
150 xe_gt_info(gt, "Setting compute mode to %d\n", num_engines);
151 gt->ccs_mode = num_engines;
152 xe_gt_record_user_engines(gt);
153 xe_gt_reset_async(gt);
156 mutex_unlock(&xe->drm.filelist_mutex);
161 static DEVICE_ATTR_RW(ccs_mode);
163 static const struct attribute *gt_ccs_mode_attrs[] = {
164 &dev_attr_ccs_mode.attr,
165 &dev_attr_num_cslices.attr,
169 static void xe_gt_ccs_mode_sysfs_fini(void *arg)
171 struct xe_gt *gt = arg;
173 sysfs_remove_files(gt->sysfs, gt_ccs_mode_attrs);
177 * xe_gt_ccs_mode_sysfs_init - Initialize CCS mode sysfs interfaces
180 * Through a per-gt 'ccs_mode' sysfs interface, the user can enable a fixed
181 * number of compute hardware engines to which the available compute slices
182 * are to be allocated. This user configuration change triggers a gt reset
183 * and it is expected that there are no open drm clients while doing so.
184 * The number of available compute slices is exposed to user through a per-gt
185 * 'num_cslices' sysfs interface.
187 * Returns: Returns error value for failure and 0 for success.
189 int xe_gt_ccs_mode_sysfs_init(struct xe_gt *gt)
191 struct xe_device *xe = gt_to_xe(gt);
194 if (!xe_gt_ccs_mode_enabled(gt))
197 err = sysfs_create_files(gt->sysfs, gt_ccs_mode_attrs);
201 return devm_add_action_or_reset(xe->drm.dev, xe_gt_ccs_mode_sysfs_fini, gt);