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 42b8569d23SJoel Stanley static ssize_t opal_get_sys_param(u32 param_id, u32 length, void *buffer) 434029cd66SNeelesh Gupta { 444029cd66SNeelesh Gupta struct opal_msg msg; 45b8569d23SJoel Stanley ssize_t ret; 46b8569d23SJoel Stanley int token; 474029cd66SNeelesh Gupta 484029cd66SNeelesh Gupta token = opal_async_get_token_interruptible(); 494029cd66SNeelesh Gupta if (token < 0) { 504029cd66SNeelesh Gupta if (token != -ERESTARTSYS) 514029cd66SNeelesh Gupta pr_err("%s: Couldn't get the token, returning\n", 524029cd66SNeelesh Gupta __func__); 534029cd66SNeelesh Gupta ret = token; 544029cd66SNeelesh Gupta goto out; 554029cd66SNeelesh Gupta } 564029cd66SNeelesh Gupta 574029cd66SNeelesh Gupta ret = opal_get_param(token, param_id, (u64)buffer, length); 584029cd66SNeelesh Gupta if (ret != OPAL_ASYNC_COMPLETION) 594029cd66SNeelesh Gupta goto out_token; 604029cd66SNeelesh Gupta 614029cd66SNeelesh Gupta ret = opal_async_wait_response(token, &msg); 624029cd66SNeelesh Gupta if (ret) { 63b8569d23SJoel Stanley pr_err("%s: Failed to wait for the async response, %zd\n", 644029cd66SNeelesh Gupta __func__, ret); 654029cd66SNeelesh Gupta goto out_token; 664029cd66SNeelesh Gupta } 674029cd66SNeelesh Gupta 68bb4398e1SAnton Blanchard ret = be64_to_cpu(msg.params[1]); 694029cd66SNeelesh Gupta 704029cd66SNeelesh Gupta out_token: 714029cd66SNeelesh Gupta opal_async_release_token(token); 724029cd66SNeelesh Gupta out: 734029cd66SNeelesh Gupta return ret; 744029cd66SNeelesh Gupta } 754029cd66SNeelesh Gupta 764029cd66SNeelesh Gupta static int opal_set_sys_param(u32 param_id, u32 length, void *buffer) 774029cd66SNeelesh Gupta { 784029cd66SNeelesh Gupta struct opal_msg msg; 794029cd66SNeelesh Gupta int ret, token; 804029cd66SNeelesh Gupta 814029cd66SNeelesh Gupta token = opal_async_get_token_interruptible(); 824029cd66SNeelesh Gupta if (token < 0) { 834029cd66SNeelesh Gupta if (token != -ERESTARTSYS) 844029cd66SNeelesh Gupta pr_err("%s: Couldn't get the token, returning\n", 854029cd66SNeelesh Gupta __func__); 864029cd66SNeelesh Gupta ret = token; 874029cd66SNeelesh Gupta goto out; 884029cd66SNeelesh Gupta } 894029cd66SNeelesh Gupta 904029cd66SNeelesh Gupta ret = opal_set_param(token, param_id, (u64)buffer, length); 914029cd66SNeelesh Gupta 924029cd66SNeelesh Gupta if (ret != OPAL_ASYNC_COMPLETION) 934029cd66SNeelesh Gupta goto out_token; 944029cd66SNeelesh Gupta 954029cd66SNeelesh Gupta ret = opal_async_wait_response(token, &msg); 964029cd66SNeelesh Gupta if (ret) { 974029cd66SNeelesh Gupta pr_err("%s: Failed to wait for the async response, %d\n", 984029cd66SNeelesh Gupta __func__, ret); 994029cd66SNeelesh Gupta goto out_token; 1004029cd66SNeelesh Gupta } 1014029cd66SNeelesh Gupta 102bb4398e1SAnton Blanchard ret = be64_to_cpu(msg.params[1]); 1034029cd66SNeelesh Gupta 1044029cd66SNeelesh Gupta out_token: 1054029cd66SNeelesh Gupta opal_async_release_token(token); 1064029cd66SNeelesh Gupta out: 1074029cd66SNeelesh Gupta return ret; 1084029cd66SNeelesh Gupta } 1094029cd66SNeelesh Gupta 1104029cd66SNeelesh Gupta static ssize_t sys_param_show(struct kobject *kobj, 1114029cd66SNeelesh Gupta struct kobj_attribute *kobj_attr, char *buf) 1124029cd66SNeelesh Gupta { 1134029cd66SNeelesh Gupta struct param_attr *attr = container_of(kobj_attr, struct param_attr, 1144029cd66SNeelesh Gupta kobj_attr); 115b8569d23SJoel Stanley ssize_t ret; 1164029cd66SNeelesh Gupta 1174029cd66SNeelesh Gupta mutex_lock(&opal_sysparam_mutex); 1184029cd66SNeelesh Gupta ret = opal_get_sys_param(attr->param_id, attr->param_size, 1194029cd66SNeelesh Gupta param_data_buf); 1204029cd66SNeelesh Gupta if (ret) 1214029cd66SNeelesh Gupta goto out; 1224029cd66SNeelesh Gupta 1234029cd66SNeelesh Gupta memcpy(buf, param_data_buf, attr->param_size); 1244029cd66SNeelesh Gupta 125ba9a32b1SJoel Stanley ret = attr->param_size; 1264029cd66SNeelesh Gupta out: 1274029cd66SNeelesh Gupta mutex_unlock(&opal_sysparam_mutex); 128ba9a32b1SJoel Stanley return ret; 1294029cd66SNeelesh Gupta } 1304029cd66SNeelesh Gupta 1314029cd66SNeelesh Gupta static ssize_t sys_param_store(struct kobject *kobj, 1324029cd66SNeelesh Gupta struct kobj_attribute *kobj_attr, const char *buf, size_t count) 1334029cd66SNeelesh Gupta { 1344029cd66SNeelesh Gupta struct param_attr *attr = container_of(kobj_attr, struct param_attr, 1354029cd66SNeelesh Gupta kobj_attr); 136b8569d23SJoel Stanley ssize_t ret; 1374029cd66SNeelesh Gupta 13885390378SJoel Stanley /* MAX_PARAM_DATA_LEN is sizeof(param_data_buf) */ 13985390378SJoel Stanley if (count > MAX_PARAM_DATA_LEN) 14085390378SJoel Stanley count = MAX_PARAM_DATA_LEN; 14185390378SJoel Stanley 1424029cd66SNeelesh Gupta mutex_lock(&opal_sysparam_mutex); 1434029cd66SNeelesh Gupta memcpy(param_data_buf, buf, count); 1444029cd66SNeelesh Gupta ret = opal_set_sys_param(attr->param_id, attr->param_size, 1454029cd66SNeelesh Gupta param_data_buf); 1464029cd66SNeelesh Gupta mutex_unlock(&opal_sysparam_mutex); 147ba9a32b1SJoel Stanley if (!ret) 148ba9a32b1SJoel Stanley ret = count; 149ba9a32b1SJoel Stanley return ret; 1504029cd66SNeelesh Gupta } 1514029cd66SNeelesh Gupta 1524029cd66SNeelesh Gupta void __init opal_sys_param_init(void) 1534029cd66SNeelesh Gupta { 1544029cd66SNeelesh Gupta struct device_node *sysparam; 1554029cd66SNeelesh Gupta struct param_attr *attr; 1564029cd66SNeelesh Gupta u32 *id, *size; 1574029cd66SNeelesh Gupta int count, i; 1584029cd66SNeelesh Gupta u8 *perm; 1594029cd66SNeelesh Gupta 1604029cd66SNeelesh Gupta if (!opal_kobj) { 1614029cd66SNeelesh Gupta pr_warn("SYSPARAM: opal kobject is not available\n"); 1624029cd66SNeelesh Gupta goto out; 1634029cd66SNeelesh Gupta } 1644029cd66SNeelesh Gupta 1654029cd66SNeelesh Gupta sysparam_kobj = kobject_create_and_add("sysparams", opal_kobj); 1664029cd66SNeelesh Gupta if (!sysparam_kobj) { 1674029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to create sysparam kobject\n"); 1684029cd66SNeelesh Gupta goto out; 1694029cd66SNeelesh Gupta } 1704029cd66SNeelesh Gupta 1714029cd66SNeelesh Gupta /* Allocate big enough buffer for any get/set transactions */ 1724029cd66SNeelesh Gupta param_data_buf = kzalloc(MAX_PARAM_DATA_LEN, GFP_KERNEL); 1734029cd66SNeelesh Gupta if (!param_data_buf) { 1744029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory for param data " 1754029cd66SNeelesh Gupta "buf\n"); 1764029cd66SNeelesh Gupta goto out_kobj_put; 1774029cd66SNeelesh Gupta } 1784029cd66SNeelesh Gupta 1794029cd66SNeelesh Gupta sysparam = of_find_node_by_path("/ibm,opal/sysparams"); 1804029cd66SNeelesh Gupta if (!sysparam) { 1814029cd66SNeelesh Gupta pr_err("SYSPARAM: Opal sysparam node not found\n"); 1824029cd66SNeelesh Gupta goto out_param_buf; 1834029cd66SNeelesh Gupta } 1844029cd66SNeelesh Gupta 1854029cd66SNeelesh Gupta if (!of_device_is_compatible(sysparam, "ibm,opal-sysparams")) { 1864029cd66SNeelesh Gupta pr_err("SYSPARAM: Opal sysparam node not compatible\n"); 1874029cd66SNeelesh Gupta goto out_node_put; 1884029cd66SNeelesh Gupta } 1894029cd66SNeelesh Gupta 1904029cd66SNeelesh Gupta /* Number of parameters exposed through DT */ 1914029cd66SNeelesh Gupta count = of_property_count_strings(sysparam, "param-name"); 1924029cd66SNeelesh Gupta if (count < 0) { 1934029cd66SNeelesh Gupta pr_err("SYSPARAM: No string found of property param-name in " 1944029cd66SNeelesh Gupta "the node %s\n", sysparam->name); 1954029cd66SNeelesh Gupta goto out_node_put; 1964029cd66SNeelesh Gupta } 1974029cd66SNeelesh Gupta 1984029cd66SNeelesh Gupta id = kzalloc(sizeof(*id) * count, GFP_KERNEL); 1994029cd66SNeelesh Gupta if (!id) { 2004029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read parameter " 2014029cd66SNeelesh Gupta "id\n"); 2024029cd66SNeelesh Gupta goto out_node_put; 2034029cd66SNeelesh Gupta } 2044029cd66SNeelesh Gupta 2054029cd66SNeelesh Gupta size = kzalloc(sizeof(*size) * count, GFP_KERNEL); 2064029cd66SNeelesh Gupta if (!size) { 2074029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read parameter " 2084029cd66SNeelesh Gupta "size\n"); 2094029cd66SNeelesh Gupta goto out_free_id; 2104029cd66SNeelesh Gupta } 2114029cd66SNeelesh Gupta 2124029cd66SNeelesh Gupta perm = kzalloc(sizeof(*perm) * count, GFP_KERNEL); 2134029cd66SNeelesh Gupta if (!perm) { 2144029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read supported " 2154029cd66SNeelesh Gupta "action on the parameter"); 2164029cd66SNeelesh Gupta goto out_free_size; 2174029cd66SNeelesh Gupta } 2184029cd66SNeelesh Gupta 2194029cd66SNeelesh Gupta if (of_property_read_u32_array(sysparam, "param-id", id, count)) { 2204029cd66SNeelesh Gupta pr_err("SYSPARAM: Missing property param-id in the DT\n"); 2214029cd66SNeelesh Gupta goto out_free_perm; 2224029cd66SNeelesh Gupta } 2234029cd66SNeelesh Gupta 2244029cd66SNeelesh Gupta if (of_property_read_u32_array(sysparam, "param-len", size, count)) { 225*16003d23SJoel Stanley pr_err("SYSPARAM: Missing property param-len in the DT\n"); 2264029cd66SNeelesh Gupta goto out_free_perm; 2274029cd66SNeelesh Gupta } 2284029cd66SNeelesh Gupta 2294029cd66SNeelesh Gupta 2304029cd66SNeelesh Gupta if (of_property_read_u8_array(sysparam, "param-perm", perm, count)) { 231*16003d23SJoel Stanley pr_err("SYSPARAM: Missing property param-perm in the DT\n"); 2324029cd66SNeelesh Gupta goto out_free_perm; 2334029cd66SNeelesh Gupta } 2344029cd66SNeelesh Gupta 2354029cd66SNeelesh Gupta attr = kzalloc(sizeof(*attr) * count, GFP_KERNEL); 2364029cd66SNeelesh Gupta if (!attr) { 2374029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory for parameter " 2384029cd66SNeelesh Gupta "attributes\n"); 2394029cd66SNeelesh Gupta goto out_free_perm; 2404029cd66SNeelesh Gupta } 2414029cd66SNeelesh Gupta 2424029cd66SNeelesh Gupta /* For each of the parameters, populate the parameter attributes */ 2434029cd66SNeelesh Gupta for (i = 0; i < count; i++) { 2444029cd66SNeelesh Gupta sysfs_attr_init(&attr[i].kobj_attr.attr); 2454029cd66SNeelesh Gupta attr[i].param_id = id[i]; 2464029cd66SNeelesh Gupta attr[i].param_size = size[i]; 2474029cd66SNeelesh Gupta if (of_property_read_string_index(sysparam, "param-name", i, 2484029cd66SNeelesh Gupta &attr[i].kobj_attr.attr.name)) 2494029cd66SNeelesh Gupta continue; 2504029cd66SNeelesh Gupta 2514029cd66SNeelesh Gupta /* If the parameter is read-only or read-write */ 2524029cd66SNeelesh Gupta switch (perm[i] & 3) { 2534029cd66SNeelesh Gupta case OPAL_SYSPARAM_READ: 2544029cd66SNeelesh Gupta attr[i].kobj_attr.attr.mode = S_IRUGO; 2554029cd66SNeelesh Gupta break; 2564029cd66SNeelesh Gupta case OPAL_SYSPARAM_WRITE: 2574029cd66SNeelesh Gupta attr[i].kobj_attr.attr.mode = S_IWUGO; 2584029cd66SNeelesh Gupta break; 2594029cd66SNeelesh Gupta case OPAL_SYSPARAM_RW: 2604029cd66SNeelesh Gupta attr[i].kobj_attr.attr.mode = S_IRUGO | S_IWUGO; 2614029cd66SNeelesh Gupta break; 2624029cd66SNeelesh Gupta default: 2634029cd66SNeelesh Gupta break; 2644029cd66SNeelesh Gupta } 2654029cd66SNeelesh Gupta 2664029cd66SNeelesh Gupta attr[i].kobj_attr.show = sys_param_show; 2674029cd66SNeelesh Gupta attr[i].kobj_attr.store = sys_param_store; 2684029cd66SNeelesh Gupta 2694029cd66SNeelesh Gupta if (sysfs_create_file(sysparam_kobj, &attr[i].kobj_attr.attr)) { 2704029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to create sysfs file %s\n", 2714029cd66SNeelesh Gupta attr[i].kobj_attr.attr.name); 2724029cd66SNeelesh Gupta goto out_free_attr; 2734029cd66SNeelesh Gupta } 2744029cd66SNeelesh Gupta } 2754029cd66SNeelesh Gupta 2764029cd66SNeelesh Gupta kfree(perm); 2774029cd66SNeelesh Gupta kfree(size); 2784029cd66SNeelesh Gupta kfree(id); 2794029cd66SNeelesh Gupta of_node_put(sysparam); 2804029cd66SNeelesh Gupta return; 2814029cd66SNeelesh Gupta 2824029cd66SNeelesh Gupta out_free_attr: 2834029cd66SNeelesh Gupta kfree(attr); 2844029cd66SNeelesh Gupta out_free_perm: 2854029cd66SNeelesh Gupta kfree(perm); 2864029cd66SNeelesh Gupta out_free_size: 2874029cd66SNeelesh Gupta kfree(size); 2884029cd66SNeelesh Gupta out_free_id: 2894029cd66SNeelesh Gupta kfree(id); 2904029cd66SNeelesh Gupta out_node_put: 2914029cd66SNeelesh Gupta of_node_put(sysparam); 2924029cd66SNeelesh Gupta out_param_buf: 2934029cd66SNeelesh Gupta kfree(param_data_buf); 2944029cd66SNeelesh Gupta out_kobj_put: 2954029cd66SNeelesh Gupta kobject_put(sysparam_kobj); 2964029cd66SNeelesh Gupta out: 2974029cd66SNeelesh Gupta return; 2984029cd66SNeelesh Gupta } 299