114039Sstacze01@arm.com/*
214039Sstacze01@arm.com * Copyright (c) 2014, 2018-2019 ARM Limited
314039Sstacze01@arm.com * All rights reserved
414039Sstacze01@arm.com *
514039Sstacze01@arm.com * The license below extends only to copyright in the software and shall
614039Sstacze01@arm.com * not be construed as granting a license to any other intellectual
714039Sstacze01@arm.com * property including but not limited to intellectual property relating
814039Sstacze01@arm.com * to a hardware implementation of the functionality of the software
914039Sstacze01@arm.com * licensed hereunder.  You may use the software subject to the license
1014039Sstacze01@arm.com * terms below provided that you ensure that this notice is replicated
1114039Sstacze01@arm.com * unmodified and in its entirety in all distributions of the software,
1214039Sstacze01@arm.com * modified or unmodified, in source code or in binary form.
1314039Sstacze01@arm.com *
1414039Sstacze01@arm.com * Redistribution and use in source and binary forms, with or without
1514039Sstacze01@arm.com * modification, are permitted provided that the following conditions are
1614039Sstacze01@arm.com * met: redistributions of source code must retain the above copyright
1714039Sstacze01@arm.com * notice, this list of conditions and the following disclaimer;
1814039Sstacze01@arm.com * redistributions in binary form must reproduce the above copyright
1914039Sstacze01@arm.com * notice, this list of conditions and the following disclaimer in the
2014039Sstacze01@arm.com * documentation and/or other materials provided with the distribution;
2114039Sstacze01@arm.com * neither the name of the copyright holders nor the names of its
2214039Sstacze01@arm.com * contributors may be used to endorse or promote products derived from
2314039Sstacze01@arm.com * this software without specific prior written permission.
2414039Sstacze01@arm.com *
2514039Sstacze01@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2614039Sstacze01@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2714039Sstacze01@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2814039Sstacze01@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2914039Sstacze01@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3014039Sstacze01@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3114039Sstacze01@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3214039Sstacze01@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3314039Sstacze01@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3414039Sstacze01@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3514039Sstacze01@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3614039Sstacze01@arm.com *
3714039Sstacze01@arm.com * Authors: Stan Czerniawski
3814039Sstacze01@arm.com */
3914039Sstacze01@arm.com
4014039Sstacze01@arm.com#ifndef __DEV_ARM_SMMU_V3_CACHES_HH__
4114039Sstacze01@arm.com#define __DEV_ARM_SMMU_V3_CACHES_HH__
4214039Sstacze01@arm.com
4314039Sstacze01@arm.com#include <stdint.h>
4414039Sstacze01@arm.com
4514039Sstacze01@arm.com#include <array>
4614039Sstacze01@arm.com#include <cstddef>
4714039Sstacze01@arm.com#include <string>
4814039Sstacze01@arm.com#include <vector>
4914039Sstacze01@arm.com
5014039Sstacze01@arm.com#include "base/random.hh"
5114039Sstacze01@arm.com#include "base/statistics.hh"
5214039Sstacze01@arm.com#include "base/types.hh"
5314039Sstacze01@arm.com
5414039Sstacze01@arm.com#define WALK_CACHE_LEVELS 4
5514039Sstacze01@arm.com
5614039Sstacze01@arm.comenum {
5714039Sstacze01@arm.com    SMMU_CACHE_REPL_ROUND_ROBIN,
5814039Sstacze01@arm.com    SMMU_CACHE_REPL_RANDOM,
5914039Sstacze01@arm.com    SMMU_CACHE_REPL_LRU,
6014039Sstacze01@arm.com};
6114039Sstacze01@arm.com
6214039Sstacze01@arm.comclass SMMUv3BaseCache
6314039Sstacze01@arm.com{
6414039Sstacze01@arm.com  protected:
6514039Sstacze01@arm.com    int replacementPolicy;
6614039Sstacze01@arm.com    size_t nextToReplace;
6714039Sstacze01@arm.com    Random random;
6814039Sstacze01@arm.com    uint32_t useStamp;
6914039Sstacze01@arm.com
7014039Sstacze01@arm.com    Stats::Formula averageLookups;
7114039Sstacze01@arm.com    Stats::Scalar totalLookups;
7214039Sstacze01@arm.com
7314039Sstacze01@arm.com    Stats::Formula averageMisses;
7414039Sstacze01@arm.com    Stats::Scalar totalMisses;
7514039Sstacze01@arm.com
7614039Sstacze01@arm.com    Stats::Formula averageUpdates;
7714039Sstacze01@arm.com    Stats::Scalar totalUpdates;
7814039Sstacze01@arm.com
7914039Sstacze01@arm.com    Stats::Formula averageHitRate;
8014039Sstacze01@arm.com
8114039Sstacze01@arm.com    Stats::Scalar insertions;
8214039Sstacze01@arm.com
8314039Sstacze01@arm.com    static int decodePolicyName(const std::string &policy_name);
8414039Sstacze01@arm.com
8514039Sstacze01@arm.com  public:
8614039Sstacze01@arm.com    SMMUv3BaseCache(const std::string &policy_name, uint32_t seed);
8714039Sstacze01@arm.com    virtual ~SMMUv3BaseCache() {}
8814039Sstacze01@arm.com
8914039Sstacze01@arm.com    virtual void regStats(const std::string &name);
9014039Sstacze01@arm.com};
9114039Sstacze01@arm.com
9214039Sstacze01@arm.comclass SMMUTLB : public SMMUv3BaseCache
9314039Sstacze01@arm.com{
9414039Sstacze01@arm.com  public:
9514039Sstacze01@arm.com    enum AllocPolicy {
9614039Sstacze01@arm.com        ALLOC_ANY_WAY,
9714039Sstacze01@arm.com        ALLOC_ANY_BUT_LAST_WAY,
9814039Sstacze01@arm.com        ALLOC_LAST_WAY,
9914039Sstacze01@arm.com    };
10014039Sstacze01@arm.com
10114039Sstacze01@arm.com    struct Entry
10214039Sstacze01@arm.com    {
10314039Sstacze01@arm.com        bool valid;
10414039Sstacze01@arm.com        bool prefetched;
10514039Sstacze01@arm.com        mutable uint32_t lastUsed;
10614039Sstacze01@arm.com
10714039Sstacze01@arm.com        // TAGS
10814039Sstacze01@arm.com        uint32_t sid;
10914039Sstacze01@arm.com        uint32_t ssid;
11014039Sstacze01@arm.com        Addr va;
11114039Sstacze01@arm.com        Addr vaMask;
11214039Sstacze01@arm.com
11314039Sstacze01@arm.com        // EXTRA TAGS
11414039Sstacze01@arm.com        uint16_t asid;
11514039Sstacze01@arm.com        uint16_t vmid;
11614039Sstacze01@arm.com
11714039Sstacze01@arm.com        // OUTPUTS
11814039Sstacze01@arm.com        Addr pa;
11914039Sstacze01@arm.com        uint8_t permissions;
12014039Sstacze01@arm.com    };
12114039Sstacze01@arm.com
12214039Sstacze01@arm.com    SMMUTLB(unsigned numEntries, unsigned _associativity,
12314039Sstacze01@arm.com            const std::string &policy);
12414039Sstacze01@arm.com    SMMUTLB(const SMMUTLB& tlb) = delete;
12514039Sstacze01@arm.com    virtual ~SMMUTLB() {}
12614039Sstacze01@arm.com
12714039Sstacze01@arm.com    const Entry *lookup(uint32_t sid, uint32_t ssid, Addr va,
12814039Sstacze01@arm.com                        bool updStats=true);
12914039Sstacze01@arm.com    const Entry *lookupAnyVA(uint32_t sid, uint32_t ssid,
13014039Sstacze01@arm.com                             bool updStats=true);
13114039Sstacze01@arm.com    void store(const Entry &incoming, AllocPolicy alloc);
13214039Sstacze01@arm.com
13314132Sgiacomo.travaglini@arm.com    void invalidateSSID(uint32_t sid, uint32_t ssid);
13414132Sgiacomo.travaglini@arm.com    void invalidateSID(uint32_t sid);
13514039Sstacze01@arm.com    void invalidateVA(Addr va, uint16_t asid, uint16_t vmid);
13614039Sstacze01@arm.com    void invalidateVAA(Addr va, uint16_t vmid);
13714039Sstacze01@arm.com    void invalidateASID(uint16_t asid, uint16_t vmid);
13814039Sstacze01@arm.com    void invalidateVMID(uint16_t vmid);
13914039Sstacze01@arm.com    void invalidateAll();
14014039Sstacze01@arm.com
14114039Sstacze01@arm.com  private:
14214039Sstacze01@arm.com    typedef std::vector<Entry> Set;
14314039Sstacze01@arm.com    std::vector<Set> sets;
14414039Sstacze01@arm.com
14514039Sstacze01@arm.com    size_t associativity;
14614039Sstacze01@arm.com
14714132Sgiacomo.travaglini@arm.com    size_t pickSetIdx(uint32_t sid, uint32_t ssid) const;
14814039Sstacze01@arm.com    size_t pickSetIdx(Addr va) const;
14914039Sstacze01@arm.com    size_t pickEntryIdxToReplace(const Set &set, AllocPolicy alloc);
15014039Sstacze01@arm.com};
15114039Sstacze01@arm.com
15214039Sstacze01@arm.comclass ARMArchTLB : public SMMUv3BaseCache
15314039Sstacze01@arm.com{
15414039Sstacze01@arm.com  public:
15514039Sstacze01@arm.com    struct Entry
15614039Sstacze01@arm.com    {
15714039Sstacze01@arm.com        bool valid;
15814039Sstacze01@arm.com        mutable uint32_t lastUsed;
15914039Sstacze01@arm.com
16014039Sstacze01@arm.com        // TAGS
16114039Sstacze01@arm.com        Addr va;
16214039Sstacze01@arm.com        Addr vaMask;
16314039Sstacze01@arm.com        uint16_t asid;
16414039Sstacze01@arm.com        uint16_t vmid;
16514039Sstacze01@arm.com
16614039Sstacze01@arm.com        // OUTPUTS
16714039Sstacze01@arm.com        Addr pa;
16814039Sstacze01@arm.com        uint8_t permissions;
16914039Sstacze01@arm.com    };
17014039Sstacze01@arm.com
17114039Sstacze01@arm.com    ARMArchTLB(unsigned numEntries, unsigned _associativity,
17214039Sstacze01@arm.com               const std::string &policy);
17314039Sstacze01@arm.com    virtual ~ARMArchTLB() {}
17414039Sstacze01@arm.com
17514039Sstacze01@arm.com    const Entry *lookup(Addr va, uint16_t asid, uint16_t vmid,
17614039Sstacze01@arm.com                        bool updStats=true);
17714039Sstacze01@arm.com
17814039Sstacze01@arm.com    void store(const Entry &incoming);
17914039Sstacze01@arm.com
18014039Sstacze01@arm.com    void invalidateVA(Addr va, uint16_t asid, uint16_t vmid);
18114039Sstacze01@arm.com    void invalidateVAA(Addr va, uint16_t vmid);
18214039Sstacze01@arm.com    void invalidateASID(uint16_t asid, uint16_t vmid);
18314039Sstacze01@arm.com    void invalidateVMID(uint16_t vmid);
18414039Sstacze01@arm.com    void invalidateAll();
18514039Sstacze01@arm.com
18614039Sstacze01@arm.com  private:
18714039Sstacze01@arm.com    typedef std::vector<Entry> Set;
18814039Sstacze01@arm.com    std::vector<Set> sets;
18914039Sstacze01@arm.com
19014039Sstacze01@arm.com    size_t associativity;
19114039Sstacze01@arm.com
19214039Sstacze01@arm.com    size_t pickSetIdx(Addr va, uint16_t asid, uint16_t vmid) const;
19314039Sstacze01@arm.com    size_t pickEntryIdxToReplace(const Set &set);
19414039Sstacze01@arm.com};
19514039Sstacze01@arm.com
19614039Sstacze01@arm.comclass IPACache : public SMMUv3BaseCache
19714039Sstacze01@arm.com{
19814039Sstacze01@arm.com  public:
19914039Sstacze01@arm.com    struct Entry
20014039Sstacze01@arm.com    {
20114039Sstacze01@arm.com        bool valid;
20214039Sstacze01@arm.com        mutable uint32_t lastUsed;
20314039Sstacze01@arm.com
20414039Sstacze01@arm.com        // TAGS
20514039Sstacze01@arm.com        Addr ipa;
20614039Sstacze01@arm.com        Addr ipaMask;
20714039Sstacze01@arm.com        uint16_t vmid;
20814039Sstacze01@arm.com
20914039Sstacze01@arm.com        // OUTPUTS
21014039Sstacze01@arm.com        Addr pa;
21114039Sstacze01@arm.com        uint8_t permissions;
21214039Sstacze01@arm.com    };
21314039Sstacze01@arm.com
21414039Sstacze01@arm.com    IPACache(unsigned numEntries, unsigned _associativity,
21514039Sstacze01@arm.com             const std::string &policy);
21614039Sstacze01@arm.com    virtual ~IPACache() {}
21714039Sstacze01@arm.com
21814039Sstacze01@arm.com    const Entry *lookup(Addr ipa, uint16_t vmid, bool updStats=true);
21914039Sstacze01@arm.com    void store(const Entry &incoming);
22014039Sstacze01@arm.com
22114039Sstacze01@arm.com    void invalidateIPA(Addr ipa, uint16_t vmid);
22214039Sstacze01@arm.com    void invalidateIPAA(Addr ipa);
22314039Sstacze01@arm.com    void invalidateVMID(uint16_t vmid);
22414039Sstacze01@arm.com    void invalidateAll();
22514039Sstacze01@arm.com
22614039Sstacze01@arm.com  private:
22714039Sstacze01@arm.com    typedef std::vector<Entry> Set;
22814039Sstacze01@arm.com    std::vector<Set> sets;
22914039Sstacze01@arm.com
23014039Sstacze01@arm.com    size_t associativity;
23114039Sstacze01@arm.com
23214039Sstacze01@arm.com    size_t pickSetIdx(Addr ipa, uint16_t vmid) const;
23314039Sstacze01@arm.com    size_t pickEntryIdxToReplace(const Set &set);
23414039Sstacze01@arm.com};
23514039Sstacze01@arm.com
23614039Sstacze01@arm.comclass ConfigCache : public SMMUv3BaseCache
23714039Sstacze01@arm.com{
23814039Sstacze01@arm.com  public:
23914039Sstacze01@arm.com    struct Entry
24014039Sstacze01@arm.com    {
24114039Sstacze01@arm.com        bool valid;
24214039Sstacze01@arm.com        mutable uint32_t lastUsed;
24314039Sstacze01@arm.com
24414039Sstacze01@arm.com        // TAGS
24514039Sstacze01@arm.com        uint32_t sid;
24614039Sstacze01@arm.com        uint32_t ssid;
24714039Sstacze01@arm.com
24814039Sstacze01@arm.com        // OUTPUTS
24914039Sstacze01@arm.com        bool stage1_en;
25014039Sstacze01@arm.com        bool stage2_en;
25114039Sstacze01@arm.com        Addr ttb0;
25214039Sstacze01@arm.com        Addr ttb1;
25314039Sstacze01@arm.com        Addr httb;
25414039Sstacze01@arm.com        uint16_t asid;
25514039Sstacze01@arm.com        uint16_t vmid;
25614039Sstacze01@arm.com        uint8_t stage1_tg;
25714039Sstacze01@arm.com        uint8_t stage2_tg;
25814101Sgiacomo.travaglini@arm.com        uint8_t t0sz;
25914101Sgiacomo.travaglini@arm.com        uint8_t s2t0sz;
26014039Sstacze01@arm.com    };
26114039Sstacze01@arm.com
26214039Sstacze01@arm.com    ConfigCache(unsigned numEntries, unsigned _associativity,
26314039Sstacze01@arm.com                const std::string &policy);
26414039Sstacze01@arm.com    virtual ~ConfigCache() {}
26514039Sstacze01@arm.com
26614039Sstacze01@arm.com    const Entry *lookup(uint32_t sid, uint32_t ssid, bool updStats=true);
26714039Sstacze01@arm.com    void store(const Entry &incoming);
26814039Sstacze01@arm.com
26914039Sstacze01@arm.com    void invalidateSSID(uint32_t sid, uint32_t ssid);
27014039Sstacze01@arm.com    void invalidateSID(uint32_t sid);
27114039Sstacze01@arm.com    void invalidateAll();
27214039Sstacze01@arm.com
27314039Sstacze01@arm.com  private:
27414039Sstacze01@arm.com    typedef std::vector<Entry> Set;
27514039Sstacze01@arm.com    std::vector<Set> sets;
27614039Sstacze01@arm.com
27714039Sstacze01@arm.com    size_t associativity;
27814039Sstacze01@arm.com
27914039Sstacze01@arm.com    size_t pickSetIdx(uint32_t sid, uint32_t ssid) const;
28014039Sstacze01@arm.com    size_t pickEntryIdxToReplace(const Set &set);
28114039Sstacze01@arm.com};
28214039Sstacze01@arm.com
28314039Sstacze01@arm.comclass WalkCache : public SMMUv3BaseCache
28414039Sstacze01@arm.com{
28514039Sstacze01@arm.com  public:
28614039Sstacze01@arm.com    struct Entry
28714039Sstacze01@arm.com    {
28814039Sstacze01@arm.com        bool valid;
28914039Sstacze01@arm.com        mutable uint32_t lastUsed;
29014039Sstacze01@arm.com
29114039Sstacze01@arm.com        // TAGS
29214039Sstacze01@arm.com        Addr va;
29314039Sstacze01@arm.com        Addr vaMask;
29414039Sstacze01@arm.com        uint16_t asid;
29514039Sstacze01@arm.com        uint16_t vmid;
29614039Sstacze01@arm.com        unsigned stage;
29714039Sstacze01@arm.com        unsigned level;
29814039Sstacze01@arm.com
29914039Sstacze01@arm.com        // OUTPUTS
30014039Sstacze01@arm.com        bool leaf;
30114039Sstacze01@arm.com        Addr pa;
30214039Sstacze01@arm.com        uint8_t permissions;
30314039Sstacze01@arm.com    };
30414039Sstacze01@arm.com
30514039Sstacze01@arm.com    WalkCache(const std::array<unsigned, 2*WALK_CACHE_LEVELS> &_sizes,
30614039Sstacze01@arm.com              unsigned _associativity, const std::string &policy);
30714039Sstacze01@arm.com    virtual ~WalkCache() {}
30814039Sstacze01@arm.com
30914039Sstacze01@arm.com    const Entry *lookup(Addr va, Addr vaMask, uint16_t asid, uint16_t vmid,
31014039Sstacze01@arm.com                        unsigned stage, unsigned level, bool updStats=true);
31114039Sstacze01@arm.com    void store(const Entry &incoming);
31214039Sstacze01@arm.com
31314221Sadrian.herrera@arm.com    void invalidateVA(Addr va, uint16_t asid, uint16_t vmid,
31414221Sadrian.herrera@arm.com                      const bool leaf_only);
31514221Sadrian.herrera@arm.com    void invalidateVAA(Addr va, uint16_t vmid, const bool leaf_only);
31614039Sstacze01@arm.com    void invalidateASID(uint16_t asid, uint16_t vmid);
31714039Sstacze01@arm.com    void invalidateVMID(uint16_t vmid);
31814039Sstacze01@arm.com    void invalidateAll();
31914039Sstacze01@arm.com
32014039Sstacze01@arm.com    void regStats(const std::string &name) override;
32114039Sstacze01@arm.com
32214039Sstacze01@arm.com  protected:
32314039Sstacze01@arm.com    unsigned int lookupsByStageLevel[2][WALK_CACHE_LEVELS];
32414039Sstacze01@arm.com    Stats::Formula averageLookupsByStageLevel[2][WALK_CACHE_LEVELS];
32514039Sstacze01@arm.com    Stats::Scalar totalLookupsByStageLevel[2][WALK_CACHE_LEVELS];
32614039Sstacze01@arm.com
32714039Sstacze01@arm.com    unsigned int missesByStageLevel[2][WALK_CACHE_LEVELS];
32814039Sstacze01@arm.com    Stats::Formula averageMissesByStageLevel[2][WALK_CACHE_LEVELS];
32914039Sstacze01@arm.com    Stats::Scalar totalMissesByStageLevel[2][WALK_CACHE_LEVELS];
33014039Sstacze01@arm.com
33114039Sstacze01@arm.com    unsigned int updatesByStageLevel[2][WALK_CACHE_LEVELS];
33214039Sstacze01@arm.com    Stats::Formula averageUpdatesByStageLevel[2][WALK_CACHE_LEVELS];
33314039Sstacze01@arm.com    Stats::Scalar totalUpdatesByStageLevel[2][WALK_CACHE_LEVELS];
33414039Sstacze01@arm.com
33514039Sstacze01@arm.com    Stats::Formula averageHitRateByStageLevel[2][WALK_CACHE_LEVELS];
33614039Sstacze01@arm.com
33714039Sstacze01@arm.com    Stats::Scalar insertionsByStageLevel[2][WALK_CACHE_LEVELS];
33814039Sstacze01@arm.com
33914039Sstacze01@arm.com  private:
34014039Sstacze01@arm.com    typedef std::vector<Entry> Set;
34114039Sstacze01@arm.com    std::vector<Set> sets;
34214039Sstacze01@arm.com
34314039Sstacze01@arm.com    size_t associativity;
34414039Sstacze01@arm.com    std::array<unsigned, 2*WALK_CACHE_LEVELS> sizes;
34514039Sstacze01@arm.com    std::array<unsigned, 2*WALK_CACHE_LEVELS> offsets;
34614039Sstacze01@arm.com
34714039Sstacze01@arm.com    size_t pickSetIdx(Addr va, Addr vaMask,
34814039Sstacze01@arm.com                      unsigned stage, unsigned level) const;
34914039Sstacze01@arm.com
35014039Sstacze01@arm.com    size_t pickEntryIdxToReplace(const Set &set,
35114039Sstacze01@arm.com                                 unsigned stage, unsigned level);
35214039Sstacze01@arm.com};
35314039Sstacze01@arm.com
35414039Sstacze01@arm.com#endif /* __DEV_ARM_SMMU_V3_CACHES_HH__ */
355