From nobody Fri Sep 25 17:42:54 2026 Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4204B36606A for ; Thu, 10 Sep 2026 02:19:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789006766; cv=none; b=hyyQfNMcD8AvzfsbBxchEmvPIxfTlOqUtolPC1+cwrSKdgtuxqlZehcCzkCdcobKEtR8g/wQv1TAcDrjcCJQ5niskXMWSNLQouhdFntjhfxQiQulnFMoR1hSbLWjgs7G3ZQ9wCEdcv9wcV2VwNuFdZ3kRA1g2H5RUdC9MEk8SVg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789006766; c=relaxed/simple; bh=4ssMfydNhx0e/pAaE8tlH9aVyGRcmjwMRizcNlTI8CA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hB5T6y1Dt1Fcth8AQrMa2K7uhX4nqfYwu11U3N8eRiUuvlhapsT7xxjMb+3doSjSUHAQ7sPXmY5pYElqWWxne9BLe2atpF5cfkCdp+/Rll6FnVxVwSPp8hdQmoceVXi4N2KJv2KqR7PUKspMKj1KD86lcK0WLiMpQByYyD26Qx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com; spf=pass smtp.mailfrom=riscstar.com; dkim=pass (2048-bit key) header.d=riscstar-com.20251104.gappssmtp.com header.i=@riscstar-com.20251104.gappssmtp.com header.b=ValuYGQX; arc=none smtp.client-ip=209.85.222.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=riscstar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=riscstar-com.20251104.gappssmtp.com header.i=@riscstar-com.20251104.gappssmtp.com header.b="ValuYGQX" Received: by mail-qk1-f169.google.com with SMTP id af79cd13be357-92e85499ffbso520567885a.0 for ; Wed, 09 Sep 2026 19:19:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=riscstar-com.20251104.gappssmtp.com; s=20251104; t=1789006764; x=1789611564; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=W2pS9qh/eX0ccyCJkHhJeDU7kKBDT75Cqi6iNrcLGYE=; b=ValuYGQXfCCWanqWXroSgxt9zS97nTJOUIiv5uUOVJbLCDJTBkBzIuDcD3T7xXp3tR NiZi5l1lKWkEn654MHOFmxFHD2cVpVwcLH4IhPIG/NTZaJ6WJ92KkVUsjXYwY5dDJ8I8 Ypf0gh6VD0XWJ7LNe4ryPJ++6gu7d1WloZv8Rp790XEggsAWVVI0MsSRVI1MfmT9SRNH MqO7+W+L006dQZzDqzPxnp6UH9E1lJS00pTPhCru3w8+AzMHG+oeRBs7zDLtHCWPUgv3 WsB2Xx9CB2x8hxjQUSCtqFUeXBrh3fBsyiOZF8jPMN/pqSZeHbr7awH5sT4mBFqoTZ18 B1dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789006764; x=1789611564; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=W2pS9qh/eX0ccyCJkHhJeDU7kKBDT75Cqi6iNrcLGYE=; b=b3ZNxLAzDSLMIMNqM55WHrI+8TeMY9y6BwuT6ThM8TbFLBMrv4XazfpLQ2s9iQ3bxg xQTCxQdtwjNCMkQE9Ig6wqw04lNh+MH617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bb90 with SMTP id af79cd13be357-9398042909bmr4404190585a.24.1789006764168; Wed, 09 Sep 2026 19:19:24 -0700 (PDT) Received: from zippy.localdomain ([73.62.185.64]) by smtp.gmail.com with ESMTPSA id af79cd13be357-9397fb70fbdsm1561375585a.32.2026.09.09.19.19.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Sep 2026 19:19:23 -0700 (PDT) From: Alex Elder To: bhelgaas@google.com Cc: lizhi.hou@amd.com, herve.codina@bootlin.com, andrea.porta@suse.com, daniel@riscstar.com, mohdayaa@qti.qualcomm.com, lbiancon@qti.qualcomm.com, mani@kernel.org, robh@kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/3] PCI: of: make a flags argument optional Date: Wed, 9 Sep 2026 21:19:16 -0500 Message-ID: <20260910021919.3421449-2-elder@riscstar.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260910021919.3421449-1-elder@riscstar.com> References: <20260910021919.3421449-1-elder@riscstar.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The address of a u32 object is passed to of_pci_get_addr_flags() so it can be filled with the computed flags value. Allow a null pointer to be passed, so that the validity of the resource's flags can be checked without filling in the flags value. Signed-off-by: Alex Elder --- v2: - Moved this patch to this series drivers/pci/of_property.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c index 1e5d7dde467b8..a6c0cca986bdc 100644 --- a/drivers/pci/of_property.c +++ b/drivers/pci/of_property.c @@ -82,9 +82,11 @@ static int of_pci_get_addr_flags(const struct resource *= res, u32 *flags) else return -EINVAL; =20 - *flags =3D FIELD_PREP(OF_PCI_ADDR_FIELD_SS, ss); - if (res->flags & IORESOURCE_PREFETCH) - *flags |=3D OF_PCI_ADDR_FIELD_PREFETCH; + if (flags) { + *flags =3D FIELD_PREP(OF_PCI_ADDR_FIELD_SS, ss); + if (res->flags & IORESOURCE_PREFETCH) + *flags |=3D OF_PCI_ADDR_FIELD_PREFETCH; + } =20 return 0; } --=20 2.53.0 From nobody Fri Sep 25 17:42:54 2026 Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62D3437A85E for ; Thu, 10 Sep 2026 02:19:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789006767; cv=none; b=tdDkJ7nyfmvbUahkn3e5OP/JbhmgimpRYIWmMKOIGvj0QHYvToUGV0dBDvN+VQQ8vF388BpYs8TLRB2T/9277QvWXpjKF3DCpaJCgMFNnplHgmw24qWZ8bj8j5gyHOXMvJe154RS7EjZsFKtkEQrwk/6xPkY848Z6cNniH5OXD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789006767; c=relaxed/simple; bh=0CIVmuZgnNf8YV7SFFeCJfSGG6AnPk/hZxzAYsVAg94=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OL8nCH5eHynBj8MOl2+FOlZiOuE3HDZ/KUE8lRRXj6MxM2l+EkwwUxnLl2Kk6ijI/SCWS4boZV+AnOr5ZxGSvtht5D0k1oUpz9UUH8PKVP0eHFt1S8Qq6h12NV7D9kErkLicnhvAa4ABxDGnnJBaf8t+wabNrAqyiYtP/opwfLU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com; spf=pass smtp.mailfrom=riscstar.com; dkim=pass (2048-bit key) header.d=riscstar-com.20251104.gappssmtp.com header.i=@riscstar-com.20251104.gappssmtp.com header.b=c4bQ5Vdd; arc=none smtp.client-ip=209.85.222.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=riscstar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=riscstar-com.20251104.gappssmtp.com header.i=@riscstar-com.20251104.gappssmtp.com header.b="c4bQ5Vdd" Received: by mail-qk1-f176.google.com with SMTP id af79cd13be357-939adff838bso289210485a.0 for ; Wed, 09 Sep 2026 19:19:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=riscstar-com.20251104.gappssmtp.com; s=20251104; t=1789006765; x=1789611565; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6KPtyojyWu8c8G12DM6+VdUpBOs3905ZK16r2yXTzMY=; b=c4bQ5Vdd86XR4KEjWwEXwBRZz3sg0Fq7Ae+u/WWk9/bHtQM7N/cYj9wylH9hKuc6lQ /Oo+zC7huDlTwbykwV+gq+Iw2xr4ZZL4LeNRGH2kzJMUnX/hpa5KdshkiLgddXXOf+zQ HDlQ10X/JPyN/l9FRbrguzTTSdmtoLpocY4/ZiDGr8l0iOwO2qmKnLFpO+saerSbap66 v9NyWVlWxXFv1mfmxaF0lHZsaJQWN2KEOzjEM5bcI5KPyPZSm+byC/+dNiMhjbqWDhAX N81aP0IZW3b0yKp/In/WHadiuW51JPoKA6Hs0XL+3aCkHoayQZKd62mfjwBc5tj14/KT XvQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789006765; x=1789611565; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=6KPtyojyWu8c8G12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xx+EPOY y2UBN0kjK7gDPEw7PsWniLBCxy61NqpKTftQHZapp43pb+5KVTGIaBqFiipiHStpMJ/gFaJYzvZ 3M= X-Received: by 2002:a05:620a:298a:b0:936:623e:2603 with SMTP id af79cd13be357-93980371472mr4079820985a.17.1789006765265; Wed, 09 Sep 2026 19:19:25 -0700 (PDT) Received: from zippy.localdomain ([73.62.185.64]) by smtp.gmail.com with ESMTPSA id af79cd13be357-9397fb70fbdsm1561375585a.32.2026.09.09.19.19.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Sep 2026 19:19:24 -0700 (PDT) From: Alex Elder To: bhelgaas@google.com Cc: lizhi.hou@amd.com, herve.codina@bootlin.com, andrea.porta@suse.com, daniel@riscstar.com, mohdayaa@qti.qualcomm.com, lbiancon@qti.qualcomm.com, mani@kernel.org, robh@kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/3] PCI: of: introduce of_pci_build_prop_ranges() Date: Wed, 9 Sep 2026 21:19:17 -0500 Message-ID: <20260910021919.3421449-3-elder@riscstar.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260910021919.3421449-1-elder@riscstar.com> References: <20260910021919.3421449-1-elder@riscstar.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the bulk of what happens inside of_pci_prop_ranges() into a helper function that builds up the value to be passed to of_changeset_add_prop_u32_array(). This helper will be used in the next commit to build the ranges property for a statically-defined PCI endpoint devicetree node when it has a "pci-ep-bus" sub-node. Signed-off-by: Alex Elder --- drivers/pci/of_property.c | 129 +++++++++++++++++++++++++------------- 1 file changed, 86 insertions(+), 43 deletions(-) diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c index a6c0cca986bdc..9f30b3c09a730 100644 --- a/drivers/pci/of_property.c +++ b/drivers/pci/of_property.c @@ -102,57 +102,100 @@ static int of_pci_prop_bus_range(struct pci_dev *pde= v, ARRAY_SIZE(bus_range)); } =20 +/* + * Build a "ranges" property value that defines the mapping between + * child and parent PCI address space for each of the given PCI + * device's resources. + * + * Returns a dynamically allocated array of u32 devicetree cells, or + * a null pointer if allocation fails. The cell array is built as a + * of_pci_prop_ranges structure consisting of 8 32-bit cells in host + * byte order. It is suitable for use as the value of a PCI device + * node "ranges" property passed to of_changeset_add_prop_u32_array(). + * The total number of cells in the array is returned in *count. + * + * Caller is responsible for ensuring the returned pointer gets freed. + */ +static u32 *of_pci_build_prop_ranges(struct pci_dev *pdev, u32 *count) +{ + bool bridge_device =3D pci_is_bridge(pdev); + struct of_pci_range_entry *entries; + struct of_pci_range_entry *ep; + u32 resource_count =3D 0; + struct resource *res; + u32 first; + u32 num; + u32 i; + + if (bridge_device) { + first =3D PCI_BRIDGE_RESOURCES; + num =3D PCI_BRIDGE_RESOURCE_NUM; + } else { + first =3D PCI_STD_RESOURCES; + num =3D PCI_STD_NUM_BARS; + } + + /* First count how many resources will get a range property */ + res =3D &pdev->resource[first]; + for (i =3D 0; i < num; i++, res++) + if (resource_size(res) && !of_pci_get_addr_flags(res, NULL)) + resource_count++; + + entries =3D kzalloc_objs(*entries, resource_count); + if (!entries) + return NULL; + + ep =3D entries; + res =3D &pdev->resource[first]; + for (i =3D 0; i < num; i++, res++) { + u64 size =3D resource_size(res); + u32 flags; + + if (!size || of_pci_get_addr_flags(res, &flags)) + continue; + + /* Record the size in the range entry */ + ep->size[0] =3D upper_32_bits(size); + ep->size[1] =3D lower_32_bits(size); + + /* Record the parent bus address from the resource */ + of_pci_set_address(pdev, ep->parent_addr, + pci_bus_address(pdev, first + i), + flags, false); + + /* + * For a bridge device, the child address matches the + * parent address (including its flags cell). For an + * endpoint device, the (flags) cell contains the BAR + * number, and the two address cells are zero. + */ + if (bridge_device) + memcpy(ep->child_addr, ep->parent_addr, + sizeof(ep->child_addr)); + else + ep->child_addr[0] =3D i; + + ep++; + } + *count =3D resource_count * sizeof(*ep) / sizeof(u32); + + return (u32 *)entries; +} + static int of_pci_prop_ranges(struct pci_dev *pdev, struct of_changeset *o= cs, struct device_node *np) { struct of_pci_range_entry *rp; - struct resource *res; - int i, j, ret; - u32 flags, num; - u64 val64; + u32 *value; + u32 count; + int ret; =20 - if (pci_is_bridge(pdev)) { - num =3D PCI_BRIDGE_RESOURCE_NUM; - res =3D &pdev->resource[PCI_BRIDGE_RESOURCES]; - } else { - num =3D PCI_STD_NUM_BARS; - res =3D &pdev->resource[PCI_STD_RESOURCES]; - } - - rp =3D kzalloc_objs(*rp, num); - if (!rp) + value =3D of_pci_build_prop_ranges(pdev, &count); + if (!value) return -ENOMEM; =20 - for (i =3D 0, j =3D 0; j < num; j++) { - if (!resource_size(&res[j])) - continue; + ret =3D of_changeset_add_prop_u32_array(ocs, np, "ranges", value, count); =20 - if (of_pci_get_addr_flags(&res[j], &flags)) - continue; - - val64 =3D pci_bus_address(pdev, &res[j] - pdev->resource); - of_pci_set_address(pdev, rp[i].parent_addr, val64, flags, - false); - if (pci_is_bridge(pdev)) { - memcpy(rp[i].child_addr, rp[i].parent_addr, - sizeof(rp[i].child_addr)); - } else { - /* - * For endpoint device, the lower 64-bits of child - * address is always zero. - */ - rp[i].child_addr[0] =3D j; - } - - val64 =3D resource_size(&res[j]); - rp[i].size[0] =3D upper_32_bits(val64); - rp[i].size[1] =3D lower_32_bits(val64); - - i++; - } - - ret =3D of_changeset_add_prop_u32_array(ocs, np, "ranges", (u32 *)rp, - i * sizeof(*rp) / sizeof(u32)); kfree(rp); =20 return ret; --=20 2.53.0 From nobody Fri Sep 25 17:42:54 2026 Received: from mail-qk2-f12.google.com (mail-qk2-f12.google.com [74.125.230.204]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B8818380FC7 for ; Thu, 10 Sep 2026 02:19:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.230.204 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789006769; cv=none; b=MEcb6IOXPDtgRL56Fmcceofeto2Ix35opOh6h07VbIsm2mAtqxemFQf/81w7BM41rMYVTwRor3nR3MSJ1zb8btcM603m7kWpiJT0onpwp8P9e9q0zJ4G90wWLfWoHlo7wIUcZpJgwD9zVtHdNBV1kB/G0obYbCY1oLYHZdcAcvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789006769; c=relaxed/simple; bh=5MOq8eSAsAE1DEdUWJh2bKxyRUZPAevKcw96z8w2iCc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GpOWtT/P3lydA4YUHunM1pxRAyFVGuRbf/zMu8Oxu/PkYi1CpzLM+8okZ9WieUunRuDd4mlUB9ArmqkeR5m0yHDSEE5/k0kt9Q5/2RE2k0q82dUcKYS21MH7VWDxO4Fr42OwJbf9TcrQMcKFAU4h6A7F+MkZ9r4zgNWzFOYCsOE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com; spf=pass smtp.mailfrom=riscstar.com; dkim=pass (2048-bit key) header.d=riscstar-com.20251104.gappssmtp.com header.i=@riscstar-com.20251104.gappssmtp.com header.b=VADHFCCJ; arc=none smtp.client-ip=74.125.230.204 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=riscstar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=riscstar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=riscstar-com.20251104.gappssmtp.com header.i=@riscstar-com.20251104.gappssmtp.com header.b="VADHFCCJ" Received: by mail-qk2-f12.google.com with SMTP id af79cd13be357-93910cadeb0so177408585a.3 for ; Wed, 09 Sep 2026 19:19:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=riscstar-com.20251104.gappssmtp.com; s=20251104; t=1789006766; x=1789611566; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=1h1SxGwHKtv/DLXLoTD1RLT1c1zDSUlNRXfN66S2tYs=; b=VADHFCCJ0oMrwyAuV+gq3S3T1vEDl2068lC9d2rFTjxV00vafkCriJ0gYPqJdvDM6E QmmnW5muT5H9dfORXzw0D1tFA6EpTp733t9c4d2pmigtX2S9ixfRK7cvQSTAUdKrmEP8 SbVmWLhJ328ydZ7CkTcBv7xk4+K2xH8V9KkgLahojgkmC3r/TCle0zVlmvfYU+erW25g yzBxgarKDad2iMh992oYoZ+Y9tzLHlzamAqtcut/2CYJjZsZBJ5MeOYl+hF2igkA9bPS 8cUAG5jtJI++8k9yQPSA8ZdnqU6x8SxMskzPqtIDQpeF66m6Q56wnGKocP5R9m+dODGL w4XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789006766; x=1789611566; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=1h1SxGwHKtv/DLXLoTD1RLT1c1zDSUlNRXfN66S2tYs=; b=f0+JPFGr1I65Ha6yYE5AOkxDqt9/O7W4G7d2F03J25z87ncMx0O/y5JdUAmjL3GL8d 0xXscGQIJ8r5STMppY0/SjTOa9JTcQQmTp6ExExXmvaGhyOpvlK5M/SxoKj5OqzJTwU2 ZSeh5ARpXneIGkZHMafU7UmLw+jFGx/zJorrPVHhajzp1XoNtkM+dltEdDlXCXw1iaBU RrVoCwoNpt1PKKFNdqRhh2Cc29DJAGXQ8P9m75gnrKEoab/MwnQkCm4rYFPvY6CSPDbD BlnL6ixHLBt9YpAMmBTJcFzAbFiY6x9F3+DYUcPEF7aKmE33jxTp/n0GVhHsK9uy4uxk V6Fg== X-Forwarded-Encrypted: i=1; AKwUvByWvJ83QZ8aY7OswtkEJrDGPaR0C2AYAzhAw1XIDV5WQcEZyG+C2tnHgJei3fr7paKOq1kz9ppQgHQ3GBk=@vger.kernel.org X-Gm-Message-State: AFuF++kQUszBDpRlktY1NIVZpdBdGW+/AbxsBHwyYG7bR9rG9LvxDuF4 l9W1eIg+naWaycYWr7faAsxK0kHohLn1Jbtvxhg1PFtXzSRaSchLWYgBHh2McEQdvFA= X-Gm-Gg: AYBFou1YU3V79N4sO4LYJTwQbjRQyiUrisTVCq+ZgdSPERi76BOPMzP3eSqiJ0b7xxm HPvBJWwmWtmJQtKnQFiTAgqrRX0IUCMO9pwVADG4+watBzrdINXsT2/05+MrKAdGmoXQWXTHh6c 5SmHinphqiV6PGyMjQVFnpOx1D2o2921xbMZUSW4RdUT2EnmH+jCxrX6jbBDSiIwREgbFEbWGW+ IF/O82ZwoHwdUQ3+GKzOKkOQZtHZkrLWrwV/2UVc+xejDnqhSAZhPS6xzfn5/CGLc4q4uUgFrkm gDYbTsMEkGhCMu367JJefesIrTF1CGReHUyqZYOlcyoTW5NqhmnClwd56Mmw5e6PivbT+OD+mJD 40E5k3PHbmOZ3FzL61shmp1oSCWIg5sVmhEuHXKgxEfGI7F676qDw8BJNKdVHNUTIa1y2Hru8yr ZwcbOl7Fk1ZUcLYjNoA6sy+yzFAjVVAtV+d90J9vXgeyDIPrU+7E0nuqd9tf9aVO2XnUoG/1oQZ hQ= X-Received: by 2002:a05:620a:4089:b0:937:2e9f:70a8 with SMTP id af79cd13be357-939c94ef495mr1377322485a.39.1789006766432; Wed, 09 Sep 2026 19:19:26 -0700 (PDT) Received: from zippy.localdomain ([73.62.185.64]) by smtp.gmail.com with ESMTPSA id af79cd13be357-9397fb70fbdsm1561375585a.32.2026.09.09.19.19.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Sep 2026 19:19:26 -0700 (PDT) From: Alex Elder To: bhelgaas@google.com Cc: lizhi.hou@amd.com, herve.codina@bootlin.com, andrea.porta@suse.com, daniel@riscstar.com, mohdayaa@qti.qualcomm.com, lbiancon@qti.qualcomm.com, mani@kernel.org, robh@kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/3] PCI: of: introduce of_pci_update_endpoint_node_ranges() Date: Wed, 9 Sep 2026 21:19:18 -0500 Message-ID: <20260910021919.3421449-4-elder@riscstar.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260910021919.3421449-1-elder@riscstar.com> References: <20260910021919.3421449-1-elder@riscstar.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit 407d1a51921e9 ("PCI: Create device tree node for bridge") introduced the PCI_DYNAMIC_OF_NODES Kconfig option, which creates a devicetree node for a PCI bridge as part of pci_bus_add_device(). Its successor commit ae9813db1dc5a ("PCI: Add quirks to generate device tree node for Xilinx Alveo U50") shows how to use a PCI final fixup quirk to also create a devicetree node for a non-bridge PCI device. In both cases, of_pci_make_dev_node() uses an OF changeset to dynamically create a node populated with appropriate properties and apply it to the live devicetree. The dynamic devicetree node for a PCI device will include a "ranges" property, and a new type of 3-cell address is introduced for use within an endpoint. The endpoint's ranges property will contain a range entry for each of the endpoint's BARs. The "child address" portion of each range will use the BAR number in the "flags" (first) cell in the address. This allows addresses within the endpoint to be expressed relative to whatever address gets assigned to each BAR. Unfortunately, if a PCI endpoint device had a devicetree node set up statically, its "ranges" property (if present) will be static, and it cannot contain the addresses assigned to the endpoint's BARs during enumeration. This means that the "BAR number" based addressing scheme doesn't work for PCI endpoints whose devicetree nodes are created statically. To remedy this, modify of_pci_make_dev_node() to dynamically create a "ranges" property just as is done when the endpoint has a null devicetree node pointer. The device node will be updated to add the new "ranges" property (or replace it if one exists). This allows "BAR number" addresses to work correctly even when the endpoint's devicetree node is created statically. Signed-off-by: Alex Elder --- v2: - Only update ranges property if there is a pci-ep-bus node drivers/pci/of.c | 89 +++++++++++++++++++++++++++++++++++---- drivers/pci/of_property.c | 2 +- drivers/pci/pci.h | 1 + 3 files changed, 83 insertions(+), 9 deletions(-) diff --git a/drivers/pci/of.c b/drivers/pci/of.c index 5a040ed836744..d9c215a3bfae5 100644 --- a/drivers/pci/of.c +++ b/drivers/pci/of.c @@ -742,20 +742,93 @@ void of_pci_remove_node(struct pci_dev *pdev) of_node_put(np); } =20 +/* Returns true if the ranges property was added or updated successfully */ +static bool of_pci_update_endpoint_node_ranges(struct pci_dev *pdev) +{ + struct device_node *np =3D pci_device_to_OF_node(pdev); + struct property *prop; + u32 *value; + u32 size; + + prop =3D kzalloc_obj(*prop); + if (!prop) + return false; + + value =3D of_pci_build_prop_ranges(pdev, &size); + if (!value) { + kfree(prop); + return false; + } + + prop->name =3D "ranges"; + prop->length =3D size * sizeof(u32); + prop->value =3D value; + + /* The property value needs to be in big-endian byte order */ + while (size--) + cpu_to_be32s(value++); + + /* of_update_property() consumes the allocated property */ + of_update_property(np, prop); + + return true; +} + +/* + * Create a devicetree node for a PCI device. If the device is a bridge + * and it already has a devicetree node, there's nothing further to do. + * If it is a bridge without an existing devicetree node, one is created + * dynamically. + * + * This function can also be called (via PCI quirk) for a PCI endpoint + * (function) that implements a PCI endpoint bus. As with a PCI bridge, + * if the endpoint has no existing devicetree node, one is created + * dynamically. The node will include a ranges property that maps + * BAR-relative addresses in the child to the PCI address ranges + * assigned to the PCI endpoint BARs. + * + * If an endpoint already has a devicetree node, and it includes a + * "pci-ep-bus" sub-node, its ranges property must still be dynamically + * populated so that it can take into account the BAR ranges assigned + * during PCI enumeration. + */ void of_pci_make_dev_node(struct pci_dev *pdev) { - struct device_node *ppnode, *np =3D NULL; + struct device_node *np =3D pci_device_to_OF_node(pdev); + struct device *dev =3D &pdev->dev; + struct device_node *ppnode; + struct of_changeset *cset; const char *pci_type; - struct of_changeset *cset; const char *name; int ret; =20 - /* - * If there is already a device tree node linked to this device, - * return immediately. - */ - if (pci_device_to_OF_node(pdev)) + /* See if the PCI device already has a devicetree node */ + if (np) { + struct device_node *child; + + /* Nothing further needed for a bridge */ + if (pci_is_bridge(pdev)) + return; + + /* + * We only update the ranges property if the endpoint's + * devicetree node includes a "pci-ep-bus" sub-node. + */ + child =3D of_get_child_by_name(np, "pci-ep-bus"); + if (!child) + return; + of_node_put(child); + + /* + * Update the ranges property, defining an entry for each + * BAR, mapping BAR offsets to the PCI bus address based + * on the BAR's assigned range. + */ + if (!of_pci_update_endpoint_node_ranges(pdev)) + dev_err(dev, "failed to update ranges property\n"); + return; + } =20 /* Check if there is device tree node for parent device */ if (!pdev->bus->self) @@ -794,7 +867,7 @@ void of_pci_make_dev_node(struct pci_dev *pdev) =20 np->data =3D cset; =20 - ret =3D device_add_of_node(&pdev->dev, np); + ret =3D device_add_of_node(dev, np); if (ret) goto out_revert_cset; =20 diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c index 9f30b3c09a730..8e1548c4aac3c 100644 --- a/drivers/pci/of_property.c +++ b/drivers/pci/of_property.c @@ -116,7 +116,7 @@ static int of_pci_prop_bus_range(struct pci_dev *pdev, * * Caller is responsible for ensuring the returned pointer gets freed. */ -static u32 *of_pci_build_prop_ranges(struct pci_dev *pdev, u32 *count) +u32 *of_pci_build_prop_ranges(struct pci_dev *pdev, u32 *count) { bool bridge_device =3D pci_is_bridge(pdev); struct of_pci_range_entry *entries; diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 2e33d3bd4b0ba..1461e52777532 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -1318,6 +1318,7 @@ struct of_changeset; #ifdef CONFIG_PCI_DYNAMIC_OF_NODES void of_pci_make_dev_node(struct pci_dev *pdev); void of_pci_remove_node(struct pci_dev *pdev); +u32 *of_pci_build_prop_ranges(struct pci_dev *pdev, u32 *count); int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs, struct device_node *np); void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge); --=20 2.53.0