1/*
2 * Copyright (c) 2013, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10#ifndef DRIVERKIT_H
11#define DRIVERKIT_H
12
13#include <barrelfish/types.h>
14#include <errors/errno.h>
15#include <collections/list.h>
16
17struct bfdriver;
18struct bfdriver_instance;
19
20/**
21 * Kaluga passes a CNode with capabilities to the pci driver. The offset
22 * in this CNode are defined here
23 */
24#define DRIVERKIT_ARGCN_SLOT_IOMMU      0
25#define DRIVERKIT_ARGCN_SLOT_INT        1
26#define DRIVERKIT_ARGCN_SLOT_PCI_EP     2
27#define DRIVERKIT_ARGCN_SLOT_KALUGA_EP  3
28#define DRIVERKIT_ARGCN_SLOT_BAR0       4
29#define DRIVERKIT_ARGCN_SLOT_BAR1       5
30#define DRIVERKIT_ARGCN_SLOT_BAR2       6
31#define DRIVERKIT_ARGCN_SLOT_BAR3       7
32#define DRIVERKIT_ARGCN_SLOT_BAR4       8
33#define DRIVERKIT_ARGCN_SLOT_BAR5       9
34#define DRIVERKIT_ARGCN_SLOT_BAR6       10
35#define DRIVERKIT_ARGCN_SLOT_MAX        11
36
37typedef errval_t(*driverkit_get_ep_fn)(struct bfdriver_instance*, bool lmp, struct capref*);
38
39// Generic struc to track all driver instance state.
40struct bfdriver_instance {
41    char name[256]; //< This is owned by driverkit 'modules.c'.
42    struct bfdriver* driver; //< This is owned by driverkit 'modules.c'.
43    uint32_t core;
44
45    struct capref caps[6];
46    uint8_t capc;
47    struct cnoderef argcn;
48    struct capref   argcn_cap;
49    char *argv[4];
50    char _argv[4][256];
51    uint8_t argc;
52
53    struct capref ctrl;
54    iref_t device; //< Driver state. This is owned by the driver implementation.
55    void* dstate;  //< Driver state. This is owned by the driver implementation.
56};
57
58typedef errval_t(*driver_init_fn)(struct bfdriver_instance*, uint64_t flags, iref_t*);
59typedef errval_t(*driver_attach_fn)(struct bfdriver_instance*);
60typedef errval_t(*driver_detach_fn)(struct bfdriver_instance*);
61typedef errval_t(*driver_set_sleep_level_fn)(struct bfdriver_instance*, uint32_t level);
62typedef errval_t(*driver_destroy_fn)(struct bfdriver_instance*);
63typedef errval_t(*driver_get_ep_fn)(struct bfdriver_instance*, bool lmp, struct capref* ret_cap);
64
65struct bfdriver {
66    char name[256];
67    driver_init_fn init;
68    driver_attach_fn attach;
69    driver_detach_fn detach;
70    driver_set_sleep_level_fn set_sleep_level;
71    driver_destroy_fn destroy;
72    driver_get_ep_fn get_ep;
73};
74
75errval_t driverkit_create_driver(const char* cls, struct bfdriver_instance *bfi,
76                                 uint64_t flags, iref_t* dev, struct capref* ctrl);
77errval_t driverkit_destroy(const char* name);
78void driverkit_list(struct bfdriver**, size_t*);
79struct bfdriver* driverkit_lookup_cls(const char*);
80
81
82errval_t driverkit_get_pci_cap(struct bfdriver_instance *bfi, struct capref *cap);
83errval_t driverkit_get_interrupt_cap(struct bfdriver_instance *bfi, struct capref *cap);
84errval_t driverkit_get_iommu_cap(struct bfdriver_instance *bfi, struct capref *cap);
85errval_t driverkit_get_bar_cap(struct bfdriver_instance *bfi, uint8_t idx,
86                               struct capref *cap);
87
88
89/** driver domain flounder interface */
90struct domain_instance {
91    uint64_t service_id;
92    collections_listnode* to_spawn;
93    collections_listnode* spawned;
94    struct ddomain_binding *b;
95};
96
97struct driver_instance {
98    char* driver_name;
99    char* inst_name;
100    size_t arg_idx;
101    char** args;
102    size_t cap_idx;
103    struct capref* caps;
104    uint64_t flags;
105    iref_t dev;
106
107    // Control interface
108    struct capref control_ep;
109    struct dcontrol_binding* ctrl;
110    bool bound;
111
112    struct capref argcn_cap;
113    struct cnoderef argcn;
114};
115errval_t ddomain_communication_init(iref_t kaluga_iref, uint64_t id);
116errval_t ddomain_controller_init(void);
117struct domain_instance* ddomain_create_domain_instance(uint64_t id);
118struct driver_instance* ddomain_create_driver_instance(char* driver_name, char* inst_name);
119errval_t ddomain_instantiate_driver(struct domain_instance* di, struct driver_instance* drv);
120void ddomain_free_driver_inst(void* arg);
121void ddomain_free_domain_inst(void* arg);
122errval_t ddomain_driver_add_cap(struct driver_instance* drv, struct capref cap);
123errval_t ddomain_driver_add_arg(struct driver_instance* drv, char *str);
124void ddomain_wait_for_id(void);
125
126/** driver control flounder interface */
127errval_t dcontrol_service_init(struct bfdriver_instance* bfi, struct waitset* ws,
128                                bool lmp, struct capref* ret_cap);
129errval_t map_device_register(lpaddr_t, size_t, lvaddr_t*);
130errval_t map_device_cap(struct capref, lvaddr_t *);
131
132errval_t driverkit_local_service_register(char* name, void* tbl);
133void* driverkit_local_service_lookup(char* name);
134
135#define __bfdrivers		__attribute__((__section__(".bfdrivers")))
136#define __visible       __attribute__((__externally_visible__))
137#define __waligned       __attribute__((__aligned__(sizeof(size_t))))
138#define __used          __attribute__((__used__))
139#define ___PASTE(a,b) a##b
140#define __PASTE(a,b) ___PASTE(a,b)
141#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
142
143#define DEFINE_MODULE(name, init_fn, attach_fn, detach_fn, sleep_fn, destroy_fn, get_ep_fn) \
144    struct bfdriver __UNIQUE_ID(name)               \
145        __used                                      \
146        __visible                                   \
147        __bfdrivers                                 \
148        __waligned =  {                             \
149        #name,                                      \
150        init_fn,                                    \
151        attach_fn,                                  \
152        detach_fn,                                  \
153        sleep_fn,                                   \
154        destroy_fn,                                  \
155        get_ep_fn                                  \
156    };
157
158
159
160
161
162#endif // DRIVERKIT_H
163