1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #define pr_fmt(fmt)	"papr-sysparm: " fmt
4 
5 #include <linux/bug.h>
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/printk.h>
9 #include <linux/slab.h>
10 #include <asm/rtas.h>
11 #include <asm/papr-sysparm.h>
12 #include <asm/rtas-work-area.h>
13 
papr_sysparm_buf_alloc(void)14 struct papr_sysparm_buf *papr_sysparm_buf_alloc(void)
15 {
16 	struct papr_sysparm_buf *buf = kzalloc(sizeof(*buf), GFP_KERNEL);
17 
18 	return buf;
19 }
20 
papr_sysparm_buf_free(struct papr_sysparm_buf * buf)21 void papr_sysparm_buf_free(struct papr_sysparm_buf *buf)
22 {
23 	kfree(buf);
24 }
25 
26 /**
27  * papr_sysparm_get() - Retrieve the value of a PAPR system parameter.
28  * @param: PAPR system parameter token as described in
29  *         7.3.16 "System Parameters Option".
30  * @buf: A &struct papr_sysparm_buf as returned from papr_sysparm_buf_alloc().
31  *
32  * Place the result of querying the specified parameter, if available,
33  * in @buf. The result includes a be16 length header followed by the
34  * value, which may be a string or binary data. See &struct papr_sysparm_buf.
35  *
36  * Since there is at least one parameter (60, OS Service Entitlement
37  * Status) where the results depend on the incoming contents of the
38  * work area, the caller-supplied buffer is copied unmodified into the
39  * work area before calling ibm,get-system-parameter.
40  *
41  * A defined parameter may not be implemented on a given system, and
42  * some implemented parameters may not be available to all partitions
43  * on a system. A parameter's disposition may change at any time due
44  * to system configuration changes or partition migration.
45  *
46  * Context: This function may sleep.
47  *
48  * Return: 0 on success, -errno otherwise. @buf is unmodified on error.
49  */
50 
papr_sysparm_get(papr_sysparm_t param,struct papr_sysparm_buf * buf)51 int papr_sysparm_get(papr_sysparm_t param, struct papr_sysparm_buf *buf)
52 {
53 	const s32 token = rtas_function_token(RTAS_FN_IBM_GET_SYSTEM_PARAMETER);
54 	struct rtas_work_area *work_area;
55 	s32 fwrc;
56 	int ret;
57 
58 	might_sleep();
59 
60 	if (WARN_ON(!buf))
61 		return -EFAULT;
62 
63 	if (token == RTAS_UNKNOWN_SERVICE)
64 		return -ENOENT;
65 
66 	work_area = rtas_work_area_alloc(sizeof(*buf));
67 
68 	memcpy(rtas_work_area_raw_buf(work_area), buf, sizeof(*buf));
69 
70 	do {
71 		fwrc = rtas_call(token, 3, 1, NULL, param.token,
72 				 rtas_work_area_phys(work_area),
73 				 rtas_work_area_size(work_area));
74 	} while (rtas_busy_delay(fwrc));
75 
76 	switch (fwrc) {
77 	case 0:
78 		ret = 0;
79 		memcpy(buf, rtas_work_area_raw_buf(work_area), sizeof(*buf));
80 		break;
81 	case -3: /* parameter not implemented */
82 		ret = -EOPNOTSUPP;
83 		break;
84 	case -9002: /* this partition not authorized to retrieve this parameter */
85 		ret = -EPERM;
86 		break;
87 	case -9999: /* "parameter error" e.g. the buffer is too small */
88 		ret = -EINVAL;
89 		break;
90 	default:
91 		pr_err("unexpected ibm,get-system-parameter result %d\n", fwrc);
92 		fallthrough;
93 	case -1: /* Hardware/platform error */
94 		ret = -EIO;
95 		break;
96 	}
97 
98 	rtas_work_area_free(work_area);
99 
100 	return ret;
101 }
102 
papr_sysparm_set(papr_sysparm_t param,const struct papr_sysparm_buf * buf)103 int papr_sysparm_set(papr_sysparm_t param, const struct papr_sysparm_buf *buf)
104 {
105 	const s32 token = rtas_function_token(RTAS_FN_IBM_SET_SYSTEM_PARAMETER);
106 	struct rtas_work_area *work_area;
107 	s32 fwrc;
108 	int ret;
109 
110 	might_sleep();
111 
112 	if (WARN_ON(!buf))
113 		return -EFAULT;
114 
115 	if (token == RTAS_UNKNOWN_SERVICE)
116 		return -ENOENT;
117 
118 	work_area = rtas_work_area_alloc(sizeof(*buf));
119 
120 	memcpy(rtas_work_area_raw_buf(work_area), buf, sizeof(*buf));
121 
122 	do {
123 		fwrc = rtas_call(token, 2, 1, NULL, param.token,
124 				 rtas_work_area_phys(work_area));
125 	} while (rtas_busy_delay(fwrc));
126 
127 	switch (fwrc) {
128 	case 0:
129 		ret = 0;
130 		break;
131 	case -3: /* parameter not supported */
132 		ret = -EOPNOTSUPP;
133 		break;
134 	case -9002: /* this partition not authorized to modify this parameter */
135 		ret = -EPERM;
136 		break;
137 	case -9999: /* "parameter error" e.g. invalid input data */
138 		ret = -EINVAL;
139 		break;
140 	default:
141 		pr_err("unexpected ibm,set-system-parameter result %d\n", fwrc);
142 		fallthrough;
143 	case -1: /* Hardware/platform error */
144 		ret = -EIO;
145 		break;
146 	}
147 
148 	rtas_work_area_free(work_area);
149 
150 	return ret;
151 }
152