Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6175814imu; Sun, 2 Dec 2018 11:50:02 -0800 (PST) X-Google-Smtp-Source: AFSGD/V9Lby+29CtF1LWhpn2CIbm1oPtBcCiUyYfTQo+sl08PhLgW6PiBVSpsKOQh7L9Scm4zZYr X-Received: by 2002:a17:902:b112:: with SMTP id q18mr13283033plr.255.1543780202745; Sun, 02 Dec 2018 11:50:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543780202; cv=none; d=google.com; s=arc-20160816; b=eAyqgKw9tT1QuZVEgfWcphZD7A31aWj73dix+ss17f2VXya/wgD4qsIemZGOHGge6Y MIz7RNkZHM0o0A3fq4xHdOKesJHVnPgHM/jtNO9tIjfS9pZDku2kf5zGUszBHsFNsq6S Rk9YnmBt/3BnW7bFp6SG8E19e2LLkewIDWw8cKO7N3Vc/TA8CUEIL/8PY3a2PHzqTZGL twSvM7kUgWFKeO6TS0OXrmJo1AzEEXABNkaZGIWWoYvphcIdlAephQz47K4+mte3QLof 5s4IzVCHC8faAW6qmYpzEv9WnmNq0nRgF/vNkcyeVrUkpvCni3cVqrC47m74nEyi1uIh X+vA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:openpgp:from:references:cc:to:subject :dkim-signature; bh=hyZyF9GQ1xqzTmMsHX2JOnraF+CGzvULpqgyXQAI7jw=; b=Btg0jcey6YV9qgX+gAl2oohmYOrUFrZ0lcj1OrtV5CEzdtN/czaWsh2oruoWtfAjva 0SwTyCi7naChJGZmvKNySWWeGdsyT6GJHpK2KIAFbiIEgusVFmfgwrDAOkJ1h/YNr5dJ ap9/2dTBotVN9EEYxuwVAkNS6JqeqwYeYuSIpBF2kV8OB96aiCk6VvdsrQxgaT48BdTy L01NiRzptFrMS5d00ivsj0MgPQJ1oo7KmOCbq0Abgtz9mind7T3lJHhSsRsP2Mhqy8Re eKO7VU8CwrmfcYdq6yykXJUluhlNx2etSEHmVrsDZA4eVYu1ckSKufTh+24XVKuEu6OM P3Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=T0G7LyLt; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p18si11050672plo.223.2018.12.02.11.49.47; Sun, 02 Dec 2018 11:50:02 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=T0G7LyLt; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725791AbeLBTro (ORCPT + 99 others); Sun, 2 Dec 2018 14:47:44 -0500 Received: from mail-ed1-f66.google.com ([209.85.208.66]:39094 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725616AbeLBTrn (ORCPT ); Sun, 2 Dec 2018 14:47:43 -0500 Received: by mail-ed1-f66.google.com with SMTP id b14so8936676edt.6; Sun, 02 Dec 2018 11:47:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=hyZyF9GQ1xqzTmMsHX2JOnraF+CGzvULpqgyXQAI7jw=; b=T0G7LyLtXLOIWWiZ4X/qhYyiwbh2kcp/txdXpmqm0nyiAKGlM3FLrwMLMSMymj1+pL 6n7TQ6EEQnlksiFAe7kAfjEzpnnwrAhAXYeaRr9eaJy5RCmV7w7k+re6gqOx9mxOHweD 1eyAKMUGfFtBJk30dFLS6v9WCfQqAWNzWUQ8lzvJY2W7A5iwskLKFVw0MJXnQ0fBrxk5 RrY1uNhKsl/icpqIHEC68qSLdc55AKYoTPx7Ev8SwAGXx/dv6ze2NR54zxsZRsRDpdDz 1qynoa5MGyB8aPDUN55DdEWfEisoJXfj4LVzWkA9VQ48WKH4JBETwL0T6vC9Muae0bPg v/sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:openpgp:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=hyZyF9GQ1xqzTmMsHX2JOnraF+CGzvULpqgyXQAI7jw=; b=H0dPerq4tdM3n727Iv3ZoYGzZ6RojPwvJjSW7HAcU0/+oaLo7QaexHnAP0b2KbfLFV cvj1f/OK9GnLJE47/fFOXq92NZhs4RyxyHLl8uou1d7qdU3s+Diu4fT2gZROzyBhmT4u ufMlXGdaeZIFfkP4DSuCaV27LYKciRt72dCaFTwu75ONMS3uG37YlmnJQF9YZv6+jgpo eQ7l/5xf9IvyZRl1+B1FQHSO9sQIUGpEGqH5KXBxI7/5K3AXAp3sBzawns7KxDZ732B0 hpg0qdN2Hq0USz3beU27vD6eOTLiUkXZAcgr+DgkZgVuAmTn8+WiPzyS0wpV0gnS7J/+ Lqhg== X-Gm-Message-State: AA+aEWY1JMYiCYInoiZlJJuNVp3AcUgInzTDwklZRJUVVMH3yRSU9+Gy 4VmJXXRknP92FayXExAS5fU= X-Received: by 2002:a50:a5e2:: with SMTP id b31mr12157642edc.5.1543780058908; Sun, 02 Dec 2018 11:47:38 -0800 (PST) Received: from ziggy.stardust ([93.176.147.153]) by smtp.gmail.com with ESMTPSA id g3sm3382431edc.63.2018.12.02.11.47.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 02 Dec 2018 11:47:38 -0800 (PST) Subject: Re: [PATCH v27 2/2] soc: mediatek: Add Mediatek CMDQ helper To: Houlong Wei Cc: Jassi Brar , Rob Herring , Daniel Kurtz , Sascha Hauer , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, srv_heupstream@mediatek.com, Sascha Hauer , Philipp Zabel , Nicolas Boichat , CK HU , Bibby Hsieh , YT Shen , Daoyuan Huang , Jiaguang Zhang , Dennis-YC Hsieh , Monica Wang , HS Liao , ginny.chen@mediatek.com, enzhu.wang@mediatek.com, kendrick.hsu@mediatek.com References: <1543462629-32746-1-git-send-email-houlong.wei@mediatek.com> <1543462629-32746-3-git-send-email-houlong.wei@mediatek.com> From: Matthias Brugger Openpgp: preference=signencrypt Autocrypt: addr=matthias.bgg@gmail.com; prefer-encrypt=mutual; keydata= xsFNBFP1zgUBEAC21D6hk7//0kOmsUrE3eZ55kjc9DmFPKIz6l4NggqwQjBNRHIMh04BbCMY fL3eT7ZsYV5nur7zctmJ+vbszoOASXUpfq8M+S5hU2w7sBaVk5rpH9yW8CUWz2+ZpQXPJcFa OhLZuSKB1F5JcvLbETRjNzNU7B3TdS2+zkgQQdEyt7Ij2HXGLJ2w+yG2GuR9/iyCJRf10Okq gTh//XESJZ8S6KlOWbLXRE+yfkKDXQx2Jr1XuVvM3zPqH5FMg8reRVFsQ+vI0b+OlyekT/Xe 0Hwvqkev95GG6x7yseJwI+2ydDH6M5O7fPKFW5mzAdDE2g/K9B4e2tYK6/rA7Fq4cqiAw1+u EgO44+eFgv082xtBez5WNkGn18vtw0LW3ESmKh19u6kEGoi0WZwslCNaGFrS4M7OH+aOJeqK fx5dIv2CEbxc6xnHY7dwkcHikTA4Qdb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Sm3I++aYozF25xQoT+7zCx2cQi33jwvnJAK1o4VlNx36RfrxzBqc1uZGzJBCQu4 8UjmUSsTwWC3HpE/D9sM+xACs803lFUIZC5H62G059cCPAXKgsFpNMKmBAWweBkVJAisoQeX 50OP+/11ArV0cv+fOTfJj0/KwFXJaaYh3LUQNILLBNxkSrhCLl8dUg53IbHx4NfIAgqxLWGf XM8DY1aFdU79pac005PuhxCWkKTJz3gCmznnoat4GCnL5gy/m0Qk45l4PFqwWXVLo9AQg2Kp 3mlIFZ6fsEKIAN5hxlbNvNb9V2Zo5bFZjPWPFTxOteM0omUAS+QopwU0yPLLGJVf2iCmItHc UXI+r2JwH1CJjrHWeQEI2ucSKsNa8FllDmG/fQ== Message-ID: Date: Sun, 2 Dec 2018 20:47:36 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: <1543462629-32746-3-git-send-email-houlong.wei@mediatek.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 29/11/2018 04:37, Houlong Wei wrote: > Add Mediatek CMDQ helper to create CMDQ packet and assemble GCE op code. > > Signed-off-by: Houlong Wei > Signed-off-by: HS Liao > --- > drivers/soc/mediatek/Kconfig | 12 ++ > drivers/soc/mediatek/Makefile | 1 + > drivers/soc/mediatek/mtk-cmdq-helper.c | 300 ++++++++++++++++++++++++++++++++ > include/linux/soc/mediatek/mtk-cmdq.h | 133 ++++++++++++++ > 4 files changed, 446 insertions(+) > create mode 100644 drivers/soc/mediatek/mtk-cmdq-helper.c > create mode 100644 include/linux/soc/mediatek/mtk-cmdq.h > Now part of v4.20-next/soc Thanks a lot! > diff --git a/drivers/soc/mediatek/Kconfig b/drivers/soc/mediatek/Kconfig > index a7d0667..17bd759 100644 > --- a/drivers/soc/mediatek/Kconfig > +++ b/drivers/soc/mediatek/Kconfig > @@ -4,6 +4,18 @@ > menu "MediaTek SoC drivers" > depends on ARCH_MEDIATEK || COMPILE_TEST > > +config MTK_CMDQ > + tristate "MediaTek CMDQ Support" > + depends on ARCH_MEDIATEK || COMPILE_TEST > + select MAILBOX > + select MTK_CMDQ_MBOX > + select MTK_INFRACFG > + help > + Say yes here to add support for the MediaTek Command Queue (CMDQ) > + driver. The CMDQ is used to help read/write registers with critical > + time limitation, such as updating display configuration during the > + vblank. > + > config MTK_INFRACFG > bool "MediaTek INFRACFG Support" > select REGMAP > diff --git a/drivers/soc/mediatek/Makefile b/drivers/soc/mediatek/Makefile > index 12998b0..64ce5ee 100644 > --- a/drivers/soc/mediatek/Makefile > +++ b/drivers/soc/mediatek/Makefile > @@ -1,3 +1,4 @@ > +obj-$(CONFIG_MTK_CMDQ) += mtk-cmdq-helper.o > obj-$(CONFIG_MTK_INFRACFG) += mtk-infracfg.o > obj-$(CONFIG_MTK_PMIC_WRAP) += mtk-pmic-wrap.o > obj-$(CONFIG_MTK_SCPSYS) += mtk-scpsys.o > diff --git a/drivers/soc/mediatek/mtk-cmdq-helper.c b/drivers/soc/mediatek/mtk-cmdq-helper.c > new file mode 100644 > index 0000000..ff9fef5 > --- /dev/null > +++ b/drivers/soc/mediatek/mtk-cmdq-helper.c > @@ -0,0 +1,300 @@ > +// SPDX-License-Identifier: GPL-2.0 > +// > +// Copyright (c) 2018 MediaTek Inc. > + > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define CMDQ_ARG_A_WRITE_MASK 0xffff > +#define CMDQ_WRITE_ENABLE_MASK BIT(0) > +#define CMDQ_EOC_IRQ_EN BIT(0) > +#define CMDQ_EOC_CMD ((u64)((CMDQ_CODE_EOC << CMDQ_OP_CODE_SHIFT)) \ > + << 32 | CMDQ_EOC_IRQ_EN) > + > +static void cmdq_client_timeout(struct timer_list *t) > +{ > + struct cmdq_client *client = from_timer(client, t, timer); > + > + dev_err(client->client.dev, "cmdq timeout!\n"); > +} > + > +struct cmdq_client *cmdq_mbox_create(struct device *dev, int index, u32 timeout) > +{ > + struct cmdq_client *client; > + > + client = kzalloc(sizeof(*client), GFP_KERNEL); > + if (!client) > + return (struct cmdq_client *)-ENOMEM; > + > + client->timeout_ms = timeout; > + if (timeout != CMDQ_NO_TIMEOUT) { > + spin_lock_init(&client->lock); > + timer_setup(&client->timer, cmdq_client_timeout, 0); > + } > + client->pkt_cnt = 0; > + client->client.dev = dev; > + client->client.tx_block = false; > + client->chan = mbox_request_channel(&client->client, index); > + > + if (IS_ERR(client->chan)) { > + long err; > + > + dev_err(dev, "failed to request channel\n"); > + err = PTR_ERR(client->chan); > + kfree(client); > + > + return ERR_PTR(err); > + } > + > + return client; > +} > +EXPORT_SYMBOL(cmdq_mbox_create); > + > +void cmdq_mbox_destroy(struct cmdq_client *client) > +{ > + if (client->timeout_ms != CMDQ_NO_TIMEOUT) { > + spin_lock(&client->lock); > + del_timer_sync(&client->timer); > + spin_unlock(&client->lock); > + } > + mbox_free_channel(client->chan); > + kfree(client); > +} > +EXPORT_SYMBOL(cmdq_mbox_destroy); > + > +struct cmdq_pkt *cmdq_pkt_create(struct cmdq_client *client, size_t size) > +{ > + struct cmdq_pkt *pkt; > + struct device *dev; > + dma_addr_t dma_addr; > + > + pkt = kzalloc(sizeof(*pkt), GFP_KERNEL); > + if (!pkt) > + return ERR_PTR(-ENOMEM); > + pkt->va_base = kzalloc(size, GFP_KERNEL); > + if (!pkt->va_base) { > + kfree(pkt); > + return ERR_PTR(-ENOMEM); > + } > + pkt->buf_size = size; > + pkt->cl = (void *)client; > + > + dev = client->chan->mbox->dev; > + dma_addr = dma_map_single(dev, pkt->va_base, pkt->buf_size, > + DMA_TO_DEVICE); > + if (dma_mapping_error(dev, dma_addr)) { > + dev_err(dev, "dma map failed, size=%u\n", (u32)(u64)size); > + kfree(pkt->va_base); > + kfree(pkt); > + return ERR_PTR(-ENOMEM); > + } > + > + pkt->pa_base = dma_addr; > + > + return pkt; > +} > +EXPORT_SYMBOL(cmdq_pkt_create); > + > +void cmdq_pkt_destroy(struct cmdq_pkt *pkt) > +{ > + struct cmdq_client *client = (struct cmdq_client *)pkt->cl; > + > + dma_unmap_single(client->chan->mbox->dev, pkt->pa_base, pkt->buf_size, > + DMA_TO_DEVICE); > + kfree(pkt->va_base); > + kfree(pkt); > +} > +EXPORT_SYMBOL(cmdq_pkt_destroy); > + > +static int cmdq_pkt_append_command(struct cmdq_pkt *pkt, enum cmdq_code code, > + u32 arg_a, u32 arg_b) > +{ > + u64 *cmd_ptr; > + > + if (unlikely(pkt->cmd_buf_size + CMDQ_INST_SIZE > pkt->buf_size)) { > + /* > + * In the case of allocated buffer size (pkt->buf_size) is used > + * up, the real required size (pkt->cmdq_buf_size) is still > + * increased, so that the user knows how much memory should be > + * ultimately allocated after appending all commands and > + * flushing the command packet. Therefor, the user can call > + * cmdq_pkt_create() again with the real required buffer size. > + */ > + pkt->cmd_buf_size += CMDQ_INST_SIZE; > + WARN_ONCE(1, "%s: buffer size %u is too small !\n", > + __func__, (u32)pkt->buf_size); > + return -ENOMEM; > + } > + cmd_ptr = pkt->va_base + pkt->cmd_buf_size; > + (*cmd_ptr) = (u64)((code << CMDQ_OP_CODE_SHIFT) | arg_a) << 32 | arg_b; > + pkt->cmd_buf_size += CMDQ_INST_SIZE; > + > + return 0; > +} > + > +int cmdq_pkt_write(struct cmdq_pkt *pkt, u32 value, u32 subsys, u32 offset) > +{ > + u32 arg_a = (offset & CMDQ_ARG_A_WRITE_MASK) | > + (subsys << CMDQ_SUBSYS_SHIFT); > + > + return cmdq_pkt_append_command(pkt, CMDQ_CODE_WRITE, arg_a, value); > +} > +EXPORT_SYMBOL(cmdq_pkt_write); > + > +int cmdq_pkt_write_mask(struct cmdq_pkt *pkt, u32 value, > + u32 subsys, u32 offset, u32 mask) > +{ > + u32 offset_mask = offset; > + int err = 0; > + > + if (mask != 0xffffffff) { > + err = cmdq_pkt_append_command(pkt, CMDQ_CODE_MASK, 0, ~mask); > + offset_mask |= CMDQ_WRITE_ENABLE_MASK; > + } > + err |= cmdq_pkt_write(pkt, value, subsys, offset_mask); > + > + return err; > +} > +EXPORT_SYMBOL(cmdq_pkt_write_mask); > + > +int cmdq_pkt_wfe(struct cmdq_pkt *pkt, u32 event) > +{ > + u32 arg_b; > + > + if (event >= CMDQ_MAX_EVENT) > + return -EINVAL; > + > + /* > + * WFE arg_b > + * bit 0-11: wait value > + * bit 15: 1 - wait, 0 - no wait > + * bit 16-27: update value > + * bit 31: 1 - update, 0 - no update > + */ > + arg_b = CMDQ_WFE_UPDATE | CMDQ_WFE_WAIT | CMDQ_WFE_WAIT_VALUE; > + > + return cmdq_pkt_append_command(pkt, CMDQ_CODE_WFE, event, arg_b); > +} > +EXPORT_SYMBOL(cmdq_pkt_wfe); > + > +int cmdq_pkt_clear_event(struct cmdq_pkt *pkt, u32 event) > +{ > + if (event >= CMDQ_MAX_EVENT) > + return -EINVAL; > + > + return cmdq_pkt_append_command(pkt, CMDQ_CODE_WFE, event, > + CMDQ_WFE_UPDATE); > +} > +EXPORT_SYMBOL(cmdq_pkt_clear_event); > + > +static int cmdq_pkt_finalize(struct cmdq_pkt *pkt) > +{ > + int err; > + > + /* insert EOC and generate IRQ for each command iteration */ > + err = cmdq_pkt_append_command(pkt, CMDQ_CODE_EOC, 0, CMDQ_EOC_IRQ_EN); > + > + /* JUMP to end */ > + err |= cmdq_pkt_append_command(pkt, CMDQ_CODE_JUMP, 0, CMDQ_JUMP_PASS); > + > + return err; > +} > + > +static void cmdq_pkt_flush_async_cb(struct cmdq_cb_data data) > +{ > + struct cmdq_pkt *pkt = (struct cmdq_pkt *)data.data; > + struct cmdq_task_cb *cb = &pkt->cb; > + struct cmdq_client *client = (struct cmdq_client *)pkt->cl; > + > + if (client->timeout_ms != CMDQ_NO_TIMEOUT) { > + unsigned long flags = 0; > + > + spin_lock_irqsave(&client->lock, flags); > + if (--client->pkt_cnt == 0) > + del_timer(&client->timer); > + else > + mod_timer(&client->timer, jiffies + > + msecs_to_jiffies(client->timeout_ms)); > + spin_unlock_irqrestore(&client->lock, flags); > + } > + > + dma_sync_single_for_cpu(client->chan->mbox->dev, pkt->pa_base, > + pkt->cmd_buf_size, DMA_TO_DEVICE); > + if (cb->cb) { > + data.data = cb->data; > + cb->cb(data); > + } > +} > + > +int cmdq_pkt_flush_async(struct cmdq_pkt *pkt, cmdq_async_flush_cb cb, > + void *data) > +{ > + int err; > + unsigned long flags = 0; > + struct cmdq_client *client = (struct cmdq_client *)pkt->cl; > + > + err = cmdq_pkt_finalize(pkt); > + if (err < 0) > + return err; > + > + pkt->cb.cb = cb; > + pkt->cb.data = data; > + pkt->async_cb.cb = cmdq_pkt_flush_async_cb; > + pkt->async_cb.data = pkt; > + > + dma_sync_single_for_device(client->chan->mbox->dev, pkt->pa_base, > + pkt->cmd_buf_size, DMA_TO_DEVICE); > + > + if (client->timeout_ms != CMDQ_NO_TIMEOUT) { > + spin_lock_irqsave(&client->lock, flags); > + if (client->pkt_cnt++ == 0) > + mod_timer(&client->timer, jiffies + > + msecs_to_jiffies(client->timeout_ms)); > + spin_unlock_irqrestore(&client->lock, flags); > + } > + > + mbox_send_message(client->chan, pkt); > + /* We can send next packet immediately, so just call txdone. */ > + mbox_client_txdone(client->chan, 0); > + > + return 0; > +} > +EXPORT_SYMBOL(cmdq_pkt_flush_async); > + > +struct cmdq_flush_completion { > + struct completion cmplt; > + bool err; > +}; > + > +static void cmdq_pkt_flush_cb(struct cmdq_cb_data data) > +{ > + struct cmdq_flush_completion *cmplt; > + > + cmplt = (struct cmdq_flush_completion *)data.data; > + if (data.sta != CMDQ_CB_NORMAL) > + cmplt->err = true; > + else > + cmplt->err = false; > + complete(&cmplt->cmplt); > +} > + > +int cmdq_pkt_flush(struct cmdq_pkt *pkt) > +{ > + struct cmdq_flush_completion cmplt; > + int err; > + > + init_completion(&cmplt.cmplt); > + err = cmdq_pkt_flush_async(pkt, cmdq_pkt_flush_cb, &cmplt); > + if (err < 0) > + return err; > + wait_for_completion(&cmplt.cmplt); > + > + return cmplt.err ? -EFAULT : 0; > +} > +EXPORT_SYMBOL(cmdq_pkt_flush); > + > +MODULE_LICENSE("GPL v2"); > diff --git a/include/linux/soc/mediatek/mtk-cmdq.h b/include/linux/soc/mediatek/mtk-cmdq.h > new file mode 100644 > index 0000000..54ade13 > --- /dev/null > +++ b/include/linux/soc/mediatek/mtk-cmdq.h > @@ -0,0 +1,133 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * Copyright (c) 2018 MediaTek Inc. > + * > + */ > + > +#ifndef __MTK_CMDQ_H__ > +#define __MTK_CMDQ_H__ > + > +#include > +#include > +#include > + > +#define CMDQ_NO_TIMEOUT 0xffffffffu > + > +/** cmdq event maximum */ > +#define CMDQ_MAX_EVENT 0x3ff > + > +struct cmdq_pkt; > + > +struct cmdq_client { > + spinlock_t lock; > + u32 pkt_cnt; > + struct mbox_client client; > + struct mbox_chan *chan; > + struct timer_list timer; > + u32 timeout_ms; /* in unit of microsecond */ > +}; > + > +/** > + * cmdq_mbox_create() - create CMDQ mailbox client and channel > + * @dev: device of CMDQ mailbox client > + * @index: index of CMDQ mailbox channel > + * @timeout: timeout of a pkt execution by GCE, in unit of microsecond, set > + * CMDQ_NO_TIMEOUT if a timer is not used. > + * > + * Return: CMDQ mailbox client pointer > + */ > +struct cmdq_client *cmdq_mbox_create(struct device *dev, int index, > + u32 timeout); > + > +/** > + * cmdq_mbox_destroy() - destroy CMDQ mailbox client and channel > + * @client: the CMDQ mailbox client > + */ > +void cmdq_mbox_destroy(struct cmdq_client *client); > + > +/** > + * cmdq_pkt_create() - create a CMDQ packet > + * @client: the CMDQ mailbox client > + * @size: required CMDQ buffer size > + * > + * Return: CMDQ packet pointer > + */ > +struct cmdq_pkt *cmdq_pkt_create(struct cmdq_client *client, size_t size); > + > +/** > + * cmdq_pkt_destroy() - destroy the CMDQ packet > + * @pkt: the CMDQ packet > + */ > +void cmdq_pkt_destroy(struct cmdq_pkt *pkt); > + > +/** > + * cmdq_pkt_write() - append write command to the CMDQ packet > + * @pkt: the CMDQ packet > + * @value: the specified target register value > + * @subsys: the CMDQ sub system code > + * @offset: register offset from CMDQ sub system > + * > + * Return: 0 for success; else the error code is returned > + */ > +int cmdq_pkt_write(struct cmdq_pkt *pkt, u32 value, u32 subsys, u32 offset); > + > +/** > + * cmdq_pkt_write_mask() - append write command with mask to the CMDQ packet > + * @pkt: the CMDQ packet > + * @value: the specified target register value > + * @subsys: the CMDQ sub system code > + * @offset: register offset from CMDQ sub system > + * @mask: the specified target register mask > + * > + * Return: 0 for success; else the error code is returned > + */ > +int cmdq_pkt_write_mask(struct cmdq_pkt *pkt, u32 value, > + u32 subsys, u32 offset, u32 mask); > + > +/** > + * cmdq_pkt_wfe() - append wait for event command to the CMDQ packet > + * @pkt: the CMDQ packet > + * @event: the desired event type to "wait and CLEAR" > + * > + * Return: 0 for success; else the error code is returned > + */ > +int cmdq_pkt_wfe(struct cmdq_pkt *pkt, u32 event); > + > +/** > + * cmdq_pkt_clear_event() - append clear event command to the CMDQ packet > + * @pkt: the CMDQ packet > + * @event: the desired event to be cleared > + * > + * Return: 0 for success; else the error code is returned > + */ > +int cmdq_pkt_clear_event(struct cmdq_pkt *pkt, u32 event); > + > +/** > + * cmdq_pkt_flush_async() - trigger CMDQ to asynchronously execute the CMDQ > + * packet and call back at the end of done packet > + * @pkt: the CMDQ packet > + * @cb: called at the end of done packet > + * @data: this data will pass back to cb > + * > + * Return: 0 for success; else the error code is returned > + * > + * Trigger CMDQ to asynchronously execute the CMDQ packet and call back > + * at the end of done packet. Note that this is an ASYNC function. When the > + * function returned, it may or may not be finished. > + */ > +int cmdq_pkt_flush_async(struct cmdq_pkt *pkt, cmdq_async_flush_cb cb, > + void *data); > + > +/** > + * cmdq_pkt_flush() - trigger CMDQ to execute the CMDQ packet > + * @pkt: the CMDQ packet > + * > + * Return: 0 for success; else the error code is returned > + * > + * Trigger CMDQ to execute the CMDQ packet. Note that this is a > + * synchronous flush function. When the function returned, the recorded > + * commands have been done. > + */ > +int cmdq_pkt_flush(struct cmdq_pkt *pkt); > + > +#endif /* __MTK_CMDQ_H__ */ >