First public contribution.
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\nkernsmp\nk_irq.cpp
24 #include <e32cmn_private.h>
28 NIrq Irq[NK_MAX_IRQS];
29 NIrqHandler Handlers[NK_MAX_IRQ_HANDLERS];
30 NIrqHandler* NIrqHandler::FirstFree;
32 extern "C" void send_irq_ipi(TSubScheduler*);
34 void StepCookie(volatile TUint16& p, TInt n)
48 : iNIrqLock(TSpinLock::EOrderNIrq)
57 TInt NIrq::BindRaw(NIsr aIsr, TAny* aPtr)
59 // Call only from thread context
63 if (iStaticFlags & EShared)
68 if ( (iIState & ERaw) || !iHandlers.IsEmpty())
73 iHandlers.iA.iNext = (SDblQueLink*)aIsr;
74 iHandlers.iA.iPrev = (SDblQueLink*)aPtr;
75 __e32_atomic_ior_rel32(&iIState, ERaw);
77 iNIrqLock.UnlockOnly();
82 TInt NIrq::UnbindRaw()
84 // Call only from thread context
85 TInt r = DisableRaw(TRUE);
92 iHandlers.iA.iNext = 0;
93 iHandlers.iA.iPrev = 0;
94 ++iGeneration; // release anyone still waiting in Disable()
95 __e32_atomic_and_rel32(&iIState, ~(ERaw|EUnbind));
97 iNIrqLock.UnlockOnly();
102 TInt NIrq::DisableRaw(TBool aUnbind)
106 TInt irq = __SPIN_LOCK_IRQSAVE(iNIrqLock);
107 if (!(iIState & ERaw))
113 __e32_atomic_ior_acq32(&iIState, EUnbind);
114 if (!(iEnabledEvents & 1))
121 __SPIN_UNLOCK_IRQRESTORE(iNIrqLock,irq);
122 TInt c = NKern::CurrentContext();
123 if (wait && c!=NKern::EInterrupt)
125 // wait for currently running handler to finish or interrupt to be reenabled
126 if (c==NKern::EThread)
127 NKern::ThreadEnterCS();
128 HwWaitCpus(); // ensure other CPUs have had a chance to accept any outstanding interrupts
129 TUint32 g = iGeneration;
130 while ( ((iIState >> 16) || HwPending()) && (iGeneration == g))
134 if (c==NKern::EThread)
135 NKern::ThreadLeaveCS();
140 TInt NIrq::EnableRaw()
143 TInt irq = __SPIN_LOCK_IRQSAVE(iNIrqLock);
144 if (!(iIState & ERaw))
146 else if (iIState & EUnbind)
148 else if (iEnabledEvents & 1)
154 __SPIN_UNLOCK_IRQRESTORE(iNIrqLock,irq);
158 TInt NIrq::Bind(NIrqHandler* aH)
160 // Call only from thread context
163 if (!(iIState & ERaw))
166 TBool empty = iHandlers.IsEmpty();
167 TBool shared = iStaticFlags & EShared;
168 TBool exclusive = iIState & NIrqHandler::EExclusive;
171 if (!shared || exclusive)
173 r = KErrAccessDenied;
176 NIrqHandler* h = _LOFF(iHandlers.First(), NIrqHandler, iIrqLink);
177 if (h->iHState & NIrqHandler::EExclusive)
179 r = KErrAccessDenied;
184 iHandlers.Add(&aH->iIrqLink);
193 TRACE_IRQ12(16, this, iVector, iIState);
194 TBool eoi_done = FALSE;
195 TUint32 rcf0 = EnterIsr(); // for initial run count
196 TUint32 rcf1 = iIState; // might have changed while we were waiting in EnterIsr()
199 if (!(rcf1 & EUnbind))
201 NIsr f = (NIsr)iHandlers.iA.iNext;
202 TAny* p = iHandlers.iA.iPrev;
214 if (!(iStaticFlags & ELevel))
220 // Handler list can't be touched now
221 SDblQueLink* anchor = &iHandlers.iA;
222 SDblQueLink* p = anchor->iNext;
225 NIrqHandler* h = _LOFF(p, NIrqHandler, iIrqLink);
234 if ((iStaticFlags & ELevel) && iEventsPending)
236 // For a level triggered interrupt make sure interrupt is disabled until
237 // all pending event handlers have run, to avoid a continuous interrupt.
238 TInt irq = __SPIN_LOCK_IRQSAVE(iNIrqLock);
244 __SPIN_UNLOCK_IRQRESTORE(iNIrqLock,irq);
249 void NIrqHandler::Activate(TInt aCount)
251 TUint32 orig = DoActivate(aCount);
252 TRACE_IRQ12(17, this, orig, aCount);
253 if (orig & (EDisable|EUnbind|EActive))
254 return; // disabled or already active
257 // we need to enforce mutual exclusion between the event handler
258 // and the tied thread or thread group, so the event handler must
259 // run on the CPU to which the thread or group is currently attached
260 // once the event has been attached to that CPU, the thread/group
261 // can't be migrated until the event handler completes.
262 // need a pending event count for the tied thread/group
263 // so we know when the thread/group can be migrated
264 TInt tied_cpu = iTied->BeginTiedEvent();
265 TInt this_cpu = NKern::CurrentCpu();
266 if (tied_cpu != this_cpu)
268 __e32_atomic_add_acq32(&iIrq->iEventsPending, 1);
269 TheSubSchedulers[tied_cpu].QueueEventAndKick(this);
270 // FIXME: move IRQ over to tied CPU if this is the only handler for that IRQ
271 // what to do about shared IRQs?
275 // event can run on this CPU so run it now
279 TRACE_IRQ8(18, this, orig);
282 TRACE_IRQ8(19, this, orig);
283 if (!(orig & EActive))
286 iTied->EndTiedEvent();
287 return; // that was last occurrence or event now disabled
290 __e32_atomic_add_ord32(&iIrq->iEventsPending, 1);
291 // add event to this cpu
292 SubScheduler().QueueEventAndKick(this);
296 NIrqHandler::NIrqHandler()
301 iHState = EDisable|EBind|ENotReady|EEventHandlerIrq;
304 memclr(iNIrqHandlerSpare, sizeof(iNIrqHandlerSpare));
307 void NIrqHandler::Free()
310 NEventHandler::TiedLock.LockOnly();
311 if (!iTied) // Only free if iTied has been cleared
313 iIrqLink.iNext = FirstFree;
316 NEventHandler::TiedLock.UnlockOnly();
320 NIrqHandler* NIrqHandler::Alloc()
323 NEventHandler::TiedLock.LockOnly();
324 NIrqHandler* p = FirstFree;
326 FirstFree = (NIrqHandler*)p->iIrqLink.iNext;
327 NEventHandler::TiedLock.UnlockOnly();
330 new (p) NIrqHandler();
334 TInt NIrqHandler::Enable(TInt aHandle)
336 // call from any context
337 TBool reactivate = FALSE;
338 TInt r = KErrNotReady;
342 TInt irq = __SPIN_LOCK_IRQSAVE(pI->iNIrqLock); // OK since NIrq's are never deleted
343 if (iIrq==pI && TUint(aHandle)==iHandle) // check handler not unbound
345 TUint32 orig = DoSetEnabled(); // clear EDisable and EBind provided neither EUnbind nor ENotReady set
346 if (!(orig & (EUnbind|ENotReady)))
349 if (orig & EDisable) // check not already enabled
352 TUint32 n = pI->iEnabledEvents;
353 pI->iEnabledEvents += 2;
355 pI->HwEnable(); // enable HW interrupt if this is first handler to be enabled
356 if ((orig >> 16) && !(orig & EActive))
357 // replay remembered interrupt(s)
364 pI->iNIrqLock.UnlockOnly();
366 pI->iNIrqLock.LockOnly();
368 __SPIN_UNLOCK_IRQRESTORE(pI->iNIrqLock,irq);
372 TInt NIrqHandler::Disable(TBool aUnbind, TInt aHandle)
374 // call from any context
378 TInt irq = __SPIN_LOCK_IRQSAVE(pI->iNIrqLock); // OK since NIrq's are never deleted
379 if (iIrq != pI || TUint(aHandle)!=iHandle) // check handler not unbound
381 __SPIN_UNLOCK_IRQRESTORE(pI->iNIrqLock,irq);
384 TInt r = aUnbind ? KErrGeneral : KErrNone;
385 TUint32 f = aUnbind ? EUnbind|EDisable : EDisable;
386 TUint32 orig = __e32_atomic_ior_acq32(&iHState, f);
387 TUint32 g = iGeneration;
388 if (!(orig & EDisable)) // check not already disabled
390 pI->iEnabledEvents -= 2;
391 if (!pI->iEnabledEvents)
392 pI->HwDisable(); // disable HW interrupt if no more enabled handlers
394 if (aUnbind && !(orig & EUnbind))
396 volatile TUint16& cookie = *(volatile TUint16*)(((TUint8*)&iHandle)+2);
397 StepCookie(cookie, 1);
400 __SPIN_UNLOCK_IRQRESTORE(pI->iNIrqLock,irq);
401 if (NKern::CurrentContext() != NKern::EInterrupt)
403 // wait for currently running handler to finish or interrupt to be reenabled
404 while ((iHState & EActive) && (iGeneration == g))
412 TInt NIrqHandler::Unbind(TInt aId, NSchedulable* aTied)
414 TInt r = Disable(TRUE, aId); // waits for any current activation of ISR to finish
415 if (r==KErrNone || aTied) // returns KErrGeneral if someone else already unbound this interrupt handler
417 // Possible race condition here between tied thread termination and interrupt unbind.
418 // We need to be sure that the iTied field must be NULL before the tied thread/group
421 NEventHandler::TiedLock.LockOnly(); // this guarantees pH->iTied cannot change
422 NSchedulable* t = iTied;
425 // We need to guarantee the object pointed to by t cannot be deleted until we
426 // have finished with it.
434 if (aTied && aTied==t)
438 NEventHandler::TiedLock.UnlockOnly();
449 void NIrqHandler::DoUnbind()
451 // Call only from thread context
459 TBool TSubScheduler::QueueEvent(NEventHandler* aEvent)
461 TInt irq = __SPIN_LOCK_IRQSAVE(iEventHandlerLock);
462 TBool pending = iEventHandlersPending;
463 iEventHandlersPending = TRUE;
464 iEventHandlers.Add(aEvent);
465 __SPIN_UNLOCK_IRQRESTORE(iEventHandlerLock,irq);
469 void TSubScheduler::QueueEventAndKick(NEventHandler* aEvent)
471 if (QueueEvent(aEvent))
478 extern "C" void run_event_handlers(TSubScheduler* aS)
480 while (aS->iEventHandlersPending)
482 TInt irq = __SPIN_LOCK_IRQSAVE(aS->iEventHandlerLock);
483 if (aS->iEventHandlers.IsEmpty())
485 aS->iEventHandlersPending = FALSE;
486 __SPIN_UNLOCK_IRQRESTORE(aS->iEventHandlerLock, irq);
489 NIrqHandler* h = (NIrqHandler*)aS->iEventHandlers.First()->Deque();
490 if (aS->iEventHandlers.IsEmpty())
491 aS->iEventHandlersPending = FALSE;
492 TInt type = h->iHType;
493 NSchedulable* tied = h->iTied;
494 if (type == NEventHandler::EEventHandlerNTimer)
498 mb(); // make sure dequeue observed and iFn,iPtr,iTied sampled before state change observed
499 h->i8888.iHState1 = NTimer::EIdle; // can't touch timer again after this
500 __SPIN_UNLOCK_IRQRESTORE(aS->iEventHandlerLock, irq);
503 tied->EndTiedEvent();
506 __SPIN_UNLOCK_IRQRESTORE(aS->iEventHandlerLock, irq);
507 TBool requeue = TRUE;
510 case NEventHandler::EEventHandlerIrq:
513 // event can run on this CPU so run it now
514 // if event tied, migration of tied thread/group will have been blocked
515 orig = h->EventBegin();
516 TRACE_IRQ8(20, h, orig);
519 if (!(h->iHState & NIrqHandler::ERunCountMask)) // if run count still nonzero, definitely still active
522 irq = __SPIN_LOCK_IRQSAVE(pI->iNIrqLock);
523 orig = h->EventDone();
524 TRACE_IRQ8(22, h, orig);
525 if (!(orig & NIrqHandler::EActive))
527 // handler is no longer active - can't touch it again
528 // pI is OK since NIrq's are never deleted/reused
530 if (__e32_atomic_add_rel32(&pI->iEventsPending, TUint32(-1)) == 1)
532 if (pI->iEnabledEvents & 1)
534 pI->iEnabledEvents &= ~1;
535 if (pI->iEnabledEvents)
540 __SPIN_UNLOCK_IRQRESTORE(pI->iNIrqLock,irq);
545 __KTRACE_OPT(KPANIC,DEBUGPRINT("h=%08x",h));
546 __NK_ASSERT_ALWAYS(0);
548 if (tied && !requeue)
550 // If the tied thread/group has no more tied events outstanding
551 // and has a migration pending, trigger the migration now.
552 // Atomically change the tied_cpu to the target CPU here. An IDFC
553 // can then effect the migration.
554 // Note that the tied code can't run in parallel with us until
555 // the tied_cpu is changed. However it could run as soon as the
556 // tied_cpu is changed (e.g. if added to ready list after change)
557 tied->EndTiedEvent();
561 // still pending so put it back on the queue
562 // leave interrupt disabled (if so) and migration of tied thread/group blocked
568 /******************************************************************************
569 * Public interrupt management functions
570 ******************************************************************************/
572 void NKern::InterruptInit0()
576 NIrqHandler::FirstFree = 0;
577 for (i=NK_MAX_IRQ_HANDLERS-1; i>=0; --i)
579 StepCookie(cookie, 61);
580 NIrqHandler* h = &::Handlers[i];
581 __KTRACE_OPT(KBOOT,DEBUGPRINT("NIrqHandler[%d] at %08x", i, h));
583 h->iHandle = (cookie << 16) | i;
584 h->iIrqLink.iNext = NIrqHandler::FirstFree;
585 NIrqHandler::FirstFree = h;
590 EXPORT_C TInt NKern::InterruptInit(TInt aId, TUint32 aFlags, TInt aVector, TUint32 aHwId, TAny* aExt)
592 __KTRACE_OPT(KBOOT,DEBUGPRINT("NKII: ID=%02x F=%08x V=%03x HWID=%08x X=%08x", aId, aFlags, aVector, aHwId, aExt));
593 TRACE_IRQ12(0, (aId|(aVector<<16)), aFlags, aHwId);
594 if (TUint(aId) >= TUint(NK_MAX_IRQS))
596 NIrq* pI = &Irq[aId];
597 __KTRACE_OPT(KBOOT,DEBUGPRINT("NIrq[%02x] at %08x", aId, pI));
598 TRACE_IRQ8(1, aId, pI);
600 pI->iX = (NIrqX*)aExt;
601 pI->iIndex = (TUint16)aId;
603 pI->iVector = aVector;
604 pI->iStaticFlags = (TUint16)(aFlags & 0x13);
605 if (aFlags & NKern::EIrqInit_Count)
606 pI->iIState |= NIrq::ECount;
608 __e32_atomic_and_rel32(&pI->iIState, ~NIrq::EWait);
612 EXPORT_C TInt NKern::InterruptBind(TInt aId, NIsr aIsr, TAny* aPtr, TUint32 aFlags, NSchedulable* aTied)
614 __KTRACE_OPT(KNKERN,DEBUGPRINT(">NKIB: ID=%02x ISR=%08x(%08x) F=%08x T=%T", aId, aIsr, aPtr, aFlags, aTied));
615 TRACE_IRQ12(2, aId, aIsr, aPtr);
616 TRACE_IRQ12(3, aId, aFlags, aTied);
617 CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"NKern::InterruptBind");
618 if (TUint(aId) >= TUint(NK_MAX_IRQS))
620 TRACE_IRQ8(4, aId, KErrArgument);
623 NIrq* pI = &Irq[aId];
625 NSchedulable* pT = 0;
626 if (aFlags & NKern::EIrqBind_Tied)
629 aTied = NKern::CurrentThread();
632 TInt r = KErrNoMemory;
634 NKern::ThreadEnterCS();
635 if (!(aFlags & NKern::EIrqBind_Raw))
637 pH = NIrqHandler::Alloc();
642 __e32_atomic_add_ord32(&pH->iGeneration, 1);
643 if (aFlags & EIrqBind_Exclusive)
644 pH->iHState |= NIrqHandler::EExclusive;
645 if (aFlags & EIrqBind_Count)
646 pH->iHState |= NIrqHandler::ECount;
650 handle = pH->iHandle;
651 // We assume that aTied cannot disappear entirely before we return
655 r = pT->AddTiedEvent(pH);
669 if (aFlags & NKern::EIrqBind_Tied)
670 r = KErrNotSupported;
672 r = pI->BindRaw(aIsr, aPtr);
677 // clear ENotReady so handler can be enabled
678 __e32_atomic_and_rel32(&pH->iHState, ~NIrqHandler::ENotReady);
681 NKern::ThreadLeaveCS();
682 __KTRACE_OPT(KNKERN,DEBUGPRINT("<NKIB: %08x", r));
683 TRACE_IRQ8(4, aId, r);
687 TInt NIrq::FromHandle(TInt& aHandle, NIrq*& aIrq, NIrqHandler*& aHandler)
689 TRACE_IRQ4(5, aHandle);
693 NIrqHandler* pH2 = 0;
695 SDblQueLink* anchor = 0;
697 TInt r = KErrArgument;
698 if (aHandle & NKern::EIrqCookieMask)
700 i = aHandle & NKern::EIrqIndexMask;
701 if (i>=NK_MAX_IRQ_HANDLERS)
704 if (pH->iHandle != TUint(aHandle))
711 if (TUint32(aHandle)>=NK_MAX_IRQS)
713 pI = &::Irq[aHandle];
714 if (pI->iIState & NIrq::ERaw)
720 if (pI->iStaticFlags & NIrq::EShared)
722 anchor = &pI->iHandlers.iA;
723 pH = _LOFF(anchor->iNext, NIrqHandler, iIrqLink);
725 if (i>=NK_MAX_IRQ_HANDLERS)
727 pH2 = &::Handlers[i];
730 if (pH->iIrq != pI || anchor->iPrev != anchor->iNext)
732 aHandle = pH->iHandle;
738 TRACE_IRQ12(7, aHandle, aIrq, aHandler);
742 EXPORT_C TInt NKern::InterruptUnbind(TInt aId)
745 __KTRACE_OPT(KNKERN,DEBUGPRINT(">NKIU: ID=%08x", aId));
746 CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"NKern::InterruptUnbind");
749 TInt r = NIrq::FromHandle(aId, pI, pH);
752 NKern::ThreadEnterCS();
760 r = pH->Unbind(aId, 0);
762 NKern::ThreadLeaveCS();
767 EXPORT_C TInt NKern::InterruptEnable(TInt aId)
769 __KTRACE_OPT(KNKERN,DEBUGPRINT(">NKIE: ID=%08x", aId));
773 TInt r = NIrq::FromHandle(aId, pI, pH);
775 r = pH ? pH->Enable(aId) : pI->EnableRaw();
780 EXPORT_C TInt NKern::InterruptDisable(TInt aId)
782 __KTRACE_OPT(KNKERN,DEBUGPRINT(">NKID: ID=%08x", aId));
786 TInt r = NIrq::FromHandle(aId, pI, pH);
788 r = pH ? pH->Disable(FALSE, aId) : pI->DisableRaw(FALSE);
793 EXPORT_C TInt NKern::InterruptClear(TInt aId)
795 __KTRACE_OPT(KNKERN,DEBUGPRINT(">NKIC: ID=%08x", aId));
796 return KErrNotSupported;
799 EXPORT_C TInt NKern::InterruptSetPriority(TInt aId, TInt aPri)
801 __KTRACE_OPT(KNKERN,DEBUGPRINT(">NKIS: ID=%08x PRI=%08x", aId, aPri));
802 return KErrNotSupported;
805 EXPORT_C TInt NKern::InterruptSetCpuMask(TInt aId, TUint32 aMask)
807 __KTRACE_OPT(KNKERN,DEBUGPRINT(">NKIM: ID=%08x M=%08x", aId, aMask));
808 return KErrNotSupported;
811 EXPORT_C void NKern::Interrupt(TInt aId)
813 __NK_ASSERT_ALWAYS(TUint(aId) < TUint(NK_MAX_IRQS));
814 NIrq* pI = &Irq[aId];