1 /* $NetBSD: mcadma_machdep.c,v 1.1 2007/12/17 19:09:40 garbled Exp $ */
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mcadma_machdep.c,v 1.1 2007/12/17 19:09:40 garbled Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/syslog.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
43 #define _POWERPC_BUS_DMA_PRIVATE
44 #include <machine/bus.h>
46 #include <machine/pio.h>
48 #include <dev/mca/mcavar.h>
49 #include <dev/mca/mcareg.h>
51 struct rs6000_dma_cookie
{
53 uint32_t bid
; /* the BID of this IOCC */
54 uint32_t tce_off
; /* offset of the TCE */
56 uint32_t nrofslaves
; /* nrof slave channels */
57 uint32_t slave_regs
; /* allocation bitmask */
58 uint32_t dma_regs
; /* allocation bitmask */
62 * Entry points for MCA DMA. These are mostly wrappers around
63 * the generic functions that understand how to deal with bounce
64 * buffers, if necessary.
67 struct powerpc_bus_dma_tag mca_bus_dma_tag
= {
68 0, /* _bounce_thresh */
69 _mca_bus_dmamap_create
,
72 _bus_dmamap_load_mbuf
,
76 NULL
, /* _dmamap_sync */
85 * Allocate a DMA map, and set up DMA channel.
88 _mca_bus_dmamap_create(bus_dma_tag_t t
, bus_size_t size
, int flags
,
89 bus_dmamap_t
*dmamp
, int dmach
)
92 struct rs6000_dma_cookie
*cookie
;
96 if (dmach
< 0 || dmach
>= MAX_DMA_CHANNELS
) {
97 printf("mcadma_create: invalid DMA channel %d\n", dmach
);
102 panic("mca_dmamap_create: dmamap sz %ld > 65536", (long) size
);
107 * MCA DMA transfer can be maximum 65536 bytes long and must
108 * be in one chunk. No specific boundary constraints are present.
110 if ((error
= _bus_dmamap_create(t
, size
, 1, 65536, 0, flags
, dmamp
)))
113 cookie
= (struct rs6000_dma_cookie
*) (*dmamp
)->_dm_cookie
;
115 if (cookie
== NULL
) {
117 * Allocate our cookie if not yet done.
119 cookie
= malloc(sizeof(struct rs6000_dma_cookie
), M_DMAMAP
,
120 ((flags
& BUS_DMA_NOWAIT
) ? M_NOWAIT
: M_WAITOK
) | M_ZERO
);
121 if (cookie
== NULL
) {
125 (*dmamp
)->_dm_cookie
= cookie
;
128 /* Encode DMA channel */
129 cookie
->id_flags
&= 0x0f;
130 cookie
->id_flags
|= dmach
<< 4;
132 /* Mark the dmamap as using DMA controller. Some devices
133 * drive DMA themselves, and don't need the MCA DMA controller.
134 * To distinguish the two, use a flag for dmamaps which use the DMA
137 (*dmamp
)->_dm_flags
|= _MCABUS_DMA_USEDMACTRL
;
142 _mca_bus_dmamap_destroy(bus_dma_tag_t t
, bus_dmamap_t map
)
144 struct rs6000_dma_cookie
*cookie
= map
->_dm_cookie
;
146 free(cookie
, M_DMAMAP
);
147 _bus_dmamap_destroy(t
, map
);
151 * Load an MCA DMA map with a linear buffer.
154 _mca_bus_dmamap_load(bus_dma_tag_t t
, bus_dmamap_t map
, void *buf
,
155 bus_size_t buflen
, struct proc
*p
, int flags
)
157 struct rs6000_dma_cookie
*cookie
= map
->_dm_cookie
;
160 /* Make sure that on error condition we return "no valid mappings." */
164 error
= _bus_dmamap_load(t
, map
, buf
, buflen
, p
, flags
);
169 * Like _mca_bus_dmamap_load(), but for mbufs.
172 _mca_bus_dmamap_load_mbuf(bus_dma_tag_t t
, bus_dmamap_t map
, struct mbuf
*m0
,
175 struct rs6000_dma_cookie
*cookie
= map
->_dm_cookie
;
178 /* Make sure that on error condition we return "no valid mappings." */
183 if ((m0
->m_flags
& M_PKTHDR
) == 0)
184 panic("_mca_bus_dmamap_load_mbuf: no packet header");
187 if (m0
->m_pkthdr
.len
> map
->_dm_size
)
190 error
= _bus_dmamap_load_mbuf(t
, map
, m0
, flags
);
195 * Like _mca_bus_dmamap_load(), but for uios.
198 _mca_bus_dmamap_load_uio(bus_dma_tag_t t
, bus_dmamap_t map
, struct uio
*uio
,
202 panic("_mca_bus_dmamap_load_uio: not implemented");
206 * Like _mca_bus_dmamap_load(), but for raw memory allocated with
207 * bus_dmamem_alloc().
210 _mca_bus_dmamap_load_raw(bus_dma_tag_t t
, bus_dmamap_t map
,
211 bus_dma_segment_t
*segs
, int nsegs
, bus_size_t size
, int flags
)
214 panic("_mca_bus_dmamap_load_raw: not implemented");
218 * Unload an MCA DMA map.
221 _mca_bus_dmamap_unload(bus_dma_tag_t t
, bus_dmamap_t map
)
224 _bus_dmamap_unload(t
, map
);
228 * Allocate memory safe for MCA DMA.
231 _mca_bus_dmamem_alloc(bus_dma_tag_t t
, bus_size_t size
, bus_size_t alignment
,
232 bus_size_t boundary
, bus_dma_segment_t
*segs
, int nsegs
, int *rsegs
,
237 high
= trunc_page(avail_end
);
239 return _bus_dmamem_alloc_range(t
, size
, alignment
, boundary
,
240 segs
, nsegs
, rsegs
, flags
, 0, high
);