1/*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 *          Steve Reinhardt
30 */
31
32#ifndef __ARCH_ALPHA_TLB_HH__
33#define __ARCH_ALPHA_TLB_HH__
34
35#include <map>
36
37#include "arch/alpha/ev5.hh"
38#include "arch/alpha/isa_traits.hh"
39#include "arch/alpha/pagetable.hh"
40#include "arch/alpha/utility.hh"
41#include "arch/alpha/vtophys.hh"
42#include "arch/generic/tlb.hh"
43#include "base/statistics.hh"
44#include "mem/request.hh"
45#include "params/AlphaTLB.hh"
46
47class ThreadContext;
48
49namespace AlphaISA {
50
51struct TlbEntry;
52
53class TLB : public BaseTLB
54{
55  protected:
56    mutable Stats::Scalar fetch_hits;
57    mutable Stats::Scalar fetch_misses;
58    mutable Stats::Scalar fetch_acv;
59    mutable Stats::Formula fetch_accesses;
60    mutable Stats::Scalar read_hits;
61    mutable Stats::Scalar read_misses;
62    mutable Stats::Scalar read_acv;
63    mutable Stats::Scalar read_accesses;
64    mutable Stats::Scalar write_hits;
65    mutable Stats::Scalar write_misses;
66    mutable Stats::Scalar write_acv;
67    mutable Stats::Scalar write_accesses;
68    Stats::Formula data_hits;
69    Stats::Formula data_misses;
70    Stats::Formula data_acv;
71    Stats::Formula data_accesses;
72
73
74    typedef std::multimap<Addr, int> PageTable;
75    PageTable lookupTable;  // Quick lookup into page table
76
77    std::vector<TlbEntry> table; // the Page Table
78    int nlu;                // not last used entry (for replacement)
79
80    void nextnlu() { if (++nlu >= table.size()) nlu = 0; }
81    TlbEntry *lookup(Addr vpn, uint8_t asn);
82
83  public:
84    typedef AlphaTLBParams Params;
85    TLB(const Params *p);
86    virtual ~TLB();
87
88    void takeOverFrom(BaseTLB *otlb) override {}
89
90    void regStats() override;
91
92    int getsize() const { return table.size(); }
93
94    TlbEntry &index(bool advance = true);
95    void insert(Addr vaddr, TlbEntry &entry);
96
97    void flushAll() override;
98    void flushProcesses();
99    void flushAddr(Addr addr, uint8_t asn);
100
101    void
102    demapPage(Addr vaddr, uint64_t asn) override
103    {
104        assert(asn < (1 << 8));
105        flushAddr(vaddr, asn);
106    }
107
108    // static helper functions... really EV5 VM traits
109    static bool
110    validVirtualAddress(Addr vaddr)
111    {
112        // unimplemented bits must be all 0 or all 1
113        Addr unimplBits = vaddr & VAddrUnImplMask;
114        return unimplBits == 0 || unimplBits == VAddrUnImplMask;
115    }
116
117    static Fault checkCacheability(const RequestPtr &req, bool itb = false);
118
119    // Checkpointing
120    void serialize(CheckpointOut &cp) const override;
121    void unserialize(CheckpointIn &cp) override;
122
123    // Most recently used page table entries
124    TlbEntry *EntryCache[3];
125    inline void
126    flushCache()
127    {
128        memset(EntryCache, 0, 3 * sizeof(TlbEntry*));
129    }
130
131    inline TlbEntry *
132    updateCache(TlbEntry *entry) {
133        EntryCache[2] = EntryCache[1];
134        EntryCache[1] = EntryCache[0];
135        EntryCache[0] = entry;
136        return entry;
137    }
138
139  protected:
140    Fault translateData(const RequestPtr &req, ThreadContext *tc, bool write);
141    Fault translateInst(const RequestPtr &req, ThreadContext *tc);
142
143  public:
144    Fault translateAtomic(
145            const RequestPtr &req, ThreadContext *tc, Mode mode) override;
146    void translateTiming(
147            const RequestPtr &req, ThreadContext *tc,
148            Translation *translation, Mode mode) override;
149    Fault finalizePhysical(
150            const RequestPtr &req, ThreadContext *tc,
151            Mode mode) const override;
152};
153
154} // namespace AlphaISA
155
156#endif // __ARCH_ALPHA_TLB_HH__
157