Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * Procedures for interfacing to the RTAS on CHRP machines. |
| 5 | * |
| 6 | * Peter Bergner, IBM March 2001. |
| 7 | * Copyright (C) 2001 IBM. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <stdarg.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/spinlock.h> |
Paul Gortmaker | 4b16f8e | 2011-07-22 18:24:23 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
Randy Dunlap | a941564 | 2006-01-11 12:17:48 -0800 | [diff] [blame] | 16 | #include <linux/capability.h> |
Benjamin Herrenschmidt | 21fe330 | 2005-11-07 16:41:59 +1100 | [diff] [blame] | 17 | #include <linux/delay.h> |
Robert Jennings | 120496a | 2013-05-07 04:34:11 +0000 | [diff] [blame] | 18 | #include <linux/cpu.h> |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 19 | #include <linux/smp.h> |
| 20 | #include <linux/completion.h> |
| 21 | #include <linux/cpumask.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 22 | #include <linux/memblock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Amerigo Wang | c5f4175 | 2011-07-25 17:13:10 -0700 | [diff] [blame] | 24 | #include <linux/reboot.h> |
Al Viro | 4c392e6 | 2018-05-02 23:20:48 +1000 | [diff] [blame] | 25 | #include <linux/syscalls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include <asm/prom.h> |
| 28 | #include <asm/rtas.h> |
Michael Ellerman | 31a7f67 | 2006-01-31 17:17:47 +1100 | [diff] [blame] | 29 | #include <asm/hvcall.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/machdep.h> |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 31 | #include <asm/firmware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/page.h> |
| 33 | #include <asm/param.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/delay.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 35 | #include <linux/uaccess.h> |
Michael Ellerman | 296167a | 2006-01-11 11:54:09 +1100 | [diff] [blame] | 36 | #include <asm/udbg.h> |
Arnd Bergmann | a7f3184 | 2006-03-23 00:00:08 +0100 | [diff] [blame] | 37 | #include <asm/syscalls.h> |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 38 | #include <asm/smp.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 39 | #include <linux/atomic.h> |
Benjamin Herrenschmidt | c4007a2 | 2009-06-16 16:42:50 +0000 | [diff] [blame] | 40 | #include <asm/time.h> |
Brian King | 46db2f8 | 2009-08-28 12:06:29 +0000 | [diff] [blame] | 41 | #include <asm/mmu.h> |
Jesse Larrew | 3b7a27d | 2010-12-01 12:31:26 +0000 | [diff] [blame] | 42 | #include <asm/topology.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Michael Ellerman | cd5cdeb | 2015-11-24 22:26:12 +1100 | [diff] [blame] | 44 | /* This is here deliberately so it's only used in this file */ |
| 45 | void enter_rtas(unsigned long); |
| 46 | |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 47 | struct rtas_t rtas = { |
Thomas Gleixner | edc35bd | 2009-12-03 12:38:57 +0100 | [diff] [blame] | 48 | .lock = __ARCH_SPIN_LOCK_UNLOCKED |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 50 | EXPORT_SYMBOL(rtas); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | DEFINE_SPINLOCK(rtas_data_buf_lock); |
Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 53 | EXPORT_SYMBOL(rtas_data_buf_lock); |
| 54 | |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 55 | char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned; |
Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 56 | EXPORT_SYMBOL(rtas_data_buf); |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | unsigned long rtas_rmo_buf; |
| 59 | |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 60 | /* |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 61 | * If non-NULL, this gets called when the kernel terminates. |
| 62 | * This is done like this so rtas_flash can be a module. |
| 63 | */ |
| 64 | void (*rtas_flash_term_hook)(int); |
| 65 | EXPORT_SYMBOL(rtas_flash_term_hook); |
| 66 | |
Benjamin Herrenschmidt | f97bb36 | 2009-06-16 16:42:49 +0000 | [diff] [blame] | 67 | /* RTAS use home made raw locking instead of spin_lock_irqsave |
| 68 | * because those can be called from within really nasty contexts |
| 69 | * such as having the timebase stopped which would lockup with |
| 70 | * normal locks and spinlock debugging enabled |
| 71 | */ |
| 72 | static unsigned long lock_rtas(void) |
| 73 | { |
| 74 | unsigned long flags; |
| 75 | |
| 76 | local_irq_save(flags); |
| 77 | preempt_disable(); |
Will Deacon | 58788a9 | 2017-10-18 12:51:09 +0100 | [diff] [blame] | 78 | arch_spin_lock(&rtas.lock); |
Benjamin Herrenschmidt | f97bb36 | 2009-06-16 16:42:49 +0000 | [diff] [blame] | 79 | return flags; |
| 80 | } |
| 81 | |
| 82 | static void unlock_rtas(unsigned long flags) |
| 83 | { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 84 | arch_spin_unlock(&rtas.lock); |
Benjamin Herrenschmidt | f97bb36 | 2009-06-16 16:42:49 +0000 | [diff] [blame] | 85 | local_irq_restore(flags); |
| 86 | preempt_enable(); |
| 87 | } |
| 88 | |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 89 | /* |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 90 | * call_rtas_display_status and call_rtas_display_status_delay |
| 91 | * are designed only for very early low-level debugging, which |
| 92 | * is why the token is hard-coded to 10. |
| 93 | */ |
Anton Blanchard | 2712826 | 2013-08-07 02:01:31 +1000 | [diff] [blame] | 94 | static void call_rtas_display_status(unsigned char c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | unsigned long s; |
| 97 | |
| 98 | if (!rtas.base) |
| 99 | return; |
Michael Ellerman | 4456f45 | 2015-11-24 22:26:11 +1100 | [diff] [blame] | 100 | |
Benjamin Herrenschmidt | f97bb36 | 2009-06-16 16:42:49 +0000 | [diff] [blame] | 101 | s = lock_rtas(); |
Michael Ellerman | 4456f45 | 2015-11-24 22:26:11 +1100 | [diff] [blame] | 102 | rtas_call_unlocked(&rtas.args, 10, 1, 1, NULL, c); |
Benjamin Herrenschmidt | f97bb36 | 2009-06-16 16:42:49 +0000 | [diff] [blame] | 103 | unlock_rtas(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Michael Ellerman | 296167a | 2006-01-11 11:54:09 +1100 | [diff] [blame] | 106 | static void call_rtas_display_status_delay(char c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | static int pending_newline = 0; /* did last write end with unprinted newline? */ |
| 109 | static int width = 16; |
| 110 | |
| 111 | if (c == '\n') { |
| 112 | while (width-- > 0) |
| 113 | call_rtas_display_status(' '); |
| 114 | width = 16; |
Benjamin Herrenschmidt | 21fe330 | 2005-11-07 16:41:59 +1100 | [diff] [blame] | 115 | mdelay(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | pending_newline = 1; |
| 117 | } else { |
| 118 | if (pending_newline) { |
| 119 | call_rtas_display_status('\r'); |
| 120 | call_rtas_display_status('\n'); |
| 121 | } |
| 122 | pending_newline = 0; |
| 123 | if (width--) { |
| 124 | call_rtas_display_status(c); |
| 125 | udelay(10000); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
Michael Ellerman | cc46bb9 | 2006-06-23 18:20:16 +1000 | [diff] [blame] | 130 | void __init udbg_init_rtas_panel(void) |
Michael Ellerman | 296167a | 2006-01-11 11:54:09 +1100 | [diff] [blame] | 131 | { |
| 132 | udbg_putc = call_rtas_display_status_delay; |
| 133 | } |
| 134 | |
Michael Ellerman | cc46bb9 | 2006-06-23 18:20:16 +1000 | [diff] [blame] | 135 | #ifdef CONFIG_UDBG_RTAS_CONSOLE |
| 136 | |
| 137 | /* If you think you're dying before early_init_dt_scan_rtas() does its |
| 138 | * work, you can hard code the token values for your firmware here and |
| 139 | * hardcode rtas.base/entry etc. |
| 140 | */ |
| 141 | static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE; |
| 142 | static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE; |
| 143 | |
| 144 | static void udbg_rtascon_putc(char c) |
| 145 | { |
| 146 | int tries; |
| 147 | |
| 148 | if (!rtas.base) |
| 149 | return; |
| 150 | |
| 151 | /* Add CRs before LFs */ |
| 152 | if (c == '\n') |
| 153 | udbg_rtascon_putc('\r'); |
| 154 | |
| 155 | /* if there is more than one character to be displayed, wait a bit */ |
| 156 | for (tries = 0; tries < 16; tries++) { |
| 157 | if (rtas_call(rtas_putchar_token, 1, 1, NULL, c) == 0) |
| 158 | break; |
| 159 | udelay(1000); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static int udbg_rtascon_getc_poll(void) |
| 164 | { |
| 165 | int c; |
| 166 | |
| 167 | if (!rtas.base) |
| 168 | return -1; |
| 169 | |
| 170 | if (rtas_call(rtas_getchar_token, 0, 2, &c)) |
| 171 | return -1; |
| 172 | |
| 173 | return c; |
| 174 | } |
| 175 | |
| 176 | static int udbg_rtascon_getc(void) |
| 177 | { |
| 178 | int c; |
| 179 | |
| 180 | while ((c = udbg_rtascon_getc_poll()) == -1) |
| 181 | ; |
| 182 | |
| 183 | return c; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | void __init udbg_init_rtas_console(void) |
| 188 | { |
| 189 | udbg_putc = udbg_rtascon_putc; |
| 190 | udbg_getc = udbg_rtascon_getc; |
| 191 | udbg_getc_poll = udbg_rtascon_getc_poll; |
| 192 | } |
| 193 | #endif /* CONFIG_UDBG_RTAS_CONSOLE */ |
| 194 | |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 195 | void rtas_progress(char *s, unsigned short hex) |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 196 | { |
| 197 | struct device_node *root; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 198 | int width; |
Anton Blanchard | 08bc1dc | 2013-08-07 02:01:29 +1000 | [diff] [blame] | 199 | const __be32 *p; |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 200 | char *os; |
| 201 | static int display_character, set_indicator; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 202 | static int display_width, display_lines, form_feed; |
Tobias Klauser | c5a69d5 | 2007-02-17 20:11:19 +0100 | [diff] [blame] | 203 | static const int *row_width; |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 204 | static DEFINE_SPINLOCK(progress_lock); |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 205 | static int current_line; |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 206 | static int pending_newline = 0; /* did last write end with unprinted newline? */ |
| 207 | |
| 208 | if (!rtas.base) |
| 209 | return; |
| 210 | |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 211 | if (display_width == 0) { |
| 212 | display_width = 0x10; |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 213 | if ((root = of_find_node_by_path("/rtas"))) { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 214 | if ((p = of_get_property(root, |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 215 | "ibm,display-line-length", NULL))) |
Anton Blanchard | 08bc1dc | 2013-08-07 02:01:29 +1000 | [diff] [blame] | 216 | display_width = be32_to_cpu(*p); |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 217 | if ((p = of_get_property(root, |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 218 | "ibm,form-feed", NULL))) |
Anton Blanchard | 08bc1dc | 2013-08-07 02:01:29 +1000 | [diff] [blame] | 219 | form_feed = be32_to_cpu(*p); |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 220 | if ((p = of_get_property(root, |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 221 | "ibm,display-number-of-lines", NULL))) |
Anton Blanchard | 08bc1dc | 2013-08-07 02:01:29 +1000 | [diff] [blame] | 222 | display_lines = be32_to_cpu(*p); |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 223 | row_width = of_get_property(root, |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 224 | "ibm,display-truncation-length", NULL); |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 225 | of_node_put(root); |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 226 | } |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 227 | display_character = rtas_token("display-character"); |
| 228 | set_indicator = rtas_token("set-indicator"); |
| 229 | } |
| 230 | |
| 231 | if (display_character == RTAS_UNKNOWN_SERVICE) { |
| 232 | /* use hex display if available */ |
| 233 | if (set_indicator != RTAS_UNKNOWN_SERVICE) |
| 234 | rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | spin_lock(&progress_lock); |
| 239 | |
| 240 | /* |
| 241 | * Last write ended with newline, but we didn't print it since |
| 242 | * it would just clear the bottom line of output. Print it now |
| 243 | * instead. |
| 244 | * |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 245 | * If no newline is pending and form feed is supported, clear the |
| 246 | * display with a form feed; otherwise, print a CR to start output |
| 247 | * at the beginning of the line. |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 248 | */ |
| 249 | if (pending_newline) { |
| 250 | rtas_call(display_character, 1, 1, NULL, '\r'); |
| 251 | rtas_call(display_character, 1, 1, NULL, '\n'); |
| 252 | pending_newline = 0; |
| 253 | } else { |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 254 | current_line = 0; |
| 255 | if (form_feed) |
| 256 | rtas_call(display_character, 1, 1, NULL, |
| 257 | (char)form_feed); |
| 258 | else |
| 259 | rtas_call(display_character, 1, 1, NULL, '\r'); |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 260 | } |
| 261 | |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 262 | if (row_width) |
| 263 | width = row_width[current_line]; |
| 264 | else |
| 265 | width = display_width; |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 266 | os = s; |
| 267 | while (*os) { |
| 268 | if (*os == '\n' || *os == '\r') { |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 269 | /* If newline is the last character, save it |
| 270 | * until next call to avoid bumping up the |
| 271 | * display output. |
| 272 | */ |
| 273 | if (*os == '\n' && !os[1]) { |
| 274 | pending_newline = 1; |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 275 | current_line++; |
| 276 | if (current_line > display_lines-1) |
| 277 | current_line = display_lines-1; |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 278 | spin_unlock(&progress_lock); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | /* RTAS wants CR-LF, not just LF */ |
| 283 | |
| 284 | if (*os == '\n') { |
| 285 | rtas_call(display_character, 1, 1, NULL, '\r'); |
| 286 | rtas_call(display_character, 1, 1, NULL, '\n'); |
| 287 | } else { |
| 288 | /* CR might be used to re-draw a line, so we'll |
| 289 | * leave it alone and not add LF. |
| 290 | */ |
| 291 | rtas_call(display_character, 1, 1, NULL, *os); |
| 292 | } |
| 293 | |
Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 294 | if (row_width) |
| 295 | width = row_width[current_line]; |
| 296 | else |
| 297 | width = display_width; |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 298 | } else { |
| 299 | width--; |
| 300 | rtas_call(display_character, 1, 1, NULL, *os); |
| 301 | } |
| 302 | |
| 303 | os++; |
| 304 | |
| 305 | /* if we overwrite the screen length */ |
| 306 | if (width <= 0) |
| 307 | while ((*os != 0) && (*os != '\n') && (*os != '\r')) |
| 308 | os++; |
| 309 | } |
| 310 | |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 311 | spin_unlock(&progress_lock); |
| 312 | } |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 313 | EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */ |
Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 314 | |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 315 | int rtas_token(const char *service) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
Anton Blanchard | 08bc1dc | 2013-08-07 02:01:29 +1000 | [diff] [blame] | 317 | const __be32 *tokp; |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 318 | if (rtas.dev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | return RTAS_UNKNOWN_SERVICE; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 320 | tokp = of_get_property(rtas.dev, service, NULL); |
Anton Blanchard | 08bc1dc | 2013-08-07 02:01:29 +1000 | [diff] [blame] | 321 | return tokp ? be32_to_cpu(*tokp) : RTAS_UNKNOWN_SERVICE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 323 | EXPORT_SYMBOL(rtas_token); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Nathan Lynch | f2d6d2d | 2006-12-06 18:50:45 -0600 | [diff] [blame] | 325 | int rtas_service_present(const char *service) |
| 326 | { |
| 327 | return rtas_token(service) != RTAS_UNKNOWN_SERVICE; |
| 328 | } |
| 329 | EXPORT_SYMBOL(rtas_service_present); |
| 330 | |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 331 | #ifdef CONFIG_RTAS_ERROR_LOGGING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | /* |
| 333 | * Return the firmware-specified size of the error log buffer |
| 334 | * for all rtas calls that require an error buffer argument. |
| 335 | * This includes 'check-exception' and 'rtas-last-error'. |
| 336 | */ |
| 337 | int rtas_get_error_log_max(void) |
| 338 | { |
| 339 | static int rtas_error_log_max; |
| 340 | if (rtas_error_log_max) |
| 341 | return rtas_error_log_max; |
| 342 | |
| 343 | rtas_error_log_max = rtas_token ("rtas-error-log-max"); |
| 344 | if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) || |
| 345 | (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) { |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 346 | printk (KERN_WARNING "RTAS: bad log buffer size %d\n", |
| 347 | rtas_error_log_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | rtas_error_log_max = RTAS_ERROR_LOG_MAX; |
| 349 | } |
| 350 | return rtas_error_log_max; |
| 351 | } |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 352 | EXPORT_SYMBOL(rtas_get_error_log_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | |
Michael Ellerman | 1c21a29 | 2008-05-08 14:27:19 +1000 | [diff] [blame] | 355 | static char rtas_err_buf[RTAS_ERROR_LOG_MAX]; |
| 356 | static int rtas_last_error_token; |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | /** Return a copy of the detailed error text associated with the |
| 359 | * most recent failed call to rtas. Because the error text |
| 360 | * might go stale if there are any other intervening rtas calls, |
| 361 | * this routine must be called atomically with whatever produced |
| 362 | * the error (i.e. with rtas.lock still held from the previous call). |
| 363 | */ |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 364 | static char *__fetch_rtas_last_error(char *altbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | { |
| 366 | struct rtas_args err_args, save_args; |
| 367 | u32 bufsz; |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 368 | char *buf = NULL; |
| 369 | |
| 370 | if (rtas_last_error_token == -1) |
| 371 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | bufsz = rtas_get_error_log_max(); |
| 374 | |
Anton Blanchard | 2712826 | 2013-08-07 02:01:31 +1000 | [diff] [blame] | 375 | err_args.token = cpu_to_be32(rtas_last_error_token); |
| 376 | err_args.nargs = cpu_to_be32(2); |
| 377 | err_args.nret = cpu_to_be32(1); |
| 378 | err_args.args[0] = cpu_to_be32(__pa(rtas_err_buf)); |
| 379 | err_args.args[1] = cpu_to_be32(bufsz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | err_args.args[2] = 0; |
| 381 | |
| 382 | save_args = rtas.args; |
| 383 | rtas.args = err_args; |
| 384 | |
| 385 | enter_rtas(__pa(&rtas.args)); |
| 386 | |
| 387 | err_args = rtas.args; |
| 388 | rtas.args = save_args; |
| 389 | |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 390 | /* Log the error in the unlikely case that there was one. */ |
| 391 | if (unlikely(err_args.args[2] == 0)) { |
| 392 | if (altbuf) { |
| 393 | buf = altbuf; |
| 394 | } else { |
| 395 | buf = rtas_err_buf; |
Michael Ellerman | f691fa1 | 2015-03-30 14:10:37 +1100 | [diff] [blame] | 396 | if (slab_is_available()) |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 397 | buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC); |
| 398 | } |
| 399 | if (buf) |
| 400 | memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX); |
| 401 | } |
| 402 | |
| 403 | return buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 406 | #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL) |
| 407 | |
| 408 | #else /* CONFIG_RTAS_ERROR_LOGGING */ |
| 409 | #define __fetch_rtas_last_error(x) NULL |
| 410 | #define get_errorlog_buffer() NULL |
| 411 | #endif |
| 412 | |
Michael Ellerman | 209eb4e | 2015-12-16 21:01:42 +1100 | [diff] [blame] | 413 | |
| 414 | static void |
| 415 | va_rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret, |
| 416 | va_list list) |
| 417 | { |
| 418 | int i; |
| 419 | |
| 420 | args->token = cpu_to_be32(token); |
| 421 | args->nargs = cpu_to_be32(nargs); |
| 422 | args->nret = cpu_to_be32(nret); |
| 423 | args->rets = &(args->args[nargs]); |
| 424 | |
| 425 | for (i = 0; i < nargs; ++i) |
| 426 | args->args[i] = cpu_to_be32(va_arg(list, __u32)); |
| 427 | |
| 428 | for (i = 0; i < nret; ++i) |
| 429 | args->rets[i] = 0; |
| 430 | |
| 431 | enter_rtas(__pa(args)); |
| 432 | } |
| 433 | |
| 434 | void rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret, ...) |
| 435 | { |
| 436 | va_list list; |
| 437 | |
| 438 | va_start(list, nret); |
| 439 | va_rtas_call_unlocked(args, token, nargs, nret, list); |
| 440 | va_end(list); |
| 441 | } |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | int rtas_call(int token, int nargs, int nret, int *outputs, ...) |
| 444 | { |
| 445 | va_list list; |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 446 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | unsigned long s; |
| 448 | struct rtas_args *rtas_args; |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 449 | char *buff_copy = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | int ret; |
| 451 | |
Michael Ellerman | 24da3dd | 2006-06-23 18:20:10 +1000 | [diff] [blame] | 452 | if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | return -1; |
| 454 | |
Benjamin Herrenschmidt | f97bb36 | 2009-06-16 16:42:49 +0000 | [diff] [blame] | 455 | s = lock_rtas(); |
Michael Ellerman | 209eb4e | 2015-12-16 21:01:42 +1100 | [diff] [blame] | 456 | |
| 457 | /* We use the global rtas args buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | rtas_args = &rtas.args; |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | va_start(list, outputs); |
Michael Ellerman | 209eb4e | 2015-12-16 21:01:42 +1100 | [diff] [blame] | 461 | va_rtas_call_unlocked(rtas_args, token, nargs, nret, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | va_end(list); |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | /* A -1 return code indicates that the last command couldn't |
| 465 | be completed due to a hardware error. */ |
Anton Blanchard | 2712826 | 2013-08-07 02:01:31 +1000 | [diff] [blame] | 466 | if (be32_to_cpu(rtas_args->rets[0]) == -1) |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 467 | buff_copy = __fetch_rtas_last_error(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
| 469 | if (nret > 1 && outputs != NULL) |
| 470 | for (i = 0; i < nret-1; ++i) |
Anton Blanchard | 2712826 | 2013-08-07 02:01:31 +1000 | [diff] [blame] | 471 | outputs[i] = be32_to_cpu(rtas_args->rets[i+1]); |
| 472 | ret = (nret > 0)? be32_to_cpu(rtas_args->rets[0]): 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Benjamin Herrenschmidt | f97bb36 | 2009-06-16 16:42:49 +0000 | [diff] [blame] | 474 | unlock_rtas(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | if (buff_copy) { |
| 477 | log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0); |
Michael Ellerman | f691fa1 | 2015-03-30 14:10:37 +1100 | [diff] [blame] | 478 | if (slab_is_available()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | kfree(buff_copy); |
| 480 | } |
| 481 | return ret; |
| 482 | } |
Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 483 | EXPORT_SYMBOL(rtas_call); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 485 | /* For RTAS_BUSY (-2), delay for 1 millisecond. For an extended busy status |
| 486 | * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | */ |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 488 | unsigned int rtas_busy_delay_time(int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 490 | int order; |
| 491 | unsigned int ms = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 493 | if (status == RTAS_BUSY) { |
| 494 | ms = 1; |
Thomas Huth | 9ef0319 | 2015-07-22 18:56:47 +0200 | [diff] [blame] | 495 | } else if (status >= RTAS_EXTENDED_DELAY_MIN && |
| 496 | status <= RTAS_EXTENDED_DELAY_MAX) { |
| 497 | order = status - RTAS_EXTENDED_DELAY_MIN; |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 498 | for (ms = 1; order > 0; order--) |
| 499 | ms *= 10; |
| 500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 502 | return ms; |
| 503 | } |
Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 504 | EXPORT_SYMBOL(rtas_busy_delay_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 506 | /* For an RTAS busy status code, perform the hinted delay. */ |
| 507 | unsigned int rtas_busy_delay(int status) |
| 508 | { |
| 509 | unsigned int ms; |
| 510 | |
| 511 | might_sleep(); |
| 512 | ms = rtas_busy_delay_time(status); |
Anton Blanchard | eca590f | 2011-04-07 01:54:07 +0000 | [diff] [blame] | 513 | if (ms && need_resched()) |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 514 | msleep(ms); |
| 515 | |
| 516 | return ms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 518 | EXPORT_SYMBOL(rtas_busy_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Michael Ellerman | 1c21a29 | 2008-05-08 14:27:19 +1000 | [diff] [blame] | 520 | static int rtas_error_rc(int rtas_rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | { |
| 522 | int rc; |
| 523 | |
| 524 | switch (rtas_rc) { |
| 525 | case -1: /* Hardware Error */ |
| 526 | rc = -EIO; |
| 527 | break; |
| 528 | case -3: /* Bad indicator/domain/etc */ |
| 529 | rc = -EINVAL; |
| 530 | break; |
| 531 | case -9000: /* Isolation error */ |
| 532 | rc = -EFAULT; |
| 533 | break; |
| 534 | case -9001: /* Outstanding TCE/PTE */ |
| 535 | rc = -EEXIST; |
| 536 | break; |
| 537 | case -9002: /* No usable slot */ |
| 538 | rc = -ENODEV; |
| 539 | break; |
| 540 | default: |
| 541 | printk(KERN_ERR "%s: unexpected RTAS error %d\n", |
Harvey Harrison | e48b1b4 | 2008-03-29 08:21:07 +1100 | [diff] [blame] | 542 | __func__, rtas_rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | rc = -ERANGE; |
| 544 | break; |
| 545 | } |
| 546 | return rc; |
| 547 | } |
| 548 | |
| 549 | int rtas_get_power_level(int powerdomain, int *level) |
| 550 | { |
| 551 | int token = rtas_token("get-power-level"); |
| 552 | int rc; |
| 553 | |
| 554 | if (token == RTAS_UNKNOWN_SERVICE) |
| 555 | return -ENOENT; |
| 556 | |
| 557 | while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY) |
| 558 | udelay(1); |
| 559 | |
| 560 | if (rc < 0) |
| 561 | return rtas_error_rc(rc); |
| 562 | return rc; |
| 563 | } |
Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 564 | EXPORT_SYMBOL(rtas_get_power_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | int rtas_set_power_level(int powerdomain, int level, int *setlevel) |
| 567 | { |
| 568 | int token = rtas_token("set-power-level"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | int rc; |
| 570 | |
| 571 | if (token == RTAS_UNKNOWN_SERVICE) |
| 572 | return -ENOENT; |
| 573 | |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 574 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | rc = rtas_call(token, 2, 2, setlevel, powerdomain, level); |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 576 | } while (rtas_busy_delay(rc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
| 578 | if (rc < 0) |
| 579 | return rtas_error_rc(rc); |
| 580 | return rc; |
| 581 | } |
Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 582 | EXPORT_SYMBOL(rtas_set_power_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
| 584 | int rtas_get_sensor(int sensor, int index, int *state) |
| 585 | { |
| 586 | int token = rtas_token("get-sensor-state"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | int rc; |
| 588 | |
| 589 | if (token == RTAS_UNKNOWN_SERVICE) |
| 590 | return -ENOENT; |
| 591 | |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 592 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | rc = rtas_call(token, 2, 2, state, sensor, index); |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 594 | } while (rtas_busy_delay(rc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | if (rc < 0) |
| 597 | return rtas_error_rc(rc); |
| 598 | return rc; |
| 599 | } |
Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 600 | EXPORT_SYMBOL(rtas_get_sensor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
Thomas Huth | 1c2cb59 | 2015-07-17 12:46:58 +0200 | [diff] [blame] | 602 | int rtas_get_sensor_fast(int sensor, int index, int *state) |
| 603 | { |
| 604 | int token = rtas_token("get-sensor-state"); |
| 605 | int rc; |
| 606 | |
| 607 | if (token == RTAS_UNKNOWN_SERVICE) |
| 608 | return -ENOENT; |
| 609 | |
| 610 | rc = rtas_call(token, 2, 2, state, sensor, index); |
| 611 | WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN && |
| 612 | rc <= RTAS_EXTENDED_DELAY_MAX)); |
| 613 | |
| 614 | if (rc < 0) |
| 615 | return rtas_error_rc(rc); |
| 616 | return rc; |
| 617 | } |
| 618 | |
Nathan Lynch | edc72ac | 2008-12-11 09:14:25 +0000 | [diff] [blame] | 619 | bool rtas_indicator_present(int token, int *maxindex) |
| 620 | { |
| 621 | int proplen, count, i; |
| 622 | const struct indicator_elem { |
Anton Blanchard | 08bc1dc | 2013-08-07 02:01:29 +1000 | [diff] [blame] | 623 | __be32 token; |
| 624 | __be32 maxindex; |
Nathan Lynch | edc72ac | 2008-12-11 09:14:25 +0000 | [diff] [blame] | 625 | } *indicators; |
| 626 | |
| 627 | indicators = of_get_property(rtas.dev, "rtas-indicators", &proplen); |
| 628 | if (!indicators) |
| 629 | return false; |
| 630 | |
| 631 | count = proplen / sizeof(struct indicator_elem); |
| 632 | |
| 633 | for (i = 0; i < count; i++) { |
Anton Blanchard | 08bc1dc | 2013-08-07 02:01:29 +1000 | [diff] [blame] | 634 | if (__be32_to_cpu(indicators[i].token) != token) |
Nathan Lynch | edc72ac | 2008-12-11 09:14:25 +0000 | [diff] [blame] | 635 | continue; |
| 636 | if (maxindex) |
Anton Blanchard | 08bc1dc | 2013-08-07 02:01:29 +1000 | [diff] [blame] | 637 | *maxindex = __be32_to_cpu(indicators[i].maxindex); |
Nathan Lynch | edc72ac | 2008-12-11 09:14:25 +0000 | [diff] [blame] | 638 | return true; |
| 639 | } |
| 640 | |
| 641 | return false; |
| 642 | } |
| 643 | EXPORT_SYMBOL(rtas_indicator_present); |
| 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | int rtas_set_indicator(int indicator, int index, int new_value) |
| 646 | { |
| 647 | int token = rtas_token("set-indicator"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | int rc; |
| 649 | |
| 650 | if (token == RTAS_UNKNOWN_SERVICE) |
| 651 | return -ENOENT; |
| 652 | |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 653 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value); |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 655 | } while (rtas_busy_delay(rc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
| 657 | if (rc < 0) |
| 658 | return rtas_error_rc(rc); |
| 659 | return rc; |
| 660 | } |
Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 661 | EXPORT_SYMBOL(rtas_set_indicator); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Haren Myneni | 81b73dd | 2006-07-27 14:29:00 -0700 | [diff] [blame] | 663 | /* |
| 664 | * Ignoring RTAS extended delay |
| 665 | */ |
| 666 | int rtas_set_indicator_fast(int indicator, int index, int new_value) |
| 667 | { |
| 668 | int rc; |
| 669 | int token = rtas_token("set-indicator"); |
| 670 | |
| 671 | if (token == RTAS_UNKNOWN_SERVICE) |
| 672 | return -ENOENT; |
| 673 | |
| 674 | rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value); |
| 675 | |
Thomas Huth | 9ef0319 | 2015-07-22 18:56:47 +0200 | [diff] [blame] | 676 | WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN && |
| 677 | rc <= RTAS_EXTENDED_DELAY_MAX)); |
Haren Myneni | 81b73dd | 2006-07-27 14:29:00 -0700 | [diff] [blame] | 678 | |
| 679 | if (rc < 0) |
| 680 | return rtas_error_rc(rc); |
| 681 | |
| 682 | return rc; |
| 683 | } |
| 684 | |
Daniel Axtens | 95ec77c | 2016-07-12 10:54:52 +1000 | [diff] [blame] | 685 | void __noreturn rtas_restart(char *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | { |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 687 | if (rtas_flash_term_hook) |
| 688 | rtas_flash_term_hook(SYS_RESTART); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | printk("RTAS system-reboot returned %d\n", |
| 690 | rtas_call(rtas_token("system-reboot"), 0, 1, NULL)); |
| 691 | for (;;); |
| 692 | } |
| 693 | |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 694 | void rtas_power_off(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 696 | if (rtas_flash_term_hook) |
| 697 | rtas_flash_term_hook(SYS_POWER_OFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | /* allow power on only with power button press */ |
| 699 | printk("RTAS power-off returned %d\n", |
| 700 | rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1)); |
| 701 | for (;;); |
| 702 | } |
| 703 | |
Daniel Axtens | 95ec77c | 2016-07-12 10:54:52 +1000 | [diff] [blame] | 704 | void __noreturn rtas_halt(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 706 | if (rtas_flash_term_hook) |
| 707 | rtas_flash_term_hook(SYS_HALT); |
| 708 | /* allow power on only with power button press */ |
| 709 | printk("RTAS power-off returned %d\n", |
| 710 | rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1)); |
| 711 | for (;;); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /* Must be in the RMO region, so we place it here */ |
| 715 | static char rtas_os_term_buf[2048]; |
| 716 | |
Paul Mackerras | 8f51506 | 2007-12-03 09:30:04 +1100 | [diff] [blame] | 717 | void rtas_os_term(char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | { |
| 719 | int status; |
| 720 | |
Anton Blanchard | e9bbc8c | 2010-02-18 12:11:51 +0000 | [diff] [blame] | 721 | /* |
| 722 | * Firmware with the ibm,extended-os-term property is guaranteed |
| 723 | * to always return from an ibm,os-term call. Earlier versions without |
| 724 | * this property may terminate the partition which we want to avoid |
| 725 | * since it interferes with panic_timeout. |
| 726 | */ |
| 727 | if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term") || |
| 728 | RTAS_UNKNOWN_SERVICE == rtas_token("ibm,extended-os-term")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | return; |
| 730 | |
Paul Mackerras | 8f51506 | 2007-12-03 09:30:04 +1100 | [diff] [blame] | 731 | snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str); |
| 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | do { |
| 734 | status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL, |
| 735 | __pa(rtas_os_term_buf)); |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 736 | } while (rtas_busy_delay(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 738 | if (status != 0) |
Anton Blanchard | e9bbc8c | 2010-02-18 12:11:51 +0000 | [diff] [blame] | 739 | printk(KERN_EMERG "ibm,os-term call failed %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 742 | static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE; |
| 743 | #ifdef CONFIG_PPC_PSERIES |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 744 | static int __rtas_suspend_last_cpu(struct rtas_suspend_me_data *data, int wake_when_done) |
| 745 | { |
| 746 | u16 slb_size = mmu_slb_size; |
| 747 | int rc = H_MULTI_THREADS_ACTIVE; |
| 748 | int cpu; |
| 749 | |
| 750 | slb_set_size(SLB_MIN_SIZE); |
| 751 | printk(KERN_DEBUG "calling ibm,suspend-me on cpu %i\n", smp_processor_id()); |
| 752 | |
| 753 | while (rc == H_MULTI_THREADS_ACTIVE && !atomic_read(&data->done) && |
| 754 | !atomic_read(&data->error)) |
| 755 | rc = rtas_call(data->token, 0, 1, NULL); |
| 756 | |
| 757 | if (rc || atomic_read(&data->error)) { |
| 758 | printk(KERN_DEBUG "ibm,suspend-me returned %d\n", rc); |
| 759 | slb_set_size(slb_size); |
| 760 | } |
| 761 | |
| 762 | if (atomic_read(&data->error)) |
| 763 | rc = atomic_read(&data->error); |
| 764 | |
| 765 | atomic_set(&data->error, rc); |
Brian King | 9ee820f | 2011-05-04 16:01:20 +1000 | [diff] [blame] | 766 | pSeries_coalesce_init(); |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 767 | |
| 768 | if (wake_when_done) { |
| 769 | atomic_set(&data->done, 1); |
| 770 | |
| 771 | for_each_online_cpu(cpu) |
| 772 | plpar_hcall_norets(H_PROD, get_hard_smp_processor_id(cpu)); |
| 773 | } |
| 774 | |
| 775 | if (atomic_dec_return(&data->working) == 0) |
| 776 | complete(data->complete); |
| 777 | |
| 778 | return rc; |
| 779 | } |
| 780 | |
| 781 | int rtas_suspend_last_cpu(struct rtas_suspend_me_data *data) |
| 782 | { |
| 783 | atomic_inc(&data->working); |
| 784 | return __rtas_suspend_last_cpu(data, 0); |
| 785 | } |
| 786 | |
| 787 | static int __rtas_suspend_cpu(struct rtas_suspend_me_data *data, int wake_when_done) |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 788 | { |
Brian King | f52862f | 2009-02-17 06:49:50 +0000 | [diff] [blame] | 789 | long rc = H_SUCCESS; |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 790 | unsigned long msr_save; |
| 791 | int cpu; |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 792 | |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 793 | atomic_inc(&data->working); |
| 794 | |
| 795 | /* really need to ensure MSR.EE is off for H_JOIN */ |
| 796 | msr_save = mfmsr(); |
| 797 | mtmsr(msr_save & ~(MSR_EE)); |
| 798 | |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 799 | while (rc == H_SUCCESS && !atomic_read(&data->done) && !atomic_read(&data->error)) |
Brian King | f52862f | 2009-02-17 06:49:50 +0000 | [diff] [blame] | 800 | rc = plpar_hcall_norets(H_JOIN); |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 801 | |
| 802 | mtmsr(msr_save); |
| 803 | |
| 804 | if (rc == H_SUCCESS) { |
| 805 | /* This cpu was prodded and the suspend is complete. */ |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 806 | goto out; |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 807 | } else if (rc == H_CONTINUE) { |
| 808 | /* All other cpus are in H_JOIN, this cpu does |
| 809 | * the suspend. |
| 810 | */ |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 811 | return __rtas_suspend_last_cpu(data, wake_when_done); |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 812 | } else { |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 813 | printk(KERN_ERR "H_JOIN on cpu %i failed with rc = %ld\n", |
| 814 | smp_processor_id(), rc); |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 815 | atomic_set(&data->error, rc); |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 816 | } |
Brian King | f52862f | 2009-02-17 06:49:50 +0000 | [diff] [blame] | 817 | |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 818 | if (wake_when_done) { |
| 819 | atomic_set(&data->done, 1); |
Brian King | f52862f | 2009-02-17 06:49:50 +0000 | [diff] [blame] | 820 | |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 821 | /* This cpu did the suspend or got an error; in either case, |
| 822 | * we need to prod all other other cpus out of join state. |
| 823 | * Extra prods are harmless. |
| 824 | */ |
| 825 | for_each_online_cpu(cpu) |
| 826 | plpar_hcall_norets(H_PROD, get_hard_smp_processor_id(cpu)); |
| 827 | } |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 828 | out: |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 829 | if (atomic_dec_return(&data->working) == 0) |
| 830 | complete(data->complete); |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 831 | return rc; |
| 832 | } |
| 833 | |
| 834 | int rtas_suspend_cpu(struct rtas_suspend_me_data *data) |
| 835 | { |
| 836 | return __rtas_suspend_cpu(data, 0); |
| 837 | } |
| 838 | |
| 839 | static void rtas_percpu_suspend_me(void *info) |
| 840 | { |
| 841 | __rtas_suspend_cpu((struct rtas_suspend_me_data *)info, 1); |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 842 | } |
| 843 | |
Robert Jennings | 120496a | 2013-05-07 04:34:11 +0000 | [diff] [blame] | 844 | enum rtas_cpu_state { |
| 845 | DOWN, |
| 846 | UP, |
| 847 | }; |
| 848 | |
| 849 | #ifndef CONFIG_SMP |
| 850 | static int rtas_cpu_state_change_mask(enum rtas_cpu_state state, |
| 851 | cpumask_var_t cpus) |
| 852 | { |
| 853 | if (!cpumask_empty(cpus)) { |
| 854 | cpumask_clear(cpus); |
| 855 | return -EINVAL; |
| 856 | } else |
| 857 | return 0; |
| 858 | } |
| 859 | #else |
| 860 | /* On return cpumask will be altered to indicate CPUs changed. |
| 861 | * CPUs with states changed will be set in the mask, |
| 862 | * CPUs with status unchanged will be unset in the mask. */ |
| 863 | static int rtas_cpu_state_change_mask(enum rtas_cpu_state state, |
| 864 | cpumask_var_t cpus) |
| 865 | { |
| 866 | int cpu; |
| 867 | int cpuret = 0; |
| 868 | int ret = 0; |
| 869 | |
| 870 | if (cpumask_empty(cpus)) |
| 871 | return 0; |
| 872 | |
| 873 | for_each_cpu(cpu, cpus) { |
| 874 | switch (state) { |
| 875 | case DOWN: |
| 876 | cpuret = cpu_down(cpu); |
| 877 | break; |
| 878 | case UP: |
| 879 | cpuret = cpu_up(cpu); |
| 880 | break; |
| 881 | } |
| 882 | if (cpuret) { |
| 883 | pr_debug("%s: cpu_%s for cpu#%d returned %d.\n", |
| 884 | __func__, |
| 885 | ((state == UP) ? "up" : "down"), |
| 886 | cpu, cpuret); |
| 887 | if (!ret) |
| 888 | ret = cpuret; |
| 889 | if (state == UP) { |
| 890 | /* clear bits for unchanged cpus, return */ |
| 891 | cpumask_shift_right(cpus, cpus, cpu); |
| 892 | cpumask_shift_left(cpus, cpus, cpu); |
| 893 | break; |
| 894 | } else { |
| 895 | /* clear bit for unchanged cpu, continue */ |
| 896 | cpumask_clear_cpu(cpu, cpus); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | return ret; |
| 902 | } |
| 903 | #endif |
| 904 | |
| 905 | int rtas_online_cpus_mask(cpumask_var_t cpus) |
| 906 | { |
| 907 | int ret; |
| 908 | |
| 909 | ret = rtas_cpu_state_change_mask(UP, cpus); |
| 910 | |
| 911 | if (ret) { |
| 912 | cpumask_var_t tmp_mask; |
| 913 | |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 914 | if (!alloc_cpumask_var(&tmp_mask, GFP_KERNEL)) |
Robert Jennings | 120496a | 2013-05-07 04:34:11 +0000 | [diff] [blame] | 915 | return ret; |
| 916 | |
| 917 | /* Use tmp_mask to preserve cpus mask from first failure */ |
| 918 | cpumask_copy(tmp_mask, cpus); |
| 919 | rtas_offline_cpus_mask(tmp_mask); |
| 920 | free_cpumask_var(tmp_mask); |
| 921 | } |
| 922 | |
| 923 | return ret; |
| 924 | } |
| 925 | EXPORT_SYMBOL(rtas_online_cpus_mask); |
| 926 | |
| 927 | int rtas_offline_cpus_mask(cpumask_var_t cpus) |
| 928 | { |
| 929 | return rtas_cpu_state_change_mask(DOWN, cpus); |
| 930 | } |
| 931 | EXPORT_SYMBOL(rtas_offline_cpus_mask); |
| 932 | |
Tyrel Datwyler | c03e737 | 2015-03-27 12:47:25 -0700 | [diff] [blame] | 933 | int rtas_ibm_suspend_me(u64 handle) |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 934 | { |
Dave C Boutcher | 368a6ba | 2006-06-12 19:49:20 -0500 | [diff] [blame] | 935 | long state; |
| 936 | long rc; |
Anton Blanchard | b9377ff | 2006-07-19 08:01:28 +1000 | [diff] [blame] | 937 | unsigned long retbuf[PLPAR_HCALL_BUFSIZE]; |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 938 | struct rtas_suspend_me_data data; |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 939 | DECLARE_COMPLETION_ONSTACK(done); |
Robert Jennings | 120496a | 2013-05-07 04:34:11 +0000 | [diff] [blame] | 940 | cpumask_var_t offline_mask; |
| 941 | int cpuret; |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 942 | |
| 943 | if (!rtas_service_present("ibm,suspend-me")) |
| 944 | return -ENOSYS; |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 945 | |
Dave C Boutcher | 368a6ba | 2006-06-12 19:49:20 -0500 | [diff] [blame] | 946 | /* Make sure the state is valid */ |
Cyril Bur | 3df76a9 | 2015-01-21 13:32:00 +1100 | [diff] [blame] | 947 | rc = plpar_hcall(H_VASI_STATE, retbuf, handle); |
Anton Blanchard | b9377ff | 2006-07-19 08:01:28 +1000 | [diff] [blame] | 948 | |
| 949 | state = retbuf[0]; |
Dave C Boutcher | 368a6ba | 2006-06-12 19:49:20 -0500 | [diff] [blame] | 950 | |
| 951 | if (rc) { |
| 952 | printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc); |
| 953 | return rc; |
| 954 | } else if (state == H_VASI_ENABLED) { |
Tyrel Datwyler | c03e737 | 2015-03-27 12:47:25 -0700 | [diff] [blame] | 955 | return -EAGAIN; |
Dave C Boutcher | 368a6ba | 2006-06-12 19:49:20 -0500 | [diff] [blame] | 956 | } else if (state != H_VASI_SUSPENDING) { |
| 957 | printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n", |
| 958 | state); |
Tyrel Datwyler | c03e737 | 2015-03-27 12:47:25 -0700 | [diff] [blame] | 959 | return -EIO; |
Dave C Boutcher | 368a6ba | 2006-06-12 19:49:20 -0500 | [diff] [blame] | 960 | } |
| 961 | |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 962 | if (!alloc_cpumask_var(&offline_mask, GFP_KERNEL)) |
Robert Jennings | 120496a | 2013-05-07 04:34:11 +0000 | [diff] [blame] | 963 | return -ENOMEM; |
| 964 | |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 965 | atomic_set(&data.working, 0); |
Brian King | f52862f | 2009-02-17 06:49:50 +0000 | [diff] [blame] | 966 | atomic_set(&data.done, 0); |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 967 | atomic_set(&data.error, 0); |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 968 | data.token = rtas_token("ibm,suspend-me"); |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 969 | data.complete = &done; |
Robert Jennings | 120496a | 2013-05-07 04:34:11 +0000 | [diff] [blame] | 970 | |
| 971 | /* All present CPUs must be online */ |
| 972 | cpumask_andnot(offline_mask, cpu_present_mask, cpu_online_mask); |
| 973 | cpuret = rtas_online_cpus_mask(offline_mask); |
| 974 | if (cpuret) { |
| 975 | pr_err("%s: Could not bring present CPUs online.\n", __func__); |
| 976 | atomic_set(&data.error, cpuret); |
| 977 | goto out; |
| 978 | } |
| 979 | |
Nathan Fontenot | 85a88ca | 2018-09-17 14:14:02 -0500 | [diff] [blame] | 980 | cpu_hotplug_disable(); |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 981 | |
Gautham R. Shenoy | dfd718a | 2018-10-01 16:10:39 +0530 | [diff] [blame] | 982 | /* Check if we raced with a CPU-Offline Operation */ |
| 983 | if (unlikely(!cpumask_equal(cpu_present_mask, cpu_online_mask))) { |
| 984 | pr_err("%s: Raced against a concurrent CPU-Offline\n", |
| 985 | __func__); |
| 986 | atomic_set(&data.error, -EBUSY); |
| 987 | goto out_hotplug_enable; |
| 988 | } |
| 989 | |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 990 | /* Call function on all CPUs. One of us will make the |
| 991 | * rtas call |
| 992 | */ |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 993 | if (on_each_cpu(rtas_percpu_suspend_me, &data, 0)) |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 994 | atomic_set(&data.error, -EINVAL); |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 995 | |
Nathan Lynch | 8f5c757 | 2007-11-14 03:15:13 +1100 | [diff] [blame] | 996 | wait_for_completion(&done); |
| 997 | |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 998 | if (atomic_read(&data.error) != 0) |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 999 | printk(KERN_ERR "Error doing global join\n"); |
| 1000 | |
Gautham R. Shenoy | dfd718a | 2018-10-01 16:10:39 +0530 | [diff] [blame] | 1001 | out_hotplug_enable: |
Nathan Fontenot | 85a88ca | 2018-09-17 14:14:02 -0500 | [diff] [blame] | 1002 | cpu_hotplug_enable(); |
Brian King | 444080d | 2012-01-11 06:56:04 +0000 | [diff] [blame] | 1003 | |
Robert Jennings | 120496a | 2013-05-07 04:34:11 +0000 | [diff] [blame] | 1004 | /* Take down CPUs not online prior to suspend */ |
| 1005 | cpuret = rtas_offline_cpus_mask(offline_mask); |
| 1006 | if (cpuret) |
| 1007 | pr_warn("%s: Could not restore CPUs to offline state.\n", |
| 1008 | __func__); |
| 1009 | |
| 1010 | out: |
| 1011 | free_cpumask_var(offline_mask); |
Brian King | 8fe93f8 | 2010-07-07 12:31:01 +0000 | [diff] [blame] | 1012 | return atomic_read(&data.error); |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 1013 | } |
| 1014 | #else /* CONFIG_PPC_PSERIES */ |
Tyrel Datwyler | c03e737 | 2015-03-27 12:47:25 -0700 | [diff] [blame] | 1015 | int rtas_ibm_suspend_me(u64 handle) |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 1016 | { |
| 1017 | return -ENOSYS; |
| 1018 | } |
| 1019 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
Anton Blanchard | 6431f20 | 2012-03-21 15:47:07 +0000 | [diff] [blame] | 1021 | /** |
| 1022 | * Find a specific pseries error log in an RTAS extended event log. |
| 1023 | * @log: RTAS error/event log |
| 1024 | * @section_id: two character section identifier |
| 1025 | * |
| 1026 | * Returns a pointer to the specified errorlog or NULL if not found. |
| 1027 | */ |
| 1028 | struct pseries_errorlog *get_pseries_errorlog(struct rtas_error_log *log, |
| 1029 | uint16_t section_id) |
| 1030 | { |
| 1031 | struct rtas_ext_event_log_v6 *ext_log = |
| 1032 | (struct rtas_ext_event_log_v6 *)log->buffer; |
| 1033 | struct pseries_errorlog *sect; |
| 1034 | unsigned char *p, *log_end; |
Greg Kurz | a08a53ea | 2014-04-04 09:35:13 +0200 | [diff] [blame] | 1035 | uint32_t ext_log_length = rtas_error_extended_log_length(log); |
| 1036 | uint8_t log_format = rtas_ext_event_log_format(ext_log); |
| 1037 | uint32_t company_id = rtas_ext_event_company_id(ext_log); |
Anton Blanchard | 6431f20 | 2012-03-21 15:47:07 +0000 | [diff] [blame] | 1038 | |
| 1039 | /* Check that we understand the format */ |
Greg Kurz | a08a53ea | 2014-04-04 09:35:13 +0200 | [diff] [blame] | 1040 | if (ext_log_length < sizeof(struct rtas_ext_event_log_v6) || |
| 1041 | log_format != RTAS_V6EXT_LOG_FORMAT_EVENT_LOG || |
| 1042 | company_id != RTAS_V6EXT_COMPANY_ID_IBM) |
Anton Blanchard | 6431f20 | 2012-03-21 15:47:07 +0000 | [diff] [blame] | 1043 | return NULL; |
| 1044 | |
Greg Kurz | a08a53ea | 2014-04-04 09:35:13 +0200 | [diff] [blame] | 1045 | log_end = log->buffer + ext_log_length; |
Anton Blanchard | 6431f20 | 2012-03-21 15:47:07 +0000 | [diff] [blame] | 1046 | p = ext_log->vendor_log; |
| 1047 | |
| 1048 | while (p < log_end) { |
| 1049 | sect = (struct pseries_errorlog *)p; |
Greg Kurz | a08a53ea | 2014-04-04 09:35:13 +0200 | [diff] [blame] | 1050 | if (pseries_errorlog_id(sect) == section_id) |
Anton Blanchard | 6431f20 | 2012-03-21 15:47:07 +0000 | [diff] [blame] | 1051 | return sect; |
Greg Kurz | a08a53ea | 2014-04-04 09:35:13 +0200 | [diff] [blame] | 1052 | p += pseries_errorlog_length(sect); |
Anton Blanchard | 6431f20 | 2012-03-21 15:47:07 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | return NULL; |
| 1056 | } |
| 1057 | |
Greg Kurz | 599d287 | 2014-03-19 17:02:51 +0100 | [diff] [blame] | 1058 | /* We assume to be passed big endian arguments */ |
Al Viro | 4c392e6 | 2018-05-02 23:20:48 +1000 | [diff] [blame] | 1059 | SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | { |
| 1061 | struct rtas_args args; |
| 1062 | unsigned long flags; |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 1063 | char *buff_copy, *errbuf = NULL; |
Greg Kurz | 599d287 | 2014-03-19 17:02:51 +0100 | [diff] [blame] | 1064 | int nargs, nret, token; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | |
| 1066 | if (!capable(CAP_SYS_ADMIN)) |
| 1067 | return -EPERM; |
| 1068 | |
Vasant Hegde | 8832317 | 2015-10-16 15:53:29 +0530 | [diff] [blame] | 1069 | if (!rtas.entry) |
| 1070 | return -EINVAL; |
| 1071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0) |
| 1073 | return -EFAULT; |
| 1074 | |
Greg Kurz | 599d287 | 2014-03-19 17:02:51 +0100 | [diff] [blame] | 1075 | nargs = be32_to_cpu(args.nargs); |
| 1076 | nret = be32_to_cpu(args.nret); |
| 1077 | token = be32_to_cpu(args.token); |
| 1078 | |
Andrew Donnellan | a9862c7 | 2016-03-18 17:36:33 +1100 | [diff] [blame] | 1079 | if (nargs >= ARRAY_SIZE(args.args) |
Greg Kurz | 599d287 | 2014-03-19 17:02:51 +0100 | [diff] [blame] | 1080 | || nret > ARRAY_SIZE(args.args) |
| 1081 | || nargs + nret > ARRAY_SIZE(args.args)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | return -EINVAL; |
| 1083 | |
| 1084 | /* Copy in args. */ |
| 1085 | if (copy_from_user(args.args, uargs->args, |
| 1086 | nargs * sizeof(rtas_arg_t)) != 0) |
| 1087 | return -EFAULT; |
| 1088 | |
Greg Kurz | 599d287 | 2014-03-19 17:02:51 +0100 | [diff] [blame] | 1089 | if (token == RTAS_UNKNOWN_SERVICE) |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 1090 | return -EINVAL; |
| 1091 | |
Nathan Fontenot | b79998f | 2008-07-31 02:23:27 +1000 | [diff] [blame] | 1092 | args.rets = &args.args[nargs]; |
Greg Kurz | 599d287 | 2014-03-19 17:02:51 +0100 | [diff] [blame] | 1093 | memset(args.rets, 0, nret * sizeof(rtas_arg_t)); |
Nathan Fontenot | b79998f | 2008-07-31 02:23:27 +1000 | [diff] [blame] | 1094 | |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 1095 | /* Need to handle ibm,suspend_me call specially */ |
Greg Kurz | 599d287 | 2014-03-19 17:02:51 +0100 | [diff] [blame] | 1096 | if (token == ibm_suspend_me_token) { |
Cyril Bur | 3df76a9 | 2015-01-21 13:32:00 +1100 | [diff] [blame] | 1097 | |
| 1098 | /* |
Tyrel Datwyler | c03e737 | 2015-03-27 12:47:25 -0700 | [diff] [blame] | 1099 | * rtas_ibm_suspend_me assumes the streamid handle is in cpu |
| 1100 | * endian, or at least the hcall within it requires it. |
Cyril Bur | 3df76a9 | 2015-01-21 13:32:00 +1100 | [diff] [blame] | 1101 | */ |
Tyrel Datwyler | c03e737 | 2015-03-27 12:47:25 -0700 | [diff] [blame] | 1102 | int rc = 0; |
Cyril Bur | 3df76a9 | 2015-01-21 13:32:00 +1100 | [diff] [blame] | 1103 | u64 handle = ((u64)be32_to_cpu(args.args[0]) << 32) |
| 1104 | | be32_to_cpu(args.args[1]); |
Tyrel Datwyler | c03e737 | 2015-03-27 12:47:25 -0700 | [diff] [blame] | 1105 | rc = rtas_ibm_suspend_me(handle); |
| 1106 | if (rc == -EAGAIN) |
| 1107 | args.rets[0] = cpu_to_be32(RTAS_NOT_SUSPENDABLE); |
| 1108 | else if (rc == -EIO) |
| 1109 | args.rets[0] = cpu_to_be32(-1); |
| 1110 | else if (rc) |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 1111 | return rc; |
| 1112 | goto copy_return; |
| 1113 | } |
| 1114 | |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 1115 | buff_copy = get_errorlog_buffer(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
Benjamin Herrenschmidt | f97bb36 | 2009-06-16 16:42:49 +0000 | [diff] [blame] | 1117 | flags = lock_rtas(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | |
| 1119 | rtas.args = args; |
| 1120 | enter_rtas(__pa(&rtas.args)); |
| 1121 | args = rtas.args; |
| 1122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | /* A -1 return code indicates that the last command couldn't |
| 1124 | be completed due to a hardware error. */ |
Greg Kurz | 599d287 | 2014-03-19 17:02:51 +0100 | [diff] [blame] | 1125 | if (be32_to_cpu(args.rets[0]) == -1) |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 1126 | errbuf = __fetch_rtas_last_error(buff_copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
Benjamin Herrenschmidt | f97bb36 | 2009-06-16 16:42:49 +0000 | [diff] [blame] | 1128 | unlock_rtas(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | |
| 1130 | if (buff_copy) { |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 1131 | if (errbuf) |
| 1132 | log_error(errbuf, ERR_TYPE_RTAS_LOG, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | kfree(buff_copy); |
| 1134 | } |
| 1135 | |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 1136 | copy_return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | /* Copy out args. */ |
| 1138 | if (copy_to_user(uargs->args + nargs, |
| 1139 | args.args + nargs, |
Greg Kurz | 599d287 | 2014-03-19 17:02:51 +0100 | [diff] [blame] | 1140 | nret * sizeof(rtas_arg_t)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | return -EFAULT; |
| 1142 | |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | /* |
Anton Blanchard | 14ed740 | 2014-09-17 22:15:34 +1000 | [diff] [blame] | 1147 | * Call early during boot, before mem init, to retrieve the RTAS |
| 1148 | * information from the device-tree and allocate the RMO buffer for userland |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | * accesses. |
| 1150 | */ |
| 1151 | void __init rtas_initialize(void) |
| 1152 | { |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 1153 | unsigned long rtas_region = RTAS_INSTANTIATE_MAX; |
Gavin Shan | de6d2d1 | 2017-01-24 09:49:53 +1100 | [diff] [blame] | 1154 | u32 base, size, entry; |
| 1155 | int no_base, no_size, no_entry; |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 1156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | /* Get RTAS dev node and fill up our "rtas" structure with infos |
| 1158 | * about it. |
| 1159 | */ |
| 1160 | rtas.dev = of_find_node_by_name(NULL, "rtas"); |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 1161 | if (!rtas.dev) |
| 1162 | return; |
| 1163 | |
Gavin Shan | de6d2d1 | 2017-01-24 09:49:53 +1100 | [diff] [blame] | 1164 | no_base = of_property_read_u32(rtas.dev, "linux,rtas-base", &base); |
| 1165 | no_size = of_property_read_u32(rtas.dev, "rtas-size", &size); |
| 1166 | if (no_base || no_size) { |
Gavin Shan | 8b25778 | 2017-01-24 09:49:54 +1100 | [diff] [blame] | 1167 | of_node_put(rtas.dev); |
Gavin Shan | dbecd50 | 2017-01-24 09:49:52 +1100 | [diff] [blame] | 1168 | rtas.dev = NULL; |
| 1169 | return; |
| 1170 | } |
| 1171 | |
Gavin Shan | de6d2d1 | 2017-01-24 09:49:53 +1100 | [diff] [blame] | 1172 | rtas.base = base; |
| 1173 | rtas.size = size; |
| 1174 | no_entry = of_property_read_u32(rtas.dev, "linux,rtas-entry", &entry); |
| 1175 | rtas.entry = no_entry ? rtas.base : entry; |
Gavin Shan | dbecd50 | 2017-01-24 09:49:52 +1100 | [diff] [blame] | 1176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | /* If RTAS was found, allocate the RMO buffer for it and look for |
| 1178 | * the stop-self token if any |
| 1179 | */ |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 1180 | #ifdef CONFIG_PPC64 |
Benjamin Herrenschmidt | 484cc1ed | 2016-07-05 15:03:54 +1000 | [diff] [blame] | 1181 | if (firmware_has_feature(FW_FEATURE_LPAR)) { |
Benjamin Herrenschmidt | cd3db0c | 2010-07-06 15:39:02 -0700 | [diff] [blame] | 1182 | rtas_region = min(ppc64_rma_size, RTAS_INSTANTIATE_MAX); |
Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 1183 | ibm_suspend_me_token = rtas_token("ibm,suspend-me"); |
| 1184 | } |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 1185 | #endif |
Mike Rapoport | 0ba9e6e | 2019-03-11 23:29:35 -0700 | [diff] [blame] | 1186 | rtas_rmo_buf = memblock_phys_alloc_range(RTAS_RMOBUF_MAX, PAGE_SIZE, |
| 1187 | 0, rtas_region); |
| 1188 | if (!rtas_rmo_buf) |
| 1189 | panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n", |
| 1190 | PAGE_SIZE, &rtas_region); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | |
Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 1192 | #ifdef CONFIG_RTAS_ERROR_LOGGING |
| 1193 | rtas_last_error_token = rtas_token("rtas-last-error"); |
| 1194 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
Michael Ellerman | 458148c | 2006-06-23 18:20:13 +1000 | [diff] [blame] | 1196 | |
| 1197 | int __init early_init_dt_scan_rtas(unsigned long node, |
| 1198 | const char *uname, int depth, void *data) |
| 1199 | { |
Rob Herring | 9d0c4df | 2014-04-01 23:49:03 -0500 | [diff] [blame] | 1200 | const u32 *basep, *entryp, *sizep; |
Michael Ellerman | 458148c | 2006-06-23 18:20:13 +1000 | [diff] [blame] | 1201 | |
| 1202 | if (depth != 1 || strcmp(uname, "rtas") != 0) |
| 1203 | return 0; |
| 1204 | |
| 1205 | basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL); |
| 1206 | entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL); |
| 1207 | sizep = of_get_flat_dt_prop(node, "rtas-size", NULL); |
| 1208 | |
| 1209 | if (basep && entryp && sizep) { |
| 1210 | rtas.base = *basep; |
| 1211 | rtas.entry = *entryp; |
| 1212 | rtas.size = *sizep; |
| 1213 | } |
| 1214 | |
Michael Ellerman | cc46bb9 | 2006-06-23 18:20:16 +1000 | [diff] [blame] | 1215 | #ifdef CONFIG_UDBG_RTAS_CONSOLE |
| 1216 | basep = of_get_flat_dt_prop(node, "put-term-char", NULL); |
| 1217 | if (basep) |
| 1218 | rtas_putchar_token = *basep; |
| 1219 | |
| 1220 | basep = of_get_flat_dt_prop(node, "get-term-char", NULL); |
| 1221 | if (basep) |
| 1222 | rtas_getchar_token = *basep; |
Michael Neuling | 9a2ded5 | 2006-08-16 23:12:14 -0500 | [diff] [blame] | 1223 | |
| 1224 | if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE && |
| 1225 | rtas_getchar_token != RTAS_UNKNOWN_SERVICE) |
| 1226 | udbg_init_rtas_console(); |
| 1227 | |
Michael Ellerman | cc46bb9 | 2006-06-23 18:20:16 +1000 | [diff] [blame] | 1228 | #endif |
| 1229 | |
Michael Ellerman | 458148c | 2006-06-23 18:20:13 +1000 | [diff] [blame] | 1230 | /* break now */ |
| 1231 | return 1; |
| 1232 | } |
Benjamin Herrenschmidt | c4007a2 | 2009-06-16 16:42:50 +0000 | [diff] [blame] | 1233 | |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 1234 | static arch_spinlock_t timebase_lock; |
Benjamin Herrenschmidt | c4007a2 | 2009-06-16 16:42:50 +0000 | [diff] [blame] | 1235 | static u64 timebase = 0; |
| 1236 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 1237 | void rtas_give_timebase(void) |
Benjamin Herrenschmidt | c4007a2 | 2009-06-16 16:42:50 +0000 | [diff] [blame] | 1238 | { |
| 1239 | unsigned long flags; |
| 1240 | |
| 1241 | local_irq_save(flags); |
| 1242 | hard_irq_disable(); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 1243 | arch_spin_lock(&timebase_lock); |
Benjamin Herrenschmidt | c4007a2 | 2009-06-16 16:42:50 +0000 | [diff] [blame] | 1244 | rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL); |
| 1245 | timebase = get_tb(); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 1246 | arch_spin_unlock(&timebase_lock); |
Benjamin Herrenschmidt | c4007a2 | 2009-06-16 16:42:50 +0000 | [diff] [blame] | 1247 | |
| 1248 | while (timebase) |
| 1249 | barrier(); |
| 1250 | rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL); |
| 1251 | local_irq_restore(flags); |
| 1252 | } |
| 1253 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 1254 | void rtas_take_timebase(void) |
Benjamin Herrenschmidt | c4007a2 | 2009-06-16 16:42:50 +0000 | [diff] [blame] | 1255 | { |
| 1256 | while (!timebase) |
| 1257 | barrier(); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 1258 | arch_spin_lock(&timebase_lock); |
Benjamin Herrenschmidt | c4007a2 | 2009-06-16 16:42:50 +0000 | [diff] [blame] | 1259 | set_tb(timebase >> 32, timebase & 0xffffffff); |
| 1260 | timebase = 0; |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 1261 | arch_spin_unlock(&timebase_lock); |
Benjamin Herrenschmidt | c4007a2 | 2009-06-16 16:42:50 +0000 | [diff] [blame] | 1262 | } |