From patchwork Wed Nov 7 06:52:55 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathyush K X-Patchwork-Id: 1710131 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by patchwork1.kernel.org (Postfix) with ESMTP id 0F68F3FC8F for ; Wed, 7 Nov 2012 11:37:47 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9CA7BA08E0 for ; Wed, 7 Nov 2012 03:37:46 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mailout4.samsung.com (mailout4.samsung.com [203.254.224.34]) by gabe.freedesktop.org (Postfix) with ESMTP id 043449E760 for ; Tue, 6 Nov 2012 22:33:19 -0800 (PST) Received: from epcpsbgm2.samsung.com (epcpsbgm2 [203.254.230.27]) by mailout4.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0MD300HTLU7EC9K0@mailout4.samsung.com> for dri-devel@lists.freedesktop.org; Wed, 07 Nov 2012 15:33:18 +0900 (KST) Received: from epcpsbgm2.samsung.com ( [172.20.52.126]) by epcpsbgm2.samsung.com (EPCPMTA) with SMTP id 04.09.12699.DA00A905; Wed, 07 Nov 2012 15:33:18 +0900 (KST) X-AuditID: cbfee61b-b7f616d00000319b-66-509a00ad3fbf Received: from epmmp1.local.host ( [203.254.227.16]) by epcpsbgm2.samsung.com (EPCPMTA) with SMTP id C3.09.12699.DA00A905; Wed, 07 Nov 2012 15:33:17 +0900 (KST) Received: from localhost.localdomain ([107.108.73.106]) by mmp1.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0MD300H8EU5IHNA0@mmp1.samsung.com> for dri-devel@lists.freedesktop.org; Wed, 07 Nov 2012 15:33:17 +0900 (KST) From: Prathyush K To: dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org Subject: [PATCH] scatterlist: add sg_clone_table function Date: Wed, 07 Nov 2012 12:22:55 +0530 Message-id: <1352271175-1336-1-git-send-email-prathyush.k@samsung.com> X-Mailer: git-send-email 1.7.0.4 DLP-Filter: Pass X-MTR: 20000000000000000@CPGS X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrILMWRmVeSWpSXmKPExsWyRsSkTncdw6wAg1OPGC2ufH3P5sDocb/7 OFMAYxSXTUpqTmZZapG+XQJXRtPaI2wFGyQr9nRdY2pgPCzSxcjJISFgIjFx4RMWCFtM4sK9 9WxdjFwcQgJLGSW+79jJDFN0/NhPMFtIYBGjRN/7VIii1UwSS35MAkuwCWhL/JpzlxXEFhFw kJi28QcTiM0M1Hzx6GqgDRwcwgKWEmvfxoKEWQRUJfZMPQAW5hVwk/j2JwRilYJE67JD7BAl AhLfJh8CK5EQkJXYdADqmh1sEstfqUPYkhIHV9xgmcAouICRYRWjaGpBckFxUnqukV5xYm5x aV66XnJ+7iZGYDid/vdMegfjqgaLQ4wCHIxKPLwWqTMDhFgTy4orcw8xSnAwK4nw3v0HFOJN SaysSi3Kjy8qzUktPsToA3TIRGYp0eR8YKjnlcQbGpuYmxqbWhoZmZma4hBWEudt9kgJEBJI TyxJzU5NLUgtghnHxMEp1cA4MXLuve+vGpZL2UtkJv5QO7X/hd35Y7tPstpOPvfuxIVErozz S67fVhXwePIsRu3c1dPrn+4QXLnHrXpS6sOrO7+FLj5+7WdXfm1mbMweIwVOg3c5KV/z7vx4 MOtKYLBG4H/D0BNvuJdH3L1i7vHf783JUMH85WX/cz7Vy5nLvCjz43v1xcFusRJLcUaioRZz UXEiAG2CWr1UAgAA X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrILMWRmVeSWpSXmKPExsVy+t9jAd21DLMCDGacMbK48vU9mwOjx/3u 40wBjFENjDYZqYkpqUUKqXnJ+SmZeem2St7B8c7xpmYGhrqGlhbmSgp5ibmptkouPgG6bpk5 QGOVFMoSc0qBQgGJxcVK+naYJoSGuOlawDRG6PqGBMH1GBmggYQ1jBlNa4+wFWyQrNjTdY2p gfGwSBcjJ4eEgInE8WM/mSFsMYkL99azgdhCAosYJfrep3YxcgHZq5kklvyYBFbEJqAt8WvO XVYQW0TAQWLaxh9MIDYz0KCLR1ezdDFycAgLWEqsfRsLEmYRUJXYM/UAWJhXwE3i258QiFUK Eq3LDrFPYORewMiwilE0tSC5oDgpPddIrzgxt7g0L10vOT93EyM4XJ9J72Bc1WBxiFGAg1GJ h9cidWaAEGtiWXFl7iFGCQ5mJRHeu/+AQrwpiZVVqUX58UWlOanFhxh9gJZPZJYSTc4HxlJe SbyhsYm5qbGppYmFiZklDmElcd5mj5QAIYH0xJLU7NTUgtQimHFMHJxSDYwTnTtfOjF+ZThi Jl11KqCnzGrCsyz281cfMDifPS5/oC7JZPX73fx3fx3u/icnP3u5Rn+qrHlcx6fIj2+PHhMO eKScI/Je8z+HwKlo1l7G81YcfXLLFBwDEhMf7/7DbbH4bDnz/M/Nn5ynToh5Jv3sIptcyoOo QN5zr6Z5mF/Ytchk438Z3fdKLMUZiYZazEXFiQDwcdBYhAIAAA== X-CFilter-Loop: Reflected X-Mailman-Approved-At: Wed, 07 Nov 2012 03:37:27 -0800 Cc: t.stanislaws@samsung.com, daniel.vetter@ffwll.ch 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 This patch adds another constructor for an sg table. An sg table is created from an existing sg table. The new sg table is allocated and initialized with same data from the original sg table. The user has to call 'sg_clone_table' with the required sg table, the existing sg table and the gfp allocation mask. This function can be used in the dma-buf framework. If a buffer needs to be shared across multiple devices using the dma_buf framework, an sg table needs to be created and mapped to the target device's address space. This is done in most drivers by creating the sg table from the pages of the buffer (e.g. calling sg_alloc_table_from_pages function). In case this needs to be done frequently (e.g. a framebuffer is repeatedly shared with the GPU, video accelerator, CSC etc), it is efficient to create an sg table once during buffer allocation and then create temporary sg tables for dma mapping from the original sg table. Signed-off-by: Prathyush K --- include/linux/scatterlist.h | 1 + lib/scatterlist.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 0 deletions(-) diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index 4bd6c06..fd12525 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -215,6 +215,7 @@ void sg_free_table(struct sg_table *); int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int, gfp_t, sg_alloc_fn *); int sg_alloc_table(struct sg_table *, unsigned int, gfp_t); +int sg_clone_table(struct sg_table *, struct sg_table *, gfp_t); int sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages, unsigned int n_pages, unsigned long offset, unsigned long size, diff --git a/lib/scatterlist.c b/lib/scatterlist.c index 3675452..4f106b3 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -329,6 +329,42 @@ int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask) } EXPORT_SYMBOL(sg_alloc_table); +/* + * sg_clone_table - Allocate and initialize an sg table from an existing + * sg table + * @sgt_in: The sg table to clone + * @sgt_out: The output sg table cloned from the original sg table. + * @gfp_mask: GFP allocation mask + * + * Description: + * Allocate and initialize an sg table from an existing sg table. A user + * would want to create a temporary sg table which is a clone of an + * existing table. This cloned sg table is released by sg_free_table. + * + * Returns: + * 0 on success, negative error on failure + */ +int sg_clone_table(struct sg_table *sgt_in, struct sg_table *sgt_out, + gfp_t gfp_mask) +{ + struct scatterlist *s, *s_out; + unsigned int i; + int ret; + + ret = sg_alloc_table(sgt_out, sgt_in->orig_nents, gfp_mask); + if (unlikely(ret)) + return ret; + + s_out = sgt_out->sgl; + for_each_sg(sgt_in->sgl, s, sgt_in->orig_nents, i) { + sg_set_page(s_out, sg_page(s), s->length, s->offset); + s_out = sg_next(s_out); + } + + return 0; +} +EXPORT_SYMBOL(sg_clone_table); + /** * sg_alloc_table_from_pages - Allocate and initialize an sg table from * an array of pages