1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PowerNV OPAL Powercap interface
5 * Copyright 2017 IBM Corp.
8 #define pr_fmt(fmt) "opal-powercap: " fmt
11 #include <linux/kobject.h>
12 #include <linux/slab.h>
16 static DEFINE_MUTEX(powercap_mutex
);
18 static struct kobject
*powercap_kobj
;
20 struct powercap_attr
{
22 struct kobj_attribute attr
;
26 struct attribute_group pg
;
27 struct powercap_attr
*pattrs
;
30 static ssize_t
powercap_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
33 struct powercap_attr
*pcap_attr
= container_of(attr
,
34 struct powercap_attr
, attr
);
39 token
= opal_async_get_token_interruptible();
41 pr_devel("Failed to get token\n");
45 ret
= mutex_lock_interruptible(&powercap_mutex
);
49 ret
= opal_get_powercap(pcap_attr
->handle
, token
, (u32
*)__pa(&pcap
));
51 case OPAL_ASYNC_COMPLETION
:
52 ret
= opal_async_wait_response(token
, &msg
);
54 pr_devel("Failed to wait for the async response\n");
58 ret
= opal_error_code(opal_get_async_rc(msg
));
60 ret
= sprintf(buf
, "%u\n", be32_to_cpu(pcap
));
66 ret
= sprintf(buf
, "%u\n", be32_to_cpu(pcap
));
71 ret
= opal_error_code(ret
);
75 mutex_unlock(&powercap_mutex
);
77 opal_async_release_token(token
);
81 static ssize_t
powercap_store(struct kobject
*kobj
,
82 struct kobj_attribute
*attr
, const char *buf
,
85 struct powercap_attr
*pcap_attr
= container_of(attr
,
86 struct powercap_attr
, attr
);
91 ret
= kstrtoint(buf
, 0, &pcap
);
95 token
= opal_async_get_token_interruptible();
97 pr_devel("Failed to get token\n");
101 ret
= mutex_lock_interruptible(&powercap_mutex
);
105 ret
= opal_set_powercap(pcap_attr
->handle
, token
, pcap
);
107 case OPAL_ASYNC_COMPLETION
:
108 ret
= opal_async_wait_response(token
, &msg
);
110 pr_devel("Failed to wait for the async response\n");
114 ret
= opal_error_code(opal_get_async_rc(msg
));
122 ret
= opal_error_code(ret
);
126 mutex_unlock(&powercap_mutex
);
128 opal_async_release_token(token
);
132 static void powercap_add_attr(int handle
, const char *name
,
133 struct powercap_attr
*attr
)
135 attr
->handle
= handle
;
136 sysfs_attr_init(&attr
->attr
.attr
);
137 attr
->attr
.attr
.name
= name
;
138 attr
->attr
.attr
.mode
= 0444;
139 attr
->attr
.show
= powercap_show
;
142 void __init
opal_powercap_init(void)
144 struct device_node
*powercap
, *node
;
147 powercap
= of_find_compatible_node(NULL
, NULL
, "ibm,opal-powercap");
149 pr_devel("Powercap node not found\n");
153 pcaps
= kcalloc(of_get_child_count(powercap
), sizeof(*pcaps
),
158 powercap_kobj
= kobject_create_and_add("powercap", opal_kobj
);
159 if (!powercap_kobj
) {
160 pr_warn("Failed to create powercap kobject\n");
165 for_each_child_of_node(powercap
, node
) {
168 bool has_cur
= false, has_min
= false, has_max
= false;
170 if (!of_property_read_u32(node
, "powercap-min", &min
)) {
175 if (!of_property_read_u32(node
, "powercap-max", &max
)) {
180 if (!of_property_read_u32(node
, "powercap-current", &cur
)) {
185 pcaps
[i
].pattrs
= kcalloc(j
, sizeof(struct powercap_attr
),
187 if (!pcaps
[i
].pattrs
)
188 goto out_pcaps_pattrs
;
190 pcaps
[i
].pg
.attrs
= kcalloc(j
+ 1, sizeof(struct attribute
*),
192 if (!pcaps
[i
].pg
.attrs
) {
193 kfree(pcaps
[i
].pattrs
);
194 goto out_pcaps_pattrs
;
198 pcaps
[i
].pg
.name
= kasprintf(GFP_KERNEL
, "%pOFn", node
);
200 powercap_add_attr(min
, "powercap-min",
201 &pcaps
[i
].pattrs
[j
]);
202 pcaps
[i
].pg
.attrs
[j
] = &pcaps
[i
].pattrs
[j
].attr
.attr
;
207 powercap_add_attr(max
, "powercap-max",
208 &pcaps
[i
].pattrs
[j
]);
209 pcaps
[i
].pg
.attrs
[j
] = &pcaps
[i
].pattrs
[j
].attr
.attr
;
214 powercap_add_attr(cur
, "powercap-current",
215 &pcaps
[i
].pattrs
[j
]);
216 pcaps
[i
].pattrs
[j
].attr
.attr
.mode
|= 0220;
217 pcaps
[i
].pattrs
[j
].attr
.store
= powercap_store
;
218 pcaps
[i
].pg
.attrs
[j
] = &pcaps
[i
].pattrs
[j
].attr
.attr
;
222 if (sysfs_create_group(powercap_kobj
, &pcaps
[i
].pg
)) {
223 pr_warn("Failed to create powercap attribute group %s\n",
225 goto out_pcaps_pattrs
;
234 kfree(pcaps
[i
].pattrs
);
235 kfree(pcaps
[i
].pg
.attrs
);
236 kfree(pcaps
[i
].pg
.name
);
238 kobject_put(powercap_kobj
);