From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by mx.groups.io with SMTP id smtpd.web11.75731.1673574421384987319 for ; Thu, 12 Jan 2023 17:47:01 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=H68cQplu; spf=pass (domain: intel.com, ip: 134.134.136.100, mailfrom: isaac.w.oram@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673574421; x=1705110421; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=V3rLH6kavchSgElfgjOG61rtoVKZ4yKzJz1ESovBr6o=; b=H68cQplunw+QAAQwmYWlyWlugKvcDmJow/Up3wKSeO6NWrjohe+P/zdB tRdzYqu9ry1tgaaSRctkU8+cMpu8b7rFGBzcRzGHgS3P1810wEaMx0sj1 4x+Bq27C7xasCxW0EYGMohCrRQKd8dPtBQKJUv6+7tJsE7IZ/SQC46oAE 6KQUIdpDHfHJKobTOIisfuaZ2i8NK5mk9KouX5Mo/xitIktVCVa10Vzk2 mWK7i7xOy1SD0lbE16N6wNUn1YtHqajNu3V+PcTpG32pADY/584Fq5azA fD/6mdN4M0nPu2T9zGv3aHG/WtCHF8rRwF/0lbjC4g5kbPIUiMpN7jSNX w==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="388382636" X-IronPort-AV: E=Sophos;i="5.97,212,1669104000"; d="scan'208";a="388382636" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 17:47:00 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="726538642" X-IronPort-AV: E=Sophos;i="5.97,212,1669104000"; d="scan'208";a="726538642" Received: from orsmsx602.amr.corp.intel.com ([10.22.229.15]) by fmsmga004.fm.intel.com with ESMTP; 12 Jan 2023 17:46:59 -0800 Received: from orsmsx611.amr.corp.intel.com (10.22.229.24) by ORSMSX602.amr.corp.intel.com (10.22.229.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Thu, 12 Jan 2023 17:46:59 -0800 Received: from orsmsx610.amr.corp.intel.com (10.22.229.23) by ORSMSX611.amr.corp.intel.com (10.22.229.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Thu, 12 Jan 2023 17:46:58 -0800 Received: from ORSEDG602.ED.cps.intel.com (10.7.248.7) by orsmsx610.amr.corp.intel.com (10.22.229.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16 via Frontend Transport; Thu, 12 Jan 2023 17:46:58 -0800 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (104.47.57.40) by edgegateway.intel.com (134.134.137.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.16; Thu, 12 Jan 2023 17:46:58 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bxlwg8fX8d6WIOtTi7ZM6mRibHmJHIJzF0qhVDKlAvNW4Qn0aft7WZxu4WNFSMLJSk3NKBNDkaEKeRo6WAT531B7YelQwkP+vW89+0DAr6JstR+DGHYQrZ77Ol3VL+cOpuRka5EoiVIQE2dIk639aaGpJRYPVoR/rtAfdRbsavqbTju871BVoIHCtGUqc0ploYkDe0OW2TqZG48QiYNGn7/JuqYqnhoKcxThSrzcHBpzeDk9B6gu0tmn8frQx44YG148xL6bEOV8UIvqXGjMGBawFq2AOKzOqOT3ukXhTZ1g7Qnae3zsc71ti9xrujdkflZIkMRpIBc/0fEAFDsJ3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=iVxVQzs7ZrYfe5hFI/Az2vLbUbZOMaJ2G88CUpzyXA4=; b=L1QqwjYtITG8GT4Z2P3SO2Xr0ezsQCjQH17/ctRZzUmcE2Iu3okCXAxty73nGJuQ0DrJUmmI2lD2ZjuSsaHhM11ol6nGk/vHEh3g3J0YRrgcjbm9pCwLyHgKvrPGt+rxiiYj95hqKjWCg+OLeaSOOJAO3cyjKSH4iqgs8RKMI0uSMRUgiLhXhB4OOWRB3/Ylc+BJhACklQpU4uXtI+OKfpm6pT5pK6THyW/g2vDGyuA2f8/FcaItFEOQkIZYoxfY/dD1d7MO4CifwAxm9TmUU7JTzUT+soy+JFlZqxDSa8beHc3S/XGWS5lcDo4k+acZZwfoPEf8c7V26Hu214w/iw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from SA1PR11MB5801.namprd11.prod.outlook.com (2603:10b6:806:23d::13) by SA1PR11MB6869.namprd11.prod.outlook.com (2603:10b6:806:29c::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5986.18; Fri, 13 Jan 2023 01:46:56 +0000 Received: from SA1PR11MB5801.namprd11.prod.outlook.com ([fe80::7623:38f5:ab31:61a5]) by SA1PR11MB5801.namprd11.prod.outlook.com ([fe80::7623:38f5:ab31:61a5%5]) with mapi id 15.20.5986.019; Fri, 13 Jan 2023 01:46:56 +0000 From: "Isaac Oram" To: "devel@edk2.groups.io" CC: Theo Jehl , Gerd Hoffmann Subject: Re: [PATCH edk2-platforms 1/2] QemuOpenBoardPkg: Redo PCI bus initialization Thread-Topic: [PATCH edk2-platforms 1/2] QemuOpenBoardPkg: Redo PCI bus initialization Thread-Index: AQHZJtudB5rOKi6v9EC/xE14jPrf+a6bhG9Q Date: Fri, 13 Jan 2023 01:46:37 +0000 Message-ID: References: <20230112231359.452800-1-pedro.falcato@gmail.com> <20230112231359.452800-2-pedro.falcato@gmail.com> In-Reply-To: <20230112231359.452800-2-pedro.falcato@gmail.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: SA1PR11MB5801:EE_|SA1PR11MB6869:EE_ x-ms-office365-filtering-correlation-id: 7225dfa6-e641-4d38-fa1d-08daf5080228 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: Wz3rNDYhXNtpiJ1HrstwLTy3ac6S4RHT7teDrrhDxvTYFmmcXt+2TukKOtApycTMw1vktwSfPsqeVW82ZUbdJlm+cV4U9LSKI8kcWzx31VYF9sPTpbyWR1MAoMskmJJ7TGu+WebsvAY3zCMnY5sBIRxRrvC2yq9vASxkn5mg/O2iN5/KlGxE0/+/saudhlLR+vMzRxLKVsqdoyLnwncbpfxIHbYoCuEgB1LIzn9TxEHJKg5xrhrSUNZ4JCvhUjfBMM+lVJHlki9oc0+sUGrV64dDBN9hzTf4sn9EXS8a6YIU9C2+VBMIC8xx4dIZcmjkY9L6HpXP4LWDGoBcal9Hb3xdOmcDr+WlBIm1t+u0Scyx+HXG6khxXldPEarmhV+x+90hNZTsfU3+WKreKyyt40h7a5gYGIPcLM2riKBhTaP0BJ+A0ac0pBgDiAraLFGBZ0QwsQclsiNSwh3zALxCQG6cHy80WtmHw8jU5Tkgcw+vLS/NY3Cmdi7M6I5/IQ9afL5EIlPbgZ6QmwNvlwMj0F4NWKginhYKU67p5CIEkJk7SRmCuDOTDPrBHVVm2ML2wdQin9rogd6i+1JWwGKy8lwCexhE09k1qws3LRczqN7N6UU6Y1LR0exUe+LVWF/xEO+0izU4ubxmdFRaZQDNnwGtzbzyJj9RIqRRPnayaMUHMl8PO3G0JzXTVg3st+yt8GZpHENHdCEOGXev2UlzFA== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA1PR11MB5801.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(366004)(396003)(136003)(39860400002)(346002)(376002)(451199015)(38070700005)(19627235002)(33656002)(30864003)(478600001)(26005)(316002)(7696005)(186003)(5660300002)(71200400001)(9686003)(66446008)(41300700001)(55016003)(66946007)(4326008)(66476007)(64756008)(6916009)(54906003)(8676002)(66556008)(8936002)(52536014)(86362001)(76116006)(83380400001)(2906002)(53546011)(6506007)(38100700002)(122000001)(82960400001)(6666004)(579004);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?deaKp4Z6caHITYgHXTsvc9an34mrtFC+Y1L3qJwisxQkwblwERqfveCmO0B7?= =?us-ascii?Q?QahXL+XtAYbnr/zOFeApEnifdFPKw7AjkHd1cXnLaXC1ogzn4Xy9hq4d5YDh?= =?us-ascii?Q?PpOOwZKTswmKtbQkgl5h9XO+AozTvpOfVUyifENwIZqHJxpYhQwLeCdu6hWH?= =?us-ascii?Q?+XCvnEDV9VjynVY/F5D1M62VnAvQBFUjIEfRNI538X7CqjzyqtxrNYmLypDc?= =?us-ascii?Q?pPUelZ70igJYjBbOAnO1rgNJOnahekvRsNo3IljIvmLUI8JxpAfsXOa6WBcx?= =?us-ascii?Q?5vasOA54HIGPzNoj8XBGhr0z1ufOmqbtW1tPlF1wuhBEudiv5f5WlgPRf0io?= =?us-ascii?Q?CB4kdO73pE4x+a8zdy9Rnl/yZKdMYjpOk0Fbba4hw18bZz635lFk/fwh5mAC?= =?us-ascii?Q?0meOEaANZ2rFVxTTsSLOjJ91g6TC4ap5w0qZ7CvLrPO9Lad7PIOxhvcMG0yn?= =?us-ascii?Q?2bCizzxehXlXQmbhK/W79DySBT8dTTaU8Mfak03EbKvCIGXXJ94nLIyE1I4K?= =?us-ascii?Q?Tyr5lgRkSBl79oyp2PB9aNFQOG7oxGAkRkuPCe6r2EqvpOmK/0VOaFYNZUc9?= =?us-ascii?Q?lA3xwSFx5E1xgQHwm+tpVkinf4mBCeoyPp7r2AJP/BnL1J+SottM7t05uo3b?= =?us-ascii?Q?o/nGRz0DCjuIzA6bzajpijDZkhHiaCOqTOZP+ET4x3PSCjRo0pFX9G8kX8AG?= =?us-ascii?Q?SfQf7JW6T3JcRlZ1qLFVutpMFEMy+9niSQbZJZUypSc37JemURvkStf/ovBQ?= =?us-ascii?Q?kNESpca3lc7ch2qJDWhBmt5d5kZ8TcRmfvQ9CybjC0TWiQEAVNcV7eUdwwcK?= =?us-ascii?Q?jnHi5p9uMbegFIEvSfnb2tDjq5fN+bmeCpWcAzRs7+VyT7T82Zy4wXdvyP/a?= =?us-ascii?Q?Xcwz2TZBbg4gW23boI67SVAcqp57rwDGJy/vqDr521Ysr9vkWW9sDkWNVbx2?= =?us-ascii?Q?93JpZ4pid49S2igJYyJfGgmNp9Q+iPXhlbdAEXyKyNV3u4wEG45+EmLs0jZ8?= =?us-ascii?Q?f+mwVmRRqnyWlLqpUl+0e2rI+8dWm9jkrS+SvLQTKgKQqS8wW8YiRBxrmJx7?= =?us-ascii?Q?z4rvkJlZ3FuInxRCZxsyQAZZLTo+QKXDd5QID+oO6KnUthrBVQNN/vOXJjFh?= =?us-ascii?Q?muGtpb8OEnm1nAQbpTHPKu5PQ1ZZS5yMlGsAxilL9/18NLNkzdLZwWKgI0qk?= =?us-ascii?Q?I0kueBRhweoaYqFbc3c+OyaDmvOEu/yqGqeGrcAA9XkSg1cgPpo7udM21Nkd?= =?us-ascii?Q?/MZyNbN2jmH8MXcGGYKXQb72kn9QIiB//s9gRQ8DqCKzRVSabsHqy4o68MsM?= =?us-ascii?Q?2Hojh4Bf8JIqqdl2UxJg1UxImMI0YmeH9WeiKsyud550VFj2auZKDEio2M+8?= =?us-ascii?Q?DOley6NAh9VPSyvLC1hibpEeVSa8obwVak7pWZm09d2QtWZtWMecU8/FxZYz?= =?us-ascii?Q?s30fchwbmSZ0gw03q1rUsiKruD8xdTzV4BeksQFn3+0GnBkz/mY2oiiIWOGn?= =?us-ascii?Q?j0l86qLj0MQ54PLA58ba9lM/n8tTA347FP4NuXekHhk5Vhjk9UrGjhdZm7Xe?= =?us-ascii?Q?lJCiMepclk6CxewkZF5G3zysh5KXjla6YHoRJMA0?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SA1PR11MB5801.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7225dfa6-e641-4d38-fa1d-08daf5080228 X-MS-Exchange-CrossTenant-originalarrivaltime: 13 Jan 2023 01:46:37.0817 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: bkT47QFSs2ObxuzoHcIOMtAkbCyH3atSvX5wxcQnrIwINTphRkXwfgRKwI1netRHbWTi682lw1gmHyMk5iVSYQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR11MB6869 Return-Path: isaac.w.oram@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Isaac Oram Minor comments that could be fixed: Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/Memory.c: 65: We generally = don't like includes, defines, etc in the middle of the code. Convention is= near the top of the file so easy to find. Also generally good to initiali= ze everything to a known value. And probably add a comment for what this i= s for. =20 The name strikes me as odd. The traditional name might be TOLUM (top of lo= w usable memory). Or maybe you might find something like mMaxUsableMemoryA= ddress as more readable. It might also be good to assert if it has not been initialized yet when the= function is called. It would be better to return a status. Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/Pci.c: Again, mixing globals= with code. Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInit.h (49) "Initialise" Am I allowed to correct British English spellings? Joking as= ide it looks like a typo to use both Initialise and InitializePci. I would= suggest that Initialize is more common in our codebase. Platform\Qemu\QemuOpenBoardPkg\PlatformInitPei\PlatformInitPei.inf: 49: Du= plicate line of code. C:\Source\Open\edk2-platforms\Platform\Qemu\QemuOpenBoardPkg\QemuOpenBoardP= kg.dsc: =20 The additions are the only | out of vertical alignment. I typically see hex numbers in uppercase. I think that makes it more reada= ble, but I am not sure if that is a formal requirement of coding style. Regards, Isaac -----Original Message----- From: Pedro Falcato =20 Sent: Thursday, January 12, 2023 3:14 PM To: devel@edk2.groups.io Cc: Pedro Falcato ; Oram, Isaac W ; Theo Jehl ; Gerd Hoffmann Subject: [PATCH edk2-platforms 1/2] QemuOpenBoardPkg: Redo PCI bus initiali= zation Rework PCI MMIO space into something more robust and compatible with variou= s QEMU configurations. Also, set up 64-bit memory regions for 64-bit BARs a= nd drop OvmfPkg's PciHostBridgeLib for MinPlatformPkg's PciHostBridgeLibSim= ple, which does the job just fine. (cc Gerd for possible comments given his experience with OVMF and qemu) Signed-off-by: Pedro Falcato Cc: Isaac Oram Cc: Theo Jehl Cc: Gerd Hoffmann --- .../Include/Dsc/Stage2.dsc.inc | 4 +- .../QemuOpenBoardPkg/PlatformInitPei/Memory.c | 22 +- .../QemuOpenBoardPkg/PlatformInitPei/Pci.c | 227 +++++++++++++++--- .../QemuOpenBoardPkg/PlatformInitPei/Pcie.c | 106 -------- .../PlatformInitPei/PlatformInit.c | 17 +- .../PlatformInitPei/PlatformInit.h | 32 ++- .../PlatformInitPei/PlatformInitPei.inf | 18 +- .../QemuOpenBoardPkg/QemuOpenBoardPkg.dsc | 17 +- 8 files changed, 274 insertions(+), 169 deletions(-) delete mode 100644 P= latform/Qemu/QemuOpenBoardPkg/PlatformInitPei/Pcie.c diff --git a/Platform/Qemu/QemuOpenBoardPkg/Include/Dsc/Stage2.dsc.inc b/Pl= atform/Qemu/QemuOpenBoardPkg/Include/Dsc/Stage2.dsc.inc index d2e41ce79fda..c240c38cabef 100644 --- a/Platform/Qemu/QemuOpenBoardPkg/Include/Dsc/Stage2.dsc.inc +++ b/Platform/Qemu/QemuOpenBoardPkg/Include/Dsc/Stage2.dsc.inc @@ -3,14 +3,14 @@ # # @copyright # Copyright (C) 2022 Theo Jehl +# Copyright (c) 2023 Pedro Falcato All rights reserved. # # SPDX-License-Identifier: BSD-2-Clause-Patent ## =20 [LibraryClasses.Common] ResetSystemLib | OvmfPkg/Library/ResetSystemLib/BaseResetSystem= Lib.inf - PciHostBridgeLib | OvmfPkg/Library/PciHostBridgeLib/PciHostBridge= Lib.inf - PciHostBridgeUtilityLib | OvmfPkg/Library/PciHostBridgeUtilityLib/PciHos= tBridgeUtilityLib.inf + PciHostBridgeLib | MinPlatformPkg/Pci/Library/PciHostBridgeLibSim= ple/PciHostBridgeLibSimple.inf DxeHardwareInfoLib | OvmfPkg/Library/HardwareInfoLib/DxeHardwareInf= oLib.inf =20 [LibraryClasses.Common.PEIM] diff --git a/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/Memory.c b/Plat= form/Qemu/QemuOpenBoardPkg/PlatformInitPei/Memory.c index 21705256191b..4f312c36016e 100644 --- a/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/Memory.c +++ b/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/Memory.c @@ -2,6 +2,7 @@ Memory probing and installation =20 Copyright (c) 2022 Theo Jehl All rights reserved. + Copyright (c) 2023 Pedro Falcato All rights reserved. SPDX-License-Identifier: BSD-2-Clause-Patent **/ =20 @@ -53,12 +54,27 @@ GetMemoryBelow4Gb ( Size +=3D E820Entry.Length; } else { ASSERT (Size =3D=3D (UINT32)Size); - return (UINT32) Size; + return (UINT32)Size; } } =20 ASSERT (Size =3D=3D (UINT32)Size); - return (UINT32) Size; + return (UINT32)Size; +} + +STATIC UINT64 mTopNonHoleAddr; + +/** + Return the largest reserved/DRAM/etc address. + + @return Largest non-hole address. +**/ +UINT64 +GetTopNonHoleAddr ( + VOID + ) +{ + return mTopNonHoleAddr; } =20 /** @@ -222,6 +238,8 @@ InstallMemory ( E820Entry.BaseAddr + E820Entry.Length - 1, E820Entry.Type )); + + mTopNonHoleAddr =3D MAX (mTopNonHoleAddr, E820Entry.BaseAddr +=20 + E820Entry.Length - 1); } =20 ASSERT (LargestE820Entry.Length !=3D 0); diff --git a/Platform/Qemu/Qemu= OpenBoardPkg/PlatformInitPei/Pci.c b/Platform/Qemu/QemuOpenBoardPkg/Platfor= mInitPei/Pci.c index 4e6b784d9890..d2724d205552 100644 --- a/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/Pci.c +++ b/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/Pci.c @@ -1,7 +1,8 @@ /** @file Pci.c - PCI Initialization for PIIX4 QEMU + PCI Initialization for QEMU platforms =20 Copyright (c) 2022 Theo Jehl All rights reserved. + Copyright (c) 2023 Pedro Falcato All rights reserved. SPDX-License-Identifier: BSD-2-Clause-Patent **/ =20 @@ -9,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -16,55 +18,220 @@ #include #include =20 +typedef EFI_STATUS (*SETUP_PCI_CALLBACK)( + VOID + ); + +typedef struct PlatformInfo { + UINT16 PciIoBase; + UINT16 PciIoSize; + BOOLEAN HasPcie; + SETUP_PCI_CALLBACK SetupPci; +} QEMU_PLATFORM_INFO; + +/** + Set up PCI on the q35 platform. + + @retval EFI_SUCCESS Success. +**/ +STATIC +EFI_STATUS +PciSetupQ35 ( + VOID + ); + +STATIC +CONST +QEMU_PLATFORM_INFO PlatformData[] =3D +{ + { PIIX4_PCI_IO_BASE, PIIX4_PCI_IO_SIZE, FALSE, NULL }, + { Q35_PCI_IO_BASE, Q35_PCI_IO_SIZE, TRUE, PciSetupQ35 } +}; + +/** + Get platform info for the given platform. + range PCDs. Asserts on bad QEMU_PLATFORMs. + + @retval Pointer to the QEMU_PLATFORM_INFO. +**/ +STATIC +CONST +QEMU_PLATFORM_INFO * +GetPlatform ( + IN QEMU_PLATFORM Plat + ) +{ + ASSERT (Plat < QEMU_PLATFORM_MAX); + return &PlatformData[Plat]; +} + /** - Initialize PCI support for QEMU PIIX4 machine. + Initialise PCI QEMU platforms. =20 - It also publishes PCI MMIO and IO ranges PCDs for OVMF PciHostBridgeLib. + Updates PCI IO port and MMIO range PCDs. =20 @retval EFI_SUCCESS Initialization was a success. - @retval EFI_UNSUPPORTED Initialization failed (Memory below 4Gb probing= failed). **/ EFI_STATUS EFIAPI -InitializePciPIIX4 ( +InitializePci ( + IN QEMU_PLATFORM Platform + ) +{ + CONST QEMU_PLATFORM_INFO *PlatformData; + UINT32 Tolud; + UINT32 MemBase; + UINT32 MemSize; + UINT64 Mem64Base; + UINT64 Mem64Size; + UINT32 EcamBase; + + PlatformData =3D GetPlatform (Platform); + + ASSERT (PlatformData !=3D NULL); + + Tolud =3D GetMemoryBelow4Gb (); + + // + // Configure the IO port ranges PCDs based on the platform // =20 + PcdSet16S (PcdPciReservedIobase, PlatformData->PciIoBase); PcdSet16S=20 + (PcdPciReservedIoLimit, PlatformData->PciIoBase +=20 + PlatformData->PciIoSize - 1); + + // + // Set up the PCI MMIO ranges + // Some context around this logic (after diving into QEMU source): + // Traditionally, QEMU seems to want to use a split at around //=20 + 0xe0000000 (3.5GiB). After that things are reserved for PCI MMIO. + // However, PIIX4 grew a gigabyte_align option that can readjust this =20 + // down to 0xc0000000 (3GiB). + // Q35 logic seems to dock lowmem at around 0x80000000 (2GiB) if it=20 + can't // fit the whole lowmem under 4GB. If it can, it limits lowmem to = 0xb0000000. + // + // It's worth noting that QEMU also grew an option to change lowmem=20 + based on the // user's preferences. Because of all of this, it's near=20 + impossible to hardcode // a range, so we grab TOLUD (not in a literal=20 + way, since QEMU does not implement // that register ;)) and calculate=20 + our PCI MMIO based on that. This also makes it so // the DSDT built by Q= EMU will have correct _CRS ranges. + // hw/pci-host/q35.c explicitly says our PCI hole ranges from [TOLUD, IO= APIC]. + // As far as I can tell, we seem to be allowed to add a 64-bit resource = range anywhere. + // + + MemBase =3D Tolud; + MemSize =3D PCI_MMIO_TOP_ADDRESS - MemBase; + + // + // Set things up in the following way: + // ---------------------------------- + // Mem32-decoding-region + // ---------------------------------- + // ECAM/MMCONFIG + // ---------------------------------- + // Misc top regions, 64-bit memory + // ---------------------------------- + // Mem64-decoding-region + // ---------------------------------- + // + + if (PlatformData->HasPcie) { + ASSERT (FixedPcdGet64 (PcdPciExpressBaseAddress) < BASE_4GB); + EcamBase =3D (UINT32)FixedPcdGet64 (PcdPciExpressBaseAddress); + + BuildResourceDescriptorHob ( + EFI_RESOURCE_MEMORY_MAPPED_IO, + EFI_RESOURCE_ATTRIBUTE_PRESENT | + EFI_RESOURCE_ATTRIBUTE_INITIALIZED | + EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE | + EFI_RESOURCE_ATTRIBUTE_TESTED, + EcamBase, + SIZE_256MB + ); + + BuildMemoryAllocationHob ( + EcamBase, + SIZE_256MB, + EfiMemoryMappedIO + ); + + // + // Adjust the PCI MEM window + // + MemSize =3D EcamBase - MemBase; + + DEBUG ((DEBUG_INFO, "PlatformInitPei: Using ECAM @ [%x, %x]\n",=20 + EcamBase, EcamBase + SIZE_256MB - 1)); } + + PcdSetBoolS (PcdPciNoExtendedConfigSpace, !PlatformData->HasPcie); + + PcdSet32S (PcdPciReservedMemBase, MemBase); PcdSet32S=20 + (PcdPciReservedMemLimit, MemBase + MemSize - 1); + + DEBUG ((DEBUG_INFO, "PlatformInitPei: PCI Mem window @ [%x, %x]\n",=20 + MemBase, MemBase + MemSize - 1)); + + // + // For simplicity, start the Mem64 region after every other // valid=20 + non-hole memory region. + // Note: Unclear if this has any disadvantages. + // + Mem64Base =3D GetTopNonHoleAddr (); + + if (Mem64Base < BASE_4GB) { + Mem64Base =3D BASE_4GB; + } + + Mem64Size =3D SIZE_32GB; + + PcdSet64S (PcdPciReservedMemAbove4GBBase, Mem64Base); PcdSet64S=20 + (PcdPciReservedMemAbove4GBLimit, Mem64Base + Mem64Size - 1); + + DEBUG ((DEBUG_INFO, "PlatformInitPei: PCI Mem64 window @ [%lx,=20 + %lx]\n", Mem64Base, Mem64Base + Mem64Size - 1)); + + if (PlatformData->SetupPci) { + EFI_STATUS Status; + Status =3D PlatformData->SetupPci (); + + if (EFI_ERROR (Status)) { + return Status; + } + } + + return EFI_SUCCESS; +} + +/** + Set up PCI on the q35 platform. + + @retval EFI_SUCCESS Success. +**/ +STATIC +EFI_STATUS +PciSetupQ35 ( VOID ) { - UINTN PciIoBase; - UINTN PciIoSize; - UINTN PciMmio32Base; - UINTN PciMmio32Size; + UINT64 PciExBarBase; + + PciExBarBase =3D PcdGet64 (PcdPciExpressBaseAddress); =20 // - // Setup PCI IO ranges for 440FX/PIIX4 platform + // Disable the ECAM before re-programming it. // - PciIoBase =3D PIIX4_PCI_IO_BASE; - PciIoSize =3D PIIX4_PCI_IO_SIZE; - - PcdSet64S (PcdPciIoBase, PciIoBase); - PcdSet64S (PcdPciIoSize, PciIoSize); + PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW), 0); =20 // - // QEMU only allow a maximum of 2.8Gb of real memory below 4G - // PCI MMIO range below 4Gb starts at the end of real memory below 4G + // Now, program the PCIe ECAM into the MCH PCIEXBAR register. We do=20 + it in a high-low order // as to avoid temporary misdecoding of addresses= . // - PciMmio32Base =3D (UINTN)GetMemoryBelow4Gb (); =20 - if (PciMmio32Base =3D=3D 0) { - DEBUG ((DEBUG_ERROR, "Unable to detect memory below 4Gb\n")); - ASSERT (PciMmio32Base !=3D 0); - return EFI_UNSUPPORTED; - } - - DEBUG ((DEBUG_ERROR, "Memory below 4Gb: %x \n", PciMmio32Base)); + PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH),=20 + (UINT32)(RShiftU64 (PciExBarBase, 32))); =20 // - // Maximum size being PCI_MMIO_TOP_ADDRESS - TopOfLowMem to avoid overla= pping with IO-APIC and other hardware mmio ranges + // Finally, program the low bits and simultaneously enable it. // - PciMmio32Size =3D PCI_MMIO_TOP_ADDRESS - PciMmio32Base; - - PcdSet64S (PcdPciMmio32Base, PciMmio32Base); - PcdSet64S (PcdPciMmio32Size, PciMmio32Size); + PciWrite32 ( + DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW), + (UINT32)PciExBarBase | MCH_PCIEXBAR_BUS_FF | MCH_PCIEXBAR_EN + ); =20 return EFI_SUCCESS; } diff --git a/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/Pcie.c b/Platfo= rm/Qemu/QemuOpenBoardPkg/PlatformInitPei/Pcie.c deleted file mode 100644 index 0a5f0ff398de..000000000000 --- a/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/Pcie.c +++ /dev/null @@ -1,106 +0,0 @@ -/** @file Pcie.c - PCI Express initialization for QEMU Q35 - - Copyright (c) 2022 Theo Jehl All rights reserved. - SPDX-License-Identifier: BSD-2-Clause-Patent -**/ - -#include "PlatformInit.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#in= clude - -/** - Initialize PCI Express support for QEMU Q35 system. - It also publishes PCI MMIO and IO ranges PCDs for OVMF PciHostBridgeLib. - - @retval EFI_SUCCESS Initialization was successful -**/ -EFI_STATUS -EFIA= PI -InitializePcie ( - VOID - ) -{ - UINTN PciBase; - UINTN PciSize; - UINTN PciIoBase; - UINTN PciIoSize; - - union { - UINT64 Uint64; - UINT32 Uint32[2]; - } PciExBarBase; - - PciExBarBase.Uint64 =3D FixedPcdGet64 (PcdPciExpressBaseAddress); - - // - // Build a reserved memory space for PCIE MMIO - // - BuildResourceDescriptorHob ( - EFI_RESOURCE_MEMORY_RESERVED, - EFI_RESOURCE_ATTRIBUTE_PRESENT | - EFI_RESOURCE_ATTRIBUTE_INITIALIZED | - EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE | - EFI_RESOURCE_ATTRIBUTE_TESTED, - PciExBarBase.Uint64, - SIZE_256MB - ); - - BuildMemoryAllocationHob ( - PciExBarBase.Uint64, - SIZE_256MB, - EfiReservedMemoryType - ); - - // - // Clear lower 32 bits of register - // - PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW), 0); - - // - // Program PCIE MMIO Base address in MCH PCIEXBAR register - // - PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH), PciExBarBase.Uint32[= 1]); - - // - // Enable 256Mb MMIO space - // - PciWrite32 ( - DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW), - PciExBarBase.Uint32[0] | MCH_PCIEXBAR_BUS_FF | MCH_PCIEXBAR_EN - ); - - // - // Disable PCI/PCIe MMIO above 4Gb - // - PcdSet64S (PcdPciMmio64Size, 0); - - // - // Set Pci MMIO space below 4GB - // - PciBase =3D (UINTN)(PcdGet64 (PcdPciExpressBaseAddress) + SIZE_256MB); - PciSize =3D PCI_MMIO_TOP_ADDRESS - PciBase; - - PcdSet64S (PcdPciMmio32Base, PciBase); - PcdSet64S (PcdPciMmio32Size, PciSize); - - // - // Set Pci IO port range - // - PciIoBase =3D Q35_PCI_IO_BASE; - PciIoSize =3D Q35_PCI_IO_SIZE; - - PcdSet64S (PcdPciIoBase, PciIoBase); - PcdSet64S (PcdPciIoSize, PciIoSize); - - return EFI_SUCCESS; -} diff --git a/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInit.c = b/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInit.c index 7849298b52d5..d23895afc967 100644 --- a/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInit.c +++ b/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInit.c @@ -2,6 +2,7 @@ Platform initialization PEIM for QEMU =20 Copyright (c) 2022 Theo Jehl All rights reserved. + Copyright (c) 2023 Pedro Falcato All rights reserved. SPDX-License-Identifier: BSD-2-Clause-Patent **/ =20 @@ -36,8 +37,6 @@ PlatformInit ( if (EFI_ERROR (Status)) { DEBUG ((DEBUG_ERROR, "Memory installation failed\n")); return Status; - } else { - DEBUG ((DEBUG_INFO, "Memory installation success\n")); } =20 // @@ -48,7 +47,7 @@ PlatformInit ( EfiPlatformInfo =3D AllocateZeroPool (sizeof (EFI_HOB_PLATFORM_INFO)); if (EfiPlatformInfo =3D=3D NULL) { DEBUG ((DEBUG_ERROR, "Failed to allocate pool for EFI_HOB_PLATFORM_INF= O\n")); - return EFI_UNSUPPORTED; + return EFI_OUT_OF_RESOURCES; } =20 // @@ -56,20 +55,14 @@ PlatformInit ( // DeviceId =3D PciCf8Read16 (PCI_CF8_LIB_ADDRESS (0, 0, 0, PCI_DEVICE_ID_O= FFSET)); DEBUG ((DEBUG_INFO, "Building gUefiOvmfPkgPlatformInfoGuid with Host bri= dge dev ID %x \n", DeviceId)); - (*EfiPlatformInfo).HostBridgeDevId =3D DeviceId; + EfiPlatformInfo->HostBridgeDevId =3D DeviceId; =20 BuildGuidDataHob (&gUefiOvmfPkgPlatformInfoGuid, EfiPlatformInfo, sizeof= (EFI_HOB_PLATFORM_INFO)); =20 PcdSet16S (PcdOvmfHostBridgePciDevId, DeviceId); =20 // - // Initialize PCI or PCIe based on current emulated system + // Finally, initialize PCI MMIO ranges and possibly the MMCONFIG // - if (DeviceId =3D=3D INTEL_Q35_MCH_DEVICE_ID) { - DEBUG ((DEBUG_INFO, "Q35: Initialize PCIe\n")); - return InitializePcie (); - } else { - DEBUG ((DEBUG_INFO, "PIIX4: Initialize PCI\n")); - return InitializePciPIIX4 (); - } + return InitializePci (DeviceId =3D=3D INTEL_Q35_MCH_DEVICE_ID ?=20 + QEMU_PLATFORM_Q35 : QEMU_PLATFORM_PIIX4); } diff --git a/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInit.h = b/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInit.h index 771d2f958c40..f4044df3dbf5 100644 --- a/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInit.h +++ b/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInit.h @@ -2,6 +2,7 @@ Headers for PlatformInitPei PEIM =20 Copyright (c) 2022 Theo Jehl All rights reserved. + Copyright (c) 2023 Pedro Falcato All rights reserved. SPDX-License-Identifier: BSD-2-Clause-Patent **/ =20 @@ -38,16 +39,23 @@ InstallMemory ( IN CONST EFI_PEI_SERVICES **PeiServices ); =20 -EFI_STATUS -EFIAPI -InitializePcie ( - VOID - ); +typedef enum Platforms { + QEMU_PLATFORM_PIIX4 =3D 0, + QEMU_PLATFORM_Q35, + QEMU_PLATFORM_MAX +} QEMU_PLATFORM; + +/** + Initialise PCI QEMU platforms. + + Updates PCI IO port and MMIO range PCDs. =20 + @retval EFI_SUCCESS Initialization was a success. +**/ EFI_STATUS EFIAPI -InitializePciPIIX4 ( - VOID +InitializePci ( + QEMU_PLATFORM Platform ); =20 EFI_STATUS @@ -56,4 +64,14 @@ MaxCpuInit ( VOID ); =20 +/** + Return the largest reserved/DRAM/etc address. + + @return Largest non-hole address. +**/ +UINT64 +GetTopNonHoleAddr ( + VOID + ); + #endif //QEMU_OPEN_BOARD_PKG_PLATFORM_INIT_H_ diff --git a/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInitPei= .inf b/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInitPei.inf index 43b1e13adfeb..5f42d46178f7 100644 --- a/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInitPei.inf +++ b/Platform/Qemu/QemuOpenBoardPkg/PlatformInitPei/PlatformInitPei.inf @@ -4,6 +4,7 @@ # Simple PEIM for QEMU PIIX4/Q35 Memory, SMP and PCI/PCI Express initiali= zation # # Copyright (c) 2022 Theo Jehl +# Copyright (c) 2023 Pedro Falcato All rights reserved. # SPDX-License-Identifier: BSD-2-Clause-Patent =20 [Defines] @@ -17,14 +18,15 @@ [Packages] OvmfPkg/OvmfPkg.dec MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec QemuOpenBoardPkg/QemuOpenBoardPkg.dec UefiCpuPkg/UefiCpuPkg.dec + MinPlatformPkg/MinPlatformPkg.dec =20 [Sources] PlatformInit.h PlatformInit.c Memory.c - Pcie.c Pci.c Cpu.c =20 @@ -43,13 +45,15 @@ gEfiMdePkgTokenSpaceGuid.PcdPciExpressBaseAddress gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber gUefiCpuPkgTokenSpaceGuid.PcdCpuBootLogicalProcessorNumber - gUefiOvmfPkgTokenSpaceGuid.PcdPciIoBase - gUefiOvmfPkgTokenSpaceGuid.PcdPciIoSize - gUefiOvmfPkgTokenSpaceGuid.PcdPciMmio32Base - gUefiOvmfPkgTokenSpaceGuid.PcdPciMmio32Size - gUefiOvmfPkgTokenSpaceGuid.PcdPciMmio64Base - gUefiOvmfPkgTokenSpaceGuid.PcdPciMmio64Size gEfiMdePkgTokenSpaceGuid.PcdPciExpressBaseAddress + gEfiMdePkgTokenSpaceGuid.PcdPciExpressBaseAddress + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedMemAbove4GBBase + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedMemAbove4GBLimit + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedMemBase + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedMemLimit + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedIobase + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedIoLimit + gMinPlatformPkgTokenSpaceGuid.PcdPciNoExtendedConfigSpace gUefiOvmfPkgTokenSpaceGuid.PcdQ35TsegMbytes =20 [FeaturePcd] diff --git a/Platform/Qemu/QemuOpenBoardPkg/QemuOpenBoardPkg.dsc b/Platform= /Qemu/QemuOpenBoardPkg/QemuOpenBoardPkg.dsc index f5c317c83e6a..b2b82b84be4c 100644 --- a/Platform/Qemu/QemuOpenBoardPkg/QemuOpenBoardPkg.dsc +++ b/Platform/Qemu/QemuOpenBoardPkg/QemuOpenBoardPkg.dsc @@ -4,6 +4,7 @@ # Description file for QemuOpenBoardPkg # # Copyright (c) 2022 Theo Je= hl +# Copyright (c) 2023 Pedro Falcato All rights reserved. # SPDX-License-Identifier: BSD-2-Clause-Patent ## =20 @@ -78,9 +79,7 @@ gQemuOpenBoardPkgTokenSpaceGuid.PcdDebugIoPort | = 0x402 gEfiMdePkgTokenSpaceGuid.PcdFSBClock | = 100000000 =20 - # PCIe base address for Q35 machines - # QEMU usable memory below 4G cannot exceed 2.8Gb - gEfiMdePkgTokenSpaceGuid.PcdPciExpressBaseAddress | = 0xB0000000 + gEfiMdePkgTokenSpaceGuid.PcdPciExpressBaseAddress = | 0xE0000000 =20 gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable | = TRUE gEfiMdeModulePkgTokenSpaceGuid.PcdResetOnMemoryTypeInformationChange | = FALSE @@ -119,6 +118,18 @@ gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber | = 0 gUefiCpuPkgTokenSpaceGuid.PcdCpuBootLogicalProcessorNumber | = 0 =20 + # All of these MinPlatform PCI PCDs are filled in PlatformInitPei #=20 + They must be dynamic since we don't know what platform (ICH9 or PIIX4)=20 + we're booting # or how the memory map looks like. + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedMemAbove4GBBase = | 0xffffffffffffffff + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedMemAbove4GBLimit = | 0 + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedMemBase = | 0x90000000 + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedMemLimit = | 0 + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedIobase = | 0x2000 + gMinPlatformPkgTokenSpaceGuid.PcdPciReservedIoLimit = | 0xffff + + gMinPlatformPkgTokenSpaceGuid.PcdPciNoExtendedConfigSpace = | TRUE + !if $(SMM_REQUIRED) =3D=3D TRUE gUefiOvmfPkgTokenSpaceGuid.PcdQ35TsegMbytes | = 8 gUefiOvmfPkgTokenSpaceGuid.PcdQ35SmramAtDefaultSmbase | = FALSE -- 2.39.0