dma.h 3.75 KB
Newer Older
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Copyright 2021 Max Planck Institute for Software Systems, and
 * National University of Singapore
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

Antoine Kaufmann's avatar
Antoine Kaufmann committed
25
26
27
28
29
30
31
32
33
#ifndef MEM_H_
#define MEM_H_

#include <set>
#include <deque>

#include "Vinterface.h"
#include "verilated.h"

Antoine Kaufmann's avatar
Antoine Kaufmann committed
34
35
36
#include "debug.h"
#include "coord.h"

Antoine Kaufmann's avatar
Antoine Kaufmann committed
37
38
39
40
#define MAX_DMA_LEN 2048

class DMAEngine;
class MemWriter;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
41
class MemReader;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
42
43
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
77
78
79
80
81
82
83
84
85

struct DMAPorts {
    /* inputs to DMA engine */
    vluint64_t &dma_addr;
    vluint8_t  &dma_ram_sel;
    vluint32_t &dma_ram_addr;
    vluint16_t &dma_len;
    vluint8_t  &dma_tag;
    vluint8_t  &dma_valid;

    /* outputs of DMA engine */
    vluint8_t &dma_ready;
    vluint8_t &dma_status_tag;
    vluint8_t &dma_status_valid;


    DMAPorts(vluint64_t &dma_addr_, vluint8_t &dma_ram_sel_,
            vluint32_t &dma_ram_addr_, vluint16_t &dma_len_,
            vluint8_t &dma_tag_, vluint8_t &dma_valid_,
            vluint8_t &dma_ready_, vluint8_t &dma_status_tag_,
            vluint8_t &dma_status_valid_)
        : dma_addr(dma_addr_), dma_ram_sel(dma_ram_sel_),
        dma_ram_addr(dma_ram_addr_), dma_len(dma_len_),
        dma_tag(dma_tag_), dma_valid(dma_valid_),
        dma_ready(dma_ready_), dma_status_tag(dma_status_tag_),
        dma_status_valid(dma_status_valid_)
    {
    }
};

struct DMAOp {
    DMAEngine *engine;
    uint64_t dma_addr;
    size_t len;
    uint64_t ram_addr;
    bool write;
    uint8_t  ram_sel;
    uint8_t tag;
    uint8_t data[MAX_DMA_LEN];
};

class DMAEngine {
    protected:
        DMAPorts &p;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
86
        PCICoordinator &coord;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
87

Antoine Kaufmann's avatar
Antoine Kaufmann committed
88
89
        DMAEngine(DMAPorts &p_, PCICoordinator &coord_)
            : p(p_), coord(coord_) { }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103

    public:
        virtual void pci_op_complete(DMAOp *op) = 0;
        virtual void mem_op_complete(DMAOp *op) = 0;
};

class DMAReader : public DMAEngine {
    protected:
        std::set<DMAOp *> pending;
        std::deque<DMAOp *> completed;
        const char *label;
        MemWriter &mw;

    public:
Antoine Kaufmann's avatar
Antoine Kaufmann committed
104
105
106
        DMAReader(const char *label_, DMAPorts &p_, MemWriter &mw_,
                PCICoordinator &coord_)
            : DMAEngine(p_, coord_), label(label_), mw(mw_)
Antoine Kaufmann's avatar
Antoine Kaufmann committed
107
108
109
110
111
112
113
114
        {
        }

        virtual void pci_op_complete(DMAOp *op);
        virtual void mem_op_complete(DMAOp *op);
        void step();
};

Antoine Kaufmann's avatar
Antoine Kaufmann committed
115
116
117
118
119
120
121
122
class DMAWriter : public DMAEngine {
    protected:
        std::set<DMAOp *> pending;
        std::deque<DMAOp *> completed;
        const char *label;
        MemReader &mr;

    public:
Antoine Kaufmann's avatar
Antoine Kaufmann committed
123
124
125
        DMAWriter(const char *label_, DMAPorts &p_, MemReader &mr_,
                PCICoordinator &coord_)
            : DMAEngine(p_, coord_), label(label_), mr(mr_)
Antoine Kaufmann's avatar
Antoine Kaufmann committed
126
127
128
129
130
131
132
133
134
        {
        }

        virtual void pci_op_complete(DMAOp *op);
        virtual void mem_op_complete(DMAOp *op);
        void step();
};


Antoine Kaufmann's avatar
Antoine Kaufmann committed
135
#endif /* ndef DMA_H_ */