--- /dev/null
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <drm/drm_managed.h>
+
+#include "xe_device.h"
+#include "xe_gt.h"
+#include "xe_gt_idle_sysfs.h"
+#include "xe_gt_sysfs.h"
+#include "xe_guc_pc.h"
+
+/**
+ * DOC: Xe GT Idle
+ *
+ * Provides sysfs entries for idle properties of GT
+ *
+ * device/gt#/gtidle/name - name of the state
+ * device/gt#/gtidle/idle_residency_ms - Provides residency of the idle state in ms
+ * device/gt#/gtidle/idle_status - Provides current idle state
+ */
+
+static struct xe_gt_idle *dev_to_gtidle(struct device *dev)
+{
+       struct kobject *kobj = &dev->kobj;
+
+       return &kobj_to_gt(kobj->parent)->gtidle;
+}
+
+static struct xe_gt *gtidle_to_gt(struct xe_gt_idle *gtidle)
+{
+       return container_of(gtidle, struct xe_gt, gtidle);
+}
+
+static struct xe_guc_pc *gtidle_to_pc(struct xe_gt_idle *gtidle)
+{
+       return >idle_to_gt(gtidle)->uc.guc.pc;
+}
+
+static const char *gt_idle_state_to_string(enum xe_gt_idle_state state)
+{
+       switch (state) {
+       case GT_IDLE_C0:
+               return "gt-c0";
+       case GT_IDLE_C6:
+               return "gt-c6";
+       default:
+               return "unknown";
+       }
+}
+
+static u64 get_residency_ms(struct xe_gt_idle *gtidle, u64 cur_residency)
+{
+       u64 delta, overflow_residency, prev_residency;
+
+       overflow_residency = BIT_ULL(32);
+
+       /*
+        * Counter wrap handling
+        * Store previous hw counter values for counter wrap-around handling
+        * Relying on sufficient frequency of queries otherwise counters can still wrap.
+        */
+       prev_residency = gtidle->prev_residency;
+       gtidle->prev_residency = cur_residency;
+
+       /* delta */
+       if (cur_residency >= prev_residency)
+               delta = cur_residency - prev_residency;
+       else
+               delta = cur_residency + (overflow_residency - prev_residency);
+
+       /* Add delta to extended raw driver copy of idle residency */
+       cur_residency = gtidle->cur_residency + delta;
+       gtidle->cur_residency = cur_residency;
+
+       /* residency multiplier in ns, convert to ms */
+       cur_residency = mul_u64_u32_div(cur_residency, gtidle->residency_multiplier, 1e6);
+
+       return cur_residency;
+}
+
+static ssize_t name_show(struct device *dev,
+                        struct device_attribute *attr, char *buff)
+{
+       struct xe_gt_idle *gtidle = dev_to_gtidle(dev);
+
+       return sysfs_emit(buff, gtidle->name);
+}
+static DEVICE_ATTR_RO(name);
+
+static ssize_t idle_status_show(struct device *dev,
+                               struct device_attribute *attr, char *buff)
+{
+       struct xe_gt_idle *gtidle = dev_to_gtidle(dev);
+       struct xe_guc_pc *pc = gtidle_to_pc(gtidle);
+       enum xe_gt_idle_state state;
+
+       state = gtidle->idle_status(pc);
+
+       return sysfs_emit(buff, "%s\n", gt_idle_state_to_string(state));
+}
+static DEVICE_ATTR_RO(idle_status);
+
+static ssize_t idle_residency_ms_show(struct device *dev,
+                                     struct device_attribute *attr, char *buff)
+{
+       struct xe_gt_idle *gtidle = dev_to_gtidle(dev);
+       struct xe_guc_pc *pc = gtidle_to_pc(gtidle);
+       u64 residency;
+
+       residency = gtidle->idle_residency(pc);
+       return sysfs_emit(buff, "%llu\n", get_residency_ms(gtidle, residency));
+}
+static DEVICE_ATTR_RO(idle_residency_ms);
+
+static const struct attribute *gt_idle_attrs[] = {
+       &dev_attr_name.attr,
+       &dev_attr_idle_status.attr,
+       &dev_attr_idle_residency_ms.attr,
+       NULL,
+};
+
+static void gt_idle_sysfs_fini(struct drm_device *drm, void *arg)
+{
+       struct kobject *kobj = arg;
+
+       sysfs_remove_files(kobj, gt_idle_attrs);
+       kobject_put(kobj);
+}
+
+void xe_gt_idle_sysfs_init(struct xe_gt_idle *gtidle)
+{
+       struct xe_gt *gt = gtidle_to_gt(gtidle);
+       struct xe_device *xe = gt_to_xe(gt);
+       struct kobject *kobj;
+       int err;
+
+       kobj = kobject_create_and_add("gtidle", gt->sysfs);
+       if (!kobj) {
+               drm_warn(&xe->drm, "%s failed, err: %d\n", __func__, -ENOMEM);
+               return;
+       }
+
+       sprintf(gtidle->name, "gt%d-rc\n", gt->info.id);
+       /* Multiplier for  RC6 Residency counter in units of 1.28us */
+       gtidle->residency_multiplier = 1280;
+       gtidle->idle_residency = xe_guc_pc_rc6_residency;
+       gtidle->idle_status = xe_guc_pc_rc_status;
+
+       err = sysfs_create_files(kobj, gt_idle_attrs);
+       if (err) {
+               kobject_put(kobj);
+               drm_warn(&xe->drm, "failed to register gtidle sysfs, err: %d\n", err);
+               return;
+       }
+
+       err = drmm_add_action_or_reset(&xe->drm, gt_idle_sysfs_fini, kobj);
+       if (err)
+               drm_warn(&xe->drm, "%s: drmm_add_action_or_reset failed, err: %d\n",
+                        __func__, err);
+}
 
  *
  * Render-C states is also a GuC PC feature that is now enabled in Xe for
  * all platforms.
- * Xe's GuC PC provides a sysfs API for Render-C States:
  *
- * device/gt#/rc* *read-only* files:
- * - rc_status: Provide the actual immediate status of Render-C: (rc0 or rc6)
- * - rc6_residency: Provide the rc6_residency counter in units of 1.28 uSec.
- *                  Prone to overflows.
  */
 
 static struct xe_guc *
 }
 static DEVICE_ATTR_RW(freq_max);
 
-static ssize_t rc_status_show(struct device *dev,
-                             struct device_attribute *attr, char *buff)
+/**
+ * xe_guc_pc_rc_status - get the current Render C state
+ * @pc: XE_GuC_PC instance
+ */
+enum xe_gt_idle_state xe_guc_pc_rc_status(struct xe_guc_pc *pc)
 {
-       struct xe_guc_pc *pc = dev_to_pc(dev);
        struct xe_gt *gt = pc_to_gt(pc);
        u32 reg;
 
 
        switch (REG_FIELD_GET(RCN_MASK, reg)) {
        case GT_RC6:
-               return sysfs_emit(buff, "rc6\n");
+               return GT_IDLE_C6;
        case GT_RC0:
-               return sysfs_emit(buff, "rc0\n");
+               return GT_IDLE_C0;
        default:
-               return -ENOENT;
+               return GT_IDLE_UNKNOWN;
        }
 }
-static DEVICE_ATTR_RO(rc_status);
 
-static ssize_t rc6_residency_show(struct device *dev,
-                                 struct device_attribute *attr, char *buff)
+/**
+ * xe_guc_pc_rc6_residency - rc6 residency counter
+ * @pc: Xe_GuC_PC instance
+ */
+u64 xe_guc_pc_rc6_residency(struct xe_guc_pc *pc)
 {
-       struct xe_guc_pc *pc = dev_to_pc(dev);
        struct xe_gt *gt = pc_to_gt(pc);
        u32 reg;
-       ssize_t ret;
-
-       xe_device_mem_access_get(pc_to_xe(pc));
-       ret = xe_force_wake_get(gt_to_fw(gt), XE_FORCEWAKE_ALL);
-       if (ret)
-               goto out;
 
+       xe_device_mem_access_get(gt_to_xe(gt));
        reg = xe_mmio_read32(gt, GT_GFX_RC6);
-       ret = sysfs_emit(buff, "%u\n", reg);
+       xe_device_mem_access_put(gt_to_xe(gt));
 
-       XE_WARN_ON(xe_force_wake_put(gt_to_fw(gt), XE_FORCEWAKE_ALL));
-out:
-       xe_device_mem_access_put(pc_to_xe(pc));
-       return ret;
+       return reg;
 }
-static DEVICE_ATTR_RO(rc6_residency);
 
 static const struct attribute *pc_attrs[] = {
        &dev_attr_freq_act.attr,
        &dev_attr_freq_rpn.attr,
        &dev_attr_freq_min.attr,
        &dev_attr_freq_max.attr,
-       &dev_attr_rc_status.attr,
-       &dev_attr_rc6_residency.attr,
        NULL
 };