From patchwork Fri May 5 14:00:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oza Pawandeep X-Patchwork-Id: 9713631 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 4D4F960362 for ; Fri, 5 May 2017 14:01:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4702228699 for ; Fri, 5 May 2017 14:01:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3B836286B9; Fri, 5 May 2017 14:01:51 +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 9032528699 for ; Fri, 5 May 2017 14:01:50 +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=9BkpzBSuHXBazp59FP0P4o5V1UQNBYNWTLaaNoJHazA=; b=cB2fnG9tthOwMtGdgxYInxAm66 3HHTc90X8yWytRWcg4eyUb8HN+5BqizIXz61xtadrOaOgDMmL/8PW0+YlgOaNoVaKDaNwyK3sbhEy BgtC+t++oo7yEviS4oSwoQqG/GP7d3bkJFUZVmUtUz/SbWY+gq1IbWn9FrdTdeNtm99a2xL8UNXbk bbc2MBUgrY9MC81h0qGGKjpqdMRJNsJRwFAi0oJ37D5QTSMj7nG8BxW57rbcwIQm7OLIR9bTo7w+8 mJSc9wdjd06rPNyPlmJDe5H2JBiejG2dLlb/QSPi24UZQ4G9qgYEvaDETerIKod0nBM/Jxpo6jJCL dc5V2Abg==; 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 1d6dnp-0002i2-0M; Fri, 05 May 2017 14:01:49 +0000 Received: from mail-wr0-x22c.google.com ([2a00:1450:400c:c0c::22c]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1d6dnN-0001nr-AN for linux-arm-kernel@lists.infradead.org; Fri, 05 May 2017 14:01:29 +0000 Received: by mail-wr0-x22c.google.com with SMTP id w50so4452158wrc.0 for ; Fri, 05 May 2017 07:01:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=KKi4lY5+BeIlYC6yzasGL7ZM0zzusOTPHrXKFeih6ys=; b=MsGN+yQUGAHQxwR8tJBNQK9IvPbFCkdMnFUOK2OCANNC/p9/kv5lOQzdSc9ZCo6oJM M319B4s/FtiH0CVDk5vT3Q+9sfep44Z6LBRk5CKeq0RpCYidzHNHzAR72Qn3NZhd2nbY 7VKugkLy5wf+J/1dDU0CWUKO56G+6xeZwaRzQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=KKi4lY5+BeIlYC6yzasGL7ZM0zzusOTPHrXKFeih6ys=; b=XA4vLPneA9lglI69ZpOa9zHN/2Y45QY5VR9/QJQe/cAIaf1vfw8VDidgqq1stOb3fx PJo7bRpzhUpdWB5RN5gg6qbQBGa1lsQRKEmapL9hwyFaMdsFFCoMNxf3+mi52kSEtrKd OJ7ZVZ+l4e8C248GMK6zdxqiSVZAhbPf3qXy5KlTFZkQPvwk9TJJNoWIc8vFlZC/VgoR ouyR5YXP3qVQRvSIlb/EDoAt2esdBjD2v5dwpWLFjw4sJ3mbCTUwgL5GyWLa8zMcAcop miPCM0GPfamdgoTivfgjv/ggN49ZTk51yi8rMmFcePx3njl3mpZtKRy619XE0iZRXYMG nKFA== X-Gm-Message-State: AN3rC/7qy9xSLXt/fX7gLTNbxHvuIN7JhxnbTnWRR6rA+HA1JBz44Qhp ajWv2EycmIiTFD/b X-Received: by 10.223.151.203 with SMTP id t11mr25180310wrb.18.1493992859350; Fri, 05 May 2017 07:00:59 -0700 (PDT) Received: from anjanavk-OptiPlex-7010.dhcp.avagotech.net ([192.19.237.250]) by smtp.gmail.com with ESMTPSA id 188sm2264174wmf.29.2017.05.05.07.00.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 05 May 2017 07:00:58 -0700 (PDT) From: Oza Pawandeep To: Joerg Roedel , Robin Murphy Subject: [PATCH v5 3/3] PCI/of fix of_dma_get_range; get PCI specific dma-ranges Date: Fri, 5 May 2017 19:30:27 +0530 Message-Id: <1493992827-1553-4-git-send-email-oza.oza@broadcom.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1493992827-1553-1-git-send-email-oza.oza@broadcom.com> References: <1493992827-1553-1-git-send-email-oza.oza@broadcom.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170505_070122_155433_A0AD1034 X-CRM114-Status: GOOD ( 19.69 ) 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: devicetree@vger.kernel.org, Oza Pawandeep , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, bcm-kernel-feedback-list@broadcom.com, Oza Pawandeep , 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 current device framework and OF framework integration assumes dma-ranges in a way where memory-mapped devices define their dma-ranges. (child-bus-address, parent-bus-address, length). of_dma_configure is specifically written to take care of memory mapped devices. but no implementation exists for pci to take care of pcie based memory ranges. for e.g. iproc based SOCs and other SOCs(suc as rcar) have PCI world dma-ranges. dma-ranges = <0x43000000 0x00 0x00 0x00 0x00 0x80 0x00>; this patch fixes the bug in of_dma_get_range, which with as is, parses the PCI memory ranges and return wrong size as 0. in order to get largest possible dma_mask. this patch also retuns the largest possible size based on dma-ranges, for e.g. dma-ranges = <0x43000000 0x00 0x00 0x00 0x00 0x80 0x00>; we should get dev->coherent_dma_mask=0x7fffffffff. based on which IOVA allocation space will honour PCI host bridge limitations. the implementation hooks bus specific callbacks for getting dma-ranges. Signed-off-by: Oza Pawandeep diff --git a/drivers/of/address.c b/drivers/of/address.c index 02b2903..b43e347 100644 --- a/drivers/of/address.c +++ b/drivers/of/address.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,8 @@ struct of_bus { int na, int ns, int pna); int (*translate)(__be32 *addr, u64 offset, int na); unsigned int (*get_flags)(const __be32 *addr); + int (*get_dma_ranges)(struct device_node *np, + u64 *dma_addr, u64 *paddr, u64 *size); }; /* @@ -171,6 +174,144 @@ static int of_bus_pci_translate(__be32 *addr, u64 offset, int na) { return of_bus_default_translate(addr + 1, offset, na - 1); } + +static int of_bus_pci_get_dma_ranges(struct device_node *np, u64 *dma_addr, + u64 *paddr, u64 *size) +{ + struct device_node *node = of_node_get(np); + int ret = 0; + struct resource_entry *window; + LIST_HEAD(res); + + if (!node) + return -EINVAL; + + *size = 0; + /* + * PCI dma-ranges is not mandatory property. + * many devices do no need to have it, since + * host bridge does not require inbound memory + * configuration or rather have design limitations. + * so we look for dma-ranges, if missing we + * just return the caller full size, and also + * no dma-ranges suggests that, host bridge allows + * whatever comes in, so we set dma_addr to 0. + */ + ret = of_pci_get_dma_ranges(np, &res); + if (!ret) { + resource_list_for_each_entry(window, &res) { + struct resource *res_dma = window->res; + + if (*size < resource_size(res_dma)) { + *dma_addr = res_dma->start - window->offset; + *paddr = res_dma->start; + *size = resource_size(res_dma); + } + } + } + pci_free_resource_list(&res); + + /* + * return the largest possible size, + * since PCI master allows everything. + */ + if (*size == 0) { + pr_debug("empty/zero size dma-ranges found for node(%s)\n", + np->full_name); + *size = DMA_BIT_MASK(sizeof(dma_addr_t) * 8) - 1; + *dma_addr = *paddr = 0; + ret = 0; + } + + pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n", + *dma_addr, *paddr, *size); + + of_node_put(node); + + return ret; +} + +static int get_dma_ranges(struct device_node *np, u64 *dma_addr, + u64 *paddr, u64 *size) +{ + struct device_node *node = of_node_get(np); + const __be32 *ranges = NULL; + int len, naddr, nsize, pna; + int ret = 0; + u64 dmaaddr; + + if (!node) + return -EINVAL; + + while (1) { + naddr = of_n_addr_cells(node); + nsize = of_n_size_cells(node); + node = of_get_next_parent(node); + if (!node) + break; + + ranges = of_get_property(node, "dma-ranges", &len); + + /* Ignore empty ranges, they imply no translation required */ + if (ranges && len > 0) + break; + + /* + * At least empty ranges has to be defined for parent node if + * DMA is supported + */ + if (!ranges) + break; + } + + if (!ranges) { + pr_debug("no dma-ranges found for node(%s)\n", np->full_name); + ret = -ENODEV; + goto out; + } + + len /= sizeof(u32); + + pna = of_n_addr_cells(node); + + /* dma-ranges format: + * DMA addr : naddr cells + * CPU addr : pna cells + * size : nsize cells + */ + dmaaddr = of_read_number(ranges, naddr); + *paddr = of_translate_dma_address(np, ranges); + if (*paddr == OF_BAD_ADDR) { + pr_err("translation of DMA address(%pad) to CPU address failed node(%s)\n", + dma_addr, np->full_name); + ret = -EINVAL; + goto out; + } + *dma_addr = dmaaddr; + + *size = of_read_number(ranges + naddr + pna, nsize); + + pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n", + *dma_addr, *paddr, *size); + +out: + of_node_put(node); + + return ret; +} + +static int of_bus_isa_get_dma_ranges(struct device_node *np, u64 *dma_addr, + u64 *paddr, u64 *size) +{ + return get_dma_ranges(np, dma_addr, paddr, size); +} + +static int of_bus_default_get_dma_ranges(struct device_node *np, u64 *dma_addr, + u64 *paddr, u64 *size) +{ + return get_dma_ranges(np, dma_addr, paddr, size); +} + #endif /* CONFIG_OF_ADDRESS_PCI */ #ifdef CONFIG_PCI @@ -424,6 +565,7 @@ static unsigned int of_bus_isa_get_flags(const __be32 *addr) .map = of_bus_pci_map, .translate = of_bus_pci_translate, .get_flags = of_bus_pci_get_flags, + .get_dma_ranges = of_bus_pci_get_dma_ranges, }, #endif /* CONFIG_OF_ADDRESS_PCI */ /* ISA */ @@ -435,6 +577,7 @@ static unsigned int of_bus_isa_get_flags(const __be32 *addr) .map = of_bus_isa_map, .translate = of_bus_isa_translate, .get_flags = of_bus_isa_get_flags, + .get_dma_ranges = of_bus_isa_get_dma_ranges, }, /* Default */ { @@ -445,6 +588,7 @@ static unsigned int of_bus_isa_get_flags(const __be32 *addr) .map = of_bus_default_map, .translate = of_bus_default_translate, .get_flags = of_bus_default_get_flags, + .get_dma_ranges = of_bus_default_get_dma_ranges, }, }; @@ -820,74 +964,20 @@ void __iomem *of_io_request_and_map(struct device_node *np, int index, * size : nsize cells * * It returns -ENODEV if "dma-ranges" property was not found - * for this device in DT. + * for this device in DT, except if PCI device then, dma-ranges + * can be optional property, and in that case returns size with + * entire host memory. */ int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size) { - struct device_node *node = of_node_get(np); - const __be32 *ranges = NULL; - int len, naddr, nsize, pna; - int ret = 0; - u64 dmaaddr; - - if (!node) - return -EINVAL; - - while (1) { - naddr = of_n_addr_cells(node); - nsize = of_n_size_cells(node); - node = of_get_next_parent(node); - if (!node) - break; - - ranges = of_get_property(node, "dma-ranges", &len); - - /* Ignore empty ranges, they imply no translation required */ - if (ranges && len > 0) - break; - - /* - * At least empty ranges has to be defined for parent node if - * DMA is supported - */ - if (!ranges) - break; - } - - if (!ranges) { - pr_debug("no dma-ranges found for node(%s)\n", np->full_name); - ret = -ENODEV; - goto out; - } - - len /= sizeof(u32); - - pna = of_n_addr_cells(node); - - /* dma-ranges format: - * DMA addr : naddr cells - * CPU addr : pna cells - * size : nsize cells - */ - dmaaddr = of_read_number(ranges, naddr); - *paddr = of_translate_dma_address(np, ranges); - if (*paddr == OF_BAD_ADDR) { - pr_err("translation of DMA address(%pad) to CPU address failed node(%s)\n", - dma_addr, np->full_name); - ret = -EINVAL; - goto out; - } - *dma_addr = dmaaddr; - - *size = of_read_number(ranges + naddr + pna, nsize); - - pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n", - *dma_addr, *paddr, *size); + struct of_bus *bus; -out: - of_node_put(node); + /* get bus specific dma-ranges. */ + bus = of_match_bus(np); + if (bus->get_dma_ranges) + return bus->get_dma_ranges(np, dma_addr, paddr, size); - return ret; + return 0; } EXPORT_SYMBOL_GPL(of_dma_get_range);