xref: /linux/Documentation/power/userland-swsusp.rst (revision ead5d1f4d877e92c051e1a1ade623d0d30e71619)
1151f4e2bSMauro Carvalho Chehab=====================================================
26e1819d6SRafael J. WysockiDocumentation for userland software suspend interface
3151f4e2bSMauro Carvalho Chehab=====================================================
4151f4e2bSMauro Carvalho Chehab
56e1819d6SRafael J. Wysocki	(C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
66e1819d6SRafael J. Wysocki
76e1819d6SRafael J. WysockiFirst, the warnings at the beginning of swsusp.txt still apply.
86e1819d6SRafael J. Wysocki
96e1819d6SRafael J. WysockiSecond, you should read the FAQ in swsusp.txt _now_ if you have not
106e1819d6SRafael J. Wysockidone it already.
116e1819d6SRafael J. Wysocki
126e1819d6SRafael J. WysockiNow, to use the userland interface for software suspend you need special
136e1819d6SRafael J. Wysockiutilities that will read/write the system memory snapshot from/to the
146e1819d6SRafael J. Wysockikernel.  Such utilities are available, for example, from
15bf73bae6SRafael J. Wysocki<http://suspend.sourceforge.net>.  You may want to have a look at them if you
16bf73bae6SRafael J. Wysockiare going to develop your own suspend/resume utilities.
176e1819d6SRafael J. Wysocki
186e1819d6SRafael J. WysockiThe interface consists of a character device providing the open(),
196e1819d6SRafael J. Wysockirelease(), read(), and write() operations as well as several ioctl()
203010f8caSRafael J. Wysockicommands defined in include/linux/suspend_ioctls.h .  The major and minor
216e1819d6SRafael J. Wysockinumbers of the device are, respectively, 10 and 231, and they can
226e1819d6SRafael J. Wysockibe read from /sys/class/misc/snapshot/dev.
236e1819d6SRafael J. Wysocki
246e1819d6SRafael J. WysockiThe device can be open either for reading or for writing.  If open for
256e1819d6SRafael J. Wysockireading, it is considered to be in the suspend mode.  Otherwise it is
26bf73bae6SRafael J. Wysockiassumed to be in the resume mode.  The device cannot be open for simultaneous
27bf73bae6SRafael J. Wysockireading and writing.  It is also impossible to have the device open more than
28bf73bae6SRafael J. Wysockionce at a time.
296e1819d6SRafael J. Wysocki
30bc6a0cbdSPavel MachekEven opening the device has side effects. Data structures are
31bc6a0cbdSPavel Machekallocated, and PM_HIBERNATION_PREPARE / PM_RESTORE_PREPARE chains are
32bc6a0cbdSPavel Machekcalled.
33bc6a0cbdSPavel Machek
346e1819d6SRafael J. WysockiThe ioctl() commands recognized by the device are:
356e1819d6SRafael J. Wysocki
36151f4e2bSMauro Carvalho ChehabSNAPSHOT_FREEZE
37151f4e2bSMauro Carvalho Chehab	freeze user space processes (the current process is
38cc5d207cSRafael J. Wysocki	not frozen); this is required for SNAPSHOT_CREATE_IMAGE
396e1819d6SRafael J. Wysocki	and SNAPSHOT_ATOMIC_RESTORE to succeed
406e1819d6SRafael J. Wysocki
41151f4e2bSMauro Carvalho ChehabSNAPSHOT_UNFREEZE
42151f4e2bSMauro Carvalho Chehab	thaw user space processes frozen by SNAPSHOT_FREEZE
436e1819d6SRafael J. Wysocki
44151f4e2bSMauro Carvalho ChehabSNAPSHOT_CREATE_IMAGE
45151f4e2bSMauro Carvalho Chehab	create a snapshot of the system memory; the
466e1819d6SRafael J. Wysocki	last argument of ioctl() should be a pointer to an int variable,
476e1819d6SRafael J. Wysocki	the value of which will indicate whether the call returned after
486e1819d6SRafael J. Wysocki	creating the snapshot (1) or after restoring the system memory state
496e1819d6SRafael J. Wysocki	from it (0) (after resume the system finds itself finishing the
50cc5d207cSRafael J. Wysocki	SNAPSHOT_CREATE_IMAGE ioctl() again); after the snapshot
516e1819d6SRafael J. Wysocki	has been created the read() operation can be used to transfer
526e1819d6SRafael J. Wysocki	it out of the kernel
536e1819d6SRafael J. Wysocki
54151f4e2bSMauro Carvalho ChehabSNAPSHOT_ATOMIC_RESTORE
55151f4e2bSMauro Carvalho Chehab	restore the system memory state from the
566e1819d6SRafael J. Wysocki	uploaded snapshot image; before calling it you should transfer
576e1819d6SRafael J. Wysocki	the system memory snapshot back to the kernel using the write()
586e1819d6SRafael J. Wysocki	operation; this call will not succeed if the snapshot
596e1819d6SRafael J. Wysocki	image is not available to the kernel
606e1819d6SRafael J. Wysocki
61151f4e2bSMauro Carvalho ChehabSNAPSHOT_FREE
62151f4e2bSMauro Carvalho Chehab	free memory allocated for the snapshot image
636e1819d6SRafael J. Wysocki
64151f4e2bSMauro Carvalho ChehabSNAPSHOT_PREF_IMAGE_SIZE
65151f4e2bSMauro Carvalho Chehab	set the preferred maximum size of the image
666e1819d6SRafael J. Wysocki	(the kernel will do its best to ensure the image size will not exceed
676e1819d6SRafael J. Wysocki	this number, but if it turns out to be impossible, the kernel will
686e1819d6SRafael J. Wysocki	create the smallest image possible)
696e1819d6SRafael J. Wysocki
70151f4e2bSMauro Carvalho ChehabSNAPSHOT_GET_IMAGE_SIZE
71151f4e2bSMauro Carvalho Chehab	return the actual size of the hibernation image
72*51995ff5SEric Biggers	(the last argument should be a pointer to a loff_t variable that
73*51995ff5SEric Biggers	will contain the result if the call is successful)
74af508b34SRafael J. Wysocki
75151f4e2bSMauro Carvalho ChehabSNAPSHOT_AVAIL_SWAP_SIZE
76*51995ff5SEric Biggers	return the amount of available swap in bytes
77*51995ff5SEric Biggers	(the last argument should be a pointer to a loff_t variable that
78*51995ff5SEric Biggers	will contain the result if the call is successful)
796e1819d6SRafael J. Wysocki
80151f4e2bSMauro Carvalho ChehabSNAPSHOT_ALLOC_SWAP_PAGE
81151f4e2bSMauro Carvalho Chehab	allocate a swap page from the resume partition
826e1819d6SRafael J. Wysocki	(the last argument should be a pointer to a loff_t variable that
836e1819d6SRafael J. Wysocki	will contain the swap page offset if the call is successful)
846e1819d6SRafael J. Wysocki
85151f4e2bSMauro Carvalho ChehabSNAPSHOT_FREE_SWAP_PAGES
86151f4e2bSMauro Carvalho Chehab	free all swap pages allocated by
87cc5d207cSRafael J. Wysocki	SNAPSHOT_ALLOC_SWAP_PAGE
886e1819d6SRafael J. Wysocki
89151f4e2bSMauro Carvalho ChehabSNAPSHOT_SET_SWAP_AREA
90151f4e2bSMauro Carvalho Chehab	set the resume partition and the offset (in <PAGE_SIZE>
91bf73bae6SRafael J. Wysocki	units) from the beginning of the partition at which the swap header is
92bf73bae6SRafael J. Wysocki	located (the last ioctl() argument should point to a struct
933010f8caSRafael J. Wysocki	resume_swap_area, as defined in kernel/power/suspend_ioctls.h,
943010f8caSRafael J. Wysocki	containing the resume device specification and the offset); for swap
953010f8caSRafael J. Wysocki	partitions the offset is always 0, but it is different from zero for
96151f4e2bSMauro Carvalho Chehab	swap files (see Documentation/power/swsusp-and-swap-files.rst for
97395cf969SPaul Bolle	details).
98bf73bae6SRafael J. Wysocki
99151f4e2bSMauro Carvalho ChehabSNAPSHOT_PLATFORM_SUPPORT
100151f4e2bSMauro Carvalho Chehab	enable/disable the hibernation platform support,
101eb57c1cfSRafael J. Wysocki	depending on the argument value (enable, if the argument is nonzero)
102eb57c1cfSRafael J. Wysocki
103151f4e2bSMauro Carvalho ChehabSNAPSHOT_POWER_OFF
104151f4e2bSMauro Carvalho Chehab	make the kernel transition the system to the hibernation
105eb57c1cfSRafael J. Wysocki	state (eg. ACPI S4) using the platform (eg. ACPI) driver
106eb57c1cfSRafael J. Wysocki
107151f4e2bSMauro Carvalho ChehabSNAPSHOT_S2RAM
108151f4e2bSMauro Carvalho Chehab	suspend to RAM; using this call causes the kernel to
109bf73bae6SRafael J. Wysocki	immediately enter the suspend-to-RAM state, so this call must always
110bf73bae6SRafael J. Wysocki	be preceded by the SNAPSHOT_FREEZE call and it is also necessary
111bf73bae6SRafael J. Wysocki	to use the SNAPSHOT_UNFREEZE call after the system wakes up.  This call
112bf73bae6SRafael J. Wysocki	is needed to implement the suspend-to-both mechanism in which the
113bf73bae6SRafael J. Wysocki	suspend image is first created, as though the system had been suspended
114bf73bae6SRafael J. Wysocki	to disk, and then the system is suspended to RAM (this makes it possible
115bf73bae6SRafael J. Wysocki	to resume the system from RAM if there's enough battery power or restore
116bf73bae6SRafael J. Wysocki	its state on the basis of the saved suspend image otherwise)
117bf73bae6SRafael J. Wysocki
1186e1819d6SRafael J. WysockiThe device's read() operation can be used to transfer the snapshot image from
1196e1819d6SRafael J. Wysockithe kernel.  It has the following limitations:
120151f4e2bSMauro Carvalho Chehab
1216e1819d6SRafael J. Wysocki- you cannot read() more than one virtual memory page at a time
1221f999d14SMasanari Iida- read()s across page boundaries are impossible (ie. if you read() 1/2 of
1236e1819d6SRafael J. Wysocki  a page in the previous call, you will only be able to read()
124151f4e2bSMauro Carvalho Chehab  **at most** 1/2 of the page in the next call)
1256e1819d6SRafael J. Wysocki
1266e1819d6SRafael J. WysockiThe device's write() operation is used for uploading the system memory snapshot
1276e1819d6SRafael J. Wysockiinto the kernel.  It has the same limitations as the read() operation.
1286e1819d6SRafael J. Wysocki
1296e1819d6SRafael J. WysockiThe release() operation frees all memory allocated for the snapshot image
130cc5d207cSRafael J. Wysockiand all swap pages allocated with SNAPSHOT_ALLOC_SWAP_PAGE (if any).
1316e1819d6SRafael J. WysockiThus it is not necessary to use either SNAPSHOT_FREE or
1326e1819d6SRafael J. WysockiSNAPSHOT_FREE_SWAP_PAGES before closing the device (in fact it will also
1336e1819d6SRafael J. Wysockiunfreeze user space processes frozen by SNAPSHOT_UNFREEZE if they are
1346e1819d6SRafael J. Wysockistill frozen when the device is being closed).
1356e1819d6SRafael J. Wysocki
1366e1819d6SRafael J. WysockiCurrently it is assumed that the userland utilities reading/writing the
13719f59460SMatt LaPlantesnapshot image from/to the kernel will use a swap partition, called the resume
138bf73bae6SRafael J. Wysockipartition, or a swap file as storage space (if a swap file is used, the resume
139bf73bae6SRafael J. Wysockipartition is the partition that holds this file).  However, this is not really
140bf73bae6SRafael J. Wysockirequired, as they can use, for example, a special (blank) suspend partition or
141cc5d207cSRafael J. Wysockia file on a partition that is unmounted before SNAPSHOT_CREATE_IMAGE and
142bf73bae6SRafael J. Wysockimounted afterwards.
1436e1819d6SRafael J. Wysocki
144af508b34SRafael J. WysockiThese utilities MUST NOT make any assumptions regarding the ordering of
145af508b34SRafael J. Wysockidata within the snapshot image.  The contents of the image are entirely owned
146af508b34SRafael J. Wysockiby the kernel and its structure may be changed in future kernel releases.
1476e1819d6SRafael J. Wysocki
1486e1819d6SRafael J. WysockiThe snapshot image MUST be written to the kernel unaltered (ie. all of the image
1496e1819d6SRafael J. Wysockidata, metadata and header MUST be written in _exactly_ the same amount, form
1506e1819d6SRafael J. Wysockiand order in which they have been read).  Otherwise, the behavior of the
1516e1819d6SRafael J. Wysockiresumed system may be totally unpredictable.
1526e1819d6SRafael J. Wysocki
1536e1819d6SRafael J. WysockiWhile executing SNAPSHOT_ATOMIC_RESTORE the kernel checks if the
1546e1819d6SRafael J. Wysockistructure of the snapshot image is consistent with the information stored
1556e1819d6SRafael J. Wysockiin the image header.  If any inconsistencies are detected,
1566e1819d6SRafael J. WysockiSNAPSHOT_ATOMIC_RESTORE will not succeed.  Still, this is not a fool-proof
1576e1819d6SRafael J. Wysockimechanism and the userland utilities using the interface SHOULD use additional
1586e1819d6SRafael J. Wysockimeans, such as checksums, to ensure the integrity of the snapshot image.
1596e1819d6SRafael J. Wysocki
1606e1819d6SRafael J. WysockiThe suspending and resuming utilities MUST lock themselves in memory,
16125985edcSLucas De Marchipreferably using mlockall(), before calling SNAPSHOT_FREEZE.
1626e1819d6SRafael J. Wysocki
163cc5d207cSRafael J. WysockiThe suspending utility MUST check the value stored by SNAPSHOT_CREATE_IMAGE
1646e1819d6SRafael J. Wysockiin the memory location pointed to by the last argument of ioctl() and proceed
1656e1819d6SRafael J. Wysockiin accordance with it:
166151f4e2bSMauro Carvalho Chehab
1676e1819d6SRafael J. Wysocki1. 	If the value is 1 (ie. the system memory snapshot has just been
1686e1819d6SRafael J. Wysocki	created and the system is ready for saving it):
169151f4e2bSMauro Carvalho Chehab
1706e1819d6SRafael J. Wysocki	(a)	The suspending utility MUST NOT close the snapshot device
1716e1819d6SRafael J. Wysocki		_unless_ the whole suspend procedure is to be cancelled, in
1726e1819d6SRafael J. Wysocki		which case, if the snapshot image has already been saved, the
17325985edcSLucas De Marchi		suspending utility SHOULD destroy it, preferably by zapping
1746e1819d6SRafael J. Wysocki		its header.  If the suspend is not to be cancelled, the
1756e1819d6SRafael J. Wysocki		system MUST be powered off or rebooted after the snapshot
1766e1819d6SRafael J. Wysocki		image has been saved.
1776e1819d6SRafael J. Wysocki	(b)	The suspending utility SHOULD NOT attempt to perform any
1786e1819d6SRafael J. Wysocki		file system operations (including reads) on the file systems
179cc5d207cSRafael J. Wysocki		that were mounted before SNAPSHOT_CREATE_IMAGE has been
1806e1819d6SRafael J. Wysocki		called.  However, it MAY mount a file system that was not
1816e1819d6SRafael J. Wysocki		mounted at that time and perform some operations on it (eg.
1826e1819d6SRafael J. Wysocki		use it for saving the image).
183151f4e2bSMauro Carvalho Chehab
1846e1819d6SRafael J. Wysocki2.	If the value is 0 (ie. the system state has just been restored from
1856e1819d6SRafael J. Wysocki	the snapshot image), the suspending utility MUST close the snapshot
1866e1819d6SRafael J. Wysocki	device.  Afterwards it will be treated as a regular userland process,
1876e1819d6SRafael J. Wysocki	so it need not exit.
1886e1819d6SRafael J. Wysocki
1896e1819d6SRafael J. WysockiThe resuming utility SHOULD NOT attempt to mount any file systems that could
1906e1819d6SRafael J. Wysockibe mounted before suspend and SHOULD NOT attempt to perform any operations
1916e1819d6SRafael J. Wysockiinvolving such file systems.
1926e1819d6SRafael J. Wysocki
1936e1819d6SRafael J. WysockiFor details, please refer to the source code.
194