powerdomain.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. /*
  2. * OMAP powerdomain control
  3. *
  4. * Copyright (C) 2007-2008, 2011 Texas Instruments, Inc.
  5. * Copyright (C) 2007-2011 Nokia Corporation
  6. *
  7. * Written by Paul Walmsley
  8. * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
  9. * State counting code by Tero Kristo <tero.kristo@nokia.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #undef DEBUG
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/list.h>
  19. #include <linux/errno.h>
  20. #include <linux/string.h>
  21. #include <linux/spinlock.h>
  22. #include <trace/events/power.h>
  23. #include "cm2xxx_3xxx.h"
  24. #include "prcm44xx.h"
  25. #include "cm44xx.h"
  26. #include "prm2xxx_3xxx.h"
  27. #include "prm44xx.h"
  28. #include <asm/cpu.h>
  29. #include "powerdomain.h"
  30. #include "clockdomain.h"
  31. #include "voltage.h"
  32. #include "soc.h"
  33. #include "pm.h"
  34. #define PWRDM_TRACE_STATES_FLAG (1<<31)
  35. enum {
  36. PWRDM_STATE_NOW = 0,
  37. PWRDM_STATE_PREV,
  38. };
  39. /*
  40. * Types of sleep_switch used internally in omap_set_pwrdm_state()
  41. * and its associated static functions
  42. *
  43. * XXX Better documentation is needed here
  44. */
  45. #define ALREADYACTIVE_SWITCH 0
  46. #define FORCEWAKEUP_SWITCH 1
  47. #define LOWPOWERSTATE_SWITCH 2
  48. /* pwrdm_list contains all registered struct powerdomains */
  49. static LIST_HEAD(pwrdm_list);
  50. static struct pwrdm_ops *arch_pwrdm;
  51. /* Private functions */
  52. static struct powerdomain *_pwrdm_lookup(const char *name)
  53. {
  54. struct powerdomain *pwrdm, *temp_pwrdm;
  55. pwrdm = NULL;
  56. list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
  57. if (!strcmp(name, temp_pwrdm->name)) {
  58. pwrdm = temp_pwrdm;
  59. break;
  60. }
  61. }
  62. return pwrdm;
  63. }
  64. /**
  65. * _pwrdm_register - register a powerdomain
  66. * @pwrdm: struct powerdomain * to register
  67. *
  68. * Adds a powerdomain to the internal powerdomain list. Returns
  69. * -EINVAL if given a null pointer, -EEXIST if a powerdomain is
  70. * already registered by the provided name, or 0 upon success.
  71. */
  72. static int _pwrdm_register(struct powerdomain *pwrdm)
  73. {
  74. int i;
  75. struct voltagedomain *voltdm;
  76. if (!pwrdm || !pwrdm->name)
  77. return -EINVAL;
  78. if (cpu_is_omap44xx() &&
  79. pwrdm->prcm_partition == OMAP4430_INVALID_PRCM_PARTITION) {
  80. pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
  81. pwrdm->name);
  82. return -EINVAL;
  83. }
  84. if (_pwrdm_lookup(pwrdm->name))
  85. return -EEXIST;
  86. if (arch_pwrdm && arch_pwrdm->pwrdm_has_voltdm)
  87. if (!arch_pwrdm->pwrdm_has_voltdm())
  88. goto skip_voltdm;
  89. voltdm = voltdm_lookup(pwrdm->voltdm.name);
  90. if (!voltdm) {
  91. pr_err("powerdomain: %s: voltagedomain %s does not exist\n",
  92. pwrdm->name, pwrdm->voltdm.name);
  93. return -EINVAL;
  94. }
  95. pwrdm->voltdm.ptr = voltdm;
  96. INIT_LIST_HEAD(&pwrdm->voltdm_node);
  97. skip_voltdm:
  98. spin_lock_init(&pwrdm->_lock);
  99. list_add(&pwrdm->node, &pwrdm_list);
  100. /* Initialize the powerdomain's state counter */
  101. for (i = 0; i < PWRDM_MAX_PWRSTS; i++)
  102. pwrdm->state_counter[i] = 0;
  103. pwrdm->ret_logic_off_counter = 0;
  104. for (i = 0; i < pwrdm->banks; i++)
  105. pwrdm->ret_mem_off_counter[i] = 0;
  106. if (arch_pwrdm && arch_pwrdm->pwrdm_wait_transition)
  107. arch_pwrdm->pwrdm_wait_transition(pwrdm);
  108. pwrdm->state = pwrdm_read_pwrst(pwrdm);
  109. pwrdm->state_counter[pwrdm->state] = 1;
  110. pr_debug("powerdomain: registered %s\n", pwrdm->name);
  111. return 0;
  112. }
  113. static void _update_logic_membank_counters(struct powerdomain *pwrdm)
  114. {
  115. int i;
  116. u8 prev_logic_pwrst, prev_mem_pwrst;
  117. prev_logic_pwrst = pwrdm_read_prev_logic_pwrst(pwrdm);
  118. if ((pwrdm->pwrsts_logic_ret == PWRSTS_OFF_RET) &&
  119. (prev_logic_pwrst == PWRDM_POWER_OFF))
  120. pwrdm->ret_logic_off_counter++;
  121. for (i = 0; i < pwrdm->banks; i++) {
  122. prev_mem_pwrst = pwrdm_read_prev_mem_pwrst(pwrdm, i);
  123. if ((pwrdm->pwrsts_mem_ret[i] == PWRSTS_OFF_RET) &&
  124. (prev_mem_pwrst == PWRDM_POWER_OFF))
  125. pwrdm->ret_mem_off_counter[i]++;
  126. }
  127. }
  128. static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
  129. {
  130. int prev, next, state, trace_state = 0;
  131. if (pwrdm == NULL)
  132. return -EINVAL;
  133. state = pwrdm_read_pwrst(pwrdm);
  134. switch (flag) {
  135. case PWRDM_STATE_NOW:
  136. prev = pwrdm->state;
  137. break;
  138. case PWRDM_STATE_PREV:
  139. prev = pwrdm_read_prev_pwrst(pwrdm);
  140. if (pwrdm->state != prev)
  141. pwrdm->state_counter[prev]++;
  142. if (prev == PWRDM_POWER_RET)
  143. _update_logic_membank_counters(pwrdm);
  144. /*
  145. * If the power domain did not hit the desired state,
  146. * generate a trace event with both the desired and hit states
  147. */
  148. next = pwrdm_read_next_pwrst(pwrdm);
  149. if (next != prev) {
  150. trace_state = (PWRDM_TRACE_STATES_FLAG |
  151. ((next & OMAP_POWERSTATE_MASK) << 8) |
  152. ((prev & OMAP_POWERSTATE_MASK) << 0));
  153. trace_power_domain_target_rcuidle(pwrdm->name,
  154. trace_state,
  155. smp_processor_id());
  156. }
  157. break;
  158. default:
  159. return -EINVAL;
  160. }
  161. if (state != prev)
  162. pwrdm->state_counter[state]++;
  163. pm_dbg_update_time(pwrdm, prev);
  164. pwrdm->state = state;
  165. return 0;
  166. }
  167. static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
  168. {
  169. pwrdm_clear_all_prev_pwrst(pwrdm);
  170. _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
  171. return 0;
  172. }
  173. static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
  174. {
  175. _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
  176. return 0;
  177. }
  178. /**
  179. * _pwrdm_save_clkdm_state_and_activate - prepare for power state change
  180. * @pwrdm: struct powerdomain * to operate on
  181. * @curr_pwrst: current power state of @pwrdm
  182. * @pwrst: power state to switch to
  183. *
  184. * Determine whether the powerdomain needs to be turned on before
  185. * attempting to switch power states. Called by
  186. * omap_set_pwrdm_state(). NOTE that if the powerdomain contains
  187. * multiple clockdomains, this code assumes that the first clockdomain
  188. * supports software-supervised wakeup mode - potentially a problem.
  189. * Returns the power state switch mode currently in use (see the
  190. * "Types of sleep_switch" comment above).
  191. */
  192. static u8 _pwrdm_save_clkdm_state_and_activate(struct powerdomain *pwrdm,
  193. u8 curr_pwrst, u8 pwrst)
  194. {
  195. u8 sleep_switch;
  196. if (curr_pwrst < PWRDM_POWER_ON) {
  197. if (curr_pwrst > pwrst &&
  198. pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
  199. arch_pwrdm->pwrdm_set_lowpwrstchange) {
  200. sleep_switch = LOWPOWERSTATE_SWITCH;
  201. } else {
  202. clkdm_deny_idle_nolock(pwrdm->pwrdm_clkdms[0]);
  203. sleep_switch = FORCEWAKEUP_SWITCH;
  204. }
  205. } else {
  206. sleep_switch = ALREADYACTIVE_SWITCH;
  207. }
  208. return sleep_switch;
  209. }
  210. /**
  211. * _pwrdm_restore_clkdm_state - restore the clkdm hwsup state after pwrst change
  212. * @pwrdm: struct powerdomain * to operate on
  213. * @sleep_switch: return value from _pwrdm_save_clkdm_state_and_activate()
  214. *
  215. * Restore the clockdomain state perturbed by
  216. * _pwrdm_save_clkdm_state_and_activate(), and call the power state
  217. * bookkeeping code. Called by omap_set_pwrdm_state(). NOTE that if
  218. * the powerdomain contains multiple clockdomains, this assumes that
  219. * the first associated clockdomain supports either
  220. * hardware-supervised idle control in the register, or
  221. * software-supervised sleep. No return value.
  222. */
  223. static void _pwrdm_restore_clkdm_state(struct powerdomain *pwrdm,
  224. u8 sleep_switch)
  225. {
  226. switch (sleep_switch) {
  227. case FORCEWAKEUP_SWITCH:
  228. clkdm_allow_idle_nolock(pwrdm->pwrdm_clkdms[0]);
  229. break;
  230. case LOWPOWERSTATE_SWITCH:
  231. if (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
  232. arch_pwrdm->pwrdm_set_lowpwrstchange)
  233. arch_pwrdm->pwrdm_set_lowpwrstchange(pwrdm);
  234. pwrdm_state_switch_nolock(pwrdm);
  235. break;
  236. }
  237. }
  238. /* Public functions */
  239. /**
  240. * pwrdm_register_platform_funcs - register powerdomain implementation fns
  241. * @po: func pointers for arch specific implementations
  242. *
  243. * Register the list of function pointers used to implement the
  244. * powerdomain functions on different OMAP SoCs. Should be called
  245. * before any other pwrdm_register*() function. Returns -EINVAL if
  246. * @po is null, -EEXIST if platform functions have already been
  247. * registered, or 0 upon success.
  248. */
  249. int pwrdm_register_platform_funcs(struct pwrdm_ops *po)
  250. {
  251. if (!po)
  252. return -EINVAL;
  253. if (arch_pwrdm)
  254. return -EEXIST;
  255. arch_pwrdm = po;
  256. return 0;
  257. }
  258. /**
  259. * pwrdm_register_pwrdms - register SoC powerdomains
  260. * @ps: pointer to an array of struct powerdomain to register
  261. *
  262. * Register the powerdomains available on a particular OMAP SoC. Must
  263. * be called after pwrdm_register_platform_funcs(). May be called
  264. * multiple times. Returns -EACCES if called before
  265. * pwrdm_register_platform_funcs(); -EINVAL if the argument @ps is
  266. * null; or 0 upon success.
  267. */
  268. int pwrdm_register_pwrdms(struct powerdomain **ps)
  269. {
  270. struct powerdomain **p = NULL;
  271. if (!arch_pwrdm)
  272. return -EEXIST;
  273. if (!ps)
  274. return -EINVAL;
  275. for (p = ps; *p; p++)
  276. _pwrdm_register(*p);
  277. return 0;
  278. }
  279. /**
  280. * pwrdm_complete_init - set up the powerdomain layer
  281. *
  282. * Do whatever is necessary to initialize registered powerdomains and
  283. * powerdomain code. Currently, this programs the next power state
  284. * for each powerdomain to ON. This prevents powerdomains from
  285. * unexpectedly losing context or entering high wakeup latency modes
  286. * with non-power-management-enabled kernels. Must be called after
  287. * pwrdm_register_pwrdms(). Returns -EACCES if called before
  288. * pwrdm_register_pwrdms(), or 0 upon success.
  289. */
  290. int pwrdm_complete_init(void)
  291. {
  292. struct powerdomain *temp_p;
  293. if (list_empty(&pwrdm_list))
  294. return -EACCES;
  295. list_for_each_entry(temp_p, &pwrdm_list, node)
  296. pwrdm_set_next_pwrst(temp_p, PWRDM_POWER_ON);
  297. return 0;
  298. }
  299. /**
  300. * pwrdm_lock - acquire a Linux spinlock on a powerdomain
  301. * @pwrdm: struct powerdomain * to lock
  302. *
  303. * Acquire the powerdomain spinlock on @pwrdm. No return value.
  304. */
  305. void pwrdm_lock(struct powerdomain *pwrdm)
  306. __acquires(&pwrdm->_lock)
  307. {
  308. spin_lock_irqsave(&pwrdm->_lock, pwrdm->_lock_flags);
  309. }
  310. /**
  311. * pwrdm_unlock - release a Linux spinlock on a powerdomain
  312. * @pwrdm: struct powerdomain * to unlock
  313. *
  314. * Release the powerdomain spinlock on @pwrdm. No return value.
  315. */
  316. void pwrdm_unlock(struct powerdomain *pwrdm)
  317. __releases(&pwrdm->_lock)
  318. {
  319. spin_unlock_irqrestore(&pwrdm->_lock, pwrdm->_lock_flags);
  320. }
  321. /**
  322. * pwrdm_lookup - look up a powerdomain by name, return a pointer
  323. * @name: name of powerdomain
  324. *
  325. * Find a registered powerdomain by its name @name. Returns a pointer
  326. * to the struct powerdomain if found, or NULL otherwise.
  327. */
  328. struct powerdomain *pwrdm_lookup(const char *name)
  329. {
  330. struct powerdomain *pwrdm;
  331. if (!name)
  332. return NULL;
  333. pwrdm = _pwrdm_lookup(name);
  334. return pwrdm;
  335. }
  336. /**
  337. * pwrdm_for_each - call function on each registered clockdomain
  338. * @fn: callback function *
  339. *
  340. * Call the supplied function @fn for each registered powerdomain.
  341. * The callback function @fn can return anything but 0 to bail out
  342. * early from the iterator. Returns the last return value of the
  343. * callback function, which should be 0 for success or anything else
  344. * to indicate failure; or -EINVAL if the function pointer is null.
  345. */
  346. int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
  347. void *user)
  348. {
  349. struct powerdomain *temp_pwrdm;
  350. int ret = 0;
  351. if (!fn)
  352. return -EINVAL;
  353. list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
  354. ret = (*fn)(temp_pwrdm, user);
  355. if (ret)
  356. break;
  357. }
  358. return ret;
  359. }
  360. /**
  361. * pwrdm_add_clkdm - add a clockdomain to a powerdomain
  362. * @pwrdm: struct powerdomain * to add the clockdomain to
  363. * @clkdm: struct clockdomain * to associate with a powerdomain
  364. *
  365. * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
  366. * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
  367. * presented with invalid pointers; -ENOMEM if memory could not be allocated;
  368. * or 0 upon success.
  369. */
  370. int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
  371. {
  372. int i;
  373. int ret = -EINVAL;
  374. if (!pwrdm || !clkdm)
  375. return -EINVAL;
  376. pr_debug("powerdomain: %s: associating clockdomain %s\n",
  377. pwrdm->name, clkdm->name);
  378. for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
  379. if (!pwrdm->pwrdm_clkdms[i])
  380. break;
  381. #ifdef DEBUG
  382. if (pwrdm->pwrdm_clkdms[i] == clkdm) {
  383. ret = -EINVAL;
  384. goto pac_exit;
  385. }
  386. #endif
  387. }
  388. if (i == PWRDM_MAX_CLKDMS) {
  389. pr_debug("powerdomain: %s: increase PWRDM_MAX_CLKDMS for clkdm %s\n",
  390. pwrdm->name, clkdm->name);
  391. WARN_ON(1);
  392. ret = -ENOMEM;
  393. goto pac_exit;
  394. }
  395. pwrdm->pwrdm_clkdms[i] = clkdm;
  396. ret = 0;
  397. pac_exit:
  398. return ret;
  399. }
  400. /**
  401. * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
  402. * @pwrdm: struct powerdomain *
  403. *
  404. * Return the number of controllable memory banks in powerdomain @pwrdm,
  405. * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
  406. */
  407. int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
  408. {
  409. if (!pwrdm)
  410. return -EINVAL;
  411. return pwrdm->banks;
  412. }
  413. /**
  414. * pwrdm_set_next_pwrst - set next powerdomain power state
  415. * @pwrdm: struct powerdomain * to set
  416. * @pwrst: one of the PWRDM_POWER_* macros
  417. *
  418. * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
  419. * may not enter this state immediately if the preconditions for this state
  420. * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
  421. * null or if the power state is invalid for the powerdomin, or returns 0
  422. * upon success.
  423. */
  424. int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
  425. {
  426. int ret = -EINVAL;
  427. if (!pwrdm)
  428. return -EINVAL;
  429. if (!(pwrdm->pwrsts & (1 << pwrst)))
  430. return -EINVAL;
  431. pr_debug("powerdomain: %s: setting next powerstate to %0x\n",
  432. pwrdm->name, pwrst);
  433. if (arch_pwrdm && arch_pwrdm->pwrdm_set_next_pwrst) {
  434. /* Trace the pwrdm desired target state */
  435. trace_power_domain_target_rcuidle(pwrdm->name, pwrst,
  436. smp_processor_id());
  437. /* Program the pwrdm desired target state */
  438. ret = arch_pwrdm->pwrdm_set_next_pwrst(pwrdm, pwrst);
  439. }
  440. return ret;
  441. }
  442. /**
  443. * pwrdm_read_next_pwrst - get next powerdomain power state
  444. * @pwrdm: struct powerdomain * to get power state
  445. *
  446. * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
  447. * if the powerdomain pointer is null or returns the next power state
  448. * upon success.
  449. */
  450. int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
  451. {
  452. int ret = -EINVAL;
  453. if (!pwrdm)
  454. return -EINVAL;
  455. if (arch_pwrdm && arch_pwrdm->pwrdm_read_next_pwrst)
  456. ret = arch_pwrdm->pwrdm_read_next_pwrst(pwrdm);
  457. return ret;
  458. }
  459. /**
  460. * pwrdm_read_pwrst - get current powerdomain power state
  461. * @pwrdm: struct powerdomain * to get power state
  462. *
  463. * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
  464. * if the powerdomain pointer is null or returns the current power state
  465. * upon success. Note that if the power domain only supports the ON state
  466. * then just return ON as the current state.
  467. */
  468. int pwrdm_read_pwrst(struct powerdomain *pwrdm)
  469. {
  470. int ret = -EINVAL;
  471. if (!pwrdm)
  472. return -EINVAL;
  473. if (pwrdm->pwrsts == PWRSTS_ON)
  474. return PWRDM_POWER_ON;
  475. if (arch_pwrdm && arch_pwrdm->pwrdm_read_pwrst)
  476. ret = arch_pwrdm->pwrdm_read_pwrst(pwrdm);
  477. return ret;
  478. }
  479. /**
  480. * pwrdm_read_prev_pwrst - get previous powerdomain power state
  481. * @pwrdm: struct powerdomain * to get previous power state
  482. *
  483. * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
  484. * if the powerdomain pointer is null or returns the previous power state
  485. * upon success.
  486. */
  487. int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
  488. {
  489. int ret = -EINVAL;
  490. if (!pwrdm)
  491. return -EINVAL;
  492. if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_pwrst)
  493. ret = arch_pwrdm->pwrdm_read_prev_pwrst(pwrdm);
  494. return ret;
  495. }
  496. /**
  497. * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
  498. * @pwrdm: struct powerdomain * to set
  499. * @pwrst: one of the PWRDM_POWER_* macros
  500. *
  501. * Set the next power state @pwrst that the logic portion of the
  502. * powerdomain @pwrdm will enter when the powerdomain enters retention.
  503. * This will be either RETENTION or OFF, if supported. Returns
  504. * -EINVAL if the powerdomain pointer is null or the target power
  505. * state is not not supported, or returns 0 upon success.
  506. */
  507. int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
  508. {
  509. int ret = -EINVAL;
  510. if (!pwrdm)
  511. return -EINVAL;
  512. if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
  513. return -EINVAL;
  514. pr_debug("powerdomain: %s: setting next logic powerstate to %0x\n",
  515. pwrdm->name, pwrst);
  516. if (arch_pwrdm && arch_pwrdm->pwrdm_set_logic_retst)
  517. ret = arch_pwrdm->pwrdm_set_logic_retst(pwrdm, pwrst);
  518. return ret;
  519. }
  520. /**
  521. * pwrdm_set_mem_onst - set memory power state while powerdomain ON
  522. * @pwrdm: struct powerdomain * to set
  523. * @bank: memory bank number to set (0-3)
  524. * @pwrst: one of the PWRDM_POWER_* macros
  525. *
  526. * Set the next power state @pwrst that memory bank @bank of the
  527. * powerdomain @pwrdm will enter when the powerdomain enters the ON
  528. * state. @bank will be a number from 0 to 3, and represents different
  529. * types of memory, depending on the powerdomain. Returns -EINVAL if
  530. * the powerdomain pointer is null or the target power state is not
  531. * not supported for this memory bank, -EEXIST if the target memory
  532. * bank does not exist or is not controllable, or returns 0 upon
  533. * success.
  534. */
  535. int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
  536. {
  537. int ret = -EINVAL;
  538. if (!pwrdm)
  539. return -EINVAL;
  540. if (pwrdm->banks < (bank + 1))
  541. return -EEXIST;
  542. if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
  543. return -EINVAL;
  544. pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-ON to %0x\n",
  545. pwrdm->name, bank, pwrst);
  546. if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_onst)
  547. ret = arch_pwrdm->pwrdm_set_mem_onst(pwrdm, bank, pwrst);
  548. return ret;
  549. }
  550. /**
  551. * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
  552. * @pwrdm: struct powerdomain * to set
  553. * @bank: memory bank number to set (0-3)
  554. * @pwrst: one of the PWRDM_POWER_* macros
  555. *
  556. * Set the next power state @pwrst that memory bank @bank of the
  557. * powerdomain @pwrdm will enter when the powerdomain enters the
  558. * RETENTION state. Bank will be a number from 0 to 3, and represents
  559. * different types of memory, depending on the powerdomain. @pwrst
  560. * will be either RETENTION or OFF, if supported. Returns -EINVAL if
  561. * the powerdomain pointer is null or the target power state is not
  562. * not supported for this memory bank, -EEXIST if the target memory
  563. * bank does not exist or is not controllable, or returns 0 upon
  564. * success.
  565. */
  566. int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
  567. {
  568. int ret = -EINVAL;
  569. if (!pwrdm)
  570. return -EINVAL;
  571. if (pwrdm->banks < (bank + 1))
  572. return -EEXIST;
  573. if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
  574. return -EINVAL;
  575. pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-RET to %0x\n",
  576. pwrdm->name, bank, pwrst);
  577. if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_retst)
  578. ret = arch_pwrdm->pwrdm_set_mem_retst(pwrdm, bank, pwrst);
  579. return ret;
  580. }
  581. /**
  582. * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
  583. * @pwrdm: struct powerdomain * to get current logic retention power state
  584. *
  585. * Return the power state that the logic portion of powerdomain @pwrdm
  586. * will enter when the powerdomain enters retention. Returns -EINVAL
  587. * if the powerdomain pointer is null or returns the logic retention
  588. * power state upon success.
  589. */
  590. int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
  591. {
  592. int ret = -EINVAL;
  593. if (!pwrdm)
  594. return -EINVAL;
  595. if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_pwrst)
  596. ret = arch_pwrdm->pwrdm_read_logic_pwrst(pwrdm);
  597. return ret;
  598. }
  599. /**
  600. * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
  601. * @pwrdm: struct powerdomain * to get previous logic power state
  602. *
  603. * Return the powerdomain @pwrdm's previous logic power state. Returns
  604. * -EINVAL if the powerdomain pointer is null or returns the previous
  605. * logic power state upon success.
  606. */
  607. int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
  608. {
  609. int ret = -EINVAL;
  610. if (!pwrdm)
  611. return -EINVAL;
  612. if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_logic_pwrst)
  613. ret = arch_pwrdm->pwrdm_read_prev_logic_pwrst(pwrdm);
  614. return ret;
  615. }
  616. /**
  617. * pwrdm_read_logic_retst - get next powerdomain logic power state
  618. * @pwrdm: struct powerdomain * to get next logic power state
  619. *
  620. * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
  621. * if the powerdomain pointer is null or returns the next logic
  622. * power state upon success.
  623. */
  624. int pwrdm_read_logic_retst(struct powerdomain *pwrdm)
  625. {
  626. int ret = -EINVAL;
  627. if (!pwrdm)
  628. return -EINVAL;
  629. if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_retst)
  630. ret = arch_pwrdm->pwrdm_read_logic_retst(pwrdm);
  631. return ret;
  632. }
  633. /**
  634. * pwrdm_read_mem_pwrst - get current memory bank power state
  635. * @pwrdm: struct powerdomain * to get current memory bank power state
  636. * @bank: memory bank number (0-3)
  637. *
  638. * Return the powerdomain @pwrdm's current memory power state for bank
  639. * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
  640. * the target memory bank does not exist or is not controllable, or
  641. * returns the current memory power state upon success.
  642. */
  643. int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
  644. {
  645. int ret = -EINVAL;
  646. if (!pwrdm)
  647. return ret;
  648. if (pwrdm->banks < (bank + 1))
  649. return ret;
  650. if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
  651. bank = 1;
  652. if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_pwrst)
  653. ret = arch_pwrdm->pwrdm_read_mem_pwrst(pwrdm, bank);
  654. return ret;
  655. }
  656. /**
  657. * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
  658. * @pwrdm: struct powerdomain * to get previous memory bank power state
  659. * @bank: memory bank number (0-3)
  660. *
  661. * Return the powerdomain @pwrdm's previous memory power state for
  662. * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
  663. * -EEXIST if the target memory bank does not exist or is not
  664. * controllable, or returns the previous memory power state upon
  665. * success.
  666. */
  667. int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
  668. {
  669. int ret = -EINVAL;
  670. if (!pwrdm)
  671. return ret;
  672. if (pwrdm->banks < (bank + 1))
  673. return ret;
  674. if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
  675. bank = 1;
  676. if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_mem_pwrst)
  677. ret = arch_pwrdm->pwrdm_read_prev_mem_pwrst(pwrdm, bank);
  678. return ret;
  679. }
  680. /**
  681. * pwrdm_read_mem_retst - get next memory bank power state
  682. * @pwrdm: struct powerdomain * to get mext memory bank power state
  683. * @bank: memory bank number (0-3)
  684. *
  685. * Return the powerdomain pwrdm's next memory power state for bank
  686. * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
  687. * the target memory bank does not exist or is not controllable, or
  688. * returns the next memory power state upon success.
  689. */
  690. int pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
  691. {
  692. int ret = -EINVAL;
  693. if (!pwrdm)
  694. return ret;
  695. if (pwrdm->banks < (bank + 1))
  696. return ret;
  697. if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_retst)
  698. ret = arch_pwrdm->pwrdm_read_mem_retst(pwrdm, bank);
  699. return ret;
  700. }
  701. /**
  702. * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
  703. * @pwrdm: struct powerdomain * to clear
  704. *
  705. * Clear the powerdomain's previous power state register @pwrdm.
  706. * Clears the entire register, including logic and memory bank
  707. * previous power states. Returns -EINVAL if the powerdomain pointer
  708. * is null, or returns 0 upon success.
  709. */
  710. int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
  711. {
  712. int ret = -EINVAL;
  713. if (!pwrdm)
  714. return ret;
  715. /*
  716. * XXX should get the powerdomain's current state here;
  717. * warn & fail if it is not ON.
  718. */
  719. pr_debug("powerdomain: %s: clearing previous power state reg\n",
  720. pwrdm->name);
  721. if (arch_pwrdm && arch_pwrdm->pwrdm_clear_all_prev_pwrst)
  722. ret = arch_pwrdm->pwrdm_clear_all_prev_pwrst(pwrdm);
  723. return ret;
  724. }
  725. /**
  726. * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
  727. * @pwrdm: struct powerdomain *
  728. *
  729. * Enable automatic context save-and-restore upon power state change
  730. * for some devices in the powerdomain @pwrdm. Warning: this only
  731. * affects a subset of devices in a powerdomain; check the TRM
  732. * closely. Returns -EINVAL if the powerdomain pointer is null or if
  733. * the powerdomain does not support automatic save-and-restore, or
  734. * returns 0 upon success.
  735. */
  736. int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
  737. {
  738. int ret = -EINVAL;
  739. if (!pwrdm)
  740. return ret;
  741. if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
  742. return ret;
  743. pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n", pwrdm->name);
  744. if (arch_pwrdm && arch_pwrdm->pwrdm_enable_hdwr_sar)
  745. ret = arch_pwrdm->pwrdm_enable_hdwr_sar(pwrdm);
  746. return ret;
  747. }
  748. /**
  749. * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
  750. * @pwrdm: struct powerdomain *
  751. *
  752. * Disable automatic context save-and-restore upon power state change
  753. * for some devices in the powerdomain @pwrdm. Warning: this only
  754. * affects a subset of devices in a powerdomain; check the TRM
  755. * closely. Returns -EINVAL if the powerdomain pointer is null or if
  756. * the powerdomain does not support automatic save-and-restore, or
  757. * returns 0 upon success.
  758. */
  759. int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
  760. {
  761. int ret = -EINVAL;
  762. if (!pwrdm)
  763. return ret;
  764. if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
  765. return ret;
  766. pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n", pwrdm->name);
  767. if (arch_pwrdm && arch_pwrdm->pwrdm_disable_hdwr_sar)
  768. ret = arch_pwrdm->pwrdm_disable_hdwr_sar(pwrdm);
  769. return ret;
  770. }
  771. /**
  772. * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
  773. * @pwrdm: struct powerdomain *
  774. *
  775. * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
  776. * for some devices, or 0 if it does not.
  777. */
  778. bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
  779. {
  780. return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
  781. }
  782. int pwrdm_state_switch_nolock(struct powerdomain *pwrdm)
  783. {
  784. int ret;
  785. if (!pwrdm || !arch_pwrdm)
  786. return -EINVAL;
  787. ret = arch_pwrdm->pwrdm_wait_transition(pwrdm);
  788. if (!ret)
  789. ret = _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
  790. return ret;
  791. }
  792. int __deprecated pwrdm_state_switch(struct powerdomain *pwrdm)
  793. {
  794. int ret;
  795. pwrdm_lock(pwrdm);
  796. ret = pwrdm_state_switch_nolock(pwrdm);
  797. pwrdm_unlock(pwrdm);
  798. return ret;
  799. }
  800. int pwrdm_pre_transition(struct powerdomain *pwrdm)
  801. {
  802. if (pwrdm)
  803. _pwrdm_pre_transition_cb(pwrdm, NULL);
  804. else
  805. pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
  806. return 0;
  807. }
  808. int pwrdm_post_transition(struct powerdomain *pwrdm)
  809. {
  810. if (pwrdm)
  811. _pwrdm_post_transition_cb(pwrdm, NULL);
  812. else
  813. pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
  814. return 0;
  815. }
  816. /**
  817. * pwrdm_get_valid_lp_state() - Find best match deep power state
  818. * @pwrdm: power domain for which we want to find best match
  819. * @is_logic_state: Are we looking for logic state match here? Should
  820. * be one of PWRDM_xxx macro values
  821. * @req_state: requested power state
  822. *
  823. * Returns: closest match for requested power state. default fallback
  824. * is RET for logic state and ON for power state.
  825. *
  826. * This does a search from the power domain data looking for the
  827. * closest valid power domain state that the hardware can achieve.
  828. * PRCM definitions for PWRSTCTRL allows us to program whatever
  829. * configuration we'd like, and PRCM will actually attempt such
  830. * a transition, however if the powerdomain does not actually support it,
  831. * we endup with a hung system. The valid power domain states are already
  832. * available in our powerdomain data files. So this function tries to do
  833. * the following:
  834. * a) find if we have an exact match to the request - no issues.
  835. * b) else find if a deeper power state is possible.
  836. * c) failing which, it tries to find closest higher power state for the
  837. * request.
  838. */
  839. u8 pwrdm_get_valid_lp_state(struct powerdomain *pwrdm,
  840. bool is_logic_state, u8 req_state)
  841. {
  842. u8 pwrdm_states = is_logic_state ? pwrdm->pwrsts_logic_ret :
  843. pwrdm->pwrsts;
  844. /* For logic, ret is highest and others, ON is highest */
  845. u8 default_pwrst = is_logic_state ? PWRDM_POWER_RET : PWRDM_POWER_ON;
  846. u8 new_pwrst;
  847. bool found;
  848. /* If it is already supported, nothing to search */
  849. if (pwrdm_states & BIT(req_state))
  850. return req_state;
  851. if (!req_state)
  852. goto up_search;
  853. /*
  854. * So, we dont have a exact match
  855. * Can we get a deeper power state match?
  856. */
  857. new_pwrst = req_state - 1;
  858. found = true;
  859. while (!(pwrdm_states & BIT(new_pwrst))) {
  860. /* No match even at OFF? Not available */
  861. if (new_pwrst == PWRDM_POWER_OFF) {
  862. found = false;
  863. break;
  864. }
  865. new_pwrst--;
  866. }
  867. if (found)
  868. goto done;
  869. up_search:
  870. /* OK, no deeper ones, can we get a higher match? */
  871. new_pwrst = req_state + 1;
  872. while (!(pwrdm_states & BIT(new_pwrst))) {
  873. if (new_pwrst > PWRDM_POWER_ON) {
  874. WARN(1, "powerdomain: %s: Fix max powerstate to ON\n",
  875. pwrdm->name);
  876. return PWRDM_POWER_ON;
  877. }
  878. if (new_pwrst == default_pwrst)
  879. break;
  880. new_pwrst++;
  881. }
  882. done:
  883. return new_pwrst;
  884. }
  885. /**
  886. * omap_set_pwrdm_state - change a powerdomain's current power state
  887. * @pwrdm: struct powerdomain * to change the power state of
  888. * @pwrst: power state to change to
  889. *
  890. * Change the current hardware power state of the powerdomain
  891. * represented by @pwrdm to the power state represented by @pwrst.
  892. * Returns -EINVAL if @pwrdm is null or invalid or if the
  893. * powerdomain's current power state could not be read, or returns 0
  894. * upon success or if @pwrdm does not support @pwrst or any
  895. * lower-power state. XXX Should not return 0 if the @pwrdm does not
  896. * support @pwrst or any lower-power state: this should be an error.
  897. */
  898. int omap_set_pwrdm_state(struct powerdomain *pwrdm, u8 pwrst)
  899. {
  900. u8 next_pwrst, sleep_switch;
  901. int curr_pwrst;
  902. int ret = 0;
  903. if (!pwrdm || IS_ERR(pwrdm))
  904. return -EINVAL;
  905. while (!(pwrdm->pwrsts & (1 << pwrst))) {
  906. if (pwrst == PWRDM_POWER_OFF)
  907. return ret;
  908. pwrst--;
  909. }
  910. pwrdm_lock(pwrdm);
  911. curr_pwrst = pwrdm_read_pwrst(pwrdm);
  912. if (curr_pwrst < 0) {
  913. ret = -EINVAL;
  914. goto osps_out;
  915. }
  916. next_pwrst = pwrdm_read_next_pwrst(pwrdm);
  917. if (curr_pwrst == pwrst && next_pwrst == pwrst)
  918. goto osps_out;
  919. sleep_switch = _pwrdm_save_clkdm_state_and_activate(pwrdm, curr_pwrst,
  920. pwrst);
  921. ret = pwrdm_set_next_pwrst(pwrdm, pwrst);
  922. if (ret)
  923. pr_err("%s: unable to set power state of powerdomain: %s\n",
  924. __func__, pwrdm->name);
  925. _pwrdm_restore_clkdm_state(pwrdm, sleep_switch);
  926. osps_out:
  927. pwrdm_unlock(pwrdm);
  928. return ret;
  929. }
  930. /**
  931. * pwrdm_get_context_loss_count - get powerdomain's context loss count
  932. * @pwrdm: struct powerdomain * to wait for
  933. *
  934. * Context loss count is the sum of powerdomain off-mode counter, the
  935. * logic off counter and the per-bank memory off counter. Returns negative
  936. * (and WARNs) upon error, otherwise, returns the context loss count.
  937. */
  938. int pwrdm_get_context_loss_count(struct powerdomain *pwrdm)
  939. {
  940. int i, count;
  941. if (!pwrdm) {
  942. WARN(1, "powerdomain: %s: pwrdm is null\n", __func__);
  943. return -ENODEV;
  944. }
  945. count = pwrdm->state_counter[PWRDM_POWER_OFF];
  946. count += pwrdm->ret_logic_off_counter;
  947. for (i = 0; i < pwrdm->banks; i++)
  948. count += pwrdm->ret_mem_off_counter[i];
  949. /*
  950. * Context loss count has to be a non-negative value. Clear the sign
  951. * bit to get a value range from 0 to INT_MAX.
  952. */
  953. count &= INT_MAX;
  954. pr_debug("powerdomain: %s: context loss count = %d\n",
  955. pwrdm->name, count);
  956. return count;
  957. }
  958. /**
  959. * pwrdm_can_ever_lose_context - can this powerdomain ever lose context?
  960. * @pwrdm: struct powerdomain *
  961. *
  962. * Given a struct powerdomain * @pwrdm, returns 1 if the powerdomain
  963. * can lose either memory or logic context or if @pwrdm is invalid, or
  964. * returns 0 otherwise. This function is not concerned with how the
  965. * powerdomain registers are programmed (i.e., to go off or not); it's
  966. * concerned with whether it's ever possible for this powerdomain to
  967. * go off while some other part of the chip is active. This function
  968. * assumes that every powerdomain can go to either ON or INACTIVE.
  969. */
  970. bool pwrdm_can_ever_lose_context(struct powerdomain *pwrdm)
  971. {
  972. int i;
  973. if (!pwrdm) {
  974. pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
  975. __func__);
  976. return 1;
  977. }
  978. if (pwrdm->pwrsts & PWRSTS_OFF)
  979. return 1;
  980. if (pwrdm->pwrsts & PWRSTS_RET) {
  981. if (pwrdm->pwrsts_logic_ret & PWRSTS_OFF)
  982. return 1;
  983. for (i = 0; i < pwrdm->banks; i++)
  984. if (pwrdm->pwrsts_mem_ret[i] & PWRSTS_OFF)
  985. return 1;
  986. }
  987. for (i = 0; i < pwrdm->banks; i++)
  988. if (pwrdm->pwrsts_mem_on[i] & PWRSTS_OFF)
  989. return 1;
  990. return 0;
  991. }