From nobody Thu May 2 09:28:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1519697447320232.6498396083415; Mon, 26 Feb 2018 18:10:47 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 2438020955F36; Mon, 26 Feb 2018 18:04:41 -0800 (PST) Received: from mail-pl0-x243.google.com (mail-pl0-x243.google.com [IPv6:2607:f8b0:400e:c01::243]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id DB6F4209574EF for ; Mon, 26 Feb 2018 18:04:39 -0800 (PST) Received: by mail-pl0-x243.google.com with SMTP id c11-v6so7052144plo.0 for ; Mon, 26 Feb 2018 18:10:45 -0800 (PST) Received: from localhost.localdomain ([104.237.91.49]) by smtp.gmail.com with ESMTPSA id a138sm21289210pfd.47.2018.02.26.18.10.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Feb 2018 18:10:44 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:400e:c01::243; helo=mail-pl0-x243.google.com; envelope-from=heyi.guo@linaro.org; receiver=edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=sweFxy/DtRNHFAwtfqonFKMwz6AI9pLpUfty39xCF8E=; b=C5CD1c4Ka+3t34F+uysT/eQVedn2XUSD4f17/7zg+ow2pKwFxF2pSEX/SRYvWL5ySb v56Cszul0YExkFmCojm58RilDdni28NFuXRYW/pOhg8seq0ew/w5h+D1APxEzqN7ltI6 /pfxW8zdY2TO9HAncMbHp6G27MnpyV/Yx09L8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=sweFxy/DtRNHFAwtfqonFKMwz6AI9pLpUfty39xCF8E=; b=AwsgXON3YcWsa9puavQbe3fQvrKp3Mlt0484pjEMnr2pnS3CVzkMmjWzSGSBOuH2wg 8PXdbdRZfNCaJebfzv45xx+mOkF1nqN7OiJZajl7o/y83w/s7iDdSyLd4A6SSTNL1tkR JbeucJIQOtWe8g065EJS77hYa+8X0G1whsbc5g9ipzQMCdT4PL55VsmxLrnu2k8qJJMU GtXspwIMTDMv0DVP1V9W8cZieO77gE232CZfkicVP71QDnLuM6G1FDjQtoKfvaUuq3FI RSo6eydL+n8Oivj0cdiuGw5/rbc7PnHOKOpahQQLmVQFMeIrulUjpoi9pHATdZx2yK2z 2GvQ== X-Gm-Message-State: APf1xPBTj/LXZpInrI5/KIkLX2yUS3dGnJOTmcQ2AitThFdww2m0lemF DshVJrCbacizqH+h9uNsWlqDyJRAyB4= X-Google-Smtp-Source: AH8x226qIyyELyqygXyPRO8nNedd2xaqRGVHZJb/Hw8tJR/mf4kIyjIdCai9snNm4C/bL3+jLWP2UQ== X-Received: by 2002:a17:902:8c92:: with SMTP id t18-v6mr12562653plo.449.1519697445101; Mon, 26 Feb 2018 18:10:45 -0800 (PST) From: Heyi Guo To: edk2-devel@lists.01.org Date: Tue, 27 Feb 2018 10:09:47 +0800 Message-Id: <1519697389-3525-2-git-send-email-heyi.guo@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519697389-3525-1-git-send-email-heyi.guo@linaro.org> References: <1519697389-3525-1-git-send-email-heyi.guo@linaro.org> Subject: [edk2] [RFC v4 1/3] MdeModulePkg/PciHostBridgeDxe: Add support for address translation X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ruiyu Ni , Eric Dong , Ard Biesheuvel , Heyi Guo , Michael D Kinney , Laszlo Ersek , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" PCI address translation is necessary for some non-x86 platforms. On such platforms, address value (denoted as "device address" or "address in PCI view") set to PCI BAR registers in configuration space might be different from the address which is used by CPU to access the registers in memory BAR or IO BAR spaces (denoted as "host address" or "address in CPU view"). The difference between the two addresses is called "Address Translation Offset" or simply "translation", and can be represented by "Address Translation Offset" in ACPI QWORD Address Space Descriptor (Offset 0x1E). However UEFI and ACPI differs on the definitions of QWORD Address Space Descriptor, and we will follow UEFI definition on UEFI protocols, such as PCI root bridge IO protocol and PCI IO protocol. In UEFI 2.7, "Address Translation Offset" is "Offset to apply to the Starting address to convert it to a PCI address". This means: 1. Translation =3D device address - host address. 2. PciRootBridgeIo->Configuration should return CPU view address, as well as PciIo->GetBarAttributes. Summary of addresses used: 1. Base and Limit in PCI_ROOT_BRIDGE_APERTURE are device address, for it is easy to check whether the address is below 4G or above 4G. 2. Address returned by EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL::GetProposedResources is device address, or else PCI bus driver cannot set correct address into PCI BAR registers. 3. Address returned by PciRootBridgeIo->Configuration is host address per UEFI 2.7 definition. 4. Addresses used in GCD manipulation are host address. 5. Addresses in ResAllocNode of PCI_ROOT_BRIDGE_INSTANCE are host address, for they are allocated from GCD. 6. Address passed to PciHostBridgeResourceConflict is host address, for it comes from ResAllocNode. RESTRICTION: to simplify the situation, we require the alignment of Translation must be larger than any BAR alignment in the same root bridge, so that resource allocation alignment can be applied to both device address and host address. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Heyi Guo Cc: Ruiyu Ni Cc: Ard Biesheuvel Cc: Star Zeng Cc: Eric Dong Cc: Laszlo Ersek Cc: Michael D Kinney --- Notes: v4: - Add ASSERT (FALSE) to default branch in GetTranslationByResourceType [Laszlo] - Fix bug when passing BaseAddress to gDS->AllocateIoSpace and gDS->AllocateMemorySpace [Laszlo] - Add comment for applying alignment to both device address and host address, and add NOTE for the alignment requirement of Translation, as well as in commit message [Laszlo][Ray] - Improve indention for the code in CreateRootBridge [Laszlo] - Improve comment for Translation in PCI_ROOT_BRIDGE_APERTURE definition [Laszlo] - Ignore translation of bus in CreateRootBridge MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostResource.h | 2 + MdeModulePkg/Include/Library/PciHostBridgeLib.h | 14 +++ MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostBridge.c | 85 +++++++++++-= -- MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciRootBridgeIo.c | 122 ++++++++++++= +++++--- 4 files changed, 194 insertions(+), 29 deletions(-) diff --git a/MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostResource.h b/MdeM= odulePkg/Bus/Pci/PciHostBridgeDxe/PciHostResource.h index 8612c0c3251b..662c2dd59529 100644 --- a/MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostResource.h +++ b/MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostResource.h @@ -38,6 +38,8 @@ typedef enum { =20 typedef struct { PCI_RESOURCE_TYPE Type; + // Base is a host address instead of a device address when address trans= lation + // exists and host address !=3D device address UINT64 Base; UINT64 Length; UINT64 Alignment; diff --git a/MdeModulePkg/Include/Library/PciHostBridgeLib.h b/MdeModulePkg= /Include/Library/PciHostBridgeLib.h index d42e9ecdd763..c842a4152e85 100644 --- a/MdeModulePkg/Include/Library/PciHostBridgeLib.h +++ b/MdeModulePkg/Include/Library/PciHostBridgeLib.h @@ -20,8 +20,22 @@ // (Base > Limit) indicates an aperture is not available. // typedef struct { + // Base and Limit are the device address instead of host address when + // Translation is not zero UINT64 Base; UINT64 Limit; + // According to UEFI 2.7, Device Address =3D Host Address + Translation, + // so Translation =3D Device Address - Host Address. + // On platforms where Translation is not zero, the subtraction is probab= ly to + // be performed with UINT64 wrap-around semantics, for we may translate = an + // above-4G host address into a below-4G device address for legacy PCIe = device + // compatibility. + // NOTE: The alignment of Translation is required to be larger than any = BAR + // alignment in the same root bridge, so that the same alignment can be + // applied to both device address and host address, which simplifies the + // situation and makes the current resource allocation code in generic P= CI + // host bridge driver still work. + UINT64 Translation; } PCI_ROOT_BRIDGE_APERTURE; =20 typedef struct { diff --git a/MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostBridge.c b/MdeMod= ulePkg/Bus/Pci/PciHostBridgeDxe/PciHostBridge.c index 1494848c3e8c..1e65faee9084 100644 --- a/MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostBridge.c +++ b/MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostBridge.c @@ -32,6 +32,30 @@ EDKII_IOMMU_PROTOCOL *mIoMmuProtocol; EFI_EVENT mIoMmuEvent; VOID *mIoMmuRegistration; =20 +STATIC +UINT64 +GetTranslationByResourceType ( + IN PCI_ROOT_BRIDGE_INSTANCE *RootBridge, + IN PCI_RESOURCE_TYPE ResourceType + ) +{ + switch (ResourceType) { + case TypeIo: + return RootBridge->Io.Translation; + case TypeMem32: + return RootBridge->Mem.Translation; + case TypePMem32: + return RootBridge->PMem.Translation; + case TypeMem64: + return RootBridge->MemAbove4G.Translation; + case TypePMem64: + return RootBridge->PMemAbove4G.Translation; + default: + ASSERT (FALSE); + return 0; + } +} + /** Ensure the compatibility of an IO space descriptor with the IO aperture. =20 @@ -366,6 +390,7 @@ InitializePciHostBridge ( UINTN MemApertureIndex; BOOLEAN ResourceAssigned; LIST_ENTRY *Link; + UINT64 TempHostAddress; =20 RootBridges =3D PciHostBridgeGetRootBridges (&RootBridgeCount); if ((RootBridges =3D=3D NULL) || (RootBridgeCount =3D=3D 0)) { @@ -411,18 +436,24 @@ InitializePciHostBridge ( } =20 if (RootBridges[Index].Io.Base <=3D RootBridges[Index].Io.Limit) { + // Base and Limit in PCI_ROOT_BRIDGE_APERTURE are device address. + // According to UEFI 2.7, device address =3D host address + Translat= ion. + // For GCD resource manipulation, we should use host address, so + // Translation is subtracted from device address here. Status =3D AddIoSpace ( - RootBridges[Index].Io.Base, + RootBridges[Index].Io.Base - RootBridges[Index].Io.Transl= ation, RootBridges[Index].Io.Limit - RootBridges[Index].Io.Base = + 1 ); ASSERT_EFI_ERROR (Status); if (ResourceAssigned) { + TempHostAddress =3D RootBridges[Index].Io.Base - + RootBridges[Index].Io.Translation; Status =3D gDS->AllocateIoSpace ( EfiGcdAllocateAddress, EfiGcdIoTypeIo, 0, RootBridges[Index].Io.Limit - RootBridges[Index].I= o.Base + 1, - &RootBridges[Index].Io.Base, + &TempHostAddress, gImageHandle, NULL ); @@ -443,14 +474,18 @@ InitializePciHostBridge ( =20 for (MemApertureIndex =3D 0; MemApertureIndex < ARRAY_SIZE (MemApertur= es); MemApertureIndex++) { if (MemApertures[MemApertureIndex]->Base <=3D MemApertures[MemApertu= reIndex]->Limit) { + // Base and Limit in PCI_ROOT_BRIDGE_APERTURE are device address. + // According to UEFI 2.7, device address =3D host address + Transl= ation. + // For GCD resource manipulation, we should use host address, so + // Translation is subtracted from device address here. Status =3D AddMemoryMappedIoSpace ( - MemApertures[MemApertureIndex]->Base, + MemApertures[MemApertureIndex]->Base - MemApertures[Mem= ApertureIndex]->Translation, MemApertures[MemApertureIndex]->Limit - MemApertures[Me= mApertureIndex]->Base + 1, EFI_MEMORY_UC ); ASSERT_EFI_ERROR (Status); Status =3D gDS->SetMemorySpaceAttributes ( - MemApertures[MemApertureIndex]->Base, + MemApertures[MemApertureIndex]->Base - MemAperture= s[MemApertureIndex]->Translation, MemApertures[MemApertureIndex]->Limit - MemApertur= es[MemApertureIndex]->Base + 1, EFI_MEMORY_UC ); @@ -458,12 +493,14 @@ InitializePciHostBridge ( DEBUG ((DEBUG_WARN, "PciHostBridge driver failed to set EFI_MEMO= RY_UC to MMIO aperture - %r.\n", Status)); } if (ResourceAssigned) { + TempHostAddress =3D MemApertures[MemApertureIndex]->Base - + MemApertures[MemApertureIndex]->Translation; Status =3D gDS->AllocateMemorySpace ( EfiGcdAllocateAddress, EfiGcdMemoryTypeMemoryMappedIo, 0, MemApertures[MemApertureIndex]->Limit - MemApert= ures[MemApertureIndex]->Base + 1, - &MemApertures[MemApertureIndex]->Base, + &TempHostAddress, gImageHandle, NULL ); @@ -654,6 +691,11 @@ AllocateResource ( if (BaseAddress < Limit) { // // Have to make sure Aligment is handled since we are doing direct add= ress allocation + // Strictly speaking, alignment requirement should be applied to device + // address instead of host address which is used in GCD manipulation b= elow, + // but as we restrict the alignment of Translation to be larger than a= ny BAR + // alignment in the root bridge, we can simplify the situation and con= sider + // the same alignment requirement is also applied to host address. // BaseAddress =3D ALIGN_VALUE (BaseAddress, LShiftU64 (1, BitsOfAlignmen= t)); =20 @@ -824,12 +866,17 @@ NotifyPhase ( =20 switch (Index) { case TypeIo: + // Base and Limit in PCI_ROOT_BRIDGE_APERTURE are device addre= ss. + // According to UEFI 2.7, device address =3D host address + Tr= anslation. + // For AllocateResource is manipulating GCD resource, we shoul= d use + // host address here, so Translation is subtracted from Base a= nd + // Limit. BaseAddress =3D AllocateResource ( FALSE, RootBridge->ResAllocNode[Index].Length, MIN (15, BitsOfAlignment), - ALIGN_VALUE (RootBridge->Io.Base, Alignment + = 1), - RootBridge->Io.Limit + ALIGN_VALUE (RootBridge->Io.Base, Alignment + = 1) - RootBridge->Io.Translation, + RootBridge->Io.Limit - RootBridge->Io.Translat= ion ); break; =20 @@ -838,8 +885,8 @@ NotifyPhase ( TRUE, RootBridge->ResAllocNode[Index].Length, MIN (63, BitsOfAlignment), - ALIGN_VALUE (RootBridge->MemAbove4G.Base, Alig= nment + 1), - RootBridge->MemAbove4G.Limit + ALIGN_VALUE (RootBridge->MemAbove4G.Base, Alig= nment + 1) - RootBridge->MemAbove4G.Translation, + RootBridge->MemAbove4G.Limit - RootBridge->Mem= Above4G.Translation ); if (BaseAddress !=3D MAX_UINT64) { break; @@ -853,8 +900,8 @@ NotifyPhase ( TRUE, RootBridge->ResAllocNode[Index].Length, MIN (31, BitsOfAlignment), - ALIGN_VALUE (RootBridge->Mem.Base, Alignment += 1), - RootBridge->Mem.Limit + ALIGN_VALUE (RootBridge->Mem.Base, Alignment += 1) - RootBridge->Mem.Translation, + RootBridge->Mem.Limit - RootBridge->Mem.Transl= ation ); break; =20 @@ -863,8 +910,8 @@ NotifyPhase ( TRUE, RootBridge->ResAllocNode[Index].Length, MIN (63, BitsOfAlignment), - ALIGN_VALUE (RootBridge->PMemAbove4G.Base, Ali= gnment + 1), - RootBridge->PMemAbove4G.Limit + ALIGN_VALUE (RootBridge->PMemAbove4G.Base, Ali= gnment + 1) - RootBridge->PMemAbove4G.Translation, + RootBridge->PMemAbove4G.Limit - RootBridge->PM= emAbove4G.Translation ); if (BaseAddress !=3D MAX_UINT64) { break; @@ -877,8 +924,8 @@ NotifyPhase ( TRUE, RootBridge->ResAllocNode[Index].Length, MIN (31, BitsOfAlignment), - ALIGN_VALUE (RootBridge->PMem.Base, Alignment = + 1), - RootBridge->PMem.Limit + ALIGN_VALUE (RootBridge->PMem.Base, Alignment = + 1) - RootBridge->PMem.Translation, + RootBridge->PMem.Limit - RootBridge->PMem.Tran= slation ); break; =20 @@ -1152,6 +1199,7 @@ StartBusEnumeration ( Descriptor->AddrSpaceGranularity =3D 0; Descriptor->AddrRangeMin =3D RootBridge->Bus.Base; Descriptor->AddrRangeMax =3D 0; + // Ignore translation offset for bus Descriptor->AddrTranslationOffset =3D 0; Descriptor->AddrLen =3D RootBridge->Bus.Limit - RootBr= idge->Bus.Base + 1; =20 @@ -1421,7 +1469,12 @@ GetProposedResources ( Descriptor->Desc =3D ACPI_ADDRESS_SPACE_DESCRIP= TOR; Descriptor->Len =3D sizeof (EFI_ACPI_ADDRESS_S= PACE_DESCRIPTOR) - 3;; Descriptor->GenFlag =3D 0; - Descriptor->AddrRangeMin =3D RootBridge->ResAllocNode[I= ndex].Base; + // AddrRangeMin in Resource Descriptor here should be device add= ress + // instead of host address, or else PCI bus driver cannot set co= rrect + // address into PCI BAR registers. + // Base in ResAllocNode is a host address, so Translation is add= ed. + Descriptor->AddrRangeMin =3D RootBridge->ResAllocNode[I= ndex].Base + + GetTranslationByResourceType (RootBridge, Index); Descriptor->AddrRangeMax =3D 0; Descriptor->AddrTranslationOffset =3D (ResStatus =3D=3D ResAlloc= ated) ? EFI_RESOURCE_SATISFIED : PCI_RESOURCE_LESS; Descriptor->AddrLen =3D RootBridge->ResAllocNode[I= ndex].Length; diff --git a/MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciRootBridgeIo.c b/MdeM= odulePkg/Bus/Pci/PciHostBridgeDxe/PciRootBridgeIo.c index dc06c16dc038..edaa0d48a441 100644 --- a/MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciRootBridgeIo.c +++ b/MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciRootBridgeIo.c @@ -86,12 +86,28 @@ CreateRootBridge ( (Bridge->AllocationAttributes & EFI_PCI_HOST_BRIDGE_COMBINE_MEM_= PMEM) !=3D 0 ? L"CombineMemPMem " : L"", (Bridge->AllocationAttributes & EFI_PCI_HOST_BRIDGE_MEM64_DECODE= ) !=3D 0 ? L"Mem64Decode" : L"" )); + // We don't see any scenario for bus translation, so translation for bus= is just ignored. DEBUG ((EFI_D_INFO, " Bus: %lx - %lx\n", Bridge->Bus.Base, Bri= dge->Bus.Limit)); - DEBUG ((EFI_D_INFO, " Io: %lx - %lx\n", Bridge->Io.Base, Brid= ge->Io.Limit)); - DEBUG ((EFI_D_INFO, " Mem: %lx - %lx\n", Bridge->Mem.Base, Bri= dge->Mem.Limit)); - DEBUG ((EFI_D_INFO, " MemAbove4G: %lx - %lx\n", Bridge->MemAbove4G.Ba= se, Bridge->MemAbove4G.Limit)); - DEBUG ((EFI_D_INFO, " PMem: %lx - %lx\n", Bridge->PMem.Base, Br= idge->PMem.Limit)); - DEBUG ((EFI_D_INFO, " PMemAbove4G: %lx - %lx\n", Bridge->PMemAbove4G.B= ase, Bridge->PMemAbove4G.Limit)); + DEBUG (( + DEBUG_INFO, " Io: %lx - %lx Translation=3D%lx\n", + Bridge->Io.Base, Bridge->Io.Limit, Bridge->Io.Translation + )); + DEBUG (( + DEBUG_INFO, " Mem: %lx - %lx Translation=3D%lx\n", + Bridge->Mem.Base, Bridge->Mem.Limit, Bridge->Mem.Translation + )); + DEBUG (( + DEBUG_INFO, " MemAbove4G: %lx - %lx Translation=3D%lx\n", + Bridge->MemAbove4G.Base, Bridge->MemAbove4G.Limit, Bridge->MemAbove4G.= Translation + )); + DEBUG (( + DEBUG_INFO, " PMem: %lx - %lx Translation=3D%lx\n", + Bridge->PMem.Base, Bridge->PMem.Limit, Bridge->PMem.Translation + )); + DEBUG (( + DEBUG_INFO, " PMemAbove4G: %lx - %lx Translation=3D%lx\n", + Bridge->PMemAbove4G.Base, Bridge->PMemAbove4G.Limit, Bridge->PMemAbove= 4G.Translation + )); =20 // // Make sure Mem and MemAbove4G apertures are valid @@ -206,7 +222,15 @@ CreateRootBridge ( } RootBridge->ResAllocNode[Index].Type =3D Index; if (Bridge->ResourceAssigned && (Aperture->Limit >=3D Aperture->Base))= { - RootBridge->ResAllocNode[Index].Base =3D Aperture->Base; + // Ignore translation for bus + if (Index =3D=3D TypeBus) { + RootBridge->ResAllocNode[Index].Base =3D Aperture->Base; + } else { + // Base in ResAllocNode is a host address, while Base in Aperture = is a + // device address, so translation needs to be subtracted. + RootBridge->ResAllocNode[Index].Base =3D Aperture->Base - + Aperture->Translation; + } RootBridge->ResAllocNode[Index].Length =3D Aperture->Limit - Apertur= e->Base + 1; RootBridge->ResAllocNode[Index].Status =3D ResAllocated; } else { @@ -403,6 +427,28 @@ RootBridgeIoCheckParameter ( return EFI_SUCCESS; } =20 +EFI_STATUS +RootBridgeIoGetMemTranslationByAddress ( + IN PCI_ROOT_BRIDGE_INSTANCE *RootBridge, + IN UINT64 Address, + IN OUT UINT64 *Translation + ) +{ + if (Address >=3D RootBridge->Mem.Base && Address <=3D RootBridge->Mem.Li= mit) { + *Translation =3D RootBridge->Mem.Translation; + } else if (Address >=3D RootBridge->PMem.Base && Address <=3D RootBridge= ->PMem.Limit) { + *Translation =3D RootBridge->PMem.Translation; + } else if (Address >=3D RootBridge->MemAbove4G.Base && Address <=3D Root= Bridge->MemAbove4G.Limit) { + *Translation =3D RootBridge->MemAbove4G.Translation; + } else if (Address >=3D RootBridge->PMemAbove4G.Base && Address <=3D Roo= tBridge->PMemAbove4G.Limit) { + *Translation =3D RootBridge->PMemAbove4G.Translation; + } else { + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + /** Polls an address in memory mapped I/O space until an exit condition is m= et, or a timeout occurs. @@ -658,13 +704,24 @@ RootBridgeIoMemRead ( ) { EFI_STATUS Status; + PCI_ROOT_BRIDGE_INSTANCE *RootBridge; + UINT64 Translation; =20 Status =3D RootBridgeIoCheckParameter (This, MemOperation, Width, Addres= s, Count, Buffer); if (EFI_ERROR (Status)) { return Status; } - return mCpuIo->Mem.Read (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width, Addr= ess, Count, Buffer); + + RootBridge =3D ROOT_BRIDGE_FROM_THIS (This); + Status =3D RootBridgeIoGetMemTranslationByAddress (RootBridge, Address, = &Translation); + if (EFI_ERROR (Status)) { + return Status; + } + + // Address passed to CpuIo->Mem.Read should be a host address instead of + // device address, so Translation should be subtracted. + return mCpuIo->Mem.Read (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width, Addr= ess - Translation, Count, Buffer); } =20 /** @@ -705,13 +762,24 @@ RootBridgeIoMemWrite ( ) { EFI_STATUS Status; + PCI_ROOT_BRIDGE_INSTANCE *RootBridge; + UINT64 Translation; =20 Status =3D RootBridgeIoCheckParameter (This, MemOperation, Width, Addres= s, Count, Buffer); if (EFI_ERROR (Status)) { return Status; } - return mCpuIo->Mem.Write (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width, Add= ress, Count, Buffer); + + RootBridge =3D ROOT_BRIDGE_FROM_THIS (This); + Status =3D RootBridgeIoGetMemTranslationByAddress (RootBridge, Address, = &Translation); + if (EFI_ERROR (Status)) { + return Status; + } + + // Address passed to CpuIo->Mem.Write should be a host address instead of + // device address, so Translation should be subtracted. + return mCpuIo->Mem.Write (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width, Add= ress - Translation, Count, Buffer); } =20 /** @@ -746,6 +814,8 @@ RootBridgeIoIoRead ( ) { EFI_STATUS Status; + PCI_ROOT_BRIDGE_INSTANCE *RootBridge; + Status =3D RootBridgeIoCheckParameter ( This, IoOperation, Width, Address, Count, Buffer @@ -753,7 +823,12 @@ RootBridgeIoIoRead ( if (EFI_ERROR (Status)) { return Status; } - return mCpuIo->Io.Read (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width, Addre= ss, Count, Buffer); + + RootBridge =3D ROOT_BRIDGE_FROM_THIS (This); + + // Address passed to CpuIo->Io.Read should be a host address instead of + // device address, so Translation should be subtracted. + return mCpuIo->Io.Read (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width, Addre= ss - RootBridge->Io.Translation, Count, Buffer); } =20 /** @@ -788,6 +863,8 @@ RootBridgeIoIoWrite ( ) { EFI_STATUS Status; + PCI_ROOT_BRIDGE_INSTANCE *RootBridge; + Status =3D RootBridgeIoCheckParameter ( This, IoOperation, Width, Address, Count, Buffer @@ -795,7 +872,12 @@ RootBridgeIoIoWrite ( if (EFI_ERROR (Status)) { return Status; } - return mCpuIo->Io.Write (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width, Addr= ess, Count, Buffer); + + RootBridge =3D ROOT_BRIDGE_FROM_THIS (This); + + // Address passed to CpuIo->Io.Write should be a host address instead of + // device address, so Translation should be subtracted. + return mCpuIo->Io.Write (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width, Addr= ess - RootBridge->Io.Translation, Count, Buffer); } =20 /** @@ -1615,25 +1697,39 @@ RootBridgeIoConfiguration ( =20 Descriptor->Desc =3D ACPI_ADDRESS_SPACE_DESCRIPTOR; Descriptor->Len =3D sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3; + // According to UEFI 2.7, RootBridgeIo->Configuration should return ad= dress + // range in CPU view (host address), and ResAllocNode->Base is already= a CPU + // view address (host address). Descriptor->AddrRangeMin =3D ResAllocNode->Base; Descriptor->AddrRangeMax =3D ResAllocNode->Base + ResAllocNode->Lengt= h - 1; Descriptor->AddrLen =3D ResAllocNode->Length; switch (ResAllocNode->Type) { =20 case TypeIo: - Descriptor->ResType =3D ACPI_ADDRESS_SPACE_TYPE_IO; + Descriptor->ResType =3D ACPI_ADDRESS_SPACE_TYPE_IO; + Descriptor->AddrTranslationOffset =3D RootBridge->Io.Translation; break; =20 case TypePMem32: - Descriptor->SpecificFlag =3D EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_= CACHEABLE_PREFETCHABLE; + Descriptor->SpecificFlag =3D EFI_ACPI_MEMORY_RESOURCE_SPECI= FIC_FLAG_CACHEABLE_PREFETCHABLE; + Descriptor->AddrTranslationOffset =3D RootBridge->PMem.Translation; + Descriptor->ResType =3D ACPI_ADDRESS_SPACE_TYPE_MEM; + Descriptor->AddrSpaceGranularity =3D 32; + break; + case TypeMem32: + Descriptor->AddrTranslationOffset =3D RootBridge->Mem.Translation; Descriptor->ResType =3D ACPI_ADDRESS_SPACE_TYPE_MEM; Descriptor->AddrSpaceGranularity =3D 32; break; =20 case TypePMem64: - Descriptor->SpecificFlag =3D EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_= CACHEABLE_PREFETCHABLE; + Descriptor->SpecificFlag =3D EFI_ACPI_MEMORY_RESOURCE_SPECI= FIC_FLAG_CACHEABLE_PREFETCHABLE; + Descriptor->AddrTranslationOffset =3D RootBridge->PMemAbove4G.Transl= ation; + Descriptor->ResType =3D ACPI_ADDRESS_SPACE_TYPE_MEM; + Descriptor->AddrSpaceGranularity =3D 64; case TypeMem64: + Descriptor->AddrTranslationOffset =3D RootBridge->MemAbove4G.Transla= tion; Descriptor->ResType =3D ACPI_ADDRESS_SPACE_TYPE_MEM; Descriptor->AddrSpaceGranularity =3D 64; break; --=20 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Thu May 2 09:28:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1519697449695163.33606416709551; Mon, 26 Feb 2018 18:10:49 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 8827820955F2B; Mon, 26 Feb 2018 18:04:43 -0800 (PST) Received: from mail-pl0-x243.google.com (mail-pl0-x243.google.com [IPv6:2607:f8b0:400e:c01::243]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 26902209574EF for ; Mon, 26 Feb 2018 18:04:42 -0800 (PST) Received: by mail-pl0-x243.google.com with SMTP id w21so10470278plp.11 for ; Mon, 26 Feb 2018 18:10:47 -0800 (PST) Received: from localhost.localdomain ([104.237.91.49]) by smtp.gmail.com with ESMTPSA id a138sm21289210pfd.47.2018.02.26.18.10.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Feb 2018 18:10:47 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:400e:c01::243; helo=mail-pl0-x243.google.com; envelope-from=heyi.guo@linaro.org; receiver=edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=AYSvTpkKH3HkKndCwIKm4x8MwR03fLPgznyqaVdDMa8=; b=UkfZeyX2SARL+o5dHkLdlXATEIezdepys0QYhBzU2CaSo5stdqDB7t2JN0vRGeCMI7 eyihx7Et4swMOkCU+4Zh0GrFJWfFp5D5c1iiHcsnBspl43h35rA/N/22oIHFcEoG+1Ab FItvGR/A3OXJskopW013f7szJ+K7B18ZlH1NE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=AYSvTpkKH3HkKndCwIKm4x8MwR03fLPgznyqaVdDMa8=; b=f0QVlW2BZu8mqqias27wdIFb4bHaf1Dm8FVU7PY61yeB5AhWNxZBlgr9YdVsmFMxZi J95ZMO+nKIITa3F0AC5UPAzRao6rIage6TfK+08c4p4KqZgGvbz50LlUVodkPhgQpSNW 7EP7i3GhyBNrh/bEsQbnoGZ5yrGW66mQDNwoUelsxs/w8TX9x5klAQViiF1BC2qSLsXH 7B61HOIcN8DGhk8A1q8NkeIPqeiKjvrJIQ0qX9QNYBIvLzbOwz4VUWJR14zCtdOhPUO8 7aifys5j3KnyVEsyJMhYG8yhFYplSfBgr2TyFZWqj6RiWfm9/dDrac7JTmRU5idtVwR2 qA6Q== X-Gm-Message-State: APf1xPAbw1OZZJt38qzd8chlX1dZ27c8jtE0YWOPmbJRY65K8weUwaae YsuHwVe5FKwIw+/rhPpA5Q7NAONK9oI= X-Google-Smtp-Source: AH8x224wuZM7YRi7lhankbNrEBRgx2jnGVhQR/B6Irpd9e8EbEmkilNDi74hZq48pOGQZtbZHE13xA== X-Received: by 2002:a17:902:710f:: with SMTP id a15-v6mr12304249pll.87.1519697447479; Mon, 26 Feb 2018 18:10:47 -0800 (PST) From: Heyi Guo To: edk2-devel@lists.01.org Date: Tue, 27 Feb 2018 10:09:48 +0800 Message-Id: <1519697389-3525-3-git-send-email-heyi.guo@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519697389-3525-1-git-send-email-heyi.guo@linaro.org> References: <1519697389-3525-1-git-send-email-heyi.guo@linaro.org> Subject: [edk2] [RFC v4 2/3] MdeModulePkg/PciBus: convert host address to device address X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ruiyu Ni , Eric Dong , Ard Biesheuvel , Heyi Guo , Michael D Kinney , Laszlo Ersek , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" According to UEFI spec 2.7, PciRootBridgeIo->Configuration() should return host address (CPU view ddress) rather than device address (PCI view address), so in function GetMmioAddressTranslationOffset we need to convert the range to device address before comparing. And device address =3D host address + translation offset. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Heyi Guo Cc: Ruiyu Ni Cc: Ard Biesheuvel Cc: Star Zeng Cc: Eric Dong Cc: Laszlo Ersek Cc: Michael D Kinney --- MdeModulePkg/Bus/Pci/PciBusDxe/PciIo.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciIo.c b/MdeModulePkg/Bus/Pci/= PciBusDxe/PciIo.c index 190f4b0dc7ed..fef3eceb7f62 100644 --- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciIo.c +++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciIo.c @@ -1812,10 +1812,14 @@ GetMmioAddressTranslationOffset ( return (UINT64) -1; } =20 + // According to UEFI 2.7, EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL::Configuration= () + // returns host address instead of device address, while AddrTranslation= Offset + // is not zero, and device address =3D host address + AddrTranslationOff= set, so + // we convert host address to device address for range compare. while (Configuration->Desc =3D=3D ACPI_ADDRESS_SPACE_DESCRIPTOR) { if ((Configuration->ResType =3D=3D ACPI_ADDRESS_SPACE_TYPE_MEM) && - (Configuration->AddrRangeMin <=3D AddrRangeMin) && - (Configuration->AddrRangeMin + Configuration->AddrLen >=3D AddrRan= geMin + AddrLen) + (Configuration->AddrRangeMin + Configuration->AddrTranslationOffse= t <=3D AddrRangeMin) && + (Configuration->AddrRangeMin + Configuration->AddrLen + Configurat= ion->AddrTranslationOffset >=3D AddrRangeMin + AddrLen) ) { return Configuration->AddrTranslationOffset; } --=20 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Thu May 2 09:28:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1519697452411179.06243620926227; Mon, 26 Feb 2018 18:10:52 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 00DC8222630AE; Mon, 26 Feb 2018 18:04:46 -0800 (PST) Received: from mail-pl0-x243.google.com (mail-pl0-x243.google.com [IPv6:2607:f8b0:400e:c01::243]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 5DC48209574EF for ; Mon, 26 Feb 2018 18:04:44 -0800 (PST) Received: by mail-pl0-x243.google.com with SMTP id s13so10466564plq.6 for ; Mon, 26 Feb 2018 18:10:50 -0800 (PST) Received: from localhost.localdomain ([104.237.91.49]) by smtp.gmail.com with ESMTPSA id a138sm21289210pfd.47.2018.02.26.18.10.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Feb 2018 18:10:49 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:400e:c01::243; helo=mail-pl0-x243.google.com; envelope-from=heyi.guo@linaro.org; receiver=edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=KY/TY1vQXahnUmVIDRxMzVNKvNFaI0EiawWm+wlL7A4=; b=HXr2a70ixIhw+KlLpKzPw0sFe2nspI4bvqBiicCzM8F/K+dAhA7WYTlJvgsZCW++6X B3MTTQO0CtZNiem8eOMewrltysUUH+uf8d4IzdcGoeLs4jdH/reVAvMH1G4kCK9RVMyZ ixs0zOEocgfLGnCBe73M1ZvU4zMEc73fU3NRo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=KY/TY1vQXahnUmVIDRxMzVNKvNFaI0EiawWm+wlL7A4=; b=N9HB8U1kZrtXNqa+hFwHg73zes88XxHHmMDYd+sVD6Jz+G1qwo+GHYOzlJkTGvMicI UZQUgqds7O7oi5WLnnATWd4N+50ueXpQ7U6qio/MM9Hc5+bjojvjUYhyjNEe2SwcfGOo QgoXwfXhqGqNLkgnskWzM1+V1RL8eUdjUlw+x4gjBx3lHi2+s3meEwUzKPNtnsLXshVc t9ZGN5roHtEgUDmHp11T8CfqmGVumWkzvPuemBM87kPOFcrigzd6/IUmNHiDr6oaDXjM Ba2DaunQJtBzeIM6layyL4p9H9hidzTKvwUA1faGIkxv/1aoDmRH4QbSavKfLdme3+rt RlOg== X-Gm-Message-State: APf1xPAFCVpuT16O7ML+DfY7e01SncYLlVZri/wX+WwiVDyoPXAtSJDg Auly1Oz0nDAykJ8Mey1d0mJyGX7t5QU= X-Google-Smtp-Source: AH8x226hKCs+q46G4iZc1HPF9yN+P40jjFLWsTAB4Wo1uGOFpehTw5LqttV8e6VSM6Rui/r9tih5QA== X-Received: by 2002:a17:902:8f95:: with SMTP id z21-v6mr12618954plo.132.1519697449738; Mon, 26 Feb 2018 18:10:49 -0800 (PST) From: Heyi Guo To: edk2-devel@lists.01.org Date: Tue, 27 Feb 2018 10:09:49 +0800 Message-Id: <1519697389-3525-4-git-send-email-heyi.guo@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519697389-3525-1-git-send-email-heyi.guo@linaro.org> References: <1519697389-3525-1-git-send-email-heyi.guo@linaro.org> Subject: [edk2] [RFC v4 3/3] MdeModulePkg/PciBus: return CPU address for GetBarAttributes X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ruiyu Ni , Eric Dong , Ard Biesheuvel , Heyi Guo , Michael D Kinney , Laszlo Ersek , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" According to UEFI spec 2.7, PciIo->GetBarAttributes should return host address (CPU view ddress) rather than device address (PCI view address), and device address =3D host address + address translation offset, so we subtract translation from device address before returning. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Heyi Guo Cc: Ruiyu Ni Cc: Ard Biesheuvel Cc: Star Zeng Cc: Eric Dong Cc: Laszlo Ersek Cc: Michael D Kinney --- MdeModulePkg/Bus/Pci/PciBusDxe/PciIo.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciIo.c b/MdeModulePkg/Bus/Pci/= PciBusDxe/PciIo.c index fef3eceb7f62..62179eb44bbd 100644 --- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciIo.c +++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciIo.c @@ -1972,6 +1972,10 @@ PciIoGetBarAttributes ( return EFI_UNSUPPORTED; } } + + // According to UEFI spec 2.7, we need return host address for + // PciIo->GetBarAttributes, and host address =3D device address - tran= slation. + Descriptor->AddrRangeMin -=3D Descriptor->AddrTranslationOffset; } =20 return EFI_SUCCESS; --=20 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel