fdt_dma_machdep.c revision 1.1.6.2 1 1.1.6.2 martin /* $NetBSD: fdt_dma_machdep.c,v 1.1.6.2 2020/04/08 14:07:35 martin Exp $ */
2 1.1.6.2 martin
3 1.1.6.2 martin /*-
4 1.1.6.2 martin * Copyright (c) 2020 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1.6.2 martin * All rights reserved.
6 1.1.6.2 martin *
7 1.1.6.2 martin * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 martin * modification, are permitted provided that the following conditions
9 1.1.6.2 martin * are met:
10 1.1.6.2 martin * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 martin * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 martin * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 martin * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 martin * documentation and/or other materials provided with the distribution.
15 1.1.6.2 martin *
16 1.1.6.2 martin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.6.2 martin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.6.2 martin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.6.2 martin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.6.2 martin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1.6.2 martin * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1.6.2 martin * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1.6.2 martin * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1.6.2 martin * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.6.2 martin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.6.2 martin * SUCH DAMAGE.
27 1.1.6.2 martin */
28 1.1.6.2 martin
29 1.1.6.2 martin #include <sys/cdefs.h>
30 1.1.6.2 martin __KERNEL_RCSID(0, "$NetBSD: fdt_dma_machdep.c,v 1.1.6.2 2020/04/08 14:07:35 martin Exp $");
31 1.1.6.2 martin
32 1.1.6.2 martin #include <sys/param.h>
33 1.1.6.2 martin #include <sys/bus.h>
34 1.1.6.2 martin #include <sys/kmem.h>
35 1.1.6.2 martin
36 1.1.6.2 martin #include <dev/fdt/fdtvar.h>
37 1.1.6.2 martin
38 1.1.6.2 martin extern struct arm32_bus_dma_tag arm_generic_dma_tag;
39 1.1.6.2 martin
40 1.1.6.2 martin bus_dma_tag_t
41 1.1.6.2 martin fdtbus_dma_tag_create(int phandle, const struct fdt_dma_range *ranges,
42 1.1.6.2 martin u_int nranges)
43 1.1.6.2 martin {
44 1.1.6.2 martin struct arm32_bus_dma_tag *tagp;
45 1.1.6.2 martin u_int n;
46 1.1.6.2 martin
47 1.1.6.2 martin const int flags = of_hasprop(phandle, "dma-coherent") ?
48 1.1.6.2 martin _BUS_DMAMAP_COHERENT : 0;
49 1.1.6.2 martin
50 1.1.6.2 martin tagp = kmem_alloc(sizeof(*tagp), KM_SLEEP);
51 1.1.6.2 martin *tagp = arm_generic_dma_tag;
52 1.1.6.2 martin if (nranges == 0) {
53 1.1.6.2 martin tagp->_nranges = 1;
54 1.1.6.2 martin tagp->_ranges = kmem_alloc(sizeof(*tagp->_ranges), KM_SLEEP);
55 1.1.6.2 martin tagp->_ranges[0].dr_sysbase = 0;
56 1.1.6.2 martin tagp->_ranges[0].dr_busbase = 0;
57 1.1.6.2 martin tagp->_ranges[0].dr_len = UINTPTR_MAX;
58 1.1.6.2 martin tagp->_ranges[0].dr_flags = flags;
59 1.1.6.2 martin } else {
60 1.1.6.2 martin tagp->_nranges = nranges;
61 1.1.6.2 martin tagp->_ranges = kmem_alloc(sizeof(*tagp->_ranges) * nranges,
62 1.1.6.2 martin KM_SLEEP);
63 1.1.6.2 martin for (n = 0; n < nranges; n++) {
64 1.1.6.2 martin tagp->_ranges[n].dr_sysbase = ranges[n].dr_sysbase;
65 1.1.6.2 martin tagp->_ranges[n].dr_busbase = ranges[n].dr_busbase;
66 1.1.6.2 martin tagp->_ranges[n].dr_len = ranges[n].dr_len;
67 1.1.6.2 martin tagp->_ranges[n].dr_flags = flags;
68 1.1.6.2 martin }
69 1.1.6.2 martin }
70 1.1.6.2 martin
71 1.1.6.2 martin return tagp;
72 1.1.6.2 martin }
73