1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework 4 * for Non-CPU Devices. 5 * 6 * Copyright (C) 2011 Samsung Electronics 7 * MyungJoo Ham <[email protected]> 8 */ 9 10 #ifndef __LINUX_DEVFREQ_H__ 11 #define __LINUX_DEVFREQ_H__ 12 13 #include <linux/device.h> 14 #include <linux/notifier.h> 15 #include <linux/pm_opp.h> 16 17 #define DEVFREQ_NAME_LEN 16 18 19 /* DEVFREQ governor name */ 20 #define DEVFREQ_GOV_SIMPLE_ONDEMAND "simple_ondemand" 21 #define DEVFREQ_GOV_PERFORMANCE "performance" 22 #define DEVFREQ_GOV_POWERSAVE "powersave" 23 #define DEVFREQ_GOV_USERSPACE "userspace" 24 #define DEVFREQ_GOV_PASSIVE "passive" 25 26 /* DEVFREQ notifier interface */ 27 #define DEVFREQ_TRANSITION_NOTIFIER (0) 28 29 /* Transition notifiers of DEVFREQ_TRANSITION_NOTIFIER */ 30 #define DEVFREQ_PRECHANGE (0) 31 #define DEVFREQ_POSTCHANGE (1) 32 33 struct devfreq; 34 struct devfreq_governor; 35 36 /** 37 * struct devfreq_dev_status - Data given from devfreq user device to 38 * governors. Represents the performance 39 * statistics. 40 * @total_time: The total time represented by this instance of 41 * devfreq_dev_status 42 * @busy_time: The time that the device was working among the 43 * total_time. 44 * @current_frequency: The operating frequency. 45 * @private_data: An entry not specified by the devfreq framework. 46 * A device and a specific governor may have their 47 * own protocol with private_data. However, because 48 * this is governor-specific, a governor using this 49 * will be only compatible with devices aware of it. 50 */ 51 struct devfreq_dev_status { 52 /* both since the last measure */ 53 unsigned long total_time; 54 unsigned long busy_time; 55 unsigned long current_frequency; 56 void *private_data; 57 }; 58 59 /* 60 * The resulting frequency should be at most this. (this bound is the 61 * least upper bound; thus, the resulting freq should be lower or same) 62 * If the flag is not set, the resulting frequency should be at most the 63 * bound (greatest lower bound) 64 */ 65 #define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1 66 67 /** 68 * struct devfreq_dev_profile - Devfreq's user device profile 69 * @initial_freq: The operating frequency when devfreq_add_device() is 70 * called. 71 * @polling_ms: The polling interval in ms. 0 disables polling. 72 * @target: The device should set its operating frequency at 73 * freq or lowest-upper-than-freq value. If freq is 74 * higher than any operable frequency, set maximum. 75 * Before returning, target function should set 76 * freq at the current frequency. 77 * The "flags" parameter's possible values are 78 * explained above with "DEVFREQ_FLAG_*" macros. 79 * @get_dev_status: The device should provide the current performance 80 * status to devfreq. Governors are recommended not to 81 * use this directly. Instead, governors are recommended 82 * to use devfreq_update_stats() along with 83 * devfreq.last_status. 84 * @get_cur_freq: The device should provide the current frequency 85 * at which it is operating. 86 * @exit: An optional callback that is called when devfreq 87 * is removing the devfreq object due to error or 88 * from devfreq_remove_device() call. If the user 89 * has registered devfreq->nb at a notifier-head, 90 * this is the time to unregister it. 91 * @freq_table: Optional list of frequencies to support statistics 92 * and freq_table must be generated in ascending order. 93 * @max_state: The size of freq_table. 94 */ 95 struct devfreq_dev_profile { 96 unsigned long initial_freq; 97 unsigned int polling_ms; 98 99 int (*target)(struct device *dev, unsigned long *freq, u32 flags); 100 int (*get_dev_status)(struct device *dev, 101 struct devfreq_dev_status *stat); 102 int (*get_cur_freq)(struct device *dev, unsigned long *freq); 103 void (*exit)(struct device *dev); 104 105 unsigned long *freq_table; 106 unsigned int max_state; 107 }; 108 109 /** 110 * struct devfreq - Device devfreq structure 111 * @node: list node - contains the devices with devfreq that have been 112 * registered. 113 * @lock: a mutex to protect accessing devfreq. 114 * @dev: device registered by devfreq class. dev.parent is the device 115 * using devfreq. 116 * @profile: device-specific devfreq profile 117 * @governor: method how to choose frequency based on the usage. 118 * @governor_name: devfreq governor name for use with this devfreq 119 * @nb: notifier block used to notify devfreq object that it should 120 * reevaluate operable frequencies. Devfreq users may use 121 * devfreq.nb to the corresponding register notifier call chain. 122 * @work: delayed work for load monitoring. 123 * @previous_freq: previously configured frequency value. 124 * @data: Private data of the governor. The devfreq framework does not 125 * touch this. 126 * @min_freq: Limit minimum frequency requested by user (0: none) 127 * @max_freq: Limit maximum frequency requested by user (0: none) 128 * @scaling_min_freq: Limit minimum frequency requested by OPP interface 129 * @scaling_max_freq: Limit maximum frequency requested by OPP interface 130 * @stop_polling: devfreq polling status of a device. 131 * @suspend_freq: frequency of a device set during suspend phase. 132 * @resume_freq: frequency of a device set in resume phase. 133 * @suspend_count: suspend requests counter for a device. 134 * @total_trans: Number of devfreq transitions 135 * @trans_table: Statistics of devfreq transitions 136 * @time_in_state: Statistics of devfreq states 137 * @last_stat_updated: The last time stat updated 138 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier 139 * @nb_min: Notifier block for DEV_PM_QOS_MIN_FREQUENCY 140 * @nb_max: Notifier block for DEV_PM_QOS_MAX_FREQUENCY 141 * 142 * This structure stores the devfreq information for a give device. 143 * 144 * Note that when a governor accesses entries in struct devfreq in its 145 * functions except for the context of callbacks defined in struct 146 * devfreq_governor, the governor should protect its access with the 147 * struct mutex lock in struct devfreq. A governor may use this mutex 148 * to protect its own private data in void *data as well. 149 */ 150 struct devfreq { 151 struct list_head node; 152 153 struct mutex lock; 154 struct device dev; 155 struct devfreq_dev_profile *profile; 156 const struct devfreq_governor *governor; 157 char governor_name[DEVFREQ_NAME_LEN]; 158 struct notifier_block nb; 159 struct delayed_work work; 160 161 unsigned long previous_freq; 162 struct devfreq_dev_status last_status; 163 164 void *data; /* private data for governors */ 165 166 unsigned long min_freq; 167 unsigned long max_freq; 168 unsigned long scaling_min_freq; 169 unsigned long scaling_max_freq; 170 bool stop_polling; 171 172 unsigned long suspend_freq; 173 unsigned long resume_freq; 174 atomic_t suspend_count; 175 176 /* information for device frequency transition */ 177 unsigned int total_trans; 178 unsigned int *trans_table; 179 unsigned long *time_in_state; 180 unsigned long last_stat_updated; 181 182 struct srcu_notifier_head transition_notifier_list; 183 184 struct notifier_block nb_min; 185 struct notifier_block nb_max; 186 }; 187 188 struct devfreq_freqs { 189 unsigned long old; 190 unsigned long new; 191 }; 192 193 #if defined(CONFIG_PM_DEVFREQ) 194 extern struct devfreq *devfreq_add_device(struct device *dev, 195 struct devfreq_dev_profile *profile, 196 const char *governor_name, 197 void *data); 198 extern int devfreq_remove_device(struct devfreq *devfreq); 199 extern struct devfreq *devm_devfreq_add_device(struct device *dev, 200 struct devfreq_dev_profile *profile, 201 const char *governor_name, 202 void *data); 203 extern void devm_devfreq_remove_device(struct device *dev, 204 struct devfreq *devfreq); 205 206 /* Supposed to be called by PM callbacks */ 207 extern int devfreq_suspend_device(struct devfreq *devfreq); 208 extern int devfreq_resume_device(struct devfreq *devfreq); 209 210 extern void devfreq_suspend(void); 211 extern void devfreq_resume(void); 212 213 /** 214 * update_devfreq() - Reevaluate the device and configure frequency 215 * @devfreq: the devfreq device 216 * 217 * Note: devfreq->lock must be held 218 */ 219 extern int update_devfreq(struct devfreq *devfreq); 220 221 /* Helper functions for devfreq user device driver with OPP. */ 222 extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev, 223 unsigned long *freq, u32 flags); 224 extern int devfreq_register_opp_notifier(struct device *dev, 225 struct devfreq *devfreq); 226 extern int devfreq_unregister_opp_notifier(struct device *dev, 227 struct devfreq *devfreq); 228 extern int devm_devfreq_register_opp_notifier(struct device *dev, 229 struct devfreq *devfreq); 230 extern void devm_devfreq_unregister_opp_notifier(struct device *dev, 231 struct devfreq *devfreq); 232 extern int devfreq_register_notifier(struct devfreq *devfreq, 233 struct notifier_block *nb, 234 unsigned int list); 235 extern int devfreq_unregister_notifier(struct devfreq *devfreq, 236 struct notifier_block *nb, 237 unsigned int list); 238 extern int devm_devfreq_register_notifier(struct device *dev, 239 struct devfreq *devfreq, 240 struct notifier_block *nb, 241 unsigned int list); 242 extern void devm_devfreq_unregister_notifier(struct device *dev, 243 struct devfreq *devfreq, 244 struct notifier_block *nb, 245 unsigned int list); 246 extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, 247 int index); 248 249 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND) 250 /** 251 * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq 252 * and devfreq_add_device 253 * @upthreshold: If the load is over this value, the frequency jumps. 254 * Specify 0 to use the default. Valid value = 0 to 100. 255 * @downdifferential: If the load is under upthreshold - downdifferential, 256 * the governor may consider slowing the frequency down. 257 * Specify 0 to use the default. Valid value = 0 to 100. 258 * downdifferential < upthreshold must hold. 259 * 260 * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor, 261 * the governor uses the default values. 262 */ 263 struct devfreq_simple_ondemand_data { 264 unsigned int upthreshold; 265 unsigned int downdifferential; 266 }; 267 #endif 268 269 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE) 270 /** 271 * struct devfreq_passive_data - void *data fed to struct devfreq 272 * and devfreq_add_device 273 * @parent: the devfreq instance of parent device. 274 * @get_target_freq: Optional callback, Returns desired operating frequency 275 * for the device using passive governor. That is called 276 * when passive governor should decide the next frequency 277 * by using the new frequency of parent devfreq device 278 * using governors except for passive governor. 279 * If the devfreq device has the specific method to decide 280 * the next frequency, should use this callback. 281 * @this: the devfreq instance of own device. 282 * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list 283 * 284 * The devfreq_passive_data have to set the devfreq instance of parent 285 * device with governors except for the passive governor. But, don't need to 286 * initialize the 'this' and 'nb' field because the devfreq core will handle 287 * them. 288 */ 289 struct devfreq_passive_data { 290 /* Should set the devfreq instance of parent device */ 291 struct devfreq *parent; 292 293 /* Optional callback to decide the next frequency of passvice device */ 294 int (*get_target_freq)(struct devfreq *this, unsigned long *freq); 295 296 /* For passive governor's internal use. Don't need to set them */ 297 struct devfreq *this; 298 struct notifier_block nb; 299 }; 300 #endif 301 302 #else /* !CONFIG_PM_DEVFREQ */ 303 static inline struct devfreq *devfreq_add_device(struct device *dev, 304 struct devfreq_dev_profile *profile, 305 const char *governor_name, 306 void *data) 307 { 308 return ERR_PTR(-ENOSYS); 309 } 310 311 static inline int devfreq_remove_device(struct devfreq *devfreq) 312 { 313 return 0; 314 } 315 316 static inline struct devfreq *devm_devfreq_add_device(struct device *dev, 317 struct devfreq_dev_profile *profile, 318 const char *governor_name, 319 void *data) 320 { 321 return ERR_PTR(-ENOSYS); 322 } 323 324 static inline void devm_devfreq_remove_device(struct device *dev, 325 struct devfreq *devfreq) 326 { 327 } 328 329 static inline int devfreq_suspend_device(struct devfreq *devfreq) 330 { 331 return 0; 332 } 333 334 static inline int devfreq_resume_device(struct devfreq *devfreq) 335 { 336 return 0; 337 } 338 339 static inline void devfreq_suspend(void) {} 340 static inline void devfreq_resume(void) {} 341 342 static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev, 343 unsigned long *freq, u32 flags) 344 { 345 return ERR_PTR(-EINVAL); 346 } 347 348 static inline int devfreq_register_opp_notifier(struct device *dev, 349 struct devfreq *devfreq) 350 { 351 return -EINVAL; 352 } 353 354 static inline int devfreq_unregister_opp_notifier(struct device *dev, 355 struct devfreq *devfreq) 356 { 357 return -EINVAL; 358 } 359 360 static inline int devm_devfreq_register_opp_notifier(struct device *dev, 361 struct devfreq *devfreq) 362 { 363 return -EINVAL; 364 } 365 366 static inline void devm_devfreq_unregister_opp_notifier(struct device *dev, 367 struct devfreq *devfreq) 368 { 369 } 370 371 static inline int devfreq_register_notifier(struct devfreq *devfreq, 372 struct notifier_block *nb, 373 unsigned int list) 374 { 375 return 0; 376 } 377 378 static inline int devfreq_unregister_notifier(struct devfreq *devfreq, 379 struct notifier_block *nb, 380 unsigned int list) 381 { 382 return 0; 383 } 384 385 static inline int devm_devfreq_register_notifier(struct device *dev, 386 struct devfreq *devfreq, 387 struct notifier_block *nb, 388 unsigned int list) 389 { 390 return 0; 391 } 392 393 static inline void devm_devfreq_unregister_notifier(struct device *dev, 394 struct devfreq *devfreq, 395 struct notifier_block *nb, 396 unsigned int list) 397 { 398 } 399 400 static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, 401 int index) 402 { 403 return ERR_PTR(-ENODEV); 404 } 405 406 static inline int devfreq_update_stats(struct devfreq *df) 407 { 408 return -EINVAL; 409 } 410 #endif /* CONFIG_PM_DEVFREQ */ 411 412 #endif /* __LINUX_DEVFREQ_H__ */ 413