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