1#include <stdio.h>
2
3#include <barrelfish/barrelfish.h>
4#include <barrelfish/cpu_arch.h>
5#include <barrelfish/nameservice_client.h>
6#include <barrelfish/types.h>
7//#include <if/int_route_controller_defs.h>
8#include <hpet_debug.h>
9#include <hpet_int_cntrl.h>
10#include <if/int_route_controller_defs.h>
11#include <int_route/int_model.h>
12
13struct hpet_ctrl_state {
14    struct hpet_comp_st *comp_st;
15    int bound;
16    char label[128];
17    struct int_route_controller_binding *bind;
18};
19
20
21static void add_mapping(struct int_route_controller_binding *b,
22                        const char *label, const char *class,
23                        int_route_controller_int_message_t from,
24                        int_route_controller_int_message_t to) {
25
26    struct hpet_ctrl_state *st = b->st;
27    errval_t err;
28    HPET_DEBUG("(add_mapping) label:%s, class:%s (%" PRIu64 ", %" PRIu64 ") to "
29               "(%" PRIu64 ", %" PRIu64 ")\n",
30               label, class, from.addr, from.msg, to.addr, to.msg);
31
32    // activate fsb interrupt
33    if(to.port == 0){
34        err = hpet_comp_enable_fsb_int(st->comp_st, to.addr, to.msg);
35    } else {
36        err = hpet_comp_enable_ioapic_int(st->comp_st, to.msg);
37    }
38    // hpet->timer[from.port].fsb_address = to.addr
39    if(err_is_fail(err)){
40        DEBUG_ERR(err, "map_fsb_int");
41    }
42};
43
44static void hpet_ctrl_bind_cb(void *stin, errval_t err,
45                              struct int_route_controller_binding *bind) {
46
47    HPET_DEBUG("(hpet_ctrl_bind_cb) bind_cb\n");
48    struct hpet_ctrl_state *st = stin;
49    bind->rx_vtbl.add_mapping = add_mapping;
50    bind->tx_vtbl.register_controller(bind, BLOCKING_CONT, st->label, "");
51    // Store state in binding
52    st->bind = bind;
53    st->bound = true;
54}
55
56errval_t init_hpet_int_controller(struct hpet_comp_st *comp_st, const char * lbl) {
57
58    errval_t err;
59    HPET_DEBUG("(init_hpet_int_controller) start, label=%s\n", lbl);
60
61    // initializng hpet controller state
62    struct hpet_ctrl_state *st = malloc(sizeof(struct hpet_ctrl_state));
63    st->bound = false;
64    st->comp_st = comp_st;
65    strncpy(st->label, lbl, sizeof(st->label));
66
67    // Connect to int route service
68    iref_t int_route_service;
69    err = nameservice_blocking_lookup("int_ctrl_service", &int_route_service);
70    if (!err_is_ok(err)) {
71        DEBUG_ERR(err, "Could not lookup int_route_service\n");
72        return err;
73    }
74
75    err = int_route_controller_bind(int_route_service, hpet_ctrl_bind_cb, st,
76                                    get_default_waitset(),
77                                    IDC_BIND_FLAGS_DEFAULT);
78
79    if (!err_is_ok(err)) {
80        DEBUG_ERR(err, "int bind\n");
81        return err;
82    }
83
84    while (!st->bound)
85        event_dispatch(get_default_waitset());
86
87    return SYS_ERR_OK;
88}
89