opp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /*
  2. * Generic OPP Interface
  3. *
  4. * Copyright (C) 2009-2010 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. */
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/device.h>
  18. #include <linux/list.h>
  19. #include <linux/rculist.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/pm_opp.h>
  22. #include <linux/of.h>
  23. #include <linux/export.h>
  24. /*
  25. * Internal data structure organization with the OPP layer library is as
  26. * 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 opp
  37. * is represented by the opp structure.
  38. */
  39. /**
  40. * struct dev_pm_opp - Generic OPP description structure
  41. * @node: opp list node. The nodes are maintained throughout the lifetime
  42. * of boot. It is expected only an optimal set of OPPs are
  43. * added to the library by the SoC framework.
  44. * RCU usage: opp list is traversed with RCU locks. node
  45. * modification is possible realtime, hence the modifications
  46. * are protected by the dev_opp_list_lock for integrity.
  47. * IMPORTANT: the opp nodes should be maintained in increasing
  48. * order.
  49. * @dynamic: not-created from static DT entries.
  50. * @available: true/false - marks if this OPP as available or not
  51. * @rate: Frequency in hertz
  52. * @u_volt: Nominal voltage in microvolts corresponding to this OPP
  53. * @dev_opp: points back to the device_opp struct this opp belongs to
  54. * @rcu_head: RCU callback head used for deferred freeing
  55. *
  56. * This structure stores the OPP information for a given device.
  57. */
  58. struct dev_pm_opp {
  59. struct list_head node;
  60. bool available;
  61. bool dynamic;
  62. unsigned long rate;
  63. unsigned long u_volt;
  64. struct device_opp *dev_opp;
  65. struct rcu_head rcu_head;
  66. };
  67. /**
  68. * struct device_opp - Device opp structure
  69. * @node: list node - contains the devices with OPPs that
  70. * have been registered. Nodes once added are not modified in this
  71. * 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_lock
  74. * @dev: device pointer
  75. * @srcu_head: notifier head to notify the OPP availability changes.
  76. * @rcu_head: RCU callback head used for deferred freeing
  77. * @opp_list: list of opps
  78. *
  79. * This is an internal data structure maintaining the link to opps attached to
  80. * a device. This structure is not meant to be shared to users as it is
  81. * meant for book keeping and private to OPP library.
  82. *
  83. * Because the opp structures can be used from both rcu and srcu readers, we
  84. * need to wait for the grace period of both of them before freeing any
  85. * resources. And so we have used kfree_rcu() from within call_srcu() handlers.
  86. */
  87. struct device_opp {
  88. struct list_head node;
  89. struct device *dev;
  90. struct srcu_notifier_head srcu_head;
  91. struct rcu_head rcu_head;
  92. struct list_head opp_list;
  93. };
  94. /*
  95. * The root of the list of all devices. All device_opp structures branch off
  96. * from here, with each device_opp containing the list of opp it supports in
  97. * various states of availability.
  98. */
  99. static LIST_HEAD(dev_opp_list);
  100. /* Lock to allow exclusive modification to the device and opp lists */
  101. static DEFINE_MUTEX(dev_opp_list_lock);
  102. #define opp_rcu_lockdep_assert() \
  103. do { \
  104. rcu_lockdep_assert(rcu_read_lock_held() || \
  105. lockdep_is_held(&dev_opp_list_lock), \
  106. "Missing rcu_read_lock() or " \
  107. "dev_opp_list_lock protection"); \
  108. } while (0)
  109. /**
  110. * _find_device_opp() - find device_opp struct using device pointer
  111. * @dev: device pointer used to lookup device OPPs
  112. *
  113. * Search list of device OPPs for one containing matching device. Does a RCU
  114. * reader operation to grab the pointer needed.
  115. *
  116. * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or
  117. * -EINVAL based on type of error.
  118. *
  119. * Locking: This function must be called under rcu_read_lock(). device_opp
  120. * is a RCU protected pointer. This means that device_opp is valid as long
  121. * as we are under RCU lock.
  122. */
  123. static struct device_opp *_find_device_opp(struct device *dev)
  124. {
  125. struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
  126. if (unlikely(IS_ERR_OR_NULL(dev))) {
  127. pr_err("%s: Invalid parameters\n", __func__);
  128. return ERR_PTR(-EINVAL);
  129. }
  130. list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {
  131. if (tmp_dev_opp->dev == dev) {
  132. dev_opp = tmp_dev_opp;
  133. break;
  134. }
  135. }
  136. return dev_opp;
  137. }
  138. /**
  139. * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp
  140. * @opp: opp for which voltage has to be returned for
  141. *
  142. * Return: voltage in micro volt corresponding to the opp, else
  143. * return 0
  144. *
  145. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  146. * protected pointer. This means that opp which could have been fetched by
  147. * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
  148. * under RCU lock. The pointer returned by the opp_find_freq family must be
  149. * used in the same section as the usage of this function with the pointer
  150. * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
  151. * pointer.
  152. */
  153. unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
  154. {
  155. struct dev_pm_opp *tmp_opp;
  156. unsigned long v = 0;
  157. opp_rcu_lockdep_assert();
  158. tmp_opp = rcu_dereference(opp);
  159. if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
  160. pr_err("%s: Invalid parameters\n", __func__);
  161. else
  162. v = tmp_opp->u_volt;
  163. return v;
  164. }
  165. EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
  166. /**
  167. * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
  168. * @opp: opp for which frequency has to be returned for
  169. *
  170. * Return: frequency in hertz corresponding to the opp, else
  171. * return 0
  172. *
  173. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  174. * protected pointer. This means that opp which could have been fetched by
  175. * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
  176. * under RCU lock. The pointer returned by the opp_find_freq family must be
  177. * used in the same section as the usage of this function with the pointer
  178. * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
  179. * pointer.
  180. */
  181. unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
  182. {
  183. struct dev_pm_opp *tmp_opp;
  184. unsigned long f = 0;
  185. opp_rcu_lockdep_assert();
  186. tmp_opp = rcu_dereference(opp);
  187. if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
  188. pr_err("%s: Invalid parameters\n", __func__);
  189. else
  190. f = tmp_opp->rate;
  191. return f;
  192. }
  193. EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
  194. /**
  195. * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
  196. * @dev: device for which we do this operation
  197. *
  198. * Return: This function returns the number of available opps if there are any,
  199. * else returns 0 if none or the corresponding error value.
  200. *
  201. * Locking: This function takes rcu_read_lock().
  202. */
  203. int dev_pm_opp_get_opp_count(struct device *dev)
  204. {
  205. struct device_opp *dev_opp;
  206. struct dev_pm_opp *temp_opp;
  207. int count = 0;
  208. rcu_read_lock();
  209. dev_opp = _find_device_opp(dev);
  210. if (IS_ERR(dev_opp)) {
  211. count = PTR_ERR(dev_opp);
  212. dev_err(dev, "%s: device OPP not found (%d)\n",
  213. __func__, count);
  214. goto out_unlock;
  215. }
  216. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  217. if (temp_opp->available)
  218. count++;
  219. }
  220. out_unlock:
  221. rcu_read_unlock();
  222. return count;
  223. }
  224. EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
  225. /**
  226. * dev_pm_opp_find_freq_exact() - search for an exact frequency
  227. * @dev: device for which we do this operation
  228. * @freq: frequency to search for
  229. * @available: true/false - match for available opp
  230. *
  231. * Return: Searches for exact match in the opp list and returns pointer to the
  232. * matching opp if found, else returns ERR_PTR in case of error and should
  233. * be handled using IS_ERR. Error return values can be:
  234. * EINVAL: for bad pointer
  235. * ERANGE: no match found for search
  236. * ENODEV: if device not found in list of registered devices
  237. *
  238. * Note: available is a modifier for the search. if available=true, then the
  239. * match is for exact matching frequency and is available in the stored OPP
  240. * table. if false, the match is for exact frequency which is not available.
  241. *
  242. * This provides a mechanism to enable an opp which is not available currently
  243. * or the opposite as well.
  244. *
  245. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  246. * protected pointer. The reason for the same is that the opp pointer which is
  247. * returned will remain valid for use with opp_get_{voltage, freq} only while
  248. * under the locked area. The pointer returned must be used prior to unlocking
  249. * with rcu_read_unlock() to maintain the integrity of the pointer.
  250. */
  251. struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
  252. unsigned long freq,
  253. bool available)
  254. {
  255. struct device_opp *dev_opp;
  256. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  257. opp_rcu_lockdep_assert();
  258. dev_opp = _find_device_opp(dev);
  259. if (IS_ERR(dev_opp)) {
  260. int r = PTR_ERR(dev_opp);
  261. dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
  262. return ERR_PTR(r);
  263. }
  264. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  265. if (temp_opp->available == available &&
  266. temp_opp->rate == freq) {
  267. opp = temp_opp;
  268. break;
  269. }
  270. }
  271. return opp;
  272. }
  273. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
  274. /**
  275. * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
  276. * @dev: device for which we do this operation
  277. * @freq: Start frequency
  278. *
  279. * Search for the matching ceil *available* OPP from a starting freq
  280. * for a device.
  281. *
  282. * Return: matching *opp and refreshes *freq accordingly, else returns
  283. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  284. * values can be:
  285. * EINVAL: for bad pointer
  286. * ERANGE: no match found for search
  287. * ENODEV: if device not found in list of registered devices
  288. *
  289. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  290. * protected pointer. The reason for the same is that the opp pointer which is
  291. * returned will remain valid for use with opp_get_{voltage, freq} only while
  292. * under the locked area. The pointer returned must be used prior to unlocking
  293. * with rcu_read_unlock() to maintain the integrity of the pointer.
  294. */
  295. struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
  296. unsigned long *freq)
  297. {
  298. struct device_opp *dev_opp;
  299. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  300. opp_rcu_lockdep_assert();
  301. if (!dev || !freq) {
  302. dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
  303. return ERR_PTR(-EINVAL);
  304. }
  305. dev_opp = _find_device_opp(dev);
  306. if (IS_ERR(dev_opp))
  307. return ERR_CAST(dev_opp);
  308. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  309. if (temp_opp->available && temp_opp->rate >= *freq) {
  310. opp = temp_opp;
  311. *freq = opp->rate;
  312. break;
  313. }
  314. }
  315. return opp;
  316. }
  317. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
  318. /**
  319. * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
  320. * @dev: device for which we do this operation
  321. * @freq: Start frequency
  322. *
  323. * Search for the matching floor *available* OPP from a starting freq
  324. * for a device.
  325. *
  326. * Return: matching *opp and refreshes *freq accordingly, else returns
  327. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  328. * values can be:
  329. * EINVAL: for bad pointer
  330. * ERANGE: no match found for search
  331. * ENODEV: if device not found in list of registered devices
  332. *
  333. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  334. * protected pointer. The reason for the same is that the opp pointer which is
  335. * returned will remain valid for use with opp_get_{voltage, freq} only while
  336. * under the locked area. The pointer returned must be used prior to unlocking
  337. * with rcu_read_unlock() to maintain the integrity of the pointer.
  338. */
  339. struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
  340. unsigned long *freq)
  341. {
  342. struct device_opp *dev_opp;
  343. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  344. opp_rcu_lockdep_assert();
  345. if (!dev || !freq) {
  346. dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
  347. return ERR_PTR(-EINVAL);
  348. }
  349. dev_opp = _find_device_opp(dev);
  350. if (IS_ERR(dev_opp))
  351. return ERR_CAST(dev_opp);
  352. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  353. if (temp_opp->available) {
  354. /* go to the next node, before choosing prev */
  355. if (temp_opp->rate > *freq)
  356. break;
  357. else
  358. opp = temp_opp;
  359. }
  360. }
  361. if (!IS_ERR(opp))
  362. *freq = opp->rate;
  363. return opp;
  364. }
  365. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
  366. /**
  367. * _add_device_opp() - Allocate a new device OPP table
  368. * @dev: device for which we do this operation
  369. *
  370. * New device node which uses OPPs - used when multiple devices with OPP tables
  371. * are maintained.
  372. *
  373. * Return: valid device_opp pointer if success, else NULL.
  374. */
  375. static struct device_opp *_add_device_opp(struct device *dev)
  376. {
  377. struct device_opp *dev_opp;
  378. /*
  379. * Allocate a new device OPP table. In the infrequent case where a new
  380. * device is needed to be added, we pay this penalty.
  381. */
  382. dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
  383. if (!dev_opp)
  384. return NULL;
  385. dev_opp->dev = dev;
  386. srcu_init_notifier_head(&dev_opp->srcu_head);
  387. INIT_LIST_HEAD(&dev_opp->opp_list);
  388. /* Secure the device list modification */
  389. list_add_rcu(&dev_opp->node, &dev_opp_list);
  390. return dev_opp;
  391. }
  392. /**
  393. * _opp_add_dynamic() - Allocate a dynamic OPP.
  394. * @dev: device for which we do this operation
  395. * @freq: Frequency in Hz for this OPP
  396. * @u_volt: Voltage in uVolts for this OPP
  397. * @dynamic: Dynamically added OPPs.
  398. *
  399. * This function adds an opp definition to the opp list and returns status.
  400. * The opp is made available by default and it can be controlled using
  401. * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
  402. *
  403. * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and
  404. * freed by of_free_opp_table.
  405. *
  406. * Locking: The internal device_opp and opp structures are RCU protected.
  407. * Hence this function internally uses RCU updater strategy with mutex locks
  408. * to keep the integrity of the internal data structures. Callers should ensure
  409. * that this function is *NOT* called under RCU protection or in contexts where
  410. * mutex cannot be locked.
  411. *
  412. * Return:
  413. * 0 On success OR
  414. * Duplicate OPPs (both freq and volt are same) and opp->available
  415. * -EEXIST Freq are same and volt are different OR
  416. * Duplicate OPPs (both freq and volt are same) and !opp->available
  417. * -ENOMEM Memory allocation failure
  418. */
  419. static int _opp_add_dynamic(struct device *dev, unsigned long freq,
  420. long u_volt, bool dynamic)
  421. {
  422. struct device_opp *dev_opp = NULL;
  423. struct dev_pm_opp *opp, *new_opp;
  424. struct list_head *head;
  425. int ret;
  426. /* allocate new OPP node */
  427. new_opp = kzalloc(sizeof(*new_opp), GFP_KERNEL);
  428. if (!new_opp)
  429. return -ENOMEM;
  430. /* Hold our list modification lock here */
  431. mutex_lock(&dev_opp_list_lock);
  432. /* populate the opp table */
  433. new_opp->rate = freq;
  434. new_opp->u_volt = u_volt;
  435. new_opp->available = true;
  436. new_opp->dynamic = dynamic;
  437. /* Check for existing list for 'dev' */
  438. dev_opp = _find_device_opp(dev);
  439. if (IS_ERR(dev_opp)) {
  440. dev_opp = _add_device_opp(dev);
  441. if (!dev_opp) {
  442. ret = -ENOMEM;
  443. goto free_opp;
  444. }
  445. head = &dev_opp->opp_list;
  446. goto list_add;
  447. }
  448. /*
  449. * Insert new OPP in order of increasing frequency
  450. * and discard if already present
  451. */
  452. head = &dev_opp->opp_list;
  453. list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
  454. if (new_opp->rate <= opp->rate)
  455. break;
  456. else
  457. head = &opp->node;
  458. }
  459. /* Duplicate OPPs ? */
  460. if (new_opp->rate == opp->rate) {
  461. ret = opp->available && new_opp->u_volt == opp->u_volt ?
  462. 0 : -EEXIST;
  463. dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
  464. __func__, opp->rate, opp->u_volt, opp->available,
  465. new_opp->rate, new_opp->u_volt, new_opp->available);
  466. goto free_opp;
  467. }
  468. list_add:
  469. new_opp->dev_opp = dev_opp;
  470. list_add_rcu(&new_opp->node, head);
  471. mutex_unlock(&dev_opp_list_lock);
  472. /*
  473. * Notify the changes in the availability of the operable
  474. * frequency/voltage list.
  475. */
  476. srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
  477. return 0;
  478. free_opp:
  479. mutex_unlock(&dev_opp_list_lock);
  480. kfree(new_opp);
  481. return ret;
  482. }
  483. /**
  484. * dev_pm_opp_add() - Add an OPP table from a table definitions
  485. * @dev: device for which we do this operation
  486. * @freq: Frequency in Hz for this OPP
  487. * @u_volt: Voltage in uVolts for this OPP
  488. *
  489. * This function adds an opp definition to the opp list and returns status.
  490. * The opp is made available by default and it can be controlled using
  491. * dev_pm_opp_enable/disable functions.
  492. *
  493. * Locking: The internal device_opp and opp structures are RCU protected.
  494. * Hence this function internally uses RCU updater strategy with mutex locks
  495. * to keep the integrity of the internal data structures. Callers should ensure
  496. * that this function is *NOT* called under RCU protection or in contexts where
  497. * mutex cannot be locked.
  498. *
  499. * Return:
  500. * 0 On success OR
  501. * Duplicate OPPs (both freq and volt are same) and opp->available
  502. * -EEXIST Freq are same and volt are different OR
  503. * Duplicate OPPs (both freq and volt are same) and !opp->available
  504. * -ENOMEM Memory allocation failure
  505. */
  506. int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
  507. {
  508. return _opp_add_dynamic(dev, freq, u_volt, true);
  509. }
  510. EXPORT_SYMBOL_GPL(dev_pm_opp_add);
  511. /**
  512. * _kfree_opp_rcu() - Free OPP RCU handler
  513. * @head: RCU head
  514. */
  515. static void _kfree_opp_rcu(struct rcu_head *head)
  516. {
  517. struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
  518. kfree_rcu(opp, rcu_head);
  519. }
  520. /**
  521. * _kfree_device_rcu() - Free device_opp RCU handler
  522. * @head: RCU head
  523. */
  524. static void _kfree_device_rcu(struct rcu_head *head)
  525. {
  526. struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
  527. kfree_rcu(device_opp, rcu_head);
  528. }
  529. /**
  530. * _opp_remove() - Remove an OPP from a table definition
  531. * @dev_opp: points back to the device_opp struct this opp belongs to
  532. * @opp: pointer to the OPP to remove
  533. *
  534. * This function removes an opp definition from the opp list.
  535. *
  536. * Locking: The internal device_opp and opp structures are RCU protected.
  537. * It is assumed that the caller holds required mutex for an RCU updater
  538. * strategy.
  539. */
  540. static void _opp_remove(struct device_opp *dev_opp,
  541. struct dev_pm_opp *opp)
  542. {
  543. /*
  544. * Notify the changes in the availability of the operable
  545. * frequency/voltage list.
  546. */
  547. srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
  548. list_del_rcu(&opp->node);
  549. call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
  550. if (list_empty(&dev_opp->opp_list)) {
  551. list_del_rcu(&dev_opp->node);
  552. call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
  553. _kfree_device_rcu);
  554. }
  555. }
  556. /**
  557. * dev_pm_opp_remove() - Remove an OPP from OPP list
  558. * @dev: device for which we do this operation
  559. * @freq: OPP to remove with matching 'freq'
  560. *
  561. * This function removes an opp from the opp list.
  562. *
  563. * Locking: The internal device_opp and opp structures are RCU protected.
  564. * Hence this function internally uses RCU updater strategy with mutex locks
  565. * to keep the integrity of the internal data structures. Callers should ensure
  566. * that this function is *NOT* called under RCU protection or in contexts where
  567. * mutex cannot be locked.
  568. */
  569. void dev_pm_opp_remove(struct device *dev, unsigned long freq)
  570. {
  571. struct dev_pm_opp *opp;
  572. struct device_opp *dev_opp;
  573. bool found = false;
  574. /* Hold our list modification lock here */
  575. mutex_lock(&dev_opp_list_lock);
  576. dev_opp = _find_device_opp(dev);
  577. if (IS_ERR(dev_opp))
  578. goto unlock;
  579. list_for_each_entry(opp, &dev_opp->opp_list, node) {
  580. if (opp->rate == freq) {
  581. found = true;
  582. break;
  583. }
  584. }
  585. if (!found) {
  586. dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
  587. __func__, freq);
  588. goto unlock;
  589. }
  590. _opp_remove(dev_opp, opp);
  591. unlock:
  592. mutex_unlock(&dev_opp_list_lock);
  593. }
  594. EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
  595. /**
  596. * _opp_set_availability() - helper to set the availability of an opp
  597. * @dev: device for which we do this operation
  598. * @freq: OPP frequency to modify availability
  599. * @availability_req: availability status requested for this opp
  600. *
  601. * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
  602. * share a common logic which is isolated here.
  603. *
  604. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  605. * copy operation, returns 0 if no modifcation was done OR modification was
  606. * successful.
  607. *
  608. * Locking: The internal device_opp and opp structures are RCU protected.
  609. * Hence this function internally uses RCU updater strategy with mutex locks to
  610. * keep the integrity of the internal data structures. Callers should ensure
  611. * that this function is *NOT* called under RCU protection or in contexts where
  612. * mutex locking or synchronize_rcu() blocking calls cannot be used.
  613. */
  614. static int _opp_set_availability(struct device *dev, unsigned long freq,
  615. bool availability_req)
  616. {
  617. struct device_opp *dev_opp;
  618. struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
  619. int r = 0;
  620. /* keep the node allocated */
  621. new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
  622. if (!new_opp)
  623. return -ENOMEM;
  624. mutex_lock(&dev_opp_list_lock);
  625. /* Find the device_opp */
  626. dev_opp = _find_device_opp(dev);
  627. if (IS_ERR(dev_opp)) {
  628. r = PTR_ERR(dev_opp);
  629. dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
  630. goto unlock;
  631. }
  632. /* Do we have the frequency? */
  633. list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
  634. if (tmp_opp->rate == freq) {
  635. opp = tmp_opp;
  636. break;
  637. }
  638. }
  639. if (IS_ERR(opp)) {
  640. r = PTR_ERR(opp);
  641. goto unlock;
  642. }
  643. /* Is update really needed? */
  644. if (opp->available == availability_req)
  645. goto unlock;
  646. /* copy the old data over */
  647. *new_opp = *opp;
  648. /* plug in new node */
  649. new_opp->available = availability_req;
  650. list_replace_rcu(&opp->node, &new_opp->node);
  651. mutex_unlock(&dev_opp_list_lock);
  652. call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
  653. /* Notify the change of the OPP availability */
  654. if (availability_req)
  655. srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
  656. new_opp);
  657. else
  658. srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
  659. new_opp);
  660. return 0;
  661. unlock:
  662. mutex_unlock(&dev_opp_list_lock);
  663. kfree(new_opp);
  664. return r;
  665. }
  666. /**
  667. * dev_pm_opp_enable() - Enable a specific OPP
  668. * @dev: device for which we do this operation
  669. * @freq: OPP frequency to enable
  670. *
  671. * Enables a provided opp. If the operation is valid, this returns 0, else the
  672. * corresponding error value. It is meant to be used for users an OPP available
  673. * after being temporarily made unavailable with dev_pm_opp_disable.
  674. *
  675. * Locking: The internal device_opp and opp structures are RCU protected.
  676. * Hence this function indirectly uses RCU and mutex locks to keep the
  677. * integrity of the internal data structures. Callers should ensure that
  678. * this function is *NOT* called under RCU protection or in contexts where
  679. * mutex locking or synchronize_rcu() blocking calls cannot be used.
  680. *
  681. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  682. * copy operation, returns 0 if no modifcation was done OR modification was
  683. * successful.
  684. */
  685. int dev_pm_opp_enable(struct device *dev, unsigned long freq)
  686. {
  687. return _opp_set_availability(dev, freq, true);
  688. }
  689. EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
  690. /**
  691. * dev_pm_opp_disable() - Disable a specific OPP
  692. * @dev: device for which we do this operation
  693. * @freq: OPP frequency to disable
  694. *
  695. * Disables a provided opp. If the operation is valid, this returns
  696. * 0, else the corresponding error value. It is meant to be a temporary
  697. * control by users to make this OPP not available until the circumstances are
  698. * right to make it available again (with a call to dev_pm_opp_enable).
  699. *
  700. * Locking: The internal device_opp and opp structures are RCU protected.
  701. * Hence this function indirectly uses RCU and mutex locks to keep the
  702. * integrity of the internal data structures. Callers should ensure that
  703. * this function is *NOT* called under RCU protection or in contexts where
  704. * mutex locking or synchronize_rcu() blocking calls cannot be used.
  705. *
  706. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  707. * copy operation, returns 0 if no modifcation was done OR modification was
  708. * successful.
  709. */
  710. int dev_pm_opp_disable(struct device *dev, unsigned long freq)
  711. {
  712. return _opp_set_availability(dev, freq, false);
  713. }
  714. EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
  715. /**
  716. * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
  717. * @dev: device pointer used to lookup device OPPs.
  718. *
  719. * Return: pointer to notifier head if found, otherwise -ENODEV or
  720. * -EINVAL based on type of error casted as pointer. value must be checked
  721. * with IS_ERR to determine valid pointer or error result.
  722. *
  723. * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
  724. * protected pointer. The reason for the same is that the opp pointer which is
  725. * returned will remain valid for use with opp_get_{voltage, freq} only while
  726. * under the locked area. The pointer returned must be used prior to unlocking
  727. * with rcu_read_unlock() to maintain the integrity of the pointer.
  728. */
  729. struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
  730. {
  731. struct device_opp *dev_opp = _find_device_opp(dev);
  732. if (IS_ERR(dev_opp))
  733. return ERR_CAST(dev_opp); /* matching type */
  734. return &dev_opp->srcu_head;
  735. }
  736. EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
  737. #ifdef CONFIG_OF
  738. /**
  739. * of_init_opp_table() - Initialize opp table from device tree
  740. * @dev: device pointer used to lookup device OPPs.
  741. *
  742. * Register the initial OPP table with the OPP library for given device.
  743. *
  744. * Locking: The internal device_opp and opp structures are RCU protected.
  745. * Hence this function indirectly uses RCU updater strategy with mutex locks
  746. * to keep the integrity of the internal data structures. Callers should ensure
  747. * that this function is *NOT* called under RCU protection or in contexts where
  748. * mutex cannot be locked.
  749. *
  750. * Return:
  751. * 0 On success OR
  752. * Duplicate OPPs (both freq and volt are same) and opp->available
  753. * -EEXIST Freq are same and volt are different OR
  754. * Duplicate OPPs (both freq and volt are same) and !opp->available
  755. * -ENOMEM Memory allocation failure
  756. * -ENODEV when 'operating-points' property is not found or is invalid data
  757. * in device node.
  758. * -ENODATA when empty 'operating-points' property is found
  759. */
  760. int of_init_opp_table(struct device *dev)
  761. {
  762. const struct property *prop;
  763. const __be32 *val;
  764. int nr;
  765. prop = of_find_property(dev->of_node, "operating-points", NULL);
  766. if (!prop)
  767. return -ENODEV;
  768. if (!prop->value)
  769. return -ENODATA;
  770. /*
  771. * Each OPP is a set of tuples consisting of frequency and
  772. * voltage like <freq-kHz vol-uV>.
  773. */
  774. nr = prop->length / sizeof(u32);
  775. if (nr % 2) {
  776. dev_err(dev, "%s: Invalid OPP list\n", __func__);
  777. return -EINVAL;
  778. }
  779. val = prop->value;
  780. while (nr) {
  781. unsigned long freq = be32_to_cpup(val++) * 1000;
  782. unsigned long volt = be32_to_cpup(val++);
  783. if (_opp_add_dynamic(dev, freq, volt, false))
  784. dev_warn(dev, "%s: Failed to add OPP %ld\n",
  785. __func__, freq);
  786. nr -= 2;
  787. }
  788. return 0;
  789. }
  790. EXPORT_SYMBOL_GPL(of_init_opp_table);
  791. /**
  792. * of_free_opp_table() - Free OPP table entries created from static DT entries
  793. * @dev: device pointer used to lookup device OPPs.
  794. *
  795. * Free OPPs created using static entries present in DT.
  796. *
  797. * Locking: The internal device_opp and opp structures are RCU protected.
  798. * Hence this function indirectly uses RCU updater strategy with mutex locks
  799. * to keep the integrity of the internal data structures. Callers should ensure
  800. * that this function is *NOT* called under RCU protection or in contexts where
  801. * mutex cannot be locked.
  802. */
  803. void of_free_opp_table(struct device *dev)
  804. {
  805. struct device_opp *dev_opp;
  806. struct dev_pm_opp *opp, *tmp;
  807. /* Check for existing list for 'dev' */
  808. dev_opp = _find_device_opp(dev);
  809. if (IS_ERR(dev_opp)) {
  810. int error = PTR_ERR(dev_opp);
  811. if (error != -ENODEV)
  812. WARN(1, "%s: dev_opp: %d\n",
  813. IS_ERR_OR_NULL(dev) ?
  814. "Invalid device" : dev_name(dev),
  815. error);
  816. return;
  817. }
  818. /* Hold our list modification lock here */
  819. mutex_lock(&dev_opp_list_lock);
  820. /* Free static OPPs */
  821. list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
  822. if (!opp->dynamic)
  823. _opp_remove(dev_opp, opp);
  824. }
  825. mutex_unlock(&dev_opp_list_lock);
  826. }
  827. EXPORT_SYMBOL_GPL(of_free_opp_table);
  828. #endif