1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2018 Intel Corporation. All rights reserved.
4  *
5  * Author: Tomasz Lauda <tomasz.lauda@linux.intel.com>
6  *         Janusz Jankowski <janusz.jankowski@linux.intel.com>
7  */
8 
9 /**
10  * \file include/sof/lib/pm_runtime.h
11  * \brief Runtime power management header file
12  * \author Tomasz Lauda <tomasz.lauda@linux.intel.com>
13  */
14 
15 #ifndef __SOF_LIB_PM_RUNTIME_H__
16 #define __SOF_LIB_PM_RUNTIME_H__
17 
18 #include <platform/lib/pm_runtime.h>
19 #include <sof/sof.h>
20 #include <sof/spinlock.h>
21 #include <sof/trace/trace.h>
22 #include <user/trace.h>
23 #include <stdint.h>
24 
25 /** \addtogroup pm_runtime PM Runtime
26  *  PM runtime specification.
27  *  @{
28  */
29 
30 /* PM runtime flags */
31 
32 #define RPM_ASYNC		0x01	/**< Request is asynchronous */
33 
34 /** \brief Runtime power management context */
35 enum pm_runtime_context {
36 	PM_RUNTIME_HOST_DMA_L1 = 0,	/**< Host DMA L1 */
37 	SSP_CLK,			/**< SSP Clock */
38 	SSP_POW,			/**< SSP Power */
39 	DMIC_CLK,			/**< DMIC Clock */
40 	DMIC_POW,			/**< DMIC Power */
41 	DW_DMAC_CLK,			/**< DW DMAC Clock */
42 	CORE_MEMORY_POW,		/**< Core Memory power */
43 	CORE_HP_CLK,			/**< High Performance Clock*/
44 	PM_RUNTIME_DSP			/**< DSP */
45 };
46 
47 /** \brief Runtime power management data. */
48 struct pm_runtime_data {
49 	spinlock_t lock;	/**< lock mechanism */
50 	void *platform_data;	/**< platform specific data */
51 #if CONFIG_DSP_RESIDENCY_COUNTERS
52 	struct r_counters_data *r_counters; /**< diagnostic DSP residency counters */
53 #endif
54 };
55 
56 #if CONFIG_DSP_RESIDENCY_COUNTERS
57 /**
58  * \brief DSP residency counters
59  * R0, R1, R2 are DSP residency counters which can be used differently
60  * based on platform implementation.
61  * In general R0 is the highest power consumption state while R2 is
62  * the lowest power consumption state. See platform specific pm_runtime.h
63  * for the platform HW specific mapping.
64  */
65 enum dsp_r_state {
66 	r0_r_state = 0,
67 	r1_r_state,
68 	r2_r_state
69 };
70 
71 /** \brief Diagnostic DSP residency counters data */
72 struct r_counters_data {
73 	enum dsp_r_state cur_r_state;	/**< current dsp_r_state */
74 	uint64_t ts;					/**< dsp_r_state timestamp */
75 };
76 #endif
77 
78 /**
79  * \brief Initializes runtime power management.
80  */
81 void pm_runtime_init(struct sof *sof);
82 
83 /**
84  * \brief Retrieves power management resource (async).
85  *
86  * \param[in] context Type of power management context.
87  * \param[in] index Index of the device.
88  */
89 void pm_runtime_get(enum pm_runtime_context context, uint32_t index);
90 
91 /**
92  * \brief Retrieves power management resource.
93  *
94  * \param[in] context Type of power management context.
95  * \param[in] index Index of the device.
96  */
97 void pm_runtime_get_sync(enum pm_runtime_context context, uint32_t index);
98 
99 /**
100  * \brief Releases power management resource (async).
101  *
102  * \param[in] context Type of power management context.
103  * \param[in] index Index of the device.
104  */
105 void pm_runtime_put(enum pm_runtime_context context, uint32_t index);
106 
107 /**
108  * \brief Releases power management resource.
109  *
110  * \param[in] context Type of power management context.
111  * \param[in] index Index of the device.
112  */
113 void pm_runtime_put_sync(enum pm_runtime_context context, uint32_t index);
114 
115 /**
116  * \brief Enables power management operations for the resource.
117  *
118  * \param[in] context Type of power management context.
119  * \param[in] index Index of the device.
120  */
121 void pm_runtime_enable(enum pm_runtime_context context, uint32_t index);
122 
123 /**
124  * \brief Disables power management operations for the resource.
125  *
126  * \param[in] context Type of power management context.
127  * \param[in] index Index of the device.
128  */
129 void pm_runtime_disable(enum pm_runtime_context context, uint32_t index);
130 
131 /**
132  * \brief Reports state of the power managed resource.
133  *
134  * @param context Type of power management context.
135  * @param index Index of the resource.
136  *
137  * @return true if the resource is active or pm disabled, false otherwise.
138  */
139 bool pm_runtime_is_active(enum pm_runtime_context context, uint32_t index);
140 
141 /**
142  * \brief Retrieves pointer to runtime power management data.
143  *
144  * @return Runtime power management data pointer.
145  */
pm_runtime_data_get(void)146 static inline struct pm_runtime_data *pm_runtime_data_get(void)
147 {
148 	return sof_get()->prd;
149 }
150 
151 #if CONFIG_DSP_RESIDENCY_COUNTERS
152 /**
153  * \brief Initializes DSP residency counters.
154  *
155  * \param[in] context Type of power management context.
156  */
157 void init_dsp_r_state(enum dsp_r_state);
158 
159 /**
160  * \brief Reports DSP residency state.
161  *
162  * \param[in] new state
163  */
164 void report_dsp_r_state(enum dsp_r_state);
165 
166 /**
167  * \brief Retrieves current DSP residency state.
168  *
169  * @return active DSP residency state
170  */
171 enum dsp_r_state get_dsp_r_state(void);
172 #endif
173 
174 /** @}*/
175 
176 #endif /* __SOF_LIB_PM_RUNTIME_H__ */
177