15
15
16
16
#include <Protocol/PciRootBridgeIo.h>
17
17
18
+ #include <Library/IoMmuLib.h> // MU_CHANGE
19
+
18
20
typedef struct {
19
21
EFI_PHYSICAL_ADDRESS AllocAddress ;
20
22
VOID * HostAddress ;
21
23
EFI_PCI_IO_PROTOCOL_OPERATION Operation ;
22
24
UINTN NumberOfBytes ;
25
+ VOID * IoMmuContext ; // MU_CHANGE
23
26
} NON_DISCOVERABLE_PCI_DEVICE_MAP_INFO ;
24
27
25
28
/**
@@ -1278,6 +1281,13 @@ NonCoherentPciIoMap (
1278
1281
EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor ;
1279
1282
BOOLEAN Bounce ;
1280
1283
1284
+ // MU_CHANGE [BEGIN]
1285
+ VOID * IoMmuHostAddress ;
1286
+ EDKII_IOMMU_OPERATION IoMmuOperation ;
1287
+ UINT64 IoMmuAttribute ;
1288
+
1289
+ // MU_CHANGE [END]
1290
+
1281
1291
AllocAddress = NULL ; // MS_CHANGE for vs2017
1282
1292
1283
1293
if ((HostAddress == NULL ) ||
@@ -1303,6 +1313,7 @@ NonCoherentPciIoMap (
1303
1313
MapInfo -> HostAddress = HostAddress ;
1304
1314
MapInfo -> Operation = Operation ;
1305
1315
MapInfo -> NumberOfBytes = * NumberOfBytes ;
1316
+ MapInfo -> IoMmuContext = NULL ; // MU_CHANGE
1306
1317
1307
1318
Dev = NON_DISCOVERABLE_PCI_DEVICE_FROM_PCI_IO (This );
1308
1319
@@ -1372,10 +1383,12 @@ NonCoherentPciIoMap (
1372
1383
gBS -> CopyMem (AllocAddress , HostAddress , * NumberOfBytes );
1373
1384
}
1374
1385
1375
- * DeviceAddress = MapInfo -> AllocAddress ;
1386
+ * DeviceAddress = MapInfo -> AllocAddress ;
1387
+ IoMmuHostAddress = (VOID * )(UINTN )MapInfo -> AllocAddress ; // MU_CHANGE
1376
1388
} else {
1377
1389
MapInfo -> AllocAddress = 0 ;
1378
1390
* DeviceAddress = (EFI_PHYSICAL_ADDRESS )(UINTN )HostAddress ;
1391
+ IoMmuHostAddress = HostAddress ; // MU_CHANGE
1379
1392
1380
1393
//
1381
1394
// We are not using a bounce buffer: the mapping is sufficiently
@@ -1396,6 +1409,60 @@ NonCoherentPciIoMap (
1396
1409
}
1397
1410
1398
1411
* 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
+
1399
1466
return EFI_SUCCESS ;
1400
1467
1401
1468
FreeMapInfo :
@@ -1422,12 +1489,26 @@ NonCoherentPciIoUnmap (
1422
1489
)
1423
1490
{
1424
1491
NON_DISCOVERABLE_PCI_DEVICE_MAP_INFO * MapInfo ;
1492
+ EFI_STATUS Status ; // MU_CHANGE
1425
1493
1426
1494
if (Mapping == NULL ) {
1427
1495
return EFI_DEVICE_ERROR ;
1428
1496
}
1429
1497
1430
1498
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
+
1431
1512
if (MapInfo -> AllocAddress != 0 ) {
1432
1513
//
1433
1514
// We are using a bounce buffer: copy back the data if necessary,
0 commit comments