energy_model.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Energy Model of devices
  4. *
  5. * Copyright (c) 2018-2020, Arm ltd.
  6. * Written by: Quentin Perret, Arm ltd.
  7. * Improvements provided by: Lukasz Luba, Arm ltd.
  8. */
  9. #define pr_fmt(fmt) "energy_model: " fmt
  10. #include <linux/cpu.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/energy_model.h>
  14. #include <linux/sched/topology.h>
  15. #include <linux/slab.h>
  16. /*
  17. * Mutex serializing the registrations of performance domains and letting
  18. * callbacks defined by drivers sleep.
  19. */
  20. static DEFINE_MUTEX(em_pd_mutex);
  21. static bool _is_cpu_device(struct device *dev)
  22. {
  23. return (dev->bus == &cpu_subsys);
  24. }
  25. #ifdef CONFIG_DEBUG_FS
  26. static struct dentry *rootdir;
  27. static void em_debug_create_ps(struct em_perf_state *ps, struct dentry *pd)
  28. {
  29. struct dentry *d;
  30. char name[24];
  31. snprintf(name, sizeof(name), "ps:%lu", ps->frequency);
  32. /* Create per-ps directory */
  33. d = debugfs_create_dir(name, pd);
  34. debugfs_create_ulong("frequency", 0444, d, &ps->frequency);
  35. debugfs_create_ulong("power", 0444, d, &ps->power);
  36. debugfs_create_ulong("cost", 0444, d, &ps->cost);
  37. }
  38. static int em_debug_cpus_show(struct seq_file *s, void *unused)
  39. {
  40. seq_printf(s, "%*pbl\n", cpumask_pr_args(to_cpumask(s->private)));
  41. return 0;
  42. }
  43. DEFINE_SHOW_ATTRIBUTE(em_debug_cpus);
  44. static int em_debug_units_show(struct seq_file *s, void *unused)
  45. {
  46. struct em_perf_domain *pd = s->private;
  47. char *units = pd->milliwatts ? "milliWatts" : "bogoWatts";
  48. seq_printf(s, "%s\n", units);
  49. return 0;
  50. }
  51. DEFINE_SHOW_ATTRIBUTE(em_debug_units);
  52. static void em_debug_create_pd(struct device *dev)
  53. {
  54. struct dentry *d;
  55. int i;
  56. /* Create the directory of the performance domain */
  57. d = debugfs_create_dir(dev_name(dev), rootdir);
  58. if (_is_cpu_device(dev))
  59. debugfs_create_file("cpus", 0444, d, dev->em_pd->cpus,
  60. &em_debug_cpus_fops);
  61. debugfs_create_file("units", 0444, d, dev->em_pd, &em_debug_units_fops);
  62. /* Create a sub-directory for each performance state */
  63. for (i = 0; i < dev->em_pd->nr_perf_states; i++)
  64. em_debug_create_ps(&dev->em_pd->table[i], d);
  65. }
  66. static void em_debug_remove_pd(struct device *dev)
  67. {
  68. struct dentry *debug_dir;
  69. debug_dir = debugfs_lookup(dev_name(dev), rootdir);
  70. debugfs_remove_recursive(debug_dir);
  71. }
  72. static int __init em_debug_init(void)
  73. {
  74. /* Create /sys/kernel/debug/energy_model directory */
  75. rootdir = debugfs_create_dir("energy_model", NULL);
  76. return 0;
  77. }
  78. fs_initcall(em_debug_init);
  79. #else /* CONFIG_DEBUG_FS */
  80. static void em_debug_create_pd(struct device *dev) {}
  81. static void em_debug_remove_pd(struct device *dev) {}
  82. #endif
  83. static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
  84. int nr_states, struct em_data_callback *cb)
  85. {
  86. unsigned long power, freq, prev_freq = 0, prev_cost = ULONG_MAX;
  87. struct em_perf_state *table;
  88. int i, ret;
  89. u64 fmax;
  90. table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL);
  91. if (!table)
  92. return -ENOMEM;
  93. /* Build the list of performance states for this performance domain */
  94. for (i = 0, freq = 0; i < nr_states; i++, freq++) {
  95. /*
  96. * active_power() is a driver callback which ceils 'freq' to
  97. * lowest performance state of 'dev' above 'freq' and updates
  98. * 'power' and 'freq' accordingly.
  99. */
  100. ret = cb->active_power(&power, &freq, dev);
  101. if (ret) {
  102. dev_err(dev, "EM: invalid perf. state: %d\n",
  103. ret);
  104. goto free_ps_table;
  105. }
  106. /*
  107. * We expect the driver callback to increase the frequency for
  108. * higher performance states.
  109. */
  110. if (freq <= prev_freq) {
  111. dev_err(dev, "EM: non-increasing freq: %lu\n",
  112. freq);
  113. goto free_ps_table;
  114. }
  115. /*
  116. * The power returned by active_state() is expected to be
  117. * positive, in milli-watts and to fit into 16 bits.
  118. */
  119. if (!power || power > EM_MAX_POWER) {
  120. dev_err(dev, "EM: invalid power: %lu\n",
  121. power);
  122. goto free_ps_table;
  123. }
  124. table[i].power = power;
  125. table[i].frequency = prev_freq = freq;
  126. }
  127. /* Compute the cost of each performance state. */
  128. fmax = (u64) table[nr_states - 1].frequency;
  129. for (i = nr_states - 1; i >= 0; i--) {
  130. unsigned long power_res = em_scale_power(table[i].power);
  131. table[i].cost = div64_u64(fmax * power_res,
  132. table[i].frequency);
  133. if (table[i].cost >= prev_cost) {
  134. dev_dbg(dev, "EM: OPP:%lu is inefficient\n",
  135. table[i].frequency);
  136. } else {
  137. prev_cost = table[i].cost;
  138. }
  139. }
  140. pd->table = table;
  141. pd->nr_perf_states = nr_states;
  142. return 0;
  143. free_ps_table:
  144. kfree(table);
  145. return -EINVAL;
  146. }
  147. static int em_create_pd(struct device *dev, int nr_states,
  148. struct em_data_callback *cb, cpumask_t *cpus)
  149. {
  150. struct em_perf_domain *pd;
  151. struct device *cpu_dev;
  152. int cpu, ret;
  153. if (_is_cpu_device(dev)) {
  154. pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
  155. if (!pd)
  156. return -ENOMEM;
  157. cpumask_copy(em_span_cpus(pd), cpus);
  158. } else {
  159. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  160. if (!pd)
  161. return -ENOMEM;
  162. }
  163. ret = em_create_perf_table(dev, pd, nr_states, cb);
  164. if (ret) {
  165. kfree(pd);
  166. return ret;
  167. }
  168. if (_is_cpu_device(dev))
  169. for_each_cpu(cpu, cpus) {
  170. cpu_dev = get_cpu_device(cpu);
  171. cpu_dev->em_pd = pd;
  172. }
  173. dev->em_pd = pd;
  174. return 0;
  175. }
  176. /**
  177. * em_pd_get() - Return the performance domain for a device
  178. * @dev : Device to find the performance domain for
  179. *
  180. * Returns the performance domain to which @dev belongs, or NULL if it doesn't
  181. * exist.
  182. */
  183. struct em_perf_domain *em_pd_get(struct device *dev)
  184. {
  185. if (IS_ERR_OR_NULL(dev))
  186. return NULL;
  187. return dev->em_pd;
  188. }
  189. EXPORT_SYMBOL_GPL(em_pd_get);
  190. /**
  191. * em_cpu_get() - Return the performance domain for a CPU
  192. * @cpu : CPU to find the performance domain for
  193. *
  194. * Returns the performance domain to which @cpu belongs, or NULL if it doesn't
  195. * exist.
  196. */
  197. struct em_perf_domain *em_cpu_get(int cpu)
  198. {
  199. struct device *cpu_dev;
  200. cpu_dev = get_cpu_device(cpu);
  201. if (!cpu_dev)
  202. return NULL;
  203. return em_pd_get(cpu_dev);
  204. }
  205. EXPORT_SYMBOL_GPL(em_cpu_get);
  206. /**
  207. * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
  208. * @dev : Device for which the EM is to register
  209. * @nr_states : Number of performance states to register
  210. * @cb : Callback functions providing the data of the Energy Model
  211. * @cpus : Pointer to cpumask_t, which in case of a CPU device is
  212. * obligatory. It can be taken from i.e. 'policy->cpus'. For other
  213. * type of devices this should be set to NULL.
  214. * @milliwatts : Flag indicating that the power values are in milliWatts or
  215. * in some other scale. It must be set properly.
  216. *
  217. * Create Energy Model tables for a performance domain using the callbacks
  218. * defined in cb.
  219. *
  220. * The @milliwatts is important to set with correct value. Some kernel
  221. * sub-systems might rely on this flag and check if all devices in the EM are
  222. * using the same scale.
  223. *
  224. * If multiple clients register the same performance domain, all but the first
  225. * registration will be ignored.
  226. *
  227. * Return 0 on success
  228. */
  229. int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
  230. struct em_data_callback *cb, cpumask_t *cpus,
  231. bool milliwatts)
  232. {
  233. unsigned long cap, prev_cap = 0;
  234. int cpu, ret;
  235. if (!dev || !nr_states || !cb)
  236. return -EINVAL;
  237. /*
  238. * Use a mutex to serialize the registration of performance domains and
  239. * let the driver-defined callback functions sleep.
  240. */
  241. mutex_lock(&em_pd_mutex);
  242. if (dev->em_pd) {
  243. ret = -EEXIST;
  244. goto unlock;
  245. }
  246. if (_is_cpu_device(dev)) {
  247. if (!cpus) {
  248. dev_err(dev, "EM: invalid CPU mask\n");
  249. ret = -EINVAL;
  250. goto unlock;
  251. }
  252. for_each_cpu(cpu, cpus) {
  253. if (em_cpu_get(cpu)) {
  254. dev_err(dev, "EM: exists for CPU%d\n", cpu);
  255. ret = -EEXIST;
  256. goto unlock;
  257. }
  258. /*
  259. * All CPUs of a domain must have the same
  260. * micro-architecture since they all share the same
  261. * table.
  262. */
  263. cap = arch_scale_cpu_capacity(cpu);
  264. if (prev_cap && prev_cap != cap) {
  265. dev_err(dev, "EM: CPUs of %*pbl must have the same capacity\n",
  266. cpumask_pr_args(cpus));
  267. ret = -EINVAL;
  268. goto unlock;
  269. }
  270. prev_cap = cap;
  271. }
  272. }
  273. ret = em_create_pd(dev, nr_states, cb, cpus);
  274. if (ret)
  275. goto unlock;
  276. dev->em_pd->milliwatts = milliwatts;
  277. em_debug_create_pd(dev);
  278. dev_info(dev, "EM: created perf domain\n");
  279. unlock:
  280. mutex_unlock(&em_pd_mutex);
  281. return ret;
  282. }
  283. EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
  284. /**
  285. * em_dev_unregister_perf_domain() - Unregister Energy Model (EM) for a device
  286. * @dev : Device for which the EM is registered
  287. *
  288. * Unregister the EM for the specified @dev (but not a CPU device).
  289. */
  290. void em_dev_unregister_perf_domain(struct device *dev)
  291. {
  292. if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
  293. return;
  294. if (_is_cpu_device(dev))
  295. return;
  296. /*
  297. * The mutex separates all register/unregister requests and protects
  298. * from potential clean-up/setup issues in the debugfs directories.
  299. * The debugfs directory name is the same as device's name.
  300. */
  301. mutex_lock(&em_pd_mutex);
  302. em_debug_remove_pd(dev);
  303. kfree(dev->em_pd->table);
  304. kfree(dev->em_pd);
  305. dev->em_pd = NULL;
  306. mutex_unlock(&em_pd_mutex);
  307. }
  308. EXPORT_SYMBOL_GPL(em_dev_unregister_perf_domain);