1/*
2 * Copyright 2017, 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#include <sel4/sel4.h>
14#include <platsupport/timer.h>
15#include <platsupport/ltimer.h>
16#include <sel4utils/util.h>
17#include <sel4utils/time_server/client.h>
18#include <utils/util.h>
19
20typedef struct {
21    seL4_CPtr ep;
22    seL4_Word label;
23} client_ltimer_t;
24
25static int client_get_time(void *data, uint64_t *time)
26{
27    client_ltimer_t *ltimer = data;
28    seL4_MessageInfo_t info = seL4_MessageInfo_new(ltimer->label, 0, 0, 1);
29    seL4_SetMR(0, GET_TIME);
30    seL4_Call(ltimer->ep, info);
31    *time = sel4utils_64_get_mr(1);
32    return seL4_GetMR(0);
33}
34
35static int client_set_timeout(void *data, uint64_t ns, timeout_type_t type)
36{
37    client_ltimer_t *ltimer = data;
38    seL4_MessageInfo_t info = seL4_MessageInfo_new(ltimer->label, 0, 0, 2 + SEL4UTILS_64_WORDS);
39    seL4_SetMR(0, SET_TIMEOUT);
40    seL4_SetMR(1, type);
41    sel4utils_64_set_mr(2, ns);
42    info = seL4_Call(ltimer->ep, info);
43    return seL4_GetMR(0);
44}
45
46int sel4utils_rpc_ltimer_init(ltimer_t *ltimer, ps_io_ops_t ops, seL4_CPtr ep, seL4_Word label)
47{
48    ltimer->get_time = client_get_time;
49    ltimer->set_timeout = client_set_timeout;
50
51    int error = ps_calloc(&ops.malloc_ops, 1, sizeof(client_ltimer_t), &ltimer->data);
52    if (error) {
53        return error;
54    }
55    assert(ltimer->data != NULL);
56    client_ltimer_t *client_ltimer = ltimer->data;
57    client_ltimer->ep = ep;
58    client_ltimer->label = label;
59
60    /* success! */
61    return 0;
62}
63