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: 2851094 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id E7DBC9F271 for ; Thu, 29 Aug 2013 03:05:42 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DC1022033F for ; Thu, 29 Aug 2013 03:05:41 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id D8EAC20254 for ; Thu, 29 Aug 2013 03:05:40 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C5601E6396 for ; Wed, 28 Aug 2013 20:05:40 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-yh0-f74.google.com (mail-yh0-f74.google.com [209.85.213.74]) by gabe.freedesktop.org (Postfix) with ESMTP id 57335E643B for ; Wed, 28 Aug 2013 18:51:42 -0700 (PDT) Received: by mail-yh0-f74.google.com with SMTP id c41so729741yho.1 for ; Wed, 28 Aug 2013 18:51:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=oZc5VbgwBFILT7lXbXV9gFN9+xObKGa/ojTtTUqhgrQ=; b=je7XTAI99RRus0Hjmv8wQB38o7QEUlS6CYjneAMBqtZjSKnKE5WodkG1zIeeu5GptV ZbckNq/XPetBp35tgYXaUZO0X6uIoVSk4IK5dvMA/B946WQR90AvePPLTOYRofx80mpO K0emnPe6Onw+tM9o2mcZpQ7+9p4dQ8zHP8EUrlll+oUIAf9LBJO8yMlZoaZXQb6rVJOy eRzGEmQkVEnfLNgAHvhB3DkI2GN/qVX5AfC0davrETREArQK3k8SHvC81yLL/P2AyIxe AKGF2YcWxmzaNS4I4TrD7Ky40dyimxFCE1vVKtTU+qDO+AQrqWIOKd2LnWIMnW51ZM91 EYWg== 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=oZc5VbgwBFILT7lXbXV9gFN9+xObKGa/ojTtTUqhgrQ=; b=FhJmBz8I53ZdjcIAJn3BZZIYEYGSBEbnn+GeXnTx2YKj9w9zn5Mvo98aLIs4RlG7pE 2ykPGk1mvoY03OJy+hEMMGXNJk9h3QiH3q1aeH238SGUBTWrNmve9tx26MYywzSj1EwC h9FJfGQoFfyY7l9CdqjLPTsucsze/JpfveFQ3i+bubFxSOyvMtT52+yfxZZTubgjP2er 8jp0sMq55CfTHpBO25Yzc2Ss5qrW/fyL137CKACYXrOdRaxZXsRXoqlzUNdMXZyUAgZi NljdAIQgjGr/HsBzfT53LoB/uBViVk3EwOctWJ9Qv1sFFqN+cctxdSKrJNhu6yAIPW2H J9/g== X-Gm-Message-State: ALoCoQkCg6umZLCi5SXG13Z27LZLiQ7NxH2AeT+e+U0rmSeLOb6IE9N3TvMJIZdwl3NOyiNv1ydi4dG+EpHSS9afFmjfr4+j4TgSRecVfRTv08W4OkPJoh7ohGzxHpzBREWx8tRH+8dgHN1NXXoGm+1DYkteVHUNUAScHkmJDxeCZmXTG2KQa+wLLplckbF5dXakJ5TK7Ul1HZMMq12HxOOr9xH1e14EXg== 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 Subject: [RFC 4/4] video: adf: add memblock helper 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 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dri-devel-bounces+patchwork-dri-devel=patchwork.kernel.org@lists.freedesktop.org Errors-To: dri-devel-bounces+patchwork-dri-devel=patchwork.kernel.org@lists.freedesktop.org X-Spam-Status: No, score=-6.6 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable 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 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_ */