1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef COMMON_SDK_LINUX_KERNEL_POWER_POWER_H
3 #define COMMON_SDK_LINUX_KERNEL_POWER_POWER_H
4
5 #include <linux/suspend.h>
6 #include <linux/suspend_ioctls.h>
7 #include <linux/utsname.h>
8 #include <linux/freezer.h>
9 #include <linux/compiler.h>
10
11 struct swsusp_info {
12 struct new_utsname uts;
13 u32 version_code;
14 unsigned long num_physpages;
15 int cpus;
16 unsigned long image_pages;
17 unsigned long pages;
18 unsigned long size;
19 } __aligned(PAGE_SIZE);
20
21 #ifdef CONFIG_HIBERNATION
22 /* kernel/power/snapshot.c */
23 extern void __init hibernate_reserved_size_init(void);
24 extern void __init hibernate_image_size_init(void);
25
26 #ifdef CONFIG_ARCH_HIBERNATION_HEADER
27 /* Maximum size of architecture specific data in a hibernation header */
28 #define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4)
29
30 extern int arch_hibernation_header_save(void *addr, unsigned int max_size);
31 extern int arch_hibernation_header_restore(void *addr);
32
init_header_complete(struct swsusp_info * info)33 static inline int init_header_complete(struct swsusp_info *info)
34 {
35 return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE);
36 }
37
check_image_kernel(struct swsusp_info * info)38 static inline const char *check_image_kernel(struct swsusp_info *info)
39 {
40 return arch_hibernation_header_restore(info) ? "architecture specific data" : NULL;
41 }
42 #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
43
44 extern int hibernate_resume_nonboot_cpu_disable(void);
45
46 /*
47 * Keep some memory free so that I/O operations can succeed without paging
48 * [Might this be more than 4 MB?]
49 */
50 #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT)
51
52 /*
53 * Keep 1 MB of memory free so that device drivers can allocate some pages in
54 * their .suspend() routines without breaking the suspend to disk.
55 */
56 #define SPARE_PAGES ((1024 * 1024) >> PAGE_SHIFT)
57
58 asmlinkage int swsusp_save(void);
59
60 /* kernel/power/hibernate.c */
61 extern bool freezer_test_done;
62
63 extern int hibernation_snapshot(int platform_mode);
64 extern int hibernation_restore(int platform_mode);
65 extern int hibernation_platform_enter(void);
66
67 #ifdef CONFIG_STRICT_KERNEL_RWX
68 /* kernel/power/snapshot.c */
69 extern void enable_restore_image_protection(void);
70 #else
enable_restore_image_protection(void)71 static inline void enable_restore_image_protection(void)
72 {
73 }
74 #endif /* CONFIG_STRICT_KERNEL_RWX */
75
76 #else /* !CONFIG_HIBERNATION */
77
hibernate_reserved_size_init(void)78 static inline void hibernate_reserved_size_init(void)
79 {
80 }
hibernate_image_size_init(void)81 static inline void hibernate_image_size_init(void)
82 {
83 }
84 #endif /* !CONFIG_HIBERNATION */
85
86 #define power_attr(_name) \
87 static struct kobj_attribute _name##_attr = { \
88 .attr = \
89 { \
90 .name = __stringify(_name), \
91 .mode = 0644, \
92 }, \
93 .show = _name##_show, \
94 .store = _name##_store, \
95 }
96
97 #define power_attr_ro(_name) \
98 static struct kobj_attribute _name##_attr = { \
99 .attr = \
100 { \
101 .name = __stringify(_name), \
102 .mode = S_IRUGO, \
103 }, \
104 .show = _name##_show, \
105 }
106
107 /* Preferred image size in bytes (default 500 MB) */
108 extern unsigned long image_size;
109 /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
110 extern unsigned long reserved_size;
111 extern int in_suspend;
112 extern dev_t swsusp_resume_device;
113 extern sector_t swsusp_resume_block;
114
115 extern int create_basic_memory_bitmaps(void);
116 extern void free_basic_memory_bitmaps(void);
117 extern int hibernate_preallocate_memory(void);
118
119 extern void clear_or_poison_free_pages(void);
120
121 /**
122 * Auxiliary structure used for reading the snapshot image data and
123 * metadata from and writing them to the list of page backup entries
124 * (PBEs) which is the main data structure of swsusp.
125 *
126 * Using struct snapshot_handle we can transfer the image, including its
127 * metadata, as a continuous sequence of bytes with the help of
128 * snapshot_read_next() and snapshot_write_next().
129 *
130 * The code that writes the image to a storage or transfers it to
131 * the user land is required to use snapshot_read_next() for this
132 * purpose and it should not make any assumptions regarding the internal
133 * structure of the image. Similarly, the code that reads the image from
134 * a storage or transfers it from the user land is required to use
135 * snapshot_write_next().
136 *
137 * This may allow us to change the internal structure of the image
138 * in the future with considerably less effort.
139 */
140
141 struct snapshot_handle {
142 unsigned int cur; /* number of the block of PAGE_SIZE bytes the
143 * next operation will refer to (ie. current)
144 */
145 void *buffer; /* address of the block to read from
146 * or write to
147 */
148 int sync_read; /* Set to one to notify the caller of
149 * snapshot_write_next() that it may
150 * need to call wait_on_bio_chain()
151 */
152 };
153
154 /* This macro returns the address from/to which the caller of
155 * snapshot_read_next()/snapshot_write_next() is allowed to
156 * read/write data after the function returns
157 */
158 #define data_of(handle) ((handle).buffer)
159
160 extern unsigned int snapshot_additional_pages(struct zone *zone);
161 extern unsigned long snapshot_get_image_size(void);
162 extern int snapshot_read_next(struct snapshot_handle *handle);
163 extern int snapshot_write_next(struct snapshot_handle *handle);
164 extern void snapshot_write_finalize(struct snapshot_handle *handle);
165 extern int snapshot_image_loaded(struct snapshot_handle *handle);
166
167 extern bool hibernate_acquire(void);
168 extern void hibernate_release(void);
169
170 extern sector_t alloc_swapdev_block(int swap);
171 extern void free_all_swap_pages(int swap);
172 extern int swsusp_swap_in_use(void);
173
174 /*
175 * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
176 * the image header.
177 */
178 #define SF_PLATFORM_MODE 1
179 #define SF_NOCOMPRESS_MODE 2
180 #define SF_CRC32_MODE 4
181
182 /* kernel/power/hibernate.c */
183 extern int swsusp_check(void);
184 extern void swsusp_free(void);
185 extern int swsusp_read(unsigned int *flags_p);
186 extern int swsusp_write(unsigned int flags);
187 extern void swsusp_close(fmode_t);
188 #ifdef CONFIG_SUSPEND
189 extern int swsusp_unmark(void);
190 #endif
191
192 struct __kernel_old_timeval;
193 /* kernel/power/swsusp.c */
194 extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
195
196 #ifdef CONFIG_SUSPEND
197 /* kernel/power/suspend.c */
198 extern const char *const pm_labels[];
199 extern const char *pm_states[];
200 extern const char *mem_sleep_states[];
201
202 extern int suspend_devices_and_enter(suspend_state_t state);
203 #else /* !CONFIG_SUSPEND */
204 #define mem_sleep_current PM_SUSPEND_ON
205
suspend_devices_and_enter(suspend_state_t state)206 static inline int suspend_devices_and_enter(suspend_state_t state)
207 {
208 return -ENOSYS;
209 }
210 #endif /* !CONFIG_SUSPEND */
211
212 #ifdef CONFIG_PM_TEST_SUSPEND
213 /* kernel/power/suspend_test.c */
214 extern void suspend_test_start(void);
215 extern void suspend_test_finish(const char *label);
216 #else /* !CONFIG_PM_TEST_SUSPEND */
suspend_test_start(void)217 static inline void suspend_test_start(void)
218 {
219 }
suspend_test_finish(const char * label)220 static inline void suspend_test_finish(const char *label)
221 {
222 }
223 #endif /* !CONFIG_PM_TEST_SUSPEND */
224
225 #ifdef CONFIG_PM_SLEEP
226 /* kernel/power/main.c */
227 extern int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down);
228 extern int pm_notifier_call_chain(unsigned long val);
229 #endif
230
231 #ifdef CONFIG_HIGHMEM
232 int restore_highmem(void);
233 #else
count_highmem_pages(void)234 static inline unsigned int count_highmem_pages(void)
235 {
236 return 0;
237 }
restore_highmem(void)238 static inline int restore_highmem(void)
239 {
240 return 0;
241 }
242 #endif
243
244 /*
245 * Suspend test levels
246 */
247 enum {
248 /* keep first */
249 TEST_NONE,
250 TEST_CORE,
251 TEST_CPUS,
252 TEST_PLATFORM,
253 TEST_DEVICES,
254 TEST_FREEZER,
255 /* keep last */
256 __TEST_AFTER_LAST
257 };
258
259 #define TEST_FIRST TEST_NONE
260 #define TEST_MAX (__TEST_AFTER_LAST - 1)
261
262 #ifdef CONFIG_PM_SLEEP_DEBUG
263 extern int pm_test_level;
264 #else
265 #define pm_test_level (TEST_NONE)
266 #endif
267
268 #ifdef CONFIG_SUSPEND_FREEZER
suspend_freeze_processes(void)269 static inline int suspend_freeze_processes(void)
270 {
271 int error;
272
273 error = freeze_processes();
274 /*
275 * freeze_processes() automatically thaws every task if freezing
276 * fails. So we need not do anything extra upon error.
277 */
278 if (error) {
279 return error;
280 }
281
282 error = freeze_kernel_threads();
283 /*
284 * freeze_kernel_threads() thaws only kernel threads upon freezing
285 * failure. So we have to thaw the userspace tasks ourselves.
286 */
287 if (error) {
288 thaw_processes();
289 }
290
291 return error;
292 }
293
suspend_thaw_processes(void)294 static inline void suspend_thaw_processes(void)
295 {
296 thaw_processes();
297 }
298 #else
suspend_freeze_processes(void)299 static inline int suspend_freeze_processes(void)
300 {
301 return 0;
302 }
303
suspend_thaw_processes(void)304 static inline void suspend_thaw_processes(void)
305 {
306 }
307 #endif
308
309 #ifdef CONFIG_PM_AUTOSLEEP
310
311 /* kernel/power/autosleep.c */
312 extern int pm_autosleep_init(void);
313 extern int pm_autosleep_lock(void);
314 extern void pm_autosleep_unlock(void);
315 extern suspend_state_t pm_autosleep_state(void);
316 extern int pm_autosleep_set_state(suspend_state_t state);
317
318 #else /* !CONFIG_PM_AUTOSLEEP */
319
pm_autosleep_init(void)320 static inline int pm_autosleep_init(void)
321 {
322 return 0;
323 }
pm_autosleep_lock(void)324 static inline int pm_autosleep_lock(void)
325 {
326 return 0;
327 }
pm_autosleep_unlock(void)328 static inline void pm_autosleep_unlock(void)
329 {
330 }
pm_autosleep_state(void)331 static inline suspend_state_t pm_autosleep_state(void)
332 {
333 return PM_SUSPEND_ON;
334 }
335
336 #endif /* !CONFIG_PM_AUTOSLEEP */
337
338 #ifdef CONFIG_PM_WAKELOCKS
339
340 /* kernel/power/wakelock.c */
341 extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
342 extern int pm_wake_lock(const char *buf);
343 extern int pm_wake_unlock(const char *buf);
344
345 #endif /* !CONFIG_PM_WAKELOCKS */
346 #endif
347