]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - arch/powerpc/kernel/rtas.c
Merge tag 'split-asm_system_h-for-linus-20120328' of git://git.kernel.org/pub/scm...
[linux-2.6.git] / arch / powerpc / kernel / rtas.c
index 14353b8789ddebabe7bc653d2a43ac90b4abe902..fcec38241f794f204e4f44a052c4b16ce25b125c 100644 (file)
 #include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/spinlock.h>
-#include <linux/module.h>
+#include <linux/export.h>
 #include <linux/init.h>
 #include <linux/capability.h>
 #include <linux/delay.h>
+#include <linux/smp.h>
+#include <linux/completion.h>
+#include <linux/cpumask.h>
+#include <linux/memblock.h>
+#include <linux/slab.h>
+#include <linux/reboot.h>
 
 #include <asm/prom.h>
 #include <asm/rtas.h>
 #include <asm/hvcall.h>
-#include <asm/semaphore.h>
 #include <asm/machdep.h>
 #include <asm/firmware.h>
 #include <asm/page.h>
 #include <asm/param.h>
-#include <asm/system.h>
 #include <asm/delay.h>
 #include <asm/uaccess.h>
-#include <asm/lmb.h>
 #include <asm/udbg.h>
 #include <asm/syscalls.h>
+#include <asm/smp.h>
+#include <linux/atomic.h>
+#include <asm/time.h>
+#include <asm/mmu.h>
+#include <asm/topology.h>
+#include <asm/pSeries_reconfig.h>
 
 struct rtas_t rtas = {
-       .lock = SPIN_LOCK_UNLOCKED
+       .lock = __ARCH_SPIN_LOCK_UNLOCKED
 };
 EXPORT_SYMBOL(rtas);
 
-struct rtas_suspend_me_data {
-       long waiting;
-       struct rtas_args *args;
-};
-
 DEFINE_SPINLOCK(rtas_data_buf_lock);
 EXPORT_SYMBOL(rtas_data_buf_lock);
 
@@ -60,6 +64,28 @@ unsigned long rtas_rmo_buf;
 void (*rtas_flash_term_hook)(int);
 EXPORT_SYMBOL(rtas_flash_term_hook);
 
+/* RTAS use home made raw locking instead of spin_lock_irqsave
+ * because those can be called from within really nasty contexts
+ * such as having the timebase stopped which would lockup with
+ * normal locks and spinlock debugging enabled
+ */
+static unsigned long lock_rtas(void)
+{
+       unsigned long flags;
+
+       local_irq_save(flags);
+       preempt_disable();
+       arch_spin_lock_flags(&rtas.lock, flags);
+       return flags;
+}
+
+static void unlock_rtas(unsigned long flags)
+{
+       arch_spin_unlock(&rtas.lock);
+       local_irq_restore(flags);
+       preempt_enable();
+}
+
 /*
  * call_rtas_display_status and call_rtas_display_status_delay
  * are designed only for very early low-level debugging, which
@@ -72,7 +98,7 @@ static void call_rtas_display_status(char c)
 
        if (!rtas.base)
                return;
-       spin_lock_irqsave(&rtas.lock, s);
+       s = lock_rtas();
 
        args->token = 10;
        args->nargs = 1;
@@ -82,7 +108,7 @@ static void call_rtas_display_status(char c)
 
        enter_rtas(__pa(args));
 
-       spin_unlock_irqrestore(&rtas.lock, s);
+       unlock_rtas(s);
 }
 
 static void call_rtas_display_status_delay(char c)
@@ -182,7 +208,7 @@ void rtas_progress(char *s, unsigned short hex)
        char *os;
        static int display_character, set_indicator;
        static int display_width, display_lines, form_feed;
-       const static int *row_width;
+       static const int *row_width;
        static DEFINE_SPINLOCK(progress_lock);
        static int current_line;
        static int pending_newline = 0;  /* did last write end with unprinted newline? */
@@ -192,18 +218,19 @@ void rtas_progress(char *s, unsigned short hex)
 
        if (display_width == 0) {
                display_width = 0x10;
-               if ((root = find_path_device("/rtas"))) {
-                       if ((p = get_property(root,
+               if ((root = of_find_node_by_path("/rtas"))) {
+                       if ((p = of_get_property(root,
                                        "ibm,display-line-length", NULL)))
                                display_width = *p;
-                       if ((p = get_property(root,
+                       if ((p = of_get_property(root,
                                        "ibm,form-feed", NULL)))
                                form_feed = *p;
-                       if ((p = get_property(root,
+                       if ((p = of_get_property(root,
                                        "ibm,display-number-of-lines", NULL)))
                                display_lines = *p;
-                       row_width = get_property(root,
+                       row_width = of_get_property(root,
                                        "ibm,display-truncation-length", NULL);
+                       of_node_put(root);
                }
                display_character = rtas_token("display-character");
                set_indicator = rtas_token("set-indicator");
@@ -298,11 +325,17 @@ int rtas_token(const char *service)
        const int *tokp;
        if (rtas.dev == NULL)
                return RTAS_UNKNOWN_SERVICE;
-       tokp = get_property(rtas.dev, service, NULL);
+       tokp = of_get_property(rtas.dev, service, NULL);
        return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
 }
 EXPORT_SYMBOL(rtas_token);
 
+int rtas_service_present(const char *service)
+{
+       return rtas_token(service) != RTAS_UNKNOWN_SERVICE;
+}
+EXPORT_SYMBOL(rtas_service_present);
+
 #ifdef CONFIG_RTAS_ERROR_LOGGING
 /*
  * Return the firmware-specified size of the error log buffer
@@ -327,8 +360,8 @@ int rtas_get_error_log_max(void)
 EXPORT_SYMBOL(rtas_get_error_log_max);
 
 
-char rtas_err_buf[RTAS_ERROR_LOG_MAX];
-int rtas_last_error_token;
+static char rtas_err_buf[RTAS_ERROR_LOG_MAX];
+static int rtas_last_error_token;
 
 /** Return a copy of the detailed error text associated with the
  *  most recent failed call to rtas.  Because the error text
@@ -397,8 +430,7 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
        if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
                return -1;
 
-       /* Gotta do something different here, use global lock for now... */
-       spin_lock_irqsave(&rtas.lock, s);
+       s = lock_rtas();
        rtas_args = &rtas.args;
 
        rtas_args->token = token;
@@ -425,8 +457,7 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
                        outputs[i] = rtas_args->rets[i+1];
        ret = (nret > 0)? rtas_args->rets[0]: 0;
 
-       /* Gotta do something different here, use global lock for now... */
-       spin_unlock_irqrestore(&rtas.lock, s);
+       unlock_rtas(s);
 
        if (buff_copy) {
                log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
@@ -464,14 +495,14 @@ unsigned int rtas_busy_delay(int status)
 
        might_sleep();
        ms = rtas_busy_delay_time(status);
-       if (ms)
+       if (ms && need_resched())
                msleep(ms);
 
        return ms;
 }
 EXPORT_SYMBOL(rtas_busy_delay);
 
-int rtas_error_rc(int rtas_rc)
+static int rtas_error_rc(int rtas_rc)
 {
        int rc;
 
@@ -493,7 +524,7 @@ int rtas_error_rc(int rtas_rc)
                        break;
                default:
                        printk(KERN_ERR "%s: unexpected RTAS error %d\n",
-                                       __FUNCTION__, rtas_rc);
+                                       __func__, rtas_rc);
                        rc = -ERANGE;
                        break;
        }
@@ -553,6 +584,32 @@ int rtas_get_sensor(int sensor, int index, int *state)
 }
 EXPORT_SYMBOL(rtas_get_sensor);
 
+bool rtas_indicator_present(int token, int *maxindex)
+{
+       int proplen, count, i;
+       const struct indicator_elem {
+               u32 token;
+               u32 maxindex;
+       } *indicators;
+
+       indicators = of_get_property(rtas.dev, "rtas-indicators", &proplen);
+       if (!indicators)
+               return false;
+
+       count = proplen / sizeof(struct indicator_elem);
+
+       for (i = 0; i < count; i++) {
+               if (indicators[i].token != token)
+                       continue;
+               if (maxindex)
+                       *maxindex = indicators[i].maxindex;
+               return true;
+       }
+
+       return false;
+}
+EXPORT_SYMBOL(rtas_indicator_present);
+
 int rtas_set_indicator(int indicator, int index, int new_value)
 {
        int token = rtas_token("set-indicator");
@@ -571,6 +628,27 @@ int rtas_set_indicator(int indicator, int index, int new_value)
 }
 EXPORT_SYMBOL(rtas_set_indicator);
 
+/*
+ * Ignoring RTAS extended delay
+ */
+int rtas_set_indicator_fast(int indicator, int index, int new_value)
+{
+       int rc;
+       int token = rtas_token("set-indicator");
+
+       if (token == RTAS_UNKNOWN_SERVICE)
+               return -ENOENT;
+
+       rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
+
+       WARN_ON(rc == -2 || (rc >= 9900 && rc <= 9905));
+
+       if (rc < 0)
+               return rtas_error_rc(rc);
+
+       return rc;
+}
+
 void rtas_restart(char *cmd)
 {
        if (rtas_flash_term_hook)
@@ -607,7 +685,14 @@ void rtas_os_term(char *str)
 {
        int status;
 
-       if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
+       /*
+        * Firmware with the ibm,extended-os-term property is guaranteed
+        * to always return from an ibm,os-term call. Earlier versions without
+        * this property may terminate the partition which we want to avoid
+        * since it interferes with panic_timeout.
+        */
+       if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term") ||
+           RTAS_UNKNOWN_SERVICE == rtas_token("ibm,extended-os-term"))
                return;
 
        snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
@@ -618,58 +703,121 @@ void rtas_os_term(char *str)
        } while (rtas_busy_delay(status));
 
        if (status != 0)
-               printk(KERN_EMERG "ibm,os-term call failed %d\n",
-                              status);
+               printk(KERN_EMERG "ibm,os-term call failed %d\n", status);
 }
 
 static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
 #ifdef CONFIG_PPC_PSERIES
-static void rtas_percpu_suspend_me(void *info)
+static int __rtas_suspend_last_cpu(struct rtas_suspend_me_data *data, int wake_when_done)
 {
-       int i;
-       long rc;
-       long flags;
-       struct rtas_suspend_me_data *data =
-               (struct rtas_suspend_me_data *)info;
+       u16 slb_size = mmu_slb_size;
+       int rc = H_MULTI_THREADS_ACTIVE;
+       int cpu;
 
-       /*
-        * We use "waiting" to indicate our state.  As long
-        * as it is >0, we are still trying to all join up.
-        * If it goes to 0, we have successfully joined up and
-        * one thread got H_CONTINUE.  If any error happens,
-        * we set it to <0.
-        */
-       local_irq_save(flags);
-       do {
+       slb_set_size(SLB_MIN_SIZE);
+       printk(KERN_DEBUG "calling ibm,suspend-me on cpu %i\n", smp_processor_id());
+
+       while (rc == H_MULTI_THREADS_ACTIVE && !atomic_read(&data->done) &&
+              !atomic_read(&data->error))
+               rc = rtas_call(data->token, 0, 1, NULL);
+
+       if (rc || atomic_read(&data->error)) {
+               printk(KERN_DEBUG "ibm,suspend-me returned %d\n", rc);
+               slb_set_size(slb_size);
+       }
+
+       if (atomic_read(&data->error))
+               rc = atomic_read(&data->error);
+
+       atomic_set(&data->error, rc);
+       pSeries_coalesce_init();
+
+       if (wake_when_done) {
+               atomic_set(&data->done, 1);
+
+               for_each_online_cpu(cpu)
+                       plpar_hcall_norets(H_PROD, get_hard_smp_processor_id(cpu));
+       }
+
+       if (atomic_dec_return(&data->working) == 0)
+               complete(data->complete);
+
+       return rc;
+}
+
+int rtas_suspend_last_cpu(struct rtas_suspend_me_data *data)
+{
+       atomic_inc(&data->working);
+       return __rtas_suspend_last_cpu(data, 0);
+}
+
+static int __rtas_suspend_cpu(struct rtas_suspend_me_data *data, int wake_when_done)
+{
+       long rc = H_SUCCESS;
+       unsigned long msr_save;
+       int cpu;
+
+       atomic_inc(&data->working);
+
+       /* really need to ensure MSR.EE is off for H_JOIN */
+       msr_save = mfmsr();
+       mtmsr(msr_save & ~(MSR_EE));
+
+       while (rc == H_SUCCESS && !atomic_read(&data->done) && !atomic_read(&data->error))
                rc = plpar_hcall_norets(H_JOIN);
-               smp_rmb();
-       } while (rc == H_SUCCESS && data->waiting > 0);
-       if (rc == H_SUCCESS)
-               goto out;
 
-       if (rc == H_CONTINUE) {
-               data->waiting = 0;
-               data->args->args[data->args->nargs] =
-                       rtas_call(ibm_suspend_me_token, 0, 1, NULL);
-               for_each_possible_cpu(i)
-                       plpar_hcall_norets(H_PROD,i);
+       mtmsr(msr_save);
+
+       if (rc == H_SUCCESS) {
+               /* This cpu was prodded and the suspend is complete. */
+               goto out;
+       } else if (rc == H_CONTINUE) {
+               /* All other cpus are in H_JOIN, this cpu does
+                * the suspend.
+                */
+               return __rtas_suspend_last_cpu(data, wake_when_done);
        } else {
-               data->waiting = -EBUSY;
-               printk(KERN_ERR "Error on H_JOIN hypervisor call\n");
+               printk(KERN_ERR "H_JOIN on cpu %i failed with rc = %ld\n",
+                      smp_processor_id(), rc);
+               atomic_set(&data->error, rc);
        }
 
+       if (wake_when_done) {
+               atomic_set(&data->done, 1);
+
+               /* This cpu did the suspend or got an error; in either case,
+                * we need to prod all other other cpus out of join state.
+                * Extra prods are harmless.
+                */
+               for_each_online_cpu(cpu)
+                       plpar_hcall_norets(H_PROD, get_hard_smp_processor_id(cpu));
+       }
 out:
-       local_irq_restore(flags);
-       return;
+       if (atomic_dec_return(&data->working) == 0)
+               complete(data->complete);
+       return rc;
 }
 
-static int rtas_ibm_suspend_me(struct rtas_args *args)
+int rtas_suspend_cpu(struct rtas_suspend_me_data *data)
+{
+       return __rtas_suspend_cpu(data, 0);
+}
+
+static void rtas_percpu_suspend_me(void *info)
+{
+       __rtas_suspend_cpu((struct rtas_suspend_me_data *)info, 1);
+}
+
+int rtas_ibm_suspend_me(struct rtas_args *args)
 {
-       int i;
        long state;
        long rc;
        unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
        struct rtas_suspend_me_data data;
+       DECLARE_COMPLETION_ONSTACK(done);
+
+       if (!rtas_service_present("ibm,suspend-me"))
+               return -ENOSYS;
 
        /* Make sure the state is valid */
        rc = plpar_hcall(H_VASI_STATE, retbuf,
@@ -690,33 +838,69 @@ static int rtas_ibm_suspend_me(struct rtas_args *args)
                return 0;
        }
 
-       data.waiting = 1;
-       data.args = args;
+       atomic_set(&data.working, 0);
+       atomic_set(&data.done, 0);
+       atomic_set(&data.error, 0);
+       data.token = rtas_token("ibm,suspend-me");
+       data.complete = &done;
+       stop_topology_update();
 
        /* Call function on all CPUs.  One of us will make the
         * rtas call
         */
-       if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
-               data.waiting = -EINVAL;
+       if (on_each_cpu(rtas_percpu_suspend_me, &data, 0))
+               atomic_set(&data.error, -EINVAL);
+
+       wait_for_completion(&done);
 
-       if (data.waiting != 0)
+       if (atomic_read(&data.error) != 0)
                printk(KERN_ERR "Error doing global join\n");
 
-       /* Prod each CPU.  This won't hurt, and will wake
-        * anyone we successfully put to sleep with H_JOIN.
-        */
-       for_each_possible_cpu(i)
-               plpar_hcall_norets(H_PROD, i);
+       start_topology_update();
 
-       return data.waiting;
+       return atomic_read(&data.error);
 }
 #else /* CONFIG_PPC_PSERIES */
-static int rtas_ibm_suspend_me(struct rtas_args *args)
+int rtas_ibm_suspend_me(struct rtas_args *args)
 {
        return -ENOSYS;
 }
 #endif
 
+/**
+ * Find a specific pseries error log in an RTAS extended event log.
+ * @log: RTAS error/event log
+ * @section_id: two character section identifier
+ *
+ * Returns a pointer to the specified errorlog or NULL if not found.
+ */
+struct pseries_errorlog *get_pseries_errorlog(struct rtas_error_log *log,
+                                             uint16_t section_id)
+{
+       struct rtas_ext_event_log_v6 *ext_log =
+               (struct rtas_ext_event_log_v6 *)log->buffer;
+       struct pseries_errorlog *sect;
+       unsigned char *p, *log_end;
+
+       /* Check that we understand the format */
+       if (log->extended_log_length < sizeof(struct rtas_ext_event_log_v6) ||
+           ext_log->log_format != RTAS_V6EXT_LOG_FORMAT_EVENT_LOG ||
+           ext_log->company_id != RTAS_V6EXT_COMPANY_ID_IBM)
+               return NULL;
+
+       log_end = log->buffer + log->extended_log_length;
+       p = ext_log->vendor_log;
+
+       while (p < log_end) {
+               sect = (struct pseries_errorlog *)p;
+               if (sect->id == section_id)
+                       return sect;
+               p += sect->length;
+       }
+
+       return NULL;
+}
+
 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
 {
        struct rtas_args args;
@@ -745,6 +929,9 @@ asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
        if (args.token == RTAS_UNKNOWN_SERVICE)
                return -EINVAL;
 
+       args.rets = &args.args[nargs];
+       memset(args.rets, 0, args.nret * sizeof(rtas_arg_t));
+
        /* Need to handle ibm,suspend_me call specially */
        if (args.token == ibm_suspend_me_token) {
                rc = rtas_ibm_suspend_me(&args);
@@ -755,20 +942,18 @@ asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
 
        buff_copy = get_errorlog_buffer();
 
-       spin_lock_irqsave(&rtas.lock, flags);
+       flags = lock_rtas();
 
        rtas.args = args;
        enter_rtas(__pa(&rtas.args));
        args = rtas.args;
 
-       args.rets = &args.args[nargs];
-
        /* A -1 return code indicates that the last command couldn't
           be completed due to a hardware error. */
        if (args.rets[0] == -1)
                errbuf = __fetch_rtas_last_error(buff_copy);
 
-       spin_unlock_irqrestore(&rtas.lock, flags);
+       unlock_rtas(flags);
 
        if (buff_copy) {
                if (errbuf)
@@ -786,31 +971,6 @@ asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
        return 0;
 }
 
-/* This version can't take the spinlock, because it never returns */
-
-struct rtas_args rtas_stop_self_args = {
-       /* The token is initialized for real in setup_system() */
-       .token = RTAS_UNKNOWN_SERVICE,
-       .nargs = 0,
-       .nret = 1,
-       .rets = &rtas_stop_self_args.args[0],
-};
-
-void rtas_stop_self(void)
-{
-       struct rtas_args *rtas_args = &rtas_stop_self_args;
-
-       local_irq_disable();
-
-       BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
-
-       printk("cpu %u (hwid %u) Ready to die...\n",
-              smp_processor_id(), hard_smp_processor_id());
-       enter_rtas(__pa(rtas_args));
-
-       panic("Alas, I survived.\n");
-}
-
 /*
  * Call early during boot, before mem init or bootmem, to retrieve the RTAS
  * informations from the device-tree and allocate the RMO buffer for userland
@@ -827,12 +987,12 @@ void __init rtas_initialize(void)
        if (rtas.dev) {
                const u32 *basep, *entryp, *sizep;
 
-               basep = get_property(rtas.dev, "linux,rtas-base", NULL);
-               sizep = get_property(rtas.dev, "rtas-size", NULL);
+               basep = of_get_property(rtas.dev, "linux,rtas-base", NULL);
+               sizep = of_get_property(rtas.dev, "rtas-size", NULL);
                if (basep != NULL && sizep != NULL) {
                        rtas.base = *basep;
                        rtas.size = *sizep;
-                       entryp = get_property(rtas.dev,
+                       entryp = of_get_property(rtas.dev,
                                        "linux,rtas-entry", NULL);
                        if (entryp == NULL) /* Ugh */
                                rtas.entry = rtas.base;
@@ -849,15 +1009,12 @@ void __init rtas_initialize(void)
         */
 #ifdef CONFIG_PPC64
        if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) {
-               rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
+               rtas_region = min(ppc64_rma_size, RTAS_INSTANTIATE_MAX);
                ibm_suspend_me_token = rtas_token("ibm,suspend-me");
        }
 #endif
-       rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
+       rtas_rmo_buf = memblock_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
 
-#ifdef CONFIG_HOTPLUG_CPU
-       rtas_stop_self_args.token = rtas_token("stop-self");
-#endif /* CONFIG_HOTPLUG_CPU */
 #ifdef CONFIG_RTAS_ERROR_LOGGING
        rtas_last_error_token = rtas_token("rtas-last-error");
 #endif
@@ -889,8 +1046,43 @@ int __init early_init_dt_scan_rtas(unsigned long node,
        basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
        if (basep)
                rtas_getchar_token = *basep;
+
+       if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
+           rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
+               udbg_init_rtas_console();
+
 #endif
 
        /* break now */
        return 1;
 }
+
+static arch_spinlock_t timebase_lock;
+static u64 timebase = 0;
+
+void __cpuinit rtas_give_timebase(void)
+{
+       unsigned long flags;
+
+       local_irq_save(flags);
+       hard_irq_disable();
+       arch_spin_lock(&timebase_lock);
+       rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
+       timebase = get_tb();
+       arch_spin_unlock(&timebase_lock);
+
+       while (timebase)
+               barrier();
+       rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
+       local_irq_restore(flags);
+}
+
+void __cpuinit rtas_take_timebase(void)
+{
+       while (!timebase)
+               barrier();
+       arch_spin_lock(&timebase_lock);
+       set_tb(timebase >> 32, timebase & 0xffffffff);
+       timebase = 0;
+       arch_spin_unlock(&timebase_lock);
+}