BACKPORT: dmaengine: core: expose max burst capability to clients
[firefly-linux-kernel-4.4.55.git] / kernel / power / hibernate.c
index b26f5f1e773e6b6aa3420ee1fb60c9b8fc6025cc..b7dd5718836e2b76c0c0700251bae66364e72da8 100644 (file)
 #include <linux/syscore_ops.h>
 #include <linux/ctype.h>
 #include <linux/genhd.h>
+#include <linux/ktime.h>
+#include <trace/events/power.h>
 
 #include "power.h"
 
 
 static int nocompress;
 static int noresume;
+static int nohibernate;
 static int resume_wait;
-static int resume_delay;
+static unsigned int resume_delay;
 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
 dev_t swsusp_resume_device;
 sector_t swsusp_resume_block;
-int in_suspend __nosavedata;
+__visible int in_suspend __nosavedata;
 
 enum {
        HIBERNATION_INVALID,
@@ -61,6 +64,11 @@ bool freezer_test_done;
 
 static const struct platform_hibernation_ops *hibernation_ops;
 
+bool hibernation_available(void)
+{
+       return (nohibernate == 0);
+}
+
 /**
  * hibernation_set_ops - Set the global hibernate operations.
  * @ops: Hibernation operations to use in subsequent hibernation transitions.
@@ -82,6 +90,7 @@ void hibernation_set_ops(const struct platform_hibernation_ops *ops)
 
        unlock_system_sleep();
 }
+EXPORT_SYMBOL_GPL(hibernation_set_ops);
 
 static bool entering_platform_hibernation;
 
@@ -224,22 +233,23 @@ static void platform_recover(int platform_mode)
  * @nr_pages: Number of memory pages processed between @start and @stop.
  * @msg: Additional diagnostic message to print.
  */
-void swsusp_show_speed(struct timeval *start, struct timeval *stop,
-                       unsigned nr_pages, char *msg)
+void swsusp_show_speed(ktime_t start, ktime_t stop,
+                     unsigned nr_pages, char *msg)
 {
-       s64 elapsed_centisecs64;
-       int centisecs;
-       int k;
-       int kps;
-
-       elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
-       do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
+       ktime_t diff;
+       u64 elapsed_centisecs64;
+       unsigned int centisecs;
+       unsigned int k;
+       unsigned int kps;
+
+       diff = ktime_sub(stop, start);
+       elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
        centisecs = elapsed_centisecs64;
        if (centisecs == 0)
                centisecs = 1;  /* avoid div-by-zero */
        k = nr_pages * (PAGE_SIZE / 1024);
        kps = (k * 100) / centisecs;
-       printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
+       printk(KERN_INFO "PM: %s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
                        msg, k,
                        centisecs / 100, centisecs % 100,
                        kps / 1000, (kps % 1000) / 10);
@@ -287,16 +297,18 @@ static int create_image(int platform_mode)
 
        in_suspend = 1;
        save_processor_state();
+       trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
        error = swsusp_arch_suspend();
+       trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
        if (error)
                printk(KERN_ERR "PM: Error %d creating hibernation image\n",
                        error);
        /* Restore control flow magically appears here */
        restore_processor_state();
-       if (!in_suspend) {
+       if (!in_suspend)
                events_check_enabled = false;
-               platform_leave(platform_mode);
-       }
+
+       platform_leave(platform_mode);
 
  Power_up:
        syscore_resume();
@@ -327,6 +339,7 @@ int hibernation_snapshot(int platform_mode)
        pm_message_t msg;
        int error;
 
+       pm_suspend_clear_flags();
        error = platform_begin(platform_mode);
        if (error)
                goto Close;
@@ -357,7 +370,6 @@ int hibernation_snapshot(int platform_mode)
        }
 
        suspend_console();
-       ftrace_stop();
        pm_restrict_gfp_mask();
 
        error = dpm_suspend(PMSG_FREEZE);
@@ -383,7 +395,6 @@ int hibernation_snapshot(int platform_mode)
        if (error || !in_suspend)
                pm_restore_gfp_mask();
 
-       ftrace_start();
        resume_console();
        dpm_complete(msg);
 
@@ -486,15 +497,19 @@ int hibernation_restore(int platform_mode)
 
        pm_prepare_console();
        suspend_console();
-       ftrace_stop();
        pm_restrict_gfp_mask();
        error = dpm_suspend_start(PMSG_QUIESCE);
        if (!error) {
                error = resume_target_kernel(platform_mode);
-               dpm_resume_end(PMSG_RECOVER);
+               /*
+                * The above should either succeed and jump to the new kernel,
+                * or return with an error. Otherwise things are just
+                * undefined, so let's be paranoid.
+                */
+               BUG_ON(!error);
        }
+       dpm_resume_end(PMSG_RECOVER);
        pm_restore_gfp_mask();
-       ftrace_start();
        resume_console();
        pm_restore_console();
        return error;
@@ -521,7 +536,6 @@ int hibernation_platform_enter(void)
 
        entering_platform_hibernation = true;
        suspend_console();
-       ftrace_stop();
        error = dpm_suspend_start(PMSG_HIBERNATE);
        if (error) {
                if (hibernation_ops->recover)
@@ -539,7 +553,7 @@ int hibernation_platform_enter(void)
 
        error = disable_nonboot_cpus();
        if (error)
-               goto Platform_finish;
+               goto Enable_cpus;
 
        local_irq_disable();
        syscore_suspend();
@@ -555,6 +569,8 @@ int hibernation_platform_enter(void)
  Power_up:
        syscore_resume();
        local_irq_enable();
+
+ Enable_cpus:
        enable_nonboot_cpus();
 
  Platform_finish:
@@ -565,7 +581,6 @@ int hibernation_platform_enter(void)
  Resume_devices:
        entering_platform_hibernation = false;
        dpm_resume_end(PMSG_RESTORE);
-       ftrace_start();
        resume_console();
 
  Close:
@@ -594,7 +609,8 @@ static void power_down(void)
        case HIBERNATION_PLATFORM:
                hibernation_platform_enter();
        case HIBERNATION_SHUTDOWN:
-               kernel_power_off();
+               if (pm_power_off)
+                       kernel_power_off();
                break;
 #ifdef CONFIG_SUSPEND
        case HIBERNATION_SUSPEND:
@@ -622,7 +638,8 @@ static void power_down(void)
         * corruption after resume.
         */
        printk(KERN_CRIT "PM: Please power down manually\n");
-       while(1);
+       while (1)
+               cpu_relax();
 }
 
 /**
@@ -632,6 +649,11 @@ int hibernate(void)
 {
        int error;
 
+       if (!hibernation_available()) {
+               pr_debug("PM: Hibernation not available.\n");
+               return -EPERM;
+       }
+
        lock_system_sleep();
        /* The snapshot device should not be opened while we're running */
        if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
@@ -644,22 +666,23 @@ int hibernate(void)
        if (error)
                goto Exit;
 
-       /* Allocate memory management structures */
-       error = create_basic_memory_bitmaps();
-       if (error)
-               goto Exit;
-
        printk(KERN_INFO "PM: Syncing filesystems ... ");
        sys_sync();
        printk("done.\n");
 
        error = freeze_processes();
        if (error)
-               goto Free_bitmaps;
+               goto Exit;
+
+       lock_device_hotplug();
+       /* Allocate memory management structures */
+       error = create_basic_memory_bitmaps();
+       if (error)
+               goto Thaw;
 
        error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
        if (error || freezer_test_done)
-               goto Thaw;
+               goto Free_bitmaps;
 
        if (in_suspend) {
                unsigned int flags = 0;
@@ -682,14 +705,14 @@ int hibernate(void)
                pr_debug("PM: Image restored successfully.\n");
        }
 
+ Free_bitmaps:
+       free_basic_memory_bitmaps();
  Thaw:
+       unlock_device_hotplug();
        thaw_processes();
 
        /* Don't bother checking whether freezer_test_done is true */
        freezer_test_done = false;
-
- Free_bitmaps:
-       free_basic_memory_bitmaps();
  Exit:
        pm_notifier_call_chain(PM_POST_HIBERNATION);
        pm_restore_console();
@@ -711,7 +734,7 @@ int hibernate(void)
  * contents of memory is restored from the saved image.
  *
  * If this is successful, control reappears in the restored target kernel in
- * hibernation_snaphot() which returns to hibernate().  Otherwise, the routine
+ * hibernation_snapshot() which returns to hibernate().  Otherwise, the routine
  * attempts to recover gracefully and make the kernel return to the normal mode
  * of operation.
  */
@@ -723,7 +746,7 @@ static int software_resume(void)
        /*
         * If the user said "noresume".. bail out early.
         */
-       if (noresume)
+       if (noresume || !hibernation_available())
                return 0;
 
        /*
@@ -806,21 +829,20 @@ static int software_resume(void)
        pm_prepare_console();
        error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
        if (error)
-               goto close_finish;
-
-       error = create_basic_memory_bitmaps();
-       if (error)
-               goto close_finish;
+               goto Close_Finish;
 
        pr_debug("PM: Preparing processes for restore.\n");
        error = freeze_processes();
-       if (error) {
-               swsusp_close(FMODE_READ);
-               goto Done;
-       }
+       if (error)
+               goto Close_Finish;
 
        pr_debug("PM: Loading hibernation image.\n");
 
+       lock_device_hotplug();
+       error = create_basic_memory_bitmaps();
+       if (error)
+               goto Thaw;
+
        error = swsusp_read(&flags);
        swsusp_close(FMODE_READ);
        if (!error)
@@ -828,9 +850,10 @@ static int software_resume(void)
 
        printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
        swsusp_free();
-       thaw_processes();
- Done:
        free_basic_memory_bitmaps();
+ Thaw:
+       unlock_device_hotplug();
+       thaw_processes();
  Finish:
        pm_notifier_call_chain(PM_POST_RESTORE);
        pm_restore_console();
@@ -840,12 +863,12 @@ static int software_resume(void)
        mutex_unlock(&pm_mutex);
        pr_debug("PM: Hibernation image not present or could not be loaded.\n");
        return error;
-close_finish:
+ Close_Finish:
        swsusp_close(FMODE_READ);
        goto Finish;
 }
 
-late_initcall(software_resume);
+late_initcall_sync(software_resume);
 
 
 static const char * const hibernation_modes[] = {
@@ -889,6 +912,9 @@ static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
        int i;
        char *start = buf;
 
+       if (!hibernation_available())
+               return sprintf(buf, "[disabled]\n");
+
        for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
                if (!hibernation_modes[i])
                        continue;
@@ -923,6 +949,9 @@ static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
        char *p;
        int mode = HIBERNATION_INVALID;
 
+       if (!hibernation_available())
+               return -EPERM;
+
        p = memchr(buf, '\n', n);
        len = p ? p - buf : n;
 
@@ -971,16 +1000,20 @@ static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
                            const char *buf, size_t n)
 {
-       unsigned int maj, min;
        dev_t res;
-       int ret = -EINVAL;
+       int len = n;
+       char *name;
 
-       if (sscanf(buf, "%u:%u", &maj, &min) != 2)
-               goto out;
+       if (len && buf[len-1] == '\n')
+               len--;
+       name = kstrndup(buf, len, GFP_KERNEL);
+       if (!name)
+               return -ENOMEM;
 
-       res = MKDEV(maj,min);
-       if (maj != MAJOR(res) || min != MINOR(res))
-               goto out;
+       res = name_to_dev_t(name);
+       kfree(name);
+       if (!res)
+               return -EINVAL;
 
        lock_system_sleep();
        swsusp_resume_device = res;
@@ -988,9 +1021,7 @@ static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
        printk(KERN_INFO "PM: Starting manual resume from disk\n");
        noresume = 0;
        software_resume();
-       ret = n;
- out:
-       return ret;
+       return n;
 }
 
 power_attr(resume);
@@ -1088,6 +1119,10 @@ static int __init hibernate_setup(char *str)
                noresume = 1;
        else if (!strncmp(str, "nocompress", 10))
                nocompress = 1;
+       else if (!strncmp(str, "no", 2)) {
+               noresume = 1;
+               nohibernate = 1;
+       }
        return 1;
 }
 
@@ -1105,13 +1140,30 @@ static int __init resumewait_setup(char *str)
 
 static int __init resumedelay_setup(char *str)
 {
-       resume_delay = simple_strtoul(str, NULL, 0);
+       int rc = kstrtouint(str, 0, &resume_delay);
+
+       if (rc)
+               return rc;
        return 1;
 }
 
+static int __init nohibernate_setup(char *str)
+{
+       noresume = 1;
+       nohibernate = 1;
+       return 1;
+}
+
+static int __init kaslr_nohibernate_setup(char *str)
+{
+       return nohibernate_setup(str);
+}
+
 __setup("noresume", noresume_setup);
 __setup("resume_offset=", resume_offset_setup);
 __setup("resume=", resume_setup);
 __setup("hibernate=", hibernate_setup);
 __setup("resumewait", resumewait_setup);
 __setup("resumedelay=", resumedelay_setup);
+__setup("nohibernate", nohibernate_setup);
+__setup("kaslr", kaslr_nohibernate_setup);