/*1* Generic OPP Interface2*3* Copyright (C) 2009-2010 Texas Instruments Incorporated.4* Nishanth Menon5* Romit Dasgupta6* Kevin Hilman7*8* This program is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License version 2 as10* published by the Free Software Foundation.11*/1213#include <linux/kernel.h>14#include <linux/errno.h>15#include <linux/err.h>16#include <linux/init.h>17#include <linux/slab.h>18#include <linux/cpufreq.h>19#include <linux/list.h>20#include <linux/rculist.h>21#include <linux/rcupdate.h>22#include <linux/opp.h>2324/*25* Internal data structure organization with the OPP layer library is as26* follows:27* dev_opp_list (root)28* |- device 1 (represents voltage domain 1)29* | |- opp 1 (availability, freq, voltage)30* | |- opp 2 ..31* ... ...32* | `- opp n ..33* |- device 2 (represents the next voltage domain)34* ...35* `- device m (represents mth voltage domain)36* device 1, 2.. are represented by dev_opp structure while each opp37* is represented by the opp structure.38*/3940/**41* struct opp - Generic OPP description structure42* @node: opp list node. The nodes are maintained throughout the lifetime43* of boot. It is expected only an optimal set of OPPs are44* added to the library by the SoC framework.45* RCU usage: opp list is traversed with RCU locks. node46* modification is possible realtime, hence the modifications47* are protected by the dev_opp_list_lock for integrity.48* IMPORTANT: the opp nodes should be maintained in increasing49* order.50* @available: true/false - marks if this OPP as available or not51* @rate: Frequency in hertz52* @u_volt: Nominal voltage in microvolts corresponding to this OPP53* @dev_opp: points back to the device_opp struct this opp belongs to54*55* This structure stores the OPP information for a given device.56*/57struct opp {58struct list_head node;5960bool available;61unsigned long rate;62unsigned long u_volt;6364struct device_opp *dev_opp;65};6667/**68* struct device_opp - Device opp structure69* @node: list node - contains the devices with OPPs that70* have been registered. Nodes once added are not modified in this71* list.72* RCU usage: nodes are not modified in the list of device_opp,73* however addition is possible and is secured by dev_opp_list_lock74* @dev: device pointer75* @opp_list: list of opps76*77* This is an internal data structure maintaining the link to opps attached to78* a device. This structure is not meant to be shared to users as it is79* meant for book keeping and private to OPP library80*/81struct device_opp {82struct list_head node;8384struct device *dev;85struct list_head opp_list;86};8788/*89* The root of the list of all devices. All device_opp structures branch off90* from here, with each device_opp containing the list of opp it supports in91* various states of availability.92*/93static LIST_HEAD(dev_opp_list);94/* Lock to allow exclusive modification to the device and opp lists */95static DEFINE_MUTEX(dev_opp_list_lock);9697/**98* find_device_opp() - find device_opp struct using device pointer99* @dev: device pointer used to lookup device OPPs100*101* Search list of device OPPs for one containing matching device. Does a RCU102* reader operation to grab the pointer needed.103*104* Returns pointer to 'struct device_opp' if found, otherwise -ENODEV or105* -EINVAL based on type of error.106*107* Locking: This function must be called under rcu_read_lock(). device_opp108* is a RCU protected pointer. This means that device_opp is valid as long109* as we are under RCU lock.110*/111static struct device_opp *find_device_opp(struct device *dev)112{113struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);114115if (unlikely(IS_ERR_OR_NULL(dev))) {116pr_err("%s: Invalid parameters\n", __func__);117return ERR_PTR(-EINVAL);118}119120list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {121if (tmp_dev_opp->dev == dev) {122dev_opp = tmp_dev_opp;123break;124}125}126127return dev_opp;128}129130/**131* opp_get_voltage() - Gets the voltage corresponding to an available opp132* @opp: opp for which voltage has to be returned for133*134* Return voltage in micro volt corresponding to the opp, else135* return 0136*137* Locking: This function must be called under rcu_read_lock(). opp is a rcu138* protected pointer. This means that opp which could have been fetched by139* opp_find_freq_{exact,ceil,floor} functions is valid as long as we are140* under RCU lock. The pointer returned by the opp_find_freq family must be141* used in the same section as the usage of this function with the pointer142* prior to unlocking with rcu_read_unlock() to maintain the integrity of the143* pointer.144*/145unsigned long opp_get_voltage(struct opp *opp)146{147struct opp *tmp_opp;148unsigned long v = 0;149150tmp_opp = rcu_dereference(opp);151if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)152pr_err("%s: Invalid parameters\n", __func__);153else154v = tmp_opp->u_volt;155156return v;157}158159/**160* opp_get_freq() - Gets the frequency corresponding to an available opp161* @opp: opp for which frequency has to be returned for162*163* Return frequency in hertz corresponding to the opp, else164* return 0165*166* Locking: This function must be called under rcu_read_lock(). opp is a rcu167* protected pointer. This means that opp which could have been fetched by168* opp_find_freq_{exact,ceil,floor} functions is valid as long as we are169* under RCU lock. The pointer returned by the opp_find_freq family must be170* used in the same section as the usage of this function with the pointer171* prior to unlocking with rcu_read_unlock() to maintain the integrity of the172* pointer.173*/174unsigned long opp_get_freq(struct opp *opp)175{176struct opp *tmp_opp;177unsigned long f = 0;178179tmp_opp = rcu_dereference(opp);180if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)181pr_err("%s: Invalid parameters\n", __func__);182else183f = tmp_opp->rate;184185return f;186}187188/**189* opp_get_opp_count() - Get number of opps available in the opp list190* @dev: device for which we do this operation191*192* This function returns the number of available opps if there are any,193* else returns 0 if none or the corresponding error value.194*195* Locking: This function must be called under rcu_read_lock(). This function196* internally references two RCU protected structures: device_opp and opp which197* are safe as long as we are under a common RCU locked section.198*/199int opp_get_opp_count(struct device *dev)200{201struct device_opp *dev_opp;202struct opp *temp_opp;203int count = 0;204205dev_opp = find_device_opp(dev);206if (IS_ERR(dev_opp)) {207int r = PTR_ERR(dev_opp);208dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);209return r;210}211212list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {213if (temp_opp->available)214count++;215}216217return count;218}219220/**221* opp_find_freq_exact() - search for an exact frequency222* @dev: device for which we do this operation223* @freq: frequency to search for224* @available: true/false - match for available opp225*226* Searches for exact match in the opp list and returns pointer to the matching227* opp if found, else returns ERR_PTR in case of error and should be handled228* using IS_ERR.229*230* Note: available is a modifier for the search. if available=true, then the231* match is for exact matching frequency and is available in the stored OPP232* table. if false, the match is for exact frequency which is not available.233*234* This provides a mechanism to enable an opp which is not available currently235* or the opposite as well.236*237* Locking: This function must be called under rcu_read_lock(). opp is a rcu238* protected pointer. The reason for the same is that the opp pointer which is239* returned will remain valid for use with opp_get_{voltage, freq} only while240* under the locked area. The pointer returned must be used prior to unlocking241* with rcu_read_unlock() to maintain the integrity of the pointer.242*/243struct opp *opp_find_freq_exact(struct device *dev, unsigned long freq,244bool available)245{246struct device_opp *dev_opp;247struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);248249dev_opp = find_device_opp(dev);250if (IS_ERR(dev_opp)) {251int r = PTR_ERR(dev_opp);252dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);253return ERR_PTR(r);254}255256list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {257if (temp_opp->available == available &&258temp_opp->rate == freq) {259opp = temp_opp;260break;261}262}263264return opp;265}266267/**268* opp_find_freq_ceil() - Search for an rounded ceil freq269* @dev: device for which we do this operation270* @freq: Start frequency271*272* Search for the matching ceil *available* OPP from a starting freq273* for a device.274*275* Returns matching *opp and refreshes *freq accordingly, else returns276* ERR_PTR in case of error and should be handled using IS_ERR.277*278* Locking: This function must be called under rcu_read_lock(). opp is a rcu279* protected pointer. The reason for the same is that the opp pointer which is280* returned will remain valid for use with opp_get_{voltage, freq} only while281* under the locked area. The pointer returned must be used prior to unlocking282* with rcu_read_unlock() to maintain the integrity of the pointer.283*/284struct opp *opp_find_freq_ceil(struct device *dev, unsigned long *freq)285{286struct device_opp *dev_opp;287struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);288289if (!dev || !freq) {290dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);291return ERR_PTR(-EINVAL);292}293294dev_opp = find_device_opp(dev);295if (IS_ERR(dev_opp))296return opp;297298list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {299if (temp_opp->available && temp_opp->rate >= *freq) {300opp = temp_opp;301*freq = opp->rate;302break;303}304}305306return opp;307}308309/**310* opp_find_freq_floor() - Search for a rounded floor freq311* @dev: device for which we do this operation312* @freq: Start frequency313*314* Search for the matching floor *available* OPP from a starting freq315* for a device.316*317* Returns matching *opp and refreshes *freq accordingly, else returns318* ERR_PTR in case of error and should be handled using IS_ERR.319*320* Locking: This function must be called under rcu_read_lock(). opp is a rcu321* protected pointer. The reason for the same is that the opp pointer which is322* returned will remain valid for use with opp_get_{voltage, freq} only while323* under the locked area. The pointer returned must be used prior to unlocking324* with rcu_read_unlock() to maintain the integrity of the pointer.325*/326struct opp *opp_find_freq_floor(struct device *dev, unsigned long *freq)327{328struct device_opp *dev_opp;329struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);330331if (!dev || !freq) {332dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);333return ERR_PTR(-EINVAL);334}335336dev_opp = find_device_opp(dev);337if (IS_ERR(dev_opp))338return opp;339340list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {341if (temp_opp->available) {342/* go to the next node, before choosing prev */343if (temp_opp->rate > *freq)344break;345else346opp = temp_opp;347}348}349if (!IS_ERR(opp))350*freq = opp->rate;351352return opp;353}354355/**356* opp_add() - Add an OPP table from a table definitions357* @dev: device for which we do this operation358* @freq: Frequency in Hz for this OPP359* @u_volt: Voltage in uVolts for this OPP360*361* This function adds an opp definition to the opp list and returns status.362* The opp is made available by default and it can be controlled using363* opp_enable/disable functions.364*365* Locking: The internal device_opp and opp structures are RCU protected.366* Hence this function internally uses RCU updater strategy with mutex locks367* to keep the integrity of the internal data structures. Callers should ensure368* that this function is *NOT* called under RCU protection or in contexts where369* mutex cannot be locked.370*/371int opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)372{373struct device_opp *dev_opp = NULL;374struct opp *opp, *new_opp;375struct list_head *head;376377/* allocate new OPP node */378new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);379if (!new_opp) {380dev_warn(dev, "%s: Unable to create new OPP node\n", __func__);381return -ENOMEM;382}383384/* Hold our list modification lock here */385mutex_lock(&dev_opp_list_lock);386387/* Check for existing list for 'dev' */388dev_opp = find_device_opp(dev);389if (IS_ERR(dev_opp)) {390/*391* Allocate a new device OPP table. In the infrequent case392* where a new device is needed to be added, we pay this393* penalty.394*/395dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);396if (!dev_opp) {397mutex_unlock(&dev_opp_list_lock);398kfree(new_opp);399dev_warn(dev,400"%s: Unable to create device OPP structure\n",401__func__);402return -ENOMEM;403}404405dev_opp->dev = dev;406INIT_LIST_HEAD(&dev_opp->opp_list);407408/* Secure the device list modification */409list_add_rcu(&dev_opp->node, &dev_opp_list);410}411412/* populate the opp table */413new_opp->dev_opp = dev_opp;414new_opp->rate = freq;415new_opp->u_volt = u_volt;416new_opp->available = true;417418/* Insert new OPP in order of increasing frequency */419head = &dev_opp->opp_list;420list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {421if (new_opp->rate < opp->rate)422break;423else424head = &opp->node;425}426427list_add_rcu(&new_opp->node, head);428mutex_unlock(&dev_opp_list_lock);429430return 0;431}432433/**434* opp_set_availability() - helper to set the availability of an opp435* @dev: device for which we do this operation436* @freq: OPP frequency to modify availability437* @availability_req: availability status requested for this opp438*439* Set the availability of an OPP with an RCU operation, opp_{enable,disable}440* share a common logic which is isolated here.441*442* Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the443* copy operation, returns 0 if no modifcation was done OR modification was444* successful.445*446* Locking: The internal device_opp and opp structures are RCU protected.447* Hence this function internally uses RCU updater strategy with mutex locks to448* keep the integrity of the internal data structures. Callers should ensure449* that this function is *NOT* called under RCU protection or in contexts where450* mutex locking or synchronize_rcu() blocking calls cannot be used.451*/452static int opp_set_availability(struct device *dev, unsigned long freq,453bool availability_req)454{455struct device_opp *tmp_dev_opp, *dev_opp = NULL;456struct opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);457int r = 0;458459/* keep the node allocated */460new_opp = kmalloc(sizeof(struct opp), GFP_KERNEL);461if (!new_opp) {462dev_warn(dev, "%s: Unable to create OPP\n", __func__);463return -ENOMEM;464}465466mutex_lock(&dev_opp_list_lock);467468/* Find the device_opp */469list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {470if (dev == tmp_dev_opp->dev) {471dev_opp = tmp_dev_opp;472break;473}474}475if (IS_ERR(dev_opp)) {476r = PTR_ERR(dev_opp);477dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);478goto unlock;479}480481/* Do we have the frequency? */482list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {483if (tmp_opp->rate == freq) {484opp = tmp_opp;485break;486}487}488if (IS_ERR(opp)) {489r = PTR_ERR(opp);490goto unlock;491}492493/* Is update really needed? */494if (opp->available == availability_req)495goto unlock;496/* copy the old data over */497*new_opp = *opp;498499/* plug in new node */500new_opp->available = availability_req;501502list_replace_rcu(&opp->node, &new_opp->node);503mutex_unlock(&dev_opp_list_lock);504synchronize_rcu();505506/* clean up old opp */507new_opp = opp;508goto out;509510unlock:511mutex_unlock(&dev_opp_list_lock);512out:513kfree(new_opp);514return r;515}516517/**518* opp_enable() - Enable a specific OPP519* @dev: device for which we do this operation520* @freq: OPP frequency to enable521*522* Enables a provided opp. If the operation is valid, this returns 0, else the523* corresponding error value. It is meant to be used for users an OPP available524* after being temporarily made unavailable with opp_disable.525*526* Locking: The internal device_opp and opp structures are RCU protected.527* Hence this function indirectly uses RCU and mutex locks to keep the528* integrity of the internal data structures. Callers should ensure that529* this function is *NOT* called under RCU protection or in contexts where530* mutex locking or synchronize_rcu() blocking calls cannot be used.531*/532int opp_enable(struct device *dev, unsigned long freq)533{534return opp_set_availability(dev, freq, true);535}536537/**538* opp_disable() - Disable a specific OPP539* @dev: device for which we do this operation540* @freq: OPP frequency to disable541*542* Disables a provided opp. If the operation is valid, this returns543* 0, else the corresponding error value. It is meant to be a temporary544* control by users to make this OPP not available until the circumstances are545* right to make it available again (with a call to opp_enable).546*547* Locking: The internal device_opp and opp structures are RCU protected.548* Hence this function indirectly uses RCU and mutex locks to keep the549* integrity of the internal data structures. Callers should ensure that550* this function is *NOT* called under RCU protection or in contexts where551* mutex locking or synchronize_rcu() blocking calls cannot be used.552*/553int opp_disable(struct device *dev, unsigned long freq)554{555return opp_set_availability(dev, freq, false);556}557558#ifdef CONFIG_CPU_FREQ559/**560* opp_init_cpufreq_table() - create a cpufreq table for a device561* @dev: device for which we do this operation562* @table: Cpufreq table returned back to caller563*564* Generate a cpufreq table for a provided device- this assumes that the565* opp list is already initialized and ready for usage.566*567* This function allocates required memory for the cpufreq table. It is568* expected that the caller does the required maintenance such as freeing569* the table as required.570*571* Returns -EINVAL for bad pointers, -ENODEV if the device is not found, -ENOMEM572* if no memory available for the operation (table is not populated), returns 0573* if successful and table is populated.574*575* WARNING: It is important for the callers to ensure refreshing their copy of576* the table if any of the mentioned functions have been invoked in the interim.577*578* Locking: The internal device_opp and opp structures are RCU protected.579* To simplify the logic, we pretend we are updater and hold relevant mutex here580* Callers should ensure that this function is *NOT* called under RCU protection581* or in contexts where mutex locking cannot be used.582*/583int opp_init_cpufreq_table(struct device *dev,584struct cpufreq_frequency_table **table)585{586struct device_opp *dev_opp;587struct opp *opp;588struct cpufreq_frequency_table *freq_table;589int i = 0;590591/* Pretend as if I am an updater */592mutex_lock(&dev_opp_list_lock);593594dev_opp = find_device_opp(dev);595if (IS_ERR(dev_opp)) {596int r = PTR_ERR(dev_opp);597mutex_unlock(&dev_opp_list_lock);598dev_err(dev, "%s: Device OPP not found (%d)\n", __func__, r);599return r;600}601602freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *603(opp_get_opp_count(dev) + 1), GFP_KERNEL);604if (!freq_table) {605mutex_unlock(&dev_opp_list_lock);606dev_warn(dev, "%s: Unable to allocate frequency table\n",607__func__);608return -ENOMEM;609}610611list_for_each_entry(opp, &dev_opp->opp_list, node) {612if (opp->available) {613freq_table[i].index = i;614freq_table[i].frequency = opp->rate / 1000;615i++;616}617}618mutex_unlock(&dev_opp_list_lock);619620freq_table[i].index = i;621freq_table[i].frequency = CPUFREQ_TABLE_END;622623*table = &freq_table[0];624625return 0;626}627#endif /* CONFIG_CPU_FREQ */628629630