2011-09-04 21:24:27 +02:00
|
|
|
/****************************************************************************
|
2012-01-22 17:42:49 +01:00
|
|
|
* drivers/power/pm_update.c
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
2018-11-10 14:30:23 +01:00
|
|
|
* Copyright (C) 2011-2012, 2016, 2018 Gregory Nutt. All rights reserved.
|
2012-01-22 17:42:49 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2016-03-27 21:03:47 +02:00
|
|
|
#include <stdint.h>
|
2011-09-04 21:24:27 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2012-01-22 17:42:49 +01:00
|
|
|
#include <nuttx/power/pm.h>
|
2011-09-04 21:24:27 +02:00
|
|
|
#include <nuttx/wqueue.h>
|
|
|
|
|
2015-12-30 01:29:24 +01:00
|
|
|
#include "pm.h"
|
2011-09-04 21:24:27 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_PM
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2018-11-10 14:30:23 +01:00
|
|
|
|
2011-09-04 21:24:27 +02:00
|
|
|
/* CONFIG_PM_MEMORY is the total number of time slices (including the current
|
2018-11-10 14:30:23 +01:00
|
|
|
* time slice). The history of previous values is then CONFIG_PM_MEMORY-1.
|
2011-09-04 21:24:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_PM_MEMORY > 1
|
2019-10-29 01:28:41 +01:00
|
|
|
static const int16_t g_pmcoeffs[CONFIG_PM_MEMORY - 1] =
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
|
|
|
CONFIG_PM_COEF1
|
|
|
|
#if CONFIG_PM_MEMORY > 2
|
|
|
|
, CONFIG_PM_COEF2
|
|
|
|
#endif
|
|
|
|
#if CONFIG_PM_MEMORY > 3
|
|
|
|
, CONFIG_PM_COEF3
|
|
|
|
#endif
|
|
|
|
#if CONFIG_PM_MEMORY > 4
|
|
|
|
, CONFIG_PM_COEF4
|
|
|
|
#endif
|
|
|
|
#if CONFIG_PM_MEMORY > 5
|
|
|
|
, CONFIG_PM_COEF5
|
|
|
|
#endif
|
|
|
|
#if CONFIG_PM_MEMORY > 6
|
|
|
|
# warning "This logic needs to be extended"
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2011-09-05 16:58:36 +02:00
|
|
|
/* Threshold activity values to enter into the next lower power consumption
|
|
|
|
* state. Indexing is next state 0:IDLE, 1:STANDBY, 2:SLEEP.
|
|
|
|
*/
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2019-08-26 17:51:40 +02:00
|
|
|
static const int32_t g_pmenterthresh[3] =
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
|
|
|
CONFIG_PM_IDLEENTER_THRESH,
|
|
|
|
CONFIG_PM_STANDBYENTER_THRESH,
|
|
|
|
CONFIG_PM_SLEEPENTER_THRESH
|
|
|
|
};
|
|
|
|
|
2011-09-05 16:58:36 +02:00
|
|
|
/* Threshold activity values to leave the current low power consdumption
|
|
|
|
* state. Indexing is current state 0:IDLE, 1: STANDBY, 2: SLEEP.
|
|
|
|
*/
|
|
|
|
|
2019-08-26 17:51:40 +02:00
|
|
|
static const int32_t g_pmexitthresh[3] =
|
2011-09-05 16:58:36 +02:00
|
|
|
{
|
|
|
|
CONFIG_PM_IDLEEXIT_THRESH,
|
|
|
|
CONFIG_PM_STANDBYEXIT_THRESH,
|
|
|
|
CONFIG_PM_SLEEPEXIT_THRESH
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Threshold time slice count to enter the next low power consdumption
|
|
|
|
* state. Indexing is next state 0:IDLE, 1: STANDBY, 2: SLEEP.
|
|
|
|
*/
|
|
|
|
|
2019-08-26 17:51:40 +02:00
|
|
|
static const uint32_t g_pmcount[3] =
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
|
|
|
CONFIG_PM_IDLEENTER_COUNT,
|
|
|
|
CONFIG_PM_STANDBYENTER_COUNT,
|
|
|
|
CONFIG_PM_SLEEPENTER_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-08-27 21:20:28 +02:00
|
|
|
* Public Functions
|
2011-09-04 21:24:27 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-08-27 21:20:28 +02:00
|
|
|
* Name: pm_update
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-08-27 21:20:28 +02:00
|
|
|
* This internal function is called at the end of a time slice in order to
|
2011-09-04 21:24:27 +02:00
|
|
|
* update driver activity metrics and recommended states.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-08-27 21:20:28 +02:00
|
|
|
* domain - The PM domain associated with the accumulator
|
2018-11-10 14:30:23 +01:00
|
|
|
* accum - The value of the activity accumulator at the end of the time
|
|
|
|
* slice.
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2018-08-27 21:20:28 +02:00
|
|
|
* This function may be called from a driver, perhaps even at the interrupt
|
|
|
|
* level. It may also be called from the IDLE loop at the lowest possible
|
|
|
|
* priority level.
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
void pm_update(int domain, int16_t accum)
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
2016-03-27 21:03:47 +02:00
|
|
|
FAR struct pm_domain_s *pdom;
|
2011-09-05 16:58:36 +02:00
|
|
|
int32_t Y;
|
|
|
|
int index;
|
2011-09-04 21:24:27 +02:00
|
|
|
#if CONFIG_PM_MEMORY > 1
|
2011-09-05 16:58:36 +02:00
|
|
|
int32_t denom;
|
2016-03-27 21:03:47 +02:00
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get a convenience pointer to minimize all of the indexing */
|
|
|
|
|
2018-08-27 21:20:28 +02:00
|
|
|
DEBUGASSERT(domain >= 0 && domain < CONFIG_PM_NDOMAINS);
|
|
|
|
pdom = &g_pmglobals.domain[domain];
|
2016-03-27 21:03:47 +02:00
|
|
|
|
2018-08-27 21:28:48 +02:00
|
|
|
#if CONFIG_PM_MEMORY > 1
|
2018-11-10 14:30:23 +01:00
|
|
|
/* We won't bother to do anything until we have accumulated
|
|
|
|
* CONFIG_PM_MEMORY-1 samples.
|
|
|
|
*/
|
2018-08-27 21:28:48 +02:00
|
|
|
|
2019-10-29 01:28:41 +01:00
|
|
|
if (pdom->mcnt < CONFIG_PM_MEMORY - 1)
|
2018-11-10 14:30:23 +01:00
|
|
|
{
|
|
|
|
index = pdom->mcnt++;
|
|
|
|
pdom->memory[index] = accum;
|
|
|
|
return;
|
|
|
|
}
|
2016-03-27 21:03:47 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
/* The averaging algorithm is simply: Y = (An*X + SUM(Ai*Yi))/SUM(Aj), where
|
|
|
|
* i = 1..n-1 and j= 1..n, n is the length of the "memory", Ai is the
|
|
|
|
* weight applied to each value, and X is the current activity.
|
|
|
|
*
|
|
|
|
* CONFIG_PM_MEMORY provides the memory for the algorithm. Default: 2
|
|
|
|
* CONFIG_PM_COEFn provides weight for each sample. Default: 1
|
|
|
|
*
|
|
|
|
* First, calculate Y = An*X
|
|
|
|
*/
|
|
|
|
|
|
|
|
Y = CONFIG_PM_COEFN * accum;
|
|
|
|
denom = CONFIG_PM_COEFN;
|
|
|
|
|
|
|
|
/* Then calculate Y += SUM(Ai*Yi), i = 1..n-1. The oldest sample will
|
|
|
|
* reside at the domain's mndx (and this is the value that we will overwrite
|
|
|
|
* with the new value).
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0, j = pdom->mndx;
|
2019-10-29 01:28:41 +01:00
|
|
|
i < CONFIG_PM_MEMORY - 1;
|
2018-11-10 14:30:23 +01:00
|
|
|
i++, j++)
|
|
|
|
{
|
2019-10-29 01:28:41 +01:00
|
|
|
if (j >= CONFIG_PM_MEMORY - 1)
|
2018-08-27 21:28:48 +02:00
|
|
|
{
|
2018-11-10 14:30:23 +01:00
|
|
|
j = 0;
|
2018-08-27 21:28:48 +02:00
|
|
|
}
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
Y += g_pmcoeffs[i] * pdom->memory[j];
|
|
|
|
denom += g_pmcoeffs[i];
|
|
|
|
}
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
/* Compute and save the new activity value */
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
Y /= denom;
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
index = pdom->mndx++;
|
|
|
|
pdom->memory[index] = Y;
|
2019-10-29 01:28:41 +01:00
|
|
|
if (pdom->mndx >= CONFIG_PM_MEMORY - 1)
|
2018-11-10 14:30:23 +01:00
|
|
|
{
|
|
|
|
pdom->mndx = 0;
|
|
|
|
}
|
2018-08-27 21:28:48 +02:00
|
|
|
#else
|
2011-09-05 16:58:36 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
/* No smoothing */
|
2011-09-05 16:58:36 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
Y = accum;
|
2018-08-27 21:28:48 +02:00
|
|
|
#endif
|
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
/* First check if increased activity should cause us to return to the
|
|
|
|
* normal operating state. This would be unlikely for the lowest power
|
|
|
|
* consumption states because the CPU is probably asleep. However this
|
|
|
|
* probably does apply for the IDLE state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pdom->state > PM_NORMAL)
|
|
|
|
{
|
|
|
|
/* Get the table index for the current state (which will be the
|
|
|
|
* current state minus one)
|
2011-09-05 16:58:36 +02:00
|
|
|
*/
|
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
index = pdom->state - 1;
|
|
|
|
|
|
|
|
/* Has the threshold to return to normal power consumption state been
|
|
|
|
* exceeded?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (Y > g_pmexitthresh[index])
|
2011-09-05 16:58:36 +02:00
|
|
|
{
|
2018-11-10 14:30:23 +01:00
|
|
|
/* Yes... reset the count and recommend the normal state. */
|
2011-09-05 16:58:36 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
pdom->btime = clock_systimer();
|
|
|
|
pdom->recommended = PM_NORMAL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
/* Now, compare this new activity level to the thresholds and counts for
|
|
|
|
* the next lower power consumption state. If we are already in the SLEEP
|
|
|
|
* state, then there is nothing more to be done (in fact, I would be
|
|
|
|
* surprised to be executing!).
|
|
|
|
*/
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
if (pdom->state < PM_SLEEP)
|
|
|
|
{
|
|
|
|
unsigned int nextstate;
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
/* Get the next state and the table index for the next state (which will
|
|
|
|
* be the current state)
|
|
|
|
*/
|
|
|
|
|
|
|
|
index = pdom->state;
|
|
|
|
nextstate = pdom->state + 1;
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
/* Has the threshold to enter the next lower power consumption state
|
|
|
|
* been exceeded?
|
2011-09-05 16:58:36 +02:00
|
|
|
*/
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
if (Y > g_pmenterthresh[index])
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
2018-11-10 14:30:23 +01:00
|
|
|
/* No... reset the count and recommend the current state */
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
pdom->btime = clock_systimer();
|
|
|
|
pdom->recommended = pdom->state;
|
|
|
|
}
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
/* Yes.. have we already recommended this state? If so, do nothing */
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
else if (pdom->recommended < nextstate)
|
|
|
|
{
|
|
|
|
/* No.. calculate the count. Has it passed the count required
|
|
|
|
* for a state transition?
|
2011-09-04 21:24:27 +02:00
|
|
|
*/
|
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
if (clock_systimer() - pdom->btime >=
|
|
|
|
g_pmcount[index] * TIME_SLICE_TICKS)
|
2018-08-27 21:28:48 +02:00
|
|
|
{
|
2018-11-10 14:30:23 +01:00
|
|
|
/* Yes, recommend the new state and set up for the next
|
|
|
|
* transition.
|
2018-08-27 21:28:48 +02:00
|
|
|
*/
|
|
|
|
|
2018-11-10 14:30:23 +01:00
|
|
|
pdom->btime = clock_systimer();
|
|
|
|
pdom->recommended = nextstate;
|
2011-09-04 21:24:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-04 02:54:09 +02:00
|
|
|
#endif /* CONFIG_PM */
|