1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2023 Intel Corporation 4 */ 5 6 #include <drm/drm_managed.h> 7 8 #include "xe_force_wake.h" 9 #include "xe_device.h" 10 #include "xe_gt.h" 11 #include "xe_gt_idle.h" 12 #include "xe_gt_sysfs.h" 13 #include "xe_guc_pc.h" 14 #include "regs/xe_gt_regs.h" 15 #include "xe_macros.h" 16 #include "xe_mmio.h" 17 #include "xe_pm.h" 18 19 /** 20 * DOC: Xe GT Idle 21 * 22 * Contains functions that init GT idle features like C6 23 * 24 * device/gt#/gtidle/name - name of the state 25 * device/gt#/gtidle/idle_residency_ms - Provides residency of the idle state in ms 26 * device/gt#/gtidle/idle_status - Provides current idle state 27 */ 28 29 static struct xe_gt_idle *dev_to_gtidle(struct device *dev) 30 { 31 struct kobject *kobj = &dev->kobj; 32 33 return &kobj_to_gt(kobj->parent)->gtidle; 34 } 35 36 static struct xe_gt *gtidle_to_gt(struct xe_gt_idle *gtidle) 37 { 38 return container_of(gtidle, struct xe_gt, gtidle); 39 } 40 41 static struct xe_guc_pc *gtidle_to_pc(struct xe_gt_idle *gtidle) 42 { 43 return >idle_to_gt(gtidle)->uc.guc.pc; 44 } 45 46 static struct xe_device * 47 pc_to_xe(struct xe_guc_pc *pc) 48 { 49 struct xe_guc *guc = container_of(pc, struct xe_guc, pc); 50 struct xe_gt *gt = container_of(guc, struct xe_gt, uc.guc); 51 52 return gt_to_xe(gt); 53 } 54 55 static const char *gt_idle_state_to_string(enum xe_gt_idle_state state) 56 { 57 switch (state) { 58 case GT_IDLE_C0: 59 return "gt-c0"; 60 case GT_IDLE_C6: 61 return "gt-c6"; 62 default: 63 return "unknown"; 64 } 65 } 66 67 static u64 get_residency_ms(struct xe_gt_idle *gtidle, u64 cur_residency) 68 { 69 u64 delta, overflow_residency, prev_residency; 70 71 overflow_residency = BIT_ULL(32); 72 73 /* 74 * Counter wrap handling 75 * Store previous hw counter values for counter wrap-around handling 76 * Relying on sufficient frequency of queries otherwise counters can still wrap. 77 */ 78 prev_residency = gtidle->prev_residency; 79 gtidle->prev_residency = cur_residency; 80 81 /* delta */ 82 if (cur_residency >= prev_residency) 83 delta = cur_residency - prev_residency; 84 else 85 delta = cur_residency + (overflow_residency - prev_residency); 86 87 /* Add delta to extended raw driver copy of idle residency */ 88 cur_residency = gtidle->cur_residency + delta; 89 gtidle->cur_residency = cur_residency; 90 91 /* residency multiplier in ns, convert to ms */ 92 cur_residency = mul_u64_u32_div(cur_residency, gtidle->residency_multiplier, 1e6); 93 94 return cur_residency; 95 } 96 97 void xe_gt_idle_enable_pg(struct xe_gt *gt) 98 { 99 struct xe_device *xe = gt_to_xe(gt); 100 u32 pg_enable; 101 int i, j; 102 103 /* Disable CPG for PVC */ 104 if (xe->info.platform == XE_PVC) 105 return; 106 107 xe_device_assert_mem_access(gt_to_xe(gt)); 108 109 pg_enable = RENDER_POWERGATE_ENABLE | MEDIA_POWERGATE_ENABLE; 110 111 for (i = XE_HW_ENGINE_VCS0, j = 0; i <= XE_HW_ENGINE_VCS7; ++i, ++j) { 112 if ((gt->info.engine_mask & BIT(i))) 113 pg_enable |= (VDN_HCP_POWERGATE_ENABLE(j) | 114 VDN_MFXVDENC_POWERGATE_ENABLE(j)); 115 } 116 117 XE_WARN_ON(xe_force_wake_get(gt_to_fw(gt), XE_FW_GT)); 118 if (xe->info.skip_guc_pc) { 119 /* 120 * GuC sets the hysteresis value when GuC PC is enabled 121 * else set it to 25 (25 * 1.28us) 122 */ 123 xe_mmio_write32(gt, MEDIA_POWERGATE_IDLE_HYSTERESIS, 25); 124 xe_mmio_write32(gt, RENDER_POWERGATE_IDLE_HYSTERESIS, 25); 125 } 126 127 xe_mmio_write32(gt, POWERGATE_ENABLE, pg_enable); 128 XE_WARN_ON(xe_force_wake_put(gt_to_fw(gt), XE_FW_GT)); 129 } 130 131 void xe_gt_idle_disable_pg(struct xe_gt *gt) 132 { 133 xe_device_assert_mem_access(gt_to_xe(gt)); 134 XE_WARN_ON(xe_force_wake_get(gt_to_fw(gt), XE_FW_GT)); 135 136 xe_mmio_write32(gt, POWERGATE_ENABLE, 0); 137 138 XE_WARN_ON(xe_force_wake_put(gt_to_fw(gt), XE_FW_GT)); 139 } 140 141 static ssize_t name_show(struct device *dev, 142 struct device_attribute *attr, char *buff) 143 { 144 struct xe_gt_idle *gtidle = dev_to_gtidle(dev); 145 struct xe_guc_pc *pc = gtidle_to_pc(gtidle); 146 ssize_t ret; 147 148 xe_pm_runtime_get(pc_to_xe(pc)); 149 ret = sysfs_emit(buff, "%s\n", gtidle->name); 150 xe_pm_runtime_put(pc_to_xe(pc)); 151 152 return ret; 153 } 154 static DEVICE_ATTR_RO(name); 155 156 static ssize_t idle_status_show(struct device *dev, 157 struct device_attribute *attr, char *buff) 158 { 159 struct xe_gt_idle *gtidle = dev_to_gtidle(dev); 160 struct xe_guc_pc *pc = gtidle_to_pc(gtidle); 161 enum xe_gt_idle_state state; 162 163 xe_pm_runtime_get(pc_to_xe(pc)); 164 state = gtidle->idle_status(pc); 165 xe_pm_runtime_put(pc_to_xe(pc)); 166 167 return sysfs_emit(buff, "%s\n", gt_idle_state_to_string(state)); 168 } 169 static DEVICE_ATTR_RO(idle_status); 170 171 static ssize_t idle_residency_ms_show(struct device *dev, 172 struct device_attribute *attr, char *buff) 173 { 174 struct xe_gt_idle *gtidle = dev_to_gtidle(dev); 175 struct xe_guc_pc *pc = gtidle_to_pc(gtidle); 176 u64 residency; 177 178 xe_pm_runtime_get(pc_to_xe(pc)); 179 residency = gtidle->idle_residency(pc); 180 xe_pm_runtime_put(pc_to_xe(pc)); 181 182 return sysfs_emit(buff, "%llu\n", get_residency_ms(gtidle, residency)); 183 } 184 static DEVICE_ATTR_RO(idle_residency_ms); 185 186 static const struct attribute *gt_idle_attrs[] = { 187 &dev_attr_name.attr, 188 &dev_attr_idle_status.attr, 189 &dev_attr_idle_residency_ms.attr, 190 NULL, 191 }; 192 193 static void gt_idle_fini(void *arg) 194 { 195 struct kobject *kobj = arg; 196 struct xe_gt *gt = kobj_to_gt(kobj->parent); 197 198 xe_gt_idle_disable_pg(gt); 199 200 if (gt_to_xe(gt)->info.skip_guc_pc) { 201 XE_WARN_ON(xe_force_wake_get(gt_to_fw(gt), XE_FW_GT)); 202 xe_gt_idle_disable_c6(gt); 203 xe_force_wake_put(gt_to_fw(gt), XE_FW_GT); 204 } 205 206 sysfs_remove_files(kobj, gt_idle_attrs); 207 kobject_put(kobj); 208 } 209 210 int xe_gt_idle_init(struct xe_gt_idle *gtidle) 211 { 212 struct xe_gt *gt = gtidle_to_gt(gtidle); 213 struct xe_device *xe = gt_to_xe(gt); 214 struct kobject *kobj; 215 int err; 216 217 kobj = kobject_create_and_add("gtidle", gt->sysfs); 218 if (!kobj) 219 return -ENOMEM; 220 221 if (xe_gt_is_media_type(gt)) { 222 snprintf(gtidle->name, sizeof(gtidle->name), "gt%d-mc", gt->info.id); 223 gtidle->idle_residency = xe_guc_pc_mc6_residency; 224 } else { 225 snprintf(gtidle->name, sizeof(gtidle->name), "gt%d-rc", gt->info.id); 226 gtidle->idle_residency = xe_guc_pc_rc6_residency; 227 } 228 229 /* Multiplier for Residency counter in units of 1.28us */ 230 gtidle->residency_multiplier = 1280; 231 gtidle->idle_status = xe_guc_pc_c_status; 232 233 err = sysfs_create_files(kobj, gt_idle_attrs); 234 if (err) { 235 kobject_put(kobj); 236 return err; 237 } 238 239 xe_gt_idle_enable_pg(gt); 240 241 return devm_add_action_or_reset(xe->drm.dev, gt_idle_fini, kobj); 242 } 243 244 void xe_gt_idle_enable_c6(struct xe_gt *gt) 245 { 246 xe_device_assert_mem_access(gt_to_xe(gt)); 247 xe_force_wake_assert_held(gt_to_fw(gt), XE_FW_GT); 248 249 /* Units of 1280 ns for a total of 5s */ 250 xe_mmio_write32(gt, RC_IDLE_HYSTERSIS, 0x3B9ACA); 251 /* Enable RC6 */ 252 xe_mmio_write32(gt, RC_CONTROL, 253 RC_CTL_HW_ENABLE | RC_CTL_TO_MODE | RC_CTL_RC6_ENABLE); 254 } 255 256 void xe_gt_idle_disable_c6(struct xe_gt *gt) 257 { 258 xe_device_assert_mem_access(gt_to_xe(gt)); 259 xe_force_wake_assert_held(gt_to_fw(gt), XE_FW_GT); 260 261 xe_mmio_write32(gt, RC_CONTROL, 0); 262 xe_mmio_write32(gt, RC_STATE, 0); 263 } 264