1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2023 Intel Corporation 4 */ 5 6 #include "xe_gt_freq.h" 7 8 #include <linux/kobject.h> 9 #include <linux/sysfs.h> 10 11 #include <drm/drm_managed.h> 12 #include <drm/drm_print.h> 13 14 #include "xe_device_types.h" 15 #include "xe_gt_sysfs.h" 16 #include "xe_gt_throttle_sysfs.h" 17 #include "xe_guc_pc.h" 18 19 /** 20 * DOC: Xe GT Frequency Management 21 * 22 * This component is responsible for the raw GT frequency management, including 23 * the sysfs API. 24 * 25 * Underneath, Xe enables GuC SLPC automated frequency management. GuC is then 26 * allowed to request PCODE any frequency between the Minimum and the Maximum 27 * selected by this component. Furthermore, it is important to highlight that 28 * PCODE is the ultimate decision maker of the actual running frequency, based 29 * on thermal and other running conditions. 30 * 31 * Xe's Freq provides a sysfs API for frequency management: 32 * 33 * device/tile#/gt#/freq0/<item>_freq *read-only* files: 34 * - act_freq: The actual resolved frequency decided by PCODE. 35 * - cur_freq: The current one requested by GuC PC to the PCODE. 36 * - rpn_freq: The Render Performance (RP) N level, which is the minimal one. 37 * - rpe_freq: The Render Performance (RP) E level, which is the efficient one. 38 * - rp0_freq: The Render Performance (RP) 0 level, which is the maximum one. 39 * 40 * device/tile#/gt#/freq0/<item>_freq *read-write* files: 41 * - min_freq: Min frequency request. 42 * - max_freq: Max frequency request. 43 * If max <= min, then freq_min becomes a fixed frequency request. 44 */ 45 46 static struct xe_guc_pc * 47 dev_to_pc(struct device *dev) 48 { 49 return &kobj_to_gt(dev->kobj.parent)->uc.guc.pc; 50 } 51 52 static ssize_t act_freq_show(struct device *dev, 53 struct device_attribute *attr, char *buf) 54 { 55 struct xe_guc_pc *pc = dev_to_pc(dev); 56 57 return sysfs_emit(buf, "%d\n", xe_guc_pc_get_act_freq(pc)); 58 } 59 static DEVICE_ATTR_RO(act_freq); 60 61 static ssize_t cur_freq_show(struct device *dev, 62 struct device_attribute *attr, char *buf) 63 { 64 struct xe_guc_pc *pc = dev_to_pc(dev); 65 u32 freq; 66 ssize_t ret; 67 68 ret = xe_guc_pc_get_cur_freq(pc, &freq); 69 if (ret) 70 return ret; 71 72 return sysfs_emit(buf, "%d\n", freq); 73 } 74 static DEVICE_ATTR_RO(cur_freq); 75 76 static ssize_t rp0_freq_show(struct device *dev, 77 struct device_attribute *attr, char *buf) 78 { 79 struct xe_guc_pc *pc = dev_to_pc(dev); 80 81 return sysfs_emit(buf, "%d\n", xe_guc_pc_get_rp0_freq(pc)); 82 } 83 static DEVICE_ATTR_RO(rp0_freq); 84 85 static ssize_t rpe_freq_show(struct device *dev, 86 struct device_attribute *attr, char *buf) 87 { 88 struct xe_guc_pc *pc = dev_to_pc(dev); 89 90 return sysfs_emit(buf, "%d\n", xe_guc_pc_get_rpe_freq(pc)); 91 } 92 static DEVICE_ATTR_RO(rpe_freq); 93 94 static ssize_t rpn_freq_show(struct device *dev, 95 struct device_attribute *attr, char *buf) 96 { 97 struct xe_guc_pc *pc = dev_to_pc(dev); 98 99 return sysfs_emit(buf, "%d\n", xe_guc_pc_get_rpn_freq(pc)); 100 } 101 static DEVICE_ATTR_RO(rpn_freq); 102 103 static ssize_t min_freq_show(struct device *dev, 104 struct device_attribute *attr, char *buf) 105 { 106 struct xe_guc_pc *pc = dev_to_pc(dev); 107 u32 freq; 108 ssize_t ret; 109 110 ret = xe_guc_pc_get_min_freq(pc, &freq); 111 if (ret) 112 return ret; 113 114 return sysfs_emit(buf, "%d\n", freq); 115 } 116 117 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr, 118 const char *buff, size_t count) 119 { 120 struct xe_guc_pc *pc = dev_to_pc(dev); 121 u32 freq; 122 ssize_t ret; 123 124 ret = kstrtou32(buff, 0, &freq); 125 if (ret) 126 return ret; 127 128 ret = xe_guc_pc_set_min_freq(pc, freq); 129 if (ret) 130 return ret; 131 132 return count; 133 } 134 static DEVICE_ATTR_RW(min_freq); 135 136 static ssize_t max_freq_show(struct device *dev, 137 struct device_attribute *attr, char *buf) 138 { 139 struct xe_guc_pc *pc = dev_to_pc(dev); 140 u32 freq; 141 ssize_t ret; 142 143 ret = xe_guc_pc_get_max_freq(pc, &freq); 144 if (ret) 145 return ret; 146 147 return sysfs_emit(buf, "%d\n", freq); 148 } 149 150 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr, 151 const char *buff, size_t count) 152 { 153 struct xe_guc_pc *pc = dev_to_pc(dev); 154 u32 freq; 155 ssize_t ret; 156 157 ret = kstrtou32(buff, 0, &freq); 158 if (ret) 159 return ret; 160 161 ret = xe_guc_pc_set_max_freq(pc, freq); 162 if (ret) 163 return ret; 164 165 return count; 166 } 167 static DEVICE_ATTR_RW(max_freq); 168 169 static const struct attribute *freq_attrs[] = { 170 &dev_attr_act_freq.attr, 171 &dev_attr_cur_freq.attr, 172 &dev_attr_rp0_freq.attr, 173 &dev_attr_rpe_freq.attr, 174 &dev_attr_rpn_freq.attr, 175 &dev_attr_min_freq.attr, 176 &dev_attr_max_freq.attr, 177 NULL 178 }; 179 180 static void freq_fini(struct drm_device *drm, void *arg) 181 { 182 struct kobject *kobj = arg; 183 184 sysfs_remove_files(kobj, freq_attrs); 185 kobject_put(kobj); 186 } 187 188 /** 189 * xe_gt_freq_init - Initialize Xe Freq component 190 * @gt: Xe GT object 191 * 192 * It needs to be initialized after GT Sysfs and GuC PC components are ready. 193 */ 194 void xe_gt_freq_init(struct xe_gt *gt) 195 { 196 struct xe_device *xe = gt_to_xe(gt); 197 int err; 198 199 if (xe->info.skip_guc_pc) 200 return; 201 202 gt->freq = kobject_create_and_add("freq0", gt->sysfs); 203 if (!gt->freq) { 204 drm_warn(&xe->drm, "failed to add freq0 directory to %s\n", 205 kobject_name(gt->sysfs)); 206 return; 207 } 208 209 err = drmm_add_action_or_reset(&xe->drm, freq_fini, gt->freq); 210 if (err) { 211 drm_warn(&xe->drm, "%s: drmm_add_action_or_reset failed, err: %d\n", 212 __func__, err); 213 return; 214 } 215 216 err = sysfs_create_files(gt->freq, freq_attrs); 217 if (err) 218 drm_warn(&xe->drm, "failed to add freq attrs to %s, err: %d\n", 219 kobject_name(gt->freq), err); 220 221 xe_gt_throttle_sysfs_init(gt); 222 } 223