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