opp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. * Returns 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. tmp_opp = rcu_dereference(opp);
  158. if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
  159. pr_err("%s: Invalid parameters\n", __func__);
  160. else
  161. v = tmp_opp->u_volt;
  162. return v;
  163. }
  164. EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
  165. /**
  166. * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
  167. * @opp: opp for which frequency has to be returned for
  168. *
  169. * Return frequency in hertz corresponding to the opp, else
  170. * return 0
  171. *
  172. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  173. * protected pointer. This means that opp which could have been fetched by
  174. * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
  175. * under RCU lock. The pointer returned by the opp_find_freq family must be
  176. * used in the same section as the usage of this function with the pointer
  177. * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
  178. * pointer.
  179. */
  180. unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
  181. {
  182. struct dev_pm_opp *tmp_opp;
  183. unsigned long f = 0;
  184. tmp_opp = rcu_dereference(opp);
  185. if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
  186. pr_err("%s: Invalid parameters\n", __func__);
  187. else
  188. f = tmp_opp->rate;
  189. return f;
  190. }
  191. EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
  192. /**
  193. * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
  194. * @dev: device for which we do this operation
  195. *
  196. * This function returns the number of available opps if there are any,
  197. * else returns 0 if none or the corresponding error value.
  198. *
  199. * Locking: This function takes rcu_read_lock().
  200. */
  201. int dev_pm_opp_get_opp_count(struct device *dev)
  202. {
  203. struct device_opp *dev_opp;
  204. struct dev_pm_opp *temp_opp;
  205. int count = 0;
  206. rcu_read_lock();
  207. dev_opp = find_device_opp(dev);
  208. if (IS_ERR(dev_opp)) {
  209. count = PTR_ERR(dev_opp);
  210. dev_err(dev, "%s: device OPP not found (%d)\n",
  211. __func__, count);
  212. goto out_unlock;
  213. }
  214. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  215. if (temp_opp->available)
  216. count++;
  217. }
  218. out_unlock:
  219. rcu_read_unlock();
  220. return count;
  221. }
  222. EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
  223. /**
  224. * dev_pm_opp_find_freq_exact() - search for an exact frequency
  225. * @dev: device for which we do this operation
  226. * @freq: frequency to search for
  227. * @available: true/false - match for available opp
  228. *
  229. * Searches for exact match in the opp list and returns pointer to the matching
  230. * opp if found, else returns ERR_PTR in case of error and should be handled
  231. * using IS_ERR. Error return values can be:
  232. * EINVAL: for bad pointer
  233. * ERANGE: no match found for search
  234. * ENODEV: if device not found in list of registered devices
  235. *
  236. * Note: available is a modifier for the search. if available=true, then the
  237. * match is for exact matching frequency and is available in the stored OPP
  238. * table. if false, the match is for exact frequency which is not available.
  239. *
  240. * This provides a mechanism to enable an opp which is not available currently
  241. * or the opposite as well.
  242. *
  243. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  244. * protected pointer. The reason for the same is that the opp pointer which is
  245. * returned will remain valid for use with opp_get_{voltage, freq} only while
  246. * under the locked area. The pointer returned must be used prior to unlocking
  247. * with rcu_read_unlock() to maintain the integrity of the pointer.
  248. */
  249. struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
  250. unsigned long freq,
  251. bool available)
  252. {
  253. struct device_opp *dev_opp;
  254. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  255. opp_rcu_lockdep_assert();
  256. dev_opp = find_device_opp(dev);
  257. if (IS_ERR(dev_opp)) {
  258. int r = PTR_ERR(dev_opp);
  259. dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
  260. return ERR_PTR(r);
  261. }
  262. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  263. if (temp_opp->available == available &&
  264. temp_opp->rate == freq) {
  265. opp = temp_opp;
  266. break;
  267. }
  268. }
  269. return opp;
  270. }
  271. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
  272. /**
  273. * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
  274. * @dev: device for which we do this operation
  275. * @freq: Start frequency
  276. *
  277. * Search for the matching ceil *available* OPP from a starting freq
  278. * for a device.
  279. *
  280. * Returns matching *opp and refreshes *freq accordingly, else returns
  281. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  282. * values can be:
  283. * EINVAL: for bad pointer
  284. * ERANGE: no match found for search
  285. * ENODEV: if device not found in list of registered devices
  286. *
  287. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  288. * protected pointer. The reason for the same is that the opp pointer which is
  289. * returned will remain valid for use with opp_get_{voltage, freq} only while
  290. * under the locked area. The pointer returned must be used prior to unlocking
  291. * with rcu_read_unlock() to maintain the integrity of the pointer.
  292. */
  293. struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
  294. unsigned long *freq)
  295. {
  296. struct device_opp *dev_opp;
  297. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  298. opp_rcu_lockdep_assert();
  299. if (!dev || !freq) {
  300. dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
  301. return ERR_PTR(-EINVAL);
  302. }
  303. dev_opp = find_device_opp(dev);
  304. if (IS_ERR(dev_opp))
  305. return ERR_CAST(dev_opp);
  306. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  307. if (temp_opp->available && temp_opp->rate >= *freq) {
  308. opp = temp_opp;
  309. *freq = opp->rate;
  310. break;
  311. }
  312. }
  313. return opp;
  314. }
  315. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
  316. /**
  317. * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
  318. * @dev: device for which we do this operation
  319. * @freq: Start frequency
  320. *
  321. * Search for the matching floor *available* OPP from a starting freq
  322. * for a device.
  323. *
  324. * Returns matching *opp and refreshes *freq accordingly, else returns
  325. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  326. * values can be:
  327. * EINVAL: for bad pointer
  328. * ERANGE: no match found for search
  329. * ENODEV: if device not found in list of registered devices
  330. *
  331. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  332. * protected pointer. The reason for the same is that the opp pointer which is
  333. * returned will remain valid for use with opp_get_{voltage, freq} only while
  334. * under the locked area. The pointer returned must be used prior to unlocking
  335. * with rcu_read_unlock() to maintain the integrity of the pointer.
  336. */
  337. struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
  338. unsigned long *freq)
  339. {
  340. struct device_opp *dev_opp;
  341. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  342. opp_rcu_lockdep_assert();
  343. if (!dev || !freq) {
  344. dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
  345. return ERR_PTR(-EINVAL);
  346. }
  347. dev_opp = find_device_opp(dev);
  348. if (IS_ERR(dev_opp))
  349. return ERR_CAST(dev_opp);
  350. list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
  351. if (temp_opp->available) {
  352. /* go to the next node, before choosing prev */
  353. if (temp_opp->rate > *freq)
  354. break;
  355. else
  356. opp = temp_opp;
  357. }
  358. }
  359. if (!IS_ERR(opp))
  360. *freq = opp->rate;
  361. return opp;
  362. }
  363. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
  364. static struct device_opp *add_device_opp(struct device *dev)
  365. {
  366. struct device_opp *dev_opp;
  367. /*
  368. * Allocate a new device OPP table. In the infrequent case where a new
  369. * device is needed to be added, we pay this penalty.
  370. */
  371. dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
  372. if (!dev_opp)
  373. return NULL;
  374. dev_opp->dev = dev;
  375. srcu_init_notifier_head(&dev_opp->srcu_head);
  376. INIT_LIST_HEAD(&dev_opp->opp_list);
  377. /* Secure the device list modification */
  378. list_add_rcu(&dev_opp->node, &dev_opp_list);
  379. return dev_opp;
  380. }
  381. static int dev_pm_opp_add_dynamic(struct device *dev, unsigned long freq,
  382. unsigned long u_volt, bool dynamic)
  383. {
  384. struct device_opp *dev_opp = NULL;
  385. struct dev_pm_opp *opp, *new_opp;
  386. struct list_head *head;
  387. int ret;
  388. /* allocate new OPP node */
  389. new_opp = kzalloc(sizeof(*new_opp), GFP_KERNEL);
  390. if (!new_opp) {
  391. dev_warn(dev, "%s: Unable to create new OPP node\n", __func__);
  392. return -ENOMEM;
  393. }
  394. /* Hold our list modification lock here */
  395. mutex_lock(&dev_opp_list_lock);
  396. /* populate the opp table */
  397. new_opp->rate = freq;
  398. new_opp->u_volt = u_volt;
  399. new_opp->available = true;
  400. new_opp->dynamic = dynamic;
  401. /* Check for existing list for 'dev' */
  402. dev_opp = find_device_opp(dev);
  403. if (IS_ERR(dev_opp)) {
  404. dev_opp = add_device_opp(dev);
  405. if (!dev_opp) {
  406. ret = -ENOMEM;
  407. goto free_opp;
  408. }
  409. head = &dev_opp->opp_list;
  410. goto list_add;
  411. }
  412. /*
  413. * Insert new OPP in order of increasing frequency
  414. * and discard if already present
  415. */
  416. head = &dev_opp->opp_list;
  417. list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
  418. if (new_opp->rate <= opp->rate)
  419. break;
  420. else
  421. head = &opp->node;
  422. }
  423. /* Duplicate OPPs ? */
  424. if (new_opp->rate == opp->rate) {
  425. ret = opp->available && new_opp->u_volt == opp->u_volt ?
  426. 0 : -EEXIST;
  427. dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
  428. __func__, opp->rate, opp->u_volt, opp->available,
  429. new_opp->rate, new_opp->u_volt, new_opp->available);
  430. goto free_opp;
  431. }
  432. list_add:
  433. new_opp->dev_opp = dev_opp;
  434. list_add_rcu(&new_opp->node, head);
  435. mutex_unlock(&dev_opp_list_lock);
  436. /*
  437. * Notify the changes in the availability of the operable
  438. * frequency/voltage list.
  439. */
  440. srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
  441. return 0;
  442. free_opp:
  443. mutex_unlock(&dev_opp_list_lock);
  444. kfree(new_opp);
  445. return ret;
  446. }
  447. /**
  448. * dev_pm_opp_add() - Add an OPP table from a table definitions
  449. * @dev: device for which we do this operation
  450. * @freq: Frequency in Hz for this OPP
  451. * @u_volt: Voltage in uVolts for this OPP
  452. *
  453. * This function adds an opp definition to the opp list and returns status.
  454. * The opp is made available by default and it can be controlled using
  455. * dev_pm_opp_enable/disable functions.
  456. *
  457. * Locking: The internal device_opp and opp structures are RCU protected.
  458. * Hence this function internally uses RCU updater strategy with mutex locks
  459. * to keep the integrity of the internal data structures. Callers should ensure
  460. * that this function is *NOT* called under RCU protection or in contexts where
  461. * mutex cannot be locked.
  462. *
  463. * Return:
  464. * 0: On success OR
  465. * Duplicate OPPs (both freq and volt are same) and opp->available
  466. * -EEXIST: Freq are same and volt are different OR
  467. * Duplicate OPPs (both freq and volt are same) and !opp->available
  468. * -ENOMEM: Memory allocation failure
  469. */
  470. int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
  471. {
  472. return dev_pm_opp_add_dynamic(dev, freq, u_volt, true);
  473. }
  474. EXPORT_SYMBOL_GPL(dev_pm_opp_add);
  475. static void kfree_opp_rcu(struct rcu_head *head)
  476. {
  477. struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
  478. kfree_rcu(opp, rcu_head);
  479. }
  480. static void kfree_device_rcu(struct rcu_head *head)
  481. {
  482. struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
  483. kfree_rcu(device_opp, rcu_head);
  484. }
  485. static void __dev_pm_opp_remove(struct device_opp *dev_opp,
  486. struct dev_pm_opp *opp)
  487. {
  488. /*
  489. * Notify the changes in the availability of the operable
  490. * frequency/voltage list.
  491. */
  492. srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
  493. list_del_rcu(&opp->node);
  494. call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, kfree_opp_rcu);
  495. if (list_empty(&dev_opp->opp_list)) {
  496. list_del_rcu(&dev_opp->node);
  497. call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
  498. kfree_device_rcu);
  499. }
  500. }
  501. /**
  502. * dev_pm_opp_remove() - Remove an OPP from OPP list
  503. * @dev: device for which we do this operation
  504. * @freq: OPP to remove with matching 'freq'
  505. *
  506. * This function removes an opp from the opp list.
  507. */
  508. void dev_pm_opp_remove(struct device *dev, unsigned long freq)
  509. {
  510. struct dev_pm_opp *opp;
  511. struct device_opp *dev_opp;
  512. bool found = false;
  513. /* Hold our list modification lock here */
  514. mutex_lock(&dev_opp_list_lock);
  515. dev_opp = find_device_opp(dev);
  516. if (IS_ERR(dev_opp))
  517. goto unlock;
  518. list_for_each_entry(opp, &dev_opp->opp_list, node) {
  519. if (opp->rate == freq) {
  520. found = true;
  521. break;
  522. }
  523. }
  524. if (!found) {
  525. dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
  526. __func__, freq);
  527. goto unlock;
  528. }
  529. __dev_pm_opp_remove(dev_opp, opp);
  530. unlock:
  531. mutex_unlock(&dev_opp_list_lock);
  532. }
  533. EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
  534. /**
  535. * opp_set_availability() - helper to set the availability of an opp
  536. * @dev: device for which we do this operation
  537. * @freq: OPP frequency to modify availability
  538. * @availability_req: availability status requested for this opp
  539. *
  540. * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
  541. * share a common logic which is isolated here.
  542. *
  543. * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
  544. * copy operation, returns 0 if no modifcation was done OR modification was
  545. * successful.
  546. *
  547. * Locking: The internal device_opp and opp structures are RCU protected.
  548. * Hence this function internally uses RCU updater strategy with mutex locks to
  549. * keep the integrity of the internal data structures. Callers should ensure
  550. * that this function is *NOT* called under RCU protection or in contexts where
  551. * mutex locking or synchronize_rcu() blocking calls cannot be used.
  552. */
  553. static int opp_set_availability(struct device *dev, unsigned long freq,
  554. bool availability_req)
  555. {
  556. struct device_opp *dev_opp;
  557. struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
  558. int r = 0;
  559. /* keep the node allocated */
  560. new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
  561. if (!new_opp) {
  562. dev_warn(dev, "%s: Unable to create OPP\n", __func__);
  563. return -ENOMEM;
  564. }
  565. mutex_lock(&dev_opp_list_lock);
  566. /* Find the device_opp */
  567. dev_opp = find_device_opp(dev);
  568. if (IS_ERR(dev_opp)) {
  569. r = PTR_ERR(dev_opp);
  570. dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
  571. goto unlock;
  572. }
  573. /* Do we have the frequency? */
  574. list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
  575. if (tmp_opp->rate == freq) {
  576. opp = tmp_opp;
  577. break;
  578. }
  579. }
  580. if (IS_ERR(opp)) {
  581. r = PTR_ERR(opp);
  582. goto unlock;
  583. }
  584. /* Is update really needed? */
  585. if (opp->available == availability_req)
  586. goto unlock;
  587. /* copy the old data over */
  588. *new_opp = *opp;
  589. /* plug in new node */
  590. new_opp->available = availability_req;
  591. list_replace_rcu(&opp->node, &new_opp->node);
  592. mutex_unlock(&dev_opp_list_lock);
  593. call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, kfree_opp_rcu);
  594. /* Notify the change of the OPP availability */
  595. if (availability_req)
  596. srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
  597. new_opp);
  598. else
  599. srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
  600. new_opp);
  601. return 0;
  602. unlock:
  603. mutex_unlock(&dev_opp_list_lock);
  604. kfree(new_opp);
  605. return r;
  606. }
  607. /**
  608. * dev_pm_opp_enable() - Enable a specific OPP
  609. * @dev: device for which we do this operation
  610. * @freq: OPP frequency to enable
  611. *
  612. * Enables a provided opp. If the operation is valid, this returns 0, else the
  613. * corresponding error value. It is meant to be used for users an OPP available
  614. * after being temporarily made unavailable with dev_pm_opp_disable.
  615. *
  616. * Locking: The internal device_opp and opp structures are RCU protected.
  617. * Hence this function indirectly uses RCU and mutex locks to keep the
  618. * integrity of the internal data structures. Callers should ensure that
  619. * this function is *NOT* called under RCU protection or in contexts where
  620. * mutex locking or synchronize_rcu() blocking calls cannot be used.
  621. */
  622. int dev_pm_opp_enable(struct device *dev, unsigned long freq)
  623. {
  624. return opp_set_availability(dev, freq, true);
  625. }
  626. EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
  627. /**
  628. * dev_pm_opp_disable() - Disable a specific OPP
  629. * @dev: device for which we do this operation
  630. * @freq: OPP frequency to disable
  631. *
  632. * Disables a provided opp. If the operation is valid, this returns
  633. * 0, else the corresponding error value. It is meant to be a temporary
  634. * control by users to make this OPP not available until the circumstances are
  635. * right to make it available again (with a call to dev_pm_opp_enable).
  636. *
  637. * Locking: The internal device_opp and opp structures are RCU protected.
  638. * Hence this function indirectly uses RCU and mutex locks to keep the
  639. * integrity of the internal data structures. Callers should ensure that
  640. * this function is *NOT* called under RCU protection or in contexts where
  641. * mutex locking or synchronize_rcu() blocking calls cannot be used.
  642. */
  643. int dev_pm_opp_disable(struct device *dev, unsigned long freq)
  644. {
  645. return opp_set_availability(dev, freq, false);
  646. }
  647. EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
  648. /**
  649. * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
  650. * @dev: device pointer used to lookup device OPPs.
  651. */
  652. struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
  653. {
  654. struct device_opp *dev_opp = find_device_opp(dev);
  655. if (IS_ERR(dev_opp))
  656. return ERR_CAST(dev_opp); /* matching type */
  657. return &dev_opp->srcu_head;
  658. }
  659. #ifdef CONFIG_OF
  660. /**
  661. * of_init_opp_table() - Initialize opp table from device tree
  662. * @dev: device pointer used to lookup device OPPs.
  663. *
  664. * Register the initial OPP table with the OPP library for given device.
  665. */
  666. int of_init_opp_table(struct device *dev)
  667. {
  668. const struct property *prop;
  669. const __be32 *val;
  670. int nr;
  671. prop = of_find_property(dev->of_node, "operating-points", NULL);
  672. if (!prop)
  673. return -ENODEV;
  674. if (!prop->value)
  675. return -ENODATA;
  676. /*
  677. * Each OPP is a set of tuples consisting of frequency and
  678. * voltage like <freq-kHz vol-uV>.
  679. */
  680. nr = prop->length / sizeof(u32);
  681. if (nr % 2) {
  682. dev_err(dev, "%s: Invalid OPP list\n", __func__);
  683. return -EINVAL;
  684. }
  685. val = prop->value;
  686. while (nr) {
  687. unsigned long freq = be32_to_cpup(val++) * 1000;
  688. unsigned long volt = be32_to_cpup(val++);
  689. if (dev_pm_opp_add_dynamic(dev, freq, volt, false))
  690. dev_warn(dev, "%s: Failed to add OPP %ld\n",
  691. __func__, freq);
  692. nr -= 2;
  693. }
  694. return 0;
  695. }
  696. EXPORT_SYMBOL_GPL(of_init_opp_table);
  697. /**
  698. * of_free_opp_table() - Free OPP table entries created from static DT entries
  699. * @dev: device pointer used to lookup device OPPs.
  700. *
  701. * Free OPPs created using static entries present in DT.
  702. */
  703. void of_free_opp_table(struct device *dev)
  704. {
  705. struct device_opp *dev_opp;
  706. struct dev_pm_opp *opp, *tmp;
  707. /* Check for existing list for 'dev' */
  708. dev_opp = find_device_opp(dev);
  709. if (IS_ERR(dev_opp)) {
  710. int error = PTR_ERR(dev_opp);
  711. if (error != -ENODEV)
  712. WARN(1, "%s: dev_opp: %d\n",
  713. IS_ERR_OR_NULL(dev) ?
  714. "Invalid device" : dev_name(dev),
  715. error);
  716. return;
  717. }
  718. /* Hold our list modification lock here */
  719. mutex_lock(&dev_opp_list_lock);
  720. /* Free static OPPs */
  721. list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
  722. if (!opp->dynamic)
  723. __dev_pm_opp_remove(dev_opp, opp);
  724. }
  725. mutex_unlock(&dev_opp_list_lock);
  726. }
  727. EXPORT_SYMBOL_GPL(of_free_opp_table);
  728. #endif