2015-09-04 11:17:26 +03:00
/*
* Generic OPP Interface
*
* Copyright ( C ) 2009 - 2010 Texas Instruments Incorporated .
* Nishanth Menon
* Romit Dasgupta
* Kevin Hilman
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation .
*/
# ifndef __DRIVER_OPP_H__
# define __DRIVER_OPP_H__
# include <linux/device.h>
# include <linux/kernel.h>
# include <linux/list.h>
2015-11-11 05:29:01 +03:00
# include <linux/limits.h>
2015-09-04 11:17:26 +03:00
# include <linux/pm_opp.h>
# include <linux/rculist.h>
# include <linux/rcupdate.h>
2016-02-09 08:00:38 +03:00
struct clk ;
2016-02-09 08:00:33 +03:00
struct regulator ;
PM / OPP: Protect updates to list_dev with mutex
dev_opp_list_lock is used everywhere to protect device and OPP lists,
but dev_pm_opp_set_sharing_cpus() is missed somehow. And instead we used
rcu-lock, which wouldn't help here as we are adding a new list_dev.
This also fixes a problem where we have called kzalloc(..., GFP_KERNEL)
from within rcu-lock, which isn't allowed as kzalloc can sleep when
called with GFP_KERNEL.
With CONFIG_DEBUG_ATOMIC_SLEEP set, we get following lockdep-splat:
include/linux/rcupdate.h:578 Illegal context switch in RCU read-side critical section!
other info that might help us debug this:
rcu_scheduler_active = 1, debug_locks = 0
5 locks held by swapper/0/1:
#0: (&dev->mutex){......}, at: [<c02f68f4>] __driver_attach+0x48/0x98
#1: (&dev->mutex){......}, at: [<c02f6904>] __driver_attach+0x58/0x98
#2: (cpu_hotplug.lock){++++++}, at: [<c00249d0>] get_online_cpus+0x40/0xb0
#3: (subsys mutex#5){+.+.+.}, at: [<c02f4f8c>] subsys_interface_register+0x44/0xdc
#4: (rcu_read_lock){......}, at: [<c0305c80>] dev_pm_opp_set_sharing_cpus+0x0/0x1e4
stack backtrace:
CPU: 1 PID: 1 Comm: swapper/0 Tainted: G W 4.3.0-rc7-00047-g81f5932958a8 #59
Hardware name: SAMSUNG EXYNOS (Flattened Device Tree)
[<c0016874>] (unwind_backtrace) from [<c001355c>] (show_stack+0x10/0x14)
[<c001355c>] (show_stack) from [<c022553c>] (dump_stack+0x94/0xbc)
[<c022553c>] (dump_stack) from [<c004904c>] (___might_sleep+0x24c/0x298)
[<c004904c>] (___might_sleep) from [<c00f07e4>] (kmem_cache_alloc+0xe8/0x164)
[<c00f07e4>] (kmem_cache_alloc) from [<c0305354>] (_add_list_dev+0x30/0x58)
[<c0305354>] (_add_list_dev) from [<c0305d50>] (dev_pm_opp_set_sharing_cpus+0xd0/0x1e4)
[<c0305d50>] (dev_pm_opp_set_sharing_cpus) from [<c040eda4>] (cpufreq_init+0x4cc/0x62c)
[<c040eda4>] (cpufreq_init) from [<c040a964>] (cpufreq_online+0xbc/0x73c)
[<c040a964>] (cpufreq_online) from [<c02f4fe0>] (subsys_interface_register+0x98/0xdc)
[<c02f4fe0>] (subsys_interface_register) from [<c040a640>] (cpufreq_register_driver+0x110/0x17c)
[<c040a640>] (cpufreq_register_driver) from [<c040ef64>] (dt_cpufreq_probe+0x60/0x8c)
[<c040ef64>] (dt_cpufreq_probe) from [<c02f8084>] (platform_drv_probe+0x44/0xa4)
[<c02f8084>] (platform_drv_probe) from [<c02f67c0>] (driver_probe_device+0x208/0x2f4)
[<c02f67c0>] (driver_probe_device) from [<c02f6940>] (__driver_attach+0x94/0x98)
[<c02f6940>] (__driver_attach) from [<c02f4c1c>] (bus_for_each_dev+0x68/0x9c)
Reported-by: Michael Turquette <mturquette@baylibre.com>
Reviewed-by: Stephen Boyd <sboyd@codeaurora.org>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Cc: 4.3 <stable@vger.kernel.org> # 4.3
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2015-11-05 11:51:19 +03:00
/* Lock to allow exclusive modification to the device and opp lists */
2016-02-16 11:47:53 +03:00
extern struct mutex opp_table_lock ;
PM / OPP: Protect updates to list_dev with mutex
dev_opp_list_lock is used everywhere to protect device and OPP lists,
but dev_pm_opp_set_sharing_cpus() is missed somehow. And instead we used
rcu-lock, which wouldn't help here as we are adding a new list_dev.
This also fixes a problem where we have called kzalloc(..., GFP_KERNEL)
from within rcu-lock, which isn't allowed as kzalloc can sleep when
called with GFP_KERNEL.
With CONFIG_DEBUG_ATOMIC_SLEEP set, we get following lockdep-splat:
include/linux/rcupdate.h:578 Illegal context switch in RCU read-side critical section!
other info that might help us debug this:
rcu_scheduler_active = 1, debug_locks = 0
5 locks held by swapper/0/1:
#0: (&dev->mutex){......}, at: [<c02f68f4>] __driver_attach+0x48/0x98
#1: (&dev->mutex){......}, at: [<c02f6904>] __driver_attach+0x58/0x98
#2: (cpu_hotplug.lock){++++++}, at: [<c00249d0>] get_online_cpus+0x40/0xb0
#3: (subsys mutex#5){+.+.+.}, at: [<c02f4f8c>] subsys_interface_register+0x44/0xdc
#4: (rcu_read_lock){......}, at: [<c0305c80>] dev_pm_opp_set_sharing_cpus+0x0/0x1e4
stack backtrace:
CPU: 1 PID: 1 Comm: swapper/0 Tainted: G W 4.3.0-rc7-00047-g81f5932958a8 #59
Hardware name: SAMSUNG EXYNOS (Flattened Device Tree)
[<c0016874>] (unwind_backtrace) from [<c001355c>] (show_stack+0x10/0x14)
[<c001355c>] (show_stack) from [<c022553c>] (dump_stack+0x94/0xbc)
[<c022553c>] (dump_stack) from [<c004904c>] (___might_sleep+0x24c/0x298)
[<c004904c>] (___might_sleep) from [<c00f07e4>] (kmem_cache_alloc+0xe8/0x164)
[<c00f07e4>] (kmem_cache_alloc) from [<c0305354>] (_add_list_dev+0x30/0x58)
[<c0305354>] (_add_list_dev) from [<c0305d50>] (dev_pm_opp_set_sharing_cpus+0xd0/0x1e4)
[<c0305d50>] (dev_pm_opp_set_sharing_cpus) from [<c040eda4>] (cpufreq_init+0x4cc/0x62c)
[<c040eda4>] (cpufreq_init) from [<c040a964>] (cpufreq_online+0xbc/0x73c)
[<c040a964>] (cpufreq_online) from [<c02f4fe0>] (subsys_interface_register+0x98/0xdc)
[<c02f4fe0>] (subsys_interface_register) from [<c040a640>] (cpufreq_register_driver+0x110/0x17c)
[<c040a640>] (cpufreq_register_driver) from [<c040ef64>] (dt_cpufreq_probe+0x60/0x8c)
[<c040ef64>] (dt_cpufreq_probe) from [<c02f8084>] (platform_drv_probe+0x44/0xa4)
[<c02f8084>] (platform_drv_probe) from [<c02f67c0>] (driver_probe_device+0x208/0x2f4)
[<c02f67c0>] (driver_probe_device) from [<c02f6940>] (__driver_attach+0x94/0x98)
[<c02f6940>] (__driver_attach) from [<c02f4c1c>] (bus_for_each_dev+0x68/0x9c)
Reported-by: Michael Turquette <mturquette@baylibre.com>
Reviewed-by: Stephen Boyd <sboyd@codeaurora.org>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Cc: 4.3 <stable@vger.kernel.org> # 4.3
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2015-11-05 11:51:19 +03:00
2016-05-05 13:50:33 +03:00
extern struct list_head opp_tables ;
2015-09-04 11:17:26 +03:00
/*
* Internal data structure organization with the OPP layer library is as
* follows :
2016-02-16 11:47:53 +03:00
* opp_tables ( root )
2015-09-04 11:17:26 +03:00
* | - device 1 ( represents voltage domain 1 )
* | | - opp 1 ( availability , freq , voltage )
* | | - opp 2 . .
* . . . . . .
* | ` - opp n . .
* | - device 2 ( represents the next voltage domain )
* . . .
* ` - device m ( represents mth voltage domain )
2016-02-16 11:47:53 +03:00
* device 1 , 2. . are represented by opp_table structure while each opp
2015-09-04 11:17:26 +03:00
* is represented by the opp structure .
*/
/**
* struct dev_pm_opp - Generic OPP description structure
2016-02-16 11:47:53 +03:00
* @ node : opp table node . The nodes are maintained throughout the lifetime
2015-09-04 11:17:26 +03:00
* of boot . It is expected only an optimal set of OPPs are
* added to the library by the SoC framework .
2016-02-16 11:47:53 +03:00
* RCU usage : opp table is traversed with RCU locks . node
2015-09-04 11:17:26 +03:00
* modification is possible realtime , hence the modifications
2016-02-16 11:47:53 +03:00
* are protected by the opp_table_lock for integrity .
2015-09-04 11:17:26 +03:00
* IMPORTANT : the opp nodes should be maintained in increasing
* order .
* @ available : true / false - marks if this OPP as available or not
2015-11-19 06:43:56 +03:00
* @ dynamic : not - created from static DT entries .
2015-09-04 11:17:26 +03:00
* @ turbo : true if turbo ( boost ) OPP
2015-11-11 05:29:01 +03:00
* @ suspend : true if suspend OPP
2015-09-04 11:17:26 +03:00
* @ rate : Frequency in hertz
2016-12-01 13:58:17 +03:00
* @ supply : Power supply voltage / current values
2015-09-04 11:17:26 +03:00
* @ clock_latency_ns : Latency ( in nanoseconds ) of switching to this OPP ' s
* frequency from any other OPP ' s frequency .
2016-02-16 11:47:53 +03:00
* @ opp_table : points back to the opp_table struct this opp belongs to
2015-09-04 11:17:26 +03:00
* @ rcu_head : RCU callback head used for deferred freeing
* @ np : OPP ' s device node .
2015-11-11 05:29:01 +03:00
* @ dentry : debugfs dentry pointer ( per opp )
2015-09-04 11:17:26 +03:00
*
* This structure stores the OPP information for a given device .
*/
struct dev_pm_opp {
struct list_head node ;
bool available ;
bool dynamic ;
bool turbo ;
2015-11-11 05:29:01 +03:00
bool suspend ;
2015-09-04 11:17:26 +03:00
unsigned long rate ;
2016-12-01 13:58:17 +03:00
struct dev_pm_opp_supply supply ;
2015-09-04 11:17:26 +03:00
unsigned long clock_latency_ns ;
2016-02-16 11:47:53 +03:00
struct opp_table * opp_table ;
2015-09-04 11:17:26 +03:00
struct rcu_head rcu_head ;
struct device_node * np ;
2015-11-11 05:29:01 +03:00
# ifdef CONFIG_DEBUG_FS
struct dentry * dentry ;
# endif
2015-09-04 11:17:26 +03:00
} ;
/**
2016-02-16 11:47:53 +03:00
* struct opp_device - devices managed by ' struct opp_table '
2015-09-04 11:17:26 +03:00
* @ node : list node
* @ dev : device to which the struct object belongs
* @ rcu_head : RCU callback head used for deferred freeing
2015-11-11 05:29:01 +03:00
* @ dentry : debugfs dentry pointer ( per device )
2015-09-04 11:17:26 +03:00
*
2016-02-16 11:47:53 +03:00
* This is an internal data structure maintaining the devices that are managed
* by ' struct opp_table ' .
2015-09-04 11:17:26 +03:00
*/
2016-02-16 11:47:53 +03:00
struct opp_device {
2015-09-04 11:17:26 +03:00
struct list_head node ;
const struct device * dev ;
struct rcu_head rcu_head ;
2015-11-11 05:29:01 +03:00
# ifdef CONFIG_DEBUG_FS
struct dentry * dentry ;
# endif
2015-09-04 11:17:26 +03:00
} ;
2016-06-16 16:33:11 +03:00
enum opp_table_access {
OPP_TABLE_ACCESS_UNKNOWN = 0 ,
OPP_TABLE_ACCESS_EXCLUSIVE = 1 ,
OPP_TABLE_ACCESS_SHARED = 2 ,
} ;
2015-09-04 11:17:26 +03:00
/**
2016-02-16 11:47:53 +03:00
* struct opp_table - Device opp structure
* @ node : table node - contains the devices with OPPs that
2015-09-04 11:17:26 +03:00
* have been registered . Nodes once added are not modified in this
2016-02-16 11:47:53 +03:00
* table .
* RCU usage : nodes are not modified in the table of opp_table ,
* however addition is possible and is secured by opp_table_lock
2015-09-04 11:17:26 +03:00
* @ srcu_head : notifier head to notify the OPP availability changes .
* @ rcu_head : RCU callback head used for deferred freeing
* @ dev_list : list of devices that share these OPPs
2016-02-16 11:47:53 +03:00
* @ opp_list : table of opps
2015-09-04 11:17:26 +03:00
* @ np : struct device_node pointer for opp ' s DT node .
2015-11-19 06:43:56 +03:00
* @ clock_latency_ns_max : Max clock latency in nanoseconds .
2015-09-04 11:17:26 +03:00
* @ shared_opp : OPP is shared between multiple devices .
2015-11-19 06:43:56 +03:00
* @ suspend_opp : Pointer to OPP to be used during device suspend .
2015-12-09 05:31:46 +03:00
* @ supported_hw : Array of version number to support .
* @ supported_hw_count : Number of elements in supported_hw array .
2015-12-09 05:31:47 +03:00
* @ prop_name : A name to postfix to many DT properties , while parsing them .
2016-02-09 08:00:38 +03:00
* @ clk : Device ' s clock handle
2016-02-09 08:00:33 +03:00
* @ regulator : Supply regulator
2015-11-11 05:29:01 +03:00
* @ dentry : debugfs dentry pointer of the real device directory ( not links ) .
* @ dentry_name : Name of the real dentry .
2015-09-04 11:17:26 +03:00
*
2016-02-09 08:00:37 +03:00
* @ voltage_tolerance_v1 : In percentage , for v1 bindings only .
*
2015-09-04 11:17:26 +03:00
* This is an internal data structure maintaining the link to opps attached to
* a device . This structure is not meant to be shared to users as it is
* meant for book keeping and private to OPP library .
*
* Because the opp structures can be used from both rcu and srcu readers , we
* need to wait for the grace period of both of them before freeing any
* resources . And so we have used kfree_rcu ( ) from within call_srcu ( ) handlers .
*/
2016-02-16 11:47:53 +03:00
struct opp_table {
2015-09-04 11:17:26 +03:00
struct list_head node ;
struct srcu_notifier_head srcu_head ;
struct rcu_head rcu_head ;
struct list_head dev_list ;
struct list_head opp_list ;
struct device_node * np ;
unsigned long clock_latency_ns_max ;
2016-02-09 08:00:37 +03:00
/* For backward compatibility with v1 bindings */
unsigned int voltage_tolerance_v1 ;
2016-06-16 16:33:11 +03:00
enum opp_table_access shared_opp ;
2015-09-04 11:17:26 +03:00
struct dev_pm_opp * suspend_opp ;
2015-11-11 05:29:01 +03:00
2015-12-09 05:31:46 +03:00
unsigned int * supported_hw ;
unsigned int supported_hw_count ;
2015-12-09 05:31:47 +03:00
const char * prop_name ;
2016-02-09 08:00:38 +03:00
struct clk * clk ;
2016-02-09 08:00:33 +03:00
struct regulator * regulator ;
2015-12-09 05:31:46 +03:00
2015-11-11 05:29:01 +03:00
# ifdef CONFIG_DEBUG_FS
struct dentry * dentry ;
char dentry_name [ NAME_MAX ] ;
# endif
2015-09-04 11:17:26 +03:00
} ;
/* Routines internal to opp core */
2016-02-16 11:47:53 +03:00
struct opp_table * _find_opp_table ( struct device * dev ) ;
struct opp_device * _add_opp_dev ( const struct device * dev , struct opp_table * opp_table ) ;
2016-05-05 13:50:33 +03:00
void _dev_pm_opp_remove_table ( struct device * dev , bool remove_all ) ;
struct dev_pm_opp * _allocate_opp ( struct device * dev , struct opp_table * * opp_table ) ;
int _opp_add ( struct device * dev , struct dev_pm_opp * new_opp , struct opp_table * opp_table ) ;
void _opp_remove ( struct opp_table * opp_table , struct dev_pm_opp * opp , bool notify ) ;
int _opp_add_v1 ( struct device * dev , unsigned long freq , long u_volt , bool dynamic ) ;
void _dev_pm_opp_cpumask_remove_table ( const struct cpumask * cpumask , bool of ) ;
# ifdef CONFIG_OF
void _of_init_opp_table ( struct opp_table * opp_table , struct device * dev ) ;
# else
static inline void _of_init_opp_table ( struct opp_table * opp_table , struct device * dev ) { }
# endif
2015-09-04 11:17:26 +03:00
2015-11-11 05:29:01 +03:00
# ifdef CONFIG_DEBUG_FS
void opp_debug_remove_one ( struct dev_pm_opp * opp ) ;
2016-02-16 11:47:53 +03:00
int opp_debug_create_one ( struct dev_pm_opp * opp , struct opp_table * opp_table ) ;
int opp_debug_register ( struct opp_device * opp_dev , struct opp_table * opp_table ) ;
void opp_debug_unregister ( struct opp_device * opp_dev , struct opp_table * opp_table ) ;
2015-11-11 05:29:01 +03:00
# else
static inline void opp_debug_remove_one ( struct dev_pm_opp * opp ) { }
static inline int opp_debug_create_one ( struct dev_pm_opp * opp ,
2016-02-16 11:47:53 +03:00
struct opp_table * opp_table )
2015-11-11 05:29:01 +03:00
{ return 0 ; }
2016-02-16 11:47:53 +03:00
static inline int opp_debug_register ( struct opp_device * opp_dev ,
struct opp_table * opp_table )
2015-11-11 05:29:01 +03:00
{ return 0 ; }
2016-02-16 11:47:53 +03:00
static inline void opp_debug_unregister ( struct opp_device * opp_dev ,
struct opp_table * opp_table )
2015-11-11 05:29:01 +03:00
{ }
# endif /* DEBUG_FS */
2015-09-04 11:17:26 +03:00
# endif /* __DRIVER_OPP_H__ */