From patchwork Thu Aug 19 14:40:22 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guennadi Liakhovetski X-Patchwork-Id: 120366 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.4/8.14.3) with ESMTP id o7JEehjk030356 for ; Thu, 19 Aug 2010 14:40:43 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753802Ab0HSOkP (ORCPT ); Thu, 19 Aug 2010 10:40:15 -0400 Received: from mailout-de.gmx.net ([213.165.64.22]:44250 "HELO mail.gmx.net" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with SMTP id S1753798Ab0HSOkM (ORCPT ); Thu, 19 Aug 2010 10:40:12 -0400 Received: (qmail invoked by alias); 19 Aug 2010 14:40:10 -0000 Received: from p508982A3.dip0.t-ipconnect.de (EHLO axis700.grange) [80.137.130.163] by mail.gmx.net (mp064) with SMTP; 19 Aug 2010 16:40:10 +0200 X-Authenticated: #20450766 X-Provags-ID: V01U2FsdGVkX18UgzU/MV4US62URTdVEmEnMzerFJhf6hMEHqjqkx 5XVGXJo1mNdcsY Received: from lyakh (helo=localhost) by axis700.grange with local-esmtp (Exim 4.63) (envelope-from ) id 1Om6I2-0007dE-U5; Thu, 19 Aug 2010 16:40:22 +0200 Date: Thu, 19 Aug 2010 16:40:22 +0200 (CEST) From: Guennadi Liakhovetski To: Russell King - ARM Linux cc: linux-sh@vger.kernel.org, Marin Mitov , =?ISO-8859-15?Q?Uwe_Kleine-K=F6nig?= , =?ISO-8859-15?Q?Philippe_R=E9tornaz?= , Greg Kroah-Hartman , linux-arm-kernel@lists.infradead.org, Janusz Krzysztofik Subject: [PATCH 4/5] DMA: Add functions to preallocate DMA memory dor devices In-Reply-To: Message-ID: References: MIME-Version: 1.0 X-Y-GMX-Trusted: 0 Sender: linux-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Thu, 19 Aug 2010 14:40:44 +0000 (UTC) diff --git a/drivers/base/dma-coherent.c b/drivers/base/dma-coherent.c index 8efdfd4..9eb2856 100644 --- a/drivers/base/dma-coherent.c +++ b/drivers/base/dma-coherent.c @@ -170,3 +170,53 @@ int dma_release_from_coherent(struct device *dev, int order, void *vaddr) return 0; } EXPORT_SYMBOL(dma_release_from_coherent); + +int dma_preallocate_coherent_memory(struct device *dev, size_t size, gfp_t gfp, + int flags) +{ + int pages = size >> PAGE_SHIFT; + int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); + dma_addr_t dma_handle; + void *buf; + + if (!(flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) || !dev) + return -EINVAL; + + if (!size) + return 0; + + buf = dma_alloc_coherent(NULL, size, &dma_handle, gfp); + if (!buf) + return -ENOMEM; + + dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem) + bitmap_size, gfp); + if (!dev->dma_mem) + goto ealloc; + + dev->dma_mem->virt_base = buf; + dev->dma_mem->device_base = dma_handle; + dev->dma_mem->size = pages; + dev->dma_mem->flags = flags; + + return 0; + +ealloc: + dma_free_coherent(dev, size, buf, dma_handle); + + return -ENOMEM; +} +EXPORT_SYMBOL(dma_preallocate_coherent_memory); + +void dma_release_preallocated_memory(struct device *dev) +{ + struct dma_coherent_mem *mem = dev->dma_mem; + + if (!mem) + return; + + dev->dma_mem = NULL; + dma_free_coherent(dev, mem->size << PAGE_SHIFT, mem->virt_base, + mem->device_base); + kfree(mem); +} +EXPORT_SYMBOL(dma_release_preallocated_memory); diff --git a/include/asm-generic/dma-coherent.h b/include/asm-generic/dma-coherent.h index a92bc09..08a66a1 100644 --- a/include/asm-generic/dma-coherent.h +++ b/include/asm-generic/dma-coherent.h @@ -21,6 +21,8 @@ void dma_release_declared_memory(struct device *dev); void *dma_mark_declared_memory_occupied(struct device *dev, dma_addr_t device_addr, size_t size); +int dma_preallocate_coherent_memory(struct device *dev, size_t size, gfp_t gfp, int flags); +void dma_release_preallocated_memory(struct device *dev); #else #define dma_alloc_from_coherent(dev, size, handle, ret) (0) #define dma_release_from_coherent(dev, order, vaddr) (0) diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 77ce97f..0912abf 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -177,6 +177,18 @@ dma_mark_declared_memory_occupied(struct device *dev, { return ERR_PTR(-EBUSY); } + +static inline int +dma_preallocate_coherent_memory(struct device *dev, size_t size, gfp_t gfp, int flags) +{ + return -EINVAL; +} + +static inline void +dma_release_preallocated_memory(struct device *dev) +{ +} + #endif /*