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