From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 BCE4E1459F9 for ; Thu, 13 Jun 2024 11:50:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279453; cv=none; b=l20jZoYnA1zHM5RYhEdRn3eu44NZlKv/68o4c6J0dEEmuSq9T93Z3o/c6eSMQCUhJV37PxKVh+qD8qrB/Ogjk3U8TUj9hn36HSQ/W0Drj8l4pK2ox41YVESs+J7kdbDTQUEWWwt4fhgCaU/7IwtgQ/AR4xG6DIQxyJEv44tomRc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279453; c=relaxed/simple; bh=uxWR+cyvQnS6F+Ku7i7pYs3ZA+OagBoxvbut16jkiT8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YkAVxICPRWt6IK1Evg/zIWRsaKZnx8EjJrc7yq1ZljntYrINNz/FgSu5G27xgm+VelXHKTmartwOj5gwZCTL2Xg1lsGaMb8Bs9VnD1yluUF7RKyMgLnzCdr4MUPIz5vum7C4WUn0cMAr8qXHPae5FinlBWyoP0utdKnb4oxC+fQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=NX52g99H; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="NX52g99H" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279450; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Oyizp71aDVZTVSj1hEgGPWD4Zgvxaje3MhAhMHAAQDo=; b=NX52g99Hk0H0FIKhUpXswNHRiDDSQFzCZznmq3WOB0NESB9BP2bjegGvvi9aGAzPJsRgKh P5aCCaqKopxc7Q5R/Mj5Y00V5+66P8Yd//Vr16TNkdhhgIVbwYBkG6C6/lHb1Etg0cFKSY 6/duLjD8iu97vMYwcjby8uV2x1vnftw= Received: from mail-lj1-f200.google.com (mail-lj1-f200.google.com [209.85.208.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-628-j_XODUmmO46GQOVy368ySQ-1; Thu, 13 Jun 2024 07:50:47 -0400 X-MC-Unique: j_XODUmmO46GQOVy368ySQ-1 Received: by mail-lj1-f200.google.com with SMTP id 38308e7fff4ca-2ebf1470cbaso442981fa.1 for ; Thu, 13 Jun 2024 04:50:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279446; x=1718884246; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Oyizp71aDVZTVSj1hEgGPWD4Zgvxaje3MhAhMHAAQDo=; b=ark1gKI/P+fi8GTawVopjZsffm/1oEmyOblS7jAcXv0wJJXtth26u/FX2+IyqHJjqa 5fRUrST46gO4R+GTLlQwBJWZh+1zOUMoDTqj/FDldNUw69qms2sFWGU3Xvm2qHpJNAO7 QLuqTix+jd9Zx65FmvLfBhrFO0lGM2lEwZFZQ9D8VewABkkfNGSkawvPi2e5OSXcoPhG O2O82kapilo6Z0o0uyIC72NT4509xOriNskTqwqs4Hp1lK+fa22R5lUgUJgUsPKXS8I5 kDTzG+iV2osXXidjoyCO0ppmyC/AEBihW7TuDRpL9XABH08uR0acZrqtxdmu6HJKwBEn pJ9Q== X-Forwarded-Encrypted: i=1; AJvYcCW9vRwI41GU3i6bNdj28oDcw+/nX5BFTmfd5cxac4nSVXWHCaiNs/3s8lpseWWGELucxSFfsuUgsT0pjJVwzxJ3VTxDxD2/4sCHyYum X-Gm-Message-State: AOJu0YxMVsp7IQwDFfr0ke/P2Scc24w5F/NGxVzzbRPn9/uSg2Ztudn5 a7mHwhgixWxjQcWx68prMS2io958wx7VfvK8DRmU2c0gvFhZMD8xpyJOfA7PN9giY5uGqt0sxwE vMKNdBtpF8DpBmEITmtGys/ETfrWoqsLjp+0Tkr3C6N+5a8XpGnQ0pB6xMf7Xfw== X-Received: by 2002:ac2:4db6:0:b0:52b:b349:c224 with SMTP id 2adb3069b0e04-52ca59e8a34mr41630e87.0.1718279446136; Thu, 13 Jun 2024 04:50:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLhEgk5PVdiMFlFsN3ltE7pAoZnfjyVgZ6hFXz98dULDmP/AUIKfgL84nHKqB6KbULbimrYw== X-Received: by 2002:ac2:4db6:0:b0:52b:b349:c224 with SMTP id 2adb3069b0e04-52ca59e8a34mr41612e87.0.1718279445706; Thu, 13 Jun 2024 04:50:45 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:45 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 01/13] PCI: Add and use devres helper for bit masks Date: Thu, 13 Jun 2024 13:50:14 +0200 Message-ID: <20240613115032.29098-2-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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 current derves implementation uses manual shift operations to check whether a bit in a mask is set. The code can be made more readable by writing a small helper function for that. Implement mask_contains_bar() and use it where applicable. Link: https://lore.kernel.org/r/20240605081605.18769-3-pstanner@redhat.com Signed-off-by: Philipp Stanner Signed-off-by: Bjorn Helgaas --- drivers/pci/devres.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 2c562b9eaf80..f13edd4a3873 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -161,6 +161,10 @@ int pcim_set_mwi(struct pci_dev *dev) } EXPORT_SYMBOL(pcim_set_mwi); =20 +static inline bool mask_contains_bar(int mask, int bar) +{ + return mask & BIT(bar); +} =20 static void pcim_release(struct device *gendev, void *res) { @@ -169,7 +173,7 @@ static void pcim_release(struct device *gendev, void *r= es) int i; =20 for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) - if (this->region_mask & (1 << i)) + if (mask_contains_bar(this->region_mask, i)) pci_release_region(dev, i); =20 if (this->mwi) @@ -363,7 +367,7 @@ int pcim_iomap_regions(struct pci_dev *pdev, int mask, = const char *name) for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) { unsigned long len; =20 - if (!(mask & (1 << i))) + if (!mask_contains_bar(mask, i)) continue; =20 rc =3D -EINVAL; @@ -386,7 +390,7 @@ int pcim_iomap_regions(struct pci_dev *pdev, int mask, = const char *name) pci_release_region(pdev, i); err_inval: while (--i >=3D 0) { - if (!(mask & (1 << i))) + if (!mask_contains_bar(mask, i)) continue; pcim_iounmap(pdev, iomap[i]); pci_release_region(pdev, i); @@ -438,7 +442,7 @@ void pcim_iounmap_regions(struct pci_dev *pdev, int mas= k) return; =20 for (i =3D 0; i < PCIM_IOMAP_MAX; i++) { - if (!(mask & (1 << i))) + if (!mask_contains_bar(mask, i)) continue; =20 pcim_iounmap(pdev, iomap[i]); --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 A97AA1459EA for ; Thu, 13 Jun 2024 11:50:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279452; cv=none; b=EwZL59wA8UZBQL82hLcE6S3OeUm0P3dH3ppU/qMh73Px2q+YHTP5a3HRs7puWDHBp0wfEeZW8j9ld9nK7MbY3lIAwlupOYmLGhkWDR/4Ky7lRGFL4umO9SB83TnolZ+aT4RDPW9z/EU+AtNFiMKS8KHdqh7K5hSYHZ37hEa67ug= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279452; c=relaxed/simple; bh=TgkR4Nl7UsfcnDus5PAQ/eglmpUMQS/oRcoljkpQuXI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sCQzTVxMUl90jPhAQT3gImeD6PNza2LvfnUrjbNm+px6vEgnViHojQgVXxIt5Oi3LF1kmYM3fNLES37quL4eihuEMz1ZgWsahO7MIoLYEolGqmpm+wTs8vyJPbz4y5qjf1YklZIWZg1Wt5DFB1sAy6TcbzhVXYkNCo4rvEnaY1A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZdNVIF+P; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZdNVIF+P" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279449; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qTRnfk+xncNuJMn54sP5qzAQh4gcZ4Sy3yYao6iQeYc=; b=ZdNVIF+PjrWZ/q07uXOujXwgtW0eY7cw6hVfZIdnysC0EaqqYSIEoJedkVg7+fqW94ST4d 5QmBPkrUcdVCIH6xRHgckUUJlQfjpJxSLNlUi7UWI6Z+x1gtJv+ACUh4UW5PqcCI98Uh2t 0IbIBe5jMhO8FWogNOMkOPUWBMCz/2g= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-682-MylTMsg5PImQKntpQi-FCw-1; Thu, 13 Jun 2024 07:50:48 -0400 X-MC-Unique: MylTMsg5PImQKntpQi-FCw-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-42183fdb22bso1602315e9.1 for ; Thu, 13 Jun 2024 04:50:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279447; x=1718884247; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qTRnfk+xncNuJMn54sP5qzAQh4gcZ4Sy3yYao6iQeYc=; b=U6d9pgxkyIRkW5FYFFYx5dlEPG1WNBpK1ofHvRHOAQcM4wvNMZK+g3qv4nPX/6ijcr gWQXlsk/6/t7uIrUcvJV8SdmCIJ6jsEAWMR6K3mODb8czTdSoB6/gXHO2kkGwWT+PjQn 1Mc5Dnj1o6n4IDLN0gyf+5PTepCM/zrfOU60lG9L6ak3+2bT0FGiTg+TCZNUrpbpBkdi 7x5UMMs5Erpybk+8y4efqj7bQYGzprPdlYdGutHmBehlR9rV2w1bjpGKn0hLrqVm8pQq cLYzhfZEbY9wAXZEOAgohHutdbStpnCTqQWInur6JZKXSQHUfPsO/8521dW6F3bQ2Rt1 Hltg== X-Forwarded-Encrypted: i=1; AJvYcCXvkfVWwampbRyvAoaxVFM7ok5E7U3r8yV6kHG8Tc/umfzQz6dqzu0GGnUz1OBG69i0kfRdzNpPz/Ww4BkJMh3T0dFar+wDrID3RErk X-Gm-Message-State: AOJu0YyJPx5GFGW20TdXJSYX7qvZ8ZYIOASKvBHoeC3rNlql0Lz8HB+d AB2kuFpdxnbHuBVr8cHP7LgXlYdXfF8gDTfTRffFqEn+/yhdiZBEf+S7xLvogyI7tzeqLx1jt7b oczOlCwsekCRpjv9N3Eb1pNhi8ePKp3PEMnNhwkUJhxH4gP9ZxHqiCkpu9SU8gw== X-Received: by 2002:a05:600c:350b:b0:421:74d4:f32c with SMTP id 5b1f17b1804b1-423041e1d4cmr1371105e9.1.1718279447061; Thu, 13 Jun 2024 04:50:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHv8ZBvdV4IZRRiIgJbZsBfiAJdHKRwnxvdFK5xMqJJnZPKz428Y6zIL/fRH5SPX2HOkbTO0A== X-Received: by 2002:a05:600c:350b:b0:421:74d4:f32c with SMTP id 5b1f17b1804b1-423041e1d4cmr1370945e9.1.1718279446627; Thu, 13 Jun 2024 04:50:46 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:46 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 02/13] PCI: Add devres helpers for iomap table Date: Thu, 13 Jun 2024 13:50:15 +0200 Message-ID: <20240613115032.29098-3-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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 pcim_iomap_devres.table administrated by pcim_iomap_table() has its entries set and unset at several places throughout devres.c using manual iterations which are effectively code duplications. Add pcim_add_mapping_to_legacy_table() and pcim_remove_mapping_from_legacy_table() helper functions and use them where possible. Link: https://lore.kernel.org/r/20240605081605.18769-4-pstanner@redhat.com Signed-off-by: Philipp Stanner [bhelgaas: s/short bar/int bar/ for consistency] Signed-off-by: Bjorn Helgaas --- drivers/pci/devres.c | 77 +++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index f13edd4a3873..845d6fab0ce7 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -297,6 +297,52 @@ void __iomem * const *pcim_iomap_table(struct pci_dev = *pdev) } EXPORT_SYMBOL(pcim_iomap_table); =20 +/* + * Fill the legacy mapping-table, so that drivers using the old API can + * still get a BAR's mapping address through pcim_iomap_table(). + */ +static int pcim_add_mapping_to_legacy_table(struct pci_dev *pdev, + void __iomem *mapping, int bar) +{ + void __iomem **legacy_iomap_table; + + if (bar >=3D PCI_STD_NUM_BARS) + return -EINVAL; + + legacy_iomap_table =3D (void __iomem **)pcim_iomap_table(pdev); + if (!legacy_iomap_table) + return -ENOMEM; + + /* The legacy mechanism doesn't allow for duplicate mappings. */ + WARN_ON(legacy_iomap_table[bar]); + + legacy_iomap_table[bar] =3D mapping; + + return 0; +} + +/* + * Remove a mapping. The table only contains whole-BAR mappings, so this w= ill + * never interfere with ranged mappings. + */ +static void pcim_remove_mapping_from_legacy_table(struct pci_dev *pdev, + void __iomem *addr) +{ + int bar; + void __iomem **legacy_iomap_table; + + legacy_iomap_table =3D (void __iomem **)pcim_iomap_table(pdev); + if (!legacy_iomap_table) + return; + + for (bar =3D 0; bar < PCI_STD_NUM_BARS; bar++) { + if (legacy_iomap_table[bar] =3D=3D addr) { + legacy_iomap_table[bar] =3D NULL; + return; + } + } +} + /** * pcim_iomap - Managed pcim_iomap() * @pdev: PCI device to iomap for @@ -308,16 +354,20 @@ EXPORT_SYMBOL(pcim_iomap_table); */ void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxl= en) { - void __iomem **tbl; + void __iomem *mapping; =20 - BUG_ON(bar >=3D PCIM_IOMAP_MAX); - - tbl =3D (void __iomem **)pcim_iomap_table(pdev); - if (!tbl || tbl[bar]) /* duplicate mappings not allowed */ + mapping =3D pci_iomap(pdev, bar, maxlen); + if (!mapping) return NULL; =20 - tbl[bar] =3D pci_iomap(pdev, bar, maxlen); - return tbl[bar]; + if (pcim_add_mapping_to_legacy_table(pdev, mapping, bar) !=3D 0) + goto err_table; + + return mapping; + +err_table: + pci_iounmap(pdev, mapping); + return NULL; } EXPORT_SYMBOL(pcim_iomap); =20 @@ -330,20 +380,9 @@ EXPORT_SYMBOL(pcim_iomap); */ void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) { - void __iomem **tbl; - int i; - pci_iounmap(pdev, addr); =20 - tbl =3D (void __iomem **)pcim_iomap_table(pdev); - BUG_ON(!tbl); - - for (i =3D 0; i < PCIM_IOMAP_MAX; i++) - if (tbl[i] =3D=3D addr) { - tbl[i] =3D NULL; - return; - } - WARN_ON(1); + pcim_remove_mapping_from_legacy_table(pdev, addr); } EXPORT_SYMBOL(pcim_iounmap); =20 --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 10E971474B7 for ; Thu, 13 Jun 2024 11:50:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279455; cv=none; b=gXnQaWSILMlbbEqx5reZXtjbIJ/BmOiDD3ssj/eTR7aewDVhdpSgSv2XTPU1Pvs3/J/igG5I6J744n8wY0JRgtNUXc4T47uOK3uKq5vVLYojt872POe7g/OxS+iRs7cjKUap4oUOMP/KGDdqtCcoNXlfhhZE9sZjYhbTrdCD6DI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279455; c=relaxed/simple; bh=uWUzFsdvpSHc64lHKkPsUVgfWCCMYvJqUAk/DiCtcSs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=t3zoG2O2x6UGoSbR1AEsMg2eieIe3n1Fp2IyWnlmAWzBaJuLpwoNvrlLDwqkV+yJUSq3qVH3T7lSMvTF0pJqOWdtH59TtWz4pArYY2M6U4clJrfBFg5nLi7SrpxtjsC33W4vTi4lERa9pLgtYB5/8xnaoMlEJSORmTlJL45fLSc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=N47oFeH1; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="N47oFeH1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279451; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=owM7d9MOt/jRqhqh2hkOR6CRgSB25b9Tr8ToAYjipS8=; b=N47oFeH1BChznm1zV8b8bLKZoJGnx5MxftjyDSCf5bsNPjRm5GcN6L2fg97oNXZWa4aR4W zvyu3X4dXhlvFUdU13jS40FCjUM0ApB4S/ItM5DzXlBQP3nD3+c4HeudKXlS46vm+Ei2yO XgOqYaLc5kxwNVoaNI8SnFqb2wSiOKQ= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-690-n4sfoac4PRa5wHC0SJofSQ-1; Thu, 13 Jun 2024 07:50:49 -0400 X-MC-Unique: n4sfoac4PRa5wHC0SJofSQ-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-35f1ccdbf82so92442f8f.1 for ; Thu, 13 Jun 2024 04:50:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279448; x=1718884248; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=owM7d9MOt/jRqhqh2hkOR6CRgSB25b9Tr8ToAYjipS8=; b=ib6iAv0wCqPXlEBdNHby6WT5DBYSxRVETVBzvzBF57cyrRfkIuM/z+4R8MU+8ZiSeh 7olPCBrtFylWUnAMmqlSldn10UgBF4uOd6Z7UZuHGiw6QbvBgt4dKDZ8tnx5xufyFo+Z cnN8ArIgzobQhmGag1E91E4i3Ci/IHk4jZ/XrJfY7ksr2kCP6hDUMaivw+yjzh4b9zxM nVpVvDn6/dQ1nXD1IBs3+ZNvUyxFkoBW8rokp8VImiVMPO5HPyzXLR6fdMRw+Botzwni x/EM/3TgsR2I69v8vwFE6qdmqZ1zUJU5JWR50s2aBsdMFf03jID0Axuui9ZNPFapGLdF 9d+Q== X-Forwarded-Encrypted: i=1; AJvYcCULRjIOoD+px22FrfbZxw0jihVo/KpdiqbAaoKPaWnly6vRlShF8LPckVHC6IN97qoS9GsFTFuF9Qx+VxEsfrH73l6LWxpasK4sVjrI X-Gm-Message-State: AOJu0YzEsRYDBKv8r8rm2+OPPYsao9yvQkEsSvQEY0ShEL6wAh4j2jWJ 81R7N7Gv9afbgf8m8B5vS4ezrYuA5YvckHqa458xeq/2tvQpa1WTP5FyvgMOsbB2AHkeNTxEs33 5e+ecwEj/K+/6HqgQQCB9qOidf9tvYp6zuR55ekxJdo/MDX63UOGZ5YiUK69a0A== X-Received: by 2002:a05:6000:1565:b0:35f:306f:1580 with SMTP id ffacd0b85a97d-360799b1549mr219954f8f.0.1718279448134; Thu, 13 Jun 2024 04:50:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE9cmmBRW9Stdsi/0+iK7+da7RrjIrMU5kxwu1c5epv9+cZ27afQqEVK8UIzXm69jHmn71+7Q== X-Received: by 2002:a05:6000:1565:b0:35f:306f:1580 with SMTP id ffacd0b85a97d-360799b1549mr219936f8f.0.1718279447676; Thu, 13 Jun 2024 04:50:47 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:47 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 03/13] PCI: Add partial-BAR devres support Date: Thu, 13 Jun 2024 13:50:16 +0200 Message-ID: <20240613115032.29098-4-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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" With the current PCI devres API implementing a managed version of pci_iomap_range() is impossible. Furthermore, the PCI devres API currently is inconsistent and complicated. This is in large part due to the fact that there are hybrid functions which are only sometimes managed via devres, and functions IO-mapping and requesting several BARs at once and returning mappings through a separately administrated table. This table's indexing mechanism does not support partial-BAR mappings. Another notable problem is that there are no separate managed counterparts for region-request functions such as pci_request_region(), as they exist for other PCI functions (e.g., pci_iomap() <-> pcim_iomap()). Instead, functions based on __pci_request_region() change their internal behavior and suddenly become managed functions when pcim_enable_device() instead of pci_enable_device() is used. This API is hard to understand and potentially bug-provoking. Hence, it should be made more consistent. This patch adds the necessary infrastructure for partial-BAR mappings managed with devres. That infrastructure also serves as a ground layer for significantly simplifying the PCI devres API in subsequent patches which can then cleanly separate managed and unmanaged API. When having the long term goal of providing always managed functions prefixed with "pcim_" and never managed functions prefixed with "pci_" and, thus, separating managed and unmanaged APIs cleanly, new PCI devres infrastructure cannot use __pci_request_region() and its wrappers since those would then again interact with PCI devres and, consequently, prevent the managed nature from being removed from the pci_* functions in the first place. Thus, it's necessary to provide an alternative to __pci_request_region(). This patch addresses the following problems of the PCI devres API: a) There is no PCI devres infrastructure on which a managed counter part to pci_iomap_range() could be based on. b) The vast majority of the users of plural functions such as pcim_iomap_regions() only ever sets a single bit in the bit mask, consequently making them singular functions anyways. c) region-request functions being sometimes managed and sometimes not is bug-provoking. pcim_* functions should always be managed, pci_* functions never. Add a new PCI device resource, pcim_addr_devres, that serves to encapsulate all device resource types related to region requests and IO-mappings since those are very frequently created together. Add a set of alternatives cleanly separated from the hybrid mechanism in __pci_request_region() and its respective wrappers: - __pcim_request_region_range() - __pcim_release_region_range() - __pcim_request_region() - __pcim_release_region() Add the following PCI-internal devres functions based on the above: - pcim_iomap_region() - pcim_iounmap_region() - _pcim_request_region() - pcim_request_region() - pcim_release_region() - pcim_request_all_regions() - pcim_release_all_regions() Add new needed helper pcim_remove_bar_from_legacy_table(). Rework the following public interfaces using the new infrastructure listed above: - pcim_iomap_release() - pcim_iomap() - pcim_iounmap() - pcim_iomap_regions() - pcim_iomap_regions_request_all() - pcim_iounmap_regions() Update API documentation. Link: https://lore.kernel.org/r/20240605081605.18769-5-pstanner@redhat.com Signed-off-by: Philipp Stanner Signed-off-by: Bjorn Helgaas --- drivers/pci/devres.c | 608 ++++++++++++++++++++++++++++++++++++++----- drivers/pci/pci.c | 22 ++ drivers/pci/pci.h | 5 + 3 files changed, 568 insertions(+), 67 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 845d6fab0ce7..cf2c11b54ca6 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -4,14 +4,243 @@ #include "pci.h" =20 /* - * PCI iomap devres + * On the state of PCI's devres implementation: + * + * The older devres API for PCI has two significant problems: + * + * 1. It is very strongly tied to the statically allocated mapping table in + * struct pcim_iomap_devres below. This is mostly solved in the sense o= f the + * pcim_ functions in this file providing things like ranged mapping by + * bypassing this table, wheras the functions that were present in the = old + * API still enter the mapping addresses into the table for users of th= e old + * API. + * + * 2. The region-request-functions in pci.c do become managed IF the devic= e has + * been enabled with pcim_enable_device() instead of pci_enable_device(= ). + * This resulted in the API becoming inconsistent: Some functions have = an + * obviously managed counter-part (e.g., pci_iomap() <-> pcim_iomap()), + * whereas some don't and are never managed, while others don't and are + * _sometimes_ managed (e.g. pci_request_region()). + * + * Consequently, in the new API, region requests performed by the pcim_ + * functions are automatically cleaned up through the devres callback + * pcim_addr_resource_release(), while requests performed by + * pcim_enable_device() + pci_*region*() are automatically cleaned up + * through the for-loop in pcim_release(). + * + * TODO 1: + * Remove the legacy table entirely once all calls to pcim_iomap_table() in + * the kernel have been removed. + * + * TODO 2: + * Port everyone calling pcim_enable_device() + pci_*region*() to using the + * pcim_ functions. Then, remove all devres functionality from pci_*region= *() + * functions and remove the associated cleanups described above in point #= 2. */ -#define PCIM_IOMAP_MAX PCI_STD_NUM_BARS =20 +/* + * Legacy struct storing addresses to whole mapped BARs. + */ struct pcim_iomap_devres { - void __iomem *table[PCIM_IOMAP_MAX]; + void __iomem *table[PCI_STD_NUM_BARS]; +}; + +enum pcim_addr_devres_type { + /* Default initializer. */ + PCIM_ADDR_DEVRES_TYPE_INVALID, + + /* A requested region spanning an entire BAR. */ + PCIM_ADDR_DEVRES_TYPE_REGION, + + /* + * A requested region spanning an entire BAR, and a mapping for + * the entire BAR. + */ + PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING, + + /* + * A mapping within a BAR, either spanning the whole BAR or just a + * range. Without a requested region. + */ + PCIM_ADDR_DEVRES_TYPE_MAPPING, }; =20 +/* + * This struct envelops IO or MEM addresses, i.e., mappings and region + * requests, because those are very frequently requested and released + * together. + */ +struct pcim_addr_devres { + enum pcim_addr_devres_type type; + void __iomem *baseaddr; + unsigned long offset; + unsigned long len; + short bar; +}; + +static inline void pcim_addr_devres_clear(struct pcim_addr_devres *res) +{ + memset(res, 0, sizeof(*res)); + res->bar =3D -1; +} + +/* + * The following functions, __pcim_*_region*, exist as counterparts to the + * versions from pci.c - which, unfortunately, can be in "hybrid mode", i.= e., + * sometimes managed, sometimes not. + * + * To separate the APIs cleanly, we define our own, simplified versions he= re. + */ + +/** + * __pcim_request_region_range - Request a ranged region + * @pdev: PCI device the region belongs to + * @bar: BAR the range is within + * @offset: offset from the BAR's start address + * @maxlen: length in bytes, beginning at @offset + * @name: name associated with the request + * @req_flags: flags for the request, e.g., for kernel-exclusive requests + * + * Returns: 0 on success, a negative error code on failure. + * + * Request a range within a device's PCI BAR. Sanity check the input. + */ +static int __pcim_request_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long maxlen, + const char *name, int req_flags) +{ + resource_size_t start =3D pci_resource_start(pdev, bar); + resource_size_t len =3D pci_resource_len(pdev, bar); + unsigned long dev_flags =3D pci_resource_flags(pdev, bar); + + if (start =3D=3D 0 || len =3D=3D 0) /* Unused BAR. */ + return 0; + if (len <=3D offset) + return -EINVAL; + + start +=3D offset; + len -=3D offset; + + if (len > maxlen && maxlen !=3D 0) + len =3D maxlen; + + if (dev_flags & IORESOURCE_IO) { + if (!request_region(start, len, name)) + return -EBUSY; + } else if (dev_flags & IORESOURCE_MEM) { + if (!__request_mem_region(start, len, name, req_flags)) + return -EBUSY; + } else { + /* That's not a device we can request anything on. */ + return -ENODEV; + } + + return 0; +} + +static void __pcim_release_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long maxlen) +{ + resource_size_t start =3D pci_resource_start(pdev, bar); + resource_size_t len =3D pci_resource_len(pdev, bar); + unsigned long flags =3D pci_resource_flags(pdev, bar); + + if (len <=3D offset || start =3D=3D 0) + return; + + if (len =3D=3D 0 || maxlen =3D=3D 0) /* This an unused BAR. Do nothing. */ + return; + + start +=3D offset; + len -=3D offset; + + if (len > maxlen) + len =3D maxlen; + + if (flags & IORESOURCE_IO) + release_region(start, len); + else if (flags & IORESOURCE_MEM) + release_mem_region(start, len); +} + +static int __pcim_request_region(struct pci_dev *pdev, int bar, + const char *name, int flags) +{ + unsigned long offset =3D 0; + unsigned long len =3D pci_resource_len(pdev, bar); + + return __pcim_request_region_range(pdev, bar, offset, len, name, flags); +} + +static void __pcim_release_region(struct pci_dev *pdev, int bar) +{ + unsigned long offset =3D 0; + unsigned long len =3D pci_resource_len(pdev, bar); + + __pcim_release_region_range(pdev, bar, offset, len); +} + +static void pcim_addr_resource_release(struct device *dev, void *resource_= raw) +{ + struct pci_dev *pdev =3D to_pci_dev(dev); + struct pcim_addr_devres *res =3D resource_raw; + + switch (res->type) { + case PCIM_ADDR_DEVRES_TYPE_REGION: + __pcim_release_region(pdev, res->bar); + break; + case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING: + pci_iounmap(pdev, res->baseaddr); + __pcim_release_region(pdev, res->bar); + break; + case PCIM_ADDR_DEVRES_TYPE_MAPPING: + pci_iounmap(pdev, res->baseaddr); + break; + default: + break; + } +} + +static struct pcim_addr_devres *pcim_addr_devres_alloc(struct pci_dev *pde= v) +{ + struct pcim_addr_devres *res; + + res =3D devres_alloc_node(pcim_addr_resource_release, sizeof(*res), + GFP_KERNEL, dev_to_node(&pdev->dev)); + if (res) + pcim_addr_devres_clear(res); + return res; +} + +/* Just for consistency and readability. */ +static inline void pcim_addr_devres_free(struct pcim_addr_devres *res) +{ + devres_free(res); +} + +/* + * Used by devres to identify a pcim_addr_devres. + */ +static int pcim_addr_resources_match(struct device *dev, void *a_raw, void= *b_raw) +{ + struct pcim_addr_devres *a, *b; + + a =3D a_raw; + b =3D b_raw; + + if (a->type !=3D b->type) + return 0; + + switch (a->type) { + case PCIM_ADDR_DEVRES_TYPE_REGION: + case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING: + return a->bar =3D=3D b->bar; + case PCIM_ADDR_DEVRES_TYPE_MAPPING: + return a->baseaddr =3D=3D b->baseaddr; + default: + return 0; + } +} =20 static void devm_pci_unmap_iospace(struct device *dev, void *ptr) { @@ -92,8 +321,8 @@ EXPORT_SYMBOL(devm_pci_remap_cfgspace); * * All operations are managed and will be undone on driver detach. * - * Returns a pointer to the remapped memory or an ERR_PTR() encoded error = code - * on failure. Usage example:: + * Returns a pointer to the remapped memory or an IOMEM_ERR_PTR() encoded = error + * code on failure. Usage example:: * * res =3D platform_get_resource(pdev, IORESOURCE_MEM, 0); * base =3D devm_pci_remap_cfg_resource(&pdev->dev, res); @@ -172,6 +401,17 @@ static void pcim_release(struct device *gendev, void *= res) struct pci_devres *this =3D res; int i; =20 + /* + * This is legacy code. + * + * All regions requested by a pcim_ function do get released through + * pcim_addr_resource_release(). Thanks to the hybrid nature of the pci_ + * region-request functions, this for-loop has to release the regions + * if they have been requested by such a function. + * + * TODO: Remove this once all users of pcim_enable_device() PLUS + * pci-region-request-functions have been ported to pcim_ functions. + */ for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) if (mask_contains_bar(this->region_mask, i)) pci_release_region(dev, i); @@ -258,19 +498,21 @@ EXPORT_SYMBOL(pcim_pin_device); =20 static void pcim_iomap_release(struct device *gendev, void *res) { - struct pci_dev *dev =3D to_pci_dev(gendev); - struct pcim_iomap_devres *this =3D res; - int i; - - for (i =3D 0; i < PCIM_IOMAP_MAX; i++) - if (this->table[i]) - pci_iounmap(dev, this->table[i]); + /* + * Do nothing. This is legacy code. + * + * Cleanup of the mappings is now done directly through the callbacks + * registered when creating them. + */ } =20 /** * pcim_iomap_table - access iomap allocation table * @pdev: PCI device to access iomap table for * + * Returns: + * Const pointer to array of __iomem pointers on success, NULL on failure. + * * Access iomap allocation table for @dev. If iomap table doesn't * exist and @pdev is managed, it will be allocated. All iomaps * recorded in the iomap table are automatically unmapped on driver @@ -343,30 +585,67 @@ static void pcim_remove_mapping_from_legacy_table(str= uct pci_dev *pdev, } } =20 +/* + * The same as pcim_remove_mapping_from_legacy_table(), but identifies the + * mapping by its BAR index. + */ +static void pcim_remove_bar_from_legacy_table(struct pci_dev *pdev, short = bar) +{ + void __iomem **legacy_iomap_table; + + if (bar >=3D PCI_STD_NUM_BARS) + return; + + legacy_iomap_table =3D (void __iomem **)pcim_iomap_table(pdev); + if (!legacy_iomap_table) + return; + + legacy_iomap_table[bar] =3D NULL; +} + /** * pcim_iomap - Managed pcim_iomap() * @pdev: PCI device to iomap for * @bar: BAR to iomap * @maxlen: Maximum length of iomap * - * Managed pci_iomap(). Map is automatically unmapped on driver - * detach. + * Returns: __iomem pointer on success, NULL on failure. + * + * Managed pci_iomap(). Map is automatically unmapped on driver detach. If + * desired, unmap manually only with pcim_iounmap(). + * + * This SHOULD only be used once per BAR. + * + * NOTE: + * Contrary to the other pcim_* functions, this function does not return an + * IOMEM_ERR_PTR() on failure, but a simple NULL. This is done for backwar= ds + * compatibility. */ void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxl= en) { void __iomem *mapping; + struct pcim_addr_devres *res; + + res =3D pcim_addr_devres_alloc(pdev); + if (!res) + return NULL; + res->type =3D PCIM_ADDR_DEVRES_TYPE_MAPPING; =20 mapping =3D pci_iomap(pdev, bar, maxlen); if (!mapping) - return NULL; + goto err_iomap; + res->baseaddr =3D mapping; =20 if (pcim_add_mapping_to_legacy_table(pdev, mapping, bar) !=3D 0) goto err_table; =20 + devres_add(&pdev->dev, res); return mapping; =20 err_table: pci_iounmap(pdev, mapping); +err_iomap: + pcim_addr_devres_free(res); return NULL; } EXPORT_SYMBOL(pcim_iomap); @@ -376,91 +655,291 @@ EXPORT_SYMBOL(pcim_iomap); * @pdev: PCI device to iounmap for * @addr: Address to unmap * - * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap(). + * Managed pci_iounmap(). @addr must have been mapped using a pcim_* mappi= ng + * function. */ void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) { - pci_iounmap(pdev, addr); + struct pcim_addr_devres res_searched; + + pcim_addr_devres_clear(&res_searched); + res_searched.type =3D PCIM_ADDR_DEVRES_TYPE_MAPPING; + res_searched.baseaddr =3D addr; + + if (devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched) !=3D 0) { + /* Doesn't exist. User passed nonsense. */ + return; + } =20 pcim_remove_mapping_from_legacy_table(pdev, addr); } EXPORT_SYMBOL(pcim_iounmap); =20 +/** + * pcim_iomap_region - Request and iomap a PCI BAR + * @pdev: PCI device to map IO resources for + * @bar: Index of a BAR to map + * @name: Name associated with the request + * + * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure. + * + * Mapping and region will get automatically released on driver detach. If + * desired, release manually only with pcim_iounmap_region(). + */ +static void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar, + const char *name) +{ + int ret; + struct pcim_addr_devres *res; + + res =3D pcim_addr_devres_alloc(pdev); + if (!res) + return IOMEM_ERR_PTR(-ENOMEM); + + res->type =3D PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING; + res->bar =3D bar; + + ret =3D __pcim_request_region(pdev, bar, name, 0); + if (ret !=3D 0) + goto err_region; + + res->baseaddr =3D pci_iomap(pdev, bar, 0); + if (!res->baseaddr) { + ret =3D -EINVAL; + goto err_iomap; + } + + devres_add(&pdev->dev, res); + return res->baseaddr; + +err_iomap: + __pcim_release_region(pdev, bar); +err_region: + pcim_addr_devres_free(res); + + return IOMEM_ERR_PTR(ret); +} + +/** + * pcim_iounmap_region - Unmap and release a PCI BAR + * @pdev: PCI device to operate on + * @bar: Index of BAR to unmap and release + * + * Unmap a BAR and release its region manually. Only pass BARs that were + * previously mapped by pcim_iomap_region(). + */ +static void pcim_iounmap_region(struct pci_dev *pdev, int bar) +{ + struct pcim_addr_devres res_searched; + + pcim_addr_devres_clear(&res_searched); + res_searched.type =3D PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING; + res_searched.bar =3D bar; + + devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched); +} + /** * pcim_iomap_regions - Request and iomap PCI BARs * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to request and iomap - * @name: Name used when requesting regions + * @name: Name associated with the requests + * + * Returns: 0 on success, negative error code on failure. * * Request and iomap regions specified by @mask. */ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name) { - void __iomem * const *iomap; - int i, rc; + int ret; + short bar; + void __iomem *mapping; =20 - iomap =3D pcim_iomap_table(pdev); - if (!iomap) - return -ENOMEM; + for (bar =3D 0; bar < DEVICE_COUNT_RESOURCE; bar++) { + if (!mask_contains_bar(mask, bar)) + continue; =20 - for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) { - unsigned long len; + mapping =3D pcim_iomap_region(pdev, bar, name); + if (IS_ERR(mapping)) { + ret =3D PTR_ERR(mapping); + goto err; + } + ret =3D pcim_add_mapping_to_legacy_table(pdev, mapping, bar); + if (ret !=3D 0) + goto err; + } =20 - if (!mask_contains_bar(mask, i)) - continue; + return 0; =20 - rc =3D -EINVAL; - len =3D pci_resource_len(pdev, i); - if (!len) - goto err_inval; +err: + while (--bar >=3D 0) { + pcim_iounmap_region(pdev, bar); + pcim_remove_bar_from_legacy_table(pdev, bar); + } =20 - rc =3D pci_request_region(pdev, i, name); - if (rc) - goto err_inval; + return ret; +} +EXPORT_SYMBOL(pcim_iomap_regions); =20 - rc =3D -ENOMEM; - if (!pcim_iomap(pdev, i, 0)) - goto err_region; +static int _pcim_request_region(struct pci_dev *pdev, int bar, const char = *name, + int request_flags) +{ + int ret; + struct pcim_addr_devres *res; + + res =3D pcim_addr_devres_alloc(pdev); + if (!res) + return -ENOMEM; + res->type =3D PCIM_ADDR_DEVRES_TYPE_REGION; + res->bar =3D bar; + + ret =3D __pcim_request_region(pdev, bar, name, request_flags); + if (ret !=3D 0) { + pcim_addr_devres_free(res); + return ret; } =20 + devres_add(&pdev->dev, res); return 0; +} =20 - err_region: - pci_release_region(pdev, i); - err_inval: - while (--i >=3D 0) { - if (!mask_contains_bar(mask, i)) - continue; - pcim_iounmap(pdev, iomap[i]); - pci_release_region(pdev, i); +/** + * pcim_request_region - Request a PCI BAR + * @pdev: PCI device to requestion region for + * @bar: Index of BAR to request + * @name: Name associated with the request + * + * Returns: 0 on success, a negative error code on failure. + * + * Request region specified by @bar. + * + * The region will automatically be released on driver detach. If desired, + * release manually only with pcim_release_region(). + */ +static int pcim_request_region(struct pci_dev *pdev, int bar, const char *= name) +{ + return _pcim_request_region(pdev, bar, name, 0); +} + +/** + * pcim_release_region - Release a PCI BAR + * @pdev: PCI device to operate on + * @bar: Index of BAR to release + * + * Release a region manually that was previously requested by + * pcim_request_region(). + */ +static void pcim_release_region(struct pci_dev *pdev, int bar) +{ + struct pcim_addr_devres res_searched; + + pcim_addr_devres_clear(&res_searched); + res_searched.type =3D PCIM_ADDR_DEVRES_TYPE_REGION; + res_searched.bar =3D bar; + + devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched); +} + + +/** + * pcim_release_all_regions - Release all regions of a PCI-device + * @pdev: the PCI device + * + * Release all regions previously requested through pcim_request_region() + * or pcim_request_all_regions(). + * + * Can be called from any context, i.e., not necessarily as a counterpart = to + * pcim_request_all_regions(). + */ +static void pcim_release_all_regions(struct pci_dev *pdev) +{ + short bar; + + for (bar =3D 0; bar < PCI_STD_NUM_BARS; bar++) + pcim_release_region(pdev, bar); +} + +/** + * pcim_request_all_regions - Request all regions + * @pdev: PCI device to map IO resources for + * @name: name associated with the request + * + * Returns: 0 on success, negative error code on failure. + * + * Requested regions will automatically be released at driver detach. If + * desired, release individual regions with pcim_release_region() or all of + * them at once with pcim_release_all_regions(). + */ +static int pcim_request_all_regions(struct pci_dev *pdev, const char *name) +{ + int ret; + short bar; + + for (bar =3D 0; bar < PCI_STD_NUM_BARS; bar++) { + ret =3D pcim_request_region(pdev, bar, name); + if (ret !=3D 0) + goto err; } =20 - return rc; + return 0; + +err: + pcim_release_all_regions(pdev); + + return ret; } -EXPORT_SYMBOL(pcim_iomap_regions); =20 /** * pcim_iomap_regions_request_all - Request all BARs and iomap specified o= nes * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to iomap - * @name: Name used when requesting regions + * @name: Name associated with the requests + * + * Returns: 0 on success, negative error code on failure. * * Request all PCI BARs and iomap regions specified by @mask. + * + * To release these resources manually, call pcim_release_region() for the + * regions and pcim_iounmap() for the mappings. */ int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, const char *name) { - int request_mask =3D ((1 << 6) - 1) & ~mask; - int rc; + short bar; + int ret; + void __iomem **legacy_iomap_table; =20 - rc =3D pci_request_selected_regions(pdev, request_mask, name); - if (rc) - return rc; + ret =3D pcim_request_all_regions(pdev, name); + if (ret !=3D 0) + return ret; =20 - rc =3D pcim_iomap_regions(pdev, mask, name); - if (rc) - pci_release_selected_regions(pdev, request_mask); - return rc; + for (bar =3D 0; bar < PCI_STD_NUM_BARS; bar++) { + if (!mask_contains_bar(mask, bar)) + continue; + if (!pcim_iomap(pdev, bar, 0)) + goto err; + } + + return 0; + +err: + /* + * If bar is larger than 0, then pcim_iomap() above has most likely + * failed because of -EINVAL. If it is equal 0, most likely the table + * couldn't be created, indicating -ENOMEM. + */ + ret =3D bar > 0 ? -EINVAL : -ENOMEM; + legacy_iomap_table =3D (void __iomem **)pcim_iomap_table(pdev); + + while (--bar >=3D 0) + pcim_iounmap(pdev, legacy_iomap_table[bar]); + + pcim_release_all_regions(pdev); + + return ret; } EXPORT_SYMBOL(pcim_iomap_regions_request_all); =20 @@ -473,19 +952,14 @@ EXPORT_SYMBOL(pcim_iomap_regions_request_all); */ void pcim_iounmap_regions(struct pci_dev *pdev, int mask) { - void __iomem * const *iomap; - int i; + short bar; =20 - iomap =3D pcim_iomap_table(pdev); - if (!iomap) - return; - - for (i =3D 0; i < PCIM_IOMAP_MAX; i++) { - if (!mask_contains_bar(mask, i)) + for (bar =3D 0; bar < PCI_STD_NUM_BARS; bar++) { + if (!mask_contains_bar(mask, bar)) continue; =20 - pcim_iounmap(pdev, iomap[i]); - pci_release_region(pdev, i); + pcim_iounmap_region(pdev, bar); + pcim_remove_bar_from_legacy_table(pdev, bar); } } EXPORT_SYMBOL(pcim_iounmap_regions); diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 59e0949fb079..d94445f5f882 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3883,6 +3883,17 @@ void pci_release_region(struct pci_dev *pdev, int ba= r) release_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)); =20 + /* + * This devres utility makes this function sometimes managed + * (when pcim_enable_device() has been called before). + * + * This is bad because it conflicts with the pcim_ functions being + * exclusively responsible for managed PCI. Its "sometimes yes, + * sometimes no" nature can cause bugs. + * + * TODO: Remove this once all users that use pcim_enable_device() PLUS + * a region request function have been ported to using pcim_ functions. + */ dr =3D find_pci_dr(pdev); if (dr) dr->region_mask &=3D ~(1 << bar); @@ -3927,6 +3938,17 @@ static int __pci_request_region(struct pci_dev *pdev= , int bar, goto err_out; } =20 + /* + * This devres utility makes this function sometimes managed + * (when pcim_enable_device() has been called before). + * + * This is bad because it conflicts with the pcim_ functions being + * exclusively responsible for managed pci. Its "sometimes yes, + * sometimes no" nature can cause bugs. + * + * TODO: Remove this once all users that use pcim_enable_device() PLUS + * a region request function have been ported to using pcim_ functions. + */ dr =3D find_pci_dr(pdev); if (dr) dr->region_mask |=3D 1 << bar; diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index fd44565c4756..c09487f5550c 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -826,6 +826,11 @@ struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; unsigned int mwi:1; + + /* + * TODO: remove the region_mask once everyone calling + * pcim_enable_device() + pci_*region*() is ported to pcim_ functions. + */ u32 region_mask; }; =20 --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 3A12D1459E5 for ; Thu, 13 Jun 2024 11:50:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279453; cv=none; b=QHFFsHq4E9ac5sDfEuxFmo4Cnsoad3FMNWn07qARisXSrkrYO3AQWyTItsNcEp7UMxxDa1S018fvtFehOsx08won4HaiqPP40SsIeqQZyyZ8S94TmBf+MS6eIbmreJtRO4/2lvnHEzP/jEOqAGfTctzrsZzd6gudIp8xTFzcyr4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279453; c=relaxed/simple; bh=CKmb7V8YQKy60Y3zYZ5nF36qcetaUUfvS68MhKTVZyk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CTtw4gXabzczZhKuCvkUa47aamX+pJ+YMwWfiSRvBtSxbpdHS6d+Nnj9TcJL+YLnjJ+S2g85HhLKro0AcBGhnDqHD3wqMEoASgyBQekK3hhtpOyWhBcX1Ryp/GqmDiaH0f4PCTInBNe2ezKkO0hQO9A6NDnMEv4xUpLbnm5y1qA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=T+oNyFNl; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="T+oNyFNl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279451; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CL+kLbe+Y26ksl0hcQ0e5foEJJ8TqrY6DSq54p9TklM=; b=T+oNyFNl0mII1OBF/8KP+MAxCc/IzMe4TWH5Obv5qpsVY5pM34NJs/namCkBfbLPgVWDFj VX46WNsJgav3xBDFg+Q2l1KcIuYOs/p+LPQX7A82bJqHFd9ESUSKHVjs9WcTvWks7s5Nxk C3bKBwupmDYArK9Z/6RnrfSRqPriKm8= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-473-0oy7-F2nMNq21rbiDqVesw-1; Thu, 13 Jun 2024 07:50:49 -0400 X-MC-Unique: 0oy7-F2nMNq21rbiDqVesw-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-421920de031so1547365e9.0 for ; Thu, 13 Jun 2024 04:50:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279449; x=1718884249; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CL+kLbe+Y26ksl0hcQ0e5foEJJ8TqrY6DSq54p9TklM=; b=jYQpmAqGy9p/rR/avKViIZC8+SjGfTBqpda1ncw7RWv3D7nEGTxIYHlAUhXGX18UVy JK0m9xodhFM5OgnK46mGryE4yw0cq2zXscyuDfTXMkiDklwBXdYIANEmRlvskBcQKt+D r33JeBCfKgnZck3ky6isrqK/bvcVUcmXctlRiM0RMBl3L0hPYl8XCHzAOMZ8qm2OttGI fP9gT04oAn3hZ5Y7xY5Wjqp7miCToWRJOK7Dst6eyu59MYHoXnDbc41OT1Qttp3bJbZ3 yGT3NGtfY2x9sV2Bxd2uBorDP5Ikwk7StMRNrWexWKuMQpyimA24UKGmwhIWXW1fdRbz QHsw== X-Forwarded-Encrypted: i=1; AJvYcCVBK9P9zfjTXuhYpwOFUU57tcyKodGHCq6eGiBITPhqX/K/ihFAbVcnA/qyqDCz0AItFC2IoSLM9iBH2ljtjX4nLSVbR71YxWmNmtbb X-Gm-Message-State: AOJu0Yw0atDkAZCy8S4y5iyOrlD4gsOjAgBUbS8N7hGGyW99gxynfj+E b9da8jfQY6ecryB1H5HaHFHeLXMGobmjcNp00eGgmms4mk5lXRqX/v1DTa8VRXcBFqz3PxwZiJU TahkvOOUdAQ8YmvHKo2h/qZpeuFb92Ifj+gkFvqFvW0qiOL8+lsa5kHI2ubXoyA== X-Received: by 2002:a5d:5f93:0:b0:35f:306f:1587 with SMTP id ffacd0b85a97d-36079a50f05mr208534f8f.5.1718279448838; Thu, 13 Jun 2024 04:50:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHEPTiocjmZpdSuZITHvA9vbQ/P9nyYz57aqk+QbvsZnFOIEMIsJGsexzqpTKeFCx6yJ+Ucbw== X-Received: by 2002:a5d:5f93:0:b0:35f:306f:1587 with SMTP id ffacd0b85a97d-36079a50f05mr208519f8f.5.1718279448583; Thu, 13 Jun 2024 04:50:48 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:48 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 04/13] PCI: Deprecate two surplus devres functions Date: Thu, 13 Jun 2024 13:50:17 +0200 Message-ID: <20240613115032.29098-5-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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" pcim_iomap_table() should not be used anymore because it contributed to the PCI devres API being designed contrary to devres's design goals. pcim_iomap_regions_request_all() is a surplus, complicated function that can easily be replaced by using a pcim_* request function in combination with a pcim_* mapping function. Mark pcim_iomap_table() and pcim_iomap_regions_request_all() as deprecated in the function documentation. Link: https://lore.kernel.org/r/20240605081605.18769-6-pstanner@redhat.com Signed-off-by: Philipp Stanner Signed-off-by: Bjorn Helgaas --- drivers/pci/devres.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index cf2c11b54ca6..5ecffc7424ed 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -507,7 +507,7 @@ static void pcim_iomap_release(struct device *gendev, v= oid *res) } =20 /** - * pcim_iomap_table - access iomap allocation table + * pcim_iomap_table - access iomap allocation table (DEPRECATED) * @pdev: PCI device to access iomap table for * * Returns: @@ -521,6 +521,11 @@ static void pcim_iomap_release(struct device *gendev, = void *res) * This function might sleep when the table is first allocated but can * be safely called without context and guaranteed to succeed once * allocated. + * + * This function is DEPRECATED. Do not use it in new code. Instead, obtain= a + * mapping's address directly from one of the pcim_* mapping functions. For + * example: + * void __iomem *mappy =3D pcim_iomap(pdev, bar, length); */ void __iomem * const *pcim_iomap_table(struct pci_dev *pdev) { @@ -894,6 +899,7 @@ static int pcim_request_all_regions(struct pci_dev *pde= v, const char *name) =20 /** * pcim_iomap_regions_request_all - Request all BARs and iomap specified o= nes + * (DEPRECATED) * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to iomap * @name: Name associated with the requests @@ -904,6 +910,10 @@ static int pcim_request_all_regions(struct pci_dev *pd= ev, const char *name) * * To release these resources manually, call pcim_release_region() for the * regions and pcim_iounmap() for the mappings. + * + * This function is DEPRECATED. Don't use it in new code. Instead, use one + * of the pcim_* region request functions in combination with a pcim_* + * mapping function. */ int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, const char *name) --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 292901474BF for ; Thu, 13 Jun 2024 11:50:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279456; cv=none; b=Qqa0QBos3QIpLGIYjtwqKJ+wcPbu8FfYG9IPzhstEDzSpLa4SZxQPI3Q7dK31l3birutgA60svznIWf4XgKVBcIlHhRgITKI5RdSXiXePmGCm1ECafqpK+uQF6wGtBqQaMF7806jxY+7NFLNjyFPou8DTfrauw5QpLlnhi2F6aI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279456; c=relaxed/simple; bh=Rc0vk4XaTa4NVY067IZa1x33Rqvecp2YYmlgZGJ+vNs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AXd+TEpVBWfPqJIRomC0kwNJnEO7nPaBqJycpAa3yWkkH9tBDCFAO76YQYgUF/iJpoA0NqY6rF0g+oE4HkaX1FHoECxgvD6QTnNP49IoYe2kObZxxzWpjYWUbzpNgwKfdYejv9E+osJMqXDlwjZTUpnltVH+2GiWTSJdnPBpCIg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=GZpx68r7; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GZpx68r7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279453; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ViBYsK7r7YHaCmYfaljCmdF+x2qktzp5uILOcoPl4ng=; b=GZpx68r7RHlMbmH6kRric3fBdZHm3zP4mrJaFwvqM2yi4ZY6VahE/S2ZsdtFQzEHpDSgMW 446D95MudpA93Ee2+qYZi83FMdnD6cIgSuQt+1MwdSXkGxz04yEMthUHji1/pMyfupr6C5 knv28vdqPWK12TIMgu+Lk7uMv+MxXPA= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-692-nauzBHe4PlSf2VDTdhgmqA-1; Thu, 13 Jun 2024 07:50:51 -0400 X-MC-Unique: nauzBHe4PlSf2VDTdhgmqA-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4210e98f8d7so1509785e9.3 for ; Thu, 13 Jun 2024 04:50:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279450; x=1718884250; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ViBYsK7r7YHaCmYfaljCmdF+x2qktzp5uILOcoPl4ng=; b=xIRDllx+8tDMm736xa1CDZNFzTFt5DKAtTnnfkh9dq4e872Bjlb80nk6MJeVIXPOpF EGIUMr4rvU1zBc3yklaV/fjnQppbF9DfvtWzj7pcRq8lowvL75PtwnynocSZVoXfkDxc Ay7MXgzkv0adGnFIWnxihWy96DNX1yfdSC/bVQXj48Wv22rVOejTb2+p4oSEFeTzl7+U fFhLuYQWtD8oOiu8XMf2vUb+MRqc7cCLp4yMY1qEsdctPr8y6vE4qYqexFMvt4WBjX37 NcOMKn7pxo9ONuO+YTKLZJFpw1MIK4jFFg1xQisMEHZLdKkxmLTQPdtyPGr7owXmnWCK vYBQ== X-Forwarded-Encrypted: i=1; AJvYcCV9aKwYQ971WSxbd93DYcl/+GgD/RZZ2udfBNV5Ni20BXo4F6d6z3SOj+xW9mrU0F4VL+nNojLOd+Rtd58wfycvYLj/zm1+KKST+oPm X-Gm-Message-State: AOJu0YxeivyZW6J7PoItuAcQzdiy5OBAunQYaIM4M8/zlJfgNmCIYvRy iGCmUDilKIn50qCuyCOkgDetkIOOu2vyyHgqsG7XQkOwbPe3Qr01DT/kiOYbjsh+6KFeQS4JNv6 qXD9N8kmIvKiAnAGP27X4W5L76If28A/KR1XD6ixrFYAIE0OL9yCqpolteA+ZsA== X-Received: by 2002:a05:6000:4023:b0:35f:2fd3:85d5 with SMTP id ffacd0b85a97d-36079a472f1mr168567f8f.3.1718279450568; Thu, 13 Jun 2024 04:50:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEBBfx4nYnLD/abQa3JiWhpHQ7DZlbaL9/HZMDFVCmtO64CnJnRENAa7IHgBrFFdGtk0NJd5w== X-Received: by 2002:a05:6000:4023:b0:35f:2fd3:85d5 with SMTP id ffacd0b85a97d-36079a472f1mr168552f8f.3.1718279450264; Thu, 13 Jun 2024 04:50:50 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:49 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 05/13] PCI: Make devres region requests consistent Date: Thu, 13 Jun 2024 13:50:18 +0200 Message-ID: <20240613115032.29098-6-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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" Now that pure managed region request functions are available, the implementation of the hybrid-functions which are only sometimes managed can be made more consistent and readable by wrapping those always-managed functions. Implement pcim_request_region_exclusive() as a PCI-internal helper. Have the PCI request / release functions call their pcim_ counterparts. Remove the now surplus region_mask from struct pci_devres. Link: https://lore.kernel.org/r/20240605081605.18769-7-pstanner@redhat.com Signed-off-by: Philipp Stanner Signed-off-by: Bjorn Helgaas --- drivers/pci/devres.c | 53 ++++++++++++++++++++++---------------------- drivers/pci/pci.c | 47 +++++++++++++-------------------------- drivers/pci/pci.h | 10 ++++----- 3 files changed, 45 insertions(+), 65 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 5ecffc7424ed..d90bed785c3f 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -24,18 +24,15 @@ * * Consequently, in the new API, region requests performed by the pcim_ * functions are automatically cleaned up through the devres callback - * pcim_addr_resource_release(), while requests performed by - * pcim_enable_device() + pci_*region*() are automatically cleaned up - * through the for-loop in pcim_release(). + * pcim_addr_resource_release(). + * Users utilizing pcim_enable_device() + pci_*region*() are redirected= in + * pci.c to the managed functions here in this file. This isn't exactly + * perfect, but the only alternative way would be to port ALL drivers u= sing + * said combination to pcim_ functions. * - * TODO 1: + * TODO: * Remove the legacy table entirely once all calls to pcim_iomap_table() in * the kernel have been removed. - * - * TODO 2: - * Port everyone calling pcim_enable_device() + pci_*region*() to using the - * pcim_ functions. Then, remove all devres functionality from pci_*region= *() - * functions and remove the associated cleanups described above in point #= 2. */ =20 /* @@ -399,22 +396,6 @@ static void pcim_release(struct device *gendev, void *= res) { struct pci_dev *dev =3D to_pci_dev(gendev); struct pci_devres *this =3D res; - int i; - - /* - * This is legacy code. - * - * All regions requested by a pcim_ function do get released through - * pcim_addr_resource_release(). Thanks to the hybrid nature of the pci_ - * region-request functions, this for-loop has to release the regions - * if they have been requested by such a function. - * - * TODO: Remove this once all users of pcim_enable_device() PLUS - * pci-region-request-functions have been ported to pcim_ functions. - */ - for (i =3D 0; i < DEVICE_COUNT_RESOURCE; i++) - if (mask_contains_bar(this->region_mask, i)) - pci_release_region(dev, i); =20 if (this->mwi) pci_clear_mwi(dev); @@ -823,11 +804,29 @@ static int _pcim_request_region(struct pci_dev *pdev,= int bar, const char *name, * The region will automatically be released on driver detach. If desired, * release manually only with pcim_release_region(). */ -static int pcim_request_region(struct pci_dev *pdev, int bar, const char *= name) +int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) { return _pcim_request_region(pdev, bar, name, 0); } =20 +/** + * pcim_request_region_exclusive - Request a PCI BAR exclusively + * @pdev: PCI device to requestion region for + * @bar: Index of BAR to request + * @name: Name associated with the request + * + * Returns: 0 on success, a negative error code on failure. + * + * Request region specified by @bar exclusively. + * + * The region will automatically be released on driver detach. If desired, + * release manually only with pcim_release_region(). + */ +int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const cha= r *name) +{ + return _pcim_request_region(pdev, bar, name, IORESOURCE_EXCLUSIVE); +} + /** * pcim_release_region - Release a PCI BAR * @pdev: PCI device to operate on @@ -836,7 +835,7 @@ static int pcim_request_region(struct pci_dev *pdev, in= t bar, const char *name) * Release a region manually that was previously requested by * pcim_request_region(). */ -static void pcim_release_region(struct pci_dev *pdev, int bar) +void pcim_release_region(struct pci_dev *pdev, int bar) { struct pcim_addr_devres res_searched; =20 diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index d94445f5f882..7013699db242 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3872,7 +3872,15 @@ EXPORT_SYMBOL(pci_enable_atomic_ops_to_root); */ void pci_release_region(struct pci_dev *pdev, int bar) { - struct pci_devres *dr; + /* + * This is done for backwards compatibility, because the old PCI devres + * API had a mode in which the function became managed if it had been + * enabled with pcim_enable_device() instead of pci_enable_device(). + */ + if (pci_is_managed(pdev)) { + pcim_release_region(pdev, bar); + return; + } =20 if (pci_resource_len(pdev, bar) =3D=3D 0) return; @@ -3882,21 +3890,6 @@ void pci_release_region(struct pci_dev *pdev, int ba= r) else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) release_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)); - - /* - * This devres utility makes this function sometimes managed - * (when pcim_enable_device() has been called before). - * - * This is bad because it conflicts with the pcim_ functions being - * exclusively responsible for managed PCI. Its "sometimes yes, - * sometimes no" nature can cause bugs. - * - * TODO: Remove this once all users that use pcim_enable_device() PLUS - * a region request function have been ported to using pcim_ functions. - */ - dr =3D find_pci_dr(pdev); - if (dr) - dr->region_mask &=3D ~(1 << bar); } EXPORT_SYMBOL(pci_release_region); =20 @@ -3922,7 +3915,12 @@ EXPORT_SYMBOL(pci_release_region); static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name, int exclusive) { - struct pci_devres *dr; + if (pci_is_managed(pdev)) { + if (exclusive =3D=3D IORESOURCE_EXCLUSIVE) + return pcim_request_region_exclusive(pdev, bar, res_name); + + return pcim_request_region(pdev, bar, res_name); + } =20 if (pci_resource_len(pdev, bar) =3D=3D 0) return 0; @@ -3938,21 +3936,6 @@ static int __pci_request_region(struct pci_dev *pdev= , int bar, goto err_out; } =20 - /* - * This devres utility makes this function sometimes managed - * (when pcim_enable_device() has been called before). - * - * This is bad because it conflicts with the pcim_ functions being - * exclusively responsible for managed pci. Its "sometimes yes, - * sometimes no" nature can cause bugs. - * - * TODO: Remove this once all users that use pcim_enable_device() PLUS - * a region request function have been ported to using pcim_ functions. - */ - dr =3D find_pci_dr(pdev); - if (dr) - dr->region_mask |=3D 1 << bar; - return 0; =20 err_out: diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index c09487f5550c..2403c5a0ff7a 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -826,16 +826,14 @@ struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; unsigned int mwi:1; - - /* - * TODO: remove the region_mask once everyone calling - * pcim_enable_device() + pci_*region*() is ported to pcim_ functions. - */ - u32 region_mask; }; =20 struct pci_devres *find_pci_dr(struct pci_dev *pdev); =20 +int pcim_request_region(struct pci_dev *pdev, int bar, const char *name); +int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const cha= r *name); +void pcim_release_region(struct pci_dev *pdev, int bar); + /* * Config Address for PCI Configuration Mechanism #1 * --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 D7F0F1487E7 for ; Thu, 13 Jun 2024 11:50:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279458; cv=none; b=VMONYe87OdxMSQlV/fMlRNSeAIVxV5pS3pfR1CM8AkTAhiWnszpXheKs2p4F2bPgzPANK0qp147441HAzQvYX/z9Equ/8wpErk3WmDzBuQC0tEH4HpEltQ6bHghUvkC9pTGUkuVSp/Tn4IrSEO7mDTMloG5f+xHb0+p03uBF7nI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279458; c=relaxed/simple; bh=5PE3+Mg+QJfwk/81MP4neUpiVdZJoI7gS0UJ6lgiki8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=M9av5vIozgdEzOacHp8/PVjAOw94VFHfacdu/i+KRGhWOLxPm/kYs6dqyHsUPCUkVAv2voY6c+erz2FemKdl8+CwQPe/CEX+2w5aHE09/2s8WThN6ym8S5W1vKncP8AuhtPHP1Q/meATBButxmwV8z+uRCueCgtBRydkzEWxL0Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=T/n41iVj; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="T/n41iVj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279456; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QeDlmSjlnwr2O/aP5XCLyUukk4Ihvpzfb1AuxNmdg5k=; b=T/n41iVjh1P6aYCjsIKYID7ZZOeTnFD73SvMxRYW7yHb7wqVkqNq1CoL4kx9nc2vK6iDWc Q0uaHaiXod3YR5GVqd1SSZ1lpD7JvqvLzZoDT1ftTU3YjOMarBFpjrZOwuRtf0V9mhgELw zeQd926AFOaWKSlh8umc3vmhGA4TkhE= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-30-oYDIUXGaM_yFJv6iZNi6Dw-1; Thu, 13 Jun 2024 07:50:52 -0400 X-MC-Unique: oYDIUXGaM_yFJv6iZNi6Dw-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-35f291b147dso4584f8f.0 for ; Thu, 13 Jun 2024 04:50:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279451; x=1718884251; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QeDlmSjlnwr2O/aP5XCLyUukk4Ihvpzfb1AuxNmdg5k=; b=h/lXtuYLaP/RIc30h5wmWMigdN2o4e28VhjgUhFkKcvJR+Dz1fZK2tncHvOvfPHeW3 RPMowjZiAguVypEe8oc9USWeGe0zL9LRjUWVLgGgC8QbOI6gBc896zw2bJOyZ0xBB4VE GkmNkfYP+Nxdl8bUIJZu0L97X/XPOr8Ntord7Hw+tUh34D97F/0f12H/FqaaklUexjDE yVSakJO2tEliy+jZhWm3+nXvsxM2iVsjDsEXo6/TikZasZjs2MrLT6ZEP8jQdllLiFtj FwW2HSYIc2TU2BHgq6hJ6MCQVtLSxv0SDjKC8Kr7lzQrsmcvdXtVClX6rNsnEFwqKay6 B4nw== X-Forwarded-Encrypted: i=1; AJvYcCVcbpBMgD7ZUpsHKJ6y36DaDAVDq+wHCpwxcKV9IiQb0LrutTvpGkDpa3Q7BEEQIIDLopGGUjL9Xk9EY4yglkwGum5gfBhyvjwGYCM7 X-Gm-Message-State: AOJu0YzMlODfx56QwdPfNkWpqojzqNQrKo61yiR0YNpoTg1i77DcmLua DP0HhEg4MUJ+2U3fdJGL0PcmqKMRlUXuSbbpHCMg9Ix212NodVJHT/D7W8UiBTE22eZuZtOhWDD W6F5wED1P1ku5LSbSYnjjSbu4gM/+oscOm9qBt56fWwe4mIAtUGnGEZpQzygUBg== X-Received: by 2002:a05:6000:1845:b0:35f:1f7e:892e with SMTP id ffacd0b85a97d-36079a5fdddmr172025f8f.7.1718279451485; Thu, 13 Jun 2024 04:50:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHpv9lSaVfD9w56GIK9nZEr6kUGUg9N6/YznSMrwsXxnz492p2v4uVfvUh2ehH31as5XiaCgw== X-Received: by 2002:a05:6000:1845:b0:35f:1f7e:892e with SMTP id ffacd0b85a97d-36079a5fdddmr172006f8f.7.1718279451134; Thu, 13 Jun 2024 04:50:51 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:50 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 06/13] PCI: Warn users about complicated devres nature Date: Thu, 13 Jun 2024 13:50:19 +0200 Message-ID: <20240613115032.29098-7-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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 PCI region-request functions become managed functions when pcim_enable_device() has been called previously instead of pci_enable_device(). This has already caused a bug (in 8558de401b5f) by confusing users, who came to believe that all PCI functions, such as pci_iomap_range(), suddenly are managed that way, which is not the case. Add comments to the relevant functions' docstrings that warn users about this behavior. Link: https://lore.kernel.org/r/20240605081605.18769-8-pstanner@redhat.com Signed-off-by: Philipp Stanner Signed-off-by: Bjorn Helgaas --- drivers/pci/iomap.c | 16 ++++++++++++++++ drivers/pci/pci.c | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/drivers/pci/iomap.c b/drivers/pci/iomap.c index c9725428e387..a715a4803c95 100644 --- a/drivers/pci/iomap.c +++ b/drivers/pci/iomap.c @@ -23,6 +23,10 @@ * * @maxlen specifies the maximum length to map. If you want to get access = to * the complete BAR from offset to the end, pass %0 here. + * + * NOTE: + * This function is never managed, even if you initialized with + * pcim_enable_device(). * */ void __iomem *pci_iomap_range(struct pci_dev *dev, int bar, @@ -63,6 +67,10 @@ EXPORT_SYMBOL(pci_iomap_range); * * @maxlen specifies the maximum length to map. If you want to get access = to * the complete BAR from offset to the end, pass %0 here. + * + * NOTE: + * This function is never managed, even if you initialized with + * pcim_enable_device(). * */ void __iomem *pci_iomap_wc_range(struct pci_dev *dev, int bar, @@ -106,6 +114,10 @@ EXPORT_SYMBOL_GPL(pci_iomap_wc_range); * * @maxlen specifies the maximum length to map. If you want to get access = to * the complete BAR without checking for its length first, pass %0 here. + * + * NOTE: + * This function is never managed, even if you initialized with + * pcim_enable_device(). If you need automatic cleanup, use pcim_iomap(). * */ void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) { @@ -127,6 +139,10 @@ EXPORT_SYMBOL(pci_iomap); * * @maxlen specifies the maximum length to map. If you want to get access = to * the complete BAR without checking for its length first, pass %0 here. + * + * NOTE: + * This function is never managed, even if you initialized with + * pcim_enable_device(). * */ void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long max= len) { diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 7013699db242..5e4f377411ec 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3900,6 +3900,8 @@ EXPORT_SYMBOL(pci_release_region); * @res_name: Name to be associated with resource. * @exclusive: whether the region access is exclusive or not * + * Returns: 0 on success, negative error code on failure. + * * Mark the PCI region associated with PCI device @pdev BAR @bar as * being reserved by owner @res_name. Do not access any * address inside the PCI regions unless this call returns @@ -3950,6 +3952,8 @@ static int __pci_request_region(struct pci_dev *pdev,= int bar, * @bar: BAR to be reserved * @res_name: Name to be associated with resource * + * Returns: 0 on success, negative error code on failure. + * * Mark the PCI region associated with PCI device @pdev BAR @bar as * being reserved by owner @res_name. Do not access any * address inside the PCI regions unless this call returns @@ -3957,6 +3961,11 @@ static int __pci_request_region(struct pci_dev *pdev= , int bar, * * Returns 0 on success, or %EBUSY on error. A warning * message is also printed on failure. + * + * NOTE: + * This is a "hybrid" function: It's normally unmanaged, but becomes manag= ed + * when pcim_enable_device() has been called in advance. This hybrid featu= re is + * DEPRECATED! If you want managed cleanup, use the pcim_* functions inste= ad. */ int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name) { @@ -4007,6 +4016,13 @@ static int __pci_request_selected_regions(struct pci= _dev *pdev, int bars, * @pdev: PCI device whose resources are to be reserved * @bars: Bitmask of BARs to be requested * @res_name: Name to be associated with resource + * + * Returns: 0 on success, negative error code on failure. + * + * NOTE: + * This is a "hybrid" function: It's normally unmanaged, but becomes manag= ed + * when pcim_enable_device() has been called in advance. This hybrid featu= re is + * DEPRECATED! If you want managed cleanup, use the pcim_* functions inste= ad. */ int pci_request_selected_regions(struct pci_dev *pdev, int bars, const char *res_name) @@ -4015,6 +4031,19 @@ int pci_request_selected_regions(struct pci_dev *pde= v, int bars, } EXPORT_SYMBOL(pci_request_selected_regions); =20 +/** + * pci_request_selected_regions_exclusive - Request regions exclusively + * @pdev: PCI device to request regions from + * @bars: bit mask of BARs to request + * @res_name: name to be associated with the requests + * + * Returns: 0 on success, negative error code on failure. + * + * NOTE: + * This is a "hybrid" function: It's normally unmanaged, but becomes manag= ed + * when pcim_enable_device() has been called in advance. This hybrid featu= re is + * DEPRECATED! If you want managed cleanup, use the pcim_* functions inste= ad. + */ int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars, const char *res_name) { @@ -4032,7 +4061,6 @@ EXPORT_SYMBOL(pci_request_selected_regions_exclusive); * successful call to pci_request_regions(). Call this function only * after all use of the PCI regions has ceased. */ - void pci_release_regions(struct pci_dev *pdev) { pci_release_selected_regions(pdev, (1 << PCI_STD_NUM_BARS) - 1); @@ -4064,6 +4092,8 @@ EXPORT_SYMBOL(pci_request_regions); * @pdev: PCI device whose resources are to be reserved * @res_name: Name to be associated with resource. * + * Returns: 0 on success, negative error code on failure. + * * Mark all PCI regions associated with PCI device @pdev as being reserved * by owner @res_name. Do not access any address inside the PCI regions * unless this call returns successfully. @@ -4073,6 +4103,11 @@ EXPORT_SYMBOL(pci_request_regions); * * Returns 0 on success, or %EBUSY on error. A warning message is also * printed on failure. + * + * NOTE: + * This is a "hybrid" function: It's normally unmanaged, but becomes manag= ed + * when pcim_enable_device() has been called in advance. This hybrid featu= re is + * DEPRECATED! If you want managed cleanup, use the pcim_* functions inste= ad. */ int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_na= me) { @@ -4404,6 +4439,11 @@ void pci_disable_parity(struct pci_dev *dev) * @enable: boolean: whether to enable or disable PCI INTx * * Enables/disables PCI INTx for device @pdev + * + * NOTE: + * This is a "hybrid" function: It's normally unmanaged, but becomes manag= ed + * when pcim_enable_device() has been called in advance. This hybrid featu= re is + * DEPRECATED! */ void pci_intx(struct pci_dev *pdev, int enable) { --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 4B1E21487D4 for ; Thu, 13 Jun 2024 11:50:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279457; cv=none; b=tYyBGU79YwUbxi4Ivz+nLrmTn+h+evBQdAcqfm1sTLBo/Eh552MeeAld6CHpo+KmXyOQ4ee1CuJ52/ElD3Ux9jZ0EuBDsPU/MCpi4c+jLwE8zV85POkUJw6X6dm0A1hPne8WppL4TK5xpAwrLf09mkPRjEGO3J8glu79uGJnxVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279457; c=relaxed/simple; bh=xCJskR4ZILXQ5wDrghh4Sq+j29QXGosY9+0FgltF0/I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GbuDL2YJv+PwpMd/2YZMyDoVqyu3nAujMvp6P3l7NZW8wSLDHQcTE0CyTWhJJ8HfmiNddx9I1TVPih3JJiYIlHt6vzbTkSy1Y6B7pDD+faoj1V+3OSfM5J1eKGyY/WSc1XY9goOxph5uCYXOHi9SSO5g9RR7Mvpo/0q4pFnUk1A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ONkuou/t; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ONkuou/t" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279455; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qMEejGKihJo2G832kSH48RcHSFeE2XrrHWpYzOQBaEo=; b=ONkuou/tKfMRfE3Y/QnJKm4lGCi2cZQPQ6CR6B8qrIHmWlDf6dkZy/8/YGVroVTM3SXGci dFHoNd2HBkFREFwznSprQjI6MC4oBGT1fBE8BscpPV1Jz/hfRUGxl2sdih+3Lg0nomAeyg SfakEpQhEiy0kHSgOCD2m7Oh/M8czbc= Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-584-gEQy3wTYN0mppo9KHaN7KQ-1; Thu, 13 Jun 2024 07:50:54 -0400 X-MC-Unique: gEQy3wTYN0mppo9KHaN7KQ-1 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-2ea8ad932a5so455551fa.0 for ; Thu, 13 Jun 2024 04:50:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279452; x=1718884252; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qMEejGKihJo2G832kSH48RcHSFeE2XrrHWpYzOQBaEo=; b=xJEPrHHkbisDFTZnMV+n30i3dkVHSpS0rxRngKF4mogLt8kQ7BYk+c20SQtLIam4dQ 32a3zsMUw6UpSP3oWHt23WA6qHXgw9tpFBEjhQhUI+Du55PWd/KwfJIPfej1ijICnuiT 8SyicNp7ivy9VRvEcp6mumFEEbL+76gg7rUdYrQrKOJ1qULFaJXB2iVnKVwaPuXvDl6C UCWPc+pFsv872A0+6dpsVRKAv+xnj4nme91Gp2OqiYjYNm323cvfMWTClL16mbjrp+w1 R7EGFrTsOn/1YQSLUza42GPqNx5yoXqdG8yd9eWs4R9BKsgWzOVKeGFJXPZ5qmY0kg8Q j4/g== X-Forwarded-Encrypted: i=1; AJvYcCWvcNmVVYVUoP97XkeptGm78gSWo0k41ogycAiswQ0fvG8oG5lvkBtwb+01dzCIPmksrcfcsEoGk9+4KuOobYzQCBdUi0r9fy27cGR6 X-Gm-Message-State: AOJu0Yyjile2jUFDM8jFMIvIyzWnjGGdniO5fhZHIiVVfU3+wVfdu4ER mCWmHNHJQ7H+UgPS+7HNW4RVaSCOP0RGQrgFJfBIIIgLn2yqUjF0wNSLhhSjjSVWWvn6BZUfddf aew4JMaiyRVka/Tw6mBrVw8LM8JPY1oRbHX/Bx/gqvkBXCKfMlOM7y+56Y583vw== X-Received: by 2002:a05:6512:1598:b0:52c:8784:5b2c with SMTP id 2adb3069b0e04-52ca5d03c67mr13439e87.3.1718279452679; Thu, 13 Jun 2024 04:50:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHEqupRIXquizQF60Ogvk1gSUj/lELnfPgWk0B7d0l5dOVUd+wsdDCM/WABqJ9TrRzGJhmkEQ== X-Received: by 2002:a05:6512:1598:b0:52c:8784:5b2c with SMTP id 2adb3069b0e04-52ca5d03c67mr13426e87.3.1718279452377; Thu, 13 Jun 2024 04:50:52 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:51 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 07/13] PCI: Remove enabled status bit from pci_devres Date: Thu, 13 Jun 2024 13:50:20 +0200 Message-ID: <20240613115032.29098-8-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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 PCI devres implementation has a separate boolean to track whether a device is enabled. That, however, can easily be tracked in an agnostic manner through the function pci_is_enabled(). Using it allows for simplifying the PCI devres implementation. Replace the separate 'enabled' status bit from struct pci_devres with calls to pci_is_enabled() at the appropriate places. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 11 ++++------- drivers/pci/pci.c | 6 ------ drivers/pci/pci.h | 1 - 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index d90bed785c3f..643e3a94a1d6 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -403,7 +403,7 @@ static void pcim_release(struct device *gendev, void *r= es) if (this->restore_intx) pci_intx(dev, this->orig_intx); =20 - if (this->enabled && !this->pinned) + if (pci_is_enabled(dev) && !this->pinned) pci_disable_device(dev); } =20 @@ -446,14 +446,11 @@ int pcim_enable_device(struct pci_dev *pdev) dr =3D get_pci_dr(pdev); if (unlikely(!dr)) return -ENOMEM; - if (dr->enabled) - return 0; =20 rc =3D pci_enable_device(pdev); - if (!rc) { + if (!rc) pdev->is_managed =3D 1; - dr->enabled =3D 1; - } + return rc; } EXPORT_SYMBOL(pcim_enable_device); @@ -471,7 +468,7 @@ void pcim_pin_device(struct pci_dev *pdev) struct pci_devres *dr; =20 dr =3D find_pci_dr(pdev); - WARN_ON(!dr || !dr->enabled); + WARN_ON(!dr || !pci_is_enabled(pdev)); if (dr) dr->pinned =3D 1; } diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 5e4f377411ec..db2cc48f3d63 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2218,12 +2218,6 @@ void pci_disable_enabled_device(struct pci_dev *dev) */ void pci_disable_device(struct pci_dev *dev) { - struct pci_devres *dr; - - dr =3D find_pci_dr(dev); - if (dr) - dr->enabled =3D 0; - dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <=3D 0, "disabling already-disabled device"); =20 diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 2403c5a0ff7a..d7f00b43b098 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -821,7 +821,6 @@ static inline pci_power_t mid_pci_get_power_state(struc= t pci_dev *pdev) * then remove them from here. */ struct pci_devres { - unsigned int enabled:1; unsigned int pinned:1; unsigned int orig_intx:1; unsigned int restore_intx:1; --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 C0B1B14882B for ; Thu, 13 Jun 2024 11:50:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279459; cv=none; b=aky7YbViOscOg8Y9u4noV31lh1GF7y/5B1JYX2a8MyGGu/jhnO0YIIVBcRZMq9R+s0NRs/20n/IPtdO9/s9gp4c25YepUaaB2SbAWmq6smwJGnnMQYJOcpDIpM2REEtohWIJ0pGGWDS0CI6nlVQ21uQoe+tgewtOledOdbm/1FE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279459; c=relaxed/simple; bh=pCQsKw5JTEucg35lXycT49o+c0HiqjktK3rXy0MLKzM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pge9Etxb+tuXwyoJoXQa4t11/GGhcGsD+mPhOJ5Hvh92m5n9fV9Xg6yHHfuOe43IlAKnoUwoN0ZPmgKJtiHl5JZKjrsWLgR/fhDzNNQKznm/atyr9qSxC+GAhm79kcX9SVJl/AV30TgP6LYhGhUETRnVt4RuaVtYw3/ruDog9HI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=h8Cfdo2Q; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="h8Cfdo2Q" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279457; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cVfgCjKnb7mN7tw6g3EsRXSFrlAe3Q6EtTbj0A26jHk=; b=h8Cfdo2QREHXS+XW3Qn/4dbxXQ8yNMudE6eowX96DkN2B14bjGwJ+/vXef1Vp3ITsingyn RjM2GzEZTWi0M9e7oMMPAfRGHZ7Qrr4j4EscEtPbuv+YAOGih7/9IUWfXdH1M+Dc/YepWR 7+IWxZ2ntlaTOOArLXEqNOUmjUNtkao= Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-638-CL4W1an-M8SLHnHJNxvJ1Q-1; Thu, 13 Jun 2024 07:50:55 -0400 X-MC-Unique: CL4W1an-M8SLHnHJNxvJ1Q-1 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-2eab2099bbfso1652251fa.2 for ; Thu, 13 Jun 2024 04:50:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279454; x=1718884254; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cVfgCjKnb7mN7tw6g3EsRXSFrlAe3Q6EtTbj0A26jHk=; b=ZBXGhodvG3vULugAE8UQVYTFdUrhVa7kDwrcsw2UUikkeARH6OvsEKCdLF1tWHgMn/ 9OWmI/SyUdS0iu+t0FVa+xbIklYqbvODaac1sLAchJDO0eP+kqacHYczHw00rm2MfwDM c33JXSxuF69SB6okpm4FehJSA3GChOUC/z6AZrdVfhBv2tDOzsXG/19w5GTYd+S0jKNN sZWBSdy4ZzBpI6XcppKbawpHvfPsS8C0kenabeP7kGw2tB8Zf+PB58pzYswjfU+ptcWQ V/NZgJr2i0Zy2+jnshKjHpY/skkRSH09H1vJCqX64z/d/q9ZrBgBcQgkYl9Gqb2ublrK 5Ggw== X-Forwarded-Encrypted: i=1; AJvYcCUwBCoAXQLMuld0fV355z1KHbWMG6CfRZL0pf1itxSgmhQz3k7JZRfoodEnJxePApAz1OdBLvEqdDz0NRkyg3HeMQgLumgUeE324VN/ X-Gm-Message-State: AOJu0YyhJVBPTyVLpAPqrHbluxLeFPTgskZeIXlFYm4OBITZZt4/qyRq bPDkuYjk3CZgn220iUefjKDD0TiDTEEhaGHbGf95qaX4qFyR3u3KbR/HG1/p6HOTuttgYY4KSbL D4ruGmLYd7ckGyuV49C1ZBdawVWpqGmUljUKYrwJ1WR2KgGbVPqTW1YwLXoI1NYq8a89bvA== X-Received: by 2002:a05:6512:ac3:b0:52c:9e80:387a with SMTP id 2adb3069b0e04-52ca59e9d11mr72166e87.0.1718279453801; Thu, 13 Jun 2024 04:50:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH3dSZsb8l1qGTBxCy/CxpKhrwqSIxs3fGuB9Mz5Llia8E6mlHaM5OG7LJkgO+LSWP5CNnfZA== X-Received: by 2002:a05:6512:ac3:b0:52c:9e80:387a with SMTP id 2adb3069b0e04-52ca59e9d11mr72136e87.0.1718279453401; Thu, 13 Jun 2024 04:50:53 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:53 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 08/13] PCI: Move pinned status bit to struct pci_dev Date: Thu, 13 Jun 2024 13:50:21 +0200 Message-ID: <20240613115032.29098-9-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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 bit describing whether the PCI device is currently pinned is stored in struct pci_devres. To clean up and simplify the PCI devres API, it's better if this information is stored in struct pci_dev. This will later permit simplifying pcim_enable_device(). Move the 'pinned' boolean bit to struct pci_dev. Restructure bits in struct pci_dev so the pm / pme fields are next to each other. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 14 ++++---------- drivers/pci/pci.h | 1 - include/linux/pci.h | 3 ++- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 643e3a94a1d6..84caa0034813 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -403,7 +403,7 @@ static void pcim_release(struct device *gendev, void *r= es) if (this->restore_intx) pci_intx(dev, this->orig_intx); =20 - if (pci_is_enabled(dev) && !this->pinned) + if (pci_is_enabled(dev) && !dev->pinned) pci_disable_device(dev); } =20 @@ -459,18 +459,12 @@ EXPORT_SYMBOL(pcim_enable_device); * pcim_pin_device - Pin managed PCI device * @pdev: PCI device to pin * - * Pin managed PCI device @pdev. Pinned device won't be disabled on - * driver detach. @pdev must have been enabled with - * pcim_enable_device(). + * Pin managed PCI device @pdev. Pinned device won't be disabled on driver + * detach. @pdev must have been enabled with pcim_enable_device(). */ void pcim_pin_device(struct pci_dev *pdev) { - struct pci_devres *dr; - - dr =3D find_pci_dr(pdev); - WARN_ON(!dr || !pci_is_enabled(pdev)); - if (dr) - dr->pinned =3D 1; + pdev->pinned =3D true; } EXPORT_SYMBOL(pcim_pin_device); =20 diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index d7f00b43b098..6e02ba1b5947 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -821,7 +821,6 @@ static inline pci_power_t mid_pci_get_power_state(struc= t pci_dev *pdev) * then remove them from here. */ struct pci_devres { - unsigned int pinned:1; unsigned int orig_intx:1; unsigned int restore_intx:1; unsigned int mwi:1; diff --git a/include/linux/pci.h b/include/linux/pci.h index fb004fd4e889..0c19f0717899 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -367,10 +367,11 @@ struct pci_dev { this is D0-D3, D0 being fully functional, and D3 being off. */ u8 pm_cap; /* PM capability offset */ - unsigned int imm_ready:1; /* Supports Immediate Readiness */ unsigned int pme_support:5; /* Bitmask of states from which PME# can be generated */ unsigned int pme_poll:1; /* Poll device's PME status bit */ + unsigned int pinned:1; /* Whether this dev is pinned */ + unsigned int imm_ready:1; /* Supports Immediate Readiness */ unsigned int d1_support:1; /* Low power state D1 is supported */ unsigned int d2_support:1; /* Low power state D2 is supported */ unsigned int no_d1d2:1; /* D1 and D2 are forbidden */ --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 11D2C148FEC for ; Thu, 13 Jun 2024 11:50:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279461; cv=none; b=V23Z2O1qtCmP2VxObCJ1cGdaXHZOdTqNEHTrzEC96uuihEf1/0EHMW9uJUqJXL499Ln5qBxoKWIMH5MYcheIe02EAkwW5AkTCbMVMmmnInlZ4nm5bPYhKBqhSFcqgK/VuR5VJE/0FwENzm3uYQP8Vz8qJ7GYM9JUvO05F9Ojyl8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279461; c=relaxed/simple; bh=MwGxicOTdTG5eVnpyN9pPmSLSF5RFtnjndQbPoUTe1Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qX4FW60yDaemorjnhpKBU3AZNfoMuxfdaX1zi+ZTRT+pfj9+pBhQFRcWTeA9EugRaQ0Wxgm8dGt+SjaflR+d2DWOJGZPzaacchT39ZrooqtFoj+5jdjbk1O1lZ4+R7BDPI11TsMZB62hY/UU+wT71pb4UmjmJ5NQawLhqUzG57M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZscrAdky; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZscrAdky" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279459; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Kh8+O2F7jgAFtGp/MUXcCP7XcWz8NnTH6l7tP4PdYbo=; b=ZscrAdkytuizXlRLjQRFgt3xqaczA8XoXyeoc2EdSLbWJowhfsWRnr7XmIlvKiwkhLd+V9 XDmscMIWP717gXFuGJvzrCk2+TVjyt7TxCGwdO7DarrIBNtIwyBQL+1jVc7hipjNVQyC51 Auif768ud4poYzjDe64yV70yn3MVCP0= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-638-o7XV-61yM5Ki2tFy16rr1Q-1; Thu, 13 Jun 2024 07:50:55 -0400 X-MC-Unique: o7XV-61yM5Ki2tFy16rr1Q-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-36073543200so107624f8f.3 for ; Thu, 13 Jun 2024 04:50:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279454; x=1718884254; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Kh8+O2F7jgAFtGp/MUXcCP7XcWz8NnTH6l7tP4PdYbo=; b=SSm1IgE7QyGykZt2iEshll5pWVPZ4EbXUxEIadw6cZqOEyBQYGQB+GtEtc6oBqqjoS Jr/BLZPxEuPEVy9bus9oemw/CRnYlvOgk/t7F7gJoeuKDsEhBK5Gw3qM61gRrJDbp5p/ 0Tcm0yYwNaVYXO8UNAKVl3rqcTGlZ1hGk7xitLnB1xspIzeCDBuWJJAJqTwNVMFCDNhs Fr1F2y3LO/o+VzBEzhAxzqMo0A4H5f+gyQH9N+FASlwEXub8AOMpupbN8BgAbCrFmsdP 7nCfwLTRpPuaEuvqMWbqDV14MfWv84XhfvJaAB/QqOU0+VBxq73M94Q2FtMcocORZ4lS 2yJA== X-Forwarded-Encrypted: i=1; AJvYcCXvdSynWaFPSvtjRiYAYECJoiuZTrDBlyU5lHZbct0EzzCmHs1g9CXozQqAXNkPcfK3tNwOWjufGjjcPKA=@vger.kernel.org X-Gm-Message-State: AOJu0YzhDY0SZ59j6T/O1iBCJl8qB4jBpfF6N0848weHCFhZBaj8/TTy NXiOBIxWWblqvhBjTFgVmAaGaSm2W75Br0BrRcpWudJNtq/SpujZF6UUs6Qs59KgEqwy5CKmVpD NpUj65Q9SreE2JC+9xK8dTskTOXwYhdP19B7Yfhyz4KcRKrnK9TWhAs4LkOi/Xg== X-Received: by 2002:a5d:5f93:0:b0:360:6f5c:f5b3 with SMTP id ffacd0b85a97d-36079a6be34mr198411f8f.7.1718279454632; Thu, 13 Jun 2024 04:50:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFWMTtngZAgGMBECTjwydMRwQNlXRtCSyH8PxV40YfOhooXvc8s+2rU6f0CqUMNMS0veU1KZA== X-Received: by 2002:a5d:5f93:0:b0:360:6f5c:f5b3 with SMTP id ffacd0b85a97d-36079a6be34mr198391f8f.7.1718279454381; Thu, 13 Jun 2024 04:50:54 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:54 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 09/13] PCI: Give pcim_set_mwi() its own devres callback Date: Thu, 13 Jun 2024 13:50:22 +0200 Message-ID: <20240613115032.29098-10-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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" Managing pci_set_mwi() with devres can easily be done with its own callback, without the necessity to store any state about it in a device-related struct. Remove the MWI state from struct pci_devres. Give pcim_set_mwi() a separate devres-callback. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 29 ++++++++++++++++++----------- drivers/pci/pci.h | 1 - 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 84caa0034813..e8de93e95eb6 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -366,24 +366,34 @@ void __iomem *devm_pci_remap_cfg_resource(struct devi= ce *dev, } EXPORT_SYMBOL(devm_pci_remap_cfg_resource); =20 +static void __pcim_clear_mwi(void *pdev_raw) +{ + struct pci_dev *pdev =3D pdev_raw; + + pci_clear_mwi(pdev); +} + /** * pcim_set_mwi - a device-managed pci_set_mwi() - * @dev: the PCI device for which MWI is enabled + * @pdev: the PCI device for which MWI is enabled * * Managed pci_set_mwi(). * * RETURNS: An appropriate -ERRNO error value on error, or zero for succes= s. */ -int pcim_set_mwi(struct pci_dev *dev) +int pcim_set_mwi(struct pci_dev *pdev) { - struct pci_devres *dr; + int ret; =20 - dr =3D find_pci_dr(dev); - if (!dr) - return -ENOMEM; + ret =3D devm_add_action(&pdev->dev, __pcim_clear_mwi, pdev); + if (ret !=3D 0) + return ret; + + ret =3D pci_set_mwi(pdev); + if (ret !=3D 0) + devm_remove_action(&pdev->dev, __pcim_clear_mwi, pdev); =20 - dr->mwi =3D 1; - return pci_set_mwi(dev); + return ret; } EXPORT_SYMBOL(pcim_set_mwi); =20 @@ -397,9 +407,6 @@ static void pcim_release(struct device *gendev, void *r= es) struct pci_dev *dev =3D to_pci_dev(gendev); struct pci_devres *this =3D res; =20 - if (this->mwi) - pci_clear_mwi(dev); - if (this->restore_intx) pci_intx(dev, this->orig_intx); =20 diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 6e02ba1b5947..c355bb6a698d 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -823,7 +823,6 @@ static inline pci_power_t mid_pci_get_power_state(struc= t pci_dev *pdev) struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; - unsigned int mwi:1; }; =20 struct pci_devres *find_pci_dr(struct pci_dev *pdev); --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 2386A14884C for ; Thu, 13 Jun 2024 11:50:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279460; cv=none; b=PVLjILTp1NwkjoUnZCcEik6PI9aUbjejAiRSioKzA1n5Tt+Hc1cMCv5oKmko11Qi6AFjYFDdQXAzeIlUu3ySixyCYtN3D++8G+ceSWSp09JVnrc14Nzxxts76N4dvfuBMJwGrg5/bLVJMHxNYKnCZzEPLFNMFi/mFvL9YvYO1Hk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279460; c=relaxed/simple; bh=mgYu+PdBk2Uxe11LPVB6MI3T1q9XfYavbWG7yJM+v24=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SKVQTZiY7ayH807XaM1J9+D2wjB2IQzD+jI//PgWR7kmuQ1lkMFcRswRnYjzXvEJpqYMD5xgIIPbTj4fQsgakgiwX1ybrYhfqT5XIVZnL+3cKTN/wO/F++MTeKx9QNnUbs92RLIR1wmyschHKc17Gf7dMZYweY80iNK6iwCtH/E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=fALYxhiH; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fALYxhiH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279458; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UXtJP09mnY/VoBFabkO3D30y5UZGrDuagfM4gYTmfUM=; b=fALYxhiHA0EEJ0xam/mMjXgxyFZuiAUW+g6Kh8bGCfXHWxdAmSn2Pq/qck8Q5gnTbjFUzs xN9rwPMV6fSLaQ/99uTF0BlIP9pwralUIjzjRwNs5KEppQ5yAfqz2uWwTxqZj6gBhSxUcR Yz9DUFaJKJvbN+u6/yRQZrrOmR7XsPM= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-312-khmDtXtIPxqF1zx5JBpIBQ-1; Thu, 13 Jun 2024 07:50:56 -0400 X-MC-Unique: khmDtXtIPxqF1zx5JBpIBQ-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-42110b33fecso1617145e9.0 for ; Thu, 13 Jun 2024 04:50:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279456; x=1718884256; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UXtJP09mnY/VoBFabkO3D30y5UZGrDuagfM4gYTmfUM=; b=oeRMLF2KKOr9AeICBeSJS1Sq7bbtP8aH+JONgwcnW+6TnzA7mh20DWmVkd/exwY4Ft fBiD6+SMcCHd0nNUqMTY65qDoJ3RzIwA4phLN5dzUV19dvYUy5WjiAUa4jR6iaUkgJf6 RoF3y2G7DxvRqhCKM1YMM9EFbynNIrnTCqGDYPhOQizxhNsgjoGorapOYuxF2X+JoQk/ amcYoB2GXMwwHRmz/DhaJMakwYn+FwNcZaLUnq9xMkDNtcSR1mFu2a2SqE8VQN+BN3xZ ibHMVTQGDa0XJyBE1JdZuqSYsO6sPU5fkEXZeIACtJ5qCzAU+SAGUrTr+zK3gSX8TBGR q67w== X-Forwarded-Encrypted: i=1; AJvYcCWSIqsQloH+lMKyVueYPsPoA2Kic6hDAx0dRxfLuwW/6/6/S6QwZOw7eB5GsvWnax1b7Rk8KBHeFb6/XWf/kaIFzfGUJShXuuSLwFa7 X-Gm-Message-State: AOJu0Yz3gRnd/YaknPON5wycf/iAt7yRjao8+NHtIbhjXjl4r7n9J5wH ETuHr4tSzfpXXajhBeWZ45Ob11uWigideBc+GfwQlr3Iy0W+bNJKvt6PyfQrIjEmEIgTj0+wYM6 oS/kb/8dXN4GGuuFkB0iUdyY8mKeLgMEkI8s9q/RZb/VtcCqjYrSdlwMEfxhi+A== X-Received: by 2002:a05:6000:1aca:b0:35f:305d:d2c8 with SMTP id ffacd0b85a97d-36079a4c065mr196890f8f.3.1718279455711; Thu, 13 Jun 2024 04:50:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFg6sk3xnm8Dx/Hnr1TCgUs2mRnf7/ulhG/8nI0xWi/V2RpnZJUye6zcNtI5u+FrxW/ybB/fw== X-Received: by 2002:a05:6000:1aca:b0:35f:305d:d2c8 with SMTP id ffacd0b85a97d-36079a4c065mr196879f8f.3.1718279455407; Thu, 13 Jun 2024 04:50:55 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:55 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 10/13] PCI: Give pci_intx() its own devres callback Date: Thu, 13 Jun 2024 13:50:23 +0200 Message-ID: <20240613115032.29098-11-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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" pci_intx() is one of the functions that have "hybrid mode" (i.e., sometimes managed, sometimes not). Providing a separate pcim_intx() function with its own device resource and cleanup callback allows for removing further large parts of the legacy PCI devres implementation. As in the region-request-functions, pci_intx() has to call into its managed counterpart for backwards compatibility. As pci_intx() is an outdated function, pcim_intx() shall not be made visible to drivers via a public API. Implement pcim_intx() with its own device resource. Make pci_intx() call pcim_intx() in the managed case. Remove the now surplus function find_pci_dr(). Signed-off-by: Philipp Stanner Reported-by: Ashish Kalra Tested-by: Ashish Kalra --- drivers/pci/devres.c | 76 ++++++++++++++++++++++++++++++++++++-------- drivers/pci/pci.c | 21 ++++++------ drivers/pci/pci.h | 13 ++++---- 3 files changed, 80 insertions(+), 30 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index e8de93e95eb6..7b72c952a9e5 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -42,6 +42,11 @@ struct pcim_iomap_devres { void __iomem *table[PCI_STD_NUM_BARS]; }; =20 +/* Used to restore the old intx state on driver detach. */ +struct pcim_intx_devres { + int orig_intx; +}; + enum pcim_addr_devres_type { /* Default initializer. */ PCIM_ADDR_DEVRES_TYPE_INVALID, @@ -397,32 +402,75 @@ int pcim_set_mwi(struct pci_dev *pdev) } EXPORT_SYMBOL(pcim_set_mwi); =20 + static inline bool mask_contains_bar(int mask, int bar) { return mask & BIT(bar); } =20 -static void pcim_release(struct device *gendev, void *res) +static void pcim_intx_restore(struct device *dev, void *data) { - struct pci_dev *dev =3D to_pci_dev(gendev); - struct pci_devres *this =3D res; + struct pci_dev *pdev =3D to_pci_dev(dev); + struct pcim_intx_devres *res =3D data; =20 - if (this->restore_intx) - pci_intx(dev, this->orig_intx); + pci_intx(pdev, res->orig_intx); +} =20 - if (pci_is_enabled(dev) && !dev->pinned) - pci_disable_device(dev); +static struct pcim_intx_devres *get_or_create_intx_devres(struct device *d= ev) +{ + struct pcim_intx_devres *res; + + res =3D devres_find(dev, pcim_intx_restore, NULL, NULL); + if (res) + return res; + + res =3D devres_alloc(pcim_intx_restore, sizeof(*res), GFP_KERNEL); + if (res) + devres_add(dev, res); + + return res; } =20 -/* - * TODO: After the last four callers in pci.c are ported, find_pci_dr() - * needs to be made static again. +/** + * pcim_intx - managed pci_intx() + * @pdev: the PCI device to operate on + * @enable: boolean: whether to enable or disable PCI INTx + * + * Returns: 0 on success, -ENOMEM on error. + * + * Enables/disables PCI INTx for device @pdev. + * Restores the original state on driver detach. */ -struct pci_devres *find_pci_dr(struct pci_dev *pdev) +int pcim_intx(struct pci_dev *pdev, int enable) { - if (pci_is_managed(pdev)) - return devres_find(&pdev->dev, pcim_release, NULL, NULL); - return NULL; + u16 pci_command, new; + struct pcim_intx_devres *res; + + res =3D get_or_create_intx_devres(&pdev->dev); + if (!res) + return -ENOMEM; + + res->orig_intx =3D !enable; + + pci_read_config_word(pdev, PCI_COMMAND, &pci_command); + + if (enable) + new =3D pci_command & ~PCI_COMMAND_INTX_DISABLE; + else + new =3D pci_command | PCI_COMMAND_INTX_DISABLE; + + if (new !=3D pci_command) + pci_write_config_word(pdev, PCI_COMMAND, new); + + return 0; +} + +static void pcim_release(struct device *gendev, void *res) +{ + struct pci_dev *dev =3D to_pci_dev(gendev); + + if (pci_is_enabled(dev) && !dev->pinned) + pci_disable_device(dev); } =20 static struct pci_devres *get_pci_dr(struct pci_dev *pdev) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index db2cc48f3d63..1b4832a60047 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -4443,6 +4443,16 @@ void pci_intx(struct pci_dev *pdev, int enable) { u16 pci_command, new; =20 + /* + * This is done for backwards compatibility, because the old PCI devres + * API had a mode in which this function became managed if the dev had + * been enabled with pcim_enable_device() instead of pci_enable_device(). + */ + if (pci_is_managed(pdev)) { + WARN_ON_ONCE(pcim_intx(pdev, enable) !=3D 0); + return; + } + pci_read_config_word(pdev, PCI_COMMAND, &pci_command); =20 if (enable) @@ -4450,17 +4460,8 @@ void pci_intx(struct pci_dev *pdev, int enable) else new =3D pci_command | PCI_COMMAND_INTX_DISABLE; =20 - if (new !=3D pci_command) { - struct pci_devres *dr; - + if (new !=3D pci_command) pci_write_config_word(pdev, PCI_COMMAND, new); - - dr =3D find_pci_dr(pdev); - if (dr && !dr->restore_intx) { - dr->restore_intx =3D 1; - dr->orig_intx =3D !enable; - } - } } EXPORT_SYMBOL_GPL(pci_intx); =20 diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index c355bb6a698d..9e87528f1157 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -816,16 +816,17 @@ static inline pci_power_t mid_pci_get_power_state(str= uct pci_dev *pdev) * there's no need to track it separately. pci_devres is initialized * when a device is enabled using managed PCI device enable interface. * - * TODO: Struct pci_devres and find_pci_dr() only need to be here because - * they're used in pci.c. Port or move these functions to devres.c and - * then remove them from here. + * TODO: Struct pci_devres only needs to be here because they're used in p= ci.c. + * Port or move these functions to devres.c and then remove them from here. */ struct pci_devres { - unsigned int orig_intx:1; - unsigned int restore_intx:1; + /* + * TODO: + * This struct is now surplus. Remove it by refactoring pci/devres.c + */ }; =20 -struct pci_devres *find_pci_dr(struct pci_dev *pdev); +int pcim_intx(struct pci_dev *dev, int enable); =20 int pcim_request_region(struct pci_dev *pdev, int bar, const char *name); int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const cha= r *name); --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 14699148FED for ; Thu, 13 Jun 2024 11:50:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279461; cv=none; b=f03KoaXkcWhkphDm+B3PxS7toZakIkSaKwS8aG7kdCAp8ny3ERXy/6oZetUJXz71IAJa45BfHzHTIKwWICIByKqIeBgk44B32pM+Wzps6tqt0k/jsyI2hzu7mysnHhKK3aQowdpfcqeUEpp9tbWCseQzYGdDfMK03dbyCpIREVI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279461; c=relaxed/simple; bh=r0grzUmBcSDYCaYgn9ysXFCKA4yu6vMMhBtQa+BDezY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YAj+Wy8J/u6flc6jCdB/TlImb0XVHB2GLl3jqDko4eEm6Qms5I0m6udOFu9CQBIgygemaW9XtI+gUgI8hddrMdMYeP9UYjXZeF0i9KgyWl42hk9jhIfwLSgXnnlbupbf8BeylUP+KPlJ2hWlX2kyShpgfSefK2vOByGKoHik1ng= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=HVF7FhFw; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="HVF7FhFw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279459; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DkCOJZ/wPtUvxbKR6iZK0j94vWla8EApzDXg+GJsTLg=; b=HVF7FhFw9OXidnm3QlIMvIgeUOdvT53c33TnYvYyfyEaN7MsNGp+q1rAPIkl9gJPDgPkV9 Agne8n0HMabXQDpiS4UHSpir8Pf3OpQS8mQVlW18032GBL9D8sk6WgJg4q7rxLbiRvoZmh kS+yay74tJnOMg2XEjqczx2Mz8neTCY= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-583-6ku1QIayMXKhF4rQbZLRUA-1; Thu, 13 Jun 2024 07:50:57 -0400 X-MC-Unique: 6ku1QIayMXKhF4rQbZLRUA-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4218118d1efso1513535e9.2 for ; Thu, 13 Jun 2024 04:50:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279457; x=1718884257; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DkCOJZ/wPtUvxbKR6iZK0j94vWla8EApzDXg+GJsTLg=; b=oL0C5ObBxO2C/ctzUcdV6NTM3SAhbjZeHy6piKU+pMT9R/hZAUZEXHfAd0uk0tuxC8 YwPk4UDenUMH50FX6b7pSl0MLrimtexju5iOa7qbiHYd3RTaJfM1g2qwwxBjtYRL3Pa9 YJJvhztr/gMEx3oyaVpZzn9Hkv/Dj+ShUt9NxpyZw4gFt+3XVzn/eiUUQ9P0xuiHq25O 41Sm+5OvSJZG5uZSZOO0nobsE11lV3C+4cssrPOVjBOKFvPTkzrPKJMKGKT/NMnn1UGk FStBhD56hZSEy4ZBwZ3Z/F7PcWzk/GisE0rUVeYAt6D1x6PsvudirR0Sij+TsTSMXv10 Q0bQ== X-Forwarded-Encrypted: i=1; AJvYcCWtyHv5YVizHuHXKl+POK9P7KDJ+6qUzq5+6JmWMnaJ0kgIay3Fc2R3fcXXC9NxKODTsRRdNOpa92waV8/5lLlonW3OXM0v9elFqDEs X-Gm-Message-State: AOJu0YwL/NEFcLmI/0zRvC5MMWOXHQ95r9+9laVe7CeihInj2n/doX7W N5CvLNS8VJ1TE/tjUxX+yWuc0MkSRC1uBNDRli577Xj5EkxQrkYNB2l8bmSCKyB2FvDywPVaVmU oDC2H1S1x/KJ7wDTjmExSnnhNHdxhmmodAo2QJBQ5uHA/zi4JhzMTE2o7xt3K7Q== X-Received: by 2002:a05:6000:1845:b0:35f:e38:6758 with SMTP id ffacd0b85a97d-36079a587a3mr156395f8f.7.1718279456841; Thu, 13 Jun 2024 04:50:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF2TEvywhfauVMW37zv3JfoMBzZNIERzrEKPTEghMv7QdmvZthOHWTnGztKiTfV2ZqUSTTNVg== X-Received: by 2002:a05:6000:1845:b0:35f:e38:6758 with SMTP id ffacd0b85a97d-36079a587a3mr156374f8f.7.1718279456563; Thu, 13 Jun 2024 04:50:56 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:56 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 11/13] PCI: Remove legacy pcim_release() Date: Thu, 13 Jun 2024 13:50:24 +0200 Message-ID: <20240613115032.29098-12-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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" Thanks to preceding cleanup steps, pcim_release() is now not needed anymore and can be replaced by pcim_disable_device(), which is the exact counterpart to pcim_enable_device(). This permits removing further parts of the old PCI devres implementation. Replace pcim_release() with pcim_disable_device(). Remove the now surplus function get_pci_dr(). Remove the struct pci_devres from pci.h. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 53 +++++++++++++++++++++----------------------- drivers/pci/pci.h | 16 ------------- 2 files changed, 25 insertions(+), 44 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 7b72c952a9e5..37ac8fd37291 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -465,48 +465,45 @@ int pcim_intx(struct pci_dev *pdev, int enable) return 0; } =20 -static void pcim_release(struct device *gendev, void *res) +static void pcim_disable_device(void *pdev_raw) { - struct pci_dev *dev =3D to_pci_dev(gendev); - - if (pci_is_enabled(dev) && !dev->pinned) - pci_disable_device(dev); -} - -static struct pci_devres *get_pci_dr(struct pci_dev *pdev) -{ - struct pci_devres *dr, *new_dr; - - dr =3D devres_find(&pdev->dev, pcim_release, NULL, NULL); - if (dr) - return dr; + struct pci_dev *pdev =3D pdev_raw; =20 - new_dr =3D devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL); - if (!new_dr) - return NULL; - return devres_get(&pdev->dev, new_dr, NULL, NULL); + if (!pdev->pinned) + pci_disable_device(pdev); } =20 /** * pcim_enable_device - Managed pci_enable_device() * @pdev: PCI device to be initialized * - * Managed pci_enable_device(). + * Returns: 0 on success, negative error code on failure. + * + * Managed pci_enable_device(). Device will automatically be disabled on + * driver detach. */ int pcim_enable_device(struct pci_dev *pdev) { - struct pci_devres *dr; - int rc; + int ret; =20 - dr =3D get_pci_dr(pdev); - if (unlikely(!dr)) - return -ENOMEM; + ret =3D devm_add_action(&pdev->dev, pcim_disable_device, pdev); + if (ret !=3D 0) + return ret; =20 - rc =3D pci_enable_device(pdev); - if (!rc) - pdev->is_managed =3D 1; + /* + * We prefer removing the action in case of an error over + * devm_add_action_or_reset() because the later could theoretically be + * disturbed by users having pinned the device too soon. + */ + ret =3D pci_enable_device(pdev); + if (ret !=3D 0) { + devm_remove_action(&pdev->dev, pcim_disable_device, pdev); + return ret; + } =20 - return rc; + pdev->is_managed =3D true; + + return ret; } EXPORT_SYMBOL(pcim_enable_device); =20 diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 9e87528f1157..e51e6fa79fcc 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -810,22 +810,6 @@ static inline pci_power_t mid_pci_get_power_state(stru= ct pci_dev *pdev) } #endif =20 -/* - * Managed PCI resources. This manages device on/off, INTx/MSI/MSI-X - * on/off and BAR regions. pci_dev itself records MSI/MSI-X status, so - * there's no need to track it separately. pci_devres is initialized - * when a device is enabled using managed PCI device enable interface. - * - * TODO: Struct pci_devres only needs to be here because they're used in p= ci.c. - * Port or move these functions to devres.c and then remove them from here. - */ -struct pci_devres { - /* - * TODO: - * This struct is now surplus. Remove it by refactoring pci/devres.c - */ -}; - int pcim_intx(struct pci_dev *dev, int enable); =20 int pcim_request_region(struct pci_dev *pdev, int bar, const char *name); --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 2AB20145B08 for ; Thu, 13 Jun 2024 11:58:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279914; cv=none; b=qi2NWESTaIJkg3eMZa5QZ3QaMqmJW6YaluMf0sc0aQGlwUjLiCml9ObmrCfWRbFQ0ECRFSw/0f1YPGUyMzWZHFmJ6Yz26fcZHJgUITHOXDxvCKi3+N9friq76ZdaXRya0RPw4em8rSsJsHabD/FSuM1W3Apq5wgkK3NYsc94neg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279914; c=relaxed/simple; bh=XM+HU7hb6QVEb253YkrStrtEiG/dnPO/pYNia1HjcLU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qEynPlSNF1yFPHFVSwQiCVdlwX9LnW2aQS1f2xB5UIeh78G6YET1Tnbd5bxXprWwaAmzzmciQc3tahObuBexT5I+tnUtMsuWT1xG0Pn/7Mvl9MbLznfukKISDAsPtykioDEiHyYRELQgsDvRjs2VjoSpJJQSwcd45fKOlUGf/q4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZB91pbcG; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZB91pbcG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279911; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=000KYz6ID2QoUMG52/WJvZNJGOa8UH1A+/S3718oumg=; b=ZB91pbcGaGYDXYoHox4qb0YYoXdv5LDFmcpUB60aJt96TD8leyelMpQnD7Z8grIje0l+Mb XGhb2+R3bS+6r7jllDRa2BHZzAAfxGZ4FdLCYaJTvaxxLmD99YkCeTyL1qPJ+g08RDRoiu b85Ggv52urVfV3JwerPlL8DG7uRAWqI= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-507-JTwOMPR3OAKIYnXitB3iEA-1; Thu, 13 Jun 2024 07:58:30 -0400 X-MC-Unique: JTwOMPR3OAKIYnXitB3iEA-1 Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-57cb9286e9eso53659a12.0 for ; Thu, 13 Jun 2024 04:58:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279908; x=1718884708; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=000KYz6ID2QoUMG52/WJvZNJGOa8UH1A+/S3718oumg=; b=eZSmycbXtdQW3+7qLWhx14kfP45bis7chp7CVAvPmiU8p/o/RhdKt53NGYxNydBZBN Aax/pecQN/NpOHNskVzTfhvwGvba+39CpTnbwD0oK2uxul/q+568cVaQiRa/EMg0/s0L kt4YKsFbdT6G7v4t8r74wDDSPS/zzI98lM4gz3QnDWGmuTAtwQIU9Q4BPP3LwIWwuslZ wJiKumclBhBMG9oArdjO1F27hP7cZ1mZbK+dR96ZQeWqdxZj+k8pDVlZznomcOKGIcmq R3ZQaiqMuRBK9aab9uMZlrqRonA3tPdG8U2Woz7f+m6lTexaAFeJNvRboktdytLRwhq5 fyhA== X-Forwarded-Encrypted: i=1; AJvYcCWP0kLIEW4bqjt/bQjBmNpPNukxq+KV57YvFPcYsgehP9xwudZpKWAh+t7tXqBNQGDW3tiTFCwTYrnDWlAo1vTlwCj7xvcSrTR9Uo5Y X-Gm-Message-State: AOJu0YwRJ6m1G4HBLnfBz/oq/9PQQg6Vl2peEo2/UTKZZghOrOOOBjXg C5qE4JvS7EQ0HyV0VdTybajWuNEwEujizqpzcZLW9B04BVfpd/7L4g6ms51SNsrp6iycwS1oC83 3WaEb30zKiU+iecqwPVn58TKT5AqQUJNsiZ4cEUtCopVLotIceqdyMTjcuikHUpVMJhm/xw== X-Received: by 2002:a50:aa94:0:b0:57c:61b3:bdc1 with SMTP id 4fb4d7f45d1cf-57cbc21968amr110944a12.3.1718279908538; Thu, 13 Jun 2024 04:58:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGnhuJuSAKliDxcFbokg6egscMNv+7jsPTJ86B+AWP/UZU7fL3bpTZJKANl4Bt8/kMHQDmyzw== X-Received: by 2002:a05:6512:ac3:b0:52c:9e80:387a with SMTP id 2adb3069b0e04-52ca59e9d11mr72313e87.0.1718279457545; Thu, 13 Jun 2024 04:50:57 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:57 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 12/13] PCI: Add pcim_iomap_range() Date: Thu, 13 Jun 2024 13:50:25 +0200 Message-ID: <20240613115032.29098-13-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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 only managed mapping function currently is pcim_iomap() which doesn't allow for mapping an area starting at a certain offset, which many drivers want. Add pcim_iomap_range() as an exported function. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/pci.h | 2 ++ 2 files changed, 46 insertions(+) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 37ac8fd37291..2f0379a4e58f 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -1015,3 +1015,47 @@ void pcim_iounmap_regions(struct pci_dev *pdev, int = mask) } } EXPORT_SYMBOL(pcim_iounmap_regions); + +/** + * pcim_iomap_range - Create a ranged __iomap mapping within a PCI BAR + * @pdev: PCI device to map IO resources for + * @bar: Index of the BAR + * @offset: Offset from the begin of the BAR + * @len: Length in bytes for the mapping + * + * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure. + * + * Creates a new IO-Mapping within the specified @bar, ranging from @offse= t to + * @offset + @len. + * + * The mapping will automatically get unmapped on driver detach. If desire= d, + * release manually only with pcim_iounmap(). + */ +void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len) +{ + void __iomem *mapping; + struct pcim_addr_devres *res; + + res =3D pcim_addr_devres_alloc(pdev); + if (!res) + return IOMEM_ERR_PTR(-ENOMEM); + + mapping =3D pci_iomap_range(pdev, bar, offset, len); + if (!mapping) { + pcim_addr_devres_free(res); + return IOMEM_ERR_PTR(-EINVAL); + } + + res->type =3D PCIM_ADDR_DEVRES_TYPE_MAPPING; + res->baseaddr =3D mapping; + + /* + * Ranged mappings don't get added to the legacy-table, since the table + * only ever keeps track of whole BARs. + */ + + devres_add(&pdev->dev, res); + return mapping; +} +EXPORT_SYMBOL(pcim_iomap_range); diff --git a/include/linux/pci.h b/include/linux/pci.h index 0c19f0717899..98893a89bb5b 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2303,6 +2303,8 @@ int pcim_iomap_regions(struct pci_dev *pdev, int mask= , const char *name); int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, const char *name); void pcim_iounmap_regions(struct pci_dev *pdev, int mask); +void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len); =20 extern int pci_pci_problems; #define PCIPCI_FAIL 1 /* No PCI PCI DMA */ --=20 2.45.0 From nobody Tue Dec 16 16:37:14 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 9E7F2149C41 for ; Thu, 13 Jun 2024 11:51:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279464; cv=none; b=T6sVCp74F+BgTQA22fvfct6E9tWolzx+Oem54YRVN2Crie9T7xAgyaNPibGDaYgGyldIvz6DwPmyW6Aoiq4axju29Bx2zA0fypeeLg24w+LVvqUFC1BdGaHNbXZ/PIdeQ6vxB3deSsPaajQ2URmJvM6klJ8qrtdgAILfz7WocRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718279464; c=relaxed/simple; bh=iGO9d2tDV8jgMXAb9ljo4BmxYhAXls7ncq8pchC9GTM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n8vmr/dnBK3qiBveU1jV/fUqDdUczZlFjNgfOIQgMBmeUUrByw/z8GSXFG+v7+9udrgEYwjM5cZjt5NOQ8tJn5cEr1bJmnxjfB9K8OQSD4ydmVUP8nNlzvy246ocUQ9OD6WA48Jo/EwFTDaXa1+Aydl0JFGg5hKA9lpPG87QYeU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=iJdn2JSK; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="iJdn2JSK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718279461; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9WQJKGGQ63IpYpwAqks0j3zwgngy8EFW/hBQkkDLpOw=; b=iJdn2JSKo5UmFrR0ugN7WDNJnADI+q/iHS3wsxwrBwj0luJgq6wF2hvbiW1zzZZ+ymA6Wz BukKK2Twj8vyoNVea4PtlWEJ7+8+ZXyUV/BEQ64F5VHv5KngFs0QzmihAov9VIH7jq2Cpg qqx75VA8vuHAGvh64DHELqNCkzwrGmk= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-35-EwyvKO4VMD2z4flqWhLWnQ-1; Thu, 13 Jun 2024 07:51:00 -0400 X-MC-Unique: EwyvKO4VMD2z4flqWhLWnQ-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-36073543200so107649f8f.3 for ; Thu, 13 Jun 2024 04:51:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718279459; x=1718884259; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9WQJKGGQ63IpYpwAqks0j3zwgngy8EFW/hBQkkDLpOw=; b=mpXvRBPiGuUgletKKDPnfibWNflYy8LlHmlU6wi4wAM2QWVfBn9TLHh5XFbVSiJZaD lqZ07RgyUTmu9Bxu9Phn9muc4mb/0rbdTl0B6uqCB1NW3W1D1RVNTt4uIbUeRfjJRDdH 6sg4mlVrnE97m0bsioSQW/IU8dSDStbOtII/O2kGwodfGfJmEmawygbZ9ttrclWtwFio A40IyD2UPJO5nfslLwHVzCq0TWpfcN9KXeTxcI6dpWT3tK5VcCEggpRHPokFeGmZfYUi aOO5qhdjT1U92VegZOr24HgDAgJf/Cl+MQMJCmHEJmCsPJLmYNCsZPmyC7CFpwz3lC1c 9BXg== X-Forwarded-Encrypted: i=1; AJvYcCX87m7Vyfxd8+/+c+MsbaWPnWPQ/wmjz1GXXedZCG9AAWQwTkDVugvUdJJcpj+xfTkMso6DVEM3Je/b7FE=@vger.kernel.org X-Gm-Message-State: AOJu0YwlXjiSle2UPxLbTv4FhCru8Wsmm9fdo/lcuBwgYc1XQrdRsb7P FwklV/07v0Yr9INWFvHiRm/RcJOY89XKLN+o3SUZSa1DQAIhOsKvuEilonwQJm1kT6lFCSNWDGs Bz/AOyWfTuPkyzMOlYoRo41DxZHdaEG0firvXSrX5vf+VDfWpvGSqI8Lyp44Tzg== X-Received: by 2002:a5d:6da4:0:b0:35f:2584:7714 with SMTP id ffacd0b85a97d-36079a55d96mr164404f8f.5.1718279459403; Thu, 13 Jun 2024 04:50:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IElWKl4RSn/FE/WxBDl2/l3vU5Ab6FM43jl4nz2rwpq5A2nGMHBM+4ko0mCUeuR7gvoXMoyQA== X-Received: by 2002:a5d:6da4:0:b0:35f:2584:7714 with SMTP id ffacd0b85a97d-36079a55d96mr164394f8f.5.1718279459138; Thu, 13 Jun 2024 04:50:59 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3607509c883sm1510620f8f.29.2024.06.13.04.50.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 04:50:58 -0700 (PDT) From: Philipp Stanner To: Hans de Goede , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Bjorn Helgaas , Sam Ravnborg , dakr@redhat.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Philipp Stanner Subject: [PATCH v9 13/13] drm/vboxvideo: fix mapping leaks Date: Thu, 13 Jun 2024 13:50:26 +0200 Message-ID: <20240613115032.29098-14-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240613115032.29098-1-pstanner@redhat.com> References: <20240613115032.29098-1-pstanner@redhat.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" When the PCI devres API was introduced to this driver, it was wrongly assumed that initializing the device with pcim_enable_device() instead of pci_enable_device() will make all PCI functions managed. This is wrong and was caused by the quite confusing PCI devres API in which some, but not all, functions become managed that way. The function pci_iomap_range() is never managed. Replace pci_iomap_range() with the actually managed function pcim_iomap_range(). Fixes: 8558de401b5f ("drm/vboxvideo: use managed pci functions") Signed-off-by: Philipp Stanner Reviewed-by: Hans de Goede --- drivers/gpu/drm/vboxvideo/vbox_main.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/drivers/gpu/drm/vboxvideo/vbox_main.c b/drivers/gpu/drm/vboxvi= deo/vbox_main.c index 42c2d8a99509..d4ade9325401 100644 --- a/drivers/gpu/drm/vboxvideo/vbox_main.c +++ b/drivers/gpu/drm/vboxvideo/vbox_main.c @@ -42,12 +42,11 @@ static int vbox_accel_init(struct vbox_private *vbox) /* Take a command buffer for each screen from the end of usable VRAM. */ vbox->available_vram_size -=3D vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE; =20 - vbox->vbva_buffers =3D pci_iomap_range(pdev, 0, - vbox->available_vram_size, - vbox->num_crtcs * - VBVA_MIN_BUFFER_SIZE); - if (!vbox->vbva_buffers) - return -ENOMEM; + vbox->vbva_buffers =3D pcim_iomap_range( + pdev, 0, vbox->available_vram_size, + vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE); + if (IS_ERR(vbox->vbva_buffers)) + return PTR_ERR(vbox->vbva_buffers); =20 for (i =3D 0; i < vbox->num_crtcs; ++i) { vbva_setup_buffer_context(&vbox->vbva_info[i], @@ -116,11 +115,10 @@ int vbox_hw_init(struct vbox_private *vbox) DRM_INFO("VRAM %08x\n", vbox->full_vram_size); =20 /* Map guest-heap at end of vram */ - vbox->guest_heap =3D - pci_iomap_range(pdev, 0, GUEST_HEAP_OFFSET(vbox), - GUEST_HEAP_SIZE); - if (!vbox->guest_heap) - return -ENOMEM; + vbox->guest_heap =3D pcim_iomap_range(pdev, 0, + GUEST_HEAP_OFFSET(vbox), GUEST_HEAP_SIZE); + if (IS_ERR(vbox->guest_heap)) + return PTR_ERR(vbox->guest_heap); =20 /* Create guest-heap mem-pool use 2^4 =3D 16 byte chunks */ vbox->guest_pool =3D devm_gen_pool_create(vbox->ddev.dev, 4, -1, --=20 2.45.0