1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22d0c58adSRafael J. Wysocki /*
32d0c58adSRafael J. Wysocki * Abstract code for CPUFreq governor tunable sysfs attributes.
42d0c58adSRafael J. Wysocki *
52d0c58adSRafael J. Wysocki * Copyright (C) 2016, Intel Corporation
62d0c58adSRafael J. Wysocki * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
72d0c58adSRafael J. Wysocki */
82d0c58adSRafael J. Wysocki
92d0c58adSRafael J. Wysocki #include "cpufreq_governor.h"
102d0c58adSRafael J. Wysocki
to_gov_attr(struct attribute * attr)112d0c58adSRafael J. Wysocki static inline struct governor_attr *to_gov_attr(struct attribute *attr)
122d0c58adSRafael J. Wysocki {
132d0c58adSRafael J. Wysocki return container_of(attr, struct governor_attr, attr);
142d0c58adSRafael J. Wysocki }
152d0c58adSRafael J. Wysocki
governor_show(struct kobject * kobj,struct attribute * attr,char * buf)162d0c58adSRafael J. Wysocki static ssize_t governor_show(struct kobject *kobj, struct attribute *attr,
172d0c58adSRafael J. Wysocki char *buf)
182d0c58adSRafael J. Wysocki {
192d0c58adSRafael J. Wysocki struct governor_attr *gattr = to_gov_attr(attr);
202d0c58adSRafael J. Wysocki
212d0c58adSRafael J. Wysocki return gattr->show(to_gov_attr_set(kobj), buf);
222d0c58adSRafael J. Wysocki }
232d0c58adSRafael J. Wysocki
governor_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)242d0c58adSRafael J. Wysocki static ssize_t governor_store(struct kobject *kobj, struct attribute *attr,
252d0c58adSRafael J. Wysocki const char *buf, size_t count)
262d0c58adSRafael J. Wysocki {
272d0c58adSRafael J. Wysocki struct gov_attr_set *attr_set = to_gov_attr_set(kobj);
282d0c58adSRafael J. Wysocki struct governor_attr *gattr = to_gov_attr(attr);
292d0c58adSRafael J. Wysocki int ret;
302d0c58adSRafael J. Wysocki
312d0c58adSRafael J. Wysocki mutex_lock(&attr_set->update_lock);
322d0c58adSRafael J. Wysocki ret = attr_set->usage_count ? gattr->store(attr_set, buf, count) : -EBUSY;
332d0c58adSRafael J. Wysocki mutex_unlock(&attr_set->update_lock);
342d0c58adSRafael J. Wysocki return ret;
352d0c58adSRafael J. Wysocki }
362d0c58adSRafael J. Wysocki
372d0c58adSRafael J. Wysocki const struct sysfs_ops governor_sysfs_ops = {
382d0c58adSRafael J. Wysocki .show = governor_show,
392d0c58adSRafael J. Wysocki .store = governor_store,
402d0c58adSRafael J. Wysocki };
412d0c58adSRafael J. Wysocki EXPORT_SYMBOL_GPL(governor_sysfs_ops);
422d0c58adSRafael J. Wysocki
gov_attr_set_init(struct gov_attr_set * attr_set,struct list_head * list_node)432d0c58adSRafael J. Wysocki void gov_attr_set_init(struct gov_attr_set *attr_set, struct list_head *list_node)
442d0c58adSRafael J. Wysocki {
452d0c58adSRafael J. Wysocki INIT_LIST_HEAD(&attr_set->policy_list);
462d0c58adSRafael J. Wysocki mutex_init(&attr_set->update_lock);
472d0c58adSRafael J. Wysocki attr_set->usage_count = 1;
482d0c58adSRafael J. Wysocki list_add(list_node, &attr_set->policy_list);
492d0c58adSRafael J. Wysocki }
502d0c58adSRafael J. Wysocki EXPORT_SYMBOL_GPL(gov_attr_set_init);
512d0c58adSRafael J. Wysocki
gov_attr_set_get(struct gov_attr_set * attr_set,struct list_head * list_node)522d0c58adSRafael J. Wysocki void gov_attr_set_get(struct gov_attr_set *attr_set, struct list_head *list_node)
532d0c58adSRafael J. Wysocki {
542d0c58adSRafael J. Wysocki mutex_lock(&attr_set->update_lock);
552d0c58adSRafael J. Wysocki attr_set->usage_count++;
562d0c58adSRafael J. Wysocki list_add(list_node, &attr_set->policy_list);
572d0c58adSRafael J. Wysocki mutex_unlock(&attr_set->update_lock);
582d0c58adSRafael J. Wysocki }
592d0c58adSRafael J. Wysocki EXPORT_SYMBOL_GPL(gov_attr_set_get);
602d0c58adSRafael J. Wysocki
gov_attr_set_put(struct gov_attr_set * attr_set,struct list_head * list_node)612d0c58adSRafael J. Wysocki unsigned int gov_attr_set_put(struct gov_attr_set *attr_set, struct list_head *list_node)
622d0c58adSRafael J. Wysocki {
632d0c58adSRafael J. Wysocki unsigned int count;
642d0c58adSRafael J. Wysocki
652d0c58adSRafael J. Wysocki mutex_lock(&attr_set->update_lock);
662d0c58adSRafael J. Wysocki list_del(list_node);
672d0c58adSRafael J. Wysocki count = --attr_set->usage_count;
682d0c58adSRafael J. Wysocki mutex_unlock(&attr_set->update_lock);
692d0c58adSRafael J. Wysocki if (count)
702d0c58adSRafael J. Wysocki return count;
712d0c58adSRafael J. Wysocki
722d0c58adSRafael J. Wysocki mutex_destroy(&attr_set->update_lock);
73*cdef1196SJames Morse kobject_put(&attr_set->kobj);
742d0c58adSRafael J. Wysocki return 0;
752d0c58adSRafael J. Wysocki }
762d0c58adSRafael J. Wysocki EXPORT_SYMBOL_GPL(gov_attr_set_put);
77