devfreq.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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_work periodically monitors every registered device.
  31. * The minimum polling interval is one jiffy. The polling interval is
  32. * determined by the minimum polling period among all polling devfreq
  33. * devices. The resolution of polling interval is one jiffy.
  34. */
  35. static bool polling;
  36. static struct workqueue_struct *devfreq_wq;
  37. static struct delayed_work devfreq_work;
  38. /* wait removing if this is to be removed */
  39. static struct devfreq *wait_remove_device;
  40. /* The list of all device-devfreq */
  41. static LIST_HEAD(devfreq_list);
  42. static DEFINE_MUTEX(devfreq_list_lock);
  43. /**
  44. * find_device_devfreq() - find devfreq struct using device pointer
  45. * @dev: device pointer used to lookup device devfreq.
  46. *
  47. * Search the list of device devfreqs and return the matched device's
  48. * devfreq info. devfreq_list_lock should be held by the caller.
  49. */
  50. static struct devfreq *find_device_devfreq(struct device *dev)
  51. {
  52. struct devfreq *tmp_devfreq;
  53. if (unlikely(IS_ERR_OR_NULL(dev))) {
  54. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  55. return ERR_PTR(-EINVAL);
  56. }
  57. WARN(!mutex_is_locked(&devfreq_list_lock),
  58. "devfreq_list_lock must be locked.");
  59. list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
  60. if (tmp_devfreq->dev.parent == dev)
  61. return tmp_devfreq;
  62. }
  63. return ERR_PTR(-ENODEV);
  64. }
  65. /**
  66. * update_devfreq() - Reevaluate the device and configure frequency.
  67. * @devfreq: the devfreq instance.
  68. *
  69. * Note: Lock devfreq->lock before calling update_devfreq
  70. * This function is exported for governors.
  71. */
  72. int update_devfreq(struct devfreq *devfreq)
  73. {
  74. unsigned long freq;
  75. int err = 0;
  76. if (!mutex_is_locked(&devfreq->lock)) {
  77. WARN(true, "devfreq->lock must be locked by the caller.\n");
  78. return -EINVAL;
  79. }
  80. /* Reevaluate the proper frequency */
  81. err = devfreq->governor->get_target_freq(devfreq, &freq);
  82. if (err)
  83. return err;
  84. err = devfreq->profile->target(devfreq->dev.parent, &freq);
  85. if (err)
  86. return err;
  87. devfreq->previous_freq = freq;
  88. return err;
  89. }
  90. /**
  91. * devfreq_notifier_call() - Notify that the device frequency requirements
  92. * has been changed out of devfreq framework.
  93. * @nb the notifier_block (supposed to be devfreq->nb)
  94. * @type not used
  95. * @devp not used
  96. *
  97. * Called by a notifier that uses devfreq->nb.
  98. */
  99. static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
  100. void *devp)
  101. {
  102. struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
  103. int ret;
  104. mutex_lock(&devfreq->lock);
  105. ret = update_devfreq(devfreq);
  106. mutex_unlock(&devfreq->lock);
  107. return ret;
  108. }
  109. /**
  110. * _remove_devfreq() - Remove devfreq from the device.
  111. * @devfreq: the devfreq struct
  112. * @skip: skip calling device_unregister().
  113. *
  114. * Note that the caller should lock devfreq->lock before calling
  115. * this. _remove_devfreq() will unlock it and free devfreq
  116. * internally. devfreq_list_lock should be locked by the caller
  117. * as well (not relased at return)
  118. *
  119. * Lock usage:
  120. * devfreq->lock: locked before call.
  121. * unlocked at return (and freed)
  122. * devfreq_list_lock: locked before call.
  123. * kept locked at return.
  124. * if devfreq is centrally polled.
  125. *
  126. * Freed memory:
  127. * devfreq
  128. */
  129. static void _remove_devfreq(struct devfreq *devfreq, bool skip)
  130. {
  131. if (!mutex_is_locked(&devfreq->lock)) {
  132. WARN(true, "devfreq->lock must be locked by the caller.\n");
  133. return;
  134. }
  135. if (!devfreq->governor->no_central_polling &&
  136. !mutex_is_locked(&devfreq_list_lock)) {
  137. WARN(true, "devfreq_list_lock must be locked by the caller.\n");
  138. return;
  139. }
  140. if (devfreq->being_removed)
  141. return;
  142. devfreq->being_removed = true;
  143. if (devfreq->profile->exit)
  144. devfreq->profile->exit(devfreq->dev.parent);
  145. if (devfreq->governor->exit)
  146. devfreq->governor->exit(devfreq);
  147. if (!skip && get_device(&devfreq->dev)) {
  148. device_unregister(&devfreq->dev);
  149. put_device(&devfreq->dev);
  150. }
  151. if (!devfreq->governor->no_central_polling)
  152. list_del(&devfreq->node);
  153. mutex_unlock(&devfreq->lock);
  154. mutex_destroy(&devfreq->lock);
  155. kfree(devfreq);
  156. }
  157. /**
  158. * devfreq_dev_release() - Callback for struct device to release the device.
  159. * @dev: the devfreq device
  160. *
  161. * This calls _remove_devfreq() if _remove_devfreq() is not called.
  162. * Note that devfreq_dev_release() could be called by _remove_devfreq() as
  163. * well as by others unregistering the device.
  164. */
  165. static void devfreq_dev_release(struct device *dev)
  166. {
  167. struct devfreq *devfreq = to_devfreq(dev);
  168. bool central_polling = !devfreq->governor->no_central_polling;
  169. /*
  170. * If devfreq_dev_release() was called by device_unregister() of
  171. * _remove_devfreq(), we cannot mutex_lock(&devfreq->lock) and
  172. * being_removed is already set. This also partially checks the case
  173. * where devfreq_dev_release() is called from a thread other than
  174. * the one called _remove_devfreq(); however, this case is
  175. * dealt completely with another following being_removed check.
  176. *
  177. * Because being_removed is never being
  178. * unset, we do not need to worry about race conditions on
  179. * being_removed.
  180. */
  181. if (devfreq->being_removed)
  182. return;
  183. if (central_polling)
  184. mutex_lock(&devfreq_list_lock);
  185. mutex_lock(&devfreq->lock);
  186. /*
  187. * Check being_removed flag again for the case where
  188. * devfreq_dev_release() was called in a thread other than the one
  189. * possibly called _remove_devfreq().
  190. */
  191. if (devfreq->being_removed) {
  192. mutex_unlock(&devfreq->lock);
  193. goto out;
  194. }
  195. /* devfreq->lock is unlocked and removed in _removed_devfreq() */
  196. _remove_devfreq(devfreq, true);
  197. out:
  198. if (central_polling)
  199. mutex_unlock(&devfreq_list_lock);
  200. }
  201. /**
  202. * devfreq_monitor() - Periodically poll devfreq objects.
  203. * @work: the work struct used to run devfreq_monitor periodically.
  204. *
  205. */
  206. static void devfreq_monitor(struct work_struct *work)
  207. {
  208. static unsigned long last_polled_at;
  209. struct devfreq *devfreq, *tmp;
  210. int error;
  211. unsigned long jiffies_passed;
  212. unsigned long next_jiffies = ULONG_MAX, now = jiffies;
  213. struct device *dev;
  214. /* Initially last_polled_at = 0, polling every device at bootup */
  215. jiffies_passed = now - last_polled_at;
  216. last_polled_at = now;
  217. if (jiffies_passed == 0)
  218. jiffies_passed = 1;
  219. mutex_lock(&devfreq_list_lock);
  220. list_for_each_entry_safe(devfreq, tmp, &devfreq_list, node) {
  221. mutex_lock(&devfreq->lock);
  222. dev = devfreq->dev.parent;
  223. /* Do not remove tmp for a while */
  224. wait_remove_device = tmp;
  225. if (devfreq->governor->no_central_polling ||
  226. devfreq->next_polling == 0) {
  227. mutex_unlock(&devfreq->lock);
  228. continue;
  229. }
  230. mutex_unlock(&devfreq_list_lock);
  231. /*
  232. * Reduce more next_polling if devfreq_wq took an extra
  233. * delay. (i.e., CPU has been idled.)
  234. */
  235. if (devfreq->next_polling <= jiffies_passed) {
  236. error = update_devfreq(devfreq);
  237. /* Remove a devfreq with an error. */
  238. if (error && error != -EAGAIN) {
  239. dev_err(dev, "Due to update_devfreq error(%d), devfreq(%s) is removed from the device\n",
  240. error, devfreq->governor->name);
  241. /*
  242. * Unlock devfreq before locking the list
  243. * in order to avoid deadlock with
  244. * find_device_devfreq or others
  245. */
  246. mutex_unlock(&devfreq->lock);
  247. mutex_lock(&devfreq_list_lock);
  248. /* Check if devfreq is already removed */
  249. if (IS_ERR(find_device_devfreq(dev)))
  250. continue;
  251. mutex_lock(&devfreq->lock);
  252. /* This unlocks devfreq->lock and free it */
  253. _remove_devfreq(devfreq, false);
  254. continue;
  255. }
  256. devfreq->next_polling = devfreq->polling_jiffies;
  257. } else {
  258. devfreq->next_polling -= jiffies_passed;
  259. }
  260. if (devfreq->next_polling)
  261. next_jiffies = (next_jiffies > devfreq->next_polling) ?
  262. devfreq->next_polling : next_jiffies;
  263. mutex_unlock(&devfreq->lock);
  264. mutex_lock(&devfreq_list_lock);
  265. }
  266. wait_remove_device = NULL;
  267. mutex_unlock(&devfreq_list_lock);
  268. if (next_jiffies > 0 && next_jiffies < ULONG_MAX) {
  269. polling = true;
  270. queue_delayed_work(devfreq_wq, &devfreq_work, next_jiffies);
  271. } else {
  272. polling = false;
  273. }
  274. }
  275. /**
  276. * devfreq_add_device() - Add devfreq feature to the device
  277. * @dev: the device to add devfreq feature.
  278. * @profile: device-specific profile to run devfreq.
  279. * @governor: the policy to choose frequency.
  280. * @data: private data for the governor. The devfreq framework does not
  281. * touch this value.
  282. */
  283. struct devfreq *devfreq_add_device(struct device *dev,
  284. struct devfreq_dev_profile *profile,
  285. const struct devfreq_governor *governor,
  286. void *data)
  287. {
  288. struct devfreq *devfreq;
  289. int err = 0;
  290. if (!dev || !profile || !governor) {
  291. dev_err(dev, "%s: Invalid parameters.\n", __func__);
  292. return ERR_PTR(-EINVAL);
  293. }
  294. if (!governor->no_central_polling) {
  295. mutex_lock(&devfreq_list_lock);
  296. devfreq = find_device_devfreq(dev);
  297. mutex_unlock(&devfreq_list_lock);
  298. if (!IS_ERR(devfreq)) {
  299. dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
  300. err = -EINVAL;
  301. goto err_out;
  302. }
  303. }
  304. devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
  305. if (!devfreq) {
  306. dev_err(dev, "%s: Unable to create devfreq for the device\n",
  307. __func__);
  308. err = -ENOMEM;
  309. goto err_out;
  310. }
  311. mutex_init(&devfreq->lock);
  312. mutex_lock(&devfreq->lock);
  313. devfreq->dev.parent = dev;
  314. devfreq->dev.class = devfreq_class;
  315. devfreq->dev.release = devfreq_dev_release;
  316. devfreq->profile = profile;
  317. devfreq->governor = governor;
  318. devfreq->previous_freq = profile->initial_freq;
  319. devfreq->data = data;
  320. devfreq->next_polling = devfreq->polling_jiffies
  321. = msecs_to_jiffies(devfreq->profile->polling_ms);
  322. devfreq->nb.notifier_call = devfreq_notifier_call;
  323. dev_set_name(&devfreq->dev, dev_name(dev));
  324. err = device_register(&devfreq->dev);
  325. if (err) {
  326. put_device(&devfreq->dev);
  327. goto err_dev;
  328. }
  329. if (governor->init)
  330. err = governor->init(devfreq);
  331. if (err)
  332. goto err_init;
  333. mutex_unlock(&devfreq->lock);
  334. if (governor->no_central_polling)
  335. goto out;
  336. mutex_lock(&devfreq_list_lock);
  337. list_add(&devfreq->node, &devfreq_list);
  338. if (devfreq_wq && devfreq->next_polling && !polling) {
  339. polling = true;
  340. queue_delayed_work(devfreq_wq, &devfreq_work,
  341. devfreq->next_polling);
  342. }
  343. mutex_unlock(&devfreq_list_lock);
  344. out:
  345. return devfreq;
  346. err_init:
  347. device_unregister(&devfreq->dev);
  348. err_dev:
  349. mutex_unlock(&devfreq->lock);
  350. kfree(devfreq);
  351. err_out:
  352. return ERR_PTR(err);
  353. }
  354. /**
  355. * devfreq_remove_device() - Remove devfreq feature from a device.
  356. * @devfreq the devfreq instance to be removed
  357. */
  358. int devfreq_remove_device(struct devfreq *devfreq)
  359. {
  360. bool central_polling;
  361. if (!devfreq)
  362. return -EINVAL;
  363. central_polling = !devfreq->governor->no_central_polling;
  364. if (central_polling) {
  365. mutex_lock(&devfreq_list_lock);
  366. while (wait_remove_device == devfreq) {
  367. mutex_unlock(&devfreq_list_lock);
  368. schedule();
  369. mutex_lock(&devfreq_list_lock);
  370. }
  371. }
  372. mutex_lock(&devfreq->lock);
  373. _remove_devfreq(devfreq, false); /* it unlocks devfreq->lock */
  374. if (central_polling)
  375. mutex_unlock(&devfreq_list_lock);
  376. return 0;
  377. }
  378. static ssize_t show_governor(struct device *dev,
  379. struct device_attribute *attr, char *buf)
  380. {
  381. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  382. }
  383. static ssize_t show_freq(struct device *dev,
  384. struct device_attribute *attr, char *buf)
  385. {
  386. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  387. }
  388. static ssize_t show_polling_interval(struct device *dev,
  389. struct device_attribute *attr, char *buf)
  390. {
  391. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  392. }
  393. static ssize_t store_polling_interval(struct device *dev,
  394. struct device_attribute *attr,
  395. const char *buf, size_t count)
  396. {
  397. struct devfreq *df = to_devfreq(dev);
  398. unsigned int value;
  399. int ret;
  400. ret = sscanf(buf, "%u", &value);
  401. if (ret != 1)
  402. goto out;
  403. mutex_lock(&df->lock);
  404. df->profile->polling_ms = value;
  405. df->next_polling = df->polling_jiffies
  406. = msecs_to_jiffies(value);
  407. mutex_unlock(&df->lock);
  408. ret = count;
  409. if (df->governor->no_central_polling)
  410. goto out;
  411. mutex_lock(&devfreq_list_lock);
  412. if (df->next_polling > 0 && !polling) {
  413. polling = true;
  414. queue_delayed_work(devfreq_wq, &devfreq_work,
  415. df->next_polling);
  416. }
  417. mutex_unlock(&devfreq_list_lock);
  418. out:
  419. return ret;
  420. }
  421. static ssize_t show_central_polling(struct device *dev,
  422. struct device_attribute *attr, char *buf)
  423. {
  424. return sprintf(buf, "%d\n",
  425. !to_devfreq(dev)->governor->no_central_polling);
  426. }
  427. static struct device_attribute devfreq_attrs[] = {
  428. __ATTR(governor, S_IRUGO, show_governor, NULL),
  429. __ATTR(cur_freq, S_IRUGO, show_freq, NULL),
  430. __ATTR(central_polling, S_IRUGO, show_central_polling, NULL),
  431. __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval,
  432. store_polling_interval),
  433. { },
  434. };
  435. /**
  436. * devfreq_start_polling() - Initialize data structure for devfreq framework and
  437. * start polling registered devfreq devices.
  438. */
  439. static int __init devfreq_start_polling(void)
  440. {
  441. mutex_lock(&devfreq_list_lock);
  442. polling = false;
  443. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  444. INIT_DELAYED_WORK_DEFERRABLE(&devfreq_work, devfreq_monitor);
  445. mutex_unlock(&devfreq_list_lock);
  446. devfreq_monitor(&devfreq_work.work);
  447. return 0;
  448. }
  449. late_initcall(devfreq_start_polling);
  450. static int __init devfreq_init(void)
  451. {
  452. devfreq_class = class_create(THIS_MODULE, "devfreq");
  453. if (IS_ERR(devfreq_class)) {
  454. pr_err("%s: couldn't create class\n", __FILE__);
  455. return PTR_ERR(devfreq_class);
  456. }
  457. devfreq_class->dev_attrs = devfreq_attrs;
  458. return 0;
  459. }
  460. subsys_initcall(devfreq_init);
  461. static void __exit devfreq_exit(void)
  462. {
  463. class_destroy(devfreq_class);
  464. }
  465. module_exit(devfreq_exit);
  466. /*
  467. * The followings are helper functions for devfreq user device drivers with
  468. * OPP framework.
  469. */
  470. /**
  471. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  472. * freq value given to target callback.
  473. * @dev The devfreq user device. (parent of devfreq)
  474. * @freq The frequency given to target function
  475. *
  476. */
  477. struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq)
  478. {
  479. struct opp *opp = opp_find_freq_ceil(dev, freq);
  480. if (opp == ERR_PTR(-ENODEV))
  481. opp = opp_find_freq_floor(dev, freq);
  482. return opp;
  483. }
  484. /**
  485. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  486. * for any changes in the OPP availability
  487. * changes
  488. * @dev The devfreq user device. (parent of devfreq)
  489. * @devfreq The devfreq object.
  490. */
  491. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  492. {
  493. struct srcu_notifier_head *nh = opp_get_notifier(dev);
  494. if (IS_ERR(nh))
  495. return PTR_ERR(nh);
  496. return srcu_notifier_chain_register(nh, &devfreq->nb);
  497. }
  498. /**
  499. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  500. * notified for any changes in the OPP
  501. * availability changes anymore.
  502. * @dev The devfreq user device. (parent of devfreq)
  503. * @devfreq The devfreq object.
  504. *
  505. * At exit() callback of devfreq_dev_profile, this must be included if
  506. * devfreq_recommended_opp is used.
  507. */
  508. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  509. {
  510. struct srcu_notifier_head *nh = opp_get_notifier(dev);
  511. if (IS_ERR(nh))
  512. return PTR_ERR(nh);
  513. return srcu_notifier_chain_unregister(nh, &devfreq->nb);
  514. }
  515. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  516. MODULE_DESCRIPTION("devfreq class support");
  517. MODULE_LICENSE("GPL");