MemConfig.py revision 11183:276ad9121192
112855Sgabeblack@google.com# Copyright (c) 2013 ARM Limited
212855Sgabeblack@google.com# All rights reserved.
312855Sgabeblack@google.com#
412855Sgabeblack@google.com# The license below extends only to copyright in the software and shall
512855Sgabeblack@google.com# not be construed as granting a license to any other intellectual
612855Sgabeblack@google.com# property including but not limited to intellectual property relating
712855Sgabeblack@google.com# to a hardware implementation of the functionality of the software
812855Sgabeblack@google.com# licensed hereunder.  You may use the software subject to the license
912855Sgabeblack@google.com# terms below provided that you ensure that this notice is replicated
1012855Sgabeblack@google.com# unmodified and in its entirety in all distributions of the software,
1112855Sgabeblack@google.com# modified or unmodified, in source code or in binary form.
1212855Sgabeblack@google.com#
1312855Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
1412855Sgabeblack@google.com# modification, are permitted provided that the following conditions are
1512855Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
1612855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
1712855Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
1812855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
1912855Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
2012855Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
2112855Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
2212855Sgabeblack@google.com# this software without specific prior written permission.
2312855Sgabeblack@google.com#
2412855Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2512855Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2612855Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2712855Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2812855Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2912855Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3012855Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3112855Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3212855Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3312855Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3412855Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3512855Sgabeblack@google.com#
3612855Sgabeblack@google.com# Authors: Andreas Sandberg
3712855Sgabeblack@google.com#          Andreas Hansson
3812855Sgabeblack@google.com
3912855Sgabeblack@google.comimport m5.objects
4012855Sgabeblack@google.comimport inspect
4112855Sgabeblack@google.comimport sys
4212855Sgabeblack@google.comimport HMC
4312855Sgabeblack@google.comfrom textwrap import  TextWrapper
4412855Sgabeblack@google.com
4512855Sgabeblack@google.com# Dictionary of mapping names of real memory controller models to
4612855Sgabeblack@google.com# classes.
4712855Sgabeblack@google.com_mem_classes = {}
4812855Sgabeblack@google.com
4912855Sgabeblack@google.comdef is_mem_class(cls):
5012855Sgabeblack@google.com    """Determine if a class is a memory controller that can be instantiated"""
5112855Sgabeblack@google.com
5212855Sgabeblack@google.com    # We can't use the normal inspect.isclass because the ParamFactory
5312855Sgabeblack@google.com    # and ProxyFactory classes have a tendency to confuse it.
5412855Sgabeblack@google.com    try:
5512855Sgabeblack@google.com        return issubclass(cls, m5.objects.AbstractMemory) and \
5612855Sgabeblack@google.com            not cls.abstract
5712855Sgabeblack@google.com    except TypeError:
5812855Sgabeblack@google.com        return False
5912855Sgabeblack@google.com
6012855Sgabeblack@google.comdef get(name):
6112855Sgabeblack@google.com    """Get a memory class from a user provided class name."""
6212855Sgabeblack@google.com
6312855Sgabeblack@google.com    try:
64        mem_class = _mem_classes[name]
65        return mem_class
66    except KeyError:
67        print "%s is not a valid memory controller." % (name,)
68        sys.exit(1)
69
70def print_mem_list():
71    """Print a list of available memory classes."""
72
73    print "Available memory classes:"
74    doc_wrapper = TextWrapper(initial_indent="\t\t", subsequent_indent="\t\t")
75    for name, cls in _mem_classes.items():
76        print "\t%s" % name
77
78        # Try to extract the class documentation from the class help
79        # string.
80        doc = inspect.getdoc(cls)
81        if doc:
82            for line in doc_wrapper.wrap(doc):
83                print line
84
85def mem_names():
86    """Return a list of valid memory names."""
87    return _mem_classes.keys()
88
89# Add all memory controllers in the object hierarchy.
90for name, cls in inspect.getmembers(m5.objects, is_mem_class):
91    _mem_classes[name] = cls
92
93def create_mem_ctrl(cls, r, i, nbr_mem_ctrls, intlv_bits, intlv_size):
94    """
95    Helper function for creating a single memoy controller from the given
96    options.  This function is invoked multiple times in config_mem function
97    to create an array of controllers.
98    """
99
100    import math
101    intlv_low_bit = int(math.log(intlv_size, 2))
102
103    # Use basic hashing for the channel selection, and preferably use
104    # the lower tag bits from the last level cache. As we do not know
105    # the details of the caches here, make an educated guess. 4 MByte
106    # 4-way associative with 64 byte cache lines is 6 offset bits and
107    # 14 index bits.
108    xor_low_bit = 20
109
110    # Create an instance so we can figure out the address
111    # mapping and row-buffer size
112    ctrl = cls()
113
114    # Only do this for DRAMs
115    if issubclass(cls, m5.objects.DRAMCtrl):
116        # Inform each controller how many channels to account
117        # for
118        ctrl.channels = nbr_mem_ctrls
119
120        # If the channel bits are appearing after the column
121        # bits, we need to add the appropriate number of bits
122        # for the row buffer size
123        if ctrl.addr_mapping.value == 'RoRaBaChCo':
124            # This computation only really needs to happen
125            # once, but as we rely on having an instance we
126            # end up having to repeat it for each and every
127            # one
128            rowbuffer_size = ctrl.device_rowbuffer_size.value * \
129                ctrl.devices_per_rank.value
130
131            intlv_low_bit = int(math.log(rowbuffer_size, 2))
132
133    # We got all we need to configure the appropriate address
134    # range
135    ctrl.range = m5.objects.AddrRange(r.start, size = r.size(),
136                                      intlvHighBit = \
137                                          intlv_low_bit + intlv_bits - 1,
138                                      xorHighBit = \
139                                          xor_low_bit + intlv_bits - 1,
140                                      intlvBits = intlv_bits,
141                                      intlvMatch = i)
142    return ctrl
143
144def config_mem(options, system):
145    """
146    Create the memory controllers based on the options and attach them.
147
148    If requested, we make a multi-channel configuration of the
149    selected memory controller class by creating multiple instances of
150    the specific class. The individual controllers have their
151    parameters set such that the address range is interleaved between
152    them.
153    """
154
155    if ( options.mem_type == "HMC_2500_x32"):
156        HMC.config_hmc(options, system)
157        subsystem = system.hmc
158        xbar = system.hmc.xbar
159    else:
160        subsystem = system
161        xbar = system.membus
162
163    if options.tlm_memory:
164        system.external_memory = m5.objects.ExternalSlave(
165            port_type="tlm",
166            port_data=options.tlm_memory,
167            port=system.membus.master,
168            addr_ranges=system.mem_ranges)
169        system.kernel_addr_check = False
170        return
171
172    if options.external_memory_system:
173        subsystem.external_memory = m5.objects.ExternalSlave(
174            port_type=options.external_memory_system,
175            port_data="init_mem0", port=xbar.master,
176            addr_ranges=system.mem_ranges)
177        subsystem.kernel_addr_check = False
178        return
179
180    nbr_mem_ctrls = options.mem_channels
181    import math
182    from m5.util import fatal
183    intlv_bits = int(math.log(nbr_mem_ctrls, 2))
184    if 2 ** intlv_bits != nbr_mem_ctrls:
185        fatal("Number of memory channels must be a power of 2")
186
187    cls = get(options.mem_type)
188    mem_ctrls = []
189
190    # The default behaviour is to interleave memory channels on 128
191    # byte granularity, or cache line granularity if larger than 128
192    # byte. This value is based on the locality seen across a large
193    # range of workloads.
194    intlv_size = max(128, system.cache_line_size.value)
195
196    # For every range (most systems will only have one), create an
197    # array of controllers and set their parameters to match their
198    # address mapping in the case of a DRAM
199    for r in system.mem_ranges:
200        for i in xrange(nbr_mem_ctrls):
201            mem_ctrl = create_mem_ctrl(cls, r, i, nbr_mem_ctrls, intlv_bits,
202                                       intlv_size)
203            # Set the number of ranks based on the command-line
204            # options if it was explicitly set
205            if issubclass(cls, m5.objects.DRAMCtrl) and \
206                    options.mem_ranks:
207                mem_ctrl.ranks_per_channel = options.mem_ranks
208
209            mem_ctrls.append(mem_ctrl)
210
211    subsystem.mem_ctrls = mem_ctrls
212
213    # Connect the controllers to the membus
214    for i in xrange(len(subsystem.mem_ctrls)):
215        subsystem.mem_ctrls[i].port = xbar.master
216