From patchwork Thu Feb 21 07:40:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 10823259 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CFCD01390 for ; Thu, 21 Feb 2019 07:40:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BFEFE2EE2A for ; Thu, 21 Feb 2019 07:40:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B3F302EE28; Thu, 21 Feb 2019 07:40:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1EBBD2EE28 for ; Thu, 21 Feb 2019 07:40:47 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0947889381; Thu, 21 Feb 2019 07:40:43 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by gabe.freedesktop.org (Postfix) with ESMTPS id 106508937C for ; Thu, 21 Feb 2019 07:40:41 +0000 (UTC) Received: by mail-pg1-x541.google.com with SMTP id q206so13371822pgq.4 for ; Wed, 20 Feb 2019 23:40:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=DKgUdz/J1rmM8dQ93ENbc8BkFgGujdvCLx05VHfoMYA=; b=Sj/x2EJ7WmgTE1gBs6aEujgoYoq+0uYiIdWwye+vvZQ+33oo/b4MaFlypDYo5CnGaI gImX34OK9Byuanq5H0iI53SJy7pnpIu93VtWJ5AXukzTYneN+IT0tuaJs/OrhOR1Y/QL I4n7ALvO0kfA+WYdB4BxvE+xxS+2B1bB8qj2RJrfVeHXLuwNBpe7o6doJH1y3SpY5iQp hUEG2MvxrubR6ijVRj05Ks9jbi3RU3XZmF/NGRsjpRLs7kRUG9UUm6k0HbdANaQi8Igo Yj5qX6vFk74XLjTOoXgqWfnZa5DqOC5kf9AD+dd3e9O63jREs1W/Se+sPRyGvNN4Nd/b gYCQ== X-Gm-Message-State: AHQUAuZcwraicipcbmr1F2RLduBrolIOT1sffMbPO3F4YwH16xfEc0gd 0MT2ReNt9KyN6oEYCwcrLNx+VA== X-Google-Smtp-Source: AHgI3IbeNDJ4zEHbia3wBqcvGvKLWAoDN4EHsj4U0T6kamRL971JDiS30zGk46+Mum03GKJVmm9eBA== X-Received: by 2002:a62:503:: with SMTP id 3mr38275144pff.176.1550734840365; Wed, 20 Feb 2019 23:40:40 -0800 (PST) Received: from localhost.localdomain ([2601:1c2:680:1319:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id g136sm35907237pfb.154.2019.02.20.23.40.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 20 Feb 2019 23:40:39 -0800 (PST) From: John Stultz To: Laura Abbott Subject: [EARLY RFC][PATCH 4/4] dma-buf: pools: Add CMA pool to dmabuf pools Date: Wed, 20 Feb 2019 23:40:30 -0800 Message-Id: <1550734830-23499-5-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1550734830-23499-1-git-send-email-john.stultz@linaro.org> References: <1550734830-23499-1-git-send-email-john.stultz@linaro.org> X-Mailman-Original-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=DKgUdz/J1rmM8dQ93ENbc8BkFgGujdvCLx05VHfoMYA=; b=njrOiJQQokGCyPXqt71mDK9pN8GSnoXIrEavDjhEs6vOqHSRzZ4F1YPelwnY/93qvq IyBsf5KDLBpos005/DcjtUvrEu/5K1SeyzG6g9MM+4CROUSdDMNKak0tlidG/YzL/PQe S3+nDjfgjpDTq3smGnkhA0wdnIo2f3PNn7ST0iOjP4lO01zIUn2zDsT3WpLk2vuOob7D lHHJp+gznzJ7j+T4I/9lkE+Kp1xJPWfYSKYRNignwzlBYVRiZOh8Ckh63aBAu/7J7ygj x5F/cCwiyw7Hx5trU/5McIra3OBF/Z6j3TqzeoUUHct0ncxPi6GFsixJTcB0Y27RnsnY J2Zw== X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Chenbo Feng , Alistair Strachan , Liam Mark , "Andrew F . Davis" , dri-devel@lists.freedesktop.org MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP This adds a CMA pool, which allows userspace to allocate a dma-buf of contiguous memory out of a CMA region. Cc: Laura Abbott Cc: Benjamin Gaignard Cc: Sumit Semwal Cc: Liam Mark Cc: Brian Starkey Cc: Andrew F. Davis Cc: Chenbo Feng Cc: Alistair Strachan Cc: dri-devel@lists.freedesktop.org Signed-off-by: John Stultz --- drivers/dma-buf/pools/Kconfig | 8 +++ drivers/dma-buf/pools/Makefile | 1 + drivers/dma-buf/pools/cma_pool.c | 143 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 drivers/dma-buf/pools/cma_pool.c diff --git a/drivers/dma-buf/pools/Kconfig b/drivers/dma-buf/pools/Kconfig index 787b2a6..e984304 100644 --- a/drivers/dma-buf/pools/Kconfig +++ b/drivers/dma-buf/pools/Kconfig @@ -15,3 +15,11 @@ config DMABUF_POOLS_SYSTEM help Choose this option to enable the system dmabuf pool. The system pool is backed by pages from the buddy allocator. If in doubt, say Y. + +config DMABUF_POOLS_CMA + bool "DMA-BUF CMA Pool" + depends on DMABUF_POOLS && DMA_CMA + help + Choose this option to enable dma-buf CMA pools. This pool is backed + by the Contiguous Memory Allocator (CMA). If your system has these + regions, you should say Y here. diff --git a/drivers/dma-buf/pools/Makefile b/drivers/dma-buf/pools/Makefile index 2ccf2a1..ac8aa28 100644 --- a/drivers/dma-buf/pools/Makefile +++ b/drivers/dma-buf/pools/Makefile @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_DMABUF_POOLS) += dmabuf-pools.o pool-ioctl.o pool-helpers.o page_pool.o obj-$(CONFIG_DMABUF_POOLS_SYSTEM) += system_pool.o +obj-$(CONFIG_DMABUF_POOLS_CMA) += cma_pool.o diff --git a/drivers/dma-buf/pools/cma_pool.c b/drivers/dma-buf/pools/cma_pool.c new file mode 100644 index 0000000..0bd783f --- /dev/null +++ b/drivers/dma-buf/pools/cma_pool.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * drivers/dma-buf/pools/cma_pool.c + * + * Copyright (C) 2012, 2019 Linaro Ltd. + * Author: for ST-Ericsson. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "dmabuf-pools.h" + +struct cma_pool { + struct dmabuf_pool pool; + struct cma *cma; +}; + +#define to_cma_pool(x) container_of(x, struct cma_pool, pool) + +/* dmabuf pool CMA operations functions */ +static int cma_pool_allocate(struct dmabuf_pool *pool, + struct dmabuf_pool_buffer *buffer, + unsigned long len, + unsigned long flags) +{ + struct cma_pool *cma_pool = to_cma_pool(pool); + struct sg_table *table; + struct page *pages; + unsigned long size = PAGE_ALIGN(len); + unsigned long nr_pages = size >> PAGE_SHIFT; + unsigned long align = get_order(size); + int ret; + + if (align > CONFIG_CMA_ALIGNMENT) + align = CONFIG_CMA_ALIGNMENT; + + pages = cma_alloc(cma_pool->cma, nr_pages, align, false); + if (!pages) + return -ENOMEM; + + if (PageHighMem(pages)) { + unsigned long nr_clear_pages = nr_pages; + struct page *page = pages; + + while (nr_clear_pages > 0) { + void *vaddr = kmap_atomic(page); + + memset(vaddr, 0, PAGE_SIZE); + kunmap_atomic(vaddr); + page++; + nr_clear_pages--; + } + } else { + memset(page_address(pages), 0, size); + } + + table = kmalloc(sizeof(*table), GFP_KERNEL); + if (!table) + goto err; + + ret = sg_alloc_table(table, 1, GFP_KERNEL); + if (ret) + goto free_mem; + + sg_set_page(table->sgl, pages, size, 0); + + buffer->priv_virt = pages; + buffer->sg_table = table; + return 0; + +free_mem: + kfree(table); +err: + cma_release(cma_pool->cma, pages, nr_pages); + return -ENOMEM; +} + +static void cma_pool_free(struct dmabuf_pool_buffer *buffer) +{ + struct cma_pool *cma_pool = to_cma_pool(buffer->pool); + struct page *pages = buffer->priv_virt; + unsigned long nr_pages = PAGE_ALIGN(buffer->size) >> PAGE_SHIFT; + + /* release memory */ + cma_release(cma_pool->cma, pages, nr_pages); + /* release sg table */ + sg_free_table(buffer->sg_table); + kfree(buffer->sg_table); +} + +static struct dmabuf_pool_ops cma_pool_ops = { + .allocate = cma_pool_allocate, + .free = cma_pool_free, + .map_user = dmabuf_pool_map_user, + .map_kernel = dmabuf_pool_map_kernel, + .unmap_kernel = dmabuf_pool_unmap_kernel, +}; + +static struct dmabuf_pool *__cma_pool_create(struct cma *cma) +{ + struct cma_pool *cma_pool; + + cma_pool = kzalloc(sizeof(*cma_pool), GFP_KERNEL); + + if (!cma_pool) + return ERR_PTR(-ENOMEM); + + cma_pool->pool.ops = &cma_pool_ops; + /* + * get device from private pools data, later it will be + * used to make the link with reserved CMA memory + */ + cma_pool->cma = cma; + return &cma_pool->pool; +} + +static int __add_cma_pools(struct cma *cma, void *data) +{ + struct dmabuf_pool *pool; + + pool = __cma_pool_create(cma); + if (IS_ERR(pool)) + return PTR_ERR(pool); + + pool->name = cma_get_name(cma); + + dmabuf_pool_add(pool); + + return 0; +} + +static int add_cma_pools(void) +{ + cma_for_each_area(__add_cma_pools, NULL); + return 0; +} +device_initcall(add_cma_pools);