From patchwork Sat May 25 00:19:44 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 17211 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ea0-f197.google.com (mail-ea0-f197.google.com [209.85.215.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 3256C2397B for ; Sat, 25 May 2013 00:21:21 +0000 (UTC) Received: by mail-ea0-f197.google.com with SMTP id z7sf3663516eaf.4 for ; Fri, 24 May 2013 17:20:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-beenthere:x-forwarded-to:x-forwarded-for:delivered-to:from:to:cc :subject:date:message-id:x-mailer:in-reply-to:references :mime-version:x-gm-message-state:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-google-group-id:list-post:list-help:list-archive:list-unsubscribe :content-type:content-transfer-encoding; bh=8pYkLGGrU7lRcKfDet/Vbx6n3gDWFQTrOn8Fbd8vQjc=; b=hFFRmj8ngzKJiDPFCaHCXjCmQxyQCr728It1rm4PduUJW7noDmAFGMJbf46GYJwqcl KhwlFNuefwM8gBO4k33suvdXuPXDQuMnO7xdll4JdVBjLoYRC0VPQ/0e1fXw6nVvWTVB B2tvRm2pifkMVC1xND16OZ6GDoEJPm2gYtUFReUucMcytIvLyFHIk3ZpQNCG4YvBT2vs jv7t7/Sbpvh2No+AAwx+NBylY7/I3pqa6q3LqlpnqOu96Y1ed8Pit5YUhvD/YL6cPTth kyVmop/CtJRGGqxsxAWq8klmBw+Gt72/oanBJyDxwEZRHfxdkr1SYNtyei5yFAbfKXl2 o4VQ== X-Received: by 10.180.12.200 with SMTP id a8mr667999wic.1.1369441221429; Fri, 24 May 2013 17:20:21 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.180.99.234 with SMTP id et10ls389459wib.12.gmail; Fri, 24 May 2013 17:20:21 -0700 (PDT) X-Received: by 10.180.187.234 with SMTP id fv10mr924754wic.29.1369441221278; Fri, 24 May 2013 17:20:21 -0700 (PDT) Received: from mail-ve0-x233.google.com (mail-ve0-x233.google.com [2607:f8b0:400c:c01::233]) by mx.google.com with ESMTPS id cd2si823239wjc.121.2013.05.24.17.20.20 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 24 May 2013 17:20:20 -0700 (PDT) Received-SPF: neutral (google.com: 2607:f8b0:400c:c01::233 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=2607:f8b0:400c:c01::233; Received: by mail-ve0-f179.google.com with SMTP id d10so1615494vea.10 for ; Fri, 24 May 2013 17:20:19 -0700 (PDT) X-Received: by 10.52.163.207 with SMTP id yk15mr8490726vdb.42.1369441219757; Fri, 24 May 2013 17:20:19 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.126.138 with SMTP id c10csp123663vcs; Fri, 24 May 2013 17:20:19 -0700 (PDT) X-Received: by 10.66.148.201 with SMTP id tu9mr20184893pab.56.1369441218760; Fri, 24 May 2013 17:20:18 -0700 (PDT) Received: from mail-pd0-f173.google.com (mail-pd0-f173.google.com [209.85.192.173]) by mx.google.com with ESMTPS id o4si14259921pac.221.2013.05.24.17.20.18 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 24 May 2013 17:20:18 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.192.173 is neither permitted nor denied by best guess record for domain of john.stultz@linaro.org) client-ip=209.85.192.173; Received: by mail-pd0-f173.google.com with SMTP id v14so3466618pde.18 for ; Fri, 24 May 2013 17:20:18 -0700 (PDT) X-Received: by 10.66.230.228 with SMTP id tb4mr20113476pac.160.1369441218304; Fri, 24 May 2013 17:20:18 -0700 (PDT) Received: from localhost.localdomain (c-24-21-54-107.hsd1.or.comcast.net. [24.21.54.107]) by mx.google.com with ESMTPSA id 3sm18032243pbj.46.2013.05.24.17.20.16 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 24 May 2013 17:20:17 -0700 (PDT) From: John Stultz To: Jesse Barker Cc: John Stultz , Arnd Bergmann , Serban Constantinescu Subject: [PATCH 3/4] ion: Add carveout heap Date: Fri, 24 May 2013 17:19:44 -0700 Message-Id: <1369441185-3434-4-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1369441185-3434-1-git-send-email-john.stultz@linaro.org> References: <1369441185-3434-1-git-send-email-john.stultz@linaro.org> MIME-Version: 1.0 X-Gm-Message-State: ALoCoQnERBJdOetGoxEHgUY0Vf8O2eyfPNyZ6tMthMnHWGEeBEiHd2IPtUU1UIShrxtbNzejqZhq X-Original-Sender: john.stultz@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 2607:f8b0:400c:c01::233 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Add carveout heap to ion subsystem. This provides physically contiguous memory for devices like cameras and secure media playback codecs. XXX: This needs much more rational and justification. This work was originally by Rebecca Schultz Zavin And contains fixes and improvements by: Arve Hjønnevåg Benjamin Gaignard Colin Cross Dima Zavin Greg Hackmann Jesse Barker Johan Mossberg JP Abgrall KyongHo Cho Laura Abbott Olav Haugan Cc: Jesse Barker Cc: Arnd Bergmann Cc: Serban Constantinescu Signed-off-by: John Stultz --- drivers/gpu/ion/Makefile | 3 +- drivers/gpu/ion/ion_carveout_heap.c | 162 ++++++++++++++++++++++++++++++++++++ drivers/gpu/ion/ion_heap.c | 6 ++ include/linux/ion.h | 5 ++ 4 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/ion/ion_carveout_heap.c diff --git a/drivers/gpu/ion/Makefile b/drivers/gpu/ion/Makefile index cc144dc..dc788e1 100644 --- a/drivers/gpu/ion/Makefile +++ b/drivers/gpu/ion/Makefile @@ -1 +1,2 @@ -obj-$(CONFIG_ION) += ion.o ion_heap.o ion_page_pool.o ion_system_heap.o +obj-$(CONFIG_ION) += ion.o ion_heap.o ion_page_pool.o ion_system_heap.o \ + ion_carveout_heap.o diff --git a/drivers/gpu/ion/ion_carveout_heap.c b/drivers/gpu/ion/ion_carveout_heap.c new file mode 100644 index 0000000..8145b42 --- /dev/null +++ b/drivers/gpu/ion/ion_carveout_heap.c @@ -0,0 +1,162 @@ +/* + * drivers/gpu/ion/ion_carveout_heap.c + * + * Copyright (C) 2011 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * 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. + * + */ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include "ion_priv.h" + +/* #include */ +#define MT_MEMORY 9 +#define MT_MEMORY_NONCACHED 11 + +struct ion_carveout_heap { + struct ion_heap heap; + struct gen_pool *pool; + ion_phys_addr_t base; +}; + +ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, + unsigned long size, + unsigned long align) +{ + struct ion_carveout_heap *carveout_heap = + container_of(heap, struct ion_carveout_heap, heap); + unsigned long offset = gen_pool_alloc(carveout_heap->pool, size); + + if (!offset) + return ION_CARVEOUT_ALLOCATE_FAIL; + + return offset; +} + +void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr, + unsigned long size) +{ + struct ion_carveout_heap *carveout_heap = + container_of(heap, struct ion_carveout_heap, heap); + + if (addr == ION_CARVEOUT_ALLOCATE_FAIL) + return; + gen_pool_free(carveout_heap->pool, addr, size); +} + +static int ion_carveout_heap_phys(struct ion_heap *heap, + struct ion_buffer *buffer, + ion_phys_addr_t *addr, size_t *len) +{ + *addr = buffer->priv_phys; + *len = buffer->size; + return 0; +} + +static int ion_carveout_heap_allocate(struct ion_heap *heap, + struct ion_buffer *buffer, + unsigned long size, unsigned long align, + unsigned long flags) +{ + buffer->priv_phys = ion_carveout_allocate(heap, size, align); + return buffer->priv_phys == ION_CARVEOUT_ALLOCATE_FAIL ? -ENOMEM : 0; +} + +static void ion_carveout_heap_free(struct ion_buffer *buffer) +{ + struct ion_heap *heap = buffer->heap; + + ion_carveout_free(heap, buffer->priv_phys, buffer->size); + buffer->priv_phys = ION_CARVEOUT_ALLOCATE_FAIL; +} + +struct sg_table *ion_carveout_heap_map_dma(struct ion_heap *heap, + struct ion_buffer *buffer) +{ + struct sg_table *table; + int ret; + + table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); + if (!table) + return ERR_PTR(-ENOMEM); + ret = sg_alloc_table(table, 1, GFP_KERNEL); + if (ret) { + kfree(table); + return ERR_PTR(ret); + } + sg_set_page(table->sgl, phys_to_page(buffer->priv_phys), buffer->size, + 0); + return table; +} + +void ion_carveout_heap_unmap_dma(struct ion_heap *heap, + struct ion_buffer *buffer) +{ + sg_free_table(buffer->sg_table); +} + +int ion_carveout_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer, + struct vm_area_struct *vma) +{ + return remap_pfn_range(vma, vma->vm_start, + __phys_to_pfn(buffer->priv_phys) + vma->vm_pgoff, + vma->vm_end - vma->vm_start, + pgprot_noncached(vma->vm_page_prot)); +} + +static struct ion_heap_ops carveout_heap_ops = { + .allocate = ion_carveout_heap_allocate, + .free = ion_carveout_heap_free, + .phys = ion_carveout_heap_phys, + .map_dma = ion_carveout_heap_map_dma, + .unmap_dma = ion_carveout_heap_unmap_dma, + .map_user = ion_carveout_heap_map_user, +}; + +struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *heap_data) +{ + struct ion_carveout_heap *carveout_heap; + + carveout_heap = kzalloc(sizeof(struct ion_carveout_heap), GFP_KERNEL); + if (!carveout_heap) + return ERR_PTR(-ENOMEM); + + carveout_heap->pool = gen_pool_create(12, -1); + if (!carveout_heap->pool) { + kfree(carveout_heap); + return ERR_PTR(-ENOMEM); + } + carveout_heap->base = heap_data->base; + gen_pool_add(carveout_heap->pool, carveout_heap->base, heap_data->size, + -1); + carveout_heap->heap.ops = &carveout_heap_ops; + carveout_heap->heap.type = ION_HEAP_TYPE_CARVEOUT; + + return &carveout_heap->heap; +} + +void ion_carveout_heap_destroy(struct ion_heap *heap) +{ + struct ion_carveout_heap *carveout_heap = + container_of(heap, struct ion_carveout_heap, heap); + + gen_pool_destroy(carveout_heap->pool); + kfree(carveout_heap); + carveout_heap = NULL; +} diff --git a/drivers/gpu/ion/ion_heap.c b/drivers/gpu/ion/ion_heap.c index 0f2e7bb..3a565fc 100644 --- a/drivers/gpu/ion/ion_heap.c +++ b/drivers/gpu/ion/ion_heap.c @@ -101,6 +101,9 @@ struct ion_heap *ion_heap_create(struct ion_platform_heap *heap_data) case ION_HEAP_TYPE_SYSTEM: heap = ion_system_heap_create(heap_data); break; + case ION_HEAP_TYPE_CARVEOUT: + heap = ion_carveout_heap_create(heap_data); + break; default: pr_err("%s: Invalid heap type %d\n", __func__, heap_data->type); @@ -131,6 +134,9 @@ void ion_heap_destroy(struct ion_heap *heap) case ION_HEAP_TYPE_SYSTEM: ion_system_heap_destroy(heap); break; + case ION_HEAP_TYPE_CARVEOUT: + ion_carveout_heap_destroy(heap); + break; default: pr_err("%s: Invalid heap type %d\n", __func__, heap->type); diff --git a/include/linux/ion.h b/include/linux/ion.h index a4adac4..1ef8a0d 100644 --- a/include/linux/ion.h +++ b/include/linux/ion.h @@ -24,6 +24,9 @@ struct ion_handle; * enum ion_heap_types - list of all possible types of heaps * @ION_HEAP_TYPE_SYSTEM: memory allocated via vmalloc * @ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kmalloc + * @ION_HEAP_TYPE_CARVEOUT: memory allocated from a prereserved + * carveout heap, allocations are physically + * contiguous * @ION_NUM_HEAPS: helper for iterating over heaps, a bit mask * is used to identify the heaps, so only 32 * total heap types are supported @@ -31,11 +34,13 @@ struct ion_handle; enum ion_heap_type { ION_HEAP_TYPE_SYSTEM, ION_HEAP_TYPE_SYSTEM_CONTIG, + ION_HEAP_TYPE_CARVEOUT, ION_NUM_HEAPS = 16, }; #define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM) #define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG) +#define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT) #define ION_NUM_HEAP_IDS sizeof(unsigned int) * 8