From nobody Mon Sep 16 19:47:46 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1699879901; cv=none; d=zohomail.com; s=zohoarc; b=d6Nx9Uab/oaC/F3hZkYNoodFq/XJTSOM3zHlyZK4Z/TUqCI9Hf8+esU6tyR0/DcAqOImPDN/uhPurwmAADt9Nyk9Y7xmht8MkfAZT8zpYBgKT2sseKmTNUyJCjktytTKUicptkCUiV6cKzCrkwvfzGJlWtXr/3LB0PAVP+AS2pU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699879901; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Onk9QxmyyaXrupbRmx0M+aIO5AGYsMNFSXnY/RPkB2M=; b=I5aeAyNgKRye1DXSjGiMbezJyI7F4y1IAZVHkpvhtciqBQrDL3cNKH8t/JEqR6dApXZIy5NjI255mwz9TFoqwmryjrRCMueqLYCAepWGXHNqj1AOGD52qVPsAnFU398jVks51MTwdv4fZUAC7FhLh5ur81KZwiZwDAtprQ/YFtI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1699879901599936.552718058007; Mon, 13 Nov 2023 04:51:41 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r2WOx-0007gI-1I; Mon, 13 Nov 2023 07:50:51 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r2WOw-0007fS-3J for qemu-devel@nongnu.org; Mon, 13 Nov 2023 07:50:50 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r2WOu-0005Du-9A for qemu-devel@nongnu.org; Mon, 13 Nov 2023 07:50:49 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-94-0ZtkwhRDPvi-sEqN8aOC7A-1; Mon, 13 Nov 2023 07:50:41 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3DFB13C1AC94; Mon, 13 Nov 2023 12:50:41 +0000 (UTC) Received: from sirius.home.kraxel.org (unknown [10.39.194.59]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EA8D85031; Mon, 13 Nov 2023 12:50:40 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 8AB061804A63; Mon, 13 Nov 2023 13:50:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699879846; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Onk9QxmyyaXrupbRmx0M+aIO5AGYsMNFSXnY/RPkB2M=; b=i7iiJPWqX6RX6p0418Rqjx02hcjYe8uilqnxjev7Ft0tqyuZ51lyJr2KjI9SMD15mN3tpy +i3R3Fn8s+HMGMJZjjD5zHfmD9hF/AYxROn6vsmXpGCh/2gCh//gi6pkMunQweOFPoXRp5 mRom39/YwIw7SuYJKNqOmJTOr7LAp4M= X-MC-Unique: 0ZtkwhRDPvi-sEqN8aOC7A-1 From: Gerd Hoffmann To: seabios@seabios.org Cc: qemu-devel@nongnu.org, Gerd Hoffmann , Kevin O'Connor , Claudio Fontana Subject: [PATCH v6] limit address space used for pci devices. Date: Mon, 13 Nov 2023 13:50:39 +0100 Message-ID: <20231113125039.57200-1-kraxel@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=kraxel@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1699879903545100003 Content-Type: text/plain; charset="utf-8" For better compatibility with old linux kernels, see source code comment. Also rename some variables to make the code more readable, following suggestions by Kevin. Related (same problem in ovmf): https://github.com/tianocore/edk2/commit/c1e853769046 Cc: Kevin O'Connor Reported-by: Claudio Fontana Signed-off-by: Gerd Hoffmann --- src/fw/pciinit.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/fw/pciinit.c b/src/fw/pciinit.c index c7084f5e397e..6b13cd5b7d1c 100644 --- a/src/fw/pciinit.c +++ b/src/fw/pciinit.c @@ -46,12 +46,16 @@ static const char *region_type_name[] =3D { [ PCI_REGION_TYPE_PREFMEM ] =3D "prefmem", }; =20 +// Memory ranges exported to legacy ACPI type table generation u64 pcimem_start =3D BUILD_PCIMEM_START; u64 pcimem_end =3D BUILD_PCIMEM_END; u64 pcimem64_start =3D BUILD_PCIMEM64_START; u64 pcimem64_end =3D BUILD_PCIMEM64_END; -u64 pci_io_low_end =3D 0xa000; -u32 pci_use_64bit =3D 0; + +// Resource allocation limits +static u64 pci_io_low_end =3D 0xa000; +static u64 pci_mem64_top =3D 0; +static u32 pci_pad_mem64 =3D 0; =20 struct pci_region_entry { struct pci_device *dev; @@ -966,8 +970,9 @@ static int pci_bios_check_devices(struct pci_bus *busse= s) int resource_optional =3D 0; if (hotplug_support =3D=3D HOTPLUG_PCIE) resource_optional =3D pcie_cap && (type =3D=3D PCI_REGION_= TYPE_IO); - if (hotplug_support && pci_use_64bit && is64 && (type =3D=3D P= CI_REGION_TYPE_PREFMEM)) - align =3D (u64)1 << (CPUPhysBits - 11); + if (hotplug_support && pci_pad_mem64 && is64 + && (type =3D=3D PCI_REGION_TYPE_PREFMEM)) + align =3D pci_mem64_top >> 11; if (align > sum && hotplug_support && !resource_optional) sum =3D align; /* reserve min size for hot-plug */ if (size > sum) { @@ -1111,7 +1116,7 @@ static void pci_bios_map_devices(struct pci_bus *buss= es) panic("PCI: out of I/O address space\n"); =20 dprintf(1, "PCI: 32: %016llx - %016llx\n", pcimem_start, pcimem_end); - if (pci_use_64bit || pci_bios_init_root_regions_mem(busses)) { + if (pci_pad_mem64 || pci_bios_init_root_regions_mem(busses)) { struct pci_region r64_mem, r64_pref; r64_mem.list.first =3D NULL; r64_pref.list.first =3D NULL; @@ -1131,14 +1136,13 @@ static void pci_bios_map_devices(struct pci_bus *bu= sses) r64_mem.base =3D le64_to_cpu(romfile_loadint("etc/reserved-memory-= end", 0)); if (r64_mem.base < 0x100000000LL + RamSizeOver4G) r64_mem.base =3D 0x100000000LL + RamSizeOver4G; - if (CPUPhysBits) { - u64 top =3D 1LL << CPUPhysBits; + if (pci_mem64_top) { u64 size =3D (ALIGN(sum_mem, (1LL<<30)) + ALIGN(sum_pref, (1LL<<30))); - if (pci_use_64bit) - size =3D ALIGN(size, (1LL<<(CPUPhysBits-3))); - if (r64_mem.base < top - size) { - r64_mem.base =3D top - size; + if (pci_pad_mem64) + size =3D ALIGN(size, pci_mem64_top >> 3); + if (r64_mem.base < pci_mem64_top - size) { + r64_mem.base =3D pci_mem64_top - size; } if (e820_is_used(r64_mem.base, size)) r64_mem.base -=3D size; @@ -1181,8 +1185,18 @@ pci_setup(void) =20 dprintf(3, "pci setup\n"); =20 + if (CPUPhysBits) { + pci_mem64_top =3D 1LL << CPUPhysBits; + if (CPUPhysBits > 46) { + // Old linux kernels have trouble dealing with more than 46 + // phys-bits, so avoid that for now. Seems to be a bug in the + // virtio-pci driver. Reported: centos-7, ubuntu-18.04 + pci_mem64_top =3D 1LL << 46; + } + } + if (CPUPhysBits >=3D 36 && CPULongMode && RamSizeOver4G) - pci_use_64bit =3D 1; + pci_pad_mem64 =3D 1; =20 dprintf(1, "=3D=3D=3D PCI bus & bridge init =3D=3D=3D\n"); if (pci_probe_host() !=3D 0) { --=20 2.41.0