1/*
2 * Copyright 2018, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13/*-
14 * Copyright (c) 2014, 2015 Antti Kantee.  All Rights Reserved.
15 * Copyright (c) 2015 Martin Lucina.  All Rights Reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 *    notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 *    notice, this list of conditions and the following disclaimer in the
24 *    documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
27 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include <sel4/kernel.h>
40
41#include <utils/util.h>
42#include <platsupport/timer.h>
43#include <bmk-core/core.h>
44#include <bmk-core/platform.h>
45#include <bmk-core/printf.h>
46#include <sel4/helpers.h>
47#include <stdio.h>
48
49/*
50 * Return monotonic time since system boot in nanoseconds.
51 */
52bmk_time_t
53bmk_platform_cpu_clock_monotonic(void)
54{
55    return arch_cpu_clock_monotonic();
56}
57
58/*
59 * Return epoch offset (wall time offset to monotonic clock start).
60 */
61bmk_time_t
62bmk_platform_cpu_clock_epochoffset(void)
63{
64    return arch_cpu_clock_epochoffset();
65}
66
67/*
68 * Block the CPU until monotonic time is *no later than* the specified time.
69 * Returns early if any interrupts are serviced, or if the requested delay is
70 * too short.
71 */
72//This is only called in schedule if there is no runnable thread.
73void
74bmk_platform_cpu_block(bmk_time_t until)
75{
76    bmk_time_t now, delta_ns;
77    bmk_assert(env.spldepth > 0);
78
79    /*
80     * Return if called too late.  Doing do ensures that the time
81     * delta is positive.
82     */
83    now = bmk_platform_cpu_clock_monotonic();
84    if (until <= now) {
85        return;
86    }
87
88    /*
89     * Compute the delta between time to wake and now. Setup a timeout via RPC to the
90     * timer server.
91     */
92    delta_ns = until - now;
93    int res;
94    if (is_ltimer(&env.custom_simple)) {
95        res = ltimer_set_timeout(&env.custom_simple.timer_config.ltimer.ltimer, delta_ns, TIMEOUT_RELATIVE);
96    } else {
97        res = env.custom_simple.timer_config.interface.oneshot_relative(0, delta_ns);
98    }
99    if (res != 0) {
100        bmk_platform_splx(0);
101        bmk_platform_splhigh();
102        return;
103    }
104
105    env.should_wakeup = 1;
106    bmk_platform_splx(0);
107    seL4_Wait(env.custom_simple.timer_config.timer_ntfn, NULL);
108
109    env.should_wakeup = 0;
110
111    bmk_platform_splhigh();
112}
113