busdma_bufalloc.h revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012 Ian Lepore
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29/*
30 * $FreeBSD: stable/11/sys/sys/busdma_bufalloc.h 330897 2018-03-14 03:19:51Z eadler $
31 */
32
33/*
34 * A buffer pool manager, for use by a platform's busdma implementation.
35 */
36
37#ifndef _MACHINE_BUSDMA_BUFALLOC_H_
38#define _MACHINE_BUSDMA_BUFALLOC_H_
39
40#include <machine/bus.h>
41#include <vm/uma.h>
42
43/*
44 * Information about a buffer zone, returned by busdma_bufalloc_findzone().
45 */
46struct busdma_bufzone {
47	bus_size_t	size;
48	uma_zone_t	umazone;
49	char		name[24];
50};
51
52/*
53 * Opaque handle type returned by busdma_bufalloc_create().
54 */
55struct busdma_bufalloc;
56typedef struct busdma_bufalloc *busdma_bufalloc_t;
57
58/*
59 * Create an allocator that manages a pool of DMA buffers.
60 *
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.
68 *
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).
72 *
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.
75 *
76 * If 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.
82 */
83busdma_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);
87
88/*
89 * Destroy an allocator created by busdma_bufalloc_create().
90 * Safe to call with a NULL pointer.
91 */
92void busdma_bufalloc_destroy(busdma_bufalloc_t ba);
93
94/*
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.
98 */
99struct busdma_bufzone * busdma_bufalloc_findzone(busdma_bufalloc_t ba,
100    bus_size_t size);
101
102/*
103 * These built-in allocation routines are available for managing a pools of
104 * uncacheable memory on platforms that support VM_MEMATTR_UNCACHEABLE.
105 *
106 * Allocation is done using kmem_alloc_attr() with these parameters:
107 *   lowaddr  = 0
108 *   highaddr = BUS_SPACE_MAXADDR
109 *   memattr  = VM_MEMATTR_UNCACHEABLE.
110 *
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.
114 */
115void * busdma_bufalloc_alloc_uncacheable(uma_zone_t zone, vm_size_t size,
116    uint8_t *pflag, int wait);
117void  busdma_bufalloc_free_uncacheable(void *item, vm_size_t size,
118    uint8_t pflag);
119
120#endif	/* _MACHINE_BUSDMA_BUFALLOC_H_ */
121
122