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