summaryrefslogtreecommitdiff
path: root/src/mem/ruby/system/DMASequencer.cc
blob: d29dba6021a3db0b1b7912dfc2e22a6b6fdb0626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

#include "mem/ruby/system/DMASequencer.hh"
#include "mem/ruby/buffers/MessageBuffer.hh"
#include "mem/ruby/slicc_interface/AbstractController.hh"

/* SLICC generated types */
#include "mem/protocol/DMARequestMsg.hh"
#include "mem/protocol/DMARequestType.hh"
#include "mem/protocol/DMAResponseMsg.hh"
#include "mem/ruby/system/System.hh"

DMASequencer::DMASequencer(const string & name)
  : RubyPort(name)
{
}

void DMASequencer::init(const vector<string> & argv)
{
  m_version = -1;
  m_controller = NULL;
  for (size_t i=0;i<argv.size();i+=2) {
    if (argv[i] == "controller")
      m_controller = RubySystem::getController(argv[i+1]);
    else if (argv[i] == "version")
      m_version = atoi(argv[i+1].c_str());
  }
  assert(m_controller != NULL);
  assert(m_version != -1);

  m_mandatory_q_ptr = m_controller->getMandatoryQueue();
  m_is_busy = false;
  m_data_block_mask = ~ (~0 << RubySystem::getBlockSizeBits());
}

int64_t DMASequencer::makeRequest(const RubyRequest & request)
{
  uint64_t paddr = request.paddr;
  uint8_t* data = request.data;
  int len = request.len;
  bool write = false;
  switch(request.type) {
  case RubyRequestType_LD:
    write = false;
    break;
  case RubyRequestType_ST:
    write = true;
    break;
  case RubyRequestType_NULL:
  case RubyRequestType_IFETCH:
  case RubyRequestType_Locked_Read:
  case RubyRequestType_Locked_Write:
  case RubyRequestType_RMW_Read:
  case RubyRequestType_RMW_Write:
  case RubyRequestType_NUM:
    assert(0);
  }

  assert(!m_is_busy);  // only support one outstanding DMA request
  m_is_busy = true;

  active_request.start_paddr = paddr;
  active_request.write = write;
  active_request.data = data;
  active_request.len = len;
  active_request.bytes_completed = 0;
  active_request.bytes_issued = 0;
  active_request.id = makeUniqueRequestID();

  DMARequestMsg msg;
  msg.getPhysicalAddress() = Address(paddr);
  msg.getLineAddress() = line_address(msg.getPhysicalAddress());
  msg.getType() = write ? DMARequestType_WRITE : DMARequestType_READ;
  msg.getOffset() = paddr & m_data_block_mask;
  msg.getLen() = (msg.getOffset() + len) <= RubySystem::getBlockSizeBytes() ?
    len : 
    RubySystem::getBlockSizeBytes() - msg.getOffset();
  if (write) {
    msg.getType() = DMARequestType_WRITE;
    msg.getDataBlk().setData(data, msg.getOffset(), msg.getLen());
  } else {
    msg.getType() = DMARequestType_READ;
  }
  m_mandatory_q_ptr->enqueue(msg);
  active_request.bytes_issued += msg.getLen();

  return active_request.id;
}

void DMASequencer::issueNext()
{
  assert(m_is_busy == true);
  active_request.bytes_completed = active_request.bytes_issued;
  if (active_request.len == active_request.bytes_completed) {
    m_hit_callback(active_request.id);
    m_is_busy = false;
    return;
  }

  DMARequestMsg msg;
  msg.getPhysicalAddress() = Address(active_request.start_paddr + 
				     active_request.bytes_completed);
  assert((msg.getPhysicalAddress().getAddress() & m_data_block_mask) == 0);
  msg.getLineAddress() = line_address(msg.getPhysicalAddress());
  msg.getOffset() = 0;
  msg.getType() = (active_request.write ? DMARequestType_WRITE : 
		   DMARequestType_READ);
  msg.getLen() = (active_request.len - 
		  active_request.bytes_completed < RubySystem::getBlockSizeBytes() ?
		  active_request.len - active_request.bytes_completed :
		  RubySystem::getBlockSizeBytes());
  if (active_request.write) {
    msg.getDataBlk().setData(&active_request.data[active_request.bytes_completed], 
			     0, msg.getLen());
    msg.getType() = DMARequestType_WRITE;
  } else {
    msg.getType() = DMARequestType_READ;
  }
  m_mandatory_q_ptr->enqueue(msg);
  active_request.bytes_issued += msg.getLen();
}

void DMASequencer::dataCallback(const DataBlock & dblk)
{
  assert(m_is_busy == true);
  int len = active_request.bytes_issued - active_request.bytes_completed;
  int offset = 0;
  if (active_request.bytes_completed == 0)
    offset = active_request.start_paddr & m_data_block_mask;
  assert( active_request.write == false );
  memcpy(&active_request.data[active_request.bytes_completed], 
	 dblk.getData(offset, len), len);
  issueNext();
}

void DMASequencer::ackCallback()
{
  issueNext();
}

void DMASequencer::printConfig(ostream & out)
{

}