los_pm.c 16.1 KB
Newer Older
Z
zhushengle 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
 *
 * 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 of the copyright holder 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 HOLDER 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.
 */

#include "los_pm.h"
#include "securec.h"
#include "los_sched_pri.h"
#include "los_init.h"
#include "los_memory.h"
#include "los_spinlock.h"
Z
zhushengle 已提交
38
#include "los_swtmr.h"
Z
zhushengle 已提交
39 40
#include "los_mp.h"

Z
zhushengle 已提交
41 42
#define OS_MS_PER_TICK (OS_SYS_MS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND)

Z
zhushengle 已提交
43 44 45 46 47 48 49 50 51
#ifdef LOSCFG_KERNEL_PM
#define PM_INFO_SHOW(seqBuf, arg...) do {                   \
    if (seqBuf != NULL) {                                   \
        (void)LosBufPrintf((struct SeqBuf *)seqBuf, ##arg); \
    } else {                                                \
        PRINTK(arg);                                        \
    }                                                       \
} while (0)

Z
zhushengle 已提交
52 53 54 55
#define OS_PM_LOCK_MAX         0xFFFFU
#define OS_PM_LOCK_NAME_MAX    28
#define OS_PM_SYS_EARLY        1
#define OS_PM_SYS_DEVICE_EARLY 2
Z
zhushengle 已提交
56

Z
zhushengle 已提交
57 58
typedef UINT32 (*SysSuspend)(VOID);
typedef UINT32 (*Suspend)(UINT32 mode);
Z
zhushengle 已提交
59 60 61 62

typedef struct {
    CHAR         name[OS_PM_LOCK_NAME_MAX];
    UINT32       count;
Z
zhushengle 已提交
63
    UINT32       swtmrID;
Z
zhushengle 已提交
64 65 66 67
    LOS_DL_LIST  list;
} OsPmLockCB;

typedef struct {
Z
zhushengle 已提交
68 69
    LOS_SysSleepEnum  pmMode;
    LOS_SysSleepEnum  sysMode;
Z
zhushengle 已提交
70
    UINT16            lock;
Z
zhushengle 已提交
71 72 73 74
    BOOL              isWake;
    LosPmDevice       *device;
    LosPmSysctrl      *sysctrl;
    UINT64            enterSleepTime;
Z
zhushengle 已提交
75 76 77
    LOS_DL_LIST       lockList;
} LosPmCB;

Z
zhushengle 已提交
78 79 80
#define PM_EVENT_LOCK_MASK    0xF
#define PM_EVENT_LOCK_RELEASE 0x1
STATIC EVENT_CB_S g_pmEvent;
Z
zhushengle 已提交
81 82 83
STATIC LosPmCB g_pmCB;
STATIC SPIN_LOCK_INIT(g_pmSpin);

Z
zhushengle 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
STATIC VOID OsPmTickTimerStart(LosPmCB *pm)
{
    (VOID)pm;
    return;
}

STATIC BOOL OsPmTickTimerStop(LosPmCB *pm)
{
    (VOID)pm;
    return FALSE;
}

STATIC VOID OsPmCpuResume(LosPmCB *pm)
{
    if ((pm->sysMode == LOS_SYS_NORMAL_SLEEP) && (pm->sysctrl->normalResume != NULL)) {
        pm->sysctrl->normalResume();
    } else if ((pm->sysMode == LOS_SYS_LIGHT_SLEEP) && (pm->sysctrl->lightResume != NULL)) {
        pm->sysctrl->lightResume();
    } else if ((pm->sysMode == LOS_SYS_DEEP_SLEEP) && (pm->sysctrl->deepResume != NULL)) {
        pm->sysctrl->deepResume();
    }
}

STATIC SysSuspend OsPmCpuSuspend(LosPmCB *pm)
{
    SysSuspend sysSuspend = NULL;

    /* cpu enter low power mode */
    LOS_ASSERT(pm->sysctrl != NULL);

    if (pm->sysMode == LOS_SYS_NORMAL_SLEEP) {
        sysSuspend = pm->sysctrl->normalSuspend;
    } else if (pm->sysMode == LOS_SYS_LIGHT_SLEEP) {
        sysSuspend = pm->sysctrl->lightSuspend;
    } else if (pm->sysMode == LOS_SYS_DEEP_SLEEP) {
        sysSuspend = pm->sysctrl->deepSuspend;
    } else {
        sysSuspend = pm->sysctrl->shutdownSuspend;
    }

    LOS_ASSERT(sysSuspend != NULL);

    return sysSuspend;
}

STATIC VOID OsPmResumePrepare(LosPmCB *pm, UINT32 mode, UINT32 prepare)
{
    if ((prepare == 0) && (pm->device->resume != NULL)) {
        pm->device->resume(mode);
    }

    if (((prepare == 0) || (prepare == OS_PM_SYS_DEVICE_EARLY)) && (pm->sysctrl->late != NULL)) {
        pm->sysctrl->late(mode);
    }
}

STATIC UINT32 OsPmSuspendPrepare(Suspend sysSuspendEarly, Suspend deviceSuspend, UINT32 mode, UINT32 *prepare)
{
    UINT32 ret;

    if (sysSuspendEarly != NULL) {
        ret = sysSuspendEarly(mode);
        if (ret != LOS_OK) {
            *prepare = OS_PM_SYS_EARLY;
            return ret;
        }
    }

    if (deviceSuspend != NULL) {
        ret = deviceSuspend(mode);
        if (ret != LOS_OK) {
            *prepare = OS_PM_SYS_DEVICE_EARLY;
            return ret;
        }
    }

    return LOS_OK;
}

STATIC UINT32 OsPmSuspendCheck(LosPmCB *pm, Suspend *sysSuspendEarly, Suspend *deviceSuspend, LOS_SysSleepEnum *mode)
{
    LOS_SpinLock(&g_pmSpin);
    pm->sysMode = pm->pmMode;
    if (pm->lock > 0) {
        pm->sysMode = LOS_SYS_NORMAL_SLEEP;
        LOS_SpinUnlock(&g_pmSpin);
        return LOS_NOK;
    }

    pm->isWake = FALSE;
    *mode = pm->sysMode;
    *sysSuspendEarly = pm->sysctrl->early;
    *deviceSuspend = pm->device->suspend;
    LOS_SpinUnlock(&g_pmSpin);
    return LOS_OK;
}

STATIC UINT32 OsPmSuspendSleep(LosPmCB *pm)
{
    UINT32 ret, intSave;
    Suspend sysSuspendEarly, deviceSuspend;
    LOS_SysSleepEnum mode;
    UINT32 prepare = 0;
    BOOL tickTimerStop = FALSE;
    SysSuspend sysSuspend;
    UINT64 currTime;

    ret = OsPmSuspendCheck(pm, &sysSuspendEarly, &deviceSuspend, &mode);
    if (ret != LOS_OK) {
        PRINT_ERR("Pm suspend mode is normal sleep! lock count %d\n", pm->lock);
        return ret;
    }

    ret = OsPmSuspendPrepare(sysSuspendEarly, deviceSuspend, (UINT32)mode, &prepare);
    if (ret != LOS_OK) {
        LOS_SpinLockSave(&g_pmSpin, &intSave);
        LOS_TaskLock();
        goto EXIT;
    }

    LOS_SpinLockSave(&g_pmSpin, &intSave);
    LOS_TaskLock();
    if (pm->isWake || (pm->lock > 0)) {
        goto EXIT;
    }

    tickTimerStop = OsPmTickTimerStop(pm);
    if (!tickTimerStop) {
        currTime = OsGetCurrSchedTimeCycle();
        OsSchedResetSchedResponseTime(0);
        OsSchedUpdateExpireTime(currTime);
    }

    sysSuspend = OsPmCpuSuspend(pm);
    LOS_SpinUnlockRestore(&g_pmSpin, intSave);

    if (!pm->isWake) {
        ret = sysSuspend();
    }

    LOS_SpinLockSave(&g_pmSpin, &intSave);

    OsPmCpuResume(pm);

    OsPmTickTimerStart(pm);

EXIT:
    pm->sysMode = LOS_SYS_NORMAL_SLEEP;
    OsPmResumePrepare(pm, (UINT32)mode, prepare);
    LOS_SpinUnlockRestore(&g_pmSpin, intSave);

    LOS_TaskUnlock();
    return ret;
}

STATIC UINT32 OsPmDeviceRegister(LosPmCB *pm, LosPmDevice *device)
{
    if ((device->suspend == NULL) || (device->resume == NULL)) {
        return LOS_EINVAL;
    }

    LOS_SpinLock(&g_pmSpin);
    pm->device = device;
    LOS_SpinUnlock(&g_pmSpin);

    return LOS_OK;
}

STATIC UINT32 OsPmSysctrlRegister(LosPmCB *pm, LosPmSysctrl *sysctrl)
{
    LOS_SpinLock(&g_pmSpin);
    pm->sysctrl = sysctrl;
    LOS_SpinUnlock(&g_pmSpin);

    return LOS_OK;
}

UINT32 LOS_PmRegister(LOS_PmNodeType type, VOID *node)
{
    LosPmCB *pm = &g_pmCB;

    if (node == NULL) {
        return LOS_EINVAL;
    }

    switch (type) {
        case LOS_PM_TYPE_DEVICE:
            return OsPmDeviceRegister(pm, (LosPmDevice *)node);
        case LOS_PM_TYPE_TICK_TIMER:
            PRINT_ERR("Pm, %d is an unsupported type\n", type);
            return LOS_EINVAL;
        case LOS_PM_TYPE_SYSCTRL:
            return OsPmSysctrlRegister(pm, (LosPmSysctrl *)node);
        default:
            break;
    }

    return LOS_EINVAL;
}

STATIC UINT32 OsPmDeviceUnregister(LosPmCB *pm, LosPmDevice *device)
{
    LOS_SpinLock(&g_pmSpin);
    if (pm->device == device) {
        pm->device = NULL;
        pm->pmMode = LOS_SYS_NORMAL_SLEEP;
        LOS_SpinUnlock(&g_pmSpin);
        return LOS_OK;
    }

    LOS_SpinUnlock(&g_pmSpin);
    return LOS_EINVAL;
}

STATIC UINT32 OsPmSysctrlUnregister(LosPmCB *pm, LosPmSysctrl *sysctrl)
{
    LOS_SpinLock(&g_pmSpin);
    pm->sysctrl = NULL;
    LOS_SpinUnlock(&g_pmSpin);

    return LOS_OK;
}

UINT32 LOS_PmUnregister(LOS_PmNodeType type, VOID *node)
{
    LosPmCB *pm = &g_pmCB;

    if (node == NULL) {
        return LOS_EINVAL;
    }

    switch (type) {
        case LOS_PM_TYPE_DEVICE:
            return OsPmDeviceUnregister(pm, (LosPmDevice *)node);
        case LOS_PM_TYPE_TICK_TIMER:
            PRINT_ERR("Pm, %d is an unsupported type\n", type);
            return LOS_EINVAL;
        case LOS_PM_TYPE_SYSCTRL:
            return OsPmSysctrlUnregister(pm, (LosPmSysctrl *)node);
        default:
            break;
    }

    return LOS_EINVAL;
}

VOID LOS_PmWakeSet(VOID)
{
    LosPmCB *pm = &g_pmCB;

    LOS_SpinLock(&g_pmSpin);
    pm->isWake = TRUE;
    LOS_SpinUnlock(&g_pmSpin);
    return;
}

Z
zhushengle 已提交
340 341 342 343 344 345
LOS_SysSleepEnum LOS_PmModeGet(VOID)
{
    LOS_SysSleepEnum mode;
    LosPmCB *pm = &g_pmCB;

    LOS_SpinLock(&g_pmSpin);
Z
zhushengle 已提交
346
    mode = pm->pmMode;
Z
zhushengle 已提交
347 348 349 350 351
    LOS_SpinUnlock(&g_pmSpin);

    return mode;
}

Z
zhushengle 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
UINT32 LOS_PmModeSet(LOS_SysSleepEnum mode)
{
    LosPmCB *pm = &g_pmCB;
    INT32 sleepMode = (INT32)mode;

    if ((sleepMode < 0) || (sleepMode > LOS_SYS_SHUTDOWN)) {
        return LOS_EINVAL;
    }

    LOS_SpinLock(&g_pmSpin);
    if ((mode != LOS_SYS_NORMAL_SLEEP) && (pm->device == NULL)) {
        LOS_SpinUnlock(&g_pmSpin);
        return LOS_EINVAL;
    }

    if ((mode == LOS_SYS_LIGHT_SLEEP) && (pm->sysctrl->lightSuspend == NULL)) {
        LOS_SpinUnlock(&g_pmSpin);
        return LOS_EINVAL;
    }

    if ((mode == LOS_SYS_DEEP_SLEEP) && (pm->sysctrl->deepSuspend == NULL)) {
        LOS_SpinUnlock(&g_pmSpin);
        return LOS_EINVAL;
    }

    if ((mode == LOS_SYS_SHUTDOWN) && (pm->sysctrl->shutdownSuspend == NULL)) {
        LOS_SpinUnlock(&g_pmSpin);
        return LOS_EINVAL;
    }

    pm->pmMode = mode;
    LOS_SpinUnlock(&g_pmSpin);

    return LOS_OK;
}

Z
zhushengle 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
UINT32 LOS_PmLockCountGet(VOID)
{
    UINT16 count;
    LosPmCB *pm = &g_pmCB;

    LOS_SpinLock(&g_pmSpin);
    count = pm->lock;
    LOS_SpinUnlock(&g_pmSpin);

    return (UINT32)count;
}

VOID LOS_PmLockInfoShow(struct SeqBuf *m)
{
    LosPmCB *pm = &g_pmCB;
    OsPmLockCB *lock = NULL;
    LOS_DL_LIST *head = &pm->lockList;
    LOS_DL_LIST *list = head->pstNext;

Z
zhushengle 已提交
407
    LOS_SpinLock(&g_pmSpin);
Z
zhushengle 已提交
408 409 410 411 412
    while (list != head) {
        lock = LOS_DL_LIST_ENTRY(list, OsPmLockCB, list);
        PM_INFO_SHOW(m, "%-30s%5u\n\r", lock->name, lock->count);
        list = list->pstNext;
    }
Z
zhushengle 已提交
413
    LOS_SpinUnlock(&g_pmSpin);
Z
zhushengle 已提交
414 415 416 417

    return;
}

Z
zhushengle 已提交
418
UINT32 OsPmLockRequest(const CHAR *name, UINT32 swtmrID)
Z
zhushengle 已提交
419 420 421
{
    INT32 len;
    errno_t err;
Z
zhushengle 已提交
422
    UINT32 ret = LOS_EINVAL;
Z
zhushengle 已提交
423 424 425 426 427 428 429 430 431 432
    LosPmCB *pm = &g_pmCB;
    OsPmLockCB *lock = NULL;
    LOS_DL_LIST *head = &pm->lockList;
    LOS_DL_LIST *list = head->pstNext;

    if (OS_INT_ACTIVE) {
        return LOS_EINTR;
    }

    len = strlen(name);
Z
zhushengle 已提交
433
    if (len <= 0) {
Z
zhushengle 已提交
434 435 436 437 438 439 440 441 442 443
        return LOS_EINVAL;
    }

    LOS_SpinLock(&g_pmSpin);
    if (pm->lock >= OS_PM_LOCK_MAX) {
        LOS_SpinUnlock(&g_pmSpin);
        return LOS_EINVAL;
    }

    while (list != head) {
Z
zhushengle 已提交
444
        OsPmLockCB *listNode = LOS_DL_LIST_ENTRY(list, OsPmLockCB, list);
Z
zhushengle 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
        if (strcmp(name, listNode->name) == 0) {
            lock = listNode;
            break;
        }

        list = list->pstNext;
    }

    if (lock == NULL) {
        lock = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, sizeof(OsPmLockCB));
        if (lock == NULL) {
            LOS_SpinUnlock(&g_pmSpin);
            return LOS_ENOMEM;
        }
        err = memcpy_s(lock->name, OS_PM_LOCK_NAME_MAX, name, len + 1);
        if (err != EOK) {
            LOS_SpinUnlock(&g_pmSpin);
            (VOID)LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, lock);
            return err;
        }
        lock->count = 1;
Z
zhushengle 已提交
466
        lock->swtmrID = swtmrID;
Z
zhushengle 已提交
467 468 469 470 471
        LOS_ListTailInsert(head, &lock->list);
    } else if (lock->count < OS_PM_LOCK_MAX) {
        lock->count++;
    }

Z
zhushengle 已提交
472 473 474 475 476 477 478 479 480 481 482
    if ((lock->swtmrID != OS_INVALID) && (lock->count > 1)) {
        lock->count--;
        LOS_SpinUnlock(&g_pmSpin);
        return LOS_EINVAL;
    }

    if (pm->lock < OS_PM_LOCK_MAX) {
        pm->lock++;
        ret = LOS_OK;
    }

Z
zhushengle 已提交
483
    LOS_SpinUnlock(&g_pmSpin);
Z
zhushengle 已提交
484 485 486 487 488 489 490 491 492 493
    return ret;
}

UINT32 LOS_PmLockRequest(const CHAR *name)
{
    if (name == NULL) {
        return LOS_EINVAL;
    }

    return OsPmLockRequest(name, OS_INVALID);
Z
zhushengle 已提交
494 495 496 497
}

UINT32 LOS_PmLockRelease(const CHAR *name)
{
Z
zhushengle 已提交
498
    UINT32 ret = LOS_EINVAL;
Z
zhushengle 已提交
499 500 501 502
    LosPmCB *pm = &g_pmCB;
    OsPmLockCB *lock = NULL;
    LOS_DL_LIST *head = &pm->lockList;
    LOS_DL_LIST *list = head->pstNext;
Z
zhushengle 已提交
503 504 505
    OsPmLockCB *lockFree = NULL;
    BOOL isRelease = FALSE;
    UINT32 mode;
Z
zhushengle 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

    if (name == NULL) {
        return LOS_EINVAL;
    }

    if (OS_INT_ACTIVE) {
        return LOS_EINTR;
    }

    LOS_SpinLock(&g_pmSpin);
    if (pm->lock == 0) {
        LOS_SpinUnlock(&g_pmSpin);
        return LOS_EINVAL;
    }

Z
zhushengle 已提交
521
    mode = (UINT32)pm->pmMode;
Z
zhushengle 已提交
522
    while (list != head) {
Z
zhushengle 已提交
523
        OsPmLockCB *listNode = LOS_DL_LIST_ENTRY(list, OsPmLockCB, list);
Z
zhushengle 已提交
524 525 526 527 528 529 530 531 532 533
        if (strcmp(name, listNode->name) == 0) {
            lock = listNode;
            break;
        }

        list = list->pstNext;
    }

    if (lock == NULL) {
        LOS_SpinUnlock(&g_pmSpin);
Z
zhushengle 已提交
534
        return LOS_EINVAL;
Z
zhushengle 已提交
535 536 537 538 539 540 541
    } else if (lock->count > 0) {
        lock->count--;
        if (lock->count == 0) {
            LOS_ListDelete(&lock->list);
            lockFree = lock;
        }
    }
Z
zhushengle 已提交
542 543 544 545 546 547 548 549

    if (pm->lock > 0) {
        pm->lock--;
        if (pm->lock == 0) {
            isRelease = TRUE;
        }
        ret = LOS_OK;
    }
Z
zhushengle 已提交
550 551
    LOS_SpinUnlock(&g_pmSpin);

Z
zhushengle 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
    if (lockFree != NULL) {
        (VOID)LOS_SwtmrDelete(lockFree->swtmrID);
        (VOID)LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, lockFree);
    }

    if (isRelease && (mode > LOS_SYS_NORMAL_SLEEP)) {
        (VOID)LOS_EventWrite(&g_pmEvent, PM_EVENT_LOCK_RELEASE);
    }

    return ret;
}

STATIC VOID OsPmSwtmrHandler(UINT32 arg)
{
    const CHAR *name = (const CHAR *)arg;
    UINT32 ret = LOS_PmLockRelease(name);
    if (ret != LOS_OK) {
        PRINT_ERR("Pm delay lock %s release faled! : 0x%x\n", name, ret);
    }
}

UINT32 LOS_PmTimeLockRequest(const CHAR *name, UINT64 millisecond)
{
    UINT32 ticks;
    UINT16 swtmrID;
    UINT32 ret;

    if ((name == NULL) || !millisecond) {
        return LOS_EINVAL;
    }

    ticks = (UINT32)((millisecond + OS_MS_PER_TICK - 1) / OS_MS_PER_TICK);
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
    ret = LOS_SwtmrCreate(ticks, LOS_SWTMR_MODE_ONCE, OsPmSwtmrHandler, &swtmrID, (UINT32)(UINTPTR)name,
                          OS_SWTMR_ROUSES_ALLOW, OS_SWTMR_ALIGN_INSENSITIVE);
#else
    ret = LOS_SwtmrCreate(ticks, LOS_SWTMR_MODE_ONCE, OsPmSwtmrHandler, &swtmrID, (UINT32)(UINTPTR)name);
#endif
    if (ret != LOS_OK) {
        return ret;
    }

    ret = OsPmLockRequest(name, swtmrID);
    if (ret != LOS_OK) {
        (VOID)LOS_SwtmrDelete(swtmrID);
        return ret;
    }

    ret = LOS_SwtmrStart(swtmrID);
    if (ret != LOS_OK) {
        (VOID)LOS_PmLockRelease(name);
    }

    return ret;
}

UINT32 LOS_PmReadLock(VOID)
{
    UINT32 ret = LOS_EventRead(&g_pmEvent, PM_EVENT_LOCK_MASK, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
    if (ret > PM_EVENT_LOCK_MASK) {
        PRINT_ERR("%s event read failed! ERROR: 0x%x\n", __FUNCTION__, ret);
    }

Z
zhushengle 已提交
615 616 617
    return LOS_OK;
}

Z
zhushengle 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
UINT32 LOS_PmSuspend(UINT32 wakeCount)
{
    (VOID)wakeCount;
    return OsPmSuspendSleep(&g_pmCB);
}

BOOL OsIsPmMode(VOID)
{
    LosPmCB *pm = &g_pmCB;

    LOS_SpinLock(&g_pmSpin);
    if ((pm->sysMode != LOS_SYS_NORMAL_SLEEP) && (pm->lock == 0)) {
        LOS_SpinUnlock(&g_pmSpin);
        return TRUE;
    }
    LOS_SpinUnlock(&g_pmSpin);
    return FALSE;
}

Z
zhushengle 已提交
637 638 639 640 641 642
UINT32 OsPmInit(VOID)
{
    LosPmCB *pm = &g_pmCB;

    (VOID)memset_s(pm, sizeof(LosPmCB), 0, sizeof(LosPmCB));

Z
zhushengle 已提交
643
    pm->pmMode = LOS_SYS_NORMAL_SLEEP;
Z
zhushengle 已提交
644
    LOS_ListInit(&pm->lockList);
Z
zhushengle 已提交
645 646
    (VOID)LOS_EventInit(&g_pmEvent);

Z
zhushengle 已提交
647 648 649 650 651
    return LOS_OK;
}

LOS_MODULE_INIT(OsPmInit, LOS_INIT_LEVEL_KMOD_EXTENDED);
#endif