tsunami_cchip.cc revision 8232:b28d06a175be
1/*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ali Saidi
29 *          Ron Dreslinski
30 */
31
32/** @file
33 * Emulation of the Tsunami CChip CSRs
34 */
35
36#include <deque>
37#include <string>
38#include <vector>
39
40#include "arch/alpha/ev5.hh"
41#include "base/trace.hh"
42#include "config/the_isa.hh"
43#include "cpu/intr_control.hh"
44#include "cpu/thread_context.hh"
45#include "debug/IPI.hh"
46#include "debug/Tsunami.hh"
47#include "dev/alpha/tsunami.hh"
48#include "dev/alpha/tsunami_cchip.hh"
49#include "dev/alpha/tsunamireg.h"
50#include "mem/packet.hh"
51#include "mem/packet_access.hh"
52#include "mem/port.hh"
53#include "params/TsunamiCChip.hh"
54#include "sim/system.hh"
55
56using namespace std;
57//Should this be AlphaISA?
58using namespace TheISA;
59
60TsunamiCChip::TsunamiCChip(const Params *p)
61    : BasicPioDevice(p), tsunami(p->tsunami)
62{
63    pioSize = 0x10000000;
64
65    drir = 0;
66    ipint = 0;
67    itint = 0;
68
69    for (int x = 0; x < Tsunami::Max_CPUs; x++)
70    {
71        dim[x] = 0;
72        dir[x] = 0;
73    }
74
75    //Put back pointer in tsunami
76    tsunami->cchip = this;
77}
78
79Tick
80TsunamiCChip::read(PacketPtr pkt)
81{
82    DPRINTF(Tsunami, "read  va=%#x size=%d\n", pkt->getAddr(), pkt->getSize());
83
84    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
85
86    Addr regnum = (pkt->getAddr() - pioAddr) >> 6;
87    Addr daddr = (pkt->getAddr() - pioAddr);
88
89    pkt->allocate();
90    switch (pkt->getSize()) {
91
92      case sizeof(uint64_t):
93          pkt->set<uint64_t>(0);
94
95          if (daddr & TSDEV_CC_BDIMS)
96          {
97              pkt->set(dim[(daddr >> 4) & 0x3F]);
98              break;
99          }
100
101          if (daddr & TSDEV_CC_BDIRS)
102          {
103              pkt->set(dir[(daddr >> 4) & 0x3F]);
104              break;
105          }
106
107          switch(regnum) {
108              case TSDEV_CC_CSR:
109                  pkt->set(0x0);
110                  break;
111              case TSDEV_CC_MTR:
112                  panic("TSDEV_CC_MTR not implemeted\n");
113                   break;
114              case TSDEV_CC_MISC:
115                  pkt->set(((ipint << 8) & 0xF) | ((itint << 4) & 0xF) |
116                                     (pkt->req->contextId() & 0x3));
117                  // currently, FS cannot handle MT so contextId and
118                  // cpuId are effectively the same, don't know if it will
119                  // matter if FS becomes MT enabled.  I suspect no because
120                  // we are currently able to boot up to 64 procs anyway
121                  // which would render the CPUID of this register useless
122                  // anyway
123                  break;
124              case TSDEV_CC_AAR0:
125              case TSDEV_CC_AAR1:
126              case TSDEV_CC_AAR2:
127              case TSDEV_CC_AAR3:
128                  pkt->set(0);
129                  break;
130              case TSDEV_CC_DIM0:
131                  pkt->set(dim[0]);
132                  break;
133              case TSDEV_CC_DIM1:
134                  pkt->set(dim[1]);
135                  break;
136              case TSDEV_CC_DIM2:
137                  pkt->set(dim[2]);
138                  break;
139              case TSDEV_CC_DIM3:
140                  pkt->set(dim[3]);
141                  break;
142              case TSDEV_CC_DIR0:
143                  pkt->set(dir[0]);
144                  break;
145              case TSDEV_CC_DIR1:
146                  pkt->set(dir[1]);
147                  break;
148              case TSDEV_CC_DIR2:
149                  pkt->set(dir[2]);
150                  break;
151              case TSDEV_CC_DIR3:
152                  pkt->set(dir[3]);
153                  break;
154              case TSDEV_CC_DRIR:
155                  pkt->set(drir);
156                  break;
157              case TSDEV_CC_PRBEN:
158                  panic("TSDEV_CC_PRBEN not implemented\n");
159                  break;
160              case TSDEV_CC_IIC0:
161              case TSDEV_CC_IIC1:
162              case TSDEV_CC_IIC2:
163              case TSDEV_CC_IIC3:
164                  panic("TSDEV_CC_IICx not implemented\n");
165                  break;
166              case TSDEV_CC_MPR0:
167              case TSDEV_CC_MPR1:
168              case TSDEV_CC_MPR2:
169              case TSDEV_CC_MPR3:
170                  panic("TSDEV_CC_MPRx not implemented\n");
171                  break;
172              case TSDEV_CC_IPIR:
173                  pkt->set(ipint);
174                  break;
175              case TSDEV_CC_ITIR:
176                  pkt->set(itint);
177                  break;
178              default:
179                  panic("default in cchip read reached, accessing 0x%x\n");
180           } // uint64_t
181
182      break;
183      case sizeof(uint32_t):
184      case sizeof(uint16_t):
185      case sizeof(uint8_t):
186      default:
187        panic("invalid access size(?) for tsunami register!\n");
188    }
189    DPRINTF(Tsunami, "Tsunami CChip: read  regnum=%#x size=%d data=%lld\n",
190            regnum, pkt->getSize(), pkt->get<uint64_t>());
191
192    pkt->makeAtomicResponse();
193    return pioDelay;
194}
195
196Tick
197TsunamiCChip::write(PacketPtr pkt)
198{
199    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
200    Addr daddr = pkt->getAddr() - pioAddr;
201    Addr regnum = (pkt->getAddr() - pioAddr) >> 6 ;
202
203
204    assert(pkt->getSize() == sizeof(uint64_t));
205
206    DPRINTF(Tsunami, "write - addr=%#x value=%#x\n", pkt->getAddr(), pkt->get<uint64_t>());
207
208    bool supportedWrite = false;
209
210
211    if (daddr & TSDEV_CC_BDIMS)
212    {
213        int number = (daddr >> 4) & 0x3F;
214
215        uint64_t bitvector;
216        uint64_t olddim;
217        uint64_t olddir;
218
219        olddim = dim[number];
220        olddir = dir[number];
221        dim[number] = pkt->get<uint64_t>();
222        dir[number] = dim[number] & drir;
223        for(int x = 0; x < Tsunami::Max_CPUs; x++)
224        {
225            bitvector = ULL(1) << x;
226            // Figure out which bits have changed
227            if ((dim[number] & bitvector) != (olddim & bitvector))
228            {
229                // The bit is now set and it wasn't before (set)
230                if((dim[number] & bitvector) && (dir[number] & bitvector))
231                {
232                    tsunami->intrctrl->post(number, TheISA::INTLEVEL_IRQ1, x);
233                    DPRINTF(Tsunami, "dim write resulting in posting dir"
234                            " interrupt to cpu %d\n", number);
235                }
236                else if ((olddir & bitvector) &&
237                        !(dir[number] & bitvector))
238                {
239                    // The bit was set and now its now clear and
240                    // we were interrupting on that bit before
241                    tsunami->intrctrl->clear(number, TheISA::INTLEVEL_IRQ1, x);
242                    DPRINTF(Tsunami, "dim write resulting in clear"
243                            " dir interrupt to cpu %d\n", number);
244
245                }
246
247
248            }
249        }
250    } else {
251        switch(regnum) {
252          case TSDEV_CC_CSR:
253              panic("TSDEV_CC_CSR write\n");
254          case TSDEV_CC_MTR:
255              panic("TSDEV_CC_MTR write not implemented\n");
256          case TSDEV_CC_MISC:
257            uint64_t ipreq;
258            ipreq = (pkt->get<uint64_t>() >> 12) & 0xF;
259            //If it is bit 12-15, this is an IPI post
260            if (ipreq) {
261                reqIPI(ipreq);
262                supportedWrite = true;
263            }
264
265            //If it is bit 8-11, this is an IPI clear
266            uint64_t ipintr;
267            ipintr = (pkt->get<uint64_t>() >> 8) & 0xF;
268            if (ipintr) {
269                clearIPI(ipintr);
270                supportedWrite = true;
271            }
272
273            //If it is the 4-7th bit, clear the RTC interrupt
274            uint64_t itintr;
275              itintr = (pkt->get<uint64_t>() >> 4) & 0xF;
276            if (itintr) {
277                  clearITI(itintr);
278                supportedWrite = true;
279            }
280
281              // ignore NXMs
282              if (pkt->get<uint64_t>() & 0x10000000)
283                  supportedWrite = true;
284
285            if(!supportedWrite)
286                  panic("TSDEV_CC_MISC write not implemented\n");
287
288            break;
289            case TSDEV_CC_AAR0:
290            case TSDEV_CC_AAR1:
291            case TSDEV_CC_AAR2:
292            case TSDEV_CC_AAR3:
293                panic("TSDEV_CC_AARx write not implemeted\n");
294            case TSDEV_CC_DIM0:
295            case TSDEV_CC_DIM1:
296            case TSDEV_CC_DIM2:
297            case TSDEV_CC_DIM3:
298                int number;
299                if(regnum == TSDEV_CC_DIM0)
300                    number = 0;
301                else if(regnum == TSDEV_CC_DIM1)
302                    number = 1;
303                else if(regnum == TSDEV_CC_DIM2)
304                    number = 2;
305                else
306                    number = 3;
307
308                uint64_t bitvector;
309                uint64_t olddim;
310                uint64_t olddir;
311
312                olddim = dim[number];
313                olddir = dir[number];
314                dim[number] = pkt->get<uint64_t>();
315                dir[number] = dim[number] & drir;
316                for(int x = 0; x < 64; x++)
317                {
318                    bitvector = ULL(1) << x;
319                    // Figure out which bits have changed
320                    if ((dim[number] & bitvector) != (olddim & bitvector))
321                    {
322                        // The bit is now set and it wasn't before (set)
323                        if((dim[number] & bitvector) && (dir[number] & bitvector))
324                        {
325                          tsunami->intrctrl->post(number, TheISA::INTLEVEL_IRQ1, x);
326                          DPRINTF(Tsunami, "posting dir interrupt to cpu 0\n");
327                        }
328                        else if ((olddir & bitvector) &&
329                                !(dir[number] & bitvector))
330                        {
331                            // The bit was set and now its now clear and
332                            // we were interrupting on that bit before
333                            tsunami->intrctrl->clear(number, TheISA::INTLEVEL_IRQ1, x);
334                          DPRINTF(Tsunami, "dim write resulting in clear"
335                                    " dir interrupt to cpu %d\n",
336                                    x);
337
338                        }
339
340
341                    }
342                }
343                break;
344            case TSDEV_CC_DIR0:
345            case TSDEV_CC_DIR1:
346            case TSDEV_CC_DIR2:
347            case TSDEV_CC_DIR3:
348                panic("TSDEV_CC_DIR write not implemented\n");
349            case TSDEV_CC_DRIR:
350                panic("TSDEV_CC_DRIR write not implemented\n");
351            case TSDEV_CC_PRBEN:
352                panic("TSDEV_CC_PRBEN write not implemented\n");
353            case TSDEV_CC_IIC0:
354            case TSDEV_CC_IIC1:
355            case TSDEV_CC_IIC2:
356            case TSDEV_CC_IIC3:
357                panic("TSDEV_CC_IICx write not implemented\n");
358            case TSDEV_CC_MPR0:
359            case TSDEV_CC_MPR1:
360            case TSDEV_CC_MPR2:
361            case TSDEV_CC_MPR3:
362                panic("TSDEV_CC_MPRx write not implemented\n");
363            case TSDEV_CC_IPIR:
364                clearIPI(pkt->get<uint64_t>());
365                break;
366            case TSDEV_CC_ITIR:
367                clearITI(pkt->get<uint64_t>());
368                break;
369            case TSDEV_CC_IPIQ:
370                reqIPI(pkt->get<uint64_t>());
371                break;
372            default:
373              panic("default in cchip read reached, accessing 0x%x\n");
374        }  // swtich(regnum)
375    } // not BIG_TSUNAMI write
376    pkt->makeAtomicResponse();
377    return pioDelay;
378}
379
380void
381TsunamiCChip::clearIPI(uint64_t ipintr)
382{
383    int numcpus = sys->threadContexts.size();
384    assert(numcpus <= Tsunami::Max_CPUs);
385
386    if (ipintr) {
387        for (int cpunum=0; cpunum < numcpus; cpunum++) {
388            // Check each cpu bit
389            uint64_t cpumask = ULL(1) << cpunum;
390            if (ipintr & cpumask) {
391                // Check if there is a pending ipi
392                if (ipint & cpumask) {
393                    ipint &= ~cpumask;
394                    tsunami->intrctrl->clear(cpunum, TheISA::INTLEVEL_IRQ3, 0);
395                    DPRINTF(IPI, "clear IPI IPI cpu=%d\n", cpunum);
396                }
397                else
398                    warn("clear IPI for CPU=%d, but NO IPI\n", cpunum);
399            }
400        }
401    }
402    else
403        panic("Big IPI Clear, but not processors indicated\n");
404}
405
406void
407TsunamiCChip::clearITI(uint64_t itintr)
408{
409    int numcpus = sys->threadContexts.size();
410    assert(numcpus <= Tsunami::Max_CPUs);
411
412    if (itintr) {
413        for (int i=0; i < numcpus; i++) {
414            uint64_t cpumask = ULL(1) << i;
415            if (itintr & cpumask & itint) {
416                tsunami->intrctrl->clear(i, TheISA::INTLEVEL_IRQ2, 0);
417                itint &= ~cpumask;
418                DPRINTF(Tsunami, "clearing rtc interrupt to cpu=%d\n", i);
419            }
420        }
421    }
422    else
423        panic("Big ITI Clear, but not processors indicated\n");
424}
425
426void
427TsunamiCChip::reqIPI(uint64_t ipreq)
428{
429    int numcpus = sys->threadContexts.size();
430    assert(numcpus <= Tsunami::Max_CPUs);
431
432    if (ipreq) {
433        for (int cpunum=0; cpunum < numcpus; cpunum++) {
434            // Check each cpu bit
435            uint64_t cpumask = ULL(1) << cpunum;
436            if (ipreq & cpumask) {
437                // Check if there is already an ipi (bits 8:11)
438                if (!(ipint & cpumask)) {
439                    ipint  |= cpumask;
440                    tsunami->intrctrl->post(cpunum, TheISA::INTLEVEL_IRQ3, 0);
441                    DPRINTF(IPI, "send IPI cpu=%d\n", cpunum);
442                }
443                else
444                    warn("post IPI for CPU=%d, but IPI already\n", cpunum);
445            }
446        }
447    }
448    else
449        panic("Big IPI Request, but not processors indicated\n");
450}
451
452
453void
454TsunamiCChip::postRTC()
455{
456    int size = sys->threadContexts.size();
457    assert(size <= Tsunami::Max_CPUs);
458
459    for (int i = 0; i < size; i++) {
460        uint64_t cpumask = ULL(1) << i;
461       if (!(cpumask & itint)) {
462           itint |= cpumask;
463           tsunami->intrctrl->post(i, TheISA::INTLEVEL_IRQ2, 0);
464           DPRINTF(Tsunami, "Posting RTC interrupt to cpu=%d\n", i);
465       }
466    }
467
468}
469
470void
471TsunamiCChip::postDRIR(uint32_t interrupt)
472{
473    uint64_t bitvector = ULL(1) << interrupt;
474    uint64_t size = sys->threadContexts.size();
475    assert(size <= Tsunami::Max_CPUs);
476    drir |= bitvector;
477
478    for(int i=0; i < size; i++) {
479        dir[i] = dim[i] & drir;
480       if (dim[i] & bitvector) {
481              tsunami->intrctrl->post(i, TheISA::INTLEVEL_IRQ1, interrupt);
482              DPRINTF(Tsunami, "posting dir interrupt to cpu %d,"
483                        "interrupt %d\n",i, interrupt);
484       }
485    }
486}
487
488void
489TsunamiCChip::clearDRIR(uint32_t interrupt)
490{
491    uint64_t bitvector = ULL(1) << interrupt;
492    uint64_t size = sys->threadContexts.size();
493    assert(size <= Tsunami::Max_CPUs);
494
495    if (drir & bitvector)
496    {
497        drir &= ~bitvector;
498        for(int i=0; i < size; i++) {
499           if (dir[i] & bitvector) {
500               tsunami->intrctrl->clear(i, TheISA::INTLEVEL_IRQ1, interrupt);
501               DPRINTF(Tsunami, "clearing dir interrupt to cpu %d,"
502                    "interrupt %d\n",i, interrupt);
503
504           }
505           dir[i] = dim[i] & drir;
506        }
507    }
508    else
509        DPRINTF(Tsunami, "Spurrious clear? interrupt %d\n", interrupt);
510}
511
512
513void
514TsunamiCChip::serialize(std::ostream &os)
515{
516    SERIALIZE_ARRAY(dim, Tsunami::Max_CPUs);
517    SERIALIZE_ARRAY(dir, Tsunami::Max_CPUs);
518    SERIALIZE_SCALAR(ipint);
519    SERIALIZE_SCALAR(itint);
520    SERIALIZE_SCALAR(drir);
521}
522
523void
524TsunamiCChip::unserialize(Checkpoint *cp, const std::string &section)
525{
526    UNSERIALIZE_ARRAY(dim, Tsunami::Max_CPUs);
527    UNSERIALIZE_ARRAY(dir, Tsunami::Max_CPUs);
528    UNSERIALIZE_SCALAR(ipint);
529    UNSERIALIZE_SCALAR(itint);
530    UNSERIALIZE_SCALAR(drir);
531}
532
533TsunamiCChip *
534TsunamiCChipParams::create()
535{
536    return new TsunamiCChip(this);
537}
538