los_pm.c 19.6 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
/*
 * 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.h"
#include "los_timer.h"
#include "los_memory.h"
37
#include "los_swtmr.h"
Z
zhushengle 已提交
38 39 40 41

#if (LOSCFG_KERNEL_PM == 1)
#define OS_PM_NODE_FREE 0x80000000U
#define OS_PM_LOCK_MAX  0xFFFFU
42 43
#define OS_PM_SYS_EARLY        1
#define OS_PM_SYS_DEVICE_EARLY 2
Z
zhushengle 已提交
44

45
typedef UINT32 (*Suspend)(UINT32 mode);
Z
zhushengle 已提交
46 47 48 49

typedef struct {
    CHAR         *name;
    UINT32       count;
50
    UINT32       swtmrID;
Z
zhushengle 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    LOS_DL_LIST  list;
} OsPmLockCB;

typedef struct {
    LOS_SysSleepEnum  pmMode;
    LOS_SysSleepEnum  sysMode;
    UINT16            lock;
    BOOL              isWake;
    LosPmDevice       *device;
    LosPmSysctrl      *sysctrl;
    LosPmTickTimer    *tickTimer;
#if (LOSCFG_BASE_CORE_TICK_WTIMER == 0)
    UINT64            enterSleepTime;
#endif
    LOS_DL_LIST       lockList;
} LosPmCB;

68 69 70
#define PM_EVENT_LOCK_MASK    0xF
#define PM_EVENT_LOCK_RELEASE 0x1
STATIC EVENT_CB_S g_pmEvent;
Z
zhushengle 已提交
71
STATIC LosPmCB g_pmCB;
72
STATIC LosPmSysctrl g_sysctrl;
Z
zhushengle 已提交
73 74
STATIC UINT64 g_pmSleepTime;

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
STATIC VOID OsPmSysctrlInit(VOID)
{
    /* Default handler functions, which are implemented by the product */
    g_sysctrl.early = NULL;
    g_sysctrl.late = NULL;
    g_sysctrl.normalSuspend = HalEnterSleep;
    g_sysctrl.normalResume = NULL;
    g_sysctrl.lightSuspend = HalEnterSleep;
    g_sysctrl.lightResume = NULL;
    g_sysctrl.deepSuspend = HalEnterSleep;
    g_sysctrl.deepResume = NULL;
    g_sysctrl.shutdownSuspend = NULL;
    g_sysctrl.shutdownResume = NULL;
}

Z
zhushengle 已提交
90 91
STATIC VOID OsPmTickTimerStart(LosPmCB *pm)
{
Z
zhushengle 已提交
92
#if (LOSCFG_BASE_CORE_TICK_WTIMER == 0)
Z
zhushengle 已提交
93
    UINT64 currTime, sleepTime, realSleepTime;
Z
zhushengle 已提交
94
#endif
Z
zhushengle 已提交
95 96
    LosPmTickTimer *tickTimer = pm->tickTimer;

Z
zhushengle 已提交
97 98 99 100 101 102 103 104 105
    if ((tickTimer == NULL) || (tickTimer->tickUnlock == NULL)) {
        return;
    }

#if (LOSCFG_BASE_CORE_TICK_WTIMER == 0)
    if (tickTimer->timerStop != NULL) {
        /* Restore the main CPU frequency */
        sleepTime = tickTimer->timerCycleGet();
        tickTimer->timerStop();
Z
zhushengle 已提交
106

Z
zhushengle 已提交
107 108 109 110 111 112 113 114
        realSleepTime = OS_SYS_CYCLE_TO_NS(sleepTime, tickTimer->freq);
        realSleepTime = OS_SYS_NS_TO_CYCLE(realSleepTime, OS_SYS_CLOCK);
        currTime = pm->enterSleepTime + realSleepTime;
        pm->enterSleepTime = 0;

        OsSchedTimerBaseReset(currTime);
    }
#endif
Z
zhushengle 已提交
115 116 117 118 119

    tickTimer->tickUnlock();
    return;
}

120
STATIC BOOL OsPmTickTimerStop(LosPmCB *pm)
Z
zhushengle 已提交
121
{
Z
zhushengle 已提交
122
#if (LOSCFG_BASE_CORE_TICK_WTIMER == 0)
Z
zhushengle 已提交
123 124
    UINT64 sleepCycle;
    UINT64 realSleepTime = g_pmSleepTime;
Z
zhushengle 已提交
125
#endif
Z
zhushengle 已提交
126 127
    LosPmTickTimer *tickTimer = pm->tickTimer;

Z
zhushengle 已提交
128
    if ((tickTimer == NULL) || (tickTimer->tickLock == NULL)) {
129
        return FALSE;
Z
zhushengle 已提交
130 131 132
    }

#if (LOSCFG_BASE_CORE_TICK_WTIMER == 0)
Z
zhushengle 已提交
133 134
    if (tickTimer->timerStart != NULL) {
        if (realSleepTime == 0) {
135
            return FALSE;
Z
zhushengle 已提交
136
        }
Z
zhushengle 已提交
137 138 139 140 141 142 143 144

        sleepCycle = OS_SYS_CYCLE_TO_NS(realSleepTime, OS_SYS_CLOCK);
        sleepCycle = OS_SYS_NS_TO_CYCLE(sleepCycle, tickTimer->freq);

        /* The main CPU reduces the frequency */
        pm->enterSleepTime = OsGetCurrSysTimeCycle();
        tickTimer->tickLock();
        tickTimer->timerStart(sleepCycle);
145
        return TRUE;
Z
zhushengle 已提交
146
    }
Z
zhushengle 已提交
147 148 149
#endif

    tickTimer->tickLock();
150
    return TRUE;
Z
zhushengle 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163
}

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();
    }
}

164
STATIC VOID OsPmCpuSuspend(LosPmCB *pm)
Z
zhushengle 已提交
165 166 167 168 169
{
    /* cpu enter low power mode */
    LOS_ASSERT(pm->sysctrl != NULL);

    if (pm->sysMode == LOS_SYS_NORMAL_SLEEP) {
170
        pm->sysctrl->normalSuspend();
Z
zhushengle 已提交
171
    } else if (pm->sysMode == LOS_SYS_LIGHT_SLEEP) {
172
        pm->sysctrl->lightSuspend();
Z
zhushengle 已提交
173
    } else if (pm->sysMode == LOS_SYS_DEEP_SLEEP) {
174
        pm->sysctrl->deepSuspend();
Z
zhushengle 已提交
175
    } else {
176
        pm->sysctrl->shutdownSuspend();
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 211 212 213 214
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)
Z
zhushengle 已提交
215 216 217 218 219
{
    UINT32 intSave;

    intSave = LOS_IntLock();
    pm->sysMode = pm->pmMode;
220
    if (pm->lock > 0) {
Z
zhushengle 已提交
221
        pm->sysMode = LOS_SYS_NORMAL_SLEEP;
Z
zhushengle 已提交
222
        LOS_IntRestore(intSave);
Z
zhushengle 已提交
223
        return LOS_NOK;
Z
zhushengle 已提交
224 225
    }

Z
zhushengle 已提交
226
    pm->isWake = FALSE;
227 228 229 230 231 232
    *mode = pm->sysMode;
    *sysSuspendEarly = pm->sysctrl->early;
    *deviceSuspend = pm->device->suspend;
    LOS_IntRestore(intSave);
    return LOS_OK;
}
Z
zhushengle 已提交
233

234 235 236 237 238 239 240 241 242 243 244 245
STATIC UINT32 OsPmSuspendSleep(LosPmCB *pm)
{
    UINT32 ret, intSave;
    Suspend sysSuspendEarly, deviceSuspend;
    LOS_SysSleepEnum mode;
    UINT32 prepare = 0;
    BOOL tickTimerStop = FALSE;
    UINT64 currTime;

    ret = OsPmSuspendCheck(pm, &sysSuspendEarly, &deviceSuspend, &mode);
    if (ret != LOS_OK) {
        return ret;
Z
zhushengle 已提交
246 247
    }

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    ret = OsPmSuspendPrepare(sysSuspendEarly, deviceSuspend, (UINT32)mode, &prepare);
    if (ret != LOS_OK) {
        intSave = LOS_IntLock();
        LOS_TaskLock();
        goto EXIT;
    }

    intSave = LOS_IntLock();
    LOS_TaskLock();
    if (pm->isWake || (pm->lock > 0)) {
        goto EXIT;
    }

    tickTimerStop = OsPmTickTimerStop(pm);
    if (!tickTimerStop) {
        currTime = OsGetCurrSchedTimeCycle();
        OsSchedResetSchedResponseTime(0);
265
        OsSchedUpdateExpireTime(currTime, TRUE);
266
    }
Z
zhushengle 已提交
267

268
    OsPmCpuSuspend(pm);
Z
zhushengle 已提交
269 270 271

    OsPmCpuResume(pm);

Z
zhushengle 已提交
272
    OsPmTickTimerStart(pm);
Z
zhushengle 已提交
273

274
EXIT:
Z
zhushengle 已提交
275
    pm->sysMode = LOS_SYS_NORMAL_SLEEP;
276
    OsPmResumePrepare(pm, (UINT32)mode, prepare);
Z
zhushengle 已提交
277 278 279 280 281 282
    LOS_IntRestore(intSave);

    LOS_TaskUnlock();
    return ret;
}

283
STATIC VOID OsPmNormalSleep(VOID)
Z
zhushengle 已提交
284 285
{
    UINT32 intSave;
286
    LosPmCB *pm = &g_pmCB;
Z
zhushengle 已提交
287 288 289

    intSave = LOS_IntLock();

290
    OsPmCpuSuspend(pm);
Z
zhushengle 已提交
291

Z
zhushengle 已提交
292
    OsPmCpuResume(pm);
293

Z
zhushengle 已提交
294
    LOS_IntRestore(intSave);
Z
zhushengle 已提交
295
}
Z
zhushengle 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

STATIC UINT32 OsPmDeviceRegister(LosPmCB *pm, LosPmDevice *device)
{
    UINT32 intSave;

    if ((device->suspend == NULL) || (device->resume == NULL)) {
        return LOS_ERRNO_PM_INVALID_PARAM;
    }

    intSave = LOS_IntLock();
    pm->device = device;
    LOS_IntRestore(intSave);

    return LOS_OK;
}

STATIC UINT32 OsPmTickTimerRegister(LosPmCB *pm, LosPmTickTimer *tickTimer)
{
    UINT32 intSave;

Z
zhushengle 已提交
316 317 318 319 320 321 322 323 324
    if ((tickTimer->tickLock == NULL) || (tickTimer->tickUnlock == NULL)) {
        return LOS_ERRNO_PM_INVALID_PARAM;
    }

    if (((tickTimer->timerStart == NULL) && (tickTimer->timerStop == NULL) &&
         (tickTimer->timerCycleGet == NULL) && (tickTimer->freq == 0)) ||
        ((tickTimer->timerStart != NULL) && (tickTimer->timerStop != NULL) &&
         (tickTimer->timerCycleGet != NULL) && (tickTimer->freq != 0))) {
        intSave = LOS_IntLock();
Z
zhushengle 已提交
325
#if (LOSCFG_BASE_CORE_TICK_WTIMER == 0)
Z
zhushengle 已提交
326
        pm->enterSleepTime = 0;
Z
zhushengle 已提交
327
#endif
Z
zhushengle 已提交
328 329 330 331 332 333
        pm->tickTimer = tickTimer;
        LOS_IntRestore(intSave);
        return LOS_OK;
    }

    return LOS_ERRNO_PM_INVALID_PARAM;
Z
zhushengle 已提交
334 335 336 337
}

STATIC UINT32 OsPmSysctrlRegister(LosPmCB *pm, LosPmSysctrl *sysctrl)
{
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    UINT32 intSave = LOS_IntLock();
    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 已提交
368 369 370 371 372 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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
    }
    LOS_IntRestore(intSave);
    return LOS_OK;
}

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

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

    switch (type) {
        case LOS_PM_TYPE_DEVICE:
            return OsPmDeviceRegister(pm, (LosPmDevice *)node);
        case LOS_PM_TYPE_TICK_TIMER:
            return OsPmTickTimerRegister(pm, (LosPmTickTimer *)node);
        case LOS_PM_TYPE_SYSCTRL:
            return OsPmSysctrlRegister(pm, (LosPmSysctrl *)node);
        default:
            break;
    }

    return LOS_ERRNO_PM_INVALID_TYPE;
}

STATIC UINT32 OsPmDeviceUnregister(LosPmCB *pm, LosPmDevice *device)
{
    UINT32 intSave;

    intSave = LOS_IntLock();
    if (pm->device == device) {
        pm->device = NULL;
        pm->pmMode = LOS_SYS_NORMAL_SLEEP;
        LOS_IntRestore(intSave);
        return LOS_OK;
    }

    LOS_IntRestore(intSave);
    return LOS_ERRNO_PM_INVALID_NODE;
}

STATIC UINT32 OsPmTickTimerUnregister(LosPmCB *pm, LosPmTickTimer *tickTimer)
{
    UINT32 intSave;

    intSave = LOS_IntLock();
    if (pm->tickTimer == tickTimer) {
        pm->tickTimer = NULL;
        if ((pm->pmMode != LOS_SYS_NORMAL_SLEEP) && (pm->pmMode != LOS_SYS_LIGHT_SLEEP)) {
            pm->pmMode = LOS_SYS_NORMAL_SLEEP;
        }
        LOS_IntRestore(intSave);
        return LOS_OK;
    }

    LOS_IntRestore(intSave);
    return LOS_ERRNO_PM_INVALID_NODE;
}

STATIC UINT32 OsPmSysctrlUnregister(LosPmCB *pm, LosPmSysctrl *sysctrl)
{
431
    (VOID)sysctrl;
Z
zhushengle 已提交
432

433 434 435
    UINT32 intSave = LOS_IntLock();
    OsPmSysctrlInit();
    pm->pmMode = LOS_SYS_NORMAL_SLEEP;
Z
zhushengle 已提交
436
    LOS_IntRestore(intSave);
437
    return LOS_OK;
Z
zhushengle 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
}

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

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

    switch (type) {
        case LOS_PM_TYPE_DEVICE:
            return OsPmDeviceUnregister(pm, (LosPmDevice *)node);
        case LOS_PM_TYPE_TICK_TIMER:
            return OsPmTickTimerUnregister(pm, (LosPmTickTimer *)node);
        case LOS_PM_TYPE_SYSCTRL:
            return OsPmSysctrlUnregister(pm, (LosPmSysctrl *)node);
        default:
            break;
    }

    return LOS_ERRNO_PM_INVALID_TYPE;
}

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

    intSave = LOS_IntLock();
    pm->isWake = TRUE;
    LOS_IntRestore(intSave);
    return;
}

LOS_SysSleepEnum LOS_PmModeGet(VOID)
{
    LOS_SysSleepEnum mode;
    LosPmCB *pm = &g_pmCB;

    UINT32 intSave = LOS_IntLock();
    mode = pm->pmMode;
    LOS_IntRestore(intSave);

    return mode;
}

UINT32 LOS_PmModeSet(LOS_SysSleepEnum mode)
{
    UINT32 intSave;
    LosPmCB *pm = &g_pmCB;
    INT32 sleepMode = (INT32)mode;

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

    intSave = LOS_IntLock();
    if ((mode != LOS_SYS_NORMAL_SLEEP) && (pm->device == NULL)) {
        LOS_IntRestore(intSave);
        return LOS_ERRNO_PM_DEVICE_NULL;
    }

    if ((mode == LOS_SYS_LIGHT_SLEEP) && (pm->sysctrl->lightSuspend == NULL)) {
        LOS_IntRestore(intSave);
        return LOS_ERRNO_PM_HANDLER_NULL;
    }

    if ((mode == LOS_SYS_DEEP_SLEEP) && (pm->sysctrl->deepSuspend == NULL)) {
        LOS_IntRestore(intSave);
        return LOS_ERRNO_PM_HANDLER_NULL;
    }

    if ((mode == LOS_SYS_SHUTDOWN) && (pm->sysctrl->shutdownSuspend == NULL)) {
        LOS_IntRestore(intSave);
        return LOS_ERRNO_PM_HANDLER_NULL;
    }

    pm->pmMode = mode;
    LOS_IntRestore(intSave);

    return LOS_OK;
}

#if (LOSCFG_KERNEL_PM_DEBUG == 1)
VOID LOS_PmLockInfoShow(VOID)
{
    UINT32 intSave;
    LosPmCB *pm = &g_pmCB;
    OsPmLockCB *lock = NULL;
    LOS_DL_LIST *head = &pm->lockList;
    LOS_DL_LIST *list = head->pstNext;

    PRINTK("Name                          Count\n\r");

    intSave = LOS_IntLock();
    while (list != head) {
        lock = LOS_DL_LIST_ENTRY(list, OsPmLockCB, list);
        PRINTK("%-30s%5u\n\r", lock->name, lock->count);
        list = list->pstNext;
    }
    LOS_IntRestore(intSave);

    return;
}
#endif

545
UINT32 OsPmLockRequest(const CHAR *name, UINT32 swtmrID)
Z
zhushengle 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
{
    UINT32 intSave;
    UINT32 ret = LOS_ERRNO_PM_NOT_LOCK;
    LosPmCB *pm = &g_pmCB;
    OsPmLockCB *listNode = NULL;
    OsPmLockCB *lock = NULL;
    LOS_DL_LIST *head = &pm->lockList;
    LOS_DL_LIST *list = head->pstNext;

    intSave = LOS_IntLock();
    while (list != head) {
        listNode = LOS_DL_LIST_ENTRY(list, OsPmLockCB, list);
        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_IntRestore(intSave);
            return LOS_NOK;
        }
        lock->name = (CHAR *)name;
        lock->count = 1;
574
        lock->swtmrID = swtmrID;
Z
zhushengle 已提交
575 576 577 578
        LOS_ListTailInsert(head, &lock->list);
    } else if (lock->count < OS_PM_LOCK_MAX) {
        lock->count++;
    }
579 580 581 582 583 584

    if ((lock->swtmrID != OS_INVALID) && (lock->count > 1)) {
        lock->count--;
        LOS_IntRestore(intSave);
        return LOS_ERRNO_PM_ALREADY_LOCK;
    }
Z
zhushengle 已提交
585 586 587 588 589 590 591 592 593 594

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

    LOS_IntRestore(intSave);
    return ret;
}

595 596 597 598 599 600 601 602 603
UINT32 LOS_PmLockRequest(const CHAR *name)
{
    if (name == NULL) {
        return LOS_ERRNO_PM_INVALID_PARAM;
    }

    return OsPmLockRequest(name, OS_INVALID);
}

Z
zhushengle 已提交
604 605 606 607 608 609 610 611 612
UINT32 LOS_PmLockRelease(const CHAR *name)
{
    UINT32 intSave;
    UINT32 ret = LOS_ERRNO_PM_NOT_LOCK;
    LosPmCB *pm = &g_pmCB;
    OsPmLockCB *lock = NULL;
    OsPmLockCB *listNode = NULL;
    LOS_DL_LIST *head = &pm->lockList;
    LOS_DL_LIST *list = head->pstNext;
613 614 615
    OsPmLockCB *lockFree = NULL;
    BOOL isRelease = FALSE;
    UINT32 mode;
Z
zhushengle 已提交
616 617 618 619 620 621

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

    intSave = LOS_IntLock();
622
    mode = (UINT32)pm->pmMode;
Z
zhushengle 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
    while (list != head) {
        listNode = LOS_DL_LIST_ENTRY(list, OsPmLockCB, list);
        if (strcmp(name, listNode->name) == 0) {
            lock = listNode;
            break;
        }

        list = list->pstNext;
    }

    if (lock == NULL) {
        LOS_IntRestore(intSave);
        return LOS_ERRNO_PM_NOT_LOCK;
    } else if (lock->count > 0) {
        lock->count--;
        if (lock->count == 0) {
            LOS_ListDelete(&lock->list);
            lockFree = lock;
        }
    }

    if (pm->lock > 0) {
        pm->lock--;
646 647 648
        if (pm->lock == 0) {
            isRelease = TRUE;
        }
Z
zhushengle 已提交
649 650 651
        ret = LOS_OK;
    }
    LOS_IntRestore(intSave);
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689

    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;
    UINT32 swtmrID;
    UINT32 ret;

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

    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);
Z
zhushengle 已提交
690
#endif
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
    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);
    }

Z
zhushengle 已提交
706 707 708
    return ret;
}

709 710 711 712
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) {
713
        PRINT_ERR("%s event read failed! ERROR: 0x%x\n", __FUNCTION__, ret);
714 715 716 717 718 719 720 721 722 723 724
    }

    return LOS_OK;
}

UINT32 LOS_PmSuspend(UINT32 wakeCount)
{
    (VOID)wakeCount;
    return OsPmSuspendSleep(&g_pmCB);
}

Z
zhushengle 已提交
725 726 727 728 729
STATIC VOID OsPmSleepTimeSet(UINT64 sleepTime)
{
    g_pmSleepTime = sleepTime;
}

730 731 732 733 734 735 736 737 738 739 740 741 742
BOOL OsIsPmMode(VOID)
{
    LosPmCB *pm = &g_pmCB;

    UINT32 intSave = LOS_IntLock();
    if ((pm->sysMode != LOS_SYS_NORMAL_SLEEP) && (pm->lock == 0)) {
        LOS_IntRestore(intSave);
        return TRUE;
    }
    LOS_IntRestore(intSave);
    return FALSE;
}

Z
zhushengle 已提交
743 744 745 746 747 748 749 750 751
UINT32 OsPmInit(VOID)
{
    UINT32 ret;
    LosPmCB *pm = &g_pmCB;

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

    pm->pmMode = LOS_SYS_NORMAL_SLEEP;
    LOS_ListInit(&pm->lockList);
752
    (VOID)LOS_EventInit(&g_pmEvent);
Z
zhushengle 已提交
753 754 755 756 757 758

    ret = OsSchedRealSleepTimeSet(OsPmSleepTimeSet);
    if (ret != LOS_OK) {
        return ret;
    }

759
    ret = OsPmEnterHandlerSet(OsPmNormalSleep);
Z
zhushengle 已提交
760 761 762 763
    if (ret != LOS_OK) {
        return ret;
    }

764 765
    OsPmSysctrlInit();
    pm->sysctrl = &g_sysctrl;
Z
zhushengle 已提交
766 767 768
    return ret;
}
#endif