From nobody Fri May 3 06:28:36 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1509511727907532.3054477789246; Tue, 31 Oct 2017 21:48:47 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 04C0721CEB13F; Tue, 31 Oct 2017 21:44:54 -0700 (PDT) Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 2970220945BDC for ; Tue, 31 Oct 2017 21:44:52 -0700 (PDT) Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 31 Oct 2017 21:48:45 -0700 Received: from ray-dev.ccr.corp.intel.com ([10.239.9.7]) by orsmga005.jf.intel.com with ESMTP; 31 Oct 2017 21:48:43 -0700 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=134.134.136.24; helo=mga09.intel.com; envelope-from=ruiyu.ni@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,327,1505804400"; d="scan'208";a="167665992" From: Ruiyu Ni To: edk2-devel@lists.01.org Date: Wed, 1 Nov 2017 12:48:39 +0800 Message-Id: <20171101044839.1772-1-ruiyu.ni@intel.com> X-Mailer: git-send-email 2.12.2.windows.2 Subject: [edk2] [PATCH v2] MdeModulePkg/PciBus: Disable BME of all devices when entering RT X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael D Kinney , Michael Turner , Jiewen Yao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The patch ensures all DMA transactions are blocked after ExitBootService. If a platform enables IOMMU before and needs disable IOMMU after ExitBootService, the IOMMU should be disabled after PCI bus driver disables BME. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Michael Turner Signed-off-by: Ruiyu Ni Cc: Michael D Kinney Cc: Jiewen Yao Cc: Jeff Fan Reviewed-by: Jiewen.yao@intel.com --- MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h | 2 + MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf | 3 + MdeModulePkg/Bus/Pci/PciBusDxe/PciDeviceSupport.c | 86 +++++++++++++++++++= ++++ 3 files changed, 91 insertions(+) diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h b/MdeModulePkg/Bus/Pci= /PciBusDxe/PciBus.h index 55eb3a5a80..79b5b71082 100644 --- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h +++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h @@ -18,6 +18,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER= EXPRESS OR IMPLIED. =20 #include =20 +#include + #include #include #include diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf b/MdeModulePkg/Bu= s/Pci/PciBusDxe/PciBusDxe.inf index 97608bfcf2..d5b8fab3ca 100644 --- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf +++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf @@ -80,6 +80,9 @@ [LibraryClasses] DebugLib PeCoffLib =20 +[Guids] + gEfiEventExitBootServicesGuid ## SOMETIMES_CONSUMES ##= Event + [Protocols] gEfiPciHotPlugRequestProtocolGuid ## SOMETIMES_PRODUCES gEfiPciIoProtocolGuid ## BY_START diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciDeviceSupport.c b/MdeModuleP= kg/Bus/Pci/PciBusDxe/PciDeviceSupport.c index 97bb971a59..004f2a3b5b 100644 --- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciDeviceSupport.c +++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciDeviceSupport.c @@ -21,6 +21,72 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHE= R EXPRESS OR IMPLIED. LIST_ENTRY mPciDevicePool; =20 /** + Disable Bus Master Enable bit in all devices in the list. + + @param Devices A device list. +**/ +VOID +DisableBmeOnTree ( + IN LIST_ENTRY *Devices + ) +{ + LIST_ENTRY *Link; + PCI_IO_DEVICE *PciIoDevice; + UINT16 Command; + + for ( Link =3D GetFirstNode (Devices) + ; !IsNull (Devices, Link) + ; Link =3D GetNextNode (Devices, Link) + ) { + PciIoDevice =3D PCI_IO_DEVICE_FROM_LINK (Link); + // + // Turn off all children's Bus Master, if any + // + DisableBmeOnTree (&PciIoDevice->ChildList); + + // + // If this is a device that supports BME, disable BME on this device. + // + if ((PciIoDevice->Supports & EFI_PCI_IO_ATTRIBUTE_BUS_MASTER) !=3D 0) { + PCI_READ_COMMAND_REGISTER(PciIoDevice, &Command); + if ((Command & EFI_PCI_COMMAND_BUS_MASTER) !=3D 0) { + Command &=3D ~EFI_PCI_COMMAND_BUS_MASTER; + PCI_SET_COMMAND_REGISTER (PciIoDevice, Command); + DEBUG (( + DEBUG_INFO," %02x %02x %02x %04x\n", + PciIoDevice->BusNumber, PciIoDevice->DeviceNumber, PciIoDevice->= FunctionNumber, + Command + )); + } + } + } +} + +/** + Exit Boot Services Event notification handler. + + Disable Bus Master on any that were enabled during BDS. + + @param[in] Event Event whose notification function is being invoked. + @param[in] Context Pointer to the notification function's context. + +**/ +VOID +EFIAPI +OnExitBootServices ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + DEBUG (( + DEBUG_INFO, + "PciBus: Disable Bus Master of all devices...\n" + " Bus# Device# Function# NewCommand\n" + )); + DisableBmeOnTree(&mPciDevicePool); +} + +/** Initialize the PCI devices pool. =20 **/ @@ -29,7 +95,27 @@ InitializePciDevicePool ( VOID ) { + EFI_EVENT ExitBootServicesEvent; + EFI_STATUS Status; + InitializeListHead (&mPciDevicePool); + + // + // DisableBME on ExitBootServices should be synchonized with any IOMMU E= xitBootServices routine. + // DisableBME should be run before the IOMMU protections are disabled. + // One way to do this is to ensure that the IOMMU ExitBootServices callb= ack runs at TPL_CALLBACK. + // + Status =3D gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + OnExitBootServices, + NULL, + &gEfiEventExitBootServicesGuid, + &ExitBootServicesEvent + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "PciBus: Unable to hook ExitBootServices event - = %r\n", Status)); + } } =20 /** --=20 2.12.2.windows.2 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel