From patchwork Wed Nov 7 10:28:57 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathyush K X-Patchwork-Id: 1710141 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 BCE383FC8F for ; Wed, 7 Nov 2012 11:39:30 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B97EBA08E9 for ; Wed, 7 Nov 2012 03:39:30 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mailout1.samsung.com (mailout1.samsung.com [203.254.224.24]) by gabe.freedesktop.org (Postfix) with ESMTP id 888089E787 for ; Wed, 7 Nov 2012 02:09:06 -0800 (PST) Received: from epcpsbgm2.samsung.com (epcpsbgm2 [203.254.230.27]) by mailout1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0MD400F8W473F7I0@mailout1.samsung.com> for dri-devel@lists.freedesktop.org; Wed, 07 Nov 2012 19:09:05 +0900 (KST) Received: from epcpsbgm2.samsung.com ( [172.20.52.122]) by epcpsbgm2.samsung.com (EPCPMTA) with SMTP id E4.A1.12699.1433A905; Wed, 07 Nov 2012 19:09:05 +0900 (KST) X-AuditID: cbfee61b-b7f616d00000319b-3f-509a33410f07 Received: from epmmp2 ( [203.254.227.17]) by epcpsbgm2.samsung.com (EPCPMTA) with SMTP id A4.A1.12699.0433A905; Wed, 07 Nov 2012 19:09:04 +0900 (KST) Received: from localhost.localdomain ([107.108.73.106]) by mmp2.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0MD4004XW45KF180@mmp2.samsung.com> for dri-devel@lists.freedesktop.org; Wed, 07 Nov 2012 19:09:04 +0900 (KST) From: Prathyush K To: dri-devel@lists.freedesktop.org Subject: [PATCH 1/3] scatterlist: add sg_clone_table function Date: Wed, 07 Nov 2012 15:58:57 +0530 Message-id: <1352284139-9497-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+NgFjrKLMWRmVeSWpSXmKPExsWyRsSkStfReFaAwZ2JXBZXvr5nc2D0uN99 nCmAMYrLJiU1J7MstUjfLoEro2ntEbaCDZIVe7quMTUwHhbpYuTkkBAwkbg3/xELhC0mceHe erYuRi4OIYGljBL3P95ghyk6s/MpO0RiOqPE0mvHmSGc1UwSfw9uZQKpYhPQlvg15y4riC0i oCzxd+IqRhCbWUBC4kj7WTYQW1jAVuLk724wm0VAVaL/0huwel4BN4m9DQtYIbYpSLQuO8QO USMg8W3yIaDzOIDishKbDoDtlRDYwSYx5cEqqLMlJQ6uuMEygVFwASPDKkbR1ILkguKk9Fwj veLE3OLSvHS95PzcTYzAsDr975n0DsZVDRaHGAU4GJV4eC1SZwYIsSaWFVfmHmKU4GBWEuFl 4J0VIMSbklhZlVqUH19UmpNafIjRB+iSicxSosn5wJDPK4k3NDYxNzU2tTQyMjM1xSGsJM7b 7JESICSQnliSmp2aWpBaBDOOiYNTqoFxc/xmy6X2dmc/rJXUKrzCa3Gy80zZvC9abNnH/1du EPSvNp2cZj7b/fEcw5aNRxmm3zCd8PG7xRuu3XVFZtOXc6yMOZj32FH1iNrBfQIH2G3KNP4s s3x669GVfz+//A350ZaVFWPHOilqL/eG2pfel5PzD4UWs65h4632lGh42KSvNFf5f+gWJZbi jERDLeai4kQA0T7zIFgCAAA= X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrCLMWRmVeSWpSXmKPExsVy+t9jQV0H41kBBsduWltc+fqezYHR4373 caYAxqgGRpuM1MSU1CKF1Lzk/JTMvHRbJe/geOd4UzMDQ11DSwtzJYW8xNxUWyUXnwBdt8wc oLFKCmWJOaVAoYDE4mIlfTtME0JD3HQtYBojdH1DguB6jAzQQMIaxoymtUfYCjZIVuzpusbU wHhYpIuRk0NCwETizM6n7BC2mMSFe+vZuhi5OIQEpjNKLL12nBnCWc0k8ffgViaQKjYBbYlf c+6ygtgiAsoSfyeuYgSxmQUkJI60n2UDsYUFbCVO/u4Gs1kEVCX6L70Bq+cVcJPY27CAFWKb gkTrskPsExi5FzAyrGIUTS1ILihOSs810itOzC0uzUvXS87P3cQIDtpn0jsYVzVYHGIU4GBU 4uG1SJ0ZIMSaWFZcmXuIUYKDWUmEl4F3VoAQb0piZVVqUX58UWlOavEhRh+g7ROZpUST84ER lVcSb2hsYm5qbGppYmFiZolDWEmct9kjJUBIID2xJDU7NbUgtQhmHBMHp1QD49TINdVvDktZ RAgv3p3exne/64z397eO07bPWXHv5t39739L1etwHEic+jUmYsm29ckehh3X3t6Usvyld+1H i90G+7JjbH8TktpurXjtHuCwM37SRrvC9ZbKPeXrWrnjNP0YY/ldO3+uaDqby6zurjTr2eEA Aa6ZzOLbPWXsetn725Omsi9VVGIpzkg01GIuKk4EAAblod2HAgAA X-CFilter-Loop: Reflected X-Mailman-Approved-At: Wed, 07 Nov 2012 03:37:27 -0800 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