Path: blob/master/arch/powerpc/platforms/powernv/opal-powercap.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* PowerNV OPAL Powercap interface3*4* Copyright 2017 IBM Corp.5*/67#define pr_fmt(fmt) "opal-powercap: " fmt89#include <linux/of.h>10#include <linux/kobject.h>11#include <linux/slab.h>1213#include <asm/opal.h>1415static DEFINE_MUTEX(powercap_mutex);1617static struct kobject *powercap_kobj;1819struct powercap_attr {20u32 handle;21struct kobj_attribute attr;22};2324static struct pcap {25struct attribute_group pg;26struct powercap_attr *pattrs;27} *pcaps;2829static ssize_t powercap_show(struct kobject *kobj, struct kobj_attribute *attr,30char *buf)31{32struct powercap_attr *pcap_attr = container_of(attr,33struct powercap_attr, attr);34struct opal_msg msg;35u32 pcap;36int ret, token;3738token = opal_async_get_token_interruptible();39if (token < 0) {40pr_devel("Failed to get token\n");41return token;42}4344ret = mutex_lock_interruptible(&powercap_mutex);45if (ret)46goto out_token;4748ret = opal_get_powercap(pcap_attr->handle, token, (u32 *)__pa(&pcap));49switch (ret) {50case OPAL_ASYNC_COMPLETION:51ret = opal_async_wait_response(token, &msg);52if (ret) {53pr_devel("Failed to wait for the async response\n");54ret = -EIO;55goto out;56}57ret = opal_error_code(opal_get_async_rc(msg));58if (!ret) {59ret = sprintf(buf, "%u\n", be32_to_cpu(pcap));60if (ret < 0)61ret = -EIO;62}63break;64case OPAL_SUCCESS:65ret = sprintf(buf, "%u\n", be32_to_cpu(pcap));66if (ret < 0)67ret = -EIO;68break;69default:70ret = opal_error_code(ret);71}7273out:74mutex_unlock(&powercap_mutex);75out_token:76opal_async_release_token(token);77return ret;78}7980static ssize_t powercap_store(struct kobject *kobj,81struct kobj_attribute *attr, const char *buf,82size_t count)83{84struct powercap_attr *pcap_attr = container_of(attr,85struct powercap_attr, attr);86struct opal_msg msg;87u32 pcap;88int ret, token;8990ret = kstrtoint(buf, 0, &pcap);91if (ret)92return ret;9394token = opal_async_get_token_interruptible();95if (token < 0) {96pr_devel("Failed to get token\n");97return token;98}99100ret = mutex_lock_interruptible(&powercap_mutex);101if (ret)102goto out_token;103104ret = opal_set_powercap(pcap_attr->handle, token, pcap);105switch (ret) {106case OPAL_ASYNC_COMPLETION:107ret = opal_async_wait_response(token, &msg);108if (ret) {109pr_devel("Failed to wait for the async response\n");110ret = -EIO;111goto out;112}113ret = opal_error_code(opal_get_async_rc(msg));114if (!ret)115ret = count;116break;117case OPAL_SUCCESS:118ret = count;119break;120default:121ret = opal_error_code(ret);122}123124out:125mutex_unlock(&powercap_mutex);126out_token:127opal_async_release_token(token);128return ret;129}130131static void __init powercap_add_attr(int handle, const char *name,132struct powercap_attr *attr)133{134attr->handle = handle;135sysfs_attr_init(&attr->attr.attr);136attr->attr.attr.name = name;137attr->attr.attr.mode = 0444;138attr->attr.show = powercap_show;139}140141void __init opal_powercap_init(void)142{143struct device_node *powercap, *node;144int i = 0;145146powercap = of_find_compatible_node(NULL, NULL, "ibm,opal-powercap");147if (!powercap) {148pr_devel("Powercap node not found\n");149return;150}151152pcaps = kcalloc(of_get_child_count(powercap), sizeof(*pcaps),153GFP_KERNEL);154if (!pcaps)155goto out_put_powercap;156157powercap_kobj = kobject_create_and_add("powercap", opal_kobj);158if (!powercap_kobj) {159pr_warn("Failed to create powercap kobject\n");160goto out_pcaps;161}162163i = 0;164for_each_child_of_node(powercap, node) {165u32 cur, min, max;166int j = 0;167bool has_cur = false, has_min = false, has_max = false;168169if (!of_property_read_u32(node, "powercap-min", &min)) {170j++;171has_min = true;172}173174if (!of_property_read_u32(node, "powercap-max", &max)) {175j++;176has_max = true;177}178179if (!of_property_read_u32(node, "powercap-current", &cur)) {180j++;181has_cur = true;182}183184pcaps[i].pattrs = kcalloc(j, sizeof(struct powercap_attr),185GFP_KERNEL);186if (!pcaps[i].pattrs)187goto out_pcaps_pattrs;188189pcaps[i].pg.attrs = kcalloc(j + 1, sizeof(struct attribute *),190GFP_KERNEL);191if (!pcaps[i].pg.attrs) {192kfree(pcaps[i].pattrs);193goto out_pcaps_pattrs;194}195196j = 0;197pcaps[i].pg.name = kasprintf(GFP_KERNEL, "%pOFn", node);198if (!pcaps[i].pg.name) {199kfree(pcaps[i].pattrs);200kfree(pcaps[i].pg.attrs);201goto out_pcaps_pattrs;202}203204if (has_min) {205powercap_add_attr(min, "powercap-min",206&pcaps[i].pattrs[j]);207pcaps[i].pg.attrs[j] = &pcaps[i].pattrs[j].attr.attr;208j++;209}210211if (has_max) {212powercap_add_attr(max, "powercap-max",213&pcaps[i].pattrs[j]);214pcaps[i].pg.attrs[j] = &pcaps[i].pattrs[j].attr.attr;215j++;216}217218if (has_cur) {219powercap_add_attr(cur, "powercap-current",220&pcaps[i].pattrs[j]);221pcaps[i].pattrs[j].attr.attr.mode |= 0220;222pcaps[i].pattrs[j].attr.store = powercap_store;223pcaps[i].pg.attrs[j] = &pcaps[i].pattrs[j].attr.attr;224j++;225}226227if (sysfs_create_group(powercap_kobj, &pcaps[i].pg)) {228pr_warn("Failed to create powercap attribute group %s\n",229pcaps[i].pg.name);230goto out_pcaps_pattrs;231}232i++;233}234of_node_put(powercap);235236return;237238out_pcaps_pattrs:239while (--i >= 0) {240kfree(pcaps[i].pattrs);241kfree(pcaps[i].pg.attrs);242kfree(pcaps[i].pg.name);243}244kobject_put(powercap_kobj);245of_node_put(node);246out_pcaps:247kfree(pcaps);248out_put_powercap:249of_node_put(powercap);250}251252253