From patchwork Thu Mar 2 21:44:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laura Abbott X-Patchwork-Id: 9601689 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 41B9460453 for ; Thu, 2 Mar 2017 21:50:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 300A6285EF for ; Thu, 2 Mar 2017 21:50:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 24B6A285FB; Thu, 2 Mar 2017 21:50:22 +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=-1.4 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_SORBS_SPAM autolearn=no version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D0153285EF for ; Thu, 2 Mar 2017 21:50:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=chwaINd52cr1aBdPzg62jtabVvBMzYTlG0ZSh7ZBTe4=; b=RrIkcyBQ0EDCTJsYKVKLeXjxgO 4qml9KWFe3al6h18BGLrDVijLE1MC9C1hGyXtz2fTuTqYJgOUQVjZ9Y2eUVmhVXvRv5hg8GIdtIfy 05O5IV0iIgU7GYOOybiZztUMbGNIdNRbsN1242VHPbp/sUsfTyFxNAwJoHq+gI9mCL4giT/Cmtitg A65sue0zsc6F90scgcQi3h4R/TwUf4aPGDF3H8F2XDTkTHw3Muo5MEs4uu0ywS0D/27tARPHjVauJ BaOJrStl/F58FVXmzcZmGGLvRRbVjCS15XjmVFXmk90qKeVsHKSCtt0FGtylqs2dUbuIff1LB/Wqj 0khCXG8g==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cjYc4-0004Ck-Vi; Thu, 02 Mar 2017 21:50:16 +0000 Received: from mail-qk0-f169.google.com ([209.85.220.169]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cjYXh-0007uV-TS for linux-arm-kernel@lists.infradead.org; Thu, 02 Mar 2017 21:45:50 +0000 Received: by mail-qk0-f169.google.com with SMTP id s186so146633053qkb.1 for ; Thu, 02 Mar 2017 13:45:25 -0800 (PST) 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:in-reply-to :references; bh=EzE3pK+wuKV/5EaQF42c3tB4S4OMwYF7Ou1YgZuSiiM=; b=a8UM+USkEdJlBM0njsi87Sfz7ezbOJRI6Y5ZnTsCfk9oLpeaEAmVEG+/3p+G6XLi7K z93Hs98ZZcgKhcSm22Xa2Upfb457e6W+4EMNeNud9kh7QEYRuoQSuQ7wiATJ6GzoFxSl Oc+kSwcotBzAN2vJHZ5jXj/ODAUv+W677vGpaUxauxn5Z+S/l4uAxZNRy8gCbz6/75w7 yMRIY2oKKkxMH1GXoPopaUv0EtRR/iIwGAajayDG7xoxp+16nKt5jrC3PIRYjJvFF8yB +1Qgr4RSw8yX5UCd1B4c7Yf+UTNNmrCn5MWA3LYQ8IyfUg7tYN8L+YTiiQf3bITr6pS+ wT4Q== X-Gm-Message-State: AMke39nybKm/6RT8kQQJ4Aw25oihZO+0bAG3aP+fClb8iZ+RpYOmq1B3at2ntuGjTwRNdP0W X-Received: by 10.55.189.130 with SMTP id n124mr19194470qkf.235.1488491124503; Thu, 02 Mar 2017 13:45:24 -0800 (PST) Received: from labbott-redhat-machine.redhat.com ([2601:602:9802:a8dc::5ce7]) by smtp.gmail.com with ESMTPSA id r10sm6261671qte.1.2017.03.02.13.45.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Mar 2017 13:45:23 -0800 (PST) From: Laura Abbott To: Sumit Semwal , Riley Andrews , arve@android.com Subject: [RFC PATCH 10/12] staging: android: ion: Use CMA APIs directly Date: Thu, 2 Mar 2017 13:44:42 -0800 Message-Id: <1488491084-17252-11-git-send-email-labbott@redhat.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488491084-17252-1-git-send-email-labbott@redhat.com> References: <1488491084-17252-1-git-send-email-labbott@redhat.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170302_134546_407466_ADBB324A X-CRM114-Status: GOOD ( 15.28 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: devel@driverdev.osuosl.org, romlem@google.com, Greg Kroah-Hartman , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-mm@kvack.org, Mark Brown , Benjamin Gaignard , Daniel Vetter , Laura Abbott , Brian Starkey , linux-arm-kernel@lists.infradead.org, linux-media@vger.kernel.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP When CMA was first introduced, its primary use was for DMA allocation and the only way to get CMA memory was to call dma_alloc_coherent. This put Ion in an awkward position since there was no device structure readily available and setting one up messed up the coherency model. These days, CMA can be allocated directly from the APIs. Switch to using this model to avoid needing a dummy device. This also avoids awkward caching questions. Signed-off-by: Laura Abbott --- drivers/staging/android/ion/ion_cma_heap.c | 97 ++++++++---------------------- 1 file changed, 26 insertions(+), 71 deletions(-) diff --git a/drivers/staging/android/ion/ion_cma_heap.c b/drivers/staging/android/ion/ion_cma_heap.c index d562fd7..6838825 100644 --- a/drivers/staging/android/ion/ion_cma_heap.c +++ b/drivers/staging/android/ion/ion_cma_heap.c @@ -19,24 +19,19 @@ #include #include #include -#include +#include +#include #include "ion.h" #include "ion_priv.h" struct ion_cma_heap { struct ion_heap heap; - struct device *dev; + struct cma *cma; }; #define to_cma_heap(x) container_of(x, struct ion_cma_heap, heap) -struct ion_cma_buffer_info { - void *cpu_addr; - dma_addr_t handle; - struct sg_table *table; -}; - /* ION CMA heap operations functions */ static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer, @@ -44,93 +39,53 @@ static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer, unsigned long flags) { struct ion_cma_heap *cma_heap = to_cma_heap(heap); - struct device *dev = cma_heap->dev; - struct ion_cma_buffer_info *info; - - dev_dbg(dev, "Request buffer allocation len %ld\n", len); - - if (buffer->flags & ION_FLAG_CACHED) - return -EINVAL; + struct sg_table *table; + struct page *pages; + int ret; - info = kzalloc(sizeof(*info), GFP_KERNEL); - if (!info) + pages = cma_alloc(cma_heap->cma, len, 0); + if (!pages) return -ENOMEM; - info->cpu_addr = dma_alloc_coherent(dev, len, &(info->handle), - GFP_HIGHUSER | __GFP_ZERO); - - if (!info->cpu_addr) { - dev_err(dev, "Fail to allocate buffer\n"); + table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); + if (!table) goto err; - } - info->table = kmalloc(sizeof(*info->table), GFP_KERNEL); - if (!info->table) + ret = sg_alloc_table(table, 1, GFP_KERNEL); + if (ret) goto free_mem; - if (dma_get_sgtable(dev, info->table, info->cpu_addr, info->handle, - len)) - goto free_table; - /* keep this for memory release */ - buffer->priv_virt = info; - buffer->sg_table = info->table; - dev_dbg(dev, "Allocate buffer %p\n", buffer); + sg_set_page(table->sgl, pages, len, 0); + + buffer->priv_virt = pages; + buffer->sg_table = table; return 0; -free_table: - kfree(info->table); free_mem: - dma_free_coherent(dev, len, info->cpu_addr, info->handle); + kfree(table); err: - kfree(info); + cma_release(cma_heap->cma, pages, buffer->size); return -ENOMEM; } static void ion_cma_free(struct ion_buffer *buffer) { struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap); - struct device *dev = cma_heap->dev; - struct ion_cma_buffer_info *info = buffer->priv_virt; + struct page *pages = buffer->priv_virt; - dev_dbg(dev, "Release buffer %p\n", buffer); /* release memory */ - dma_free_coherent(dev, buffer->size, info->cpu_addr, info->handle); + cma_release(cma_heap->cma, pages, buffer->size); /* release sg table */ - sg_free_table(info->table); - kfree(info->table); - kfree(info); -} - -static int ion_cma_mmap(struct ion_heap *mapper, struct ion_buffer *buffer, - struct vm_area_struct *vma) -{ - struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap); - struct device *dev = cma_heap->dev; - struct ion_cma_buffer_info *info = buffer->priv_virt; - - return dma_mmap_coherent(dev, vma, info->cpu_addr, info->handle, - buffer->size); -} - -static void *ion_cma_map_kernel(struct ion_heap *heap, - struct ion_buffer *buffer) -{ - struct ion_cma_buffer_info *info = buffer->priv_virt; - /* kernel memory mapping has been done at allocation time */ - return info->cpu_addr; -} - -static void ion_cma_unmap_kernel(struct ion_heap *heap, - struct ion_buffer *buffer) -{ + sg_free_table(buffer->sg_table); + kfree(buffer->sg_table); } static struct ion_heap_ops ion_cma_ops = { .allocate = ion_cma_allocate, .free = ion_cma_free, - .map_user = ion_cma_mmap, - .map_kernel = ion_cma_map_kernel, - .unmap_kernel = ion_cma_unmap_kernel, + .map_user = ion_heap_map_user, + .map_kernel = ion_heap_map_kernel, + .unmap_kernel = ion_heap_unmap_kernel, }; struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data) @@ -147,7 +102,7 @@ struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data) * get device from private heaps data, later it will be * used to make the link with reserved CMA memory */ - cma_heap->dev = data->priv; + cma_heap->cma = data->priv; cma_heap->heap.type = ION_HEAP_TYPE_DMA; return &cma_heap->heap; }