Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp1507993rdd; Thu, 11 Jan 2024 00:58:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IEQ7T49L0Gvg27tBxKL2beluWSb6Fso3oKeFl+dIp64WrlBOPjNT7w0J0ns7gSyoNSYkuhC X-Received: by 2002:a17:90a:42:b0:28d:eef4:92e2 with SMTP id 2-20020a17090a004200b0028deef492e2mr70367pjb.54.1704963536707; Thu, 11 Jan 2024 00:58:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704963536; cv=none; d=google.com; s=arc-20160816; b=iqwGfyVTUc/XVpDEdrfQ4Ahxv2NXWEqS74tmdm5155EC05Czbvp4Z4Ym1lhuoi+dRo fVJybHT9uL2Jb3Cg9lvmKuimYx4IdGJIHCjouckHVSzdQYeqNtwKhyR22J/W8B5j5sP1 TO3QC12Vu6sIvwmSZFEyKGovqc9O7gc5Xtevb8On4XnwhPq6ygF+UJYBkbzko8nuji1n pVPq5uyh4SOKpcJeNjKa85PU2pvReMlg3et5vfBlkjmP5uOvLk4GNSNaiUYDSrRmdAV9 V/6mz1tPon2C9ofHfbTpAcjEvCewb0t5ewdJYpdnqADTUeSDP33Jhbyg8vZyZv9Y+U7S O3dQ== ARC-Message-Signature: i=1; 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=oTGzUnuImaRUCmV4376iFTUXoCEDIuJDS3P/LYSpU0A=; fh=pvthNihVFviQ+zW2qM9/pSdOLuZcwnsNgc+5FSX/1K0=; b=G+d/39K3UBSRjOha6rsk3cnJ+CV/cs3Vnz72i4amUAUdjnc2PT3GHONkApEa8yEgjr uJ+eeu1/U3y5JLY5GIENUbQ1syGdGE64DuLKL1DNSExvyyf1VvhFf1bW/wNfvKJvQt8A +f6MfDtYwKh4+3fWSHp8u0aY/RAsfYFCc16a9VGv2q5RVtZ+tbS7SNe/pBRW4s0hgxlj AoiCeS0EfsVjsO+kWp5oMk0axBpLSb2B/V+MAB/72UM7YsKSl+uJ8fJz7pqoiqqwZk0h Aj0zuqzwnz2o5VhfdrdlFwQubgLAojP8GnuEEd9/l0cWkbNzipnfowBzXiQZ/d56HX9i /uog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=djD0pAZh; spf=pass (google.com: domain of linux-kernel+bounces-23262-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23262-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id c20-20020a17090a8d1400b0028c26709e8bsi3147584pjo.10.2024.01.11.00.58.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 00:58:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23262-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=djD0pAZh; spf=pass (google.com: domain of linux-kernel+bounces-23262-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23262-linux.lists.archive=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 516B82860CA for ; Thu, 11 Jan 2024 08:58:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8D4D8156F9; Thu, 11 Jan 2024 08:56:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="djD0pAZh" 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 C422D10798 for ; Thu, 11 Jan 2024 08:56:26 +0000 (UTC) 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=1704963385; 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=oTGzUnuImaRUCmV4376iFTUXoCEDIuJDS3P/LYSpU0A=; b=djD0pAZheu7KQ9RYnxMWmnq/7iVF2AIzBjUgsUl7FhnisLgIBsLAYjIdzPw4CHy+3mVXDx RWWhya2op2s9k+4PHlF+h5Ym0PJSYtEG4SKipSFh2pdnrdHmoYameukTU54bDT4zxuhowI Q/5w99hKBsmv7ELaGSa7cmTjRjz70F0= 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-190-pIA0ON_OM7-30_nAmLwqIg-1; Thu, 11 Jan 2024 03:56:21 -0500 X-MC-Unique: pIA0ON_OM7-30_nAmLwqIg-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-78130939196so123166785a.1 for ; Thu, 11 Jan 2024 00:56:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704963381; x=1705568181; 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=oTGzUnuImaRUCmV4376iFTUXoCEDIuJDS3P/LYSpU0A=; b=t3zGierPZqjqPuvUJx9lJTZJo5ND4ay9Itq9Ho5JiAdMfTWZ/hTREYdXlBWqdDTFJU 6cHHUBRnnvUIZ3xNifUuxND/nVINcBrUTMWRubs3gU66PvB4m3+sf78+btcc6fvTar5W wxGs+WQ1sBi7vUYOQe2sWI0vfl1yWEG0vJMeUTxQrEka/iFquPfSeBG20OeVwcwmgGIu ggma8gH8SqyKfm2gOR2p60S6SykNmJdy8hM/B1F3JOq2dyBd/1mLHZk2hb2HLfrrssFK RgQ2WJwk4UjbA3+hmYb8rYExYC8eYZi0J03rJeCOOlRzQgCNg3bEe4qeIN2oYdq/nSrl z9lg== X-Gm-Message-State: AOJu0YyO/BL9BxSr4Y6GUVkRwXw0ZNpYC3wTdsn+WY72ujrtbUY3Lm9M O9yBRE72nhcGknG9vxekQgIvwOvYOgcGCwoatUUmiwlURt3Ob1VZcUtFe5tfyes7BEh37LpiG3W c4YkM1GeHJH0BYSKQ550RvHInqf1LLwvU X-Received: by 2002:a05:6214:500f:b0:681:926:891e with SMTP id jo15-20020a056214500f00b006810926891emr1565506qvb.3.1704963380830; Thu, 11 Jan 2024 00:56:20 -0800 (PST) X-Received: by 2002:a05:6214:500f:b0:681:926:891e with SMTP id jo15-20020a056214500f00b006810926891emr1565494qvb.3.1704963380562; Thu, 11 Jan 2024 00:56:20 -0800 (PST) Received: from pstanner-thinkpadt14sgen1.remote.csb (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id e16-20020a0cd650000000b0067f7d131256sm168341qvj.17.2024.01.11.00.56.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 00:56:20 -0800 (PST) From: Philipp Stanner To: Bjorn Helgaas , Arnd Bergmann , Johannes Berg , Randy Dunlap , NeilBrown , John Sanpe , Kent Overstreet , Niklas Schnelle , Philipp Stanner , Dave Jiang , Uladzislau Koshchanka , "Masami Hiramatsu (Google)" , David Gow , Kees Cook , Rae Moar , Geert Uytterhoeven , "wuqiang.matt" , Yury Norov , Jason Baron , Thomas Gleixner , Marco Elver , Andrew Morton , Ben Dooks , dakr@redhat.com Cc: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, linux-arch@vger.kernel.org, stable@vger.kernel.org, Arnd Bergmann Subject: [PATCH v5 RESEND 5/5] lib, pci: unify generic pci_iounmap() Date: Thu, 11 Jan 2024 09:55:40 +0100 Message-ID: <20240111085540.7740-6-pstanner@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240111085540.7740-1-pstanner@redhat.com> References: <20240111085540.7740-1-pstanner@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The implementation of pci_iounmap() is currently scattered over two files, drivers/pci/iomap.c and lib/iomap.c. Additionally, architectures can define their own version. To have only one version, it's necessary to create a helper function, iomem_is_ioport(), that tells pci_iounmap() whether the passed address points to an ioport or normal memory. iomem_is_ioport() can be provided through two different ways: 1. The architecture itself provides it. As of today, the version coming from lib/iomap.c de facto is the x86-specific version and comes into play when CONFIG_GENERIC_IOMAP is selected. This rather confusing naming is an artifact left by the removal of IA64. 2. As a default version in include/asm-generic/io.h for those architectures that don't use CONFIG_GENERIC_IOMAP, but also don't provide their own version of iomem_is_ioport(). Once all architectures that support ports provide iomem_is_ioport(), the arch-specific definitions for pci_iounmap() can be removed and the archs can use the generic implementation, instead. Create a unified version of pci_iounmap() in drivers/pci/iomap.c. Provide the function iomem_is_ioport() in include/asm-generic/io.h (generic) and lib/iomap.c ("pseudo-generic" for x86). Remove the CONFIG_GENERIC_IOMAP guard around ARCH_WANTS_GENERIC_PCI_IOUNMAP so that configs that set CONFIG_GENERIC_PCI_IOMAP without CONFIG_GENERIC_IOMAP still get the function. Add TODOs for follow-up work on the "generic is not generic but x86-specific"-Problem. Suggested-by: Arnd Bergmann Signed-off-by: Philipp Stanner Reviewed-by: Arnd Bergmann --- drivers/pci/iomap.c | 46 +++++++++++++------------------------ include/asm-generic/io.h | 27 ++++++++++++++++++++-- include/asm-generic/iomap.h | 21 +++++++++++++++++ lib/iomap.c | 28 ++++++++++++++++------ 4 files changed, 83 insertions(+), 39 deletions(-) diff --git a/drivers/pci/iomap.c b/drivers/pci/iomap.c index 91285fcff1ba..b7faf22ec8f5 100644 --- a/drivers/pci/iomap.c +++ b/drivers/pci/iomap.c @@ -135,44 +135,30 @@ void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen) EXPORT_SYMBOL_GPL(pci_iomap_wc); /* - * pci_iounmap() somewhat illogically comes from lib/iomap.c for the - * CONFIG_GENERIC_IOMAP case, because that's the code that knows about - * the different IOMAP ranges. + * This check is still necessary due to legacy reasons. * - * But if the architecture does not use the generic iomap code, and if - * it has _not_ defined it's own private pci_iounmap function, we define - * it here. - * - * NOTE! This default implementation assumes that if the architecture - * support ioport mapping (HAS_IOPORT_MAP), the ioport mapping will - * be fixed to the range [ PCI_IOBASE, PCI_IOBASE+IO_SPACE_LIMIT [, - * and does not need unmapping with 'ioport_unmap()'. - * - * If you have different rules for your architecture, you need to - * implement your own pci_iounmap() that knows the rules for where - * and how IO vs MEM get mapped. - * - * This code is odd, and the ARCH_HAS/ARCH_WANTS #define logic comes - * from legacy header file behavior. In particular, - * it would seem to make sense to do the iounmap(p) for the non-IO-space - * case here regardless, but that's not what the old header file code - * did. Probably incorrectly, but this is meant to be bug-for-bug - * compatible. + * TODO: Have all architectures that provide their own pci_iounmap() provide + * iomem_is_ioport() instead. Remove this #if afterwards. */ #if defined(ARCH_WANTS_GENERIC_PCI_IOUNMAP) -void pci_iounmap(struct pci_dev *dev, void __iomem *p) +/** + * pci_iounmap - Unmapp a mapping + * @dev: PCI device the mapping belongs to + * @addr: start address of the mapping + * + * Unmapp a PIO or MMIO mapping. + */ +void pci_iounmap(struct pci_dev *dev, void __iomem *addr) { -#ifdef ARCH_HAS_GENERIC_IOPORT_MAP - uintptr_t start = (uintptr_t) PCI_IOBASE; - uintptr_t addr = (uintptr_t) p; - - if (addr >= start && addr < start + IO_SPACE_LIMIT) { - ioport_unmap(p); +#ifdef CONFIG_HAS_IOPORT_MAP + if (iomem_is_ioport(addr)) { + ioport_unmap(addr); return; } #endif - iounmap(p); + + iounmap(addr); } EXPORT_SYMBOL(pci_iounmap); diff --git a/include/asm-generic/io.h b/include/asm-generic/io.h index bac63e874c7b..58c7bf4080da 100644 --- a/include/asm-generic/io.h +++ b/include/asm-generic/io.h @@ -1129,11 +1129,34 @@ extern void ioport_unmap(void __iomem *p); #endif /* CONFIG_GENERIC_IOMAP */ #endif /* CONFIG_HAS_IOPORT_MAP */ -#ifndef CONFIG_GENERIC_IOMAP +/* + * TODO: + * remove this once all architectures replaced their pci_iounmap() with + * a custom implementation of iomem_is_ioport(). + */ #ifndef pci_iounmap +#define pci_iounmap pci_iounmap #define ARCH_WANTS_GENERIC_PCI_IOUNMAP +#endif /* pci_iounmap */ + +/* + * This function is a helper only needed for the generic pci_iounmap(). + * It's provided here if the architecture does not provide its own version. + */ +#ifndef iomem_is_ioport +#define iomem_is_ioport iomem_is_ioport +static inline bool iomem_is_ioport(void __iomem *addr_raw) +{ +#ifdef CONFIG_HAS_IOPORT + uintptr_t start = (uintptr_t)PCI_IOBASE; + uintptr_t addr = (uintptr_t)addr_raw; + + if (addr >= start && addr < start + IO_SPACE_LIMIT) + return true; #endif -#endif + return false; +} +#endif /* iomem_is_ioport */ #ifndef xlate_dev_mem_ptr #define xlate_dev_mem_ptr xlate_dev_mem_ptr diff --git a/include/asm-generic/iomap.h b/include/asm-generic/iomap.h index 196087a8126e..2cdc6988a102 100644 --- a/include/asm-generic/iomap.h +++ b/include/asm-generic/iomap.h @@ -110,6 +110,27 @@ static inline void __iomem *ioremap_np(phys_addr_t offset, size_t size) } #endif +/* + * If CONFIG_GENERIC_IOMAP is selected and the architecture does NOT provide its + * own version, ARCH_WANTS_GENERIC_IOMEM_IS_IOPORT makes sure that the generic + * version from asm-generic/io.h is NOT used and instead the second "generic" + * version from lib/iomap.c is used. + * + * There are currently two generic versions because of a difficult cleanup + * process. Namely, the version in lib/iomap.c once was really generic when IA64 + * still existed. Today, it's only really used by x86. + * + * TODO: Move the version from lib/iomap.c to x86 specific code. Then, remove + * this ARCH_WANTS_GENERIC_IOMEM_IS_IOPORT-mechanism. + */ +#ifdef CONFIG_GENERIC_IOMAP +#ifndef iomem_is_ioport +#define iomem_is_ioport iomem_is_ioport +bool iomem_is_ioport(void __iomem *addr); +#define ARCH_WANTS_GENERIC_IOMEM_IS_IOPORT +#endif /* iomem_is_ioport */ +#endif /* CONFIG_GENERIC_IOMAP */ + #include #endif diff --git a/lib/iomap.c b/lib/iomap.c index 4f8b31baa575..eb9a879ebf42 100644 --- a/lib/iomap.c +++ b/lib/iomap.c @@ -418,12 +418,26 @@ EXPORT_SYMBOL(ioport_map); EXPORT_SYMBOL(ioport_unmap); #endif /* CONFIG_HAS_IOPORT_MAP */ -#ifdef CONFIG_PCI -/* Hide the details if this is a MMIO or PIO address space and just do what - * you expect in the correct way. */ -void pci_iounmap(struct pci_dev *dev, void __iomem * addr) +/* + * If CONFIG_GENERIC_IOMAP is selected and the architecture does NOT provide its + * own version, ARCH_WANTS_GENERIC_IOMEM_IS_IOPORT makes sure that the generic + * version from asm-generic/io.h is NOT used and instead the second "generic" + * version from this file here is used. + * + * There are currently two generic versions because of a difficult cleanup + * process. Namely, the version in lib/iomap.c once was really generic when IA64 + * still existed. Today, it's only really used by x86. + * + * TODO: Move this function to x86-specific code. + */ +#if defined(ARCH_WANTS_GENERIC_IOMEM_IS_IOPORT) +bool iomem_is_ioport(void __iomem *addr) { - IO_COND(addr, /* nothing */, iounmap(addr)); + unsigned long port = (unsigned long __force)addr; + + if (port > PIO_OFFSET && port < PIO_RESERVED) + return true; + + return false; } -EXPORT_SYMBOL(pci_iounmap); -#endif /* CONFIG_PCI */ +#endif /* ARCH_WANTS_GENERIC_IOMEM_IS_IOPORT */ -- 2.43.0