Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp523991imm; Sat, 7 Jul 2018 02:32:27 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdS8l7fyTVJ2KD8R9j9dQYwCLB0Fh1++3EMsfruufhoEIU/VXiNfqmMct2tDuaJSD9Dc2PE X-Received: by 2002:a65:55c6:: with SMTP id k6-v6mr12028191pgs.6.1530955947181; Sat, 07 Jul 2018 02:32:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530955947; cv=none; d=google.com; s=arc-20160816; b=i+CF8/ixrPY5F7k9aaYAGiihA8B8IOcCpzom8vgmDrharXSPd1N3TD8n2Ishj05FfG +3Fe90yyf42Sn+x7xE1BVURdtuxuuh8DAEBs3zbEYcPZDyfMXppeDlEzfQUr6TQ+xC0z KEIMuZVkUo+5V8iqhkaS5Qo2wlwZ2TXsUO+kG0Pc5YrWkoMUofr+mkKeOkCKomL3026n R1nqPPjgrJciVHf2hBMK4RR0At0eiMdwp/Ox84PE1twTMvRPUeJGYKzERxXZlpA54C6n cKtxIQdCCxLumaa0/iUKP8Sf0uVCKK7TltOLy6i6kWMpxqQhE5kNiAP7SulEK0N9wz3x yqkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=94ZyIMZ/dLB8qvu+JKnRQWXmdTmrE3PuNq6rsqgDX3s=; b=vaZSSCoN/LpKIPYB0ne5JMIsDyU1FHr9qfGb2faAc8NtPv3Gg2MQ/81EhA2ql0CkTN sJqIhNdZa27dMEOEYcNrui1XlPwHZeO28JJgrv97eACQoEKJDP4T7M59OWhK8oqP9v4w a7uHYnw0CA9osl8sWgfFMAjcy3hcwMDUgDZ7b/35XfKlz+oLAuNClnZmpJBjnJ0MNaBq FyLWjDODuv1O4bvaYS/ghoaAZFFcdFsZfrpeMZmUgETVCtfd5JmM/u4X95LeQCQ6Frlw 1fxwBxutX41YbimxNzwbXXED+wPJzyk1oF6xXnaeYooshLz5LoWKucO1FGQs0+7y1UOG WFbg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n187-v6si9786930pgn.368.2018.07.07.02.32.13; Sat, 07 Jul 2018 02:32:27 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754071AbeGGJa5 (ORCPT + 99 others); Sat, 7 Jul 2018 05:30:57 -0400 Received: from mailgw02.mediatek.com ([210.61.82.184]:33475 "EHLO mailgw02.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751693AbeGGJaP (ORCPT ); Sat, 7 Jul 2018 05:30:15 -0400 X-UUID: 8435d2dc943649188c03cd9b619db69b-20180707 Received: from mtkcas07.mediatek.inc [(172.21.101.84)] by mailgw02.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 595739208; Sat, 07 Jul 2018 17:30:12 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs03n2.mediatek.inc (172.21.101.182) with Microsoft SMTP Server (TLS) id 15.0.1210.3; Sat, 7 Jul 2018 17:30:10 +0800 Received: from localhost.localdomain (10.17.3.153) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1210.3 via Frontend Transport; Sat, 7 Jul 2018 17:30:10 +0800 From: Jun Gao To: Wolfram Sang CC: , , , , , , Jun Gao Subject: [PATCH v2 3/3] i2c: mediatek: Use DMA safe buffers for i2c transactions Date: Sat, 7 Jul 2018 17:29:55 +0800 Message-ID: <1530955795-17714-4-git-send-email-jun.gao@mediatek.com> X-Mailer: git-send-email 1.8.1.1.dirty In-Reply-To: <1530955795-17714-1-git-send-email-jun.gao@mediatek.com> References: <1530955795-17714-1-git-send-email-jun.gao@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jun Gao DMA mode will always be used in i2c transactions, try to allocate a DMA safe buffer if the buf of struct i2c_msg used is not DMA safe. Signed-off-by: Jun Gao --- drivers/i2c/busses/i2c-mt65xx.c | 62 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/drivers/i2c/busses/i2c-mt65xx.c b/drivers/i2c/busses/i2c-mt65xx.c index 806e8b90..c92cae7 100644 --- a/drivers/i2c/busses/i2c-mt65xx.c +++ b/drivers/i2c/busses/i2c-mt65xx.c @@ -441,6 +441,8 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, u16 control_reg; u16 restart_flag = 0; u32 reg_4g_mode; + u8 *dma_rd_buf = NULL; + u8 *dma_wr_buf = NULL; dma_addr_t rpaddr = 0; dma_addr_t wpaddr = 0; int ret; @@ -500,10 +502,18 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, if (i2c->op == I2C_MASTER_RD) { writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); writel(I2C_DMA_CON_RX, i2c->pdmabase + OFFSET_CON); - rpaddr = dma_map_single(i2c->dev, msgs->buf, + + dma_rd_buf = i2c_get_dma_safe_msg_buf(msgs, 0); + if (!dma_rd_buf) + return -ENOMEM; + + rpaddr = dma_map_single(i2c->dev, dma_rd_buf, msgs->len, DMA_FROM_DEVICE); - if (dma_mapping_error(i2c->dev, rpaddr)) + if (dma_mapping_error(i2c->dev, rpaddr)) { + i2c_free_dma_safe_msg_buf(msgs, dma_rd_buf); + return -ENOMEM; + } if (i2c->dev_comp->support_33bits) { reg_4g_mode = mtk_i2c_set_4g_mode(rpaddr); @@ -515,10 +525,18 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, } else if (i2c->op == I2C_MASTER_WR) { writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); writel(I2C_DMA_CON_TX, i2c->pdmabase + OFFSET_CON); - wpaddr = dma_map_single(i2c->dev, msgs->buf, + + dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 0); + if (!dma_wr_buf) + return -ENOMEM; + + wpaddr = dma_map_single(i2c->dev, dma_wr_buf, msgs->len, DMA_TO_DEVICE); - if (dma_mapping_error(i2c->dev, wpaddr)) + if (dma_mapping_error(i2c->dev, wpaddr)) { + i2c_free_dma_safe_msg_buf(msgs, dma_wr_buf); + return -ENOMEM; + } if (i2c->dev_comp->support_33bits) { reg_4g_mode = mtk_i2c_set_4g_mode(wpaddr); @@ -530,16 +548,39 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, } else { writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_INT_FLAG); writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_CON); - wpaddr = dma_map_single(i2c->dev, msgs->buf, + + dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 0); + if (!dma_wr_buf) + return -ENOMEM; + + wpaddr = dma_map_single(i2c->dev, dma_wr_buf, msgs->len, DMA_TO_DEVICE); - if (dma_mapping_error(i2c->dev, wpaddr)) + if (dma_mapping_error(i2c->dev, wpaddr)) { + i2c_free_dma_safe_msg_buf(msgs, dma_wr_buf); + return -ENOMEM; - rpaddr = dma_map_single(i2c->dev, (msgs + 1)->buf, + } + + dma_rd_buf = i2c_get_dma_safe_msg_buf((msgs + 1), 0); + if (!dma_rd_buf) { + dma_unmap_single(i2c->dev, wpaddr, + msgs->len, DMA_TO_DEVICE); + + i2c_free_dma_safe_msg_buf(msgs, dma_wr_buf); + + return -ENOMEM; + } + + rpaddr = dma_map_single(i2c->dev, dma_rd_buf, (msgs + 1)->len, DMA_FROM_DEVICE); if (dma_mapping_error(i2c->dev, rpaddr)) { dma_unmap_single(i2c->dev, wpaddr, msgs->len, DMA_TO_DEVICE); + + i2c_free_dma_safe_msg_buf(msgs, dma_wr_buf); + i2c_free_dma_safe_msg_buf((msgs + 1), dma_rd_buf); + return -ENOMEM; } @@ -578,14 +619,21 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, if (i2c->op == I2C_MASTER_WR) { dma_unmap_single(i2c->dev, wpaddr, msgs->len, DMA_TO_DEVICE); + + i2c_release_dma_safe_msg_buf(msgs, dma_wr_buf); } else if (i2c->op == I2C_MASTER_RD) { dma_unmap_single(i2c->dev, rpaddr, msgs->len, DMA_FROM_DEVICE); + + i2c_release_dma_safe_msg_buf(msgs, dma_rd_buf); } else { dma_unmap_single(i2c->dev, wpaddr, msgs->len, DMA_TO_DEVICE); dma_unmap_single(i2c->dev, rpaddr, (msgs + 1)->len, DMA_FROM_DEVICE); + + i2c_release_dma_safe_msg_buf(msgs, dma_wr_buf); + i2c_release_dma_safe_msg_buf((msgs + 1), dma_rd_buf); } if (ret == 0) { -- 1.8.1.1