1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PowerNV OPAL Power-Shift-Ratio interface
5 * Copyright 2017 IBM Corp.
8 #define pr_fmt(fmt) "opal-psr: " fmt
11 #include <linux/kobject.h>
12 #include <linux/slab.h>
16 static DEFINE_MUTEX(psr_mutex);
18 static struct kobject *psr_kobj;
20 static struct psr_attr {
22 struct kobj_attribute attr;
25 static ssize_t psr_show(struct kobject *kobj, struct kobj_attribute *attr,
28 struct psr_attr *psr_attr = container_of(attr, struct psr_attr, attr);
32 token = opal_async_get_token_interruptible();
34 pr_devel("Failed to get token\n");
38 ret = mutex_lock_interruptible(&psr_mutex);
42 ret = opal_get_power_shift_ratio(psr_attr->handle, token,
45 case OPAL_ASYNC_COMPLETION:
46 ret = opal_async_wait_response(token, &msg);
48 pr_devel("Failed to wait for the async response\n");
52 ret = opal_error_code(opal_get_async_rc(msg));
54 ret = sprintf(buf, "%u\n", be32_to_cpu(psr));
60 ret = sprintf(buf, "%u\n", be32_to_cpu(psr));
65 ret = opal_error_code(ret);
69 mutex_unlock(&psr_mutex);
71 opal_async_release_token(token);
75 static ssize_t psr_store(struct kobject *kobj, struct kobj_attribute *attr,
76 const char *buf, size_t count)
78 struct psr_attr *psr_attr = container_of(attr, struct psr_attr, attr);
82 ret = kstrtoint(buf, 0, &psr);
86 token = opal_async_get_token_interruptible();
88 pr_devel("Failed to get token\n");
92 ret = mutex_lock_interruptible(&psr_mutex);
96 ret = opal_set_power_shift_ratio(psr_attr->handle, token, psr);
98 case OPAL_ASYNC_COMPLETION:
99 ret = opal_async_wait_response(token, &msg);
101 pr_devel("Failed to wait for the async response\n");
105 ret = opal_error_code(opal_get_async_rc(msg));
113 ret = opal_error_code(ret);
117 mutex_unlock(&psr_mutex);
119 opal_async_release_token(token);
123 void __init opal_psr_init(void)
125 struct device_node *psr, *node;
128 psr = of_find_compatible_node(NULL, NULL,
129 "ibm,opal-power-shift-ratio");
131 pr_devel("Power-shift-ratio node not found\n");
135 psr_attrs = kcalloc(of_get_child_count(psr), sizeof(*psr_attrs),
140 psr_kobj = kobject_create_and_add("psr", opal_kobj);
142 pr_warn("Failed to create psr kobject\n");
146 for_each_child_of_node(psr, node) {
147 if (of_property_read_u32(node, "handle",
148 &psr_attrs[i].handle))
151 sysfs_attr_init(&psr_attrs[i].attr.attr);
152 if (of_property_read_string(node, "label",
153 &psr_attrs[i].attr.attr.name))
155 psr_attrs[i].attr.attr.mode = 0664;
156 psr_attrs[i].attr.show = psr_show;
157 psr_attrs[i].attr.store = psr_store;
158 if (sysfs_create_file(psr_kobj, &psr_attrs[i].attr.attr)) {
159 pr_devel("Failed to create psr sysfs file %s\n",
160 psr_attrs[i].attr.attr.name);
168 kobject_put(psr_kobj);