blob: ab6d07e78945db8c4c77a8a480549125592c0a77 [file] [log] [blame]
Nishanth Menona0dd7b72014-05-05 08:33:50 -05001/*
Viresh Kumar33692dc2015-09-04 13:47:25 +05302 * Generic OPP helper interface for CPU device
Nishanth Menona0dd7b72014-05-05 08:33:50 -05003 *
4 * Copyright (C) 2009-2014 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
Viresh Kumard6d2a522015-10-17 09:45:18 +053013
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053016#include <linux/cpu.h>
Nishanth Menona0dd7b72014-05-05 08:33:50 -050017#include <linux/cpufreq.h>
Nishanth Menona0dd7b72014-05-05 08:33:50 -050018#include <linux/err.h>
19#include <linux/errno.h>
20#include <linux/export.h>
Nishanth Menona0dd7b72014-05-05 08:33:50 -050021#include <linux/slab.h>
22
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053023#include "opp.h"
24
Viresh Kumar33692dc2015-09-04 13:47:25 +053025#ifdef CONFIG_CPU_FREQ
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053026
Nishanth Menona0dd7b72014-05-05 08:33:50 -050027/**
28 * dev_pm_opp_init_cpufreq_table() - create a cpufreq table for a device
29 * @dev: device for which we do this operation
30 * @table: Cpufreq table returned back to caller
31 *
32 * Generate a cpufreq table for a provided device- this assumes that the
Viresh Kumar2c2709d2016-02-16 14:17:53 +053033 * opp table is already initialized and ready for usage.
Nishanth Menona0dd7b72014-05-05 08:33:50 -050034 *
35 * This function allocates required memory for the cpufreq table. It is
36 * expected that the caller does the required maintenance such as freeing
37 * the table as required.
38 *
39 * Returns -EINVAL for bad pointers, -ENODEV if the device is not found, -ENOMEM
40 * if no memory available for the operation (table is not populated), returns 0
41 * if successful and table is populated.
42 *
43 * WARNING: It is important for the callers to ensure refreshing their copy of
44 * the table if any of the mentioned functions have been invoked in the interim.
Nishanth Menona0dd7b72014-05-05 08:33:50 -050045 */
46int dev_pm_opp_init_cpufreq_table(struct device *dev,
47 struct cpufreq_frequency_table **table)
48{
49 struct dev_pm_opp *opp;
50 struct cpufreq_frequency_table *freq_table = NULL;
51 int i, max_opps, ret = 0;
52 unsigned long rate;
53
Nishanth Menona0dd7b72014-05-05 08:33:50 -050054 max_opps = dev_pm_opp_get_opp_count(dev);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +053055 if (max_opps <= 0)
56 return max_opps ? max_opps : -ENODATA;
Nishanth Menona0dd7b72014-05-05 08:33:50 -050057
Jia-Ju Bai4a823c02018-01-26 16:48:49 +080058 freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_KERNEL);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +053059 if (!freq_table)
60 return -ENOMEM;
Nishanth Menona0dd7b72014-05-05 08:33:50 -050061
62 for (i = 0, rate = 0; i < max_opps; i++, rate++) {
63 /* find next rate */
64 opp = dev_pm_opp_find_freq_ceil(dev, &rate);
65 if (IS_ERR(opp)) {
66 ret = PTR_ERR(opp);
67 goto out;
68 }
69 freq_table[i].driver_data = i;
70 freq_table[i].frequency = rate / 1000;
Bartlomiej Zolnierkiewicz79eea442015-07-29 16:23:08 +053071
72 /* Is Boost/turbo opp ? */
73 if (dev_pm_opp_is_turbo(opp))
74 freq_table[i].flags = CPUFREQ_BOOST_FREQ;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +053075
76 dev_pm_opp_put(opp);
Nishanth Menona0dd7b72014-05-05 08:33:50 -050077 }
78
79 freq_table[i].driver_data = i;
80 freq_table[i].frequency = CPUFREQ_TABLE_END;
81
82 *table = &freq_table[0];
83
84out:
Nishanth Menona0dd7b72014-05-05 08:33:50 -050085 if (ret)
86 kfree(freq_table);
87
88 return ret;
89}
90EXPORT_SYMBOL_GPL(dev_pm_opp_init_cpufreq_table);
91
92/**
93 * dev_pm_opp_free_cpufreq_table() - free the cpufreq table
94 * @dev: device for which we do this operation
95 * @table: table to free
96 *
97 * Free up the table allocated by dev_pm_opp_init_cpufreq_table
98 */
99void dev_pm_opp_free_cpufreq_table(struct device *dev,
100 struct cpufreq_frequency_table **table)
101{
102 if (!table)
103 return;
104
105 kfree(*table);
106 *table = NULL;
107}
108EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table);
Viresh Kumar33692dc2015-09-04 13:47:25 +0530109#endif /* CONFIG_CPU_FREQ */
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530110
Viresh Kumar2a4eb732018-09-13 13:14:36 +0530111void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask,
Viresh Kumar404b1362018-09-13 13:09:27 +0530112 int last_cpu)
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530113{
114 struct device *cpu_dev;
115 int cpu;
116
117 WARN_ON(cpumask_empty(cpumask));
118
119 for_each_cpu(cpu, cpumask) {
Viresh Kumar404b1362018-09-13 13:09:27 +0530120 if (cpu == last_cpu)
121 break;
122
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530123 cpu_dev = get_cpu_device(cpu);
124 if (!cpu_dev) {
125 pr_err("%s: failed to get cpu%d device\n", __func__,
126 cpu);
127 continue;
128 }
129
Viresh Kumar2a4eb732018-09-13 13:14:36 +0530130 _dev_pm_opp_find_and_remove_table(cpu_dev);
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530131 }
132}
Sudeep Holla411466c2016-05-03 15:05:04 +0100133
134/**
135 * dev_pm_opp_cpumask_remove_table() - Removes OPP table for @cpumask
136 * @cpumask: cpumask for which OPP table needs to be removed
137 *
138 * This removes the OPP tables for CPUs present in the @cpumask.
139 * This should be used to remove all the OPPs entries associated with
140 * the cpus in @cpumask.
Sudeep Holla411466c2016-05-03 15:05:04 +0100141 */
142void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask)
143{
Viresh Kumar2a4eb732018-09-13 13:14:36 +0530144 _dev_pm_opp_cpumask_remove_table(cpumask, -1);
Sudeep Holla411466c2016-05-03 15:05:04 +0100145}
146EXPORT_SYMBOL_GPL(dev_pm_opp_cpumask_remove_table);
147
Viresh Kumar2c931042016-04-21 14:28:56 +0530148/**
149 * dev_pm_opp_set_sharing_cpus() - Mark OPP table as shared by few CPUs
150 * @cpu_dev: CPU device for which we do this operation
151 * @cpumask: cpumask of the CPUs which share the OPP table with @cpu_dev
152 *
153 * This marks OPP table of the @cpu_dev as shared by the CPUs present in
154 * @cpumask.
155 *
156 * Returns -ENODEV if OPP table isn't already present.
Viresh Kumar2c931042016-04-21 14:28:56 +0530157 */
Viresh Kumardde370b2016-04-27 08:52:22 +0530158int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev,
Arnd Bergmannddbb74b2016-04-30 13:33:29 +0200159 const struct cpumask *cpumask)
Viresh Kumar2c931042016-04-21 14:28:56 +0530160{
161 struct opp_device *opp_dev;
162 struct opp_table *opp_table;
163 struct device *dev;
164 int cpu, ret = 0;
165
Viresh Kumar2c931042016-04-21 14:28:56 +0530166 opp_table = _find_opp_table(cpu_dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530167 if (IS_ERR(opp_table))
168 return PTR_ERR(opp_table);
Viresh Kumar2c931042016-04-21 14:28:56 +0530169
170 for_each_cpu(cpu, cpumask) {
171 if (cpu == cpu_dev->id)
172 continue;
173
174 dev = get_cpu_device(cpu);
175 if (!dev) {
176 dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
177 __func__, cpu);
178 continue;
179 }
180
181 opp_dev = _add_opp_dev(dev, opp_table);
182 if (!opp_dev) {
183 dev_err(dev, "%s: failed to add opp-dev for cpu%d device\n",
184 __func__, cpu);
185 continue;
186 }
Viresh Kumar46e7a4e2016-04-21 14:28:57 +0530187
188 /* Mark opp-table as multiple CPUs are sharing it now */
Viresh Kumar79ee2e82016-06-16 19:03:11 +0530189 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;
Viresh Kumar2c931042016-04-21 14:28:56 +0530190 }
Viresh Kumar5b650b32017-01-23 10:11:48 +0530191
192 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar2c931042016-04-21 14:28:56 +0530193
194 return ret;
195}
196EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus);
Viresh Kumar6f707da2016-04-27 08:52:23 +0530197
198/**
199 * dev_pm_opp_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with @cpu_dev
200 * @cpu_dev: CPU device for which we do this operation
201 * @cpumask: cpumask to update with information of sharing CPUs
202 *
203 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.
204 *
Viresh Kumar79ee2e82016-06-16 19:03:11 +0530205 * Returns -ENODEV if OPP table isn't already present and -EINVAL if the OPP
206 * table's status is access-unknown.
Viresh Kumar6f707da2016-04-27 08:52:23 +0530207 */
Arnd Bergmannddbb74b2016-04-30 13:33:29 +0200208int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
Viresh Kumar6f707da2016-04-27 08:52:23 +0530209{
210 struct opp_device *opp_dev;
211 struct opp_table *opp_table;
212 int ret = 0;
213
Viresh Kumar6f707da2016-04-27 08:52:23 +0530214 opp_table = _find_opp_table(cpu_dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530215 if (IS_ERR(opp_table))
216 return PTR_ERR(opp_table);
Viresh Kumar6f707da2016-04-27 08:52:23 +0530217
Viresh Kumar79ee2e82016-06-16 19:03:11 +0530218 if (opp_table->shared_opp == OPP_TABLE_ACCESS_UNKNOWN) {
219 ret = -EINVAL;
Viresh Kumar5b650b32017-01-23 10:11:48 +0530220 goto put_opp_table;
Viresh Kumar79ee2e82016-06-16 19:03:11 +0530221 }
222
Viresh Kumar6f707da2016-04-27 08:52:23 +0530223 cpumask_clear(cpumask);
224
Viresh Kumar79ee2e82016-06-16 19:03:11 +0530225 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
Viresh Kumar3d255692018-08-03 07:05:21 +0530226 mutex_lock(&opp_table->lock);
Viresh Kumar6f707da2016-04-27 08:52:23 +0530227 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
228 cpumask_set_cpu(opp_dev->dev->id, cpumask);
Viresh Kumar3d255692018-08-03 07:05:21 +0530229 mutex_unlock(&opp_table->lock);
Viresh Kumar6f707da2016-04-27 08:52:23 +0530230 } else {
231 cpumask_set_cpu(cpu_dev->id, cpumask);
232 }
233
Viresh Kumar5b650b32017-01-23 10:11:48 +0530234put_opp_table:
235 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar6f707da2016-04-27 08:52:23 +0530236
237 return ret;
238}
239EXPORT_SYMBOL_GPL(dev_pm_opp_get_sharing_cpus);