123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832 |
- /*++
- Copyright (c) 2012 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- amlos.c
- Abstract:
- This module implements operating system support functions for the ACPI AML
- interpreter and namespace.
- Author:
- Evan Green 13-Nov-2012
- Environment:
- Kernel
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/kernel/driver.h>
- #include "acpiobj.h"
- #include "amlos.h"
- #include "amlops.h"
- #include "namespce.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // Define the allocation tag for ACPI AMLallocations.
- //
- #define ACPI_AML_ALLOCATION_TAG 0x696C6D41 // 'ilmA'
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- /*++
- Structure Description:
- This structure stores the implementation of an ACPI mutex object.
- Members:
- QueuedLock - Stores a pointer to the OS queued lock implementing the
- synchronization primitive.
- OwningContext - Stores a pointer to the execution context (thread) that
- has the lock acquired.
- RecursionCount - Stores the number of acquire calls that have been made.
- SyncLevel - Stores the sync level of this mutex.
- PreviousSyncLevel - Stores the sync level of the execution context
- immediately before the acquire call was made.
- --*/
- typedef struct _ACPI_MUTEX {
- PQUEUED_LOCK QueuedLock;
- PAML_EXECUTION_CONTEXT OwningContext;
- ULONG RecursionCount;
- ULONG SyncLevel;
- ULONG PreviousSyncLevel;
- } ACPI_MUTEX, *PACPI_MUTEX;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- //
- // -------------------------------------------------------------------- Globals
- //
- PQUEUED_LOCK AcpiPciLock = NULL;
- //
- // ------------------------------------------------------------------ Functions
- //
- KSTATUS
- AcpipInitializeOperatingSystemAmlSupport (
- VOID
- )
- /*++
- Routine Description:
- This routine initializes operating system specific support for the AML
- interpreter.
- Arguments:
- None.
- Return Value:
- Status code.
- --*/
- {
- KSTATUS Status;
- AcpiPciLock = KeCreateQueuedLock();
- if (AcpiPciLock == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto InitializeOperatingSystemAmlSupportEnd;
- }
- Status = STATUS_SUCCESS;
- InitializeOperatingSystemAmlSupportEnd:
- return Status;
- }
- PVOID
- AcpipAllocateMemory (
- ULONG Size
- )
- /*++
- Routine Description:
- This routine allocates memory from the operating system for the ACPI
- interpreter and namespace.
- Arguments:
- Size - Supplies the size of the allocation, in bytes.
- Return Value:
- Returns a pointer to the allocated memory on success.
- NULL on failure.
- --*/
- {
- return MmAllocatePagedPool(Size, ACPI_AML_ALLOCATION_TAG);
- }
- VOID
- AcpipFreeMemory (
- PVOID Allocation
- )
- /*++
- Routine Description:
- This routine frees memory allocated for the ACPI AML interpreter and
- namespace.
- Arguments:
- Allocation - Supplies a pointer to the allocated memory.
- Return Value:
- None.
- --*/
- {
- MmFreePagedPool(Allocation);
- return;
- }
- VOID
- AcpipFatalError (
- ULONGLONG Parameter1,
- ULONGLONG Parameter2,
- ULONGLONG Parameter3,
- ULONGLONG Parameter4
- )
- /*++
- Routine Description:
- This routine takes the system down as gracefully as possible.
- Arguments:
- Parameter1 - Supplies an optional parameter.
- Parameter2 - Supplies an optional parameter.
- Parameter3 - Supplies an optional parameter.
- Parameter4 - Supplies an optional parameter.
- Return Value:
- This function does not return.
- --*/
- {
- KeCrashSystem(CRASH_ACPI_FAILURE,
- Parameter1,
- Parameter2,
- Parameter3,
- Parameter4);
- }
- VOID
- AcpipSleep (
- ULONG Milliseconds
- )
- /*++
- Routine Description:
- This routine delays the current thread's execution by at least the given
- number of milliseconds (the delays can be significantly longer). During this
- time, other threads will run.
- Arguments:
- Milliseconds - Supplies the minimum number of milliseconds to delay.
- Return Value:
- None.
- --*/
- {
- ASSERT(KeGetRunLevel() == RunLevelLow);
- KeDelayExecution(FALSE, FALSE, Milliseconds * MICROSECONDS_PER_MILLISECOND);
- return;
- }
- VOID
- AcpipBusySpin (
- ULONG Microseconds
- )
- /*++
- Routine Description:
- This routine stalls the current processor by the given number of
- microseconds. This routine busy spins, unless preemption occurs no other
- threads will run during this delay.
- Arguments:
- Microseconds - Supplies the minimum number of microseconds to delay.
- Return Value:
- None.
- --*/
- {
- HlBusySpin(Microseconds);
- return;
- }
- ULONGLONG
- AcpipGetTimerValue (
- )
- /*++
- Routine Description:
- This routine returns a monotomically non-decreasing value representing the
- number of hundred nanosecond units that have elapsed since some epoch in
- the past (could be system boot).
- Arguments:
- None.
- Return Value:
- Returns the number of hundred nanosecond units (10^-7 seconds) that have
- elapsed.
- --*/
- {
- ULONGLONG Frequency;
- ULONGLONG Value;
- Frequency = HlQueryTimeCounterFrequency();
- Value = HlQueryTimeCounter();
- //
- // Scale to hundred nanosecond units.
- //
- Value = (Value * (NANOSECONDS_PER_SECOND / 100)) / Frequency;
- return Value;
- }
- PVOID
- AcpipCreateMutex (
- ULONG SyncLevel
- )
- /*++
- Routine Description:
- This routine creates an operating system mutex object to back an ACPI mutex
- used in the AML interpreter.
- Arguments:
- SyncLevel - Supplies the ACPI-defined sync level of the mutex.
- Return Value:
- Returns a pointer to the mutex object on success.
- NULL on failure.
- --*/
- {
- PACPI_MUTEX Mutex;
- KSTATUS Status;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- Mutex = AcpipAllocateMemory(sizeof(ACPI_MUTEX));
- if (Mutex == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto CreateMutexEnd;
- }
- RtlZeroMemory(Mutex, sizeof(ACPI_MUTEX));
- Mutex->SyncLevel = SyncLevel;
- Mutex->QueuedLock = KeCreateQueuedLock();
- if (Mutex->QueuedLock == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto CreateMutexEnd;
- }
- Status = STATUS_SUCCESS;
- CreateMutexEnd:
- if (!KSUCCESS(Status)) {
- if (Mutex != NULL) {
- if (Mutex->QueuedLock != NULL) {
- KeDestroyQueuedLock(Mutex->QueuedLock);
- }
- AcpipFreeMemory(Mutex);
- Mutex = NULL;
- }
- }
- return Mutex;
- }
- VOID
- AcpipDestroyMutex (
- PVOID Mutex
- )
- /*++
- Routine Description:
- This routine destroys an operating system mutex object.
- Arguments:
- Mutex - Supplies a pointer to the OS mutex object returned during the
- create mutex routine.
- Return Value:
- None.
- --*/
- {
- PACPI_MUTEX AcpiMutex;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- ASSERT(Mutex != NULL);
- AcpiMutex = (PACPI_MUTEX)Mutex;
- ASSERT(AcpiMutex->OwningContext == NULL);
- KeDestroyQueuedLock(AcpiMutex->QueuedLock);
- AcpipFreeMemory(AcpiMutex);
- return;
- }
- BOOL
- AcpipAcquireMutex (
- PAML_EXECUTION_CONTEXT Context,
- PVOID Mutex,
- ULONG TimeoutInMilliseconds
- )
- /*++
- Routine Description:
- This routine attempts to acquire a mutex object.
- Arguments:
- Context - Supplies a pointer to the execution context.
- Mutex - Supplies a pointer to the mutex to acquire.
- TimeoutInMilliseconds - Supplies the number of milliseconds to wait before
- returning anyway and timing out (failing the acquire).
- Return Value:
- TRUE if the timeout occurred and the mutex was not acquired.
- FALSE if the mutex was successfully acquired.
- --*/
- {
- PACPI_MUTEX AcpiMutex;
- KSTATUS Status;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- AcpiMutex = (PACPI_MUTEX)Mutex;
- //
- // ACPI dictates that mutexes must be acquired in order by sync level.
- // This assert indicates bad firmware has attempted to acquire two mutexes
- // in the wrong order.
- //
- ASSERT(Context->SyncLevel <= AcpiMutex->SyncLevel);
- if (AcpiMutex->OwningContext == Context) {
- AcpiMutex->RecursionCount += 1;
- return TRUE;
- }
- if (TimeoutInMilliseconds == ACPI_MUTEX_WAIT_INDEFINITELY) {
- TimeoutInMilliseconds = WAIT_TIME_INDEFINITE;
- }
- Status = KeAcquireQueuedLockTimed(AcpiMutex->QueuedLock,
- TimeoutInMilliseconds);
- if (!KSUCCESS(Status)) {
- return FALSE;
- }
- //
- // Save the previous sync level in the mutex and set the sync level to that
- // of the mutex.
- //
- AcpiMutex->OwningContext = Context;
- AcpiMutex->PreviousSyncLevel = Context->SyncLevel;
- Context->SyncLevel = AcpiMutex->SyncLevel;
- return TRUE;
- }
- VOID
- AcpipReleaseMutex (
- PAML_EXECUTION_CONTEXT Context,
- PVOID Mutex
- )
- /*++
- Routine Description:
- This routine releases an acquired mutex object. This object must have been
- successfully acquired using the acquire routine.
- Arguments:
- Context - Supplies a pointer to the execution context.
- Mutex - Supplies a pointer to the mutex to release.
- Return Value:
- None.
- --*/
- {
- PACPI_MUTEX AcpiMutex;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- AcpiMutex = (PACPI_MUTEX)Mutex;
- //
- // This assert fires when ACPI firmware attempts to release a mutex it
- // never acquired (or release more times than it acquired, as the mutex is
- // recursive).
- //
- ASSERT(AcpiMutex->OwningContext == Context);
- ASSERT(Context->SyncLevel == AcpiMutex->SyncLevel);
- //
- // If this is an inner recursive release, just decrement the count and
- // return.
- //
- if (AcpiMutex->RecursionCount != 0) {
- ASSERT(AcpiMutex->RecursionCount < 0x10000000);
- AcpiMutex->RecursionCount -= 1;
- return;
- }
- //
- // Clear the owning context and restore the sync level. Once this routine
- // is out of the mutex structure, drop the real lock that others are
- // blocked on.
- //
- AcpiMutex->OwningContext = NULL;
- Context->SyncLevel = AcpiMutex->PreviousSyncLevel;
- KeReleaseQueuedLock(AcpiMutex->QueuedLock);
- return;
- }
- PVOID
- AcpipCreateEvent (
- )
- /*++
- Routine Description:
- This routine creates an operating system event object to back an ACPI Event
- used in the AML interpreter.
- Arguments:
- None.
- Return Value:
- Returns a pointer to the event object on success.
- NULL on failure.
- --*/
- {
- PKEVENT Event;
- Event = KeCreateEvent(NULL);
- return Event;
- }
- VOID
- AcpipDestroyEvent (
- PVOID Event
- )
- /*++
- Routine Description:
- This routine destroys an operating system event object.
- Arguments:
- Event - Supplies a pointer to the OS event object returned during the
- create event routine.
- Return Value:
- None.
- --*/
- {
- KeDestroyEvent(Event);
- return;
- }
- BOOL
- AcpipWaitForEvent (
- PVOID Event,
- ULONG TimeoutInMilliseconds
- )
- /*++
- Routine Description:
- This routine waits at least the specified number of milliseconds for the
- given event object.
- Arguments:
- Event - Supplies a pointer to the event to wait for.
- TimeoutInMilliseconds - Supplies the number of milliseconds to wait before
- returning anyway and timing out (failing the wait).
- Return Value:
- TRUE if the timeout occurred and the event was not acquired.
- FALSE if execution continued because the event was signaled.
- --*/
- {
- KSTATUS Status;
- Status = KeWaitForEvent(Event, FALSE, TimeoutInMilliseconds);
- if (!KSUCCESS(Status)) {
- return FALSE;
- }
- return TRUE;
- }
- VOID
- AcpipSignalEvent (
- PVOID Event
- )
- /*++
- Routine Description:
- This routine signals an event, releasing all parties waiting on it.
- Arguments:
- Event - Supplies a pointer to the event to signal.
- Return Value:
- None.
- --*/
- {
- KeSignalEvent(Event, SignalOptionSignalAll);
- return;
- }
- VOID
- AcpipResetEvent (
- PVOID Event
- )
- /*++
- Routine Description:
- This routine resets an event back to its unsignaled state, causing any
- party who subsequently waits on this event to block.
- Arguments:
- Event - Supplies a pointer to the event to unsignal.
- Return Value:
- None.
- --*/
- {
- KeSignalEvent(Event, SignalOptionUnsignal);
- return;
- }
- KSTATUS
- AcpipNotifyOperatingSystem (
- PACPI_OBJECT Object,
- ULONGLONG NotificationValue
- )
- /*++
- Routine Description:
- This routine is called by executing AML code to notify the operating system
- of something.
- Arguments:
- Object - Supplies the object generating the notification. This object will
- be of type Processor, Thermal Zone, or Device.
- NotificationValue - Supplies the type of notification being sent.
- Return Value:
- Status code.
- --*/
- {
- RtlDebugPrint("ACPI: OS Notify 0x%I64x!\n", NotificationValue);
- ASSERT(FALSE);
- return STATUS_NOT_IMPLEMENTED;
- }
- VOID
- AcpipAcquirePciLock (
- )
- /*++
- Routine Description:
- This routine acquires the PCI lock, used to synchronize early access to
- PCI configuration space with the PCI driver actually coming online.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- //
- // This routine is expecting only to be called at low run level, as it
- // does not raise to acquire.
- //
- ASSERT(KeGetRunLevel() == RunLevelLow);
- KeAcquireQueuedLock(AcpiPciLock);
- return;
- }
- VOID
- AcpipReleasePciLock (
- )
- /*++
- Routine Description:
- This routine releases the PCI lock, used to synchronize early access to
- PCI configuration space with the PCI driver actually coming online.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- //
- // This routine is expecting only to be called at low run level, as it
- // does not raise to acquire.
- //
- ASSERT(KeGetRunLevel() == RunLevelLow);
- KeReleaseQueuedLock(AcpiPciLock);
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|