2019-11-09 16:09:33 +01:00
|
|
|
/****************************************************************************
|
2022-08-01 11:31:49 +02:00
|
|
|
* drivers/power/pm/greedy_governor.c
|
2019-11-09 16:09:33 +01:00
|
|
|
*
|
2021-03-31 11:15:31 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-11-09 16:09:33 +01:00
|
|
|
*
|
2021-03-31 11:15:31 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-11-09 16:09:33 +01:00
|
|
|
*
|
2021-03-31 11:15:31 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-11-09 16:09:33 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/signal.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/power/pm.h>
|
|
|
|
#include <nuttx/power/power_ioctl.h>
|
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
|
|
|
|
#include "pm.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-06-10 10:07:37 +02:00
|
|
|
struct pm_domain_state_s
|
|
|
|
{
|
|
|
|
struct wdog_s wdog;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pm_greedy_governor_s
|
|
|
|
{
|
|
|
|
struct pm_domain_state_s domain_states[CONFIG_PM_NDOMAINS];
|
|
|
|
};
|
|
|
|
|
2019-11-09 16:09:33 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* PM governor methods */
|
|
|
|
|
2022-07-22 06:10:50 +02:00
|
|
|
static void greedy_governor_statechanged(int domain,
|
|
|
|
enum pm_state_e newstate);
|
|
|
|
static enum pm_state_e greedy_governor_checkstate(int domain);
|
|
|
|
static void greedy_governor_activity(int domain, int count);
|
2019-11-09 16:09:33 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-14 07:42:02 +01:00
|
|
|
static const struct pm_governor_s g_greedy_governor_ops =
|
2019-11-09 16:09:33 +01:00
|
|
|
{
|
2022-07-22 06:10:50 +02:00
|
|
|
NULL, /* initialize */
|
|
|
|
NULL, /* deinitialize */
|
|
|
|
greedy_governor_statechanged, /* statechanged */
|
|
|
|
greedy_governor_checkstate, /* checkstate */
|
|
|
|
greedy_governor_activity, /* activity */
|
|
|
|
NULL /* priv */
|
2019-11-09 16:09:33 +01:00
|
|
|
};
|
|
|
|
|
2022-06-10 10:07:37 +02:00
|
|
|
static struct pm_greedy_governor_s g_pm_greedy_governor;
|
|
|
|
|
2019-11-09 16:09:33 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: greedy_governor_statechanged
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-01 08:20:14 +02:00
|
|
|
static void greedy_governor_statechanged(int domain,
|
|
|
|
enum pm_state_e newstate)
|
2019-11-09 16:09:33 +01:00
|
|
|
{
|
|
|
|
/* no need to react to state changes */
|
|
|
|
|
|
|
|
UNUSED(domain);
|
|
|
|
UNUSED(newstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: user_governor_checkstate
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static enum pm_state_e greedy_governor_checkstate(int domain)
|
|
|
|
{
|
2022-06-10 10:07:37 +02:00
|
|
|
FAR struct pm_domain_state_s *pdomstate;
|
2019-11-09 16:09:33 +01:00
|
|
|
FAR struct pm_domain_s *pdom;
|
|
|
|
irqstate_t flags;
|
2022-06-10 10:07:37 +02:00
|
|
|
int state;
|
2019-11-09 16:09:33 +01:00
|
|
|
|
2022-06-10 10:07:37 +02:00
|
|
|
pdomstate = &g_pm_greedy_governor.domain_states[domain];
|
2019-11-09 16:09:33 +01:00
|
|
|
pdom = &g_pmglobals.domain[domain];
|
|
|
|
state = PM_NORMAL;
|
|
|
|
|
|
|
|
/* We disable interrupts since pm_stay()/pm_relax() could be simultaneously
|
|
|
|
* invoked, which modifies the stay count which we are about to read
|
|
|
|
*/
|
|
|
|
|
2022-12-30 06:42:31 +01:00
|
|
|
flags = pm_domain_lock(domain);
|
2019-11-09 16:09:33 +01:00
|
|
|
|
2022-06-10 10:07:37 +02:00
|
|
|
if (!WDOG_ISACTIVE(&pdomstate->wdog))
|
2019-11-09 16:09:33 +01:00
|
|
|
{
|
2022-06-10 10:07:37 +02:00
|
|
|
/* Find the lowest power-level which is not locked. */
|
|
|
|
|
2022-07-25 08:36:44 +02:00
|
|
|
while (dq_empty(&pdom->wakelock[state]) && state < (PM_COUNT - 1))
|
2022-06-10 10:07:37 +02:00
|
|
|
{
|
|
|
|
state++;
|
|
|
|
}
|
2019-11-09 16:09:33 +01:00
|
|
|
}
|
|
|
|
|
2022-12-30 06:42:31 +01:00
|
|
|
pm_domain_unlock(domain, flags);
|
2019-11-09 16:09:33 +01:00
|
|
|
|
|
|
|
/* Return the found state */
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2022-06-10 10:07:37 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: governor_timer_cb
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void greedy_governor_timer_cb(wdparm_t arg)
|
|
|
|
{
|
2022-06-21 13:46:51 +02:00
|
|
|
pm_auto_updatestate((int)arg);
|
2022-06-10 10:07:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: greedy_activity
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void greedy_governor_activity(int domain, int count)
|
|
|
|
{
|
|
|
|
FAR struct pm_domain_state_s *pdomstate;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
pdomstate = &g_pm_greedy_governor.domain_states[domain];
|
|
|
|
count = count ? count : 1;
|
|
|
|
|
2022-12-30 06:42:31 +01:00
|
|
|
flags = pm_domain_lock(domain);
|
2022-06-10 10:07:37 +02:00
|
|
|
|
|
|
|
if (TICK2SEC(wd_gettime(&pdomstate->wdog)) < count)
|
|
|
|
{
|
|
|
|
wd_start(&pdomstate->wdog, SEC2TICK(count),
|
|
|
|
greedy_governor_timer_cb, (wdparm_t)domain);
|
|
|
|
}
|
|
|
|
|
2022-12-30 06:42:31 +01:00
|
|
|
pm_domain_unlock(domain, flags);
|
2022-06-10 10:07:37 +02:00
|
|
|
}
|
|
|
|
|
2019-11-09 16:09:33 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pm_greedy_governor_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the user_governor driver as the specified device.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success. Otherwise a negated errno value is
|
|
|
|
* returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR const struct pm_governor_s *pm_greedy_governor_initialize(void)
|
|
|
|
{
|
|
|
|
return &g_greedy_governor_ops;
|
|
|
|
}
|