xref: /optee_os/core/include/kernel/pm.h (revision 5a913ee74d3c71af2a2860ce8a4e7aeab2916f9b)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2018, Linaro Limited
4  */
5 
6 #ifndef __KERNEL_PM_H
7 #define __KERNEL_PM_H
8 
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <tee_api_types.h>
12 
13 /*
14  * Platform hints on targeted power state. Hints are stored in a 32bit
15  * unsigned value. Lower bits defines generic resource bit flags. Higher
16  * bits stores a platform specific value specific platform driver may
17  * understand. Registered callbacks may choose to use or ignore these hints.
18  *
19  * PM_HINT_CLOCK_STATE - When set clock shall be suspended/restored
20  * PM_HINT_POWER_STATE - When set device power shall be suspended/restored
21  * PM_HINT_IO_STATE - When set IO pins shall be suspended/restored
22  * PM_HINT_CONTEXT_STATE - When set the full context be suspended/restored
23  * PM_HINT_PLATFORM_STATE_MASK - Bit mask reserved for platform specific hints
24  * PM_HINT_PLATFORM_STATE_SHIFT - LSBit position of platform specific hints mask
25  */
26 #define PM_HINT_CLOCK_STATE		BIT(0)
27 #define PM_HINT_POWER_STATE		BIT(1)
28 #define PM_HINT_IO_STATE		BIT(2)
29 #define PM_HINT_CONTEXT_STATE		BIT(3)
30 #define PM_HINT_PLATFORM_STATE_MASK	GENMASK_32(31, 16)
31 #define PM_HINT_PLATFORM_STATE_SHIFT	16
32 
33 /*
34  * PM_OP_SUSPEND: platform is suspending to a target low power state
35  * PM_OP_RESUME: platform is resuming from low power state
36  */
37 enum pm_op {
38 	PM_OP_SUSPEND = 0,
39 	PM_OP_RESUME = 1,
40 };
41 
42 /*
43  * Registered callbacks are called the ordering directives specified
44  * by the PM_CB_ORDER_* value. Driver ordered callbacks at suspended
45  * first/resumed last. Core service ordered callbacks are suspended
46  * last/resumed first.
47  */
48 enum pm_callback_order {
49 	PM_CB_ORDER_DRIVER = 0,
50 	PM_CB_ORDER_CORE_SERVICE,
51 	PM_CB_ORDER_MAX
52 };
53 
54 #define PM_CALLBACK_HANDLE_INITIALIZER(_callback, _handle, _order)	\
55 		(struct pm_callback_handle){				\
56 			.callback = (_callback),			\
57 			.handle = (_handle),				\
58 			.order = (_order),				\
59 		}
60 
61 #define PM_CALLBACK_GET_HANDLE(pm_handle)	((pm_handle)->handle)
62 
63 struct pm_callback_handle;
64 typedef TEE_Result (*pm_callback)(enum pm_op op, uint32_t pm_hint,
65 				  const struct pm_callback_handle *pm_handle);
66 
67 /*
68  * Drivers and services can register a callback function for the platform
69  * suspend and resume sequences. A private address handle can be registered
70  * with the callback and retrieved from the callback. Callback can be
71  * registered with a specific call order as defined per PM_CB_ORDER_*.
72  *
73  * Callback shall return an error if failing to complete target transition.
74  * This information may be used by the platform to resume a platform on
75  * non-fatal failure to suspend.
76  *
77  * Callback implementations should ensure their functions belong to unpaged
78  * memory sections (see KEEP_PAGER()) since the callback is likely to be
79  * called from an unpaged execution context.
80  *
81  * Power Mamagement callback functions API:
82  *
83  * TEE_Result (*callback)(enum pm_op op,
84  *			  unsigned int pm_hint,
85  *			  const struct pm_callback_handle *pm_handle);
86  *
87  * @op - Target operation: either PM_SUSPEND or PM_RESUME
88  * @pm_hint - Hints on power state platform suspends to /resumes from.
89  *		PM_STATE_HINT_* defines the supported values.
90  * @pm_handle - Reference to the struct pm_callback_handle related to to
91  *		registered callback. Callback can retrieve the registered
92  *		private handle with PM_CALLBACK_GET_HANDLE().
93  *
94  * Return a TEE_Result compliant return code
95  */
96 /*
97  * struct pm_callback_handle store the callback registration directives.
98  *
99  * @callback - Registered callback function
100  * @handle - Registered private handler for the callback
101  * @order - Registered callback call order priority (PM_CB_ORDER_*)
102  */
103 struct pm_callback_handle {
104 	/* Set by the caller when registering a callback */
105 	pm_callback callback;
106 	void *handle;
107 	uint8_t order;
108 	/* Set by the system according to execution context */
109 	uint8_t flags;
110 };
111 
112 /*
113  * Register a callback for suspend/resume sequence
114  * Refer to struct pm_callback_handle for description of the callbacks
115  * API and the registration directives.
116  *
117  * @pm_handle: Reference callback registration directives
118  */
119 void register_pm_cb(struct pm_callback_handle *pm_handle);
120 
121 /*
122  * Register a driver callback for generic suspend/resume.
123  * Refer to struct pm_callback_handle for description of the callbacks
124  * API.
125  *
126  * @callback: Registered callback function
127  * @handle: Registered private handle argument for the callback
128  */
129 static inline void register_pm_driver_cb(pm_callback callback, void *handle)
130 {
131 	register_pm_cb(&PM_CALLBACK_HANDLE_INITIALIZER(callback, handle,
132 						       PM_CB_ORDER_DRIVER));
133 }
134 
135 /*
136  * Register a core service callback for generic suspend/resume.
137  * Refer to struct pm_callback_handle for description of the callbacks
138  * API.
139  *
140  * @callback: Registered callback function
141  * @handle: Registered private handle argument for the callback
142  */
143 static inline void register_pm_core_service_cb(pm_callback callback,
144 					       void *handle)
145 {
146 	register_pm_cb(&PM_CALLBACK_HANDLE_INITIALIZER(callback, handle,
147 						PM_CB_ORDER_CORE_SERVICE));
148 }
149 
150 /*
151  * Request call to registered PM callbacks
152  *
153  * @op: Either PM_OP_SUSPEND or PM_OP_RESUME
154  * @pm_hint: Hint (PM_HINT_*) on state the platform suspends to/resumes from.
155  *
156  * Return a TEE_Result compliant status
157  */
158 TEE_Result pm_change_state(enum pm_op op, uint32_t pm_hint);
159 
160 #endif /*__KERNEL_PM_H*/
161