2 The implementation for EFI_ISA_IO_PROTOCOL.
4 Copyright (c) 2006 - 2007, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalIsaIo.h"
18 // Driver Support Global Variables
20 EFI_ISA_IO_PROTOCOL IsaIoInterface
= {
40 EFI_ISA_DMA_REGISTERS DmaRegisters
[8] = {
65 }, // Channel 4 is invalid
84 report a error Status code of PCI bus driver controller
86 @param Code - The error status code.
88 @return EFI_SUCCESS - Success to report status code.
91 ReportErrorStatusCode (
92 EFI_STATUS_CODE_VALUE Code
96 return REPORT_STATUS_CODE (
97 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
103 // Driver Support Functions
107 Initializes an ISA I/O Instance
109 @param IsaIoDevice - The iso device to be initialized.
110 @param IsaDeviceResourceList - The resource list.
112 @retval EFI_SUCCESS - Initial success.
116 InitializeIsaIoInstance (
117 IN ISA_IO_DEVICE
*IsaIoDevice
,
118 IN EFI_ISA_ACPI_RESOURCE_LIST
*IsaDeviceResourceList
122 // Initializes an ISA I/O Instance
127 sizeof (EFI_ISA_IO_PROTOCOL
)
130 IsaIoDevice
->IsaIo
.ResourceList
= IsaDeviceResourceList
;
136 Performs an ISA I/O Read Cycle
138 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
139 @param Width - Signifies the width of the I/O operation.
140 @param Offset - The offset in ISA I/O space to start the I/O operation.
141 @param Count - The number of I/O operations to perform.
142 @param Buffer - The destination buffer to store the results
144 @retval EFI_SUCCESS - The data was read from the device sucessfully.
145 @retval EFI_UNSUPPORTED - The Offset is not valid for this device.
146 @retval EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
147 @retval EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
153 IN EFI_ISA_IO_PROTOCOL
*This
,
154 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
162 ISA_IO_DEVICE
*IsaIoDevice
;
164 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
167 // Verify Isa IO Access
169 Status
= IsaIoVerifyAccess (
176 if (EFI_ERROR (Status
)) {
180 // Call PciIo->Io.Read
182 Status
= IsaIoDevice
->PciIo
->Io
.Read (
184 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
185 EFI_PCI_IO_PASS_THROUGH_BAR
,
191 if (EFI_ERROR (Status
)) {
192 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
199 Performs an ISA I/O Write Cycle
201 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
202 @param Width - Signifies the width of the I/O operation.
203 @param Offset - The offset in ISA I/O space to start the I/O operation.
204 @param Count - The number of I/O operations to perform.
205 @param Buffer - The source buffer to write data from
207 @retval EFI_SUCCESS - The data was writen to the device sucessfully.
208 @retval EFI_UNSUPPORTED - The Offset is not valid for this device.
209 @retval EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
210 @retval EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
216 IN EFI_ISA_IO_PROTOCOL
*This
,
217 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
224 ISA_IO_DEVICE
*IsaIoDevice
;
226 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
229 // Verify Isa IO Access
231 Status
= IsaIoVerifyAccess (
238 if (EFI_ERROR (Status
)) {
242 // Call PciIo->Io.Write
244 Status
= IsaIoDevice
->PciIo
->Io
.Write (
246 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
247 EFI_PCI_IO_PASS_THROUGH_BAR
,
253 if (EFI_ERROR (Status
)) {
254 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
261 Writes an 8 bit I/O Port
263 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
264 @param Offset - The offset in ISA IO space to start the IO operation.
265 @param Value - The data to write port.
267 @retval EFI_SUCCESS - Success.
268 @retval EFI_INVALID_PARAMETER - Parameter is invalid.
269 @retval EFI_UNSUPPORTED - The address range specified by Offset is not valid.
270 @retval EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
275 IN EFI_ISA_IO_PROTOCOL
*This
,
282 ISA_IO_DEVICE
*IsaIoDevice
;
284 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
287 // Call PciIo->Io.Write
289 Status
= IsaIoDevice
->PciIo
->Io
.Write (
292 EFI_PCI_IO_PASS_THROUGH_BAR
,
297 if (EFI_ERROR (Status
)) {
298 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
308 Writes I/O operation base address and count number to a 8 bit I/O Port.
310 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
311 @param AddrOffset - The address' offset.
312 @param PageOffset - The page's offest.
313 @param CountOffset - The count's offset.
314 @param BaseAddress - The base address.
315 @param Count - The number of I/O operations to perform.
317 @retval EFI_SUCCESS - Success.
318 @retval EFI_INVALID_PARAMETER - Parameter is invalid.
319 @retval EFI_UNSUPPORTED - The address range specified by these Offsets and Count is not valid.
320 @retval EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
325 IN EFI_ISA_IO_PROTOCOL
*This
,
326 IN UINT32 AddrOffset
,
327 IN UINT32 PageOffset
,
328 IN UINT32 CountOffset
,
329 IN UINT32 BaseAddress
,
336 Status
= WritePort (This
, AddrOffset
, (UINT8
) (BaseAddress
& 0xff));
337 if (EFI_ERROR (Status
)) {
341 Status
= WritePort (This
, AddrOffset
, (UINT8
) ((BaseAddress
>> 8) & 0xff));
342 if (EFI_ERROR (Status
)) {
346 Status
= WritePort (This
, PageOffset
, (UINT8
) ((BaseAddress
>> 16) & 0xff));
347 if (EFI_ERROR (Status
)) {
351 Status
= WritePort (This
, CountOffset
, (UINT8
) (Count
& 0xff));
352 if (EFI_ERROR (Status
)) {
356 Status
= WritePort (This
, CountOffset
, (UINT8
) ((Count
>> 8) & 0xff));
357 if (EFI_ERROR (Status
)) {
365 Unmaps a memory region for DMA
367 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
368 @param Mapping - The mapping value returned from EFI_ISA_IO.Map().
370 @retval EFI_SUCCESS - The range was unmapped.
371 @retval EFI_DEVICE_ERROR - The data was not committed to the target system memory.
377 IN EFI_ISA_IO_PROTOCOL
*This
,
381 ISA_MAP_INFO
*IsaMapInfo
;
384 // Unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
386 if (!FeaturePcdGet (PcdIsaBusSupportDma
)) {
387 return EFI_UNSUPPORTED
;
391 // See if the Map() operation associated with this Unmap() required a mapping
392 // buffer.If a mapping buffer was not required, then this function simply
393 // returns EFI_SUCCESS.
395 if (Mapping
!= NULL
) {
397 // Get the MAP_INFO structure from Mapping
399 IsaMapInfo
= (ISA_MAP_INFO
*) Mapping
;
402 // If this is a write operation from the Agent's point of view,
403 // then copy the contents of the mapped buffer into the real buffer
404 // so the processor can read the contents of the real buffer.
406 if (IsaMapInfo
->Operation
== EfiIsaIoOperationBusMasterWrite
) {
408 (VOID
*) (UINTN
) IsaMapInfo
->HostAddress
,
409 (VOID
*) (UINTN
) IsaMapInfo
->MappedHostAddress
,
410 IsaMapInfo
->NumberOfBytes
414 // Free the mapped buffer and the MAP_INFO structure.
416 gBS
->FreePages (IsaMapInfo
->MappedHostAddress
, IsaMapInfo
->NumberOfPages
);
417 gBS
->FreePool (IsaMapInfo
);
426 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
428 @retval EFI_SUCCESS - The buffers were flushed.
429 @retval EFI_DEVICE_ERROR - The buffers were not flushed due to a hardware error.
435 IN EFI_ISA_IO_PROTOCOL
*This
440 ISA_IO_DEVICE
*IsaIoDevice
;
442 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
447 Status
= IsaIoDevice
->PciIo
->Flush (IsaIoDevice
->PciIo
);
449 if (EFI_ERROR (Status
)) {
450 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
457 Verifies access to an ISA device
459 @param IsaIoDevice - The ISA device to be verified.
460 @param Type - The Access type. The input must be either IsaAccessTypeMem or IsaAccessTypeIo.
461 @param Width - Signifies the width of the memory operation.
462 @param Count - The number of memory operations to perform.
463 @param Offset - The offset in ISA memory space to start the memory operation.
465 @retval EFI_SUCCESS - Verify success.
466 @retval EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
467 @retval EFI_UNSUPPORTED - The device ont support the access type.
472 IN ISA_IO_DEVICE
*IsaIoDevice
,
473 IN ISA_ACCESS_TYPE Type
,
474 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
476 IN OUT UINT32
*Offset
480 EFI_ISA_ACPI_RESOURCE
*Item
;
483 if (Width
< EfiIsaIoWidthUint8
||
484 Width
>= EfiIsaIoWidthMaximum
||
485 Width
== EfiIsaIoWidthReserved
||
486 Width
== EfiIsaIoWidthFifoReserved
||
487 Width
== EfiIsaIoWidthFillReserved
489 return EFI_INVALID_PARAMETER
;
493 // If Width is EfiIsaIoWidthFifoUintX then convert to EfiIsaIoWidthUintX
494 // If Width is EfiIsaIoWidthFillUintX then convert to EfiIsaIoWidthUintX
496 if (Width
>= EfiIsaIoWidthFifoUint8
&& Width
<= EfiIsaIoWidthFifoReserved
) {
500 Width
= (EFI_ISA_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
502 Status
= EFI_UNSUPPORTED
;
503 Item
= IsaIoDevice
->IsaIo
.ResourceList
->ResourceItem
;
504 while (Item
->Type
!= EfiIsaAcpiResourceEndOfList
) {
505 if ((Type
== IsaAccessTypeMem
&& Item
->Type
== EfiIsaAcpiResourceMemory
) ||
506 (Type
== IsaAccessTypeIo
&& Item
->Type
== EfiIsaAcpiResourceIo
)
508 if (*Offset
>= Item
->StartRange
&& (*Offset
+ Count
* (UINT32
)(1 << Width
)) - 1 <= Item
->EndRange
) {
512 if (*Offset
>= Item
->StartRange
&& *Offset
<= Item
->EndRange
) {
513 Status
= EFI_INVALID_PARAMETER
;
525 Performs an ISA Memory Read Cycle
527 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
528 @param Width - Signifies the width of the memory operation.
529 @param Offset - The offset in ISA memory space to start the memory operation.
530 @param Count - The number of memory operations to perform.
531 @param Buffer - The destination buffer to store the results
533 @retval EFI_SUCCESS - The data was read from the device successfully.
534 @retval EFI_UNSUPPORTED - The Offset is not valid for this device.
535 @retval EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
536 @retval EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
542 IN EFI_ISA_IO_PROTOCOL
*This
,
543 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
551 ISA_IO_DEVICE
*IsaIoDevice
;
554 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
557 // So we just return EFI_UNSUPPORTED for these functions.
559 if (!FeaturePcdGet (PcdIsaBusSupportIsaMemory
)) {
560 return EFI_UNSUPPORTED
;
563 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
566 // Verify the Isa Io Access
568 Status
= IsaIoVerifyAccess (
575 if (EFI_ERROR (Status
)) {
579 // Call PciIo->Mem.Read
581 Status
= IsaIoDevice
->PciIo
->Mem
.Read (
583 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
584 EFI_PCI_IO_PASS_THROUGH_BAR
,
590 if (EFI_ERROR (Status
)) {
591 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
598 Performs an ISA Memory Write Cycle
600 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
601 @param Width - Signifies the width of the memory operation.
602 @param Offset - The offset in ISA memory space to start the memory operation.
603 @param Count - The number of memory operations to perform.
604 @param Buffer - The source buffer to write data from
606 @retval EFI_SUCCESS - The data was written to the device sucessfully.
607 @retval EFI_UNSUPPORTED - The Offset is not valid for this device.
608 @retval EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
609 @retval EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
615 IN EFI_ISA_IO_PROTOCOL
*This
,
616 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
623 ISA_IO_DEVICE
*IsaIoDevice
;
626 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
629 // So we just return EFI_UNSUPPORTED for these functions.
631 if (!FeaturePcdGet (PcdIsaBusSupportIsaMemory
)) {
632 return EFI_UNSUPPORTED
;
635 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
638 // Verify Isa IO Access
640 Status
= IsaIoVerifyAccess (
647 if (EFI_ERROR (Status
)) {
651 // Call PciIo->Mem.Write
653 Status
= IsaIoDevice
->PciIo
->Mem
.Write (
655 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
656 EFI_PCI_IO_PASS_THROUGH_BAR
,
662 if (EFI_ERROR (Status
)) {
663 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
670 Performs an ISA I/O Copy Memory
672 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
673 @param Width - Signifies the width of the memory copy operation.
674 @param DestOffset - The offset of the destination
675 @param SrcOffset - The offset of the source
676 @param Count - The number of memory copy operations to perform
678 @retval EFI_SUCCESS - The data was copied sucessfully.
679 @retval EFI_UNSUPPORTED - The DestOffset or SrcOffset is not valid for this device.
680 @retval EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
681 @retval EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
687 IN EFI_ISA_IO_PROTOCOL
*This
,
688 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
689 IN UINT32 DestOffset
,
696 ISA_IO_DEVICE
*IsaIoDevice
;
699 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
702 // So we just return EFI_UNSUPPORTED for these functions.
704 if (!FeaturePcdGet (PcdIsaBusSupportIsaMemory
)) {
705 return EFI_UNSUPPORTED
;
708 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
711 // Verify Isa IO Access for destination and source
713 Status
= IsaIoVerifyAccess (
720 if (EFI_ERROR (Status
)) {
724 Status
= IsaIoVerifyAccess (
731 if (EFI_ERROR (Status
)) {
735 // Call PciIo->CopyMem
737 Status
= IsaIoDevice
->PciIo
->CopyMem (
739 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
740 EFI_PCI_IO_PASS_THROUGH_BAR
,
742 EFI_PCI_IO_PASS_THROUGH_BAR
,
747 if (EFI_ERROR (Status
)) {
748 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
755 Maps a memory region for DMA, note this implementation
756 only supports slave read/write operation to save code size.
758 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
759 @param Operation - Indicates the type of DMA (slave or bus master), and if
760 the DMA operation is going to read or write to system memory.
761 @param ChannelNumber - The slave channel number to use for this DMA operation.
762 If Operation and ChannelAttributes shows that this device
763 performs bus mastering DMA, then this field is ignored.
764 The legal range for this field is 0..7.
765 @param ChannelAttributes - The attributes of the DMA channel to use for this DMA operation
766 @param HostAddress - The system memory address to map to the device.
767 @param NumberOfBytes - On input the number of bytes to map. On output the number
768 of bytes that were mapped.
769 @param DeviceAddress - The resulting map address for the bus master device to use
770 to access the hosts HostAddress.
771 @param Mapping - A resulting value to pass to EFI_ISA_IO.Unmap().
773 @retval EFI_SUCCESS - The range was mapped for the returned NumberOfBytes.
774 @retval EFI_INVALID_PARAMETER - The Operation or HostAddress is undefined.
775 @retval EFI_UNSUPPORTED - The HostAddress can not be mapped as a common buffer.
776 @retval EFI_DEVICE_ERROR - The system hardware could not map the requested address.
777 @retval EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
781 IsaIoMap_OnlySupportSlaveReadWrite (
782 IN EFI_ISA_IO_PROTOCOL
*This
,
783 IN EFI_ISA_IO_PROTOCOL_OPERATION Operation
,
784 IN UINT8 ChannelNumber OPTIONAL
,
785 IN UINT32 ChannelAttributes
,
786 IN VOID
*HostAddress
,
787 IN OUT UINTN
*NumberOfBytes
,
788 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
794 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
795 ISA_MAP_INFO
*IsaMapInfo
;
797 UINTN MaxNumberOfBytes
;
803 UINT8 DmaChannelMode
;
805 if ((NULL
== This
) ||
806 (NULL
== HostAddress
) ||
807 (NULL
== NumberOfBytes
) ||
808 (NULL
== DeviceAddress
) ||
811 return EFI_INVALID_PARAMETER
;
816 // Initialize the return values to their defaults
821 // Make sure the Operation parameter is valid.
822 // Light IsaIo only supports two operations.
824 if (!(Operation
== EfiIsaIoOperationSlaveRead
||
825 Operation
== EfiIsaIoOperationSlaveWrite
)) {
826 return EFI_INVALID_PARAMETER
;
829 if (ChannelNumber
>= 4) {
831 // The Light IsaIo doesn't support channelNumber larger than 4.
833 return EFI_INVALID_PARAMETER
;
837 // Map the HostAddress to a DeviceAddress.
839 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
840 if ((PhysicalAddress
+*NumberOfBytes
) > ISA_MAX_MEMORY_ADDRESS
) {
842 // Common Buffer operations can not be remapped. If the common buffer
843 // is above 16MB, then it is not possible to generate a mapping, so return
846 if (Operation
== EfiIsaIoOperationBusMasterCommonBuffer
) {
847 return EFI_UNSUPPORTED
;
850 // Allocate an ISA_MAP_INFO structure to remember the mapping when Unmap()
853 IsaMapInfo
= AllocatePool (sizeof (ISA_MAP_INFO
));
854 if (IsaMapInfo
== NULL
) {
856 return EFI_OUT_OF_RESOURCES
;
859 // Return a pointer to the MAP_INFO structure in Mapping
861 *Mapping
= IsaMapInfo
;
864 // Initialize the MAP_INFO structure
866 IsaMapInfo
->Operation
= Operation
;
867 IsaMapInfo
->NumberOfBytes
= *NumberOfBytes
;
868 IsaMapInfo
->NumberOfPages
= EFI_SIZE_TO_PAGES (*NumberOfBytes
);
869 IsaMapInfo
->HostAddress
= PhysicalAddress
;
870 IsaMapInfo
->MappedHostAddress
= ISA_MAX_MEMORY_ADDRESS
- 1;
873 // Allocate a buffer below 16MB to map the transfer to.
875 Status
= gBS
->AllocatePages (
878 IsaMapInfo
->NumberOfPages
,
879 &IsaMapInfo
->MappedHostAddress
881 if (EFI_ERROR (Status
)) {
882 gBS
->FreePool (IsaMapInfo
);
888 // If this is a read operation from the DMA agents's point of view,
889 // then copy the contents of the real buffer into the mapped buffer
890 // so the DMA agent can read the contents of the real buffer.
892 if (Operation
== EfiIsaIoOperationSlaveRead
) {
894 (VOID
*) (UINTN
) IsaMapInfo
->MappedHostAddress
,
895 (VOID
*) (UINTN
) IsaMapInfo
->HostAddress
,
896 IsaMapInfo
->NumberOfBytes
900 // The DeviceAddress is the address of the maped buffer below 16 MB
902 *DeviceAddress
= IsaMapInfo
->MappedHostAddress
;
905 // The transfer is below 16 MB, so the DeviceAddress is simply the
908 *DeviceAddress
= PhysicalAddress
;
912 // Figure out what to program into the DMA Channel Mode Register
914 DmaMode
= (UINT8
) (B_8237_DMA_CHMODE_INCREMENT
| (ChannelNumber
& 0x03));
915 if (Operation
== EfiIsaIoOperationSlaveRead
) {
916 DmaMode
|= V_8237_DMA_CHMODE_MEM2IO
;
918 DmaMode
|= V_8237_DMA_CHMODE_IO2MEM
;
921 // We only support EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SINGLE_MODE in simplified IsaIo
923 DmaMode
|= V_8237_DMA_CHMODE_SINGLE
;
926 // A Slave DMA transfer can not cross a 64K boundary.
927 // Compute *NumberOfBytes based on this restriction.
929 MaxNumberOfBytes
= 0x10000 - ((UINT32
) (*DeviceAddress
) & 0xffff);
930 if (*NumberOfBytes
> MaxNumberOfBytes
) {
931 *NumberOfBytes
= MaxNumberOfBytes
;
934 // Compute the values to program into the BaseAddress and Count registers
935 // of the Slave DMA controller
937 BaseAddress
= (UINT32
) (*DeviceAddress
);
938 Count
= (UINT16
) (*NumberOfBytes
- 1);
940 // Program the DMA Write Single Mask Register for ChannelNumber
941 // Clear the DMA Byte Pointer Register
943 DmaMask
= R_8237_DMA_WRSMSK_CH0_3
;
944 DmaClear
= R_8237_DMA_CBPR_CH0_3
;
945 DmaChannelMode
= R_8237_DMA_CHMODE_CH0_3
;
950 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
952 if (EFI_ERROR (Status
)) {
959 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
961 if (EFI_ERROR (Status
)) {
965 Status
= WritePort (This
, DmaChannelMode
, DmaMode
);
966 if (EFI_ERROR (Status
)) {
970 Status
= WriteDmaPort (
972 DmaRegisters
[ChannelNumber
].Address
,
973 DmaRegisters
[ChannelNumber
].Page
,
974 DmaRegisters
[ChannelNumber
].Count
,
978 if (EFI_ERROR (Status
)) {
985 (UINT8
) (ChannelNumber
& 0x03)
987 if (EFI_ERROR (Status
)) {
995 Maps a memory region for DMA. This implementation implement the
996 the full mapping support.
998 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
999 @param Operation - Indicates the type of DMA (slave or bus master), and if
1000 the DMA operation is going to read or write to system memory.
1001 @param ChannelNumber - The slave channel number to use for this DMA operation.
1002 If Operation and ChannelAttributes shows that this device
1003 performs bus mastering DMA, then this field is ignored.
1004 The legal range for this field is 0..7.
1005 @param ChannelAttributes - The attributes of the DMA channel to use for this DMA operation
1006 @param HostAddress - The system memory address to map to the device.
1007 @param NumberOfBytes - On input the number of bytes to map. On output the number
1008 of bytes that were mapped.
1009 @param DeviceAddress - The resulting map address for the bus master device to use
1010 - to access the hosts HostAddress.
1011 @param Mapping - A resulting value to pass to EFI_ISA_IO.Unmap().
1013 @retval EFI_SUCCESS - The range was mapped for the returned NumberOfBytes.
1014 @retval EFI_INVALID_PARAMETER - The Operation or HostAddress is undefined.
1015 @retval EFI_UNSUPPORTED - The HostAddress can not be mapped as a common buffer.
1016 @retval EFI_DEVICE_ERROR - The system hardware could not map the requested address.
1017 @retval EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
1021 IsaIoMap_FullSupport (
1022 IN EFI_ISA_IO_PROTOCOL
*This
,
1023 IN EFI_ISA_IO_PROTOCOL_OPERATION Operation
,
1024 IN UINT8 ChannelNumber OPTIONAL
,
1025 IN UINT32 ChannelAttributes
,
1026 IN VOID
*HostAddress
,
1027 IN OUT UINTN
*NumberOfBytes
,
1028 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
1036 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
1037 ISA_MAP_INFO
*IsaMapInfo
;
1039 UINTN MaxNumberOfBytes
;
1045 UINT8 DmaChannelMode
;
1047 if ((NULL
== This
) ||
1048 (NULL
== HostAddress
) ||
1049 (NULL
== NumberOfBytes
) ||
1050 (NULL
== DeviceAddress
) ||
1053 return EFI_INVALID_PARAMETER
;
1058 // Initialize the return values to their defaults
1063 // Make sure the Operation parameter is valid
1065 if (Operation
< 0 || Operation
>= EfiIsaIoOperationMaximum
) {
1066 return EFI_INVALID_PARAMETER
;
1069 if (ChannelNumber
>= 8) {
1070 return EFI_INVALID_PARAMETER
;
1074 // See if this is a Slave DMA Operation
1078 if (Operation
== EfiIsaIoOperationSlaveRead
) {
1079 Operation
= EfiIsaIoOperationBusMasterRead
;
1084 if (Operation
== EfiIsaIoOperationSlaveWrite
) {
1085 Operation
= EfiIsaIoOperationBusMasterWrite
;
1092 // Make sure that ChannelNumber is a valid channel number
1093 // Channel 4 is used to cascade, so it is illegal.
1095 if (ChannelNumber
== 4 || ChannelNumber
> 7) {
1096 return EFI_INVALID_PARAMETER
;
1099 // This implementation only support COMPATIBLE DMA Transfers
1101 if (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_COMPATIBLE
)) {
1102 return EFI_INVALID_PARAMETER
;
1105 if (ChannelAttributes
&
1107 EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_A
|
1108 EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_B
|
1109 EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_C
1112 return EFI_INVALID_PARAMETER
;
1115 if (ChannelNumber
< 4) {
1117 // If this is Channel 0..3, then the width must be 8 bit
1119 if (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_8
) ||
1120 (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_16
)
1122 return EFI_INVALID_PARAMETER
;
1126 // If this is Channel 4..7, then the width must be 16 bit
1128 if ((ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_8
) ||
1129 (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_16
))
1131 return EFI_INVALID_PARAMETER
;
1135 // Either Demand Mode or Single Mode must be selected, but not both
1137 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SINGLE_MODE
) {
1138 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_DEMAND_MODE
) {
1139 return EFI_INVALID_PARAMETER
;
1142 if (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_DEMAND_MODE
)) {
1143 return EFI_INVALID_PARAMETER
;
1148 // Map the HostAddress to a DeviceAddress.
1150 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
1151 if ((PhysicalAddress
+*NumberOfBytes
) > ISA_MAX_MEMORY_ADDRESS
) {
1153 // Common Buffer operations can not be remapped. If the common buffer
1154 // is above 16MB, then it is not possible to generate a mapping, so return
1157 if (Operation
== EfiIsaIoOperationBusMasterCommonBuffer
) {
1158 return EFI_UNSUPPORTED
;
1161 // Allocate an ISA_MAP_INFO structure to remember the mapping when Unmap()
1164 IsaMapInfo
= AllocatePool (sizeof (ISA_MAP_INFO
));
1165 if (IsaMapInfo
== NULL
) {
1167 return EFI_OUT_OF_RESOURCES
;
1170 // Return a pointer to the MAP_INFO structure in Mapping
1172 *Mapping
= IsaMapInfo
;
1175 // Initialize the MAP_INFO structure
1177 IsaMapInfo
->Operation
= Operation
;
1178 IsaMapInfo
->NumberOfBytes
= *NumberOfBytes
;
1179 IsaMapInfo
->NumberOfPages
= EFI_SIZE_TO_PAGES (*NumberOfBytes
);
1180 IsaMapInfo
->HostAddress
= PhysicalAddress
;
1181 IsaMapInfo
->MappedHostAddress
= ISA_MAX_MEMORY_ADDRESS
- 1;
1184 // Allocate a buffer below 16MB to map the transfer to.
1186 Status
= gBS
->AllocatePages (
1188 EfiBootServicesData
,
1189 IsaMapInfo
->NumberOfPages
,
1190 &IsaMapInfo
->MappedHostAddress
1192 if (EFI_ERROR (Status
)) {
1193 gBS
->FreePool (IsaMapInfo
);
1199 // If this is a read operation from the DMA agents's point of view,
1200 // then copy the contents of the real buffer into the mapped buffer
1201 // so the DMA agent can read the contents of the real buffer.
1203 if (Operation
== EfiIsaIoOperationBusMasterRead
) {
1205 (VOID
*) (UINTN
) IsaMapInfo
->MappedHostAddress
,
1206 (VOID
*) (UINTN
) IsaMapInfo
->HostAddress
,
1207 IsaMapInfo
->NumberOfBytes
1211 // The DeviceAddress is the address of the maped buffer below 16 MB
1213 *DeviceAddress
= IsaMapInfo
->MappedHostAddress
;
1216 // The transfer is below 16 MB, so the DeviceAddress is simply the
1219 *DeviceAddress
= PhysicalAddress
;
1222 // If this is a Bus Master operation then return
1228 // Figure out what to program into the DMA Channel Mode Register
1230 DmaMode
= (UINT8
) (B_8237_DMA_CHMODE_INCREMENT
| (ChannelNumber
& 0x03));
1232 DmaMode
|= V_8237_DMA_CHMODE_MEM2IO
;
1234 DmaMode
|= V_8237_DMA_CHMODE_IO2MEM
;
1237 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_AUTO_INITIALIZE
) {
1238 DmaMode
|= B_8237_DMA_CHMODE_AE
;
1241 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_DEMAND_MODE
) {
1242 DmaMode
|= V_8237_DMA_CHMODE_DEMAND
;
1245 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SINGLE_MODE
) {
1246 DmaMode
|= V_8237_DMA_CHMODE_SINGLE
;
1249 // A Slave DMA transfer can not cross a 64K boundary.
1250 // Compute *NumberOfBytes based on this restriction.
1252 MaxNumberOfBytes
= 0x10000 - ((UINT32
) (*DeviceAddress
) & 0xffff);
1253 if (*NumberOfBytes
> MaxNumberOfBytes
) {
1254 *NumberOfBytes
= MaxNumberOfBytes
;
1257 // Compute the values to program into the BaseAddress and Count registers
1258 // of the Slave DMA controller
1260 if (ChannelNumber
< 4) {
1261 BaseAddress
= (UINT32
) (*DeviceAddress
);
1262 Count
= (UINT16
) (*NumberOfBytes
- 1);
1264 BaseAddress
= (UINT32
) (((UINT32
) (*DeviceAddress
) & 0xff0000) | (((UINT32
) (*DeviceAddress
) & 0xffff) >> 1));
1265 Count
= (UINT16
) ((*NumberOfBytes
- 1) >> 1);
1268 // Program the DMA Write Single Mask Register for ChannelNumber
1269 // Clear the DMA Byte Pointer Register
1271 if (ChannelNumber
< 4) {
1272 DmaMask
= R_8237_DMA_WRSMSK_CH0_3
;
1273 DmaClear
= R_8237_DMA_CBPR_CH0_3
;
1274 DmaChannelMode
= R_8237_DMA_CHMODE_CH0_3
;
1276 DmaMask
= R_8237_DMA_WRSMSK_CH4_7
;
1277 DmaClear
= R_8237_DMA_CBPR_CH4_7
;
1278 DmaChannelMode
= R_8237_DMA_CHMODE_CH4_7
;
1281 Status
= WritePort (
1284 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
1286 if (EFI_ERROR (Status
)) {
1290 Status
= WritePort (
1293 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
1295 if (EFI_ERROR (Status
)) {
1299 Status
= WritePort (This
, DmaChannelMode
, DmaMode
);
1300 if (EFI_ERROR (Status
)) {
1304 Status
= WriteDmaPort (
1306 DmaRegisters
[ChannelNumber
].Address
,
1307 DmaRegisters
[ChannelNumber
].Page
,
1308 DmaRegisters
[ChannelNumber
].Count
,
1312 if (EFI_ERROR (Status
)) {
1316 Status
= WritePort (
1319 (UINT8
) (ChannelNumber
& 0x03)
1321 if (EFI_ERROR (Status
)) {
1329 Maps a memory region for DMA
1331 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
1332 @param Operation - Indicates the type of DMA (slave or bus master), and if
1333 the DMA operation is going to read or write to system memory.
1334 @param ChannelNumber - The slave channel number to use for this DMA operation.
1335 If Operation and ChannelAttributes shows that this device
1336 performs bus mastering DMA, then this field is ignored.
1337 The legal range for this field is 0..7.
1338 @param ChannelAttributes - The attributes of the DMA channel to use for this DMA operation
1339 @param HostAddress - The system memory address to map to the device.
1340 @param NumberOfBytes - On input the number of bytes to map. On output the number
1341 of bytes that were mapped.
1342 @param DeviceAddress - The resulting map address for the bus master device to use
1343 - to access the hosts HostAddress.
1344 @param Mapping - A resulting value to pass to EFI_ISA_IO.Unmap().
1347 @retval EFI_SUCCESS - The range was mapped for the returned NumberOfBytes.
1348 @retval EFI_INVALID_PARAMETER - The Operation or HostAddress is undefined.
1349 @retval EFI_UNSUPPORTED - The HostAddress can not be mapped as a common buffer.
1350 @retval EFI_DEVICE_ERROR - The system hardware could not map the requested address.
1351 @retval EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
1357 IN EFI_ISA_IO_PROTOCOL
*This
,
1358 IN EFI_ISA_IO_PROTOCOL_OPERATION Operation
,
1359 IN UINT8 ChannelNumber OPTIONAL
,
1360 IN UINT32 ChannelAttributes
,
1361 IN VOID
*HostAddress
,
1362 IN OUT UINTN
*NumberOfBytes
,
1363 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
1369 // Or unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
1371 if (!FeaturePcdGet (PcdIsaBusSupportDma
)) {
1372 return EFI_UNSUPPORTED
;
1375 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
1378 // So we just return EFI_UNSUPPORTED for these functions.
1380 if (FeaturePcdGet (PcdIsaBusOnlySupportSlaveDma
)) {
1381 return IsaIoMap_OnlySupportSlaveReadWrite (
1393 return IsaIoMap_FullSupport (
1407 Allocates a common buffer for DMA
1409 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
1410 @param Type - The type allocation to perform.
1411 @param MemoryType - The type of memory to allocate.
1412 @param Pages - The number of pages to allocate.
1413 @param HostAddress - A pointer to store the base address of the allocated range.
1414 @param Attributes - The requested bit mask of attributes for the allocated range.
1416 @retval EFI_SUCCESS - The requested memory pages were allocated.
1417 @retval EFI_INVALID_PARAMETER - Type is invalid or MemoryType is invalid or HostAddress is NULL
1418 @retval EFI_UNSUPPORTED - Attributes is unsupported or the memory range specified
1419 by HostAddress, Pages, and Type is not available for common buffer use.
1420 @retval EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
1425 IsaIoAllocateBuffer (
1426 IN EFI_ISA_IO_PROTOCOL
*This
,
1427 IN EFI_ALLOCATE_TYPE Type
,
1428 IN EFI_MEMORY_TYPE MemoryType
,
1430 OUT VOID
**HostAddress
,
1431 IN UINT64 Attributes
1435 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
1438 // Set Feature Flag PcdIsaBusOnlySupportSlaveDma to FALSE to disable support for
1440 // Or unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
1442 if (!FeaturePcdGet (PcdIsaBusSupportDma
) || FeaturePcdGet (PcdIsaBusOnlySupportSlaveDma
)) {
1443 return EFI_UNSUPPORTED
;
1446 if (HostAddress
== NULL
) {
1447 return EFI_INVALID_PARAMETER
;
1450 if (Type
< AllocateAnyPages
|| Type
>= MaxAllocateType
) {
1451 return EFI_INVALID_PARAMETER
;
1454 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
1456 if (MemoryType
!= EfiBootServicesData
&& MemoryType
!= EfiRuntimeServicesData
) {
1457 return EFI_INVALID_PARAMETER
;
1460 if (Attributes
&~(EFI_ISA_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE
| EFI_ISA_IO_ATTRIBUTE_MEMORY_CACHED
)) {
1461 return EFI_UNSUPPORTED
;
1464 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) (ISA_MAX_MEMORY_ADDRESS
- 1);
1465 if (Type
== AllocateAddress
) {
1466 if ((UINTN
) (*HostAddress
) >= ISA_MAX_MEMORY_ADDRESS
) {
1467 return EFI_UNSUPPORTED
;
1469 PhysicalAddress
= (UINTN
) (*HostAddress
);
1473 if (Type
== AllocateAnyPages
) {
1474 Type
= AllocateMaxAddress
;
1477 Status
= gBS
->AllocatePages (Type
, MemoryType
, Pages
, &PhysicalAddress
);
1478 if (EFI_ERROR (Status
)) {
1479 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
1483 *HostAddress
= (VOID
*) (UINTN
) PhysicalAddress
;
1489 Frees a common buffer
1491 @param This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
1492 @param Pages - The number of pages to free.
1493 @param HostAddress - The base address of the allocated range.
1496 @retval EFI_SUCCESS - The requested memory pages were freed.
1497 @retval EFI_INVALID_PARAMETER - The memory was not allocated with EFI_ISA_IO.AllocateBufer().
1504 IN EFI_ISA_IO_PROTOCOL
*This
,
1506 IN VOID
*HostAddress
1510 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
1513 // Set Feature Flag PcdIsaBusOnlySupportSlaveDma to FALSE to disable support for
1515 // Or unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
1517 if (!FeaturePcdGet (PcdIsaBusSupportDma
) || FeaturePcdGet (PcdIsaBusOnlySupportSlaveDma
)) {
1518 return EFI_UNSUPPORTED
;
1521 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
1522 Status
= gBS
->FreePages (
1526 if (EFI_ERROR (Status
)) {
1527 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);