dma_device.hh revision 12087:0e082672ac6b
14776Sgblack@eecs.umich.edu/*
24776Sgblack@eecs.umich.edu * Copyright (c) 2012-2013, 2015, 2017 ARM Limited
34776Sgblack@eecs.umich.edu * All rights reserved.
44776Sgblack@eecs.umich.edu *
54776Sgblack@eecs.umich.edu * The license below extends only to copyright in the software and shall
64776Sgblack@eecs.umich.edu * not be construed as granting a license to any other intellectual
74776Sgblack@eecs.umich.edu * property including but not limited to intellectual property relating
84776Sgblack@eecs.umich.edu * to a hardware implementation of the functionality of the software
94776Sgblack@eecs.umich.edu * licensed hereunder.  You may use the software subject to the license
104776Sgblack@eecs.umich.edu * terms below provided that you ensure that this notice is replicated
114776Sgblack@eecs.umich.edu * unmodified and in its entirety in all distributions of the software,
124776Sgblack@eecs.umich.edu * modified or unmodified, in source code or in binary form.
134776Sgblack@eecs.umich.edu *
144776Sgblack@eecs.umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan
154776Sgblack@eecs.umich.edu * All rights reserved.
164776Sgblack@eecs.umich.edu *
174776Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
184776Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
194776Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
204776Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
214776Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
224776Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
234776Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
244776Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
254776Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
264776Sgblack@eecs.umich.edu * this software without specific prior written permission.
274776Sgblack@eecs.umich.edu *
284776Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
294776Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
304776Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
314776Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
324776Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
334776Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
344776Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
354832Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
364776Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
374776Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
384776Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
394776Sgblack@eecs.umich.edu *
404776Sgblack@eecs.umich.edu * Authors: Ali Saidi
414776Sgblack@eecs.umich.edu *          Nathan Binkert
424776Sgblack@eecs.umich.edu *          Andreas Sandberg
434776Sgblack@eecs.umich.edu */
444776Sgblack@eecs.umich.edu
454776Sgblack@eecs.umich.edu#ifndef __DEV_DMA_DEVICE_HH__
464776Sgblack@eecs.umich.edu#define __DEV_DMA_DEVICE_HH__
474776Sgblack@eecs.umich.edu
484776Sgblack@eecs.umich.edu#include <deque>
494776Sgblack@eecs.umich.edu#include <memory>
504776Sgblack@eecs.umich.edu
514776Sgblack@eecs.umich.edu#include "base/circlebuf.hh"
524776Sgblack@eecs.umich.edu#include "dev/io_device.hh"
534776Sgblack@eecs.umich.edu#include "params/DmaDevice.hh"
544776Sgblack@eecs.umich.edu#include "sim/drain.hh"
554776Sgblack@eecs.umich.edu#include "sim/system.hh"
564776Sgblack@eecs.umich.edu
574776Sgblack@eecs.umich.educlass DmaPort : public MasterPort, public Drainable
584776Sgblack@eecs.umich.edu{
594776Sgblack@eecs.umich.edu  private:
604776Sgblack@eecs.umich.edu
614776Sgblack@eecs.umich.edu    /**
624776Sgblack@eecs.umich.edu     * Take the first packet of the transmit list and attempt to send
634776Sgblack@eecs.umich.edu     * it as a timing request. If it is successful, schedule the
644776Sgblack@eecs.umich.edu     * sending of the next packet, otherwise remember that we are
654776Sgblack@eecs.umich.edu     * waiting for a retry.
664776Sgblack@eecs.umich.edu     */
674776Sgblack@eecs.umich.edu    void trySendTimingReq();
684776Sgblack@eecs.umich.edu
694776Sgblack@eecs.umich.edu    /**
704776Sgblack@eecs.umich.edu     * For timing, attempt to send the first item on the transmit
714776Sgblack@eecs.umich.edu     * list, and if it is successful and there are more packets
724776Sgblack@eecs.umich.edu     * waiting, then schedule the sending of the next packet. For
734776Sgblack@eecs.umich.edu     * atomic, simply send and process everything on the transmit
744776Sgblack@eecs.umich.edu     * list.
754776Sgblack@eecs.umich.edu     */
764776Sgblack@eecs.umich.edu    void sendDma();
774776Sgblack@eecs.umich.edu
784776Sgblack@eecs.umich.edu    /**
794776Sgblack@eecs.umich.edu     * Handle a response packet by updating the corresponding DMA
804776Sgblack@eecs.umich.edu     * request state to reflect the bytes received, and also update
814776Sgblack@eecs.umich.edu     * the pending request counter. If the DMA request that this
824776Sgblack@eecs.umich.edu     * packet is part of is complete, then signal the completion event
834776Sgblack@eecs.umich.edu     * if present, potentially with a delay added to it.
844776Sgblack@eecs.umich.edu     *
854776Sgblack@eecs.umich.edu     * @param pkt Response packet to handler
864776Sgblack@eecs.umich.edu     * @param delay Additional delay for scheduling the completion event
874776Sgblack@eecs.umich.edu     */
884776Sgblack@eecs.umich.edu    void handleResp(PacketPtr pkt, Tick delay = 0);
894776Sgblack@eecs.umich.edu
904776Sgblack@eecs.umich.edu    struct DmaReqState : public Packet::SenderState
914776Sgblack@eecs.umich.edu    {
924776Sgblack@eecs.umich.edu        /** Event to call on the device when this transaction (all packets)
934776Sgblack@eecs.umich.edu         * complete. */
944776Sgblack@eecs.umich.edu        Event *completionEvent;
954776Sgblack@eecs.umich.edu
964776Sgblack@eecs.umich.edu        /** Total number of bytes that this transaction involves. */
974776Sgblack@eecs.umich.edu        const Addr totBytes;
984776Sgblack@eecs.umich.edu
994776Sgblack@eecs.umich.edu        /** Number of bytes that have been acked for this transaction. */
1004776Sgblack@eecs.umich.edu        Addr numBytes;
1014776Sgblack@eecs.umich.edu
1024776Sgblack@eecs.umich.edu        /** Amount to delay completion of dma by */
1034776Sgblack@eecs.umich.edu        const Tick delay;
1044776Sgblack@eecs.umich.edu
1054776Sgblack@eecs.umich.edu        DmaReqState(Event *ce, Addr tb, Tick _delay)
1064776Sgblack@eecs.umich.edu            : completionEvent(ce), totBytes(tb), numBytes(0), delay(_delay)
1074776Sgblack@eecs.umich.edu        {}
1084776Sgblack@eecs.umich.edu    };
1094776Sgblack@eecs.umich.edu
1104776Sgblack@eecs.umich.edu  public:
1114776Sgblack@eecs.umich.edu    /** The device that owns this port. */
1124776Sgblack@eecs.umich.edu    MemObject *const device;
1134776Sgblack@eecs.umich.edu
1144776Sgblack@eecs.umich.edu    /** The system that device/port are in. This is used to select which mode
1154776Sgblack@eecs.umich.edu     * we are currently operating in. */
1164776Sgblack@eecs.umich.edu    System *const sys;
1174776Sgblack@eecs.umich.edu
1184776Sgblack@eecs.umich.edu    /** Id for all requests */
1194776Sgblack@eecs.umich.edu    const MasterID masterId;
1204776Sgblack@eecs.umich.edu
1214776Sgblack@eecs.umich.edu  protected:
1224776Sgblack@eecs.umich.edu    /** Use a deque as we never do any insertion or removal in the middle */
1234776Sgblack@eecs.umich.edu    std::deque<PacketPtr> transmitList;
1244776Sgblack@eecs.umich.edu
1254776Sgblack@eecs.umich.edu    /** Event used to schedule a future sending from the transmit list. */
1264776Sgblack@eecs.umich.edu    EventFunctionWrapper sendEvent;
1274776Sgblack@eecs.umich.edu
1284776Sgblack@eecs.umich.edu    /** Number of outstanding packets the dma port has. */
1294776Sgblack@eecs.umich.edu    uint32_t pendingCount;
1304776Sgblack@eecs.umich.edu
1314776Sgblack@eecs.umich.edu    /** If the port is currently waiting for a retry before it can
1325034Smilesck@eecs.umich.edu     * send whatever it is that it's sending. */
1334776Sgblack@eecs.umich.edu    bool inRetry;
1344776Sgblack@eecs.umich.edu
1354776Sgblack@eecs.umich.edu  protected:
1364776Sgblack@eecs.umich.edu
1374776Sgblack@eecs.umich.edu    bool recvTimingResp(PacketPtr pkt) override;
1384776Sgblack@eecs.umich.edu    void recvReqRetry() override;
1394776Sgblack@eecs.umich.edu
1404776Sgblack@eecs.umich.edu    void queueDma(PacketPtr pkt);
1414776Sgblack@eecs.umich.edu
1424776Sgblack@eecs.umich.edu  public:
1434776Sgblack@eecs.umich.edu
1444776Sgblack@eecs.umich.edu    DmaPort(MemObject *dev, System *s);
1454776Sgblack@eecs.umich.edu
1464776Sgblack@eecs.umich.edu    RequestPtr dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
1474776Sgblack@eecs.umich.edu                         uint8_t *data, Tick delay, Request::Flags flag = 0);
148
149    bool dmaPending() const { return pendingCount > 0; }
150
151    DrainState drain() override;
152};
153
154class DmaDevice : public PioDevice
155{
156   protected:
157    DmaPort dmaPort;
158
159  public:
160    typedef DmaDeviceParams Params;
161    DmaDevice(const Params *p);
162    virtual ~DmaDevice() { }
163
164    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data,
165                  Tick delay = 0)
166    {
167        dmaPort.dmaAction(MemCmd::WriteReq, addr, size, event, data, delay);
168    }
169
170    void dmaRead(Addr addr, int size, Event *event, uint8_t *data,
171                 Tick delay = 0)
172    {
173        dmaPort.dmaAction(MemCmd::ReadReq, addr, size, event, data, delay);
174    }
175
176    bool dmaPending() const { return dmaPort.dmaPending(); }
177
178    void init() override;
179
180    unsigned int cacheBlockSize() const { return sys->cacheLineSize(); }
181
182    BaseMasterPort &getMasterPort(const std::string &if_name,
183                                  PortID idx = InvalidPortID) override;
184
185};
186
187/**
188 * DMA callback class.
189 *
190 * Allows one to register for a callback event after a sequence of (potentially
191 * non-contiguous) DMA transfers on a DmaPort completes.  Derived classes must
192 * implement the process() method and use getChunkEvent() to allocate a
193 * callback event for each participating DMA.
194 */
195class DmaCallback : public Drainable
196{
197  public:
198    virtual const std::string name() const { return "DmaCallback"; }
199
200    /**
201     * DmaPort ensures that all oustanding DMA accesses have completed before
202     * it finishes draining.  However, DmaChunkEvents scheduled with a delay
203     * might still be sitting on the event queue.  Therefore, draining is not
204     * complete until count is 0, which ensures that all outstanding
205     * DmaChunkEvents associated with this DmaCallback have fired.
206     */
207    DrainState drain() override
208    {
209        return count ? DrainState::Draining : DrainState::Drained;
210    }
211
212  protected:
213    int count;
214
215    DmaCallback()
216        : count(0)
217    { }
218
219    virtual ~DmaCallback() { }
220
221    /**
222     * Callback function invoked on completion of all chunks.
223     */
224    virtual void process() = 0;
225
226  private:
227    /**
228     * Called by DMA engine completion event on each chunk completion.
229     * Since the object may delete itself here, callers should not use
230     * the object pointer after calling this function.
231     */
232    void chunkComplete()
233    {
234        if (--count == 0) {
235            process();
236            // Need to notify DrainManager that this object is finished
237            // draining, even though it is immediately deleted.
238            signalDrainDone();
239            delete this;
240        }
241    }
242
243    /**
244     * Event invoked by DmaDevice on completion of each chunk.
245     */
246    class DmaChunkEvent : public Event
247    {
248      private:
249        DmaCallback *callback;
250
251      public:
252        DmaChunkEvent(DmaCallback *cb)
253          : Event(Default_Pri, AutoDelete), callback(cb)
254        { }
255
256        void process() { callback->chunkComplete(); }
257    };
258
259  public:
260
261    /**
262     * Request a chunk event.  Chunks events should be provided to each DMA
263     * request that wishes to participate in this DmaCallback.
264     */
265    Event *getChunkEvent()
266    {
267        ++count;
268        return new DmaChunkEvent(this);
269    }
270};
271
272/**
273 * Buffered DMA engine helper class
274 *
275 * This class implements a simple DMA engine that feeds a FIFO
276 * buffer. The size of the buffer, the maximum number of pending
277 * requests and the maximum request size are all set when the engine
278 * is instantiated.
279 *
280 * An <i>asynchronous</i> transfer of a <i>block</i> of data
281 * (designated by a start address and a size) is started by calling
282 * the startFill() method. The DMA engine will aggressively try to
283 * keep the internal FIFO full. As soon as there is room in the FIFO
284 * for more data <i>and</i> there are free request slots, a new fill
285 * will be started.
286 *
287 * Data in the FIFO can be read back using the get() and tryGet()
288 * methods. Both request a block of data from the FIFO. However, get()
289 * panics if the block cannot be satisfied, while tryGet() simply
290 * returns false. The latter call makes it possible to implement
291 * custom buffer underrun handling.
292 *
293 * A simple use case would be something like this:
294 * \code{.cpp}
295 *     // Create a DMA engine with a 1KiB buffer. Issue up to 8 concurrent
296 *     // uncacheable 64 byte (maximum) requests.
297 *     DmaReadFifo *dma = new DmaReadFifo(port, 1024, 64, 8,
298 *                                        Request::UNCACHEABLE);
299 *
300 *     // Start copying 4KiB data from 0xFF000000
301 *     dma->startFill(0xFF000000, 0x1000);
302 *
303 *     // Some time later when there is data in the FIFO.
304 *     uint8_t data[8];
305 *     dma->get(data, sizeof(data))
306 * \endcode
307 *
308 *
309 * The DMA engine allows new blocks to be requested as soon as the
310 * last request for a block has been sent (i.e., there is no need to
311 * wait for pending requests to complete). This can be queried with
312 * the atEndOfBlock() method and more advanced implementations may
313 * override the onEndOfBlock() callback.
314 */
315class DmaReadFifo : public Drainable, public Serializable
316{
317  public:
318    DmaReadFifo(DmaPort &port, size_t size,
319                unsigned max_req_size,
320                unsigned max_pending,
321                Request::Flags flags = 0);
322
323    ~DmaReadFifo();
324
325  public: // Serializable
326    void serialize(CheckpointOut &cp) const override;
327    void unserialize(CheckpointIn &cp) override;
328
329  public: // Drainable
330    DrainState drain() override;
331
332  public: // FIFO access
333    /**
334     * @{
335     * @name FIFO access
336     */
337    /**
338     * Try to read data from the FIFO.
339     *
340     * This method reads len bytes of data from the FIFO and stores
341     * them in the memory location pointed to by dst. The method
342     * fails, and no data is written to the buffer, if the FIFO
343     * doesn't contain enough data to satisfy the request.
344     *
345     * @param dst Pointer to a destination buffer
346     * @param len Amount of data to read.
347     * @return true on success, false otherwise.
348     */
349    bool tryGet(uint8_t *dst, size_t len);
350
351    template<typename T>
352    bool tryGet(T &value) {
353        return tryGet(static_cast<T *>(&value), sizeof(T));
354    };
355
356    /**
357     * Read data from the FIFO and panic on failure.
358     *
359     * @see tryGet()
360     *
361     * @param dst Pointer to a destination buffer
362     * @param len Amount of data to read.
363     */
364    void get(uint8_t *dst, size_t len);
365
366    template<typename T>
367    T get() {
368        T value;
369        get(static_cast<uint8_t *>(&value), sizeof(T));
370        return value;
371    };
372
373    /** Get the amount of data stored in the FIFO */
374    size_t size() const { return buffer.size(); }
375    /** Flush the FIFO */
376    void flush() { buffer.flush(); }
377
378    /** @} */
379  public: // FIFO fill control
380    /**
381     * @{
382     * @name FIFO fill control
383     */
384    /**
385     * Start filling the FIFO.
386     *
387     * @warn It's considered an error to call start on an active DMA
388     * engine unless the last request from the active block has been
389     * sent (i.e., atEndOfBlock() is true).
390     *
391     * @param start Physical address to copy from.
392     * @param size Size of the block to copy.
393     */
394    void startFill(Addr start, size_t size);
395
396    /**
397     * Stop the DMA engine.
398     *
399     * Stop filling the FIFO and ignore incoming responses for pending
400     * requests. The onEndOfBlock() callback will not be called after
401     * this method has been invoked. However, once the last response
402     * has been received, the onIdle() callback will still be called.
403     */
404    void stopFill();
405
406    /**
407     * Has the DMA engine sent out the last request for the active
408     * block?
409     */
410    bool atEndOfBlock() const {
411        return nextAddr == endAddr;
412    }
413
414    /**
415     * Is the DMA engine active (i.e., are there still in-flight
416     * accesses)?
417     */
418    bool isActive() const {
419        return !(pendingRequests.empty() && atEndOfBlock());
420    }
421
422    /** @} */
423  protected: // Callbacks
424    /**
425     * @{
426     * @name Callbacks
427     */
428    /**
429     * End of block callback
430     *
431     * This callback is called <i>once</i> after the last access in a
432     * block has been sent. It is legal for a derived class to call
433     * startFill() from this method to initiate a transfer.
434     */
435    virtual void onEndOfBlock() {};
436
437    /**
438     * Last response received callback
439     *
440     * This callback is called when the DMA engine becomes idle (i.e.,
441     * there are no pending requests).
442     *
443     * It is possible for a DMA engine to reach the end of block and
444     * become idle at the same tick. In such a case, the
445     * onEndOfBlock() callback will be called first. This callback
446     * will <i>NOT</i> be called if that callback initiates a new DMA transfer.
447     */
448    virtual void onIdle() {};
449
450    /** @} */
451  private: // Configuration
452    /** Maximum request size in bytes */
453    const Addr maxReqSize;
454    /** Maximum FIFO size in bytes */
455    const size_t fifoSize;
456    /** Request flags */
457    const Request::Flags reqFlags;
458
459    DmaPort &port;
460
461  private:
462    class DmaDoneEvent : public Event
463    {
464      public:
465        DmaDoneEvent(DmaReadFifo *_parent, size_t max_size);
466
467        void kill();
468        void cancel();
469        bool canceled() const { return _canceled; }
470        void reset(size_t size);
471        void process();
472
473        bool done() const { return _done; }
474        size_t requestSize() const { return _requestSize; }
475        const uint8_t *data() const { return _data.data(); }
476        uint8_t *data() { return _data.data(); }
477
478      private:
479        DmaReadFifo *parent;
480        bool _done;
481        bool _canceled;
482        size_t _requestSize;
483        std::vector<uint8_t> _data;
484    };
485
486    typedef std::unique_ptr<DmaDoneEvent> DmaDoneEventUPtr;
487
488    /**
489     * DMA request done, handle incoming data and issue new
490     * request.
491     */
492    void dmaDone();
493
494    /** Handle pending requests that have been flagged as done. */
495    void handlePending();
496
497    /** Try to issue new DMA requests or bypass DMA requests*/
498    void resumeFill();
499
500    /** Try to issue new DMA requests during normal execution*/
501    void resumeFillTiming();
502
503    /** Try to bypass DMA requests in KVM execution mode */
504    void resumeFillFunctional();
505
506  private: // Internal state
507    Fifo<uint8_t> buffer;
508
509    Addr nextAddr;
510    Addr endAddr;
511
512    std::deque<DmaDoneEventUPtr> pendingRequests;
513    std::deque<DmaDoneEventUPtr> freeRequests;
514};
515
516#endif // __DEV_DMA_DEVICE_HH__
517