coord.h 3.63 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
#ifndef COORD_H_
#define COORD_H_

#include <deque>
#include <iostream>
30
#include <map>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
31

Antoine Kaufmann's avatar
Antoine Kaufmann committed
32
#include "sims/nic/corundum/debug.h"
Antoine Kaufmann's avatar
Antoine Kaufmann committed
33
34

class DMAOp;
35
struct MMIOOp;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
36
37
38

void pci_dma_issue(DMAOp *op);
void pci_msi_issue(uint8_t vec);
39
void pci_rwcomp_issue(MMIOOp *op);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
40
41

class PCICoordinator {
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
 protected:
  struct PCIOp {
    union {
      DMAOp *dma_op;
      MMIOOp *mmio_op;
      uint32_t msi_vec;
    };
    enum {
      OP_DMA,
      OP_MSI,
      OP_RWCOMP,
    } type;
    bool ready;
  };

  std::deque<PCIOp *> queue;
  std::map<DMAOp *, PCIOp *> dmamap;

  void process() {
    PCIOp *op;
    while (!queue.empty()) {
      op = queue.front();
      if (!op->ready)
        break;

      queue.pop_front();
      if (op->type == PCIOp::OP_MSI) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
69
#ifdef COORD_DEBUG
70
        std::cout << main_time << " issuing msi " << op->msi_vec << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
71
#endif
72
73
        pci_msi_issue(op->msi_vec);
      } else if (op->type == PCIOp::OP_DMA) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
74
#ifdef COORD_DEBUG
75
        std::cout << main_time << " issuing dma " << op->dma_op << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
76
#endif
77
78
79
        pci_dma_issue(op->dma_op);
        dmamap.erase(op->dma_op);
      } else if (op->type == PCIOp::OP_RWCOMP) {
80
#ifdef COORD_DEBUG
81
        std::cout << main_time << " issuing mmio " << op->mmio_op << std::endl;
82
#endif
83
84
85
86
87
88
89
90
91
92
93
        pci_rwcomp_issue(op->mmio_op);
      } else {
        throw "unknown type";
      }

      delete op;
    }
  }

 public:
  void dma_register(DMAOp *dma_op, bool ready) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
94
#ifdef COORD_DEBUG
95
96
    std::cout << main_time << " registering dma op " << dma_op << "  " << ready
              << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
97
#endif
98
99
100
101
    PCIOp *op = new PCIOp;
    op->dma_op = dma_op;
    op->type = PCIOp::OP_DMA;
    op->ready = ready;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
102

103
104
    queue.push_back(op);
    dmamap[dma_op] = op;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
105

106
107
    process();
  }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
108

109
  void dma_mark_ready(DMAOp *op) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
110
#ifdef COORD_DEBUG
111
    std::cout << main_time << " readying dma op " << op << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
112
#endif
113
    dmamap[op]->ready = true;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
114

115
116
    process();
  }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
117

118
  void msi_enqueue(uint32_t vec) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
119
#ifdef COORD_DEBUG
120
    std::cout << main_time << " enqueuing MSI " << vec << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
121
#endif
122
123
124
125
126
    PCIOp *op = new PCIOp;
    op->msi_vec = vec;
    op->type = PCIOp::OP_MSI;
    op->ready = true;
    queue.push_back(op);
127

128
129
    process();
  }
130

131
  void mmio_comp_enqueue(MMIOOp *mmio_op) {
132
#ifdef COORD_DEBUG
133
    std::cout << main_time << " enqueuing MMIO comp " << mmio_op << std::endl;
134
#endif
135
136
137
138
139
140
141
142
    PCIOp *op = new PCIOp;
    op->mmio_op = mmio_op;
    op->type = PCIOp::OP_RWCOMP;
    op->ready = true;
    queue.push_back(op);

    process();
  }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
143
144
};

Antoine Kaufmann's avatar
Antoine Kaufmann committed
145
#endif  // COORD_H_