summaryrefslogtreecommitdiffstats
path: root/src/usr/intr/intrrp.C
blob: 23aa9177733e63b25e0b5d84e1ef60d33c968820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//  IBM_PROLOG_BEGIN_TAG
//  This is an automatically generated prolog.
//
//  $Source: src/usr/intr/intrrp.C $
//
//  IBM CONFIDENTIAL
//
//  COPYRIGHT International Business Machines Corp. 2011
//
//  p1
//
//  Object Code Only (OCO) source materials
//  Licensed Internal Code Source Materials
//  IBM HostBoot Licensed Internal Code
//
//  The source code for this program is not published or other-
//  wise divested of its trade secrets, irrespective of what has
//  been deposited with the U.S. Copyright Office.
//
//  Origin: 30
//
//  IBM_PROLOG_END
/**
 * @file intrrp.C
 * @brief Interrupt Resource Provider
 */

#include "intrrp.H"
#include <trace/interface.H>
#include <errno.h>
#include <initservice/taskargs.H>
#include <util/singleton.H>
#include <intr/intr_reasoncodes.H>
#include <sys/mmio.h>
#include <sys/misc.h>
#include <kernel/console.H>
#include <sys/task.h>
#include <targeting/targetservice.H>
#include <vmmconst.h>
#include <targeting/util.H>

using namespace INTR;

trace_desc_t * g_trac_intr = NULL;
TRAC_INIT(&g_trac_intr, INTR_COMP_NAME, 2 * 1024);


/**
 * setup _start and handle barrier
 */
TASK_ENTRY_MACRO( IntrRp::init );


void IntrRp::init( void * i_taskArgs )
{
    errlHndl_t err = NULL;
    err = Singleton<IntrRp>::instance()._init();
    INITSERVICE::TaskArgs* args =
        static_cast<INITSERVICE::TaskArgs*>(i_taskArgs);
    if(err)
    {
        args->postErrorLog(err);
    }
}


//  ICPBAR = INTP.ICP_BAR[0:25] in P7 = 0x3FBFF90 + (8*node) + procPos
//  P7 Scom address 0x02011C09 P8 = 0x020109c9
//  BaseAddress:
//  BA[18:43] = ICPBAR  (P8 says [14:43] (30 bits))
//  BA[47:49] = COREid (0-7)
//  BA[50:51] = cpu thread (0-3)
//
//  BA+0  = XIRR (poll - Read/Write has no side effects))
//  BA+4  = XIRR (Read locks HW, Write -> EOI to HW))
//  BA+12 = MFRR  (1 byte)
//  BA+16 = LINKA (4 bytes)
//  BA+20 = LINKB (4 bytes)
//  BA+24 = LINKC (4 bytes)
errlHndl_t IntrRp::_init()
{
    errlHndl_t err = NULL;

    // TODO Temporaritly DISABLE in VBU until P8 support is added
    if( TARGETING::is_vpo() )
    {
        iv_isVBU = true;
    }

    // get the PIR
    // Which ever cpu core this is running on is the MASTER cpu
    // Make master thread 0
    iv_masterCpu.word = task_getcpuid();
    iv_masterCpu.threadId = 0;

    uint64_t realAddr = 0;

    // TODO Does the BAR scom reg need to be read here or set here??
    //      Who sets the BAR?
    //uint64_t size = sizeof(realAddr);
    //TARGETING::TargetService& targetService = TARGETING::targetService();
    //TARGETING::Target* procTarget = NULL;
    //targetService.masterProcChipTargetHandle( procTarget );
    //
    // TODO What if this does not jive with with the PIR?

    // TODO Does this need to be read here?
    //err = deviceRead(procTarget,
    //                 &realAddr,
    //                 size,
    //                 DEVICE_SCOM_ADDRESS(ICPBAR_SCOM_ADDR));
    //if(err) return err;

    if(realAddr == 0)
    {
        realAddr = (static_cast<uint64_t>(ICPBAR_VAL)) << 34;
    }

    // TODO Does this need to be set here?
    // err = deviceWrite(procTarget,
    //                   &realAddr,
    //                   size,
    //                   DEVICE_SCOM_ADDRESS(ICPBAR_SCOM_ADDR));

    realAddr &= 0xFFFFFFFC00000000ul;   //[0:29] is ICP_BAR
    realAddr >>= 14;                    //[14:43] is BAR field in real address


    // The realAddr is the base address for the whole system.
    // Therefore the realAddr must be based on the processor with
    // lowest BAR value in the system. (usually n0p0)
    // TODO Adjust the realAddr if the BAR came from a processor other
    // than cpuid 0

    TRACFCOMP(g_trac_intr,"INTR: realAddr = %lx",realAddr);

    // VADDR_SIZE is 1MB per chip - max 32 -> 32MB
    iv_baseAddr = reinterpret_cast<uint64_t>
        (mmio_dev_map(reinterpret_cast<void*>(realAddr),THIRTYTWO_MB));

    TRACFCOMP(g_trac_intr,"INTR: vAddr = %lx",iv_baseAddr);

    err = checkAddress(iv_baseAddr);
    if(!err)
    {

        // Set up the interrupt provider registers
        // NOTE: Simics only supports 4 threads, and two cores per proc chip.
        //
        // NOTE: P7 register address format only supports 4 threads per core.
        //       This will change for P8
        //
        // NOTE: It's only possible to set up the master core at this point.
        //
        // Set up link registers to forward all intrpts to master cpu.
        //
        // There is one register set per cpu thread or 1024 register sets max.
        size_t threads = cpu_thread_count();

        if(threads > 4)   //TODO remove when true P8 support is added
        {
            TRACFCOMP(g_trac_intr,
                      "I>intrrp needs to be updated to handle > 4 threads");
            threads = 4;
        }

        PIR_t pir = iv_masterCpu;
        for(size_t thread = 0; thread < threads; ++thread)
        {
            pir.threadId = thread;
            initInterruptPresenter(pir);
        }

        // Get the kernel msg queue for ext intr
        // Create a task to handle the messages
        iv_msgQ = msg_q_create();
        msg_q_register(iv_msgQ, INTR_MSGQ);

        task_create(IntrRp::msg_handler, NULL);
    }

    return err;
}

errlHndl_t IntrRp::enableInterrupts()
{
    errlHndl_t err = NULL;

    // TODO Temporarily DISABLE in VBU until P8 support is added
    if(iv_isVBU) return err;

    // Enable the interrupt on master processor core, thread 0
    uint64_t baseAddr = iv_baseAddr + cpuOffsetAddr(iv_masterCpu);

    err = checkAddress(baseAddr);
    if(!err)
    {
        uint8_t * cppr = reinterpret_cast<uint8_t*>(baseAddr+CPPR_OFFSET);
        *cppr = 0xff;
    }

    return err;
}

errlHndl_t IntrRp::disableInterrupts()
{
    errlHndl_t err = NULL;

    // Disable the interrupt on master processor core, thread 0
    // TODO Temporarily DISABLE in VBU until P8 support is added
    if(iv_isVBU) return err;

    uint64_t baseAddr = iv_baseAddr + cpuOffsetAddr(iv_masterCpu);

    err = checkAddress(baseAddr);
    if(!err)
    {
        uint8_t * cppr = reinterpret_cast<uint8_t*>(baseAddr+CPPR_OFFSET);
        *cppr = 0;
    }

    return err;
}

/**
 * Helper function to start the messge handler
 */
void IntrRp::msg_handler(void * unused)
{
    Singleton<IntrRp>::instance().msgHandler();
}


void IntrRp::msgHandler()
{
    while(1)
    {
        msg_t* msg = msg_wait(iv_msgQ); // wait for interrupt msg

        switch(msg->type)
        {
            case MSG_INTR_EXTERN:
                {
                    // Acknowlege msg
                    msg->data[1] = 0;
                    msg_respond(iv_msgQ, msg);

                    ext_intr_t type = NO_INTERRUPT;

                    // type = XISR = XIRR[8:31]
                    // priority = XIRR[0:7]
                    uint32_t * xirrAddress =
                        reinterpret_cast<uint32_t *>(iv_baseAddr+ XIRR_OFFSET+
                                                  cpuOffsetAddr(iv_masterCpu));

                    // xirr was read by interrupt message handler.
                    // Passed in as data[0]
                    uint32_t xirr = static_cast<uint32_t>(msg->data[0]);
                    type = static_cast<ext_intr_t>(xirr & 0x00FFFFFF);

                    TRACDCOMP(g_trac_intr,"External Interrupt recieved, Type=%x",type);

                    Registry_t::iterator r = iv_registry.find(type);
                    if(r != iv_registry.end())
                    {
                        msg_q_t msgQ = r->second;
                        msg_t * rmsg = msg_allocate();
                        rmsg->type = type;
                        int rc = msg_sendrecv(msgQ,rmsg);
                        if(rc)
                        {
                            TRACFCOMP(g_trac_intr,ERR_MRK
                                      "External Interrupt recieved type = %d, "
                                      "but could not send message to registered"
                                      " handler. Ignorming it. rc = %d",
                                      (uint32_t) type, rc);
                        }
                    }
                    else  // no queue registered for this interrupt type
                    {
                        // Throw it away for now.
                        TRACFCOMP(g_trac_intr,ERR_MRK
                                  "External Interrupt recieved type = %d, but "
                                  "nothing registered to handle it. "
                                  "Ignoreing it.",
                                  (uint32_t)type);
                    }

                    // Writing the XIRR with the same value read earlier
                    // tells the interrupt presenter hardware to signal an EOI.
                    *xirrAddress = xirr;
                }
                break;

            case MSG_INTR_REGISTER_MSGQ:
                {
                    msg_q_t l_msgQ= reinterpret_cast<msg_q_t>(msg->data[0]);
                    ext_intr_t l_t= static_cast<ext_intr_t>(msg->data[1]);
                    errlHndl_t err = registerInterrupt(l_msgQ,l_t);

                    msg->data[1] = reinterpret_cast<uint64_t>(err);
                    msg_respond(iv_msgQ,msg);
                }
                break;

            case MSG_INTR_ENABLE:
                {
                    errlHndl_t err = enableInterrupts();
                    msg->data[1] = reinterpret_cast<uint64_t>(err);
                    msg_respond(iv_msgQ,msg);
                }
                break;

            case MSG_INTR_DISABLE:
                {
                    errlHndl_t err =disableInterrupts();
                    msg->data[1] = reinterpret_cast<uint64_t>(err);
                    msg_respond(iv_msgQ,msg);
                }
                break;

            //  Called when a new cpu becomes active other than the master
            //  Expect a call for each new core
            case MSG_INTR_ADD_CPU_USR:
            case MSG_INTR_ADD_CPU:
                {
                    PIR_t pir = msg->data[0];

                    size_t threads = cpu_thread_count();

                    for(size_t thread = 0; thread < threads; ++thread)
                    {
                        pir.threadId = thread;
                        initInterruptPresenter(pir);
                    }

                    msg->data[1] = 0;
                    msg_respond(iv_msgQ, msg);
                }
                break;

            default:
                msg->data[1] = -EINVAL;
                msg_respond(iv_msgQ, msg);
        }
    }
}


errlHndl_t IntrRp::registerInterrupt(msg_q_t i_msgQ, ext_intr_t i_type)
{
    errlHndl_t err = NULL;

    Registry_t::iterator r = iv_registry.find(i_type);
    if(r == iv_registry.end())
    {
        iv_registry[i_type] = i_msgQ;
    }
    else
    {
        if(r->second != i_msgQ)
        {
        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        INTR_MODULE_ID
         * @reasoncode      INTR_ALREADY_REGISTERED
         * @userdata1       Interrupt type
         * @userdata2       0
         *
         * @defdesc         Interrupt type already registered
         *
         */
            err = new ERRORLOG::ErrlEntry
                (
                 ERRORLOG::ERRL_SEV_INFORMATIONAL,      // severity
                 INTR::INTR_MODULE_ID,                  // moduleid
                 INTR::INTR_ALREADY_REGISTERED,         // reason code
                 i_type,
                 0
                );
        }

    }
    return err;
}

void IntrRp::initInterruptPresenter(const PIR_t i_pir) const
{
    // TODO Temporaritly DISABLE in VBU until P8 support is added
    if(iv_isVBU) return;

    //@fixme - this seems redundant...
    if( TARGETING::is_vpo() )
    {
        return;
    }

    uint64_t baseAddr = iv_baseAddr + cpuOffsetAddr(i_pir);
    uint8_t * cppr =
        reinterpret_cast<uint8_t*>(baseAddr + CPPR_OFFSET);
    uint32_t * plinkReg =
        reinterpret_cast<uint32_t *>(baseAddr + LINKA_OFFSET);

    TRACDCOMP(g_trac_intr,"PIR 0x%x offset: 0x%lx",
              i_pir.word,
              cpuOffsetAddr(i_pir));
    if(i_pir.word == iv_masterCpu.word)
    {
        *cppr = 0xff;
    }
    else
    {
        *cppr = 0;      // no interrupts allowed except on master
    }

    // Links are intended to be set up in rings.  If an interrupt ends up
    // where it started, it gets rejected by hardware.
    //
    // According to BOOK IV, The links regs are setup by firmware.
    //
    // Should be possible to link all interrupt forwarding directly to
    // the master core and either make them direct (lspec = 0) or by setting
    // the LOOPTRIP bit to stop the forwarding at the masterProc.
    //
    LinkReg_t linkReg;
    linkReg.word = 0;
    linkReg.loopTrip = 1;   // needed?
    linkReg.pchip= (iv_masterCpu.nodeId*8)+iv_masterCpu.chipId;
    linkReg.pcore=  iv_masterCpu.coreId;
    linkReg.tspec=  iv_masterCpu.threadId;

    *(plinkReg) = linkReg.word;
    *(plinkReg + 1) = linkReg.word;
    linkReg.last = 1;
    *(plinkReg + 2) = linkReg.word;
}


errlHndl_t IntrRp::checkAddress(uint64_t i_addr)
{
    errlHndl_t err = NULL;

    if(i_addr < VMM_VADDR_DEVICE_SEGMENT_FIRST)
    {
        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        INTR_MODULE_ID
         * @reasoncode      INTR_BAD_VIRTUAL_IO_ADDRESS
         * @userdata1       The bad virtual address
         * @userdata2       0
         *
         * @defdesc         The virutal address is not a valid IO address
         *
         */
        err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,
             INTR::INTR_MODULE_ID,
             INTR::INTR_BAD_VIRTUAL_IO_ADDRESS,
             i_addr,
             0
            );
    }

    return err;
}


// Register a message queue with a particular intr type
errlHndl_t INTR::registerMsgQ(msg_q_t i_msgQ, ext_intr_t i_type)
{
    errlHndl_t err = NULL;
    // Can't add while handling an interrupt, so
    // send msg instead of direct call
    msg_q_t intr_msgQ = msg_q_resolve(INTR_MSGQ);
    if(intr_msgQ)
    {
        msg_t * msg = msg_allocate();
        msg->type = MSG_INTR_REGISTER_MSGQ;
        msg->data[0] = reinterpret_cast<uint64_t>(i_msgQ);
        msg->data[1] = static_cast<uint64_t>(i_type);

        msg_sendrecv(intr_msgQ, msg);
        err = reinterpret_cast<errlHndl_t>(msg->data[1]);
    }
    else
    {
        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        INTR_MODULE_ID
         * @reasoncode      INTR_REGISTRY_NOT_READY
         * @userdata1       Interrupt type to register
         * @userdata2       0
         *
         * @defdesc         Interrupt resource provider not initialized yet.
         *
         */
        err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,      // severity
             INTR::INTR_MODULE_ID,                  // moduleid
             INTR::INTR_REGISTRY_NOT_READY,         // reason code
             static_cast<uint64_t>(i_type),
             0
            );
    }
    return err;
}

/*
 * Enable hardware to report external interrupts
 */
errlHndl_t INTR::enableExternalInterrupts()
{
    errlHndl_t err = NULL;
    msg_q_t intr_msgQ = msg_q_resolve(INTR_MSGQ);
    if(intr_msgQ)
    {
        msg_t * msg = msg_allocate();
        msg->type = MSG_INTR_ENABLE;

        msg_sendrecv(intr_msgQ, msg);

        err = reinterpret_cast<errlHndl_t>(msg->data[1]);
    }
    else
    {
        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        INTR_MODULE_ID
         * @reasoncode      INTR_RP_NOT_INITIALIZED
         * @userdata1       MSG_INTR_ENABLE
         * @userdata2       0
         *
         * @defdesc         Interrupt resource provider not initialized yet.
         *
         */
        err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,      // severity
             INTR::INTR_MODULE_ID,                  // moduleid
             INTR::INTR_RP_NOT_INITIALIZED,         // reason code
             static_cast<uint64_t>(MSG_INTR_ENABLE),
             0
            );
    }
    return err;
}

/*
 * Disable hardware from reporting external interrupts
 */
errlHndl_t INTR::disableExternalInterrupts()
{
    errlHndl_t err = NULL;
    // Can't disable while handling interrupt, so send msg to serialize
    msg_q_t intr_msgQ = msg_q_resolve(INTR_MSGQ);
    if(intr_msgQ)
    {
        msg_t * msg = msg_allocate();
        msg->type = MSG_INTR_DISABLE;

        msg_sendrecv(intr_msgQ, msg);

        err = reinterpret_cast<errlHndl_t>(msg->data[1]);
    }
    else
    {
        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        INTR_MODULE_ID
         * @reasoncode      INTR_RP_NOT_INITIALIZED
         * @userdata1       MSG_INTR_DISABLE
         * @userdata2       0
         *
         * @defdesc         Interrupt resource provider not initialized yet.
         *
         */
        err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,      // severity
             INTR::INTR_MODULE_ID,                  // moduleid
             INTR::INTR_RP_NOT_INITIALIZED,         // reason code
             static_cast<uint64_t>(MSG_INTR_DISABLE),
             0
            );
    }
    return err;
}

OpenPOWER on IntegriCloud