1a9643ea8Slogwang /*- 2*22ce4affSfengbojiang * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*22ce4affSfengbojiang * 4a9643ea8Slogwang * Copyright (c) 2012 Ian Lepore 5a9643ea8Slogwang * All rights reserved. 6a9643ea8Slogwang * 7a9643ea8Slogwang * Redistribution and use in source and binary forms, with or without 8a9643ea8Slogwang * modification, are permitted provided that the following conditions 9a9643ea8Slogwang * are met: 10a9643ea8Slogwang * 1. Redistributions of source code must retain the above copyright 11a9643ea8Slogwang * notice, this list of conditions and the following disclaimer. 12a9643ea8Slogwang * 2. Redistributions in binary form must reproduce the above copyright 13a9643ea8Slogwang * notice, this list of conditions and the following disclaimer in the 14a9643ea8Slogwang * documentation and/or other materials provided with the distribution. 15a9643ea8Slogwang * 16a9643ea8Slogwang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17a9643ea8Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18a9643ea8Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19a9643ea8Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20a9643ea8Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21a9643ea8Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22a9643ea8Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23a9643ea8Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24a9643ea8Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25a9643ea8Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26a9643ea8Slogwang * SUCH DAMAGE. 27a9643ea8Slogwang */ 28a9643ea8Slogwang 29a9643ea8Slogwang /* 30a9643ea8Slogwang * $FreeBSD$ 31a9643ea8Slogwang */ 32a9643ea8Slogwang 33a9643ea8Slogwang /* 34a9643ea8Slogwang * A buffer pool manager, for use by a platform's busdma implementation. 35a9643ea8Slogwang */ 36a9643ea8Slogwang 37a9643ea8Slogwang #ifndef _MACHINE_BUSDMA_BUFALLOC_H_ 38a9643ea8Slogwang #define _MACHINE_BUSDMA_BUFALLOC_H_ 39a9643ea8Slogwang 40a9643ea8Slogwang #include <machine/bus.h> 41a9643ea8Slogwang #include <vm/uma.h> 42a9643ea8Slogwang 43a9643ea8Slogwang /* 44a9643ea8Slogwang * Information about a buffer zone, returned by busdma_bufalloc_findzone(). 45a9643ea8Slogwang */ 46a9643ea8Slogwang struct busdma_bufzone { 47a9643ea8Slogwang bus_size_t size; 48a9643ea8Slogwang uma_zone_t umazone; 49a9643ea8Slogwang char name[24]; 50a9643ea8Slogwang }; 51a9643ea8Slogwang 52a9643ea8Slogwang /* 53a9643ea8Slogwang * Opaque handle type returned by busdma_bufalloc_create(). 54a9643ea8Slogwang */ 55a9643ea8Slogwang struct busdma_bufalloc; 56a9643ea8Slogwang typedef struct busdma_bufalloc *busdma_bufalloc_t; 57a9643ea8Slogwang 58a9643ea8Slogwang /* 59a9643ea8Slogwang * Create an allocator that manages a pool of DMA buffers. 60a9643ea8Slogwang * 61a9643ea8Slogwang * The allocator manages a collection of uma(9) zones of buffers in power-of-two 62a9643ea8Slogwang * sized increments ranging from minimum_alignment to the platform's PAGE_SIZE. 63a9643ea8Slogwang * The buffers within each zone are aligned on boundaries corresponding to the 64a9643ea8Slogwang * buffer size, and thus by implication each buffer is contiguous within a page 65a9643ea8Slogwang * and does not cross a power of two boundary larger than the buffer size. 66a9643ea8Slogwang * These rules are intended to make it easy for a busdma implementation to 67a9643ea8Slogwang * check whether a tag's constraints allow use of a buffer from the allocator. 68a9643ea8Slogwang * 69a9643ea8Slogwang * minimum_alignment is also the minimum buffer allocation size. For platforms 70a9643ea8Slogwang * with software-assisted cache coherency, this is typically the data cache line 71a9643ea8Slogwang * size (and MUST not be smaller than the cache line size). 72a9643ea8Slogwang * 73a9643ea8Slogwang * name appears in zone stats as 'dma name nnnnn' where 'dma' is fixed and 74a9643ea8Slogwang * 'nnnnn' is the size of buffers in that zone. 75a9643ea8Slogwang * 76*22ce4affSfengbojiang * If the alloc/free function pointers are NULL, the regular uma internal 77a9643ea8Slogwang * allocators are used (I.E., you get "plain old kernel memory"). On a platform 78a9643ea8Slogwang * with an exclusion zone that applies to all DMA operations, a custom allocator 79a9643ea8Slogwang * could be used to ensure no buffer memory is ever allocated from that zone, 80a9643ea8Slogwang * allowing the bus_dmamem_alloc() implementation to make the assumption that 81a9643ea8Slogwang * buffers provided by the allocation could never lead to the need for a bounce. 82a9643ea8Slogwang */ 83a9643ea8Slogwang busdma_bufalloc_t busdma_bufalloc_create(const char *name, 84a9643ea8Slogwang bus_size_t minimum_alignment, 85a9643ea8Slogwang uma_alloc uma_alloc_func, uma_free uma_free_func, 86a9643ea8Slogwang u_int32_t uma_zcreate_flags); 87a9643ea8Slogwang 88a9643ea8Slogwang /* 89a9643ea8Slogwang * Destroy an allocator created by busdma_bufalloc_create(). 90a9643ea8Slogwang * Safe to call with a NULL pointer. 91a9643ea8Slogwang */ 92a9643ea8Slogwang void busdma_bufalloc_destroy(busdma_bufalloc_t ba); 93a9643ea8Slogwang 94a9643ea8Slogwang /* 95a9643ea8Slogwang * Return a pointer to the busdma_bufzone that should be used to allocate or 96a9643ea8Slogwang * free a buffer of the given size. Returns NULL if the size is larger than the 97a9643ea8Slogwang * largest zone handled by the allocator. 98a9643ea8Slogwang */ 99a9643ea8Slogwang struct busdma_bufzone * busdma_bufalloc_findzone(busdma_bufalloc_t ba, 100a9643ea8Slogwang bus_size_t size); 101a9643ea8Slogwang 102a9643ea8Slogwang /* 103a9643ea8Slogwang * These built-in allocation routines are available for managing a pools of 104a9643ea8Slogwang * uncacheable memory on platforms that support VM_MEMATTR_UNCACHEABLE. 105a9643ea8Slogwang * 106a9643ea8Slogwang * Allocation is done using kmem_alloc_attr() with these parameters: 107a9643ea8Slogwang * lowaddr = 0 108a9643ea8Slogwang * highaddr = BUS_SPACE_MAXADDR 109a9643ea8Slogwang * memattr = VM_MEMATTR_UNCACHEABLE. 110a9643ea8Slogwang * 111a9643ea8Slogwang * If your platform has no exclusion region (lowaddr/highaddr), and its pmap 112a9643ea8Slogwang * routines support pmap_page_set_memattr() and the VM_MEMATTR_UNCACHEABLE flag 113a9643ea8Slogwang * you can probably use these when you need uncacheable buffers. 114a9643ea8Slogwang */ 115a9643ea8Slogwang void * busdma_bufalloc_alloc_uncacheable(uma_zone_t zone, vm_size_t size, 116*22ce4affSfengbojiang int domain, uint8_t *pflag, int wait); 117a9643ea8Slogwang void busdma_bufalloc_free_uncacheable(void *item, vm_size_t size, 118a9643ea8Slogwang uint8_t pflag); 119a9643ea8Slogwang 120a9643ea8Slogwang #endif /* _MACHINE_BUSDMA_BUFALLOC_H_ */ 121