packet_queue.cc revision 8948
12914SN/A/*
28856SN/A * Copyright (c) 2012 ARM Limited
38856SN/A * All rights reserved.
48856SN/A *
58856SN/A * The license below extends only to copyright in the software and shall
68856SN/A * not be construed as granting a license to any other intellectual
78856SN/A * property including but not limited to intellectual property relating
88856SN/A * to a hardware implementation of the functionality of the software
98856SN/A * licensed hereunder.  You may use the software subject to the license
108856SN/A * terms below provided that you ensure that this notice is replicated
118856SN/A * unmodified and in its entirety in all distributions of the software,
128856SN/A * modified or unmodified, in source code or in binary form.
138856SN/A *
142914SN/A * Copyright (c) 2006 The Regents of The University of Michigan
152914SN/A * All rights reserved.
162914SN/A *
172914SN/A * Redistribution and use in source and binary forms, with or without
182914SN/A * modification, are permitted provided that the following conditions are
192914SN/A * met: redistributions of source code must retain the above copyright
202914SN/A * notice, this list of conditions and the following disclaimer;
212914SN/A * redistributions in binary form must reproduce the above copyright
222914SN/A * notice, this list of conditions and the following disclaimer in the
232914SN/A * documentation and/or other materials provided with the distribution;
242914SN/A * neither the name of the copyright holders nor the names of its
252914SN/A * contributors may be used to endorse or promote products derived from
262914SN/A * this software without specific prior written permission.
272914SN/A *
282914SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292914SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302914SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312914SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322914SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332914SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342914SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352914SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362914SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372914SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382914SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392914SN/A *
402914SN/A * Authors: Ali Saidi
418856SN/A *          Andreas Hansson
422914SN/A */
432914SN/A
448914Sandreas.hansson@arm.com#include "debug/PacketQueue.hh"
458914Sandreas.hansson@arm.com#include "mem/packet_queue.hh"
462914SN/A
475740SN/Ausing namespace std;
485740SN/A
498914Sandreas.hansson@arm.comPacketQueue::PacketQueue(EventManager& _em, Port& _port,
508914Sandreas.hansson@arm.com                         const std::string _label)
518914Sandreas.hansson@arm.com    : em(_em), label(_label), sendEvent(this), drainEvent(NULL), port(_port),
525740SN/A      waitingOnRetry(false)
535740SN/A{
545740SN/A}
555740SN/A
568914Sandreas.hansson@arm.comPacketQueue::~PacketQueue()
575740SN/A{
585740SN/A}
595740SN/A
608914Sandreas.hansson@arm.comvoid
618914Sandreas.hansson@arm.comPacketQueue::retry()
628914Sandreas.hansson@arm.com{
638914Sandreas.hansson@arm.com    DPRINTF(PacketQueue, "Queue %s received retry\n", name());
648914Sandreas.hansson@arm.com    assert(waitingOnRetry);
658914Sandreas.hansson@arm.com    sendDeferredPacket();
668914Sandreas.hansson@arm.com}
678914Sandreas.hansson@arm.com
684929SN/Abool
698914Sandreas.hansson@arm.comPacketQueue::checkFunctional(PacketPtr pkt)
703091SN/A{
718856SN/A    pkt->pushLabel(label);
728856SN/A
734490SN/A    DeferredPacketIterator i = transmitList.begin();
744490SN/A    DeferredPacketIterator end = transmitList.end();
758856SN/A    bool found = false;
763296SN/A
778856SN/A    while (!found && i != end) {
788856SN/A        // If the buffered packet contains data, and it overlaps the
798856SN/A        // current packet, then update data
808856SN/A        found = pkt->checkFunctional(i->pkt);
818856SN/A        ++i;
823284SN/A    }
834929SN/A
848856SN/A    pkt->popLabel();
858856SN/A
868856SN/A    return found;
874490SN/A}
883342SN/A
894490SN/Avoid
908914Sandreas.hansson@arm.comPacketQueue::schedSendEvent(Tick when)
918708SN/A{
928856SN/A    // if we are waiting on a retry, do not schedule a send event, and
938856SN/A    // instead rely on retry being called
948708SN/A    if (waitingOnRetry) {
958856SN/A        assert(!sendEvent.scheduled());
968708SN/A        return;
978708SN/A    }
988708SN/A
998856SN/A    if (!sendEvent.scheduled()) {
1008914Sandreas.hansson@arm.com        em.schedule(&sendEvent, when);
1018856SN/A    } else if (sendEvent.when() > when) {
1028914Sandreas.hansson@arm.com        em.reschedule(&sendEvent, when);
1038708SN/A    }
1048708SN/A}
1052914SN/A
1062914SN/Avoid
1078948Sandreas.hansson@arm.comPacketQueue::schedSendTiming(PacketPtr pkt, Tick when, bool send_as_snoop)
1083403SN/A{
1097823SN/A    assert(when > curTick());
1104492SN/A
1118914Sandreas.hansson@arm.com    // nothing on the list, or earlier than current front element,
1128914Sandreas.hansson@arm.com    // schedule an event
1134666SN/A    if (transmitList.empty() || when < transmitList.front().tick) {
1148914Sandreas.hansson@arm.com        // note that currently we ignore a potentially outstanding retry
1158914Sandreas.hansson@arm.com        // and could in theory put a new packet at the head of the
1168914Sandreas.hansson@arm.com        // transmit list before retrying the existing packet
1178948Sandreas.hansson@arm.com        transmitList.push_front(DeferredPacket(when, pkt, send_as_snoop));
1184666SN/A        schedSendEvent(when);
1194666SN/A        return;
1204666SN/A    }
1214666SN/A
1228914Sandreas.hansson@arm.com    // list is non-empty and this belongs at the end
1234666SN/A    if (when >= transmitList.back().tick) {
1248948Sandreas.hansson@arm.com        transmitList.push_back(DeferredPacket(when, pkt, send_as_snoop));
1253450SN/A        return;
1263403SN/A    }
1273450SN/A
1288914Sandreas.hansson@arm.com    // this belongs in the middle somewhere, insertion sort
1294490SN/A    DeferredPacketIterator i = transmitList.begin();
1308914Sandreas.hansson@arm.com    ++i; // already checked for insertion at front
1318914Sandreas.hansson@arm.com    while (i != transmitList.end() && when >= i->tick)
1328914Sandreas.hansson@arm.com        ++i;
1338948Sandreas.hansson@arm.com    transmitList.insert(i, DeferredPacket(when, pkt, send_as_snoop));
1343403SN/A}
1353403SN/A
1368914Sandreas.hansson@arm.comvoid PacketQueue::trySendTiming()
1378856SN/A{
1388856SN/A    assert(deferredPacketReady());
1398914Sandreas.hansson@arm.com
1408856SN/A    // take the next packet off the list here, as we might return to
1418856SN/A    // ourselves through the sendTiming call below
1428856SN/A    DeferredPacket dp = transmitList.front();
1438856SN/A    transmitList.pop_front();
1448856SN/A
1458856SN/A    // attempt to send the packet and remember the outcome
1468948Sandreas.hansson@arm.com    if (!dp.sendAsSnoop)
1478948Sandreas.hansson@arm.com        waitingOnRetry = !port.sendTiming(dp.pkt);
1488948Sandreas.hansson@arm.com    else
1498948Sandreas.hansson@arm.com        waitingOnRetry = !port.sendTimingSnoop(dp.pkt);
1508856SN/A
1518856SN/A    if (waitingOnRetry) {
1528856SN/A        // put the packet back at the front of the list (packet should
1538856SN/A        // not have changed since it wasn't accepted)
1548856SN/A        assert(!sendEvent.scheduled());
1558856SN/A        transmitList.push_front(dp);
1568856SN/A    }
1578856SN/A}
1588856SN/A
1598856SN/Avoid
1608914Sandreas.hansson@arm.comPacketQueue::scheduleSend(Tick time)
1618856SN/A{
1628856SN/A    // the next ready time is either determined by the next deferred packet,
1638856SN/A    // or in the cache through the MSHR ready time
1648856SN/A    Tick nextReady = std::min(deferredPacketReadyTime(), time);
1658914Sandreas.hansson@arm.com
1668856SN/A    if (nextReady != MaxTick) {
1678856SN/A        // if the sendTiming caused someone else to call our
1688856SN/A        // recvTiming we could already have an event scheduled, check
1698856SN/A        if (!sendEvent.scheduled())
1708914Sandreas.hansson@arm.com            em.schedule(&sendEvent, std::max(nextReady, curTick() + 1));
1718856SN/A    } else {
1728856SN/A        // no more to send, so if we're draining, we may be done
1738856SN/A        if (drainEvent && !sendEvent.scheduled()) {
1748856SN/A            drainEvent->process();
1758856SN/A            drainEvent = NULL;
1768856SN/A        }
1778856SN/A    }
1788856SN/A}
1794492SN/A
1803403SN/Avoid
1818914Sandreas.hansson@arm.comPacketQueue::sendDeferredPacket()
1822914SN/A{
1838914Sandreas.hansson@arm.com    // try to send what is on the list, this will set waitingOnRetry
1848914Sandreas.hansson@arm.com    // accordingly
1858856SN/A    trySendTiming();
1864492SN/A
1878856SN/A    // if we succeeded and are not waiting for a retry, schedule the
1888856SN/A    // next send
1898856SN/A    if (!waitingOnRetry) {
1908856SN/A        scheduleSend();
1914492SN/A    }
1924492SN/A}
1934492SN/A
1944492SN/Avoid
1958914Sandreas.hansson@arm.comPacketQueue::processSendEvent()
1964492SN/A{
1974492SN/A    assert(!waitingOnRetry);
1984492SN/A    sendDeferredPacket();
1992914SN/A}
2002914SN/A
2012914SN/Aunsigned int
2028914Sandreas.hansson@arm.comPacketQueue::drain(Event *de)
2032914SN/A{
2048856SN/A    if (transmitList.empty() && !sendEvent.scheduled())
2052914SN/A        return 0;
2062914SN/A    drainEvent = de;
2072914SN/A    return 1;
2082914SN/A}
209