From patchwork Thu Aug 29 01:51:21 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Hackmann X-Patchwork-Id: 19601 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qe0-f72.google.com (mail-qe0-f72.google.com [209.85.128.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 88727248D3 for ; Thu, 29 Aug 2013 01:51:55 +0000 (UTC) Received: by mail-qe0-f72.google.com with SMTP id b4sf7860481qen.3 for ; Wed, 28 Aug 2013 18:51:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=delivered-to:from:to:date:message-id:in-reply-to:references:cc :subject:precedence:list-id:list-unsubscribe:list-archive:list-post :list-help:list-subscribe:mime-version:errors-to:sender :x-original-sender:x-original-authentication-results:mailing-list :content-type:content-transfer-encoding; bh=0UQggheRkYYCayt2Vw5/NDjU4Anx9BepFuBcZ76apCc=; b=UL67aImnSBL/rMNZzU3f8Fdr25CPC72UfK0SN6NPgWgkC+R/5pwGCJP4E3uMxTjpgV iDaFwWxN2z5zr+ofN0oIg/WjlId5mUsmPPn0neTmwIsE/RFmZJ/XryYfYHJTARoB1caa UP0w6Zn5XI0bd7z6bwlWt38a+HxyhibTvgdf0Mf/uwMRSdxacM4EhArxt3O1hPwCoapS w1ND1TlTLPAQ42Gp9O4M/L8vh4O+SzRpvWcc1cGM4vPRw7+zofNqRxfBFn/vGynRcBXW xMasBgMwUs1ImGNBafeOXO4wF8mpcGiOArVnY/56i8AGFixktHc6rpW4UlgbekvTPQh+ o7/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:cc:subject:precedence:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version :errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list:content-type :content-transfer-encoding; bh=0UQggheRkYYCayt2Vw5/NDjU4Anx9BepFuBcZ76apCc=; b=B778SzcxmxD1rlS0/zb1lpu1DzQXpXeraq358syHReAOEFZhyWrfAddwM4TiPhoFvk r3ZppovNLlK0MBJt5VBObD6ZwB2Olk7MeuZvqUQMClznMNbaXP48keQ6Tt7czoTZzuLJ mJ8NxuNnunip+Ybt/Zd5g22WPhtq6Lzxz9tzeTRORybmx38ZY1jXtreVAKMIXVBAvMsr wYhIn8CMGyOnWmwQbnLABY/NmbPEAg2V2BXySnNPa7S8bO/7mb+f/WzEKsKqW0msH5fF FdU0o3z84KaJitmHBpPE+9ZylOQ6UJ6JjOc/EdywPQAtZbICHdHmTvKsMMWfp5bPTA2n FBvQ== X-Received: by 10.236.69.35 with SMTP id m23mr358089yhd.6.1377741115334; Wed, 28 Aug 2013 18:51:55 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.85.134 with SMTP id h6ls645676qez.10.gmail; Wed, 28 Aug 2013 18:51:55 -0700 (PDT) X-Received: by 10.58.73.202 with SMTP id n10mr663482vev.7.1377741115215; Wed, 28 Aug 2013 18:51:55 -0700 (PDT) Received: from mail-vb0-x232.google.com (mail-vb0-x232.google.com [2607:f8b0:400c:c02::232]) by mx.google.com with ESMTPS id cz7si393161veb.109.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 28 Aug 2013 18:51:55 -0700 (PDT) Received-SPF: neutral (google.com: 2607:f8b0:400c:c02::232 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=2607:f8b0:400c:c02::232; Received: by mail-vb0-f50.google.com with SMTP id x14so4817711vbb.9 for ; Wed, 28 Aug 2013 18:51:55 -0700 (PDT) X-Gm-Message-State: ALoCoQk094vqcD0962IBm70cZfVWev6wODWED/CienvD6+1FZEFaSyjraX4uc+OQ8Y0kDWeiFnF/ X-Received: by 10.221.27.73 with SMTP id rp9mr515064vcb.29.1377741115101; Wed, 28 Aug 2013 18:51:55 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp389555vcz; Wed, 28 Aug 2013 18:51:54 -0700 (PDT) X-Received: by 10.14.241.74 with SMTP id f50mr1015302eer.29.1377741113166; Wed, 28 Aug 2013 18:51:53 -0700 (PDT) Received: from ip-10-141-164-156.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id u46si1000072eeg.46.1969.12.31.16.00.00 (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 28 Aug 2013 18:51:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-141-164-156.ec2.internal) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1VErO1-0003WV-HC; Thu, 29 Aug 2013 01:51:01 +0000 Received: from mail-qc0-f202.google.com ([209.85.216.202]) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1VErNz-0003VW-Jp for linaro-mm-sig@lists.linaro.org; Thu, 29 Aug 2013 01:50:59 +0000 Received: by mail-qc0-f202.google.com with SMTP id l13so728026qcy.3 for ; Wed, 28 Aug 2013 18:51:40 -0700 (PDT) X-Received: by 10.236.17.161 with SMTP id j21mr165132yhj.55.1377741100736; Wed, 28 Aug 2013 18:51:40 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id z45si1785528yha.7.1969.12.31.16.00.00 (version=TLSv1.1 cipher=AES128-SHA bits=128/128); Wed, 28 Aug 2013 18:51:40 -0700 (PDT) Received: from ghackmann.mtv.corp.google.com (ghackmann.mtv.corp.google.com [172.18.120.106]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id 848AA31C1BD; Wed, 28 Aug 2013 18:51:40 -0700 (PDT) Received: by ghackmann.mtv.corp.google.com (Postfix, from userid 163046) id 46A8B1005DB; Wed, 28 Aug 2013 18:51:40 -0700 (PDT) From: Greg Hackmann To: dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org Date: Wed, 28 Aug 2013 18:51:21 -0700 Message-Id: <1377741081-30189-5-git-send-email-ghackmann@google.com> X-Mailer: git-send-email 1.8.0 In-Reply-To: <1377741081-30189-1-git-send-email-ghackmann@google.com> References: <1377741081-30189-1-git-send-email-ghackmann@google.com> Cc: konkers@google.com Subject: [Linaro-mm-sig] [RFC 4/4] video: adf: add memblock helper X-BeenThere: linaro-mm-sig@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: linaro-mm-sig-bounces@lists.linaro.org Sender: linaro-mm-sig-bounces@lists.linaro.org X-Original-Sender: ghackmann@google.com X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 2607:f8b0:400c:c02::232 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=neutral (bad format) header.i=@google.com; dmarc=fail (p=REJECT dis=NONE) d=google.com Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Provides a dma-buf exporter for memblocks, mainly useful for ADF devices to wrap their bootloader logos Signed-off-by: Greg Hackmann --- drivers/video/adf/Kconfig | 5 ++ drivers/video/adf/Makefile | 2 + drivers/video/adf/adf_memblock.c | 150 +++++++++++++++++++++++++++++++++++++++ include/video/adf_memblock.h | 20 ++++++ 4 files changed, 177 insertions(+) create mode 100644 drivers/video/adf/adf_memblock.c create mode 100644 include/video/adf_memblock.h diff --git a/drivers/video/adf/Kconfig b/drivers/video/adf/Kconfig index 30b0611..ad0c0eb 100644 --- a/drivers/video/adf/Kconfig +++ b/drivers/video/adf/Kconfig @@ -8,3 +8,8 @@ menuconfig ADF_DISPLAY_CORE depends on ADF depends on DISPLAY_CORE tristate "Helper for implementing ADF interface ops with Display Core devices" + +menuconfig ADF_MEMBLOCK + depends on ADF + depends on HAVE_MEMBLOCK + tristate "Helper for using memblocks as buffers in ADF drivers" diff --git a/drivers/video/adf/Makefile b/drivers/video/adf/Makefile index 30164ee..97f9c98 100644 --- a/drivers/video/adf/Makefile +++ b/drivers/video/adf/Makefile @@ -10,3 +10,5 @@ obj-$(CONFIG_ADF) += adf.o \ obj-$(CONFIG_COMPAT) += adf_fops32.o obj-$(CONFIG_ADF_DISPLAY_CORE) += adf_display.o + +obj-$(CONFIG_ADF_MEMBLOCK) += adf_memblock.o diff --git a/drivers/video/adf/adf_memblock.c b/drivers/video/adf/adf_memblock.c new file mode 100644 index 0000000..a1b7ec6 --- /dev/null +++ b/drivers/video/adf/adf_memblock.c @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2013 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include +#include +#include +#include + +struct adf_memblock_pdata { + phys_addr_t base; +}; + +struct sg_table *adf_memblock_map(struct dma_buf_attachment *attach, + enum dma_data_direction direction) +{ + struct adf_memblock_pdata *pdata = attach->dmabuf->priv; + unsigned long pfn = (pdata->base >> PAGE_SHIFT); + struct page *page = pfn_to_page(pfn); + struct sg_table *table; + int ret; + + table = kzalloc(sizeof(*table), GFP_KERNEL); + if (!table) + return ERR_PTR(-ENOMEM); + + ret = sg_alloc_table(table, 1, GFP_KERNEL); + if (ret < 0) + goto err; + + sg_set_page(table->sgl, page, attach->dmabuf->size, 0); + return table; + +err: + kfree(table); + return ERR_PTR(ret); +} + +void adf_memblock_unmap(struct dma_buf_attachment *attach, + struct sg_table *table, enum dma_data_direction direction) +{ + sg_free_table(table); +} + +static void __init_memblock adf_memblock_release(struct dma_buf *buf) +{ + struct adf_memblock_pdata *pdata = buf->priv; + int err = memblock_free(pdata->base, buf->size); + + if (err < 0) + pr_warn("%s: freeing memblock failed: %d\n", __func__, err); + kfree(pdata); +} + +static void *adf_memblock_do_kmap(struct dma_buf *buf, unsigned long pgoffset, + bool atomic) +{ + struct adf_memblock_pdata *pdata = buf->priv; + unsigned long pfn = (pdata->base >> PAGE_SHIFT) + pgoffset; + struct page *page = pfn_to_page(pfn); + + if (atomic) + return kmap_atomic(page); + else + return kmap(page); +} + +static void *adf_memblock_kmap_atomic(struct dma_buf *buf, + unsigned long pgoffset) +{ + return adf_memblock_do_kmap(buf, pgoffset, true); +} + +static void adf_memblock_kunmap_atomic(struct dma_buf *buf, + unsigned long pgoffset, void *vaddr) +{ + kunmap_atomic(vaddr); +} + +static void *adf_memblock_kmap(struct dma_buf *buf, unsigned long pgoffset) +{ + return adf_memblock_do_kmap(buf, pgoffset, false); +} + +static void adf_memblock_kunmap(struct dma_buf *buf, unsigned long pgoffset, + void *vaddr) +{ + kunmap(vaddr); +} + +static int adf_memblock_mmap(struct dma_buf *buf, struct vm_area_struct *vma) +{ + struct adf_memblock_pdata *pdata = buf->priv; + unsigned long pfn = pdata->base >> PAGE_SHIFT; + + return remap_pfn_range(vma, vma->vm_start, pfn, + vma->vm_end - vma->vm_start, vma->vm_page_prot); +} + +struct dma_buf_ops adf_memblock_ops = { + .map_dma_buf = adf_memblock_map, + .unmap_dma_buf = adf_memblock_unmap, + .release = adf_memblock_release, + .kmap_atomic = adf_memblock_kmap_atomic, + .kunmap_atomic = adf_memblock_kunmap_atomic, + .kmap = adf_memblock_kmap, + .kunmap = adf_memblock_kunmap, + .mmap = adf_memblock_mmap, +}; + +/** + * adf_memblock_export - export a memblock reserved area as a dma-buf + * + * @base: base physical address + * @size: memblock size + * @flags: mode flags for the dma-buf's file + * + * @base and @size must be page-aligned. + * + * Returns a dma-buf on success or ERR_PTR(-errno) on failure. + */ +struct dma_buf *adf_memblock_export(phys_addr_t base, size_t size, int flags) +{ + struct adf_memblock_pdata *pdata; + struct dma_buf *buf; + + if (PAGE_ALIGN(base) != base || PAGE_ALIGN(size) != size) + return ERR_PTR(-EINVAL); + + pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); + if (!pdata) + return ERR_PTR(-ENOMEM); + + pdata->base = base; + buf = dma_buf_export(pdata, &adf_memblock_ops, size, flags); + if (IS_ERR(buf)) + kfree(pdata); + + return buf; +} diff --git a/include/video/adf_memblock.h b/include/video/adf_memblock.h new file mode 100644 index 0000000..6256e0e --- /dev/null +++ b/include/video/adf_memblock.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2013 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#ifndef _VIDEO_ADF_MEMBLOCK_H_ +#define _VIDEO_ADF_MEMBLOCK_H_ + +struct dma_buf *adf_memblock_export(phys_addr_t base, size_t size, int flags); + +#endif /* _VIDEO_ADF_MEMBLOCK_H_ */