1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29
30machine(MachineType:DMA, "DMA Controller") 
31    : DMASequencer * dma_sequencer;
32      Cycles request_latency := 6;
33
34      MessageBuffer * responseFromDir, network="From", virtual_network="1",
35            vnet_type="response";
36      MessageBuffer * requestToDir, network="To", virtual_network="0",
37            vnet_type="request";
38      MessageBuffer * mandatoryQueue;
39{
40  state_declaration(State, desc="DMA states", default="DMA_State_READY") {
41    READY, AccessPermission:Invalid, desc="Ready to accept a new request";
42    BUSY_RD, AccessPermission:Busy, desc="Busy: currently processing a request";
43    BUSY_WR, AccessPermission:Busy, desc="Busy: currently processing a request";
44  }
45
46  enumeration(Event, desc="DMA events") {
47    ReadRequest,  desc="A new read request";
48    WriteRequest, desc="A new write request";
49    Data,         desc="Data from a DMA memory read";
50    Ack,          desc="DMA write to memory completed";
51  }
52
53  structure(TBE, desc="...") {
54    State TBEState,    desc="Transient state";
55    DataBlock DataBlk, desc="Data";
56  }
57
58  structure(TBETable, external = "yes") {
59    TBE lookup(Addr);
60    void allocate(Addr);
61    void deallocate(Addr);
62    bool isPresent(Addr);
63  }
64
65  void set_tbe(TBE b);
66  void unset_tbe();
67  void wakeUpAllBuffers();
68
69  TBETable TBEs, template="<DMA_TBE>", constructor="m_number_of_TBEs";
70
71  Tick clockEdge();
72  MachineID mapAddressToMachine(Addr addr, MachineType mtype);
73
74  State getState(TBE tbe, Addr addr) {
75    if (is_valid(tbe)) {
76        return tbe.TBEState;
77    } else {
78        return State:READY;
79    }
80  }
81
82  void setState(TBE tbe, Addr addr, State state) {
83    if (is_valid(tbe)) {
84        tbe.TBEState := state;
85    }
86  }
87
88  AccessPermission getAccessPermission(Addr addr) {
89    return AccessPermission:NotPresent;
90  }
91
92  void setAccessPermission(Addr addr, State state) {
93  }
94
95  void functionalRead(Addr addr, Packet *pkt) {
96    error("DMA does not support functional read.");
97  }
98
99  int functionalWrite(Addr addr, Packet *pkt) {
100    error("DMA does not support functional write.");
101  }
102
103  out_port(requestToDir_out, DMARequestMsg, requestToDir, desc="...");
104
105  in_port(dmaRequestQueue_in, SequencerMsg, mandatoryQueue, desc="...") {
106    if (dmaRequestQueue_in.isReady(clockEdge())) {
107      peek(dmaRequestQueue_in, SequencerMsg) {
108        if (in_msg.Type == SequencerRequestType:LD ) {
109          trigger(Event:ReadRequest, in_msg.LineAddress, TBEs[in_msg.LineAddress]);
110        } else if (in_msg.Type == SequencerRequestType:ST) {
111          trigger(Event:WriteRequest, in_msg.LineAddress, TBEs[in_msg.LineAddress]);
112        } else {
113          error("Invalid request type");
114        }
115      }
116    }
117  }
118
119  in_port(dmaResponseQueue_in, DMAResponseMsg, responseFromDir, desc="...") {
120    if (dmaResponseQueue_in.isReady(clockEdge())) {
121      peek( dmaResponseQueue_in, DMAResponseMsg) {
122        if (in_msg.Type == DMAResponseType:ACK) {
123          trigger(Event:Ack, in_msg.LineAddress, TBEs[in_msg.LineAddress]);
124        } else if (in_msg.Type == DMAResponseType:DATA) {
125          trigger(Event:Data, in_msg.LineAddress, TBEs[in_msg.LineAddress]);
126        } else {
127          error("Invalid response type");
128        }
129      }
130    }
131  }
132
133  action(s_sendReadRequest, "s", desc="Send a DMA read request to memory") {
134    peek(dmaRequestQueue_in, SequencerMsg) {
135      enqueue(requestToDir_out, DMARequestMsg, request_latency) {
136        out_msg.PhysicalAddress := in_msg.PhysicalAddress;
137        out_msg.LineAddress := in_msg.LineAddress; 
138        out_msg.Type := DMARequestType:READ;
139        out_msg.Requestor := machineID;
140        out_msg.DataBlk := in_msg.DataBlk;
141        out_msg.Len := in_msg.Len;
142        out_msg.Destination.add(mapAddressToMachine(address, MachineType:Directory));
143        out_msg.MessageSize := MessageSizeType:Writeback_Control;
144      }
145    }
146  }
147
148  action(s_sendWriteRequest, "\s", desc="Send a DMA write request to memory") {
149    peek(dmaRequestQueue_in, SequencerMsg) {
150      enqueue(requestToDir_out, DMARequestMsg, request_latency) {
151          out_msg.PhysicalAddress := in_msg.PhysicalAddress;
152          out_msg.LineAddress := in_msg.LineAddress; 
153          out_msg.Type := DMARequestType:WRITE;
154          out_msg.Requestor := machineID;
155          out_msg.DataBlk := in_msg.DataBlk;
156          out_msg.Len := in_msg.Len;
157          out_msg.Destination.add(mapAddressToMachine(address, MachineType:Directory));
158          out_msg.MessageSize := MessageSizeType:Writeback_Control;
159        }
160      }
161  }
162
163  action(a_ackCallback, "a", desc="Notify dma controller that write request completed") {
164    dma_sequencer.ackCallback(address);
165  }
166
167  action(d_dataCallback, "d", desc="Write data to dma sequencer") {
168    dma_sequencer.dataCallback(tbe.DataBlk, address);
169  }
170
171  action(t_updateTBEData, "t", desc="Update TBE Data") {
172    assert(is_valid(tbe));
173    peek( dmaResponseQueue_in, DMAResponseMsg) {
174        tbe.DataBlk := in_msg.DataBlk;
175    }
176  }
177
178  action(v_allocateTBE, "v", desc="Allocate TBE entry") {
179    TBEs.allocate(address);
180    set_tbe(TBEs[address]);
181  }
182
183  action(w_deallocateTBE, "w", desc="Deallocate TBE entry") {
184    TBEs.deallocate(address);
185    unset_tbe();
186  }
187
188  action(p_popRequestQueue, "p", desc="Pop request queue") {
189    dmaRequestQueue_in.dequeue(clockEdge());
190  }
191
192  action(p_popResponseQueue, "\p", desc="Pop request queue") {
193    dmaResponseQueue_in.dequeue(clockEdge());
194  }
195
196  action(zz_stallAndWaitRequestQueue, "zz", desc="...") {
197    stall_and_wait(dmaRequestQueue_in, address);
198  }
199
200  action(wkad_wakeUpAllDependents, "wkad", desc="wake-up all dependents") {
201    wakeUpAllBuffers();
202  }
203
204  transition(READY, ReadRequest, BUSY_RD) {
205    v_allocateTBE;
206    s_sendReadRequest;
207    p_popRequestQueue;
208  }
209
210  transition(READY, WriteRequest, BUSY_WR) {
211    v_allocateTBE;
212    s_sendWriteRequest;
213    p_popRequestQueue;
214  }
215
216  transition(BUSY_RD, Data, READY) {
217    t_updateTBEData;
218    d_dataCallback;
219    w_deallocateTBE;
220    p_popResponseQueue;
221    wkad_wakeUpAllDependents;
222  }
223
224  transition(BUSY_WR, Ack, READY) {
225    a_ackCallback;
226    w_deallocateTBE;
227    p_popResponseQueue;
228    wkad_wakeUpAllDependents;
229  }
230
231  transition({BUSY_RD,BUSY_WR}, {ReadRequest,WriteRequest}) {
232     zz_stallAndWaitRequestQueue;
233  }
234
235}
236