Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  * Procedures for interfacing to the RTAS on CHRP machines.
0005  *
0006  * Peter Bergner, IBM   March 2001.
0007  * Copyright (C) 2001 IBM.
0008  */
0009 
0010 #include <linux/stdarg.h>
0011 #include <linux/kernel.h>
0012 #include <linux/types.h>
0013 #include <linux/spinlock.h>
0014 #include <linux/export.h>
0015 #include <linux/init.h>
0016 #include <linux/capability.h>
0017 #include <linux/delay.h>
0018 #include <linux/cpu.h>
0019 #include <linux/sched.h>
0020 #include <linux/smp.h>
0021 #include <linux/completion.h>
0022 #include <linux/cpumask.h>
0023 #include <linux/memblock.h>
0024 #include <linux/slab.h>
0025 #include <linux/reboot.h>
0026 #include <linux/syscalls.h>
0027 #include <linux/of.h>
0028 #include <linux/of_fdt.h>
0029 
0030 #include <asm/interrupt.h>
0031 #include <asm/rtas.h>
0032 #include <asm/hvcall.h>
0033 #include <asm/machdep.h>
0034 #include <asm/firmware.h>
0035 #include <asm/page.h>
0036 #include <asm/param.h>
0037 #include <asm/delay.h>
0038 #include <linux/uaccess.h>
0039 #include <asm/udbg.h>
0040 #include <asm/syscalls.h>
0041 #include <asm/smp.h>
0042 #include <linux/atomic.h>
0043 #include <asm/time.h>
0044 #include <asm/mmu.h>
0045 #include <asm/topology.h>
0046 #include <asm/paca.h>
0047 
0048 /* This is here deliberately so it's only used in this file */
0049 void enter_rtas(unsigned long);
0050 
0051 static inline void do_enter_rtas(unsigned long args)
0052 {
0053     unsigned long msr;
0054 
0055     /*
0056      * Make sure MSR[RI] is currently enabled as it will be forced later
0057      * in enter_rtas.
0058      */
0059     msr = mfmsr();
0060     BUG_ON(!(msr & MSR_RI));
0061 
0062     BUG_ON(!irqs_disabled());
0063 
0064     hard_irq_disable(); /* Ensure MSR[EE] is disabled on PPC64 */
0065 
0066     enter_rtas(args);
0067 
0068     srr_regs_clobbered(); /* rtas uses SRRs, invalidate */
0069 }
0070 
0071 struct rtas_t rtas = {
0072     .lock = __ARCH_SPIN_LOCK_UNLOCKED
0073 };
0074 EXPORT_SYMBOL(rtas);
0075 
0076 DEFINE_SPINLOCK(rtas_data_buf_lock);
0077 EXPORT_SYMBOL(rtas_data_buf_lock);
0078 
0079 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
0080 EXPORT_SYMBOL(rtas_data_buf);
0081 
0082 unsigned long rtas_rmo_buf;
0083 
0084 /*
0085  * If non-NULL, this gets called when the kernel terminates.
0086  * This is done like this so rtas_flash can be a module.
0087  */
0088 void (*rtas_flash_term_hook)(int);
0089 EXPORT_SYMBOL(rtas_flash_term_hook);
0090 
0091 /* RTAS use home made raw locking instead of spin_lock_irqsave
0092  * because those can be called from within really nasty contexts
0093  * such as having the timebase stopped which would lockup with
0094  * normal locks and spinlock debugging enabled
0095  */
0096 static unsigned long lock_rtas(void)
0097 {
0098     unsigned long flags;
0099 
0100     local_irq_save(flags);
0101     preempt_disable();
0102     arch_spin_lock(&rtas.lock);
0103     return flags;
0104 }
0105 
0106 static void unlock_rtas(unsigned long flags)
0107 {
0108     arch_spin_unlock(&rtas.lock);
0109     local_irq_restore(flags);
0110     preempt_enable();
0111 }
0112 
0113 /*
0114  * call_rtas_display_status and call_rtas_display_status_delay
0115  * are designed only for very early low-level debugging, which
0116  * is why the token is hard-coded to 10.
0117  */
0118 static void call_rtas_display_status(unsigned char c)
0119 {
0120     unsigned long s;
0121 
0122     if (!rtas.base)
0123         return;
0124 
0125     s = lock_rtas();
0126     rtas_call_unlocked(&rtas.args, 10, 1, 1, NULL, c);
0127     unlock_rtas(s);
0128 }
0129 
0130 static void call_rtas_display_status_delay(char c)
0131 {
0132     static int pending_newline = 0;  /* did last write end with unprinted newline? */
0133     static int width = 16;
0134 
0135     if (c == '\n') {    
0136         while (width-- > 0)
0137             call_rtas_display_status(' ');
0138         width = 16;
0139         mdelay(500);
0140         pending_newline = 1;
0141     } else {
0142         if (pending_newline) {
0143             call_rtas_display_status('\r');
0144             call_rtas_display_status('\n');
0145         } 
0146         pending_newline = 0;
0147         if (width--) {
0148             call_rtas_display_status(c);
0149             udelay(10000);
0150         }
0151     }
0152 }
0153 
0154 void __init udbg_init_rtas_panel(void)
0155 {
0156     udbg_putc = call_rtas_display_status_delay;
0157 }
0158 
0159 #ifdef CONFIG_UDBG_RTAS_CONSOLE
0160 
0161 /* If you think you're dying before early_init_dt_scan_rtas() does its
0162  * work, you can hard code the token values for your firmware here and
0163  * hardcode rtas.base/entry etc.
0164  */
0165 static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE;
0166 static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE;
0167 
0168 static void udbg_rtascon_putc(char c)
0169 {
0170     int tries;
0171 
0172     if (!rtas.base)
0173         return;
0174 
0175     /* Add CRs before LFs */
0176     if (c == '\n')
0177         udbg_rtascon_putc('\r');
0178 
0179     /* if there is more than one character to be displayed, wait a bit */
0180     for (tries = 0; tries < 16; tries++) {
0181         if (rtas_call(rtas_putchar_token, 1, 1, NULL, c) == 0)
0182             break;
0183         udelay(1000);
0184     }
0185 }
0186 
0187 static int udbg_rtascon_getc_poll(void)
0188 {
0189     int c;
0190 
0191     if (!rtas.base)
0192         return -1;
0193 
0194     if (rtas_call(rtas_getchar_token, 0, 2, &c))
0195         return -1;
0196 
0197     return c;
0198 }
0199 
0200 static int udbg_rtascon_getc(void)
0201 {
0202     int c;
0203 
0204     while ((c = udbg_rtascon_getc_poll()) == -1)
0205         ;
0206 
0207     return c;
0208 }
0209 
0210 
0211 void __init udbg_init_rtas_console(void)
0212 {
0213     udbg_putc = udbg_rtascon_putc;
0214     udbg_getc = udbg_rtascon_getc;
0215     udbg_getc_poll = udbg_rtascon_getc_poll;
0216 }
0217 #endif /* CONFIG_UDBG_RTAS_CONSOLE */
0218 
0219 void rtas_progress(char *s, unsigned short hex)
0220 {
0221     struct device_node *root;
0222     int width;
0223     const __be32 *p;
0224     char *os;
0225     static int display_character, set_indicator;
0226     static int display_width, display_lines, form_feed;
0227     static const int *row_width;
0228     static DEFINE_SPINLOCK(progress_lock);
0229     static int current_line;
0230     static int pending_newline = 0;  /* did last write end with unprinted newline? */
0231 
0232     if (!rtas.base)
0233         return;
0234 
0235     if (display_width == 0) {
0236         display_width = 0x10;
0237         if ((root = of_find_node_by_path("/rtas"))) {
0238             if ((p = of_get_property(root,
0239                     "ibm,display-line-length", NULL)))
0240                 display_width = be32_to_cpu(*p);
0241             if ((p = of_get_property(root,
0242                     "ibm,form-feed", NULL)))
0243                 form_feed = be32_to_cpu(*p);
0244             if ((p = of_get_property(root,
0245                     "ibm,display-number-of-lines", NULL)))
0246                 display_lines = be32_to_cpu(*p);
0247             row_width = of_get_property(root,
0248                     "ibm,display-truncation-length", NULL);
0249             of_node_put(root);
0250         }
0251         display_character = rtas_token("display-character");
0252         set_indicator = rtas_token("set-indicator");
0253     }
0254 
0255     if (display_character == RTAS_UNKNOWN_SERVICE) {
0256         /* use hex display if available */
0257         if (set_indicator != RTAS_UNKNOWN_SERVICE)
0258             rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
0259         return;
0260     }
0261 
0262     spin_lock(&progress_lock);
0263 
0264     /*
0265      * Last write ended with newline, but we didn't print it since
0266      * it would just clear the bottom line of output. Print it now
0267      * instead.
0268      *
0269      * If no newline is pending and form feed is supported, clear the
0270      * display with a form feed; otherwise, print a CR to start output
0271      * at the beginning of the line.
0272      */
0273     if (pending_newline) {
0274         rtas_call(display_character, 1, 1, NULL, '\r');
0275         rtas_call(display_character, 1, 1, NULL, '\n');
0276         pending_newline = 0;
0277     } else {
0278         current_line = 0;
0279         if (form_feed)
0280             rtas_call(display_character, 1, 1, NULL,
0281                   (char)form_feed);
0282         else
0283             rtas_call(display_character, 1, 1, NULL, '\r');
0284     }
0285  
0286     if (row_width)
0287         width = row_width[current_line];
0288     else
0289         width = display_width;
0290     os = s;
0291     while (*os) {
0292         if (*os == '\n' || *os == '\r') {
0293             /* If newline is the last character, save it
0294              * until next call to avoid bumping up the
0295              * display output.
0296              */
0297             if (*os == '\n' && !os[1]) {
0298                 pending_newline = 1;
0299                 current_line++;
0300                 if (current_line > display_lines-1)
0301                     current_line = display_lines-1;
0302                 spin_unlock(&progress_lock);
0303                 return;
0304             }
0305  
0306             /* RTAS wants CR-LF, not just LF */
0307  
0308             if (*os == '\n') {
0309                 rtas_call(display_character, 1, 1, NULL, '\r');
0310                 rtas_call(display_character, 1, 1, NULL, '\n');
0311             } else {
0312                 /* CR might be used to re-draw a line, so we'll
0313                  * leave it alone and not add LF.
0314                  */
0315                 rtas_call(display_character, 1, 1, NULL, *os);
0316             }
0317  
0318             if (row_width)
0319                 width = row_width[current_line];
0320             else
0321                 width = display_width;
0322         } else {
0323             width--;
0324             rtas_call(display_character, 1, 1, NULL, *os);
0325         }
0326  
0327         os++;
0328  
0329         /* if we overwrite the screen length */
0330         if (width <= 0)
0331             while ((*os != 0) && (*os != '\n') && (*os != '\r'))
0332                 os++;
0333     }
0334  
0335     spin_unlock(&progress_lock);
0336 }
0337 EXPORT_SYMBOL(rtas_progress);       /* needed by rtas_flash module */
0338 
0339 int rtas_token(const char *service)
0340 {
0341     const __be32 *tokp;
0342     if (rtas.dev == NULL)
0343         return RTAS_UNKNOWN_SERVICE;
0344     tokp = of_get_property(rtas.dev, service, NULL);
0345     return tokp ? be32_to_cpu(*tokp) : RTAS_UNKNOWN_SERVICE;
0346 }
0347 EXPORT_SYMBOL(rtas_token);
0348 
0349 int rtas_service_present(const char *service)
0350 {
0351     return rtas_token(service) != RTAS_UNKNOWN_SERVICE;
0352 }
0353 EXPORT_SYMBOL(rtas_service_present);
0354 
0355 #ifdef CONFIG_RTAS_ERROR_LOGGING
0356 /*
0357  * Return the firmware-specified size of the error log buffer
0358  *  for all rtas calls that require an error buffer argument.
0359  *  This includes 'check-exception' and 'rtas-last-error'.
0360  */
0361 int rtas_get_error_log_max(void)
0362 {
0363     static int rtas_error_log_max;
0364     if (rtas_error_log_max)
0365         return rtas_error_log_max;
0366 
0367     rtas_error_log_max = rtas_token ("rtas-error-log-max");
0368     if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
0369         (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
0370         printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
0371             rtas_error_log_max);
0372         rtas_error_log_max = RTAS_ERROR_LOG_MAX;
0373     }
0374     return rtas_error_log_max;
0375 }
0376 EXPORT_SYMBOL(rtas_get_error_log_max);
0377 
0378 
0379 static char rtas_err_buf[RTAS_ERROR_LOG_MAX];
0380 static int rtas_last_error_token;
0381 
0382 /** Return a copy of the detailed error text associated with the
0383  *  most recent failed call to rtas.  Because the error text
0384  *  might go stale if there are any other intervening rtas calls,
0385  *  this routine must be called atomically with whatever produced
0386  *  the error (i.e. with rtas.lock still held from the previous call).
0387  */
0388 static char *__fetch_rtas_last_error(char *altbuf)
0389 {
0390     struct rtas_args err_args, save_args;
0391     u32 bufsz;
0392     char *buf = NULL;
0393 
0394     if (rtas_last_error_token == -1)
0395         return NULL;
0396 
0397     bufsz = rtas_get_error_log_max();
0398 
0399     err_args.token = cpu_to_be32(rtas_last_error_token);
0400     err_args.nargs = cpu_to_be32(2);
0401     err_args.nret = cpu_to_be32(1);
0402     err_args.args[0] = cpu_to_be32(__pa(rtas_err_buf));
0403     err_args.args[1] = cpu_to_be32(bufsz);
0404     err_args.args[2] = 0;
0405 
0406     save_args = rtas.args;
0407     rtas.args = err_args;
0408 
0409     do_enter_rtas(__pa(&rtas.args));
0410 
0411     err_args = rtas.args;
0412     rtas.args = save_args;
0413 
0414     /* Log the error in the unlikely case that there was one. */
0415     if (unlikely(err_args.args[2] == 0)) {
0416         if (altbuf) {
0417             buf = altbuf;
0418         } else {
0419             buf = rtas_err_buf;
0420             if (slab_is_available())
0421                 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
0422         }
0423         if (buf)
0424             memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
0425     }
0426 
0427     return buf;
0428 }
0429 
0430 #define get_errorlog_buffer()   kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
0431 
0432 #else /* CONFIG_RTAS_ERROR_LOGGING */
0433 #define __fetch_rtas_last_error(x)  NULL
0434 #define get_errorlog_buffer()       NULL
0435 #endif
0436 
0437 
0438 static void
0439 va_rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret,
0440               va_list list)
0441 {
0442     int i;
0443 
0444     args->token = cpu_to_be32(token);
0445     args->nargs = cpu_to_be32(nargs);
0446     args->nret  = cpu_to_be32(nret);
0447     args->rets  = &(args->args[nargs]);
0448 
0449     for (i = 0; i < nargs; ++i)
0450         args->args[i] = cpu_to_be32(va_arg(list, __u32));
0451 
0452     for (i = 0; i < nret; ++i)
0453         args->rets[i] = 0;
0454 
0455     do_enter_rtas(__pa(args));
0456 }
0457 
0458 void rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret, ...)
0459 {
0460     va_list list;
0461 
0462     va_start(list, nret);
0463     va_rtas_call_unlocked(args, token, nargs, nret, list);
0464     va_end(list);
0465 }
0466 
0467 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
0468 {
0469     va_list list;
0470     int i;
0471     unsigned long s;
0472     struct rtas_args *rtas_args;
0473     char *buff_copy = NULL;
0474     int ret;
0475 
0476     if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
0477         return -1;
0478 
0479     if ((mfmsr() & (MSR_IR|MSR_DR)) != (MSR_IR|MSR_DR)) {
0480         WARN_ON_ONCE(1);
0481         return -1;
0482     }
0483 
0484     s = lock_rtas();
0485 
0486     /* We use the global rtas args buffer */
0487     rtas_args = &rtas.args;
0488 
0489     va_start(list, outputs);
0490     va_rtas_call_unlocked(rtas_args, token, nargs, nret, list);
0491     va_end(list);
0492 
0493     /* A -1 return code indicates that the last command couldn't
0494        be completed due to a hardware error. */
0495     if (be32_to_cpu(rtas_args->rets[0]) == -1)
0496         buff_copy = __fetch_rtas_last_error(NULL);
0497 
0498     if (nret > 1 && outputs != NULL)
0499         for (i = 0; i < nret-1; ++i)
0500             outputs[i] = be32_to_cpu(rtas_args->rets[i+1]);
0501     ret = (nret > 0)? be32_to_cpu(rtas_args->rets[0]): 0;
0502 
0503     unlock_rtas(s);
0504 
0505     if (buff_copy) {
0506         log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
0507         if (slab_is_available())
0508             kfree(buff_copy);
0509     }
0510     return ret;
0511 }
0512 EXPORT_SYMBOL(rtas_call);
0513 
0514 /**
0515  * rtas_busy_delay_time() - From an RTAS status value, calculate the
0516  *                          suggested delay time in milliseconds.
0517  *
0518  * @status: a value returned from rtas_call() or similar APIs which return
0519  *          the status of a RTAS function call.
0520  *
0521  * Context: Any context.
0522  *
0523  * Return:
0524  * * 100000 - If @status is 9905.
0525  * * 10000  - If @status is 9904.
0526  * * 1000   - If @status is 9903.
0527  * * 100    - If @status is 9902.
0528  * * 10     - If @status is 9901.
0529  * * 1      - If @status is either 9900 or -2. This is "wrong" for -2, but
0530  *            some callers depend on this behavior, and the worst outcome
0531  *            is that they will delay for longer than necessary.
0532  * * 0      - If @status is not a busy or extended delay value.
0533  */
0534 unsigned int rtas_busy_delay_time(int status)
0535 {
0536     int order;
0537     unsigned int ms = 0;
0538 
0539     if (status == RTAS_BUSY) {
0540         ms = 1;
0541     } else if (status >= RTAS_EXTENDED_DELAY_MIN &&
0542            status <= RTAS_EXTENDED_DELAY_MAX) {
0543         order = status - RTAS_EXTENDED_DELAY_MIN;
0544         for (ms = 1; order > 0; order--)
0545             ms *= 10;
0546     }
0547 
0548     return ms;
0549 }
0550 EXPORT_SYMBOL(rtas_busy_delay_time);
0551 
0552 /**
0553  * rtas_busy_delay() - helper for RTAS busy and extended delay statuses
0554  *
0555  * @status: a value returned from rtas_call() or similar APIs which return
0556  *          the status of a RTAS function call.
0557  *
0558  * Context: Process context. May sleep or schedule.
0559  *
0560  * Return:
0561  * * true  - @status is RTAS_BUSY or an extended delay hint. The
0562  *           caller may assume that the CPU has been yielded if necessary,
0563  *           and that an appropriate delay for @status has elapsed.
0564  *           Generally the caller should reattempt the RTAS call which
0565  *           yielded @status.
0566  *
0567  * * false - @status is not @RTAS_BUSY nor an extended delay hint. The
0568  *           caller is responsible for handling @status.
0569  */
0570 bool rtas_busy_delay(int status)
0571 {
0572     unsigned int ms;
0573     bool ret;
0574 
0575     switch (status) {
0576     case RTAS_EXTENDED_DELAY_MIN...RTAS_EXTENDED_DELAY_MAX:
0577         ret = true;
0578         ms = rtas_busy_delay_time(status);
0579         /*
0580          * The extended delay hint can be as high as 100 seconds.
0581          * Surely any function returning such a status is either
0582          * buggy or isn't going to be significantly slowed by us
0583          * polling at 1HZ. Clamp the sleep time to one second.
0584          */
0585         ms = clamp(ms, 1U, 1000U);
0586         /*
0587          * The delay hint is an order-of-magnitude suggestion, not
0588          * a minimum. It is fine, possibly even advantageous, for
0589          * us to pause for less time than hinted. For small values,
0590          * use usleep_range() to ensure we don't sleep much longer
0591          * than actually needed.
0592          *
0593          * See Documentation/timers/timers-howto.rst for
0594          * explanation of the threshold used here. In effect we use
0595          * usleep_range() for 9900 and 9901, msleep() for
0596          * 9902-9905.
0597          */
0598         if (ms <= 20)
0599             usleep_range(ms * 100, ms * 1000);
0600         else
0601             msleep(ms);
0602         break;
0603     case RTAS_BUSY:
0604         ret = true;
0605         /*
0606          * We should call again immediately if there's no other
0607          * work to do.
0608          */
0609         cond_resched();
0610         break;
0611     default:
0612         ret = false;
0613         /*
0614          * Not a busy or extended delay status; the caller should
0615          * handle @status itself. Ensure we warn on misuses in
0616          * atomic context regardless.
0617          */
0618         might_sleep();
0619         break;
0620     }
0621 
0622     return ret;
0623 }
0624 EXPORT_SYMBOL(rtas_busy_delay);
0625 
0626 static int rtas_error_rc(int rtas_rc)
0627 {
0628     int rc;
0629 
0630     switch (rtas_rc) {
0631         case -1:        /* Hardware Error */
0632             rc = -EIO;
0633             break;
0634         case -3:        /* Bad indicator/domain/etc */
0635             rc = -EINVAL;
0636             break;
0637         case -9000:     /* Isolation error */
0638             rc = -EFAULT;
0639             break;
0640         case -9001:     /* Outstanding TCE/PTE */
0641             rc = -EEXIST;
0642             break;
0643         case -9002:     /* No usable slot */
0644             rc = -ENODEV;
0645             break;
0646         default:
0647             printk(KERN_ERR "%s: unexpected RTAS error %d\n",
0648                     __func__, rtas_rc);
0649             rc = -ERANGE;
0650             break;
0651     }
0652     return rc;
0653 }
0654 
0655 int rtas_get_power_level(int powerdomain, int *level)
0656 {
0657     int token = rtas_token("get-power-level");
0658     int rc;
0659 
0660     if (token == RTAS_UNKNOWN_SERVICE)
0661         return -ENOENT;
0662 
0663     while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
0664         udelay(1);
0665 
0666     if (rc < 0)
0667         return rtas_error_rc(rc);
0668     return rc;
0669 }
0670 EXPORT_SYMBOL(rtas_get_power_level);
0671 
0672 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
0673 {
0674     int token = rtas_token("set-power-level");
0675     int rc;
0676 
0677     if (token == RTAS_UNKNOWN_SERVICE)
0678         return -ENOENT;
0679 
0680     do {
0681         rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
0682     } while (rtas_busy_delay(rc));
0683 
0684     if (rc < 0)
0685         return rtas_error_rc(rc);
0686     return rc;
0687 }
0688 EXPORT_SYMBOL(rtas_set_power_level);
0689 
0690 int rtas_get_sensor(int sensor, int index, int *state)
0691 {
0692     int token = rtas_token("get-sensor-state");
0693     int rc;
0694 
0695     if (token == RTAS_UNKNOWN_SERVICE)
0696         return -ENOENT;
0697 
0698     do {
0699         rc = rtas_call(token, 2, 2, state, sensor, index);
0700     } while (rtas_busy_delay(rc));
0701 
0702     if (rc < 0)
0703         return rtas_error_rc(rc);
0704     return rc;
0705 }
0706 EXPORT_SYMBOL(rtas_get_sensor);
0707 
0708 int rtas_get_sensor_fast(int sensor, int index, int *state)
0709 {
0710     int token = rtas_token("get-sensor-state");
0711     int rc;
0712 
0713     if (token == RTAS_UNKNOWN_SERVICE)
0714         return -ENOENT;
0715 
0716     rc = rtas_call(token, 2, 2, state, sensor, index);
0717     WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
0718                     rc <= RTAS_EXTENDED_DELAY_MAX));
0719 
0720     if (rc < 0)
0721         return rtas_error_rc(rc);
0722     return rc;
0723 }
0724 
0725 bool rtas_indicator_present(int token, int *maxindex)
0726 {
0727     int proplen, count, i;
0728     const struct indicator_elem {
0729         __be32 token;
0730         __be32 maxindex;
0731     } *indicators;
0732 
0733     indicators = of_get_property(rtas.dev, "rtas-indicators", &proplen);
0734     if (!indicators)
0735         return false;
0736 
0737     count = proplen / sizeof(struct indicator_elem);
0738 
0739     for (i = 0; i < count; i++) {
0740         if (__be32_to_cpu(indicators[i].token) != token)
0741             continue;
0742         if (maxindex)
0743             *maxindex = __be32_to_cpu(indicators[i].maxindex);
0744         return true;
0745     }
0746 
0747     return false;
0748 }
0749 EXPORT_SYMBOL(rtas_indicator_present);
0750 
0751 int rtas_set_indicator(int indicator, int index, int new_value)
0752 {
0753     int token = rtas_token("set-indicator");
0754     int rc;
0755 
0756     if (token == RTAS_UNKNOWN_SERVICE)
0757         return -ENOENT;
0758 
0759     do {
0760         rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
0761     } while (rtas_busy_delay(rc));
0762 
0763     if (rc < 0)
0764         return rtas_error_rc(rc);
0765     return rc;
0766 }
0767 EXPORT_SYMBOL(rtas_set_indicator);
0768 
0769 /*
0770  * Ignoring RTAS extended delay
0771  */
0772 int rtas_set_indicator_fast(int indicator, int index, int new_value)
0773 {
0774     int rc;
0775     int token = rtas_token("set-indicator");
0776 
0777     if (token == RTAS_UNKNOWN_SERVICE)
0778         return -ENOENT;
0779 
0780     rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
0781 
0782     WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
0783                     rc <= RTAS_EXTENDED_DELAY_MAX));
0784 
0785     if (rc < 0)
0786         return rtas_error_rc(rc);
0787 
0788     return rc;
0789 }
0790 
0791 /**
0792  * rtas_ibm_suspend_me() - Call ibm,suspend-me to suspend the LPAR.
0793  *
0794  * @fw_status: RTAS call status will be placed here if not NULL.
0795  *
0796  * rtas_ibm_suspend_me() should be called only on a CPU which has
0797  * received H_CONTINUE from the H_JOIN hcall. All other active CPUs
0798  * should be waiting to return from H_JOIN.
0799  *
0800  * rtas_ibm_suspend_me() may suspend execution of the OS
0801  * indefinitely. Callers should take appropriate measures upon return, such as
0802  * resetting watchdog facilities.
0803  *
0804  * Callers may choose to retry this call if @fw_status is
0805  * %RTAS_THREADS_ACTIVE.
0806  *
0807  * Return:
0808  * 0          - The partition has resumed from suspend, possibly after
0809  *              migration to a different host.
0810  * -ECANCELED - The operation was aborted.
0811  * -EAGAIN    - There were other CPUs not in H_JOIN at the time of the call.
0812  * -EBUSY     - Some other condition prevented the suspend from succeeding.
0813  * -EIO       - Hardware/platform error.
0814  */
0815 int rtas_ibm_suspend_me(int *fw_status)
0816 {
0817     int fwrc;
0818     int ret;
0819 
0820     fwrc = rtas_call(rtas_token("ibm,suspend-me"), 0, 1, NULL);
0821 
0822     switch (fwrc) {
0823     case 0:
0824         ret = 0;
0825         break;
0826     case RTAS_SUSPEND_ABORTED:
0827         ret = -ECANCELED;
0828         break;
0829     case RTAS_THREADS_ACTIVE:
0830         ret = -EAGAIN;
0831         break;
0832     case RTAS_NOT_SUSPENDABLE:
0833     case RTAS_OUTSTANDING_COPROC:
0834         ret = -EBUSY;
0835         break;
0836     case -1:
0837     default:
0838         ret = -EIO;
0839         break;
0840     }
0841 
0842     if (fw_status)
0843         *fw_status = fwrc;
0844 
0845     return ret;
0846 }
0847 
0848 void __noreturn rtas_restart(char *cmd)
0849 {
0850     if (rtas_flash_term_hook)
0851         rtas_flash_term_hook(SYS_RESTART);
0852     printk("RTAS system-reboot returned %d\n",
0853            rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
0854     for (;;);
0855 }
0856 
0857 void rtas_power_off(void)
0858 {
0859     if (rtas_flash_term_hook)
0860         rtas_flash_term_hook(SYS_POWER_OFF);
0861     /* allow power on only with power button press */
0862     printk("RTAS power-off returned %d\n",
0863            rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
0864     for (;;);
0865 }
0866 
0867 void __noreturn rtas_halt(void)
0868 {
0869     if (rtas_flash_term_hook)
0870         rtas_flash_term_hook(SYS_HALT);
0871     /* allow power on only with power button press */
0872     printk("RTAS power-off returned %d\n",
0873            rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
0874     for (;;);
0875 }
0876 
0877 /* Must be in the RMO region, so we place it here */
0878 static char rtas_os_term_buf[2048];
0879 
0880 void rtas_os_term(char *str)
0881 {
0882     int status;
0883 
0884     /*
0885      * Firmware with the ibm,extended-os-term property is guaranteed
0886      * to always return from an ibm,os-term call. Earlier versions without
0887      * this property may terminate the partition which we want to avoid
0888      * since it interferes with panic_timeout.
0889      */
0890     if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term") ||
0891         RTAS_UNKNOWN_SERVICE == rtas_token("ibm,extended-os-term"))
0892         return;
0893 
0894     snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
0895 
0896     do {
0897         status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
0898                    __pa(rtas_os_term_buf));
0899     } while (rtas_busy_delay(status));
0900 
0901     if (status != 0)
0902         printk(KERN_EMERG "ibm,os-term call failed %d\n", status);
0903 }
0904 
0905 /**
0906  * rtas_activate_firmware() - Activate a new version of firmware.
0907  *
0908  * Context: This function may sleep.
0909  *
0910  * Activate a new version of partition firmware. The OS must call this
0911  * after resuming from a partition hibernation or migration in order
0912  * to maintain the ability to perform live firmware updates. It's not
0913  * catastrophic for this method to be absent or to fail; just log the
0914  * condition in that case.
0915  */
0916 void rtas_activate_firmware(void)
0917 {
0918     int token;
0919     int fwrc;
0920 
0921     token = rtas_token("ibm,activate-firmware");
0922     if (token == RTAS_UNKNOWN_SERVICE) {
0923         pr_notice("ibm,activate-firmware method unavailable\n");
0924         return;
0925     }
0926 
0927     do {
0928         fwrc = rtas_call(token, 0, 1, NULL);
0929     } while (rtas_busy_delay(fwrc));
0930 
0931     if (fwrc)
0932         pr_err("ibm,activate-firmware failed (%i)\n", fwrc);
0933 }
0934 
0935 #ifdef CONFIG_PPC_PSERIES
0936 /**
0937  * rtas_call_reentrant() - Used for reentrant rtas calls
0938  * @token:  Token for desired reentrant RTAS call
0939  * @nargs:  Number of Input Parameters
0940  * @nret:   Number of Output Parameters
0941  * @outputs:    Array of outputs
0942  * @...:    Inputs for desired RTAS call
0943  *
0944  * According to LoPAR documentation, only "ibm,int-on", "ibm,int-off",
0945  * "ibm,get-xive" and "ibm,set-xive" are currently reentrant.
0946  * Reentrant calls need their own rtas_args buffer, so not using rtas.args, but
0947  * PACA one instead.
0948  *
0949  * Return:  -1 on error,
0950  *      First output value of RTAS call if (nret > 0),
0951  *      0 otherwise,
0952  */
0953 int rtas_call_reentrant(int token, int nargs, int nret, int *outputs, ...)
0954 {
0955     va_list list;
0956     struct rtas_args *args;
0957     unsigned long flags;
0958     int i, ret = 0;
0959 
0960     if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
0961         return -1;
0962 
0963     local_irq_save(flags);
0964     preempt_disable();
0965 
0966     /* We use the per-cpu (PACA) rtas args buffer */
0967     args = local_paca->rtas_args_reentrant;
0968 
0969     va_start(list, outputs);
0970     va_rtas_call_unlocked(args, token, nargs, nret, list);
0971     va_end(list);
0972 
0973     if (nret > 1 && outputs)
0974         for (i = 0; i < nret - 1; ++i)
0975             outputs[i] = be32_to_cpu(args->rets[i + 1]);
0976 
0977     if (nret > 0)
0978         ret = be32_to_cpu(args->rets[0]);
0979 
0980     local_irq_restore(flags);
0981     preempt_enable();
0982 
0983     return ret;
0984 }
0985 
0986 #endif /* CONFIG_PPC_PSERIES */
0987 
0988 /**
0989  * get_pseries_errorlog() - Find a specific pseries error log in an RTAS
0990  *                          extended event log.
0991  * @log: RTAS error/event log
0992  * @section_id: two character section identifier
0993  *
0994  * Return: A pointer to the specified errorlog or NULL if not found.
0995  */
0996 noinstr struct pseries_errorlog *get_pseries_errorlog(struct rtas_error_log *log,
0997                               uint16_t section_id)
0998 {
0999     struct rtas_ext_event_log_v6 *ext_log =
1000         (struct rtas_ext_event_log_v6 *)log->buffer;
1001     struct pseries_errorlog *sect;
1002     unsigned char *p, *log_end;
1003     uint32_t ext_log_length = rtas_error_extended_log_length(log);
1004     uint8_t log_format = rtas_ext_event_log_format(ext_log);
1005     uint32_t company_id = rtas_ext_event_company_id(ext_log);
1006 
1007     /* Check that we understand the format */
1008     if (ext_log_length < sizeof(struct rtas_ext_event_log_v6) ||
1009         log_format != RTAS_V6EXT_LOG_FORMAT_EVENT_LOG ||
1010         company_id != RTAS_V6EXT_COMPANY_ID_IBM)
1011         return NULL;
1012 
1013     log_end = log->buffer + ext_log_length;
1014     p = ext_log->vendor_log;
1015 
1016     while (p < log_end) {
1017         sect = (struct pseries_errorlog *)p;
1018         if (pseries_errorlog_id(sect) == section_id)
1019             return sect;
1020         p += pseries_errorlog_length(sect);
1021     }
1022 
1023     return NULL;
1024 }
1025 
1026 #ifdef CONFIG_PPC_RTAS_FILTER
1027 
1028 /*
1029  * The sys_rtas syscall, as originally designed, allows root to pass
1030  * arbitrary physical addresses to RTAS calls. A number of RTAS calls
1031  * can be abused to write to arbitrary memory and do other things that
1032  * are potentially harmful to system integrity, and thus should only
1033  * be used inside the kernel and not exposed to userspace.
1034  *
1035  * All known legitimate users of the sys_rtas syscall will only ever
1036  * pass addresses that fall within the RMO buffer, and use a known
1037  * subset of RTAS calls.
1038  *
1039  * Accordingly, we filter RTAS requests to check that the call is
1040  * permitted, and that provided pointers fall within the RMO buffer.
1041  * The rtas_filters list contains an entry for each permitted call,
1042  * with the indexes of the parameters which are expected to contain
1043  * addresses and sizes of buffers allocated inside the RMO buffer.
1044  */
1045 struct rtas_filter {
1046     const char *name;
1047     int token;
1048     /* Indexes into the args buffer, -1 if not used */
1049     int buf_idx1;
1050     int size_idx1;
1051     int buf_idx2;
1052     int size_idx2;
1053 
1054     int fixed_size;
1055 };
1056 
1057 static struct rtas_filter rtas_filters[] __ro_after_init = {
1058     { "ibm,activate-firmware", -1, -1, -1, -1, -1 },
1059     { "ibm,configure-connector", -1, 0, -1, 1, -1, 4096 },  /* Special cased */
1060     { "display-character", -1, -1, -1, -1, -1 },
1061     { "ibm,display-message", -1, 0, -1, -1, -1 },
1062     { "ibm,errinjct", -1, 2, -1, -1, -1, 1024 },
1063     { "ibm,close-errinjct", -1, -1, -1, -1, -1 },
1064     { "ibm,open-errinjct", -1, -1, -1, -1, -1 },
1065     { "ibm,get-config-addr-info2", -1, -1, -1, -1, -1 },
1066     { "ibm,get-dynamic-sensor-state", -1, 1, -1, -1, -1 },
1067     { "ibm,get-indices", -1, 2, 3, -1, -1 },
1068     { "get-power-level", -1, -1, -1, -1, -1 },
1069     { "get-sensor-state", -1, -1, -1, -1, -1 },
1070     { "ibm,get-system-parameter", -1, 1, 2, -1, -1 },
1071     { "get-time-of-day", -1, -1, -1, -1, -1 },
1072     { "ibm,get-vpd", -1, 0, -1, 1, 2 },
1073     { "ibm,lpar-perftools", -1, 2, 3, -1, -1 },
1074     { "ibm,platform-dump", -1, 4, 5, -1, -1 },      /* Special cased */
1075     { "ibm,read-slot-reset-state", -1, -1, -1, -1, -1 },
1076     { "ibm,scan-log-dump", -1, 0, 1, -1, -1 },
1077     { "ibm,set-dynamic-indicator", -1, 2, -1, -1, -1 },
1078     { "ibm,set-eeh-option", -1, -1, -1, -1, -1 },
1079     { "set-indicator", -1, -1, -1, -1, -1 },
1080     { "set-power-level", -1, -1, -1, -1, -1 },
1081     { "set-time-for-power-on", -1, -1, -1, -1, -1 },
1082     { "ibm,set-system-parameter", -1, 1, -1, -1, -1 },
1083     { "set-time-of-day", -1, -1, -1, -1, -1 },
1084 #ifdef CONFIG_CPU_BIG_ENDIAN
1085     { "ibm,suspend-me", -1, -1, -1, -1, -1 },
1086     { "ibm,update-nodes", -1, 0, -1, -1, -1, 4096 },
1087     { "ibm,update-properties", -1, 0, -1, -1, -1, 4096 },
1088 #endif
1089     { "ibm,physical-attestation", -1, 0, 1, -1, -1 },
1090 };
1091 
1092 static bool in_rmo_buf(u32 base, u32 end)
1093 {
1094     return base >= rtas_rmo_buf &&
1095         base < (rtas_rmo_buf + RTAS_USER_REGION_SIZE) &&
1096         base <= end &&
1097         end >= rtas_rmo_buf &&
1098         end < (rtas_rmo_buf + RTAS_USER_REGION_SIZE);
1099 }
1100 
1101 static bool block_rtas_call(int token, int nargs,
1102                 struct rtas_args *args)
1103 {
1104     int i;
1105 
1106     for (i = 0; i < ARRAY_SIZE(rtas_filters); i++) {
1107         struct rtas_filter *f = &rtas_filters[i];
1108         u32 base, size, end;
1109 
1110         if (token != f->token)
1111             continue;
1112 
1113         if (f->buf_idx1 != -1) {
1114             base = be32_to_cpu(args->args[f->buf_idx1]);
1115             if (f->size_idx1 != -1)
1116                 size = be32_to_cpu(args->args[f->size_idx1]);
1117             else if (f->fixed_size)
1118                 size = f->fixed_size;
1119             else
1120                 size = 1;
1121 
1122             end = base + size - 1;
1123 
1124             /*
1125              * Special case for ibm,platform-dump - NULL buffer
1126              * address is used to indicate end of dump processing
1127              */
1128             if (!strcmp(f->name, "ibm,platform-dump") &&
1129                 base == 0)
1130                 return false;
1131 
1132             if (!in_rmo_buf(base, end))
1133                 goto err;
1134         }
1135 
1136         if (f->buf_idx2 != -1) {
1137             base = be32_to_cpu(args->args[f->buf_idx2]);
1138             if (f->size_idx2 != -1)
1139                 size = be32_to_cpu(args->args[f->size_idx2]);
1140             else if (f->fixed_size)
1141                 size = f->fixed_size;
1142             else
1143                 size = 1;
1144             end = base + size - 1;
1145 
1146             /*
1147              * Special case for ibm,configure-connector where the
1148              * address can be 0
1149              */
1150             if (!strcmp(f->name, "ibm,configure-connector") &&
1151                 base == 0)
1152                 return false;
1153 
1154             if (!in_rmo_buf(base, end))
1155                 goto err;
1156         }
1157 
1158         return false;
1159     }
1160 
1161 err:
1162     pr_err_ratelimited("sys_rtas: RTAS call blocked - exploit attempt?\n");
1163     pr_err_ratelimited("sys_rtas: token=0x%x, nargs=%d (called by %s)\n",
1164                token, nargs, current->comm);
1165     return true;
1166 }
1167 
1168 static void __init rtas_syscall_filter_init(void)
1169 {
1170     unsigned int i;
1171 
1172     for (i = 0; i < ARRAY_SIZE(rtas_filters); i++)
1173         rtas_filters[i].token = rtas_token(rtas_filters[i].name);
1174 }
1175 
1176 #else
1177 
1178 static bool block_rtas_call(int token, int nargs,
1179                 struct rtas_args *args)
1180 {
1181     return false;
1182 }
1183 
1184 static void __init rtas_syscall_filter_init(void)
1185 {
1186 }
1187 
1188 #endif /* CONFIG_PPC_RTAS_FILTER */
1189 
1190 /* We assume to be passed big endian arguments */
1191 SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)
1192 {
1193     struct rtas_args args;
1194     unsigned long flags;
1195     char *buff_copy, *errbuf = NULL;
1196     int nargs, nret, token;
1197 
1198     if (!capable(CAP_SYS_ADMIN))
1199         return -EPERM;
1200 
1201     if (!rtas.entry)
1202         return -EINVAL;
1203 
1204     if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
1205         return -EFAULT;
1206 
1207     nargs = be32_to_cpu(args.nargs);
1208     nret  = be32_to_cpu(args.nret);
1209     token = be32_to_cpu(args.token);
1210 
1211     if (nargs >= ARRAY_SIZE(args.args)
1212         || nret > ARRAY_SIZE(args.args)
1213         || nargs + nret > ARRAY_SIZE(args.args))
1214         return -EINVAL;
1215 
1216     /* Copy in args. */
1217     if (copy_from_user(args.args, uargs->args,
1218                nargs * sizeof(rtas_arg_t)) != 0)
1219         return -EFAULT;
1220 
1221     if (token == RTAS_UNKNOWN_SERVICE)
1222         return -EINVAL;
1223 
1224     args.rets = &args.args[nargs];
1225     memset(args.rets, 0, nret * sizeof(rtas_arg_t));
1226 
1227     if (block_rtas_call(token, nargs, &args))
1228         return -EINVAL;
1229 
1230     /* Need to handle ibm,suspend_me call specially */
1231     if (token == rtas_token("ibm,suspend-me")) {
1232 
1233         /*
1234          * rtas_ibm_suspend_me assumes the streamid handle is in cpu
1235          * endian, or at least the hcall within it requires it.
1236          */
1237         int rc = 0;
1238         u64 handle = ((u64)be32_to_cpu(args.args[0]) << 32)
1239                       | be32_to_cpu(args.args[1]);
1240         rc = rtas_syscall_dispatch_ibm_suspend_me(handle);
1241         if (rc == -EAGAIN)
1242             args.rets[0] = cpu_to_be32(RTAS_NOT_SUSPENDABLE);
1243         else if (rc == -EIO)
1244             args.rets[0] = cpu_to_be32(-1);
1245         else if (rc)
1246             return rc;
1247         goto copy_return;
1248     }
1249 
1250     buff_copy = get_errorlog_buffer();
1251 
1252     flags = lock_rtas();
1253 
1254     rtas.args = args;
1255     do_enter_rtas(__pa(&rtas.args));
1256     args = rtas.args;
1257 
1258     /* A -1 return code indicates that the last command couldn't
1259        be completed due to a hardware error. */
1260     if (be32_to_cpu(args.rets[0]) == -1)
1261         errbuf = __fetch_rtas_last_error(buff_copy);
1262 
1263     unlock_rtas(flags);
1264 
1265     if (buff_copy) {
1266         if (errbuf)
1267             log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
1268         kfree(buff_copy);
1269     }
1270 
1271  copy_return:
1272     /* Copy out args. */
1273     if (copy_to_user(uargs->args + nargs,
1274              args.args + nargs,
1275              nret * sizeof(rtas_arg_t)) != 0)
1276         return -EFAULT;
1277 
1278     return 0;
1279 }
1280 
1281 /*
1282  * Call early during boot, before mem init, to retrieve the RTAS
1283  * information from the device-tree and allocate the RMO buffer for userland
1284  * accesses.
1285  */
1286 void __init rtas_initialize(void)
1287 {
1288     unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
1289     u32 base, size, entry;
1290     int no_base, no_size, no_entry;
1291 
1292     /* Get RTAS dev node and fill up our "rtas" structure with infos
1293      * about it.
1294      */
1295     rtas.dev = of_find_node_by_name(NULL, "rtas");
1296     if (!rtas.dev)
1297         return;
1298 
1299     no_base = of_property_read_u32(rtas.dev, "linux,rtas-base", &base);
1300     no_size = of_property_read_u32(rtas.dev, "rtas-size", &size);
1301     if (no_base || no_size) {
1302         of_node_put(rtas.dev);
1303         rtas.dev = NULL;
1304         return;
1305     }
1306 
1307     rtas.base = base;
1308     rtas.size = size;
1309     no_entry = of_property_read_u32(rtas.dev, "linux,rtas-entry", &entry);
1310     rtas.entry = no_entry ? rtas.base : entry;
1311 
1312     /* If RTAS was found, allocate the RMO buffer for it and look for
1313      * the stop-self token if any
1314      */
1315 #ifdef CONFIG_PPC64
1316     if (firmware_has_feature(FW_FEATURE_LPAR))
1317         rtas_region = min(ppc64_rma_size, RTAS_INSTANTIATE_MAX);
1318 #endif
1319     rtas_rmo_buf = memblock_phys_alloc_range(RTAS_USER_REGION_SIZE, PAGE_SIZE,
1320                          0, rtas_region);
1321     if (!rtas_rmo_buf)
1322         panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n",
1323               PAGE_SIZE, &rtas_region);
1324 
1325 #ifdef CONFIG_RTAS_ERROR_LOGGING
1326     rtas_last_error_token = rtas_token("rtas-last-error");
1327 #endif
1328 
1329     rtas_syscall_filter_init();
1330 }
1331 
1332 int __init early_init_dt_scan_rtas(unsigned long node,
1333         const char *uname, int depth, void *data)
1334 {
1335     const u32 *basep, *entryp, *sizep;
1336 
1337     if (depth != 1 || strcmp(uname, "rtas") != 0)
1338         return 0;
1339 
1340     basep  = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
1341     entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1342     sizep  = of_get_flat_dt_prop(node, "rtas-size", NULL);
1343 
1344 #ifdef CONFIG_PPC64
1345     /* need this feature to decide the crashkernel offset */
1346     if (of_get_flat_dt_prop(node, "ibm,hypertas-functions", NULL))
1347         powerpc_firmware_features |= FW_FEATURE_LPAR;
1348 #endif
1349 
1350     if (basep && entryp && sizep) {
1351         rtas.base = *basep;
1352         rtas.entry = *entryp;
1353         rtas.size = *sizep;
1354     }
1355 
1356 #ifdef CONFIG_UDBG_RTAS_CONSOLE
1357     basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
1358     if (basep)
1359         rtas_putchar_token = *basep;
1360 
1361     basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
1362     if (basep)
1363         rtas_getchar_token = *basep;
1364 
1365     if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
1366         rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
1367         udbg_init_rtas_console();
1368 
1369 #endif
1370 
1371     /* break now */
1372     return 1;
1373 }
1374 
1375 static arch_spinlock_t timebase_lock;
1376 static u64 timebase = 0;
1377 
1378 void rtas_give_timebase(void)
1379 {
1380     unsigned long flags;
1381 
1382     local_irq_save(flags);
1383     hard_irq_disable();
1384     arch_spin_lock(&timebase_lock);
1385     rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
1386     timebase = get_tb();
1387     arch_spin_unlock(&timebase_lock);
1388 
1389     while (timebase)
1390         barrier();
1391     rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
1392     local_irq_restore(flags);
1393 }
1394 
1395 void rtas_take_timebase(void)
1396 {
1397     while (!timebase)
1398         barrier();
1399     arch_spin_lock(&timebase_lock);
1400     set_tb(timebase >> 32, timebase & 0xffffffff);
1401     timebase = 0;
1402     arch_spin_unlock(&timebase_lock);
1403 }