18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * sysfs.c - sysfs support
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * (C) 2006-2007 Shaohua Li <shaohua.li@intel.com>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This code is licenced under the GPL.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/cpuidle.h>
118c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/cpu.h>
148c2ecf20Sopenharmony_ci#include <linux/completion.h>
158c2ecf20Sopenharmony_ci#include <linux/capability.h>
168c2ecf20Sopenharmony_ci#include <linux/device.h>
178c2ecf20Sopenharmony_ci#include <linux/kobject.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include "cpuidle.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic ssize_t show_available_governors(struct device *dev,
228c2ecf20Sopenharmony_ci					struct device_attribute *attr,
238c2ecf20Sopenharmony_ci					char *buf)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	ssize_t i = 0;
268c2ecf20Sopenharmony_ci	struct cpuidle_governor *tmp;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	mutex_lock(&cpuidle_lock);
298c2ecf20Sopenharmony_ci	list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
308c2ecf20Sopenharmony_ci		if (i >= (ssize_t) (PAGE_SIZE - (CPUIDLE_NAME_LEN + 2)))
318c2ecf20Sopenharmony_ci			goto out;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci		i += scnprintf(&buf[i], CPUIDLE_NAME_LEN + 1, "%s ", tmp->name);
348c2ecf20Sopenharmony_ci	}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciout:
378c2ecf20Sopenharmony_ci	i+= sprintf(&buf[i], "\n");
388c2ecf20Sopenharmony_ci	mutex_unlock(&cpuidle_lock);
398c2ecf20Sopenharmony_ci	return i;
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic ssize_t show_current_driver(struct device *dev,
438c2ecf20Sopenharmony_ci				   struct device_attribute *attr,
448c2ecf20Sopenharmony_ci				   char *buf)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	ssize_t ret;
478c2ecf20Sopenharmony_ci	struct cpuidle_driver *drv;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	spin_lock(&cpuidle_driver_lock);
508c2ecf20Sopenharmony_ci	drv = cpuidle_get_driver();
518c2ecf20Sopenharmony_ci	if (drv)
528c2ecf20Sopenharmony_ci		ret = sprintf(buf, "%s\n", drv->name);
538c2ecf20Sopenharmony_ci	else
548c2ecf20Sopenharmony_ci		ret = sprintf(buf, "none\n");
558c2ecf20Sopenharmony_ci	spin_unlock(&cpuidle_driver_lock);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return ret;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic ssize_t show_current_governor(struct device *dev,
618c2ecf20Sopenharmony_ci				     struct device_attribute *attr,
628c2ecf20Sopenharmony_ci				     char *buf)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	ssize_t ret;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	mutex_lock(&cpuidle_lock);
678c2ecf20Sopenharmony_ci	if (cpuidle_curr_governor)
688c2ecf20Sopenharmony_ci		ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
698c2ecf20Sopenharmony_ci	else
708c2ecf20Sopenharmony_ci		ret = sprintf(buf, "none\n");
718c2ecf20Sopenharmony_ci	mutex_unlock(&cpuidle_lock);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return ret;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic ssize_t store_current_governor(struct device *dev,
778c2ecf20Sopenharmony_ci				      struct device_attribute *attr,
788c2ecf20Sopenharmony_ci				      const char *buf, size_t count)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	char gov_name[CPUIDLE_NAME_LEN + 1];
818c2ecf20Sopenharmony_ci	int ret;
828c2ecf20Sopenharmony_ci	struct cpuidle_governor *gov;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	ret = sscanf(buf, "%" __stringify(CPUIDLE_NAME_LEN) "s", gov_name);
858c2ecf20Sopenharmony_ci	if (ret != 1)
868c2ecf20Sopenharmony_ci		return -EINVAL;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	mutex_lock(&cpuidle_lock);
898c2ecf20Sopenharmony_ci	ret = -EINVAL;
908c2ecf20Sopenharmony_ci	list_for_each_entry(gov, &cpuidle_governors, governor_list) {
918c2ecf20Sopenharmony_ci		if (!strncmp(gov->name, gov_name, CPUIDLE_NAME_LEN)) {
928c2ecf20Sopenharmony_ci			ret = cpuidle_switch_governor(gov);
938c2ecf20Sopenharmony_ci			break;
948c2ecf20Sopenharmony_ci		}
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci	mutex_unlock(&cpuidle_lock);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	return ret ? ret : count;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL);
1028c2ecf20Sopenharmony_cistatic DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL);
1038c2ecf20Sopenharmony_cistatic DEVICE_ATTR(current_governor, 0644, show_current_governor,
1048c2ecf20Sopenharmony_ci				   store_current_governor);
1058c2ecf20Sopenharmony_cistatic DEVICE_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic struct attribute *cpuidle_attrs[] = {
1088c2ecf20Sopenharmony_ci	&dev_attr_available_governors.attr,
1098c2ecf20Sopenharmony_ci	&dev_attr_current_driver.attr,
1108c2ecf20Sopenharmony_ci	&dev_attr_current_governor.attr,
1118c2ecf20Sopenharmony_ci	&dev_attr_current_governor_ro.attr,
1128c2ecf20Sopenharmony_ci	NULL
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic struct attribute_group cpuidle_attr_group = {
1168c2ecf20Sopenharmony_ci	.attrs = cpuidle_attrs,
1178c2ecf20Sopenharmony_ci	.name = "cpuidle",
1188c2ecf20Sopenharmony_ci};
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/**
1218c2ecf20Sopenharmony_ci * cpuidle_add_interface - add CPU global sysfs attributes
1228c2ecf20Sopenharmony_ci * @dev: the target device
1238c2ecf20Sopenharmony_ci */
1248c2ecf20Sopenharmony_ciint cpuidle_add_interface(struct device *dev)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	return sysfs_create_group(&dev->kobj, &cpuidle_attr_group);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/**
1308c2ecf20Sopenharmony_ci * cpuidle_remove_interface - remove CPU global sysfs attributes
1318c2ecf20Sopenharmony_ci * @dev: the target device
1328c2ecf20Sopenharmony_ci */
1338c2ecf20Sopenharmony_civoid cpuidle_remove_interface(struct device *dev)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	sysfs_remove_group(&dev->kobj, &cpuidle_attr_group);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistruct cpuidle_attr {
1398c2ecf20Sopenharmony_ci	struct attribute attr;
1408c2ecf20Sopenharmony_ci	ssize_t (*show)(struct cpuidle_device *, char *);
1418c2ecf20Sopenharmony_ci	ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistruct cpuidle_device_kobj {
1478c2ecf20Sopenharmony_ci	struct cpuidle_device *dev;
1488c2ecf20Sopenharmony_ci	struct completion kobj_unregister;
1498c2ecf20Sopenharmony_ci	struct kobject kobj;
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic inline struct cpuidle_device *to_cpuidle_device(struct kobject *kobj)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	struct cpuidle_device_kobj *kdev =
1558c2ecf20Sopenharmony_ci		container_of(kobj, struct cpuidle_device_kobj, kobj);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return kdev->dev;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic ssize_t cpuidle_show(struct kobject *kobj, struct attribute *attr,
1618c2ecf20Sopenharmony_ci			    char *buf)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	int ret = -EIO;
1648c2ecf20Sopenharmony_ci	struct cpuidle_device *dev = to_cpuidle_device(kobj);
1658c2ecf20Sopenharmony_ci	struct cpuidle_attr *cattr = attr_to_cpuidleattr(attr);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (cattr->show) {
1688c2ecf20Sopenharmony_ci		mutex_lock(&cpuidle_lock);
1698c2ecf20Sopenharmony_ci		ret = cattr->show(dev, buf);
1708c2ecf20Sopenharmony_ci		mutex_unlock(&cpuidle_lock);
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci	return ret;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic ssize_t cpuidle_store(struct kobject *kobj, struct attribute *attr,
1768c2ecf20Sopenharmony_ci			     const char *buf, size_t count)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	int ret = -EIO;
1798c2ecf20Sopenharmony_ci	struct cpuidle_device *dev = to_cpuidle_device(kobj);
1808c2ecf20Sopenharmony_ci	struct cpuidle_attr *cattr = attr_to_cpuidleattr(attr);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	if (cattr->store) {
1838c2ecf20Sopenharmony_ci		mutex_lock(&cpuidle_lock);
1848c2ecf20Sopenharmony_ci		ret = cattr->store(dev, buf, count);
1858c2ecf20Sopenharmony_ci		mutex_unlock(&cpuidle_lock);
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci	return ret;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic const struct sysfs_ops cpuidle_sysfs_ops = {
1918c2ecf20Sopenharmony_ci	.show = cpuidle_show,
1928c2ecf20Sopenharmony_ci	.store = cpuidle_store,
1938c2ecf20Sopenharmony_ci};
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic void cpuidle_sysfs_release(struct kobject *kobj)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct cpuidle_device_kobj *kdev =
1988c2ecf20Sopenharmony_ci		container_of(kobj, struct cpuidle_device_kobj, kobj);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	complete(&kdev->kobj_unregister);
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic struct kobj_type ktype_cpuidle = {
2048c2ecf20Sopenharmony_ci	.sysfs_ops = &cpuidle_sysfs_ops,
2058c2ecf20Sopenharmony_ci	.release = cpuidle_sysfs_release,
2068c2ecf20Sopenharmony_ci};
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistruct cpuidle_state_attr {
2098c2ecf20Sopenharmony_ci	struct attribute attr;
2108c2ecf20Sopenharmony_ci	ssize_t (*show)(struct cpuidle_state *, \
2118c2ecf20Sopenharmony_ci					struct cpuidle_state_usage *, char *);
2128c2ecf20Sopenharmony_ci	ssize_t (*store)(struct cpuidle_state *, \
2138c2ecf20Sopenharmony_ci			struct cpuidle_state_usage *, const char *, size_t);
2148c2ecf20Sopenharmony_ci};
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci#define define_one_state_ro(_name, show) \
2178c2ecf20Sopenharmony_cistatic struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci#define define_one_state_rw(_name, show, store) \
2208c2ecf20Sopenharmony_cistatic struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci#define define_show_state_function(_name) \
2238c2ecf20Sopenharmony_cistatic ssize_t show_state_##_name(struct cpuidle_state *state, \
2248c2ecf20Sopenharmony_ci			 struct cpuidle_state_usage *state_usage, char *buf) \
2258c2ecf20Sopenharmony_ci{ \
2268c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", state->_name);\
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci#define define_show_state_ull_function(_name) \
2308c2ecf20Sopenharmony_cistatic ssize_t show_state_##_name(struct cpuidle_state *state, \
2318c2ecf20Sopenharmony_ci				  struct cpuidle_state_usage *state_usage, \
2328c2ecf20Sopenharmony_ci				  char *buf)				\
2338c2ecf20Sopenharmony_ci{ \
2348c2ecf20Sopenharmony_ci	return sprintf(buf, "%llu\n", state_usage->_name);\
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci#define define_show_state_str_function(_name) \
2388c2ecf20Sopenharmony_cistatic ssize_t show_state_##_name(struct cpuidle_state *state, \
2398c2ecf20Sopenharmony_ci				  struct cpuidle_state_usage *state_usage, \
2408c2ecf20Sopenharmony_ci				  char *buf)				\
2418c2ecf20Sopenharmony_ci{ \
2428c2ecf20Sopenharmony_ci	if (state->_name[0] == '\0')\
2438c2ecf20Sopenharmony_ci		return sprintf(buf, "<null>\n");\
2448c2ecf20Sopenharmony_ci	return sprintf(buf, "%s\n", state->_name);\
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci#define define_show_state_time_function(_name) \
2488c2ecf20Sopenharmony_cistatic ssize_t show_state_##_name(struct cpuidle_state *state, \
2498c2ecf20Sopenharmony_ci				  struct cpuidle_state_usage *state_usage, \
2508c2ecf20Sopenharmony_ci				  char *buf) \
2518c2ecf20Sopenharmony_ci{ \
2528c2ecf20Sopenharmony_ci	return sprintf(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cidefine_show_state_time_function(exit_latency)
2568c2ecf20Sopenharmony_cidefine_show_state_time_function(target_residency)
2578c2ecf20Sopenharmony_cidefine_show_state_function(power_usage)
2588c2ecf20Sopenharmony_cidefine_show_state_ull_function(usage)
2598c2ecf20Sopenharmony_cidefine_show_state_ull_function(rejected)
2608c2ecf20Sopenharmony_cidefine_show_state_str_function(name)
2618c2ecf20Sopenharmony_cidefine_show_state_str_function(desc)
2628c2ecf20Sopenharmony_cidefine_show_state_ull_function(above)
2638c2ecf20Sopenharmony_cidefine_show_state_ull_function(below)
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic ssize_t show_state_time(struct cpuidle_state *state,
2668c2ecf20Sopenharmony_ci			       struct cpuidle_state_usage *state_usage,
2678c2ecf20Sopenharmony_ci			       char *buf)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	return sprintf(buf, "%llu\n", ktime_to_us(state_usage->time_ns));
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic ssize_t show_state_disable(struct cpuidle_state *state,
2738c2ecf20Sopenharmony_ci				  struct cpuidle_state_usage *state_usage,
2748c2ecf20Sopenharmony_ci				  char *buf)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	return sprintf(buf, "%llu\n",
2778c2ecf20Sopenharmony_ci		       state_usage->disable & CPUIDLE_STATE_DISABLED_BY_USER);
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_cistatic ssize_t store_state_disable(struct cpuidle_state *state,
2818c2ecf20Sopenharmony_ci				   struct cpuidle_state_usage *state_usage,
2828c2ecf20Sopenharmony_ci				   const char *buf, size_t size)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	unsigned int value;
2858c2ecf20Sopenharmony_ci	int err;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	if (!capable(CAP_SYS_ADMIN))
2888c2ecf20Sopenharmony_ci		return -EPERM;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	err = kstrtouint(buf, 0, &value);
2918c2ecf20Sopenharmony_ci	if (err)
2928c2ecf20Sopenharmony_ci		return err;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if (value)
2958c2ecf20Sopenharmony_ci		state_usage->disable |= CPUIDLE_STATE_DISABLED_BY_USER;
2968c2ecf20Sopenharmony_ci	else
2978c2ecf20Sopenharmony_ci		state_usage->disable &= ~CPUIDLE_STATE_DISABLED_BY_USER;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	return size;
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic ssize_t show_state_default_status(struct cpuidle_state *state,
3038c2ecf20Sopenharmony_ci					  struct cpuidle_state_usage *state_usage,
3048c2ecf20Sopenharmony_ci					  char *buf)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	return sprintf(buf, "%s\n",
3078c2ecf20Sopenharmony_ci		       state->flags & CPUIDLE_FLAG_OFF ? "disabled" : "enabled");
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cidefine_one_state_ro(name, show_state_name);
3118c2ecf20Sopenharmony_cidefine_one_state_ro(desc, show_state_desc);
3128c2ecf20Sopenharmony_cidefine_one_state_ro(latency, show_state_exit_latency);
3138c2ecf20Sopenharmony_cidefine_one_state_ro(residency, show_state_target_residency);
3148c2ecf20Sopenharmony_cidefine_one_state_ro(power, show_state_power_usage);
3158c2ecf20Sopenharmony_cidefine_one_state_ro(usage, show_state_usage);
3168c2ecf20Sopenharmony_cidefine_one_state_ro(rejected, show_state_rejected);
3178c2ecf20Sopenharmony_cidefine_one_state_ro(time, show_state_time);
3188c2ecf20Sopenharmony_cidefine_one_state_rw(disable, show_state_disable, store_state_disable);
3198c2ecf20Sopenharmony_cidefine_one_state_ro(above, show_state_above);
3208c2ecf20Sopenharmony_cidefine_one_state_ro(below, show_state_below);
3218c2ecf20Sopenharmony_cidefine_one_state_ro(default_status, show_state_default_status);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic struct attribute *cpuidle_state_default_attrs[] = {
3248c2ecf20Sopenharmony_ci	&attr_name.attr,
3258c2ecf20Sopenharmony_ci	&attr_desc.attr,
3268c2ecf20Sopenharmony_ci	&attr_latency.attr,
3278c2ecf20Sopenharmony_ci	&attr_residency.attr,
3288c2ecf20Sopenharmony_ci	&attr_power.attr,
3298c2ecf20Sopenharmony_ci	&attr_usage.attr,
3308c2ecf20Sopenharmony_ci	&attr_rejected.attr,
3318c2ecf20Sopenharmony_ci	&attr_time.attr,
3328c2ecf20Sopenharmony_ci	&attr_disable.attr,
3338c2ecf20Sopenharmony_ci	&attr_above.attr,
3348c2ecf20Sopenharmony_ci	&attr_below.attr,
3358c2ecf20Sopenharmony_ci	&attr_default_status.attr,
3368c2ecf20Sopenharmony_ci	NULL
3378c2ecf20Sopenharmony_ci};
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistruct cpuidle_state_kobj {
3408c2ecf20Sopenharmony_ci	struct cpuidle_state *state;
3418c2ecf20Sopenharmony_ci	struct cpuidle_state_usage *state_usage;
3428c2ecf20Sopenharmony_ci	struct completion kobj_unregister;
3438c2ecf20Sopenharmony_ci	struct kobject kobj;
3448c2ecf20Sopenharmony_ci	struct cpuidle_device *device;
3458c2ecf20Sopenharmony_ci};
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci#ifdef CONFIG_SUSPEND
3488c2ecf20Sopenharmony_ci#define define_show_state_s2idle_ull_function(_name) \
3498c2ecf20Sopenharmony_cistatic ssize_t show_state_s2idle_##_name(struct cpuidle_state *state, \
3508c2ecf20Sopenharmony_ci					 struct cpuidle_state_usage *state_usage, \
3518c2ecf20Sopenharmony_ci					 char *buf)				\
3528c2ecf20Sopenharmony_ci{ \
3538c2ecf20Sopenharmony_ci	return sprintf(buf, "%llu\n", state_usage->s2idle_##_name);\
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cidefine_show_state_s2idle_ull_function(usage);
3578c2ecf20Sopenharmony_cidefine_show_state_s2idle_ull_function(time);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci#define define_one_state_s2idle_ro(_name, show) \
3608c2ecf20Sopenharmony_cistatic struct cpuidle_state_attr attr_s2idle_##_name = \
3618c2ecf20Sopenharmony_ci	__ATTR(_name, 0444, show, NULL)
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cidefine_one_state_s2idle_ro(usage, show_state_s2idle_usage);
3648c2ecf20Sopenharmony_cidefine_one_state_s2idle_ro(time, show_state_s2idle_time);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic struct attribute *cpuidle_state_s2idle_attrs[] = {
3678c2ecf20Sopenharmony_ci	&attr_s2idle_usage.attr,
3688c2ecf20Sopenharmony_ci	&attr_s2idle_time.attr,
3698c2ecf20Sopenharmony_ci	NULL
3708c2ecf20Sopenharmony_ci};
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic const struct attribute_group cpuidle_state_s2idle_group = {
3738c2ecf20Sopenharmony_ci	.name	= "s2idle",
3748c2ecf20Sopenharmony_ci	.attrs	= cpuidle_state_s2idle_attrs,
3758c2ecf20Sopenharmony_ci};
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_cistatic void cpuidle_add_s2idle_attr_group(struct cpuidle_state_kobj *kobj)
3788c2ecf20Sopenharmony_ci{
3798c2ecf20Sopenharmony_ci	int ret;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	if (!kobj->state->enter_s2idle)
3828c2ecf20Sopenharmony_ci		return;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	ret = sysfs_create_group(&kobj->kobj, &cpuidle_state_s2idle_group);
3858c2ecf20Sopenharmony_ci	if (ret)
3868c2ecf20Sopenharmony_ci		pr_debug("%s: sysfs attribute group not created\n", __func__);
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic void cpuidle_remove_s2idle_attr_group(struct cpuidle_state_kobj *kobj)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	if (kobj->state->enter_s2idle)
3928c2ecf20Sopenharmony_ci		sysfs_remove_group(&kobj->kobj, &cpuidle_state_s2idle_group);
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ci#else
3958c2ecf20Sopenharmony_cistatic inline void cpuidle_add_s2idle_attr_group(struct cpuidle_state_kobj *kobj) { }
3968c2ecf20Sopenharmony_cistatic inline void cpuidle_remove_s2idle_attr_group(struct cpuidle_state_kobj *kobj) { }
3978c2ecf20Sopenharmony_ci#endif /* CONFIG_SUSPEND */
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci#define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
4008c2ecf20Sopenharmony_ci#define kobj_to_state(k) (kobj_to_state_obj(k)->state)
4018c2ecf20Sopenharmony_ci#define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
4028c2ecf20Sopenharmony_ci#define kobj_to_device(k) (kobj_to_state_obj(k)->device)
4038c2ecf20Sopenharmony_ci#define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_cistatic ssize_t cpuidle_state_show(struct kobject *kobj, struct attribute *attr,
4068c2ecf20Sopenharmony_ci				  char *buf)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	int ret = -EIO;
4098c2ecf20Sopenharmony_ci	struct cpuidle_state *state = kobj_to_state(kobj);
4108c2ecf20Sopenharmony_ci	struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
4118c2ecf20Sopenharmony_ci	struct cpuidle_state_attr *cattr = attr_to_stateattr(attr);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	if (cattr->show)
4148c2ecf20Sopenharmony_ci		ret = cattr->show(state, state_usage, buf);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	return ret;
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic ssize_t cpuidle_state_store(struct kobject *kobj, struct attribute *attr,
4208c2ecf20Sopenharmony_ci				   const char *buf, size_t size)
4218c2ecf20Sopenharmony_ci{
4228c2ecf20Sopenharmony_ci	int ret = -EIO;
4238c2ecf20Sopenharmony_ci	struct cpuidle_state *state = kobj_to_state(kobj);
4248c2ecf20Sopenharmony_ci	struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
4258c2ecf20Sopenharmony_ci	struct cpuidle_state_attr *cattr = attr_to_stateattr(attr);
4268c2ecf20Sopenharmony_ci	struct cpuidle_device *dev = kobj_to_device(kobj);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (cattr->store)
4298c2ecf20Sopenharmony_ci		ret = cattr->store(state, state_usage, buf, size);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	/* reset poll time cache */
4328c2ecf20Sopenharmony_ci	dev->poll_limit_ns = 0;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	return ret;
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_cistatic const struct sysfs_ops cpuidle_state_sysfs_ops = {
4388c2ecf20Sopenharmony_ci	.show = cpuidle_state_show,
4398c2ecf20Sopenharmony_ci	.store = cpuidle_state_store,
4408c2ecf20Sopenharmony_ci};
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_cistatic void cpuidle_state_sysfs_release(struct kobject *kobj)
4438c2ecf20Sopenharmony_ci{
4448c2ecf20Sopenharmony_ci	struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	complete(&state_obj->kobj_unregister);
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_cistatic struct kobj_type ktype_state_cpuidle = {
4508c2ecf20Sopenharmony_ci	.sysfs_ops = &cpuidle_state_sysfs_ops,
4518c2ecf20Sopenharmony_ci	.default_attrs = cpuidle_state_default_attrs,
4528c2ecf20Sopenharmony_ci	.release = cpuidle_state_sysfs_release,
4538c2ecf20Sopenharmony_ci};
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistatic inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	cpuidle_remove_s2idle_attr_group(device->kobjs[i]);
4588c2ecf20Sopenharmony_ci	kobject_put(&device->kobjs[i]->kobj);
4598c2ecf20Sopenharmony_ci	wait_for_completion(&device->kobjs[i]->kobj_unregister);
4608c2ecf20Sopenharmony_ci	kfree(device->kobjs[i]);
4618c2ecf20Sopenharmony_ci	device->kobjs[i] = NULL;
4628c2ecf20Sopenharmony_ci}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci/**
4658c2ecf20Sopenharmony_ci * cpuidle_add_state_sysfs - adds cpuidle states sysfs attributes
4668c2ecf20Sopenharmony_ci * @device: the target device
4678c2ecf20Sopenharmony_ci */
4688c2ecf20Sopenharmony_cistatic int cpuidle_add_state_sysfs(struct cpuidle_device *device)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	int i, ret = -ENOMEM;
4718c2ecf20Sopenharmony_ci	struct cpuidle_state_kobj *kobj;
4728c2ecf20Sopenharmony_ci	struct cpuidle_device_kobj *kdev = device->kobj_dev;
4738c2ecf20Sopenharmony_ci	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	/* state statistics */
4768c2ecf20Sopenharmony_ci	for (i = 0; i < drv->state_count; i++) {
4778c2ecf20Sopenharmony_ci		kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
4788c2ecf20Sopenharmony_ci		if (!kobj) {
4798c2ecf20Sopenharmony_ci			ret = -ENOMEM;
4808c2ecf20Sopenharmony_ci			goto error_state;
4818c2ecf20Sopenharmony_ci		}
4828c2ecf20Sopenharmony_ci		kobj->state = &drv->states[i];
4838c2ecf20Sopenharmony_ci		kobj->state_usage = &device->states_usage[i];
4848c2ecf20Sopenharmony_ci		kobj->device = device;
4858c2ecf20Sopenharmony_ci		init_completion(&kobj->kobj_unregister);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci		ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle,
4888c2ecf20Sopenharmony_ci					   &kdev->kobj, "state%d", i);
4898c2ecf20Sopenharmony_ci		if (ret) {
4908c2ecf20Sopenharmony_ci			kobject_put(&kobj->kobj);
4918c2ecf20Sopenharmony_ci			kfree(kobj);
4928c2ecf20Sopenharmony_ci			goto error_state;
4938c2ecf20Sopenharmony_ci		}
4948c2ecf20Sopenharmony_ci		cpuidle_add_s2idle_attr_group(kobj);
4958c2ecf20Sopenharmony_ci		kobject_uevent(&kobj->kobj, KOBJ_ADD);
4968c2ecf20Sopenharmony_ci		device->kobjs[i] = kobj;
4978c2ecf20Sopenharmony_ci	}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	return 0;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_cierror_state:
5028c2ecf20Sopenharmony_ci	for (i = i - 1; i >= 0; i--)
5038c2ecf20Sopenharmony_ci		cpuidle_free_state_kobj(device, i);
5048c2ecf20Sopenharmony_ci	return ret;
5058c2ecf20Sopenharmony_ci}
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci/**
5088c2ecf20Sopenharmony_ci * cpuidle_remove_driver_sysfs - removes the cpuidle states sysfs attributes
5098c2ecf20Sopenharmony_ci * @device: the target device
5108c2ecf20Sopenharmony_ci */
5118c2ecf20Sopenharmony_cistatic void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
5128c2ecf20Sopenharmony_ci{
5138c2ecf20Sopenharmony_ci	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device);
5148c2ecf20Sopenharmony_ci	int i;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	for (i = 0; i < drv->state_count; i++)
5178c2ecf20Sopenharmony_ci		cpuidle_free_state_kobj(device, i);
5188c2ecf20Sopenharmony_ci}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci#ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
5218c2ecf20Sopenharmony_ci#define kobj_to_driver_kobj(k) container_of(k, struct cpuidle_driver_kobj, kobj)
5228c2ecf20Sopenharmony_ci#define attr_to_driver_attr(a) container_of(a, struct cpuidle_driver_attr, attr)
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci#define define_one_driver_ro(_name, show)                       \
5258c2ecf20Sopenharmony_ci	static struct cpuidle_driver_attr attr_driver_##_name = \
5268c2ecf20Sopenharmony_ci		__ATTR(_name, 0444, show, NULL)
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_cistruct cpuidle_driver_kobj {
5298c2ecf20Sopenharmony_ci	struct cpuidle_driver *drv;
5308c2ecf20Sopenharmony_ci	struct completion kobj_unregister;
5318c2ecf20Sopenharmony_ci	struct kobject kobj;
5328c2ecf20Sopenharmony_ci};
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_cistruct cpuidle_driver_attr {
5358c2ecf20Sopenharmony_ci	struct attribute attr;
5368c2ecf20Sopenharmony_ci	ssize_t (*show)(struct cpuidle_driver *, char *);
5378c2ecf20Sopenharmony_ci	ssize_t (*store)(struct cpuidle_driver *, const char *, size_t);
5388c2ecf20Sopenharmony_ci};
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_cistatic ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
5418c2ecf20Sopenharmony_ci{
5428c2ecf20Sopenharmony_ci	ssize_t ret;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	spin_lock(&cpuidle_driver_lock);
5458c2ecf20Sopenharmony_ci	ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
5468c2ecf20Sopenharmony_ci	spin_unlock(&cpuidle_driver_lock);
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	return ret;
5498c2ecf20Sopenharmony_ci}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_cistatic void cpuidle_driver_sysfs_release(struct kobject *kobj)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
5548c2ecf20Sopenharmony_ci	complete(&driver_kobj->kobj_unregister);
5558c2ecf20Sopenharmony_ci}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_cistatic ssize_t cpuidle_driver_show(struct kobject *kobj, struct attribute *attr,
5588c2ecf20Sopenharmony_ci				   char *buf)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	int ret = -EIO;
5618c2ecf20Sopenharmony_ci	struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
5628c2ecf20Sopenharmony_ci	struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	if (dattr->show)
5658c2ecf20Sopenharmony_ci		ret = dattr->show(driver_kobj->drv, buf);
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	return ret;
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_cistatic ssize_t cpuidle_driver_store(struct kobject *kobj, struct attribute *attr,
5718c2ecf20Sopenharmony_ci				    const char *buf, size_t size)
5728c2ecf20Sopenharmony_ci{
5738c2ecf20Sopenharmony_ci	int ret = -EIO;
5748c2ecf20Sopenharmony_ci	struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
5758c2ecf20Sopenharmony_ci	struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	if (dattr->store)
5788c2ecf20Sopenharmony_ci		ret = dattr->store(driver_kobj->drv, buf, size);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	return ret;
5818c2ecf20Sopenharmony_ci}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_cidefine_one_driver_ro(name, show_driver_name);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_cistatic const struct sysfs_ops cpuidle_driver_sysfs_ops = {
5868c2ecf20Sopenharmony_ci	.show = cpuidle_driver_show,
5878c2ecf20Sopenharmony_ci	.store = cpuidle_driver_store,
5888c2ecf20Sopenharmony_ci};
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_cistatic struct attribute *cpuidle_driver_default_attrs[] = {
5918c2ecf20Sopenharmony_ci	&attr_driver_name.attr,
5928c2ecf20Sopenharmony_ci	NULL
5938c2ecf20Sopenharmony_ci};
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_cistatic struct kobj_type ktype_driver_cpuidle = {
5968c2ecf20Sopenharmony_ci	.sysfs_ops = &cpuidle_driver_sysfs_ops,
5978c2ecf20Sopenharmony_ci	.default_attrs = cpuidle_driver_default_attrs,
5988c2ecf20Sopenharmony_ci	.release = cpuidle_driver_sysfs_release,
5998c2ecf20Sopenharmony_ci};
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci/**
6028c2ecf20Sopenharmony_ci * cpuidle_add_driver_sysfs - adds the driver name sysfs attribute
6038c2ecf20Sopenharmony_ci * @dev: the target device
6048c2ecf20Sopenharmony_ci */
6058c2ecf20Sopenharmony_cistatic int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
6068c2ecf20Sopenharmony_ci{
6078c2ecf20Sopenharmony_ci	struct cpuidle_driver_kobj *kdrv;
6088c2ecf20Sopenharmony_ci	struct cpuidle_device_kobj *kdev = dev->kobj_dev;
6098c2ecf20Sopenharmony_ci	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
6108c2ecf20Sopenharmony_ci	int ret;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	kdrv = kzalloc(sizeof(*kdrv), GFP_KERNEL);
6138c2ecf20Sopenharmony_ci	if (!kdrv)
6148c2ecf20Sopenharmony_ci		return -ENOMEM;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	kdrv->drv = drv;
6178c2ecf20Sopenharmony_ci	init_completion(&kdrv->kobj_unregister);
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	ret = kobject_init_and_add(&kdrv->kobj, &ktype_driver_cpuidle,
6208c2ecf20Sopenharmony_ci				   &kdev->kobj, "driver");
6218c2ecf20Sopenharmony_ci	if (ret) {
6228c2ecf20Sopenharmony_ci		kobject_put(&kdrv->kobj);
6238c2ecf20Sopenharmony_ci		kfree(kdrv);
6248c2ecf20Sopenharmony_ci		return ret;
6258c2ecf20Sopenharmony_ci	}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	kobject_uevent(&kdrv->kobj, KOBJ_ADD);
6288c2ecf20Sopenharmony_ci	dev->kobj_driver = kdrv;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	return ret;
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci/**
6348c2ecf20Sopenharmony_ci * cpuidle_remove_driver_sysfs - removes the driver name sysfs attribute
6358c2ecf20Sopenharmony_ci * @dev: the target device
6368c2ecf20Sopenharmony_ci */
6378c2ecf20Sopenharmony_cistatic void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
6388c2ecf20Sopenharmony_ci{
6398c2ecf20Sopenharmony_ci	struct cpuidle_driver_kobj *kdrv = dev->kobj_driver;
6408c2ecf20Sopenharmony_ci	kobject_put(&kdrv->kobj);
6418c2ecf20Sopenharmony_ci	wait_for_completion(&kdrv->kobj_unregister);
6428c2ecf20Sopenharmony_ci	kfree(kdrv);
6438c2ecf20Sopenharmony_ci}
6448c2ecf20Sopenharmony_ci#else
6458c2ecf20Sopenharmony_cistatic inline int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	return 0;
6488c2ecf20Sopenharmony_ci}
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_cistatic inline void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
6518c2ecf20Sopenharmony_ci{
6528c2ecf20Sopenharmony_ci	;
6538c2ecf20Sopenharmony_ci}
6548c2ecf20Sopenharmony_ci#endif
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci/**
6578c2ecf20Sopenharmony_ci * cpuidle_add_device_sysfs - adds device specific sysfs attributes
6588c2ecf20Sopenharmony_ci * @device: the target device
6598c2ecf20Sopenharmony_ci */
6608c2ecf20Sopenharmony_ciint cpuidle_add_device_sysfs(struct cpuidle_device *device)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	int ret;
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	ret = cpuidle_add_state_sysfs(device);
6658c2ecf20Sopenharmony_ci	if (ret)
6668c2ecf20Sopenharmony_ci		return ret;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	ret = cpuidle_add_driver_sysfs(device);
6698c2ecf20Sopenharmony_ci	if (ret)
6708c2ecf20Sopenharmony_ci		cpuidle_remove_state_sysfs(device);
6718c2ecf20Sopenharmony_ci	return ret;
6728c2ecf20Sopenharmony_ci}
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci/**
6758c2ecf20Sopenharmony_ci * cpuidle_remove_device_sysfs : removes device specific sysfs attributes
6768c2ecf20Sopenharmony_ci * @device : the target device
6778c2ecf20Sopenharmony_ci */
6788c2ecf20Sopenharmony_civoid cpuidle_remove_device_sysfs(struct cpuidle_device *device)
6798c2ecf20Sopenharmony_ci{
6808c2ecf20Sopenharmony_ci	cpuidle_remove_driver_sysfs(device);
6818c2ecf20Sopenharmony_ci	cpuidle_remove_state_sysfs(device);
6828c2ecf20Sopenharmony_ci}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci/**
6858c2ecf20Sopenharmony_ci * cpuidle_add_sysfs - creates a sysfs instance for the target device
6868c2ecf20Sopenharmony_ci * @dev: the target device
6878c2ecf20Sopenharmony_ci */
6888c2ecf20Sopenharmony_ciint cpuidle_add_sysfs(struct cpuidle_device *dev)
6898c2ecf20Sopenharmony_ci{
6908c2ecf20Sopenharmony_ci	struct cpuidle_device_kobj *kdev;
6918c2ecf20Sopenharmony_ci	struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
6928c2ecf20Sopenharmony_ci	int error;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	/*
6958c2ecf20Sopenharmony_ci	 * Return if cpu_device is not setup for this CPU.
6968c2ecf20Sopenharmony_ci	 *
6978c2ecf20Sopenharmony_ci	 * This could happen if the arch did not set up cpu_device
6988c2ecf20Sopenharmony_ci	 * since this CPU is not in cpu_present mask and the
6998c2ecf20Sopenharmony_ci	 * driver did not send a correct CPU mask during registration.
7008c2ecf20Sopenharmony_ci	 * Without this check we would end up passing bogus
7018c2ecf20Sopenharmony_ci	 * value for &cpu_dev->kobj in kobject_init_and_add()
7028c2ecf20Sopenharmony_ci	 */
7038c2ecf20Sopenharmony_ci	if (!cpu_dev)
7048c2ecf20Sopenharmony_ci		return -ENODEV;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
7078c2ecf20Sopenharmony_ci	if (!kdev)
7088c2ecf20Sopenharmony_ci		return -ENOMEM;
7098c2ecf20Sopenharmony_ci	kdev->dev = dev;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	init_completion(&kdev->kobj_unregister);
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	error = kobject_init_and_add(&kdev->kobj, &ktype_cpuidle, &cpu_dev->kobj,
7148c2ecf20Sopenharmony_ci				   "cpuidle");
7158c2ecf20Sopenharmony_ci	if (error) {
7168c2ecf20Sopenharmony_ci		kobject_put(&kdev->kobj);
7178c2ecf20Sopenharmony_ci		kfree(kdev);
7188c2ecf20Sopenharmony_ci		return error;
7198c2ecf20Sopenharmony_ci	}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	dev->kobj_dev = kdev;
7228c2ecf20Sopenharmony_ci	kobject_uevent(&kdev->kobj, KOBJ_ADD);
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	return 0;
7258c2ecf20Sopenharmony_ci}
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci/**
7288c2ecf20Sopenharmony_ci * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
7298c2ecf20Sopenharmony_ci * @dev: the target device
7308c2ecf20Sopenharmony_ci */
7318c2ecf20Sopenharmony_civoid cpuidle_remove_sysfs(struct cpuidle_device *dev)
7328c2ecf20Sopenharmony_ci{
7338c2ecf20Sopenharmony_ci	struct cpuidle_device_kobj *kdev = dev->kobj_dev;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	kobject_put(&kdev->kobj);
7368c2ecf20Sopenharmony_ci	wait_for_completion(&kdev->kobj_unregister);
7378c2ecf20Sopenharmony_ci	kfree(kdev);
7388c2ecf20Sopenharmony_ci}
739