1 // SPDX-License-Identifier: GPL-2.0-only
3 * Abstract code for CPUFreq governor tunable sysfs attributes.
5 * Copyright (C) 2016, Intel Corporation
6 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
9 #include "cpufreq_governor.h"
11 static inline struct gov_attr_set
*to_gov_attr_set(struct kobject
*kobj
)
13 return container_of(kobj
, struct gov_attr_set
, kobj
);
16 static inline struct governor_attr
*to_gov_attr(struct attribute
*attr
)
18 return container_of(attr
, struct governor_attr
, attr
);
21 static ssize_t
governor_show(struct kobject
*kobj
, struct attribute
*attr
,
24 struct governor_attr
*gattr
= to_gov_attr(attr
);
26 return gattr
->show(to_gov_attr_set(kobj
), buf
);
29 static ssize_t
governor_store(struct kobject
*kobj
, struct attribute
*attr
,
30 const char *buf
, size_t count
)
32 struct gov_attr_set
*attr_set
= to_gov_attr_set(kobj
);
33 struct governor_attr
*gattr
= to_gov_attr(attr
);
36 mutex_lock(&attr_set
->update_lock
);
37 ret
= attr_set
->usage_count
? gattr
->store(attr_set
, buf
, count
) : -EBUSY
;
38 mutex_unlock(&attr_set
->update_lock
);
42 const struct sysfs_ops governor_sysfs_ops
= {
43 .show
= governor_show
,
44 .store
= governor_store
,
46 EXPORT_SYMBOL_GPL(governor_sysfs_ops
);
48 void gov_attr_set_init(struct gov_attr_set
*attr_set
, struct list_head
*list_node
)
50 INIT_LIST_HEAD(&attr_set
->policy_list
);
51 mutex_init(&attr_set
->update_lock
);
52 attr_set
->usage_count
= 1;
53 list_add(list_node
, &attr_set
->policy_list
);
55 EXPORT_SYMBOL_GPL(gov_attr_set_init
);
57 void gov_attr_set_get(struct gov_attr_set
*attr_set
, struct list_head
*list_node
)
59 mutex_lock(&attr_set
->update_lock
);
60 attr_set
->usage_count
++;
61 list_add(list_node
, &attr_set
->policy_list
);
62 mutex_unlock(&attr_set
->update_lock
);
64 EXPORT_SYMBOL_GPL(gov_attr_set_get
);
66 unsigned int gov_attr_set_put(struct gov_attr_set
*attr_set
, struct list_head
*list_node
)
70 mutex_lock(&attr_set
->update_lock
);
72 count
= --attr_set
->usage_count
;
73 mutex_unlock(&attr_set
->update_lock
);
77 kobject_put(&attr_set
->kobj
);
78 mutex_destroy(&attr_set
->update_lock
);
81 EXPORT_SYMBOL_GPL(gov_attr_set_put
);