xref: /openbmc/linux/arch/powerpc/kernel/rtas.c (revision d699090510c3223641a23834b4710e2d4309a6ad)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  * Procedures for interfacing to the RTAS on CHRP machines.
5  *
6  * Peter Bergner, IBM	March 2001.
7  * Copyright (C) 2001 IBM.
8  */
9 
10 #define pr_fmt(fmt)	"rtas: " fmt
11 
12 #include <linux/bsearch.h>
13 #include <linux/capability.h>
14 #include <linux/delay.h>
15 #include <linux/export.h>
16 #include <linux/init.h>
17 #include <linux/kconfig.h>
18 #include <linux/kernel.h>
19 #include <linux/lockdep.h>
20 #include <linux/memblock.h>
21 #include <linux/nospec.h>
22 #include <linux/of.h>
23 #include <linux/of_fdt.h>
24 #include <linux/reboot.h>
25 #include <linux/sched.h>
26 #include <linux/security.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/stdarg.h>
30 #include <linux/syscalls.h>
31 #include <linux/types.h>
32 #include <linux/uaccess.h>
33 #include <linux/xarray.h>
34 
35 #include <asm/delay.h>
36 #include <asm/firmware.h>
37 #include <asm/interrupt.h>
38 #include <asm/machdep.h>
39 #include <asm/mmu.h>
40 #include <asm/page.h>
41 #include <asm/rtas-work-area.h>
42 #include <asm/rtas.h>
43 #include <asm/time.h>
44 #include <asm/trace.h>
45 #include <asm/udbg.h>
46 
47 struct rtas_filter {
48 	/* Indexes into the args buffer, -1 if not used */
49 	const int buf_idx1;
50 	const int size_idx1;
51 	const int buf_idx2;
52 	const int size_idx2;
53 	/*
54 	 * Assumed buffer size per the spec if the function does not
55 	 * have a size parameter, e.g. ibm,errinjct. 0 if unused.
56 	 */
57 	const int fixed_size;
58 };
59 
60 /**
61  * struct rtas_function - Descriptor for RTAS functions.
62  *
63  * @token: Value of @name if it exists under the /rtas node.
64  * @name: Function name.
65  * @filter: If non-NULL, invoking this function via the rtas syscall is
66  *          generally allowed, and @filter describes constraints on the
67  *          arguments. See also @banned_for_syscall_on_le.
68  * @banned_for_syscall_on_le: Set when call via sys_rtas is generally allowed
69  *                            but specifically restricted on ppc64le. Such
70  *                            functions are believed to have no users on
71  *                            ppc64le, and we want to keep it that way. It does
72  *                            not make sense for this to be set when @filter
73  *                            is NULL.
74  */
75 struct rtas_function {
76 	s32 token;
77 	const bool banned_for_syscall_on_le:1;
78 	const char * const name;
79 	const struct rtas_filter *filter;
80 };
81 
82 static struct rtas_function rtas_function_table[] __ro_after_init = {
83 	[RTAS_FNIDX__CHECK_EXCEPTION] = {
84 		.name = "check-exception",
85 	},
86 	[RTAS_FNIDX__DISPLAY_CHARACTER] = {
87 		.name = "display-character",
88 		.filter = &(const struct rtas_filter) {
89 			.buf_idx1 = -1, .size_idx1 = -1,
90 			.buf_idx2 = -1, .size_idx2 = -1,
91 		},
92 	},
93 	[RTAS_FNIDX__EVENT_SCAN] = {
94 		.name = "event-scan",
95 	},
96 	[RTAS_FNIDX__FREEZE_TIME_BASE] = {
97 		.name = "freeze-time-base",
98 	},
99 	[RTAS_FNIDX__GET_POWER_LEVEL] = {
100 		.name = "get-power-level",
101 		.filter = &(const struct rtas_filter) {
102 			.buf_idx1 = -1, .size_idx1 = -1,
103 			.buf_idx2 = -1, .size_idx2 = -1,
104 		},
105 	},
106 	[RTAS_FNIDX__GET_SENSOR_STATE] = {
107 		.name = "get-sensor-state",
108 		.filter = &(const struct rtas_filter) {
109 			.buf_idx1 = -1, .size_idx1 = -1,
110 			.buf_idx2 = -1, .size_idx2 = -1,
111 		},
112 	},
113 	[RTAS_FNIDX__GET_TERM_CHAR] = {
114 		.name = "get-term-char",
115 	},
116 	[RTAS_FNIDX__GET_TIME_OF_DAY] = {
117 		.name = "get-time-of-day",
118 		.filter = &(const struct rtas_filter) {
119 			.buf_idx1 = -1, .size_idx1 = -1,
120 			.buf_idx2 = -1, .size_idx2 = -1,
121 		},
122 	},
123 	[RTAS_FNIDX__IBM_ACTIVATE_FIRMWARE] = {
124 		.name = "ibm,activate-firmware",
125 		.filter = &(const struct rtas_filter) {
126 			.buf_idx1 = -1, .size_idx1 = -1,
127 			.buf_idx2 = -1, .size_idx2 = -1,
128 		},
129 	},
130 	[RTAS_FNIDX__IBM_CBE_START_PTCAL] = {
131 		.name = "ibm,cbe-start-ptcal",
132 	},
133 	[RTAS_FNIDX__IBM_CBE_STOP_PTCAL] = {
134 		.name = "ibm,cbe-stop-ptcal",
135 	},
136 	[RTAS_FNIDX__IBM_CHANGE_MSI] = {
137 		.name = "ibm,change-msi",
138 	},
139 	[RTAS_FNIDX__IBM_CLOSE_ERRINJCT] = {
140 		.name = "ibm,close-errinjct",
141 		.filter = &(const struct rtas_filter) {
142 			.buf_idx1 = -1, .size_idx1 = -1,
143 			.buf_idx2 = -1, .size_idx2 = -1,
144 		},
145 	},
146 	[RTAS_FNIDX__IBM_CONFIGURE_BRIDGE] = {
147 		.name = "ibm,configure-bridge",
148 	},
149 	[RTAS_FNIDX__IBM_CONFIGURE_CONNECTOR] = {
150 		.name = "ibm,configure-connector",
151 		.filter = &(const struct rtas_filter) {
152 			.buf_idx1 = 0, .size_idx1 = -1,
153 			.buf_idx2 = 1, .size_idx2 = -1,
154 			.fixed_size = 4096,
155 		},
156 	},
157 	[RTAS_FNIDX__IBM_CONFIGURE_KERNEL_DUMP] = {
158 		.name = "ibm,configure-kernel-dump",
159 	},
160 	[RTAS_FNIDX__IBM_CONFIGURE_PE] = {
161 		.name = "ibm,configure-pe",
162 	},
163 	[RTAS_FNIDX__IBM_CREATE_PE_DMA_WINDOW] = {
164 		.name = "ibm,create-pe-dma-window",
165 	},
166 	[RTAS_FNIDX__IBM_DISPLAY_MESSAGE] = {
167 		.name = "ibm,display-message",
168 		.filter = &(const struct rtas_filter) {
169 			.buf_idx1 = 0, .size_idx1 = -1,
170 			.buf_idx2 = -1, .size_idx2 = -1,
171 		},
172 	},
173 	[RTAS_FNIDX__IBM_ERRINJCT] = {
174 		.name = "ibm,errinjct",
175 		.filter = &(const struct rtas_filter) {
176 			.buf_idx1 = 2, .size_idx1 = -1,
177 			.buf_idx2 = -1, .size_idx2 = -1,
178 			.fixed_size = 1024,
179 		},
180 	},
181 	[RTAS_FNIDX__IBM_EXTI2C] = {
182 		.name = "ibm,exti2c",
183 	},
184 	[RTAS_FNIDX__IBM_GET_CONFIG_ADDR_INFO] = {
185 		.name = "ibm,get-config-addr-info",
186 	},
187 	[RTAS_FNIDX__IBM_GET_CONFIG_ADDR_INFO2] = {
188 		.name = "ibm,get-config-addr-info2",
189 		.filter = &(const struct rtas_filter) {
190 			.buf_idx1 = -1, .size_idx1 = -1,
191 			.buf_idx2 = -1, .size_idx2 = -1,
192 		},
193 	},
194 	[RTAS_FNIDX__IBM_GET_DYNAMIC_SENSOR_STATE] = {
195 		.name = "ibm,get-dynamic-sensor-state",
196 		.filter = &(const struct rtas_filter) {
197 			.buf_idx1 = 1, .size_idx1 = -1,
198 			.buf_idx2 = -1, .size_idx2 = -1,
199 		},
200 	},
201 	[RTAS_FNIDX__IBM_GET_INDICES] = {
202 		.name = "ibm,get-indices",
203 		.filter = &(const struct rtas_filter) {
204 			.buf_idx1 = 2, .size_idx1 = 3,
205 			.buf_idx2 = -1, .size_idx2 = -1,
206 		},
207 	},
208 	[RTAS_FNIDX__IBM_GET_RIO_TOPOLOGY] = {
209 		.name = "ibm,get-rio-topology",
210 	},
211 	[RTAS_FNIDX__IBM_GET_SYSTEM_PARAMETER] = {
212 		.name = "ibm,get-system-parameter",
213 		.filter = &(const struct rtas_filter) {
214 			.buf_idx1 = 1, .size_idx1 = 2,
215 			.buf_idx2 = -1, .size_idx2 = -1,
216 		},
217 	},
218 	[RTAS_FNIDX__IBM_GET_VPD] = {
219 		.name = "ibm,get-vpd",
220 		.filter = &(const struct rtas_filter) {
221 			.buf_idx1 = 0, .size_idx1 = -1,
222 			.buf_idx2 = 1, .size_idx2 = 2,
223 		},
224 	},
225 	[RTAS_FNIDX__IBM_GET_XIVE] = {
226 		.name = "ibm,get-xive",
227 	},
228 	[RTAS_FNIDX__IBM_INT_OFF] = {
229 		.name = "ibm,int-off",
230 	},
231 	[RTAS_FNIDX__IBM_INT_ON] = {
232 		.name = "ibm,int-on",
233 	},
234 	[RTAS_FNIDX__IBM_IO_QUIESCE_ACK] = {
235 		.name = "ibm,io-quiesce-ack",
236 	},
237 	[RTAS_FNIDX__IBM_LPAR_PERFTOOLS] = {
238 		.name = "ibm,lpar-perftools",
239 		.filter = &(const struct rtas_filter) {
240 			.buf_idx1 = 2, .size_idx1 = 3,
241 			.buf_idx2 = -1, .size_idx2 = -1,
242 		},
243 	},
244 	[RTAS_FNIDX__IBM_MANAGE_FLASH_IMAGE] = {
245 		.name = "ibm,manage-flash-image",
246 	},
247 	[RTAS_FNIDX__IBM_MANAGE_STORAGE_PRESERVATION] = {
248 		.name = "ibm,manage-storage-preservation",
249 	},
250 	[RTAS_FNIDX__IBM_NMI_INTERLOCK] = {
251 		.name = "ibm,nmi-interlock",
252 	},
253 	[RTAS_FNIDX__IBM_NMI_REGISTER] = {
254 		.name = "ibm,nmi-register",
255 	},
256 	[RTAS_FNIDX__IBM_OPEN_ERRINJCT] = {
257 		.name = "ibm,open-errinjct",
258 		.filter = &(const struct rtas_filter) {
259 			.buf_idx1 = -1, .size_idx1 = -1,
260 			.buf_idx2 = -1, .size_idx2 = -1,
261 		},
262 	},
263 	[RTAS_FNIDX__IBM_OPEN_SRIOV_ALLOW_UNFREEZE] = {
264 		.name = "ibm,open-sriov-allow-unfreeze",
265 	},
266 	[RTAS_FNIDX__IBM_OPEN_SRIOV_MAP_PE_NUMBER] = {
267 		.name = "ibm,open-sriov-map-pe-number",
268 	},
269 	[RTAS_FNIDX__IBM_OS_TERM] = {
270 		.name = "ibm,os-term",
271 	},
272 	[RTAS_FNIDX__IBM_PARTNER_CONTROL] = {
273 		.name = "ibm,partner-control",
274 	},
275 	[RTAS_FNIDX__IBM_PHYSICAL_ATTESTATION] = {
276 		.name = "ibm,physical-attestation",
277 		.filter = &(const struct rtas_filter) {
278 			.buf_idx1 = 0, .size_idx1 = 1,
279 			.buf_idx2 = -1, .size_idx2 = -1,
280 		},
281 	},
282 	[RTAS_FNIDX__IBM_PLATFORM_DUMP] = {
283 		.name = "ibm,platform-dump",
284 		.filter = &(const struct rtas_filter) {
285 			.buf_idx1 = 4, .size_idx1 = 5,
286 			.buf_idx2 = -1, .size_idx2 = -1,
287 		},
288 	},
289 	[RTAS_FNIDX__IBM_POWER_OFF_UPS] = {
290 		.name = "ibm,power-off-ups",
291 	},
292 	[RTAS_FNIDX__IBM_QUERY_INTERRUPT_SOURCE_NUMBER] = {
293 		.name = "ibm,query-interrupt-source-number",
294 	},
295 	[RTAS_FNIDX__IBM_QUERY_PE_DMA_WINDOW] = {
296 		.name = "ibm,query-pe-dma-window",
297 	},
298 	[RTAS_FNIDX__IBM_READ_PCI_CONFIG] = {
299 		.name = "ibm,read-pci-config",
300 	},
301 	[RTAS_FNIDX__IBM_READ_SLOT_RESET_STATE] = {
302 		.name = "ibm,read-slot-reset-state",
303 		.filter = &(const struct rtas_filter) {
304 			.buf_idx1 = -1, .size_idx1 = -1,
305 			.buf_idx2 = -1, .size_idx2 = -1,
306 		},
307 	},
308 	[RTAS_FNIDX__IBM_READ_SLOT_RESET_STATE2] = {
309 		.name = "ibm,read-slot-reset-state2",
310 	},
311 	[RTAS_FNIDX__IBM_REMOVE_PE_DMA_WINDOW] = {
312 		.name = "ibm,remove-pe-dma-window",
313 	},
314 	[RTAS_FNIDX__IBM_RESET_PE_DMA_WINDOW] = {
315 		/*
316 		 * Note: PAPR+ v2.13 7.3.31.4.1 spells this as
317 		 * "ibm,reset-pe-dma-windows" (plural), but RTAS
318 		 * implementations use the singular form in practice.
319 		 */
320 		.name = "ibm,reset-pe-dma-window",
321 	},
322 	[RTAS_FNIDX__IBM_SCAN_LOG_DUMP] = {
323 		.name = "ibm,scan-log-dump",
324 		.filter = &(const struct rtas_filter) {
325 			.buf_idx1 = 0, .size_idx1 = 1,
326 			.buf_idx2 = -1, .size_idx2 = -1,
327 		},
328 	},
329 	[RTAS_FNIDX__IBM_SET_DYNAMIC_INDICATOR] = {
330 		.name = "ibm,set-dynamic-indicator",
331 		.filter = &(const struct rtas_filter) {
332 			.buf_idx1 = 2, .size_idx1 = -1,
333 			.buf_idx2 = -1, .size_idx2 = -1,
334 		},
335 	},
336 	[RTAS_FNIDX__IBM_SET_EEH_OPTION] = {
337 		.name = "ibm,set-eeh-option",
338 		.filter = &(const struct rtas_filter) {
339 			.buf_idx1 = -1, .size_idx1 = -1,
340 			.buf_idx2 = -1, .size_idx2 = -1,
341 		},
342 	},
343 	[RTAS_FNIDX__IBM_SET_SLOT_RESET] = {
344 		.name = "ibm,set-slot-reset",
345 	},
346 	[RTAS_FNIDX__IBM_SET_SYSTEM_PARAMETER] = {
347 		.name = "ibm,set-system-parameter",
348 		.filter = &(const struct rtas_filter) {
349 			.buf_idx1 = 1, .size_idx1 = -1,
350 			.buf_idx2 = -1, .size_idx2 = -1,
351 		},
352 	},
353 	[RTAS_FNIDX__IBM_SET_XIVE] = {
354 		.name = "ibm,set-xive",
355 	},
356 	[RTAS_FNIDX__IBM_SLOT_ERROR_DETAIL] = {
357 		.name = "ibm,slot-error-detail",
358 	},
359 	[RTAS_FNIDX__IBM_SUSPEND_ME] = {
360 		.name = "ibm,suspend-me",
361 		.banned_for_syscall_on_le = true,
362 		.filter = &(const struct rtas_filter) {
363 			.buf_idx1 = -1, .size_idx1 = -1,
364 			.buf_idx2 = -1, .size_idx2 = -1,
365 		},
366 	},
367 	[RTAS_FNIDX__IBM_TUNE_DMA_PARMS] = {
368 		.name = "ibm,tune-dma-parms",
369 	},
370 	[RTAS_FNIDX__IBM_UPDATE_FLASH_64_AND_REBOOT] = {
371 		.name = "ibm,update-flash-64-and-reboot",
372 	},
373 	[RTAS_FNIDX__IBM_UPDATE_NODES] = {
374 		.name = "ibm,update-nodes",
375 		.banned_for_syscall_on_le = true,
376 		.filter = &(const struct rtas_filter) {
377 			.buf_idx1 = 0, .size_idx1 = -1,
378 			.buf_idx2 = -1, .size_idx2 = -1,
379 			.fixed_size = 4096,
380 		},
381 	},
382 	[RTAS_FNIDX__IBM_UPDATE_PROPERTIES] = {
383 		.name = "ibm,update-properties",
384 		.banned_for_syscall_on_le = true,
385 		.filter = &(const struct rtas_filter) {
386 			.buf_idx1 = 0, .size_idx1 = -1,
387 			.buf_idx2 = -1, .size_idx2 = -1,
388 			.fixed_size = 4096,
389 		},
390 	},
391 	[RTAS_FNIDX__IBM_VALIDATE_FLASH_IMAGE] = {
392 		.name = "ibm,validate-flash-image",
393 	},
394 	[RTAS_FNIDX__IBM_WRITE_PCI_CONFIG] = {
395 		.name = "ibm,write-pci-config",
396 	},
397 	[RTAS_FNIDX__NVRAM_FETCH] = {
398 		.name = "nvram-fetch",
399 	},
400 	[RTAS_FNIDX__NVRAM_STORE] = {
401 		.name = "nvram-store",
402 	},
403 	[RTAS_FNIDX__POWER_OFF] = {
404 		.name = "power-off",
405 	},
406 	[RTAS_FNIDX__PUT_TERM_CHAR] = {
407 		.name = "put-term-char",
408 	},
409 	[RTAS_FNIDX__QUERY_CPU_STOPPED_STATE] = {
410 		.name = "query-cpu-stopped-state",
411 	},
412 	[RTAS_FNIDX__READ_PCI_CONFIG] = {
413 		.name = "read-pci-config",
414 	},
415 	[RTAS_FNIDX__RTAS_LAST_ERROR] = {
416 		.name = "rtas-last-error",
417 	},
418 	[RTAS_FNIDX__SET_INDICATOR] = {
419 		.name = "set-indicator",
420 		.filter = &(const struct rtas_filter) {
421 			.buf_idx1 = -1, .size_idx1 = -1,
422 			.buf_idx2 = -1, .size_idx2 = -1,
423 		},
424 	},
425 	[RTAS_FNIDX__SET_POWER_LEVEL] = {
426 		.name = "set-power-level",
427 		.filter = &(const struct rtas_filter) {
428 			.buf_idx1 = -1, .size_idx1 = -1,
429 			.buf_idx2 = -1, .size_idx2 = -1,
430 		},
431 	},
432 	[RTAS_FNIDX__SET_TIME_FOR_POWER_ON] = {
433 		.name = "set-time-for-power-on",
434 		.filter = &(const struct rtas_filter) {
435 			.buf_idx1 = -1, .size_idx1 = -1,
436 			.buf_idx2 = -1, .size_idx2 = -1,
437 		},
438 	},
439 	[RTAS_FNIDX__SET_TIME_OF_DAY] = {
440 		.name = "set-time-of-day",
441 		.filter = &(const struct rtas_filter) {
442 			.buf_idx1 = -1, .size_idx1 = -1,
443 			.buf_idx2 = -1, .size_idx2 = -1,
444 		},
445 	},
446 	[RTAS_FNIDX__START_CPU] = {
447 		.name = "start-cpu",
448 	},
449 	[RTAS_FNIDX__STOP_SELF] = {
450 		.name = "stop-self",
451 	},
452 	[RTAS_FNIDX__SYSTEM_REBOOT] = {
453 		.name = "system-reboot",
454 	},
455 	[RTAS_FNIDX__THAW_TIME_BASE] = {
456 		.name = "thaw-time-base",
457 	},
458 	[RTAS_FNIDX__WRITE_PCI_CONFIG] = {
459 		.name = "write-pci-config",
460 	},
461 };
462 
463 /*
464  * Nearly all RTAS calls need to be serialized. All uses of the
465  * default rtas_args block must hold rtas_lock.
466  *
467  * Exceptions to the RTAS serialization requirement (e.g. stop-self)
468  * must use a separate rtas_args structure.
469  */
470 static DEFINE_RAW_SPINLOCK(rtas_lock);
471 static struct rtas_args rtas_args;
472 
473 /**
474  * rtas_function_token() - RTAS function token lookup.
475  * @handle: Function handle, e.g. RTAS_FN_EVENT_SCAN.
476  *
477  * Context: Any context.
478  * Return: the token value for the function if implemented by this platform,
479  *         otherwise RTAS_UNKNOWN_SERVICE.
480  */
rtas_function_token(const rtas_fn_handle_t handle)481 s32 rtas_function_token(const rtas_fn_handle_t handle)
482 {
483 	const size_t index = handle.index;
484 	const bool out_of_bounds = index >= ARRAY_SIZE(rtas_function_table);
485 
486 	if (WARN_ONCE(out_of_bounds, "invalid function index %zu", index))
487 		return RTAS_UNKNOWN_SERVICE;
488 	/*
489 	 * Various drivers attempt token lookups on non-RTAS
490 	 * platforms.
491 	 */
492 	if (!rtas.dev)
493 		return RTAS_UNKNOWN_SERVICE;
494 
495 	return rtas_function_table[index].token;
496 }
497 EXPORT_SYMBOL_GPL(rtas_function_token);
498 
rtas_function_cmp(const void * a,const void * b)499 static int rtas_function_cmp(const void *a, const void *b)
500 {
501 	const struct rtas_function *f1 = a;
502 	const struct rtas_function *f2 = b;
503 
504 	return strcmp(f1->name, f2->name);
505 }
506 
507 /*
508  * Boot-time initialization of the function table needs the lookup to
509  * return a non-const-qualified object. Use rtas_name_to_function()
510  * in all other contexts.
511  */
__rtas_name_to_function(const char * name)512 static struct rtas_function *__rtas_name_to_function(const char *name)
513 {
514 	const struct rtas_function key = {
515 		.name = name,
516 	};
517 	struct rtas_function *found;
518 
519 	found = bsearch(&key, rtas_function_table, ARRAY_SIZE(rtas_function_table),
520 			sizeof(rtas_function_table[0]), rtas_function_cmp);
521 
522 	return found;
523 }
524 
rtas_name_to_function(const char * name)525 static const struct rtas_function *rtas_name_to_function(const char *name)
526 {
527 	return __rtas_name_to_function(name);
528 }
529 
530 static DEFINE_XARRAY(rtas_token_to_function_xarray);
531 
rtas_token_to_function_xarray_init(void)532 static int __init rtas_token_to_function_xarray_init(void)
533 {
534 	int err = 0;
535 
536 	for (size_t i = 0; i < ARRAY_SIZE(rtas_function_table); ++i) {
537 		const struct rtas_function *func = &rtas_function_table[i];
538 		const s32 token = func->token;
539 
540 		if (token == RTAS_UNKNOWN_SERVICE)
541 			continue;
542 
543 		err = xa_err(xa_store(&rtas_token_to_function_xarray,
544 				      token, (void *)func, GFP_KERNEL));
545 		if (err)
546 			break;
547 	}
548 
549 	return err;
550 }
551 arch_initcall(rtas_token_to_function_xarray_init);
552 
553 /*
554  * For use by sys_rtas(), where the token value is provided by user
555  * space and we don't want to warn on failed lookups.
556  */
rtas_token_to_function_untrusted(s32 token)557 static const struct rtas_function *rtas_token_to_function_untrusted(s32 token)
558 {
559 	return xa_load(&rtas_token_to_function_xarray, token);
560 }
561 
562 /*
563  * Reverse lookup for deriving the function descriptor from a
564  * known-good token value in contexts where the former is not already
565  * available. @token must be valid, e.g. derived from the result of a
566  * prior lookup against the function table.
567  */
rtas_token_to_function(s32 token)568 static const struct rtas_function *rtas_token_to_function(s32 token)
569 {
570 	const struct rtas_function *func;
571 
572 	if (WARN_ONCE(token < 0, "invalid token %d", token))
573 		return NULL;
574 
575 	func = rtas_token_to_function_untrusted(token);
576 
577 	if (WARN_ONCE(!func, "unexpected failed lookup for token %d", token))
578 		return NULL;
579 
580 	return func;
581 }
582 
583 /* This is here deliberately so it's only used in this file */
584 void enter_rtas(unsigned long);
585 
__do_enter_rtas(struct rtas_args * args)586 static void __do_enter_rtas(struct rtas_args *args)
587 {
588 	enter_rtas(__pa(args));
589 	srr_regs_clobbered(); /* rtas uses SRRs, invalidate */
590 }
591 
__do_enter_rtas_trace(struct rtas_args * args)592 static void __do_enter_rtas_trace(struct rtas_args *args)
593 {
594 	const char *name = NULL;
595 
596 	if (args == &rtas_args)
597 		lockdep_assert_held(&rtas_lock);
598 	/*
599 	 * If the tracepoints that consume the function name aren't
600 	 * active, avoid the lookup.
601 	 */
602 	if ((trace_rtas_input_enabled() || trace_rtas_output_enabled())) {
603 		const s32 token = be32_to_cpu(args->token);
604 		const struct rtas_function *func = rtas_token_to_function(token);
605 
606 		name = func->name;
607 	}
608 
609 	trace_rtas_input(args, name);
610 	trace_rtas_ll_entry(args);
611 
612 	__do_enter_rtas(args);
613 
614 	trace_rtas_ll_exit(args);
615 	trace_rtas_output(args, name);
616 }
617 
do_enter_rtas(struct rtas_args * args)618 static void do_enter_rtas(struct rtas_args *args)
619 {
620 	const unsigned long msr = mfmsr();
621 	/*
622 	 * Situations where we want to skip any active tracepoints for
623 	 * safety reasons:
624 	 *
625 	 * 1. The last code executed on an offline CPU as it stops,
626 	 *    i.e. we're about to call stop-self. The tracepoints'
627 	 *    function name lookup uses xarray, which uses RCU, which
628 	 *    isn't valid to call on an offline CPU.  Any events
629 	 *    emitted on an offline CPU will be discarded anyway.
630 	 *
631 	 * 2. In real mode, as when invoking ibm,nmi-interlock from
632 	 *    the pseries MCE handler. We cannot count on trace
633 	 *    buffers or the entries in rtas_token_to_function_xarray
634 	 *    to be contained in the RMO.
635 	 */
636 	const unsigned long mask = MSR_IR | MSR_DR;
637 	const bool can_trace = likely(cpu_online(raw_smp_processor_id()) &&
638 				      (msr & mask) == mask);
639 	/*
640 	 * Make sure MSR[RI] is currently enabled as it will be forced later
641 	 * in enter_rtas.
642 	 */
643 	BUG_ON(!(msr & MSR_RI));
644 
645 	BUG_ON(!irqs_disabled());
646 
647 	hard_irq_disable(); /* Ensure MSR[EE] is disabled on PPC64 */
648 
649 	if (can_trace)
650 		__do_enter_rtas_trace(args);
651 	else
652 		__do_enter_rtas(args);
653 }
654 
655 struct rtas_t rtas;
656 
657 DEFINE_SPINLOCK(rtas_data_buf_lock);
658 EXPORT_SYMBOL_GPL(rtas_data_buf_lock);
659 
660 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __aligned(SZ_4K);
661 EXPORT_SYMBOL_GPL(rtas_data_buf);
662 
663 unsigned long rtas_rmo_buf;
664 
665 /*
666  * If non-NULL, this gets called when the kernel terminates.
667  * This is done like this so rtas_flash can be a module.
668  */
669 void (*rtas_flash_term_hook)(int);
670 EXPORT_SYMBOL_GPL(rtas_flash_term_hook);
671 
672 /*
673  * call_rtas_display_status and call_rtas_display_status_delay
674  * are designed only for very early low-level debugging, which
675  * is why the token is hard-coded to 10.
676  */
call_rtas_display_status(unsigned char c)677 static void call_rtas_display_status(unsigned char c)
678 {
679 	unsigned long flags;
680 
681 	if (!rtas.base)
682 		return;
683 
684 	raw_spin_lock_irqsave(&rtas_lock, flags);
685 	rtas_call_unlocked(&rtas_args, 10, 1, 1, NULL, c);
686 	raw_spin_unlock_irqrestore(&rtas_lock, flags);
687 }
688 
call_rtas_display_status_delay(char c)689 static void call_rtas_display_status_delay(char c)
690 {
691 	static int pending_newline = 0;  /* did last write end with unprinted newline? */
692 	static int width = 16;
693 
694 	if (c == '\n') {
695 		while (width-- > 0)
696 			call_rtas_display_status(' ');
697 		width = 16;
698 		mdelay(500);
699 		pending_newline = 1;
700 	} else {
701 		if (pending_newline) {
702 			call_rtas_display_status('\r');
703 			call_rtas_display_status('\n');
704 		}
705 		pending_newline = 0;
706 		if (width--) {
707 			call_rtas_display_status(c);
708 			udelay(10000);
709 		}
710 	}
711 }
712 
udbg_init_rtas_panel(void)713 void __init udbg_init_rtas_panel(void)
714 {
715 	udbg_putc = call_rtas_display_status_delay;
716 }
717 
718 #ifdef CONFIG_UDBG_RTAS_CONSOLE
719 
720 /* If you think you're dying before early_init_dt_scan_rtas() does its
721  * work, you can hard code the token values for your firmware here and
722  * hardcode rtas.base/entry etc.
723  */
724 static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE;
725 static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE;
726 
udbg_rtascon_putc(char c)727 static void udbg_rtascon_putc(char c)
728 {
729 	int tries;
730 
731 	if (!rtas.base)
732 		return;
733 
734 	/* Add CRs before LFs */
735 	if (c == '\n')
736 		udbg_rtascon_putc('\r');
737 
738 	/* if there is more than one character to be displayed, wait a bit */
739 	for (tries = 0; tries < 16; tries++) {
740 		if (rtas_call(rtas_putchar_token, 1, 1, NULL, c) == 0)
741 			break;
742 		udelay(1000);
743 	}
744 }
745 
udbg_rtascon_getc_poll(void)746 static int udbg_rtascon_getc_poll(void)
747 {
748 	int c;
749 
750 	if (!rtas.base)
751 		return -1;
752 
753 	if (rtas_call(rtas_getchar_token, 0, 2, &c))
754 		return -1;
755 
756 	return c;
757 }
758 
udbg_rtascon_getc(void)759 static int udbg_rtascon_getc(void)
760 {
761 	int c;
762 
763 	while ((c = udbg_rtascon_getc_poll()) == -1)
764 		;
765 
766 	return c;
767 }
768 
769 
udbg_init_rtas_console(void)770 void __init udbg_init_rtas_console(void)
771 {
772 	udbg_putc = udbg_rtascon_putc;
773 	udbg_getc = udbg_rtascon_getc;
774 	udbg_getc_poll = udbg_rtascon_getc_poll;
775 }
776 #endif /* CONFIG_UDBG_RTAS_CONSOLE */
777 
rtas_progress(char * s,unsigned short hex)778 void rtas_progress(char *s, unsigned short hex)
779 {
780 	struct device_node *root;
781 	int width;
782 	const __be32 *p;
783 	char *os;
784 	static int display_character, set_indicator;
785 	static int display_width, display_lines, form_feed;
786 	static const int *row_width;
787 	static DEFINE_SPINLOCK(progress_lock);
788 	static int current_line;
789 	static int pending_newline = 0;  /* did last write end with unprinted newline? */
790 
791 	if (!rtas.base)
792 		return;
793 
794 	if (display_width == 0) {
795 		display_width = 0x10;
796 		if ((root = of_find_node_by_path("/rtas"))) {
797 			if ((p = of_get_property(root,
798 					"ibm,display-line-length", NULL)))
799 				display_width = be32_to_cpu(*p);
800 			if ((p = of_get_property(root,
801 					"ibm,form-feed", NULL)))
802 				form_feed = be32_to_cpu(*p);
803 			if ((p = of_get_property(root,
804 					"ibm,display-number-of-lines", NULL)))
805 				display_lines = be32_to_cpu(*p);
806 			row_width = of_get_property(root,
807 					"ibm,display-truncation-length", NULL);
808 			of_node_put(root);
809 		}
810 		display_character = rtas_function_token(RTAS_FN_DISPLAY_CHARACTER);
811 		set_indicator = rtas_function_token(RTAS_FN_SET_INDICATOR);
812 	}
813 
814 	if (display_character == RTAS_UNKNOWN_SERVICE) {
815 		/* use hex display if available */
816 		if (set_indicator != RTAS_UNKNOWN_SERVICE)
817 			rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
818 		return;
819 	}
820 
821 	spin_lock(&progress_lock);
822 
823 	/*
824 	 * Last write ended with newline, but we didn't print it since
825 	 * it would just clear the bottom line of output. Print it now
826 	 * instead.
827 	 *
828 	 * If no newline is pending and form feed is supported, clear the
829 	 * display with a form feed; otherwise, print a CR to start output
830 	 * at the beginning of the line.
831 	 */
832 	if (pending_newline) {
833 		rtas_call(display_character, 1, 1, NULL, '\r');
834 		rtas_call(display_character, 1, 1, NULL, '\n');
835 		pending_newline = 0;
836 	} else {
837 		current_line = 0;
838 		if (form_feed)
839 			rtas_call(display_character, 1, 1, NULL,
840 				  (char)form_feed);
841 		else
842 			rtas_call(display_character, 1, 1, NULL, '\r');
843 	}
844 
845 	if (row_width)
846 		width = row_width[current_line];
847 	else
848 		width = display_width;
849 	os = s;
850 	while (*os) {
851 		if (*os == '\n' || *os == '\r') {
852 			/* If newline is the last character, save it
853 			 * until next call to avoid bumping up the
854 			 * display output.
855 			 */
856 			if (*os == '\n' && !os[1]) {
857 				pending_newline = 1;
858 				current_line++;
859 				if (current_line > display_lines-1)
860 					current_line = display_lines-1;
861 				spin_unlock(&progress_lock);
862 				return;
863 			}
864 
865 			/* RTAS wants CR-LF, not just LF */
866 
867 			if (*os == '\n') {
868 				rtas_call(display_character, 1, 1, NULL, '\r');
869 				rtas_call(display_character, 1, 1, NULL, '\n');
870 			} else {
871 				/* CR might be used to re-draw a line, so we'll
872 				 * leave it alone and not add LF.
873 				 */
874 				rtas_call(display_character, 1, 1, NULL, *os);
875 			}
876 
877 			if (row_width)
878 				width = row_width[current_line];
879 			else
880 				width = display_width;
881 		} else {
882 			width--;
883 			rtas_call(display_character, 1, 1, NULL, *os);
884 		}
885 
886 		os++;
887 
888 		/* if we overwrite the screen length */
889 		if (width <= 0)
890 			while ((*os != 0) && (*os != '\n') && (*os != '\r'))
891 				os++;
892 	}
893 
894 	spin_unlock(&progress_lock);
895 }
896 EXPORT_SYMBOL_GPL(rtas_progress);		/* needed by rtas_flash module */
897 
rtas_token(const char * service)898 int rtas_token(const char *service)
899 {
900 	const struct rtas_function *func;
901 	const __be32 *tokp;
902 
903 	if (rtas.dev == NULL)
904 		return RTAS_UNKNOWN_SERVICE;
905 
906 	func = rtas_name_to_function(service);
907 	if (func)
908 		return func->token;
909 	/*
910 	 * The caller is looking up a name that is not known to be an
911 	 * RTAS function. Either it's a function that needs to be
912 	 * added to the table, or they're misusing rtas_token() to
913 	 * access non-function properties of the /rtas node. Warn and
914 	 * fall back to the legacy behavior.
915 	 */
916 	WARN_ONCE(1, "unknown function `%s`, should it be added to rtas_function_table?\n",
917 		  service);
918 
919 	tokp = of_get_property(rtas.dev, service, NULL);
920 	return tokp ? be32_to_cpu(*tokp) : RTAS_UNKNOWN_SERVICE;
921 }
922 EXPORT_SYMBOL_GPL(rtas_token);
923 
rtas_service_present(const char * service)924 int rtas_service_present(const char *service)
925 {
926 	return rtas_token(service) != RTAS_UNKNOWN_SERVICE;
927 }
928 
929 #ifdef CONFIG_RTAS_ERROR_LOGGING
930 
931 static u32 rtas_error_log_max __ro_after_init = RTAS_ERROR_LOG_MAX;
932 
933 /*
934  * Return the firmware-specified size of the error log buffer
935  *  for all rtas calls that require an error buffer argument.
936  *  This includes 'check-exception' and 'rtas-last-error'.
937  */
rtas_get_error_log_max(void)938 int rtas_get_error_log_max(void)
939 {
940 	return rtas_error_log_max;
941 }
942 
init_error_log_max(void)943 static void __init init_error_log_max(void)
944 {
945 	static const char propname[] __initconst = "rtas-error-log-max";
946 	u32 max;
947 
948 	if (of_property_read_u32(rtas.dev, propname, &max)) {
949 		pr_warn("%s not found, using default of %u\n",
950 			propname, RTAS_ERROR_LOG_MAX);
951 		max = RTAS_ERROR_LOG_MAX;
952 	}
953 
954 	if (max > RTAS_ERROR_LOG_MAX) {
955 		pr_warn("%s = %u, clamping max error log size to %u\n",
956 			propname, max, RTAS_ERROR_LOG_MAX);
957 		max = RTAS_ERROR_LOG_MAX;
958 	}
959 
960 	rtas_error_log_max = max;
961 }
962 
963 
964 static char rtas_err_buf[RTAS_ERROR_LOG_MAX];
965 
966 /** Return a copy of the detailed error text associated with the
967  *  most recent failed call to rtas.  Because the error text
968  *  might go stale if there are any other intervening rtas calls,
969  *  this routine must be called atomically with whatever produced
970  *  the error (i.e. with rtas_lock still held from the previous call).
971  */
__fetch_rtas_last_error(char * altbuf)972 static char *__fetch_rtas_last_error(char *altbuf)
973 {
974 	const s32 token = rtas_function_token(RTAS_FN_RTAS_LAST_ERROR);
975 	struct rtas_args err_args, save_args;
976 	u32 bufsz;
977 	char *buf = NULL;
978 
979 	lockdep_assert_held(&rtas_lock);
980 
981 	if (token == -1)
982 		return NULL;
983 
984 	bufsz = rtas_get_error_log_max();
985 
986 	err_args.token = cpu_to_be32(token);
987 	err_args.nargs = cpu_to_be32(2);
988 	err_args.nret = cpu_to_be32(1);
989 	err_args.args[0] = cpu_to_be32(__pa(rtas_err_buf));
990 	err_args.args[1] = cpu_to_be32(bufsz);
991 	err_args.args[2] = 0;
992 
993 	save_args = rtas_args;
994 	rtas_args = err_args;
995 
996 	do_enter_rtas(&rtas_args);
997 
998 	err_args = rtas_args;
999 	rtas_args = save_args;
1000 
1001 	/* Log the error in the unlikely case that there was one. */
1002 	if (unlikely(err_args.args[2] == 0)) {
1003 		if (altbuf) {
1004 			buf = altbuf;
1005 		} else {
1006 			buf = rtas_err_buf;
1007 			if (slab_is_available())
1008 				buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
1009 		}
1010 		if (buf)
1011 			memmove(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
1012 	}
1013 
1014 	return buf;
1015 }
1016 
1017 #define get_errorlog_buffer()	kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
1018 
1019 #else /* CONFIG_RTAS_ERROR_LOGGING */
1020 #define __fetch_rtas_last_error(x)	NULL
1021 #define get_errorlog_buffer()		NULL
init_error_log_max(void)1022 static void __init init_error_log_max(void) {}
1023 #endif
1024 
1025 
1026 static void
va_rtas_call_unlocked(struct rtas_args * args,int token,int nargs,int nret,va_list list)1027 va_rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret,
1028 		      va_list list)
1029 {
1030 	int i;
1031 
1032 	args->token = cpu_to_be32(token);
1033 	args->nargs = cpu_to_be32(nargs);
1034 	args->nret  = cpu_to_be32(nret);
1035 	args->rets  = &(args->args[nargs]);
1036 
1037 	for (i = 0; i < nargs; ++i)
1038 		args->args[i] = cpu_to_be32(va_arg(list, __u32));
1039 
1040 	for (i = 0; i < nret; ++i)
1041 		args->rets[i] = 0;
1042 
1043 	do_enter_rtas(args);
1044 }
1045 
1046 /**
1047  * rtas_call_unlocked() - Invoke an RTAS firmware function without synchronization.
1048  * @args: RTAS parameter block to be used for the call, must obey RTAS addressing
1049  *        constraints.
1050  * @token: Identifies the function being invoked.
1051  * @nargs: Number of input parameters. Does not include token.
1052  * @nret: Number of output parameters, including the call status.
1053  * @....: List of @nargs input parameters.
1054  *
1055  * Invokes the RTAS function indicated by @token, which the caller
1056  * should obtain via rtas_function_token().
1057  *
1058  * This function is similar to rtas_call(), but must be used with a
1059  * limited set of RTAS calls specifically exempted from the general
1060  * requirement that only one RTAS call may be in progress at any
1061  * time. Examples include stop-self and ibm,nmi-interlock.
1062  */
rtas_call_unlocked(struct rtas_args * args,int token,int nargs,int nret,...)1063 void rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret, ...)
1064 {
1065 	va_list list;
1066 
1067 	va_start(list, nret);
1068 	va_rtas_call_unlocked(args, token, nargs, nret, list);
1069 	va_end(list);
1070 }
1071 
token_is_restricted_errinjct(s32 token)1072 static bool token_is_restricted_errinjct(s32 token)
1073 {
1074 	return token == rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT) ||
1075 	       token == rtas_function_token(RTAS_FN_IBM_ERRINJCT);
1076 }
1077 
1078 /**
1079  * rtas_call() - Invoke an RTAS firmware function.
1080  * @token: Identifies the function being invoked.
1081  * @nargs: Number of input parameters. Does not include token.
1082  * @nret: Number of output parameters, including the call status.
1083  * @outputs: Array of @nret output words.
1084  * @....: List of @nargs input parameters.
1085  *
1086  * Invokes the RTAS function indicated by @token, which the caller
1087  * should obtain via rtas_function_token().
1088  *
1089  * The @nargs and @nret arguments must match the number of input and
1090  * output parameters specified for the RTAS function.
1091  *
1092  * rtas_call() returns RTAS status codes, not conventional Linux errno
1093  * values. Callers must translate any failure to an appropriate errno
1094  * in syscall context. Most callers of RTAS functions that can return
1095  * -2 or 990x should use rtas_busy_delay() to correctly handle those
1096  * statuses before calling again.
1097  *
1098  * The return value descriptions are adapted from 7.2.8 [RTAS] Return
1099  * Codes of the PAPR and CHRP specifications.
1100  *
1101  * Context: Process context preferably, interrupt context if
1102  *          necessary.  Acquires an internal spinlock and may perform
1103  *          GFP_ATOMIC slab allocation in error path. Unsafe for NMI
1104  *          context.
1105  * Return:
1106  * *                          0 - RTAS function call succeeded.
1107  * *                         -1 - RTAS function encountered a hardware or
1108  *                                platform error, or the token is invalid,
1109  *                                or the function is restricted by kernel policy.
1110  * *                         -2 - Specs say "A necessary hardware device was busy,
1111  *                                and the requested function could not be
1112  *                                performed. The operation should be retried at
1113  *                                a later time." This is misleading, at least with
1114  *                                respect to current RTAS implementations. What it
1115  *                                usually means in practice is that the function
1116  *                                could not be completed while meeting RTAS's
1117  *                                deadline for returning control to the OS (250us
1118  *                                for PAPR/PowerVM, typically), but the call may be
1119  *                                immediately reattempted to resume work on it.
1120  * *                         -3 - Parameter error.
1121  * *                         -7 - Unexpected state change.
1122  * *                9000...9899 - Vendor-specific success codes.
1123  * *                9900...9905 - Advisory extended delay. Caller should try
1124  *                                again after ~10^x ms has elapsed, where x is
1125  *                                the last digit of the status [0-5]. Again going
1126  *                                beyond the PAPR text, 990x on PowerVM indicates
1127  *                                contention for RTAS-internal resources. Other
1128  *                                RTAS call sequences in progress should be
1129  *                                allowed to complete before reattempting the
1130  *                                call.
1131  * *                      -9000 - Multi-level isolation error.
1132  * *              -9999...-9004 - Vendor-specific error codes.
1133  * * Additional negative values - Function-specific error.
1134  * * Additional positive values - Function-specific success.
1135  */
rtas_call(int token,int nargs,int nret,int * outputs,...)1136 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
1137 {
1138 	struct pin_cookie cookie;
1139 	va_list list;
1140 	int i;
1141 	unsigned long flags;
1142 	struct rtas_args *args;
1143 	char *buff_copy = NULL;
1144 	int ret;
1145 
1146 	if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
1147 		return -1;
1148 
1149 	if (token_is_restricted_errinjct(token)) {
1150 		/*
1151 		 * It would be nicer to not discard the error value
1152 		 * from security_locked_down(), but callers expect an
1153 		 * RTAS status, not an errno.
1154 		 */
1155 		if (security_locked_down(LOCKDOWN_RTAS_ERROR_INJECTION))
1156 			return -1;
1157 	}
1158 
1159 	if ((mfmsr() & (MSR_IR|MSR_DR)) != (MSR_IR|MSR_DR)) {
1160 		WARN_ON_ONCE(1);
1161 		return -1;
1162 	}
1163 
1164 	raw_spin_lock_irqsave(&rtas_lock, flags);
1165 	cookie = lockdep_pin_lock(&rtas_lock);
1166 
1167 	/* We use the global rtas args buffer */
1168 	args = &rtas_args;
1169 
1170 	va_start(list, outputs);
1171 	va_rtas_call_unlocked(args, token, nargs, nret, list);
1172 	va_end(list);
1173 
1174 	/* A -1 return code indicates that the last command couldn't
1175 	   be completed due to a hardware error. */
1176 	if (be32_to_cpu(args->rets[0]) == -1)
1177 		buff_copy = __fetch_rtas_last_error(NULL);
1178 
1179 	if (nret > 1 && outputs != NULL)
1180 		for (i = 0; i < nret-1; ++i)
1181 			outputs[i] = be32_to_cpu(args->rets[i + 1]);
1182 	ret = (nret > 0) ? be32_to_cpu(args->rets[0]) : 0;
1183 
1184 	lockdep_unpin_lock(&rtas_lock, cookie);
1185 	raw_spin_unlock_irqrestore(&rtas_lock, flags);
1186 
1187 	if (buff_copy) {
1188 		log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
1189 		if (slab_is_available())
1190 			kfree(buff_copy);
1191 	}
1192 	return ret;
1193 }
1194 EXPORT_SYMBOL_GPL(rtas_call);
1195 
1196 /**
1197  * rtas_busy_delay_time() - From an RTAS status value, calculate the
1198  *                          suggested delay time in milliseconds.
1199  *
1200  * @status: a value returned from rtas_call() or similar APIs which return
1201  *          the status of a RTAS function call.
1202  *
1203  * Context: Any context.
1204  *
1205  * Return:
1206  * * 100000 - If @status is 9905.
1207  * * 10000  - If @status is 9904.
1208  * * 1000   - If @status is 9903.
1209  * * 100    - If @status is 9902.
1210  * * 10     - If @status is 9901.
1211  * * 1      - If @status is either 9900 or -2. This is "wrong" for -2, but
1212  *            some callers depend on this behavior, and the worst outcome
1213  *            is that they will delay for longer than necessary.
1214  * * 0      - If @status is not a busy or extended delay value.
1215  */
rtas_busy_delay_time(int status)1216 unsigned int rtas_busy_delay_time(int status)
1217 {
1218 	int order;
1219 	unsigned int ms = 0;
1220 
1221 	if (status == RTAS_BUSY) {
1222 		ms = 1;
1223 	} else if (status >= RTAS_EXTENDED_DELAY_MIN &&
1224 		   status <= RTAS_EXTENDED_DELAY_MAX) {
1225 		order = status - RTAS_EXTENDED_DELAY_MIN;
1226 		for (ms = 1; order > 0; order--)
1227 			ms *= 10;
1228 	}
1229 
1230 	return ms;
1231 }
1232 
1233 /*
1234  * Early boot fallback for rtas_busy_delay().
1235  */
rtas_busy_delay_early(int status)1236 static bool __init rtas_busy_delay_early(int status)
1237 {
1238 	static size_t successive_ext_delays __initdata;
1239 	bool retry;
1240 
1241 	switch (status) {
1242 	case RTAS_EXTENDED_DELAY_MIN...RTAS_EXTENDED_DELAY_MAX:
1243 		/*
1244 		 * In the unlikely case that we receive an extended
1245 		 * delay status in early boot, the OS is probably not
1246 		 * the cause, and there's nothing we can do to clear
1247 		 * the condition. Best we can do is delay for a bit
1248 		 * and hope it's transient. Lie to the caller if it
1249 		 * seems like we're stuck in a retry loop.
1250 		 */
1251 		mdelay(1);
1252 		retry = true;
1253 		successive_ext_delays += 1;
1254 		if (successive_ext_delays > 1000) {
1255 			pr_err("too many extended delays, giving up\n");
1256 			dump_stack();
1257 			retry = false;
1258 			successive_ext_delays = 0;
1259 		}
1260 		break;
1261 	case RTAS_BUSY:
1262 		retry = true;
1263 		successive_ext_delays = 0;
1264 		break;
1265 	default:
1266 		retry = false;
1267 		successive_ext_delays = 0;
1268 		break;
1269 	}
1270 
1271 	return retry;
1272 }
1273 
1274 /**
1275  * rtas_busy_delay() - helper for RTAS busy and extended delay statuses
1276  *
1277  * @status: a value returned from rtas_call() or similar APIs which return
1278  *          the status of a RTAS function call.
1279  *
1280  * Context: Process context. May sleep or schedule.
1281  *
1282  * Return:
1283  * * true  - @status is RTAS_BUSY or an extended delay hint. The
1284  *           caller may assume that the CPU has been yielded if necessary,
1285  *           and that an appropriate delay for @status has elapsed.
1286  *           Generally the caller should reattempt the RTAS call which
1287  *           yielded @status.
1288  *
1289  * * false - @status is not @RTAS_BUSY nor an extended delay hint. The
1290  *           caller is responsible for handling @status.
1291  */
rtas_busy_delay(int status)1292 bool __ref rtas_busy_delay(int status)
1293 {
1294 	unsigned int ms;
1295 	bool ret;
1296 
1297 	/*
1298 	 * Can't do timed sleeps before timekeeping is up.
1299 	 */
1300 	if (system_state < SYSTEM_SCHEDULING)
1301 		return rtas_busy_delay_early(status);
1302 
1303 	switch (status) {
1304 	case RTAS_EXTENDED_DELAY_MIN...RTAS_EXTENDED_DELAY_MAX:
1305 		ret = true;
1306 		ms = rtas_busy_delay_time(status);
1307 		/*
1308 		 * The extended delay hint can be as high as 100 seconds.
1309 		 * Surely any function returning such a status is either
1310 		 * buggy or isn't going to be significantly slowed by us
1311 		 * polling at 1HZ. Clamp the sleep time to one second.
1312 		 */
1313 		ms = clamp(ms, 1U, 1000U);
1314 		/*
1315 		 * The delay hint is an order-of-magnitude suggestion, not
1316 		 * a minimum. It is fine, possibly even advantageous, for
1317 		 * us to pause for less time than hinted. For small values,
1318 		 * use usleep_range() to ensure we don't sleep much longer
1319 		 * than actually needed.
1320 		 *
1321 		 * See Documentation/timers/timers-howto.rst for
1322 		 * explanation of the threshold used here. In effect we use
1323 		 * usleep_range() for 9900 and 9901, msleep() for
1324 		 * 9902-9905.
1325 		 */
1326 		if (ms <= 20)
1327 			usleep_range(ms * 100, ms * 1000);
1328 		else
1329 			msleep(ms);
1330 		break;
1331 	case RTAS_BUSY:
1332 		ret = true;
1333 		/*
1334 		 * We should call again immediately if there's no other
1335 		 * work to do.
1336 		 */
1337 		cond_resched();
1338 		break;
1339 	default:
1340 		ret = false;
1341 		/*
1342 		 * Not a busy or extended delay status; the caller should
1343 		 * handle @status itself. Ensure we warn on misuses in
1344 		 * atomic context regardless.
1345 		 */
1346 		might_sleep();
1347 		break;
1348 	}
1349 
1350 	return ret;
1351 }
1352 EXPORT_SYMBOL_GPL(rtas_busy_delay);
1353 
rtas_error_rc(int rtas_rc)1354 int rtas_error_rc(int rtas_rc)
1355 {
1356 	int rc;
1357 
1358 	switch (rtas_rc) {
1359 	case RTAS_HARDWARE_ERROR:	/* Hardware Error */
1360 		rc = -EIO;
1361 		break;
1362 	case RTAS_INVALID_PARAMETER:	/* Bad indicator/domain/etc */
1363 		rc = -EINVAL;
1364 		break;
1365 	case -9000:			/* Isolation error */
1366 		rc = -EFAULT;
1367 		break;
1368 	case -9001:			/* Outstanding TCE/PTE */
1369 		rc = -EEXIST;
1370 		break;
1371 	case -9002:			/* No usable slot */
1372 		rc = -ENODEV;
1373 		break;
1374 	default:
1375 		pr_err("%s: unexpected error %d\n", __func__, rtas_rc);
1376 		rc = -ERANGE;
1377 		break;
1378 	}
1379 	return rc;
1380 }
1381 EXPORT_SYMBOL_GPL(rtas_error_rc);
1382 
rtas_get_power_level(int powerdomain,int * level)1383 int rtas_get_power_level(int powerdomain, int *level)
1384 {
1385 	int token = rtas_function_token(RTAS_FN_GET_POWER_LEVEL);
1386 	int rc;
1387 
1388 	if (token == RTAS_UNKNOWN_SERVICE)
1389 		return -ENOENT;
1390 
1391 	while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
1392 		udelay(1);
1393 
1394 	if (rc < 0)
1395 		return rtas_error_rc(rc);
1396 	return rc;
1397 }
1398 EXPORT_SYMBOL_GPL(rtas_get_power_level);
1399 
rtas_set_power_level(int powerdomain,int level,int * setlevel)1400 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
1401 {
1402 	int token = rtas_function_token(RTAS_FN_SET_POWER_LEVEL);
1403 	int rc;
1404 
1405 	if (token == RTAS_UNKNOWN_SERVICE)
1406 		return -ENOENT;
1407 
1408 	do {
1409 		rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
1410 	} while (rtas_busy_delay(rc));
1411 
1412 	if (rc < 0)
1413 		return rtas_error_rc(rc);
1414 	return rc;
1415 }
1416 EXPORT_SYMBOL_GPL(rtas_set_power_level);
1417 
rtas_get_sensor(int sensor,int index,int * state)1418 int rtas_get_sensor(int sensor, int index, int *state)
1419 {
1420 	int token = rtas_function_token(RTAS_FN_GET_SENSOR_STATE);
1421 	int rc;
1422 
1423 	if (token == RTAS_UNKNOWN_SERVICE)
1424 		return -ENOENT;
1425 
1426 	do {
1427 		rc = rtas_call(token, 2, 2, state, sensor, index);
1428 	} while (rtas_busy_delay(rc));
1429 
1430 	if (rc < 0)
1431 		return rtas_error_rc(rc);
1432 	return rc;
1433 }
1434 EXPORT_SYMBOL_GPL(rtas_get_sensor);
1435 
rtas_get_sensor_fast(int sensor,int index,int * state)1436 int rtas_get_sensor_fast(int sensor, int index, int *state)
1437 {
1438 	int token = rtas_function_token(RTAS_FN_GET_SENSOR_STATE);
1439 	int rc;
1440 
1441 	if (token == RTAS_UNKNOWN_SERVICE)
1442 		return -ENOENT;
1443 
1444 	rc = rtas_call(token, 2, 2, state, sensor, index);
1445 	WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
1446 				    rc <= RTAS_EXTENDED_DELAY_MAX));
1447 
1448 	if (rc < 0)
1449 		return rtas_error_rc(rc);
1450 	return rc;
1451 }
1452 
rtas_indicator_present(int token,int * maxindex)1453 bool rtas_indicator_present(int token, int *maxindex)
1454 {
1455 	int proplen, count, i;
1456 	const struct indicator_elem {
1457 		__be32 token;
1458 		__be32 maxindex;
1459 	} *indicators;
1460 
1461 	indicators = of_get_property(rtas.dev, "rtas-indicators", &proplen);
1462 	if (!indicators)
1463 		return false;
1464 
1465 	count = proplen / sizeof(struct indicator_elem);
1466 
1467 	for (i = 0; i < count; i++) {
1468 		if (__be32_to_cpu(indicators[i].token) != token)
1469 			continue;
1470 		if (maxindex)
1471 			*maxindex = __be32_to_cpu(indicators[i].maxindex);
1472 		return true;
1473 	}
1474 
1475 	return false;
1476 }
1477 
rtas_set_indicator(int indicator,int index,int new_value)1478 int rtas_set_indicator(int indicator, int index, int new_value)
1479 {
1480 	int token = rtas_function_token(RTAS_FN_SET_INDICATOR);
1481 	int rc;
1482 
1483 	if (token == RTAS_UNKNOWN_SERVICE)
1484 		return -ENOENT;
1485 
1486 	do {
1487 		rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
1488 	} while (rtas_busy_delay(rc));
1489 
1490 	if (rc < 0)
1491 		return rtas_error_rc(rc);
1492 	return rc;
1493 }
1494 EXPORT_SYMBOL_GPL(rtas_set_indicator);
1495 
1496 /*
1497  * Ignoring RTAS extended delay
1498  */
rtas_set_indicator_fast(int indicator,int index,int new_value)1499 int rtas_set_indicator_fast(int indicator, int index, int new_value)
1500 {
1501 	int token = rtas_function_token(RTAS_FN_SET_INDICATOR);
1502 	int rc;
1503 
1504 	if (token == RTAS_UNKNOWN_SERVICE)
1505 		return -ENOENT;
1506 
1507 	rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
1508 
1509 	WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
1510 				    rc <= RTAS_EXTENDED_DELAY_MAX));
1511 
1512 	if (rc < 0)
1513 		return rtas_error_rc(rc);
1514 
1515 	return rc;
1516 }
1517 
1518 /**
1519  * rtas_ibm_suspend_me() - Call ibm,suspend-me to suspend the LPAR.
1520  *
1521  * @fw_status: RTAS call status will be placed here if not NULL.
1522  *
1523  * rtas_ibm_suspend_me() should be called only on a CPU which has
1524  * received H_CONTINUE from the H_JOIN hcall. All other active CPUs
1525  * should be waiting to return from H_JOIN.
1526  *
1527  * rtas_ibm_suspend_me() may suspend execution of the OS
1528  * indefinitely. Callers should take appropriate measures upon return, such as
1529  * resetting watchdog facilities.
1530  *
1531  * Callers may choose to retry this call if @fw_status is
1532  * %RTAS_THREADS_ACTIVE.
1533  *
1534  * Return:
1535  * 0          - The partition has resumed from suspend, possibly after
1536  *              migration to a different host.
1537  * -ECANCELED - The operation was aborted.
1538  * -EAGAIN    - There were other CPUs not in H_JOIN at the time of the call.
1539  * -EBUSY     - Some other condition prevented the suspend from succeeding.
1540  * -EIO       - Hardware/platform error.
1541  */
rtas_ibm_suspend_me(int * fw_status)1542 int rtas_ibm_suspend_me(int *fw_status)
1543 {
1544 	int token = rtas_function_token(RTAS_FN_IBM_SUSPEND_ME);
1545 	int fwrc;
1546 	int ret;
1547 
1548 	fwrc = rtas_call(token, 0, 1, NULL);
1549 
1550 	switch (fwrc) {
1551 	case 0:
1552 		ret = 0;
1553 		break;
1554 	case RTAS_SUSPEND_ABORTED:
1555 		ret = -ECANCELED;
1556 		break;
1557 	case RTAS_THREADS_ACTIVE:
1558 		ret = -EAGAIN;
1559 		break;
1560 	case RTAS_NOT_SUSPENDABLE:
1561 	case RTAS_OUTSTANDING_COPROC:
1562 		ret = -EBUSY;
1563 		break;
1564 	case -1:
1565 	default:
1566 		ret = -EIO;
1567 		break;
1568 	}
1569 
1570 	if (fw_status)
1571 		*fw_status = fwrc;
1572 
1573 	return ret;
1574 }
1575 
rtas_restart(char * cmd)1576 void __noreturn rtas_restart(char *cmd)
1577 {
1578 	if (rtas_flash_term_hook)
1579 		rtas_flash_term_hook(SYS_RESTART);
1580 	pr_emerg("system-reboot returned %d\n",
1581 		 rtas_call(rtas_function_token(RTAS_FN_SYSTEM_REBOOT), 0, 1, NULL));
1582 	for (;;);
1583 }
1584 
rtas_power_off(void)1585 void rtas_power_off(void)
1586 {
1587 	if (rtas_flash_term_hook)
1588 		rtas_flash_term_hook(SYS_POWER_OFF);
1589 	/* allow power on only with power button press */
1590 	pr_emerg("power-off returned %d\n",
1591 		 rtas_call(rtas_function_token(RTAS_FN_POWER_OFF), 2, 1, NULL, -1, -1));
1592 	for (;;);
1593 }
1594 
rtas_halt(void)1595 void __noreturn rtas_halt(void)
1596 {
1597 	if (rtas_flash_term_hook)
1598 		rtas_flash_term_hook(SYS_HALT);
1599 	/* allow power on only with power button press */
1600 	pr_emerg("power-off returned %d\n",
1601 		 rtas_call(rtas_function_token(RTAS_FN_POWER_OFF), 2, 1, NULL, -1, -1));
1602 	for (;;);
1603 }
1604 
1605 /* Must be in the RMO region, so we place it here */
1606 static char rtas_os_term_buf[2048];
1607 static bool ibm_extended_os_term;
1608 
rtas_os_term(char * str)1609 void rtas_os_term(char *str)
1610 {
1611 	s32 token = rtas_function_token(RTAS_FN_IBM_OS_TERM);
1612 	static struct rtas_args args;
1613 	int status;
1614 
1615 	/*
1616 	 * Firmware with the ibm,extended-os-term property is guaranteed
1617 	 * to always return from an ibm,os-term call. Earlier versions without
1618 	 * this property may terminate the partition which we want to avoid
1619 	 * since it interferes with panic_timeout.
1620 	 */
1621 
1622 	if (token == RTAS_UNKNOWN_SERVICE || !ibm_extended_os_term)
1623 		return;
1624 
1625 	snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
1626 
1627 	/*
1628 	 * Keep calling as long as RTAS returns a "try again" status,
1629 	 * but don't use rtas_busy_delay(), which potentially
1630 	 * schedules.
1631 	 */
1632 	do {
1633 		rtas_call_unlocked(&args, token, 1, 1, NULL, __pa(rtas_os_term_buf));
1634 		status = be32_to_cpu(args.rets[0]);
1635 	} while (rtas_busy_delay_time(status));
1636 
1637 	if (status != 0)
1638 		pr_emerg("ibm,os-term call failed %d\n", status);
1639 }
1640 
1641 /**
1642  * rtas_activate_firmware() - Activate a new version of firmware.
1643  *
1644  * Context: This function may sleep.
1645  *
1646  * Activate a new version of partition firmware. The OS must call this
1647  * after resuming from a partition hibernation or migration in order
1648  * to maintain the ability to perform live firmware updates. It's not
1649  * catastrophic for this method to be absent or to fail; just log the
1650  * condition in that case.
1651  */
rtas_activate_firmware(void)1652 void rtas_activate_firmware(void)
1653 {
1654 	int token = rtas_function_token(RTAS_FN_IBM_ACTIVATE_FIRMWARE);
1655 	int fwrc;
1656 
1657 	if (token == RTAS_UNKNOWN_SERVICE) {
1658 		pr_notice("ibm,activate-firmware method unavailable\n");
1659 		return;
1660 	}
1661 
1662 	do {
1663 		fwrc = rtas_call(token, 0, 1, NULL);
1664 	} while (rtas_busy_delay(fwrc));
1665 
1666 	if (fwrc)
1667 		pr_err("ibm,activate-firmware failed (%i)\n", fwrc);
1668 }
1669 
1670 /**
1671  * get_pseries_errorlog() - Find a specific pseries error log in an RTAS
1672  *                          extended event log.
1673  * @log: RTAS error/event log
1674  * @section_id: two character section identifier
1675  *
1676  * Return: A pointer to the specified errorlog or NULL if not found.
1677  */
get_pseries_errorlog(struct rtas_error_log * log,uint16_t section_id)1678 noinstr struct pseries_errorlog *get_pseries_errorlog(struct rtas_error_log *log,
1679 						      uint16_t section_id)
1680 {
1681 	struct rtas_ext_event_log_v6 *ext_log =
1682 		(struct rtas_ext_event_log_v6 *)log->buffer;
1683 	struct pseries_errorlog *sect;
1684 	unsigned char *p, *log_end;
1685 	uint32_t ext_log_length = rtas_error_extended_log_length(log);
1686 	uint8_t log_format = rtas_ext_event_log_format(ext_log);
1687 	uint32_t company_id = rtas_ext_event_company_id(ext_log);
1688 
1689 	/* Check that we understand the format */
1690 	if (ext_log_length < sizeof(struct rtas_ext_event_log_v6) ||
1691 	    log_format != RTAS_V6EXT_LOG_FORMAT_EVENT_LOG ||
1692 	    company_id != RTAS_V6EXT_COMPANY_ID_IBM)
1693 		return NULL;
1694 
1695 	log_end = log->buffer + ext_log_length;
1696 	p = ext_log->vendor_log;
1697 
1698 	while (p < log_end) {
1699 		sect = (struct pseries_errorlog *)p;
1700 		if (pseries_errorlog_id(sect) == section_id)
1701 			return sect;
1702 		p += pseries_errorlog_length(sect);
1703 	}
1704 
1705 	return NULL;
1706 }
1707 
1708 /*
1709  * The sys_rtas syscall, as originally designed, allows root to pass
1710  * arbitrary physical addresses to RTAS calls. A number of RTAS calls
1711  * can be abused to write to arbitrary memory and do other things that
1712  * are potentially harmful to system integrity, and thus should only
1713  * be used inside the kernel and not exposed to userspace.
1714  *
1715  * All known legitimate users of the sys_rtas syscall will only ever
1716  * pass addresses that fall within the RMO buffer, and use a known
1717  * subset of RTAS calls.
1718  *
1719  * Accordingly, we filter RTAS requests to check that the call is
1720  * permitted, and that provided pointers fall within the RMO buffer.
1721  * If a function is allowed to be invoked via the syscall, then its
1722  * entry in the rtas_functions table points to a rtas_filter that
1723  * describes its constraints, with the indexes of the parameters which
1724  * are expected to contain addresses and sizes of buffers allocated
1725  * inside the RMO buffer.
1726  */
1727 
in_rmo_buf(u32 base,u32 end)1728 static bool in_rmo_buf(u32 base, u32 end)
1729 {
1730 	return base >= rtas_rmo_buf &&
1731 		base < (rtas_rmo_buf + RTAS_USER_REGION_SIZE) &&
1732 		base <= end &&
1733 		end >= rtas_rmo_buf &&
1734 		end < (rtas_rmo_buf + RTAS_USER_REGION_SIZE);
1735 }
1736 
block_rtas_call(int token,int nargs,struct rtas_args * args)1737 static bool block_rtas_call(int token, int nargs,
1738 			    struct rtas_args *args)
1739 {
1740 	const struct rtas_function *func;
1741 	const struct rtas_filter *f;
1742 	const bool is_platform_dump = token == rtas_function_token(RTAS_FN_IBM_PLATFORM_DUMP);
1743 	const bool is_config_conn = token == rtas_function_token(RTAS_FN_IBM_CONFIGURE_CONNECTOR);
1744 	u32 base, size, end;
1745 
1746 	/*
1747 	 * If this token doesn't correspond to a function the kernel
1748 	 * understands, you're not allowed to call it.
1749 	 */
1750 	func = rtas_token_to_function_untrusted(token);
1751 	if (!func)
1752 		goto err;
1753 	/*
1754 	 * And only functions with filters attached are allowed.
1755 	 */
1756 	f = func->filter;
1757 	if (!f)
1758 		goto err;
1759 	/*
1760 	 * And some functions aren't allowed on LE.
1761 	 */
1762 	if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN) && func->banned_for_syscall_on_le)
1763 		goto err;
1764 
1765 	if (f->buf_idx1 != -1) {
1766 		base = be32_to_cpu(args->args[f->buf_idx1]);
1767 		if (f->size_idx1 != -1)
1768 			size = be32_to_cpu(args->args[f->size_idx1]);
1769 		else if (f->fixed_size)
1770 			size = f->fixed_size;
1771 		else
1772 			size = 1;
1773 
1774 		end = base + size - 1;
1775 
1776 		/*
1777 		 * Special case for ibm,platform-dump - NULL buffer
1778 		 * address is used to indicate end of dump processing
1779 		 */
1780 		if (is_platform_dump && base == 0)
1781 			return false;
1782 
1783 		if (!in_rmo_buf(base, end))
1784 			goto err;
1785 	}
1786 
1787 	if (f->buf_idx2 != -1) {
1788 		base = be32_to_cpu(args->args[f->buf_idx2]);
1789 		if (f->size_idx2 != -1)
1790 			size = be32_to_cpu(args->args[f->size_idx2]);
1791 		else if (f->fixed_size)
1792 			size = f->fixed_size;
1793 		else
1794 			size = 1;
1795 		end = base + size - 1;
1796 
1797 		/*
1798 		 * Special case for ibm,configure-connector where the
1799 		 * address can be 0
1800 		 */
1801 		if (is_config_conn && base == 0)
1802 			return false;
1803 
1804 		if (!in_rmo_buf(base, end))
1805 			goto err;
1806 	}
1807 
1808 	return false;
1809 err:
1810 	pr_err_ratelimited("sys_rtas: RTAS call blocked - exploit attempt?\n");
1811 	pr_err_ratelimited("sys_rtas: token=0x%x, nargs=%d (called by %s)\n",
1812 			   token, nargs, current->comm);
1813 	return true;
1814 }
1815 
1816 /* We assume to be passed big endian arguments */
SYSCALL_DEFINE1(rtas,struct rtas_args __user *,uargs)1817 SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)
1818 {
1819 	struct pin_cookie cookie;
1820 	struct rtas_args args;
1821 	unsigned long flags;
1822 	char *buff_copy, *errbuf = NULL;
1823 	int nargs, nret, token;
1824 
1825 	if (!capable(CAP_SYS_ADMIN))
1826 		return -EPERM;
1827 
1828 	if (!rtas.entry)
1829 		return -EINVAL;
1830 
1831 	if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
1832 		return -EFAULT;
1833 
1834 	nargs = be32_to_cpu(args.nargs);
1835 	nret  = be32_to_cpu(args.nret);
1836 	token = be32_to_cpu(args.token);
1837 
1838 	if (nargs >= ARRAY_SIZE(args.args)
1839 	    || nret > ARRAY_SIZE(args.args)
1840 	    || nargs + nret > ARRAY_SIZE(args.args))
1841 		return -EINVAL;
1842 
1843 	nargs = array_index_nospec(nargs, ARRAY_SIZE(args.args));
1844 	nret = array_index_nospec(nret, ARRAY_SIZE(args.args) - nargs);
1845 
1846 	/* Copy in args. */
1847 	if (copy_from_user(args.args, uargs->args,
1848 			   nargs * sizeof(rtas_arg_t)) != 0)
1849 		return -EFAULT;
1850 
1851 	if (token == RTAS_UNKNOWN_SERVICE)
1852 		return -EINVAL;
1853 
1854 	args.rets = &args.args[nargs];
1855 	memset(args.rets, 0, nret * sizeof(rtas_arg_t));
1856 
1857 	if (block_rtas_call(token, nargs, &args))
1858 		return -EINVAL;
1859 
1860 	if (token_is_restricted_errinjct(token)) {
1861 		int err;
1862 
1863 		err = security_locked_down(LOCKDOWN_RTAS_ERROR_INJECTION);
1864 		if (err)
1865 			return err;
1866 	}
1867 
1868 	/* Need to handle ibm,suspend_me call specially */
1869 	if (token == rtas_function_token(RTAS_FN_IBM_SUSPEND_ME)) {
1870 
1871 		/*
1872 		 * rtas_ibm_suspend_me assumes the streamid handle is in cpu
1873 		 * endian, or at least the hcall within it requires it.
1874 		 */
1875 		int rc = 0;
1876 		u64 handle = ((u64)be32_to_cpu(args.args[0]) << 32)
1877 		              | be32_to_cpu(args.args[1]);
1878 		rc = rtas_syscall_dispatch_ibm_suspend_me(handle);
1879 		if (rc == -EAGAIN)
1880 			args.rets[0] = cpu_to_be32(RTAS_NOT_SUSPENDABLE);
1881 		else if (rc == -EIO)
1882 			args.rets[0] = cpu_to_be32(-1);
1883 		else if (rc)
1884 			return rc;
1885 		goto copy_return;
1886 	}
1887 
1888 	buff_copy = get_errorlog_buffer();
1889 
1890 	raw_spin_lock_irqsave(&rtas_lock, flags);
1891 	cookie = lockdep_pin_lock(&rtas_lock);
1892 
1893 	rtas_args = args;
1894 	do_enter_rtas(&rtas_args);
1895 	args = rtas_args;
1896 
1897 	/* A -1 return code indicates that the last command couldn't
1898 	   be completed due to a hardware error. */
1899 	if (be32_to_cpu(args.rets[0]) == -1)
1900 		errbuf = __fetch_rtas_last_error(buff_copy);
1901 
1902 	lockdep_unpin_lock(&rtas_lock, cookie);
1903 	raw_spin_unlock_irqrestore(&rtas_lock, flags);
1904 
1905 	if (buff_copy) {
1906 		if (errbuf)
1907 			log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
1908 		kfree(buff_copy);
1909 	}
1910 
1911  copy_return:
1912 	/* Copy out args. */
1913 	if (copy_to_user(uargs->args + nargs,
1914 			 args.args + nargs,
1915 			 nret * sizeof(rtas_arg_t)) != 0)
1916 		return -EFAULT;
1917 
1918 	return 0;
1919 }
1920 
rtas_function_table_init(void)1921 static void __init rtas_function_table_init(void)
1922 {
1923 	struct property *prop;
1924 
1925 	for (size_t i = 0; i < ARRAY_SIZE(rtas_function_table); ++i) {
1926 		struct rtas_function *curr = &rtas_function_table[i];
1927 		struct rtas_function *prior;
1928 		int cmp;
1929 
1930 		curr->token = RTAS_UNKNOWN_SERVICE;
1931 
1932 		if (i == 0)
1933 			continue;
1934 		/*
1935 		 * Ensure table is sorted correctly for binary search
1936 		 * on function names.
1937 		 */
1938 		prior = &rtas_function_table[i - 1];
1939 
1940 		cmp = strcmp(prior->name, curr->name);
1941 		if (cmp < 0)
1942 			continue;
1943 
1944 		if (cmp == 0) {
1945 			pr_err("'%s' has duplicate function table entries\n",
1946 			       curr->name);
1947 		} else {
1948 			pr_err("function table unsorted: '%s' wrongly precedes '%s'\n",
1949 			       prior->name, curr->name);
1950 		}
1951 	}
1952 
1953 	for_each_property_of_node(rtas.dev, prop) {
1954 		struct rtas_function *func;
1955 
1956 		if (prop->length != sizeof(u32))
1957 			continue;
1958 
1959 		func = __rtas_name_to_function(prop->name);
1960 		if (!func)
1961 			continue;
1962 
1963 		func->token = be32_to_cpup((__be32 *)prop->value);
1964 
1965 		pr_debug("function %s has token %u\n", func->name, func->token);
1966 	}
1967 }
1968 
1969 /*
1970  * Call early during boot, before mem init, to retrieve the RTAS
1971  * information from the device-tree and allocate the RMO buffer for userland
1972  * accesses.
1973  */
rtas_initialize(void)1974 void __init rtas_initialize(void)
1975 {
1976 	unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
1977 	u32 base, size, entry;
1978 	int no_base, no_size, no_entry;
1979 
1980 	/* Get RTAS dev node and fill up our "rtas" structure with infos
1981 	 * about it.
1982 	 */
1983 	rtas.dev = of_find_node_by_name(NULL, "rtas");
1984 	if (!rtas.dev)
1985 		return;
1986 
1987 	no_base = of_property_read_u32(rtas.dev, "linux,rtas-base", &base);
1988 	no_size = of_property_read_u32(rtas.dev, "rtas-size", &size);
1989 	if (no_base || no_size) {
1990 		of_node_put(rtas.dev);
1991 		rtas.dev = NULL;
1992 		return;
1993 	}
1994 
1995 	rtas.base = base;
1996 	rtas.size = size;
1997 	no_entry = of_property_read_u32(rtas.dev, "linux,rtas-entry", &entry);
1998 	rtas.entry = no_entry ? rtas.base : entry;
1999 
2000 	init_error_log_max();
2001 
2002 	/* Must be called before any function token lookups */
2003 	rtas_function_table_init();
2004 
2005 	/*
2006 	 * Discover this now to avoid a device tree lookup in the
2007 	 * panic path.
2008 	 */
2009 	ibm_extended_os_term = of_property_read_bool(rtas.dev, "ibm,extended-os-term");
2010 
2011 	/* If RTAS was found, allocate the RMO buffer for it and look for
2012 	 * the stop-self token if any
2013 	 */
2014 #ifdef CONFIG_PPC64
2015 	if (firmware_has_feature(FW_FEATURE_LPAR))
2016 		rtas_region = min(ppc64_rma_size, RTAS_INSTANTIATE_MAX);
2017 #endif
2018 	rtas_rmo_buf = memblock_phys_alloc_range(RTAS_USER_REGION_SIZE, PAGE_SIZE,
2019 						 0, rtas_region);
2020 	if (!rtas_rmo_buf)
2021 		panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n",
2022 		      PAGE_SIZE, &rtas_region);
2023 
2024 	rtas_work_area_reserve_arena(rtas_region);
2025 }
2026 
early_init_dt_scan_rtas(unsigned long node,const char * uname,int depth,void * data)2027 int __init early_init_dt_scan_rtas(unsigned long node,
2028 		const char *uname, int depth, void *data)
2029 {
2030 	const u32 *basep, *entryp, *sizep;
2031 
2032 	if (depth != 1 || strcmp(uname, "rtas") != 0)
2033 		return 0;
2034 
2035 	basep  = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
2036 	entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
2037 	sizep  = of_get_flat_dt_prop(node, "rtas-size", NULL);
2038 
2039 #ifdef CONFIG_PPC64
2040 	/* need this feature to decide the crashkernel offset */
2041 	if (of_get_flat_dt_prop(node, "ibm,hypertas-functions", NULL))
2042 		powerpc_firmware_features |= FW_FEATURE_LPAR;
2043 #endif
2044 
2045 	if (basep && entryp && sizep) {
2046 		rtas.base = *basep;
2047 		rtas.entry = *entryp;
2048 		rtas.size = *sizep;
2049 	}
2050 
2051 #ifdef CONFIG_UDBG_RTAS_CONSOLE
2052 	basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
2053 	if (basep)
2054 		rtas_putchar_token = *basep;
2055 
2056 	basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
2057 	if (basep)
2058 		rtas_getchar_token = *basep;
2059 
2060 	if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
2061 	    rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
2062 		udbg_init_rtas_console();
2063 
2064 #endif
2065 
2066 	/* break now */
2067 	return 1;
2068 }
2069 
2070 static DEFINE_RAW_SPINLOCK(timebase_lock);
2071 static u64 timebase = 0;
2072 
rtas_give_timebase(void)2073 void rtas_give_timebase(void)
2074 {
2075 	unsigned long flags;
2076 
2077 	raw_spin_lock_irqsave(&timebase_lock, flags);
2078 	hard_irq_disable();
2079 	rtas_call(rtas_function_token(RTAS_FN_FREEZE_TIME_BASE), 0, 1, NULL);
2080 	timebase = get_tb();
2081 	raw_spin_unlock(&timebase_lock);
2082 
2083 	while (timebase)
2084 		barrier();
2085 	rtas_call(rtas_function_token(RTAS_FN_THAW_TIME_BASE), 0, 1, NULL);
2086 	local_irq_restore(flags);
2087 }
2088 
rtas_take_timebase(void)2089 void rtas_take_timebase(void)
2090 {
2091 	while (!timebase)
2092 		barrier();
2093 	raw_spin_lock(&timebase_lock);
2094 	set_tb(timebase >> 32, timebase & 0xffffffff);
2095 	timebase = 0;
2096 	raw_spin_unlock(&timebase_lock);
2097 }
2098