From patchwork Tue May 7 10:25:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ilpo_J=C3=A4rvinen?= X-Patchwork-Id: 13656583 X-Patchwork-Delegate: bhelgaas@google.com Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (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 E98FF14EC4D; Tue, 7 May 2024 10:25:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077553; cv=none; b=EXzGhCuN6ru0ZQir+E5I+7ne/QOKoMvmaBeYQigyTYO58Dgu6jVNB4CB3PbjArUBVR+nxqoCRpVMXEfQqEuvZV8vcsNb5ngInnKfuF73gRsXakpz1QfS6TSAjvZUQiPowKkxRAYNriGB5//+KXGvpdakrZC3v7h4UmnVW9fuc2w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077553; c=relaxed/simple; bh=KAj0RxikzJoUJzlDAVpvqjT/K/KSlR+Z+qURBJkcFOM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=WhfoM0g/9hv8FYeR8weTpuF5IRq1qVT5fm2VIqFWBIS6poG0tZqaAYoyWNejX52OVLjsfqqg4onEP65C4qXYTGutBv4wbRd1b2VipwY/sTzgHGiQbWmgN5qqzNdUS9p9KWVxPTGYrO9wut/f+VfRbuLXDKNwSPt3JKo9rrp8yQI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=MsSCrJSv; arc=none smtp.client-ip=192.198.163.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="MsSCrJSv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1715077552; x=1746613552; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KAj0RxikzJoUJzlDAVpvqjT/K/KSlR+Z+qURBJkcFOM=; b=MsSCrJSvjaMFsM2dwsI4U3o2oNsToUj3LL18hytM+8I2egfAzzl/WXJm rcWbVOEWttfkCEv6QqPknzJ0V3fo179O1IZK9mH7JUvvqtHW1ggwpdVus yrNaEQU4pFsJCoqGSTt4kbYPu1T8u2tFb/3BNput6fIOCXuUwIC5WD0yh T0ZD+xGHevQQOmUHuCcpkoNNT/4I4TKu60g+HpadWlE9ZBibK5pS00Cch y6EY7QGf2XPM0CzW3QHVtA4I5fXiydJ59+0NdvRgMPPcP8hiJknSUVbkk MNLblKvTCD5zCxujZRBLn5O3Jv6PK0HC9NUHIHKzrc5suk1wc/kuD4mc/ A==; X-CSE-ConnectionGUID: Sn+lI999Rc6ujkyvr2nuKw== X-CSE-MsgGUID: d/evep70QcyB2WtwSX/HiA== X-IronPort-AV: E=McAfee;i="6600,9927,11065"; a="36243489" X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="36243489" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:25:51 -0700 X-CSE-ConnectionGUID: gbH9mbRRR8W+w5dovI6hCA== X-CSE-MsgGUID: NI5U66wNQeWTJHMc3mA3Cw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="33307465" Received: from ijarvine-desk1.ger.corp.intel.com (HELO localhost) ([10.245.247.74]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:25:47 -0700 From: =?utf-8?q?Ilpo_J=C3=A4rvinen?= To: linux-pci@vger.kernel.org, Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Igor Mammedov , Mika Westerberg , Andy Shevchenko , "Rafael J . Wysocki" , Jonathan Cameron , Yinghai Lu , Jesse Barnes , linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Lidong Wang Subject: [PATCH v3 1/8] PCI: Fix resource double counting on remove & rescan Date: Tue, 7 May 2024 13:25:16 +0300 Message-Id: <20240507102523.57320-2-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> References: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 pbus_size_mem() keeps the size of the optional resources in children_add_size. When calculating the PCI bridge window size, calculate_memsize() lower bounds size by old_size before adding children_add_size and performing the window size alignment. This results in double counting for the resources in children_add_size because old_size may be based on the previous size of the bridge window after it has already included children_add_size (that is, size1 in pbus_size_mem() from an earlier invocation of that function). As a result, on repeated remove of the bus & rescan cycles the resource size keeps increasing when children_add_size is non-zero as can be seen from this extract: iomem0: 23fffd00000-23fffdfffff : PCI Bus 0000:03 iomem1: 20000000000-200001fffff : PCI Bus 0000:03 iomem2: 20000000000-200002fffff : PCI Bus 0000:03 iomem3: 20000000000-200003fffff : PCI Bus 0000:03 iomem4: 20000000000-200004fffff : PCI Bus 0000:03 Solve the double counting by moving old_size check later in calculate_memsize() so that children_add_size is already accounted for. After the patch, the bridge window retains its size as expected: iomem0: 23fffd00000-23fffdfffff : PCI Bus 0000:03 iomem1: 20000000000-200000fffff : PCI Bus 0000:03 iomem2: 20000000000-200000fffff : PCI Bus 0000:03 Fixes: a4ac9fea016f ("PCI : Calculate right add_size") Tested-by: Lidong Wang Signed-off-by: Ilpo Järvinen Reviewed-by: Mika Westerberg --- drivers/pci/setup-bus.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c index 909e6a7c3cc3..141d6b31959b 100644 --- a/drivers/pci/setup-bus.c +++ b/drivers/pci/setup-bus.c @@ -829,11 +829,9 @@ static resource_size_t calculate_memsize(resource_size_t size, size = min_size; if (old_size == 1) old_size = 0; - if (size < old_size) - size = old_size; - size = ALIGN(max(size, add_size) + children_add_size, align); - return size; + size = max(size, add_size) + children_add_size; + return ALIGN(max(size, old_size), align); } resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus, From patchwork Tue May 7 10:25:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ilpo_J=C3=A4rvinen?= X-Patchwork-Id: 13656584 X-Patchwork-Delegate: bhelgaas@google.com Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (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 72ABF14EC4D; Tue, 7 May 2024 10:26:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077564; cv=none; b=l4sTgQCdJvtgXluO0gG6nT63V7pQtU9F0qAv5u+2HLS0NST0IqlUSBPCv9+193ZJWSJBt2FlTxtqGyXeWka7VjrCDKm5UJCSsHMMYK7VICoNwgTMJ/APkHvTGDAdiLtOugInKgk5wmaiAmbkhXGtTTFv+IumrNFSAccqYGsw0lQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077564; c=relaxed/simple; bh=W/1p5rLCE7EfDYekQAv2sivheXm6yRukXo8sDbl94r4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=X1LsJxdLETV/gQuLvI2aMmTG8eP8KDNtiam3DP+XMBj2t9oZqCtGJUCCeCNMaLwdI4QBm9ZT88PL1Mts+9e/5L24No/DRkS5ENuMz5cWpxJzBi1gcZiqzI2maJ6ha4PQc24N7AJ8bKGXiZp4WmXjqNHDeobKxITo3qxaw6oMIcY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=CCqgXBSW; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="CCqgXBSW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1715077561; x=1746613561; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=W/1p5rLCE7EfDYekQAv2sivheXm6yRukXo8sDbl94r4=; b=CCqgXBSWNnqpLmAmfEgx0ntx027cmcU/xgolEB7UyKfhBC/8YmATkrPn 3G1mQ7zTLYflEd9Y3FAJhCLcR/KKBfWIxmITSi+6t+6WhUllttBXmwl1J kZ/l2hXlPlTRbWxgk9lk+E6llYN32tvOUtvGCuXgtzjRCq26Sb8hOQhbe ifkt6j67IR9cu0MhP7bYpmcdBj3yA+gHbwf9ckUqNafDy1Yk6Odgg1EOM kGg3cHt91e7xtn0U7FPOpczLpb28rQrCN0QAJm36EF00k/wv3aeD/NjqA 57Kj9Px1yN4oZnl5D6Fxr1lMoVinOGD3PXxRkHHZkw2eyB4rGueMJhg1g g==; X-CSE-ConnectionGUID: FCVUeelfQZucYO9jy2tb/A== X-CSE-MsgGUID: L24ZPt+xRvS+tf1VYG83jA== X-IronPort-AV: E=McAfee;i="6600,9927,11065"; a="10746752" X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="10746752" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:00 -0700 X-CSE-ConnectionGUID: izw6jrMIT2qzxXAzWAkc2A== X-CSE-MsgGUID: iIg+bEoTSIaBdp/zD8jIRg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="59647773" Received: from ijarvine-desk1.ger.corp.intel.com (HELO localhost) ([10.245.247.74]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:25:56 -0700 From: =?utf-8?q?Ilpo_J=C3=A4rvinen?= To: linux-pci@vger.kernel.org, Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Igor Mammedov , Mika Westerberg , Andy Shevchenko , "Rafael J . Wysocki" , Jonathan Cameron , linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Lidong Wang , Andy Shevchenko Subject: [PATCH v3 2/8] resource: Rename find_resource() to find_resource_space() Date: Tue, 7 May 2024 13:25:17 +0300 Message-Id: <20240507102523.57320-3-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> References: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Rename find_resource() to find_resource_space() to better describe what the functions does. This is a preparation for exposing it beyond resource.c which is needed by PCI core. Also rename the __ variant to match the names. Tested-by: Lidong Wang Signed-off-by: Ilpo Järvinen Reviewed-by: Andy Shevchenko Reviewed-by: Mika Westerberg --- kernel/resource.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/kernel/resource.c b/kernel/resource.c index fcbca39dbc45..e163e0a8f2f8 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -628,13 +628,12 @@ static void resource_clip(struct resource *res, resource_size_t min, } /* - * Find empty slot in the resource tree with the given range and + * Find empty space in the resource tree with the given range and * alignment constraints */ -static int __find_resource(struct resource *root, struct resource *old, - struct resource *new, - resource_size_t size, - struct resource_constraint *constraint) +static int __find_resource_space(struct resource *root, struct resource *old, + struct resource *new, resource_size_t size, + struct resource_constraint *constraint) { struct resource *this = root->child; struct resource tmp = *new, avail, alloc; @@ -688,13 +687,13 @@ next: if (!this || this->end == root->end) } /* - * Find empty slot in the resource tree given range and alignment. + * Find empty space in the resource tree given range and alignment. */ -static int find_resource(struct resource *root, struct resource *new, - resource_size_t size, - struct resource_constraint *constraint) +static int find_resource_space(struct resource *root, struct resource *new, + resource_size_t size, + struct resource_constraint *constraint) { - return __find_resource(root, NULL, new, size, constraint); + return __find_resource_space(root, NULL, new, size, constraint); } /** @@ -717,7 +716,7 @@ static int reallocate_resource(struct resource *root, struct resource *old, write_lock(&resource_lock); - if ((err = __find_resource(root, old, &new, newsize, constraint))) + if ((err = __find_resource_space(root, old, &new, newsize, constraint))) goto out; if (resource_contains(&new, old)) { @@ -786,7 +785,7 @@ int allocate_resource(struct resource *root, struct resource *new, } write_lock(&resource_lock); - err = find_resource(root, new, size, &constraint); + err = find_resource_space(root, new, size, &constraint); if (err >= 0 && __request_resource(root, new)) err = -EBUSY; write_unlock(&resource_lock); From patchwork Tue May 7 10:25:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ilpo_J=C3=A4rvinen?= X-Patchwork-Id: 13656585 X-Patchwork-Delegate: bhelgaas@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) (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 98F0D14EC6E; Tue, 7 May 2024 10:26:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077573; cv=none; b=ARJlkfmonHIvtTRcIK7D8VjavSYMy8kYPUW5ZxcT9Y0MMdc5dq68yXWAe3uyY/A5TaBEGLsTfyhfA9Yn/1WS3VOQNo7igR14UMZ4Zu7OWjT4FiMyunU5/A8wF9QiAtVf/EKpfi4YQOE1562nlk9pKABnLmcxXxKKg2ZyKO1DR7E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077573; c=relaxed/simple; bh=0pOejR4b1nnv+vutW3rPvxAwbOICY8dGGQ7kTWbPnwo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=OJLzGVpFn2nYpPkVRhq9Iz7jsjrfQt2NDnXSbZAwimIfyuzg6+wrWqfD87HzGutA0WMvsNzXiswMusoqEg0uZe3GIPfGn1biJ8tGjIIqyqjN09jD7//3ZE7KM4l/PZ6zb0cS2lPOgSJ27DGrCWfUYrahFtixGgDWmgSd7fVW0i4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Epb6r02H; arc=none smtp.client-ip=198.175.65.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Epb6r02H" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1715077572; x=1746613572; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0pOejR4b1nnv+vutW3rPvxAwbOICY8dGGQ7kTWbPnwo=; b=Epb6r02HCavWcDolNFTcklydn7pJRjjf2dRtg+7D1YsXvZPw+J0XhTg8 RPhvO7Xh6MatfjK5NU6fSNbh/vXr8VNAd7kAprYY/T7XFBiuAH36aOhzd sPD5GvWiP9hWm9AfahL3U/MN8EAllTVCozDBKgjrTnI+z+ns/7J/u8nGy 9t8ea/ZJa659HVMWs+aR7a8iMqlkt5CjGxh4gAYwl4j+fhMiUvXkOMV6j Gi3zDS1LCk+NzzMAjtUM0gp6U3jG8pyUijNRBOOV89pSP9EZDsWo0neGr /FoxG2NPGmueDN1x3u1qBlulokIAJQksb8/8SHSmc8pojLqQ7Qc70TCxW A==; X-CSE-ConnectionGUID: 5eP9nW5EThqzPWOZ0ZG5xw== X-CSE-MsgGUID: 1/v+zRgUTI6vas3MO+EXZw== X-IronPort-AV: E=McAfee;i="6600,9927,11065"; a="14647108" X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="14647108" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:11 -0700 X-CSE-ConnectionGUID: Sj6dYPp+THiCZ04++GKnvg== X-CSE-MsgGUID: ULSpPTdrSRKZkllcY+5eFQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="29059922" Received: from ijarvine-desk1.ger.corp.intel.com (HELO localhost) ([10.245.247.74]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:06 -0700 From: =?utf-8?q?Ilpo_J=C3=A4rvinen?= To: linux-pci@vger.kernel.org, Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Igor Mammedov , Mika Westerberg , Andy Shevchenko , "Rafael J . Wysocki" , Jonathan Cameron , linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Lidong Wang , Andy Shevchenko Subject: [PATCH v3 3/8] resource: Document find_resource_space() and resource_constraint Date: Tue, 7 May 2024 13:25:18 +0300 Message-Id: <20240507102523.57320-4-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> References: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Document find_resource_space() and the struct resource_constraint as they are going to be exposed outside of resource.c. Tested-by: Lidong Wang Signed-off-by: Ilpo Järvinen Reviewed-by: Andy Shevchenko Reviewed-by: Mika Westerberg --- kernel/resource.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/kernel/resource.c b/kernel/resource.c index e163e0a8f2f8..3f15a32d9c42 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -48,7 +48,19 @@ struct resource iomem_resource = { }; EXPORT_SYMBOL(iomem_resource); -/* constraints to be met while allocating resources */ +/** + * struct resource_constraint - constraints to be met while searching empty + * resource space + * @min: The minimum address for the memory range + * @max: The maximum address for the memory range + * @align: Alignment for the start address of the empty space + * @alignf: Additional alignment constraints callback + * @alignf_data: Data provided for @alignf callback + * + * Contains the range and alignment constraints that have to be met during + * find_resource_space(). @alignf can be NULL indicating no alignment beyond + * @align is necessary. + */ struct resource_constraint { resource_size_t min, max, align; resource_size_t (*alignf)(void *, const struct resource *, @@ -686,8 +698,19 @@ next: if (!this || this->end == root->end) return -EBUSY; } -/* - * Find empty space in the resource tree given range and alignment. +/** + * find_resource_space - Find empty space in the resource tree + * @root: Root resource descriptor + * @new: Resource descriptor awaiting an empty resource space + * @size: The minimum size of the empty space + * @constraint: The range and alignment constraints to be met + * + * Finds an empty space under @root in the resource tree satisfying range and + * alignment @constraints. + * + * Return: + * * %0 - if successful, @new members start, end, and flags are altered. + * * %-EBUSY - if no empty space was found. */ static int find_resource_space(struct resource *root, struct resource *new, resource_size_t size, From patchwork Tue May 7 10:25:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ilpo_J=C3=A4rvinen?= X-Patchwork-Id: 13656586 X-Patchwork-Delegate: bhelgaas@google.com Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (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 43EC814EC6E; Tue, 7 May 2024 10:26:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077583; cv=none; b=Cv7ieigs0FztcuAp5NKqbddi7Z7nqNShgDYcC+PmtIcKE+CadXNP9K+40vCc6YQRbWilcgwVXdcH8o70cXlurtQHzlM1u9emIwkau0E0Y19mvEEmRZzifW3EJ75+s2lhXdRKuOG+HB96o5OrLgdifpVgTQP8EE016UIyouHcqbg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077583; c=relaxed/simple; bh=miOeqO2HdKsG2/bDooVNLUjXiD0Q3Vj+quzbPCrosQQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=jYcIeTv2pCRHKJfwFB+DGkacYQLeznmDJmMdGIbcCsvL8LEfDwLBY2UdiwGQ9qxujRHnDFwSgPkGiZZzynwn9i83i6+x3hediXBHk/M7GCnzmS+2dn1bAYtic0OsCGuMWE3yhUoKz1CsuTVZZH8opLVLnVCHz2o+MHSetrKxygg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=QVZSLiQt; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="QVZSLiQt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1715077582; x=1746613582; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=miOeqO2HdKsG2/bDooVNLUjXiD0Q3Vj+quzbPCrosQQ=; b=QVZSLiQtnfRn+3DjRxI4TIG1h0Bd6FGmp4N08p2JGm0i0DDo7ZWa9VMJ RJdByWj2bXiDNxMT5wM07+HWKNCG+RJ1qWZg054bBoOv9GzTd71YCMO4D oSSS9MsMnPqM+tnTlV5jnw7hbmosgrIB+Plr+jQtByWnunyvB0ORFaGUc bEGaeedcGhBIQkYZNX6Ed9cAhgH96MM1BDUDXF+ZPQiLqJ+bmf43U29o/ fXNDMTZyLK9sgjx/Y4DXbLnHug4f/amz+2tn7huSozZpydPSmvTgJ1gDq 84Zjb+5S4Nm96f0Bb5PWlp1/ga9Y5sArbIFOqrKTzIpzo7q1GZ1+d07pe Q==; X-CSE-ConnectionGUID: ZoH9nZt8SB+2+wPdEPAMig== X-CSE-MsgGUID: PKNXAghTTemAq/aRO7f+vQ== X-IronPort-AV: E=McAfee;i="6600,9927,11065"; a="10746795" X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="10746795" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:21 -0700 X-CSE-ConnectionGUID: fJg+Q2OwT+m+UgN7zVBNqg== X-CSE-MsgGUID: lJxIxVboSdWnOBlxykGV3g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="59647805" Received: from ijarvine-desk1.ger.corp.intel.com (HELO localhost) ([10.245.247.74]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:16 -0700 From: =?utf-8?q?Ilpo_J=C3=A4rvinen?= To: linux-pci@vger.kernel.org, Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Igor Mammedov , Mika Westerberg , Andy Shevchenko , "Rafael J . Wysocki" , Jonathan Cameron , linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Andy Shevchenko , Lidong Wang Subject: [PATCH v3 4/8] resource: Use typedef for alignf callback Date: Tue, 7 May 2024 13:25:19 +0300 Message-Id: <20240507102523.57320-5-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> References: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To make it simpler to declare resource constraint alignf callbacks, add typedef for it and document it. Suggested-by: Andy Shevchenko Tested-by: Lidong Wang Signed-off-by: Ilpo Järvinen Reviewed-by: Andy Shevchenko Reviewed-by: Mika Westerberg --- drivers/pci/bus.c | 10 ++-------- include/linux/ioport.h | 22 ++++++++++++++++++---- include/linux/pci.h | 5 +---- kernel/resource.c | 8 ++------ 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c index 826b5016a101..dfc99b3cb958 100644 --- a/drivers/pci/bus.c +++ b/drivers/pci/bus.c @@ -176,10 +176,7 @@ static void pci_clip_resource_to_region(struct pci_bus *bus, static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res, resource_size_t size, resource_size_t align, resource_size_t min, unsigned long type_mask, - resource_size_t (*alignf)(void *, - const struct resource *, - resource_size_t, - resource_size_t), + resource_alignf alignf, void *alignf_data, struct pci_bus_region *region) { @@ -250,10 +247,7 @@ static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res, int pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res, resource_size_t size, resource_size_t align, resource_size_t min, unsigned long type_mask, - resource_size_t (*alignf)(void *, - const struct resource *, - resource_size_t, - resource_size_t), + resource_alignf alignf, void *alignf_data) { #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT diff --git a/include/linux/ioport.h b/include/linux/ioport.h index db7fe25f3370..28266426e5bf 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -188,6 +188,23 @@ enum { #define DEFINE_RES_DMA(_dma) \ DEFINE_RES_DMA_NAMED((_dma), NULL) +/** + * typedef resource_alignf - Resource alignment callback + * @data: Private data used by the callback + * @res: Resource candidate range (an empty resource space) + * @size: The minimum size of the empty space + * @align: Alignment from the constraints + * + * Callback allows calculating resource placement and alignment beyond min, + * max, and align fields in the struct resource_constraint. + * + * Return: Start address for the resource. + */ +typedef resource_size_t (*resource_alignf)(void *data, + const struct resource *res, + resource_size_t size, + resource_size_t align); + /* PC/ISA/whatever - the normal PC address spaces: IO and memory */ extern struct resource ioport_resource; extern struct resource iomem_resource; @@ -207,10 +224,7 @@ extern void arch_remove_reservations(struct resource *avail); extern int allocate_resource(struct resource *root, struct resource *new, resource_size_t size, resource_size_t min, resource_size_t max, resource_size_t align, - resource_size_t (*alignf)(void *, - const struct resource *, - resource_size_t, - resource_size_t), + resource_alignf alignf, void *alignf_data); struct resource *lookup_resource(struct resource *root, resource_size_t start); int adjust_resource(struct resource *res, resource_size_t start, diff --git a/include/linux/pci.h b/include/linux/pci.h index 16493426a04f..dde87db6b982 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1552,10 +1552,7 @@ int __must_check pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res, resource_size_t size, resource_size_t align, resource_size_t min, unsigned long type_mask, - resource_size_t (*alignf)(void *, - const struct resource *, - resource_size_t, - resource_size_t), + resource_alignf alignf, void *alignf_data); diff --git a/kernel/resource.c b/kernel/resource.c index 3f15a32d9c42..26ad6f223652 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -63,8 +63,7 @@ EXPORT_SYMBOL(iomem_resource); */ struct resource_constraint { resource_size_t min, max, align; - resource_size_t (*alignf)(void *, const struct resource *, - resource_size_t, resource_size_t); + resource_alignf alignf; void *alignf_data; }; @@ -783,10 +782,7 @@ static int reallocate_resource(struct resource *root, struct resource *old, int allocate_resource(struct resource *root, struct resource *new, resource_size_t size, resource_size_t min, resource_size_t max, resource_size_t align, - resource_size_t (*alignf)(void *, - const struct resource *, - resource_size_t, - resource_size_t), + resource_alignf alignf, void *alignf_data) { int err; From patchwork Tue May 7 10:25:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ilpo_J=C3=A4rvinen?= X-Patchwork-Id: 13656587 X-Patchwork-Delegate: bhelgaas@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (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 7A7021514CE; Tue, 7 May 2024 10:26:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077592; cv=none; b=RTMn7b93YDof1SOVhk8Tg+kvuhrGIBvB54WxUchMG3mvFtM32UjrakRQw0vbMIonBNhkwvXA4ZwiGYqlfW/Y2i0LF/aY8TrS8VxWx4MAm0Ini8NzoctJmtF5pYQdfT04sjln/ldTP7cA9AV11nIW8exxeF13Lsrvh8dun0yHaf8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077592; c=relaxed/simple; bh=jhrlnsAO2on72ucVzVfwXWvO+qQRZl2vqCOZJh3oiMM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=Kl1uXRV29EdXJKvsDBqSEvUwXuwrBFiKAbwCiNlZPmrYh1YIVJM7h+YTzwnx7XXJvatz9NlUgdOYiHjatknOjoWs+yEr0a5u1LRbiBxBVX+egwcULO02TzEu12Zt4/a0zFgrGPrM+56wdx/Ln1axMbFJkYxV6YiPGbmKwryjwGk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=FK2lQARn; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="FK2lQARn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1715077591; x=1746613591; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jhrlnsAO2on72ucVzVfwXWvO+qQRZl2vqCOZJh3oiMM=; b=FK2lQARn2xQ2XyzLbVldkK6h4TrsPJqgx1Ji0b3sgZugQzUitpYPz7Mg JIjJYcwN8X358FMSNyIAKiTz9wZHJNHFozhLCQoY1WllnaVd4xy3oYoCC e8kPUZX7CPsJALVzWm9GVYj/1sddcMifcoJ0NYPlzDcDGw7gnc0tqdrAk 9yaPhqErl4k8lB+XdjbIqVCQuUqnjOgt5TGJ9oacUCwKbf7Ka4RRPM08Q dPKopHaixa0bnWVM7a+3Maf/ybi2dca2tfXkjRWAzt1mohu/5F/WJCV46 q5SJomSuV67CHm3Rthm0T874mAnAQC//nzWeD+JghXrZ6nUSWoG40FJIz g==; X-CSE-ConnectionGUID: 5ZAvAdcYTCC8t+F+6lezxA== X-CSE-MsgGUID: CEmjIZjhRkexCOULksqFWw== X-IronPort-AV: E=McAfee;i="6600,9927,11065"; a="10987099" X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="10987099" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:31 -0700 X-CSE-ConnectionGUID: xSn39xCiSA2h6foBR1GuKg== X-CSE-MsgGUID: pkjZ2lTuTfGeJVsfy8vKew== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="28535561" Received: from ijarvine-desk1.ger.corp.intel.com (HELO localhost) ([10.245.247.74]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:27 -0700 From: =?utf-8?q?Ilpo_J=C3=A4rvinen?= To: linux-pci@vger.kernel.org, Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Igor Mammedov , Mika Westerberg , Andy Shevchenko , "Rafael J . Wysocki" , Jonathan Cameron , linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Lidong Wang , Andy Shevchenko Subject: [PATCH v3 5/8] resource: Handle simple alignment inside __find_resource_space() Date: Tue, 7 May 2024 13:25:20 +0300 Message-Id: <20240507102523.57320-6-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> References: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 allocate_resource() accepts ->alignf() callback to perform custom alignment beyond constraint->align. If alignf is NULL, simple_align_resource() is used which only returns avail->start (no change). Using avail->start directly is natural and can be done with a conditional in __find_resource_space() instead which avoids unnecessarily using callback. It makes the code inside __find_resource_space() more obvious and removes the need for the caller to provide constraint->alignf unnecessarily. This is preparation for exporting find_resource_space(). Tested-by: Lidong Wang Signed-off-by: Ilpo Järvinen Reviewed-by: Andy Shevchenko Reviewed-by: Mika Westerberg --- kernel/resource.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/kernel/resource.c b/kernel/resource.c index 26ad6f223652..35c44c23b037 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -621,14 +621,6 @@ void __weak arch_remove_reservations(struct resource *avail) { } -static resource_size_t simple_align_resource(void *data, - const struct resource *avail, - resource_size_t size, - resource_size_t align) -{ - return avail->start; -} - static void resource_clip(struct resource *res, resource_size_t min, resource_size_t max) { @@ -648,6 +640,7 @@ static int __find_resource_space(struct resource *root, struct resource *old, { struct resource *this = root->child; struct resource tmp = *new, avail, alloc; + resource_alignf alignf = constraint->alignf; tmp.start = root->start; /* @@ -676,8 +669,12 @@ static int __find_resource_space(struct resource *root, struct resource *old, avail.flags = new->flags & ~IORESOURCE_UNSET; if (avail.start >= tmp.start) { alloc.flags = avail.flags; - alloc.start = constraint->alignf(constraint->alignf_data, &avail, - size, constraint->align); + if (alignf) { + alloc.start = alignf(constraint->alignf_data, + &avail, size, constraint->align); + } else { + alloc.start = avail.start; + } alloc.end = alloc.start + size - 1; if (alloc.start <= alloc.end && resource_contains(&avail, &alloc)) { @@ -788,9 +785,6 @@ int allocate_resource(struct resource *root, struct resource *new, int err; struct resource_constraint constraint; - if (!alignf) - alignf = simple_align_resource; - constraint.min = min; constraint.max = max; constraint.align = align; From patchwork Tue May 7 10:25:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ilpo_J=C3=A4rvinen?= X-Patchwork-Id: 13656588 X-Patchwork-Delegate: bhelgaas@google.com Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (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 C87AA14F11B; Tue, 7 May 2024 10:26:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.19 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077604; cv=none; b=FlGoRPQK3DTgNfiqtyvea2XTfsuifSFeZiuKGYlqe9TQ5dX1ofpeT82blaUbT39LPPCxXZXrkdb9LrLQuPJoLxjbXspZsjHEBmESg9QjOumIZkyu8D4tU+O2+5iYZ/5rMAEzGV8TuYshyLHgzzM1ThPqD/pHWt64GDT24dOSEWU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077604; c=relaxed/simple; bh=OtRBI9pR8VBzzT0laOEFtAlpEA+0KVdeY8kwVqKXTZU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=rIQbTcL32G0iaWpzDFrSWgNkJ9KFbhl66XrNEselEbmwkczvvVNkrrs5xVpi8MultViNfXUFQbqKRhZFTAtvE1UZiHPKkpqALYbe2FuLSsaOWzq+wNrgVIO6YeNMujFlE1hZ7lsze0SWYQ1Mri5YVY2o+gGyZLQTLX5Mc3VbpDI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=FLXsWMzR; arc=none smtp.client-ip=192.198.163.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="FLXsWMzR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1715077603; x=1746613603; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OtRBI9pR8VBzzT0laOEFtAlpEA+0KVdeY8kwVqKXTZU=; b=FLXsWMzRlSxPybEqMJ6SlBJMf5MYN38R/NNqJ7kCzhA9AQPfBCS9pTJc v2klth8SWnnrUajdkbIpBG/JKRkx9hV9FBbwlzSFl1KpbWW0rLM59odCW KGUys2LL14PkcGDyKPKc8IYoGZ73zb1Na7wOSMangvo6clncL6HGVIqHk c+/DEiiPu4pyixp19MH8iGDOMkl6dyR4uO6CLJZwwBRKEulnPkDYs1TA/ tjtkKhYZBv0mlon0SShvxrdxABADeMjs3g1Futjdw/rsUrcxow1bgDcVg JQWoQ+beAQUpuYf1lEWx4E5IcnS+ypLqu4Ul1HUnzhAjiXqI7pnGUV52K g==; X-CSE-ConnectionGUID: LZHkhUcIR5CYaBswXBh2eg== X-CSE-MsgGUID: 4/odz09lSkWz3QUk0qov5g== X-IronPort-AV: E=McAfee;i="6600,9927,11065"; a="10729844" X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="10729844" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:41 -0700 X-CSE-ConnectionGUID: KoQkCv+qQsOQ9pW7bquFig== X-CSE-MsgGUID: d+BhI2B0Tvm4EbT1wXE6Uw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="33280265" Received: from ijarvine-desk1.ger.corp.intel.com (HELO localhost) ([10.245.247.74]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:36 -0700 From: =?utf-8?q?Ilpo_J=C3=A4rvinen?= To: linux-pci@vger.kernel.org, Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Igor Mammedov , Mika Westerberg , Andy Shevchenko , "Rafael J . Wysocki" , Jonathan Cameron , linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Lidong Wang , Andy Shevchenko Subject: [PATCH v3 6/8] resource: Export find_resource_space() Date: Tue, 7 May 2024 13:25:21 +0300 Message-Id: <20240507102523.57320-7-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> References: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 PCI bridge window logic needs to find out in advance to the actual allocation if there is an empty space big enough to fit the window. Export find_resource_space() for the purpose. Also move the struct resource_constraint into generic header to be able to use the new interface. Tested-by: Lidong Wang Signed-off-by: Ilpo Järvinen Reviewed-by: Andy Shevchenko Reviewed-by: Mika Westerberg --- include/linux/ioport.h | 22 ++++++++++++++++++++++ kernel/resource.c | 26 ++++---------------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/include/linux/ioport.h b/include/linux/ioport.h index 28266426e5bf..6e9fb667a1c5 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -205,6 +205,25 @@ typedef resource_size_t (*resource_alignf)(void *data, resource_size_t size, resource_size_t align); +/** + * struct resource_constraint - constraints to be met while searching empty + * resource space + * @min: The minimum address for the memory range + * @max: The maximum address for the memory range + * @align: Alignment for the start address of the empty space + * @alignf: Additional alignment constraints callback + * @alignf_data: Data provided for @alignf callback + * + * Contains the range and alignment constraints that have to be met during + * find_resource_space(). @alignf can be NULL indicating no alignment beyond + * @align is necessary. + */ +struct resource_constraint { + resource_size_t min, max, align; + resource_alignf alignf; + void *alignf_data; +}; + /* PC/ISA/whatever - the normal PC address spaces: IO and memory */ extern struct resource ioport_resource; extern struct resource iomem_resource; @@ -278,6 +297,9 @@ static inline bool resource_union(const struct resource *r1, const struct resour return true; } +int find_resource_space(struct resource *root, struct resource *new, + resource_size_t size, struct resource_constraint *constraint); + /* Convenience shorthand with allocation */ #define request_region(start,n,name) __request_region(&ioport_resource, (start), (n), (name), 0) #define request_muxed_region(start,n,name) __request_region(&ioport_resource, (start), (n), (name), IORESOURCE_MUXED) diff --git a/kernel/resource.c b/kernel/resource.c index 35c44c23b037..14777afb0a99 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -48,25 +48,6 @@ struct resource iomem_resource = { }; EXPORT_SYMBOL(iomem_resource); -/** - * struct resource_constraint - constraints to be met while searching empty - * resource space - * @min: The minimum address for the memory range - * @max: The maximum address for the memory range - * @align: Alignment for the start address of the empty space - * @alignf: Additional alignment constraints callback - * @alignf_data: Data provided for @alignf callback - * - * Contains the range and alignment constraints that have to be met during - * find_resource_space(). @alignf can be NULL indicating no alignment beyond - * @align is necessary. - */ -struct resource_constraint { - resource_size_t min, max, align; - resource_alignf alignf; - void *alignf_data; -}; - static DEFINE_RWLOCK(resource_lock); static struct resource *next_resource(struct resource *p, bool skip_children) @@ -708,12 +689,13 @@ next: if (!this || this->end == root->end) * * %0 - if successful, @new members start, end, and flags are altered. * * %-EBUSY - if no empty space was found. */ -static int find_resource_space(struct resource *root, struct resource *new, - resource_size_t size, - struct resource_constraint *constraint) +int find_resource_space(struct resource *root, struct resource *new, + resource_size_t size, + struct resource_constraint *constraint) { return __find_resource_space(root, NULL, new, size, constraint); } +EXPORT_SYMBOL_GPL(find_resource_space); /** * reallocate_resource - allocate a slot in the resource tree given range & alignment. From patchwork Tue May 7 10:25:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ilpo_J=C3=A4rvinen?= X-Patchwork-Id: 13656589 X-Patchwork-Delegate: bhelgaas@google.com Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (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 8A64514F11B; Tue, 7 May 2024 10:26:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.19 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077612; cv=none; b=XSReHzGDQKfo7e+ruhaOZ5wtXlL6fFNjZRSOARjCjeKeyTSbJ+WFcmYtMK3q+oqeE7rCcelLRFMFaIFUJZRNPG/6Bq5cU7qS5pfv5B3oPjB8zfVA1d4/TpSnH3X2up6JUbDbu7xy7LMoLfxOh8LvHo6LQzmhDzglAo9ZSQTXtL4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077612; c=relaxed/simple; bh=sRJlzCczuj0xPegba6C3Pnkn/jUNQt5RNJg+ram5wAc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=PkgMgl2X5CIwLT4oI3MNkSYWuYeU2tUUilnrA43LEiw9McYJfCkNMqyP9A470sbsnN2q7d8OeobQSsvUGFmjONSA18rIlwU5lD9E6y4JYd9GLYOklg7h4Tv7gE2YGa9yUKc/0pVR9dmhi3ICR+3kD9PyFLztFVdGs5eQnDyxgOs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=a0Jxd/oT; arc=none smtp.client-ip=192.198.163.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="a0Jxd/oT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1715077611; x=1746613611; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sRJlzCczuj0xPegba6C3Pnkn/jUNQt5RNJg+ram5wAc=; b=a0Jxd/oTFxAkJsa1j5Ftm3eQSSmKzlnaB1FNyXfIGb/H08JnFPtDWxZV JVvQ85fVtfAJap2BJX1mF4JkE+7ueR6HnwHbiwWxBUVGerWP/H/rTnKzZ IVlfk7FwMIUqcCWPRKt2zycpEAbrPfAyWR3T7VL5CYqFvGyBmVKKZTrFC Hd19ML2UOif2SaMYONsMwNpDjX1GQKnA3vbsZpPK5X8NBnhISESgIL8DH uuWaOnlOeiByrQA/uPZ+x/ElB6rxlvnyB+Q60sfCABaxVwfOx2HBkHdoY 8UeWeGHS5aUYcbU0MzhUll1emQr9YC11xKWBGcZkRqnZUil9A4aQBqSaW A==; X-CSE-ConnectionGUID: hm+2YKm8TJSSKR3dCZ6CaA== X-CSE-MsgGUID: AnCBI0BqTCeTOqIYjBeZeQ== X-IronPort-AV: E=McAfee;i="6600,9927,11065"; a="10729877" X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="10729877" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:51 -0700 X-CSE-ConnectionGUID: LULzOzlnReebYa+E5zhaYQ== X-CSE-MsgGUID: TJBtTMT6SLu5DzVmxvDw9Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="33280303" Received: from ijarvine-desk1.ger.corp.intel.com (HELO localhost) ([10.245.247.74]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:47 -0700 From: =?utf-8?q?Ilpo_J=C3=A4rvinen?= To: linux-pci@vger.kernel.org, Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Igor Mammedov , Mika Westerberg , Andy Shevchenko , "Rafael J . Wysocki" , Jonathan Cameron , linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ilpo_J=C3=A4rvinen?= Subject: [PATCH v3 7/8] PCI: Make minimum bridge window alignment reference more obvious Date: Tue, 7 May 2024 13:25:22 +0300 Message-Id: <20240507102523.57320-8-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> References: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Calculations related to bridge window size contain literal 20 that is the minimum alignment for a bridge window. Make the code more obvious by converting the literal 20 to __ffs(SZ_1MB). Signed-off-by: Ilpo Järvinen Reviewed-by: Mika Westerberg --- drivers/pci/setup-bus.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c index 141d6b31959b..bca1df46f19c 100644 --- a/drivers/pci/setup-bus.c +++ b/drivers/pci/setup-bus.c @@ -14,6 +14,7 @@ * tighter packing. Prefetchable range support. */ +#include #include #include #include @@ -21,6 +22,7 @@ #include #include #include +#include #include #include #include "pci.h" @@ -957,7 +959,7 @@ static inline resource_size_t calculate_mem_align(resource_size_t *aligns, for (order = 0; order <= max_order; order++) { resource_size_t align1 = 1; - align1 <<= (order + 20); + align1 <<= (order + __ffs(SZ_1M)); if (!align) min_align = align1; @@ -1047,7 +1049,7 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, * resources. */ align = pci_resource_alignment(dev, r); - order = __ffs(align) - 20; + order = __ffs(align) - __ffs(SZ_1M); if (order < 0) order = 0; if (order >= ARRAY_SIZE(aligns)) { From patchwork Tue May 7 10:25:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ilpo_J=C3=A4rvinen?= X-Patchwork-Id: 13656590 X-Patchwork-Delegate: bhelgaas@google.com Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (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 E803815099D; Tue, 7 May 2024 10:27:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077622; cv=none; b=N5jcJx7Y9p2I567TLP4vRwCYHhCybC+U6nbdXJN5VS/dS1HETlGimiKE3Qb4f2OcSmdUqgQL7YNiaLUSow8lK95Vuf7aMqUMoo0oENKEx6Nwqd45luhax+yk8H0fx+6KA82lix338aGJNFn2QqkhYB5VEhWZI/nozm4GkePE1UU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715077622; c=relaxed/simple; bh=nt/ZLutgromZ7gcwB6Dx88HxYCkd13s5thFkq0sTUgo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=dEGG5v75gZnoQlK4nCjwhkCZ8HA4lHEmaMMiH3OexzUtS1NwSMvA55F6hTW4CSduWceL21v9QJeC4Ube0k23lXdv9rUMavYfK+e85Wpuuo26WqvYLbyZARA2k3XmGUpm7n90IUBV9K9cVkcBwubICSBXYgMHRdrr+2aaU5pupTE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=XGcApnKE; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="XGcApnKE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1715077621; x=1746613621; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nt/ZLutgromZ7gcwB6Dx88HxYCkd13s5thFkq0sTUgo=; b=XGcApnKEDn2PSU+fYCVUJDbaRMGr5NjB7NPVM/sKOtBwar2D/fbdYL3a 9EsRfq3g4/jRiJmUolgIZWnNWtfmvC0Om+mxQ3huU8FCe00EI/DxMqdBu N59VCWcKtk0kdLjsBBHnZRRDkIsm8o92R+5lQAdIkCHpZrEsOPzCuuHfg wzYRjYrnDLzoGGJ4Kv6MXriKI5YmfPVoPRIFEx6sJDQy97IbWzvd/lHKI 2SUcjTNc5UgB0syUluGMDx3h+dOVHilpyzNVzYLp8zfch25fCYwlv9Npr ZjJinQBbEbNWllLrzm4QucOuy1YD60SSzodv35E/GaxOLN/tO0nTGtJ+I w==; X-CSE-ConnectionGUID: ZmscuW21T8KM9hCIK0FJwg== X-CSE-MsgGUID: 2JSlOsT+SnS+ummjeAUlaQ== X-IronPort-AV: E=McAfee;i="6600,9927,11065"; a="10987193" X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="10987193" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:27:01 -0700 X-CSE-ConnectionGUID: T6h0186/TKC0AdLZaYMvMQ== X-CSE-MsgGUID: ljHjrteKTm2UmOKNsvbwIA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,261,1708416000"; d="scan'208";a="28535704" Received: from ijarvine-desk1.ger.corp.intel.com (HELO localhost) ([10.245.247.74]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2024 03:26:56 -0700 From: =?utf-8?q?Ilpo_J=C3=A4rvinen?= To: linux-pci@vger.kernel.org, Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Igor Mammedov , Mika Westerberg , Andy Shevchenko , "Rafael J . Wysocki" , Jonathan Cameron , linux-kernel@vger.kernel.org Cc: =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Lidong Wang Subject: [PATCH v3 8/8] PCI: Relax bridge window tail sizing rules Date: Tue, 7 May 2024 13:25:23 +0300 Message-Id: <20240507102523.57320-9-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> References: <20240507102523.57320-1-ilpo.jarvinen@linux.intel.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 During remove & rescan cycle, PCI subsystem will recalculate and adjust the bridge window sizing that was initially done by "BIOS". The size calculation is based on the required alignment of the largest resource among the downstream resources as per pbus_size_mem() (unimportant or zero parameters marked with "..."): min_align = calculate_mem_align(aligns, max_order); size0 = calculate_memsize(size, ..., min_align); inside calculate_memsize(), for the largest alignment: min_align = align1 >> 1; ... return min_align; and then in calculate_memsize(): return ALIGN(max(size, ...), align); If the original bridge window sizing tried to conserve space, this will lead to massive increase of the required bridge window size when the downstream has a large disparity in BAR sizes. E.g., with 16MiB and 16GiB BARs this results in 24GiB bridge window size even if 16MiB BAR does not require gigabytes of space to fit. When doing remove & rescan for a bus that contains such a PCI device, a larger bridge window is suddenly required on rescan but when there is a bridge window upstream that is already assigned based on the original size, it cannot be enlarged to the new requirement. This causes the allocation of the bridge window to fail (0x600000000 > 0x400ffffff): pci 0000:02:01.0: PCI bridge to [bus 03] pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff] pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref] pci 0000:01:00.0: PCI bridge to [bus 02-04] pci 0000:01:00.0: bridge window [mem 0x40400000-0x406fffff] pci 0000:01:00.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref] pci 0000:03:00.0: device released pci 0000:02:01.0: device released pcieport 0000:01:00.0: scanning [bus 02-04] behind bridge, pass 0 pci 0000:02:01.0: PCI bridge to [bus 03] pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff] pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref] pci 0000:02:01.0: scanning [bus 03-03] behind bridge, pass 0 pci 0000:03:00.0: BAR 0 [mem 0x6400000000-0x6400ffffff 64bit pref] pci 0000:03:00.0: BAR 2 [mem 0x6000000000-0x63ffffffff 64bit pref] pci 0000:03:00.0: ROM [mem 0x40400000-0x405fffff pref] pci 0000:02:01.0: PCI bridge to [bus 03] pci 0000:02:01.0: scanning [bus 03-03] behind bridge, pass 1 pcieport 0000:01:00.0: scanning [bus 02-04] behind bridge, pass 1 pci 0000:02:01.0: bridge window [mem size 0x600000000 64bit pref]: can't assign; no space pci 0000:02:01.0: bridge window [mem size 0x600000000 64bit pref]: failed to assign pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]: assigned pci 0000:03:00.0: BAR 2 [mem size 0x400000000 64bit pref]: can't assign; no space pci 0000:03:00.0: BAR 2 [mem size 0x400000000 64bit pref]: failed to assign pci 0000:03:00.0: BAR 0 [mem size 0x01000000 64bit pref]: can't assign; no space pci 0000:03:00.0: BAR 0 [mem size 0x01000000 64bit pref]: failed to assign pci 0000:03:00.0: ROM [mem 0x40400000-0x405fffff pref]: assigned pci 0000:02:01.0: PCI bridge to [bus 03] pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff] This is a major surprise for users who are suddenly left with a PCIe device that was working fine with the original bridge window sizing. Even if the already assigned bridge window could be enlarged by reallocation in some cases (something the current code does not attempt to do), it is not possible in general case and the large amount of wasted space at the tail of the bridge window may lead to other resource exhaustion problems on Root Complex level (think of multiple PCIe cards with VFs and BAR size disparity in a single system). PCI specifications only expect natural alignment for BARs (PCI Express Base Specification, rev. 6.1 sect. 7.5.1.2.1) and minimum of 1MiB alignment for the bridge window (PCI Express Base Specification, rev 6.1 sect. 7.5.1.3). The current bridge window tail alignment rule was introduced in the commit 5d0a8965aea9 ("[PATCH] 2.5.14: New PCI allocation code (alpha, arm, parisc) [2/2]") that only states: "pbus_size_mem: core stuff; tested with randomly generated sets of resources". It does not explain the motivation for the extra tail space allocated that is not truly needed by the downstream resources. As such, it is far from clear if it ever has been required by any HW. To prevent PCIe cards with BAR size disparity from becoming unusable after remove & rescan cycle, attempt to do a truly minimal allocation for memory resources if needed. First check if the normally calculated bridge window will not fit into an already assigned upstream resource. In such case, try with relaxed bridge window tail sizing rules instead where no extra tail space is requested beyond what the downstream resources require. Only enforce the alignment requirement of the bridge window itself (normally 1MiB). With this patch, the resources are successfully allocated: pci 0000:02:01.0: PCI bridge to [bus 03] pci 0000:02:01.0: scanning [bus 03-03] behind bridge, pass 1 pcieport 0000:01:00.0: scanning [bus 02-04] behind bridge, pass 1 pcieport 0000:01:00.0: Assigned bridge window [mem 0x6000000000-0x6400ffffff 64bit pref] to [bus 02-04] cannot fit 0x600000000 required for 0000:02:01.0 bridging to [bus 03] pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref] to [bus 03] requires relaxed alignment rules pcieport 0000:01:00.0: Assigned bridge window [mem 0x40400000-0x406fffff] to [bus 02-04] free space at [mem 0x40400000-0x405fffff] pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref]: assigned pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]: assigned pci 0000:03:00.0: BAR 2 [mem 0x6000000000-0x63ffffffff 64bit pref]: assigned pci 0000:03:00.0: BAR 0 [mem 0x6400000000-0x6400ffffff 64bit pref]: assigned pci 0000:03:00.0: ROM [mem 0x40400000-0x405fffff pref]: assigned pci 0000:02:01.0: PCI bridge to [bus 03] pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff] pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref] This patch draws inspiration from the initial investigations and work by Mika Westerberg. Closes: https://bugzilla.kernel.org/show_bug.cgi?id=216795 Link: https://lore.kernel.org/linux-pci/20190812144144.2646-1-mika.westerberg@linux.intel.com/ Fixes: 5d0a8965aea9 ("[PATCH] 2.5.14: New PCI allocation code (alpha, arm, parisc) [2/2]") Tested-by: Lidong Wang Signed-off-by: Ilpo Järvinen Reviewed-by: Mika Westerberg --- drivers/pci/setup-bus.c | 79 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c index bca1df46f19c..11ee60b9ca71 100644 --- a/drivers/pci/setup-bus.c +++ b/drivers/pci/setup-bus.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -971,6 +972,67 @@ static inline resource_size_t calculate_mem_align(resource_size_t *aligns, return min_align; } +/** + * pbus_upstream_space_available - Check no upstream resource limits allocation + * @bus: The bus + * @mask: Mask the resource flag, then compare it with type + * @type: The type of resource from bridge + * @size: The size required from the bridge window + * @align: Required alignment for the resource + * + * Checks that @size can fit inside the upstream bridge resources that are + * already assigned. + * + * Return: %true if enough space is available on all assigned upstream + * resources. + */ +static bool pbus_upstream_space_available(struct pci_bus *bus, unsigned long mask, + unsigned long type, resource_size_t size, + resource_size_t align) +{ + struct resource_constraint constraint = { + .max = RESOURCE_SIZE_MAX, + .align = align, + }; + struct pci_bus *downstream = bus; + struct resource *r; + + while ((bus = bus->parent)) { + if (pci_is_root_bus(bus)) + break; + + pci_bus_for_each_resource(bus, r) { + if (!r || !r->parent || (r->flags & mask) != type) + continue; + + if (resource_size(r) >= size) { + struct resource gap = {}; + + if (find_resource_space(r, &gap, size, &constraint) == 0) { + gap.flags = type; + pci_dbg(bus->self, + "Assigned bridge window %pR to %pR free space at %pR\n", + r, &bus->busn_res, &gap); + return true; + } + } + + if (bus->self) { + pci_info(bus->self, + "Assigned bridge window %pR to %pR cannot fit 0x%llx required for %s bridging to %pR\n", + r, &bus->busn_res, + (unsigned long long)size, + pci_name(downstream->self), + &downstream->busn_res); + } + + return false; + } + } + + return true; +} + /** * pbus_size_mem() - Size the memory window of a given bus * @@ -997,7 +1059,7 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, struct list_head *realloc_head) { struct pci_dev *dev; - resource_size_t min_align, align, size, size0, size1; + resource_size_t min_align, win_align, align, size, size0, size1; resource_size_t aligns[24]; /* Alignments from 1MB to 8TB */ int order, max_order; struct resource *b_res = find_bus_resource_of_type(bus, @@ -1076,10 +1138,23 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, } } + win_align = window_alignment(bus, b_res->flags); min_align = calculate_mem_align(aligns, max_order); - min_align = max(min_align, window_alignment(bus, b_res->flags)); + min_align = max(min_align, win_align); size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), min_align); add_align = max(min_align, add_align); + + if (bus->self && size0 && + !pbus_upstream_space_available(bus, mask | IORESOURCE_PREFETCH, type, + size0, add_align)) { + min_align = 1ULL << (max_order + __ffs(SZ_1M)); + min_align = max(min_align, win_align); + size0 = calculate_memsize(size, min_size, 0, 0, resource_size(b_res), win_align); + add_align = win_align; + pci_info(bus->self, "bridge window %pR to %pR requires relaxed alignment rules\n", + b_res, &bus->busn_res); + } + size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 : calculate_memsize(size, min_size, add_size, children_add_size, resource_size(b_res), add_align);