energy_model.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Energy Model of devices
  4. *
  5. * Copyright (c) 2018-2021, 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/cpufreq.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/energy_model.h>
  15. #include <linux/sched/topology.h>
  16. #include <linux/slab.h>
  17. /*
  18. * Mutex serializing the registrations of performance domains and letting
  19. * callbacks defined by drivers sleep.
  20. */
  21. static DEFINE_MUTEX(em_pd_mutex);
  22. static void em_cpufreq_update_efficiencies(struct device *dev,
  23. struct em_perf_state *table);
  24. static void em_check_capacity_update(void);
  25. static void em_update_workfn(struct work_struct *work);
  26. static DECLARE_DELAYED_WORK(em_update_work, em_update_workfn);
  27. static bool _is_cpu_device(struct device *dev)
  28. {
  29. return (dev->bus == &cpu_subsys);
  30. }
  31. #ifdef CONFIG_DEBUG_FS
  32. static struct dentry *rootdir;
  33. struct em_dbg_info {
  34. struct em_perf_domain *pd;
  35. int ps_id;
  36. };
  37. #define DEFINE_EM_DBG_SHOW(name, fname) \
  38. static int em_debug_##fname##_show(struct seq_file *s, void *unused) \
  39. { \
  40. struct em_dbg_info *em_dbg = s->private; \
  41. struct em_perf_state *table; \
  42. unsigned long val; \
  43. \
  44. rcu_read_lock(); \
  45. table = em_perf_state_from_pd(em_dbg->pd); \
  46. val = table[em_dbg->ps_id].name; \
  47. rcu_read_unlock(); \
  48. \
  49. seq_printf(s, "%lu\n", val); \
  50. return 0; \
  51. } \
  52. DEFINE_SHOW_ATTRIBUTE(em_debug_##fname)
  53. DEFINE_EM_DBG_SHOW(frequency, frequency);
  54. DEFINE_EM_DBG_SHOW(power, power);
  55. DEFINE_EM_DBG_SHOW(cost, cost);
  56. DEFINE_EM_DBG_SHOW(performance, performance);
  57. DEFINE_EM_DBG_SHOW(flags, inefficiency);
  58. static void em_debug_create_ps(struct em_perf_domain *em_pd,
  59. struct em_dbg_info *em_dbg, int i,
  60. struct dentry *pd)
  61. {
  62. struct em_perf_state *table;
  63. unsigned long freq;
  64. struct dentry *d;
  65. char name[24];
  66. em_dbg[i].pd = em_pd;
  67. em_dbg[i].ps_id = i;
  68. rcu_read_lock();
  69. table = em_perf_state_from_pd(em_pd);
  70. freq = table[i].frequency;
  71. rcu_read_unlock();
  72. snprintf(name, sizeof(name), "ps:%lu", freq);
  73. /* Create per-ps directory */
  74. d = debugfs_create_dir(name, pd);
  75. debugfs_create_file("frequency", 0444, d, &em_dbg[i],
  76. &em_debug_frequency_fops);
  77. debugfs_create_file("power", 0444, d, &em_dbg[i],
  78. &em_debug_power_fops);
  79. debugfs_create_file("cost", 0444, d, &em_dbg[i],
  80. &em_debug_cost_fops);
  81. debugfs_create_file("performance", 0444, d, &em_dbg[i],
  82. &em_debug_performance_fops);
  83. debugfs_create_file("inefficient", 0444, d, &em_dbg[i],
  84. &em_debug_inefficiency_fops);
  85. }
  86. static int em_debug_cpus_show(struct seq_file *s, void *unused)
  87. {
  88. seq_printf(s, "%*pbl\n", cpumask_pr_args(to_cpumask(s->private)));
  89. return 0;
  90. }
  91. DEFINE_SHOW_ATTRIBUTE(em_debug_cpus);
  92. static int em_debug_flags_show(struct seq_file *s, void *unused)
  93. {
  94. struct em_perf_domain *pd = s->private;
  95. seq_printf(s, "%#lx\n", pd->flags);
  96. return 0;
  97. }
  98. DEFINE_SHOW_ATTRIBUTE(em_debug_flags);
  99. static void em_debug_create_pd(struct device *dev)
  100. {
  101. struct em_dbg_info *em_dbg;
  102. struct dentry *d;
  103. int i;
  104. /* Create the directory of the performance domain */
  105. d = debugfs_create_dir(dev_name(dev), rootdir);
  106. if (_is_cpu_device(dev))
  107. debugfs_create_file("cpus", 0444, d, dev->em_pd->cpus,
  108. &em_debug_cpus_fops);
  109. debugfs_create_file("flags", 0444, d, dev->em_pd,
  110. &em_debug_flags_fops);
  111. em_dbg = devm_kcalloc(dev, dev->em_pd->nr_perf_states,
  112. sizeof(*em_dbg), GFP_KERNEL);
  113. if (!em_dbg)
  114. return;
  115. /* Create a sub-directory for each performance state */
  116. for (i = 0; i < dev->em_pd->nr_perf_states; i++)
  117. em_debug_create_ps(dev->em_pd, em_dbg, i, d);
  118. }
  119. static void em_debug_remove_pd(struct device *dev)
  120. {
  121. debugfs_lookup_and_remove(dev_name(dev), rootdir);
  122. }
  123. static int __init em_debug_init(void)
  124. {
  125. /* Create /sys/kernel/debug/energy_model directory */
  126. rootdir = debugfs_create_dir("energy_model", NULL);
  127. return 0;
  128. }
  129. fs_initcall(em_debug_init);
  130. #else /* CONFIG_DEBUG_FS */
  131. static void em_debug_create_pd(struct device *dev) {}
  132. static void em_debug_remove_pd(struct device *dev) {}
  133. #endif
  134. static void em_release_table_kref(struct kref *kref)
  135. {
  136. /* It was the last owner of this table so we can free */
  137. kfree_rcu(container_of(kref, struct em_perf_table, kref), rcu);
  138. }
  139. /**
  140. * em_table_free() - Handles safe free of the EM table when needed
  141. * @table : EM table which is going to be freed
  142. *
  143. * No return values.
  144. */
  145. void em_table_free(struct em_perf_table *table)
  146. {
  147. kref_put(&table->kref, em_release_table_kref);
  148. }
  149. /**
  150. * em_table_alloc() - Allocate a new EM table
  151. * @pd : EM performance domain for which this must be done
  152. *
  153. * Allocate a new EM table and initialize its kref to indicate that it
  154. * has a user.
  155. * Returns allocated table or NULL.
  156. */
  157. struct em_perf_table *em_table_alloc(struct em_perf_domain *pd)
  158. {
  159. struct em_perf_table *table;
  160. int table_size;
  161. table_size = sizeof(struct em_perf_state) * pd->nr_perf_states;
  162. table = kzalloc(sizeof(*table) + table_size, GFP_KERNEL);
  163. if (!table)
  164. return NULL;
  165. kref_init(&table->kref);
  166. return table;
  167. }
  168. static void em_init_performance(struct device *dev, struct em_perf_domain *pd,
  169. struct em_perf_state *table, int nr_states)
  170. {
  171. u64 fmax, max_cap;
  172. int i, cpu;
  173. /* This is needed only for CPUs and EAS skip other devices */
  174. if (!_is_cpu_device(dev))
  175. return;
  176. cpu = cpumask_first(em_span_cpus(pd));
  177. /*
  178. * Calculate the performance value for each frequency with
  179. * linear relationship. The final CPU capacity might not be ready at
  180. * boot time, but the EM will be updated a bit later with correct one.
  181. */
  182. fmax = (u64) table[nr_states - 1].frequency;
  183. max_cap = (u64) arch_scale_cpu_capacity(cpu);
  184. for (i = 0; i < nr_states; i++)
  185. table[i].performance = div64_u64(max_cap * table[i].frequency,
  186. fmax);
  187. }
  188. static int em_compute_costs(struct device *dev, struct em_perf_state *table,
  189. struct em_data_callback *cb, int nr_states,
  190. unsigned long flags)
  191. {
  192. unsigned long prev_cost = ULONG_MAX;
  193. int i, ret;
  194. /* This is needed only for CPUs and EAS skip other devices */
  195. if (!_is_cpu_device(dev))
  196. return 0;
  197. /* Compute the cost of each performance state. */
  198. for (i = nr_states - 1; i >= 0; i--) {
  199. unsigned long power_res, cost;
  200. if ((flags & EM_PERF_DOMAIN_ARTIFICIAL) && cb->get_cost) {
  201. ret = cb->get_cost(dev, table[i].frequency, &cost);
  202. if (ret || !cost || cost > EM_MAX_POWER) {
  203. dev_err(dev, "EM: invalid cost %lu %d\n",
  204. cost, ret);
  205. return -EINVAL;
  206. }
  207. } else {
  208. /* increase resolution of 'cost' precision */
  209. power_res = table[i].power * 10;
  210. cost = power_res / table[i].performance;
  211. }
  212. table[i].cost = cost;
  213. if (table[i].cost >= prev_cost) {
  214. table[i].flags = EM_PERF_STATE_INEFFICIENT;
  215. dev_dbg(dev, "EM: OPP:%lu is inefficient\n",
  216. table[i].frequency);
  217. } else {
  218. prev_cost = table[i].cost;
  219. }
  220. }
  221. return 0;
  222. }
  223. /**
  224. * em_dev_compute_costs() - Calculate cost values for new runtime EM table
  225. * @dev : Device for which the EM table is to be updated
  226. * @table : The new EM table that is going to get the costs calculated
  227. * @nr_states : Number of performance states
  228. *
  229. * Calculate the em_perf_state::cost values for new runtime EM table. The
  230. * values are used for EAS during task placement. It also calculates and sets
  231. * the efficiency flag for each performance state. When the function finish
  232. * successfully the EM table is ready to be updated and used by EAS.
  233. *
  234. * Return 0 on success or a proper error in case of failure.
  235. */
  236. int em_dev_compute_costs(struct device *dev, struct em_perf_state *table,
  237. int nr_states)
  238. {
  239. return em_compute_costs(dev, table, NULL, nr_states, 0);
  240. }
  241. /**
  242. * em_dev_update_perf_domain() - Update runtime EM table for a device
  243. * @dev : Device for which the EM is to be updated
  244. * @new_table : The new EM table that is going to be used from now
  245. *
  246. * Update EM runtime modifiable table for the @dev using the provided @table.
  247. *
  248. * This function uses a mutex to serialize writers, so it must not be called
  249. * from a non-sleeping context.
  250. *
  251. * Return 0 on success or an error code on failure.
  252. */
  253. int em_dev_update_perf_domain(struct device *dev,
  254. struct em_perf_table *new_table)
  255. {
  256. struct em_perf_table *old_table;
  257. struct em_perf_domain *pd;
  258. if (!dev)
  259. return -EINVAL;
  260. /* Serialize update/unregister or concurrent updates */
  261. mutex_lock(&em_pd_mutex);
  262. if (!dev->em_pd) {
  263. mutex_unlock(&em_pd_mutex);
  264. return -EINVAL;
  265. }
  266. pd = dev->em_pd;
  267. kref_get(&new_table->kref);
  268. old_table = rcu_dereference_protected(pd->em_table,
  269. lockdep_is_held(&em_pd_mutex));
  270. rcu_assign_pointer(pd->em_table, new_table);
  271. em_cpufreq_update_efficiencies(dev, new_table->state);
  272. em_table_free(old_table);
  273. mutex_unlock(&em_pd_mutex);
  274. return 0;
  275. }
  276. EXPORT_SYMBOL_GPL(em_dev_update_perf_domain);
  277. static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
  278. struct em_perf_state *table,
  279. struct em_data_callback *cb,
  280. unsigned long flags)
  281. {
  282. unsigned long power, freq, prev_freq = 0;
  283. int nr_states = pd->nr_perf_states;
  284. int i, ret;
  285. /* Build the list of performance states for this performance domain */
  286. for (i = 0, freq = 0; i < nr_states; i++, freq++) {
  287. /*
  288. * active_power() is a driver callback which ceils 'freq' to
  289. * lowest performance state of 'dev' above 'freq' and updates
  290. * 'power' and 'freq' accordingly.
  291. */
  292. ret = cb->active_power(dev, &power, &freq);
  293. if (ret) {
  294. dev_err(dev, "EM: invalid perf. state: %d\n",
  295. ret);
  296. return -EINVAL;
  297. }
  298. /*
  299. * We expect the driver callback to increase the frequency for
  300. * higher performance states.
  301. */
  302. if (freq <= prev_freq) {
  303. dev_err(dev, "EM: non-increasing freq: %lu\n",
  304. freq);
  305. return -EINVAL;
  306. }
  307. /*
  308. * The power returned by active_state() is expected to be
  309. * positive and be in range.
  310. */
  311. if (!power || power > EM_MAX_POWER) {
  312. dev_err(dev, "EM: invalid power: %lu\n",
  313. power);
  314. return -EINVAL;
  315. }
  316. table[i].power = power;
  317. table[i].frequency = prev_freq = freq;
  318. }
  319. em_init_performance(dev, pd, table, nr_states);
  320. ret = em_compute_costs(dev, table, cb, nr_states, flags);
  321. if (ret)
  322. return -EINVAL;
  323. return 0;
  324. }
  325. static int em_create_pd(struct device *dev, int nr_states,
  326. struct em_data_callback *cb, cpumask_t *cpus,
  327. unsigned long flags)
  328. {
  329. struct em_perf_table *em_table;
  330. struct em_perf_domain *pd;
  331. struct device *cpu_dev;
  332. int cpu, ret, num_cpus;
  333. if (_is_cpu_device(dev)) {
  334. num_cpus = cpumask_weight(cpus);
  335. /* Prevent max possible energy calculation to not overflow */
  336. if (num_cpus > EM_MAX_NUM_CPUS) {
  337. dev_err(dev, "EM: too many CPUs, overflow possible\n");
  338. return -EINVAL;
  339. }
  340. pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
  341. if (!pd)
  342. return -ENOMEM;
  343. cpumask_copy(em_span_cpus(pd), cpus);
  344. } else {
  345. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  346. if (!pd)
  347. return -ENOMEM;
  348. }
  349. pd->nr_perf_states = nr_states;
  350. em_table = em_table_alloc(pd);
  351. if (!em_table)
  352. goto free_pd;
  353. ret = em_create_perf_table(dev, pd, em_table->state, cb, flags);
  354. if (ret)
  355. goto free_pd_table;
  356. rcu_assign_pointer(pd->em_table, em_table);
  357. if (_is_cpu_device(dev))
  358. for_each_cpu(cpu, cpus) {
  359. cpu_dev = get_cpu_device(cpu);
  360. cpu_dev->em_pd = pd;
  361. }
  362. dev->em_pd = pd;
  363. return 0;
  364. free_pd_table:
  365. kfree(em_table);
  366. free_pd:
  367. kfree(pd);
  368. return -EINVAL;
  369. }
  370. static void
  371. em_cpufreq_update_efficiencies(struct device *dev, struct em_perf_state *table)
  372. {
  373. struct em_perf_domain *pd = dev->em_pd;
  374. struct cpufreq_policy *policy;
  375. int found = 0;
  376. int i, cpu;
  377. if (!_is_cpu_device(dev))
  378. return;
  379. /* Try to get a CPU which is active and in this PD */
  380. cpu = cpumask_first_and(em_span_cpus(pd), cpu_active_mask);
  381. if (cpu >= nr_cpu_ids) {
  382. dev_warn(dev, "EM: No online CPU for CPUFreq policy\n");
  383. return;
  384. }
  385. policy = cpufreq_cpu_get(cpu);
  386. if (!policy) {
  387. dev_warn(dev, "EM: Access to CPUFreq policy failed\n");
  388. return;
  389. }
  390. for (i = 0; i < pd->nr_perf_states; i++) {
  391. if (!(table[i].flags & EM_PERF_STATE_INEFFICIENT))
  392. continue;
  393. if (!cpufreq_table_set_inefficient(policy, table[i].frequency))
  394. found++;
  395. }
  396. cpufreq_cpu_put(policy);
  397. if (!found)
  398. return;
  399. /*
  400. * Efficiencies have been installed in CPUFreq, inefficient frequencies
  401. * will be skipped. The EM can do the same.
  402. */
  403. pd->flags |= EM_PERF_DOMAIN_SKIP_INEFFICIENCIES;
  404. }
  405. /**
  406. * em_pd_get() - Return the performance domain for a device
  407. * @dev : Device to find the performance domain for
  408. *
  409. * Returns the performance domain to which @dev belongs, or NULL if it doesn't
  410. * exist.
  411. */
  412. struct em_perf_domain *em_pd_get(struct device *dev)
  413. {
  414. if (IS_ERR_OR_NULL(dev))
  415. return NULL;
  416. return dev->em_pd;
  417. }
  418. EXPORT_SYMBOL_GPL(em_pd_get);
  419. /**
  420. * em_cpu_get() - Return the performance domain for a CPU
  421. * @cpu : CPU to find the performance domain for
  422. *
  423. * Returns the performance domain to which @cpu belongs, or NULL if it doesn't
  424. * exist.
  425. */
  426. struct em_perf_domain *em_cpu_get(int cpu)
  427. {
  428. struct device *cpu_dev;
  429. cpu_dev = get_cpu_device(cpu);
  430. if (!cpu_dev)
  431. return NULL;
  432. return em_pd_get(cpu_dev);
  433. }
  434. EXPORT_SYMBOL_GPL(em_cpu_get);
  435. /**
  436. * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
  437. * @dev : Device for which the EM is to register
  438. * @nr_states : Number of performance states to register
  439. * @cb : Callback functions providing the data of the Energy Model
  440. * @cpus : Pointer to cpumask_t, which in case of a CPU device is
  441. * obligatory. It can be taken from i.e. 'policy->cpus'. For other
  442. * type of devices this should be set to NULL.
  443. * @microwatts : Flag indicating that the power values are in micro-Watts or
  444. * in some other scale. It must be set properly.
  445. *
  446. * Create Energy Model tables for a performance domain using the callbacks
  447. * defined in cb.
  448. *
  449. * The @microwatts is important to set with correct value. Some kernel
  450. * sub-systems might rely on this flag and check if all devices in the EM are
  451. * using the same scale.
  452. *
  453. * If multiple clients register the same performance domain, all but the first
  454. * registration will be ignored.
  455. *
  456. * Return 0 on success
  457. */
  458. int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
  459. struct em_data_callback *cb, cpumask_t *cpus,
  460. bool microwatts)
  461. {
  462. struct em_perf_table *em_table;
  463. unsigned long cap, prev_cap = 0;
  464. unsigned long flags = 0;
  465. int cpu, ret;
  466. if (!dev || !nr_states || !cb)
  467. return -EINVAL;
  468. /*
  469. * Use a mutex to serialize the registration of performance domains and
  470. * let the driver-defined callback functions sleep.
  471. */
  472. mutex_lock(&em_pd_mutex);
  473. if (dev->em_pd) {
  474. ret = -EEXIST;
  475. goto unlock;
  476. }
  477. if (_is_cpu_device(dev)) {
  478. if (!cpus) {
  479. dev_err(dev, "EM: invalid CPU mask\n");
  480. ret = -EINVAL;
  481. goto unlock;
  482. }
  483. for_each_cpu(cpu, cpus) {
  484. if (em_cpu_get(cpu)) {
  485. dev_err(dev, "EM: exists for CPU%d\n", cpu);
  486. ret = -EEXIST;
  487. goto unlock;
  488. }
  489. /*
  490. * All CPUs of a domain must have the same
  491. * micro-architecture since they all share the same
  492. * table.
  493. */
  494. cap = arch_scale_cpu_capacity(cpu);
  495. if (prev_cap && prev_cap != cap) {
  496. dev_err(dev, "EM: CPUs of %*pbl must have the same capacity\n",
  497. cpumask_pr_args(cpus));
  498. ret = -EINVAL;
  499. goto unlock;
  500. }
  501. prev_cap = cap;
  502. }
  503. }
  504. if (microwatts)
  505. flags |= EM_PERF_DOMAIN_MICROWATTS;
  506. else if (cb->get_cost)
  507. flags |= EM_PERF_DOMAIN_ARTIFICIAL;
  508. /*
  509. * EM only supports uW (exception is artificial EM).
  510. * Therefore, check and force the drivers to provide
  511. * power in uW.
  512. */
  513. if (!microwatts && !(flags & EM_PERF_DOMAIN_ARTIFICIAL)) {
  514. dev_err(dev, "EM: only supports uW power values\n");
  515. ret = -EINVAL;
  516. goto unlock;
  517. }
  518. ret = em_create_pd(dev, nr_states, cb, cpus, flags);
  519. if (ret)
  520. goto unlock;
  521. dev->em_pd->flags |= flags;
  522. em_table = rcu_dereference_protected(dev->em_pd->em_table,
  523. lockdep_is_held(&em_pd_mutex));
  524. em_cpufreq_update_efficiencies(dev, em_table->state);
  525. em_debug_create_pd(dev);
  526. dev_info(dev, "EM: created perf domain\n");
  527. unlock:
  528. mutex_unlock(&em_pd_mutex);
  529. if (_is_cpu_device(dev))
  530. em_check_capacity_update();
  531. return ret;
  532. }
  533. EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
  534. /**
  535. * em_dev_unregister_perf_domain() - Unregister Energy Model (EM) for a device
  536. * @dev : Device for which the EM is registered
  537. *
  538. * Unregister the EM for the specified @dev (but not a CPU device).
  539. */
  540. void em_dev_unregister_perf_domain(struct device *dev)
  541. {
  542. if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
  543. return;
  544. if (_is_cpu_device(dev))
  545. return;
  546. /*
  547. * The mutex separates all register/unregister requests and protects
  548. * from potential clean-up/setup issues in the debugfs directories.
  549. * The debugfs directory name is the same as device's name.
  550. */
  551. mutex_lock(&em_pd_mutex);
  552. em_debug_remove_pd(dev);
  553. em_table_free(rcu_dereference_protected(dev->em_pd->em_table,
  554. lockdep_is_held(&em_pd_mutex)));
  555. kfree(dev->em_pd);
  556. dev->em_pd = NULL;
  557. mutex_unlock(&em_pd_mutex);
  558. }
  559. EXPORT_SYMBOL_GPL(em_dev_unregister_perf_domain);
  560. static struct em_perf_table *em_table_dup(struct em_perf_domain *pd)
  561. {
  562. struct em_perf_table *em_table;
  563. struct em_perf_state *ps, *new_ps;
  564. int ps_size;
  565. em_table = em_table_alloc(pd);
  566. if (!em_table)
  567. return NULL;
  568. new_ps = em_table->state;
  569. rcu_read_lock();
  570. ps = em_perf_state_from_pd(pd);
  571. /* Initialize data based on old table */
  572. ps_size = sizeof(struct em_perf_state) * pd->nr_perf_states;
  573. memcpy(new_ps, ps, ps_size);
  574. rcu_read_unlock();
  575. return em_table;
  576. }
  577. static int em_recalc_and_update(struct device *dev, struct em_perf_domain *pd,
  578. struct em_perf_table *em_table)
  579. {
  580. int ret;
  581. ret = em_compute_costs(dev, em_table->state, NULL, pd->nr_perf_states,
  582. pd->flags);
  583. if (ret)
  584. goto free_em_table;
  585. ret = em_dev_update_perf_domain(dev, em_table);
  586. if (ret)
  587. goto free_em_table;
  588. /*
  589. * This is one-time-update, so give up the ownership in this updater.
  590. * The EM framework has incremented the usage counter and from now
  591. * will keep the reference (then free the memory when needed).
  592. */
  593. free_em_table:
  594. em_table_free(em_table);
  595. return ret;
  596. }
  597. /*
  598. * Adjustment of CPU performance values after boot, when all CPUs capacites
  599. * are correctly calculated.
  600. */
  601. static void em_adjust_new_capacity(unsigned int cpu, struct device *dev,
  602. struct em_perf_domain *pd)
  603. {
  604. unsigned long cpu_capacity = arch_scale_cpu_capacity(cpu);
  605. struct em_perf_table *em_table;
  606. struct em_perf_state *table;
  607. unsigned long em_max_perf;
  608. rcu_read_lock();
  609. table = em_perf_state_from_pd(pd);
  610. em_max_perf = table[pd->nr_perf_states - 1].performance;
  611. rcu_read_unlock();
  612. if (em_max_perf == cpu_capacity)
  613. return;
  614. pr_debug("updating cpu%d cpu_cap=%lu old capacity=%lu\n", cpu,
  615. cpu_capacity, em_max_perf);
  616. em_table = em_table_dup(pd);
  617. if (!em_table) {
  618. dev_warn(dev, "EM: allocation failed\n");
  619. return;
  620. }
  621. em_init_performance(dev, pd, em_table->state, pd->nr_perf_states);
  622. em_recalc_and_update(dev, pd, em_table);
  623. }
  624. static void em_check_capacity_update(void)
  625. {
  626. cpumask_var_t cpu_done_mask;
  627. int cpu, failed_cpus = 0;
  628. if (!zalloc_cpumask_var(&cpu_done_mask, GFP_KERNEL)) {
  629. pr_warn("no free memory\n");
  630. return;
  631. }
  632. /* Check if CPUs capacity has changed than update EM */
  633. for_each_possible_cpu(cpu) {
  634. struct cpufreq_policy *policy;
  635. struct em_perf_domain *pd;
  636. struct device *dev;
  637. if (cpumask_test_cpu(cpu, cpu_done_mask))
  638. continue;
  639. policy = cpufreq_cpu_get(cpu);
  640. if (!policy) {
  641. failed_cpus++;
  642. continue;
  643. }
  644. cpufreq_cpu_put(policy);
  645. dev = get_cpu_device(cpu);
  646. pd = em_pd_get(dev);
  647. if (!pd || em_is_artificial(pd))
  648. continue;
  649. cpumask_or(cpu_done_mask, cpu_done_mask,
  650. em_span_cpus(pd));
  651. em_adjust_new_capacity(cpu, dev, pd);
  652. }
  653. if (failed_cpus)
  654. schedule_delayed_work(&em_update_work, msecs_to_jiffies(1000));
  655. free_cpumask_var(cpu_done_mask);
  656. }
  657. static void em_update_workfn(struct work_struct *work)
  658. {
  659. em_check_capacity_update();
  660. }
  661. /**
  662. * em_dev_update_chip_binning() - Update Energy Model after the new voltage
  663. * information is present in the OPPs.
  664. * @dev : Device for which the Energy Model has to be updated.
  665. *
  666. * This function allows to update easily the EM with new values available in
  667. * the OPP framework and DT. It can be used after the chip has been properly
  668. * verified by device drivers and the voltages adjusted for the 'chip binning'.
  669. */
  670. int em_dev_update_chip_binning(struct device *dev)
  671. {
  672. struct em_perf_table *em_table;
  673. struct em_perf_domain *pd;
  674. int i, ret;
  675. if (IS_ERR_OR_NULL(dev))
  676. return -EINVAL;
  677. pd = em_pd_get(dev);
  678. if (!pd) {
  679. dev_warn(dev, "Couldn't find Energy Model\n");
  680. return -EINVAL;
  681. }
  682. em_table = em_table_dup(pd);
  683. if (!em_table) {
  684. dev_warn(dev, "EM: allocation failed\n");
  685. return -ENOMEM;
  686. }
  687. /* Update power values which might change due to new voltage in OPPs */
  688. for (i = 0; i < pd->nr_perf_states; i++) {
  689. unsigned long freq = em_table->state[i].frequency;
  690. unsigned long power;
  691. ret = dev_pm_opp_calc_power(dev, &power, &freq);
  692. if (ret) {
  693. em_table_free(em_table);
  694. return ret;
  695. }
  696. em_table->state[i].power = power;
  697. }
  698. return em_recalc_and_update(dev, pd, em_table);
  699. }
  700. EXPORT_SYMBOL_GPL(em_dev_update_chip_binning);