From patchwork Fri Oct 10 20:07:58 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sumit Semwal X-Patchwork-Id: 38637 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f70.google.com (mail-la0-f70.google.com [209.85.215.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 7B8A5202DB for ; Fri, 10 Oct 2014 20:09:09 +0000 (UTC) Received: by mail-la0-f70.google.com with SMTP id ge10sf2562495lab.1 for ; Fri, 10 Oct 2014 13:09:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:delivered-to:from:to:subject:date :message-id:in-reply-to:references:cc:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:list-subscribe :mime-version:errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list:content-type :content-transfer-encoding; bh=wNGyozR+Jk9os73fHkb+9f1LjXz5bHUusShKX3LjP2c=; b=essNq1vLGPSHKUtlnthzRJF6kNoKf8I+y1f/Deatk292hwNX2eWLhx9DVDXrApUxgR /ni5M/Y5T2rmAK0tJ+EYtsfsx5KufRPLgKVoPxiBdpPNrr+KAk4JFWB9X+qcPvolz8Je r3JJ3yTBtK5XVmSCX4/qsim4hrZHl6EFc6Q5/JbqHms3emLNIMrqQAfTNpnj1dSl2+cd HO185wg/oDVAZkp7ATdF/uhKOlIgxnExBRLo8eM5l7kCZLHtRJ8oYAyGYwveWlYtPoUS qqDHFdUljJQp1TDolgfsDfcRfgk0esbwwYVKKNML7BHXhZz9KzyXTJ64LJeC7j5morRM kxfg== X-Gm-Message-State: ALoCoQnSxwzDMo4dhdcDpJAeAMkv3X3nIxxk8gQgj2na7Q9vTvGlxg4o2wxDBf0nwxpJigOAUnRW X-Received: by 10.152.1.136 with SMTP id 8mr1385848lam.5.1412971748253; Fri, 10 Oct 2014 13:09:08 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.5.199 with SMTP id u7ls121986lau.10.gmail; Fri, 10 Oct 2014 13:09:08 -0700 (PDT) X-Received: by 10.152.4.194 with SMTP id m2mr7291143lam.63.1412971748098; Fri, 10 Oct 2014 13:09:08 -0700 (PDT) Received: from mail-la0-f44.google.com (mail-la0-f44.google.com [209.85.215.44]) by mx.google.com with ESMTPS id cq12si9669298lad.130.2014.10.10.13.09.07 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 10 Oct 2014 13:09:07 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.44 as permitted sender) client-ip=209.85.215.44; Received: by mail-la0-f44.google.com with SMTP id hs14so3954096lab.3 for ; Fri, 10 Oct 2014 13:09:07 -0700 (PDT) X-Received: by 10.112.138.202 with SMTP id qs10mr7081300lbb.66.1412971747668; Fri, 10 Oct 2014 13:09:07 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.84.229 with SMTP id c5csp492784lbz; Fri, 10 Oct 2014 13:09:06 -0700 (PDT) X-Received: by 10.68.94.34 with SMTP id cz2mr7531152pbb.7.1412971746028; Fri, 10 Oct 2014 13:09:06 -0700 (PDT) Received: from gabe.freedesktop.org (gabe.freedesktop.org. [131.252.210.177]) by mx.google.com with ESMTP id xw3si4711320pac.75.2014.10.10.13.09.05 for ; Fri, 10 Oct 2014 13:09:06 -0700 (PDT) Received-SPF: none (google.com: dri-devel-bounces@lists.freedesktop.org does not designate permitted sender hosts) client-ip=131.252.210.177; Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C15D46E44E; Fri, 10 Oct 2014 13:09:04 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pd0-f170.google.com (mail-pd0-f170.google.com [209.85.192.170]) by gabe.freedesktop.org (Postfix) with ESMTP id F22946E44E for ; Fri, 10 Oct 2014 13:09:03 -0700 (PDT) Received: by mail-pd0-f170.google.com with SMTP id p10so2266977pdj.1 for ; Fri, 10 Oct 2014 13:09:03 -0700 (PDT) X-Received: by 10.70.48.142 with SMTP id l14mr7616343pdn.29.1412971743861; Fri, 10 Oct 2014 13:09:03 -0700 (PDT) Received: from localhost.localdomain ([103.227.99.153]) by mx.google.com with ESMTPSA id ap5sm4365463pad.22.2014.10.10.13.09.01 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 10 Oct 2014 13:09:03 -0700 (PDT) From: Sumit Semwal To: linux-kernel@vger.kernel.org Subject: [RFC 4/4] cenalloc: a sample allocator for contiguous page allocation Date: Sat, 11 Oct 2014 01:37:58 +0530 Message-Id: <1412971678-4457-5-git-send-email-sumit.semwal@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1412971678-4457-1-git-send-email-sumit.semwal@linaro.org> References: <1412971678-4457-1-git-send-email-sumit.semwal@linaro.org> Cc: linaro-kernel@lists.linaro.org, Greg Kroah-Hartman , dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, Benjamin Gaignard , linux-media@vger.kernel.org X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: sumit.semwal@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.44 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 From: Benjamin Gaignard Signed-off-by: Benjamin Gaignard Signed-off-by: Sumit Semwal Cc: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman Cc: linux-media@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Cc: linaro-mm-sig@lists.linaro.org --- drivers/cenalloc/Makefile | 2 +- drivers/cenalloc/cenalloc_system_contig.c | 225 ++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 drivers/cenalloc/cenalloc_system_contig.c diff --git a/drivers/cenalloc/Makefile b/drivers/cenalloc/Makefile index d36b531..2f69b61 100644 --- a/drivers/cenalloc/Makefile +++ b/drivers/cenalloc/Makefile @@ -1,3 +1,3 @@ # Makefile for the cenalloc helper -obj-y += cenalloc.o +obj-y += cenalloc.o cenalloc_system_contig.o diff --git a/drivers/cenalloc/cenalloc_system_contig.c b/drivers/cenalloc/cenalloc_system_contig.c new file mode 100644 index 0000000..ecf0c03 --- /dev/null +++ b/drivers/cenalloc/cenalloc_system_contig.c @@ -0,0 +1,225 @@ +/* + * central allocator using kmalloc + * + * Copyright(C) 2014 Linaro Limited. All rights reserved. + * Author: Benjamin Gaignard + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + * + * 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. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cenalloc_priv.h" + +static gfp_t low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN); + +void cenalloc_pages_sync_for_device(struct device *dev, struct page *page, + size_t size, enum dma_data_direction dir) +{ + struct scatterlist sg; + + sg_init_table(&sg, 1); + sg_set_page(&sg, page, size, 0); + /* + * This is not correct - sg_dma_address needs a dma_addr_t that is valid + * for the the targeted device, but this works on the currently targeted + * hardware. + */ + sg_dma_address(&sg) = page_to_phys(page); + dma_sync_sg_for_device(dev, &sg, 1, dir); +} + +static int cenalloc_system_contig_allocate(struct cenalloc_allocator *allocator, + struct cenalloc_buffer *buffer, unsigned long len, + unsigned long align, unsigned long flags) +{ + int order = get_order(len); + struct page *page; + struct sg_table *table; + unsigned long i; + int ret; + + if (align > (PAGE_SIZE << order)) + return -EINVAL; + + page = alloc_pages(low_order_gfp_flags, order); + if (!page) + return -ENOMEM; + + split_page(page, order); + + len = PAGE_ALIGN(len); + for (i = len >> PAGE_SHIFT; i < (1 << order); i++) + __free_page(page + i); + + table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); + if (!table) { + ret = -ENOMEM; + goto free_pages; + } + + ret = sg_alloc_table(table, 1, GFP_KERNEL); + if (ret) + goto free_table; + + sg_set_page(table->sgl, page, len, 0); + + buffer->sg_table = table; + + cenalloc_pages_sync_for_device(NULL, page, len, DMA_BIDIRECTIONAL); + + return 0; + +free_table: + kfree(table); +free_pages: + for (i = 0; i < len >> PAGE_SHIFT; i++) + __free_page(page + i); + + return ret; + +} + +static void cenalloc_system_contig_free(struct cenalloc_buffer *buffer) +{ + struct sg_table *table = buffer->sg_table; + struct page *page = sg_page(table->sgl); + unsigned long pages = PAGE_ALIGN(buffer->size) >> PAGE_SHIFT; + unsigned long i; + + for (i = 0; i < pages; i++) + __free_page(page + i); + sg_free_table(table); + kfree(table); +} + +static struct sg_table *cenalloc_system_contig_map_dma + (struct cenalloc_allocator *allocator, struct cenalloc_buffer *buffer) +{ + return buffer->sg_table; +} + +static void cenalloc_system_contig_unmap_dma + (struct cenalloc_allocator *allocator, struct cenalloc_buffer *buffer) +{ + +} + +static void *cenalloc_system_contig_map_kernel + (struct cenalloc_allocator *allocator, struct cenalloc_buffer *buffer) +{ + struct scatterlist *sg; + int i, j; + void *vaddr; + pgprot_t pgprot; + struct sg_table *table = buffer->sg_table; + int npages = PAGE_ALIGN(buffer->size) / PAGE_SIZE; + struct page **pages = vmalloc(sizeof(struct page *) * npages); + struct page **tmp = pages; + + if (!pages) + return NULL; + + pgprot = pgprot_writecombine(PAGE_KERNEL); + + for_each_sg(table->sgl, sg, table->nents, i) { + int npages_this_entry = PAGE_ALIGN(sg->length) / PAGE_SIZE; + struct page *page = sg_page(sg); + + BUG_ON(i >= npages); + for (j = 0; j < npages_this_entry; j++) + *(tmp++) = page++; + } + vaddr = vmap(pages, npages, VM_MAP, pgprot); + vfree(pages); + + if (vaddr == NULL) + return ERR_PTR(-ENOMEM); + + return vaddr; +} + +static void cenalloc_system_contig_unmap_kernel + (struct cenalloc_allocator *allocator, struct cenalloc_buffer *buffer) +{ + vunmap(buffer->vaddr); +} + +static int cenalloc_system_contig_map_user + (struct cenalloc_allocator *mapper, struct cenalloc_buffer *buffer, + struct vm_area_struct *vma) +{ + struct sg_table *table = buffer->sg_table; + unsigned long addr = vma->vm_start; + unsigned long offset = vma->vm_pgoff * PAGE_SIZE; + struct scatterlist *sg; + int i; + int ret; + + for_each_sg(table->sgl, sg, table->nents, i) { + struct page *page = sg_page(sg); + unsigned long remainder = vma->vm_end - addr; + unsigned long len = sg->length; + + if (offset >= sg->length) { + offset -= sg->length; + continue; + } else if (offset) { + page += offset / PAGE_SIZE; + len = sg->length - offset; + offset = 0; + } + len = min(len, remainder); + ret = remap_pfn_range(vma, addr, page_to_pfn(page), len, + vma->vm_page_prot); + if (ret) + return ret; + addr += len; + if (addr >= vma->vm_end) + return 0; + } + return 0; +} + +static void cenalloc_system_contig_sync_for_device + (struct cenalloc_allocator *allocator, struct cenalloc_buffer *buffer, + struct device *dev, enum dma_data_direction dir) +{ + /* TODO */ +} + +static struct cenalloc_allocator_ops system_contig_ops = { + .allocate = cenalloc_system_contig_allocate, + .free = cenalloc_system_contig_free, + .map_dma = cenalloc_system_contig_map_dma, + .unmap_dma = cenalloc_system_contig_unmap_dma, + .map_kernel = cenalloc_system_contig_map_kernel, + .unmap_kernel = cenalloc_system_contig_unmap_kernel, + .map_user = cenalloc_system_contig_map_user, + .sync_for_device = cenalloc_system_contig_sync_for_device, +}; + +struct cenalloc_allocator system_contig = { + .ops = &system_contig_ops, + .type = CENALLOC_ALLOCATOR_SYSTEM, + .id = CENALLOC_ALLOCATOR_SYSTEM, + .name = "system contiguous memory allocator", +};