2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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.
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.
32 #ifndef __DEV_IO_DEVICE_HH__
33 #define __DEV_IO_DEVICE_HH__
35 #include "base/chunk_generator.hh"
36 #include "mem/mem_object.hh"
37 #include "mem/packet_impl.hh"
38 #include "sim/eventq.hh"
39 #include "sim/sim_object.hh"
47 * The PioPort class is a programmed i/o port that all devices that are
48 * sensitive to an address range use. The port takes all the memory
49 * access types and roles them into one read() and write() call that the device
50 * must respond to. The device must also provide the addressRanges() function
51 * with which it returns the address ranges it is interested in. An extra
52 * sendTiming() function is implemented which takes an delay. In this way the
53 * device can immediatly call sendTiming(pkt, time) after processing a request
54 * and the request will be handled by the port even if the port bus the device
55 * connects to is blocked.
57 class PioPort : public Port
60 /** The device that this port serves. */
63 /** The system that device/port are in. This is used to select which mode
64 * we are currently operating in. */
67 /** A list of outgoing timing response packets that haven't been serviced
69 std::list<Packet*> transmitList;
71 /** The current status of the peer(bus) that we are connected to. */
74 virtual bool recvTiming(Packet *pkt);
76 virtual Tick recvAtomic(Packet *pkt);
78 virtual void recvFunctional(Packet *pkt) ;
80 virtual void recvStatusChange(Status status)
81 { peerStatus = status; }
83 virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop);
85 void resendNacked(Packet *pkt);
88 * This class is used to implemented sendTiming() with a delay. When a delay
89 * is requested a new event is created. When the event time expires it
90 * attempts to send the packet. If it cannot, the packet is pushed onto the
91 * transmit list to be sent when recvRetry() is called. */
92 class SendEvent : public Event
97 SendEvent(PioPort *p, Packet *pkt, Tick t)
98 : Event(&mainEventQueue), port(p), packet(pkt)
99 { schedule(curTick + t); }
101 virtual void process();
103 virtual const char *description()
104 { return "Future scheduled sendTiming event"; }
106 friend class PioPort;
109 /** Number of timing requests that are emulating the device timing before
110 * attempting to end up on the bus.
114 /** If we need to drain, keep the drain event around until we're done
118 /** Schedule a sendTiming() event to be called in the future. */
119 void sendTiming(Packet *pkt, Tick time)
120 { outTiming++; new PioPort::SendEvent(this, pkt, time); }
122 /** This function is notification that the device should attempt to send a
124 virtual void recvRetry();
127 PioPort(PioDevice *dev, System *s, std::string pname = "-pioport");
129 unsigned int drain(Event *de);
131 friend class PioPort::SendEvent;
135 class DmaPort : public Port
138 struct DmaReqState : public Packet::SenderState
140 /** Event to call on the device when this transaction (all packets)
142 Event *completionEvent;
144 /** Where we came from for some sanity checking. */
147 /** Total number of bytes that this transaction involves. */
150 /** Number of bytes that have been acked for this transaction. */
153 DmaReqState(Event *ce, Port *p, Addr tb)
154 : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0)
159 std::list<Packet*> transmitList;
161 /** The system that device/port are in. This is used to select which mode
162 * we are currently operating in. */
165 /** Number of outstanding packets the dma port has. */
168 /** If a dmaAction is in progress. */
169 int actionInProgress;
171 /** If we need to drain, keep the drain event around until we're done
175 virtual bool recvTiming(Packet *pkt);
176 virtual Tick recvAtomic(Packet *pkt)
177 { panic("dma port shouldn't be used for pio access."); }
178 virtual void recvFunctional(Packet *pkt)
179 { panic("dma port shouldn't be used for pio access."); }
181 virtual void recvStatusChange(Status status)
184 virtual void recvRetry() ;
186 virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
187 { resp.clear(); snoop.clear(); }
189 void sendDma(Packet *pkt, bool front = false);
192 DmaPort(DmaDevice *dev, System *s);
194 void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
195 uint8_t *data = NULL);
197 bool dmaPending() { return pendingCount > 0; }
199 unsigned int drain(Event *de);
203 * This device is the base class which all devices senstive to an address range
204 * inherit from. There are three pure virtual functions which all devices must
205 * implement addressRanges(), read(), and write(). The magic do choose which
206 * mode we are in, etc is handled by the PioPort so the device doesn't have to
210 class PioDevice : public MemObject
214 /** The platform we are in. This is used to decide what type of memory
215 * transaction we should perform. */
220 /** The pioPort that handles the requests for us and provides us requests
224 virtual void addressRanges(AddrRangeList &range_list) = 0;
226 /** As far as the devices are concerned they only accept atomic transactions
227 * which are converted to either a write or a read. */
228 Tick recvAtomic(Packet *pkt)
229 { return pkt->isRead() ? this->read(pkt) : this->write(pkt); }
231 /** Pure virtual function that the device must implement. Called when a read
232 * command is recieved by the port.
233 * @param pkt Packet describing this request
234 * @return number of ticks it took to complete
236 virtual Tick read(Packet *pkt) = 0;
238 /** Pure virtual function that the device must implement. Called when a
239 * write command is recieved by the port.
240 * @param pkt Packet describing this request
241 * @return number of ticks it took to complete
243 virtual Tick write(Packet *pkt) = 0;
246 /** Params struct which is extended through each device based on the
247 * parameters it needs. Since we are re-writing everything, we might as well
248 * start from the bottom this time. */
261 const Params *params() const { return _params; }
264 : MemObject(p->name), platform(p->platform), sys(p->system),
265 pioPort(NULL), _params(p)
268 virtual ~PioDevice();
272 virtual unsigned int drain(Event *de);
274 virtual Port *getPort(const std::string &if_name, int idx = -1)
276 if (if_name == "pio") {
278 panic("pio port already connected to.");
279 pioPort = new PioPort(this, sys);
284 friend class PioPort;
288 class BasicPioDevice : public PioDevice
291 struct Params : public PioDevice::Params
298 /** Address that the device listens to. */
301 /** Size that the device's address range. */
304 /** Delay that the device experinces on an access. */
308 BasicPioDevice(Params *p)
309 : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), pioDelay(p->pio_delay)
312 /** return the address ranges that this device responds to.
313 * @params range_list range list to populate with ranges
315 void addressRanges(AddrRangeList &range_list);
319 class DmaDevice : public PioDevice
325 DmaDevice(Params *p);
326 virtual ~DmaDevice();
328 void dmaWrite(Addr addr, int size, Event *event, uint8_t *data)
329 { dmaPort->dmaAction(Packet::WriteReq, addr, size, event, data) ; }
331 void dmaRead(Addr addr, int size, Event *event, uint8_t *data = NULL)
332 { dmaPort->dmaAction(Packet::ReadReq, addr, size, event, data); }
334 bool dmaPending() { return dmaPort->dmaPending(); }
336 virtual unsigned int drain(Event *de);
338 virtual Port *getPort(const std::string &if_name, int idx = -1)
340 if (if_name == "pio") {
342 panic("pio port already connected to.");
343 pioPort = new PioPort(this, sys);
345 } else if (if_name == "dma") {
347 panic("dma port already connected to.");
348 dmaPort = new DmaPort(this, sys);
354 friend class DmaPort;
358 #endif // __DEV_IO_DEVICE_HH__