From patchwork Tue Feb 6 13:39:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 197446 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1543703dyb; Tue, 6 Feb 2024 05:41:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IHj4cErEEKeHdbUMMRnCmSKALfCE0mdS5HtReyxQYhjBKqeZjfQCBxHtuGkgS23GYC0ndVU X-Received: by 2002:a17:906:550:b0:a37:247d:59 with SMTP id k16-20020a170906055000b00a37247d0059mr1841848eja.25.1707226915833; Tue, 06 Feb 2024 05:41:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707226915; cv=pass; d=google.com; s=arc-20160816; b=kYE6Ryni6hl2Zog1bhDmftXOWNawE2i3VG9mDSXXQfz7GYIv1qomiBSxjKjm/Q94kw BnGaJya01Mt3qMYQy8uP9dOnBHfxup56ehcjRC5VrqN+10J+/i57paTjyXullLcESoxQ XLHgqqhvb80+ZSLuEhCHBaqqhcnJhRvZtYwQxGUI55E9fqB1OQ/U4/wE38KDpGpn5V6G nXISJ1Zc/Kxr2+XrKBGbnb8LrryzwEAHSKxK4jA6L65WdWHstNmiP0kY17wJvsIB1h7B B3xxrISQr5owcaRco2TSVnXAkJMhtY2K830lWW4Xm22nuIh9dQoHFSTBm0DVfuxRfTTJ PPuQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=jPhqNOqxfCv1D+Eeh5ibCQbTiDS1skQv+I51iBzSayk=; fh=8vt2IiS8An50gs/N8Y7+P9JXhox0zG3wR8Fi1X0dxg0=; b=poemjXK3g1HBuk1vtPj9tc8qwjrF/KgNshlDT7YIJj6SpnKatBcwpe913zXAthmuwP BE+EiOQkL6keEmFYjhP5D/Kd+1is9DBfeV/p87xt4YmdXsUMW/Muldx0XjtwtDFCQqxq Rhrw9I/SuUk4X728Hcoz33izYm5PDH2JxL/TX55R0+1emLtgGqPLBrfByeGVttXLTLGa GL8hirO7u07t4kROsxvE+UPMqq11MJF7aJhhqzZAX3m+2wmkcS04S9IY8xHFf+IpDGCG hrcVTVRvKnvGg6FbuwV5esmM/8jALjPmOdqhXf/bLiHRwwICjYPcJxdWFkGYao9e8rvO tDrw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="XZI2v/Bq"; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55031-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55031-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCXM6g1OSKvDxCgPufI16cJs2VG8M5xGut70leBxzfnraHAGq9EnzTGeV1o1WntyENnIEeId226njUPXkXwDVdThLbq+QQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id f20-20020a170906495400b00a36fa9f1919si1109108ejt.452.2024.02.06.05.41.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:41:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55031-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="XZI2v/Bq"; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55031-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55031-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A30751F258A8 for ; Tue, 6 Feb 2024 13:41:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C59B4131E45; Tue, 6 Feb 2024 13:40:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XZI2v/Bq" 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 C8EA3131754 for ; Tue, 6 Feb 2024 13:40:34 +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=1707226837; cv=none; b=Ig9mPp8Me+EpQjkug2JnVpxOyY/TYInrqipXY6vqttzJ9uaQzwNW0v3vf/tNkRj4prAs8LSLUPFLFVC41EevZ1SOm8cWzbNO7gkDyqIfTaezlI60sH7e9UNEvoBRlNAxEA4sNSmXJO1wIla0hJ3w9tunkxNa+b6bE045SZ6EyMw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707226837; c=relaxed/simple; bh=7KglswOihbbFIPfis3Yyk+inqTSCz1crMZ9ws0GOWR8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Z84f1jFVE0YoCSOhcvfgTOuP8zy3+2fg7e0MPfA7ypqfJpEAY47mRncQ5uB5RnRGdDsH7vOw6XxYf7739gJOqSWnVvAA4MKpm6sZ6npW9BKv2kgLQ3Ie7ByXqje4NSP3uyEsQt3Q8EoDANLnl53BaXe18Kpv1QAz9kuFljs1yC8= 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=XZI2v/Bq; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707226833; 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=jPhqNOqxfCv1D+Eeh5ibCQbTiDS1skQv+I51iBzSayk=; b=XZI2v/BqFAhnQWAnu4R5OOT5w9nLqvIwdSB2yP99y7JK/UXMG8h68QXKlgsAsKdhBHz/Qh NQLF1YVGbZrTOS4oC5I7HiqSvuYVrA180bBGTFKa3T5PvQlkr4Um71Dy3MNzYFTvIOK6dQ iMig6ynslVfaOAY5UdP7H9NAWxJ66Fs= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-624-pR25HTZKNyS-h8GLfDU88Q-1; Tue, 06 Feb 2024 08:40:31 -0500 X-MC-Unique: pR25HTZKNyS-h8GLfDU88Q-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-78356ddf3cfso293205985a.1 for ; Tue, 06 Feb 2024 05:40:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707226831; x=1707831631; 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=jPhqNOqxfCv1D+Eeh5ibCQbTiDS1skQv+I51iBzSayk=; b=IRWTsgAnrnIv3m/1//2z4TqrdtoZcIXad1Ar1wWKZ7nq4JXXyW1MEKgNW4qqln2y2j E3AN82eDmbHpZnmFKZjMxJOV9heLCsdTqFSKrnfP4ohBn/BaITS6r/qQ5R99b/JledGE 04C6mjDw21kwX4+rMxVm59kRc8Ng26/lL/KCLKUbuu3hJIIZfKQHby5+I9W0pvfX9Ek0 TtJMK9Et2CkWFZclxfsNtb51z0XXo5JZtZXPBXehl6p+ZTMxzQ5eHTeLMmYpnxZFS2Y9 eZHBi/389BwSBiiwLXTjOuMlL3StYjfk2CUDLJ/GowauHaE7Ed+GjZMmch8nCyhIc0mV qsQA== X-Forwarded-Encrypted: i=1; AJvYcCXQa22a0c6RAhz1cvzMsblLVIvUXG786txu/jhMwgxF1EaaOi4LBeal13JrKVD4R8sul1zr4BX8E0FsX9r7wXA/X4IAYEoy/bR+fF+h X-Gm-Message-State: AOJu0YxA2jyYMoSogtsShY8Pjv2cMdcpzgAxW7A5NfYUlKfuY/AHaQTr NPtoz62A0GNXD7lzOtIODSCYrAkFTijnUrZZb3L6FuhXfxFcd8tCqe6iYpwQJCUyLX6+V3H/9nE VQwlWcIsauolnGzgtma1zyoFfeV6CwGktZ+FFI05sdZIIpwchyzWka3HH24taEg== X-Received: by 2002:a05:620a:17a1:b0:784:ae7:20c1 with SMTP id ay33-20020a05620a17a100b007840ae720c1mr2365427qkb.5.1707226830545; Tue, 06 Feb 2024 05:40:30 -0800 (PST) X-Received: by 2002:a05:620a:17a1:b0:784:ae7:20c1 with SMTP id ay33-20020a05620a17a100b007840ae720c1mr2365392qkb.5.1707226829972; Tue, 06 Feb 2024 05:40:29 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWVSZrnY5CjerkB8j8yhcI+1q9GkDqgS7IO0ZpFdJvZpYj3TnYCRrbhi3hi3wAUdgfJnZHqpP0MFaeTxIt2Zx7u4FgHt/9cHsj5LBhSCqk4n75XEVaXYN0flL8fAtdcBU1BI/xY7h7cJwA0QOArhsWERp51JD+hJjK8v5/m76bkea7y6fkyrgynN6VmpGi2fE0AOyUYC2LVtLn4pS/qChitiw5DodMfn3b2RBby5O3Qb5DiImQ6QEu2/5PKPoJI6Q/ThvTCNoGAiGaHLCNQw6NNoYznLw3vmfIy7GxuIixHkJiBNGTRQDBHfyH7r3NhDlyb/lhzZKm8zM07nnBHki0hQyqEX/13gPuxOHxkHPbgBuas6ziX/0BK3fsUeex7lVjfmpBE Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id vu4-20020a05620a560400b0078544c8be9asm903791qkn.87.2024.02.06.05.40.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:40:29 -0800 (PST) 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 v3 01/10] PCI: Add new set of devres functions Date: Tue, 6 Feb 2024 14:39:47 +0100 Message-ID: <20240206134000.23561-3-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206134000.23561-2-pstanner@redhat.com> References: <20240206134000.23561-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790157170053956576 X-GMAIL-MSGID: 1790157170053956576 The PCI devres API is not extensible to partial BAR mappings and has bug-provoking features. Improve that by providing better alternatives. 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 hat the PCI devres API has been ipmlemented 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: void __iomem *mapping = 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 singular pcim_ functions that use devres directly and bypass the legacy iomap table mechanism. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 473 ++++++++++++++++++++++++++++++++++++++++++- include/linux/pci.h | 11 + 2 files changed, 479 insertions(+), 5 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 4bd1e125bca1..4e47f00a03a2 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -8,10 +8,215 @@ */ #define PCIM_IOMAP_MAX PCI_STD_NUM_BARS +/* + * Legacy struct storing addresses to whole mapped BARs. + */ struct pcim_iomap_devres { void __iomem *table[PCIM_IOMAP_MAX]; }; +enum pcim_addr_devres_type { + /* Default initializer. */ + PCIM_ADDR_DEVRES_TYPE_INVALID, + + /* A region spanning an entire BAR. */ + PCIM_ADDR_DEVRES_TYPE_REGION, + + /* A region spanning an entire BAR, and a mapping for that whole 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, + + /* A ranged region within a BAR, with a mapping spanning that range. */ + PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING +}; + +/* + * This struct envelopes IO or MEM addresses, that means 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 = -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 here. + */ + +/** + * __pcim_request_region_range - Request a ranged region + * @pdev: PCI device the region belongs to + * @bar: The BAR the region 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. For example for kernel-exclusive requests. + * + * Returns: 0 on success, a negative error code on failure. + * + * Request a ranged region within a device's PCI BAR. This function performs + * sanity checks on 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 = pci_resource_start(pdev, bar); + resource_size_t len = pci_resource_len(pdev, bar); + unsigned long dev_flags = pci_resource_flags(pdev, bar); + + if (start == 0 || len == 0) /* That's an unused BAR. */ + return 0; + if (len <= offset) + return -EINVAL; + + start += offset; + len -= offset; + + if (len > maxlen && maxlen != 0) + len = 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 = pci_resource_start(pdev, bar); + resource_size_t len = pci_resource_len(pdev, bar); + unsigned long flags = pci_resource_flags(pdev, bar); + + if (len <= offset || start == 0) + return; + + if (len == 0 || maxlen == 0) /* This an unused BAR. Do nothing. */ + return; + + start += offset; + len -= offset; + + if (len > maxlen) + len = 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 = 0; + unsigned long len = 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 = 0; + unsigned long len = 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 = to_pci_dev(dev); + struct pcim_addr_devres *res = 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; + case PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING: + pci_iounmap(pdev, res->baseaddr); + __pcim_release_region_range(pdev, res->bar, res->offset, res->len); + break; + default: + break; + } +} + +static struct pcim_addr_devres *pcim_addr_devres_alloc(struct pci_dev *pdev) +{ + struct pcim_addr_devres *res; + + res = 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 = a_raw; + b = b_raw; + + if (a->type != b->type) + return 0; + + switch (a->type) { + case PCIM_ADDR_DEVRES_TYPE_REGION: + case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING: + return a->bar == b->bar; + case PCIM_ADDR_DEVRES_TYPE_MAPPING: + return a->baseaddr == b->baseaddr; + case PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING: + return a->bar == b->bar && a->offset == b->offset && a->len == b->len; + default: + return 0; + } +} static void devm_pci_unmap_iospace(struct device *dev, void *ptr) { @@ -92,8 +297,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 = platform_get_resource(pdev, IORESOURCE_MEM, 0); * base = devm_pci_remap_cfg_resource(&pdev->dev, res); @@ -341,15 +546,80 @@ void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) tbl[i] = NULL; return; } - WARN_ON(1); } EXPORT_SYMBOL(pcim_iounmap); +/** + * 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(). + */ +void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar, const char *name) +{ + int ret; + struct pcim_addr_devres *res; + + res = pcim_addr_devres_alloc(pdev); + if (!res) + return IOMEM_ERR_PTR(-ENOMEM); + + res->type = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING; + res->bar = bar; + + ret = __pcim_request_region(pdev, bar, name, 0); + if (ret != 0) + goto err_region; + + res->baseaddr = pci_iomap(pdev, bar, 0); + if (!res->baseaddr) { + ret = -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); +} +EXPORT_SYMBOL(pcim_iomap_region); + +/** + * 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(). + */ +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 = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING; + res_searched.bar = bar; + + devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched); +} +EXPORT_SYMBOL(pcim_iounmap_region); + /** * 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 * * Request and iomap regions specified by @mask. */ @@ -402,7 +672,7 @@ EXPORT_SYMBOL(pcim_iomap_regions); * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones * @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 * * Request all PCI BARs and iomap regions specified by @mask. */ @@ -448,3 +718,196 @@ void pcim_iounmap_regions(struct pci_dev *pdev, int mask) } } EXPORT_SYMBOL(pcim_iounmap_regions); + +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 = pcim_addr_devres_alloc(pdev); + if (!res) + return -ENOMEM; + res->type = PCIM_ADDR_DEVRES_TYPE_REGION; + res->bar = bar; + + ret = __pcim_request_region(pdev, bar, name, request_flags); + if (ret != 0) { + pcim_addr_devres_free(res); + return ret; + } + + devres_add(&pdev->dev, res); + return 0; +} + +/** + * 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(). + */ +int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) +{ + return _pcim_request_region(pdev, bar, name, 0); +} +EXPORT_SYMBOL(pcim_request_region); + +/** + * 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(). + */ +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 = PCIM_ADDR_DEVRES_TYPE_REGION; + res_searched.bar = bar; + + devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched); +} +EXPORT_SYMBOL(pcim_release_region); + +/** + * 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 @offset to + * @offset + @len. + * + * The mapping will automatically get unmapped on driver detach. If desired, + * 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 = pcim_addr_devres_alloc(pdev); + if (!res) + return IOMEM_ERR_PTR(-ENOMEM); + + mapping = pci_iomap_range(pdev, bar, offset, len); + if (!mapping) { + pcim_addr_devres_free(res); + return IOMEM_ERR_PTR(-EINVAL); + } + + res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING; + res->baseaddr = 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); + +/** + * pcim_iomap_region_range - Request and map a range within a PCI BAR + * @pdev: PCI device to map IO resources for + * @bar: Index of BAR to request within + * @offset: Offset from the begin of the BAR + * @len: Length in bytes for the mapping + * @name: Name associated with the request + * + * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure. + * + * Request region with a range specified by @offset and @len within @bar and + * iomap it. + * + * The region will automatically be released and the mapping be unmapped on + * driver detach. If desired, release manually only with + * pcim_iounmap_region_range(). + * + * You probably should only use this function if you explicitly do not want to + * request the entire BAR. For most use-cases, combining pcim_request_region() + * and pcim_iomap_range() should be sufficient. + */ +void __iomem *pcim_iomap_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len, const char *name) +{ + int ret; + struct pcim_addr_devres *res; + + res = pcim_addr_devres_alloc(pdev); + if (!res) + return IOMEM_ERR_PTR(-ENOMEM); + + res->type = PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING; + res->bar = bar; + res->offset = offset; + res->len = len; + + ret = __pcim_request_region_range(pdev, bar, offset, len, name, 0); + if (ret != 0) + goto err_region; + + res->baseaddr = pci_iomap_range(pdev, bar, offset, len); + if (!res->baseaddr) { + ret = -EINVAL; + goto err_iomap; + } + + devres_add(&pdev->dev, res); + return res->baseaddr; + +err_iomap: + __pcim_release_region_range(pdev, bar, offset, len); +err_region: + pcim_addr_devres_free(res); + + return IOMEM_ERR_PTR(ret); +} +EXPORT_SYMBOL(pcim_iomap_region_range); + +/** + * pcim_iounmap_region_range - Unmap and release a range within a PCI BAR + * @pdev: PCI device to operate on + * @bar: Index of BAR containing the range + * @offset: Offset from the begin of the BAR + * @len: Length in bytes for the mapping + * + * Unmaps and releases a memory area within the specified PCI BAR. + * + * This function may not be used to free only part of a range. Only use this + * function with the exact parameters you previously used successfully in + * pcim_iomap_region_range(). + */ +void pcim_iounmap_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len) +{ + struct pcim_addr_devres res_searched; + pcim_addr_devres_clear(&res_searched); + + res_searched.type = PCIM_ADDR_DEVRES_TYPE_REGION_RANGE_MAPPING; + res_searched.bar = bar; + res_searched.offset = offset; + res_searched.len = len; + + devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched); +} +EXPORT_SYMBOL(pcim_iounmap_region_range); diff --git a/include/linux/pci.h b/include/linux/pci.h index add9368e6314..5c676a1a1a3f 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2318,10 +2318,21 @@ static inline void pci_fixup_device(enum pci_fixup_pass pass, void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen); void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr); void __iomem * const *pcim_iomap_table(struct pci_dev *pdev); +void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar, const char *name); +void pcim_iounmap_region(struct pci_dev *pdev, int bar); 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); +int pcim_request_region(struct pci_dev *pdev, int bar, const char *res_name); +void pcim_release_region(struct pci_dev *pdev, int bar); +void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len); +void __iomem *pcim_iomap_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len, + const char *res_name); +void pcim_iounmap_region_range(struct pci_dev *pdev, int bar, + unsigned long offset, unsigned long len); extern int pci_pci_problems; #define PCIPCI_FAIL 1 /* No PCI PCI DMA */ From patchwork Tue Feb 6 13:39:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 197447 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1543783dyb; Tue, 6 Feb 2024 05:42:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IEerEjkc3T9O4fubbbdXwCv6w87iZs6Gdb827mDrgsADbS7VW13+ei8eIUuOmRQ6LSNc2wc X-Received: by 2002:a17:906:6c8c:b0:a37:249d:9ff6 with SMTP id s12-20020a1709066c8c00b00a37249d9ff6mr1765037ejr.69.1707226922383; Tue, 06 Feb 2024 05:42:02 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707226922; cv=pass; d=google.com; s=arc-20160816; b=FJqJfEXZkA+Ma/ILkRcZhv74FbFTakKu6tKaojNJpvSup7RiKMjksjTDRqAhDK0qlY KyWLdPnPY+7OkzTRM6vVKw6qoWuXyHKazf5TVi9QhBc3Bsv6dqfFt4MkkKw/9TY2Tfiw 9zatOZ0QB7y4WDywFvGE9sNWe2vhhlXvpgId0V9DAZycuJIxU30JlLizikcU5cz74vIB tlFp6ZH0RJYHFf8sQYoWk6yqlAXUCcWMYAD537rlY6ryMygr4ZZGeynESZqRNewInRJ+ eNW6T88+NZ8GSMY+d0nR0AA0J82mc6fzQBGheA7Cvk9qrVEX+aW5A6DpiFh7RvXsg1Mp TwoQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=tQLdT5/oe3g+sF83qFqQEI70V41MpGXR9AOtVTLwpto=; fh=Zc4FofXHxsEj25VTDjkAYkLExmPJ0wZKabTZMYkuooA=; b=DyzFjlhk7ya1WAwpC46dut8N4hxUjSvMBJJzZHUloIZBQRqwwq326yeabnvqW72Eme McTLCWldVAqNlFQ45SpEBRTdtgadJCS2uB8HJtsMPC2SLjsDc/J+ITeCG7QbOfjYnoi9 I8qmzeLvrKLFw54nYOvlLoCG4I4Obyts4aUdRltUemm5tCvv8jIReaSK4+HFNN8gYM8w xrnQ8C3Nz0Hpc6V53XbW7Cy7v+CuufL+SRTBmKqx40RGko1tFN36qlone8VwONgHdjvW FTc7REVF0wiOi7GwxifKQydbfY7f0+tyoGTZ8/uan+A/BJpiTZ1xTGQ4ROJs44QAkoju O6mg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=KgX9RTYo; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55032-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55032-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCU244ZxE0j3AeOpjCsxxTUTAEon+auWwT5nVLB5KIL7Fp1XvBvYmT2Y6D7HwaSyTteomdw8WkrxsGM8Swjo7Au1HoBzRw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id gu10-20020a170906f28a00b00a338fffc21fsi1145219ejb.670.2024.02.06.05.42.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:42:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55032-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=KgX9RTYo; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55032-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55032-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0DF621F27427 for ; Tue, 6 Feb 2024 13:41:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 58C5213247D; Tue, 6 Feb 2024 13:40:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="KgX9RTYo" 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 3C04313175F for ; Tue, 6 Feb 2024 13:40:36 +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=1707226838; cv=none; b=tw5lXU/UkMEXD7L8i1m6dtWjRwTOTdOF9sFcxm8T3SBs/+zJssTwH5f5cMmfvoIvV0SS60I1p0bgmD3AiEbwCqS3tgS1IniRRvpuyGYS7a7v6vrR1wu6fhiHr3PRrIfqth2z8SMxt0boXzC8dsPgKnQs6RfZIyDmuYDRTi2GDm8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707226838; c=relaxed/simple; bh=lcMUgUb3jVUPExIMieyUd2sDp2aU5XE8ZW1LqgOKaIE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DbWz2f0iQ30kjz9HKoi1nnxccQwYCdfXPdCgS3DZ/cmkEpuPthssSZOYy13THVNgnfo2CbbjEBPqC96rZMl9eS2kev65wEnGYeOBgYzIJQGWTaPcc6SK8jMJuXW/MNhc6ft6xJKzN1Yxdsytjgzx+0SY8rEiMW5D2pGB9fyO0fA= 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=KgX9RTYo; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707226835; 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=tQLdT5/oe3g+sF83qFqQEI70V41MpGXR9AOtVTLwpto=; b=KgX9RTYozWUART/djuxF6X+yCr1ibSPzqtNPkEg4PbAn1FzMjeq2rAx0n3lb1Njsrng2yl ZD01PFy3SUxCbpQwfWUD2CUv2axw3zo0mxUCW9Gp+HKaZOi7ecpE3bsCgXy9wcpVjGSMG+ DOU3LmpKC1VE7sSVtYLpf7Z82VLKExU= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-661-ctIr52ZzMpOie5xpCphe1A-1; Tue, 06 Feb 2024 08:40:33 -0500 X-MC-Unique: ctIr52ZzMpOie5xpCphe1A-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-78356ddf3cfso293207985a.1 for ; Tue, 06 Feb 2024 05:40:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707226833; x=1707831633; 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=tQLdT5/oe3g+sF83qFqQEI70V41MpGXR9AOtVTLwpto=; b=BUb3YXytK/F6dPGOkqgxNh6okmclsR5cwYs6gXTq8pM7hXysfoESavOW4Kuro53fqQ e+ycwly89bJBhzdEV+vPPQX9y3K+QNR4RzFZ9nbmwrM6c4jWgzZzMHM2axl1tx3MLWdg 09REeBTtNjbirn5pMXGrazp/OUuWYJGUNRIzRDSgDalwiQqIQ+X1LUjxOcw7sxdKHun5 l8quU0TMuCHugP//ofSxWPASHd0WFDtHst934VDR5poW/faKRkOk2OXNBsqhsXZiAlOU o5OTE+p8LGVGlgUDXCQWOj4tdIqsfO5T4TVoxqZ+VQQeP7omfSHdQ6jYR3SoGICNEqxy H/Pw== X-Forwarded-Encrypted: i=1; AJvYcCXWb+Bj1p4pgRUz2VNtwypQ205LI5rRTFZmA8Spw0L+OXsqEXspeumdWbKm78v7D3BmEBVzLOF4Xd7rv3p00gJmk5ZQnHIQiRvfInFU X-Gm-Message-State: AOJu0Yy+rTqrHBePdnJFMYtDcHqL7Ph5lxWaIxBpazuWLkeB/BYscNBq jmtpyNKQKF2azjFGc0cond77UZYvnaQHbTMQlColkLV+lS5hcUnmY4pRwrPukiJqz053A6QpWzV P56MGgtarfg/RNW/dAgCztArVjVpUDVsbQTEsdoX6xJHMn/kzL9lOq7uZq9wInQ== X-Received: by 2002:a05:620a:40c5:b0:785:4475:7f7d with SMTP id g5-20020a05620a40c500b0078544757f7dmr2814019qko.4.1707226832928; Tue, 06 Feb 2024 05:40:32 -0800 (PST) X-Received: by 2002:a05:620a:40c5:b0:785:4475:7f7d with SMTP id g5-20020a05620a40c500b0078544757f7dmr2813987qko.4.1707226832472; Tue, 06 Feb 2024 05:40:32 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWPaGsX6AocbtIWNVY7FAyjMFGJ99rnmnclsSLrCyjQXWdv7WZTM7TsMvX0Uwel9E35owhxBDZ4SETZQvGDd7YzVQGB3B1h44+pb7Gcs7RSPecm2mUROIQKv6WuX7AEC0LjEG6t6aVjHjnMq1JiADxQj2scxDFmvxApJR1d4cOH9dQ3pyfKOOqQQ+rd79tT53h60cJQNKJUxpyk4En3GiMAZmaWL+S9a4OiI7wgKs1Lhxn19jI26sc6yEFY/XuRYT4QUntnW8GOwwtkQkPOCuHzDA9sAQ4CcHwBZogNQYfDZsEVIA7tjGxX8Jnax1YX0RWwvJfIxDrH3tattp/VJRbN99jakSTBKo9F+tS5dP5nxmoJiIQa9bIriFNDxHtJik2ScKtG Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id vu4-20020a05620a560400b0078544c8be9asm903791qkn.87.2024.02.06.05.40.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:40:31 -0800 (PST) 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 v3 02/10] PCI: Deprecate iomap-table functions Date: Tue, 6 Feb 2024 14:39:48 +0100 Message-ID: <20240206134000.23561-4-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206134000.23561-2-pstanner@redhat.com> References: <20240206134000.23561-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790157177238089416 X-GMAIL-MSGID: 1790157177238089416 The old plural devres functions tie the PCI devres API to the iomap-table mechanism which unfortunately is not extensible. As the plural functions are almost never used with more than one bit set in their bit-mask, deprecating them and encouraging users to use the new singular functions instead is reasonable. Furthermore, to make the implementation more consistent and extensible, the plural functions should use the singular functions. Add new wrapper to request / release all BARs. Make the plural functions call into the singular functions. Mark the plural functions as deprecated. Remove as much of the iomap-table-mechanism as possible. Add comments describing the path towards a cleaned-up API. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 374 +++++++++++++++++++++++++++++++++---------- drivers/pci/pci.c | 20 +++ drivers/pci/pci.h | 5 + include/linux/pci.h | 2 + 4 files changed, 318 insertions(+), 83 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 4e47f00a03a2..724429ab4f64 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -4,15 +4,43 @@ #include "pci.h" /* - * 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 of 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 the old + * API. + * 2. The region-request-functions in pci.c do become managed IF the device 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 /* * 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 { @@ -373,6 +401,16 @@ static void pcim_release(struct device *gendev, void *res) struct pci_devres *this = 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 = 0; i < DEVICE_COUNT_RESOURCE; i++) if (this->region_mask & (1 << i)) pci_release_region(dev, i); @@ -461,19 +499,21 @@ EXPORT_SYMBOL(pcim_pin_device); static void pcim_iomap_release(struct device *gendev, void *res) { - struct pci_dev *dev = to_pci_dev(gendev); - struct pcim_iomap_devres *this = res; - int i; - - for (i = 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. + */ } /** - * pcim_iomap_table - access iomap allocation table + * pcim_iomap_table - access iomap allocation table (DEPRECATED) * @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 @@ -482,6 +522,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 = pcim_iomap(pdev, barnr, length); */ void __iomem * const *pcim_iomap_table(struct pci_dev *pdev) { @@ -500,27 +545,114 @@ void __iomem * const *pcim_iomap_table(struct pci_dev *pdev) } EXPORT_SYMBOL(pcim_iomap_table); +/* + * 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, short bar) +{ + void __iomem **legacy_iomap_table; + + if (bar >= PCI_STD_NUM_BARS) + return -EINVAL; + + legacy_iomap_table = (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] = mapping; + + return 0; +} + +/* + * Removes a mapping. The table only contains whole-bar-mappings, so this will + * never interfere with ranged mappings. + */ +static void pcim_remove_mapping_from_legacy_table(struct pci_dev *pdev, + void __iomem *addr) +{ + short bar; + void __iomem **legacy_iomap_table; + + legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev); + if (!legacy_iomap_table) + return; + + for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { + if (legacy_iomap_table[bar] == addr) { + legacy_iomap_table[bar] = NULL; + return; + } + } +} + +/* + * 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 >= PCI_STD_NUM_BARS) + return; + + legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev); + if (!legacy_iomap_table) + return; + + legacy_iomap_table[bar] = 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 backwards + * compatibility. */ void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen) { - void __iomem **tbl; - - BUG_ON(bar >= PCIM_IOMAP_MAX); + void __iomem *mapping; + struct pcim_addr_devres *res; - tbl = (void __iomem **)pcim_iomap_table(pdev); - if (!tbl || tbl[bar]) /* duplicate mappings not allowed */ + res = pcim_addr_devres_alloc(pdev); + if (!res) return NULL; + res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING; + + mapping = pci_iomap(pdev, bar, maxlen); + if (!mapping) + goto err_iomap; + res->baseaddr = mapping; + + if (pcim_add_mapping_to_legacy_table(pdev, mapping, bar) != 0) + goto err_table; - tbl[bar] = pci_iomap(pdev, bar, maxlen); - return tbl[bar]; + devres_add(&pdev->dev, res); + return mapping; + +err_table: + pci_iounmap(pdev, mapping); +err_iomap: + pcim_addr_devres_free(res); + return NULL; } EXPORT_SYMBOL(pcim_iomap); @@ -529,23 +661,24 @@ 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 pcim_iomap() or + * pcim_iomap_range(). */ void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) { - void __iomem **tbl; - int i; + struct pcim_addr_devres res_searched; - pci_iounmap(pdev, addr); + pcim_addr_devres_clear(&res_searched); + res_searched.type = PCIM_ADDR_DEVRES_TYPE_MAPPING; + res_searched.baseaddr = addr; - tbl = (void __iomem **)pcim_iomap_table(pdev); - BUG_ON(!tbl); + if (devres_release(&pdev->dev, pcim_addr_resource_release, + pcim_addr_resources_match, &res_searched) != 0) { + /* Doesn't exist. User passed nonsense. */ + return; + } - for (i = 0; i < PCIM_IOMAP_MAX; i++) - if (tbl[i] == addr) { - tbl[i] = NULL; - return; - } + pcim_remove_mapping_from_legacy_table(pdev, addr); } EXPORT_SYMBOL(pcim_iounmap); @@ -615,106 +748,181 @@ void pcim_iounmap_region(struct pci_dev *pdev, int bar) } EXPORT_SYMBOL(pcim_iounmap_region); +static inline bool mask_contains_bar(int mask, int bar) +{ + return mask & BIT(bar); +} + /** - * pcim_iomap_regions - Request and iomap PCI BARs + * pcim_iomap_regions - Request and iomap PCI BARs (DEPRECATED) * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to request and iomap * @name: Name associated with the requests * + * Returns: 0 on success, negative error code on failure. + * * Request and iomap regions specified by @mask. + * + * This function is DEPRECATED. Don't use it in new code. + * Use pcim_iomap_region() instead. */ 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; - iomap = pcim_iomap_table(pdev); - if (!iomap) - return -ENOMEM; + for (bar = 0; bar < DEVICE_COUNT_RESOURCE; bar++) { + if (!mask_contains_bar(mask, bar)) + continue; - for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { - unsigned long len; + mapping = pcim_iomap_region(pdev, bar, name); + if (IS_ERR(mapping)) { + ret = PTR_ERR(mapping); + goto err; + } + ret = pcim_add_mapping_to_legacy_table(pdev, mapping, bar); + if (ret != 0) + goto err; + } - if (!(mask & (1 << i))) - continue; + return 0; - rc = -EINVAL; - len = pci_resource_len(pdev, i); - if (!len) - goto err_inval; +err: + while (--bar >= 0) { + pcim_iounmap_region(pdev, bar); + pcim_remove_bar_from_legacy_table(pdev, bar); + } - rc = pci_request_region(pdev, i, name); - if (rc) - goto err_inval; + return ret; +} +EXPORT_SYMBOL(pcim_iomap_regions); - rc = -ENOMEM; - if (!pcim_iomap(pdev, i, 0)) - goto err_region; +/** + * pcim_release_all_regions - Release all regions of a PCI-device + * @pdev: the PCI device + * + * Will 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(). + */ +void pcim_release_all_regions(struct pci_dev *pdev) +{ + short bar; + + for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) + pcim_release_region(pdev, bar); +} +EXPORT_SYMBOL(pcim_release_all_regions); + +/** + * 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(). + */ +int pcim_request_all_regions(struct pci_dev *pdev, const char *name) +{ + int ret; + short bar; + + for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { + ret = pcim_request_region(pdev, bar, name); + if (ret != 0) + goto err; } return 0; - err_region: - pci_release_region(pdev, i); - err_inval: - while (--i >= 0) { - if (!(mask & (1 << i))) - continue; - pcim_iounmap(pdev, iomap[i]); - pci_release_region(pdev, i); - } +err: + pcim_release_all_regions(pdev); - return rc; + return ret; } -EXPORT_SYMBOL(pcim_iomap_regions); +EXPORT_SYMBOL(pcim_request_all_regions); /** - * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones + * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones (DEPRECATED) * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to iomap * @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. + * + * This function is DEPRECATED. Don't use it in new code. + * Use pcim_request_all_regions() + pcim_iomap*() instead. */ int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, const char *name) { - int request_mask = ((1 << 6) - 1) & ~mask; - int rc; + short bar; + int ret; + void __iomem **legacy_iomap_table; + + ret = pcim_request_all_regions(pdev, name); + if (ret != 0) + return ret; - rc = pci_request_selected_regions(pdev, request_mask, name); - if (rc) - return rc; + for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { + if (!mask_contains_bar(mask, bar)) + continue; + if (!pcim_iomap(pdev, bar, 0)) + goto err; + } - rc = pcim_iomap_regions(pdev, mask, name); - if (rc) - pci_release_selected_regions(pdev, request_mask); - return rc; + return 0; + +err: + /* + * Here it gets tricky: pcim_iomap() above has most likely + * failed because it got an OOM when trying to create the + * legacy-table. + * We check here if that has happened. If not, pcim_iomap() + * must have failed because of EINVAL. + */ + legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev); + ret = legacy_iomap_table ? -EINVAL : -ENOMEM; + + while (--bar >= 0) + pcim_iounmap(pdev, legacy_iomap_table[bar]); + + pcim_release_all_regions(pdev); + + return ret; } EXPORT_SYMBOL(pcim_iomap_regions_request_all); /** - * pcim_iounmap_regions - Unmap and release PCI BARs + * pcim_iounmap_regions - Unmap and release PCI BARs (DEPRECATED) * @pdev: PCI device to map IO resources for * @mask: Mask of BARs to unmap and release * * Unmap and release regions specified by @mask. + * + * This function is DEPRECATED. Don't use it in new code. */ void pcim_iounmap_regions(struct pci_dev *pdev, int mask) { - void __iomem * const *iomap; - int i; - - iomap = pcim_iomap_table(pdev); - if (!iomap) - return; + short bar; - for (i = 0; i < PCIM_IOMAP_MAX; i++) { - if (!(mask & (1 << i))) + for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { + if (!mask_contains_bar(mask, bar)) continue; - 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 19f18c3856e8..adf3c627c712 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3946,6 +3946,16 @@ void pci_release_region(struct pci_dev *pdev, int bar) 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 = find_pci_dr(pdev); if (dr) dr->region_mask &= ~(1 << bar); @@ -3990,6 +4000,16 @@ static int __pci_request_region(struct pci_dev *pdev, int bar, goto err_out; } + /* + * 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 = find_pci_dr(pdev); if (dr) dr->region_mask |= 1 << bar; diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 2215858b2584..4883be71e40c 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -816,6 +816,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; }; diff --git a/include/linux/pci.h b/include/linux/pci.h index 5c676a1a1a3f..04a0065974e5 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2326,6 +2326,8 @@ int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask, void pcim_iounmap_regions(struct pci_dev *pdev, int mask); int pcim_request_region(struct pci_dev *pdev, int bar, const char *res_name); void pcim_release_region(struct pci_dev *pdev, int bar); +void pcim_release_all_regions(struct pci_dev *pdev); +int pcim_request_all_regions(struct pci_dev *pdev, const char *name); void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar, unsigned long offset, unsigned long len); void __iomem *pcim_iomap_region_range(struct pci_dev *pdev, int bar, From patchwork Tue Feb 6 13:39:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 197455 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1550369dyb; Tue, 6 Feb 2024 05:54:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IEDPWqe3+hQlMwFO1CapUg2wLplVjb+OMZn5azmUPDhFmGiTvDH6sI9EdrOF52WdpRgpTh2 X-Received: by 2002:a17:90b:4b83:b0:296:15e6:380c with SMTP id lr3-20020a17090b4b8300b0029615e6380cmr2604766pjb.45.1707227683398; Tue, 06 Feb 2024 05:54:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707227683; cv=pass; d=google.com; s=arc-20160816; b=Wl8UftTLeMFn7xtEfTWhPTu6eehUgYwKisaXjTU3QE2uisOQBptmsnJyBLw1she/Pw cMJFpQJGZCIdfw95R11nb8JbwtpQ/zoBvple2XUsgNy8KwfJ5iPkT7X51cV+0C8F8rcz KwbdYK0xdDMQMAF2AU6bPDbiwLXS7V4SLDh9ry65BvzD8sFYvBX3tIzLwDbVHVB6ifGl nv9oTHlbwrN7Ve9MLy5nvO/ff+TsyU84xWb22I5K+TJA3ZPfC1ps7ZHAL4/z+e42tesd f2gxiTQuDPrQ5cllcDg2pClEC7flREBF5xHzN1f9qErkIKEPuhCmhLrZY6YnGXP/8FoR 6FPA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=5W8BzE0YxhsoFZleLy0MgAqqm8C3W0oimN6YDb86Ncw=; fh=iP+6GjxiYq1zzsHTCW/eqPJCdBMzkA3o9uoO5qaGldY=; b=0HUATQUgvk5tmQynEjgt2CQKvbpvuzLNfEX7eN9T64VNaOIpJnwK2dnogaiXXWx4fx OIQ2FZY2lsDt16g7anqVaa4Cz04vsvk0sPnSB92PPDnBNtK4Si0OJP+a9Hfxv/6VX2BX YIFwK3HyotY07JTPg+GPmj7S1LLpG8MJOOGyYJZ3pXwChUcQfzhPCYZkwp1StwRQ2YOa dvUzJrhnj3X5vwGqM2FKVnqF9uHAfwwNyQp/iuryq07MejfNA7wuseUcoTVtSUCWm6qK izbhuzEyrmcXyHKpgv/H0UPaqRf7wwXfIzAaDOT6nDFLlCcTfMA2dTzKZ95KD1gG1WkG QCiw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=b4ggJ7N7; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55033-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55033-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCVIriDziDAkpcH2Hu17/dGjjoiHiicR47YR26oujsGbAcG1QWGJdGbOPyibkFSpUB7YRwMfgqVFw3HS9cKlgj/y1egtZA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id in18-20020a17090b439200b00296a2ce4d8csi1176326pjb.114.2024.02.06.05.54.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:54:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55033-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=b4ggJ7N7; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55033-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55033-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id DC6E4B26319 for ; Tue, 6 Feb 2024 13:41:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2A05D132470; Tue, 6 Feb 2024 13:40:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="b4ggJ7N7" 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 98F38131E2A for ; Tue, 6 Feb 2024 13:40:39 +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=1707226841; cv=none; b=slc62L0SGdS2HstzoQd3W9wGhH0ZMqirO9sIewstAFB1Zw40fSlx1wt6A/zT8todYCm8kjKb4J7eIOQW5IuhPkrSNCSw15UdlzWSUQp61CDoxgqK/k67qnFpzVk/JUrwnIjm4eFp7c67sxw6KdNuWiNHOUGKg5b/FLS/h0bAkY4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707226841; c=relaxed/simple; bh=AAw/We9WI+NQKj7wqdsP135RjOGeGnoDC7qmIfjGo+E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lyxsWIdwI0fhyqfEwKTcUKVpLwtMGs/ytwt4Gq8p4XVhrPXCe6wMrAHJouYtqDhX8j32OKCVUSGHAn41dootuy+GwN12+psWv0kYuDydvUylPKQIf+109J3jz2deWQRJYKTVkgRGWpOBqzSjmydy5L3XGBh+AzPR3dtyAWDTTFE= 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=b4ggJ7N7; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707226838; 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=5W8BzE0YxhsoFZleLy0MgAqqm8C3W0oimN6YDb86Ncw=; b=b4ggJ7N7UZLEkballnPqsikfZPOABYg7pafMvhGSHyB5rFZrUOgV4+pZHDzV47NYtmGbuF TEbHVVSqLa/hCZ3kRs+QcOSK/bHIgGixphh6OTKjpa9Ubk6Rs+G8hVWJhLBGRr40oH+A+i OZhDjrXo7w8T2/n9GbaJlzdWt3U/HqQ= Received: from mail-oo1-f72.google.com (mail-oo1-f72.google.com [209.85.161.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-591-d2OFRLtVNQq8ubH8ttWdAA-1; Tue, 06 Feb 2024 08:40:36 -0500 X-MC-Unique: d2OFRLtVNQq8ubH8ttWdAA-1 Received: by mail-oo1-f72.google.com with SMTP id 006d021491bc7-59a1c8f2f98so1137820eaf.1 for ; Tue, 06 Feb 2024 05:40:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707226836; x=1707831636; 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=5W8BzE0YxhsoFZleLy0MgAqqm8C3W0oimN6YDb86Ncw=; b=VXIbW1DzKW9Tv+vu1SVruIkUex+OpzdtNAJJ9ZXaisgVvKlvJ5thRcptXX46XFXaGZ lccccrHp2rcn0N3CHmBy3PqtRWSBK0M2XWCpnry7CRV6S99aUsM9Qc4FxNAks/a5f7Is Aq8PuVf4dnvfSdT+gposbwaPQDnINuq7Gk8d/5t77wwhlj3McrevANTJ1uT3GDO8vtsV PNh7+oy/IzGpotWdxR/EFdX2J7Y948DqMBVkzkZKRSf+aC+nl5jHcoxFrfUa9YPmSKAk HTNuik4c2zvubCGhIAF9nvMbsgGYXwxEnNAYtuVoJWvkEaEtgjUajbnkmeqeg9dO3yZT X4eQ== X-Gm-Message-State: AOJu0YxQWD1l+wfpDdo1A2YpTBVn/2Hz5DkHJR5/j1Fq+Lq64aoulfkj ePJInC59UFTCLjQg5VWSwkJjPiu5izzs9jYRNkvO5hfFaj3QqRgF2FBuNvTDrOjW9une0IVGj4a oh9V2rAlvMAxFb+3DH0T1m1NQjFhHuI1wfZbXX4rAZkKSsJEhcE2Wt/8gN/BZdA== X-Received: by 2002:a4a:cb8b:0:b0:59c:e48e:50fc with SMTP id y11-20020a4acb8b000000b0059ce48e50fcmr1959100ooq.1.1707226835596; Tue, 06 Feb 2024 05:40:35 -0800 (PST) X-Received: by 2002:a4a:cb8b:0:b0:59c:e48e:50fc with SMTP id y11-20020a4acb8b000000b0059ce48e50fcmr1959086ooq.1.1707226835264; Tue, 06 Feb 2024 05:40:35 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXv8TZ9TKxI54TnpwOXmdUBR+ZSKgnm57uwFvG5jHQ0xBdMGfDi2j1wZHd+A7P7FRSmu5kbx1QKl0JzhJQueNA745kD5roozvpDHSYO0exuh2dh3Gzuxpj4tXVZNGhkm2k0DX7sTP3nDEpaKhLONSab1vN2eGHFoYrZ2aPtOR1F8XXSdNuO/THjytXzDXL9WyMvMDBmldz1udLwphDCKGGlkAEcB8nkI0yJZvd/gTzKRTk/B4RtXOosq2hx/nox87IEwv7XSdXC+x92nDEgd/v0/by2QdEqpHUMR3qNKOQZMqCPjNMaJlUAgI/EQu4Xt69o+WayLDv2T/3wx840DmOxvArl6DFygfqbGgihnMXefz280N693rSNMg0bRv2Sb4WvnRKS Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id vu4-20020a05620a560400b0078544c8be9asm903791qkn.87.2024.02.06.05.40.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:40:34 -0800 (PST) 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 v3 03/10] PCI: Warn users about complicated devres nature Date: Tue, 6 Feb 2024 14:39:49 +0100 Message-ID: <20240206134000.23561-5-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206134000.23561-2-pstanner@redhat.com> References: <20240206134000.23561-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790157975358542586 X-GMAIL-MSGID: 1790157975358542586 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 bugs by confusing users, who came to believe that all pci functions, such as pci_iomap_range(), suddenly are managed that way. This is not the case. Add comments to the relevant functions' docstrings that warn users about this behavior. Signed-off-by: Philipp Stanner --- drivers/pci/iomap.c | 18 ++++++++++++++ drivers/pci/pci.c | 60 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/drivers/pci/iomap.c b/drivers/pci/iomap.c index c9725428e387..ea3b9842132a 100644 --- a/drivers/pci/iomap.c +++ b/drivers/pci/iomap.c @@ -23,6 +23,11 @@ * * @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(). + * If you need automatic cleanup, use pcim_iomap_range(). * */ void __iomem *pci_iomap_range(struct pci_dev *dev, int bar, @@ -63,6 +68,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 +115,11 @@ 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 +141,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 maxlen) { diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index adf3c627c712..5c8bca2c5945 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3969,6 +3969,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 @@ -3980,6 +3982,13 @@ EXPORT_SYMBOL(pci_release_region); * * Returns 0 on success, or %EBUSY on error. A warning * message is also printed on failure. + * + * NOTE: + * This is a "hybrid" function: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you need to implement a new pci + * function that does automatic cleanup, write a new pcim_* function that uses + * devres directly. */ static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name, int exclusive) @@ -4028,6 +4037,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 @@ -4035,6 +4046,12 @@ 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: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. */ int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name) { @@ -4060,6 +4077,13 @@ void pci_release_selected_regions(struct pci_dev *pdev, int bars) } EXPORT_SYMBOL(pci_release_selected_regions); +/* + * NOTE: + * This is a "hybrid" function: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. + */ static int __pci_request_selected_regions(struct pci_dev *pdev, int bars, const char *res_name, int excl) { @@ -4085,6 +4109,14 @@ 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: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. */ int pci_request_selected_regions(struct pci_dev *pdev, int bars, const char *res_name) @@ -4093,6 +4125,20 @@ int pci_request_selected_regions(struct pci_dev *pdev, int bars, } EXPORT_SYMBOL(pci_request_selected_regions); +/** + * 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: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. + */ int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars, const char *res_name) { @@ -4110,7 +4156,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); @@ -4142,6 +4187,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. @@ -4151,6 +4198,12 @@ 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: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. */ int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name) { @@ -4482,6 +4535,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: Its normally unmanaged, but becomes managed + * when pcim_enable_device() has been called in advance. + * This hybrid feature is DEPRECATED! */ void pci_intx(struct pci_dev *pdev, int enable) { From patchwork Tue Feb 6 13:39:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 197448 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1543810dyb; Tue, 6 Feb 2024 05:42:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IEkZR8dqY8If/6FYfloC0vTZbyTs7sYU9ad7eNlqGvRF5SCKpYN9IXDFkMOz+ebYVDa4+uH X-Received: by 2002:a62:e405:0:b0:6da:bcea:4cd4 with SMTP id r5-20020a62e405000000b006dabcea4cd4mr3649442pfh.16.1707226926147; Tue, 06 Feb 2024 05:42:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707226926; cv=pass; d=google.com; s=arc-20160816; b=a9zAWFI8f9Fne4K0mks+AnUsh3o5vYAbwhH8FK3hTIvtYSrVuQv9XIsqtvGS2nbjO4 tBboBHUbcExtkse7xU6ihbqDFQ496gIAtZIY4TNHL7rMxyIg9HGDBF0351qMQOP6DRUU AoF2LXlqBdcl/rSxd95n5o5TgN6MHXZak/J88DT8YsYIKBuPThf5mDicPD0pSJS8zaXh TISp1ZXJ3tCPQzP0EW1nf5qEpNQvbsIBHrvE5zKuGr2CJrBJhQ/MKfR2ZzxfKdU9+r2s hVlrR/tLoPDXRHv9BoPgf0Gvf1swYtdUB0XItUzbzMzHUNoNlK0mQ9k8IyOsVtYxnmjC AOPA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=JQlpJfDup+dDGOwPHLvFIbkEOafdDPtlHCm5JScFyak=; fh=h/L4NqwGeK7fqVLHh47oEVMJMHfED5ROEpLg33IN/1A=; b=Cj0n6KDplVCakEHHdiQycFhf6WKdu4r7RbcIwZgqHSHA3aPKJKX4If/dNL3ExplsTU qw6OqCgeTbA3QTSPslRqiNggbG7+cgrD8Eg46Ka4U5gTFlnu0aQ8RKbfnXEMh1y3DF19 JD8TDjSPysgT39lWcHounLYQgJMw3FCedSlhCZV+7TIVxCvl6B0kRasweOIBZRyP+qff 3+W4ZGie9Au0JG7Ld9N4+MYdb4x2QDPPrxcr8X8r28Tlm+0PWrMa5pV7aAxORptiiN1X Pf9I2Sm/tJzFB6RrZUp6Uc1rT9CArKzwCvuK66krJSAFrxCGCxuRVW2liTz1eFGU+tW9 OUlQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Myo6Ue7J; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55034-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55034-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCWffcyp+5/8DsgeT1YMl1vu33LeuDRfKSQuRXpUmeDqc59DARbCHcKsgCgSElFXSFl/nNjutO6TQzGn7Z7g7OqE6j6FXw== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id f17-20020a635551000000b005d8b81271dbsi1692409pgm.286.2024.02.06.05.42.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:42:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55034-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Myo6Ue7J; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55034-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55034-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id DD83028405F for ; Tue, 6 Feb 2024 13:42:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0650F132C07; Tue, 6 Feb 2024 13:40:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Myo6Ue7J" 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 7541D131E40 for ; Tue, 6 Feb 2024 13:40:41 +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=1707226843; cv=none; b=J2GJKT2fpIAVM4nDtdd29fk5NPT4nS94J4mZJ86Alf4Gwvmf8NLs/x95zYrxuxsu93wNviZHyfAoVWVS/tBdnPe+mo6ixnKKghkOMAkwgI2AEbkpG9V6vsaxwSApM0TsALuTp/H2DPZPGRv2hjoRFRFUFjFz+H54k4eX+hDG09E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707226843; c=relaxed/simple; bh=QbCJv1bU7q2v4rcQ1PiD2cRd7RTDQzeL/n/hRGzDIro=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BQYsIq0xkMGGlg3sb1y5cB+5edRur2Bfirxoa77YTvJJk5x9al0OrEOZBet374otczXPBONHWGXRwfp6Uw+tQzYhLl8Ro9MbftS1YRy7IEBWAx0TmPz6TTIO9sD0d/lInJjPIsj8FnBPX7A1hFgZPL9amtEPDqilRiQPJt4PUMw= 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=Myo6Ue7J; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707226840; 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=JQlpJfDup+dDGOwPHLvFIbkEOafdDPtlHCm5JScFyak=; b=Myo6Ue7Jy56DFKJ9QMyDdEgloqzHXEiua6Dl6pScHF4osmc0jqs0Z2sAb6k7rQRvA7QLmC 1lFWhtWzg7uMPmFHWBeM9UMzKM20oOBucMphdxi4i4ctenHzUtt6YlUTDQ2VExDx8Yfndf 9MZZOCSfdNJ7cqS+RpCU8XB1OD1IWYo= Received: from mail-oo1-f70.google.com (mail-oo1-f70.google.com [209.85.161.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-663-H676bn_ZN2Oe0OwNISOs8w-1; Tue, 06 Feb 2024 08:40:39 -0500 X-MC-Unique: H676bn_ZN2Oe0OwNISOs8w-1 Received: by mail-oo1-f70.google.com with SMTP id 006d021491bc7-59a44d084edso840063eaf.0 for ; Tue, 06 Feb 2024 05:40:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707226838; x=1707831638; 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=JQlpJfDup+dDGOwPHLvFIbkEOafdDPtlHCm5JScFyak=; b=npxHfamWgEgKGjrdQRc7Vt6oQB2n1bcA87cfFqvNFVaXVMoFbgTvy6pTSVXSjCZKJW jPjhwauSwMcZl9nV8/oXZbw7TeRXurCnrxWs6PY6NsT5ep9qe0G6HyAY/zpvDYL3N5AG g0xGk8YG41pdClAVgkUoNvQmA+s7UTl6lzxJWueVvw3WzSWzHwRKSwXnrXSddmkLrwLi WWS7ZIYGky6IW7gCOHDLSJW3LuC0avwvtzUd6tfNtFrgkvx9wgdN/LRssAuf5nRx6Eh6 hUAOpKtvL5mL4lwhVME85YWvy8RuruFVBV5EHO5SC8RT/PyMCu0z5S6m0rcsCy0mq9pM 6IHw== X-Forwarded-Encrypted: i=1; AJvYcCX+P1ORRYXrvaXeIrhdC9eXGuNrImDgSUhS+LgXjYPuvs77wlJX5SJMZprnOwZN3oHq2DqPrh6DH/l+TUWvciOtz5mEPOBgLByRbG0/ X-Gm-Message-State: AOJu0Ywe5cpHs8LuqD74SiE0H+KPMaj6g2ByTm+TYDH6y1dpG99OM22I K8itpn6DLOtGQ1KHjJP0AUacP3aYwQTk3ld9n/IX1e3dmKCj+RTXMFw3GHwXY8s0CN9Z/G1VS4B I2n3z/SR+OqDcAv+lJ6x9KpQpCJrzqabfwG6ubeOsWTqreT4s3ShQMdit7+krXA== X-Received: by 2002:a4a:a446:0:b0:599:e8ff:66d9 with SMTP id w6-20020a4aa446000000b00599e8ff66d9mr2755239ool.1.1707226838479; Tue, 06 Feb 2024 05:40:38 -0800 (PST) X-Received: by 2002:a4a:a446:0:b0:599:e8ff:66d9 with SMTP id w6-20020a4aa446000000b00599e8ff66d9mr2755229ool.1.1707226838133; Tue, 06 Feb 2024 05:40:38 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWU+2Py4oVZMn22mLfoM2xIqMZc/38FGSw2MgLnBY8Wr2BUEuzhQHfvNKHm3WFoodpY+EXTDq1RYZu0W1BMZkdvaqzuTQZT/DY4WwXuLmAvrSxdSVcd19JLtSLh68IMMTJhkbpU+D0ENxZWqk37knbLlUmMWYQpVBmDbaMf3QVOhm91V68H0cClZGtgS0+pA9CKC5czCOAUPDkCGSj0jIlX1nWMlz61FxCIncnPcBb4oHwfnZ4HeRX86Ou2IVSNktBhJ/smPmsYuLHgZzFZN0dohpJPlT/U2+5AvEeALNhHXGg/LxBc6ZlMweJeqlLZctMfp9RE64qtd2qoMAEduXBz9OTid2jbtNFzyIPiZJ0hiXxKTOcaGz29nch7GtKZLr5+l4lx Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id vu4-20020a05620a560400b0078544c8be9asm903791qkn.87.2024.02.06.05.40.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:40:37 -0800 (PST) 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 v3 04/10] PCI: Make devres region requests consistent Date: Tue, 6 Feb 2024 14:39:50 +0100 Message-ID: <20240206134000.23561-6-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206134000.23561-2-pstanner@redhat.com> References: <20240206134000.23561-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790157181708603759 X-GMAIL-MSGID: 1790157181708603759 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 a new pcim_ function for exclusively requested regions. Have the pci_request / release functions call their pcim_ counterparts. Remove the now surplus region_mask from struct pci_devres. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 49 ++++++++++++++++++++++--------------------- drivers/pci/pci.c | 50 +++++++++++++++----------------------------- drivers/pci/pci.h | 6 ------ include/linux/pci.h | 1 + 4 files changed, 43 insertions(+), 63 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 724429ab4f64..a0e8e47b2de9 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -22,18 +22,15 @@ * _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(). + * 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 using + * 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. */ /* @@ -399,21 +396,6 @@ static void pcim_release(struct device *gendev, void *res) { struct pci_dev *dev = to_pci_dev(gendev); struct pci_devres *this = 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 = 0; i < DEVICE_COUNT_RESOURCE; i++) - if (this->region_mask & (1 << i)) - pci_release_region(dev, i); if (this->mwi) pci_clear_mwi(dev); @@ -968,6 +950,25 @@ int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) } EXPORT_SYMBOL(pcim_request_region); +/** + * 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 char *name) +{ + return _pcim_request_region(pdev, bar, name, IORESOURCE_EXCLUSIVE); +} +EXPORT_SYMBOL(pcim_request_region_exclusive); + /** * pcim_release_region - Release a PCI BAR * @pdev: PCI device to operate on diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 5c8bca2c5945..c919f85a269d 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3935,7 +3935,15 @@ EXPORT_SYMBOL_GPL(pci_common_swizzle); */ 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; + } if (pci_resource_len(pdev, bar) == 0) return; @@ -3945,20 +3953,6 @@ void pci_release_region(struct pci_dev *pdev, int bar) 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 = find_pci_dr(pdev); - if (dr) - dr->region_mask &= ~(1 << bar); } EXPORT_SYMBOL(pci_release_region); @@ -3986,14 +3980,18 @@ EXPORT_SYMBOL(pci_release_region); * NOTE: * This is a "hybrid" function: Its normally unmanaged, but becomes managed * when pcim_enable_device() has been called in advance. - * This hybrid feature is DEPRECATED! If you need to implement a new pci - * function that does automatic cleanup, write a new pcim_* function that uses - * devres directly. + * This hybrid feature is DEPRECATED! If you want managed cleanup, use the + * pcim_* functions instead. */ 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 == IORESOURCE_EXCLUSIVE) + return pcim_request_region_exclusive(pdev, bar, res_name); + + return pcim_request_region(pdev, bar, res_name); + } if (pci_resource_len(pdev, bar) == 0) return 0; @@ -4009,20 +4007,6 @@ static int __pci_request_region(struct pci_dev *pdev, int bar, goto err_out; } - /* - * 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 = find_pci_dr(pdev); - if (dr) - dr->region_mask |= 1 << bar; - return 0; err_out: diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 4883be71e40c..f5944e3cb7fb 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -816,12 +816,6 @@ 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; }; struct pci_devres *find_pci_dr(struct pci_dev *pdev); diff --git a/include/linux/pci.h b/include/linux/pci.h index 04a0065974e5..83a683a1f4e5 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2325,6 +2325,7 @@ 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); int pcim_request_region(struct pci_dev *pdev, int bar, const char *res_name); +int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const char *name); void pcim_release_region(struct pci_dev *pdev, int bar); void pcim_release_all_regions(struct pci_dev *pdev); int pcim_request_all_regions(struct pci_dev *pdev, const char *name); From patchwork Tue Feb 6 13:39:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 197456 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1550538dyb; Tue, 6 Feb 2024 05:55:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IGxrNlfD0WXXeINQeTlvScBajkc3GvLRedUy7aQDuom+yszN2WFr8Sy5/VMPYaHaPyJ1rV2 X-Received: by 2002:a05:6358:6f1a:b0:175:6fff:d6a0 with SMTP id r26-20020a0563586f1a00b001756fffd6a0mr2786042rwn.1.1707227705038; Tue, 06 Feb 2024 05:55:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707227705; cv=pass; d=google.com; s=arc-20160816; b=yIj1JAth/Tli5MxM+B71EMBupY65q5qkXg8GKoiSIaDeGdiW04LHd/CotGRpjCW5D9 bLEffyp5ExLLjJnE8JVuW/ZHuNUL4BPaHribADTksqkNDnHIoy23RYQFe+uS5rgb/bjD XuK60GPEglR4bRtHQGzxfFz5sZ7r+tEgh2rnzQM8LljOPKlF9CsYubXzCgTaRKVewy0I BSoyQT/iZjZrVYpsH52HL2hb7WyVny99V5jeNFt2+75WfHbqc3hEFBXKe3uAu3QieiH8 7mOm8I6CK5x9bu6awdI7ASBQGjo4510kgffocXcrOrmeputub4Stlv7ETxQ3R9sm8xsl ocjQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=d3QB4wa/ZeBRooj+eB6dP6QJm1U/XuptG2DuaZLsRqk=; fh=NjSfsPUP+LeDBvmuUy8lCjp5jP1Qk38gAYB5Lyaw/+I=; b=JzwlzEIWZs1A71HC6N2A3hSXvfzULajTnDOGdNncQOp0g4IN1z3bW5AJE8MJ3/LAG4 inuYNdt7cj7zaKx1p/hU46fRwQSpVrrzMdHDIjjGGT88CVhn8gJww/vORrRNExy2LNjV 0tBNrxm5k2eBk+8xDHoYvyJQTR5hqck60cJi+ORYsfLH1dCuGPOwUIDdQotGpbPfwJK2 7kvKDkEtAKbG5XhI1JvSCaKXO0GZqWET/iq8dp1mjSyE6dMzPREdhezDxuzAyzC9gX3T vsXaHPxULFzItWN2rhbjsdnoiQHkRuxTLDXzOQGS5F7Tdi5EJqq+IfGlmt9kBqUL6lmj DKuA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OwYPWZSU; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55035-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55035-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCW4pl95qDBhXomM8yZG8sOoZTKIdS5E5wL6BAzjberGhxH3h9Wlh8ylCN+paSeYVYrQE846QZVJGwkuXqeRCx6HNZ4iFg== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d12-20020a63f24c000000b005cde3cc04c5si1728536pgk.821.2024.02.06.05.55.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:55:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55035-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OwYPWZSU; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55035-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55035-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 58E3EB26E96 for ; Tue, 6 Feb 2024 13:42:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 72C50132C13; Tue, 6 Feb 2024 13:40:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="OwYPWZSU" 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 1B77E132479 for ; Tue, 6 Feb 2024 13:40:44 +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=1707226846; cv=none; b=R7gx+dBLAwYtTNuMeBowuNbWT8aNJhVJM3t9OudtvB9Bn7/tMZ0yKGZtJ16MNM4HhTWqWVoYYmEFOJ6rKFeEjJe//8dUnWxO15qiWh84oBYJmgwKNKDarSkX831MHI3THNrgtHY5glOo/SZ/nW0z70lZQZ5hkUaNHVlqNUA9eeU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707226846; c=relaxed/simple; bh=bnxwDEgv9QFQUVRxuPEvyxebBStu2y9kJ562+f/7/NU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e+Q4qjoD7EJq46pre3GBbM2JjGUN/3B41dqkscU0PITZgaA68CgrvyJd8agGmINWhFYrIchZgqM4HFwhLWZuT4JS5/l8Q/Y56L0V9a1u8VG1BfHebIWkh2/MMGLdBDVCdXb/FMhcKeaXhWkm8CsX+yo06/DtRd5DfKq4csph3Dc= 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=OwYPWZSU; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707226843; 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=d3QB4wa/ZeBRooj+eB6dP6QJm1U/XuptG2DuaZLsRqk=; b=OwYPWZSUv0MviBvLgG9o8MgfVncAgMlB+2ftTSiKjYuEzSF+PiH1BuEUuK5QxAKVd3hvsp 4J9uEoCSTmFttHahbg8dAqdO+QCBJfW14KXtRqvJTTmm8/XwACFv3EXefr8QqaX3BTUrXE X6i626R5S0Q7D3PyN1K91tHW9HCvS4Y= Received: from mail-yw1-f197.google.com (mail-yw1-f197.google.com [209.85.128.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-227-DINitj3zNYqjDscG6I3y-g-1; Tue, 06 Feb 2024 08:40:42 -0500 X-MC-Unique: DINitj3zNYqjDscG6I3y-g-1 Received: by mail-yw1-f197.google.com with SMTP id 00721157ae682-5ff64f5a960so11236447b3.0 for ; Tue, 06 Feb 2024 05:40:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707226841; x=1707831641; 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=d3QB4wa/ZeBRooj+eB6dP6QJm1U/XuptG2DuaZLsRqk=; b=BihMQL1CtUosuSjOYbdFnMlttd/kgxBcaRFJXQnRmSJGnaxZKQrnSkIgyr7UoKxsnp iPEqQz+33HaCbLwOaqhcwFuaHyHscLce5hFEUlh0bqE6ghUWpsUz2uxXI/q58ZaYe2A4 Cw3blikhi1f1nyqEtsWZpI7wnFfbPM17FUekIX7+icmAJ3Hav9z6gESJGa8lRW0Y1KR1 Y5ZT6hWlI9gs1unFcTlrP8TThzQt3afukfgmThTVVMDTztalxkvSXIh25gmK1jrCDHV7 UG62RXlKVMFs9OKlI3T42i53fEbLXRhOdAmkQbBRzJBy6D/5MRi/8z9aS/D3EEkRm+Og Uv+g== X-Forwarded-Encrypted: i=1; AJvYcCWuH2xZX5Yv49q4aA6dtYRLBcZw5z62uWMfZdfX+OLKh59uCcNPlMowd/F0zzRSBOVuEldh7vz7W/4CJ0KdpcuMAiXJdJvWa1g+v0DZ X-Gm-Message-State: AOJu0Yyz4qHqYhD9gXJnHS+dBO3dVvCOrgIh8Z6zDM5x7c0lggc3Ul/o mwcqFPQecpXezN9a4iI8VgSCOveWR7cdFLdoFC5PPgDw3OkZLqZlsg3VUk/hKJH2ojrwZFIO9Ec k4xYWcC2BPQXEY80aFM4cARa//bKZMp5jUBD4Ok2djN2NeOCzm2b1sEQZqNQc7Q== X-Received: by 2002:a81:c542:0:b0:604:7ab0:dcc7 with SMTP id o2-20020a81c542000000b006047ab0dcc7mr1129899ywj.1.1707226841025; Tue, 06 Feb 2024 05:40:41 -0800 (PST) X-Received: by 2002:a81:c542:0:b0:604:7ab0:dcc7 with SMTP id o2-20020a81c542000000b006047ab0dcc7mr1129872ywj.1.1707226840648; Tue, 06 Feb 2024 05:40:40 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCW4ChpH2LRg+my/aRS16W7LPb6AplmZo0EHMKniRUO0w+shgnOak1Q/IIGUrMQ8BLGe6fQuD5eR+frJ8KO2WcF+zxKo6oDfnwJapOv6j0mvLg6sZ9sVR4eUpwvpT/XPwucq4Bb/f/3XifcysR651YtpBhzZfIs0cUZ/pxFR7ktvEx73h0G1d6IBz1HbZ29T578T+GxMGw+fVvVnk+gsy3nqC0OfBtYSizlx5XHrYkOKUkeQ0xhQ2c2vIVAMhbwzfxNiqE3xyEmrzn+LXEu4GjPQMeYugVSrvjKxdrY/vQ9JoROMecA6h18RiX1MEV68WWWzIXMpoD1B2IfISEQLX0thVTyDJRZWLMpxZnXcKWMIOBFNhbRYyUYFe5+MbCfdLq3BJW+A Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id vu4-20020a05620a560400b0078544c8be9asm903791qkn.87.2024.02.06.05.40.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:40:40 -0800 (PST) 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 v3 05/10] PCI: Move dev-enabled status bit to struct pci_dev Date: Tue, 6 Feb 2024 14:39:51 +0100 Message-ID: <20240206134000.23561-7-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206134000.23561-2-pstanner@redhat.com> References: <20240206134000.23561-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790157998231583586 X-GMAIL-MSGID: 1790157998231583586 The bit describing whether the PCI device is currently enabled is stored in struct pci_devres. Besides this struct being subject of a cleanup process, struct pci_device is in general the right place to store this information, since it is not devres-specific. Move the 'enabled' boolean bit to struct pci_dev. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 11 ++++------- drivers/pci/pci.c | 17 ++++++++++------- drivers/pci/pci.h | 1 - include/linux/pci.h | 1 + 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index a0e8e47b2de9..3d84f13ec9b7 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -403,7 +403,7 @@ static void pcim_release(struct device *gendev, void *res) if (this->restore_intx) pci_intx(dev, this->orig_intx); - if (this->enabled && !this->pinned) + if (!this->pinned) pci_disable_device(dev); } @@ -448,14 +448,11 @@ int pcim_enable_device(struct pci_dev *pdev) dr = get_pci_dr(pdev); if (unlikely(!dr)) return -ENOMEM; - if (dr->enabled) - return 0; rc = pci_enable_device(pdev); - if (!rc) { + if (!rc) pdev->is_managed = 1; - dr->enabled = 1; - } + return rc; } EXPORT_SYMBOL(pcim_enable_device); @@ -473,7 +470,7 @@ void pcim_pin_device(struct pci_dev *pdev) struct pci_devres *dr; dr = find_pci_dr(pdev); - WARN_ON(!dr || !dr->enabled); + WARN_ON(!dr || !pdev->enabled); if (dr) dr->pinned = 1; } diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index c919f85a269d..a6b5140fc6f6 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2013,6 +2013,9 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) u16 cmd; u8 pin; + if (dev->enabled) + return 0; + err = pci_set_power_state(dev, PCI_D0); if (err < 0 && err != -EIO) return err; @@ -2027,7 +2030,7 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) pci_fixup_device(pci_fixup_enable, dev); if (dev->msi_enabled || dev->msix_enabled) - return 0; + goto success_out; pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin); if (pin) { @@ -2037,6 +2040,8 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) cmd & ~PCI_COMMAND_INTX_DISABLE); } +success_out: + dev->enabled = true; return 0; } @@ -2206,6 +2211,9 @@ static void do_pci_disable_device(struct pci_dev *dev) { u16 pci_command; + if (!dev->enabled) + return; + pci_read_config_word(dev, PCI_COMMAND, &pci_command); if (pci_command & PCI_COMMAND_MASTER) { pci_command &= ~PCI_COMMAND_MASTER; @@ -2213,6 +2221,7 @@ static void do_pci_disable_device(struct pci_dev *dev) } pcibios_disable_device(dev); + dev->enabled = false; } /** @@ -2240,12 +2249,6 @@ void pci_disable_enabled_device(struct pci_dev *dev) */ void pci_disable_device(struct pci_dev *dev) { - struct pci_devres *dr; - - dr = find_pci_dr(dev); - if (dr) - dr->enabled = 0; - dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0, "disabling already-disabled device"); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index f5944e3cb7fb..9038d7c822ae 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -811,7 +811,6 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev) * when a device is enabled using managed PCI device enable interface. */ struct pci_devres { - unsigned int enabled:1; unsigned int pinned:1; unsigned int orig_intx:1; unsigned int restore_intx:1; diff --git a/include/linux/pci.h b/include/linux/pci.h index 83a683a1f4e5..f3ed3ee6b9c4 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -367,6 +367,7 @@ struct pci_dev { this is D0-D3, D0 being fully functional, and D3 being off. */ u8 pm_cap; /* PM capability offset */ + unsigned int enabled:1; /* Whether this dev is enabled */ unsigned int imm_ready:1; /* Supports Immediate Readiness */ unsigned int pme_support:5; /* Bitmask of states from which PME# can be generated */ From patchwork Tue Feb 6 13:39:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 197449 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1544125dyb; Tue, 6 Feb 2024 05:42:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IHImemVrmz8mnIECc77UO4tlg5n2vx/OuyxPrfhBcI+YrxfYDn4Et5Wu1CTLbWbpL/i63bf X-Received: by 2002:a9d:66c6:0:b0:6e2:7a71:4cc0 with SMTP id t6-20020a9d66c6000000b006e27a714cc0mr2844820otm.21.1707226957423; Tue, 06 Feb 2024 05:42:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707226957; cv=pass; d=google.com; s=arc-20160816; b=n64sNHT7QxHzwGEUmG7YNfUwnL2JyX5PcJki1wLVZTTuLGhVnAuy4d1ZTT7Hy3AblL ITohEnyLDZ9d4V84pChNqp/08OQqFWvkQWq1xsblfsb5scKI3rWLF9jUJnPnh2+mD6CN iDBbfvI/tNVxm2iTXxlWpGRZQeeMqmu4fQTvB6srAbDkrEcPGOaGYAx/3rGpPPtQbuCR 8co+FLhk0L4dkf6Uo2DoX2T9xXalOjPCH1dL5ltJwiBnhTzAKlsu+7e8Bi+1lInYJ1pA LfVAtwucEIrdRMMadiqH6tCmojDGsrzuKt5rpQX+bJlqDwS8b0RH33zigc7FZFdqjjFe N90w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=r9Jq7nnvSUuT+9XIHZBABjxVtcpsKSdKyI5Paa1Y+pE=; fh=Zf8MjSSwQYrVDpBqhFA/26f3MuKrBshC7wQMbAlZgF0=; b=01vmSJqOgY2QNBffMyvOnXuEfJRUjU2BuKVAIdmMn/v7/Ob9W72vQiD0z2rKiFVnED HJMlZpQp7ZpY4/bZhjp0F1qnXG8/CSCFDOBdNRbjKe1hJ3cWB2qVrggaJpYu+0thVtMg 1Msox5cUs3F2jqE2fl0MOhcdx35JDz6elldjwnTDAIwciGXJhgUOAGwvDk6uLSAJ25ul N2WISmbftTVyUOoi0IPyw+qqaSTn3vZ+4dR0KZAaAmfDlMnBYY9yF5ZBQS+DF8bLlV/E GQUMBGEho1xxqByZ+fBQAKxzGGGkBdgBNIGjXe28z7fcpqbpQalm/9y/cQetO8h6k4b4 n1hQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BwVgL1Hm; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55036-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55036-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCWaS6PXq8GWjlHwbDR7nAfR/bUpxpDcKEaMCrtJmg3c26Far+3Em/08PmxAxmpQLoHSfus5kCxThSUf5m+FKf8pBOYRhA== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id q20-20020a05622a04d400b0042c2ddbc837si2329490qtx.84.2024.02.06.05.42.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:42:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55036-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BwVgL1Hm; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55036-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55036-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 231DC1C2451F for ; Tue, 6 Feb 2024 13:42:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 64629132C1C; Tue, 6 Feb 2024 13:40:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="BwVgL1Hm" 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 6810F132C0F for ; Tue, 6 Feb 2024 13:40:48 +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=1707226850; cv=none; b=Hsx5X6I3ZVwZbiXLPsNlVv7QJ88cZ/Q3kE4ki23WcAa+oxJwjEhlmYZsdzoA+tc5EFRM+wpyNmnKSvoPpnd4skefAU+aR5Ab3PXgKWRSjADVj4WMemV7Wgj5kyxz/ok+axxbL78+SDj6nNI4XQ7GHj6fdcZ3kTKMrKBWAXgK/WY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707226850; c=relaxed/simple; bh=4YgCxdXbltrx/1JUqv7edFbj4RwZgpQUdghTMt8J4VE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AyE7QC2XtRHCzL4SrHBDm5qPxAoad+g3U8Bj5jAenMBavxcgM42ILS2u/5m0feVpj2yTnKe+HrjUFAfd0RaiYyejAKhiJN/C67Q/jViMjDFfaHugnvShCbQegHaPWe2EyH2ngF09xM+GE0Bst10Ib/vush5f9q9YyypqbGY9gTQ= 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=BwVgL1Hm; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707226847; 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=r9Jq7nnvSUuT+9XIHZBABjxVtcpsKSdKyI5Paa1Y+pE=; b=BwVgL1HmZvuXfOZwZv02V0FdwY2l3lpU64NPxg98NL3QIul4okt5ZiXh7Qic/oB1YqXqw9 LiGowr5jVNVwqAgvCM0bKG/6czhqrxBoSfC6H6MxL/t73KqmG56pZJr6+/OGRVyyjFiyry MC1r5/SNOzYA8OQQa/rqMc+mxSOomFM= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-141-2QZILw8ZNv258ivHqpf9Gw-1; Tue, 06 Feb 2024 08:40:46 -0500 X-MC-Unique: 2QZILw8ZNv258ivHqpf9Gw-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-78356ddf3cfso293215585a.1 for ; Tue, 06 Feb 2024 05:40:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707226845; x=1707831645; 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=r9Jq7nnvSUuT+9XIHZBABjxVtcpsKSdKyI5Paa1Y+pE=; b=CXn8gVTXBqVBemZwIwKHGJtZ9iKnUu3bI5l8MsXdbDX9Od97dXmgz0kYvZZkffpftD 2p0szdlSDSJv8+K1z2rG52rzQDcuKmpQPkMROb5a1HN8Cg7PCB9bZxnuVe8c6+Ela5Tr tNa2rstrJ2rNtPyXbSTO9j1wYRNMWN2LUc4BeO+TY9UqNW/ZxAm3HjMav2P6B/EE2ZEh HYSXakA1kTMD7s0iXhW/MEg/xHkOZHRjY0nScFTJ9tsWX/7BuLTbAexDEaRyEsHPjnbF L45FNG5VxzYs4Urza9E7dX0ab4tnYJVHDx1tuyOuN4Dd/kSxBWBSHq8ioav2AtIbO4WY U91g== X-Forwarded-Encrypted: i=1; AJvYcCU9DpG6a8LuyKwg4Vwsd0HdzC4IkiVgAB+EmV/OAQuKJGGWQ5bGYp0IgfG3iqbc7M7/5d3XcDT+sPrOfgi65kHtaJNXqYCWr9k0ed1D X-Gm-Message-State: AOJu0YzCj04iwh+D0MkRhgDiTAao5xtyEjUcisnZotcI06P5ruSqtfUf s1Vtj4C8Tummo6tAGiFWb8mrIjgPOpaLG9kW02JsYMygYTjTx+2Cp3NXt0sIruw8KXquGiA8aQi pO8WU7arbrXBtHoyoAGxku5LKMSSlNb1hweBY1EBy+3sjOQAmEp6G4bHHqn5M2RO0rfOB2A== X-Received: by 2002:a05:620a:460f:b0:785:8aa0:a317 with SMTP id br15-20020a05620a460f00b007858aa0a317mr2924508qkb.0.1707226845428; Tue, 06 Feb 2024 05:40:45 -0800 (PST) X-Received: by 2002:a05:620a:460f:b0:785:8aa0:a317 with SMTP id br15-20020a05620a460f00b007858aa0a317mr2924486qkb.0.1707226845127; Tue, 06 Feb 2024 05:40:45 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXN2QOuE+kU+UnrJ98Xqwnwk3jc22gdIjYBQVhbuoIxlt0r+kYzNbjiRtQA+Pf4Kq0oojmXSPzQFcepw46xsipquBkmG+gucNTra5EPE0xDkg6wEKhZac8+AJuJBBYi1IscHr724HN37Fy7kx4s+kheM102+jo8Y52kbocRFE6iQIeHfHcBQ5GAPFT3gHguujXDesRI24vTPC1DoVsyRCObqct21+YK9jhmGW/YaQDmRCp87zAzY36O4OAOl5YHAnbdEJN97bQwySadese6yBP74jPL0IeKBhHQa7ODzyXBlIbMgPS74tqO7r4ujuY0GquxM9pyL2dXfBoFN+3tDHFuAP1sEL12WA7GoJBj/AoFXEJiHed/fJduPr6GjtaeIHy4xziV Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id vu4-20020a05620a560400b0078544c8be9asm903791qkn.87.2024.02.06.05.40.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:40:43 -0800 (PST) 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 v3 06/10] PCI: Move pinned status bit to struct pci_dev Date: Tue, 6 Feb 2024 14:39:52 +0100 Message-ID: <20240206134000.23561-8-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206134000.23561-2-pstanner@redhat.com> References: <20240206134000.23561-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790157214497353322 X-GMAIL-MSGID: 1790157214497353322 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, because it allows for checking that device's pinned-status directly through 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 | 5 +++-- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 3d84f13ec9b7..628cde665e77 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -403,7 +403,7 @@ static void pcim_release(struct device *gendev, void *res) if (this->restore_intx) pci_intx(dev, this->orig_intx); - if (!this->pinned) + if (!dev->pinned) pci_disable_device(dev); } @@ -461,18 +461,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 = find_pci_dr(pdev); - WARN_ON(!dr || !pdev->enabled); - if (dr) - dr->pinned = 1; + pdev->pinned = true; } EXPORT_SYMBOL(pcim_pin_device); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 9038d7c822ae..586b2047c275 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -811,7 +811,6 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev) * when a device is enabled using managed PCI device enable interface. */ 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 f3ed3ee6b9c4..3d8e75a8b3f6 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -367,11 +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 enabled:1; /* Whether this dev is enabled */ - 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 */ From patchwork Tue Feb 6 13:39:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 197450 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1544306dyb; Tue, 6 Feb 2024 05:42:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IFLjSCpEEVjmmt1kZ6ZPFJWCK2PsFBDOBK2uoLRrY88SoNKeUNKopTHzUd3r0DwihWwnBbs X-Received: by 2002:a05:620a:1583:b0:785:9282:426d with SMTP id d3-20020a05620a158300b007859282426dmr1010175qkk.41.1707226975489; Tue, 06 Feb 2024 05:42:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707226975; cv=pass; d=google.com; s=arc-20160816; b=XPCGSf1AXzHD2T+slEgheFtJDIn+vf1iz9UUm4bunFhbIzFYFi6yecjoeht5PuiaaA mggJIv5JORgDGTtTvDFGK1TnjDKagKfesyAYB3/yVI84AZcFK1cIBkCkCxd3lR87Eln8 gTtuDHsr2qvDTMdVA7whRNxxPPqLQGNfzcj+vphWYT2/eClYz6sCjNiCCfNsBvg1p20Z qLXdmesm24Eoxh3rZai0o3b+6orygNuQoxTHU9irAyTRHZSKCAyBXycHwalhTMbmF7mB HdOmUQAt6t9f9wQZwJZzMQ4bNYDoePoC03ItnrF5BhJbaNBhDAAkvCiji7DcUDwURSJv lxrg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=7AZZPeDLSIQhGpyqdXYj5Wako5b4bPLUCKUFeVUAK8s=; fh=mbVvRrkHTGxSFktzrb6n0/C8JvGaMmmRyhvL+E6lpRg=; b=D60xAKDXf93+0LMaM5ChQIzER1RFM5y2rDUjn2RZAetrQT8DaVq680mtrApXCDpnkm OcW5evxs4hzZjkgD9ggR+I43CHlbh7FOHb0y43JPl4vrk4phpG9dEgnUwnnWTJW9TuWm TFUOvJF4p2WXDPctlEx2toMwG+gTJI+P2mIgOAvTztRZKKXfuSQzuXAMVqq36mo6cw7I txtBp/myxszAfDxPFwBIWkPpTLzZLlvkT2TITBI5rwPUDQrYJ2yS7CdTQEMhwQbhmOr4 rLDZ+FWSSQIu1+ib52LYrdlltMsfOdI2oDQLHcVGVKQGQabLqKu0e5s3OzObWrn4rVQh jXIg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XFd525EK; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55037-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55037-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCVOZvnQ1+wbLvXeqgODDkqt0IxM5aX/Lih9CO3hupJHekdYgvQPxdzZfN3DJadlJUort3vY6o4LHjMR9gse7lK+Aai10Q== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 17-20020a05620a04d100b007855d88743dsi2176768qks.520.2024.02.06.05.42.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:42:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55037-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XFd525EK; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55037-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55037-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 3C7BF1C23DAD for ; Tue, 6 Feb 2024 13:42:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D89441332A5; Tue, 6 Feb 2024 13:40:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XFd525EK" 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 30EF5132C3E for ; Tue, 6 Feb 2024 13:40:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707226854; cv=none; b=aCvUCRWIEs9wEBC33SPuF/0gFa8ss/mlLmy94UAWesQKTyRFr2zEEqxyQCU4Pyn/pjxtxXStQy3KrL3kNaThmZ8d1dmTD971SmCPIqRDL2tH6DzASWzGiC/ZEDOunsDVWCbGwnIAv9ntu/n4U/oUPxL8g+KpOAJMGbqkQeUiS94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707226854; c=relaxed/simple; bh=TowMgeLUGRs1hnMQjk19y8O1hjjTxf8YbaaZY8r8KoQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RPMjxRo0+wdvSBQesmOvPiZVbCgbTrk35DFqadeDrNVAuW772y0+tddM3Um5IgLCPezDfOm/XN9XRayW1RIqnOlEQZ7YkPn3in+EseehgJyUIREdxYMJii+ztw2xn80muxe7BJngNAhAMG8hA4QUsTQmBvLN2D5dc++5mWlrfJU= 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=XFd525EK; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707226852; 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=7AZZPeDLSIQhGpyqdXYj5Wako5b4bPLUCKUFeVUAK8s=; b=XFd525EKNU941PY4TR7tWf0fqCh5xH/+RSJCoUnI6btaiRGKfaforjM7am0AYUqYBHuHNT 2kPIi5Y8hal9KZhvCtuQ24PXaIxgSZfiG/HiG6BEezN4oHpmZP3vK+U4ApeO9RSe7WJXqY mTzmckK2v1D0nn0J4k0KnmXQi/0KPPY= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-48-cvgGnh0fONqZ-uptnKCWIw-1; Tue, 06 Feb 2024 08:40:51 -0500 X-MC-Unique: cvgGnh0fONqZ-uptnKCWIw-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-7853449295cso25887085a.0 for ; Tue, 06 Feb 2024 05:40:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707226850; x=1707831650; 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=7AZZPeDLSIQhGpyqdXYj5Wako5b4bPLUCKUFeVUAK8s=; b=AKBKcCobXVN6RUGUzwQrtVikA/sDtaCA1innv5U0H2VjWQlqy/iMjrJdOg/hbi7tna OIKBIa51cRNlvkhXylIshauU34qlHNNv3BUT5vlSg2cOQJwro95R32f4gVR58vG9BwTF 5qezUnZ32VOXyeUzxKMjhfAFvvJmvoZZC5yS5k8QZPf8gp3088EbpLAyQwLbECdAZluK Gdni2ha/KwK1PlylPe1cuekdxz4EnXm5mYqA8zabhbxNRqionXrSYqGJCdxpRNg9Wuh6 1dZ4Fp/KGRmKQ/p44i7Dz7dOM3zkbmx/PEEHHD9ljXWnP1rHHPSXHciTEw248mlwTEJR peaA== X-Gm-Message-State: AOJu0YxpjAVTY8fGT8SbK9JwLrSXiMNJsWJArMR5JC9oxKumX74yRBTR FRX1olDk1fWvgOITpTBqbZM2e+Ls1uik+9e+ubhCR2vGybNl4AoueC+KVuhfpEWr+fHaVJ3cZO5 SuXksmme7OMpiLvd402TBXokG3iiA3KjSbXJRMx8+SgO6HKonmACp/oL9sFB+V2C5nuYq5w== X-Received: by 2002:a05:620a:4714:b0:785:65bc:6cc1 with SMTP id bs20-20020a05620a471400b0078565bc6cc1mr2808221qkb.5.1707226850503; Tue, 06 Feb 2024 05:40:50 -0800 (PST) X-Received: by 2002:a05:620a:4714:b0:785:65bc:6cc1 with SMTP id bs20-20020a05620a471400b0078565bc6cc1mr2808193qkb.5.1707226850162; Tue, 06 Feb 2024 05:40:50 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWxKz2AS2kCg26W1YFQSCC6cN1TQFyl00vI8SJusbemv44LJDN294ZNx81ZJTllVzo6lrDYzd9cB9u2oUv/09MqwrPZMkh+cmCIjsyWL/V5LIkRYx150ArqnQekZaEu9Gw4ZKQmIPFG9mFjy2eFjaoHxoZpj9Wqas5mrNDPaAXdH9g0P2JNNqXBvb7aD1Df5LmUywY3BqqckFtKGmHWfGl8JxXokTkpbxqEo2LGQmOidzl6+nkC5S7gyDkomYtorhMcYCQVaRM8RTMnAqh8VFdCPAeK8I/tAPufT5yUwq22SxbR/clnAu17itZMQbSGnNxW4FjnviRCGUWOqd6zxcI/CtVYyCazSInsFGpRzGpoO9yDr58I+rPaTOhNNcGAS2YJojI3 Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id vu4-20020a05620a560400b0078544c8be9asm903791qkn.87.2024.02.06.05.40.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:40:48 -0800 (PST) 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 v3 07/10] PCI: Give pcim_set_mwi() its own devres callback Date: Tue, 6 Feb 2024 14:39:53 +0100 Message-ID: <20240206134000.23561-9-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206134000.23561-2-pstanner@redhat.com> References: <20240206134000.23561-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790157232865504488 X-GMAIL-MSGID: 1790157232865504488 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 628cde665e77..89d2d6341b19 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -370,24 +370,34 @@ void __iomem *devm_pci_remap_cfg_resource(struct device *dev, } EXPORT_SYMBOL(devm_pci_remap_cfg_resource); +static void __pcim_clear_mwi(void *pdev_raw) +{ + struct pci_dev *pdev = 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 success. */ -int pcim_set_mwi(struct pci_dev *dev) +int pcim_set_mwi(struct pci_dev *pdev) { - struct pci_devres *dr; + int ret; - dr = find_pci_dr(dev); - if (!dr) - return -ENOMEM; + ret = devm_add_action(&pdev->dev, __pcim_clear_mwi, pdev); + if (ret != 0) + return ret; + + ret = pci_set_mwi(pdev); + if (ret != 0) + devm_remove_action(&pdev->dev, __pcim_clear_mwi, pdev); - dr->mwi = 1; - return pci_set_mwi(dev); + return ret; } EXPORT_SYMBOL(pcim_set_mwi); @@ -397,9 +407,6 @@ static void pcim_release(struct device *gendev, void *res) struct pci_dev *dev = to_pci_dev(gendev); struct pci_devres *this = res; - if (this->mwi) - pci_clear_mwi(dev); - if (this->restore_intx) pci_intx(dev, this->orig_intx); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 586b2047c275..eaec3b207908 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -813,7 +813,6 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev) struct pci_devres { unsigned int orig_intx:1; unsigned int restore_intx:1; - unsigned int mwi:1; }; struct pci_devres *find_pci_dr(struct pci_dev *pdev); From patchwork Tue Feb 6 13:39:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 197451 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1544464dyb; Tue, 6 Feb 2024 05:43:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IH8Jdvc2EZY1B3r1wZGEfy09HfKL008TlgwPbIB82lX0ws7caCryblxu3SH/8MLoRP9ph8K X-Received: by 2002:a05:622a:341:b0:42c:14b7:b603 with SMTP id r1-20020a05622a034100b0042c14b7b603mr3453404qtw.36.1707226993564; Tue, 06 Feb 2024 05:43:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707226993; cv=pass; d=google.com; s=arc-20160816; b=xIxJNGDNbM9HGFj1r4/7J8izhzzX1R7M/VaHTpk3Mu4ygINhmoyGh/WJ3d7q1NmlmG qFmzkHkP5Za9SDgWfR0NoExqZNnldWwIXKC1kQVezFywFudLwUjNl32NAKL0FHBCpOIe snIHVXYYVNwaUvGGNl9PDucEAXEyy1NZ0Ppv300tuF7FTkE2HtNZnqPmS1tvEu8V3Chq +uEMGssBggawKBikzVZWrrlF+7S/LoY/u1a25H7PySe2Q9UibMxs8DHjCkIuAP4QWxNL bD681hVCHhyvc4SGRwX3Z9d006R0gsc3qZ+t11l5aRkR149kWKM4uBlWG1YqMDTyvJi5 vvkA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=1Jh3czCqQDL/0c9zpNumpr/ueC3CRmD2SdHwNhVsws4=; fh=Fr8tgQm1TAST0p2R7/q9JLKNTmgJ1sS83ZueCDaRN1M=; b=YgceaReGw9yR6XVyA2A6iiRERvZJNxu2gOXt6ixWXH52yFppHsxshwZAhfSjQjdJlZ 6/RKIgjclTiIjqFf5fKqvktbIVG0Kkceptb1mwk6zQF+6PWUmwnET3Bz+eGxZMoQLRAx 6lNNO+GiJsBAQ7JCuT4MSTJQahflqDPHunnXM/kANjgKG7DHbKOj8xTHOjCYtKA/0dCZ oh4/QKQfgO8Qf8nAIKpf9nicN93fVUfo55l0dv1fg99uZpmhbkjkuPyI0Yq8zNvRTP87 Ok2NWnlm121LQqq5z/Ecw49rmaz5cPkSSHdbKeLwnX1wWErzolYrEKQyqcsu9RhK4uzO oW7A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eE53OMJe; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55038-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55038-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCXdWFVN/4MYfPF98JXIJALGO+GQJM1OqiGapV8fpPg5yUtDWc45cOjuN1+fKZIte9pADnK+cCy4qokREDw2vYo3eWbfCQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id c11-20020ac87dcb000000b0042c0c3c908fsi2329830qte.192.2024.02.06.05.43.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:43:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55038-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eE53OMJe; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55038-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55038-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4BF281C243F8 for ; Tue, 6 Feb 2024 13:43:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 284961332B1; Tue, 6 Feb 2024 13:41:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eE53OMJe" 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 84152131729 for ; Tue, 6 Feb 2024 13:40:55 +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=1707226858; cv=none; b=DlLjZjHLqxZeC59T5hKun2nX247Q1rrNSEi/uApctrvNrOIQMfrlBLVuTjN4K87H0RokADsuka7wLKR2kK0ZoJ5iYsAfZxxQWOcxvTOQeAwZsAzYbd3V5cmA0z1cr7K/MIJ6tvKb+UlkQBUUvkGckb6xIOOCXMa0ivFYIN5q1tw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707226858; c=relaxed/simple; bh=ZJ83zn6ztPV2uljoE1yZ3KKMxpJLVe/lGJ8ZusJAz8I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kPy255gdoFFMrg4oLlTVFPJe538j++c9UAa/KPeIFm3Ea7uQGVp5YvfHHpWavOFJSWB4M3d+Z1pVzO8dWullTih7bSN1D0s826pVSd/a9c9XzE5l2jurIRDvzYBYpYVAJmPt4cEwUqvoHQMavThdDOevqzB3CJ36PTt5mK7oEJU= 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=eE53OMJe; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707226854; 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=1Jh3czCqQDL/0c9zpNumpr/ueC3CRmD2SdHwNhVsws4=; b=eE53OMJe56dGsttNsUK5jiU0vsFV1KAzuEKC820woQQvTkylVB8LYfNBk2v27qLHf21baH wQ7xcWTPG4qCYAaFfTYuZNKbsy1C1wQoExFfN5MEFZfc/EDxf4aZh01LDgGmLc23h1Wj6v W+r/lHqfvmaLu0W4SAQhM6mC0luWG6c= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-190-ad2G-EpmOF6KOLIOt8zIqw-1; Tue, 06 Feb 2024 08:40:53 -0500 X-MC-Unique: ad2G-EpmOF6KOLIOt8zIqw-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-78543d8dfd6so47977385a.1 for ; Tue, 06 Feb 2024 05:40:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707226853; x=1707831653; 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=1Jh3czCqQDL/0c9zpNumpr/ueC3CRmD2SdHwNhVsws4=; b=lsk+eBzgg7L805QQNf2oQ8oMj7qO87LKLENq5ez4l93AtLcthAE1XhM2ciPVa9xOSM GvaaNJXKMUSYGj9k8GtvqJn91IHSSiHQpiWXZq58Me7Wh5v2con5RhNQPL6DOMrulxR+ HwfDnnTfT7FItB+8RKRR4klpXb5uQEHATdAQDjtr7WiVtlbKdyqziXrFky0c7vrrdST8 5fLe7mKz1nfcVgJqYutCEXdZ4hXnsphflZZ2FXK9k3IQV9cM/6GKjS8Viu6Pmuc6SF7Q qJPQSmoZoUZxuEyhcNOtCqH+pSbkPOpuKkVFdeM+Ht0cyc5aKi180BmfQAqY61OPAXL7 ThKQ== X-Gm-Message-State: AOJu0YxIHODs3poX8KLSmD7hWunQBdS/Z+YA6Gm+v+AJw2cHNDH+6ilW fMbk6tV+BbiV6TZSh+DtFKxysSnM2wxb230338SOJkZvkouR1OKbUUtPAGgHzPEICMr0uDgsVOp xglTBLAeTZwg6Gu6xTHOiVplSERj1tBck1lxS7bLElKDa8E8bNmUkhtOxY+gG8Q== X-Received: by 2002:a05:620a:2994:b0:785:9219:4a43 with SMTP id r20-20020a05620a299400b0078592194a43mr1160655qkp.0.1707226852819; Tue, 06 Feb 2024 05:40:52 -0800 (PST) X-Received: by 2002:a05:620a:2994:b0:785:9219:4a43 with SMTP id r20-20020a05620a299400b0078592194a43mr1160636qkp.0.1707226852504; Tue, 06 Feb 2024 05:40:52 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCW58JBDBApNpqJsWEGCS9AfiEEgz4FYsMiw9RHIh8YS/o9y6CqyWUT3O0cCIrUkSvFFUTQgZw1aVSiGh8XUhat01vzCYbg01AKfA10KZLd50xCK/Fq4/uHWExw1Icq69Uz1Rsj2+FVDflE2P0HSGB48AvdOkNI6pK9/nCjj6u3bqGTWn+O8KjXMg6GtsC+r/bGA4RU922mfLClydAUH4va1s68jaLPWHonU3ih72UOFT/68qUdEvY/20hduKvq8QlHmB61tb1uOkQgC2wkYn1MJ8c2J+J7+Hd2LDBVbK9Sbi4J5O+hDLOcPnCNfZgjqh5rdatXyTd9D46UCB1K9Q1DlJBQj057kHVr0cCMSNkjIJETYrWdPzCDxHia1ifDUr/okJMlD Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id vu4-20020a05620a560400b0078544c8be9asm903791qkn.87.2024.02.06.05.40.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:40:52 -0800 (PST) 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 v3 08/10] PCI: Give pci(m)_intx its own devres callback Date: Tue, 6 Feb 2024 14:39:54 +0100 Message-ID: <20240206134000.23561-10-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206134000.23561-2-pstanner@redhat.com> References: <20240206134000.23561-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790157251788455386 X-GMAIL-MSGID: 1790157251788455386 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 other 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 struct pci_devres from pci.h. Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 76 ++++++++++++++++++++++++++++++++++---------- drivers/pci/pci.c | 24 +++++++------- drivers/pci/pci.h | 21 +++--------- 3 files changed, 77 insertions(+), 44 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 89d2d6341b19..8a643f15140a 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -40,6 +40,11 @@ struct pcim_iomap_devres { void __iomem *table[PCI_STD_NUM_BARS]; }; +/* 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, @@ -401,31 +406,70 @@ int pcim_set_mwi(struct pci_dev *pdev) } EXPORT_SYMBOL(pcim_set_mwi); +static void pcim_intx_restore(struct device *dev, void *data) +{ + struct pci_dev *pdev = to_pci_dev(dev); + struct pcim_intx_devres *res = data; -static void pcim_release(struct device *gendev, void *res) + pci_intx(pdev, res->orig_intx); +} + +static struct pcim_intx_devres *get_or_create_intx_devres(struct device *dev) { - struct pci_dev *dev = to_pci_dev(gendev); - struct pci_devres *this = res; + struct pcim_intx_devres *res; - if (this->restore_intx) - pci_intx(dev, this->orig_intx); + res = devres_find(dev, pcim_intx_restore, NULL, NULL); + if (res) + return res; - if (!dev->pinned) - pci_disable_device(dev); + res = devres_alloc(pcim_intx_restore, sizeof(*res), GFP_KERNEL); + if (res) + devres_add(dev, res); + + return res; } -/* - * TODO: - * Once the last four callers in pci.c are ported, this function here 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 = get_or_create_intx_devres(&pdev->dev); + if (!res) + return -ENOMEM; + + res->orig_intx = !enable; + + pci_read_config_word(pdev, PCI_COMMAND, &pci_command); + + if (enable) + new = pci_command & ~PCI_COMMAND_INTX_DISABLE; + else + new = pci_command | PCI_COMMAND_INTX_DISABLE; + + if (new != 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 = to_pci_dev(gendev); + + if (!dev->pinned) + pci_disable_device(dev); } -EXPORT_SYMBOL(find_pci_dr); static struct pci_devres *get_pci_dr(struct pci_dev *pdev) { diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index a6b5140fc6f6..fe8cbf9be0fc 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3939,7 +3939,7 @@ EXPORT_SYMBOL_GPL(pci_common_swizzle); void pci_release_region(struct pci_dev *pdev, int bar) { /* - * This is done for backwards compatibility, because the old pci-devres + * 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(). */ @@ -4527,11 +4527,22 @@ void pci_disable_parity(struct pci_dev *dev) * This is a "hybrid" function: Its normally unmanaged, but becomes managed * when pcim_enable_device() has been called in advance. * This hybrid feature is DEPRECATED! + * Use pcim_intx() if you need a managed version. */ void pci_intx(struct pci_dev *pdev, int enable) { u16 pci_command, new; + /* + * 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) != 0); + return; + } + pci_read_config_word(pdev, PCI_COMMAND, &pci_command); if (enable) @@ -4539,17 +4550,8 @@ void pci_intx(struct pci_dev *pdev, int enable) else new = pci_command | PCI_COMMAND_INTX_DISABLE; - if (new != pci_command) { - struct pci_devres *dr; - + if (new != pci_command) pci_write_config_word(pdev, PCI_COMMAND, new); - - dr = find_pci_dr(pdev); - if (dr && !dr->restore_intx) { - dr->restore_intx = 1; - dr->orig_intx = !enable; - } - } } EXPORT_SYMBOL_GPL(pci_intx); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index eaec3b207908..ee8d38bf3f9e 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -798,24 +798,11 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev) #endif /* - * TODO: - * The following two components wouldn't need to be here if they weren't used at - * four last places in pci.c - * Port or move these functions to devres.c and then remove the - * pci_devres-components from this header file here. + * This is a helper that shall only ever be called by pci_intx(). It helps + * cleaning up the PCI devres API in which pci_intx() became a managed function + * under certain conditions. */ -/* - * 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. - */ -struct pci_devres { - unsigned int orig_intx:1; - unsigned int restore_intx:1; -}; - -struct pci_devres *find_pci_dr(struct pci_dev *pdev); +int pcim_intx(struct pci_dev *dev, int enable); /* * Config Address for PCI Configuration Mechanism #1 From patchwork Tue Feb 6 13:39:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 197453 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1544784dyb; Tue, 6 Feb 2024 05:43:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IEp21yHfQSR0jBQGvgGD+fGgpVxinEcVs/QrMBPxcEJIpEda2rJC46OPDYPAbLeCvkx7hIc X-Received: by 2002:a17:906:694:b0:a36:e435:5259 with SMTP id u20-20020a170906069400b00a36e4355259mr1664077ejb.36.1707227030746; Tue, 06 Feb 2024 05:43:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707227030; cv=pass; d=google.com; s=arc-20160816; b=Cb8v5Pm09IKPa76L0vh54+whLCYKX2SE6ZCG8cSSTaCY0iFYhNTjgrVCLVRlaC0f52 25geIKtbZXa/cVedlTTQLuRyYDAaosKhcMoIWu2EC/4/uC1aC7WBMUy01GlJjLt5wFS4 Pw+Zaa+p60MfF/Z3wmW4u2az4OOGawZ3DoJIEU1YMUiYbyaUKmUVaJXoE89bSYGZjok/ 4y8Sa6TUBusngy39N3OntWtryzJ2e+QZG3hzJlbCLcF5J6QHN4/9/wJWiECP3lWUxB7t 1gm81uRRQJHmVO1Nw/m2SKuoE63clsl/vg2FnkaTGn6pSo+/KU6Cc2F3utf8j7fcxdEb zIXA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=nDrBrW1QyBkIZW9XXC7FxnbgE53QwXCFFfi0uuWv0Ls=; fh=p+VU2vK8EewOGwSCk8M6zmunQFanw260bq4NaD2H4dc=; b=fgV9b5vM8bwjO8fWnfKVw0XBPx/qVOa8hqTjnPw16/YMhNn2gCWgOmh1yIViiVi+8X zJoJaKeozVfN6Ypl6uPuP8Q4SvyOd98Fq+FoOciFsxylf2kOY1Hu5A1mZlgwp8eyOqey 3WmZKV+pm4sd8i22RO0ee+nleGHtwrZJOuiNn/AtjsuPx+5vR5VPvEOhhNr8lO/2Pmnm lLGjEri+UbW7giwirIPEMvmvV4Oz2QuB2VksNRYWBaC7XPNqxycz4q+hJElhvdGOnrFc u41+POcZCMCFGfAa1M9PLawQpUa6IuBwBMBv3naF4z/KXT3U9NHiqXRgUb4YwOVXN1F+ chPw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JaCwSqEH; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55040-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55040-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCWx7hceRTXp4tpjvTSZic1blqmLufzU7/6WZi7nNZ/ujBr4plT8Tlwd0pLLKAezb7xH6/DIW8MTRgnkKqQstA2uSloTfg== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g21-20020a1709067c5500b00a3788e943aesi1130244ejp.114.2024.02.06.05.43.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:43:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55040-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JaCwSqEH; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55040-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55040-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 32CB41F2310B for ; Tue, 6 Feb 2024 13:43:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C179E13343E; Tue, 6 Feb 2024 13:41:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JaCwSqEH" 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 4BB0A133401 for ; Tue, 6 Feb 2024 13:41:02 +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=1707226864; cv=none; b=jVk70hqqN0KIU5ILZgrmzR3MEunbfMUxbIjjymCrjkw3klM69np1LifIb5UOqNL9gsDefe8WWWaQLAyAQ9qzyO8dfmIThDOmf8WE74cBJz2F0V+vmmABMzd++wYQcN4QEP1jpwuYL64jybq2x1Cp8co6O+LF0MegDLM+LPRZAMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707226864; c=relaxed/simple; bh=TeuRzMDENTP+mrbhGHtQfvvCYg0RN9gxYq3N6G/Xkt4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qMgEDVyhSX1cvDfYZlyFBP2vMIFBh/hrIZAeB/LAai1XKPAhO6keyZO/3g6HI1rG5xzqXN6pWeyizyxMVsNQ+i9SESgCwOXs/vv4AHcgxRW9MCJEm/2HFCgVlQfo1EVCGKoGOopicwM9F4qRbhwgd+MuO94jhH6xrYMwO4Fy87I= 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=JaCwSqEH; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707226862; 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=nDrBrW1QyBkIZW9XXC7FxnbgE53QwXCFFfi0uuWv0Ls=; b=JaCwSqEHUGKTx68twpIU6PrIKE/Ae8iZn+Ayh7Z4ALyjG7qfNPJoyQlh2Fl8yiu8vcTY8K q4h0r1dP39fPrcad9Eo0/kbjRq6M5cy59HUAGYxqQh4QCrOYawxtmEWZ/lFGE2RzcA9k43 PMK0UZy5QQHFrFSFPfa84Bc0hcKYOxk= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-422-QzTy7-gnPsiLvNh33mBz9w-1; Tue, 06 Feb 2024 08:40:56 -0500 X-MC-Unique: QzTy7-gnPsiLvNh33mBz9w-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-78130939196so253319685a.1 for ; Tue, 06 Feb 2024 05:40:56 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707226856; x=1707831656; 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=nDrBrW1QyBkIZW9XXC7FxnbgE53QwXCFFfi0uuWv0Ls=; b=BV4a1Jqo5VVlKUTaXFe8L93/tgKEeL4OB/tPDZG+mFOfj52s+hbPEtUSWle99+8abR h1jhJjrVi4n7SqEkgYBmzPuqxgrJjwHfmWa7rWY17Qdhek7U62nQDEdyZFfh7TkUDKXm ilvxC+VoG7TnWnQ73vSeZHS8QTdTS30NoFAgs/WcCj8d07C/aQpU2Tlach1ZrX3xQZ5s bgjcf0KfGSm2OI68l+kok2aNYr3DGlcTwEIyTUemZs6lOaYWv3Uc3RtPQ5yANxYWBvvv jv3QN06bmnHl0jCdDfXXUfbOud7EBPcwis3Yr2rEiYheqesSp2tjUzE+Fr1wTeOsND+s 6cKg== X-Gm-Message-State: AOJu0YwsMk2T13Qh955QTREsaBC5GBtxPFp5ckfbZQ3MvSyKOAe150/3 T5ADHqDYs4XLC+IcvmkvBM5Whs9el2I+GJm6m0bI2TdmeI0mZqrT/ilvRYQgaC94B5Fo1RyG2Q1 KDOZK1mzDKPQ8Oxj1BLuDzfR0wUtMf+G3ltZRc8YyL7aXraQUU/DXEXtGg/I4gw== X-Received: by 2002:a05:620a:4722:b0:785:4588:7a09 with SMTP id bs34-20020a05620a472200b0078545887a09mr2762772qkb.3.1707226856086; Tue, 06 Feb 2024 05:40:56 -0800 (PST) X-Received: by 2002:a05:620a:4722:b0:785:4588:7a09 with SMTP id bs34-20020a05620a472200b0078545887a09mr2762751qkb.3.1707226855798; Tue, 06 Feb 2024 05:40:55 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXY73u7g+juzT4qNhBv3adaHj4fe+28SURqOK6AMlIWi7SNnZKGhEb2SSunmcFOciqesjrdctJWePLgX61nFQtGT5DaHz6Drpqx3G76kNq/mJa5K3DoJrAWl6f6g4DFCZF0ILo9JChZ4/clq2pplqhGRQMKv+BDBiNhFo86TN0jVsQ0QNTSA6pXfzo/lV39SDF7eXdr0HLEdQW8DLwItLzDNvPVqbSGxXWzQmvii1m9FK8AvubZrpLmdghe10es0xS+rE+5D09CNNgsmtT/eZjDpF1Pbzdgh0a3oxdS5CUX+UBfXjFLQWbZ8iPUzFmXBwQhDDVav0ymM1W7urvifsSe2K0KvynAiVS3TjOJqMuTG/VdoSvxMAAzsrZG9Gk6tkYfCuMm Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id vu4-20020a05620a560400b0078544c8be9asm903791qkn.87.2024.02.06.05.40.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:40:54 -0800 (PST) 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 v3 09/10] PCI: Remove legacy pcim_release() Date: Tue, 6 Feb 2024 14:39:55 +0100 Message-ID: <20240206134000.23561-11-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206134000.23561-2-pstanner@redhat.com> References: <20240206134000.23561-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790157291192833783 X-GMAIL-MSGID: 1790157291192833783 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 devres API. Replace pcim_release() with pcim_disable_device(). Remove the now surplus function get_pci_dr(). Signed-off-by: Philipp Stanner --- drivers/pci/devres.c | 49 +++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c index 8a643f15140a..3e567773c556 100644 --- a/drivers/pci/devres.c +++ b/drivers/pci/devres.c @@ -463,48 +463,41 @@ int pcim_intx(struct pci_dev *pdev, int enable) return 0; } -static void pcim_release(struct device *gendev, void *res) +static void pcim_disable_device(void *pdev_raw) { - struct pci_dev *dev = to_pci_dev(gendev); - - if (!dev->pinned) - pci_disable_device(dev); -} - -static struct pci_devres *get_pci_dr(struct pci_dev *pdev) -{ - struct pci_devres *dr, *new_dr; - - dr = devres_find(&pdev->dev, pcim_release, NULL, NULL); - if (dr) - return dr; + struct pci_dev *pdev = pdev_raw; - new_dr = 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); } /** * 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; - dr = get_pci_dr(pdev); - if (unlikely(!dr)) - return -ENOMEM; + ret = devm_add_action(&pdev->dev, pcim_disable_device, pdev); + if (ret != 0) + return ret; - rc = pci_enable_device(pdev); - if (!rc) - pdev->is_managed = 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 = pci_enable_device(pdev); + if (ret != 0) + devm_remove_action(&pdev->dev, pcim_disable_device, pdev); - return rc; + return ret; } EXPORT_SYMBOL(pcim_enable_device); From patchwork Tue Feb 6 13:39:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 197452 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1544675dyb; Tue, 6 Feb 2024 05:43:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IHE7w9G71JFHVmRgtGg2jcUerKyB0KZr8DlIlKUDWr6e2fxPzvN1qMfRRGWdwoCW/4NN/LZ X-Received: by 2002:a81:f907:0:b0:5ff:7ba4:8897 with SMTP id x7-20020a81f907000000b005ff7ba48897mr1559169ywm.38.1707227019371; Tue, 06 Feb 2024 05:43:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707227019; cv=pass; d=google.com; s=arc-20160816; b=OpwKZ6rqgoZlS2+TE6q2P2slLSYScd4g7L5bzaYUtEIqkkiPtD1P1Pev0m8QuDxdZE H2rnmaWfHrVtbxyI2t4Upfl/dtbd9b53o2Pdh4ZnwwCjgp042or9JUDfYw7zicBzDNud 7l26u6koG9s7kzyxoL12NxAkX6kv/UFMCiXFWIjrO/+KzNsZyUbmJ3Wno6n7ajrJMu70 qAkPx7beD8lISdrv3pK2zcEUhpC+kaA5AdY/KHVX7Ep1cGgCnpgBwptZUn1/+gwz1iMY NDiKlE5MrzYYdiBCKwijQLqJFD9rQQwVn9zcV12K23AB59TepcRi3RepNa17q/3nAmX3 yYig== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=wFTRxYvZ76dhFqAdPDH/a2zkO7llSf+/xEzdCoge4iY=; fh=F46QCGTuN1zhOjbTM9n6S4JxeRr+ZOIIPIvZvPH21zM=; b=OrZ61ncp2UQMFH1wXs4n8BPnoTmME71jDfkqQYtgcEvU8drqnokbMNI74h2Y+Wh49j iHKdhqYx4DGR/J74DfXPC1TEzTY5Xw78mvSQdpQUTJnAvLia+jS/kvLL+Ggj3QdQDeNL 0Bn3CrCb3oQ2ujDikeALmsZdcv4dlFVbZ25En3r5VKCgTmJm5MiNbTJ1p1eJFSZyBWP/ CPjHjuyXPZhtoJOxcymZeBM4qPnz3xYlDokr9YJcmaARDHuetIOj68LDKDahsLhCYywY 7rOf8OWs/OhRveNbVor40P9YDGMMV7VlS6XqLiOJ+flkLTvr3wqz4nVxgxpUfShNANDl tLag==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MQd0HvdN; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55039-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55039-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCXOXyGr6ETqtqpI4j34ejdjXvOlLGCnrQxcRHq3bzJUoVdl9B4atGV+DyX7objpkK0MODPN3P9QWwOyYZkcFNpb1h8xJQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id c15-20020ac85a8f000000b0042c00d7cc50si2408126qtc.52.2024.02.06.05.43.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:43:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55039-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MQd0HvdN; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-55039-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55039-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 1EE111C23EF3 for ; Tue, 6 Feb 2024 13:43:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2AFB0133430; Tue, 6 Feb 2024 13:41:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MQd0HvdN" 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 8C4AD1332BD for ; Tue, 6 Feb 2024 13:41:01 +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=1707226863; cv=none; b=kaEnnh/GbXwf2psgt54cTVOqTtYRA3xmpvXNIA/ACIo5MQ4yK3dlnVI5Pj5DNa3GQG+CuRTUcYlXk/2rrzCcDDT6zAW9keZrnxXsZl77ikG5x/vhsl2Ts9yQ+iKG7Rczfj1bJYjjJM0CcE2mgo+7sl3kQ4cCOZ72TXguZvBggpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707226863; c=relaxed/simple; bh=DqC1146Gw+gtEXd1U6evjshsYPG+7+z9wKN5tAwt5no=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=htyTi0wr2OES2WKPunx0H1CxW33x45Yf+ok93mkM3VK/luoB/Yinz/iaWRgdD+PYad+Qnebqq0C7eM9QpBtgD00iE3W2djizCQaAFJUNOa1MYuLxzjAxuOhvn78LqC1ja505B4XO3J6Z6EfgAb5Pu+zlupoomw/T6jiPA+4tiBI= 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=MQd0HvdN; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707226860; 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=wFTRxYvZ76dhFqAdPDH/a2zkO7llSf+/xEzdCoge4iY=; b=MQd0HvdNzAzjr6Wwhz5+1MFypwBgPEOci7c6lpO1CFFwKfrUXMP9XBMGlV76cr+5l0aaB9 qwh/DVOe1WtdOCK0MBdbcMTs3SXYgvZws+kfYcF2CU0yfx/dmp7mFnCoMdleH7gaCurlPJ ioQZPcxPu2+WskD066OlJmry4axj/y0= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-472-VBqYc4A8MManfjrazG0Y7w-1; Tue, 06 Feb 2024 08:40:59 -0500 X-MC-Unique: VBqYc4A8MManfjrazG0Y7w-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-78543d8dfd6so47980685a.1 for ; Tue, 06 Feb 2024 05:40:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707226859; x=1707831659; 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=wFTRxYvZ76dhFqAdPDH/a2zkO7llSf+/xEzdCoge4iY=; b=wa5q1PrV8pCZL0V7/ZzD0n+s1rTz8hUAhlck3RSRChBrwX7+CwMWxC22Ai6muA2ZJk Xll4FAH8mnVii1uAUAbp5FUeXbcjd92SUcVYRk8+j400aqDxWwcEJ0OLBaxJJwR5wfei 3FPFsdxm9s7gMUN2R6+cK1qrs+8gXZoW/HSzoW5Kj9ND7eX8EsdeblRgMLJqPejip2AI 7beoWI2ZRZHJ81fALJVFQo/x+fBkdvznFiRHmFah4hurJ+7tr/AUOoHQ6dg5tRdHFavl 4yjSHH4z+4O1sDT67uDf6jraiVvD5ZnYrgrKwtrDTMe6RWB+h3tiOC8fn/1CTOfaFytV DcxA== X-Gm-Message-State: AOJu0YwEtzwCDtggPdhpacocUwXGrGDkUrrYghkqoqq03llITF9XmWQM PJcIiaXXplzRyCUKfCLP3KJqGOFv7yFr1G8KJPfp8gI5Oy7xXUmrndXG7/DgXaiJOkLf7ovzFLg g0IFdppF4ltgIr+pJSWHUgb24a+gJ16jF46cWTFWQj2J59KiRvsREKUhA7NoBqg== X-Received: by 2002:a05:620a:458f:b0:785:8aed:219c with SMTP id bp15-20020a05620a458f00b007858aed219cmr2740652qkb.4.1707226858855; Tue, 06 Feb 2024 05:40:58 -0800 (PST) X-Received: by 2002:a05:620a:458f:b0:785:8aed:219c with SMTP id bp15-20020a05620a458f00b007858aed219cmr2740622qkb.4.1707226858478; Tue, 06 Feb 2024 05:40:58 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVVeDR5b9c7g/LX4DSP+QZjXTjR4mD9i/Uh3Ezjtj2GW3JtLorKY5AblA+CjtJql2aLlNALxzV0bl4U41u/GhhZLatfz9z0bPdBgozM5EYqgeElngR5qR0Txp3/2qm26N1Ys4+/8hSBu6u0aVRfjpHdO1etZE2hVCPT0KXoSUUzd617dh59feMZZeAjbZhdv8EnSQMoiuo89rEq+BvdPIr9sAN3QLOVlkylgY3mNTLjiVFjIdPDcpIFzQC9jWFF/j85Y4fRB2DTkj/Y3sTRuTE4bDbGsJR7R5v8GBdTiU3fm3/tDc7ma55apc9cEjsouFx/iBcEiQmJasZt4DHn09OqYIwb+Wn3YbkFIprccJT/mkYLQbiwHpsE+JR6Ql4TqRJ4ZFLdqlLKi8ArBJYgqbXk+HpyMNjYOPlQc7SM Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id vu4-20020a05620a560400b0078544c8be9asm903791qkn.87.2024.02.06.05.40.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 05:40:57 -0800 (PST) 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 , stable@kernel.vger.org Subject: [PATCH v3 10/10] drm/vboxvideo: fix mapping leaks Date: Tue, 6 Feb 2024 14:39:56 +0100 Message-ID: <20240206134000.23561-12-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240206134000.23561-2-pstanner@redhat.com> References: <20240206134000.23561-2-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790157279149159577 X-GMAIL-MSGID: 1790157279149159577 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(). CC: # v5.10+ Fixes: 8558de401b5f ("drm/vboxvideo: use managed pci functions") Signed-off-by: Philipp Stanner --- 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/vboxvideo/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 -= vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE; - vbox->vbva_buffers = 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 = 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); for (i = 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); /* Map guest-heap at end of vram */ - vbox->guest_heap = - pci_iomap_range(pdev, 0, GUEST_HEAP_OFFSET(vbox), - GUEST_HEAP_SIZE); - if (!vbox->guest_heap) - return -ENOMEM; + vbox->guest_heap = pcim_iomap_range(pdev, 0, + GUEST_HEAP_OFFSET(vbox), GUEST_HEAP_SIZE); + if (IS_ERR(vbox->guest_heap)) + return PTR_ERR(vbox->guest_heap); /* Create guest-heap mem-pool use 2^4 = 16 byte chunks */ vbox->guest_pool = devm_gen_pool_create(vbox->ddev.dev, 4, -1,