5b3dd98abf4b432d16e3f91de41e59abc0eb44ec
[gem5.git] / src / dev / arm / smmu_v3_slaveifc.cc
1 /*
2 * Copyright (c) 2019 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include "dev/arm/smmu_v3_slaveifc.hh"
39
40 #include "base/trace.hh"
41 #include "debug/SMMUv3.hh"
42 #include "dev/arm/smmu_v3.hh"
43 #include "dev/arm/smmu_v3_transl.hh"
44
45 SMMUv3SlaveInterface::SMMUv3SlaveInterface(
46 const SMMUv3SlaveInterfaceParams *p) :
47 ClockedObject(p),
48 smmu(nullptr),
49 microTLB(new SMMUTLB(p->utlb_entries,
50 p->utlb_assoc,
51 p->utlb_policy)),
52 mainTLB(new SMMUTLB(p->tlb_entries,
53 p->tlb_assoc,
54 p->tlb_policy)),
55 microTLBEnable(p->utlb_enable),
56 mainTLBEnable(p->tlb_enable),
57 slavePortSem(1),
58 microTLBSem(p->utlb_slots),
59 mainTLBSem(p->tlb_slots),
60 microTLBLat(p->utlb_lat),
61 mainTLBLat(p->tlb_lat),
62 slavePort(new SMMUSlavePort(csprintf("%s.slave", name()), *this)),
63 atsSlavePort(name() + ".atsSlave", *this),
64 atsMasterPort(name() + ".atsMaster", *this),
65 portWidth(p->port_width),
66 wrBufSlotsRemaining(p->wrbuf_slots),
67 xlateSlotsRemaining(p->xlate_slots),
68 pendingMemAccesses(0),
69 prefetchEnable(p->prefetch_enable),
70 prefetchReserveLastWay(
71 p->prefetch_reserve_last_way),
72 deviceNeedsRetry(false),
73 atsDeviceNeedsRetry(false),
74 sendDeviceRetryEvent(*this),
75 atsSendDeviceRetryEvent(this)
76 {}
77
78 void
79 SMMUv3SlaveInterface::sendRange()
80 {
81 if (slavePort->isConnected()) {
82 inform("Slave port is connected to %s\n", slavePort->getPeer());
83
84 slavePort->sendRangeChange();
85 } else {
86 fatal("Slave port is not connected.\n");
87 }
88 }
89
90 Port&
91 SMMUv3SlaveInterface::getPort(const std::string &name, PortID id)
92 {
93 if (name == "ats_master") {
94 return atsMasterPort;
95 } else if (name == "slave") {
96 return *slavePort;
97 } else if (name == "ats_slave") {
98 return atsSlavePort;
99 } else {
100 return ClockedObject::getPort(name, id);
101 }
102 }
103
104 void
105 SMMUv3SlaveInterface::schedTimingResp(PacketPtr pkt)
106 {
107 slavePort->schedTimingResp(pkt, nextCycle());
108 }
109
110 void
111 SMMUv3SlaveInterface::schedAtsTimingResp(PacketPtr pkt)
112 {
113 atsSlavePort.schedTimingResp(pkt, nextCycle());
114
115 if (atsDeviceNeedsRetry) {
116 atsDeviceNeedsRetry = false;
117 schedule(atsSendDeviceRetryEvent, nextCycle());
118 }
119 }
120
121 Tick
122 SMMUv3SlaveInterface::recvAtomic(PacketPtr pkt)
123 {
124 DPRINTF(SMMUv3, "[a] req from %s addr=%#x size=%#x\n",
125 slavePort->getPeer(), pkt->getAddr(), pkt->getSize());
126
127 std::string proc_name = csprintf("%s.port", name());
128 SMMUTranslationProcess proc(proc_name, *smmu, *this);
129 proc.beginTransaction(SMMUTranslRequest::fromPacket(pkt));
130
131 SMMUAction a = smmu->runProcessAtomic(&proc, pkt);
132 assert(a.type == ACTION_SEND_RESP);
133
134 return a.delay;
135 }
136
137 bool
138 SMMUv3SlaveInterface::recvTimingReq(PacketPtr pkt)
139 {
140 DPRINTF(SMMUv3, "[t] req from %s addr=%#x size=%#x\n",
141 slavePort->getPeer(), pkt->getAddr(), pkt->getSize());
142
143 // @todo: We need to pay for this and not just zero it out
144 pkt->headerDelay = pkt->payloadDelay = 0;
145
146 unsigned nbeats =
147 (pkt->getSize() + (portWidth-1)) / portWidth;
148
149 if (xlateSlotsRemaining==0 ||
150 (pkt->isWrite() && wrBufSlotsRemaining < nbeats))
151 {
152 deviceNeedsRetry = true;
153 return false;
154 }
155
156 if (pkt->isWrite())
157 wrBufSlotsRemaining -= nbeats;
158
159 std::string proc_name = csprintf("%s.port", name());
160 SMMUTranslationProcess *proc =
161 new SMMUTranslationProcess(proc_name, *smmu, *this);
162 proc->beginTransaction(SMMUTranslRequest::fromPacket(pkt));
163
164 smmu->runProcessTiming(proc, pkt);
165
166 return true;
167 }
168
169 Tick
170 SMMUv3SlaveInterface::atsSlaveRecvAtomic(PacketPtr pkt)
171 {
172 DPRINTF(SMMUv3, "[a] ATS slave req addr=%#x size=%#x\n",
173 pkt->getAddr(), pkt->getSize());
174
175 std::string proc_name = csprintf("%s.atsport", name());
176 const bool ats_request = true;
177 SMMUTranslationProcess proc(
178 proc_name, *smmu, *this);
179 proc.beginTransaction(SMMUTranslRequest::fromPacket(pkt, ats_request));
180
181 SMMUAction a = smmu->runProcessAtomic(&proc, pkt);
182 assert(a.type == ACTION_SEND_RESP_ATS);
183
184 return a.delay;
185 }
186
187 bool
188 SMMUv3SlaveInterface::atsSlaveRecvTimingReq(PacketPtr pkt)
189 {
190 DPRINTF(SMMUv3, "[t] ATS slave req addr=%#x size=%#x\n",
191 pkt->getAddr(), pkt->getSize());
192
193 // @todo: We need to pay for this and not just zero it out
194 pkt->headerDelay = pkt->payloadDelay = 0;
195
196 if (xlateSlotsRemaining == 0) {
197 deviceNeedsRetry = true;
198 return false;
199 }
200
201 std::string proc_name = csprintf("%s.atsport", name());
202 const bool ats_request = true;
203 SMMUTranslationProcess *proc =
204 new SMMUTranslationProcess(proc_name, *smmu, *this);
205 proc->beginTransaction(SMMUTranslRequest::fromPacket(pkt, ats_request));
206
207 smmu->runProcessTiming(proc, pkt);
208
209 return true;
210 }
211
212 bool
213 SMMUv3SlaveInterface::atsMasterRecvTimingResp(PacketPtr pkt)
214 {
215 DPRINTF(SMMUv3, "[t] ATS master resp addr=%#x size=%#x\n",
216 pkt->getAddr(), pkt->getSize());
217
218 // @todo: We need to pay for this and not just zero it out
219 pkt->headerDelay = pkt->payloadDelay = 0;
220
221 SMMUProcess *proc =
222 safe_cast<SMMUProcess *>(pkt->popSenderState());
223
224 smmu->runProcessTiming(proc, pkt);
225
226 return true;
227 }
228
229 void
230 SMMUv3SlaveInterface::sendDeviceRetry()
231 {
232 slavePort->sendRetryReq();
233 }
234
235 void
236 SMMUv3SlaveInterface::atsSendDeviceRetry()
237 {
238 DPRINTF(SMMUv3, "ATS retry\n");
239 atsSlavePort.sendRetryReq();
240 }
241
242 void
243 SMMUv3SlaveInterface::scheduleDeviceRetry()
244 {
245 if (deviceNeedsRetry && !sendDeviceRetryEvent.scheduled()) {
246 DPRINTF(SMMUv3, "sched slave retry\n");
247 deviceNeedsRetry = false;
248 schedule(sendDeviceRetryEvent, nextCycle());
249 }
250 }
251
252 DrainState
253 SMMUv3SlaveInterface::drain()
254 {
255 // Wait until all SMMU translations are completed
256 if (xlateSlotsRemaining < params()->xlate_slots) {
257 return DrainState::Draining;
258 }
259 return DrainState::Drained;
260 }
261
262 SMMUv3SlaveInterface*
263 SMMUv3SlaveInterfaceParams::create()
264 {
265 return new SMMUv3SlaveInterface(this);
266 }