From nobody Thu Feb 12 17:27:25 2026 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 4EED2745F4 for ; Mon, 10 Jun 2024 09:32:06 +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=1718011927; cv=none; b=D0ejyE3FmJzvNYSVAw3IzpZQXtSTNDyESnEL7U8u9Oa4bbEIYvTLGiJdlKda4Yf6EWacz79VOOk5uQa+IGM8KBDfx+Ip0uPajbyoOgC174KOW+sSYa53qIHx/vQlMlQ5aIVxC/9IvWWPafpVmDDctNrk44JSIU6pQm/M+VrBERw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011927; c=relaxed/simple; bh=uxWR+cyvQnS6F+Ku7i7pYs3ZA+OagBoxvbut16jkiT8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dTUnFdtUsAWVUokmKMBsWb0UfRjW5xKa+FgsVpkxfecw+EumRAcqMum1Fv8lhkw+d7kX5+Ff5vsn+4DcfWP1QinaWOvBeESheCm285mAjuDgnWm+SRQdkwZ6aID0zjiXflP/bbU05awVnMZKJaK6CTXtntq9PnrHjqWtQ1jAigA= 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=bjAjzY4d; 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="bjAjzY4d" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011925; 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=bjAjzY4dZQ04UhdkOLzNLGH6i7y/hSEUK/b9wpeeeBKmNcA0GviTq3a0pEDm31G+5q27lZ W4RvMcDPnSi23LgKCne3ZHuHCAo4WS82SkvOSNs0F/Hw6ywvuMDgtVdjXkkiR+xyO3SoF6 yIcRLI3NJbgzaFulQDN4gJUgySwpqvA= 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-433-XeSfhOfXOhiqCldYe3d3Xg-1; Mon, 10 Jun 2024 05:32:02 -0400 X-MC-Unique: XeSfhOfXOhiqCldYe3d3Xg-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4215f69475cso3516505e9.3 for ; Mon, 10 Jun 2024 02:32:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011921; x=1718616721; 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=o7TIt4yvTdaEEmBNiiNSu3upEPluLOCiO2kgWuoMq91/dNmeeJ2xJImq9d6R+QLOrM PwYX/i6znPKwTtDgtf61HsuV0GUkk/qhk7R7P9BEXV6xFZljLYu+kRwjT+s0CEmJSBco dP6rmBLbmRBatW3Az/fKL2gc567OqWdHORF704NxXXMI0rEulItjHL4RPwBLhmNzYShn 8dEbzBKUDh6UUxH1QhKxqwtA5BLkxDrB6TmGSWiU3Tkf0ui+lGPbJY8g3HgBRxGFG4aQ DNubH81YY4UkVdE+dKYv9Hn1qimyOX58ipSy7Ys0+os+VGgaBGyn81yvHI8yAARqbNvc 20FA== X-Forwarded-Encrypted: i=1; AJvYcCX2H7v5/UDGFx/t5BkYDTiDOs/wr3rw7oqJacGWpi9PawCIEagiqY8m7DtGiPrAKqFyWNDFzqAaM0IeGtrwq6TQe65oe/BU8169E+SJ X-Gm-Message-State: AOJu0YyVS40OGTZAxWTaqEkUIK1i25Ukc8DbdoxbcXtlE1fUhIRpJEN6 LMJ8E+muEVWpBZQKhyi2yOM3eIMv5PhDPrrhtFnf7meIWEUCKoPT9z2lyg15nVqOsruDpq0bhuE oVNBuiifDeNdV1QcCV2uKar34p53d9FGvPpjqtAt2g7u1i2o3g63r+KG/ZvvAMg== X-Received: by 2002:a5d:64cb:0:b0:354:e778:3665 with SMTP id ffacd0b85a97d-35efee307bamr7423570f8f.5.1718011920873; Mon, 10 Jun 2024 02:32:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHBB6YZLiS2HIy0dE82G5S8NImrCKfK+wiAnir8JXLqf86PoMnWoXCllIT/8+SHiawccCY93w== X-Received: by 2002:a5d:64cb:0:b0:354:e778:3665 with SMTP id ffacd0b85a97d-35efee307bamr7423555f8f.5.1718011920534; Mon, 10 Jun 2024 02:32:00 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.31.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:00 -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 v8 01/13] PCI: Add and use devres helper for bit masks Date: Mon, 10 Jun 2024 11:31:23 +0200 Message-ID: <20240610093149.20640-2-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 Thu Feb 12 17:27:25 2026 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 E2F7C7407D for ; Mon, 10 Jun 2024 09:32:05 +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=1718011928; cv=none; b=H8dN9sQvowa1yIlp5gF8ArfEm8ho+OVMs20DazKgg5flOERp1jaSASdeHPOC71QvwzuKOVnXzImikgJhjUa8pJYEB7G+Qc7whkLfinuy4Maqyktq7a3OD/RBihgPlmg7Nh9oFt7ntH9f2arLyBhKgRndHK1mV0i855DszTbDqyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011928; c=relaxed/simple; bh=TgkR4Nl7UsfcnDus5PAQ/eglmpUMQS/oRcoljkpQuXI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NONC3NZWebrwdCupUJX+6pjyokqsNtzZQjr6bq4dHhKu6rnJCqMZHg7uAkPgXWU9q6EC1fei7qNUpzyBiv7IlWzLKMDHVsyflbWDBAnYHBRZKK/Z8jiXoPpb8lyFzveUd4e+WqD1wsD2MZZC1iT8iFWKHaZsSebcLb8ts5aKgw8= 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=fBgve4Ha; 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="fBgve4Ha" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011925; 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=fBgve4HaBONMdXGPyAQqmE5QnrszPXafnzxJMgaG57Nb1v+u2uJoZtqPIT+2gVkuqGq3H3 57+Sd4atkSQd8sYS0Xwl4ak9sRBO1j26yjGdlEVOZC/D8O0tY6ghDl9Ahon6zNRWM8ZZ6T BdyszpxeymKlf9HOxjZIasRkJDWERGc= 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-397-JlplhzJMPfChmiAklxDNhQ-1; Mon, 10 Jun 2024 05:32:03 -0400 X-MC-Unique: JlplhzJMPfChmiAklxDNhQ-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-35f1ccdbf82so14868f8f.1 for ; Mon, 10 Jun 2024 02:32:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011922; x=1718616722; 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=dk85bLIBMeRukJNYbT9lN088cojBkVmKgnBCGy8Xc40p9RoC6Pwga01qu0SclXJhev gOqb0DiLjvwLDPuxAeaEabIIk07J1oQilEeAXjmpDxx3fRabLLzfArPtBmu4MeeRQ9G0 3kNr87FA39AK+STn94Um2u8br+s4Le6cKrUGUhxBZZbsK0h5QLa101EPWQr67z89iiGr ay/bZT4oDpIB34miMlbU7r5QiWtxFHzf7u8FpdDdpazEsIUG+h2aKMj+5x2zH9cnxYSL Hi2W8VAnxsv5k1wbD6hP5LI8Ndo5GdKMPGntmaUeCfaR/iutQoJNl90g8ltKZ7UohXSC 2KuA== X-Forwarded-Encrypted: i=1; AJvYcCXBlXr3T0SmNrnceoyF91v5268HabJZ1fqtz9QhdsA9398fbPXIRuxC+ImXL0attdYFzBwzmqmFi/znuDD4r3ShYSpXhwbnc6TvSl0p X-Gm-Message-State: AOJu0YzY8HFFW5JTi+2DBVw6I13eyfpJxdlmWHBFpADf1nrA2bhLvsDB PbmOp+Nipo3Unl0EW/FGuHAnJwmS62bo8B4ssyepyPdrr+lpCHmjq9X/Hv55E/p2zwSw7PxeQ4z a2OsMD9s5aBDhP9DXlZnzsM3azzMLCjxYCbR2gzZumstkhGsjjrHAr9NCDh1cTA== X-Received: by 2002:a5d:588e:0:b0:34d:b5d6:fe4b with SMTP id ffacd0b85a97d-35efedd2e3emr6479222f8f.4.1718011921898; Mon, 10 Jun 2024 02:32:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHZ34RIrsm60vplmSUHSp8HATp7AS7kEdOir9ms2KbhbicgUfnK/HnyWUyl28CP1kKuZ42sMA== X-Received: by 2002:a5d:588e:0:b0:34d:b5d6:fe4b with SMTP id ffacd0b85a97d-35efedd2e3emr6479204f8f.4.1718011921628; Mon, 10 Jun 2024 02:32:01 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:01 -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 v8 02/13] PCI: Add devres helpers for iomap table Date: Mon, 10 Jun 2024 11:31:24 +0200 Message-ID: <20240610093149.20640-3-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 Thu Feb 12 17:27:25 2026 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 83E7E757EF for ; Mon, 10 Jun 2024 09:32:07 +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=1718011930; cv=none; b=gqskLd36I6jCNxcxBrWUIVSn1A6OfdA5J+CqaMlF0uDjqUtrq5pRSXEv6YfgK3Dz+xrktkLiKtRf41QvBruRxSGVU4PcXRnZzUeeSjEhr0JX4378iJ0JU9IP4XfhZ5JmgyURi6NaoRtbHtMWU0Kmi5ktynN7VTl6l2xs2/Zlnfg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011930; c=relaxed/simple; bh=Tpme1oCPpt2P8kxKkNCy1FQ2rDoe6YzhFxy7ARkhB7o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nef0LWpGHTjM+eL7vpcliUjhja4+Fl9SO17bJVm7PU0RvBm67z+EArQIYbLAHIvjR0UTcKl/XyPno71Bqnogy8aUQTCAYBddr1su7dPwunvtumza+qralJSjxPzgBUHqpc1IXayPg7fFyAmiUmi88Tuk9G9XsE6k39DzuY6K3H4= 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=IG1VfmFm; 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="IG1VfmFm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011926; 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=qmJSx7rgddASyMl5reNwBFP1bVpLulSbLJtH1TuMUGc=; b=IG1VfmFmEoqXvoKqiiOwK2mh3FHJK5og9iNtKvynNvW5JtgqPxOJCM9cTChNNcYBWk8dGx jHN5MdFpEBzIc+SR13tTViQZi1b3x+qBx6wiD+5UQmE13WOyd/jJ/zUpADo7KY7K6qeM5K gy6AwniNgy8UGM+JrHQE0ISPNebWU/c= 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-631-qmgFZGA4NsCOE2_I24_zXQ-1; Mon, 10 Jun 2024 05:32:04 -0400 X-MC-Unique: qmgFZGA4NsCOE2_I24_zXQ-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-42180a0730dso1799445e9.0 for ; Mon, 10 Jun 2024 02:32:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011924; x=1718616724; 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=qmJSx7rgddASyMl5reNwBFP1bVpLulSbLJtH1TuMUGc=; b=Vv0vwoF+VaaGcVWSYwAFpFn0Z6Ks6Vy39GrXZ4JG6LziqSSqAY2ww/w+CbEiaJMGYz pAf31vNbSLJIlk+bZiW6aZC9TTTEtHwxuXmzPdhyjWkeabcZBuLMVuojxf6pVJ/9/GUr bl3JgaYey+d3tJB64XBK4BIRMLoVmnKjOuF4hKIiAEXX46G5xkny7MaqqS4UbmMhP0V6 ZyEAaz/Ao6ekytPNC1jhqpaOXqf396mx7BpMmvIlJPtntUl+6rb7/pAXn7LIXfh2+SF3 a8BvLS+nIsUKZiIxa1DI84krNid7AHcINt6RGY/FuC0C99A76k/NqfzXfP4CheJrSzp5 KmPQ== X-Forwarded-Encrypted: i=1; AJvYcCU7BF9nVeqqv8Gmh8ajqdDzZon8QuvEXhHCmmjPzkOr6NO4B+e0yadEsSDo6R16mn1gsKV+jy5ZjvG3uQN7ex8OYvbIktznKdpa9nL9 X-Gm-Message-State: AOJu0Yy/q5CAMEJigQbjyJeYkcmtIiGZDjWTQTUbhjge/qVJ0ktes5Bb 7xJ9HqmLYLUW13mRrhQ6mBIZBxKtHI79RFhXbnFrMsbfgvLAvPSeRpeU5/awhgYypzC/PV6ljfT EKhDH+eBFFY90UR/7CxE5eDdK51CCs2oDF4HsXA7vNjBwnJRbN+PcKea9W/Lx6A== X-Received: by 2002:a5d:588e:0:b0:34d:b5d6:fe4b with SMTP id ffacd0b85a97d-35efedd2e3emr6479282f8f.4.1718011923303; Mon, 10 Jun 2024 02:32:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGFZ+dzVu/w9la4npo4bAizHSvmjDBY8n1VN8yj/JR9K0tLafmUbMQC7IVGgJtAV8KXMW0tQg== X-Received: by 2002:a5d:588e:0:b0:34d:b5d6:fe4b with SMTP id ffacd0b85a97d-35efedd2e3emr6479261f8f.4.1718011922783; Mon, 10 Jun 2024 02:32:02 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:02 -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 v8 03/13] PCI: Reimplement plural devres functions Date: Mon, 10 Jun 2024 11:31:25 +0200 Message-ID: <20240610093149.20640-4-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 original PCI devres API was implemented, priority was given to the creation of a set of "plural functions" such as pcim_request_regions(). These functions have bit masks as parameters to specify which BARs shall get mapped. Most users, however, only use those to map 1-3 BARs. A complete set of "singular functions" does not exist. As functions mapping / requesting multiple BARs at once have (almost) no mechanism in C to return the resources to the caller of the plural function, the PCI devres API utilizes the iomap-table administrated by the function pcim_iomap_table(). The entire PCI devres API was strongly tied to that table which only allows for mapping whole, complete BARs, as the BAR's index is used as table index. Consequently, it's not possible to, e.g., have a pcim_iomap_range() function with that mechanism. An additional problem is that the PCI devres API has been implemented in a sort of "hybrid-mode": Some unmanaged functions have managed counterparts (e.g.: pci_iomap() <-> pcim_iomap()), making their managed nature obvious to the programmer. However, the region-request functions in pci.c, prefixed with pci_, behave either managed or unmanaged, depending on whether pci_enable_device() or pcim_enable_device() has been called in advance. This hybrid API is confusing and should be more cleanly separated by providing always-managed functions prefixed with pcim_. Thus, the existing PCI devres API is not desirable because: a) The vast majority of the users of the plural functions only ever sets a single bit in the bit mask, consequently making them singular functions anyways. b) There is no mechanism to request / iomap only part of a BAR. c) The iomap-table mechanism is over-engineered and complicated. Even worse, some users index over the table administration function directly, e.g.: void __iomem *mapping =3D pcim_iomap_table(pdev)[my_index]; This can not perform bounds checks; an invalid index won't cause return of -EINVAL or even NULL, resulting in undefined behavior. d) region-request functions being sometimes managed and sometimes not is bug-provoking. Implement a set of internal helper functions that don't have the problem of a hybrid nature that their counter parts in pci.c have. Write those helpers in a generic manner so that they can easily be extended to, e.g., ranged mappings and requests. Implement a set of singular functions that use devres as it's intended and use those singular functions to reimplement the plural functions. 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..82f71f5e164a 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 Thu Feb 12 17:27:25 2026 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 0E83576410 for ; Mon, 10 Jun 2024 09:32:08 +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=1718011930; cv=none; b=ANHU45PlPcn016yXcunGJMlnBqI/G0M6vqnD1HnzSHAvkJA5rV5pa9Q0AhE5QkKPj9EeHvWO3qrNIRkv/vDFwjp60M5MnzgZ1JYsUvPeZxfTxg26ryJik3LdnQUrV6zo7WU7d8t36mGcBxzMN0wIIB4zy9xKI+cv/NuCOfWOq6c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011930; c=relaxed/simple; bh=R+ahaaj9Z92r/qtOUZY3Zpj6x9Bj6I0WxW/a/NzXs2M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iKNDDu2e9ICAVM0Tt+/xwZtjCq4vX9ltEh2NOXLpXfqiClBiQJvCknd7j1jA5QVzOSOJBe8GghEQYMqa1kTTuxJOPjPcLSfe2aax74BFTVOGj8QBFWr0hYbNnuS5gwayV4WhD3CodJ9p9u2/jVHzyqdPD/VUqvwi06YNBPEaVMU= 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=UqSLvAhG; 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="UqSLvAhG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011928; 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=ZiUwSs1HtnX+QGE2Zl9KICBy9ex3+lEWzbjMxYLRGDA=; b=UqSLvAhGzVDocEbP8aRlcUWirw1IKUAIDjMPYO5QDOdcfAkoHRJzG0G6z37MbO/3WLX8zh QNVBDyD1crP/4whtIsqHE2MjnjOIcZN9YsEXWGRCQoJ9QVZipMT/RoWDD5gmt69Of3SvNx 0lz77TkhbYjpEDiuq9+qgIPwlHynwn0= 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-16-qVgwIBMmPyC56jyRm0z11A-1; Mon, 10 Jun 2024 05:32:05 -0400 X-MC-Unique: qVgwIBMmPyC56jyRm0z11A-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-421920de031so1511975e9.0 for ; Mon, 10 Jun 2024 02:32:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011924; x=1718616724; 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=ZiUwSs1HtnX+QGE2Zl9KICBy9ex3+lEWzbjMxYLRGDA=; b=or1/cjh8lcli/OWwqpJplKuuD7Uu7OtehjcOEtQo/FqYWNUGUVSV1Oau+ZXF4PVSUv pe5Ag9EvI+rh9FMf9K2++IrQ3Lu0kzaRGECgYnwzf2ei1g23BMndOd2SBGWqgbYxUmb8 rXiy53E6FNMjw827UKMd6xHkajyZ9PHbBrOvs8dkAKXzlOd0JkJZRmqb0wo47OD+5B4j dvjQt2nvuvTUwqGqrdkf4/dzBXeIXhQTE318A9FyjBI0FhWOzU8/JDdt89P0nG7Ko4DL J4Ju4pXwXLgluih9kzuKBrDOge7q9TOAf2D7F03pIcoBGYUXJiI/G/Hyn8Tf/f9NDyQI sENg== X-Forwarded-Encrypted: i=1; AJvYcCVk5yjBQeRB5MgdnL1ae8aIQ/XbZzCff7ae0iesRt2dvgEfQGFKkNfsnU79NKPi0pSiqQd7HRom/0J/Bkpq1BedqNZzWGL6ka6TLdQi X-Gm-Message-State: AOJu0YzuRMYjrcMsUwP85Vr7vOYkwqSUVZeyCCc6a592/SVxS6zK68Vr 4XFuty9e57yQAkP72J1Ee++HMwmLVYz7mWJtvdJtInEh0+ko1Ecy91KEM01ijLNg93cBin8zzWo kxjZQc2OIYdlnQ7GVoSZsNnpQyAGSRfmgr8K5BOUEWl9y54fBUGI23yr3hMCKHA== X-Received: by 2002:a5d:6c65:0:b0:354:fc97:e6e3 with SMTP id ffacd0b85a97d-35efee1d38fmr6464070f8f.5.1718011923975; Mon, 10 Jun 2024 02:32:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGHVbLME23XHqqxGSb/L2UcmsZWCbVxunmo7ktVNIV+TisnX0BItNxZ6wxaQvx45l+MZoucwQ== X-Received: by 2002:a5d:6c65:0:b0:354:fc97:e6e3 with SMTP id ffacd0b85a97d-35efee1d38fmr6464047f8f.5.1718011923677; Mon, 10 Jun 2024 02:32:03 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:03 -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 v8 04/13] PCI: Deprecate two surplus devres functions Date: Mon, 10 Jun 2024 11:31:26 +0200 Message-ID: <20240610093149.20640-5-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 82f71f5e164a..54b10f5433ab 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 Thu Feb 12 17:27:26 2026 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 EFA177A715 for ; Mon, 10 Jun 2024 09:32:13 +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=1718011935; cv=none; b=CgrKaK3gH2irpa+V6Va+pVXyQgtSo+oJmC2vn5hgWf5dhUE36YL2O03LACU5oy18sXcgm8denr/SPIgnypmQHbO7DxNLD8Z5bBPM7gQqzSxA/ZleUEby+gE/AZs0zT+lbSaXtI4OW7ME4CuZ6kz/Q3UDb4NKv6R87E+Q8T6QcCE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011935; c=relaxed/simple; bh=p5aP8feMDL/KVPuUsmLl9Z6+CZ7Vvg39RrLQww3jf+8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kV0h8C0fT0hEDJ6JiEmQSGNDIIxr/C6rUw//xhYSSX3y7kA23sW3kYLwJdmu6WVPjhHP06ICJ6UmNCLdvaRznMojNTBNGYz9QG9v/CwvdMG8buo0hcambJ40Zrb7vbDleYPE5FlQZGHGXKbRRDf3U6HFZr4sAA8fungWltpKO3k= 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=Zh+N+N4s; 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="Zh+N+N4s" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011932; 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=SzRgXuNR+5R6UFNTz4a6dQ6zzeQCJg+lenadNGYN7bg=; b=Zh+N+N4s6NUSkJx7K5PEYIBwoW4U4/ROpnGmxNKIKBbA2mYxbE9veoSr4l/GhHZafQvHxw Amq5QjhOSn+lvt8wWpsTn+dbQxwdSYDDmI8sNQ3r4zEnyXFKsqaWdHj3EWbaXzC9t5K5Bm LwDeEWP9qFSfUjpwfRI4Wze93KeHEGI= 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-562-a5KX4GwgNfS-u939a0Z0WA-1; Mon, 10 Jun 2024 05:32:06 -0400 X-MC-Unique: a5KX4GwgNfS-u939a0Z0WA-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4210e98f8d7so6355125e9.3 for ; Mon, 10 Jun 2024 02:32:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011925; x=1718616725; 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=SzRgXuNR+5R6UFNTz4a6dQ6zzeQCJg+lenadNGYN7bg=; b=dQNXFzswP9HpiAXmfJ+lufjOLOL1ub3+Afv3qcueADMtHakb7OkIIATN0cf8/OmiVe pie6bTrJP3F3iSMx4mz9kYICfUsOrucFP/ce1wsjc/ydBjrHznQg3Fz/BJBdGx0tZ40W zh/28rSO8BZzlOzYdgdySiUNxhhTrrKXm0x5K6/dCK2EOOsZYC5HAi6Ku0OwukCgv6Xw XM+jkBvR4ifcYVadG/uEkMFXtmdF5VF/CLpWXUv/NePDpdQ1pxNud9kF11d8B2qIGCkf WHFBli0tlmXfUfZgVLBZega+VLKuAsL7KamBigJNnDBE9PnIl9z7hCsqeEsvEwiLudVa q6zA== X-Forwarded-Encrypted: i=1; AJvYcCVuzvctgWErty1RQFIuibsciGve2U0sHyWaF5MeU/3KTORkGgcRtAYkNCLfCYll+vRAIdeO2hQgnvx/82vWJJMsn5V5/hKXlhLt9SOj X-Gm-Message-State: AOJu0YwBPL11F/5aoXsWI2SoircK2TiuQkLREHihF5l5xGbqW30Gh7QX kQq86IEeIBYpEAy1bQT/TZ6UdHJfmV+bpzdoue+gmN0/5tVDK8mXAPQv/twz2jwFXUK0L82VmcL QSQDTzdQKLIAGDCyuToCAx1wxsX3+xY1uev5A7qQT6xvSQHuHQuLP7JqrlMNSkR0umEGoSg== X-Received: by 2002:a05:6000:2a1:b0:35f:2287:7b97 with SMTP id ffacd0b85a97d-35f22877d54mr1983938f8f.4.1718011925304; Mon, 10 Jun 2024 02:32:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEnJj+pjvnt6EbKvh468//5k+11F9WXb809fPmNpxRIYhVMOIHvE6nxXbo9J+kfKJh1cgO5QA== X-Received: by 2002:a05:6000:2a1:b0:35f:2287:7b97 with SMTP id ffacd0b85a97d-35f22877d54mr1983925f8f.4.1718011924931; Mon, 10 Jun 2024 02:32:04 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:04 -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 v8 05/13] PCI: Make devres region requests consistent Date: Mon, 10 Jun 2024 11:31:27 +0200 Message-ID: <20240610093149.20640-6-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 54b10f5433ab..f2a1250c0679 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 Thu Feb 12 17:27:26 2026 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 2A7BC78C8B for ; Mon, 10 Jun 2024 09:32:13 +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=1718011934; cv=none; b=VKJRwLnT2NS5QO+w/gs+0dSLx10eHHxltM6ZHfNj+uu/WliFque218bCQ2KDTCY23XWz1gQiW36cK4gZJGPmEHe3qhVOO5nI/wp9a4wWbXX17ESN5Arq/ofivEH6I86E2KojyyZ/L5i0E70/AvgoF9cZP4NzOkbUOR3gG1k8Qgo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011934; c=relaxed/simple; bh=yZ5BKFpZwAyKkfsZmuzlsekm8L8yl3Htpr+3BjCRxJA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SB8Xzpx+XD+VycDyauP/RAdrGMTDHKS/G/vgaYx1PilFSu4wYR0847HgP7WXx4id/3uwLZDYTvjvbDOzxoI4I8qe3rrybAeUkjqs8CFZzxhNEWPhzCLgTRMBtJnJtsLSAnHMps5L02Zw++XmOenLFj/0nDOWDDw1DlbNzt6btn4= 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=e4EnQE0V; 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="e4EnQE0V" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011932; 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=j9dFlyi1HoB9hw7OSY8YroyFypFphGOIBSX/wCz7lA0=; b=e4EnQE0V4bGIqeDkt4JCPzHV78J5STRBSe9mJ/5H+OWZJOKE2M5FTZzOs4kjejx+aGDqtE eAYoF4NOUK0rKXU+VPBUqGCWERntgAbuiwaGb4BxlPaOusLQjV2UQ5TmMSvZKEHWo6MTdL lTfe0/jHVrubtvHPEoiSOViasys17bI= 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-57-jcCLZEFjOGSzQlb4PF7fgA-1; Mon, 10 Jun 2024 05:32:07 -0400 X-MC-Unique: jcCLZEFjOGSzQlb4PF7fgA-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-421920de031so1512105e9.0 for ; Mon, 10 Jun 2024 02:32:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011926; x=1718616726; 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=j9dFlyi1HoB9hw7OSY8YroyFypFphGOIBSX/wCz7lA0=; b=vqFOE8gdnUExJtX6VK7zAZKSumC5/sGGM05LcgNdvl4No470wmJfCDk9hQQ0S0Lzap Ru4zj4NFKMw0oA7IxNZkRanamf7u1oiyooBMNtrefjRpVWc4SzkaDfkfnu2wMt+31YXi LgjiyWpIhjo3DS/+g+sWmArvDB7/zqMf6W3eYwjq7pNFObT4XzuHHcRljct4ZdYltOmc UONy9tSxeU3RIrblIq55AnCDw7LULF3ubY4guU/zdB+W7T2PVdWsxrCSD2zdbarSOnYB qKDyO9lnxE0IPGYDXnG1YQgA6LlFb59yXW5NO5bW1Qit/v1rIEL/R4JVmkkz5KVgt9mF Ni6A== X-Forwarded-Encrypted: i=1; AJvYcCXdMZDJ0FlFcKZ7Bqm5ryGGsYIVr6BEkrA0y8qwdgm+e2mlwvQjysgQGPgqzJobs/h4IKeKgLO8+5GvaBR27ecjqVpN+RETpE3I0jDy X-Gm-Message-State: AOJu0Yx0GrnqUyzJgWQqDrNtTSpYyb13Q/dzG0D93ig6MFDE49J3djts vCzOA5gFzidnY2VM8tiSfJqoz3CTuZzFFJC8e2XBGDmo2eWEuR/tAD8v9j8ZZMyd3y42w7Mi7dR sKNovC4KmaDVTrt0r6xxauk1nacLp3Nuld5pq680BvK/Jsn8laMXdE0blu+/RDQ== X-Received: by 2002:a05:6000:1449:b0:35f:2584:770e with SMTP id ffacd0b85a97d-35f258478fdmr1253733f8f.0.1718011926172; Mon, 10 Jun 2024 02:32:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEziWNez2EK+YomAgaeshNAtde9wnxaSAihS2piYtCVgKznJZAQPET9dT+eD9H5nnkLmNWrZQ== X-Received: by 2002:a05:6000:1449:b0:35f:2584:770e with SMTP id ffacd0b85a97d-35f258478fdmr1253715f8f.0.1718011925874; Mon, 10 Jun 2024 02:32:05 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:05 -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 v8 06/13] PCI: Warn users about complicated devres nature Date: Mon, 10 Jun 2024 11:31:28 +0200 Message-ID: <20240610093149.20640-7-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 Thu Feb 12 17:27:26 2026 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 DE7AF77F1B for ; Mon, 10 Jun 2024 09:32:10 +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=1718011933; cv=none; b=JBsnfuBw+TYIrxAXCPHzJKy7HHjcvzm1vUnjeUcL6u2Y2CA3ZrQjfqSlQiqwSB3lv/DC3XxrouGj4mYBGw38zloIfzVR6Ox0MN4q0igFXdbfJtZRAEtafKcb1WxmtHjEim3oLWp64PxlBe24Iri9QkBReDIxrj/lwX2zw/qTRh4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011933; c=relaxed/simple; bh=i/wwwpIUQNPto8+tty9E05yTd9y0taZqALuDGbqx1SM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=a7pmGTXXVN2Ym/Ltc4vt8KubgWXhaHtsoSjCQqw15lcvd5sS+DhlPj5kh2EYACMLWUpDPo+RfaJMz+R6zbrlM4OfVhsn5vJwMKbNoWok09pdunVs5R6+W61gU+MLItdgDg4YWGXytzjau5akLn0eUEcRoObgUELAv4fQC8dJ1oY= 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=cVqkiSpH; 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="cVqkiSpH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011929; 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=h8U3rxW1s1YIVMSUu9ZuPsxLMiEiJVd/mP29b/0kZtc=; b=cVqkiSpH3AghjZkuWssN/RhXHQaFQgku5swbzO34OtcNGRDsrS60ITKP3yJcM5JnMCRkLH RrxVb8yCFO1vxwRutBYrrCBNddZuMHjteIHlYuClWO4AtTeRIKgC0Ylyc9CIm67hlLiEdU Jw+ggabYXWBXfhWoZYrGPO2lApqiWpE= 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-73-9J6jWQKxOT-JkKM9iOoWCw-1; Mon, 10 Jun 2024 05:32:08 -0400 X-MC-Unique: 9J6jWQKxOT-JkKM9iOoWCw-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4218118d1efso1915215e9.2 for ; Mon, 10 Jun 2024 02:32:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011927; x=1718616727; 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=h8U3rxW1s1YIVMSUu9ZuPsxLMiEiJVd/mP29b/0kZtc=; b=OxEd4vg3oMxzitC/wWwx+jv+JnFkv1KxrLdUtjR8qWAIfiC4c9xwgDOkIJt5Srj4sd 35gEGU+PfWWpvd2lu91MIwKamYMu9HNZBaFRVezkSSIzXIRfW9U2N4l2hgHSqkPNcbfU lhfh1Lgld4SZ3PkkLrrHGcN5hELXe0bQCLirs2JSSYbhqNfB0mWRefgfmRjwo+CD7/bk PKdzRDb4jaEOKFDnAUgD+aY3bCKx+As/YM9XmqZVutjPv5tcImRc1PWRa729xGKdzG3r y5m0dBPtDbsOVAPDUNjcM3XIDYCdYYAc+lD/LK+0FlIbKjT10o9daJb/ATakAgzOKGSW JGhg== X-Forwarded-Encrypted: i=1; AJvYcCUKxVM8HH+/3B0l2BFICZ4IDw//P8q5kzhBC1oIRF1QoMwA2qgfoV3r9g/cNoaiPe2cVWXkP90oV/f9Bk9Mh/WFC7ScgI0823ohKd1J X-Gm-Message-State: AOJu0Yxtm7uF1WllJjRV5IdEW+i5uVd3PIkxlwRIYMXZjZxO94OLC5ux EeubC+CzB17naFH5HWIqyoIwOYBGjbnm6R/KnzQSxCLy1ycm4QWopPlEXVLKedlMXRs2ABHwiX6 84vJRxQyW3zd5vnxQxdAF9DhSnPKSEiSDy6qEVTkE/UK28m3ngM9G7KuvZlkEqg== X-Received: by 2002:a5d:5988:0:b0:35f:1b2f:8697 with SMTP id ffacd0b85a97d-35f1b2f890amr3471332f8f.1.1718011927086; Mon, 10 Jun 2024 02:32:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEqdCkgAR+1q+z4vuNJAhn568KdHv5oS6P7/Q9rNnLG+Le3g8tuihyGLjdodx7ImjfadRMULg== X-Received: by 2002:a5d:5988:0:b0:35f:1b2f:8697 with SMTP id ffacd0b85a97d-35f1b2f890amr3471320f8f.1.1718011926842; Mon, 10 Jun 2024 02:32:06 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:06 -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 v8 07/13] PCI: Remove enabled status bit from pci_devres Date: Mon, 10 Jun 2024 11:31:29 +0200 Message-ID: <20240610093149.20640-8-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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. However, that can easily be tracked through the function pci_is_enabled() which is agnostic. 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 f2a1250c0679..9d25940ce260 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 Thu Feb 12 17:27:26 2026 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 9D9B6745F4 for ; Mon, 10 Jun 2024 09:32:11 +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=1718011933; cv=none; b=sloErZp+qmCEiD+oE37DLXdTLjsMIRZH5tEiItcHrfaS7wIn3mG5lpSAynOEPexv02ptwyl02pRyXlEVueAX5wGo7U1HjZWHC+qgt+FgJOvJZvazLAGUpOQWA0OMDmqedkRe6qZh7tbfh6frdktfXxpVE6OCekSuYV8YSIIPg/M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011933; c=relaxed/simple; bh=JZ0qVkY2AySl55GbKU4MmbsmlZou/0KAge/Bt6uyTGY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZOpc6/E3OnpV3XAc4LwfcMxCmNSmcTnfocohJRzSdq1bcPKnYSAwcYwMXhZ0Z62t7MdtNtk/z7OSsCHibDD6cv6KS0q9M4TeUlHF2XnR7jN5lKm8X/TuZLq4U3iCCrz8qa6/oQ9or9Tk0sgfEjlRx1WT+VxzEiJkv1p0IqwP7hI= 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=Dn7L1JqD; 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="Dn7L1JqD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011930; 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=YC1P2jDfqZ/b0bhQ66lkvDoa/VkJd5IY0GIkzmrgRyA=; b=Dn7L1JqDp3+Q4JxSXAAabidSnjVOSlTRBd5w7JxvSSxauJjB/xKyPaCEQYb05AD0Ihd+oM h9fBQACMHmtTmjWUJd6aPyzOTlyp/IqUh/4whHWHNHpRaoSMgULzS0dC9goSpvcBF7j2cM +1dvotHuNiBFMTDLVJyosebP6HbvjCY= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-689-OAHgm70cNDW_DAj3mbtYww-1; Mon, 10 Jun 2024 05:32:09 -0400 X-MC-Unique: OAHgm70cNDW_DAj3mbtYww-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-42180ddc0b9so1691965e9.0 for ; Mon, 10 Jun 2024 02:32:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011928; x=1718616728; 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=YC1P2jDfqZ/b0bhQ66lkvDoa/VkJd5IY0GIkzmrgRyA=; b=iFVWsBBPXo3oCMkOJY6SRTOX5mfeFBlkTkYrqFU2aR4y2UXUiuAU2DAQMf4KQRZAaq KCwDSlfb/encqNtzvADaTCU+cSkCQho8g5bvZQYtmnE7iu/tBGtLAy+zS+AVhWz71YvO w1dvy7Fa5VNWLmm97sTNKGDGZPZWxNsDmvwFYm48lNo3+81x5ijxcX9viYwx1GXKZBQD mMd396RFFaokA0Et2fHmnrZnYHe8zCqIZPbFge2y047bgtN3oqitpH5Enz6vuBaKWsZR j49KVf1vk6wCEi5PHXDuwemeU4v50Ub3ao3sg7Dm9cNHC6VRWxJQiKvH/02Ta2sZC434 9c1Q== X-Forwarded-Encrypted: i=1; AJvYcCU7WoKAef7f504klwzEhPP+/j991kZ7ByYi8dPkMBSJXsnvKcsq8chnSBClS28hp2iSq5B+fMEkm77FE53AKkTqrxobUtlsmJULVKLw X-Gm-Message-State: AOJu0YwXkM6PR9eLnfYzPLqbS9Gct7OO6SbmRZsHrfwIn7NPTcYD5LKc LDq2pQL6Ai1YR+6EETPd83fpfW+1RjvVEjV0s4r0IUjgXzeq99ArDYxDCwt5/V1scGoHlVxzmL+ 8pvemluI7L7J/1yCOmKmSNprCKuZ23iZZxzm7CwE42uXNGXZnJ8Ti8VtZMQ/01Q== X-Received: by 2002:a5d:6c65:0:b0:354:fa7d:dcf5 with SMTP id ffacd0b85a97d-35efea7e189mr6469036f8f.0.1718011928180; Mon, 10 Jun 2024 02:32:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHVJwLx4/C+jduwDhQw/dzOe6oYBT02eJHjQK+Qoax/SQHHf7G+cPrhRcc8vOfgRdZ1mMl4TQ== X-Received: by 2002:a5d:6c65:0:b0:354:fa7d:dcf5 with SMTP id ffacd0b85a97d-35efea7e189mr6469015f8f.0.1718011927762; Mon, 10 Jun 2024 02:32:07 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:07 -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 v8 08/13] PCI: Move pinned status bit to struct pci_dev Date: Mon, 10 Jun 2024 11:31:30 +0200 Message-ID: <20240610093149.20640-9-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 | 4 +++- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 9d25940ce260..2696baef5c2c 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..cc9247f78158 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -367,10 +367,12 @@ 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 enabled:1; /* Whether this dev is enabled */ + 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 Thu Feb 12 17:27:26 2026 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 737FF745E7 for ; Mon, 10 Jun 2024 09:32:12 +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=1718011934; cv=none; b=M+XclL6xmu63ghDQkhjOKXer9km5k4d+I2Tbq3ZaPNk5pQP9K08s6yQTCzfQ4qE41QWxOhg/TkSjgXP2mvrf4lvgLtJcQAtUmHS3PqZoUFV5XkSmF6hxgWQnM1aQMG0oih+zfRXyEsSbItLsMC//O+gzFvk5Jc/IE/IOChWljvc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011934; c=relaxed/simple; bh=nOn0DRXnO8oQWYZd4giX2BtFyTmcsIi2D+kKy3al+uE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TEN6l3sqVcBV1zwcvI0exFDptCZFaUbofVU0B8+5l6dl5rBLQccXVDPDHINqtd67XLBbTGURG1KVvFPnPenOq92H/Feqduez+Pv6oN86fEgpBDms3gLsRZmaSKUTOtGs0ROUxsyeMaJUSbXuLCdUrdIv21Dgua8Pj+eD2/qsxNo= 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=Y0v3m+cO; 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="Y0v3m+cO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011931; 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=0E+yd/NqAYSK8WFgRSEHpHq5LwGzPXxKeB5ucvUWBek=; b=Y0v3m+cOx4XCDkhY4VGbPBfFXdkYnNQtWjBICZ8KA1uCXUPLN8uqIidWrzaDgpJkBZIALn prkS/XzNJWAcV3W59rlhiYYTX9mttJQxObuReYnaQYZF87V1UKxgXy9Bvbjul11qkE83Au s534IItx+QDkF/1DL+6X52KoD44ZQAQ= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-590-RgELuasfMrei6gPMjocXMA-1; Mon, 10 Jun 2024 05:32:10 -0400 X-MC-Unique: RgELuasfMrei6gPMjocXMA-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4217f4b7355so1940525e9.3 for ; Mon, 10 Jun 2024 02:32:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011929; x=1718616729; 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=0E+yd/NqAYSK8WFgRSEHpHq5LwGzPXxKeB5ucvUWBek=; b=JnJJf7IGnXtfN3NInSelJqlupxKfeePa20tiU4FxFpvVNoqvnkfHjxRkHzLphxUmKP AW2DfB4kyYNFP/8lzwT5oTJB+BkIyjeysvb3PNemL3GMofBsypzI5nMHoDt4O3QLpPn3 nnw60XwDwz4PvO6/NyZRI8k6R/QquYh2KNmVwB6htYAwTs+dBrrrBnragNyIKeA9AyeM gV3s9VpFTl5aZD/tvbiWwmZLaUi2nQ2lAr4Wqx9WcCdDlirc8TyPsNGi7BLRNlcD7x2k iOvvV2PVHOs0DhltRn7MJyLU9/GNAvEBt8H2vhMagACzLgQ7jeknkFRVHYHXmwz2RhBu Mwqw== X-Forwarded-Encrypted: i=1; AJvYcCWpjNHTxjAqexV3lQQ47Ow68nK3hnNBoEnyG6ILUKi19Wcxp4xGaS/poEmu41Jd0h3oP//gLhF+8/gEbiudHZ7Xl4+v+goNnxY9QAjj X-Gm-Message-State: AOJu0Yzumfq5T+YjYm2pMqyJtmALWXo243TdY0Q6+qsjna/xxvVPAP4f +FrFvQq59Lz5hdh0PpWj44ge0FdU2vriGZuipeQLpRp37IuIQKFEqmc3vF/6LSy4bWfygiO85SR iKurAoAe2PM4jmVm4HUZgpo0l5BWMXaNnP2f5fSSWEpX3U3Qjvmf1WZ6v3Vawuw== X-Received: by 2002:a5d:464b:0:b0:35f:247e:fbcb with SMTP id ffacd0b85a97d-35f247efda6mr1269297f8f.2.1718011928886; Mon, 10 Jun 2024 02:32:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHrZxYD3HEuC5SQMDdGziGt7mw/V9beP3O+PtWTxv0mACsmhdqWKMeo5TR1WqKS8I+N5uEZTw== X-Received: by 2002:a5d:464b:0:b0:35f:247e:fbcb with SMTP id ffacd0b85a97d-35f247efda6mr1269289f8f.2.1718011928640; Mon, 10 Jun 2024 02:32:08 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:08 -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 v8 09/13] PCI: Give pcim_set_mwi() its own devres callback Date: Mon, 10 Jun 2024 11:31:31 +0200 Message-ID: <20240610093149.20640-10-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 2696baef5c2c..a0a59338cd92 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 Thu Feb 12 17:27:26 2026 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 085747D071 for ; Mon, 10 Jun 2024 09:32:14 +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=1718011937; cv=none; b=nPu232DsbNEGey3fIZ3zHEr2WuzrkVNpTtjv7NFrUcg3zPCAO0wJAJGww6OwkV4W2eUZalMsBx0f8BtIhdm1mkrtN9QRF/OFvsINx3s6DxgJn3PHFDOTWyQTn9PLdzbPKtrXCrq+Q1lCz4MAJY2z/veXwlNJDhCKrm3J9WW3uJc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011937; c=relaxed/simple; bh=CFdCNeZZ6CyxrinDB0AsxWeYdDz1/5n2fdvfoFvvDF4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=udtWFVB9JkChxMGbWg8XR9yCUL75plIclxLqOpSfmEytPOs6xvdESOPEgVFb0kUPvpMYSCzKtzr/yQtiTfLxp9ec/ZWSEGzJKWerjapyDQ7ivA/SaUlOj1sTsHU6OiPPTmCFpESR4pLy8q6nFzxKCTu56xeqFZ7El3eI0XRDIYU= 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=PJQv/ZsG; 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="PJQv/ZsG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011934; 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=PmBGcmpGbvOFP1AIf55egClciELzX12rkzdK4Dy+Hd4=; b=PJQv/ZsGPGORL6zqAUSz8d8MMkPBuFSJPUZPZr7pbsDcKw1tUNxZO4K9GK+WKw2LgA4rF+ +FsxSRUla4SYiWLOy4FY/DBJhGGCDnTCYw3H3AdN+ifdKGMilMNcJfTBIpjt1BVv+6Agbs 2iGstWkdwJ+g0N/pQoP4qfCigfRkvo0= 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-640-6zxHNKvKMw6atIst8OOXVg-1; Mon, 10 Jun 2024 05:32:12 -0400 X-MC-Unique: 6zxHNKvKMw6atIst8OOXVg-1 Received: by mail-lj1-f200.google.com with SMTP id 38308e7fff4ca-2ebd69e45b3so2133691fa.0 for ; Mon, 10 Jun 2024 02:32:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011930; x=1718616730; 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=PmBGcmpGbvOFP1AIf55egClciELzX12rkzdK4Dy+Hd4=; b=HNtJnQmm0Zw5wW/aX8kdBrN74yOPLGiPmcD7U2/oM/hZHaLc+K8qea896C2eJw90Vw 20tR3z6hA/5TkqMe3KVJYBeLHQGXw8K4trH2TCP/kufjiO4fpCX0spDgu1irWwYIz2Jn RwsdF8v5iU+0mbEfUlzxopoNnFYuXQkJM8zhWuF0xMZw8nHafO1v8OCi8I2T4pnKO84P xd4YvjgN7/tvHY9W0KZKaltuwh0gJs3Wyc64e4k+Sv+dmnqMZsfy/+Nl2oufGWVtfQ9L AkLg/mGidGX9eGb50v8BBudmA1bdU+s+ZNuOJW1HfIqRtIgTy32TF0ZtdOeV3bsWnXbu 2vcQ== X-Forwarded-Encrypted: i=1; AJvYcCWGDZq+6EDSRfur5028R//cpBfIkngwJ7v0t9I2gBr4rrTubEuzHmd79kfuGBya6CDzg9A/ZciD6TIxKczDfdZaDtvEWo0HD+YB46c6 X-Gm-Message-State: AOJu0YxhW3VQ+cxxz7ezG8r8NsOewX3y5aWH7WadJH0bgGQ7kFiFXNxC dZc26mA22gCUD3AksUleBml8X47ylD7zjKDiZgEl2+SNxewR+YJqr6gMuPn0teKHLz3v127Bx5m Mdp1/6/Bp/3Wef9hcSH97oMg//3o6Ta6du9eflKJT4/74au6TkOn+RmG2oDYI/A== X-Received: by 2002:a05:6512:551:b0:52b:8325:6eed with SMTP id 2adb3069b0e04-52bb9f76f7dmr5384550e87.2.1718011930236; Mon, 10 Jun 2024 02:32:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFgbn5PpJ3Jnv5nGP2smWEohtnvWJ0KNMfA5EI1Y5tPrYLamq+qiSktIeCn16zhQ6SoTJbiEA== X-Received: by 2002:a05:6512:551:b0:52b:8325:6eed with SMTP id 2adb3069b0e04-52bb9f76f7dmr5384538e87.2.1718011929791; Mon, 10 Jun 2024 02:32:09 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:09 -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 v8 10/13] PCI: Give pci_intx() its own devres callback Date: Mon, 10 Jun 2024 11:31:32 +0200 Message-ID: <20240610093149.20640-11-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 --- 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 a0a59338cd92..0bb144fdb69b 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 Thu Feb 12 17:27:26 2026 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 D84237D41D for ; Mon, 10 Jun 2024 09:32:17 +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=1718011939; cv=none; b=MlzDEjZyHrI+9cp6TlmGm8tRtlU6KiTWtQGAnPQSYORybxVE0LaeYm//MLiz1MPlfWH5fwLSHjjTwlPrStK+J6grWd0sHmgWYRGV3/e8Bxxqueej43cyrjtpD/r2pEGJKoqzBz4mkC2AA+VxxVqPCHiUh7KOp44991qLrF+Akx0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011939; c=relaxed/simple; bh=60by0ZGSmu26TnhpKT16jO9iZCuapVeU+qpbrVig91Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BqjYrKIpJvIPp+EaQJXuz4O/mGopgbO8fwdPNDlPIDBF5y0e5eXYb1CxW3x0wl0lmosgF2SsEHdOznthgt7oE/TNFQJDCaULatbAFOT+Sd7XAxYBQPOlrizKPFcWuSwHywfsilUQ6HnaDszFV3zC3vxKPP4ESNR5w0VS7ex/2PE= 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=X7HMR04a; 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="X7HMR04a" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011937; 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=uYVaQkWqZSrx/i/zySmVb2SXxUjFViRl1HRUPLR6DN4=; b=X7HMR04aHaXE2hYSalLxwtLE1fsU2PV763aO6imEcMRtMLlafjt3Ja9iyeEnB3H0SUkxrD UWEjNeCs0uZlO+5Gh2atQHC8QRx/ZGAjmSfaUw0DS0OlYPsSt7DX383MgkTL2BC+DPZH2Z ylwr/RreySFkCU9MAriFqCityJ16Ueg= 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-444-bCsRIYdmOz6udcTNIrsAZg-1; Mon, 10 Jun 2024 05:32:12 -0400 X-MC-Unique: bCsRIYdmOz6udcTNIrsAZg-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-42110b33fecso5730435e9.0 for ; Mon, 10 Jun 2024 02:32:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011931; x=1718616731; 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=uYVaQkWqZSrx/i/zySmVb2SXxUjFViRl1HRUPLR6DN4=; b=pR7gV0rnuhMOq4rUeNKjcvgCPRxqYpn0iDfy8mzhY0qgWtZcipVI/rLqKyUNtnSyNk 1R6r+Y8a04gYVuztVOKOtRJ8p2mwghHuRdqV4HO7pFc73n8TDDXj4d+VnNf+fgpy1Fsm gD4Az+nsdUN3kzQTZ4QSu5g8H54mpAVrRoJ7KdqpQLQADzxRis4eioDX7vhWlS8yv7KF M55cK3jZHontPiDm6lz+fTrx026IIEQchCpN/bZxJo7yEXZxc2+Xa5n5TQsUVMhcBvsM jIi1F6TgDVm8GPEA1y3mOHmUJY86DFzHlJBbrBTT9V1/OFNlx79a52GEvKdGMpO0cAG0 W2kw== X-Forwarded-Encrypted: i=1; AJvYcCXZ4brmIprFiI1WaBO821w/3PH3XeFBSx43/dfRzUh+V5DyiQLVTv4LOq0cLCddx5rYlDxYiEsXc6fc52aHYBwI5H5XNP+GSDoDvYmV X-Gm-Message-State: AOJu0YwZGyING1hwhqb86Cl1sbLKSY+1tiYFmlfvccbQ8L6zzwB+0fZg 8wxtKds/gvuKwZyGHczXXfEajNsJh/OEbtLOWDisVvY55dh7A9PFLHLiU/P8FpszkqzhOOOrDiJ c6sytXVrvzvkVab4nfNpkvoj5Z1AOVwPUxf3OIKTW7dCEsnkbhFHhu2hD+74fpw== X-Received: by 2002:a05:6000:1ac7:b0:35f:229e:9c6d with SMTP id ffacd0b85a97d-35f229e9eafmr2316700f8f.6.1718011931037; Mon, 10 Jun 2024 02:32:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHC5EISwyWHtH2eTnirfsjXBBnu5C7SW4kxtaGwTzx8vXOIkqsWX8QFdvGZWKIwJ9P9VX916w== X-Received: by 2002:a05:6000:1ac7:b0:35f:229e:9c6d with SMTP id ffacd0b85a97d-35f229e9eafmr2316683f8f.6.1718011930743; Mon, 10 Jun 2024 02:32:10 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:10 -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 v8 11/13] PCI: Remove legacy pcim_release() Date: Mon, 10 Jun 2024 11:31:33 +0200 Message-ID: <20240610093149.20640-12-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 0bb144fdb69b..e92a8802832f 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 Thu Feb 12 17:27:26 2026 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 92F2874C1B for ; Mon, 10 Jun 2024 09:32:15 +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=1718011937; cv=none; b=ebIwAhM6AxKzzl89uId4rpOSZp7EsqlnzgLtL9FDIXddEWqOHWGUKXs362Eyk/jIFfN/zps0Ob3ZBacbVeXGqISZME4pWLeaLLs5LMfW7Ud89deA/1ko2oHMrGK4w1jh5TMv9HZUFYGwwZ/fyu7lGc/5MkeQNxsy4kivLVmOytU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011937; c=relaxed/simple; bh=39N7dQNsh6HdaHQrF4jqoIMCjCm9/18LRI3vr0xgqik=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MOXbKGeRg7Z5/P9O2O44uBSjlj+23YMkL6iwOxvCTIMntEwuBRG8suo0AAtpkIvOw6AbpYJ2pZMbuoRIKxaAUHB3fpRjup8vhKpheyoG71KXzgrKoeD6IypZPxO+I8AXJDBgWxjxwP/M5JNrqpMcDOiVmgmV9cIu7VQ8WwuAwZs= 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=H+iKzw64; 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="H+iKzw64" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011934; 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=XYyS0MFkgb40Rf9CNRl6khW3DB/TwdBG9c2B8GVIp3I=; b=H+iKzw64zQZE+amnC154GtNsxhsWoRqXmcPId5ynQOciNuoQlnqDtZAeEUVBdyIFDcDDFX CR3Kups+IZDpa2IDYVpXgalWBB1Rla8JzLC4ialhspZVFEmIsc1NdpsXVKOceHEhP7/09e 170mMCicJIcmsOMQeZ+4EFpgrpXO7oM= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-606-VAKgIYGqMfaCjX6MZByqvg-1; Mon, 10 Jun 2024 05:32:13 -0400 X-MC-Unique: VAKgIYGqMfaCjX6MZByqvg-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-42180ddc0b9so1692075e9.0 for ; Mon, 10 Jun 2024 02:32:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011932; x=1718616732; 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=XYyS0MFkgb40Rf9CNRl6khW3DB/TwdBG9c2B8GVIp3I=; b=SvmT2GZcQQ6xjB+eVh7IpztilD5ftPSBcm0EFGENpQH/frDKFa3SFsB0JjZ4Ux32ZF Kl93dwmtS+Su/fOyqDJTCTsZj1oc4d+iKfyRsCBpQJhTnSuxiZTfUuExIX5F3VxL9ALe ntW6br6XEKq7K3eirMPfSvC4D8TDlrUYc+ca6aijbNZo+Lxb+hxGv5y/+RYZwbFSHB0C 9U+MjgG0g7JBoERygIKJfkeYJ8mnguOnsbmam+qZVid4cG6zYlvQ534SCGiVMb3Wb52y tQK0g9PIpRcQIm452UGPIhTZ8IIqoOO4iVpagsGGt5iyEOsB3apphFgRY8A9FpHMZPkC EyaQ== X-Forwarded-Encrypted: i=1; AJvYcCULnZueACO7vDMoxFPdwhGX+GcisqBLeOlkoA6hp4T+tSUiTuSk3Ec1j3Ouw684BxSpHHUHwmphOQwdRR35D6VFQoP4kLMhg5l5bTaE X-Gm-Message-State: AOJu0Yzp5g5Obu5LQYjcVWESjLH8kYDJXyjEeIGsP92BHuzh5J2o/ntu +kr6VS+YB8x0+UUV4WGrtPLuSp2U4ETnRFx3r0nOK8z4Z/oIejTjZpfus+DByiURTinBtu6ZPgb EYH/VSZ2TcnGVeFAWw32PX9c3z2yLB9xfmlVmS70Q38SeLZAlP3dUfk/sphuntQ== X-Received: by 2002:a5d:526a:0:b0:35f:1412:fa8a with SMTP id ffacd0b85a97d-35f1412fcddmr3111484f8f.1.1718011932106; Mon, 10 Jun 2024 02:32:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFa4v7/IL4a5fGSrL47j3kH3zCfktetXms9w5+CrxpIYCETGQiN/XQkBtYRt2zSMtVQVzGA1A== X-Received: by 2002:a5d:526a:0:b0:35f:1412:fa8a with SMTP id ffacd0b85a97d-35f1412fcddmr3111472f8f.1.1718011931796; Mon, 10 Jun 2024 02:32:11 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:11 -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 v8 12/13] PCI: Add pcim_iomap_range() Date: Mon, 10 Jun 2024 11:31:34 +0200 Message-ID: <20240610093149.20640-13-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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 e92a8802832f..96f18243742b 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 cc9247f78158..bee1b2754219 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2304,6 +2304,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 Thu Feb 12 17:27:26 2026 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 C5E1F8005B for ; Mon, 10 Jun 2024 09:32:18 +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=1718011940; cv=none; b=IcXnUJpA3/Ce7+HLPQnkFR9BfDxhC4Uy34wdg1HXHhww50upjwCCBx0KPIf6GAtza06vH4ojyZjFWzS7ye9SYCv5gOy75b7aOtBqb2rQoeqG89x1FHRCl3bwV9mbftkHGkblV211teCdzub7ttaqtDQdzNdEPokCBxfgM1pq744= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718011940; c=relaxed/simple; bh=iGO9d2tDV8jgMXAb9ljo4BmxYhAXls7ncq8pchC9GTM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k1efuVhVqg7y3OBowjQVPfpkJPiGRG8bnQLp4BiL/trlaD1lBOwew2s7sxvFYTfQlNxU1TYnUgL6rpfAPfZq/rw6wqhjZ5r+NIlKwlIk09fcezFY6FbNCyThZItSdrfNwhNa+jO1hbzNlUm0ei46fnpFYVs/Fi8Oy7N2qRN4nK4= 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=hRiGn2ji; 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="hRiGn2ji" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718011938; 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=hRiGn2jiTKeoe6jpy3OAr3JM0PnnpWtIG7YuFWuY3A0y8I9Q4heDm+4RaXHDblQePfbacb OaMurPuqTK6UFRYT0pWY+M1ZwgeKp3/uUg2zZND5nhtYh7EY5nleqmX2pLXfjGd8Jb2o1C PekcFdP26u7hx5L1OJofLMfkiMvutVA= Received: from mail-lj1-f198.google.com (mail-lj1-f198.google.com [209.85.208.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-427-WmZVQbNOPZGY2KqBT5Fseg-1; Mon, 10 Jun 2024 05:32:14 -0400 X-MC-Unique: WmZVQbNOPZGY2KqBT5Fseg-1 Received: by mail-lj1-f198.google.com with SMTP id 38308e7fff4ca-2eae267d112so3093191fa.1 for ; Mon, 10 Jun 2024 02:32:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718011933; x=1718616733; 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=WwsIPWcLN4KNYF2ALETS/H3q/l66ooD3/Muo9zbESlW9VPxEh95cH4WE7EaSg7jOXC lPA0oOh2DfS/M1pKjJLuGdF5qyAsAlG/Txgr1D2A4tH26wYJmRZ5CMEhn1UawACFHmaV l694Ar11iHr5W1O8qbzBo9uKAg9o2hVzY/cI10dg81q5hMy/M6xjv7vZyp3zQvaoc7A8 DlLSXLag9BtAJf6d/HNtBIXeiYKqFc0Cv6gn6r0+MyKQPZYmDY4js9MZ95Xr1glMxs6d yr5wGxbXvWLXP6r/QCWcGGKuXeyAuharDiF6udXJTg7BhehQrN2AzfLZYsdcHHCzTM5x 57NA== X-Forwarded-Encrypted: i=1; AJvYcCVSqFuS/k9622OudMwdipXkk9yMT1kJ6Y4CTbpCG9XI9lE0rvUlt58WnIU4PX/IiNqZ8OTE73GBmd3T4Yj99Dq3X2vrHDK1TtP7eJVb X-Gm-Message-State: AOJu0YxPIdjrrY6hA2nXcMSLF4s3hJe1bQKCxZgWuR346px9rUwCFd6X n3G1EmXp+MpCcay+CNRfusTM7if9ofD1KS6QJywTeoh+S4H2ZORh0MZ2sFbwkQMeqwEWBDuWpJU LB5ZC4LDcixiH6rRoF3sw20HQl1XWtQdDyOje5mQ8f/+ptdksjlZioelfPMjdVQ== X-Received: by 2002:a19:2d0d:0:b0:52c:8857:7631 with SMTP id 2adb3069b0e04-52c88577793mr2123921e87.1.1718011933078; Mon, 10 Jun 2024 02:32:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFfUeStS0N5eJVU1XtsiGlp2iqafq61SQZHkJWQKYKYv8JTZLy1IaAESVE2BsaioUjz+IfNVg== X-Received: by 2002:a19:2d0d:0:b0:52c:8857:7631 with SMTP id 2adb3069b0e04-52c88577793mr2123916e87.1.1718011932865; Mon, 10 Jun 2024 02:32:12 -0700 (PDT) Received: from pstanner-thinkpadt14sgen1.muc.redhat.com (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35f0ce4b62fsm7257545f8f.80.2024.06.10.02.32.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:32:12 -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 v8 13/13] drm/vboxvideo: fix mapping leaks Date: Mon, 10 Jun 2024 11:31:35 +0200 Message-ID: <20240610093149.20640-14-pstanner@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240610093149.20640-1-pstanner@redhat.com> References: <20240610093149.20640-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