3 Copyright (c) 2006 - 2007, Intel Corporation<BR>
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 The implementation for EFI_ISA_IO_PROTOCOL.
23 // Include common header file for this module.
25 #include "InternalIsaIo.h"
27 #include <IndustryStandard/Pcat.h>
30 // Driver Support Global Variables
32 EFI_ISA_IO_PROTOCOL IsaIoInterface
= {
52 static EFI_ISA_DMA_REGISTERS DmaRegisters
[8] = {
77 }, // Channel 4 is invalid
96 ReportErrorStatusCode (
97 EFI_STATUS_CODE_VALUE Code
103 report a error Status code of PCI bus driver controller
107 Code - The error status code.
111 EFI_SUCCESS - Success to report status code.
116 return REPORT_STATUS_CODE (
117 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
123 // Driver Support Functions
127 InitializeIsaIoInstance (
128 IN ISA_IO_DEVICE
*IsaIoDevice
,
129 IN EFI_ISA_ACPI_RESOURCE_LIST
*IsaDeviceResourceList
135 Initializes an ISA I/O Instance
139 IsaIoDevice - The iso device to be initialized.
140 IsaDeviceResourceList - The resource list.
144 EFI_SUCCESS - Initial success.
149 // Initializes an ISA I/O Instance
154 sizeof (EFI_ISA_IO_PROTOCOL
)
157 IsaIoDevice
->IsaIo
.ResourceList
= IsaDeviceResourceList
;
165 IN EFI_ISA_IO_PROTOCOL
*This
,
166 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
175 Performs an ISA I/O Read Cycle
179 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
180 Width - Signifies the width of the I/O operation.
181 Offset - The offset in ISA I/O space to start the I/O operation.
182 Count - The number of I/O operations to perform.
183 Buffer - The destination buffer to store the results
187 EFI_SUCCESS - The data was read from the device sucessfully.
188 EFI_UNSUPPORTED - The Offset is not valid for this device.
189 EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
190 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
195 ISA_IO_DEVICE
*IsaIoDevice
;
197 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
200 // Verify Isa IO Access
202 Status
= IsaIoVerifyAccess (
209 if (EFI_ERROR (Status
)) {
213 // Call PciIo->Io.Read
215 Status
= IsaIoDevice
->PciIo
->Io
.Read (
217 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
218 EFI_PCI_IO_PASS_THROUGH_BAR
,
224 if (EFI_ERROR (Status
)) {
225 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
234 IN EFI_ISA_IO_PROTOCOL
*This
,
235 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
244 Performs an ISA I/O Write Cycle
248 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
249 Width - Signifies the width of the I/O operation.
250 Offset - The offset in ISA I/O space to start the I/O operation.
251 Count - The number of I/O operations to perform.
252 Buffer - The source buffer to write data from
256 EFI_SUCCESS - The data was writen to the device sucessfully.
257 EFI_UNSUPPORTED - The Offset is not valid for this device.
258 EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
259 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
264 ISA_IO_DEVICE
*IsaIoDevice
;
266 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
269 // Verify Isa IO Access
271 Status
= IsaIoVerifyAccess (
278 if (EFI_ERROR (Status
)) {
282 // Call PciIo->Io.Write
284 Status
= IsaIoDevice
->PciIo
->Io
.Write (
286 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
287 EFI_PCI_IO_PASS_THROUGH_BAR
,
293 if (EFI_ERROR (Status
)) {
294 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
302 IN EFI_ISA_IO_PROTOCOL
*This
,
310 Writes an 8 bit I/O Port
314 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
315 Offset - The offset in ISA IO space to start the IO operation.
316 Value - The data to write port.
320 EFI_SUCCESS - Success.
321 EFI_INVALID_PARAMETER - Parameter is invalid.
322 EFI_UNSUPPORTED - The address range specified by Offset is not valid.
323 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
328 ISA_IO_DEVICE
*IsaIoDevice
;
330 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
333 // Call PciIo->Io.Write
335 Status
= IsaIoDevice
->PciIo
->Io
.Write (
338 EFI_PCI_IO_PASS_THROUGH_BAR
,
343 if (EFI_ERROR (Status
)) {
344 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
355 IN EFI_ISA_IO_PROTOCOL
*This
,
356 IN UINT32 AddrOffset
,
357 IN UINT32 PageOffset
,
358 IN UINT32 CountOffset
,
359 IN UINT32 BaseAddress
,
366 Writes I/O operation base address and count number to a 8 bit I/O Port.
370 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
371 AddrOffset - The address' offset.
372 PageOffset - The page's offest.
373 CountOffset - The count's offset.
374 BaseAddress - The base address.
375 Count - The number of I/O operations to perform.
379 EFI_SUCCESS - Success.
380 EFI_INVALID_PARAMETER - Parameter is invalid.
381 EFI_UNSUPPORTED - The address range specified by these Offsets and Count is not valid.
382 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
388 Status
= WritePort (This
, AddrOffset
, (UINT8
) (BaseAddress
& 0xff));
389 if (EFI_ERROR (Status
)) {
393 Status
= WritePort (This
, AddrOffset
, (UINT8
) ((BaseAddress
>> 8) & 0xff));
394 if (EFI_ERROR (Status
)) {
398 Status
= WritePort (This
, PageOffset
, (UINT8
) ((BaseAddress
>> 16) & 0xff));
399 if (EFI_ERROR (Status
)) {
403 Status
= WritePort (This
, CountOffset
, (UINT8
) (Count
& 0xff));
404 if (EFI_ERROR (Status
)) {
408 Status
= WritePort (This
, CountOffset
, (UINT8
) ((Count
>> 8) & 0xff));
409 if (EFI_ERROR (Status
)) {
419 IN EFI_ISA_IO_PROTOCOL
*This
,
426 Unmaps a memory region for DMA
430 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
431 Mapping - The mapping value returned from EFI_ISA_IO.Map().
435 EFI_SUCCESS - The range was unmapped.
436 EFI_DEVICE_ERROR - The data was not committed to the target system memory.
440 ISA_MAP_INFO
*IsaMapInfo
;
443 // Unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
445 if (!FeaturePcdGet (PcdIsaBusSupportDma
)) {
446 return EFI_UNSUPPORTED
;
450 // See if the Map() operation associated with this Unmap() required a mapping
451 // buffer.If a mapping buffer was not required, then this function simply
452 // returns EFI_SUCCESS.
454 if (Mapping
!= NULL
) {
456 // Get the MAP_INFO structure from Mapping
458 IsaMapInfo
= (ISA_MAP_INFO
*) Mapping
;
461 // If this is a write operation from the Agent's point of view,
462 // then copy the contents of the mapped buffer into the real buffer
463 // so the processor can read the contents of the real buffer.
465 if (IsaMapInfo
->Operation
== EfiIsaIoOperationBusMasterWrite
) {
467 (VOID
*) (UINTN
) IsaMapInfo
->HostAddress
,
468 (VOID
*) (UINTN
) IsaMapInfo
->MappedHostAddress
,
469 IsaMapInfo
->NumberOfBytes
473 // Free the mapped buffer and the MAP_INFO structure.
475 gBS
->FreePages (IsaMapInfo
->MappedHostAddress
, IsaMapInfo
->NumberOfPages
);
476 gBS
->FreePool (IsaMapInfo
);
485 IN EFI_ISA_IO_PROTOCOL
*This
495 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
499 EFI_SUCCESS - The buffers were flushed.
500 EFI_DEVICE_ERROR - The buffers were not flushed due to a hardware error.
505 ISA_IO_DEVICE
*IsaIoDevice
;
507 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
512 Status
= IsaIoDevice
->PciIo
->Flush (IsaIoDevice
->PciIo
);
514 if (EFI_ERROR (Status
)) {
515 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
523 IN ISA_IO_DEVICE
*IsaIoDevice
,
524 IN ISA_ACCESS_TYPE Type
,
525 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
527 IN OUT UINT32
*Offset
533 Verifies access to an ISA device
537 IsaIoDevice - The ISA device to be verified.
538 Type - The Access type. The input must be either IsaAccessTypeMem or IsaAccessTypeIo.
539 Width - Signifies the width of the memory operation.
540 Count - The number of memory operations to perform.
541 Offset - The offset in ISA memory space to start the memory operation.
545 EFI_SUCCESS - Verify success.
546 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
547 EFI_UNSUPPORTED - The device ont support the access type.
551 EFI_ISA_ACPI_RESOURCE
*Item
;
554 if (Width
< EfiIsaIoWidthUint8
||
555 Width
>= EfiIsaIoWidthMaximum
||
556 Width
== EfiIsaIoWidthReserved
||
557 Width
== EfiIsaIoWidthFifoReserved
||
558 Width
== EfiIsaIoWidthFillReserved
560 return EFI_INVALID_PARAMETER
;
564 // If Width is EfiIsaIoWidthFifoUintX then convert to EfiIsaIoWidthUintX
565 // If Width is EfiIsaIoWidthFillUintX then convert to EfiIsaIoWidthUintX
567 if (Width
>= EfiIsaIoWidthFifoUint8
&& Width
<= EfiIsaIoWidthFifoReserved
) {
571 Width
= (EFI_ISA_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
573 Status
= EFI_UNSUPPORTED
;
574 Item
= IsaIoDevice
->IsaIo
.ResourceList
->ResourceItem
;
575 while (Item
->Type
!= EfiIsaAcpiResourceEndOfList
) {
576 if ((Type
== IsaAccessTypeMem
&& Item
->Type
== EfiIsaAcpiResourceMemory
) ||
577 (Type
== IsaAccessTypeIo
&& Item
->Type
== EfiIsaAcpiResourceIo
)
579 if (*Offset
>= Item
->StartRange
&& (*Offset
+ Count
* (UINT32
)(1 << Width
)) - 1 <= Item
->EndRange
) {
583 if (*Offset
>= Item
->StartRange
&& *Offset
<= Item
->EndRange
) {
584 Status
= EFI_INVALID_PARAMETER
;
597 IN EFI_ISA_IO_PROTOCOL
*This
,
598 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
607 Performs an ISA Memory Read Cycle
611 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
612 Width - Signifies the width of the memory operation.
613 Offset - The offset in ISA memory space to start the memory operation.
614 Count - The number of memory operations to perform.
615 Buffer - The destination buffer to store the results
619 EFI_SUCCESS - The data was read from the device successfully.
620 EFI_UNSUPPORTED - The Offset is not valid for this device.
621 EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
622 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
627 ISA_IO_DEVICE
*IsaIoDevice
;
630 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
633 // So we just return EFI_UNSUPPORTED for these functions.
635 if (!FeaturePcdGet (PcdIsaBusSupportIsaMemory
)) {
636 return EFI_UNSUPPORTED
;
639 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
642 // Verify the Isa Io Access
644 Status
= IsaIoVerifyAccess (
651 if (EFI_ERROR (Status
)) {
655 // Call PciIo->Mem.Read
657 Status
= IsaIoDevice
->PciIo
->Mem
.Read (
659 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
660 EFI_PCI_IO_PASS_THROUGH_BAR
,
666 if (EFI_ERROR (Status
)) {
667 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
676 IN EFI_ISA_IO_PROTOCOL
*This
,
677 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
686 Performs an ISA Memory Write Cycle
690 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
691 Width - Signifies the width of the memory operation.
692 Offset - The offset in ISA memory space to start the memory operation.
693 Count - The number of memory operations to perform.
694 Buffer - The source buffer to write data from
698 EFI_SUCCESS - The data was written to the device sucessfully.
699 EFI_UNSUPPORTED - The Offset is not valid for this device.
700 EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
701 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
706 ISA_IO_DEVICE
*IsaIoDevice
;
709 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
712 // So we just return EFI_UNSUPPORTED for these functions.
714 if (!FeaturePcdGet (PcdIsaBusSupportIsaMemory
)) {
715 return EFI_UNSUPPORTED
;
718 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
721 // Verify Isa IO Access
723 Status
= IsaIoVerifyAccess (
730 if (EFI_ERROR (Status
)) {
734 // Call PciIo->Mem.Write
736 Status
= IsaIoDevice
->PciIo
->Mem
.Write (
738 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
739 EFI_PCI_IO_PASS_THROUGH_BAR
,
745 if (EFI_ERROR (Status
)) {
746 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
755 IN EFI_ISA_IO_PROTOCOL
*This
,
756 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
757 IN UINT32 DestOffset
,
765 Performs an ISA I/O Copy Memory
769 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
770 Width - Signifies the width of the memory copy operation.
771 DestOffset - The offset of the destination
772 SrcOffset - The offset of the source
773 Count - The number of memory copy operations to perform
777 EFI_SUCCESS - The data was copied sucessfully.
778 EFI_UNSUPPORTED - The DestOffset or SrcOffset is not valid for this device.
779 EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
780 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
785 ISA_IO_DEVICE
*IsaIoDevice
;
788 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
791 // So we just return EFI_UNSUPPORTED for these functions.
793 if (!FeaturePcdGet (PcdIsaBusSupportIsaMemory
)) {
794 return EFI_UNSUPPORTED
;
797 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
800 // Verify Isa IO Access for destination and source
802 Status
= IsaIoVerifyAccess (
809 if (EFI_ERROR (Status
)) {
813 Status
= IsaIoVerifyAccess (
820 if (EFI_ERROR (Status
)) {
824 // Call PciIo->CopyMem
826 Status
= IsaIoDevice
->PciIo
->CopyMem (
828 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
829 EFI_PCI_IO_PASS_THROUGH_BAR
,
831 EFI_PCI_IO_PASS_THROUGH_BAR
,
836 if (EFI_ERROR (Status
)) {
837 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
845 IsaIoMap_OnlySupportSlaveReadWrite (
846 IN EFI_ISA_IO_PROTOCOL
*This
,
847 IN EFI_ISA_IO_PROTOCOL_OPERATION Operation
,
848 IN UINT8 ChannelNumber OPTIONAL
,
849 IN UINT32 ChannelAttributes
,
850 IN VOID
*HostAddress
,
851 IN OUT UINTN
*NumberOfBytes
,
852 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
859 Maps a memory region for DMA, note this implementation
860 only supports slave read/write operation to save code size.
864 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
865 Operation - Indicates the type of DMA (slave or bus master), and if
866 the DMA operation is going to read or write to system memory.
867 ChannelNumber - The slave channel number to use for this DMA operation.
868 If Operation and ChannelAttributes shows that this device
869 performs bus mastering DMA, then this field is ignored.
870 The legal range for this field is 0..7.
871 ChannelAttributes - The attributes of the DMA channel to use for this DMA operation
872 HostAddress - The system memory address to map to the device.
873 NumberOfBytes - On input the number of bytes to map. On output the number
874 of bytes that were mapped.
875 DeviceAddress - The resulting map address for the bus master device to use
876 to access the hosts HostAddress.
877 Mapping - A resulting value to pass to EFI_ISA_IO.Unmap().
881 EFI_SUCCESS - The range was mapped for the returned NumberOfBytes.
882 EFI_INVALID_PARAMETER - The Operation or HostAddress is undefined.
883 EFI_UNSUPPORTED - The HostAddress can not be mapped as a common buffer.
884 EFI_DEVICE_ERROR - The system hardware could not map the requested address.
885 EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
890 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
891 ISA_MAP_INFO
*IsaMapInfo
;
893 UINTN MaxNumberOfBytes
;
899 UINT8 DmaChannelMode
;
901 if ((NULL
== This
) ||
902 (NULL
== HostAddress
) ||
903 (NULL
== NumberOfBytes
) ||
904 (NULL
== DeviceAddress
) ||
907 return EFI_INVALID_PARAMETER
;
912 // Initialize the return values to their defaults
917 // Make sure the Operation parameter is valid.
918 // Light IsaIo only supports two operations.
920 if (!(Operation
== EfiIsaIoOperationSlaveRead
||
921 Operation
== EfiIsaIoOperationSlaveWrite
)) {
922 return EFI_INVALID_PARAMETER
;
925 if (ChannelNumber
>= 4) {
927 // The Light IsaIo doesn't support channelNumber larger than 4.
929 return EFI_INVALID_PARAMETER
;
933 // Map the HostAddress to a DeviceAddress.
935 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
936 if ((PhysicalAddress
+*NumberOfBytes
) > ISA_MAX_MEMORY_ADDRESS
) {
938 // Common Buffer operations can not be remapped. If the common buffer
939 // is above 16MB, then it is not possible to generate a mapping, so return
942 if (Operation
== EfiIsaIoOperationBusMasterCommonBuffer
) {
943 return EFI_UNSUPPORTED
;
946 // Allocate an ISA_MAP_INFO structure to remember the mapping when Unmap()
949 IsaMapInfo
= AllocatePool (sizeof (ISA_MAP_INFO
));
950 if (IsaMapInfo
== NULL
) {
952 return EFI_OUT_OF_RESOURCES
;
955 // Return a pointer to the MAP_INFO structure in Mapping
957 *Mapping
= IsaMapInfo
;
960 // Initialize the MAP_INFO structure
962 IsaMapInfo
->Operation
= Operation
;
963 IsaMapInfo
->NumberOfBytes
= *NumberOfBytes
;
964 IsaMapInfo
->NumberOfPages
= EFI_SIZE_TO_PAGES (*NumberOfBytes
);
965 IsaMapInfo
->HostAddress
= PhysicalAddress
;
966 IsaMapInfo
->MappedHostAddress
= ISA_MAX_MEMORY_ADDRESS
- 1;
969 // Allocate a buffer below 16MB to map the transfer to.
971 Status
= gBS
->AllocatePages (
974 IsaMapInfo
->NumberOfPages
,
975 &IsaMapInfo
->MappedHostAddress
977 if (EFI_ERROR (Status
)) {
978 gBS
->FreePool (IsaMapInfo
);
984 // If this is a read operation from the DMA agents's point of view,
985 // then copy the contents of the real buffer into the mapped buffer
986 // so the DMA agent can read the contents of the real buffer.
988 if (Operation
== EfiIsaIoOperationSlaveRead
) {
990 (VOID
*) (UINTN
) IsaMapInfo
->MappedHostAddress
,
991 (VOID
*) (UINTN
) IsaMapInfo
->HostAddress
,
992 IsaMapInfo
->NumberOfBytes
996 // The DeviceAddress is the address of the maped buffer below 16 MB
998 *DeviceAddress
= IsaMapInfo
->MappedHostAddress
;
1001 // The transfer is below 16 MB, so the DeviceAddress is simply the
1004 *DeviceAddress
= PhysicalAddress
;
1008 // Figure out what to program into the DMA Channel Mode Register
1010 DmaMode
= (UINT8
) (B_8237_DMA_CHMODE_INCREMENT
| (ChannelNumber
& 0x03));
1011 if (Operation
== EfiIsaIoOperationSlaveRead
) {
1012 DmaMode
|= V_8237_DMA_CHMODE_MEM2IO
;
1014 DmaMode
|= V_8237_DMA_CHMODE_IO2MEM
;
1017 // We only support EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SINGLE_MODE in simplified IsaIo
1019 DmaMode
|= V_8237_DMA_CHMODE_SINGLE
;
1022 // A Slave DMA transfer can not cross a 64K boundary.
1023 // Compute *NumberOfBytes based on this restriction.
1025 MaxNumberOfBytes
= 0x10000 - ((UINT32
) (*DeviceAddress
) & 0xffff);
1026 if (*NumberOfBytes
> MaxNumberOfBytes
) {
1027 *NumberOfBytes
= MaxNumberOfBytes
;
1030 // Compute the values to program into the BaseAddress and Count registers
1031 // of the Slave DMA controller
1033 BaseAddress
= (UINT32
) (*DeviceAddress
);
1034 Count
= (UINT16
) (*NumberOfBytes
- 1);
1036 // Program the DMA Write Single Mask Register for ChannelNumber
1037 // Clear the DMA Byte Pointer Register
1039 DmaMask
= R_8237_DMA_WRSMSK_CH0_3
;
1040 DmaClear
= R_8237_DMA_CBPR_CH0_3
;
1041 DmaChannelMode
= R_8237_DMA_CHMODE_CH0_3
;
1043 Status
= WritePort (
1046 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
1048 if (EFI_ERROR (Status
)) {
1052 Status
= WritePort (
1055 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
1057 if (EFI_ERROR (Status
)) {
1061 Status
= WritePort (This
, DmaChannelMode
, DmaMode
);
1062 if (EFI_ERROR (Status
)) {
1066 Status
= WriteDmaPort (
1068 DmaRegisters
[ChannelNumber
].Address
,
1069 DmaRegisters
[ChannelNumber
].Page
,
1070 DmaRegisters
[ChannelNumber
].Count
,
1074 if (EFI_ERROR (Status
)) {
1078 Status
= WritePort (
1081 (UINT8
) (ChannelNumber
& 0x03)
1083 if (EFI_ERROR (Status
)) {
1092 IsaIoMap_FullSupport (
1093 IN EFI_ISA_IO_PROTOCOL
*This
,
1094 IN EFI_ISA_IO_PROTOCOL_OPERATION Operation
,
1095 IN UINT8 ChannelNumber OPTIONAL
,
1096 IN UINT32 ChannelAttributes
,
1097 IN VOID
*HostAddress
,
1098 IN OUT UINTN
*NumberOfBytes
,
1099 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
1104 Routine Description:
1106 Maps a memory region for DMA. This implementation implement the
1107 the full mapping support.
1111 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
1112 Operation - Indicates the type of DMA (slave or bus master), and if
1113 the DMA operation is going to read or write to system memory.
1114 ChannelNumber - The slave channel number to use for this DMA operation.
1115 If Operation and ChannelAttributes shows that this device
1116 performs bus mastering DMA, then this field is ignored.
1117 The legal range for this field is 0..7.
1118 ChannelAttributes - The attributes of the DMA channel to use for this DMA operation
1119 HostAddress - The system memory address to map to the device.
1120 NumberOfBytes - On input the number of bytes to map. On output the number
1121 of bytes that were mapped.
1122 DeviceAddress - The resulting map address for the bus master device to use
1123 - to access the hosts HostAddress.
1124 Mapping - A resulting value to pass to EFI_ISA_IO.Unmap().
1128 EFI_SUCCESS - The range was mapped for the returned NumberOfBytes.
1129 EFI_INVALID_PARAMETER - The Operation or HostAddress is undefined.
1130 EFI_UNSUPPORTED - The HostAddress can not be mapped as a common buffer.
1131 EFI_DEVICE_ERROR - The system hardware could not map the requested address.
1132 EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
1139 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
1140 ISA_MAP_INFO
*IsaMapInfo
;
1142 UINTN MaxNumberOfBytes
;
1148 UINT8 DmaChannelMode
;
1150 if ((NULL
== This
) ||
1151 (NULL
== HostAddress
) ||
1152 (NULL
== NumberOfBytes
) ||
1153 (NULL
== DeviceAddress
) ||
1156 return EFI_INVALID_PARAMETER
;
1161 // Initialize the return values to their defaults
1166 // Make sure the Operation parameter is valid
1168 if (Operation
< 0 || Operation
>= EfiIsaIoOperationMaximum
) {
1169 return EFI_INVALID_PARAMETER
;
1172 // See if this is a Slave DMA Operation
1176 if (Operation
== EfiIsaIoOperationSlaveRead
) {
1177 Operation
= EfiIsaIoOperationBusMasterRead
;
1182 if (Operation
== EfiIsaIoOperationSlaveWrite
) {
1183 Operation
= EfiIsaIoOperationBusMasterWrite
;
1190 // Make sure that ChannelNumber is a valid channel number
1191 // Channel 4 is used to cascade, so it is illegal.
1193 if (ChannelNumber
== 4 || ChannelNumber
> 7) {
1194 return EFI_INVALID_PARAMETER
;
1197 // This implementation only support COMPATIBLE DMA Transfers
1199 if (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_COMPATIBLE
)) {
1200 return EFI_INVALID_PARAMETER
;
1203 if (ChannelAttributes
&
1205 EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_A
|
1206 EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_B
|
1207 EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_C
1210 return EFI_INVALID_PARAMETER
;
1213 if (ChannelNumber
< 4) {
1215 // If this is Channel 0..3, then the width must be 8 bit
1217 if (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_8
) ||
1218 (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_16
)
1220 return EFI_INVALID_PARAMETER
;
1224 // If this is Channel 4..7, then the width must be 16 bit
1226 if ((ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_8
) ||
1227 (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_16
))
1229 return EFI_INVALID_PARAMETER
;
1233 // Either Demand Mode or Single Mode must be selected, but not both
1235 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SINGLE_MODE
) {
1236 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_DEMAND_MODE
) {
1237 return EFI_INVALID_PARAMETER
;
1240 if (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_DEMAND_MODE
)) {
1241 return EFI_INVALID_PARAMETER
;
1246 // Map the HostAddress to a DeviceAddress.
1248 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
1249 if ((PhysicalAddress
+*NumberOfBytes
) > ISA_MAX_MEMORY_ADDRESS
) {
1251 // Common Buffer operations can not be remapped. If the common buffer
1252 // is above 16MB, then it is not possible to generate a mapping, so return
1255 if (Operation
== EfiIsaIoOperationBusMasterCommonBuffer
) {
1256 return EFI_UNSUPPORTED
;
1259 // Allocate an ISA_MAP_INFO structure to remember the mapping when Unmap()
1262 IsaMapInfo
= AllocatePool (sizeof (ISA_MAP_INFO
));
1263 if (IsaMapInfo
== NULL
) {
1265 return EFI_OUT_OF_RESOURCES
;
1268 // Return a pointer to the MAP_INFO structure in Mapping
1270 *Mapping
= IsaMapInfo
;
1273 // Initialize the MAP_INFO structure
1275 IsaMapInfo
->Operation
= Operation
;
1276 IsaMapInfo
->NumberOfBytes
= *NumberOfBytes
;
1277 IsaMapInfo
->NumberOfPages
= EFI_SIZE_TO_PAGES (*NumberOfBytes
);
1278 IsaMapInfo
->HostAddress
= PhysicalAddress
;
1279 IsaMapInfo
->MappedHostAddress
= ISA_MAX_MEMORY_ADDRESS
- 1;
1282 // Allocate a buffer below 16MB to map the transfer to.
1284 Status
= gBS
->AllocatePages (
1286 EfiBootServicesData
,
1287 IsaMapInfo
->NumberOfPages
,
1288 &IsaMapInfo
->MappedHostAddress
1290 if (EFI_ERROR (Status
)) {
1291 gBS
->FreePool (IsaMapInfo
);
1297 // If this is a read operation from the DMA agents's point of view,
1298 // then copy the contents of the real buffer into the mapped buffer
1299 // so the DMA agent can read the contents of the real buffer.
1301 if (Operation
== EfiIsaIoOperationBusMasterRead
) {
1303 (VOID
*) (UINTN
) IsaMapInfo
->MappedHostAddress
,
1304 (VOID
*) (UINTN
) IsaMapInfo
->HostAddress
,
1305 IsaMapInfo
->NumberOfBytes
1309 // The DeviceAddress is the address of the maped buffer below 16 MB
1311 *DeviceAddress
= IsaMapInfo
->MappedHostAddress
;
1314 // The transfer is below 16 MB, so the DeviceAddress is simply the
1317 *DeviceAddress
= PhysicalAddress
;
1320 // If this is a Bus Master operation then return
1326 // Figure out what to program into the DMA Channel Mode Register
1328 DmaMode
= (UINT8
) (B_8237_DMA_CHMODE_INCREMENT
| (ChannelNumber
& 0x03));
1330 DmaMode
|= V_8237_DMA_CHMODE_MEM2IO
;
1332 DmaMode
|= V_8237_DMA_CHMODE_IO2MEM
;
1335 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_AUTO_INITIALIZE
) {
1336 DmaMode
|= B_8237_DMA_CHMODE_AE
;
1339 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_DEMAND_MODE
) {
1340 DmaMode
|= V_8237_DMA_CHMODE_DEMAND
;
1343 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SINGLE_MODE
) {
1344 DmaMode
|= V_8237_DMA_CHMODE_SINGLE
;
1347 // A Slave DMA transfer can not cross a 64K boundary.
1348 // Compute *NumberOfBytes based on this restriction.
1350 MaxNumberOfBytes
= 0x10000 - ((UINT32
) (*DeviceAddress
) & 0xffff);
1351 if (*NumberOfBytes
> MaxNumberOfBytes
) {
1352 *NumberOfBytes
= MaxNumberOfBytes
;
1355 // Compute the values to program into the BaseAddress and Count registers
1356 // of the Slave DMA controller
1358 if (ChannelNumber
< 4) {
1359 BaseAddress
= (UINT32
) (*DeviceAddress
);
1360 Count
= (UINT16
) (*NumberOfBytes
- 1);
1362 BaseAddress
= (UINT32
) (((UINT32
) (*DeviceAddress
) & 0xff0000) | (((UINT32
) (*DeviceAddress
) & 0xffff) >> 1));
1363 Count
= (UINT16
) ((*NumberOfBytes
- 1) >> 1);
1366 // Program the DMA Write Single Mask Register for ChannelNumber
1367 // Clear the DMA Byte Pointer Register
1369 if (ChannelNumber
< 4) {
1370 DmaMask
= R_8237_DMA_WRSMSK_CH0_3
;
1371 DmaClear
= R_8237_DMA_CBPR_CH0_3
;
1372 DmaChannelMode
= R_8237_DMA_CHMODE_CH0_3
;
1374 DmaMask
= R_8237_DMA_WRSMSK_CH4_7
;
1375 DmaClear
= R_8237_DMA_CBPR_CH4_7
;
1376 DmaChannelMode
= R_8237_DMA_CHMODE_CH4_7
;
1379 Status
= WritePort (
1382 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
1384 if (EFI_ERROR (Status
)) {
1388 Status
= WritePort (
1391 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
1393 if (EFI_ERROR (Status
)) {
1397 Status
= WritePort (This
, DmaChannelMode
, DmaMode
);
1398 if (EFI_ERROR (Status
)) {
1402 Status
= WriteDmaPort (
1404 DmaRegisters
[ChannelNumber
].Address
,
1405 DmaRegisters
[ChannelNumber
].Page
,
1406 DmaRegisters
[ChannelNumber
].Count
,
1410 if (EFI_ERROR (Status
)) {
1414 Status
= WritePort (
1417 (UINT8
) (ChannelNumber
& 0x03)
1419 if (EFI_ERROR (Status
)) {
1429 IN EFI_ISA_IO_PROTOCOL
*This
,
1430 IN EFI_ISA_IO_PROTOCOL_OPERATION Operation
,
1431 IN UINT8 ChannelNumber OPTIONAL
,
1432 IN UINT32 ChannelAttributes
,
1433 IN VOID
*HostAddress
,
1434 IN OUT UINTN
*NumberOfBytes
,
1435 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
1440 Routine Description:
1442 Maps a memory region for DMA
1446 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
1447 Operation - Indicates the type of DMA (slave or bus master), and if
1448 the DMA operation is going to read or write to system memory.
1449 ChannelNumber - The slave channel number to use for this DMA operation.
1450 If Operation and ChannelAttributes shows that this device
1451 performs bus mastering DMA, then this field is ignored.
1452 The legal range for this field is 0..7.
1453 ChannelAttributes - The attributes of the DMA channel to use for this DMA operation
1454 HostAddress - The system memory address to map to the device.
1455 NumberOfBytes - On input the number of bytes to map. On output the number
1456 of bytes that were mapped.
1457 DeviceAddress - The resulting map address for the bus master device to use
1458 - to access the hosts HostAddress.
1459 Mapping - A resulting value to pass to EFI_ISA_IO.Unmap().
1463 EFI_SUCCESS - The range was mapped for the returned NumberOfBytes.
1464 EFI_INVALID_PARAMETER - The Operation or HostAddress is undefined.
1465 EFI_UNSUPPORTED - The HostAddress can not be mapped as a common buffer.
1466 EFI_DEVICE_ERROR - The system hardware could not map the requested address.
1467 EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
1472 // Or unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
1474 if (!FeaturePcdGet (PcdIsaBusSupportDma
)) {
1475 return EFI_UNSUPPORTED
;
1478 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
1481 // So we just return EFI_UNSUPPORTED for these functions.
1483 if (FeaturePcdGet (PcdIsaBusOnlySupportSlaveDma
)) {
1484 return IsaIoMap_OnlySupportSlaveReadWrite (
1496 return IsaIoMap_FullSupport (
1510 IsaIoAllocateBuffer (
1511 IN EFI_ISA_IO_PROTOCOL
*This
,
1512 IN EFI_ALLOCATE_TYPE Type
,
1513 IN EFI_MEMORY_TYPE MemoryType
,
1515 OUT VOID
**HostAddress
,
1516 IN UINT64 Attributes
1520 Routine Description:
1522 Allocates a common buffer for DMA
1526 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
1527 Type - The type allocation to perform.
1528 MemoryType - The type of memory to allocate.
1529 Pages - The number of pages to allocate.
1530 HostAddress - A pointer to store the base address of the allocated range.
1531 Attributes - The requested bit mask of attributes for the allocated range.
1535 EFI_SUCCESS - The requested memory pages were allocated.
1536 EFI_INVALID_PARAMETER - Type is invalid or MemoryType is invalid or HostAddress is NULL
1537 EFI_UNSUPPORTED - Attributes is unsupported or the memory range specified
1538 by HostAddress, Pages, and Type is not available for common buffer use.
1539 EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
1544 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
1547 // Set Feature Flag PcdIsaBusOnlySupportSlaveDma to FALSE to disable support for
1549 // Or unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
1551 if (!FeaturePcdGet (PcdIsaBusSupportDma
) || FeaturePcdGet (PcdIsaBusOnlySupportSlaveDma
)) {
1552 return EFI_UNSUPPORTED
;
1555 if (HostAddress
== NULL
) {
1556 return EFI_INVALID_PARAMETER
;
1559 if (Type
< AllocateAnyPages
|| Type
>= MaxAllocateType
) {
1560 return EFI_INVALID_PARAMETER
;
1563 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
1565 if (MemoryType
!= EfiBootServicesData
&& MemoryType
!= EfiRuntimeServicesData
) {
1566 return EFI_INVALID_PARAMETER
;
1569 if (Attributes
&~(EFI_ISA_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE
| EFI_ISA_IO_ATTRIBUTE_MEMORY_CACHED
)) {
1570 return EFI_UNSUPPORTED
;
1573 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) (ISA_MAX_MEMORY_ADDRESS
- 1);
1574 if (Type
== AllocateAddress
) {
1575 if ((UINTN
) (*HostAddress
) >= ISA_MAX_MEMORY_ADDRESS
) {
1576 return EFI_UNSUPPORTED
;
1578 PhysicalAddress
= (UINTN
) (*HostAddress
);
1582 if (Type
== AllocateAnyPages
) {
1583 Type
= AllocateMaxAddress
;
1586 Status
= gBS
->AllocatePages (Type
, MemoryType
, Pages
, &PhysicalAddress
);
1587 if (EFI_ERROR (Status
)) {
1588 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
1592 *HostAddress
= (VOID
*) (UINTN
) PhysicalAddress
;
1599 IN EFI_ISA_IO_PROTOCOL
*This
,
1601 IN VOID
*HostAddress
1605 Routine Description:
1607 Frees a common buffer
1611 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
1612 Pages - The number of pages to free.
1613 HostAddress - The base address of the allocated range.
1617 EFI_SUCCESS - The requested memory pages were freed.
1618 EFI_INVALID_PARAMETER - The memory was not allocated with EFI_ISA_IO.AllocateBufer().
1623 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
1626 // Set Feature Flag PcdIsaBusOnlySupportSlaveDma to FALSE to disable support for
1628 // Or unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
1630 if (!FeaturePcdGet (PcdIsaBusSupportDma
) || FeaturePcdGet (PcdIsaBusOnlySupportSlaveDma
)) {
1631 return EFI_UNSUPPORTED
;
1634 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
1635 Status
= gBS
->FreePages (
1639 if (EFI_ERROR (Status
)) {
1640 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);