12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202 |
- /*
- * OMAP powerdomain control
- *
- * Copyright (C) 2007-2008, 2011 Texas Instruments, Inc.
- * Copyright (C) 2007-2011 Nokia Corporation
- *
- * Written by Paul Walmsley
- * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
- * State counting code by Tero Kristo <tero.kristo@nokia.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- */
- #undef DEBUG
- #include <linux/kernel.h>
- #include <linux/types.h>
- #include <linux/list.h>
- #include <linux/errno.h>
- #include <linux/string.h>
- #include <linux/spinlock.h>
- #include <trace/events/power.h>
- #include "cm2xxx_3xxx.h"
- #include "prcm44xx.h"
- #include "cm44xx.h"
- #include "prm2xxx_3xxx.h"
- #include "prm44xx.h"
- #include <asm/cpu.h>
- #include "powerdomain.h"
- #include "clockdomain.h"
- #include "voltage.h"
- #include "soc.h"
- #include "pm.h"
- #define PWRDM_TRACE_STATES_FLAG (1<<31)
- enum {
- PWRDM_STATE_NOW = 0,
- PWRDM_STATE_PREV,
- };
- /*
- * Types of sleep_switch used internally in omap_set_pwrdm_state()
- * and its associated static functions
- *
- * XXX Better documentation is needed here
- */
- #define ALREADYACTIVE_SWITCH 0
- #define FORCEWAKEUP_SWITCH 1
- #define LOWPOWERSTATE_SWITCH 2
- /* pwrdm_list contains all registered struct powerdomains */
- static LIST_HEAD(pwrdm_list);
- static struct pwrdm_ops *arch_pwrdm;
- /* Private functions */
- static struct powerdomain *_pwrdm_lookup(const char *name)
- {
- struct powerdomain *pwrdm, *temp_pwrdm;
- pwrdm = NULL;
- list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
- if (!strcmp(name, temp_pwrdm->name)) {
- pwrdm = temp_pwrdm;
- break;
- }
- }
- return pwrdm;
- }
- /**
- * _pwrdm_register - register a powerdomain
- * @pwrdm: struct powerdomain * to register
- *
- * Adds a powerdomain to the internal powerdomain list. Returns
- * -EINVAL if given a null pointer, -EEXIST if a powerdomain is
- * already registered by the provided name, or 0 upon success.
- */
- static int _pwrdm_register(struct powerdomain *pwrdm)
- {
- int i;
- struct voltagedomain *voltdm;
- if (!pwrdm || !pwrdm->name)
- return -EINVAL;
- if (cpu_is_omap44xx() &&
- pwrdm->prcm_partition == OMAP4430_INVALID_PRCM_PARTITION) {
- pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
- pwrdm->name);
- return -EINVAL;
- }
- if (_pwrdm_lookup(pwrdm->name))
- return -EEXIST;
- if (arch_pwrdm && arch_pwrdm->pwrdm_has_voltdm)
- if (!arch_pwrdm->pwrdm_has_voltdm())
- goto skip_voltdm;
- voltdm = voltdm_lookup(pwrdm->voltdm.name);
- if (!voltdm) {
- pr_err("powerdomain: %s: voltagedomain %s does not exist\n",
- pwrdm->name, pwrdm->voltdm.name);
- return -EINVAL;
- }
- pwrdm->voltdm.ptr = voltdm;
- INIT_LIST_HEAD(&pwrdm->voltdm_node);
- skip_voltdm:
- spin_lock_init(&pwrdm->_lock);
- list_add(&pwrdm->node, &pwrdm_list);
- /* Initialize the powerdomain's state counter */
- for (i = 0; i < PWRDM_MAX_PWRSTS; i++)
- pwrdm->state_counter[i] = 0;
- pwrdm->ret_logic_off_counter = 0;
- for (i = 0; i < pwrdm->banks; i++)
- pwrdm->ret_mem_off_counter[i] = 0;
- if (arch_pwrdm && arch_pwrdm->pwrdm_wait_transition)
- arch_pwrdm->pwrdm_wait_transition(pwrdm);
- pwrdm->state = pwrdm_read_pwrst(pwrdm);
- pwrdm->state_counter[pwrdm->state] = 1;
- pr_debug("powerdomain: registered %s\n", pwrdm->name);
- return 0;
- }
- static void _update_logic_membank_counters(struct powerdomain *pwrdm)
- {
- int i;
- u8 prev_logic_pwrst, prev_mem_pwrst;
- prev_logic_pwrst = pwrdm_read_prev_logic_pwrst(pwrdm);
- if ((pwrdm->pwrsts_logic_ret == PWRSTS_OFF_RET) &&
- (prev_logic_pwrst == PWRDM_POWER_OFF))
- pwrdm->ret_logic_off_counter++;
- for (i = 0; i < pwrdm->banks; i++) {
- prev_mem_pwrst = pwrdm_read_prev_mem_pwrst(pwrdm, i);
- if ((pwrdm->pwrsts_mem_ret[i] == PWRSTS_OFF_RET) &&
- (prev_mem_pwrst == PWRDM_POWER_OFF))
- pwrdm->ret_mem_off_counter[i]++;
- }
- }
- static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
- {
- int prev, next, state, trace_state = 0;
- if (pwrdm == NULL)
- return -EINVAL;
- state = pwrdm_read_pwrst(pwrdm);
- switch (flag) {
- case PWRDM_STATE_NOW:
- prev = pwrdm->state;
- break;
- case PWRDM_STATE_PREV:
- prev = pwrdm_read_prev_pwrst(pwrdm);
- if (pwrdm->state != prev)
- pwrdm->state_counter[prev]++;
- if (prev == PWRDM_POWER_RET)
- _update_logic_membank_counters(pwrdm);
- /*
- * If the power domain did not hit the desired state,
- * generate a trace event with both the desired and hit states
- */
- next = pwrdm_read_next_pwrst(pwrdm);
- if (next != prev) {
- trace_state = (PWRDM_TRACE_STATES_FLAG |
- ((next & OMAP_POWERSTATE_MASK) << 8) |
- ((prev & OMAP_POWERSTATE_MASK) << 0));
- trace_power_domain_target_rcuidle(pwrdm->name,
- trace_state,
- smp_processor_id());
- }
- break;
- default:
- return -EINVAL;
- }
- if (state != prev)
- pwrdm->state_counter[state]++;
- pm_dbg_update_time(pwrdm, prev);
- pwrdm->state = state;
- return 0;
- }
- static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
- {
- pwrdm_clear_all_prev_pwrst(pwrdm);
- _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
- return 0;
- }
- static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
- {
- _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
- return 0;
- }
- /**
- * _pwrdm_save_clkdm_state_and_activate - prepare for power state change
- * @pwrdm: struct powerdomain * to operate on
- * @curr_pwrst: current power state of @pwrdm
- * @pwrst: power state to switch to
- *
- * Determine whether the powerdomain needs to be turned on before
- * attempting to switch power states. Called by
- * omap_set_pwrdm_state(). NOTE that if the powerdomain contains
- * multiple clockdomains, this code assumes that the first clockdomain
- * supports software-supervised wakeup mode - potentially a problem.
- * Returns the power state switch mode currently in use (see the
- * "Types of sleep_switch" comment above).
- */
- static u8 _pwrdm_save_clkdm_state_and_activate(struct powerdomain *pwrdm,
- u8 curr_pwrst, u8 pwrst)
- {
- u8 sleep_switch;
- if (curr_pwrst < PWRDM_POWER_ON) {
- if (curr_pwrst > pwrst &&
- pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
- arch_pwrdm->pwrdm_set_lowpwrstchange) {
- sleep_switch = LOWPOWERSTATE_SWITCH;
- } else {
- clkdm_deny_idle_nolock(pwrdm->pwrdm_clkdms[0]);
- sleep_switch = FORCEWAKEUP_SWITCH;
- }
- } else {
- sleep_switch = ALREADYACTIVE_SWITCH;
- }
- return sleep_switch;
- }
- /**
- * _pwrdm_restore_clkdm_state - restore the clkdm hwsup state after pwrst change
- * @pwrdm: struct powerdomain * to operate on
- * @sleep_switch: return value from _pwrdm_save_clkdm_state_and_activate()
- *
- * Restore the clockdomain state perturbed by
- * _pwrdm_save_clkdm_state_and_activate(), and call the power state
- * bookkeeping code. Called by omap_set_pwrdm_state(). NOTE that if
- * the powerdomain contains multiple clockdomains, this assumes that
- * the first associated clockdomain supports either
- * hardware-supervised idle control in the register, or
- * software-supervised sleep. No return value.
- */
- static void _pwrdm_restore_clkdm_state(struct powerdomain *pwrdm,
- u8 sleep_switch)
- {
- switch (sleep_switch) {
- case FORCEWAKEUP_SWITCH:
- clkdm_allow_idle_nolock(pwrdm->pwrdm_clkdms[0]);
- break;
- case LOWPOWERSTATE_SWITCH:
- if (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
- arch_pwrdm->pwrdm_set_lowpwrstchange)
- arch_pwrdm->pwrdm_set_lowpwrstchange(pwrdm);
- pwrdm_state_switch_nolock(pwrdm);
- break;
- }
- }
- /* Public functions */
- /**
- * pwrdm_register_platform_funcs - register powerdomain implementation fns
- * @po: func pointers for arch specific implementations
- *
- * Register the list of function pointers used to implement the
- * powerdomain functions on different OMAP SoCs. Should be called
- * before any other pwrdm_register*() function. Returns -EINVAL if
- * @po is null, -EEXIST if platform functions have already been
- * registered, or 0 upon success.
- */
- int pwrdm_register_platform_funcs(struct pwrdm_ops *po)
- {
- if (!po)
- return -EINVAL;
- if (arch_pwrdm)
- return -EEXIST;
- arch_pwrdm = po;
- return 0;
- }
- /**
- * pwrdm_register_pwrdms - register SoC powerdomains
- * @ps: pointer to an array of struct powerdomain to register
- *
- * Register the powerdomains available on a particular OMAP SoC. Must
- * be called after pwrdm_register_platform_funcs(). May be called
- * multiple times. Returns -EACCES if called before
- * pwrdm_register_platform_funcs(); -EINVAL if the argument @ps is
- * null; or 0 upon success.
- */
- int pwrdm_register_pwrdms(struct powerdomain **ps)
- {
- struct powerdomain **p = NULL;
- if (!arch_pwrdm)
- return -EEXIST;
- if (!ps)
- return -EINVAL;
- for (p = ps; *p; p++)
- _pwrdm_register(*p);
- return 0;
- }
- /**
- * pwrdm_complete_init - set up the powerdomain layer
- *
- * Do whatever is necessary to initialize registered powerdomains and
- * powerdomain code. Currently, this programs the next power state
- * for each powerdomain to ON. This prevents powerdomains from
- * unexpectedly losing context or entering high wakeup latency modes
- * with non-power-management-enabled kernels. Must be called after
- * pwrdm_register_pwrdms(). Returns -EACCES if called before
- * pwrdm_register_pwrdms(), or 0 upon success.
- */
- int pwrdm_complete_init(void)
- {
- struct powerdomain *temp_p;
- if (list_empty(&pwrdm_list))
- return -EACCES;
- list_for_each_entry(temp_p, &pwrdm_list, node)
- pwrdm_set_next_pwrst(temp_p, PWRDM_POWER_ON);
- return 0;
- }
- /**
- * pwrdm_lock - acquire a Linux spinlock on a powerdomain
- * @pwrdm: struct powerdomain * to lock
- *
- * Acquire the powerdomain spinlock on @pwrdm. No return value.
- */
- void pwrdm_lock(struct powerdomain *pwrdm)
- __acquires(&pwrdm->_lock)
- {
- spin_lock_irqsave(&pwrdm->_lock, pwrdm->_lock_flags);
- }
- /**
- * pwrdm_unlock - release a Linux spinlock on a powerdomain
- * @pwrdm: struct powerdomain * to unlock
- *
- * Release the powerdomain spinlock on @pwrdm. No return value.
- */
- void pwrdm_unlock(struct powerdomain *pwrdm)
- __releases(&pwrdm->_lock)
- {
- spin_unlock_irqrestore(&pwrdm->_lock, pwrdm->_lock_flags);
- }
- /**
- * pwrdm_lookup - look up a powerdomain by name, return a pointer
- * @name: name of powerdomain
- *
- * Find a registered powerdomain by its name @name. Returns a pointer
- * to the struct powerdomain if found, or NULL otherwise.
- */
- struct powerdomain *pwrdm_lookup(const char *name)
- {
- struct powerdomain *pwrdm;
- if (!name)
- return NULL;
- pwrdm = _pwrdm_lookup(name);
- return pwrdm;
- }
- /**
- * pwrdm_for_each - call function on each registered clockdomain
- * @fn: callback function *
- *
- * Call the supplied function @fn for each registered powerdomain.
- * The callback function @fn can return anything but 0 to bail out
- * early from the iterator. Returns the last return value of the
- * callback function, which should be 0 for success or anything else
- * to indicate failure; or -EINVAL if the function pointer is null.
- */
- int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
- void *user)
- {
- struct powerdomain *temp_pwrdm;
- int ret = 0;
- if (!fn)
- return -EINVAL;
- list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
- ret = (*fn)(temp_pwrdm, user);
- if (ret)
- break;
- }
- return ret;
- }
- /**
- * pwrdm_add_clkdm - add a clockdomain to a powerdomain
- * @pwrdm: struct powerdomain * to add the clockdomain to
- * @clkdm: struct clockdomain * to associate with a powerdomain
- *
- * Associate the clockdomain @clkdm with a powerdomain @pwrdm. This
- * enables the use of pwrdm_for_each_clkdm(). Returns -EINVAL if
- * presented with invalid pointers; -ENOMEM if memory could not be allocated;
- * or 0 upon success.
- */
- int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
- {
- int i;
- int ret = -EINVAL;
- if (!pwrdm || !clkdm)
- return -EINVAL;
- pr_debug("powerdomain: %s: associating clockdomain %s\n",
- pwrdm->name, clkdm->name);
- for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
- if (!pwrdm->pwrdm_clkdms[i])
- break;
- #ifdef DEBUG
- if (pwrdm->pwrdm_clkdms[i] == clkdm) {
- ret = -EINVAL;
- goto pac_exit;
- }
- #endif
- }
- if (i == PWRDM_MAX_CLKDMS) {
- pr_debug("powerdomain: %s: increase PWRDM_MAX_CLKDMS for clkdm %s\n",
- pwrdm->name, clkdm->name);
- WARN_ON(1);
- ret = -ENOMEM;
- goto pac_exit;
- }
- pwrdm->pwrdm_clkdms[i] = clkdm;
- ret = 0;
- pac_exit:
- return ret;
- }
- /**
- * pwrdm_get_mem_bank_count - get number of memory banks in this powerdomain
- * @pwrdm: struct powerdomain *
- *
- * Return the number of controllable memory banks in powerdomain @pwrdm,
- * starting with 1. Returns -EINVAL if the powerdomain pointer is null.
- */
- int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
- {
- if (!pwrdm)
- return -EINVAL;
- return pwrdm->banks;
- }
- /**
- * pwrdm_set_next_pwrst - set next powerdomain power state
- * @pwrdm: struct powerdomain * to set
- * @pwrst: one of the PWRDM_POWER_* macros
- *
- * Set the powerdomain @pwrdm's next power state to @pwrst. The powerdomain
- * may not enter this state immediately if the preconditions for this state
- * have not been satisfied. Returns -EINVAL if the powerdomain pointer is
- * null or if the power state is invalid for the powerdomin, or returns 0
- * upon success.
- */
- int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return -EINVAL;
- if (!(pwrdm->pwrsts & (1 << pwrst)))
- return -EINVAL;
- pr_debug("powerdomain: %s: setting next powerstate to %0x\n",
- pwrdm->name, pwrst);
- if (arch_pwrdm && arch_pwrdm->pwrdm_set_next_pwrst) {
- /* Trace the pwrdm desired target state */
- trace_power_domain_target_rcuidle(pwrdm->name, pwrst,
- smp_processor_id());
- /* Program the pwrdm desired target state */
- ret = arch_pwrdm->pwrdm_set_next_pwrst(pwrdm, pwrst);
- }
- return ret;
- }
- /**
- * pwrdm_read_next_pwrst - get next powerdomain power state
- * @pwrdm: struct powerdomain * to get power state
- *
- * Return the powerdomain @pwrdm's next power state. Returns -EINVAL
- * if the powerdomain pointer is null or returns the next power state
- * upon success.
- */
- int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return -EINVAL;
- if (arch_pwrdm && arch_pwrdm->pwrdm_read_next_pwrst)
- ret = arch_pwrdm->pwrdm_read_next_pwrst(pwrdm);
- return ret;
- }
- /**
- * pwrdm_read_pwrst - get current powerdomain power state
- * @pwrdm: struct powerdomain * to get power state
- *
- * Return the powerdomain @pwrdm's current power state. Returns -EINVAL
- * if the powerdomain pointer is null or returns the current power state
- * upon success. Note that if the power domain only supports the ON state
- * then just return ON as the current state.
- */
- int pwrdm_read_pwrst(struct powerdomain *pwrdm)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return -EINVAL;
- if (pwrdm->pwrsts == PWRSTS_ON)
- return PWRDM_POWER_ON;
- if (arch_pwrdm && arch_pwrdm->pwrdm_read_pwrst)
- ret = arch_pwrdm->pwrdm_read_pwrst(pwrdm);
- return ret;
- }
- /**
- * pwrdm_read_prev_pwrst - get previous powerdomain power state
- * @pwrdm: struct powerdomain * to get previous power state
- *
- * Return the powerdomain @pwrdm's previous power state. Returns -EINVAL
- * if the powerdomain pointer is null or returns the previous power state
- * upon success.
- */
- int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return -EINVAL;
- if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_pwrst)
- ret = arch_pwrdm->pwrdm_read_prev_pwrst(pwrdm);
- return ret;
- }
- /**
- * pwrdm_set_logic_retst - set powerdomain logic power state upon retention
- * @pwrdm: struct powerdomain * to set
- * @pwrst: one of the PWRDM_POWER_* macros
- *
- * Set the next power state @pwrst that the logic portion of the
- * powerdomain @pwrdm will enter when the powerdomain enters retention.
- * This will be either RETENTION or OFF, if supported. Returns
- * -EINVAL if the powerdomain pointer is null or the target power
- * state is not not supported, or returns 0 upon success.
- */
- int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return -EINVAL;
- if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
- return -EINVAL;
- pr_debug("powerdomain: %s: setting next logic powerstate to %0x\n",
- pwrdm->name, pwrst);
- if (arch_pwrdm && arch_pwrdm->pwrdm_set_logic_retst)
- ret = arch_pwrdm->pwrdm_set_logic_retst(pwrdm, pwrst);
- return ret;
- }
- /**
- * pwrdm_set_mem_onst - set memory power state while powerdomain ON
- * @pwrdm: struct powerdomain * to set
- * @bank: memory bank number to set (0-3)
- * @pwrst: one of the PWRDM_POWER_* macros
- *
- * Set the next power state @pwrst that memory bank @bank of the
- * powerdomain @pwrdm will enter when the powerdomain enters the ON
- * state. @bank will be a number from 0 to 3, and represents different
- * types of memory, depending on the powerdomain. Returns -EINVAL if
- * the powerdomain pointer is null or the target power state is not
- * not supported for this memory bank, -EEXIST if the target memory
- * bank does not exist or is not controllable, or returns 0 upon
- * success.
- */
- int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return -EINVAL;
- if (pwrdm->banks < (bank + 1))
- return -EEXIST;
- if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
- return -EINVAL;
- pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-ON to %0x\n",
- pwrdm->name, bank, pwrst);
- if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_onst)
- ret = arch_pwrdm->pwrdm_set_mem_onst(pwrdm, bank, pwrst);
- return ret;
- }
- /**
- * pwrdm_set_mem_retst - set memory power state while powerdomain in RET
- * @pwrdm: struct powerdomain * to set
- * @bank: memory bank number to set (0-3)
- * @pwrst: one of the PWRDM_POWER_* macros
- *
- * Set the next power state @pwrst that memory bank @bank of the
- * powerdomain @pwrdm will enter when the powerdomain enters the
- * RETENTION state. Bank will be a number from 0 to 3, and represents
- * different types of memory, depending on the powerdomain. @pwrst
- * will be either RETENTION or OFF, if supported. Returns -EINVAL if
- * the powerdomain pointer is null or the target power state is not
- * not supported for this memory bank, -EEXIST if the target memory
- * bank does not exist or is not controllable, or returns 0 upon
- * success.
- */
- int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return -EINVAL;
- if (pwrdm->banks < (bank + 1))
- return -EEXIST;
- if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
- return -EINVAL;
- pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-RET to %0x\n",
- pwrdm->name, bank, pwrst);
- if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_retst)
- ret = arch_pwrdm->pwrdm_set_mem_retst(pwrdm, bank, pwrst);
- return ret;
- }
- /**
- * pwrdm_read_logic_pwrst - get current powerdomain logic retention power state
- * @pwrdm: struct powerdomain * to get current logic retention power state
- *
- * Return the power state that the logic portion of powerdomain @pwrdm
- * will enter when the powerdomain enters retention. Returns -EINVAL
- * if the powerdomain pointer is null or returns the logic retention
- * power state upon success.
- */
- int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return -EINVAL;
- if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_pwrst)
- ret = arch_pwrdm->pwrdm_read_logic_pwrst(pwrdm);
- return ret;
- }
- /**
- * pwrdm_read_prev_logic_pwrst - get previous powerdomain logic power state
- * @pwrdm: struct powerdomain * to get previous logic power state
- *
- * Return the powerdomain @pwrdm's previous logic power state. Returns
- * -EINVAL if the powerdomain pointer is null or returns the previous
- * logic power state upon success.
- */
- int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return -EINVAL;
- if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_logic_pwrst)
- ret = arch_pwrdm->pwrdm_read_prev_logic_pwrst(pwrdm);
- return ret;
- }
- /**
- * pwrdm_read_logic_retst - get next powerdomain logic power state
- * @pwrdm: struct powerdomain * to get next logic power state
- *
- * Return the powerdomain pwrdm's logic power state. Returns -EINVAL
- * if the powerdomain pointer is null or returns the next logic
- * power state upon success.
- */
- int pwrdm_read_logic_retst(struct powerdomain *pwrdm)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return -EINVAL;
- if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_retst)
- ret = arch_pwrdm->pwrdm_read_logic_retst(pwrdm);
- return ret;
- }
- /**
- * pwrdm_read_mem_pwrst - get current memory bank power state
- * @pwrdm: struct powerdomain * to get current memory bank power state
- * @bank: memory bank number (0-3)
- *
- * Return the powerdomain @pwrdm's current memory power state for bank
- * @bank. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
- * the target memory bank does not exist or is not controllable, or
- * returns the current memory power state upon success.
- */
- int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return ret;
- if (pwrdm->banks < (bank + 1))
- return ret;
- if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
- bank = 1;
- if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_pwrst)
- ret = arch_pwrdm->pwrdm_read_mem_pwrst(pwrdm, bank);
- return ret;
- }
- /**
- * pwrdm_read_prev_mem_pwrst - get previous memory bank power state
- * @pwrdm: struct powerdomain * to get previous memory bank power state
- * @bank: memory bank number (0-3)
- *
- * Return the powerdomain @pwrdm's previous memory power state for
- * bank @bank. Returns -EINVAL if the powerdomain pointer is null,
- * -EEXIST if the target memory bank does not exist or is not
- * controllable, or returns the previous memory power state upon
- * success.
- */
- int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return ret;
- if (pwrdm->banks < (bank + 1))
- return ret;
- if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
- bank = 1;
- if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_mem_pwrst)
- ret = arch_pwrdm->pwrdm_read_prev_mem_pwrst(pwrdm, bank);
- return ret;
- }
- /**
- * pwrdm_read_mem_retst - get next memory bank power state
- * @pwrdm: struct powerdomain * to get mext memory bank power state
- * @bank: memory bank number (0-3)
- *
- * Return the powerdomain pwrdm's next memory power state for bank
- * x. Returns -EINVAL if the powerdomain pointer is null, -EEXIST if
- * the target memory bank does not exist or is not controllable, or
- * returns the next memory power state upon success.
- */
- int pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return ret;
- if (pwrdm->banks < (bank + 1))
- return ret;
- if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_retst)
- ret = arch_pwrdm->pwrdm_read_mem_retst(pwrdm, bank);
- return ret;
- }
- /**
- * pwrdm_clear_all_prev_pwrst - clear previous powerstate register for a pwrdm
- * @pwrdm: struct powerdomain * to clear
- *
- * Clear the powerdomain's previous power state register @pwrdm.
- * Clears the entire register, including logic and memory bank
- * previous power states. Returns -EINVAL if the powerdomain pointer
- * is null, or returns 0 upon success.
- */
- int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return ret;
- /*
- * XXX should get the powerdomain's current state here;
- * warn & fail if it is not ON.
- */
- pr_debug("powerdomain: %s: clearing previous power state reg\n",
- pwrdm->name);
- if (arch_pwrdm && arch_pwrdm->pwrdm_clear_all_prev_pwrst)
- ret = arch_pwrdm->pwrdm_clear_all_prev_pwrst(pwrdm);
- return ret;
- }
- /**
- * pwrdm_enable_hdwr_sar - enable automatic hardware SAR for a pwrdm
- * @pwrdm: struct powerdomain *
- *
- * Enable automatic context save-and-restore upon power state change
- * for some devices in the powerdomain @pwrdm. Warning: this only
- * affects a subset of devices in a powerdomain; check the TRM
- * closely. Returns -EINVAL if the powerdomain pointer is null or if
- * the powerdomain does not support automatic save-and-restore, or
- * returns 0 upon success.
- */
- int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return ret;
- if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
- return ret;
- pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n", pwrdm->name);
- if (arch_pwrdm && arch_pwrdm->pwrdm_enable_hdwr_sar)
- ret = arch_pwrdm->pwrdm_enable_hdwr_sar(pwrdm);
- return ret;
- }
- /**
- * pwrdm_disable_hdwr_sar - disable automatic hardware SAR for a pwrdm
- * @pwrdm: struct powerdomain *
- *
- * Disable automatic context save-and-restore upon power state change
- * for some devices in the powerdomain @pwrdm. Warning: this only
- * affects a subset of devices in a powerdomain; check the TRM
- * closely. Returns -EINVAL if the powerdomain pointer is null or if
- * the powerdomain does not support automatic save-and-restore, or
- * returns 0 upon success.
- */
- int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
- {
- int ret = -EINVAL;
- if (!pwrdm)
- return ret;
- if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
- return ret;
- pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n", pwrdm->name);
- if (arch_pwrdm && arch_pwrdm->pwrdm_disable_hdwr_sar)
- ret = arch_pwrdm->pwrdm_disable_hdwr_sar(pwrdm);
- return ret;
- }
- /**
- * pwrdm_has_hdwr_sar - test whether powerdomain supports hardware SAR
- * @pwrdm: struct powerdomain *
- *
- * Returns 1 if powerdomain @pwrdm supports hardware save-and-restore
- * for some devices, or 0 if it does not.
- */
- bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
- {
- return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
- }
- int pwrdm_state_switch_nolock(struct powerdomain *pwrdm)
- {
- int ret;
- if (!pwrdm || !arch_pwrdm)
- return -EINVAL;
- ret = arch_pwrdm->pwrdm_wait_transition(pwrdm);
- if (!ret)
- ret = _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
- return ret;
- }
- int __deprecated pwrdm_state_switch(struct powerdomain *pwrdm)
- {
- int ret;
- pwrdm_lock(pwrdm);
- ret = pwrdm_state_switch_nolock(pwrdm);
- pwrdm_unlock(pwrdm);
- return ret;
- }
- int pwrdm_pre_transition(struct powerdomain *pwrdm)
- {
- if (pwrdm)
- _pwrdm_pre_transition_cb(pwrdm, NULL);
- else
- pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
- return 0;
- }
- int pwrdm_post_transition(struct powerdomain *pwrdm)
- {
- if (pwrdm)
- _pwrdm_post_transition_cb(pwrdm, NULL);
- else
- pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
- return 0;
- }
- /**
- * pwrdm_get_valid_lp_state() - Find best match deep power state
- * @pwrdm: power domain for which we want to find best match
- * @is_logic_state: Are we looking for logic state match here? Should
- * be one of PWRDM_xxx macro values
- * @req_state: requested power state
- *
- * Returns: closest match for requested power state. default fallback
- * is RET for logic state and ON for power state.
- *
- * This does a search from the power domain data looking for the
- * closest valid power domain state that the hardware can achieve.
- * PRCM definitions for PWRSTCTRL allows us to program whatever
- * configuration we'd like, and PRCM will actually attempt such
- * a transition, however if the powerdomain does not actually support it,
- * we endup with a hung system. The valid power domain states are already
- * available in our powerdomain data files. So this function tries to do
- * the following:
- * a) find if we have an exact match to the request - no issues.
- * b) else find if a deeper power state is possible.
- * c) failing which, it tries to find closest higher power state for the
- * request.
- */
- u8 pwrdm_get_valid_lp_state(struct powerdomain *pwrdm,
- bool is_logic_state, u8 req_state)
- {
- u8 pwrdm_states = is_logic_state ? pwrdm->pwrsts_logic_ret :
- pwrdm->pwrsts;
- /* For logic, ret is highest and others, ON is highest */
- u8 default_pwrst = is_logic_state ? PWRDM_POWER_RET : PWRDM_POWER_ON;
- u8 new_pwrst;
- bool found;
- /* If it is already supported, nothing to search */
- if (pwrdm_states & BIT(req_state))
- return req_state;
- if (!req_state)
- goto up_search;
- /*
- * So, we dont have a exact match
- * Can we get a deeper power state match?
- */
- new_pwrst = req_state - 1;
- found = true;
- while (!(pwrdm_states & BIT(new_pwrst))) {
- /* No match even at OFF? Not available */
- if (new_pwrst == PWRDM_POWER_OFF) {
- found = false;
- break;
- }
- new_pwrst--;
- }
- if (found)
- goto done;
- up_search:
- /* OK, no deeper ones, can we get a higher match? */
- new_pwrst = req_state + 1;
- while (!(pwrdm_states & BIT(new_pwrst))) {
- if (new_pwrst > PWRDM_POWER_ON) {
- WARN(1, "powerdomain: %s: Fix max powerstate to ON\n",
- pwrdm->name);
- return PWRDM_POWER_ON;
- }
- if (new_pwrst == default_pwrst)
- break;
- new_pwrst++;
- }
- done:
- return new_pwrst;
- }
- /**
- * omap_set_pwrdm_state - change a powerdomain's current power state
- * @pwrdm: struct powerdomain * to change the power state of
- * @pwrst: power state to change to
- *
- * Change the current hardware power state of the powerdomain
- * represented by @pwrdm to the power state represented by @pwrst.
- * Returns -EINVAL if @pwrdm is null or invalid or if the
- * powerdomain's current power state could not be read, or returns 0
- * upon success or if @pwrdm does not support @pwrst or any
- * lower-power state. XXX Should not return 0 if the @pwrdm does not
- * support @pwrst or any lower-power state: this should be an error.
- */
- int omap_set_pwrdm_state(struct powerdomain *pwrdm, u8 pwrst)
- {
- u8 next_pwrst, sleep_switch;
- int curr_pwrst;
- int ret = 0;
- if (!pwrdm || IS_ERR(pwrdm))
- return -EINVAL;
- while (!(pwrdm->pwrsts & (1 << pwrst))) {
- if (pwrst == PWRDM_POWER_OFF)
- return ret;
- pwrst--;
- }
- pwrdm_lock(pwrdm);
- curr_pwrst = pwrdm_read_pwrst(pwrdm);
- if (curr_pwrst < 0) {
- ret = -EINVAL;
- goto osps_out;
- }
- next_pwrst = pwrdm_read_next_pwrst(pwrdm);
- if (curr_pwrst == pwrst && next_pwrst == pwrst)
- goto osps_out;
- sleep_switch = _pwrdm_save_clkdm_state_and_activate(pwrdm, curr_pwrst,
- pwrst);
- ret = pwrdm_set_next_pwrst(pwrdm, pwrst);
- if (ret)
- pr_err("%s: unable to set power state of powerdomain: %s\n",
- __func__, pwrdm->name);
- _pwrdm_restore_clkdm_state(pwrdm, sleep_switch);
- osps_out:
- pwrdm_unlock(pwrdm);
- return ret;
- }
- /**
- * pwrdm_get_context_loss_count - get powerdomain's context loss count
- * @pwrdm: struct powerdomain * to wait for
- *
- * Context loss count is the sum of powerdomain off-mode counter, the
- * logic off counter and the per-bank memory off counter. Returns negative
- * (and WARNs) upon error, otherwise, returns the context loss count.
- */
- int pwrdm_get_context_loss_count(struct powerdomain *pwrdm)
- {
- int i, count;
- if (!pwrdm) {
- WARN(1, "powerdomain: %s: pwrdm is null\n", __func__);
- return -ENODEV;
- }
- count = pwrdm->state_counter[PWRDM_POWER_OFF];
- count += pwrdm->ret_logic_off_counter;
- for (i = 0; i < pwrdm->banks; i++)
- count += pwrdm->ret_mem_off_counter[i];
- /*
- * Context loss count has to be a non-negative value. Clear the sign
- * bit to get a value range from 0 to INT_MAX.
- */
- count &= INT_MAX;
- pr_debug("powerdomain: %s: context loss count = %d\n",
- pwrdm->name, count);
- return count;
- }
- /**
- * pwrdm_can_ever_lose_context - can this powerdomain ever lose context?
- * @pwrdm: struct powerdomain *
- *
- * Given a struct powerdomain * @pwrdm, returns 1 if the powerdomain
- * can lose either memory or logic context or if @pwrdm is invalid, or
- * returns 0 otherwise. This function is not concerned with how the
- * powerdomain registers are programmed (i.e., to go off or not); it's
- * concerned with whether it's ever possible for this powerdomain to
- * go off while some other part of the chip is active. This function
- * assumes that every powerdomain can go to either ON or INACTIVE.
- */
- bool pwrdm_can_ever_lose_context(struct powerdomain *pwrdm)
- {
- int i;
- if (!pwrdm) {
- pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
- __func__);
- return 1;
- }
- if (pwrdm->pwrsts & PWRSTS_OFF)
- return 1;
- if (pwrdm->pwrsts & PWRSTS_RET) {
- if (pwrdm->pwrsts_logic_ret & PWRSTS_OFF)
- return 1;
- for (i = 0; i < pwrdm->banks; i++)
- if (pwrdm->pwrsts_mem_ret[i] & PWRSTS_OFF)
- return 1;
- }
- for (i = 0; i < pwrdm->banks; i++)
- if (pwrdm->pwrsts_mem_on[i] & PWRSTS_OFF)
- return 1;
- return 0;
- }
|