nuttx/drivers/power/pm/stability_governor.c
Xiang Xiao c47b1e2c5b !sys/types.h: change time_t and clock_t to int64_t to align with other OSes
POSIX leaves the signedness of time_t and clock_t unspecified, but
mainstream implementations (Linux glibc/musl, the BSDs, macOS, RTEMS,
Zephyr's POSIX layer, Windows _time64) expose time_t as signed 64-bit.
NuttX has historically used uint64_t only because it was tied to the
CONFIG_SYSTEM_TIME64 knob; with that gone, switch:

  time_t   : uint64_t  -> int64_t
  clock_t  : uint64_t  -> int64_t
  CLOCK_MAX: UINT64_MAX -> INT64_MAX

This lets (time_t)-1 sentinels, negative tick deltas, and host-side
headers behave as on every other POSIX system without source churn.

Headers updated:
  - include/sys/types.h, include/limits.h, include/nuttx/clock.h
  - include/nuttx/fs/hostfs.h (nuttx_time_t alias)
  - include/nuttx/{mqueue.h,wdog.h,wqueue.h,timers/clkcnt.h}

Because clock_t is now signed 64-bit, the NuttX-internal sclock_t
alias becomes redundant: every sclock_t/SCLOCK_MAX use is folded
back to clock_t/CLOCK_MAX (notably in sched/wdog, sched/mqueue,
sched/sched, sched/clock, sched/timer, libs/libc/time, fs/vfs and
the drivers/arch consumers below).

Tick/period constants (NSEC_PER_SEC, USEC_PER_SEC, MSEC_PER_SEC,
SEC_PER_MIN, ...) in include/nuttx/clock.h are retyped from "long"
literals to INT64_C(...) so that 64-bit arithmetic no longer
depends on the host's long width.

Strip now-redundant (time_t)/(clock_t)/(unsigned long) casts and
unsigned-only branches across the tree:
  - arch RTC / oneshot / tickless lowerhalfs:
      arm: cxd56xx, efm32, imxrt, lc823450, max326xx, sam34, sama5,
           samd5e5, samv7, stm32, stm32f7, stm32h7, stm32l4, stm32wb,
           xmc4
      mips: pic32mz       sparc: bm3803       x86_64: intel64
      risc-v/xtensa: espressif (esp_i2c[_slave], esp_rtc,
           esp32c3{_i2c,_rtc,_wifi_adapter}, esp32{,s2,s3}_*),
           mpfs_perf
  - drivers: audio/tone, input/aw86225, power/pm/{activity,
           stability}_governor, rpmsg/rpmsg_ping,
           timers/{ds3231,mcp794xx,pcf85263,rx8010},
           wireless/ieee80211/bcm43xxx, wireless/spirit/spirit_spi
  - core: fs/vfs/{fs_poll,fs_timerfd}, mm/iob/iob_alloc,
          libs/libc/{netdb/lib_dnscache,time/{lib_calendar2utc,
          lib_time}}, net/icmp/icmp_pmtu, net/icmpv6/icmpv6_pmtu,
          net/ipfrag, net/tcp/{tcp.h,tcp_timer},
          net/utils/net_snoop, net/mld/mld_query (drop the now-dead
          mld_mrc2mrd helper since signed math handles it directly),
          sched/clock/{clock,clock_initialize},
          sched/sched/{sched_profil,sched_setparam,sched_setscheduler},
          sched/pthread/pthread_create,
          sched/wdog/{wd_gettime,wd_start,wdog.h},
          sched/timer/timer_gettime, sched/mqueue/*

Flip the few in-tree printf format strings that assumed an
unsigned 64-bit tv_sec:
  * drivers/rpmsg/rpmsg_ping.c                       PRIu64 -> PRId64
  * arch/xtensa/src/esp32{,s2,s3}/esp32*_oneshot_lowerhalf.c
                                          PRIu32 (already wrong) -> PRId64

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2026-05-19 16:21:28 +08:00

241 lines
7.4 KiB
C

/****************************************************************************
* drivers/power/pm/stability_governor.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/clock.h>
#include <nuttx/config.h>
#include <nuttx/wdog.h>
#include <sys/types.h>
#include <stdbool.h>
#include <nuttx/power/pm.h>
#include "pm.h"
/****************************************************************************
* Private Types
****************************************************************************/
struct pm_stability_governor_domain_s
{
/* Timer to wakeup system, delay the sleep request */
struct wdog_s wdog;
/* The Idle is wakeup from the governor wdog itself */
bool wdog_wakeup;
/* This state has not been maintained long enough to meet the threshold. */
enum pm_state_e state_pending;
};
struct pm_stability_governor_s
{
struct pm_stability_governor_domain_s domain[CONFIG_PM_NDOMAINS];
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* PM governor methods */
static void stability_governor_statechanged(int domain,
enum pm_state_e newstate);
static enum pm_state_e stability_governor_checkstate(int domain);
static void stability_governor_activity(int domain, int count);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct pm_governor_s g_stability_governor_ops =
{
NULL, /* initialize */
NULL, /* deinitialize */
stability_governor_statechanged, /* statechanged */
stability_governor_checkstate, /* checkstate */
stability_governor_activity, /* activity */
NULL /* priv */
};
static const clock_t g_stability_governor_thresh[PM_COUNT] =
{
0,
CONFIG_PM_GOVERNOR_STABILITY_IDLE_THRESH,
CONFIG_PM_GOVERNOR_STABILITY_STANDBY_THRESH,
CONFIG_PM_GOVERNOR_STABILITY_SLEEP_THRESH,
};
static struct pm_stability_governor_s g_stability_governor;
/****************************************************************************
* Private Functions
****************************************************************************/
/* Timer cb only to make sure system will wake from WFI */
static void stability_governor_timer_cb(wdparm_t arg)
{
}
/****************************************************************************
* Name: stability_governor_statechanged
****************************************************************************/
static void stability_governor_statechanged(int domain,
enum pm_state_e newstate)
{
if (newstate == PM_RESTORE)
{
if (WDOG_ISACTIVE(&g_stability_governor.domain[domain].wdog))
{
clock_t left;
/* The left tick from wdog, if >0 should be other irq source */
left = wd_gettime(&g_stability_governor.domain[domain].wdog);
if (left <= 0)
{
g_stability_governor.domain[domain].wdog_wakeup = true;
}
/* Don't have to execute callback */
wd_cancel(&g_stability_governor.domain[domain].wdog);
}
}
else
{
enum pm_state_e state;
clock_t thresh;
state = g_stability_governor.domain[domain].state_pending;
thresh = g_stability_governor_thresh[state];
if (thresh > 0 && state != newstate)
{
wd_start(&g_stability_governor.domain[domain].wdog, thresh,
stability_governor_timer_cb, 0);
}
}
}
/****************************************************************************
* Name: user_governor_checkstate
****************************************************************************/
static enum pm_state_e stability_governor_checkstate(int domain)
{
FAR struct pm_stability_governor_domain_s *gdom;
FAR struct pm_domain_s *pdom;
enum pm_state_e state_pending;
enum pm_state_e state;
irqstate_t flags;
bool wdog_wakeup;
gdom = &g_stability_governor.domain[domain];
pdom = &g_pmdomains[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
*/
flags = spin_lock_irqsave(&pdom->lock);
/* Find the lowest power-level which is not locked. */
while (dq_empty(&pdom->wakelock[state]) && state < (PM_COUNT - 1))
{
state++;
}
state_pending = gdom->state_pending;
wdog_wakeup = gdom->wdog_wakeup;
gdom->state_pending = state;
gdom->wdog_wakeup = false;
/* If pm stability check disabled state or pm stable enough, do nothing */
if (g_stability_governor_thresh[state] > 0 &&
(!wdog_wakeup || state_pending != state))
{
state = pdom->state;
if (g_stability_governor_thresh[state] > 0)
{
/* The domain last state can not be backward, need to holding
* to the lowest power-level with stability check disabled
*/
for (; state > PM_NORMAL; state--)
{
if (g_stability_governor_thresh[state] == 0)
{
break;
}
}
}
}
spin_unlock_irqrestore(&pdom->lock, flags);
/* Return the found state */
return state;
}
/****************************************************************************
* Name: greedy_activity
****************************************************************************/
static void stability_governor_activity(int domain, int count)
{
pm_staytimeout(domain, PM_NORMAL, (count ? count : 1) * 1000);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pm_stability_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_stability_governor_initialize(void)
{
return &g_stability_governor_ops;
}