devfreq.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
  3. * for Non-CPU Devices.
  4. *
  5. * Copyright (C) 2011 Samsung Electronics
  6. * MyungJoo Ham <myungjoo.ham@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/stat.h>
  20. #include <linux/opp.h>
  21. #include <linux/devfreq.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/list.h>
  25. #include <linux/printk.h>
  26. #include <linux/hrtimer.h>
  27. #include "governor.h"
  28. struct class *devfreq_class;
  29. /*
  30. * devfreq core provides delayed work based load monitoring helper
  31. * functions. Governors can use these or can implement their own
  32. * monitoring mechanism.
  33. */
  34. static struct workqueue_struct *devfreq_wq;
  35. /* The list of all device-devfreq */
  36. static LIST_HEAD(devfreq_list);
  37. static DEFINE_MUTEX(devfreq_list_lock);
  38. /**
  39. * find_device_devfreq() - find devfreq struct using device pointer
  40. * @dev: device pointer used to lookup device devfreq.
  41. *
  42. * Search the list of device devfreqs and return the matched device's
  43. * devfreq info. devfreq_list_lock should be held by the caller.
  44. */
  45. static struct devfreq *find_device_devfreq(struct device *dev)
  46. {
  47. struct devfreq *tmp_devfreq;
  48. if (unlikely(IS_ERR_OR_NULL(dev))) {
  49. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  50. return ERR_PTR(-EINVAL);
  51. }
  52. WARN(!mutex_is_locked(&devfreq_list_lock),
  53. "devfreq_list_lock must be locked.");
  54. list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
  55. if (tmp_devfreq->dev.parent == dev)
  56. return tmp_devfreq;
  57. }
  58. return ERR_PTR(-ENODEV);
  59. }
  60. /* Load monitoring helper functions for governors use */
  61. /**
  62. * update_devfreq() - Reevaluate the device and configure frequency.
  63. * @devfreq: the devfreq instance.
  64. *
  65. * Note: Lock devfreq->lock before calling update_devfreq
  66. * This function is exported for governors.
  67. */
  68. int update_devfreq(struct devfreq *devfreq)
  69. {
  70. unsigned long freq;
  71. int err = 0;
  72. u32 flags = 0;
  73. if (!mutex_is_locked(&devfreq->lock)) {
  74. WARN(true, "devfreq->lock must be locked by the caller.\n");
  75. return -EINVAL;
  76. }
  77. /* Reevaluate the proper frequency */
  78. err = devfreq->governor->get_target_freq(devfreq, &freq);
  79. if (err)
  80. return err;
  81. /*
  82. * Adjust the freuqency with user freq and QoS.
  83. *
  84. * List from the highest proiority
  85. * max_freq (probably called by thermal when it's too hot)
  86. * min_freq
  87. */
  88. if (devfreq->min_freq && freq < devfreq->min_freq) {
  89. freq = devfreq->min_freq;
  90. flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
  91. }
  92. if (devfreq->max_freq && freq > devfreq->max_freq) {
  93. freq = devfreq->max_freq;
  94. flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
  95. }
  96. err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
  97. if (err)
  98. return err;
  99. devfreq->previous_freq = freq;
  100. return err;
  101. }
  102. /**
  103. * devfreq_monitor() - Periodically poll devfreq objects.
  104. * @work: the work struct used to run devfreq_monitor periodically.
  105. *
  106. */
  107. static void devfreq_monitor(struct work_struct *work)
  108. {
  109. int err;
  110. struct devfreq *devfreq = container_of(work,
  111. struct devfreq, work.work);
  112. mutex_lock(&devfreq->lock);
  113. err = update_devfreq(devfreq);
  114. if (err)
  115. dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
  116. queue_delayed_work(devfreq_wq, &devfreq->work,
  117. msecs_to_jiffies(devfreq->profile->polling_ms));
  118. mutex_unlock(&devfreq->lock);
  119. }
  120. /**
  121. * devfreq_monitor_start() - Start load monitoring of devfreq instance
  122. * @devfreq: the devfreq instance.
  123. *
  124. * Helper function for starting devfreq device load monitoing. By
  125. * default delayed work based monitoring is supported. Function
  126. * to be called from governor in response to DEVFREQ_GOV_START
  127. * event when device is added to devfreq framework.
  128. */
  129. void devfreq_monitor_start(struct devfreq *devfreq)
  130. {
  131. INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
  132. if (devfreq->profile->polling_ms)
  133. queue_delayed_work(devfreq_wq, &devfreq->work,
  134. msecs_to_jiffies(devfreq->profile->polling_ms));
  135. }
  136. /**
  137. * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
  138. * @devfreq: the devfreq instance.
  139. *
  140. * Helper function to stop devfreq device load monitoing. Function
  141. * to be called from governor in response to DEVFREQ_GOV_STOP
  142. * event when device is removed from devfreq framework.
  143. */
  144. void devfreq_monitor_stop(struct devfreq *devfreq)
  145. {
  146. cancel_delayed_work_sync(&devfreq->work);
  147. }
  148. /**
  149. * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
  150. * @devfreq: the devfreq instance.
  151. *
  152. * Helper function to suspend devfreq device load monitoing. Function
  153. * to be called from governor in response to DEVFREQ_GOV_SUSPEND
  154. * event or when polling interval is set to zero.
  155. *
  156. * Note: Though this function is same as devfreq_monitor_stop(),
  157. * intentionally kept separate to provide hooks for collecting
  158. * transition statistics.
  159. */
  160. void devfreq_monitor_suspend(struct devfreq *devfreq)
  161. {
  162. mutex_lock(&devfreq->lock);
  163. if (devfreq->stop_polling) {
  164. mutex_unlock(&devfreq->lock);
  165. return;
  166. }
  167. devfreq->stop_polling = true;
  168. mutex_unlock(&devfreq->lock);
  169. cancel_delayed_work_sync(&devfreq->work);
  170. }
  171. /**
  172. * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
  173. * @devfreq: the devfreq instance.
  174. *
  175. * Helper function to resume devfreq device load monitoing. Function
  176. * to be called from governor in response to DEVFREQ_GOV_RESUME
  177. * event or when polling interval is set to non-zero.
  178. */
  179. void devfreq_monitor_resume(struct devfreq *devfreq)
  180. {
  181. mutex_lock(&devfreq->lock);
  182. if (!devfreq->stop_polling)
  183. goto out;
  184. if (!delayed_work_pending(&devfreq->work) &&
  185. devfreq->profile->polling_ms)
  186. queue_delayed_work(devfreq_wq, &devfreq->work,
  187. msecs_to_jiffies(devfreq->profile->polling_ms));
  188. devfreq->stop_polling = false;
  189. out:
  190. mutex_unlock(&devfreq->lock);
  191. }
  192. /**
  193. * devfreq_interval_update() - Update device devfreq monitoring interval
  194. * @devfreq: the devfreq instance.
  195. * @delay: new polling interval to be set.
  196. *
  197. * Helper function to set new load monitoring polling interval. Function
  198. * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
  199. */
  200. void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
  201. {
  202. unsigned int cur_delay = devfreq->profile->polling_ms;
  203. unsigned int new_delay = *delay;
  204. mutex_lock(&devfreq->lock);
  205. devfreq->profile->polling_ms = new_delay;
  206. if (devfreq->stop_polling)
  207. goto out;
  208. /* if new delay is zero, stop polling */
  209. if (!new_delay) {
  210. mutex_unlock(&devfreq->lock);
  211. cancel_delayed_work_sync(&devfreq->work);
  212. return;
  213. }
  214. /* if current delay is zero, start polling with new delay */
  215. if (!cur_delay) {
  216. queue_delayed_work(devfreq_wq, &devfreq->work,
  217. msecs_to_jiffies(devfreq->profile->polling_ms));
  218. goto out;
  219. }
  220. /* if current delay is greater than new delay, restart polling */
  221. if (cur_delay > new_delay) {
  222. mutex_unlock(&devfreq->lock);
  223. cancel_delayed_work_sync(&devfreq->work);
  224. mutex_lock(&devfreq->lock);
  225. if (!devfreq->stop_polling)
  226. queue_delayed_work(devfreq_wq, &devfreq->work,
  227. msecs_to_jiffies(devfreq->profile->polling_ms));
  228. }
  229. out:
  230. mutex_unlock(&devfreq->lock);
  231. }
  232. /**
  233. * devfreq_notifier_call() - Notify that the device frequency requirements
  234. * has been changed out of devfreq framework.
  235. * @nb the notifier_block (supposed to be devfreq->nb)
  236. * @type not used
  237. * @devp not used
  238. *
  239. * Called by a notifier that uses devfreq->nb.
  240. */
  241. static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
  242. void *devp)
  243. {
  244. struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
  245. int ret;
  246. mutex_lock(&devfreq->lock);
  247. ret = update_devfreq(devfreq);
  248. mutex_unlock(&devfreq->lock);
  249. return ret;
  250. }
  251. /**
  252. * _remove_devfreq() - Remove devfreq from the list and release its resources.
  253. * @devfreq: the devfreq struct
  254. * @skip: skip calling device_unregister().
  255. */
  256. static void _remove_devfreq(struct devfreq *devfreq, bool skip)
  257. {
  258. mutex_lock(&devfreq_list_lock);
  259. if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
  260. mutex_unlock(&devfreq_list_lock);
  261. dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
  262. return;
  263. }
  264. list_del(&devfreq->node);
  265. mutex_unlock(&devfreq_list_lock);
  266. devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_STOP, NULL);
  267. if (devfreq->profile->exit)
  268. devfreq->profile->exit(devfreq->dev.parent);
  269. if (!skip && get_device(&devfreq->dev)) {
  270. device_unregister(&devfreq->dev);
  271. put_device(&devfreq->dev);
  272. }
  273. mutex_destroy(&devfreq->lock);
  274. kfree(devfreq);
  275. }
  276. /**
  277. * devfreq_dev_release() - Callback for struct device to release the device.
  278. * @dev: the devfreq device
  279. *
  280. * This calls _remove_devfreq() if _remove_devfreq() is not called.
  281. * Note that devfreq_dev_release() could be called by _remove_devfreq() as
  282. * well as by others unregistering the device.
  283. */
  284. static void devfreq_dev_release(struct device *dev)
  285. {
  286. struct devfreq *devfreq = to_devfreq(dev);
  287. _remove_devfreq(devfreq, true);
  288. }
  289. /**
  290. * devfreq_add_device() - Add devfreq feature to the device
  291. * @dev: the device to add devfreq feature.
  292. * @profile: device-specific profile to run devfreq.
  293. * @governor: the policy to choose frequency.
  294. * @data: private data for the governor. The devfreq framework does not
  295. * touch this value.
  296. */
  297. struct devfreq *devfreq_add_device(struct device *dev,
  298. struct devfreq_dev_profile *profile,
  299. const struct devfreq_governor *governor,
  300. void *data)
  301. {
  302. struct devfreq *devfreq;
  303. int err = 0;
  304. if (!dev || !profile || !governor) {
  305. dev_err(dev, "%s: Invalid parameters.\n", __func__);
  306. return ERR_PTR(-EINVAL);
  307. }
  308. mutex_lock(&devfreq_list_lock);
  309. devfreq = find_device_devfreq(dev);
  310. mutex_unlock(&devfreq_list_lock);
  311. if (!IS_ERR(devfreq)) {
  312. dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
  313. err = -EINVAL;
  314. goto err_out;
  315. }
  316. devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
  317. if (!devfreq) {
  318. dev_err(dev, "%s: Unable to create devfreq for the device\n",
  319. __func__);
  320. err = -ENOMEM;
  321. goto err_out;
  322. }
  323. mutex_init(&devfreq->lock);
  324. mutex_lock(&devfreq->lock);
  325. devfreq->dev.parent = dev;
  326. devfreq->dev.class = devfreq_class;
  327. devfreq->dev.release = devfreq_dev_release;
  328. devfreq->profile = profile;
  329. devfreq->governor = governor;
  330. devfreq->previous_freq = profile->initial_freq;
  331. devfreq->data = data;
  332. devfreq->nb.notifier_call = devfreq_notifier_call;
  333. dev_set_name(&devfreq->dev, dev_name(dev));
  334. err = device_register(&devfreq->dev);
  335. if (err) {
  336. put_device(&devfreq->dev);
  337. mutex_unlock(&devfreq->lock);
  338. goto err_dev;
  339. }
  340. mutex_unlock(&devfreq->lock);
  341. mutex_lock(&devfreq_list_lock);
  342. list_add(&devfreq->node, &devfreq_list);
  343. mutex_unlock(&devfreq_list_lock);
  344. err = devfreq->governor->event_handler(devfreq,
  345. DEVFREQ_GOV_START, NULL);
  346. if (err) {
  347. dev_err(dev, "%s: Unable to start governor for the device\n",
  348. __func__);
  349. goto err_init;
  350. }
  351. return devfreq;
  352. err_init:
  353. list_del(&devfreq->node);
  354. device_unregister(&devfreq->dev);
  355. err_dev:
  356. kfree(devfreq);
  357. err_out:
  358. return ERR_PTR(err);
  359. }
  360. EXPORT_SYMBOL(devfreq_add_device);
  361. /**
  362. * devfreq_remove_device() - Remove devfreq feature from a device.
  363. * @devfreq the devfreq instance to be removed
  364. */
  365. int devfreq_remove_device(struct devfreq *devfreq)
  366. {
  367. if (!devfreq)
  368. return -EINVAL;
  369. _remove_devfreq(devfreq, false);
  370. return 0;
  371. }
  372. EXPORT_SYMBOL(devfreq_remove_device);
  373. static ssize_t show_governor(struct device *dev,
  374. struct device_attribute *attr, char *buf)
  375. {
  376. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  377. }
  378. static ssize_t show_freq(struct device *dev,
  379. struct device_attribute *attr, char *buf)
  380. {
  381. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  382. }
  383. static ssize_t show_polling_interval(struct device *dev,
  384. struct device_attribute *attr, char *buf)
  385. {
  386. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  387. }
  388. static ssize_t store_polling_interval(struct device *dev,
  389. struct device_attribute *attr,
  390. const char *buf, size_t count)
  391. {
  392. struct devfreq *df = to_devfreq(dev);
  393. unsigned int value;
  394. int ret;
  395. ret = sscanf(buf, "%u", &value);
  396. if (ret != 1)
  397. goto out;
  398. df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
  399. ret = count;
  400. out:
  401. return ret;
  402. }
  403. static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr,
  404. const char *buf, size_t count)
  405. {
  406. struct devfreq *df = to_devfreq(dev);
  407. unsigned long value;
  408. int ret;
  409. unsigned long max;
  410. ret = sscanf(buf, "%lu", &value);
  411. if (ret != 1)
  412. goto out;
  413. mutex_lock(&df->lock);
  414. max = df->max_freq;
  415. if (value && max && value > max) {
  416. ret = -EINVAL;
  417. goto unlock;
  418. }
  419. df->min_freq = value;
  420. update_devfreq(df);
  421. ret = count;
  422. unlock:
  423. mutex_unlock(&df->lock);
  424. out:
  425. return ret;
  426. }
  427. static ssize_t show_min_freq(struct device *dev, struct device_attribute *attr,
  428. char *buf)
  429. {
  430. return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq);
  431. }
  432. static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr,
  433. const char *buf, size_t count)
  434. {
  435. struct devfreq *df = to_devfreq(dev);
  436. unsigned long value;
  437. int ret;
  438. unsigned long min;
  439. ret = sscanf(buf, "%lu", &value);
  440. if (ret != 1)
  441. goto out;
  442. mutex_lock(&df->lock);
  443. min = df->min_freq;
  444. if (value && min && value < min) {
  445. ret = -EINVAL;
  446. goto unlock;
  447. }
  448. df->max_freq = value;
  449. update_devfreq(df);
  450. ret = count;
  451. unlock:
  452. mutex_unlock(&df->lock);
  453. out:
  454. return ret;
  455. }
  456. static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr,
  457. char *buf)
  458. {
  459. return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq);
  460. }
  461. static struct device_attribute devfreq_attrs[] = {
  462. __ATTR(governor, S_IRUGO, show_governor, NULL),
  463. __ATTR(cur_freq, S_IRUGO, show_freq, NULL),
  464. __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval,
  465. store_polling_interval),
  466. __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq),
  467. __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq),
  468. { },
  469. };
  470. static int __init devfreq_init(void)
  471. {
  472. devfreq_class = class_create(THIS_MODULE, "devfreq");
  473. if (IS_ERR(devfreq_class)) {
  474. pr_err("%s: couldn't create class\n", __FILE__);
  475. return PTR_ERR(devfreq_class);
  476. }
  477. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  478. if (IS_ERR(devfreq_wq)) {
  479. class_destroy(devfreq_class);
  480. pr_err("%s: couldn't create workqueue\n", __FILE__);
  481. return PTR_ERR(devfreq_wq);
  482. }
  483. devfreq_class->dev_attrs = devfreq_attrs;
  484. return 0;
  485. }
  486. subsys_initcall(devfreq_init);
  487. static void __exit devfreq_exit(void)
  488. {
  489. class_destroy(devfreq_class);
  490. destroy_workqueue(devfreq_wq);
  491. }
  492. module_exit(devfreq_exit);
  493. /*
  494. * The followings are helper functions for devfreq user device drivers with
  495. * OPP framework.
  496. */
  497. /**
  498. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  499. * freq value given to target callback.
  500. * @dev The devfreq user device. (parent of devfreq)
  501. * @freq The frequency given to target function
  502. * @flags Flags handed from devfreq framework.
  503. *
  504. */
  505. struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq,
  506. u32 flags)
  507. {
  508. struct opp *opp;
  509. if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
  510. /* The freq is an upper bound. opp should be lower */
  511. opp = opp_find_freq_floor(dev, freq);
  512. /* If not available, use the closest opp */
  513. if (opp == ERR_PTR(-ENODEV))
  514. opp = opp_find_freq_ceil(dev, freq);
  515. } else {
  516. /* The freq is an lower bound. opp should be higher */
  517. opp = opp_find_freq_ceil(dev, freq);
  518. /* If not available, use the closest opp */
  519. if (opp == ERR_PTR(-ENODEV))
  520. opp = opp_find_freq_floor(dev, freq);
  521. }
  522. return opp;
  523. }
  524. /**
  525. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  526. * for any changes in the OPP availability
  527. * changes
  528. * @dev The devfreq user device. (parent of devfreq)
  529. * @devfreq The devfreq object.
  530. */
  531. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  532. {
  533. struct srcu_notifier_head *nh = opp_get_notifier(dev);
  534. if (IS_ERR(nh))
  535. return PTR_ERR(nh);
  536. return srcu_notifier_chain_register(nh, &devfreq->nb);
  537. }
  538. /**
  539. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  540. * notified for any changes in the OPP
  541. * availability changes anymore.
  542. * @dev The devfreq user device. (parent of devfreq)
  543. * @devfreq The devfreq object.
  544. *
  545. * At exit() callback of devfreq_dev_profile, this must be included if
  546. * devfreq_recommended_opp is used.
  547. */
  548. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  549. {
  550. struct srcu_notifier_head *nh = opp_get_notifier(dev);
  551. if (IS_ERR(nh))
  552. return PTR_ERR(nh);
  553. return srcu_notifier_chain_unregister(nh, &devfreq->nb);
  554. }
  555. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  556. MODULE_DESCRIPTION("devfreq class support");
  557. MODULE_LICENSE("GPL");