Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2056342pxb; Mon, 11 Oct 2021 20:26:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzp2/9PFlXjZKplVzq4GsKzYwNvjBbQVpOOfJpIH7UEar6NHMRxQLvUr/R8CbZqc6S2WGmJ X-Received: by 2002:a63:4f43:: with SMTP id p3mr20761437pgl.435.1634009199073; Mon, 11 Oct 2021 20:26:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634009199; cv=none; d=google.com; s=arc-20160816; b=lzFu1uQ6N48LVI9seqJCZJWi/OqLTXQP+B65TKpTqsbmgg0FNscKVLtkkPkU9hjiQJ ZBhmB2uTIe3FGOTW12xyZf7BLFeYXTOidUuksUxthejBLuNU2l+7SD+TeEIWdbyXzdXJ rOkkABAeMwaV8A/HmtAICFLX4Dq9TAblH17r2YunlLWKUh3T8OxsRCxrWrf6RHy/l9fM wCOZIIl8GtPLzfxDTC1hX6Rxz/oh2f1DvaH4R0LV2im1S7fZ5R9+Pby5xcdnDHA00SV5 qSAfFkrDVhBxHeieki1Z+/73fvSNRT8pXbhskgJ+4+xljNWzt4cPwfM5Va2CxikBRK+x ZK7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=q7Lg8fyn07ADjDsxMw6jQAqAKoRn7ilIng5grDvkO/M=; b=zLdUxGvX0gJgK1YHXIItvHtdVnkqqzgAlH69q7i1OYv5wOHdqi/tMeOtlIrut9DLcK N9PK978afLWyuMRJuSdYDE3sv9sRpON8NlgYfo9/GWClphNHcRiU4TeWEGG5QtQZRxxQ aAEXqKCedGWiYF6WvYDcitdQC5v8Jnjf8bPSeamAeLtPfn1a9aLveBLMh59p1oE9WsOd W0z2KtxquNyaK3sAJqTXjEFAUs+OD2GDARHMNSbjM2X83N/qCwOx3dz2XtunkqRW45m+ 1VNFJf8HuLCAsLSUXxWQ4A1fpwbmk5+QNwFXlQ+dSnkfQbsIFfTveL6moFaoz4Dy5VA9 06gw== ARC-Authentication-Results: i=1; mx.google.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 j30si4614314pga.452.2021.10.11.20.26.27; Mon, 11 Oct 2021 20:26:39 -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; 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 S232126AbhJLD0z (ORCPT + 99 others); Mon, 11 Oct 2021 23:26:55 -0400 Received: from mx22.baidu.com ([220.181.50.185]:40066 "EHLO baidu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231755AbhJLD0y (ORCPT ); Mon, 11 Oct 2021 23:26:54 -0400 Received: from BC-Mail-Ex11.internal.baidu.com (unknown [172.31.51.51]) by Forcepoint Email with ESMTPS id 10170D13D26DC406E149; Tue, 12 Oct 2021 11:24:52 +0800 (CST) Received: from BJHW-MAIL-EX27.internal.baidu.com (10.127.64.42) by BC-Mail-Ex11.internal.baidu.com (172.31.51.51) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2242.12; Tue, 12 Oct 2021 11:24:51 +0800 Received: from LAPTOP-UKSR4ENP.internal.baidu.com (172.31.63.8) by BJHW-MAIL-EX27.internal.baidu.com (10.127.64.42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2308.14; Tue, 12 Oct 2021 11:24:48 +0800 From: Cai Huoqing To: CC: Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Peter Huewe , Jarkko Sakkinen , Jason Gunthorpe , , , Subject: [PATCH v2] tpm: ibmvtpm: Make use of dma_alloc_noncoherent() Date: Tue, 12 Oct 2021 11:24:41 +0800 Message-ID: <20211012032442.2423-1-caihuoqing@baidu.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [172.31.63.8] X-ClientProxiedBy: BC-Mail-EX08.internal.baidu.com (172.31.51.48) To BJHW-MAIL-EX27.internal.baidu.com (10.127.64.42) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Replacing kmalloc/kfree/get_zeroed_page/free_page/dma_map_single/ dma_unmap_single() with dma_alloc_noncoherent/dma_free_noncoherent() helps to reduce code size, and simplify the code, and the hardware can keep DMA coherent itself. Signed-off-by: Cai Huoqing --- v1->v2: *Change to dma_alloc/free_noncoherent from dma_alloc/free_coherent. *Update changelog. drivers/char/tpm/tpm_ibmvtpm.c | 63 +++++++++++----------------------- 1 file changed, 20 insertions(+), 43 deletions(-) diff --git a/drivers/char/tpm/tpm_ibmvtpm.c b/drivers/char/tpm/tpm_ibmvtpm.c index 3af4c07a9342..b4552f8400b8 100644 --- a/drivers/char/tpm/tpm_ibmvtpm.c +++ b/drivers/char/tpm/tpm_ibmvtpm.c @@ -356,15 +356,13 @@ static void tpm_ibmvtpm_remove(struct vio_dev *vdev) rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); - dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle, - CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL); - free_page((unsigned long)ibmvtpm->crq_queue.crq_addr); - - if (ibmvtpm->rtce_buf) { - dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle, - ibmvtpm->rtce_size, DMA_BIDIRECTIONAL); - kfree(ibmvtpm->rtce_buf); - } + dma_free_noncoherent(ibmvtpm->dev, CRQ_RES_BUF_SIZE, crq_q->crq_addr, + crq_q->crq_dma_handle, DMA_BIDIRECTIONAL); + + if (ibmvtpm->rtce_buf) + dma_free_noncoherent(ibmvtpm->dev, + ibmvtpm->rtce_size, ibmvtpm->rtce_buf, + ibmvtpm->rtce_dma_handle, DMA_BIDIRECTIONAL); kfree(ibmvtpm); /* For tpm_ibmvtpm_get_desired_dma */ @@ -522,23 +520,12 @@ static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq, return; } ibmvtpm->rtce_size = be16_to_cpu(crq->len); - ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size, - GFP_ATOMIC); - if (!ibmvtpm->rtce_buf) { - dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n"); - return; - } - - ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev, - ibmvtpm->rtce_buf, ibmvtpm->rtce_size, - DMA_BIDIRECTIONAL); - - if (dma_mapping_error(ibmvtpm->dev, - ibmvtpm->rtce_dma_handle)) { - kfree(ibmvtpm->rtce_buf); - ibmvtpm->rtce_buf = NULL; - dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n"); - } + ibmvtpm->rtce_buf = dma_alloc_noncoherent(ibmvtpm->dev, + ibmvtpm->rtce_size, + &ibmvtpm->rtce_dma_handle, + DMA_BIDIRECTIONAL, GFP_ATOMIC); + if (!ibmvtpm->rtce_buf) + dev_err(ibmvtpm->dev, "Failed to dma allocate rtce buffer\n"); return; case VTPM_GET_VERSION_RES: @@ -618,22 +605,14 @@ static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev, ibmvtpm->vdev = vio_dev; crq_q = &ibmvtpm->crq_queue; - crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL); - if (!crq_q->crq_addr) { - dev_err(dev, "Unable to allocate memory for crq_addr\n"); - goto cleanup; - } crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr); init_waitqueue_head(&crq_q->wq); - ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr, - CRQ_RES_BUF_SIZE, - DMA_BIDIRECTIONAL); - - if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) { - dev_err(dev, "dma mapping failed\n"); + crq_q->crq_addr = dma_alloc_noncoherent(dev, CRQ_RES_BUF_SIZE, + &ibmvtpm->crq_dma_handle, + DMA_BIDIRECTIONAL, GFP_KERNEL); + if (!crq_q->crq_addr) goto cleanup; - } rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE); @@ -642,7 +621,7 @@ static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev, if (rc) { dev_err(dev, "Unable to register CRQ rc=%d\n", rc); - goto reg_crq_cleanup; + goto cleanup; } rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0, @@ -704,13 +683,11 @@ static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev, do { rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address); } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1)); -reg_crq_cleanup: - dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE, - DMA_BIDIRECTIONAL); cleanup: if (ibmvtpm) { if (crq_q->crq_addr) - free_page((unsigned long)crq_q->crq_addr); + dma_free_noncoherent(dev, CRQ_RES_BUF_SIZE, crq_q->crq_addr, + crq_q->crq_dma_handle, DMA_BIDIRECTIONAL); kfree(ibmvtpm); } -- 2.25.1