123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308 |
- /*++
- Copyright (c) 2013 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:
- fixedreg.c
- Abstract:
- This module implements support for accessing ACPI fixed hardware, which
- is a mess because there are so many different ways to both access and
- specify the register locations.
- Author:
- Evan Green 21-Nov-2013
- Environment:
- Kernel
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/kernel/driver.h>
- #include "acpip.h"
- #include "amlos.h"
- #include "fixedreg.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // Define the number of seconds to wait for the global lock before taking
- // the system down.
- //
- #define ACPI_GLOBAL_LOCK_TIMEOUT 60
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- KSTATUS
- AcpipReadFixedRegister (
- ULONG AddressA,
- ULONG AddressB,
- UCHAR AddressLength,
- ULONG GenericAddressAOffset,
- ULONG GenericAddressBOffset,
- PVOID *MappedAddressA,
- PVOID *MappedAddressB,
- PULONG MappedSizeA,
- PULONG MappedSizeB,
- PULONG Value
- );
- KSTATUS
- AcpipWriteFixedRegister (
- ULONG AddressA,
- ULONG AddressB,
- UCHAR AddressLength,
- ULONG GenericAddressAOffset,
- ULONG GenericAddressBOffset,
- PVOID *MappedAddressA,
- PVOID *MappedAddressB,
- PULONG MappedSizeA,
- PULONG MappedSizeB,
- ULONG Value
- );
- KSTATUS
- AcpipReadGenericAddressFixedRegister (
- PVOID *MappedAddress,
- PULONG MappedSize,
- PGENERIC_ADDRESS GenericAddress,
- PULONG Value
- );
- KSTATUS
- AcpipWriteGenericAddressFixedRegister (
- PVOID *MappedAddress,
- PULONG MappedSize,
- PGENERIC_ADDRESS GenericAddress,
- ULONG Value
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Store a pointer to the PM1 control block.
- //
- PVOID AcpiPm1aControlRegister = NULL;
- ULONG AcpiPm1aControlRegisterSize = 0;
- PVOID AcpiPm1bControlRegister = NULL;
- ULONG AcpiPm1bControlRegisterSize = 0;
- PVOID AcpiPm2ControlRegister = NULL;
- ULONG AcpiPm2ControlRegisterSize = 0;
- PVOID AcpiPm1aEventRegister = NULL;
- ULONG AcpiPm1aEventRegisterSize = 0;
- PVOID AcpiPm1bEventRegister = NULL;
- ULONG AcpiPm1bEventRegisterSize = 0;
- //
- // Store a pointer to the lock that protects the global lock.
- //
- PQUEUED_LOCK AcpiGlobalLock = NULL;
- //
- // Store a pointer to the FACS table.
- //
- PFACS AcpiFacsTable = NULL;
- //
- // ------------------------------------------------------------------ Functions
- //
- KSTATUS
- AcpipReadPm1ControlRegister (
- PULONG Value
- )
- /*++
- Routine Description:
- This routine reads the PM1 control register.
- Arguments:
- Value - Supplies a pointer where the value will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- PFADT Fadt;
- KSTATUS Status;
- Fadt = AcpiFadtTable;
- if (Fadt == NULL) {
- return STATUS_NOT_SUPPORTED;
- }
- Status = AcpipReadFixedRegister(Fadt->Pm1aControlBlock,
- Fadt->Pm1bControlBlock,
- Fadt->Pm1ControlLength,
- FIELD_OFFSET(FADT, XPm1aControlBlock),
- FIELD_OFFSET(FADT, XPm1bControlBlock),
- &AcpiPm1aControlRegister,
- &AcpiPm1bControlRegister,
- &AcpiPm1aControlRegisterSize,
- &AcpiPm1bControlRegisterSize,
- Value);
- return Status;
- }
- KSTATUS
- AcpipWritePm1ControlRegister (
- ULONG Value
- )
- /*++
- Routine Description:
- This routine writes to the PM1 control register.
- Arguments:
- Value - Supplies the value to write.
- Return Value:
- Status code.
- --*/
- {
- PFADT Fadt;
- KSTATUS Status;
- Fadt = AcpiFadtTable;
- if (Fadt == NULL) {
- return STATUS_NOT_SUPPORTED;
- }
- Status = AcpipWriteFixedRegister(Fadt->Pm1aControlBlock,
- Fadt->Pm1bControlBlock,
- Fadt->Pm1ControlLength,
- FIELD_OFFSET(FADT, XPm1aControlBlock),
- FIELD_OFFSET(FADT, XPm1bControlBlock),
- &AcpiPm1aControlRegister,
- &AcpiPm1bControlRegister,
- &AcpiPm1aControlRegisterSize,
- &AcpiPm1bControlRegisterSize,
- Value);
- return Status;
- }
- KSTATUS
- AcpipReadPm2ControlRegister (
- PULONG Value
- )
- /*++
- Routine Description:
- This routine reads the PM2 control register.
- Arguments:
- Value - Supplies a pointer where the value will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- PFADT Fadt;
- KSTATUS Status;
- Fadt = AcpiFadtTable;
- if (Fadt == NULL) {
- return STATUS_NOT_SUPPORTED;
- }
- Status = AcpipReadFixedRegister(Fadt->Pm2ControlBlock,
- 0,
- Fadt->Pm2ControlLength,
- FIELD_OFFSET(FADT, XPm2ControlBlock),
- 0,
- &AcpiPm2ControlRegister,
- NULL,
- &AcpiPm2ControlRegisterSize,
- NULL,
- Value);
- return Status;
- }
- KSTATUS
- AcpipWritePm2ControlRegister (
- ULONG Value
- )
- /*++
- Routine Description:
- This routine writes to the PM2 control register.
- Arguments:
- Value - Supplies the value to write.
- Return Value:
- Status code.
- --*/
- {
- PFADT Fadt;
- KSTATUS Status;
- Fadt = AcpiFadtTable;
- if (Fadt == NULL) {
- return STATUS_NOT_SUPPORTED;
- }
- Status = AcpipWriteFixedRegister(Fadt->Pm2ControlBlock,
- 0,
- Fadt->Pm2ControlLength,
- FIELD_OFFSET(FADT, XPm2ControlBlock),
- 0,
- &AcpiPm2ControlRegister,
- NULL,
- &AcpiPm2ControlRegisterSize,
- NULL,
- Value);
- return Status;
- }
- KSTATUS
- AcpipReadPm1EventRegister (
- PULONG Value
- )
- /*++
- Routine Description:
- This routine reads the PM1 event/status register.
- Arguments:
- Value - Supplies a pointer where the value will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- PFADT Fadt;
- KSTATUS Status;
- Fadt = AcpiFadtTable;
- if (Fadt == NULL) {
- return STATUS_NOT_SUPPORTED;
- }
- Status = AcpipReadFixedRegister(Fadt->Pm1aEventBlock,
- Fadt->Pm1bEventBlock,
- Fadt->Pm1EventLength,
- FIELD_OFFSET(FADT, XPm1aEventBlock),
- FIELD_OFFSET(FADT, XPm1bEventBlock),
- &AcpiPm1aEventRegister,
- &AcpiPm1bEventRegister,
- &AcpiPm1aEventRegisterSize,
- &AcpiPm1bEventRegisterSize,
- Value);
- return Status;
- }
- KSTATUS
- AcpipWritePm1EventRegister (
- ULONG Value
- )
- /*++
- Routine Description:
- This routine writes to the PM1 event register.
- Arguments:
- Value - Supplies the value to write.
- Return Value:
- Status code.
- --*/
- {
- PFADT Fadt;
- KSTATUS Status;
- Fadt = AcpiFadtTable;
- if (Fadt == NULL) {
- return STATUS_NOT_SUPPORTED;
- }
- Status = AcpipWriteFixedRegister(Fadt->Pm1aEventBlock,
- Fadt->Pm1bEventBlock,
- Fadt->Pm1EventLength,
- FIELD_OFFSET(FADT, XPm1aEventBlock),
- FIELD_OFFSET(FADT, XPm1bEventBlock),
- &AcpiPm1aEventRegister,
- &AcpiPm1bEventRegister,
- &AcpiPm1aEventRegisterSize,
- &AcpiPm1bEventRegisterSize,
- Value);
- return Status;
- }
- VOID
- AcpipAcquireGlobalLock (
- VOID
- )
- /*++
- Routine Description:
- This routine acquires the ACPI global lock that coordinates between the
- OSPM and firmware in SMI-land (or in some external controller).
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- ULONG CurrentValue;
- ULONG NewValue;
- ULONG OriginalValue;
- KSTATUS Status;
- ULONGLONG Timeout;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- if (AcpiFacsTable == NULL) {
- return;
- }
- KeAcquireQueuedLock(AcpiGlobalLock);
- Timeout = KeGetRecentTimeCounter() +
- (HlQueryTimeCounterFrequency() * ACPI_GLOBAL_LOCK_TIMEOUT);
- //
- // Loop trying to acquire the lock until the timeout occurs.
- //
- Status = STATUS_TIMEOUT;
- do {
- //
- // Loop trying to get a clean compare-exchange.
- //
- while (TRUE) {
- OriginalValue = AcpiFacsTable->GlobalLock;
- //
- // Clear the pending bit. If the owner bit is set, set the pending
- // bit.
- //
- NewValue = (OriginalValue | FACS_GLOBAL_LOCK_OWNED) &
- (~FACS_GLOBAL_LOCK_PENDING);
- if ((OriginalValue & FACS_GLOBAL_LOCK_OWNED) != 0) {
- NewValue |= FACS_GLOBAL_LOCK_PENDING;
- }
- CurrentValue = RtlAtomicCompareExchange32(
- &(AcpiFacsTable->GlobalLock),
- NewValue,
- OriginalValue);
- if (CurrentValue == OriginalValue) {
- break;
- }
- }
- //
- // If the value shoved in there didn't have the pending bit set, then
- // this routine must have just set the owner bit successfully.
- //
- if ((NewValue & FACS_GLOBAL_LOCK_PENDING) == 0) {
- Status = STATUS_SUCCESS;
- break;
- }
- //
- // Stall for a bit to let the firmware do its thing.
- //
- KeDelayExecution(FALSE, FALSE, MICROSECONDS_PER_MILLISECOND);
- } while (KeGetRecentTimeCounter() <= Timeout);
- //
- // It's serious not to be able to acquire the lock.
- //
- if (!KSUCCESS(Status)) {
- AcpipFatalError(ACPI_CRASH_GLOBAL_LOCK_FAILURE,
- Status,
- (UINTN)AcpiFacsTable,
- 0);
- }
- return;
- }
- VOID
- AcpipReleaseGlobalLock (
- VOID
- )
- /*++
- Routine Description:
- This routine releases the ACPI global lock.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- ULONG CurrentValue;
- ULONG NewValue;
- ULONG OriginalValue;
- KSTATUS Status;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- if (AcpiFacsTable == NULL) {
- return;
- }
- //
- // Loop trying to get a clean compare exchange clearing the owned and
- // pending bits.
- //
- while (TRUE) {
- OriginalValue = AcpiFacsTable->GlobalLock;
- NewValue = OriginalValue &
- (~(FACS_GLOBAL_LOCK_PENDING | FACS_GLOBAL_LOCK_OWNED));
- CurrentValue = RtlAtomicCompareExchange32(&(AcpiFacsTable->GlobalLock),
- NewValue,
- OriginalValue);
- if (CurrentValue == OriginalValue) {
- break;
- }
- }
- //
- // If the firmware wants control, signal to them that it's their turn.
- //
- if ((OriginalValue & FACS_GLOBAL_LOCK_PENDING) != 0) {
- Status = AcpipReadPm1ControlRegister(&OriginalValue);
- if (KSUCCESS(Status)) {
- OriginalValue |= FADT_PM1_CONTROL_GLOBAL_LOCK_RELEASED;
- AcpipWritePm1ControlRegister(OriginalValue);
- }
- }
- KeReleaseQueuedLock(AcpiGlobalLock);
- return;
- }
- KSTATUS
- AcpipInitializeFixedRegisterSupport (
- VOID
- )
- /*++
- Routine Description:
- This routine initializes support for accessing fixed registers.
- Arguments:
- None.
- Return Value:
- Status code.
- --*/
- {
- PHYSICAL_ADDRESS FacsPhysicalAddress;
- PFADT Fadt;
- KSTATUS Status;
- Fadt = AcpiFadtTable;
- if (Fadt != NULL) {
- //
- // Get the physical address of the FACS table.
- //
- FacsPhysicalAddress = 0;
- if (Fadt->Header.Length >=
- FIELD_OFFSET(FADT, XFirmwareControl) + sizeof(ULONGLONG)) {
- FacsPhysicalAddress = Fadt->XFirmwareControl;
- }
- if (FacsPhysicalAddress == 0) {
- FacsPhysicalAddress = Fadt->FirmwareControlAddress;
- }
- //
- // Map the FACS if it's present. Map it cache-disable as it
- // communicates directly to firmware.
- //
- if (FacsPhysicalAddress != 0) {
- AcpiFacsTable = MmMapPhysicalAddress(FacsPhysicalAddress,
- sizeof(FACS),
- TRUE,
- FALSE,
- TRUE);
- if (AcpiFacsTable == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto InitializeFixedRegisterSupportEnd;
- }
- //
- // Also create a global lock to protect the global lock.
- //
- AcpiGlobalLock = KeCreateQueuedLock();
- if (AcpiGlobalLock == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto InitializeFixedRegisterSupportEnd;
- }
- }
- }
- Status = STATUS_SUCCESS;
- InitializeFixedRegisterSupportEnd:
- return Status;
- }
- VOID
- AcpipUnmapFixedRegisters (
- VOID
- )
- /*++
- Routine Description:
- This routine is called before a driver is about to be unloaded from memory.
- It unmaps any mappings created to access the fixed ACPI registers.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- if (AcpiGlobalLock != NULL) {
- KeDestroyQueuedLock(AcpiGlobalLock);
- }
- //
- // Unmap the fixed hardware.
- //
- if (AcpiPm1aControlRegister != NULL) {
- MmUnmapAddress(AcpiPm1aControlRegister, AcpiPm1aControlRegisterSize);
- AcpiPm1aControlRegister = NULL;
- AcpiPm1aControlRegisterSize = 0;
- }
- if (AcpiPm1bControlRegister != NULL) {
- MmUnmapAddress(AcpiPm1bControlRegister, AcpiPm1bControlRegisterSize);
- AcpiPm1bControlRegister = NULL;
- AcpiPm1bControlRegisterSize = 0;
- }
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- KSTATUS
- AcpipReadFixedRegister (
- ULONG AddressA,
- ULONG AddressB,
- UCHAR AddressLength,
- ULONG GenericAddressAOffset,
- ULONG GenericAddressBOffset,
- PVOID *MappedAddressA,
- PVOID *MappedAddressB,
- PULONG MappedSizeA,
- PULONG MappedSizeB,
- PULONG Value
- )
- /*++
- Routine Description:
- This routine reads a fixed register from the FADT.
- Arguments:
- AddressA - Supplies the I/O port address of the A (or only) portion of
- the register.
- AddressB - Supplies the I/O port address of the B portion of the register
- if it exists, or 0 if there is only an A portion.
- AddressLength - Supplies the length of the I/O port address in bytes.
- GenericAddressAOffset - Supplies the offset into the FADT where the
- generic address of the A portion resides, or 0 if there is no
- generic address for this register in the FADT.
- GenericAddressBOffset - Supplies the offset into the FADT where the
- generic address of the B portion resides, or 0 if there is no B portion.
- MappedAddressA - Supplies a pointer that on input contains the mapped
- address of the register A for memory mapped addresses. If the value of
- this is NULL and the generic address is memory backed, it will be
- mapped in this routine and the virtual address stored here.
- MappedAddressB - Supplies a pointer that on input contains the mapped
- address of the register B for memory mapped addresses. If the value of
- this is NULL and the generic address is memory backed, it will be
- mapped in this routine and the virtual address stored here.
- MappedSizeA - Supplies a pointer that on input contains the size of the
- virtual mapping for this address. If this routine maps the register,
- it will fill in the size here.
- MappedSizeB - Supplies a pointer that on input contains the size of the
- virtual mapping for this address. If this routine maps the register,
- it will fill in the size here.
- Value - Supplies a pointer where the value will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- PFADT Fadt;
- PGENERIC_ADDRESS GenericAddressA;
- PGENERIC_ADDRESS GenericAddressB;
- ULONG MaxOffset;
- KSTATUS Status;
- ULONG ValueA;
- ULONG ValueB;
- Fadt = AcpiFadtTable;
- if (Fadt == NULL) {
- return STATUS_NOT_SUPPORTED;
- }
- ValueA = 0;
- ValueB = 0;
- //
- // Use the extended value if it's there.
- //
- GenericAddressA = (PVOID)Fadt + GenericAddressAOffset;
- GenericAddressB = (PVOID)Fadt + GenericAddressBOffset;
- MaxOffset = GenericAddressAOffset;
- if (GenericAddressBOffset > GenericAddressAOffset) {
- MaxOffset = GenericAddressBOffset;
- }
- MaxOffset += sizeof(GENERIC_ADDRESS);
- if ((Fadt->Header.Length >= MaxOffset) &&
- (GenericAddressA->Address != 0)) {
- Status = AcpipReadGenericAddressFixedRegister(MappedAddressA,
- MappedSizeA,
- GenericAddressA,
- &ValueA);
- if (!KSUCCESS(Status)) {
- return Status;
- }
- if (GenericAddressB->Address != 0) {
- Status = AcpipReadGenericAddressFixedRegister(MappedAddressB,
- MappedSizeB,
- GenericAddressB,
- &ValueB);
- if (!KSUCCESS(Status)) {
- return Status;
- }
- }
- *Value = ValueA | ValueB;
- return STATUS_SUCCESS;
- }
- //
- // Use the old fashioned values.
- //
- if (AddressLength == 0) {
- return STATUS_NOT_SUPPORTED;
- }
- ASSERT(AddressA != 0);
- switch (AddressLength) {
- case 1:
- ValueA = HlIoPortInByte(AddressA);
- if (AddressB != 0) {
- ValueB = HlIoPortInByte(AddressB);
- }
- break;
- case 2:
- ValueA = HlIoPortInShort(AddressA);
- if (AddressB != 0) {
- ValueB = HlIoPortInShort(AddressB);
- }
- break;
- case 4:
- ValueA = HlIoPortInLong(AddressA);
- if (AddressB != 0) {
- ValueB = HlIoPortInLong(AddressB);
- }
- break;
- default:
- ASSERT(FALSE);
- return STATUS_NOT_SUPPORTED;
- }
- *Value = ValueA | ValueB;
- return STATUS_SUCCESS;
- }
- KSTATUS
- AcpipWriteFixedRegister (
- ULONG AddressA,
- ULONG AddressB,
- UCHAR AddressLength,
- ULONG GenericAddressAOffset,
- ULONG GenericAddressBOffset,
- PVOID *MappedAddressA,
- PVOID *MappedAddressB,
- PULONG MappedSizeA,
- PULONG MappedSizeB,
- ULONG Value
- )
- /*++
- Routine Description:
- This routine writes to a fixed register from the FADT.
- Arguments:
- AddressA - Supplies the I/O port address of the A (or only) portion of
- the register.
- AddressB - Supplies the I/O port address of the B portion of the register
- if it exists, or 0 if there is only an A portion.
- AddressLength - Supplies the length of the I/O port address in bytes.
- GenericAddressAOffset - Supplies the offset into the FADT where the
- generic address of the A portion resides, or 0 if there is no
- generic address for this register in the FADT.
- GenericAddressBOffset - Supplies the offset into the FADT where the
- generic address of the B portion resides, or 0 if there is no B portion.
- MappedAddressA - Supplies a pointer that on input contains the mapped
- address of the register A for memory mapped addresses. If the value of
- this is NULL and the generic address is memory backed, it will be
- mapped in this routine and the virtual address stored here.
- MappedAddressB - Supplies a pointer that on input contains the mapped
- address of the register B for memory mapped addresses. If the value of
- this is NULL and the generic address is memory backed, it will be
- mapped in this routine and the virtual address stored here.
- MappedSizeA - Supplies a pointer that on input contains the size of the
- virtual mapping for this address. If this routine maps the register,
- it will fill in the size here.
- MappedSizeB - Supplies a pointer that on input contains the size of the
- virtual mapping for this address. If this routine maps the register,
- it will fill in the size here.
- Value - Supplies the value to write.
- Return Value:
- Status code.
- --*/
- {
- PFADT Fadt;
- PGENERIC_ADDRESS GenericAddressA;
- PGENERIC_ADDRESS GenericAddressB;
- ULONG MaxOffset;
- KSTATUS Status;
- Fadt = AcpiFadtTable;
- if (Fadt == NULL) {
- return STATUS_NOT_SUPPORTED;
- }
- //
- // Use the extended value if it's there.
- //
- GenericAddressA = (PVOID)Fadt + GenericAddressAOffset;
- GenericAddressB = (PVOID)Fadt + GenericAddressBOffset;
- MaxOffset = GenericAddressAOffset;
- if (GenericAddressBOffset > GenericAddressAOffset) {
- MaxOffset = GenericAddressBOffset;
- }
- MaxOffset += sizeof(GENERIC_ADDRESS);
- if ((Fadt->Header.Length >= MaxOffset) &&
- (GenericAddressA->Address != 0)) {
- Status = AcpipWriteGenericAddressFixedRegister(MappedAddressA,
- MappedSizeA,
- GenericAddressA,
- Value);
- if (!KSUCCESS(Status)) {
- return Status;
- }
- if (GenericAddressB->Address != 0) {
- Status = AcpipWriteGenericAddressFixedRegister(MappedAddressB,
- MappedSizeB,
- GenericAddressB,
- Value);
- if (!KSUCCESS(Status)) {
- return Status;
- }
- }
- return STATUS_SUCCESS;
- }
- //
- // Use the old fashioned values.
- //
- if (AddressLength == 0) {
- return STATUS_NOT_SUPPORTED;
- }
- ASSERT(AddressA != 0);
- switch (AddressLength) {
- case 1:
- HlIoPortOutByte(AddressA, (UCHAR)Value);
- if (AddressB != 0) {
- HlIoPortOutByte(AddressB, Value);
- }
- break;
- case 2:
- HlIoPortOutShort(AddressA, (USHORT)Value);
- if (AddressB != 0) {
- HlIoPortOutShort(AddressB, (USHORT)Value);
- }
- break;
- case 4:
- HlIoPortOutLong(AddressA, Value);
- if (AddressB != 0) {
- HlIoPortOutLong(AddressB, Value);
- }
- break;
- default:
- ASSERT(FALSE);
- return STATUS_NOT_SUPPORTED;
- }
- return STATUS_SUCCESS;
- }
- KSTATUS
- AcpipReadGenericAddressFixedRegister (
- PVOID *MappedAddress,
- PULONG MappedSize,
- PGENERIC_ADDRESS GenericAddress,
- PULONG Value
- )
- /*++
- Routine Description:
- This routine reads a register value from a generic address.
- Arguments:
- MappedAddress - Supplies a pointer that on input contains the mapped
- address of the register for memory mapped addresses. If the value of
- this is NULL and the generic address is memory backed, it will be
- mapped in this routine and the virtual address stored here.
- MappedSize - Supplies a pointer that on input contains the size of the
- virtual mapping for this address. If this routine maps the register,
- it will fill in the size here.
- GenericAddress - Supplies a pointer to the generic address detailing the
- location of the register.
- Value - Supplies a pointer where the value will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- USHORT AccessSize;
- USHORT IoPortAddress;
- //
- // Deal with odd offsets if needed.
- //
- ASSERT(GenericAddress->RegisterBitOffset == 0);
- AccessSize = GenericAddress->AccessSize;
- if (AccessSize == 0) {
- ASSERT(GenericAddress->RegisterBitWidth != 0);
- AccessSize = GenericAddress->RegisterBitWidth / BITS_PER_BYTE;
- }
- switch (GenericAddress->AddressSpaceId) {
- case AddressSpaceMemory:
- if (*MappedAddress == NULL) {
- *MappedAddress = MmMapPhysicalAddress(GenericAddress->Address,
- GenericAddress->AccessSize,
- TRUE,
- FALSE,
- TRUE);
- if (*MappedAddress == NULL) {
- return STATUS_INSUFFICIENT_RESOURCES;
- }
- *MappedSize = GenericAddress->AccessSize;
- }
- switch (AccessSize) {
- case 1:
- *Value = HlReadRegister8(*MappedAddress);
- break;
- case 2:
- *Value = HlReadRegister16(*MappedAddress);
- break;
- case 4:
- *Value = HlReadRegister32(*MappedAddress);
- break;
- default:
- ASSERT(FALSE);
- return STATUS_NOT_SUPPORTED;
- }
- break;
- case AddressSpaceIo:
- IoPortAddress = (USHORT)(GenericAddress->Address);
- switch (AccessSize) {
- case 1:
- *Value = HlIoPortInByte(IoPortAddress);
- break;
- case 2:
- *Value = HlIoPortInShort(IoPortAddress);
- break;
- case 4:
- *Value = HlIoPortInLong(IoPortAddress);
- break;
- default:
- ASSERT(FALSE);
- return STATUS_NOT_SUPPORTED;
- }
- break;
- //
- // Implement other types if needed.
- //
- default:
- ASSERT(FALSE);
- return STATUS_NOT_IMPLEMENTED;
- }
- return STATUS_SUCCESS;
- }
- KSTATUS
- AcpipWriteGenericAddressFixedRegister (
- PVOID *MappedAddress,
- PULONG MappedSize,
- PGENERIC_ADDRESS GenericAddress,
- ULONG Value
- )
- /*++
- Routine Description:
- This routine writes a register value to a generic address.
- Arguments:
- MappedAddress - Supplies a pointer that on input contains the mapped
- address of the register for memory mapped addresses. If the value of
- this is NULL and the generic address is memory backed, it will be
- mapped in this routine and the virtual address stored here.
- MappedSize - Supplies a pointer that on input contains the size of the
- virtual mapping for this address. If this routine maps the register,
- it will fill in the size here.
- GenericAddress - Supplies a pointer to the generic address detailing the
- location of the register.
- Value - Supplies the value to write.
- Return Value:
- Status code.
- --*/
- {
- USHORT AccessSize;
- USHORT IoPortAddress;
- //
- // Deal with odd offsets if needed.
- //
- ASSERT(GenericAddress->RegisterBitOffset == 0);
- AccessSize = GenericAddress->AccessSize;
- if (AccessSize == 0) {
- ASSERT(GenericAddress->RegisterBitWidth != 0);
- AccessSize = GenericAddress->RegisterBitWidth / BITS_PER_BYTE;
- } else {
- ASSERT(GenericAddress->RegisterBitWidth ==
- GenericAddress->AccessSize * BITS_PER_BYTE);
- }
- switch (GenericAddress->AddressSpaceId) {
- case AddressSpaceMemory:
- if (*MappedAddress == NULL) {
- *MappedAddress = MmMapPhysicalAddress(GenericAddress->Address,
- GenericAddress->AccessSize,
- TRUE,
- FALSE,
- TRUE);
- if (*MappedAddress == NULL) {
- return STATUS_INSUFFICIENT_RESOURCES;
- }
- *MappedSize = GenericAddress->AccessSize;
- }
- switch (AccessSize) {
- case 1:
- HlWriteRegister8(*MappedAddress, (UCHAR)Value);
- break;
- case 2:
- HlWriteRegister16(*MappedAddress, (USHORT)Value);
- break;
- case 4:
- HlWriteRegister32(*MappedAddress, Value);
- break;
- default:
- ASSERT(FALSE);
- return STATUS_NOT_SUPPORTED;
- }
- break;
- case AddressSpaceIo:
- IoPortAddress = (USHORT)(GenericAddress->Address);
- switch (AccessSize) {
- case 1:
- HlIoPortOutByte(IoPortAddress, (UCHAR)Value);
- break;
- case 2:
- HlIoPortOutShort(IoPortAddress, (USHORT)Value);
- break;
- case 4:
- HlIoPortOutLong(IoPortAddress, Value);
- break;
- default:
- ASSERT(FALSE);
- return STATUS_NOT_SUPPORTED;
- }
- break;
- //
- // Implement other types if needed.
- //
- default:
- ASSERT(FALSE);
- return STATUS_NOT_IMPLEMENTED;
- }
- return STATUS_SUCCESS;
- }
|