devfreq.c 16 KB

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