Some clients, such as PCIe, may operate at the same clock frequency
across different data rates by varying link width. In such cases,
frequency alone is not sufficient to uniquely identify an OPP.
To support these scenarios, introduce a new API
dev_pm_opp_find_freq_level_exact() that allows OPP lookup using
both frequency and performance level.
Signed-off-by: Krishna Chaitanya Chundru <krishna.chundru@oss.qualcomm.com>
---
drivers/opp/core.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++
drivers/opp/opp.h | 13 +++++++
include/linux/pm_opp.h | 13 +++++++
3 files changed, 129 insertions(+)
diff --git a/drivers/opp/core.c b/drivers/opp/core.c
index edbd60501cf00dfd1957f7d19b228d1c61bbbdcc..4aeb394dcd73a1ca70899a887f8a8c4282ec6d57 100644
--- a/drivers/opp/core.c
+++ b/drivers/opp/core.c
@@ -461,6 +461,14 @@ int dev_pm_opp_get_opp_count(struct device *dev)
EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
/* Helpers to read keys */
+static unsigned long _read_opp_key(struct dev_pm_opp *opp, int index, struct dev_pm_opp_key *key)
+{
+ key->freq = opp->rates[index];
+ key->level = opp->level;
+
+ return true;
+}
+
static unsigned long _read_freq(struct dev_pm_opp *opp, int index)
{
return opp->rates[index];
@@ -488,6 +496,21 @@ static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
return false;
}
+static bool _compare_opp_key_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
+ struct dev_pm_opp_key opp_key, struct dev_pm_opp_key key)
+{
+ bool freq_match = (opp_key.freq == 0 || key.freq == 0 || opp_key.freq == key.freq);
+ bool level_match = (opp_key.level == OPP_LEVEL_UNSET ||
+ key.level == OPP_LEVEL_UNSET || opp_key.level == key.level);
+
+ if (freq_match && level_match) {
+ *opp = temp_opp;
+ return true;
+ }
+
+ return false;
+}
+
static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
unsigned long opp_key, unsigned long key)
{
@@ -541,6 +564,40 @@ static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table,
return opp;
}
+static struct dev_pm_opp *_opp_table_find_opp_key(struct opp_table *opp_table,
+ struct dev_pm_opp_key *key, int index, bool available,
+ unsigned long (*read)(struct dev_pm_opp *opp, int index,
+ struct dev_pm_opp_key *key),
+ bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
+ struct dev_pm_opp_key opp_key, struct dev_pm_opp_key key),
+ bool (*assert)(struct opp_table *opp_table, unsigned int index))
+{
+ struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
+ struct dev_pm_opp_key temp_key;
+
+ /* Assert that the requirement is met */
+ if (assert && !assert(opp_table, index))
+ return ERR_PTR(-EINVAL);
+
+ guard(mutex)(&opp_table->lock);
+
+ list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
+ if (temp_opp->available == available) {
+ read(temp_opp, index, &temp_key);
+ if (compare(&opp, temp_opp, temp_key, *key))
+ break;
+ }
+ }
+
+ /* Increment the reference count of OPP */
+ if (!IS_ERR(opp)) {
+ *key = temp_key;
+ dev_pm_opp_get(opp);
+ }
+
+ return opp;
+}
+
static struct dev_pm_opp *
_find_key(struct device *dev, unsigned long *key, int index, bool available,
unsigned long (*read)(struct dev_pm_opp *opp, int index),
@@ -632,6 +689,52 @@ struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
}
EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
+/**
+ * dev_pm_opp_find_freq_level_exact() - Search for an exact frequency and level
+ * @dev: Device for which the OPP is being searched
+ * @freq: Frequency to match (in Hz)
+ * @level: Performance level to match
+ * @available: true/false - match for available OPP
+ *
+ * Return: Searches for an exact match of frequency and performance level in the
+ * OPP table and returns pointer to the matching opp if found, else returns ERR_PTR
+ * in case of error and should be handled using IS_ERR. Error return values can be:
+ * EINVAL: for bad pointer
+ * ERANGE: no match found for search
+ * ENODEV: if device not found in list of registered devices
+ *
+ * Note: available is a modifier for the search. if available=true, then the
+ * match is for exact matching frequency and is available in the stored OPP
+ * table. if false, the match is for exact frequency which is not available.
+ *
+ * This provides a mechanism to enable an opp which is not available currently
+ * or the opposite as well.
+ *
+ * The callers are required to call dev_pm_opp_put() for the returned OPP after
+ * use.
+ */
+struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev,
+ unsigned long freq,
+ unsigned int level,
+ bool available)
+{
+ struct opp_table *opp_table __free(put_opp_table);
+ struct dev_pm_opp_key key;
+
+ opp_table = _find_opp_table(dev);
+ if (IS_ERR(opp_table)) {
+ dev_err(dev, "%s: OPP table not found (%ld)\n", __func__,
+ PTR_ERR(opp_table));
+ return ERR_CAST(opp_table);
+ }
+
+ key.freq = freq;
+ key.level = level;
+ return _opp_table_find_opp_key(opp_table, &key, 0, available, _read_opp_key,
+ _compare_opp_key_exact, assert_single_clk);
+}
+EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_level_exact);
+
/**
* dev_pm_opp_find_freq_exact_indexed() - Search for an exact freq for the
* clock corresponding to the index
diff --git a/drivers/opp/opp.h b/drivers/opp/opp.h
index 9eba63e01a9e7650cf2e49515b70ba73f72210fc..60bb0042e30f73a8e31c1688e0cb3842cdea09d5 100644
--- a/drivers/opp/opp.h
+++ b/drivers/opp/opp.h
@@ -249,6 +249,19 @@ struct opp_table {
#endif
};
+/**
+ * struct dev_pm_opp_key - Key used to identify OPP entries
+ * @freq: Frequency in Hz
+ * @level: Performance level associated with the frequency
+ *
+ * This is internal structure, used to help in mapping frequency
+ * and performance level combinations to specific OPP entries.
+ */
+struct dev_pm_opp_key {
+ unsigned long freq;
+ unsigned int level;
+};
+
/* Routines internal to opp core */
bool _opp_remove_all_static(struct opp_table *opp_table);
int _get_opp_count(struct opp_table *opp_table);
diff --git a/include/linux/pm_opp.h b/include/linux/pm_opp.h
index cf477beae4bbede88223566df5f43d85adc5a816..a93f2670da6c2f10aa7ac5ea020fc3ef14fef113 100644
--- a/include/linux/pm_opp.h
+++ b/include/linux/pm_opp.h
@@ -131,6 +131,11 @@ struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
unsigned long freq,
bool available);
+struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev,
+ unsigned long freq,
+ unsigned int level,
+ bool available);
+
struct dev_pm_opp *
dev_pm_opp_find_freq_exact_indexed(struct device *dev, unsigned long freq,
u32 index, bool available);
@@ -289,6 +294,14 @@ static inline struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
return ERR_PTR(-EOPNOTSUPP);
}
+struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev,
+ unsigned long freq,
+ unsigned int level,
+ bool available)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+
static inline struct dev_pm_opp *
dev_pm_opp_find_freq_exact_indexed(struct device *dev, unsigned long freq,
u32 index, bool available)
--
2.34.1
Hi Krishna, kernel test robot noticed the following build errors: [auto build test ERROR on c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9] url: https://github.com/intel-lab-lkp/linux/commits/Krishna-Chaitanya-Chundru/PM-OPP-Support-to-match-OPP-based-on-both-frequency-and-level/20250818-162759 base: c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9 patch link: https://lore.kernel.org/r/20250818-opp_pcie-v2-1-071524d98967%40oss.qualcomm.com patch subject: [PATCH v2 1/3] PM/OPP: Support to match OPP based on both frequency and level. config: parisc-allnoconfig (https://download.01.org/0day-ci/archive/20250819/202508190712.5L4VOrmr-lkp@intel.com/config) compiler: hppa-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250819/202508190712.5L4VOrmr-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202508190712.5L4VOrmr-lkp@intel.com/ All error/warnings (new ones prefixed by >>): In file included from include/linux/cpufreq.h:18, from include/trace/events/power.h:8, from kernel/cpu.c:42: >> include/linux/pm_opp.h:297:20: warning: no previous prototype for 'dev_pm_opp_find_freq_level_exact' [-Wmissing-prototypes] 297 | struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- hppa-linux-ld: kernel/sched/core.o: in function `dev_pm_opp_find_freq_level_exact': >> (.text+0xba4): multiple definition of `dev_pm_opp_find_freq_level_exact'; kernel/cpu.o:(.text+0x6a0): first defined here hppa-linux-ld: kernel/sched/fair.o: in function `dev_pm_opp_find_freq_level_exact': (.text+0x50ac): multiple definition of `dev_pm_opp_find_freq_level_exact'; kernel/cpu.o:(.text+0x6a0): first defined here hppa-linux-ld: kernel/sched/build_policy.o: in function `dev_pm_opp_find_freq_level_exact': (.text+0x4d44): multiple definition of `dev_pm_opp_find_freq_level_exact'; kernel/cpu.o:(.text+0x6a0): first defined here hppa-linux-ld: kernel/sched/build_utility.o: in function `dev_pm_opp_find_freq_level_exact': (.text+0x2b44): multiple definition of `dev_pm_opp_find_freq_level_exact'; kernel/cpu.o:(.text+0x6a0): first defined here hppa-linux-ld: kernel/power/qos.o: in function `dev_pm_opp_find_freq_level_exact': (.text+0x188): multiple definition of `dev_pm_opp_find_freq_level_exact'; kernel/cpu.o:(.text+0x6a0): first defined here hppa-linux-ld: kernel/time/tick-common.o: in function `dev_pm_opp_find_freq_level_exact': (.text+0x164): multiple definition of `dev_pm_opp_find_freq_level_exact'; kernel/cpu.o:(.text+0x6a0): first defined here hppa-linux-ld: fs/proc/cpuinfo.o: in function `dev_pm_opp_find_freq_level_exact': (.text+0x24): multiple definition of `dev_pm_opp_find_freq_level_exact'; kernel/cpu.o:(.text+0x6a0): first defined here hppa-linux-ld: drivers/base/core.o: in function `dev_pm_opp_find_freq_level_exact': (.text+0x5348): multiple definition of `dev_pm_opp_find_freq_level_exact'; kernel/cpu.o:(.text+0x6a0): first defined here hppa-linux-ld: drivers/base/syscore.o: in function `dev_pm_opp_find_freq_level_exact': (.text+0xb8): multiple definition of `dev_pm_opp_find_freq_level_exact'; kernel/cpu.o:(.text+0x6a0): first defined here -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Hi Krishna, kernel test robot noticed the following build warnings: [auto build test WARNING on c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9] url: https://github.com/intel-lab-lkp/linux/commits/Krishna-Chaitanya-Chundru/PM-OPP-Support-to-match-OPP-based-on-both-frequency-and-level/20250818-162759 base: c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9 patch link: https://lore.kernel.org/r/20250818-opp_pcie-v2-1-071524d98967%40oss.qualcomm.com patch subject: [PATCH v2 1/3] PM/OPP: Support to match OPP based on both frequency and level. config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20250819/202508190743.lszMYiro-lkp@intel.com/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250819/202508190743.lszMYiro-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202508190743.lszMYiro-lkp@intel.com/ All warnings (new ones prefixed by >>): In file included from kernel/sched/core.c:89: In file included from kernel/sched/sched.h:31: In file included from include/linux/cpufreq.h:18: >> include/linux/pm_opp.h:297:20: warning: no previous prototype for function 'dev_pm_opp_find_freq_level_exact' [-Wmissing-prototypes] 297 | struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev, | ^ include/linux/pm_opp.h:297:1: note: declare 'static' if the function is not intended to be used outside of this translation unit 297 | struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev, | ^ | static kernel/sched/core.c:2445:20: warning: unused function 'rq_has_pinned_tasks' [-Wunused-function] 2445 | static inline bool rq_has_pinned_tasks(struct rq *rq) | ^~~~~~~~~~~~~~~~~~~ kernel/sched/core.c:5795:20: warning: unused function 'sched_tick_stop' [-Wunused-function] 5795 | static inline void sched_tick_stop(int cpu) { } | ^~~~~~~~~~~~~~~ kernel/sched/core.c:6514:20: warning: unused function 'sched_core_cpu_deactivate' [-Wunused-function] 6514 | static inline void sched_core_cpu_deactivate(unsigned int cpu) {} | ^~~~~~~~~~~~~~~~~~~~~~~~~ kernel/sched/core.c:6515:20: warning: unused function 'sched_core_cpu_dying' [-Wunused-function] 6515 | static inline void sched_core_cpu_dying(unsigned int cpu) {} | ^~~~~~~~~~~~~~~~~~~~ kernel/sched/core.c:8305:20: warning: unused function 'balance_hotplug_wait' [-Wunused-function] 8305 | static inline void balance_hotplug_wait(void) | ^~~~~~~~~~~~~~~~~~~~ 6 warnings generated. -- In file included from kernel/sched/fair.c:57: In file included from kernel/sched/sched.h:31: In file included from include/linux/cpufreq.h:18: >> include/linux/pm_opp.h:297:20: warning: no previous prototype for function 'dev_pm_opp_find_freq_level_exact' [-Wmissing-prototypes] 297 | struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev, | ^ include/linux/pm_opp.h:297:1: note: declare 'static' if the function is not intended to be used outside of this translation unit 297 | struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev, | ^ | static kernel/sched/fair.c:482:20: warning: unused function 'list_del_leaf_cfs_rq' [-Wunused-function] 482 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | ^~~~~~~~~~~~~~~~~~~~ kernel/sched/fair.c:503:19: warning: unused function 'tg_is_idle' [-Wunused-function] 503 | static inline int tg_is_idle(struct task_group *tg) | ^~~~~~~~~~ kernel/sched/fair.c:3997:20: warning: unused function 'load_avg_is_decayed' [-Wunused-function] 3997 | static inline bool load_avg_is_decayed(struct sched_avg *sa) | ^~~~~~~~~~~~~~~~~~~ kernel/sched/fair.c:6640:20: warning: unused function 'sync_throttle' [-Wunused-function] 6640 | static inline void sync_throttle(struct task_group *tg, int cpu) {} | ^~~~~~~~~~~~~ kernel/sched/fair.c:6664:37: warning: unused function 'tg_cfs_bandwidth' [-Wunused-function] 6664 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) | ^~~~~~~~~~~~~~~~ kernel/sched/fair.c:6668:20: warning: unused function 'destroy_cfs_bandwidth' [-Wunused-function] 6668 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | ^~~~~~~~~~~~~~~~~~~~~ kernel/sched/fair.c:9991:20: warning: unused function 'check_misfit_status' [-Wunused-function] 9991 | static inline bool check_misfit_status(struct rq *rq) | ^~~~~~~~~~~~~~~~~~~ 8 warnings generated. -- In file included from kernel/sched/build_policy.c:41: In file included from kernel/sched/sched.h:31: In file included from include/linux/cpufreq.h:18: >> include/linux/pm_opp.h:297:20: warning: no previous prototype for function 'dev_pm_opp_find_freq_level_exact' [-Wmissing-prototypes] 297 | struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev, | ^ include/linux/pm_opp.h:297:1: note: declare 'static' if the function is not intended to be used outside of this translation unit 297 | struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev, | ^ | static 1 warning generated. vim +/dev_pm_opp_find_freq_level_exact +297 include/linux/pm_opp.h 296 > 297 struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev, 298 unsigned long freq, 299 unsigned int level, 300 bool available) 301 { 302 return ERR_PTR(-EOPNOTSUPP); 303 } 304 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
On 18-08-25, 13:52, Krishna Chaitanya Chundru wrote: > +static bool _compare_opp_key_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, > + struct dev_pm_opp_key opp_key, struct dev_pm_opp_key key) > +{ > + bool freq_match = (opp_key.freq == 0 || key.freq == 0 || opp_key.freq == key.freq); Why !opp_key.freq is okay ? If the user has provided a freq value, then it must match. Isn't it ? > + bool level_match = (opp_key.level == OPP_LEVEL_UNSET || > + key.level == OPP_LEVEL_UNSET || opp_key.level == key.level); We should compare bw too I guess in the same routine. > + if (freq_match && level_match) { > + *opp = temp_opp; > + return true; > + } > + > + return false; > +} > +/** > + * dev_pm_opp_find_freq_level_exact() - Search for an exact frequency and level Instead dev_pm_opp_find_key_exact() and let the user pass the key struct itself. > +struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev, > + unsigned long freq, > + unsigned int level, > + bool available) > +{ > + struct opp_table *opp_table __free(put_opp_table); The constructor here must be real, i.e. initialize opp_table here itself. This is well documented in cleanup.h. Yes there are examples like this in the OPP core which are required to be fixed too. -- viresh
On 8/18/2025 2:25 PM, Viresh Kumar wrote: > On 18-08-25, 13:52, Krishna Chaitanya Chundru wrote: >> +static bool _compare_opp_key_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp, >> + struct dev_pm_opp_key opp_key, struct dev_pm_opp_key key) >> +{ >> + bool freq_match = (opp_key.freq == 0 || key.freq == 0 || opp_key.freq == key.freq); > > Why !opp_key.freq is okay ? If the user has provided a freq value, > then it must match. Isn't it ? > ok I will fix this in next patch. >> + bool level_match = (opp_key.level == OPP_LEVEL_UNSET || >> + key.level == OPP_LEVEL_UNSET || opp_key.level == key.level); > > We should compare bw too I guess in the same routine. ok I will add bw similar to level, > >> + if (freq_match && level_match) { >> + *opp = temp_opp; >> + return true; >> + } >> + >> + return false; >> +} >> +/** >> + * dev_pm_opp_find_freq_level_exact() - Search for an exact frequency and level > > Instead dev_pm_opp_find_key_exact() and let the user pass the key > struct itself. > ack >> +struct dev_pm_opp *dev_pm_opp_find_freq_level_exact(struct device *dev, >> + unsigned long freq, >> + unsigned int level, >> + bool available) >> +{ >> + struct opp_table *opp_table __free(put_opp_table); > > The constructor here must be real, i.e. initialize opp_table here > itself. This is well documented in cleanup.h. Yes there are examples > like this in the OPP core which are required to be fixed too. ack. - Krishna Chaitanya. >
On 18-08-25, 14:25, Viresh Kumar wrote: Also subject should be: "OPP: Add support to find OPP for a set of keys" or something on those lines (I was more looking for the OPP: prefix). -- viresh
© 2016 - 2025 Red Hat, Inc.