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: 158874 Delivered-To: patches@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp183497jaa; Wed, 20 Feb 2019 23:40:40 -0800 (PST) X-Received: by 2002:a62:560f:: with SMTP id k15mr38646812pfb.231.1550734840773; Wed, 20 Feb 2019 23:40:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550734840; cv=none; d=google.com; s=arc-20160816; b=bFvtBceBJTCFVCN8V33ubD6scA6zJF4Hm8OAjHYBeIYaHTAo3hsBOXz1pGZWDTIFkC BphdoWJZVEcLmohDBd3lTb/kfF2YGrWf2VPFXg2VHgBzzvYeEnmLBvrkCo2fnxkd5Ioh tb1pAeirtOFXyP91w7ydmI2nXlr3DfL5CcZ7C6LoQG9QkJXa0ZcYW64Xob+nSHxsHM/r sbX/+pJdY+DcM3GO9DKmHY/ugYIvlKDphlfMX3mYt20XqQl0sTKUtDa2dxAfG3tw35st RTtffarhdqAZ6VeMXydrIvH6zG9U06hqusQRWEvAK+prbs4fQWpl5g16XQxfa+W3N+LE yQ5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=DKgUdz/J1rmM8dQ93ENbc8BkFgGujdvCLx05VHfoMYA=; b=XSrpjhjLDrGyRzNRdg1+AXnAoLOTG6LqdSpuea8tj/Vc2NFL0rsdlUhNtmslk+ZKeu UiruTCqH2tUHLmN+Z90jho6QNBnoQ7tAcWu+vDNpzn7Uo/7EjuAHhRhi6YdpHMyKZUtT MSeqGTCAqu9lEOjpaYCvWFEX4hV9kSpYlCPQz7l3LBzeDoJUVCqWgMs5apVPMO0mCY4g 08QZkEp1QJTemzF1ntPRWouhUffVNVPvxD2Y6z0KWL2rEuMAk8GpHW7ppP/wRdNZoBjs 0ZQoulEKrpo7Dksht8dX9kAy0JNAYrYPvX33aHH9NGxJpNFxlU0RRoWfGPDbMWuhCrqS jO2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=njrOiJQQ; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id i6sor18142807pfa.30.2019.02.20.23.40.40 for (Google Transport Security); Wed, 20 Feb 2019 23:40:40 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=njrOiJQQ; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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-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=RaeJGmofzWzRm9esldvVd1lrLKV2+SaFwm/A/qEouc+VTsI/jgt6EbeqK9+74DnMjl 3TbfTxiJEV+x3k/8N8XWLH9C9cJNlbH+O33XEss6MhRfO59q0axlRITmxtxhydwlDlTl OHqaynLEQjiH4BL2FSuJ96aIElXhNBMztXhaQCqx/id/BgonDfpxxGIDwa59GVSX2AyZ P2gbm3CM5Gr9CZQRpZl57T/bA7AwP1k/4MRAdoLfw10FqQptmboEyjQCT+rs2sszg3Ki rNQ1+A8rOdBAO2Quit62uK7dCIxEOTF7n5rKMEX90rWozCHlAGoByIm0vZOoB8CDRcDx SfKQ== X-Gm-Message-State: AHQUAuZ3b2Z4X4/aVVq11FI6n+zl+5/LaPwHeygodazPc+iecI17KWz1 ZBn34h44QpqZY+8KX6axveLStDet 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) Return-Path: 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 Cc: John Stultz , Benjamin Gaignard , Sumit Semwal , Liam Mark , Brian Starkey , "Andrew F . Davis" , Chenbo Feng , Alistair Strachan , dri-devel@lists.freedesktop.org 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> 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 -- 2.7.4 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);