From patchwork Sat Apr 22 08:08:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oza Pawandeep X-Patchwork-Id: 9694249 X-Patchwork-Delegate: bhelgaas@google.com 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 B2B786038E for ; Sat, 22 Apr 2017 08:11:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A3C9628621 for ; Sat, 22 Apr 2017 08:11:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9702E28628; Sat, 22 Apr 2017 08:11:45 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3F9C228625 for ; Sat, 22 Apr 2017 08:11:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1043302AbdDVILU (ORCPT ); Sat, 22 Apr 2017 04:11:20 -0400 Received: from mail-wm0-f43.google.com ([74.125.82.43]:34684 "EHLO mail-wm0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1043170AbdDVIIl (ORCPT ); Sat, 22 Apr 2017 04:08:41 -0400 Received: by mail-wm0-f43.google.com with SMTP id r190so1501131wme.1 for ; Sat, 22 Apr 2017 01:08:40 -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; bh=tkSYtTHzjHbppVdqxDCRxE+Me3dROyjD2On8g40OEe0=; b=IOz8mkFjZFsChUXGgYRrOg1sT3gv6XnU9z0QEIslDpJXS1Xa3+MLZrmfTGD+0GaK6M TwkwMrTP1wZP4AQxr8i/s22xX1js2fQo1hQexpJdas1S4sObXJzhPBM1ffd0he8pJDEH G5SYCwl6fMZylTQWqpk3R1J/wl26qUf4CdwAU= 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; bh=tkSYtTHzjHbppVdqxDCRxE+Me3dROyjD2On8g40OEe0=; b=GY2Izp2Q+a+H/MEho7yTdD0yZbKRl466I63QJgvYMkQu6cwm237VVV6QzF+Kr8qk7m wSDmvy5jXyjhlEOt7QI1HJAlckD15ZiX/POZp7hHnBz790ykbsVT4OD2/TzgwXxXJfcW hKX+oN91VvkIVl29wHMXw8qNT5Flu/Zz9kiq6ctSdYMYd6Z3wL4ifULpSr/iZoskIm2u rYrBp4GcCxbvpn01gzVR9kVIJsq9SmDpKY4TdwlJKoEkeAG7szoVO3VT5vOY7XhAkQmU amESjwX/34+xt2PK44llObymkcnT9AEVbPe1ejBqfeFYImx81o+OS+hAAdeciX3KWzg3 9x7w== X-Gm-Message-State: AN3rC/5UCM7VVzXhod8JUzzDTyQpXH1Td8T8oQpt+NAy0b2/CugVv3UX EEatpf9XLjE1C6gNioM= X-Received: by 10.28.156.140 with SMTP id f134mr1940729wme.40.1492848519830; Sat, 22 Apr 2017 01:08:39 -0700 (PDT) Received: from anjanavk-OptiPlex-7010.dhcp.avagotech.net ([192.19.237.250]) by smtp.gmail.com with ESMTPSA id l19sm14376326wre.2.2017.04.22.01.08.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 22 Apr 2017 01:08:38 -0700 (PDT) From: Oza Pawandeep To: Joerg Roedel , Robin Murphy Cc: iommu@lists.linux-foundation.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org, bcm-kernel-feedback-list@broadcom.com, Oza Pawandeep , Oza Pawandeep Subject: [RFC PATH] of/pci/dma: fix DMA configruation for PCI masters Date: Sat, 22 Apr 2017 13:38:07 +0530 Message-Id: <1492848487-11768-1-git-send-email-oza.oza@broadcom.com> X-Mailer: git-send-email 1.9.1 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP current device frmework and of framework integration assumes dma-ranges in a way where memory-mapped devices define their dma-ranges. dma-ranges: (child-bus-address, parent-bus-address, length). but iproc based SOCs and other SOCs(suc as rcar) have PCI world dma-ranges. dma-ranges = <0x43000000 0x00 0x00 0x00 0x00 0x80 0x00>; of_dma_configure is specifically witten to take care of memory mapped devices. but no implementation exists for pci to take care of pcie based memory ranges. in fact pci world doesnt seem to define standard dma-ranges this patch served following purposes 1) exposes intrface to the pci host driver for thir inbound memory ranges 2) provide an interface to callers such as of_dma_get_ranges. so then the returned size get best possible (largest) dma_mask. for e.g. dma-ranges = <0x43000000 0x00 0x00 0x00 0x00 0x80 0x00>; we should get dev->coherent_dma_mask=0x7fffffffff. 3) this patch handles multiple inbound windows and dma-ranges. it is left to the caller, how it wants to use them. the new function returns the resources in a standard and unform way 4) this way the callers of of_dma_get_ranges does not need to change. and 5) leaves scope of adding PCI flag handling for inbound memory by the new function. Signed-off-by: Oza Pawandeep diff --git a/drivers/of/address.c b/drivers/of/address.c index 02b2903..ec21191 100644 --- a/drivers/of/address.c +++ b/drivers/of/address.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -829,10 +830,30 @@ int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *siz int len, naddr, nsize, pna; int ret = 0; u64 dmaaddr; + struct resource_entry *window; + LIST_HEAD(res); if (!node) return -EINVAL; + if (strcmp(np->name, "pci")) { + *size = 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); + goto out; + } + while (1) { naddr = of_n_addr_cells(node); nsize = of_n_size_cells(node); diff --git a/drivers/of/of_pci.c b/drivers/of/of_pci.c index 0ee42c3..2aa9401 100644 --- a/drivers/of/of_pci.c +++ b/drivers/of/of_pci.c @@ -283,6 +283,83 @@ int of_pci_get_host_bridge_resources(struct device_node *dev, return err; } EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources); + +/** + * of_pci_get_dma_ranges - Parse PCI host bridge inbound resources from DT + * @np: device node of the host bridge having the dma-ranges property + * @resources: list where the range of resources will be added after DT parsing + * + * It is the caller's job to free the @resources list. + * + * This function will parse the "dma-ranges" property of a + * PCI host bridge device node and setup the resource mapping based + * on its content. + * + * It returns zero if the range parsing has been successful or a standard error + * value if it failed. + */ + +int of_pci_get_dma_ranges(struct device_node *np, struct list_head *resources) +{ + struct device_node *node = of_node_get(np); + int rlen; + int ret = 0; + const int na = 3, ns = 2; + struct resource *res; + struct of_pci_range_parser parser; + struct of_pci_range range; + + if (!node) + return -EINVAL; + + parser.node = node; + parser.pna = of_n_addr_cells(node); + parser.np = parser.pna + na + ns; + + parser.range = of_get_property(node, "dma-ranges", &rlen); + + if (!parser.range) { + pr_debug("pcie device has no dma-ranges defined for node(%s)\n", + np->full_name); + ret = -ENODEV; + goto out; + } + + parser.end = parser.range + rlen / sizeof(__be32); + + for_each_of_pci_range(&parser, &range) { + /* + * If we failed translation or got a zero-sized region + * then skip this range + */ + if (range.cpu_addr == OF_BAD_ADDR || range.size == 0) + continue; + + res = kzalloc(sizeof(struct resource), GFP_KERNEL); + if (!res) { + ret = -ENOMEM; + goto parse_failed; + } + + ret = of_pci_range_to_resource(&range, np, res); + if (ret) { + kfree(res); + continue; + } + + pci_add_resource_offset(resources, res, + res->start - range.pci_addr); + } + + return ret; + +parse_failed: + pci_free_resource_list(resources); +out: + of_node_put(node); + return ret; +} +EXPORT_SYMBOL_GPL(of_pci_get_dma_ranges); #endif /* CONFIG_OF_ADDRESS */ #ifdef CONFIG_PCI_MSI diff --git a/include/linux/of_pci.h b/include/linux/of_pci.h index 0e0974e..617b90d 100644 --- a/include/linux/of_pci.h +++ b/include/linux/of_pci.h @@ -76,6 +76,7 @@ static inline void of_pci_check_probe_only(void) { } int of_pci_get_host_bridge_resources(struct device_node *dev, unsigned char busno, unsigned char bus_max, struct list_head *resources, resource_size_t *io_base); +int of_pci_get_dma_ranges(struct device_node *np, struct list_head *resources); #else static inline int of_pci_get_host_bridge_resources(struct device_node *dev, unsigned char busno, unsigned char bus_max, @@ -83,6 +84,12 @@ static inline int of_pci_get_host_bridge_resources(struct device_node *dev, { return -EINVAL; } + +static inline int of_pci_get_dma_ranges(struct device_node *np, + struct list_head *resources) +{ + return -EINVAL; +} #endif #if defined(CONFIG_OF) && defined(CONFIG_PCI_MSI)