From patchwork Thu Sep 18 01:30:17 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liviu Dudau X-Patchwork-Id: 37578 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-we0-f199.google.com (mail-we0-f199.google.com [74.125.82.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 3160F2054D for ; Thu, 18 Sep 2014 01:34:29 +0000 (UTC) Received: by mail-we0-f199.google.com with SMTP id q58sf83668wes.6 for ; Wed, 17 Sep 2014 18:34:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:subject:date:message-id :in-reply-to:references:cc:precedence:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version:sender :errors-to:x-original-sender:x-original-authentication-results :mailing-list:content-type:content-transfer-encoding; bh=W9YTsDcs+OvLv9x+1TSaX5M38vROMExXfo5uh2fE6Ck=; b=S8I4387Qc+zSBRykW6/G+9sO+CHUxa3kKogZVh+J9EIBD8120VO8VTt9Zf4qFk7TZb 5nk4MfTajSlWXlfwTmLj6ahrxIUsAQdeoGHQwAZL3nkLXjWDvpIpMILLzB8QWZYtHGkn AciDZH6guzAeDj1NWOb8RWp+xV8zGWFAFQRwxyz6fFM+fKLz3FUwejLY11tMtjCywRuS YytUdumvQPYGjYYulmCmLh/iDkIfjv3et4rpQ7wmL5KAk/h7RuZSDNRJulY0o56YtPoW nc2tPg0aNgyr8C605o65g7kbe4B68Qo22wSngQlFnmAOTZaKlMCLH0Jf0259Gf/C/eY7 E+Gw== X-Gm-Message-State: ALoCoQlnYG1Sdv2YJzLKDPjhOOdd4Q2Hg0hn0mOU6h7MedwR5qkWKAf+kAeKffVA280b1UcccpEU X-Received: by 10.180.105.3 with SMTP id gi3mr8308710wib.3.1411004068414; Wed, 17 Sep 2014 18:34:28 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.3.132 with SMTP id c4ls158546lac.88.gmail; Wed, 17 Sep 2014 18:34:28 -0700 (PDT) X-Received: by 10.112.148.170 with SMTP id tt10mr820892lbb.61.1411004068262; Wed, 17 Sep 2014 18:34:28 -0700 (PDT) Received: from mail-la0-f44.google.com (mail-la0-f44.google.com [209.85.215.44]) by mx.google.com with ESMTPS id ln8si30580825lac.122.2014.09.17.18.34.28 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 17 Sep 2014 18:34:28 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.44 as permitted sender) client-ip=209.85.215.44; Received: by mail-la0-f44.google.com with SMTP id q1so207771lam.17 for ; Wed, 17 Sep 2014 18:34:28 -0700 (PDT) X-Received: by 10.152.42.136 with SMTP id o8mr1173142lal.71.1411004068189; Wed, 17 Sep 2014 18:34:28 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.130.169 with SMTP id of9csp679758lbb; Wed, 17 Sep 2014 18:34:27 -0700 (PDT) X-Received: by 10.70.1.111 with SMTP id 15mr1471298pdl.28.1411004066569; Wed, 17 Sep 2014 18:34:26 -0700 (PDT) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id ri8si268980pbc.70.2014.09.17.18.34.25 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 17 Sep 2014 18:34:26 -0700 (PDT) Received-SPF: none (google.com: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org does not designate permitted sender hosts) client-ip=2001:1868:205::9; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XUQaw-0003B1-FC; Thu, 18 Sep 2014 01:33:14 +0000 Received: from fw-tnat.cambridge.arm.com ([217.140.96.21] helo=cam-smtp0.cambridge.arm.com) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XUQZW-0001io-Jl for linux-arm-kernel@lists.infradead.org; Thu, 18 Sep 2014 01:31:48 +0000 Received: from e106497-lin.cambridge.arm.com (e106497-lin.cambridge.arm.com [10.1.195.53]) by cam-smtp0.cambridge.arm.com (8.13.8/8.13.8) with ESMTP id s8I1UPl6003595; Thu, 18 Sep 2014 02:30:28 +0100 From: Liviu Dudau To: Bjorn Helgaas , Arnd Bergmann , Rob Herring , Jason Gunthorpe , Benjamin Herrenschmidt , Catalin Marinas , Will Deacon , Russell King , linux-pci , Linus Walleij Subject: [PATCH v11 02/10] PCI: Introduce helper functions to deal with PCI I/O ranges. Date: Thu, 18 Sep 2014 02:30:17 +0100 Message-Id: <1411003825-21521-3-git-send-email-Liviu.Dudau@arm.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1411003825-21521-1-git-send-email-Liviu.Dudau@arm.com> References: <1411003825-21521-1-git-send-email-Liviu.Dudau@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140917_183147_031502_D3C55154 X-CRM114-Status: GOOD ( 19.33 ) X-Spam-Score: -3.0 (---) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-3.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/, medium trust [217.140.96.21 listed in list.dnswl.org] -0.7 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -0.0 SPF_PASS SPF: sender matches SPF record Cc: linux-arch , Sinan Kaya , Kukjin Kim , Device Tree ML , Jingoo Han , LKML , Grant Likely , Tanmay Inamdar , Suravee Suthikulanit , Grant Likely , LAKML X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: liviu.dudau@arm.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.44 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Some architectures do not have a simple view of the PCI I/O space and instead use a range of CPU addresses that map to bus addresses. For some architectures these ranges will be expressed by OF bindings in a device tree file. This patch introduces a pci_register_io_range() helper function with a generic implementation that can be used by such architectures to keep track of the I/O ranges described by the PCI bindings. If the PCI_IOBASE macro is not defined that signals lack of support for PCI and we return an error. In order to retrieve the CPU address associated with an I/O port, a new helper function pci_pio_to_address() is introduced. This will search in the list of ranges registered with pci_register_io_range() and return the CPU address that corresponds to the given port. Cc: Grant Likely Cc: Arnd Bergmann Reviewed-by: Catalin Marinas Acked-by: Rob Herring Signed-off-by: Liviu Dudau --- drivers/of/address.c | 100 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/of_address.h | 2 + 2 files changed, 102 insertions(+) diff --git a/drivers/of/address.c b/drivers/of/address.c index e371825..2373a92 100644 --- a/drivers/of/address.c +++ b/drivers/of/address.c @@ -5,6 +5,7 @@ #include #include #include +#include #include /* Max address size we deal with */ @@ -601,12 +602,111 @@ const __be32 *of_get_address(struct device_node *dev, int index, u64 *size, } EXPORT_SYMBOL(of_get_address); +#ifdef PCI_IOBASE +struct io_range { + struct list_head list; + phys_addr_t start; + resource_size_t size; +}; + +static LIST_HEAD(io_range_list); +static DEFINE_SPINLOCK(io_range_lock); +#endif + +/* + * Record the PCI IO range (expressed as CPU physical address + size). + * Return a negative value if an error has occured, zero otherwise + */ +int __weak pci_register_io_range(phys_addr_t addr, resource_size_t size) +{ +#ifdef PCI_IOBASE + struct io_range *range; + resource_size_t allocated_size = 0; + + /* check if the range hasn't been previously recorded */ + spin_lock(&io_range_lock); + list_for_each_entry(range, &io_range_list, list) { + if (addr >= range->start && addr + size <= range->start + size) + return 0; + allocated_size += range->size; + } + spin_unlock(&io_range_lock); + + /* range not registed yet, check for available space */ + if (allocated_size + size - 1 > IO_SPACE_LIMIT) { + /* if it's too big check if 64K space can be reserved */ + if (allocated_size + SZ_64K - 1 > IO_SPACE_LIMIT) + return -E2BIG; + + size = SZ_64K; + pr_warn("Requested IO range too big, new size set to 64K\n"); + } + + /* add the range to the list */ + range = kzalloc(sizeof(*range), GFP_KERNEL); + if (!range) + return -ENOMEM; + + range->start = addr; + range->size = size; + + spin_lock(&io_range_lock); + list_add_tail(&range->list, &io_range_list); + spin_unlock(&io_range_lock); +#endif + + return 0; +} + +phys_addr_t pci_pio_to_address(unsigned long pio) +{ + phys_addr_t address = (phys_addr_t)OF_BAD_ADDR; + +#ifdef PCI_IOBASE + struct io_range *range; + resource_size_t allocated_size = 0; + + if (pio > IO_SPACE_LIMIT) + return address; + + spin_lock(&io_range_lock); + list_for_each_entry(range, &io_range_list, list) { + if (pio >= allocated_size && pio < allocated_size + range->size) { + address = range->start + pio - allocated_size; + break; + } + allocated_size += range->size; + } + spin_unlock(&io_range_lock); +#endif + + return address; +} + unsigned long __weak pci_address_to_pio(phys_addr_t address) { +#ifdef PCI_IOBASE + struct io_range *res; + resource_size_t offset = 0; + unsigned long addr = -1; + + spin_lock(&io_range_lock); + list_for_each_entry(res, &io_range_list, list) { + if (address >= res->start && address < res->start + res->size) { + addr = res->start - address + offset; + break; + } + offset += res->size; + } + spin_unlock(&io_range_lock); + + return addr; +#else if (address > IO_SPACE_LIMIT) return (unsigned long)-1; return (unsigned long) address; +#endif } static int __of_address_to_resource(struct device_node *dev, diff --git a/include/linux/of_address.h b/include/linux/of_address.h index fb7b722..f8cc7da 100644 --- a/include/linux/of_address.h +++ b/include/linux/of_address.h @@ -55,7 +55,9 @@ extern void __iomem *of_iomap(struct device_node *device, int index); extern const __be32 *of_get_address(struct device_node *dev, int index, u64 *size, unsigned int *flags); +extern int pci_register_io_range(phys_addr_t addr, resource_size_t size); extern unsigned long pci_address_to_pio(phys_addr_t addr); +extern phys_addr_t pci_pio_to_address(unsigned long pio); extern int of_pci_range_parser_init(struct of_pci_range_parser *parser, struct device_node *node);