112966SMatteo.Andreozzi@arm.com/*
212966SMatteo.Andreozzi@arm.com * Copyright (c) 2018 ARM Limited
312966SMatteo.Andreozzi@arm.com * All rights reserved
412966SMatteo.Andreozzi@arm.com *
512966SMatteo.Andreozzi@arm.com * The license below extends only to copyright in the software and shall
612966SMatteo.Andreozzi@arm.com * not be construed as granting a license to any other intellectual
712966SMatteo.Andreozzi@arm.com * property including but not limited to intellectual property relating
812966SMatteo.Andreozzi@arm.com * to a hardware implementation of the functionality of the software
912966SMatteo.Andreozzi@arm.com * licensed hereunder.  You may use the software subject to the license
1012966SMatteo.Andreozzi@arm.com * terms below provided that you ensure that this notice is replicated
1112966SMatteo.Andreozzi@arm.com * unmodified and in its entirety in all distributions of the software,
1212966SMatteo.Andreozzi@arm.com * modified or unmodified, in source code or in binary form.
1312966SMatteo.Andreozzi@arm.com *
1412966SMatteo.Andreozzi@arm.com * Redistribution and use in source and binary forms, with or without
1512966SMatteo.Andreozzi@arm.com * modification, are permitted provided that the following conditions are
1612966SMatteo.Andreozzi@arm.com * met: redistributions of source code must retain the above copyright
1712966SMatteo.Andreozzi@arm.com * notice, this list of conditions and the following disclaimer;
1812966SMatteo.Andreozzi@arm.com * redistributions in binary form must reproduce the above copyright
1912966SMatteo.Andreozzi@arm.com * notice, this list of conditions and the following disclaimer in the
2012966SMatteo.Andreozzi@arm.com * documentation and/or other materials provided with the distribution;
2112966SMatteo.Andreozzi@arm.com * neither the name of the copyright holders nor the names of its
2212966SMatteo.Andreozzi@arm.com * contributors may be used to endorse or promote products derived from
2312966SMatteo.Andreozzi@arm.com * this software without specific prior written permission.
2412966SMatteo.Andreozzi@arm.com *
2512966SMatteo.Andreozzi@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2612966SMatteo.Andreozzi@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2712966SMatteo.Andreozzi@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2812966SMatteo.Andreozzi@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2912966SMatteo.Andreozzi@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3012966SMatteo.Andreozzi@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3112966SMatteo.Andreozzi@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3212966SMatteo.Andreozzi@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3312966SMatteo.Andreozzi@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3412966SMatteo.Andreozzi@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3512966SMatteo.Andreozzi@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3612966SMatteo.Andreozzi@arm.com *
3712966SMatteo.Andreozzi@arm.com * Author: Matteo Andreozzi
3812966SMatteo.Andreozzi@arm.com */
3912966SMatteo.Andreozzi@arm.com
4012966SMatteo.Andreozzi@arm.com#ifndef __MEM_QOS_Q_POLICY_HH__
4112966SMatteo.Andreozzi@arm.com#define __MEM_QOS_Q_POLICY_HH__
4212966SMatteo.Andreozzi@arm.com
4312966SMatteo.Andreozzi@arm.com#include <deque>
4412966SMatteo.Andreozzi@arm.com#include <unordered_set>
4512966SMatteo.Andreozzi@arm.com
4612966SMatteo.Andreozzi@arm.com#include "mem/packet.hh"
4712966SMatteo.Andreozzi@arm.com#include "params/QoSMemCtrl.hh"
4812966SMatteo.Andreozzi@arm.com
4912966SMatteo.Andreozzi@arm.comnamespace QoS {
5012966SMatteo.Andreozzi@arm.com
5112966SMatteo.Andreozzi@arm.comclass MemCtrl;
5212966SMatteo.Andreozzi@arm.com
5312966SMatteo.Andreozzi@arm.com/**
5412966SMatteo.Andreozzi@arm.com * QoS Queue Policy
5512966SMatteo.Andreozzi@arm.com *
5612966SMatteo.Andreozzi@arm.com * The QoS Queue Policy class implements algorithms to schedule packets
5712966SMatteo.Andreozzi@arm.com * within the same QoS priority queue
5812966SMatteo.Andreozzi@arm.com */
5912966SMatteo.Andreozzi@arm.comclass QueuePolicy
6012966SMatteo.Andreozzi@arm.com{
6112966SMatteo.Andreozzi@arm.com  public:
6212966SMatteo.Andreozzi@arm.com    typedef std::deque<PacketPtr> PacketQueue;
6312966SMatteo.Andreozzi@arm.com
6412966SMatteo.Andreozzi@arm.com    /**
6512966SMatteo.Andreozzi@arm.com     * This factory method is used for generating the queue policy. It takes
6612966SMatteo.Andreozzi@arm.com     * the memory controller params as an argument and returns the related
6712966SMatteo.Andreozzi@arm.com     * QueuePolicy object.  If no particular QueuePolicy has been specified in
6812966SMatteo.Andreozzi@arm.com     * the QoSMemCtrlParams, the method will default to a LIFO queue policy.
6912966SMatteo.Andreozzi@arm.com     *
7012966SMatteo.Andreozzi@arm.com     * @param p QoS::MemCtrl parameter variable
7112966SMatteo.Andreozzi@arm.com     * @return Pointer to the QueuePolicy
7212966SMatteo.Andreozzi@arm.com     */
7312966SMatteo.Andreozzi@arm.com    static QueuePolicy* create(const QoSMemCtrlParams* p);
7412966SMatteo.Andreozzi@arm.com
7512966SMatteo.Andreozzi@arm.com    /**
7612966SMatteo.Andreozzi@arm.com     * This method is called by the memory controller after it enqueues a
7712966SMatteo.Andreozzi@arm.com     * packet. It is a way for the queue policy to hook into the packet
7812966SMatteo.Andreozzi@arm.com     * enqueuing and update relevant metadata. This will then be used once
7912966SMatteo.Andreozzi@arm.com     * the QueuePolicy::selectPacket will be called.
8012966SMatteo.Andreozzi@arm.com     *
8112966SMatteo.Andreozzi@arm.com     * @param pkt Enqueued packet
8212966SMatteo.Andreozzi@arm.com     */
8312966SMatteo.Andreozzi@arm.com    virtual void enqueuePacket(PacketPtr pkt) {};
8412966SMatteo.Andreozzi@arm.com
8512966SMatteo.Andreozzi@arm.com    /**
8612966SMatteo.Andreozzi@arm.com     * Policy selector.
8712966SMatteo.Andreozzi@arm.com     * The implementation of this virtual method selects the packet
8812966SMatteo.Andreozzi@arm.com     * to be serviced from the packet queue passed as an argument.
8912966SMatteo.Andreozzi@arm.com     *
9012966SMatteo.Andreozzi@arm.com     * @param queue Packet queue
9112966SMatteo.Andreozzi@arm.com     * @return Iterator pointing to the packet in the queue to be
9212966SMatteo.Andreozzi@arm.com     *         serviced
9312966SMatteo.Andreozzi@arm.com     */
9412966SMatteo.Andreozzi@arm.com    virtual PacketQueue::iterator selectPacket(PacketQueue* queue) = 0;
9512966SMatteo.Andreozzi@arm.com
9612966SMatteo.Andreozzi@arm.com    /**
9712966SMatteo.Andreozzi@arm.com     * Setting a pointer to the Memory Controller implementing
9812966SMatteo.Andreozzi@arm.com     * the policy.
9912966SMatteo.Andreozzi@arm.com     */
10012966SMatteo.Andreozzi@arm.com    void setMemCtrl(MemCtrl* mem) { memCtrl = mem; };
10112966SMatteo.Andreozzi@arm.com
10213475Snikos.nikoleris@arm.com    virtual ~QueuePolicy() {};
10313475Snikos.nikoleris@arm.com
10412966SMatteo.Andreozzi@arm.com  protected:
10512966SMatteo.Andreozzi@arm.com    QueuePolicy(const QoSMemCtrlParams* p)
10612966SMatteo.Andreozzi@arm.com      : memCtrl(nullptr)
10712966SMatteo.Andreozzi@arm.com    {}
10812966SMatteo.Andreozzi@arm.com
10912966SMatteo.Andreozzi@arm.com    /** Pointer to parent memory controller implementing the policy */
11012966SMatteo.Andreozzi@arm.com    MemCtrl* memCtrl;
11112966SMatteo.Andreozzi@arm.com};
11212966SMatteo.Andreozzi@arm.com
11312966SMatteo.Andreozzi@arm.com/** Last In First Out Queue Policy */
11412966SMatteo.Andreozzi@arm.comclass LifoQueuePolicy : public QueuePolicy
11512966SMatteo.Andreozzi@arm.com{
11612966SMatteo.Andreozzi@arm.com  public:
11712966SMatteo.Andreozzi@arm.com    LifoQueuePolicy(const QoSMemCtrlParams* p)
11812966SMatteo.Andreozzi@arm.com      : QueuePolicy(p)
11912966SMatteo.Andreozzi@arm.com    {}
12012966SMatteo.Andreozzi@arm.com
12112966SMatteo.Andreozzi@arm.com    /**
12212966SMatteo.Andreozzi@arm.com     * Implements LIFO packet select policy
12312966SMatteo.Andreozzi@arm.com     *
12412966SMatteo.Andreozzi@arm.com     * @param queue The queue in which to select a packet
12512966SMatteo.Andreozzi@arm.com     * @return Iterator to the selected packet
12612966SMatteo.Andreozzi@arm.com     */
12712966SMatteo.Andreozzi@arm.com    PacketQueue::iterator
12812966SMatteo.Andreozzi@arm.com    selectPacket(PacketQueue* queue) override
12912966SMatteo.Andreozzi@arm.com    {
13012966SMatteo.Andreozzi@arm.com        return queue->end();
13112966SMatteo.Andreozzi@arm.com    }
13212966SMatteo.Andreozzi@arm.com};
13312966SMatteo.Andreozzi@arm.com
13412966SMatteo.Andreozzi@arm.com/** First In First Out Queue Policy */
13512966SMatteo.Andreozzi@arm.comclass FifoQueuePolicy : public QueuePolicy
13612966SMatteo.Andreozzi@arm.com{
13712966SMatteo.Andreozzi@arm.com  public:
13812966SMatteo.Andreozzi@arm.com    FifoQueuePolicy(const QoSMemCtrlParams* p)
13912966SMatteo.Andreozzi@arm.com      : QueuePolicy(p)
14012966SMatteo.Andreozzi@arm.com    {}
14112966SMatteo.Andreozzi@arm.com
14212966SMatteo.Andreozzi@arm.com    /**
14312966SMatteo.Andreozzi@arm.com     * Implements FCFS packet select policy
14412966SMatteo.Andreozzi@arm.com     *
14512966SMatteo.Andreozzi@arm.com     * @param queue The queue in which to select a packet
14612966SMatteo.Andreozzi@arm.com     * @return Iterator to the selected packet
14712966SMatteo.Andreozzi@arm.com     */
14812966SMatteo.Andreozzi@arm.com    PacketQueue::iterator
14912966SMatteo.Andreozzi@arm.com    selectPacket(PacketQueue* queue) override
15012966SMatteo.Andreozzi@arm.com    {
15112966SMatteo.Andreozzi@arm.com        return queue->begin();
15212966SMatteo.Andreozzi@arm.com    }
15312966SMatteo.Andreozzi@arm.com};
15412966SMatteo.Andreozzi@arm.com
15512966SMatteo.Andreozzi@arm.com/**
15612966SMatteo.Andreozzi@arm.com * Least Recently Granted Queue Policy
15712966SMatteo.Andreozzi@arm.com * It selects packets from the queue with a round
15812966SMatteo.Andreozzi@arm.com * robin-like policy: using the master id as a switching
15912966SMatteo.Andreozzi@arm.com * parameter rather than switching over a time quantum.
16012966SMatteo.Andreozzi@arm.com */
16112966SMatteo.Andreozzi@arm.comclass LrgQueuePolicy : public QueuePolicy
16212966SMatteo.Andreozzi@arm.com{
16312966SMatteo.Andreozzi@arm.com  public:
16412966SMatteo.Andreozzi@arm.com    LrgQueuePolicy(const QoSMemCtrlParams* p)
16512966SMatteo.Andreozzi@arm.com      : QueuePolicy(p)
16612966SMatteo.Andreozzi@arm.com    {}
16712966SMatteo.Andreozzi@arm.com
16812966SMatteo.Andreozzi@arm.com    void enqueuePacket(PacketPtr pkt) override;
16912966SMatteo.Andreozzi@arm.com
17012966SMatteo.Andreozzi@arm.com    /**
17112966SMatteo.Andreozzi@arm.com     * Implements LRG packet select policy
17212966SMatteo.Andreozzi@arm.com     *
17312966SMatteo.Andreozzi@arm.com     * @param queue The queue in which to select a packet
17412966SMatteo.Andreozzi@arm.com     * @return Iterator to the selected packet
17512966SMatteo.Andreozzi@arm.com     */
17612966SMatteo.Andreozzi@arm.com    PacketQueue::iterator
17712966SMatteo.Andreozzi@arm.com    selectPacket(PacketQueue* queue) override;
17812966SMatteo.Andreozzi@arm.com
17912966SMatteo.Andreozzi@arm.com  protected:
18012966SMatteo.Andreozzi@arm.com    /**
18112966SMatteo.Andreozzi@arm.com     * Support structure for lrg algorithms:
18212966SMatteo.Andreozzi@arm.com     * keeps track of serviced masters,
18312966SMatteo.Andreozzi@arm.com     * always serve the front element.
18412966SMatteo.Andreozzi@arm.com     */
18512966SMatteo.Andreozzi@arm.com    std::list<MasterID> toServe;
18612966SMatteo.Andreozzi@arm.com};
18712966SMatteo.Andreozzi@arm.com
18812966SMatteo.Andreozzi@arm.com} // namespace QoS
18912966SMatteo.Andreozzi@arm.com
19012966SMatteo.Andreozzi@arm.com#endif /* __MEM_QOS_Q_POLICY_HH__ */
191