mem: Add packet matching functions

Add both block and non-block-aligned packet matching functions,
so that both address and secure bits are checked when checking
whether a packet matches a request.

Change-Id: Id0069befb925d112e06f250741cb47d9dfa249cc
Signed-off-by: Daniel R. Carvalho <odanrc@yahoo.com.br>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/17533
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
Maintainer: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Nikos Nikoleris <nikos.nikoleris@arm.com>
diff --git a/src/mem/cache/base.cc b/src/mem/cache/base.cc
index f43c2ec..ddcfd80 100644
--- a/src/mem/cache/base.cc
+++ b/src/mem/cache/base.cc
@@ -2451,7 +2451,7 @@
     } else {
         // let our snoop responses go first if there are responses to
         // the same addresses
-        if (checkConflictingSnoop(entry->blkAddr)) {
+        if (checkConflictingSnoop(entry->getTarget()->pkt)) {
             return;
         }
         waitingOnRetry = entry->sendPacket(cache);
diff --git a/src/mem/cache/base.hh b/src/mem/cache/base.hh
index a45dcba..8d5ed11 100644
--- a/src/mem/cache/base.hh
+++ b/src/mem/cache/base.hh
@@ -189,10 +189,12 @@
          * send out, and if so simply stall any requests, and schedule
          * a send event at the same time as the next snoop response is
          * being sent out.
+         *
+         * @param pkt The packet to check for conflicts against.
          */
-        bool checkConflictingSnoop(Addr addr)
+        bool checkConflictingSnoop(const PacketPtr pkt)
         {
-            if (snoopRespQueue.hasAddr(addr)) {
+            if (snoopRespQueue.checkConflict(pkt, cache.blkSize)) {
                 DPRINTF(CachePort, "Waiting for snoop response to be "
                         "sent\n");
                 Tick when = snoopRespQueue.deferredPacketReadyTime();
diff --git a/src/mem/cache/cache.cc b/src/mem/cache/cache.cc
index eac278a..4643e1d 100644
--- a/src/mem/cache/cache.cc
+++ b/src/mem/cache/cache.cc
@@ -383,7 +383,7 @@
                                                        pkt->req->masterId());
             pf = new Packet(req, pkt->cmd);
             pf->allocate();
-            assert(pf->getAddr() == pkt->getAddr());
+            assert(pf->matchAddr(pkt));
             assert(pf->getSize() == pkt->getSize());
         }
 
@@ -781,7 +781,7 @@
                     pkt->payloadDelay;
                 if (pkt->isRead() && !is_error) {
                     // sanity check
-                    assert(pkt->getAddr() == tgt_pkt->getAddr());
+                    assert(pkt->matchAddr(tgt_pkt));
                     assert(pkt->getSize() >= tgt_pkt->getSize());
 
                     tgt_pkt->setData(pkt->getConstPtr<uint8_t>());
diff --git a/src/mem/packet.cc b/src/mem/packet.cc
index 7172219..1d1b5b8 100644
--- a/src/mem/packet.cc
+++ b/src/mem/packet.cc
@@ -396,6 +396,32 @@
     return str.str();
 }
 
+bool
+Packet::matchBlockAddr(const Addr addr, const bool is_secure,
+                       const int blk_size) const
+{
+    return (getBlockAddr(blk_size) == addr) && (isSecure() == is_secure);
+}
+
+bool
+Packet::matchBlockAddr(const PacketPtr pkt, const int blk_size) const
+{
+    return matchBlockAddr(pkt->getBlockAddr(blk_size), pkt->isSecure(),
+                          blk_size);
+}
+
+bool
+Packet::matchAddr(const Addr addr, const bool is_secure) const
+{
+    return (getAddr() == addr) && (isSecure() == is_secure);
+}
+
+bool
+Packet::matchAddr(const PacketPtr pkt) const
+{
+    return matchAddr(pkt->getAddr(), pkt->isSecure());
+}
+
 Packet::PrintReqState::PrintReqState(std::ostream &_os, int _verbosity)
     : curPrefixPtr(new std::string("")), os(_os), verbosity(_verbosity)
 {
diff --git a/src/mem/packet.hh b/src/mem/packet.hh
index 4e3ea15..f942e8d 100644
--- a/src/mem/packet.hh
+++ b/src/mem/packet.hh
@@ -980,6 +980,45 @@
         flags.set(VALID_SIZE);
     }
 
+    /**
+     * Check if packet corresponds to a given block-aligned address and
+     * address space.
+     *
+     * @param addr The address to compare against.
+     * @param is_secure Whether addr belongs to the secure address space.
+     * @param blk_size Block size in bytes.
+     * @return Whether packet matches description.
+     */
+    bool matchBlockAddr(const Addr addr, const bool is_secure,
+                        const int blk_size) const;
+
+    /**
+     * Check if this packet refers to the same block-aligned address and
+     * address space as another packet.
+     *
+     * @param pkt The packet to compare against.
+     * @param blk_size Block size in bytes.
+     * @return Whether packet matches description.
+     */
+    bool matchBlockAddr(const PacketPtr pkt, const int blk_size) const;
+
+    /**
+     * Check if packet corresponds to a given address and address space.
+     *
+     * @param addr The address to compare against.
+     * @param is_secure Whether addr belongs to the secure address space.
+     * @return Whether packet matches description.
+     */
+    bool matchAddr(const Addr addr, const bool is_secure) const;
+
+    /**
+     * Check if this packet refers to the same address and address space as
+     * another packet.
+     *
+     * @param pkt The packet to compare against.
+     * @return Whether packet matches description.
+     */
+    bool matchAddr(const PacketPtr pkt) const;
 
   public:
     /**
diff --git a/src/mem/packet_queue.cc b/src/mem/packet_queue.cc
index 282625a..dd1ba3d 100644
--- a/src/mem/packet_queue.cc
+++ b/src/mem/packet_queue.cc
@@ -72,12 +72,12 @@
 }
 
 bool
-PacketQueue::hasAddr(Addr addr) const
+PacketQueue::checkConflict(const PacketPtr pkt, const int blk_size) const
 {
     // caller is responsible for ensuring that all packets have the
     // same alignment
     for (const auto& p : transmitList) {
-        if (p.pkt->getAddr() == addr)
+        if (p.pkt->matchBlockAddr(pkt, blk_size))
             return true;
     }
     return false;
@@ -138,8 +138,7 @@
     auto it = transmitList.end();
     while (it != transmitList.begin()) {
         --it;
-        if ((forceOrder && it->pkt->getAddr() == pkt->getAddr()) ||
-            it->tick <= when) {
+        if ((forceOrder && it->pkt->matchAddr(pkt)) || it->tick <= when) {
             // emplace inserts the element before the position pointed to by
             // the iterator, so advance it one step
             transmitList.emplace(++it, when, pkt);
diff --git a/src/mem/packet_queue.hh b/src/mem/packet_queue.hh
index 4ac4bf3..b9c5b75 100644
--- a/src/mem/packet_queue.hh
+++ b/src/mem/packet_queue.hh
@@ -172,9 +172,14 @@
     { return transmitList.empty() ? MaxTick : transmitList.front().tick; }
 
     /**
-     * Check if a packets address exists in the queue.
+     * Check if a packet corresponding to the same address exists in the
+     * queue.
+     *
+     * @param pkt The packet to compare against.
+     * @param blk_size Block size in bytes.
+     * @return Whether a corresponding packet is found.
      */
-    bool hasAddr(Addr addr) const;
+    bool checkConflict(const PacketPtr pkt, const int blk_size) const;
 
     /** Check the list of buffered packets against the supplied
      * functional request. */
diff --git a/src/mem/simple_mem.cc b/src/mem/simple_mem.cc
index 297aa61..fcc1cff 100644
--- a/src/mem/simple_mem.cc
+++ b/src/mem/simple_mem.cc
@@ -174,7 +174,7 @@
         auto i = packetQueue.end();
         --i;
         while (i != packetQueue.begin() && when_to_send < i->tick &&
-               i->pkt->getAddr() != pkt->getAddr())
+               !i->pkt->matchAddr(pkt))
             --i;
 
         // emplace inserts the element before the position pointed to by