Skip to content

Commit 969f9f4

Browse files
committed
Add IoMmu support to NonDiscoverablePciDeviceDxe
Uses IoMmuLib to do IoMmu protocol mappings.
1 parent f3bac24 commit 969f9f4

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
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: 73 additions & 2 deletions
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,7 +1409,54 @@ NonCoherentPciIoMap (
13961409
}
13971410

13981411
*Mapping = MapInfo;
1399-
return EFI_SUCCESS;
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 (EFI_ERROR (Status)) {
1444+
DEBUG ((DEBUG_ERROR, "%a - IoMmu Map failed.\n", __func__));
1445+
return Status;
1446+
}
1447+
1448+
Status = IoMmuSetAttribute (
1449+
NULL,
1450+
MapInfo->IoMmuContext,
1451+
IoMmuAttribute
1452+
);
1453+
if (EFI_ERROR (Status)) {
1454+
DEBUG ((DEBUG_ERROR, "%a - IoMmu SetAttribute failed.\n", __func__));
1455+
}
1456+
1457+
return Status;
1458+
1459+
// MU_CHANGE [END]
14001460

14011461
FreeMapInfo:
14021462
FreePool (MapInfo);
@@ -1422,12 +1482,23 @@ NonCoherentPciIoUnmap (
14221482
)
14231483
{
14241484
NON_DISCOVERABLE_PCI_DEVICE_MAP_INFO *MapInfo;
1485+
EFI_STATUS Status; // MU_CHANGE
14251486

14261487
if (Mapping == NULL) {
14271488
return EFI_DEVICE_ERROR;
14281489
}
14291490

14301491
MapInfo = Mapping;
1492+
1493+
// MU_CHANGE [BEGIN]
1494+
Status = IoMmuUnmap (MapInfo->IoMmuContext);
1495+
if (EFI_ERROR (Status)) {
1496+
DEBUG ((DEBUG_ERROR, "%a - IoMmu Unmap failed.\n", __func__));
1497+
return Status;
1498+
}
1499+
1500+
// MU_CHANGE [END]
1501+
14311502
if (MapInfo->AllocAddress != 0) {
14321503
//
14331504
// We are using a bounce buffer: copy back the data if necessary,

0 commit comments

Comments
 (0)