los_pm.c 17.4 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
typedef UINT32 (*Suspend)(UINT32 mode);
Z
zhushengle 已提交
58 59 60 61

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

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

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

STATIC VOID OsPmSysctrlInit(VOID);
Z
zhushengle 已提交
85

Z
zhushengle 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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();
    }
}

109
VOID OsPmCpuSuspend(LosPmCB *pm)
Z
zhushengle 已提交
110 111 112 113 114
{
    /* cpu enter low power mode */
    LOS_ASSERT(pm->sysctrl != NULL);

    if (pm->sysMode == LOS_SYS_NORMAL_SLEEP) {
115
        pm->sysctrl->normalSuspend();
Z
zhushengle 已提交
116
    } else if (pm->sysMode == LOS_SYS_LIGHT_SLEEP) {
117
        pm->sysctrl->lightSuspend();
Z
zhushengle 已提交
118
    } else if (pm->sysMode == LOS_SYS_DEEP_SLEEP) {
119
        pm->sysctrl->deepSuspend();
Z
zhushengle 已提交
120
    } else {
121
        pm->sysctrl->shutdownSuspend();
Z
zhushengle 已提交
122 123 124 125 126
    }
}

STATIC VOID OsPmResumePrepare(LosPmCB *pm, UINT32 mode, UINT32 prepare)
{
127
    if ((prepare == 0) && (pm->device != NULL) && (pm->device->resume != NULL)) {
Z
zhushengle 已提交
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
        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;
172 173 174 175 176
    if (pm->device != NULL) {
        *deviceSuspend = pm->device->suspend;
    } else {
        *deviceSuspend = NULL;
    }
Z
zhushengle 已提交
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
    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;

    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) {
        OsSchedResetSchedResponseTime(0);
211
        OsSchedUpdateExpireTime();
Z
zhushengle 已提交
212 213
    }

214
    OsPmCpuSuspend(pm);
Z
zhushengle 已提交
215 216 217 218 219 220 221 222 223

    OsPmCpuResume(pm);

    OsPmTickTimerStart(pm);

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

224
    LOS_SpinUnlockRestore(&g_pmSpin, intSave);
Z
zhushengle 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    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);
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
    if (sysctrl->early != NULL) {
        pm->sysctrl->early = sysctrl->early;
    }
    if (sysctrl->late != NULL) {
        pm->sysctrl->late = sysctrl->late;
    }
    if (sysctrl->normalSuspend != NULL) {
        pm->sysctrl->normalSuspend = sysctrl->normalSuspend;
    }
    if (sysctrl->normalResume != NULL) {
        pm->sysctrl->normalResume = sysctrl->normalResume;
    }
    if (sysctrl->lightSuspend != NULL) {
        pm->sysctrl->lightSuspend = sysctrl->lightSuspend;
    }
    if (sysctrl->lightResume != NULL) {
        pm->sysctrl->lightResume = sysctrl->lightResume;
    }
    if (sysctrl->deepSuspend != NULL) {
        pm->sysctrl->deepSuspend = sysctrl->deepSuspend;
    }
    if (sysctrl->deepResume != NULL) {
        pm->sysctrl->deepResume = sysctrl->deepResume;
    }
    if (sysctrl->shutdownSuspend != NULL) {
        pm->sysctrl->shutdownSuspend = sysctrl->shutdownSuspend;
    }
    if (sysctrl->shutdownResume != NULL) {
        pm->sysctrl->shutdownResume = sysctrl->shutdownResume;
    }
Z
zhushengle 已提交
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
    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)
{
319
    (VOID)sysctrl;
Z
zhushengle 已提交
320
    LOS_SpinLock(&g_pmSpin);
321 322
    OsPmSysctrlInit();
    pm->pmMode = LOS_SYS_NORMAL_SLEEP;
Z
zhushengle 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    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 已提交
361 362 363 364 365 366
LOS_SysSleepEnum LOS_PmModeGet(VOID)
{
    LOS_SysSleepEnum mode;
    LosPmCB *pm = &g_pmCB;

    LOS_SpinLock(&g_pmSpin);
Z
zhushengle 已提交
367
    mode = pm->pmMode;
Z
zhushengle 已提交
368 369 370 371 372
    LOS_SpinUnlock(&g_pmSpin);

    return mode;
}

Z
zhushengle 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
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_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 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
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 已提交
423
    LOS_SpinLock(&g_pmSpin);
Z
zhushengle 已提交
424 425 426 427 428
    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 已提交
429
    LOS_SpinUnlock(&g_pmSpin);
Z
zhushengle 已提交
430 431 432 433

    return;
}

Z
zhushengle 已提交
434
UINT32 OsPmLockRequest(const CHAR *name, UINT32 swtmrID)
Z
zhushengle 已提交
435 436 437
{
    INT32 len;
    errno_t err;
Z
zhushengle 已提交
438
    UINT32 ret = LOS_EINVAL;
Z
zhushengle 已提交
439 440 441 442 443 444 445 446 447 448
    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 已提交
449
    if (len <= 0) {
Z
zhushengle 已提交
450 451 452 453 454 455 456 457 458 459
        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 已提交
460
        OsPmLockCB *listNode = LOS_DL_LIST_ENTRY(list, OsPmLockCB, list);
Z
zhushengle 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
        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 已提交
482
        lock->swtmrID = swtmrID;
Z
zhushengle 已提交
483 484 485 486 487
        LOS_ListTailInsert(head, &lock->list);
    } else if (lock->count < OS_PM_LOCK_MAX) {
        lock->count++;
    }

Z
zhushengle 已提交
488 489 490 491 492 493 494 495 496 497 498
    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 已提交
499
    LOS_SpinUnlock(&g_pmSpin);
Z
zhushengle 已提交
500 501 502 503 504 505 506 507 508 509
    return ret;
}

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

    return OsPmLockRequest(name, OS_INVALID);
Z
zhushengle 已提交
510 511 512 513
}

UINT32 LOS_PmLockRelease(const CHAR *name)
{
Z
zhushengle 已提交
514
    UINT32 ret = LOS_EINVAL;
Z
zhushengle 已提交
515 516 517 518
    LosPmCB *pm = &g_pmCB;
    OsPmLockCB *lock = NULL;
    LOS_DL_LIST *head = &pm->lockList;
    LOS_DL_LIST *list = head->pstNext;
Z
zhushengle 已提交
519 520 521
    OsPmLockCB *lockFree = NULL;
    BOOL isRelease = FALSE;
    UINT32 mode;
Z
zhushengle 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536

    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 已提交
537
    mode = (UINT32)pm->pmMode;
Z
zhushengle 已提交
538
    while (list != head) {
Z
zhushengle 已提交
539
        OsPmLockCB *listNode = LOS_DL_LIST_ENTRY(list, OsPmLockCB, list);
Z
zhushengle 已提交
540 541 542 543 544 545 546 547 548 549
        if (strcmp(name, listNode->name) == 0) {
            lock = listNode;
            break;
        }

        list = list->pstNext;
    }

    if (lock == NULL) {
        LOS_SpinUnlock(&g_pmSpin);
Z
zhushengle 已提交
550
        return LOS_EINVAL;
Z
zhushengle 已提交
551 552 553 554 555 556 557
    } else if (lock->count > 0) {
        lock->count--;
        if (lock->count == 0) {
            LOS_ListDelete(&lock->list);
            lockFree = lock;
        }
    }
Z
zhushengle 已提交
558 559 560 561 562 563 564 565

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

Z
zhushengle 已提交
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 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    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 已提交
631 632 633
    return LOS_OK;
}

Z
zhushengle 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
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;
}

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
STATIC UINT32 OsPmSuspendDefaultHandler(VOID)
{
    PRINTK("Enter pm default handler!!!\n");
    WFI;
    return LOS_OK;
}

STATIC VOID OsPmSysctrlInit(VOID)
{
    /* Default handler functions, which are implemented by the product */
    g_sysctrl.early = NULL;
    g_sysctrl.late = NULL;
    g_sysctrl.normalSuspend = OsPmSuspendDefaultHandler;
    g_sysctrl.normalResume = NULL;
    g_sysctrl.lightSuspend = OsPmSuspendDefaultHandler;
    g_sysctrl.lightResume = NULL;
    g_sysctrl.deepSuspend = OsPmSuspendDefaultHandler;
    g_sysctrl.deepResume = NULL;
    g_sysctrl.shutdownSuspend = NULL;
    g_sysctrl.shutdownResume = NULL;
}

Z
zhushengle 已提交
675 676 677 678 679 680
UINT32 OsPmInit(VOID)
{
    LosPmCB *pm = &g_pmCB;

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

Z
zhushengle 已提交
681
    pm->pmMode = LOS_SYS_NORMAL_SLEEP;
Z
zhushengle 已提交
682
    LOS_ListInit(&pm->lockList);
Z
zhushengle 已提交
683 684
    (VOID)LOS_EventInit(&g_pmEvent);

685 686
    OsPmSysctrlInit();
    pm->sysctrl = &g_sysctrl;
Z
zhushengle 已提交
687 688 689 690 691
    return LOS_OK;
}

LOS_MODULE_INIT(OsPmInit, LOS_INIT_LEVEL_KMOD_EXTENDED);
#endif