2 * Copyright (c) 2009-2012 Mark D. Hill and David A. Wood
3 * Copyright (c) 2010-2012 Advanced Micro Devices, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 machine(L1Cache, "MI Example L1 Cache")
31 : Sequencer * sequencer;
32 CacheMemory * cacheMemory;
33 Cycles cache_response_latency := 12;
34 Cycles issue_latency := 2;
38 MessageBuffer * requestFromCache, network="To", virtual_network="2",
39 ordered="true", vnet_type="request";
40 MessageBuffer * responseFromCache, network="To", virtual_network="4",
41 ordered="true", vnet_type="response";
43 MessageBuffer * forwardToCache, network="From", virtual_network="3",
44 ordered="true", vnet_type="forward";
45 MessageBuffer * responseToCache, network="From", virtual_network="4",
46 ordered="true", vnet_type="response";
49 state_declaration(State, desc="Cache states") {
50 I, AccessPermission:Invalid, desc="Not Present/Invalid";
51 II, AccessPermission:Busy, desc="Not Present/Invalid, issued PUT";
52 M, AccessPermission:Read_Write, desc="Modified";
53 MI, AccessPermission:Busy, desc="Modified, issued PUT";
54 MII, AccessPermission:Busy, desc="Modified, issued PUTX, received nack";
56 IS, AccessPermission:Busy, desc="Issued request for LOAD/IFETCH";
57 IM, AccessPermission:Busy, desc="Issued request for STORE/ATOMIC";
61 enumeration(Event, desc="Cache events") {
64 Load, desc="Load request from processor";
65 Ifetch, desc="Ifetch request from processor";
66 Store, desc="Store request from processor";
68 Data, desc="Data from network";
69 Fwd_GETX, desc="Forward from network";
71 Inv, desc="Invalidate request from dir";
73 Replacement, desc="Replace a block";
74 Writeback_Ack, desc="Ack from the directory for a writeback";
75 Writeback_Nack, desc="Nack from the directory for a writeback";
78 // STRUCTURE DEFINITIONS
80 MessageBuffer mandatoryQueue, ordered="false";
83 structure(Entry, desc="...", interface="AbstractCacheEntry") {
84 State CacheState, desc="cache state";
85 bool Dirty, desc="Is the data dirty (different than memory)?";
86 DataBlock DataBlk, desc="Data in the block";
90 structure(TBE, desc="...") {
91 State TBEState, desc="Transient state";
92 DataBlock DataBlk, desc="data for the block, required for concurrent writebacks";
95 structure(TBETable, external="yes") {
97 void allocate(Address);
98 void deallocate(Address);
99 bool isPresent(Address);
104 TBETable TBEs, template="<L1Cache_TBE>", constructor="m_number_of_TBEs";
107 void set_cache_entry(AbstractCacheEntry a);
108 void unset_cache_entry();
111 void profileMsgDelay(int virtualNetworkType, Cycles b);
113 Entry getCacheEntry(Address address), return_by_pointer="yes" {
114 return static_cast(Entry, "pointer", cacheMemory.lookup(address));
118 Event mandatory_request_type_to_event(RubyRequestType type) {
119 if (type == RubyRequestType:LD) {
121 } else if (type == RubyRequestType:IFETCH) {
123 } else if ((type == RubyRequestType:ST) || (type == RubyRequestType:ATOMIC)) {
126 error("Invalid RubyRequestType");
130 State getState(TBE tbe, Entry cache_entry, Address addr) {
135 else if (is_valid(cache_entry)) {
136 return cache_entry.CacheState;
143 void setState(TBE tbe, Entry cache_entry, Address addr, State state) {
146 tbe.TBEState := state;
149 if (is_valid(cache_entry)) {
150 cache_entry.CacheState := state;
154 AccessPermission getAccessPermission(Address addr) {
155 TBE tbe := TBEs[addr];
157 return L1Cache_State_to_permission(tbe.TBEState);
160 Entry cache_entry := getCacheEntry(addr);
161 if(is_valid(cache_entry)) {
162 return L1Cache_State_to_permission(cache_entry.CacheState);
165 return AccessPermission:NotPresent;
168 void setAccessPermission(Entry cache_entry, Address addr, State state) {
169 if (is_valid(cache_entry)) {
170 cache_entry.changePermission(L1Cache_State_to_permission(state));
174 DataBlock getDataBlock(Address addr), return_by_ref="yes" {
175 TBE tbe := TBEs[addr];
180 return getCacheEntry(addr).DataBlk;
185 out_port(requestNetwork_out, RequestMsg, requestFromCache);
186 out_port(responseNetwork_out, ResponseMsg, responseFromCache);
188 in_port(forwardRequestNetwork_in, RequestMsg, forwardToCache) {
189 if (forwardRequestNetwork_in.isReady()) {
190 peek(forwardRequestNetwork_in, RequestMsg, block_on="Addr") {
192 Entry cache_entry := getCacheEntry(in_msg.Addr);
193 TBE tbe := TBEs[in_msg.Addr];
195 if (in_msg.Type == CoherenceRequestType:GETX) {
196 trigger(Event:Fwd_GETX, in_msg.Addr, cache_entry, tbe);
198 else if (in_msg.Type == CoherenceRequestType:WB_ACK) {
199 trigger(Event:Writeback_Ack, in_msg.Addr, cache_entry, tbe);
201 else if (in_msg.Type == CoherenceRequestType:WB_NACK) {
202 trigger(Event:Writeback_Nack, in_msg.Addr, cache_entry, tbe);
204 else if (in_msg.Type == CoherenceRequestType:INV) {
205 trigger(Event:Inv, in_msg.Addr, cache_entry, tbe);
208 error("Unexpected message");
214 in_port(responseNetwork_in, ResponseMsg, responseToCache) {
215 if (responseNetwork_in.isReady()) {
216 peek(responseNetwork_in, ResponseMsg, block_on="Addr") {
218 Entry cache_entry := getCacheEntry(in_msg.Addr);
219 TBE tbe := TBEs[in_msg.Addr];
221 if (in_msg.Type == CoherenceResponseType:DATA) {
222 trigger(Event:Data, in_msg.Addr, cache_entry, tbe);
225 error("Unexpected message");
232 in_port(mandatoryQueue_in, RubyRequest, mandatoryQueue, desc="...") {
233 if (mandatoryQueue_in.isReady()) {
234 peek(mandatoryQueue_in, RubyRequest, block_on="LineAddress") {
236 Entry cache_entry := getCacheEntry(in_msg.LineAddress);
237 if (is_invalid(cache_entry) &&
238 cacheMemory.cacheAvail(in_msg.LineAddress) == false ) {
239 // make room for the block
240 trigger(Event:Replacement, cacheMemory.cacheProbe(in_msg.LineAddress),
241 getCacheEntry(cacheMemory.cacheProbe(in_msg.LineAddress)),
242 TBEs[cacheMemory.cacheProbe(in_msg.LineAddress)]);
245 trigger(mandatory_request_type_to_event(in_msg.Type), in_msg.LineAddress,
246 cache_entry, TBEs[in_msg.LineAddress]);
254 action(a_issueRequest, "a", desc="Issue a request") {
255 enqueue(requestNetwork_out, RequestMsg, issue_latency) {
256 out_msg.Addr := address;
257 out_msg.Type := CoherenceRequestType:GETX;
258 out_msg.Requestor := machineID;
259 out_msg.Destination.add(map_Address_to_Directory(address));
260 out_msg.MessageSize := MessageSizeType:Control;
264 action(b_issuePUT, "b", desc="Issue a PUT request") {
265 enqueue(requestNetwork_out, RequestMsg, issue_latency) {
266 assert(is_valid(cache_entry));
267 out_msg.Addr := address;
268 out_msg.Type := CoherenceRequestType:PUTX;
269 out_msg.Requestor := machineID;
270 out_msg.Destination.add(map_Address_to_Directory(address));
271 out_msg.DataBlk := cache_entry.DataBlk;
272 out_msg.MessageSize := MessageSizeType:Data;
276 action(e_sendData, "e", desc="Send data from cache to requestor") {
277 peek(forwardRequestNetwork_in, RequestMsg) {
278 enqueue(responseNetwork_out, ResponseMsg, cache_response_latency) {
279 assert(is_valid(cache_entry));
280 out_msg.Addr := address;
281 out_msg.Type := CoherenceResponseType:DATA;
282 out_msg.Sender := machineID;
283 out_msg.Destination.add(in_msg.Requestor);
284 out_msg.DataBlk := cache_entry.DataBlk;
285 out_msg.MessageSize := MessageSizeType:Response_Data;
290 action(ee_sendDataFromTBE, "\e", desc="Send data from TBE to requestor") {
291 peek(forwardRequestNetwork_in, RequestMsg) {
292 enqueue(responseNetwork_out, ResponseMsg, cache_response_latency) {
293 assert(is_valid(tbe));
294 out_msg.Addr := address;
295 out_msg.Type := CoherenceResponseType:DATA;
296 out_msg.Sender := machineID;
297 out_msg.Destination.add(in_msg.Requestor);
298 out_msg.DataBlk := tbe.DataBlk;
299 out_msg.MessageSize := MessageSizeType:Response_Data;
304 action(i_allocateL1CacheBlock, "i", desc="Allocate a cache block") {
305 if (is_valid(cache_entry)) {
307 set_cache_entry(cacheMemory.allocate(address, new Entry));
311 action(h_deallocateL1CacheBlock, "h", desc="deallocate a cache block") {
312 if (is_valid(cache_entry)) {
313 cacheMemory.deallocate(address);
318 action(m_popMandatoryQueue, "m", desc="Pop the mandatory request queue") {
319 mandatoryQueue_in.dequeue();
322 action(n_popResponseQueue, "n", desc="Pop the response queue") {
323 profileMsgDelay(1, responseNetwork_in.dequeue());
326 action(o_popForwardedRequestQueue, "o", desc="Pop the forwarded request queue") {
327 profileMsgDelay(2, forwardRequestNetwork_in.dequeue());
330 action(p_profileMiss, "pi", desc="Profile cache miss") {
331 ++cacheMemory.demand_misses;
334 action(p_profileHit, "ph", desc="Profile cache miss") {
335 ++cacheMemory.demand_hits;
338 action(r_load_hit, "r", desc="Notify sequencer the load completed.") {
339 assert(is_valid(cache_entry));
340 DPRINTF(RubySlicc,"%s\n", cache_entry.DataBlk);
341 sequencer.readCallback(address, cache_entry.DataBlk, false);
344 action(rx_load_hit, "rx", desc="External load completed.") {
345 peek(responseNetwork_in, ResponseMsg) {
346 assert(is_valid(cache_entry));
347 DPRINTF(RubySlicc,"%s\n", cache_entry.DataBlk);
348 sequencer.readCallback(address, cache_entry.DataBlk, true,
349 machineIDToMachineType(in_msg.Sender));
353 action(s_store_hit, "s", desc="Notify sequencer that store completed.") {
354 assert(is_valid(cache_entry));
355 DPRINTF(RubySlicc,"%s\n", cache_entry.DataBlk);
356 sequencer.writeCallback(address, cache_entry.DataBlk, false);
359 action(sx_store_hit, "sx", desc="External store completed.") {
360 peek(responseNetwork_in, ResponseMsg) {
361 assert(is_valid(cache_entry));
362 DPRINTF(RubySlicc,"%s\n", cache_entry.DataBlk);
363 sequencer.writeCallback(address, cache_entry.DataBlk, true,
364 machineIDToMachineType(in_msg.Sender));
368 action(u_writeDataToCache, "u", desc="Write data to the cache") {
369 peek(responseNetwork_in, ResponseMsg) {
370 assert(is_valid(cache_entry));
371 cache_entry.DataBlk := in_msg.DataBlk;
375 action(forward_eviction_to_cpu, "\cc", desc="sends eviction information to the processor") {
376 if (send_evictions) {
377 DPRINTF(RubySlicc, "Sending invalidation for %s to the CPU\n", address);
378 sequencer.evictionCallback(address);
382 action(v_allocateTBE, "v", desc="Allocate TBE") {
383 TBEs.allocate(address);
384 set_tbe(TBEs[address]);
387 action(w_deallocateTBE, "w", desc="Deallocate TBE") {
388 TBEs.deallocate(address);
392 action(x_copyDataFromCacheToTBE, "x", desc="Copy data from cache to TBE") {
393 assert(is_valid(cache_entry));
394 assert(is_valid(tbe));
395 tbe.DataBlk := cache_entry.DataBlk;
398 action(z_stall, "z", desc="stall") {
404 transition({IS, IM, MI, II, MII}, {Load, Ifetch, Store, Replacement}) {
408 transition({IS, IM}, {Fwd_GETX, Inv}) {
412 transition(MI, Inv) {
413 o_popForwardedRequestQueue;
416 transition(M, Store) {
422 transition(M, {Load, Ifetch}) {
429 o_popForwardedRequestQueue;
432 transition(I, Store, IM) {
434 i_allocateL1CacheBlock;
440 transition(I, {Load, Ifetch}, IS) {
442 i_allocateL1CacheBlock;
448 transition(IS, Data, M) {
455 transition(IM, Data, M) {
462 transition(M, Fwd_GETX, I) {
464 forward_eviction_to_cpu;
465 o_popForwardedRequestQueue;
468 transition(I, Replacement) {
469 h_deallocateL1CacheBlock;
472 transition(M, {Replacement,Inv}, MI) {
475 x_copyDataFromCacheToTBE;
476 forward_eviction_to_cpu;
477 h_deallocateL1CacheBlock;
480 transition(MI, Writeback_Ack, I) {
482 o_popForwardedRequestQueue;
485 transition(MI, Fwd_GETX, II) {
487 o_popForwardedRequestQueue;
490 transition(MI, Writeback_Nack, MII) {
491 o_popForwardedRequestQueue;
494 transition(MII, Fwd_GETX, I) {
497 o_popForwardedRequestQueue;
500 transition(II, Writeback_Nack, I) {
502 o_popForwardedRequestQueue;