Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #include <linux/suspend.h>
0003 #include <linux/suspend_ioctls.h>
0004 #include <linux/utsname.h>
0005 #include <linux/freezer.h>
0006 #include <linux/compiler.h>
0007 #include <linux/cpu.h>
0008 #include <linux/cpuidle.h>
0009 
0010 struct swsusp_info {
0011     struct new_utsname  uts;
0012     u32         version_code;
0013     unsigned long       num_physpages;
0014     int         cpus;
0015     unsigned long       image_pages;
0016     unsigned long       pages;
0017     unsigned long       size;
0018 } __aligned(PAGE_SIZE);
0019 
0020 #ifdef CONFIG_HIBERNATION
0021 /* kernel/power/snapshot.c */
0022 extern void __init hibernate_reserved_size_init(void);
0023 extern void __init hibernate_image_size_init(void);
0024 
0025 #ifdef CONFIG_ARCH_HIBERNATION_HEADER
0026 /* Maximum size of architecture specific data in a hibernation header */
0027 #define MAX_ARCH_HEADER_SIZE    (sizeof(struct new_utsname) + 4)
0028 
0029 extern int arch_hibernation_header_save(void *addr, unsigned int max_size);
0030 extern int arch_hibernation_header_restore(void *addr);
0031 
0032 static inline int init_header_complete(struct swsusp_info *info)
0033 {
0034     return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE);
0035 }
0036 
0037 static inline const char *check_image_kernel(struct swsusp_info *info)
0038 {
0039     return arch_hibernation_header_restore(info) ?
0040             "architecture specific data" : NULL;
0041 }
0042 #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
0043 
0044 extern int hibernate_resume_nonboot_cpu_disable(void);
0045 
0046 /*
0047  * Keep some memory free so that I/O operations can succeed without paging
0048  * [Might this be more than 4 MB?]
0049  */
0050 #define PAGES_FOR_IO    ((4096 * 1024) >> PAGE_SHIFT)
0051 
0052 /*
0053  * Keep 1 MB of memory free so that device drivers can allocate some pages in
0054  * their .suspend() routines without breaking the suspend to disk.
0055  */
0056 #define SPARE_PAGES ((1024 * 1024) >> PAGE_SHIFT)
0057 
0058 asmlinkage int swsusp_save(void);
0059 
0060 /* kernel/power/hibernate.c */
0061 extern bool freezer_test_done;
0062 
0063 extern int hibernation_snapshot(int platform_mode);
0064 extern int hibernation_restore(int platform_mode);
0065 extern int hibernation_platform_enter(void);
0066 
0067 #ifdef CONFIG_STRICT_KERNEL_RWX
0068 /* kernel/power/snapshot.c */
0069 extern void enable_restore_image_protection(void);
0070 #else
0071 static inline void enable_restore_image_protection(void) {}
0072 #endif /* CONFIG_STRICT_KERNEL_RWX */
0073 
0074 #else /* !CONFIG_HIBERNATION */
0075 
0076 static inline void hibernate_reserved_size_init(void) {}
0077 static inline void hibernate_image_size_init(void) {}
0078 #endif /* !CONFIG_HIBERNATION */
0079 
0080 #define power_attr(_name) \
0081 static struct kobj_attribute _name##_attr = {   \
0082     .attr   = {             \
0083         .name = __stringify(_name), \
0084         .mode = 0644,           \
0085     },                  \
0086     .show   = _name##_show,         \
0087     .store  = _name##_store,        \
0088 }
0089 
0090 #define power_attr_ro(_name) \
0091 static struct kobj_attribute _name##_attr = {   \
0092     .attr   = {             \
0093         .name = __stringify(_name), \
0094         .mode = S_IRUGO,        \
0095     },                  \
0096     .show   = _name##_show,         \
0097 }
0098 
0099 /* Preferred image size in bytes (default 500 MB) */
0100 extern unsigned long image_size;
0101 /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
0102 extern unsigned long reserved_size;
0103 extern int in_suspend;
0104 extern dev_t swsusp_resume_device;
0105 extern sector_t swsusp_resume_block;
0106 
0107 extern int create_basic_memory_bitmaps(void);
0108 extern void free_basic_memory_bitmaps(void);
0109 extern int hibernate_preallocate_memory(void);
0110 
0111 extern void clear_or_poison_free_pages(void);
0112 
0113 /**
0114  *  Auxiliary structure used for reading the snapshot image data and
0115  *  metadata from and writing them to the list of page backup entries
0116  *  (PBEs) which is the main data structure of swsusp.
0117  *
0118  *  Using struct snapshot_handle we can transfer the image, including its
0119  *  metadata, as a continuous sequence of bytes with the help of
0120  *  snapshot_read_next() and snapshot_write_next().
0121  *
0122  *  The code that writes the image to a storage or transfers it to
0123  *  the user land is required to use snapshot_read_next() for this
0124  *  purpose and it should not make any assumptions regarding the internal
0125  *  structure of the image.  Similarly, the code that reads the image from
0126  *  a storage or transfers it from the user land is required to use
0127  *  snapshot_write_next().
0128  *
0129  *  This may allow us to change the internal structure of the image
0130  *  in the future with considerably less effort.
0131  */
0132 
0133 struct snapshot_handle {
0134     unsigned int    cur;    /* number of the block of PAGE_SIZE bytes the
0135                  * next operation will refer to (ie. current)
0136                  */
0137     void        *buffer;    /* address of the block to read from
0138                      * or write to
0139                      */
0140     int     sync_read;  /* Set to one to notify the caller of
0141                      * snapshot_write_next() that it may
0142                      * need to call wait_on_bio_chain()
0143                      */
0144 };
0145 
0146 /* This macro returns the address from/to which the caller of
0147  * snapshot_read_next()/snapshot_write_next() is allowed to
0148  * read/write data after the function returns
0149  */
0150 #define data_of(handle) ((handle).buffer)
0151 
0152 extern unsigned int snapshot_additional_pages(struct zone *zone);
0153 extern unsigned long snapshot_get_image_size(void);
0154 extern int snapshot_read_next(struct snapshot_handle *handle);
0155 extern int snapshot_write_next(struct snapshot_handle *handle);
0156 extern void snapshot_write_finalize(struct snapshot_handle *handle);
0157 extern int snapshot_image_loaded(struct snapshot_handle *handle);
0158 
0159 extern bool hibernate_acquire(void);
0160 extern void hibernate_release(void);
0161 
0162 extern sector_t alloc_swapdev_block(int swap);
0163 extern void free_all_swap_pages(int swap);
0164 extern int swsusp_swap_in_use(void);
0165 
0166 /*
0167  * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
0168  * the image header.
0169  */
0170 #define SF_PLATFORM_MODE    1
0171 #define SF_NOCOMPRESS_MODE  2
0172 #define SF_CRC32_MODE           4
0173 #define SF_HW_SIG       8
0174 
0175 /* kernel/power/hibernate.c */
0176 extern int swsusp_check(void);
0177 extern void swsusp_free(void);
0178 extern int swsusp_read(unsigned int *flags_p);
0179 extern int swsusp_write(unsigned int flags);
0180 extern void swsusp_close(fmode_t);
0181 #ifdef CONFIG_SUSPEND
0182 extern int swsusp_unmark(void);
0183 #endif
0184 
0185 struct __kernel_old_timeval;
0186 /* kernel/power/swsusp.c */
0187 extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
0188 
0189 #ifdef CONFIG_SUSPEND
0190 /* kernel/power/suspend.c */
0191 extern const char * const pm_labels[];
0192 extern const char *pm_states[];
0193 extern const char *mem_sleep_states[];
0194 
0195 extern int suspend_devices_and_enter(suspend_state_t state);
0196 #else /* !CONFIG_SUSPEND */
0197 #define mem_sleep_current   PM_SUSPEND_ON
0198 
0199 static inline int suspend_devices_and_enter(suspend_state_t state)
0200 {
0201     return -ENOSYS;
0202 }
0203 #endif /* !CONFIG_SUSPEND */
0204 
0205 #ifdef CONFIG_PM_TEST_SUSPEND
0206 /* kernel/power/suspend_test.c */
0207 extern void suspend_test_start(void);
0208 extern void suspend_test_finish(const char *label);
0209 #else /* !CONFIG_PM_TEST_SUSPEND */
0210 static inline void suspend_test_start(void) {}
0211 static inline void suspend_test_finish(const char *label) {}
0212 #endif /* !CONFIG_PM_TEST_SUSPEND */
0213 
0214 #ifdef CONFIG_PM_SLEEP
0215 /* kernel/power/main.c */
0216 extern int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down);
0217 extern int pm_notifier_call_chain(unsigned long val);
0218 #endif
0219 
0220 #ifdef CONFIG_HIGHMEM
0221 int restore_highmem(void);
0222 #else
0223 static inline unsigned int count_highmem_pages(void) { return 0; }
0224 static inline int restore_highmem(void) { return 0; }
0225 #endif
0226 
0227 /*
0228  * Suspend test levels
0229  */
0230 enum {
0231     /* keep first */
0232     TEST_NONE,
0233     TEST_CORE,
0234     TEST_CPUS,
0235     TEST_PLATFORM,
0236     TEST_DEVICES,
0237     TEST_FREEZER,
0238     /* keep last */
0239     __TEST_AFTER_LAST
0240 };
0241 
0242 #define TEST_FIRST  TEST_NONE
0243 #define TEST_MAX    (__TEST_AFTER_LAST - 1)
0244 
0245 #ifdef CONFIG_PM_SLEEP_DEBUG
0246 extern int pm_test_level;
0247 #else
0248 #define pm_test_level   (TEST_NONE)
0249 #endif
0250 
0251 #ifdef CONFIG_SUSPEND_FREEZER
0252 static inline int suspend_freeze_processes(void)
0253 {
0254     int error;
0255 
0256     error = freeze_processes();
0257     /*
0258      * freeze_processes() automatically thaws every task if freezing
0259      * fails. So we need not do anything extra upon error.
0260      */
0261     if (error)
0262         return error;
0263 
0264     error = freeze_kernel_threads();
0265     /*
0266      * freeze_kernel_threads() thaws only kernel threads upon freezing
0267      * failure. So we have to thaw the userspace tasks ourselves.
0268      */
0269     if (error)
0270         thaw_processes();
0271 
0272     return error;
0273 }
0274 
0275 static inline void suspend_thaw_processes(void)
0276 {
0277     thaw_processes();
0278 }
0279 #else
0280 static inline int suspend_freeze_processes(void)
0281 {
0282     return 0;
0283 }
0284 
0285 static inline void suspend_thaw_processes(void)
0286 {
0287 }
0288 #endif
0289 
0290 #ifdef CONFIG_PM_AUTOSLEEP
0291 
0292 /* kernel/power/autosleep.c */
0293 extern int pm_autosleep_init(void);
0294 extern int pm_autosleep_lock(void);
0295 extern void pm_autosleep_unlock(void);
0296 extern suspend_state_t pm_autosleep_state(void);
0297 extern int pm_autosleep_set_state(suspend_state_t state);
0298 
0299 #else /* !CONFIG_PM_AUTOSLEEP */
0300 
0301 static inline int pm_autosleep_init(void) { return 0; }
0302 static inline int pm_autosleep_lock(void) { return 0; }
0303 static inline void pm_autosleep_unlock(void) {}
0304 static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; }
0305 
0306 #endif /* !CONFIG_PM_AUTOSLEEP */
0307 
0308 #ifdef CONFIG_PM_WAKELOCKS
0309 
0310 /* kernel/power/wakelock.c */
0311 extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
0312 extern int pm_wake_lock(const char *buf);
0313 extern int pm_wake_unlock(const char *buf);
0314 
0315 #endif /* !CONFIG_PM_WAKELOCKS */
0316 
0317 static inline int pm_sleep_disable_secondary_cpus(void)
0318 {
0319     cpuidle_pause();
0320     return suspend_disable_secondary_cpus();
0321 }
0322 
0323 static inline void pm_sleep_enable_secondary_cpus(void)
0324 {
0325     suspend_enable_secondary_cpus();
0326     cpuidle_resume();
0327 }