1/*
2 * Copyright 2016, 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(D61_BSD)
11 */
12
13#ifndef _TIMER_SERVER_DEVICE_TIMER_H_
14#define _TIMER_SERVER_DEVICE_TIMER_H_
15
16#include <stdio.h>
17#include <stdint.h>
18#include <stdbool.h>
19#include <sel4/sel4.h>
20#include <data_struct/cvector.h>
21#include <refos-util/device_io.h>
22#include <platsupport/timer.h>
23#include <platsupport/plat/timer.h>
24
25/*! @file
26    @brief timer server timer device manager. */
27
28#define TIMESERV_DEVICE_TIMER_MAGIC 0x54F1A770
29#define TIMESERV_DEVICE_TIMER_WAITER_MAGIC 0x2F4401A9
30
31/*! @brief Timer device waiter structure. */
32struct device_timer_waiter {
33    uint32_t magic;
34    uint64_t time;
35    seL4_CPtr reply;
36    struct srv_client *client; /* No ownership. */
37};
38
39/*! @brief Timer device state structure. */
40struct device_timer_state {
41    uint32_t magic;
42    dev_io_ops_t *io; /* No ownership, weak reference. */
43    bool initialised;
44
45    /*! This should point to a timer device. */
46    pstimer_t *timerDev; /* No ownership. Weak ref to static. */
47
48    /*! This should point to a timer device that is capable of generating periodic interrupts.
49        Note that this may point to the exact same device as timerDev. */
50    pstimer_t *tickDev; /* No ownership. Weak ref to static. */
51
52    cvector_t waiterList; /* struct device_timer_waiter */
53    uint64_t cumulativeTime; /*!< Current cumulative time. */
54    uint64_t timerIRQPeriod;
55};
56
57/*! @brief Initialies the timer device management module.
58    @param s The global timer device state structure (No ownership).
59    @param io The initialised device IO manager. (No ownership).
60*/
61void device_timer_init(struct device_timer_state *s, dev_io_ops_t *io);
62
63/*! @brief Get the current time since epoch in nanoseconds.
64    @param s The global timer device state structure (No ownership).
65    @return The current time in nanoseconds.
66*/
67uint64_t device_timer_get_time(struct device_timer_state *s);
68
69/*! @brief Save the current caller client's reply cap, and reply to it when its sleep time has
70           passed.
71    @param s The global timer device state structure (No ownership).
72    @param c The client structure of the waiting client.
73    @param waitTime The amount of time in nanoseconds that the client wishes to wait, relative to
74                    now.
75    @return ESUCCESS if success, refos_err_t otherwise.
76*/
77int device_timer_save_caller_as_waiter(struct device_timer_state *s, struct srv_client *c,
78        uint64_t waitTime);
79
80/*! @brief Purge all weak references to client form waiting list. Used when client dies.
81    @param client The dying client to be purged.
82*/
83void device_timer_purge_client(struct device_timer_state *client);
84
85#endif /* _TIMER_SERVER_DEVICE_TIMER_H_ */