From patchwork Thu Feb 18 15:05:16 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 8351401 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id BF6E59FC55 for ; Thu, 18 Feb 2016 15:06:39 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9E6F820125 for ; Thu, 18 Feb 2016 15:06:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 68F77203AB for ; Thu, 18 Feb 2016 15:06:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1946611AbcBRPGe (ORCPT ); Thu, 18 Feb 2016 10:06:34 -0500 Received: from mail-wm0-f48.google.com ([74.125.82.48]:36663 "EHLO mail-wm0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1946585AbcBRPGc (ORCPT ); Thu, 18 Feb 2016 10:06:32 -0500 Received: by mail-wm0-f48.google.com with SMTP id g62so32400859wme.1 for ; Thu, 18 Feb 2016 07:06:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=aC07E8ZW0DwbXYj4BF8dpJxIuQH5P0oV59R+6HHzPMM=; b=kiXm4SmLBZig9nm/kv9pC8GAOiVw1xRT6mJFU4JrQArMnmxNWLwkKRqCVEj4dHt5/j q8IAWTzlhw20F2XTSUVxD9rkqEYGtpZJt0uAIxNaAk1SfSovYgiySEYKnofa6wd91Z2M 9xUUee22XAwoj7YYm/HV3D5XKywpS1ZdzpqLA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=aC07E8ZW0DwbXYj4BF8dpJxIuQH5P0oV59R+6HHzPMM=; b=L0dGCnSauSL/6M4kPqxnH2cO+cNFkmh+nQtkbVge0YsE0kzakJNJkQSlehGhlGmsaF 3W5EcuIYZ2GKurzA+UILAISR2gnv3VsAyrcdhLGkc7i30LdfdPCy8+fMc+YNZZxjFVSR nQqJiuor7F0MVa9mQqoE0Y+o1CLQpDU11Vm2Xg8DlbseWtuKNWyg98YQOC1IYINPxpC7 4Vt99pDKeJ+a+S0pncH44BSqp3f9idjkMoG5pRNnj46grvLRk164VxXXMospvbQrlUBO w7JwBPLhHKUQnN4V4+ULBar8/UR6jYont0BFVQQlqu61Pafyib5ouyOYfPqM9FSQWfJ0 TSEg== X-Gm-Message-State: AG10YOR/4J0aSU0Qq4zcU6BdFJPCp6lAEmTUZePImujhMl883/nF4TNrn5F+1uHuiKurC7JT X-Received: by 10.28.24.80 with SMTP id 77mr3779720wmy.16.1455807990740; Thu, 18 Feb 2016 07:06:30 -0800 (PST) Received: from lmenx321.st.com. ([80.12.39.4]) by smtp.gmail.com with ESMTPSA id z127sm3416247wme.5.2016.02.18.07.06.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 18 Feb 2016 07:06:29 -0800 (PST) From: Benjamin Gaignard To: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-security-module@vger.kernel.org, laurent.pinchart@ideasonboard.com, zoltan.kuscsik@linaro.org, sumit.semwal@linaro.org, cc.ma@mediatek.com Cc: Benjamin Gaignard Subject: [PATCH v6 2/3] SMAF: add CMA allocator Date: Thu, 18 Feb 2016 16:05:16 +0100 Message-Id: <1455807917-19901-3-git-send-email-benjamin.gaignard@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1455807917-19901-1-git-send-email-benjamin.gaignard@linaro.org> References: <1455807917-19901-1-git-send-email-benjamin.gaignard@linaro.org> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP SMAF CMA allocator implement helpers functions to allow SMAF to allocate contiguous memory. match() each if at least one of the attached devices have coherent_dma_mask set to DMA_BIT_MASK(32). For allocation it use dma_alloc_attrs() with DMA_ATTR_WRITE_COMBINE and not dma_alloc_writecombine to be compatible with ARM 64bits architecture Signed-off-by: Benjamin Gaignard --- drivers/smaf/Kconfig | 6 ++ drivers/smaf/Makefile | 1 + drivers/smaf/smaf-cma.c | 199 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 drivers/smaf/smaf-cma.c diff --git a/drivers/smaf/Kconfig b/drivers/smaf/Kconfig index d36651a..058ec4c 100644 --- a/drivers/smaf/Kconfig +++ b/drivers/smaf/Kconfig @@ -3,3 +3,9 @@ config SMAF depends on DMA_SHARED_BUFFER help Choose this option to enable Secure Memory Allocation Framework + +config SMAF_CMA + tristate "SMAF CMA allocator" + depends on SMAF && HAVE_DMA_ATTRS + help + Choose this option to enable CMA allocation within SMAF diff --git a/drivers/smaf/Makefile b/drivers/smaf/Makefile index 40cd882..05bab01b 100644 --- a/drivers/smaf/Makefile +++ b/drivers/smaf/Makefile @@ -1 +1,2 @@ obj-$(CONFIG_SMAF) += smaf-core.o +obj-$(CONFIG_SMAF_CMA) += smaf-cma.o diff --git a/drivers/smaf/smaf-cma.c b/drivers/smaf/smaf-cma.c new file mode 100644 index 0000000..6366777 --- /dev/null +++ b/drivers/smaf/smaf-cma.c @@ -0,0 +1,199 @@ +/* + * smaf-cma.c + * + * Copyright (C) Linaro SA 2015 + * Author: Benjamin Gaignard for Linaro. + * License terms: GNU General Public License (GPL), version 2 + */ + +#include +#include +#include +#include + +struct smaf_cma_buffer_info { + struct device *dev; + size_t size; + void *vaddr; + dma_addr_t paddr; +}; + +/** + * find_matching_device - iterate over the attached devices to find one + * with coherent_dma_mask correctly set to DMA_BIT_MASK(32). + * Matching device (if any) will be used to aim CMA area. + */ +static struct device *find_matching_device(struct dma_buf *dmabuf) +{ + struct dma_buf_attachment *attach_obj; + + list_for_each_entry(attach_obj, &dmabuf->attachments, node) { + if (attach_obj->dev->coherent_dma_mask == DMA_BIT_MASK(32)) + return attach_obj->dev; + } + + return NULL; +} + +/** + * smaf_cma_match - return true if at least one device has been found + */ +static bool smaf_cma_match(struct dma_buf *dmabuf) +{ + return !!find_matching_device(dmabuf); +} + +static void smaf_cma_release(struct dma_buf *dmabuf) +{ + struct smaf_cma_buffer_info *info = dmabuf->priv; + DEFINE_DMA_ATTRS(attrs); + + dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs); + + dma_free_attrs(info->dev, info->size, info->vaddr, info->paddr, &attrs); + + kfree(info); +} + +static struct sg_table *smaf_cma_map(struct dma_buf_attachment *attachment, + enum dma_data_direction direction) +{ + struct smaf_cma_buffer_info *info = attachment->dmabuf->priv; + struct sg_table *sgt; + int ret; + + sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); + if (!sgt) + return NULL; + + ret = dma_get_sgtable(info->dev, sgt, info->vaddr, + info->paddr, info->size); + if (ret < 0) + goto out; + + sg_dma_address(sgt->sgl) = info->paddr; + return sgt; + +out: + kfree(sgt); + return NULL; +} + +static void smaf_cma_unmap(struct dma_buf_attachment *attachment, + struct sg_table *sgt, + enum dma_data_direction direction) +{ + /* do nothing */ +} + +static int smaf_cma_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) +{ + struct smaf_cma_buffer_info *info = dmabuf->priv; + int ret; + DEFINE_DMA_ATTRS(attrs); + + dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs); + + if (info->size < vma->vm_end - vma->vm_start) + return -EINVAL; + + vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; + ret = dma_mmap_attrs(info->dev, vma, info->vaddr, info->paddr, + info->size, &attrs); + + return ret; +} + +static void *smaf_cma_vmap(struct dma_buf *dmabuf) +{ + struct smaf_cma_buffer_info *info = dmabuf->priv; + + return info->vaddr; +} + +static void *smaf_kmap_atomic(struct dma_buf *dmabuf, unsigned long offset) +{ + struct smaf_cma_buffer_info *info = dmabuf->priv; + + return (void *)info->vaddr + offset; +} + +static struct dma_buf_ops smaf_cma_ops = { + .map_dma_buf = smaf_cma_map, + .unmap_dma_buf = smaf_cma_unmap, + .mmap = smaf_cma_mmap, + .release = smaf_cma_release, + .kmap_atomic = smaf_kmap_atomic, + .kmap = smaf_kmap_atomic, + .vmap = smaf_cma_vmap, +}; + +static struct dma_buf *smaf_cma_allocate(struct dma_buf *dmabuf, + size_t length, unsigned int flags) +{ + struct dma_buf_attachment *attach_obj; + struct smaf_cma_buffer_info *info; + struct dma_buf *cma_dmabuf; + int ret; + + DEFINE_DMA_BUF_EXPORT_INFO(export); + DEFINE_DMA_ATTRS(attrs); + + dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs); + + info = kzalloc(sizeof(*info), GFP_KERNEL); + if (!info) + return NULL; + + info->dev = find_matching_device(dmabuf); + info->size = length; + info->vaddr = dma_alloc_attrs(info->dev, info->size, &info->paddr, + GFP_KERNEL | __GFP_NOWARN, &attrs); + if (!info->vaddr) { + ret = -ENOMEM; + goto error; + } + + export.ops = &smaf_cma_ops; + export.size = info->size; + export.flags = flags; + export.priv = info; + + cma_dmabuf = dma_buf_export(&export); + if (IS_ERR(cma_dmabuf)) + goto error; + + list_for_each_entry(attach_obj, &dmabuf->attachments, node) { + dma_buf_attach(cma_dmabuf, attach_obj->dev); + } + + return cma_dmabuf; + +error: + kfree(info); + return NULL; +} + +static struct smaf_allocator smaf_cma = { + .match = smaf_cma_match, + .allocate = smaf_cma_allocate, + .name = "smaf-cma", + .ranking = 0, +}; + +static int __init smaf_cma_init(void) +{ + INIT_LIST_HEAD(&smaf_cma.list_node); + return smaf_register_allocator(&smaf_cma); +} +module_init(smaf_cma_init); + +static void __exit smaf_cma_deinit(void) +{ + smaf_unregister_allocator(&smaf_cma); +} +module_exit(smaf_cma_deinit); + +MODULE_DESCRIPTION("SMAF CMA module"); +MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR("Benjamin Gaignard ");