From patchwork Tue Apr 11 12:29:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Pieralisi X-Patchwork-Id: 9675119 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CF56060382 for ; Tue, 11 Apr 2017 12:31:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C249F28384 for ; Tue, 11 Apr 2017 12:31:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B77E82852D; Tue, 11 Apr 2017 12:31:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3491B2852B for ; Tue, 11 Apr 2017 12:31:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=akqQGZpGETHFwCm+q9IowU1pMX/702RFFDJYJMmJ9Dc=; b=ItbQSJUgYzRZaW/pstEY8W19so yEhbjVCo5OJITg4N0A/WY4YbJTJkU2eRbDh7uhCuQcL2+dTDok5SFwBNSTgogq30JdnFTJDD2Gl62 8jdSBVtfqVGEa0KvA2B+uqXntERZD9h2vmi9yPmytQ1PGNp4z/DKn4NGqfFUJpqjADWFNfHe+IhOX 0UZXb1jM8EpR9rM3ooPXhmptiWfpw6z0n796H1nlN0JKhD+7+KmmsG1Qc7S2VotfS7Lnmmc++Ds6q ctqyQSXijSOMGcdMAJaeAiuTC6NFwGwVK9As5EKxRB4si5jQCLlNOxPnVK8Fn3vlASI9eVb3bgfAz OGqeqFAQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cxuxO-0002Cz-Td; Tue, 11 Apr 2017 12:31:38 +0000 Received: from foss.arm.com ([217.140.101.70]) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cxuwG-0006ds-RR for linux-arm-kernel@lists.infradead.org; Tue, 11 Apr 2017 12:30:42 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C804B1688; Tue, 11 Apr 2017 05:30:28 -0700 (PDT) Received: from red-moon.cambridge.arm.com (red-moon.cambridge.arm.com [10.1.206.55]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7CDA93F23B; Tue, 11 Apr 2017 05:30:27 -0700 (PDT) From: Lorenzo Pieralisi To: linux-pci@vger.kernel.org Subject: [PATCH v3 32/32] lib: implement Devres ioremap_nopost() interface Date: Tue, 11 Apr 2017 13:29:12 +0100 Message-Id: <20170411122923.6285-33-lorenzo.pieralisi@arm.com> X-Mailer: git-send-email 2.10.0 In-Reply-To: <20170411122923.6285-1-lorenzo.pieralisi@arm.com> References: <20170411122923.6285-1-lorenzo.pieralisi@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170411_053029_140450_1AD38388 X-CRM114-Status: GOOD ( 12.65 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, Lorenzo Pieralisi , Jonathan Corbet , linux-kernel@vger.kernel.org, Tejun Heo , Bjorn Helgaas , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP The introduction of the ioremap_nopost() interface allows kernel drivers to map memory through a dedicated kernel interface providing non-posted writes semantics. Introduce two new functions in the Devres kernel layer and Devres documentation: - devm_ioremap_nopost() - devm_ioremap_nopost_resource() so that drivers can make use of devm_* interface to map memory regions requiring non-posted writes memory attributes. Signed-off-by: Lorenzo Pieralisi Cc: Jonathan Corbet Cc: Bjorn Helgaas Cc: Tejun Heo --- Documentation/driver-model/devres.txt | 3 ++ include/linux/device.h | 2 + include/linux/io.h | 2 + lib/devres.c | 78 +++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+) diff --git a/Documentation/driver-model/devres.txt b/Documentation/driver-model/devres.txt index bf34d5b..9991a66 100644 --- a/Documentation/driver-model/devres.txt +++ b/Documentation/driver-model/devres.txt @@ -294,7 +294,10 @@ IOMAP devm_ioremap() devm_ioremap_nocache() devm_ioremap_wc() + devm_ioremap_nopost() devm_ioremap_resource() : checks resource, requests memory region, ioremaps + devm_ioremap_nopost_resource() : do devm_ioremap_resource() with nopost + memory attributes devm_iounmap() pcim_iomap() pcim_iomap_regions() : do request_region() and iomap() on multiple BARs diff --git a/include/linux/device.h b/include/linux/device.h index 9ef518a..1dce865 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -689,6 +689,8 @@ extern unsigned long devm_get_free_pages(struct device *dev, extern void devm_free_pages(struct device *dev, unsigned long addr); void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res); +void __iomem *devm_ioremap_nopost_resource(struct device *dev, + struct resource *res); /* allows to add/remove a custom action to devres stack */ int devm_add_action(struct device *dev, void (*action)(void *), void *data); diff --git a/include/linux/io.h b/include/linux/io.h index 82ef36e..e34d799 100644 --- a/include/linux/io.h +++ b/include/linux/io.h @@ -79,6 +79,8 @@ void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset, resource_size_t size); void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset, resource_size_t size); +void __iomem *devm_ioremap_nopost(struct device *dev, resource_size_t offset, + resource_size_t size); void devm_iounmap(struct device *dev, void __iomem *addr); int check_signature(const volatile void __iomem *io_addr, const unsigned char *signature, int length); diff --git a/lib/devres.c b/lib/devres.c index 78eca71..d16bd76 100644 --- a/lib/devres.c +++ b/lib/devres.c @@ -100,6 +100,34 @@ void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset, EXPORT_SYMBOL(devm_ioremap_wc); /** + * devm_ioremap_nopost - Managed ioremap_nopost() + * @dev: Generic device to remap IO address for + * @offset: Resource address to map + * @size: Size of map + * + * Managed ioremap_nopost(). Map is automatically unmapped on driver detach. + */ +void __iomem *devm_ioremap_nopost(struct device *dev, resource_size_t offset, + resource_size_t size) +{ + void __iomem **ptr, *addr; + + ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return NULL; + + addr = ioremap_nopost(offset, size); + if (addr) { + *ptr = addr; + devres_add(dev, ptr); + } else + devres_free(ptr); + + return addr; +} +EXPORT_SYMBOL(devm_ioremap_nopost); + +/** * devm_iounmap - Managed iounmap() * @dev: Generic device to unmap for * @addr: Address to unmap @@ -163,6 +191,56 @@ void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res) } EXPORT_SYMBOL(devm_ioremap_resource); +/** + * devm_ioremap_nopost_resource() - devm_ioremap_resource() nopost version + * @dev: generic device to handle the resource for + * @res: resource to be handled + * + * Checks that a resource is a valid memory region, requests the memory + * region and ioremaps it with ioremap_nopost() interface. + * 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: + * + * res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + * base = devm_ioremap_nopost_resource(&pdev->dev, res); + * if (IS_ERR(base)) + * return PTR_ERR(base); + */ +void __iomem *devm_ioremap_nopost_resource(struct device *dev, + struct resource *res) +{ + resource_size_t size; + const char *name; + void __iomem *dest_ptr; + + BUG_ON(!dev); + + if (!res || resource_type(res) != IORESOURCE_MEM) { + dev_err(dev, "invalid resource\n"); + return IOMEM_ERR_PTR(-EINVAL); + } + + size = resource_size(res); + name = res->name ?: dev_name(dev); + + if (!devm_request_mem_region(dev, res->start, size, name)) { + dev_err(dev, "can't request region for resource %pR\n", res); + return IOMEM_ERR_PTR(-EBUSY); + } + + dest_ptr = devm_ioremap_nopost(dev, res->start, size); + if (!dest_ptr) { + dev_err(dev, "ioremap failed for resource %pR\n", res); + devm_release_mem_region(dev, res->start, size); + dest_ptr = IOMEM_ERR_PTR(-ENOMEM); + } + + return dest_ptr; +} +EXPORT_SYMBOL(devm_ioremap_nopost_resource); + #ifdef CONFIG_HAS_IOPORT_MAP /* * Generic iomap devres