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