governor_passive.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/devfreq/governor_passive.c
  4. *
  5. * Copyright (C) 2016 Samsung Electronics
  6. * Author: Chanwoo Choi <cw00.choi@samsung.com>
  7. * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/cpu.h>
  11. #include <linux/cpufreq.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/slab.h>
  14. #include <linux/device.h>
  15. #include <linux/devfreq.h>
  16. #include <linux/devfreq-governor.h>
  17. #include <linux/units.h>
  18. /**
  19. * struct devfreq_cpu_data - Hold the per-cpu data
  20. * @node: list node
  21. * @dev: reference to cpu device.
  22. * @first_cpu: the cpumask of the first cpu of a policy.
  23. * @opp_table: reference to cpu opp table.
  24. * @cur_freq: the current frequency of the cpu.
  25. * @min_freq: the min frequency of the cpu.
  26. * @max_freq: the max frequency of the cpu.
  27. *
  28. * This structure stores the required cpu_data of a cpu.
  29. * This is auto-populated by the governor.
  30. */
  31. struct devfreq_cpu_data {
  32. struct list_head node;
  33. struct device *dev;
  34. unsigned int first_cpu;
  35. struct opp_table *opp_table;
  36. unsigned int cur_freq;
  37. unsigned int min_freq;
  38. unsigned int max_freq;
  39. };
  40. static struct devfreq_cpu_data *
  41. get_parent_cpu_data(struct devfreq_passive_data *p_data,
  42. struct cpufreq_policy *policy)
  43. {
  44. struct devfreq_cpu_data *parent_cpu_data;
  45. if (!p_data || !policy)
  46. return NULL;
  47. list_for_each_entry(parent_cpu_data, &p_data->cpu_data_list, node)
  48. if (parent_cpu_data->first_cpu == cpumask_first(policy->related_cpus))
  49. return parent_cpu_data;
  50. return NULL;
  51. }
  52. static void delete_parent_cpu_data(struct devfreq_passive_data *p_data)
  53. {
  54. struct devfreq_cpu_data *parent_cpu_data, *tmp;
  55. list_for_each_entry_safe(parent_cpu_data, tmp, &p_data->cpu_data_list, node) {
  56. list_del(&parent_cpu_data->node);
  57. if (parent_cpu_data->opp_table)
  58. dev_pm_opp_put_opp_table(parent_cpu_data->opp_table);
  59. kfree(parent_cpu_data);
  60. }
  61. }
  62. static unsigned long get_target_freq_by_required_opp(struct device *p_dev,
  63. struct opp_table *p_opp_table,
  64. struct opp_table *opp_table,
  65. unsigned long *freq)
  66. {
  67. struct dev_pm_opp *opp = NULL, *p_opp = NULL;
  68. unsigned long target_freq;
  69. if (!p_dev || !p_opp_table || !opp_table || !freq)
  70. return 0;
  71. p_opp = devfreq_recommended_opp(p_dev, freq, 0);
  72. if (IS_ERR(p_opp))
  73. return 0;
  74. opp = dev_pm_opp_xlate_required_opp(p_opp_table, opp_table, p_opp);
  75. dev_pm_opp_put(p_opp);
  76. if (IS_ERR(opp))
  77. return 0;
  78. target_freq = dev_pm_opp_get_freq(opp);
  79. dev_pm_opp_put(opp);
  80. return target_freq;
  81. }
  82. static int get_target_freq_with_cpufreq(struct devfreq *devfreq,
  83. unsigned long *target_freq)
  84. {
  85. struct devfreq_passive_data *p_data =
  86. (struct devfreq_passive_data *)devfreq->data;
  87. struct devfreq_cpu_data *parent_cpu_data;
  88. struct cpufreq_policy *policy;
  89. unsigned long cpu, cpu_cur, cpu_min, cpu_max, cpu_percent;
  90. unsigned long dev_min, dev_max;
  91. unsigned long freq = 0;
  92. int ret = 0;
  93. for_each_online_cpu(cpu) {
  94. policy = cpufreq_cpu_get(cpu);
  95. if (!policy) {
  96. ret = -EINVAL;
  97. continue;
  98. }
  99. parent_cpu_data = get_parent_cpu_data(p_data, policy);
  100. if (!parent_cpu_data) {
  101. cpufreq_cpu_put(policy);
  102. continue;
  103. }
  104. /* Get target freq via required opps */
  105. cpu_cur = parent_cpu_data->cur_freq * HZ_PER_KHZ;
  106. freq = get_target_freq_by_required_opp(parent_cpu_data->dev,
  107. parent_cpu_data->opp_table,
  108. devfreq->opp_table, &cpu_cur);
  109. if (freq) {
  110. *target_freq = max(freq, *target_freq);
  111. cpufreq_cpu_put(policy);
  112. continue;
  113. }
  114. /* Use interpolation if required opps is not available */
  115. devfreq_get_freq_range(devfreq, &dev_min, &dev_max);
  116. cpu_min = parent_cpu_data->min_freq;
  117. cpu_max = parent_cpu_data->max_freq;
  118. cpu_cur = parent_cpu_data->cur_freq;
  119. cpu_percent = ((cpu_cur - cpu_min) * 100) / (cpu_max - cpu_min);
  120. freq = dev_min + mult_frac(dev_max - dev_min, cpu_percent, 100);
  121. *target_freq = max(freq, *target_freq);
  122. cpufreq_cpu_put(policy);
  123. }
  124. return ret;
  125. }
  126. static int get_target_freq_with_devfreq(struct devfreq *devfreq,
  127. unsigned long *freq)
  128. {
  129. struct devfreq_passive_data *p_data
  130. = (struct devfreq_passive_data *)devfreq->data;
  131. struct devfreq *parent_devfreq = (struct devfreq *)p_data->parent;
  132. unsigned long child_freq = ULONG_MAX;
  133. int i, count;
  134. /* Get target freq via required opps */
  135. child_freq = get_target_freq_by_required_opp(parent_devfreq->dev.parent,
  136. parent_devfreq->opp_table,
  137. devfreq->opp_table, freq);
  138. if (child_freq)
  139. goto out;
  140. /* Use interpolation if required opps is not available */
  141. for (i = 0; i < parent_devfreq->max_state; i++)
  142. if (parent_devfreq->freq_table[i] == *freq)
  143. break;
  144. if (i == parent_devfreq->max_state)
  145. return -EINVAL;
  146. if (i < devfreq->max_state) {
  147. child_freq = devfreq->freq_table[i];
  148. } else {
  149. count = devfreq->max_state;
  150. child_freq = devfreq->freq_table[count - 1];
  151. }
  152. out:
  153. *freq = child_freq;
  154. return 0;
  155. }
  156. static int devfreq_passive_get_target_freq(struct devfreq *devfreq,
  157. unsigned long *freq)
  158. {
  159. struct devfreq_passive_data *p_data =
  160. (struct devfreq_passive_data *)devfreq->data;
  161. int ret;
  162. if (!p_data)
  163. return -EINVAL;
  164. /*
  165. * If the devfreq device with passive governor has the specific method
  166. * to determine the next frequency, should use the get_target_freq()
  167. * of struct devfreq_passive_data.
  168. */
  169. if (p_data->get_target_freq)
  170. return p_data->get_target_freq(devfreq, freq);
  171. switch (p_data->parent_type) {
  172. case DEVFREQ_PARENT_DEV:
  173. ret = get_target_freq_with_devfreq(devfreq, freq);
  174. break;
  175. case CPUFREQ_PARENT_DEV:
  176. ret = get_target_freq_with_cpufreq(devfreq, freq);
  177. break;
  178. default:
  179. ret = -EINVAL;
  180. dev_err(&devfreq->dev, "Invalid parent type\n");
  181. break;
  182. }
  183. return ret;
  184. }
  185. static int cpufreq_passive_notifier_call(struct notifier_block *nb,
  186. unsigned long event, void *ptr)
  187. {
  188. struct devfreq_passive_data *p_data =
  189. container_of(nb, struct devfreq_passive_data, nb);
  190. struct devfreq *devfreq = (struct devfreq *)p_data->this;
  191. struct devfreq_cpu_data *parent_cpu_data;
  192. struct cpufreq_freqs *freqs = ptr;
  193. unsigned int cur_freq;
  194. int ret;
  195. if (event != CPUFREQ_POSTCHANGE || !freqs)
  196. return 0;
  197. parent_cpu_data = get_parent_cpu_data(p_data, freqs->policy);
  198. if (!parent_cpu_data || parent_cpu_data->cur_freq == freqs->new)
  199. return 0;
  200. cur_freq = parent_cpu_data->cur_freq;
  201. parent_cpu_data->cur_freq = freqs->new;
  202. mutex_lock(&devfreq->lock);
  203. ret = devfreq_update_target(devfreq, freqs->new);
  204. mutex_unlock(&devfreq->lock);
  205. if (ret) {
  206. parent_cpu_data->cur_freq = cur_freq;
  207. dev_err(&devfreq->dev, "failed to update the frequency.\n");
  208. return ret;
  209. }
  210. return 0;
  211. }
  212. static int cpufreq_passive_unregister_notifier(struct devfreq *devfreq)
  213. {
  214. struct devfreq_passive_data *p_data
  215. = (struct devfreq_passive_data *)devfreq->data;
  216. int ret;
  217. if (p_data->nb.notifier_call) {
  218. ret = cpufreq_unregister_notifier(&p_data->nb,
  219. CPUFREQ_TRANSITION_NOTIFIER);
  220. if (ret < 0)
  221. return ret;
  222. }
  223. delete_parent_cpu_data(p_data);
  224. return 0;
  225. }
  226. static int cpufreq_passive_register_notifier(struct devfreq *devfreq)
  227. {
  228. struct devfreq_passive_data *p_data
  229. = (struct devfreq_passive_data *)devfreq->data;
  230. struct device *dev = devfreq->dev.parent;
  231. struct opp_table *opp_table = NULL;
  232. struct devfreq_cpu_data *parent_cpu_data;
  233. struct cpufreq_policy *policy;
  234. struct device *cpu_dev;
  235. unsigned int cpu;
  236. int ret;
  237. p_data->cpu_data_list
  238. = (struct list_head)LIST_HEAD_INIT(p_data->cpu_data_list);
  239. p_data->nb.notifier_call = cpufreq_passive_notifier_call;
  240. ret = cpufreq_register_notifier(&p_data->nb, CPUFREQ_TRANSITION_NOTIFIER);
  241. if (ret) {
  242. dev_err(dev, "failed to register cpufreq notifier\n");
  243. p_data->nb.notifier_call = NULL;
  244. goto err;
  245. }
  246. for_each_possible_cpu(cpu) {
  247. policy = cpufreq_cpu_get(cpu);
  248. if (!policy) {
  249. ret = -EPROBE_DEFER;
  250. goto err;
  251. }
  252. parent_cpu_data = get_parent_cpu_data(p_data, policy);
  253. if (parent_cpu_data) {
  254. cpufreq_cpu_put(policy);
  255. continue;
  256. }
  257. parent_cpu_data = kzalloc_obj(*parent_cpu_data);
  258. if (!parent_cpu_data) {
  259. ret = -ENOMEM;
  260. goto err_put_policy;
  261. }
  262. cpu_dev = get_cpu_device(cpu);
  263. if (!cpu_dev) {
  264. dev_err(dev, "failed to get cpu device\n");
  265. ret = -ENODEV;
  266. goto err_free_cpu_data;
  267. }
  268. opp_table = dev_pm_opp_get_opp_table(cpu_dev);
  269. if (IS_ERR(opp_table)) {
  270. dev_err(dev, "failed to get opp_table of cpu%d\n", cpu);
  271. ret = PTR_ERR(opp_table);
  272. goto err_free_cpu_data;
  273. }
  274. parent_cpu_data->dev = cpu_dev;
  275. parent_cpu_data->opp_table = opp_table;
  276. parent_cpu_data->first_cpu = cpumask_first(policy->related_cpus);
  277. parent_cpu_data->cur_freq = policy->cur;
  278. parent_cpu_data->min_freq = policy->cpuinfo.min_freq;
  279. parent_cpu_data->max_freq = policy->cpuinfo.max_freq;
  280. list_add_tail(&parent_cpu_data->node, &p_data->cpu_data_list);
  281. cpufreq_cpu_put(policy);
  282. }
  283. mutex_lock(&devfreq->lock);
  284. ret = devfreq_update_target(devfreq, 0L);
  285. mutex_unlock(&devfreq->lock);
  286. if (ret)
  287. dev_err(dev, "failed to update the frequency\n");
  288. return ret;
  289. err_free_cpu_data:
  290. kfree(parent_cpu_data);
  291. err_put_policy:
  292. cpufreq_cpu_put(policy);
  293. err:
  294. return ret;
  295. }
  296. static int devfreq_passive_notifier_call(struct notifier_block *nb,
  297. unsigned long event, void *ptr)
  298. {
  299. struct devfreq_passive_data *data
  300. = container_of(nb, struct devfreq_passive_data, nb);
  301. struct devfreq *devfreq = (struct devfreq *)data->this;
  302. struct devfreq *parent = (struct devfreq *)data->parent;
  303. struct devfreq_freqs *freqs = (struct devfreq_freqs *)ptr;
  304. unsigned long freq = freqs->new;
  305. int ret = 0;
  306. mutex_lock_nested(&devfreq->lock, SINGLE_DEPTH_NESTING);
  307. switch (event) {
  308. case DEVFREQ_PRECHANGE:
  309. if (parent->previous_freq > freq)
  310. ret = devfreq_update_target(devfreq, freq);
  311. break;
  312. case DEVFREQ_POSTCHANGE:
  313. if (parent->previous_freq < freq)
  314. ret = devfreq_update_target(devfreq, freq);
  315. break;
  316. }
  317. mutex_unlock(&devfreq->lock);
  318. if (ret < 0)
  319. dev_warn(&devfreq->dev,
  320. "failed to update devfreq using passive governor\n");
  321. return NOTIFY_DONE;
  322. }
  323. static int devfreq_passive_unregister_notifier(struct devfreq *devfreq)
  324. {
  325. struct devfreq_passive_data *p_data
  326. = (struct devfreq_passive_data *)devfreq->data;
  327. struct devfreq *parent = (struct devfreq *)p_data->parent;
  328. struct notifier_block *nb = &p_data->nb;
  329. return devfreq_unregister_notifier(parent, nb, DEVFREQ_TRANSITION_NOTIFIER);
  330. }
  331. static int devfreq_passive_register_notifier(struct devfreq *devfreq)
  332. {
  333. struct devfreq_passive_data *p_data
  334. = (struct devfreq_passive_data *)devfreq->data;
  335. struct devfreq *parent = (struct devfreq *)p_data->parent;
  336. struct notifier_block *nb = &p_data->nb;
  337. if (!parent)
  338. return -EPROBE_DEFER;
  339. nb->notifier_call = devfreq_passive_notifier_call;
  340. return devfreq_register_notifier(parent, nb, DEVFREQ_TRANSITION_NOTIFIER);
  341. }
  342. static int devfreq_passive_event_handler(struct devfreq *devfreq,
  343. unsigned int event, void *data)
  344. {
  345. struct devfreq_passive_data *p_data
  346. = (struct devfreq_passive_data *)devfreq->data;
  347. int ret = 0;
  348. if (!p_data)
  349. return -EINVAL;
  350. p_data->this = devfreq;
  351. switch (event) {
  352. case DEVFREQ_GOV_START:
  353. if (p_data->parent_type == DEVFREQ_PARENT_DEV)
  354. ret = devfreq_passive_register_notifier(devfreq);
  355. else if (p_data->parent_type == CPUFREQ_PARENT_DEV)
  356. ret = cpufreq_passive_register_notifier(devfreq);
  357. break;
  358. case DEVFREQ_GOV_STOP:
  359. if (p_data->parent_type == DEVFREQ_PARENT_DEV)
  360. WARN_ON(devfreq_passive_unregister_notifier(devfreq));
  361. else if (p_data->parent_type == CPUFREQ_PARENT_DEV)
  362. WARN_ON(cpufreq_passive_unregister_notifier(devfreq));
  363. break;
  364. default:
  365. break;
  366. }
  367. return ret;
  368. }
  369. static struct devfreq_governor devfreq_passive = {
  370. .name = DEVFREQ_GOV_PASSIVE,
  371. .flags = DEVFREQ_GOV_FLAG_IMMUTABLE,
  372. .get_target_freq = devfreq_passive_get_target_freq,
  373. .event_handler = devfreq_passive_event_handler,
  374. };
  375. static int __init devfreq_passive_init(void)
  376. {
  377. return devfreq_add_governor(&devfreq_passive);
  378. }
  379. subsys_initcall(devfreq_passive_init);
  380. static void __exit devfreq_passive_exit(void)
  381. {
  382. int ret;
  383. ret = devfreq_remove_governor(&devfreq_passive);
  384. if (ret)
  385. pr_err("%s: failed remove governor %d\n", __func__, ret);
  386. }
  387. module_exit(devfreq_passive_exit);
  388. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  389. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  390. MODULE_DESCRIPTION("DEVFREQ Passive governor");
  391. MODULE_LICENSE("GPL v2");