Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp996733pxu; Fri, 16 Oct 2020 00:42:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzh/H2hH4AnSx9ydS8OkTugVt9/qXcc73AWsxCsFzkU5qHVgxWF0ulp+mpzrmGLSqvQY+Td X-Received: by 2002:a50:d5de:: with SMTP id g30mr2473957edj.245.1602834124727; Fri, 16 Oct 2020 00:42:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1602834124; cv=pass; d=google.com; s=arc-20160816; b=vpTDIfapJbwgGPMHSo5SZnunZByXuG+xKdkKXwaGWQMmOt8MuK3+F19buFWQdHhhdT ZWQNa1/z+vUODVororkCDIXS4asL2QNN+/FgHFJwsuX1xm9KSAXDaLktLQuSvRCL2qi3 VZJ21gn1SRoKmpQe1o/d/JCpyTDq1QbDKnmG7wE0EgtG9TKOFS37po6caY63YRm1iGmg PpN2rUTUaWFZoOwuZ3PZ0BXLAvOnBidlj09q+nALWe2sQFZ7WJiKIQr7odJEugX1LuS5 fm0WgJhQgI24ndBaE25MR63gDF8Dy5Lt56wZlnFjUAEjf0OX9ZQTf4P9Fm1c5aK03hxv DGRw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=D0yuSkDRNJU7GdRfvpVp+lS6DCfstUx8mWBjsEkTkAA=; b=NwyYXEMroQ9Iqzq/o2ANpKVsbrkfg8Hw8FcpfxYQVKYjBCOU+skIEfwMvBKsJFBte0 YnD3m4C1nYYZE0xwsVhSZJlBSAgMlb7hMszVnWO64UriyYiY3Fkv5aCDaG0cuLbx/x/m kPV3jMvUhVjP56vIiDCvL/yu+xKCdLBTlm4n+dp3gvbyPk5nYRIi1o+ZKBq6AtfhxHZo Dh4jRJXBMyr2e2emdIPAh1mSR/PxIuX79OcuCpPzTRo0VeNAHgxWZFLuS7JSb6q4Vwjx AgCbZDXfyZfXuZWW/aDKKTd4upkGYUi0lgAlwIAdoBEJIBOOxMNB9CCJCab9wCq4aVx0 n3sg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=SbyIbW5T; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q5si1100991edv.293.2020.10.16.00.41.42; Fri, 16 Oct 2020 00:42:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=SbyIbW5T; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404726AbgJPHkP (ORCPT + 99 others); Fri, 16 Oct 2020 03:40:15 -0400 Received: from mail-dm6nam10on2086.outbound.protection.outlook.com ([40.107.93.86]:18913 "EHLO NAM10-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2404630AbgJPHkP (ORCPT ); Fri, 16 Oct 2020 03:40:15 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YnrzkY+V2uju2ZFULEniyC/Pwagd/57zTfguYsjX3aL1jIF2TmODAYG2wcHD6zeWNPZjhQaYi6OB/Vgs5S7egTCVE7tEu27xTtTbBM/s4uaJQ0nZnX0lUNh73AOJb7nUyy4neIfKYnlRa9sXim4YESNqEWZVm7kXquak1lMnmy2jnUR/d6+ADsf0dlWoibWTzY29gOBJqjbBhiUZIxaPxZqk2z9Eu6UJWqONZFoSJ0FSzY4hdrsfi6UoGco0IC5paQ7giQjVZEAvAgKUoAsIV6xlAc5G+7ml0RGtyluR3FQ/KAQEL+UGX1RyqXaco1QvqetwQekSsFXIpraJFI8erA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=D0yuSkDRNJU7GdRfvpVp+lS6DCfstUx8mWBjsEkTkAA=; b=evUGVw6QiKaSZqEVVaQ6oTrY5OYLZcLnbbvKfBq4qJbKeF0abvBEa5LufUB5zuXQN1oe1rURo1vzUG61EYvHWqM1l/KDUCBfdB85TYg53qhFe2d3sqE+n2u3gvLaMSCdGbh4GKSTFQkhyWc1x+BkR+F4pyg3TX2SIjhxXLdeZ0Q0MTByzkdDAOiZ63zzSz7H/mJ7mUMXLyopAmEc2mcTiNeNJu2n2Mo7tPeSXvEPE0W7I1yUeaaNZNsouhQ5IxFgFpRmSBwaz6YTMb4t1cJcWP7abnOJw10YvRJwhBqU7l1ltWO96BfuMt5DYKil2kfueB6Afj1/f3UOS92VRQKKEg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=D0yuSkDRNJU7GdRfvpVp+lS6DCfstUx8mWBjsEkTkAA=; b=SbyIbW5T7PW2+yz8MZz1txVWXahwkM3O5Y3bjTIN8NZ7W5vqAxtfCXt6GS2lbnOL8ibR/WfUHuaobiyowYms0k37RFsDYoqby/SVE/sSO8NTG2lMdauz+Gw+31l/DEPMIMD6eQoMiMhJcjqy/Lt44vTlInI0WBQY81aRIgI2vTE= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from BN8PR12MB3540.namprd12.prod.outlook.com (2603:10b6:408:6c::33) by BN6PR1201MB0212.namprd12.prod.outlook.com (2603:10b6:405:56::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3477.20; Fri, 16 Oct 2020 07:40:10 +0000 Received: from BN8PR12MB3540.namprd12.prod.outlook.com ([fe80::9d06:595:4be5:3af7]) by BN8PR12MB3540.namprd12.prod.outlook.com ([fe80::9d06:595:4be5:3af7%7]) with mapi id 15.20.3477.024; Fri, 16 Oct 2020 07:40:10 +0000 From: Sanjay R Mehta To: vkoul@kernel.org Cc: gregkh@linuxfoundation.org, dan.j.williams@intel.com, Thomas.Lendacky@amd.com, Shyam-sundar.S-k@amd.com, Nehal-bakulchandra.Shah@amd.com, robh@kernel.org, mchehab+samsung@kernel.org, davem@davemloft.net, linux-kernel@vger.kernel.org, dmaengine@vger.kernel.org, Sanjay R Mehta Subject: [PATCH v7 2/3] dmaengine: ptdma: register PTDMA controller as a DMA resource Date: Fri, 16 Oct 2020 02:39:06 -0500 Message-Id: <1602833947-82021-3-git-send-email-Sanju.Mehta@amd.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1602833947-82021-1-git-send-email-Sanju.Mehta@amd.com> References: <1602833947-82021-1-git-send-email-Sanju.Mehta@amd.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.156.251] X-ClientProxiedBy: MAXPR01CA0101.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:5d::19) To BN8PR12MB3540.namprd12.prod.outlook.com (2603:10b6:408:6c::33) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sanjuamdntb2.amd.com (165.204.156.251) by MAXPR01CA0101.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:5d::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.20.3477.21 via Frontend Transport; Fri, 16 Oct 2020 07:40:06 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: f041f533-896e-4b85-3c6b-08d871a6b577 X-MS-TrafficTypeDiagnostic: BN6PR1201MB0212: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:154; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Qve4YGWp6hHlMDfLimX5NEuH/KtNPAn70nk6DETpcLYW3asRZpXCqpZztyT05hl8YT4HMx9tFY5e0OqdfUXCcBPjJDiVYUegsZSTHNpIW3LKIt4WwUzh5bXrAokNfxRWUVPP6MaovEp7V+rOQPjNnLuqCF3KaHMi8qYZ4E4WipT6HG/7SzpeV6DR6KqlI/ytur9XZjOsv7G+/br8evfumlig2gKE9JQ8Y1HR8xwQQq41nVjLBwWlLeAkZvo3WWmix+2B4XxYEecfNdKxl97GPdP4bQzXvB5Q2tClfn6lLRPvKgcaW8pKpiEsb3gy7aQMGPaUGPRk8Git+1qNyfWwmMNn+pK9cammkpIscP2sIEQWaVfqYs8cLlzeXjMatLgr X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN8PR12MB3540.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(376002)(136003)(346002)(39860400002)(396003)(366004)(478600001)(4326008)(6916009)(5660300002)(2616005)(956004)(34490700002)(6486002)(86362001)(316002)(7696005)(16526019)(186003)(52116002)(26005)(8936002)(30864003)(8676002)(66946007)(66556008)(36756003)(2906002)(66476007)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: YBRayofbeWJ/037F/S07ergwBtrXnGIGAoupv/UYb0eYj9jgNJ2FmVSQVHt177OOV+0lsV0HbBQySwx2uecRB/4iI5ucfFCKgtDs+CXgSyF7UHSB0SlQsXeJMNU/tleFmGBmN0/UmvaSIIB7xq+vPs67z7GmGsTrMorwqIxoWVbIo+7Vli6dnf/uc7EE9Gs7Ba/v7UsLe3gcoLEMJhICizc+uGJHjNqT+HT46PJkkdJ8u0Ufo7mWROzdKKzMZhUzVQlikX7tm9d9y7IYZTx4r+VmdV+4/af5PQo+PFAjOURv2rStYCd8sb/MAt1UdM0RtiEmG2cFSBbuRk89W2HwgJOfsfQxxjWRbefCPM3rZJ/WSnAIGspKSkLNbtWeYYh13FHYLfSlQMFjmUB/ScgZIkkcN6Sbu/gZ9eitOmJvugH1K89Icja48w1BSa5/1LbhfH1j2FhbODF5W5cM+6ZT/Y2qfCP/lylb7dOx7uiwphloJh4gPS8oW1ocVnsNj+YGjwPMwOlpNms73DLofqWOkcU3hvAvQyVV9zTQ/PSNYyywjXML8yoOEfgIQPm2UqYboAc3bOcZZkh8HtuJTEaxUhNj6kksLdy2FUPIUR8jHQ1gIat8RtjVqb8oO1OmUs6tp9NYkevXsU2UKkcyLKLxYA== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f041f533-896e-4b85-3c6b-08d871a6b577 X-MS-Exchange-CrossTenant-AuthSource: BN8PR12MB3540.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Oct 2020 07:40:09.8463 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: P3Izo37YqcQkMAVewU180XkX6yLD+kTau7GWx0g0uys3tfu5ouTv7T2HScqeU/28Xy2uAnKw3ieP/nSpBfgMbQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR1201MB0212 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sanjay R Mehta Register ptdma queue to Linux dmaengine framework as general-purpose DMA channels. Signed-off-by: Sanjay R Mehta --- drivers/dma/ptdma/Kconfig | 2 + drivers/dma/ptdma/Makefile | 2 +- drivers/dma/ptdma/ptdma-dev.c | 32 +++ drivers/dma/ptdma/ptdma-dmaengine.c | 554 ++++++++++++++++++++++++++++++++++++ drivers/dma/ptdma/ptdma.h | 31 ++ 5 files changed, 620 insertions(+), 1 deletion(-) create mode 100644 drivers/dma/ptdma/ptdma-dmaengine.c diff --git a/drivers/dma/ptdma/Kconfig b/drivers/dma/ptdma/Kconfig index f93f9c2..694ed27 100644 --- a/drivers/dma/ptdma/Kconfig +++ b/drivers/dma/ptdma/Kconfig @@ -2,6 +2,8 @@ config AMD_PTDMA tristate "AMD PassThru DMA Engine" depends on X86_64 && PCI + select DMA_ENGINE + select DMA_VIRTUAL_CHANNELS help Enable support for the AMD PTDMA controller. This controller provides DMA capabilities & performs high bandwidth memory to diff --git a/drivers/dma/ptdma/Makefile b/drivers/dma/ptdma/Makefile index 320fa82..a528cb0 100644 --- a/drivers/dma/ptdma/Makefile +++ b/drivers/dma/ptdma/Makefile @@ -5,6 +5,6 @@ obj-$(CONFIG_AMD_PTDMA) += ptdma.o -ptdma-objs := ptdma-dev.o +ptdma-objs := ptdma-dev.o ptdma-dmaengine.o ptdma-$(CONFIG_PCI) += ptdma-pci.o diff --git a/drivers/dma/ptdma/ptdma-dev.c b/drivers/dma/ptdma/ptdma-dev.c index d1e9892..4bdad35 100644 --- a/drivers/dma/ptdma/ptdma-dev.c +++ b/drivers/dma/ptdma/ptdma-dev.c @@ -127,6 +127,26 @@ static inline void pt_core_enable_queue_interrupts(struct pt_device *pt) iowrite32(SUPPORTED_INTERRUPTS, pt->cmd_q.reg_int_enable); } +static void pt_do_cmd_complete(unsigned long data) +{ + struct pt_tasklet_data *tdata = (struct pt_tasklet_data *)data; + struct pt_cmd *cmd = tdata->cmd; + struct pt_cmd_queue *cmd_q = &cmd->pt->cmd_q; + u32 tail; + + tail = lower_32_bits(cmd_q->qdma_tail + cmd_q->qidx * Q_DESC_SIZE); + if (cmd_q->cmd_error) { + /* + * Log the error and flush the queue by + * moving the head pointer + */ + pt_log_error(cmd_q->pt, cmd_q->cmd_error); + iowrite32(tail, cmd_q->reg_head_lo); + } + + cmd->pt_cmd_callback(cmd->data, cmd->ret); +} + static irqreturn_t pt_core_irq_handler(int irq, void *data) { struct pt_device *pt = (struct pt_device *)data; @@ -150,6 +170,7 @@ static irqreturn_t pt_core_irq_handler(int irq, void *data) } pt_core_enable_queue_interrupts(pt); + pt_do_cmd_complete((ulong)&pt->tdata); return IRQ_HANDLED; } @@ -253,8 +274,16 @@ int pt_core_init(struct pt_device *pt) pt_core_enable_queue_interrupts(pt); + /* Register the DMA engine support */ + ret = pt_dmaengine_register(pt); + if (ret) + goto e_dmaengine; + return 0; +e_dmaengine: + free_irq(pt->pt_irq, pt); + e_dma_alloc: dma_free_coherent(dev, cmd_q->qsize, cmd_q->qbase, cmd_q->qbase_dma); @@ -270,6 +299,9 @@ void pt_core_destroy(struct pt_device *pt) struct pt_cmd_queue *cmd_q = &pt->cmd_q; struct pt_cmd *cmd; + /* Unregister the DMA engine */ + pt_dmaengine_unregister(pt); + /* Disable and clear interrupts */ pt_core_disable_queue_interrupts(pt); diff --git a/drivers/dma/ptdma/ptdma-dmaengine.c b/drivers/dma/ptdma/ptdma-dmaengine.c new file mode 100644 index 0000000..9b9b77c --- /dev/null +++ b/drivers/dma/ptdma/ptdma-dmaengine.c @@ -0,0 +1,554 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * AMD Passthrough DMA device driver + * -- Based on the CCP driver + * + * Copyright (C) 2016,2020 Advanced Micro Devices, Inc. + * + * Author: Sanjay R Mehta + * Author: Gary R Hook + */ + +#include "ptdma.h" +#include "../dmaengine.h" +#include "../virt-dma.h" + +#define PT_DMA_WIDTH(_mask) \ +({ \ + u64 mask = (_mask) + 1; \ + (mask == 0) ? 64 : fls64(mask); \ +}) + +static inline struct pt_dma_chan *to_pt_chan(struct dma_chan *dma_chan) +{ + return container_of(dma_chan, struct pt_dma_chan, vc.chan); +} + +static inline struct pt_dma_desc *to_pt_desc(struct virt_dma_desc *vd) +{ + return container_of(vd, struct pt_dma_desc, vd); +} + +static void pt_free_cmd_resources(struct pt_device *pt, + struct list_head *list) +{ + struct pt_dma_cmd *cmd, *ctmp; + + list_for_each_entry_safe(cmd, ctmp, list, entry) { + list_del(&cmd->entry); + kmem_cache_free(pt->dma_cmd_cache, cmd); + } +} + +static void pt_free_chan_resources(struct dma_chan *dma_chan) +{ + struct pt_dma_chan *chan = to_pt_chan(dma_chan); + + vchan_free_chan_resources(&chan->vc); +} + +static void pt_synchronize(struct dma_chan *dma_chan) +{ + struct pt_dma_chan *chan = to_pt_chan(dma_chan); + + vchan_synchronize(&chan->vc); +} + +static void pt_do_cleanup(struct virt_dma_desc *vd) +{ + struct pt_dma_desc *desc = to_pt_desc(vd); + struct pt_device *pt = desc->pt; + + pt_free_cmd_resources(pt, &desc->cmdlist); + kmem_cache_free(pt->dma_desc_cache, desc); +} + +static int pt_issue_next_cmd(struct pt_dma_desc *desc) +{ + struct pt_passthru_engine *pt_engine; + struct pt_dma_cmd *cmd; + struct pt_device *pt; + struct pt_cmd *pt_cmd; + struct pt_cmd_queue *cmd_q; + + cmd = list_first_entry(&desc->cmdlist, struct pt_dma_cmd, entry); + desc->issued_to_hw = 1; + + pt_cmd = &cmd->pt_cmd; + pt = pt_cmd->pt; + cmd_q = &pt->cmd_q; + pt_engine = &pt_cmd->passthru; + + pt->tdata.cmd = pt_cmd; + + /* Execute the command */ + pt_cmd->ret = pt_core_perform_passthru(cmd_q, pt_engine); + + return 0; +} + +static void pt_free_active_cmd(struct pt_dma_desc *desc) +{ + struct pt_dma_cmd *cmd = NULL; + + if (desc->issued_to_hw) + cmd = list_first_entry_or_null(&desc->cmdlist, struct pt_dma_cmd, + entry); + if (!cmd) + return; + + list_del(&cmd->entry); + kmem_cache_free(desc->pt->dma_cmd_cache, cmd); +} + +static struct pt_dma_desc *pt_next_dma_desc(struct pt_dma_chan *chan) +{ + /* Get the next DMA descriptor on the active list */ + struct virt_dma_desc *vd = vchan_next_desc(&chan->vc); + + return vd ? to_pt_desc(vd) : NULL; +} + +static struct pt_dma_desc *__pt_next_dma_desc(struct pt_dma_chan *chan) +{ + /* Get the next DMA descriptor on the active list */ + struct virt_dma_desc *vd = vchan_next_desc(&chan->vc); + + if (list_empty(&chan->vc.desc_submitted)) + return NULL; + + vd = list_empty(&chan->vc.desc_issued) ? + list_first_entry(&chan->vc.desc_submitted, + struct virt_dma_desc, node) : NULL; + + vchan_issue_pending(&chan->vc); + + return vd ? to_pt_desc(vd) : NULL; +} + +static struct pt_dma_desc *pt_handle_active_desc(struct pt_dma_chan *chan, + struct pt_dma_desc *desc) +{ + struct dma_async_tx_descriptor *tx_desc; + struct virt_dma_desc *vd; + unsigned long flags; + + /* Loop over descriptors until one is found with commands */ + do { + if (desc) { + /* Remove the DMA command from the list and free it */ + pt_free_active_cmd(desc); + if (!desc->issued_to_hw) { + /* No errors, keep going */ + if (desc->status != DMA_ERROR) + return desc; + /* Error, free remaining commands and move on */ + pt_free_cmd_resources(desc->pt, + &desc->cmdlist); + } + + tx_desc = &desc->vd.tx; + vd = &desc->vd; + } else { + tx_desc = NULL; + } + + spin_lock_irqsave(&chan->vc.lock, flags); + + if (desc) { + if (desc->status != DMA_ERROR) + desc->status = DMA_COMPLETE; + + dma_cookie_complete(tx_desc); + dma_descriptor_unmap(tx_desc); + list_del(&desc->vd.node); + } + + desc = pt_next_dma_desc(chan); + + spin_unlock_irqrestore(&chan->vc.lock, flags); + + if (tx_desc) { + dmaengine_desc_get_callback_invoke(tx_desc, NULL); + dma_run_dependencies(tx_desc); + vchan_vdesc_fini(vd); + } + } while (desc); + + return NULL; +} + +static void pt_cmd_callback(void *data, int err) +{ + struct pt_dma_desc *desc = data; + struct dma_chan *dma_chan; + struct pt_dma_chan *chan; + int ret; + + if (err == -EINPROGRESS) + return; + + dma_chan = desc->vd.tx.chan; + chan = to_pt_chan(dma_chan); + + if (err) + desc->status = DMA_ERROR; + + while (true) { + /* Check for DMA descriptor completion */ + desc = pt_handle_active_desc(chan, desc); + + /* Don't submit cmd if no descriptor or DMA is paused */ + if (!desc) + break; + + ret = pt_issue_next_cmd(desc); + if (!ret) + break; + + desc->status = DMA_ERROR; + } +} + +static struct pt_dma_cmd *pt_alloc_dma_cmd(struct pt_dma_chan *chan) +{ + struct pt_dma_cmd *cmd; + + cmd = kmem_cache_zalloc(chan->pt->dma_cmd_cache, GFP_NOWAIT); + + return cmd; +} + +static struct pt_dma_desc *pt_alloc_dma_desc(struct pt_dma_chan *chan, + unsigned long flags) +{ + struct pt_dma_desc *desc; + + desc = kmem_cache_zalloc(chan->pt->dma_desc_cache, GFP_NOWAIT); + if (!desc) + return NULL; + + vchan_tx_prep(&chan->vc, &desc->vd, flags); + + desc->pt = chan->pt; + desc->issued_to_hw = 0; + INIT_LIST_HEAD(&desc->cmdlist); + desc->status = DMA_IN_PROGRESS; + + return desc; +} + +static struct pt_dma_desc *pt_create_desc(struct dma_chan *dma_chan, + struct scatterlist *dst_sg, + unsigned int dst_nents, + struct scatterlist *src_sg, + unsigned int src_nents, + unsigned long flags) +{ + struct pt_dma_chan *chan = to_pt_chan(dma_chan); + struct pt_device *pt = chan->pt; + struct pt_dma_desc *desc; + struct pt_dma_cmd *cmd; + struct pt_cmd *pt_cmd; + struct pt_passthru_engine *pt_engine; + unsigned int src_offset, src_len; + unsigned int dst_offset, dst_len; + unsigned int len; + size_t total_len; + + if (!dst_sg || !src_sg) + return NULL; + + if (!dst_nents || !src_nents) + return NULL; + + desc = pt_alloc_dma_desc(chan, flags); + if (!desc) + return NULL; + + total_len = 0; + + src_len = sg_dma_len(src_sg); + src_offset = 0; + + dst_len = sg_dma_len(dst_sg); + dst_offset = 0; + + while (true) { + if (!src_len) { + src_nents--; + if (!src_nents) + break; + + src_sg = sg_next(src_sg); + if (!src_sg) + break; + + src_len = sg_dma_len(src_sg); + src_offset = 0; + continue; + } + + if (!dst_len) { + dst_nents--; + if (!dst_nents) + break; + + dst_sg = sg_next(dst_sg); + if (!dst_sg) + break; + + dst_len = sg_dma_len(dst_sg); + dst_offset = 0; + continue; + } + + len = min(dst_len, src_len); + + cmd = pt_alloc_dma_cmd(chan); + if (!cmd) + goto err; + + pt_cmd = &cmd->pt_cmd; + pt_cmd->pt = chan->pt; + pt_engine = &pt_cmd->passthru; + pt_cmd->engine = PT_ENGINE_PASSTHRU; + pt_engine->src_dma = sg_dma_address(src_sg) + src_offset; + pt_engine->dst_dma = sg_dma_address(dst_sg) + dst_offset; + pt_engine->src_len = len; + pt_cmd->pt_cmd_callback = pt_cmd_callback; + pt_cmd->data = desc; + + list_add_tail(&cmd->entry, &desc->cmdlist); + + total_len += len; + + src_len -= len; + src_offset += len; + + dst_len -= len; + dst_offset += len; + } + + desc->len = total_len; + + if (list_empty(&desc->cmdlist)) + goto err; + + return desc; + +err: + pt_free_cmd_resources(pt, &desc->cmdlist); + kmem_cache_free(pt->dma_desc_cache, desc); + + return NULL; +} + +static struct dma_async_tx_descriptor * +pt_prep_dma_memcpy(struct dma_chan *dma_chan, dma_addr_t dst, + dma_addr_t src, size_t len, unsigned long flags) +{ + struct scatterlist dst_sg, src_sg; + struct pt_dma_desc *desc; + + sg_init_table(&dst_sg, 1); + sg_dma_address(&dst_sg) = dst; + sg_dma_len(&dst_sg) = len; + + sg_init_table(&src_sg, 1); + sg_dma_address(&src_sg) = src; + sg_dma_len(&src_sg) = len; + + desc = pt_create_desc(dma_chan, &dst_sg, 1, &src_sg, 1, flags); + if (!desc) + return NULL; + + return &desc->vd.tx; +} + +static struct dma_async_tx_descriptor * +pt_prep_dma_interrupt(struct dma_chan *dma_chan, unsigned long flags) +{ + struct pt_dma_chan *chan = to_pt_chan(dma_chan); + struct pt_dma_desc *desc; + + desc = pt_alloc_dma_desc(chan, flags); + if (!desc) + return NULL; + + return &desc->vd.tx; +} + +static void pt_issue_pending(struct dma_chan *dma_chan) +{ + struct pt_dma_chan *chan = to_pt_chan(dma_chan); + struct pt_dma_desc *desc; + unsigned long flags; + + spin_lock_irqsave(&chan->vc.lock, flags); + + desc = __pt_next_dma_desc(chan); + + spin_unlock_irqrestore(&chan->vc.lock, flags); + + /* If there was nothing active, start processing */ + if (desc) + pt_cmd_callback(desc, 0); +} + +static enum dma_status pt_tx_status(struct dma_chan *dma_chan, + dma_cookie_t cookie, + struct dma_tx_state *state) +{ + struct pt_dma_chan *chan = to_pt_chan(dma_chan); + struct pt_dma_desc *desc; + enum dma_status ret; + unsigned long flags; + struct virt_dma_desc *vd; + + ret = dma_cookie_status(dma_chan, cookie, state); + if (ret == DMA_COMPLETE) + return ret; + + spin_lock_irqsave(&chan->vc.lock, flags); + vd = vchan_find_desc(&chan->vc, cookie); + desc = vd ? to_pt_desc(vd) : NULL; + ret = desc->status; + spin_unlock_irqrestore(&chan->vc.lock, flags); + + return ret; +} + +static int pt_pause(struct dma_chan *dma_chan) +{ + struct pt_dma_chan *chan = to_pt_chan(dma_chan); + unsigned long flags; + + spin_lock_irqsave(&chan->vc.lock, flags); + pt_stop_queue(&chan->pt->cmd_q); + spin_unlock_irqrestore(&chan->vc.lock, flags); + + return 0; +} + +static int pt_resume(struct dma_chan *dma_chan) +{ + struct pt_dma_chan *chan = to_pt_chan(dma_chan); + struct pt_dma_desc *desc = NULL; + unsigned long flags; + + spin_lock_irqsave(&chan->vc.lock, flags); + pt_start_queue(&chan->pt->cmd_q); + desc = __pt_next_dma_desc(chan); + spin_unlock_irqrestore(&chan->vc.lock, flags); + + /* If there was something active, re-start */ + if (desc) + pt_cmd_callback(desc, 0); + + return 0; +} + +static int pt_terminate_all(struct dma_chan *dma_chan) +{ + struct pt_dma_chan *chan = to_pt_chan(dma_chan); + + vchan_free_chan_resources(&chan->vc); + + return 0; +} + +int pt_dmaengine_register(struct pt_device *pt) +{ + struct pt_dma_chan *chan; + struct dma_device *dma_dev = &pt->dma_dev; + char *dma_cmd_cache_name; + char *dma_desc_cache_name; + int ret; + + pt->pt_dma_chan = devm_kzalloc(pt->dev, sizeof(*pt->pt_dma_chan), + GFP_KERNEL); + if (!pt->pt_dma_chan) + return -ENOMEM; + + dma_cmd_cache_name = devm_kasprintf(pt->dev, GFP_KERNEL, + "%s-dmaengine-cmd-cache", + pt->name); + if (!dma_cmd_cache_name) + return -ENOMEM; + + pt->dma_cmd_cache = kmem_cache_create(dma_cmd_cache_name, + sizeof(struct pt_dma_cmd), + sizeof(void *), + SLAB_HWCACHE_ALIGN, NULL); + if (!pt->dma_cmd_cache) + return -ENOMEM; + + dma_desc_cache_name = devm_kasprintf(pt->dev, GFP_KERNEL, + "%s-dmaengine-desc-cache", + pt->name); + if (!dma_desc_cache_name) { + ret = -ENOMEM; + goto err_cache; + } + + pt->dma_desc_cache = kmem_cache_create(dma_desc_cache_name, + sizeof(struct pt_dma_desc), + sizeof(void *), + SLAB_HWCACHE_ALIGN, NULL); + if (!pt->dma_desc_cache) { + ret = -ENOMEM; + goto err_cache; + } + + dma_dev->dev = pt->dev; + dma_dev->src_addr_widths = PT_DMA_WIDTH(dma_get_mask(pt->dev)); + dma_dev->dst_addr_widths = PT_DMA_WIDTH(dma_get_mask(pt->dev)); + dma_dev->directions = DMA_MEM_TO_MEM; + dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR; + dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask); + dma_cap_set(DMA_INTERRUPT, dma_dev->cap_mask); + dma_cap_set(DMA_PRIVATE, dma_dev->cap_mask); + + INIT_LIST_HEAD(&dma_dev->channels); + + chan = pt->pt_dma_chan; + chan->pt = pt; + + dma_dev->device_free_chan_resources = pt_free_chan_resources; + dma_dev->device_prep_dma_memcpy = pt_prep_dma_memcpy; + dma_dev->device_prep_dma_interrupt = pt_prep_dma_interrupt; + dma_dev->device_issue_pending = pt_issue_pending; + dma_dev->device_tx_status = pt_tx_status; + dma_dev->device_pause = pt_pause; + dma_dev->device_resume = pt_resume; + dma_dev->device_terminate_all = pt_terminate_all; + dma_dev->device_synchronize = pt_synchronize; + + chan->vc.desc_free = pt_do_cleanup; + vchan_init(&chan->vc, dma_dev); + + ret = dma_async_device_register(dma_dev); + if (ret) + goto err_reg; + + return 0; + +err_reg: + kmem_cache_destroy(pt->dma_desc_cache); + +err_cache: + kmem_cache_destroy(pt->dma_cmd_cache); + + return ret; +} + +void pt_dmaengine_unregister(struct pt_device *pt) +{ + struct dma_device *dma_dev = &pt->dma_dev; + + dma_async_device_unregister(dma_dev); + + kmem_cache_destroy(pt->dma_desc_cache); + kmem_cache_destroy(pt->dma_cmd_cache); +} diff --git a/drivers/dma/ptdma/ptdma.h b/drivers/dma/ptdma/ptdma.h index 9e5819f..67c24bd 100644 --- a/drivers/dma/ptdma/ptdma.h +++ b/drivers/dma/ptdma/ptdma.h @@ -20,6 +20,9 @@ #include #include #include +#include + +#include "../virt-dma.h" #define MAX_PT_NAME_LEN 16 #define MAX_DMAPOOL_NAME_LEN 32 @@ -172,6 +175,25 @@ struct pt_cmd { void *data; }; +struct pt_dma_cmd { + struct list_head entry; + struct pt_cmd pt_cmd; +}; + +struct pt_dma_desc { + struct virt_dma_desc vd; + struct pt_device *pt; + struct list_head cmdlist; + enum dma_status status; + size_t len; + bool issued_to_hw; +}; + +struct pt_dma_chan { + struct virt_dma_chan vc; + struct pt_device *pt; +}; + struct pt_cmd_queue { struct pt_device *pt; @@ -240,6 +262,12 @@ struct pt_device { */ struct pt_cmd_queue cmd_q; + /* Support for the DMA Engine capabilities */ + struct dma_device dma_dev; + struct pt_dma_chan *pt_dma_chan; + struct kmem_cache *dma_cmd_cache; + struct kmem_cache *dma_desc_cache; + wait_queue_head_t lsb_queue; struct pt_tasklet_data tdata; @@ -304,6 +332,9 @@ struct pt_dev_vdata { const unsigned int bar; }; +int pt_dmaengine_register(struct pt_device *pt); +void pt_dmaengine_unregister(struct pt_device *pt); + int pt_core_init(struct pt_device *pt); void pt_core_destroy(struct pt_device *pt); -- 2.7.4