1
2/*
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
4 * All rights reserved.
5 *
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.
16 *
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.
28 */
29
30#include "mem/ruby/libruby.hh"
31#include "mem/ruby/common/Global.hh"
32#include "mem/ruby/system/Sequencer.hh"
33#include "mem/ruby/system/System.hh"
34#include "mem/protocol/Protocol.hh"
35#include "mem/ruby/profiler/Profiler.hh"
36#include "mem/ruby/system/CacheMemory.hh"
37#include "mem/protocol/CacheMsg.hh"
38#include "mem/ruby/recorder/Tracer.hh"
39#include "mem/ruby/common/SubBlock.hh"
40#include "mem/protocol/Protocol.hh"
41#include "mem/gems_common/Map.hh"
42#include "mem/ruby/buffers/MessageBuffer.hh"
43#include "mem/ruby/slicc_interface/AbstractController.hh"
44#include "cpu/rubytest/RubyTester.hh"
45
46#include "params/RubySequencer.hh"
47
48//Sequencer::Sequencer(int core_id, MessageBuffer* mandatory_q)
49
50#define LLSC_FAIL -2
51long int already = 0;
52
53Sequencer *
54RubySequencerParams::create()
55{
56 return new Sequencer(this);
57}
58
59Sequencer::Sequencer(const Params *p)
60 : RubyPort(p), deadlockCheckEvent(this)
61{
62 m_store_waiting_on_load_cycles = 0;
63 m_store_waiting_on_store_cycles = 0;
64 m_load_waiting_on_store_cycles = 0;
65 m_load_waiting_on_load_cycles = 0;
66
67 m_outstanding_count = 0;
68
69 m_max_outstanding_requests = 0;
70 m_deadlock_threshold = 0;
71 m_instCache_ptr = NULL;
72 m_dataCache_ptr = NULL;
73
74 m_instCache_ptr = p->icache;
75 m_dataCache_ptr = p->dcache;
76 m_max_outstanding_requests = p->max_outstanding_requests;
77 m_deadlock_threshold = p->deadlock_threshold;
77
78 m_usingRubyTester = p->using_ruby_tester;
79
80 assert(m_max_outstanding_requests > 0);
81 assert(m_deadlock_threshold > 0);
82 assert(m_instCache_ptr != NULL);
83 assert(m_dataCache_ptr != NULL);
84}
85
86Sequencer::~Sequencer() {
87
88}
89
90void Sequencer::wakeup() {
91 // Check for deadlock of any of the requests
92 Time current_time = g_eventQueue_ptr->getTime();
93
94 // Check across all outstanding requests
95 int total_outstanding = 0;
96
97 Vector<Address> keys = m_readRequestTable.keys();
98 for (int i=0; i<keys.size(); i++) {
99 SequencerRequest* request = m_readRequestTable.lookup(keys[i]);
100 if (current_time - request->issue_time >= m_deadlock_threshold) {
101 WARN_MSG("Possible Deadlock detected");
102 WARN_EXPR(request);
103 WARN_EXPR(m_version);
104 WARN_EXPR(request->ruby_request.paddr);
105 WARN_EXPR(keys.size());
106 WARN_EXPR(current_time);
107 WARN_EXPR(request->issue_time);
108 WARN_EXPR(current_time - request->issue_time);
109 ERROR_MSG("Aborting");
110 }
111 }
112
113 keys = m_writeRequestTable.keys();
114 for (int i=0; i<keys.size(); i++) {
115 SequencerRequest* request = m_writeRequestTable.lookup(keys[i]);
116 if (current_time - request->issue_time >= m_deadlock_threshold) {
117 WARN_MSG("Possible Deadlock detected");
118 WARN_EXPR(request);
119 WARN_EXPR(m_version);
120 WARN_EXPR(current_time);
121 WARN_EXPR(request->issue_time);
122 WARN_EXPR(current_time - request->issue_time);
123 WARN_EXPR(keys.size());
124 ERROR_MSG("Aborting");
125 }
126 }
127 total_outstanding += m_writeRequestTable.size() + m_readRequestTable.size();
128
129 assert(m_outstanding_count == total_outstanding);
130
131 if (m_outstanding_count > 0) { // If there are still outstanding requests, keep checking
132 schedule(deadlockCheckEvent,
133 (m_deadlock_threshold * g_eventQueue_ptr->getClock()) + curTick);
134 }
135}
136
137void Sequencer::printStats(ostream & out) const {
138 out << "Sequencer: " << m_name << endl;
139 out << " store_waiting_on_load_cycles: " << m_store_waiting_on_load_cycles << endl;
140 out << " store_waiting_on_store_cycles: " << m_store_waiting_on_store_cycles << endl;
141 out << " load_waiting_on_load_cycles: " << m_load_waiting_on_load_cycles << endl;
142 out << " load_waiting_on_store_cycles: " << m_load_waiting_on_store_cycles << endl;
143}
144
145void Sequencer::printProgress(ostream& out) const{
146 /*
147 int total_demand = 0;
148 out << "Sequencer Stats Version " << m_version << endl;
149 out << "Current time = " << g_eventQueue_ptr->getTime() << endl;
150 out << "---------------" << endl;
151 out << "outstanding requests" << endl;
152
153 Vector<Address> rkeys = m_readRequestTable.keys();
154 int read_size = rkeys.size();
155 out << "proc " << m_version << " Read Requests = " << read_size << endl;
156 // print the request table
157 for(int i=0; i < read_size; ++i){
158 SequencerRequest * request = m_readRequestTable.lookup(rkeys[i]);
159 out << "\tRequest[ " << i << " ] = " << request->type << " Address " << rkeys[i] << " Posted " << request->issue_time << " PF " << PrefetchBit_No << endl;
160 total_demand++;
161 }
162
163 Vector<Address> wkeys = m_writeRequestTable.keys();
164 int write_size = wkeys.size();
165 out << "proc " << m_version << " Write Requests = " << write_size << endl;
166 // print the request table
167 for(int i=0; i < write_size; ++i){
168 CacheMsg & request = m_writeRequestTable.lookup(wkeys[i]);
169 out << "\tRequest[ " << i << " ] = " << request.getType() << " Address " << wkeys[i] << " Posted " << request.getTime() << " PF " << request.getPrefetch() << endl;
170 if( request.getPrefetch() == PrefetchBit_No ){
171 total_demand++;
172 }
173 }
174
175 out << endl;
176
177 out << "Total Number Outstanding: " << m_outstanding_count << endl;
178 out << "Total Number Demand : " << total_demand << endl;
179 out << "Total Number Prefetches : " << m_outstanding_count - total_demand << endl;
180 out << endl;
181 out << endl;
182 */
183}
184
185void Sequencer::printConfig(ostream& out) const {
186 out << "Seqeuncer config: " << m_name << endl;
187 out << " controller: " << m_controller->getName() << endl;
188 out << " version: " << m_version << endl;
189 out << " max_outstanding_requests: " << m_max_outstanding_requests << endl;
190 out << " deadlock_threshold: " << m_deadlock_threshold << endl;
191}
192
193// Insert the request on the correct request table. Return true if
194// the entry was already present.
195bool Sequencer::insertRequest(SequencerRequest* request) {
196 int total_outstanding = m_writeRequestTable.size() + m_readRequestTable.size();
197
198 assert(m_outstanding_count == total_outstanding);
199
200 // See if we should schedule a deadlock check
201 if (deadlockCheckEvent.scheduled() == false) {
202 schedule(deadlockCheckEvent, m_deadlock_threshold + curTick);
203 }
204
205 Address line_addr(request->ruby_request.paddr);
206 line_addr.makeLineAddress();
207 if ((request->ruby_request.type == RubyRequestType_ST) ||
208 (request->ruby_request.type == RubyRequestType_RMW_Read) ||
209 (request->ruby_request.type == RubyRequestType_RMW_Write) ||
210 (request->ruby_request.type == RubyRequestType_Locked_Read) ||
211 (request->ruby_request.type == RubyRequestType_Locked_Write)) {
212 if (m_writeRequestTable.exist(line_addr)) {
213 m_writeRequestTable.lookup(line_addr) = request;
214 // return true;
215 assert(0); // drh5: isn't this an error? do you lose the initial request?
216 }
217 m_writeRequestTable.allocate(line_addr);
218 m_writeRequestTable.lookup(line_addr) = request;
219 m_outstanding_count++;
220 } else {
221 if (m_readRequestTable.exist(line_addr)) {
222 m_readRequestTable.lookup(line_addr) = request;
223 // return true;
224 assert(0); // drh5: isn't this an error? do you lose the initial request?
225 }
226 m_readRequestTable.allocate(line_addr);
227 m_readRequestTable.lookup(line_addr) = request;
228 m_outstanding_count++;
229 }
230
231 g_system_ptr->getProfiler()->sequencerRequests(m_outstanding_count);
232
233 total_outstanding = m_writeRequestTable.size() + m_readRequestTable.size();
234 assert(m_outstanding_count == total_outstanding);
235
236 return false;
237}
238
239void Sequencer::removeRequest(SequencerRequest* srequest) {
240
241 assert(m_outstanding_count == m_writeRequestTable.size() + m_readRequestTable.size());
242
243 const RubyRequest & ruby_request = srequest->ruby_request;
244 Address line_addr(ruby_request.paddr);
245 line_addr.makeLineAddress();
246 if ((ruby_request.type == RubyRequestType_ST) ||
247 (ruby_request.type == RubyRequestType_RMW_Read) ||
248 (ruby_request.type == RubyRequestType_RMW_Write) ||
249 (ruby_request.type == RubyRequestType_Locked_Read) ||
250 (ruby_request.type == RubyRequestType_Locked_Write)) {
251 m_writeRequestTable.deallocate(line_addr);
252 } else {
253 m_readRequestTable.deallocate(line_addr);
254 }
255 m_outstanding_count--;
256
257 assert(m_outstanding_count == m_writeRequestTable.size() + m_readRequestTable.size());
258}
259
260void Sequencer::writeCallback(const Address& address, DataBlock& data) {
261
262 assert(address == line_address(address));
263 assert(m_writeRequestTable.exist(line_address(address)));
264
265 SequencerRequest* request = m_writeRequestTable.lookup(address);
266
267 removeRequest(request);
268
269 assert((request->ruby_request.type == RubyRequestType_ST) ||
270 (request->ruby_request.type == RubyRequestType_RMW_Read) ||
271 (request->ruby_request.type == RubyRequestType_RMW_Write) ||
272 (request->ruby_request.type == RubyRequestType_Locked_Read) ||
273 (request->ruby_request.type == RubyRequestType_Locked_Write));
274
275 if (request->ruby_request.type == RubyRequestType_Locked_Read) {
276 m_dataCache_ptr->setLocked(address, m_version);
277 }
278 else if (request->ruby_request.type == RubyRequestType_RMW_Read) {
279 m_controller->blockOnQueue(address, m_mandatory_q_ptr);
280 }
281 else if (request->ruby_request.type == RubyRequestType_RMW_Write) {
282 m_controller->unblock(address);
283 }
284
285 hitCallback(request, data);
286}
287
288void Sequencer::readCallback(const Address& address, DataBlock& data) {
289
290 assert(address == line_address(address));
291 assert(m_readRequestTable.exist(line_address(address)));
292
293 SequencerRequest* request = m_readRequestTable.lookup(address);
294 removeRequest(request);
295
296 assert((request->ruby_request.type == RubyRequestType_LD) ||
297 (request->ruby_request.type == RubyRequestType_RMW_Read) ||
298 (request->ruby_request.type == RubyRequestType_IFETCH));
299
300 hitCallback(request, data);
301}
302
303void Sequencer::hitCallback(SequencerRequest* srequest, DataBlock& data) {
304 const RubyRequest & ruby_request = srequest->ruby_request;
305 Address request_address(ruby_request.paddr);
306 Address request_line_address(ruby_request.paddr);
307 request_line_address.makeLineAddress();
308 RubyRequestType type = ruby_request.type;
309 Time issued_time = srequest->issue_time;
310
311 // Set this cache entry to the most recently used
312 if (type == RubyRequestType_IFETCH) {
313 if (m_instCache_ptr->isTagPresent(request_line_address) )
314 m_instCache_ptr->setMRU(request_line_address);
315 } else {
316 if (m_dataCache_ptr->isTagPresent(request_line_address) )
317 m_dataCache_ptr->setMRU(request_line_address);
318 }
319
320 assert(g_eventQueue_ptr->getTime() >= issued_time);
321 Time miss_latency = g_eventQueue_ptr->getTime() - issued_time;
322
323 // Profile the miss latency for all non-zero demand misses
324 if (miss_latency != 0) {
325 g_system_ptr->getProfiler()->missLatency(miss_latency, type);
326
327 if (Debug::getProtocolTrace()) {
328 g_system_ptr->getProfiler()->profileTransition("Seq", m_version, Address(ruby_request.paddr),
329 "", "Done", "", int_to_string(miss_latency)+" cycles");
330 }
331 }
332 /*
333 if (request.getPrefetch() == PrefetchBit_Yes) {
334 return; // Ignore the prefetch
335 }
336 */
337
338 // update the data
339 if (ruby_request.data != NULL) {
340 if ((type == RubyRequestType_LD) ||
341 (type == RubyRequestType_IFETCH) ||
342 (type == RubyRequestType_RMW_Read)) {
343 memcpy(ruby_request.data, data.getData(request_address.getOffset(), ruby_request.len), ruby_request.len);
344 } else {
345 data.setData(ruby_request.data, request_address.getOffset(), ruby_request.len);
346 }
347 }
348
349 //
350 // If using the RubyTester, update the RubyTester sender state's subBlock
351 // with the recieved data. The tester will later access this state.
352 // Note: RubyPort will access it's sender state before the RubyTester.
353 //
354 if (m_usingRubyTester) {
355 //
356 // Since the hit callback func only takes a request id, we must iterate
357 // through the requests and update the packet's subBlock here.
358 // All this would be fixed if we could attach a M5 pkt pointer to the
359 // ruby request, however that change will break the libruby interface so
360 // we'll hold off on that for now.
361 //
362 RequestMap::iterator i = pending_cpu_requests.find(srequest->id);
363 if (i == pending_cpu_requests.end())
364 panic("could not find pending request %d\n", srequest->id);
365 RequestCookie *cookie = i->second;
366 Packet *pkt = cookie->pkt;
367
368 RubyTester::SenderState* testerSenderState;
369 testerSenderState = safe_cast<RubyTester::SenderState*>(pkt->senderState);
370 testerSenderState->subBlock->mergeFrom(data);
371 }
372
373 m_hit_callback(srequest->id);
374 delete srequest;
375}
376
377// Returns true if the sequencer already has a load or store outstanding
378int Sequencer::isReady(const RubyRequest& request) {
379 bool is_outstanding_store = m_writeRequestTable.exist(line_address(Address(request.paddr)));
380 bool is_outstanding_load = m_readRequestTable.exist(line_address(Address(request.paddr)));
381 if ( is_outstanding_store ) {
382 if ((request.type == RubyRequestType_LD) ||
383 (request.type == RubyRequestType_IFETCH) ||
384 (request.type == RubyRequestType_RMW_Read)) {
385 m_store_waiting_on_load_cycles++;
386 } else {
387 m_store_waiting_on_store_cycles++;
388 }
389 return LIBRUBY_ALIASED_REQUEST;
390 } else if ( is_outstanding_load ) {
391 if ((request.type == RubyRequestType_ST) ||
392 (request.type == RubyRequestType_RMW_Write) ) {
393 m_load_waiting_on_store_cycles++;
394 } else {
395 m_load_waiting_on_load_cycles++;
396 }
397 return LIBRUBY_ALIASED_REQUEST;
398 }
399
400 if (m_outstanding_count >= m_max_outstanding_requests) {
401 return LIBRUBY_BUFFER_FULL;
402 }
403
404 return 1;
405}
406
407bool Sequencer::empty() const {
408 return (m_writeRequestTable.size() == 0) && (m_readRequestTable.size() == 0);
409}
410
411
412int64_t Sequencer::makeRequest(const RubyRequest & request)
413{
414 assert(Address(request.paddr).getOffset() + request.len <= RubySystem::getBlockSizeBytes());
415 int ready = isReady(request);
416 if (ready > 0) {
417 int64_t id = makeUniqueRequestID();
418 SequencerRequest *srequest = new SequencerRequest(request, id, g_eventQueue_ptr->getTime());
419 bool found = insertRequest(srequest);
420 if (!found) {
421 if (request.type == RubyRequestType_Locked_Write) {
422 // NOTE: it is OK to check the locked flag here as the mandatory queue will be checked first
423 // ensuring that nothing comes between checking the flag and servicing the store
424 if (!m_dataCache_ptr->isLocked(line_address(Address(request.paddr)), m_version)) {
425 return LLSC_FAIL;
426 }
427 else {
428 m_dataCache_ptr->clearLocked(line_address(Address(request.paddr)));
429 }
430 }
431 issueRequest(request);
432
433 // TODO: issue hardware prefetches here
434 return id;
435 }
436 else {
437 assert(0);
438 return 0;
439 }
440 } else {
441 return ready;
442 }
443}
444
445void Sequencer::issueRequest(const RubyRequest& request) {
446
447 // TODO: get rid of CacheMsg, CacheRequestType, and AccessModeTYpe, & have SLICC use RubyRequest and subtypes natively
448 CacheRequestType ctype;
449 switch(request.type) {
450 case RubyRequestType_IFETCH:
451 ctype = CacheRequestType_IFETCH;
452 break;
453 case RubyRequestType_LD:
454 ctype = CacheRequestType_LD;
455 break;
456 case RubyRequestType_ST:
457 ctype = CacheRequestType_ST;
458 break;
459 case RubyRequestType_Locked_Read:
460 case RubyRequestType_Locked_Write:
461 ctype = CacheRequestType_ATOMIC;
462 break;
463 case RubyRequestType_RMW_Read:
464 ctype = CacheRequestType_ATOMIC;
465 break;
466 case RubyRequestType_RMW_Write:
467 ctype = CacheRequestType_ATOMIC;
468 break;
469 default:
470 assert(0);
471 }
472 AccessModeType amtype;
473 switch(request.access_mode){
474 case RubyAccessMode_User:
475 amtype = AccessModeType_UserMode;
476 break;
477 case RubyAccessMode_Supervisor:
478 amtype = AccessModeType_SupervisorMode;
479 break;
480 case RubyAccessMode_Device:
481 amtype = AccessModeType_UserMode;
482 break;
483 default:
484 assert(0);
485 }
486 Address line_addr(request.paddr);
487 line_addr.makeLineAddress();
488 CacheMsg msg(line_addr, Address(request.paddr), ctype, Address(request.pc), amtype, request.len, PrefetchBit_No, request.proc_id);
489
490 if (Debug::getProtocolTrace()) {
491 g_system_ptr->getProfiler()->profileTransition("Seq", m_version, Address(request.paddr),
492 "", "Begin", "", RubyRequestType_to_string(request.type));
493 }
494
495 if (g_system_ptr->getTracer()->traceEnabled()) {
496 g_system_ptr->getTracer()->traceRequest(this, line_addr, Address(request.pc),
497 request.type, g_eventQueue_ptr->getTime());
498 }
499
500 Time latency = 0; // initialzed to an null value
501
502 if (request.type == RubyRequestType_IFETCH)
503 latency = m_instCache_ptr->getLatency();
504 else
505 latency = m_dataCache_ptr->getLatency();
506
507 // Send the message to the cache controller
508 assert(latency > 0);
509
510 assert(m_mandatory_q_ptr != NULL);
511 m_mandatory_q_ptr->enqueue(msg, latency);
512}
513/*
514bool Sequencer::tryCacheAccess(const Address& addr, CacheRequestType type,
515 AccessModeType access_mode,
516 int size, DataBlock*& data_ptr) {
517 if (type == CacheRequestType_IFETCH) {
518 return m_instCache_ptr->tryCacheAccess(line_address(addr), type, data_ptr);
519 } else {
520 return m_dataCache_ptr->tryCacheAccess(line_address(addr), type, data_ptr);
521 }
522}
523*/
524
525void Sequencer::print(ostream& out) const {
526 out << "[Sequencer: " << m_version
527 << ", outstanding requests: " << m_outstanding_count;
528
529 out << ", read request table: " << m_readRequestTable
530 << ", write request table: " << m_writeRequestTable;
531 out << "]";
532}
533
534// this can be called from setState whenever coherence permissions are upgraded
535// when invoked, coherence violations will be checked for the given block
536void Sequencer::checkCoherence(const Address& addr) {
537#ifdef CHECK_COHERENCE
538 g_system_ptr->checkGlobalCoherenceInvariant(addr);
539#endif
540}
541