1# Copyright (c) 2015 ARM Limited
2# All rights reserved.
3#
4# The license below extends only to copyright in the software and shall
5# not be construed as granting a license to any other intellectual
6# property including but not limited to intellectual property relating
7# to a hardware implementation of the functionality of the software
8# licensed hereunder.  You may use the software subject to the license
9# terms below provided that you ensure that this notice is replicated
10# unmodified and in its entirety in all distributions of the software,
11# modified or unmodified, in source code or in binary form.
12#
13# Redistribution and use in source and binary forms, with or without
14# modification, are permitted provided that the following conditions are
15# met: redistributions of source code must retain the above copyright
16# notice, this list of conditions and the following disclaimer;
17# redistributions in binary form must reproduce the above copyright
18# notice, this list of conditions and the following disclaimer in the
19# documentation and/or other materials provided with the distribution;
20# neither the name of the copyright holders nor the names of its
21# contributors may be used to endorse or promote products derived from
22# this software without specific prior written permission.
23#
24# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35#
36# Authors: David Guillen Fandos
37
38from m5.SimObject import *
39from m5.objects.ClockedObject import ClockedObject
40
41from m5.params import *
42from m5.objects import ThermalDomain
43
44
45# Represents a thermal node
46class ThermalNode(SimObject):
47    type = 'ThermalNode'
48    cxx_header = "sim/power/thermal_node.hh"
49
50# Represents a thermal resistor
51class ThermalResistor(SimObject):
52    type = 'ThermalResistor'
53    cxx_header = "sim/power/thermal_model.hh"
54
55    cxx_exports = [
56        PyBindMethod("setNodes"),
57    ]
58
59    resistance = Param.Float(1.0, "Thermal resistance, expressed in Kelvin per Watt")
60
61# Represents a thermal capacitor
62class ThermalCapacitor(SimObject):
63    type = 'ThermalCapacitor'
64    cxx_header = "sim/power/thermal_model.hh"
65
66    cxx_exports = [
67        PyBindMethod("setNodes"),
68    ]
69
70    capacitance = Param.Float(1.0, "Thermal capacitance, expressed in Joules per Kelvin")
71
72# Represents a fixed temperature node (ie. air)
73class ThermalReference(SimObject, object):
74    type = 'ThermalReference'
75    cxx_header = "sim/power/thermal_model.hh"
76
77    cxx_exports = [
78        PyBindMethod("setNode"),
79    ]
80
81    # Static temperature which may change over time
82    temperature = Param.Float(25.0, "Operational temperature in Celsius")
83
84
85# Represents a thermal capacitor
86class ThermalModel(ClockedObject):
87    type = 'ThermalModel'
88    cxx_header = "sim/power/thermal_model.hh"
89
90    cxx_exports = [
91        PyBindMethod("addCapacitor"),
92        PyBindMethod("addResistor"),
93        PyBindMethod("addReference"),
94        PyBindMethod("addDomain"),
95        PyBindMethod("addNode"),
96        PyBindMethod("doStep"),
97    ]
98
99    step = Param.Float(0.01, "Simulation step (in seconds) for thermal simulation")
100
101    def populate(self):
102        if not hasattr(self,"_capacitors"): self._capacitors = []
103        if not hasattr(self,"_resistors"): self._resistors = []
104        if not hasattr(self,"_domains"): self._domains = []
105        if not hasattr(self,"_references"): self._references = []
106        if not hasattr(self,"_nodes"): self._nodes = []
107
108    def init(self):
109        self.populate()
110
111        for ref, node in self._references:
112            ref.getCCObject().setNode(node.getCCObject())
113            self.getCCObject().addReference(ref.getCCObject())
114
115        for dom, node in self._domains:
116            dom.getCCObject().setNode(node.getCCObject())
117            self.getCCObject().addDomain(dom.getCCObject())
118
119        for cap, node1, node2 in self._capacitors:
120            cap.getCCObject().setNodes(node1.getCCObject(), node2.getCCObject())
121            self.getCCObject().addCapacitor(cap.getCCObject())
122
123        for res, node1, node2 in self._resistors:
124            res.getCCObject().setNodes(node1.getCCObject(), node2.getCCObject())
125            self.getCCObject().addResistor(res.getCCObject())
126
127        for node in self._nodes:
128            self.getCCObject().addNode(node.getCCObject())
129
130    def addCapacitor(self, cap, node1, node2):
131        self.populate()
132        self._capacitors.append( (cap, node1, node2) )
133        self._parent.thermal_components.append(cap)
134        self.addNodes(node1,node2)
135
136    def addResistor(self, res, node1, node2):
137        self.populate()
138        self._resistors.append( (res, node1, node2) )
139        self._parent.thermal_components.append(res)
140        self.addNodes(node1,node2)
141
142    def addReference(self, ref, node):
143        self.populate()
144        self._references.append( (ref, node) )
145        self._parent.thermal_components.append(ref)
146        self.addNodes(node)
147
148    def addDomain(self, dom, node):
149        self.populate()
150        self._domains.append( (dom, node) )
151        self.addNodes(node)
152
153    def addNodes(self, *nodes):
154        for node in nodes:
155            if node not in self._nodes:
156                self._nodes.append(node)
157                self._parent.thermal_components.append(node)
158
159    def doStep(self):
160        self.getCCObject().doStep()
161