Skip to content

Commit 7931eea

Browse files
committed
Add IoMmu support to NonDiscoverablePciDeviceDxe
Uses IoMmuLib to do IoMmu protocol mappings.
1 parent fada69c commit 7931eea

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

MdeModulePkg/Bus/Pci/NonDiscoverablePciDeviceDxe/NonDiscoverablePciDeviceDxe.inf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
UefiBootServicesTableLib
3434
UefiDriverEntryPoint
3535
UefiLib
36+
IoMmuLib ## MU_CHANGE
3637

3738
[Protocols]
3839
gEfiPciIoProtocolGuid ## BY_START

MdeModulePkg/Bus/Pci/NonDiscoverablePciDeviceDxe/NonDiscoverablePciDeviceIo.c

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515

1616
#include <Protocol/PciRootBridgeIo.h>
1717

18+
#include <Library/IoMmuLib.h> // MU_CHANGE
19+
1820
typedef struct {
1921
EFI_PHYSICAL_ADDRESS AllocAddress;
2022
VOID *HostAddress;
2123
EFI_PCI_IO_PROTOCOL_OPERATION Operation;
2224
UINTN NumberOfBytes;
25+
VOID *IoMmuContext; // MU_CHANGE
2326
} NON_DISCOVERABLE_PCI_DEVICE_MAP_INFO;
2427

2528
/**
@@ -1278,6 +1281,13 @@ NonCoherentPciIoMap (
12781281
EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
12791282
BOOLEAN Bounce;
12801283

1284+
// MU_CHANGE [BEGIN]
1285+
VOID *IoMmuHostAddress;
1286+
EDKII_IOMMU_OPERATION IoMmuOperation;
1287+
UINT64 IoMmuAttribute;
1288+
1289+
// MU_CHANGE [END]
1290+
12811291
AllocAddress = NULL; // MS_CHANGE for vs2017
12821292

12831293
if ((HostAddress == NULL) ||
@@ -1303,6 +1313,7 @@ NonCoherentPciIoMap (
13031313
MapInfo->HostAddress = HostAddress;
13041314
MapInfo->Operation = Operation;
13051315
MapInfo->NumberOfBytes = *NumberOfBytes;
1316+
MapInfo->IoMmuContext = NULL; // MU_CHANGE
13061317

13071318
Dev = NON_DISCOVERABLE_PCI_DEVICE_FROM_PCI_IO (This);
13081319

@@ -1372,10 +1383,12 @@ NonCoherentPciIoMap (
13721383
gBS->CopyMem (AllocAddress, HostAddress, *NumberOfBytes);
13731384
}
13741385

1375-
*DeviceAddress = MapInfo->AllocAddress;
1386+
*DeviceAddress = MapInfo->AllocAddress;
1387+
IoMmuHostAddress = (VOID *)(UINTN)MapInfo->AllocAddress; // MU_CHANGE
13761388
} else {
13771389
MapInfo->AllocAddress = 0;
13781390
*DeviceAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)HostAddress;
1391+
IoMmuHostAddress = HostAddress; // MU_CHANGE
13791392

13801393
//
13811394
// We are not using a bounce buffer: the mapping is sufficiently
@@ -1396,6 +1409,60 @@ NonCoherentPciIoMap (
13961409
}
13971410

13981411
*Mapping = MapInfo;
1412+
1413+
// MU_CHANGE [BEGIN]
1414+
switch (Operation) {
1415+
case EfiPciIoOperationBusMasterRead:
1416+
IoMmuOperation = EdkiiIoMmuOperationBusMasterRead;
1417+
IoMmuAttribute = EDKII_IOMMU_ACCESS_READ;
1418+
break;
1419+
1420+
case EfiPciIoOperationBusMasterWrite:
1421+
IoMmuOperation = EdkiiIoMmuOperationBusMasterWrite;
1422+
IoMmuAttribute = EDKII_IOMMU_ACCESS_WRITE;
1423+
break;
1424+
1425+
case EfiPciIoOperationBusMasterCommonBuffer:
1426+
IoMmuOperation = EdkiiIoMmuOperationBusMasterCommonBuffer;
1427+
IoMmuAttribute = EDKII_IOMMU_ACCESS_READ | EDKII_IOMMU_ACCESS_WRITE;
1428+
break;
1429+
1430+
default:
1431+
DEBUG ((DEBUG_ERROR, "%a - Invalid operation %d\n", __func__, Operation));
1432+
ASSERT (FALSE);
1433+
return EFI_INVALID_PARAMETER;
1434+
}
1435+
1436+
Status = IoMmuMap (
1437+
IoMmuOperation,
1438+
IoMmuHostAddress,
1439+
NumberOfBytes,
1440+
DeviceAddress,
1441+
&MapInfo->IoMmuContext
1442+
);
1443+
if (Status != EFI_UNSUPPORTED) {
1444+
if (EFI_ERROR (Status)) {
1445+
DEBUG ((DEBUG_ERROR, "%a - IoMmuMap failed.\n", __func__));
1446+
ASSERT (FALSE);
1447+
return Status;
1448+
}
1449+
}
1450+
1451+
Status = IoMmuSetAttribute (
1452+
NULL,
1453+
MapInfo->IoMmuContext,
1454+
IoMmuAttribute
1455+
);
1456+
if (Status != EFI_UNSUPPORTED) {
1457+
if (EFI_ERROR (Status)) {
1458+
DEBUG ((DEBUG_ERROR, "%a - IoMmuSetAttribute failed.\n", __func__));
1459+
ASSERT (FALSE);
1460+
return Status;
1461+
}
1462+
}
1463+
1464+
// MU_CHANGE [END]
1465+
13991466
return EFI_SUCCESS;
14001467

14011468
FreeMapInfo:
@@ -1422,12 +1489,26 @@ NonCoherentPciIoUnmap (
14221489
)
14231490
{
14241491
NON_DISCOVERABLE_PCI_DEVICE_MAP_INFO *MapInfo;
1492+
EFI_STATUS Status; // MU_CHANGE
14251493

14261494
if (Mapping == NULL) {
14271495
return EFI_DEVICE_ERROR;
14281496
}
14291497

14301498
MapInfo = Mapping;
1499+
1500+
// MU_CHANGE [BEGIN]
1501+
Status = IoMmuUnmap (MapInfo->IoMmuContext);
1502+
if (Status != EFI_UNSUPPORTED) {
1503+
if (EFI_ERROR (Status)) {
1504+
DEBUG ((DEBUG_ERROR, "%a - IoMmuUnmap failed.\n", __func__));
1505+
ASSERT (FALSE);
1506+
return Status;
1507+
}
1508+
}
1509+
1510+
// MU_CHANGE [END]
1511+
14311512
if (MapInfo->AllocAddress != 0) {
14321513
//
14331514
// We are using a bounce buffer: copy back the data if necessary,

0 commit comments

Comments
 (0)