111539Sandreas.hansson@arm.com# Copyright (c) 2012-2013, 2015-2016 ARM Limited
29522SAndreas.Sandberg@ARM.com# All rights reserved
311320Ssteve.reinhardt@amd.com#
49522SAndreas.Sandberg@ARM.com# The license below extends only to copyright in the software and shall
59522SAndreas.Sandberg@ARM.com# not be construed as granting a license to any other intellectual
69522SAndreas.Sandberg@ARM.com# property including but not limited to intellectual property relating
79522SAndreas.Sandberg@ARM.com# to a hardware implementation of the functionality of the software
89522SAndreas.Sandberg@ARM.com# licensed hereunder.  You may use the software subject to the license
99522SAndreas.Sandberg@ARM.com# terms below provided that you ensure that this notice is replicated
109522SAndreas.Sandberg@ARM.com# unmodified and in its entirety in all distributions of the software,
119522SAndreas.Sandberg@ARM.com# modified or unmodified, in source code or in binary form.
1211320Ssteve.reinhardt@amd.com#
136981SLisa.Hsu@amd.com# Copyright (c) 2010 Advanced Micro Devices, Inc.
146981SLisa.Hsu@amd.com# All rights reserved.
156981SLisa.Hsu@amd.com#
166981SLisa.Hsu@amd.com# Redistribution and use in source and binary forms, with or without
176981SLisa.Hsu@amd.com# modification, are permitted provided that the following conditions are
186981SLisa.Hsu@amd.com# met: redistributions of source code must retain the above copyright
196981SLisa.Hsu@amd.com# notice, this list of conditions and the following disclaimer;
206981SLisa.Hsu@amd.com# redistributions in binary form must reproduce the above copyright
216981SLisa.Hsu@amd.com# notice, this list of conditions and the following disclaimer in the
226981SLisa.Hsu@amd.com# documentation and/or other materials provided with the distribution;
236981SLisa.Hsu@amd.com# neither the name of the copyright holders nor the names of its
246981SLisa.Hsu@amd.com# contributors may be used to endorse or promote products derived from
256981SLisa.Hsu@amd.com# this software without specific prior written permission.
266981SLisa.Hsu@amd.com#
276981SLisa.Hsu@amd.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
286981SLisa.Hsu@amd.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
296981SLisa.Hsu@amd.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
306981SLisa.Hsu@amd.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
316981SLisa.Hsu@amd.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
326981SLisa.Hsu@amd.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
336981SLisa.Hsu@amd.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
346981SLisa.Hsu@amd.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
356981SLisa.Hsu@amd.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
366981SLisa.Hsu@amd.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
376981SLisa.Hsu@amd.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
386981SLisa.Hsu@amd.com#
396981SLisa.Hsu@amd.com# Authors: Lisa Hsu
406981SLisa.Hsu@amd.com
416981SLisa.Hsu@amd.com# Configure the M5 cache hierarchy config in one place
426981SLisa.Hsu@amd.com#
436981SLisa.Hsu@amd.com
4412564Sgabeblack@google.comfrom __future__ import print_function
4513774Sandreas.sandberg@arm.comfrom __future__ import absolute_import
4612564Sgabeblack@google.com
476981SLisa.Hsu@amd.comimport m5
486981SLisa.Hsu@amd.comfrom m5.objects import *
4913774Sandreas.sandberg@arm.comfrom .Caches import *
5013876Sjavier.bueno@metempsy.comfrom . import HWPConfig
516981SLisa.Hsu@amd.com
526981SLisa.Hsu@amd.comdef config_cache(options, system):
5310780SCurtis.Dunham@arm.com    if options.external_memory_system and (options.caches or options.l2cache):
5412564Sgabeblack@google.com        print("External caches and internal caches are exclusive options.\n")
5510780SCurtis.Dunham@arm.com        sys.exit(1)
5610780SCurtis.Dunham@arm.com
5710780SCurtis.Dunham@arm.com    if options.external_memory_system:
5810780SCurtis.Dunham@arm.com        ExternalCache = ExternalCacheFactory(options.external_memory_system)
5910780SCurtis.Dunham@arm.com
6012014Sgabeblack@google.com    if options.cpu_type == "O3_ARM_v7a_3":
619522SAndreas.Sandberg@ARM.com        try:
6213774Sandreas.sandberg@arm.com            import cores.arm.O3_ARM_v7a as core
639522SAndreas.Sandberg@ARM.com        except:
6412564Sgabeblack@google.com            print("O3_ARM_v7a_3 is unavailable. Did you compile the O3 model?")
659522SAndreas.Sandberg@ARM.com            sys.exit(1)
669522SAndreas.Sandberg@ARM.com
6711539Sandreas.hansson@arm.com        dcache_class, icache_class, l2_cache_class, walk_cache_class = \
6813774Sandreas.sandberg@arm.com            core.O3_ARM_v7a_DCache, core.O3_ARM_v7a_ICache, \
6913774Sandreas.sandberg@arm.com            core.O3_ARM_v7aL2, \
7013806Sjavier.bueno@metempsy.com            core.O3_ARM_v7aWalkCache
7113811Sjavier.bueno@metempsy.com    elif options.cpu_type == "HPI":
7213811Sjavier.bueno@metempsy.com        try:
7313811Sjavier.bueno@metempsy.com            import cores.arm.HPI as core
7413811Sjavier.bueno@metempsy.com        except:
7513811Sjavier.bueno@metempsy.com            print("HPI is unavailable.")
7613811Sjavier.bueno@metempsy.com            sys.exit(1)
7713811Sjavier.bueno@metempsy.com
7813811Sjavier.bueno@metempsy.com        dcache_class, icache_class, l2_cache_class, walk_cache_class = \
7913811Sjavier.bueno@metempsy.com            core.HPI_DCache, core.HPI_ICache, core.HPI_L2, core.HPI_WalkCache
809522SAndreas.Sandberg@ARM.com    else:
8111539Sandreas.hansson@arm.com        dcache_class, icache_class, l2_cache_class, walk_cache_class = \
8211539Sandreas.hansson@arm.com            L1_DCache, L1_ICache, L2Cache, None
8311539Sandreas.hansson@arm.com
8411539Sandreas.hansson@arm.com        if buildEnv['TARGET_ISA'] == 'x86':
8511539Sandreas.hansson@arm.com            walk_cache_class = PageTableWalkerCache
869522SAndreas.Sandberg@ARM.com
879815SAndreas Hansson <andreas.hansson>    # Set the cache line size of the system
889815SAndreas Hansson <andreas.hansson>    system.cache_line_size = options.cacheline_size
899815SAndreas Hansson <andreas.hansson>
9011251Sradhika.jagtap@ARM.com    # If elastic trace generation is enabled, make sure the memory system is
9111251Sradhika.jagtap@ARM.com    # minimal so that compute delays do not include memory access latencies.
9211251Sradhika.jagtap@ARM.com    # Configure the compulsory L1 caches for the O3CPU, do not configure
9311251Sradhika.jagtap@ARM.com    # any more caches.
9411251Sradhika.jagtap@ARM.com    if options.l2cache and options.elastic_trace_en:
9511251Sradhika.jagtap@ARM.com        fatal("When elastic trace is enabled, do not configure L2 caches.")
9611251Sradhika.jagtap@ARM.com
976981SLisa.Hsu@amd.com    if options.l2cache:
989284Sandreas.hansson@arm.com        # Provide a clock for the L2 and the L1-to-L2 bus here as they
999284Sandreas.hansson@arm.com        # are not connected using addTwoLevelCacheHierarchy. Use the
10010720Sandreas.hansson@arm.com        # same clock as the CPUs.
1019793Sakash.bagdia@arm.com        system.l2 = l2_cache_class(clk_domain=system.cpu_clk_domain,
1029522SAndreas.Sandberg@ARM.com                                   size=options.l2_size,
1039815SAndreas Hansson <andreas.hansson>                                   assoc=options.l2_assoc)
1048724Srdreslin@umich.edu
10510720Sandreas.hansson@arm.com        system.tol2bus = L2XBar(clk_domain = system.cpu_clk_domain)
1068839Sandreas.hansson@arm.com        system.l2.cpu_side = system.tol2bus.master
1078839Sandreas.hansson@arm.com        system.l2.mem_side = system.membus.slave
10813876Sjavier.bueno@metempsy.com        if options.l2_hwp_type:
10913876Sjavier.bueno@metempsy.com            hwpClass = HWPConfig.get(options.l2_hwp_type)
11013876Sjavier.bueno@metempsy.com            if system.l2.prefetcher != "Null":
11113876Sjavier.bueno@metempsy.com                print("Warning: l2-hwp-type is set (", hwpClass, "), but",
11213876Sjavier.bueno@metempsy.com                      "the current l2 has a default Hardware Prefetcher",
11313876Sjavier.bueno@metempsy.com                      "of type", type(system.l2.prefetcher), ", using the",
11413876Sjavier.bueno@metempsy.com                      "specified by the flag option.")
11513876Sjavier.bueno@metempsy.com            system.l2.prefetcher = hwpClass()
1166981SLisa.Hsu@amd.com
11710613SMarco.Elver@ARM.com    if options.memchecker:
11810613SMarco.Elver@ARM.com        system.memchecker = MemChecker()
11910613SMarco.Elver@ARM.com
12013731Sandreas.sandberg@arm.com    for i in range(options.num_cpus):
1216981SLisa.Hsu@amd.com        if options.caches:
1229522SAndreas.Sandberg@ARM.com            icache = icache_class(size=options.l1i_size,
1239815SAndreas Hansson <andreas.hansson>                                  assoc=options.l1i_assoc)
1249522SAndreas.Sandberg@ARM.com            dcache = dcache_class(size=options.l1d_size,
1259815SAndreas Hansson <andreas.hansson>                                  assoc=options.l1d_assoc)
1268724Srdreslin@umich.edu
12711539Sandreas.hansson@arm.com            # If we have a walker cache specified, instantiate two
12811539Sandreas.hansson@arm.com            # instances here
12911539Sandreas.hansson@arm.com            if walk_cache_class:
13011539Sandreas.hansson@arm.com                iwalkcache = walk_cache_class()
13111539Sandreas.hansson@arm.com                dwalkcache = walk_cache_class()
13211539Sandreas.hansson@arm.com            else:
13311539Sandreas.hansson@arm.com                iwalkcache = None
13411539Sandreas.hansson@arm.com                dwalkcache = None
13511539Sandreas.hansson@arm.com
13610613SMarco.Elver@ARM.com            if options.memchecker:
13710613SMarco.Elver@ARM.com                dcache_mon = MemCheckerMonitor(warn_only=True)
13810613SMarco.Elver@ARM.com                dcache_real = dcache
13910613SMarco.Elver@ARM.com
14010613SMarco.Elver@ARM.com                # Do not pass the memchecker into the constructor of
14110613SMarco.Elver@ARM.com                # MemCheckerMonitor, as it would create a copy; we require
14210613SMarco.Elver@ARM.com                # exactly one MemChecker instance.
14310613SMarco.Elver@ARM.com                dcache_mon.memchecker = system.memchecker
14410613SMarco.Elver@ARM.com
14510613SMarco.Elver@ARM.com                # Connect monitor
14610613SMarco.Elver@ARM.com                dcache_mon.mem_side = dcache.cpu_side
14710613SMarco.Elver@ARM.com
14810613SMarco.Elver@ARM.com                # Let CPU connect to monitors
14910613SMarco.Elver@ARM.com                dcache = dcache_mon
15010613SMarco.Elver@ARM.com
15113876Sjavier.bueno@metempsy.com            if options.l1d_hwp_type:
15213876Sjavier.bueno@metempsy.com                hwpClass = HWPConfig.get(options.l1d_hwp_type)
15313876Sjavier.bueno@metempsy.com                if dcache.prefetcher != m5.params.NULL:
15413876Sjavier.bueno@metempsy.com                    print("Warning: l1d-hwp-type is set (", hwpClass, "), but",
15513876Sjavier.bueno@metempsy.com                          "the current l1d has a default Hardware Prefetcher",
15613876Sjavier.bueno@metempsy.com                          "of type", type(dcache.prefetcher), ", using the",
15713876Sjavier.bueno@metempsy.com                          "specified by the flag option.")
15813876Sjavier.bueno@metempsy.com                dcache.prefetcher = hwpClass()
15913876Sjavier.bueno@metempsy.com
16013876Sjavier.bueno@metempsy.com            if options.l1i_hwp_type:
16113876Sjavier.bueno@metempsy.com                hwpClass = HWPConfig.get(options.l1i_hwp_type)
16213876Sjavier.bueno@metempsy.com                if icache.prefetcher != m5.params.NULL:
16313876Sjavier.bueno@metempsy.com                    print("Warning: l1i-hwp-type is set (", hwpClass, "), but",
16413876Sjavier.bueno@metempsy.com                          "the current l1i has a default Hardware Prefetcher",
16513876Sjavier.bueno@metempsy.com                          "of type", type(icache.prefetcher), ", using the",
16613876Sjavier.bueno@metempsy.com                          "specified by the flag option.")
16713876Sjavier.bueno@metempsy.com                icache.prefetcher = hwpClass()
16813876Sjavier.bueno@metempsy.com
1699284Sandreas.hansson@arm.com            # When connecting the caches, the clock is also inherited
1709284Sandreas.hansson@arm.com            # from the CPU in question
17111539Sandreas.hansson@arm.com            system.cpu[i].addPrivateSplitL1Caches(icache, dcache,
17211539Sandreas.hansson@arm.com                                                  iwalkcache, dwalkcache)
17310613SMarco.Elver@ARM.com
17410613SMarco.Elver@ARM.com            if options.memchecker:
17510613SMarco.Elver@ARM.com                # The mem_side ports of the caches haven't been connected yet.
17610613SMarco.Elver@ARM.com                # Make sure connectAllPorts connects the right objects.
17710613SMarco.Elver@ARM.com                system.cpu[i].dcache = dcache_real
17810613SMarco.Elver@ARM.com                system.cpu[i].dcache_mon = dcache_mon
17910613SMarco.Elver@ARM.com
18010780SCurtis.Dunham@arm.com        elif options.external_memory_system:
18110780SCurtis.Dunham@arm.com            # These port names are presented to whatever 'external' system
18210780SCurtis.Dunham@arm.com            # gem5 is connecting to.  Its configuration will likely depend
18310780SCurtis.Dunham@arm.com            # on these names.  For simplicity, we would advise configuring
18410780SCurtis.Dunham@arm.com            # it to use this naming scheme; if this isn't possible, change
18510780SCurtis.Dunham@arm.com            # the names below.
18610780SCurtis.Dunham@arm.com            if buildEnv['TARGET_ISA'] in ['x86', 'arm']:
18710780SCurtis.Dunham@arm.com                system.cpu[i].addPrivateSplitL1Caches(
18810780SCurtis.Dunham@arm.com                        ExternalCache("cpu%d.icache" % i),
18910780SCurtis.Dunham@arm.com                        ExternalCache("cpu%d.dcache" % i),
19010780SCurtis.Dunham@arm.com                        ExternalCache("cpu%d.itb_walker_cache" % i),
19110780SCurtis.Dunham@arm.com                        ExternalCache("cpu%d.dtb_walker_cache" % i))
19210780SCurtis.Dunham@arm.com            else:
19310780SCurtis.Dunham@arm.com                system.cpu[i].addPrivateSplitL1Caches(
19410780SCurtis.Dunham@arm.com                        ExternalCache("cpu%d.icache" % i),
19510780SCurtis.Dunham@arm.com                        ExternalCache("cpu%d.dcache" % i))
19610780SCurtis.Dunham@arm.com
1978863Snilay@cs.wisc.edu        system.cpu[i].createInterruptController()
1986981SLisa.Hsu@amd.com        if options.l2cache:
1997876Sgblack@eecs.umich.edu            system.cpu[i].connectAllPorts(system.tol2bus, system.membus)
20010780SCurtis.Dunham@arm.com        elif options.external_memory_system:
20110780SCurtis.Dunham@arm.com            system.cpu[i].connectUncachedPorts(system.membus)
2026981SLisa.Hsu@amd.com        else:
2037876Sgblack@eecs.umich.edu            system.cpu[i].connectAllPorts(system.membus)
2046981SLisa.Hsu@amd.com
2056981SLisa.Hsu@amd.com    return system
20610780SCurtis.Dunham@arm.com
20710780SCurtis.Dunham@arm.com# ExternalSlave provides a "port", but when that port connects to a cache,
20810780SCurtis.Dunham@arm.com# the connecting CPU SimObject wants to refer to its "cpu_side".
20910780SCurtis.Dunham@arm.com# The 'ExternalCache' class provides this adaptation by rewriting the name,
21010780SCurtis.Dunham@arm.com# eliminating distracting changes elsewhere in the config code.
21110780SCurtis.Dunham@arm.comclass ExternalCache(ExternalSlave):
21210780SCurtis.Dunham@arm.com    def __getattr__(cls, attr):
21310780SCurtis.Dunham@arm.com        if (attr == "cpu_side"):
21410780SCurtis.Dunham@arm.com            attr = "port"
21510780SCurtis.Dunham@arm.com        return super(ExternalSlave, cls).__getattr__(attr)
21610780SCurtis.Dunham@arm.com
21710780SCurtis.Dunham@arm.com    def __setattr__(cls, attr, value):
21810780SCurtis.Dunham@arm.com        if (attr == "cpu_side"):
21910780SCurtis.Dunham@arm.com            attr = "port"
22010780SCurtis.Dunham@arm.com        return super(ExternalSlave, cls).__setattr__(attr, value)
22110780SCurtis.Dunham@arm.com
22210780SCurtis.Dunham@arm.comdef ExternalCacheFactory(port_type):
22310780SCurtis.Dunham@arm.com    def make(name):
22410780SCurtis.Dunham@arm.com        return ExternalCache(port_data=name, port_type=port_type,
22510780SCurtis.Dunham@arm.com                             addr_ranges=[AllMemory])
22610780SCurtis.Dunham@arm.com    return make
227