1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/cdefs.h>
30 /*
31 * Buffer allocation support routines for bus_dmamem_alloc implementations.
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/busdma_bufalloc.h>
38 #include <sys/domainset.h>
39 #include <sys/malloc.h>
40
41 #include <vm/vm.h>
42 #include <vm/vm_extern.h>
43 #include <vm/vm_kern.h>
44 #include <vm/uma.h>
45
46 /*
47 * We manage buffer zones up to a page in size. Buffers larger than a page can
48 * be managed by one of the kernel's page-oriented memory allocation routines as
49 * efficiently as what we can do here. Also, a page is the largest size for
50 * which we can g'tee contiguity when using uma, and contiguity is one of the
51 * requirements we have to fulfill.
52 */
53 #define MIN_ZONE_BUFSIZE 32
54 #define MAX_ZONE_BUFSIZE PAGE_SIZE
55
56 /*
57 * The static array of 12 bufzones is big enough to handle all the zones for the
58 * smallest supported allocation size of 32 through the largest supported page
59 * size of 64K. If you up the biggest page size number, up the array size too.
60 * Basically the size of the array needs to be log2(maxsize)-log2(minsize)+1,
61 * but I don't know of an easy way to express that as a compile-time constant.
62 */
63 #if PAGE_SIZE > 65536
64 #error Unsupported page size
65 #endif
66
67 struct busdma_bufalloc {
68 bus_size_t min_size;
69 size_t num_zones;
70 struct busdma_bufzone buf_zones[12];
71 };
72
73 busdma_bufalloc_t
busdma_bufalloc_create(const char * name,bus_size_t minimum_alignment,uma_alloc alloc_func,uma_free free_func,uint32_t zcreate_flags)74 busdma_bufalloc_create(const char *name, bus_size_t minimum_alignment,
75 uma_alloc alloc_func, uma_free free_func, uint32_t zcreate_flags)
76 {
77 struct busdma_bufalloc *ba;
78 struct busdma_bufzone *bz;
79 int i;
80 bus_size_t cursize;
81
82 ba = malloc(sizeof(struct busdma_bufalloc), M_DEVBUF,
83 M_ZERO | M_WAITOK);
84
85 ba->min_size = MAX(MIN_ZONE_BUFSIZE, minimum_alignment);
86
87 /*
88 * Each uma zone is created with an alignment of size-1, meaning that
89 * the alignment is equal to the size (I.E., 64 byte buffers are aligned
90 * to 64 byte boundaries, etc). This allows for a fast efficient test
91 * when deciding whether a pool buffer meets the constraints of a given
92 * tag used for allocation: the buffer is usable if tag->alignment <=
93 * bufzone->size.
94 */
95 for (i = 0, bz = ba->buf_zones, cursize = ba->min_size;
96 i < nitems(ba->buf_zones) && cursize <= MAX_ZONE_BUFSIZE;
97 ++i, ++bz, cursize <<= 1) {
98 snprintf(bz->name, sizeof(bz->name), "dma %.10s %ju",
99 name, (uintmax_t)cursize);
100 bz->size = cursize;
101 bz->umazone = uma_zcreate(bz->name, bz->size,
102 NULL, NULL, NULL, NULL, bz->size - 1, zcreate_flags);
103 if (alloc_func != NULL)
104 uma_zone_set_allocf(bz->umazone, alloc_func);
105 if (free_func != NULL)
106 uma_zone_set_freef(bz->umazone, free_func);
107 ++ba->num_zones;
108 }
109
110 return (ba);
111 }
112
113 void
busdma_bufalloc_destroy(busdma_bufalloc_t ba)114 busdma_bufalloc_destroy(busdma_bufalloc_t ba)
115 {
116 struct busdma_bufzone *bz;
117 int i;
118
119 if (ba == NULL)
120 return;
121
122 for (i = 0, bz = ba->buf_zones; i < ba->num_zones; ++i, ++bz) {
123 uma_zdestroy(bz->umazone);
124 }
125
126 free(ba, M_DEVBUF);
127 }
128
129 struct busdma_bufzone *
busdma_bufalloc_findzone(busdma_bufalloc_t ba,bus_size_t size)130 busdma_bufalloc_findzone(busdma_bufalloc_t ba, bus_size_t size)
131 {
132 struct busdma_bufzone *bz;
133 int i;
134
135 if (size > MAX_ZONE_BUFSIZE)
136 return (NULL);
137
138 for (i = 0, bz = ba->buf_zones; i < ba->num_zones; ++i, ++bz) {
139 if (bz->size >= size)
140 return (bz);
141 }
142
143 panic("Didn't find a buffer zone of the right size");
144 }
145
146 void *
busdma_bufalloc_alloc_uncacheable(uma_zone_t zone,vm_size_t size,int domain,uint8_t * pflag,int wait)147 busdma_bufalloc_alloc_uncacheable(uma_zone_t zone, vm_size_t size, int domain,
148 uint8_t *pflag, int wait)
149 {
150
151 #ifdef VM_MEMATTR_UNCACHEABLE
152 /* Inform UMA that this allocator uses kernel_arena/object. */
153 *pflag = UMA_SLAB_KERNEL;
154
155 return (kmem_alloc_attr_domainset(DOMAINSET_FIXED(domain), size,
156 wait, 0, BUS_SPACE_MAXADDR, VM_MEMATTR_UNCACHEABLE));
157 #else
158 panic("VM_MEMATTR_UNCACHEABLE unavailable");
159 #endif /* VM_MEMATTR_UNCACHEABLE */
160 }
161
162 void
busdma_bufalloc_free_uncacheable(void * item,vm_size_t size,uint8_t pflag)163 busdma_bufalloc_free_uncacheable(void *item, vm_size_t size, uint8_t pflag)
164 {
165
166 kmem_free(item, size);
167 }
168