xref: /freebsd-src/sys/kern/subr_busdma_bufalloc.c (revision 8ef2c021822f59a334e3926e8505c8a8e8a3475f)
105d90350SOlivier Houchard /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
405d90350SOlivier Houchard  * Copyright (c) 2012 Ian Lepore
505d90350SOlivier Houchard  * All rights reserved.
605d90350SOlivier Houchard  *
705d90350SOlivier Houchard  * Redistribution and use in source and binary forms, with or without
805d90350SOlivier Houchard  * modification, are permitted provided that the following conditions
905d90350SOlivier Houchard  * are met:
1005d90350SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
1105d90350SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
1205d90350SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
1305d90350SOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
1405d90350SOlivier Houchard  *    documentation and/or other materials provided with the distribution.
1505d90350SOlivier Houchard  *
1605d90350SOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1705d90350SOlivier Houchard  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1805d90350SOlivier Houchard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1905d90350SOlivier Houchard  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2005d90350SOlivier Houchard  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2105d90350SOlivier Houchard  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2205d90350SOlivier Houchard  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2305d90350SOlivier Houchard  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2405d90350SOlivier Houchard  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2505d90350SOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2605d90350SOlivier Houchard  * SUCH DAMAGE.
2705d90350SOlivier Houchard  */
2805d90350SOlivier Houchard 
2905d90350SOlivier Houchard #include <sys/cdefs.h>
3005d90350SOlivier Houchard /*
3105d90350SOlivier Houchard  * Buffer allocation support routines for bus_dmamem_alloc implementations.
3205d90350SOlivier Houchard  */
3305d90350SOlivier Houchard 
3405d90350SOlivier Houchard #include <sys/param.h>
3505d90350SOlivier Houchard #include <sys/systm.h>
3605d90350SOlivier Houchard #include <sys/bus.h>
3705d90350SOlivier Houchard #include <sys/busdma_bufalloc.h>
389978bd99SMark Johnston #include <sys/domainset.h>
3905d90350SOlivier Houchard #include <sys/malloc.h>
4005d90350SOlivier Houchard 
4105d90350SOlivier Houchard #include <vm/vm.h>
4205d90350SOlivier Houchard #include <vm/vm_extern.h>
4305d90350SOlivier Houchard #include <vm/vm_kern.h>
4405d90350SOlivier Houchard #include <vm/uma.h>
4505d90350SOlivier Houchard 
4605d90350SOlivier Houchard /*
4705d90350SOlivier Houchard  * We manage buffer zones up to a page in size.  Buffers larger than a page can
4805d90350SOlivier Houchard  * be managed by one of the kernel's page-oriented memory allocation routines as
4905d90350SOlivier Houchard  * efficiently as what we can do here.  Also, a page is the largest size for
5005d90350SOlivier Houchard  * which we can g'tee contiguity when using uma, and contiguity is one of the
5105d90350SOlivier Houchard  * requirements we have to fulfill.
5205d90350SOlivier Houchard  */
5305d90350SOlivier Houchard #define	MIN_ZONE_BUFSIZE	32
5405d90350SOlivier Houchard #define	MAX_ZONE_BUFSIZE	PAGE_SIZE
5505d90350SOlivier Houchard 
5605d90350SOlivier Houchard /*
5705d90350SOlivier Houchard  * The static array of 12 bufzones is big enough to handle all the zones for the
5805d90350SOlivier Houchard  * smallest supported allocation size of 32 through the largest supported page
5905d90350SOlivier Houchard  * size of 64K.  If you up the biggest page size number, up the array size too.
6005d90350SOlivier Houchard  * Basically the size of the array needs to be log2(maxsize)-log2(minsize)+1,
6105d90350SOlivier Houchard  * but I don't know of an easy way to express that as a compile-time constant.
6205d90350SOlivier Houchard  */
6305d90350SOlivier Houchard #if PAGE_SIZE > 65536
6405d90350SOlivier Houchard #error Unsupported page size
6505d90350SOlivier Houchard #endif
6605d90350SOlivier Houchard 
6705d90350SOlivier Houchard struct busdma_bufalloc {
6805d90350SOlivier Houchard 	bus_size_t		min_size;
6905d90350SOlivier Houchard 	size_t			num_zones;
7005d90350SOlivier Houchard 	struct busdma_bufzone	buf_zones[12];
7105d90350SOlivier Houchard };
7205d90350SOlivier Houchard 
7305d90350SOlivier Houchard 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)7405d90350SOlivier Houchard busdma_bufalloc_create(const char *name, bus_size_t minimum_alignment,
752cee5861SJohn Baldwin     uma_alloc alloc_func, uma_free free_func, uint32_t zcreate_flags)
7605d90350SOlivier Houchard {
7705d90350SOlivier Houchard 	struct busdma_bufalloc *ba;
7805d90350SOlivier Houchard 	struct busdma_bufzone *bz;
7905d90350SOlivier Houchard 	int i;
8005d90350SOlivier Houchard 	bus_size_t cursize;
8105d90350SOlivier Houchard 
8205d90350SOlivier Houchard 	ba = malloc(sizeof(struct busdma_bufalloc), M_DEVBUF,
8305d90350SOlivier Houchard 	    M_ZERO | M_WAITOK);
8405d90350SOlivier Houchard 
8505d90350SOlivier Houchard 	ba->min_size = MAX(MIN_ZONE_BUFSIZE, minimum_alignment);
8605d90350SOlivier Houchard 
8705d90350SOlivier Houchard 	/*
8805d90350SOlivier Houchard 	 * Each uma zone is created with an alignment of size-1, meaning that
8905d90350SOlivier Houchard 	 * the alignment is equal to the size (I.E., 64 byte buffers are aligned
9005d90350SOlivier Houchard 	 * to 64 byte boundaries, etc).  This allows for a fast efficient test
9105d90350SOlivier Houchard 	 * when deciding whether a pool buffer meets the constraints of a given
9205d90350SOlivier Houchard 	 * tag used for allocation: the buffer is usable if tag->alignment <=
9305d90350SOlivier Houchard 	 * bufzone->size.
9405d90350SOlivier Houchard 	 */
9505d90350SOlivier Houchard 	for (i = 0, bz = ba->buf_zones, cursize = ba->min_size;
9605d90350SOlivier Houchard 	    i < nitems(ba->buf_zones) && cursize <= MAX_ZONE_BUFSIZE;
9705d90350SOlivier Houchard 	    ++i, ++bz, cursize <<= 1) {
9811ead989SIan Lepore 		snprintf(bz->name, sizeof(bz->name), "dma %.10s %ju",
9911ead989SIan Lepore 		    name, (uintmax_t)cursize);
10005d90350SOlivier Houchard 		bz->size = cursize;
10105d90350SOlivier Houchard 		bz->umazone = uma_zcreate(bz->name, bz->size,
10205d90350SOlivier Houchard 		    NULL, NULL, NULL, NULL, bz->size - 1, zcreate_flags);
10305d90350SOlivier Houchard 		if (alloc_func != NULL)
10405d90350SOlivier Houchard 			uma_zone_set_allocf(bz->umazone, alloc_func);
10505d90350SOlivier Houchard 		if (free_func != NULL)
10605d90350SOlivier Houchard 			uma_zone_set_freef(bz->umazone, free_func);
10705d90350SOlivier Houchard 		++ba->num_zones;
10805d90350SOlivier Houchard 	}
10905d90350SOlivier Houchard 
11005d90350SOlivier Houchard 	return (ba);
11105d90350SOlivier Houchard }
11205d90350SOlivier Houchard 
11305d90350SOlivier Houchard void
busdma_bufalloc_destroy(busdma_bufalloc_t ba)11405d90350SOlivier Houchard busdma_bufalloc_destroy(busdma_bufalloc_t ba)
11505d90350SOlivier Houchard {
11605d90350SOlivier Houchard 	struct busdma_bufzone *bz;
11705d90350SOlivier Houchard 	int i;
11805d90350SOlivier Houchard 
11905d90350SOlivier Houchard 	if (ba == NULL)
12005d90350SOlivier Houchard 		return;
12105d90350SOlivier Houchard 
12205d90350SOlivier Houchard 	for (i = 0, bz = ba->buf_zones; i < ba->num_zones; ++i, ++bz) {
12305d90350SOlivier Houchard 		uma_zdestroy(bz->umazone);
12405d90350SOlivier Houchard 	}
12505d90350SOlivier Houchard 
12605d90350SOlivier Houchard 	free(ba, M_DEVBUF);
12705d90350SOlivier Houchard }
12805d90350SOlivier Houchard 
12905d90350SOlivier Houchard struct busdma_bufzone *
busdma_bufalloc_findzone(busdma_bufalloc_t ba,bus_size_t size)13005d90350SOlivier Houchard busdma_bufalloc_findzone(busdma_bufalloc_t ba, bus_size_t size)
13105d90350SOlivier Houchard {
13205d90350SOlivier Houchard 	struct busdma_bufzone *bz;
13305d90350SOlivier Houchard 	int i;
13405d90350SOlivier Houchard 
13505d90350SOlivier Houchard 	if (size > MAX_ZONE_BUFSIZE)
13605d90350SOlivier Houchard 		return (NULL);
13705d90350SOlivier Houchard 
13805d90350SOlivier Houchard 	for (i = 0, bz = ba->buf_zones; i < ba->num_zones; ++i, ++bz) {
13905d90350SOlivier Houchard 		if (bz->size >= size)
14005d90350SOlivier Houchard 			return (bz);
14105d90350SOlivier Houchard 	}
14205d90350SOlivier Houchard 
14305d90350SOlivier Houchard 	panic("Didn't find a buffer zone of the right size");
14405d90350SOlivier Houchard }
14505d90350SOlivier Houchard 
14605d90350SOlivier Houchard void *
busdma_bufalloc_alloc_uncacheable(uma_zone_t zone,vm_size_t size,int domain,uint8_t * pflag,int wait)147ab3185d1SJeff Roberson busdma_bufalloc_alloc_uncacheable(uma_zone_t zone, vm_size_t size, int domain,
148f2c2231eSRyan Stone     uint8_t *pflag, int wait)
14905d90350SOlivier Houchard {
15005d90350SOlivier Houchard 
1519978bd99SMark Johnston #ifdef VM_MEMATTR_UNCACHEABLE
1525df87b21SJeff Roberson 	/* Inform UMA that this allocator uses kernel_arena/object. */
15305d90350SOlivier Houchard 	*pflag = UMA_SLAB_KERNEL;
15405d90350SOlivier Houchard 
155f49fd63aSJohn Baldwin 	return (kmem_alloc_attr_domainset(DOMAINSET_FIXED(domain), size,
1569978bd99SMark Johnston 	    wait, 0, BUS_SPACE_MAXADDR, VM_MEMATTR_UNCACHEABLE));
15705d90350SOlivier Houchard #else
15805d90350SOlivier Houchard 	panic("VM_MEMATTR_UNCACHEABLE unavailable");
15905d90350SOlivier Houchard #endif	/* VM_MEMATTR_UNCACHEABLE */
16005d90350SOlivier Houchard }
16105d90350SOlivier Houchard 
16205d90350SOlivier Houchard void
busdma_bufalloc_free_uncacheable(void * item,vm_size_t size,uint8_t pflag)163f2c2231eSRyan Stone busdma_bufalloc_free_uncacheable(void *item, vm_size_t size, uint8_t pflag)
16405d90350SOlivier Houchard {
16505d90350SOlivier Houchard 
166f49fd63aSJohn Baldwin 	kmem_free(item, size);
16705d90350SOlivier Houchard }
168