14029cd66SNeelesh Gupta /* 24029cd66SNeelesh Gupta * PowerNV system parameter code 34029cd66SNeelesh Gupta * 44029cd66SNeelesh Gupta * Copyright (C) 2013 IBM 54029cd66SNeelesh Gupta * 64029cd66SNeelesh Gupta * This program is free software; you can redistribute it and/or modify 74029cd66SNeelesh Gupta * it under the terms of the GNU General Public License as published by 84029cd66SNeelesh Gupta * the Free Software Foundation; either version 2 of the License, or 94029cd66SNeelesh Gupta * (at your option) any later version. 104029cd66SNeelesh Gupta * 114029cd66SNeelesh Gupta * This program is distributed in the hope that it will be useful, 124029cd66SNeelesh Gupta * but WITHOUT ANY WARRANTY; without even the implied warranty of 134029cd66SNeelesh Gupta * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 144029cd66SNeelesh Gupta * GNU General Public License for more details. 154029cd66SNeelesh Gupta * 164029cd66SNeelesh Gupta * You should have received a copy of the GNU General Public License 174029cd66SNeelesh Gupta * along with this program; if not, write to the Free Software 184029cd66SNeelesh Gupta * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 194029cd66SNeelesh Gupta */ 204029cd66SNeelesh Gupta 214029cd66SNeelesh Gupta #include <linux/kobject.h> 224029cd66SNeelesh Gupta #include <linux/mutex.h> 234029cd66SNeelesh Gupta #include <linux/slab.h> 244029cd66SNeelesh Gupta #include <linux/of.h> 254029cd66SNeelesh Gupta #include <linux/gfp.h> 264029cd66SNeelesh Gupta #include <linux/stat.h> 274029cd66SNeelesh Gupta #include <asm/opal.h> 284029cd66SNeelesh Gupta 294029cd66SNeelesh Gupta #define MAX_PARAM_DATA_LEN 64 304029cd66SNeelesh Gupta 314029cd66SNeelesh Gupta static DEFINE_MUTEX(opal_sysparam_mutex); 324029cd66SNeelesh Gupta static struct kobject *sysparam_kobj; 334029cd66SNeelesh Gupta static void *param_data_buf; 344029cd66SNeelesh Gupta 354029cd66SNeelesh Gupta struct param_attr { 364029cd66SNeelesh Gupta struct list_head list; 374029cd66SNeelesh Gupta u32 param_id; 384029cd66SNeelesh Gupta u32 param_size; 394029cd66SNeelesh Gupta struct kobj_attribute kobj_attr; 404029cd66SNeelesh Gupta }; 414029cd66SNeelesh Gupta 424029cd66SNeelesh Gupta static int opal_get_sys_param(u32 param_id, u32 length, void *buffer) 434029cd66SNeelesh Gupta { 444029cd66SNeelesh Gupta struct opal_msg msg; 454029cd66SNeelesh Gupta int ret, token; 464029cd66SNeelesh Gupta 474029cd66SNeelesh Gupta token = opal_async_get_token_interruptible(); 484029cd66SNeelesh Gupta if (token < 0) { 494029cd66SNeelesh Gupta if (token != -ERESTARTSYS) 504029cd66SNeelesh Gupta pr_err("%s: Couldn't get the token, returning\n", 514029cd66SNeelesh Gupta __func__); 524029cd66SNeelesh Gupta ret = token; 534029cd66SNeelesh Gupta goto out; 544029cd66SNeelesh Gupta } 554029cd66SNeelesh Gupta 564029cd66SNeelesh Gupta ret = opal_get_param(token, param_id, (u64)buffer, length); 574029cd66SNeelesh Gupta if (ret != OPAL_ASYNC_COMPLETION) 584029cd66SNeelesh Gupta goto out_token; 594029cd66SNeelesh Gupta 604029cd66SNeelesh Gupta ret = opal_async_wait_response(token, &msg); 614029cd66SNeelesh Gupta if (ret) { 624029cd66SNeelesh Gupta pr_err("%s: Failed to wait for the async response, %d\n", 634029cd66SNeelesh Gupta __func__, ret); 644029cd66SNeelesh Gupta goto out_token; 654029cd66SNeelesh Gupta } 664029cd66SNeelesh Gupta 67bb4398e1SAnton Blanchard ret = be64_to_cpu(msg.params[1]); 684029cd66SNeelesh Gupta 694029cd66SNeelesh Gupta out_token: 704029cd66SNeelesh Gupta opal_async_release_token(token); 714029cd66SNeelesh Gupta out: 724029cd66SNeelesh Gupta return ret; 734029cd66SNeelesh Gupta } 744029cd66SNeelesh Gupta 754029cd66SNeelesh Gupta static int opal_set_sys_param(u32 param_id, u32 length, void *buffer) 764029cd66SNeelesh Gupta { 774029cd66SNeelesh Gupta struct opal_msg msg; 784029cd66SNeelesh Gupta int ret, token; 794029cd66SNeelesh Gupta 804029cd66SNeelesh Gupta token = opal_async_get_token_interruptible(); 814029cd66SNeelesh Gupta if (token < 0) { 824029cd66SNeelesh Gupta if (token != -ERESTARTSYS) 834029cd66SNeelesh Gupta pr_err("%s: Couldn't get the token, returning\n", 844029cd66SNeelesh Gupta __func__); 854029cd66SNeelesh Gupta ret = token; 864029cd66SNeelesh Gupta goto out; 874029cd66SNeelesh Gupta } 884029cd66SNeelesh Gupta 894029cd66SNeelesh Gupta ret = opal_set_param(token, param_id, (u64)buffer, length); 904029cd66SNeelesh Gupta 914029cd66SNeelesh Gupta if (ret != OPAL_ASYNC_COMPLETION) 924029cd66SNeelesh Gupta goto out_token; 934029cd66SNeelesh Gupta 944029cd66SNeelesh Gupta ret = opal_async_wait_response(token, &msg); 954029cd66SNeelesh Gupta if (ret) { 964029cd66SNeelesh Gupta pr_err("%s: Failed to wait for the async response, %d\n", 974029cd66SNeelesh Gupta __func__, ret); 984029cd66SNeelesh Gupta goto out_token; 994029cd66SNeelesh Gupta } 1004029cd66SNeelesh Gupta 101bb4398e1SAnton Blanchard ret = be64_to_cpu(msg.params[1]); 1024029cd66SNeelesh Gupta 1034029cd66SNeelesh Gupta out_token: 1044029cd66SNeelesh Gupta opal_async_release_token(token); 1054029cd66SNeelesh Gupta out: 1064029cd66SNeelesh Gupta return ret; 1074029cd66SNeelesh Gupta } 1084029cd66SNeelesh Gupta 1094029cd66SNeelesh Gupta static ssize_t sys_param_show(struct kobject *kobj, 1104029cd66SNeelesh Gupta struct kobj_attribute *kobj_attr, char *buf) 1114029cd66SNeelesh Gupta { 1124029cd66SNeelesh Gupta struct param_attr *attr = container_of(kobj_attr, struct param_attr, 1134029cd66SNeelesh Gupta kobj_attr); 1144029cd66SNeelesh Gupta int ret; 1154029cd66SNeelesh Gupta 1164029cd66SNeelesh Gupta mutex_lock(&opal_sysparam_mutex); 1174029cd66SNeelesh Gupta ret = opal_get_sys_param(attr->param_id, attr->param_size, 1184029cd66SNeelesh Gupta param_data_buf); 1194029cd66SNeelesh Gupta if (ret) 1204029cd66SNeelesh Gupta goto out; 1214029cd66SNeelesh Gupta 1224029cd66SNeelesh Gupta memcpy(buf, param_data_buf, attr->param_size); 1234029cd66SNeelesh Gupta 124*ba9a32b1SJoel Stanley ret = attr->param_size; 1254029cd66SNeelesh Gupta out: 1264029cd66SNeelesh Gupta mutex_unlock(&opal_sysparam_mutex); 127*ba9a32b1SJoel Stanley return ret; 1284029cd66SNeelesh Gupta } 1294029cd66SNeelesh Gupta 1304029cd66SNeelesh Gupta static ssize_t sys_param_store(struct kobject *kobj, 1314029cd66SNeelesh Gupta struct kobj_attribute *kobj_attr, const char *buf, size_t count) 1324029cd66SNeelesh Gupta { 1334029cd66SNeelesh Gupta struct param_attr *attr = container_of(kobj_attr, struct param_attr, 1344029cd66SNeelesh Gupta kobj_attr); 1354029cd66SNeelesh Gupta int ret; 1364029cd66SNeelesh Gupta 1374029cd66SNeelesh Gupta mutex_lock(&opal_sysparam_mutex); 1384029cd66SNeelesh Gupta memcpy(param_data_buf, buf, count); 1394029cd66SNeelesh Gupta ret = opal_set_sys_param(attr->param_id, attr->param_size, 1404029cd66SNeelesh Gupta param_data_buf); 1414029cd66SNeelesh Gupta mutex_unlock(&opal_sysparam_mutex); 142*ba9a32b1SJoel Stanley if (!ret) 143*ba9a32b1SJoel Stanley ret = count; 144*ba9a32b1SJoel Stanley return ret; 1454029cd66SNeelesh Gupta } 1464029cd66SNeelesh Gupta 1474029cd66SNeelesh Gupta void __init opal_sys_param_init(void) 1484029cd66SNeelesh Gupta { 1494029cd66SNeelesh Gupta struct device_node *sysparam; 1504029cd66SNeelesh Gupta struct param_attr *attr; 1514029cd66SNeelesh Gupta u32 *id, *size; 1524029cd66SNeelesh Gupta int count, i; 1534029cd66SNeelesh Gupta u8 *perm; 1544029cd66SNeelesh Gupta 1554029cd66SNeelesh Gupta if (!opal_kobj) { 1564029cd66SNeelesh Gupta pr_warn("SYSPARAM: opal kobject is not available\n"); 1574029cd66SNeelesh Gupta goto out; 1584029cd66SNeelesh Gupta } 1594029cd66SNeelesh Gupta 1604029cd66SNeelesh Gupta sysparam_kobj = kobject_create_and_add("sysparams", opal_kobj); 1614029cd66SNeelesh Gupta if (!sysparam_kobj) { 1624029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to create sysparam kobject\n"); 1634029cd66SNeelesh Gupta goto out; 1644029cd66SNeelesh Gupta } 1654029cd66SNeelesh Gupta 1664029cd66SNeelesh Gupta /* Allocate big enough buffer for any get/set transactions */ 1674029cd66SNeelesh Gupta param_data_buf = kzalloc(MAX_PARAM_DATA_LEN, GFP_KERNEL); 1684029cd66SNeelesh Gupta if (!param_data_buf) { 1694029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory for param data " 1704029cd66SNeelesh Gupta "buf\n"); 1714029cd66SNeelesh Gupta goto out_kobj_put; 1724029cd66SNeelesh Gupta } 1734029cd66SNeelesh Gupta 1744029cd66SNeelesh Gupta sysparam = of_find_node_by_path("/ibm,opal/sysparams"); 1754029cd66SNeelesh Gupta if (!sysparam) { 1764029cd66SNeelesh Gupta pr_err("SYSPARAM: Opal sysparam node not found\n"); 1774029cd66SNeelesh Gupta goto out_param_buf; 1784029cd66SNeelesh Gupta } 1794029cd66SNeelesh Gupta 1804029cd66SNeelesh Gupta if (!of_device_is_compatible(sysparam, "ibm,opal-sysparams")) { 1814029cd66SNeelesh Gupta pr_err("SYSPARAM: Opal sysparam node not compatible\n"); 1824029cd66SNeelesh Gupta goto out_node_put; 1834029cd66SNeelesh Gupta } 1844029cd66SNeelesh Gupta 1854029cd66SNeelesh Gupta /* Number of parameters exposed through DT */ 1864029cd66SNeelesh Gupta count = of_property_count_strings(sysparam, "param-name"); 1874029cd66SNeelesh Gupta if (count < 0) { 1884029cd66SNeelesh Gupta pr_err("SYSPARAM: No string found of property param-name in " 1894029cd66SNeelesh Gupta "the node %s\n", sysparam->name); 1904029cd66SNeelesh Gupta goto out_node_put; 1914029cd66SNeelesh Gupta } 1924029cd66SNeelesh Gupta 1934029cd66SNeelesh Gupta id = kzalloc(sizeof(*id) * count, GFP_KERNEL); 1944029cd66SNeelesh Gupta if (!id) { 1954029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read parameter " 1964029cd66SNeelesh Gupta "id\n"); 1974029cd66SNeelesh Gupta goto out_node_put; 1984029cd66SNeelesh Gupta } 1994029cd66SNeelesh Gupta 2004029cd66SNeelesh Gupta size = kzalloc(sizeof(*size) * count, GFP_KERNEL); 2014029cd66SNeelesh Gupta if (!size) { 2024029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read parameter " 2034029cd66SNeelesh Gupta "size\n"); 2044029cd66SNeelesh Gupta goto out_free_id; 2054029cd66SNeelesh Gupta } 2064029cd66SNeelesh Gupta 2074029cd66SNeelesh Gupta perm = kzalloc(sizeof(*perm) * count, GFP_KERNEL); 2084029cd66SNeelesh Gupta if (!perm) { 2094029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read supported " 2104029cd66SNeelesh Gupta "action on the parameter"); 2114029cd66SNeelesh Gupta goto out_free_size; 2124029cd66SNeelesh Gupta } 2134029cd66SNeelesh Gupta 2144029cd66SNeelesh Gupta if (of_property_read_u32_array(sysparam, "param-id", id, count)) { 2154029cd66SNeelesh Gupta pr_err("SYSPARAM: Missing property param-id in the DT\n"); 2164029cd66SNeelesh Gupta goto out_free_perm; 2174029cd66SNeelesh Gupta } 2184029cd66SNeelesh Gupta 2194029cd66SNeelesh Gupta if (of_property_read_u32_array(sysparam, "param-len", size, count)) { 2204029cd66SNeelesh Gupta pr_err("SYSPARAM: Missing propery param-len in the DT\n"); 2214029cd66SNeelesh Gupta goto out_free_perm; 2224029cd66SNeelesh Gupta } 2234029cd66SNeelesh Gupta 2244029cd66SNeelesh Gupta 2254029cd66SNeelesh Gupta if (of_property_read_u8_array(sysparam, "param-perm", perm, count)) { 2264029cd66SNeelesh Gupta pr_err("SYSPARAM: Missing propery param-perm in the DT\n"); 2274029cd66SNeelesh Gupta goto out_free_perm; 2284029cd66SNeelesh Gupta } 2294029cd66SNeelesh Gupta 2304029cd66SNeelesh Gupta attr = kzalloc(sizeof(*attr) * count, GFP_KERNEL); 2314029cd66SNeelesh Gupta if (!attr) { 2324029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory for parameter " 2334029cd66SNeelesh Gupta "attributes\n"); 2344029cd66SNeelesh Gupta goto out_free_perm; 2354029cd66SNeelesh Gupta } 2364029cd66SNeelesh Gupta 2374029cd66SNeelesh Gupta /* For each of the parameters, populate the parameter attributes */ 2384029cd66SNeelesh Gupta for (i = 0; i < count; i++) { 2394029cd66SNeelesh Gupta sysfs_attr_init(&attr[i].kobj_attr.attr); 2404029cd66SNeelesh Gupta attr[i].param_id = id[i]; 2414029cd66SNeelesh Gupta attr[i].param_size = size[i]; 2424029cd66SNeelesh Gupta if (of_property_read_string_index(sysparam, "param-name", i, 2434029cd66SNeelesh Gupta &attr[i].kobj_attr.attr.name)) 2444029cd66SNeelesh Gupta continue; 2454029cd66SNeelesh Gupta 2464029cd66SNeelesh Gupta /* If the parameter is read-only or read-write */ 2474029cd66SNeelesh Gupta switch (perm[i] & 3) { 2484029cd66SNeelesh Gupta case OPAL_SYSPARAM_READ: 2494029cd66SNeelesh Gupta attr[i].kobj_attr.attr.mode = S_IRUGO; 2504029cd66SNeelesh Gupta break; 2514029cd66SNeelesh Gupta case OPAL_SYSPARAM_WRITE: 2524029cd66SNeelesh Gupta attr[i].kobj_attr.attr.mode = S_IWUGO; 2534029cd66SNeelesh Gupta break; 2544029cd66SNeelesh Gupta case OPAL_SYSPARAM_RW: 2554029cd66SNeelesh Gupta attr[i].kobj_attr.attr.mode = S_IRUGO | S_IWUGO; 2564029cd66SNeelesh Gupta break; 2574029cd66SNeelesh Gupta default: 2584029cd66SNeelesh Gupta break; 2594029cd66SNeelesh Gupta } 2604029cd66SNeelesh Gupta 2614029cd66SNeelesh Gupta attr[i].kobj_attr.show = sys_param_show; 2624029cd66SNeelesh Gupta attr[i].kobj_attr.store = sys_param_store; 2634029cd66SNeelesh Gupta 2644029cd66SNeelesh Gupta if (sysfs_create_file(sysparam_kobj, &attr[i].kobj_attr.attr)) { 2654029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to create sysfs file %s\n", 2664029cd66SNeelesh Gupta attr[i].kobj_attr.attr.name); 2674029cd66SNeelesh Gupta goto out_free_attr; 2684029cd66SNeelesh Gupta } 2694029cd66SNeelesh Gupta } 2704029cd66SNeelesh Gupta 2714029cd66SNeelesh Gupta kfree(perm); 2724029cd66SNeelesh Gupta kfree(size); 2734029cd66SNeelesh Gupta kfree(id); 2744029cd66SNeelesh Gupta of_node_put(sysparam); 2754029cd66SNeelesh Gupta return; 2764029cd66SNeelesh Gupta 2774029cd66SNeelesh Gupta out_free_attr: 2784029cd66SNeelesh Gupta kfree(attr); 2794029cd66SNeelesh Gupta out_free_perm: 2804029cd66SNeelesh Gupta kfree(perm); 2814029cd66SNeelesh Gupta out_free_size: 2824029cd66SNeelesh Gupta kfree(size); 2834029cd66SNeelesh Gupta out_free_id: 2844029cd66SNeelesh Gupta kfree(id); 2854029cd66SNeelesh Gupta out_node_put: 2864029cd66SNeelesh Gupta of_node_put(sysparam); 2874029cd66SNeelesh Gupta out_param_buf: 2884029cd66SNeelesh Gupta kfree(param_data_buf); 2894029cd66SNeelesh Gupta out_kobj_put: 2904029cd66SNeelesh Gupta kobject_put(sysparam_kobj); 2914029cd66SNeelesh Gupta out: 2924029cd66SNeelesh Gupta return; 2934029cd66SNeelesh Gupta } 294