summaryrefslogtreecommitdiffstats
path: root/sys/kern/dma_alloc.c
diff options
context:
space:
mode:
authorderaadt <deraadt@openbsd.org>2010-07-13 16:47:02 +0000
committerderaadt <deraadt@openbsd.org>2010-07-13 16:47:02 +0000
commit725701172d1394e4f1faddf6ddfcbdf4c0c7c243 (patch)
tree59ad1702da1f586796a5da7ede096811e334380c /sys/kern/dma_alloc.c
parenthandle Tm, like in tiff2ps (diff)
downloadwireguard-openbsd-725701172d1394e4f1faddf6ddfcbdf4c0c7c243.tar.xz
wireguard-openbsd-725701172d1394e4f1faddf6ddfcbdf4c0c7c243.zip
dma_alloc() and dma_free(). This is a thin shim on top of a bag of
pools, sized by powers of 2, which are constrained to dma memory. ok matthew tedu thib
Diffstat (limited to 'sys/kern/dma_alloc.c')
-rw-r--r--sys/kern/dma_alloc.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/sys/kern/dma_alloc.c b/sys/kern/dma_alloc.c
new file mode 100644
index 00000000000..7c61f906ffd
--- /dev/null
+++ b/sys/kern/dma_alloc.c
@@ -0,0 +1,76 @@
+/* $OpenBSD: dma_alloc.c,v 1.1 2010/07/13 16:47:03 deraadt Exp $ */
+/*
+ * Copyright (c) 2010 Theo de Raadt <deraadt@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/param.h>
+#include <sys/pool.h>
+#include <uvm/uvm.h>
+
+static __inline int dma_alloc_index(size_t size);
+
+#define DMA_BUCKET_OFFSET 4
+static char dmanames[PAGE_SHIFT - DMA_BUCKET_OFFSET][8];
+struct pool dmapools[PAGE_SHIFT - DMA_BUCKET_OFFSET];
+
+void
+dma_alloc_init(void)
+{
+ int i;
+
+ for (i = 0; i < nitems(dmapools); i++) {
+ snprintf(dmanames[i], sizeof(dmanames[0]), "dma%d",
+ 1 << (i + DMA_BUCKET_OFFSET));
+ pool_init(&dmapools[i], 1 << (i + DMA_BUCKET_OFFSET), 0, 0, 0,
+ dmanames[i], NULL);
+ pool_set_constraints(&dmapools[i], &dma_constraint, 1);
+ /* XXX need pool_setlowat(&dmapools[i], dmalowat); */
+ }
+}
+
+static __inline int
+dma_alloc_index(size_t sz)
+{
+ int b;
+
+ for (b = 0; b < nitems(dmapools); b++)
+ if (sz <= (1 << (b + DMA_BUCKET_OFFSET)))
+ return (b);
+#ifdef DEBUG
+ printf("dma_alloc/free: object %d too large\n", sz)
+#endif
+ return (-1);
+}
+
+void *
+dma_alloc(size_t size, int prflags)
+{
+ int pi = dma_alloc_index(size);
+
+ if (pi == -1)
+ return (NULL);
+ return pool_get(&dmapools[pi], prflags);
+}
+
+
+void
+dma_free(void *m, size_t size)
+{
+ int pi = dma_alloc_index(size);
+
+ if (pi == -1)
+ return;
+ pool_put(&dmapools[pi], m);
+}