From patchwork Thu Sep 19 22:35:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Winiarski?= X-Patchwork-Id: 13808027 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 43B7E1B2EF3; Thu, 19 Sep 2024 22:38:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=198.175.65.9 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726785515; cv=fail; b=qkQq8rjelVd8X3aEUK75N4CJ5VQcVO3uXwDuzPjvRc1y7ZuXRkW5NFUcxUKPe9PaYwgzM7RGxdU1lRuluJmuVSmMM5GXCOvyAp9Kdud41nzQiiwdSJOg/4as+H0vvCaCZ0kli2RsYSaAeMAB7OXTany5h+KYXcgrB84Imb01R2o= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726785515; c=relaxed/simple; bh=SkkbXRnm25tgMF0IgRVj3Ttc412D7UDre1mtD2GNklU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=oKQ41o788AngJ5zViM0O3fdNpwyMavwCNuhUQAYEUSO7lDDgJayI4OsQOfHexNF2LHA6r+OW8NxqssS4ISWjGUkxefNCCrEs4zyjk7Ma2XoYyncrZT9YXYb/AMW4GLRP3Xuyt7fZ+61by8nerUa6fZS8eLnwwfhl9ynIDCATFzE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=heMFLgIN; arc=fail smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="heMFLgIN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1726785513; x=1758321513; h=from:to:cc:subject:date:message-id:in-reply-to: references:content-transfer-encoding:mime-version; bh=SkkbXRnm25tgMF0IgRVj3Ttc412D7UDre1mtD2GNklU=; b=heMFLgINP3NtORu8SbJ39oDhXZm9Awc9Tm7VM9vpaKEmxQzz3L2qyBoM d9QumfS+2XIT0c0divgOJPYsMesmTkHDA8AJq9EAfXuy9fv4ZGp9k6ZcY BXEmUhFEgKMIVUUhsdScrxGo8oFbntElpwxZZxunFSZk+BLX1362n05Gp +0dIK1Fvta40X35GKHscO+5DYia683F5KOBJgvv1wxuydxd6k9te9eBen V5eE1v1Nq8OhEjTa8Ek1H5gIU3UIOH7slNDjzLw0E5fdoc/zan3U7W1LD RNblDZx66SSqD8tehNXkLRhPDq3ulDTMduoQr2dMlwqqVxtXJNO5feRHN A==; X-CSE-ConnectionGUID: 5qNFEaMuR1eELCRWB3flQg== X-CSE-MsgGUID: /cn+f855TsmlWg1elJ2g7Q== X-IronPort-AV: E=McAfee;i="6700,10204,11200"; a="48309515" X-IronPort-AV: E=Sophos;i="6.10,243,1719903600"; d="scan'208";a="48309515" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Sep 2024 15:38:32 -0700 X-CSE-ConnectionGUID: ZnsHlbloRSaMJra4DysVTQ== X-CSE-MsgGUID: UKmxLiyNT4eHnlS21sS9iQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,243,1719903600"; d="scan'208";a="70334039" Received: from fmsmsx601.amr.corp.intel.com ([10.18.126.81]) by fmviesa010.fm.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 19 Sep 2024 15:38:32 -0700 Received: from fmsmsx611.amr.corp.intel.com (10.18.126.91) by fmsmsx601.amr.corp.intel.com (10.18.126.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Thu, 19 Sep 2024 15:38:31 -0700 Received: from fmsmsx612.amr.corp.intel.com (10.18.126.92) by fmsmsx611.amr.corp.intel.com (10.18.126.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Thu, 19 Sep 2024 15:38:31 -0700 Received: from FMSEDG603.ED.cps.intel.com (10.1.192.133) by fmsmsx612.amr.corp.intel.com (10.18.126.92) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39 via Frontend Transport; Thu, 19 Sep 2024 15:38:31 -0700 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (104.47.70.41) by edgegateway.intel.com (192.55.55.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Thu, 19 Sep 2024 15:38:31 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=N9z7AdkOajGTE1de+aWqASh7i9Zk4oHzqVzTq/V0S7iRvsNpUR6tR7Ub98yK1iEv1FL4TZrY7jerq89hPl971KQ1syWigzZJfMMUvbS0XgoQlATvbtjCT9j42uB2mCaISyKzGQZ0AQg+8eHsxOFmDQIwber9sypTklwIKxnMTb472TL3dk0df71lO4DB/AWINnCxHvALCc4f6rV4YnFxkDvbFCvkQdzLDpWSrNQZaMwGYIUplDqKydkCRXj2PRWBDqm1qOAv1FCKhM9hHbfuK9aCKeAhSLysFBqEPr/fOVr/kL4RFOkUN59uO1MxtbrLtnZ3l60dJzSAhm0UuxhuRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=O2e8pnCVWF9f5NxGXVDREsXbrrXkrB+voyNQz3045ag=; b=WUIJvilPVKYbG+DeqbGtIOsTxLiwqLBxiMLHfgfIxRH2GqNUmR73UFc9nO0za66opAELBSHXWFKYZcG/XAKd6ZwO9/wYX2HEVWHjM2aIw8/g8ec8GgJehdoEwsTfZThIdGx3Gmvs46mU1b9WPPSO0dWY5Qb0de8FaBK2U7lXYDVvbNIecMSv4iMpeKfZp8De/PyEebt8hijyWuG41S+FCxU2Lx/SJ5Nmd2d29yugxI1QTZf8XKjFoLAedkRurRtsWzIHRqB8QaBH9KmYkGiJanJzO0Z5KVItDuCFei+/hdL7AoXWrz+gmHtdnpZRO21zftPD4/7JH+35YbEDsRtpHg== 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 Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; Received: from DM4PR11MB5373.namprd11.prod.outlook.com (2603:10b6:5:394::7) by MW4PR11MB5824.namprd11.prod.outlook.com (2603:10b6:303:187::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.26; Thu, 19 Sep 2024 22:38:27 +0000 Received: from DM4PR11MB5373.namprd11.prod.outlook.com ([fe80::927a:9c08:26f7:5b39]) by DM4PR11MB5373.namprd11.prod.outlook.com ([fe80::927a:9c08:26f7:5b39%5]) with mapi id 15.20.7982.018; Thu, 19 Sep 2024 22:38:27 +0000 From: =?utf-8?q?Micha=C5=82_Winiarski?= To: , , , , "Bjorn Helgaas" , =?utf-8?q?Christian_K=C3=B6nig?= , =?utf-8?q?Krzy?= =?utf-8?q?sztof_Wilczy=C5=84ski?= CC: Rodrigo Vivi , Michal Wajdeczko , Lucas De Marchi , =?utf-8?q?Thomas_Hellstr=C3=B6m?= , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Matt Roper , =?utf-8?q?Micha=C5=82_Winiarski?= Subject: [PATCH v2 1/3] PCI: Add support for VF Resizable Bar extended cap Date: Fri, 20 Sep 2024 00:35:55 +0200 Message-ID: <20240919223557.1897608-2-michal.winiarski@intel.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240919223557.1897608-1-michal.winiarski@intel.com> References: <20240919223557.1897608-1-michal.winiarski@intel.com> X-ClientProxiedBy: MI2P293CA0014.ITAP293.PROD.OUTLOOK.COM (2603:10a6:290:45::13) To DM4PR11MB5373.namprd11.prod.outlook.com (2603:10b6:5:394::7) Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM4PR11MB5373:EE_|MW4PR11MB5824:EE_ X-MS-Office365-Filtering-Correlation-Id: f1b8401a-a19d-45d2-61af-08dcd8fbc72e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|7416014|1800799024; X-Microsoft-Antispam-Message-Info: =?utf-8?q?8UtKPVCZqpHgW42gLogpKe80LHie1oe?= =?utf-8?q?9PZTyAzjQfN0erkQ5wOK7WhfzF+WCk19CJFde6bzB9dHI//rmhDGE0DbGLE54Z2q/?= =?utf-8?q?7lgsb5s4tZlgdnyyQGysemSO0gez/SEkBZK+EbIVAQ77GJvRtt3KjewveJryGDjFc?= =?utf-8?q?/d2jbZc+X+FrTrQsDeNlPpJO+ZnSbUP4faXD7h83X/Dg9Kz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kSl1uhTOisoq4j9?= =?utf-8?q?BLy1tyEwmIsXphv0vccPBlPt+2Cgw1YdzWDNvp+hDe1T/tSoEKz1DnW/kUxw3ztOj?= =?utf-8?q?JqjeRHO4IUHKSJ/RHlaQPy/pZe7WHMkOtU1Co5oW9LwqdbgH5cMnh6ROUeKHfYAZf?= =?utf-8?q?uZjbFZSTeooAdNuj+y5MP4OXTRqefSvz2+shVlbeUWXZzklSOtL9mXQnMwZbWZnvd?= =?utf-8?q?pDt+5VEF8vSQOIOtqZQidg5xro6LcwQ154g=3D=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM4PR11MB5373.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(376014)(7416014)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?XOfSWvttxg8chJmpwHb59EfEiibq?= =?utf-8?q?mYzgJPx4R1DAo92eLFrecsGiN3TElQ2oKMeF7qUhv9n1J3vlE5XMnpMJLDuTT306o?= =?utf-8?q?zF9iyMmr5sy6j4HP2ObRhPzs6MbSHAtjFy6b3QWWzBWMHoJrx2CrVNcJxxkUxcE0I?= =?utf-8?q?hWr2GliLpb3gkdF0ujCpfCXeKGFR9YFRx0QuyO4eQeEKOkFTp0+kUCbVz/ywLB0ob?= =?utf-8?q?Hm+cbAjPlQ2lhg6zTBJmXEJFaIekRCYsXIX1PlLKbl/zJ/NRy0f8UT1+SOQ5Ckh4s?= =?utf-8?q?7NhABhLeMLL8Hgbr5Whjlrvv2X6g+Ul7unxqvMYq8o1dlFwrJlXCu7qtIiREWhHoj?= =?utf-8?q?W+1+TPECDm4LI/MEPuL/n+vpIxnoawEMTwXERZ8dY52na43sNP+FVl5Eku1BCiRf4?= =?utf-8?q?BRWmmdwwd27plKBAVwpIa7vyLGME9q6L/aRH4Pf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4FQ+vqUHH?= =?utf-8?q?yv84elIuxi5ArmeZS4sXPPZuHhhbzouOXdutcZlejxwtKvb32DOdO7bdFcCAfdsl7?= =?utf-8?q?412XrPCmVOcQId4rSRvLbrp/TQLvGihC2Xwf6lJ1xny8lQuSq7vTgyQERpAfTZoKq?= =?utf-8?q?eH83gRtEuldUA8Q7psXFRInGpI9QLZsqDgcN7x3Kb1LhGqCdUZLLGek9/XCkNtXFt?= =?utf-8?q?fMQ9D3igEYsl4fKy9a9/XZb/zCcbDxFR+Q=3D=3D?= X-MS-Exchange-CrossTenant-Network-Message-Id: f1b8401a-a19d-45d2-61af-08dcd8fbc72e X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5373.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Sep 2024 22:38:27.1217 (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: 8nN6RGIhfIcjCHQUXm1MGlxK9aKqfeAMdCMsnWrP/ObMNU0HjKuzlZfCSkprk1EOLS1Z6ha/uoxgNmtTLrXCbkXg2FaqS19ORtpTY8iBPjg= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR11MB5824 X-OriginatorOrg: intel.com Similar to regular resizable BAR, VF BAR can also be resized. The structures are very similar, which means we can reuse most of the implementation. See PCIe r4.0, sec 9.3.7.4. Signed-off-by: MichaƂ Winiarski --- drivers/pci/iov.c | 28 ++++++++++++++++++++++ drivers/pci/pci.c | 40 ++++++++++++++++++++++++++++++- drivers/pci/pci.h | 14 ++++++++++- drivers/pci/setup-res.c | 44 ++++++++++++++++++++++++++++++----- include/uapi/linux/pci_regs.h | 1 + 5 files changed, 119 insertions(+), 8 deletions(-) diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c index aaa33e8dc4c97..e8ccd2ae0f024 100644 --- a/drivers/pci/iov.c +++ b/drivers/pci/iov.c @@ -153,6 +153,34 @@ resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno) return dev->sriov->barsz[resno - PCI_IOV_RESOURCES]; } +bool pci_resource_is_iov(struct pci_dev *dev, int resno) +{ + if (resno >= PCI_IOV_RESOURCES && resno <= PCI_IOV_RESOURCE_END) + return true; + + return false; +} + +void pci_iov_resource_set_size(struct pci_dev *dev, int resno, resource_size_t size) +{ + if (!pci_resource_is_iov(dev, resno)) { + dev_WARN(&dev->dev, "%s is not an IOV resource\n", + pci_resource_name(dev, resno)); + return; + } + + dev->sriov->barsz[resno - PCI_IOV_RESOURCES] = size; +} + +bool pci_iov_memory_decoding_enabled(struct pci_dev *dev) +{ + u16 cmd; + + pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_CTRL, &cmd); + + return cmd & PCI_SRIOV_CTRL_MSE; +} + static void pci_read_vf_config_common(struct pci_dev *virtfn) { struct pci_dev *physfn = virtfn->physfn; diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index ffaaca0978cbc..d4522e365e7ba 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -1901,6 +1901,35 @@ static void pci_restore_rebar_state(struct pci_dev *pdev) } } +static void pci_restore_vf_rebar_state(struct pci_dev *pdev) +{ + unsigned int pos, nbars, i; + u32 ctrl; + + if (!pdev->is_physfn) + return; + + pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_VF_REBAR); + if (!pos) + return; + + pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl); + nbars = FIELD_GET(PCI_REBAR_CTRL_NBAR_MASK, ctrl); + + for (i = 0; i < nbars; i++, pos += 8) { + struct resource *res; + int bar_idx, size; + + pci_read_config_dword(pdev, pos + PCI_REBAR_CTRL, &ctrl); + bar_idx = ctrl & PCI_REBAR_CTRL_BAR_IDX; + res = pdev->resource + bar_idx; + size = pci_rebar_bytes_to_size(pdev->sriov->barsz[bar_idx]); + ctrl &= ~PCI_REBAR_CTRL_BAR_SIZE; + ctrl |= FIELD_PREP(PCI_REBAR_CTRL_BAR_SIZE, size); + pci_write_config_dword(pdev, pos + PCI_REBAR_CTRL, ctrl); + } +} + /** * pci_restore_state - Restore the saved state of a PCI device * @dev: PCI device that we're dealing with @@ -1916,6 +1945,7 @@ void pci_restore_state(struct pci_dev *dev) pci_restore_ats_state(dev); pci_restore_vc_state(dev); pci_restore_rebar_state(dev); + pci_restore_vf_rebar_state(dev); pci_restore_dpc_state(dev); pci_restore_ptm_state(dev); @@ -3703,10 +3733,18 @@ void pci_acs_init(struct pci_dev *dev) */ static int pci_rebar_find_pos(struct pci_dev *pdev, int bar) { + int cap = PCI_EXT_CAP_ID_REBAR; unsigned int pos, nbars, i; u32 ctrl; - pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_REBAR); +#ifdef CONFIG_PCI_IOV + if (pci_resource_is_iov(pdev, bar)) { + cap = PCI_EXT_CAP_ID_VF_REBAR; + bar -= PCI_IOV_RESOURCES; + } +#endif + + pos = pci_find_ext_capability(pdev, cap); if (!pos) return -ENOTSUPP; diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 79c8398f39384..e763b3fd4c7a2 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -556,6 +556,9 @@ void pci_restore_iov_state(struct pci_dev *dev); int pci_iov_bus_range(struct pci_bus *bus); extern const struct attribute_group sriov_pf_dev_attr_group; extern const struct attribute_group sriov_vf_dev_attr_group; +bool pci_resource_is_iov(struct pci_dev *dev, int resno); +bool pci_iov_memory_decoding_enabled(struct pci_dev *dev); +void pci_iov_resource_set_size(struct pci_dev *dev, int resno, resource_size_t size); #else static inline int pci_iov_init(struct pci_dev *dev) { @@ -568,7 +571,16 @@ static inline int pci_iov_bus_range(struct pci_bus *bus) { return 0; } - +static inline bool pci_iov_memory_decoding_enabled(struct pci_dev *dev) +{ + return false; +} +static inline bool pci_resource_is_iov(struct pci_dev *dev, int resno) +{ + return false; +} +static inline void pci_iov_resource_set_size(struct pci_dev *dev, int resno, + resource_size_t size) { } #endif /* CONFIG_PCI_IOV */ #ifdef CONFIG_PCIE_PTM diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c index c6d933ddfd464..87a952a114f38 100644 --- a/drivers/pci/setup-res.c +++ b/drivers/pci/setup-res.c @@ -427,13 +427,44 @@ void pci_release_resource(struct pci_dev *dev, int resno) } EXPORT_SYMBOL(pci_release_resource); +static bool pci_memory_decoding_enabled(struct pci_dev *dev) +{ + u16 cmd; + + pci_read_config_word(dev, PCI_COMMAND, &cmd); + + return cmd & PCI_COMMAND_MEMORY; +} + +static int pci_resize_check_memory_decoding(struct pci_dev *dev, int resno) +{ + if (!pci_resource_is_iov(dev, resno) && pci_memory_decoding_enabled(dev)) + return -EBUSY; + else if (pci_resource_is_iov(dev, resno) && pci_iov_memory_decoding_enabled(dev)) + return -EBUSY; + + return 0; +} + +static void pci_resize_resource_set_size(struct pci_dev *dev, int resno, int size) +{ + resource_size_t res_size = pci_rebar_size_to_bytes(size); + struct resource *res = dev->resource + resno; + + if (!pci_resource_is_iov(dev, resno)) { + res->end = res->start + res_size - 1; + } else { + res->end = res->start + res_size * pci_sriov_get_totalvfs(dev) - 1; + pci_iov_resource_set_size(dev, resno, res_size); + } +} + int pci_resize_resource(struct pci_dev *dev, int resno, int size) { struct resource *res = dev->resource + resno; struct pci_host_bridge *host; int old, ret; u32 sizes; - u16 cmd; /* Check if we must preserve the firmware's resource assignment */ host = pci_find_host_bridge(dev->bus); @@ -444,9 +475,9 @@ int pci_resize_resource(struct pci_dev *dev, int resno, int size) if (!(res->flags & IORESOURCE_UNSET)) return -EBUSY; - pci_read_config_word(dev, PCI_COMMAND, &cmd); - if (cmd & PCI_COMMAND_MEMORY) - return -EBUSY; + ret = pci_resize_check_memory_decoding(dev, resno); + if (ret) + return ret; sizes = pci_rebar_get_possible_sizes(dev, resno); if (!sizes) @@ -463,7 +494,7 @@ int pci_resize_resource(struct pci_dev *dev, int resno, int size) if (ret) return ret; - res->end = res->start + pci_rebar_size_to_bytes(size) - 1; + pci_resize_resource_set_size(dev, resno, size); /* Check if the new config works by trying to assign everything. */ if (dev->bus->self) { @@ -475,7 +506,8 @@ int pci_resize_resource(struct pci_dev *dev, int resno, int size) error_resize: pci_rebar_set_size(dev, resno, old); - res->end = res->start + pci_rebar_size_to_bytes(old) - 1; + pci_resize_resource_set_size(dev, resno, old); + return ret; } EXPORT_SYMBOL(pci_resize_resource); diff --git a/include/uapi/linux/pci_regs.h b/include/uapi/linux/pci_regs.h index 94c00996e633e..cb010008c6bb3 100644 --- a/include/uapi/linux/pci_regs.h +++ b/include/uapi/linux/pci_regs.h @@ -738,6 +738,7 @@ #define PCI_EXT_CAP_ID_L1SS 0x1E /* L1 PM Substates */ #define PCI_EXT_CAP_ID_PTM 0x1F /* Precision Time Measurement */ #define PCI_EXT_CAP_ID_DVSEC 0x23 /* Designated Vendor-Specific */ +#define PCI_EXT_CAP_ID_VF_REBAR 0x24 /* VF Resizable BAR */ #define PCI_EXT_CAP_ID_DLF 0x25 /* Data Link Feature */ #define PCI_EXT_CAP_ID_PL_16GT 0x26 /* Physical Layer 16.0 GT/s */ #define PCI_EXT_CAP_ID_PL_32GT 0x2A /* Physical Layer 32.0 GT/s */