nicbm.h 4.86 KB
Newer Older
1
#include <set>
2
#include <deque>
3
4
5
extern "C" {
    #include <nicsim.h>
}
6

7
8
9
10
11
12
13
namespace nicbm {

#include <cassert>


static const size_t MAX_DMA_LEN = 2048;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
14
15
16
17
18
19
20
class DMAOp {
    public:
        virtual ~DMAOp() { }
        bool write;
        uint64_t dma_addr;
        size_t len;
        void *data;
21
22
};

23
24
25
26
27
28
29
class TimedEvent {
    public:
        virtual ~TimedEvent() { }
        uint64_t time;
};


Antoine Kaufmann's avatar
Antoine Kaufmann committed
30
31
32
33
34
35
/**
 * The Runner drives the main simulation loop. It's initialized with a reference
 * to a device it should manage, and then once `runMain` is called, it will
 * start interacting with the PCI and Ethernet queue and forwarding calls to the
 * device as needed.
 * */
36
37
38
class Runner {
    public:
        class Device {
39
40
41
42
43
            protected:
                bool int_intx_en;
                bool int_msi_en;
                bool int_msix_en;

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
            public:
                /**
                 * Initialize device specific parameters (pci dev/vendor id,
                 * BARs etc. in intro struct.
                 */
                virtual void setup_intro(struct cosim_pcie_proto_dev_intro &di)
                    = 0;

                /**
                 * execute a register read from `bar`:`addr` of length `len`.
                 * Should store result in `dest`.
                 */
                virtual void reg_read(uint8_t bar, uint64_t addr, void *dest,
                        size_t len) = 0;

                /**
                 * execute a register write to `bar`:`addr` of length `len`,
                 * with the data in `src`.
                 */
                virtual void reg_write(uint8_t bar, uint64_t addr,
                        const void *src, size_t len) = 0;

                /**
                 * the previously issued DMA operation `op` completed.
                 */
                virtual void dma_complete(DMAOp &op) = 0;

                /**
                 * A packet has arrived on the wire, of length `len` with
                 * payload `data`.
                 */
                virtual void eth_rx(uint8_t port, const void *data, size_t len)
                    = 0;
77
78
79
80
81

                /**
                 * A timed event is due.
                 */
                virtual void timed_event(TimedEvent &ev);
82
83
84
85
86
87

                /**
                 * Device control update
                 */
                virtual void devctrl_update(
                        struct cosim_pcie_proto_h2d_devctrl &devctrl);
88
89
90
        };

    protected:
91
92
93
94
95
96
97
        struct event_cmp {
            bool operator() (TimedEvent *a, TimedEvent *b)
            {
                return a->time < b->time;
            }
        };

98
        Device &dev;
99
        std::set<TimedEvent *, event_cmp> events;
100
101
        std::deque<DMAOp *> dma_queue;
        size_t dma_pending;
102
        uint64_t mac_addr;
103
        struct nicsim_params nsparams;
104
        struct cosim_pcie_proto_dev_intro dintro;
105
106
107
108
109
110
111
112

        volatile union cosim_pcie_proto_d2h *d2h_alloc(void);
        volatile union cosim_eth_proto_d2n *d2n_alloc(void);

        void h2d_read(volatile struct cosim_pcie_proto_h2d_read *read);
        void h2d_write(volatile struct cosim_pcie_proto_h2d_write *write);
        void h2d_readcomp(volatile struct cosim_pcie_proto_h2d_readcomp *rc);
        void h2d_writecomp(volatile struct cosim_pcie_proto_h2d_writecomp *wc);
113
        void h2d_devctrl(volatile struct cosim_pcie_proto_h2d_devctrl *dc);
114
115
116
117
118
        void poll_h2d();

        void eth_recv(volatile struct cosim_eth_proto_n2d_recv *recv);
        void poll_n2d();

119
120
        bool event_next(uint64_t &retval);
        void event_trigger();
121
122
123

        void dma_do(DMAOp &op);
        void dma_trigger();
124
125
126
    public:
        Runner(Device &dev_);

Antoine Kaufmann's avatar
Antoine Kaufmann committed
127
        /** Run the simulation */
128
129
130
131
132
        int runMain(int argc, char *argv[]);

        /* these three are for `Runner::Device`. */
        void issue_dma(DMAOp &op);
        void msi_issue(uint8_t vec);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
133
        void msix_issue(uint8_t vec);
134
        void eth_send(const void *data, size_t len);
135

136
137
138
        void event_schedule(TimedEvent &evt);
        void event_cancel(TimedEvent &evt);

139
        uint64_t time_ps() const;
140
        uint64_t get_mac_addr() const;
141
142
};

Antoine Kaufmann's avatar
Antoine Kaufmann committed
143
144
145
/**
 * Very simple device that just has one register size.
 */
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
template <class TReg = uint32_t>
class SimpleDevice : public Runner::Device {
    public:
        virtual TReg reg_read(uint8_t bar, uint64_t addr) = 0;
        virtual void reg_write(uint8_t bar, uint64_t addr, TReg val) = 0;

        virtual void reg_read(uint8_t bar, uint64_t addr, void *dest,
                size_t len)
        {
            assert(len == sizeof(TReg));
            TReg r = reg_read(bar, addr);
            memcpy(dest, &r, sizeof(r));
        }

        virtual void reg_write(uint8_t bar, uint64_t addr,
                const void *src, size_t len)
        {
            assert(len == sizeof(TReg));
            TReg r;
            memcpy(&r, src, sizeof(r));
            reg_write(bar, addr, r);
        }
};
}