1*1a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 24029cd66SNeelesh Gupta /* 34029cd66SNeelesh Gupta * PowerNV system parameter code 44029cd66SNeelesh Gupta * 54029cd66SNeelesh Gupta * Copyright (C) 2013 IBM 64029cd66SNeelesh Gupta */ 74029cd66SNeelesh Gupta 84029cd66SNeelesh Gupta #include <linux/kobject.h> 94029cd66SNeelesh Gupta #include <linux/mutex.h> 104029cd66SNeelesh Gupta #include <linux/slab.h> 114029cd66SNeelesh Gupta #include <linux/of.h> 124029cd66SNeelesh Gupta #include <linux/gfp.h> 134029cd66SNeelesh Gupta #include <linux/stat.h> 144029cd66SNeelesh Gupta #include <asm/opal.h> 154029cd66SNeelesh Gupta 164029cd66SNeelesh Gupta #define MAX_PARAM_DATA_LEN 64 174029cd66SNeelesh Gupta 184029cd66SNeelesh Gupta static DEFINE_MUTEX(opal_sysparam_mutex); 194029cd66SNeelesh Gupta static struct kobject *sysparam_kobj; 204029cd66SNeelesh Gupta static void *param_data_buf; 214029cd66SNeelesh Gupta 224029cd66SNeelesh Gupta struct param_attr { 234029cd66SNeelesh Gupta struct list_head list; 244029cd66SNeelesh Gupta u32 param_id; 254029cd66SNeelesh Gupta u32 param_size; 264029cd66SNeelesh Gupta struct kobj_attribute kobj_attr; 274029cd66SNeelesh Gupta }; 284029cd66SNeelesh Gupta 29b8569d23SJoel Stanley static ssize_t opal_get_sys_param(u32 param_id, u32 length, void *buffer) 304029cd66SNeelesh Gupta { 314029cd66SNeelesh Gupta struct opal_msg msg; 32b8569d23SJoel Stanley ssize_t ret; 33b8569d23SJoel Stanley int token; 344029cd66SNeelesh Gupta 354029cd66SNeelesh Gupta token = opal_async_get_token_interruptible(); 364029cd66SNeelesh Gupta if (token < 0) { 374029cd66SNeelesh Gupta if (token != -ERESTARTSYS) 384029cd66SNeelesh Gupta pr_err("%s: Couldn't get the token, returning\n", 394029cd66SNeelesh Gupta __func__); 404029cd66SNeelesh Gupta ret = token; 414029cd66SNeelesh Gupta goto out; 424029cd66SNeelesh Gupta } 434029cd66SNeelesh Gupta 444029cd66SNeelesh Gupta ret = opal_get_param(token, param_id, (u64)buffer, length); 4514aae78fSCédric Le Goater if (ret != OPAL_ASYNC_COMPLETION) { 4614aae78fSCédric Le Goater ret = opal_error_code(ret); 474029cd66SNeelesh Gupta goto out_token; 4814aae78fSCédric Le Goater } 494029cd66SNeelesh Gupta 504029cd66SNeelesh Gupta ret = opal_async_wait_response(token, &msg); 514029cd66SNeelesh Gupta if (ret) { 52b8569d23SJoel Stanley pr_err("%s: Failed to wait for the async response, %zd\n", 534029cd66SNeelesh Gupta __func__, ret); 544029cd66SNeelesh Gupta goto out_token; 554029cd66SNeelesh Gupta } 564029cd66SNeelesh Gupta 57d0226d31SSuraj Jitindar Singh ret = opal_error_code(opal_get_async_rc(msg)); 584029cd66SNeelesh Gupta 594029cd66SNeelesh Gupta out_token: 604029cd66SNeelesh Gupta opal_async_release_token(token); 614029cd66SNeelesh Gupta out: 624029cd66SNeelesh Gupta return ret; 634029cd66SNeelesh Gupta } 644029cd66SNeelesh Gupta 654029cd66SNeelesh Gupta static int opal_set_sys_param(u32 param_id, u32 length, void *buffer) 664029cd66SNeelesh Gupta { 674029cd66SNeelesh Gupta struct opal_msg msg; 684029cd66SNeelesh Gupta int ret, token; 694029cd66SNeelesh Gupta 704029cd66SNeelesh Gupta token = opal_async_get_token_interruptible(); 714029cd66SNeelesh Gupta if (token < 0) { 724029cd66SNeelesh Gupta if (token != -ERESTARTSYS) 734029cd66SNeelesh Gupta pr_err("%s: Couldn't get the token, returning\n", 744029cd66SNeelesh Gupta __func__); 754029cd66SNeelesh Gupta ret = token; 764029cd66SNeelesh Gupta goto out; 774029cd66SNeelesh Gupta } 784029cd66SNeelesh Gupta 794029cd66SNeelesh Gupta ret = opal_set_param(token, param_id, (u64)buffer, length); 804029cd66SNeelesh Gupta 8114aae78fSCédric Le Goater if (ret != OPAL_ASYNC_COMPLETION) { 8214aae78fSCédric Le Goater ret = opal_error_code(ret); 834029cd66SNeelesh Gupta goto out_token; 8414aae78fSCédric Le Goater } 854029cd66SNeelesh Gupta 864029cd66SNeelesh Gupta ret = opal_async_wait_response(token, &msg); 874029cd66SNeelesh Gupta if (ret) { 884029cd66SNeelesh Gupta pr_err("%s: Failed to wait for the async response, %d\n", 894029cd66SNeelesh Gupta __func__, ret); 904029cd66SNeelesh Gupta goto out_token; 914029cd66SNeelesh Gupta } 924029cd66SNeelesh Gupta 93d0226d31SSuraj Jitindar Singh ret = opal_error_code(opal_get_async_rc(msg)); 944029cd66SNeelesh Gupta 954029cd66SNeelesh Gupta out_token: 964029cd66SNeelesh Gupta opal_async_release_token(token); 974029cd66SNeelesh Gupta out: 984029cd66SNeelesh Gupta return ret; 994029cd66SNeelesh Gupta } 1004029cd66SNeelesh Gupta 1014029cd66SNeelesh Gupta static ssize_t sys_param_show(struct kobject *kobj, 1024029cd66SNeelesh Gupta struct kobj_attribute *kobj_attr, char *buf) 1034029cd66SNeelesh Gupta { 1044029cd66SNeelesh Gupta struct param_attr *attr = container_of(kobj_attr, struct param_attr, 1054029cd66SNeelesh Gupta kobj_attr); 106b8569d23SJoel Stanley ssize_t ret; 1074029cd66SNeelesh Gupta 1084029cd66SNeelesh Gupta mutex_lock(&opal_sysparam_mutex); 1094029cd66SNeelesh Gupta ret = opal_get_sys_param(attr->param_id, attr->param_size, 1104029cd66SNeelesh Gupta param_data_buf); 1114029cd66SNeelesh Gupta if (ret) 1124029cd66SNeelesh Gupta goto out; 1134029cd66SNeelesh Gupta 1144029cd66SNeelesh Gupta memcpy(buf, param_data_buf, attr->param_size); 1154029cd66SNeelesh Gupta 116ba9a32b1SJoel Stanley ret = attr->param_size; 1174029cd66SNeelesh Gupta out: 1184029cd66SNeelesh Gupta mutex_unlock(&opal_sysparam_mutex); 119ba9a32b1SJoel Stanley return ret; 1204029cd66SNeelesh Gupta } 1214029cd66SNeelesh Gupta 1224029cd66SNeelesh Gupta static ssize_t sys_param_store(struct kobject *kobj, 1234029cd66SNeelesh Gupta struct kobj_attribute *kobj_attr, const char *buf, size_t count) 1244029cd66SNeelesh Gupta { 1254029cd66SNeelesh Gupta struct param_attr *attr = container_of(kobj_attr, struct param_attr, 1264029cd66SNeelesh Gupta kobj_attr); 127b8569d23SJoel Stanley ssize_t ret; 1284029cd66SNeelesh Gupta 12985390378SJoel Stanley /* MAX_PARAM_DATA_LEN is sizeof(param_data_buf) */ 13085390378SJoel Stanley if (count > MAX_PARAM_DATA_LEN) 13185390378SJoel Stanley count = MAX_PARAM_DATA_LEN; 13285390378SJoel Stanley 1334029cd66SNeelesh Gupta mutex_lock(&opal_sysparam_mutex); 1344029cd66SNeelesh Gupta memcpy(param_data_buf, buf, count); 1354029cd66SNeelesh Gupta ret = opal_set_sys_param(attr->param_id, attr->param_size, 1364029cd66SNeelesh Gupta param_data_buf); 1374029cd66SNeelesh Gupta mutex_unlock(&opal_sysparam_mutex); 138ba9a32b1SJoel Stanley if (!ret) 139ba9a32b1SJoel Stanley ret = count; 140ba9a32b1SJoel Stanley return ret; 1414029cd66SNeelesh Gupta } 1424029cd66SNeelesh Gupta 1434029cd66SNeelesh Gupta void __init opal_sys_param_init(void) 1444029cd66SNeelesh Gupta { 1454029cd66SNeelesh Gupta struct device_node *sysparam; 1464029cd66SNeelesh Gupta struct param_attr *attr; 1474029cd66SNeelesh Gupta u32 *id, *size; 1484029cd66SNeelesh Gupta int count, i; 1494029cd66SNeelesh Gupta u8 *perm; 1504029cd66SNeelesh Gupta 1514029cd66SNeelesh Gupta if (!opal_kobj) { 1524029cd66SNeelesh Gupta pr_warn("SYSPARAM: opal kobject is not available\n"); 1534029cd66SNeelesh Gupta goto out; 1544029cd66SNeelesh Gupta } 1554029cd66SNeelesh Gupta 15638c04887SJoel Stanley /* Some systems do not use sysparams; this is not an error */ 15738c04887SJoel Stanley sysparam = of_find_node_by_path("/ibm,opal/sysparams"); 15838c04887SJoel Stanley if (!sysparam) 15938c04887SJoel Stanley goto out; 16038c04887SJoel Stanley 16138c04887SJoel Stanley if (!of_device_is_compatible(sysparam, "ibm,opal-sysparams")) { 16238c04887SJoel Stanley pr_err("SYSPARAM: Opal sysparam node not compatible\n"); 16338c04887SJoel Stanley goto out_node_put; 16438c04887SJoel Stanley } 16538c04887SJoel Stanley 1664029cd66SNeelesh Gupta sysparam_kobj = kobject_create_and_add("sysparams", opal_kobj); 1674029cd66SNeelesh Gupta if (!sysparam_kobj) { 1684029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to create sysparam kobject\n"); 16938c04887SJoel Stanley goto out_node_put; 1704029cd66SNeelesh Gupta } 1714029cd66SNeelesh Gupta 1724029cd66SNeelesh Gupta /* Allocate big enough buffer for any get/set transactions */ 1734029cd66SNeelesh Gupta param_data_buf = kzalloc(MAX_PARAM_DATA_LEN, GFP_KERNEL); 1744029cd66SNeelesh Gupta if (!param_data_buf) { 1754029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory for param data " 1764029cd66SNeelesh Gupta "buf\n"); 1774029cd66SNeelesh Gupta goto out_kobj_put; 1784029cd66SNeelesh Gupta } 1794029cd66SNeelesh Gupta 1804029cd66SNeelesh Gupta /* Number of parameters exposed through DT */ 1814029cd66SNeelesh Gupta count = of_property_count_strings(sysparam, "param-name"); 1824029cd66SNeelesh Gupta if (count < 0) { 1834029cd66SNeelesh Gupta pr_err("SYSPARAM: No string found of property param-name in " 184b9ef7b4bSRob Herring "the node %pOFn\n", sysparam); 18538c04887SJoel Stanley goto out_param_buf; 1864029cd66SNeelesh Gupta } 1874029cd66SNeelesh Gupta 1886396bb22SKees Cook id = kcalloc(count, sizeof(*id), GFP_KERNEL); 1894029cd66SNeelesh Gupta if (!id) { 1904029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read parameter " 1914029cd66SNeelesh Gupta "id\n"); 19238c04887SJoel Stanley goto out_param_buf; 1934029cd66SNeelesh Gupta } 1944029cd66SNeelesh Gupta 1956396bb22SKees Cook size = kcalloc(count, sizeof(*size), GFP_KERNEL); 1964029cd66SNeelesh Gupta if (!size) { 1974029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read parameter " 1984029cd66SNeelesh Gupta "size\n"); 1994029cd66SNeelesh Gupta goto out_free_id; 2004029cd66SNeelesh Gupta } 2014029cd66SNeelesh Gupta 2026396bb22SKees Cook perm = kcalloc(count, sizeof(*perm), GFP_KERNEL); 2034029cd66SNeelesh Gupta if (!perm) { 2044029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory to read supported " 2054029cd66SNeelesh Gupta "action on the parameter"); 2064029cd66SNeelesh Gupta goto out_free_size; 2074029cd66SNeelesh Gupta } 2084029cd66SNeelesh Gupta 2094029cd66SNeelesh Gupta if (of_property_read_u32_array(sysparam, "param-id", id, count)) { 2104029cd66SNeelesh Gupta pr_err("SYSPARAM: Missing property param-id in the DT\n"); 2114029cd66SNeelesh Gupta goto out_free_perm; 2124029cd66SNeelesh Gupta } 2134029cd66SNeelesh Gupta 2144029cd66SNeelesh Gupta if (of_property_read_u32_array(sysparam, "param-len", size, count)) { 21516003d23SJoel Stanley pr_err("SYSPARAM: Missing property param-len in the DT\n"); 2164029cd66SNeelesh Gupta goto out_free_perm; 2174029cd66SNeelesh Gupta } 2184029cd66SNeelesh Gupta 2194029cd66SNeelesh Gupta 2204029cd66SNeelesh Gupta if (of_property_read_u8_array(sysparam, "param-perm", perm, count)) { 22116003d23SJoel Stanley pr_err("SYSPARAM: Missing property param-perm in the DT\n"); 2224029cd66SNeelesh Gupta goto out_free_perm; 2234029cd66SNeelesh Gupta } 2244029cd66SNeelesh Gupta 2256396bb22SKees Cook attr = kcalloc(count, sizeof(*attr), GFP_KERNEL); 2264029cd66SNeelesh Gupta if (!attr) { 2274029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to allocate memory for parameter " 2284029cd66SNeelesh Gupta "attributes\n"); 2294029cd66SNeelesh Gupta goto out_free_perm; 2304029cd66SNeelesh Gupta } 2314029cd66SNeelesh Gupta 2324029cd66SNeelesh Gupta /* For each of the parameters, populate the parameter attributes */ 2334029cd66SNeelesh Gupta for (i = 0; i < count; i++) { 23463aecfb2SJoel Stanley if (size[i] > MAX_PARAM_DATA_LEN) { 23563aecfb2SJoel Stanley pr_warn("SYSPARAM: Not creating parameter %d as size " 23663aecfb2SJoel Stanley "exceeds buffer length\n", i); 23763aecfb2SJoel Stanley continue; 23863aecfb2SJoel Stanley } 23963aecfb2SJoel Stanley 2404029cd66SNeelesh Gupta sysfs_attr_init(&attr[i].kobj_attr.attr); 2414029cd66SNeelesh Gupta attr[i].param_id = id[i]; 2424029cd66SNeelesh Gupta attr[i].param_size = size[i]; 2434029cd66SNeelesh Gupta if (of_property_read_string_index(sysparam, "param-name", i, 2444029cd66SNeelesh Gupta &attr[i].kobj_attr.attr.name)) 2454029cd66SNeelesh Gupta continue; 2464029cd66SNeelesh Gupta 2474029cd66SNeelesh Gupta /* If the parameter is read-only or read-write */ 2484029cd66SNeelesh Gupta switch (perm[i] & 3) { 2494029cd66SNeelesh Gupta case OPAL_SYSPARAM_READ: 25057ad583fSRussell Currey attr[i].kobj_attr.attr.mode = 0444; 2514029cd66SNeelesh Gupta break; 2524029cd66SNeelesh Gupta case OPAL_SYSPARAM_WRITE: 25357ad583fSRussell Currey attr[i].kobj_attr.attr.mode = 0200; 2544029cd66SNeelesh Gupta break; 2554029cd66SNeelesh Gupta case OPAL_SYSPARAM_RW: 25657ad583fSRussell Currey attr[i].kobj_attr.attr.mode = 0644; 2574029cd66SNeelesh Gupta break; 2584029cd66SNeelesh Gupta default: 2594029cd66SNeelesh Gupta break; 2604029cd66SNeelesh Gupta } 2614029cd66SNeelesh Gupta 2624029cd66SNeelesh Gupta attr[i].kobj_attr.show = sys_param_show; 2634029cd66SNeelesh Gupta attr[i].kobj_attr.store = sys_param_store; 2644029cd66SNeelesh Gupta 2654029cd66SNeelesh Gupta if (sysfs_create_file(sysparam_kobj, &attr[i].kobj_attr.attr)) { 2664029cd66SNeelesh Gupta pr_err("SYSPARAM: Failed to create sysfs file %s\n", 2674029cd66SNeelesh Gupta attr[i].kobj_attr.attr.name); 2684029cd66SNeelesh Gupta goto out_free_attr; 2694029cd66SNeelesh Gupta } 2704029cd66SNeelesh Gupta } 2714029cd66SNeelesh Gupta 2724029cd66SNeelesh Gupta kfree(perm); 2734029cd66SNeelesh Gupta kfree(size); 2744029cd66SNeelesh Gupta kfree(id); 2754029cd66SNeelesh Gupta of_node_put(sysparam); 2764029cd66SNeelesh Gupta return; 2774029cd66SNeelesh Gupta 2784029cd66SNeelesh Gupta out_free_attr: 2794029cd66SNeelesh Gupta kfree(attr); 2804029cd66SNeelesh Gupta out_free_perm: 2814029cd66SNeelesh Gupta kfree(perm); 2824029cd66SNeelesh Gupta out_free_size: 2834029cd66SNeelesh Gupta kfree(size); 2844029cd66SNeelesh Gupta out_free_id: 2854029cd66SNeelesh Gupta kfree(id); 2864029cd66SNeelesh Gupta out_param_buf: 2874029cd66SNeelesh Gupta kfree(param_data_buf); 2884029cd66SNeelesh Gupta out_kobj_put: 2894029cd66SNeelesh Gupta kobject_put(sysparam_kobj); 29038c04887SJoel Stanley out_node_put: 29138c04887SJoel Stanley of_node_put(sysparam); 2924029cd66SNeelesh Gupta out: 2934029cd66SNeelesh Gupta return; 2944029cd66SNeelesh Gupta } 295