1*75b842b8Sskrll /* $NetBSD: fdt_dma_machdep.c,v 1.2 2023/05/07 12:41:48 skrll Exp $ */
2224a11ecSskrll
3224a11ecSskrll /*-
4224a11ecSskrll * Copyright (c) 2022 The NetBSD Foundation, Inc.
5224a11ecSskrll * All rights reserved.
6224a11ecSskrll *
7224a11ecSskrll * This code is derived from software contributed to The NetBSD Foundation
8224a11ecSskrll * by Nick Hudson
9224a11ecSskrll *
10224a11ecSskrll * Redistribution and use in source and binary forms, with or without
11224a11ecSskrll * modification, are permitted provided that the following conditions
12224a11ecSskrll * are met:
13224a11ecSskrll * 1. Redistributions of source code must retain the above copyright
14224a11ecSskrll * notice, this list of conditions and the following disclaimer.
15224a11ecSskrll * 2. Redistributions in binary form must reproduce the above copyright
16224a11ecSskrll * notice, this list of conditions and the following disclaimer in the
17224a11ecSskrll * documentation and/or other materials provided with the distribution.
18224a11ecSskrll *
19224a11ecSskrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20224a11ecSskrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21224a11ecSskrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22224a11ecSskrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23224a11ecSskrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24224a11ecSskrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25224a11ecSskrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26224a11ecSskrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27224a11ecSskrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28224a11ecSskrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29224a11ecSskrll * POSSIBILITY OF SUCH DAMAGE.
30224a11ecSskrll */
31224a11ecSskrll
32224a11ecSskrll #include <sys/cdefs.h>
33*75b842b8Sskrll __KERNEL_RCSID(0, "$NetBSD: fdt_dma_machdep.c,v 1.2 2023/05/07 12:41:48 skrll Exp $");
34224a11ecSskrll
35224a11ecSskrll #include <sys/param.h>
36224a11ecSskrll #include <sys/bus.h>
37224a11ecSskrll #include <sys/kmem.h>
38224a11ecSskrll
39224a11ecSskrll #include <dev/fdt/fdtvar.h>
40224a11ecSskrll
41*75b842b8Sskrll extern struct riscv_bus_dma_tag riscv_generic_dma_tag;
42*75b842b8Sskrll
43224a11ecSskrll bus_dma_tag_t
fdtbus_dma_tag_create(int phandle,const struct fdt_dma_range * ranges,u_int nranges)44224a11ecSskrll fdtbus_dma_tag_create(int phandle, const struct fdt_dma_range *ranges,
45224a11ecSskrll u_int nranges)
46224a11ecSskrll {
47*75b842b8Sskrll struct riscv_bus_dma_tag *tagp;
48*75b842b8Sskrll u_int n;
49*75b842b8Sskrll
50*75b842b8Sskrll // Check bindings.
51*75b842b8Sskrll const int flags = of_hasprop(phandle, "dma-coherent") ?
52*75b842b8Sskrll _BUS_DMAMAP_COHERENT : 0;
53*75b842b8Sskrll
54*75b842b8Sskrll tagp = kmem_alloc(sizeof(*tagp), KM_SLEEP);
55*75b842b8Sskrll *tagp = riscv_generic_dma_tag;
56*75b842b8Sskrll if (nranges == 0) {
57*75b842b8Sskrll tagp->_nranges = 1;
58*75b842b8Sskrll tagp->_ranges = kmem_alloc(sizeof(*tagp->_ranges), KM_SLEEP);
59*75b842b8Sskrll tagp->_ranges[0].dr_sysbase = 0;
60*75b842b8Sskrll tagp->_ranges[0].dr_busbase = 0;
61*75b842b8Sskrll tagp->_ranges[0].dr_len = UINTPTR_MAX;
62*75b842b8Sskrll tagp->_ranges[0].dr_flags = flags;
63*75b842b8Sskrll } else {
64*75b842b8Sskrll tagp->_nranges = nranges;
65*75b842b8Sskrll tagp->_ranges = kmem_alloc(sizeof(*tagp->_ranges) * nranges,
66*75b842b8Sskrll KM_SLEEP);
67*75b842b8Sskrll for (n = 0; n < nranges; n++) {
68*75b842b8Sskrll tagp->_ranges[n].dr_sysbase = ranges[n].dr_sysbase;
69*75b842b8Sskrll tagp->_ranges[n].dr_busbase = ranges[n].dr_busbase;
70*75b842b8Sskrll tagp->_ranges[n].dr_len = ranges[n].dr_len;
71*75b842b8Sskrll tagp->_ranges[n].dr_flags = flags;
72*75b842b8Sskrll }
73*75b842b8Sskrll }
74*75b842b8Sskrll
75*75b842b8Sskrll return tagp;
76224a11ecSskrll }
77