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); 5814aae78fSCédric Le Goater if (ret != OPAL_ASYNC_COMPLETION) { 5914aae78fSCédric Le Goater ret = opal_error_code(ret); 604029cd66SNeelesh Gupta goto out_token; 6114aae78fSCédric Le Goater } 624029cd66SNeelesh Gupta 634029cd66SNeelesh Gupta ret = opal_async_wait_response(token, &msg); 644029cd66SNeelesh Gupta if (ret) { 65b8569d23SJoel Stanley pr_err("%s: Failed to wait for the async response, %zd\n", 664029cd66SNeelesh Gupta __func__, ret); 674029cd66SNeelesh Gupta goto out_token; 684029cd66SNeelesh Gupta } 694029cd66SNeelesh Gupta 70d0226d31SSuraj Jitindar Singh ret = opal_error_code(opal_get_async_rc(msg)); 714029cd66SNeelesh Gupta 724029cd66SNeelesh Gupta out_token: 734029cd66SNeelesh Gupta opal_async_release_token(token); 744029cd66SNeelesh Gupta out: 754029cd66SNeelesh Gupta return ret; 764029cd66SNeelesh Gupta } 774029cd66SNeelesh Gupta 784029cd66SNeelesh Gupta static int opal_set_sys_param(u32 param_id, u32 length, void *buffer) 794029cd66SNeelesh Gupta { 804029cd66SNeelesh Gupta struct opal_msg msg; 814029cd66SNeelesh Gupta int ret, token; 824029cd66SNeelesh Gupta 834029cd66SNeelesh Gupta token = opal_async_get_token_interruptible(); 844029cd66SNeelesh Gupta if (token < 0) { 854029cd66SNeelesh Gupta if (token != -ERESTARTSYS) 864029cd66SNeelesh Gupta pr_err("%s: Couldn't get the token, returning\n", 874029cd66SNeelesh Gupta __func__); 884029cd66SNeelesh Gupta ret = token; 894029cd66SNeelesh Gupta goto out; 904029cd66SNeelesh Gupta } 914029cd66SNeelesh Gupta 924029cd66SNeelesh Gupta ret = opal_set_param(token, param_id, (u64)buffer, length); 934029cd66SNeelesh Gupta 9414aae78fSCédric Le Goater if (ret != OPAL_ASYNC_COMPLETION) { 9514aae78fSCédric Le Goater ret = opal_error_code(ret); 964029cd66SNeelesh Gupta goto out_token; 9714aae78fSCédric Le Goater } 984029cd66SNeelesh Gupta 994029cd66SNeelesh Gupta ret = opal_async_wait_response(token, &msg); 1004029cd66SNeelesh Gupta if (ret) { 1014029cd66SNeelesh Gupta pr_err("%s: Failed to wait for the async response, %d\n", 1024029cd66SNeelesh Gupta __func__, ret); 1034029cd66SNeelesh Gupta goto out_token; 1044029cd66SNeelesh Gupta } 1054029cd66SNeelesh Gupta 106d0226d31SSuraj Jitindar Singh ret = opal_error_code(opal_get_async_rc(msg)); 1074029cd66SNeelesh Gupta 1084029cd66SNeelesh Gupta out_token: 1094029cd66SNeelesh Gupta opal_async_release_token(token); 1104029cd66SNeelesh Gupta out: 1114029cd66SNeelesh Gupta return ret; 1124029cd66SNeelesh Gupta } 1134029cd66SNeelesh Gupta 1144029cd66SNeelesh Gupta static ssize_t sys_param_show(struct kobject *kobj, 1154029cd66SNeelesh Gupta struct kobj_attribute *kobj_attr, char *buf) 1164029cd66SNeelesh Gupta { 1174029cd66SNeelesh Gupta struct param_attr *attr = container_of(kobj_attr, struct param_attr, 1184029cd66SNeelesh Gupta kobj_attr); 119b8569d23SJoel Stanley ssize_t ret; 1204029cd66SNeelesh Gupta 1214029cd66SNeelesh Gupta mutex_lock(&opal_sysparam_mutex); 1224029cd66SNeelesh Gupta ret = opal_get_sys_param(attr->param_id, attr->param_size, 1234029cd66SNeelesh Gupta param_data_buf); 1244029cd66SNeelesh Gupta if (ret) 1254029cd66SNeelesh Gupta goto out; 1264029cd66SNeelesh Gupta 1274029cd66SNeelesh Gupta memcpy(buf, param_data_buf, attr->param_size); 1284029cd66SNeelesh Gupta 129ba9a32b1SJoel Stanley ret = attr->param_size; 1304029cd66SNeelesh Gupta out: 1314029cd66SNeelesh Gupta mutex_unlock(&opal_sysparam_mutex); 132ba9a32b1SJoel Stanley return ret; 1334029cd66SNeelesh Gupta } 1344029cd66SNeelesh Gupta 1354029cd66SNeelesh Gupta static ssize_t sys_param_store(struct kobject *kobj, 1364029cd66SNeelesh Gupta struct kobj_attribute *kobj_attr, const char *buf, size_t count) 1374029cd66SNeelesh Gupta { 1384029cd66SNeelesh Gupta struct param_attr *attr = container_of(kobj_attr, struct param_attr, 1394029cd66SNeelesh Gupta kobj_attr); 140b8569d23SJoel Stanley ssize_t ret; 1414029cd66SNeelesh Gupta 14285390378SJoel Stanley /* MAX_PARAM_DATA_LEN is sizeof(param_data_buf) */ 14385390378SJoel Stanley if (count > MAX_PARAM_DATA_LEN) 14485390378SJoel Stanley count = MAX_PARAM_DATA_LEN; 14585390378SJoel Stanley 1464029cd66SNeelesh Gupta mutex_lock(&opal_sysparam_mutex); 1474029cd66SNeelesh Gupta memcpy(param_data_buf, buf, count); 1484029cd66SNeelesh Gupta ret = opal_set_sys_param(attr->param_id, attr->param_size, 1494029cd66SNeelesh Gupta param_data_buf); 1504029cd66SNeelesh Gupta mutex_unlock(&opal_sysparam_mutex); 151ba9a32b1SJoel Stanley if (!ret) 152ba9a32b1SJoel Stanley ret = count; 153ba9a32b1SJoel Stanley return ret; 1544029cd66SNeelesh Gupta } 1554029cd66SNeelesh Gupta 1564029cd66SNeelesh Gupta void __init opal_sys_param_init(void) 1574029cd66SNeelesh Gupta { 1584029cd66SNeelesh Gupta struct device_node *sysparam; 1594029cd66SNeelesh Gupta struct param_attr *attr; 1604029cd66SNeelesh Gupta u32 *id, *size; 1614029cd66SNeelesh Gupta int count, i; 1624029cd66SNeelesh Gupta u8 *perm; 1634029cd66SNeelesh Gupta 1644029cd66SNeelesh Gupta if (!opal_kobj) { 1654029cd66SNeelesh Gupta pr_warn("SYSPARAM: opal kobject is not available\n"); 1664029cd66SNeelesh Gupta goto out; 1674029cd66SNeelesh Gupta } 1684029cd66SNeelesh Gupta 16938c04887SJoel Stanley /* Some systems do not use sysparams; this is not an error */ 17038c04887SJoel Stanley sysparam = of_find_node_by_path("/ibm,opal/sysparams"); 17138c04887SJoel Stanley if (!sysparam) 17238c04887SJoel Stanley goto out; 17338c04887SJoel Stanley 17438c04887SJoel Stanley if (!of_device_is_compatible(sysparam, "ibm,opal-sysparams")) { 17538c04887SJoel Stanley pr_err("SYSPARAM: Opal sysparam node not compatible\n"); 17638c04887SJoel Stanley goto out_node_put; 17738c04887SJoel Stanley } 17838c04887SJoel Stanley 1794029cd66SNeelesh Gupta sysparam_kobj = kobject_create_and_add("sysparams", opal_kobj); 1804029cd66SNeelesh Gupta if (!sysparam_kobj) { 1814029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to create sysparam kobject\n"); 18238c04887SJoel Stanley goto out_node_put; 1834029cd66SNeelesh Gupta } 1844029cd66SNeelesh Gupta 1854029cd66SNeelesh Gupta /* Allocate big enough buffer for any get/set transactions */ 1864029cd66SNeelesh Gupta param_data_buf = kzalloc(MAX_PARAM_DATA_LEN, GFP_KERNEL); 1874029cd66SNeelesh Gupta if (!param_data_buf) { 1884029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory for param data " 1894029cd66SNeelesh Gupta "buf\n"); 1904029cd66SNeelesh Gupta goto out_kobj_put; 1914029cd66SNeelesh Gupta } 1924029cd66SNeelesh Gupta 1934029cd66SNeelesh Gupta /* Number of parameters exposed through DT */ 1944029cd66SNeelesh Gupta count = of_property_count_strings(sysparam, "param-name"); 1954029cd66SNeelesh Gupta if (count < 0) { 1964029cd66SNeelesh Gupta pr_err("SYSPARAM: No string found of property param-name in " 1974029cd66SNeelesh Gupta "the node %s\n", sysparam->name); 19838c04887SJoel Stanley goto out_param_buf; 1994029cd66SNeelesh Gupta } 2004029cd66SNeelesh Gupta 2014029cd66SNeelesh Gupta id = kzalloc(sizeof(*id) * count, GFP_KERNEL); 2024029cd66SNeelesh Gupta if (!id) { 2034029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read parameter " 2044029cd66SNeelesh Gupta "id\n"); 20538c04887SJoel Stanley goto out_param_buf; 2064029cd66SNeelesh Gupta } 2074029cd66SNeelesh Gupta 2084029cd66SNeelesh Gupta size = kzalloc(sizeof(*size) * count, GFP_KERNEL); 2094029cd66SNeelesh Gupta if (!size) { 2104029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read parameter " 2114029cd66SNeelesh Gupta "size\n"); 2124029cd66SNeelesh Gupta goto out_free_id; 2134029cd66SNeelesh Gupta } 2144029cd66SNeelesh Gupta 2154029cd66SNeelesh Gupta perm = kzalloc(sizeof(*perm) * count, GFP_KERNEL); 2164029cd66SNeelesh Gupta if (!perm) { 2174029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read supported " 2184029cd66SNeelesh Gupta "action on the parameter"); 2194029cd66SNeelesh Gupta goto out_free_size; 2204029cd66SNeelesh Gupta } 2214029cd66SNeelesh Gupta 2224029cd66SNeelesh Gupta if (of_property_read_u32_array(sysparam, "param-id", id, count)) { 2234029cd66SNeelesh Gupta pr_err("SYSPARAM: Missing property param-id in the DT\n"); 2244029cd66SNeelesh Gupta goto out_free_perm; 2254029cd66SNeelesh Gupta } 2264029cd66SNeelesh Gupta 2274029cd66SNeelesh Gupta if (of_property_read_u32_array(sysparam, "param-len", size, count)) { 22816003d23SJoel Stanley pr_err("SYSPARAM: Missing property param-len in the DT\n"); 2294029cd66SNeelesh Gupta goto out_free_perm; 2304029cd66SNeelesh Gupta } 2314029cd66SNeelesh Gupta 2324029cd66SNeelesh Gupta 2334029cd66SNeelesh Gupta if (of_property_read_u8_array(sysparam, "param-perm", perm, count)) { 23416003d23SJoel Stanley pr_err("SYSPARAM: Missing property param-perm in the DT\n"); 2354029cd66SNeelesh Gupta goto out_free_perm; 2364029cd66SNeelesh Gupta } 2374029cd66SNeelesh Gupta 2384029cd66SNeelesh Gupta attr = kzalloc(sizeof(*attr) * count, GFP_KERNEL); 2394029cd66SNeelesh Gupta if (!attr) { 2404029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory for parameter " 2414029cd66SNeelesh Gupta "attributes\n"); 2424029cd66SNeelesh Gupta goto out_free_perm; 2434029cd66SNeelesh Gupta } 2444029cd66SNeelesh Gupta 2454029cd66SNeelesh Gupta /* For each of the parameters, populate the parameter attributes */ 2464029cd66SNeelesh Gupta for (i = 0; i < count; i++) { 24763aecfb2SJoel Stanley if (size[i] > MAX_PARAM_DATA_LEN) { 24863aecfb2SJoel Stanley pr_warn("SYSPARAM: Not creating parameter %d as size " 24963aecfb2SJoel Stanley "exceeds buffer length\n", i); 25063aecfb2SJoel Stanley continue; 25163aecfb2SJoel Stanley } 25263aecfb2SJoel Stanley 2534029cd66SNeelesh Gupta sysfs_attr_init(&attr[i].kobj_attr.attr); 2544029cd66SNeelesh Gupta attr[i].param_id = id[i]; 2554029cd66SNeelesh Gupta attr[i].param_size = size[i]; 2564029cd66SNeelesh Gupta if (of_property_read_string_index(sysparam, "param-name", i, 2574029cd66SNeelesh Gupta &attr[i].kobj_attr.attr.name)) 2584029cd66SNeelesh Gupta continue; 2594029cd66SNeelesh Gupta 2604029cd66SNeelesh Gupta /* If the parameter is read-only or read-write */ 2614029cd66SNeelesh Gupta switch (perm[i] & 3) { 2624029cd66SNeelesh Gupta case OPAL_SYSPARAM_READ: 263*57ad583fSRussell Currey attr[i].kobj_attr.attr.mode = 0444; 2644029cd66SNeelesh Gupta break; 2654029cd66SNeelesh Gupta case OPAL_SYSPARAM_WRITE: 266*57ad583fSRussell Currey attr[i].kobj_attr.attr.mode = 0200; 2674029cd66SNeelesh Gupta break; 2684029cd66SNeelesh Gupta case OPAL_SYSPARAM_RW: 269*57ad583fSRussell Currey attr[i].kobj_attr.attr.mode = 0644; 2704029cd66SNeelesh Gupta break; 2714029cd66SNeelesh Gupta default: 2724029cd66SNeelesh Gupta break; 2734029cd66SNeelesh Gupta } 2744029cd66SNeelesh Gupta 2754029cd66SNeelesh Gupta attr[i].kobj_attr.show = sys_param_show; 2764029cd66SNeelesh Gupta attr[i].kobj_attr.store = sys_param_store; 2774029cd66SNeelesh Gupta 2784029cd66SNeelesh Gupta if (sysfs_create_file(sysparam_kobj, &attr[i].kobj_attr.attr)) { 2794029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to create sysfs file %s\n", 2804029cd66SNeelesh Gupta attr[i].kobj_attr.attr.name); 2814029cd66SNeelesh Gupta goto out_free_attr; 2824029cd66SNeelesh Gupta } 2834029cd66SNeelesh Gupta } 2844029cd66SNeelesh Gupta 2854029cd66SNeelesh Gupta kfree(perm); 2864029cd66SNeelesh Gupta kfree(size); 2874029cd66SNeelesh Gupta kfree(id); 2884029cd66SNeelesh Gupta of_node_put(sysparam); 2894029cd66SNeelesh Gupta return; 2904029cd66SNeelesh Gupta 2914029cd66SNeelesh Gupta out_free_attr: 2924029cd66SNeelesh Gupta kfree(attr); 2934029cd66SNeelesh Gupta out_free_perm: 2944029cd66SNeelesh Gupta kfree(perm); 2954029cd66SNeelesh Gupta out_free_size: 2964029cd66SNeelesh Gupta kfree(size); 2974029cd66SNeelesh Gupta out_free_id: 2984029cd66SNeelesh Gupta kfree(id); 2994029cd66SNeelesh Gupta out_param_buf: 3004029cd66SNeelesh Gupta kfree(param_data_buf); 3014029cd66SNeelesh Gupta out_kobj_put: 3024029cd66SNeelesh Gupta kobject_put(sysparam_kobj); 30338c04887SJoel Stanley out_node_put: 30438c04887SJoel Stanley of_node_put(sysparam); 3054029cd66SNeelesh Gupta out: 3064029cd66SNeelesh Gupta return; 3074029cd66SNeelesh Gupta } 308